| 1 |
(* ra-spill-with-renaming.sml |
| 2 |
* |
| 3 |
* COPYRIGHT (c) 2001 Bell Labs, Lucent Technologies |
| 4 |
* |
| 5 |
* This version also performs local renaming on the spill code. |
| 6 |
* For example, spilling t below |
| 7 |
* |
| 8 |
* t <- ... |
| 9 |
* .. <- t |
| 10 |
* .... |
| 11 |
* .... |
| 12 |
* .. <- t |
| 13 |
* .. <- t |
| 14 |
* |
| 15 |
* would result in |
| 16 |
* |
| 17 |
* tmp1 <- ... |
| 18 |
* mem[t] <- tmp1 |
| 19 |
* .. <- tmp1 <---- rename from t to tmp1 |
| 20 |
* .... |
| 21 |
* .... |
| 22 |
* tmp2 <- mem[t] |
| 23 |
* .. <- tmp2 |
| 24 |
* .. <- tmp2 <---- rename from t to tmp2 |
| 25 |
* |
| 26 |
* That is, we try to avoid inserting reload code whenever it is possible. |
| 27 |
* This is done by keeping track of which values are live locally. |
| 28 |
* |
| 29 |
* Allen (5/9/00) |
| 30 |
* |
| 31 |
*) |
| 32 |
(* |
| 33 |
* This module manages the spill/reload process. |
| 34 |
* The reason this is detached from the main module is that |
| 35 |
* I can't understand the old code. |
| 36 |
* |
| 37 |
* Okay, now I understand the code. |
| 38 |
* |
| 39 |
* The new code does things slightly differently. |
| 40 |
* Here, we are given an instruction and a list of registers to spill |
| 41 |
* and reload. We rewrite the instruction until all instances of these |
| 42 |
* registers are rewritten. |
| 43 |
* |
| 44 |
* (12/13/99) Some major caveats when spill coalescing/coloring is used: |
| 45 |
* When parallel copies are generated and spill coalescing/coloring is used, |
| 46 |
* two special cases have to be identified: |
| 47 |
* |
| 48 |
* Case 1 (spillLoc dst = spillLoc src) |
| 49 |
* Suppose we have a parallel copy |
| 50 |
* (u,v) <- (x,y) |
| 51 |
* where u has to be spilled and y has to reloaded. When both |
| 52 |
* u and y are mapped to location M. The following wrong code may |
| 53 |
* be generated: |
| 54 |
* M <- x (spill u) |
| 55 |
* v <- M (reload y) |
| 56 |
* This is incorrect. Instead, we generate a dummy copy and |
| 57 |
* delay the spill after the reload, like this: |
| 58 |
* |
| 59 |
* tmp <- x (save value of u) |
| 60 |
* v <- M (reload y) |
| 61 |
* M <- tmp (spill u) |
| 62 |
* Case 2 (spillLoc copyTmp = spillLoc src) |
| 63 |
* Another case that can cause problems is when the spill location of |
| 64 |
* the copy temporary is the same as that of one of the sources: |
| 65 |
* |
| 66 |
* (a, b, v) <- (b, a, u) where spillLoc(u) = spillLoc(tmp) = v |
| 67 |
* |
| 68 |
* The incorrect code is |
| 69 |
* (a, b) <- (b, a) |
| 70 |
* v <- M |
| 71 |
* But then the shuffle code for the copy can clobber the location M. |
| 72 |
* |
| 73 |
* tmp <- M |
| 74 |
* (a, b) <- (b, a) |
| 75 |
* v <- tmp |
| 76 |
* |
| 77 |
* (Note that spillLoc copyTmp = spillLoc src can never happen) |
| 78 |
* |
| 79 |
* -- Allen |
| 80 |
*) |
| 81 |
|
| 82 |
functor RASpillWithRenaming |
| 83 |
(structure InsnProps : INSN_PROPERTIES |
| 84 |
structure Asm : INSTRUCTION_EMITTER (* where I = InsnProps.I *) |
| 85 |
where type I.addressing_mode = InsnProps.I.addressing_mode |
| 86 |
and type I.ea = InsnProps.I.ea |
| 87 |
and type I.instr = InsnProps.I.instr |
| 88 |
and type I.instruction = InsnProps.I.instruction |
| 89 |
and type I.operand = InsnProps.I.operand |
| 90 |
|
| 91 |
(* Spilling a variable v creates tiny live-ranges at all its definitions |
| 92 |
* and uses. The following parameter is the maximal distance of |
| 93 |
* live-ranges created between a definition and its use, |
| 94 |
* measured in the number of instructions. If, max_dist = D, then |
| 95 |
* the spill routine will never create a new live-range that is more |
| 96 |
* than D instructions apart. |
| 97 |
*) |
| 98 |
val max_dist : int ref |
| 99 |
|
| 100 |
(* When this parameter is on, the spill routine will keep track of |
| 101 |
* multiple values for the renaming process. This is recommended |
| 102 |
* if the architecture has a lot of free registers. But it should |
| 103 |
* probably be turned off on the x86. |
| 104 |
*) |
| 105 |
val keep_multiple_values : bool ref |
| 106 |
) : RA_SPILL = |
| 107 |
struct |
| 108 |
val debug = false |
| 109 |
|
| 110 |
structure I = InsnProps.I |
| 111 |
structure P = InsnProps |
| 112 |
structure C = I.C |
| 113 |
structure CBase = CellsBasis |
| 114 |
structure Core = RACore |
| 115 |
structure G = Core.G |
| 116 |
|
| 117 |
fun error msg = MLRiscErrorMsg.error("RASpillWithRenaming",msg) |
| 118 |
|
| 119 |
fun dec1 n = Word.toIntX(Word.fromInt n - 0w1) |
| 120 |
fun dec{block,insn} = {block=block,insn=dec1 insn} |
| 121 |
|
| 122 |
structure T = RASpillTypes(I) |
| 123 |
open T |
| 124 |
|
| 125 |
fun uniq s = CBase.SortedCells.return(CBase.SortedCells.uniq s) |
| 126 |
val i2s = Int.toString |
| 127 |
fun pt2s{block,insn} = "b"^i2s block^":"^i2s insn |
| 128 |
|
| 129 |
val Asm.S.STREAM{emit, ...} = Asm.makeStream[] |
| 130 |
|
| 131 |
(* val spilledCopyTmps = MLRiscControl.getCounter "ra-spilled-copy-temps" *) |
| 132 |
|
| 133 |
(* |
| 134 |
* The following function performs spilling. |
| 135 |
*) |
| 136 |
fun spillRewrite |
| 137 |
{graph=G as G.GRAPH{showReg, spilledRegs, nodes, mode, dedicated,...}, |
| 138 |
spill : spill, |
| 139 |
spillCopyTmp : spillCopyTmp, |
| 140 |
spillSrc : spillSrc, |
| 141 |
renameSrc : renameSrc, |
| 142 |
reload : reload, |
| 143 |
reloadDst : reloadDst, |
| 144 |
copyInstr : copyInstr, |
| 145 |
cellkind, |
| 146 |
spillSet, reloadSet, killSet |
| 147 |
} = |
| 148 |
let |
| 149 |
(* Must do this to make sure the interference graph is |
| 150 |
* reflected to the cells |
| 151 |
*) |
| 152 |
val _ = Core.updateCellAliases G |
| 153 |
|
| 154 |
val getSpillLoc = Core.spillLoc G |
| 155 |
fun spillLocOf(CBase.CELL{id, ...}) = getSpillLoc id |
| 156 |
val spillLocsOf = map spillLocOf |
| 157 |
val getnode = IntHashTable.lookup nodes |
| 158 |
val getnode = fn CBase.CELL{id, ...} => getnode id |
| 159 |
|
| 160 |
val MAX_DIST = !max_dist |
| 161 |
|
| 162 |
val insnDefUse = P.defUse cellkind |
| 163 |
|
| 164 |
fun hasNonDedicated rs = |
| 165 |
let fun loop [] = false |
| 166 |
| loop(r::rs) = |
| 167 |
if dedicated(CBase.registerId r) then loop rs else true |
| 168 |
in loop rs end |
| 169 |
|
| 170 |
(* Merge prohibited registers *) |
| 171 |
val enterSpill = IntHashTable.insert spilledRegs |
| 172 |
val addProh = app (fn c => enterSpill(CBase.registerId c,true)) |
| 173 |
|
| 174 |
val getSpills = G.PPtHashTable.find spillSet |
| 175 |
val getSpills = fn p => case getSpills p of SOME s => s | NONE => [] |
| 176 |
val getReloads = G.PPtHashTable.find reloadSet |
| 177 |
val getReloads = fn p => case getReloads p of SOME s => s | NONE => [] |
| 178 |
val getKills = G.PPtHashTable.find killSet |
| 179 |
val getKills = fn p => case getKills p of SOME s => s | NONE => [] |
| 180 |
|
| 181 |
fun getLoc(G.NODE{color=ref(G.ALIASED n), ...}) = getLoc n |
| 182 |
| getLoc(G.NODE{color=ref(G.MEMREG(_, m)), ...}) = G.MEM_REG m |
| 183 |
| getLoc(G.NODE{color=ref(G.SPILL_LOC s), ...}) = G.FRAME s |
| 184 |
| getLoc(G.NODE{color=ref(G.SPILLED), number, ...}) = G.FRAME number |
| 185 |
| getLoc(G.NODE{color=ref(G.PSEUDO), number, ...}) = G.FRAME number |
| 186 |
| getLoc _ = error "getLoc" |
| 187 |
|
| 188 |
fun printRegs regs = |
| 189 |
app (fn r => print(concat[ |
| 190 |
CBase.toString r, " [", Core.spillLocToString G (CBase.cellId r), |
| 191 |
"] " |
| 192 |
])) regs |
| 193 |
|
| 194 |
val parallelCopies = Word.andb(Core.HAS_PARALLEL_COPIES, mode) <> 0w0 |
| 195 |
|
| 196 |
fun chase(CBase.CELL{col=ref(CBase.ALIASED c), ...}) = chase c |
| 197 |
| chase c = c |
| 198 |
|
| 199 |
fun cellId(CBase.CELL{id, ...}) = id |
| 200 |
|
| 201 |
fun sameCell(CBase.CELL{id=x,...}, CBase.CELL{id=y, ...}) = x=y |
| 202 |
|
| 203 |
fun same(x,regToSpill) = sameCell(chase x,regToSpill) |
| 204 |
|
| 205 |
(* |
| 206 |
* Rewrite the instruction given that a bunch of registers have |
| 207 |
* to be spilled and reloaded. |
| 208 |
*) |
| 209 |
fun spillRewrite{pt, instrs, annotations} = |
| 210 |
let |
| 211 |
(* Environment manipulation functions. |
| 212 |
* The environment is just a list of triples. |
| 213 |
*) |
| 214 |
fun update(pt,env,r,NONE) = kill(env, r) |
| 215 |
| update(pt,env,r,SOME newReg) = |
| 216 |
(* if the register is a dedicated register, conservatively kill |
| 217 |
* the value r in the current environment. this is necessary |
| 218 |
* because dedicated registers have been removed from the |
| 219 |
* def/use information. |
| 220 |
*) |
| 221 |
if dedicated(CBase.registerId newReg) |
| 222 |
then kill(env, r) |
| 223 |
else (r,newReg,pt)::(if !keep_multiple_values then env else []) |
| 224 |
|
| 225 |
and kill(env,r) = |
| 226 |
let fun loop([], env') = env' |
| 227 |
| loop((binding as (r',_,_))::env,env') = |
| 228 |
loop(env, |
| 229 |
if CBase.sameColor(r, r') then env' else binding::env') |
| 230 |
in loop(env, []) end |
| 231 |
|
| 232 |
(* |
| 233 |
* Insert reloading code for an instruction. |
| 234 |
* Note: reload code goes after the instruction, if any. |
| 235 |
*) |
| 236 |
fun reloadInstr(pt,instr,regToSpill,env,spillLoc) = |
| 237 |
let val {code, proh, newReg} = |
| 238 |
reload{instr=instr,reg=regToSpill, |
| 239 |
spillLoc=spillLoc,annotations=annotations} |
| 240 |
in addProh(proh); |
| 241 |
(code,update(pt,env,regToSpill,newReg)) |
| 242 |
end |
| 243 |
|
| 244 |
(* |
| 245 |
* Renaming the source for an instruction. |
| 246 |
*) |
| 247 |
fun renameInstr(pt,instr,regToSpill,env,toSrc) = |
| 248 |
let val {code, proh, newReg} = |
| 249 |
renameSrc{instr=instr, fromSrc=regToSpill,toSrc=toSrc} |
| 250 |
in addProh(proh); |
| 251 |
(code,update(pt,env,regToSpill,newReg)) |
| 252 |
end |
| 253 |
|
| 254 |
(* |
| 255 |
* Remove uses of regToSpill from a set of parallel copies. |
| 256 |
* If there are multiple uses, then return multiple moves. |
| 257 |
*) |
| 258 |
fun extractUses(regToSpill, rds, rss) = |
| 259 |
let fun loop(rd::rds, rs::rss, newRds, rds', rss') = |
| 260 |
if same(rs,regToSpill) then |
| 261 |
loop(rds, rss, rd::newRds, rds', rss') |
| 262 |
else |
| 263 |
loop(rds, rss, newRds, rd::rds', rs::rss') |
| 264 |
| loop(_, _, newRds, rds', rss') = (newRds, rds', rss') |
| 265 |
in loop(rds, rss, [], [], []) end |
| 266 |
|
| 267 |
(* |
| 268 |
* Insert reload code for the sources of a copy. |
| 269 |
* Transformation: |
| 270 |
* d1..dn <- s1..sn |
| 271 |
* => |
| 272 |
* d1..dn/r <- s1...sn/r. |
| 273 |
* reload code |
| 274 |
* reload copies |
| 275 |
* |
| 276 |
*) |
| 277 |
fun reloadCopySrc(instr,regToSpill,env,spillLoc) = |
| 278 |
let val (dst, src) = P.moveDstSrc instr |
| 279 |
val (rds, copyDst, copySrc) = extractUses(regToSpill, dst, src) |
| 280 |
fun processMoves([], reloadCode) = reloadCode |
| 281 |
| processMoves(rd::rds, reloadCode) = |
| 282 |
let val code = |
| 283 |
reloadDst{spillLoc=spillLoc,reg=regToSpill, |
| 284 |
dst=rd,annotations=annotations} |
| 285 |
in processMoves(rds, code@reloadCode) |
| 286 |
end |
| 287 |
val reloadCode = processMoves(rds, []) |
| 288 |
in case copyDst of |
| 289 |
[] => (reloadCode, env) |
| 290 |
| _ => (copyInstr((copyDst, copySrc), instr) @ reloadCode, env) |
| 291 |
end |
| 292 |
|
| 293 |
fun diff({block=b1:int,insn=i1},{block=b2,insn=i2}) = |
| 294 |
if b1=b2 then i1-i2 else MAX_DIST+1 |
| 295 |
|
| 296 |
(* |
| 297 |
* Insert reload code |
| 298 |
*) |
| 299 |
fun reload(pt,instr,regToSpill,env,spillLoc) = |
| 300 |
if P.moveInstr instr then |
| 301 |
reloadCopySrc(instr,regToSpill,env,spillLoc) |
| 302 |
else |
| 303 |
let fun lookup [] = |
| 304 |
reloadInstr(pt,instr,regToSpill,env,spillLoc) |
| 305 |
| lookup((r,currentReg,defPt)::env) = |
| 306 |
if CBase.sameColor(r,regToSpill) then |
| 307 |
if defPt = pt |
| 308 |
then lookup env(* this is NOT the right renaming!*) |
| 309 |
else if diff(defPt,pt) <= MAX_DIST then |
| 310 |
renameInstr(pt,instr,regToSpill,env,currentReg) |
| 311 |
else |
| 312 |
reloadInstr(pt,instr,regToSpill,env,spillLoc) |
| 313 |
else |
| 314 |
lookup(env) |
| 315 |
in lookup env |
| 316 |
end |
| 317 |
|
| 318 |
(* |
| 319 |
* Check whether the id is in a list |
| 320 |
*) |
| 321 |
fun containsId(id,[]) = false |
| 322 |
| containsId(id:CBase.cell_id,r::rs) = r = id orelse containsId(id,rs) |
| 323 |
fun spillConflict(G.FRAME loc, rs) = containsId(~loc, rs) |
| 324 |
| spillConflict(G.MEM_REG(CBase.CELL{id, ...}), rs) = |
| 325 |
containsId(id, rs) |
| 326 |
|
| 327 |
fun contains(r',[]) = false |
| 328 |
| contains(r',r::rs) = sameCell(r',r) orelse contains(r',rs) |
| 329 |
|
| 330 |
(* |
| 331 |
* Insert spill code for an instruction. |
| 332 |
* Spill code occur after the instruction. |
| 333 |
* If the value in regToSpill is never used, the client also |
| 334 |
* has the opportunity to remove the instruction. |
| 335 |
*) |
| 336 |
fun spillInstr(pt,instr,regToSpill,spillLoc,kill,env) = |
| 337 |
let val {code, proh, newReg} = |
| 338 |
spill{instr=instr, |
| 339 |
kill=kill, spillLoc=spillLoc, |
| 340 |
reg=regToSpill, annotations=annotations} |
| 341 |
in addProh(proh); |
| 342 |
(code, update(pt,env,regToSpill,newReg)) |
| 343 |
end |
| 344 |
|
| 345 |
(* Remove the definition regToSpill <- from |
| 346 |
* parallel copies rds <- rss. |
| 347 |
* Note, there is a guarantee that regToSpill is not aliased |
| 348 |
* to another register in the rds set. |
| 349 |
*) |
| 350 |
fun extractDef(regToSpill,rds,rss,kill) = |
| 351 |
let fun loop(rd::rds, rs::rss, rds', rss') = |
| 352 |
if spillLocOf rd = spillLocOf rs then |
| 353 |
(rs, rds@rds', rss@rss', true) |
| 354 |
else if same(rd, regToSpill) then |
| 355 |
(rs, rds@rds', rss@rss', kill) |
| 356 |
else loop(rds, rss, rd::rds', rs::rss') |
| 357 |
| loop _ = let |
| 358 |
fun pr r = print(concat[ |
| 359 |
CBase.toString r, ":", i2s(spillLocOf r), " " |
| 360 |
]) |
| 361 |
in |
| 362 |
print("rds="); |
| 363 |
app pr rds; |
| 364 |
print("\nrss="); |
| 365 |
app pr rss; |
| 366 |
print "\n"; |
| 367 |
error("extractDef: "^CBase.toString regToSpill) |
| 368 |
end |
| 369 |
in loop(rds, rss, [], []) end |
| 370 |
|
| 371 |
(* |
| 372 |
* Insert spill code for a destination of a copy |
| 373 |
* suppose d = r and we have a copy d <- s in |
| 374 |
* d1...dn <- s1...sn |
| 375 |
* |
| 376 |
* d1...dn <- s1...sn |
| 377 |
* => |
| 378 |
* spill s to spillLoc |
| 379 |
* d1...dn/d <- s1...sn/s |
| 380 |
* |
| 381 |
* However, if the spill code may ovewrite the spill location |
| 382 |
* shared by other uses, we do the following less |
| 383 |
* efficient scheme: |
| 384 |
* |
| 385 |
* /* save the result of d */ |
| 386 |
* d1...dn, tmp <- s1...sn, s |
| 387 |
* spill tmp to spillLoc /* spill d */ |
| 388 |
* |
| 389 |
*) |
| 390 |
fun spillCopyDst(pt,instr,regToSpill,spillLoc, |
| 391 |
kill,env,don'tOverwrite) = |
| 392 |
let val (dst, src) = P.moveDstSrc instr |
| 393 |
val (mvSrc,copyDst,copySrc,kill) = |
| 394 |
extractDef(regToSpill,dst,src,kill) |
| 395 |
val copy = case copyDst of |
| 396 |
[] => [] |
| 397 |
| _ => copyInstr((copyDst,copySrc),instr) |
| 398 |
in if kill |
| 399 |
then (* kill the move *) |
| 400 |
((* print ("Copy "^Int.toString(hd mvDst)^" <- "^ |
| 401 |
Int.toString(hd mvSrc)^" removed\n"); *) |
| 402 |
(copy, env) |
| 403 |
) |
| 404 |
else (* normal spill *) |
| 405 |
if spillConflict(spillLoc, don'tOverwrite) orelse |
| 406 |
(* if the register is a dedicated register, treat the copy as |
| 407 |
* a normal spill. this is necessary because dedicated registers |
| 408 |
* have been removed from the def/use information. |
| 409 |
*) |
| 410 |
dedicated(CBase.registerId mvSrc) then |
| 411 |
let (* cycle found *) |
| 412 |
(*val _ = print("Register r"^Int.toString regToSpill^ |
| 413 |
" overwrites ["^Int.toString spillLoc^"]\n")*) |
| 414 |
val tmp = I.C.newVar regToSpill (* new temporary *) |
| 415 |
val copy = copyInstr((tmp::copyDst, mvSrc::copySrc), |
| 416 |
instr) |
| 417 |
val spillCode = spillSrc{src=tmp,reg=regToSpill, |
| 418 |
spillLoc=spillLoc, |
| 419 |
annotations=annotations} |
| 420 |
in (copy @ spillCode, [(regToSpill,tmp,pt)]) |
| 421 |
end |
| 422 |
else |
| 423 |
let (* spill the move instruction *) |
| 424 |
val spillCode = spillSrc{src=mvSrc,reg=regToSpill, |
| 425 |
spillLoc=spillLoc, |
| 426 |
annotations=annotations} |
| 427 |
in (spillCode @ copy, [(regToSpill,mvSrc,pt)]) |
| 428 |
end |
| 429 |
end |
| 430 |
|
| 431 |
(* |
| 432 |
* Insert spill code for a copy |
| 433 |
*) |
| 434 |
fun spillCopy(pt,instr,regToSpill,spillLoc,kill,env,don'tOverwrite)= |
| 435 |
case P.moveTmpR instr of |
| 436 |
NONE => spillCopyDst(pt,instr,regToSpill,spillLoc,kill,env, |
| 437 |
don'tOverwrite) |
| 438 |
| SOME tmp => |
| 439 |
if same(tmp, regToSpill) |
| 440 |
then ((* spilledCopyTmps := !spilledCopyTmps + 1; *) |
| 441 |
[spillCopyTmp{copy=instr, spillLoc=spillLoc, |
| 442 |
reg=regToSpill, |
| 443 |
annotations=annotations}], []) |
| 444 |
else spillCopyDst(pt,instr,regToSpill,spillLoc,kill, |
| 445 |
env, don'tOverwrite) |
| 446 |
|
| 447 |
(* |
| 448 |
* Insert spill code |
| 449 |
*) |
| 450 |
fun spill(pt,instr,regToSpill,spillLoc,killSet,env,don'tOverwrite) = |
| 451 |
let val kill = contains(regToSpill,killSet) |
| 452 |
in if P.moveInstr instr then |
| 453 |
spillCopy(pt,instr,regToSpill,spillLoc,kill,env,don'tOverwrite) |
| 454 |
else |
| 455 |
spillInstr(pt,instr,regToSpill,spillLoc,kill,env) |
| 456 |
end |
| 457 |
|
| 458 |
fun contains([],reg) = false |
| 459 |
| contains(r::rs,reg) = same(r,reg) orelse contains(rs,reg) |
| 460 |
fun hasDef(i,reg) = contains(#1(insnDefUse i),reg) |
| 461 |
fun hasUse(i,reg) = contains(#2(insnDefUse i),reg) |
| 462 |
|
| 463 |
fun spillOneReg(pt,[],_,_,_,env,_) = ([], env) |
| 464 |
| spillOneReg(pt,i::instrs,r,spillLoc,killSet,env,don'tOverwrite)= |
| 465 |
if hasDef(i,r) then |
| 466 |
let val (instrs',env) = |
| 467 |
spill(pt,i,r,spillLoc,killSet,env,don'tOverwrite) |
| 468 |
in spillOneReg(pt,instrs'@instrs,r,spillLoc, |
| 469 |
killSet,env,don'tOverwrite) |
| 470 |
end |
| 471 |
else |
| 472 |
let val (instrs,env) = |
| 473 |
spillOneReg(pt,instrs,r,spillLoc,killSet, |
| 474 |
env,don'tOverwrite) |
| 475 |
in (i::instrs, env) |
| 476 |
end |
| 477 |
|
| 478 |
fun reloadOneReg(pt,[],_,env,_) = ([], env) |
| 479 |
| reloadOneReg(pt,i::instrs,r,env,spillLoc) = |
| 480 |
if hasUse(i,r) then |
| 481 |
let val (instrs',env) = reload(pt,i,r,env,spillLoc) |
| 482 |
in reloadOneReg(pt,instrs'@instrs,r,env,spillLoc) |
| 483 |
end |
| 484 |
else |
| 485 |
let val (instrs, env) = reloadOneReg(pt,instrs,r,env,spillLoc) |
| 486 |
in (i::instrs, env) |
| 487 |
end |
| 488 |
|
| 489 |
(* This function spills a set of registers for an instruction *) |
| 490 |
fun spillAll(pt,instrs,[],killSet,env,don'tOverwrite) = (instrs,env) |
| 491 |
| spillAll(pt,instrs,r::rs,killSet,env,don'tOverwrite) = |
| 492 |
let val node = getnode r |
| 493 |
val spillLoc = getLoc node |
| 494 |
val (instrs, env) = |
| 495 |
spillOneReg(pt,instrs,r,spillLoc,killSet, |
| 496 |
env,don'tOverwrite) |
| 497 |
in spillAll(pt,instrs,rs,killSet,env,don'tOverwrite) |
| 498 |
end |
| 499 |
|
| 500 |
(* This function reloads a set of registers for an instruction *) |
| 501 |
fun reloadAll(pt,instrs,env,[]) = (instrs, env) |
| 502 |
| reloadAll(pt,instrs,env,r::rs) = |
| 503 |
let val node = getnode r |
| 504 |
val spillLoc = getLoc node |
| 505 |
val (instrs, env) = reloadOneReg(pt,instrs,r,env,spillLoc) |
| 506 |
in reloadAll(pt, instrs, env, rs) |
| 507 |
end |
| 508 |
|
| 509 |
fun loop([], pt, env, newInstrs) = newInstrs |
| 510 |
| loop(instr::rest, pt, env, newInstrs) = |
| 511 |
let val spillRegs = getSpills pt |
| 512 |
val reloadRegs = getReloads pt |
| 513 |
in case (spillRegs, reloadRegs) of |
| 514 |
([], []) => |
| 515 |
let val env' = |
| 516 |
case env of |
| 517 |
[] => [] (* An approximation here *) |
| 518 |
| _ => let val (defs, uses) = insnDefUse instr |
| 519 |
in if hasNonDedicated defs orelse |
| 520 |
hasNonDedicated uses then [] |
| 521 |
else env |
| 522 |
end |
| 523 |
(* should be handled better *) |
| 524 |
in loop(rest, dec pt, env', instr::newInstrs) |
| 525 |
end |
| 526 |
| _ => |
| 527 |
(* Eliminate duplicates from the spill/reload candidates *) |
| 528 |
let val killRegs = getKills pt |
| 529 |
val spillRegs = uniq spillRegs |
| 530 |
val reloadRegs = uniq reloadRegs |
| 531 |
|
| 532 |
(* spill locations that we can't overwrite if we |
| 533 |
* are spilling a parallel copy |
| 534 |
*) |
| 535 |
val don'tOverwrite = |
| 536 |
if parallelCopies then spillLocsOf reloadRegs |
| 537 |
else [] |
| 538 |
|
| 539 |
fun prEnv env = ( |
| 540 |
print("Env="); |
| 541 |
app (fn (r,v,_) => |
| 542 |
print(concat[ |
| 543 |
CBase.toString r, "=>", |
| 544 |
CBase.toString v, " " |
| 545 |
])) env; |
| 546 |
print "\n") |
| 547 |
|
| 548 |
val (instrs,env) = |
| 549 |
spillAll(pt,[instr],spillRegs,killRegs, |
| 550 |
env,don'tOverwrite) |
| 551 |
|
| 552 |
val _ = if debug then |
| 553 |
(print("pt="^pt2s pt^"\n"); |
| 554 |
case spillRegs of |
| 555 |
[] => () |
| 556 |
| _ => (print("Spilling "); |
| 557 |
printRegs spillRegs; |
| 558 |
print "\n"); |
| 559 |
case reloadRegs of |
| 560 |
[] => () |
| 561 |
| _ => (print("Reloading "); |
| 562 |
printRegs reloadRegs; |
| 563 |
print "\n"); |
| 564 |
print "Before:"; emit instr; |
| 565 |
prEnv env |
| 566 |
) else () |
| 567 |
|
| 568 |
val (instrs, env) = |
| 569 |
reloadAll(pt,instrs,env,reloadRegs) |
| 570 |
|
| 571 |
val _ = if debug then |
| 572 |
(print "After:"; app emit instrs; |
| 573 |
print "------------------\n") |
| 574 |
else () |
| 575 |
|
| 576 |
fun concat([], l) = l |
| 577 |
| concat(a::b, l) = concat(b, a::l) |
| 578 |
in loop(rest, dec pt, env, concat(instrs, newInstrs)) |
| 579 |
end |
| 580 |
end |
| 581 |
in loop(rev instrs, pt, [], []) |
| 582 |
end |
| 583 |
in spillRewrite |
| 584 |
end |
| 585 |
end |