StgCmmHeap.hs 20.5 KB
Newer Older
1 2 3 4 5 6 7 8 9
-----------------------------------------------------------------------------
--
-- Stg to C--: heap management functions
--
-- (c) The University of Glasgow 2004-2006
--
-----------------------------------------------------------------------------

module StgCmmHeap (
10 11
        getVirtHp, setVirtHp, setRealHp,
        getHpRelOffset, hpRel,
12

13
        entryHeapCheck, altHeapCheck, noEscapeHeapCheck, altHeapCheckReturnsTo,
14 15
        heapStackCheckGen,
        entryHeapCheck',
16

17 18
        mkVirtHeapOffsets, mkVirtConstrOffsets,
        mkStaticClosureFields, mkStaticClosure,
19

20
        allocDynClosure, allocDynClosureCmm,
21
        emitSetDynHdr
22 23 24 25 26 27 28 29 30
    ) where

#include "HsVersions.h"

import StgSyn
import CLabel
import StgCmmLayout
import StgCmmUtils
import StgCmmMonad
31
import StgCmmProf (profDynAlloc, dynProfHdr, staticProfHdr)
32 33 34 35
import StgCmmTicky
import StgCmmClosure
import StgCmmEnv

36
import MkGraph
37

38
import Hoopl
39
import SMRep
40
import Cmm
41 42
import CmmUtils
import CostCentre
43
import IdInfo( CafInfo(..), mayHaveCafRefs )
44
import Id ( Id )
45
import Module
46
import DynFlags
47
import FastString( mkFastString, fsLit )
48

49
import Control.Monad (when)
50
import Data.Maybe (isJust)
51

52
-----------------------------------------------------------
53
--              Initialise dynamic heap objects
54 55 56
-----------------------------------------------------------

allocDynClosure
57 58
        :: Maybe Id
        -> CmmInfoTable
Simon Marlow's avatar
Simon Marlow committed
59
        -> LambdaFormInfo
60 61 62 63 64 65 66
        -> CmmExpr              -- Cost Centre to stick in the object
        -> CmmExpr              -- Cost Centre to blame for this alloc
                                -- (usually the same; sometimes "OVERHEAD")

        -> [(NonVoid StgArg, VirtualHpOffset)]  -- Offsets from start of object
                                                -- ie Info ptr has offset zero.
                                                -- No void args in here
67
        -> FCode CmmExpr -- returns Hp+n
68

69
allocDynClosureCmm
70
        :: Maybe Id -> CmmInfoTable -> LambdaFormInfo -> CmmExpr -> CmmExpr
71 72 73
        -> [(CmmExpr, VirtualHpOffset)]
        -> FCode CmmExpr -- returns Hp+n

74
-- allocDynClosure allocates the thing in the heap,
75
-- and modifies the virtual Hp to account for this.
76 77 78
-- The second return value is the graph that sets the value of the
-- returned LocalReg, which should point to the closure after executing
-- the graph.
79

80 81 82 83 84 85 86 87 88 89
-- allocDynClosure returns an (Hp+8) CmmExpr, and hence the result is
-- only valid until Hp is changed.  The caller should assign the
-- result to a LocalReg if it is required to remain live.
--
-- The reason we don't assign it to a LocalReg here is that the caller
-- is often about to call regIdInfo, which immediately assigns the
-- result of allocDynClosure to a new temp in order to add the tag.
-- So by not generating a LocalReg here we avoid a common source of
-- new temporaries and save some compile time.  This can be quite
-- significant - see test T4801.
90 91


92
allocDynClosure mb_id info_tbl lf_info use_cc _blame_cc args_w_offsets
93 94
  = do  { let (args, offsets) = unzip args_w_offsets
        ; cmm_args <- mapM getArgAmode args     -- No void args
95
        ; allocDynClosureCmm mb_id info_tbl lf_info
Simon Marlow's avatar
Simon Marlow committed
96
                             use_cc _blame_cc (zip cmm_args offsets)
97 98
        }

99
allocDynClosureCmm mb_id info_tbl lf_info use_cc _blame_cc amodes_w_offsets
100 101 102
  = do  { virt_hp <- getVirtHp

        -- SAY WHAT WE ARE ABOUT TO DO
Simon Marlow's avatar
Simon Marlow committed
103
        ; let rep = cit_rep info_tbl
104
        ; tickyDynAlloc mb_id rep lf_info
Simon Marlow's avatar
Simon Marlow committed
105
        ; profDynAlloc rep use_cc
106 107 108 109 110 111 112 113

        -- FIND THE OFFSET OF THE INFO-PTR WORD
        ; let   info_offset = virt_hp + 1
                -- info_offset is the VirtualHpOffset of the first
                -- word of the new object
                -- Remember, virtHp points to last allocated word,
                -- ie 1 *before* the info-ptr word of new object.

Simon Marlow's avatar
Simon Marlow committed
114
                info_ptr = CmmLit (CmmLabel (cit_lbl info_tbl))
115 116 117

        -- ALLOCATE THE OBJECT
        ; base <- getHpRelOffset info_offset
118
        ; emitComment $ mkFastString "allocDynClosure"
119 120 121 122 123
        ; emitSetDynHdr base info_ptr  use_cc
        ; let (cmm_args, offsets) = unzip amodes_w_offsets
        ; hpStore base cmm_args offsets

        -- BUMP THE VIRTUAL HEAP POINTER
124 125
        ; dflags <- getDynFlags
        ; setVirtHp (virt_hp + heapClosureSize dflags rep)
126

127 128
        ; getHpRelOffset info_offset
        }
129 130

emitSetDynHdr :: CmmExpr -> CmmExpr -> CmmExpr -> FCode ()
131
emitSetDynHdr base info_ptr ccs
132 133
  = do dflags <- getDynFlags
       hpStore base (header dflags) [0..]
134
  where
135 136
    header :: DynFlags -> [CmmExpr]
    header dflags = [info_ptr] ++ dynProfHdr dflags ccs
Jan Stolarek's avatar
Jan Stolarek committed
137
        -- ToDof: Parallel stuff
138
        -- No ticky header
139 140 141 142

hpStore :: CmmExpr -> [CmmExpr] -> [VirtualHpOffset] -> FCode ()
-- Store the item (expr,off) in base[off]
hpStore base vals offs
143 144 145
  = do dflags <- getDynFlags
       let mk_store val off = mkStore (cmmOffsetW dflags base off) val
       emit (catAGraphs (zipWith mk_store vals offs))
146 147 148


-----------------------------------------------------------
149
--              Layout of static closures
150 151 152 153 154
-----------------------------------------------------------

-- Make a static closure, adding on any extra padding needed for CAFs,
-- and adding a static link field if necessary.

155
mkStaticClosureFields
156 157
        :: DynFlags
        -> CmmInfoTable
158
        -> CostCentreStack
159
        -> CafInfo
160 161
        -> [CmmLit]             -- Payload
        -> [CmmLit]             -- The full closure
162 163
mkStaticClosureFields dflags info_tbl ccs caf_refs payload
  = mkStaticClosure dflags info_lbl ccs payload padding
164
        static_link_field saved_info_field
165
  where
Simon Marlow's avatar
Simon Marlow committed
166
    info_lbl = cit_lbl info_tbl
167 168 169 170 171 172 173 174 175

    -- CAFs must have consistent layout, regardless of whether they
    -- are actually updatable or not.  The layout of a CAF is:
    --
    --        3 saved_info
    --        2 static_link
    --        1 indirectee
    --        0 info ptr
    --
Simon Marlow's avatar
Simon Marlow committed
176 177 178
    -- the static_link and saved_info fields must always be in the
    -- same place.  So we use isThunkRep rather than closureUpdReqd
    -- here:
179

Simon Marlow's avatar
Simon Marlow committed
180
    is_caf = isThunkRep (cit_rep info_tbl)
181

182
    padding
183 184
        | is_caf && null payload = [mkIntCLit dflags 0]
        | otherwise = []
185 186

    static_link_field
187
        | is_caf || staticClosureNeedsLink (mayHaveCafRefs caf_refs) info_tbl
Simon Marlow's avatar
Simon Marlow committed
188 189 190
        = [static_link_value]
        | otherwise
        = []
191 192

    saved_info_field
193
        | is_caf     = [mkIntCLit dflags 0]
194
        | otherwise  = []
195

196
        -- For a static constructor which has NoCafRefs, we set the
197 198
        -- static link field to a non-zero value so the garbage
        -- collector will ignore it.
199
    static_link_value
200 201
        | mayHaveCafRefs caf_refs  = mkIntCLit dflags 0
        | otherwise                = mkIntCLit dflags 1  -- No CAF refs
202 203


204
mkStaticClosure :: DynFlags -> CLabel -> CostCentreStack -> [CmmLit]
205
  -> [CmmLit] -> [CmmLit] -> [CmmLit] -> [CmmLit]
206
mkStaticClosure dflags info_lbl ccs payload padding static_link_field saved_info_field
207
  =  [CmmLabel info_lbl]
Jan Stolarek's avatar
Jan Stolarek committed
208
  ++ staticProfHdr dflags ccs
209
  ++ concatMap (padLitToWord dflags) payload
210
  ++ padding
211 212 213
  ++ static_link_field
  ++ saved_info_field

214 215
-- JD: Simon had ellided this padding, but without it the C back end asserts
-- failure. Maybe it's a bad assertion, and this padding is indeed unnecessary?
216 217 218
padLitToWord :: DynFlags -> CmmLit -> [CmmLit]
padLitToWord dflags lit = lit : padding pad_length
  where width = typeWidth (cmmLitType dflags lit)
219
        pad_length = wORD_SIZE dflags - widthInBytes width :: Int
220 221 222 223 224 225 226

        padding n | n <= 0 = []
                  | n `rem` 2 /= 0 = CmmInt 0 W8  : padding (n-1)
                  | n `rem` 4 /= 0 = CmmInt 0 W16 : padding (n-2)
                  | n `rem` 8 /= 0 = CmmInt 0 W32 : padding (n-4)
                  | otherwise      = CmmInt 0 W64 : padding (n-8)

227
-----------------------------------------------------------
228
--              Heap overflow checking
229 230 231 232 233 234 235 236 237 238 239 240
-----------------------------------------------------------

{- Note [Heap checks]
   ~~~~~~~~~~~~~~~~~~
Heap checks come in various forms.  We provide the following entry
points to the runtime system, all of which use the native C-- entry
convention.

  * gc() performs garbage collection and returns
    nothing to its caller

  * A series of canned entry points like
241
        r = gc_1p( r )
242 243
    where r is a pointer.  This performs gc, and
    then returns its argument r to its caller.
244

245
  * A series of canned entry points like
246
        gcfun_2p( f, x, y )
247 248 249 250 251 252 253 254 255
    where f is a function closure of arity 2
    This performs garbage collection, keeping alive the
    three argument ptrs, and then tail-calls f(x,y)

These are used in the following circumstances

* entryHeapCheck: Function entry
    (a) With a canned GC entry sequence
        f( f_clo, x:ptr, y:ptr ) {
256 257 258
             Hp = Hp+8
             if Hp > HpLim goto L
             ...
259 260 261
          L: HpAlloc = 8
             jump gcfun_2p( f_clo, x, y ) }
     Note the tail call to the garbage collector;
262
     it should do no register shuffling
263 264 265

    (b) No canned sequence
        f( f_clo, x:ptr, y:ptr, ...etc... ) {
266 267 268
          T: Hp = Hp+8
             if Hp > HpLim goto L
             ...
269
          L: HpAlloc = 8
270 271
             call gc()  -- Needs an info table
             goto T }
272 273

* altHeapCheck: Immediately following an eval
274 275
  Started as
        case f x y of r { (p,q) -> rhs }
276 277 278
  (a) With a canned sequence for the results of f
       (which is the very common case since
       all boxed cases return just one pointer
279 280 281 282 283 284
           ...
           r = f( x, y )
        K:      -- K needs an info table
           Hp = Hp+8
           if Hp > HpLim goto L
           ...code for rhs...
285

286 287
        L: r = gc_1p( r )
           goto K }
288

289 290 291 292
        Here, the info table needed by the call
        to gc_1p should be the *same* as the
        one for the call to f; the C-- optimiser
        spots this sharing opportunity)
293 294 295

   (b) No canned sequence for results of f
       Note second info table
296 297 298 299 300 301
           ...
           (r1,r2,r3) = call f( x, y )
        K:
           Hp = Hp+8
           if Hp > HpLim goto L
           ...code for rhs...
302

303 304
        L: call gc()    -- Extra info table here
           goto K
305 306 307

* generalHeapCheck: Anywhere else
  e.g. entry to thunk
308
       case branch *not* following eval,
309 310 311
       or let-no-escape
  Exactly the same as the previous case:

312 313 314 315
        K:      -- K needs an info table
           Hp = Hp+8
           if Hp > HpLim goto L
           ...
316

317 318
        L: call gc()
           goto K
319 320 321 322 323
-}

--------------------------------------------------------------
-- A heap/stack check at a function or thunk entry point.

324 325 326 327 328 329
entryHeapCheck :: ClosureInfo
               -> Maybe LocalReg -- Function (closure environment)
               -> Int            -- Arity -- not same as len args b/c of voids
               -> [LocalReg]     -- Non-void args (empty for thunk)
               -> FCode ()
               -> FCode ()
330

331
entryHeapCheck cl_info nodeSet arity args code
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
  = entryHeapCheck' is_fastf node arity args code
  where
    node = case nodeSet of
              Just r  -> CmmReg (CmmLocal r)
              Nothing -> CmmLit (CmmLabel $ staticClosureLabel cl_info)

    is_fastf = case closureFunInfo cl_info of
                 Just (_, ArgGen _) -> False
                 _otherwise         -> True

-- | lower-level version for CmmParse
entryHeapCheck' :: Bool           -- is a known function pattern
                -> CmmExpr        -- expression for the closure pointer
                -> Int            -- Arity -- not same as len args b/c of voids
                -> [LocalReg]     -- Non-void args (empty for thunk)
                -> FCode ()
                -> FCode ()
entryHeapCheck' is_fastf node arity args code
350 351
  = do dflags <- getDynFlags
       let is_thunk = arity == 0
352 353

           args' = map (CmmReg . CmmLocal) args
354 355 356 357 358 359 360
           stg_gc_fun    = CmmReg (CmmGlobal GCFun)
           stg_gc_enter1 = CmmReg (CmmGlobal GCEnter1)

           {- Thunks:          jump stg_gc_enter_1

              Function (fast): call (NativeNode) stg_gc_fun(fun, args)

361
              Function (slow): call (slow) stg_gc_fun(fun, args)
362 363 364
           -}
           gc_call upd
               | is_thunk
365
                 = mkJump dflags NativeNodeCall stg_gc_enter1 [node] upd
366 367

               | is_fastf
368
                 = mkJump dflags NativeNodeCall stg_gc_fun (node : args') upd
369 370

               | otherwise
371
                 = mkJump dflags Slow stg_gc_fun (node : args') upd
372 373

       updfr_sz <- getUpdFrameOff
374 375 376

       loop_id <- newLabelC
       emitLabel loop_id
377
       heapCheck True True (gc_call updfr_sz <*> mkBranch loop_id) code
378

379 380
-- ------------------------------------------------------------
-- A heap/stack check in a case alternative
381

382 383 384 385 386 387 388 389 390 391 392 393 394 395 396

-- If there are multiple alts and we need to GC, but don't have a
-- continuation already (the scrut was simple), then we should
-- pre-generate the continuation.  (if there are multiple alts it is
-- always a canned GC point).

-- altHeapCheck:
-- If we have a return continuation,
--   then if it is a canned GC pattern,
--           then we do mkJumpReturnsTo
--           else we do a normal call to stg_gc_noregs
--   else if it is a canned GC pattern,
--           then generate the continuation and do mkCallReturnsTo
--           else we do a normal call to stg_gc_noregs

397
altHeapCheck :: [LocalReg] -> FCode a -> FCode a
398 399 400 401
altHeapCheck regs code = altOrNoEscapeHeapCheck False regs code

altOrNoEscapeHeapCheck :: Bool -> [LocalReg] -> FCode a -> FCode a
altOrNoEscapeHeapCheck checkYield regs code = do
402 403
    dflags <- getDynFlags
    case cannedGCEntryPoint dflags regs of
404
      Nothing -> genericGC checkYield code
405 406
      Just gc -> do
        lret <- newLabelC
407
        let (off, _, copyin) = copyInOflow dflags NativeReturn (Young lret) regs []
408 409 410
        lcont <- newLabelC
        emitOutOfLine lret (copyin <*> mkBranch lcont)
        emitLabel lcont
411
        cannedGCReturnsTo checkYield False gc regs lret off code
412 413 414

altHeapCheckReturnsTo :: [LocalReg] -> Label -> ByteOff -> FCode a -> FCode a
altHeapCheckReturnsTo regs lret off code
415 416
  = do dflags <- getDynFlags
       case cannedGCEntryPoint dflags regs of
417 418 419 420 421 422 423 424
           Nothing -> genericGC False code
           Just gc -> cannedGCReturnsTo False True gc regs lret off code

-- noEscapeHeapCheck is implemented identically to altHeapCheck (which
-- is more efficient), but cannot be optimized away in the non-allocating
-- case because it may occur in a loop
noEscapeHeapCheck :: [LocalReg] -> FCode a -> FCode a
noEscapeHeapCheck regs code = altOrNoEscapeHeapCheck True regs code
425

426
cannedGCReturnsTo :: Bool -> Bool -> CmmExpr -> [LocalReg] -> Label -> ByteOff
427 428
                  -> FCode a
                  -> FCode a
429
cannedGCReturnsTo checkYield cont_on_stack gc regs lret off code
430 431
  = do dflags <- getDynFlags
       updfr_sz <- getUpdFrameOff
432
       heapCheck False checkYield (gc_call dflags gc updfr_sz) code
433 434
  where
    reg_exprs = map (CmmReg . CmmLocal) regs
435
      -- Note [stg_gc arguments]
436

437 438 439 440
      -- NB. we use the NativeReturn convention for passing arguments
      -- to the canned heap-check routines, because we are in a case
      -- alternative and hence the [LocalReg] was passed to us in the
      -- NativeReturn convention.
441
    gc_call dflags label sp
442 443 444 445
      | cont_on_stack
      = mkJumpReturnsTo dflags label NativeReturn reg_exprs lret off sp
      | otherwise
      = mkCallReturnsTo dflags label NativeReturn reg_exprs lret off sp []
446

447 448
genericGC :: Bool -> FCode a -> FCode a
genericGC checkYield code
449 450 451
  = do updfr_sz <- getUpdFrameOff
       lretry <- newLabelC
       emitLabel lretry
452
       call <- mkCall generic_gc (GC, GC) [] [] updfr_sz []
453
       heapCheck False checkYield (call <*> mkBranch lretry) code
454

455 456
cannedGCEntryPoint :: DynFlags -> [LocalReg] -> Maybe CmmExpr
cannedGCEntryPoint dflags regs
457
  = case map localRegType regs of
458
      []  -> Just (mkGcLabel "stg_gc_noregs")
459
      [ty]
460 461 462 463 464
          | isGcPtrType ty -> Just (mkGcLabel "stg_gc_unpt_r1")
          | isFloatType ty -> case width of
                                  W32       -> Just (mkGcLabel "stg_gc_f1")
                                  W64       -> Just (mkGcLabel "stg_gc_d1")
                                  _         -> Nothing
465

466 467 468
          | width == wordWidth dflags -> Just (mkGcLabel "stg_gc_unbx_r1")
          | width == W64              -> Just (mkGcLabel "stg_gc_l1")
          | otherwise                 -> Nothing
469 470
          where
              width = typeWidth ty
471 472 473 474 475 476 477 478 479 480 481 482
      [ty1,ty2]
          |  isGcPtrType ty1
          && isGcPtrType ty2 -> Just (mkGcLabel "stg_gc_pp")
      [ty1,ty2,ty3]
          |  isGcPtrType ty1
          && isGcPtrType ty2
          && isGcPtrType ty3 -> Just (mkGcLabel "stg_gc_ppp")
      [ty1,ty2,ty3,ty4]
          |  isGcPtrType ty1
          && isGcPtrType ty2
          && isGcPtrType ty3
          && isGcPtrType ty4 -> Just (mkGcLabel "stg_gc_pppp")
483
      _otherwise -> Nothing
484

485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
-- Note [stg_gc arguments]
-- It might seem that we could avoid passing the arguments to the
-- stg_gc function, because they are already in the right registers.
-- While this is usually the case, it isn't always.  Sometimes the
-- code generator has cleverly avoided the eval in a case, e.g. in
-- ffi/should_run/4221.hs we found
--
--   case a_r1mb of z
--     FunPtr x y -> ...
--
-- where a_r1mb is bound a top-level constructor, and is known to be
-- evaluated.  The codegen just assigns x, y and z, and continues;
-- R1 is never assigned.
--
-- So we'll have to rely on optimisations to eliminatethese
-- assignments where possible.

502

503 504
-- | The generic GC procedure; no params, no results
generic_gc :: CmmExpr
505
generic_gc = mkGcLabel "stg_gc_noregs"
506 507

-- | Create a CLabel for calling a garbage collector entry point
508 509
mkGcLabel :: String -> CmmExpr
mkGcLabel s = CmmLit (CmmLabel (mkCmmCodeLabel rtsPackageId (fsLit s)))
510 511

-------------------------------
512 513
heapCheck :: Bool -> Bool -> CmmAGraph -> FCode a -> FCode a
heapCheck checkStack checkYield do_gc code
514
  = getHeapUsage $ \ hpHw ->
515 516
    -- Emit heap checks, but be sure to do it lazily so
    -- that the conditionals on hpHw don't cause a black hole
517 518 519 520 521 522 523
    do  { dflags <- getDynFlags
        ; let mb_alloc_bytes
                 | hpHw > 0  = Just (mkIntExpr dflags (hpHw * (wORD_SIZE dflags)))
                 | otherwise = Nothing
              stk_hwm | checkStack = Just (CmmLit CmmHighStackMark)
                      | otherwise  = Nothing
        ; codeOnly $ do_checks stk_hwm checkYield mb_alloc_bytes do_gc
nfrisby's avatar
nfrisby committed
524
        ; tickyAllocHeap True hpHw
525 526
        ; setRealHp hpHw
        ; code }
527

528 529 530 531 532 533 534 535 536
heapStackCheckGen :: Maybe CmmExpr -> Maybe CmmExpr -> FCode ()
heapStackCheckGen stk_hwm mb_bytes
  = do updfr_sz <- getUpdFrameOff
       lretry <- newLabelC
       emitLabel lretry
       call <- mkCall generic_gc (GC, GC) [] [] updfr_sz []
       do_checks stk_hwm False  mb_bytes (call <*> mkBranch lretry)

do_checks :: Maybe CmmExpr    -- Should we check the stack?
537
          -> Bool       -- Should we check for preemption?
538
          -> Maybe CmmExpr    -- Heap headroom (bytes)
539
          -> CmmAGraph  -- What to do on failure
540
          -> FCode ()
541
do_checks mb_stk_hwm checkYield mb_alloc_lit do_gc = do
542
  dflags <- getDynFlags
543 544
  gc_id <- newLabelC

545
  let
546 547 548
    Just alloc_lit = mb_alloc_lit

    bump_hp   = cmmOffsetExprB dflags (CmmReg hpReg) alloc_lit
549 550

    -- Sp overflow if (Sp - CmmHighStack < SpLim)
551 552
    sp_oflo sp_hwm =
         CmmMachOp (mo_wordULt dflags)
553
                  [CmmMachOp (MO_Sub (typeWidth (cmmRegType dflags spReg)))
554
                             [CmmReg spReg, sp_hwm],
555 556 557 558 559 560
                   CmmReg spLimReg]

    -- Hp overflow if (Hp > HpLim)
    -- (Hp has been incremented by now)
    -- HpLim points to the LAST WORD of valid allocation space.
    hp_oflo = CmmMachOp (mo_wordUGt dflags)
561
                  [CmmReg hpReg, CmmReg (CmmGlobal HpLim)]
562

563
    alloc_n = mkAssign (CmmGlobal HpAlloc) alloc_lit
564

565 566
  case mb_stk_hwm of
    Nothing -> return ()
567
    Just stk_hwm -> tickyStackCheck >> (emit =<< mkCmmIfGoto (sp_oflo stk_hwm) gc_id)
568

569
  if (isJust mb_alloc_lit)
570
    then do
571
     tickyHeapCheck
572 573
     emitAssign hpReg bump_hp
     emit =<< mkCmmIfThen hp_oflo (alloc_n <*> mkBranch gc_id)
574
    else do
ian@well-typed.com's avatar
ian@well-typed.com committed
575
      when (not (gopt Opt_OmitYields dflags) && checkYield) $ do
576 577 578 579 580
         -- Yielding if HpLim == 0
         let yielding = CmmMachOp (mo_wordEq dflags)
                                  [CmmReg (CmmGlobal HpLim),
                                   CmmLit (zeroCLit dflags)]
         emit =<< mkCmmIfGoto yielding gc_id
581 582

  emitOutOfLine gc_id $
583 584
     do_gc -- this is expected to jump back somewhere

585 586 587 588 589 590
                -- Test for stack pointer exhaustion, then
                -- bump heap pointer, and test for heap exhaustion
                -- Note that we don't move the heap pointer unless the
                -- stack check succeeds.  Otherwise we might end up
                -- with slop at the end of the current block, which can
                -- confuse the LDV profiler.