| 1 |
(* sparcProps.sml |
| 2 |
* |
| 3 |
* COPYRIGHT (c) 2002 Bell Labs, Lucent Technologies |
| 4 |
*) |
| 5 |
|
| 6 |
functor SparcProps |
| 7 |
(structure SparcInstr : SPARCINSTR |
| 8 |
structure MLTreeEval : MLTREE_EVAL (* where T = SparcInstr.T *) |
| 9 |
where type T.Basis.cond = SparcInstr.T.Basis.cond |
| 10 |
and type T.Basis.div_rounding_mode = SparcInstr.T.Basis.div_rounding_mode |
| 11 |
and type T.Basis.ext = SparcInstr.T.Basis.ext |
| 12 |
and type T.Basis.fcond = SparcInstr.T.Basis.fcond |
| 13 |
and type T.Basis.rounding_mode = SparcInstr.T.Basis.rounding_mode |
| 14 |
and type T.Constant.const = SparcInstr.T.Constant.const |
| 15 |
and type ('s,'r,'f,'c) T.Extension.ccx = ('s,'r,'f,'c) SparcInstr.T.Extension.ccx |
| 16 |
and type ('s,'r,'f,'c) T.Extension.fx = ('s,'r,'f,'c) SparcInstr.T.Extension.fx |
| 17 |
and type ('s,'r,'f,'c) T.Extension.rx = ('s,'r,'f,'c) SparcInstr.T.Extension.rx |
| 18 |
and type ('s,'r,'f,'c) T.Extension.sx = ('s,'r,'f,'c) SparcInstr.T.Extension.sx |
| 19 |
and type T.I.div_rounding_mode = SparcInstr.T.I.div_rounding_mode |
| 20 |
and type T.Region.region = SparcInstr.T.Region.region |
| 21 |
and type T.ccexp = SparcInstr.T.ccexp |
| 22 |
and type T.fexp = SparcInstr.T.fexp |
| 23 |
(* and type T.labexp = SparcInstr.T.labexp *) |
| 24 |
and type T.mlrisc = SparcInstr.T.mlrisc |
| 25 |
and type T.oper = SparcInstr.T.oper |
| 26 |
and type T.rep = SparcInstr.T.rep |
| 27 |
and type T.rexp = SparcInstr.T.rexp |
| 28 |
and type T.stm = SparcInstr.T.stm |
| 29 |
structure MLTreeHash : MLTREE_HASH (* where T = SparcInstr.T *) |
| 30 |
where type T.Basis.cond = SparcInstr.T.Basis.cond |
| 31 |
and type T.Basis.div_rounding_mode = SparcInstr.T.Basis.div_rounding_mode |
| 32 |
and type T.Basis.ext = SparcInstr.T.Basis.ext |
| 33 |
and type T.Basis.fcond = SparcInstr.T.Basis.fcond |
| 34 |
and type T.Basis.rounding_mode = SparcInstr.T.Basis.rounding_mode |
| 35 |
and type T.Constant.const = SparcInstr.T.Constant.const |
| 36 |
and type ('s,'r,'f,'c) T.Extension.ccx = ('s,'r,'f,'c) SparcInstr.T.Extension.ccx |
| 37 |
and type ('s,'r,'f,'c) T.Extension.fx = ('s,'r,'f,'c) SparcInstr.T.Extension.fx |
| 38 |
and type ('s,'r,'f,'c) T.Extension.rx = ('s,'r,'f,'c) SparcInstr.T.Extension.rx |
| 39 |
and type ('s,'r,'f,'c) T.Extension.sx = ('s,'r,'f,'c) SparcInstr.T.Extension.sx |
| 40 |
and type T.I.div_rounding_mode = SparcInstr.T.I.div_rounding_mode |
| 41 |
and type T.Region.region = SparcInstr.T.Region.region |
| 42 |
and type T.ccexp = SparcInstr.T.ccexp |
| 43 |
and type T.fexp = SparcInstr.T.fexp |
| 44 |
(* and type T.labexp = SparcInstr.T.labexp *) |
| 45 |
and type T.mlrisc = SparcInstr.T.mlrisc |
| 46 |
and type T.oper = SparcInstr.T.oper |
| 47 |
and type T.rep = SparcInstr.T.rep |
| 48 |
and type T.rexp = SparcInstr.T.rexp |
| 49 |
and type T.stm = SparcInstr.T.stm |
| 50 |
) : INSN_PROPERTIES = |
| 51 |
struct |
| 52 |
structure I = SparcInstr |
| 53 |
structure C = I.C |
| 54 |
structure T = I.T |
| 55 |
structure CB = CellsBasis |
| 56 |
|
| 57 |
exception NegateConditional |
| 58 |
|
| 59 |
fun error msg = MLRiscErrorMsg.error("SparcProps",msg) |
| 60 |
|
| 61 |
datatype kind = IK_JUMP | IK_NOP | IK_INSTR | IK_COPY | IK_CALL |
| 62 |
| IK_CALL_WITH_CUTS | IK_PHI | IK_SOURCE | IK_SINK |
| 63 |
datatype target = LABELLED of Label.label | FALLTHROUGH | ESCAPES |
| 64 |
|
| 65 |
val zeroR = Option.valOf(C.zeroReg CB.GP) |
| 66 |
val r15 = C.Reg CB.GP 15 |
| 67 |
val r31 = C.Reg CB.GP 31 |
| 68 |
|
| 69 |
(*======================================================================== |
| 70 |
* Instruction Kinds |
| 71 |
*========================================================================*) |
| 72 |
fun instrKind(I.ANNOTATION{i, ...}) = instrKind i |
| 73 |
| instrKind(I.COPY _) = IK_COPY |
| 74 |
| instrKind(I.INSTR instr) = |
| 75 |
(case instr |
| 76 |
of (I.Bicc _) => IK_JUMP |
| 77 |
| (I.FBfcc _) => IK_JUMP |
| 78 |
| (I.JMP _) => IK_JUMP |
| 79 |
| (I.RET _) => IK_JUMP |
| 80 |
| (I.BR _) => IK_JUMP |
| 81 |
| (I.BP _) => IK_JUMP |
| 82 |
| (I.Ticc {t=I.BA, ...}) => IK_JUMP (* trap always *) |
| 83 |
| (I.CALL{cutsTo=_::_,...}) => IK_CALL_WITH_CUTS |
| 84 |
| (I.CALL _) => IK_CALL |
| 85 |
| (I.JMPL{cutsTo=_::_,...}) => IK_CALL_WITH_CUTS |
| 86 |
| (I.JMPL _) => IK_CALL |
| 87 |
| (I.PHI _) => IK_PHI |
| 88 |
| (I.SOURCE _) => IK_SOURCE |
| 89 |
| (I.SINK _) => IK_SINK |
| 90 |
| _ => IK_INSTR |
| 91 |
(*esac*)) |
| 92 |
| instrKind _ = error "instrKind" |
| 93 |
|
| 94 |
fun branchTargets(I.ANNOTATION{i,...}) = branchTargets i |
| 95 |
| branchTargets(I.INSTR(instr)) = |
| 96 |
(case instr |
| 97 |
of (I.Bicc{b=I.BA,label,...}) => [LABELLED label] |
| 98 |
| (I.Bicc{label,...}) => [LABELLED label, FALLTHROUGH] |
| 99 |
| (I.FBfcc{b=I.FBA,label,...}) => [LABELLED label] |
| 100 |
| (I.FBfcc{label,...}) => [LABELLED label, FALLTHROUGH] |
| 101 |
| (I.BR{label,...}) => [LABELLED label, FALLTHROUGH] |
| 102 |
| (I.BP{label,...}) => [LABELLED label, FALLTHROUGH] |
| 103 |
| (I.JMP{labs=[],...}) => [ESCAPES] |
| 104 |
| (I.RET _) => [ESCAPES] |
| 105 |
| (I.JMP{labs,...}) => map LABELLED labs |
| 106 |
| (I.CALL{cutsTo,...}) => FALLTHROUGH::map LABELLED cutsTo |
| 107 |
| (I.JMPL{cutsTo,...}) => FALLTHROUGH::map LABELLED cutsTo |
| 108 |
| (I.Ticc{t=I.BA, ...}) => [ESCAPES] |
| 109 |
| _ => error "branchTargets" |
| 110 |
(*esac*)) |
| 111 |
| branchTargets _ = error "branchTargets" |
| 112 |
|
| 113 |
|
| 114 |
fun setJumpTarget(I.ANNOTATION{a,i}, l) = I.ANNOTATION{a=a, i=setJumpTarget(i,l)} |
| 115 |
| setJumpTarget(I.INSTR(I.Bicc{b=I.BA,a,nop,...}), L) = |
| 116 |
I.bicc{b=I.BA,a=a,label=L,nop=nop} |
| 117 |
| setJumpTarget _ = error "setJumpTarget" |
| 118 |
|
| 119 |
|
| 120 |
fun setBranchTargets{i=I.ANNOTATION{a,i}, t, f} = |
| 121 |
I.ANNOTATION{a=a, i=setBranchTargets{i=i, t=t, f=f}} |
| 122 |
| setBranchTargets{i=I.INSTR(I.Bicc{b=I.BA,a,nop,...}), ...} = |
| 123 |
error "setBranchTargets: Bicc" |
| 124 |
| setBranchTargets{i=I.INSTR(I.Bicc{b,a,nop,...}), t, f} = |
| 125 |
I.bicc{b=b,a=a,label=t,nop=nop} |
| 126 |
| setBranchTargets{i=I.INSTR(I.FBfcc{b,a,nop,...}), t=T, ...} = |
| 127 |
I.fbfcc{b=b, a=a, label=T, nop=nop} |
| 128 |
| setBranchTargets{i=I.INSTR(I.BR{rcond,p,r,a,nop,...}), t=T, ...} = |
| 129 |
I.br{rcond=rcond, p=p, r=r, a=a, label=T, nop=nop} |
| 130 |
| setBranchTargets{i=I.INSTR(I.BP{b,cc,p,a,nop,...}), t=T, ...} = |
| 131 |
I.bp{b=b, cc=cc, p=p, a=a, label=T, nop=nop} |
| 132 |
| setBranchTargets _ = error "setBranchTargets" |
| 133 |
|
| 134 |
fun revCond I.BA = I.BN |
| 135 |
| revCond I.BN = I.BA |
| 136 |
| revCond I.BNE = I.BE |
| 137 |
| revCond I.BE = I.BNE |
| 138 |
| revCond I.BG = I.BLE |
| 139 |
| revCond I.BLE = I.BG |
| 140 |
| revCond I.BGE = I.BL |
| 141 |
| revCond I.BL = I.BGE |
| 142 |
| revCond I.BGU = I.BLEU |
| 143 |
| revCond I.BLEU = I.BGU |
| 144 |
| revCond I.BCC = I.BCS |
| 145 |
| revCond I.BCS = I.BCC |
| 146 |
| revCond I.BPOS = I.BNEG |
| 147 |
| revCond I.BNEG = I.BPOS |
| 148 |
| revCond I.BVC = I.BVS |
| 149 |
| revCond I.BVS = I.BVC |
| 150 |
|
| 151 |
fun revFcond I.FBA = I.FBN |
| 152 |
| revFcond I.FBN = I.FBA |
| 153 |
| revFcond I.FBU = I.FBO |
| 154 |
| revFcond I.FBG = I.FBULE |
| 155 |
| revFcond I.FBUG = I.FBLE |
| 156 |
| revFcond I.FBL = I.FBUGE |
| 157 |
| revFcond I.FBUL = I.FBGE |
| 158 |
| revFcond I.FBLG = I.FBUE |
| 159 |
| revFcond I.FBNE = I.FBE |
| 160 |
| revFcond I.FBE = I.FBNE |
| 161 |
| revFcond I.FBUE = I.FBLG |
| 162 |
| revFcond I.FBGE = I.FBUL |
| 163 |
| revFcond I.FBUGE = I.FBL |
| 164 |
| revFcond I.FBLE = I.FBUG |
| 165 |
| revFcond I.FBULE = I.FBG |
| 166 |
| revFcond I.FBO = I.FBU |
| 167 |
|
| 168 |
fun revRcond I.RZ = I.RNZ |
| 169 |
| revRcond I.RLEZ = I.RGZ |
| 170 |
| revRcond I.RLZ = I.RGEZ |
| 171 |
| revRcond I.RNZ = I.RZ |
| 172 |
| revRcond I.RGZ = I.RLEZ |
| 173 |
| revRcond I.RGEZ = I.RLZ |
| 174 |
|
| 175 |
fun revP I.PT = I.PN |
| 176 |
| revP I.PN = I.PT |
| 177 |
|
| 178 |
fun negateConditional (I.INSTR(I.Bicc{b,a,nop,...}), lab) = |
| 179 |
I.bicc{b=revCond b,a=a,label=lab,nop=nop} |
| 180 |
| negateConditional (I.INSTR(I.FBfcc{b,a,nop,...}), lab) = |
| 181 |
I.fbfcc{b=revFcond b,a=a,label=lab,nop=nop} |
| 182 |
| negateConditional (I.INSTR(I.BR{p,r,rcond,a,nop,...}), lab) = |
| 183 |
I.br{p=revP p,a=a,r=r,rcond=revRcond rcond,label=lab,nop=nop} |
| 184 |
| negateConditional (I.INSTR(I.BP{b,cc,p,a,nop,...}), lab) = |
| 185 |
I.bp{p=revP p,a=a,b=revCond b,cc=cc,label=lab,nop=nop} |
| 186 |
| negateConditional (I.ANNOTATION{i,a}, lab) = |
| 187 |
I.ANNOTATION{i=negateConditional(i, lab), a=a} |
| 188 |
| negateConditional _ = raise NegateConditional |
| 189 |
|
| 190 |
fun jump label = I.bicc{b=I.BA,a=true,label=label,nop=true} |
| 191 |
|
| 192 |
val immedRange = {lo= ~4096, hi = 4095} |
| 193 |
|
| 194 |
fun loadImmed{immed,t} = |
| 195 |
I.arith{a=I.OR,r=zeroR,i= |
| 196 |
if #lo immedRange <= immed andalso immed <= #hi immedRange |
| 197 |
then I.IMMED immed else I.LAB(T.LI(IntInf.fromInt immed)),d=t} |
| 198 |
fun loadOperand{opn, t} = I.arith{a=I.OR,r=zeroR,i=opn, d=t} |
| 199 |
|
| 200 |
fun moveInstr(I.ANNOTATION{i,...}) = moveInstr i |
| 201 |
| moveInstr(I.COPY _) = true |
| 202 |
| moveInstr(I.LIVE _) = false |
| 203 |
| moveInstr(I.KILL _) = false |
| 204 |
| moveInstr _ = false |
| 205 |
|
| 206 |
fun nop() = I.sethi{d=zeroR, i=0} |
| 207 |
|
| 208 |
(*======================================================================== |
| 209 |
* Parallel Move |
| 210 |
*========================================================================*) |
| 211 |
fun moveTmpR(I.COPY{tmp, ...}) = |
| 212 |
(case tmp |
| 213 |
of SOME(I.Direct r) => SOME r |
| 214 |
| SOME(I.FDirect f) => SOME f |
| 215 |
| _ => NONE |
| 216 |
(*esac*)) |
| 217 |
| moveTmpR(I.ANNOTATION{i,...}) = moveTmpR i |
| 218 |
| moveTmpR _ = NONE |
| 219 |
|
| 220 |
|
| 221 |
fun moveDstSrc(I.COPY{dst,src,...}) = (dst,src) |
| 222 |
| moveDstSrc(I.ANNOTATION{i,...}) = moveDstSrc i |
| 223 |
| moveDstSrc _ = error "moveDstSrc" |
| 224 |
|
| 225 |
(*======================================================================== |
| 226 |
* Equality and hashing |
| 227 |
*========================================================================*) |
| 228 |
fun hashOpn(I.REG r) = CB.hashCell r |
| 229 |
| hashOpn(I.IMMED i) = Word.fromInt i |
| 230 |
| hashOpn(I.LAB l) = MLTreeHash.hash l |
| 231 |
| hashOpn(I.LO l) = MLTreeHash.hash l |
| 232 |
| hashOpn(I.HI l) = MLTreeHash.hash l |
| 233 |
fun eqOpn(I.REG a,I.REG b) = CB.sameColor(a,b) |
| 234 |
| eqOpn(I.IMMED a,I.IMMED b) = a = b |
| 235 |
| eqOpn(I.LAB a,I.LAB b) = MLTreeEval.==(a,b) |
| 236 |
| eqOpn(I.LO a,I.LO b) = MLTreeEval.==(a,b) |
| 237 |
| eqOpn(I.HI a,I.HI b) = MLTreeEval.==(a,b) |
| 238 |
| eqOpn _ = false |
| 239 |
|
| 240 |
fun defUseR instr = let |
| 241 |
fun oper (I.REG r,def,use) = (def,r::use) |
| 242 |
| oper (_,def,use) = (def,use) |
| 243 |
fun sparcDU instr = |
| 244 |
(case instr |
| 245 |
of I.LOAD {r,d,i,...} => oper(i,[d],[r]) |
| 246 |
| I.STORE {r,d,i,...} => oper(i,[],[r,d]) |
| 247 |
| I.FLOAD {r,d,i,...} => oper(i,[],[r]) |
| 248 |
| I.FSTORE {r,d,i,...} => oper(i,[],[r]) |
| 249 |
| I.SETHI {d,...} => ([d],[]) |
| 250 |
| I.ARITH {r,i,d,...} => oper(i,[d],[r]) |
| 251 |
| I.SHIFT {r,i,d,...} => oper(i,[d],[r]) |
| 252 |
| I.JMPL{defs,uses,d,r,i,...} => |
| 253 |
oper(i,d:: C.getReg defs,r:: C.getReg uses) |
| 254 |
| I.BR{r,...} => ([],[r]) |
| 255 |
| I.MOVicc{i,d,...} => oper(i,[d],[d]) |
| 256 |
| I.MOVfcc{i,d,...} => oper(i,[d],[d]) |
| 257 |
| I.MOVR{r,i,d,...} => oper(i,[d],[r,d]) |
| 258 |
| I.CALL{defs,uses,...} => (r15 :: C.getReg defs, C.getReg uses) |
| 259 |
| I.JMP{r,i,...} => oper(i,[],[r]) |
| 260 |
| I.RET{leaf=false,...} => ([],[r31]) |
| 261 |
| I.RET{leaf=true,...} => ([],[r15]) |
| 262 |
| I.SAVE{r,i,d} => oper(i,[d],[r]) |
| 263 |
| I.RESTORE{r,i,d} => oper(i,[d],[r]) |
| 264 |
| I.Ticc{r,i,...} => oper(i,[],[r]) |
| 265 |
| I.RDY{d,...} => ([d],[]) |
| 266 |
| I.WRY{r,i,...} => oper(i,[],[r]) |
| 267 |
| _ => ([],[]) |
| 268 |
(*esac*)) |
| 269 |
in |
| 270 |
case instr |
| 271 |
of I.ANNOTATION{i, ...} => defUseR i |
| 272 |
| I.LIVE{regs, ...} => ([], C.getReg regs) |
| 273 |
| I.KILL{regs, ...} => (C.getReg regs, []) |
| 274 |
| I.INSTR(i) => sparcDU(i) |
| 275 |
| I.COPY{k, dst, src, tmp, ...} => let |
| 276 |
val (d,u) = case k of CB.GP => (dst, src) | _ => ([], []) |
| 277 |
in |
| 278 |
case tmp |
| 279 |
of SOME(I.Direct r) => (r::d, u) |
| 280 |
| SOME(I.Displace{base, ...}) => (d, base::u) |
| 281 |
| _ => (d,u) |
| 282 |
end |
| 283 |
end |
| 284 |
|
| 285 |
(* Use of FP registers *) |
| 286 |
fun defUseF instr = let |
| 287 |
fun sparcDU instr = |
| 288 |
(case instr of |
| 289 |
I.FLOAD{r,d,i,...} => ([d],[]) |
| 290 |
| I.FSTORE{r,d,i,...} => ([],[d]) |
| 291 |
| I.FPop1{r,d,...} => ([d],[r]) |
| 292 |
| I.FPop2{r1,r2,d,...} => ([d],[r1,r2]) |
| 293 |
| I.FCMP{r1,r2,...} => ([],[r1,r2]) |
| 294 |
| I.JMPL{defs,uses,...} => (C.getFreg defs,C.getFreg uses) |
| 295 |
| I.CALL{defs,uses,...} => (C.getFreg defs,C.getFreg uses) |
| 296 |
| I.FMOVicc{r,d,...} => ([d],[r,d]) |
| 297 |
| I.FMOVfcc{r,d,...} => ([d],[r,d]) |
| 298 |
| _ => ([],[]) |
| 299 |
(*esac*)) |
| 300 |
in |
| 301 |
case instr |
| 302 |
of I.ANNOTATION{i, ...} => defUseF i |
| 303 |
| I.LIVE{regs, ...} => ([], C.getFreg regs) |
| 304 |
| I.KILL{regs, ...} => (C.getFreg regs, []) |
| 305 |
| I.COPY{k, dst, src, tmp, ...} => let |
| 306 |
val (d, u) = case k of CB.FP => (dst, src) | _ => ([],[]) |
| 307 |
in |
| 308 |
case tmp |
| 309 |
of SOME(I.FDirect f) => (f::d, u) |
| 310 |
| _ => (d, u) |
| 311 |
end |
| 312 |
| I.INSTR(i) => sparcDU(i) |
| 313 |
end |
| 314 |
|
| 315 |
fun defUse CB.GP = defUseR |
| 316 |
| defUse CB.FP = defUseF |
| 317 |
| defUse _ = error "defUse" |
| 318 |
|
| 319 |
(*======================================================================== |
| 320 |
* Annotations |
| 321 |
*========================================================================*) |
| 322 |
fun getAnnotations(I.ANNOTATION{i,a}) = |
| 323 |
let val (i,an) = getAnnotations i in (i,a::an) end |
| 324 |
| getAnnotations i = (i,[]) |
| 325 |
fun annotate(i,a) = I.ANNOTATION{i=i,a=a} |
| 326 |
|
| 327 |
(*======================================================================== |
| 328 |
* Replicate an instruction |
| 329 |
*========================================================================*) |
| 330 |
fun replicate(I.ANNOTATION{i,a}) = I.ANNOTATION{i=replicate i,a=a} |
| 331 |
| replicate(I.COPY{k, sz, tmp=SOME _, dst, src}) = |
| 332 |
I.COPY{k=k, sz=sz, tmp=SOME(I.Direct(C.newReg())), dst=dst, src=src} |
| 333 |
| replicate i = i |
| 334 |
end |