SCCfinal.lhs 13.4 KB
Newer Older
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
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

This is now a sort-of-normal STG-to-STG pass (WDP 94/06), run by stg2stg.

* Traverses the STG program collecting the cost centres. These are
  required to declare the cost centres at the start of code
  generation.

  Note: because of cross-module unfolding, some of these cost centres
  may be from other modules.  But will still have to give them
  "extern" declarations.

* Puts on CAF cost-centres if the user has asked for individual CAF
  cost-centres.

* Ditto for individual DICT cost-centres.

* Boxes top-level inherited functions passed as arguments.

* "Distributes" given cost-centres to all as-yet-unmarked RHSs.

\begin{code}
module SCCfinal ( stgMassageForProfiling ) where

28
#include "HsVersions.h"
29
30

import StgSyn
31

sof's avatar
sof committed
32
import CmdLineOpts	( opt_AutoSccsOnIndividualCafs )
33
import CostCentre	-- lots of things
34
import Id		( Id )
sof's avatar
sof committed
35
import Module		( Module )
36
import UniqSupply	( uniqFromSupply, splitUniqSupply, UniqSupply )
sof's avatar
sof committed
37
import Unique           ( Unique )
38
import VarSet
39
import ListSetOps	( removeDups )
sof's avatar
sof committed
40
import Outputable	
41
42
43
44
45

infixr 9 `thenMM`, `thenMM_`
\end{code}

\begin{code}
46
type CollectedCCs = ([CostCentre],	-- locally defined ones
47
48
		     [CostCentre],	-- ones needing "extern" decls
		     [CostCentreStack]) -- singleton stacks (for CAFs)
49
50

stgMassageForProfiling
51
	:: Module			-- module name
52
53
	-> UniqSupply		    	-- unique supply
	-> [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
63
64
65
66
	(fixed_ccs, fixed_cc_stacks)
	  = if opt_AutoSccsOnIndividualCafs
	    then ([],[])  -- don't need "all CAFs" CC 
			  -- (for Prelude, we use PreludeCC)
	    else ([all_cafs_cc], [all_cafs_ccs])
67
68
69
70

	local_ccs_no_dups  = fst (removeDups cmpCostCentre local_ccs)
	extern_ccs_no_dups = fst (removeDups cmpCostCentre extern_ccs)
    in
71
72
73
    ((fixed_ccs ++ local_ccs_no_dups, 
      extern_ccs_no_dups, 
      fixed_cc_stacks ++ cc_stacks), stg_binds2)
74
75
  where

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
86
87
      = do_top_rhs b rhs 		`thenMM` \ rhs' ->
	addTopLevelIshId b (
	   do_top_bindings bs `thenMM` \bs' ->
88
	   returnMM (StgNonRec srt b rhs' : bs')
89
90
	)

91
    do_top_bindings (StgRec srt pairs : bs)
92
93
94
      = addTopLevelIshIds binders (
	   mapMM do_pair pairs		`thenMM` \ pairs2 ->
	   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
101
102
103
	   = do_top_rhs b rhs	`thenMM` \ rhs2 ->
	     returnMM (b, rhs2)

    ----------
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
109
	-- Trivial _scc_ around nothing but static data
	-- 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
118
119
120
121
        && not (isSccCountCostCentre cc)
	-- Top level CAF without a cost centre attached
	-- Attach and collect cc of trivial _scc_ in body
      = collectCC cc					`thenMM_`
	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
128
	-- Top level CAF without a cost centre attached
	-- Attach CAF cc (collect if individual CAF ccs)
129
      = (if opt_AutoSccsOnIndividualCafs 
130
		then let cc = mkAutoCC binder mod_name CafCC
131
132
133
134
135
136
137
			 ccs = mkSingletonCCS cc
		     in
		     collectCC  cc  `thenMM_`
		     collectCCS ccs `thenMM_`
		     returnMM ccs
		else 
		     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
143
	-- Top level CAF with cost centre attached
	-- 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
153
154
155
      | otherwise
      = pprPanic "SCCfinal: CAF with cc:" (ppr no_ccs)

    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
165
    do_expr (StgLit l) = returnMM (StgLit l)

166
167
    do_expr (StgApp fn args)
      = boxHigherOrderArgs (StgApp fn) args
168

169
170
171
    do_expr (StgConApp con args)
      = boxHigherOrderArgs (\args -> StgConApp con args) args

172
173
    do_expr (StgOpApp con args res_ty)
      = boxHigherOrderArgs (\args -> StgOpApp con args res_ty) args
174

175
    do_expr (StgSCC cc expr)	-- Ha, we found a cost centre!
176
177
      = collectCC cc		`thenMM_`
	do_expr expr		`thenMM` \ expr' ->
178
	returnMM (StgSCC cc expr')
179

180
    do_expr (StgCase expr fv1 fv2 bndr srt alts)
181
182
      = do_expr expr		`thenMM` \ expr' ->
	do_alts alts		`thenMM` \ alts' ->
183
	returnMM (StgCase expr' fv1 fv2 bndr srt alts')
184
      where
185
	do_alts (StgAlgAlts tycon alts def) 
186
187
	  = mapMM do_alt alts 	`thenMM` \ alts' ->
	    do_deflt def	`thenMM` \ def' ->
188
	    returnMM (StgAlgAlts tycon alts' def')
189
190
191
192
193
	  where
	    do_alt (id, bs, use_mask, e)
	      = do_expr e `thenMM` \ e' ->
		returnMM (id, bs, use_mask, e')

194
	do_alts (StgPrimAlts tycon alts def) 
195
196
	  = mapMM do_alt alts	`thenMM` \ alts' ->
	    do_deflt def	`thenMM` \ def' ->
197
	    returnMM (StgPrimAlts tycon alts' def')
198
199
200
201
202
203
	  where
	    do_alt (l,e)
	      = do_expr e `thenMM` \ e' ->
		returnMM (l,e')

	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
210
211
212
213
214
215
	= do_let b e `thenMM` \ (b,e) ->
	  returnMM (StgLet b e)

    do_expr (StgLetNoEscape lvs1 lvs2 b e)
	= do_let b e `thenMM` \ (b,e) ->
	  returnMM (StgLetNoEscape lvs1 lvs2 b e)

216
217
218
219
#ifdef DEBUG
    do_expr other = pprPanic "SCCfinal.do_expr" (ppr other)
#endif

220
221
    ----------------------------------

222
    do_let (StgNonRec srt b rhs) e
223
224
225
      = do_rhs rhs		 	`thenMM` \ rhs' ->
	addTopLevelIshId b (
	  do_expr e		  	`thenMM` \ e' ->
226
	  returnMM (StgNonRec srt b rhs',e')
227
228
        )

229
    do_let (StgRec srt pairs) e
230
231
232
      = addTopLevelIshIds binders (
	   mapMM do_pair pairs	 	`thenMM` \ pairs' ->
	   do_expr e		  	`thenMM` \ e' ->
233
	   returnMM (StgRec srt pairs', e')
234
	)
235
      where
236
237
238
239
	binders = map fst pairs
	do_pair (b, rhs) 
	   = do_rhs rhs			`thenMM` \ rhs2 ->
	     returnMM (b, rhs2)
240

241
    ----------------------------------
242
    do_rhs :: StgRhs -> MassageM StgRhs
243
	-- We play much the same game as we did in do_top_rhs above;
244
	-- but we don't have to worry about cafs etc.
245

246
{-
247
    do_rhs (StgRhsClosure closure_cc bi fv u [] (StgSCC ty cc (StgCon (DataCon con) args _)))
248
      | not (isSccCountCostCentre cc)
249
250
      = collectCC cc `thenMM_`
	returnMM (StgRhsCon cc con args)
251
-}
252

253
254
255
256
257
258
259
260
261
262
263
    do_rhs (StgRhsClosure _ bi fv u args expr)
      = slurpSCCs currentCCS expr		`thenMM` \ (expr', ccs) ->
	do_expr expr'				`thenMM` \ expr'' ->
	returnMM (StgRhsClosure ccs bi fv u args expr'')
      where
	slurpSCCs ccs (StgSCC cc e) 
	     = collectCC cc 			`thenMM_`
	       slurpSCCs ccs e			`thenMM` \ (e', ccs')  ->
	       returnMM (e', pushCCOnCCS cc ccs')
	slurpSCCs ccs e 
	     = returnMM (e, ccs)
264
265

    do_rhs (StgRhsCon cc con args)
266
      = returnMM (StgRhsCon currentCCS con args)
267
268
269
270
271
272
273
274
\end{code}

%************************************************************************
%*									*
\subsection{Boxing higher-order args}
%*									*
%************************************************************************

275
276
277
Boxing is *turned off* at the moment, until we can figure out how to
do it properly in general.

278
279
\begin{code}
boxHigherOrderArgs
280
    :: ([StgArg] -> StgExpr)
281
			-- An application lacking its arguments
282
    -> [StgArg]		-- arguments which we might box
283
    -> MassageM StgExpr
284

285
286
287
288
#ifndef PROF_DO_BOXING
boxHigherOrderArgs almost_expr args
   = returnMM (almost_expr args)
#else
289
boxHigherOrderArgs almost_expr args
290
291
292
  = getTopLevelIshIds		`thenMM` \ ids ->
    mapAccumMM (do_arg ids) [] args	`thenMM` \ (let_bindings, new_args) ->
    returnMM (foldr (mk_stg_let currentCCS) (almost_expr new_args) let_bindings)
293
294
295
  where
    ---------------

296
    do_arg ids bindings arg@(StgVarArg old_var)
297
	|  (not (isLocalVar old_var) || elemVarSet old_var ids)
298
299
300
301
302
	&& isFunType var_type
      =     -- make a trivial let-binding for the top-level function
	getUniqueMM		`thenMM` \ uniq ->
	let
	    new_var = mkSysLocal SLIT("sf") uniq var_type
303
	in
304
305
306
307
308
	returnMM ( (new_var, old_var) : bindings, StgVarArg new_var )
      where
	var_type = idType old_var

    do_arg ids bindings arg = returnMM (bindings, arg)
309
310

    ---------------
311
    mk_stg_let :: CostCentreStack -> (Id, Id) -> StgExpr -> StgExpr
312
313
314

    mk_stg_let cc (new_var, old_var) body
      = let
315
	    rhs_body    = StgApp old_var [{-args-}]
316
	    rhs_closure = StgRhsClosure cc stgArgOcc [{-fvs-}] ReEntrant [{-args-}] rhs_body
317
        in
318
	StgLet (StgNonRec NoSRT{-eeek!!!-} new_var rhs_closure) body
319
      where
320
	bOGUS_LVs = emptyUniqSet -- easier to print than: panic "mk_stg_let: LVs"
321
322
323

isFunType var_type 
  = case splitForAllTys var_type of
324
	(_, ty) -> maybeToBool (splitFunTy_Maybe ty)
325
#endif
326
327
328
329
330
331
332
333
334
335
\end{code}

%************************************************************************
%*									*
\subsection{Boring monad stuff for this}
%*									*
%************************************************************************

\begin{code}
type MassageM result
336
  =  Module		-- module name
337
  -> CostCentreStack	-- prevailing CostCentre
338
			-- if none, subsumedCosts at top-level
339
			-- currentCostCentre at nested levels
340
  -> UniqSupply
341
  -> VarSet		-- toplevel-ish Ids for boxing
342
343
344
  -> CollectedCCs
  -> (CollectedCCs, result)

345
-- the initMM function also returns the final CollectedCCs
346

347
initMM :: Module	-- module name, which we may consult
348
       -> UniqSupply
349
350
351
       -> MassageM a
       -> (CollectedCCs, a)

352
initMM mod_name init_us m = m mod_name noCCS init_us emptyVarSet ([],[],[])
353
354
355
356

thenMM  :: MassageM a -> (a -> MassageM b) -> MassageM b
thenMM_ :: MassageM a -> (MassageM b) -> MassageM b

357
thenMM expr cont mod scope_cc us ids ccs
358
  = case splitUniqSupply us	of { (s1, s2) ->
359
360
    case (expr mod scope_cc s1 ids ccs) of { (ccs2, result) ->
    cont result mod scope_cc s2 ids ccs2 }}
361

362
thenMM_ expr cont mod scope_cc us ids ccs
363
  = case splitUniqSupply us	of { (s1, s2) ->
364
365
    case (expr mod scope_cc s1 ids ccs)	of { (ccs2, _) ->
    cont mod scope_cc s2 ids ccs2 }}
366
367

returnMM :: a -> MassageM a
368
returnMM result mod scope_cc us ids ccs = (ccs, result)
369
370

nopMM :: MassageM ()
371
nopMM mod scope_cc us ids ccs = (ccs, ())
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387

mapMM :: (a -> MassageM b) -> [a] -> MassageM [b]
mapMM f [] = returnMM []
mapMM f (m:ms)
  = f m		`thenMM` \ r  ->
    mapMM f ms	`thenMM` \ rs ->
    returnMM (r:rs)

mapAccumMM :: (acc -> x -> MassageM (acc, y)) -> acc -> [x] -> MassageM (acc, [y])
mapAccumMM f b [] = returnMM (b, [])
mapAccumMM f b (m:ms)
  = f b m		`thenMM` \ (b2, r)  ->
    mapAccumMM f b2 ms	`thenMM` \ (b3, rs) ->
    returnMM (b3, r:rs)

getUniqueMM :: MassageM Unique
388
getUniqueMM mod scope_cc us ids ccs = (ccs, uniqFromSupply us)
389

390
391
392
393
addTopLevelIshId :: Id -> MassageM a -> MassageM a
addTopLevelIshId id scope mod scope_cc us ids ccs
  | isCurrentCCS scope_cc = scope mod scope_cc us ids ccs
  | otherwise             = scope mod scope_cc us (extendVarSet ids id) ccs
394

395
396
397
398
addTopLevelIshIds :: [Id] -> MassageM a -> MassageM a
addTopLevelIshIds [] cont = cont
addTopLevelIshIds (id:ids) cont 
  = addTopLevelIshId id (addTopLevelIshIds ids cont)
399

400
401
402
403
404
405
406
407
getTopLevelIshIds :: MassageM VarSet
getTopLevelIshIds mod scope_cc us ids ccs = (ccs, ids)
\end{code}

The prevailing CCS is used to tell whether we're in a top-levelish
position, where top-levelish is defined as "not inside a lambda".
Prevailing CCs used to be used for something much more complicated,
I'm sure --SDM
408

409
\begin{code}
410
set_lambda_cc :: MassageM a -> MassageM a
411
412
set_lambda_cc action mod scope_cc us ids ccs
  = action mod currentCCS us ids ccs
413

414
415
416
set_prevailing_cc :: CostCentreStack -> MassageM a -> MassageM a
set_prevailing_cc cc_to_set_to action mod scope_cc us ids ccs
  = action mod cc_to_set_to us ids ccs
417

418
get_prevailing_cc :: MassageM CostCentreStack
419
get_prevailing_cc mod scope_cc us ids ccs = (ccs, scope_cc)
420
421
422
423
424
\end{code}

\begin{code}
collectCC :: CostCentre -> MassageM ()

425
collectCC cc mod_name scope_cc us ids (local_ccs, extern_ccs, ccss)
426
  = ASSERT(not (noCCAttached cc))
427
    if (cc `ccFromThisModule` mod_name) then
428
	((cc : local_ccs, extern_ccs, ccss), ())
429
    else -- must declare it "extern"
430
431
432
433
	((local_ccs, cc : extern_ccs, ccss), ())

collectCCS :: CostCentreStack -> MassageM ()

434
collectCCS ccs mod_name scope_cc us ids (local_ccs, extern_ccs, ccss)
435
436
  = ASSERT(not (noCCSAttached ccs))
    ((local_ccs, extern_ccs, ccs : ccss), ())
437
\end{code}