2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 %************************************************************************
6 \section[OccurAnal]{Occurrence analysis pass}
8 %************************************************************************
10 The occurrence analyser re-typechecks a core expression, returning a new
11 core expression with (hopefully) improved usage information.
15 occurAnalysePgm, occurAnalyseExpr
18 #include "HsVersions.h"
22 import Type ( tyVarsOfType )
23 import CoreUtils ( exprIsTrivial, isDefaultAlt, mkCoerceI, isExpandableApp )
24 import Coercion ( CoercionI(..), mkSymCoI )
28 import Name ( Name, localiseName )
32 import Var ( Var, varUnique )
33 import Maybes ( orElse )
34 import Digraph ( SCC(..), stronglyConnCompFromEdgedVerticesR )
35 import PrelNames ( buildIdKey, foldrIdKey, runSTRepIdKey, augmentIdKey )
38 import Util ( mapAndUnzip, filterOut )
46 %************************************************************************
48 \subsection[OccurAnal-main]{Counting occurrences: main function}
50 %************************************************************************
52 Here's the externally-callable interface:
55 occurAnalysePgm :: [CoreBind] -> [CoreRule] -> [CoreBind]
56 occurAnalysePgm binds rules
57 = snd (go (initOccEnv rules) binds)
59 initial_uds = addIdOccs emptyDetails (rulesFreeVars rules)
60 -- The RULES keep things alive!
62 go :: OccEnv -> [CoreBind] -> (UsageDetails, [CoreBind])
66 = (final_usage, bind' ++ binds')
68 (bs_usage, binds') = go env binds
69 (final_usage, bind') = occAnalBind env env bind bs_usage
71 occurAnalyseExpr :: CoreExpr -> CoreExpr
72 -- Do occurrence analysis, and discard occurence info returned
73 occurAnalyseExpr expr = snd (occAnal (initOccEnv []) expr)
77 %************************************************************************
79 \subsection[OccurAnal-main]{Counting occurrences: main function}
81 %************************************************************************
87 occAnalBind :: OccEnv -- The incoming OccEnv
88 -> OccEnv -- Same, but trimmed by (binderOf bind)
90 -> UsageDetails -- Usage details of scope
91 -> (UsageDetails, -- Of the whole let(rec)
94 occAnalBind env _ (NonRec binder rhs) body_usage
95 | isTyCoVar binder -- A type let; we don't gather usage info
96 = (body_usage, [NonRec binder rhs])
98 | not (binder `usedIn` body_usage) -- It's not mentioned
101 | otherwise -- It's mentioned in the body
102 = (body_usage' +++ addRuleUsage rhs_usage binder, -- Note [Rules are extra RHSs]
103 [NonRec tagged_binder rhs'])
105 (body_usage', tagged_binder) = tagBinder body_usage binder
106 (rhs_usage, rhs') = occAnalRhs env tagged_binder rhs
111 Dropping dead code for recursive bindings is done in a very simple way:
113 the entire set of bindings is dropped if none of its binders are
114 mentioned in its body; otherwise none are.
116 This seems to miss an obvious improvement.
128 Now 'f' is unused! But it's OK! Dependency analysis will sort this
129 out into a letrec for 'g' and a 'let' for 'f', and then 'f' will get
130 dropped. It isn't easy to do a perfect job in one blow. Consider
141 Note [Loop breaking and RULES]
142 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
143 Loop breaking is surprisingly subtle. First read the section 4 of
144 "Secrets of the GHC inliner". This describes our basic plan.
146 However things are made quite a bit more complicated by RULES. Remember
148 * Note [Rules are extra RHSs]
149 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
150 A RULE for 'f' is like an extra RHS for 'f'. That way the "parent"
151 keeps the specialised "children" alive. If the parent dies
152 (because it isn't referenced any more), then the children will die
153 too (unless they are already referenced directly).
155 To that end, we build a Rec group for each cyclic strongly
157 *treating f's rules as extra RHSs for 'f'*.
158 More concretely, the SCC analysis runs on a graph with an edge
159 from f -> g iff g is mentioned in
164 Under (b) we include variables free in *either* LHS *or* RHS of
165 the rule. The former might seems silly, but see Note [Rule
166 dependency info]. So in Example [eftInt], eftInt and eftIntFB
167 will be put in the same Rec, even though their 'main' RHSs are
170 * Note [Rules are visible in their own rec group]
171 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
172 We want the rules for 'f' to be visible in f's right-hand side.
173 And we'd like them to be visible in other functions in f's Rec
174 group. E.g. in Example [Specialisation rules] we want f' rule
175 to be visible in both f's RHS, and fs's RHS.
177 This means that we must simplify the RULEs first, before looking
178 at any of the definitions. This is done by Simplify.simplRecBind,
179 when it calls addLetIdInfo.
181 * Note [Choosing loop breakers]
182 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
183 We avoid infinite inlinings by choosing loop breakers, and
184 ensuring that a loop breaker cuts each loop. But what is a
185 "loop"? In particular, a RULE is like an equation for 'f' that
186 is *always* inlined if it is applicable. We do *not* disable
187 rules for loop-breakers. It's up to whoever makes the rules to
188 make sure that the rules themselves always terminate. See Note
189 [Rules for recursive functions] in Simplify.lhs
192 f's RHS mentions g, and
193 g has a RULE that mentions h, and
194 h has a RULE that mentions f
196 then we *must* choose f to be a loop breaker. In general, take the
197 free variables of f's RHS, and augment it with all the variables
198 reachable by RULES from those starting points. That is the whole
199 reason for computing rule_fv_env in occAnalBind. (Of course we
200 only consider free vars that are also binders in this Rec group.)
202 Note that when we compute this rule_fv_env, we only consider variables
203 free in the *RHS* of the rule, in contrast to the way we build the
204 Rec group in the first place (Note [Rule dependency info])
206 Note that if 'g' has RHS that mentions 'w', we should add w to
207 g's loop-breaker edges. More concretely there is an edge from f -> g
209 (a) g is mentioned in f's RHS
210 (b) h is mentioned in f's RHS, and
211 g appears in the RHS of a RULE of h
212 or a transitive sequence of rules starting with h
214 Note that in Example [eftInt], *neither* eftInt *nor* eftIntFB is
215 chosen as a loop breaker, because their RHSs don't mention each other.
216 And indeed both can be inlined safely.
218 Note that the edges of the graph we use for computing loop breakers
219 are not the same as the edges we use for computing the Rec blocks.
220 That's why we compute
221 rec_edges for the Rec block analysis
222 loop_breaker_edges for the loop breaker analysis
225 * Note [Weak loop breakers]
226 ~~~~~~~~~~~~~~~~~~~~~~~~~
227 There is a last nasty wrinkle. Suppose we have
237 Remmber that we simplify the RULES before any RHS (see Note
238 [Rules are visible in their own rec group] above).
240 So we must *not* postInlineUnconditionally 'g', even though
241 its RHS turns out to be trivial. (I'm assuming that 'g' is
242 not choosen as a loop breaker.) Why not? Because then we
243 drop the binding for 'g', which leaves it out of scope in the
246 We "solve" this by making g a "weak" or "rules-only" loop breaker,
247 with OccInfo = IAmLoopBreaker True. A normal "strong" loop breaker
248 has IAmLoopBreaker False. So
250 Inline postInlineUnconditionally
251 IAmLoopBreaker False no no
252 IAmLoopBreaker True yes no
255 The **sole** reason for this kind of loop breaker is so that
256 postInlineUnconditionally does not fire. Ugh.
258 * Note [Rule dependency info]
259 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
260 The VarSet in a SpecInfo is used for dependency analysis in the
261 occurrence analyser. We must track free vars in *both* lhs and rhs.
262 Hence use of idRuleVars, rather than idRuleRhsVars in addRuleUsage.
266 Then if we substitute y for x, we'd better do so in the
267 rule's LHS too, so we'd better ensure the dependency is respected
270 * Note [Inline rules]
272 None of the above stuff about RULES applies to Inline Rules,
273 stored in a CoreUnfolding. The unfolding, if any, is simplified
274 at the same time as the regular RHS of the function, so it should
275 be treated *exactly* like an extra RHS.
280 Example (from GHC.Enum):
282 eftInt :: Int# -> Int# -> [Int]
283 eftInt x y = ...(non-recursive)...
285 {-# INLINE [0] eftIntFB #-}
286 eftIntFB :: (Int -> r -> r) -> r -> Int# -> Int# -> r
287 eftIntFB c n x y = ...(non-recursive)...
290 "eftInt" [~1] forall x y. eftInt x y = build (\ c n -> eftIntFB c n x y)
291 "eftIntList" [1] eftIntFB (:) [] = eftInt
294 Example [Specialisation rules]
295 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
296 Consider this group, which is typical of what SpecConstr builds:
298 fs a = ....f (C a)....
299 f x = ....f (C a)....
300 {-# RULE f (C a) = fs a #-}
302 So 'f' and 'fs' are in the same Rec group (since f refers to fs via its RULE).
304 But watch out! If 'fs' is not chosen as a loop breaker, we may get an infinite loop:
305 - the RULE is applied in f's RHS (see Note [Self-recursive rules] in Simplify
306 - fs is inlined (say it's small)
307 - now there's another opportunity to apply the RULE
309 This showed up when compiling Control.Concurrent.Chan.getChanContents.
313 occAnalBind _ env (Rec pairs) body_usage
314 = foldr occAnalRec (body_usage, []) sccs
315 -- For a recursive group, we
316 -- * occ-analyse all the RHSs
317 -- * compute strongly-connected components
318 -- * feed those components to occAnalRec
320 -------------Dependency analysis ------------------------------
321 bndr_set = mkVarSet (map fst pairs)
323 sccs :: [SCC (Node Details)]
324 sccs = {-# SCC "occAnalBind.scc" #-} stronglyConnCompFromEdgedVerticesR rec_edges
326 rec_edges :: [Node Details]
327 rec_edges = {-# SCC "occAnalBind.assoc" #-} map make_node pairs
329 make_node (bndr, rhs)
330 = (ND bndr rhs' all_rhs_usage rhs_fvs, varUnique bndr, out_edges)
332 (rhs_usage, rhs') = occAnalRhs env bndr rhs
333 all_rhs_usage = addIdOccs rhs_usage rule_vars -- Note [Rules are extra RHSs]
334 rhs_fvs = intersectUFM_C (\b _ -> b) bndr_set rhs_usage
335 out_edges = keysUFM (rhs_fvs `unionVarSet` rule_vars)
336 rule_vars = idRuleVars bndr -- See Note [Rule dependency info]
337 -- (a -> b) means a mentions b
338 -- Given the usage details (a UFM that gives occ info for each free var of
339 -- the RHS) we can get the list of free vars -- or rather their Int keys --
340 -- by just extracting the keys from the finite map. Grimy, but fast.
341 -- Previously we had this:
342 -- [ bndr | bndr <- bndrs,
343 -- maybeToBool (lookupVarEnv rhs_usage bndr)]
344 -- which has n**2 cost, and this meant that edges_from alone
345 -- consumed 10% of total runtime!
347 -----------------------------
348 occAnalRec :: SCC (Node Details) -> (UsageDetails, [CoreBind])
349 -> (UsageDetails, [CoreBind])
351 -- The NonRec case is just like a Let (NonRec ...) above
352 occAnalRec (AcyclicSCC (ND bndr rhs rhs_usage _, _, _)) (body_usage, binds)
353 | not (bndr `usedIn` body_usage)
354 = (body_usage, binds)
356 | otherwise -- It's mentioned in the body
357 = (body_usage' +++ rhs_usage,
358 NonRec tagged_bndr rhs : binds)
360 (body_usage', tagged_bndr) = tagBinder body_usage bndr
363 -- The Rec case is the interesting one
364 -- See Note [Loop breaking]
365 occAnalRec (CyclicSCC nodes) (body_usage, binds)
366 | not (any (`usedIn` body_usage) bndrs) -- NB: look at body_usage, not total_usage
367 = (body_usage, binds) -- Dead code
369 | otherwise -- At this point we always build a single Rec
370 = (final_usage, Rec pairs : binds)
373 bndrs = [b | (ND b _ _ _, _, _) <- nodes]
374 bndr_set = mkVarSet bndrs
376 ----------------------------
377 -- Tag the binders with their occurrence info
378 total_usage = foldl add_usage body_usage nodes
379 add_usage usage_so_far (ND _ _ rhs_usage _, _, _) = usage_so_far +++ rhs_usage
380 (final_usage, tagged_nodes) = mapAccumL tag_node total_usage nodes
382 tag_node :: UsageDetails -> Node Details -> (UsageDetails, Node Details)
383 -- (a) Tag the binders in the details with occ info
384 -- (b) Mark the binder with "weak loop-breaker" OccInfo
385 -- saying "no preInlineUnconditionally" if it is used
386 -- in any rule (lhs or rhs) of the recursive group
387 -- See Note [Weak loop breakers]
388 tag_node usage (ND bndr rhs rhs_usage rhs_fvs, k, ks)
389 = (usage `delVarEnv` bndr, (ND bndr2 rhs rhs_usage rhs_fvs, k, ks))
391 bndr2 | bndr `elemVarSet` all_rule_fvs = makeLoopBreaker True bndr1
393 bndr1 = setBinderOcc usage bndr
394 all_rule_fvs = bndr_set `intersectVarSet` foldr (unionVarSet . idRuleVars)
397 ----------------------------
398 -- Now reconstruct the cycle
399 pairs | no_rules = reOrderCycle 0 tagged_nodes []
400 | otherwise = foldr (reOrderRec 0) [] $
401 stronglyConnCompFromEdgedVerticesR loop_breaker_edges
403 -- See Note [Choosing loop breakers] for loop_breaker_edges
404 loop_breaker_edges = map mk_node tagged_nodes
405 mk_node (details@(ND _ _ _ rhs_fvs), k, _) = (details, k, new_ks)
407 new_ks = keysUFM (fst (extendFvs rule_fv_env rhs_fvs))
409 ------------------------------------
410 rule_fv_env :: IdEnv IdSet -- Variables from this group mentioned in RHS of rules
411 -- Domain is *subset* of bound vars (others have no rule fvs)
412 rule_fv_env = transClosureFV init_rule_fvs
413 no_rules = null init_rule_fvs
414 init_rule_fvs = [(b, rule_fvs)
417 , let rule_fvs = idRuleRhsVars b `intersectVarSet` bndr_set
418 , not (isEmptyVarSet rule_fvs)]
421 @reOrderRec@ is applied to the list of (binder,rhs) pairs for a cyclic
422 strongly connected component (there's guaranteed to be a cycle). It returns the
424 a) in a better order,
425 b) with some of the Ids having a IAmALoopBreaker pragma
427 The "loop-breaker" Ids are sufficient to break all cycles in the SCC. This means
428 that the simplifier can guarantee not to loop provided it never records an inlining
429 for these no-inline guys.
431 Furthermore, the order of the binds is such that if we neglect dependencies
432 on the no-inline Ids then the binds are topologically sorted. This means
433 that the simplifier will generally do a good job if it works from top bottom,
434 recording inlinings for any Ids which aren't marked as "no-inline" as it goes.
437 [June 98: I don't understand the following paragraphs, and I've
438 changed the a=b case again so that it isn't a special case any more.]
440 Here's a case that bit me:
448 Re-ordering doesn't change the order of bindings, but there was no loop-breaker.
450 My solution was to make a=b bindings record b as Many, rather like INLINE bindings.
451 Perhaps something cleverer would suffice.
456 type Node details = (details, Unique, [Unique]) -- The Ints are gotten from the Unique,
457 -- which is gotten from the Id.
458 data Details = ND Id -- Binder
461 UsageDetails -- Full usage from RHS,
462 -- including *both* RULES *and* InlineRule unfolding
464 IdSet -- Other binders *from this Rec group* mentioned in
466 -- * any InlineRule unfolding
467 -- but *excluding* any RULES
469 reOrderRec :: Int -> SCC (Node Details)
470 -> [(Id,CoreExpr)] -> [(Id,CoreExpr)]
471 -- Sorted into a plausible order. Enough of the Ids have
472 -- IAmALoopBreaker pragmas that there are no loops left.
473 reOrderRec _ (AcyclicSCC (ND bndr rhs _ _, _, _)) pairs = (bndr, rhs) : pairs
474 reOrderRec depth (CyclicSCC cycle) pairs = reOrderCycle depth cycle pairs
476 reOrderCycle :: Int -> [Node Details] -> [(Id,CoreExpr)] -> [(Id,CoreExpr)]
478 = panic "reOrderCycle"
479 reOrderCycle _ [bind] pairs -- Common case of simple self-recursion
480 = (makeLoopBreaker False bndr, rhs) : pairs
482 (ND bndr rhs _ _, _, _) = bind
484 reOrderCycle depth (bind : binds) pairs
485 = -- Choose a loop breaker, mark it no-inline,
486 -- do SCC analysis on the rest, and recursively sort them out
487 -- pprTrace "reOrderCycle" (ppr [b | (ND b _ _ _, _, _) <- bind:binds]) $
488 foldr (reOrderRec new_depth)
489 ([ (makeLoopBreaker False bndr, rhs)
490 | (ND bndr rhs _ _, _, _) <- chosen_binds] ++ pairs)
491 (stronglyConnCompFromEdgedVerticesR unchosen)
493 (chosen_binds, unchosen) = choose_loop_breaker [bind] (score bind) [] binds
495 approximate_loop_breaker = depth >= 2
496 new_depth | approximate_loop_breaker = 0
497 | otherwise = depth+1
498 -- After two iterations (d=0, d=1) give up
499 -- and approximate, returning to d=0
501 -- This loop looks for the bind with the lowest score
502 -- to pick as the loop breaker. The rest accumulate in
503 choose_loop_breaker loop_binds _loop_sc acc []
504 = (loop_binds, acc) -- Done
506 -- If approximate_loop_breaker is True, we pick *all*
507 -- nodes with lowest score, else just one
508 -- See Note [Complexity of loop breaking]
509 choose_loop_breaker loop_binds loop_sc acc (bind : binds)
510 | sc < loop_sc -- Lower score so pick this new one
511 = choose_loop_breaker [bind] sc (loop_binds ++ acc) binds
513 | approximate_loop_breaker && sc == loop_sc
514 = choose_loop_breaker (bind : loop_binds) loop_sc acc binds
516 | otherwise -- Higher score so don't pick it
517 = choose_loop_breaker loop_binds loop_sc (bind : acc) binds
521 score :: Node Details -> Int -- Higher score => less likely to be picked as loop breaker
522 score (ND bndr rhs _ _, _, _)
523 | not (isId bndr) = 100 -- A type or cercion varialbe is never a loop breaker
525 | isDFunId bndr = 9 -- Never choose a DFun as a loop breaker
526 -- Note [DFuns should not be loop breakers]
528 | Just (inl_source, _) <- isStableUnfolding_maybe (idUnfolding bndr)
530 InlineWrapper {} -> 10 -- Note [INLINE pragmas]
531 _other -> 3 -- Data structures are more important than this
532 -- so that dictionary/method recursion unravels
533 -- Note that this case hits all InlineRule things, so we
534 -- never look at 'rhs for InlineRule stuff. That's right, because
535 -- 'rhs' is irrelevant for inlining things with an InlineRule
537 | is_con_app rhs = 5 -- Data types help with cases: Note [Constructor applications]
539 | exprIsTrivial rhs = 10 -- Practically certain to be inlined
540 -- Used to have also: && not (isExportedId bndr)
541 -- But I found this sometimes cost an extra iteration when we have
542 -- rec { d = (a,b); a = ...df...; b = ...df...; df = d }
543 -- where df is the exported dictionary. Then df makes a really
544 -- bad choice for loop breaker
547 -- If an Id is marked "never inline" then it makes a great loop breaker
548 -- The only reason for not checking that here is that it is rare
549 -- and I've never seen a situation where it makes a difference,
550 -- so it probably isn't worth the time to test on every binder
551 -- | isNeverActive (idInlinePragma bndr) = -10
553 | isOneOcc (idOccInfo bndr) = 2 -- Likely to be inlined
555 | canUnfold (realIdUnfolding bndr) = 1
556 -- The Id has some kind of unfolding
557 -- Ignore loop-breaker-ness here because that is what we are setting!
561 -- Checking for a constructor application
562 -- Cheap and cheerful; the simplifer moves casts out of the way
563 -- The lambda case is important to spot x = /\a. C (f a)
564 -- which comes up when C is a dictionary constructor and
565 -- f is a default method.
566 -- Example: the instance for Show (ST s a) in GHC.ST
568 -- However we *also* treat (\x. C p q) as a con-app-like thing,
569 -- Note [Closure conversion]
570 is_con_app (Var v) = isConLikeId v
571 is_con_app (App f _) = is_con_app f
572 is_con_app (Lam _ e) = is_con_app e
573 is_con_app (Note _ e) = is_con_app e
576 makeLoopBreaker :: Bool -> Id -> Id
577 -- Set the loop-breaker flag: see Note [Weak loop breakers]
578 makeLoopBreaker weak bndr
579 = ASSERT2( isId bndr, ppr bndr ) setIdOccInfo bndr (IAmALoopBreaker weak)
582 Note [Complexity of loop breaking]
583 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
584 The loop-breaking algorithm knocks out one binder at a time, and
585 performs a new SCC analysis on the remaining binders. That can
586 behave very badly in tightly-coupled groups of bindings; in the
587 worst case it can be (N**2)*log N, because it does a full SCC
588 on N, then N-1, then N-2 and so on.
590 To avoid this, we switch plans after 2 (or whatever) attempts:
591 Plan A: pick one binder with the lowest score, make it
592 a loop breaker, and try again
593 Plan B: pick *all* binders with the lowest score, make them
594 all loop breakers, and try again
595 Since there are only a small finite number of scores, this will
596 terminate in a constant number of iterations, rather than O(N)
599 You might thing that it's very unlikely, but RULES make it much
600 more likely. Here's a real example from Trac #1969:
601 Rec { $dm = \d.\x. op d
602 {-# RULES forall d. $dm Int d = $s$dm1
603 forall d. $dm Bool d = $s$dm2 #-}
605 dInt = MkD .... opInt ...
606 dInt = MkD .... opBool ...
611 $s$dm2 = \x. op dBool }
612 The RULES stuff means that we can't choose $dm as a loop breaker
613 (Note [Choosing loop breakers]), so we must choose at least (say)
614 opInt *and* opBool, and so on. The number of loop breakders is
615 linear in the number of instance declarations.
617 Note [INLINE pragmas]
618 ~~~~~~~~~~~~~~~~~~~~~
619 Avoid choosing a function with an INLINE pramga as the loop breaker!
620 If such a function is mutually-recursive with a non-INLINE thing,
621 then the latter should be the loop-breaker.
623 Usually this is just a question of optimisation. But a particularly
624 bad case is wrappers generated by the demand analyser: if you make
625 then into a loop breaker you may get an infinite inlining loop. For
628 $wfoo x = ....foo x....
630 {-loop brk-} foo x = ...$wfoo x...
632 The interface file sees the unfolding for $wfoo, and sees that foo is
633 strict (and hence it gets an auto-generated wrapper). Result: an
634 infinite inlining in the importing scope. So be a bit careful if you
635 change this. A good example is Tree.repTree in
636 nofib/spectral/minimax. If the repTree wrapper is chosen as the loop
637 breaker then compiling Game.hs goes into an infinite loop. This
638 happened when we gave is_con_app a lower score than inline candidates:
641 = __inline_me (/\a. \w w1 w2 ->
642 case Tree.$wrepTree @ a w w1 w2 of
643 { (# ww1, ww2 #) -> Branch @ a ww1 ww2 })
646 (# w2_smP, map a (Tree a) (Tree.repTree a w1 w) (w w2) #)
648 Here we do *not* want to choose 'repTree' as the loop breaker.
650 Note [DFuns should not be loop breakers]
651 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
652 It's particularly bad to make a DFun into a loop breaker. See
653 Note [How instance declarations are translated] in TcInstDcls
655 We give DFuns a higher score than ordinary CONLIKE things because
656 if there's a choice we want the DFun to be the non-looop breker. Eg
658 rec { sc = /\ a \$dC. $fBWrap (T a) ($fCT @ a $dC)
660 $fCT :: forall a_afE. (Roman.C a_afE) => Roman.C (Roman.T a_afE)
662 $fCT = /\a \$dC. MkD (T a) ((sc @ a $dC) |> blah) ($ctoF @ a $dC)
665 Here 'sc' (the superclass) looks CONLIKE, but we'll never get to it
666 if we can't unravel the DFun first.
668 Note [Constructor applications]
669 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
670 It's really really important to inline dictionaries. Real
671 example (the Enum Ordering instance from GHC.Base):
673 rec f = \ x -> case d of (p,q,r) -> p x
674 g = \ x -> case d of (p,q,r) -> q x
677 Here, f and g occur just once; but we can't inline them into d.
678 On the other hand we *could* simplify those case expressions if
679 we didn't stupidly choose d as the loop breaker.
680 But we won't because constructor args are marked "Many".
681 Inlining dictionaries is really essential to unravelling
682 the loops in static numeric dictionaries, see GHC.Float.
684 Note [Closure conversion]
685 ~~~~~~~~~~~~~~~~~~~~~~~~~
686 We treat (\x. C p q) as a high-score candidate in the letrec scoring algorithm.
687 The immediate motivation came from the result of a closure-conversion transformation
688 which generated code like this:
690 data Clo a b = forall c. Clo (c -> a -> b) c
692 ($:) :: Clo a b -> a -> b
693 Clo f env $: x = f env x
695 rec { plus = Clo plus1 ()
697 ; plus1 _ n = Clo plus2 n
700 ; plus2 (Succ m) n = Succ (plus $: m $: n) }
702 If we inline 'plus' and 'plus1', everything unravels nicely. But if
703 we choose 'plus1' as the loop breaker (which is entirely possible
704 otherwise), the loop does not unravel nicely.
707 @occAnalRhs@ deals with the question of bindings where the Id is marked
708 by an INLINE pragma. For these we record that anything which occurs
709 in its RHS occurs many times. This pessimistically assumes that ths
710 inlined binder also occurs many times in its scope, but if it doesn't
711 we'll catch it next time round. At worst this costs an extra simplifier pass.
712 ToDo: try using the occurrence info for the inline'd binder.
714 [March 97] We do the same for atomic RHSs. Reason: see notes with reOrderRec.
715 [June 98, SLPJ] I've undone this change; I don't understand it. See notes with reOrderRec.
720 -> Id -> CoreExpr -- Binder and rhs
721 -- For non-recs the binder is alrady tagged
722 -- with occurrence info
723 -> (UsageDetails, CoreExpr)
724 -- Returned usage details includes any INLINE rhs
726 occAnalRhs env id rhs
727 | isId id = (addIdOccs rhs_usage (idUnfoldingVars id), rhs')
728 | otherwise = (rhs_usage, rhs')
729 -- Include occurrences for the "extra RHS" from a CoreUnfolding
731 (rhs_usage, rhs') = occAnal ctxt rhs
732 ctxt | certainly_inline id = env
733 | otherwise = rhsCtxt env
734 -- Note that we generally use an rhsCtxt. This tells the occ anal n
735 -- that it's looking at an RHS, which has an effect in occAnalApp
737 -- But there's a problem. Consider
742 -- First time round, it looks as if x1 and x2 occur as an arg of a
743 -- let-bound constructor ==> give them a many-occurrence.
744 -- But then x3 is inlined (unconditionally as it happens) and
745 -- next time round, x2 will be, and the next time round x1 will be
746 -- Result: multiple simplifier iterations. Sigh.
747 -- Crude solution: use rhsCtxt for things that occur just once...
749 certainly_inline id = case idOccInfo id of
750 OneOcc in_lam one_br _ -> not in_lam && one_br
757 addRuleUsage :: UsageDetails -> Var -> UsageDetails
758 -- Add the usage from RULES in Id to the usage
759 addRuleUsage usage var
760 | isId var = addIdOccs usage (idRuleVars var)
762 -- idRuleVars here: see Note [Rule dependency info]
764 addIdOccs :: UsageDetails -> VarSet -> UsageDetails
765 addIdOccs usage id_set = foldVarSet add usage id_set
767 add v u | isId v = addOneOcc u v NoOccInfo
769 -- Give a non-committal binder info (i.e NoOccInfo) because
770 -- a) Many copies of the specialised thing can appear
771 -- b) We don't want to substitute a BIG expression inside a RULE
772 -- even if that's the only occurrence of the thing
773 -- (Same goes for INLINE.)
781 -> (UsageDetails, -- Gives info only about the "interesting" Ids
784 occAnal _ (Type t) = (emptyDetails, Type t)
785 occAnal env (Var v) = (mkOneOcc env v False, Var v)
786 -- At one stage, I gathered the idRuleVars for v here too,
787 -- which in a way is the right thing to do.
788 -- But that went wrong right after specialisation, when
789 -- the *occurrences* of the overloaded function didn't have any
790 -- rules in them, so the *specialised* versions looked as if they
791 -- weren't used at all.
794 We regard variables that occur as constructor arguments as "dangerousToDup":
798 f x = let y = expensive x in
800 (case z of {(p,q)->q}, case z of {(p,q)->q})
803 We feel free to duplicate the WHNF (True,y), but that means
804 that y may be duplicated thereby.
806 If we aren't careful we duplicate the (expensive x) call!
807 Constructors are rather like lambdas in this way.
810 occAnal _ expr@(Lit _) = (emptyDetails, expr)
814 occAnal env (Note note@(SCC _) body)
815 = case occAnal env body of { (usage, body') ->
816 (mapVarEnv markInsideSCC usage, Note note body')
819 occAnal env (Note note body)
820 = case occAnal env body of { (usage, body') ->
821 (usage, Note note body')
824 occAnal env (Cast expr co)
825 = case occAnal env expr of { (usage, expr') ->
826 (markManyIf (isRhsEnv env) usage, Cast expr' co)
827 -- If we see let x = y `cast` co
828 -- then mark y as 'Many' so that we don't
829 -- immediately inline y again.
834 occAnal env app@(App _ _)
835 = occAnalApp env (collectArgs app)
837 -- Ignore type variables altogether
838 -- (a) occurrences inside type lambdas only not marked as InsideLam
839 -- (b) type variables not in environment
841 occAnal env (Lam x body) | isTyCoVar x
842 = case occAnal env body of { (body_usage, body') ->
843 (body_usage, Lam x body')
846 -- For value lambdas we do a special hack. Consider
848 -- If we did nothing, x is used inside the \y, so would be marked
849 -- as dangerous to dup. But in the common case where the abstraction
850 -- is applied to two arguments this is over-pessimistic.
851 -- So instead, we just mark each binder with its occurrence
852 -- info in the *body* of the multiple lambda.
853 -- Then, the simplifier is careful when partially applying lambdas.
855 occAnal env expr@(Lam _ _)
856 = case occAnal env_body body of { (body_usage, body') ->
858 (final_usage, tagged_binders) = tagLamBinders body_usage binders'
859 -- Use binders' to put one-shot info on the lambdas
861 -- URGH! Sept 99: we don't seem to be able to use binders' here, because
862 -- we get linear-typed things in the resulting program that we can't handle yet.
863 -- (e.g. PrelShow) TODO
865 really_final_usage = if linear then
868 mapVarEnv markInsideLam final_usage
871 mkLams tagged_binders body') }
873 env_body = vanillaCtxt (trimOccEnv env binders)
874 -- Body is (no longer) an RhsContext
875 (binders, body) = collectBinders expr
876 binders' = oneShotGroup env binders
877 linear = all is_one_shot binders'
878 is_one_shot b = isId b && isOneShotBndr b
880 occAnal env (Case scrut bndr ty alts)
881 = case occ_anal_scrut scrut alts of { (scrut_usage, scrut') ->
882 case mapAndUnzip occ_anal_alt alts of { (alts_usage_s, alts') ->
884 alts_usage = foldr1 combineAltsUsageDetails alts_usage_s
885 (alts_usage1, tagged_bndr) = tag_case_bndr alts_usage bndr
886 total_usage = scrut_usage +++ alts_usage1
888 total_usage `seq` (total_usage, Case scrut' tagged_bndr ty alts') }}
890 -- Note [Case binder usage]
891 -- ~~~~~~~~~~~~~~~~~~~~~~~~
892 -- The case binder gets a usage of either "many" or "dead", never "one".
893 -- Reason: we like to inline single occurrences, to eliminate a binding,
894 -- but inlining a case binder *doesn't* eliminate a binding.
895 -- We *don't* want to transform
896 -- case x of w { (p,q) -> f w }
898 -- case x of w { (p,q) -> f (p,q) }
899 tag_case_bndr usage bndr
900 = case lookupVarEnv usage bndr of
901 Nothing -> (usage, setIdOccInfo bndr IAmDead)
902 Just _ -> (usage `delVarEnv` bndr, setIdOccInfo bndr NoOccInfo)
904 alt_env = mkAltEnv env scrut bndr
905 occ_anal_alt = occAnalAlt alt_env bndr
907 occ_anal_scrut (Var v) (alt1 : other_alts)
908 | not (null other_alts) || not (isDefaultAlt alt1)
909 = (mkOneOcc env v True, Var v) -- The 'True' says that the variable occurs
910 -- in an interesting context; the case has
911 -- at least one non-default alternative
912 occ_anal_scrut scrut _alts
913 = occAnal (vanillaCtxt env) scrut -- No need for rhsCtxt
915 occAnal env (Let bind body)
916 = case occAnal env_body body of { (body_usage, body') ->
917 case occAnalBind env env_body bind body_usage of { (final_usage, new_binds) ->
918 (final_usage, mkLets new_binds body') }}
920 env_body = trimOccEnv env (bindersOf bind)
922 occAnalArgs :: OccEnv -> [CoreExpr] -> (UsageDetails, [CoreExpr])
924 = case mapAndUnzip (occAnal arg_env) args of { (arg_uds_s, args') ->
925 (foldr (+++) emptyDetails arg_uds_s, args')}
927 arg_env = vanillaCtxt env
930 Applications are dealt with specially because we want
931 the "build hack" to work.
935 -> (Expr CoreBndr, [Arg CoreBndr])
936 -> (UsageDetails, Expr CoreBndr)
937 occAnalApp env (Var fun, args)
938 = case args_stuff of { (args_uds, args') ->
940 final_args_uds = markManyIf (isRhsEnv env && is_exp) args_uds
941 -- We mark the free vars of the argument of a constructor or PAP
942 -- as "many", if it is the RHS of a let(rec).
943 -- This means that nothing gets inlined into a constructor argument
944 -- position, which is what we want. Typically those constructor
945 -- arguments are just variables, or trivial expressions.
947 -- This is the *whole point* of the isRhsEnv predicate
949 (fun_uds +++ final_args_uds, mkApps (Var fun) args') }
951 fun_uniq = idUnique fun
952 fun_uds = mkOneOcc env fun (valArgCount args > 0)
953 is_exp = isExpandableApp fun (valArgCount args)
954 -- See Note [CONLIKE pragma] in BasicTypes
955 -- The definition of is_exp should match that in
956 -- Simplify.prepareRhs
958 -- Hack for build, fold, runST
959 args_stuff | fun_uniq == buildIdKey = appSpecial env 2 [True,True] args
960 | fun_uniq == augmentIdKey = appSpecial env 2 [True,True] args
961 | fun_uniq == foldrIdKey = appSpecial env 3 [False,True] args
962 | fun_uniq == runSTRepIdKey = appSpecial env 2 [True] args
963 -- (foldr k z xs) may call k many times, but it never
964 -- shares a partial application of k; hence [False,True]
965 -- This means we can optimise
966 -- foldr (\x -> let v = ...x... in \y -> ...v...) z xs
967 -- by floating in the v
969 | otherwise = occAnalArgs env args
972 occAnalApp env (fun, args)
973 = case occAnal (addAppCtxt env args) fun of { (fun_uds, fun') ->
974 -- The addAppCtxt is a bit cunning. One iteration of the simplifier
975 -- often leaves behind beta redexs like
977 -- Here we would like to mark x,y as one-shot, and treat the whole
978 -- thing much like a let. We do this by pushing some True items
979 -- onto the context stack.
981 case occAnalArgs env args of { (args_uds, args') ->
983 final_uds = fun_uds +++ args_uds
985 (final_uds, mkApps fun' args') }}
988 markManyIf :: Bool -- If this is true
989 -> UsageDetails -- Then do markMany on this
991 markManyIf True uds = mapVarEnv markMany uds
992 markManyIf False uds = uds
995 -> Int -> CtxtTy -- Argument number, and context to use for it
997 -> (UsageDetails, [CoreExpr])
998 appSpecial env n ctxt args
1001 arg_env = vanillaCtxt env
1003 go _ [] = (emptyDetails, []) -- Too few args
1005 go 1 (arg:args) -- The magic arg
1006 = case occAnal (setCtxtTy arg_env ctxt) arg of { (arg_uds, arg') ->
1007 case occAnalArgs env args of { (args_uds, args') ->
1008 (arg_uds +++ args_uds, arg':args') }}
1011 = case occAnal arg_env arg of { (arg_uds, arg') ->
1012 case go (n-1) args of { (args_uds, args') ->
1013 (arg_uds +++ args_uds, arg':args') }}
1017 Note [Binders in case alternatives]
1018 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1020 case x of y { (a,b) -> f y }
1021 We treat 'a', 'b' as dead, because they don't physically occur in the
1022 case alternative. (Indeed, a variable is dead iff it doesn't occur in
1023 its scope in the output of OccAnal.) It really helps to know when
1024 binders are unused. See esp the call to isDeadBinder in
1025 Simplify.mkDupableAlt
1027 In this example, though, the Simplifier will bring 'a' and 'b' back to
1028 life, beause it binds 'y' to (a,b) (imagine got inlined and
1032 occAnalAlt :: OccEnv
1035 -> (UsageDetails, Alt IdWithOccInfo)
1036 occAnalAlt env case_bndr (con, bndrs, rhs)
1038 env' = trimOccEnv env bndrs
1040 case occAnal env' rhs of { (rhs_usage1, rhs1) ->
1042 proxies = getProxies env' case_bndr
1043 (rhs_usage2, rhs2) = foldrBag wrapProxy (rhs_usage1, rhs1) proxies
1044 (alt_usg, tagged_bndrs) = tagLamBinders rhs_usage2 bndrs
1045 bndrs' = tagged_bndrs -- See Note [Binders in case alternatives]
1047 (alt_usg, (con, bndrs', rhs2)) }
1049 wrapProxy :: ProxyBind -> (UsageDetails, CoreExpr) -> (UsageDetails, CoreExpr)
1050 wrapProxy (bndr, rhs_var, co) (body_usg, body)
1051 | not (bndr `usedIn` body_usg)
1054 = (body_usg' +++ rhs_usg, Let (NonRec tagged_bndr rhs) body)
1056 (body_usg', tagged_bndr) = tagBinder body_usg bndr
1057 rhs_usg = unitVarEnv rhs_var NoOccInfo -- We don't need exact info
1058 rhs = mkCoerceI co (Var rhs_var)
1062 %************************************************************************
1066 %************************************************************************
1070 = OccEnv { occ_encl :: !OccEncl -- Enclosing context information
1071 , occ_ctxt :: !CtxtTy -- Tells about linearity
1072 , occ_proxy :: ProxyEnv
1073 , occ_rule_fvs :: ImpRuleUsage }
1076 -----------------------------
1077 -- OccEncl is used to control whether to inline into constructor arguments
1079 -- x = (p,q) -- Don't inline p or q
1080 -- y = /\a -> (p a, q a) -- Still don't inline p or q
1081 -- z = f (p,q) -- Do inline p,q; it may make a rule fire
1082 -- So OccEncl tells enought about the context to know what to do when
1083 -- we encounter a contructor application or PAP.
1086 = OccRhs -- RHS of let(rec), albeit perhaps inside a type lambda
1087 -- Don't inline into constructor args here
1088 | OccVanilla -- Argument of function, body of lambda, scruintee of case etc.
1089 -- Do inline into constructor args here
1091 instance Outputable OccEncl where
1092 ppr OccRhs = ptext (sLit "occRhs")
1093 ppr OccVanilla = ptext (sLit "occVanilla")
1095 type CtxtTy = [Bool]
1098 -- True:ctxt Analysing a function-valued expression that will be
1099 -- applied just once
1101 -- False:ctxt Analysing a function-valued expression that may
1102 -- be applied many times; but when it is,
1103 -- the CtxtTy inside applies
1105 initOccEnv :: [CoreRule] -> OccEnv
1106 initOccEnv rules = OccEnv { occ_encl = OccVanilla
1108 , occ_proxy = PE emptyVarEnv emptyVarSet
1109 , occ_rule_fvs = findImpRuleUsage rules }
1111 vanillaCtxt :: OccEnv -> OccEnv
1112 vanillaCtxt env = env { occ_encl = OccVanilla, occ_ctxt = [] }
1114 rhsCtxt :: OccEnv -> OccEnv
1115 rhsCtxt env = env { occ_encl = OccRhs, occ_ctxt = [] }
1117 setCtxtTy :: OccEnv -> CtxtTy -> OccEnv
1118 setCtxtTy env ctxt = env { occ_ctxt = ctxt }
1120 isRhsEnv :: OccEnv -> Bool
1121 isRhsEnv (OccEnv { occ_encl = OccRhs }) = True
1122 isRhsEnv (OccEnv { occ_encl = OccVanilla }) = False
1124 oneShotGroup :: OccEnv -> [CoreBndr] -> [CoreBndr]
1125 -- The result binders have one-shot-ness set that they might not have had originally.
1126 -- This happens in (build (\cn -> e)). Here the occurrence analyser
1127 -- linearity context knows that c,n are one-shot, and it records that fact in
1128 -- the binder. This is useful to guide subsequent float-in/float-out tranformations
1130 oneShotGroup (OccEnv { occ_ctxt = ctxt }) bndrs
1133 go _ [] rev_bndrs = reverse rev_bndrs
1135 go (lin_ctxt:ctxt) (bndr:bndrs) rev_bndrs
1136 | isId bndr = go ctxt bndrs (bndr':rev_bndrs)
1138 bndr' | lin_ctxt = setOneShotLambda bndr
1141 go ctxt (bndr:bndrs) rev_bndrs = go ctxt bndrs (bndr:rev_bndrs)
1143 addAppCtxt :: OccEnv -> [Arg CoreBndr] -> OccEnv
1144 addAppCtxt env@(OccEnv { occ_ctxt = ctxt }) args
1145 = env { occ_ctxt = replicate (valArgCount args) True ++ ctxt }
1148 %************************************************************************
1152 %************************************************************************
1155 type ImpRuleUsage = NameEnv UsageDetails
1156 -- Maps an *imported* Id f to the UsageDetails for *local* Ids
1157 -- used on the RHS for a *local* rule for f.
1162 Consider this, where A.g is an imported Id
1165 {-# RULE "foo" forall x. A.g x = f x #-}
1167 Obviously there's a loop, but the danger is that the occurrence analyser
1168 will say that 'f' is not a loop breaker. Then the simplifier will
1171 and then gaily inline 'f'. Result infinite loop. More realistically,
1172 these kind of rules are generated when specialising imported INLINABLE Ids.
1174 Solution: treat an occurrence of A.g as an occurrence of all the local Ids
1175 that occur on the RULE's RHS. This mapping from imported Id to local Ids
1176 is held in occ_rule_fvs.
1179 findImpRuleUsage :: [CoreRule] -> ImpRuleUsage
1180 -- Find the *local* Ids that can be reached transitively,
1181 -- via local rules, from each *imported* Id.
1182 -- Sigh: this function seems more complicated than it is really worth
1183 findImpRuleUsage rules
1184 = mkNameEnv [ (f, mapUFM (\_ -> NoOccInfo) ls)
1186 , let ls = find_lcl_deps f
1187 , not (isEmptyVarSet ls) ]
1189 rule_names = map ru_fn rules
1190 rule_name_set = mkNameSet rule_names
1192 imp_deps :: NameEnv VarSet
1193 -- (f,g) means imported Id 'g' appears in RHS of
1194 -- rule for imported Id 'f', *or* does so transitively
1195 imp_deps = foldr add_imp emptyNameEnv rules
1196 add_imp rule acc = extendNameEnv_C unionVarSet acc (ru_fn rule)
1197 (exprSomeFreeVars keep_imp (ru_rhs rule))
1198 keep_imp v = isId v && (idName v `elemNameSet` rule_name_set)
1199 full_imp_deps = transClosureFV (ufmToList imp_deps)
1201 lcl_deps :: NameEnv VarSet
1202 -- (f, l) means localId 'l' appears immediately
1203 -- in the RHS of a rule for imported Id 'f'
1204 -- Remember, many rules might have the same ru_fn
1205 -- so we do need to fold
1206 lcl_deps = foldr add_lcl emptyNameEnv rules
1207 add_lcl rule acc = extendNameEnv_C unionVarSet acc (ru_fn rule)
1208 (exprFreeIds (ru_rhs rule))
1210 find_lcl_deps :: Name -> VarSet
1212 = foldVarSet (unionVarSet . lookup_lcl . idName) (lookup_lcl f)
1213 (lookupNameEnv full_imp_deps f `orElse` emptyVarSet)
1214 lookup_lcl :: Name -> VarSet
1215 lookup_lcl g = lookupNameEnv lcl_deps g `orElse` emptyVarSet
1218 transClosureFV :: Uniquable a => [(a, VarSet)] -> UniqFM VarSet
1219 -- If (f,g), (g,h) are in the input, then (f,h) is in the output
1220 transClosureFV fv_list
1222 | otherwise = transClosureFV new_fv_list
1224 env = listToUFM fv_list
1225 (no_change, new_fv_list) = mapAccumL bump True fv_list
1226 bump no_change (b,fvs)
1227 | no_change_here = (no_change, (b,fvs))
1228 | otherwise = (False, (b,new_fvs))
1230 (new_fvs, no_change_here) = extendFvs env fvs
1233 extendFvs :: UniqFM VarSet -> VarSet -> (VarSet, Bool)
1234 -- (extendFVs env s) returns
1235 -- (s `union` env(s), env(s) `subset` s)
1237 = foldVarSet add (s, True) s
1239 add v (vs, no_change_so_far)
1240 = case lookupUFM env v of
1241 Just fvs | not (fvs `subVarSet` s)
1242 -> (vs `unionVarSet` fvs, False)
1243 _ -> (vs, no_change_so_far)
1247 %************************************************************************
1251 %************************************************************************
1255 = PE (IdEnv (Id, [(Id,CoercionI)])) VarSet
1256 -- Main env, and its free variables (of both range and domain)
1261 The ProxyEnv keeps track of the connection between case binders and
1262 scrutinee. Specifically, if
1263 sc |-> (sc, [...(cb, co)...])
1264 is a binding in the ProxyEnv, then
1266 Typically we add such a binding when encountering the case expression
1267 case (sc |> coi) of cb { ... }
1270 * The domain of the ProxyEnv is the variable (or casted variable)
1271 scrutinees of enclosing cases. This is additionally used
1272 to ensure we gather occurrence info even for GlobalId scrutinees;
1273 see Note [Binder swap for GlobalId scrutinee]
1275 * The ProxyEnv is just an optimisation; you can throw away any
1276 element without losing correctness. And we do so when pushing
1277 it inside a binding (see trimProxyEnv).
1279 * One scrutinee might map to many case binders: Eg
1280 case sc of cb1 { DEFAULT -> ....case sc of cb2 { ... } .. }
1283 * If sc1 |-> (sc2, [...(cb, co)...]), then sc1==sc2
1284 It's a UniqFM and we sometimes need the domain Id
1286 * Any particular case binder 'cb' occurs only once in entire range
1290 The Main Reason for having a ProxyEnv is so that when we encounter
1291 case e of cb { pi -> ri }
1292 we can find all the in-scope variables derivable from 'cb',
1293 and effectively add let-bindings for them (or at least for the
1294 ones *mentioned* in ri) thus:
1295 case e of cb { pi -> let { x = ..cb..; y = ...cb.. }
1297 In this way we'll replace occurrences of 'x', 'y' with 'cb',
1298 which implements the Binder-swap idea (see Note [Binder swap])
1300 The function getProxies finds these bindings; then we
1301 add just the necessary ones, using wrapProxy.
1305 We do these two transformations right here:
1307 (1) case x of b { pi -> ri }
1309 case x of b { pi -> let x=b in ri }
1311 (2) case (x |> co) of b { pi -> ri }
1313 case (x |> co) of b { pi -> let x = b |> sym co in ri }
1315 Why (2)? See Note [Case of cast]
1317 In both cases, in a particular alternative (pi -> ri), we only
1319 (a) x occurs free in (pi -> ri)
1320 (ie it occurs in ri, but is not bound in pi)
1321 (b) the pi does not bind b (or the free vars of co)
1322 We need (a) and (b) for the inserted binding to be correct.
1324 For the alternatives where we inject the binding, we can transfer
1325 all x's OccInfo to b. And that is the point.
1328 * The deliberate shadowing of 'x'.
1329 * That (a) rapidly becomes false, so no bindings are injected.
1331 The reason for doing these transformations here is because it allows
1332 us to adjust the OccInfo for 'x' and 'b' as we go.
1334 * Suppose the only occurrences of 'x' are the scrutinee and in the
1335 ri; then this transformation makes it occur just once, and hence
1336 get inlined right away.
1338 * If we do this in the Simplifier, we don't know whether 'x' is used
1339 in ri, so we are forced to pessimistically zap b's OccInfo even
1340 though it is typically dead (ie neither it nor x appear in the
1341 ri). There's nothing actually wrong with zapping it, except that
1342 it's kind of nice to know which variables are dead. My nose
1343 tells me to keep this information as robustly as possible.
1345 The Maybe (Id,CoreExpr) passed to occAnalAlt is the extra let-binding
1346 {x=b}; it's Nothing if the binder-swap doesn't happen.
1348 There is a danger though. Consider
1350 in case (f v) of w -> ...v...v...
1351 And suppose that (f v) expands to just v. Then we'd like to
1352 use 'w' instead of 'v' in the alternative. But it may be too
1353 late; we may have substituted the (cheap) x+#y for v in the
1354 same simplifier pass that reduced (f v) to v.
1356 I think this is just too bad. CSE will recover some of it.
1360 Consider case (x `cast` co) of b { I# ->
1361 ... (case (x `cast` co) of {...}) ...
1362 We'd like to eliminate the inner case. That is the motivation for
1363 equation (2) in Note [Binder swap]. When we get to the inner case, we
1364 inline x, cancel the casts, and away we go.
1366 Note [Binder swap on GlobalId scrutinees]
1367 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1368 When the scrutinee is a GlobalId we must take care in two ways
1370 i) In order to *know* whether 'x' occurs free in the RHS, we need its
1371 occurrence info. BUT, we don't gather occurrence info for
1372 GlobalIds. That's one use for the (small) occ_proxy env in OccEnv is
1373 for: it says "gather occurrence info for these.
1375 ii) We must call localiseId on 'x' first, in case it's a GlobalId, or
1376 has an External Name. See, for example, SimplEnv Note [Global Ids in
1379 Note [getProxies is subtle]
1380 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
1381 The code for getProxies isn't all that obvious. Consider
1383 case v |> cov of x { DEFAULT ->
1384 case x |> cox1 of y { DEFAULT ->
1385 case x |> cox2 of z { DEFAULT -> r
1387 These will give us a ProxyEnv looking like:
1388 x |-> (x, [(y, cox1), (z, cox2)])
1389 v |-> (v, [(x, cov)])
1391 From this we want to extract the bindings
1396 Notice that later bindings may mention earlier ones, and that
1397 we need to go "both ways".
1399 Historical note [no-case-of-case]
1400 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1401 We *used* to suppress the binder-swap in case expressions when
1402 -fno-case-of-case is on. Old remarks:
1403 "This happens in the first simplifier pass,
1404 and enhances full laziness. Here's the bad case:
1405 f = \ y -> ...(case x of I# v -> ...(case x of ...) ... )
1406 If we eliminate the inner case, we trap it inside the I# v -> arm,
1407 which might prevent some full laziness happening. I've seen this
1408 in action in spectral/cichelli/Prog.hs:
1409 [(m,n) | m <- [1..max], n <- [1..max]]
1410 Hence the check for NoCaseOfCase."
1411 However, now the full-laziness pass itself reverses the binder-swap, so this
1412 check is no longer necessary.
1414 Historical note [Suppressing the case binder-swap]
1415 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1416 This old note describes a problem that is also fixed by doing the
1417 binder-swap in OccAnal:
1419 There is another situation when it might make sense to suppress the
1420 case-expression binde-swap. If we have
1422 case x of w1 { DEFAULT -> case x of w2 { A -> e1; B -> e2 }
1423 ...other cases .... }
1425 We'll perform the binder-swap for the outer case, giving
1427 case x of w1 { DEFAULT -> case w1 of w2 { A -> e1; B -> e2 }
1428 ...other cases .... }
1430 But there is no point in doing it for the inner case, because w1 can't
1431 be inlined anyway. Furthermore, doing the case-swapping involves
1432 zapping w2's occurrence info (see paragraphs that follow), and that
1433 forces us to bind w2 when doing case merging. So we get
1435 case x of w1 { A -> let w2 = w1 in e1
1436 B -> let w2 = w1 in e2
1437 ...other cases .... }
1439 This is plain silly in the common case where w2 is dead.
1441 Even so, I can't see a good way to implement this idea. I tried
1442 not doing the binder-swap if the scrutinee was already evaluated
1443 but that failed big-time:
1447 case v of w { MkT x ->
1448 case x of x1 { I# y1 ->
1449 case x of x2 { I# y2 -> ...
1451 Notice that because MkT is strict, x is marked "evaluated". But to
1452 eliminate the last case, we must either make sure that x (as well as
1453 x1) has unfolding MkT y1. THe straightforward thing to do is to do
1454 the binder-swap. So this whole note is a no-op.
1456 It's fixed by doing the binder-swap in OccAnal because we can do the
1457 binder-swap unconditionally and still get occurrence analysis
1461 extendProxyEnv :: ProxyEnv -> Id -> CoercionI -> Id -> ProxyEnv
1462 -- (extendPE x co y) typically arises from
1463 -- case (x |> co) of y { ... }
1464 -- It extends the proxy env with the binding
1466 extendProxyEnv pe scrut co case_bndr
1467 | scrut == case_bndr = PE env1 fvs1 -- If case_bndr shadows scrut,
1468 | otherwise = PE env2 fvs2 -- don't extend
1470 PE env1 fvs1 = trimProxyEnv pe [case_bndr]
1471 env2 = extendVarEnv_Acc add single env1 scrut1 (case_bndr,co)
1472 single cb_co = (scrut1, [cb_co])
1473 add cb_co (x, cb_cos) = (x, cb_co:cb_cos)
1474 fvs2 = fvs1 `unionVarSet` freeVarsCoI co
1475 `extendVarSet` case_bndr
1476 `extendVarSet` scrut1
1478 scrut1 = mkLocalId (localiseName (idName scrut)) (idType scrut)
1479 -- Localise the scrut_var before shadowing it; we're making a
1480 -- new binding for it, and it might have an External Name, or
1481 -- even be a GlobalId; Note [Binder swap on GlobalId scrutinees]
1482 -- Also we don't want any INLILNE or NOINLINE pragmas!
1485 type ProxyBind = (Id, Id, CoercionI)
1487 getProxies :: OccEnv -> Id -> Bag ProxyBind
1488 -- Return a bunch of bindings [...(xi,ei)...]
1489 -- such that let { ...; xi=ei; ... } binds the xi using y alone
1490 -- See Note [getProxies is subtle]
1491 getProxies (OccEnv { occ_proxy = PE pe _ }) case_bndr
1492 = -- pprTrace "wrapProxies" (ppr case_bndr) $
1495 fwd_pe :: IdEnv (Id, CoercionI)
1496 fwd_pe = foldVarEnv add1 emptyVarEnv pe
1498 add1 (x,ycos) env = foldr (add2 x) env ycos
1499 add2 x (y,co) env = extendVarEnv env y (x,co)
1501 go_fwd :: Id -> Bag ProxyBind
1502 -- Return bindings derivable from case_bndr
1503 go_fwd case_bndr = -- pprTrace "go_fwd" (vcat [ppr case_bndr, text "fwd_pe =" <+> ppr fwd_pe,
1504 -- text "pe =" <+> ppr pe]) $
1508 | Just (scrut, co) <- lookupVarEnv fwd_pe case_bndr
1509 = unitBag (scrut, case_bndr, mkSymCoI co)
1510 `unionBags` go_fwd scrut
1511 `unionBags` go_bwd scrut [pr | pr@(cb,_) <- lookup_bwd scrut
1516 lookup_bwd :: Id -> [(Id, CoercionI)]
1517 -- Return case_bndrs that are connected to scrut
1518 lookup_bwd scrut = case lookupVarEnv pe scrut of
1520 Just (_, cb_cos) -> cb_cos
1522 go_bwd :: Id -> [(Id, CoercionI)] -> Bag ProxyBind
1523 go_bwd scrut cb_cos = foldr (unionBags . go_bwd1 scrut) emptyBag cb_cos
1525 go_bwd1 :: Id -> (Id, CoercionI) -> Bag ProxyBind
1526 go_bwd1 scrut (case_bndr, co)
1527 = -- pprTrace "go_bwd1" (ppr case_bndr) $
1528 unitBag (case_bndr, scrut, co)
1529 `unionBags` go_bwd case_bndr (lookup_bwd case_bndr)
1532 mkAltEnv :: OccEnv -> CoreExpr -> Id -> OccEnv
1533 -- Does two things: a) makes the occ_ctxt = OccVanilla
1534 -- b) extends the ProxyEnv if possible
1535 mkAltEnv env scrut cb
1536 = env { occ_encl = OccVanilla, occ_proxy = pe' }
1540 Var v -> extendProxyEnv pe v (IdCo (idType v)) cb
1541 Cast (Var v) co -> extendProxyEnv pe v (ACo co) cb
1542 _other -> trimProxyEnv pe [cb]
1545 trimOccEnv :: OccEnv -> [CoreBndr] -> OccEnv
1546 trimOccEnv env bndrs = env { occ_proxy = trimProxyEnv (occ_proxy env) bndrs }
1549 trimProxyEnv :: ProxyEnv -> [CoreBndr] -> ProxyEnv
1550 -- We are about to push this ProxyEnv inside a binding for 'bndrs'
1551 -- So dump any ProxyEnv bindings which mention any of the bndrs
1552 trimProxyEnv (PE pe fvs) bndrs
1553 | not (bndr_set `intersectsVarSet` fvs)
1556 = PE pe' (fvs `minusVarSet` bndr_set)
1558 pe' = mapVarEnv trim pe
1559 bndr_set = mkVarSet bndrs
1560 trim (scrut, cb_cos) | scrut `elemVarSet` bndr_set = (scrut, [])
1561 | otherwise = (scrut, filterOut discard cb_cos)
1562 discard (cb,co) = bndr_set `intersectsVarSet`
1563 extendVarSet (freeVarsCoI co) cb
1566 freeVarsCoI :: CoercionI -> VarSet
1567 freeVarsCoI (IdCo t) = tyVarsOfType t
1568 freeVarsCoI (ACo co) = tyVarsOfType co
1572 %************************************************************************
1574 \subsection[OccurAnal-types]{OccEnv}
1576 %************************************************************************
1579 type UsageDetails = IdEnv OccInfo -- A finite map from ids to their usage
1580 -- INVARIANT: never IAmDead
1581 -- (Deadness is signalled by not being in the map at all)
1583 (+++), combineAltsUsageDetails
1584 :: UsageDetails -> UsageDetails -> UsageDetails
1587 = plusVarEnv_C addOccInfo usage1 usage2
1589 combineAltsUsageDetails usage1 usage2
1590 = plusVarEnv_C orOccInfo usage1 usage2
1592 addOneOcc :: UsageDetails -> Id -> OccInfo -> UsageDetails
1593 addOneOcc usage id info
1594 = plusVarEnv_C addOccInfo usage (unitVarEnv id info)
1595 -- ToDo: make this more efficient
1597 emptyDetails :: UsageDetails
1598 emptyDetails = (emptyVarEnv :: UsageDetails)
1600 usedIn :: Id -> UsageDetails -> Bool
1601 v `usedIn` details = isExportedId v || v `elemVarEnv` details
1603 type IdWithOccInfo = Id
1605 tagLamBinders :: UsageDetails -- Of scope
1607 -> (UsageDetails, -- Details with binders removed
1608 [IdWithOccInfo]) -- Tagged binders
1609 -- Used for lambda and case binders
1610 -- It copes with the fact that lambda bindings can have InlineRule
1611 -- unfoldings, used for join points
1612 tagLamBinders usage binders = usage' `seq` (usage', bndrs')
1614 (usage', bndrs') = mapAccumR tag_lam usage binders
1615 tag_lam usage bndr = (usage2, setBinderOcc usage bndr)
1617 usage1 = usage `delVarEnv` bndr
1618 usage2 | isId bndr = addIdOccs usage1 (idUnfoldingVars bndr)
1619 | otherwise = usage1
1621 tagBinder :: UsageDetails -- Of scope
1623 -> (UsageDetails, -- Details with binders removed
1624 IdWithOccInfo) -- Tagged binders
1626 tagBinder usage binder
1628 usage' = usage `delVarEnv` binder
1629 binder' = setBinderOcc usage binder
1631 usage' `seq` (usage', binder')
1633 setBinderOcc :: UsageDetails -> CoreBndr -> CoreBndr
1634 setBinderOcc usage bndr
1635 | isTyCoVar bndr = bndr
1636 | isExportedId bndr = case idOccInfo bndr of
1638 _ -> setIdOccInfo bndr NoOccInfo
1639 -- Don't use local usage info for visible-elsewhere things
1640 -- BUT *do* erase any IAmALoopBreaker annotation, because we're
1641 -- about to re-generate it and it shouldn't be "sticky"
1643 | otherwise = setIdOccInfo bndr occ_info
1645 occ_info = lookupVarEnv usage bndr `orElse` IAmDead
1649 %************************************************************************
1651 \subsection{Operations over OccInfo}
1653 %************************************************************************
1656 mkOneOcc :: OccEnv -> Id -> InterestingCxt -> UsageDetails
1657 mkOneOcc env id int_cxt
1658 | isLocalId id = unitVarEnv id (OneOcc False True int_cxt)
1659 | PE env _ <- occ_proxy env
1660 , id `elemVarEnv` env = unitVarEnv id NoOccInfo
1661 | Just uds <- lookupNameEnv (occ_rule_fvs env) (idName id)
1663 | otherwise = emptyDetails
1665 markMany, markInsideLam, markInsideSCC :: OccInfo -> OccInfo
1667 markMany _ = NoOccInfo
1669 markInsideSCC occ = markMany occ
1671 markInsideLam (OneOcc _ one_br int_cxt) = OneOcc True one_br int_cxt
1672 markInsideLam occ = occ
1674 addOccInfo, orOccInfo :: OccInfo -> OccInfo -> OccInfo
1676 addOccInfo a1 a2 = ASSERT( not (isDeadOcc a1 || isDeadOcc a2) )
1677 NoOccInfo -- Both branches are at least One
1678 -- (Argument is never IAmDead)
1680 -- (orOccInfo orig new) is used
1681 -- when combining occurrence info from branches of a case
1683 orOccInfo (OneOcc in_lam1 _ int_cxt1)
1684 (OneOcc in_lam2 _ int_cxt2)
1685 = OneOcc (in_lam1 || in_lam2)
1686 False -- False, because it occurs in both branches
1687 (int_cxt1 && int_cxt2)
1688 orOccInfo a1 a2 = ASSERT( not (isDeadOcc a1 || isDeadOcc a2) )