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