| 1 |
local |
| 2 |
structure I = |
| 3 |
struct |
| 4 |
include "alphaInstr.sml" |
| 5 |
end |
| 6 |
in |
| 7 |
|
| 8 |
functor AlphaPeephole |
| 9 |
(structure Instr : ALPHAINSTR |
| 10 |
structure Eval : MLTREE_EVAL |
| 11 |
sharing Instr.T = Eval.T |
| 12 |
) : PEEPHOLE = |
| 13 |
struct |
| 14 |
structure I = Instr |
| 15 |
structure CB = CellsBasis |
| 16 |
|
| 17 |
(* IMPORTANT: instructions are now given in forward order *) |
| 18 |
fun peephole instrs = |
| 19 |
let fun isZero(I.LABop le) = ((Eval.valueOf le = 0) handle _ => false) |
| 20 |
| isZero(I.REGop r) = CB.registerNum r = 31 |
| 21 |
| isZero(I.IMMop i) = i = 0 |
| 22 |
| isZero _ = false |
| 23 |
|
| 24 |
fun removable(I.INSTR(I.LDA{r, b, d})) = |
| 25 |
isZero d andalso CB.sameColor(r,b) |
| 26 |
| removable(I.INSTR(I.OPERATE{oper=(I.ADDQ | I.SUBQ), ra, rb, rc})) = |
| 27 |
CB.sameColor(ra,rc) andalso isZero rb |
| 28 |
| removable(I.ANNOTATION{i,a}) = removable i |
| 29 |
| removable _ = false |
| 30 |
|
| 31 |
fun symmetric(I.STQ, I.LDQ) = true |
| 32 |
| symmetric(I.STL, I.LDL) = true |
| 33 |
| symmetric(I.STW, I.LDW) = true |
| 34 |
| symmetric(I.STB, I.LDB) = true |
| 35 |
| symmetric _ = false |
| 36 |
|
| 37 |
fun sameOperand (I.REGop r1, I.REGop r2) = CB.sameColor(r1,r2) |
| 38 |
| sameOperand (I.IMMop i1, I.IMMop i2) = i1 = i2 |
| 39 |
| sameOperand (I.LABop l1, I.LABop l2) = |
| 40 |
((Eval.valueOf l1 = Eval.valueOf l2) handle _ => false) |
| 41 |
| sameOperand _ = false |
| 42 |
|
| 43 |
fun loop(current, instrs) = |
| 44 |
case current of |
| 45 |
[] => instrs |
| 46 |
| (st as I.INSTR(I.STORE{stOp, r=r1, b=b1, d=d1, ...})):: |
| 47 |
(ld as I.INSTR(I.LOAD {ldOp, r=r2, b=b2, d=d2, ...}))::rest |
| 48 |
where symmetric(stOp, ldOp) andalso |
| 49 |
CB.sameColor(r1,r2) andalso |
| 50 |
CB.sameColor(b1,b2) andalso |
| 51 |
sameOperand(d1,d2) => loop(rest, st::instrs) |
| 52 |
| i::rest where removable i => loop(rest, instrs) |
| 53 |
| i::rest => loop(rest, i::instrs) |
| 54 |
in loop(instrs, []) |
| 55 |
end |
| 56 |
end |
| 57 |
|
| 58 |
end |