Debian Codegraph @

lib/mlrisc-lib/MLRISC/x86/ra/x86PseudoR.sml

raw
1 signature X86REWRITE_PSEUDO = sig
2 structure F : FLOWGRAPH
3 (*
4 * Takes a cluster and returns a range of registers to prohibit
5 * from spilling. The arguments are:
6 * 1. The first pseudo register
7 * 2. The regmap before RA32. If this is guaranteed to be
8 * an identity you can use the identity function.
9 * I use I.C.lookup regmap.
10 *
11 * 3. The cluster.
12 *
13 * It returns a range of registers.
14 *
15 * NOTE: this version does not assume that the original regmap
16 * is an identity. So there is some ugly regmap business to
17 * take care of.
18 *
19 *)
20 val rewrite :
21 { firstPseudo : F.I.C.cell,
22 originalRegmap : F.I.C.cell -> F.I.C.cell,
23 pruneCellSets : bool (* should we remove references to memory
24 * registers from all cell sets?
25 *)
26 } -> F.cluster -> F.I.C.cell * F.I.C.cell
27 end
28
29
30 functor X86RewritePseudo
31 (structure Instr : X86INSTR
32 structure Flowgraph : FLOWGRAPH where I = Instr
33 val ea : int -> Instr.operand) : X86REWRITE_PSEUDO =
34 struct
35 structure C = X86Cells
36 structure I = Instr
37 structure F = Flowgraph
38
39 fun error msg = MLRiscErrorMsg.error("X86RewritePseudo",msg)
40
41 fun rewrite {firstPseudo, originalRegmap, pruneCellSets}
42 (F.CLUSTER{blocks, regmap, ...}) =
43 let
44 val first = C.newReg()
45 val lookup = C.lookup regmap
46 fun shuffle(dests, srcs, tmp) = let
47 fun move(rd,rs) = I.MOVE{mvOp=I.MOVL, src=rs, dst=rd}
48 fun loop((p as (rd, dst, rs, src))::rest, changed, used, done, instrs) =
49 if List.exists (fn (r : I.C.cell) => dst=r) used then
50 loop(rest, changed, used, p::done, instrs)
51 else
52 loop(rest, true, used, done, move(I.Direct rd, I.Direct rs)::instrs)
53 | loop([], false, _, done, instrs) = (done, instrs)
54 | loop([], true, _, done, instrs) =
55 loop(done, false, map #4 done, [], instrs)
56
57 fun cycle([], instrs) = instrs
58 | cycle(moves, instrs) =
59 (case loop(moves, false, map #4 moves, [], instrs)
60 of ([], instrs) => instrs
61 | ((rd, rd', rs, rs')::nonCyclic, instrs) => let
62 val SOME tmpR = tmp
63 val instrs' = move(tmpR, I.Direct rs)::instrs
64 val (cyclic, instrs'') =
65 loop(nonCyclic, false, map #4 nonCyclic, [], instrs')
66 in cycle(cyclic, move(I.Direct rd, Option.valOf tmp)::instrs'')
67 end
68 (*esac*))
69 fun rmCoalesced([], [], remain, coalesced) = (remain, coalesced)
70 | rmCoalesced(rd::rds, rs::rss, remain, coalesced) = let
71 val dst = lookup rd
72 val src = lookup rs
73 in
74 if dst = ~1 then (* eliminate dead copies *)
75 rmCoalesced(rds, rss, remain, coalesced)
76 else if dst = src then
77 rmCoalesced(rds, rss, remain,
78 move(I.Direct rd, I.Direct rs)::coalesced)
79 else rmCoalesced(rds, rss, (rd, dst, rs, src)::remain, coalesced)
80 end
81 in rev (cycle (rmCoalesced(dests, srcs, [], [])))
82 end
83
84 fun doBlock(F.BBLOCK{blknum, insns, liveOut, succ, ...}) = let
85 fun pseudoR r = (r >= 8 andalso r < firstPseudo)
86 fun resetLiveOut() = let
87 fun reset(gp, fp, cc) =
88 liveOut := (List.filter (not o pseudoR) gp, fp, cc)
89 in
90 case !succ
91 of [] => reset(!liveOut)
92 | [(F.EXIT _,_)] => reset(!liveOut)
93 | _ => ()
94 end
95
96 (* subst: hd(acc) is the last instruction in the stream. *)
97 fun subst(instr, acc) = let
98 fun mark(i,[]) = i
99 | mark(i,a::an) = mark(I.ANNOTATION{i=i,a=a},an)
100
101 fun movl{src, dst, acc} =
102 I.MOVE{mvOp=I.MOVL, src=src, dst=dst}::acc
103
104 fun displace(base, disp, acc, mem) =
105 let val base' = originalRegmap base
106 in if pseudoR base' then
107 let val tmpR = C.newReg()
108 val newDisp = I.Displace{base=tmpR, disp=disp, mem=mem}
109 in (newDisp, movl{src=ea base', dst=I.Direct tmpR, acc=acc})
110 end
111 else (I.Displace{base=base, disp=disp, mem=mem}, acc)
112 end
113
114 fun indexedEa(base, index, scale, disp, mem) =
115 I.Indexed{base=base, index=index, scale=scale, disp=disp, mem=mem}
116
117 fun indexed(NONE, index, scale, disp, acc, mem) =
118 let val index' = originalRegmap index
119 in if pseudoR index' then
120 let val tmpR = C.newReg()
121 val newIndx = indexedEa(NONE, tmpR, scale, disp, mem)
122 in (newIndx,movl{src=ea index', dst=I.Direct tmpR, acc=acc})
123 end
124 else (indexedEa(NONE, index, scale, disp, mem), acc)
125 end
126 | indexed(ba as SOME base, index, scale, disp, acc, mem) =
127 let val base' = originalRegmap base
128 val index' = originalRegmap index
129 val b = pseudoR base'
130 val i = pseudoR index'
131 in if b andalso i then
132 let val tmpB = C.newReg()
133 val tmpI = C.newReg()
134 val opnd = indexedEa(SOME tmpB, tmpI, scale, disp, mem)
135 in (opnd, movl{src=ea base', dst=I.Direct tmpB,
136 acc=movl{src=ea index',
137 dst=I.Direct tmpI, acc=acc}})
138 end
139 else if b then let
140 val tmpB = C.newReg()
141 in (indexedEa(SOME tmpB, index, scale, disp, mem),
142 movl{src=ea base', dst=I.Direct tmpB, acc=acc})
143 end
144 else if i then let
145 val tmpI = C.newReg()
146 in (indexedEa(ba, tmpI, scale, disp, mem),
147 movl{src=ea index', dst=I.Direct tmpI, acc=acc})
148 end
149 else (indexedEa(ba, index, scale, disp, mem), acc)
150
151 end
152 fun direct(r, acc) =
153 let val r' = originalRegmap r
154 in if pseudoR r' then (ea r', acc) else (I.Direct r, acc)
155 end
156
157 fun operand(I.Direct r, acc) = direct(r, acc)
158 | operand(I.Indexed{base, index, scale, disp, mem}, acc) =
159 indexed(base, index, scale, disp, acc, mem)
160 | operand(I.Displace{base, disp, mem}, acc) =
161 displace(base, disp, acc, mem)
162 | operand arg = arg
163
164 fun done(opnd, f, an) =
165 let val (opnd', acc') = operand(opnd, acc)
166 in mark(f opnd', an) :: acc'
167 end
168
169 fun memArg(I.Displace _) = true
170 | memArg(I.Indexed _) = true
171 | memArg(I.MemReg _) = true
172 | memArg(I.LabelEA _) = true
173 | memArg _ = false
174
175 fun withTmp f =
176 let val t = C.newReg()
177 in f t
178 end
179
180 fun rewriteCmpTest(cmptest, lsrc, rsrc, an) =
181 let val (lsrcOpnd, acc1) = operand(lsrc, acc)
182 val (rsrcOpnd, acc2) = operand(rsrc, acc1)
183 in if memArg lsrcOpnd andalso memArg rsrcOpnd then
184 withTmp(fn t =>
185 mark(cmptest{lsrc=I.Direct t, rsrc=rsrcOpnd},an)::
186 movl{src=lsrcOpnd, dst=I.Direct t, acc=acc2})
187 else
188 mark(cmptest{lsrc=lsrcOpnd, rsrc=rsrcOpnd},an)::acc2
189 end
190
191 fun rewrite(instr,an) =
192 case instr
193 of I.JMP(opnd, labs) => done(opnd,fn opnd => I.JMP(opnd, labs),an)
194 | I.JCC{opnd, cond} =>
195 done(opnd,fn opnd => I.JCC{opnd=opnd, cond=cond}, an)
196 | I.MOVE{src, dst, mvOp} => let
197 val (srcOpnd, acc1) = operand(src, acc)
198 val (dstOpnd, acc2) = operand(dst, acc1)
199 in
200 if memArg srcOpnd andalso memArg dstOpnd then
201 withTmp(fn t =>
202 mark(I.MOVE{src=I.Direct t, dst=dstOpnd, mvOp=mvOp},an)::
203 movl{src=srcOpnd, dst=I.Direct t, acc=acc2})
204 else
205 mark(I.MOVE{src=srcOpnd, dst=dstOpnd, mvOp=mvOp},an)::acc2
206 end
207 | I.LEA{r32, addr} => let
208 val (srcOpnd, acc1) = operand(addr, acc)
209 val r32' = originalRegmap r32
210 in
211 if pseudoR r32' then
212 withTmp(fn t =>
213 movl{dst=ea r32', src=I.Direct t,
214 acc=mark(I.LEA{r32=t, addr=srcOpnd},an)::acc1})
215 else mark(I.LEA{r32=r32, addr=srcOpnd},an)::acc1
216 end
217 | I.CMPL{lsrc, rsrc} => rewriteCmpTest(I.CMPL, lsrc, rsrc, an)
218 | I.CMPW{lsrc, rsrc} => rewriteCmpTest(I.CMPW, lsrc, rsrc, an)
219 | I.CMPB{lsrc, rsrc} => rewriteCmpTest(I.CMPB, lsrc, rsrc, an)
220 | I.TESTL{lsrc, rsrc} => rewriteCmpTest(I.TESTL, lsrc, rsrc, an)
221 | I.TESTW{lsrc, rsrc} => rewriteCmpTest(I.TESTW, lsrc, rsrc, an)
222 | I.TESTB{lsrc, rsrc} => rewriteCmpTest(I.TESTB, lsrc, rsrc, an)
223 | I.BINARY{binOp, src, dst} => let
224 val (srcOpnd, acc1) = operand(src, acc)
225 val (dstOpnd, acc2) = operand(dst, acc1)
226 in
227 if memArg srcOpnd andalso memArg dstOpnd then
228 withTmp(fn t =>
229 mark(I.BINARY{binOp=binOp,src=I.Direct t,dst=dstOpnd},an)::
230 movl{src=srcOpnd, dst=I.Direct t, acc=acc2})
231 else
232 mark(I.BINARY{binOp=binOp,src=srcOpnd,dst=dstOpnd},an)::acc2
233 end
234 | I.CALL(opnd,def,use,mem) => let
235 val (opnd1, acc1) = operand(opnd, acc)
236 fun cellset(gp, fp, cc) =
237 if pruneCellSets then
238 (List.filter (not o pseudoR) gp, fp, cc)
239 else
240 (gp, fp, cc)
241 in mark(I.CALL(opnd1, cellset def, cellset use, mem),an)::acc1
242 end
243 | I.MULTDIV{multDivOp, src} =>
244 done(src,
245 fn opnd => I.MULTDIV{multDivOp=multDivOp, src=opnd}, an)
246 | I.MUL3{dst, src1, src2} => let
247 val (src1Opnd, acc1) = operand(src1, acc)
248 val dst' = originalRegmap dst
249 in
250 if pseudoR dst' then
251 withTmp(fn t =>
252 movl{dst=ea dst', src=I.Direct t, acc=
253 mark(I.MUL3{dst=t, src1=src1Opnd, src2=src2},an)::acc1})
254 else mark(I.MUL3{dst=dst, src1=src1Opnd, src2=src2},an)::acc1
255 end
256 | I.UNARY{unOp, opnd} =>
257 done(opnd, fn opnd => I.UNARY{unOp=unOp, opnd=opnd}, an)
258 | I.SET{cond, opnd} =>
259 done(opnd, fn opnd => I.SET{cond=cond, opnd=opnd}, an)
260 | I.PUSHL opnd => done(opnd, I.PUSHL, an)
261 | I.PUSHW opnd => done(opnd, I.PUSHW, an)
262 | I.PUSHB opnd => done(opnd, I.PUSHB, an)
263 | I.POP opnd => done(opnd, I.POP, an)
264 | I.CMOV{cond, src, dst} =>
265 let val (srcOpnd, acc1) = operand(src, acc)
266 val dst' = originalRegmap dst
267 in if pseudoR dst then
268 withTmp(fn t =>
269 movl{dst=ea dst', src=I.Direct t, acc=
270 mark(I.CMOV{cond=cond, dst=t, src=srcOpnd},an)::
271 acc1})
272 else
273 mark(I.CMOV{cond=cond, dst=dst, src=srcOpnd},an)::acc1
274 end
275 | I.COPY{dst, src, tmp} => let
276 (* Note:
277 * Parallel copies are not allowed after this point.
278 * Consider:
279 * (r8, r9, edx) <- (566, 567, 560)
280 *
281 * RA32 may well decide to allocate 560 to r8.
282 * After the rewrite we will get:
283 *
284 * mem[r8] <- 566
285 * mem[r9] <- 567
286 * edx <- 560
287 *
288 * If 560 should spill, we all of a sudden have the
289 * incorrect value being read from the spill location.
290 *)
291 fun f((instr as I.MOVE{mvOp, src, dst})::rest, acc) =
292 (case (src, dst)
293 of (I.Direct s, I.Direct d) =>
294 let val d' = originalRegmap d
295 val s' = originalRegmap s
296 in if s'=d' then f(rest, acc)
297 else if pseudoR d' andalso pseudoR s' then
298 f(rest, withTmp(fn t =>
299 (movl{src=I.Direct t, dst=ea d',
300 acc=movl{src=ea s',
301 dst=I.Direct t, acc=acc}})))
302 else if pseudoR d' then
303 f(rest, withTmp(fn t =>
304 (movl{src=I.Direct s, dst=ea d', acc=acc})))
305 else if pseudoR s' then
306 f(rest, withTmp(fn t =>
307 (movl{src=ea s', dst=I.Direct d, acc=acc})))
308 else f(rest,I.COPY{src=[s], dst=[d],tmp=NONE}::acc)
309 end
310
311 | _ => f(rest, instr::acc)
312 (*esac*))
313
314 | f([], acc) = acc
315 in f(shuffle (dst, src, tmp), acc)
316 end
317 | I.FSTPT opnd => done(opnd, I.FSTPT, an)
318 | I.FSTPL opnd => done(opnd, I.FSTPL, an)
319 | I.FSTPS opnd => done(opnd, I.FSTPS, an)
320 | I.FSTL opnd => done(opnd, I.FSTL, an)
321 | I.FSTS opnd => done(opnd, I.FSTS, an)
322 | I.FLDT opnd => done(opnd, I.FLDT, an)
323 | I.FLDL opnd => done(opnd, I.FLDL, an)
324 | I.FLDS opnd => done(opnd, I.FLDS, an)
325 | I.FILD opnd => done(opnd, I.FILD, an)
326 | I.FILDL opnd => done(opnd, I.FILDL, an)
327 | I.FILDLL opnd => done(opnd, I.FILDLL, an)
328 | I.FENV{fenvOp, opnd} => done(opnd,
329 fn opnd => I.FENV{fenvOp=fenvOp,opnd=opnd}, an)
330 | I.FBINARY{src,dst,binOp} =>
331 done(src,
332 fn opnd => I.FBINARY{binOp=binOp, src=opnd, dst=dst},an)
333 | I.FIBINARY{src,binOp} =>
334 done(src, fn opnd => I.FIBINARY{binOp=binOp, src=opnd},an)
335 | I.ANNOTATION{i,a} => rewrite(i,a::an)
336 | _ => mark(instr,an)::acc
337 in rewrite(instr,[])
338 end (* subst *)
339 in insns := List.foldl subst [] (rev(!insns));
340 if pruneCellSets then resetLiveOut() else ()
341 end (*doBlock*)
342 | doBlock _ = ()
343 in app doBlock blocks; (first, C.newReg())
344 end (* rewrite *)
345 end