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, getNewStrictness, setIdNewStrictness,
26 idNewDemandInfo, setIdNewDemandInfo, newStrictnessFromOld )
27 import IdInfo ( newDemand )
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, equalLength )
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)
120 dmdAnal sigs Bot e = (botDmdType, e)
122 dmdAnal sigs Lazy e = let
123 (res_ty, e') = dmdAnal sigs Eval e
125 (deferType res_ty, e')
126 -- It's important not to analyse e with a lazy demand because
127 -- a) When we encounter case s of (a,b) ->
128 -- we demand s with U(d1d2)... but if the overall demand is lazy
129 -- that is wrong, and we'd need to reduce the demand on s,
130 -- which is inconvenient
131 -- b) More important, consider
132 -- f (let x = R in x+x), where f is lazy
133 -- We still want to mark x as demanded, because it will be when we
134 -- enter the let. If we analyse f's arg with a Lazy demand, we'll
135 -- just mark x as Lazy
136 -- c) The application rule wouldn't be right either
137 -- Evaluating (f x) in a L demand does *not* cause
138 -- evaluation of f in a C(L) demand!
141 dmdAnal sigs dmd (Lit lit)
142 = (topDmdType, Lit lit)
144 dmdAnal sigs dmd (Var var)
145 = (dmdTransform sigs var dmd, Var var)
147 dmdAnal sigs dmd (Note n e)
148 = (dmd_ty, Note n e')
150 (dmd_ty, e') = dmdAnal sigs dmd' e
152 Coerce _ _ -> Eval -- This coerce usually arises from a recursive
153 other -> dmd -- newtype, and we don't want to look inside them
154 -- for exactly the same reason that we don't look
155 -- inside recursive products -- we might not reach
156 -- a fixpoint. So revert to a vanilla Eval demand
158 dmdAnal sigs dmd (App fun (Type ty))
159 = (fun_ty, App fun' (Type ty))
161 (fun_ty, fun') = dmdAnal sigs dmd fun
163 -- Lots of the other code is there to make this
164 -- beautiful, compositional, application rule :-)
165 dmdAnal sigs dmd e@(App fun arg) -- Non-type arguments
166 = let -- [Type arg handled above]
167 (fun_ty, fun') = dmdAnal sigs (Call dmd) fun
168 (arg_ty, arg') = dmdAnal sigs arg_dmd arg
169 (arg_dmd, res_ty) = splitDmdTy fun_ty
171 (res_ty `bothType` arg_ty, App fun' arg')
173 dmdAnal sigs dmd (Lam var body)
176 (body_ty, body') = dmdAnal sigs dmd body
178 (body_ty, Lam var body')
180 | Call body_dmd <- dmd -- A call demand: good!
182 (body_ty, body') = dmdAnal sigs body_dmd body
183 (lam_ty, var') = annotateLamIdBndr body_ty var
185 (lam_ty, Lam var' body')
187 | otherwise -- Not enough demand on the lambda; but do the body
188 = let -- anyway to annotate it and gather free var info
189 (body_ty, body') = dmdAnal sigs Eval body
190 (lam_ty, var') = annotateLamIdBndr body_ty var
192 (deferType lam_ty, Lam var' body')
194 dmdAnal sigs dmd (Case scrut case_bndr [alt@(DataAlt dc,bndrs,rhs)])
195 | let tycon = dataConTyCon dc,
196 isProductTyCon tycon,
197 not (isRecursiveTyCon tycon)
199 sigs_alt = extendSigEnv NotTopLevel sigs case_bndr case_bndr_sig
200 (alt_ty, alt') = dmdAnalAlt sigs_alt dmd alt
201 (alt_ty1, case_bndr') = annotateBndr alt_ty case_bndr
202 (_, bndrs', _) = alt'
203 case_bndr_sig = StrictSig (mkDmdType emptyVarEnv [] RetCPR)
204 -- Inside the alternative, the case binder has the CPR property.
205 -- Meaning that a case on it will successfully cancel.
207 -- f True x = case x of y { I# x' -> if x' ==# 3 then y else I# 8 }
210 -- We want f to have the CPR property:
211 -- f b x = case fw b x of { r -> I# r }
212 -- fw True x = case x of y { I# x' -> if x' ==# 3 then x' else 8 }
215 -- Figure out whether the demand on the case binder is used, and use
216 -- that to set the scrut_dmd. This is utterly essential.
217 -- Consider f x = case x of y { (a,b) -> k y a }
218 -- If we just take scrut_demand = U(L,A), then we won't pass x to the
219 -- worker, so the worker will rebuild
220 -- x = (a, absent-error)
221 -- and that'll crash.
222 -- So at one stage I had:
223 -- dead_case_bndr = isAbsentDmd (idNewDemandInfo case_bndr')
224 -- keepity | dead_case_bndr = Drop
225 -- | otherwise = Keep
228 -- case x of y { (a,b) -> h y + a }
229 -- where h : U(LL) -> T
230 -- The above code would compute a Keep for x, since y is not Abs, which is silly
231 -- The insight is, of course, that a demand on y is a demand on the
232 -- scrutinee, so we need to `both` it with the scrut demand
234 scrut_dmd = mkSeq Drop [idNewDemandInfo b | b <- bndrs', isId b]
236 idNewDemandInfo case_bndr'
238 (scrut_ty, scrut') = dmdAnal sigs scrut_dmd scrut
240 (alt_ty1 `bothType` scrut_ty, Case scrut' case_bndr' [alt'])
242 dmdAnal sigs dmd (Case scrut case_bndr alts)
244 (alt_tys, alts') = mapAndUnzip (dmdAnalAlt sigs dmd) alts
245 (scrut_ty, scrut') = dmdAnal sigs Eval scrut
246 (alt_ty, case_bndr') = annotateBndr (foldr1 lubType alt_tys) case_bndr
248 -- pprTrace "dmdAnal:Case" (ppr alts $$ ppr alt_tys)
249 (alt_ty `bothType` scrut_ty, Case scrut' case_bndr' alts')
251 dmdAnal sigs dmd (Let (NonRec id rhs) body)
253 (sigs', lazy_fv, (id1, rhs')) = dmdAnalRhs NotTopLevel sigs (id, rhs)
254 (body_ty, body') = dmdAnal sigs' dmd body
255 (body_ty1, id2) = annotateBndr body_ty id1
256 body_ty2 = addLazyFVs body_ty1 lazy_fv
258 (let vanilla_dmd = vanillaCall (idArity id)
259 actual_dmd = idNewDemandInfo id2
261 if not (vanilla_dmd `betterDemand` actual_dmd) then
262 pprTrace "dmdLet: better demand" (ppr id <+> vcat [text "vanilla" <+> ppr vanilla_dmd,
263 text "actual" <+> ppr actual_dmd])
265 (body_ty2, Let (NonRec id2 rhs') body')
267 dmdAnal sigs dmd (Let (Rec pairs) body)
269 bndrs = map fst pairs
270 (sigs', lazy_fv, pairs') = dmdFix NotTopLevel sigs pairs
271 (body_ty, body') = dmdAnal sigs' dmd body
272 body_ty1 = addLazyFVs body_ty lazy_fv
274 sigs' `seq` body_ty `seq`
276 (body_ty2, _) = annotateBndrs body_ty1 bndrs
277 -- Don't bother to add demand info to recursive
278 -- binders as annotateBndr does;
279 -- being recursive, we can't treat them strictly.
280 -- But we do need to remove the binders from the result demand env
282 (body_ty2, Let (Rec pairs') body')
285 dmdAnalAlt sigs dmd (con,bndrs,rhs)
287 (rhs_ty, rhs') = dmdAnal sigs dmd rhs
288 (alt_ty, bndrs') = annotateBndrs rhs_ty bndrs
290 (alt_ty, (con, bndrs', rhs'))
293 %************************************************************************
295 \subsection{Bindings}
297 %************************************************************************
300 dmdFix :: TopLevelFlag
301 -> SigEnv -- Does not include bindings for this binding
304 [(Id,CoreExpr)]) -- Binders annotated with stricness info
306 dmdFix top_lvl sigs orig_pairs
307 = loop 1 initial_sigs orig_pairs
309 bndrs = map fst orig_pairs
310 initial_sigs = extendSigEnvList sigs [(id, (initial_sig id, top_lvl)) | id <- bndrs]
313 -> SigEnv -- Already contains the current sigs
315 -> (SigEnv, DmdEnv, [(Id,CoreExpr)])
317 | all (same_sig sigs sigs') bndrs
318 = (sigs', lazy_fv, pairs')
319 -- Note: use pairs', not pairs. pairs' is the result of
320 -- processing the RHSs with sigs (= sigs'), whereas pairs
321 -- is the result of processing the RHSs with the *previous*
322 -- iteration of sigs.
323 | n >= 10 = pprTrace "dmdFix loop" (ppr n <+> (vcat
324 [ text "Sigs:" <+> ppr [(id,lookup sigs id, lookup sigs' id) | (id,_) <- pairs],
325 text "env:" <+> ppr (ufmToList sigs),
326 text "binds:" <+> pprCoreBinding (Rec pairs)]))
327 (emptySigEnv, emptyDmdEnv, orig_pairs) -- Safe output
328 | otherwise = loop (n+1) sigs' pairs'
330 -- Use the new signature to do the next pair
331 -- The occurrence analyser has arranged them in a good order
332 -- so this can significantly reduce the number of iterations needed
333 ((sigs',lazy_fv), pairs') = mapAccumL (my_downRhs top_lvl) (sigs, emptyDmdEnv) pairs
335 my_downRhs top_lvl (sigs,lazy_fv) (id,rhs)
336 = -- pprTrace "downRhs {" (ppr id <+> (ppr old_sig))
338 -- pprTrace "downRhsEnd" (ppr id <+> ppr new_sig <+> char '}' )
339 ((sigs', lazy_fv'), pair')
342 (sigs', lazy_fv1, pair') = dmdAnalRhs top_lvl sigs (id,rhs)
343 lazy_fv' = plusUFM_C both lazy_fv lazy_fv1
344 -- old_sig = lookup sigs id
345 -- new_sig = lookup sigs' id
347 -- Get an initial strictness signature from the Id
348 -- itself. That way we make use of earlier iterations
349 -- of the fixpoint algorithm. (Cunning plan.)
350 -- Note that the cunning plan extends to the DmdEnv too,
351 -- since it is part of the strictness signature
352 initial_sig id = idNewStrictness_maybe id `orElse` botSig
354 same_sig sigs sigs' var = lookup sigs var == lookup sigs' var
355 lookup sigs var = case lookupVarEnv sigs var of
358 dmdAnalRhs :: TopLevelFlag
359 -> SigEnv -> (Id, CoreExpr)
360 -> (SigEnv, DmdEnv, (Id, CoreExpr))
361 -- Process the RHS of the binding, add the strictness signature
362 -- to the Id, and augment the environment with the signature as well.
364 dmdAnalRhs top_lvl sigs (id, rhs)
365 = (sigs', lazy_fv, (id', rhs'))
367 arity = idArity id -- The idArity should be up to date
368 -- The simplifier was run just beforehand
369 (rhs_dmd_ty, rhs') = dmdAnal sigs (vanillaCall arity) rhs
370 (lazy_fv, sig_ty) = WARN( arity /= dmdTypeDepth rhs_dmd_ty, ppr id )
371 mkSigTy id rhs rhs_dmd_ty
372 id' = id `setIdNewStrictness` sig_ty
373 sigs' = extendSigEnv top_lvl sigs id sig_ty
376 %************************************************************************
378 \subsection{Strictness signatures and types}
380 %************************************************************************
383 mkTopSigTy :: CoreExpr -> DmdType -> StrictSig
384 -- Take a DmdType and turn it into a StrictSig
385 -- NB: not used for never-inline things; hence False
386 mkTopSigTy rhs dmd_ty = snd (mk_sig_ty False False rhs dmd_ty)
388 mkSigTy :: Id -> CoreExpr -> DmdType -> (DmdEnv, StrictSig)
389 mkSigTy id rhs dmd_ty = mk_sig_ty (isNeverActive (idInlinePragma id))
390 (isStrictDmd (idNewDemandInfo id))
393 mk_sig_ty never_inline strictly_demanded rhs (DmdType fv dmds res)
394 | never_inline && not (isBotRes res)
396 -- Don't strictness-analyse NOINLINE things. Why not? Because
397 -- the NOINLINE says "don't expose any of the inner workings at the call
398 -- site" and the strictness is certainly an inner working.
400 -- More concretely, the demand analyser discovers the following strictness
401 -- for unsafePerformIO: C(U(AV))
403 -- unsafePerformIO (\s -> let r = f x in
404 -- case writeIORef v r s of (# s1, _ #) ->
406 -- The strictness analyser will find that the binding for r is strict,
407 -- (becuase of uPIO's strictness sig), and so it'll evaluate it before
408 -- doing the writeIORef. This actually makes tests/lib/should_run/memo002
411 -- Solution: don't expose the strictness of unsafePerformIO.
413 -- But we do want to expose the strictness of error functions,
414 -- which are also often marked NOINLINE
415 -- {-# NOINLINE foo #-}
416 -- foo x = error ("wubble buggle" ++ x)
417 -- So (hack, hack) we only drop the strictness for non-bottom things
418 -- This is all very unsatisfactory.
419 = (deferEnv fv, topSig)
422 = (lazy_fv, mkStrictSig dmd_ty)
424 dmd_ty = DmdType strict_fv final_dmds res'
426 lazy_fv = filterUFM (not . isStrictDmd) fv
427 strict_fv = filterUFM isStrictDmd fv
428 -- We put the strict FVs in the DmdType of the Id, so
429 -- that at its call sites we unleash demands on its strict fvs.
430 -- An example is 'roll' in imaginary/wheel-sieve2
431 -- Something like this:
433 -- go y = if ... then roll (x-1) else x+1
436 -- We want to see that roll is strict in x, which is because
437 -- go is called. So we put the DmdEnv for x in go's DmdType.
440 -- f :: Int -> Int -> Int
441 -- f x y = let t = x+1
442 -- h z = if z==0 then t else
443 -- if z==1 then x+1 else
447 -- Calling h does indeed evaluate x, but we can only see
448 -- that if we unleash a demand on x at the call site for t.
450 -- Incidentally, here's a place where lambda-lifting h would
451 -- lose the cigar --- we couldn't see the joint strictness in t/x
454 -- We don't want to put *all* the fv's from the RHS into the
455 -- DmdType, because that makes fixpointing very slow --- the
456 -- DmdType gets full of lazy demands that are slow to converge.
458 final_dmds = setUnpackStrategy dmds
459 -- Set the unpacking strategy
462 RetCPR | ignore_cpr_info -> TopRes
464 ignore_cpr_info = is_thunk && not strictly_demanded
465 is_thunk = not (exprIsValue rhs)
466 -- If the rhs is a thunk, we forget the CPR info, because
467 -- it is presumably shared (else it would have been inlined, and
468 -- so we'd lose sharing if w/w'd it into a function.
470 -- Also, if the strictness analyser has figured out (in a previous iteration)
471 -- that it's strict, the let-to-case transformation will happen, so again
473 -- This made a big difference to PrelBase.modInt, which had something like
474 -- modInt = \ x -> let r = ... -> I# v in
475 -- ...body strict in r...
476 -- r's RHS isn't a value yet; but modInt returns r in various branches, so
477 -- if r doesn't have the CPR property then neither does modInt
478 -- Another case I found in practice (in Complex.magnitude), looks like this:
479 -- let k = if ... then I# a else I# b
480 -- in ... body strict in k ....
481 -- (For this example, it doesn't matter whether k is returned as part of
482 -- the overall result.) Left to itself, the simplifier will make a join
484 -- let $j k = ...body strict in k...
485 -- if ... then $j (I# a) else $j (I# b)
488 -- The difficulty with this is that we need the strictness type to
489 -- look at the body... but we now need the body to calculate the demand
490 -- on the variable, so we can decide whether its strictness type should
491 -- have a CPR in it or not. Simple solution:
492 -- a) use strictness info from the previous iteration
493 -- b) make sure we do at least 2 iterations, by doing a second
494 -- round for top-level non-recs. Top level recs will get at
495 -- least 2 iterations except for totally-bottom functions
496 -- which aren't very interesting anyway.
498 -- NB: strictly_demanded is never true of a top-level Id, or of a recursive Id.
501 The unpack strategy determines whether we'll *really* unpack the argument,
502 or whether we'll just remember its strictness. If unpacking would give
503 rise to a *lot* of worker args, we may decide not to unpack after all.
506 setUnpackStrategy :: [Demand] -> [Demand]
508 = snd (go (opt_MaxWorkerArgs - nonAbsentArgs ds) ds)
510 go :: Int -- Max number of args available for sub-components of [Demand]
512 -> (Int, [Demand]) -- Args remaining after subcomponents of [Demand] are unpacked
514 go n (Seq keep cs : ds)
515 | n' >= 0 = Seq keep cs' `cons` go n'' ds
516 | otherwise = Eval `cons` go n ds
519 n' = n + box - non_abs_args
522 Drop -> 1 -- Add one to the budget if we drop the top-level arg
523 non_abs_args = nonAbsentArgs cs
524 -- Delete # of non-absent args to which we'll now be committed
526 go n (d:ds) = d `cons` go n ds
529 cons d (n,ds) = (n, d:ds)
531 nonAbsentArgs :: [Demand] -> Int
533 nonAbsentArgs (Abs : ds) = nonAbsentArgs ds
534 nonAbsentArgs (d : ds) = 1 + nonAbsentArgs ds
538 %************************************************************************
540 \subsection{Strictness signatures and types}
542 %************************************************************************
545 splitDmdTy :: DmdType -> (Demand, DmdType)
546 -- Split off one function argument
547 -- We already have a suitable demand on all
548 -- free vars, so no need to add more!
549 splitDmdTy (DmdType fv (dmd:dmds) res_ty) = (dmd, DmdType fv dmds res_ty)
550 splitDmdTy ty@(DmdType fv [] TopRes) = (Lazy, ty)
551 splitDmdTy ty@(DmdType fv [] BotRes) = (Bot, ty)
553 splitDmdTy ty@(DmdType fv [] RetCPR) = panic "splitDmdTy"
554 -- We should not be applying a product as a function!
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,
602 setIdNewDemandInfo var (argDemand var dmd))
604 (fv', dmd) = removeFV fv var res
606 annotateBndrs = mapAccumR annotateBndr
608 annotateLamIdBndr dmd_ty@(DmdType fv ds res) id
609 -- For lambdas we add the demand to the argument demands
610 -- Only called for Ids
612 (DmdType fv' (hacked_dmd:ds) res, setIdNewDemandInfo id hacked_dmd)
614 (fv', dmd) = removeFV fv id res
615 hacked_dmd = argDemand id dmd
616 -- This call to argDemand is vital, because otherwise we label
617 -- a lambda binder with demand 'B'. But in terms of calling
618 -- conventions that's Abs, because we don't pass it. But
619 -- when we do a w/w split we get
620 -- fw x = (\x y:B -> ...) x (error "oops")
621 -- And then the simplifier things the 'B' is a strict demand
622 -- and evaluates the (error "oops"). Sigh
624 removeFV fv var res = (fv', dmd)
626 fv' = fv `delVarEnv` var
627 dmd = lookupVarEnv fv var `orElse` deflt
628 deflt | isBotRes res = Bot
632 %************************************************************************
634 \subsection{Strictness signatures}
636 %************************************************************************
639 type SigEnv = VarEnv (StrictSig, TopLevelFlag)
640 -- We use the SigEnv to tell us whether to
641 -- record info about a variable in the DmdEnv
642 -- We do so if it's a LocalId, but not top-level
644 -- The DmdEnv gives the demand on the free vars of the function
645 -- when it is given enough args to satisfy the strictness signature
647 emptySigEnv = emptyVarEnv
649 extendSigEnv :: TopLevelFlag -> SigEnv -> Id -> StrictSig -> SigEnv
650 extendSigEnv top_lvl env var sig = extendVarEnv env var (sig, top_lvl)
652 extendSigEnvList = extendVarEnvList
654 dmdTransform :: SigEnv -- The strictness environment
655 -> Id -- The function
656 -> Demand -- The demand on the function
657 -> DmdType -- The demand type of the function in this context
658 -- Returned DmdEnv includes the demand on
659 -- this function plus demand on its free variables
661 dmdTransform sigs var dmd
663 ------ DATA CONSTRUCTOR
664 | isDataConId var, -- Data constructor
665 Seq k ds <- res_dmd -- and the demand looks inside its fields
667 StrictSig dmd_ty = idNewStrictness var -- It must have a strictness sig
668 DmdType _ _ con_res = dmd_ty
671 if arity == call_depth then -- Saturated, so unleash the demand
673 -- ds can be empty, when we are just seq'ing the thing
674 -- If so we must make up a suitable bunch of demands
675 dmd_ds | null ds = replicate arity Abs
676 | otherwise = ASSERT( ds `lengthIs` arity ) ds
679 Keep -> bothLazy_s dmd_ds
681 Defer -> pprTrace "dmdTransform: surprising!" (ppr var)
682 -- I don't think this can happen
684 -- Important! If we Keep the constructor application, then
685 -- we need the demands the constructor places (always lazy)
686 -- If not, we don't need to. For example:
687 -- f p@(x,y) = (p,y) -- S(AL)
689 -- It's vital that we don't calculate Absent for a!
691 mkDmdType emptyDmdEnv arg_ds con_res
692 -- Must remember whether it's a product, hence con_res, not TopRes
696 ------ IMPORTED FUNCTION
697 | isGlobalId var, -- Imported function
698 let StrictSig dmd_ty = getNewStrictness var
699 = if dmdTypeDepth dmd_ty <= call_depth then -- Saturated, so unleash the demand
704 ------ LOCAL LET/REC BOUND THING
705 | Just (StrictSig dmd_ty, top_lvl) <- lookupVarEnv sigs var
707 fn_ty | dmdTypeDepth dmd_ty <= call_depth = dmd_ty
708 | otherwise = deferType dmd_ty
709 -- NB: it's important to use deferType, and not just return topDmdType
710 -- Consider let { f x y = p + x } in f 1
711 -- The application isn't saturated, but we must nevertheless propagate
712 -- a lazy demand for p!
714 addVarDmd top_lvl fn_ty var dmd
716 ------ LOCAL NON-LET/REC BOUND THING
717 | otherwise -- Default case
721 (call_depth, res_dmd) = splitCallDmd dmd
725 %************************************************************************
729 %************************************************************************
732 splitCallDmd :: Demand -> (Int, Demand)
733 splitCallDmd (Call d) = case splitCallDmd d of
735 splitCallDmd d = (0, d)
737 vanillaCall :: Arity -> Demand
739 vanillaCall n = Call (vanillaCall (n-1))
741 deferType :: DmdType -> DmdType
742 deferType (DmdType fv _ _) = DmdType (deferEnv fv) [] TopRes
743 -- Notice that we throw away info about both arguments and results
744 -- For example, f = let ... in \x -> x
745 -- We don't want to get a stricness type V->T for f.
748 deferEnv :: DmdEnv -> DmdEnv
749 deferEnv fv = mapVarEnv defer fv
752 bothLazy :: Demand -> Demand
754 bothLazy_s :: [Demand] -> [Demand]
755 bothLazy_s = map bothLazy
759 argDemand :: Id -> Demand -> Demand
760 argDemand id dmd | isUnLiftedType (idType id) = unliftedArgDemand dmd
761 | otherwise = liftedArgDemand dmd
763 liftedArgDemand :: Demand -> Demand
764 -- The 'Defer' demands are just Lazy at function boundaries
765 -- Ugly! Ask John how to improve it.
766 liftedArgDemand (Seq Defer ds) = Lazy
767 liftedArgDemand (Seq k ds) = Seq k (map liftedArgDemand ds)
768 -- Urk! Don't have type info here
769 liftedArgDemand Err = Eval -- Args passed to a bottoming function
770 liftedArgDemand Bot = Abs -- Don't pass args that are consumed by bottom/err
771 liftedArgDemand d = d
773 unliftedArgDemand :: Demand -> Demand
774 -- Same idea, but for unlifted types the domain is much simpler:
775 -- Either we use it (Lazy) or we don't (Abs)
776 unliftedArgDemand Bot = Abs
777 unliftedArgDemand Abs = Abs
778 unliftedArgDemand other = Lazy
782 betterStrictness :: StrictSig -> StrictSig -> Bool
783 betterStrictness (StrictSig t1) (StrictSig t2) = betterDmdType t1 t2
785 betterDmdType t1 t2 = (t1 `lubType` t2) == t2
787 betterDemand :: Demand -> Demand -> Bool
788 -- If d1 `better` d2, and d2 `better` d2, then d1==d2
789 betterDemand d1 d2 = (d1 `lub` d2) == d2
791 squashDmdEnv (StrictSig (DmdType fv ds res)) = StrictSig (DmdType emptyDmdEnv ds res)
795 -------------------------
796 -- Consider (if x then y else []) with demand V
797 -- Then the first branch gives {y->V} and the second
798 -- *implicitly* has {y->A}. So we must put {y->(V `lub` A)}
799 -- in the result env.
800 lubType (DmdType fv1 ds1 r1) (DmdType fv2 ds2 r2)
801 = DmdType lub_fv2 (zipWith lub ds1 ds2) (r1 `lubRes` r2)
803 lub_fv = plusUFM_C lub fv1 fv2
804 lub_fv1 = modifyEnv (not (isBotRes r1)) defer fv2 fv1 lub_fv
805 lub_fv2 = modifyEnv (not (isBotRes r2)) defer fv1 fv2 lub_fv1
806 -- lub is the identity for Bot
808 -----------------------------------
809 -- (t1 `bothType` t2) takes the argument/result info from t1,
810 -- using t2 just for its free-var info
811 -- NB: Don't forget about r2! It might be BotRes, which is
812 -- a bottom demand on all the in-scope variables.
813 -- Peter: can this be done more neatly?
814 bothType (DmdType fv1 ds1 r1) (DmdType fv2 ds2 r2)
815 = DmdType both_fv2 ds1 (r1 `bothRes` r2)
817 both_fv = plusUFM_C both fv1 fv2
818 both_fv1 = modifyEnv (isBotRes r1) (`both` Bot) fv2 fv1 both_fv
819 both_fv2 = modifyEnv (isBotRes r2) (`both` Bot) fv1 fv2 both_fv1
820 -- both is the identity for Abs
827 lubRes RetCPR RetCPR = RetCPR
828 lubRes r1 r2 = TopRes
830 -- If either diverges, the whole thing does
831 -- Otherwise take CPR info from the first
832 bothRes r1 BotRes = BotRes
837 -- A Seq can have an empty list of demands, in the polymorphic case.
840 lubs ds1 ds2 = ASSERT( equalLength ds1 ds2 ) zipWith lub ds1 ds2
842 -----------------------------------
843 -- A Seq can have an empty list of demands, in the polymorphic case.
846 boths ds1 ds2 = ASSERT( equalLength ds1 ds2 ) zipWith both ds1 ds2
850 modifyEnv :: Bool -- No-op if False
851 -> (Demand -> Demand) -- The zapper
852 -> DmdEnv -> DmdEnv -- Env1 and Env2
853 -> DmdEnv -> DmdEnv -- Transform this env
854 -- Zap anything in Env1 but not in Env2
855 -- Assume: dom(env) includes dom(Env1) and dom(Env2)
857 modifyEnv need_to_modify zapper env1 env2 env
858 | need_to_modify = foldr zap env (keysUFM (env1 `minusUFM` env2))
861 zap uniq env = addToUFM_Directly env uniq (zapper current_val)
863 current_val = expectJust "modifyEnv" (lookupUFM_Directly env uniq)
867 %************************************************************************
869 \subsection{LUB and BOTH}
871 %************************************************************************
875 lub :: Demand -> Demand -> Demand
880 lub Err Abs = Lazy -- E.g. f x = if ... then True else error x
882 | null ds = Seq (case k of { Drop -> Keep; other -> k }) []
884 | not (null ds) = Seq k [Err `lub` d | d <- ds]
885 -- E.g. f x = if ... then fst x else error x
886 -- We *cannot* use the (lub Err d = d) case,
887 -- else we'd get U(VA) for x's demand!!
896 lub Eval (Seq Defer ds) = Lazy -- Essential!
897 lub Eval (Seq Drop ds) | not (null ds) = Seq Drop [Lazy | d <- ds]
899 -- For the Seq Drop case, consider
901 -- f n (x:xs) = f (n+x) xs
902 -- Here we want to do better than just V for n. It's
903 -- unboxed in the (x:xs) case, and we might be prepared to
904 -- rebox it in the [] case.
905 -- But if we don't use *any* of the components, give up
908 lub (Call d1) (Call d2) = Call (lub d1 d2)
909 lub d1@(Call _) d2 = d2 `lub` d1
911 lub (Seq k1 ds1) (Seq k2 ds2)
912 = Seq (k1 `lub_keep` k2) (lub_ds k1 ds1 k2 ds2)
915 lub_ds Keep ds1 Keep ds2 = ds1 `lubs` ds2
916 lub_ds Keep ds1 non_keep ds2 | null ds1 = [Lazy | d <- ds2]
917 | otherwise = bothLazy_s ds1 `lubs` ds2
919 lub_ds non_keep ds1 Keep ds2 | null ds2 = [Lazy | d <- ds1]
920 | otherwise = ds1 `lubs` bothLazy_s ds2
922 lub_ds k1 ds1 k2 ds2 = ds1 `lubs` ds2
925 -- Note that (Keep `lub` Drop) is Drop, not Keep
926 -- Why not? See the example above with (lub Eval d).
929 lub_keep Drop Defer = Defer
930 lub_keep Drop k = Drop
932 lub_keep Defer k = Defer
934 lub d1@(Seq _ _) d2 = d2 `lub` d1
937 defer :: Demand -> Demand
938 -- Computes (Abs `lub` d)
939 -- For the Bot case consider
940 -- f x y = if ... then x else error x
941 -- Then for y we get Abs `lub` Bot, and we really
945 defer (Seq Keep ds) = Lazy
946 defer (Seq _ ds) = Seq Defer ds
950 both :: Demand -> Demand -> Demand
955 | not (null ds) = Seq (case k of { Defer -> Drop; other -> k })
956 [both Bot d | d <- ds]
957 -- E.g. f x = if ... then error (fst x) else fst x
958 -- This equation helps results slightly,
959 -- but is not necessary for soundness
968 both Lazy Eval = Eval
969 both Lazy (Call d) = Call d
970 both Lazy (Seq Defer ds) = Lazy
971 both Lazy (Seq k ds) = Seq Keep ds
974 -- For the (Eval `both` Bot) case, consider
976 -- From 'error' itself we get demand Bot on x
977 -- From the arg demand on x we get Eval
978 -- So we want Eval `both` Bot to be Err.
979 -- That's what Err is *for*
982 both Eval (Seq k ds) = Seq Keep ds
985 both (Call d1) (Call d2) = Call (d1 `both` d2)
986 both d1@(Call _) d2 = d2 `both` d1
988 both (Seq k1 ds1) (Seq k2 ds2)
989 = Seq (k1 `both_keep` k2) (both_ds k1 ds1 k2 ds2)
992 both_keep Keep k2 = Keep
994 both_keep Drop Keep = Keep
995 both_keep Drop k2 = Drop
997 both_keep Defer k2 = k2
1000 both_ds Defer ds1 Defer ds2 = ds1 `boths` ds2
1001 both_ds Defer ds1 non_defer ds2 = map defer ds1 `boths` ds2
1003 both_ds non_defer ds1 Defer ds2 = ds1 `boths` map defer ds2
1005 both_ds k1 ds1 k2 ds2 = ds1 `boths` ds2
1007 both d1@(Seq _ _) d2 = d2 `both` d1
1011 %************************************************************************
1013 \subsection{Miscellaneous
1015 %************************************************************************
1019 get_changes binds = vcat (map get_changes_bind binds)
1021 get_changes_bind (Rec pairs) = vcat (map get_changes_pr pairs)
1022 get_changes_bind (NonRec id rhs) = get_changes_pr (id,rhs)
1024 get_changes_pr (id,rhs)
1025 = get_changes_var id $$ get_changes_expr rhs
1028 | isId var = get_changes_str var $$ get_changes_dmd var
1031 get_changes_expr (Type t) = empty
1032 get_changes_expr (Var v) = empty
1033 get_changes_expr (Lit l) = empty
1034 get_changes_expr (Note n e) = get_changes_expr e
1035 get_changes_expr (App e1 e2) = get_changes_expr e1 $$ get_changes_expr e2
1036 get_changes_expr (Lam b e) = {- get_changes_var b $$ -} get_changes_expr e
1037 get_changes_expr (Let b e) = get_changes_bind b $$ get_changes_expr e
1038 get_changes_expr (Case e b a) = get_changes_expr e $$ {- get_changes_var b $$ -} vcat (map get_changes_alt a)
1040 get_changes_alt (con,bs,rhs) = {- vcat (map get_changes_var bs) $$ -} get_changes_expr rhs
1043 | new_better && old_better = empty
1044 | new_better = message "BETTER"
1045 | old_better = message "WORSE"
1046 | otherwise = message "INCOMPARABLE"
1048 message word = text word <+> text "strictness for" <+> ppr id <+> info
1049 info = (text "Old" <+> ppr old) $$ (text "New" <+> ppr new)
1050 new = squashDmdEnv (idNewStrictness id) -- Don't report diffs in the env
1051 old = newStrictnessFromOld id
1052 old_better = old `betterStrictness` new
1053 new_better = new `betterStrictness` old
1056 | isUnLiftedType (idType id) = empty -- Not useful
1057 | new_better && old_better = empty
1058 | new_better = message "BETTER"
1059 | old_better = message "WORSE"
1060 | otherwise = message "INCOMPARABLE"
1062 message word = text word <+> text "demand for" <+> ppr id <+> info
1063 info = (text "Old" <+> ppr old) $$ (text "New" <+> ppr new)
1064 new = liftedArgDemand (idNewDemandInfo id) -- To avoid spurious improvements
1065 old = newDemand (idDemandInfo id)
1066 new_better = new `betterDemand` old
1067 old_better = old `betterDemand` new