| 1 |
(* alphaProps.sml |
| 2 |
* |
| 3 |
* COPYRIGHT (c) 1996 Bell Laboratories. |
| 4 |
* |
| 5 |
*) |
| 6 |
|
| 7 |
functor AlphaProps |
| 8 |
(structure Instr : ALPHAINSTR |
| 9 |
structure MLTreeHash : MLTREE_HASH (* where T = Instr.T *) |
| 10 |
where type T.Basis.cond = Instr.T.Basis.cond |
| 11 |
and type T.Basis.div_rounding_mode = Instr.T.Basis.div_rounding_mode |
| 12 |
and type T.Basis.ext = Instr.T.Basis.ext |
| 13 |
and type T.Basis.fcond = Instr.T.Basis.fcond |
| 14 |
and type T.Basis.rounding_mode = Instr.T.Basis.rounding_mode |
| 15 |
and type T.Constant.const = Instr.T.Constant.const |
| 16 |
and type ('s,'r,'f,'c) T.Extension.ccx = ('s,'r,'f,'c) Instr.T.Extension.ccx |
| 17 |
and type ('s,'r,'f,'c) T.Extension.fx = ('s,'r,'f,'c) Instr.T.Extension.fx |
| 18 |
and type ('s,'r,'f,'c) T.Extension.rx = ('s,'r,'f,'c) Instr.T.Extension.rx |
| 19 |
and type ('s,'r,'f,'c) T.Extension.sx = ('s,'r,'f,'c) Instr.T.Extension.sx |
| 20 |
and type T.I.div_rounding_mode = Instr.T.I.div_rounding_mode |
| 21 |
and type T.Region.region = Instr.T.Region.region |
| 22 |
and type T.ccexp = Instr.T.ccexp |
| 23 |
and type T.fexp = Instr.T.fexp |
| 24 |
(* and type T.labexp = Instr.T.labexp *) |
| 25 |
and type T.mlrisc = Instr.T.mlrisc |
| 26 |
and type T.oper = Instr.T.oper |
| 27 |
and type T.rep = Instr.T.rep |
| 28 |
and type T.rexp = Instr.T.rexp |
| 29 |
and type T.stm = Instr.T.stm |
| 30 |
structure MLTreeEval : MLTREE_EVAL (* where T = Instr.T *) |
| 31 |
where type T.Basis.cond = Instr.T.Basis.cond |
| 32 |
and type T.Basis.div_rounding_mode = Instr.T.Basis.div_rounding_mode |
| 33 |
and type T.Basis.ext = Instr.T.Basis.ext |
| 34 |
and type T.Basis.fcond = Instr.T.Basis.fcond |
| 35 |
and type T.Basis.rounding_mode = Instr.T.Basis.rounding_mode |
| 36 |
and type T.Constant.const = Instr.T.Constant.const |
| 37 |
and type ('s,'r,'f,'c) T.Extension.ccx = ('s,'r,'f,'c) Instr.T.Extension.ccx |
| 38 |
and type ('s,'r,'f,'c) T.Extension.fx = ('s,'r,'f,'c) Instr.T.Extension.fx |
| 39 |
and type ('s,'r,'f,'c) T.Extension.rx = ('s,'r,'f,'c) Instr.T.Extension.rx |
| 40 |
and type ('s,'r,'f,'c) T.Extension.sx = ('s,'r,'f,'c) Instr.T.Extension.sx |
| 41 |
and type T.I.div_rounding_mode = Instr.T.I.div_rounding_mode |
| 42 |
and type T.Region.region = Instr.T.Region.region |
| 43 |
and type T.ccexp = Instr.T.ccexp |
| 44 |
and type T.fexp = Instr.T.fexp |
| 45 |
(* and type T.labexp = Instr.T.labexp *) |
| 46 |
and type T.mlrisc = Instr.T.mlrisc |
| 47 |
and type T.oper = Instr.T.oper |
| 48 |
and type T.rep = Instr.T.rep |
| 49 |
and type T.rexp = Instr.T.rexp |
| 50 |
and type T.stm = Instr.T.stm |
| 51 |
):INSN_PROPERTIES = |
| 52 |
struct |
| 53 |
structure I = Instr |
| 54 |
structure C = I.C |
| 55 |
structure CB = CellsBasis |
| 56 |
|
| 57 |
exception NegateConditional |
| 58 |
|
| 59 |
fun error msg = MLRiscErrorMsg.impossible ("alphaProps."^msg) |
| 60 |
|
| 61 |
val zeroR = Option.valOf(C.zeroReg CB.GP) |
| 62 |
|
| 63 |
datatype kind = IK_JUMP | IK_NOP | IK_INSTR | IK_COPY | IK_CALL |
| 64 |
| IK_CALL_WITH_CUTS | IK_PHI | IK_SOURCE | IK_SINK |
| 65 |
|
| 66 |
datatype target = LABELLED of Label.label | FALLTHROUGH | ESCAPES |
| 67 |
|
| 68 |
(*======================================================================== |
| 69 |
* Instruction Kinds |
| 70 |
*========================================================================*) |
| 71 |
fun instrKind(I.ANNOTATION{i, ...}) = instrKind i |
| 72 |
| instrKind(I.COPY _) = IK_COPY |
| 73 |
| instrKind(I.INSTR instr) = |
| 74 |
(case instr |
| 75 |
of (I.BRANCH _) => IK_JUMP |
| 76 |
| (I.FBRANCH _) => IK_JUMP |
| 77 |
| (I.JMPL _) => IK_JUMP |
| 78 |
| (I.JSR{cutsTo=[],...}) => IK_CALL |
| 79 |
| (I.JSR _) => IK_CALL_WITH_CUTS |
| 80 |
| (I.BSR{cutsTo=[],...}) => IK_CALL |
| 81 |
| (I.BSR _) => IK_CALL_WITH_CUTS |
| 82 |
| (I.RET _) => IK_JUMP |
| 83 |
| (I.PHI _) => IK_PHI |
| 84 |
| (I.SOURCE _) => IK_SOURCE |
| 85 |
| (I.SINK _) => IK_SINK |
| 86 |
| _ => IK_INSTR |
| 87 |
(*esac*)) |
| 88 |
| instrKind _ = IK_INSTR |
| 89 |
|
| 90 |
fun moveInstr(I.ANNOTATION{i, ...}) = moveInstr i |
| 91 |
| moveInstr(I.COPY _) = true |
| 92 |
| moveInstr _ = false |
| 93 |
|
| 94 |
val nop = |
| 95 |
fn () => I.operate{oper=I.BIS, ra=zeroR, rb=I.REGop zeroR, rc=zeroR} |
| 96 |
|
| 97 |
(*======================================================================== |
| 98 |
* Parallel Move |
| 99 |
*========================================================================*) |
| 100 |
fun moveTmpR(I.COPY{tmp=SOME t, ...}) = |
| 101 |
(case t of I.Direct r => SOME r | I.FDirect f => SOME f | _ => NONE) |
| 102 |
| moveTmpR(I.ANNOTATION{i,...}) = moveTmpR i |
| 103 |
| moveTmpR _ = NONE |
| 104 |
|
| 105 |
fun moveDstSrc(I.COPY{dst, src, ...}) = (dst, src) |
| 106 |
| moveDstSrc(I.ANNOTATION{i,...}) = moveDstSrc i |
| 107 |
| moveDstSrc _ = error "moveDstSrc" |
| 108 |
|
| 109 |
(*======================================================================== |
| 110 |
* Branches and Calls/Returns |
| 111 |
*========================================================================*) |
| 112 |
fun branchTargets(I.ANNOTATION{i,...}) = branchTargets i |
| 113 |
| branchTargets(I.INSTR(instr)) = |
| 114 |
(case instr |
| 115 |
of (I.BRANCH{b=I.BR, lab, ...}) => [LABELLED lab] |
| 116 |
| (I.BRANCH{lab, ...}) => [LABELLED lab, FALLTHROUGH] |
| 117 |
| (I.FBRANCH{lab, ...}) => [LABELLED lab, FALLTHROUGH] |
| 118 |
| (I.JMPL(_,[])) => [ESCAPES] |
| 119 |
| (I.JMPL(_,labs)) => map LABELLED labs |
| 120 |
| (I.RET _) => [ESCAPES] |
| 121 |
| (I.JSR{cutsTo, ...}) => FALLTHROUGH::map LABELLED cutsTo |
| 122 |
| (I.BSR{cutsTo, ...}) => FALLTHROUGH::map LABELLED cutsTo |
| 123 |
| _ => error "branchTargets" |
| 124 |
(*esac*)) |
| 125 |
| branchTargets _ = error "branchTargets" |
| 126 |
|
| 127 |
fun jump label = I.branch{b=I.BR,r=zeroR,lab=label} |
| 128 |
|
| 129 |
val immedRange = {lo= ~32768, hi = 32768} |
| 130 |
fun loadImmed{immed,t} = |
| 131 |
I.lda{r=t,b=zeroR, |
| 132 |
d=if #lo immedRange <= immed andalso immed <= #hi immedRange |
| 133 |
then I.IMMop immed else I.LABop(I.T.LI(I.T.I.fromInt(64,immed)))} |
| 134 |
fun loadOperand{opn,t} = I.lda{r=t,b=zeroR,d=opn} |
| 135 |
|
| 136 |
fun setJumpTarget(I.ANNOTATION{i,a},lab) = |
| 137 |
I.ANNOTATION{i=setJumpTarget(i,lab),a=a} |
| 138 |
| setJumpTarget(I.INSTR(I.BRANCH{b=I.BR,r as CB.CELL{id=31,...}, ...}), lab) = |
| 139 |
I.branch{b=I.BR,r=r,lab=lab} |
| 140 |
| setJumpTarget _ = error "setJumpTarget" |
| 141 |
|
| 142 |
fun setBranchTargets{i=I.ANNOTATION{a,i}, t, f} = |
| 143 |
I.ANNOTATION{a=a, i=setBranchTargets{i=i, t=t, f=f}} |
| 144 |
| setBranchTargets{i=I.INSTR(I.BRANCH{b=I.BR,r as CB.CELL{id=31,...}, ...}), ...} = |
| 145 |
error "setBranchTargets" |
| 146 |
| setBranchTargets{i=I.INSTR(I.BRANCH{b,r,...}), t, f} = I.branch{b=b,r=r,lab=t} |
| 147 |
| setBranchTargets{i=I.INSTR(I.FBRANCH{b,f,...}), t, ...} = I.fbranch{b=b,f=f,lab=t} |
| 148 |
| setBranchTargets _ = error "setBranchTargets" |
| 149 |
|
| 150 |
|
| 151 |
fun negateConditional (br, lab) = let |
| 152 |
fun revBranch I.BEQ = I.BNE |
| 153 |
| revBranch I.BGE = I.BLT |
| 154 |
| revBranch I.BGT = I.BLE |
| 155 |
| revBranch I.BLE = I.BGT |
| 156 |
| revBranch I.BLT = I.BGE |
| 157 |
| revBranch I.BLBC = I.BLBS |
| 158 |
| revBranch I.BLBS = I.BLBC |
| 159 |
| revBranch _ = raise NegateConditional |
| 160 |
fun revFBranch I.FBEQ = I.FBNE |
| 161 |
| revFBranch I.FBNE = I.FBEQ |
| 162 |
| revFBranch I.FBGE = I.FBLT |
| 163 |
| revFBranch I.FBGT = I.FBLE |
| 164 |
| revFBranch I.FBLE = I.FBGT |
| 165 |
| revFBranch I.FBLT = I.FBGE |
| 166 |
in |
| 167 |
case br |
| 168 |
of I.INSTR(I.BRANCH{b,r,...}) => I.branch{b=revBranch b,r=r,lab=lab} |
| 169 |
| I.INSTR(I.FBRANCH{b,f,...}) => I.fbranch{b=revFBranch b,f=f,lab=lab} |
| 170 |
| I.ANNOTATION{i,a} => I.ANNOTATION{i=negateConditional(i, lab),a=a} |
| 171 |
| _ => raise NegateConditional |
| 172 |
end |
| 173 |
|
| 174 |
(*======================================================================== |
| 175 |
* Equality and hashing for operands |
| 176 |
*========================================================================*) |
| 177 |
fun hashOpn(I.REGop r) = CB.hashCell r |
| 178 |
| hashOpn(I.IMMop i) = Word.fromInt i |
| 179 |
| hashOpn(I.HILABop l) = MLTreeHash.hash l |
| 180 |
| hashOpn(I.LOLABop l) = MLTreeHash.hash l |
| 181 |
| hashOpn(I.LABop l) = MLTreeHash.hash l |
| 182 |
|
| 183 |
fun eqOpn(I.REGop a,I.REGop b) = CB.sameColor(a,b) |
| 184 |
| eqOpn(I.IMMop a,I.IMMop b) = a = b |
| 185 |
| eqOpn(I.HILABop a,I.HILABop b) = MLTreeEval.==(a,b) |
| 186 |
| eqOpn(I.LOLABop a,I.LOLABop b) = MLTreeEval.==(a,b) |
| 187 |
| eqOpn(I.LABop a,I.LABop b) = MLTreeEval.==(a,b) |
| 188 |
| eqOpn _ = false |
| 189 |
|
| 190 |
(*======================================================================== |
| 191 |
* Definition and use (for register allocation mainly) |
| 192 |
*========================================================================*) |
| 193 |
fun defUseR instr = let |
| 194 |
fun alphaDU(instr) = let |
| 195 |
fun Oper {oper, ra, rb=I.REGop rb, rc} = ([rc], [ra, rb]) |
| 196 |
| Oper {oper, ra, rb, rc} = ([rc], [ra]) |
| 197 |
fun Opn(I.REGop rb,rs) = rb::rs |
| 198 |
| Opn(_,rs) = rs |
| 199 |
fun FMem (freg, (rd, _)) = ([], [rd]) |
| 200 |
fun trap (def,use) =(def, use) |
| 201 |
|
| 202 |
in |
| 203 |
case instr of |
| 204 |
(* load/store instructions *) |
| 205 |
I.LDA{r, b, ...} => ([r], [b]) |
| 206 |
| I.LDAH{r, b, ...} => ([r], [b]) |
| 207 |
| I.LOAD{r, b, ...} => ([r], [b]) |
| 208 |
| I.STORE{r, b, ...} => ([], [r,b]) |
| 209 |
| I.FLOAD{b, ...} => ([], [b]) |
| 210 |
| I.FSTORE{b, ...} => ([], [b]) |
| 211 |
(* branch instructions *) |
| 212 |
| I.JMPL ({r, b, ...},_) => ([r], [b]) |
| 213 |
| I.JSR{r, b, defs, uses, ...} => (r::C.getReg defs, b::C.getReg uses) |
| 214 |
| I.BSR{r, defs, uses, ...} => (r::C.getReg defs,C.getReg uses) |
| 215 |
| I.RET{r, b, ...} => ([r],[b]) |
| 216 |
| I.BRANCH{b=I.BR, r, ...} => ([r], []) |
| 217 |
| I.BRANCH{r, ...} => ([], [r]) |
| 218 |
(* operate *) |
| 219 |
| I.OPERATE arg => Oper arg |
| 220 |
| I.PSEUDOARITH {oper, ra, rb=I.REGop rb, rc, tmps} => |
| 221 |
(rc:: C.getReg tmps, [ra, rb]) |
| 222 |
| I.PSEUDOARITH {oper, ra, rb, rc, tmps} => (rc:: C.getReg tmps, [ra]) |
| 223 |
| I.OPERATEV arg => trap(Oper arg) |
| 224 |
| I.CMOVE{ra,rb,rc,...} => ([rc],Opn(rb,[ra,rc])) |
| 225 |
(* floating operate *) |
| 226 |
| I.FOPERATEV _ => trap([], []) |
| 227 |
| I.TRAPB => trap([],[]) |
| 228 |
(* macro *) |
| 229 |
| I.CALL_PAL{def,use, ...} => (C.getReg def, C.getReg use) |
| 230 |
| _ => ([],[]) |
| 231 |
end |
| 232 |
in |
| 233 |
case instr |
| 234 |
of I.ANNOTATION{a, i, ...} => defUseR i |
| 235 |
| I.LIVE{regs, ...} => ([], C.getReg regs) |
| 236 |
| I.KILL{regs, ...} => (C.getReg regs, []) |
| 237 |
| I.COPY{k=CB.GP, dst, src, tmp, ...} => |
| 238 |
(case tmp |
| 239 |
of SOME(I.Direct r) => (r::dst, src) |
| 240 |
| SOME(I.Displace{base, ...}) => (dst, base::src) |
| 241 |
| _ => (dst, src) |
| 242 |
(*esac*)) |
| 243 |
| I.COPY _ => ([], []) |
| 244 |
| I.INSTR(i) => alphaDU(i) |
| 245 |
|
| 246 |
end |
| 247 |
|
| 248 |
(* Use of FP registers *) |
| 249 |
fun defUseF instr = let |
| 250 |
fun alphaDU instr = |
| 251 |
case instr of |
| 252 |
I.FBRANCH{f, ...} => ([],[f]) |
| 253 |
| I.FLOAD{r, ...} => ([r], []) |
| 254 |
| I.FSTORE{r, ...} => ([], [r]) |
| 255 |
| I.FOPERATE{fa, fb, fc, ...} => ([fc], [fa, fb]) |
| 256 |
| I.FUNARY{fb, fc, ...} => ([fc], [fb]) |
| 257 |
| I.PSEUDOARITH{tmps, ...} => (C.getFreg tmps, []) |
| 258 |
| I.FOPERATEV{fa, fb, fc, ...} => ([fc], [fa, fb]) |
| 259 |
| I.FCMOVE{fa,fb,fc,...} => ([fc], [fa, fb]) |
| 260 |
| I.JSR{defs,uses, ...} => (C.getFreg defs,C.getFreg uses) |
| 261 |
| I.BSR{defs,uses, ...} => (C.getFreg defs,C.getFreg uses) |
| 262 |
| _ => ([],[]) |
| 263 |
in |
| 264 |
case instr |
| 265 |
of I.ANNOTATION{a, i, ...} => defUseF i |
| 266 |
| I.INSTR(i) => alphaDU(i) |
| 267 |
| I.LIVE{regs, ...} => ([], C.getFreg regs) |
| 268 |
| I.COPY{k=CB.FP, dst, src, tmp, ...} => |
| 269 |
(case tmp |
| 270 |
of SOME(I.FDirect f) => (f::dst, src) |
| 271 |
| _ => (dst, src) |
| 272 |
(*esac*)) |
| 273 |
| I.COPY _ => ([], []) |
| 274 |
| I.KILL{regs, ...} => (C.getFreg regs, []) |
| 275 |
end |
| 276 |
|
| 277 |
fun defUse CB.GP = defUseR |
| 278 |
| defUse CB.FP = defUseF |
| 279 |
| defUse _ = error "defUse" |
| 280 |
|
| 281 |
(*======================================================================= |
| 282 |
* Annotations |
| 283 |
*=======================================================================*) |
| 284 |
fun getAnnotations(I.ANNOTATION{i,a}) = |
| 285 |
let val (i,an) = getAnnotations i in (i,a::an) end |
| 286 |
| getAnnotations i = (i,[]) |
| 287 |
fun annotate(i,a) = I.ANNOTATION{i=i,a=a} |
| 288 |
|
| 289 |
(*======================================================================== |
| 290 |
* Replicate an instruction |
| 291 |
*========================================================================*) |
| 292 |
fun replicate(I.ANNOTATION{i,a}) = I.ANNOTATION{i=replicate i,a=a} |
| 293 |
| replicate(I.COPY{k, sz, tmp=SOME _, dst, src}) = |
| 294 |
I.COPY{tmp=SOME(I.Direct(C.newReg())), dst=dst, src=src, k=k, sz=sz} |
| 295 |
| replicate i = i |
| 296 |
end |