1 module CmmBrokenBlock (
6 cmmBlockFromBrokenBlock,
10 #include "HsVersions.h"
20 -----------------------------------------------------------------------------
22 -----------------------------------------------------------------------------
24 -- |Similar to a 'CmmBlock' with a little extra information
25 -- to help the CPS analysis.
28 brokenBlockId :: BlockId, -- ^ The block's label like a 'CmmBasicBlock'
29 brokenBlockEntry :: BlockEntryInfo,
30 -- ^ Ways this block can be entered
32 brokenBlockStmts :: [CmmStmt],
33 -- ^ Body like a CmmBasicBlock
34 -- (but without the last statement)
36 brokenBlockTargets :: [BlockId],
37 -- ^ Blocks that this block could
38 -- branch to one either by conditional
39 -- branches or via the last statement
41 brokenBlockExit :: FinalStmt
42 -- ^ The final statement of the block
45 -- | How a block could be entered
47 = FunctionEntry -- ^ Block is the beginning of a function
48 CLabel -- ^ The function name
49 CmmFormals -- ^ Aguments to function
51 | ContinuationEntry -- ^ Return point of a function call
52 CmmFormals -- ^ return values (argument to continuation)
54 | ControlEntry -- ^ Any other kind of block.
55 -- Only entered due to control flow.
57 -- TODO: Consider adding ProcPointEntry
58 -- no return values, but some live might end up as
59 -- params or possibly in the frame
62 -- | Final statement in a 'BlokenBlock'.
63 -- Constructors and arguments match those in 'Cmm',
64 -- but are restricted to branches, returns, jumps, calls and switches
66 = FinalBranch -- ^ Same as 'CmmBranch'
67 BlockId -- ^ Target must be a ControlEntry
69 | FinalReturn -- ^ Same as 'CmmReturn'
70 CmmActuals -- ^ Return values
72 | FinalJump -- ^ Same as 'CmmJump'
73 CmmExpr -- ^ The function to call
74 CmmActuals -- ^ Arguments of the call
76 | FinalCall -- ^ Same as 'CmmForeignCall'
77 -- followed by 'CmmGoto'
78 BlockId -- ^ Target of the 'CmmGoto'
79 -- (must be a 'ContinuationEntry')
80 CmmCallTarget -- ^ The function to call
81 CmmFormals -- ^ Results from call
82 -- (redundant with ContinuationEntry)
83 CmmActuals -- ^ Arguments to call
85 | FinalSwitch -- ^ Same as a 'CmmSwitch'
86 CmmExpr -- ^ Scrutinee (zero based)
87 [Maybe BlockId] -- ^ Targets
89 -----------------------------------------------------------------------------
90 -- Operations for broken blocks
91 -----------------------------------------------------------------------------
93 -----------------------------------------------------------------------------
94 -- | Takes a 'CmmBasicBlock' and breaks it up into a list of 'BrokenBlock'
95 -- by splitting on each 'CmmCall' in the 'CmmBasicBlock'.
98 [Unique] -- ^ An infinite list of uniques
99 -- to create names of the new blocks with
100 -> CmmBasicBlock -- ^ Input block to break apart
101 -> BlockEntryInfo -- ^ Info for the first created 'BrokenBlock'
103 breakBlock uniques (BasicBlock ident stmts) entry =
104 breakBlock' uniques ident entry [] [] stmts
106 breakBlock' uniques current_id entry exits accum_stmts stmts =
108 [] -> panic "block doesn't end in jump, goto, return or switch"
109 [CmmJump target arguments] ->
110 [BrokenBlock current_id entry accum_stmts
112 (FinalJump target arguments)]
113 [CmmReturn arguments] ->
114 [BrokenBlock current_id entry accum_stmts
116 (FinalReturn arguments)]
117 [CmmBranch target] ->
118 [BrokenBlock current_id entry accum_stmts
120 (FinalBranch target)]
121 [CmmSwitch expr targets] ->
122 [BrokenBlock current_id entry accum_stmts
123 (mapMaybe id targets ++ exits)
124 (FinalSwitch expr targets)]
125 (CmmJump _ _:_) -> panic "jump in middle of block"
126 (CmmReturn _:_) -> panic "return in middle of block"
127 (CmmBranch _:_) -> panic "branch in middle of block"
128 (CmmSwitch _ _:_) -> panic "switch in middle of block"
130 -- Detect this special case to remain an inverse of
131 -- 'cmmBlockFromBrokenBlock'
132 {- TODO: Interferes with proc point detection
133 [CmmCall target results arguments,
134 CmmBranch next_id] -> [block]
136 block = do_call current_id entry accum_stmts exits next_id
137 target results arguments
139 (CmmCall target results arguments:stmts) -> block : rest
141 next_id = BlockId $ head uniques
142 block = do_call current_id entry accum_stmts exits next_id
143 target results arguments
144 rest = breakBlock' (tail uniques) next_id
145 (ContinuationEntry results) [] [] stmts
147 breakBlock' uniques current_id entry
148 (cond_branch_target s++exits)
152 do_call current_id entry accum_stmts exits next_id
153 target results arguments =
154 BrokenBlock current_id entry accum_stmts (next_id:exits)
155 (FinalCall next_id target results arguments)
157 cond_branch_target (CmmCondBranch _ target) = [target]
158 cond_branch_target _ = []
160 -----------------------------------------------------------------------------
161 -- | Convert from a BrokenBlock back to an equivalent CmmBasicBlock
162 -- Needed by liveness analysis
163 cmmBlockFromBrokenBlock :: BrokenBlock -> CmmBasicBlock
164 cmmBlockFromBrokenBlock (BrokenBlock ident _ stmts _ exit) =
165 BasicBlock ident (stmts++exit_stmt)
169 FinalBranch target -> [CmmBranch target]
170 FinalReturn arguments -> [CmmReturn arguments]
171 FinalJump target arguments -> [CmmJump target arguments]
172 FinalSwitch expr targets -> [CmmSwitch expr targets]
173 FinalCall branch_target call_target results arguments ->
174 [CmmCall call_target results arguments,
175 CmmBranch branch_target]
177 -----------------------------------------------------------------------------
178 -- | Build a mapping so we can lookup a 'BrokenBlock' by its 'BlockId'
179 blocksToBlockEnv :: [BrokenBlock] -> BlockEnv BrokenBlock
180 blocksToBlockEnv blocks = listToUFM $ map (\b -> (brokenBlockId b, b)) blocks