[project @ 2004-09-30 10:35:15 by simonpj]
[ghc-hetmet.git] / ghc / compiler / simplCore / OccurAnal.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3 %
4 %************************************************************************
5 %*                                                                      *
6 \section[OccurAnal]{Occurrence analysis pass}
7 %*                                                                      *
8 %************************************************************************
9
10 The occurrence analyser re-typechecks a core expression, returning a new
11 core expression with (hopefully) improved usage information.
12
13 \begin{code}
14 module OccurAnal (
15         occurAnalyseBinds, occurAnalyseGlobalExpr, occurAnalyseRule
16     ) where
17
18 #include "HsVersions.h"
19
20 import CoreSyn
21 import CoreFVs          ( idRuleVars )
22 import CoreUtils        ( exprIsTrivial )
23 import Id               ( isDataConWorkId, isOneShotBndr, setOneShotLambda, 
24                           idOccInfo, setIdOccInfo,
25                           isExportedId, modifyIdInfo, idInfo, idArity,
26                           idSpecialisation, isLocalId,
27                           idType, idUnique, Id
28                         )
29 import IdInfo           ( copyIdInfo )
30 import BasicTypes       ( OccInfo(..), isOneOcc )
31
32 import VarSet
33 import VarEnv
34
35 import Type             ( isFunTy, dropForAlls )
36 import Maybes           ( orElse )
37 import Digraph          ( stronglyConnCompR, SCC(..) )
38 import PrelNames        ( buildIdKey, foldrIdKey, runSTRepIdKey, augmentIdKey )
39 import Unique           ( Unique )
40 import UniqFM           ( keysUFM )  
41 import Util             ( zipWithEqual, mapAndUnzip )
42 import Outputable
43 \end{code}
44
45
46 %************************************************************************
47 %*                                                                      *
48 \subsection[OccurAnal-main]{Counting occurrences: main function}
49 %*                                                                      *
50 %************************************************************************
51
52 Here's the externally-callable interface:
53
54 \begin{code}
55 occurAnalyseGlobalExpr :: CoreExpr -> CoreExpr
56 occurAnalyseGlobalExpr expr
57   =     -- Top level expr, so no interesting free vars, and
58         -- discard occurence info returned
59     snd (occAnal (initOccEnv emptyVarSet) expr)
60
61 occurAnalyseRule :: CoreRule -> CoreRule
62 occurAnalyseRule rule@(BuiltinRule _ _) = rule
63 occurAnalyseRule (Rule str act tpl_vars tpl_args rhs)
64                 -- Add occ info to tpl_vars, rhs
65   = Rule str act tpl_vars' tpl_args rhs'
66   where
67     (rhs_uds, rhs') = occAnal (initOccEnv (mkVarSet tpl_vars)) rhs
68     (_, tpl_vars')  = tagBinders rhs_uds tpl_vars
69 \end{code}
70
71
72 %************************************************************************
73 %*                                                                      *
74 \subsection{Top level stuff}
75 %*                                                                      *
76 %************************************************************************
77
78 In @occAnalTop@ we do indirection-shorting.  That is, if we have this:
79
80         x_local = <expression>
81         ...
82         x_exported = loc
83
84 where exp is exported, and loc is not, then we replace it with this:
85
86         x_local = x_exported
87         x_exported = <expression>
88         ...
89
90 Without this we never get rid of the x_exported = x_local thing.  This
91 save a gratuitous jump (from \tr{x_exported} to \tr{x_local}), and
92 makes strictness information propagate better.  This used to happen in
93 the final phase, but it's tidier to do it here.
94
95 If more than one exported thing is equal to a local thing (i.e., the
96 local thing really is shared), then we do one only:
97 \begin{verbatim}
98         x_local = ....
99         x_exported1 = x_local
100         x_exported2 = x_local
101 ==>
102         x_exported1 = ....
103
104         x_exported2 = x_exported1
105 \end{verbatim}
106
107 We rely on prior eta reduction to simplify things like
108 \begin{verbatim}
109         x_exported = /\ tyvars -> x_local tyvars
110 ==>
111         x_exported = x_local
112 \end{verbatim}
113 Hence,there's a possibility of leaving unchanged something like this:
114 \begin{verbatim}
115         x_local = ....
116         x_exported1 = x_local Int
117 \end{verbatim}
118 By the time we've thrown away the types in STG land this 
119 could be eliminated.  But I don't think it's very common
120 and it's dangerous to do this fiddling in STG land 
121 because we might elminate a binding that's mentioned in the
122 unfolding for something.
123
124 \begin{code}
125 occurAnalyseBinds :: [CoreBind] -> [CoreBind]
126
127 occurAnalyseBinds binds
128   = binds'
129   where
130     (_, _, binds') = go (initOccEnv emptyVarSet) binds
131
132     go :: OccEnv -> [CoreBind]
133        -> (UsageDetails,        -- Occurrence info
134            IdEnv Id,            -- Indirection elimination info
135                                 --   Maps local-id -> exported-id, but it embodies
136                                 --   bindings of the form exported-id = local-id in
137                                 --   the argument to go
138            [CoreBind])          -- Occ-analysed bindings, less the exported-id=local-id ones
139
140     go env [] = (emptyDetails, emptyVarEnv, [])
141
142     go env (bind : binds)
143       = let
144             new_env                        = env `addNewCands` (bindersOf bind)
145             (scope_usage, ind_env, binds') = go new_env binds
146             (final_usage, new_binds)       = occAnalBind env (zapBind ind_env bind) scope_usage
147                                                 -- NB: I zap before occur-analysing, so
148                                                 -- I don't need to worry about getting the
149                                                 -- occ info on the new bindings right.
150         in
151         case bind of
152             NonRec exported_id (Var local_id) 
153                 | shortMeOut ind_env exported_id local_id
154                 -- Special case for eliminating indirections
155                 --   Note: it's a shortcoming that this only works for
156                 --         non-recursive bindings.  Elminating indirections
157                 --         makes perfect sense for recursive bindings too, but
158                 --         it's more complicated to implement, so I haven't done so
159                 -> (scope_usage, ind_env', binds')
160                 where
161                    ind_env' = extendVarEnv ind_env local_id exported_id
162
163             other ->    -- Ho ho! The normal case
164                      (final_usage, ind_env, new_binds ++ binds')
165                    
166
167 -- Deal with any indirections
168 zapBind ind_env (NonRec bndr rhs) 
169   | bndr `elemVarEnv` ind_env                      = Rec (zap ind_env (bndr,rhs))
170                 -- The Rec isn't strictly necessary, but it's convenient
171 zapBind ind_env (Rec pairs)
172   | or [id `elemVarEnv` ind_env | (id,_) <- pairs] = Rec (concat (map (zap ind_env) pairs))
173
174 zapBind ind_env bind = bind
175
176 zap ind_env pair@(local_id,rhs)
177   = case lookupVarEnv ind_env local_id of
178         Nothing          -> [pair]
179         Just exported_id -> [(local_id, Var exported_id),
180                              (exported_id', rhs)]
181                          where
182                             exported_id' = modifyIdInfo (copyIdInfo (idInfo local_id)) exported_id
183                         
184 shortMeOut ind_env exported_id local_id
185 -- The if-then-else stuff is just so I can get a pprTrace to see
186 -- how often I don't get shorting out becuase of IdInfo stuff
187   = if isExportedId exported_id &&              -- Only if this is exported
188
189        isLocalId local_id &&                    -- Only if this one is defined in this
190                                                 --      module, so that we *can* change its
191                                                 --      binding to be the exported thing!
192
193        not (isExportedId local_id) &&           -- Only if this one is not itself exported,
194                                                 --      since the transformation will nuke it
195    
196        not (local_id `elemVarEnv` ind_env)      -- Only if not already substituted for
197     then
198         True
199
200 {- No longer needed
201         if shortableIdInfo (idInfo exported_id)         -- Only if its IdInfo is 'shortable'
202                                                         -- (see the defn of IdInfo.shortableIdInfo)
203         then True
204         else 
205 #ifdef DEBUG 
206           pprTrace "shortMeOut:" (ppr exported_id)
207 #endif
208                                                 False
209 -}
210     else
211         False
212 \end{code}
213
214
215 %************************************************************************
216 %*                                                                      *
217 \subsection[OccurAnal-main]{Counting occurrences: main function}
218 %*                                                                      *
219 %************************************************************************
220
221 Bindings
222 ~~~~~~~~
223
224 \begin{code}
225 type IdWithOccInfo = Id                 -- An Id with fresh PragmaInfo attached
226
227 type Node details = (details, Unique, [Unique]) -- The Ints are gotten from the Unique,
228                                                 -- which is gotten from the Id.
229 type Details1     = (Id, UsageDetails, CoreExpr)
230 type Details2     = (IdWithOccInfo, CoreExpr)
231
232
233 occAnalBind :: OccEnv
234             -> CoreBind
235             -> UsageDetails             -- Usage details of scope
236             -> (UsageDetails,           -- Of the whole let(rec)
237                 [CoreBind])
238
239 occAnalBind env (NonRec binder rhs) body_usage
240   | not (binder `usedIn` body_usage)            -- It's not mentioned
241   = (body_usage, [])
242
243   | otherwise                   -- It's mentioned in the body
244   = (final_body_usage `combineUsageDetails` rhs_usage,
245      [NonRec tagged_binder rhs'])
246
247   where
248     (final_body_usage, tagged_binder) = tagBinder body_usage binder
249     (rhs_usage, rhs')                 = occAnalRhs env tagged_binder rhs
250 \end{code}
251
252 Dropping dead code for recursive bindings is done in a very simple way:
253
254         the entire set of bindings is dropped if none of its binders are
255         mentioned in its body; otherwise none are.
256
257 This seems to miss an obvious improvement.
258 @
259         letrec  f = ...g...
260                 g = ...f...
261         in
262         ...g...
263
264 ===>
265
266         letrec f = ...g...
267                g = ...(...g...)...
268         in
269         ...g...
270 @
271
272 Now @f@ is unused. But dependency analysis will sort this out into a
273 @letrec@ for @g@ and a @let@ for @f@, and then @f@ will get dropped.
274 It isn't easy to do a perfect job in one blow.  Consider
275
276 @
277         letrec f = ...g...
278                g = ...h...
279                h = ...k...
280                k = ...m...
281                m = ...m...
282         in
283         ...m...
284 @
285
286
287 \begin{code}
288 occAnalBind env (Rec pairs) body_usage
289   = foldr (_scc_ "occAnalBind.dofinal" do_final_bind) (body_usage, []) sccs
290   where
291     binders = map fst pairs
292     rhs_env = env `addNewCands` binders
293
294     analysed_pairs :: [Details1]
295     analysed_pairs  = [ (bndr, rhs_usage, rhs')
296                       | (bndr, rhs) <- pairs,
297                         let (rhs_usage, rhs') = occAnalRhs rhs_env bndr rhs
298                       ]
299
300     sccs :: [SCC (Node Details1)]
301     sccs = _scc_ "occAnalBind.scc" stronglyConnCompR edges
302
303
304     ---- stuff for dependency analysis of binds -------------------------------
305     edges :: [Node Details1]
306     edges = _scc_ "occAnalBind.assoc"
307             [ (details, idUnique id, edges_from rhs_usage)
308             | details@(id, rhs_usage, rhs) <- analysed_pairs
309             ]
310
311         -- (a -> b) means a mentions b
312         -- Given the usage details (a UFM that gives occ info for each free var of
313         -- the RHS) we can get the list of free vars -- or rather their Int keys --
314         -- by just extracting the keys from the finite map.  Grimy, but fast.
315         -- Previously we had this:
316         --      [ bndr | bndr <- bndrs,
317         --               maybeToBool (lookupVarEnv rhs_usage bndr)]
318         -- which has n**2 cost, and this meant that edges_from alone 
319         -- consumed 10% of total runtime!
320     edges_from :: UsageDetails -> [Unique]
321     edges_from rhs_usage = _scc_ "occAnalBind.edges_from"
322                            keysUFM rhs_usage
323
324     ---- stuff to "re-constitute" bindings from dependency-analysis info ------
325
326         -- Non-recursive SCC
327     do_final_bind (AcyclicSCC ((bndr, rhs_usage, rhs'), _, _)) (body_usage, binds_so_far)
328       | not (bndr `usedIn` body_usage)
329       = (body_usage, binds_so_far)                      -- Dead code
330       | otherwise
331       = (combined_usage, new_bind : binds_so_far)       
332       where
333         total_usage                   = combineUsageDetails body_usage rhs_usage
334         (combined_usage, tagged_bndr) = tagBinder total_usage bndr
335         new_bind                      = NonRec tagged_bndr rhs'
336
337         -- Recursive SCC
338     do_final_bind (CyclicSCC cycle) (body_usage, binds_so_far)
339       | not (any (`usedIn` body_usage) bndrs)           -- NB: look at body_usage, not total_usage
340       = (body_usage, binds_so_far)                      -- Dead code
341       | otherwise
342       = (combined_usage, final_bind:binds_so_far)
343       where
344         details                        = [details   | (details, _, _) <- cycle]
345         bndrs                          = [bndr      | (bndr, _, _)      <- details]
346         rhs_usages                     = [rhs_usage | (_, rhs_usage, _) <- details]
347         total_usage                    = foldr combineUsageDetails body_usage rhs_usages
348         (combined_usage, tagged_bndrs) = tagBinders total_usage bndrs
349         final_bind                     = Rec (reOrderRec env new_cycle)
350
351         new_cycle = CyclicSCC (zipWithEqual "occAnalBind" mk_new_bind tagged_bndrs cycle)
352         mk_new_bind tagged_bndr ((_, _, rhs'), key, keys) = ((tagged_bndr, rhs'), key, keys)
353 \end{code}
354
355 @reOrderRec@ is applied to the list of (binder,rhs) pairs for a cyclic
356 strongly connected component (there's guaranteed to be a cycle).  It returns the
357 same pairs, but 
358         a) in a better order,
359         b) with some of the Ids having a IMustNotBeINLINEd pragma
360
361 The "no-inline" Ids are sufficient to break all cycles in the SCC.  This means
362 that the simplifier can guarantee not to loop provided it never records an inlining
363 for these no-inline guys.
364
365 Furthermore, the order of the binds is such that if we neglect dependencies
366 on the no-inline Ids then the binds are topologically sorted.  This means
367 that the simplifier will generally do a good job if it works from top bottom,
368 recording inlinings for any Ids which aren't marked as "no-inline" as it goes.
369
370 ==============
371 [June 98: I don't understand the following paragraphs, and I've 
372           changed the a=b case again so that it isn't a special case any more.]
373
374 Here's a case that bit me:
375
376         letrec
377                 a = b
378                 b = \x. BIG
379         in
380         ...a...a...a....
381
382 Re-ordering doesn't change the order of bindings, but there was no loop-breaker.
383
384 My solution was to make a=b bindings record b as Many, rather like INLINE bindings.
385 Perhaps something cleverer would suffice.
386 ===============
387
388 You might think that you can prevent non-termination simply by making
389 sure that we simplify a recursive binding's RHS in an environment that
390 simply clones the recursive Id.  But no.  Consider
391
392                 letrec f = \x -> let z = f x' in ...
393
394                 in
395                 let n = f y
396                 in
397                 case n of { ... }
398
399 We bind n to its *simplified* RHS, we then *re-simplify* it when
400 we inline n.  Then we may well inline f; and then the same thing
401 happens with z!
402
403 I don't think it's possible to prevent non-termination by environment
404 manipulation in this way.  Apart from anything else, successive
405 iterations of the simplifier may unroll recursive loops in cases like
406 that above.  The idea of beaking every recursive loop with an
407 IMustNotBeINLINEd pragma is much much better.
408
409
410 \begin{code}
411 reOrderRec
412         :: OccEnv
413         -> SCC (Node Details2)
414         -> [Details2]
415                         -- Sorted into a plausible order.  Enough of the Ids have
416                         --      dontINLINE pragmas that there are no loops left.
417
418         -- Non-recursive case
419 reOrderRec env (AcyclicSCC (bind, _, _)) = [bind]
420
421         -- Common case of simple self-recursion
422 reOrderRec env (CyclicSCC [bind])
423   = [(setIdOccInfo tagged_bndr IAmALoopBreaker, rhs)]
424   where
425     ((tagged_bndr, rhs), _, _) = bind
426
427 reOrderRec env (CyclicSCC (bind : binds))
428   =     -- Choose a loop breaker, mark it no-inline,
429         -- do SCC analysis on the rest, and recursively sort them out
430     concat (map (reOrderRec env) (stronglyConnCompR unchosen))
431     ++ 
432     [(setIdOccInfo tagged_bndr IAmALoopBreaker, rhs)]
433
434   where
435     (chosen_pair, unchosen) = choose_loop_breaker bind (score bind) [] binds
436     (tagged_bndr, rhs)      = chosen_pair
437
438         -- This loop looks for the bind with the lowest score
439         -- to pick as the loop  breaker.  The rest accumulate in 
440     choose_loop_breaker (details,_,_) loop_sc acc []
441         = (details, acc)        -- Done
442
443     choose_loop_breaker loop_bind loop_sc acc (bind : binds)
444         | sc < loop_sc  -- Lower score so pick this new one
445         = choose_loop_breaker bind sc (loop_bind : acc) binds
446
447         | otherwise     -- No lower so don't pick it
448         = choose_loop_breaker loop_bind loop_sc (bind : acc) binds
449         where
450           sc = score bind
451           
452     score :: Node Details2 -> Int       -- Higher score => less likely to be picked as loop breaker
453     score ((bndr, rhs), _, _)
454         | exprIsTrivial rhs        = 4  -- Practically certain to be inlined
455                 -- Used to have also: && not (isExportedId bndr)
456                 -- But I found this sometimes cost an extra iteration when we have
457                 --      rec { d = (a,b); a = ...df...; b = ...df...; df = d }
458                 -- where df is the exported dictionary. Then df makes a really
459                 -- bad choice for loop breaker
460           
461         | not_fun_ty (idType bndr) = 3  -- Data types help with cases
462                 -- This used to have a lower score than inlineCandidate, but
463                 -- it's *really* helpful if dictionaries get inlined fast,
464                 -- so I'm experimenting with giving higher priority to data-typed things
465
466         | inlineCandidate bndr rhs = 2  -- Likely to be inlined
467
468         | not (isEmptyCoreRules (idSpecialisation bndr)) = 1
469                 -- Avoid things with specialisations; we'd like
470                 -- to take advantage of them in the subsequent bindings
471
472         | otherwise = 0
473
474     inlineCandidate :: Id -> CoreExpr -> Bool
475     inlineCandidate id (Note InlineMe _) = True
476     inlineCandidate id rhs               = isOneOcc (idOccInfo id)
477
478         -- Real example (the Enum Ordering instance from PrelBase):
479         --      rec     f = \ x -> case d of (p,q,r) -> p x
480         --              g = \ x -> case d of (p,q,r) -> q x
481         --              d = (v, f, g)
482         --
483         -- Here, f and g occur just once; but we can't inline them into d.
484         -- On the other hand we *could* simplify those case expressions if
485         -- we didn't stupidly choose d as the loop breaker.
486         -- But we won't because constructor args are marked "Many".
487
488     not_fun_ty ty = not (isFunTy (dropForAlls ty))
489 \end{code}
490
491 @occAnalRhs@ deals with the question of bindings where the Id is marked
492 by an INLINE pragma.  For these we record that anything which occurs
493 in its RHS occurs many times.  This pessimistically assumes that ths
494 inlined binder also occurs many times in its scope, but if it doesn't
495 we'll catch it next time round.  At worst this costs an extra simplifier pass.
496 ToDo: try using the occurrence info for the inline'd binder.
497
498 [March 97] We do the same for atomic RHSs.  Reason: see notes with reOrderRec.
499 [June 98, SLPJ]  I've undone this change; I don't understand it.  See notes with reOrderRec.
500
501
502 \begin{code}
503 occAnalRhs :: OccEnv
504            -> Id -> CoreExpr    -- Binder and rhs
505                                 -- For non-recs the binder is alrady tagged
506                                 -- with occurrence info
507            -> (UsageDetails, CoreExpr)
508
509 occAnalRhs env id rhs
510   = (final_usage, rhs')
511   where
512     (rhs_usage, rhs') = occAnal ctxt rhs
513     ctxt | certainly_inline id = env
514          | otherwise           = rhsCtxt env
515         -- Note that we generally use an rhsCtxt.  This tells the occ anal n
516         -- that it's looking at an RHS, which has an effect in occAnalApp
517         --
518         -- But there's a problem.  Consider
519         --      x1 = a0 : []
520         --      x2 = a1 : x1
521         --      x3 = a2 : x2
522         --      g  = f x3
523         -- First time round, it looks as if x1 and x2 occur as an arg of a 
524         -- let-bound constructor ==> give them a many-occurrence.
525         -- But then x3 is inlined (unconditionally as it happens) and
526         -- next time round, x2 will be, and the next time round x1 will be
527         -- Result: multiple simplifier iterations.  Sigh.  
528         -- Crude solution: use rhsCtxt for things that occur just once...
529
530     certainly_inline id = case idOccInfo id of
531                             OneOcc in_lam one_br -> not in_lam && one_br
532                             other                -> False
533
534         -- [March 98] A new wrinkle is that if the binder has specialisations inside
535         -- it then we count the specialised Ids as "extra rhs's".  That way
536         -- the "parent" keeps the specialised "children" alive.  If the parent
537         -- dies (because it isn't referenced any more), then the children will
538         -- die too unless they are already referenced directly.
539
540     final_usage = foldVarSet add rhs_usage (idRuleVars id)
541     add v u = addOneOcc u v NoOccInfo           -- Give a non-committal binder info
542                                                 -- (i.e manyOcc) because many copies
543                                                 -- of the specialised thing can appear
544
545 \end{code}
546
547 Expressions
548 ~~~~~~~~~~~
549 \begin{code}
550 occAnal :: OccEnv
551         -> CoreExpr
552         -> (UsageDetails,       -- Gives info only about the "interesting" Ids
553             CoreExpr)
554
555 occAnal env (Type t)  = (emptyDetails, Type t)
556
557 occAnal env (Var v) 
558   = (var_uds, Var v)
559   where
560     var_uds | isCandidate env v = unitVarEnv v oneOcc
561             | otherwise         = emptyDetails
562
563     -- At one stage, I gathered the idRuleVars for v here too,
564     -- which in a way is the right thing to do.
565     -- But that went wrong right after specialisation, when
566     -- the *occurrences* of the overloaded function didn't have any
567     -- rules in them, so the *specialised* versions looked as if they
568     -- weren't used at all.
569
570 \end{code}
571
572 We regard variables that occur as constructor arguments as "dangerousToDup":
573
574 \begin{verbatim}
575 module A where
576 f x = let y = expensive x in 
577       let z = (True,y) in 
578       (case z of {(p,q)->q}, case z of {(p,q)->q})
579 \end{verbatim}
580
581 We feel free to duplicate the WHNF (True,y), but that means
582 that y may be duplicated thereby.
583
584 If we aren't careful we duplicate the (expensive x) call!
585 Constructors are rather like lambdas in this way.
586
587 \begin{code}
588 occAnal env expr@(Lit lit) = (emptyDetails, expr)
589 \end{code}
590
591 \begin{code}
592 occAnal env (Note InlineMe body)
593   = case occAnal env body of { (usage, body') -> 
594     (mapVarEnv markMany usage, Note InlineMe body')
595     }
596
597 occAnal env (Note note@(SCC cc) body)
598   = case occAnal env body of { (usage, body') ->
599     (mapVarEnv markInsideSCC usage, Note note body')
600     }
601
602 occAnal env (Note note body)
603   = case occAnal env body of { (usage, body') ->
604     (usage, Note note body')
605     }
606 \end{code}
607
608 \begin{code}
609 occAnal env app@(App fun arg)
610   = occAnalApp env (collectArgs app) False
611
612 -- Ignore type variables altogether
613 --   (a) occurrences inside type lambdas only not marked as InsideLam
614 --   (b) type variables not in environment
615
616 occAnal env expr@(Lam x body) | isTyVar x
617   = case occAnal env body of { (body_usage, body') ->
618     (body_usage, Lam x body')
619     }
620
621 -- For value lambdas we do a special hack.  Consider
622 --      (\x. \y. ...x...)
623 -- If we did nothing, x is used inside the \y, so would be marked
624 -- as dangerous to dup.  But in the common case where the abstraction
625 -- is applied to two arguments this is over-pessimistic.
626 -- So instead, we just mark each binder with its occurrence
627 -- info in the *body* of the multiple lambda.
628 -- Then, the simplifier is careful when partially applying lambdas.
629
630 occAnal env expr@(Lam _ _)
631   = case occAnal env_body body of { (body_usage, body') ->
632     let
633         (final_usage, tagged_binders) = tagBinders body_usage binders
634         --      URGH!  Sept 99: we don't seem to be able to use binders' here, because
635         --      we get linear-typed things in the resulting program that we can't handle yet.
636         --      (e.g. PrelShow)  TODO 
637
638         really_final_usage = if linear then
639                                 final_usage
640                              else
641                                 mapVarEnv markInsideLam final_usage
642     in
643     (really_final_usage,
644      mkLams tagged_binders body') }
645   where
646     (binders, body)   = collectBinders expr
647     (linear, env1, _) = oneShotGroup env binders
648     env2              = env1 `addNewCands` binders      -- Add in-scope binders
649     env_body          = vanillaCtxt env2                -- Body is (no longer) an RhsContext
650
651 -- gaw 2004
652 occAnal env (Case scrut bndr ty alts)
653   = case mapAndUnzip (occAnalAlt alt_env bndr) alts of { (alts_usage_s, alts')   -> 
654     case occAnal (vanillaCtxt env) scrut                    of { (scrut_usage, scrut') ->
655         -- No need for rhsCtxt
656     let
657         alts_usage  = foldr1 combineAltsUsageDetails alts_usage_s
658         alts_usage' = addCaseBndrUsage alts_usage
659         (alts_usage1, tagged_bndr) = tagBinder alts_usage' bndr
660         total_usage = scrut_usage `combineUsageDetails` alts_usage1
661     in
662 -- gaw 2004
663     total_usage `seq` (total_usage, Case scrut' tagged_bndr ty alts') }}
664   where
665     alt_env = env `addNewCand` bndr
666
667         -- The case binder gets a usage of either "many" or "dead", never "one".
668         -- Reason: we like to inline single occurrences, to eliminate a binding,
669         -- but inlining a case binder *doesn't* eliminate a binding.
670         -- We *don't* want to transform
671         --      case x of w { (p,q) -> f w }
672         -- into
673         --      case x of w { (p,q) -> f (p,q) }
674     addCaseBndrUsage usage = case lookupVarEnv usage bndr of
675                                 Nothing  -> usage
676                                 Just occ -> extendVarEnv usage bndr (markMany occ)
677
678 occAnal env (Let bind body)
679   = case occAnal new_env body            of { (body_usage, body') ->
680     case occAnalBind env bind body_usage of { (final_usage, new_binds) ->
681        (final_usage, mkLets new_binds body') }}
682   where
683     new_env = env `addNewCands` (bindersOf bind)
684
685 occAnalArgs env args
686   = case mapAndUnzip (occAnal arg_env) args of  { (arg_uds_s, args') ->
687     (foldr combineUsageDetails emptyDetails arg_uds_s, args')}
688   where
689     arg_env = vanillaCtxt env
690 \end{code}
691
692 Applications are dealt with specially because we want
693 the "build hack" to work.
694
695 \begin{code}
696 -- Hack for build, fold, runST
697 occAnalApp env (Var fun, args) is_rhs
698   = case args_stuff of { (args_uds, args') ->
699     let
700         -- We mark the free vars of the argument of a constructor or PAP 
701         -- as "many", if it is the RHS of a let(rec).
702         -- This means that nothing gets inlined into a constructor argument
703         -- position, which is what we want.  Typically those constructor
704         -- arguments are just variables, or trivial expressions.
705         --
706         -- This is the *whole point* of the isRhsEnv predicate
707         final_args_uds
708                 | isRhsEnv env,
709                   isDataConWorkId fun || valArgCount args < idArity fun
710                 = mapVarEnv markMany args_uds
711                 | otherwise = args_uds
712     in
713     (fun_uds `combineUsageDetails` final_args_uds, mkApps (Var fun) args') }
714   where
715     fun_uniq = idUnique fun
716
717     fun_uds | isCandidate env fun = unitVarEnv fun oneOcc
718             | otherwise           = emptyDetails
719
720     args_stuff  | fun_uniq == buildIdKey    = appSpecial env 2 [True,True]  args
721                 | fun_uniq == augmentIdKey  = appSpecial env 2 [True,True]  args
722                 | fun_uniq == foldrIdKey    = appSpecial env 3 [False,True] args
723                 | fun_uniq == runSTRepIdKey = appSpecial env 2 [True]       args
724                         -- (foldr k z xs) may call k many times, but it never
725                         -- shares a partial application of k; hence [False,True]
726                         -- This means we can optimise
727                         --      foldr (\x -> let v = ...x... in \y -> ...v...) z xs
728                         -- by floating in the v
729
730                 | otherwise = occAnalArgs env args
731
732
733 occAnalApp env (fun, args) is_rhs
734   = case occAnal (addAppCtxt env args) fun of   { (fun_uds, fun') ->
735         -- The addAppCtxt is a bit cunning.  One iteration of the simplifier
736         -- often leaves behind beta redexs like
737         --      (\x y -> e) a1 a2
738         -- Here we would like to mark x,y as one-shot, and treat the whole
739         -- thing much like a let.  We do this by pushing some True items
740         -- onto the context stack.
741
742     case occAnalArgs env args of        { (args_uds, args') ->
743     let
744         final_uds = fun_uds `combineUsageDetails` args_uds
745     in
746     (final_uds, mkApps fun' args') }}
747     
748 appSpecial :: OccEnv 
749            -> Int -> CtxtTy     -- Argument number, and context to use for it
750            -> [CoreExpr]
751            -> (UsageDetails, [CoreExpr])
752 appSpecial env n ctxt args
753   = go n args
754   where
755     arg_env = vanillaCtxt env
756
757     go n [] = (emptyDetails, [])        -- Too few args
758
759     go 1 (arg:args)                     -- The magic arg
760       = case occAnal (setCtxt arg_env ctxt) arg of      { (arg_uds, arg') ->
761         case occAnalArgs env args of                    { (args_uds, args') ->
762         (combineUsageDetails arg_uds args_uds, arg':args') }}
763     
764     go n (arg:args)
765       = case occAnal arg_env arg of     { (arg_uds, arg') ->
766         case go (n-1) args of           { (args_uds, args') ->
767         (combineUsageDetails arg_uds args_uds, arg':args') }}
768 \end{code}
769
770     
771 Case alternatives
772 ~~~~~~~~~~~~~~~~~
773 If the case binder occurs at all, the other binders effectively do too.  
774 For example
775         case e of x { (a,b) -> rhs }
776 is rather like
777         let x = (a,b) in rhs
778 If e turns out to be (e1,e2) we indeed get something like
779         let a = e1; b = e2; x = (a,b) in rhs
780
781 \begin{code}
782 occAnalAlt env case_bndr (con, bndrs, rhs)
783   = case occAnal (env `addNewCands` bndrs) rhs of { (rhs_usage, rhs') ->
784     let
785         (final_usage, tagged_bndrs) = tagBinders rhs_usage bndrs
786         final_bndrs | case_bndr `elemVarEnv` final_usage = bndrs
787                     | otherwise                         = tagged_bndrs
788                 -- Leave the binders untagged if the case 
789                 -- binder occurs at all; see note above
790     in
791     (final_usage, (con, final_bndrs, rhs')) }
792 \end{code}
793
794
795 %************************************************************************
796 %*                                                                      *
797 \subsection[OccurAnal-types]{OccEnv}
798 %*                                                                      *
799 %************************************************************************
800
801 \begin{code}
802 data OccEnv
803   = OccEnv IdSet        -- In-scope Ids; we gather info about these only
804            OccEncl      -- Enclosing context information
805            CtxtTy       -- Tells about linearity
806
807 -- OccEncl is used to control whether to inline into constructor arguments
808 -- For example:
809 --      x = (p,q)               -- Don't inline p or q
810 --      y = /\a -> (p a, q a)   -- Still don't inline p or q
811 --      z = f (p,q)             -- Do inline p,q; it may make a rule fire
812 -- So OccEncl tells enought about the context to know what to do when
813 -- we encounter a contructor application or PAP.
814
815 data OccEncl
816   = OccRhs              -- RHS of let(rec), albeit perhaps inside a type lambda
817                         -- Don't inline into constructor args here
818   | OccVanilla          -- Argument of function, body of lambda, scruintee of case etc.
819                         -- Do inline into constructor args here
820
821 type CtxtTy = [Bool]
822         -- []           No info
823         --
824         -- True:ctxt    Analysing a function-valued expression that will be
825         --                      applied just once
826         --
827         -- False:ctxt   Analysing a function-valued expression that may
828         --                      be applied many times; but when it is, 
829         --                      the CtxtTy inside applies
830
831 initOccEnv :: VarSet -> OccEnv
832 initOccEnv vars = OccEnv vars OccRhs []
833
834 isRhsEnv (OccEnv _ OccRhs     _) = True
835 isRhsEnv (OccEnv _ OccVanilla _) = False
836
837 isCandidate :: OccEnv -> Id -> Bool
838 isCandidate (OccEnv cands encl _) id = id `elemVarSet` cands 
839
840 addNewCands :: OccEnv -> [Id] -> OccEnv
841 addNewCands (OccEnv cands encl ctxt) ids
842   = OccEnv (extendVarSetList cands ids) encl ctxt
843
844 addNewCand :: OccEnv -> Id -> OccEnv
845 addNewCand (OccEnv cands encl ctxt) id
846   = OccEnv (extendVarSet cands id) encl ctxt
847
848 setCtxt :: OccEnv -> CtxtTy -> OccEnv
849 setCtxt (OccEnv cands encl _) ctxt = OccEnv cands encl ctxt
850
851 oneShotGroup :: OccEnv -> [CoreBndr] -> (Bool, OccEnv, [CoreBndr])
852         -- True <=> this is a one-shot linear lambda group
853         -- The [CoreBndr] are the binders.
854
855         -- The result binders have one-shot-ness set that they might not have had originally.
856         -- This happens in (build (\cn -> e)).  Here the occurrence analyser
857         -- linearity context knows that c,n are one-shot, and it records that fact in
858         -- the binder. This is useful to guide subsequent float-in/float-out tranformations
859
860 oneShotGroup (OccEnv cands encl ctxt) bndrs 
861   = case go ctxt bndrs [] of
862         (new_ctxt, new_bndrs) -> (all is_one_shot new_bndrs, OccEnv cands encl new_ctxt, new_bndrs)
863   where
864     is_one_shot b = isId b && isOneShotBndr b
865
866     go ctxt [] rev_bndrs = (ctxt, reverse rev_bndrs)
867
868     go (lin_ctxt:ctxt) (bndr:bndrs) rev_bndrs
869         | isId bndr = go ctxt bndrs (bndr':rev_bndrs)
870         where
871           bndr' | lin_ctxt  = setOneShotLambda bndr
872                 | otherwise = bndr
873
874     go ctxt (bndr:bndrs) rev_bndrs = go ctxt bndrs (bndr:rev_bndrs)
875
876
877 vanillaCtxt (OccEnv cands _ _) = OccEnv cands OccVanilla []
878 rhsCtxt     (OccEnv cands _ _) = OccEnv cands OccRhs     []
879
880 addAppCtxt (OccEnv cands encl ctxt) args 
881   = OccEnv cands encl (replicate (valArgCount args) True ++ ctxt)
882 \end{code}
883
884 %************************************************************************
885 %*                                                                      *
886 \subsection[OccurAnal-types]{OccEnv}
887 %*                                                                      *
888 %************************************************************************
889
890 \begin{code}
891 type UsageDetails = IdEnv OccInfo       -- A finite map from ids to their usage
892
893 combineUsageDetails, combineAltsUsageDetails
894         :: UsageDetails -> UsageDetails -> UsageDetails
895
896 combineUsageDetails usage1 usage2
897   = plusVarEnv_C addOccInfo usage1 usage2
898
899 combineAltsUsageDetails usage1 usage2
900   = plusVarEnv_C orOccInfo usage1 usage2
901
902 addOneOcc :: UsageDetails -> Id -> OccInfo -> UsageDetails
903 addOneOcc usage id info
904   = plusVarEnv_C addOccInfo usage (unitVarEnv id info)
905         -- ToDo: make this more efficient
906
907 emptyDetails = (emptyVarEnv :: UsageDetails)
908
909 usedIn :: Id -> UsageDetails -> Bool
910 v `usedIn` details =  isExportedId v || v `elemVarEnv` details
911
912 tagBinders :: UsageDetails          -- Of scope
913            -> [Id]                  -- Binders
914            -> (UsageDetails,        -- Details with binders removed
915               [IdWithOccInfo])    -- Tagged binders
916
917 tagBinders usage binders
918  = let
919      usage' = usage `delVarEnvList` binders
920      uss    = map (setBinderOcc usage) binders
921    in
922    usage' `seq` (usage', uss)
923
924 tagBinder :: UsageDetails           -- Of scope
925           -> Id                     -- Binders
926           -> (UsageDetails,         -- Details with binders removed
927               IdWithOccInfo)        -- Tagged binders
928
929 tagBinder usage binder
930  = let
931      usage'  = usage `delVarEnv` binder
932      binder' = setBinderOcc usage binder
933    in
934    usage' `seq` (usage', binder')
935
936 setBinderOcc :: UsageDetails -> CoreBndr -> CoreBndr
937 setBinderOcc usage bndr
938   | isTyVar bndr      = bndr
939   | isExportedId bndr = case idOccInfo bndr of
940                           NoOccInfo -> bndr
941                           other     -> setIdOccInfo bndr NoOccInfo
942             -- Don't use local usage info for visible-elsewhere things
943             -- BUT *do* erase any IAmALoopBreaker annotation, because we're
944             -- about to re-generate it and it shouldn't be "sticky"
945                           
946   | otherwise = setIdOccInfo bndr occ_info
947   where
948     occ_info = lookupVarEnv usage bndr `orElse` IAmDead
949 \end{code}
950
951
952 %************************************************************************
953 %*                                                                      *
954 \subsection{Operations over OccInfo}
955 %*                                                                      *
956 %************************************************************************
957
958 \begin{code}
959 oneOcc :: OccInfo
960 oneOcc = OneOcc False True
961
962 markMany, markInsideLam, markInsideSCC :: OccInfo -> OccInfo
963
964 markMany IAmDead = IAmDead
965 markMany other   = NoOccInfo
966
967 markInsideSCC occ = markMany occ
968
969 markInsideLam (OneOcc _ one_br) = OneOcc True one_br
970 markInsideLam occ               = occ
971
972 addOccInfo, orOccInfo :: OccInfo -> OccInfo -> OccInfo
973
974 addOccInfo IAmDead info2 = info2
975 addOccInfo info1 IAmDead = info1
976 addOccInfo info1 info2   = NoOccInfo
977
978 -- (orOccInfo orig new) is used
979 -- when combining occurrence info from branches of a case
980
981 orOccInfo IAmDead info2 = info2
982 orOccInfo info1 IAmDead = info1
983 orOccInfo (OneOcc in_lam1 one_branch1)
984           (OneOcc in_lam2 one_branch2)
985   = OneOcc (in_lam1 || in_lam2)
986            False        -- False, because it occurs in both branches
987
988 orOccInfo info1 info2 = NoOccInfo
989 \end{code}