| 1 |
(* |
| 2 |
* Note, this file contains conditional pattern matching rules. |
| 3 |
* You'll have to run it thru the tool wheregen |
| 4 |
* (source for this is in the directory Tools/WhereGen) |
| 5 |
* to generate the output. |
| 6 |
* |
| 7 |
* -- Allen |
| 8 |
*) |
| 9 |
|
| 10 |
local |
| 11 |
|
| 12 |
|
| 13 |
structure I = |
| 14 |
struct |
| 15 |
include "x86Instr.sml" (* import instruction definitions *) |
| 16 |
end |
| 17 |
|
| 18 |
in |
| 19 |
|
| 20 |
functor X86Peephole |
| 21 |
(structure Instr : X86INSTR |
| 22 |
structure Eval : MLTREE_EVAL |
| 23 |
(* sharing Instr.T = Eval.T *) |
| 24 |
where type T.Basis.cond = Instr.T.Basis.cond |
| 25 |
and type T.Basis.div_rounding_mode = Instr.T.Basis.div_rounding_mode |
| 26 |
and type T.Basis.ext = Instr.T.Basis.ext |
| 27 |
and type T.Basis.fcond = Instr.T.Basis.fcond |
| 28 |
and type T.Basis.rounding_mode = Instr.T.Basis.rounding_mode |
| 29 |
and type T.Constant.const = Instr.T.Constant.const |
| 30 |
and type ('s,'r,'f,'c) T.Extension.ccx = ('s,'r,'f,'c) Instr.T.Extension.ccx |
| 31 |
and type ('s,'r,'f,'c) T.Extension.fx = ('s,'r,'f,'c) Instr.T.Extension.fx |
| 32 |
and type ('s,'r,'f,'c) T.Extension.rx = ('s,'r,'f,'c) Instr.T.Extension.rx |
| 33 |
and type ('s,'r,'f,'c) T.Extension.sx = ('s,'r,'f,'c) Instr.T.Extension.sx |
| 34 |
and type T.I.div_rounding_mode = Instr.T.I.div_rounding_mode |
| 35 |
and type T.Region.region = Instr.T.Region.region |
| 36 |
and type T.ccexp = Instr.T.ccexp |
| 37 |
and type T.fexp = Instr.T.fexp |
| 38 |
(* and type T.labexp = Instr.T.labexp *) |
| 39 |
and type T.mlrisc = Instr.T.mlrisc |
| 40 |
and type T.oper = Instr.T.oper |
| 41 |
and type T.rep = Instr.T.rep |
| 42 |
and type T.rexp = Instr.T.rexp |
| 43 |
and type T.stm = Instr.T.stm |
| 44 |
) : PEEPHOLE = |
| 45 |
struct |
| 46 |
structure I = Instr |
| 47 |
structure C = I.C |
| 48 |
structure CBase = CellsBasis |
| 49 |
|
| 50 |
(* IMPORTANT: instructions are given in forward order *) |
| 51 |
fun peephole instrs = |
| 52 |
let fun isStackPtr(I.Direct r) = CBase.sameColor(r, C.esp) |
| 53 |
| isStackPtr _ = false |
| 54 |
|
| 55 |
fun isZeroLE le = (Eval.valueOf le = 0) handle _ => false |
| 56 |
|
| 57 |
fun isZero(I.Immed n) = n = 0 |
| 58 |
| isZero(I.ImmedLabel le) = isZeroLE le |
| 59 |
| isZero _ = false |
| 60 |
|
| 61 |
fun isZeroOpt NONE = true |
| 62 |
| isZeroOpt (SOME opn) = isZero opn |
| 63 |
|
| 64 |
fun loop(code, instrs) = |
| 65 |
(case code of |
| 66 |
[] => instrs |
| 67 |
|
| 68 |
(* x <- x +/- 0; |
| 69 |
*) |
| 70 |
| I.INSTR(I.BINARY{binOp=(I.ADDL | I.SUBL), |
| 71 |
src=I.ImmedLabel le, ...})::rest |
| 72 |
where isZeroLE le => loop(rest, instrs) |
| 73 |
|
| 74 |
(* remove lea 0(r), r *) |
| 75 |
| I.INSTR(I.LEA{r32, |
| 76 |
addr=I.Displace{base, disp=I.ImmedLabel le,...}})::rest |
| 77 |
where (isZeroLE le) andalso |
| 78 |
CBase.sameColor(r32,base) => loop(rest, instrs) |
| 79 |
|
| 80 |
(* addl n, %esp; subl m, %esp |
| 81 |
* => addl (n-m), %esp ;; when m < n |
| 82 |
* => - ;; when m = n |
| 83 |
* => subl (m-n), %esp ;; when m > n |
| 84 |
*) |
| 85 |
| I.INSTR(I.BINARY{binOp=I.ADDL, src=I.Immed n, dst=I.Direct d_i}):: |
| 86 |
I.INSTR(I.BINARY{binOp=I.SUBL, src=I.Immed m, dst=I.Direct d_j}):: |
| 87 |
rest |
| 88 |
where CBase.sameColor(d_i, C.esp) andalso |
| 89 |
CBase.sameColor(d_j, C.esp) => |
| 90 |
if (m = n) then loop (rest, instrs) |
| 91 |
else if (m < n) then |
| 92 |
loop(rest, |
| 93 |
I.binary{binOp=I.ADDL, src=I.Immed(n-m), |
| 94 |
dst=I.Direct(C.esp)}::instrs) |
| 95 |
else |
| 96 |
loop(rest, |
| 97 |
I.binary{binOp=I.SUBL, src=I.Immed(m-n), |
| 98 |
dst=I.Direct(C.esp)}::instrs) |
| 99 |
|
| 100 |
(* push folding: |
| 101 |
* subl 4, %esp |
| 102 |
* movl src, 0(%esp) (where src <> %esp !!! ) |
| 103 |
* => |
| 104 |
* pushl src |
| 105 |
*) |
| 106 |
| I.INSTR(I.BINARY{binOp=I.SUBL,src=I.Immed 4,dst=I.Direct dst_i}):: |
| 107 |
I.INSTR(I.MOVE{mvOp=I.MOVL,src, |
| 108 |
dst=I.Displace{base,disp=I.Immed 0,...}}) |
| 109 |
::rest |
| 110 |
where CBase.sameColor(base, C.esp) andalso |
| 111 |
CBase.sameColor(dst_i, C.esp) andalso |
| 112 |
not(isStackPtr src) => |
| 113 |
loop(rest, I.pushl src::instrs) |
| 114 |
|
| 115 |
(* pop folding: |
| 116 |
* movl 0(%esp), dst (where dst <> %esp!!!!) |
| 117 |
* addl 4, %esp |
| 118 |
* => |
| 119 |
* popl dst |
| 120 |
*) |
| 121 |
| I.INSTR(I.MOVE{mvOp=I.MOVL, |
| 122 |
src=I.Displace{base, disp=I.Immed 0, ...}, dst}):: |
| 123 |
I.INSTR(I.BINARY{binOp=I.ADDL, src=I.Immed 4, |
| 124 |
dst=I.Direct dst_i}):: |
| 125 |
rest |
| 126 |
where CBase.sameColor(base, C.esp) andalso |
| 127 |
CBase.sameColor(dst_i,C.esp) andalso |
| 128 |
not(isStackPtr dst) => |
| 129 |
loop(rest, I.pop dst::instrs) |
| 130 |
|
| 131 |
| I.INSTR(I.MOVE{mvOp=I.MOVL, src, dst as I.Direct _})::rest |
| 132 |
where isZero src => |
| 133 |
loop(rest, I.binary{binOp=I.XORL, src=dst, dst=dst}::instrs) |
| 134 |
|
| 135 |
| i::rest => loop(rest, i::instrs) |
| 136 |
) |
| 137 |
in loop(instrs, []) |
| 138 |
end |
| 139 |
end |
| 140 |
|
| 141 |
end |