Merge remote branch 'origin/master'
[ghc-hetmet.git] / compiler / nativeGen / AsmCodeGen.lhs
1 -- -----------------------------------------------------------------------------
2 --
3 -- (c) The University of Glasgow 1993-2004
4 -- 
5 -- This is the top-level module in the native code generator.
6 --
7 -- -----------------------------------------------------------------------------
8
9 \begin{code}
10 module AsmCodeGen ( nativeCodeGen ) where
11
12 #include "HsVersions.h"
13 #include "nativeGen/NCG.h"
14
15
16 #if i386_TARGET_ARCH || x86_64_TARGET_ARCH
17 import X86.CodeGen
18 import X86.Regs
19 import X86.Instr
20 import X86.Ppr
21
22 #elif sparc_TARGET_ARCH
23 import SPARC.CodeGen
24 import SPARC.CodeGen.Expand
25 import SPARC.Regs
26 import SPARC.Instr
27 import SPARC.Ppr
28 import SPARC.ShortcutJump
29
30 #elif powerpc_TARGET_ARCH
31 import PPC.CodeGen
32 import PPC.Cond
33 import PPC.Regs
34 import PPC.RegInfo
35 import PPC.Instr
36 import PPC.Ppr
37
38 #else
39 #error "AsmCodeGen: unknown architecture"
40
41 #endif
42
43 import RegAlloc.Liveness
44 import qualified RegAlloc.Linear.Main           as Linear
45
46 import qualified GraphColor                     as Color
47 import qualified RegAlloc.Graph.Main            as Color
48 import qualified RegAlloc.Graph.Stats           as Color
49 import qualified RegAlloc.Graph.TrivColorable   as Color
50
51 import TargetReg
52 import Platform
53 import Config
54 import Instruction
55 import PIC
56 import Reg
57 import NCGMonad
58
59 import BlockId
60 import CgUtils          ( fixStgRegisters )
61 import OldCmm
62 import CmmOpt           ( cmmEliminateDeadBlocks, cmmMiniInline, cmmMachOpFold )
63 import OldPprCmm
64 import CLabel
65
66 import UniqFM
67 import Unique           ( Unique, getUnique )
68 import UniqSupply
69 import DynFlags
70 import StaticFlags
71 import Util
72
73 import Digraph
74 import qualified Pretty
75 import BufWrite
76 import Outputable
77 import FastString
78 import UniqSet
79 import ErrUtils
80 import Module
81
82 -- DEBUGGING ONLY
83 --import OrdList
84
85 import Data.List
86 import Data.Maybe
87 import Control.Monad
88 import System.IO
89
90 {-
91 The native-code generator has machine-independent and
92 machine-dependent modules.
93
94 This module ("AsmCodeGen") is the top-level machine-independent
95 module.  Before entering machine-dependent land, we do some
96 machine-independent optimisations (defined below) on the
97 'CmmStmts's.
98
99 We convert to the machine-specific 'Instr' datatype with
100 'cmmCodeGen', assuming an infinite supply of registers.  We then use
101 a machine-independent register allocator ('regAlloc') to rejoin
102 reality.  Obviously, 'regAlloc' has machine-specific helper
103 functions (see about "RegAllocInfo" below).
104
105 Finally, we order the basic blocks of the function so as to minimise
106 the number of jumps between blocks, by utilising fallthrough wherever
107 possible.
108
109 The machine-dependent bits break down as follows:
110
111   * ["MachRegs"]  Everything about the target platform's machine
112     registers (and immediate operands, and addresses, which tend to
113     intermingle/interact with registers).
114
115   * ["MachInstrs"]  Includes the 'Instr' datatype (possibly should
116     have a module of its own), plus a miscellany of other things
117     (e.g., 'targetDoubleSize', 'smStablePtrTable', ...)
118
119   * ["MachCodeGen"]  is where 'Cmm' stuff turns into
120     machine instructions.
121
122   * ["PprMach"] 'pprInstr' turns an 'Instr' into text (well, really
123     a 'Doc').
124
125   * ["RegAllocInfo"] In the register allocator, we manipulate
126     'MRegsState's, which are 'BitSet's, one bit per machine register.
127     When we want to say something about a specific machine register
128     (e.g., ``it gets clobbered by this instruction''), we set/unset
129     its bit.  Obviously, we do this 'BitSet' thing for efficiency
130     reasons.
131
132     The 'RegAllocInfo' module collects together the machine-specific
133     info needed to do register allocation.
134
135    * ["RegisterAlloc"] The (machine-independent) register allocator.
136 -}
137
138 -- -----------------------------------------------------------------------------
139 -- Top-level of the native codegen
140
141 --------------------
142 nativeCodeGen :: DynFlags -> Handle -> UniqSupply -> [RawCmm] -> IO ()
143 nativeCodeGen dflags h us cmms
144  = do
145         let split_cmms  = concat $ map add_split cmms
146
147         -- BufHandle is a performance hack.  We could hide it inside
148         -- Pretty if it weren't for the fact that we do lots of little
149         -- printDocs here (in order to do codegen in constant space).
150         bufh <- newBufHandle h
151         (imports, prof) <- cmmNativeGens dflags bufh us split_cmms [] [] 0
152         bFlush bufh
153
154         let (native, colorStats, linearStats)
155                 = unzip3 prof
156
157         -- dump native code
158         dumpIfSet_dyn dflags
159                 Opt_D_dump_asm "Asm code"
160                 (vcat $ map (docToSDoc . pprNatCmmTop) $ concat native)
161
162         -- dump global NCG stats for graph coloring allocator
163         (case concat $ catMaybes colorStats of
164           []    -> return ()
165           stats -> do   
166                 -- build the global register conflict graph
167                 let graphGlobal 
168                         = foldl Color.union Color.initGraph
169                         $ [ Color.raGraph stat
170                                 | stat@Color.RegAllocStatsStart{} <- stats]
171            
172                 dumpSDoc dflags Opt_D_dump_asm_stats "NCG stats"
173                         $ Color.pprStats stats graphGlobal
174
175                 dumpIfSet_dyn dflags
176                         Opt_D_dump_asm_conflicts "Register conflict graph"
177                         $ Color.dotGraph 
178                                 targetRegDotColor 
179                                 (Color.trivColorable 
180                                         targetVirtualRegSqueeze 
181                                         targetRealRegSqueeze)
182                         $ graphGlobal)
183
184
185         -- dump global NCG stats for linear allocator
186         (case concat $ catMaybes linearStats of
187                 []      -> return ()
188                 stats   -> dumpSDoc dflags Opt_D_dump_asm_stats "NCG stats"
189                                 $ Linear.pprStats (concat native) stats)
190
191         -- write out the imports
192         Pretty.printDoc Pretty.LeftMode h
193                 $ makeImportsDoc dflags (concat imports)
194
195         return  ()
196
197  where  add_split (Cmm tops)
198                 | dopt Opt_SplitObjs dflags = split_marker : tops
199                 | otherwise                 = tops
200
201         split_marker = CmmProc [] mkSplitMarkerLabel (ListGraph [])
202
203
204 -- | Do native code generation on all these cmms.
205 --
206 cmmNativeGens :: DynFlags
207               -> BufHandle
208               -> UniqSupply
209               -> [RawCmmTop]
210               -> [[CLabel]]
211               -> [ ([NatCmmTop Instr],
212                    Maybe [Color.RegAllocStats Instr],
213                    Maybe [Linear.RegAllocStats]) ]
214               -> Int
215               -> IO ( [[CLabel]],
216                       [([NatCmmTop Instr],
217                       Maybe [Color.RegAllocStats Instr],
218                       Maybe [Linear.RegAllocStats])] )
219
220 cmmNativeGens _ _ _ [] impAcc profAcc _
221         = return (reverse impAcc, reverse profAcc)
222
223 cmmNativeGens dflags h us (cmm : cmms) impAcc profAcc count
224  = do
225         (us', native, imports, colorStats, linearStats)
226                 <- cmmNativeGen dflags us cmm count
227
228         Pretty.bufLeftRender h
229                 $ {-# SCC "pprNativeCode" #-} Pretty.vcat $ map pprNatCmmTop native
230
231            -- carefully evaluate this strictly.  Binding it with 'let'
232            -- and then using 'seq' doesn't work, because the let
233            -- apparently gets inlined first.
234         lsPprNative <- return $!
235                 if  dopt Opt_D_dump_asm       dflags
236                  || dopt Opt_D_dump_asm_stats dflags
237                         then native
238                         else []
239
240         count' <- return $! count + 1;
241
242         -- force evaulation all this stuff to avoid space leaks
243         seqString (showSDoc $ vcat $ map ppr imports) `seq` return ()
244
245         cmmNativeGens dflags h us' cmms
246                         (imports : impAcc)
247                         ((lsPprNative, colorStats, linearStats) : profAcc)
248                         count'
249
250  where  seqString []            = ()
251         seqString (x:xs)        = x `seq` seqString xs `seq` ()
252
253
254 -- | Complete native code generation phase for a single top-level chunk of Cmm.
255 --      Dumping the output of each stage along the way.
256 --      Global conflict graph and NGC stats
257 cmmNativeGen 
258         :: DynFlags
259         -> UniqSupply
260         -> RawCmmTop                                    -- ^ the cmm to generate code for
261         -> Int                                          -- ^ sequence number of this top thing
262         -> IO   ( UniqSupply
263                 , [NatCmmTop Instr]                     -- native code
264                 , [CLabel]                              -- things imported by this cmm
265                 , Maybe [Color.RegAllocStats Instr]     -- stats for the coloring register allocator
266                 , Maybe [Linear.RegAllocStats])         -- stats for the linear register allocators
267
268 cmmNativeGen dflags us cmm count
269  = do
270
271         -- rewrite assignments to global regs
272         let fixed_cmm =
273                 {-# SCC "fixStgRegisters" #-}
274                 fixStgRegisters cmm
275
276         -- cmm to cmm optimisations
277         let (opt_cmm, imports) =
278                 {-# SCC "cmmToCmm" #-}
279                 cmmToCmm dflags fixed_cmm
280
281         dumpIfSet_dyn dflags
282                 Opt_D_dump_opt_cmm "Optimised Cmm"
283                 (pprCmm $ Cmm [opt_cmm])
284
285         -- generate native code from cmm
286         let ((native, lastMinuteImports), usGen) =
287                 {-# SCC "genMachCode" #-}
288                 initUs us $ genMachCode dflags opt_cmm
289
290         dumpIfSet_dyn dflags
291                 Opt_D_dump_asm_native "Native code"
292                 (vcat $ map (docToSDoc . pprNatCmmTop) native)
293
294         -- tag instructions with register liveness information
295         let (withLiveness, usLive) =
296                 {-# SCC "regLiveness" #-}
297                 initUs usGen 
298                         $ mapUs regLiveness 
299                         $ map natCmmTopToLive native
300
301         dumpIfSet_dyn dflags
302                 Opt_D_dump_asm_liveness "Liveness annotations added"
303                 (vcat $ map ppr withLiveness)
304                 
305         -- allocate registers
306         (alloced, usAlloc, ppr_raStatsColor, ppr_raStatsLinear) <-
307          if ( dopt Opt_RegsGraph dflags
308            || dopt Opt_RegsIterative dflags)
309           then do
310                 -- the regs usable for allocation
311                 let (alloc_regs :: UniqFM (UniqSet RealReg))
312                         = foldr (\r -> plusUFM_C unionUniqSets
313                                         $ unitUFM (targetClassOfRealReg r) (unitUniqSet r))
314                                 emptyUFM
315                         $ allocatableRegs
316
317                 -- do the graph coloring register allocation
318                 let ((alloced, regAllocStats), usAlloc)
319                         = {-# SCC "RegAlloc" #-}
320                           initUs usLive
321                           $ Color.regAlloc
322                                 dflags
323                                 alloc_regs
324                                 (mkUniqSet [0..maxSpillSlots])
325                                 withLiveness
326
327                 -- dump out what happened during register allocation
328                 dumpIfSet_dyn dflags
329                         Opt_D_dump_asm_regalloc "Registers allocated"
330                         (vcat $ map (docToSDoc . pprNatCmmTop) alloced)
331
332                 dumpIfSet_dyn dflags
333                         Opt_D_dump_asm_regalloc_stages "Build/spill stages"
334                         (vcat   $ map (\(stage, stats)
335                                         -> text "# --------------------------"
336                                         $$ text "#  cmm " <> int count <> text " Stage " <> int stage
337                                         $$ ppr stats)
338                                 $ zip [0..] regAllocStats)
339
340                 let mPprStats =
341                         if dopt Opt_D_dump_asm_stats dflags
342                          then Just regAllocStats else Nothing
343
344                 -- force evaluation of the Maybe to avoid space leak
345                 mPprStats `seq` return ()
346
347                 return  ( alloced, usAlloc
348                         , mPprStats
349                         , Nothing)
350
351           else do
352                 -- do linear register allocation
353                 let ((alloced, regAllocStats), usAlloc) 
354                         = {-# SCC "RegAlloc" #-}
355                           initUs usLive
356                           $ liftM unzip
357                           $ mapUs Linear.regAlloc withLiveness
358
359                 dumpIfSet_dyn dflags
360                         Opt_D_dump_asm_regalloc "Registers allocated"
361                         (vcat $ map (docToSDoc . pprNatCmmTop) alloced)
362
363                 let mPprStats =
364                         if dopt Opt_D_dump_asm_stats dflags
365                          then Just (catMaybes regAllocStats) else Nothing
366
367                 -- force evaluation of the Maybe to avoid space leak
368                 mPprStats `seq` return ()
369
370                 return  ( alloced, usAlloc
371                         , Nothing
372                         , mPprStats)
373
374         ---- x86fp_kludge.  This pass inserts ffree instructions to clear
375         ---- the FPU stack on x86.  The x86 ABI requires that the FPU stack
376         ---- is clear, and library functions can return odd results if it
377         ---- isn't.
378         ----
379         ---- NB. must happen before shortcutBranches, because that
380         ---- generates JXX_GBLs which we can't fix up in x86fp_kludge.
381         let kludged =
382 #if i386_TARGET_ARCH
383                 {-# SCC "x86fp_kludge" #-}
384                 map x86fp_kludge alloced
385 #else
386                 alloced
387 #endif
388
389         ---- generate jump tables
390         let tabled      =
391                 {-# SCC "generateJumpTables" #-}
392                 generateJumpTables kludged
393
394         ---- shortcut branches
395         let shorted     =
396                 {-# SCC "shortcutBranches" #-}
397                 shortcutBranches dflags tabled
398
399         ---- sequence blocks
400         let sequenced   =
401                 {-# SCC "sequenceBlocks" #-}
402                 map sequenceTop shorted
403
404         ---- expansion of SPARC synthetic instrs
405 #if sparc_TARGET_ARCH
406         let expanded = 
407                 {-# SCC "sparc_expand" #-}
408                 map expandTop sequenced
409
410         dumpIfSet_dyn dflags
411                 Opt_D_dump_asm_expanded "Synthetic instructions expanded"
412                 (vcat $ map (docToSDoc . pprNatCmmTop) expanded)
413 #else
414         let expanded = 
415                 sequenced
416 #endif
417
418         return  ( usAlloc
419                 , expanded
420                 , lastMinuteImports ++ imports
421                 , ppr_raStatsColor
422                 , ppr_raStatsLinear)
423
424
425 #if i386_TARGET_ARCH
426 x86fp_kludge :: NatCmmTop Instr -> NatCmmTop Instr
427 x86fp_kludge top@(CmmData _ _) = top
428 x86fp_kludge (CmmProc info lbl (ListGraph code)) = 
429         CmmProc info lbl (ListGraph $ i386_insert_ffrees code)
430 #endif
431
432
433 -- | Build a doc for all the imports.
434 --
435 makeImportsDoc :: DynFlags -> [CLabel] -> Pretty.Doc
436 makeImportsDoc dflags imports
437  = dyld_stubs imports
438
439 #if HAVE_SUBSECTIONS_VIA_SYMBOLS
440                 -- On recent versions of Darwin, the linker supports
441                 -- dead-stripping of code and data on a per-symbol basis.
442                 -- There's a hack to make this work in PprMach.pprNatCmmTop.
443             Pretty.$$ Pretty.text ".subsections_via_symbols"
444 #endif
445 #if HAVE_GNU_NONEXEC_STACK
446                 -- On recent GNU ELF systems one can mark an object file
447                 -- as not requiring an executable stack. If all objects
448                 -- linked into a program have this note then the program
449                 -- will not use an executable stack, which is good for
450                 -- security. GHC generated code does not need an executable
451                 -- stack so add the note in:
452             Pretty.$$ Pretty.text ".section .note.GNU-stack,\"\",@progbits"
453 #endif
454                 -- And just because every other compiler does, lets stick in
455                 -- an identifier directive: .ident "GHC x.y.z"
456             Pretty.$$ let compilerIdent = Pretty.text "GHC" Pretty.<+>
457                                           Pretty.text cProjectVersion
458                        in Pretty.text ".ident" Pretty.<+>
459                           Pretty.doubleQuotes compilerIdent
460
461  where
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-}
467
468         arch    = platformArch  $ targetPlatform dflags
469         os      = platformOS    $ targetPlatform dflags
470         
471         -- (Hack) sometimes two Labels pretty-print the same, but have
472         -- different uniques; so we compare their text versions...
473         dyld_stubs imps
474                 | needImportedSymbols arch os
475                 = Pretty.vcat $
476                         (pprGotDeclaration arch os :) $
477                         map ( pprImportedSymbol arch os . fst . head) $
478                         groupBy (\(_,a) (_,b) -> a == b) $
479                         sortBy (\(_,a) (_,b) -> compare a b) $
480                         map doPpr $
481                         imps
482                 | otherwise
483                 = Pretty.empty
484
485         doPpr lbl = (lbl, renderWithStyle (pprCLabel lbl) astyle)
486         astyle = mkCodeStyle AsmStyle
487
488
489 -- -----------------------------------------------------------------------------
490 -- Sequencing the basic blocks
491
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
496 -- fallthroughs.
497
498 sequenceTop 
499         :: NatCmmTop Instr
500         -> NatCmmTop Instr
501
502 sequenceTop top@(CmmData _ _) = top
503 sequenceTop (CmmProc info lbl (ListGraph blocks)) = 
504   CmmProc info lbl (ListGraph $ makeFarBranches $ sequenceBlocks blocks)
505
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.
512
513 -- FYI, the classic layout for basic blocks uses postorder DFS; this
514 -- algorithm is implemented in Hoopl.
515
516 sequenceBlocks 
517         :: Instruction instr
518         => [NatBasicBlock instr] 
519         -> [NatBasicBlock instr]
520
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.
525
526
527 sccBlocks 
528         :: Instruction instr
529         => [NatBasicBlock instr] 
530         -> [SCC ( NatBasicBlock instr
531                 , Unique
532                 , [Unique])]
533
534 sccBlocks blocks = stronglyConnCompFromEdgedVerticesR (map mkNode blocks)
535
536 -- we're only interested in the last instruction of
537 -- the block, and only if it has a single destination.
538 getOutEdges 
539         :: Instruction instr
540         => [instr] -> [Unique]
541
542 getOutEdges instrs 
543         = case jumpDestsOfInstr (last instrs) of
544                 [one] -> [getUnique one]
545                 _many -> []
546
547 mkNode :: (Instruction t)
548        => GenBasicBlock t
549        -> (GenBasicBlock t, Unique, [Unique])
550 mkNode block@(BasicBlock id instrs) = (block, getUnique id, getOutEdges instrs)
551
552 seqBlocks :: (Eq t) => [(GenBasicBlock t1, t, [t])] -> [GenBasicBlock t1]
553 seqBlocks [] = []
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'
559   where
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"
564
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
570
571
572 -- -----------------------------------------------------------------------------
573 -- Making far branches
574
575 -- Conditional branches on PowerPC are limited to +-32KB; if our Procs get too
576 -- big, we have to work around this limitation.
577
578 makeFarBranches 
579         :: [NatBasicBlock Instr] 
580         -> [NatBasicBlock Instr]
581
582 #if powerpc_TARGET_ARCH
583 makeFarBranches blocks
584     | last blockAddresses < nearLimit = blocks
585     | otherwise = zipWith handleBlock blockAddresses blocks
586     where
587         blockAddresses = scanl (+) 0 $ map blockLen blocks
588         blockLen (BasicBlock _ instrs) = length instrs
589         
590         handleBlock addr (BasicBlock id instrs)
591                 = BasicBlock id (zipWith makeFar [addr..] instrs)
592         
593         makeFar _ (BCC ALWAYS tgt) = BCC ALWAYS tgt
594         makeFar addr (BCC cond tgt)
595             | abs (addr - targetAddr) >= nearLimit
596             = BCCFAR cond tgt
597             | otherwise
598             = BCC cond tgt
599             where Just targetAddr = lookupUFM blockAddressMap tgt
600         makeFar _ other            = other
601         
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
606                          -- things exactly
607         
608         blockAddressMap = listToUFM $ zip (map blockId blocks) blockAddresses
609 #else
610 makeFarBranches = id
611 #endif
612
613 -- -----------------------------------------------------------------------------
614 -- Generate jump tables
615
616 -- Analyzes all native code and generates data sections for all jump
617 -- table instructions.
618 generateJumpTables
619         :: [NatCmmTop Instr] -> [NatCmmTop Instr]
620 generateJumpTables xs = concatMap f xs
621     where f p@(CmmProc _ _ (ListGraph xs)) = p : concatMap g xs
622           f p = [p]
623           g (BasicBlock _ xs) = catMaybes (map generateJumpTableForInstr xs)
624
625 -- -----------------------------------------------------------------------------
626 -- Shortcut branches
627
628 shortcutBranches 
629         :: DynFlags 
630         -> [NatCmmTop Instr] 
631         -> [NatCmmTop Instr]
632
633 shortcutBranches dflags tops
634   | optLevel dflags < 1 = tops    -- only with -O or higher
635   | otherwise           = map (apply_mapping mapping) tops'
636   where
637     (tops', mappings) = mapAndUnzip build_mapping tops
638     mapping = foldr plusUFM emptyUFM mappings
639
640 build_mapping :: GenCmmTop d t (ListGraph Instr)
641               -> (GenCmmTop d t (ListGraph Instr), UniqFM JumpDest)
642 build_mapping top@(CmmData _ _) = (top, emptyUFM)
643 build_mapping (CmmProc info lbl (ListGraph []))
644   = (CmmProc info lbl (ListGraph []), emptyUFM)
645 build_mapping (CmmProc info lbl (ListGraph (head:blocks)))
646   = (CmmProc info lbl (ListGraph (head:others)), mapping)
647         -- drop the shorted blocks, but don't ever drop the first one,
648         -- because it is pointed to by a global label.
649   where
650     -- find all the blocks that just consist of a jump that can be
651     -- shorted.
652     -- Don't completely eliminate loops here -- that can leave a dangling jump!
653     (_, shortcut_blocks, others) = foldl split (emptyBlockSet, [], []) blocks
654     split (s, shortcut_blocks, others) b@(BasicBlock id [insn])
655         | Just (DestBlockId dest) <- canShortcut insn,
656           (setMember dest s) || dest == id -- loop checks
657         = (s, shortcut_blocks, b : others)
658     split (s, shortcut_blocks, others) (BasicBlock id [insn])
659         | Just dest <- canShortcut insn
660         = (setInsert id s, (id,dest) : shortcut_blocks, others)
661     split (s, shortcut_blocks, others) other = (s, shortcut_blocks, other : others)
662
663
664     -- build a mapping from BlockId to JumpDest for shorting branches
665     mapping = foldl add emptyUFM shortcut_blocks
666     add ufm (id,dest) = addToUFM ufm id dest
667     
668 apply_mapping :: UniqFM JumpDest
669               -> GenCmmTop CmmStatic h (ListGraph Instr)
670               -> GenCmmTop CmmStatic h (ListGraph Instr)
671 apply_mapping ufm (CmmData sec statics) 
672   = CmmData sec (map (shortcutStatic (lookupUFM ufm)) statics)
673   -- we need to get the jump tables, so apply the mapping to the entries
674   -- of a CmmData too.
675 apply_mapping ufm (CmmProc info lbl (ListGraph blocks))
676   = CmmProc info lbl (ListGraph $ map short_bb blocks)
677   where
678     short_bb (BasicBlock id insns) = BasicBlock id $! map short_insn insns
679     short_insn i = shortcutJump (lookupUFM ufm) i
680                  -- shortcutJump should apply the mapping repeatedly,
681                  -- just in case we can short multiple branches.
682
683 -- -----------------------------------------------------------------------------
684 -- Instruction selection
685
686 -- Native code instruction selection for a chunk of stix code.  For
687 -- this part of the computation, we switch from the UniqSM monad to
688 -- the NatM monad.  The latter carries not only a Unique, but also an
689 -- Int denoting the current C stack pointer offset in the generated
690 -- code; this is needed for creating correct spill offsets on
691 -- architectures which don't offer, or for which it would be
692 -- prohibitively expensive to employ, a frame pointer register.  Viz,
693 -- x86.
694
695 -- The offset is measured in bytes, and indicates the difference
696 -- between the current (simulated) C stack-ptr and the value it was at
697 -- the beginning of the block.  For stacks which grow down, this value
698 -- should be either zero or negative.
699
700 -- Switching between the two monads whilst carrying along the same
701 -- Unique supply breaks abstraction.  Is that bad?
702
703 genMachCode 
704         :: DynFlags 
705         -> RawCmmTop 
706         -> UniqSM 
707                 ( [NatCmmTop Instr]
708                 , [CLabel])
709
710 genMachCode dflags cmm_top
711   = do  { initial_us <- getUs
712         ; let initial_st           = mkNatM_State initial_us 0 dflags
713               (new_tops, final_st) = initNat initial_st (cmmTopCodeGen dflags cmm_top)
714               final_delta          = natm_delta final_st
715               final_imports        = natm_imports final_st
716         ; if   final_delta == 0
717           then return (new_tops, final_imports)
718           else pprPanic "genMachCode: nonzero final delta" (int final_delta)
719     }
720
721 -- -----------------------------------------------------------------------------
722 -- Generic Cmm optimiser
723
724 {-
725 Here we do:
726
727   (a) Constant folding
728   (b) Simple inlining: a temporary which is assigned to and then
729       used, once, can be shorted.
730   (c) Position independent code and dynamic linking
731         (i)  introduce the appropriate indirections
732              and position independent refs
733         (ii) compile a list of imported symbols
734
735 Ideas for other things we could do:
736
737   - shortcut jumps-to-jumps
738   - simple CSE: if an expr is assigned to a temp, then replace later occs of
739     that expr with the temp, until the expr is no longer valid (can push through
740     temp assignments, and certain assigns to mem...)
741 -}
742
743 cmmToCmm :: DynFlags -> RawCmmTop -> (RawCmmTop, [CLabel])
744 cmmToCmm _ top@(CmmData _ _) = (top, [])
745 cmmToCmm dflags (CmmProc info lbl (ListGraph blocks)) = runCmmOpt dflags $ do
746   blocks' <- mapM cmmBlockConFold (cmmMiniInline (cmmEliminateDeadBlocks blocks))
747   return $ CmmProc info lbl (ListGraph blocks')
748
749 newtype CmmOptM a = CmmOptM (([CLabel], DynFlags) -> (# a, [CLabel] #))
750
751 instance Monad CmmOptM where
752   return x = CmmOptM $ \(imports, _) -> (# x,imports #)
753   (CmmOptM f) >>= g =
754     CmmOptM $ \(imports, dflags) ->
755                 case f (imports, dflags) of
756                   (# x, imports' #) ->
757                     case g x of
758                       CmmOptM g' -> g' (imports', dflags)
759
760 addImportCmmOpt :: CLabel -> CmmOptM ()
761 addImportCmmOpt lbl = CmmOptM $ \(imports, _dflags) -> (# (), lbl:imports #)
762
763 getDynFlagsCmmOpt :: CmmOptM DynFlags
764 getDynFlagsCmmOpt = CmmOptM $ \(imports, dflags) -> (# dflags, imports #)
765
766 runCmmOpt :: DynFlags -> CmmOptM a -> (a, [CLabel])
767 runCmmOpt dflags (CmmOptM f) = case f ([], dflags) of
768                         (# result, imports #) -> (result, imports)
769
770 cmmBlockConFold :: CmmBasicBlock -> CmmOptM CmmBasicBlock
771 cmmBlockConFold (BasicBlock id stmts) = do
772   stmts' <- mapM cmmStmtConFold stmts
773   return $ BasicBlock id stmts'
774
775 cmmStmtConFold :: CmmStmt -> CmmOptM CmmStmt
776 cmmStmtConFold stmt
777    = case stmt of
778         CmmAssign reg src
779            -> do src' <- cmmExprConFold DataReference src
780                  return $ case src' of
781                    CmmReg reg' | reg == reg' -> CmmNop
782                    new_src -> CmmAssign reg new_src
783
784         CmmStore addr src
785            -> do addr' <- cmmExprConFold DataReference addr
786                  src'  <- cmmExprConFold DataReference src
787                  return $ CmmStore addr' src'
788
789         CmmJump addr regs
790            -> do addr' <- cmmExprConFold JumpReference addr
791                  return $ CmmJump addr' regs
792
793         CmmCall target regs args srt returns
794            -> do target' <- case target of
795                               CmmCallee e conv -> do
796                                 e' <- cmmExprConFold CallReference e
797                                 return $ CmmCallee e' conv
798                               other -> return other
799                  args' <- mapM (\(CmmHinted arg hint) -> do
800                                   arg' <- cmmExprConFold DataReference arg
801                                   return (CmmHinted arg' hint)) args
802                  return $ CmmCall target' regs args' srt returns
803
804         CmmCondBranch test dest
805            -> do test' <- cmmExprConFold DataReference test
806                  return $ case test' of
807                    CmmLit (CmmInt 0 _) -> 
808                      CmmComment (mkFastString ("deleted: " ++ 
809                                         showSDoc (pprStmt stmt)))
810
811                    CmmLit (CmmInt _ _) -> CmmBranch dest
812                    _other -> CmmCondBranch test' dest
813
814         CmmSwitch expr ids
815            -> do expr' <- cmmExprConFold DataReference expr
816                  return $ CmmSwitch expr' ids
817
818         other
819            -> return other
820
821
822 cmmExprConFold :: ReferenceKind -> CmmExpr -> CmmOptM CmmExpr
823 cmmExprConFold referenceKind expr = do
824      dflags <- getDynFlagsCmmOpt
825      let arch = platformArch (targetPlatform dflags)
826      case expr of
827         CmmLoad addr rep
828            -> do addr' <- cmmExprConFold DataReference addr
829                  return $ CmmLoad addr' rep
830
831         CmmMachOp mop args
832            -- For MachOps, we first optimize the children, and then we try 
833            -- our hand at some constant-folding.
834            -> do args' <- mapM (cmmExprConFold DataReference) args
835                  return $ cmmMachOpFold mop args'
836
837         CmmLit (CmmLabel lbl)
838            -> do
839                 cmmMakeDynamicReference dflags addImportCmmOpt referenceKind lbl
840         CmmLit (CmmLabelOff lbl off)
841            -> do
842                  dynRef <- cmmMakeDynamicReference dflags addImportCmmOpt referenceKind lbl
843                  return $ cmmMachOpFold (MO_Add wordWidth) [
844                      dynRef,
845                      (CmmLit $ CmmInt (fromIntegral off) wordWidth)
846                    ]
847
848         -- On powerpc (non-PIC), it's easier to jump directly to a label than
849         -- to use the register table, so we replace these registers
850         -- with the corresponding labels:
851         CmmReg (CmmGlobal EagerBlackholeInfo)
852           | arch == ArchPPC && not opt_PIC
853           -> cmmExprConFold referenceKind $
854              CmmLit (CmmLabel (mkCmmCodeLabel rtsPackageId (fsLit "__stg_EAGER_BLACKHOLE_info")))
855         CmmReg (CmmGlobal GCEnter1)
856           | arch == ArchPPC && not opt_PIC
857           -> cmmExprConFold referenceKind $
858              CmmLit (CmmLabel (mkCmmCodeLabel rtsPackageId (fsLit "__stg_gc_enter_1"))) 
859         CmmReg (CmmGlobal GCFun)
860           | arch == ArchPPC && not opt_PIC
861           -> cmmExprConFold referenceKind $
862              CmmLit (CmmLabel (mkCmmCodeLabel rtsPackageId (fsLit "__stg_gc_fun")))
863
864         other
865            -> return other
866
867 \end{code}
868