7f7b921fa86f2351f70493cf8baf60fb1c67ab4d
[ghc.git] / compiler / simplCore / FloatOut.hs
1 {-
2 (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3
4 \section[FloatOut]{Float bindings outwards (towards the top level)}
5
6 ``Long-distance'' floating of bindings towards the top level.
7 -}
8
9 {-# LANGUAGE CPP #-}
10 {-# OPTIONS_GHC -fno-warn-orphans #-}
11
12 module FloatOut ( floatOutwards ) where
13
14 import CoreSyn
15 import CoreUtils
16 import MkCore
17 import CoreArity ( etaExpand )
18 import CoreMonad ( FloatOutSwitches(..) )
19
20 import DynFlags
21 import ErrUtils ( dumpIfSet_dyn )
22 import Id ( Id, idArity, isBottomingId )
23 import Var ( Var )
24 import SetLevels
25 import UniqSupply ( UniqSupply )
26 import Bag
27 import Util
28 import Maybes
29 import Outputable
30 import FastString
31 import qualified Data.IntMap as M
32
33 #include "HsVersions.h"
34
35 {-
36 -----------------
37 Overall game plan
38 -----------------
39
40 The Big Main Idea is:
41
42 To float out sub-expressions that can thereby get outside
43 a non-one-shot value lambda, and hence may be shared.
44
45
46 To achieve this we may need to do two thing:
47
48 a) Let-bind the sub-expression:
49
50 f (g x) ==> let lvl = f (g x) in lvl
51
52 Now we can float the binding for 'lvl'.
53
54 b) More than that, we may need to abstract wrt a type variable
55
56 \x -> ... /\a -> let v = ...a... in ....
57
58 Here the binding for v mentions 'a' but not 'x'. So we
59 abstract wrt 'a', to give this binding for 'v':
60
61 vp = /\a -> ...a...
62 v = vp a
63
64 Now the binding for vp can float out unimpeded.
65 I can't remember why this case seemed important enough to
66 deal with, but I certainly found cases where important floats
67 didn't happen if we did not abstract wrt tyvars.
68
69 With this in mind we can also achieve another goal: lambda lifting.
70 We can make an arbitrary (function) binding float to top level by
71 abstracting wrt *all* local variables, not just type variables, leaving
72 a binding that can be floated right to top level. Whether or not this
73 happens is controlled by a flag.
74
75
76 Random comments
77 ~~~~~~~~~~~~~~~
78
79 At the moment we never float a binding out to between two adjacent
80 lambdas. For example:
81
82 @
83 \x y -> let t = x+x in ...
84 ===>
85 \x -> let t = x+x in \y -> ...
86 @
87 Reason: this is less efficient in the case where the original lambda
88 is never partially applied.
89
90 But there's a case I've seen where this might not be true. Consider:
91 @
92 elEm2 x ys
93 = elem' x ys
94 where
95 elem' _ [] = False
96 elem' x (y:ys) = x==y || elem' x ys
97 @
98 It turns out that this generates a subexpression of the form
99 @
100 \deq x ys -> let eq = eqFromEqDict deq in ...
101 @
102 vwhich might usefully be separated to
103 @
104 \deq -> let eq = eqFromEqDict deq in \xy -> ...
105 @
106 Well, maybe. We don't do this at the moment.
107
108
109 ************************************************************************
110 * *
111 \subsection[floatOutwards]{@floatOutwards@: let-floating interface function}
112 * *
113 ************************************************************************
114 -}
115
116 floatOutwards :: FloatOutSwitches
117 -> DynFlags
118 -> UniqSupply
119 -> CoreProgram -> IO CoreProgram
120
121 floatOutwards float_sws dflags us pgm
122 = do {
123 let { annotated_w_levels = setLevels float_sws pgm us ;
124 (fss, binds_s') = unzip (map floatTopBind annotated_w_levels)
125 } ;
126
127 dumpIfSet_dyn dflags Opt_D_verbose_core2core "Levels added:"
128 (vcat (map ppr annotated_w_levels));
129
130 let { (tlets, ntlets, lams) = get_stats (sum_stats fss) };
131
132 dumpIfSet_dyn dflags Opt_D_dump_simpl_stats "FloatOut stats:"
133 (hcat [ int tlets, ptext (sLit " Lets floated to top level; "),
134 int ntlets, ptext (sLit " Lets floated elsewhere; from "),
135 int lams, ptext (sLit " Lambda groups")]);
136
137 return (bagToList (unionManyBags binds_s'))
138 }
139
140 floatTopBind :: LevelledBind -> (FloatStats, Bag CoreBind)
141 floatTopBind bind
142 = case (floatBind bind) of { (fs, floats, bind') ->
143 let float_bag = flattenTopFloats floats
144 in case bind' of
145 Rec prs -> (fs, unitBag (Rec (addTopFloatPairs float_bag prs)))
146 NonRec {} -> (fs, float_bag `snocBag` bind') }
147
148 {-
149 ************************************************************************
150 * *
151 \subsection[FloatOut-Bind]{Floating in a binding (the business end)}
152 * *
153 ************************************************************************
154 -}
155
156 floatBind :: LevelledBind -> (FloatStats, FloatBinds, CoreBind)
157 floatBind (NonRec (TB var _) rhs)
158 = case (floatExpr rhs) of { (fs, rhs_floats, rhs') ->
159
160 -- A tiresome hack:
161 -- see Note [Bottoming floats: eta expansion] in SetLevels
162 let rhs'' | isBottomingId var = etaExpand (idArity var) rhs'
163 | otherwise = rhs'
164
165 in (fs, rhs_floats, NonRec var rhs'') }
166
167 floatBind (Rec pairs)
168 = case floatList do_pair pairs of { (fs, rhs_floats, new_pairs) ->
169 (fs, rhs_floats, Rec (concat new_pairs)) }
170 where
171 do_pair (TB name spec, rhs)
172 | isTopLvl dest_lvl -- See Note [floatBind for top level]
173 = case (floatExpr rhs) of { (fs, rhs_floats, rhs') ->
174 (fs, emptyFloats, addTopFloatPairs (flattenTopFloats rhs_floats) [(name, rhs')])}
175 | otherwise -- Note [Floating out of Rec rhss]
176 = case (floatExpr rhs) of { (fs, rhs_floats, rhs') ->
177 case (partitionByLevel dest_lvl rhs_floats) of { (rhs_floats', heres) ->
178 case (splitRecFloats heres) of { (pairs, case_heres) ->
179 (fs, rhs_floats', (name, installUnderLambdas case_heres rhs') : pairs) }}}
180 where
181 dest_lvl = floatSpecLevel spec
182
183 splitRecFloats :: Bag FloatBind -> ([(Id,CoreExpr)], Bag FloatBind)
184 -- The "tail" begins with a case
185 -- See Note [Floating out of Rec rhss]
186 splitRecFloats fs
187 = go [] (bagToList fs)
188 where
189 go prs (FloatLet (NonRec b r) : fs) = go ((b,r):prs) fs
190 go prs (FloatLet (Rec prs') : fs) = go (prs' ++ prs) fs
191 go prs fs = (prs, listToBag fs)
192
193 installUnderLambdas :: Bag FloatBind -> CoreExpr -> CoreExpr
194 -- Note [Floating out of Rec rhss]
195 installUnderLambdas floats e
196 | isEmptyBag floats = e
197 | otherwise = go e
198 where
199 go (Lam b e) = Lam b (go e)
200 go e = install floats e
201
202 ---------------
203 floatList :: (a -> (FloatStats, FloatBinds, b)) -> [a] -> (FloatStats, FloatBinds, [b])
204 floatList _ [] = (zeroStats, emptyFloats, [])
205 floatList f (a:as) = case f a of { (fs_a, binds_a, b) ->
206 case floatList f as of { (fs_as, binds_as, bs) ->
207 (fs_a `add_stats` fs_as, binds_a `plusFloats` binds_as, b:bs) }}
208
209 {-
210 Note [Floating out of Rec rhss]
211 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
212 Consider Rec { f<1,0> = \xy. body }
213 From the body we may get some floats. The ones with level <1,0> must
214 stay here, since they may mention f. Ideally we'd like to make them
215 part of the Rec block pairs -- but we can't if there are any
216 FloatCases involved.
217
218 Nor is it a good idea to dump them in the rhs, but outside the lambda
219 f = case x of I# y -> \xy. body
220 because now f's arity might get worse, which is Not Good. (And if
221 there's an SCC around the RHS it might not get better again.
222 See Trac #5342.)
223
224 So, gruesomely, we split the floats into
225 * the outer FloatLets, which can join the Rec, and
226 * an inner batch starting in a FloatCase, which are then
227 pushed *inside* the lambdas.
228 This loses full-laziness the rare situation where there is a
229 FloatCase and a Rec interacting.
230
231 Note [floatBind for top level]
232 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
233 We may have a *nested* binding whose destination level is (FloatMe tOP_LEVEL), thus
234 letrec { foo <0,0> = .... (let bar<0,0> = .. in ..) .... }
235 The binding for bar will be in the "tops" part of the floating binds,
236 and thus not partioned by floatBody.
237
238 We could perhaps get rid of the 'tops' component of the floating binds,
239 but this case works just as well.
240
241
242 ************************************************************************
243
244 \subsection[FloatOut-Expr]{Floating in expressions}
245 * *
246 ************************************************************************
247 -}
248
249 floatBody :: Level
250 -> LevelledExpr
251 -> (FloatStats, FloatBinds, CoreExpr)
252
253 floatBody lvl arg -- Used rec rhss, and case-alternative rhss
254 = case (floatExpr arg) of { (fsa, floats, arg') ->
255 case (partitionByLevel lvl floats) of { (floats', heres) ->
256 -- Dump bindings are bound here
257 (fsa, floats', install heres arg') }}
258
259 -----------------
260 floatExpr :: LevelledExpr
261 -> (FloatStats, FloatBinds, CoreExpr)
262 floatExpr (Var v) = (zeroStats, emptyFloats, Var v)
263 floatExpr (Type ty) = (zeroStats, emptyFloats, Type ty)
264 floatExpr (Coercion co) = (zeroStats, emptyFloats, Coercion co)
265 floatExpr (Lit lit) = (zeroStats, emptyFloats, Lit lit)
266
267 floatExpr (App e a)
268 = case (floatExpr e) of { (fse, floats_e, e') ->
269 case (floatExpr a) of { (fsa, floats_a, a') ->
270 (fse `add_stats` fsa, floats_e `plusFloats` floats_a, App e' a') }}
271
272 floatExpr lam@(Lam (TB _ lam_spec) _)
273 = let (bndrs_w_lvls, body) = collectBinders lam
274 bndrs = [b | TB b _ <- bndrs_w_lvls]
275 bndr_lvl = floatSpecLevel lam_spec
276 -- All the binders have the same level
277 -- See SetLevels.lvlLamBndrs
278 in
279 case (floatBody bndr_lvl body) of { (fs, floats, body') ->
280 (add_to_stats fs floats, floats, mkLams bndrs body') }
281
282 floatExpr (Tick tickish expr)
283 | tickish `tickishScopesLike` SoftScope -- not scoped, can just float
284 = case (floatExpr expr) of { (fs, floating_defns, expr') ->
285 (fs, floating_defns, Tick tickish expr') }
286
287 | not (tickishCounts tickish) || tickishCanSplit tickish
288 = case (floatExpr expr) of { (fs, floating_defns, expr') ->
289 let -- Annotate bindings floated outwards past an scc expression
290 -- with the cc. We mark that cc as "duplicated", though.
291 annotated_defns = wrapTick (mkNoCount tickish) floating_defns
292 in
293 (fs, annotated_defns, Tick tickish expr') }
294
295 | otherwise
296 = pprPanic "floatExpr tick" (ppr tickish)
297
298 floatExpr (Cast expr co)
299 = case (floatExpr expr) of { (fs, floating_defns, expr') ->
300 (fs, floating_defns, Cast expr' co) }
301
302 floatExpr (Let bind body)
303 = case bind_spec of
304 FloatMe dest_lvl
305 -> case (floatBind bind) of { (fsb, bind_floats, bind') ->
306 case (floatExpr body) of { (fse, body_floats, body') ->
307 ( add_stats fsb fse
308 , bind_floats `plusFloats` unitLetFloat dest_lvl bind'
309 `plusFloats` body_floats
310 , body') }}
311
312 StayPut bind_lvl -- See Note [Avoiding unnecessary floating]
313 -> case (floatBind bind) of { (fsb, bind_floats, bind') ->
314 case (floatBody bind_lvl body) of { (fse, body_floats, body') ->
315 ( add_stats fsb fse
316 , bind_floats `plusFloats` body_floats
317 , Let bind' body') }}
318 where
319 bind_spec = case bind of
320 NonRec (TB _ s) _ -> s
321 Rec ((TB _ s, _) : _) -> s
322 Rec [] -> panic "floatExpr:rec"
323
324 floatExpr (Case scrut (TB case_bndr case_spec) ty alts)
325 = case case_spec of
326 FloatMe dest_lvl -- Case expression moves
327 | [(con@(DataAlt {}), bndrs, rhs)] <- alts
328 -> case floatExpr scrut of { (fse, fde, scrut') ->
329 case floatExpr rhs of { (fsb, fdb, rhs') ->
330 let
331 float = unitCaseFloat dest_lvl scrut'
332 case_bndr con [b | TB b _ <- bndrs]
333 in
334 (add_stats fse fsb, fde `plusFloats` float `plusFloats` fdb, rhs') }}
335 | otherwise
336 -> pprPanic "Floating multi-case" (ppr alts)
337
338 StayPut bind_lvl -- Case expression stays put
339 -> case floatExpr scrut of { (fse, fde, scrut') ->
340 case floatList (float_alt bind_lvl) alts of { (fsa, fda, alts') ->
341 (add_stats fse fsa, fda `plusFloats` fde, Case scrut' case_bndr ty alts')
342 }}
343 where
344 float_alt bind_lvl (con, bs, rhs)
345 = case (floatBody bind_lvl rhs) of { (fs, rhs_floats, rhs') ->
346 (fs, rhs_floats, (con, [b | TB b _ <- bs], rhs')) }
347
348 {-
349 Note [Avoiding unnecessary floating]
350 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
351 In general we want to avoid floating a let unnecessarily, because
352 it might worsen strictness:
353 let
354 x = ...(let y = e in y+y)....
355 Here y is demanded. If we float it outside the lazy 'x=..' then
356 we'd have to zap its demand info, and it may never be restored.
357
358 So at a 'let' we leave the binding right where the are unless
359 the binding will escape a value lambda, e.g.
360
361 (\x -> let y = fac 100 in y)
362
363 That's what the partitionByMajorLevel does in the floatExpr (Let ...)
364 case.
365
366 Notice, though, that we must take care to drop any bindings
367 from the body of the let that depend on the staying-put bindings.
368
369 We used instead to do the partitionByMajorLevel on the RHS of an '=',
370 in floatRhs. But that was quite tiresome. We needed to test for
371 values or trival rhss, because (in particular) we don't want to insert
372 new bindings between the "=" and the "\". E.g.
373 f = \x -> let <bind> in <body>
374 We do not want
375 f = let <bind> in \x -> <body>
376 (a) The simplifier will immediately float it further out, so we may
377 as well do so right now; in general, keeping rhss as manifest
378 values is good
379 (b) If a float-in pass follows immediately, it might add yet more
380 bindings just after the '='. And some of them might (correctly)
381 be strict even though the 'let f' is lazy, because f, being a value,
382 gets its demand-info zapped by the simplifier.
383 And even all that turned out to be very fragile, and broke
384 altogether when profiling got in the way.
385
386 So now we do the partition right at the (Let..) itself.
387
388 ************************************************************************
389 * *
390 \subsection{Utility bits for floating stats}
391 * *
392 ************************************************************************
393
394 I didn't implement this with unboxed numbers. I don't want to be too
395 strict in this stuff, as it is rarely turned on. (WDP 95/09)
396 -}
397
398 data FloatStats
399 = FlS Int -- Number of top-floats * lambda groups they've been past
400 Int -- Number of non-top-floats * lambda groups they've been past
401 Int -- Number of lambda (groups) seen
402
403 get_stats :: FloatStats -> (Int, Int, Int)
404 get_stats (FlS a b c) = (a, b, c)
405
406 zeroStats :: FloatStats
407 zeroStats = FlS 0 0 0
408
409 sum_stats :: [FloatStats] -> FloatStats
410 sum_stats xs = foldr add_stats zeroStats xs
411
412 add_stats :: FloatStats -> FloatStats -> FloatStats
413 add_stats (FlS a1 b1 c1) (FlS a2 b2 c2)
414 = FlS (a1 + a2) (b1 + b2) (c1 + c2)
415
416 add_to_stats :: FloatStats -> FloatBinds -> FloatStats
417 add_to_stats (FlS a b c) (FB tops others)
418 = FlS (a + lengthBag tops) (b + lengthBag (flattenMajor others)) (c + 1)
419
420 {-
421 ************************************************************************
422 * *
423 \subsection{Utility bits for floating}
424 * *
425 ************************************************************************
426
427 Note [Representation of FloatBinds]
428 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
429 The FloatBinds types is somewhat important. We can get very large numbers
430 of floating bindings, often all destined for the top level. A typical example
431 is x = [4,2,5,2,5, .... ]
432 Then we get lots of small expressions like (fromInteger 4), which all get
433 lifted to top level.
434
435 The trouble is that
436 (a) we partition these floating bindings *at every binding site*
437 (b) SetLevels introduces a new bindings site for every float
438 So we had better not look at each binding at each binding site!
439
440 That is why MajorEnv is represented as a finite map.
441
442 We keep the bindings destined for the *top* level separate, because
443 we float them out even if they don't escape a *value* lambda; see
444 partitionByMajorLevel.
445 -}
446
447 type FloatLet = CoreBind -- INVARIANT: a FloatLet is always lifted
448 type MajorEnv = M.IntMap MinorEnv -- Keyed by major level
449 type MinorEnv = M.IntMap (Bag FloatBind) -- Keyed by minor level
450
451 data FloatBinds = FB !(Bag FloatLet) -- Destined for top level
452 !MajorEnv -- Levels other than top
453 -- See Note [Representation of FloatBinds]
454
455 instance Outputable FloatBinds where
456 ppr (FB fbs defs)
457 = ptext (sLit "FB") <+> (braces $ vcat
458 [ ptext (sLit "tops =") <+> ppr fbs
459 , ptext (sLit "non-tops =") <+> ppr defs ])
460
461 flattenTopFloats :: FloatBinds -> Bag CoreBind
462 flattenTopFloats (FB tops defs)
463 = ASSERT2( isEmptyBag (flattenMajor defs), ppr defs )
464 tops
465
466 addTopFloatPairs :: Bag CoreBind -> [(Id,CoreExpr)] -> [(Id,CoreExpr)]
467 addTopFloatPairs float_bag prs
468 = foldrBag add prs float_bag
469 where
470 add (NonRec b r) prs = (b,r):prs
471 add (Rec prs1) prs2 = prs1 ++ prs2
472
473 flattenMajor :: MajorEnv -> Bag FloatBind
474 flattenMajor = M.fold (unionBags . flattenMinor) emptyBag
475
476 flattenMinor :: MinorEnv -> Bag FloatBind
477 flattenMinor = M.fold unionBags emptyBag
478
479 emptyFloats :: FloatBinds
480 emptyFloats = FB emptyBag M.empty
481
482 unitCaseFloat :: Level -> CoreExpr -> Id -> AltCon -> [Var] -> FloatBinds
483 unitCaseFloat (Level major minor) e b con bs
484 = FB emptyBag (M.singleton major (M.singleton minor (unitBag (FloatCase e b con bs))))
485
486 unitLetFloat :: Level -> FloatLet -> FloatBinds
487 unitLetFloat lvl@(Level major minor) b
488 | isTopLvl lvl = FB (unitBag b) M.empty
489 | otherwise = FB emptyBag (M.singleton major (M.singleton minor floats))
490 where
491 floats = unitBag (FloatLet b)
492
493 plusFloats :: FloatBinds -> FloatBinds -> FloatBinds
494 plusFloats (FB t1 l1) (FB t2 l2)
495 = FB (t1 `unionBags` t2) (l1 `plusMajor` l2)
496
497 plusMajor :: MajorEnv -> MajorEnv -> MajorEnv
498 plusMajor = M.unionWith plusMinor
499
500 plusMinor :: MinorEnv -> MinorEnv -> MinorEnv
501 plusMinor = M.unionWith unionBags
502
503 install :: Bag FloatBind -> CoreExpr -> CoreExpr
504 install defn_groups expr
505 = foldrBag wrapFloat expr defn_groups
506
507 partitionByLevel
508 :: Level -- Partitioning level
509 -> FloatBinds -- Defns to be divided into 2 piles...
510 -> (FloatBinds, -- Defns with level strictly < partition level,
511 Bag FloatBind) -- The rest
512
513 {-
514 -- ---- partitionByMajorLevel ----
515 -- Float it if we escape a value lambda,
516 -- *or* if we get to the top level
517 -- *or* if it's a case-float and its minor level is < current
518 --
519 -- If we can get to the top level, say "yes" anyway. This means that
520 -- x = f e
521 -- transforms to
522 -- lvl = e
523 -- x = f lvl
524 -- which is as it should be
525
526 partitionByMajorLevel (Level major _) (FB tops defns)
527 = (FB tops outer, heres `unionBags` flattenMajor inner)
528 where
529 (outer, mb_heres, inner) = M.splitLookup major defns
530 heres = case mb_heres of
531 Nothing -> emptyBag
532 Just h -> flattenMinor h
533 -}
534
535 partitionByLevel (Level major minor) (FB tops defns)
536 = (FB tops (outer_maj `plusMajor` M.singleton major outer_min),
537 here_min `unionBags` flattenMinor inner_min
538 `unionBags` flattenMajor inner_maj)
539
540 where
541 (outer_maj, mb_here_maj, inner_maj) = M.splitLookup major defns
542 (outer_min, mb_here_min, inner_min) = case mb_here_maj of
543 Nothing -> (M.empty, Nothing, M.empty)
544 Just min_defns -> M.splitLookup minor min_defns
545 here_min = mb_here_min `orElse` emptyBag
546
547 wrapTick :: Tickish Id -> FloatBinds -> FloatBinds
548 wrapTick t (FB tops defns)
549 = FB (mapBag wrap_bind tops) (M.map (M.map wrap_defns) defns)
550 where
551 wrap_defns = mapBag wrap_one
552
553 wrap_bind (NonRec binder rhs) = NonRec binder (maybe_tick rhs)
554 wrap_bind (Rec pairs) = Rec (mapSnd maybe_tick pairs)
555
556 wrap_one (FloatLet bind) = FloatLet (wrap_bind bind)
557 wrap_one (FloatCase e b c bs) = FloatCase (maybe_tick e) b c bs
558
559 maybe_tick e | exprIsHNF e = tickHNFArgs t e
560 | otherwise = mkTick t e
561 -- we don't need to wrap a tick around an HNF when we float it
562 -- outside a tick: that is an invariant of the tick semantics
563 -- Conversely, inlining of HNFs inside an SCC is allowed, and
564 -- indeed the HNF we're floating here might well be inlined back
565 -- again, and we don't want to end up with duplicate ticks.