| 1 |
(* alphaRewrite.sml -- rewrite an alpha instruction |
| 2 |
* |
| 3 |
* COPYRIGHT (c) 1997 Bell Labs |
| 4 |
*) |
| 5 |
|
| 6 |
functor AlphaRewrite(Instr : ALPHAINSTR) = struct |
| 7 |
structure I=Instr |
| 8 |
structure C=I.C |
| 9 |
structure CB=CellsBasis |
| 10 |
structure CS=CB.CellSet |
| 11 |
fun error msg = MLRiscErrorMsg.error ("AlphaRewrite", msg) |
| 12 |
|
| 13 |
fun rewriteUse(instr, rs, rt) = let |
| 14 |
fun match r = CB.sameColor(r,rs) |
| 15 |
fun replace r = if match r then rt else r |
| 16 |
fun replaceEA(SOME(I.Displace{base, disp, mem})) = |
| 17 |
SOME(I.Displace{base=replace base, disp=disp, mem=mem}) |
| 18 |
| replaceEA ea = ea |
| 19 |
fun alphaUse(instr) = let |
| 20 |
fun isRegOp (I.REGop r) = match r |
| 21 |
| isRegOp _ = false |
| 22 |
fun rwOperand(opnd as I.REGop r) = |
| 23 |
if match r then I.REGop rt else opnd |
| 24 |
| rwOperand opnd = opnd |
| 25 |
fun load(ldClass, {ldOp, r, b, d, mem}) = |
| 26 |
if match b |
| 27 |
then ldClass{ldOp=ldOp, r=r, b=rt, d=d, mem=mem} |
| 28 |
else instr |
| 29 |
fun fstore(stClass, {stOp, r, b, d, mem}) = |
| 30 |
if match b then stClass{stOp=stOp, r=r, b=rt, d=d, mem=mem} |
| 31 |
else instr |
| 32 |
fun store{stOp, r, b, d, mem} = |
| 33 |
if match r then |
| 34 |
if match b then |
| 35 |
I.STORE{stOp=stOp, r=rt, b=rt, d=d, mem=mem} |
| 36 |
else |
| 37 |
I.STORE{stOp=stOp, r=rt, b=b, d=d, mem=mem} |
| 38 |
else if match b then |
| 39 |
I.STORE{stOp=stOp, r=r, b=rt, d=d, mem=mem} |
| 40 |
else instr |
| 41 |
fun operate(opClass, {oper, ra, rb, rc}) = |
| 42 |
if match ra then |
| 43 |
if isRegOp rb then |
| 44 |
opClass{oper=oper, ra=rt, rb=I.REGop rt, rc=rc} |
| 45 |
else opClass{oper=oper, ra=rt, rb=rb, rc=rc} |
| 46 |
else if isRegOp rb then |
| 47 |
opClass{oper=oper, ra=ra, rb=I.REGop rt, rc=rc} |
| 48 |
else instr |
| 49 |
|
| 50 |
in |
| 51 |
case instr |
| 52 |
of I.LDA{r, b, d} => if match b then I.LDA{r=r, b=rt, d=d} else instr |
| 53 |
| I.LDAH{r, b, d} => if match b then I.LDAH{r=r, b=rt, d=d} else instr |
| 54 |
| I.LOAD arg => load(I.LOAD, arg) |
| 55 |
| I.FLOAD farg => load(I.FLOAD, farg) |
| 56 |
| I.STORE arg => store arg |
| 57 |
| I.FSTORE farg => fstore(I.FSTORE, farg) |
| 58 |
| I.JMPL({r, b, d}, labs) => |
| 59 |
if match b then I.JMPL({r=r, b=rt, d=d}, labs) else instr |
| 60 |
| I.JSR{r, b, d, defs, uses, cutsTo, mem} => |
| 61 |
I.JSR{r=r, b=replace b, d=d, defs=defs, |
| 62 |
uses=CS.map {from=rs,to=rt} uses, cutsTo=cutsTo, mem=mem} |
| 63 |
| I.BSR{r, lab, defs, uses, cutsTo, mem} => |
| 64 |
I.BSR{r=r, lab=lab, defs=defs, |
| 65 |
uses=CS.map {from=rs,to=rt} uses, cutsTo=cutsTo, mem=mem} |
| 66 |
| I.RET{r,b,d} => I.RET{r=r, b=replace b, d=d} |
| 67 |
| I.BRANCH{b=I.BR, ...} => instr |
| 68 |
| I.BRANCH{b, r, lab} => if match r then I.BRANCH{b=b, r=rt, lab=lab} |
| 69 |
else instr |
| 70 |
| I.OPERATE arg => operate(I.OPERATE, arg) |
| 71 |
| I.OPERATEV arg => operate(I.OPERATEV, arg) |
| 72 |
| I.CMOVE{oper,ra,rb,rc} => |
| 73 |
I.CMOVE{oper=oper,ra=replace ra,rb=rwOperand rb,rc=replace rc} |
| 74 |
| I.CALL_PAL{code, def, use } => |
| 75 |
I.CALL_PAL{code=code, def=def, use=CS.map {from=rs,to=rt} use} |
| 76 |
| I.PSEUDOARITH{oper, ra, rb, rc, tmps} => |
| 77 |
I.PSEUDOARITH{oper=oper, ra=replace ra, rb=rwOperand rb, rc=rc, |
| 78 |
tmps=tmps} |
| 79 |
| _ => instr |
| 80 |
end |
| 81 |
in |
| 82 |
case instr |
| 83 |
of (I.ANNOTATION{i, ...}) => rewriteUse(i, rs, rt) |
| 84 |
| I.LIVE{regs, spilled} => I.LIVE{regs=C.addReg(rt, C.rmvReg(rs, regs)), |
| 85 |
spilled=spilled} |
| 86 |
| I.INSTR(i) => I.INSTR(alphaUse(i)) |
| 87 |
| I.COPY{k as CB.GP, sz, dst, src, tmp} => |
| 88 |
I.COPY{k=k, sz=sz, dst=dst, src=map replace src, tmp=replaceEA tmp} |
| 89 |
| _ => error "rewriteUse" |
| 90 |
end |
| 91 |
|
| 92 |
|
| 93 |
fun frewriteUse(instr, fs, ft) = let |
| 94 |
fun match f = CB.sameColor(f,fs) |
| 95 |
fun replace f = if match f then ft else f |
| 96 |
fun alphaUse(instr) = let |
| 97 |
fun foperate(opClass, {oper, fa, fb, fc}) = |
| 98 |
if match fa then |
| 99 |
opClass{oper=oper, fa=ft, fc=fc, fb=replace fb} |
| 100 |
else if match fb then opClass{oper=oper, fa=fa, fb=ft, fc=fc} |
| 101 |
else instr |
| 102 |
in |
| 103 |
case instr |
| 104 |
of I.FBRANCH{b, f, lab} => |
| 105 |
if match f then I.FBRANCH{b=b, f=ft, lab=lab} else instr |
| 106 |
| I.FSTORE{stOp, r, b, d, mem} => |
| 107 |
if match r then I.FSTORE{stOp=stOp, r=ft, b=b, d=d, mem=mem} else instr |
| 108 |
| I.FOPERATE arg => foperate(I.FOPERATE, arg) |
| 109 |
| I.FOPERATEV arg => foperate(I.FOPERATEV, arg) |
| 110 |
| I.FUNARY{oper,fb,fc} => |
| 111 |
if match fb then I.FUNARY{oper=oper,fb=ft,fc=fc} else instr |
| 112 |
| I.FCMOVE{oper,fa,fb,fc} => |
| 113 |
I.FCMOVE{oper=oper,fa=replace fa,fb=replace fb,fc=replace fc} |
| 114 |
| I.JSR{r, b, d, defs, uses, cutsTo, mem} => |
| 115 |
I.JSR{r=r, b=b, d=d, defs=defs, |
| 116 |
uses=CS.map {from=fs,to=ft} uses, cutsTo=cutsTo, mem=mem} |
| 117 |
| I.BSR{r, lab, defs, uses, cutsTo, mem} => |
| 118 |
I.BSR{r=r, lab=lab, defs=defs, |
| 119 |
uses=CS.map {from=fs,to=ft} uses, cutsTo=cutsTo, mem=mem} |
| 120 |
| _ => instr |
| 121 |
end |
| 122 |
in |
| 123 |
case instr |
| 124 |
of I.ANNOTATION{i, ...} => frewriteUse(i, fs, ft) |
| 125 |
| I.LIVE{regs, spilled} => I.LIVE{regs=C.addFreg(ft, C.rmvFreg(fs, regs)), |
| 126 |
spilled=spilled} |
| 127 |
| I.COPY{k as CB.FP, sz, dst, src, tmp} => |
| 128 |
I.COPY{k=k, sz=sz, dst=dst, tmp=tmp, src=map replace src} |
| 129 |
| I.INSTR(i) => I.INSTR(alphaUse(i)) |
| 130 |
| _ => error "frewriteUse" |
| 131 |
end |
| 132 |
|
| 133 |
|
| 134 |
fun rewriteDef(instr, rs, rt) = let |
| 135 |
fun match r = CB.sameColor(r,rs) |
| 136 |
fun rewrite r = if match r then rt else r |
| 137 |
fun ea (SOME(I.Direct r)) = SOME(I.Direct (rewrite r)) |
| 138 |
| ea x = x |
| 139 |
fun alphaDef(instr) = |
| 140 |
(case instr |
| 141 |
of I.LDA{r, b, d} => if match r then I.LDA{r=rt, b=b, d=d} else instr |
| 142 |
| I.LDAH{r, b, d} => if match r then I.LDAH{r=rt, b=b, d=d} else instr |
| 143 |
| I.LOAD{ldOp, r, b, d, mem} => |
| 144 |
if match r then I.LOAD{ldOp=ldOp, r=rt, b=b, d=d, mem=mem} else instr |
| 145 |
| I.JMPL({r, b, d}, labs) => |
| 146 |
if match r then I.JMPL({r=rt, b=b, d=d}, labs) else instr |
| 147 |
| I.JSR{r, b, d, defs, uses, cutsTo, mem} => |
| 148 |
I.JSR{r=rewrite r, b=b, d=d, defs=CS.map {from=rs,to=rt} defs, |
| 149 |
uses=uses, cutsTo=cutsTo, mem=mem} |
| 150 |
| I.BSR{r, lab, defs, uses, cutsTo, mem} => |
| 151 |
I.BSR{r=rewrite r, lab=lab, defs=CS.map {from=rs,to=rt} defs, |
| 152 |
uses=uses, cutsTo=cutsTo, mem=mem} |
| 153 |
| I.RET{r, b, d} => I.RET{r=rewrite r, b=b, d=d} |
| 154 |
| I.BRANCH{b=I.BR, r, lab} => |
| 155 |
if match r then I.BRANCH{b=I.BR, r=rt, lab=lab} else instr |
| 156 |
| I.OPERATE{oper, ra, rb, rc} => |
| 157 |
if match rc then I.OPERATE{oper=oper, ra=ra, rb=rb, rc=rt} else instr |
| 158 |
| I.OPERATEV{oper, ra, rb, rc} => |
| 159 |
if match rc then I.OPERATEV{oper=oper, ra=ra, rb=rb, rc=rt} else instr |
| 160 |
| I.CMOVE{oper,ra,rb,rc} => I.CMOVE{oper=oper,ra=ra,rb=rb,rc=rewrite rc} |
| 161 |
| I.CALL_PAL{code, def, use} => |
| 162 |
I.CALL_PAL{code=code, def=CS.map {from=rs,to=rt} def, use=use} |
| 163 |
| I.PSEUDOARITH{oper, ra, rb, rc, tmps} => |
| 164 |
I.PSEUDOARITH{oper=oper, ra=ra, rb=rb, rc=rewrite rc, |
| 165 |
tmps=CS.map {from=rs,to=rt} tmps} |
| 166 |
| _ => instr |
| 167 |
(*esac*)) |
| 168 |
in |
| 169 |
case instr |
| 170 |
of I.ANNOTATION{i, ...} => rewriteDef(i,rs,rt) |
| 171 |
| I.KILL{regs, spilled} => |
| 172 |
I.KILL{regs=C.addReg(rt, C.rmvReg(rs, regs)), spilled=spilled} |
| 173 |
| I.INSTR(i) => I.INSTR(alphaDef(i)) |
| 174 |
| I.COPY{k as CB.GP, sz, dst, src, tmp} => |
| 175 |
I.COPY{k=k, sz=sz, dst=map rewrite dst, src=src, tmp=ea tmp} |
| 176 |
| _ => error "rewriteDef" |
| 177 |
end |
| 178 |
|
| 179 |
fun frewriteDef(instr, fs, ft) = let |
| 180 |
fun match f = CB.sameColor(f,fs) |
| 181 |
fun rewrite f = if match f then ft else f |
| 182 |
fun ea (SOME(I.FDirect f)) = SOME(I.FDirect(rewrite f)) |
| 183 |
| ea x = x |
| 184 |
fun alphaDef(instr) = |
| 185 |
(case instr |
| 186 |
of I.FLOAD{ldOp, r, b, d, mem} => |
| 187 |
if match r then I.FLOAD{ldOp=ldOp, r=ft, b=b, d=d, mem=mem} else instr |
| 188 |
| I.FOPERATE{oper, fa, fb, fc} => |
| 189 |
if match fc then I.FOPERATE{oper=oper, fa=fa, fb=fb, fc=ft} else instr |
| 190 |
| I.FOPERATEV{oper, fa, fb, fc} => |
| 191 |
if match fc then I.FOPERATEV{oper=oper, fa=fa, fb=fb, fc=ft} else instr |
| 192 |
| I.FUNARY{oper,fb,fc} => |
| 193 |
if match fc then I.FUNARY{oper=oper,fb=fb,fc=ft} else instr |
| 194 |
| I.FCMOVE{oper,fa,fb,fc} => I.FCMOVE{oper=oper,fa=fa,fb=fb,fc=rewrite fc} |
| 195 |
| I.JSR{r, b, d, defs, uses, cutsTo, mem} => |
| 196 |
I.JSR{r=r, b=b, d=d, defs=CS.map {from=fs,to=ft} defs, |
| 197 |
uses=uses, cutsTo=cutsTo, mem=mem} |
| 198 |
| I.BSR{r, lab, defs, uses, cutsTo, mem} => |
| 199 |
I.BSR{r=r, lab=lab, defs=CS.map {from=fs,to=ft} defs, |
| 200 |
uses=uses, cutsTo=cutsTo, mem=mem} |
| 201 |
| I.PSEUDOARITH{oper, ra, rb, rc, tmps} => |
| 202 |
I.PSEUDOARITH{oper=oper, ra=ra, rb=rb, rc=rc, |
| 203 |
tmps=CS.map {from=fs,to=ft} tmps} |
| 204 |
| _ => instr |
| 205 |
(*esac*)) |
| 206 |
in |
| 207 |
case instr |
| 208 |
of I.ANNOTATION{i,a} => frewriteDef(i, fs, ft) |
| 209 |
| I.KILL{regs, spilled} => |
| 210 |
I.KILL{regs=C.addFreg(ft, C.rmvFreg(fs, regs)), spilled=spilled} |
| 211 |
| I.COPY{k as CB.FP, sz, dst, src, tmp} => |
| 212 |
I.COPY{k=k, sz=sz, dst=map rewrite dst, src=src, tmp=ea tmp} |
| 213 |
| I.INSTR(i) => I.INSTR(alphaDef(i)) |
| 214 |
| _ => error "frewriteDef" |
| 215 |
end |
| 216 |
end |
| 217 |
|