| 1 |
(* |
| 2 |
* Performs simple local optimizations. |
| 3 |
* This version uses IntInf |
| 4 |
*) |
| 5 |
local |
| 6 |
|
| 7 |
structure T = |
| 8 |
struct |
| 9 |
include "mltree-basis.sig" |
| 10 |
include "mltree.sig" |
| 11 |
end |
| 12 |
|
| 13 |
in |
| 14 |
|
| 15 |
functor MLTreeSimplifier |
| 16 |
(structure T : MLTREE |
| 17 |
structure Size : MLTREE_SIZE (* where T = T *) |
| 18 |
where type T.Basis.cond = T.Basis.cond |
| 19 |
and type T.Basis.div_rounding_mode = T.Basis.div_rounding_mode |
| 20 |
and type T.Basis.ext = T.Basis.ext |
| 21 |
and type T.Basis.fcond = T.Basis.fcond |
| 22 |
and type T.Basis.rounding_mode = T.Basis.rounding_mode |
| 23 |
and type T.Constant.const = T.Constant.const |
| 24 |
and type ('s,'r,'f,'c) T.Extension.ccx = ('s,'r,'f,'c) T.Extension.ccx |
| 25 |
and type ('s,'r,'f,'c) T.Extension.fx = ('s,'r,'f,'c) T.Extension.fx |
| 26 |
and type ('s,'r,'f,'c) T.Extension.rx = ('s,'r,'f,'c) T.Extension.rx |
| 27 |
and type ('s,'r,'f,'c) T.Extension.sx = ('s,'r,'f,'c) T.Extension.sx |
| 28 |
and type T.I.div_rounding_mode = T.I.div_rounding_mode |
| 29 |
and type T.Region.region = T.Region.region |
| 30 |
and type T.ccexp = T.ccexp |
| 31 |
and type T.fexp = T.fexp |
| 32 |
(* and type T.labexp = T.labexp *) |
| 33 |
and type T.mlrisc = T.mlrisc |
| 34 |
and type T.oper = T.oper |
| 35 |
and type T.rep = T.rep |
| 36 |
and type T.rexp = T.rexp |
| 37 |
and type T.stm = T.stm |
| 38 |
(* Extension *) |
| 39 |
val sext : T.rewriter -> T.sext -> T.sext |
| 40 |
val rext : T.rewriter -> T.rext -> T.rext |
| 41 |
val fext : T.rewriter -> T.fext -> T.fext |
| 42 |
val ccext : T.rewriter -> T.ccext -> T.ccext |
| 43 |
) : MLTREE_SIMPLIFIER = |
| 44 |
struct |
| 45 |
|
| 46 |
structure T = T |
| 47 |
structure I = T.I |
| 48 |
structure R = MLTreeRewrite |
| 49 |
(structure T = T |
| 50 |
val sext = sext and rext = rext and fext = fext and ccext = ccext |
| 51 |
) |
| 52 |
|
| 53 |
type simplifier = T.rewriter |
| 54 |
|
| 55 |
val _ = "literals" |
| 56 |
val zero = 0i |
| 57 |
val zeroT = T.LI zero |
| 58 |
|
| 59 |
fun simplify {addressWidth, signedAddress} = |
| 60 |
let |
| 61 |
|
| 62 |
fun dm T.DIV_TO_ZERO = I.DIV_TO_ZERO |
| 63 |
| dm T.DIV_TO_NEGINF = I.DIV_TO_NEGINF |
| 64 |
|
| 65 |
fun sim ==> exp = |
| 66 |
let |
| 67 |
in (* perform algebraic simplification and constant folding *) |
| 68 |
case exp of |
| 69 |
T.ADD(ty,T.ADD(ty', a, T.LI x), T.LI y) where ty = ty' => |
| 70 |
T.ADD(ty,a,T.LI(I.ADD(ty,x,y))) |
| 71 |
| T.ADD(ty,T.LI 0i,x) => x |
| 72 |
| T.ADD(ty,x,T.LI 0i) => x |
| 73 |
| T.ADD(ty,T.LI x,T.LI y) => T.LI(I.ADD(ty,x,y)) |
| 74 |
| T.ADD(ty,T.LABEXP x,T.LABEXP y) => T.LABEXP(T.ADD(ty,x,y)) |
| 75 |
|
| 76 |
| T.SUB(ty,T.LI 0i,T.SUB(ty',T.LI 0i, a)) where ty = ty' => a |
| 77 |
| T.SUB(ty,T.SUB(ty', a, T.LI x), T.LI y) where ty = ty' => |
| 78 |
T.SUB(ty,a,T.LI(I.ADD(ty,x,y))) |
| 79 |
| T.SUB(ty,T.LABEXP x,T.LABEXP y) => T.LABEXP(T.SUB(ty,x,y)) |
| 80 |
|
| 81 |
| T.SUB(ty,a,T.LI 0i) => a |
| 82 |
| T.SUB(ty,T.LI x,T.LI y) => T.LI(I.SUB(ty,x,y)) |
| 83 |
|
| 84 |
| T.MULS(ty,T.LI 0i,_) => zeroT |
| 85 |
| T.MULS(ty,_,T.LI 0i) => zeroT |
| 86 |
| T.MULS(ty,T.LI 1i,x) => x |
| 87 |
| T.MULS(ty,x,T.LI 1i) => x |
| 88 |
| T.MULS(ty,T.LI x,T.LI y) => T.LI(I.MULS(ty,x,y)) |
| 89 |
| T.MULS(ty,T.LABEXP x,T.LABEXP y) => T.LABEXP(T.MULS(ty,x,y)) |
| 90 |
|
| 91 |
| T.DIVS(m,ty,a,T.LI 1i) => a |
| 92 |
| T.DIVS(m,ty,T.LI x,T.LI y) where y <> zero => T.LI(I.DIVS(dm m,ty,x,y)) |
| 93 |
| T.DIVS(m,ty,T.LABEXP x,T.LABEXP y) => T.LABEXP(T.DIVS(m,ty,x,y)) |
| 94 |
|
| 95 |
| T.REMS(m,ty,a,T.LI 1i) => zeroT |
| 96 |
| T.REMS(m,ty,T.LI x,T.LI y) where y <> zero => T.LI(I.REMS(dm m,ty,x,y)) |
| 97 |
| T.REMS(m,ty,T.LABEXP x,T.LABEXP y) => T.LABEXP(T.REMS(m,ty,x,y)) |
| 98 |
|
| 99 |
| T.MULU(ty,T.LI 0i,_) => zeroT |
| 100 |
| T.MULU(ty,_,T.LI 0i) => zeroT |
| 101 |
| T.MULU(ty,T.LI 1i,x) => x |
| 102 |
| T.MULU(ty,x,T.LI 1i) => x |
| 103 |
| T.MULU(ty,T.LI x,T.LI y) => T.LI(I.MULU(ty,x,y)) |
| 104 |
| T.MULU(ty,T.LABEXP x,T.LABEXP y) => T.LABEXP(T.MULU(ty,x,y)) |
| 105 |
|
| 106 |
| T.DIVU(ty,a,T.LI 1i) => a |
| 107 |
| T.DIVU(ty,T.LI x,T.LI y) where y <> zero => T.LI(I.DIVU(ty,x,y)) |
| 108 |
| T.DIVU(ty,T.LABEXP x,T.LABEXP y) => T.LABEXP(T.DIVU(ty,x,y)) |
| 109 |
|
| 110 |
| T.REMU(ty,a,T.LI 1i) => zeroT |
| 111 |
| T.REMU(ty,T.LI x,T.LI y) where y <> zero => T.LI(I.REMU(ty,x,y)) |
| 112 |
| T.REMU(ty,T.LABEXP x,T.LABEXP y) => T.LABEXP(T.REMU(ty,x,y)) |
| 113 |
|
| 114 |
| T.NEGT(ty,T.LI x) => (T.LI(I.NEGT(ty,x)) handle Overflow => exp) |
| 115 |
| T.NEGT(ty,T.LABEXP x) => T.LABEXP(T.NEGT(ty,x)) |
| 116 |
|
| 117 |
| T.ADDT(ty,T.LI 0i,x) => x |
| 118 |
| T.ADDT(ty,x,T.LI 0i) => x |
| 119 |
| T.ADDT(ty,T.LI x,T.LI y) => |
| 120 |
(T.LI(I.ADDT(ty,x,y)) handle Overflow => exp) |
| 121 |
| T.ADDT(ty,T.LABEXP x,T.LABEXP y) => T.LABEXP(T.ADDT(ty,x,y)) |
| 122 |
|
| 123 |
| T.SUBT(ty,a,T.LI 0i) => a |
| 124 |
| T.SUBT(ty,T.LI x,T.LI y) => |
| 125 |
(T.LI(I.SUBT(ty,x,y)) handle Overflow => exp) |
| 126 |
| T.SUBT(ty,T.LABEXP x,T.LABEXP y) => T.LABEXP(T.SUBT(ty,x,y)) |
| 127 |
|
| 128 |
| T.MULT(ty,T.LI 0i,_) => zeroT |
| 129 |
| T.MULT(ty,_,T.LI 0i) => zeroT |
| 130 |
| T.MULT(ty,T.LI 1i,x) => x |
| 131 |
| T.MULT(ty,x,T.LI 1i) => x |
| 132 |
| T.MULT(ty,T.LI x,T.LI y) => |
| 133 |
(T.LI(I.MULT(ty,x,y)) handle Overflow => exp) |
| 134 |
| T.MULT(ty,T.LABEXP x,T.LABEXP y) => T.LABEXP(T.MULT(ty,x,y)) |
| 135 |
|
| 136 |
| T.DIVT(m,ty,a,T.LI 1i) => a |
| 137 |
| T.DIVT(m,ty,T.LI x,T.LI y) where y <> zero => T.LI(I.DIVT(dm m,ty,x,y)) |
| 138 |
| T.DIVT(m,ty,T.LABEXP x,T.LABEXP y) => T.LABEXP(T.DIVT(m,ty,x,y)) |
| 139 |
|
| 140 |
| T.ANDB(_,_,b as T.LI 0i) => b |
| 141 |
| T.ANDB(_,a as T.LI 0i,_) => a |
| 142 |
| T.ANDB(ty,T.NOTB(ty',a),T.NOTB(ty'',b)) |
| 143 |
where ty = ty' andalso ty' = ty'' => T.NOTB(ty,T.ORB(ty,a,b)) |
| 144 |
| T.ANDB(ty,T.LI x,T.LI y) => T.LI(I.ANDB(ty,x,y)) |
| 145 |
| T.ANDB(ty,T.LABEXP x,T.LABEXP y) => T.LABEXP(T.ANDB(ty,x,y)) |
| 146 |
|
| 147 |
| T.ORB(_,a,T.LI 0i) => a |
| 148 |
| T.ORB(_,T.LI 0i,b) => b |
| 149 |
| T.ORB(ty,T.NOTB(ty',a),T.NOTB(ty'',b)) |
| 150 |
where ty = ty' andalso ty' = ty'' => T.NOTB(ty,T.ANDB(ty,a,b)) |
| 151 |
| T.ORB(ty,T.LI x,T.LI y) => T.LI(I.ORB(ty,x,y)) |
| 152 |
| T.ORB(ty,T.LABEXP x,T.LABEXP y) => T.LABEXP(T.ORB(ty,x,y)) |
| 153 |
|
| 154 |
| T.XORB(ty,a,T.LI 0i) => a |
| 155 |
| T.XORB(ty,T.LI 0i,b) => b |
| 156 |
| T.XORB(ty,T.NOTB(ty',a),T.NOTB(ty'',b)) |
| 157 |
where ty = ty' andalso ty' = ty'' => T.NOTB(ty,T.XORB(ty,a,b)) |
| 158 |
| T.XORB(ty,T.LI x,T.LI y) => T.LI(I.XORB(ty,x,y)) |
| 159 |
| T.XORB(ty,T.LABEXP x,T.LABEXP y) => T.LABEXP(T.XORB(ty,x,y)) |
| 160 |
|
| 161 |
| T.EQVB(ty,a,T.LI 0i) => zeroT |
| 162 |
| T.EQVB(ty,T.LI 0i,b) => zeroT |
| 163 |
| T.EQVB(ty,T.LI x,T.LI y) => T.LI(I.EQVB(ty,x,y)) |
| 164 |
| T.EQVB(ty,T.LABEXP x,T.LABEXP y) => T.LABEXP(T.EQVB(ty,x,y)) |
| 165 |
|
| 166 |
| T.NOTB(ty,T.NOTB(ty',a)) where ty = ty' => a |
| 167 |
| T.NOTB(ty,T.LI n) => T.LI(I.NOTB(ty, n)) |
| 168 |
| T.NOTB(ty,T.LABEXP x) => T.LABEXP(T.NOTB(ty,x)) |
| 169 |
|
| 170 |
| T.SRA(ty,a,T.LI 0i) => a |
| 171 |
| T.SRA(ty,T.LI 0i,_) => zeroT |
| 172 |
| T.SRA(ty,T.LI x,T.LI y) => T.LI(I.SRA(ty,x,y)) |
| 173 |
| T.SRA(ty,T.LABEXP x,T.LABEXP y) => T.LABEXP(T.SRA(ty,x,y)) |
| 174 |
|
| 175 |
| T.SRL(ty,a,T.LI 0i) => a |
| 176 |
| T.SRL(ty,T.LI 0i,_) => zeroT |
| 177 |
| T.SRL(ty,_,T.LI n) where IntInf.<=(IntInf.fromInt ty,n) => zeroT |
| 178 |
| T.SRL(ty,T.LI x,T.LI y) => T.LI(I.SRL(ty,x,y)) |
| 179 |
| T.SRL(ty,T.LABEXP x,T.LABEXP y) => T.LABEXP(T.SRL(ty,x,y)) |
| 180 |
|
| 181 |
| T.SLL(ty,a,T.LI 0i) => a |
| 182 |
| T.SLL(ty,T.LI 0i,_) => zeroT |
| 183 |
| T.SLL(ty,_,T.LI n) where IntInf.<=(IntInf.fromInt ty,n) => zeroT |
| 184 |
| T.SLL(ty,T.LI x,T.LI y) => T.LI(I.SLL(ty,x,y)) |
| 185 |
| T.SLL(ty,T.LABEXP x,T.LABEXP y) => T.LABEXP(T.SLL(ty,x,y)) |
| 186 |
|
| 187 |
(* reig *) |
| 188 |
|
| 189 |
(* MLtree does not have an UNSIGNED LOAD operation. In targets |
| 190 |
where both uload and sload are provided, T.LOAD translates to |
| 191 |
uload. To get the sload, the client must emit: |
| 192 |
T.SX(ty,ty',T.LOAD(ty',ea,mem)) |
| 193 |
We don't want to simplify this here, so that the instruction |
| 194 |
selector sees it. |
| 195 |
*) |
| 196 |
|
| 197 |
| T.SX(ty,ty',T.LOAD _) => exp |
| 198 |
|
| 199 |
| T.SX(ty,ty',e) where ty = ty' => e |
| 200 |
| T.SX(ty,ty',T.LI n) => T.LI(I.SX(ty,ty',n)) |
| 201 |
| T.SX(ty,ty',T.LABEXP x) => T.LABEXP(T.SX(ty,ty',x)) |
| 202 |
|
| 203 |
| T.ZX(ty,ty',e) where ty = ty' => e |
| 204 |
| T.ZX(ty,ty',T.LI n) => T.LI(I.ZX(ty,ty',n)) |
| 205 |
| T.ZX(ty,ty',T.LABEXP x) => T.LABEXP(T.ZX(ty,ty',x)) |
| 206 |
|
| 207 |
| T.COND(ty,T.TRUE,a,b) => a |
| 208 |
| T.COND(ty,T.FALSE,a,b) => b |
| 209 |
|
| 210 |
| exp => exp |
| 211 |
end |
| 212 |
|
| 213 |
and simStm ==> (T.IF(T.TRUE,yes,no)) = yes |
| 214 |
| simStm ==> (T.IF(T.FALSE,yes,no)) = no |
| 215 |
| simStm ==> (T.SEQ[x]) = x |
| 216 |
| simStm ==> s = s |
| 217 |
|
| 218 |
and simF ==> (T.FNEG(ty,T.FNEG(ty',e))) where (ty = ty') = e |
| 219 |
| simF ==> (T.CVTF2F(ty,ty',e)) where (ty = ty') = e |
| 220 |
| simF ==> (T.FCOND(ty,T.TRUE,yes,no)) = yes |
| 221 |
| simF ==> (T.FCOND(ty,T.FALSE,yes,no)) = no |
| 222 |
| simF ==> exp = exp |
| 223 |
|
| 224 |
and cc false = T.FALSE | cc true = T.TRUE |
| 225 |
and simCC ==> (T.CMP(ty,T.EQ,T.LI x,T.LI y)) = cc(I.EQ(ty,x,y)) |
| 226 |
| simCC ==> (T.CMP(ty,T.NE,T.LI x,T.LI y)) = cc(I.NE(ty,x,y)) |
| 227 |
| simCC ==> (T.CMP(ty,T.GT,T.LI x,T.LI y)) = cc(I.GT(ty,x,y)) |
| 228 |
| simCC ==> (T.CMP(ty,T.GE,T.LI x,T.LI y)) = cc(I.GE(ty,x,y)) |
| 229 |
| simCC ==> (T.CMP(ty,T.LT,T.LI x,T.LI y)) = cc(I.LT(ty,x,y)) |
| 230 |
| simCC ==> (T.CMP(ty,T.LE,T.LI x,T.LI y)) = cc(I.LE(ty,x,y)) |
| 231 |
| simCC ==> (T.CMP(ty,T.GTU,T.LI x,T.LI y)) = cc(I.GTU(ty,x,y)) |
| 232 |
| simCC ==> (T.CMP(ty,T.LTU,T.LI x,T.LI y)) = cc(I.LTU(ty,x,y)) |
| 233 |
| simCC ==> (T.CMP(ty,T.GEU,T.LI x,T.LI y)) = cc(I.GEU(ty,x,y)) |
| 234 |
| simCC ==> (T.CMP(ty,T.LEU,T.LI x,T.LI y)) = cc(I.LEU(ty,x,y)) |
| 235 |
| simCC ==> (T.AND(T.TRUE,x)) = x |
| 236 |
| simCC ==> (T.AND(x,T.TRUE)) = x |
| 237 |
| simCC ==> (T.AND(T.FALSE,x)) = T.FALSE |
| 238 |
| simCC ==> (T.AND(x,T.FALSE)) = T.FALSE |
| 239 |
| simCC ==> (T.OR(T.FALSE,x)) = x |
| 240 |
| simCC ==> (T.OR(x,T.FALSE)) = x |
| 241 |
| simCC ==> (T.OR(T.TRUE,x)) = T.TRUE |
| 242 |
| simCC ==> (T.OR(x,T.TRUE)) = T.TRUE |
| 243 |
| simCC ==> (T.XOR(T.TRUE,T.TRUE)) = T.FALSE |
| 244 |
| simCC ==> (T.XOR(T.FALSE,x)) = x |
| 245 |
| simCC ==> (T.XOR(x,T.FALSE)) = x |
| 246 |
| simCC ==> (T.XOR(T.TRUE,x)) = T.NOT x |
| 247 |
| simCC ==> (T.XOR(x,T.TRUE)) = T.NOT x |
| 248 |
| simCC ==> (T.EQV(T.FALSE,T.FALSE)) = T.TRUE |
| 249 |
| simCC ==> (T.EQV(T.TRUE,x)) = x |
| 250 |
| simCC ==> (T.EQV(x,T.TRUE)) = x |
| 251 |
| simCC ==> (T.EQV(T.FALSE,x)) = T.NOT x |
| 252 |
| simCC ==> (T.EQV(x,T.FALSE)) = T.NOT x |
| 253 |
| simCC ==> exp = exp |
| 254 |
|
| 255 |
in R.rewrite {rexp=sim,fexp=simF,ccexp=simCC,stm=simStm} end |
| 256 |
end |
| 257 |
end (* local *) |