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