2 % (c) The AQUA Project, Glasgow University, 1993-1996
4 \section[SimplEnv]{Environment stuff for the simplifier}
8 nullSimplEnv, combineSimplEnv,
9 pprSimplEnv, -- debugging only
11 bindTyVar, bindTyVars, simplTy,
13 lookupId, bindIdToAtom,
15 getSubstEnvs, setTyEnv, setIdEnv, notInScope,
18 lookupRhsInfo, lookupOutIdEnv, isEvaluated,
19 extendEnvGivenBinding, extendEnvGivenNewRhs,
20 extendEnvGivenRhsInfo, extendEnvGivenInlining,
24 getSwitchChecker, switchIsSet, getSimplIntSwitch,
25 switchOffInlining, setCaseScrutinee,
27 setEnclosingCC, getEnclosingCC,
36 InId, InBinder, InBinding, InType,
37 OutId, OutBinder, OutBinding, OutType,
39 InExpr, InAlts, InDefault, InArg,
40 OutExpr, OutAlts, OutDefault, OutArg
43 #include "HsVersions.h"
45 import BinderInfo ( orBinderInfo, andBinderInfo, noBinderInfo, isOneOcc,
47 BinderInfo {-instances, too-}
49 import CmdLineOpts ( switchIsOn, intSwitchSet, opt_UnfoldingCreationThreshold,
50 SimplifierSwitch(..), SwitchResult(..)
53 import CoreUnfold ( mkFormSummary, couldBeSmallEnoughToInline, whnfOrBottom,
54 Unfolding(..), SimpleUnfolding(..), FormSummary(..),
55 calcUnfoldingGuidance )
56 import CoreUtils ( coreExprCc )
57 import CostCentre ( CostCentre, subsumedCosts, noCostCentreAttached )
58 import FiniteMap -- lots of things
59 import Id ( getInlinePragma,
60 nullIdEnv, growIdEnvList, lookupIdEnv, delOneFromIdEnv,
61 addOneToIdEnv, modifyIdEnv, modifyIdEnv_Directly,
63 import Literal ( Literal{-instances-} )
64 import Maybes ( expectJust )
65 import OccurAnal ( occurAnalyseExpr )
66 import PprCore -- various instances
67 import Type ( instantiateTy, Type )
68 import TyVar ( TyVarEnv, emptyTyVarEnv, plusTyVarEnv, addToTyVarEnv, growTyVarEnvList,
69 TyVarSet, emptyTyVarSet,
72 import Unique ( Unique{-instance Outputable-}, Uniquable(..) )
73 import UniqFM ( addToUFM, addToUFM_C, ufmToList )
74 import Util ( Eager, returnEager, zipEqual, thenCmp, cmpList )
78 %************************************************************************
80 \subsection[Simplify-types]{Type declarations}
82 %************************************************************************
85 type InId = Id -- Not yet cloned
86 type InBinder = (InId, BinderInfo)
87 type InType = Type -- Ditto
88 type InBinding = SimplifiableCoreBinding
89 type InExpr = SimplifiableCoreExpr
90 type InAlts = SimplifiableCoreCaseAlts
91 type InDefault = SimplifiableCoreCaseDefault
92 type InArg = SimplifiableCoreArg
94 type OutId = Id -- Cloned
96 type OutType = Type -- Cloned
97 type OutBinding = CoreBinding
98 type OutExpr = CoreExpr
99 type OutAlts = CoreCaseAlts
100 type OutDefault = CoreCaseDefault
101 type OutArg = CoreArg
103 type SwitchChecker = SimplifierSwitch -> SwitchResult
106 %************************************************************************
108 \subsubsection{The @SimplEnv@ type}
110 %************************************************************************
113 INVARIANT: we assume {\em no shadowing}. (ToDo: How can we ASSERT
114 this? WDP 94/06) This allows us to neglect keeping everything paired
115 with its static environment.
117 The environment contains bindings for all
119 {\em locally-defined}
122 For such things, any unfolding is found in the environment, not in the
123 Id. Unfoldings in the Id itself are used only for imported things
124 (otherwise we get trouble because we have to simplify the unfoldings
125 inside the Ids, etc.).
128 type InTypeEnv = (TyVarSet, -- In-scope tyvars (in result)
129 TyVarEnv Type) -- Type substitution
130 -- If t is in the in-scope set, it certainly won't be
131 -- in the domain of the substitution, and vice versa
133 type InIdEnv = (IdEnv Id, -- In-scope Ids (in result)
134 IdEnv OutArg) -- Id substitution
135 -- The in-scope set is represented by an IdEnv, because
136 -- we use it to propagate pragma info etc from binding
137 -- site to occurrences.
139 -- The substitution usually maps an Id to its clone,
140 -- but if the orig defn is a let-binding, and
141 -- the RHS of the let simplifies to an atom,
142 -- we just add the binding to the substitution and elide the let.
147 CostCentre -- The enclosing cost-centre (when profiling)
148 InTypeEnv -- Maps old type variables to new clones
149 InIdEnv -- Maps locally-bound Ids to new clones
150 OutIdEnv -- Info about the values of OutIds
151 ConAppMap -- Maps constructor applications back to OutIds
154 nullSimplEnv :: SwitchChecker -> SimplEnv
157 = SimplEnv sw_chkr subsumedCosts (emptyTyVarSet, emptyTyVarEnv) (nullIdEnv, nullIdEnv) nullIdEnv nullConApps
159 combineSimplEnv :: SimplEnv -> SimplEnv -> SimplEnv
160 combineSimplEnv env@(SimplEnv chkr _ _ _ out_id_env con_apps)
161 new_env@(SimplEnv _ encl_cc ty_env in_id_env _ _ )
162 = SimplEnv chkr encl_cc ty_env in_id_env out_id_env con_apps
164 pprSimplEnv (SimplEnv _ _ ty_env in_id_env out_id_env con_apps) = panic "pprSimplEnv"
166 getSubstEnvs :: SimplEnv -> (InTypeEnv, InIdEnv)
167 getSubstEnvs (SimplEnv _ _ ty_env in_id_env _ _) = (ty_env, in_id_env)
169 setTyEnv :: SimplEnv -> InTypeEnv -> SimplEnv
170 setTyEnv (SimplEnv chkr encl_cc _ in_id_env out_id_env con_apps) ty_env
171 = SimplEnv chkr encl_cc ty_env in_id_env out_id_env con_apps
173 setIdEnv :: SimplEnv -> InIdEnv -> SimplEnv
174 setIdEnv (SimplEnv chkr encl_cc ty_env _ out_id_env con_apps) id_env
175 = SimplEnv chkr encl_cc ty_env id_env out_id_env con_apps
179 %************************************************************************
181 \subsubsection{Command-line switches}
183 %************************************************************************
186 getSwitchChecker :: SimplEnv -> SwitchChecker
187 getSwitchChecker (SimplEnv chkr _ _ _ _ _) = chkr
189 switchIsSet :: SimplEnv -> SimplifierSwitch -> Bool
190 switchIsSet (SimplEnv chkr _ _ _ _ _) switch
191 = switchIsOn chkr switch
193 getSimplIntSwitch :: SwitchChecker -> (Int-> SimplifierSwitch) -> Int
194 getSimplIntSwitch chkr switch
195 = expectJust "getSimplIntSwitch" (intSwitchSet chkr switch)
198 setCaseScrutinee :: SimplEnv -> SimplEnv
199 setCaseScrutinee (SimplEnv chkr encl_cc ty_env in_id_env out_id_env con_apps)
200 = SimplEnv chkr' encl_cc ty_env in_id_env out_id_env con_apps
202 chkr' SimplCaseScrutinee = SwBool True
203 chkr' other = chkr other
206 @switchOffInlining@ is used to prepare the environment for simplifying
207 the RHS of an Id that's marked with an INLINE pragma. It is going to
208 be inlined wherever they are used, and then all the inlining will take
209 effect. Meanwhile, there isn't much point in doing anything to the
210 as-yet-un-INLINEd rhs. Furthremore, it's very important to switch off
212 (a) not doing so will inline a worker straight back into its wrapper!
214 and (b) Consider the following example
219 in ...g...g...g...g...g...
221 Now, if that's the ONLY occurrence of f, it will be inlined inside g,
222 and thence copied multiple times when g is inlined.
224 Andy disagrees! Example:
225 all xs = foldr (&&) True xs
226 any p = all . map p {-# INLINE any #-}
228 Problem: any won't get deforested, and so if it's exported and
229 the importer doesn't use the inlining, (eg passes it as an arg)
230 then we won't get deforestation at all.
231 We havn't solved this problem yet!
233 We prepare the envt by simply discarding the out_id_env, which has
234 all the unfolding info. At one point we did it by modifying the chkr so
235 that it said "EssentialUnfoldingsOnly", but that prevented legitmate, and important,
236 simplifications happening in the body of the RHS.
239 switchOffInlining :: SimplEnv -> SimplEnv
240 switchOffInlining (SimplEnv chkr encl_cc ty_env in_id_env out_id_env con_apps)
241 = SimplEnv chkr encl_cc ty_env in_id_env nullIdEnv nullConApps
244 %************************************************************************
246 \subsubsection{The ``enclosing cost-centre''}
248 %************************************************************************
251 setEnclosingCC :: SimplEnv -> CostCentre -> SimplEnv
253 setEnclosingCC (SimplEnv chkr _ ty_env in_id_env out_id_env con_apps) encl_cc
254 = SimplEnv chkr encl_cc ty_env in_id_env out_id_env con_apps
256 getEnclosingCC :: SimplEnv -> CostCentre
257 getEnclosingCC (SimplEnv chkr encl_cc ty_env in_id_env out_id_env con_apps) = encl_cc
260 %************************************************************************
262 \subsubsection{The @TypeEnv@ part}
264 %************************************************************************
266 These two "bind" functions extend the tyvar substitution.
267 They don't affect what tyvars are in scope.
270 bindTyVar :: SimplEnv -> TyVar -> Type -> SimplEnv
271 bindTyVar (SimplEnv chkr encl_cc (tyvars, ty_subst) in_id_env out_id_env con_apps) tyvar ty
272 = SimplEnv chkr encl_cc (tyvars, new_ty_subst) in_id_env out_id_env con_apps
274 new_ty_subst = addToTyVarEnv ty_subst tyvar ty
276 bindTyVars :: SimplEnv -> TyVarEnv Type -> SimplEnv
277 bindTyVars (SimplEnv chkr encl_cc (tyvars, ty_subst) in_id_env out_id_env con_apps) extra_subst
278 = SimplEnv chkr encl_cc (tyvars, new_ty_subst) in_id_env out_id_env con_apps
280 new_ty_subst = ty_subst `plusTyVarEnv` extra_subst
284 simplTy (SimplEnv _ _ (_, ty_subst) _ _ _) ty = returnEager (instantiateTy ty_subst ty)
287 %************************************************************************
289 \subsubsection{The ``Id env'' part}
291 %************************************************************************
294 lookupId :: SimplEnv -> Id -> Eager ans OutArg
296 lookupId (SimplEnv _ _ _ (in_scope_ids, id_subst) _ _) id
297 = case lookupIdEnv id_subst id of
298 Just atom -> returnEager atom
299 Nothing -> case lookupIdEnv in_scope_ids id of
300 Just id' -> returnEager (VarArg id')
301 Nothing -> returnEager (VarArg id)
304 notInScope forgets that the specified binder is in scope.
305 It is used when we decide to bind a let(rec) bound thing to
306 an atom, *after* the Id has been added to the in-scope mapping by simplBinder.
309 notInScope :: SimplEnv -> OutBinder -> SimplEnv
310 notInScope (SimplEnv chkr encl_cc ty_env (in_scope_ids, id_subst) out_id_env con_apps) id
311 = SimplEnv chkr encl_cc ty_env (new_in_scope_ids, id_subst) out_id_env con_apps
313 new_in_scope_ids = delOneFromIdEnv in_scope_ids id
316 These "bind" functions extend the Id substitution.
319 bindIdToAtom :: SimplEnv
321 -> OutArg -- Val args only, please
324 bindIdToAtom (SimplEnv chkr encl_cc ty_env (in_scope_ids, id_subst) out_id_env con_apps)
325 (in_id,occ_info) atom
327 LitArg _ -> SimplEnv chkr encl_cc ty_env new_in_id_env out_id_env con_apps
328 VarArg out_id -> SimplEnv chkr encl_cc ty_env new_in_id_env
329 (modifyOccInfo out_id_env (uniqueOf out_id, occ_info))
332 new_in_id_env = (in_scope_ids, addOneToIdEnv id_subst in_id atom)
336 %************************************************************************
338 \subsubsection{The @OutIdEnv@}
340 %************************************************************************
343 The domain of @OutIdInfo@ is some, but not necessarily all, in-scope @OutId@s;
344 both locally-bound ones, and perhaps some imported ones too.
347 type OutIdEnv = IdEnv (OutId, BinderInfo, RhsInfo)
350 The "Id" part is just so that we can recover the domain of the mapping, which
351 IdEnvs don't allow directly.
353 The @BinderInfo@ tells about the occurrences of the @OutId@.
354 Anything that isn't in here should be assumed to occur many times.
355 We keep this info so we can modify it when something changes.
357 The @RhsInfo@ part tells about the value to which the @OutId@ is bound.
360 data RhsInfo = NoRhsInfo
361 | OtherLit [Literal] -- It ain't one of these
362 | OtherCon [Id] -- It ain't one of these
364 -- InUnfolding is used for let(rec) bindings that
365 -- are *definitely* going to be inlined.
366 -- We record the un-simplified RHS and drop the binding
367 | InUnfolding SimplEnv -- Un-simplified unfolding
368 SimplifiableCoreExpr -- (need to snag envts therefore)
370 | OutUnfolding CostCentre
371 SimpleUnfolding -- Already-simplified unfolding
373 lookupOutIdEnv :: SimplEnv -> OutId -> Maybe (OutId,BinderInfo,RhsInfo)
374 lookupOutIdEnv (SimplEnv _ _ _ _ out_id_env _) id = lookupIdEnv out_id_env id
376 lookupRhsInfo :: SimplEnv -> OutId -> RhsInfo
378 = case lookupOutIdEnv env id of
379 Just (_,_,info) -> info
382 modifyOutEnvItem :: (OutId, BinderInfo, RhsInfo)
383 -> (OutId, BinderInfo, RhsInfo)
384 -> (OutId, BinderInfo, RhsInfo)
385 modifyOutEnvItem (id, occ, info1) (_, _, info2)
386 = case (info1, info2) of
387 (OtherLit ls1, OtherLit ls2) -> (id,occ, OtherLit (ls1++ls2))
388 (OtherCon cs1, OtherCon cs2) -> (id,occ, OtherCon (cs1++cs2))
389 (_, NoRhsInfo) -> (id,occ, info1)
390 other -> (id,occ, info2)
395 isEvaluated :: RhsInfo -> Bool
396 isEvaluated (OtherLit _) = True
397 isEvaluated (OtherCon _) = True
398 isEvaluated (OutUnfolding _ (SimpleUnfolding ValueForm _ expr)) = True
399 isEvaluated other = False
405 mkSimplUnfoldingGuidance chkr out_id rhs
406 = calcUnfoldingGuidance (getInlinePragma out_id) opt_UnfoldingCreationThreshold rhs
408 extendEnvGivenRhsInfo :: SimplEnv -> OutId -> BinderInfo -> RhsInfo -> SimplEnv
409 extendEnvGivenRhsInfo env@(SimplEnv chkr encl_cc ty_env in_id_env out_id_env con_apps)
410 out_id occ_info rhs_info
411 = SimplEnv chkr encl_cc ty_env in_id_env new_out_id_env con_apps
413 new_out_id_env = addToUFM_C modifyOutEnvItem out_id_env out_id
414 (out_id, occ_info, rhs_info)
419 modifyOccInfo out_id_env (uniq, new_occ)
420 = modifyIdEnv_Directly modify_fn out_id_env uniq
422 modify_fn (id,occ,rhs) = (id, orBinderInfo occ new_occ, rhs)
424 markDangerousOccs (SimplEnv chkr encl_cc ty_env in_id_env out_id_env con_apps) atoms
425 = SimplEnv chkr encl_cc ty_env in_id_env new_out_id_env con_apps
427 new_out_id_env = foldl (modifyIdEnv modify_fn) out_id_env [v | VarArg v <- atoms]
428 modify_fn (id,occ,rhs) = (id, noBinderInfo, rhs)
433 extendEnvGivenInlining :: SimplEnv -> Id -> BinderInfo -> InExpr -> SimplEnv
434 extendEnvGivenInlining env@(SimplEnv chkr encl_cc ty_env in_id_env out_id_env con_apps)
436 = SimplEnv chkr encl_cc ty_env in_id_env new_out_id_env con_apps
438 new_out_id_env = addToUFM out_id_env id (id, occ_info, InUnfolding env rhs)
442 %************************************************************************
444 \subsubsection{The @ConAppMap@ type}
446 %************************************************************************
448 The @ConAppMap@ maps applications of constructors (to value atoms)
449 back to an association list that says "if the constructor was applied
450 to one of these lists-of-Types, then this OutId is your man (in a
451 non-gender-specific sense)". I.e., this is a reversed mapping for
452 (part of) the main OutIdEnv
455 type ConAppMap = FiniteMap UnfoldConApp [([Type], OutId)]
458 = UCA OutId -- data constructor
459 [OutArg] -- *value* arguments; see use below
463 nullConApps = emptyFM
465 extendConApps con_apps id (Con con args)
466 = addToFM_C (\old new -> new++old) con_apps (UCA con val_args) [(ty_args,id)]
468 val_args = filter isValArg args -- Literals and Ids
469 ty_args = [ty | TyArg ty <- args] -- Just types
471 extendConApps con_apps id other_rhs = con_apps
475 lookForConstructor (SimplEnv _ _ _ _ _ con_apps) con args
476 = case lookupFM con_apps (UCA con val_args) of
479 Just assocs -> case [id | (tys, id) <- assocs,
480 and (zipWith (==) tys ty_args)]
485 val_args = filter isValArg args -- Literals and Ids
486 ty_args = [ty | TyArg ty <- args] -- Just types
490 NB: In @lookForConstructor@ we used (before Apr 94) to have a special case
491 for nullary constructors, but now we only do constructor re-use in
492 let-bindings the special case isn't necessary any more.
495 = -- Don't re-use nullary constructors; it's a waste. Consider
503 -- Here the False in the second case will get replace by "a", hardly
509 The main thing about @UnfoldConApp@ is that it has @Ord@ defined on
510 it, so we can use it for a @FiniteMap@ key.
513 instance Eq UnfoldConApp where
514 a == b = case (a `compare` b) of { EQ -> True; _ -> False }
515 a /= b = case (a `compare` b) of { EQ -> False; _ -> True }
517 instance Ord UnfoldConApp where
518 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
519 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
520 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
521 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
522 compare a b = cmp_app a b
524 cmp_app (UCA c1 as1) (UCA c2 as2)
525 = compare c1 c2 `thenCmp` cmpList cmp_arg as1 as2
527 -- ToDo: make an "instance Ord CoreArg"???
529 cmp_arg (VarArg x) (VarArg y) = x `compare` y
530 cmp_arg (LitArg x) (LitArg y) = x `compare` y
531 cmp_arg (TyArg x) (TyArg y) = panic "SimplEnv.cmp_app:TyArgs"
533 | tag x _LT_ tag y = LT
536 tag (VarArg _) = ILIT(1)
537 tag (LitArg _) = ILIT(2)
538 tag (TyArg _) = panic# "SimplEnv.cmp_app:TyArg"
542 @extendUnfoldEnvGivenRhs@ records in the UnfoldEnv info about the RHS
543 of a new binding. There is a horrid case we have to take care about,
544 due to Andr\'e Santos:
546 type Array_type b = Array Int b;
547 type Descr_type = (Int,Int);
549 tabulate :: (Int -> x) -> Descr_type -> Array_type x;
550 tabulate f (l,u) = listArray (l,u) [f i | i <- [l..u]];
554 f_aareorder::(Array_type Int) -> (Array_type t1) -> Array_type t1;
555 f_aareorder a_index a_ar=
557 f_aareorder' a_i= a_ar ! (a_index ! a_i)
558 } in tabulate f_aareorder' (bounds a_ar);
559 r_index=tabulate ((+) 1) (1,1);
560 arr = listArray (1,1) a_xs;
561 arg = f_aareorder r_index arr
564 Now, when the RHS of arg gets simplified, we inline f_aareorder to get
566 arg = let f_aareorder' a_i = arr ! (r_index ! a_i)
567 in tabulate f_aareorder' (bounds arr)
569 Note that r_index is not inlined, because it was bound to a_index which
570 occurs inside a lambda.
572 Alas, if elems is inlined, so that (elems arg) becomes (case arg of ...),
573 then arg is inlined. IF WE USE THE NEW VERSION OF arg, and re-occurrence
574 analyse it, we won't spot the inside-lambda property of r_index, so r_index
575 will get inlined inside the lambda. AARGH.
577 Solution: when we occurrence-analyse the new RHS we have to go back
578 and modify the info recorded in the UnfoldEnv for the free vars
579 of the RHS. In the example we'd go back and record that r_index is now used
583 extendEnvGivenNewRhs :: SimplEnv -> OutId -> OutExpr -> SimplEnv
584 extendEnvGivenNewRhs env out_id rhs
585 = extendEnvGivenBinding env noBinderInfo out_id rhs
587 extendEnvGivenBinding :: SimplEnv -> BinderInfo -> OutId -> OutExpr -> SimplEnv
588 extendEnvGivenBinding env@(SimplEnv chkr encl_cc ty_env in_id_env out_id_env con_apps)
590 = SimplEnv chkr encl_cc ty_env in_id_env new_out_id_env new_con_apps
592 new_out_id_env | okToInline (whnfOrBottom form)
593 (couldBeSmallEnoughToInline guidance)
595 = out_id_env_with_unfolding
598 -- Don't bother to extend the OutIdEnv unless there is some possibility
599 -- that the thing might be inlined. We check this by calling okToInline suitably.
601 new_con_apps = _scc_ "eegnr.conapps"
602 extendConApps con_apps out_id rhs
604 -- Modify the occ info for rhs's interesting free variables.
605 out_id_env_with_unfolding = _scc_ "eegnr.modify_occ"
606 foldl modifyOccInfo env1 full_fv_occ_info
607 -- NB: full_fv_occ_info *combines* the occurrence of the current binder
608 -- with the occurrences of its RHS's free variables. That's to take
610 -- let a = \x -> BIG in
612 -- in ...b...b...b...
613 -- Here "a" occurs exactly once. "b" simplifies to a small value.
614 -- So "b" will be inlined at each call site, and there's a good chance
615 -- that "a" will too. So we'd better modify "a"s occurrence info to
616 -- record the fact that it can now occur many times by virtue that "b" can.
618 full_fv_occ_info = _scc_ "eegnr.full_fv"
619 [ (uniq, fv_occ `andBinderInfo` occ_info)
620 | (uniq, fv_occ) <- ufmToList fv_occ_info
623 -- Add an unfolding and rhs_info for the new Id.
624 -- If the out_id is already in the OutIdEnv (which can happen if
625 -- the call to extendEnvGivenBinding is from extendEnvGivenNewRhs)
626 -- then just replace the unfolding, leaving occurrence info alone.
627 env1 = _scc_ "eegnr.modify_out"
628 addToUFM_C modifyOutEnvItem out_id_env out_id
629 (out_id, occ_info, rhs_info)
631 -- Occurrence-analyse the RHS
632 -- The "interesting" free variables we want occurrence info for are those
633 -- in the OutIdEnv that have only a single occurrence right now.
634 (fv_occ_info, template) = _scc_ "eegnr.occ-anal"
635 occurAnalyseExpr is_interesting rhs
637 is_interesting v = _scc_ "eegnr.mkidset"
638 case lookupIdEnv out_id_env v of
639 Just (_, occ, _) -> isOneOcc occ
642 -- Compute unfolding details
643 rhs_info = OutUnfolding unf_cc (SimpleUnfolding form guidance template)
644 form = _scc_ "eegnr.form_sum"
646 guidance = _scc_ "eegnr.guidance"
647 mkSimplUnfoldingGuidance chkr out_id rhs
649 -- Compute cost centre for thing
650 unf_cc | noCostCentreAttached expr_cc = encl_cc
651 | otherwise = expr_cc
653 expr_cc = coreExprCc rhs