2 % (c) The AQUA Project, Glasgow University, 1993-1996
4 \section[SimplVar]{Simplifier stuff related to variables}
9 simplBinder, simplBinders, simplTyBinder, simplTyBinders
12 #include "HsVersions.h"
14 import {-# SOURCE #-} Simplify ( simplExpr )
16 import CmdLineOpts ( switchIsOn, SimplifierSwitch(..) )
18 import CoreUnfold ( Unfolding(..), UnfoldingGuidance(..),
20 FormSummary, whnfOrBottom,
22 import Specialise ( substSpecEnvRhs )
23 import BinderInfo ( BinderInfo, noBinderInfo, okToInline )
25 import CostCentre ( CostCentre, isCurrentCostCentre )
26 import Id ( idType, getIdInfo, getIdUnfolding,
27 getIdSpecialisation, setIdSpecialisation,
28 idMustBeINLINEd, idHasNoFreeTyVars,
29 mkIdWithNewUniq, mkIdWithNewType,
30 elemIdEnv, isNullIdEnv, addOneToIdEnv
32 import SpecEnv ( lookupSpecEnv, substSpecEnv, isEmptySpecEnv )
33 import Literal ( isNoRepLit )
34 import MagicUFs ( applyMagicUnfoldingFun, MagicUnfoldingFun )
37 import Type ( instantiateTy, mkTyVarTy )
38 import TyCon ( tyConFamilySize )
39 import TyVar ( TyVar, cloneTyVar,
40 isEmptyTyVarEnv, addToTyVarEnv,
41 addOneToTyVarSet, elementOfTyVarSet
43 import Maybes ( maybeToBool )
47 %************************************************************************
49 \subsection[Simplify-var]{Completing variables}
51 %************************************************************************
53 This where all the heavy-duty unfolding stuff comes into its own.
56 completeVar env var args result_ty
58 | maybeToBool maybe_magic_result
59 = tick MagicUnfold `thenSmpl_`
62 -- Look for existing specialisations before
64 | maybeToBool maybe_specialisation
65 = tick SpecialisationDone `thenSmpl_`
66 simplExpr (bindTyVars env spec_bindings)
71 -- If there's an InUnfolding it means that there's no
72 -- let-binding left for the thing, so we'd better inline it!
75 Just (_, _, InUnfolding rhs_env rhs) = info_from_env
77 unfold var rhs_env rhs args result_ty
80 -- Conditional unfolding. There's a binding for the
81 -- thing, but perhaps we want to inline it anyway
82 | ( maybeToBool maybe_unfolding_info
83 && (not essential_unfoldings_only || idMustBeINLINEd var)
84 -- If "essential_unfoldings_only" is true we do no inlinings at all,
85 -- EXCEPT for things that absolutely have to be done
86 -- (see comments with idMustBeINLINEd)
88 && costCentreOk (getEnclosingCC env) (getEnclosingCC unf_env)
90 = pprTrace "Unfolding" (ppr var) $
91 unfold var unf_env unf_template args result_ty
95 = returnSmpl (mkGenApp (Var var) args)
98 info_from_env = lookupOutIdEnv env var
99 unfolding_from_id = getIdUnfolding var
101 ---------- Magic unfolding stuff
102 maybe_magic_result = case unfolding_from_id of
103 MagicUnfolding _ magic_fn -> applyMagicUnfoldingFun magic_fn
106 (Just magic_result) = maybe_magic_result
108 ---------- Unfolding stuff
109 must_unfold = case info_from_env of
110 Just (_, _, InUnfolding _ _) -> True
114 = case (info_from_env, unfolding_from_id) of
116 (Just (_, occ_info, OutUnfolding enc_cc unf), _)
117 -> Just (occ_info, setEnclosingCC env enc_cc, unf)
119 (_, CoreUnfolding unf)
120 -> Just (noBinderInfo, env, unf)
124 Just (occ_info, unf_env, simple_unfolding) = maybe_unfolding_info
125 SimpleUnfolding form guidance unf_template = simple_unfolding
127 ---------- Specialisation stuff
128 (ty_args, remaining_args) = initialTyArgs args
129 maybe_specialisation = lookupSpecEnv (getIdSpecialisation var) ty_args
130 Just (spec_bindings, spec_template) = maybe_specialisation
134 sw_chkr = getSwitchChecker env
135 essential_unfoldings_only = switchIsOn sw_chkr EssentialUnfoldingsOnly
136 is_case_scrutinee = switchIsOn sw_chkr SimplCaseScrutinee
137 ok_to_inline = okToInline (whnfOrBottom form) small_enough occ_info
138 small_enough = smallEnoughToInline arg_evals is_case_scrutinee guidance
139 arg_evals = [is_evald arg | arg <- args, isValArg arg]
141 is_evald (VarArg v) = isEvaluated (lookupRhsInfo env v)
142 is_evald (LitArg l) = True
145 -- Perform the unfolding
146 unfold var unf_env unf_template args result_ty
149 simplCount `thenSmpl` \ n ->
151 pprTrace "Ticks > 1000 and unfolding" (sep [space, int n, ppr var])
156 returnSmpl (mkGenApp (Var var) args)
159 tickUnfold var `thenSmpl_`
160 simplExpr unf_env unf_template args result_ty
163 -- costCentreOk checks that it's ok to inline this thing
164 -- The time it *isn't* is this:
166 -- f x = let y = E in
167 -- scc "foo" (...y...)
169 -- Here y has a "current cost centre", and we can't inline it inside "foo",
170 -- regardless of whether E is a WHNF or not.
172 costCentreOk cc_encl cc_rhs
173 = isCurrentCostCentre cc_encl || not (isCurrentCostCentre cc_rhs)
177 %************************************************************************
179 \section{Dealing with a single binder}
181 %************************************************************************
183 When we hit a binder we may need to
184 (a) apply the the type envt (if non-empty) to its type
185 (b) apply the type envt and id envt to its SpecEnv (if it has one)
186 (c) give it a new unique to avoid name clashes
189 simplBinder :: SimplEnv -> InBinder -> SmplM (SimplEnv, OutId)
190 simplBinder env (id, _)
191 | not_in_scope -- Not in scope, so no need to clone
192 && empty_ty_subst -- No type substitution to do inside the Id
193 && isNullIdEnv id_subst -- No id substitution to do inside the Id
195 env' = setIdEnv env (addOneToIdEnv in_scope_ids id id, id_subst)
197 returnSmpl (env', id)
202 -- I reckon the empty-env thing should catch
203 -- most no-free-tyvars things, so this test should be redundant
204 (if idHasNoFreeTyVars id then pprTrace "applyEnvsToId" (ppr id) else (\x -> x))
207 -- id1 has its type zapped
208 id1 | empty_ty_subst = id
209 | otherwise = mkIdWithNewType id ty'
211 -- id2 has its SpecEnv zapped
212 id2 | isEmptySpecEnv spec_env = id1
213 | otherwise = setIdSpecialisation id spec_env'
218 env' = setIdEnv env (addOneToIdEnv in_scope_ids id id2, id_subst)
220 returnSmpl (env', id2)
223 getUniqueSmpl `thenSmpl` \ uniq ->
225 id3 = mkIdWithNewUniq id2 uniq
226 env' = setIdEnv env (addOneToIdEnv in_scope_ids id3 id3,
227 addOneToIdEnv id_subst id (VarArg id3))
229 returnSmpl (env', id3)
232 ((in_scope_tyvars, ty_subst), (in_scope_ids, id_subst)) = getSubstEnvs env
233 empty_ty_subst = isEmptyTyVarEnv ty_subst
234 not_in_scope = not (id `elemIdEnv` in_scope_ids)
237 ty' = instantiateTy ty_subst ty
239 spec_env = getIdSpecialisation id
240 spec_env' = substSpecEnv ty_subst (substSpecEnvRhs ty_subst id_subst) spec_env
242 simplBinders :: SimplEnv -> [InBinder] -> SmplM (SimplEnv, [OutId])
243 simplBinders env binders = mapAccumLSmpl simplBinder env binders
247 simplTyBinder :: SimplEnv -> TyVar -> SmplM (SimplEnv, TyVar)
248 simplTyBinder env tyvar
249 | not (tyvar `elementOfTyVarSet` tyvars) -- No need to clone
251 env' = setTyEnv env (tyvars `addOneToTyVarSet` tyvar, ty_subst)
253 returnSmpl (env', tyvar)
255 | otherwise -- Need to clone
256 = getUniqueSmpl `thenSmpl` \ uniq ->
258 tyvar' = cloneTyVar tyvar uniq
259 env' = setTyEnv env (tyvars `addOneToTyVarSet` tyvar',
260 addToTyVarEnv ty_subst tyvar (mkTyVarTy tyvar'))
262 returnSmpl (env', tyvar')
264 ((tyvars, ty_subst), (ids, id_subst)) = getSubstEnvs env
266 simplTyBinders :: SimplEnv -> [TyVar] -> SmplM (SimplEnv, [TyVar])
267 simplTyBinders env binders = mapAccumLSmpl simplTyBinder env binders