StgCmmHeap.hs 20.7 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 31 32 33 34 35 36
    ) where

#include "HsVersions.h"

import StgSyn
import CLabel
import StgCmmLayout
import StgCmmUtils
import StgCmmMonad
import StgCmmProf
import StgCmmTicky
import StgCmmGran
import StgCmmClosure
import StgCmmEnv

37
import MkGraph
38

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

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

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

allocDynClosure
58 59
        :: Maybe Id
        -> CmmInfoTable
Simon Marlow's avatar
Simon Marlow committed
60
        -> LambdaFormInfo
61 62 63 64 65 66 67
        -> 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
68
        -> FCode CmmExpr -- returns Hp+n
69

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

75
-- allocDynClosure allocates the thing in the heap,
76
-- and modifies the virtual Hp to account for this.
77 78 79
-- 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.
80

81 82 83 84 85 86 87 88 89 90
-- 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.
91 92


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

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

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

        -- 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
115
                info_ptr = CmmLit (CmmLabel (cit_lbl info_tbl))
116 117 118

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

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

128 129
        ; getHpRelOffset info_offset
        }
130 131

emitSetDynHdr :: CmmExpr -> CmmExpr -> CmmExpr -> FCode ()
132
emitSetDynHdr base info_ptr ccs
133 134
  = do dflags <- getDynFlags
       hpStore base (header dflags) [0..]
135
  where
136 137
    header :: DynFlags -> [CmmExpr]
    header dflags = [info_ptr] ++ dynProfHdr dflags ccs
138 139 140
        -- ToDo: Gransim stuff
        -- ToDo: Parallel stuff
        -- No ticky header
141 142 143 144

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


-----------------------------------------------------------
151
--              Layout of static closures
152 153 154 155 156
-----------------------------------------------------------

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

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

    -- 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
178 179 180
    -- the static_link and saved_info fields must always be in the
    -- same place.  So we use isThunkRep rather than closureUpdReqd
    -- here:
181

Simon Marlow's avatar
Simon Marlow committed
182
    is_caf = isThunkRep (cit_rep info_tbl)
183

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

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

    saved_info_field
195
        | is_caf     = [mkIntCLit dflags 0]
196
        | otherwise  = []
197

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


206
mkStaticClosure :: DynFlags -> CLabel -> CostCentreStack -> [CmmLit]
207
  -> [CmmLit] -> [CmmLit] -> [CmmLit] -> [CmmLit]
208
mkStaticClosure dflags info_lbl ccs payload padding static_link_field saved_info_field
209 210
  =  [CmmLabel info_lbl]
  ++ variable_header_words
211
  ++ concatMap (padLitToWord dflags) payload
212
  ++ padding
213 214 215 216
  ++ static_link_field
  ++ saved_info_field
  where
    variable_header_words
217 218
        =  staticGranHdr
        ++ staticParHdr
219
        ++ staticProfHdr dflags ccs
220

221 222
-- 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?
223 224 225
padLitToWord :: DynFlags -> CmmLit -> [CmmLit]
padLitToWord dflags lit = lit : padding pad_length
  where width = typeWidth (cmmLitType dflags lit)
226
        pad_length = wORD_SIZE dflags - widthInBytes width :: Int
227 228 229 230 231 232 233

        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)

234
-----------------------------------------------------------
235
--              Heap overflow checking
236 237 238 239 240 241 242 243 244 245 246 247
-----------------------------------------------------------

{- 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
248
        r = gc_1p( r )
249 250
    where r is a pointer.  This performs gc, and
    then returns its argument r to its caller.
251

252
  * A series of canned entry points like
253
        gcfun_2p( f, x, y )
254 255 256 257 258 259 260 261 262
    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 ) {
263 264 265
             Hp = Hp+8
             if Hp > HpLim goto L
             ...
266 267 268
          L: HpAlloc = 8
             jump gcfun_2p( f_clo, x, y ) }
     Note the tail call to the garbage collector;
269
     it should do no register shuffling
270 271 272

    (b) No canned sequence
        f( f_clo, x:ptr, y:ptr, ...etc... ) {
273 274 275
          T: Hp = Hp+8
             if Hp > HpLim goto L
             ...
276
          L: HpAlloc = 8
277 278
             call gc()  -- Needs an info table
             goto T }
279 280

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

293 294
        L: r = gc_1p( r )
           goto K }
295

296 297 298 299
        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)
300 301 302

   (b) No canned sequence for results of f
       Note second info table
303 304 305 306 307 308
           ...
           (r1,r2,r3) = call f( x, y )
        K:
           Hp = Hp+8
           if Hp > HpLim goto L
           ...code for rhs...
309

310 311
        L: call gc()    -- Extra info table here
           goto K
312 313 314

* generalHeapCheck: Anywhere else
  e.g. entry to thunk
315
       case branch *not* following eval,
316 317 318
       or let-no-escape
  Exactly the same as the previous case:

319 320 321 322
        K:      -- K needs an info table
           Hp = Hp+8
           if Hp > HpLim goto L
           ...
323

324 325
        L: call gc()
           goto K
326 327 328 329 330
-}

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

331 332 333 334 335 336
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 ()
337

338
entryHeapCheck cl_info nodeSet arity args code
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
  = 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
357 358
  = do dflags <- getDynFlags
       let is_thunk = arity == 0
359 360

           args' = map (CmmReg . CmmLocal) args
361 362 363 364 365 366 367
           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)

368
              Function (slow): call (slow) stg_gc_fun(fun, args)
369 370 371
           -}
           gc_call upd
               | is_thunk
372
                 = mkJump dflags NativeNodeCall stg_gc_enter1 [node] upd
373 374

               | is_fastf
375
                 = mkJump dflags NativeNodeCall stg_gc_fun (node : args') upd
376 377

               | otherwise
378
                 = mkJump dflags Slow stg_gc_fun (node : args') upd
379 380

       updfr_sz <- getUpdFrameOff
381 382 383

       loop_id <- newLabelC
       emitLabel loop_id
384
       heapCheck True True (gc_call updfr_sz <*> mkBranch loop_id) code
385

386 387
-- ------------------------------------------------------------
-- A heap/stack check in a case alternative
388

389 390 391 392 393 394 395 396 397 398 399 400 401 402 403

-- 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

404
altHeapCheck :: [LocalReg] -> FCode a -> FCode a
405 406 407 408
altHeapCheck regs code = altOrNoEscapeHeapCheck False regs code

altOrNoEscapeHeapCheck :: Bool -> [LocalReg] -> FCode a -> FCode a
altOrNoEscapeHeapCheck checkYield regs code = do
409 410
    dflags <- getDynFlags
    case cannedGCEntryPoint dflags regs of
411
      Nothing -> genericGC checkYield code
412 413
      Just gc -> do
        lret <- newLabelC
414
        let (off, _, copyin) = copyInOflow dflags NativeReturn (Young lret) regs []
415 416 417
        lcont <- newLabelC
        emitOutOfLine lret (copyin <*> mkBranch lcont)
        emitLabel lcont
418
        cannedGCReturnsTo checkYield False gc regs lret off code
419 420 421

altHeapCheckReturnsTo :: [LocalReg] -> Label -> ByteOff -> FCode a -> FCode a
altHeapCheckReturnsTo regs lret off code
422 423
  = do dflags <- getDynFlags
       case cannedGCEntryPoint dflags regs of
424 425 426 427 428 429 430 431
           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
432

433
cannedGCReturnsTo :: Bool -> Bool -> CmmExpr -> [LocalReg] -> Label -> ByteOff
434 435
                  -> FCode a
                  -> FCode a
436
cannedGCReturnsTo checkYield cont_on_stack gc regs lret off code
437 438
  = do dflags <- getDynFlags
       updfr_sz <- getUpdFrameOff
439
       heapCheck False checkYield (gc_call dflags gc updfr_sz) code
440 441
  where
    reg_exprs = map (CmmReg . CmmLocal) regs
442
      -- Note [stg_gc arguments]
443

444 445 446 447
      -- 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.
448
    gc_call dflags label sp
449 450 451 452
      | cont_on_stack
      = mkJumpReturnsTo dflags label NativeReturn reg_exprs lret off sp
      | otherwise
      = mkCallReturnsTo dflags label NativeReturn reg_exprs lret off sp []
453

454 455
genericGC :: Bool -> FCode a -> FCode a
genericGC checkYield code
456 457 458
  = do updfr_sz <- getUpdFrameOff
       lretry <- newLabelC
       emitLabel lretry
459
       call <- mkCall generic_gc (GC, GC) [] [] updfr_sz []
460
       heapCheck False checkYield (call <*> mkBranch lretry) code
461

462 463
cannedGCEntryPoint :: DynFlags -> [LocalReg] -> Maybe CmmExpr
cannedGCEntryPoint dflags regs
464
  = case map localRegType regs of
465
      []  -> Just (mkGcLabel "stg_gc_noregs")
466
      [ty]
467 468 469 470 471 472
          | 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
        
473 474 475
          | width == wordWidth dflags -> Just (mkGcLabel "stg_gc_unbx_r1")
          | width == W64              -> Just (mkGcLabel "stg_gc_l1")
          | otherwise                 -> Nothing
476 477
          where
              width = typeWidth ty
478 479 480 481 482 483 484 485 486 487 488 489
      [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")
490
      _otherwise -> Nothing
491

492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
-- 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.

509

510 511
-- | The generic GC procedure; no params, no results
generic_gc :: CmmExpr
512
generic_gc = mkGcLabel "stg_gc_noregs"
513 514

-- | Create a CLabel for calling a garbage collector entry point
515 516
mkGcLabel :: String -> CmmExpr
mkGcLabel s = CmmLit (CmmLabel (mkCmmCodeLabel rtsPackageId (fsLit s)))
517 518

-------------------------------
519 520
heapCheck :: Bool -> Bool -> CmmAGraph -> FCode a -> FCode a
heapCheck checkStack checkYield do_gc code
521
  = getHeapUsage $ \ hpHw ->
522 523
    -- Emit heap checks, but be sure to do it lazily so
    -- that the conditionals on hpHw don't cause a black hole
524 525 526 527 528 529 530
    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
531
        ; tickyAllocHeap True hpHw
532 533 534
        ; doGranAllocate hpHw
        ; setRealHp hpHw
        ; code }
535

536 537 538 539 540 541 542 543 544
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?
545
          -> Bool       -- Should we check for preemption?
546
          -> Maybe CmmExpr    -- Heap headroom (bytes)
547
          -> CmmAGraph  -- What to do on failure
548
          -> FCode ()
549
do_checks mb_stk_hwm checkYield mb_alloc_lit do_gc = do
550
  dflags <- getDynFlags
551 552
  gc_id <- newLabelC

553
  let
554 555 556
    Just alloc_lit = mb_alloc_lit

    bump_hp   = cmmOffsetExprB dflags (CmmReg hpReg) alloc_lit
557 558

    -- Sp overflow if (Sp - CmmHighStack < SpLim)
559 560
    sp_oflo sp_hwm =
         CmmMachOp (mo_wordULt dflags)
561
                  [CmmMachOp (MO_Sub (typeWidth (cmmRegType dflags spReg)))
562
                             [CmmReg spReg, sp_hwm],
563 564 565 566 567 568
                   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)
569
                  [CmmReg hpReg, CmmReg (CmmGlobal HpLim)]
570

571
    alloc_n = mkAssign (CmmGlobal HpAlloc) alloc_lit
572

573 574
  case mb_stk_hwm of
    Nothing -> return ()
575
    Just stk_hwm -> tickyStackCheck >> (emit =<< mkCmmIfGoto (sp_oflo stk_hwm) gc_id)
576

577
  if (isJust mb_alloc_lit)
578
    then do
579
     tickyHeapCheck
580 581
     emitAssign hpReg bump_hp
     emit =<< mkCmmIfThen hp_oflo (alloc_n <*> mkBranch gc_id)
582
    else do
ian@well-typed.com's avatar
ian@well-typed.com committed
583
      when (not (gopt Opt_OmitYields dflags) && checkYield) $ do
584 585 586 587 588
         -- Yielding if HpLim == 0
         let yielding = CmmMachOp (mo_wordEq dflags)
                                  [CmmReg (CmmGlobal HpLim),
                                   CmmLit (zeroCLit dflags)]
         emit =<< mkCmmIfGoto yielding gc_id
589 590

  emitOutOfLine gc_id $
591 592
     do_gc -- this is expected to jump back somewhere

593 594 595 596 597 598
                -- 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.