| 1 |
(* amd64RegAlloc.sml |
| 2 |
* |
| 3 |
* COPYRIGHT (c) 2018 The Fellowship of SML/NJ (http://www.smlnj.org) |
| 4 |
* All rights reserved. |
| 5 |
* |
| 6 |
* Ties together the GP and FP register allocators. |
| 7 |
*) |
| 8 |
|
| 9 |
functor AMD64RegAlloc ( |
| 10 |
structure I : AMD64INSTR |
| 11 |
structure SpillHeur : RA_SPILL_HEURISTICS |
| 12 |
structure Props : AMD64INSN_PROPERTIES (* where I = I *) |
| 13 |
where type I.addressing_mode = I.addressing_mode |
| 14 |
and type I.ea = I.ea |
| 15 |
and type I.instr = I.instr |
| 16 |
and type I.instruction = I.instruction |
| 17 |
and type I.operand = I.operand |
| 18 |
structure CFG : CONTROL_FLOW_GRAPH (* where I = I *) |
| 19 |
where type I.addressing_mode = I.addressing_mode |
| 20 |
and type I.ea = I.ea |
| 21 |
and type I.instr = I.instr |
| 22 |
and type I.instruction = I.instruction |
| 23 |
and type I.operand = I.operand |
| 24 |
structure Spill : RA_SPILL (* where I = I *) |
| 25 |
where type I.addressing_mode = I.addressing_mode |
| 26 |
and type I.ea = I.ea |
| 27 |
and type I.instr = I.instr |
| 28 |
and type I.instruction = I.instruction |
| 29 |
and type I.operand = I.operand |
| 30 |
structure Asm : INSTRUCTION_EMITTER (* where I = I and S.P = CFG.P *) |
| 31 |
where type I.addressing_mode = I.addressing_mode |
| 32 |
and type I.ea = I.ea |
| 33 |
and type I.instr = I.instr |
| 34 |
and type I.instruction = I.instruction |
| 35 |
and type I.operand = I.operand |
| 36 |
where type S.P.Client.pseudo_op = CFG.P.Client.pseudo_op |
| 37 |
and type S.P.T.Basis.cond = CFG.P.T.Basis.cond |
| 38 |
and type S.P.T.Basis.div_rounding_mode = CFG.P.T.Basis.div_rounding_mode |
| 39 |
and type S.P.T.Basis.ext = CFG.P.T.Basis.ext |
| 40 |
and type S.P.T.Basis.fcond = CFG.P.T.Basis.fcond |
| 41 |
and type S.P.T.Basis.rounding_mode = CFG.P.T.Basis.rounding_mode |
| 42 |
and type S.P.T.Constant.const = CFG.P.T.Constant.const |
| 43 |
and type ('s,'r,'f,'c) S.P.T.Extension.ccx = ('s,'r,'f,'c) CFG.P.T.Extension.ccx |
| 44 |
and type ('s,'r,'f,'c) S.P.T.Extension.fx = ('s,'r,'f,'c) CFG.P.T.Extension.fx |
| 45 |
and type ('s,'r,'f,'c) S.P.T.Extension.rx = ('s,'r,'f,'c) CFG.P.T.Extension.rx |
| 46 |
and type ('s,'r,'f,'c) S.P.T.Extension.sx = ('s,'r,'f,'c) CFG.P.T.Extension.sx |
| 47 |
and type S.P.T.I.div_rounding_mode = CFG.P.T.I.div_rounding_mode |
| 48 |
and type S.P.T.Region.region = CFG.P.T.Region.region |
| 49 |
and type S.P.T.ccexp = CFG.P.T.ccexp |
| 50 |
and type S.P.T.fexp = CFG.P.T.fexp |
| 51 |
(* and type S.P.T.labexp = CFG.P.T.labexp *) |
| 52 |
and type S.P.T.mlrisc = CFG.P.T.mlrisc |
| 53 |
and type S.P.T.oper = CFG.P.T.oper |
| 54 |
and type S.P.T.rep = CFG.P.T.rep |
| 55 |
and type S.P.T.rexp = CFG.P.T.rexp |
| 56 |
and type S.P.T.stm = CFG.P.T.stm |
| 57 |
|
| 58 |
type spill_info |
| 59 |
datatype spill_operand_kind = SPILL_LOC |
| 60 |
| CONST_VAL |
| 61 |
|
| 62 |
val beforeRA : CFG.cfg -> spill_info |
| 63 |
|
| 64 |
datatype ra_phase = SPILL_PROPAGATION |
| 65 |
| SPILL_COLORING |
| 66 |
|
| 67 |
structure Int : sig |
| 68 |
val avail : CellsBasis.cell list |
| 69 |
val dedicated : CellsBasis.cell list |
| 70 |
val spillLoc : {info:spill_info, |
| 71 |
an :Annotations.annotations ref, |
| 72 |
cell:CellsBasis.cell, (* spilled cell *) |
| 73 |
id :RAGraph.logical_spill_id |
| 74 |
} -> |
| 75 |
{opnd: I.ea, |
| 76 |
kind: spill_operand_kind |
| 77 |
} |
| 78 |
val spillInit : RAGraph.interferenceGraph -> unit |
| 79 |
val phases : ra_phase list |
| 80 |
end |
| 81 |
|
| 82 |
structure Float : sig |
| 83 |
val avail : CellsBasis.cell list |
| 84 |
val dedicated : CellsBasis.cell list |
| 85 |
val spillLoc : spill_info * Annotations.annotations ref * |
| 86 |
RAGraph.logical_spill_id -> I.ea |
| 87 |
val spillInit : RAGraph.interferenceGraph -> unit |
| 88 |
val phases : ra_phase list |
| 89 |
end |
| 90 |
|
| 91 |
(* guaranteeing that floats are stored at 16-byte aligned addresses reduces the number of instructions *) |
| 92 |
val floats16ByteAligned : bool |
| 93 |
|
| 94 |
) : CFG_OPTIMIZATION = |
| 95 |
struct |
| 96 |
|
| 97 |
datatype ra_phase = datatype ra_phase |
| 98 |
|
| 99 |
fun error msg = MLRiscErrorMsg.error ("AMD64RA", msg) |
| 100 |
fun inc c = c := !c + 1 |
| 101 |
|
| 102 |
val intSpillCnt = |
| 103 |
MLRiscControl.mkCounter ("ra-int-spills", "RA int spill count") |
| 104 |
val intReloadCnt = |
| 105 |
MLRiscControl.mkCounter ("ra-int-reloads", "RA int reload count") |
| 106 |
val intRenameCnt = |
| 107 |
MLRiscControl.mkCounter ("ra-int-renames", "RA int rename count") |
| 108 |
val floatSpillCnt = |
| 109 |
MLRiscControl.mkCounter ("ra-float-spills", "RA float spill count") |
| 110 |
val floatReloadCnt = |
| 111 |
MLRiscControl.mkCounter ("ra-float-reloads", "RA float reload count") |
| 112 |
val floatRenameCnt = |
| 113 |
MLRiscControl.mkCounter ("ra-float-renames", "RA float rename count") |
| 114 |
|
| 115 |
structure CB = CellsBasis |
| 116 |
structure C = I.C |
| 117 |
|
| 118 |
val firstSpill = ref true |
| 119 |
val firstFPSpill = ref true |
| 120 |
|
| 121 |
fun spillInit (graph, CB.GP) = |
| 122 |
if !firstSpill then (* only do this once! *) |
| 123 |
(Int.spillInit graph; |
| 124 |
firstSpill := false |
| 125 |
) |
| 126 |
else () |
| 127 |
| spillInit(graph, CB.FP) = |
| 128 |
if !firstFPSpill then |
| 129 |
(Float.spillInit graph; |
| 130 |
firstFPSpill := false |
| 131 |
) |
| 132 |
else () |
| 133 |
| spillInit _ = error "spillInit" |
| 134 |
|
| 135 |
(* |
| 136 |
* Dead code elimination |
| 137 |
*) |
| 138 |
exception AMD64DeadCode |
| 139 |
val affectedBlocks = |
| 140 |
IntHashTable.mkTable(32, AMD64DeadCode) : bool IntHashTable.hash_table |
| 141 |
val deadRegs = |
| 142 |
IntHashTable.mkTable(32, AMD64DeadCode) : bool IntHashTable.hash_table |
| 143 |
|
| 144 |
fun removeDeadCode(cfg as Graph.GRAPH graph) = let |
| 145 |
val blocks = #nodes graph () |
| 146 |
fun isDead r = Option.isSome (IntHashTable.find deadRegs (CB.cellId r)) |
| 147 |
fun isAffected i = Option.getOpt (IntHashTable.find affectedBlocks i, false) |
| 148 |
fun isDeadInstr(I.ANNOTATION{i, ...}) = isDeadInstr i |
| 149 |
| isDeadInstr(I.INSTR(I.MOVE{dst=I.Direct (_,rd), ...})) = isDead rd |
| 150 |
| isDeadInstr(I.INSTR(I.FMOVE{dst=I.FDirect rd, ...})) = isDead rd |
| 151 |
| isDeadInstr(I.COPY{k=CB.GP, dst=[rd], ...}) = isDead rd |
| 152 |
| isDeadInstr(I.COPY{k=CB.FP, dst=[rd], ...}) = isDead rd |
| 153 |
| isDeadInstr _ = false |
| 154 |
fun scan [] = () |
| 155 |
| scan((blknum, CFG.BLOCK{insns, ...})::rest) = |
| 156 |
(if isAffected blknum then |
| 157 |
((* deadblocks := !deadblocks + 1; *) |
| 158 |
insns := elim(!insns, []) |
| 159 |
) else (); |
| 160 |
scan rest) |
| 161 |
and elim([], code) = List.rev code |
| 162 |
| elim(i::instrs, code) = |
| 163 |
if isDeadInstr i then |
| 164 |
((* deadcode := !deadcode + 1; *) elim(instrs, code)) |
| 165 |
else elim(instrs, i::code) |
| 166 |
in |
| 167 |
if (IntHashTable.numItems affectedBlocks > 0) |
| 168 |
then ( |
| 169 |
scan blocks; |
| 170 |
IntHashTable.clear deadRegs; |
| 171 |
IntHashTable.clear affectedBlocks) |
| 172 |
else () |
| 173 |
end |
| 174 |
|
| 175 |
structure CFG = CFG |
| 176 |
|
| 177 |
(* use the standard register allocator *) |
| 178 |
structure RA = |
| 179 |
RegisterAllocator |
| 180 |
(structure SpillHeuristics = SpillHeur |
| 181 |
structure Flowgraph = MemoryRA |
| 182 |
(RADeadCodeElim |
| 183 |
(structure Flowgraph = ClusterRA |
| 184 |
(structure Flowgraph = CFG |
| 185 |
structure Asm = Asm |
| 186 |
structure InsnProps = Props |
| 187 |
structure Spill = Spill |
| 188 |
) |
| 189 |
fun cellkind CB.GP = true |
| 190 |
| cellkind CB.FP = true |
| 191 |
| cellkind _ = false |
| 192 |
val deadRegs = deadRegs |
| 193 |
val affectedBlocks = affectedBlocks |
| 194 |
val spillInit = spillInit |
| 195 |
) |
| 196 |
) |
| 197 |
) |
| 198 |
structure PrintFlowgraph = |
| 199 |
PrintFlowgraph (structure CFG = CFG |
| 200 |
structure Asm = Asm) |
| 201 |
structure SpillInstr = AMD64SpillInstr ( |
| 202 |
structure I = I |
| 203 |
structure Props = Props |
| 204 |
val floats16ByteAligned = true) |
| 205 |
val spillFInstr = SpillInstr.spill CB.FP |
| 206 |
val reloadFInstr = SpillInstr.reload CB.FP |
| 207 |
val spillInstr = SpillInstr.spill CB.GP |
| 208 |
val reloadInstr = SpillInstr.reload CB.GP |
| 209 |
|
| 210 |
val name = "AMD64RegAlloc" |
| 211 |
val amd64CfgDebugFlg = MLRiscControl.mkFlag ("amd64-cfg-debug", "amd64 CFG debug mode") |
| 212 |
|
| 213 |
val nGPRegs = List.length Int.avail + List.length Int.dedicated |
| 214 |
val nFPRegs = List.length Float.avail + List.length Float.dedicated |
| 215 |
|
| 216 |
structure GPR = GetReg |
| 217 |
(val nRegs = nGPRegs |
| 218 |
val available = List.map CB.registerId Int.avail |
| 219 |
val first = CB.registerId (I.C.GPReg 0)) |
| 220 |
structure FPR = GetReg |
| 221 |
(val nRegs = nFPRegs |
| 222 |
val available = List.map CB.registerId Float.avail |
| 223 |
val first = CB.registerId (I.C.FPReg 0)) |
| 224 |
|
| 225 |
(* |
| 226 |
local |
| 227 |
val dedicatedR = Array.array (nGPRegs, false) |
| 228 |
val dedicatedF = Array.array (nFPRegs, false) |
| 229 |
fun set (dedicated, []) = () |
| 230 |
| set (dedicated, r :: rs) = ( |
| 231 |
Array.update (dedicated, r, true); |
| 232 |
set (dedicated, rs)) |
| 233 |
val _ = set (dedicatedR, List.map CB.registerId Int.dedicated) |
| 234 |
val _ = set (dedicatedF, List.map CB.registerId Float.dedicated) |
| 235 |
fun isDedicated dedicated r = |
| 236 |
r < Array.length dedicated andalso Array.sub (dedicated, r) |
| 237 |
in |
| 238 |
val isDedicatedR = isDedicated dedicatedR |
| 239 |
val isDedicatedF = isDedicated dedicatedF |
| 240 |
end (* local *) |
| 241 |
*) |
| 242 |
local |
| 243 |
val dedicatedR = Vector.fromList (List.map CB.registerId Int.dedicated) |
| 244 |
val dedicatedF = Vector.fromList (List.map CB.registerId Float.dedicated) |
| 245 |
in |
| 246 |
fun isDedicatedR r = Vector.exists (fn q => q = r) dedicatedR |
| 247 |
fun isDedicatedF r = Vector.exists (fn q => q = r) dedicatedF |
| 248 |
end |
| 249 |
|
| 250 |
fun copy {dst, src, tmp} = I.COPY {k=CB.GP, sz=64, dst=dst, src=src, tmp=tmp} |
| 251 |
fun fcopy{dst, src, tmp} = I.COPY{k=CB.FP, sz=64, dst=dst, src=src, tmp=tmp} |
| 252 |
fun annotate ([], i) = i |
| 253 |
| annotate (a :: an, i) = annotate (an, I.ANNOTATION {a=a, i=i}) |
| 254 |
|
| 255 |
fun resetRA() = ( |
| 256 |
firstSpill := true; |
| 257 |
firstFPSpill := true; |
| 258 |
IntHashTable.clear affectedBlocks; |
| 259 |
IntHashTable.clear deadRegs; |
| 260 |
GPR.reset (); |
| 261 |
FPR.reset ()) |
| 262 |
|
| 263 |
fun getRegLoc(s, an, cell, RA.FRAME loc) = |
| 264 |
Int.spillLoc {info=s, an=an, cell=cell, id=loc} |
| 265 |
| getRegLoc(s, an, cell, RA.MEM_REG r) = |
| 266 |
error "memory registers unsupported" |
| 267 |
|
| 268 |
fun spillR s {annotations=an, kill, reg, spillLoc, instr} = let |
| 269 |
fun annotate([], i) = i |
| 270 |
| annotate(a::an, i) = annotate(an, I.ANNOTATION{a=a, i=i}) |
| 271 |
(* preserve annotation on instruction *) |
| 272 |
fun spill(instrAn, I.ANNOTATION{a,i}) = spill(a::instrAn, i) |
| 273 |
| spill(instrAn, I.KILL{regs, spilled}) = |
| 274 |
{code=[annotate (instrAn, |
| 275 |
I.KILL {regs=C.rmvReg (reg, regs), |
| 276 |
spilled=C.addReg (reg, spilled)})], |
| 277 |
proh = [], newReg=NONE} |
| 278 |
| spill(instrAn, I.LIVE _) = error "spill: LIVE" |
| 279 |
| spill(_, I.COPY _) = error "spill: COPY" |
| 280 |
| spill(instrAn, I.INSTR _) = (case getRegLoc (s, an, reg, spillLoc) |
| 281 |
of {opnd=spillLoc, kind=SPILL_LOC} => |
| 282 |
(inc intSpillCnt; |
| 283 |
spillInstr (annotate(instrAn, instr), reg, spillLoc)) |
| 284 |
| _ => (* don't have to spill a constant *) |
| 285 |
{code=[], newReg=NONE, proh=[]} |
| 286 |
(* end case *)) |
| 287 |
in |
| 288 |
spill([], instr) |
| 289 |
end (* spillR *) |
| 290 |
|
| 291 |
fun spillReg s {src, reg, spillLoc, annotations=an} = let |
| 292 |
val _ = inc intSpillCnt |
| 293 |
val {opnd=dstLoc, kind} = getRegLoc (s, an, reg, spillLoc) |
| 294 |
val srcLoc = I.Direct (64, src) |
| 295 |
in |
| 296 |
if kind = CONST_VAL orelse Props.eqOpn (srcLoc, dstLoc) |
| 297 |
then [] |
| 298 |
else [I.move {mvOp=I.MOVQ, src=srcLoc, dst=dstLoc}] |
| 299 |
end (* spillReg *) |
| 300 |
|
| 301 |
fun spillCopyTmp s {copy=I.COPY{k=CB.GP, src, dst,...}, |
| 302 |
reg, spillLoc, annotations=an} = |
| 303 |
(case getRegLoc (s, an, reg, spillLoc) |
| 304 |
of {opnd=tmp, kind=SPILL_LOC} => ( |
| 305 |
inc intSpillCnt; |
| 306 |
copy{dst=dst, src=src, tmp=SOME tmp}) |
| 307 |
| _ => error "spillCopyTmp" |
| 308 |
(* end case *)) |
| 309 |
| spillCopyTmp s {copy=I.ANNOTATION{i, a}, reg, spillLoc, annotations} = |
| 310 |
I.ANNOTATION{i=spillCopyTmp s {copy=i, reg=reg, spillLoc=spillLoc, |
| 311 |
annotations=annotations}, a=a} |
| 312 |
| spillCopyTmp _ _ = error "spillCopyTmp" |
| 313 |
|
| 314 |
fun reloadR s {annotations=an, reg, spillLoc, instr} = let |
| 315 |
fun reload (instrAn, I.ANNOTATION{a,i}) = reload (a::instrAn, i) |
| 316 |
| reload(instrAn, I.LIVE{regs, spilled}) = |
| 317 |
{code=[I.LIVE{regs=C.rmvReg(reg, regs), |
| 318 |
spilled=C.addReg(reg, spilled)}], |
| 319 |
proh=[], newReg=NONE} |
| 320 |
| reload(_, I.KILL _) = error "reload: KILL" |
| 321 |
| reload (_, I.COPY _) = error "reload: COPY" |
| 322 |
| reload(instrAn, instr as I.INSTR _) = ( |
| 323 |
inc intReloadCnt; |
| 324 |
reloadInstr (annotate(instrAn, instr), reg, |
| 325 |
#opnd(getRegLoc(s,an,reg,spillLoc)))) |
| 326 |
in |
| 327 |
reload([], instr) |
| 328 |
end (* reloadR *) |
| 329 |
|
| 330 |
fun reloadReg s {dst, reg, spillLoc, annotations=an} = let |
| 331 |
val _ = inc intReloadCnt |
| 332 |
val srcLoc = #opnd (getRegLoc (s, an, reg, spillLoc)) |
| 333 |
val dstLoc = I.Direct (64, dst) |
| 334 |
in |
| 335 |
if Props.eqOpn(srcLoc,dstLoc) |
| 336 |
then [] |
| 337 |
else [I.move{mvOp=I.MOVQ, src=srcLoc, dst=dstLoc}] |
| 338 |
end (* reloadReg *) |
| 339 |
|
| 340 |
fun renameR {instr, fromSrc, toSrc} = ( |
| 341 |
inc intRenameCnt; |
| 342 |
reloadInstr (instr, fromSrc, I.Direct (64, toSrc))) |
| 343 |
|
| 344 |
fun copyInstrR ((rds as [d], rss as [s]), _) = |
| 345 |
if CB.sameColor(d,s) then [] else [copy {dst=rds, src=rss, tmp=NONE}] |
| 346 |
| copyInstrR((rds, rss), I.COPY{k=CB.GP, tmp, ...}) = |
| 347 |
[copy{dst=rds, src=rss, tmp=tmp}] |
| 348 |
| copyInstrR(x, I.ANNOTATION{i, a}) = |
| 349 |
copyInstrR (x, i) (* XXX *) |
| 350 |
| copyInstrR _ = error "copyInstrR" |
| 351 |
|
| 352 |
fun phases ps = let |
| 353 |
fun f ([], m) = m |
| 354 |
| f (SPILL_PROPAGATION::ps, m) = f (ps, RA.SPILL_PROPAGATION+m) |
| 355 |
| f (SPILL_COLORING::ps, m) = f (ps, RA.SPILL_COLORING+m) |
| 356 |
in |
| 357 |
f (ps, RA.NO_OPTIMIZATION) |
| 358 |
end |
| 359 |
|
| 360 |
fun raInt s = { |
| 361 |
spill = spillR s, |
| 362 |
spillSrc = spillReg s, |
| 363 |
spillCopyTmp= spillCopyTmp s, |
| 364 |
reload = reloadR s, |
| 365 |
reloadDst = reloadReg s, |
| 366 |
renameSrc = renameR, |
| 367 |
copyInstr = copyInstrR, |
| 368 |
K = List.length Int.avail, |
| 369 |
getreg = GPR.getreg, |
| 370 |
cellkind = CB.GP, |
| 371 |
dedicated = isDedicatedR, |
| 372 |
spillProh = [], |
| 373 |
memRegs = [], |
| 374 |
mode = phases Int.phases |
| 375 |
} : RA.raClient |
| 376 |
|
| 377 |
fun getFregLoc (s, an, RA.FRAME loc) = Float.spillLoc (s, an, loc) |
| 378 |
| getFregLoc (s, an, RA.MEM_REG r) = raise Fail "mem regs unsupported" |
| 379 |
|
| 380 |
fun spillF s {annotations=an, kill, reg, spillLoc, instr} = let |
| 381 |
(* preserve annotation on instruction *) |
| 382 |
fun spill(instrAn, I.ANNOTATION{a, i}) = spill(a::instrAn, i) |
| 383 |
| spill(instrAn, I.KILL{regs, spilled}) = |
| 384 |
{code=[annotate (instrAn, |
| 385 |
I.KILL {regs=C.rmvFreg(reg, regs), |
| 386 |
spilled=C.addFreg(reg, spilled)})], |
| 387 |
proh = [], newReg=NONE} |
| 388 |
| spill(instrAn, I.LIVE _) = error "spillF: LIVE" |
| 389 |
| spill(_, I.COPY _) = error "spillF: COPY" |
| 390 |
| spill(instrAn, I.INSTR _) = ( |
| 391 |
inc floatSpillCnt; |
| 392 |
spillFInstr (instr, reg, getFregLoc (s, an, spillLoc))) |
| 393 |
in |
| 394 |
spill([], instr) |
| 395 |
end (* spillF *) |
| 396 |
|
| 397 |
fun spillFreg s {src, reg, spillLoc, annotations=an} = ( |
| 398 |
inc floatSpillCnt; |
| 399 |
let val dst = getFregLoc (s, an, spillLoc) |
| 400 |
in |
| 401 |
if Props.eqOpn (I.Direct (64, src), dst) |
| 402 |
then [] |
| 403 |
else [I.fmove {fmvOp=I.MOVSD, src=I.FDirect src, dst=dst}] |
| 404 |
end) |
| 405 |
|
| 406 |
fun spillFcopyTmp s {copy=I.COPY{k=CB.FP, dst, src, ...}, spillLoc, reg, |
| 407 |
annotations=an} = ( |
| 408 |
inc floatSpillCnt; |
| 409 |
fcopy {dst=dst, src=src, tmp=SOME (getFregLoc (s, an, spillLoc))}) |
| 410 |
| spillFcopyTmp s {copy=I.ANNOTATION{i,a}, spillLoc, reg, annotations} = |
| 411 |
let val i = spillFcopyTmp s {copy=i, spillLoc=spillLoc, reg=reg, |
| 412 |
annotations=annotations} |
| 413 |
in I.ANNOTATION{i=i, a=a} end |
| 414 |
| spillFcopyTmp _ _ = error "spillFcopyTmp" |
| 415 |
|
| 416 |
fun reloadF s {annotations=an,reg,spillLoc,instr} = let |
| 417 |
fun reload (instrAn, I.ANNOTATION{a,i}) = reload(a::instrAn, i) |
| 418 |
| reload(instrAn, I.LIVE{regs, spilled}) = |
| 419 |
{code=[I.LIVE{regs=C.rmvFreg(reg, regs), |
| 420 |
spilled=C.addFreg(reg, spilled)}], |
| 421 |
proh=[], newReg=NONE} |
| 422 |
| reload(_, I.KILL _) = error "reloadF: KILL" |
| 423 |
| reload (_, I.COPY _) = error "reloadF: COPY" |
| 424 |
| reload(instrAn, instr as I.INSTR _) = ( |
| 425 |
inc floatReloadCnt; |
| 426 |
reloadFInstr (instr, reg, getFregLoc(s, an, spillLoc))) |
| 427 |
in |
| 428 |
reload([], instr) |
| 429 |
end (* reloadF *) |
| 430 |
|
| 431 |
fun reloadFreg s {dst, reg, spillLoc, annotations=an} = |
| 432 |
(inc floatReloadCnt; |
| 433 |
let val srcLoc = getFregLoc (s, an, spillLoc) |
| 434 |
val dstLoc = I.FDirect dst |
| 435 |
in |
| 436 |
if Props.eqOpn (srcLoc, dstLoc) |
| 437 |
then [] |
| 438 |
else [I.fmove {fmvOp=I.MOVSD, src=srcLoc, dst=dstLoc}] |
| 439 |
end) |
| 440 |
|
| 441 |
fun copyInstrF((rds as [_], rss as [_]), _) = |
| 442 |
fcopy{dst=rds, src=rss, tmp=NONE} |
| 443 |
| copyInstrF((rds, rss), I.COPY{k=CB.FP, tmp, ...}) = |
| 444 |
fcopy{dst=rds, src=rss, tmp=tmp} |
| 445 |
| copyInstrF(x, I.ANNOTATION{i,a}) = |
| 446 |
I.ANNOTATION{i=copyInstrF (x, i), a=a} |
| 447 |
| copyInstrF _ = error "copyInstrF" |
| 448 |
|
| 449 |
val copyInstrF = fn x => [copyInstrF x] |
| 450 |
|
| 451 |
fun renameF {instr, fromSrc, toSrc} = ( |
| 452 |
inc floatRenameCnt; |
| 453 |
reloadFInstr (instr, fromSrc, I.FDirect toSrc)) |
| 454 |
|
| 455 |
fun raFloat s = { |
| 456 |
spill = spillF s, |
| 457 |
spillSrc = spillFreg s, |
| 458 |
spillCopyTmp= spillFcopyTmp s, |
| 459 |
reload = reloadF s, |
| 460 |
reloadDst = reloadFreg s, |
| 461 |
renameSrc = renameF, |
| 462 |
copyInstr = copyInstrF, |
| 463 |
K = List.length Float.avail, |
| 464 |
getreg = FPR.getreg, |
| 465 |
cellkind = CB.FP, |
| 466 |
dedicated = isDedicatedF, |
| 467 |
spillProh = [], |
| 468 |
memRegs = [], |
| 469 |
mode = phases Float.phases |
| 470 |
} : RA.raClient |
| 471 |
|
| 472 |
fun run cfg = let |
| 473 |
val printCFG = if !amd64CfgDebugFlg |
| 474 |
then PrintFlowgraph.printCFG (!MLRiscControl.debug_stream) |
| 475 |
else fn msg => fn _ => () |
| 476 |
val _ = printCFG "\t---Before register allocation---\n" cfg; |
| 477 |
val s = beforeRA cfg |
| 478 |
val _ = resetRA () |
| 479 |
val cfg' = RA.ra [raInt s, raFloat s] cfg |
| 480 |
in |
| 481 |
removeDeadCode cfg'; |
| 482 |
printCFG "\t---After register allocation---\n" cfg'; |
| 483 |
cfg' |
| 484 |
end (* run *) |
| 485 |
|
| 486 |
end (* AMD64RegAlloc *) |