Cleanup StgCmm pass
[ghc.git] / compiler / codeGen / StgCmmBind.hs
1 -----------------------------------------------------------------------------
2 --
3 -- Stg to C-- code generation: bindings
4 --
5 -- (c) The University of Glasgow 2004-2006
6 --
7 -----------------------------------------------------------------------------
8
9 module StgCmmBind (
10 cgTopRhsClosure,
11 cgBind,
12 emitBlackHoleCode,
13 pushUpdateFrame, emitUpdateFrame
14 ) where
15
16 #include "HsVersions.h"
17
18 import StgCmmExpr
19 import StgCmmMonad
20 import StgCmmEnv
21 import StgCmmCon
22 import StgCmmHeap
23 import StgCmmProf
24 import StgCmmTicky
25 import StgCmmGran
26 import StgCmmLayout
27 import StgCmmUtils
28 import StgCmmClosure
29 import StgCmmForeign (emitPrimCall)
30
31 import MkGraph
32 import CoreSyn ( AltCon(..) )
33 import SMRep
34 import Cmm
35 import CmmInfo
36 import CmmUtils
37 import CLabel
38 import StgSyn
39 import CostCentre
40 import Id
41 import IdInfo
42 import Name
43 import Module
44 import ListSetOps
45 import Util
46 import BasicTypes
47 import Outputable
48 import FastString
49 import Maybes
50 import DynFlags
51
52 import Control.Monad
53
54 ------------------------------------------------------------------------
55 -- Top-level bindings
56 ------------------------------------------------------------------------
57
58 -- For closures bound at top level, allocate in static space.
59 -- They should have no free variables.
60
61 cgTopRhsClosure :: DynFlags
62 -> RecFlag -- member of a recursive group?
63 -> Id
64 -> CostCentreStack -- Optional cost centre annotation
65 -> StgBinderInfo
66 -> UpdateFlag
67 -> [Id] -- Args
68 -> StgExpr
69 -> (CgIdInfo, FCode ())
70
71 cgTopRhsClosure dflags rec id ccs _ upd_flag args body =
72 let closure_label = mkLocalClosureLabel (idName id) (idCafInfo id)
73 cg_id_info = litIdInfo dflags id lf_info (CmmLabel closure_label)
74 lf_info = mkClosureLFInfo dflags id TopLevel [] upd_flag args
75 in (cg_id_info, gen_code dflags lf_info closure_label)
76 where
77 -- special case for a indirection (f = g). We create an IND_STATIC
78 -- closure pointing directly to the indirectee. This is exactly
79 -- what the CAF will eventually evaluate to anyway, we're just
80 -- shortcutting the whole process, and generating a lot less code
81 -- (#7308)
82 --
83 -- Note: we omit the optimisation when this binding is part of a
84 -- recursive group, because the optimisation would inhibit the black
85 -- hole detection from working in that case. Test
86 -- concurrent/should_run/4030 fails, for instance.
87 --
88 gen_code dflags _ closure_label
89 | StgApp f [] <- body, null args, isNonRec rec
90 = do
91 cg_info <- getCgIdInfo f
92 let closure_rep = mkStaticClosureFields dflags
93 indStaticInfoTable ccs MayHaveCafRefs
94 [unLit (idInfoToAmode cg_info)]
95 emitDataLits closure_label closure_rep
96 return ()
97
98 gen_code dflags lf_info closure_label
99 = do { -- LAY OUT THE OBJECT
100 let name = idName id
101 ; mod_name <- getModuleName
102 ; let descr = closureDescription dflags mod_name name
103 closure_info = mkClosureInfo dflags True id lf_info 0 0 descr
104
105 caffy = idCafInfo id
106 info_tbl = mkCmmInfo closure_info -- XXX short-cut
107 closure_rep = mkStaticClosureFields dflags info_tbl ccs caffy []
108
109 -- BUILD THE OBJECT, AND GENERATE INFO TABLE (IF NECESSARY)
110 ; emitDataLits closure_label closure_rep
111 ; let fv_details :: [(NonVoid Id, VirtualHpOffset)]
112 (_, _, fv_details) = mkVirtHeapOffsets dflags (isLFThunk lf_info)
113 (addIdReps [])
114 -- Don't drop the non-void args until the closure info has been made
115 ; forkClosureBody (closureCodeBody True id closure_info ccs
116 (nonVoidIds args) (length args) body fv_details)
117
118 ; return () }
119
120 unLit (CmmLit l) = l
121 unLit _ = panic "unLit"
122
123 ------------------------------------------------------------------------
124 -- Non-top-level bindings
125 ------------------------------------------------------------------------
126
127 cgBind :: StgBinding -> FCode ()
128 cgBind (StgNonRec name rhs)
129 = do { (info, fcode) <- cgRhs name rhs
130 ; addBindC info
131 ; init <- fcode
132 ; emit init }
133 -- init cannot be used in body, so slightly better to sink it eagerly
134
135 cgBind (StgRec pairs)
136 = do { r <- sequence $ unzipWith cgRhs pairs
137 ; let (id_infos, fcodes) = unzip r
138 ; addBindsC id_infos
139 ; (inits, body) <- getCodeR $ sequence fcodes
140 ; emit (catAGraphs inits <*> body) }
141
142 {- Note [cgBind rec]
143
144 Recursive let-bindings are tricky.
145 Consider the following pseudocode:
146
147 let x = \_ -> ... y ...
148 y = \_ -> ... z ...
149 z = \_ -> ... x ...
150 in ...
151
152 For each binding, we need to allocate a closure, and each closure must
153 capture the address of the other closures.
154 We want to generate the following C-- code:
155 // Initialization Code
156 x = hp - 24; // heap address of x's closure
157 y = hp - 40; // heap address of x's closure
158 z = hp - 64; // heap address of x's closure
159 // allocate and initialize x
160 m[hp-8] = ...
161 m[hp-16] = y // the closure for x captures y
162 m[hp-24] = x_info;
163 // allocate and initialize y
164 m[hp-32] = z; // the closure for y captures z
165 m[hp-40] = y_info;
166 // allocate and initialize z
167 ...
168
169 For each closure, we must generate not only the code to allocate and
170 initialize the closure itself, but also some initialization Code that
171 sets a variable holding the closure pointer.
172
173 We could generate a pair of the (init code, body code), but since
174 the bindings are recursive we also have to initialise the
175 environment with the CgIdInfo for all the bindings before compiling
176 anything. So we do this in 3 stages:
177
178 1. collect all the CgIdInfos and initialise the environment
179 2. compile each binding into (init, body) code
180 3. emit all the inits, and then all the bodies
181
182 We'd rather not have separate functions to do steps 1 and 2 for
183 each binding, since in pratice they share a lot of code. So we
184 have just one function, cgRhs, that returns a pair of the CgIdInfo
185 for step 1, and a monadic computation to generate the code in step
186 2.
187
188 The alternative to separating things in this way is to use a
189 fixpoint. That's what we used to do, but it introduces a
190 maintenance nightmare because there is a subtle dependency on not
191 being too strict everywhere. Doing things this way means that the
192 FCode monad can be strict, for example.
193 -}
194
195 cgRhs :: Id
196 -> StgRhs
197 -> FCode (
198 CgIdInfo -- The info for this binding
199 , FCode CmmAGraph -- A computation which will generate the
200 -- code for the binding, and return an
201 -- assignent of the form "x = Hp - n"
202 -- (see above)
203 )
204
205 cgRhs id (StgRhsCon cc con args)
206 = withNewTickyCounterThunk False (idName id) $ -- False for "not static"
207 buildDynCon id True cc con args
208
209 {- See Note [GC recovery] in compiler/codeGen/StgCmmClosure.hs -}
210 cgRhs name (StgRhsClosure cc bi fvs upd_flag _srt args body)
211 = do dflags <- getDynFlags
212 mkRhsClosure dflags name cc bi (nonVoidIds fvs) upd_flag args body
213
214 ------------------------------------------------------------------------
215 -- Non-constructor right hand sides
216 ------------------------------------------------------------------------
217
218 mkRhsClosure :: DynFlags -> Id -> CostCentreStack -> StgBinderInfo
219 -> [NonVoid Id] -- Free vars
220 -> UpdateFlag
221 -> [Id] -- Args
222 -> StgExpr
223 -> FCode (CgIdInfo, FCode CmmAGraph)
224
225 {- mkRhsClosure looks for two special forms of the right-hand side:
226 a) selector thunks
227 b) AP thunks
228
229 If neither happens, it just calls mkClosureLFInfo. You might think
230 that mkClosureLFInfo should do all this, but it seems wrong for the
231 latter to look at the structure of an expression
232
233 Note [Selectors]
234 ~~~~~~~~~~~~~~~~
235 We look at the body of the closure to see if it's a selector---turgid,
236 but nothing deep. We are looking for a closure of {\em exactly} the
237 form:
238
239 ... = [the_fv] \ u [] ->
240 case the_fv of
241 con a_1 ... a_n -> a_i
242
243 Note [Ap thunks]
244 ~~~~~~~~~~~~~~~~
245 A more generic AP thunk of the form
246
247 x = [ x_1...x_n ] \.. [] -> x_1 ... x_n
248
249 A set of these is compiled statically into the RTS, so we just use
250 those. We could extend the idea to thunks where some of the x_i are
251 global ids (and hence not free variables), but this would entail
252 generating a larger thunk. It might be an option for non-optimising
253 compilation, though.
254
255 We only generate an Ap thunk if all the free variables are pointers,
256 for semi-obvious reasons.
257
258 -}
259
260 ---------- Note [Selectors] ------------------
261 mkRhsClosure dflags bndr _cc _bi
262 [NonVoid the_fv] -- Just one free var
263 upd_flag -- Updatable thunk
264 [] -- A thunk
265 (StgCase (StgApp scrutinee [{-no args-}])
266 _ _ _ _ -- ignore uniq, etc.
267 (AlgAlt _)
268 [(DataAlt _, params, _use_mask,
269 (StgApp selectee [{-no args-}]))])
270 | the_fv == scrutinee -- Scrutinee is the only free variable
271 && maybeToBool maybe_offset -- Selectee is a component of the tuple
272 && offset_into_int <= mAX_SPEC_SELECTEE_SIZE dflags -- Offset is small enough
273 = -- NOT TRUE: ASSERT(is_single_constructor)
274 -- The simplifier may have statically determined that the single alternative
275 -- is the only possible case and eliminated the others, even if there are
276 -- other constructors in the datatype. It's still ok to make a selector
277 -- thunk in this case, because we *know* which constructor the scrutinee
278 -- will evaluate to.
279 --
280 -- srt is discarded; it must be empty
281 cgRhsStdThunk bndr lf_info [StgVarArg the_fv]
282 where
283 lf_info = mkSelectorLFInfo bndr offset_into_int
284 (isUpdatable upd_flag)
285 (_, _, params_w_offsets) = mkVirtConstrOffsets dflags (addIdReps params)
286 -- Just want the layout
287 maybe_offset = assocMaybe params_w_offsets (NonVoid selectee)
288 Just the_offset = maybe_offset
289 offset_into_int = the_offset - fixedHdrSize dflags
290
291 ---------- Note [Ap thunks] ------------------
292 mkRhsClosure dflags bndr _cc _bi
293 fvs
294 upd_flag
295 [] -- No args; a thunk
296 (StgApp fun_id args)
297
298 | args `lengthIs` (arity-1)
299 && all (isGcPtrRep . idPrimRep . unsafe_stripNV) fvs
300 && isUpdatable upd_flag
301 && arity <= mAX_SPEC_AP_SIZE dflags
302 && not (gopt Opt_SccProfilingOn dflags)
303 -- not when profiling: we don't want to
304 -- lose information about this particular
305 -- thunk (e.g. its type) (#949)
306
307 -- Ha! an Ap thunk
308 = cgRhsStdThunk bndr lf_info payload
309
310 where
311 lf_info = mkApLFInfo bndr upd_flag arity
312 -- the payload has to be in the correct order, hence we can't
313 -- just use the fvs.
314 payload = StgVarArg fun_id : args
315 arity = length fvs
316
317 ---------- Default case ------------------
318 mkRhsClosure dflags bndr cc _ fvs upd_flag args body
319 = do { let lf_info = mkClosureLFInfo dflags bndr NotTopLevel fvs upd_flag args
320 ; (id_info, reg) <- rhsIdInfo bndr lf_info
321 ; return (id_info, gen_code lf_info reg) }
322 where
323 gen_code lf_info reg
324 = do { -- LAY OUT THE OBJECT
325 -- If the binder is itself a free variable, then don't store
326 -- it in the closure. Instead, just bind it to Node on entry.
327 -- NB we can be sure that Node will point to it, because we
328 -- haven't told mkClosureLFInfo about this; so if the binder
329 -- _was_ a free var of its RHS, mkClosureLFInfo thinks it *is*
330 -- stored in the closure itself, so it will make sure that
331 -- Node points to it...
332 ; let
333 is_elem = isIn "cgRhsClosure"
334 bndr_is_a_fv = (NonVoid bndr) `is_elem` fvs
335 reduced_fvs | bndr_is_a_fv = fvs `minusList` [NonVoid bndr]
336 | otherwise = fvs
337
338
339 -- MAKE CLOSURE INFO FOR THIS CLOSURE
340 ; mod_name <- getModuleName
341 ; dflags <- getDynFlags
342 ; let name = idName bndr
343 descr = closureDescription dflags mod_name name
344 fv_details :: [(NonVoid Id, VirtualHpOffset)]
345 (tot_wds, ptr_wds, fv_details)
346 = mkVirtHeapOffsets dflags (isLFThunk lf_info)
347 (addIdReps (map unsafe_stripNV reduced_fvs))
348 closure_info = mkClosureInfo dflags False -- Not static
349 bndr lf_info tot_wds ptr_wds
350 descr
351
352 -- BUILD ITS INFO TABLE AND CODE
353 ; forkClosureBody $
354 -- forkClosureBody: (a) ensure that bindings in here are not seen elsewhere
355 -- (b) ignore Sequel from context; use empty Sequel
356 -- And compile the body
357 closureCodeBody False bndr closure_info cc (nonVoidIds args)
358 (length args) body fv_details
359
360 -- BUILD THE OBJECT
361 -- ; (use_cc, blame_cc) <- chooseDynCostCentres cc args body
362 ; let use_cc = curCCS; blame_cc = curCCS
363 ; emit (mkComment $ mkFastString "calling allocDynClosure")
364 ; let toVarArg (NonVoid a, off) = (NonVoid (StgVarArg a), off)
365 ; let info_tbl = mkCmmInfo closure_info
366 ; hp_plus_n <- allocDynClosure (Just bndr) info_tbl lf_info use_cc blame_cc
367 (map toVarArg fv_details)
368
369 -- RETURN
370 ; return (mkRhsInit dflags reg lf_info hp_plus_n) }
371
372 -------------------------
373 cgRhsStdThunk
374 :: Id
375 -> LambdaFormInfo
376 -> [StgArg] -- payload
377 -> FCode (CgIdInfo, FCode CmmAGraph)
378
379 cgRhsStdThunk bndr lf_info payload
380 = do { (id_info, reg) <- rhsIdInfo bndr lf_info
381 ; return (id_info, gen_code reg)
382 }
383 where
384 gen_code reg -- AHA! A STANDARD-FORM THUNK
385 = withNewTickyCounterStdThunk False (idName bndr) $ -- False for "not static"
386 do
387 { -- LAY OUT THE OBJECT
388 mod_name <- getModuleName
389 ; dflags <- getDynFlags
390 ; let (tot_wds, ptr_wds, payload_w_offsets)
391 = mkVirtHeapOffsets dflags (isLFThunk lf_info) (addArgReps payload)
392
393 descr = closureDescription dflags mod_name (idName bndr)
394 closure_info = mkClosureInfo dflags False -- Not static
395 bndr lf_info tot_wds ptr_wds
396 descr
397
398 -- ; (use_cc, blame_cc) <- chooseDynCostCentres cc [{- no args-}] body
399 ; let use_cc = curCCS; blame_cc = curCCS
400
401 ; tickyEnterStdThunk closure_info
402
403 -- BUILD THE OBJECT
404 ; let info_tbl = mkCmmInfo closure_info
405 ; hp_plus_n <- allocDynClosure (Just bndr) info_tbl lf_info
406 use_cc blame_cc payload_w_offsets
407
408 -- RETURN
409 ; return (mkRhsInit dflags reg lf_info hp_plus_n) }
410
411
412 mkClosureLFInfo :: DynFlags
413 -> Id -- The binder
414 -> TopLevelFlag -- True of top level
415 -> [NonVoid Id] -- Free vars
416 -> UpdateFlag -- Update flag
417 -> [Id] -- Args
418 -> LambdaFormInfo
419 mkClosureLFInfo dflags bndr top fvs upd_flag args
420 | null args =
421 mkLFThunk (idType bndr) top (map unsafe_stripNV fvs) upd_flag
422 | otherwise =
423 mkLFReEntrant top (map unsafe_stripNV fvs) args (mkArgDescr dflags args)
424
425
426 ------------------------------------------------------------------------
427 -- The code for closures}
428 ------------------------------------------------------------------------
429
430 closureCodeBody :: Bool -- whether this is a top-level binding
431 -> Id -- the closure's name
432 -> ClosureInfo -- Lots of information about this closure
433 -> CostCentreStack -- Optional cost centre attached to closure
434 -> [NonVoid Id] -- incoming args to the closure
435 -> Int -- arity, including void args
436 -> StgExpr
437 -> [(NonVoid Id, VirtualHpOffset)] -- the closure's free vars
438 -> FCode ()
439
440 {- There are two main cases for the code for closures.
441
442 * If there are *no arguments*, then the closure is a thunk, and not in
443 normal form. So it should set up an update frame (if it is
444 shared). NB: Thunks cannot have a primitive type!
445
446 * If there is *at least one* argument, then this closure is in
447 normal form, so there is no need to set up an update frame.
448
449 The Macros for GrAnSim are produced at the beginning of the
450 argSatisfactionCheck (by calling fetchAndReschedule).
451 There info if Node points to closure is available. -- HWL -}
452
453 closureCodeBody top_lvl bndr cl_info cc _args arity body fv_details
454 | arity == 0 -- No args i.e. thunk
455 = withNewTickyCounterThunk (isStaticClosure cl_info) (closureName cl_info) $
456 emitClosureProcAndInfoTable top_lvl bndr lf_info info_tbl [] $
457 \(_, node, _) -> thunkCode cl_info fv_details cc node arity body
458 where
459 lf_info = closureLFInfo cl_info
460 info_tbl = mkCmmInfo cl_info
461
462 closureCodeBody top_lvl bndr cl_info cc args arity body fv_details
463 = -- Note: args may be [], if all args are Void
464 withNewTickyCounterFun (closureName cl_info) args $ do {
465
466 ; let
467 lf_info = closureLFInfo cl_info
468 info_tbl = mkCmmInfo cl_info
469
470 -- Emit the main entry code
471 ; emitClosureProcAndInfoTable top_lvl bndr lf_info info_tbl args $
472 \(_offset, node, arg_regs) -> do
473 -- Emit slow-entry code (for entering a closure through a PAP)
474 { mkSlowEntryCode bndr cl_info arg_regs
475
476 ; dflags <- getDynFlags
477 ; let node_points = nodeMustPointToIt dflags lf_info
478 node' = if node_points then Just node else Nothing
479 ; when node_points (ldvEnterClosure cl_info)
480 ; granYield arg_regs node_points
481
482 -- Main payload
483 ; entryHeapCheck cl_info node' arity arg_regs $ do
484 { -- ticky after heap check to avoid double counting
485 tickyEnterFun cl_info
486 ; enterCostCentreFun cc
487 (CmmMachOp (mo_wordSub dflags)
488 [ CmmReg nodeReg
489 , mkIntExpr dflags (funTag dflags cl_info) ])
490 ; fv_bindings <- mapM bind_fv fv_details
491 -- Load free vars out of closure *after*
492 -- heap check, to reduce live vars over check
493 ; when node_points $ load_fvs node lf_info fv_bindings
494 ; void $ cgExpr body
495 }}
496 }
497
498 -- A function closure pointer may be tagged, so we
499 -- must take it into account when accessing the free variables.
500 bind_fv :: (NonVoid Id, VirtualHpOffset) -> FCode (LocalReg, WordOff)
501 bind_fv (id, off) = do { reg <- rebindToReg id; return (reg, off) }
502
503 load_fvs :: LocalReg -> LambdaFormInfo -> [(LocalReg, WordOff)] -> FCode ()
504 load_fvs node lf_info = mapM_ (\ (reg, off) ->
505 do dflags <- getDynFlags
506 let tag = lfDynTag dflags lf_info
507 emit $ mkTaggedObjectLoad dflags reg node off tag)
508
509 -----------------------------------------
510 -- The "slow entry" code for a function. This entry point takes its
511 -- arguments on the stack. It loads the arguments into registers
512 -- according to the calling convention, and jumps to the function's
513 -- normal entry point. The function's closure is assumed to be in
514 -- R1/node.
515 --
516 -- The slow entry point is used for unknown calls: eg. stg_PAP_entry
517
518 mkSlowEntryCode :: Id -> ClosureInfo -> [LocalReg] -> FCode ()
519 -- If this function doesn't have a specialised ArgDescr, we need
520 -- to generate the function's arg bitmap and slow-entry code.
521 -- Here, we emit the slow-entry code.
522 mkSlowEntryCode bndr cl_info arg_regs -- function closure is already in `Node'
523 | Just (_, ArgGen _) <- closureFunInfo cl_info
524 = do dflags <- getDynFlags
525 let node = idToReg dflags (NonVoid bndr)
526 slow_lbl = closureSlowEntryLabel cl_info
527 fast_lbl = closureLocalEntryLabel dflags cl_info
528 -- mkDirectJump does not clobber `Node' containing function closure
529 jump = mkJump dflags NativeNodeCall
530 (mkLblExpr fast_lbl)
531 (map (CmmReg . CmmLocal) (node : arg_regs))
532 (initUpdFrameOff dflags)
533 emitProcWithConvention Slow Nothing slow_lbl (node : arg_regs) jump
534 | otherwise = return ()
535
536 -----------------------------------------
537 thunkCode :: ClosureInfo -> [(NonVoid Id, VirtualHpOffset)] -> CostCentreStack
538 -> LocalReg -> Int -> StgExpr -> FCode ()
539 thunkCode cl_info fv_details _cc node arity body
540 = do { dflags <- getDynFlags
541 ; let node_points = nodeMustPointToIt dflags (closureLFInfo cl_info)
542 node' = if node_points then Just node else Nothing
543 ; ldvEnterClosure cl_info -- NB: Node always points when profiling
544 ; granThunk node_points
545
546 -- Heap overflow check
547 ; entryHeapCheck cl_info node' arity [] $ do
548 { -- Overwrite with black hole if necessary
549 -- but *after* the heap-overflow check
550 ; tickyEnterThunk cl_info
551 ; when (blackHoleOnEntry cl_info && node_points)
552 (blackHoleIt node)
553
554 -- Push update frame
555 ; setupUpdate cl_info node $
556 -- We only enter cc after setting up update so
557 -- that cc of enclosing scope will be recorded
558 -- in update frame CAF/DICT functions will be
559 -- subsumed by this enclosing cc
560 do { tickyEnterThunk cl_info
561 ; enterCostCentreThunk (CmmReg nodeReg)
562 ; let lf_info = closureLFInfo cl_info
563 ; fv_bindings <- mapM bind_fv fv_details
564 ; load_fvs node lf_info fv_bindings
565 ; void $ cgExpr body }}}
566
567
568 ------------------------------------------------------------------------
569 -- Update and black-hole wrappers
570 ------------------------------------------------------------------------
571
572 blackHoleIt :: LocalReg -> FCode ()
573 -- Only called for closures with no args
574 -- Node points to the closure
575 blackHoleIt node_reg
576 = emitBlackHoleCode (CmmReg (CmmLocal node_reg))
577
578 emitBlackHoleCode :: CmmExpr -> FCode ()
579 emitBlackHoleCode node = do
580 dflags <- getDynFlags
581
582 -- Eager blackholing is normally disabled, but can be turned on with
583 -- -feager-blackholing. When it is on, we replace the info pointer
584 -- of the thunk with stg_EAGER_BLACKHOLE_info on entry.
585
586 -- If we wanted to do eager blackholing with slop filling, we'd need
587 -- to do it at the *end* of a basic block, otherwise we overwrite
588 -- the free variables in the thunk that we still need. We have a
589 -- patch for this from Andy Cheadle, but not incorporated yet. --SDM
590 -- [6/2004]
591 --
592 -- Previously, eager blackholing was enabled when ticky-ticky was
593 -- on. But it didn't work, and it wasn't strictly necessary to bring
594 -- back minimal ticky-ticky, so now EAGER_BLACKHOLING is
595 -- unconditionally disabled. -- krc 1/2007
596
597 -- Note the eager-blackholing check is here rather than in blackHoleOnEntry,
598 -- because emitBlackHoleCode is called from CmmParse.
599
600 let eager_blackholing = not (gopt Opt_SccProfilingOn dflags)
601 && gopt Opt_EagerBlackHoling dflags
602 -- Profiling needs slop filling (to support LDV
603 -- profiling), so currently eager blackholing doesn't
604 -- work with profiling.
605
606 when eager_blackholing $ do
607 emitStore (cmmOffsetW dflags node (fixedHdrSize dflags))
608 (CmmReg (CmmGlobal CurrentTSO))
609 emitPrimCall [] MO_WriteBarrier []
610 emitStore node (CmmReg (CmmGlobal EagerBlackholeInfo))
611
612 setupUpdate :: ClosureInfo -> LocalReg -> FCode () -> FCode ()
613 -- Nota Bene: this function does not change Node (even if it's a CAF),
614 -- so that the cost centre in the original closure can still be
615 -- extracted by a subsequent enterCostCentre
616 setupUpdate closure_info node body
617 | not (lfUpdatable (closureLFInfo closure_info))
618 = body
619
620 | not (isStaticClosure closure_info)
621 = if not (closureUpdReqd closure_info)
622 then do tickyUpdateFrameOmitted; body
623 else do
624 tickyPushUpdateFrame
625 dflags <- getDynFlags
626 let
627 bh = blackHoleOnEntry closure_info &&
628 not (gopt Opt_SccProfilingOn dflags) &&
629 gopt Opt_EagerBlackHoling dflags
630
631 lbl | bh = mkBHUpdInfoLabel
632 | otherwise = mkUpdInfoLabel
633
634 pushUpdateFrame lbl (CmmReg (CmmLocal node)) body
635
636 | otherwise -- A static closure
637 = do { tickyUpdateBhCaf closure_info
638
639 ; if closureUpdReqd closure_info
640 then do -- Blackhole the (updatable) CAF:
641 { upd_closure <- link_caf node True
642 ; pushUpdateFrame mkBHUpdInfoLabel upd_closure body }
643 else do {tickyUpdateFrameOmitted; body}
644 }
645
646 -----------------------------------------------------------------------------
647 -- Setting up update frames
648
649 -- Push the update frame on the stack in the Entry area,
650 -- leaving room for the return address that is already
651 -- at the old end of the area.
652 --
653 pushUpdateFrame :: CLabel -> CmmExpr -> FCode () -> FCode ()
654 pushUpdateFrame lbl updatee body
655 = do
656 updfr <- getUpdFrameOff
657 dflags <- getDynFlags
658 let
659 hdr = fixedHdrSize dflags * wORD_SIZE dflags
660 frame = updfr + hdr + sIZEOF_StgUpdateFrame_NoHdr dflags
661 --
662 emitUpdateFrame dflags (CmmStackSlot Old frame) lbl updatee
663 withUpdFrameOff frame body
664
665 emitUpdateFrame :: DynFlags -> CmmExpr -> CLabel -> CmmExpr -> FCode ()
666 emitUpdateFrame dflags frame lbl updatee = do
667 let
668 hdr = fixedHdrSize dflags * wORD_SIZE dflags
669 off_updatee = hdr + oFFSET_StgUpdateFrame_updatee dflags
670 --
671 emitStore frame (mkLblExpr lbl)
672 emitStore (cmmOffset dflags frame off_updatee) updatee
673 initUpdFrameProf frame
674
675 -----------------------------------------------------------------------------
676 -- Entering a CAF
677 --
678 -- When a CAF is first entered, it creates a black hole in the heap,
679 -- and updates itself with an indirection to this new black hole.
680 --
681 -- We update the CAF with an indirection to a newly-allocated black
682 -- hole in the heap. We also set the blocking queue on the newly
683 -- allocated black hole to be empty.
684 --
685 -- Why do we make a black hole in the heap when we enter a CAF?
686 --
687 -- - for a generational garbage collector, which needs a fast
688 -- test for whether an updatee is in an old generation or not
689 --
690 -- - for the parallel system, which can implement updates more
691 -- easily if the updatee is always in the heap. (allegedly).
692 --
693 -- When debugging, we maintain a separate CAF list so we can tell when
694 -- a CAF has been garbage collected.
695
696 -- newCAF must be called before the itbl ptr is overwritten, since
697 -- newCAF records the old itbl ptr in order to do CAF reverting
698 -- (which Hugs needs to do in order that combined mode works right.)
699 --
700
701 -- ToDo [Feb 04] This entire link_caf nonsense could all be moved
702 -- into the "newCAF" RTS procedure, which we call anyway, including
703 -- the allocation of the black-hole indirection closure.
704 -- That way, code size would fall, the CAF-handling code would
705 -- be closer together, and the compiler wouldn't need to know
706 -- about off_indirectee etc.
707
708 link_caf :: LocalReg -- pointer to the closure
709 -> Bool -- True <=> updatable, False <=> single-entry
710 -> FCode CmmExpr -- Returns amode for closure to be updated
711 -- To update a CAF we must allocate a black hole, link the CAF onto the
712 -- CAF list, then update the CAF to point to the fresh black hole.
713 -- This function returns the address of the black hole, so it can be
714 -- updated with the new value when available. The reason for all of this
715 -- is that we only want to update dynamic heap objects, not static ones,
716 -- so that generational GC is easier.
717 link_caf node _is_upd = do
718 { dflags <- getDynFlags
719 -- Alloc black hole specifying CC_HDR(Node) as the cost centre
720 ; let use_cc = costCentreFrom dflags (CmmReg nodeReg)
721 blame_cc = use_cc
722 tso = CmmReg (CmmGlobal CurrentTSO)
723
724 ; hp_rel <- allocDynClosureCmm Nothing cafBlackHoleInfoTable mkLFBlackHole
725 use_cc blame_cc [(tso,fixedHdrSize dflags)]
726 -- small optimisation: we duplicate the hp_rel expression in
727 -- both the newCAF call and the value returned below.
728 -- If we instead used allocDynClosureReg which assigns it to a reg,
729 -- then the reg is live across the newCAF call and gets spilled,
730 -- which is stupid. Really we should have an optimisation pass to
731 -- fix this, but we don't yet. --SDM
732
733 -- Call the RTS function newCAF to add the CAF to the CafList
734 -- so that the garbage collector can find them
735 -- This must be done *before* the info table pointer is overwritten,
736 -- because the old info table ptr is needed for reversion
737 ; ret <- newTemp (bWord dflags)
738 ; emitRtsCallGen [(ret,NoHint)] (mkForeignLabel (fsLit "newCAF") Nothing ForeignLabelInExternalPackage IsFunction)
739 [ (CmmReg (CmmGlobal BaseReg), AddrHint),
740 (CmmReg (CmmLocal node), AddrHint),
741 (hp_rel, AddrHint) ]
742 False
743
744 -- see Note [atomic CAF entry] in rts/sm/Storage.c
745 ; updfr <- getUpdFrameOff
746 ; emit =<< mkCmmIfThen
747 (CmmMachOp (mo_wordEq dflags) [ CmmReg (CmmLocal ret), CmmLit (zeroCLit dflags)])
748 -- re-enter the CAF
749 (let target = entryCode dflags (closureInfoPtr dflags (CmmReg (CmmLocal node))) in
750 mkJump dflags NativeNodeCall target [] updfr)
751
752 ; return hp_rel }
753
754 ------------------------------------------------------------------------
755 -- Profiling
756 ------------------------------------------------------------------------
757
758 -- For "global" data constructors the description is simply occurrence
759 -- name of the data constructor itself. Otherwise it is determined by
760 -- @closureDescription@ from the let binding information.
761
762 closureDescription :: DynFlags
763 -> Module -- Module
764 -> Name -- Id of closure binding
765 -> String
766 -- Not called for StgRhsCon which have global info tables built in
767 -- CgConTbls.lhs with a description generated from the data constructor
768 closureDescription dflags mod_name name
769 = showSDocDump dflags (char '<' <>
770 (if isExternalName name
771 then ppr name -- ppr will include the module name prefix
772 else pprModule mod_name <> char '.' <> ppr name) <>
773 char '>')
774 -- showSDocDump, because we want to see the unique on the Name.
775