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"
15 import CmdLineOpts ( opt_D_verbose_core2core, opt_D_dump_simpl_stats )
16 import ErrUtils ( dumpIfSet )
17 import CostCentre ( dupifyCC, CostCentre )
18 import Id ( Id, idType )
19 import Const ( isWHNFCon )
21 import CoreLint ( beginPass, endPass )
23 import SetLevels ( setLevels,
24 Level(..), tOP_LEVEL, ltMajLvl, ltLvl, isTopLvl
26 import BasicTypes ( Unused )
27 import Type ( isUnLiftedType )
29 import UniqSupply ( UniqSupply )
30 import List ( partition )
37 At the moment we never float a binding out to between two adjacent
41 \x y -> let t = x+x in ...
43 \x -> let t = x+x in \y -> ...
45 Reason: this is less efficient in the case where the original lambda
46 is never partially applied.
48 But there's a case I've seen where this might not be true. Consider:
54 elem' x (y:ys) = x==y || elem' x ys
56 It turns out that this generates a subexpression of the form
58 \deq x ys -> let eq = eqFromEqDict deq in ...
60 vwhich might usefully be separated to
62 \deq -> let eq = eqFromEqDict deq in \xy -> ...
64 Well, maybe. We don't do this at the moment.
67 type LevelledExpr = TaggedExpr Level
68 type LevelledBind = TaggedBind Level
69 type FloatBind = (Level, CoreBind)
70 type FloatBinds = [FloatBind]
73 %************************************************************************
75 \subsection[floatOutwards]{@floatOutwards@: let-floating interface function}
77 %************************************************************************
80 floatOutwards :: UniqSupply -> [CoreBind] -> IO [CoreBind]
84 beginPass "Float out";
86 let { annotated_w_levels = setLevels pgm us ;
87 (fss, binds_s') = unzip (map floatTopBind annotated_w_levels)
90 dumpIfSet opt_D_verbose_core2core "Levels added:"
91 (vcat (map ppr annotated_w_levels));
93 let { (tlets, ntlets, lams) = get_stats (sum_stats fss) };
95 dumpIfSet opt_D_dump_simpl_stats "FloatOut stats:"
96 (hcat [ int tlets, ptext SLIT(" Lets floated to top level; "),
97 int ntlets, ptext SLIT(" Lets floated elsewhere; from "),
98 int lams, ptext SLIT(" Lambda groups")]);
101 opt_D_verbose_core2core {- no specific flag for dumping float-out -}
105 floatTopBind bind@(NonRec _ _)
106 = case (floatBind emptyVarEnv tOP_LEVEL bind) of { (fs, floats, bind', _) ->
107 (fs, floatsToBinds floats ++ [bind'])
110 floatTopBind bind@(Rec _)
111 = case (floatBind emptyVarEnv tOP_LEVEL bind) of { (fs, floats, Rec pairs', _) ->
112 -- Actually floats will be empty
113 --false:ASSERT(null floats)
114 (fs, [Rec (floatsToBindPairs floats ++ pairs')])
118 %************************************************************************
120 \subsection[FloatOut-Bind]{Floating in a binding (the business end)}
122 %************************************************************************
126 floatBind :: IdEnv Level
129 -> (FloatStats, FloatBinds, CoreBind, IdEnv Level)
131 floatBind env lvl (NonRec (name,level) rhs)
132 = case (floatRhs env level rhs) of { (fs, rhs_floats, rhs') ->
135 extendVarEnv env name level)
138 floatBind env lvl bind@(Rec pairs)
139 = case (unzip3 (map do_pair pairs)) of { (fss, rhss_floats, new_pairs) ->
141 if not (isTopLvl bind_level) then
143 (sum_stats fss, concat rhss_floats, Rec new_pairs, new_env)
145 {- In a recursive binding, destined for the top level (only),
146 the rhs floats may contain
147 references to the bound things. For example
149 f = ...(let v = ...f... in b) ...
156 and hence we must (pessimistically) make all the floats recursive
157 with the top binding. Later dependency analysis will unravel it.
162 Rec (new_pairs ++ floatsToBindPairs (concat rhss_floats)),
167 new_env = extendVarEnvList env (map fst pairs)
169 bind_level = getBindLevel bind
171 do_pair ((name, level), rhs)
172 = case (floatRhs new_env level rhs) of { (fs, rhs_floats, rhs') ->
173 (fs, rhs_floats, (name, rhs'))
177 %************************************************************************
179 \subsection[FloatOut-Expr]{Floating in expressions}
181 %************************************************************************
188 -> (FloatStats, FloatBinds, CoreExpr)
191 = case (floatExpr env lvl arg) of { (fsa, floats, arg') ->
192 case (partitionByMajorLevel lvl floats) of { (floats', heres) ->
193 -- Dump bindings that aren't going to escape from a lambda
194 -- This is to avoid floating the x binding out of
195 -- f (let x = e in b)
196 -- unnecessarily. It even causes a bug to do so if we have
197 -- y = writeArr# a n (let x = e in b)
198 -- because the y binding is an expr-ok-for-speculation one.
199 (fsa, floats', install heres arg') }}
201 floatExpr env _ (Var v) = (zeroStats, [], Var v)
202 floatExpr env _ (Type ty) = (zeroStats, [], Type ty)
203 floatExpr env lvl (Con con as)
204 = case floatList (floatRhs env lvl) as of { (stats, floats, as') ->
205 (stats, floats, Con con as') }
207 floatExpr env lvl (App e a)
208 = case (floatExpr env lvl e) of { (fse, floats_e, e') ->
209 case (floatRhs env lvl a) of { (fsa, floats_a, a') ->
210 (fse `add_stats` fsa, floats_e ++ floats_a, App e' a') }}
212 floatExpr env lvl (Lam (tv,incd_lvl) e)
214 = case (floatExpr env incd_lvl e) of { (fs, floats, e') ->
216 -- Dump any bindings which absolutely cannot go any further
217 case (partitionByLevel incd_lvl floats) of { (floats', heres) ->
219 (fs, floats', Lam tv (install heres e'))
222 floatExpr env lvl (Lam (arg,incd_lvl) rhs)
225 new_env = extendVarEnv env arg incd_lvl
227 case (floatExpr new_env incd_lvl rhs) of { (fs, floats, rhs') ->
229 -- Dump any bindings which absolutely cannot go any further
230 case (partitionByLevel incd_lvl floats) of { (floats', heres) ->
232 (add_to_stats fs floats',
234 Lam arg (install heres rhs'))
237 floatExpr env lvl (Note note@(SCC cc) expr)
238 = case (floatExpr env lvl expr) of { (fs, floating_defns, expr') ->
240 -- Annotate bindings floated outwards past an scc expression
241 -- with the cc. We mark that cc as "duplicated", though.
243 annotated_defns = annotate (dupifyCC cc) floating_defns
245 (fs, annotated_defns, Note note expr') }
247 annotate :: CostCentre -> FloatBinds -> FloatBinds
249 annotate dupd_cc defn_groups
250 = [ (level, ann_bind floater) | (level, floater) <- defn_groups ]
252 ann_bind (NonRec binder rhs)
253 = NonRec binder (ann_rhs rhs)
256 = Rec [(binder, ann_rhs rhs) | (binder, rhs) <- pairs]
258 ann_rhs (Lam arg e) = Lam arg (ann_rhs e)
259 ann_rhs rhs@(Con con _) | isWHNFCon con = rhs -- no point in scc'ing WHNF data
260 ann_rhs rhs = Note (SCC dupd_cc) rhs
262 -- Note: Nested SCC's are preserved for the benefit of
263 -- cost centre stack profiling (Durham)
265 -- At one time I tried the effect of not float anything out of an InlineMe,
266 -- but it sometimes works badly. For example, consider PrelArr.done. It
267 -- has the form __inline (\d. e)
268 -- where e doesn't mention d. If we float this to
269 -- __inline (let x = e in \d. x)
270 -- things are bad. The inliner doesn't even inline it because it doesn't look
271 -- like a head-normal form. So it seems a lesser evil to let things float.
272 -- In SetLevels we do set the context to (Level 0 0) when we get to an InlineMe
273 -- which discourages floating out.
275 floatExpr env lvl (Note note expr) -- Other than SCCs
276 = case (floatExpr env lvl expr) of { (fs, floating_defns, expr') ->
277 (fs, floating_defns, Note note expr') }
279 floatExpr env lvl (Let bind body)
280 = case (floatBind env lvl bind) of { (fsb, rhs_floats, bind', new_env) ->
281 case (floatExpr new_env lvl body) of { (fse, body_floats, body') ->
283 rhs_floats ++ [(bind_lvl, bind')] ++ body_floats,
287 bind_lvl = getBindLevel bind
289 floatExpr env lvl (Case scrut (case_bndr, case_lvl) alts)
290 = case floatExpr env lvl scrut of { (fse, fde, scrut') ->
291 case floatList float_alt alts of { (fsa, fda, alts') ->
292 (add_stats fse fsa, fda ++ fde, Case scrut' case_bndr alts')
295 alts_env = extendVarEnv env case_bndr case_lvl
297 partition_fn = partitionByMajorLevel
299 float_alt (con, bs, rhs)
302 new_env = extendVarEnvList alts_env bs
304 case (floatExpr new_env case_lvl rhs) of { (fs, rhs_floats, rhs') ->
305 case (partition_fn case_lvl rhs_floats) of { (rhs_floats', heres) ->
306 (fs, rhs_floats', (con, bs', install heres rhs')) }}
309 floatList :: (a -> (FloatStats, FloatBinds, b)) -> [a] -> (FloatStats, FloatBinds, [b])
310 floatList f [] = (zeroStats, [], [])
311 floatList f (a:as) = case f a of { (fs_a, binds_a, b) ->
312 case floatList f as of { (fs_as, binds_as, bs) ->
313 (fs_a `add_stats` fs_as, binds_a ++ binds_as, b:bs) }}
316 %************************************************************************
318 \subsection{Utility bits for floating stats}
320 %************************************************************************
322 I didn't implement this with unboxed numbers. I don't want to be too
323 strict in this stuff, as it is rarely turned on. (WDP 95/09)
327 = FlS Int -- Number of top-floats * lambda groups they've been past
328 Int -- Number of non-top-floats * lambda groups they've been past
329 Int -- Number of lambda (groups) seen
331 get_stats (FlS a b c) = (a, b, c)
333 zeroStats = FlS 0 0 0
335 sum_stats xs = foldr add_stats zeroStats xs
337 add_stats (FlS a1 b1 c1) (FlS a2 b2 c2)
338 = FlS (a1 + a2) (b1 + b2) (c1 + c2)
340 add_to_stats (FlS a b c) floats
341 = FlS (a + length top_floats) (b + length other_floats) (c + 1)
343 (top_floats, other_floats) = partition to_very_top floats
345 to_very_top (my_lvl, _) = isTopLvl my_lvl
349 %************************************************************************
351 \subsection{Utility bits for floating}
353 %************************************************************************
356 getBindLevel (NonRec (_, lvl) _) = lvl
357 getBindLevel (Rec (((_,lvl), _) : _)) = lvl
361 partitionByMajorLevel, partitionByLevel
362 :: Level -- Partitioning level
364 -> FloatBinds -- Defns to be divided into 2 piles...
366 -> (FloatBinds, -- Defns with level strictly < partition level,
367 FloatBinds) -- The rest
370 partitionByMajorLevel ctxt_lvl defns
371 = partition float_further defns
373 -- Float it if we escape a value lambda,
374 -- or if we get to the top level
375 float_further (my_lvl, bind) = my_lvl `ltMajLvl` ctxt_lvl || isTopLvl my_lvl
376 -- The isTopLvl part says that if we can get to the top level, say "yes" anyway
382 -- which is as it should be
384 partitionByLevel ctxt_lvl defns
385 = partition float_further defns
387 float_further (my_lvl, _) = my_lvl `ltLvl` ctxt_lvl
391 floatsToBinds :: FloatBinds -> [CoreBind]
392 floatsToBinds floats = map snd floats
394 floatsToBindPairs :: FloatBinds -> [(Id,CoreExpr)]
396 floatsToBindPairs floats = concat (map mk_pairs floats)
398 mk_pairs (_, Rec pairs) = pairs
399 mk_pairs (_, NonRec binder rhs) = [(binder,rhs)]
401 install :: FloatBinds -> CoreExpr -> CoreExpr
403 install defn_groups expr
404 = foldr install_group expr defn_groups
406 install_group (_, defns) body = Let defns body