7c9ddd5c5701b30a344a8ea9960b03dcc7c52e6b
[ghc-hetmet.git] / compiler / stranal / DmdAnal.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1993-1998
3 %
4
5                         -----------------
6                         A demand analysis
7                         -----------------
8
9 \begin{code}
10 module DmdAnal ( dmdAnalPgm, dmdAnalTopRhs, 
11                  both {- needed by WwLib -}
12    ) where
13
14 #include "HsVersions.h"
15
16 import DynFlags         ( DynFlags )
17 import StaticFlags      ( opt_MaxWorkerArgs )
18 import Demand   -- All of it
19 import CoreSyn
20 import PprCore  
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, 
28                           setIdStrictness, idDemandInfo, idUnfolding,
29                           idDemandInfo_maybe, setIdDemandInfo
30                         )
31 import Var              ( Var )
32 import VarEnv
33 import TysWiredIn       ( unboxedPairDataCon )
34 import TysPrim          ( realWorldStatePrimTy )
35 import UniqFM           ( addToUFM_Directly, lookupUFM_Directly,
36                           minusUFM, filterUFM )
37 import Type             ( isUnLiftedType, coreEqType, splitTyConApp_maybe )
38 import Coercion         ( coercionKind )
39 import Util             ( mapAndUnzip, lengthIs, zipEqual )
40 import BasicTypes       ( Arity, TopLevelFlag(..), isTopLevel, isNeverActive,
41                           RecFlag(..), isRec, isMarkedStrict )
42 import Maybes           ( orElse, expectJust )
43 import Outputable
44 import Data.List
45 import FastString
46 \end{code}
47
48 To think about
49
50 * set a noinline pragma on bottoming Ids
51
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.
54
55
56 %************************************************************************
57 %*                                                                      *
58 \subsection{Top level stuff}
59 %*                                                                      *
60 %************************************************************************
61
62 \begin{code}
63 dmdAnalPgm :: DynFlags -> [CoreBind] -> IO [CoreBind]
64 dmdAnalPgm _ binds
65   = do {
66         let { binds_plus_dmds = do_prog binds } ;
67         return binds_plus_dmds
68     }
69   where
70     do_prog :: [CoreBind] -> [CoreBind]
71     do_prog binds = snd $ mapAccumL dmdAnalTopBind emptySigEnv binds
72
73 dmdAnalTopBind :: SigEnv
74                -> CoreBind 
75                -> (SigEnv, CoreBind)
76 dmdAnalTopBind sigs (NonRec id rhs)
77   = let
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
83     in
84     (sigs2, NonRec id2 rhs2)    
85
86 dmdAnalTopBind sigs (Rec pairs)
87   = let
88         (sigs', _, pairs')  = dmdFix TopLevel sigs pairs
89                 -- We get two iterations automatically
90                 -- c.f. the NonRec case above
91     in
92     (sigs', Rec pairs')
93 \end{code}
94
95 \begin{code}
96 dmdAnalTopRhs :: CoreExpr -> (StrictSig, CoreExpr)
97 -- Analyse the RHS and return
98 --      a) appropriate strictness info
99 --      b) the unfolding (decorated with stricntess info)
100 dmdAnalTopRhs rhs
101   = (sig, rhs2)
102   where
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.
113 \end{code}
114
115 %************************************************************************
116 %*                                                                      *
117 \subsection{The analyser itself}        
118 %*                                                                      *
119 %************************************************************************
120
121 \begin{code}
122 dmdAnal :: SigEnv -> Demand -> CoreExpr -> (DmdType, CoreExpr)
123
124 dmdAnal _ Abs  e = (topDmdType, e)
125
126 dmdAnal sigs dmd e 
127   | not (isStrictDmd dmd)
128   = let 
129         (res_ty, e') = dmdAnal sigs evalDmd e
130     in
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!
145
146
147 dmdAnal _ _ (Lit lit) = (topDmdType, Lit lit)
148 dmdAnal _ _ (Type ty) = (topDmdType, Type ty)   -- Doesn't happen, in fact
149
150 dmdAnal sigs dmd (Var var)
151   = (dmdTransform sigs var dmd, Var var)
152
153 dmdAnal sigs dmd (Cast e co)
154   = (dmd_ty, Cast e' co)
155   where
156     (dmd_ty, e') = dmdAnal sigs dmd' e
157     to_co        = snd (coercionKind co)
158     dmd'
159       | Just (tc, _) <- splitTyConApp_maybe to_co
160       , isRecursiveTyCon tc = evalDmd
161       | otherwise           = dmd
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
167
168 dmdAnal sigs dmd (Note n e)
169   = (dmd_ty, Note n e')
170   where
171     (dmd_ty, e') = dmdAnal sigs dmd e   
172
173 dmdAnal sigs dmd (App fun (Type ty))
174   = (fun_ty, App fun' (Type ty))
175   where
176     (fun_ty, fun') = dmdAnal sigs dmd fun
177
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
185     in
186     (res_ty `bothType` arg_ty, App fun' arg')
187
188 dmdAnal sigs dmd (Lam var body)
189   | isTyCoVar var
190   = let   
191         (body_ty, body') = dmdAnal sigs dmd body
192     in
193     (body_ty, Lam var body')
194
195   | Call body_dmd <- dmd        -- A call demand: good!
196   = let 
197         sigs'            = extendSigsWithLam sigs var
198         (body_ty, body') = dmdAnal sigs' body_dmd body
199         (lam_ty, var')   = annotateLamIdBndr sigs body_ty var
200     in
201     (lam_ty, Lam var' body')
202
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
207     in
208     (deferType lam_ty, Lam var' body')
209
210 dmdAnal sigs dmd (Case scrut case_bndr ty [alt@(DataAlt dc, _, _)])
211   | let tycon = dataConTyCon dc
212   , isProductTyCon tycon
213   , not (isRecursiveTyCon tycon)
214   = let
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.
222                 -- Example:
223                 --      f True  x = case x of y { I# x' -> if x' ==# 3 then y else I# 8 }
224                 --      f False x = I# 3
225                 --      
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 }
229                 --      fw False x = 3
230
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         
242         --
243         -- But then consider
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
249
250         alt_dmd            = Eval (Prod [idDemandInfo b | b <- bndrs', isId b])
251         scrut_dmd          = alt_dmd `both`
252                              idDemandInfo case_bndr'
253
254         (scrut_ty, scrut') = dmdAnal sigs scrut_dmd scrut
255     in
256     (alt_ty1 `bothType` scrut_ty, Case scrut' case_bndr' ty [alt'])
257
258 dmdAnal sigs dmd (Case scrut case_bndr ty alts)
259   = let
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
263     in
264 --    pprTrace "dmdAnal:Case" (ppr alts $$ ppr alt_tys)
265     (alt_ty `bothType` scrut_ty, Case scrut' case_bndr' ty alts')
266
267 dmdAnal sigs dmd (Let (NonRec id rhs) body) 
268   = let
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
273     in
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')    
287
288 dmdAnal sigs dmd (Let (Rec pairs) body) 
289   = let
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
294     in
295     sigs' `seq` body_ty `seq`
296     let
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
302     in
303     (body_ty2,  Let (Rec pairs') body')
304
305
306 dmdAnalAlt :: SigEnv -> Demand -> Alt Var -> (DmdType, Alt Var)
307 dmdAnalAlt sigs dmd (con,bndrs,rhs) 
308   = let 
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
313                      | otherwise    = alt_ty
314
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,
321         -- as if we had
322         --      case foo x s of 
323         --         (# s, r #) -> y 
324         --         other      -> return ()
325         -- So the 'y' isn't necessarily going to be evaluated
326         --
327         -- A more complete example where this shows up is:
328         --      do { let len = <expensive> ;
329         --         ; when (...) (exitWith ExitSuccess)
330         --         ; print len }
331
332         io_hack_reqd = con == DataAlt unboxedPairDataCon &&
333                        idType (head bndrs) `coreEqType` realWorldStatePrimTy
334     in  
335     (final_alt_ty, (con, bndrs', rhs'))
336
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 
343   where
344     add bndr dmd_ty = addVarDmd dmd_ty bndr seqDmd
345     str_bndrs = [ b | (b,s) <- zipEqual "addDataConPatBndrs"
346                                    (filter isId bndrs)
347                                    (dataConRepStrictness con)
348                     , isMarkedStrict s ]
349 \end{code}
350
351 Note [Add demands for strict constructors]
352 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
353 Consider this program (due to Roman):
354
355     data X a = X !a
356
357     foo :: X Int -> Int -> Int
358     foo (X a) n = go 0
359      where
360        go i | i < n     = a + go (i+1)
361             | otherwise = 0
362
363 We want the worker for 'foo' too look like this:
364
365     $wfoo :: Int# -> Int# -> Int#
366
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
372 `seq` thus:
373
374     foo (X a) n = a `seq` go 0
375
376 because the seq is discarded (very early) since X is strict!
377
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!
382
383
384 %************************************************************************
385 %*                                                                      *
386                     Demand transformer
387 %*                                                                      *
388 %************************************************************************
389
390 \begin{code}
391 dmdTransform :: SigEnv          -- The strictness environment
392              -> Id              -- The function
393              -> Demand          -- The demand on the function
394              -> DmdType         -- The demand type of the function in this context
395         -- Returned DmdEnv includes the demand on 
396         -- this function plus demand on its free variables
397
398 dmdTransform sigs var dmd
399
400 ------  DATA CONSTRUCTOR
401   | isDataConWorkId var         -- Data constructor
402   = let 
403         StrictSig dmd_ty    = idStrictness var  -- It must have a strictness sig
404         DmdType _ _ con_res = dmd_ty
405         arity               = idArity var
406     in
407     if arity == call_depth then         -- Saturated, so unleash the demand
408         let 
409                 -- Important!  If we Keep the constructor application, then
410                 -- we need the demands the constructor places (always lazy)
411                 -- If not, we don't need to.  For example:
412                 --      f p@(x,y) = (p,y)       -- S(AL)
413                 --      g a b     = f (a,b)
414                 -- It's vital that we don't calculate Absent for a!
415            dmd_ds = case res_dmd of
416                         Box (Eval ds) -> mapDmds box ds
417                         Eval ds       -> ds
418                         _             -> Poly Top
419
420                 -- ds can be empty, when we are just seq'ing the thing
421                 -- If so we must make up a suitable bunch of demands
422            arg_ds = case dmd_ds of
423                       Poly d  -> replicate arity d
424                       Prod ds -> ASSERT( ds `lengthIs` arity ) ds
425
426         in
427         mkDmdType emptyDmdEnv arg_ds con_res
428                 -- Must remember whether it's a product, hence con_res, not TopRes
429     else
430         topDmdType
431
432 ------  IMPORTED FUNCTION
433   | isGlobalId var,             -- Imported function
434     let StrictSig dmd_ty = idStrictness var
435   = -- pprTrace "strict-sig" (ppr var $$ ppr dmd_ty) $
436     if dmdTypeDepth dmd_ty <= call_depth then   -- Saturated, so unleash the demand
437         dmd_ty
438     else
439         topDmdType
440
441 ------  LOCAL LET/REC BOUND THING
442   | Just (StrictSig dmd_ty, top_lvl) <- lookupSigEnv sigs var
443   = let
444         fn_ty | dmdTypeDepth dmd_ty <= call_depth = dmd_ty 
445               | otherwise                         = deferType dmd_ty
446         -- NB: it's important to use deferType, and not just return topDmdType
447         -- Consider     let { f x y = p + x } in f 1
448         -- The application isn't saturated, but we must nevertheless propagate 
449         --      a lazy demand for p!  
450     in
451     if isTopLevel top_lvl then fn_ty    -- Don't record top level things
452     else addVarDmd fn_ty var dmd
453
454 ------  LOCAL NON-LET/REC BOUND THING
455   | otherwise                   -- Default case
456   = unitVarDmd var dmd
457
458   where
459     (call_depth, res_dmd) = splitCallDmd dmd
460 \end{code}
461
462 %************************************************************************
463 %*                                                                      *
464 \subsection{Bindings}
465 %*                                                                      *
466 %************************************************************************
467
468 \begin{code}
469 dmdFix :: TopLevelFlag
470        -> SigEnv                -- Does not include bindings for this binding
471        -> [(Id,CoreExpr)]
472        -> (SigEnv, DmdEnv,
473            [(Id,CoreExpr)])     -- Binders annotated with stricness info
474
475 dmdFix top_lvl sigs orig_pairs
476   = loop 1 initial_sigs orig_pairs
477   where
478     bndrs        = map fst orig_pairs
479     initial_sigs = addInitialSigs top_lvl sigs bndrs
480     
481     loop :: Int
482          -> SigEnv                      -- Already contains the current sigs
483          -> [(Id,CoreExpr)]             
484          -> (SigEnv, DmdEnv, [(Id,CoreExpr)])
485     loop n sigs pairs
486       | found_fixpoint
487       = (sigs', lazy_fv, pairs')
488                 -- Note: return pairs', not pairs.   pairs' is the result of 
489                 -- processing the RHSs with sigs (= sigs'), whereas pairs 
490                 -- is the result of processing the RHSs with the *previous* 
491                 -- iteration of sigs.
492
493       | n >= 10  
494       = pprTrace "dmdFix loop" (ppr n <+> (vcat 
495                         [ text "Sigs:" <+> ppr [ (id,lookupSigEnv sigs id, lookupSigEnv sigs' id) 
496                                                | (id,_) <- pairs],
497                           text "env:" <+> ppr sigs,
498                           text "binds:" <+> pprCoreBinding (Rec pairs)]))
499         (emptySigEnv, lazy_fv, orig_pairs)      -- Safe output
500                 -- The lazy_fv part is really important!  orig_pairs has no strictness
501                 -- info, including nothing about free vars.  But if we have
502                 --      letrec f = ....y..... in ...f...
503                 -- where 'y' is free in f, we must record that y is mentioned, 
504                 -- otherwise y will get recorded as absent altogether
505
506       | otherwise
507       = loop (n+1) (setNonVirgin sigs') pairs'
508       where
509         found_fixpoint = all (same_sig sigs sigs') bndrs 
510                 -- Use the new signature to do the next pair
511                 -- The occurrence analyser has arranged them in a good order
512                 -- so this can significantly reduce the number of iterations needed
513         ((sigs',lazy_fv), pairs') = mapAccumL my_downRhs (sigs, emptyDmdEnv) pairs
514         
515     my_downRhs (sigs,lazy_fv) (id,rhs) = ((sigs', lazy_fv'), pair')
516         where
517           (sigs', lazy_fv1, pair') = dmdAnalRhs top_lvl Recursive sigs (id,rhs)
518           lazy_fv'                 = plusVarEnv_C both lazy_fv lazy_fv1   
519            
520     same_sig sigs sigs' var = lookup sigs var == lookup sigs' var
521     lookup sigs var = case lookupSigEnv sigs var of
522                         Just (sig,_) -> sig
523                         Nothing      -> pprPanic "dmdFix" (ppr var)
524
525 dmdAnalRhs :: TopLevelFlag -> RecFlag
526         -> SigEnv -> (Id, CoreExpr)
527         -> (SigEnv,  DmdEnv, (Id, CoreExpr))
528 -- Process the RHS of the binding, add the strictness signature
529 -- to the Id, and augment the environment with the signature as well.
530
531 dmdAnalRhs top_lvl rec_flag sigs (id, rhs)
532  = (sigs', lazy_fv, (id', rhs'))
533  where
534   arity              = idArity id   -- The idArity should be up to date
535                                     -- The simplifier was run just beforehand
536   (rhs_dmd_ty, rhs') = dmdAnal sigs (vanillaCall arity) rhs
537   (lazy_fv, sig_ty)  = WARN( arity /= dmdTypeDepth rhs_dmd_ty && not (exprIsTrivial rhs), ppr id )
538                                 -- The RHS can be eta-reduced to just a variable, 
539                                 -- in which case we should not complain. 
540                        mkSigTy top_lvl rec_flag id rhs rhs_dmd_ty
541   id'                = id `setIdStrictness` sig_ty
542   sigs'              = extendSigEnv top_lvl sigs id sig_ty
543 \end{code}
544
545
546 %************************************************************************
547 %*                                                                      *
548 \subsection{Strictness signatures and types}
549 %*                                                                      *
550 %************************************************************************
551
552 \begin{code}
553 mkTopSigTy :: CoreExpr -> DmdType -> StrictSig
554         -- Take a DmdType and turn it into a StrictSig
555         -- NB: not used for never-inline things; hence False
556 mkTopSigTy rhs dmd_ty = snd (mk_sig_ty False False rhs dmd_ty)
557
558 mkSigTy :: TopLevelFlag -> RecFlag -> Id -> CoreExpr -> DmdType -> (DmdEnv, StrictSig)
559 mkSigTy top_lvl rec_flag id rhs dmd_ty 
560   = mk_sig_ty never_inline thunk_cpr_ok rhs dmd_ty
561   where
562     never_inline = isNeverActive (idInlineActivation id)
563     maybe_id_dmd = idDemandInfo_maybe id
564         -- Is Nothing the first time round
565
566     thunk_cpr_ok
567         | isTopLevel top_lvl       = False      -- Top level things don't get
568                                                 -- their demandInfo set at all
569         | isRec rec_flag           = False      -- Ditto recursive things
570         | Just dmd <- maybe_id_dmd = isStrictDmd dmd
571         | otherwise                = True       -- Optimistic, first time round
572                                                 -- See notes below
573 \end{code}
574
575 The thunk_cpr_ok stuff [CPR-AND-STRICTNESS]
576 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
577 If the rhs is a thunk, we usually forget the CPR info, because
578 it is presumably shared (else it would have been inlined, and 
579 so we'd lose sharing if w/w'd it into a function).  E.g.
580
581         let r = case expensive of
582                   (a,b) -> (b,a)
583         in ...
584
585 If we marked r as having the CPR property, then we'd w/w into
586
587         let $wr = \() -> case expensive of
588                             (a,b) -> (# b, a #)
589             r = case $wr () of
590                   (# b,a #) -> (b,a)
591         in ...
592
593 But now r is a thunk, which won't be inlined, so we are no further ahead.
594 But consider
595
596         f x = let r = case expensive of (a,b) -> (b,a)
597               in if foo r then r else (x,x)
598
599 Does f have the CPR property?  Well, no.
600
601 However, if the strictness analyser has figured out (in a previous 
602 iteration) that it's strict, then we DON'T need to forget the CPR info.
603 Instead we can retain the CPR info and do the thunk-splitting transform 
604 (see WorkWrap.splitThunk).
605
606 This made a big difference to PrelBase.modInt, which had something like
607         modInt = \ x -> let r = ... -> I# v in
608                         ...body strict in r...
609 r's RHS isn't a value yet; but modInt returns r in various branches, so
610 if r doesn't have the CPR property then neither does modInt
611 Another case I found in practice (in Complex.magnitude), looks like this:
612                 let k = if ... then I# a else I# b
613                 in ... body strict in k ....
614 (For this example, it doesn't matter whether k is returned as part of
615 the overall result; but it does matter that k's RHS has the CPR property.)  
616 Left to itself, the simplifier will make a join point thus:
617                 let $j k = ...body strict in k...
618                 if ... then $j (I# a) else $j (I# b)
619 With thunk-splitting, we get instead
620                 let $j x = let k = I#x in ...body strict in k...
621                 in if ... then $j a else $j b
622 This is much better; there's a good chance the I# won't get allocated.
623
624 The difficulty with this is that we need the strictness type to
625 look at the body... but we now need the body to calculate the demand
626 on the variable, so we can decide whether its strictness type should
627 have a CPR in it or not.  Simple solution: 
628         a) use strictness info from the previous iteration
629         b) make sure we do at least 2 iterations, by doing a second
630            round for top-level non-recs.  Top level recs will get at
631            least 2 iterations except for totally-bottom functions
632            which aren't very interesting anyway.
633
634 NB: strictly_demanded is never true of a top-level Id, or of a recursive Id.
635
636 The Nothing case in thunk_cpr_ok [CPR-AND-STRICTNESS]
637 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
638 Demand info now has a 'Nothing' state, just like strictness info.
639 The analysis works from 'dangerous' towards a 'safe' state; so we 
640 start with botSig for 'Nothing' strictness infos, and we start with
641 "yes, it's demanded" for 'Nothing' in the demand info.  The
642 fixpoint iteration will sort it all out.
643
644 We can't start with 'not-demanded' because then consider
645         f x = let 
646                   t = ... I# x
647               in
648               if ... then t else I# y else f x'
649
650 In the first iteration we'd have no demand info for x, so assume
651 not-demanded; then we'd get TopRes for f's CPR info.  Next iteration
652 we'd see that t was demanded, and so give it the CPR property, but by
653 now f has TopRes, so it will stay TopRes.  Instead, with the Nothing
654 setting the first time round, we say 'yes t is demanded' the first
655 time.
656
657 However, this does mean that for non-recursive bindings we must
658 iterate twice to be sure of not getting over-optimistic CPR info,
659 in the case where t turns out to be not-demanded.  This is handled
660 by dmdAnalTopBind.
661
662
663 Note [NOINLINE and strictness]
664 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
665 The strictness analyser used to have a HACK which ensured that NOINLNE
666 things were not strictness-analysed.  The reason was unsafePerformIO. 
667 Left to itself, the strictness analyser would discover this strictness 
668 for unsafePerformIO:
669         unsafePerformIO:  C(U(AV))
670 But then consider this sub-expression
671         unsafePerformIO (\s -> let r = f x in 
672                                case writeIORef v r s of (# s1, _ #) ->
673                                (# s1, r #)
674 The strictness analyser will now find that r is sure to be eval'd,
675 and may then hoist it out.  This makes tests/lib/should_run/memo002
676 deadlock.
677
678 Solving this by making all NOINLINE things have no strictness info is overkill.
679 In particular, it's overkill for runST, which is perfectly respectable.
680 Consider
681         f x = runST (return x)
682 This should be strict in x.
683
684 So the new plan is to define unsafePerformIO using the 'lazy' combinator:
685
686         unsafePerformIO (IO m) = lazy (case m realWorld# of (# _, r #) -> r)
687
688 Remember, 'lazy' is a wired-in identity-function Id, of type a->a, which is 
689 magically NON-STRICT, and is inlined after strictness analysis.  So
690 unsafePerformIO will look non-strict, and that's what we want.
691
692 Now we don't need the hack in the strictness analyser.  HOWEVER, this
693 decision does mean that even a NOINLINE function is not entirely
694 opaque: some aspect of its implementation leaks out, notably its
695 strictness.  For example, if you have a function implemented by an
696 error stub, but which has RULES, you may want it not to be eliminated
697 in favour of error!
698
699
700 \begin{code}
701 mk_sig_ty :: Bool -> Bool -> CoreExpr
702           -> DmdType -> (DmdEnv, StrictSig)
703 mk_sig_ty _never_inline thunk_cpr_ok rhs (DmdType fv dmds res) 
704   = (lazy_fv, mkStrictSig dmd_ty)
705         -- Re unused never_inline, see Note [NOINLINE and strictness]
706   where
707     dmd_ty = DmdType strict_fv final_dmds res'
708
709     lazy_fv   = filterUFM (not . isStrictDmd) fv
710     strict_fv = filterUFM isStrictDmd         fv
711         -- We put the strict FVs in the DmdType of the Id, so 
712         -- that at its call sites we unleash demands on its strict fvs.
713         -- An example is 'roll' in imaginary/wheel-sieve2
714         -- Something like this:
715         --      roll x = letrec 
716         --                   go y = if ... then roll (x-1) else x+1
717         --               in 
718         --               go ms
719         -- We want to see that roll is strict in x, which is because
720         -- go is called.   So we put the DmdEnv for x in go's DmdType.
721         --
722         -- Another example:
723         --      f :: Int -> Int -> Int
724         --      f x y = let t = x+1
725         --          h z = if z==0 then t else 
726         --                if z==1 then x+1 else
727         --                x + h (z-1)
728         --      in
729         --      h y
730         -- Calling h does indeed evaluate x, but we can only see
731         -- that if we unleash a demand on x at the call site for t.
732         --
733         -- Incidentally, here's a place where lambda-lifting h would
734         -- lose the cigar --- we couldn't see the joint strictness in t/x
735         --
736         --      ON THE OTHER HAND
737         -- We don't want to put *all* the fv's from the RHS into the
738         -- DmdType, because that makes fixpointing very slow --- the 
739         -- DmdType gets full of lazy demands that are slow to converge.
740
741     final_dmds = setUnpackStrategy dmds
742         -- Set the unpacking strategy
743         
744     res' = case res of
745                 RetCPR | ignore_cpr_info -> TopRes
746                 _                        -> res
747     ignore_cpr_info = not (exprIsHNF rhs || thunk_cpr_ok)
748 \end{code}
749
750 The unpack strategy determines whether we'll *really* unpack the argument,
751 or whether we'll just remember its strictness.  If unpacking would give
752 rise to a *lot* of worker args, we may decide not to unpack after all.
753
754 \begin{code}
755 setUnpackStrategy :: [Demand] -> [Demand]
756 setUnpackStrategy ds
757   = snd (go (opt_MaxWorkerArgs - nonAbsentArgs ds) ds)
758   where
759     go :: Int                   -- Max number of args available for sub-components of [Demand]
760        -> [Demand]
761        -> (Int, [Demand])       -- Args remaining after subcomponents of [Demand] are unpacked
762
763     go n (Eval (Prod cs) : ds) 
764         | n' >= 0   = Eval (Prod cs') `cons` go n'' ds
765         | otherwise = Box (Eval (Prod cs)) `cons` go n ds
766         where
767           (n'',cs') = go n' cs
768           n' = n + 1 - non_abs_args
769                 -- Add one to the budget 'cos we drop the top-level arg
770           non_abs_args = nonAbsentArgs cs
771                 -- Delete # of non-absent args to which we'll now be committed
772                                 
773     go n (d:ds) = d `cons` go n ds
774     go n []     = (n,[])
775
776     cons d (n,ds) = (n, d:ds)
777
778 nonAbsentArgs :: [Demand] -> Int
779 nonAbsentArgs []         = 0
780 nonAbsentArgs (Abs : ds) = nonAbsentArgs ds
781 nonAbsentArgs (_   : ds) = 1 + nonAbsentArgs ds
782 \end{code}
783
784
785 %************************************************************************
786 %*                                                                      *
787 \subsection{Strictness signatures and types}
788 %*                                                                      *
789 %************************************************************************
790
791 \begin{code}
792 unitVarDmd :: Var -> Demand -> DmdType
793 unitVarDmd var dmd = DmdType (unitVarEnv var dmd) [] TopRes
794
795 addVarDmd :: DmdType -> Var -> Demand -> DmdType
796 addVarDmd (DmdType fv ds res) var dmd
797   = DmdType (extendVarEnv_C both fv var dmd) ds res
798
799 addLazyFVs :: DmdType -> DmdEnv -> DmdType
800 addLazyFVs (DmdType fv ds res) lazy_fvs
801   = DmdType both_fv1 ds res
802   where
803     both_fv = plusVarEnv_C both fv lazy_fvs
804     both_fv1 = modifyEnv (isBotRes res) (`both` Bot) lazy_fvs fv both_fv
805         -- This modifyEnv is vital.  Consider
806         --      let f = \x -> (x,y)
807         --      in  error (f 3)
808         -- Here, y is treated as a lazy-fv of f, but we must `both` that L
809         -- demand with the bottom coming up from 'error'
810         -- 
811         -- I got a loop in the fixpointer without this, due to an interaction
812         -- with the lazy_fv filtering in mkSigTy.  Roughly, it was
813         --      letrec f n x 
814         --          = letrec g y = x `fatbar` 
815         --                         letrec h z = z + ...g...
816         --                         in h (f (n-1) x)
817         --      in ...
818         -- In the initial iteration for f, f=Bot
819         -- Suppose h is found to be strict in z, but the occurrence of g in its RHS
820         -- is lazy.  Now consider the fixpoint iteration for g, esp the demands it
821         -- places on its free variables.  Suppose it places none.  Then the
822         --      x `fatbar` ...call to h...
823         -- will give a x->V demand for x.  That turns into a L demand for x,
824         -- which floats out of the defn for h.  Without the modifyEnv, that
825         -- L demand doesn't get both'd with the Bot coming up from the inner
826         -- call to f.  So we just get an L demand for x for g.
827         --
828         -- A better way to say this is that the lazy-fv filtering should give the
829         -- same answer as putting the lazy fv demands in the function's type.
830
831 annotateBndr :: DmdType -> Var -> (DmdType, Var)
832 -- The returned env has the var deleted
833 -- The returned var is annotated with demand info
834 -- No effect on the argument demands
835 annotateBndr dmd_ty@(DmdType fv ds res) var
836   | isTyCoVar var = (dmd_ty, var)
837   | otherwise   = (DmdType fv' ds res, setIdDemandInfo var dmd)
838   where
839     (fv', dmd) = removeFV fv var res
840
841 annotateBndrs :: DmdType -> [Var] -> (DmdType, [Var])
842 annotateBndrs = mapAccumR annotateBndr
843
844 annotateLamIdBndr :: SigEnv
845                   -> DmdType    -- Demand type of body
846                   -> Id         -- Lambda binder
847                   -> (DmdType,  -- Demand type of lambda
848                       Id)       -- and binder annotated with demand     
849
850 annotateLamIdBndr sigs (DmdType fv ds res) id
851 -- For lambdas we add the demand to the argument demands
852 -- Only called for Ids
853   = ASSERT( isId id )
854     (final_ty, setIdDemandInfo id hacked_dmd)
855   where
856       -- Watch out!  See note [Lambda-bound unfoldings]
857     final_ty = case maybeUnfoldingTemplate (idUnfolding id) of
858                  Nothing  -> main_ty
859                  Just unf -> main_ty `bothType` unf_ty
860                           where
861                              (unf_ty, _) = dmdAnal sigs dmd unf
862     
863     main_ty = DmdType fv' (hacked_dmd:ds) res
864
865     (fv', dmd) = removeFV fv id res
866     hacked_dmd = argDemand dmd
867         -- This call to argDemand is vital, because otherwise we label
868         -- a lambda binder with demand 'B'.  But in terms of calling
869         -- conventions that's Abs, because we don't pass it.  But
870         -- when we do a w/w split we get
871         --      fw x = (\x y:B -> ...) x (error "oops")
872         -- And then the simplifier things the 'B' is a strict demand
873         -- and evaluates the (error "oops").  Sigh
874
875 removeFV :: DmdEnv -> Var -> DmdResult -> (DmdEnv, Demand)
876 removeFV fv id res = (fv', zapUnlifted id dmd)
877                 where
878                   fv' = fv `delVarEnv` id
879                   dmd = lookupVarEnv fv id `orElse` deflt
880                   deflt | isBotRes res = Bot
881                         | otherwise    = Abs
882
883 zapUnlifted :: Id -> Demand -> Demand
884 -- For unlifted-type variables, we are only 
885 -- interested in Bot/Abs/Box Abs
886 zapUnlifted _  Bot = Bot
887 zapUnlifted _  Abs = Abs
888 zapUnlifted id dmd | isUnLiftedType (idType id) = lazyDmd
889                    | otherwise                  = dmd
890 \end{code}
891
892 Note [Lamba-bound unfoldings]
893 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
894 We allow a lambda-bound variable to carry an unfolding, a facility that is used
895 exclusively for join points; see Note [Case binders and join points].  If so,
896 we must be careful to demand-analyse the RHS of the unfolding!  Example
897    \x. \y{=Just x}. <body>
898 Then if <body> uses 'y', then transitively it uses 'x', and we must not
899 forget that fact, otherwise we might make 'x' absent when it isn't.
900
901
902 %************************************************************************
903 %*                                                                      *
904 \subsection{Strictness signatures}
905 %*                                                                      *
906 %************************************************************************
907
908 \begin{code}
909 data SigEnv  
910   = SE { se_env    :: VarEnv (StrictSig, TopLevelFlag)
911        , se_virgin :: Bool }  -- True on first iteration only
912                               -- See Note [Initialising strictness]
913         -- We use the se_env to tell us whether to
914         -- record info about a variable in the DmdEnv
915         -- We do so if it's a LocalId, but not top-level
916         --
917         -- The DmdEnv gives the demand on the free vars of the function
918         -- when it is given enough args to satisfy the strictness signature
919
920 instance Outputable SigEnv where
921   ppr (SE { se_env = env, se_virgin = virgin })
922     = ptext (sLit "SE") <+> braces (vcat 
923          [ ptext (sLit "se_virgin =") <+> ppr virgin
924          , ptext (sLit "se_env =") <+> ppr env ])
925
926 emptySigEnv :: SigEnv
927 emptySigEnv  = SE { se_env = emptyVarEnv, se_virgin = True }
928
929 extendSigEnv :: TopLevelFlag -> SigEnv -> Id -> StrictSig -> SigEnv
930 extendSigEnv top_lvl sigs var sig 
931   = sigs { se_env = extendVarEnv (se_env sigs) var (sig, top_lvl) }
932
933 lookupSigEnv :: SigEnv -> Id -> Maybe (StrictSig, TopLevelFlag)
934 lookupSigEnv sigs id = lookupVarEnv (se_env sigs) id
935
936 addInitialSigs :: TopLevelFlag -> SigEnv -> [Id] -> SigEnv
937 -- See Note [Initialising strictness]
938 addInitialSigs top_lvl sigs@(SE { se_env = env, se_virgin = virgin }) ids
939   = sigs { se_env = extendVarEnvList env [ (id, (init_sig id, top_lvl)) 
940                                          | id <- ids ] }
941   where
942     init_sig | virgin    = \_ -> botSig
943              | otherwise = idStrictness
944
945 setNonVirgin :: SigEnv -> SigEnv
946 setNonVirgin sigs = sigs { se_virgin = False }
947
948 extendSigsWithLam :: SigEnv -> Id -> SigEnv
949 -- Extend the SigEnv when we meet a lambda binder
950 -- If the binder is marked demanded with a product demand, then give it a CPR 
951 -- signature, because in the likely event that this is a lambda on a fn defn 
952 -- [we only use this when the lambda is being consumed with a call demand],
953 -- it'll be w/w'd and so it will be CPR-ish.  E.g.
954 --      f = \x::(Int,Int).  if ...strict in x... then
955 --                              x
956 --                          else
957 --                              (a,b)
958 -- We want f to have the CPR property because x does, by the time f has been w/w'd
959 --
960 -- Also note that we only want to do this for something that
961 -- definitely has product type, else we may get over-optimistic 
962 -- CPR results (e.g. from \x -> x!).
963
964 extendSigsWithLam sigs id
965   = case idDemandInfo_maybe id of
966         Nothing              -> extendSigEnv NotTopLevel sigs id cprSig
967                 -- Optimistic in the Nothing case;
968                 -- See notes [CPR-AND-STRICTNESS]
969         Just (Eval (Prod _)) -> extendSigEnv NotTopLevel sigs id cprSig
970         _                    -> sigs
971 \end{code}
972
973 Note [Initialising strictness]
974 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
975 Our basic plan is to initialise the strictness of each Id in 
976 a recursive group to "bottom", and find a fixpoint from there.
977 However, this group A might be inside an *enclosing* recursive
978 group B, in which case we'll do the entire fixpoint shebang on A
979 for each iteration of B.
980
981 To speed things up, we initialise each iteration of B from the result
982 of the last one, which is neatly recorded in each binder.  That way we
983 make use of earlier iterations of the fixpoint algorithm.  (Cunning
984 plan.)  
985
986 But on the *first* iteration we want to *ignore* the current strictness
987 of the Id, and start from "bottom".  Nowadays the Id can have a current
988 strictness, because interface files record strictness for nested bindings.
989 To know when we are in the first iteration, we look at the se_virgin
990 field of the SigEnv.
991
992
993 %************************************************************************
994 %*                                                                      *
995                    Demands
996 %*                                                                      *
997 %************************************************************************
998
999 \begin{code}
1000 splitDmdTy :: DmdType -> (Demand, DmdType)
1001 -- Split off one function argument
1002 -- We already have a suitable demand on all
1003 -- free vars, so no need to add more!
1004 splitDmdTy (DmdType fv (dmd:dmds) res_ty) = (dmd, DmdType fv dmds res_ty)
1005 splitDmdTy ty@(DmdType _ [] res_ty)       = (resTypeArgDmd res_ty, ty)
1006
1007 splitCallDmd :: Demand -> (Int, Demand)
1008 splitCallDmd (Call d) = case splitCallDmd d of
1009                           (n, r) -> (n+1, r)
1010 splitCallDmd d        = (0, d)
1011
1012 vanillaCall :: Arity -> Demand
1013 vanillaCall 0 = evalDmd
1014 vanillaCall n = Call (vanillaCall (n-1))
1015
1016 deferType :: DmdType -> DmdType
1017 deferType (DmdType fv _ _) = DmdType (deferEnv fv) [] TopRes
1018         -- Notice that we throw away info about both arguments and results
1019         -- For example,   f = let ... in \x -> x
1020         -- We don't want to get a stricness type V->T for f.
1021
1022 deferEnv :: DmdEnv -> DmdEnv
1023 deferEnv fv = mapVarEnv defer fv
1024
1025
1026 ----------------
1027 argDemand :: Demand -> Demand
1028 -- The 'Defer' demands are just Lazy at function boundaries
1029 -- Ugly!  Ask John how to improve it.
1030 argDemand Top       = lazyDmd
1031 argDemand (Defer _) = lazyDmd
1032 argDemand (Eval ds) = Eval (mapDmds argDemand ds)
1033 argDemand (Box Bot) = evalDmd
1034 argDemand (Box d)   = box (argDemand d)
1035 argDemand Bot       = Abs       -- Don't pass args that are consumed (only) by bottom
1036 argDemand d         = d
1037 \end{code}
1038
1039 \begin{code}
1040 -------------------------
1041 lubType :: DmdType -> DmdType -> DmdType
1042 -- Consider (if x then y else []) with demand V
1043 -- Then the first branch gives {y->V} and the second
1044 --  *implicitly* has {y->A}.  So we must put {y->(V `lub` A)}
1045 -- in the result env.
1046 lubType (DmdType fv1 ds1 r1) (DmdType fv2 ds2 r2)
1047   = DmdType lub_fv2 (lub_ds ds1 ds2) (r1 `lubRes` r2)
1048   where
1049     lub_fv  = plusVarEnv_C lub fv1 fv2
1050     lub_fv1 = modifyEnv (not (isBotRes r1)) absLub fv2 fv1 lub_fv
1051     lub_fv2 = modifyEnv (not (isBotRes r2)) absLub fv1 fv2 lub_fv1
1052         -- lub is the identity for Bot
1053
1054         -- Extend the shorter argument list to match the longer
1055     lub_ds (d1:ds1) (d2:ds2) = lub d1 d2 : lub_ds ds1 ds2
1056     lub_ds []       []       = []
1057     lub_ds ds1      []       = map (`lub` resTypeArgDmd r2) ds1
1058     lub_ds []       ds2      = map (resTypeArgDmd r1 `lub`) ds2
1059
1060 -----------------------------------
1061 bothType :: DmdType -> DmdType -> DmdType
1062 -- (t1 `bothType` t2) takes the argument/result info from t1,
1063 -- using t2 just for its free-var info
1064 -- NB: Don't forget about r2!  It might be BotRes, which is
1065 --     a bottom demand on all the in-scope variables.
1066 -- Peter: can this be done more neatly?
1067 bothType (DmdType fv1 ds1 r1) (DmdType fv2 _ r2)
1068   = DmdType both_fv2 ds1 (r1 `bothRes` r2)
1069   where
1070     both_fv  = plusVarEnv_C both fv1 fv2
1071     both_fv1 = modifyEnv (isBotRes r1) (`both` Bot) fv2 fv1 both_fv
1072     both_fv2 = modifyEnv (isBotRes r2) (`both` Bot) fv1 fv2 both_fv1
1073         -- both is the identity for Abs
1074 \end{code}
1075
1076
1077 \begin{code}
1078 lubRes :: DmdResult -> DmdResult -> DmdResult
1079 lubRes BotRes r      = r
1080 lubRes r      BotRes = r
1081 lubRes RetCPR RetCPR = RetCPR
1082 lubRes _      _      = TopRes
1083
1084 bothRes :: DmdResult -> DmdResult -> DmdResult
1085 -- If either diverges, the whole thing does
1086 -- Otherwise take CPR info from the first
1087 bothRes _  BotRes = BotRes
1088 bothRes r1 _      = r1
1089 \end{code}
1090
1091 \begin{code}
1092 modifyEnv :: Bool                       -- No-op if False
1093           -> (Demand -> Demand)         -- The zapper
1094           -> DmdEnv -> DmdEnv           -- Env1 and Env2
1095           -> DmdEnv -> DmdEnv           -- Transform this env
1096         -- Zap anything in Env1 but not in Env2
1097         -- Assume: dom(env) includes dom(Env1) and dom(Env2)
1098
1099 modifyEnv need_to_modify zapper env1 env2 env
1100   | need_to_modify = foldr zap env (varEnvKeys (env1 `minusUFM` env2))
1101   | otherwise      = env
1102   where
1103     zap uniq env = addToUFM_Directly env uniq (zapper current_val)
1104                  where
1105                    current_val = expectJust "modifyEnv" (lookupUFM_Directly env uniq)
1106 \end{code}
1107
1108
1109 %************************************************************************
1110 %*                                                                      *
1111 \subsection{LUB and BOTH}
1112 %*                                                                      *
1113 %************************************************************************
1114
1115 \begin{code}
1116 lub :: Demand -> Demand -> Demand
1117
1118 lub Bot         d2 = d2
1119 lub Abs         d2 = absLub d2
1120 lub Top         _  = Top
1121 lub (Defer ds1) d2 = defer (Eval ds1 `lub` d2)
1122
1123 lub (Call d1)   (Call d2)    = Call (d1 `lub` d2)
1124 lub d1@(Call _) (Box d2)     = d1 `lub` d2      -- Just strip the box
1125 lub    (Call _) d2@(Eval _)  = d2               -- Presumably seq or vanilla eval
1126 lub d1@(Call _) d2           = d2 `lub` d1      -- Bot, Abs, Top
1127
1128 -- For the Eval case, we use these approximation rules
1129 -- Box Bot       <= Eval (Box Bot ...)
1130 -- Box Top       <= Defer (Box Bot ...)
1131 -- Box (Eval ds) <= Eval (map Box ds)
1132 lub (Eval ds1)  (Eval ds2)        = Eval (ds1 `lubs` ds2)
1133 lub (Eval ds1)  (Box Bot)         = Eval (mapDmds (`lub` Box Bot) ds1)
1134 lub (Eval ds1)  (Box (Eval ds2)) = Eval (ds1 `lubs` mapDmds box ds2)
1135 lub (Eval ds1)  (Box Abs)        = deferEval (mapDmds (`lub` Box Bot) ds1)
1136 lub d1@(Eval _) d2                = d2 `lub` d1 -- Bot,Abs,Top,Call,Defer
1137
1138 lub (Box d1)   (Box d2) = box (d1 `lub` d2)
1139 lub d1@(Box _)  d2      = d2 `lub` d1
1140
1141 lubs :: Demands -> Demands -> Demands
1142 lubs ds1 ds2 = zipWithDmds lub ds1 ds2
1143
1144 ---------------------
1145 box :: Demand -> Demand
1146 -- box is the smart constructor for Box
1147 -- It computes <B,bot> & d
1148 -- INVARIANT: (Box d) => d = Bot, Abs, Eval
1149 -- Seems to be no point in allowing (Box (Call d))
1150 box (Call d)  = Call d  -- The odd man out.  Why?
1151 box (Box d)   = Box d
1152 box (Defer _) = lazyDmd
1153 box Top       = lazyDmd -- Box Abs and Box Top
1154 box Abs       = lazyDmd -- are the same <B,L>
1155 box d         = Box d   -- Bot, Eval
1156
1157 ---------------
1158 defer :: Demand -> Demand
1159
1160 -- defer is the smart constructor for Defer
1161 -- The idea is that (Defer ds) = <U(ds), L>
1162 --
1163 -- It specifies what happens at a lazy function argument
1164 -- or a lambda; the L* operator
1165 -- Set the strictness part to L, but leave
1166 -- the boxity side unaffected
1167 -- It also ensures that Defer (Eval [LLLL]) = L
1168
1169 defer Bot        = Abs
1170 defer Abs        = Abs
1171 defer Top        = Top
1172 defer (Call _)   = lazyDmd      -- Approximation here?
1173 defer (Box _)    = lazyDmd
1174 defer (Defer ds) = Defer ds
1175 defer (Eval ds)  = deferEval ds
1176
1177 deferEval :: Demands -> Demand
1178 -- deferEval ds = defer (Eval ds)
1179 deferEval ds | allTop ds = Top
1180              | otherwise  = Defer ds
1181
1182 ---------------------
1183 absLub :: Demand -> Demand
1184 -- Computes (Abs `lub` d)
1185 -- For the Bot case consider
1186 --      f x y = if ... then x else error x
1187 --   Then for y we get Abs `lub` Bot, and we really
1188 --   want Abs overall
1189 absLub Bot        = Abs
1190 absLub Abs        = Abs
1191 absLub Top        = Top
1192 absLub (Call _)   = Top
1193 absLub (Box _)    = Top
1194 absLub (Eval ds)  = Defer (absLubs ds)  -- Or (Defer ds)?
1195 absLub (Defer ds) = Defer (absLubs ds)  -- Or (Defer ds)?
1196
1197 absLubs :: Demands -> Demands
1198 absLubs = mapDmds absLub
1199
1200 ---------------
1201 both :: Demand -> Demand -> Demand
1202
1203 both Abs d2 = d2
1204
1205 -- Note [Bottom demands]
1206 both Bot Bot        = Bot
1207 both Bot Abs        = Bot 
1208 both Bot (Eval ds)  = Eval (mapDmds (`both` Bot) ds)
1209 both Bot (Defer ds) = Eval (mapDmds (`both` Bot) ds)
1210 both Bot _          = errDmd
1211
1212 both Top Bot        = errDmd
1213 both Top Abs        = Top
1214 both Top Top        = Top
1215 both Top (Box d)    = Box d
1216 both Top (Call d)   = Call d
1217 both Top (Eval ds)  = Eval (mapDmds (`both` Top) ds)
1218 both Top (Defer ds)     -- = defer (Top `both` Eval ds)
1219                         -- = defer (Eval (mapDmds (`both` Top) ds))
1220                      = deferEval (mapDmds (`both` Top) ds)
1221
1222
1223 both (Box d1)   (Box d2)    = box (d1 `both` d2)
1224 both (Box d1)   d2@(Call _) = box (d1 `both` d2)
1225 both (Box d1)   d2@(Eval _) = box (d1 `both` d2)
1226 both (Box d1)   (Defer _)   = Box d1
1227 both d1@(Box _) d2          = d2 `both` d1
1228
1229 both (Call d1)   (Call d2)   = Call (d1 `both` d2)
1230 both (Call d1)   (Eval _)    = Call d1  -- Could do better for (Poly Bot)?
1231 both (Call d1)   (Defer _)   = Call d1  -- Ditto
1232 both d1@(Call _) d2          = d2 `both` d1
1233
1234 both (Eval ds1)  (Eval  ds2) = Eval (ds1 `boths` ds2)
1235 both (Eval ds1)  (Defer ds2) = Eval (ds1 `boths` mapDmds defer ds2)
1236 both d1@(Eval _) d2          = d2 `both` d1
1237
1238 both (Defer ds1)  (Defer ds2) = deferEval (ds1 `boths` ds2)
1239 both d1@(Defer _) d2          = d2 `both` d1
1240  
1241 boths :: Demands -> Demands -> Demands
1242 boths ds1 ds2 = zipWithDmds both ds1 ds2
1243 \end{code}
1244
1245 Note [Bottom demands]
1246 ~~~~~~~~~~~~~~~~~~~~~
1247 Consider
1248         f x = error x
1249 From 'error' itself we get demand Bot on x
1250 From the arg demand on x we get 
1251         x :-> evalDmd = Box (Eval (Poly Abs))
1252 So we get  Bot `both` Box (Eval (Poly Abs))
1253             = Seq Keep (Poly Bot)
1254
1255 Consider also
1256         f x = if ... then error (fst x) else fst x
1257 Then we get (Eval (Box Bot, Bot) `lub` Eval (SA))
1258         = Eval (SA)
1259 which is what we want.
1260
1261 Consider also
1262   f x = error [fst x]
1263 Then we get 
1264      x :-> Bot `both` Defer [SA]
1265 and we want the Bot demand to cancel out the Defer
1266 so that we get Eval [SA].  Otherwise we'd have the odd
1267 situation that
1268   f x = error (fst x)      -- Strictness U(SA)b
1269   g x = error ('y':fst x)  -- Strictness Tb
1270