Debian Codegraph @

lib/mlrisc-lib/MLRISC/amd64/ra/amd64SpillInstr.sml

raw
1 (* amd64SpillInstr.sml
2 *
3 * This functor contains callback functions for spilling and reloading
4 * instructions.
5 *)
6
7 functor AMD64SpillInstr (
8 structure I : AMD64INSTR
9 structure Props : AMD64INSN_PROPERTIES (* where I = I *)
10 where type I.addressing_mode = I.addressing_mode
11 and type I.ea = I.ea
12 and type I.instr = I.instr
13 and type I.instruction = I.instruction
14 and type I.operand = I.operand
15
16 (* guaranteeing that floats are stored at 16-byte aligned addresses reduces the number of instructions *)
17 val floats16ByteAligned : bool
18 ) : ARCH_SPILL_INSTR =
19 struct
20
21 structure CB = CellsBasis
22 structure I = I
23 structure C = I.C
24
25 fun error msg = MLRiscErrorMsg.impossible("AMD64Spill: "^ msg)
26
27 fun liveKill(add, rmv) ({regs, spilled}, reg) =
28 {regs=rmv(reg, regs), spilled=add(reg, spilled)}
29 val fLiveKill = liveKill (C.addFreg, C.rmvFreg)
30 val rLiveKill = liveKill (C.addReg, C.rmvReg)
31
32 val newReg = C.newReg
33 val newFreg = C.newFreg
34
35 fun annotate (instr,[]) = instr
36 | annotate (instr,a::an) = annotate(I.ANNOTATION{i=instr,a=a},an)
37
38 fun mark (instr, an) = annotate(I.INSTR instr, an)
39
40 fun immed(I.Immed _) = true
41 | immed(I.Immed64 _) = true
42 | immed(I.ImmedLabel _) = true
43 | immed _ = false
44
45 fun immedOrReg(I.Direct r) = true
46 | immedOrReg(I.Immed _) = true
47 | immedOrReg(I.Immed64 _) = true
48 | immedOrReg(I.ImmedLabel _) = true
49 | immedOrReg _ = false
50
51 fun isMemory(I.Displace _) = true
52 | isMemory(I.Indexed _) = true
53 | isMemory(I.LabelEA _) = true
54 | isMemory _ = false
55
56 fun mvInstr instr = let
57 fun mvOp 8 = I.MOVB
58 | mvOp 16 = I.MOVW
59 | mvOp 32 = I.MOVL
60 | mvOp 64 = I.MOVQ
61 | mvOp _ = error "mvInstr"
62 val sz = Props.szOfInstr instr
63 in
64 (sz, mvOp sz)
65 end (* mvInstr *)
66
67 fun fmvInstr instr = let
68 fun mvOp 32 = I.MOVSS
69 | mvOp 64 = I.MOVSD
70 | mvOp _ = error "fmvInstr"
71 val sz = Props.szOfFinstr instr
72 in
73 (sz, mvOp sz)
74 end (* fmvInstr *)
75
76 fun spillToEA CB.GP (r, ea) = let
77 fun move () = {code=[I.move {mvOp=I.MOVQ, dst=ea, src=I.Direct (64, r)}],
78 proh=[], newReg=NONE}
79 in
80 (case ea
81 of ( I.Displace _ | I.Indexed _ ) => move ()
82 | _ => error "spillToEA"
83 (* end case *))
84 end
85 | spillToEA CB.FP _ = error "spillToEA: FP"
86 | spillToEA _ _ = error "spillToEA"
87
88 fun reloadFromEA CB.GP (r, ea) = let
89 fun move () = {code=[I.move {mvOp=I.MOVQ, dst=I.Direct (64, r), src=ea}],
90 proh=[], newReg=NONE}
91 in
92 (case ea
93 of ( I.Displace _ | I.Indexed _ ) => move ()
94 | _ => error "reloadFromEA"
95 (* end case *))
96 end
97 | reloadFromEA CB.FP _ = error "reloadFromEA: FP"
98 | reloadFromEA _ _ = error "reloadFromEA"
99
100 (* spill a general purpose register r at instruction i to spillLoc *)
101 fun spillR (i, r, spillLoc) = let
102 fun spill (instr, an) = let
103 fun done (instr, an) = {code=[mark (instr, an)], proh=[], newReg=NONE}
104 val (defaultSz, defaultMov) = (64, I.MOVQ)
105 (* freshTmp generates a fresh temporary register, an operand with the
106 * instruction's operand size, and an operand with a 64-bit operand size. *)
107 fun freshTmp () = let
108 val tmpR = newReg ()
109 val sz = Props.szOfInstr instr
110 val tmpOpnd = I.Direct (sz, tmpR)
111 val tmpOpnd64 = I.Direct (64, tmpR)
112 in
113 (tmpR, tmpOpnd, tmpOpnd64)
114 end
115 in
116 (case instr
117 of I.CALL {opnd=addr, defs, uses, return, cutsTo, mem, pops} =>
118 done (I.CALL {opnd=addr, defs=C.rmvReg (r, defs),
119 return=return, uses=uses,
120 cutsTo=cutsTo, mem=mem, pops=pops}, an)
121 (* With sign or zero extended spills we use different operand sizes
122 * for copying to the tmp operand and for copying from the tmp operand. *)
123 | I.MOVE {mvOp as (I.MOVZBQ|I.MOVSBQ|I.MOVZWQ|I.MOVSWQ|
124 I.MOVSLQ|I.MOVZBL|I.MOVSBL|I.MOVZWL|
125 I.MOVSWL), src, dst} => let
126 val (tmpR, tmpOpnd, tmpOpnd64) = freshTmp ()
127 in
128 {proh=[tmpR], newReg=SOME tmpR,
129 code=[mark (I.MOVE {mvOp=mvOp, src=src, dst=tmpOpnd}, an),
130 I.move {mvOp=defaultMov, src=tmpOpnd64, dst=spillLoc}]}
131 end
132 (* spill is unnecessary *)
133 | I.MOVE {mvOp, src as I.Direct (_, rs), dst} =>
134 if CB.sameColor (rs, r) then {code=[], proh=[], newReg=NONE}
135 else done (I.MOVE {mvOp=mvOp, src=src, dst=spillLoc}, an)
136 | I.MOVE {mvOp, src, dst=I.Direct _} =>
137 if Props.eqOpn(src, spillLoc)
138 then {code=[], proh=[], newReg=NONE}
139 else if immed src then
140 done(I.MOVE{mvOp=mvOp, src=src, dst=spillLoc}, an)
141 else let
142 val (tmpR, tmpOpnd, tmpOpnd64) = freshTmp ()
143 in
144 {proh=[tmpR],
145 newReg=SOME tmpR,
146 code=[mark(I.MOVE {mvOp=mvOp, src=src, dst=tmpOpnd}, an),
147 I.move {mvOp=defaultMov, src=tmpOpnd64, dst=spillLoc}]}
148 end
149 | I.LEAL {addr, r32} => let
150 val tmpR = newReg()
151 in
152 {proh=[tmpR], newReg=SOME tmpR,
153 code=[mark(I.LEAL {addr=addr, r32=tmpR}, an),
154 I.move{mvOp=I.MOVL, src=I.Direct (32, tmpR),
155 dst=spillLoc}]}
156 end
157 | I.LEAQ {addr, r64} => let
158 val tmpR = newReg()
159 in
160 {proh=[tmpR], newReg=SOME tmpR,
161 code=[mark(I.LEAQ{addr=addr, r64=tmpR}, an),
162 I.move{mvOp=I.MOVQ, src=I.Direct (64,tmpR),
163 dst=spillLoc}]}
164 end
165 (* handle xorl and xorq with the special case when both operands are the
166 * same register *)
167 | I.BINARY {binOp=I.XORL, src as I.Direct (_,rs),
168 dst=I.Direct (_,rd)} =>
169 if CB.sameColor (rs,rd)
170 then {proh=[], newReg=NONE,
171 code=[mark(I.MOVE{mvOp=I.MOVL, src=I.Immed 0,
172 dst=spillLoc}, an)]}
173 else
174 {proh=[], newReg=NONE,
175 code=[mark(I.BINARY{binOp=I.XORL, src=src,
176 dst=spillLoc}, an)]}
177 | I.BINARY{binOp=I.XORQ, src as I.Direct (_,rs),
178 dst=I.Direct (_,rd)} =>
179 if CB.sameColor(rs,rd)
180 then {proh=[], newReg=NONE,
181 code=[mark(I.MOVE{mvOp=I.MOVQ, src=I.Immed 0,
182 dst=spillLoc}, an)]}
183 else {proh=[], newReg=NONE,
184 code=[mark(I.BINARY{binOp=I.XORQ, src=src,
185 dst=spillLoc}, an)]}
186
187 | I.BINARY {binOp, src, dst} => let
188 (* note: dst = r *)
189 fun multBinOp (I.IMULQ|I.IMULL|I.IMULW|I.IMULB) = true
190 | multBinOp _ = false
191 in
192 if multBinOp binOp
193 then let (* destination must remain a register *)
194 val (tmpR, tmpOpnd, tmpOpnd64) = freshTmp ()
195 in
196 {proh=[tmpR], newReg=SOME tmpR,
197 code=[I.move{mvOp=defaultMov, src=spillLoc, dst=tmpOpnd64},
198 I.binary{binOp=binOp, src=src, dst=tmpOpnd},
199 I.move{mvOp=defaultMov, src=tmpOpnd64, dst=spillLoc}]}
200 end
201 else if immedOrReg src
202 then (* can replace the destination directly *)
203 done(I.BINARY{binOp=binOp, src=src, dst=spillLoc},
204 an)
205 else let (* a memory src and non multBinOp
206 * --- cannot have two memory operands
207 *)
208 val (tmpR, tmpOpnd, tmpOpnd64) = freshTmp ()
209 in
210 { proh=[tmpR], newReg=NONE,
211 code=[I.move{mvOp=defaultMov, src=src, dst=tmpOpnd64},
212 I.binary{binOp=binOp, src=tmpOpnd,
213 dst=spillLoc}]}
214 end
215 end
216 | I.SHIFT {shiftOp, count=I.Direct (_, ecx), src, dst} =>
217 error "implement shift"
218 | I.SHIFT {shiftOp, count, src, dst} =>
219 if immedOrReg src
220 then done (I.SHIFT {shiftOp=shiftOp, src=src, dst=spillLoc,
221 count=count}, an)
222 else let
223 val (tmpR, tmpOpnd, tmpOpnd64) = freshTmp ()
224 in
225 {proh=[tmpR], newReg=SOME tmpR,
226 code=[I.move {mvOp=defaultMov, src=src, dst=tmpOpnd64},
227 I.shift {shiftOp=shiftOp, src=tmpOpnd, dst=spillLoc,
228 count=count}]}
229 end
230 | I.CMOV {cond, src, dst} =>
231 (* note: dst must be a register *)
232 (case spillLoc
233 of I.Direct (_,r) =>
234 {proh=[], newReg=NONE,
235 code=[mark(I.CMOV{cond=cond,src=src,dst=r},an)]
236 }
237 | _ => let
238 val (tmpR, _, tmpOpnd64) = freshTmp ()
239 in
240 {proh=[tmpR], newReg=SOME tmpR,
241 code=[I.move{mvOp=I.MOVQ, src=spillLoc, dst=tmpOpnd64},
242 mark(I.CMOV{cond=cond,src=src,dst=tmpR},an),
243 I.move{mvOp=I.MOVQ, src=tmpOpnd64, dst=spillLoc}]}
244 end
245 (* end case *))
246 | I.XADD{lock,sz=isz,src as I.Direct (_, srcR),dst} =>
247 if CB.sameColor (srcR, r)
248 then let val (tmpR, tmpOpnd, tmpOpnd64) = freshTmp()
249 in {proh=[tmpR],
250 code=[I.move{mvOp=I.MOVQ, src=src, dst=tmpOpnd64},
251 mark(I.XADD{lock=lock,sz=isz,src=tmpOpnd,dst=dst},an),
252 I.move{mvOp=I.MOVQ, src=tmpOpnd64, dst=spillLoc}
253 ],
254 newReg=SOME tmpR
255 }
256 end
257 else let val (tmpR, tmpOpnd, tmpOpnd64) = freshTmp()
258 in {proh=[tmpR],
259 code=[I.move{mvOp=I.MOVQ, src=dst, dst=tmpOpnd64},
260 mark(I.XADD{lock=lock,sz=isz,src=src,dst=tmpOpnd},an),
261 I.move{mvOp=I.MOVQ, src=tmpOpnd64, dst=spillLoc}
262 ],
263 newReg=SOME tmpR
264 }
265 end
266 | I.CMPXCHG{lock,sz=isz,src,dst} =>
267 if immedOrReg src then
268 {proh=[],
269 code=[mark(I.CMPXCHG{lock=lock,sz=isz,src=src,dst=spillLoc},an)],
270 newReg=NONE
271 }
272 else
273 let val (tmpR, tmpOpnd, tmpOpnd64) = freshTmp()
274 in {proh=[tmpR],
275 code=[I.move{mvOp=I.MOVQ, src=src, dst=tmpOpnd64},
276 mark(I.CMPXCHG{lock=lock,sz=isz,src=tmpOpnd,dst=spillLoc},an)],
277 newReg=SOME tmpR
278 }
279 end
280 | I.XCHG{lock,sz=isz,src,dst} =>
281 if immedOrReg src then
282 {proh=[],
283 code=[mark(I.XCHG{lock=lock,sz=isz,src=src,dst=spillLoc},an)],
284 newReg=NONE
285 }
286 else
287 let val (tmpR, tmpOpnd, tmpOpnd64) = freshTmp()
288 in {proh=[tmpR],
289 code=[I.move{mvOp=I.MOVQ, src=src, dst=tmpOpnd64},
290 mark(I.XCHG{lock=lock,sz=isz,src=tmpOpnd,dst=spillLoc},an)],
291 newReg=SOME tmpR
292 }
293 end
294 | I.RDTSC => error "spill: RDTSC"
295 | I.RDTSCP => error "spill: RDTSCP"
296 | I.MULTDIV _ => error "spill: MULTDIV"
297 | I.MUL3 {src1, src2, dst} => let
298 val tmpR = newReg()
299 in
300 {proh=[tmpR], newReg=SOME tmpR,
301 code=[mark(I.MUL3{src1=src1, src2=src2, dst=tmpR}, an),
302 I.move{mvOp=I.MOVL, src=I.Direct (32,tmpR),
303 dst=spillLoc}]
304 }
305 end
306 | I.MULQ3 {src1, src2, dst} => let
307 val tmpR = newReg()
308 in
309 {proh=[tmpR], newReg=SOME tmpR,
310 code=[mark(I.MULQ3{src1=src1, src2=src2, dst=tmpR}, an),
311 I.move{mvOp=I.MOVQ, src=I.Direct (64,tmpR),
312 dst=spillLoc}]}
313 end
314 | I.UNARY{unOp, opnd} =>
315 done(I.UNARY{unOp=unOp, opnd=spillLoc}, an)
316 | I.SET{cond, opnd} =>
317 done(I.SET{cond=cond, opnd=spillLoc}, an)
318 | I.POP _ => done (I.POP spillLoc, an)
319 | _ => error "AMD64SpillInstr.spillR"
320 (* end case *))
321 end (* spill *)
322 fun f (i, ans) = (case i
323 of I.INSTR instr => spill (instr, ans)
324 | I.ANNOTATION {i, a} => f (i, a :: ans)
325 | I.KILL lk => {code=[annotate (I.KILL (rLiveKill (lk, r)), ans)],
326 proh=[], newReg=NONE}
327 | _ => error "AMD64SpillInstr.spillR"
328 (* end case *))
329 in
330 f (i, [])
331 end (* spillR *)
332
333 fun spillF (i, r, spillLoc) = let
334 fun spill (instr, an) = let
335 val (sz, fmvOp) = fmvInstr instr
336 fun withTmp f = let
337 val tmpR = newFreg ()
338 val tmp = I.FDirect tmpR
339 in
340 {code=[mark (f tmpR, an),
341 I.fmove {fmvOp=fmvOp, src=tmp, dst=spillLoc}],
342 proh=[tmpR], newReg=SOME tmpR}
343 end (* withTmp *)
344 fun binOpWithTmp f = let
345 val tmpR = newFreg ()
346 val tmp = I.FDirect tmpR
347 in
348 {code=[I.fmove {fmvOp=fmvOp, src=spillLoc, dst=tmp},
349 mark (f tmpR, an),
350 I.fmove {fmvOp=fmvOp, src=tmp, dst=spillLoc}],
351 proh=[tmpR], newReg=SOME tmpR}
352 end (* binOpWithTmp *)
353 in
354 (case instr
355 of I.FMOVE {fmvOp, src=src as I.FDirect r', dst} =>
356 if CB.sameColor (r, r')
357 then {code=[], proh=[], newReg=NONE}
358 else {code=[mark (I.FMOVE {fmvOp=fmvOp, src=src,
359 dst=spillLoc}, an)],
360 proh=[], newReg=NONE}
361 | I.FMOVE {fmvOp, src, dst as I.FDirect _} => withTmp (fn tmpR =>
362 I.FMOVE {fmvOp=fmvOp, src=src, dst=I.FDirect tmpR})
363 | I.FBINOP {binOp, src, dst} => binOpWithTmp (fn tmpR =>
364 I.FBINOP {binOp=binOp, src=src, dst=tmpR})
365 | I.FSQRTS {dst, src} => withTmp (fn tmpR =>
366 I.FSQRTS {src=src, dst=I.FDirect tmpR})
367 | I.FSQRTD {dst, src} => withTmp (fn tmpR =>
368 I.FSQRTD {src=src, dst=I.FDirect tmpR})
369 | I.CALL {opnd, defs, uses, return, cutsTo, mem, pops} =>
370 {code=[mark (I.CALL {opnd=opnd, defs=C.rmvFreg (r, defs),
371 return=return, uses=uses,
372 cutsTo=cutsTo, mem=mem, pops=pops}, an)],
373 proh=[], newReg=NONE}
374 | _ => error "spillF"
375 (* end case *))
376 end (* spill *)
377 fun f (i, ans) = (case i
378 of I.INSTR instr => spill (instr, ans)
379 | I.ANNOTATION {i, a} => f (i, a :: ans)
380 | I.KILL lk => {code=[annotate (I.KILL (fLiveKill (lk, r)), ans)],
381 proh=[], newReg=NONE}
382 | _ => error "spillF"
383 (* end case *))
384 in
385 f (i, [])
386 end (* spillF *)
387
388 fun spill CB.GP = spillR
389 | spill CB.FP = spillF
390 | spill _ = error "spill"
391
392 val comment : string -> Annotations.annotation = (#create MLRiscAnnotations.COMMENT)
393
394 (* reload a general purpose register r at instruction i from spillLoc *)
395 fun reloadR (i, r, spillLoc) = let
396 fun reload (instr, an) = let
397 fun done (instr, an) = {code=[mark (instr, an)], proh=[], newReg=NONE}
398 fun replace (opnd as I.Direct (sz, r')) = if CB.sameColor (r, r')
399 then (case spillLoc
400 of I.Direct (_, r) =>
401 (* I believe that this case occurrs when the mlrisc register allocator
402 * is doing something clever, as the spill location is clearly not a stack
403 * location. in any case, the size associated with the register r is bogus,
404 * which is why we use the size of the register being spilled. *)
405 I.Direct (sz, r)
406 | _ => spillLoc)
407 else opnd
408 | replace opnd = opnd
409 fun operand (opnd, tmp) = let
410 fun replaceR (r', f) = if CB.sameColor (r, r')
411 then f tmp
412 else opnd
413 in
414 (case opnd
415 of I.Direct (opndSz, opndR) => replaceR (opndR, fn r' =>
416 I.Direct (opndSz, r'))
417 | I.Displace {base, disp, mem} => replaceR (base, fn r' =>
418 I.Displace {base=r', disp=disp, mem=mem})
419 | I.Indexed {base=NONE, index, scale, disp, mem} =>
420 replaceR (index, fn index' =>
421 I.Indexed {base=NONE, index=index', scale=scale,
422 disp=disp, mem=mem})
423 | I.Indexed {base=SOME b, index, scale, disp, mem} =>
424 if CB.sameColor (b, r)
425 then operand (I.Indexed {base=SOME tmp, index=index, scale=scale, disp=disp, mem=mem}, tmp)
426 else if CB.sameColor (index, r)
427 then I.Indexed {base=SOME b, index=tmp, scale=scale, disp=disp, mem=mem}
428 else opnd
429 | opnd => opnd
430 (* end case *))
431 end (* operand *)
432 fun operand' (I.Direct _, _) = spillLoc
433 | operand' (opnd, tmp) = operand (opnd, tmp)
434 (* assume that tmpR gets killed *)
435 fun withTmp' avail f = (case spillLoc
436 of I.Direct (_, tmpR) => if avail
437 then {code=[mark (f tmpR, an)], proh=[tmpR], newReg=SOME tmpR}
438 else {code=[mark (f tmpR, an)], proh=[], newReg=NONE}
439 | _ => let
440 val tmpR = newReg ()
441 in
442 {code=[I.move {mvOp=I.MOVQ, src=spillLoc,
443 dst=I.Direct (64, tmpR)},
444 mark (f tmpR, an)],
445 proh=[tmpR], newReg=SOME tmpR}
446 end
447 (* end case *))
448 val withTmp = withTmp' false
449 val withTmpAvail = withTmp' true
450 fun reloadCmp (cmp, lsrc, rsrc, an) = let
451 fun reload () = withTmp (fn tmp =>
452 cmp {lsrc=operand (lsrc, tmp), rsrc=operand (rsrc, tmp)})
453 in
454 if immedOrReg lsrc andalso immedOrReg rsrc
455 then let
456 val rsrc' = replace rsrc
457 val lsrc' = replace lsrc
458 in
459 if isMemory rsrc' andalso isMemory lsrc'
460 then reload ()
461 else done (cmp {lsrc=lsrc', rsrc=rsrc'}, an)
462 end
463 else reload ()
464 end (* reloadCmp *)
465 fun reloadTest (test, lsrc, rsrc, an) = let
466 fun reload () = withTmp (fn tmp =>
467 test {lsrc=operand (lsrc, tmp), rsrc=operand (rsrc, tmp)})
468 in
469 if immedOrReg lsrc andalso immedOrReg rsrc
470 then let
471 val rsrc' = replace rsrc
472 val lsrc' = replace lsrc
473 in
474 if isMemory rsrc'
475 then if isMemory lsrc'
476 then reload ()
477 else done (test {lsrc=rsrc', rsrc=lsrc'}, an)
478 else done (test {lsrc=lsrc', rsrc=rsrc'}, an)
479 end
480 else reload ()
481 end (* reloadTest *)
482 fun reloadBT (bitOp, lsrc, rsrc, an) =
483 reloadCmp (fn {lsrc,rsrc} =>
484 I.BITOP {bitOp=bitOp,lsrc=lsrc,rsrc=rsrc}, lsrc, rsrc, an)
485 fun reloadPush(push, arg as I.Direct _, an) =
486 done(push(replace arg), an)
487 | reloadPush(push, arg, an) =
488 withTmpAvail(fn tmpR => push (operand(arg, tmpR)))
489 in
490 (case instr
491 of I.JMP (opnd, labs) => withTmp (fn tmp =>
492 I.JMP (operand' (opnd, tmp), labs))
493 | I.JCC {opnd, cond} => withTmp (fn tmp =>
494 I.JCC {opnd=operand' (opnd, tmp), cond=cond})
495 | I.CALL {opnd, defs, uses, return, cutsTo, mem, pops} =>
496 withTmp (fn tmp =>
497 I.CALL {opnd=operand (opnd, tmp), defs=defs,
498 uses=C.rmvReg (r, uses), return=return, pops=pops,
499 cutsTo=cutsTo, mem=mem})
500 | I.MOVE {mvOp, src as I.Direct _, dst as I.Direct _} =>
501 done (I.MOVE {mvOp=mvOp, src=replace src, dst=dst}, an)
502 | I.MOVE {mvOp, src, dst as I.Direct _} => withTmpAvail (fn tmp =>
503 I.MOVE {mvOp=mvOp, src=operand (src, tmp), dst=dst})
504 | I.MOVE {mvOp, src as I.Direct _, dst} =>
505 if (Props.eqOpn (dst, spillLoc))
506 then {code=[], proh=[], newReg=NONE}
507 else withTmpAvail (fn tmp =>
508 I.MOVE {mvOp=mvOp, src=operand (src, tmp),
509 dst=operand (dst, tmp)})
510 | I.MOVE {mvOp, src, dst} => withTmpAvail (fn tmp =>
511 I.MOVE {mvOp=mvOp, src=operand (src, tmp),
512 dst=operand (dst, tmp)})
513 | I.LEAL {r32, addr} => withTmpAvail (fn tmp =>
514 I.LEAL {r32=r32, addr=operand (addr, tmp)})
515 | I.LEAQ {r64, addr} => withTmpAvail (fn tmp =>
516 I.LEAQ {r64=r64, addr=operand (addr, tmp)})
517 | I.CMPQ {lsrc, rsrc} => reloadCmp (I.CMPQ, lsrc, rsrc, an)
518 | I.CMPL {lsrc, rsrc} => reloadCmp (I.CMPL, lsrc, rsrc, an)
519 | I.CMPW {lsrc, rsrc} => reloadCmp (I.CMPW, lsrc, rsrc, an)
520 | I.CMPB {lsrc, rsrc} => reloadCmp (I.CMPB, lsrc, rsrc, an)
521 | I.TESTQ {lsrc, rsrc} => reloadTest (I.TESTQ, lsrc, rsrc, an)
522 | I.TESTL {lsrc, rsrc} => reloadTest (I.TESTL, lsrc, rsrc, an)
523 | I.TESTW {lsrc, rsrc} => reloadTest (I.TESTW, lsrc, rsrc, an)
524 | I.TESTB {lsrc, rsrc} => reloadTest (I.TESTB, lsrc, rsrc, an)
525 | I.BITOP{bitOp,lsrc, rsrc} => reloadBT (bitOp, lsrc, rsrc, an)
526 | I.BINARY{binOp, src, dst} => withTmp (fn tmp =>
527 I.BINARY{binOp=binOp, src=operand(src, tmp),
528 dst=operand(dst, tmp)})
529 | I.CMOV{cond, src, dst} =>
530 if CB.sameColor(dst,r)
531 then error "CMOV"
532 else done (I.CMOV{cond=cond, src=spillLoc, dst=dst}, an)
533 | I.SHIFT {shiftOp, count as I.Direct _, src, dst} =>
534 withTmp (fn tmp =>
535 I.SHIFT {shiftOp=shiftOp, count=count,
536 src=operand (src, tmp),
537 dst=operand (dst, tmp)})
538 | I.CMPXCHG{lock,sz,src,dst} => withTmp(fn tmpR =>
539 I.CMPXCHG{lock=lock, sz=sz,
540 src=operand(src, tmpR),
541 dst=operand(dst, tmpR)})
542 | I.XCHG{lock,sz,src,dst} => withTmp(fn tmpR =>
543 I.XCHG{lock=lock, sz=sz,
544 src=operand(src, tmpR),
545 dst=operand(dst, tmpR)})
546 | I.XADD{lock,sz,src,dst} => withTmp(fn tmpR =>
547 I.XADD {lock=lock,sz=sz,
548 src=operand(src, tmpR),
549 dst=operand(dst, tmpR)})
550 | I.MULTDIV{multDivOp, src as I.Direct _} =>
551 done (I.MULTDIV{multDivOp=multDivOp, src=replace src}, an)
552 | I.MULTDIV{multDivOp, src} => withTmp(fn tmpR =>
553 I.MULTDIV{multDivOp=multDivOp, src=operand(src, tmpR)})
554 | I.MUL3{src1, src2, dst} => withTmp (fn tmpR =>
555 I.MUL3{src1=operand(src1, tmpR), src2=src2,
556 dst=if CB.sameColor(dst,r)
557 then error "reload:MUL3" else dst})
558 | I.MULQ3{src1, src2, dst} => withTmp (fn tmpR =>
559 I.MULQ3{src1=operand(src1, tmpR), src2=src2,
560 dst=if CB.sameColor(dst,r)
561 then error "reload:MULQ3" else dst})
562 | I.UNARY{unOp, opnd} => withTmpAvail (fn tmpR =>
563 I.UNARY{unOp=unOp, opnd=operand(opnd, tmpR)})
564 | I.SET{cond, opnd} => withTmpAvail (fn tmpR =>
565 I.SET{cond=cond, opnd=operand(opnd, tmpR)})
566 | I.PUSH arg => reloadPush (I.PUSH, arg, an)
567 | I.FMOVE {fmvOp, src, dst} => withTmpAvail (fn tmp =>
568 I.FMOVE {fmvOp=fmvOp, src=operand (src, tmp),
569 dst=operand (dst, tmp)})
570 | I.FCOM {comOp, dst, src} => withTmpAvail (fn tmp =>
571 I.FCOM {comOp=comOp, dst=dst, src=operand (src, tmp)})
572 | I.FBINOP {binOp, dst, src} => withTmpAvail (fn tmp =>
573 I.FBINOP {binOp=binOp, dst=dst, src=operand (src, tmp)})
574 | I.RDTSC => error "reload: RDTSC"
575 | I.RDTSCP => error "reload: RDTSCP"
576 | _ => error "reloadR"
577 (* end case *))
578 end (* reload *)
579 fun f (i, ans) = (case i
580 of I.INSTR instr => reload (instr, ans)
581 | I.ANNOTATION {a, i} => f (i, a :: ans)
582 | I.LIVE lk => {code=[annotate (I.LIVE (rLiveKill (lk, r)), ans)],
583 proh=[], newReg=NONE}
584 | _ => error "reloadR"
585 (* end case *))
586 in
587 f (i, [])
588 end (* reloadR *)
589
590 fun reloadF (i, r, spillLoc) = let
591 fun reload (instr, an) = let
592 fun replace (opnd as I.FDirect r') =
593 if CB.sameColor (r, r') then spillLoc else opnd
594 | replace opnd = opnd
595 val (sz, fmvOp) = fmvInstr instr
596 in
597 (case instr
598 of I.FMOVE {fmvOp, src, dst=dst as I.FDirect _} =>
599 {code=[mark (I.FMOVE {fmvOp=fmvOp, src=replace src, dst=dst},
600 an)],
601 proh=[], newReg=NONE}
602 | I.FMOVE {fmvOp, src, dst} => let
603 val tmpR = newFreg ()
604 val tmp = I.FDirect tmpR
605 in
606 {code=[I.fmove {fmvOp=fmvOp, src=spillLoc, dst=tmp},
607 mark (I.FMOVE {fmvOp=fmvOp, src=tmp, dst=dst}, an)],
608 proh=[tmpR], newReg=SOME tmpR}
609 end
610 (* treat bitwise operators differently, as they require that their source operand is 16-byte aligned *)
611 | I.FBINOP {binOp=binOp as (I.XORPS | I.XORPD | I.ANDPS | I.ANDPD | I.ORPS | I.ORPD), src, dst} => if CB.sameColor (r, dst)
612 then {code=[I.fmove {fmvOp=fmvOp, src=spillLoc, dst=I.FDirect dst},
613 mark (I.FBINOP {binOp=binOp, src=src, dst=dst}, an)],
614 proh=[], newReg=NONE}
615 else if floats16ByteAligned
616 then let
617 val tmpR = newFreg ()
618 val tmp = I.FDirect tmpR
619 in
620 {code=[I.fmove {fmvOp=fmvOp, src=spillLoc, dst=tmp},
621 mark (I.FBINOP {binOp=binOp, src=tmp, dst=dst}, an)],
622 proh=[tmpR], newReg=SOME tmpR}
623 end
624 else {code=[mark (I.FBINOP {binOp=binOp, src=replace src, dst=dst}, an)], proh=[], newReg=NONE}
625 | I.FBINOP {binOp, src, dst} => if CB.sameColor (r, dst)
626 then {code=[I.fmove {fmvOp=fmvOp, src=spillLoc, dst=I.FDirect dst},
627 mark (I.FBINOP {binOp=binOp, src=src, dst=dst}, an)],
628 proh=[], newReg=NONE}
629 else {code=[mark (I.FBINOP {binOp=binOp, src=replace src, dst=dst}, an)], proh=[], newReg=NONE}
630 | I.FCOM {comOp, src, dst} => let
631 val tmpR = newFreg ()
632 val tmp = I.FDirect tmpR
633 val (src, dst) = if CB.sameColor (dst, r)
634 then (src, tmpR)
635 else (tmp, dst)
636 in
637 {code=[I.fmove {fmvOp=fmvOp, src=spillLoc, dst=tmp},
638 mark (I.FCOM {comOp=comOp, src=src, dst=dst}, an)],
639 proh=[tmpR], newReg=SOME tmpR}
640 end
641 | I.FSQRTS {dst, src} => if floats16ByteAligned
642 then let
643 val tmpR = newFreg ()
644 val tmp = I.FDirect tmpR
645 in
646 {code=[I.fmove {fmvOp=fmvOp, src=spillLoc, dst=tmp},
647 mark (I.FSQRTS {src=tmp, dst=dst}, an)],
648 proh=[tmpR], newReg=SOME tmpR}
649 end
650 else {code=[mark (I.FSQRTS {src=spillLoc, dst=dst}, an)], proh=[], newReg=NONE}
651 | I.FSQRTD {dst, src} => if floats16ByteAligned
652 then let
653 val tmpR = newFreg ()
654 val tmp = I.FDirect tmpR
655 in
656 {code=[I.fmove {fmvOp=fmvOp, src=spillLoc, dst=tmp},
657 mark (I.FSQRTD {src=tmp, dst=dst}, an)],
658 proh=[tmpR], newReg=SOME tmpR}
659 end
660 else {code=[mark (I.FSQRTD {src=spillLoc, dst=dst}, an)], proh=[], newReg=NONE}
661 | I.CALL {opnd, defs, uses, return, cutsTo, mem, pops} =>
662 {code=[mark (I.CALL {opnd=opnd, defs=C.rmvReg (r, defs),
663 uses=uses, return=return, cutsTo=cutsTo,
664 mem=mem, pops=pops}, an)],
665 proh=[], newReg=NONE}
666 | _ => error "reloadF"
667 (* end case *))
668 end (* reload *)
669 fun f (i, ans) = (case i
670 of I.INSTR i => reload (i, ans)
671 | I.ANNOTATION {i, a} => f (i, a :: ans)
672 | I.LIVE lk =>
673 {code=[annotate (I.LIVE (fLiveKill (lk, r)), ans)],
674 proh=[], newReg=NONE}
675 | _ => error "reloadF.f"
676 (* end case *))
677 in
678 f (i, [])
679 end (* reloadF *)
680
681 fun reload CB.GP = reloadR
682 | reload CB.FP = reloadF
683 | reload _ = error "reload"
684
685 end (* AMD64SpillInstr *)