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