[project @ 2001-08-31 14:40:31 by simonmar]
[ghc-hetmet.git] / ghc / 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, both {- needed by WwLib -} ) where
11
12 #include "HsVersions.h"
13
14 import CmdLineOpts      ( DynFlags, DynFlag(..), opt_MaxWorkerArgs )
15 import NewDemand        -- All of it
16 import CoreSyn
17 import CoreUtils        ( exprIsValue, exprArity )
18 import DataCon          ( dataConTyCon )
19 import TyCon            ( isProductTyCon, isRecursiveTyCon )
20 import Id               ( Id, idType, idDemandInfo,
21                           isDataConId, isImplicitId, isGlobalId,
22                           idNewStrictness, idNewStrictness_maybe, getNewStrictness, setIdNewStrictness,
23                           idNewDemandInfo, setIdNewDemandInfo, newStrictnessFromOld )
24 import IdInfo           ( newDemand )
25 import Var              ( Var )
26 import VarEnv
27 import UniqFM           ( plusUFM_C, addToUFM_Directly, lookupUFM_Directly,
28                           keysUFM, minusUFM, ufmToList, filterUFM )
29 import Type             ( isUnLiftedType )
30 import CoreLint         ( showPass, endPass )
31 import Util             ( mapAndUnzip, mapAccumL, mapAccumR )
32 import BasicTypes       ( Arity, TopLevelFlag(..), isTopLevel )
33 import Maybes           ( orElse, expectJust )
34 import Outputable
35 \end{code}
36
37 To think about
38
39 * set a noinline pragma on bottoming Ids
40
41 * Consider f x = x+1 `fatbar` error (show x)
42   We'd like to unbox x, even if that means reboxing it in the error case.
43
44 \begin{code}
45 instance Outputable TopLevelFlag where
46   ppr flag = empty
47 \end{code}
48
49 %************************************************************************
50 %*                                                                      *
51 \subsection{Top level stuff}
52 %*                                                                      *
53 %************************************************************************
54
55 \begin{code}
56 dmdAnalPgm :: DynFlags -> [CoreBind] -> IO [CoreBind]
57 dmdAnalPgm dflags binds
58   = do {
59         showPass dflags "Demand analysis" ;
60         let { binds_plus_dmds = do_prog binds ;
61               dmd_changes = get_changes binds_plus_dmds } ;
62         endPass dflags "Demand analysis" 
63                 Opt_D_dump_stranal binds_plus_dmds ;
64 #ifdef DEBUG
65         -- Only if DEBUG is on, because only then is the old strictness analyser run
66         printDump (text "Changes in demands" $$ dmd_changes) ;
67 #endif
68         return binds_plus_dmds
69     }
70   where
71     do_prog :: [CoreBind] -> [CoreBind]
72     do_prog binds = snd $ mapAccumL dmdAnalTopBind emptySigEnv binds
73
74 dmdAnalTopBind :: SigEnv
75                -> CoreBind 
76                -> (SigEnv, CoreBind)
77 dmdAnalTopBind sigs (NonRec id rhs)
78   | isImplicitId id             -- Don't touch the info on constructors, selectors etc
79   = (sigs, NonRec id rhs)       -- It's pre-computed in MkId.lhs
80   | otherwise
81   = let
82         (sigs', _, (id', rhs')) = downRhs TopLevel sigs (id, rhs)
83     in
84     (sigs', NonRec id' rhs')    
85
86 dmdAnalTopBind sigs (Rec pairs)
87   = let
88         (sigs', _, pairs')  = dmdFix TopLevel sigs pairs
89     in
90     (sigs', Rec pairs')
91 \end{code}
92
93
94 %************************************************************************
95 %*                                                                      *
96 \subsection{The analyser itself}        
97 %*                                                                      *
98 %************************************************************************
99
100 \begin{code}
101 dmdAnal :: SigEnv -> Demand -> CoreExpr -> (DmdType, CoreExpr)
102
103 dmdAnal sigs Abs  e = (topDmdType, e)
104
105 dmdAnal sigs Lazy e = let 
106                         (res_ty, e') = dmdAnal sigs Eval e
107                       in
108                       (deferType res_ty, e')
109         -- It's important not to analyse e with a lazy demand because
110         -- a) When we encounter   case s of (a,b) -> 
111         --      we demand s with U(d1d2)... but if the overall demand is lazy
112         --      that is wrong, and we'd need to reduce the demand on s,
113         --      which is inconvenient
114         -- b) More important, consider
115         --      f (let x = R in x+x), where f is lazy
116         --    We still want to mark x as demanded, because it will be when we
117         --    enter the let.  If we analyse f's arg with a Lazy demand, we'll
118         --    just mark x as Lazy
119
120
121 dmdAnal sigs dmd (Lit lit)
122   = (topDmdType, Lit lit)
123
124 dmdAnal sigs dmd (Var var)
125   = (dmdTransform sigs var dmd, Var var)
126
127 dmdAnal sigs dmd (Note n e)
128   = (dmd_ty, Note n e')
129   where
130     (dmd_ty, e') = dmdAnal sigs dmd' e  
131     dmd' = case n of
132              Coerce _ _ -> Eval   -- This coerce usually arises from a recursive
133              other      -> dmd    -- newtype, and we don't want to look inside them
134                                   -- for exactly the same reason that we don't look
135                                   -- inside recursive products -- we might not reach
136                                   -- a fixpoint.  So revert to a vanilla Eval demand
137
138 dmdAnal sigs dmd (App fun (Type ty))
139   = (fun_ty, App fun' (Type ty))
140   where
141     (fun_ty, fun') = dmdAnal sigs dmd fun
142
143 dmdAnal sigs dmd (App fun arg)  -- Non-type arguments
144   = let                         -- [Type arg handled above]
145         (fun_ty, fun')    = dmdAnal sigs (Call dmd) fun
146         (arg_ty, arg')    = dmdAnal sigs arg_dmd arg
147         (arg_dmd, res_ty) = splitDmdTy fun_ty
148     in
149     (res_ty `bothType` arg_ty, App fun' arg')
150
151 dmdAnal sigs dmd (Lam var body)
152   | isTyVar var
153   = let   
154         (body_ty, body') = dmdAnal sigs dmd body
155     in
156     (body_ty, Lam var body')
157
158   | Call body_dmd <- dmd        -- A call demand: good!
159   = let 
160         (body_ty, body') = dmdAnal sigs body_dmd body
161         (lam_ty, var')   = annotateLamIdBndr body_ty var
162     in
163     (lam_ty, Lam var' body')
164
165   | otherwise   -- Not enough demand on the lambda; but do the body
166   = let         -- anyway to annotate it and gather free var info
167         (body_ty, body') = dmdAnal sigs Eval body
168         (lam_ty, var')   = annotateLamIdBndr body_ty var
169     in
170     (deferType lam_ty, Lam var' body')
171
172 dmdAnal sigs dmd (Case scrut case_bndr [alt@(DataAlt dc,bndrs,rhs)])
173   | let tycon = dataConTyCon dc,
174     isProductTyCon tycon,
175     not (isRecursiveTyCon tycon)
176   = let
177         (alt_ty, alt')           = dmdAnalAlt sigs dmd alt
178         (alt_ty1, case_bndr')    = annotateBndr alt_ty case_bndr
179         (_, bndrs', _)           = alt'
180
181         -- Figure out whether the demand on the case binder is used, and use
182         -- that to set the scrut_dmd.  This is utterly essential.
183         -- Consider     f x = case x of y { (a,b) -> k y a }
184         -- If we just take scrut_demand = U(L,A), then we won't pass x to the
185         -- worker, so the worker will rebuild 
186         --      x = (a, absent-error)
187         -- and that'll crash.
188         -- So at one stage I had:
189         --      dead_case_bndr           = isAbsentDmd (idNewDemandInfo case_bndr')
190         --      keepity | dead_case_bndr = Drop
191         --              | otherwise      = Keep         
192         --
193         -- But then consider
194         --      case x of y { (a,b) -> h y + a }
195         -- where h : U(LL) -> T
196         -- The above code would compute a Keep for x, since y is not Abs, which is silly
197         -- The insight is, of course, that a demand on y is a demand on the
198         -- scrutinee, so we need to `both` it with the scrut demand
199
200         scrut_dmd                = Seq Drop Now [idNewDemandInfo b | b <- bndrs', isId b]
201                                    `both`
202                                    idNewDemandInfo case_bndr'
203
204         (scrut_ty, scrut')       = dmdAnal sigs scrut_dmd scrut
205     in
206     (alt_ty1 `bothType` scrut_ty, Case scrut' case_bndr' [alt'])
207
208 dmdAnal sigs dmd (Case scrut case_bndr alts)
209   = let
210         (alt_tys, alts')        = mapAndUnzip (dmdAnalAlt sigs dmd) alts
211         (scrut_ty, scrut')      = dmdAnal sigs Eval scrut
212         (alt_ty, case_bndr')    = annotateBndr (foldr1 lubType alt_tys) case_bndr
213     in
214 --    pprTrace "dmdAnal:Case" (ppr alts $$ ppr alt_tys)
215     (alt_ty `bothType` scrut_ty, Case scrut' case_bndr' alts')
216
217 dmdAnal sigs dmd (Let (NonRec id rhs) body) 
218   = let
219         (sigs', lazy_fv, (id1, rhs')) = downRhs NotTopLevel sigs (id, rhs)
220         (body_ty, body')              = dmdAnal sigs' dmd body
221         (body_ty1, id2)               = annotateBndr body_ty id1
222         body_ty2                      = addLazyFVs body_ty1 lazy_fv
223     in
224 --    pprTrace "dmdLet" (ppr id <+> ppr (sig,rhs_env))
225     (body_ty2, Let (NonRec id2 rhs') body')    
226
227 dmdAnal sigs dmd (Let (Rec pairs) body) 
228   = let
229         bndrs                    = map fst pairs
230         (sigs', lazy_fv, pairs') = dmdFix NotTopLevel sigs pairs
231         (body_ty, body')         = dmdAnal sigs' dmd body
232         body_ty1                 = addLazyFVs body_ty lazy_fv
233     in
234     sigs' `seq` body_ty `seq`
235     let
236         (body_ty2, _) = annotateBndrs body_ty1 bndrs
237                 -- Don't bother to add demand info to recursive
238                 -- binders as annotateBndr does; 
239                 -- being recursive, we can't treat them strictly.
240                 -- But we do need to remove the binders from the result demand env
241     in
242     (body_ty2,  Let (Rec pairs') body')
243
244
245 dmdAnalAlt sigs dmd (con,bndrs,rhs) 
246   = let 
247         (rhs_ty, rhs')   = dmdAnal sigs dmd rhs
248         (alt_ty, bndrs') = annotateBndrs rhs_ty bndrs
249     in
250     (alt_ty, (con, bndrs', rhs'))
251 \end{code}
252
253 %************************************************************************
254 %*                                                                      *
255 \subsection{Bindings}
256 %*                                                                      *
257 %************************************************************************
258
259 \begin{code}
260 dmdFix :: TopLevelFlag
261        -> SigEnv                -- Does not include bindings for this binding
262        -> [(Id,CoreExpr)]
263        -> (SigEnv, DmdEnv,
264            [(Id,CoreExpr)])     -- Binders annotated with stricness info
265
266 dmdFix top_lvl sigs pairs
267   = loop 1 initial_sigs pairs
268   where
269     bndrs        = map fst pairs
270     initial_sigs = extendSigEnvList sigs [(id, (initial_sig id, top_lvl)) | id <- bndrs]
271     
272     loop :: Int
273          -> SigEnv                      -- Already contains the current sigs
274          -> [(Id,CoreExpr)]             
275          -> (SigEnv, DmdEnv, [(Id,CoreExpr)])
276     loop n sigs pairs
277       | all (same_sig sigs sigs') bndrs = (sigs', lazy_fv, pairs')
278                 -- Note: use pairs', not pairs.   pairs' is the result of 
279                 -- processing the RHSs with sigs (= sigs'), whereas pairs 
280                 -- is the result of processing the RHSs with the *previous* 
281                 -- iteration of sigs.
282       | n >= 5              = pprTrace "dmdFix" (ppr n <+> (vcat 
283                                 [ text "Sigs:" <+> ppr [(id,lookup sigs id, lookup sigs' id) | (id,_) <- pairs],
284                                   text "env:" <+> ppr (ufmToList sigs),
285                                   text "binds:" <+> ppr pairs]))
286                               (loop (n+1) sigs' pairs')
287       | otherwise           = {- pprTrace "dmdFixLoop" (ppr id_sigs) -} (loop (n+1) sigs' pairs')
288       where
289                 -- Use the new signature to do the next pair
290                 -- The occurrence analyser has arranged them in a good order
291                 -- so this can significantly reduce the number of iterations needed
292         ((sigs',lazy_fv), pairs') = mapAccumL (my_downRhs top_lvl) (sigs, emptyDmdEnv) pairs
293         
294     my_downRhs top_lvl (sigs,lazy_fv) (id,rhs)
295         = -- pprTrace "downRhs {" (ppr id <+> (ppr old_sig))
296           -- (new_sig `seq` 
297           --    pprTrace "downRhsEnd" (ppr id <+> ppr new_sig <+> char '}' ) 
298           ((sigs', lazy_fv'), pair')
299           --     )
300         where
301           (sigs', lazy_fv1, pair') = downRhs top_lvl sigs (id,rhs)
302           lazy_fv'                 = plusUFM_C both lazy_fv lazy_fv1   
303           -- old_sig               = lookup sigs id
304           -- new_sig               = lookup sigs' id
305            
306         -- Get an initial strictness signature from the Id
307         -- itself.  That way we make use of earlier iterations
308         -- of the fixpoint algorithm.  (Cunning plan.)
309         -- Note that the cunning plan extends to the DmdEnv too,
310         -- since it is part of the strictness signature
311     initial_sig id = idNewStrictness_maybe id `orElse` botSig
312
313     same_sig sigs sigs' var = lookup sigs var == lookup sigs' var
314     lookup sigs var = case lookupVarEnv sigs var of
315                         Just (sig,_) -> sig
316
317 downRhs :: TopLevelFlag 
318         -> SigEnv -> (Id, CoreExpr)
319         -> (SigEnv,  DmdEnv, (Id, CoreExpr))
320 -- Process the RHS of the binding, add the strictness signature
321 -- to the Id, and augment the environment with the signature as well.
322
323 downRhs top_lvl sigs (id, rhs)
324  = (sigs', lazy_fv, (id', rhs'))
325  where
326   arity             = exprArity rhs   -- The idArity may not be up to date
327   (rhs_ty, rhs')    = dmdAnal sigs (vanillaCall arity) rhs
328   (lazy_fv, sig_ty) = mkSigTy id arity rhs rhs_ty
329   id'               = id `setIdNewStrictness` sig_ty
330   sigs'             = extendSigEnv top_lvl sigs id sig_ty
331 \end{code}
332
333 %************************************************************************
334 %*                                                                      *
335 \subsection{Strictness signatures and types}
336 %*                                                                      *
337 %************************************************************************
338
339 \begin{code}
340 mkSigTy :: Id -> Arity -> CoreExpr -> DmdType -> (DmdEnv, StrictSig)
341 -- Take a DmdType and turn it into a StrictSig
342 mkSigTy id arity rhs (DmdType fv dmds res) 
343   = (lazy_fv, mkStrictSig id arity dmd_ty)
344   where
345     dmd_ty = DmdType strict_fv final_dmds res'
346
347     lazy_fv   = filterUFM (not . isStrictDmd) fv
348     strict_fv = filterUFM isStrictDmd         fv
349         -- We put the strict FVs in the DmdType of the Id, so 
350         -- that at its call sites we unleash demands on its strict fvs.
351         -- An example is 'roll' in imaginary/wheel-sieve2
352         -- Something like this:
353         --      roll x = letrec 
354         --                   go y = if ... then roll (x-1) else x+1
355         --               in 
356         --               go ms
357         -- We want to see that roll is strict in x, which is because
358         -- go is called.   So we put the DmdEnv for x in go's DmdType.
359         --
360         -- Another example:
361         --      f :: Int -> Int -> Int
362         --      f x y = let t = x+1
363         --          h z = if z==0 then t else 
364         --                if z==1 then x+1 else
365         --                x + h (z-1)
366         --      in
367         --      h y
368         -- Calling h does indeed evaluate x, but we can only see
369         -- that if we unleash a demand on x at the call site for t.
370         --
371         -- Incidentally, here's a place where lambda-lifting h would
372         -- lose the cigar --- we couldn't see the joint strictness in t/x
373         --
374         --      ON THE OTHER HAND
375         -- We don't want to put *all* the fv's from the RHS into the
376         -- DmdType, because that makes fixpointing very slow --- the 
377         -- DmdType gets full of lazy demands that are slow to converge.
378
379     lazified_dmds = map lazify dmds
380         -- Get rid of defers in the arguments
381     final_dmds = setUnpackStrategy lazified_dmds
382         -- Set the unpacking strategy
383         
384     res' = case (dmds, res) of
385                 ([], RetCPR) | not (exprIsValue rhs) -> TopRes
386                 other                                -> res
387         -- If the rhs is a thunk, we forget the CPR info, because
388         -- it is presumably shared (else it would have been inlined, and 
389         -- so we'd lose sharing if w/w'd it into a function.
390         --
391         --      DONE IN OLD CPR ANALYSER, BUT NOT YET HERE
392         -- Also, if the strictness analyser has figured out that it's strict,
393         -- the let-to-case transformation will happen, so again it's good.
394         -- (CPR analysis runs before the simplifier has had a chance to do
395         --  the let-to-case transform.)
396         -- This made a big difference to PrelBase.modInt, which had something like
397         --      modInt = \ x -> let r = ... -> I# v in
398         --                      ...body strict in r...
399         -- r's RHS isn't a value yet; but modInt returns r in various branches, so
400         -- if r doesn't have the CPR property then neither does modInt
401 \end{code}
402
403 The unpack strategy determines whether we'll *really* unpack the argument,
404 or whether we'll just remember its strictness.  If unpacking would give
405 rise to a *lot* of worker args, we may decide not to unpack after all.
406
407 \begin{code}
408 setUnpackStrategy :: [Demand] -> [Demand]
409 setUnpackStrategy ds
410   = snd (go (opt_MaxWorkerArgs - nonAbsentArgs ds) ds)
411   where
412     go :: Int                   -- Max number of args available for sub-components of [Demand]
413        -> [Demand]
414        -> (Int, [Demand])       -- Args remaining after subcomponents of [Demand] are unpacked
415
416     go n (Seq keep _ cs : ds) 
417         | n' >= 0    = Seq keep Now cs' `cons` go n'' ds
418         | otherwise  = Eval `cons` go n ds
419         where
420           (n'',cs') = go n' cs
421           n' = n + box - non_abs_args
422           box = case keep of
423                    Keep -> 0
424                    Drop -> 1    -- Add one to the budget if we drop the top-level arg
425           non_abs_args = nonAbsentArgs cs
426                 -- Delete # of non-absent args to which we'll now be committed
427                                 
428     go n (d:ds) = d `cons` go n ds
429     go n []     = (n,[])
430
431     cons d (n,ds) = (n, d:ds)
432
433 nonAbsentArgs :: [Demand] -> Int
434 nonAbsentArgs []         = 0
435 nonAbsentArgs (Abs : ds) = nonAbsentArgs ds
436 nonAbsentArgs (d   : ds) = 1 + nonAbsentArgs ds
437 \end{code}
438
439
440 %************************************************************************
441 %*                                                                      *
442 \subsection{Strictness signatures and types}
443 %*                                                                      *
444 %************************************************************************
445
446 \begin{code}
447 splitDmdTy :: DmdType -> (Demand, DmdType)
448 -- Split off one function argument
449 splitDmdTy (DmdType fv (dmd:dmds) res_ty) = (dmd, DmdType fv dmds res_ty)
450 splitDmdTy ty@(DmdType fv [] TopRes)         = (topDmd, ty)
451 splitDmdTy ty@(DmdType fv [] BotRes)         = (Abs,    ty)
452         -- We already have a suitable demand on all
453         -- free vars, so no need to add more!
454 splitDmdTy (DmdType fv [] RetCPR)         = panic "splitDmdTy"
455 \end{code}
456
457 \begin{code}
458 unitVarDmd var dmd = DmdType (unitVarEnv var dmd) [] TopRes
459
460 addVarDmd top_lvl dmd_ty@(DmdType fv ds res) var dmd
461   | isTopLevel top_lvl = dmd_ty         -- Don't record top level things
462   | otherwise          = DmdType (extendVarEnv fv var dmd) ds res
463
464 addLazyFVs (DmdType fv ds res) lazy_fvs
465   = DmdType (plusUFM_C both fv lazy_fvs) ds res
466
467 annotateBndr :: DmdType -> Var -> (DmdType, Var)
468 -- The returned env has the var deleted
469 -- The returned var is annotated with demand info
470 -- No effect on the argument demands
471 annotateBndr dmd_ty@(DmdType fv ds res) var
472   | isTyVar var = (dmd_ty, var)
473   | otherwise   = (DmdType fv' ds res, setIdNewDemandInfo var dmd)
474   where
475     (fv', dmd) = removeFV fv var res
476
477 annotateBndrs = mapAccumR annotateBndr
478
479 annotateLamIdBndr dmd_ty@(DmdType fv ds res) id
480 -- For lambdas we add the demand to the argument demands
481 -- Only called for Ids
482   = ASSERT( isId id )
483     (DmdType fv' (hacked_dmd:ds) res, setIdNewDemandInfo id hacked_dmd)
484   where
485     (fv', dmd) = removeFV fv id res
486     hacked_dmd = case dmd of
487                     Bot   -> Abs
488                     Err   -> Abs
489                     other -> dmd
490         -- This gross hack is needed because otherwise we label
491         -- a lambda binder with demand 'B'.  But in terms of calling
492         -- conventions that's Abs, because we don't pass it.  But
493         -- when we do a w/w split we get
494         --      fw x = (\x y:B -> ...) x (error "oops")
495         -- And then the simplifier things the 'B' is a strict demand
496         -- and evaluates the (error "oops").  Sigh
497
498 removeFV fv var res = (fv', dmd)
499                 where
500                   fv' = fv `delVarEnv` var
501                   dmd = lookupVarEnv fv var `orElse` deflt
502                   deflt | isBotRes res = Bot
503                         | otherwise    = Abs
504 \end{code}
505
506 %************************************************************************
507 %*                                                                      *
508 \subsection{Strictness signatures}
509 %*                                                                      *
510 %************************************************************************
511
512 \begin{code}
513 type SigEnv  = VarEnv (StrictSig, TopLevelFlag)
514         -- We use the SigEnv to tell us whether to
515         -- record info about a variable in the DmdEnv
516         -- We do so if it's a LocalId, but not top-level
517         --
518         -- The DmdEnv gives the demand on the free vars of the function
519         -- when it is given enough args to satisfy the strictness signature
520
521 emptySigEnv  = emptyVarEnv
522
523 extendSigEnv :: TopLevelFlag -> SigEnv -> Id -> StrictSig -> SigEnv
524 extendSigEnv top_lvl env var sig = extendVarEnv env var (sig, top_lvl)
525
526 extendSigEnvList = extendVarEnvList
527
528 dmdTransform :: SigEnv          -- The strictness environment
529              -> Id              -- The function
530              -> Demand          -- The demand on the function
531              -> DmdType         -- The demand type of the function in this context
532         -- Returned DmdEnv includes the demand on 
533         -- this function plus demand on its free variables
534
535 dmdTransform sigs var dmd
536
537 ------  DATA CONSTRUCTOR
538   | isDataConId var,            -- Data constructor
539     Seq k Now ds <- res_dmd,    -- and the demand looks inside its fields
540     let StrictSig dmd_ty = idNewStrictness var, -- It must have a strictness sig
541     let DmdType _ con_ds con_res = dmd_ty
542   = if length con_ds == length ds then  -- Saturated, so unleash the demand
543         -- ds can be empty, when we are just seq'ing the thing
544         let 
545            arg_ds = case k of
546                         Keep -> zipWith lub ds con_ds
547                         Drop -> ds
548                 -- Important!  If we Keep the constructor application, then
549                 -- we need the demands the constructor places (usually lazy)
550                 -- If not, we don't need to.  For example:
551                 --      f p@(x,y) = (p,y)       -- S(AL)
552                 --      g a b     = f (a,b)
553                 -- It's vital that we don't calculate Absent for a!
554         in
555         mkDmdType emptyDmdEnv arg_ds con_res
556                 -- Must remember whether it's a product, hence con_res, not TopRes
557     else
558         topDmdType
559
560 ------  IMPORTED FUNCTION
561   | isGlobalId var,             -- Imported function
562     let StrictSig dmd_ty = getNewStrictness var
563   = if dmdTypeDepth dmd_ty <= call_depth then   -- Saturated, so unleash the demand
564         dmd_ty
565     else
566         topDmdType
567
568 ------  LOCAL LET/REC BOUND THING
569   | Just (StrictSig dmd_ty, top_lvl) <- lookupVarEnv sigs var
570   = let
571         fn_ty | dmdTypeDepth dmd_ty <= call_depth = dmd_ty 
572               | otherwise                         = deferType dmd_ty
573         -- NB: it's important to use deferType, and not just return topDmdType
574         -- Consider     let { f x y = p + x } in f 1
575         -- The application isn't saturated, but we must nevertheless propagate 
576         --      a lazy demand for p!  
577     in
578     addVarDmd top_lvl fn_ty var dmd
579
580 ------  LOCAL NON-LET/REC BOUND THING
581   | otherwise                   -- Default case
582   = unitVarDmd var dmd
583
584   where
585     (call_depth, res_dmd) = splitCallDmd dmd
586 \end{code}
587
588
589 %************************************************************************
590 %*                                                                      *
591 \subsection{Demands}
592 %*                                                                      *
593 %************************************************************************
594
595 \begin{code}
596 splitCallDmd :: Demand -> (Int, Demand)
597 splitCallDmd (Call d) = case splitCallDmd d of
598                           (n, r) -> (n+1, r)
599 splitCallDmd d        = (0, d)
600
601 vanillaCall :: Arity -> Demand
602 vanillaCall 0 = Eval
603 vanillaCall n = Call (vanillaCall (n-1))
604
605 deferType :: DmdType -> DmdType
606 deferType (DmdType fv _ _) = DmdType (mapVarEnv defer fv) [] TopRes
607         -- Notice that we throw away info about both arguments and results
608         -- For example,   f = let ... in \x -> x
609         -- We don't want to get a stricness type V->T for f.
610
611 defer :: Demand -> Demand
612 defer = lub Abs
613
614 lazify :: Demand -> Demand
615 -- The 'Defer' demands are just Lazy at function boundaries
616 -- Ugly!  Ask John how to improve it.
617 lazify (Seq k Defer ds) = Lazy
618 lazify (Seq k Now   ds) = Seq k Now (map lazify ds)
619 lazify Bot              = Abs   -- Don't pass args that are consumed by bottom/err
620 lazify Err              = Abs
621 lazify d                = d
622 \end{code}
623
624 \begin{code}
625 betterStrictness :: StrictSig -> StrictSig -> Bool
626 betterStrictness (StrictSig t1) (StrictSig t2) = betterDmdType t1 t2
627
628 betterDmdType t1 t2 = (t1 `lubType` t2) == t2
629
630 betterDemand :: Demand -> Demand -> Bool
631 -- If d1 `better` d2, and d2 `better` d2, then d1==d2
632 betterDemand d1 d2 = (d1 `lub` d2) == d2
633
634 squashDmdEnv (StrictSig (DmdType fv ds res)) = StrictSig (DmdType emptyDmdEnv ds res)
635 \end{code}
636
637
638 %************************************************************************
639 %*                                                                      *
640 \subsection{LUB and BOTH}
641 %*                                                                      *
642 %************************************************************************
643
644 \begin{code}
645 lub :: Demand -> Demand -> Demand
646
647 lub Bot  d = d
648
649 lub Lazy d = Lazy
650
651 lub Err Bot = Err 
652 lub Err d   = d 
653
654 lub Abs Bot          = Abs      -- E.g f x y = if ... then x else error x
655                                 -- Then for y we get Abs `lub` Bot, and we really
656                                 -- want Abs overall
657 lub Abs Err          = Abs
658 lub Abs Abs          = Abs    
659 lub Abs (Seq k _ ds) = Seq k Defer ds   -- Very important ('radicals' example)
660 lub Abs d            = Lazy
661
662 lub Eval Abs              = Lazy
663 lub Eval Lazy             = Lazy
664
665 lub Eval (Seq k Now  ds) = Eval         -- Urk!  Is this monotonic?
666         -- Was (incorrectly): 
667         --      lub Eval (Seq k Now ds) = Seq Keep Now ds
668         -- Incorrect because 
669         --      Eval `lub` U(VV) is not S(VV)
670         -- (because the components aren't necessarily evaluated)
671         --
672         -- Was (correctly, but pessimistically): 
673         --      lub Eval (Seq k Now ds) = Eval
674         -- Pessimistic because
675         --      f n []     = n
676         --      f n (x:xs) = f (n+x) xs
677         -- Here we want to do better than just V for n.  It's
678         -- unboxed in the (x:xs) case, and we might be prepared to
679         -- rebox it in the [] case.
680         -- To achieve this we could perhaps consider Eval to be equivalent to
681         --      U(L), or S(A)
682
683 lub Eval (Seq k Defer ds) = Lazy
684 lub Eval d                = Eval
685
686 lub (Call d1) (Call d2) = Call (lub d1 d2)
687
688 lub (Seq k1 l1 ds1) (Seq k2 l2 ds2) = Seq (k1 `vee` k2) (l1 `or_defer` l2) (lubs ds1 ds2)
689
690 -- The last clauses deal with the remaining cases for Call and Seq
691 lub d1@(Call _) d2@(Seq _ _ _) = pprPanic "lub" (ppr d1 $$ ppr d2)
692 lub d1 d2                      = lub d2 d1
693
694 -- A Seq can have an empty list of demands, in the polymorphic case.
695 lubs [] ds2 = ds2
696 lubs ds1 [] = ds1
697 lubs ds1 ds2 = ASSERT( length ds1 == length ds2 ) zipWith lub ds1 ds2
698
699 or_defer Now Now = Now
700 or_defer _   _   = Defer
701
702 -------------------------
703 -- Consider (if x then y else []) with demand V
704 -- Then the first branch gives {y->V} and the second
705 -- *implicitly* has {y->A}.  So we must put {y->(V `lub` A)}
706 -- in the result env.
707 lubType (DmdType fv1 ds1 r1) (DmdType fv2 ds2 r2)
708   = DmdType lub_fv2 (zipWith lub ds1 ds2) (r1 `lubRes` r2)
709   where
710     lub_fv  = plusUFM_C lub fv1 fv2
711     lub_fv1 = modifyEnv (not (isBotRes r1)) (Abs `lub`) fv2 fv1 lub_fv
712     lub_fv2 = modifyEnv (not (isBotRes r2)) (Abs `lub`) fv1 fv2 lub_fv1
713         -- lub is the identity for Bot
714
715 -------------------------
716 lubRes BotRes r      = r
717 lubRes r      BotRes = r
718 lubRes RetCPR RetCPR = RetCPR
719 lubRes r1     r2     = TopRes
720
721 -----------------------------------
722 vee :: Keepity -> Keepity -> Keepity
723 vee Drop Drop = Drop
724 vee k1   k2   = Keep
725
726 -----------------------------------
727 both :: Demand -> Demand -> Demand
728
729 -- The normal one
730 -- both Bot d = Bot
731
732 -- The experimental one
733 -- The idea is that (error x) places on x
734 --      both demand Bot (like on all free vars)
735 --      and demand Eval (for the arg to error)
736 -- and we want the result to be Eval.
737 both Bot Bot = Bot
738 both Bot Abs = Bot
739 both Bot d   = d
740
741 both Abs d   = d
742
743 both Err Bot = Err
744 both Err Abs = Err
745 both Err d   = d
746
747 both Lazy Bot          = Lazy
748 both Lazy Abs          = Lazy
749 both Lazy Err          = Lazy 
750 both Lazy (Seq k l ds) = Seq Keep l ds
751 both Lazy d            = d
752   -- Notice that the Seq case ensures that we have the
753   -- boxed value.  The equation originally said
754   --    both (Seq k Now ds) = Seq Keep Now ds
755   -- but it's important that the Keep is switched on even
756   -- for a deferred demand.  Otherwise a (Seq Drop Now [])
757   -- might both'd with the result, and then we won't pass
758   -- the boxed value.  Here's an example:
759   --    (x-1) `seq` (x+1, x)
760   -- From the (x+1, x) we get (U*(V) `both` L), which must give S*(V)
761   -- From (x-1) we get U(V). Combining, we must get S(V).
762   -- If we got U*(V) from the pair, we'd end up with U(V), and that
763   -- can be a disaster if a component of the data structure is absent.
764   -- [Disaster = enter an absent argument.]
765
766 both Eval (Seq k l ds) = Seq Keep Now ds
767 both Eval (Call d)     = Call d
768 both Eval d            = Eval
769
770 both (Seq k1 Defer ds1) (Seq k2 Defer ds2) = Seq (k1 `vee` k2) Defer (boths ds1  ds2)
771 both (Seq k1 l1 ds1)    (Seq k2 l2 ds2)    = Seq (k1 `vee` k2) Now   (boths ds1' ds2')
772                                            where
773                                              ds1' = case l1 of { Now -> ds1; Defer -> map defer ds1 }
774                                              ds2' = case l2 of { Now -> ds2; Defer -> map defer ds2 }
775
776 both (Call d1) (Call d2) = Call (d1 `both` d2)
777
778 -- The last clauses deal with the remaining cases for Call and Seq
779 both d1@(Call _) d2@(Seq _ _ _) = pprPanic "both" (ppr d1 $$ ppr d2)
780 both d1 d2                      = both d2 d1
781
782 -----------------------------------
783 -- A Seq can have an empty list of demands, in the polymorphic case.
784 boths [] ds2  = ds2
785 boths ds1 []  = ds1
786 boths ds1 ds2 = ASSERT( length ds1 == length ds2 ) zipWith both ds1 ds2
787
788 -----------------------------------
789 -- (t1 `bothType` t2) takes the argument/result info from t1,
790 -- using t2 just for its free-var info
791 bothType (DmdType fv1 ds1 r1) (DmdType fv2 ds2 r2)
792   = DmdType both_fv2 ds1 r1
793   where
794     both_fv  = plusUFM_C both fv1 fv2
795     both_fv1 = modifyEnv (isBotRes r1) (`both` Bot) fv2 fv1 both_fv
796     both_fv2 = modifyEnv (isBotRes r2) (`both` Bot) fv1 fv2 both_fv1
797         -- both is the identity for Abs
798 \end{code}
799
800 \begin{code}
801 modifyEnv :: Bool                       -- No-op if False
802           -> (Demand -> Demand)         -- The zapper
803           -> DmdEnv -> DmdEnv           -- Env1 and Env2
804           -> DmdEnv -> DmdEnv           -- Transform this env
805         -- Zap anything in Env1 but not in Env2
806         -- Assume: dom(env) includes dom(Env1) and dom(Env2)
807
808 modifyEnv need_to_modify zapper env1 env2 env
809   | need_to_modify = foldr zap env (keysUFM (env1 `minusUFM` env2))
810   | otherwise      = env
811   where
812     zap uniq env = addToUFM_Directly env uniq (zapper current_val)
813                  where
814                    current_val = expectJust "modifyEnv" (lookupUFM_Directly env uniq)
815 \end{code}
816
817
818 %************************************************************************
819 %*                                                                      *
820 \subsection{Miscellaneous
821 %*                                                                      *
822 %************************************************************************
823
824
825 \begin{code}
826 get_changes binds = vcat (map get_changes_bind binds)
827
828 get_changes_bind (Rec pairs) = vcat (map get_changes_pr pairs)
829 get_changes_bind (NonRec id rhs) = get_changes_pr (id,rhs)
830
831 get_changes_pr (id,rhs) 
832   | isImplicitId id = empty  -- We don't look inside these
833   | otherwise       = get_changes_var id $$ get_changes_expr rhs
834
835 get_changes_var var
836   | isId var  = get_changes_str var $$ get_changes_dmd var
837   | otherwise = empty
838
839 get_changes_expr (Type t)     = empty
840 get_changes_expr (Var v)      = empty
841 get_changes_expr (Lit l)      = empty
842 get_changes_expr (Note n e)   = get_changes_expr e
843 get_changes_expr (App e1 e2)  = get_changes_expr e1 $$ get_changes_expr e2
844 get_changes_expr (Lam b e)    = {- get_changes_var b $$ -} get_changes_expr e
845 get_changes_expr (Let b e)    = get_changes_bind b $$ get_changes_expr e
846 get_changes_expr (Case e b a) = get_changes_expr e $$ {- get_changes_var b $$ -} vcat (map get_changes_alt a)
847
848 get_changes_alt (con,bs,rhs) = {- vcat (map get_changes_var bs) $$ -} get_changes_expr rhs
849
850 get_changes_str id
851   | new_better && old_better = empty
852   | new_better               = message "BETTER"
853   | old_better               = message "WORSE"
854   | otherwise                = message "INCOMPARABLE" 
855   where
856     message word = text word <+> text "strictness for" <+> ppr id <+> info
857     info = (text "Old" <+> ppr old) $$ (text "New" <+> ppr new)
858     new = squashDmdEnv (idNewStrictness id)     -- Don't report diffs in the env
859     old = newStrictnessFromOld id
860     old_better = old `betterStrictness` new
861     new_better = new `betterStrictness` old
862
863 get_changes_dmd id
864   | isUnLiftedType (idType id) = empty  -- Not useful
865   | new_better && old_better = empty
866   | new_better               = message "BETTER"
867   | old_better               = message "WORSE"
868   | otherwise                = message "INCOMPARABLE" 
869   where
870     message word = text word <+> text "demand for" <+> ppr id <+> info
871     info = (text "Old" <+> ppr old) $$ (text "New" <+> ppr new)
872     new = lazify (idNewDemandInfo id)   -- Lazify to avoid spurious improvements
873     old = newDemand (idDemandInfo id)
874     new_better = new `betterDemand` old 
875     old_better = old `betterDemand` new
876 \end{code}