2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
6 We attach binding levels to Core bindings, in preparation for floating
9 We also let-ify many applications (notably case scrutinees), so they
10 will have a fighting chance of being floated sensible.
13 #include "HsVersions.h"
20 incMinorLvl, ltMajLvl, ltLvl, isTopLvl
21 -- not exported: , incMajorLvl, isTopMajLvl, unTopify
24 import Type ( isPrimType, isLeakFreeType, mkTyVarTy,
25 quantifyTy, TyVarTemplate -- Needed for quantifyTy
28 import Literal ( Literal(..) )
29 import CmdLineOpts ( GlobalSwitch(..) )
31 import Id ( mkSysLocal, idType, eqId,
32 isBottomingId, toplevelishId, DataCon(..)
33 IF_ATTACK_PRAGMAS(COMMA bottomIsGuaranteed)
35 import Maybes ( Maybe(..) )
36 import Pretty -- debugging only
38 import SrcLoc ( mkUnknownSrcLoc, SrcLoc )
43 %************************************************************************
45 \subsection{Level numbers}
47 %************************************************************************
51 Int -- Level number of enclosing lambdas
52 Int -- Number of big-lambda and/or case expressions between
53 -- here and the nearest enclosing lambda
55 | Top -- Means *really* the top level.
58 The {\em level number} on a (type-)lambda-bound variable is the
59 nesting depth of the (type-)lambda which binds it. On an expression, it's the
60 maximum level number of its free (type-)variables. On a let(rec)-bound
61 variable, it's the level of its RHS. On a case-bound variable, it's
62 the number of enclosing lambdas.
64 Top-level variables: level~0. Those bound on the RHS of a top-level
65 definition but ``before'' a lambda; e.g., the \tr{x} in (levels shown
68 a_0 = let b_? = ... in
69 x_1 = ... b ... in ...
72 Level 0 0 will make something get floated to a top-level "equals", @Top@
73 makes it go right to the top.
75 The main function @lvlExpr@ carries a ``context level'' (@ctxt_lvl@). That's
76 meant to be the level number of the enclosing binder in the final (floated)
77 program. If the level number of a sub-expression is less than that of the
78 context, then it might be worth let-binding the sub-expression so that it
79 will indeed float. This context level starts at @Level 0 0@; it is never @Top@.
82 type LevelledExpr = GenCoreExpr (Id, Level) Id
83 type LevelledAtom = GenCoreAtom Id
84 type LevelledBind = GenCoreBinding (Id, Level) Id
86 type LevelEnvs = (IdEnv Level, -- bind Ids to levels
87 TyVarEnv Level) -- bind type variables to levels
91 incMajorLvl :: Level -> Level
92 incMajorLvl Top = Level 1 0
93 incMajorLvl (Level major minor) = Level (major+1) 0
95 incMinorLvl :: Level -> Level
96 incMinorLvl Top = Level 0 1
97 incMinorLvl (Level major minor) = Level major (minor+1)
99 maxLvl :: Level -> Level -> Level
102 maxLvl l1@(Level maj1 min1) l2@(Level maj2 min2)
103 | (maj1 > maj2) || (maj1 == maj2 && min1 > min2) = l1
106 ltLvl :: Level -> Level -> Bool
108 ltLvl Top (Level _ _) = True
109 ltLvl (Level maj1 min1) (Level maj2 min2) = (maj1 < maj2) ||
110 (maj1 == maj2 && min1 < min2)
112 ltMajLvl :: Level -> Level -> Bool -- Tells if one level belongs to a difft
113 -- *lambda* level to another
114 ltMajLvl l1 Top = False
115 ltMajLvl Top (Level 0 _) = False
116 ltMajLvl Top (Level _ _) = True
117 ltMajLvl (Level maj1 _) (Level maj2 _) = maj1 < maj2
119 isTopLvl :: Level -> Bool
121 isTopLvl other = False
123 isTopMajLvl :: Level -> Bool -- Tells if it's the top *lambda* level
124 isTopMajLvl Top = True
125 isTopMajLvl (Level maj _) = maj == 0
127 unTopify :: Level -> Level
128 unTopify Top = Level 0 0
131 instance Outputable Level where
132 ppr sty Top = ppStr "<Top>"
133 ppr sty (Level maj min) = ppBesides [ ppChar '<', ppInt maj, ppChar ',', ppInt min, ppChar '>' ]
136 %************************************************************************
138 \subsection{Main level-setting code}
140 %************************************************************************
143 setLevels :: [CoreBinding]
144 -> (GlobalSwitch -> Bool) -- access to all global cmd-line opts
148 setLevels binds sw us
149 = do_them binds sw us
151 -- "do_them"'s main business is to thread the monad along
152 -- It gives each top binding the same empty envt, because
153 -- things unbound in the envt have level number zero implicitly
154 do_them :: [CoreBinding] -> LvlM [LevelledBind]
156 do_them [] = returnLvl []
158 = lvlTopBind b `thenLvl` \ (lvld_bind, _) ->
159 do_them bs `thenLvl` \ lvld_binds ->
160 returnLvl (lvld_bind ++ lvld_binds)
162 initial_envs = (nullIdEnv, nullTyVarEnv)
165 lvlTopBind (NonRec binder rhs)
166 = lvlBind (Level 0 0) initial_envs (AnnCoNonRec binder (freeVars rhs))
167 -- Rhs can have no free vars!
169 lvlTopBind (Rec pairs)
170 = lvlBind (Level 0 0) initial_envs (AnnCoRec [(b,freeVars rhs) | (b,rhs) <- pairs])
172 {- NEWER: Too bad about the types: WDP:
173 lvlTopBind (NonRec binder rhs)
174 = {-SIGH:wrong type: ASSERT(isEmptyUniqSet (freeVarsOf rhs))-} -- Rhs can have no free vars!
175 lvlBind (Level 0 0) initial_envs (AnnCoNonRec binder emptyUniqSet)
177 lvlTopBind (Rec pairs)
178 = lvlBind (Level 0 0) initial_envs
179 (AnnCoRec [(b, emptyUniqSet)
181 {-SIGH:ditto:ASSERT(isEmptyUniqSet (freeVarsOf rhs))-} True])
185 %************************************************************************
187 \subsection{Bindings}
189 %************************************************************************
191 The binding stuff works for top level too.
194 type CoreBindingWithFVs = AnnCoreBinding Id Id FVInfo
198 -> CoreBindingWithFVs
199 -> LvlM ([LevelledBind], LevelEnvs)
201 lvlBind ctxt_lvl envs@(venv, tenv) (AnnCoNonRec name rhs)
202 = setFloatLevel True {- Already let-bound -}
203 ctxt_lvl envs rhs ty `thenLvl` \ (final_lvl, rhs') ->
205 new_envs = (addOneToIdEnv venv name final_lvl, tenv)
207 returnLvl ([NonRec (name, final_lvl) rhs'], new_envs)
212 lvlBind ctxt_lvl envs@(venv, tenv) (AnnCoRec pairs)
213 = decideRecFloatLevel ctxt_lvl envs binders rhss
214 `thenLvl` \ (final_lvl, extra_binds, rhss') ->
216 binders_w_lvls = binders `zip` repeat final_lvl
217 new_envs = (growIdEnvList venv binders_w_lvls, tenv)
219 returnLvl (extra_binds ++ [Rec (binders_w_lvls `zip` rhss')], new_envs)
221 (binders,rhss) = unzip pairs
224 %************************************************************************
226 \subsection{Setting expression levels}
228 %************************************************************************
231 lvlExpr :: Level -- ctxt_lvl: Level of enclosing expression
232 -> LevelEnvs -- Level of in-scope names/tyvars
233 -> CoreExprWithFVs -- input expression
234 -> LvlM LevelledExpr -- Result expression
237 The @ctxt_lvl@ is, roughly, the level of the innermost enclosing
242 v = \x -> ...\y -> let r = case (..x..) of
246 When looking at the rhs of @r@, @ctxt_lvl@ will be 1 because that's
247 the level of @r@, even though it's inside a level-2 @\y@. It's
248 important that @ctxt_lvl@ is 1 and not 2 in @r@'s rhs, because we
249 don't want @lvlExpr@ to turn the scrutinee of the @case@ into an MFE
250 --- because it isn't a *maximal* free expression.
252 If there were another lambda in @r@'s rhs, it would get level-2 as well.
255 lvlExpr _ _ (_, AnnCoVar v) = returnLvl (Var v)
256 lvlExpr _ _ (_, AnnCoLit l) = returnLvl (Lit l)
257 lvlExpr _ _ (_, AnnCoCon con tys atoms) = returnLvl (Con con tys atoms)
258 lvlExpr _ _ (_, AnnCoPrim op tys atoms) = returnLvl (Prim op tys atoms)
260 lvlExpr ctxt_lvl envs@(venv, tenv) (_, AnnCoTyApp expr ty)
261 = lvlExpr ctxt_lvl envs expr `thenLvl` \ expr' ->
262 returnLvl (CoTyApp expr' ty)
264 lvlExpr ctxt_lvl envs@(venv, tenv) (_, AnnCoApp fun arg)
265 = lvlExpr ctxt_lvl envs fun `thenLvl` \ fun' ->
266 returnLvl (App fun' arg)
268 lvlExpr ctxt_lvl envs (_, AnnCoSCC cc expr)
269 = lvlExpr ctxt_lvl envs expr `thenLvl` \ expr' ->
270 returnLvl (SCC cc expr')
272 lvlExpr ctxt_lvl (venv, tenv) (_, AnnCoTyLam tyvar e)
273 = lvlExpr incd_lvl (venv, new_tenv) e `thenLvl` \ e' ->
274 returnLvl (CoTyLam tyvar e')
276 incd_lvl = incMinorLvl ctxt_lvl
277 new_tenv = addOneToTyVarEnv tenv tyvar incd_lvl
279 lvlExpr ctxt_lvl envs@(venv, tenv) (_, AnnCoLam arg rhs)
280 = lvlMFE incd_lvl (new_venv, tenv) rhs `thenLvl` \ rhs' ->
281 returnLvl (Lam (arg,incd_lvl) rhs')
283 incd_lvl = incMajorLvl ctxt_lvl
284 new_venv = growIdEnvList venv [(arg,incd_lvl)]
286 lvlExpr ctxt_lvl envs (_, AnnCoLet bind body)
287 = lvlBind ctxt_lvl envs bind `thenLvl` \ (binds', new_envs) ->
288 lvlExpr ctxt_lvl new_envs body `thenLvl` \ body' ->
289 returnLvl (foldr Let body' binds') -- mkCoLet* requires Core...
291 lvlExpr ctxt_lvl envs@(venv, tenv) (_, AnnCoCase expr alts)
292 = lvlMFE ctxt_lvl envs expr `thenLvl` \ expr' ->
293 lvl_alts alts `thenLvl` \ alts' ->
294 returnLvl (Case expr' alts')
296 expr_type = coreExprType (deAnnotate expr)
297 incd_lvl = incMinorLvl ctxt_lvl
299 lvl_alts (AnnCoAlgAlts alts deflt)
300 = mapLvl lvl_alt alts `thenLvl` \ alts' ->
301 lvl_deflt deflt `thenLvl` \ deflt' ->
302 returnLvl (AlgAlts alts' deflt')
306 bs' = [ (b, incd_lvl) | b <- bs ]
307 new_envs = (growIdEnvList venv bs', tenv)
309 lvlMFE incd_lvl new_envs e `thenLvl` \ e' ->
310 returnLvl (con, bs', e')
312 lvl_alts (AnnCoPrimAlts alts deflt)
313 = mapLvl lvl_alt alts `thenLvl` \ alts' ->
314 lvl_deflt deflt `thenLvl` \ deflt' ->
315 returnLvl (PrimAlts alts' deflt')
318 = lvlMFE incd_lvl envs e `thenLvl` \ e' ->
321 lvl_deflt AnnCoNoDefault = returnLvl NoDefault
323 lvl_deflt (AnnCoBindDefault b expr)
325 new_envs = (addOneToIdEnv venv b incd_lvl, tenv)
327 lvlMFE incd_lvl new_envs expr `thenLvl` \ expr' ->
328 returnLvl (BindDefault (b, incd_lvl) expr')
331 @lvlMFE@ is just like @lvlExpr@, except that it might let-bind
332 the expression, so that it can itself be floated.
335 lvlMFE :: Level -- Level of innermost enclosing lambda/tylam
336 -> LevelEnvs -- Level of in-scope names/tyvars
337 -> CoreExprWithFVs -- input expression
338 -> LvlM LevelledExpr -- Result expression
340 lvlMFE ctxt_lvl envs@(venv,_) ann_expr
341 | isPrimType ty -- Can't let-bind it
342 = lvlExpr ctxt_lvl envs ann_expr
344 | otherwise -- Not primitive type so could be let-bound
345 = setFloatLevel False {- Not already let-bound -}
346 ctxt_lvl envs ann_expr ty `thenLvl` \ (final_lvl, expr') ->
349 ty = coreExprType (deAnnotate ann_expr)
353 %************************************************************************
355 \subsection{Deciding floatability}
357 %************************************************************************
359 @setFloatLevel@ is used for let-bound right-hand-sides, or for MFEs which
360 are being created as let-bindings
364 YES. -> (a) try abstracting type variables.
365 If we abstract type variables it will go further, that is, past more
366 lambdas. same as asking if the level number given by the free
367 variables is less than the level number given by free variables
368 and type variables together.
369 Abstract offending type variables, e.g.
371 to let v = /\ty' -> f ty' a b
373 so that v' is not stopped by the level number of ty
374 tag the original let with its level number
375 (from its variables and type variables)
377 YES. -> No point in let binding to float a WHNF.
378 Pin (leave) expression here.
379 NO. -> Will float past a lambda?
380 (check using free variables only, not type variables)
381 YES. -> do the same as (a) above.
382 NO. -> No point in let binding if it is not going anywhere
383 Pin (leave) expression here.
386 setFloatLevel :: Bool -- True <=> the expression is already let-bound
387 -- False <=> it's a possible MFE
388 -> Level -- of context
391 -> CoreExprWithFVs -- Original rhs
392 -> Type -- Type of rhs
394 -> LvlM (Level, -- Level to attribute to this let-binding
395 LevelledExpr) -- Final rhs
397 setFloatLevel alreadyLetBound ctxt_lvl envs@(venv, tenv)
398 expr@(FVInfo fvs tfvs might_leak, _) ty
399 -- Invariant: ctxt_lvl is never = Top
400 -- Beautiful ASSERT, dudes (WDP 95/04)...
402 -- Now deal with (by not floating) trivial non-let-bound expressions
403 -- which just aren't worth let-binding in order to float. We always
404 -- choose to float even trivial let-bound things because it doesn't do
405 -- any harm, and not floating it may pin something important. For
412 -- Here, if we don't float v we won't float w, which is Bad News.
413 -- If this gives any problems we could restrict the idea to things destined
416 | not alreadyLetBound
417 && (manifestly_whnf || not will_float_past_lambda)
418 = -- Pin whnf non-let-bound expressions,
419 -- or ones which aren't going anywhere useful
420 lvlExpr ctxt_lvl envs expr `thenLvl` \ expr' ->
421 returnLvl (ctxt_lvl, expr')
423 | alreadyLetBound && not worth_type_abstraction
424 = -- Process the expression with a new ctxt_lvl, obtained from
425 -- the free vars of the expression itself
426 lvlExpr (unTopify expr_lvl) envs expr `thenLvl` \ expr' ->
427 returnLvl (maybe_unTopify expr_lvl, expr')
429 | otherwise -- This will create a let anyway, even if there is no
430 -- type variable to abstract, so we try to abstract anyway
431 = abstractWrtTyVars offending_tyvars ty envs lvl_after_ty_abstr expr
432 `thenLvl` \ final_expr ->
433 returnLvl (expr_lvl, final_expr)
434 -- OLD LIE: The body of the let, just a type application, isn't worth floating
435 -- so pin it with ctxt_lvl
436 -- The truth: better to give it expr_lvl in case it is pinning
437 -- something non-trivial which depends on it.
439 fv_list = uniqSetToList fvs
440 tv_list = uniqSetToList tfvs
441 expr_lvl = ids_only_lvl `maxLvl` tyvars_only_lvl
442 ids_only_lvl = foldr (maxLvl . idLevel venv) tOP_LEVEL fv_list
443 tyvars_only_lvl = foldr (maxLvl . tyvarLevel tenv) tOP_LEVEL tv_list
444 lvl_after_ty_abstr = ids_only_lvl --`maxLvl` non_offending_tyvars_lvl
446 will_float_past_lambda = -- Will escape lambda if let-bound
447 ids_only_lvl `ltMajLvl` ctxt_lvl
449 worth_type_abstraction = -- Will escape (more) lambda(s)/type lambda(s)
450 -- if type abstracted
451 (ids_only_lvl `ltLvl` tyvars_only_lvl)
452 && not (is_trivial de_ann_expr) -- avoids abstracting trivial type applications
454 de_ann_expr = deAnnotate expr
456 is_trivial (CoTyApp e _) = is_trivial e
457 is_trivial (Var _) = True
460 offending_tyvars = filter offending tv_list
461 --non_offending_tyvars = filter (not . offending) tv_list
462 --non_offending_tyvars_lvl = foldr (maxLvl . tyvarLevel tenv) tOP_LEVEL non_offending_tyvars
464 offending tyvar = ids_only_lvl `ltLvl` tyvarLevel tenv tyvar
466 manifestly_whnf = manifestlyWHNF de_ann_expr || manifestlyBottom de_ann_expr
468 maybe_unTopify Top | not (canFloatToTop (ty, expr)) = Level 0 0
469 maybe_unTopify lvl = lvl
470 {- ToDo [Andre]: the line above (maybe) should be Level 1 0,
471 -- so that the let will not go past the *last* lambda if it can
472 -- generate a space leak. If it is already in major level 0
473 -- It won't do any harm to give it a Level 1 0.
474 -- we should do the same test not only for things with level Top,
475 -- but also for anything that gets a major level 0.
477 f = \a -> let x = [1..1000]
480 f = let x = [1..1000]
482 is just as bad as floating x to the top level.
483 Notice it would be OK in cases like
484 f = \a -> let x = [1..1000]
488 f = let x = [1..1000]
491 as x will be gc'd after y is updated.
492 [We did not hit any problems with the above (Level 0 0) code
497 Abstract wrt tyvars, by making it just as if we had seen
502 instead of simply E. The idea is that v can be freely floated, since it
503 has no free type variables. Of course, if E has no free type
504 variables, then we just return E.
507 abstractWrtTyVars offending_tyvars ty (venv,tenv) lvl expr
508 = lvlExpr incd_lvl new_envs expr `thenLvl` \ expr' ->
509 newLvlVar poly_ty `thenLvl` \ poly_var ->
511 poly_var_rhs = mkCoTyLam offending_tyvars expr'
512 poly_var_binding = NonRec (poly_var, lvl) poly_var_rhs
513 poly_var_app = mkCoTyApps (Var poly_var) (map mkTyVarTy offending_tyvars)
514 final_expr = Let poly_var_binding poly_var_app -- mkCoLet* requires Core
518 poly_ty = snd (quantifyTy offending_tyvars ty)
520 -- These defns are just like those in the TyLam case of lvlExpr
521 (incd_lvl, tyvar_lvls) = mapAccumL next (unTopify lvl) offending_tyvars
523 next lvl tyvar = (lvl1, (tyvar,lvl1))
524 where lvl1 = incMinorLvl lvl
526 new_tenv = growTyVarEnvList tenv tyvar_lvls
527 new_envs = (venv, new_tenv)
530 Recursive definitions. We want to transform
542 x1' = /\ ab -> let D' in e1
544 xn' = /\ ab -> let D' in en
548 where ab are the tyvars pinning the defn further in than it
549 need be, and D is a bunch of simple type applications:
555 The "_cl" indicates that in D, the level numbers on the xi are the context level
556 number; type applications aren't worth floating. The D' decls are
563 but differ in their level numbers; here the ab are the newly-introduced
567 decideRecFloatLevel ctxt_lvl envs@(venv, tenv) ids rhss
568 | isTopMajLvl ids_only_lvl && -- Destination = top
569 not (all canFloatToTop (tys `zip` rhss)) -- Some can't float to top
572 ids_w_lvls = ids `zip` repeat ctxt_lvl
573 new_envs = (growIdEnvList venv ids_w_lvls, tenv)
575 mapLvl (lvlExpr ctxt_lvl new_envs) rhss `thenLvl` \ rhss' ->
576 returnLvl (ctxt_lvl, [], rhss')
578 {- OMITTED; see comments above near isWorthFloatingExpr
580 | not (any (isWorthFloating True . deAnnotate) rhss)
582 mapLvl (lvlExpr ctxt_lvl envs) rhss `thenLvl` \ rhss' ->
583 returnLvl (ctxt_lvl, [], rhss')
587 | ids_only_lvl `ltLvl` tyvars_only_lvl
588 = -- Abstract wrt tyvars;
589 -- offending_tyvars is definitely non-empty
590 -- (I love the ASSERT to check this... WDP 95/02)
592 -- These defns are just like those in the TyLam case of lvlExpr
593 (incd_lvl, tyvar_lvls) = mapAccumL next (unTopify ids_only_lvl) offending_tyvars
595 next lvl tyvar = (lvl1, (tyvar,lvl1))
596 where lvl1 = incMinorLvl lvl
598 ids_w_incd_lvl = [(id,incd_lvl) | id <- ids]
599 new_tenv = growTyVarEnvList tenv tyvar_lvls
600 new_venv = growIdEnvList venv ids_w_incd_lvl
601 new_envs = (new_venv, new_tenv)
603 mapLvl (lvlExpr incd_lvl new_envs) rhss `thenLvl` \ rhss' ->
604 mapLvl newLvlVar poly_tys `thenLvl` \ poly_vars ->
606 ids_w_poly_vars = ids `zip` poly_vars
608 -- The "d_rhss" are the right-hand sides of "D" and "D'"
609 -- in the documentation above
610 d_rhss = [ mkCoTyApps (Var poly_var) offending_tyvar_tys | poly_var <- poly_vars]
612 -- "local_binds" are "D'" in the documentation above
613 local_binds = zipWithEqual NonRec ids_w_incd_lvl d_rhss
615 poly_var_rhss = [ mkCoTyLam offending_tyvars (foldr Let rhs' local_binds)
616 | rhs' <- rhss' -- mkCoLet* requires Core...
619 poly_binds = [(poly_var, ids_only_lvl) | poly_var <- poly_vars] `zip` poly_var_rhss
622 returnLvl (ctxt_lvl, [Rec poly_binds], d_rhss)
623 -- The new right-hand sides, just a type application, aren't worth floating
624 -- so pin it with ctxt_lvl
627 = -- Let it float freely
629 ids_w_lvls = ids `zip` repeat expr_lvl
630 new_envs = (growIdEnvList venv ids_w_lvls, tenv)
632 mapLvl (lvlExpr (unTopify expr_lvl) new_envs) rhss `thenLvl` \ rhss' ->
633 returnLvl (expr_lvl, [], rhss')
638 fvs = unionManyUniqSets [freeVarsOf rhs | rhs <- rhss] `minusUniqSet` mkUniqSet ids
639 tfvs = unionManyUniqSets [freeTyVarsOf rhs | rhs <- rhss]
640 fv_list = uniqSetToList fvs
641 tv_list = uniqSetToList tfvs
643 ids_only_lvl = foldr (maxLvl . idLevel venv) tOP_LEVEL fv_list
644 tyvars_only_lvl = foldr (maxLvl . tyvarLevel tenv) tOP_LEVEL tv_list
645 expr_lvl = ids_only_lvl `maxLvl` tyvars_only_lvl
648 | ids_only_lvl `ltLvl` tyvars_only_lvl = filter offending tv_list
651 offending_tyvar_tys = map mkTyVarTy offending_tyvars
652 poly_tys = [ snd (quantifyTy offending_tyvars ty)
656 offending tyvar = ids_only_lvl `ltLvl` tyvarLevel tenv tyvar
661 {- ******** OMITTED NOW
663 isWorthFloating :: Bool -- True <=> already let-bound
664 -> CoreExpr -- The expression
667 isWorthFloating alreadyLetBound expr
669 | alreadyLetBound = isWorthFloatingExpr expr
671 | otherwise = -- No point in adding a fresh let-binding for a WHNF, because
672 -- floating it isn't beneficial enough.
673 isWorthFloatingExpr expr &&
674 not (manifestlyWHNF expr || manifestlyBottom expr)
677 isWorthFloatingExpr :: CoreExpr -> Bool
678 isWorthFloatingExpr (Var v) = False
679 isWorthFloatingExpr (Lit lit) = False
680 isWorthFloatingExpr (Con con tys []) = False -- Just a type application
681 isWorthFloatingExpr (CoTyApp expr ty) = isWorthFloatingExpr expr
682 isWorthFloatingExpr other = True
684 canFloatToTop :: (Type, CoreExprWithFVs) -> Bool
686 canFloatToTop (ty, (FVInfo _ _ (LeakFree _), expr)) = True
687 canFloatToTop (ty, (FVInfo _ _ MightLeak, expr)) = isLeakFreeType [] ty
689 valSuggestsLeakFree expr = manifestlyWHNF expr || manifestlyBottom expr
694 %************************************************************************
696 \subsection{Help functions}
698 %************************************************************************
701 idLevel :: IdEnv Level -> Id -> Level
703 = case lookupIdEnv venv v of
705 Nothing -> ASSERT(toplevelishId v)
708 tyvarLevel :: TyVarEnv Level -> TyVar -> Level
709 tyvarLevel tenv tyvar
710 = case lookupTyVarEnv tenv tyvar of
715 %************************************************************************
717 \subsection{Free-To-Level Monad}
719 %************************************************************************
723 = (GlobalSwitch -> Bool) -> UniqSupply -> result
726 = case splitUniqSupply us of { (s1, s2) ->
727 case m sw s1 of { m_result ->
730 returnLvl v sw us = v
732 mapLvl f [] = returnLvl []
734 = f x `thenLvl` \ r ->
735 mapLvl f xs `thenLvl` \ rs ->
738 mapAndUnzipLvl f [] = returnLvl ([], [])
739 mapAndUnzipLvl f (x:xs)
740 = f x `thenLvl` \ (r1, r2) ->
741 mapAndUnzipLvl f xs `thenLvl` \ (rs1, rs2) ->
742 returnLvl (r1:rs1, r2:rs2)
744 mapAndUnzip3Lvl f [] = returnLvl ([], [], [])
745 mapAndUnzip3Lvl f (x:xs)
746 = f x `thenLvl` \ (r1, r2, r3) ->
747 mapAndUnzip3Lvl f xs `thenLvl` \ (rs1, rs2, rs3) ->
748 returnLvl (r1:rs1, r2:rs2, r3:rs3)
751 We create a let-binding for `interesting' (non-utterly-trivial)
752 applications, to give them a fighting chance of being floated.
755 newLvlVar :: Type -> LvlM Id
760 id = mkSysLocal SLIT("lvl") uniq ty mkUnknownSrcLoc