2 % (c) The GRASP/AQUA Project, Glasgow University, 1993-1998
10 module DmdAnal ( dmdAnalPgm, both {- needed by WwLib -} ) where
12 #include "HsVersions.h"
14 import CmdLineOpts ( DynFlags, DynFlag(..), opt_MaxWorkerArgs )
15 import NewDemand -- All of it
17 import CoreUtils ( exprIsValue, exprArity )
18 import DataCon ( dataConTyCon )
19 import TyCon ( isProductTyCon, isRecursiveTyCon )
20 import Id ( Id, idType, idDemandInfo,
21 isDataConId, isImplicitId, isGlobalId,
22 idNewStrictness, idNewStrictness_maybe, getNewStrictness, setIdNewStrictness,
23 idNewDemandInfo, setIdNewDemandInfo, newStrictnessFromOld )
24 import IdInfo ( newDemand )
27 import UniqFM ( plusUFM_C, addToUFM_Directly, lookupUFM_Directly,
28 keysUFM, minusUFM, ufmToList, filterUFM )
29 import Type ( isUnLiftedType )
30 import CoreLint ( showPass, endPass )
31 import Util ( mapAndUnzip, mapAccumL, mapAccumR )
32 import BasicTypes ( Arity, TopLevelFlag(..), isTopLevel )
33 import Maybes ( orElse, expectJust )
39 * set a noinline pragma on bottoming Ids
41 * Consider f x = x+1 `fatbar` error (show x)
42 We'd like to unbox x, even if that means reboxing it in the error case.
45 instance Outputable TopLevelFlag where
49 %************************************************************************
51 \subsection{Top level stuff}
53 %************************************************************************
56 dmdAnalPgm :: DynFlags -> [CoreBind] -> IO [CoreBind]
57 dmdAnalPgm dflags binds
59 showPass dflags "Demand analysis" ;
60 let { binds_plus_dmds = do_prog binds ;
61 dmd_changes = get_changes binds_plus_dmds } ;
62 endPass dflags "Demand analysis"
63 Opt_D_dump_stranal binds_plus_dmds ;
65 -- Only if DEBUG is on, because only then is the old strictness analyser run
66 printDump (text "Changes in demands" $$ dmd_changes) ;
68 return binds_plus_dmds
71 do_prog :: [CoreBind] -> [CoreBind]
72 do_prog binds = snd $ mapAccumL dmdAnalTopBind emptySigEnv binds
74 dmdAnalTopBind :: SigEnv
77 dmdAnalTopBind sigs (NonRec id rhs)
78 | isImplicitId id -- Don't touch the info on constructors, selectors etc
79 = (sigs, NonRec id rhs) -- It's pre-computed in MkId.lhs
82 (sigs', _, (id', rhs')) = downRhs TopLevel sigs (id, rhs)
84 (sigs', NonRec id' rhs')
86 dmdAnalTopBind sigs (Rec pairs)
88 (sigs', _, pairs') = dmdFix TopLevel sigs pairs
94 %************************************************************************
96 \subsection{The analyser itself}
98 %************************************************************************
101 dmdAnal :: SigEnv -> Demand -> CoreExpr -> (DmdType, CoreExpr)
103 dmdAnal sigs Abs e = (topDmdType, e)
105 dmdAnal sigs Lazy e = let
106 (res_ty, e') = dmdAnal sigs Eval e
108 (deferType res_ty, e')
109 -- It's important not to analyse e with a lazy demand because
110 -- a) When we encounter case s of (a,b) ->
111 -- we demand s with U(d1d2)... but if the overall demand is lazy
112 -- that is wrong, and we'd need to reduce the demand on s,
113 -- which is inconvenient
114 -- b) More important, consider
115 -- f (let x = R in x+x), where f is lazy
116 -- We still want to mark x as demanded, because it will be when we
117 -- enter the let. If we analyse f's arg with a Lazy demand, we'll
118 -- just mark x as Lazy
121 dmdAnal sigs dmd (Lit lit)
122 = (topDmdType, Lit lit)
124 dmdAnal sigs dmd (Var var)
125 = (dmdTransform sigs var dmd, Var var)
127 dmdAnal sigs dmd (Note n e)
128 = (dmd_ty, Note n e')
130 (dmd_ty, e') = dmdAnal sigs dmd' e
132 Coerce _ _ -> Eval -- This coerce usually arises from a recursive
133 other -> dmd -- newtype, and we don't want to look inside them
134 -- for exactly the same reason that we don't look
135 -- inside recursive products -- we might not reach
136 -- a fixpoint. So revert to a vanilla Eval demand
138 dmdAnal sigs dmd (App fun (Type ty))
139 = (fun_ty, App fun' (Type ty))
141 (fun_ty, fun') = dmdAnal sigs dmd fun
143 dmdAnal sigs dmd (App fun arg) -- Non-type arguments
144 = let -- [Type arg handled above]
145 (fun_ty, fun') = dmdAnal sigs (Call dmd) fun
146 (arg_ty, arg') = dmdAnal sigs arg_dmd arg
147 (arg_dmd, res_ty) = splitDmdTy fun_ty
149 (res_ty `bothType` arg_ty, App fun' arg')
151 dmdAnal sigs dmd (Lam var body)
154 (body_ty, body') = dmdAnal sigs dmd body
156 (body_ty, Lam var body')
158 | Call body_dmd <- dmd -- A call demand: good!
160 (body_ty, body') = dmdAnal sigs body_dmd body
161 (lam_ty, var') = annotateLamIdBndr body_ty var
163 (lam_ty, Lam var' body')
165 | otherwise -- Not enough demand on the lambda; but do the body
166 = let -- anyway to annotate it and gather free var info
167 (body_ty, body') = dmdAnal sigs Eval body
168 (lam_ty, var') = annotateLamIdBndr body_ty var
170 (deferType lam_ty, Lam var' body')
172 dmdAnal sigs dmd (Case scrut case_bndr [alt@(DataAlt dc,bndrs,rhs)])
173 | let tycon = dataConTyCon dc,
174 isProductTyCon tycon,
175 not (isRecursiveTyCon tycon)
177 (alt_ty, alt') = dmdAnalAlt sigs dmd alt
178 (alt_ty1, case_bndr') = annotateBndr alt_ty case_bndr
179 (_, bndrs', _) = alt'
181 -- Figure out whether the demand on the case binder is used, and use
182 -- that to set the scrut_dmd. This is utterly essential.
183 -- Consider f x = case x of y { (a,b) -> k y a }
184 -- If we just take scrut_demand = U(L,A), then we won't pass x to the
185 -- worker, so the worker will rebuild
186 -- x = (a, absent-error)
187 -- and that'll crash.
188 -- So at one stage I had:
189 -- dead_case_bndr = isAbsentDmd (idNewDemandInfo case_bndr')
190 -- keepity | dead_case_bndr = Drop
191 -- | otherwise = Keep
194 -- case x of y { (a,b) -> h y + a }
195 -- where h : U(LL) -> T
196 -- The above code would compute a Keep for x, since y is not Abs, which is silly
197 -- The insight is, of course, that a demand on y is a demand on the
198 -- scrutinee, so we need to `both` it with the scrut demand
200 scrut_dmd = Seq Drop Now [idNewDemandInfo b | b <- bndrs', isId b]
202 idNewDemandInfo case_bndr'
204 (scrut_ty, scrut') = dmdAnal sigs scrut_dmd scrut
206 (alt_ty1 `bothType` scrut_ty, Case scrut' case_bndr' [alt'])
208 dmdAnal sigs dmd (Case scrut case_bndr alts)
210 (alt_tys, alts') = mapAndUnzip (dmdAnalAlt sigs dmd) alts
211 (scrut_ty, scrut') = dmdAnal sigs Eval scrut
212 (alt_ty, case_bndr') = annotateBndr (foldr1 lubType alt_tys) case_bndr
214 -- pprTrace "dmdAnal:Case" (ppr alts $$ ppr alt_tys)
215 (alt_ty `bothType` scrut_ty, Case scrut' case_bndr' alts')
217 dmdAnal sigs dmd (Let (NonRec id rhs) body)
219 (sigs', lazy_fv, (id1, rhs')) = downRhs NotTopLevel sigs (id, rhs)
220 (body_ty, body') = dmdAnal sigs' dmd body
221 (body_ty1, id2) = annotateBndr body_ty id1
222 body_ty2 = addLazyFVs body_ty1 lazy_fv
224 -- pprTrace "dmdLet" (ppr id <+> ppr (sig,rhs_env))
225 (body_ty2, Let (NonRec id2 rhs') body')
227 dmdAnal sigs dmd (Let (Rec pairs) body)
229 bndrs = map fst pairs
230 (sigs', lazy_fv, pairs') = dmdFix NotTopLevel sigs pairs
231 (body_ty, body') = dmdAnal sigs' dmd body
232 body_ty1 = addLazyFVs body_ty lazy_fv
234 sigs' `seq` body_ty `seq`
236 (body_ty2, _) = annotateBndrs body_ty1 bndrs
237 -- Don't bother to add demand info to recursive
238 -- binders as annotateBndr does;
239 -- being recursive, we can't treat them strictly.
240 -- But we do need to remove the binders from the result demand env
242 (body_ty2, Let (Rec pairs') body')
245 dmdAnalAlt sigs dmd (con,bndrs,rhs)
247 (rhs_ty, rhs') = dmdAnal sigs dmd rhs
248 (alt_ty, bndrs') = annotateBndrs rhs_ty bndrs
250 (alt_ty, (con, bndrs', rhs'))
253 %************************************************************************
255 \subsection{Bindings}
257 %************************************************************************
260 dmdFix :: TopLevelFlag
261 -> SigEnv -- Does not include bindings for this binding
264 [(Id,CoreExpr)]) -- Binders annotated with stricness info
266 dmdFix top_lvl sigs pairs
267 = loop 1 initial_sigs pairs
269 bndrs = map fst pairs
270 initial_sigs = extendSigEnvList sigs [(id, (initial_sig id, top_lvl)) | id <- bndrs]
273 -> SigEnv -- Already contains the current sigs
275 -> (SigEnv, DmdEnv, [(Id,CoreExpr)])
277 | all (same_sig sigs sigs') bndrs = (sigs', lazy_fv, pairs')
278 -- Note: use pairs', not pairs. pairs' is the result of
279 -- processing the RHSs with sigs (= sigs'), whereas pairs
280 -- is the result of processing the RHSs with the *previous*
281 -- iteration of sigs.
282 | n >= 5 = pprTrace "dmdFix" (ppr n <+> (vcat
283 [ text "Sigs:" <+> ppr [(id,lookup sigs id, lookup sigs' id) | (id,_) <- pairs],
284 text "env:" <+> ppr (ufmToList sigs),
285 text "binds:" <+> ppr pairs]))
286 (loop (n+1) sigs' pairs')
287 | otherwise = {- pprTrace "dmdFixLoop" (ppr id_sigs) -} (loop (n+1) sigs' pairs')
289 -- Use the new signature to do the next pair
290 -- The occurrence analyser has arranged them in a good order
291 -- so this can significantly reduce the number of iterations needed
292 ((sigs',lazy_fv), pairs') = mapAccumL (my_downRhs top_lvl) (sigs, emptyDmdEnv) pairs
294 my_downRhs top_lvl (sigs,lazy_fv) (id,rhs)
295 = -- pprTrace "downRhs {" (ppr id <+> (ppr old_sig))
297 -- pprTrace "downRhsEnd" (ppr id <+> ppr new_sig <+> char '}' )
298 ((sigs', lazy_fv'), pair')
301 (sigs', lazy_fv1, pair') = downRhs top_lvl sigs (id,rhs)
302 lazy_fv' = plusUFM_C both lazy_fv lazy_fv1
303 -- old_sig = lookup sigs id
304 -- new_sig = lookup sigs' id
306 -- Get an initial strictness signature from the Id
307 -- itself. That way we make use of earlier iterations
308 -- of the fixpoint algorithm. (Cunning plan.)
309 -- Note that the cunning plan extends to the DmdEnv too,
310 -- since it is part of the strictness signature
311 initial_sig id = idNewStrictness_maybe id `orElse` botSig
313 same_sig sigs sigs' var = lookup sigs var == lookup sigs' var
314 lookup sigs var = case lookupVarEnv sigs var of
317 downRhs :: TopLevelFlag
318 -> SigEnv -> (Id, CoreExpr)
319 -> (SigEnv, DmdEnv, (Id, CoreExpr))
320 -- Process the RHS of the binding, add the strictness signature
321 -- to the Id, and augment the environment with the signature as well.
323 downRhs top_lvl sigs (id, rhs)
324 = (sigs', lazy_fv, (id', rhs'))
326 arity = exprArity rhs -- The idArity may not be up to date
327 (rhs_ty, rhs') = dmdAnal sigs (vanillaCall arity) rhs
328 (lazy_fv, sig_ty) = mkSigTy id arity rhs rhs_ty
329 id' = id `setIdNewStrictness` sig_ty
330 sigs' = extendSigEnv top_lvl sigs id sig_ty
333 %************************************************************************
335 \subsection{Strictness signatures and types}
337 %************************************************************************
340 mkSigTy :: Id -> Arity -> CoreExpr -> DmdType -> (DmdEnv, StrictSig)
341 -- Take a DmdType and turn it into a StrictSig
342 mkSigTy id arity rhs (DmdType fv dmds res)
343 = (lazy_fv, mkStrictSig id arity dmd_ty)
345 dmd_ty = DmdType strict_fv final_dmds res'
347 lazy_fv = filterUFM (not . isStrictDmd) fv
348 strict_fv = filterUFM isStrictDmd fv
349 -- We put the strict FVs in the DmdType of the Id, so
350 -- that at its call sites we unleash demands on its strict fvs.
351 -- An example is 'roll' in imaginary/wheel-sieve2
352 -- Something like this:
354 -- go y = if ... then roll (x-1) else x+1
357 -- We want to see that roll is strict in x, which is because
358 -- go is called. So we put the DmdEnv for x in go's DmdType.
361 -- f :: Int -> Int -> Int
362 -- f x y = let t = x+1
363 -- h z = if z==0 then t else
364 -- if z==1 then x+1 else
368 -- Calling h does indeed evaluate x, but we can only see
369 -- that if we unleash a demand on x at the call site for t.
371 -- Incidentally, here's a place where lambda-lifting h would
372 -- lose the cigar --- we couldn't see the joint strictness in t/x
375 -- We don't want to put *all* the fv's from the RHS into the
376 -- DmdType, because that makes fixpointing very slow --- the
377 -- DmdType gets full of lazy demands that are slow to converge.
379 lazified_dmds = map lazify dmds
380 -- Get rid of defers in the arguments
381 final_dmds = setUnpackStrategy lazified_dmds
382 -- Set the unpacking strategy
384 res' = case (dmds, res) of
385 ([], RetCPR) | not (exprIsValue rhs) -> TopRes
387 -- If the rhs is a thunk, we forget the CPR info, because
388 -- it is presumably shared (else it would have been inlined, and
389 -- so we'd lose sharing if w/w'd it into a function.
391 -- DONE IN OLD CPR ANALYSER, BUT NOT YET HERE
392 -- Also, if the strictness analyser has figured out that it's strict,
393 -- the let-to-case transformation will happen, so again it's good.
394 -- (CPR analysis runs before the simplifier has had a chance to do
395 -- the let-to-case transform.)
396 -- This made a big difference to PrelBase.modInt, which had something like
397 -- modInt = \ x -> let r = ... -> I# v in
398 -- ...body strict in r...
399 -- r's RHS isn't a value yet; but modInt returns r in various branches, so
400 -- if r doesn't have the CPR property then neither does modInt
403 The unpack strategy determines whether we'll *really* unpack the argument,
404 or whether we'll just remember its strictness. If unpacking would give
405 rise to a *lot* of worker args, we may decide not to unpack after all.
408 setUnpackStrategy :: [Demand] -> [Demand]
410 = snd (go (opt_MaxWorkerArgs - nonAbsentArgs ds) ds)
412 go :: Int -- Max number of args available for sub-components of [Demand]
414 -> (Int, [Demand]) -- Args remaining after subcomponents of [Demand] are unpacked
416 go n (Seq keep _ cs : ds)
417 | n' >= 0 = Seq keep Now cs' `cons` go n'' ds
418 | otherwise = Eval `cons` go n ds
421 n' = n + box - non_abs_args
424 Drop -> 1 -- Add one to the budget if we drop the top-level arg
425 non_abs_args = nonAbsentArgs cs
426 -- Delete # of non-absent args to which we'll now be committed
428 go n (d:ds) = d `cons` go n ds
431 cons d (n,ds) = (n, d:ds)
433 nonAbsentArgs :: [Demand] -> Int
435 nonAbsentArgs (Abs : ds) = nonAbsentArgs ds
436 nonAbsentArgs (d : ds) = 1 + nonAbsentArgs ds
440 %************************************************************************
442 \subsection{Strictness signatures and types}
444 %************************************************************************
447 splitDmdTy :: DmdType -> (Demand, DmdType)
448 -- Split off one function argument
449 splitDmdTy (DmdType fv (dmd:dmds) res_ty) = (dmd, DmdType fv dmds res_ty)
450 splitDmdTy ty@(DmdType fv [] TopRes) = (topDmd, ty)
451 splitDmdTy ty@(DmdType fv [] BotRes) = (Abs, ty)
452 -- We already have a suitable demand on all
453 -- free vars, so no need to add more!
454 splitDmdTy (DmdType fv [] RetCPR) = panic "splitDmdTy"
458 unitVarDmd var dmd = DmdType (unitVarEnv var dmd) [] TopRes
460 addVarDmd top_lvl dmd_ty@(DmdType fv ds res) var dmd
461 | isTopLevel top_lvl = dmd_ty -- Don't record top level things
462 | otherwise = DmdType (extendVarEnv fv var dmd) ds res
464 addLazyFVs (DmdType fv ds res) lazy_fvs
465 = DmdType (plusUFM_C both fv lazy_fvs) ds res
467 annotateBndr :: DmdType -> Var -> (DmdType, Var)
468 -- The returned env has the var deleted
469 -- The returned var is annotated with demand info
470 -- No effect on the argument demands
471 annotateBndr dmd_ty@(DmdType fv ds res) var
472 | isTyVar var = (dmd_ty, var)
473 | otherwise = (DmdType fv' ds res, setIdNewDemandInfo var dmd)
475 (fv', dmd) = removeFV fv var res
477 annotateBndrs = mapAccumR annotateBndr
479 annotateLamIdBndr dmd_ty@(DmdType fv ds res) id
480 -- For lambdas we add the demand to the argument demands
481 -- Only called for Ids
483 (DmdType fv' (hacked_dmd:ds) res, setIdNewDemandInfo id hacked_dmd)
485 (fv', dmd) = removeFV fv id res
486 hacked_dmd = case dmd of
490 -- This gross hack is needed because otherwise we label
491 -- a lambda binder with demand 'B'. But in terms of calling
492 -- conventions that's Abs, because we don't pass it. But
493 -- when we do a w/w split we get
494 -- fw x = (\x y:B -> ...) x (error "oops")
495 -- And then the simplifier things the 'B' is a strict demand
496 -- and evaluates the (error "oops"). Sigh
498 removeFV fv var res = (fv', dmd)
500 fv' = fv `delVarEnv` var
501 dmd = lookupVarEnv fv var `orElse` deflt
502 deflt | isBotRes res = Bot
506 %************************************************************************
508 \subsection{Strictness signatures}
510 %************************************************************************
513 type SigEnv = VarEnv (StrictSig, TopLevelFlag)
514 -- We use the SigEnv to tell us whether to
515 -- record info about a variable in the DmdEnv
516 -- We do so if it's a LocalId, but not top-level
518 -- The DmdEnv gives the demand on the free vars of the function
519 -- when it is given enough args to satisfy the strictness signature
521 emptySigEnv = emptyVarEnv
523 extendSigEnv :: TopLevelFlag -> SigEnv -> Id -> StrictSig -> SigEnv
524 extendSigEnv top_lvl env var sig = extendVarEnv env var (sig, top_lvl)
526 extendSigEnvList = extendVarEnvList
528 dmdTransform :: SigEnv -- The strictness environment
529 -> Id -- The function
530 -> Demand -- The demand on the function
531 -> DmdType -- The demand type of the function in this context
532 -- Returned DmdEnv includes the demand on
533 -- this function plus demand on its free variables
535 dmdTransform sigs var dmd
537 ------ DATA CONSTRUCTOR
538 | isDataConId var, -- Data constructor
539 Seq k Now ds <- res_dmd, -- and the demand looks inside its fields
540 let StrictSig dmd_ty = idNewStrictness var, -- It must have a strictness sig
541 let DmdType _ con_ds con_res = dmd_ty
542 = if length con_ds == length ds then -- Saturated, so unleash the demand
543 -- ds can be empty, when we are just seq'ing the thing
546 Keep -> zipWith lub ds con_ds
548 -- Important! If we Keep the constructor application, then
549 -- we need the demands the constructor places (usually lazy)
550 -- If not, we don't need to. For example:
551 -- f p@(x,y) = (p,y) -- S(AL)
553 -- It's vital that we don't calculate Absent for a!
555 mkDmdType emptyDmdEnv arg_ds con_res
556 -- Must remember whether it's a product, hence con_res, not TopRes
560 ------ IMPORTED FUNCTION
561 | isGlobalId var, -- Imported function
562 let StrictSig dmd_ty = getNewStrictness var
563 = if dmdTypeDepth dmd_ty <= call_depth then -- Saturated, so unleash the demand
568 ------ LOCAL LET/REC BOUND THING
569 | Just (StrictSig dmd_ty, top_lvl) <- lookupVarEnv sigs var
571 fn_ty | dmdTypeDepth dmd_ty <= call_depth = dmd_ty
572 | otherwise = deferType dmd_ty
573 -- NB: it's important to use deferType, and not just return topDmdType
574 -- Consider let { f x y = p + x } in f 1
575 -- The application isn't saturated, but we must nevertheless propagate
576 -- a lazy demand for p!
578 addVarDmd top_lvl fn_ty var dmd
580 ------ LOCAL NON-LET/REC BOUND THING
581 | otherwise -- Default case
585 (call_depth, res_dmd) = splitCallDmd dmd
589 %************************************************************************
593 %************************************************************************
596 splitCallDmd :: Demand -> (Int, Demand)
597 splitCallDmd (Call d) = case splitCallDmd d of
599 splitCallDmd d = (0, d)
601 vanillaCall :: Arity -> Demand
603 vanillaCall n = Call (vanillaCall (n-1))
605 deferType :: DmdType -> DmdType
606 deferType (DmdType fv _ _) = DmdType (mapVarEnv defer fv) [] TopRes
607 -- Notice that we throw away info about both arguments and results
608 -- For example, f = let ... in \x -> x
609 -- We don't want to get a stricness type V->T for f.
611 defer :: Demand -> Demand
614 lazify :: Demand -> Demand
615 -- The 'Defer' demands are just Lazy at function boundaries
616 -- Ugly! Ask John how to improve it.
617 lazify (Seq k Defer ds) = Lazy
618 lazify (Seq k Now ds) = Seq k Now (map lazify ds)
619 lazify Bot = Abs -- Don't pass args that are consumed by bottom/err
625 betterStrictness :: StrictSig -> StrictSig -> Bool
626 betterStrictness (StrictSig t1) (StrictSig t2) = betterDmdType t1 t2
628 betterDmdType t1 t2 = (t1 `lubType` t2) == t2
630 betterDemand :: Demand -> Demand -> Bool
631 -- If d1 `better` d2, and d2 `better` d2, then d1==d2
632 betterDemand d1 d2 = (d1 `lub` d2) == d2
634 squashDmdEnv (StrictSig (DmdType fv ds res)) = StrictSig (DmdType emptyDmdEnv ds res)
638 %************************************************************************
640 \subsection{LUB and BOTH}
642 %************************************************************************
645 lub :: Demand -> Demand -> Demand
654 lub Abs Bot = Abs -- E.g f x y = if ... then x else error x
655 -- Then for y we get Abs `lub` Bot, and we really
659 lub Abs (Seq k _ ds) = Seq k Defer ds -- Very important ('radicals' example)
665 lub Eval (Seq k Now ds) = Eval -- Urk! Is this monotonic?
666 -- Was (incorrectly):
667 -- lub Eval (Seq k Now ds) = Seq Keep Now ds
669 -- Eval `lub` U(VV) is not S(VV)
670 -- (because the components aren't necessarily evaluated)
672 -- Was (correctly, but pessimistically):
673 -- lub Eval (Seq k Now ds) = Eval
674 -- Pessimistic because
676 -- f n (x:xs) = f (n+x) xs
677 -- Here we want to do better than just V for n. It's
678 -- unboxed in the (x:xs) case, and we might be prepared to
679 -- rebox it in the [] case.
680 -- To achieve this we could perhaps consider Eval to be equivalent to
683 lub Eval (Seq k Defer ds) = Lazy
686 lub (Call d1) (Call d2) = Call (lub d1 d2)
688 lub (Seq k1 l1 ds1) (Seq k2 l2 ds2) = Seq (k1 `vee` k2) (l1 `or_defer` l2) (lubs ds1 ds2)
690 -- The last clauses deal with the remaining cases for Call and Seq
691 lub d1@(Call _) d2@(Seq _ _ _) = pprPanic "lub" (ppr d1 $$ ppr d2)
692 lub d1 d2 = lub d2 d1
694 -- A Seq can have an empty list of demands, in the polymorphic case.
697 lubs ds1 ds2 = ASSERT( length ds1 == length ds2 ) zipWith lub ds1 ds2
699 or_defer Now Now = Now
702 -------------------------
703 -- Consider (if x then y else []) with demand V
704 -- Then the first branch gives {y->V} and the second
705 -- *implicitly* has {y->A}. So we must put {y->(V `lub` A)}
706 -- in the result env.
707 lubType (DmdType fv1 ds1 r1) (DmdType fv2 ds2 r2)
708 = DmdType lub_fv2 (zipWith lub ds1 ds2) (r1 `lubRes` r2)
710 lub_fv = plusUFM_C lub fv1 fv2
711 lub_fv1 = modifyEnv (not (isBotRes r1)) (Abs `lub`) fv2 fv1 lub_fv
712 lub_fv2 = modifyEnv (not (isBotRes r2)) (Abs `lub`) fv1 fv2 lub_fv1
713 -- lub is the identity for Bot
715 -------------------------
718 lubRes RetCPR RetCPR = RetCPR
719 lubRes r1 r2 = TopRes
721 -----------------------------------
722 vee :: Keepity -> Keepity -> Keepity
726 -----------------------------------
727 both :: Demand -> Demand -> Demand
732 -- The experimental one
733 -- The idea is that (error x) places on x
734 -- both demand Bot (like on all free vars)
735 -- and demand Eval (for the arg to error)
736 -- and we want the result to be Eval.
750 both Lazy (Seq k l ds) = Seq Keep l ds
752 -- Notice that the Seq case ensures that we have the
753 -- boxed value. The equation originally said
754 -- both (Seq k Now ds) = Seq Keep Now ds
755 -- but it's important that the Keep is switched on even
756 -- for a deferred demand. Otherwise a (Seq Drop Now [])
757 -- might both'd with the result, and then we won't pass
758 -- the boxed value. Here's an example:
759 -- (x-1) `seq` (x+1, x)
760 -- From the (x+1, x) we get (U*(V) `both` L), which must give S*(V)
761 -- From (x-1) we get U(V). Combining, we must get S(V).
762 -- If we got U*(V) from the pair, we'd end up with U(V), and that
763 -- can be a disaster if a component of the data structure is absent.
764 -- [Disaster = enter an absent argument.]
766 both Eval (Seq k l ds) = Seq Keep Now ds
767 both Eval (Call d) = Call d
770 both (Seq k1 Defer ds1) (Seq k2 Defer ds2) = Seq (k1 `vee` k2) Defer (boths ds1 ds2)
771 both (Seq k1 l1 ds1) (Seq k2 l2 ds2) = Seq (k1 `vee` k2) Now (boths ds1' ds2')
773 ds1' = case l1 of { Now -> ds1; Defer -> map defer ds1 }
774 ds2' = case l2 of { Now -> ds2; Defer -> map defer ds2 }
776 both (Call d1) (Call d2) = Call (d1 `both` d2)
778 -- The last clauses deal with the remaining cases for Call and Seq
779 both d1@(Call _) d2@(Seq _ _ _) = pprPanic "both" (ppr d1 $$ ppr d2)
780 both d1 d2 = both d2 d1
782 -----------------------------------
783 -- A Seq can have an empty list of demands, in the polymorphic case.
786 boths ds1 ds2 = ASSERT( length ds1 == length ds2 ) zipWith both ds1 ds2
788 -----------------------------------
789 -- (t1 `bothType` t2) takes the argument/result info from t1,
790 -- using t2 just for its free-var info
791 bothType (DmdType fv1 ds1 r1) (DmdType fv2 ds2 r2)
792 = DmdType both_fv2 ds1 r1
794 both_fv = plusUFM_C both fv1 fv2
795 both_fv1 = modifyEnv (isBotRes r1) (`both` Bot) fv2 fv1 both_fv
796 both_fv2 = modifyEnv (isBotRes r2) (`both` Bot) fv1 fv2 both_fv1
797 -- both is the identity for Abs
801 modifyEnv :: Bool -- No-op if False
802 -> (Demand -> Demand) -- The zapper
803 -> DmdEnv -> DmdEnv -- Env1 and Env2
804 -> DmdEnv -> DmdEnv -- Transform this env
805 -- Zap anything in Env1 but not in Env2
806 -- Assume: dom(env) includes dom(Env1) and dom(Env2)
808 modifyEnv need_to_modify zapper env1 env2 env
809 | need_to_modify = foldr zap env (keysUFM (env1 `minusUFM` env2))
812 zap uniq env = addToUFM_Directly env uniq (zapper current_val)
814 current_val = expectJust "modifyEnv" (lookupUFM_Directly env uniq)
818 %************************************************************************
820 \subsection{Miscellaneous
822 %************************************************************************
826 get_changes binds = vcat (map get_changes_bind binds)
828 get_changes_bind (Rec pairs) = vcat (map get_changes_pr pairs)
829 get_changes_bind (NonRec id rhs) = get_changes_pr (id,rhs)
831 get_changes_pr (id,rhs)
832 | isImplicitId id = empty -- We don't look inside these
833 | otherwise = get_changes_var id $$ get_changes_expr rhs
836 | isId var = get_changes_str var $$ get_changes_dmd var
839 get_changes_expr (Type t) = empty
840 get_changes_expr (Var v) = empty
841 get_changes_expr (Lit l) = empty
842 get_changes_expr (Note n e) = get_changes_expr e
843 get_changes_expr (App e1 e2) = get_changes_expr e1 $$ get_changes_expr e2
844 get_changes_expr (Lam b e) = {- get_changes_var b $$ -} get_changes_expr e
845 get_changes_expr (Let b e) = get_changes_bind b $$ get_changes_expr e
846 get_changes_expr (Case e b a) = get_changes_expr e $$ {- get_changes_var b $$ -} vcat (map get_changes_alt a)
848 get_changes_alt (con,bs,rhs) = {- vcat (map get_changes_var bs) $$ -} get_changes_expr rhs
851 | new_better && old_better = empty
852 | new_better = message "BETTER"
853 | old_better = message "WORSE"
854 | otherwise = message "INCOMPARABLE"
856 message word = text word <+> text "strictness for" <+> ppr id <+> info
857 info = (text "Old" <+> ppr old) $$ (text "New" <+> ppr new)
858 new = squashDmdEnv (idNewStrictness id) -- Don't report diffs in the env
859 old = newStrictnessFromOld id
860 old_better = old `betterStrictness` new
861 new_better = new `betterStrictness` old
864 | isUnLiftedType (idType id) = empty -- Not useful
865 | new_better && old_better = empty
866 | new_better = message "BETTER"
867 | old_better = message "WORSE"
868 | otherwise = message "INCOMPARABLE"
870 message word = text word <+> text "demand for" <+> ppr id <+> info
871 info = (text "Old" <+> ppr old) $$ (text "New" <+> ppr new)
872 new = lazify (idNewDemandInfo id) -- Lazify to avoid spurious improvements
873 old = newDemand (idDemandInfo id)
874 new_better = new `betterDemand` old
875 old_better = old `betterDemand` new