5af05432a83a52da108811f2bee89d00bb653d20
[ghc-hetmet.git] / ghc / compiler / profiling / SCCfinal.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3 %
4 \section[SCCfinal]{Modify and collect code generation for final STG program}
5
6 This is now a sort-of-normal STG-to-STG pass (WDP 94/06), run by stg2stg.
7
8 * Traverses the STG program collecting the cost centres. These are
9   required to declare the cost centres at the start of code
10   generation.
11
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.
15
16 * Puts on CAF cost-centres if the user has asked for individual CAF
17   cost-centres.
18
19 * Ditto for individual DICT cost-centres.
20
21 * Boxes top-level inherited functions passed as arguments.
22
23 * "Distributes" given cost-centres to all as-yet-unmarked RHSs.
24
25 \begin{code}
26 module SCCfinal ( stgMassageForProfiling ) where
27
28 #include "HsVersions.h"
29
30 import StgSyn
31
32 import CmdLineOpts      ( opt_AutoSccsOnIndividualCafs )
33 import CostCentre       -- lots of things
34 import Id               ( Id, mkSysLocal, idType, idName )
35 import Module           ( Module )
36 import UniqSupply       ( uniqFromSupply, splitUniqSupply, UniqSupply )
37 import Unique           ( Unique )
38 import Type             ( splitForAllTys, splitTyConApp_maybe )
39 import TyCon            ( isFunTyCon )
40 import VarSet
41 import UniqSet
42 import Name             ( isLocallyDefined )
43 import Util             ( removeDups )
44 import Outputable       
45
46 infixr 9 `thenMM`, `thenMM_`
47 \end{code}
48
49 \begin{code}
50 type CollectedCCs = ([CostCentre],      -- locally defined ones
51                      [CostCentre],      -- ones needing "extern" decls
52                      [CostCentreStack]) -- singleton stacks (for CAFs)
53
54 stgMassageForProfiling
55         :: Module                       -- module name
56         -> UniqSupply                   -- unique supply
57         -> [StgBinding]                 -- input
58         -> (CollectedCCs, [StgBinding])
59
60 stgMassageForProfiling mod_name us stg_binds
61   = let
62         ((local_ccs, extern_ccs, cc_stacks),
63          stg_binds2)
64           = initMM mod_name us (do_top_bindings stg_binds)
65
66         (fixed_ccs, fixed_cc_stacks)
67           = if opt_AutoSccsOnIndividualCafs
68             then ([],[])  -- don't need "all CAFs" CC 
69                           -- (for Prelude, we use PreludeCC)
70             else ([all_cafs_cc], [all_cafs_ccs])
71
72         local_ccs_no_dups  = fst (removeDups cmpCostCentre local_ccs)
73         extern_ccs_no_dups = fst (removeDups cmpCostCentre extern_ccs)
74     in
75     ((fixed_ccs ++ local_ccs_no_dups, 
76       extern_ccs_no_dups, 
77       fixed_cc_stacks ++ cc_stacks), stg_binds2)
78   where
79
80     all_cafs_cc  = mkAllCafsCC mod_name
81     all_cafs_ccs = mkSingletonCCS all_cafs_cc
82
83     ----------
84     do_top_bindings :: [StgBinding] -> MassageM [StgBinding]
85
86     do_top_bindings [] = returnMM []
87
88     do_top_bindings (StgNonRec b rhs : bs) 
89       = do_top_rhs b rhs                `thenMM` \ rhs' ->
90         addTopLevelIshId b (
91            do_top_bindings bs `thenMM` \bs' ->
92            returnMM (StgNonRec b rhs' : bs')
93         )
94
95     do_top_bindings (StgRec pairs : bs)
96       = addTopLevelIshIds binders (
97            mapMM do_pair pairs          `thenMM` \ pairs2 ->
98            do_top_bindings bs `thenMM` \ bs' ->
99            returnMM (StgRec pairs2 : bs')
100         )
101       where
102         binders = map fst pairs
103         do_pair (b, rhs) 
104            = do_top_rhs b rhs   `thenMM` \ rhs2 ->
105              returnMM (b, rhs2)
106
107     ----------
108     do_top_rhs :: Id -> StgRhs -> MassageM StgRhs
109
110     do_top_rhs binder (StgRhsClosure _ bi srt fv u [] (StgSCC cc (StgConApp con args)))
111       | not (isSccCountCostCentre cc)
112         -- Trivial _scc_ around nothing but static data
113         -- Eliminate _scc_ ... and turn into StgRhsCon
114       = returnMM (StgRhsCon dontCareCCS con args)
115
116 {- Can't do this one with cost-centre stacks:  --SDM
117     do_top_rhs binder (StgRhsClosure no_cc bi srt fv u [] (StgSCC ty cc expr))
118       | (noCCSAttached no_cc || currentOrSubsumedCCS no_cc)
119         && not (isSccCountCostCentre cc)
120         -- Top level CAF without a cost centre attached
121         -- Attach and collect cc of trivial _scc_ in body
122       = collectCC cc                                    `thenMM_`
123         set_prevailing_cc cc (do_expr expr)             `thenMM`  \ expr' ->
124         returnMM (StgRhsClosure cc bi srt fv u [] expr')
125 -}
126
127     do_top_rhs binder (StgRhsClosure no_cc bi srt fv u [] body)
128       | noCCSAttached no_cc || currentOrSubsumedCCS no_cc
129         -- Top level CAF without a cost centre attached
130         -- Attach CAF cc (collect if individual CAF ccs)
131       = (if opt_AutoSccsOnIndividualCafs 
132                 then let cc = mkAutoCC binder mod_name CafCC
133                          ccs = mkSingletonCCS cc
134                      in
135                      collectCC  cc  `thenMM_`
136                      collectCCS ccs `thenMM_`
137                      returnMM ccs
138                 else 
139                      returnMM all_cafs_ccs)             `thenMM`  \ caf_ccs ->
140            set_prevailing_cc caf_ccs (do_expr body)     `thenMM`  \ body' ->
141            returnMM (StgRhsClosure caf_ccs bi srt fv u [] body')
142
143     do_top_rhs binder (StgRhsClosure cc bi srt fv u [] body)
144         -- Top level CAF with cost centre attached
145         -- Should this be a CAF cc ??? Does this ever occur ???
146       = pprPanic "SCCfinal: CAF with cc:" (ppr cc)
147
148 {- can't do this with cost-centre stacks:  --SDM
149     do_top_rhs binder (StgRhsClosure _ bi srt fv u args (StgSCC cc expr))
150       | not (isSccCountCostCentre cc)
151         -- Top level function with trivial _scc_ in body
152         -- Attach and collect cc of trivial _scc_
153       = collectCC cc                                    `thenMM_`
154         set_prevailing_cc cc (do_expr expr)             `thenMM` \ expr' ->
155         returnMM (StgRhsClosure cc bi srt fv u args expr')
156 -}
157
158     do_top_rhs binder (StgRhsClosure no_ccs bi srt fv u args body)
159         -- Top level function, probably subsumed
160       | noCCSAttached no_ccs
161       = set_lambda_cc (do_expr body)    `thenMM` \ body' ->
162         returnMM (StgRhsClosure subsumedCCS bi srt fv u args body')
163
164       | otherwise
165       = pprPanic "SCCfinal: CAF with cc:" (ppr no_ccs)
166
167     do_top_rhs binder (StgRhsCon ccs con args)
168         -- Top-level (static) data is not counted in heap
169         -- profiles; nor do we set CCCS from it; so we
170         -- just slam in dontCareCostCentre
171       = returnMM (StgRhsCon dontCareCCS con args)
172
173     ------
174     do_expr :: StgExpr -> MassageM StgExpr
175
176     do_expr (StgLit l) = returnMM (StgLit l)
177
178     do_expr (StgApp fn args)
179       = boxHigherOrderArgs (StgApp fn) args
180
181     do_expr (StgConApp con args)
182       = boxHigherOrderArgs (\args -> StgConApp con args) args
183
184     do_expr (StgPrimApp con args res_ty)
185       = boxHigherOrderArgs (\args -> StgPrimApp con args res_ty) args
186
187     do_expr (StgSCC cc expr)    -- Ha, we found a cost centre!
188       = collectCC cc            `thenMM_`
189         do_expr expr            `thenMM` \ expr' ->
190         returnMM (StgSCC cc expr')
191
192     do_expr (StgCase expr fv1 fv2 bndr srt alts)
193       = do_expr expr            `thenMM` \ expr' ->
194         do_alts alts            `thenMM` \ alts' ->
195         returnMM (StgCase expr' fv1 fv2 bndr srt alts')
196       where
197         do_alts (StgAlgAlts ty alts def) 
198           = mapMM do_alt alts   `thenMM` \ alts' ->
199             do_deflt def        `thenMM` \ def' ->
200             returnMM (StgAlgAlts ty alts' def')
201           where
202             do_alt (id, bs, use_mask, e)
203               = do_expr e `thenMM` \ e' ->
204                 returnMM (id, bs, use_mask, e')
205
206         do_alts (StgPrimAlts ty alts def) 
207           = mapMM do_alt alts   `thenMM` \ alts' ->
208             do_deflt def        `thenMM` \ def' ->
209             returnMM (StgPrimAlts ty alts' def')
210           where
211             do_alt (l,e)
212               = do_expr e `thenMM` \ e' ->
213                 returnMM (l,e')
214
215         do_deflt StgNoDefault = returnMM StgNoDefault
216         do_deflt (StgBindDefault e) 
217           = do_expr e                   `thenMM` \ e' ->
218             returnMM (StgBindDefault e')
219
220     do_expr (StgLet b e)
221         = do_let b e `thenMM` \ (b,e) ->
222           returnMM (StgLet b e)
223
224     do_expr (StgLetNoEscape lvs1 lvs2 b e)
225         = do_let b e `thenMM` \ (b,e) ->
226           returnMM (StgLetNoEscape lvs1 lvs2 b e)
227
228     ----------------------------------
229
230     do_let (StgNonRec b rhs) e
231       = do_rhs rhs                      `thenMM` \ rhs' ->
232         addTopLevelIshId b (
233           do_expr e                     `thenMM` \ e' ->
234           returnMM (StgNonRec b rhs',e')
235         )
236
237     do_let (StgRec pairs) e
238       = addTopLevelIshIds binders (
239            mapMM do_pair pairs          `thenMM` \ pairs' ->
240            do_expr e                    `thenMM` \ e' ->
241            returnMM (StgRec pairs', e')
242         )
243       where
244         binders = map fst pairs
245         do_pair (b, rhs) 
246            = do_rhs rhs                 `thenMM` \ rhs2 ->
247              returnMM (b, rhs2)
248
249     ----------------------------------
250     do_rhs :: StgRhs -> MassageM StgRhs
251         -- We play much the same game as we did in do_top_rhs above;
252         -- but we don't have to worry about cafs etc.
253
254 {-
255     do_rhs (StgRhsClosure closure_cc bi srt fv u [] (StgSCC ty cc (StgCon (DataCon con) args _)))
256       | not (isSccCountCostCentre cc)
257       = collectCC cc `thenMM_`
258         returnMM (StgRhsCon cc con args)
259 -}
260
261 {-
262     do_rhs (StgRhsClosure _ bi srt fv u args (StgSCC ty cc expr))
263       | not (isSccCountCostCentre cc)
264       = collectCC cc                            `thenMM_`
265         set_prevailing_cc cc (do_expr expr)     `thenMM` \ expr' ->
266         returnMM (StgRhsClosure cc bi srt fv u args expr')
267 -}
268
269     do_rhs (StgRhsClosure cc bi srt fv u [] body)
270       = do_expr body                            `thenMM` \ body' ->
271         returnMM (StgRhsClosure currentCCS bi srt fv u [] body')
272
273     do_rhs (StgRhsClosure cc bi srt fv u args body)
274       = set_lambda_cc (do_expr body)            `thenMM` \ body' ->
275         get_prevailing_cc                       `thenMM` \ prev_ccs ->
276         returnMM (StgRhsClosure currentCCS bi srt fv u args body')
277
278     do_rhs (StgRhsCon cc con args)
279       = returnMM (StgRhsCon currentCCS con args)
280 \end{code}
281
282 %************************************************************************
283 %*                                                                      *
284 \subsection{Boxing higher-order args}
285 %*                                                                      *
286 %************************************************************************
287
288 Boxing is *turned off* at the moment, until we can figure out how to
289 do it properly in general.
290
291 \begin{code}
292 boxHigherOrderArgs
293     :: ([StgArg] -> StgExpr)
294                         -- An application lacking its arguments
295     -> [StgArg]         -- arguments which we might box
296     -> MassageM StgExpr
297
298 #ifndef PROF_DO_BOXING
299 boxHigherOrderArgs almost_expr args
300    = returnMM (almost_expr args)
301 #else
302 boxHigherOrderArgs almost_expr args
303   = getTopLevelIshIds           `thenMM` \ ids ->
304     mapAccumMM (do_arg ids) [] args     `thenMM` \ (let_bindings, new_args) ->
305     returnMM (foldr (mk_stg_let currentCCS) (almost_expr new_args) let_bindings)
306   where
307     ---------------
308
309     do_arg ids bindings arg@(StgVarArg old_var)
310         |  (not (isLocallyDefined old_var) || elemVarSet old_var ids)
311         && isFunType var_type
312       =     -- make a trivial let-binding for the top-level function
313         getUniqueMM             `thenMM` \ uniq ->
314         let
315             new_var = mkSysLocal SLIT("sf") uniq var_type
316         in
317         returnMM ( (new_var, old_var) : bindings, StgVarArg new_var )
318       where
319         var_type = idType old_var
320
321     do_arg ids bindings arg = returnMM (bindings, arg)
322
323     ---------------
324     mk_stg_let :: CostCentreStack -> (Id, Id) -> StgExpr -> StgExpr
325
326     mk_stg_let cc (new_var, old_var) body
327       = let
328             rhs_body    = StgApp old_var [{-args-}]
329             rhs_closure = StgRhsClosure cc stgArgOcc NoSRT [{-fvs-}] ReEntrant [{-args-}] rhs_body
330         in
331         StgLet (StgNonRec new_var rhs_closure) body
332       where
333         bOGUS_LVs = emptyUniqSet -- easier to print than: panic "mk_stg_let: LVs"
334
335 isFunType var_type 
336   = case splitForAllTys var_type of
337         (_, ty) -> case splitTyConApp_maybe ty of
338                         Just (tycon,_) | isFunTyCon tycon -> True
339                         _ -> False
340 #endif
341 \end{code}
342
343 %************************************************************************
344 %*                                                                      *
345 \subsection{Boring monad stuff for this}
346 %*                                                                      *
347 %************************************************************************
348
349 \begin{code}
350 type MassageM result
351   =  Module             -- module name
352   -> CostCentreStack    -- prevailing CostCentre
353                         -- if none, subsumedCosts at top-level
354                         -- useCurrentCostCentre at nested levels
355   -> UniqSupply
356   -> VarSet             -- toplevel-ish Ids for boxing
357   -> CollectedCCs
358   -> (CollectedCCs, result)
359
360 -- the initMM function also returns the final CollectedCCs
361
362 initMM :: Module        -- module name, which we may consult
363        -> UniqSupply
364        -> MassageM a
365        -> (CollectedCCs, a)
366
367 initMM mod_name init_us m = m mod_name noCCS init_us emptyVarSet ([],[],[])
368
369 thenMM  :: MassageM a -> (a -> MassageM b) -> MassageM b
370 thenMM_ :: MassageM a -> (MassageM b) -> MassageM b
371
372 thenMM expr cont mod scope_cc us ids ccs
373   = case splitUniqSupply us     of { (s1, s2) ->
374     case (expr mod scope_cc s1 ids ccs) of { (ccs2, result) ->
375     cont result mod scope_cc s2 ids ccs2 }}
376
377 thenMM_ expr cont mod scope_cc us ids ccs
378   = case splitUniqSupply us     of { (s1, s2) ->
379     case (expr mod scope_cc s1 ids ccs) of { (ccs2, _) ->
380     cont mod scope_cc s2 ids ccs2 }}
381
382 returnMM :: a -> MassageM a
383 returnMM result mod scope_cc us ids ccs = (ccs, result)
384
385 nopMM :: MassageM ()
386 nopMM mod scope_cc us ids ccs = (ccs, ())
387
388 mapMM :: (a -> MassageM b) -> [a] -> MassageM [b]
389 mapMM f [] = returnMM []
390 mapMM f (m:ms)
391   = f m         `thenMM` \ r  ->
392     mapMM f ms  `thenMM` \ rs ->
393     returnMM (r:rs)
394
395 mapAccumMM :: (acc -> x -> MassageM (acc, y)) -> acc -> [x] -> MassageM (acc, [y])
396 mapAccumMM f b [] = returnMM (b, [])
397 mapAccumMM f b (m:ms)
398   = f b m               `thenMM` \ (b2, r)  ->
399     mapAccumMM f b2 ms  `thenMM` \ (b3, rs) ->
400     returnMM (b3, r:rs)
401
402 getUniqueMM :: MassageM Unique
403 getUniqueMM mod scope_cc us ids ccs = (ccs, uniqFromSupply us)
404
405 addTopLevelIshId :: Id -> MassageM a -> MassageM a
406 addTopLevelIshId id scope mod scope_cc us ids ccs
407   | isCurrentCCS scope_cc = scope mod scope_cc us ids ccs
408   | otherwise             = scope mod scope_cc us (extendVarSet ids id) ccs
409
410 addTopLevelIshIds :: [Id] -> MassageM a -> MassageM a
411 addTopLevelIshIds [] cont = cont
412 addTopLevelIshIds (id:ids) cont 
413   = addTopLevelIshId id (addTopLevelIshIds ids cont)
414
415 getTopLevelIshIds :: MassageM VarSet
416 getTopLevelIshIds mod scope_cc us ids ccs = (ccs, ids)
417 \end{code}
418
419 The prevailing CCS is used to tell whether we're in a top-levelish
420 position, where top-levelish is defined as "not inside a lambda".
421 Prevailing CCs used to be used for something much more complicated,
422 I'm sure --SDM
423
424 \begin{code}
425 set_lambda_cc :: MassageM a -> MassageM a
426 set_lambda_cc action mod scope_cc us ids ccs
427   = action mod currentCCS us ids ccs
428
429 set_prevailing_cc :: CostCentreStack -> MassageM a -> MassageM a
430 set_prevailing_cc cc_to_set_to action mod scope_cc us ids ccs
431   = action mod cc_to_set_to us ids ccs
432
433 get_prevailing_cc :: MassageM CostCentreStack
434 get_prevailing_cc mod scope_cc us ids ccs = (ccs, scope_cc)
435 \end{code}
436
437 \begin{code}
438 collectCC :: CostCentre -> MassageM ()
439
440 collectCC cc mod_name scope_cc us ids (local_ccs, extern_ccs, ccss)
441   = ASSERT(not (noCCAttached cc))
442     if (cc `ccFromThisModule` mod_name) then
443         ((cc : local_ccs, extern_ccs, ccss), ())
444     else -- must declare it "extern"
445         ((local_ccs, cc : extern_ccs, ccss), ())
446
447 collectCCS :: CostCentreStack -> MassageM ()
448
449 collectCCS ccs mod_name scope_cc us ids (local_ccs, extern_ccs, ccss)
450   = ASSERT(not (noCCSAttached ccs))
451     ((local_ccs, extern_ccs, ccs : ccss), ())
452 \end{code}