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 (floatExpr env level rhs) of { (fs, rhs_floats, rhs') ->
133 -- A good dumping point
134 case (partitionByMajorLevel level rhs_floats) of { (rhs_floats', heres) ->
137 NonRec name (install heres rhs'),
138 extendVarEnv env name level)
141 floatBind env lvl bind@(Rec pairs)
142 = case (unzip3 (map do_pair pairs)) of { (fss, rhss_floats, new_pairs) ->
144 if not (isTopLvl bind_level) then
146 (sum_stats fss, concat rhss_floats, Rec new_pairs, new_env)
148 {- In a recursive binding, destined for the top level (only),
149 the rhs floats may contain
150 references to the bound things. For example
152 f = ...(let v = ...f... in b) ...
159 and hence we must (pessimistically) make all the floats recursive
160 with the top binding. Later dependency analysis will unravel it.
165 Rec (new_pairs ++ floatsToBindPairs (concat rhss_floats)),
170 new_env = extendVarEnvList env (map fst pairs)
172 bind_level = getBindLevel bind
174 do_pair ((name, level), rhs)
175 = case (floatExpr new_env level rhs) of { (fs, rhs_floats, rhs') ->
177 -- A good dumping point
178 case (partitionByMajorLevel level rhs_floats) of { (rhs_floats', heres) ->
180 (fs, rhs_floats', (name, install heres rhs'))
184 %************************************************************************
186 \subsection[FloatOut-Expr]{Floating in expressions}
188 %************************************************************************
191 floatExpr :: IdEnv Level
194 -> (FloatStats, FloatBinds, CoreExpr)
196 floatExpr env _ (Var v) = (zeroStats, [], Var v)
197 floatExpr env _ (Type ty) = (zeroStats, [], Type ty)
198 floatExpr env lvl (Con con as)
199 = case floatList (floatExpr env lvl) as of { (stats, floats, as') ->
200 (stats, floats, Con con as') }
202 floatExpr env lvl (App e a)
203 = case (floatExpr env lvl e) of { (fse, floats_e, e') ->
204 case (floatExpr env lvl a) of { (fsa, floats_a, a') ->
205 (fse `add_stats` fsa, floats_e ++ floats_a, App e' a') }}
207 floatExpr env lvl (Lam (tv,incd_lvl) e)
209 = case (floatExpr env incd_lvl e) of { (fs, floats, e') ->
211 -- Dump any bindings which absolutely cannot go any further
212 case (partitionByLevel incd_lvl floats) of { (floats', heres) ->
214 (fs, floats', Lam tv (install heres e'))
217 floatExpr env lvl (Lam (arg,incd_lvl) rhs)
220 new_env = extendVarEnv env arg incd_lvl
222 case (floatExpr new_env incd_lvl rhs) of { (fs, floats, rhs') ->
224 -- Dump any bindings which absolutely cannot go any further
225 case (partitionByLevel incd_lvl floats) of { (floats', heres) ->
227 (add_to_stats fs floats',
229 Lam arg (install heres rhs'))
232 floatExpr env lvl (Note note@(SCC cc) expr)
233 = case (floatExpr env lvl expr) of { (fs, floating_defns, expr') ->
235 -- Annotate bindings floated outwards past an scc expression
236 -- with the cc. We mark that cc as "duplicated", though.
238 annotated_defns = annotate (dupifyCC cc) floating_defns
240 (fs, annotated_defns, Note note expr') }
242 annotate :: CostCentre -> FloatBinds -> FloatBinds
244 annotate dupd_cc defn_groups
245 = [ (level, ann_bind floater) | (level, floater) <- defn_groups ]
247 ann_bind (NonRec binder rhs)
248 = NonRec binder (ann_rhs rhs)
251 = Rec [(binder, ann_rhs rhs) | (binder, rhs) <- pairs]
253 ann_rhs (Lam arg e) = Lam arg (ann_rhs e)
254 ann_rhs rhs@(Con con _) | isWHNFCon con = rhs -- no point in scc'ing WHNF data
255 ann_rhs rhs = Note (SCC dupd_cc) rhs
257 -- Note: Nested SCC's are preserved for the benefit of
258 -- cost centre stack profiling (Durham)
260 floatExpr env lvl (Note note expr) -- Other than SCCs
261 = case (floatExpr env lvl expr) of { (fs, floating_defns, expr') ->
262 (fs, floating_defns, Note note expr') }
264 floatExpr env lvl (Let bind body)
265 = case (floatBind env lvl bind) of { (fsb, rhs_floats, bind', new_env) ->
266 case (floatExpr new_env lvl body) of { (fse, body_floats, body') ->
268 rhs_floats ++ [(bind_lvl, bind')] ++ body_floats,
272 bind_lvl = getBindLevel bind
274 floatExpr env lvl (Case scrut (case_bndr, case_lvl) alts)
275 = case floatExpr env lvl scrut of { (fse, fde, scrut') ->
276 case floatList float_alt alts of { (fsa, fda, alts') ->
277 (add_stats fse fsa, fda ++ fde, Case scrut' case_bndr alts')
280 alts_env = extendVarEnv env case_bndr case_lvl
282 partition_fn = partitionByMajorLevel
284 float_alt (con, bs, rhs)
287 new_env = extendVarEnvList alts_env bs
289 case (floatExpr new_env case_lvl rhs) of { (fs, rhs_floats, rhs') ->
290 case (partition_fn case_lvl rhs_floats) of { (rhs_floats', heres) ->
291 (fs, rhs_floats', (con, bs', install heres rhs')) }}
294 floatList :: (a -> (FloatStats, FloatBinds, b)) -> [a] -> (FloatStats, FloatBinds, [b])
295 floatList f [] = (zeroStats, [], [])
296 floatList f (a:as) = case f a of { (fs_a, binds_a, b) ->
297 case floatList f as of { (fs_as, binds_as, bs) ->
298 (fs_a `add_stats` fs_as, binds_a ++ binds_as, b:bs) }}
301 %************************************************************************
303 \subsection{Utility bits for floating stats}
305 %************************************************************************
307 I didn't implement this with unboxed numbers. I don't want to be too
308 strict in this stuff, as it is rarely turned on. (WDP 95/09)
312 = FlS Int -- Number of top-floats * lambda groups they've been past
313 Int -- Number of non-top-floats * lambda groups they've been past
314 Int -- Number of lambda (groups) seen
316 get_stats (FlS a b c) = (a, b, c)
318 zeroStats = FlS 0 0 0
320 sum_stats xs = foldr add_stats zeroStats xs
322 add_stats (FlS a1 b1 c1) (FlS a2 b2 c2)
323 = FlS (a1 + a2) (b1 + b2) (c1 + c2)
325 add_to_stats (FlS a b c) floats
326 = FlS (a + length top_floats) (b + length other_floats) (c + 1)
328 (top_floats, other_floats) = partition to_very_top floats
330 to_very_top (my_lvl, _) = isTopLvl my_lvl
334 %************************************************************************
336 \subsection{Utility bits for floating}
338 %************************************************************************
341 getBindLevel (NonRec (_, lvl) _) = lvl
342 getBindLevel (Rec (((_,lvl), _) : _)) = lvl
346 partitionByMajorLevel, partitionByLevel
347 :: Level -- Partitioning level
349 -> FloatBinds -- Defns to be divided into 2 piles...
351 -> (FloatBinds, -- Defns with level strictly < partition level,
352 FloatBinds) -- The rest
355 partitionByMajorLevel ctxt_lvl defns
356 = partition float_further defns
358 float_further (my_lvl, _) = my_lvl `ltMajLvl` ctxt_lvl ||
361 partitionByLevel ctxt_lvl defns
362 = partition float_further defns
364 float_further (my_lvl, _) = my_lvl `ltLvl` ctxt_lvl
368 floatsToBinds :: FloatBinds -> [CoreBind]
369 floatsToBinds floats = map snd floats
371 floatsToBindPairs :: FloatBinds -> [(Id,CoreExpr)]
373 floatsToBindPairs floats = concat (map mk_pairs floats)
375 mk_pairs (_, Rec pairs) = pairs
376 mk_pairs (_, NonRec binder rhs) = [(binder,rhs)]
378 install :: FloatBinds -> CoreExpr -> CoreExpr
380 install defn_groups expr
381 = foldr install_group expr defn_groups
383 install_group (_, defns) body = Let defns body