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)
72 -- If there's an InUnfolding it means that there's no
73 -- let-binding left for the thing, so we'd better inline it!
76 Just (_, _, InUnfolding rhs_env rhs) = info_from_env
78 unfold var rhs_env rhs args result_ty
81 -- Conditional unfolding. There's a binding for the
82 -- thing, but perhaps we want to inline it anyway
83 | ( maybeToBool maybe_unfolding_info
84 && (not essential_unfoldings_only || idMustBeINLINEd var)
85 -- If "essential_unfoldings_only" is true we do no inlinings at all,
86 -- EXCEPT for things that absolutely have to be done
87 -- (see comments with idMustBeINLINEd)
89 && costCentreOk (getEnclosingCC env) (getEnclosingCC unf_env)
91 = -- pprTrace "Unfolding" (ppr var) $
92 unfold var unf_env unf_template args result_ty
96 = returnSmpl (mkGenApp (Var var) args)
99 info_from_env = lookupOutIdEnv env var
100 unfolding_from_id = getIdUnfolding var
102 ---------- Magic unfolding stuff
103 maybe_magic_result = case unfolding_from_id of
104 MagicUnfolding _ magic_fn -> applyMagicUnfoldingFun magic_fn
107 (Just magic_result) = maybe_magic_result
109 ---------- Unfolding stuff
110 must_unfold = case info_from_env of
111 Just (_, _, InUnfolding _ _) -> True
115 = case (info_from_env, unfolding_from_id) of
117 (Just (_, occ_info, OutUnfolding enc_cc unf), _)
118 -> Just (occ_info, setEnclosingCC env enc_cc, unf)
120 (_, CoreUnfolding unf)
121 -> Just (noBinderInfo, env, unf)
125 Just (occ_info, unf_env, simple_unfolding) = maybe_unfolding_info
126 SimpleUnfolding form guidance unf_template = simple_unfolding
128 ---------- Specialisation stuff
129 (ty_args, remaining_args) = initialTyArgs args
130 maybe_specialisation = lookupSpecEnv (getIdSpecialisation var) ty_args
131 Just (spec_bindings, spec_template) = maybe_specialisation
135 sw_chkr = getSwitchChecker env
136 essential_unfoldings_only = switchIsOn sw_chkr EssentialUnfoldingsOnly
137 is_case_scrutinee = switchIsOn sw_chkr SimplCaseScrutinee
138 ok_to_inline = okToInline (whnfOrBottom form) small_enough occ_info
139 small_enough = smallEnoughToInline var arg_evals is_case_scrutinee guidance
140 arg_evals = [is_evald arg | arg <- args, isValArg arg]
142 is_evald (VarArg v) = isEvaluated (lookupRhsInfo env v)
143 is_evald (LitArg l) = True
146 -- Perform the unfolding
147 unfold var unf_env unf_template args result_ty
150 simplCount `thenSmpl` \ n ->
152 pprTrace "Ticks > 1000 and unfolding" (sep [space, int n, ppr var])
157 returnSmpl (mkGenApp (Var var) args)
160 tickUnfold var `thenSmpl_`
161 simplExpr unf_env unf_template args result_ty
164 -- costCentreOk checks that it's ok to inline this thing
165 -- The time it *isn't* is this:
167 -- f x = let y = E in
168 -- scc "foo" (...y...)
170 -- Here y has a "current cost centre", and we can't inline it inside "foo",
171 -- regardless of whether E is a WHNF or not.
173 costCentreOk cc_encl cc_rhs
174 = isCurrentCostCentre cc_encl || not (isCurrentCostCentre cc_rhs)
178 %************************************************************************
180 \section{Dealing with a single binder}
182 %************************************************************************
184 When we hit a binder we may need to
185 (a) apply the the type envt (if non-empty) to its type
186 (b) apply the type envt and id envt to its SpecEnv (if it has one)
187 (c) give it a new unique to avoid name clashes
190 simplBinder :: SimplEnv -> InBinder -> SmplM (SimplEnv, OutId)
191 simplBinder env (id, _)
192 | not_in_scope -- Not in scope, so no need to clone
193 && empty_ty_subst -- No type substitution to do inside the Id
194 && isNullIdEnv id_subst -- No id substitution to do inside the Id
196 env' = setIdEnv env (addOneToIdEnv in_scope_ids id id, id_subst)
198 returnSmpl (env', id)
203 -- I reckon the empty-env thing should catch
204 -- most no-free-tyvars things, so this test should be redundant
205 (if idHasNoFreeTyVars id then pprTrace "applyEnvsToId" (ppr id) else (\x -> x))
208 -- id1 has its type zapped
209 id1 | empty_ty_subst = id
210 | otherwise = mkIdWithNewType id ty'
212 -- id2 has its SpecEnv zapped
213 id2 | isEmptySpecEnv spec_env = id1
214 | otherwise = setIdSpecialisation id spec_env'
219 env' = setIdEnv env (addOneToIdEnv in_scope_ids id id2, id_subst)
221 returnSmpl (env', id2)
224 getUniqueSmpl `thenSmpl` \ uniq ->
226 id3 = mkIdWithNewUniq id2 uniq
227 env' = setIdEnv env (addOneToIdEnv in_scope_ids id3 id3,
228 addOneToIdEnv id_subst id (VarArg id3))
230 returnSmpl (env', id3)
233 ((in_scope_tyvars, ty_subst), (in_scope_ids, id_subst)) = getSubstEnvs env
234 empty_ty_subst = isEmptyTyVarEnv ty_subst
235 not_in_scope = not (id `elemIdEnv` in_scope_ids)
238 ty' = instantiateTy ty_subst ty
240 spec_env = getIdSpecialisation id
241 spec_env' = substSpecEnv ty_subst (substSpecEnvRhs ty_subst id_subst) spec_env
243 simplBinders :: SimplEnv -> [InBinder] -> SmplM (SimplEnv, [OutId])
244 simplBinders env binders = mapAccumLSmpl simplBinder env binders
248 simplTyBinder :: SimplEnv -> TyVar -> SmplM (SimplEnv, TyVar)
249 simplTyBinder env tyvar
250 | not (tyvar `elementOfTyVarSet` tyvars) -- No need to clone
252 env' = setTyEnv env (tyvars `addOneToTyVarSet` tyvar, ty_subst)
254 returnSmpl (env', tyvar)
256 | otherwise -- Need to clone
257 = getUniqueSmpl `thenSmpl` \ uniq ->
259 tyvar' = cloneTyVar tyvar uniq
260 env' = setTyEnv env (tyvars `addOneToTyVarSet` tyvar',
261 addToTyVarEnv ty_subst tyvar (mkTyVarTy tyvar'))
263 returnSmpl (env', tyvar')
265 ((tyvars, ty_subst), (ids, id_subst)) = getSubstEnvs env
267 simplTyBinders :: SimplEnv -> [TyVar] -> SmplM (SimplEnv, [TyVar])
268 simplTyBinders env binders = mapAccumLSmpl simplTyBinder env binders