| 1 |
(* |
| 2 |
* X86 specific register allocator. |
| 3 |
* This module abstracts out all the nasty RA business on the x86. |
| 4 |
* So you should only have to write the callbacks. |
| 5 |
* |
| 6 |
* Here's more some info on the x86 functor. |
| 7 |
*Basically the new functor encapsulates all the features in the |
| 8 |
*x86 register allocator, including things like memory pseudo registers, |
| 9 |
*and the new floating point allocator that maps things onto the %st registers. |
| 10 |
*For floating point, we can also switch between the sethi-ullman mode and |
| 11 |
*the %st register mode. |
| 12 |
* |
| 13 |
* Notes on the parameters of the functor: |
| 14 |
* |
| 15 |
*> structure SpillHeur : RA_SPILL_HEURISTICS |
| 16 |
* |
| 17 |
* This should be one of the spill heuristic module like ChaitinSpillHeur or |
| 18 |
* Command ('i' to return to index): you can also roll your own. |
| 19 |
* |
| 20 |
*> structure Spill : RA_SPILL |
| 21 |
* |
| 22 |
* This should be either RASpill or RASpillWithRenaming. |
| 23 |
* |
| 24 |
*> val fast_floating_point : bool ref |
| 25 |
* |
| 26 |
* This flag is used to turn on the new x86 fp mode. The same flag |
| 27 |
* is also passed to the x86 instruction selection module. |
| 28 |
* |
| 29 |
*> datatype raPhase = SPILL_PROPAGATION | SPILL_COLORING |
| 30 |
* |
| 31 |
* This datatype specifies which additional phases we should run. |
| 32 |
* |
| 33 |
*> val beforeRA : flowgraph -> spill_info |
| 34 |
* |
| 35 |
* This callback is invoked before each call to RA. The RA may have |
| 36 |
* to perform both integer and floating point RA. This is called before |
| 37 |
* integer RA. |
| 38 |
* |
| 39 |
* The callbacks for integer and floating point are separated into |
| 40 |
* the substructures Int and Float. |
| 41 |
* |
| 42 |
*> structure Int : |
| 43 |
*> sig |
| 44 |
*> val avail : I.C.cell list |
| 45 |
*> val dedicated : I.C.cell list |
| 46 |
*> val memRegs : I.C.cell list |
| 47 |
*> val phases : raPhase list |
| 48 |
*> val spillLoc : spill_info * Annotations.annotations ref * |
| 49 |
*> RAGraph.logical_spill_id -> I.operand |
| 50 |
*> val spillInit : RAGraph.interferenceGraph -> unit |
| 51 |
*> end |
| 52 |
* |
| 53 |
* avail is the list of registers available for allocation |
| 54 |
* memRegs is the list of memory registers that may appear in the program |
| 55 |
* phases is a list of additional RA phases. I recommend turning on |
| 56 |
* everything: |
| 57 |
* |
| 58 |
* [SPILL_PROPAGATION, SPILL_COLORING] |
| 59 |
* |
| 60 |
* spillInit is called once before spilling occurs. |
| 61 |
* |
| 62 |
* spillLoc is a callback that maps logical_spill_ids into an x86 |
| 63 |
* effective address. The list of allocations is from the block in which |
| 64 |
* the spilled instruction occurs. The client should keep track of |
| 65 |
* existing ids, and allocate a new effective address when a new id occurs. |
| 66 |
* In general, the client should keep track of a single table of free |
| 67 |
* spill space for both integer and floating point registers. |
| 68 |
* |
| 69 |
* Previously, the spill/reload routines have to do special things in the |
| 70 |
* presence of memory registers, but that stuff is taken care of in the |
| 71 |
* new module, so all spillLoc has to do is map logical_spill_ids into |
| 72 |
* effective address. |
| 73 |
* |
| 74 |
*> structure Float : |
| 75 |
*> sig |
| 76 |
*> val avail : I.C.cell list |
| 77 |
*> val dedicated : I.C.cell list |
| 78 |
*> val memRegs : I.C.cell list |
| 79 |
*> val phases : raPhase list |
| 80 |
*> val spillLoc : spill_info * Annotations.annotations ref * |
| 81 |
*> RAGraph.logical_spill_id -> I.operand |
| 82 |
*> val spillInit : RAGraph.interferenceGraph -> unit |
| 83 |
*> end |
| 84 |
* |
| 85 |
* For floating point, it is similar. |
| 86 |
* |
| 87 |
*> |
| 88 |
*> val fastMemRegs : I.C.cell list |
| 89 |
*> val fastPhases : raPhase list |
| 90 |
* |
| 91 |
* When fast_floating_point is turned on, we use different parameters: |
| 92 |
* |
| 93 |
* avail is set to [%st(0), ..., %st(6)] |
| 94 |
* dedicated is set to [] |
| 95 |
* memRegs is set to fastMemRegs |
| 96 |
* |
| 97 |
* In general, the flow of the module is like this: |
| 98 |
* |
| 99 |
* ra: |
| 100 |
* call beforeRA() |
| 101 |
* integer RA --- call Int.spillInit() once if spilling is needed |
| 102 |
* floating fp RA --- call Real.spillInit() once if spilling is needed |
| 103 |
* if !fast_floating_point then |
| 104 |
* invoke the module X86FP to convert fake %fp registers |
| 105 |
* into real %st registers |
| 106 |
* endif |
| 107 |
* |
| 108 |
*) |
| 109 |
|
| 110 |
functor X86RA |
| 111 |
( structure I : X86INSTR |
| 112 |
structure InsnProps : INSN_PROPERTIES (* where I = I *) |
| 113 |
where type I.addressing_mode = I.addressing_mode |
| 114 |
and type I.ea = I.ea |
| 115 |
and type I.instr = I.instr |
| 116 |
and type I.instruction = I.instruction |
| 117 |
and type I.operand = I.operand |
| 118 |
structure CFG : CONTROL_FLOW_GRAPH (* where I = I *) |
| 119 |
where type I.addressing_mode = I.addressing_mode |
| 120 |
and type I.ea = I.ea |
| 121 |
and type I.instr = I.instr |
| 122 |
and type I.instruction = I.instruction |
| 123 |
and type I.operand = I.operand |
| 124 |
structure Asm : INSTRUCTION_EMITTER (* where I = I and S.P = CFG.P *) |
| 125 |
where type I.addressing_mode = I.addressing_mode |
| 126 |
and type I.ea = I.ea |
| 127 |
and type I.instr = I.instr |
| 128 |
and type I.instruction = I.instruction |
| 129 |
and type I.operand = I.operand |
| 130 |
where type S.P.Client.pseudo_op = CFG.P.Client.pseudo_op |
| 131 |
and type S.P.T.Basis.cond = CFG.P.T.Basis.cond |
| 132 |
and type S.P.T.Basis.div_rounding_mode = CFG.P.T.Basis.div_rounding_mode |
| 133 |
and type S.P.T.Basis.ext = CFG.P.T.Basis.ext |
| 134 |
and type S.P.T.Basis.fcond = CFG.P.T.Basis.fcond |
| 135 |
and type S.P.T.Basis.rounding_mode = CFG.P.T.Basis.rounding_mode |
| 136 |
and type S.P.T.Constant.const = CFG.P.T.Constant.const |
| 137 |
and type ('s,'r,'f,'c) S.P.T.Extension.ccx = ('s,'r,'f,'c) CFG.P.T.Extension.ccx |
| 138 |
and type ('s,'r,'f,'c) S.P.T.Extension.fx = ('s,'r,'f,'c) CFG.P.T.Extension.fx |
| 139 |
and type ('s,'r,'f,'c) S.P.T.Extension.rx = ('s,'r,'f,'c) CFG.P.T.Extension.rx |
| 140 |
and type ('s,'r,'f,'c) S.P.T.Extension.sx = ('s,'r,'f,'c) CFG.P.T.Extension.sx |
| 141 |
and type S.P.T.I.div_rounding_mode = CFG.P.T.I.div_rounding_mode |
| 142 |
and type S.P.T.Region.region = CFG.P.T.Region.region |
| 143 |
and type S.P.T.ccexp = CFG.P.T.ccexp |
| 144 |
and type S.P.T.fexp = CFG.P.T.fexp |
| 145 |
(* and type S.P.T.labexp = CFG.P.T.labexp *) |
| 146 |
and type S.P.T.mlrisc = CFG.P.T.mlrisc |
| 147 |
and type S.P.T.oper = CFG.P.T.oper |
| 148 |
and type S.P.T.rep = CFG.P.T.rep |
| 149 |
and type S.P.T.rexp = CFG.P.T.rexp |
| 150 |
and type S.P.T.stm = CFG.P.T.stm |
| 151 |
|
| 152 |
(* Spilling heuristics determines which node should be spilled |
| 153 |
* You can use Chaitin, ChowHenessey, or one of your own. |
| 154 |
*) |
| 155 |
structure SpillHeur : RA_SPILL_HEURISTICS |
| 156 |
|
| 157 |
(* The Spill module figures out the strategies for inserting |
| 158 |
* spill code. You can use RASpill, or RASpillWithRenaming, |
| 159 |
* or write your own if you are feeling adventurous. |
| 160 |
*) |
| 161 |
structure Spill : RA_SPILL (* where I = I *) |
| 162 |
where type I.addressing_mode = I.addressing_mode |
| 163 |
and type I.ea = I.ea |
| 164 |
and type I.instr = I.instr |
| 165 |
and type I.instruction = I.instruction |
| 166 |
and type I.operand = I.operand |
| 167 |
|
| 168 |
|
| 169 |
type spill_info (* user-defined abstract type *) |
| 170 |
|
| 171 |
(* Should we use allocate register on the floating point stack? |
| 172 |
* Note that this flag must match the one passed to the code generator |
| 173 |
* module. |
| 174 |
*) |
| 175 |
val fast_floating_point : bool ref |
| 176 |
|
| 177 |
datatype raPhase = SPILL_PROPAGATION |
| 178 |
| SPILL_COLORING |
| 179 |
|
| 180 |
datatype spillOperandKind = SPILL_LOC | CONST_VAL |
| 181 |
|
| 182 |
(* Called before register allocation; perform your initialization here. *) |
| 183 |
val beforeRA : CFG.cfg -> spill_info |
| 184 |
|
| 185 |
(* Integer register allocation parameters *) |
| 186 |
structure Int : |
| 187 |
sig |
| 188 |
val avail : CellsBasis.cell list |
| 189 |
val dedicated : CellsBasis.cell list |
| 190 |
val memRegs : CellsBasis.cell list |
| 191 |
val phases : raPhase list |
| 192 |
|
| 193 |
val spillLoc : {info:spill_info, |
| 194 |
an :Annotations.annotations ref, |
| 195 |
cell:CellsBasis.cell, (* spilled cell *) |
| 196 |
id :RAGraph.logical_spill_id |
| 197 |
} -> |
| 198 |
{ opnd: I.ea, |
| 199 |
kind: spillOperandKind |
| 200 |
} |
| 201 |
|
| 202 |
(* This function is called once before spilling begins *) |
| 203 |
val spillInit : RAGraph.interferenceGraph -> unit |
| 204 |
|
| 205 |
end |
| 206 |
|
| 207 |
(* Floating point register allocation parameters *) |
| 208 |
structure Float : |
| 209 |
sig |
| 210 |
(* Sethi-Ullman mode *) |
| 211 |
val avail : CellsBasis.cell list |
| 212 |
val dedicated : CellsBasis.cell list |
| 213 |
val memRegs : CellsBasis.cell list |
| 214 |
val phases : raPhase list |
| 215 |
|
| 216 |
val spillLoc : spill_info * Annotations.annotations ref * RAGraph.logical_spill_id |
| 217 |
-> I.ea |
| 218 |
|
| 219 |
(* This function is called once before spilling begins *) |
| 220 |
val spillInit : RAGraph.interferenceGraph -> unit |
| 221 |
|
| 222 |
(* When fast_floating_point is on, use these instead: *) |
| 223 |
val fastMemRegs : CellsBasis.cell list |
| 224 |
val fastPhases : raPhase list |
| 225 |
end |
| 226 |
|
| 227 |
) : CFG_OPTIMIZATION = |
| 228 |
struct |
| 229 |
|
| 230 |
structure CFG = CFG |
| 231 |
structure I = I |
| 232 |
structure C = I.C |
| 233 |
structure CB = CellsBasis |
| 234 |
|
| 235 |
val name = "X86RA" |
| 236 |
|
| 237 |
type flowgraph = CFG.cfg |
| 238 |
|
| 239 |
val intSpillCnt = MLRiscControl.mkCounter ("ra-int-spills", "RA int spill count") |
| 240 |
val intReloadCnt = MLRiscControl.mkCounter ("ra-int-reloads", "RA int reload count") |
| 241 |
val intRenameCnt = MLRiscControl.mkCounter ("ra-int-renames", "RA int rename count") |
| 242 |
val floatSpillCnt = MLRiscControl.mkCounter ("ra-float-spills", "RA float spill count") |
| 243 |
val floatReloadCnt = MLRiscControl.mkCounter ("ra-float-reloads", "RA float reload count") |
| 244 |
val floatRenameCnt = MLRiscControl.mkCounter ("ra-float-renames", "RA float rename count") |
| 245 |
|
| 246 |
fun inc c = c := !c + 1 |
| 247 |
|
| 248 |
val x86CfgDebugFlg = MLRiscControl.mkFlag ("x86-cfg-debug", "x86 CFG debug mode") |
| 249 |
|
| 250 |
fun error msg = MLRiscErrorMsg.error("X86RA",msg) |
| 251 |
|
| 252 |
(* |
| 253 |
val deadcode = MLRiscControl.getCounter "x86-dead-code" |
| 254 |
val deadblocks = MLRiscControl.getCounter "x86-dead-blocks" |
| 255 |
*) |
| 256 |
|
| 257 |
structure PrintFlowgraph= |
| 258 |
PrintFlowgraph(structure CFG=CFG |
| 259 |
structure Asm = Asm) |
| 260 |
|
| 261 |
structure X86FP = |
| 262 |
X86FP(structure X86Instr = I |
| 263 |
structure X86Props = InsnProps |
| 264 |
structure Flowgraph = CFG |
| 265 |
structure Liveness = Liveness(CFG) |
| 266 |
structure Asm = Asm |
| 267 |
) |
| 268 |
|
| 269 |
structure X86SpillInstr = X86SpillInstr(structure Instr=I structure Props=InsnProps) |
| 270 |
val spillFInstr = X86SpillInstr.spill CB.FP |
| 271 |
val reloadFInstr = X86SpillInstr.reload CB.FP |
| 272 |
val spillInstr = X86SpillInstr.spill CB.GP |
| 273 |
val reloadInstr = X86SpillInstr.reload CB.GP |
| 274 |
|
| 275 |
fun annotate([], i) = i |
| 276 |
| annotate(a::an, i) = annotate(an, I.ANNOTATION{a=a, i=i}) |
| 277 |
|
| 278 |
(* |
| 279 |
* Dead code elimination |
| 280 |
*) |
| 281 |
exception X86DeadCode |
| 282 |
val affectedBlocks = |
| 283 |
IntHashTable.mkTable(32,X86DeadCode) : bool IntHashTable.hash_table |
| 284 |
val deadRegs = |
| 285 |
IntHashTable.mkTable(32,X86DeadCode) : bool IntHashTable.hash_table |
| 286 |
|
| 287 |
fun removeDeadCode(cfg as Graph.GRAPH graph) = let |
| 288 |
val blocks = #nodes graph () |
| 289 |
val find = IntHashTable.find deadRegs |
| 290 |
fun isDead r = |
| 291 |
case find (CB.cellId r) of |
| 292 |
SOME _ => true |
| 293 |
| NONE => false |
| 294 |
fun isAffected i = getOpt (IntHashTable.find affectedBlocks i, false) |
| 295 |
fun isDeadInstr(I.ANNOTATION{i, ...}) = isDeadInstr i |
| 296 |
| isDeadInstr(I.INSTR(I.MOVE{dst=I.Direct rd, ...})) = isDead rd |
| 297 |
| isDeadInstr(I.INSTR(I.MOVE{dst=I.MemReg rd, ...})) = isDead rd |
| 298 |
| isDeadInstr(I.COPY{k=CB.GP, dst=[rd], ...}) = isDead rd |
| 299 |
| isDeadInstr _ = false |
| 300 |
fun scan [] = () |
| 301 |
| scan((blknum, CFG.BLOCK{insns, ...})::rest) = |
| 302 |
(if isAffected blknum then |
| 303 |
((* deadblocks := !deadblocks + 1; *) |
| 304 |
insns := elim(!insns, []) |
| 305 |
) else (); |
| 306 |
scan rest) |
| 307 |
and elim([], code) = rev code |
| 308 |
| elim(i::instrs, code) = |
| 309 |
if isDeadInstr i then |
| 310 |
((* deadcode := !deadcode + 1; *) elim(instrs, code)) |
| 311 |
else elim(instrs, i::code) |
| 312 |
in if IntHashTable.numItems affectedBlocks > 0 then |
| 313 |
(scan blocks; |
| 314 |
IntHashTable.clear deadRegs; |
| 315 |
IntHashTable.clear affectedBlocks) |
| 316 |
else () |
| 317 |
end |
| 318 |
|
| 319 |
(* This function finds out which pseudo memory registers are unused. |
| 320 |
* Those that are unused are made available for spilling. |
| 321 |
* The register allocator calls this function right before spilling |
| 322 |
* a set of nodes. |
| 323 |
*) |
| 324 |
val firstSpill = ref true |
| 325 |
val firstFPSpill = ref true |
| 326 |
|
| 327 |
fun spillInit(graph, CB.GP) = |
| 328 |
if !firstSpill then (* only do this once! *) |
| 329 |
(Int.spillInit graph; |
| 330 |
firstSpill := false |
| 331 |
) |
| 332 |
else () |
| 333 |
| spillInit(graph, CB.FP) = |
| 334 |
if !firstFPSpill then |
| 335 |
(Float.spillInit graph; |
| 336 |
firstFPSpill := false |
| 337 |
) |
| 338 |
else () |
| 339 |
| spillInit _ = error "spillInit" |
| 340 |
|
| 341 |
(* This is the generic register allocator *) |
| 342 |
structure Ra = |
| 343 |
RegisterAllocator |
| 344 |
(structure SpillHeuristics = SpillHeur |
| 345 |
structure Flowgraph = MemoryRA (* for memory coalescing *) |
| 346 |
(RADeadCodeElim (* do the funky dead code elimination stuff *) |
| 347 |
(structure Flowgraph = ClusterRA |
| 348 |
(structure Flowgraph = CFG |
| 349 |
structure Asm = Asm |
| 350 |
structure InsnProps = InsnProps |
| 351 |
structure Spill = Spill |
| 352 |
) |
| 353 |
fun cellkind CB.GP = true | cellkind _ = false |
| 354 |
val deadRegs = deadRegs |
| 355 |
val affectedBlocks = affectedBlocks |
| 356 |
val spillInit = spillInit |
| 357 |
) |
| 358 |
) |
| 359 |
) |
| 360 |
|
| 361 |
|
| 362 |
(* ------------------------------------------------------------------- |
| 363 |
* Floating point stuff |
| 364 |
* -------------------------------------------------------------------*) |
| 365 |
val KF32 = length Float.avail |
| 366 |
structure FR32 = GetReg(val nRegs=KF32 |
| 367 |
val available=map CB.registerId Float.avail |
| 368 |
val first=CB.registerId(I.C.ST 8)) |
| 369 |
|
| 370 |
val availF8 = C.Regs CB.FP {from=0, to=6, step=1} |
| 371 |
val KF8 = length availF8 |
| 372 |
structure FR8 = GetReg(val nRegs=KF8 |
| 373 |
val available=map CB.registerId availF8 |
| 374 |
val first=CB.registerId(I.C.ST 0)) |
| 375 |
|
| 376 |
(* ------------------------------------------------------------------- |
| 377 |
* Callbacks for floating point K=32 |
| 378 |
* -------------------------------------------------------------------*) |
| 379 |
fun fcopy{dst, src, tmp} = |
| 380 |
I.COPY{k=CB.FP, sz=64, dst=dst, src=src, tmp=tmp} |
| 381 |
|
| 382 |
fun copyInstrF((rds as [_], rss as [_]), _) = |
| 383 |
fcopy{dst=rds, src=rss, tmp=NONE} |
| 384 |
| copyInstrF((rds, rss), I.COPY{k=CB.FP, tmp, ...}) = |
| 385 |
fcopy{dst=rds, src=rss, tmp=tmp} |
| 386 |
| copyInstrF(x, I.ANNOTATION{i,a}) = |
| 387 |
I.ANNOTATION{i=copyInstrF(x, i), a=a} |
| 388 |
| copyInstrF _ = error "copyInstrF" |
| 389 |
|
| 390 |
val copyInstrF = fn x => [copyInstrF x] |
| 391 |
|
| 392 |
fun getFregLoc(S, an, Ra.FRAME loc) = Float.spillLoc(S, an, loc) |
| 393 |
| getFregLoc(S, an, Ra.MEM_REG r) = I.FDirect r |
| 394 |
|
| 395 |
(* spill floating point *) |
| 396 |
fun spillF S {annotations=an, kill, reg, spillLoc, instr} = let |
| 397 |
(* preserve annotation on instruction *) |
| 398 |
fun spill(instrAn, I.ANNOTATION{a, i}) = spill(a::instrAn, i) |
| 399 |
| spill(instrAn, I.KILL{regs, spilled}) = |
| 400 |
{code= |
| 401 |
[annotate |
| 402 |
(instrAn, |
| 403 |
I.KILL {regs=C.rmvFreg(reg, regs), |
| 404 |
spilled=C.addFreg(reg, spilled)})], |
| 405 |
proh = [], |
| 406 |
newReg=NONE} |
| 407 |
| spill(instrAn, I.LIVE _) = error "spillF: LIVE" |
| 408 |
| spill(_, I.COPY _) = error "spillF: COPY" |
| 409 |
| spill(instrAn, I.INSTR _) = |
| 410 |
(inc floatSpillCnt; |
| 411 |
spillFInstr(instr, reg, getFregLoc(S, an, spillLoc))) |
| 412 |
in spill([], instr) |
| 413 |
end |
| 414 |
|
| 415 |
fun spillFreg S {src, reg, spillLoc, annotations=an} = |
| 416 |
(inc floatSpillCnt; |
| 417 |
let val fstp = [I.fstpl(getFregLoc(S, an, spillLoc))] |
| 418 |
in if CB.sameColor(src,C.ST0) then fstp |
| 419 |
else I.fldl(I.FDirect(src))::fstp |
| 420 |
end |
| 421 |
) |
| 422 |
|
| 423 |
fun spillFcopyTmp S {copy=I.COPY{k=CB.FP, dst, src, ...}, spillLoc, reg, |
| 424 |
annotations=an} = |
| 425 |
(inc floatSpillCnt; |
| 426 |
fcopy{dst=dst, src=src, tmp=SOME(getFregLoc(S, an, spillLoc))} |
| 427 |
) |
| 428 |
| spillFcopyTmp S {copy=I.ANNOTATION{i,a}, spillLoc, reg, annotations} = |
| 429 |
let val i = spillFcopyTmp S {copy=i, spillLoc=spillLoc, reg=reg, |
| 430 |
annotations=annotations} |
| 431 |
in I.ANNOTATION{i=i, a=a} end |
| 432 |
| spillFcopyTmp _ _ = error "spillFcopyTmp" |
| 433 |
|
| 434 |
(* rename floating point *) |
| 435 |
fun renameF{instr, fromSrc, toSrc} = |
| 436 |
(inc floatRenameCnt; |
| 437 |
reloadFInstr(instr, fromSrc, I.FDirect toSrc) |
| 438 |
) |
| 439 |
|
| 440 |
(* reload floating point *) |
| 441 |
fun reloadF S {annotations=an,reg,spillLoc,instr} = let |
| 442 |
fun reload(instrAn, I.ANNOTATION{a,i}) = reload(a::instrAn, i) |
| 443 |
| reload(instrAn, I.LIVE{regs, spilled}) = |
| 444 |
{code=[I.LIVE{regs=C.rmvFreg(reg, regs), spilled=C.addFreg(reg, spilled)}], |
| 445 |
proh=[], |
| 446 |
newReg=NONE} |
| 447 |
| reload(_, I.KILL _) = error "reloadF: KILL" |
| 448 |
| reload (_, I.COPY _) = error "reloadF: COPY" |
| 449 |
| reload(instrAn, instr as I.INSTR _) = |
| 450 |
(inc floatReloadCnt; |
| 451 |
reloadFInstr(instr, reg, getFregLoc(S, an, spillLoc))) |
| 452 |
in reload([], instr) |
| 453 |
end |
| 454 |
|
| 455 |
fun reloadFreg S {dst, reg, spillLoc, annotations=an} = |
| 456 |
(inc floatReloadCnt; |
| 457 |
if CB.sameColor(dst,C.ST0) then |
| 458 |
[I.fldl(getFregLoc(S, an, spillLoc))] |
| 459 |
else |
| 460 |
[I.fldl(getFregLoc(S, an, spillLoc)), I.fstpl(I.FDirect dst)] |
| 461 |
) |
| 462 |
|
| 463 |
(* ------------------------------------------------------------------- |
| 464 |
* Callbacks for floating point K=7 |
| 465 |
* -------------------------------------------------------------------*) |
| 466 |
fun FMemReg f = let val fx = CB.registerNum f |
| 467 |
in if fx >= 8 andalso fx < 32 |
| 468 |
then I.FDirect f else I.FPR f |
| 469 |
end |
| 470 |
|
| 471 |
fun copyInstrF'((rds as [d], rss as [s]), _) = |
| 472 |
I.fmove{fsize=I.FP64,src=FMemReg s,dst=FMemReg d} |
| 473 |
| copyInstrF'((rds, rss), I.COPY{k=CB.FP, tmp, ...}) = |
| 474 |
fcopy{dst=rds, src=rss, tmp=tmp} |
| 475 |
| copyInstrF'(x, I.ANNOTATION{i, a}) = |
| 476 |
I.ANNOTATION{i=copyInstrF'(x,i), a=a} |
| 477 |
| copyInstrF' _ = error "copyInstrF'" |
| 478 |
|
| 479 |
val copyInstrF' = fn x => [copyInstrF' x] |
| 480 |
|
| 481 |
fun spillFreg' S {src, reg, spillLoc, annotations=an} = |
| 482 |
(inc floatSpillCnt; |
| 483 |
[I.fmove{fsize=I.FP64, src=FMemReg src, |
| 484 |
dst=getFregLoc(S, an,spillLoc)}] |
| 485 |
) |
| 486 |
|
| 487 |
fun renameF'{instr, fromSrc, toSrc} = |
| 488 |
(inc floatRenameCnt; |
| 489 |
reloadFInstr(instr, fromSrc, I.FPR toSrc) |
| 490 |
) |
| 491 |
|
| 492 |
fun reloadFreg' S {dst, reg, spillLoc, annotations=an} = |
| 493 |
(inc floatReloadCnt; |
| 494 |
[I.fmove{fsize=I.FP64, dst=FMemReg dst, |
| 495 |
src=getFregLoc(S,an,spillLoc)}] |
| 496 |
) |
| 497 |
|
| 498 |
(* ------------------------------------------------------------------- |
| 499 |
* Integer 8 stuff |
| 500 |
* -------------------------------------------------------------------*) |
| 501 |
fun copy{dst, src, tmp} = I.COPY{k=CB.GP, sz=32, dst=dst, src=src, tmp=tmp} |
| 502 |
fun memToMemMove{dst, src} = |
| 503 |
let val tmp = I.C.newReg() |
| 504 |
in [I.move{mvOp=I.MOVL,src=src,dst=I.Direct tmp}, |
| 505 |
I.move{mvOp=I.MOVL,src=I.Direct tmp,dst=dst} |
| 506 |
] |
| 507 |
end |
| 508 |
|
| 509 |
fun copyInstrR((rds as [d], rss as [s]), _) = |
| 510 |
if CB.sameColor(d,s) then [] else |
| 511 |
let val dx = CB.registerNum d and sx = CB.registerNum s |
| 512 |
in case (dx >= 8 andalso dx < 32, sx >= 8 andalso sx < 32) of |
| 513 |
(false, false) => [copy{dst=rds, src=rss, tmp=NONE}] |
| 514 |
| (true, false) => [I.move{mvOp=I.MOVL,src=I.Direct s, |
| 515 |
dst=I.MemReg d}] |
| 516 |
| (false, true) => [I.move{mvOp=I.MOVL,src=I.MemReg s, |
| 517 |
dst=I.Direct d}] |
| 518 |
| (true, true) => memToMemMove{src=I.MemReg s, dst=I.MemReg d} |
| 519 |
end |
| 520 |
| copyInstrR((rds, rss), I.COPY{k=CB.GP, tmp, ...}) = |
| 521 |
[copy{dst=rds, src=rss, tmp=tmp}] |
| 522 |
| copyInstrR(x, I.ANNOTATION{i, a}) = |
| 523 |
copyInstrR(x, i) (* XXX *) |
| 524 |
| copyInstrR _ = error "copyInstrR" |
| 525 |
|
| 526 |
|
| 527 |
fun getRegLoc(S, an, cell, Ra.FRAME loc) = |
| 528 |
Int.spillLoc{info=S, an=an, cell=cell, id=loc} |
| 529 |
| getRegLoc(S, an, cell, Ra.MEM_REG r) = {opnd=I.MemReg r,kind=SPILL_LOC} |
| 530 |
|
| 531 |
(* No, logical spill locations... *) |
| 532 |
|
| 533 |
structure GR8 = GetReg(val nRegs=8 |
| 534 |
val available=map CB.registerId Int.avail |
| 535 |
val first=0) |
| 536 |
|
| 537 |
val K8 = length Int.avail |
| 538 |
|
| 539 |
(* register allocation for general purpose registers *) |
| 540 |
fun spillR8 S {annotations=an, kill, reg, spillLoc, instr} = let |
| 541 |
fun annotate([], i) = i |
| 542 |
| annotate(a::an, i) = annotate(an, I.ANNOTATION{a=a, i=i}) |
| 543 |
|
| 544 |
(* preserve annotation on instruction *) |
| 545 |
fun spill(instrAn, I.ANNOTATION{a,i}) = spill(a::instrAn, i) |
| 546 |
| spill(instrAn, I.KILL{regs, spilled}) = |
| 547 |
{code= |
| 548 |
[annotate |
| 549 |
(instrAn, |
| 550 |
I.KILL {regs=C.rmvReg(reg, regs), |
| 551 |
spilled=C.addReg(reg, spilled)})], |
| 552 |
proh = [], |
| 553 |
newReg=NONE} |
| 554 |
| spill(instrAn, I.LIVE _) = error "spill: LIVE" |
| 555 |
| spill(_, I.COPY _) = error "spill: COPY" |
| 556 |
| spill(instrAn, I.INSTR _) = |
| 557 |
(case getRegLoc(S, an, reg, spillLoc) |
| 558 |
of {opnd=spillLoc, kind=SPILL_LOC} => |
| 559 |
( inc intSpillCnt; |
| 560 |
spillInstr(annotate(instrAn, instr), reg, spillLoc) |
| 561 |
) |
| 562 |
| _ => (* don't have to spill a constant *) |
| 563 |
{code=[], newReg=NONE, proh=[]} |
| 564 |
(*esac*)) |
| 565 |
in spill([], instr) |
| 566 |
end |
| 567 |
|
| 568 |
fun isMemReg r = let val x = CB.registerNum r |
| 569 |
in x >= 8 andalso x < 32 end |
| 570 |
|
| 571 |
fun spillReg S {src, reg, spillLoc, annotations=an} = |
| 572 |
let val _ = inc intSpillCnt |
| 573 |
val {opnd=dstLoc,kind} = getRegLoc(S,an,reg,spillLoc) |
| 574 |
val isMemReg = isMemReg src |
| 575 |
val srcLoc = if isMemReg then I.MemReg src else I.Direct src |
| 576 |
in if kind=CONST_VAL orelse InsnProps.eqOpn(srcLoc, dstLoc) then [] |
| 577 |
else if isMemReg then memToMemMove{dst=dstLoc, src=srcLoc} |
| 578 |
else [I.move{mvOp=I.MOVL, src=srcLoc, dst=dstLoc}] |
| 579 |
end |
| 580 |
|
| 581 |
fun spillCopyTmp S {copy=I.COPY{k=CB.GP, src, dst,...}, |
| 582 |
reg, spillLoc, annotations=an} = |
| 583 |
(case getRegLoc(S, an, reg, spillLoc) of |
| 584 |
{opnd=tmp, kind=SPILL_LOC} => |
| 585 |
(inc intSpillCnt; |
| 586 |
copy{dst=dst, src=src, tmp=SOME tmp} |
| 587 |
) |
| 588 |
| _ => error "spillCopyTmp" |
| 589 |
) |
| 590 |
| spillCopyTmp S {copy=I.ANNOTATION{i, a}, reg, spillLoc, annotations} = |
| 591 |
I.ANNOTATION{i=spillCopyTmp S {copy=i, reg=reg, spillLoc=spillLoc, |
| 592 |
annotations=annotations}, a=a} |
| 593 |
| spillCopyTmp _ _ = error "spillCopyTmp(2)" |
| 594 |
|
| 595 |
fun renameR8{instr, fromSrc, toSrc} = |
| 596 |
(inc intRenameCnt; |
| 597 |
reloadInstr(instr, fromSrc, I.Direct toSrc) |
| 598 |
) |
| 599 |
|
| 600 |
|
| 601 |
fun reloadR8 S {annotations=an, reg, spillLoc, instr} = let |
| 602 |
fun reload(instrAn, I.ANNOTATION{a,i}) = reload(a::instrAn, i) |
| 603 |
| reload(instrAn, I.LIVE{regs, spilled}) = |
| 604 |
{code=[I.LIVE{regs=C.rmvReg(reg, regs), spilled=C.addReg(reg, spilled)}], |
| 605 |
proh=[], |
| 606 |
newReg=NONE} |
| 607 |
| reload(_, I.KILL _) = error "reload: KILL" |
| 608 |
| reload (_, I.COPY _) = error "reload: COPY" |
| 609 |
| reload(instrAn, instr as I.INSTR _) = |
| 610 |
( inc intReloadCnt; |
| 611 |
reloadInstr(annotate(instrAn, instr), reg, #opnd(getRegLoc(S,an,reg,spillLoc))) |
| 612 |
) |
| 613 |
in reload([], instr) |
| 614 |
end |
| 615 |
|
| 616 |
fun reloadReg S {dst, reg, spillLoc, annotations=an} = |
| 617 |
let val _ = inc intReloadCnt |
| 618 |
val srcLoc = #opnd(getRegLoc(S, an, reg, spillLoc)) |
| 619 |
val isMemReg = isMemReg dst |
| 620 |
val dstLoc = if isMemReg then I.MemReg dst else I.Direct dst |
| 621 |
in if InsnProps.eqOpn(srcLoc,dstLoc) then [] |
| 622 |
else if isMemReg then memToMemMove{dst=dstLoc, src=srcLoc} |
| 623 |
else [I.move{mvOp=I.MOVL, src=srcLoc, dst=dstLoc}] |
| 624 |
end |
| 625 |
|
| 626 |
fun resetRA() = |
| 627 |
(firstSpill := true; |
| 628 |
firstFPSpill := true; |
| 629 |
IntHashTable.clear affectedBlocks; |
| 630 |
IntHashTable.clear deadRegs; |
| 631 |
if !fast_floating_point then FR8.reset() else FR32.reset(); |
| 632 |
GR8.reset() |
| 633 |
) |
| 634 |
|
| 635 |
(* Dedicated + available registers *) |
| 636 |
local |
| 637 |
fun mark(arr, _, [], others) = others |
| 638 |
| mark(arr, len, r::rs, others) = let |
| 639 |
val r = CB.registerId r |
| 640 |
in |
| 641 |
if r >= len then mark(arr, len, rs, r::others) |
| 642 |
else (Array.update(arr, r, true); mark(arr, len, rs, others)) |
| 643 |
end |
| 644 |
val dedicatedR = Array.array(32,false) |
| 645 |
val dedicatedF32 = Array.array(64,false) |
| 646 |
val otherR = mark(dedicatedR, 32, Int.dedicated, []) |
| 647 |
val otherF32 = mark(dedicatedF32, 64, Float.dedicated, []) |
| 648 |
fun isDedicated (len, arr, other) r = |
| 649 |
(r < len andalso Array.sub(arr, r)) orelse List.exists (fn d => r = d) other |
| 650 |
in |
| 651 |
val isDedicatedR : int -> bool = isDedicated (32, dedicatedR, otherR) |
| 652 |
val isDedicatedF32 : int -> bool = isDedicated (64, dedicatedF32, otherF32) |
| 653 |
val isDedicatedF8 : int -> bool = fn _ => false |
| 654 |
end |
| 655 |
|
| 656 |
fun phases ps = |
| 657 |
let fun f([], m) = m |
| 658 |
| f(SPILL_PROPAGATION::ps, m) = f(ps, Ra.SPILL_PROPAGATION+m) |
| 659 |
| f(SPILL_COLORING::ps, m) = f(ps, Ra.SPILL_COLORING+m) |
| 660 |
in f(ps, Ra.NO_OPTIMIZATION) |
| 661 |
end |
| 662 |
|
| 663 |
(* RA parameters *) |
| 664 |
|
| 665 |
(* How to allocate integer registers: |
| 666 |
* Perform register alocation + memory allocation |
| 667 |
*) |
| 668 |
fun RAInt S = |
| 669 |
{spill = spillR8 S, |
| 670 |
spillSrc = spillReg S, |
| 671 |
spillCopyTmp= spillCopyTmp S, |
| 672 |
reload = reloadR8 S, |
| 673 |
reloadDst = reloadReg S, |
| 674 |
renameSrc = renameR8, |
| 675 |
copyInstr = copyInstrR, |
| 676 |
K = K8, |
| 677 |
getreg = GR8.getreg, |
| 678 |
cellkind = CB.GP, |
| 679 |
dedicated = isDedicatedR, |
| 680 |
spillProh = [], |
| 681 |
memRegs = Int.memRegs, |
| 682 |
mode = phases(Int.phases) |
| 683 |
} : Ra.raClient |
| 684 |
|
| 685 |
(* How to allocate floating point registers: |
| 686 |
* Allocate all fp registers on the stack. This is the easy way. |
| 687 |
*) |
| 688 |
fun RAFP32 S = |
| 689 |
{spill = spillF S, |
| 690 |
spillSrc = spillFreg S, |
| 691 |
spillCopyTmp= spillFcopyTmp S, |
| 692 |
reload = reloadF S, |
| 693 |
reloadDst = reloadFreg S, |
| 694 |
renameSrc = renameF, |
| 695 |
copyInstr = copyInstrF, |
| 696 |
K = KF32, |
| 697 |
getreg = FR32.getreg, |
| 698 |
cellkind = CB.FP, |
| 699 |
dedicated = isDedicatedF32, |
| 700 |
spillProh = [], |
| 701 |
memRegs = Float.memRegs, |
| 702 |
mode = phases(Float.phases) |
| 703 |
} : Ra.raClient |
| 704 |
|
| 705 |
(* How to allocate floating point registers: |
| 706 |
* Allocate fp registers on the %st stack. Also perform |
| 707 |
* memory allcoation. |
| 708 |
*) |
| 709 |
fun RAFP8 S = |
| 710 |
{spill = spillF S, |
| 711 |
spillSrc = spillFreg' S, |
| 712 |
spillCopyTmp= spillFcopyTmp S, |
| 713 |
reload = reloadF S, |
| 714 |
reloadDst = reloadFreg' S, |
| 715 |
renameSrc = renameF', |
| 716 |
copyInstr = copyInstrF', |
| 717 |
K = KF8, |
| 718 |
getreg = FR8.getreg, |
| 719 |
cellkind = CB.FP, |
| 720 |
dedicated = isDedicatedF8, |
| 721 |
spillProh = [], |
| 722 |
memRegs = Float.fastMemRegs, |
| 723 |
mode = phases(Float.fastPhases) |
| 724 |
} : Ra.raClient |
| 725 |
|
| 726 |
(* Two RA modes, fast and normal *) |
| 727 |
fun fast_fp S = [RAInt S, RAFP8 S] |
| 728 |
fun normal_fp S = [RAInt S, RAFP32 S] |
| 729 |
|
| 730 |
(* The main ra routine *) |
| 731 |
fun run cluster = |
| 732 |
let val printGraph = |
| 733 |
if !x86CfgDebugFlg then |
| 734 |
PrintFlowgraph.printCFG(!MLRiscControl.debug_stream) |
| 735 |
else fn msg => fn _ => () |
| 736 |
|
| 737 |
val S = beforeRA cluster |
| 738 |
val _ = resetRA() |
| 739 |
|
| 740 |
(* generic register allocator *) |
| 741 |
|
| 742 |
val cluster = Ra.ra |
| 743 |
(if !fast_floating_point then fast_fp S else normal_fp S) |
| 744 |
cluster |
| 745 |
|
| 746 |
val _ = removeDeadCode cluster |
| 747 |
|
| 748 |
val _ = printGraph "\t---After register allocation K=8---\n" cluster |
| 749 |
|
| 750 |
(* Run the FP translation phase when fast floating point has |
| 751 |
* been enabled |
| 752 |
*) |
| 753 |
val cluster = |
| 754 |
if !fast_floating_point andalso I.C.numCell CB.FP () > 0 then |
| 755 |
let val cluster = X86FP.run cluster |
| 756 |
in printGraph "\t---After X86 FP translation ---\n" cluster; |
| 757 |
cluster |
| 758 |
end |
| 759 |
else cluster |
| 760 |
in cluster |
| 761 |
end |
| 762 |
|
| 763 |
end |