SCCfinal.lhs 14.2 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

Ian Lynagh's avatar
Ian Lynagh committed
32
33
import StaticFlags      ( opt_AutoSccsOnIndividualCafs )
import CostCentre       -- lots of things
Simon Marlow's avatar
Simon Marlow committed
34
35
import Id
import Name
36
import Module
Ian Lynagh's avatar
Ian Lynagh committed
37
import UniqSupply       ( splitUniqSupply, UniqSupply )
Simon Marlow's avatar
Simon Marlow committed
38
#ifdef PROF_DO_BOXING
Ian Lynagh's avatar
Ian Lynagh committed
39
import UniqSupply       ( uniqFromSupply )
Simon Marlow's avatar
Simon Marlow committed
40
#endif
41
import VarSet
Ian Lynagh's avatar
Ian Lynagh committed
42
import ListSetOps       ( removeDups )
twanvl's avatar
twanvl committed
43
import Outputable
44
45
46
47
\end{code}

\begin{code}
stgMassageForProfiling
Ian Lynagh's avatar
Ian Lynagh committed
48
49
50
51
52
        :: PackageId
        -> Module                       -- module name
        -> UniqSupply                   -- unique supply
        -> [StgBinding]                 -- input
        -> (CollectedCCs, [StgBinding])
53

Simon Marlow's avatar
Simon Marlow committed
54
stgMassageForProfiling this_pkg mod_name us stg_binds
55
  = let
Ian Lynagh's avatar
Ian Lynagh committed
56
57
58
59
60
61
62
63
64
65
66
67
        ((local_ccs, extern_ccs, cc_stacks),
         stg_binds2)
          = initMM mod_name us (do_top_bindings stg_binds)

        (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])

        local_ccs_no_dups  = fst (removeDups cmpCostCentre local_ccs)
        extern_ccs_no_dups = fst (removeDups cmpCostCentre extern_ccs)
68
    in
Ian Lynagh's avatar
Ian Lynagh committed
69
70
    ((fixed_ccs ++ local_ccs_no_dups,
      extern_ccs_no_dups,
71
      fixed_cc_stacks ++ cc_stacks), stg_binds2)
72
73
  where

74
    all_cafs_cc  = mkAllCafsCC mod_name
75
    all_cafs_ccs = mkSingletonCCS all_cafs_cc
76
77

    ----------
78
    do_top_bindings :: [StgBinding] -> MassageM [StgBinding]
79

twanvl's avatar
twanvl committed
80
    do_top_bindings [] = return []
81

twanvl's avatar
twanvl committed
82
83
84
85
86
    do_top_bindings (StgNonRec b rhs : bs) = do
        rhs' <- do_top_rhs b rhs
        addTopLevelIshId b $ do
           bs' <- do_top_bindings bs
           return (StgNonRec b rhs' : bs')
87

88
    do_top_bindings (StgRec pairs : bs)
twanvl's avatar
twanvl committed
89
90
91
92
      = addTopLevelIshIds binders $ do
           pairs2 <- mapM do_pair pairs
           bs' <- do_top_bindings bs
           return (StgRec pairs2 : bs')
93
      where
twanvl's avatar
twanvl committed
94
95
96
97
        binders = map fst pairs
        do_pair (b, rhs) = do
             rhs2 <- do_top_rhs b rhs
             return (b, rhs2)
98
99

    ----------
100
    do_top_rhs :: Id -> StgRhs -> MassageM StgRhs
101

Ian Lynagh's avatar
Ian Lynagh committed
102
    do_top_rhs _ (StgRhsClosure _ _ _ _ _ [] (StgSCC cc (StgConApp con args)))
Simon Marlow's avatar
Simon Marlow committed
103
      | not (isSccCountCostCentre cc) && not (isDllConApp this_pkg con args)
Ian Lynagh's avatar
Ian Lynagh committed
104
105
        -- Trivial _scc_ around nothing but static data
        -- Eliminate _scc_ ... and turn into StgRhsCon
106

Ian Lynagh's avatar
Ian Lynagh committed
107
        -- isDllConApp checks for LitLit args too
twanvl's avatar
twanvl committed
108
      = return (StgRhsCon dontCareCCS con args)
109

110
{- Can't do this one with cost-centre stacks:  --SDM
111
    do_top_rhs binder (StgRhsClosure no_cc bi fv u [] (StgSCC ty cc expr))
112
      | (noCCSAttached no_cc || currentOrSubsumedCCS no_cc)
113
        && not (isSccCountCostCentre cc)
Ian Lynagh's avatar
Ian Lynagh committed
114
115
        -- Top level CAF without a cost centre attached
        -- Attach and collect cc of trivial _scc_ in body
twanvl's avatar
twanvl committed
116
117
118
      = do collectCC cc
           expr' <- set_prevailing_cc cc (do_expr expr)
           return (StgRhsClosure cc bi fv u [] expr')
119
-}
120

121
    do_top_rhs binder (StgRhsClosure no_cc bi fv u srt [] body)
twanvl's avatar
twanvl committed
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
      | noCCSAttached no_cc || currentOrSubsumedCCS no_cc = do
        -- Top level CAF without a cost centre attached
        -- Attach CAF cc (collect if individual CAF ccs)
        caf_ccs <- if opt_AutoSccsOnIndividualCafs
                   then let cc = mkAutoCC binder modl CafCC
                            ccs = mkSingletonCCS cc
                                   -- careful: the binder might be :Main.main,
                                   -- which doesn't belong to module mod_name.
                                   -- bug #249, tests prof001, prof002
                            modl | Just m <- nameModule_maybe (idName binder) = m
                                 | otherwise = mod_name
                        in do
                        collectNewCC  cc
                        collectCCS ccs
                        return ccs
                   else
                        return all_cafs_ccs
        body' <- set_prevailing_cc caf_ccs (do_expr body)
        return (StgRhsClosure caf_ccs bi fv u srt [] body')
141

Ian Lynagh's avatar
Ian Lynagh committed
142
    do_top_rhs _ (StgRhsClosure cc _ _ _ _ [] _)
Ian Lynagh's avatar
Ian Lynagh committed
143
144
        -- Top level CAF with cost centre attached
        -- Should this be a CAF cc ??? Does this ever occur ???
145
      = pprPanic "SCCfinal: CAF with cc:" (ppr cc)
146

Ian Lynagh's avatar
Ian Lynagh committed
147
    do_top_rhs _ (StgRhsClosure no_ccs bi fv u srt args body)
Ian Lynagh's avatar
Ian Lynagh committed
148
        -- Top level function, probably subsumed
149
      | noCCSAttached no_ccs
twanvl's avatar
twanvl committed
150
151
      = do body' <- set_lambda_cc (do_expr body)
           return (StgRhsClosure subsumedCCS bi fv u srt args body')
152

153
154
155
      | otherwise
      = pprPanic "SCCfinal: CAF with cc:" (ppr no_ccs)

Ian Lynagh's avatar
Ian Lynagh committed
156
    do_top_rhs _ (StgRhsCon _ con args)
Ian Lynagh's avatar
Ian Lynagh committed
157
158
159
        -- Top-level (static) data is not counted in heap
        -- profiles; nor do we set CCCS from it; so we
        -- just slam in dontCareCostCentre
twanvl's avatar
twanvl committed
160
      = return (StgRhsCon dontCareCCS con args)
161
162

    ------
163
    do_expr :: StgExpr -> MassageM StgExpr
164

twanvl's avatar
twanvl committed
165
    do_expr (StgLit l) = return (StgLit l)
166

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

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

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

Ian Lynagh's avatar
Ian Lynagh committed
176
    do_expr (StgSCC cc expr) = do -- Ha, we found a cost centre!
twanvl's avatar
twanvl committed
177
178
179
        collectCC cc
        expr' <- do_expr expr
        return (StgSCC cc expr')
180

twanvl's avatar
twanvl committed
181
182
183
184
    do_expr (StgCase expr fv1 fv2 bndr srt alt_type alts) = do
        expr' <- do_expr expr
        alts' <- mapM do_alt alts
        return (StgCase expr' fv1 fv2 bndr srt alt_type alts')
185
      where
twanvl's avatar
twanvl committed
186
187
188
        do_alt (id, bs, use_mask, e) = do
            e' <- do_expr e
            return (id, bs, use_mask, e')
189

twanvl's avatar
twanvl committed
190
191
192
    do_expr (StgLet b e) = do
          (b,e) <- do_let b e
          return (StgLet b e)
193

twanvl's avatar
twanvl committed
194
195
196
    do_expr (StgLetNoEscape lvs1 lvs2 b e) = do
          (b,e) <- do_let b e
          return (StgLetNoEscape lvs1 lvs2 b e)
197

twanvl's avatar
twanvl committed
198
199
200
    do_expr (StgTick m n expr) = do
          expr' <- do_expr expr
          return (StgTick m n expr')
andy@galois.com's avatar
andy@galois.com committed
201

202
203
    do_expr other = pprPanic "SCCfinal.do_expr" (ppr other)

204
205
    ----------------------------------

twanvl's avatar
twanvl committed
206
207
208
209
210
    do_let (StgNonRec b rhs) e = do
        rhs' <- do_rhs rhs
        addTopLevelIshId b $ do
          e' <- do_expr e
          return (StgNonRec b rhs',e')
211

212
    do_let (StgRec pairs) e
twanvl's avatar
twanvl committed
213
214
215
216
      = addTopLevelIshIds binders $ do
           pairs' <- mapM do_pair pairs
           e' <- do_expr e
           return (StgRec pairs', e')
217
      where
twanvl's avatar
twanvl committed
218
219
220
221
        binders = map fst pairs
        do_pair (b, rhs) = do
             rhs2 <- do_rhs rhs
             return (b, rhs2)
222

223
    ----------------------------------
224
    do_rhs :: StgRhs -> MassageM StgRhs
Ian Lynagh's avatar
Ian Lynagh committed
225
226
        -- We play much the same game as we did in do_top_rhs above;
        -- but we don't have to worry about cafs etc.
227

228
{-
229
    do_rhs (StgRhsClosure closure_cc bi fv u [] (StgSCC ty cc (StgCon (DataCon con) args _)))
230
      | not (isSccCountCostCentre cc)
twanvl's avatar
twanvl committed
231
      = do collectCC cc
Ian Lynagh's avatar
Ian Lynagh committed
232
           return (StgRhsCon cc con args)
233
-}
234

twanvl's avatar
twanvl committed
235
236
237
238
    do_rhs (StgRhsClosure _ bi fv u srt args expr) = do
        (expr', ccs) <- slurpSCCs currentCCS expr
        expr'' <- do_expr expr'
        return (StgRhsClosure ccs bi fv u srt args expr'')
239
      where
Ian Lynagh's avatar
Ian Lynagh committed
240
        slurpSCCs ccs (StgSCC cc e)
twanvl's avatar
twanvl committed
241
242
             = do collectCC cc
                  slurpSCCs (cc `pushCCOnCCS` ccs) e
Ian Lynagh's avatar
Ian Lynagh committed
243
244
        slurpSCCs ccs e
             = return (e, ccs)
245

Ian Lynagh's avatar
Ian Lynagh committed
246
    do_rhs (StgRhsCon _ con args)
twanvl's avatar
twanvl committed
247
      = return (StgRhsCon currentCCS con args)
248
249
250
\end{code}

%************************************************************************
Ian Lynagh's avatar
Ian Lynagh committed
251
%*                                                                      *
252
\subsection{Boxing higher-order args}
Ian Lynagh's avatar
Ian Lynagh committed
253
%*                                                                      *
254
255
%************************************************************************

256
257
258
Boxing is *turned off* at the moment, until we can figure out how to
do it properly in general.

259
260
\begin{code}
boxHigherOrderArgs
261
    :: ([StgArg] -> StgExpr)
Ian Lynagh's avatar
Ian Lynagh committed
262
263
                        -- An application lacking its arguments
    -> [StgArg]         -- arguments which we might box
264
    -> MassageM StgExpr
265

266
267
#ifndef PROF_DO_BOXING
boxHigherOrderArgs almost_expr args
twanvl's avatar
twanvl committed
268
   = return (almost_expr args)
269
#else
twanvl's avatar
twanvl committed
270
271
272
273
boxHigherOrderArgs almost_expr args = do
    ids <- getTopLevelIshIds
    (let_bindings, new_args) <- mapAccumLM (do_arg ids) [] args
    return (foldr (mk_stg_let currentCCS) (almost_expr new_args) let_bindings)
274
275
276
  where
    ---------------

277
    do_arg ids bindings arg@(StgVarArg old_var)
Ian Lynagh's avatar
Ian Lynagh committed
278
279
        |  (not (isLocalVar old_var) || elemVarSet old_var ids)
        && isFunTy (dropForAlls var_type)
twanvl's avatar
twanvl committed
280
      = do    -- make a trivial let-binding for the top-level function
Ian Lynagh's avatar
Ian Lynagh committed
281
282
283
284
        uniq <- getUniqueMM
        let
            new_var = mkSysLocal FSLIT("sf") uniq var_type
        return ( (new_var, old_var) : bindings, StgVarArg new_var )
285
      where
Ian Lynagh's avatar
Ian Lynagh committed
286
        var_type = idType old_var
287

twanvl's avatar
twanvl committed
288
    do_arg ids bindings arg = return (bindings, arg)
289
290

    ---------------
291
    mk_stg_let :: CostCentreStack -> (Id, Id) -> StgExpr -> StgExpr
292
293
294

    mk_stg_let cc (new_var, old_var) body
      = let
Ian Lynagh's avatar
Ian Lynagh committed
295
296
            rhs_body    = StgApp old_var [{-args-}]
            rhs_closure = StgRhsClosure cc stgArgOcc [{-fvs-}] ReEntrant NoSRT{-eeek!!!-} [{-args-}] rhs_body
297
        in
Ian Lynagh's avatar
Ian Lynagh committed
298
        StgLet (StgNonRec new_var rhs_closure) body
299
      where
Ian Lynagh's avatar
Ian Lynagh committed
300
        bOGUS_LVs = emptyUniqSet -- easier to print than: panic "mk_stg_let: LVs"
301
#endif
302
303
304
\end{code}

%************************************************************************
Ian Lynagh's avatar
Ian Lynagh committed
305
%*                                                                      *
306
\subsection{Boring monad stuff for this}
Ian Lynagh's avatar
Ian Lynagh committed
307
%*                                                                      *
308
309
310
%************************************************************************

\begin{code}
twanvl's avatar
twanvl committed
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
newtype MassageM result
  = MassageM {
      unMassageM :: Module              -- module name
                 -> CostCentreStack     -- prevailing CostCentre
                                        -- if none, subsumedCosts at top-level
                                        -- currentCostCentre at nested levels
                 -> UniqSupply
                 -> VarSet              -- toplevel-ish Ids for boxing
                 -> CollectedCCs
                 -> (CollectedCCs, result)
    }

instance Monad MassageM where
    return x = MassageM (\_ _ _ _ ccs -> (ccs, x))
    (>>=) = thenMM
    (>>)  = thenMM_
327

328
-- the initMM function also returns the final CollectedCCs
329

Ian Lynagh's avatar
Ian Lynagh committed
330
initMM :: Module        -- module name, which we may consult
331
       -> UniqSupply
332
333
334
       -> MassageM a
       -> (CollectedCCs, a)

twanvl's avatar
twanvl committed
335
initMM mod_name init_us (MassageM m) = m mod_name noCCS init_us emptyVarSet ([],[],[])
336
337
338
339

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

twanvl's avatar
twanvl committed
340
341
342
343
thenMM expr cont = MassageM $ \mod scope_cc us ids ccs ->
    case splitUniqSupply us of { (s1, s2) ->
    case unMassageM expr mod scope_cc s1 ids ccs of { (ccs2, result) ->
    unMassageM (cont result) mod scope_cc s2 ids ccs2 }}
344

twanvl's avatar
twanvl committed
345
346
347
348
thenMM_ expr cont = MassageM $ \mod scope_cc us ids ccs ->
    case splitUniqSupply us of { (s1, s2) ->
    case unMassageM expr mod scope_cc s1 ids ccs of { (ccs2, _) ->
    unMassageM cont mod scope_cc s2 ids ccs2 }}
349

Simon Marlow's avatar
Simon Marlow committed
350
#ifdef PROF_DO_BOXING
351
getUniqueMM :: MassageM Unique
twanvl's avatar
twanvl committed
352
getUniqueMM = MassageM \mod scope_cc us ids ccs -> (ccs, uniqFromSupply us)
Simon Marlow's avatar
Simon Marlow committed
353
#endif
354

355
addTopLevelIshId :: Id -> MassageM a -> MassageM a
twanvl's avatar
twanvl committed
356
357
358
359
addTopLevelIshId id scope
   = MassageM $ \mod scope_cc us ids ccs ->
      if isCurrentCCS scope_cc then unMassageM scope mod scope_cc us ids ccs
                               else unMassageM scope mod scope_cc us (extendVarSet ids id) ccs
360

361
362
addTopLevelIshIds :: [Id] -> MassageM a -> MassageM a
addTopLevelIshIds [] cont = cont
Ian Lynagh's avatar
Ian Lynagh committed
363
addTopLevelIshIds (id:ids) cont
364
  = addTopLevelIshId id (addTopLevelIshIds ids cont)
365

Ian Lynagh's avatar
Ian Lynagh committed
366
#ifdef PROF_DO_BOXING
367
getTopLevelIshIds :: MassageM VarSet
Ian Lynagh's avatar
Ian Lynagh committed
368
369
getTopLevelIshIds = MassageM $ \_mod _scope_cc _us ids ccs -> (ccs, ids)
#endif
370
371
372
373
374
375
\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
376

377
\begin{code}
378
set_lambda_cc :: MassageM a -> MassageM a
twanvl's avatar
twanvl committed
379
set_lambda_cc action
Ian Lynagh's avatar
Ian Lynagh committed
380
   =    MassageM $     \mod _scope_cc  us ids ccs
twanvl's avatar
twanvl committed
381
   -> unMassageM action mod currentCCS us ids ccs
382

383
set_prevailing_cc :: CostCentreStack -> MassageM a -> MassageM a
twanvl's avatar
twanvl committed
384
set_prevailing_cc cc_to_set_to action
Ian Lynagh's avatar
Ian Lynagh committed
385
   =    MassageM $     \mod _scope_cc    us ids ccs
twanvl's avatar
twanvl committed
386
   -> unMassageM action mod cc_to_set_to us ids ccs
387
388
389
390
\end{code}

\begin{code}
collectCC :: CostCentre -> MassageM ()
Ian Lynagh's avatar
Ian Lynagh committed
391
392
collectCC cc
 = MassageM $ \mod_name _scope_cc _us _ids (local_ccs, extern_ccs, ccss)
twanvl's avatar
twanvl committed
393
394
  -> ASSERT(not (noCCAttached cc))
     if (cc `ccFromThisModule` mod_name) then
Ian Lynagh's avatar
Ian Lynagh committed
395
        ((cc : local_ccs, extern_ccs, ccss), ())
twanvl's avatar
twanvl committed
396
     else -- must declare it "extern"
Ian Lynagh's avatar
Ian Lynagh committed
397
        ((local_ccs, cc : extern_ccs, ccss), ())
398

399
400
-- Version of collectCC used when we definitely want to declare this
-- CC as local, even if its module name is not the same as the current
Ian Lynagh's avatar
Ian Lynagh committed
401
-- module name (eg. the special :Main module) see bug #249, #1472,
402
403
-- test prof001,prof002.
collectNewCC :: CostCentre -> MassageM ()
Ian Lynagh's avatar
Ian Lynagh committed
404
405
406
collectNewCC cc
 = MassageM $ \_mod_name _scope_cc _us _ids (local_ccs, extern_ccs, ccss)
              -> ((cc : local_ccs, extern_ccs, ccss), ())
407

408
409
collectCCS :: CostCentreStack -> MassageM ()

Ian Lynagh's avatar
Ian Lynagh committed
410
411
412
413
collectCCS ccs
 = MassageM $ \_mod_name _scope_cc _us _ids (local_ccs, extern_ccs, ccss)
              -> ASSERT(not (noCCSAttached ccs))
                       ((local_ccs, extern_ccs, ccs : ccss), ())
414
\end{code}