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 DynFlags ( DynFlags )
17 import StaticFlags ( opt_MaxWorkerArgs )
18 import Demand -- All of it
21 import CoreUtils ( exprIsHNF, exprIsTrivial )
22 import CoreArity ( exprArity )
23 import DataCon ( dataConTyCon, dataConRepStrictness )
24 import TyCon ( isProductTyCon, isRecursiveTyCon )
25 import Id ( Id, idType, idInlineActivation,
26 isDataConWorkId, isGlobalId, idArity,
27 idStrictness, idStrictness_maybe,
28 setIdStrictness, idDemandInfo, idUnfolding,
34 import TysWiredIn ( unboxedPairDataCon )
35 import TysPrim ( realWorldStatePrimTy )
36 import UniqFM ( addToUFM_Directly, lookupUFM_Directly,
37 minusUFM, ufmToList, filterUFM )
38 import Type ( isUnLiftedType, coreEqType, splitTyConApp_maybe )
39 import Coercion ( coercionKind )
40 import Util ( mapAndUnzip, lengthIs, zipEqual )
41 import BasicTypes ( Arity, TopLevelFlag(..), isTopLevel, isNeverActive,
42 RecFlag(..), isRec, isMarkedStrict )
43 import Maybes ( orElse, expectJust )
50 * set a noinline pragma on bottoming Ids
52 * Consider f x = x+1 `fatbar` error (show x)
53 We'd like to unbox x, even if that means reboxing it in the error case.
56 %************************************************************************
58 \subsection{Top level stuff}
60 %************************************************************************
63 dmdAnalPgm :: DynFlags -> [CoreBind] -> IO [CoreBind]
66 let { binds_plus_dmds = do_prog binds } ;
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)
78 ( _, _, (_, rhs1)) = dmdAnalRhs TopLevel NonRecursive sigs (id, rhs)
79 (sigs2, _, (id2, rhs2)) = dmdAnalRhs TopLevel NonRecursive sigs (id, rhs1)
80 -- Do two passes to improve CPR information
81 -- See comments with ignore_cpr_info in mk_sig_ty
82 -- and with extendSigsWithLam
84 (sigs2, NonRec id2 rhs2)
86 dmdAnalTopBind sigs (Rec pairs)
88 (sigs', _, pairs') = dmdFix TopLevel sigs pairs
89 -- We get two iterations automatically
90 -- c.f. the NonRec case above
96 dmdAnalTopRhs :: CoreExpr -> (StrictSig, CoreExpr)
97 -- Analyse the RHS and return
98 -- a) appropriate strictness info
99 -- b) the unfolding (decorated with stricntess info)
103 call_dmd = vanillaCall (exprArity rhs)
104 (_, rhs1) = dmdAnal emptySigEnv call_dmd rhs
105 (rhs_ty, rhs2) = dmdAnal emptySigEnv call_dmd rhs1
106 sig = mkTopSigTy rhs rhs_ty
107 -- Do two passes; see notes with extendSigsWithLam
108 -- Otherwise we get bogus CPR info for constructors like
109 -- newtype T a = MkT a
110 -- The constructor looks like (\x::T a -> x), modulo the coerce
111 -- extendSigsWithLam will optimistically give x a CPR tag the
112 -- first time, which is wrong in the end.
115 %************************************************************************
117 \subsection{The analyser itself}
119 %************************************************************************
122 dmdAnal :: SigEnv -> Demand -> CoreExpr -> (DmdType, CoreExpr)
124 dmdAnal _ Abs e = (topDmdType, e)
127 | not (isStrictDmd dmd)
129 (res_ty, e') = dmdAnal sigs evalDmd e
131 (deferType res_ty, e')
132 -- It's important not to analyse e with a lazy demand because
133 -- a) When we encounter case s of (a,b) ->
134 -- we demand s with U(d1d2)... but if the overall demand is lazy
135 -- that is wrong, and we'd need to reduce the demand on s,
136 -- which is inconvenient
137 -- b) More important, consider
138 -- f (let x = R in x+x), where f is lazy
139 -- We still want to mark x as demanded, because it will be when we
140 -- enter the let. If we analyse f's arg with a Lazy demand, we'll
141 -- just mark x as Lazy
142 -- c) The application rule wouldn't be right either
143 -- Evaluating (f x) in a L demand does *not* cause
144 -- evaluation of f in a C(L) demand!
147 dmdAnal _ _ (Lit lit) = (topDmdType, Lit lit)
148 dmdAnal _ _ (Type ty) = (topDmdType, Type ty) -- Doesn't happen, in fact
150 dmdAnal sigs dmd (Var var)
151 = (dmdTransform sigs var dmd, Var var)
153 dmdAnal sigs dmd (Cast e co)
154 = (dmd_ty, Cast e' co)
156 (dmd_ty, e') = dmdAnal sigs dmd' e
157 to_co = snd (coercionKind co)
159 | Just (tc, _) <- splitTyConApp_maybe to_co
160 , isRecursiveTyCon tc = evalDmd
162 -- This coerce usually arises from a recursive
163 -- newtype, and we don't want to look inside them
164 -- for exactly the same reason that we don't look
165 -- inside recursive products -- we might not reach
166 -- a fixpoint. So revert to a vanilla Eval demand
168 dmdAnal sigs dmd (Note n e)
169 = (dmd_ty, Note n e')
171 (dmd_ty, e') = dmdAnal sigs dmd e
173 dmdAnal sigs dmd (App fun (Type ty))
174 = (fun_ty, App fun' (Type ty))
176 (fun_ty, fun') = dmdAnal sigs dmd fun
178 -- Lots of the other code is there to make this
179 -- beautiful, compositional, application rule :-)
180 dmdAnal sigs dmd (App fun arg) -- Non-type arguments
181 = let -- [Type arg handled above]
182 (fun_ty, fun') = dmdAnal sigs (Call dmd) fun
183 (arg_ty, arg') = dmdAnal sigs arg_dmd arg
184 (arg_dmd, res_ty) = splitDmdTy fun_ty
186 (res_ty `bothType` arg_ty, App fun' arg')
188 dmdAnal sigs dmd (Lam var body)
191 (body_ty, body') = dmdAnal sigs dmd body
193 (body_ty, Lam var body')
195 | Call body_dmd <- dmd -- A call demand: good!
197 sigs' = extendSigsWithLam sigs var
198 (body_ty, body') = dmdAnal sigs' body_dmd body
199 (lam_ty, var') = annotateLamIdBndr sigs body_ty var
201 (lam_ty, Lam var' body')
203 | otherwise -- Not enough demand on the lambda; but do the body
204 = let -- anyway to annotate it and gather free var info
205 (body_ty, body') = dmdAnal sigs evalDmd body
206 (lam_ty, var') = annotateLamIdBndr sigs body_ty var
208 (deferType lam_ty, Lam var' body')
210 dmdAnal sigs dmd (Case scrut case_bndr ty [alt@(DataAlt dc, _, _)])
211 | let tycon = dataConTyCon dc
212 , isProductTyCon tycon
213 , not (isRecursiveTyCon tycon)
215 sigs_alt = extendSigEnv NotTopLevel sigs case_bndr case_bndr_sig
216 (alt_ty, alt') = dmdAnalAlt sigs_alt dmd alt
217 (alt_ty1, case_bndr') = annotateBndr alt_ty case_bndr
218 (_, bndrs', _) = alt'
219 case_bndr_sig = cprSig
220 -- Inside the alternative, the case binder has the CPR property.
221 -- Meaning that a case on it will successfully cancel.
223 -- f True x = case x of y { I# x' -> if x' ==# 3 then y else I# 8 }
226 -- We want f to have the CPR property:
227 -- f b x = case fw b x of { r -> I# r }
228 -- fw True x = case x of y { I# x' -> if x' ==# 3 then x' else 8 }
231 -- Figure out whether the demand on the case binder is used, and use
232 -- that to set the scrut_dmd. This is utterly essential.
233 -- Consider f x = case x of y { (a,b) -> k y a }
234 -- If we just take scrut_demand = U(L,A), then we won't pass x to the
235 -- worker, so the worker will rebuild
236 -- x = (a, absent-error)
237 -- and that'll crash.
238 -- So at one stage I had:
239 -- dead_case_bndr = isAbsentDmd (idDemandInfo case_bndr')
240 -- keepity | dead_case_bndr = Drop
241 -- | otherwise = Keep
244 -- case x of y { (a,b) -> h y + a }
245 -- where h : U(LL) -> T
246 -- The above code would compute a Keep for x, since y is not Abs, which is silly
247 -- The insight is, of course, that a demand on y is a demand on the
248 -- scrutinee, so we need to `both` it with the scrut demand
250 alt_dmd = Eval (Prod [idDemandInfo b | b <- bndrs', isId b])
251 scrut_dmd = alt_dmd `both`
252 idDemandInfo case_bndr'
254 (scrut_ty, scrut') = dmdAnal sigs scrut_dmd scrut
256 (alt_ty1 `bothType` scrut_ty, Case scrut' case_bndr' ty [alt'])
258 dmdAnal sigs dmd (Case scrut case_bndr ty alts)
260 (alt_tys, alts') = mapAndUnzip (dmdAnalAlt sigs dmd) alts
261 (scrut_ty, scrut') = dmdAnal sigs evalDmd scrut
262 (alt_ty, case_bndr') = annotateBndr (foldr1 lubType alt_tys) case_bndr
264 -- pprTrace "dmdAnal:Case" (ppr alts $$ ppr alt_tys)
265 (alt_ty `bothType` scrut_ty, Case scrut' case_bndr' ty alts')
267 dmdAnal sigs dmd (Let (NonRec id rhs) body)
269 (sigs', lazy_fv, (id1, rhs')) = dmdAnalRhs NotTopLevel NonRecursive sigs (id, rhs)
270 (body_ty, body') = dmdAnal sigs' dmd body
271 (body_ty1, id2) = annotateBndr body_ty id1
272 body_ty2 = addLazyFVs body_ty1 lazy_fv
274 -- If the actual demand is better than the vanilla call
275 -- demand, you might think that we might do better to re-analyse
276 -- the RHS with the stronger demand.
277 -- But (a) That seldom happens, because it means that *every* path in
278 -- the body of the let has to use that stronger demand
279 -- (b) It often happens temporarily in when fixpointing, because
280 -- the recursive function at first seems to place a massive demand.
281 -- But we don't want to go to extra work when the function will
282 -- probably iterate to something less demanding.
283 -- In practice, all the times the actual demand on id2 is more than
284 -- the vanilla call demand seem to be due to (b). So we don't
285 -- bother to re-analyse the RHS.
286 (body_ty2, Let (NonRec id2 rhs') body')
288 dmdAnal sigs dmd (Let (Rec pairs) body)
290 bndrs = map fst pairs
291 (sigs', lazy_fv, pairs') = dmdFix NotTopLevel sigs pairs
292 (body_ty, body') = dmdAnal sigs' dmd body
293 body_ty1 = addLazyFVs body_ty lazy_fv
295 sigs' `seq` body_ty `seq`
297 (body_ty2, _) = annotateBndrs body_ty1 bndrs
298 -- Don't bother to add demand info to recursive
299 -- binders as annotateBndr does;
300 -- being recursive, we can't treat them strictly.
301 -- But we do need to remove the binders from the result demand env
303 (body_ty2, Let (Rec pairs') body')
306 dmdAnalAlt :: SigEnv -> Demand -> Alt Var -> (DmdType, Alt Var)
307 dmdAnalAlt sigs dmd (con,bndrs,rhs)
309 (rhs_ty, rhs') = dmdAnal sigs dmd rhs
310 rhs_ty' = addDataConPatDmds con bndrs rhs_ty
311 (alt_ty, bndrs') = annotateBndrs rhs_ty' bndrs
312 final_alt_ty | io_hack_reqd = alt_ty `lubType` topDmdType
315 -- There's a hack here for I/O operations. Consider
316 -- case foo x s of { (# s, r #) -> y }
317 -- Is this strict in 'y'. Normally yes, but what if 'foo' is an I/O
318 -- operation that simply terminates the program (not in an erroneous way)?
319 -- In that case we should not evaluate y before the call to 'foo'.
320 -- Hackish solution: spot the IO-like situation and add a virtual branch,
324 -- other -> return ()
325 -- So the 'y' isn't necessarily going to be evaluated
327 -- A more complete example where this shows up is:
328 -- do { let len = <expensive> ;
329 -- ; when (...) (exitWith ExitSuccess)
332 io_hack_reqd = con == DataAlt unboxedPairDataCon &&
333 idType (head bndrs) `coreEqType` realWorldStatePrimTy
335 (final_alt_ty, (con, bndrs', rhs'))
337 addDataConPatDmds :: AltCon -> [Var] -> DmdType -> DmdType
338 -- See Note [Add demands for strict constructors]
339 addDataConPatDmds DEFAULT _ dmd_ty = dmd_ty
340 addDataConPatDmds (LitAlt _) _ dmd_ty = dmd_ty
341 addDataConPatDmds (DataAlt con) bndrs dmd_ty
342 = foldr add dmd_ty str_bndrs
344 add bndr dmd_ty = addVarDmd dmd_ty bndr seqDmd
345 str_bndrs = [ b | (b,s) <- zipEqual "addDataConPatBndrs"
347 (dataConRepStrictness con)
351 Note [Add demands for strict constructors]
352 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
353 Consider this program (due to Roman):
357 foo :: X Int -> Int -> Int
360 go i | i < n = a + go (i+1)
363 We want the worker for 'foo' too look like this:
365 $wfoo :: Int# -> Int# -> Int#
367 with the first argument unboxed, so that it is not eval'd each time
368 around the loop (which would otherwise happen, since 'foo' is not
369 strict in 'a'. It is sound for the wrapper to pass an unboxed arg
370 because X is strict, so its argument must be evaluated. And if we
371 *don't* pass an unboxed argument, we can't even repair it by adding a
374 foo (X a) n = a `seq` go 0
376 because the seq is discarded (very early) since X is strict!
378 There is the usual danger of reboxing, which as usual we ignore. But
379 if X is monomorphic, and has an UNPACK pragma, then this optimisation
380 is even more important. We don't want the wrapper to rebox an unboxed
381 argument, and pass an Int to $wfoo!
383 %************************************************************************
385 \subsection{Bindings}
387 %************************************************************************
390 dmdFix :: TopLevelFlag
391 -> SigEnv -- Does not include bindings for this binding
394 [(Id,CoreExpr)]) -- Binders annotated with stricness info
396 dmdFix top_lvl sigs orig_pairs
397 = loop 1 initial_sigs orig_pairs
399 bndrs = map fst orig_pairs
400 initial_sigs = extendSigEnvList sigs [(id, (initialSig id, top_lvl)) | id <- bndrs]
403 -> SigEnv -- Already contains the current sigs
405 -> (SigEnv, DmdEnv, [(Id,CoreExpr)])
408 = (sigs', lazy_fv, pairs')
409 -- Note: use pairs', not pairs. pairs' is the result of
410 -- processing the RHSs with sigs (= sigs'), whereas pairs
411 -- is the result of processing the RHSs with the *previous*
412 -- iteration of sigs.
414 | n >= 10 = pprTrace "dmdFix loop" (ppr n <+> (vcat
415 [ text "Sigs:" <+> ppr [(id,lookup sigs id, lookup sigs' id) | (id,_) <- pairs],
416 text "env:" <+> ppr (ufmToList sigs),
417 text "binds:" <+> pprCoreBinding (Rec pairs)]))
418 (emptySigEnv, lazy_fv, orig_pairs) -- Safe output
419 -- The lazy_fv part is really important! orig_pairs has no strictness
420 -- info, including nothing about free vars. But if we have
421 -- letrec f = ....y..... in ...f...
422 -- where 'y' is free in f, we must record that y is mentioned,
423 -- otherwise y will get recorded as absent altogether
425 | otherwise = loop (n+1) sigs' pairs'
427 found_fixpoint = all (same_sig sigs sigs') bndrs
428 -- Use the new signature to do the next pair
429 -- The occurrence analyser has arranged them in a good order
430 -- so this can significantly reduce the number of iterations needed
431 ((sigs',lazy_fv), pairs') = mapAccumL (my_downRhs top_lvl) (sigs, emptyDmdEnv) pairs
433 my_downRhs top_lvl (sigs,lazy_fv) (id,rhs)
434 = -- pprTrace "downRhs {" (ppr id <+> (ppr old_sig))
436 -- pprTrace "downRhsEnd" (ppr id <+> ppr new_sig <+> char '}' )
437 ((sigs', lazy_fv'), pair')
440 (sigs', lazy_fv1, pair') = dmdAnalRhs top_lvl Recursive sigs (id,rhs)
441 lazy_fv' = plusVarEnv_C both lazy_fv lazy_fv1
442 -- old_sig = lookup sigs id
443 -- new_sig = lookup sigs' id
445 same_sig sigs sigs' var = lookup sigs var == lookup sigs' var
446 lookup sigs var = case lookupVarEnv sigs var of
448 Nothing -> pprPanic "dmdFix" (ppr var)
450 -- Get an initial strictness signature from the Id
451 -- itself. That way we make use of earlier iterations
452 -- of the fixpoint algorithm. (Cunning plan.)
453 -- Note that the cunning plan extends to the DmdEnv too,
454 -- since it is part of the strictness signature
455 initialSig :: Id -> StrictSig
456 initialSig id = idStrictness_maybe id `orElse` botSig
458 dmdAnalRhs :: TopLevelFlag -> RecFlag
459 -> SigEnv -> (Id, CoreExpr)
460 -> (SigEnv, DmdEnv, (Id, CoreExpr))
461 -- Process the RHS of the binding, add the strictness signature
462 -- to the Id, and augment the environment with the signature as well.
464 dmdAnalRhs top_lvl rec_flag sigs (id, rhs)
465 = (sigs', lazy_fv, (id', rhs'))
467 arity = idArity id -- The idArity should be up to date
468 -- The simplifier was run just beforehand
469 (rhs_dmd_ty, rhs') = dmdAnal sigs (vanillaCall arity) rhs
470 (lazy_fv, sig_ty) = WARN( arity /= dmdTypeDepth rhs_dmd_ty && not (exprIsTrivial rhs), ppr id )
471 -- The RHS can be eta-reduced to just a variable,
472 -- in which case we should not complain.
473 mkSigTy top_lvl rec_flag id rhs rhs_dmd_ty
474 id' = id `setIdStrictness` sig_ty
475 sigs' = extendSigEnv top_lvl sigs id sig_ty
478 %************************************************************************
480 \subsection{Strictness signatures and types}
482 %************************************************************************
485 mkTopSigTy :: CoreExpr -> DmdType -> StrictSig
486 -- Take a DmdType and turn it into a StrictSig
487 -- NB: not used for never-inline things; hence False
488 mkTopSigTy rhs dmd_ty = snd (mk_sig_ty False False rhs dmd_ty)
490 mkSigTy :: TopLevelFlag -> RecFlag -> Id -> CoreExpr -> DmdType -> (DmdEnv, StrictSig)
491 mkSigTy top_lvl rec_flag id rhs dmd_ty
492 = mk_sig_ty never_inline thunk_cpr_ok rhs dmd_ty
494 never_inline = isNeverActive (idInlineActivation id)
495 maybe_id_dmd = idDemandInfo_maybe id
496 -- Is Nothing the first time round
499 | isTopLevel top_lvl = False -- Top level things don't get
500 -- their demandInfo set at all
501 | isRec rec_flag = False -- Ditto recursive things
502 | Just dmd <- maybe_id_dmd = isStrictDmd dmd
503 | otherwise = True -- Optimistic, first time round
507 The thunk_cpr_ok stuff [CPR-AND-STRICTNESS]
508 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
509 If the rhs is a thunk, we usually forget the CPR info, because
510 it is presumably shared (else it would have been inlined, and
511 so we'd lose sharing if w/w'd it into a function). E.g.
513 let r = case expensive of
517 If we marked r as having the CPR property, then we'd w/w into
519 let $wr = \() -> case expensive of
525 But now r is a thunk, which won't be inlined, so we are no further ahead.
528 f x = let r = case expensive of (a,b) -> (b,a)
529 in if foo r then r else (x,x)
531 Does f have the CPR property? Well, no.
533 However, if the strictness analyser has figured out (in a previous
534 iteration) that it's strict, then we DON'T need to forget the CPR info.
535 Instead we can retain the CPR info and do the thunk-splitting transform
536 (see WorkWrap.splitThunk).
538 This made a big difference to PrelBase.modInt, which had something like
539 modInt = \ x -> let r = ... -> I# v in
540 ...body strict in r...
541 r's RHS isn't a value yet; but modInt returns r in various branches, so
542 if r doesn't have the CPR property then neither does modInt
543 Another case I found in practice (in Complex.magnitude), looks like this:
544 let k = if ... then I# a else I# b
545 in ... body strict in k ....
546 (For this example, it doesn't matter whether k is returned as part of
547 the overall result; but it does matter that k's RHS has the CPR property.)
548 Left to itself, the simplifier will make a join point thus:
549 let $j k = ...body strict in k...
550 if ... then $j (I# a) else $j (I# b)
551 With thunk-splitting, we get instead
552 let $j x = let k = I#x in ...body strict in k...
553 in if ... then $j a else $j b
554 This is much better; there's a good chance the I# won't get allocated.
556 The difficulty with this is that we need the strictness type to
557 look at the body... but we now need the body to calculate the demand
558 on the variable, so we can decide whether its strictness type should
559 have a CPR in it or not. Simple solution:
560 a) use strictness info from the previous iteration
561 b) make sure we do at least 2 iterations, by doing a second
562 round for top-level non-recs. Top level recs will get at
563 least 2 iterations except for totally-bottom functions
564 which aren't very interesting anyway.
566 NB: strictly_demanded is never true of a top-level Id, or of a recursive Id.
568 The Nothing case in thunk_cpr_ok [CPR-AND-STRICTNESS]
569 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
570 Demand info now has a 'Nothing' state, just like strictness info.
571 The analysis works from 'dangerous' towards a 'safe' state; so we
572 start with botSig for 'Nothing' strictness infos, and we start with
573 "yes, it's demanded" for 'Nothing' in the demand info. The
574 fixpoint iteration will sort it all out.
576 We can't start with 'not-demanded' because then consider
580 if ... then t else I# y else f x'
582 In the first iteration we'd have no demand info for x, so assume
583 not-demanded; then we'd get TopRes for f's CPR info. Next iteration
584 we'd see that t was demanded, and so give it the CPR property, but by
585 now f has TopRes, so it will stay TopRes. Instead, with the Nothing
586 setting the first time round, we say 'yes t is demanded' the first
589 However, this does mean that for non-recursive bindings we must
590 iterate twice to be sure of not getting over-optimistic CPR info,
591 in the case where t turns out to be not-demanded. This is handled
595 Note [NOINLINE and strictness]
596 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
597 The strictness analyser used to have a HACK which ensured that NOINLNE
598 things were not strictness-analysed. The reason was unsafePerformIO.
599 Left to itself, the strictness analyser would discover this strictness
601 unsafePerformIO: C(U(AV))
602 But then consider this sub-expression
603 unsafePerformIO (\s -> let r = f x in
604 case writeIORef v r s of (# s1, _ #) ->
606 The strictness analyser will now find that r is sure to be eval'd,
607 and may then hoist it out. This makes tests/lib/should_run/memo002
610 Solving this by making all NOINLINE things have no strictness info is overkill.
611 In particular, it's overkill for runST, which is perfectly respectable.
613 f x = runST (return x)
614 This should be strict in x.
616 So the new plan is to define unsafePerformIO using the 'lazy' combinator:
618 unsafePerformIO (IO m) = lazy (case m realWorld# of (# _, r #) -> r)
620 Remember, 'lazy' is a wired-in identity-function Id, of type a->a, which is
621 magically NON-STRICT, and is inlined after strictness analysis. So
622 unsafePerformIO will look non-strict, and that's what we want.
624 Now we don't need the hack in the strictness analyser. HOWEVER, this
625 decision does mean that even a NOINLINE function is not entirely
626 opaque: some aspect of its implementation leaks out, notably its
627 strictness. For example, if you have a function implemented by an
628 error stub, but which has RULES, you may want it not to be eliminated
633 mk_sig_ty :: Bool -> Bool -> CoreExpr
634 -> DmdType -> (DmdEnv, StrictSig)
635 mk_sig_ty _never_inline thunk_cpr_ok rhs (DmdType fv dmds res)
636 = (lazy_fv, mkStrictSig dmd_ty)
637 -- Re unused never_inline, see Note [NOINLINE and strictness]
639 dmd_ty = DmdType strict_fv final_dmds res'
641 lazy_fv = filterUFM (not . isStrictDmd) fv
642 strict_fv = filterUFM isStrictDmd fv
643 -- We put the strict FVs in the DmdType of the Id, so
644 -- that at its call sites we unleash demands on its strict fvs.
645 -- An example is 'roll' in imaginary/wheel-sieve2
646 -- Something like this:
648 -- go y = if ... then roll (x-1) else x+1
651 -- We want to see that roll is strict in x, which is because
652 -- go is called. So we put the DmdEnv for x in go's DmdType.
655 -- f :: Int -> Int -> Int
656 -- f x y = let t = x+1
657 -- h z = if z==0 then t else
658 -- if z==1 then x+1 else
662 -- Calling h does indeed evaluate x, but we can only see
663 -- that if we unleash a demand on x at the call site for t.
665 -- Incidentally, here's a place where lambda-lifting h would
666 -- lose the cigar --- we couldn't see the joint strictness in t/x
669 -- We don't want to put *all* the fv's from the RHS into the
670 -- DmdType, because that makes fixpointing very slow --- the
671 -- DmdType gets full of lazy demands that are slow to converge.
673 final_dmds = setUnpackStrategy dmds
674 -- Set the unpacking strategy
677 RetCPR | ignore_cpr_info -> TopRes
679 ignore_cpr_info = not (exprIsHNF rhs || thunk_cpr_ok)
682 The unpack strategy determines whether we'll *really* unpack the argument,
683 or whether we'll just remember its strictness. If unpacking would give
684 rise to a *lot* of worker args, we may decide not to unpack after all.
687 setUnpackStrategy :: [Demand] -> [Demand]
689 = snd (go (opt_MaxWorkerArgs - nonAbsentArgs ds) ds)
691 go :: Int -- Max number of args available for sub-components of [Demand]
693 -> (Int, [Demand]) -- Args remaining after subcomponents of [Demand] are unpacked
695 go n (Eval (Prod cs) : ds)
696 | n' >= 0 = Eval (Prod cs') `cons` go n'' ds
697 | otherwise = Box (Eval (Prod cs)) `cons` go n ds
700 n' = n + 1 - non_abs_args
701 -- Add one to the budget 'cos we drop the top-level arg
702 non_abs_args = nonAbsentArgs cs
703 -- Delete # of non-absent args to which we'll now be committed
705 go n (d:ds) = d `cons` go n ds
708 cons d (n,ds) = (n, d:ds)
710 nonAbsentArgs :: [Demand] -> Int
712 nonAbsentArgs (Abs : ds) = nonAbsentArgs ds
713 nonAbsentArgs (_ : ds) = 1 + nonAbsentArgs ds
717 %************************************************************************
719 \subsection{Strictness signatures and types}
721 %************************************************************************
724 unitVarDmd :: Var -> Demand -> DmdType
725 unitVarDmd var dmd = DmdType (unitVarEnv var dmd) [] TopRes
727 addVarDmd :: DmdType -> Var -> Demand -> DmdType
728 addVarDmd (DmdType fv ds res) var dmd
729 = DmdType (extendVarEnv_C both fv var dmd) ds res
731 addLazyFVs :: DmdType -> DmdEnv -> DmdType
732 addLazyFVs (DmdType fv ds res) lazy_fvs
733 = DmdType both_fv1 ds res
735 both_fv = plusVarEnv_C both fv lazy_fvs
736 both_fv1 = modifyEnv (isBotRes res) (`both` Bot) lazy_fvs fv both_fv
737 -- This modifyEnv is vital. Consider
738 -- let f = \x -> (x,y)
740 -- Here, y is treated as a lazy-fv of f, but we must `both` that L
741 -- demand with the bottom coming up from 'error'
743 -- I got a loop in the fixpointer without this, due to an interaction
744 -- with the lazy_fv filtering in mkSigTy. Roughly, it was
746 -- = letrec g y = x `fatbar`
747 -- letrec h z = z + ...g...
750 -- In the initial iteration for f, f=Bot
751 -- Suppose h is found to be strict in z, but the occurrence of g in its RHS
752 -- is lazy. Now consider the fixpoint iteration for g, esp the demands it
753 -- places on its free variables. Suppose it places none. Then the
754 -- x `fatbar` ...call to h...
755 -- will give a x->V demand for x. That turns into a L demand for x,
756 -- which floats out of the defn for h. Without the modifyEnv, that
757 -- L demand doesn't get both'd with the Bot coming up from the inner
758 -- call to f. So we just get an L demand for x for g.
760 -- A better way to say this is that the lazy-fv filtering should give the
761 -- same answer as putting the lazy fv demands in the function's type.
763 annotateBndr :: DmdType -> Var -> (DmdType, Var)
764 -- The returned env has the var deleted
765 -- The returned var is annotated with demand info
766 -- No effect on the argument demands
767 annotateBndr dmd_ty@(DmdType fv ds res) var
768 | isTyCoVar var = (dmd_ty, var)
769 | otherwise = (DmdType fv' ds res, setIdDemandInfo var dmd)
771 (fv', dmd) = removeFV fv var res
773 annotateBndrs :: DmdType -> [Var] -> (DmdType, [Var])
774 annotateBndrs = mapAccumR annotateBndr
776 annotateLamIdBndr :: SigEnv
777 -> DmdType -- Demand type of body
778 -> Id -- Lambda binder
779 -> (DmdType, -- Demand type of lambda
780 Id) -- and binder annotated with demand
782 annotateLamIdBndr sigs (DmdType fv ds res) id
783 -- For lambdas we add the demand to the argument demands
784 -- Only called for Ids
786 (final_ty, setIdDemandInfo id hacked_dmd)
788 -- Watch out! See note [Lambda-bound unfoldings]
789 final_ty = case maybeUnfoldingTemplate (idUnfolding id) of
791 Just unf -> main_ty `bothType` unf_ty
793 (unf_ty, _) = dmdAnal sigs dmd unf
795 main_ty = DmdType fv' (hacked_dmd:ds) res
797 (fv', dmd) = removeFV fv id res
798 hacked_dmd = argDemand dmd
799 -- This call to argDemand is vital, because otherwise we label
800 -- a lambda binder with demand 'B'. But in terms of calling
801 -- conventions that's Abs, because we don't pass it. But
802 -- when we do a w/w split we get
803 -- fw x = (\x y:B -> ...) x (error "oops")
804 -- And then the simplifier things the 'B' is a strict demand
805 -- and evaluates the (error "oops"). Sigh
807 removeFV :: DmdEnv -> Var -> DmdResult -> (DmdEnv, Demand)
808 removeFV fv id res = (fv', zapUnlifted id dmd)
810 fv' = fv `delVarEnv` id
811 dmd = lookupVarEnv fv id `orElse` deflt
812 deflt | isBotRes res = Bot
815 zapUnlifted :: Id -> Demand -> Demand
816 -- For unlifted-type variables, we are only
817 -- interested in Bot/Abs/Box Abs
818 zapUnlifted _ Bot = Bot
819 zapUnlifted _ Abs = Abs
820 zapUnlifted id dmd | isUnLiftedType (idType id) = lazyDmd
824 Note [Lamba-bound unfoldings]
825 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
826 We allow a lambda-bound variable to carry an unfolding, a facility that is used
827 exclusively for join points; see Note [Case binders and join points]. If so,
828 we must be careful to demand-analyse the RHS of the unfolding! Example
829 \x. \y{=Just x}. <body>
830 Then if <body> uses 'y', then transitively it uses 'x', and we must not
831 forget that fact, otherwise we might make 'x' absent when it isn't.
834 %************************************************************************
836 \subsection{Strictness signatures}
838 %************************************************************************
841 type SigEnv = VarEnv (StrictSig, TopLevelFlag)
842 -- We use the SigEnv to tell us whether to
843 -- record info about a variable in the DmdEnv
844 -- We do so if it's a LocalId, but not top-level
846 -- The DmdEnv gives the demand on the free vars of the function
847 -- when it is given enough args to satisfy the strictness signature
849 emptySigEnv :: SigEnv
850 emptySigEnv = emptyVarEnv
852 extendSigEnv :: TopLevelFlag -> SigEnv -> Id -> StrictSig -> SigEnv
853 extendSigEnv top_lvl env var sig = extendVarEnv env var (sig, top_lvl)
855 extendSigEnvList :: SigEnv -> [(Id, (StrictSig, TopLevelFlag))] -> SigEnv
856 extendSigEnvList = extendVarEnvList
858 extendSigsWithLam :: SigEnv -> Id -> SigEnv
859 -- Extend the SigEnv when we meet a lambda binder
860 -- If the binder is marked demanded with a product demand, then give it a CPR
861 -- signature, because in the likely event that this is a lambda on a fn defn
862 -- [we only use this when the lambda is being consumed with a call demand],
863 -- it'll be w/w'd and so it will be CPR-ish. E.g.
864 -- f = \x::(Int,Int). if ...strict in x... then
868 -- We want f to have the CPR property because x does, by the time f has been w/w'd
870 -- Also note that we only want to do this for something that
871 -- definitely has product type, else we may get over-optimistic
872 -- CPR results (e.g. from \x -> x!).
874 extendSigsWithLam sigs id
875 = case idDemandInfo_maybe id of
876 Nothing -> extendVarEnv sigs id (cprSig, NotTopLevel)
877 -- Optimistic in the Nothing case;
878 -- See notes [CPR-AND-STRICTNESS]
879 Just (Eval (Prod _)) -> extendVarEnv sigs id (cprSig, NotTopLevel)
883 dmdTransform :: SigEnv -- The strictness environment
884 -> Id -- The function
885 -> Demand -- The demand on the function
886 -> DmdType -- The demand type of the function in this context
887 -- Returned DmdEnv includes the demand on
888 -- this function plus demand on its free variables
890 dmdTransform sigs var dmd
892 ------ DATA CONSTRUCTOR
893 | isDataConWorkId var -- Data constructor
895 StrictSig dmd_ty = idStrictness var -- It must have a strictness sig
896 DmdType _ _ con_res = dmd_ty
899 if arity == call_depth then -- Saturated, so unleash the demand
901 -- Important! If we Keep the constructor application, then
902 -- we need the demands the constructor places (always lazy)
903 -- If not, we don't need to. For example:
904 -- f p@(x,y) = (p,y) -- S(AL)
906 -- It's vital that we don't calculate Absent for a!
907 dmd_ds = case res_dmd of
908 Box (Eval ds) -> mapDmds box ds
912 -- ds can be empty, when we are just seq'ing the thing
913 -- If so we must make up a suitable bunch of demands
914 arg_ds = case dmd_ds of
915 Poly d -> replicate arity d
916 Prod ds -> ASSERT( ds `lengthIs` arity ) ds
919 mkDmdType emptyDmdEnv arg_ds con_res
920 -- Must remember whether it's a product, hence con_res, not TopRes
924 ------ IMPORTED FUNCTION
925 | isGlobalId var, -- Imported function
926 let StrictSig dmd_ty = idStrictness var
927 = if dmdTypeDepth dmd_ty <= call_depth then -- Saturated, so unleash the demand
932 ------ LOCAL LET/REC BOUND THING
933 | Just (StrictSig dmd_ty, top_lvl) <- lookupVarEnv sigs var
935 fn_ty | dmdTypeDepth dmd_ty <= call_depth = dmd_ty
936 | otherwise = deferType dmd_ty
937 -- NB: it's important to use deferType, and not just return topDmdType
938 -- Consider let { f x y = p + x } in f 1
939 -- The application isn't saturated, but we must nevertheless propagate
940 -- a lazy demand for p!
942 if isTopLevel top_lvl then fn_ty -- Don't record top level things
943 else addVarDmd fn_ty var dmd
945 ------ LOCAL NON-LET/REC BOUND THING
946 | otherwise -- Default case
950 (call_depth, res_dmd) = splitCallDmd dmd
954 %************************************************************************
958 %************************************************************************
961 splitDmdTy :: DmdType -> (Demand, DmdType)
962 -- Split off one function argument
963 -- We already have a suitable demand on all
964 -- free vars, so no need to add more!
965 splitDmdTy (DmdType fv (dmd:dmds) res_ty) = (dmd, DmdType fv dmds res_ty)
966 splitDmdTy ty@(DmdType _ [] res_ty) = (resTypeArgDmd res_ty, ty)
968 splitCallDmd :: Demand -> (Int, Demand)
969 splitCallDmd (Call d) = case splitCallDmd d of
971 splitCallDmd d = (0, d)
973 vanillaCall :: Arity -> Demand
974 vanillaCall 0 = evalDmd
975 vanillaCall n = Call (vanillaCall (n-1))
977 deferType :: DmdType -> DmdType
978 deferType (DmdType fv _ _) = DmdType (deferEnv fv) [] TopRes
979 -- Notice that we throw away info about both arguments and results
980 -- For example, f = let ... in \x -> x
981 -- We don't want to get a stricness type V->T for f.
983 deferEnv :: DmdEnv -> DmdEnv
984 deferEnv fv = mapVarEnv defer fv
988 argDemand :: Demand -> Demand
989 -- The 'Defer' demands are just Lazy at function boundaries
990 -- Ugly! Ask John how to improve it.
991 argDemand Top = lazyDmd
992 argDemand (Defer _) = lazyDmd
993 argDemand (Eval ds) = Eval (mapDmds argDemand ds)
994 argDemand (Box Bot) = evalDmd
995 argDemand (Box d) = box (argDemand d)
996 argDemand Bot = Abs -- Don't pass args that are consumed (only) by bottom
1001 -------------------------
1002 lubType :: DmdType -> DmdType -> DmdType
1003 -- Consider (if x then y else []) with demand V
1004 -- Then the first branch gives {y->V} and the second
1005 -- *implicitly* has {y->A}. So we must put {y->(V `lub` A)}
1006 -- in the result env.
1007 lubType (DmdType fv1 ds1 r1) (DmdType fv2 ds2 r2)
1008 = DmdType lub_fv2 (lub_ds ds1 ds2) (r1 `lubRes` r2)
1010 lub_fv = plusVarEnv_C lub fv1 fv2
1011 lub_fv1 = modifyEnv (not (isBotRes r1)) absLub fv2 fv1 lub_fv
1012 lub_fv2 = modifyEnv (not (isBotRes r2)) absLub fv1 fv2 lub_fv1
1013 -- lub is the identity for Bot
1015 -- Extend the shorter argument list to match the longer
1016 lub_ds (d1:ds1) (d2:ds2) = lub d1 d2 : lub_ds ds1 ds2
1018 lub_ds ds1 [] = map (`lub` resTypeArgDmd r2) ds1
1019 lub_ds [] ds2 = map (resTypeArgDmd r1 `lub`) ds2
1021 -----------------------------------
1022 bothType :: DmdType -> DmdType -> DmdType
1023 -- (t1 `bothType` t2) takes the argument/result info from t1,
1024 -- using t2 just for its free-var info
1025 -- NB: Don't forget about r2! It might be BotRes, which is
1026 -- a bottom demand on all the in-scope variables.
1027 -- Peter: can this be done more neatly?
1028 bothType (DmdType fv1 ds1 r1) (DmdType fv2 _ r2)
1029 = DmdType both_fv2 ds1 (r1 `bothRes` r2)
1031 both_fv = plusVarEnv_C both fv1 fv2
1032 both_fv1 = modifyEnv (isBotRes r1) (`both` Bot) fv2 fv1 both_fv
1033 both_fv2 = modifyEnv (isBotRes r2) (`both` Bot) fv1 fv2 both_fv1
1034 -- both is the identity for Abs
1039 lubRes :: DmdResult -> DmdResult -> DmdResult
1042 lubRes RetCPR RetCPR = RetCPR
1045 bothRes :: DmdResult -> DmdResult -> DmdResult
1046 -- If either diverges, the whole thing does
1047 -- Otherwise take CPR info from the first
1048 bothRes _ BotRes = BotRes
1053 modifyEnv :: Bool -- No-op if False
1054 -> (Demand -> Demand) -- The zapper
1055 -> DmdEnv -> DmdEnv -- Env1 and Env2
1056 -> DmdEnv -> DmdEnv -- Transform this env
1057 -- Zap anything in Env1 but not in Env2
1058 -- Assume: dom(env) includes dom(Env1) and dom(Env2)
1060 modifyEnv need_to_modify zapper env1 env2 env
1061 | need_to_modify = foldr zap env (varEnvKeys (env1 `minusUFM` env2))
1064 zap uniq env = addToUFM_Directly env uniq (zapper current_val)
1066 current_val = expectJust "modifyEnv" (lookupUFM_Directly env uniq)
1070 %************************************************************************
1072 \subsection{LUB and BOTH}
1074 %************************************************************************
1077 lub :: Demand -> Demand -> Demand
1080 lub Abs d2 = absLub d2
1082 lub (Defer ds1) d2 = defer (Eval ds1 `lub` d2)
1084 lub (Call d1) (Call d2) = Call (d1 `lub` d2)
1085 lub d1@(Call _) (Box d2) = d1 `lub` d2 -- Just strip the box
1086 lub (Call _) d2@(Eval _) = d2 -- Presumably seq or vanilla eval
1087 lub d1@(Call _) d2 = d2 `lub` d1 -- Bot, Abs, Top
1089 -- For the Eval case, we use these approximation rules
1090 -- Box Bot <= Eval (Box Bot ...)
1091 -- Box Top <= Defer (Box Bot ...)
1092 -- Box (Eval ds) <= Eval (map Box ds)
1093 lub (Eval ds1) (Eval ds2) = Eval (ds1 `lubs` ds2)
1094 lub (Eval ds1) (Box Bot) = Eval (mapDmds (`lub` Box Bot) ds1)
1095 lub (Eval ds1) (Box (Eval ds2)) = Eval (ds1 `lubs` mapDmds box ds2)
1096 lub (Eval ds1) (Box Abs) = deferEval (mapDmds (`lub` Box Bot) ds1)
1097 lub d1@(Eval _) d2 = d2 `lub` d1 -- Bot,Abs,Top,Call,Defer
1099 lub (Box d1) (Box d2) = box (d1 `lub` d2)
1100 lub d1@(Box _) d2 = d2 `lub` d1
1102 lubs :: Demands -> Demands -> Demands
1103 lubs ds1 ds2 = zipWithDmds lub ds1 ds2
1105 ---------------------
1106 box :: Demand -> Demand
1107 -- box is the smart constructor for Box
1108 -- It computes <B,bot> & d
1109 -- INVARIANT: (Box d) => d = Bot, Abs, Eval
1110 -- Seems to be no point in allowing (Box (Call d))
1111 box (Call d) = Call d -- The odd man out. Why?
1113 box (Defer _) = lazyDmd
1114 box Top = lazyDmd -- Box Abs and Box Top
1115 box Abs = lazyDmd -- are the same <B,L>
1116 box d = Box d -- Bot, Eval
1119 defer :: Demand -> Demand
1121 -- defer is the smart constructor for Defer
1122 -- The idea is that (Defer ds) = <U(ds), L>
1124 -- It specifies what happens at a lazy function argument
1125 -- or a lambda; the L* operator
1126 -- Set the strictness part to L, but leave
1127 -- the boxity side unaffected
1128 -- It also ensures that Defer (Eval [LLLL]) = L
1133 defer (Call _) = lazyDmd -- Approximation here?
1134 defer (Box _) = lazyDmd
1135 defer (Defer ds) = Defer ds
1136 defer (Eval ds) = deferEval ds
1138 deferEval :: Demands -> Demand
1139 -- deferEval ds = defer (Eval ds)
1140 deferEval ds | allTop ds = Top
1141 | otherwise = Defer ds
1143 ---------------------
1144 absLub :: Demand -> Demand
1145 -- Computes (Abs `lub` d)
1146 -- For the Bot case consider
1147 -- f x y = if ... then x else error x
1148 -- Then for y we get Abs `lub` Bot, and we really
1153 absLub (Call _) = Top
1154 absLub (Box _) = Top
1155 absLub (Eval ds) = Defer (absLubs ds) -- Or (Defer ds)?
1156 absLub (Defer ds) = Defer (absLubs ds) -- Or (Defer ds)?
1158 absLubs :: Demands -> Demands
1159 absLubs = mapDmds absLub
1162 both :: Demand -> Demand -> Demand
1166 -- Note [Bottom demands]
1169 both Bot (Eval ds) = Eval (mapDmds (`both` Bot) ds)
1170 both Bot (Defer ds) = Eval (mapDmds (`both` Bot) ds)
1173 both Top Bot = errDmd
1176 both Top (Box d) = Box d
1177 both Top (Call d) = Call d
1178 both Top (Eval ds) = Eval (mapDmds (`both` Top) ds)
1179 both Top (Defer ds) -- = defer (Top `both` Eval ds)
1180 -- = defer (Eval (mapDmds (`both` Top) ds))
1181 = deferEval (mapDmds (`both` Top) ds)
1184 both (Box d1) (Box d2) = box (d1 `both` d2)
1185 both (Box d1) d2@(Call _) = box (d1 `both` d2)
1186 both (Box d1) d2@(Eval _) = box (d1 `both` d2)
1187 both (Box d1) (Defer _) = Box d1
1188 both d1@(Box _) d2 = d2 `both` d1
1190 both (Call d1) (Call d2) = Call (d1 `both` d2)
1191 both (Call d1) (Eval _) = Call d1 -- Could do better for (Poly Bot)?
1192 both (Call d1) (Defer _) = Call d1 -- Ditto
1193 both d1@(Call _) d2 = d2 `both` d1
1195 both (Eval ds1) (Eval ds2) = Eval (ds1 `boths` ds2)
1196 both (Eval ds1) (Defer ds2) = Eval (ds1 `boths` mapDmds defer ds2)
1197 both d1@(Eval _) d2 = d2 `both` d1
1199 both (Defer ds1) (Defer ds2) = deferEval (ds1 `boths` ds2)
1200 both d1@(Defer _) d2 = d2 `both` d1
1202 boths :: Demands -> Demands -> Demands
1203 boths ds1 ds2 = zipWithDmds both ds1 ds2
1206 Note [Bottom demands]
1207 ~~~~~~~~~~~~~~~~~~~~~
1210 From 'error' itself we get demand Bot on x
1211 From the arg demand on x we get
1212 x :-> evalDmd = Box (Eval (Poly Abs))
1213 So we get Bot `both` Box (Eval (Poly Abs))
1214 = Seq Keep (Poly Bot)
1217 f x = if ... then error (fst x) else fst x
1218 Then we get (Eval (Box Bot, Bot) `lub` Eval (SA))
1220 which is what we want.
1225 x :-> Bot `both` Defer [SA]
1226 and we want the Bot demand to cancel out the Defer
1227 so that we get Eval [SA]. Otherwise we'd have the odd
1229 f x = error (fst x) -- Strictness U(SA)b
1230 g x = error ('y':fst x) -- Strictness Tb