Debian Codegraph @

lib/mlrisc-lib/MLRISC/amd64/ra/amd64Rewrite.sml

raw
1 (* amd64Rewrite.sml
2 *
3 * COPYRIGHT (c) 2018 The Fellowship of SML/NJ (http://www.smlnj.org)
4 * All rights reserved.
5 *)
6
7 functor AMD64Rewrite (Instr : AMD64INSTR) : sig
8
9 structure I : AMD64INSTR
10 structure CB : CELLS_BASIS (* = CellsBasis *)
11 where type CellSet.cellset = CellsBasis.CellSet.cellset
12 and type 'a ColorTable.hash_table = 'a CellsBasis.ColorTable.hash_table
13 and type 'a HashTable.hash_table = 'a CellsBasis.HashTable.hash_table
14 and type SortedCells.sorted_cells = CellsBasis.SortedCells.sorted_cells
15 and type cell = CellsBasis.cell
16 and type cellColor = CellsBasis.cellColor
17 and type cellkind = CellsBasis.cellkind
18 and type cellkindDesc = CellsBasis.cellkindDesc
19 and type cellkindInfo = CellsBasis.cellkindInfo
20
21 val rewriteUse : I.instruction * CB.cell * CB.cell -> I.instruction
22 val rewriteDef : I.instruction * CB.cell * CB.cell -> I.instruction
23
24 val frewriteUse : I.instruction * CB.cell * CB.cell -> I.instruction
25 val frewriteDef : I.instruction * CB.cell * CB.cell -> I.instruction
26
27 end = struct
28
29 structure I = Instr
30 structure C=I.C
31 structure CB : CELLS_BASIS = CellsBasis
32
33 fun replace (rs, rd) r = if CB.sameColor(rs, r) then rd else r
34
35 (* rewrite an operand *)
36 fun operand (rs, rd) opnd = (case opnd
37 of I.Direct(n, r) => if CB.sameColor(rs, r) then I.Direct(n, rd) else opnd
38 | I.FDirect r => if CB.sameColor(rs, r) then I.FDirect rd else opnd
39 | I.Displace{base, disp, mem} => if CB.sameColor(base,rs)
40 then I.Displace{base=rd, disp=disp, mem=mem}
41 else opnd
42 | I.Indexed{base=NONE, index, scale, disp, mem} => if CB.sameColor(rs, index)
43 then I.Indexed{base=NONE, index=rd, scale=scale, disp=disp, mem=mem}
44 else opnd
45 | I.Indexed{base as SOME rb, index, scale, disp, mem} => let
46 val base' = if CB.sameColor(rs, rb) then SOME rd else base
47 val index' = if CB.sameColor(rs, index) then rd else index
48 in
49 I.Indexed{base=base', index=index', scale=scale, disp=disp, mem=mem}
50 end
51 | _ => opnd
52 (* end case *))
53
54 fun rewriteUse' (instr, rs, rd) = let
55 val operand = operand (rs, rd)
56 in
57 case instr
58 of I.JMP(opnd, labs) => I.JMP(operand opnd, labs)
59 | I.JCC{cond, opnd} => I.JCC{cond=cond, opnd = operand opnd}
60 | I.CALL{opnd, defs, uses, return, cutsTo, mem, pops} => I.CALL{
61 opnd=operand opnd, defs=defs, return=return,
62 uses=CB.CellSet.map {from=rs,to=rd} uses, cutsTo=cutsTo,
63 mem=mem, pops=pops
64 }
65 | I.MOVE{mvOp, src, dst as I.Direct _} => I.MOVE{mvOp=mvOp, src=operand src, dst=dst}
66 | I.MOVE{mvOp, src, dst} => I.MOVE{mvOp=mvOp, src=operand src, dst=operand dst}
67 | I.LEAL{r32, addr} => I.LEAL{r32=r32, addr=operand addr}
68 | I.LEAQ{r64, addr} => I.LEAQ{r64=r64, addr=operand addr}
69 | I.CMPQ{lsrc, rsrc} => I.CMPQ{lsrc=operand lsrc, rsrc=operand rsrc}
70 | I.CMPL{lsrc, rsrc} => I.CMPL{lsrc=operand lsrc, rsrc=operand rsrc}
71 | I.CMPW{lsrc, rsrc} => I.CMPW{lsrc=operand lsrc, rsrc=operand rsrc}
72 | I.CMPB{lsrc, rsrc} => I.CMPB{lsrc=operand lsrc, rsrc=operand rsrc}
73 | I.TESTQ{lsrc, rsrc} => I.TESTQ{lsrc=operand lsrc, rsrc=operand rsrc}
74 | I.TESTL{lsrc, rsrc} => I.TESTL{lsrc=operand lsrc, rsrc=operand rsrc}
75 | I.TESTW{lsrc, rsrc} => I.TESTW{lsrc=operand lsrc, rsrc=operand rsrc}
76 | I.TESTB{lsrc, rsrc} => I.TESTB{lsrc=operand lsrc, rsrc=operand rsrc}
77 | I.BITOP{bitOp, lsrc, rsrc} =>
78 I.BITOP{bitOp=bitOp, lsrc=operand lsrc, rsrc=operand rsrc}
79 | I.BINARY{binOp, src, dst} =>
80 I.BINARY{binOp=binOp, src=operand src, dst=operand dst}
81 | I.SHIFT{shiftOp, src, dst, count} =>
82 I.SHIFT{shiftOp=shiftOp, src=operand src, dst=operand dst, count=operand src}
83 | I.MULTDIV{multDivOp, src} =>
84 I.MULTDIV{multDivOp=multDivOp, src=operand src}
85 | I.MUL3{dst, src1, src2} => I.MUL3{dst=dst, src1=operand src1, src2=src2}
86 | I.MULQ3{dst, src1, src2} => I.MULQ3{dst=dst, src1=operand src1, src2=src2}
87 | I.UNARY{unOp, opnd} => I.UNARY{unOp=unOp, opnd=operand opnd}
88 | I.SET{cond, opnd} => I.SET{cond=cond, opnd=operand opnd}
89 (* Q: what if dst is not I.Direct? *)
90 | I.CMOV{cond, src, dst} => I.CMOV{cond=cond, src=operand src, dst=dst}
91 | I.PUSH opnd => I.PUSH(operand opnd)
92 | I.POP opnd => I.POP(operand opnd)
93 | I.FMOVE{fmvOp, src, dst} => I.FMOVE{fmvOp=fmvOp, src=operand src, dst=operand dst}
94 | I.FBINOP{binOp, dst, src} => I.FBINOP{binOp=binOp, src=operand src, dst=dst}
95 | I.FCOM{comOp, src, dst} => I.FCOM{comOp=comOp, src=operand src, dst=dst}
96 | I.FSQRTS{src, dst} => I.FSQRTS{src=operand src, dst=operand dst}
97 | I.FSQRTD{src, dst} => I.FSQRTD{src=operand src, dst=operand dst}
98 | I.XCHG{lock, sz, src, dst} =>
99 I.XCHG{lock=lock, sz=sz, src=operand src, dst=operand dst}
100 | I.CMPXCHG{lock, sz, src, dst} =>
101 I.CMPXCHG{lock=lock, sz=sz, src=operand src, dst=operand dst}
102 | I.XADD{lock, sz, src, dst} =>
103 I.XADD{lock=lock, sz=sz, src=operand src, dst=operand dst}
104 | _ => instr
105 (* end case *)
106 end
107
108 fun rewriteDef' (instr, rs, rd)= let
109 val operand = operand (rs, rd)
110 in
111 case instr
112 of I.CALL{opnd, defs, uses, return, cutsTo, mem, pops} => I.CALL{
113 opnd=opnd, cutsTo=cutsTo, pops=pops,
114 return=CB.CellSet.map {from=rs,to=rd} return,
115 defs=CB.CellSet.map {from=rs,to=rd} defs,
116 uses=uses, mem=mem
117 }
118 | I.MOVE{mvOp, src, dst} => I.MOVE{mvOp=mvOp, src=src, dst=operand dst}
119 | I.LEAL{r32, addr} =>
120 if CB.sameColor(rs, r32) then I.LEAL{r32=rd, addr=addr} else instr
121 | I.LEAQ{r64, addr} =>
122 if CB.sameColor(rs, r64) then I.LEAQ{r64=rd, addr=addr} else instr
123 | I.BINARY{binOp, src, dst} => I.BINARY{binOp=binOp, src=src, dst=operand dst}
124 | I.SHIFT{shiftOp, src, dst, count} =>
125 I.SHIFT{shiftOp=shiftOp, src=src, count=count, dst=operand dst}
126 | I.MUL3{dst, src1, src2} =>
127 if CB.sameColor(rs, dst) then I.MUL3{dst=rd, src1=src1, src2=src2} else instr
128 | I.MULQ3{dst, src1, src2} =>
129 if CB.sameColor(rs, dst) then I.MULQ3{dst=rd, src1=src1, src2=src2} else instr
130 | I.UNARY{unOp, opnd} => I.UNARY{unOp=unOp, opnd=operand opnd}
131 | I.SET{cond, opnd} => I.SET{cond=cond, opnd=operand opnd}
132 | I.CMOV{cond, src, dst} =>
133 if CB.sameColor(rs, dst) then I.CMOV{cond=cond, src=src, dst=rd} else instr
134 | I.POP opnd => I.POP(operand opnd)
135 | _ => instr
136 (* end case *)
137 end
138
139 fun frewriteUse' (instr, rs, rd) = let
140 val operand = operand (rs, rd)
141 val replace = replace (rs, rd)
142 in
143 case instr
144 of I.FMOVE{fmvOp, dst, src} => I.FMOVE{fmvOp=fmvOp, dst=dst, src=operand src}
145 | I.FBINOP{binOp, dst, src} =>
146 I.FBINOP{binOp=binOp, dst=replace dst, src=operand src}
147 | I.FCOM{comOp, dst, src} =>
148 I.FCOM{comOp=comOp, dst=replace dst, src=operand src}
149 | I.FSQRTS{dst, src} => I.FSQRTS{dst=dst, src=operand src}
150 | I.FSQRTD{dst, src} => I.FSQRTD{dst=dst, src=operand src}
151 | _ => instr
152 (* end case *)
153 end
154
155 fun frewriteDef' (instr, rs, rd) = let
156 val operand = operand (rs, rd)
157 in
158 case instr
159 of I.CALL{opnd, defs, uses, return, cutsTo, mem, pops} => I.CALL{
160 opnd=opnd, cutsTo=cutsTo, pops=pops,
161 return=CB.CellSet.map {from=rs,to=rd} return,
162 defs=CB.CellSet.map {from=rs,to=rd} defs,
163 uses=uses, mem=mem
164 }
165 | I.FMOVE{fmvOp, dst, src} => I.FMOVE{fmvOp=fmvOp, dst=operand dst, src=src}
166 | I.FBINOP{binOp, dst, src} =>
167 if CB.sameColor(rs, dst) then I.FBINOP{binOp=binOp, dst=rd, src=src} else instr
168 | I.FSQRTS{dst, src} => I.FSQRTS{dst=operand dst, src=src}
169 | I.FSQRTD{dst, src} => I.FSQRTD{dst=operand dst, src=src}
170 | _ => instr
171 (* end case *)
172 end
173
174 (* lift rewrting of the `instr` type to work on the `instruction` type *)
175 fun rewrite (rwInstr, isRWUse) = let
176 fun rewrite' (instr, rs, rd) = let
177 val replace = replace (rs, rd)
178 in
179 case instr
180 of I.ANNOTATION{a, i} => I.ANNOTATION{
181 i = rewrite' (i, rs, rd),
182 a = case a
183 of CB.DEF_USE{cellkind=CB.FP, defs, uses} => let
184 val (defs', uses') = if isRWUse
185 then (defs, List.map replace uses)
186 else (List.map replace defs, uses)
187 in
188 CB.DEF_USE{cellkind = CB.FP, defs = defs', uses = uses'}
189 end
190 | _ => a
191 (* end case *)
192 }
193 | I.INSTR i => I.INSTR(rwInstr(i, rs, rd))
194 | I.COPY{k, sz, dst, src, tmp} => let
195 val (dst', src') = if isRWUse
196 then (dst, List.map replace src)
197 else (List.map replace dst, src)
198 in
199 I.COPY{k=k, sz=sz, dst=dst', src=src', tmp=tmp}
200 end
201 (* end case *)
202 end
203 in
204 rewrite'
205 end
206
207 val rewriteUse = rewrite (rewriteUse', true)
208 val rewriteDef = rewrite (rewriteDef', false)
209
210 val frewriteUse = rewrite (frewriteUse', true)
211 val frewriteDef = rewrite (frewriteDef', false)
212
213 end