2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[SCCfinal]{Modify and collect code generation for final STG program}
6 This is now a sort-of-normal STG-to-STG pass (WDP 94/06), run by stg2stg.
8 * Traverses the STG program collecting the cost centres. These are
9 required to declare the cost centres at the start of code
12 Note: because of cross-module unfolding, some of these cost centres
13 may be from other modules. But will still have to give them
14 "extern" declarations.
16 * Puts on CAF cost-centres if the user has asked for individual CAF
19 * Ditto for individual DICT cost-centres.
21 * Boxes top-level inherited functions passed as arguments.
23 * "Distributes" given cost-centres to all as-yet-unmarked RHSs.
26 module SCCfinal ( stgMassageForProfiling ) where
28 #include "HsVersions.h"
32 import CmdLineOpts ( opt_AutoSccsOnIndividualCafs )
33 import CostCentre -- lots of things
34 import Const ( Con(..) )
35 import Id ( Id, mkSysLocal, idType, idName )
36 import Module ( Module )
37 import UniqSupply ( uniqFromSupply, splitUniqSupply, UniqSupply )
38 import Unique ( Unique )
39 import Type ( splitForAllTys, splitTyConApp_maybe )
40 import TyCon ( isFunTyCon )
43 import Name ( isLocallyDefinedName )
44 import Util ( removeDups )
47 infixr 9 `thenMM`, `thenMM_`
51 type CollectedCCs = ([CostCentre], -- locally defined ones
52 [CostCentre], -- ones needing "extern" decls
53 [CostCentreStack]) -- singleton stacks (for CAFs)
55 stgMassageForProfiling
56 :: Module -> FAST_STRING -- module name, group name
57 -> UniqSupply -- unique supply
58 -> [StgBinding] -- input
59 -> (CollectedCCs, [StgBinding])
61 stgMassageForProfiling mod_name grp_name us stg_binds
63 ((local_ccs, extern_ccs, cc_stacks),
65 = initMM mod_name us (do_top_bindings stg_binds)
67 (fixed_ccs, fixed_cc_stacks)
68 = if opt_AutoSccsOnIndividualCafs
69 then ([],[]) -- don't need "all CAFs" CC
70 -- (for Prelude, we use PreludeCC)
71 else ([all_cafs_cc], [all_cafs_ccs])
73 local_ccs_no_dups = fst (removeDups cmpCostCentre local_ccs)
74 extern_ccs_no_dups = fst (removeDups cmpCostCentre extern_ccs)
76 ((fixed_ccs ++ local_ccs_no_dups,
78 fixed_cc_stacks ++ cc_stacks), stg_binds2)
81 all_cafs_cc = mkAllCafsCC mod_name grp_name
82 all_cafs_ccs = mkSingletonCCS all_cafs_cc
85 do_top_bindings :: [StgBinding] -> MassageM [StgBinding]
87 do_top_bindings [] = returnMM []
89 do_top_bindings (StgNonRec b rhs : bs)
90 = do_top_rhs b rhs `thenMM` \ rhs' ->
92 do_top_bindings bs `thenMM` \bs' ->
93 returnMM (StgNonRec b rhs' : bs')
96 do_top_bindings (StgRec pairs : bs)
97 = addTopLevelIshIds binders (
98 mapMM do_pair pairs `thenMM` \ pairs2 ->
99 do_top_bindings bs `thenMM` \ bs' ->
100 returnMM (StgRec pairs2 : bs')
103 binders = map fst pairs
105 = do_top_rhs b rhs `thenMM` \ rhs2 ->
109 do_top_rhs :: Id -> StgRhs -> MassageM StgRhs
111 do_top_rhs binder (StgRhsClosure _ bi srt fv u [] (StgSCC cc (StgCon (DataCon con) args _)))
112 | not (isSccCountCostCentre cc)
113 -- Trivial _scc_ around nothing but static data
114 -- Eliminate _scc_ ... and turn into StgRhsCon
115 = returnMM (StgRhsCon dontCareCCS con args)
117 {- Can't do this one with cost-centre stacks: --SDM
118 do_top_rhs binder (StgRhsClosure no_cc bi srt fv u [] (StgSCC ty cc expr))
119 | (noCCSAttached no_cc || currentOrSubsumedCCS no_cc)
120 && not (isSccCountCostCentre cc)
121 -- Top level CAF without a cost centre attached
122 -- Attach and collect cc of trivial _scc_ in body
123 = collectCC cc `thenMM_`
124 set_prevailing_cc cc (do_expr expr) `thenMM` \ expr' ->
125 returnMM (StgRhsClosure cc bi srt fv u [] expr')
128 do_top_rhs binder (StgRhsClosure no_cc bi srt fv u [] body)
129 | noCCSAttached no_cc || currentOrSubsumedCCS no_cc
130 -- Top level CAF without a cost centre attached
131 -- Attach CAF cc (collect if individual CAF ccs)
132 = (if opt_AutoSccsOnIndividualCafs
133 then let cc = mkAutoCC binder mod_name grp_name CafCC
134 ccs = mkSingletonCCS cc
136 collectCC cc `thenMM_`
137 collectCCS ccs `thenMM_`
140 returnMM all_cafs_ccs) `thenMM` \ caf_ccs ->
141 set_prevailing_cc caf_ccs (do_expr body) `thenMM` \ body' ->
142 returnMM (StgRhsClosure caf_ccs bi srt fv u [] body')
144 do_top_rhs binder (StgRhsClosure cc bi srt fv u [] body)
145 -- Top level CAF with cost centre attached
146 -- Should this be a CAF cc ??? Does this ever occur ???
147 = pprPanic "SCCfinal: CAF with cc:" (ppr cc)
149 {- can't do this with cost-centre stacks: --SDM
150 do_top_rhs binder (StgRhsClosure _ bi srt fv u args (StgSCC cc expr))
151 | not (isSccCountCostCentre cc)
152 -- Top level function with trivial _scc_ in body
153 -- Attach and collect cc of trivial _scc_
154 = collectCC cc `thenMM_`
155 set_prevailing_cc cc (do_expr expr) `thenMM` \ expr' ->
156 returnMM (StgRhsClosure cc bi srt fv u args expr')
159 do_top_rhs binder (StgRhsClosure no_ccs bi srt fv u args body)
160 -- Top level function, probably subsumed
161 | noCCSAttached no_ccs
162 = set_lambda_cc (do_expr body) `thenMM` \ body' ->
163 returnMM (StgRhsClosure subsumedCCS bi srt fv u args body')
166 = pprPanic "SCCfinal: CAF with cc:" (ppr no_ccs)
168 do_top_rhs binder (StgRhsCon ccs con args)
169 -- Top-level (static) data is not counted in heap
170 -- profiles; nor do we set CCCS from it; so we
171 -- just slam in dontCareCostCentre
172 = returnMM (StgRhsCon dontCareCCS con args)
175 do_expr :: StgExpr -> MassageM StgExpr
177 do_expr (StgApp fn args)
178 = boxHigherOrderArgs (StgApp fn) args
180 do_expr (StgCon con args res_ty)
181 = boxHigherOrderArgs (\args -> StgCon con args res_ty) args
183 do_expr (StgSCC cc expr) -- Ha, we found a cost centre!
184 = collectCC cc `thenMM_`
185 do_expr expr `thenMM` \ expr' ->
186 returnMM (StgSCC cc expr')
188 do_expr (StgCase expr fv1 fv2 bndr srt alts)
189 = do_expr expr `thenMM` \ expr' ->
190 do_alts alts `thenMM` \ alts' ->
191 returnMM (StgCase expr' fv1 fv2 bndr srt alts')
193 do_alts (StgAlgAlts ty alts def)
194 = mapMM do_alt alts `thenMM` \ alts' ->
195 do_deflt def `thenMM` \ def' ->
196 returnMM (StgAlgAlts ty alts' def')
198 do_alt (id, bs, use_mask, e)
199 = do_expr e `thenMM` \ e' ->
200 returnMM (id, bs, use_mask, e')
202 do_alts (StgPrimAlts ty alts def)
203 = mapMM do_alt alts `thenMM` \ alts' ->
204 do_deflt def `thenMM` \ def' ->
205 returnMM (StgPrimAlts ty alts' def')
208 = do_expr e `thenMM` \ e' ->
211 do_deflt StgNoDefault = returnMM StgNoDefault
212 do_deflt (StgBindDefault e)
213 = do_expr e `thenMM` \ e' ->
214 returnMM (StgBindDefault e')
217 = do_let b e `thenMM` \ (b,e) ->
218 returnMM (StgLet b e)
220 do_expr (StgLetNoEscape lvs1 lvs2 b e)
221 = do_let b e `thenMM` \ (b,e) ->
222 returnMM (StgLetNoEscape lvs1 lvs2 b e)
224 ----------------------------------
226 do_let (StgNonRec b rhs) e
227 = do_rhs rhs `thenMM` \ rhs' ->
229 do_expr e `thenMM` \ e' ->
230 returnMM (StgNonRec b rhs',e')
233 do_let (StgRec pairs) e
234 = addTopLevelIshIds binders (
235 mapMM do_pair pairs `thenMM` \ pairs' ->
236 do_expr e `thenMM` \ e' ->
237 returnMM (StgRec pairs', e')
240 binders = map fst pairs
242 = do_rhs rhs `thenMM` \ rhs2 ->
245 ----------------------------------
246 do_rhs :: StgRhs -> MassageM StgRhs
247 -- We play much the same game as we did in do_top_rhs above;
248 -- but we don't have to worry about cafs etc.
251 do_rhs (StgRhsClosure closure_cc bi srt fv u [] (StgSCC ty cc (StgCon (DataCon con) args _)))
252 | not (isSccCountCostCentre cc)
253 = collectCC cc `thenMM_`
254 returnMM (StgRhsCon cc con args)
258 do_rhs (StgRhsClosure _ bi srt fv u args (StgSCC ty cc expr))
259 | not (isSccCountCostCentre cc)
260 = collectCC cc `thenMM_`
261 set_prevailing_cc cc (do_expr expr) `thenMM` \ expr' ->
262 returnMM (StgRhsClosure cc bi srt fv u args expr')
265 do_rhs (StgRhsClosure cc bi srt fv u [] body)
266 = do_expr body `thenMM` \ body' ->
267 returnMM (StgRhsClosure currentCCS bi srt fv u [] body')
269 do_rhs (StgRhsClosure cc bi srt fv u args body)
270 = set_lambda_cc (do_expr body) `thenMM` \ body' ->
271 get_prevailing_cc `thenMM` \ prev_ccs ->
272 returnMM (StgRhsClosure currentCCS bi srt fv u args body')
274 do_rhs (StgRhsCon cc con args)
275 = returnMM (StgRhsCon currentCCS con args)
278 %************************************************************************
280 \subsection{Boxing higher-order args}
282 %************************************************************************
286 :: ([StgArg] -> StgExpr)
287 -- An application lacking its arguments
288 -> [StgArg] -- arguments which we might box
291 boxHigherOrderArgs almost_expr args
292 = getTopLevelIshIds `thenMM` \ ids ->
293 mapAccumMM (do_arg ids) [] args `thenMM` \ (let_bindings, new_args) ->
294 returnMM (foldr (mk_stg_let currentCCS) (almost_expr new_args) let_bindings)
297 do_arg ids bindings atom@(StgConArg _) = returnMM (bindings, atom)
299 do_arg ids bindings atom@(StgVarArg old_var)
301 var_type = idType old_var
303 if ( not (isLocallyDefinedName (idName old_var)) ||
304 elemVarSet old_var ids ) && isFunType var_type
306 -- make a trivial let-binding for the top-level function
307 getUniqueMM `thenMM` \ uniq ->
309 new_var = mkSysLocal SLIT("sf") uniq var_type
311 returnMM ( (new_var, old_var) : bindings, StgVarArg new_var )
313 returnMM (bindings, atom)
316 mk_stg_let :: CostCentreStack -> (Id, Id) -> StgExpr -> StgExpr
318 mk_stg_let cc (new_var, old_var) body
320 rhs_body = StgApp old_var [{-args-}]
321 rhs_closure = StgRhsClosure cc stgArgOcc NoSRT [{-fvs-}] ReEntrant [{-args-}] rhs_body
323 StgLet (StgNonRec new_var rhs_closure) body
325 bOGUS_LVs = emptyUniqSet -- easier to print than: panic "mk_stg_let: LVs"
328 = case splitForAllTys var_type of
329 (_, ty) -> case splitTyConApp_maybe ty of
330 Just (tycon,_) | isFunTyCon tycon -> True
335 %************************************************************************
337 \subsection{Boring monad stuff for this}
339 %************************************************************************
343 = Module -- module name
344 -> CostCentreStack -- prevailing CostCentre
345 -- if none, subsumedCosts at top-level
346 -- useCurrentCostCentre at nested levels
348 -> VarSet -- toplevel-ish Ids for boxing
350 -> (CollectedCCs, result)
352 -- the initMM function also returns the final CollectedCCs
354 initMM :: Module -- module name, which we may consult
359 initMM mod_name init_us m = m mod_name noCCS init_us emptyVarSet ([],[],[])
361 thenMM :: MassageM a -> (a -> MassageM b) -> MassageM b
362 thenMM_ :: MassageM a -> (MassageM b) -> MassageM b
364 thenMM expr cont mod scope_cc us ids ccs
365 = case splitUniqSupply us of { (s1, s2) ->
366 case (expr mod scope_cc s1 ids ccs) of { (ccs2, result) ->
367 cont result mod scope_cc s2 ids ccs2 }}
369 thenMM_ expr cont mod scope_cc us ids ccs
370 = case splitUniqSupply us of { (s1, s2) ->
371 case (expr mod scope_cc s1 ids ccs) of { (ccs2, _) ->
372 cont mod scope_cc s2 ids ccs2 }}
374 returnMM :: a -> MassageM a
375 returnMM result mod scope_cc us ids ccs = (ccs, result)
378 nopMM mod scope_cc us ids ccs = (ccs, ())
380 mapMM :: (a -> MassageM b) -> [a] -> MassageM [b]
381 mapMM f [] = returnMM []
383 = f m `thenMM` \ r ->
384 mapMM f ms `thenMM` \ rs ->
387 mapAccumMM :: (acc -> x -> MassageM (acc, y)) -> acc -> [x] -> MassageM (acc, [y])
388 mapAccumMM f b [] = returnMM (b, [])
389 mapAccumMM f b (m:ms)
390 = f b m `thenMM` \ (b2, r) ->
391 mapAccumMM f b2 ms `thenMM` \ (b3, rs) ->
394 getUniqueMM :: MassageM Unique
395 getUniqueMM mod scope_cc us ids ccs = (ccs, uniqFromSupply us)
397 addTopLevelIshId :: Id -> MassageM a -> MassageM a
398 addTopLevelIshId id scope mod scope_cc us ids ccs
399 | isCurrentCCS scope_cc = scope mod scope_cc us ids ccs
400 | otherwise = scope mod scope_cc us (extendVarSet ids id) ccs
402 addTopLevelIshIds :: [Id] -> MassageM a -> MassageM a
403 addTopLevelIshIds [] cont = cont
404 addTopLevelIshIds (id:ids) cont
405 = addTopLevelIshId id (addTopLevelIshIds ids cont)
407 getTopLevelIshIds :: MassageM VarSet
408 getTopLevelIshIds mod scope_cc us ids ccs = (ccs, ids)
411 The prevailing CCS is used to tell whether we're in a top-levelish
412 position, where top-levelish is defined as "not inside a lambda".
413 Prevailing CCs used to be used for something much more complicated,
417 set_lambda_cc :: MassageM a -> MassageM a
418 set_lambda_cc action mod scope_cc us ids ccs
419 = action mod currentCCS us ids ccs
421 set_prevailing_cc :: CostCentreStack -> MassageM a -> MassageM a
422 set_prevailing_cc cc_to_set_to action mod scope_cc us ids ccs
423 = action mod cc_to_set_to us ids ccs
425 get_prevailing_cc :: MassageM CostCentreStack
426 get_prevailing_cc mod scope_cc us ids ccs = (ccs, scope_cc)
430 collectCC :: CostCentre -> MassageM ()
432 collectCC cc mod_name scope_cc us ids (local_ccs, extern_ccs, ccss)
433 = ASSERT(not (noCCAttached cc))
434 if (cc `ccFromThisModule` mod_name) then
435 ((cc : local_ccs, extern_ccs, ccss), ())
436 else -- must declare it "extern"
437 ((local_ccs, cc : extern_ccs, ccss), ())
439 collectCCS :: CostCentreStack -> MassageM ()
441 collectCCS ccs mod_name scope_cc us ids (local_ccs, extern_ccs, ccss)
442 = ASSERT(not (noCCSAttached ccs))
443 ((local_ccs, extern_ccs, ccs : ccss), ())