(F)SLIT -> (f)sLit in CostCentre
[ghc-hetmet.git] / compiler / profiling / CostCentre.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3 %
4 \section[CostCentre]{The @CostCentre@ data type}
5
6 \begin{code}
7 {-# OPTIONS -fno-warn-incomplete-patterns #-}
8 -- The above warning supression flag is a temporary kludge.
9 -- While working on this module you are encouraged to remove it and fix
10 -- any warnings in the module. See
11 --     http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
12 -- for details
13
14 module CostCentre (
15         CostCentre(..), CcName, IsDupdCC(..), IsCafCC(..),
16                 -- All abstract except to friend: ParseIface.y
17
18         CostCentreStack,
19         CollectedCCs,
20         noCCS, subsumedCCS, currentCCS, overheadCCS, dontCareCCS,
21         noCostCentre, noCCAttached,
22         noCCSAttached, isCurrentCCS,  isSubsumedCCS, currentOrSubsumedCCS,
23         isDerivedFromCurrentCCS, maybeSingletonCCS,
24         decomposeCCS,
25
26         mkUserCC, mkAutoCC, mkAllCafsCC, 
27         mkSingletonCCS, dupifyCC, pushCCOnCCS,
28         isCafCCS, isCafCC,
29         isSccCountCostCentre,
30         sccAbleCostCentre,
31         ccFromThisModule,
32
33         pprCostCentreCore,
34         costCentreUserName,
35
36         cmpCostCentre   -- used for removing dups in a list
37     ) where
38
39 import Var              ( Id )
40 import Name
41 import Module           ( Module )
42 import Outputable       
43 import FastTypes
44 import FastString
45 import Util             ( thenCmp )
46 \end{code}
47
48 A Cost Centre Stack is something that can be attached to a closure.
49 This is either:
50         
51         - the current cost centre stack (CCCS)
52         - a pre-defined cost centre stack (there are several
53           pre-defined CCSs, see below).
54
55 \begin{code}
56 data CostCentreStack
57   = NoCCS
58
59   | CurrentCCS          -- Pinned on a let(rec)-bound 
60                         -- thunk/function/constructor, this says that the 
61                         -- cost centre to be attached to the object, when it 
62                         -- is allocated, is whatever is in the 
63                         -- current-cost-centre-stack register.
64
65   | SubsumedCCS         -- Cost centre stack for top-level subsumed functions
66                         -- (CAFs get an AllCafsCC).
67                         -- Its execution costs get subsumed into the caller.
68                         -- This guy is *only* ever pinned on static closures,
69                         -- and is *never* the cost centre for an SCC construct.
70
71   | OverheadCCS         -- We charge costs due to the profiling-system
72                         -- doing its work to "overhead".
73                         --
74                         -- Objects whose CCS is "Overhead"
75                         -- have their *allocation* charged to "overhead",
76                         -- but have the current CCS put into the object
77                         -- itself.
78
79                         -- For example, if we transform "f g" to "let
80                         -- g' = g in f g'" (so that something about
81                         -- profiling works better...), then we charge
82                         -- the *allocation* of g' to OverheadCCS, but
83                         -- we put the cost-centre of the call to f
84                         -- (i.e., current CCS) into the g' object.  When
85                         -- g' is entered, the CCS of the call
86                         -- to f will be set.
87
88   | DontCareCCS         -- We need a CCS to stick in static closures
89                         -- (for data), but we *don't* expect them to
90                         -- accumulate any costs.  But we still need
91                         -- the placeholder.  This CCS is it.
92
93   | PushCC CostCentre CostCentreStack
94                 -- These are used during code generation as the CCSs
95                 -- attached to closures.  A PushCC never appears as
96                 -- the argument to an _scc_.
97                 --
98                 -- The tail (2nd argument) is either NoCCS, indicating
99                 -- a staticly allocated CCS, or CurrentCCS indicating
100                 -- a dynamically created CCS.  We only support
101                 -- statically allocated *singleton* CCSs at the
102                 -- moment, for the purposes of initialising the CCS
103                 -- field of a CAF.
104
105   deriving (Eq, Ord)    -- needed for Ord on CLabel
106 \end{code}
107
108 A Cost Centre is the argument of an _scc_ expression.
109  
110 \begin{code}
111 data CostCentre
112   = NoCostCentre        -- Having this constructor avoids having
113                         -- to use "Maybe CostCentre" all the time.
114
115   | NormalCC {  
116                 cc_name :: CcName,      -- Name of the cost centre itself
117                 cc_mod  :: Module,      -- Name of module defining this CC.
118                 cc_is_dupd :: IsDupdCC, -- see below
119                 cc_is_caf  :: IsCafCC   -- see below
120     }
121
122   | AllCafsCC { 
123                 cc_mod  :: Module       -- Name of module defining this CC.
124     }
125
126 type CcName = FastString
127
128 data IsDupdCC
129   = OriginalCC  -- This says how the CC is *used*.  Saying that
130   | DupdCC              -- it is DupdCC doesn't make it a different
131                         -- CC, just that it a sub-expression which has
132                         -- been moved ("dupd") into a different scope.
133                         --
134                         -- The point about a dupd SCC is that we don't
135                         -- count entries to it, because it's not the
136                         -- "original" one.
137                         --
138                         -- In the papers, it's called "SCCsub",
139                         --  i.e. SCCsub CC == SCC DupdCC,
140                         -- but we are trying to avoid confusion between
141                         -- "subd" and "subsumed".  So we call the former
142                         -- "dupd".
143
144 data IsCafCC = CafCC | NotCafCC
145
146 -- synonym for triple which describes the cost centre info in the generated
147 -- code for a module.
148 type CollectedCCs
149   = ( [CostCentre]       -- local cost-centres that need to be decl'd
150     , [CostCentre]       -- "extern" cost-centres
151     , [CostCentreStack]  -- pre-defined "singleton" cost centre stacks
152     )
153 \end{code}
154
155 WILL: Would there be any merit to recording ``I am now using a
156 cost-centre from another module''?  I don't know if this would help a
157 user; it might be interesting to us to know how much computation is
158 being moved across module boundaries.
159
160 SIMON: Maybe later...
161
162 \begin{code}
163 noCCS, subsumedCCS, currentCCS, overheadCCS, dontCareCCS :: CostCentreStack
164
165 noCCS                   = NoCCS
166 subsumedCCS             = SubsumedCCS
167 currentCCS              = CurrentCCS
168 overheadCCS             = OverheadCCS
169 dontCareCCS             = DontCareCCS
170
171 noCostCentre :: CostCentre
172 noCostCentre            = NoCostCentre
173 \end{code}
174
175 Predicates on Cost-Centre Stacks
176
177 \begin{code}
178 noCCSAttached :: CostCentreStack -> Bool
179 noCCSAttached NoCCS                     = True
180 noCCSAttached _                         = False
181
182 noCCAttached :: CostCentre -> Bool
183 noCCAttached NoCostCentre               = True
184 noCCAttached _                          = False
185
186 isCurrentCCS :: CostCentreStack -> Bool
187 isCurrentCCS CurrentCCS                 = True
188 isCurrentCCS _                          = False
189
190 isSubsumedCCS :: CostCentreStack -> Bool
191 isSubsumedCCS SubsumedCCS               = True
192 isSubsumedCCS _                         = False
193
194 isCafCCS :: CostCentreStack -> Bool
195 isCafCCS (PushCC cc NoCCS)              = isCafCC cc
196 isCafCCS _                              = False
197
198 isDerivedFromCurrentCCS :: CostCentreStack -> Bool
199 isDerivedFromCurrentCCS CurrentCCS      = True
200 isDerivedFromCurrentCCS (PushCC _ ccs)  = isDerivedFromCurrentCCS ccs
201 isDerivedFromCurrentCCS _               = False
202
203 currentOrSubsumedCCS :: CostCentreStack -> Bool
204 currentOrSubsumedCCS SubsumedCCS        = True
205 currentOrSubsumedCCS CurrentCCS         = True
206 currentOrSubsumedCCS _                  = False
207
208 maybeSingletonCCS :: CostCentreStack -> Maybe CostCentre
209 maybeSingletonCCS (PushCC cc NoCCS)     = Just cc
210 maybeSingletonCCS _                     = Nothing
211 \end{code}
212
213 Building cost centres
214
215 \begin{code}
216 mkUserCC :: FastString -> Module -> CostCentre
217 mkUserCC cc_name mod
218   = NormalCC { cc_name = cc_name, cc_mod =  mod,
219                cc_is_dupd = OriginalCC, cc_is_caf = NotCafCC {-might be changed-}
220     }
221
222 mkAutoCC :: Id -> Module -> IsCafCC -> CostCentre
223 mkAutoCC id mod is_caf
224   = NormalCC { cc_name = str, cc_mod =  mod,
225                cc_is_dupd = OriginalCC, cc_is_caf = is_caf
226     }
227   where 
228         name = getName id
229         -- beware: we might be making an auto CC for a compiler-generated
230         -- thing (like a CAF when -caf-all is on), so include the uniq.
231         -- See bug #249, tests prof001, prof002
232         str | isSystemName name = mkFastString (showSDoc (ppr name))
233             | otherwise         = occNameFS (getOccName id)
234
235 mkAllCafsCC :: Module -> CostCentre
236 mkAllCafsCC m = AllCafsCC  { cc_mod = m }
237
238
239
240 mkSingletonCCS :: CostCentre -> CostCentreStack
241 mkSingletonCCS cc = pushCCOnCCS cc NoCCS
242
243 pushCCOnCCS :: CostCentre -> CostCentreStack -> CostCentreStack
244 pushCCOnCCS = PushCC
245
246 dupifyCC :: CostCentre -> CostCentre
247 dupifyCC cc = cc {cc_is_dupd = DupdCC}
248
249 isCafCC, isDupdCC :: CostCentre -> Bool
250
251 isCafCC (AllCafsCC {})                   = True
252 isCafCC (NormalCC {cc_is_caf = CafCC}) = True
253 isCafCC _                                = False
254
255 isDupdCC (NormalCC   {cc_is_dupd = DupdCC}) = True
256 isDupdCC _                                   = False
257
258 isSccCountCostCentre :: CostCentre -> Bool
259   -- Is this a cost-centre which records scc counts
260
261 #if DEBUG
262 isSccCountCostCentre NoCostCentre  = panic "isSccCount:NoCostCentre"
263 #endif
264 isSccCountCostCentre cc | isCafCC cc  = False
265                         | isDupdCC cc = False
266                         | otherwise   = True
267
268 sccAbleCostCentre :: CostCentre -> Bool
269   -- Is this a cost-centre which can be sccd ?
270
271 #if DEBUG
272 sccAbleCostCentre NoCostCentre  = panic "sccAbleCC:NoCostCentre"
273 #endif
274 sccAbleCostCentre cc | isCafCC cc = False
275                      | otherwise  = True
276
277 ccFromThisModule :: CostCentre -> Module -> Bool
278 ccFromThisModule cc m = cc_mod cc == m
279 \end{code}
280
281 \begin{code}
282 instance Eq CostCentre where
283         c1 == c2 = case c1 `cmpCostCentre` c2 of { EQ -> True; _ -> False }
284
285 instance Ord CostCentre where
286         compare = cmpCostCentre
287
288 cmpCostCentre :: CostCentre -> CostCentre -> Ordering
289
290 cmpCostCentre (AllCafsCC  {cc_mod = m1}) (AllCafsCC  {cc_mod = m2}) = m1 `compare` m2
291
292 cmpCostCentre (NormalCC {cc_name = n1, cc_mod =  m1, cc_is_caf = c1}) 
293               (NormalCC {cc_name = n2, cc_mod =  m2, cc_is_caf = c2}) 
294     -- first key is module name, then we use "kinds" (which include
295     -- names) and finally the caf flag
296   = (m1 `compare` m2) `thenCmp` (n1 `compare` n2) `thenCmp` (c1 `cmp_caf` c2)
297
298 cmpCostCentre other_1 other_2
299   = let
300         tag1 = tag_CC other_1
301         tag2 = tag_CC other_2
302     in
303     if tag1 <# tag2 then LT else GT
304   where
305     tag_CC (NormalCC   {}) = _ILIT(1)
306     tag_CC (AllCafsCC  {}) = _ILIT(2)
307
308 -- TODO: swap order of IsCafCC, add deriving Ord
309 cmp_caf :: IsCafCC -> IsCafCC -> Ordering
310 cmp_caf NotCafCC CafCC     = LT
311 cmp_caf NotCafCC NotCafCC  = EQ
312 cmp_caf CafCC    CafCC     = EQ
313 cmp_caf CafCC    NotCafCC  = GT
314
315 decomposeCCS :: CostCentreStack -> ([CostCentre],CostCentreStack)
316 decomposeCCS (PushCC cc ccs) = (cc:more, ccs') 
317   where (more,ccs') = decomposeCCS ccs
318 decomposeCCS ccs = ([],ccs)
319 \end{code}
320
321 -----------------------------------------------------------------------------
322 Printing Cost Centre Stacks.
323
324 The outputable instance for CostCentreStack prints the CCS as a C
325 expression.
326
327 NOTE: Not all cost centres are suitable for using in a static
328 initializer.  In particular, the PushCC forms where the tail is CCCS
329 may only be used in inline C code because they expand to a
330 non-constant C expression.
331
332 \begin{code}
333 instance Outputable CostCentreStack where
334   ppr NoCCS             = ptext (sLit "NO_CCS")
335   ppr CurrentCCS        = ptext (sLit "CCCS")
336   ppr OverheadCCS       = ptext (sLit "CCS_OVERHEAD")
337   ppr DontCareCCS       = ptext (sLit "CCS_DONT_CARE")
338   ppr SubsumedCCS       = ptext (sLit "CCS_SUBSUMED")
339   ppr (PushCC cc NoCCS) = ppr cc <> ptext (sLit "_ccs")
340   ppr (PushCC cc ccs)   = ptext (sLit "PushCostCentre") <> 
341                            parens (ppr ccs <> comma <> 
342                            parens(ptext (sLit "void *")) <> ppr cc)
343 \end{code}
344
345 -----------------------------------------------------------------------------
346 Printing Cost Centres.
347
348 There are several different ways in which we might want to print a
349 cost centre:
350
351         - the name of the cost centre, for profiling output (a C string)
352         - the label, i.e. C label for cost centre in .hc file.
353         - the debugging name, for output in -ddump things
354         - the interface name, for printing in _scc_ exprs in iface files.
355
356 The last 3 are derived from costCentreStr below.  The first is given
357 by costCentreName.
358
359 \begin{code}
360 instance Outputable CostCentre where
361   ppr cc = getPprStyle $ \ sty ->
362            if codeStyle sty
363            then ppCostCentreLbl cc
364            else text (costCentreUserName cc)
365
366 -- Printing in an interface file or in Core generally
367 pprCostCentreCore :: CostCentre -> SDoc
368 pprCostCentreCore (AllCafsCC {cc_mod = m})
369   = text "__sccC" <+> braces (ppr m)
370 pprCostCentreCore (NormalCC {cc_name = n, cc_mod = m,
371                              cc_is_caf = caf, cc_is_dupd = dup})
372   = text "__scc" <+> braces (hsep [
373         ftext (zEncodeFS n),
374         ppr m,
375         pp_dup dup,
376         pp_caf caf
377     ])
378
379 pp_dup :: IsDupdCC -> SDoc
380 pp_dup DupdCC = char '!'
381 pp_dup _      = empty
382
383 pp_caf :: IsCafCC -> SDoc
384 pp_caf CafCC = text "__C"
385 pp_caf _     = empty
386
387 -- Printing as a C label
388 ppCostCentreLbl :: CostCentre -> SDoc
389 ppCostCentreLbl (NoCostCentre)            = text "NONE_cc"
390 ppCostCentreLbl (AllCafsCC  {cc_mod = m}) = ppr m <> text "_CAFs_cc"
391 ppCostCentreLbl (NormalCC {cc_name = n, cc_mod = m, cc_is_caf = is_caf}) 
392   = ppr m <> char '_' <> ftext (zEncodeFS n) <> 
393         text (case is_caf of { CafCC -> "_CAF"; _ -> "" }) <> text "_cc"
394
395 -- This is the name to go in the user-displayed string, 
396 -- recorded in the cost centre declaration
397 costCentreUserName :: CostCentre -> String
398 costCentreUserName (NoCostCentre)  = "NO_CC"
399 costCentreUserName (AllCafsCC {})  = "CAF"
400 costCentreUserName (NormalCC {cc_name = name, cc_is_caf = is_caf})
401   =  case is_caf of { CafCC -> "CAF:";   _ -> "" } ++ unpackFS name
402 \end{code}