1 -----------------------------------------------------------------------------
3 -- Code generation for profiling
5 -- (c) The University of Glasgow 2004-2006
7 -----------------------------------------------------------------------------
10 mkCCostCentre, mkCCostCentreStack,
12 -- Cost-centre Profiling
13 dynProfHdr, profDynAlloc, profAlloc, staticProfHdr, initUpdFrameProf,
14 enterCostCentre, enterCostCentrePAP, enterCostCentreThunk,
18 emitCostCentreDecl, emitCostCentreStackDecl,
19 emitRegisterCC, emitRegisterCCS,
22 -- Lag/drag/void stuff
23 ldvEnter, ldvRecordCreate
26 #include "HsVersions.h"
28 -- For WORD_SIZE_IN_BITS only.
29 #include "../includes/Constants.h"
30 -- For LDV_CREATE_MASK, LDV_STATE_USE
32 #include "../includes/DerivedConstants.h"
33 -- For REP_xxx constants, which are MachReps
46 import qualified Module
51 import Constants -- Lots of field offsets
58 -----------------------------------------------------------------------------
60 -- Cost-centre-stack Profiling
62 -----------------------------------------------------------------------------
64 -- Expression representing the current cost centre stack
66 curCCS = CmmLoad curCCSAddr wordRep
68 -- Address of current CCS variable, for storing into
70 curCCSAddr = CmmLit (CmmLabel (mkRtsDataLabel SLIT("CCCS")))
72 mkCCostCentre :: CostCentre -> CmmLit
73 mkCCostCentre cc = CmmLabel (mkCCLabel cc)
75 mkCCostCentreStack :: CostCentreStack -> CmmLit
76 mkCCostCentreStack ccs = CmmLabel (mkCCSLabel ccs)
78 costCentreFrom :: CmmExpr -- A closure pointer
79 -> CmmExpr -- The cost centre from that closure
80 costCentreFrom cl = CmmLoad (cmmOffsetB cl oFFSET_StgHeader_ccs) wordRep
82 staticProfHdr :: CostCentreStack -> [CmmLit]
83 -- The profiling header words in a static closure
84 -- Was SET_STATIC_PROF_HDR
85 staticProfHdr ccs = ifProfilingL [mkCCostCentreStack ccs,
88 dynProfHdr :: CmmExpr -> [CmmExpr]
89 -- Profiling header words in a dynamic closure
90 dynProfHdr ccs = ifProfilingL [ccs, dynLdvInit]
92 initUpdFrameProf :: CmmExpr -> Code
93 -- Initialise the profiling field of an update frame
94 initUpdFrameProf frame_amode
95 = ifProfiling $ -- frame->header.prof.ccs = CCCS
96 stmtC (CmmStore (cmmOffsetB frame_amode oFFSET_StgHeader_ccs) curCCS)
97 -- frame->header.prof.hp.rs = NULL (or frame-header.prof.hp.ldvw = 0)
98 -- is unnecessary because it is not used anyhow.
100 -- -----------------------------------------------------------------------------
101 -- Recording allocation in a cost centre
103 -- | Record the allocation of a closure. The CmmExpr is the cost
104 -- centre stack to which to attribute the allocation.
105 profDynAlloc :: ClosureInfo -> CmmExpr -> Code
106 profDynAlloc cl_info ccs
108 profAlloc (CmmLit (mkIntCLit (closureSize cl_info))) ccs
110 -- | Record the allocation of a closure (size is given by a CmmExpr)
111 -- The size must be in words, because the allocation counter in a CCS counts
113 profAlloc :: CmmExpr -> CmmExpr -> Code
116 stmtC (addToMemE alloc_rep
117 (cmmOffsetB ccs oFFSET_CostCentreStack_mem_alloc)
118 (CmmMachOp (MO_U_Conv wordRep alloc_rep) $
119 [CmmMachOp mo_wordSub [words,
120 CmmLit (mkIntCLit profHdrSize)]]))
121 -- subtract the "profiling overhead", which is the
122 -- profiling header in a closure.
124 alloc_rep = REP_CostCentreStack_mem_alloc
126 -- ----------------------------------------------------------------------
127 -- Setting the cost centre in a new closure
129 chooseDynCostCentres :: CostCentreStack
132 -> FCode (CmmExpr, CmmExpr)
133 -- Called when alllcating a closure
134 -- Tells which cost centre to put in the object, and which
135 -- to blame the cost of allocation on
136 chooseDynCostCentres ccs args body = do
137 -- Cost-centre we record in the object
138 use_ccs <- emitCCS ccs
140 -- Cost-centre on whom we blame the allocation
142 | null args && isBox body = CmmLit (mkCCostCentreStack overheadCCS)
143 | otherwise = use_ccs
145 return (use_ccs, blame_ccs)
148 -- Some CostCentreStacks are a sequence of pushes on top of CCCS.
149 -- These pushes must be performed before we can refer to the stack in
151 emitCCS :: CostCentreStack -> FCode CmmExpr
152 emitCCS ccs = push_em (ccsExpr ccs') (reverse cc's)
154 (cc's, ccs') = decomposeCCS ccs
156 push_em ccs [] = return ccs
157 push_em ccs (cc:rest) = do
158 tmp <- newTemp wordRep
159 pushCostCentre tmp ccs cc
160 push_em (CmmReg tmp) rest
162 ccsExpr :: CostCentreStack -> CmmExpr
164 | isCurrentCCS ccs = curCCS
165 | otherwise = CmmLit (mkCCostCentreStack ccs)
168 isBox :: StgExpr -> Bool
169 -- If it's an utterly trivial RHS, then it must be
170 -- one introduced by boxHigherOrderArgs for profiling,
171 -- so we charge it to "OVERHEAD".
172 -- This looks like a GROSS HACK to me --SDM
173 isBox (StgApp fun []) = True
177 -- -----------------------------------------------------------------------
178 -- Setting the current cost centre on entry to a closure
180 -- For lexically scoped profiling we have to load the cost centre from
181 -- the closure entered, if the costs are not supposed to be inherited.
182 -- This is done immediately on entering the fast entry point.
184 -- Load current cost centre from closure, if not inherited.
185 -- Node is guaranteed to point to it, if profiling and not inherited.
190 -> StgExpr -- The RHS of the closure
193 -- We used to have a special case for bindings of form
195 -- where g has arity 2. The RHS is a thunk, but we don't
196 -- need to update it; and we want to subsume costs.
197 -- We don't have these sort of PAPs any more, so the special
198 -- case has gone away.
200 enterCostCentre closure_info ccs body
202 ASSERT2(not (noCCSAttached ccs), ppr (closureName closure_info) <+> ppr ccs)
203 enter_cost_centre closure_info ccs body
205 enter_cost_centre closure_info ccs body
207 = ASSERT(isToplevClosure closure_info)
211 | isDerivedFromCurrentCCS ccs
213 if re_entrant && not is_box
215 enter_ccs_fun node_ccs
217 stmtC (CmmStore curCCSAddr node_ccs)
219 -- don't forget to bump the scc count. This closure might have been
220 -- of the form let x = _scc_ "x" e in ...x..., which the SCCfinal
221 -- pass has turned into simply let x = e in ...x... and attached
222 -- the _scc_ as PushCostCentre(x,CCCS) on the x closure. So that
223 -- we don't lose the scc counter, bump it in the entry code for x.
224 -- ToDo: for a multi-push we should really bump the counter for
225 -- each of the intervening CCSs, not just the top one.
226 ; when (not (isCurrentCCS ccs)) $
227 stmtC (bumpSccCount curCCS)
231 = ASSERT(isToplevClosure closure_info)
232 ASSERT(not re_entrant)
233 do { -- This is just a special case of the isDerivedFromCurrentCCS
234 -- case above. We could delete this, but it's a micro
235 -- optimisation and saves a bit of code.
236 stmtC (CmmStore curCCSAddr enc_ccs)
237 ; stmtC (bumpSccCount node_ccs)
241 = panic "enterCostCentre"
243 enc_ccs = CmmLit (mkCCostCentreStack ccs)
244 re_entrant = closureReEntrant closure_info
245 node_ccs = costCentreFrom (CmmReg nodeReg)
248 -- set the current CCS when entering a PAP
249 enterCostCentrePAP :: CmmExpr -> Code
250 enterCostCentrePAP closure =
252 enter_ccs_fun (costCentreFrom closure)
255 enterCostCentreThunk :: CmmExpr -> Code
256 enterCostCentreThunk closure =
258 stmtC $ CmmStore curCCSAddr (costCentreFrom closure)
260 enter_ccs_fun stack = emitRtsCall SLIT("EnterFunCCS") [(stack,PtrHint)]
263 enter_ccs_fsub = enteringPAP 0
265 -- When entering a PAP, EnterFunCCS is called by both the PAP entry
266 -- code and the function entry code; we don't want the function's
267 -- entry code to also update CCCS in the event that it was called via
268 -- a PAP, so we set the flag entering_PAP to indicate that we are
269 -- entering via a PAP.
270 enteringPAP :: Integer -> Code
272 = stmtC (CmmStore (CmmLit (CmmLabel (mkRtsDataLabel SLIT("entering_PAP"))))
273 (CmmLit (CmmInt n cIntRep)))
275 ifProfiling :: Code -> Code
277 | opt_SccProfilingOn = code
280 ifProfilingL :: [a] -> [a]
282 | opt_SccProfilingOn = xs
286 -- ---------------------------------------------------------------------------
287 -- Initialising Cost Centres & CCSs
292 emitCostCentreDecl cc = do
293 { label <- mkStringCLit (costCentreUserName cc)
294 ; modl <- mkStringCLit (Module.moduleNameString
295 (Module.moduleName (cc_mod cc)))
296 -- All cost centres will be in the main package, since we
297 -- don't normally use -auto-all or add SCCs to other packages.
298 -- Hence don't emit the package name in the module here.
300 lits = [ zero, -- StgInt ccID,
301 label, -- char *label,
302 modl, -- char *module,
303 zero, -- StgWord time_ticks
304 zero64, -- StgWord64 mem_alloc
305 subsumed, -- StgInt is_caf
306 zero -- struct _CostCentre *link
308 ; emitDataLits (mkCCLabel cc) lits
311 subsumed | isCafCC cc = mkIntCLit (ord 'c') -- 'c' == is a CAF
312 | otherwise = mkIntCLit (ord 'B') -- 'B' == is boring
315 emitCostCentreStackDecl
318 emitCostCentreStackDecl ccs
319 | Just cc <- maybeSingletonCCS ccs = do
321 -- Note: to avoid making any assumptions about how the
322 -- C compiler (that compiles the RTS, in particular) does
323 -- layouts of structs containing long-longs, simply
324 -- pad out the struct with zero words until we hit the
325 -- size of the overall struct (which we get via DerivedConstants.h)
327 lits = zero : mkCCostCentre cc : replicate (sizeof_ccs_words - 2) zero
328 ; emitDataLits (mkCCSLabel ccs) lits
330 | otherwise = pprPanic "emitCostCentreStackDecl" (ppr ccs)
333 zero64 = CmmInt 0 I64
335 sizeof_ccs_words :: Int
337 -- round up to the next word.
341 (ws,ms) = SIZEOF_CostCentreStack `divMod` wORD_SIZE
343 -- ---------------------------------------------------------------------------
344 -- Registering CCs and CCSs
346 -- (cc)->link = CC_LIST;
348 -- (cc)->ccID = CC_ID++;
350 emitRegisterCC :: CostCentre -> Code
351 emitRegisterCC cc = do
352 { tmp <- newTemp cIntRep
354 CmmStore (cmmOffsetB cc_lit oFFSET_CostCentre_link)
355 (CmmLoad cC_LIST wordRep),
356 CmmStore cC_LIST cc_lit,
357 CmmAssign tmp (CmmLoad cC_ID cIntRep),
358 CmmStore (cmmOffsetB cc_lit oFFSET_CostCentre_ccID) (CmmReg tmp),
359 CmmStore cC_ID (cmmRegOffB tmp 1)
363 cc_lit = CmmLit (CmmLabel (mkCCLabel cc))
365 -- (ccs)->prevStack = CCS_LIST;
367 -- (ccs)->ccsID = CCS_ID++;
369 emitRegisterCCS :: CostCentreStack -> Code
370 emitRegisterCCS ccs = do
371 { tmp <- newTemp cIntRep
373 CmmStore (cmmOffsetB ccs_lit oFFSET_CostCentreStack_prevStack)
374 (CmmLoad cCS_LIST wordRep),
375 CmmStore cCS_LIST ccs_lit,
376 CmmAssign tmp (CmmLoad cCS_ID cIntRep),
377 CmmStore (cmmOffsetB ccs_lit oFFSET_CostCentreStack_ccsID) (CmmReg tmp),
378 CmmStore cCS_ID (cmmRegOffB tmp 1)
382 ccs_lit = CmmLit (CmmLabel (mkCCSLabel ccs))
385 cC_LIST = CmmLit (CmmLabel (mkRtsDataLabel SLIT("CC_LIST")))
386 cC_ID = CmmLit (CmmLabel (mkRtsDataLabel SLIT("CC_ID")))
388 cCS_LIST = CmmLit (CmmLabel (mkRtsDataLabel SLIT("CCS_LIST")))
389 cCS_ID = CmmLit (CmmLabel (mkRtsDataLabel SLIT("CCS_ID")))
391 -- ---------------------------------------------------------------------------
392 -- Set the current cost centre stack
394 emitSetCCC :: CostCentre -> Code
396 | not opt_SccProfilingOn = nopC
398 tmp <- newTemp wordRep
399 ASSERT( sccAbleCostCentre cc )
400 pushCostCentre tmp curCCS cc
401 stmtC (CmmStore curCCSAddr (CmmReg tmp))
402 when (isSccCountCostCentre cc) $
403 stmtC (bumpSccCount curCCS)
405 pushCostCentre :: CmmReg -> CmmExpr -> CostCentre -> Code
406 pushCostCentre result ccs cc
407 = emitRtsCallWithResult result PtrHint
408 SLIT("PushCostCentre") [(ccs,PtrHint),
409 (CmmLit (mkCCostCentre cc), PtrHint)]
411 bumpSccCount :: CmmExpr -> CmmStmt
413 = addToMem REP_CostCentreStack_scc_count
414 (cmmOffsetB ccs oFFSET_CostCentreStack_scc_count) 1
416 -----------------------------------------------------------------------------
418 -- Lag/drag/void stuff
420 -----------------------------------------------------------------------------
423 -- Initial value for the LDV field in a static closure
425 staticLdvInit :: CmmLit
426 staticLdvInit = zeroCLit
429 -- Initial value of the LDV field in a dynamic closure
431 dynLdvInit :: CmmExpr
432 dynLdvInit = -- (era << LDV_SHIFT) | LDV_STATE_CREATE
433 CmmMachOp mo_wordOr [
434 CmmMachOp mo_wordShl [loadEra, CmmLit (mkIntCLit lDV_SHIFT) ],
435 CmmLit (mkWordCLit lDV_STATE_CREATE)
439 -- Initialise the LDV word of a new closure
441 ldvRecordCreate :: CmmExpr -> Code
442 ldvRecordCreate closure = stmtC $ CmmStore (ldvWord closure) dynLdvInit
445 -- Called when a closure is entered, marks the closure as having been "used".
446 -- The closure is not an 'inherently used' one.
447 -- The closure is not IND or IND_OLDGEN because neither is considered for LDV
450 ldvEnter :: CmmExpr -> Code
451 -- Argument is a closure pointer
455 -- LDVW((c)) = (LDVW((c)) & LDV_CREATE_MASK) |
456 -- era | LDV_STATE_USE }
457 emitIf (CmmMachOp mo_wordUGt [loadEra, CmmLit zeroCLit])
458 (stmtC (CmmStore ldv_wd new_ldv_wd))
460 ldv_wd = ldvWord cl_ptr
461 new_ldv_wd = cmmOrWord (cmmAndWord (CmmLoad ldv_wd wordRep)
462 (CmmLit (mkWordCLit lDV_CREATE_MASK)))
463 (cmmOrWord loadEra (CmmLit (mkWordCLit lDV_STATE_USE)))
466 loadEra = CmmMachOp (MO_U_Conv cIntRep wordRep)
467 [CmmLoad (mkLblExpr (mkRtsDataLabel SLIT("era"))) cIntRep]
469 ldvWord :: CmmExpr -> CmmExpr
470 -- Takes the address of a closure, and returns
471 -- the address of the LDV word in the closure
472 ldvWord closure_ptr = cmmOffsetB closure_ptr oFFSET_StgHeader_ldvw
474 -- LDV constants, from ghc/includes/Constants.h
475 lDV_SHIFT = (LDV_SHIFT :: Int)
476 --lDV_STATE_MASK = (LDV_STATE_MASK :: StgWord)
477 lDV_CREATE_MASK = (LDV_CREATE_MASK :: StgWord)
478 --lDV_LAST_MASK = (LDV_LAST_MASK :: StgWord)
479 lDV_STATE_CREATE = (LDV_STATE_CREATE :: StgWord)
480 lDV_STATE_USE = (LDV_STATE_USE :: StgWord)