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 )
19 import Const ( isWHNFCon )
21 import CoreLint ( beginPass, endPass )
23 import SetLevels ( setLevels,
24 Level(..), tOP_LEVEL, ltMajLvl, ltLvl, isTopLvl
26 import BasicTypes ( Unused )
28 import UniqSupply ( UniqSupply )
29 import List ( partition )
36 At the moment we never float a binding out to between two adjacent
40 \x y -> let t = x+x in ...
42 \x -> let t = x+x in \y -> ...
44 Reason: this is less efficient in the case where the original lambda
45 is never partially applied.
47 But there's a case I've seen where this might not be true. Consider:
53 elem' x (y:ys) = x==y || elem' x ys
55 It turns out that this generates a subexpression of the form
57 \deq x ys -> let eq = eqFromEqDict deq in ...
59 vwhich might usefully be separated to
61 \deq -> let eq = eqFromEqDict deq in \xy -> ...
63 Well, maybe. We don't do this at the moment.
66 type LevelledExpr = TaggedExpr Level
67 type LevelledBind = TaggedBind Level
68 type FloatBind = (Level, CoreBind)
69 type FloatBinds = [FloatBind]
72 %************************************************************************
74 \subsection[floatOutwards]{@floatOutwards@: let-floating interface function}
76 %************************************************************************
79 floatOutwards :: UniqSupply -> [CoreBind] -> IO [CoreBind]
83 beginPass "Float out";
85 let { annotated_w_levels = setLevels pgm us ;
86 (fss, binds_s') = unzip (map floatTopBind annotated_w_levels)
89 dumpIfSet 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 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")]);
100 opt_D_verbose_core2core {- no specific flag for dumping float-out -}
104 floatTopBind bind@(NonRec _ _)
105 = case (floatBind emptyVarEnv tOP_LEVEL bind) of { (fs, floats, bind', _) ->
106 (fs, floatsToBinds floats ++ [bind'])
109 floatTopBind bind@(Rec _)
110 = case (floatBind emptyVarEnv tOP_LEVEL bind) of { (fs, floats, Rec pairs', _) ->
111 -- Actually floats will be empty
112 --false:ASSERT(null floats)
113 (fs, [Rec (floatsToBindPairs floats ++ pairs')])
117 %************************************************************************
119 \subsection[FloatOut-Bind]{Floating in a binding (the business end)}
121 %************************************************************************
125 floatBind :: IdEnv Level
128 -> (FloatStats, FloatBinds, CoreBind, IdEnv Level)
130 floatBind env lvl (NonRec (name,level) rhs)
131 = case (floatRhs env level rhs) of { (fs, rhs_floats, rhs') ->
134 extendVarEnv env name level)
137 floatBind env lvl bind@(Rec pairs)
138 = case (unzip3 (map do_pair pairs)) of { (fss, rhss_floats, new_pairs) ->
140 if not (isTopLvl bind_level) then
142 (sum_stats fss, concat rhss_floats, Rec new_pairs, new_env)
144 {- In a recursive binding, destined for the top level (only),
145 the rhs floats may contain
146 references to the bound things. For example
148 f = ...(let v = ...f... in b) ...
155 and hence we must (pessimistically) make all the floats recursive
156 with the top binding. Later dependency analysis will unravel it.
161 Rec (new_pairs ++ floatsToBindPairs (concat rhss_floats)),
166 new_env = extendVarEnvList env (map fst pairs)
168 bind_level = getBindLevel bind
170 do_pair ((name, level), rhs)
171 = case (floatRhs new_env level rhs) of { (fs, rhs_floats, rhs') ->
172 (fs, rhs_floats, (name, rhs'))
176 %************************************************************************
178 \subsection[FloatOut-Expr]{Floating in expressions}
180 %************************************************************************
187 -> (FloatStats, FloatBinds, CoreExpr)
190 = case (floatExpr env lvl arg) of { (fsa, floats, arg') ->
191 case (partitionByMajorLevel lvl floats) of { (floats', heres) ->
192 -- Dump bindings that aren't going to escape from a lambda
193 -- This is to avoid floating the x binding out of
194 -- f (let x = e in b)
195 -- unnecessarily. It even causes a bug to do so if we have
196 -- y = writeArr# a n (let x = e in b)
197 -- because the y binding is an expr-ok-for-speculation one.
198 (fsa, floats', install heres arg') }}
200 floatExpr env _ (Var v) = (zeroStats, [], Var v)
201 floatExpr env _ (Type ty) = (zeroStats, [], Type ty)
202 floatExpr env lvl (Con con as)
203 = case floatList (floatRhs env lvl) as of { (stats, floats, as') ->
204 (stats, floats, Con con as') }
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 (ann_rhs rhs)
255 = Rec [(binder, ann_rhs rhs) | (binder, rhs) <- pairs]
257 ann_rhs (Lam arg e) = Lam arg (ann_rhs e)
258 ann_rhs rhs@(Con con _) | isWHNFCon con = rhs -- no point in scc'ing WHNF data
259 ann_rhs rhs = Note (SCC dupd_cc) rhs
261 -- Note: Nested SCC's are preserved for the benefit of
262 -- cost centre stack profiling (Durham)
264 floatExpr env lvl (Note note expr) -- Other than SCCs
265 = case (floatExpr env lvl expr) of { (fs, floating_defns, expr') ->
266 (fs, floating_defns, Note note expr') }
268 floatExpr env lvl (Let bind body)
269 = case (floatBind env lvl bind) of { (fsb, rhs_floats, bind', new_env) ->
270 case (floatExpr new_env lvl body) of { (fse, body_floats, body') ->
272 rhs_floats ++ [(bind_lvl, bind')] ++ body_floats,
276 bind_lvl = getBindLevel bind
278 floatExpr env lvl (Case scrut (case_bndr, case_lvl) alts)
279 = case floatExpr env lvl scrut of { (fse, fde, scrut') ->
280 case floatList float_alt alts of { (fsa, fda, alts') ->
281 (add_stats fse fsa, fda ++ fde, Case scrut' case_bndr alts')
284 alts_env = extendVarEnv env case_bndr case_lvl
286 partition_fn = partitionByMajorLevel
288 float_alt (con, bs, rhs)
291 new_env = extendVarEnvList alts_env bs
293 case (floatExpr new_env case_lvl rhs) of { (fs, rhs_floats, rhs') ->
294 case (partition_fn case_lvl rhs_floats) of { (rhs_floats', heres) ->
295 (fs, rhs_floats', (con, bs', install heres rhs')) }}
298 floatList :: (a -> (FloatStats, FloatBinds, b)) -> [a] -> (FloatStats, FloatBinds, [b])
299 floatList f [] = (zeroStats, [], [])
300 floatList f (a:as) = case f a of { (fs_a, binds_a, b) ->
301 case floatList f as of { (fs_as, binds_as, bs) ->
302 (fs_a `add_stats` fs_as, binds_a ++ binds_as, b:bs) }}
305 %************************************************************************
307 \subsection{Utility bits for floating stats}
309 %************************************************************************
311 I didn't implement this with unboxed numbers. I don't want to be too
312 strict in this stuff, as it is rarely turned on. (WDP 95/09)
316 = FlS Int -- Number of top-floats * lambda groups they've been past
317 Int -- Number of non-top-floats * lambda groups they've been past
318 Int -- Number of lambda (groups) seen
320 get_stats (FlS a b c) = (a, b, c)
322 zeroStats = FlS 0 0 0
324 sum_stats xs = foldr add_stats zeroStats xs
326 add_stats (FlS a1 b1 c1) (FlS a2 b2 c2)
327 = FlS (a1 + a2) (b1 + b2) (c1 + c2)
329 add_to_stats (FlS a b c) floats
330 = FlS (a + length top_floats) (b + length other_floats) (c + 1)
332 (top_floats, other_floats) = partition to_very_top floats
334 to_very_top (my_lvl, _) = isTopLvl my_lvl
338 %************************************************************************
340 \subsection{Utility bits for floating}
342 %************************************************************************
345 getBindLevel (NonRec (_, lvl) _) = lvl
346 getBindLevel (Rec (((_,lvl), _) : _)) = lvl
350 partitionByMajorLevel, partitionByLevel
351 :: Level -- Partitioning level
353 -> FloatBinds -- Defns to be divided into 2 piles...
355 -> (FloatBinds, -- Defns with level strictly < partition level,
356 FloatBinds) -- The rest
359 partitionByMajorLevel ctxt_lvl defns
360 = partition float_further defns
362 float_further (my_lvl, _) = my_lvl `lt_major` ctxt_lvl
364 my_lvl `lt_major` ctxt_lvl = my_lvl `ltMajLvl` ctxt_lvl ||
367 partitionByLevel ctxt_lvl defns
368 = partition float_further defns
370 float_further (my_lvl, _) = my_lvl `ltLvl` ctxt_lvl
374 floatsToBinds :: FloatBinds -> [CoreBind]
375 floatsToBinds floats = map snd floats
377 floatsToBindPairs :: FloatBinds -> [(Id,CoreExpr)]
379 floatsToBindPairs floats = concat (map mk_pairs floats)
381 mk_pairs (_, Rec pairs) = pairs
382 mk_pairs (_, NonRec binder rhs) = [(binder,rhs)]
384 install :: FloatBinds -> CoreExpr -> CoreExpr
386 install defn_groups expr
387 = foldr install_group expr defn_groups
389 install_group (_, defns) body = Let defns body