| 1 |
(* |
| 2 |
* This module implements the memory coalescing capability of the |
| 3 |
* register allocator. |
| 4 |
*) |
| 5 |
functor MemoryRA(Flowgraph : RA_FLOWGRAPH) : RA_FLOWGRAPH = |
| 6 |
struct |
| 7 |
|
| 8 |
structure G = RAGraph |
| 9 |
structure A = Array |
| 10 |
structure W = Word |
| 11 |
|
| 12 |
val debug = false |
| 13 |
|
| 14 |
open G RACore |
| 15 |
|
| 16 |
val ra_spill_coal = MLRiscControl.mkCounter ("ra-spill-coalescing", |
| 17 |
"RA spill coalesce count") |
| 18 |
val ra_spill_prop = MLRiscControl.mkCounter ("ra-spill-propagation", |
| 19 |
"RA spill propagation count") |
| 20 |
|
| 21 |
local |
| 22 |
|
| 23 |
fun error msg = MLRiscErrorMsg.error("RACore", msg) |
| 24 |
|
| 25 |
fun concat([], b) = b |
| 26 |
| concat(x::a, b) = concat(a, x::b) |
| 27 |
|
| 28 |
fun chase(NODE{color=ref(ALIASED n),...}) = chase n |
| 29 |
| chase n = n |
| 30 |
|
| 31 |
in |
| 32 |
|
| 33 |
fun isOn(flag,mask) = Word.andb(flag,mask) <> 0w0 |
| 34 |
|
| 35 |
fun isMemLoc(SPILLED) = true |
| 36 |
| isMemLoc(SPILL_LOC _) = true |
| 37 |
| isMemLoc(MEMREG _) = true |
| 38 |
| isMemLoc _ = false |
| 39 |
|
| 40 |
(* |
| 41 |
* Spill coalescing. |
| 42 |
* Coalesce non-interfering moves between spilled nodes, |
| 43 |
* in non-increasing order of move cost. |
| 44 |
*) |
| 45 |
fun spillCoalescing(GRAPH{bitMatrix, ...}) = let |
| 46 |
val member = BM.member(!bitMatrix) |
| 47 |
val addEdge = BM.add(!bitMatrix) |
| 48 |
in |
| 49 |
fn nodesToSpill => let |
| 50 |
(* Find moves between two spilled nodes *) |
| 51 |
fun collectMoves([], mv') = mv' |
| 52 |
| collectMoves(NODE{movelist, color, ...}::ns, mv') = let |
| 53 |
fun ins([], mv') = collectMoves(ns, mv') |
| 54 |
| ins(MV{status=ref(COALESCED | CONSTRAINED), ...}::mvs, mv') = |
| 55 |
ins(mvs, mv') |
| 56 |
| ins((mv as MV{dst, src, ...})::mvs, mv') = let |
| 57 |
val NODE{color=ref cd, number=nd, ...} = chase dst |
| 58 |
val NODE{color=ref cs, number=ns, ...} = chase src |
| 59 |
in |
| 60 |
if nd=ns then ins(mvs, mv') |
| 61 |
else (case (cd, cs) |
| 62 |
of (MEMREG _, MEMREG _) => ins(mvs, mv') |
| 63 |
| _ => |
| 64 |
if isMemLoc cd andalso isMemLoc cs then |
| 65 |
ins(mvs, MV.add(mv, mv')) |
| 66 |
else |
| 67 |
ins(mvs, mv') |
| 68 |
(*esac*)) |
| 69 |
end |
| 70 |
in |
| 71 |
if isMemLoc (!color) then ins(!movelist, mv') |
| 72 |
else collectMoves(ns, mv') |
| 73 |
end |
| 74 |
|
| 75 |
(* Coalesce moves between two spilled nodes *) |
| 76 |
fun coalesceMoves(MV.EMPTY) = () |
| 77 |
| coalesceMoves(MV.TREE(MV{dst, src, cost, ...}, _, l, r)) = |
| 78 |
let val dst as NODE{color=colorDst, ...} = chase dst |
| 79 |
val src = chase src |
| 80 |
|
| 81 |
(* Make sure that dst has not been assigned a spill location *) |
| 82 |
val (dst, src) = |
| 83 |
case !colorDst of SPILLED => (dst, src) | _ => (src, dst) |
| 84 |
|
| 85 |
val dst as NODE{number=d, color=colorDst, adj=adjDst, |
| 86 |
defs=defsDst, uses=usesDst, ...} = dst |
| 87 |
val src as NODE{number=s, color=colorSrc, adj=adjSrc, |
| 88 |
defs=defsSrc, uses=usesSrc, ...} = src |
| 89 |
|
| 90 |
(* combine adjacency lists *) |
| 91 |
fun union([], adjSrc) = adjSrc |
| 92 |
| union((n as NODE{color, adj=adjT, |
| 93 |
number=t, ...})::adjDst, adjSrc) = |
| 94 |
(case !color of |
| 95 |
(SPILLED | MEMREG _ | SPILL_LOC _ | PSEUDO) => |
| 96 |
if addEdge(s, t) then |
| 97 |
(adjT := src :: !adjT; union(adjDst, n::adjSrc)) |
| 98 |
else union(adjDst, adjSrc) |
| 99 |
| COLORED _ => |
| 100 |
if addEdge(s, t) then union(adjDst, n::adjSrc) |
| 101 |
else union(adjDst, adjSrc) |
| 102 |
| _ => union(adjDst, adjSrc) |
| 103 |
) |
| 104 |
|
| 105 |
val mvs = MV.merge(l,r) |
| 106 |
|
| 107 |
fun f() = |
| 108 |
((* print(Int.toString d ^"<->"^Int.toString s^"\n");*) |
| 109 |
ra_spill_coal := !ra_spill_coal + 1; |
| 110 |
(* unify *) |
| 111 |
colorDst := ALIASED src; |
| 112 |
adjSrc := union(!adjDst, !adjSrc); |
| 113 |
defsSrc := concat(!defsDst, !defsSrc); |
| 114 |
usesSrc := concat(!usesDst, !usesSrc); |
| 115 |
coalesceMoves mvs) |
| 116 |
in |
| 117 |
if d = s then coalesceMoves mvs |
| 118 |
else (case !colorDst |
| 119 |
of MEMREG _ => coalesceMoves mvs |
| 120 |
| SPILLED => |
| 121 |
if member(d,s) then coalesceMoves mvs else f() |
| 122 |
| SPILL_LOC loc => |
| 123 |
if member(d,s) then coalesceMoves mvs else f() |
| 124 |
| _ => error "coalesceMoves" |
| 125 |
(*esac*)) |
| 126 |
end |
| 127 |
in coalesceMoves(collectMoves(nodesToSpill, MV.EMPTY)) |
| 128 |
end |
| 129 |
end (*spillCoalesce*) |
| 130 |
|
| 131 |
(* |
| 132 |
* Spill propagation. |
| 133 |
* This one uses a simple local lookahead algorithm. |
| 134 |
*) |
| 135 |
fun spillPropagation(G as GRAPH{bitMatrix, memRegs, ...}) nodesToSpill = |
| 136 |
let val spillCoalescing = spillCoalescing G |
| 137 |
exception SpillProp |
| 138 |
val visited = IntHashTable.mkTable(32, SpillProp) |
| 139 |
: bool IntHashTable.hash_table |
| 140 |
val hasBeenVisited = IntHashTable.find visited |
| 141 |
val hasBeenVisited = fn r => case hasBeenVisited r of NONE => false |
| 142 |
| SOME _ => true |
| 143 |
val markAsVisited = IntHashTable.insert visited |
| 144 |
val member = BM.member(!bitMatrix) |
| 145 |
|
| 146 |
(* compute savings due to spill coalescing. |
| 147 |
* The move list must be associated with a colorable node. |
| 148 |
* The pinned flag is to prevent the spill node from coalescing |
| 149 |
* two different fixed memory registers. |
| 150 |
*) |
| 151 |
fun coalescingSavings |
| 152 |
(node as NODE{number=me, movelist, pri=ref spillcost, ...}) = |
| 153 |
let fun interferes(x,[]) = false |
| 154 |
| interferes(x,NODE{number=y, ...}::ns) = |
| 155 |
x = y orelse member(x,y) orelse interferes(x, ns) |
| 156 |
|
| 157 |
fun moveSavings([], pinned, total) = (pinned, total+total) |
| 158 |
| moveSavings(MV{status=ref(CONSTRAINED | COALESCED), ...}::mvs, |
| 159 |
pinned, total) = |
| 160 |
moveSavings(mvs, pinned, total) |
| 161 |
| moveSavings(MV{dst, src, cost, ...}::mvs, pinned, total) = |
| 162 |
let val NODE{number=d, color=dstCol, ...} = chase dst |
| 163 |
val NODE{number=s, color=srcCol, ...} = chase src |
| 164 |
|
| 165 |
(* How much can be saved by coalescing with the memory |
| 166 |
* location x. |
| 167 |
*) |
| 168 |
fun savings(x) = |
| 169 |
if member(d, s) then |
| 170 |
(if debug then print "interfere\n" else (); |
| 171 |
moveSavings(mvs, pinned, total)) |
| 172 |
else if x = ~1 then |
| 173 |
(if debug then print (Real.toString cost^"\n") else (); |
| 174 |
moveSavings(mvs, pinned, total+cost)) |
| 175 |
else if pinned >= 0 andalso pinned <> x then |
| 176 |
(* already coalesced with another mem reg *) |
| 177 |
(if debug then print "pinned\n" else (); |
| 178 |
moveSavings(mvs, pinned, total)) |
| 179 |
else |
| 180 |
(if debug then print (Real.toString cost^"\n") else (); |
| 181 |
moveSavings(mvs, x, total+cost)) |
| 182 |
|
| 183 |
val _ = if debug then |
| 184 |
(print("Savings "^Int.toString d^" <-> "^ |
| 185 |
Int.toString s^"=") |
| 186 |
) else () |
| 187 |
in if d = s then |
| 188 |
(if debug then print "0 (trivial)\n" else (); |
| 189 |
moveSavings(mvs, pinned, total) |
| 190 |
) |
| 191 |
else |
| 192 |
case (!dstCol, !srcCol) of |
| 193 |
(SPILLED, PSEUDO) => savings(~1) |
| 194 |
| (MEMREG(m, _), PSEUDO) => savings(m) |
| 195 |
| (SPILL_LOC s, PSEUDO) => savings(~s) |
| 196 |
| (PSEUDO, SPILLED) => savings(~1) |
| 197 |
| (PSEUDO, MEMREG(m, _)) => savings(m) |
| 198 |
| (PSEUDO, SPILL_LOC s) => savings(~s) |
| 199 |
| _ => (if debug then print "0 (other)\n" else (); |
| 200 |
moveSavings(mvs, pinned, total)) |
| 201 |
end |
| 202 |
|
| 203 |
(* Find initial budget *) |
| 204 |
val _ = if debug then |
| 205 |
print("Trying to propagate "^Int.toString me^ |
| 206 |
" spill cost="^Real.toString spillcost^"\n") |
| 207 |
else () |
| 208 |
|
| 209 |
val (pinned, savings) = moveSavings(!movelist, ~1, 0.0) |
| 210 |
val budget = spillcost - savings |
| 211 |
val S = [node] |
| 212 |
|
| 213 |
(* Find lookahead nodes *) |
| 214 |
fun lookaheads([], L) = L |
| 215 |
| lookaheads(MV{cost, dst, src, ...}::mvs, L) = |
| 216 |
let val dst as NODE{number=d, ...} = chase dst |
| 217 |
val src as NODE{number=s, ...} = chase src |
| 218 |
fun check(n, node as NODE{color=ref PSEUDO, ...}) = |
| 219 |
if n = me orelse member(n, me) then |
| 220 |
lookaheads(mvs, L) |
| 221 |
else |
| 222 |
add(n, node, L, []) |
| 223 |
| check _ = lookaheads(mvs, L) |
| 224 |
and add(x, x', (l as (c,n' as NODE{number=y, ...}))::L, L') = |
| 225 |
if x = y then |
| 226 |
lookaheads(mvs, (cost+c, n')::List.revAppend(L', L)) |
| 227 |
else add(x, x', L, l::L') |
| 228 |
| add(x, x', [], L') = |
| 229 |
lookaheads(mvs, (cost, x')::L') |
| 230 |
in if d = me then check(s, src) else check(d, dst) |
| 231 |
end |
| 232 |
|
| 233 |
(* Now try to improve it by also propagating the lookahead nodes *) |
| 234 |
fun improve([], pinned, budget, S) = (budget, S) |
| 235 |
| improve((cost, node as NODE{number=n, movelist, pri, ...})::L, |
| 236 |
pinned, budget, S) = |
| 237 |
if interferes(n, S) then |
| 238 |
(if debug then |
| 239 |
print ("Excluding "^Int.toString n^" (interferes)\n") |
| 240 |
else (); |
| 241 |
improve(L, pinned, budget, S)) |
| 242 |
else |
| 243 |
let val (pinned', savings) = moveSavings(!movelist, pinned, 0.0) |
| 244 |
val defUseSavings = cost+cost |
| 245 |
val spillcost = !pri |
| 246 |
val budget' = budget - savings - defUseSavings + spillcost |
| 247 |
in if budget' <= budget then |
| 248 |
(if debug then print ("Including "^Int.toString n^"\n") |
| 249 |
else (); |
| 250 |
improve(L, pinned', budget', node::S) |
| 251 |
) |
| 252 |
else |
| 253 |
(if debug then print ("Excluding "^Int.toString n^"\n") |
| 254 |
else (); |
| 255 |
improve(L, pinned, budget, S)) |
| 256 |
end |
| 257 |
|
| 258 |
in if budget <= 0.0 then (budget, S) |
| 259 |
else improve(lookaheads(!movelist, []), pinned, budget, S) |
| 260 |
end |
| 261 |
|
| 262 |
(* Insert all spillable neighbors onto the worklist *) |
| 263 |
fun insert([], worklist) = worklist |
| 264 |
| insert((node as NODE{color=ref PSEUDO, number, ...})::adj, worklist) = |
| 265 |
if hasBeenVisited number |
| 266 |
then insert(adj, worklist) |
| 267 |
else (markAsVisited (number, true); |
| 268 |
insert(adj, node::worklist)) |
| 269 |
| insert(_::adj, worklist) = insert(adj, worklist) |
| 270 |
|
| 271 |
fun insertAll([], worklist) = worklist |
| 272 |
| insertAll(NODE{adj, ...}::nodes, worklist) = |
| 273 |
insertAll(nodes, insert(!adj, worklist)) |
| 274 |
|
| 275 |
val marker = SPILLED |
| 276 |
|
| 277 |
(* Process all nodes from the worklist *) |
| 278 |
fun propagate([], spilled) = spilled |
| 279 |
| propagate((node as NODE{color=ref PSEUDO, ...})::worklist, |
| 280 |
spilled) = |
| 281 |
let val (budget, S) = coalescingSavings(node) |
| 282 |
fun spillNodes([]) = () |
| 283 |
| spillNodes(NODE{color, ...}::nodes) = |
| 284 |
(ra_spill_prop := !ra_spill_prop + 1; |
| 285 |
color := marker; (* spill the node *) |
| 286 |
spillNodes nodes |
| 287 |
) |
| 288 |
|
| 289 |
in if budget <= 0.0 |
| 290 |
then (* propagate spill *) |
| 291 |
(if debug then |
| 292 |
(print("Propagating "); |
| 293 |
app (fn NODE{number=x, ...} => print(Int.toString x^" ")) |
| 294 |
S; |
| 295 |
print "\n") |
| 296 |
else (); |
| 297 |
spillNodes S; |
| 298 |
(* run spill coalescing *) |
| 299 |
spillCoalescing S; |
| 300 |
propagate(insertAll(S, worklist), List.revAppend(S,spilled)) |
| 301 |
) |
| 302 |
else |
| 303 |
propagate(worklist, spilled) |
| 304 |
end |
| 305 |
| propagate(_::worklist, spilled) = |
| 306 |
propagate(worklist, spilled) |
| 307 |
|
| 308 |
(* Initialize worklist *) |
| 309 |
fun init([], worklist) = worklist |
| 310 |
| init(NODE{adj, color=ref(c), ...}::rest, worklist) = |
| 311 |
if isMemLoc (c) then |
| 312 |
init(rest, insert(!adj, worklist)) |
| 313 |
else |
| 314 |
init(rest, worklist) |
| 315 |
|
| 316 |
(* |
| 317 |
* Iterate between spill coalescing and propagation |
| 318 |
*) |
| 319 |
fun iterate(spillWorkList, spilled) = |
| 320 |
let (* run one round of coalescing first *) |
| 321 |
val _ = spillCoalescing spillWorkList |
| 322 |
val propagationWorkList = init(spillWorkList, []) |
| 323 |
(* iterate on our own spill nodes *) |
| 324 |
val spilled = propagate(propagationWorkList, spilled) |
| 325 |
(* try the memory registers too *) |
| 326 |
val spilled = propagate(!memRegs, spilled) |
| 327 |
in spilled |
| 328 |
end |
| 329 |
|
| 330 |
in iterate(nodesToSpill, nodesToSpill) |
| 331 |
end |
| 332 |
|
| 333 |
|
| 334 |
(* |
| 335 |
* Spill coloring. |
| 336 |
* Assign logical spill locations to all the spill nodes. |
| 337 |
* |
| 338 |
* IMPORTANT BUG FIX: |
| 339 |
* Spilled copy temporaries are assigned its own set of colors and |
| 340 |
* cannot share with another other nodes. They can share colors with |
| 341 |
* themselves however. |
| 342 |
* |
| 343 |
* spillLoc is the first available (logical) spill location. |
| 344 |
*) |
| 345 |
|
| 346 |
fun spillColoring(GRAPH{spillLoc, copyTmps, mode, ...}) nodesToSpill = let |
| 347 |
val proh = A.array(length nodesToSpill, ~1) |
| 348 |
val firstColor= !spillLoc |
| 349 |
|
| 350 |
fun colorCopyTmps(tmps) = let |
| 351 |
fun spillTmp(NODE{color as ref(SPILLED), ...}, found) = |
| 352 |
(color := SPILL_LOC(firstColor); true) |
| 353 |
| spillTmp(_, found) = found |
| 354 |
in |
| 355 |
if List.foldl spillTmp false tmps then |
| 356 |
(spillLoc := !spillLoc + 1; firstColor + 1) |
| 357 |
else firstColor |
| 358 |
end |
| 359 |
|
| 360 |
(* color the copy temporaries first *) |
| 361 |
val firstColor = |
| 362 |
if isOn(mode, RACore.HAS_PARALLEL_COPIES) then |
| 363 |
colorCopyTmps(!copyTmps) |
| 364 |
else firstColor |
| 365 |
|
| 366 |
fun selectColor([], _, lastLoc) = (spillLoc := lastLoc) |
| 367 |
| selectColor(NODE{color as ref(SPILLED), number, adj, ...}::nodes, |
| 368 |
currLoc, lastLoc) = |
| 369 |
let |
| 370 |
fun neighbors(NODE{color=ref(SPILL_LOC s), ...}) = |
| 371 |
A.update(proh, s - firstColor, number) |
| 372 |
| neighbors(NODE{color=ref(ALIASED n), ...}) = neighbors n |
| 373 |
| neighbors _ = () |
| 374 |
|
| 375 |
val _ = app neighbors (!adj) |
| 376 |
|
| 377 |
fun findColor(loc, startingPt) = |
| 378 |
if loc = lastLoc then findColor(firstColor, startingPt) |
| 379 |
else if A.sub(proh, loc-firstColor) <> number then (loc, lastLoc) |
| 380 |
else if loc = startingPt then (lastLoc, lastLoc+1) |
| 381 |
else findColor(loc+1, startingPt) |
| 382 |
|
| 383 |
val (loc, lastLoc) = findColor(currLoc + 1, currLoc) |
| 384 |
|
| 385 |
in |
| 386 |
color := SPILL_LOC(loc); (* mark with color *) |
| 387 |
selectColor(nodes, loc, lastLoc) |
| 388 |
end |
| 389 |
| selectColor(_::nodes, currLoc, lastLoc) = |
| 390 |
selectColor(nodes, currLoc, lastLoc) |
| 391 |
in |
| 392 |
(* color the rest of the spilled nodes *) |
| 393 |
selectColor(nodesToSpill, firstColor, !spillLoc + 1) |
| 394 |
end (* spillColoring *) |
| 395 |
|
| 396 |
end (* local *) |
| 397 |
|
| 398 |
structure F = Flowgraph |
| 399 |
|
| 400 |
open F |
| 401 |
|
| 402 |
val SPILL_COALESCING = 0wx100 |
| 403 |
val SPILL_COLORING = 0wx200 |
| 404 |
val SPILL_PROPAGATION = 0wx400 |
| 405 |
|
| 406 |
(* |
| 407 |
* New services that also perform memory allocation |
| 408 |
*) |
| 409 |
fun services f = |
| 410 |
let val {build, spill=spillMethod, |
| 411 |
blockNum, instrNum, programPoint} = F.services f |
| 412 |
|
| 413 |
(* Mark nodes that are immediately aliased to mem regs; |
| 414 |
* These are nodes that need also to be spilled |
| 415 |
*) |
| 416 |
fun markMemRegs [] = () |
| 417 |
| markMemRegs(NODE{number=r, |
| 418 |
color as ref(ALIASED |
| 419 |
(NODE{color=ref(col), ...})), ...}::ns) = |
| 420 |
(case col of MEMREG _ => color := col | _ => (); |
| 421 |
markMemRegs(ns)) |
| 422 |
| markMemRegs(_::ns) = markMemRegs ns |
| 423 |
|
| 424 |
(* |
| 425 |
* Actual spill phase. |
| 426 |
* Perform the memory coalescing phases first, before doing an |
| 427 |
* actual spill. |
| 428 |
*) |
| 429 |
fun spillIt{graph = G as GRAPH{mode, ...}, nodes, |
| 430 |
copyInstr, spill, spillSrc, spillCopyTmp, |
| 431 |
reload, reloadDst, renameSrc, cellkind} = |
| 432 |
let |
| 433 |
val nodes = if isOn(mode,SPILL_PROPAGATION) then |
| 434 |
spillPropagation G nodes else nodes |
| 435 |
val _ = if isOn(mode,SPILL_COALESCING) then |
| 436 |
spillCoalescing G nodes else () |
| 437 |
val _ = if isOn(mode,SPILL_COLORING) then |
| 438 |
spillColoring G nodes else () |
| 439 |
val _ = if isOn(mode,SPILL_COALESCING+SPILL_PROPAGATION) |
| 440 |
then markMemRegs nodes else () |
| 441 |
in spillMethod |
| 442 |
{graph=G, nodes=nodes, copyInstr=copyInstr, |
| 443 |
spill=spill, spillSrc=spillSrc, spillCopyTmp=spillCopyTmp, |
| 444 |
reload=reload, reloadDst=reloadDst, |
| 445 |
renameSrc=renameSrc, cellkind=cellkind} |
| 446 |
end |
| 447 |
in {build=build, spill=spillIt, programPoint=programPoint, |
| 448 |
blockNum=blockNum, instrNum=instrNum} |
| 449 |
end |
| 450 |
|
| 451 |
end |