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