[project @ 1996-04-08 16:15:43 by partain]
[ghc-hetmet.git] / ghc / compiler / profiling / CostCentre.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
3 %
4 \section[CostCentre]{The @CostCentre@ data type}
5
6 \begin{code}
7 #include "HsVersions.h"
8
9 module CostCentre (
10         CostCentre, CcKind, IsDupdCC{-ToDo:rm-}, IsCafCC(..),
11         noCostCentre, subsumedCosts,
12         useCurrentCostCentre,
13         noCostCentreAttached, costsAreSubsumed,
14         currentOrSubsumedCosts,
15         preludeCafsCostCentre, preludeDictsCostCentre,
16         overheadCostCentre, dontCareCostCentre,
17
18         mkUserCC, mkAutoCC, mkDictCC, mkAllCafsCC, mkAllDictsCC,
19         cafifyCC, unCafifyCC, dupifyCC,
20         isCafCC, isDictCC, isDupdCC,
21         setToAbleCostCentre,
22         ccFromThisModule,
23         ccMentionsId,
24
25         uppCostCentre, uppCostCentreDecl, showCostCentre, -- printing
26
27         cmpCostCentre   -- used for removing dups in a list
28     ) where
29
30 import Ubiq{-uitous-}
31
32 import Id               ( externallyVisibleId, GenId, Id(..) )
33 import CStrings         ( identToC, stringToC )
34 import Maybes           ( Maybe(..) )
35 import Name             ( showRdr, getOccName, RdrName )
36 import Pretty           ( ppShow, prettyToUn )
37 import PprStyle         ( PprStyle(..) )
38 import UniqSet
39 import Unpretty
40 import Util
41
42 showId = panic "Whoops"
43 pprIdInUnfolding = panic "Whoops"
44 \end{code}
45
46 \begin{code}
47 data CostCentre
48   = NoCostCentre        -- Having this constructor avoids having
49                         -- to use "Maybe CostCentre" all the time.
50
51   | NormalCC    CcKind   -- CcKind will include a cost-centre name
52                 FAST_STRING      -- Name of module defining this CC.
53                 FAST_STRING   -- "Group" that this CC is in.
54                 IsDupdCC -- see below
55                 IsCafCC  -- see below
56
57   | CurrentCC           -- Pinned on a let(rec)-bound thunk/function/constructor,
58                         -- this says that the cost centre to be attached to
59                         -- the object, when it is allocated, is whatever is in the
60                         -- current-cost-centre register.
61                         -- This guy is *never* the cost centre for an SCC construct,
62                         -- and is only used for *local* (non-top-level) definitions.
63
64   | SubsumedCosts       -- Cost centre for top-level subsumed functions
65                         -- (CAFs get an AllCafsCC).
66                         -- Its execution costs get subsumed into the caller.
67                         -- This guy is *only* ever pinned on static closures,
68                         -- and is *never* the cost centre for an SCC construct.
69
70   | AllCafsCC   FAST_STRING     -- Ditto for CAFs.
71                 FAST_STRING  -- We record module and group names.
72                         -- Again, one "big" CAF cc per module, where all
73                         -- CAF costs are attributed unless the user asked for
74                         -- per-individual-CAF cost attribution.
75
76   | AllDictsCC  FAST_STRING     -- Ditto for dictionaries.
77                 FAST_STRING  -- We record module and group names.
78                         -- Again, one "big" DICT cc per module, where all
79                         -- DICT costs are attributed unless the user asked for
80                         -- per-individual-DICT cost attribution.
81                 IsDupdCC -- see below
82
83   | OverheadCC          -- We charge costs due to the profiling-system
84                         -- doing its work to "overhead".
85                         --
86                         -- Objects whose cost-centre is "Overhead"
87                         -- have their *allocation* charged to "overhead",
88                         -- but have the current CC put into the object
89                         -- itself.
90                         --
91                         -- For example, if we transform "f g" to "let
92                         -- g' = g in f g'" (so that something about
93                         -- profiling works better...), then we charge
94                         -- the *allocation* of g' to OverheadCC, but
95                         -- we put the cost-centre of the call to f
96                         -- (i.e., current CC) into the g' object.  When
97                         -- g' is entered, the cost-centre of the call
98                         -- to f will be set.
99
100   | PreludeCafsCC       -- In compiling the prelude, we do sometimes
101   | PreludeDictsCC      -- need a CC to blame; i.e., when there's a CAF,
102                         -- or other costs that really shouldn't be
103                         -- subsumed/blamed-on-the-caller.  These costs
104                         -- should be *small*.  We treat PreludeCafsCC
105                         -- as if it were shorthand for
106                         -- (AllCafsCC <PreludeSomething> _).  Analogously
107                         -- for PreludeDictsCC...
108         IsDupdCC        -- see below/above
109
110   | DontCareCC          -- We need a cost-centre to stick in static closures
111                         -- (for data), but we *don't* expect them to
112                         -- accumulate any costs.  But we still need
113                         -- the placeholder.  This CC is it.
114
115 data CcKind
116   = UserCC  FAST_STRING -- Supplied by user: String is the cc name
117   | AutoCC  Id          -- CC -auto-magically inserted for that Id
118   | DictCC  Id
119
120 data IsDupdCC
121   = AnOriginalCC        -- This says how the CC is *used*.  Saying that
122   | ADupdCC             -- it is ADupdCC doesn't make it a different
123                         -- CC, just that it a sub-expression which has
124                         -- been moved ("dupd") into a different scope.
125                         -- In the papers, it's called "SCCsub",
126                         --  i.e. SCCsub CC == SCC ADupdCC,
127                         -- but we are trying to avoid confusion between
128                         -- "subd" and "subsumed".  So we call the former
129                         -- "dupd".
130
131 data IsCafCC
132   = IsCafCC
133   | IsNotCafCC
134 \end{code}
135
136 WILL: Would there be any merit to recording ``I am now using a
137 cost-centre from another module''?  I don't know if this would help a
138 user; it might be interesting to us to know how much computation is
139 being moved across module boundaries.
140
141 SIMON: Maybe later...
142
143 \begin{code}
144 noCostCentre  = NoCostCentre
145 subsumedCosts = SubsumedCosts
146 useCurrentCostCentre = CurrentCC
147 overheadCostCentre = OverheadCC
148 preludeCafsCostCentre = PreludeCafsCC
149 dontCareCostCentre = DontCareCC
150 preludeDictsCostCentre is_dupd
151   = PreludeDictsCC (if is_dupd then ADupdCC else AnOriginalCC)
152
153 noCostCentreAttached NoCostCentre  = True
154 noCostCentreAttached _             = False
155
156 costsAreSubsumed SubsumedCosts  = True
157 costsAreSubsumed _              = False
158
159 currentOrSubsumedCosts SubsumedCosts    = True
160 currentOrSubsumedCosts CurrentCC        = True
161 currentOrSubsumedCosts _                = False
162
163 mkUserCC :: FAST_STRING -> FAST_STRING -> FAST_STRING -> CostCentre
164
165 mkUserCC cc_name module_name group_name
166   = NormalCC (UserCC cc_name) module_name group_name
167              AnOriginalCC IsNotCafCC{-might be changed-}
168
169 mkDictCC, mkAutoCC :: Id -> FAST_STRING -> FAST_STRING -> IsCafCC -> CostCentre
170
171 mkDictCC id module_name group_name is_caf
172   = NormalCC (DictCC id) module_name group_name
173              AnOriginalCC is_caf
174
175 mkAutoCC id module_name group_name is_caf
176   = NormalCC (AutoCC id) module_name group_name
177              AnOriginalCC is_caf
178
179 mkAllCafsCC  m g   = AllCafsCC  m g
180 mkAllDictsCC m g is_dupd
181   = AllDictsCC m g (if is_dupd then ADupdCC else AnOriginalCC)
182
183 cafifyCC, unCafifyCC, dupifyCC  :: CostCentre -> CostCentre
184
185 cafifyCC cc@(AllDictsCC _ _ _) = cc -- ???????? ToDo
186 cafifyCC cc@(PreludeDictsCC _) = cc -- ditto
187 cafifyCC (NormalCC kind m g is_dupd is_caf)
188   = ASSERT(not_a_calf_already is_caf)
189     NormalCC kind m g is_dupd IsCafCC
190   where
191     not_a_calf_already IsCafCC = False
192     not_a_calf_already _       = True
193 cafifyCC cc = panic ("cafifyCC"++(showCostCentre PprDebug False cc))
194
195 -- WDP 95/07: pretty dodgy
196 unCafifyCC (NormalCC kind m g is_dupd IsCafCC) = NormalCC kind m g is_dupd IsNotCafCC
197 unCafifyCC (AllCafsCC _ _)      = CurrentCC
198 unCafifyCC PreludeCafsCC        = CurrentCC
199 unCafifyCC (AllDictsCC _ _ _)   = CurrentCC
200 unCafifyCC (PreludeDictsCC _)   = CurrentCC
201 unCafifyCC other_cc             = other_cc
202
203 dupifyCC (AllDictsCC m g _) = AllDictsCC m g ADupdCC
204 dupifyCC (PreludeDictsCC _) = PreludeDictsCC ADupdCC
205 dupifyCC (NormalCC kind m g is_dupd is_caf)
206   = NormalCC kind m g ADupdCC is_caf
207 dupifyCC cc = panic ("dupifyCC"++(showCostCentre PprDebug False cc))
208
209 isCafCC, isDictCC, isDupdCC :: CostCentre -> Bool
210
211 isCafCC (AllCafsCC _ _)            = True
212 isCafCC PreludeCafsCC              = True
213 isCafCC (NormalCC _ _ _ _ IsCafCC) = True
214 isCafCC _                          = False
215
216 isDictCC (AllDictsCC _ _ _)             = True
217 isDictCC (PreludeDictsCC _)             = True
218 isDictCC (NormalCC (DictCC _) _ _ _ _)  = True
219 isDictCC _                              = False
220
221 isDupdCC (AllDictsCC _ _ ADupdCC)   = True
222 isDupdCC (PreludeDictsCC ADupdCC)   = True
223 isDupdCC (NormalCC _ _ _ ADupdCC _) = True
224 isDupdCC _                          = False
225
226 setToAbleCostCentre :: CostCentre -> Bool
227   -- Is this a cost-centre to which CCC might reasonably
228   -- be set?  setToAbleCostCentre is allowed to panic on
229   -- "nonsense" cases, too...
230
231 #if DEBUG
232 setToAbleCostCentre NoCostCentre    = panic "setToAbleCC:NoCostCentre"
233 setToAbleCostCentre SubsumedCosts   = panic "setToAbleCC:SubsumedCosts"
234 setToAbleCostCentre CurrentCC       = panic "setToAbleCC:CurrentCC"
235 setToAbleCostCentre DontCareCC      = panic "setToAbleCC:DontCareCC"
236 #endif
237
238 setToAbleCostCentre OverheadCC      = False -- see comments in type defn
239 setToAbleCostCentre other           = not (isCafCC other || isDictCC other)
240
241 ccFromThisModule :: CostCentre -> FAST_STRING{-module name-} -> Bool
242
243 ccFromThisModule (NormalCC _ m _ _ _) mod_name = m == mod_name
244 ccFromThisModule (AllCafsCC  m _)     mod_name = m == mod_name
245 ccFromThisModule (AllDictsCC m _ _)   mod_name = m == mod_name
246 ccFromThisModule PreludeCafsCC        _        = False
247 ccFromThisModule (PreludeDictsCC _)   _        = False
248 ccFromThisModule OverheadCC           _        = False
249 ccFromThisModule DontCareCC           _        = False
250   -- shouldn't ask about any others!
251 \end{code}
252
253 \begin{code}
254 ccMentionsId :: CostCentre -> Maybe Id
255
256 ccMentionsId (NormalCC (AutoCC id) _ _ _ _) = Just id
257 ccMentionsId (NormalCC (DictCC id) _ _ _ _) = Just id
258 ccMentionsId other                          = Nothing
259 \end{code}
260
261 \begin{code}
262 cmpCostCentre :: CostCentre -> CostCentre -> TAG_
263
264 cmpCostCentre (AllCafsCC  m1 _)   (AllCafsCC  m2 _)   = _CMP_STRING_ m1 m2
265 cmpCostCentre (AllDictsCC m1 _ _) (AllDictsCC m2 _ _) = _CMP_STRING_ m1 m2
266 cmpCostCentre PreludeCafsCC       PreludeCafsCC       = EQ_
267 cmpCostCentre (PreludeDictsCC _)  (PreludeDictsCC _)  = EQ_
268 cmpCostCentre OverheadCC          OverheadCC          = EQ_
269 cmpCostCentre DontCareCC          DontCareCC          = EQ_
270
271 cmpCostCentre (NormalCC k1 m1 _ _ c1) (NormalCC k2 m2 _ _ c2)
272     -- first key is module name, then we use "kinds" (which include
273     -- names)
274   = case (_CMP_STRING_ m1 m2) of
275       LT_  -> LT_
276       EQ_  -> cmp_kind k1 k2
277       GT__ -> GT_
278
279 cmpCostCentre other_1 other_2
280   = let
281         tag1 = tag_CC other_1
282         tag2 = tag_CC other_2
283     in
284     if tag1 _LT_ tag2 then LT_ else GT_
285   where
286     tag_CC (NormalCC _ _ _ _ _) = (ILIT(1) :: FAST_INT)
287     tag_CC (AllCafsCC  _ _)     = ILIT(2)
288     tag_CC (AllDictsCC _ _ _)   = ILIT(3)
289     tag_CC PreludeCafsCC        = ILIT(4)
290     tag_CC (PreludeDictsCC _)   = ILIT(5)
291     tag_CC OverheadCC           = ILIT(6)
292     tag_CC DontCareCC           = ILIT(7)
293
294     -- some BUG avoidance here...
295     tag_CC NoCostCentre  = panic# "tag_CC:NoCostCentre"
296     tag_CC SubsumedCosts = panic# "tag_CC:SubsumedCosts"
297     tag_CC CurrentCC     = panic# "tag_CC:SubsumedCosts"
298
299
300 cmp_kind (UserCC n1) (UserCC n2) = _CMP_STRING_ n1 n2
301 cmp_kind (AutoCC i1) (AutoCC i2) = cmp i1 i2
302 cmp_kind (DictCC i1) (DictCC i2) = cmp i1 i2
303 cmp_kind other_1     other_2
304   = let
305         tag1 = tag_CcKind other_1
306         tag2 = tag_CcKind other_2
307     in
308     if tag1 _LT_ tag2 then LT_ else GT_
309   where
310     tag_CcKind (UserCC _) = (ILIT(1) :: FAST_INT)
311     tag_CcKind (AutoCC _) = ILIT(2)
312     tag_CcKind (DictCC _) = ILIT(3)
313 \end{code}
314
315 \begin{code}
316 showCostCentre    :: PprStyle -> Bool -> CostCentre -> String
317 uppCostCentre     :: PprStyle -> Bool -> CostCentre -> Unpretty
318 uppCostCentreDecl :: PprStyle -> Bool -> CostCentre -> Unpretty
319
320 showCostCentre PprUnfolding print_as_string cc
321   = ASSERT(not print_as_string) -- we never "print as string w/ Unfolding"
322     ASSERT(not (noCostCentreAttached cc))
323     ASSERT(not (currentOrSubsumedCosts cc))
324     uppShow 80 (upp_cc_uf cc)
325
326 showCostCentre sty print_as_string cc
327   = uppShow 80 (uppCostCentre sty print_as_string cc)
328
329 uppCostCentre sty print_as_string NoCostCentre
330   | friendly_style sty  = uppNil
331   | print_as_string     = uppStr "\"NO_CC\""
332   | otherwise           = uppPStr SLIT("NO_CC")
333
334 uppCostCentre sty print_as_string SubsumedCosts
335   | print_as_string     = uppStr "\"SUBSUMED\""
336   | otherwise           = uppPStr SLIT("CC_SUBSUMED")
337
338 uppCostCentre sty print_as_string CurrentCC
339   | print_as_string     = uppStr "\"CURRENT_CC\""
340   | otherwise           = uppPStr SLIT("CCC")
341
342 uppCostCentre sty print_as_string OverheadCC
343   | print_as_string     = uppStr "\"OVERHEAD\""
344   | otherwise           = uppPStr SLIT("CC_OVERHEAD")
345
346 uppCostCentre sty print_as_string cc
347   = let
348         prefix_CC = uppPStr SLIT("CC_")
349
350         basic_thing -- (basic_thing, suffix_CAF)
351           = do_cc cc
352
353         basic_thing_string
354           = if friendly_sty then basic_thing else stringToC basic_thing
355     in
356     if print_as_string then
357         uppBesides [uppChar '"', uppStr basic_thing_string, uppChar '"']
358
359     else if friendly_sty then
360         uppStr basic_thing
361     else
362         uppBesides [prefix_CC,
363                     prettyToUn (identToC (_PK_ basic_thing))]
364   where
365     friendly_sty = friendly_style sty
366
367     add_module_name_maybe m str
368       = if print_as_string then str else (str ++ ('.' : m))
369
370     ----------------
371     do_cc OverheadCC         = "OVERHEAD"
372     do_cc DontCareCC         = "DONT_CARE"
373     do_cc (AllCafsCC  m _)   = if print_as_string
374                                then "CAFs_in_..."
375                                else "CAFs." ++ _UNPK_ m
376     do_cc (AllDictsCC m _ d) = do_dupd d (
377                                if print_as_string
378                                then "DICTs_in_..."
379                                else "DICTs." ++ _UNPK_ m)
380     do_cc PreludeCafsCC      = if print_as_string
381                                then "CAFs_in_..."
382                                else "CAFs"
383     do_cc (PreludeDictsCC d) = do_dupd d (
384                                if print_as_string
385                                then "DICTs_in_..."
386                                else "DICTs")
387
388     do_cc (NormalCC kind mod_name grp_name is_dupd is_caf)
389       = let
390             basic_kind = do_kind kind
391             is_a_calf  = do_calved is_caf
392         in
393         if friendly_sty then
394             do_dupd is_dupd (basic_kind ++ ('/': _UNPK_ mod_name) ++ ('/': _UNPK_ grp_name) ++ is_a_calf)
395         else
396             basic_kind
397       where
398         do_kind (UserCC name) = _UNPK_ name
399         do_kind (AutoCC id)   = do_id id ++ (if friendly_sty then "/AUTO" else "")
400         do_kind (DictCC id)   = do_id id ++ (if friendly_sty then "/DICT" else "")
401
402         do_id :: Id -> String
403         do_id id
404           = if print_as_string
405             then showRdr sty (getOccName id)    -- use occ name
406             else showId sty id                  -- we really do
407
408         do_calved IsCafCC = "/CAF"
409         do_calved _       = ""
410
411     ---------------
412     do_dupd ADupdCC str = if friendly_sty then str ++ "/DUPD" else str
413     do_dupd _       str = str
414
415 friendly_style sty -- i.e., probably for human consumption
416   = case sty of
417       PprForUser -> True
418       PprDebug   -> True
419       PprShowAll -> True
420       _          -> False
421 \end{code}
422
423 Printing unfoldings is sufficiently weird that we do it separately.
424 This should only apply to CostCentres that can be ``set to'' (cf
425 @setToAbleCostCentre@).  That excludes CAFs and
426 `overhead'---which are added at the very end---but includes dictionaries.
427 Dict \tr{_scc_}s may cross module boundaries to show ``scope'' info;
428 even if we won't ultimately do a \tr{SET_CCC} from it.
429 \begin{code}
430 upp_cc_uf (PreludeDictsCC d)
431   = uppCat [uppPStr SLIT("_PRELUDE_DICTS_CC_"), upp_dupd d]
432 upp_cc_uf (AllDictsCC m g d)
433   = uppCat [uppPStr SLIT("_ALL_DICTS_CC_"), uppStr (show (_UNPK_ m)), uppStr (show (_UNPK_ g)), upp_dupd d]
434
435 upp_cc_uf cc@(NormalCC cc_kind m g is_dupd is_caf)
436   = ASSERT(isDictCC cc || setToAbleCostCentre cc)
437     uppCat [pp_kind cc_kind, uppStr (show (_UNPK_ m)), uppStr (show (_UNPK_ g)),
438             upp_dupd is_dupd, pp_caf is_caf]
439   where
440     pp_kind (UserCC name) = uppBeside (uppPStr SLIT("_USER_CC_ ")) (uppStr (show (_UNPK_ name)))
441     pp_kind (AutoCC id)   = uppBeside (uppPStr SLIT("_AUTO_CC_ ")) (show_id id)
442     pp_kind (DictCC id)   = uppBeside (uppPStr SLIT("_DICT_CC_ ")) (show_id id)
443
444     show_id id = prettyToUn (pprIdInUnfolding no_in_scopes id)
445         where
446           no_in_scopes = emptyUniqSet
447
448     pp_caf IsCafCC    = uppPStr SLIT("_CAF_CC_")
449     pp_caf IsNotCafCC = uppPStr SLIT("_N_")
450
451 #ifdef DEBUG
452 upp_cc_uf other = panic ("upp_cc_uf:"++(showCostCentre PprDebug True other))
453 #endif
454
455 upp_dupd AnOriginalCC = uppPStr SLIT("_N_")
456 upp_dupd ADupdCC      = uppPStr SLIT("_DUPD_CC_")
457 \end{code}
458
459 \begin{code}
460 uppCostCentreDecl sty is_local cc
461 #ifdef DEBUG
462   | noCostCentreAttached cc || currentOrSubsumedCosts cc
463   = panic "uppCostCentreDecl: no cost centre!"
464   | otherwise
465 #endif
466   = if is_local then
467         uppBesides [
468             uppStr "CC_DECLARE(",
469             upp_ident, uppComma,
470             uppCostCentre sty True {-as String!-} cc, uppComma,
471             pp_str mod_name, uppComma,
472             pp_str grp_name, uppComma,
473             uppStr is_subsumed, uppComma,
474             if externally_visible then uppNil else uppPStr SLIT("static"),
475             uppStr ");"]
476     else
477         uppBesides [ uppStr "CC_EXTERN(", upp_ident, uppStr ");" ]
478   where
479     upp_ident = uppCostCentre sty False{-as identifier!-} cc
480
481     pp_str s  = uppBeside (uppPStr (_CONS_ '"'  s))  (uppChar '"')
482     pp_char c = uppBeside (uppPStr (_CONS_ '\'' c)) (uppChar '\'')
483
484     (mod_name, grp_name, is_subsumed, externally_visible)
485       = case cc of
486           AllCafsCC m g -> (m, g, cc_IS_CAF, True)
487
488           AllDictsCC m g _ -> (m, g, cc_IS_DICT, True)
489
490           NormalCC (DictCC i) m g is_dupd is_caf
491             -> (m, g, cc_IS_DICT, externallyVisibleId i)
492
493           NormalCC x m g is_dupd is_caf
494             -> (m, g, do_caf is_caf,
495                 case x of { UserCC _ -> True; AutoCC i -> externallyVisibleId i})
496       where
497         cc_IS_CAF      = "CC_IS_CAF"
498         cc_IS_DICT     = "CC_IS_DICT"
499         cc_IS_SUBSUMED = "CC_IS_SUBSUMED"
500         cc_IS_BORING   = "CC_IS_BORING"
501
502         do_caf IsCafCC       = cc_IS_CAF
503         do_caf IsNotCafCC    = cc_IS_BORING
504 \end{code}