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 OccurAnal ( occurAnalyseGlobalExpr )
34 import Literal ( isNoRepLit )
35 import MagicUFs ( applyMagicUnfoldingFun, MagicUnfoldingFun )
38 import Type ( instantiateTy, mkTyVarTy )
39 import TyCon ( tyConFamilySize )
40 import TyVar ( TyVar, cloneTyVar,
41 isEmptyTyVarEnv, addToTyVarEnv,
42 addOneToTyVarSet, elementOfTyVarSet
44 import Maybes ( maybeToBool )
48 %************************************************************************
50 \subsection[Simplify-var]{Completing variables}
52 %************************************************************************
54 This where all the heavy-duty unfolding stuff comes into its own.
57 completeVar env var args result_ty
59 | maybeToBool maybe_magic_result
60 = tick MagicUnfold `thenSmpl_`
63 -- Look for existing specialisations before
65 | maybeToBool maybe_specialisation
66 = tick SpecialisationDone `thenSmpl_`
67 simplExpr (bindTyVars env spec_bindings)
68 (occurAnalyseGlobalExpr spec_template)
73 -- Look for an unfolding. There's a binding for the
74 -- thing, but perhaps we want to inline it anyway
75 | ( maybeToBool maybe_unfolding_info
76 && (not essential_unfoldings_only || idMustBeINLINEd var)
77 -- If "essential_unfoldings_only" is true we do no inlinings at all,
78 -- EXCEPT for things that absolutely have to be done
79 -- (see comments with idMustBeINLINEd)
81 && costCentreOk (getEnclosingCC env) (getEnclosingCC unf_env)
83 = -- pprTrace "Unfolding" (ppr var) $
84 unfold var unf_env unf_template args result_ty
88 = returnSmpl (mkGenApp (Var var') args)
91 info_from_env = lookupOutIdEnv env var
92 var' = case info_from_env of
93 Just (var', _, _) -> var'
96 unfolding_from_id = getIdUnfolding var
98 ---------- Magic unfolding stuff
99 maybe_magic_result = case unfolding_from_id of
100 MagicUnfolding _ magic_fn -> applyMagicUnfoldingFun magic_fn
103 Just magic_result = maybe_magic_result
106 = case (info_from_env, unfolding_from_id) of
108 (Just (_, occ_info, OutUnfolding enc_cc unf), _)
109 -> Just (occ_info, setEnclosingCC env enc_cc, unf)
111 (_, CoreUnfolding unf)
112 -> Just (noBinderInfo, env, unf)
116 Just (occ_info, unf_env, simple_unfolding) = maybe_unfolding_info
117 SimpleUnfolding form guidance unf_template = simple_unfolding
119 ---------- Specialisation stuff
120 (ty_args, remaining_args) = initialTyArgs args
121 maybe_specialisation = lookupSpecEnv (getIdSpecialisation var) ty_args
122 Just (spec_bindings, spec_template) = maybe_specialisation
126 sw_chkr = getSwitchChecker env
127 essential_unfoldings_only = switchIsOn sw_chkr EssentialUnfoldingsOnly
128 is_case_scrutinee = switchIsOn sw_chkr SimplCaseScrutinee
129 ok_to_inline = okToInline (whnfOrBottom form) small_enough occ_info
130 small_enough = smallEnoughToInline var arg_evals is_case_scrutinee guidance
131 arg_evals = [is_evald arg | arg <- args, isValArg arg]
133 is_evald (VarArg v) = isEvaluated (lookupRhsInfo env v)
134 is_evald (LitArg l) = True
137 -- Perform the unfolding
138 unfold var unf_env unf_template args result_ty
141 simplCount `thenSmpl` \ n ->
143 pprTrace "Ticks > 1000 and unfolding" (sep [space, int n, ppr var])
148 returnSmpl (mkGenApp (Var var) args)
151 tickUnfold var `thenSmpl_`
152 simplExpr unf_env unf_template args result_ty
155 -- costCentreOk checks that it's ok to inline this thing
156 -- The time it *isn't* is this:
158 -- f x = let y = E in
159 -- scc "foo" (...y...)
161 -- Here y has a "current cost centre", and we can't inline it inside "foo",
162 -- regardless of whether E is a WHNF or not.
164 costCentreOk cc_encl cc_rhs
165 = isCurrentCostCentre cc_encl || not (isCurrentCostCentre cc_rhs)
169 %************************************************************************
171 \section{Dealing with a single binder}
173 %************************************************************************
175 When we hit a binder we may need to
176 (a) apply the the type envt (if non-empty) to its type
177 (b) apply the type envt and id envt to its SpecEnv (if it has one)
178 (c) give it a new unique to avoid name clashes
181 simplBinder :: SimplEnv -> InBinder -> SmplM (SimplEnv, OutId)
182 simplBinder env (id, _)
183 | not_in_scope -- Not in scope, so no need to clone
184 && empty_ty_subst -- No type substitution to do inside the Id
185 && isNullIdEnv id_subst -- No id substitution to do inside the Id
187 env' = setIdEnv env (addOneToIdEnv in_scope_ids id id, id_subst)
189 returnSmpl (env', id)
194 -- I reckon the empty-env thing should catch
195 -- most no-free-tyvars things, so this test should be redundant
196 (if idHasNoFreeTyVars id then pprTrace "applyEnvsToId" (ppr id) else (\x -> x))
199 -- id1 has its type zapped
200 id1 | empty_ty_subst = id
201 | otherwise = mkIdWithNewType id ty'
203 -- id2 has its SpecEnv zapped
204 id2 | isEmptySpecEnv spec_env = id1
205 | otherwise = setIdSpecialisation id spec_env'
210 env' = setIdEnv env (addOneToIdEnv in_scope_ids id id2, id_subst)
212 returnSmpl (env', id2)
215 getUniqueSmpl `thenSmpl` \ uniq ->
217 id3 = mkIdWithNewUniq id2 uniq
218 env' = setIdEnv env (addOneToIdEnv in_scope_ids id3 id3,
219 addOneToIdEnv id_subst id (VarArg id3))
221 returnSmpl (env', id3)
224 ((in_scope_tyvars, ty_subst), (in_scope_ids, id_subst)) = getEnvs env
225 empty_ty_subst = isEmptyTyVarEnv ty_subst
226 not_in_scope = not (id `elemIdEnv` in_scope_ids)
229 ty' = instantiateTy ty_subst ty
231 spec_env = getIdSpecialisation id
232 spec_env' = substSpecEnv ty_subst (substSpecEnvRhs ty_subst id_subst) spec_env
234 simplBinders :: SimplEnv -> [InBinder] -> SmplM (SimplEnv, [OutId])
235 simplBinders env binders = mapAccumLSmpl simplBinder env binders
239 simplTyBinder :: SimplEnv -> TyVar -> SmplM (SimplEnv, TyVar)
240 simplTyBinder env tyvar
241 | not (tyvar `elementOfTyVarSet` tyvars) -- No need to clone
243 env' = setTyEnv env (tyvars `addOneToTyVarSet` tyvar, ty_subst)
245 returnSmpl (env', tyvar)
247 | otherwise -- Need to clone
248 = getUniqueSmpl `thenSmpl` \ uniq ->
250 tyvar' = cloneTyVar tyvar uniq
251 env' = setTyEnv env (tyvars `addOneToTyVarSet` tyvar',
252 addToTyVarEnv ty_subst tyvar (mkTyVarTy tyvar'))
254 returnSmpl (env', tyvar')
256 ((tyvars, ty_subst), (ids, id_subst)) = getEnvs env
258 simplTyBinders :: SimplEnv -> [TyVar] -> SmplM (SimplEnv, [TyVar])
259 simplTyBinders env binders = mapAccumLSmpl simplTyBinder env binders