2 % (c) The GRASP/AQUA Project, Glasgow University, 1993-1998
10 module DmdAnal ( dmdAnalPgm, dmdAnalTopRhs,
11 both {- needed by WwLib -}
14 #include "HsVersions.h"
16 import CmdLineOpts ( DynFlags, DynFlag(..), opt_MaxWorkerArgs )
17 import NewDemand -- All of it
20 import CoreUtils ( exprIsValue, exprArity )
21 import DataCon ( dataConTyCon )
22 import TyCon ( isProductTyCon, isRecursiveTyCon )
23 import Id ( Id, idType, idDemandInfo, idInlinePragma,
24 isDataConId, isGlobalId, idArity,
25 idNewStrictness, idNewStrictness_maybe, setIdNewStrictness,
26 idNewDemandInfo, setIdNewDemandInfo, idName, idStrictness, idCprInfo )
27 import IdInfo ( newDemand, newStrictnessFromOld )
30 import UniqFM ( plusUFM_C, addToUFM_Directly, lookupUFM_Directly,
31 keysUFM, minusUFM, ufmToList, filterUFM )
32 import Type ( isUnLiftedType )
33 import CoreLint ( showPass, endPass )
34 import Util ( mapAndUnzip, mapAccumL, mapAccumR, lengthIs )
35 import BasicTypes ( Arity, TopLevelFlag(..), isTopLevel, isNeverActive )
36 import Maybes ( orElse, expectJust )
42 * set a noinline pragma on bottoming Ids
44 * Consider f x = x+1 `fatbar` error (show x)
45 We'd like to unbox x, even if that means reboxing it in the error case.
48 instance Outputable TopLevelFlag where
52 %************************************************************************
54 \subsection{Top level stuff}
56 %************************************************************************
59 dmdAnalPgm :: DynFlags -> [CoreBind] -> IO [CoreBind]
60 dmdAnalPgm dflags binds
62 showPass dflags "Demand analysis" ;
63 let { binds_plus_dmds = do_prog binds ;
64 dmd_changes = get_changes binds_plus_dmds } ;
65 endPass dflags "Demand analysis"
66 Opt_D_dump_stranal binds_plus_dmds ;
68 -- Only if DEBUG is on, because only then is the old strictness analyser run
69 printDump (text "Changes in demands" $$ dmd_changes) ;
71 return binds_plus_dmds
74 do_prog :: [CoreBind] -> [CoreBind]
75 do_prog binds = snd $ mapAccumL dmdAnalTopBind emptySigEnv binds
77 dmdAnalTopBind :: SigEnv
80 dmdAnalTopBind sigs (NonRec id rhs)
82 ( _, _, (_, rhs1)) = dmdAnalRhs TopLevel sigs (id, rhs)
83 (sigs2, _, (id2, rhs2)) = dmdAnalRhs TopLevel sigs (id, rhs1)
84 -- Do two passes to improve CPR information
85 -- See the comments with mkSigTy.ignore_cpr_info below
87 (sigs2, NonRec id2 rhs2)
89 dmdAnalTopBind sigs (Rec pairs)
91 (sigs', _, pairs') = dmdFix TopLevel sigs pairs
92 -- We get two iterations automatically
98 dmdAnalTopRhs :: CoreExpr -> (StrictSig, CoreExpr)
99 -- Analyse the RHS and return
100 -- a) appropriate strictness info
101 -- b) the unfolding (decorated with stricntess info)
105 arity = exprArity rhs
106 (rhs_ty, rhs') = dmdAnal emptySigEnv (vanillaCall arity) rhs
107 sig = mkTopSigTy rhs rhs_ty
110 %************************************************************************
112 \subsection{The analyser itself}
114 %************************************************************************
117 dmdAnal :: SigEnv -> Demand -> CoreExpr -> (DmdType, CoreExpr)
119 dmdAnal sigs Abs e = (topDmdType, e)
122 | not (isStrictDmd dmd)
124 (res_ty, e') = dmdAnal sigs evalDmd e
126 (deferType res_ty, e')
127 -- It's important not to analyse e with a lazy demand because
128 -- a) When we encounter case s of (a,b) ->
129 -- we demand s with U(d1d2)... but if the overall demand is lazy
130 -- that is wrong, and we'd need to reduce the demand on s,
131 -- which is inconvenient
132 -- b) More important, consider
133 -- f (let x = R in x+x), where f is lazy
134 -- We still want to mark x as demanded, because it will be when we
135 -- enter the let. If we analyse f's arg with a Lazy demand, we'll
136 -- just mark x as Lazy
137 -- c) The application rule wouldn't be right either
138 -- Evaluating (f x) in a L demand does *not* cause
139 -- evaluation of f in a C(L) demand!
142 dmdAnal sigs dmd (Lit lit)
143 = (topDmdType, Lit lit)
145 dmdAnal sigs dmd (Var var)
146 = (dmdTransform sigs var dmd, Var var)
148 dmdAnal sigs dmd (Note n e)
149 = (dmd_ty, Note n e')
151 (dmd_ty, e') = dmdAnal sigs dmd' e
153 Coerce _ _ -> evalDmd -- This coerce usually arises from a recursive
154 other -> dmd -- newtype, and we don't want to look inside them
155 -- for exactly the same reason that we don't look
156 -- inside recursive products -- we might not reach
157 -- a fixpoint. So revert to a vanilla Eval demand
159 dmdAnal sigs dmd (App fun (Type ty))
160 = (fun_ty, App fun' (Type ty))
162 (fun_ty, fun') = dmdAnal sigs dmd fun
164 -- Lots of the other code is there to make this
165 -- beautiful, compositional, application rule :-)
166 dmdAnal sigs dmd e@(App fun arg) -- Non-type arguments
167 = let -- [Type arg handled above]
168 (fun_ty, fun') = dmdAnal sigs (Call dmd) fun
169 (arg_ty, arg') = dmdAnal sigs arg_dmd arg
170 (arg_dmd, res_ty) = splitDmdTy fun_ty
172 (res_ty `bothType` arg_ty, App fun' arg')
174 dmdAnal sigs dmd (Lam var body)
177 (body_ty, body') = dmdAnal sigs dmd body
179 (body_ty, Lam var body')
181 | Call body_dmd <- dmd -- A call demand: good!
183 (body_ty, body') = dmdAnal sigs body_dmd body
184 (lam_ty, var') = annotateLamIdBndr body_ty var
186 (lam_ty, Lam var' body')
188 | otherwise -- Not enough demand on the lambda; but do the body
189 = let -- anyway to annotate it and gather free var info
190 (body_ty, body') = dmdAnal sigs evalDmd body
191 (lam_ty, var') = annotateLamIdBndr body_ty var
193 (deferType lam_ty, Lam var' body')
195 dmdAnal sigs dmd (Case scrut case_bndr [alt@(DataAlt dc,bndrs,rhs)])
196 | let tycon = dataConTyCon dc,
197 isProductTyCon tycon,
198 not (isRecursiveTyCon tycon)
200 sigs_alt = extendSigEnv NotTopLevel sigs case_bndr case_bndr_sig
201 (alt_ty, alt') = dmdAnalAlt sigs_alt dmd alt
202 (alt_ty1, case_bndr') = annotateBndr alt_ty case_bndr
203 (_, bndrs', _) = alt'
204 case_bndr_sig = StrictSig (mkDmdType emptyVarEnv [] RetCPR)
205 -- Inside the alternative, the case binder has the CPR property.
206 -- Meaning that a case on it will successfully cancel.
208 -- f True x = case x of y { I# x' -> if x' ==# 3 then y else I# 8 }
211 -- We want f to have the CPR property:
212 -- f b x = case fw b x of { r -> I# r }
213 -- fw True x = case x of y { I# x' -> if x' ==# 3 then x' else 8 }
216 -- Figure out whether the demand on the case binder is used, and use
217 -- that to set the scrut_dmd. This is utterly essential.
218 -- Consider f x = case x of y { (a,b) -> k y a }
219 -- If we just take scrut_demand = U(L,A), then we won't pass x to the
220 -- worker, so the worker will rebuild
221 -- x = (a, absent-error)
222 -- and that'll crash.
223 -- So at one stage I had:
224 -- dead_case_bndr = isAbsentDmd (idNewDemandInfo case_bndr')
225 -- keepity | dead_case_bndr = Drop
226 -- | otherwise = Keep
229 -- case x of y { (a,b) -> h y + a }
230 -- where h : U(LL) -> T
231 -- The above code would compute a Keep for x, since y is not Abs, which is silly
232 -- The insight is, of course, that a demand on y is a demand on the
233 -- scrutinee, so we need to `both` it with the scrut demand
235 scrut_dmd = Eval (Prod [idNewDemandInfo b | b <- bndrs', isId b])
237 idNewDemandInfo case_bndr'
239 (scrut_ty, scrut') = dmdAnal sigs scrut_dmd scrut
241 (alt_ty1 `bothType` scrut_ty, Case scrut' case_bndr' [alt'])
243 dmdAnal sigs dmd (Case scrut case_bndr alts)
245 (alt_tys, alts') = mapAndUnzip (dmdAnalAlt sigs dmd) alts
246 (scrut_ty, scrut') = dmdAnal sigs evalDmd scrut
247 (alt_ty, case_bndr') = annotateBndr (foldr1 lubType alt_tys) case_bndr
249 -- pprTrace "dmdAnal:Case" (ppr alts $$ ppr alt_tys)
250 (alt_ty `bothType` scrut_ty, Case scrut' case_bndr' alts')
252 dmdAnal sigs dmd (Let (NonRec id rhs) body)
254 (sigs', lazy_fv, (id1, rhs')) = dmdAnalRhs NotTopLevel sigs (id, rhs)
255 (body_ty, body') = dmdAnal sigs' dmd body
256 (body_ty1, id2) = annotateBndr body_ty id1
257 body_ty2 = addLazyFVs body_ty1 lazy_fv
260 -- If the actual demand is better than the vanilla
261 -- demand, we might do better to re-analyse with the
263 (let vanilla_dmd = vanillaCall (idArity id)
264 actual_dmd = idNewDemandInfo id2
266 if actual_dmd `betterDemand` vanilla_dmd && actual_dmd /= vanilla_dmd then
267 pprTrace "dmdLet: better demand" (ppr id <+> vcat [text "vanilla" <+> ppr vanilla_dmd,
268 text "actual" <+> ppr actual_dmd])
271 (body_ty2, Let (NonRec id2 rhs') body')
273 dmdAnal sigs dmd (Let (Rec pairs) body)
275 bndrs = map fst pairs
276 (sigs', lazy_fv, pairs') = dmdFix NotTopLevel sigs pairs
277 (body_ty, body') = dmdAnal sigs' dmd body
278 body_ty1 = addLazyFVs body_ty lazy_fv
280 sigs' `seq` body_ty `seq`
282 (body_ty2, _) = annotateBndrs body_ty1 bndrs
283 -- Don't bother to add demand info to recursive
284 -- binders as annotateBndr does;
285 -- being recursive, we can't treat them strictly.
286 -- But we do need to remove the binders from the result demand env
288 (body_ty2, Let (Rec pairs') body')
291 dmdAnalAlt sigs dmd (con,bndrs,rhs)
293 (rhs_ty, rhs') = dmdAnal sigs dmd rhs
294 (alt_ty, bndrs') = annotateBndrs rhs_ty bndrs
296 (alt_ty, (con, bndrs', rhs'))
299 %************************************************************************
301 \subsection{Bindings}
303 %************************************************************************
306 dmdFix :: TopLevelFlag
307 -> SigEnv -- Does not include bindings for this binding
310 [(Id,CoreExpr)]) -- Binders annotated with stricness info
312 dmdFix top_lvl sigs orig_pairs
313 = loop 1 initial_sigs orig_pairs
315 bndrs = map fst orig_pairs
316 initial_sigs = extendSigEnvList sigs [(id, (initial_sig id, top_lvl)) | id <- bndrs]
319 -> SigEnv -- Already contains the current sigs
321 -> (SigEnv, DmdEnv, [(Id,CoreExpr)])
323 | all (same_sig sigs sigs') bndrs
324 = (sigs', lazy_fv, pairs')
325 -- Note: use pairs', not pairs. pairs' is the result of
326 -- processing the RHSs with sigs (= sigs'), whereas pairs
327 -- is the result of processing the RHSs with the *previous*
328 -- iteration of sigs.
329 | n >= 10 = pprTrace "dmdFix loop" (ppr n <+> (vcat
330 [ text "Sigs:" <+> ppr [(id,lookup sigs id, lookup sigs' id) | (id,_) <- pairs],
331 text "env:" <+> ppr (ufmToList sigs),
332 text "binds:" <+> pprCoreBinding (Rec pairs)]))
333 (emptySigEnv, emptyDmdEnv, orig_pairs) -- Safe output
334 | otherwise = loop (n+1) sigs' pairs'
336 -- Use the new signature to do the next pair
337 -- The occurrence analyser has arranged them in a good order
338 -- so this can significantly reduce the number of iterations needed
339 ((sigs',lazy_fv), pairs') = mapAccumL (my_downRhs top_lvl) (sigs, emptyDmdEnv) pairs
341 my_downRhs top_lvl (sigs,lazy_fv) (id,rhs)
342 = -- pprTrace "downRhs {" (ppr id <+> (ppr old_sig))
344 -- pprTrace "downRhsEnd" (ppr id <+> ppr new_sig <+> char '}' )
345 ((sigs', lazy_fv'), pair')
348 (sigs', lazy_fv1, pair') = dmdAnalRhs top_lvl sigs (id,rhs)
349 lazy_fv' = plusUFM_C both lazy_fv lazy_fv1
350 -- old_sig = lookup sigs id
351 -- new_sig = lookup sigs' id
353 -- Get an initial strictness signature from the Id
354 -- itself. That way we make use of earlier iterations
355 -- of the fixpoint algorithm. (Cunning plan.)
356 -- Note that the cunning plan extends to the DmdEnv too,
357 -- since it is part of the strictness signature
358 initial_sig id = idNewStrictness_maybe id `orElse` botSig
360 same_sig sigs sigs' var = lookup sigs var == lookup sigs' var
361 lookup sigs var = case lookupVarEnv sigs var of
364 dmdAnalRhs :: TopLevelFlag
365 -> SigEnv -> (Id, CoreExpr)
366 -> (SigEnv, DmdEnv, (Id, CoreExpr))
367 -- Process the RHS of the binding, add the strictness signature
368 -- to the Id, and augment the environment with the signature as well.
370 dmdAnalRhs top_lvl sigs (id, rhs)
371 = (sigs', lazy_fv, (id', rhs'))
373 arity = idArity id -- The idArity should be up to date
374 -- The simplifier was run just beforehand
375 (rhs_dmd_ty, rhs') = dmdAnal sigs (vanillaCall arity) rhs
376 (lazy_fv, sig_ty) = WARN( arity /= dmdTypeDepth rhs_dmd_ty, ppr id )
377 mkSigTy id rhs rhs_dmd_ty
378 id' = id `setIdNewStrictness` sig_ty
379 sigs' = extendSigEnv top_lvl sigs id sig_ty
382 %************************************************************************
384 \subsection{Strictness signatures and types}
386 %************************************************************************
389 mkTopSigTy :: CoreExpr -> DmdType -> StrictSig
390 -- Take a DmdType and turn it into a StrictSig
391 -- NB: not used for never-inline things; hence False
392 mkTopSigTy rhs dmd_ty = snd (mk_sig_ty False False rhs dmd_ty)
394 mkSigTy :: Id -> CoreExpr -> DmdType -> (DmdEnv, StrictSig)
395 mkSigTy id rhs dmd_ty = mk_sig_ty (isNeverActive (idInlinePragma id))
396 (isStrictDmd (idNewDemandInfo id))
399 mk_sig_ty never_inline strictly_demanded rhs (DmdType fv dmds res)
400 | never_inline && not (isBotRes res)
402 -- Don't strictness-analyse NOINLINE things. Why not? Because
403 -- the NOINLINE says "don't expose any of the inner workings at the call
404 -- site" and the strictness is certainly an inner working.
406 -- More concretely, the demand analyser discovers the following strictness
407 -- for unsafePerformIO: C(U(AV))
409 -- unsafePerformIO (\s -> let r = f x in
410 -- case writeIORef v r s of (# s1, _ #) ->
412 -- The strictness analyser will find that the binding for r is strict,
413 -- (becuase of uPIO's strictness sig), and so it'll evaluate it before
414 -- doing the writeIORef. This actually makes tests/lib/should_run/memo002
417 -- Solution: don't expose the strictness of unsafePerformIO.
419 -- But we do want to expose the strictness of error functions,
420 -- which are also often marked NOINLINE
421 -- {-# NOINLINE foo #-}
422 -- foo x = error ("wubble buggle" ++ x)
423 -- So (hack, hack) we only drop the strictness for non-bottom things
424 -- This is all very unsatisfactory.
425 = (deferEnv fv, topSig)
428 = (lazy_fv, mkStrictSig dmd_ty)
430 dmd_ty = DmdType strict_fv final_dmds res'
432 lazy_fv = filterUFM (not . isStrictDmd) fv
433 strict_fv = filterUFM isStrictDmd fv
434 -- We put the strict FVs in the DmdType of the Id, so
435 -- that at its call sites we unleash demands on its strict fvs.
436 -- An example is 'roll' in imaginary/wheel-sieve2
437 -- Something like this:
439 -- go y = if ... then roll (x-1) else x+1
442 -- We want to see that roll is strict in x, which is because
443 -- go is called. So we put the DmdEnv for x in go's DmdType.
446 -- f :: Int -> Int -> Int
447 -- f x y = let t = x+1
448 -- h z = if z==0 then t else
449 -- if z==1 then x+1 else
453 -- Calling h does indeed evaluate x, but we can only see
454 -- that if we unleash a demand on x at the call site for t.
456 -- Incidentally, here's a place where lambda-lifting h would
457 -- lose the cigar --- we couldn't see the joint strictness in t/x
460 -- We don't want to put *all* the fv's from the RHS into the
461 -- DmdType, because that makes fixpointing very slow --- the
462 -- DmdType gets full of lazy demands that are slow to converge.
464 final_dmds = setUnpackStrategy dmds
465 -- Set the unpacking strategy
468 RetCPR | ignore_cpr_info -> TopRes
470 ignore_cpr_info = is_thunk && not strictly_demanded
471 is_thunk = not (exprIsValue rhs)
472 -- If the rhs is a thunk, we forget the CPR info, because
473 -- it is presumably shared (else it would have been inlined, and
474 -- so we'd lose sharing if w/w'd it into a function.
476 -- Also, if the strictness analyser has figured out (in a previous iteration)
477 -- that it's strict, the let-to-case transformation will happen, so again
479 -- This made a big difference to PrelBase.modInt, which had something like
480 -- modInt = \ x -> let r = ... -> I# v in
481 -- ...body strict in r...
482 -- r's RHS isn't a value yet; but modInt returns r in various branches, so
483 -- if r doesn't have the CPR property then neither does modInt
484 -- Another case I found in practice (in Complex.magnitude), looks like this:
485 -- let k = if ... then I# a else I# b
486 -- in ... body strict in k ....
487 -- (For this example, it doesn't matter whether k is returned as part of
488 -- the overall result.) Left to itself, the simplifier will make a join
490 -- let $j k = ...body strict in k...
491 -- if ... then $j (I# a) else $j (I# b)
494 -- The difficulty with this is that we need the strictness type to
495 -- look at the body... but we now need the body to calculate the demand
496 -- on the variable, so we can decide whether its strictness type should
497 -- have a CPR in it or not. Simple solution:
498 -- a) use strictness info from the previous iteration
499 -- b) make sure we do at least 2 iterations, by doing a second
500 -- round for top-level non-recs. Top level recs will get at
501 -- least 2 iterations except for totally-bottom functions
502 -- which aren't very interesting anyway.
504 -- NB: strictly_demanded is never true of a top-level Id, or of a recursive Id.
507 The unpack strategy determines whether we'll *really* unpack the argument,
508 or whether we'll just remember its strictness. If unpacking would give
509 rise to a *lot* of worker args, we may decide not to unpack after all.
512 setUnpackStrategy :: [Demand] -> [Demand]
514 = snd (go (opt_MaxWorkerArgs - nonAbsentArgs ds) ds)
516 go :: Int -- Max number of args available for sub-components of [Demand]
518 -> (Int, [Demand]) -- Args remaining after subcomponents of [Demand] are unpacked
520 go n (Eval (Prod cs) : ds)
521 | n' >= 0 = Eval (Prod cs') `cons` go n'' ds
522 | otherwise = Box (Eval (Prod cs)) `cons` go n ds
525 n' = n + 1 - non_abs_args
526 -- Add one to the budget 'cos we drop the top-level arg
527 non_abs_args = nonAbsentArgs cs
528 -- Delete # of non-absent args to which we'll now be committed
530 go n (d:ds) = d `cons` go n ds
533 cons d (n,ds) = (n, d:ds)
535 nonAbsentArgs :: [Demand] -> Int
537 nonAbsentArgs (Abs : ds) = nonAbsentArgs ds
538 nonAbsentArgs (d : ds) = 1 + nonAbsentArgs ds
542 %************************************************************************
544 \subsection{Strictness signatures and types}
546 %************************************************************************
549 splitDmdTy :: DmdType -> (Demand, DmdType)
550 -- Split off one function argument
551 -- We already have a suitable demand on all
552 -- free vars, so no need to add more!
553 splitDmdTy (DmdType fv (dmd:dmds) res_ty) = (dmd, DmdType fv dmds res_ty)
554 splitDmdTy ty@(DmdType fv [] res_ty) = (resTypeArgDmd res_ty, ty)
558 unitVarDmd var dmd = DmdType (unitVarEnv var dmd) [] TopRes
560 addVarDmd top_lvl dmd_ty@(DmdType fv ds res) var dmd
561 | isTopLevel top_lvl = dmd_ty -- Don't record top level things
562 | otherwise = DmdType (extendVarEnv fv var dmd) ds res
564 addLazyFVs (DmdType fv ds res) lazy_fvs
565 = DmdType both_fv1 ds res
567 both_fv = (plusUFM_C both fv lazy_fvs)
568 both_fv1 = modifyEnv (isBotRes res) (`both` Bot) lazy_fvs fv both_fv
569 -- This modifyEnv is vital. Consider
570 -- let f = \x -> (x,y)
572 -- Here, y is treated as a lazy-fv of f, but we must `both` that L
573 -- demand with the bottom coming up from 'error'
575 -- I got a loop in the fixpointer without this, due to an interaction
576 -- with the lazy_fv filtering in mkSigTy. Roughly, it was
578 -- = letrec g y = x `fatbar`
579 -- letrec h z = z + ...g...
582 -- In the initial iteration for f, f=Bot
583 -- Suppose h is found to be strict in z, but the occurrence of g in its RHS
584 -- is lazy. Now consider the fixpoint iteration for g, esp the demands it
585 -- places on its free variables. Suppose it places none. Then the
586 -- x `fatbar` ...call to h...
587 -- will give a x->V demand for x. That turns into a L demand for x,
588 -- which floats out of the defn for h. Without the modifyEnv, that
589 -- L demand doesn't get both'd with the Bot coming up from the inner
590 -- call to f. So we just get an L demand for x for g.
592 -- A better way to say this is that the lazy-fv filtering should give the
593 -- same answer as putting the lazy fv demands in the function's type.
595 annotateBndr :: DmdType -> Var -> (DmdType, Var)
596 -- The returned env has the var deleted
597 -- The returned var is annotated with demand info
598 -- No effect on the argument demands
599 annotateBndr dmd_ty@(DmdType fv ds res) var
600 | isTyVar var = (dmd_ty, var)
601 | otherwise = (DmdType fv' ds res, setIdNewDemandInfo var dmd)
603 (fv', dmd) = removeFV fv var res
605 annotateBndrs = mapAccumR annotateBndr
607 annotateLamIdBndr dmd_ty@(DmdType fv ds res) id
608 -- For lambdas we add the demand to the argument demands
609 -- Only called for Ids
611 (DmdType fv' (hacked_dmd:ds) res, setIdNewDemandInfo id hacked_dmd)
613 (fv', dmd) = removeFV fv id res
614 hacked_dmd = argDemand dmd
615 -- This call to argDemand is vital, because otherwise we label
616 -- a lambda binder with demand 'B'. But in terms of calling
617 -- conventions that's Abs, because we don't pass it. But
618 -- when we do a w/w split we get
619 -- fw x = (\x y:B -> ...) x (error "oops")
620 -- And then the simplifier things the 'B' is a strict demand
621 -- and evaluates the (error "oops"). Sigh
623 removeFV fv id res = (fv', zapUnlifted id dmd)
625 fv' = fv `delVarEnv` id
626 dmd = lookupVarEnv fv id `orElse` deflt
627 deflt | isBotRes res = Bot
630 -- For unlifted-type variables, we are only
631 -- interested in Bot/Abs/Box Abs
632 zapUnlifted is Bot = Bot
633 zapUnlifted id Abs = Abs
634 zapUnlifted id dmd | isUnLiftedType (idType id) = lazyDmd
638 %************************************************************************
640 \subsection{Strictness signatures}
642 %************************************************************************
645 type SigEnv = VarEnv (StrictSig, TopLevelFlag)
646 -- We use the SigEnv to tell us whether to
647 -- record info about a variable in the DmdEnv
648 -- We do so if it's a LocalId, but not top-level
650 -- The DmdEnv gives the demand on the free vars of the function
651 -- when it is given enough args to satisfy the strictness signature
653 emptySigEnv = emptyVarEnv
655 extendSigEnv :: TopLevelFlag -> SigEnv -> Id -> StrictSig -> SigEnv
656 extendSigEnv top_lvl env var sig = extendVarEnv env var (sig, top_lvl)
658 extendSigEnvList = extendVarEnvList
660 dmdTransform :: SigEnv -- The strictness environment
661 -> Id -- The function
662 -> Demand -- The demand on the function
663 -> DmdType -- The demand type of the function in this context
664 -- Returned DmdEnv includes the demand on
665 -- this function plus demand on its free variables
667 dmdTransform sigs var dmd
669 ------ DATA CONSTRUCTOR
670 | isDataConId var -- Data constructor
672 StrictSig dmd_ty = idNewStrictness var -- It must have a strictness sig
673 DmdType _ _ con_res = dmd_ty
676 if arity == call_depth then -- Saturated, so unleash the demand
678 -- Important! If we Keep the constructor application, then
679 -- we need the demands the constructor places (always lazy)
680 -- If not, we don't need to. For example:
681 -- f p@(x,y) = (p,y) -- S(AL)
683 -- It's vital that we don't calculate Absent for a!
684 dmd_ds = case res_dmd of
685 Box (Eval ds) -> mapDmds box ds
689 -- ds can be empty, when we are just seq'ing the thing
690 -- If so we must make up a suitable bunch of demands
691 arg_ds = case dmd_ds of
692 Poly d -> replicate arity d
693 Prod ds -> ASSERT( ds `lengthIs` arity ) ds
696 mkDmdType emptyDmdEnv arg_ds con_res
697 -- Must remember whether it's a product, hence con_res, not TopRes
701 ------ IMPORTED FUNCTION
702 | isGlobalId var, -- Imported function
703 let StrictSig dmd_ty = idNewStrictness var
704 = if dmdTypeDepth dmd_ty <= call_depth then -- Saturated, so unleash the demand
709 ------ LOCAL LET/REC BOUND THING
710 | Just (StrictSig dmd_ty, top_lvl) <- lookupVarEnv sigs var
712 fn_ty | dmdTypeDepth dmd_ty <= call_depth = dmd_ty
713 | otherwise = deferType dmd_ty
714 -- NB: it's important to use deferType, and not just return topDmdType
715 -- Consider let { f x y = p + x } in f 1
716 -- The application isn't saturated, but we must nevertheless propagate
717 -- a lazy demand for p!
719 addVarDmd top_lvl fn_ty var dmd
721 ------ LOCAL NON-LET/REC BOUND THING
722 | otherwise -- Default case
726 (call_depth, res_dmd) = splitCallDmd dmd
730 %************************************************************************
734 %************************************************************************
737 splitCallDmd :: Demand -> (Int, Demand)
738 splitCallDmd (Call d) = case splitCallDmd d of
740 splitCallDmd d = (0, d)
742 vanillaCall :: Arity -> Demand
743 vanillaCall 0 = evalDmd
744 vanillaCall n = Call (vanillaCall (n-1))
746 deferType :: DmdType -> DmdType
747 deferType (DmdType fv _ _) = DmdType (deferEnv fv) [] TopRes
748 -- Notice that we throw away info about both arguments and results
749 -- For example, f = let ... in \x -> x
750 -- We don't want to get a stricness type V->T for f.
753 deferEnv :: DmdEnv -> DmdEnv
754 deferEnv fv = mapVarEnv defer fv
758 argDemand :: Demand -> Demand
759 -- The 'Defer' demands are just Lazy at function boundaries
760 -- Ugly! Ask John how to improve it.
761 argDemand Top = lazyDmd
762 argDemand (Defer d) = lazyDmd
763 argDemand (Eval ds) = Eval (mapDmds argDemand ds)
764 argDemand (Box Bot) = evalDmd
765 argDemand (Box d) = box (argDemand d)
766 argDemand Bot = Abs -- Don't pass args that are consumed by bottom/err
771 betterStrictness :: StrictSig -> StrictSig -> Bool
772 betterStrictness (StrictSig t1) (StrictSig t2) = betterDmdType t1 t2
774 betterDmdType t1 t2 = (t1 `lubType` t2) == t2
776 betterDemand :: Demand -> Demand -> Bool
777 -- If d1 `better` d2, and d2 `better` d2, then d1==d2
778 betterDemand d1 d2 = (d1 `lub` d2) == d2
782 -------------------------
783 -- Consider (if x then y else []) with demand V
784 -- Then the first branch gives {y->V} and the second
785 -- *implicitly* has {y->A}. So we must put {y->(V `lub` A)}
786 -- in the result env.
787 lubType (DmdType fv1 ds1 r1) (DmdType fv2 ds2 r2)
788 = DmdType lub_fv2 (lub_ds ds1 ds2) (r1 `lubRes` r2)
790 lub_fv = plusUFM_C lub fv1 fv2
791 lub_fv1 = modifyEnv (not (isBotRes r1)) absLub fv2 fv1 lub_fv
792 lub_fv2 = modifyEnv (not (isBotRes r2)) absLub fv1 fv2 lub_fv1
793 -- lub is the identity for Bot
795 -- Extend the shorter argument list to match the longer
796 lub_ds (d1:ds1) (d2:ds2) = lub d1 d2 : lub_ds ds1 ds2
798 lub_ds ds1 [] = map (`lub` resTypeArgDmd r2) ds1
799 lub_ds [] ds2 = map (resTypeArgDmd r1 `lub`) ds2
801 -----------------------------------
802 -- (t1 `bothType` t2) takes the argument/result info from t1,
803 -- using t2 just for its free-var info
804 -- NB: Don't forget about r2! It might be BotRes, which is
805 -- a bottom demand on all the in-scope variables.
806 -- Peter: can this be done more neatly?
807 bothType (DmdType fv1 ds1 r1) (DmdType fv2 ds2 r2)
808 = DmdType both_fv2 ds1 (r1 `bothRes` r2)
810 both_fv = plusUFM_C both fv1 fv2
811 both_fv1 = modifyEnv (isBotRes r1) (`both` Bot) fv2 fv1 both_fv
812 both_fv2 = modifyEnv (isBotRes r2) (`both` Bot) fv1 fv2 both_fv1
813 -- both is the identity for Abs
820 lubRes RetCPR RetCPR = RetCPR
821 lubRes r1 r2 = TopRes
823 -- If either diverges, the whole thing does
824 -- Otherwise take CPR info from the first
825 bothRes r1 BotRes = BotRes
830 modifyEnv :: Bool -- No-op if False
831 -> (Demand -> Demand) -- The zapper
832 -> DmdEnv -> DmdEnv -- Env1 and Env2
833 -> DmdEnv -> DmdEnv -- Transform this env
834 -- Zap anything in Env1 but not in Env2
835 -- Assume: dom(env) includes dom(Env1) and dom(Env2)
837 modifyEnv need_to_modify zapper env1 env2 env
838 | need_to_modify = foldr zap env (keysUFM (env1 `minusUFM` env2))
841 zap uniq env = addToUFM_Directly env uniq (zapper current_val)
843 current_val = expectJust "modifyEnv" (lookupUFM_Directly env uniq)
847 %************************************************************************
849 \subsection{LUB and BOTH}
851 %************************************************************************
854 lub :: Demand -> Demand -> Demand
857 lub Abs d2 = absLub d2
859 lub (Defer ds1) d2 = defer (Eval ds1 `lub` d2)
861 lub (Call d1) (Call d2) = Call (d1 `lub` d2)
862 lub d1@(Call _) (Box d2) = d1 `lub` d2 -- Just strip the box
863 lub d1@(Call _) d2@(Eval _) = d2 -- Presumably seq or vanilla eval
864 lub d1@(Call _) d2 = d2 `lub` d1 -- Bot, Abs, Top
866 -- For the Eval case, we use these approximation rules
867 -- Box Bot <= Eval (Box Bot ...)
868 -- Box Top <= Defer (Box Bot ...)
869 -- Box (Eval ds) <= Eval (map Box ds)
870 lub (Eval ds1) (Eval ds2) = Eval (ds1 `lubs` ds2)
871 lub (Eval ds1) (Box Bot) = Eval (mapDmds (`lub` Box Bot) ds1)
872 lub (Eval ds1) (Box (Eval ds2)) = Eval (ds1 `lubs` mapDmds box ds2)
873 lub (Eval ds1) (Box Abs) = deferEval (mapDmds (`lub` Box Bot) ds1)
874 lub d1@(Eval _) d2 = d2 `lub` d1 -- Bot,Abs,Top,Call,Defer
876 lub (Box d1) (Box d2) = box (d1 `lub` d2)
877 lub d1@(Box _) d2 = d2 `lub` d1
879 lubs = zipWithDmds lub
881 ---------------------
882 -- box is the smart constructor for Box
883 -- It computes <B,bot> & d
884 -- INVARIANT: (Box d) => d = Bot, Abs, Eval
885 -- Seems to be no point in allowing (Box (Call d))
886 box (Call d) = Call d -- The odd man out. Why?
888 box (Defer _) = lazyDmd
889 box Top = lazyDmd -- Box Abs and Box Top
890 box Abs = lazyDmd -- are the same <B,L>
891 box d = Box d -- Bot, Eval
894 defer :: Demand -> Demand
896 -- defer is the smart constructor for Defer
897 -- The idea is that (Defer ds) = <U(ds), L>
899 -- It specifies what happens at a lazy function argument
900 -- or a lambda; the L* operator
901 -- Set the strictness part to L, but leave
902 -- the boxity side unaffected
903 -- It also ensures that Defer (Eval [LLLL]) = L
908 defer (Call _) = lazyDmd -- Approximation here?
909 defer (Box _) = lazyDmd
910 defer (Defer ds) = Defer ds
911 defer (Eval ds) = deferEval ds
913 -- deferEval ds = defer (Eval ds)
914 deferEval ds | allTop ds = Top
915 | otherwise = Defer ds
917 ---------------------
918 absLub :: Demand -> Demand
919 -- Computes (Abs `lub` d)
920 -- For the Bot case consider
921 -- f x y = if ... then x else error x
922 -- Then for y we get Abs `lub` Bot, and we really
927 absLub (Call _) = Top
929 absLub (Eval ds) = Defer (absLubs ds) -- Or (Defer ds)?
930 absLub (Defer ds) = Defer (absLubs ds) -- Or (Defer ds)?
932 absLubs = mapDmds absLub
935 both :: Demand -> Demand -> Demand
941 both Bot (Eval ds) = Eval (mapDmds (`both` Bot) ds)
944 -- From 'error' itself we get demand Bot on x
945 -- From the arg demand on x we get
946 -- x :-> evalDmd = Box (Eval (Poly Abs))
947 -- So we get Bot `both` Box (Eval (Poly Abs))
948 -- = Seq Keep (Poly Bot)
951 -- f x = if ... then error (fst x) else fst x
952 -- Then we get (Eval (Box Bot, Bot) `lub` Eval (SA))
954 -- which is what we want.
957 both Top Bot = errDmd
960 both Top (Box d) = Box d
961 both Top (Call d) = Call d
962 both Top (Eval ds) = Eval (mapDmds (`both` Top) ds)
963 both Top (Defer ds) -- = defer (Top `both` Eval ds)
964 -- = defer (Eval (mapDmds (`both` Top) ds))
965 = deferEval (mapDmds (`both` Top) ds)
968 both (Box d1) (Box d2) = box (d1 `both` d2)
969 both (Box d1) d2@(Call _) = box (d1 `both` d2)
970 both (Box d1) d2@(Eval _) = box (d1 `both` d2)
971 both (Box d1) (Defer d2) = Box d1
972 both d1@(Box _) d2 = d2 `both` d1
974 both (Call d1) (Call d2) = Call (d1 `both` d2)
975 both (Call d1) (Eval ds2) = Call d1 -- Could do better for (Poly Bot)?
976 both (Call d1) (Defer ds2) = Call d1 -- Ditto
977 both d1@(Call _) d2 = d1 `both` d1
979 both (Eval ds1) (Eval ds2) = Eval (ds1 `boths` ds2)
980 both (Eval ds1) (Defer ds2) = Eval (ds1 `boths` mapDmds defer ds2)
981 both d1@(Eval ds1) d2 = d2 `both` d1
983 both (Defer ds1) (Defer ds2) = deferEval (ds1 `boths` ds2)
984 both d1@(Defer ds1) d2 = d2 `both` d1
986 boths = zipWithDmds both
991 %************************************************************************
993 \subsection{Miscellaneous
995 %************************************************************************
999 get_changes binds = vcat (map get_changes_bind binds)
1001 get_changes_bind (Rec pairs) = vcat (map get_changes_pr pairs)
1002 get_changes_bind (NonRec id rhs) = get_changes_pr (id,rhs)
1004 get_changes_pr (id,rhs)
1005 = get_changes_var id $$ get_changes_expr rhs
1008 | isId var = get_changes_str var $$ get_changes_dmd var
1011 get_changes_expr (Type t) = empty
1012 get_changes_expr (Var v) = empty
1013 get_changes_expr (Lit l) = empty
1014 get_changes_expr (Note n e) = get_changes_expr e
1015 get_changes_expr (App e1 e2) = get_changes_expr e1 $$ get_changes_expr e2
1016 get_changes_expr (Lam b e) = {- get_changes_var b $$ -} get_changes_expr e
1017 get_changes_expr (Let b e) = get_changes_bind b $$ get_changes_expr e
1018 get_changes_expr (Case e b a) = get_changes_expr e $$ {- get_changes_var b $$ -} vcat (map get_changes_alt a)
1020 get_changes_alt (con,bs,rhs) = {- vcat (map get_changes_var bs) $$ -} get_changes_expr rhs
1023 | new_better && old_better = empty
1024 | new_better = message "BETTER"
1025 | old_better = message "WORSE"
1026 | otherwise = message "INCOMPARABLE"
1028 message word = text word <+> text "strictness for" <+> ppr id <+> info
1029 info = (text "Old" <+> ppr old) $$ (text "New" <+> ppr new)
1030 new = squashSig (idNewStrictness id) -- Don't report spurious diffs that the old
1031 -- strictness analyser can't track
1032 old = newStrictnessFromOld (idName id) (idArity id) (idStrictness id) (idCprInfo id)
1033 old_better = old `betterStrictness` new
1034 new_better = new `betterStrictness` old
1037 | isUnLiftedType (idType id) = empty -- Not useful
1038 | new_better && old_better = empty
1039 | new_better = message "BETTER"
1040 | old_better = message "WORSE"
1041 | otherwise = message "INCOMPARABLE"
1043 message word = text word <+> text "demand for" <+> ppr id <+> info
1044 info = (text "Old" <+> ppr old) $$ (text "New" <+> ppr new)
1045 new = squashDmd (argDemand (idNewDemandInfo id)) -- To avoid spurious improvements
1047 old = newDemand (idDemandInfo id)
1048 new_better = new `betterDemand` old
1049 old_better = old `betterDemand` new
1051 squashSig (StrictSig (DmdType fv ds res))
1052 = StrictSig (DmdType emptyDmdEnv (map squashDmd ds) res)
1054 -- squash just gets rid of call demands
1055 -- which the old analyser doesn't track
1056 squashDmd (Call d) = evalDmd
1057 squashDmd (Box d) = Box (squashDmd d)
1058 squashDmd (Eval ds) = Eval (mapDmds squashDmd ds)
1059 squashDmd (Defer ds) = Defer (mapDmds squashDmd ds)