2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[FloatOut]{Float bindings outwards (towards the top level)}
6 ``Long-distance'' floating of bindings towards the top level.
9 module FloatOut ( floatOutwards ) where
11 #include "HsVersions.h"
14 import CoreUtils ( mkSCC )
16 import CmdLineOpts ( DynFlags, DynFlag(..), dopt )
17 import ErrUtils ( dumpIfSet_dyn )
18 import CostCentre ( dupifyCC, CostCentre )
21 import CoreLint ( showPass, endPass )
22 import SetLevels ( setLevels,
23 Level(..), tOP_LEVEL, ltMajLvl, ltLvl, isTopLvl
25 import UniqSupply ( UniqSupply )
26 import List ( partition )
33 At the moment we never float a binding out to between two adjacent
37 \x y -> let t = x+x in ...
39 \x -> let t = x+x in \y -> ...
41 Reason: this is less efficient in the case where the original lambda
42 is never partially applied.
44 But there's a case I've seen where this might not be true. Consider:
50 elem' x (y:ys) = x==y || elem' x ys
52 It turns out that this generates a subexpression of the form
54 \deq x ys -> let eq = eqFromEqDict deq in ...
56 vwhich might usefully be separated to
58 \deq -> let eq = eqFromEqDict deq in \xy -> ...
60 Well, maybe. We don't do this at the moment.
63 type LevelledExpr = TaggedExpr Level
64 type LevelledBind = TaggedBind Level
65 type FloatBind = (Level, CoreBind)
66 type FloatBinds = [FloatBind]
69 %************************************************************************
71 \subsection[floatOutwards]{@floatOutwards@: let-floating interface function}
73 %************************************************************************
76 floatOutwards :: DynFlags
77 -> Bool -- True <=> float lambdas to top level
79 -> [CoreBind] -> IO [CoreBind]
81 floatOutwards dflags float_lams us pgm
83 showPass dflags float_msg ;
85 let { annotated_w_levels = setLevels float_lams pgm us ;
86 (fss, binds_s') = unzip (map floatTopBind annotated_w_levels)
89 dumpIfSet_dyn dflags Opt_D_verbose_core2core "Levels added:"
90 (vcat (map ppr annotated_w_levels));
92 let { (tlets, ntlets, lams) = get_stats (sum_stats fss) };
94 dumpIfSet_dyn dflags Opt_D_dump_simpl_stats "FloatOut stats:"
95 (hcat [ int tlets, ptext SLIT(" Lets floated to top level; "),
96 int ntlets, ptext SLIT(" Lets floated elsewhere; from "),
97 int lams, ptext SLIT(" Lambda groups")]);
99 endPass dflags float_msg Opt_D_verbose_core2core (concat binds_s')
100 {- no specific flag for dumping float-out -}
103 float_msg | float_lams = "Float out (floating lambdas too)"
104 | otherwise = "Float out (not floating lambdas)"
106 floatTopBind bind@(NonRec _ _)
107 = case (floatBind emptyVarEnv tOP_LEVEL bind) of { (fs, floats, bind', _) ->
108 (fs, floatsToBinds floats ++ [bind'])
111 floatTopBind bind@(Rec _)
112 = case (floatBind emptyVarEnv tOP_LEVEL bind) of { (fs, floats, Rec pairs', _) ->
113 -- Actually floats will be empty
114 --false:ASSERT(null floats)
115 (fs, [Rec (floatsToBindPairs floats ++ pairs')])
119 %************************************************************************
121 \subsection[FloatOut-Bind]{Floating in a binding (the business end)}
123 %************************************************************************
127 floatBind :: IdEnv Level
130 -> (FloatStats, FloatBinds, CoreBind, IdEnv Level)
132 floatBind env lvl (NonRec (name,level) rhs)
133 = case (floatRhs env level rhs) of { (fs, rhs_floats, rhs') ->
136 extendVarEnv env name level)
139 floatBind env lvl bind@(Rec pairs)
140 = case (unzip3 (map do_pair pairs)) of { (fss, rhss_floats, new_pairs) ->
142 if not (isTopLvl bind_level) then
144 (sum_stats fss, concat rhss_floats, Rec new_pairs, new_env)
146 {- In a recursive binding, destined for the top level (only),
147 the rhs floats may contain
148 references to the bound things. For example
150 f = ...(let v = ...f... in b) ...
157 and hence we must (pessimistically) make all the floats recursive
158 with the top binding. Later dependency analysis will unravel it.
163 Rec (new_pairs ++ floatsToBindPairs (concat rhss_floats)),
168 new_env = extendVarEnvList env (map fst pairs)
170 bind_level = getBindLevel bind
172 do_pair ((name, level), rhs)
173 = case (floatRhs new_env level rhs) of { (fs, rhs_floats, rhs') ->
174 (fs, rhs_floats, (name, rhs'))
178 %************************************************************************
180 \subsection[FloatOut-Expr]{Floating in expressions}
182 %************************************************************************
189 -> (FloatStats, FloatBinds, CoreExpr)
192 = case (floatExpr env lvl arg) of { (fsa, floats, arg') ->
193 case (partitionByMajorLevel lvl floats) of { (floats', heres) ->
194 -- Dump bindings that aren't going to escape from a lambda
195 -- This is to avoid floating the x binding out of
196 -- f (let x = e in b)
197 -- unnecessarily. It even causes a bug to do so if we have
198 -- y = writeArr# a n (let x = e in b)
199 -- because the y binding is an expr-ok-for-speculation one.
200 (fsa, floats', install heres arg') }}
202 floatExpr env _ (Var v) = (zeroStats, [], Var v)
203 floatExpr env _ (Type ty) = (zeroStats, [], Type ty)
204 floatExpr env _ (Lit lit) = (zeroStats, [], Lit lit)
206 floatExpr env lvl (App e a)
207 = case (floatExpr env lvl e) of { (fse, floats_e, e') ->
208 case (floatRhs env lvl a) of { (fsa, floats_a, a') ->
209 (fse `add_stats` fsa, floats_e ++ floats_a, App e' a') }}
211 floatExpr env lvl (Lam (tv,incd_lvl) e)
213 = case (floatExpr env incd_lvl e) of { (fs, floats, e') ->
215 -- Dump any bindings which absolutely cannot go any further
216 case (partitionByLevel incd_lvl floats) of { (floats', heres) ->
218 (fs, floats', Lam tv (install heres e'))
221 floatExpr env lvl (Lam (arg,incd_lvl) rhs)
224 new_env = extendVarEnv env arg incd_lvl
226 case (floatExpr new_env incd_lvl rhs) of { (fs, floats, rhs') ->
228 -- Dump any bindings which absolutely cannot go any further
229 case (partitionByLevel incd_lvl floats) of { (floats', heres) ->
231 (add_to_stats fs floats',
233 Lam arg (install heres rhs'))
236 floatExpr env lvl (Note note@(SCC cc) expr)
237 = case (floatExpr env lvl expr) of { (fs, floating_defns, expr') ->
239 -- Annotate bindings floated outwards past an scc expression
240 -- with the cc. We mark that cc as "duplicated", though.
242 annotated_defns = annotate (dupifyCC cc) floating_defns
244 (fs, annotated_defns, Note note expr') }
246 annotate :: CostCentre -> FloatBinds -> FloatBinds
248 annotate dupd_cc defn_groups
249 = [ (level, ann_bind floater) | (level, floater) <- defn_groups ]
251 ann_bind (NonRec binder rhs)
252 = NonRec binder (mkSCC dupd_cc rhs)
255 = Rec [(binder, mkSCC dupd_cc rhs) | (binder, rhs) <- pairs]
257 -- At one time I tried the effect of not float anything out of an InlineMe,
258 -- but it sometimes works badly. For example, consider PrelArr.done. It
259 -- has the form __inline (\d. e)
260 -- where e doesn't mention d. If we float this to
261 -- __inline (let x = e in \d. x)
262 -- things are bad. The inliner doesn't even inline it because it doesn't look
263 -- like a head-normal form. So it seems a lesser evil to let things float.
264 -- In SetLevels we do set the context to (Level 0 0) when we get to an InlineMe
265 -- which discourages floating out.
267 floatExpr env lvl (Note note expr) -- Other than SCCs
268 = case (floatExpr env lvl expr) of { (fs, floating_defns, expr') ->
269 (fs, floating_defns, Note note expr') }
271 floatExpr env lvl (Let bind body)
272 = case (floatBind env lvl bind) of { (fsb, rhs_floats, bind', new_env) ->
273 case (floatExpr new_env lvl body) of { (fse, body_floats, body') ->
275 rhs_floats ++ [(bind_lvl, bind')] ++ body_floats,
279 bind_lvl = getBindLevel bind
281 floatExpr env lvl (Case scrut (case_bndr, case_lvl) alts)
282 = case floatExpr env lvl scrut of { (fse, fde, scrut') ->
283 case floatList float_alt alts of { (fsa, fda, alts') ->
284 (add_stats fse fsa, fda ++ fde, Case scrut' case_bndr alts')
287 alts_env = extendVarEnv env case_bndr case_lvl
289 partition_fn = partitionByMajorLevel
291 float_alt (con, bs, rhs)
294 new_env = extendVarEnvList alts_env bs
296 case (floatExpr new_env case_lvl rhs) of { (fs, rhs_floats, rhs') ->
297 case (partition_fn case_lvl rhs_floats) of { (rhs_floats', heres) ->
298 (fs, rhs_floats', (con, bs', install heres rhs')) }}
301 floatList :: (a -> (FloatStats, FloatBinds, b)) -> [a] -> (FloatStats, FloatBinds, [b])
302 floatList f [] = (zeroStats, [], [])
303 floatList f (a:as) = case f a of { (fs_a, binds_a, b) ->
304 case floatList f as of { (fs_as, binds_as, bs) ->
305 (fs_a `add_stats` fs_as, binds_a ++ binds_as, b:bs) }}
308 %************************************************************************
310 \subsection{Utility bits for floating stats}
312 %************************************************************************
314 I didn't implement this with unboxed numbers. I don't want to be too
315 strict in this stuff, as it is rarely turned on. (WDP 95/09)
319 = FlS Int -- Number of top-floats * lambda groups they've been past
320 Int -- Number of non-top-floats * lambda groups they've been past
321 Int -- Number of lambda (groups) seen
323 get_stats (FlS a b c) = (a, b, c)
325 zeroStats = FlS 0 0 0
327 sum_stats xs = foldr add_stats zeroStats xs
329 add_stats (FlS a1 b1 c1) (FlS a2 b2 c2)
330 = FlS (a1 + a2) (b1 + b2) (c1 + c2)
332 add_to_stats (FlS a b c) floats
333 = FlS (a + length top_floats) (b + length other_floats) (c + 1)
335 (top_floats, other_floats) = partition to_very_top floats
337 to_very_top (my_lvl, _) = isTopLvl my_lvl
341 %************************************************************************
343 \subsection{Utility bits for floating}
345 %************************************************************************
348 getBindLevel (NonRec (_, lvl) _) = lvl
349 getBindLevel (Rec (((_,lvl), _) : _)) = lvl
353 partitionByMajorLevel, partitionByLevel
354 :: Level -- Partitioning level
356 -> FloatBinds -- Defns to be divided into 2 piles...
358 -> (FloatBinds, -- Defns with level strictly < partition level,
359 FloatBinds) -- The rest
362 partitionByMajorLevel ctxt_lvl defns
363 = partition float_further defns
365 -- Float it if we escape a value lambda,
366 -- or if we get to the top level
367 float_further (my_lvl, bind) = my_lvl `ltMajLvl` ctxt_lvl || isTopLvl my_lvl
368 -- The isTopLvl part says that if we can get to the top level, say "yes" anyway
374 -- which is as it should be
376 partitionByLevel ctxt_lvl defns
377 = partition float_further defns
379 float_further (my_lvl, _) = my_lvl `ltLvl` ctxt_lvl
383 floatsToBinds :: FloatBinds -> [CoreBind]
384 floatsToBinds floats = map snd floats
386 floatsToBindPairs :: FloatBinds -> [(Id,CoreExpr)]
388 floatsToBindPairs floats = concat (map mk_pairs floats)
390 mk_pairs (_, Rec pairs) = pairs
391 mk_pairs (_, NonRec binder rhs) = [(binder,rhs)]
393 install :: FloatBinds -> CoreExpr -> CoreExpr
395 install defn_groups expr
396 = foldr install_group expr defn_groups
398 install_group (_, defns) body = Let defns body