X-Git-Url: http://git.megacz.com/?p=ghc-hetmet.git;a=blobdiff_plain;f=compiler%2FnativeGen%2FAsmCodeGen.lhs;h=a04c5c7527ceedd691f7ab2063505c4abfc5b705;hp=f2a4226a4263b18534c6eb72b111438208c893ef;hb=d31dfb32ea936c22628b508c28a36c12e631430a;hpb=0d7c6cea4af4ac1137f40b8e35348744e80a23b9 diff --git a/compiler/nativeGen/AsmCodeGen.lhs b/compiler/nativeGen/AsmCodeGen.lhs index f2a4226..a04c5c7 100644 --- a/compiler/nativeGen/AsmCodeGen.lhs +++ b/compiler/nativeGen/AsmCodeGen.lhs @@ -10,14 +10,14 @@ module AsmCodeGen ( nativeCodeGen ) where #include "HsVersions.h" -#include "NCG.h" +#include "nativeGen/NCG.h" import MachInstrs import MachRegs import MachCodeGen import PprMach import RegisterAlloc -import RegAllocInfo ( jumpDests ) +import RegAllocInfo import NCGMonad import PositionIndependentCode @@ -25,20 +25,17 @@ import Cmm import CmmOpt ( cmmMiniInline, cmmMachOpFold ) import PprCmm ( pprStmt, pprCmms ) import MachOp -import CLabel ( CLabel, mkSplitMarkerLabel, mkAsmTempLabel ) -#if powerpc_TARGET_ARCH -import CLabel ( mkRtsCodeLabel ) -#endif +import CLabel import UniqFM import Unique ( Unique, getUnique ) import UniqSupply import FastTypes import List ( groupBy, sortBy ) -import CLabel ( pprCLabel ) import ErrUtils ( dumpIfSet_dyn ) -import DynFlags ( DynFlags, DynFlag(..), dopt ) +import DynFlags import StaticFlags ( opt_Static, opt_PIC ) +import Util import Config ( cProjectVersion ) import Digraph @@ -53,10 +50,10 @@ import FastString import List ( intersperse ) #endif -import DATA_INT -import DATA_WORD -import DATA_BITS -import GLAEXTS +import Data.Int +import Data.Word +import Data.Bits +import GHC.Exts {- The native-code generator has machine-independent and @@ -111,12 +108,12 @@ The machine-dependent bits break down as follows: -- NB. We *lazilly* compile each block of code for space reasons. -nativeCodeGen :: DynFlags -> [Cmm] -> UniqSupply -> IO Pretty.Doc +nativeCodeGen :: DynFlags -> [RawCmm] -> UniqSupply -> IO Pretty.Doc nativeCodeGen dflags cmms us = let (res, _) = initUs us $ cgCmm (concat (map add_split cmms)) - cgCmm :: [CmmTop] -> UniqSM (Cmm, Pretty.Doc, [CLabel]) + cgCmm :: [RawCmmTop] -> UniqSM (RawCmm, Pretty.Doc, [CLabel]) cgCmm tops = lazyMapUs (cmmNativeGen dflags) tops `thenUs` \ results -> case unzip3 results of { (cmms,docs,imps) -> @@ -141,12 +138,14 @@ nativeCodeGen dflags cmms us -- stack so add the note in: Pretty.$$ Pretty.text ".section .note.GNU-stack,\"\",@progbits" #endif +#if !defined(darwin_TARGET_OS) -- And just because every other compiler does, lets stick in -- an identifier directive: .ident "GHC x.y.z" Pretty.$$ let compilerIdent = Pretty.text "GHC" Pretty.<+> Pretty.text cProjectVersion in Pretty.text ".ident" Pretty.<+> Pretty.doubleQuotes compilerIdent +#endif ) } @@ -197,7 +196,7 @@ nativeCodeGen dflags cmms us -- Complete native code generation phase for a single top-level chunk -- of Cmm. -cmmNativeGen :: DynFlags -> CmmTop -> UniqSM (CmmTop, Pretty.Doc, [CLabel]) +cmmNativeGen :: DynFlags -> RawCmmTop -> UniqSM (RawCmmTop, Pretty.Doc, [CLabel]) cmmNativeGen dflags cmm = {-# SCC "fixAssigns" #-} fixAssignsTop cmm `thenUs` \ fixed_cmm -> @@ -210,8 +209,10 @@ cmmNativeGen dflags cmm genMachCode cmm `thenUs` \ (pre_regalloc, lastMinuteImports) -> {-# SCC "regAlloc" #-} mapUs regAlloc pre_regalloc `thenUs` \ with_regs -> + {-# SCC "shortcutBranches" #-} + shortcutBranches dflags with_regs `bind` \ shorted -> {-# SCC "sequenceBlocks" #-} - map sequenceTop with_regs `bind` \ sequenced -> + map sequenceTop shorted `bind` \ sequenced -> {-# SCC "x86fp_kludge" #-} map x86fp_kludge sequenced `bind` \ final_mach_code -> {-# SCC "vcat" #-} @@ -243,7 +244,7 @@ cmmNativeGen dflags cmm sequenceTop :: NatCmmTop -> NatCmmTop sequenceTop top@(CmmData _ _) = top sequenceTop (CmmProc info lbl params blocks) = - CmmProc info lbl params (sequenceBlocks blocks) + CmmProc info lbl params (makeFarBranches $ sequenceBlocks blocks) -- The algorithm is very simple (and stupid): we make a graph out of -- the blocks where there is an edge from one block to another iff the @@ -287,6 +288,88 @@ reorder id accum (b@(block,id',out) : rest) | id == id' = (True, (block,id,out) : reverse accum ++ rest) | otherwise = reorder id (b:accum) rest + +-- ----------------------------------------------------------------------------- +-- Making far branches + +-- Conditional branches on PowerPC are limited to +-32KB; if our Procs get too +-- big, we have to work around this limitation. + +makeFarBranches :: [NatBasicBlock] -> [NatBasicBlock] + +#if powerpc_TARGET_ARCH +makeFarBranches blocks + | last blockAddresses < nearLimit = blocks + | otherwise = zipWith handleBlock blockAddresses blocks + where + blockAddresses = scanl (+) 0 $ map blockLen blocks + blockLen (BasicBlock _ instrs) = length instrs + + handleBlock addr (BasicBlock id instrs) + = BasicBlock id (zipWith makeFar [addr..] instrs) + + makeFar addr (BCC ALWAYS tgt) = BCC ALWAYS tgt + makeFar addr (BCC cond tgt) + | abs (addr - targetAddr) >= nearLimit + = BCCFAR cond tgt + | otherwise + = BCC cond tgt + where Just targetAddr = lookupUFM blockAddressMap tgt + makeFar addr other = other + + nearLimit = 7000 -- 8192 instructions are allowed; let's keep some + -- distance, as we have a few pseudo-insns that are + -- pretty-printed as multiple instructions, + -- and it's just not worth the effort to calculate + -- things exactly + + blockAddressMap = listToUFM $ zip (map blockId blocks) blockAddresses +#else +makeFarBranches = id +#endif + +-- ----------------------------------------------------------------------------- +-- Shortcut branches + +shortcutBranches :: DynFlags -> [NatCmmTop] -> [NatCmmTop] +shortcutBranches dflags tops + | optLevel dflags < 1 = tops -- only with -O or higher + | otherwise = map (apply_mapping mapping) tops' + where + (tops', mappings) = mapAndUnzip build_mapping tops + mapping = foldr plusUFM emptyUFM mappings + +build_mapping top@(CmmData _ _) = (top, emptyUFM) +build_mapping (CmmProc info lbl params []) + = (CmmProc info lbl params [], emptyUFM) +build_mapping (CmmProc info lbl params (head:blocks)) + = (CmmProc info lbl params (head:others), mapping) + -- drop the shorted blocks, but don't ever drop the first one, + -- because it is pointed to by a global label. + where + -- find all the blocks that just consist of a jump that can be + -- shorted. + (shortcut_blocks, others) = partitionWith split blocks + split (BasicBlock id [insn]) | Just dest <- canShortcut insn + = Left (id,dest) + split other = Right other + + -- build a mapping from BlockId to JumpDest for shorting branches + mapping = foldl add emptyUFM shortcut_blocks + add ufm (id,dest) = addToUFM ufm id dest + +apply_mapping ufm (CmmData sec statics) + = CmmData sec (map (shortcutStatic (lookupUFM ufm)) statics) + -- we need to get the jump tables, so apply the mapping to the entries + -- of a CmmData too. +apply_mapping ufm (CmmProc info lbl params blocks) + = CmmProc info lbl params (map short_bb blocks) + where + short_bb (BasicBlock id insns) = BasicBlock id $! map short_insn insns + short_insn i = shortcutJump (lookupUFM ufm) i + -- shortcutJump should apply the mapping repeatedly, + -- just in case we can short multiple branches. + -- ----------------------------------------------------------------------------- -- Instruction selection @@ -307,7 +390,7 @@ reorder id accum (b@(block,id',out) : rest) -- Switching between the two monads whilst carrying along the same -- Unique supply breaks abstraction. Is that bad? -genMachCode :: CmmTop -> UniqSM ([NatCmmTop], [CLabel]) +genMachCode :: RawCmmTop -> UniqSM ([NatCmmTop], [CLabel]) genMachCode cmm_top = do { initial_us <- getUs @@ -329,7 +412,7 @@ genMachCode cmm_top -- the generic optimiser below, to avoid having two separate passes -- over the Cmm. -fixAssignsTop :: CmmTop -> UniqSM CmmTop +fixAssignsTop :: RawCmmTop -> UniqSM RawCmmTop fixAssignsTop top@(CmmData _ _) = returnUs top fixAssignsTop (CmmProc info lbl params blocks) = mapUs fixAssignsBlock blocks `thenUs` \ blocks' -> @@ -346,9 +429,6 @@ fixAssigns stmts = returnUs (concat stmtss) fixAssign :: CmmStmt -> UniqSM [CmmStmt] -fixAssign (CmmAssign (CmmGlobal BaseReg) src) - = panic "cmmStmtConFold: assignment to BaseReg"; - fixAssign (CmmAssign (CmmGlobal reg) src) | Left realreg <- reg_or_addr = returnUs [CmmAssign (CmmGlobal reg) src] @@ -361,27 +441,6 @@ fixAssign (CmmAssign (CmmGlobal reg) src) where reg_or_addr = get_GlobalReg_reg_or_addr reg -fixAssign (CmmCall target results args vols) - = mapAndUnzipUs fixResult results `thenUs` \ (results',stores) -> - returnUs (caller_save ++ - CmmCall target results' args vols : - caller_restore ++ - concat stores) - where - -- we also save/restore any caller-saves STG registers here - (caller_save, caller_restore) = callerSaveVolatileRegs vols - - fixResult g@(CmmGlobal reg,hint) = - case get_GlobalReg_reg_or_addr reg of - Left realreg -> returnUs (g, []) - Right baseRegAddr -> - getUniqueUs `thenUs` \ uq -> - let local = CmmLocal (LocalReg uq (globalRegRep reg)) in - returnUs ((local,hint), - [CmmStore baseRegAddr (CmmReg local)]) - fixResult other = - returnUs (other,[]) - fixAssign other_stmt = returnUs [other_stmt] -- ----------------------------------------------------------------------------- @@ -410,7 +469,7 @@ Ideas for other things we could do (ToDo): temp assignments, and certain assigns to mem...) -} -cmmToCmm :: CmmTop -> (CmmTop, [CLabel]) +cmmToCmm :: RawCmmTop -> (RawCmmTop, [CLabel]) cmmToCmm top@(CmmData _ _) = (top, []) cmmToCmm (CmmProc info lbl params blocks) = runCmmOpt $ do blocks' <- mapM cmmBlockConFold (cmmMiniInline blocks) @@ -442,33 +501,33 @@ cmmBlockConFold (BasicBlock id stmts) = do cmmStmtConFold stmt = case stmt of CmmAssign reg src - -> do src' <- cmmExprConFold False src + -> do src' <- cmmExprConFold DataReference src return $ case src' of CmmReg reg' | reg == reg' -> CmmNop new_src -> CmmAssign reg new_src CmmStore addr src - -> do addr' <- cmmExprConFold False addr - src' <- cmmExprConFold False src + -> do addr' <- cmmExprConFold DataReference addr + src' <- cmmExprConFold DataReference src return $ CmmStore addr' src' CmmJump addr regs - -> do addr' <- cmmExprConFold True addr + -> do addr' <- cmmExprConFold JumpReference addr return $ CmmJump addr' regs - CmmCall target regs args vols + CmmCall target regs args srt -> do target' <- case target of CmmForeignCall e conv -> do - e' <- cmmExprConFold True e + e' <- cmmExprConFold CallReference e return $ CmmForeignCall e' conv other -> return other args' <- mapM (\(arg, hint) -> do - arg' <- cmmExprConFold False arg + arg' <- cmmExprConFold DataReference arg return (arg', hint)) args - return $ CmmCall target' regs args' vols + return $ CmmCall target' regs args' srt CmmCondBranch test dest - -> do test' <- cmmExprConFold False test + -> do test' <- cmmExprConFold DataReference test return $ case test' of CmmLit (CmmInt 0 _) -> CmmComment (mkFastString ("deleted: " ++ @@ -478,29 +537,29 @@ cmmStmtConFold stmt other -> CmmCondBranch test' dest CmmSwitch expr ids - -> do expr' <- cmmExprConFold False expr + -> do expr' <- cmmExprConFold DataReference expr return $ CmmSwitch expr' ids other -> return other -cmmExprConFold isJumpTarget expr +cmmExprConFold referenceKind expr = case expr of CmmLoad addr rep - -> do addr' <- cmmExprConFold False addr + -> do addr' <- cmmExprConFold DataReference addr return $ CmmLoad addr' rep CmmMachOp mop args -- For MachOps, we first optimize the children, and then we try -- our hand at some constant-folding. - -> do args' <- mapM (cmmExprConFold False) args + -> do args' <- mapM (cmmExprConFold DataReference) args return $ cmmMachOpFold mop args' CmmLit (CmmLabel lbl) - -> cmmMakeDynamicReference addImportCmmOpt isJumpTarget lbl + -> cmmMakeDynamicReference addImportCmmOpt referenceKind lbl CmmLit (CmmLabelOff lbl off) - -> do dynRef <- cmmMakeDynamicReference addImportCmmOpt isJumpTarget lbl + -> do dynRef <- cmmMakeDynamicReference addImportCmmOpt referenceKind lbl return $ cmmMachOpFold (MO_Add wordRep) [ dynRef, (CmmLit $ CmmInt (fromIntegral off) wordRep) @@ -512,11 +571,11 @@ cmmExprConFold isJumpTarget expr -- with the corresponding labels: CmmReg (CmmGlobal GCEnter1) | not opt_PIC - -> cmmExprConFold isJumpTarget $ + -> cmmExprConFold referenceKind $ CmmLit (CmmLabel (mkRtsCodeLabel SLIT( "__stg_gc_enter_1"))) CmmReg (CmmGlobal GCFun) | not opt_PIC - -> cmmExprConFold isJumpTarget $ + -> cmmExprConFold referenceKind $ CmmLit (CmmLabel (mkRtsCodeLabel SLIT( "__stg_gc_fun"))) #endif @@ -531,12 +590,12 @@ cmmExprConFold isJumpTarget expr Left realreg -> return expr Right baseRegAddr -> case mid of - BaseReg -> cmmExprConFold False baseRegAddr - other -> cmmExprConFold False (CmmLoad baseRegAddr - (globalRegRep mid)) + BaseReg -> cmmExprConFold DataReference baseRegAddr + other -> cmmExprConFold DataReference + (CmmLoad baseRegAddr (globalRegRep mid)) -- eliminate zero offsets CmmRegOff reg 0 - -> cmmExprConFold False (CmmReg reg) + -> cmmExprConFold referenceKind (CmmReg reg) CmmRegOff (CmmGlobal mid) offset -- RegOf leaves are just a shorthand form. If the reg maps @@ -545,7 +604,7 @@ cmmExprConFold isJumpTarget expr -> case get_GlobalReg_reg_or_addr mid of Left realreg -> return expr Right baseRegAddr - -> cmmExprConFold False (CmmMachOp (MO_Add wordRep) [ + -> cmmExprConFold DataReference (CmmMachOp (MO_Add wordRep) [ CmmReg (CmmGlobal mid), CmmLit (CmmInt (fromIntegral offset) wordRep)])