2 % (c) The GRASP/AQUA Project, Glasgow University, 1993-1998
10 module DmdAnal ( dmdAnalPgm ) 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, idInfo, idArity, idCprInfo, idDemandInfo,
21 modifyIdInfo, 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 ErrUtils ( dumpIfSet_dyn )
32 import Util ( mapAndUnzip, mapAccumL, mapAccumR, zipWithEqual )
33 import BasicTypes ( Arity, TopLevelFlag(..), isTopLevel )
34 import Maybes ( orElse, expectJust )
41 * set a noinline pragma on bottoming Ids
43 * Consider f x = x+1 `fatbar` error (show x)
44 We'd like to unbox x, even if that means reboxing it in the error case.
47 instance Outputable TopLevelFlag where
51 %************************************************************************
53 \subsection{Top level stuff}
55 %************************************************************************
58 dmdAnalPgm :: DynFlags -> [CoreBind] -> IO [CoreBind]
59 dmdAnalPgm dflags binds
61 showPass dflags "Demand analysis" ;
62 let { binds_plus_dmds = do_prog binds ;
63 dmd_changes = get_changes binds_plus_dmds } ;
64 endPass dflags "Demand analysis"
65 Opt_D_dump_stranal binds_plus_dmds ;
66 printDump (text "Changes in demands" $$ dmd_changes) ;
67 return binds_plus_dmds
70 do_prog :: [CoreBind] -> [CoreBind]
71 do_prog binds = snd $ mapAccumL dmdAnalTopBind emptySigEnv binds
73 dmdAnalTopBind :: SigEnv
76 dmdAnalTopBind sigs (NonRec id rhs)
77 | isImplicitId id -- Don't touch the info on constructors, selectors etc
78 = (sigs, NonRec id rhs) -- It's pre-computed in MkId.lhs
81 (sigs', _, (id', rhs')) = downRhs TopLevel sigs (id, rhs)
83 (sigs', NonRec id' rhs')
85 dmdAnalTopBind sigs (Rec pairs)
87 (sigs', _, pairs') = dmdFix TopLevel sigs pairs
93 %************************************************************************
95 \subsection{The analyser itself}
97 %************************************************************************
100 dmdAnal :: SigEnv -> Demand -> CoreExpr -> (DmdType, CoreExpr)
102 dmdAnal sigs Abs e = (topDmdType, e)
104 dmdAnal sigs Lazy e = let
105 (res_ty, e') = dmdAnal sigs Eval e
107 (deferType res_ty, e')
108 -- It's important not to analyse e with a lazy demand because
109 -- a) When we encounter case s of (a,b) ->
110 -- we demand s with U(d1d2)... but if the overall demand is lazy
111 -- that is wrong, and we'd need to reduce the demand on s,
112 -- which is inconvenient
113 -- b) More important, consider
114 -- f (let x = R in x+x), where f is lazy
115 -- We still want to mark x as demanded, because it will be when we
116 -- enter the let. If we analyse f's arg with a Lazy demand, we'll
117 -- just mark x as Lazy
120 dmdAnal sigs dmd (Lit lit)
121 = (topDmdType, Lit lit)
123 dmdAnal sigs dmd (Var var)
124 = (dmdTransform sigs var dmd, Var var)
126 dmdAnal sigs dmd (Note n e)
127 = (dmd_ty, Note n e')
129 (dmd_ty, e') = dmdAnal sigs dmd e
131 dmdAnal sigs dmd (App fun (Type ty))
132 = (fun_ty, App fun' (Type ty))
134 (fun_ty, fun') = dmdAnal sigs dmd fun
136 dmdAnal sigs dmd (App fun arg) -- Non-type arguments
137 = let -- [Type arg handled above]
138 (fun_ty, fun') = dmdAnal sigs (Call dmd) fun
139 (arg_ty, arg') = dmdAnal sigs arg_dmd arg
140 (arg_dmd, res_ty) = splitDmdTy fun_ty
142 (res_ty `bothType` arg_ty, App fun' arg')
144 dmdAnal sigs dmd (Lam var body)
147 (body_ty, body') = dmdAnal sigs dmd body
149 (body_ty, Lam var body')
151 | Call body_dmd <- dmd -- A call demand: good!
153 (body_ty, body') = dmdAnal sigs body_dmd body
154 (lam_ty, var') = annotateLamIdBndr body_ty var
156 (lam_ty, Lam var' body')
158 | otherwise -- Not enough demand on the lambda; but do the body
159 = let -- anyway to annotate it and gather free var info
160 (body_ty, body') = dmdAnal sigs Eval body
161 (lam_ty, var') = annotateLamIdBndr body_ty var
163 (deferType lam_ty, Lam var' body')
165 dmdAnal sigs dmd (Case scrut case_bndr [alt@(DataAlt dc,bndrs,rhs)])
166 | let tycon = dataConTyCon dc,
167 isProductTyCon tycon,
168 not (isRecursiveTyCon tycon)
170 bndr_ids = filter isId bndrs
171 (alt_ty, alt') = dmdAnalAlt sigs dmd alt
172 (alt_ty1, case_bndr') = annotateBndr alt_ty case_bndr
173 (_, bndrs', _) = alt'
174 scrut_dmd = Seq Drop Now [idNewDemandInfo b | b <- bndrs', isId b]
175 (scrut_ty, scrut') = dmdAnal sigs scrut_dmd scrut
177 (alt_ty1 `bothType` scrut_ty, Case scrut' case_bndr' [alt'])
179 dmdAnal sigs dmd (Case scrut case_bndr alts)
181 (alt_tys, alts') = mapAndUnzip (dmdAnalAlt sigs dmd) alts
182 (scrut_ty, scrut') = dmdAnal sigs Eval scrut
183 (alt_ty, case_bndr') = annotateBndr (foldr1 lubType alt_tys) case_bndr
185 -- pprTrace "dmdAnal:Case" (ppr alts $$ ppr alt_tys)
186 (alt_ty `bothType` scrut_ty, Case scrut' case_bndr' alts')
188 dmdAnal sigs dmd (Let (NonRec id rhs) body)
190 (sigs', lazy_fv, (id1, rhs')) = downRhs NotTopLevel sigs (id, rhs)
191 (body_ty, body') = dmdAnal sigs' dmd body
192 (body_ty1, id2) = annotateBndr body_ty id1
193 body_ty2 = addLazyFVs body_ty1 lazy_fv
195 -- pprTrace "dmdLet" (ppr id <+> ppr (sig,rhs_env))
196 (body_ty2, Let (NonRec id2 rhs') body')
198 dmdAnal sigs dmd (Let (Rec pairs) body)
200 bndrs = map fst pairs
201 (sigs', lazy_fv, pairs') = dmdFix NotTopLevel sigs pairs
202 (body_ty, body') = dmdAnal sigs' dmd body
203 body_ty1 = addLazyFVs body_ty lazy_fv
205 sigs' `seq` body_ty `seq`
207 (body_ty2, _) = annotateBndrs body_ty1 bndrs
208 -- Don't bother to add demand info to recursive
209 -- binders as annotateBndr does;
210 -- being recursive, we can't treat them strictly.
211 -- But we do need to remove the binders from the result demand env
213 (body_ty2, Let (Rec pairs') body')
216 dmdAnalAlt sigs dmd (con,bndrs,rhs)
218 (rhs_ty, rhs') = dmdAnal sigs dmd rhs
219 (alt_ty, bndrs') = annotateBndrs rhs_ty bndrs
221 (alt_ty, (con, bndrs', rhs'))
224 %************************************************************************
226 \subsection{Bindings}
228 %************************************************************************
231 dmdFix :: TopLevelFlag
232 -> SigEnv -- Does not include bindings for this binding
235 [(Id,CoreExpr)]) -- Binders annotated with stricness info
237 dmdFix top_lvl sigs pairs
238 = loop 1 initial_sigs pairs
240 bndrs = map fst pairs
241 initial_sigs = extendSigEnvList sigs [(id, (initial_sig id, top_lvl)) | id <- bndrs]
244 -> SigEnv -- Already contains the current sigs
246 -> (SigEnv, DmdEnv, [(Id,CoreExpr)])
248 | all (same_sig sigs sigs') bndrs = (sigs', lazy_fv, pairs')
249 -- Note: use pairs', not pairs. pairs' is the result of
250 -- processing the RHSs with sigs (= sigs'), whereas pairs
251 -- is the result of processing the RHSs with the *previous*
252 -- iteration of sigs.
253 | n >= 5 = pprTrace "dmdFix" (ppr n <+> ppr pairs) (loop (n+1) sigs' pairs')
254 | otherwise = {- pprTrace "dmdFixLoop" (ppr id_sigs) -} (loop (n+1) sigs' pairs')
256 -- Use the new signature to do the next pair
257 -- The occurrence analyser has arranged them in a good order
258 -- so this can significantly reduce the number of iterations needed
259 ((sigs',lazy_fv), pairs') = mapAccumL (my_downRhs top_lvl) (sigs, emptyDmdEnv) pairs
261 my_downRhs top_lvl (sigs,lazy_fv) (id,rhs)
262 = -- pprTrace "downRhs {" (ppr id <+> (ppr old_sig))
264 -- pprTrace "downRhsEnd" (ppr id <+> ppr new_sig <+> char '}' )
265 ((sigs', lazy_fv'), pair')
268 (sigs', lazy_fv1, pair') = downRhs top_lvl sigs (id,rhs)
269 lazy_fv' = plusUFM_C both lazy_fv lazy_fv1
270 old_sig = lookup sigs id
271 new_sig = lookup sigs' id
273 -- Get an initial strictness signature from the Id
274 -- itself. That way we make use of earlier iterations
275 -- of the fixpoint algorithm. (Cunning plan.)
276 -- Note that the cunning plan extends to the DmdEnv too,
277 -- since it is part of the strictness signature
278 initial_sig id = idNewStrictness_maybe id `orElse` botSig
280 same_sig sigs sigs' var = lookup sigs var == lookup sigs' var
281 lookup sigs var = case lookupVarEnv sigs var of
284 downRhs :: TopLevelFlag
285 -> SigEnv -> (Id, CoreExpr)
286 -> (SigEnv, DmdEnv, (Id, CoreExpr))
287 -- Process the RHS of the binding, add the strictness signature
288 -- to the Id, and augment the environment with the signature as well.
290 downRhs top_lvl sigs (id, rhs)
291 = (sigs', lazy_fv, (id', rhs'))
293 arity = exprArity rhs -- The idArity may not be up to date
294 (rhs_ty, rhs') = dmdAnal sigs (vanillaCall arity) rhs
295 (lazy_fv, sig_ty) = mkSigTy id arity rhs rhs_ty
296 id' = id `setIdNewStrictness` sig_ty
297 sigs' = extendSigEnv top_lvl sigs id sig_ty
300 %************************************************************************
302 \subsection{Strictness signatures and types}
304 %************************************************************************
307 mkSigTy :: Id -> Arity -> CoreExpr -> DmdType -> (DmdEnv, StrictSig)
308 -- Take a DmdType and turn it into a StrictSig
309 mkSigTy id arity rhs (DmdType fv dmds res)
310 = (lazy_fv, mkStrictSig id arity dmd_ty)
312 dmd_ty = DmdType strict_fv lazified_dmds res'
314 lazy_fv = filterUFM (not . isStrictDmd) fv
315 strict_fv = filterUFM isStrictDmd fv
316 -- We put the strict FVs in the DmdType of the Id, so
317 -- that at its call sites we unleash demands on its strict fvs.
318 -- An example is 'roll' in imaginary/wheel-sieve2
319 -- Something like this:
321 -- go y = if ... then roll (x-1) else x+1
324 -- We want to see that roll is strict in x, which is because
325 -- go is called. So we put the DmdEnv for x in go's DmdType.
328 -- f :: Int -> Int -> Int
329 -- f x y = let t = x+1
330 -- h z = if z==0 then t else
331 -- if z==1 then x+1 else
335 -- Calling h does indeed evaluate x, but we can only see
336 -- that if we unleash a demand on x at the call site for t.
338 -- Incidentally, here's a place where lambda-lifting h would
339 -- lose the cigar --- we couldn't see the joint strictness in t/x
342 -- We don't want to put *all* the fv's from the RHS into the
343 -- DmdType, because that makes fixpointing very slow --- the
344 -- DmdType gets full of lazy demands that are slow to converge.
346 lazified_dmds = map lazify dmds
347 -- Get rid of defers in the arguments
348 final_dmds = setUnpackStrategy lazified_dmds
349 -- Set the unpacking strategy
351 res' = case (dmds, res) of
352 ([], RetCPR) | not (exprIsValue rhs) -> TopRes
354 -- If the rhs is a thunk, we forget the CPR info, because
355 -- it is presumably shared (else it would have been inlined, and
356 -- so we'd lose sharing if w/w'd it into a function.
358 -- DONE IN OLD CPR ANALYSER, BUT NOT YET HERE
359 -- Also, if the strictness analyser has figured out that it's strict,
360 -- the let-to-case transformation will happen, so again it's good.
361 -- (CPR analysis runs before the simplifier has had a chance to do
362 -- the let-to-case transform.)
363 -- This made a big difference to PrelBase.modInt, which had something like
364 -- modInt = \ x -> let r = ... -> I# v in
365 -- ...body strict in r...
366 -- r's RHS isn't a value yet; but modInt returns r in various branches, so
367 -- if r doesn't have the CPR property then neither does modInt
370 The unpack strategy determines whether we'll *really* unpack the argument,
371 or whether we'll just remember its strictness. If unpacking would give
372 rise to a *lot* of worker args, we may decide not to unpack after all.
375 setUnpackStrategy :: [Demand] -> [Demand]
377 = snd (go (opt_MaxWorkerArgs - nonAbsentArgs ds) ds)
379 go :: Int -- Max number of args available for sub-components of [Demand]
381 -> (Int, [Demand]) -- Args remaining after subcomponents of [Demand] are unpacked
383 go n (Seq keep _ cs : ds)
384 | n' >= 0 = Seq keep Now cs' `cons` go n'' ds
385 | otherwise = Eval `cons` go n ds
388 n' = n + box - non_abs_args
391 Drop -> 1 -- Add one to the budget if we drop the top-level arg
392 non_abs_args = nonAbsentArgs cs
393 -- Delete # of non-absent args to which we'll now be committed
395 go n (d:ds) = d `cons` go n ds
398 cons d (n,ds) = (n, d:ds)
400 nonAbsentArgs :: [Demand] -> Int
402 nonAbsentArgs (Abs : ds) = nonAbsentArgs ds
403 nonAbsentArgs (d : ds) = 1 + nonAbsentArgs ds
407 %************************************************************************
409 \subsection{Strictness signatures and types}
411 %************************************************************************
414 splitDmdTy :: DmdType -> (Demand, DmdType)
415 -- Split off one function argument
416 splitDmdTy (DmdType fv (dmd:dmds) res_ty) = (dmd, DmdType fv dmds res_ty)
417 splitDmdTy ty@(DmdType fv [] TopRes) = (topDmd, ty)
418 splitDmdTy ty@(DmdType fv [] BotRes) = (Abs, ty)
419 -- We already have a suitable demand on all
420 -- free vars, so no need to add more!
421 splitDmdTy (DmdType fv [] RetCPR) = panic "splitDmdTy"
425 unitVarDmd var dmd = DmdType (unitVarEnv var dmd) [] TopRes
427 addVarDmd top_lvl dmd_ty@(DmdType fv ds res) var dmd
428 | isTopLevel top_lvl = dmd_ty -- Don't record top level things
429 | otherwise = DmdType (extendVarEnv fv var dmd) ds res
431 addLazyFVs (DmdType fv ds res) lazy_fvs
432 = DmdType (plusUFM_C both fv lazy_fvs) ds res
434 annotateBndr :: DmdType -> Var -> (DmdType, Var)
435 -- The returned env has the var deleted
436 -- The returned var is annotated with demand info
437 -- No effect on the argument demands
438 annotateBndr dmd_ty@(DmdType fv ds res) var
439 | isTyVar var = (dmd_ty, var)
440 | otherwise = (DmdType fv' ds res, setIdNewDemandInfo var dmd)
442 (fv', dmd) = removeFV fv var res
444 annotateBndrs = mapAccumR annotateBndr
446 annotateLamIdBndr dmd_ty@(DmdType fv ds res) id
447 -- For lambdas we add the demand to the argument demands
448 -- Only called for Ids
450 (DmdType fv' (dmd:ds) res, setIdNewDemandInfo id dmd)
452 (fv', dmd) = removeFV fv id res
454 removeFV fv var res = (fv', dmd)
456 fv' = fv `delVarEnv` var
457 dmd = lookupVarEnv fv var `orElse` deflt
458 deflt | isBotRes res = Bot
462 %************************************************************************
464 \subsection{Strictness signatures}
466 %************************************************************************
469 type SigEnv = VarEnv (StrictSig, TopLevelFlag)
470 -- We use the SigEnv to tell us whether to
471 -- record info about a variable in the DmdEnv
472 -- We do so if it's a LocalId, but not top-level
474 -- The DmdEnv gives the demand on the free vars of the function
475 -- when it is given enough args to satisfy the strictness signature
477 emptySigEnv = emptyVarEnv
479 extendSigEnv :: TopLevelFlag -> SigEnv -> Id -> StrictSig -> SigEnv
480 extendSigEnv top_lvl env var sig = extendVarEnv env var (sig, top_lvl)
482 extendSigEnvList = extendVarEnvList
484 dmdTransform :: SigEnv -- The strictness environment
485 -> Id -- The function
486 -> Demand -- The demand on the function
487 -> DmdType -- The demand type of the function in this context
488 -- Returned DmdEnv includes the demand on
489 -- this function plus demand on its free variables
491 dmdTransform sigs var dmd
493 ------ DATA CONSTRUCTOR
494 | isDataConId var, -- Data constructor
495 Seq k Now ds <- res_dmd, -- and the demand looks inside its fields
496 let StrictSig dmd_ty = idNewStrictness var -- It must have a strictness sig
497 = if dmdTypeDepth dmd_ty == length ds then -- Saturated, so unleash the demand
498 -- ds can be empty, when we are just seq'ing the thing
499 mkDmdType emptyDmdEnv ds (dmdTypeRes dmd_ty)
500 -- Need to extract whether it's a product, hence dmdTypeRes
504 ------ IMPORTED FUNCTION
505 | isGlobalId var, -- Imported function
506 let StrictSig dmd_ty = getNewStrictness var
507 = if dmdTypeDepth dmd_ty <= call_depth then -- Saturated, so unleash the demand
512 ------ LOCAL LET/REC BOUND THING
513 | Just (StrictSig dmd_ty, top_lvl) <- lookupVarEnv sigs var
515 fn_ty | dmdTypeDepth dmd_ty <= call_depth = dmd_ty
516 | otherwise = deferType dmd_ty
517 -- NB: it's important to use deferType, and not just return topDmdType
518 -- Consider let { f x y = p + x } in f 1
519 -- The application isn't saturated, but we must nevertheless propagate
520 -- a lazy demand for p!
522 addVarDmd top_lvl fn_ty var dmd
524 ------ LOCAL NON-LET/REC BOUND THING
525 | otherwise -- Default case
529 (call_depth, res_dmd) = splitCallDmd dmd
533 %************************************************************************
537 %************************************************************************
540 splitCallDmd :: Demand -> (Int, Demand)
541 splitCallDmd (Call d) = case splitCallDmd d of
543 splitCallDmd d = (0, d)
545 vanillaCall :: Arity -> Demand
547 vanillaCall n = Call (vanillaCall (n-1))
549 deferType :: DmdType -> DmdType
550 deferType (DmdType fv _ _) = DmdType (mapVarEnv defer fv) [] TopRes
551 -- Notice that we throw away info about both arguments and results
552 -- For example, f = let ... in \x -> x
553 -- We don't want to get a stricness type V->T for f.
555 defer :: Demand -> Demand
558 defer (Seq k _ ds) = Seq k Defer ds
561 lazify :: Demand -> Demand
562 -- The 'Defer' demands are just Lazy at function boundaries
563 lazify (Seq k Defer ds) = Lazy
564 lazify (Seq k Now ds) = Seq k Now (map lazify ds)
565 lazify Bot = Abs -- Don't pass args that are consumed by bottom
570 betterStrictness :: StrictSig -> StrictSig -> Bool
571 betterStrictness (StrictSig t1) (StrictSig t2) = betterDmdType t1 t2
573 betterDmdType t1 t2 = (t1 `lubType` t2) == t2
575 betterDemand :: Demand -> Demand -> Bool
576 -- If d1 `better` d2, and d2 `better` d2, then d1==d2
577 betterDemand d1 d2 = (d1 `lub` d2) == d2
579 squashDmdEnv (StrictSig (DmdType fv ds res)) = StrictSig (DmdType emptyDmdEnv ds res)
583 %************************************************************************
585 \subsection{LUB and BOTH}
587 %************************************************************************
590 lub :: Demand -> Demand -> Demand
602 lub Abs (Seq k _ ds) = Seq k Defer ds -- Very important ('radicals' example)
607 lub Eval (Seq k Now ds) = Seq Keep Now ds
610 lub (Call d1) (Call d2) = Call (lub d1 d2)
612 lub (Seq k1 l1 ds1) (Seq k2 l2 ds2) = Seq (k1 `vee` k2) (l1 `or_defer` l2)
613 (zipWithEqual "lub" lub ds1 ds2)
615 -- The last clauses deal with the remaining cases for Call and Seq
616 lub d1@(Call _) d2@(Seq _ _ _) = pprPanic "lub" (ppr d1 $$ ppr d2)
617 lub d1 d2 = lub d2 d1
619 or_defer Now Now = Now
622 -------------------------
623 -- Consider (if x then y else []) with demand V
624 -- Then the first branch gives {y->V} and the second
625 -- *implicitly* has {y->A}. So we must put {y->(V `lub` A)}
626 -- in the result env.
627 lubType (DmdType fv1 ds1 r1) (DmdType fv2 ds2 r2)
628 = DmdType lub_fv2 (zipWith lub ds1 ds2) (r1 `lubRes` r2)
630 lub_fv = plusUFM_C lub fv1 fv2
631 lub_fv1 = modifyEnv (not (isBotRes r1)) (Abs `lub`) fv2 fv1 lub_fv
632 lub_fv2 = modifyEnv (not (isBotRes r2)) (Abs `lub`) fv1 fv2 lub_fv1
633 -- lub is the identity for Bot
635 -------------------------
638 lubRes RetCPR RetCPR = RetCPR
639 lubRes r1 r2 = TopRes
641 -----------------------------------
642 vee :: Keepity -> Keepity -> Keepity
646 -----------------------------------
647 both :: Demand -> Demand -> Demand
652 -- The experimental one
668 both Lazy (Seq k Now ds) = Seq Keep Now ds
671 -- Part of the Bot like Err experiment
672 -- both Eval Bot = Bot
673 both Eval (Seq k l ds) = Seq Keep Now ds
674 both Eval (Call d) = Call d
677 both (Seq k1 Defer ds1) (Seq k2 Defer ds2) = Seq (k1 `vee` k2) Defer
678 (zipWithEqual "both" both ds1 ds2)
679 both (Seq k1 l1 ds1) (Seq k2 l2 ds2) = Seq (k1 `vee` k2) Now
680 (zipWithEqual "both" both ds1' ds2')
682 ds1' = case l1 of { Now -> ds1; Defer -> map defer ds1 }
683 ds2' = case l2 of { Now -> ds2; Defer -> map defer ds2 }
685 both (Call d1) (Call d2) = Call (d1 `both` d2)
687 -- The last clauses deal with the remaining cases for Call and Seq
688 both d1@(Call _) d2@(Seq _ _ _) = pprPanic "both" (ppr d1 $$ ppr d2)
689 both d1 d2 = both d2 d1
691 -----------------------------------
692 bothRes :: DmdResult -> DmdResult -> DmdResult
693 -- Left-biased for CPR info
694 bothRes BotRes _ = BotRes
695 bothRes _ BotRes = BotRes
698 -----------------------------------
699 -- (t1 `bothType` t2) takes the argument/result info from t1,
700 -- using t2 just for its free-var info
701 bothType (DmdType fv1 ds1 r1) (DmdType fv2 ds2 r2)
702 = DmdType both_fv2 ds1 r1
704 both_fv = plusUFM_C both fv1 fv2
705 both_fv1 = modifyEnv (isBotRes r1) (`both` Bot) fv2 fv1 both_fv
706 both_fv2 = modifyEnv (isBotRes r2) (`both` Bot) fv1 fv2 both_fv1
707 -- both is the identity for Abs
711 modifyEnv :: Bool -- No-op if False
712 -> (Demand -> Demand) -- The zapper
713 -> DmdEnv -> DmdEnv -- Env1 and Env2
714 -> DmdEnv -> DmdEnv -- Transform this env
715 -- Zap anything in Env1 but not in Env2
716 -- Assume: dom(env) includes dom(Env1) and dom(Env2)
718 modifyEnv need_to_modify zapper env1 env2 env
719 | need_to_modify = foldr zap env (keysUFM (env1 `minusUFM` env2))
722 zap uniq env = addToUFM_Directly env uniq (zapper current_val)
724 current_val = expectJust "modifyEnv" (lookupUFM_Directly env uniq)
728 %************************************************************************
730 \subsection{Miscellaneous
732 %************************************************************************
736 get_changes binds = vcat (map get_changes_bind binds)
738 get_changes_bind (Rec pairs) = vcat (map get_changes_pr pairs)
739 get_changes_bind (NonRec id rhs) = get_changes_pr (id,rhs)
741 get_changes_pr (id,rhs)
742 | isImplicitId id = empty -- We don't look inside these
743 | otherwise = get_changes_var id $$ get_changes_expr rhs
746 | isId var = get_changes_str var $$ get_changes_dmd var
749 get_changes_expr (Type t) = empty
750 get_changes_expr (Var v) = empty
751 get_changes_expr (Lit l) = empty
752 get_changes_expr (Note n e) = get_changes_expr e
753 get_changes_expr (App e1 e2) = get_changes_expr e1 $$ get_changes_expr e2
754 get_changes_expr (Lam b e) = {- get_changes_var b $$ -} get_changes_expr e
755 get_changes_expr (Let b e) = get_changes_bind b $$ get_changes_expr e
756 get_changes_expr (Case e b a) = get_changes_expr e $$ get_changes_var b $$ vcat (map get_changes_alt a)
758 get_changes_alt (con,bs,rhs) = {- vcat (map get_changes_var bs) $$ -} get_changes_expr rhs
761 | new_better && old_better = empty
762 | new_better = message "BETTER"
763 | old_better = message "WORSE"
764 | otherwise = message "INCOMPARABLE"
766 message word = text word <+> text "strictness for" <+> ppr id <+> info
767 info = (text "Old" <+> ppr old) $$ (text "New" <+> ppr new)
768 new = squashDmdEnv (idNewStrictness id) -- Don't report diffs in the env
769 old = newStrictnessFromOld id
770 old_better = old `betterStrictness` new
771 new_better = new `betterStrictness` old
774 | isUnLiftedType (idType id) = empty -- Not useful
775 | new_better && old_better = empty
776 | new_better = message "BETTER"
777 | old_better = message "WORSE"
778 | otherwise = message "INCOMPARABLE"
780 message word = text word <+> text "demand for" <+> ppr id <+> info
781 info = (text "Old" <+> ppr old) $$ (text "New" <+> ppr new)
782 new = lazify (idNewDemandInfo id) -- Lazify to avoid spurious improvements
783 old = newDemand (idDemandInfo id)
784 new_better = new `betterDemand` old
785 old_better = old `betterDemand` new