3
( runCmmOpts, cmmCfgOpts, cmmCfgOptsZ
4
, branchChainElimZ, removeUnreachableBlocksZ, predMap
5
, replaceLabelsZ, replaceBranches, runCmmContFlowOptsZs
12
import qualified ZipCfg as G
19
import Prelude hiding (unzip, zip)
22
------------------------------------
23
runCmmContFlowOptsZs :: [CmmZ] -> [CmmZ]
24
runCmmContFlowOptsZs prog
25
= [ runTx (runCmmOpts cmmCfgOptsZ) cmm_top
28
cmmCfgOpts :: Tx (ListGraph CmmStmt)
29
cmmCfgOptsZ :: Tx (a, CmmGraph)
31
cmmCfgOpts = branchChainElim -- boring, but will get more exciting later
34
(branchChainElimZ `seqTx` blockConcatZ `seqTx` removeUnreachableBlocksZ) g
35
-- Here branchChainElim can ultimately be replaced
36
-- with a more exciting combination of optimisations
38
runCmmOpts :: Tx g -> Tx (GenCmm d h g)
39
-- Lifts a transformer on a single graph to one on the whole program
40
runCmmOpts opt = mapProcs (optProc opt)
42
optProc :: Tx g -> Tx (GenCmmTop d h g)
43
optProc _ top@(CmmData {}) = noTx top
44
optProc opt (CmmProc info lbl formals g) =
45
fmap (CmmProc info lbl formals) (opt g)
47
optGraph :: Tx g -> Tx (a, g)
48
optGraph opt (a, g) = fmap (\g' -> (a, g')) (opt g)
50
------------------------------------
51
mapProcs :: Tx (GenCmmTop d h s) -> Tx (GenCmm d h s)
52
mapProcs f (Cmm tops) = fmap Cmm (mapTx f tops)
54
----------------------------------------------------------------
55
branchChainElim :: Tx (ListGraph CmmStmt)
56
-- If L is not captured in an instruction, we can remove any
57
-- basic block of the form L: goto L', and replace L with L' everywhere else.
58
-- How does L get captured? In a CallArea.
59
branchChainElim (ListGraph blocks)
60
| null lone_branch_blocks -- No blocks to remove
61
= noTx (ListGraph blocks)
63
= aTx (ListGraph new_blocks)
65
(lone_branch_blocks, others) = partitionWith isLoneBranch blocks
66
new_blocks = map (replaceLabels env) others
67
env = mkClosureBlockEnv lone_branch_blocks
69
isLoneBranch :: CmmBasicBlock -> Either (BlockId, BlockId) CmmBasicBlock
70
isLoneBranch (BasicBlock id [CmmBranch target]) | id /= target = Left (id, target)
71
isLoneBranch other_block = Right other_block
72
-- An infinite loop is not a link in a branch chain!
74
replaceLabels :: BlockEnv BlockId -> CmmBasicBlock -> CmmBasicBlock
75
replaceLabels env (BasicBlock id stmts)
76
= BasicBlock id (map replace stmts)
78
replace (CmmBranch id) = CmmBranch (lookup id)
79
replace (CmmCondBranch e id) = CmmCondBranch e (lookup id)
80
replace (CmmSwitch e tbl) = CmmSwitch e (map (fmap lookup) tbl)
81
replace other_stmt = other_stmt
83
lookup id = lookupBlockEnv env id `orElse` id
84
----------------------------------------------------------------
85
branchChainElimZ :: Tx CmmGraph
86
-- Remove any basic block of the form L: goto L',
87
-- and replace L with L' everywhere else,
88
-- unless L is the successor of a call instruction and L'
89
-- is the entry block. You don't want to set the successor
90
-- of a function call to the entry block because there is no good way
91
-- to store both the infotables for the call and from the callee,
92
-- while putting the stack pointer in a consistent place.
94
-- JD isn't quite sure when it's safe to share continuations for different
95
-- function calls -- have to think about where the SP will be,
96
-- so we'll table that problem for now by leaving all call successors alone.
97
branchChainElimZ g@(G.LGraph eid _)
98
| null lone_branch_blocks -- No blocks to remove
101
= aTx $ replaceLabelsZ env $ G.of_block_list eid (self_branches ++ others)
103
blocks = G.to_block_list g
104
(lone_branch_blocks, others) = partitionWith isLoneBranchZ blocks
105
env = mkClosureBlockEnvZ lone_branch_blocks
107
let loop_to (id, _) =
108
if lookup id == id then
109
Just (G.Block id (G.ZLast (G.mkBranchNode id)))
112
in mapMaybe loop_to lone_branch_blocks
113
lookup id = lookupBlockEnv env id `orElse` id
115
call_succs = foldl add emptyBlockSet blocks
117
case G.last (G.unzip b) of
118
LastOther (LastCall _ (Just k) _ _ _) -> extendBlockSet succs k
120
isLoneBranchZ :: CmmBlock -> Either (BlockId, BlockId) CmmBlock
121
isLoneBranchZ (G.Block id (G.ZLast (G.LastOther (LastBranch target))))
122
| id /= target && not (elemBlockSet id call_succs) = Left (id,target)
123
isLoneBranchZ other = Right other
124
-- An infinite loop is not a link in a branch chain!
126
maybeReplaceLabels :: (Last -> Bool) -> BlockEnv BlockId -> CmmGraph -> CmmGraph
127
maybeReplaceLabels lpred env =
128
replace_eid . G.map_nodes id middle last
130
replace_eid (G.LGraph eid blocks) = G.LGraph (lookup eid) blocks
131
middle = mapExpDeepMiddle exp
132
last l = if lpred l then mapExpDeepLast exp (last' l) else l
133
last' (LastBranch bid) = LastBranch (lookup bid)
134
last' (LastCondBranch p t f) = LastCondBranch p (lookup t) (lookup f)
135
last' (LastSwitch e arms) = LastSwitch e (map (liftM lookup) arms)
136
last' (LastCall t k a res r) = LastCall t (liftM lookup k) a res r
137
exp (CmmLit (CmmBlock bid)) = CmmLit (CmmBlock (lookup bid))
138
exp (CmmStackSlot (CallArea (Young id)) i) =
139
CmmStackSlot (CallArea (Young (lookup id))) i
141
lookup id = fmap lookup (lookupBlockEnv env id) `orElse` id
143
replaceLabelsZ :: BlockEnv BlockId -> CmmGraph -> CmmGraph
144
replaceLabelsZ = maybeReplaceLabels (const True)
146
-- replaceBranchLabels :: BlockEnv BlockId -> CmmGraph -> CmmGraph
147
-- replaceBranchLabels env g@(LGraph _ _) = maybeReplaceLabels lpred env g
148
-- where lpred (LastBranch _) = True
151
replaceBranches :: BlockEnv BlockId -> CmmGraph -> CmmGraph
152
replaceBranches env g = map_nodes id id last g
154
last (LastBranch id) = LastBranch (lookup id)
155
last (LastCondBranch e ti fi) = LastCondBranch e (lookup ti) (lookup fi)
156
last (LastSwitch e tbl) = LastSwitch e (map (fmap lookup) tbl)
157
last l@(LastCall {}) = l
158
lookup id = fmap lookup (lookupBlockEnv env id) `orElse` id
160
----------------------------------------------------------------
161
-- Build a map from a block to its set of predecessors. Very useful.
162
predMap :: G.LastNode l => G.LGraph m l -> BlockEnv BlockSet
163
predMap g = G.fold_blocks add_preds emptyBlockEnv g -- find the back edges
164
where add_preds b env = foldl (add b) env (G.succs b)
165
add (G.Block bid _) env b' =
166
extendBlockEnv env b' $
167
extendBlockSet (lookupBlockEnv env b' `orElse` emptyBlockSet) bid
168
----------------------------------------------------------------
169
-- If a block B branches to a label L, L is not the entry block,
170
-- and L has no other predecessors,
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.
174
-- Order matters, so we work bottom up (reverse postorder DFS).
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.
179
-- Note: This optimization does _not_ subsume branch chain elimination.
180
blockConcatZ :: Tx CmmGraph
181
blockConcatZ = removeUnreachableBlocksZ `seqTx` blockConcatZ'
182
blockConcatZ' :: Tx CmmGraph
183
blockConcatZ' g@(G.LGraph eid blocks) =
184
tx $ replaceLabelsZ concatMap $ G.LGraph eid blocks'
185
where (changed, blocks', concatMap) =
186
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)
189
in case G.goto_end $ G.unzip b of
190
(h, G.LastOther (LastBranch b')) ->
191
if canConcatWith b' then
192
(True, extendBlockEnv blocks' bid $ splice blocks' h b',
193
extendBlockEnv concatMap b' bid)
196
num_preds bid = liftM sizeBlockSet (lookupBlockEnv backEdges bid) `orElse` 0
197
canConcatWith b' = b' /= eid && num_preds b' == 1
198
backEdges = predMap g
199
splice blocks' h bid' =
200
case lookupBlockEnv blocks' bid' of
201
Just (G.Block _ t) -> G.zip $ G.ZBlock h t
202
Nothing -> panic "unknown successor block"
203
tx = if changed then aTx else noTx
204
----------------------------------------------------------------
205
mkClosureBlockEnv :: [(BlockId, BlockId)] -> BlockEnv BlockId
206
mkClosureBlockEnv blocks = mkBlockEnv $ map follow blocks
207
where singleEnv = mkBlockEnv blocks
208
follow (id, next) = (id, endChain id next)
209
endChain orig id = case lookupBlockEnv singleEnv id of
210
Just id' | id /= orig -> endChain orig id'
212
mkClosureBlockEnvZ :: [(BlockId, BlockId)] -> BlockEnv BlockId
213
mkClosureBlockEnvZ blocks = mkBlockEnv $ map follow blocks
214
where singleEnv = mkBlockEnv blocks
215
follow (id, next) = (id, endChain id next)
216
endChain orig id = case lookupBlockEnv singleEnv id of
217
Just id' | id /= orig -> endChain orig id'
219
----------------------------------------------------------------
220
removeUnreachableBlocksZ :: Tx CmmGraph
221
removeUnreachableBlocksZ g@(G.LGraph id blocks) =
222
if length blocks' < sizeBEnv blocks then aTx $ G.of_block_list id blocks'
224
where blocks' = G.postorder_dfs g