CmmContFlowOpt.hs 9.59 KB
Newer Older
1

2 3
module CmmContFlowOpt
    ( runCmmOpts, cmmCfgOpts, cmmCfgOptsZ
dias@eecs.harvard.edu's avatar
dias@eecs.harvard.edu committed
4
    , branchChainElimZ, removeUnreachableBlocksZ, predMap
5
    , replaceLabelsZ, replaceBranches, runCmmContFlowOptsZs
6 7 8
    )
where

9
import BlockId
10 11 12
import Cmm
import CmmTx
import qualified ZipCfg as G
13
import ZipCfg
14
import ZipCfgCmmRep
dias@eecs.harvard.edu's avatar
dias@eecs.harvard.edu committed
15

16
import Maybes
Ian Lynagh's avatar
Ian Lynagh committed
17
import Control.Monad
18
import Outputable
dias@eecs.harvard.edu's avatar
dias@eecs.harvard.edu committed
19
import Prelude hiding (unzip, zip)
20 21 22
import Util

------------------------------------
23 24 25 26
runCmmContFlowOptsZs :: [CmmZ] -> [CmmZ]
runCmmContFlowOptsZs prog
  = [ runTx (runCmmOpts cmmCfgOptsZ) cmm_top
    | cmm_top <- prog ]
27 28

cmmCfgOpts  :: Tx (ListGraph CmmStmt)
29
cmmCfgOptsZ :: Tx (a, CmmGraph)
30 31

cmmCfgOpts  = branchChainElim  -- boring, but will get more exciting later
32
cmmCfgOptsZ g =
33
  optGraph
34
    (branchChainElimZ `seqTx` blockConcatZ `seqTx` removeUnreachableBlocksZ) g
35 36 37 38
        -- Here branchChainElim can ultimately be replaced
        -- with a more exciting combination of optimisations

runCmmOpts :: Tx g -> Tx (GenCmm d h g)
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
39
-- Lifts a transformer on a single graph to one on the whole program
40
runCmmOpts opt = mapProcs (optProc opt)
41

42 43 44 45 46 47 48
optProc :: Tx g -> Tx (GenCmmTop d h g)
optProc _   top@(CmmData {}) = noTx top
optProc opt (CmmProc info lbl formals g) =
  fmap (CmmProc info lbl formals) (opt g)

optGraph :: Tx g -> Tx (a, g)
optGraph opt (a, g) = fmap (\g' -> (a, g')) (opt g)
49

50 51 52 53
------------------------------------
mapProcs :: Tx (GenCmmTop d h s) -> Tx (GenCmm d h s)
mapProcs f (Cmm tops) = fmap Cmm (mapTx f tops)

54 55
----------------------------------------------------------------
branchChainElim :: Tx (ListGraph CmmStmt)
56 57 58
-- If L is not captured in an instruction, we can remove any
-- basic block of the form L: goto L', and replace L with L' everywhere else.
-- How does L get captured? In a CallArea.
59 60 61 62 63 64 65 66 67 68 69 70 71
branchChainElim (ListGraph blocks)
  | null lone_branch_blocks     -- No blocks to remove
  = noTx (ListGraph blocks)
  | otherwise
  = aTx (ListGraph new_blocks)
  where
    (lone_branch_blocks, others) = partitionWith isLoneBranch blocks
    new_blocks = map (replaceLabels env) others
    env = mkClosureBlockEnv lone_branch_blocks

isLoneBranch :: CmmBasicBlock -> Either (BlockId, BlockId) CmmBasicBlock
isLoneBranch (BasicBlock id [CmmBranch target]) | id /= target = Left (id, target)
isLoneBranch other_block                                       = Right other_block
Thomas Schilling's avatar
Thomas Schilling committed
72
   -- An infinite loop is not a link in a branch chain!
73 74 75 76 77 78 79 80 81 82 83 84 85 86

replaceLabels :: BlockEnv BlockId -> CmmBasicBlock -> CmmBasicBlock
replaceLabels env (BasicBlock id stmts)
  = BasicBlock id (map replace stmts)
  where
    replace (CmmBranch id)       = CmmBranch (lookup id)
    replace (CmmCondBranch e id) = CmmCondBranch e (lookup id)
    replace (CmmSwitch e tbl)    = CmmSwitch e (map (fmap lookup) tbl)
    replace other_stmt           = other_stmt

    lookup id = lookupBlockEnv env id `orElse` id 
----------------------------------------------------------------
branchChainElimZ :: Tx CmmGraph
-- Remove any basic block of the form L: goto L',
87 88 89 90 91 92 93 94 95 96
-- and replace L with L' everywhere else,
-- unless L is the successor of a call instruction and L'
-- is the entry block. You don't want to set the successor
-- of a function call to the entry block because there is no good way
-- to store both the infotables for the call and from the callee,
-- while putting the stack pointer in a consistent place.
--
-- JD isn't quite sure when it's safe to share continuations for different
-- function calls -- have to think about where the SP will be,
-- so we'll table that problem for now by leaving all call successors alone.
97
branchChainElimZ g@(G.LGraph eid _)
98 99 100
  | null lone_branch_blocks     -- No blocks to remove
  = noTx g
  | otherwise
101
  = aTx $ replaceLabelsZ env $ G.of_block_list eid (self_branches ++ others)
102
  where
103 104
    blocks = G.to_block_list g
    (lone_branch_blocks, others) = partitionWith isLoneBranchZ blocks
105
    env = mkClosureBlockEnvZ lone_branch_blocks
106
    self_branches =
107 108
      let loop_to (id, _) =
            if lookup id == id then
109
              Just (G.Block id (G.ZLast (G.mkBranchNode id)))
110 111 112
            else
              Nothing
      in  mapMaybe loop_to lone_branch_blocks
dias@eecs.harvard.edu's avatar
dias@eecs.harvard.edu committed
113
    lookup id = lookupBlockEnv env id `orElse` id 
114

115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
    call_succs = foldl add emptyBlockSet blocks
      where add succs b =
              case G.last (G.unzip b) of
                LastOther (LastCall _ (Just k) _ _ _) -> extendBlockSet succs k
                _ -> succs
    isLoneBranchZ :: CmmBlock -> Either (BlockId, BlockId) CmmBlock
    isLoneBranchZ (G.Block id (G.ZLast (G.LastOther (LastBranch target))))
        | id /= target && not (elemBlockSet id call_succs) = Left (id,target)
    isLoneBranchZ other = Right other
       -- An infinite loop is not a link in a branch chain!

maybeReplaceLabels :: (Last -> Bool) -> BlockEnv BlockId -> CmmGraph -> CmmGraph
maybeReplaceLabels lpred env =
  replace_eid . G.map_nodes id middle last
   where
     replace_eid (G.LGraph eid blocks) = G.LGraph (lookup eid) blocks
     middle = mapExpDeepMiddle exp
     last l = if lpred l then mapExpDeepLast exp (last' l) else l
     last' (LastBranch bid) = LastBranch (lookup bid)
     last' (LastCondBranch p t f) = LastCondBranch p (lookup t) (lookup f)
     last' (LastSwitch e arms) = LastSwitch e (map (liftM lookup) arms)
     last' (LastCall t k a res r) = LastCall t (liftM lookup k) a res r
     exp (CmmLit (CmmBlock bid)) = CmmLit (CmmBlock (lookup bid))
     exp   (CmmStackSlot (CallArea (Young id)) i) =
       CmmStackSlot (CallArea (Young (lookup id))) i
     exp e = e
     lookup id = fmap lookup (lookupBlockEnv env id) `orElse` id 
142

dias@eecs.harvard.edu's avatar
dias@eecs.harvard.edu committed
143
replaceLabelsZ :: BlockEnv BlockId -> CmmGraph -> CmmGraph
144 145 146 147 148 149
replaceLabelsZ = maybeReplaceLabels (const True)

-- replaceBranchLabels :: BlockEnv BlockId -> CmmGraph -> CmmGraph
-- replaceBranchLabels env g@(LGraph _ _) = maybeReplaceLabels lpred env g
--   where lpred (LastBranch _) = True
--         lpred _ = False
150 151 152 153 154 155 156 157

replaceBranches :: BlockEnv BlockId -> CmmGraph -> CmmGraph
replaceBranches env g = map_nodes id id last g
  where
    last (LastBranch id)          = LastBranch (lookup id)
    last (LastCondBranch e ti fi) = LastCondBranch e (lookup ti) (lookup fi)
    last (LastSwitch e tbl)       = LastSwitch e (map (fmap lookup) tbl)
    last l@(LastCall {})          = l
158
    lookup id = fmap lookup (lookupBlockEnv env id) `orElse` id 
dias@eecs.harvard.edu's avatar
dias@eecs.harvard.edu committed
159 160 161 162 163 164

----------------------------------------------------------------
-- Build a map from a block to its set of predecessors. Very useful.
predMap :: G.LastNode l => G.LGraph m l -> BlockEnv BlockSet
predMap g = G.fold_blocks add_preds emptyBlockEnv g -- find the back edges
  where add_preds b env = foldl (add b) env (G.succs b)
165
        add (G.Block bid _) env b' =
dias@eecs.harvard.edu's avatar
dias@eecs.harvard.edu committed
166 167 168
          extendBlockEnv env b' $
                extendBlockSet (lookupBlockEnv env b' `orElse` emptyBlockSet) bid
----------------------------------------------------------------
169 170
-- If a block B branches to a label L, L is not the entry block,
-- and L has no other predecessors,
dias@eecs.harvard.edu's avatar
dias@eecs.harvard.edu committed
171
-- then we can splice the block starting with L onto the end of B.
172
-- Because this optimization can be inhibited by unreachable blocks,
173
-- we first take a pass to drops unreachable blocks.
dias@eecs.harvard.edu's avatar
dias@eecs.harvard.edu committed
174
-- Order matters, so we work bottom up (reverse postorder DFS).
175 176
--
-- To ensure correctness, we have to make sure that the BlockId of the block
177
-- we are about to eliminate is not named in another instruction.
178
--
dias@eecs.harvard.edu's avatar
dias@eecs.harvard.edu committed
179
-- Note: This optimization does _not_ subsume branch chain elimination.
180 181
blockConcatZ  :: Tx CmmGraph
blockConcatZ = removeUnreachableBlocksZ `seqTx` blockConcatZ'
dias@eecs.harvard.edu's avatar
dias@eecs.harvard.edu committed
182
blockConcatZ' :: Tx CmmGraph
183 184
blockConcatZ' g@(G.LGraph eid blocks) =
  tx $ replaceLabelsZ concatMap $ G.LGraph eid blocks'
185 186
  where (changed, blocks', concatMap) =
           foldr maybe_concat (False, blocks, emptyBlockEnv) $ G.postorder_dfs g
187
        maybe_concat b@(G.Block bid _) (changed, blocks', concatMap) =
188
          let unchanged = (changed, extendBlockEnv blocks' bid b, concatMap)
dias@eecs.harvard.edu's avatar
dias@eecs.harvard.edu committed
189 190
          in case G.goto_end $ G.unzip b of
               (h, G.LastOther (LastBranch b')) ->
191
                  if canConcatWith b' then
192 193
                    (True, extendBlockEnv blocks' bid $ splice blocks' h b',
                     extendBlockEnv concatMap b' bid)
dias@eecs.harvard.edu's avatar
dias@eecs.harvard.edu committed
194 195 196
                  else unchanged
               _ -> unchanged
        num_preds bid = liftM sizeBlockSet (lookupBlockEnv backEdges bid) `orElse` 0
197
        canConcatWith b' = b' /= eid && num_preds b' == 1
dias@eecs.harvard.edu's avatar
dias@eecs.harvard.edu committed
198 199 200
        backEdges = predMap g
        splice blocks' h bid' =
          case lookupBlockEnv blocks' bid' of
201
            Just (G.Block _ t) -> G.zip $ G.ZBlock h t
202
            Nothing -> panic "unknown successor block"
dias@eecs.harvard.edu's avatar
dias@eecs.harvard.edu committed
203
        tx = if changed then aTx else noTx
204 205 206 207 208 209 210 211
----------------------------------------------------------------
mkClosureBlockEnv :: [(BlockId, BlockId)] -> BlockEnv BlockId
mkClosureBlockEnv blocks = mkBlockEnv $ map follow blocks
    where singleEnv = mkBlockEnv blocks
          follow (id, next) = (id, endChain id next)
          endChain orig id = case lookupBlockEnv singleEnv id of
                               Just id' | id /= orig -> endChain orig id'
                               _ -> id
212 213 214 215 216 217 218
mkClosureBlockEnvZ :: [(BlockId, BlockId)] -> BlockEnv BlockId
mkClosureBlockEnvZ blocks = mkBlockEnv $ map follow blocks
    where singleEnv = mkBlockEnv blocks
          follow (id, next) = (id, endChain id next)
          endChain orig id = case lookupBlockEnv singleEnv id of
                               Just id' | id /= orig -> endChain orig id'
                               _ -> id
219 220
----------------------------------------------------------------
removeUnreachableBlocksZ :: Tx CmmGraph
221 222
removeUnreachableBlocksZ g@(G.LGraph id blocks) =
  if length blocks' < sizeBEnv blocks then aTx $ G.of_block_list id blocks'
223
  else noTx g
224
    where blocks' = G.postorder_dfs g