| 1 |
(* x86-fp.sml |
| 2 |
* |
| 3 |
* COPYRIGHT (c) 2001 Bell Labs, Lucent Technologies |
| 4 |
* |
| 5 |
* This phase takes a cluster with pseudo x86 fp instructions, performs |
| 6 |
* liveness analysis to determine their live ranges, and rewrite the |
| 7 |
* program into the correct stack based code. |
| 8 |
* |
| 9 |
* The Basics |
| 10 |
* ---------- |
| 11 |
* o We assume there are 7 pseudo fp registers, %fp(0), ..., %fp(6), |
| 12 |
* which are mapped onto the %st stack. One stack location is reserved |
| 13 |
* for holding temporaries. |
| 14 |
* o Important: for floating point comparisons, we actually need |
| 15 |
* two extra stack locations in the worst case. We handle this by |
| 16 |
* specifying that the instruction define an extra temporary fp register |
| 17 |
* when necessary. |
| 18 |
* o The mapping between %fp <-> %st may change from program point to |
| 19 |
* program point. We keep track of this lazy renaming and try to minimize |
| 20 |
* the number of FXCH that we insert. |
| 21 |
* o At split and merge points, we may get inconsistent %fp <-> %st mappings. |
| 22 |
* We handle this by inserting the appropriate renaming code. |
| 23 |
* o Parallel copies (renaming) are rewritten into a sequence of FXCHs! |
| 24 |
* |
| 25 |
* Pseudo fp instructions Semantics |
| 26 |
* -------------------------------------- |
| 27 |
* FMOVE src, dst dst := src |
| 28 |
* FILOAD ea, dst dst := cvti2f(mem[ea]) |
| 29 |
* FBINOP lsrc, rsrc, dst dst := lsrc * rsrc |
| 30 |
* FIBINOP lsrc, rsrc, dst dst := lsrc * cvti2f(rsrc) |
| 31 |
* FUNOP src, dst dst := unaryOp src |
| 32 |
* FCMP lsrc, rsrc fp condition code := fcmp(lsrc, rsrc) |
| 33 |
* |
| 34 |
* An instruction may use its source operand(s) destructively. |
| 35 |
* We find this info using a global liveness analysis. |
| 36 |
* |
| 37 |
* The Translation |
| 38 |
* --------------- |
| 39 |
* o We keep track of the bindings between %fp registers and the |
| 40 |
* %st(..) staack locations. |
| 41 |
* o FXCH and FLDL are inserted at the appropriate places to move operands |
| 42 |
* to %st(0). FLDL is used if the operand is not dead. FXCH is used |
| 43 |
* if the operand is the last use. |
| 44 |
* o FCOPY's between pseudo %fp registers are done by software renaming |
| 45 |
* and generate no code by itself! |
| 46 |
* o FSTL %st(1) are also generated to pop the stack after the last use |
| 47 |
* of an operand. |
| 48 |
* |
| 49 |
* Note |
| 50 |
* ---- |
| 51 |
* 1. This module should be run after floating point register allocation. |
| 52 |
* |
| 53 |
* -- Allen Leung ([email protected]) |
| 54 |
*) |
| 55 |
|
| 56 |
functor X86FP |
| 57 |
(structure X86Instr : X86INSTR |
| 58 |
structure X86Props : INSN_PROPERTIES (* where I = X86Instr *) |
| 59 |
where type I.addressing_mode = X86Instr.addressing_mode |
| 60 |
and type I.ea = X86Instr.ea |
| 61 |
and type I.instr = X86Instr.instr |
| 62 |
and type I.instruction = X86Instr.instruction |
| 63 |
and type I.operand = X86Instr.operand |
| 64 |
structure Flowgraph : CONTROL_FLOW_GRAPH (* where I = X86Instr *) |
| 65 |
where type I.addressing_mode = X86Instr.addressing_mode |
| 66 |
and type I.ea = X86Instr.ea |
| 67 |
and type I.instr = X86Instr.instr |
| 68 |
and type I.instruction = X86Instr.instruction |
| 69 |
and type I.operand = X86Instr.operand |
| 70 |
structure Liveness : LIVENESS (* where CFG = Flowgraph *) |
| 71 |
where type CFG.I.addressing_mode = Flowgraph.I.addressing_mode |
| 72 |
and type CFG.I.ea = Flowgraph.I.ea |
| 73 |
and type CFG.I.instr = Flowgraph.I.instr |
| 74 |
and type CFG.I.instruction = Flowgraph.I.instruction |
| 75 |
and type CFG.I.operand = Flowgraph.I.operand |
| 76 |
and type CFG.P.Client.pseudo_op = Flowgraph.P.Client.pseudo_op |
| 77 |
and type CFG.P.T.Basis.cond = Flowgraph.P.T.Basis.cond |
| 78 |
and type CFG.P.T.Basis.div_rounding_mode = Flowgraph.P.T.Basis.div_rounding_mode |
| 79 |
and type CFG.P.T.Basis.ext = Flowgraph.P.T.Basis.ext |
| 80 |
and type CFG.P.T.Basis.fcond = Flowgraph.P.T.Basis.fcond |
| 81 |
and type CFG.P.T.Basis.rounding_mode = Flowgraph.P.T.Basis.rounding_mode |
| 82 |
and type CFG.P.T.Constant.const = Flowgraph.P.T.Constant.const |
| 83 |
and type ('s,'r,'f,'c) CFG.P.T.Extension.ccx = ('s,'r,'f,'c) Flowgraph.P.T.Extension.ccx |
| 84 |
and type ('s,'r,'f,'c) CFG.P.T.Extension.fx = ('s,'r,'f,'c) Flowgraph.P.T.Extension.fx |
| 85 |
and type ('s,'r,'f,'c) CFG.P.T.Extension.rx = ('s,'r,'f,'c) Flowgraph.P.T.Extension.rx |
| 86 |
and type ('s,'r,'f,'c) CFG.P.T.Extension.sx = ('s,'r,'f,'c) Flowgraph.P.T.Extension.sx |
| 87 |
and type CFG.P.T.I.div_rounding_mode = Flowgraph.P.T.I.div_rounding_mode |
| 88 |
and type CFG.P.T.Region.region = Flowgraph.P.T.Region.region |
| 89 |
and type CFG.P.T.ccexp = Flowgraph.P.T.ccexp |
| 90 |
and type CFG.P.T.fexp = Flowgraph.P.T.fexp |
| 91 |
(* and type CFG.P.T.labexp = Flowgraph.P.T.labexp *) |
| 92 |
and type CFG.P.T.mlrisc = Flowgraph.P.T.mlrisc |
| 93 |
and type CFG.P.T.oper = Flowgraph.P.T.oper |
| 94 |
and type CFG.P.T.rep = Flowgraph.P.T.rep |
| 95 |
and type CFG.P.T.rexp = Flowgraph.P.T.rexp |
| 96 |
and type CFG.P.T.stm = Flowgraph.P.T.stm |
| 97 |
and type CFG.block = Flowgraph.block |
| 98 |
and type CFG.block_kind = Flowgraph.block_kind |
| 99 |
and type CFG.edge_info = Flowgraph.edge_info |
| 100 |
and type CFG.edge_kind = Flowgraph.edge_kind |
| 101 |
and type CFG.info = Flowgraph.info |
| 102 |
structure Asm : INSTRUCTION_EMITTER (* where I = X86Instr and S.P = Flowgraph.P *) |
| 103 |
where type I.addressing_mode = X86Instr.addressing_mode |
| 104 |
and type I.ea = X86Instr.ea |
| 105 |
and type I.instr = X86Instr.instr |
| 106 |
and type I.instruction = X86Instr.instruction |
| 107 |
and type I.operand = X86Instr.operand |
| 108 |
where type S.P.Client.pseudo_op = Flowgraph.P.Client.pseudo_op |
| 109 |
and type S.P.T.Basis.cond = Flowgraph.P.T.Basis.cond |
| 110 |
and type S.P.T.Basis.div_rounding_mode = Flowgraph.P.T.Basis.div_rounding_mode |
| 111 |
and type S.P.T.Basis.ext = Flowgraph.P.T.Basis.ext |
| 112 |
and type S.P.T.Basis.fcond = Flowgraph.P.T.Basis.fcond |
| 113 |
and type S.P.T.Basis.rounding_mode = Flowgraph.P.T.Basis.rounding_mode |
| 114 |
and type S.P.T.Constant.const = Flowgraph.P.T.Constant.const |
| 115 |
and type ('s,'r,'f,'c) S.P.T.Extension.ccx = ('s,'r,'f,'c) Flowgraph.P.T.Extension.ccx |
| 116 |
and type ('s,'r,'f,'c) S.P.T.Extension.fx = ('s,'r,'f,'c) Flowgraph.P.T.Extension.fx |
| 117 |
and type ('s,'r,'f,'c) S.P.T.Extension.rx = ('s,'r,'f,'c) Flowgraph.P.T.Extension.rx |
| 118 |
and type ('s,'r,'f,'c) S.P.T.Extension.sx = ('s,'r,'f,'c) Flowgraph.P.T.Extension.sx |
| 119 |
and type S.P.T.I.div_rounding_mode = Flowgraph.P.T.I.div_rounding_mode |
| 120 |
and type S.P.T.Region.region = Flowgraph.P.T.Region.region |
| 121 |
and type S.P.T.ccexp = Flowgraph.P.T.ccexp |
| 122 |
and type S.P.T.fexp = Flowgraph.P.T.fexp |
| 123 |
(* and type S.P.T.labexp = Flowgraph.P.T.labexp *) |
| 124 |
and type S.P.T.mlrisc = Flowgraph.P.T.mlrisc |
| 125 |
and type S.P.T.oper = Flowgraph.P.T.oper |
| 126 |
and type S.P.T.rep = Flowgraph.P.T.rep |
| 127 |
and type S.P.T.rexp = Flowgraph.P.T.rexp |
| 128 |
and type S.P.T.stm = Flowgraph.P.T.stm |
| 129 |
) : CFG_OPTIMIZATION = |
| 130 |
struct |
| 131 |
val debug = false (* set this to true to debug this module |
| 132 |
* set this to false for production use. |
| 133 |
*) |
| 134 |
val debugLiveness = true (* debug liveness analysis *) |
| 135 |
val debugDead = false (* debug dead code removal *) |
| 136 |
val sanityCheck = true |
| 137 |
|
| 138 |
structure CFG = Flowgraph |
| 139 |
structure G = Graph |
| 140 |
structure I = X86Instr |
| 141 |
structure T = I.T |
| 142 |
structure P = X86Props |
| 143 |
structure C = I.C |
| 144 |
structure A = Array |
| 145 |
structure L = Label |
| 146 |
structure An = Annotations |
| 147 |
structure CB = CellsBasis |
| 148 |
structure SL = CB.SortedCells |
| 149 |
structure HT = IntHashTable |
| 150 |
structure IM = IntRedBlackMap |
| 151 |
|
| 152 |
type flowgraph = CFG.cfg |
| 153 |
type an = An.annotations |
| 154 |
|
| 155 |
val name = "X86 floating point rewrite" |
| 156 |
|
| 157 |
val debugOn = MLRiscControl.mkFlag ("x86-fp-debug", "x86 fp debug mode") |
| 158 |
val traceOn = MLRiscControl.mkFlag ("x86-fp-trace", "x86 fp trace mode") |
| 159 |
|
| 160 |
fun error msg = MLRiscErrorMsg.error("X86FP",msg) |
| 161 |
fun pr msg = TextIO.output(!MLRiscControl.debug_stream,msg) |
| 162 |
|
| 163 |
val i2s = Int.toString |
| 164 |
|
| 165 |
(* |
| 166 |
* No overflow checking is needed for integer arithmetic in this module |
| 167 |
*) |
| 168 |
|
| 169 |
fun celllistToCellset l = List.foldr CB.CellSet.add CB.CellSet.empty l |
| 170 |
fun celllistToString l = CB.CellSet.toString(celllistToCellset l) |
| 171 |
|
| 172 |
(* Annotation to mark split edges *) |
| 173 |
exception TargetMovedTo of G.node_id |
| 174 |
|
| 175 |
(*----------------------------------------------------------------------- |
| 176 |
* Primitive instruction handling routines |
| 177 |
*-----------------------------------------------------------------------*) |
| 178 |
|
| 179 |
(* Annotation an instruction *) |
| 180 |
fun mark(instr, []) = instr |
| 181 |
| mark(instr, a::an) = mark(I.ANNOTATION{i=instr,a=a}, an) |
| 182 |
|
| 183 |
(* Add pop suffix to a binary operator *) |
| 184 |
fun pop I.FADDL = I.FADDP | pop I.FADDS = I.FADDP |
| 185 |
| pop I.FSUBL = I.FSUBP | pop I.FSUBS = I.FSUBP |
| 186 |
| pop I.FSUBRL = I.FSUBRP | pop I.FSUBRS = I.FSUBRP |
| 187 |
| pop I.FMULL = I.FMULP | pop I.FMULS = I.FMULP |
| 188 |
| pop I.FDIVL = I.FDIVP | pop I.FDIVS = I.FDIVP |
| 189 |
| pop I.FDIVRL = I.FDIVRP | pop I.FDIVRS = I.FDIVRP |
| 190 |
| pop _ = error "fbinop.pop" |
| 191 |
|
| 192 |
(* Invert the operator *) |
| 193 |
fun invert I.FADDL = I.FADDL | invert I.FADDS = I.FADDS |
| 194 |
| invert I.FSUBL = I.FSUBRL | invert I.FSUBS = I.FSUBRS |
| 195 |
| invert I.FSUBRL = I.FSUBL | invert I.FSUBRS = I.FSUBS |
| 196 |
| invert I.FMULL = I.FMULL | invert I.FMULS = I.FMULS |
| 197 |
| invert I.FDIVL = I.FDIVRL | invert I.FDIVS = I.FDIVRS |
| 198 |
| invert I.FDIVRL = I.FDIVL | invert I.FDIVRS = I.FDIVS |
| 199 |
| invert I.FADDP = I.FADDP | invert I.FMULP = I.FMULP |
| 200 |
| invert I.FSUBP = I.FSUBRP | invert I.FSUBRP = I.FSUBP |
| 201 |
| invert I.FDIVP = I.FDIVRP | invert I.FDIVRP = I.FDIVP |
| 202 |
| invert _ = error "invert" |
| 203 |
|
| 204 |
(* Pseudo instructions *) |
| 205 |
fun FLD(I.FP32, ea) = I.flds ea |
| 206 |
| FLD(I.FP64, ea) = I.fldl ea |
| 207 |
| FLD(I.FP80, ea) = I.fldt ea |
| 208 |
|
| 209 |
fun FILD(I.I8, ea) = error "FILD" |
| 210 |
| FILD(I.I16, ea) = I.fild ea |
| 211 |
| FILD(I.I32, ea) = I.fildl ea |
| 212 |
| FILD(I.I64, ea) = I.fildll ea |
| 213 |
|
| 214 |
fun FSTP(I.FP32, ea) = I.fstps ea |
| 215 |
| FSTP(I.FP64, ea) = I.fstpl ea |
| 216 |
| FSTP(I.FP80, ea) = I.fstpt ea |
| 217 |
|
| 218 |
fun FST(I.FP32, ea) = I.fsts ea |
| 219 |
| FST(I.FP64, ea) = I.fstl ea |
| 220 |
| FST(I.FP80, ea) = error "FSTT" |
| 221 |
|
| 222 |
(*----------------------------------------------------------------------- |
| 223 |
* Pretty print routines |
| 224 |
*-----------------------------------------------------------------------*) |
| 225 |
fun fregToString f = "%f"^i2s(CB.registerNum f) |
| 226 |
fun fregsToString s = |
| 227 |
List.foldr (fn (r,"") => fregToString r | |
| 228 |
(r,s) => fregToString r^" "^s) "" s |
| 229 |
|
| 230 |
fun blknumOf(CFG.BLOCK{id, ...}) = id |
| 231 |
|
| 232 |
(*----------------------------------------------------------------------- |
| 233 |
* A stack datatype that mimics the x86 floating point stack |
| 234 |
* and keeps track of bindings between %st(n) and %fp(n). |
| 235 |
*-----------------------------------------------------------------------*) |
| 236 |
structure ST :> |
| 237 |
sig |
| 238 |
type stack |
| 239 |
type stnum = int (* 0 -- 7 *) |
| 240 |
val create : unit -> stack |
| 241 |
val stack0 : stack |
| 242 |
val copy : stack -> stack |
| 243 |
val clear : stack -> unit |
| 244 |
val fp : stack * CB.register_id -> stnum |
| 245 |
val st : stack * stnum -> CB.register_id |
| 246 |
val set : stack * stnum * CB.register_id -> unit |
| 247 |
val push : stack * CB.register_id -> unit |
| 248 |
val xch : stack * stnum * stnum -> unit |
| 249 |
val pop : stack -> unit |
| 250 |
val depth : stack -> int |
| 251 |
val nonFull : stack -> unit |
| 252 |
val kill : stack * CellsBasis.cell -> unit |
| 253 |
val stackToString : stack -> string |
| 254 |
val equal : stack * stack -> bool |
| 255 |
end = |
| 256 |
struct |
| 257 |
type stnum = int |
| 258 |
datatype stack = |
| 259 |
STACK of |
| 260 |
{ st : CB.register_id A.array, (* mapping %st -> %fp registers *) |
| 261 |
fp : stnum A.array, (* mapping %fp -> %st registers *) |
| 262 |
sp : int ref (* stack pointer *) |
| 263 |
} |
| 264 |
|
| 265 |
(* Create a new stack *) |
| 266 |
fun create() = STACK{st=A.array(8,~1), fp=A.array(7,16), sp=ref ~1} |
| 267 |
|
| 268 |
val stack0 = create() |
| 269 |
|
| 270 |
(* Copy a stack *) |
| 271 |
fun copy(STACK{st, fp, sp}) = |
| 272 |
let val st' = A.array(8, ~1) |
| 273 |
val fp' = A.array(7, 16) |
| 274 |
in A.copy{src=st,dst=st',di=0}; |
| 275 |
A.copy{src=fp,dst=fp',di=0}; |
| 276 |
STACK{st=st', fp=fp', sp=ref(!sp)} |
| 277 |
end |
| 278 |
|
| 279 |
(* Depth of stack *) |
| 280 |
fun depth(STACK{sp, ...}) = !sp + 1 |
| 281 |
|
| 282 |
fun nonFull(STACK{sp, ...}) = |
| 283 |
if !sp >= 7 then error "stack overflow" else () |
| 284 |
|
| 285 |
(* Given %st(n), lookup the corresponding %fp(n) *) |
| 286 |
fun st(STACK{st, sp, ...}, n) = A.sub(st, !sp - n) |
| 287 |
|
| 288 |
(* Given %fp(n), lookup the corresponding %st(n) *) |
| 289 |
fun fp(STACK{fp, sp, ...}, n) = !sp - A.sub(fp, n) |
| 290 |
|
| 291 |
fun stackToString stack = |
| 292 |
let val depth = depth stack |
| 293 |
fun f i = if i >= depth then " ]" |
| 294 |
else "%st("^i2s i^")=%f"^i2s(st(stack,i))^" "^f(i+1) |
| 295 |
in "[ "^f 0 end |
| 296 |
|
| 297 |
fun clear(STACK{st, fp, sp, ...}) = |
| 298 |
(sp := ~1; A.modify(fn _ => ~1) st; A.modify(fn _ => 16) fp) |
| 299 |
|
| 300 |
(* Set %st(n) := %f *) |
| 301 |
fun set(STACK{st, fp, sp, ...}, n, f) = |
| 302 |
(A.update(st, !sp - n, f); |
| 303 |
if f >= 0 then A.update(fp, f, !sp - n) else () |
| 304 |
) |
| 305 |
|
| 306 |
(* Pop one entry *) |
| 307 |
fun pop(STACK{sp, st, fp, ...}) = sp := !sp - 1 |
| 308 |
|
| 309 |
(* Push %fp(f) onto %st(0) *) |
| 310 |
fun push(stack as STACK{sp, ...}, f) = (sp := !sp + 1; set(stack, 0, f)) |
| 311 |
|
| 312 |
(* Exchange the contents of %st(m) and %st(n) *) |
| 313 |
fun xch(stack, m, n) = |
| 314 |
let val f_m = st(stack, m) |
| 315 |
val f_n = st(stack, n) |
| 316 |
in set(stack, m, f_n); |
| 317 |
set(stack, n, f_m) |
| 318 |
end |
| 319 |
|
| 320 |
fun kill(STACK{fp, ...}, f) = A.update(fp, CB.registerNum f, 16) |
| 321 |
|
| 322 |
fun equal(st1, st2) = |
| 323 |
let val m = depth st1 |
| 324 |
val n = depth st2 |
| 325 |
fun loop i = |
| 326 |
i >= m orelse (st(st1, i) = st(st2, i) andalso loop(i+1)) |
| 327 |
in m = n andalso loop(0) |
| 328 |
end |
| 329 |
|
| 330 |
end (* struct *) |
| 331 |
|
| 332 |
(*----------------------------------------------------------------------- |
| 333 |
* Module to handle forward propagation. |
| 334 |
* Forward propagation does the following: |
| 335 |
* Given an instruction |
| 336 |
* fmove mem, %fp(n) |
| 337 |
* We delay the generation of the load until the first use of %fp(n), |
| 338 |
* which we can further optimize by folding the load into the operand |
| 339 |
* of the instruction, if it is the last use of this operand. |
| 340 |
* If %fp(n) is dead then no load is necessary. |
| 341 |
* Of course, we have to be careful whenever we encounter other |
| 342 |
* instruction with a write. |
| 343 |
*-----------------------------------------------------------------------*) |
| 344 |
(* |
| 345 |
structure ForwardPropagation :> |
| 346 |
sig |
| 347 |
type readbuffer |
| 348 |
val create : ST.stack -> readbuffer |
| 349 |
val load : readbuffer * C.cell * I.fsize * I.ea -> unit |
| 350 |
val getreg : readbuffer * bool * C.cell * I.instruction list -> |
| 351 |
I.operand * I.instruction list |
| 352 |
val flush : readbuffer * I.instruction list -> I.instruction list |
| 353 |
end = |
| 354 |
struct |
| 355 |
|
| 356 |
datatype readbuffer = |
| 357 |
READ of { stack : ST.stack, |
| 358 |
loads : (I.fsize * I.ea) option A.array, |
| 359 |
pending : int ref |
| 360 |
} |
| 361 |
|
| 362 |
fun create stack = |
| 363 |
READ{stack =stack, |
| 364 |
loads =A.array(8, NONE), |
| 365 |
pending =ref 0 |
| 366 |
} |
| 367 |
|
| 368 |
fun load(READ{pending, loads, ...}, fd, fsize, mem) = |
| 369 |
(A.update(loads, fd, SOME(fsize, mem)); |
| 370 |
pending := !pending + 1 |
| 371 |
) |
| 372 |
|
| 373 |
(* Extract the operand for a register |
| 374 |
* If it has a delayed load associated with it then |
| 375 |
* we perform the load at this time. |
| 376 |
*) |
| 377 |
fun getreg(READ{pending, loads, stack, ...}, isLastUse, fs, code) = |
| 378 |
case A.sub(loads, fs) of |
| 379 |
NONE => |
| 380 |
let val n = ST.st(stack, fs) |
| 381 |
in if isLastUse |
| 382 |
then (ST n, code) |
| 383 |
else let val code = I.FLDL(ST n)::code |
| 384 |
in ST.push(stack, fs); (ST0, code) |
| 385 |
end |
| 386 |
end |
| 387 |
| SOME(fsize, mem) => |
| 388 |
let val code = FLD(fsize, mem)::code |
| 389 |
in A.update(loads, fs, NONE); (* delete load *) |
| 390 |
pending := !pending - 1; |
| 391 |
ST.push(stack, fs); (* fs is now in place *) |
| 392 |
(ST0, code) |
| 393 |
end |
| 394 |
|
| 395 |
(* Extract a binary operand. |
| 396 |
* We'll try to fold this into the operand |
| 397 |
*) |
| 398 |
fun getopnd(READ{pending, loads, stack,...}, isLastUse, I.FPR fs, code) = |
| 399 |
(case A.sub(loads, fs) of |
| 400 |
NONE => |
| 401 |
let val n = ST.st(stack, fs) |
| 402 |
in if isLastUse fs (* regmap XXX *) |
| 403 |
then (ST n, code) |
| 404 |
else let val code = I.FLDL(ST n)::code |
| 405 |
in ST.push(stack, fs); (ST0, code) |
| 406 |
end |
| 407 |
end |
| 408 |
| SOME(fsize, mem) => |
| 409 |
(A.update(loads, fs, NONE); (* delete load *) |
| 410 |
pending := !pending - 1; |
| 411 |
if isLastUse fs then (mem, code) |
| 412 |
else let val code = FLD(fsize, mem)::code |
| 413 |
in ST.push(stack, fs); |
| 414 |
(ST0, code) |
| 415 |
end |
| 416 |
) |
| 417 |
) |
| 418 |
| getopnd(_, _, ea, code) = (ea, code) |
| 419 |
|
| 420 |
fun flush(READ{pending=ref 0,...}, code) = code |
| 421 |
|
| 422 |
end (* struct *) |
| 423 |
*) |
| 424 |
|
| 425 |
(*----------------------------------------------------------------------- |
| 426 |
* Module to handle delayed stores. |
| 427 |
* Delayed store does the following: |
| 428 |
* Given an instruction |
| 429 |
* fstore %fp(n), %mem |
| 430 |
* We delay the generation of the store until necessary. |
| 431 |
* This gives us an opportunity to rearrange the order of the stores |
| 432 |
* to eliminate unnecessary fxch. |
| 433 |
*-----------------------------------------------------------------------*) |
| 434 |
(* |
| 435 |
structure DelayStore :> |
| 436 |
sig |
| 437 |
type writebuffer |
| 438 |
val create : ST.stack -> writebuffer |
| 439 |
val flush : writebuffer * I.instruction list -> I.instruction list |
| 440 |
end = |
| 441 |
struct |
| 442 |
datatype writebuffer = |
| 443 |
WRITE of { front : (I.ea * C.cell) list ref, |
| 444 |
back : (I.ea * C.cell) list ref, |
| 445 |
stack : ST.stack, |
| 446 |
pending : int ref |
| 447 |
} |
| 448 |
fun create stack = WRITE{front=ref [], back=ref [], |
| 449 |
stack=stack, pending=ref 0} |
| 450 |
fun flush(WRITE{pending=ref 0,...}, code) = code |
| 451 |
end (* struct *) |
| 452 |
*) |
| 453 |
|
| 454 |
(*----------------------------------------------------------------------- |
| 455 |
* Main routine. |
| 456 |
* |
| 457 |
* Algorithm: |
| 458 |
* 1. Perform liveness analysis. |
| 459 |
* 2. For each fp register, mark all its last use point(s). |
| 460 |
* Registers are popped at their last uses. |
| 461 |
* 3. Rewrite the instructions basic block by basic block. |
| 462 |
* 4. Insert shuffle code at basic block boundaries. |
| 463 |
* When necessary, split critical edges. |
| 464 |
* 5. Sacrifice a goat to make sure things don't go wrong. |
| 465 |
*-----------------------------------------------------------------------*) |
| 466 |
fun run(Cfg as G.GRAPH cfg) = |
| 467 |
let |
| 468 |
val numberOfBlks = #capacity cfg () |
| 469 |
val ENTRY = List.hd (#entries cfg ()) |
| 470 |
val EXIT = List.hd (#exits cfg ()) |
| 471 |
|
| 472 |
val getCell = C.getCellsByKind CB.FP |
| 473 |
(*extract the fp component of cellset*) |
| 474 |
|
| 475 |
val stTable = A.tabulate(8, fn n => I.ST(C.ST n)) |
| 476 |
|
| 477 |
fun ST n = (if sanityCheck andalso (n < 0 orelse n >= 8) then |
| 478 |
pr("WARNING BAD %st("^i2s n^")\n") |
| 479 |
else (); |
| 480 |
A.sub(stTable, n) |
| 481 |
) |
| 482 |
|
| 483 |
fun FXCH n = I.fxch{opnd=C.ST n} |
| 484 |
|
| 485 |
val ST0 = ST 0 |
| 486 |
val ST1 = ST 1 |
| 487 |
val POP_ST = I.fstpl ST0 (* Instruction to pop an entry *) |
| 488 |
|
| 489 |
(* Dump instructions *) |
| 490 |
fun dump instrs = |
| 491 |
let val Asm.S.STREAM{emit, ...} = |
| 492 |
AsmStream.withStream (!MLRiscControl.debug_stream) |
| 493 |
Asm.makeStream [] |
| 494 |
in app emit (rev instrs) |
| 495 |
end |
| 496 |
|
| 497 |
(* Create assembly of instruction *) |
| 498 |
fun assemble instr = |
| 499 |
let val buf = StringOutStream.mkStreamBuf() |
| 500 |
val stream = StringOutStream.openStringOut buf |
| 501 |
val Asm.S.STREAM{emit, ...} = |
| 502 |
AsmStream.withStream stream Asm.makeStream [] |
| 503 |
val _ = emit instr |
| 504 |
val s = StringOutStream.getString buf |
| 505 |
val n = String.size s |
| 506 |
in if n = 0 then s else String.substring(s, 0, n - 1) |
| 507 |
end |
| 508 |
|
| 509 |
(*------------------------------------------------------------------ |
| 510 |
* Perform liveness analysis on the floating point variables |
| 511 |
* P.S. I'm glad I didn't throw away the code liveness code. |
| 512 |
*------------------------------------------------------------------*) |
| 513 |
val defUse = P.defUse CB.FP (* def/use properties *) |
| 514 |
val {liveIn=liveInTable, liveOut=liveOutTable} = Liveness.liveness { |
| 515 |
defUse=defUse, |
| 516 |
(* updateCell=C.updateCellsByKind CB.FP, *) |
| 517 |
getCell=getCell |
| 518 |
} Cfg |
| 519 |
(*------------------------------------------------------------------ |
| 520 |
* Scan the instructions compute the last uses and dead definitions |
| 521 |
* at each program point. Ideally we can do this during the code |
| 522 |
* rewriting phase. But that's probably too error prone for now. |
| 523 |
*------------------------------------------------------------------*) |
| 524 |
fun computeLastUse(blknum, insns, liveOut) = |
| 525 |
let fun scan([], _, lastUse) = lastUse |
| 526 |
| scan(i::instrs, live, lastUse) = |
| 527 |
let val (d, u) = defUse i |
| 528 |
val d = SL.uniq(d)(* definitions *) |
| 529 |
val u = SL.uniq(u)(* uses *) |
| 530 |
val dead = SL.return(SL.difference(d, live)) |
| 531 |
val live = SL.difference(live, d) |
| 532 |
val last = SL.return(SL.difference(u, live)) |
| 533 |
val live = SL.union(live, u) |
| 534 |
val _ = |
| 535 |
if debug andalso debugLiveness then |
| 536 |
(case last of |
| 537 |
[] => () |
| 538 |
| _ => print(assemble i^"\tlast use="^ |
| 539 |
fregsToString last^"\n") |
| 540 |
) |
| 541 |
else () |
| 542 |
in scan(instrs, live, (last,dead)::lastUse) |
| 543 |
end |
| 544 |
val liveOutSet = SL.uniq liveOut |
| 545 |
val _ = |
| 546 |
if debug andalso debugLiveness then |
| 547 |
print("LiveOut("^i2s blknum^") = "^ |
| 548 |
fregsToString(SL.return liveOutSet)^"\n") |
| 549 |
else () |
| 550 |
in scan(!insns, liveOutSet, []) |
| 551 |
end |
| 552 |
|
| 553 |
(*------------------------------------------------------------------ |
| 554 |
* Temporary work space |
| 555 |
*------------------------------------------------------------------*) |
| 556 |
val {high, low} = C.cellRange CB.FP |
| 557 |
val n = high+1 |
| 558 |
val lastUseTbl = A.array(n,~1) (* table for marking last uses *) |
| 559 |
val useTbl = A.array(n,~1) (* table for marking uses *) |
| 560 |
|
| 561 |
(* %fp register bindings before and after a basic block *) |
| 562 |
val bindingsIn = A.array(numberOfBlks, NONE) |
| 563 |
val bindingsOut = A.array(numberOfBlks, NONE) |
| 564 |
val stampCounter = ref ~4096 |
| 565 |
|
| 566 |
(* Edges that need splitting *) |
| 567 |
exception NoEdgesToSplit |
| 568 |
val edgesToSplit = IntHashTable.mkTable(32, NoEdgesToSplit) |
| 569 |
val addEdgesToSplit = IntHashTable.insert edgesToSplit |
| 570 |
fun lookupEdgesToSplit b = |
| 571 |
getOpt(IntHashTable.find edgesToSplit b, []) |
| 572 |
|
| 573 |
(*------------------------------------------------------------------ |
| 574 |
* Code for handling bindings between basic block |
| 575 |
*------------------------------------------------------------------*) |
| 576 |
|
| 577 |
fun splitEdge(title, source, target, e) = |
| 578 |
(if debug andalso !traceOn then |
| 579 |
pr(title^" SPLITTING "^i2s source^"->"^ i2s target^"\n") |
| 580 |
else (); |
| 581 |
addEdgesToSplit(target,(source,target,e)::lookupEdgesToSplit target) |
| 582 |
) |
| 583 |
|
| 584 |
fun computeFreq(_,_,CFG.EDGE{w,...}) = !w |
| 585 |
|
| 586 |
(* Given a cellset, return a sorted and unique |
| 587 |
* list of elements with all non-physical registers removed |
| 588 |
*) |
| 589 |
fun removeNonPhysical celllist = |
| 590 |
let fun loop([], S) = SL.return(SL.uniq S) |
| 591 |
| loop(f::fs, S) = |
| 592 |
let val fx = CB.registerNum f |
| 593 |
in loop(fs,if fx <= 7 then f::S else S) |
| 594 |
end |
| 595 |
in loop(celllist, []) |
| 596 |
end |
| 597 |
|
| 598 |
(* Given a sorted and unique list of registers, |
| 599 |
* Return a stack with these elements |
| 600 |
*) |
| 601 |
fun newStack fregs = |
| 602 |
let val stack = ST.create() |
| 603 |
in app (fn f => ST.push(stack, CB.registerNum f)) (rev fregs); |
| 604 |
stack |
| 605 |
end |
| 606 |
|
| 607 |
(* |
| 608 |
* This function looks at all the entries on the stack, |
| 609 |
* and generate code to deallocate all the dead values. |
| 610 |
* The stack is updated. |
| 611 |
*) |
| 612 |
fun removeDeadValues(stack, liveSet, code) = |
| 613 |
let val stamp = !stampCounter |
| 614 |
val _ = stampCounter := !stampCounter - 1 |
| 615 |
fun markLive [] = () |
| 616 |
| markLive(r::rs) = |
| 617 |
(A.update(useTbl, CB.registerNum r, stamp); markLive rs) |
| 618 |
fun isLive f = A.sub(useTbl, f) = stamp |
| 619 |
fun loop(i, depth, code) = |
| 620 |
if i >= depth then code else |
| 621 |
let val f = ST.st(stack, i) |
| 622 |
in if isLive f (* live? *) |
| 623 |
then loop(i+1, depth, code) |
| 624 |
else |
| 625 |
(if debug andalso !traceOn then |
| 626 |
pr("REMOVING %f"^i2s f^" in %st("^i2s i^")"^ |
| 627 |
" current stack="^ST.stackToString stack^"\n") |
| 628 |
else (); |
| 629 |
if i = 0 then |
| 630 |
(ST.pop stack; |
| 631 |
loop(0, depth-1, POP_ST::code) |
| 632 |
) |
| 633 |
else (ST.xch(stack,0,i); |
| 634 |
ST.pop stack; |
| 635 |
loop(0, depth-1, I.fstpl(ST i)::code) |
| 636 |
) |
| 637 |
) |
| 638 |
end |
| 639 |
in markLive liveSet; |
| 640 |
loop(0, ST.depth stack, code) |
| 641 |
end |
| 642 |
|
| 643 |
|
| 644 |
(*------------------------------------------------------------------ |
| 645 |
* Given two stacks, source and target, where the bindings are |
| 646 |
* permutation of each other, generate the minimal number of |
| 647 |
* fxchs to match source with target. |
| 648 |
* |
| 649 |
* Important: source and target MUST be permutations of each other. |
| 650 |
* |
| 651 |
* Essentially, we first decompose the permutation into cycles, |
| 652 |
* and process each cycle. |
| 653 |
*------------------------------------------------------------------*) |
| 654 |
fun shuffle(source, target, code) = |
| 655 |
let val stamp = !stampCounter |
| 656 |
val _ = stampCounter := !stampCounter - 1 |
| 657 |
val permutation = lastUseTbl (* reuse the space *) |
| 658 |
|
| 659 |
val _ = if debug andalso !traceOn then |
| 660 |
pr("SHUFFLE "^ST.stackToString source^ |
| 661 |
"->"^ST.stackToString target^"\n") |
| 662 |
else () |
| 663 |
|
| 664 |
(* Compute the initial permutation *) |
| 665 |
val n = ST.depth source |
| 666 |
fun computeInitialPermutation(i) = |
| 667 |
if i >= n |
| 668 |
then () |
| 669 |
else let val f = ST.st(source, i) |
| 670 |
val j = ST.fp(target, f) |
| 671 |
in A.update(permutation, j, i); |
| 672 |
computeInitialPermutation(i+1) |
| 673 |
end |
| 674 |
val _ = computeInitialPermutation 0 |
| 675 |
|
| 676 |
(* Decompose the initial permutation into cycles. |
| 677 |
* The cycle involving 0 is treated specially. |
| 678 |
*) |
| 679 |
val visited = useTbl |
| 680 |
fun isVisited i = A.sub(visited,i) = stamp |
| 681 |
fun markAsVisited i = A.update(visited,i,stamp) |
| 682 |
fun decomposeCycles(i, cycle0, cycles) = |
| 683 |
if i >= n then (cycle0, cycles) |
| 684 |
else if isVisited i orelse |
| 685 |
A.sub(permutation, i) = i (* trivial cycle *) |
| 686 |
then decomposeCycles(i+1, cycle0, cycles) |
| 687 |
else let fun makeCycle(j, cycle, zero) = |
| 688 |
let val k = A.sub(permutation, j) |
| 689 |
val cycle = j::cycle |
| 690 |
val zero = zero orelse j = 0 |
| 691 |
in markAsVisited j; |
| 692 |
if k = i then (cycle, zero) |
| 693 |
else makeCycle(k, cycle, zero) |
| 694 |
end |
| 695 |
val (cycle, zero) = makeCycle(i, [], false) |
| 696 |
in if zero then decomposeCycles(i+1, [cycle], cycles) |
| 697 |
else decomposeCycles(i+1, cycle0, cycle::cycles) |
| 698 |
end |
| 699 |
|
| 700 |
val (cycle0, cycles) = decomposeCycles(0, [], []) |
| 701 |
|
| 702 |
(* |
| 703 |
* Generate shuffle for a cycle that does not involve 0. |
| 704 |
* Given a cycle (c_1, ..., c_k), we generate this code: |
| 705 |
* fxch %st(c_1), |
| 706 |
* fxch %st(c_2), |
| 707 |
* ... |
| 708 |
* fxch %st(c_k), |
| 709 |
* fxch %st(c_1) |
| 710 |
*) |
| 711 |
fun genxch([], code) = code |
| 712 |
| genxch(c::cs, code) = genxch(cs, FXCH c::code) |
| 713 |
|
| 714 |
fun gen([], code) = error "shuffle.gen" |
| 715 |
| gen(cs as (c::_), code) = FXCH c::genxch(cs, code) |
| 716 |
|
| 717 |
(* |
| 718 |
* Generate shuffle for a cycle that involves 0. |
| 719 |
* Given a cycle (c_1,...,c_k) we first shuffle this to |
| 720 |
* an equivalent cycle (c_1, ..., c_k) where c'_k = 0, |
| 721 |
* then we generate this code: |
| 722 |
* fxch %st(c'_1), |
| 723 |
* fxch %st(c'_2), |
| 724 |
* ... |
| 725 |
* fxch %st(c'_{k-1}), |
| 726 |
*) |
| 727 |
fun gen0([], code) = error "shuffle.gen0" |
| 728 |
| gen0(cs, code) = |
| 729 |
let fun rearrange(0::cs, cs') = cs@rev cs' |
| 730 |
| rearrange(c::cs, cs') = rearrange(cs, c::cs') |
| 731 |
| rearrange([], _) = error "shuffle.rearrange" |
| 732 |
val cs = rearrange(cs, []) |
| 733 |
in genxch(cs, code) |
| 734 |
end |
| 735 |
|
| 736 |
(* |
| 737 |
* Generate code. Must process the non-zero cycles first. |
| 738 |
*) |
| 739 |
val code = List.foldr gen code cycles |
| 740 |
val code = List.foldr gen0 code cycle0 |
| 741 |
in code |
| 742 |
end (* shuffle *) |
| 743 |
|
| 744 |
(*------------------------------------------------------------------ |
| 745 |
* Insert code at the end of a basic block. |
| 746 |
* Make sure we put code in front of a transfer instruction |
| 747 |
*------------------------------------------------------------------*) |
| 748 |
fun insertAtEnd(insns, code) = |
| 749 |
(case insns of |
| 750 |
[] => code |
| 751 |
| jmp::rest => |
| 752 |
if P.instrKind jmp = P.IK_JUMP then |
| 753 |
jmp::code@rest |
| 754 |
else |
| 755 |
code@insns |
| 756 |
) |
| 757 |
|
| 758 |
(*------------------------------------------------------------------ |
| 759 |
* Magic for inserting shuffle code at the end of a basic block |
| 760 |
*------------------------------------------------------------------*) |
| 761 |
fun shuffleOut(stackOut, insns, b, block, liveOut) = |
| 762 |
let |
| 763 |
val liveOut = removeNonPhysical(liveOut) |
| 764 |
|
| 765 |
(* Generate code that remove unnecessary values *) |
| 766 |
val code = removeDeadValues(stackOut, liveOut, []) |
| 767 |
|
| 768 |
fun done(stackOut, insns, code) = |
| 769 |
(A.update(bindingsOut,b,SOME stackOut); |
| 770 |
insertAtEnd(insns, code) |
| 771 |
) |
| 772 |
|
| 773 |
(* Generate code that shuffle values from source to target *) |
| 774 |
fun match(source, target) = |
| 775 |
done(target, insns, shuffle(source, target, [])) |
| 776 |
|
| 777 |
(* Generate code that shuffle values from source to liveOut *) |
| 778 |
fun matchLiveOut() = |
| 779 |
case liveOut of |
| 780 |
[] => done(stackOut, insns, code) |
| 781 |
| _ => match(stackOut, newStack liveOut) |
| 782 |
|
| 783 |
(* With multiple successors, find out which one we |
| 784 |
* should connect to. Choose the one from the block that |
| 785 |
* follows from this one, if that exists, or else choose |
| 786 |
* from the edge with the highest frequency. |
| 787 |
*) |
| 788 |
fun find([], _, id, best) = (id, best) |
| 789 |
| find((_, target, _)::edges, highestFreq, id, best) = |
| 790 |
let val CFG.BLOCK{freq, ...} = #node_info cfg target |
| 791 |
in if target = b+1 then (target, A.sub(bindingsIn, target)) |
| 792 |
else (case A.sub(bindingsIn, target) of |
| 793 |
NONE => find(edges, highestFreq, id, best) |
| 794 |
| this as SOME stack => |
| 795 |
if highestFreq < !freq then |
| 796 |
find(edges, !freq, target, this) |
| 797 |
else |
| 798 |
find(edges, highestFreq, id, best) |
| 799 |
) |
| 800 |
end |
| 801 |
|
| 802 |
(* |
| 803 |
* Split all edges source->target except omitThis. |
| 804 |
*) |
| 805 |
fun splitAllEdgesExcept([], omitThis) = () |
| 806 |
| splitAllEdgesExcept((source,target,e)::edges, omitThis) = |
| 807 |
if target = EXIT then error "can't split exit edge!" |
| 808 |
else |
| 809 |
(if target <> omitThis andalso |
| 810 |
target <= b andalso (* XXX *) |
| 811 |
target <> ENTRY |
| 812 |
then splitEdge("ShuffleOut",source,target,e) else (); |
| 813 |
splitAllEdgesExcept(edges, omitThis) |
| 814 |
) |
| 815 |
|
| 816 |
(* Just one successor; |
| 817 |
* try to match the bindings of the successor if it exist. |
| 818 |
*) |
| 819 |
fun matchIt succ = |
| 820 |
let val (succBlock, target) = find(succ, ~1.0, ~1, NONE) |
| 821 |
in splitAllEdgesExcept(succ, succBlock); |
| 822 |
case target of |
| 823 |
SOME stackIn => match(stackOut, stackIn) |
| 824 |
| NONE => done(stackOut,insns,code) |
| 825 |
end |
| 826 |
|
| 827 |
in case #out_edges cfg b of |
| 828 |
[] => matchLiveOut() |
| 829 |
| succ as [(_,target,_)] => |
| 830 |
if target = EXIT then matchLiveOut() |
| 831 |
else matchIt succ |
| 832 |
| succ => matchIt succ |
| 833 |
end (* shuffleOut *) |
| 834 |
|
| 835 |
(*------------------------------------------------------------------ |
| 836 |
* Compute the initial fp stack bindings for basic block b. |
| 837 |
*------------------------------------------------------------------*) |
| 838 |
fun shuffleIn(b, block, liveIn) = |
| 839 |
let |
| 840 |
val liveInSet = removeNonPhysical liveIn |
| 841 |
|
| 842 |
(* With multiple predecessors, find out which one we |
| 843 |
* should connect to. Choose the one from the block that |
| 844 |
* falls into this one, if that exists, or else choose |
| 845 |
* from the edge with the highest frequency. |
| 846 |
*) |
| 847 |
fun find([], _, best) = best |
| 848 |
| find((source, _, _)::edges, highestFreq, best) = |
| 849 |
let val CFG.BLOCK{freq, ...} = #node_info cfg source |
| 850 |
in case A.sub(bindingsOut, source) of |
| 851 |
NONE => find(edges, highestFreq, best) |
| 852 |
| this as SOME stack => |
| 853 |
if source = b-1 |
| 854 |
then this (* falls into b *) |
| 855 |
else if highestFreq < !freq then find(edges, !freq, this) |
| 856 |
else find(edges, highestFreq, best) |
| 857 |
end |
| 858 |
|
| 859 |
fun splitAllDoneEdges [] = () |
| 860 |
| splitAllDoneEdges ((source, target, e)::edges) = |
| 861 |
(if source < b andalso |
| 862 |
source <> ENTRY andalso |
| 863 |
source <> EXIT |
| 864 |
then splitEdge("ShuffleIn", source, target, e) else (); |
| 865 |
splitAllDoneEdges edges |
| 866 |
) |
| 867 |
|
| 868 |
(* The initial stack bindings are determined by the live set. |
| 869 |
* No compensation code is needed. |
| 870 |
*) |
| 871 |
fun fromLiveIn() = |
| 872 |
let val stackIn = |
| 873 |
case liveInSet of |
| 874 |
[] => ST.stack0 |
| 875 |
| _ => |
| 876 |
(pr("liveIn="^celllistToString liveIn^"\n"); |
| 877 |
newStack liveInSet |
| 878 |
) |
| 879 |
val stackOut = ST.copy stackIn |
| 880 |
in (stackIn, stackOut, []) |
| 881 |
end |
| 882 |
|
| 883 |
val pred = #in_edges cfg b |
| 884 |
|
| 885 |
val (stackIn, stackOut, code) = |
| 886 |
case find(pred, ~1.0, NONE) of |
| 887 |
NONE => (splitAllDoneEdges(pred); fromLiveIn()) |
| 888 |
| SOME stackIn' => |
| 889 |
(case pred of |
| 890 |
[_] => (* one predecessor *) |
| 891 |
(* Use the bindings as from the previous block |
| 892 |
* We first have to deallocate all unused values. |
| 893 |
*) |
| 894 |
let val stackOut = ST.copy stackIn' |
| 895 |
(* Clean the stack of unused entries *) |
| 896 |
val code = removeDeadValues(stackOut, liveInSet, []) |
| 897 |
in (stackIn', stackOut, code) end |
| 898 |
| pred => (* more than one predecessors *) |
| 899 |
let val stackIn = ST.copy stackIn' |
| 900 |
val code = removeDeadValues(stackIn, liveInSet, []) |
| 901 |
val stackOut = ST.copy stackIn |
| 902 |
in (* If we have to generate code to deallocate |
| 903 |
* the stack then we have split the edge. |
| 904 |
*) |
| 905 |
case code of |
| 906 |
[] => () |
| 907 |
| _ => splitAllDoneEdges(pred); |
| 908 |
(stackIn, stackOut, []) |
| 909 |
end |
| 910 |
) |
| 911 |
in A.update(bindingsIn, b, SOME stackIn); |
| 912 |
A.update(bindingsOut, b, SOME stackOut); |
| 913 |
(stackIn, stackOut, code) |
| 914 |
end |
| 915 |
|
| 916 |
(*------------------------------------------------------------------ |
| 917 |
* Code for patching up critical edges. |
| 918 |
* The trick is finding a good place to insert the critical edges. |
| 919 |
* Let's call an edge x->y that requires compensation |
| 920 |
* code c to be inserted an candidate edge. We write this as x->y(c) |
| 921 |
* |
| 922 |
* Here are the heuristics that we use to improve the final code: |
| 923 |
* |
| 924 |
* 1. Given two candidate edges a->x(c1) and b->x(c2) where c1=c2 |
| 925 |
* then we can merge the two copies of compensation code. |
| 926 |
* This is quite common. This generalizes to any number of edges. |
| 927 |
* |
| 928 |
* 2. Given two candidate edges a->x(c1) and b->x(c2) and where |
| 929 |
* c1 and c2 are pops, we can partially share c1 and c2. |
| 930 |
* Currently, I think I only recognize this case when |
| 931 |
* x has no fp registers live-in. |
| 932 |
* |
| 933 |
* 3. Given two candidate edges a->x(c1) and b->x(c2), |
| 934 |
* if a->x has a higher frequency then put the compensation |
| 935 |
* code in front of x (so that it falls through into x) |
| 936 |
* whenever possible. |
| 937 |
* |
| 938 |
* As you can see, the voodoo is strong here. |
| 939 |
* |
| 940 |
* The routine has two main phases: |
| 941 |
* 1. Determine the compensation code by applying the heuristics |
| 942 |
* above. |
| 943 |
* 2. Then insert them and rebuild the cfg by renaming all block |
| 944 |
* ids. This is currently necessary to keep the layout order |
| 945 |
* consistent with the order of the id. |
| 946 |
*------------------------------------------------------------------*) |
| 947 |
fun repairCriticalEdges(Cfg as G.GRAPH cfg) = |
| 948 |
let |
| 949 |
val cleanup = [#create MLRiscAnnotations.COMMENT "cleanup edge"] |
| 950 |
val critical = [#create MLRiscAnnotations.COMMENT "critical edge"] |
| 951 |
|
| 952 |
fun annotate(gen, an) = |
| 953 |
app (fn ((_,CFG.BLOCK{annotations, ...}),_) => annotations := an) |
| 954 |
gen |
| 955 |
|
| 956 |
(* |
| 957 |
* Special case: target block has stack depth of 0. |
| 958 |
* Just generate code that pop entries from the sources. |
| 959 |
* To make things interesting, we try to share code among |
| 960 |
* all the critical edges. |
| 961 |
*) |
| 962 |
fun genPoppingCode(_, []) = () |
| 963 |
| genPoppingCode(targetId, edges) = |
| 964 |
let (* Edges annotated with the source stack depth |
| 965 |
* Ordered by increasing stack height |
| 966 |
*) |
| 967 |
val edges = |
| 968 |
IM.listItemsi |
| 969 |
(foldr (fn (edge as (sourceId, _, _), M) => |
| 970 |
let val n = ST.depth(valOf(A.sub(bindingsOut,sourceId))) |
| 971 |
in IM.insert(M, n, edge :: getOpt(IM.find(M, n), [])) |
| 972 |
end) IM.empty edges) |
| 973 |
|
| 974 |
(* Generate n pops *) |
| 975 |
fun pops(0, code) = code |
| 976 |
| pops(n, code) = pops(n-1, POP_ST::code) |
| 977 |
|
| 978 |
(* Create the chain of blocks *) |
| 979 |
fun makeChain(depth, [], chain) = chain |
| 980 |
| makeChain(depth, (d, es)::es', chain) = |
| 981 |
let val code = pops(d - depth, []) |
| 982 |
in makeChain(d, es', (es, code)::chain) |
| 983 |
end |
| 984 |
|
| 985 |
val chain = makeChain(0, edges, []) |
| 986 |
|
| 987 |
in annotate(CFG.splitEdges Cfg {groups=chain, jump=false}, cleanup) |
| 988 |
end |
| 989 |
|
| 990 |
(* |
| 991 |
* Generate repair code. |
| 992 |
*) |
| 993 |
fun genRepairCode(targetId, stackIn, edges) = |
| 994 |
let val liveIn = IntHashTable.lookup liveInTable targetId |
| 995 |
val liveInSet = removeNonPhysical liveIn |
| 996 |
val _ = if debug then |
| 997 |
pr("LiveIn = "^celllistToString liveIn^"\n") |
| 998 |
else () |
| 999 |
|
| 1000 |
(* Group all edges whose output stack configurations |
| 1001 |
* are the same. Each group is merged together into |
| 1002 |
* a single compensation block |
| 1003 |
*) |
| 1004 |
fun partition([], S) = S |
| 1005 |
| partition((e as (src,_,_))::es, S) = |
| 1006 |
let val stackOut = ST.copy(valOf(A.sub(bindingsOut,src))) |
| 1007 |
fun find([], S) = partition(es, ([e],stackOut)::S) |
| 1008 |
| find((x as (es',st'))::S', S) = |
| 1009 |
if ST.equal(stackOut,st') then |
| 1010 |
partition(es, (e::es',st')::S' @ S) |
| 1011 |
else |
| 1012 |
find(S', x::S) |
| 1013 |
in find(S, []) |
| 1014 |
end |
| 1015 |
|
| 1016 |
(* Partition by the source bindings *) |
| 1017 |
val S = partition(edges, []) |
| 1018 |
|
| 1019 |
(* Compute frequencies *) |
| 1020 |
val S = map (fn (es,st) => (CFG.sumEdgeFreqs es,es,st)) S |
| 1021 |
|
| 1022 |
(* Ordered by non-increasing frequencies *) |
| 1023 |
val S = ListMergeSort.sort (fn ((x,_,_),(y,_,_)) => x < y) S |
| 1024 |
|
| 1025 |
(* Generate code *) |
| 1026 |
fun gen(freq, edges, stackOut) = |
| 1027 |
let (* deallocate unused values *) |
| 1028 |
val code = removeDeadValues(stackOut,liveInSet,[]) |
| 1029 |
(* shuffle values *) |
| 1030 |
val code = shuffle(stackOut, stackIn, code) |
| 1031 |
in annotate( |
| 1032 |
CFG.splitEdges Cfg {groups=[(edges,code)], jump=false}, |
| 1033 |
critical) |
| 1034 |
end |
| 1035 |
|
| 1036 |
in app gen S |
| 1037 |
end |
| 1038 |
|
| 1039 |
(* Split all edges entering targetId *) |
| 1040 |
fun split(targetId, edges) = |
| 1041 |
let val stackIn = valOf(A.sub(bindingsIn,targetId)) |
| 1042 |
fun log(s, t, e) = |
| 1043 |
case A.sub (bindingsOut, s) of |
| 1044 |
SOME stackOut => |
| 1045 |
(pr("SPLIT "^i2s s^"->"^i2s t^" "^ |
| 1046 |
ST.stackToString stackOut^"->"^ |
| 1047 |
ST.stackToString stackIn^"\n")) |
| 1048 |
| NONE => error "split:stackOut" |
| 1049 |
val _ = if debug andalso !traceOn then app log edges else () |
| 1050 |
in if ST.depth stackIn = 0 then genPoppingCode(targetId, edges) |
| 1051 |
else genRepairCode(targetId, stackIn, edges) |
| 1052 |
end |
| 1053 |
|
| 1054 |
in IntHashTable.appi split edgesToSplit; |
| 1055 |
CFG.changed Cfg; |
| 1056 |
Cfg |
| 1057 |
end |
| 1058 |
|
| 1059 |
(*------------------------------------------------------------------ |
| 1060 |
* Process all blocks which are not the entry or the exit |
| 1061 |
*------------------------------------------------------------------*) |
| 1062 |
val stamp = ref 0 |
| 1063 |
fun rewriteAllBlocks (_, CFG.BLOCK{kind=CFG.START, ...}) = () |
| 1064 |
| rewriteAllBlocks (_, CFG.BLOCK{kind=CFG.STOP, ...}) = () |
| 1065 |
| rewriteAllBlocks |
| 1066 |
(blknum, block as CFG.BLOCK{insns, labels, annotations, ...}) = |
| 1067 |
let val _ = |
| 1068 |
if debug andalso !debugOn then |
| 1069 |
app (fn l => pr(L.toString l^":\n")) (!labels) |
| 1070 |
else (); |
| 1071 |
val liveIn = HT.lookup liveInTable blknum |
| 1072 |
val liveOut = HT.lookup liveOutTable blknum |
| 1073 |
val st = rewrite(!stamp, blknum, block, |
| 1074 |
insns, liveIn, liveOut, |
| 1075 |
annotations) |
| 1076 |
in stamp := st (* update stamp *) |
| 1077 |
end |
| 1078 |
|
| 1079 |
(*------------------------------------------------------------------ |
| 1080 |
* Translate code within a basic block. |
| 1081 |
* Each instruction is given a unique stamp for identifying last |
| 1082 |
* uses. |
| 1083 |
*------------------------------------------------------------------*) |
| 1084 |
and rewrite(stamp, blknum, block, insns, liveIn, liveOut, |
| 1085 |
annotations) = |
| 1086 |
let val (stackIn, stack, code) = shuffleIn(blknum, block, liveIn) |
| 1087 |
|
| 1088 |
(* Dump instructions when encountering a bug *) |
| 1089 |
fun bug msg = |
| 1090 |
(pr("-------- bug in block "^i2s blknum^" ----\n"); |
| 1091 |
dump(!insns); |
| 1092 |
error msg |
| 1093 |
) |
| 1094 |
|
| 1095 |
fun loop(stamp, [], [], code) = (stamp, code) |
| 1096 |
| loop(stamp, instr::rest, (lastUse,dead)::lastUses, code) = |
| 1097 |
let fun mark(tbl, []) = () |
| 1098 |
| mark(tbl, r::rs) = |
| 1099 |
(A.update(tbl, CB.registerNum r, stamp); mark(tbl, rs)) |
| 1100 |
in mark(lastUseTbl,lastUse); (* mark all last uses *) |
| 1101 |
trans(stamp, instr, [], rest, dead, lastUses, code) |
| 1102 |
end |
| 1103 |
| loop _ = error "loop" |
| 1104 |
|
| 1105 |
(* |
| 1106 |
* Main routine that does the actual translation. |
| 1107 |
* A few reminders: |
| 1108 |
* o The instructions are processed in normal order |
| 1109 |
* and generated in the reversed order. |
| 1110 |
* o (Local) liveness is computed at the same time. |
| 1111 |
* o For each use, we have to find out whether it is |
| 1112 |
* the last use. If so, we can kill it and reclaim |
| 1113 |
* the stack entry at the same time. |
| 1114 |
*) |
| 1115 |
and trans(stamp, instr, an, rest, dead, lastUses, code) = |
| 1116 |
let (* Call this continuation when done with code generation *) |
| 1117 |
fun FINISH code = loop(stamp+1, rest, lastUses, code) |
| 1118 |
|
| 1119 |
fun KILL_THE_DEAD(dead, code) = |
| 1120 |
let fun kill([], code) = FINISH code |
| 1121 |
| kill(f::fs, code) = |
| 1122 |
let val fx = CB.registerNum f |
| 1123 |
in if debug andalso debugDead then |
| 1124 |
pr("DEAD "^fregToString f^" in "^ |
| 1125 |
ST.stackToString stack^"\n") |
| 1126 |
else (); |
| 1127 |
(* not a physical register *) |
| 1128 |
if fx >= 8 then kill(fs, code) |
| 1129 |
else |
| 1130 |
let val i = ST.fp(stack, fx) |
| 1131 |
in if debug andalso debugDead then |
| 1132 |
pr("KILLING "^fregToString f^ |
| 1133 |
"=%st("^i2s i^")\n") |
| 1134 |
else (); |
| 1135 |
if i < 0 then kill(fs, code) (* dead already *) |
| 1136 |
else if i = 0 then |
| 1137 |
(ST.pop stack; kill(fs, POP_ST::code)) |
| 1138 |
else |
| 1139 |
(ST.xch(stack,0,i); ST.pop stack; |
| 1140 |
kill(fs, I.fstpl(ST i)::code) |
| 1141 |
) |
| 1142 |
end |
| 1143 |
end |
| 1144 |
in kill(dead, code) |
| 1145 |
end |
| 1146 |
|
| 1147 |
(* Call this continuation when done with floating point |
| 1148 |
* code generation. Remove all dead code first. |
| 1149 |
*) |
| 1150 |
fun DONE code = KILL_THE_DEAD(dead, code) |
| 1151 |
|
| 1152 |
(* Is this the last use of register f? *) |
| 1153 |
fun isLastUse f = A.sub(lastUseTbl, f) = stamp |
| 1154 |
|
| 1155 |
(* Is this value dead? *) |
| 1156 |
fun isDead f = |
| 1157 |
let fun loop [] = false |
| 1158 |
| loop(r::rs) = CB.sameColor(f,r) orelse loop rs |
| 1159 |
in loop dead end |
| 1160 |
|
| 1161 |
(* Dump the stack before each intruction for debugging *) |
| 1162 |
fun log() = if debug andalso !traceOn then |
| 1163 |
pr(ST.stackToString stack^assemble instr^"...\n") |
| 1164 |
else () |
| 1165 |
|
| 1166 |
(* Find the location of a source register *) |
| 1167 |
fun getfs(f) = |
| 1168 |
let val fx = CB.registerNum f |
| 1169 |
val s = ST.fp(stack, fx) |
| 1170 |
in (isLastUse fx,s) end |
| 1171 |
|
| 1172 |
(* Generate memory to memory move *) |
| 1173 |
fun mmmove(fsize,src,dst) = |
| 1174 |
let val _ = ST.nonFull stack |
| 1175 |
val code = FLD(fsize,src)::code |
| 1176 |
val code = mark(FSTP(fsize,dst),an)::code |
| 1177 |
in DONE code end |
| 1178 |
|
| 1179 |
(* Allocate a new register in %st(0) *) |
| 1180 |
fun alloc(f,code) = (ST.push(stack,CB.registerNum f); code) |
| 1181 |
|
| 1182 |
(* register -> register move *) |
| 1183 |
fun rrmove(fs,fd) = |
| 1184 |
if CB.sameColor(fs,fd) then DONE code |
| 1185 |
else |
| 1186 |
let val (dead,ss) = getfs fs |
| 1187 |
in if dead then (* fs is dead *) |
| 1188 |
(ST.set(stack,ss,CB.registerNum fd); (* rename fd to fs *) |
| 1189 |
DONE code (* no code is generated *) |
| 1190 |
) |
| 1191 |
else (* fs is not dead; push it onto %st(0); |
| 1192 |
* set fd to %st(0) |
| 1193 |
*) |
| 1194 |
let val code = alloc(fd, code) |
| 1195 |
in DONE(mark(I.fldl(ST ss),an)::code) |
| 1196 |
end |
| 1197 |
end |
| 1198 |
|
| 1199 |
(* memory -> register move. |
| 1200 |
* Do dead code elimination here. |
| 1201 |
*) |
| 1202 |
fun mrmove(fsize,src,fd) = |
| 1203 |
if isDead fd |
| 1204 |
then FINISH code (* value has been killed *) |
| 1205 |
else |
| 1206 |
let val code = alloc(fd, code) |
| 1207 |
in DONE(mark(FLD(fsize,src),an)::code) |
| 1208 |
end |
| 1209 |
|
| 1210 |
(* exchange %st(n) and %st(0) *) |
| 1211 |
fun xch(n) = (ST.xch(stack,0,n); FXCH n) |
| 1212 |
|
| 1213 |
(* push %st(n) onto the stack *) |
| 1214 |
fun push(n) = (ST.push(stack,~2); I.fldl(ST n)) |
| 1215 |
|
| 1216 |
|
| 1217 |
(* push mem onto the stack *) |
| 1218 |
fun pushmem(src) = (ST.push(stack,~2); I.fldl(src)) |
| 1219 |
|
| 1220 |
(* register -> memory move. |
| 1221 |
* Use pop version of the opcode if it is the last use. |
| 1222 |
*) |
| 1223 |
fun rmmove(fsize,fs,dst) = |
| 1224 |
let fun fstp(code) = |
| 1225 |
(ST.pop stack; DONE(mark(FSTP(fsize,dst),an)::code)) |
| 1226 |
fun fst(code) = DONE(mark(FST(fsize,dst),an)::code) |
| 1227 |
in case getfs fs of |
| 1228 |
(true, 0) => fstp code |
| 1229 |
| (true, n) => fstp(xch n::code) |
| 1230 |
| (false, 0) => fst(code) |
| 1231 |
| (false, n) => fst(xch n::code) |
| 1232 |
end |
| 1233 |
|
| 1234 |
(* Floating point move *) |
| 1235 |
fun fmove{fsize,src=I.FPR fs,dst=I.FPR fd} = rrmove(fs,fd) |
| 1236 |
| fmove{fsize,src,dst=I.FPR fd} = mrmove(fsize,src,fd) |
| 1237 |
| fmove{fsize,src=I.FPR fs,dst} = rmmove(fsize,fs,dst) |
| 1238 |
| fmove{fsize,src,dst} = mmmove(fsize,src,dst) |
| 1239 |
|
| 1240 |
(* Floating point integer load operator *) |
| 1241 |
fun fiload{isize,ea,dst=I.FPR fd} = |
| 1242 |
let val code = alloc(fd, code) |
| 1243 |
val code = mark(FILD(isize,ea),an)::code |
| 1244 |
in DONE code |
| 1245 |
end |
| 1246 |
| fiload{isize,ea,dst} = |
| 1247 |
let val code = mark(FILD(isize,ea),an)::code |
| 1248 |
val code = I.fstpl(dst)::code (* XXX *) |
| 1249 |
in DONE code |
| 1250 |
end |
| 1251 |
|
| 1252 |
(* Make a copy of register fs to %st(0). *) |
| 1253 |
fun moveregtotop(fs, code) = |
| 1254 |
(case getfs fs of |
| 1255 |
(true, 0) => code |
| 1256 |
| (true, n) => xch n::code |
| 1257 |
| (false, n) => push n::code |
| 1258 |
) |
| 1259 |
|
| 1260 |
fun movememtotop(fsize, mem, code) = |
| 1261 |
(ST.push(stack, ~2); FLD(fsize, mem)::code) |
| 1262 |
|
| 1263 |
(* Move an operand to top of stack *) |
| 1264 |
fun movetotop(fsize, I.FPR fs, code) = moveregtotop(fs, code) |
| 1265 |
| movetotop(fsize, mem, code) = movememtotop(fsize, mem, code) |
| 1266 |
|
| 1267 |
fun storeResult(fsize, dst, n, code) = |
| 1268 |
case dst of |
| 1269 |
I.FPR fd => (ST.set(stack, n, CB.registerNum fd); DONE code) |
| 1270 |
| mem => |
| 1271 |
let val code = if n = 0 then code else xch n::code |
| 1272 |
in ST.pop stack; DONE(FSTP(fsize, mem)::code) end |
| 1273 |
|
| 1274 |
(* Floating point unary operator *) |
| 1275 |
fun funop{fsize,unOp,src,dst} = |
| 1276 |
let val code = movetotop(fsize, src, code) |
| 1277 |
val code = mark(I.funary unOp,an)::code |
| 1278 |
|
| 1279 |
(* Moronic hack to deal with partial tangent! *) |
| 1280 |
val code = |
| 1281 |
case unOp of |
| 1282 |
I.FPTAN => |
| 1283 |
(if ST.depth stack >= 7 then error "FPTAN" |
| 1284 |
else (); |
| 1285 |
POP_ST::code (* pop the useless 1.0 *) |
| 1286 |
) |
| 1287 |
| _ => code |
| 1288 |
in storeResult(fsize, dst, 0, code) |
| 1289 |
end |
| 1290 |
|
| 1291 |
(* Floating point binary operator. |
| 1292 |
* Note: |
| 1293 |
* binop src, dst |
| 1294 |
* means dst := dst binop src |
| 1295 |
* (lsrc := lsrc binop rsrc) |
| 1296 |
* on the x86 |
| 1297 |
*) |
| 1298 |
fun fbinop{fsize,binOp,lsrc,rsrc,dst} = |
| 1299 |
let (* generate code and set %st(n) = fd *) |
| 1300 |
(* op2 := op1 - op2 *) |
| 1301 |
fun oper(binOp,op1,op2,n,code) = |
| 1302 |
let val code = |
| 1303 |
mark(I.fbinary{binOp=binOp,src=op1,dst=op2},an) |
| 1304 |
::code |
| 1305 |
in storeResult(I.FP64, dst, n, code) |
| 1306 |
end |
| 1307 |
|
| 1308 |
fun operR(binOp,op1,op2,n,code) = |
| 1309 |
oper(invert binOp,op1,op2,n,code) |
| 1310 |
|
| 1311 |
fun operP(binOp,op1,op2,n,code) = |
| 1312 |
(ST.pop stack; oper(pop binOp,op1,op2,n-1,code)) |
| 1313 |
|
| 1314 |
fun operRP(binOp,op1,op2,n,code) = |
| 1315 |
(ST.pop stack; operR(pop binOp,op1,op2,n-1,code)) |
| 1316 |
|
| 1317 |
(* Many special cases to consider. |
| 1318 |
* Basically, try to reuse stack space as |
| 1319 |
* much as possible by taking advantage of last uses. |
| 1320 |
* |
| 1321 |
* Stack=[st(0)=3.0 st(1)=2.0] |
| 1322 |
* fsub %st(1), %st [1,2.0] |
| 1323 |
* fsubr %st(1), %st [-1,2.0] |
| 1324 |
* fsub %st, %st(1) [3.0,1.0] |
| 1325 |
* fsubr %st, %st(1) [3.0,-1.0] |
| 1326 |
* |
| 1327 |
* fsubp %st, %st(1) [1] |
| 1328 |
* fsubrp %st, %st(1) [-1] |
| 1329 |
* So, |
| 1330 |
* fsub %st(n), %st (means %st - %st(n) -> %st) |
| 1331 |
* fsub %st, %st(n) (means %st - %st(n) -> %st(n)) |
| 1332 |
* fsubr %st(n), %st (means %st(n) - %st -> %st) |
| 1333 |
* fsubr %st, %st(n) (means %st(n) - %st -> %st(n)) |
| 1334 |
*) |
| 1335 |
fun reg2(fx, fy) = |
| 1336 |
let val (dx, sx) = getfs fx |
| 1337 |
val (dy, sy) = getfs fy |
| 1338 |
fun loop(dx, sx, dy, sy, code) = |
| 1339 |
(* op1, op2 (dst) *) |
| 1340 |
case (dx, sx, dy, sy) of |
| 1341 |
(true, 0, false, n) => oper(binOp,ST n,ST0,0,code) |
| 1342 |
| (false, n, true, 0) => operR(binOp,ST n,ST0,0,code) |
| 1343 |
| (true, n, true, 0) => operRP(binOp,ST0,ST n,n,code) |
| 1344 |
| (true, 0, true, n) => operP(binOp,ST0,ST n,n,code) |
| 1345 |
| (false, 0, true, n) => oper(binOp,ST0,ST n,n,code) |
| 1346 |
| (true, n, false, 0) => operR(binOp,ST0,ST n,n,code) |
| 1347 |
| (true, sx, dy, sy) => |
| 1348 |
loop(true, 0, dy, sy, xch sx::code) |
| 1349 |
| (dx, sx, true, sy) => |
| 1350 |
loop(dx, sx, true, 0, xch sy::code) |
| 1351 |
| (false, sx, false, sy) => |
| 1352 |
loop(true, 0, false, sy+1, push sx::code) |
| 1353 |
in if sx = sy then (* same register *) |
| 1354 |
let val code = |
| 1355 |
case (dx, sx) of |
| 1356 |
(true, 0) => code |
| 1357 |
| (true, n) => xch n::code |
| 1358 |
| (false, n) => push n::code |
| 1359 |
in oper(binOp,ST0,ST0,0,code) |
| 1360 |
end |
| 1361 |
else loop(dx, sx, dy, sy, code) |
| 1362 |
end |
| 1363 |
|
| 1364 |
(* reg/mem operands *) |
| 1365 |
fun regmem(binOp, fx, mem) = |
| 1366 |
case getfs fx of |
| 1367 |
(true, 0) => oper(binOp,mem,ST0,0,code) |
| 1368 |
| (true, n) => oper(binOp,mem,ST0,0,xch n::code) |
| 1369 |
| (false, n) => oper(binOp,mem,ST0,0,push n::code) |
| 1370 |
|
| 1371 |
(* Two memory operands. Optimize the case when |
| 1372 |
* the two operands are identical. |
| 1373 |
*) |
| 1374 |
fun mem2(lsrc, rsrc) = |
| 1375 |
let val _ = ST.push(stack,~2) |
| 1376 |
val code = FLD(fsize,lsrc)::code |
| 1377 |
val rsrc = if P.eqOpn(lsrc, rsrc) then ST0 else rsrc |
| 1378 |
in oper(binOp,rsrc,ST0,0,code) |
| 1379 |
end |
| 1380 |
|
| 1381 |
fun process(I.FPR fx, I.FPR fy) = reg2(fx, fy) |
| 1382 |
| process(I.FPR fx, mem) = regmem(binOp, fx, mem) |
| 1383 |
| process(mem, I.FPR fy) = regmem(invert binOp, fy, mem) |
| 1384 |
| process(lsrc, rsrc) = mem2(lsrc, rsrc) |
| 1385 |
|
| 1386 |
in process(lsrc, rsrc) |
| 1387 |
end |
| 1388 |
|
| 1389 |
(* Floating point binary operator with integer conversion *) |
| 1390 |
fun fibinop{isize,binOp,lsrc,rsrc,dst} = |
| 1391 |
let fun oper(binOp,src,code) = |
| 1392 |
let val code = mark(I.fibinary{binOp=binOp,src=src},an) |
| 1393 |
::code |
| 1394 |
in storeResult(I.FP64, dst, 0, code) |
| 1395 |
end |
| 1396 |
|
| 1397 |
fun regmem(binOp, fx, mem) = |
| 1398 |
case getfs fx of |
| 1399 |
(true, 0) => oper(binOp, mem, code) |
| 1400 |
| (true, n) => oper(binOp, mem, xch n::code) |
| 1401 |
| (false, n) => oper(binOp, mem, push n::code) |
| 1402 |
|
| 1403 |
in case (lsrc, rsrc) of |
| 1404 |
(I.FPR fx, mem) => regmem(binOp, fx, mem) |
| 1405 |
| (lsrc, rsrc) => oper(binOp, rsrc, pushmem lsrc::code) |
| 1406 |
end |
| 1407 |
|
| 1408 |
(* Floating point comparison |
| 1409 |
* We have to make sure there are enough registers. |
| 1410 |
* The trick is that tmp is always a physical register. |
| 1411 |
* So we can always use it as temporary space if we |
| 1412 |
* have run out. |
| 1413 |
*) |
| 1414 |
fun fcmp{i,fsize,lsrc,rsrc} = |
| 1415 |
let fun fucompp code = |
| 1416 |
(ST.pop stack; ST.pop stack; |
| 1417 |
if i then |
| 1418 |
POP_ST :: mark(I.fucomip(ST 1), an) :: code |
| 1419 |
else |
| 1420 |
mark(I.fucompp,an) :: code |
| 1421 |
) |
| 1422 |
fun fucomp(n) = |
| 1423 |
(ST.pop stack; |
| 1424 |
mark((if i then I.fucomip else I.fucomp)(ST n),an)) |
| 1425 |
fun fucom(n) = |
| 1426 |
mark((if i then I.fucomi else I.fucom)(ST n),an) |
| 1427 |
|
| 1428 |
fun genmemcmp() = |
| 1429 |
let val code = movememtotop(fsize, rsrc, code) |
| 1430 |
val code = movememtotop(fsize, lsrc, code) |
| 1431 |
in FINISH(fucompp(code)) |
| 1432 |
end |
| 1433 |
|
| 1434 |
fun genmemregcmp(lsrc, fy) = |
| 1435 |
case getfs fy of |
| 1436 |
(false, n) => |
| 1437 |
let val code = movememtotop(fsize, lsrc, code) |
| 1438 |
in FINISH(fucomp(n+1)::code) end |
| 1439 |
| (true, n) => |
| 1440 |
let val code = if n = 0 then code else xch n::code |
| 1441 |
val code = movememtotop(fsize, lsrc, code) |
| 1442 |
in FINISH(fucompp(code)) |
| 1443 |
end |
| 1444 |
|
| 1445 |
fun genregmemcmp(fx, rsrc) = |
| 1446 |
let val code = |
| 1447 |
case getfs fx of |
| 1448 |
(true, n) => |
| 1449 |
let val code = if n = 0 then code |
| 1450 |
else xch n::code |
| 1451 |
val code = movememtotop(fsize, rsrc, code) |
| 1452 |
in xch 1::code end |
| 1453 |
| (false, n) => |
| 1454 |
let val code = movememtotop(fsize, rsrc, code) |
| 1455 |
in push(n+1)::code |
| 1456 |
end |
| 1457 |
in FINISH(fucompp(code)) |
| 1458 |
end |
| 1459 |
|
| 1460 |
(* Deal with the special case when both sources are |
| 1461 |
* in the same register |
| 1462 |
*) |
| 1463 |
fun regsame(dx, sx) = |
| 1464 |
let val (code, cmp) = |
| 1465 |
case (dx, sx) of |
| 1466 |
(true, 0) => (code, fucomp 0) (* pop once! *) |
| 1467 |
| (false, 0) => (code, fucom 0) (* don't pop! *) |
| 1468 |
| (true, n) => (xch n::code, fucomp 0) |
| 1469 |
| (false, n) => (xch n::code, fucom 0) |
| 1470 |
in FINISH(cmp::code) end |
| 1471 |
|
| 1472 |
fun reg2(fx, fy) = |
| 1473 |
(* special case is when things are already in place. |
| 1474 |
* Note: should also generate FUCOM and FUCOMP!!! |
| 1475 |
*) |
| 1476 |
let val (dx, sx) = getfs fx |
| 1477 |
val (dy, sy) = getfs fy |
| 1478 |
fun fstp(n) = |
| 1479 |
(ST.xch(stack,n,0); ST.pop stack; I.fstpl(ST n)) |
| 1480 |
in if sx = sy then regsame(dx, sx) (* same register!*) |
| 1481 |
else |
| 1482 |
(* first, move sx to %st(0) *) |
| 1483 |
let val (sy, code) = |
| 1484 |
if sx = 0 then (sy, code) (* there already *) |
| 1485 |
else (if sy = 0 then sx else sy, |
| 1486 |
xch sx::code) |
| 1487 |
|
| 1488 |
(* Generate the appropriate comparison op *) |
| 1489 |
val (sy, code, popY) = |
| 1490 |
case (dx, dy, sy) of |
| 1491 |
(true, true, 0) => (~1,fucompp code, false) |
| 1492 |
| (true, _, _) => (sy-1,fucomp sy::code,dy) |
| 1493 |
| (false, _, _) => (sy, fucom sy::code, dy) |
| 1494 |
|
| 1495 |
(* Pop fy if it is dead and hasn't already |
| 1496 |
* been popped. |
| 1497 |
*) |
| 1498 |
val code = if popY then fstp sy::code else code |
| 1499 |
in FINISH code |
| 1500 |
end |
| 1501 |
end |
| 1502 |
|
| 1503 |
in case (lsrc, rsrc) of |
| 1504 |
(I.FPR x, I.FPR y) => reg2(x, y) |
| 1505 |
| (I.FPR x, mem) => genregmemcmp(x, mem) |
| 1506 |
| (mem, I.FPR y) => genmemregcmp(mem, y) |
| 1507 |
| _ => genmemcmp() |
| 1508 |
end |
| 1509 |
|
| 1510 |
|
| 1511 |
fun prCopy(dst, src) = |
| 1512 |
ListPair.app(fn (fd, fs) => |
| 1513 |
pr(fregToString(fd)^"<-"^fregToString fs^" ")) |
| 1514 |
(dst, src) |
| 1515 |
|
| 1516 |
(* Parallel copy magic. |
| 1517 |
* For each src registers, we find out |
| 1518 |
* 1. whether it is the last use, and if so, |
| 1519 |
* 2. whether it is used more than once. |
| 1520 |
* If a source is a last and unique use, then we |
| 1521 |
* can simply rename it to appropriate destination register. |
| 1522 |
*) |
| 1523 |
fun fcopy(I.COPY{dst,src,tmp,...}) = let |
| 1524 |
fun loop([], [], copies, renames) = (copies, renames) |
| 1525 |
| loop(fd::fds, fs::fss, copies, renames) = |
| 1526 |
let val fsx = CB.registerNum fs |
| 1527 |
in if isLastUse fsx then |
| 1528 |
if A.sub(useTbl,fsx) <> stamp |
| 1529 |
(* unused *) |
| 1530 |
then (A.update(useTbl,fsx,stamp); |
| 1531 |
loop(fds, fss, copies, |
| 1532 |
if CB.sameColor(fd,fs) then renames |
| 1533 |
else (fd, fs)::renames) |
| 1534 |
) |
| 1535 |
else loop(fds, fss, (fd, fs)::copies, renames) |
| 1536 |
else loop(fds, fss, (fd, fs)::copies, renames) |
| 1537 |
end |
| 1538 |
| loop _ = error "fcopy.loop" |
| 1539 |
|
| 1540 |
(* generate code for the copies *) |
| 1541 |
fun genCopy([], code) = code |
| 1542 |
| genCopy((fd, fs)::copies, code) = |
| 1543 |
let val ss = ST.fp(stack, CB.registerNum fs) |
| 1544 |
val _ = ST.push(stack, CB.registerNum fd) |
| 1545 |
val code = I.fldl(ST ss)::code |
| 1546 |
in genCopy(copies, code) end |
| 1547 |
|
| 1548 |
(* perform the renaming; it must be done in parallel! *) |
| 1549 |
fun renaming(renames) = |
| 1550 |
let val ss = map (fn (_,fs) => |
| 1551 |
ST.fp(stack,CB.registerNum fs)) renames |
| 1552 |
in ListPair.app (fn ((fd,_),ss) => |
| 1553 |
ST.set(stack,ss,CB.registerNum fd)) |
| 1554 |
(renames, ss) |
| 1555 |
end |
| 1556 |
|
| 1557 |
(* val _ = if debug then |
| 1558 |
(ListPair.app (fn (fd, fs) => |
| 1559 |
pr(fregToString(regmap fd)^"<-"^ |
| 1560 |
fregToString(regmap fs)^" ") |
| 1561 |
) (dst, src); |
| 1562 |
pr "\n") |
| 1563 |
else () *) |
| 1564 |
|
| 1565 |
val (copies, renames) = loop(dst, src, [], []) |
| 1566 |
val code = genCopy(copies, code) |
| 1567 |
in renaming renames; |
| 1568 |
case tmp of |
| 1569 |
SOME(I.FPR f) => |
| 1570 |
(if debug andalso debugDead |
| 1571 |
then pr("KILLING tmp "^fregToString f^"\n") |
| 1572 |
else (); |
| 1573 |
ST.kill(stack, f) |
| 1574 |
) |
| 1575 |
| _ => (); |
| 1576 |
DONE code |
| 1577 |
end |
| 1578 |
| fcopy _ = error "fcopy" |
| 1579 |
|
| 1580 |
fun call(instr, return) = let |
| 1581 |
val code = mark(I.INSTR instr, an)::code |
| 1582 |
val returnSet = SL.return(SL.uniq(getCell return)) |
| 1583 |
in |
| 1584 |
case returnSet of |
| 1585 |
[] => () |
| 1586 |
| [r] => ST.push(stack, CB.registerNum r) |
| 1587 |
| _ => |
| 1588 |
error "can't return more than one fp argument (yet)"; |
| 1589 |
KILL_THE_DEAD(List.filter isDead returnSet, code) |
| 1590 |
end |
| 1591 |
fun x86trans instr = |
| 1592 |
(case instr |
| 1593 |
of I.FMOVE x => (log(); fmove x) |
| 1594 |
| I.FBINOP x => (log(); fbinop x) |
| 1595 |
| I.FIBINOP x => (log(); fibinop x) |
| 1596 |
| I.FUNOP x => (log(); funop x) |
| 1597 |
| I.FILOAD x => (log(); fiload x) |
| 1598 |
| I.FCMP x => (log(); fcmp x) |
| 1599 |
|
| 1600 |
(* handle calling convention *) |
| 1601 |
| I.CALL{return, ...} => (log(); call(instr,return)) |
| 1602 |
|
| 1603 |
(* |
| 1604 |
* Catch instructions that absolutely |
| 1605 |
* should not have been generated at this point. |
| 1606 |
*) |
| 1607 |
| (I.FLD1 | I.FLDL2E | I.FLDLG2 | I.FLDLN2 | I.FLDPI | |
| 1608 |
I.FLDZ | I.FLDL _ | I.FLDS _ | I.FLDT _ | |
| 1609 |
I.FILD _ | I.FILDL _ | I.FILDLL _ | |
| 1610 |
I.FENV _ | I.FBINARY _ | I.FIBINARY _ | I.FUNARY _ | |
| 1611 |
I.FUCOMPP | I.FUCOM _ | I.FUCOMP _ | I.FCOMPP | I.FXCH _ | |
| 1612 |
I.FCOMI _ | I.FCOMIP _ | I.FUCOMI _ | I.FUCOMIP _ | |
| 1613 |
I.FSTPL _ | I.FSTPS _ | I.FSTPT _ | I.FSTL _ | I.FSTS _ |
| 1614 |
) => bug("Illegal FP instructions") |
| 1615 |
|
| 1616 |
(* Other instructions are untouched *) |
| 1617 |
| instr => FINISH(mark(I.INSTR instr, an)::code) |
| 1618 |
(*esac*)) |
| 1619 |
in |
| 1620 |
case instr |
| 1621 |
of I.ANNOTATION{a,i} => |
| 1622 |
trans(stamp, i, a::an, rest, dead, lastUses, code) |
| 1623 |
| I.COPY{k=CB.FP, ...} => (log(); fcopy instr) |
| 1624 |
| I.LIVE _ => DONE(mark(instr, an)::code) |
| 1625 |
| I.INSTR instr => x86trans(instr) |
| 1626 |
| _ => FINISH(mark(instr, an)::code) |
| 1627 |
end (* trans *) |
| 1628 |
|
| 1629 |
(* |
| 1630 |
* Check the translation result to see if it matches the original |
| 1631 |
* code. |
| 1632 |
*) |
| 1633 |
fun checkTranslation(stackIn, stackOut, insns) = |
| 1634 |
let val n = ref(ST.depth stackIn) |
| 1635 |
fun push() = n := !n + 1 |
| 1636 |
fun pop() = n := !n - 1 |
| 1637 |
fun scan(I.INSTR(I.FBINARY{binOp, ...})) = |
| 1638 |
(case binOp of |
| 1639 |
( I.FADDP | I.FSUBP | I.FSUBRP | I.FMULP |
| 1640 |
| I.FDIVP | I.FDIVRP) => pop() |
| 1641 |
| _ => () |
| 1642 |
) |
| 1643 |
| scan(I.INSTR(I.FIBINARY{binOp, ...})) = () |
| 1644 |
| scan(I.INSTR(I.FUNARY I.FPTAN)) = push() |
| 1645 |
| scan(I.INSTR(I.FUNARY _)) = () |
| 1646 |
| scan(I.INSTR(I.FLDL(I.ST n))) = push() |
| 1647 |
| scan(I.INSTR(I.FLDL mem)) = push() |
| 1648 |
| scan(I.INSTR(I.FLDS mem)) = push() |
| 1649 |
| scan(I.INSTR(I.FLDT mem)) = push() |
| 1650 |
| scan(I.INSTR(I.FSTL(I.ST n))) = () |
| 1651 |
| scan(I.INSTR(I.FSTPL(I.ST n))) = pop() |
| 1652 |
| scan(I.INSTR(I.FSTL mem)) = () |
| 1653 |
| scan(I.INSTR(I.FSTS mem)) = () |
| 1654 |
| scan(I.INSTR(I.FSTPL mem)) = pop() |
| 1655 |
| scan(I.INSTR(I.FSTPS mem)) = pop() |
| 1656 |
| scan(I.INSTR(I.FSTPT mem)) = pop() |
| 1657 |
| scan(I.INSTR(I.FXCH{opnd=i,...})) = () |
| 1658 |
| scan(I.INSTR(I.FUCOM _)) = () |
| 1659 |
| scan(I.INSTR(I.FUCOMP _)) = pop() |
| 1660 |
| scan(I.INSTR(I.FUCOMPP)) = (pop(); pop()) |
| 1661 |
| scan(I.INSTR(I.FILD mem)) = push() |
| 1662 |
| scan(I.INSTR(I.FILDL mem)) = push() |
| 1663 |
| scan(I.INSTR(I.FILDLL mem)) = push() |
| 1664 |
| scan(I.INSTR(I.CALL{return, ...})) = |
| 1665 |
(n := 0; (* clear the stack *) |
| 1666 |
(* Simulate the pushing of arguments *) |
| 1667 |
let val returnSet = SL.return(SL.uniq(getCell return)) |
| 1668 |
in app (fn _ => push()) returnSet |
| 1669 |
end |
| 1670 |
) |
| 1671 |
| scan _ = () |
| 1672 |
val _ = app scan (rev insns); |
| 1673 |
val n = !n |
| 1674 |
val m = ST.depth stackOut |
| 1675 |
in |
| 1676 |
if n <> m then |
| 1677 |
(dump(insns); |
| 1678 |
bug("Bad translation n="^i2s n^ " expected="^i2s m^"\n") |
| 1679 |
) |
| 1680 |
else () |
| 1681 |
end |
| 1682 |
|
| 1683 |
|
| 1684 |
(* Dump the initial code *) |
| 1685 |
val _ = if debug andalso !debugOn then |
| 1686 |
(pr("-------- block "^i2s blknum^" ----"^ |
| 1687 |
celllistToString liveIn^" "^ |
| 1688 |
ST.stackToString stackIn^"\n"); |
| 1689 |
dump (!insns); |
| 1690 |
pr("succ="); |
| 1691 |
app (fn b => pr(i2s b^" ")) (#succ cfg blknum); |
| 1692 |
pr("\n") |
| 1693 |
) |
| 1694 |
else () |
| 1695 |
|
| 1696 |
(* Compute the last uses *) |
| 1697 |
val lastUse = computeLastUse(blknum, insns, liveOut) |
| 1698 |
|
| 1699 |
(* Rewrite the code *) |
| 1700 |
val (stamp, insns') = loop(stamp, rev(!insns), lastUse, code) |
| 1701 |
|
| 1702 |
(* Insert shuffle code at the end if necessary *) |
| 1703 |
val insns' = shuffleOut(stack, insns', blknum, block, liveOut) |
| 1704 |
|
| 1705 |
(* Dump translation *) |
| 1706 |
val _ = if debug andalso !debugOn then |
| 1707 |
(pr("-------- translation "^i2s blknum^"----"^ |
| 1708 |
celllistToString liveIn^" "^ |
| 1709 |
ST.stackToString stackIn^"\n"); |
| 1710 |
dump insns'; |
| 1711 |
pr("-------- done "^i2s blknum^"----"^ |
| 1712 |
celllistToString liveOut^" "^ |
| 1713 |
ST.stackToString stack^"\n") |
| 1714 |
) |
| 1715 |
else () |
| 1716 |
|
| 1717 |
(* Check if things are okay *) |
| 1718 |
val _ = if debug andalso sanityCheck then |
| 1719 |
checkTranslation(stackIn, stack, insns') |
| 1720 |
else () |
| 1721 |
|
| 1722 |
in insns := insns'; (* update the instructions *) |
| 1723 |
stamp |
| 1724 |
end (* process *) |
| 1725 |
|
| 1726 |
in (* Translate all blocks *) |
| 1727 |
stamp := C.firstPseudo; |
| 1728 |
#forall_nodes cfg rewriteAllBlocks; |
| 1729 |
(* If we found critical edges, then we have to split them... *) |
| 1730 |
if IntHashTable.numItems edgesToSplit = 0 then Cfg |
| 1731 |
else repairCriticalEdges(Cfg) |
| 1732 |
end |
| 1733 |
end (* functor *) |