X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=compiler%2Fcmm%2FCmmLive.hs;h=ed659776a8e7bbff11e6a75a48514124f2029867;hb=8a9eb3cd35117c62ac9758d118c6f4109b7330cb;hp=8d13505d36d86c59eed4ecdd883a94c115a3d784;hpb=b3ccd6d5a4366dc8089fd9c49f5edf43077de009;p=ghc-hetmet.git diff --git a/compiler/cmm/CmmLive.hs b/compiler/cmm/CmmLive.hs index 8d13505..ed65977 100644 --- a/compiler/cmm/CmmLive.hs +++ b/compiler/cmm/CmmLive.hs @@ -1,43 +1,54 @@ module CmmLive ( - CmmLive, BlockEntryLiveness, + CmmLive, + BlockEntryLiveness, cmmLiveness, - cmmFormalsToLiveLocals + cmmFormalsToLiveLocals, ) where #include "HsVersions.h" +import BlockId import Cmm import Dataflow import Maybes import Panic -import UniqFM import UniqSet -import Data.List - ----------------------------------------------------------------------------- -- Calculating what variables are live on entry to a basic block ----------------------------------------------------------------------------- --- The variables live on entry to a block +-- | The variables live on entry to a block type CmmLive = UniqSet LocalReg --- A mapping from block labels to the variables live on entry +-- | A mapping from block labels to the variables live on entry type BlockEntryLiveness = BlockEnv CmmLive +-- | A mapping from block labels to the blocks that target it +type BlockSources = BlockEnv (UniqSet BlockId) + +-- | A mapping from block labels to the statements in the block +type BlockStmts = BlockEnv [CmmStmt] + ----------------------------------------------------------------------------- --- cmmLiveness and helpers +-- | Calculated liveness info for a list of 'CmmBasicBlock' ----------------------------------------------------------------------------- cmmLiveness :: [CmmBasicBlock] -> BlockEntryLiveness cmmLiveness blocks = fixedpoint (cmmBlockDependants sources) (cmmBlockUpdate blocks') (map blockId blocks) - (listToUFM [(blockId b, emptyUniqSet) | b <- blocks]) + (mkBlockEnv [(blockId b, emptyUniqSet) | b <- blocks]) where + sources :: BlockSources sources = cmmBlockSources blocks - blocks' = cmmBlockNames blocks + + blocks' :: BlockStmts + blocks' = mkBlockEnv $ map block_name blocks + + block_name :: CmmBasicBlock -> (BlockId, [CmmStmt]) + block_name b = (blockId b, blockStmts b) {- -- For debugging, annotate each block with a comment indicating @@ -51,29 +62,26 @@ cmmLivenessComment live (BasicBlock ident stmts) = -} --------------------------------- --- cmmBlockSources --- --- Calculates a table of blocks --- that might need updating after --- a given block is updated --------------------------------- -cmmBlockSources :: [CmmBasicBlock] -> BlockEnv (UniqSet BlockId) -cmmBlockSources blocks = foldr aux emptyUFM blocks +----------------------------------------------------------------------------- +-- | Calculates a table of where one can lookup the blocks that might +-- need updating after a given block is updated in the liveness analysis +----------------------------------------------------------------------------- +cmmBlockSources :: [CmmBasicBlock] -> BlockSources +cmmBlockSources blocks = foldr aux emptyBlockEnv blocks where aux :: CmmBasicBlock - -> BlockEnv (UniqSet BlockId) - -> BlockEnv (UniqSet BlockId) + -> BlockSources + -> BlockSources aux block sourcesUFM = foldUniqSet (add_source_edges $ blockId block) sourcesUFM (branch_targets $ blockStmts block) add_source_edges :: BlockId -> BlockId - -> BlockEnv (UniqSet BlockId) - -> BlockEnv (UniqSet BlockId) + -> BlockSources + -> BlockSources add_source_edges source target ufm = - addToUFM_Acc (flip addOneToUniqSet) unitUniqSet ufm target source + addToBEnv_Acc (flip addOneToUniqSet) unitUniqSet ufm target source branch_targets :: [CmmStmt] -> UniqSet BlockId branch_targets stmts = @@ -83,40 +91,22 @@ cmmBlockSources blocks = foldr aux emptyUFM blocks target (CmmSwitch _ blocks) = mapMaybe id blocks target _ = [] --------------------------------- --- cmmBlockNames --- --- Calculates a table that maps --- block names to the list --- of statements inside them --------------------------------- -cmmBlockNames :: [CmmBasicBlock] -> BlockEnv [CmmStmt] -cmmBlockNames blocks = listToUFM $ map block_name blocks where - block_name b = (blockId b, blockStmts b) - --------------------------------- --- cmmBlockDependants +----------------------------------------------------------------------------- +-- | Given the table calculated by 'cmmBlockSources', list all blocks +-- that depend on the result of a particular block. -- --- Given the table calculated --- by cmmBlockSources created, --- list all blocks that depend --- on the result of a particular --- block. --------------------------------- -cmmBlockDependants :: BlockEnv (UniqSet BlockId) -> BlockId -> [BlockId] +-- Used by the call to 'fixedpoint'. +----------------------------------------------------------------------------- +cmmBlockDependants :: BlockSources -> BlockId -> [BlockId] cmmBlockDependants sources ident = - uniqSetToList $ lookupWithDefaultUFM sources emptyUniqSet ident + uniqSetToList $ lookupWithDefaultBEnv sources emptyUniqSet ident --------------------------------- --- cmmBlockUpdate --- --- Given the table from --- cmmBlockNames and a block --- that was updated, calculate --- an updated BlockEntryLiveness --------------------------------- +----------------------------------------------------------------------------- +-- | Given the table of type 'BlockStmts' and a block that was updated, +-- calculate an updated BlockEntryLiveness +----------------------------------------------------------------------------- cmmBlockUpdate :: - BlockEnv [CmmStmt] + BlockStmts -> BlockId -> Maybe BlockId -> BlockEntryLiveness @@ -124,15 +114,22 @@ cmmBlockUpdate :: cmmBlockUpdate blocks node _ state = if (sizeUniqSet old_live) == (sizeUniqSet new_live) then Nothing - else Just $ addToUFM state node new_live + else Just $ extendBlockEnv state node new_live where - new_live = cmmStmtListLive state block - old_live = lookupWithDefaultUFM state missing_live node - block = lookupWithDefaultUFM blocks missing_block node + new_live, old_live :: CmmLive + new_live = cmmStmtListLive state block_stmts + old_live = lookupWithDefaultBEnv state missing_live node + + block_stmts :: [CmmStmt] + block_stmts = lookupWithDefaultBEnv blocks missing_block node + missing_live = panic "unknown block id during liveness analysis" missing_block = panic "unknown block id during liveness analysis" ----------------------------------------------------------------------------- +-- Section: +----------------------------------------------------------------------------- +----------------------------------------------------------------------------- -- CmmBlockLive, cmmStmtListLive and helpers ----------------------------------------------------------------------------- @@ -159,10 +156,8 @@ addKilled new_killed live = live `minusUniqSet` new_killed -------------------------------- -- Liveness of a CmmStmt -------------------------------- -cmmFormalsToLiveLocals :: CmmFormals -> [LocalReg] -cmmFormalsToLiveLocals [] = [] -cmmFormalsToLiveLocals ((CmmGlobal _,_):args) = cmmFormalsToLiveLocals args -cmmFormalsToLiveLocals ((CmmLocal r,_):args) = r:cmmFormalsToLiveLocals args +cmmFormalsToLiveLocals :: HintedCmmFormals -> [LocalReg] +cmmFormalsToLiveLocals formals = map hintlessCmm formals cmmStmtLive :: BlockEntryLiveness -> CmmStmt -> CmmLivenessTransformer cmmStmtLive _ (CmmNop) = id @@ -175,29 +170,29 @@ cmmStmtLive _ (CmmAssign reg expr) = (CmmGlobal _) -> id cmmStmtLive _ (CmmStore expr1 expr2) = cmmExprLive expr2 . cmmExprLive expr1 -cmmStmtLive _ (CmmCall target results arguments _) = +cmmStmtLive _ (CmmCall target results arguments _ _) = target_liveness . - foldr ((.) . cmmExprLive) id (map fst arguments) . + foldr ((.) . cmmExprLive) id (map hintlessCmm arguments) . addKilled (mkUniqSet $ cmmFormalsToLiveLocals results) where target_liveness = case target of - (CmmForeignCall target _) -> cmmExprLive target + (CmmCallee target _) -> cmmExprLive target (CmmPrim _) -> id cmmStmtLive other_live (CmmBranch target) = - addLive (lookupWithDefaultUFM other_live emptyUniqSet target) + addLive (lookupWithDefaultBEnv other_live emptyUniqSet target) cmmStmtLive other_live (CmmCondBranch expr target) = cmmExprLive expr . - addLive (lookupWithDefaultUFM other_live emptyUniqSet target) + addLive (lookupWithDefaultBEnv other_live emptyUniqSet target) cmmStmtLive other_live (CmmSwitch expr targets) = cmmExprLive expr . (foldr ((.) . (addLive . - lookupWithDefaultUFM other_live emptyUniqSet)) + lookupWithDefaultBEnv other_live emptyUniqSet)) id (mapCatMaybes id targets)) cmmStmtLive _ (CmmJump expr params) = - const (cmmExprLive expr $ foldr ((.) . cmmExprLive) id (map fst params) $ emptyUniqSet) + const (cmmExprLive expr $ foldr ((.) . cmmExprLive) id (map hintlessCmm params) $ emptyUniqSet) cmmStmtLive _ (CmmReturn params) = - const (foldr ((.) . cmmExprLive) id (map fst params) $ emptyUniqSet) + const (foldr ((.) . cmmExprLive) id (map hintlessCmm params) $ emptyUniqSet) -------------------------------- -- Liveness of a CmmExpr @@ -210,6 +205,7 @@ cmmExprLive expr = addLive (mkUniqSet $ expr_liveness expr) where expr_liveness (CmmReg reg) = reg_liveness reg expr_liveness (CmmMachOp _ exprs) = concatMap expr_liveness exprs expr_liveness (CmmRegOff reg _) = reg_liveness reg + expr_liveness (CmmStackSlot _ _) = panic "cmmExprLive CmmStackSlot" reg_liveness :: CmmReg -> [LocalReg] reg_liveness (CmmLocal reg) = [reg]