| 1 |
(* weighted-block-placement-fn.sml |
| 2 |
* |
| 3 |
* COPYRIGHT (c) 2002 Bell Labs, Lucent Technologies |
| 4 |
* |
| 5 |
* This functor implements the bottom-up block-placement algorithm of |
| 6 |
* Pettis and Hansen (PLDI 1990). |
| 7 |
* |
| 8 |
* TODO |
| 9 |
* remove low-weight nodes to break cycles in chain graph |
| 10 |
*) |
| 11 |
|
| 12 |
functor WeightedBlockPlacementFn ( |
| 13 |
|
| 14 |
structure CFG : CONTROL_FLOW_GRAPH |
| 15 |
structure InsnProps : INSN_PROPERTIES (* where I = CFG.I *) |
| 16 |
where type I.addressing_mode = CFG.I.addressing_mode |
| 17 |
and type I.ea = CFG.I.ea |
| 18 |
and type I.instr = CFG.I.instr |
| 19 |
and type I.instruction = CFG.I.instruction |
| 20 |
and type I.operand = CFG.I.operand |
| 21 |
|
| 22 |
) : BLOCK_PLACEMENT = struct |
| 23 |
|
| 24 |
structure CFG = CFG |
| 25 |
structure IP = InsnProps |
| 26 |
structure G = Graph |
| 27 |
structure ITbl = IntHashTable |
| 28 |
structure PQ = LeftPriorityQFn ( |
| 29 |
struct |
| 30 |
type priority = CFG.weight |
| 31 |
val compare = Real.compare |
| 32 |
type item = CFG.edge |
| 33 |
fun priority (_, _, CFG.EDGE{w, ...}) = !w |
| 34 |
end) |
| 35 |
|
| 36 |
(* flags *) |
| 37 |
val dumpBlocks = MLRiscControl.mkFlag ( |
| 38 |
"dump-block-list", |
| 39 |
"whether block list is shown") |
| 40 |
val dumpCFG = MLRiscControl.mkFlag ( |
| 41 |
"dump-cfg-after-placement", |
| 42 |
"whether CFG is shown after block placement") |
| 43 |
val dumpStrm = MLRiscControl.debug_stream |
| 44 |
|
| 45 |
(* sequences with constant-time concatenation *) |
| 46 |
datatype 'a seq |
| 47 |
= ONE of 'a |
| 48 |
| SEQ of ('a seq * 'a seq) |
| 49 |
|
| 50 |
(* a chain of blocks that should be placed in order *) |
| 51 |
datatype chain = CHAIN of { |
| 52 |
blocks : CFG.node seq, |
| 53 |
hd : CFG.node, |
| 54 |
tl : CFG.node |
| 55 |
} |
| 56 |
|
| 57 |
fun head (CHAIN{hd, ...}) = #1 hd |
| 58 |
fun tail (CHAIN{tl, ...}) = #1 tl |
| 59 |
fun id (CHAIN{hd, ...}) = #1 hd (* use node ID of head to identify chains *) |
| 60 |
fun sameChain (CHAIN{hd=h1, ...}, CHAIN{hd=h2, ...}) = (#1 h1 = #1 h2) |
| 61 |
|
| 62 |
fun blockToString (id', CFG.BLOCK{id, ...}) = |
| 63 |
concat["<", Int.toString id', ":", Int.toString id, ">"] |
| 64 |
|
| 65 |
fun chainToString (CHAIN{hd, blocks, ...}) = let |
| 66 |
fun seq (ONE blk, l) = blockToString blk :: l |
| 67 |
| seq (SEQ(s1, s2), l) = seq(s1, "," :: seq(s2, l)) |
| 68 |
in |
| 69 |
concat("CHAIN{" :: blockToString hd :: ",[" :: seq(blocks, ["]}"])) |
| 70 |
end |
| 71 |
|
| 72 |
(* join two chains *) |
| 73 |
fun joinChains (CHAIN{blocks=b1, hd, ...}, CHAIN{blocks=b2, tl, ...}) = |
| 74 |
CHAIN{blocks=SEQ(b1, b2), hd=hd, tl=tl} |
| 75 |
|
| 76 |
val unifyChainPtrs = URef.unify joinChains |
| 77 |
|
| 78 |
(* chain pointers provide a union-find structure for chains *) |
| 79 |
type chain_ptr = chain URef.uref |
| 80 |
|
| 81 |
type block_chain_tbl = chain_ptr ITbl.hash_table |
| 82 |
|
| 83 |
(* a directed graph representing the placement ordering on chains. An edge |
| 84 |
* from chain c1 to c2 means that we should place c1 before c2. The graph |
| 85 |
* may be cyclic, so we weight the edges and remove the low-cost edge |
| 86 |
* on any cycle. |
| 87 |
*) |
| 88 |
datatype node = ND of { |
| 89 |
chain : chain, |
| 90 |
mark : bool ref, |
| 91 |
kids : edge list ref |
| 92 |
} |
| 93 |
and edge = E of { |
| 94 |
w : CFG.weight, |
| 95 |
dst : node, |
| 96 |
ign : bool ref (* if set, then ignore this edge. We use this *) |
| 97 |
(* flag to break cycles. *) |
| 98 |
} |
| 99 |
|
| 100 |
fun mkNode c = ND{chain = c, mark = ref false, kids = ref []} |
| 101 |
fun mkEdge (w, dst) = E{w = w, dst = dst, ign = ref false} |
| 102 |
|
| 103 |
(* given a table that maps block IDs to chain pointers, construct a table that |
| 104 |
* maps block IDs to their chain-placement graph nodes. |
| 105 |
*) |
| 106 |
fun mkChainPlacementGraph (tbl : block_chain_tbl) = let |
| 107 |
val gTbl = ITbl.mkTable (ITbl.numItems tbl, Fail "graph table") |
| 108 |
val find = ITbl.find gTbl |
| 109 |
val insert = ITbl.insert gTbl |
| 110 |
(* given a block ID and the chain pointer corresponding to the block |
| 111 |
* add the chain node to the graph table (this may involve creating |
| 112 |
* the node if it doesn't already exist). |
| 113 |
*) |
| 114 |
fun blockToNd (blkId, cptr, nodes) = let |
| 115 |
val chain = URef.!! cptr |
| 116 |
val chainId = id chain |
| 117 |
in |
| 118 |
case find chainId |
| 119 |
of NONE => let |
| 120 |
val nd = mkNode chain |
| 121 |
in |
| 122 |
insert (chainId, nd); |
| 123 |
if (blkId <> chainId) |
| 124 |
then insert (blkId, nd) |
| 125 |
else (); |
| 126 |
nd :: nodes |
| 127 |
end |
| 128 |
| SOME nd => (insert (blkId, nd); nodes) |
| 129 |
(* end case *) |
| 130 |
end |
| 131 |
in |
| 132 |
(ITbl.foldi blockToNd [] tbl, gTbl) |
| 133 |
end |
| 134 |
|
| 135 |
fun blockPlacement (cfg as G.GRAPH graph) = let |
| 136 |
(* a map from block IDs to their chain *) |
| 137 |
val blkTbl : chain_ptr ITbl.hash_table = let |
| 138 |
val tbl = ITbl.mkTable (#size graph (), Fail "blkTbl") |
| 139 |
val insert = ITbl.insert tbl |
| 140 |
fun ins (b : CFG.node) = insert (#1 b, |
| 141 |
URef.uRef(CHAIN{blocks = ONE b, hd = b, tl = b})) |
| 142 |
in |
| 143 |
#forall_nodes graph ins; |
| 144 |
tbl |
| 145 |
end |
| 146 |
val lookupChain = ITbl.lookup blkTbl |
| 147 |
(* the unique exit node *) |
| 148 |
val exitId = CFG.exitId cfg |
| 149 |
(* given an edge that connects two blocks, attempt to merge their chains. |
| 150 |
* We return true if a merge occurred. We do not join exit edges so that |
| 151 |
* the exit and entry nodes end up in distinct chains. |
| 152 |
*) |
| 153 |
fun join (src, dst, _) = if (dst = exitId) |
| 154 |
then false |
| 155 |
else let |
| 156 |
val cptr1 = lookupChain src |
| 157 |
val chain1 = URef.!! cptr1 |
| 158 |
val cptr2 = lookupChain dst |
| 159 |
val chain2 = URef.!! cptr2 |
| 160 |
in |
| 161 |
if (tail chain1 = src) andalso (dst = head chain2) |
| 162 |
andalso not(sameChain(chain1, chain2)) |
| 163 |
then ( |
| 164 |
(* the source block is the tail of its chain and the |
| 165 |
* destination block is the head of its chain, so we can |
| 166 |
* join the chains. |
| 167 |
*) |
| 168 |
ignore (unifyChainPtrs (cptr1, cptr2)); |
| 169 |
true) |
| 170 |
else false (* we cannot join these chains *) |
| 171 |
end |
| 172 |
(* merge chains until all of the edges have been examined; the remaining |
| 173 |
* edges cannot be fall-through. |
| 174 |
*) |
| 175 |
fun loop (pq, edges) = (case PQ.next pq |
| 176 |
of SOME(edge, pq) => if join edge |
| 177 |
then loop (pq, edges) |
| 178 |
else loop (pq, edge::edges) |
| 179 |
| NONE => edges |
| 180 |
(* end case *)) |
| 181 |
val edges = loop (PQ.fromList (#edges graph ()), []) |
| 182 |
(* construct a chain placement graph *) |
| 183 |
val (chainNodes, grTbl) = mkChainPlacementGraph blkTbl |
| 184 |
val lookupNd = ITbl.lookup grTbl |
| 185 |
fun addCFGEdge (src, dst, CFG.EDGE{k, w, ...}) = (case k |
| 186 |
(* NOTE: there may be icache benefits to including SWITCH edges. *) |
| 187 |
of CFG.SWITCH _ => () |
| 188 |
| CFG.FLOWSTO => () |
| 189 |
| _ => let |
| 190 |
val ND{chain=c1, kids, ...} = lookupNd src |
| 191 |
val dstNd as ND{chain=c2, ...} = lookupNd dst |
| 192 |
in |
| 193 |
if sameChain(c1, c2) |
| 194 |
then () |
| 195 |
else kids := mkEdge (!w, dstNd) :: !kids |
| 196 |
end |
| 197 |
(* end case *)) |
| 198 |
val _ = List.app addCFGEdge edges |
| 199 |
(* FIXME: we should remove low-weight nodes to break cycles *) |
| 200 |
(* now we construct an ordering on the chains by doing a DFS on the |
| 201 |
* chain graph. |
| 202 |
*) |
| 203 |
fun dfs (ND{mark = ref true, ...}, l) = l |
| 204 |
| dfs (ND{mark, chain, kids, ...}, l) = let |
| 205 |
fun addKid (E{ign=ref true, ...}, l) = l |
| 206 |
| addKid (E{dst, ...}, l) = dfs (dst, l) |
| 207 |
in |
| 208 |
mark := true; |
| 209 |
List.foldl addKid (chain::l) (!kids) |
| 210 |
end |
| 211 |
(* mark the exit node, since it should be last. Note that we |
| 212 |
* ensured above that the exit and entry nodes are in distinct |
| 213 |
* chains! |
| 214 |
*) |
| 215 |
val exitChain = let |
| 216 |
val ND{chain, mark, ...} = lookupNd(CFG.exitId cfg) |
| 217 |
in |
| 218 |
mark := true; |
| 219 |
chain |
| 220 |
end |
| 221 |
(* start with the entry node *) |
| 222 |
val chains = dfs (lookupNd(CFG.entryId cfg), []) |
| 223 |
(* place the rest of the nodes and add the exit node *) |
| 224 |
val chains = List.foldl dfs chains chainNodes |
| 225 |
val chains = exitChain :: chains |
| 226 |
(* extract the list of blocks from the chains list; the chains list is |
| 227 |
* in reverse order. The resulting list of blocks is in order. |
| 228 |
*) |
| 229 |
fun addChain (CHAIN{blocks, ...}, blks) = let |
| 230 |
fun addSeq (ONE b, blks) = b::blks |
| 231 |
| addSeq (SEQ(s1, s2), blks) = addSeq(s1, addSeq(s2, blks)) |
| 232 |
in |
| 233 |
addSeq (blocks, blks) |
| 234 |
end |
| 235 |
val blocks = List.foldl addChain [] chains |
| 236 |
fun updEdge (CFG.EDGE{w, a, ...}, k) = CFG.EDGE{w=w, a=a, k=k} |
| 237 |
fun updJmp f (insns as ref(i::r)) = insns := f i :: r |
| 238 |
| updJmp _ (ref []) = |
| 239 |
raise Fail "WeightedBlockPlacementFn: updJmp: empty insns" |
| 240 |
fun flipJmp (insns, lab) = |
| 241 |
updJmp (fn i => IP.negateConditional(i, lab)) insns |
| 242 |
(* set to true if we change anything *) |
| 243 |
val changed = ref false |
| 244 |
val setEdges = let |
| 245 |
val set = #set_out_edges graph |
| 246 |
in |
| 247 |
fn arg => (changed := true; set arg) |
| 248 |
end |
| 249 |
(* map a block ID to a label *) |
| 250 |
val labelOf = CFG.labelOf cfg |
| 251 |
(* patch the blocks so that unconditional jumps to the immediate successor |
| 252 |
* are replaced by fall-through edges and conditional jumps to the immediate |
| 253 |
* successor are negated. Remember that we cannot fall through to the exit |
| 254 |
* block! |
| 255 |
*) |
| 256 |
fun patch ( |
| 257 |
nd as (blkId, CFG.BLOCK{kind=CFG.NORMAL, insns, freq, ...}), |
| 258 |
(next as (nextId, _)) :: rest, |
| 259 |
l |
| 260 |
) = let |
| 261 |
fun continue () = patch (next, rest, nd::l) |
| 262 |
in |
| 263 |
case #out_edges graph blkId |
| 264 |
of [(_, dst, e as CFG.EDGE{k, w, a})] => ( |
| 265 |
case (dst = nextId, k) |
| 266 |
of (false, CFG.FALLSTHRU) => ( |
| 267 |
(* rewrite edge as JUMP and add jump insn *) |
| 268 |
setEdges (blkId, [(blkId, dst, updEdge(e, CFG.JUMP))]); |
| 269 |
insns := IP.jump(labelOf dst) :: !insns) |
| 270 |
| (true, CFG.JUMP) => |
| 271 |
if (nextId <> exitId) |
| 272 |
then ( |
| 273 |
(* rewrite edge as FALLSTHRU and remove jump insn *) |
| 274 |
setEdges (blkId, |
| 275 |
[(blkId, dst, updEdge(e, CFG.FALLSTHRU))]); |
| 276 |
insns := tl(!insns)) |
| 277 |
else () (* do not rewrite jumps to STOP block *) |
| 278 |
| _ => () |
| 279 |
(* end case *); |
| 280 |
continue()) |
| 281 |
| [(_, dst1, e1 as CFG.EDGE{k=CFG.BRANCH b, ...}), |
| 282 |
(_, dst2, e2) |
| 283 |
] => (case (dst1 = nextId, dst2 = nextId, b) |
| 284 |
of (false, false, _) => let |
| 285 |
(* here, we have to introduce a new block that |
| 286 |
* jumps to the false target. |
| 287 |
*) |
| 288 |
fun rewrite (trueId, trueE, falseId, falseE) = let |
| 289 |
val CFG.EDGE{w, a, ...} = falseE |
| 290 |
val nd' as (id, CFG.BLOCK{insns=i, ...}) = |
| 291 |
CFG.newNode cfg (!w) |
| 292 |
in |
| 293 |
(* initialize the new block *) |
| 294 |
i := [IP.jump(labelOf falseId)]; |
| 295 |
setEdges (id, [ |
| 296 |
(id, falseId, CFG.EDGE{ |
| 297 |
w = ref(!w), a = ref[], k=CFG.JUMP}) |
| 298 |
]); |
| 299 |
(* rewrite the out edges of the old block *) |
| 300 |
setEdges (blkId, [ |
| 301 |
(blkId, trueId, trueE), |
| 302 |
(blkId, id, CFG.EDGE{ |
| 303 |
k=CFG.BRANCH false, w=w, a=a |
| 304 |
}) |
| 305 |
]); |
| 306 |
(* rewrite the old jump instruction *) |
| 307 |
updJmp (fn i => |
| 308 |
IP.setBranchTargets{ |
| 309 |
i=i, t=labelOf trueId, f=labelOf id |
| 310 |
}) insns; |
| 311 |
patch (next, rest, nd'::nd::l) |
| 312 |
end |
| 313 |
in |
| 314 |
if b |
| 315 |
then rewrite (dst1, e1, dst2, e2) |
| 316 |
else rewrite (dst2, e2, dst1, e1) |
| 317 |
end |
| 318 |
| (true, _, true) => ( |
| 319 |
setEdges (blkId, [ |
| 320 |
(blkId, dst1, updEdge(e1, CFG.BRANCH false)), |
| 321 |
(blkId, dst2, updEdge(e2, CFG.BRANCH true)) |
| 322 |
]); |
| 323 |
flipJmp (insns, labelOf dst2); |
| 324 |
continue()) |
| 325 |
| (false, _, false) => ( |
| 326 |
setEdges (blkId, [ |
| 327 |
(blkId, dst1, updEdge(e1, CFG.BRANCH true)), |
| 328 |
(blkId, dst2, updEdge(e2, CFG.BRANCH false)) |
| 329 |
]); |
| 330 |
flipJmp (insns, labelOf dst1); |
| 331 |
continue()) |
| 332 |
| _ => continue() |
| 333 |
(* end case *)) |
| 334 |
| _ => continue() |
| 335 |
(* end case *) |
| 336 |
end |
| 337 |
| patch (nd, next::rest, l) = patch(next, rest, nd::l) |
| 338 |
| patch (nd, [], l) = List.rev(nd::l) |
| 339 |
val blocks = patch (hd blocks, tl blocks, []) |
| 340 |
in |
| 341 |
if !changed then CFG.changed cfg else (); |
| 342 |
if !dumpBlocks |
| 343 |
then let |
| 344 |
fun say s = TextIO.output(!dumpStrm, s) |
| 345 |
in |
| 346 |
say "Block placement order:\n"; |
| 347 |
List.app |
| 348 |
(fn b => say(concat[" ", blockToString b, "\n"])) |
| 349 |
blocks |
| 350 |
end |
| 351 |
else (); |
| 352 |
if !dumpCFG |
| 353 |
then let |
| 354 |
val prBlock = CFG.dumpBlock (!dumpStrm, cfg) |
| 355 |
in |
| 356 |
TextIO.output(!dumpStrm, "[ after block placement ]\n"); |
| 357 |
List.app prBlock blocks |
| 358 |
end |
| 359 |
else (); |
| 360 |
(cfg, blocks) |
| 361 |
end |
| 362 |
|
| 363 |
end |