1 -- -----------------------------------------------------------------------------
3 -- (c) The University of Glasgow 1993-2004
5 -- This is the top-level module in the native code generator.
7 -- -----------------------------------------------------------------------------
10 module AsmCodeGen ( nativeCodeGen ) where
12 #include "HsVersions.h"
13 #include "nativeGen/NCG.h"
22 #elif i386_TARGET_ARCH || x86_64_TARGET_ARCH
28 #elif sparc_TARGET_ARCH
30 import SPARC.CodeGen.Expand
34 import SPARC.ShortcutJump
36 #elif powerpc_TARGET_ARCH
45 #error "AsmCodeGen: unknown architecture"
49 import RegAlloc.Liveness
50 import qualified RegAlloc.Linear.Main as Linear
52 import qualified GraphColor as Color
53 import qualified RegAlloc.Graph.Main as Color
54 import qualified RegAlloc.Graph.Stats as Color
55 import qualified RegAlloc.Graph.TrivColorable as Color
65 import CgUtils ( fixStgRegisters )
67 import CmmOpt ( cmmMiniInline, cmmMachOpFold )
72 import Unique ( Unique, getUnique )
75 #if powerpc_TARGET_ARCH
76 import StaticFlags ( opt_Static, opt_PIC )
79 #if !defined(darwin_TARGET_OS)
80 import Config ( cProjectVersion )
84 import qualified Pretty
100 The native-code generator has machine-independent and
101 machine-dependent modules.
103 This module ("AsmCodeGen") is the top-level machine-independent
104 module. Before entering machine-dependent land, we do some
105 machine-independent optimisations (defined below) on the
108 We convert to the machine-specific 'Instr' datatype with
109 'cmmCodeGen', assuming an infinite supply of registers. We then use
110 a machine-independent register allocator ('regAlloc') to rejoin
111 reality. Obviously, 'regAlloc' has machine-specific helper
112 functions (see about "RegAllocInfo" below).
114 Finally, we order the basic blocks of the function so as to minimise
115 the number of jumps between blocks, by utilising fallthrough wherever
118 The machine-dependent bits break down as follows:
120 * ["MachRegs"] Everything about the target platform's machine
121 registers (and immediate operands, and addresses, which tend to
122 intermingle/interact with registers).
124 * ["MachInstrs"] Includes the 'Instr' datatype (possibly should
125 have a module of its own), plus a miscellany of other things
126 (e.g., 'targetDoubleSize', 'smStablePtrTable', ...)
128 * ["MachCodeGen"] is where 'Cmm' stuff turns into
129 machine instructions.
131 * ["PprMach"] 'pprInstr' turns an 'Instr' into text (well, really
134 * ["RegAllocInfo"] In the register allocator, we manipulate
135 'MRegsState's, which are 'BitSet's, one bit per machine register.
136 When we want to say something about a specific machine register
137 (e.g., ``it gets clobbered by this instruction''), we set/unset
138 its bit. Obviously, we do this 'BitSet' thing for efficiency
141 The 'RegAllocInfo' module collects together the machine-specific
142 info needed to do register allocation.
144 * ["RegisterAlloc"] The (machine-independent) register allocator.
147 -- -----------------------------------------------------------------------------
148 -- Top-level of the native codegen
151 nativeCodeGen :: DynFlags -> Handle -> UniqSupply -> [RawCmm] -> IO ()
152 nativeCodeGen dflags h us cmms
154 let split_cmms = concat $ map add_split cmms
156 -- BufHandle is a performance hack. We could hide it inside
157 -- Pretty if it weren't for the fact that we do lots of little
158 -- printDocs here (in order to do codegen in constant space).
159 bufh <- newBufHandle h
160 (imports, prof) <- cmmNativeGens dflags bufh us split_cmms [] [] 0
163 let (native, colorStats, linearStats)
168 Opt_D_dump_asm "Asm code"
169 (vcat $ map (docToSDoc . pprNatCmmTop) $ concat native)
171 -- dump global NCG stats for graph coloring allocator
172 (case concat $ catMaybes colorStats of
175 -- build the global register conflict graph
177 = foldl Color.union Color.initGraph
178 $ [ Color.raGraph stat
179 | stat@Color.RegAllocStatsStart{} <- stats]
181 dumpSDoc dflags Opt_D_dump_asm_stats "NCG stats"
182 $ Color.pprStats stats graphGlobal
185 Opt_D_dump_asm_conflicts "Register conflict graph"
189 targetVirtualRegSqueeze
190 targetRealRegSqueeze)
194 -- dump global NCG stats for linear allocator
195 (case concat $ catMaybes linearStats of
197 stats -> dumpSDoc dflags Opt_D_dump_asm_stats "NCG stats"
198 $ Linear.pprStats (concat native) stats)
200 -- write out the imports
201 Pretty.printDoc Pretty.LeftMode h
202 $ makeImportsDoc dflags (concat imports)
206 where add_split (Cmm tops)
207 | dopt Opt_SplitObjs dflags = split_marker : tops
210 split_marker = CmmProc [] mkSplitMarkerLabel [] (ListGraph [])
213 -- | Do native code generation on all these cmms.
215 cmmNativeGens :: DynFlags
220 -> [ ([NatCmmTop Instr],
221 Maybe [Color.RegAllocStats Instr],
222 Maybe [Linear.RegAllocStats]) ]
226 Maybe [Color.RegAllocStats Instr],
227 Maybe [Linear.RegAllocStats])] )
229 cmmNativeGens _ _ _ [] impAcc profAcc _
230 = return (reverse impAcc, reverse profAcc)
232 cmmNativeGens dflags h us (cmm : cmms) impAcc profAcc count
234 (us', native, imports, colorStats, linearStats)
235 <- cmmNativeGen dflags us cmm count
237 Pretty.bufLeftRender h
238 $ {-# SCC "pprNativeCode" #-} Pretty.vcat $ map pprNatCmmTop native
240 -- carefully evaluate this strictly. Binding it with 'let'
241 -- and then using 'seq' doesn't work, because the let
242 -- apparently gets inlined first.
243 lsPprNative <- return $!
244 if dopt Opt_D_dump_asm dflags
245 || dopt Opt_D_dump_asm_stats dflags
249 count' <- return $! count + 1;
251 -- force evaulation all this stuff to avoid space leaks
252 seqString (showSDoc $ vcat $ map ppr imports) `seq` return ()
254 cmmNativeGens dflags h us' cmms
256 ((lsPprNative, colorStats, linearStats) : profAcc)
259 where seqString [] = ()
260 seqString (x:xs) = x `seq` seqString xs `seq` ()
263 -- | Complete native code generation phase for a single top-level chunk of Cmm.
264 -- Dumping the output of each stage along the way.
265 -- Global conflict graph and NGC stats
269 -> RawCmmTop -- ^ the cmm to generate code for
270 -> Int -- ^ sequence number of this top thing
272 , [NatCmmTop Instr] -- native code
273 , [CLabel] -- things imported by this cmm
274 , Maybe [Color.RegAllocStats Instr] -- stats for the coloring register allocator
275 , Maybe [Linear.RegAllocStats]) -- stats for the linear register allocators
277 cmmNativeGen dflags us cmm count
280 -- rewrite assignments to global regs
282 {-# SCC "fixStgRegisters" #-}
285 -- cmm to cmm optimisations
286 let (opt_cmm, imports) =
287 {-# SCC "cmmToCmm" #-}
288 cmmToCmm dflags fixed_cmm
291 Opt_D_dump_opt_cmm "Optimised Cmm"
292 (pprCmm $ Cmm [opt_cmm])
294 -- generate native code from cmm
295 let ((native, lastMinuteImports), usGen) =
296 {-# SCC "genMachCode" #-}
297 initUs us $ genMachCode dflags opt_cmm
300 Opt_D_dump_asm_native "Native code"
301 (vcat $ map (docToSDoc . pprNatCmmTop) native)
303 -- tag instructions with register liveness information
304 let (withLiveness, usLive) =
305 {-# SCC "regLiveness" #-}
308 $ map natCmmTopToLive native
311 Opt_D_dump_asm_liveness "Liveness annotations added"
312 (vcat $ map ppr withLiveness)
314 -- allocate registers
315 (alloced, usAlloc, ppr_raStatsColor, ppr_raStatsLinear) <-
316 if ( dopt Opt_RegsGraph dflags
317 || dopt Opt_RegsIterative dflags)
319 -- the regs usable for allocation
320 let (alloc_regs :: UniqFM (UniqSet RealReg))
321 = foldr (\r -> plusUFM_C unionUniqSets
322 $ unitUFM (targetClassOfRealReg r) (unitUniqSet r))
326 -- do the graph coloring register allocation
327 let ((alloced, regAllocStats), usAlloc)
328 = {-# SCC "RegAlloc" #-}
333 (mkUniqSet [0..maxSpillSlots])
336 -- dump out what happened during register allocation
338 Opt_D_dump_asm_regalloc "Registers allocated"
339 (vcat $ map (docToSDoc . pprNatCmmTop) alloced)
342 Opt_D_dump_asm_regalloc_stages "Build/spill stages"
343 (vcat $ map (\(stage, stats)
344 -> text "# --------------------------"
345 $$ text "# cmm " <> int count <> text " Stage " <> int stage
347 $ zip [0..] regAllocStats)
350 if dopt Opt_D_dump_asm_stats dflags
351 then Just regAllocStats else Nothing
353 -- force evaluation of the Maybe to avoid space leak
354 mPprStats `seq` return ()
356 return ( alloced, usAlloc
361 -- do linear register allocation
362 let ((alloced, regAllocStats), usAlloc)
363 = {-# SCC "RegAlloc" #-}
366 $ mapUs Linear.regAlloc withLiveness
369 Opt_D_dump_asm_regalloc "Registers allocated"
370 (vcat $ map (docToSDoc . pprNatCmmTop) alloced)
373 if dopt Opt_D_dump_asm_stats dflags
374 then Just (catMaybes regAllocStats) else Nothing
376 -- force evaluation of the Maybe to avoid space leak
377 mPprStats `seq` return ()
379 return ( alloced, usAlloc
383 ---- shortcut branches
385 {-# SCC "shortcutBranches" #-}
386 shortcutBranches dflags alloced
390 {-# SCC "sequenceBlocks" #-}
391 map sequenceTop shorted
396 {-# SCC "x86fp_kludge" #-}
397 map x86fp_kludge sequenced
402 ---- expansion of SPARC synthetic instrs
403 #if sparc_TARGET_ARCH
405 {-# SCC "sparc_expand" #-}
406 map expandTop kludged
409 Opt_D_dump_asm_expanded "Synthetic instructions expanded"
410 (vcat $ map (docToSDoc . pprNatCmmTop) expanded)
418 , lastMinuteImports ++ imports
424 x86fp_kludge :: NatCmmTop Instr -> NatCmmTop Instr
425 x86fp_kludge top@(CmmData _ _) = top
426 x86fp_kludge (CmmProc info lbl params (ListGraph code)) =
427 CmmProc info lbl params (ListGraph $ i386_insert_ffrees code)
431 -- | Build a doc for all the imports.
433 makeImportsDoc :: DynFlags -> [CLabel] -> Pretty.Doc
434 makeImportsDoc dflags imports
437 #if HAVE_SUBSECTIONS_VIA_SYMBOLS
438 -- On recent versions of Darwin, the linker supports
439 -- dead-stripping of code and data on a per-symbol basis.
440 -- There's a hack to make this work in PprMach.pprNatCmmTop.
441 Pretty.$$ Pretty.text ".subsections_via_symbols"
443 #if HAVE_GNU_NONEXEC_STACK
444 -- On recent GNU ELF systems one can mark an object file
445 -- as not requiring an executable stack. If all objects
446 -- linked into a program have this note then the program
447 -- will not use an executable stack, which is good for
448 -- security. GHC generated code does not need an executable
449 -- stack so add the note in:
450 Pretty.$$ Pretty.text ".section .note.GNU-stack,\"\",@progbits"
452 #if !defined(darwin_TARGET_OS)
453 -- And just because every other compiler does, lets stick in
454 -- an identifier directive: .ident "GHC x.y.z"
455 Pretty.$$ let compilerIdent = Pretty.text "GHC" Pretty.<+>
456 Pretty.text cProjectVersion
457 in Pretty.text ".ident" Pretty.<+>
458 Pretty.doubleQuotes compilerIdent
462 -- Generate "symbol stubs" for all external symbols that might
463 -- come from a dynamic library.
464 dyld_stubs :: [CLabel] -> Pretty.Doc
465 {- dyld_stubs imps = Pretty.vcat $ map pprDyldSymbolStub $
466 map head $ group $ sort imps-}
468 arch = platformArch $ targetPlatform dflags
469 os = platformOS $ targetPlatform dflags
471 -- (Hack) sometimes two Labels pretty-print the same, but have
472 -- different uniques; so we compare their text versions...
474 | needImportedSymbols arch os
476 (pprGotDeclaration arch os :) $
477 map ( pprImportedSymbol arch os . fst . head) $
478 groupBy (\(_,a) (_,b) -> a == b) $
479 sortBy (\(_,a) (_,b) -> compare a b) $
485 doPpr lbl = (lbl, Pretty.render $ pprCLabel lbl astyle)
486 astyle = mkCodeStyle AsmStyle
489 -- -----------------------------------------------------------------------------
490 -- Sequencing the basic blocks
492 -- Cmm BasicBlocks are self-contained entities: they always end in a
493 -- jump, either non-local or to another basic block in the same proc.
494 -- In this phase, we attempt to place the basic blocks in a sequence
495 -- such that as many of the local jumps as possible turn into
502 sequenceTop top@(CmmData _ _) = top
503 sequenceTop (CmmProc info lbl params (ListGraph blocks)) =
504 CmmProc info lbl params (ListGraph $ makeFarBranches $ sequenceBlocks blocks)
506 -- The algorithm is very simple (and stupid): we make a graph out of
507 -- the blocks where there is an edge from one block to another iff the
508 -- first block ends by jumping to the second. Then we topologically
509 -- sort this graph. Then traverse the list: for each block, we first
510 -- output the block, then if it has an out edge, we move the
511 -- destination of the out edge to the front of the list, and continue.
513 -- FYI, the classic layout for basic blocks uses postorder DFS; this
514 -- algorithm is implemented in cmm/ZipCfg.hs (NR 6 Sep 2007).
518 => [NatBasicBlock instr]
519 -> [NatBasicBlock instr]
521 sequenceBlocks [] = []
522 sequenceBlocks (entry:blocks) =
523 seqBlocks (mkNode entry : reverse (flattenSCCs (sccBlocks blocks)))
524 -- the first block is the entry point ==> it must remain at the start.
529 => [NatBasicBlock instr]
530 -> [SCC ( NatBasicBlock instr
534 sccBlocks blocks = stronglyConnCompFromEdgedVerticesR (map mkNode blocks)
536 -- we're only interested in the last instruction of
537 -- the block, and only if it has a single destination.
540 => [instr] -> [Unique]
543 = case jumpDestsOfInstr (last instrs) of
544 [one] -> [getUnique one]
547 mkNode :: (Instruction t)
549 -> (GenBasicBlock t, Unique, [Unique])
550 mkNode block@(BasicBlock id instrs) = (block, getUnique id, getOutEdges instrs)
552 seqBlocks :: (Eq t) => [(GenBasicBlock t1, t, [t])] -> [GenBasicBlock t1]
554 seqBlocks ((block,_,[]) : rest)
555 = block : seqBlocks rest
556 seqBlocks ((block@(BasicBlock id instrs),_,[next]) : rest)
557 | can_fallthrough = BasicBlock id (init instrs) : seqBlocks rest'
558 | otherwise = block : seqBlocks rest'
560 (can_fallthrough, rest') = reorder next [] rest
561 -- TODO: we should do a better job for cycles; try to maximise the
562 -- fallthroughs within a loop.
563 seqBlocks _ = panic "AsmCodegen:seqBlocks"
565 reorder :: (Eq a) => a -> [(t, a, t1)] -> [(t, a, t1)] -> (Bool, [(t, a, t1)])
566 reorder _ accum [] = (False, reverse accum)
567 reorder id accum (b@(block,id',out) : rest)
568 | id == id' = (True, (block,id,out) : reverse accum ++ rest)
569 | otherwise = reorder id (b:accum) rest
572 -- -----------------------------------------------------------------------------
573 -- Making far branches
575 -- Conditional branches on PowerPC are limited to +-32KB; if our Procs get too
576 -- big, we have to work around this limitation.
579 :: [NatBasicBlock Instr]
580 -> [NatBasicBlock Instr]
582 #if powerpc_TARGET_ARCH
583 makeFarBranches blocks
584 | last blockAddresses < nearLimit = blocks
585 | otherwise = zipWith handleBlock blockAddresses blocks
587 blockAddresses = scanl (+) 0 $ map blockLen blocks
588 blockLen (BasicBlock _ instrs) = length instrs
590 handleBlock addr (BasicBlock id instrs)
591 = BasicBlock id (zipWith makeFar [addr..] instrs)
593 makeFar addr (BCC ALWAYS tgt) = BCC ALWAYS tgt
594 makeFar addr (BCC cond tgt)
595 | abs (addr - targetAddr) >= nearLimit
599 where Just targetAddr = lookupUFM blockAddressMap tgt
600 makeFar addr other = other
602 nearLimit = 7000 -- 8192 instructions are allowed; let's keep some
603 -- distance, as we have a few pseudo-insns that are
604 -- pretty-printed as multiple instructions,
605 -- and it's just not worth the effort to calculate
608 blockAddressMap = listToUFM $ zip (map blockId blocks) blockAddresses
613 -- -----------------------------------------------------------------------------
621 shortcutBranches dflags tops
622 | optLevel dflags < 1 = tops -- only with -O or higher
623 | otherwise = map (apply_mapping mapping) tops'
625 (tops', mappings) = mapAndUnzip build_mapping tops
626 mapping = foldr plusUFM emptyUFM mappings
628 build_mapping :: GenCmmTop d t (ListGraph Instr)
629 -> (GenCmmTop d t (ListGraph Instr), UniqFM JumpDest)
630 build_mapping top@(CmmData _ _) = (top, emptyUFM)
631 build_mapping (CmmProc info lbl params (ListGraph []))
632 = (CmmProc info lbl params (ListGraph []), emptyUFM)
633 build_mapping (CmmProc info lbl params (ListGraph (head:blocks)))
634 = (CmmProc info lbl params (ListGraph (head:others)), mapping)
635 -- drop the shorted blocks, but don't ever drop the first one,
636 -- because it is pointed to by a global label.
638 -- find all the blocks that just consist of a jump that can be
640 -- Don't completely eliminate loops here -- that can leave a dangling jump!
641 (_, shortcut_blocks, others) = foldl split (emptyBlockSet, [], []) blocks
642 split (s, shortcut_blocks, others) b@(BasicBlock id [insn])
643 | Just (DestBlockId dest) <- canShortcut insn,
644 (elemBlockSet dest s) || dest == id -- loop checks
645 = (s, shortcut_blocks, b : others)
646 split (s, shortcut_blocks, others) (BasicBlock id [insn])
647 | Just dest <- canShortcut insn
648 = (extendBlockSet s id, (id,dest) : shortcut_blocks, others)
649 split (s, shortcut_blocks, others) other = (s, shortcut_blocks, other : others)
652 -- build a mapping from BlockId to JumpDest for shorting branches
653 mapping = foldl add emptyUFM shortcut_blocks
654 add ufm (id,dest) = addToUFM ufm id dest
656 apply_mapping :: UniqFM JumpDest
657 -> GenCmmTop CmmStatic h (ListGraph Instr)
658 -> GenCmmTop CmmStatic h (ListGraph Instr)
659 apply_mapping ufm (CmmData sec statics)
660 = CmmData sec (map (shortcutStatic (lookupUFM ufm)) statics)
661 -- we need to get the jump tables, so apply the mapping to the entries
663 apply_mapping ufm (CmmProc info lbl params (ListGraph blocks))
664 = CmmProc info lbl params (ListGraph $ map short_bb blocks)
666 short_bb (BasicBlock id insns) = BasicBlock id $! map short_insn insns
667 short_insn i = shortcutJump (lookupUFM ufm) i
668 -- shortcutJump should apply the mapping repeatedly,
669 -- just in case we can short multiple branches.
671 -- -----------------------------------------------------------------------------
672 -- Instruction selection
674 -- Native code instruction selection for a chunk of stix code. For
675 -- this part of the computation, we switch from the UniqSM monad to
676 -- the NatM monad. The latter carries not only a Unique, but also an
677 -- Int denoting the current C stack pointer offset in the generated
678 -- code; this is needed for creating correct spill offsets on
679 -- architectures which don't offer, or for which it would be
680 -- prohibitively expensive to employ, a frame pointer register. Viz,
683 -- The offset is measured in bytes, and indicates the difference
684 -- between the current (simulated) C stack-ptr and the value it was at
685 -- the beginning of the block. For stacks which grow down, this value
686 -- should be either zero or negative.
688 -- Switching between the two monads whilst carrying along the same
689 -- Unique supply breaks abstraction. Is that bad?
698 genMachCode dflags cmm_top
699 = do { initial_us <- getUs
700 ; let initial_st = mkNatM_State initial_us 0 dflags
701 (new_tops, final_st) = initNat initial_st (cmmTopCodeGen dflags cmm_top)
702 final_delta = natm_delta final_st
703 final_imports = natm_imports final_st
704 ; if final_delta == 0
705 then return (new_tops, final_imports)
706 else pprPanic "genMachCode: nonzero final delta" (int final_delta)
710 -- -----------------------------------------------------------------------------
711 -- Generic Cmm optimiser
717 (b) Simple inlining: a temporary which is assigned to and then
718 used, once, can be shorted.
719 (c) Position independent code and dynamic linking
720 (i) introduce the appropriate indirections
721 and position independent refs
722 (ii) compile a list of imported symbols
724 Ideas for other things we could do (ToDo):
726 - shortcut jumps-to-jumps
727 - eliminate dead code blocks
728 - simple CSE: if an expr is assigned to a temp, then replace later occs of
729 that expr with the temp, until the expr is no longer valid (can push through
730 temp assignments, and certain assigns to mem...)
733 cmmToCmm :: DynFlags -> RawCmmTop -> (RawCmmTop, [CLabel])
734 cmmToCmm _ top@(CmmData _ _) = (top, [])
735 cmmToCmm dflags (CmmProc info lbl params (ListGraph blocks)) = runCmmOpt dflags $ do
736 blocks' <- mapM cmmBlockConFold (cmmMiniInline blocks)
737 return $ CmmProc info lbl params (ListGraph blocks')
739 newtype CmmOptM a = CmmOptM (([CLabel], DynFlags) -> (# a, [CLabel] #))
741 instance Monad CmmOptM where
742 return x = CmmOptM $ \(imports, _) -> (# x,imports #)
744 CmmOptM $ \(imports, dflags) ->
745 case f (imports, dflags) of
748 CmmOptM g' -> g' (imports', dflags)
750 addImportCmmOpt :: CLabel -> CmmOptM ()
751 addImportCmmOpt lbl = CmmOptM $ \(imports, _dflags) -> (# (), lbl:imports #)
753 getDynFlagsCmmOpt :: CmmOptM DynFlags
754 getDynFlagsCmmOpt = CmmOptM $ \(imports, dflags) -> (# dflags, imports #)
756 runCmmOpt :: DynFlags -> CmmOptM a -> (a, [CLabel])
757 runCmmOpt dflags (CmmOptM f) = case f ([], dflags) of
758 (# result, imports #) -> (result, imports)
760 cmmBlockConFold :: CmmBasicBlock -> CmmOptM CmmBasicBlock
761 cmmBlockConFold (BasicBlock id stmts) = do
762 stmts' <- mapM cmmStmtConFold stmts
763 return $ BasicBlock id stmts'
765 cmmStmtConFold :: CmmStmt -> CmmOptM CmmStmt
769 -> do src' <- cmmExprConFold DataReference src
770 return $ case src' of
771 CmmReg reg' | reg == reg' -> CmmNop
772 new_src -> CmmAssign reg new_src
775 -> do addr' <- cmmExprConFold DataReference addr
776 src' <- cmmExprConFold DataReference src
777 return $ CmmStore addr' src'
780 -> do addr' <- cmmExprConFold JumpReference addr
781 return $ CmmJump addr' regs
783 CmmCall target regs args srt returns
784 -> do target' <- case target of
785 CmmCallee e conv -> do
786 e' <- cmmExprConFold CallReference e
787 return $ CmmCallee e' conv
788 other -> return other
789 args' <- mapM (\(CmmHinted arg hint) -> do
790 arg' <- cmmExprConFold DataReference arg
791 return (CmmHinted arg' hint)) args
792 return $ CmmCall target' regs args' srt returns
794 CmmCondBranch test dest
795 -> do test' <- cmmExprConFold DataReference test
796 return $ case test' of
797 CmmLit (CmmInt 0 _) ->
798 CmmComment (mkFastString ("deleted: " ++
799 showSDoc (pprStmt stmt)))
801 CmmLit (CmmInt _ _) -> CmmBranch dest
802 _other -> CmmCondBranch test' dest
805 -> do expr' <- cmmExprConFold DataReference expr
806 return $ CmmSwitch expr' ids
812 cmmExprConFold :: ReferenceKind -> CmmExpr -> CmmOptM CmmExpr
813 cmmExprConFold referenceKind expr
816 -> do addr' <- cmmExprConFold DataReference addr
817 return $ CmmLoad addr' rep
820 -- For MachOps, we first optimize the children, and then we try
821 -- our hand at some constant-folding.
822 -> do args' <- mapM (cmmExprConFold DataReference) args
823 return $ cmmMachOpFold mop args'
825 CmmLit (CmmLabel lbl)
827 dflags <- getDynFlagsCmmOpt
828 cmmMakeDynamicReference dflags addImportCmmOpt referenceKind lbl
829 CmmLit (CmmLabelOff lbl off)
831 dflags <- getDynFlagsCmmOpt
832 dynRef <- cmmMakeDynamicReference dflags addImportCmmOpt referenceKind lbl
833 return $ cmmMachOpFold (MO_Add wordWidth) [
835 (CmmLit $ CmmInt (fromIntegral off) wordWidth)
838 #if powerpc_TARGET_ARCH
839 -- On powerpc (non-PIC), it's easier to jump directly to a label than
840 -- to use the register table, so we replace these registers
841 -- with the corresponding labels:
842 CmmReg (CmmGlobal EagerBlackholeInfo)
844 -> cmmExprConFold referenceKind $
845 CmmLit (CmmLabel (mkCmmCodeLabel rtsPackageId (fsLit "__stg_EAGER_BLACKHOLE_info")))
846 CmmReg (CmmGlobal GCEnter1)
848 -> cmmExprConFold referenceKind $
849 CmmLit (CmmLabel (mkCmmCodeLabel rtsPackageId (fsLit "__stg_gc_enter_1")))
850 CmmReg (CmmGlobal GCFun)
852 -> cmmExprConFold referenceKind $
853 CmmLit (CmmLabel (mkCmmCodeLabel rtsPackageId (fsLit "__stg_gc_fun")))