-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregalloc.sml
More file actions
386 lines (362 loc) · 15.8 KB
/
Copy pathregalloc.sml
File metadata and controls
386 lines (362 loc) · 15.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
signature REGALLOC = sig
(* alloc(f) returns mips code for function f that has all the temporaries
replaced by mips regs and the load/store of spilled temps taken care of
*)
val alloc : X86.funcode -> X86.funcode
end
structure RegAlloc :> REGALLOC =
struct
structure M = X86
structure RS = X86.RegSet
structure IG = Liveness.IG
structure RT = M.RegTb
fun getmove g (M.Move(r1,r2)) = IG.mk_edge g {from=r2,to=r1}
| getmove g _ = ()
fun print_list list : unit =
(app (fn (reg, reg2) => (print " "; print (M.reg2name reg))) list; print "\n")
fun print_set set : unit =
(RS.app (fn reg => (print " "; print (M.reg2name reg))) set; print "\n")
fun flat xs = List.foldr op@ [] xs
(* after spill renaming, make lw and sw instruction in move instruction (== simple spill algorithm) *)
fun after_spills (spills, spillL, index) =
fn M.Move(rd, rs) =>
if (RS.member(spills, rd) andalso RS.member(spills, rs)) then
[M.Move(rd, rs)]
else
if(RS.member(spills, rd)) then
(case List.find (fn (ind, reg) => reg = rd) (!spillL) of
SOME((ind, reg)) =>
[M.Sw(rs, (M.immed (ind * 4), M.reg "%esp"))]
| NONE =>
(index := !index + 1; spillL := ((!index, rd) :: !spillL);
[M.Sw(rs, (M.immed (!index * 4), M.reg "%esp"))]))
else (
if RS.member(spills, rs) then
(case List.find (fn (ind, reg) => reg = rs) (!spillL) of
SOME((ind, reg)) => (
[M.Lw(rd, (M.immed (ind * 4), M.reg "%esp"))])
| NONE => ErrorMsg.impossible ( "register isn't in spillL : " ^
M.reg2name rs ^ " : " ^ M.reg2name rd)
)
else [M.Move(rd, rs)])
| others => [others]
(* make c = a op b to ta := a, tb := c, tc = ta op tb , c := tc *)
fun rename_spills (spills, spillL, index, nonSpillL) =
fn M.Move(rd, rs) =>
if (RS.member(spills, rd) andalso RS.member(spills, rs)) then
let val tmpReg1 = M.newReg(); val tmpReg2 = M.newReg() in
nonSpillL := tmpReg1 :: (tmpReg2 :: (!nonSpillL));
[M.Move(tmpReg1, rs), M.Move(tmpReg2, tmpReg1), M.Move(rd, tmpReg2)]
end
else
[M.Move(rd, rs)]
| M.Arith1(i, r) =>
if RS.member(spills, r)
then let val tmpReg1 = M.newReg() in
nonSpillL := (tmpReg1 :: (!nonSpillL)) ;
[M.Move(tmpReg1, r), M.Arith1(i, tmpReg1), M.Move(r, tmpReg1)]
end
else
[M.Arith1(i, r)]
| M.Arith2(i,rd,rs) =>
if (RS.member(spills, rs) andalso RS.member(spills, rd))
then let val tmpReg1 = M.newReg(); val tmpReg2 = M.newReg() in
nonSpillL := tmpReg1 :: (tmpReg2 :: (!nonSpillL)) ;
[M.Move(tmpReg1, rs), M.Arith2(i, tmpReg2 ,tmpReg1), M.Move(rd,
tmpReg2)]
end
else if RS.member(spills, rs)
then let val tmpReg1 = M.newReg() in
nonSpillL := (tmpReg1 :: (!nonSpillL)) ;
[M.Move(tmpReg1, rs), M.Arith2(i, rd, tmpReg1)]
end
else if RS.member(spills, rd)
then let val tmpReg1 = M.newReg() in
nonSpillL := (tmpReg1 :: (!nonSpillL)) ;
[M.Arith2(i, tmpReg1, rs), M.Move(rd, tmpReg1)]
end
else [M.Arith2(i, rd, rs)]
| M.Arith3(i,rd,rs,rt) =>
if (RS.member(spills, rs) andalso RS.member(spills, rt) andalso
RS.member(spills, rd))
then let val tmpReg1 = M.newReg(); val tmpReg2 = M.newReg() in
nonSpillL := tmpReg1 :: (tmpReg2 :: (!nonSpillL)) ;
[M.Move(tmpReg2, rs), M.Move(tmpReg1, rt), M.Arith3(i, tmpReg2,
tmpReg2, tmpReg1), M.Move(rd, tmpReg2)]
end
else if (RS.member(spills, rs) andalso RS.member(spills, rd))
then let val tmpReg1 = M.newReg() in
nonSpillL := (tmpReg1 :: (!nonSpillL)) ;
[M.Move(tmpReg1, rs), M.Arith3(i, tmpReg1, tmpReg1, rt),
M.Move(rd, tmpReg1)]
end
else if (RS.member(spills, rt) andalso RS.member(spills, rd))
then let val tmpReg1 = M.newReg() in
nonSpillL := (tmpReg1 :: (!nonSpillL)) ;
[M.Move(tmpReg1, rt), M.Arith3(i, tmpReg1, rs, tmpReg1),
M.Move(rd, tmpReg1)]
end
else if (RS.member(spills, rs) andalso RS.member(spills, rt))
then let val tmpReg1 = M.newReg(); val tmpReg2 = M.newReg() in
nonSpillL := tmpReg1 :: (tmpReg2 :: (!nonSpillL)) ;
[M.Move(tmpReg2, rs), M.Move(tmpReg1, rt), M.Arith3(i, rd, tmpReg2,
tmpReg1)]
end
else if RS.member(spills, rs)
then let val tmpReg1 = M.newReg() in
nonSpillL := (tmpReg1 :: (!nonSpillL)) ;
[M.Move(tmpReg1, rs), M.Arith3(i, rd, tmpReg1, rt)]
end
else if RS.member(spills, rt)
then let val tmpReg1 = M.newReg() in
nonSpillL := (tmpReg1 :: (!nonSpillL)) ;
[M.Move(tmpReg1, rt), M.Arith3(i, rd, rs, tmpReg1)]
end
else if RS.member(spills, rd)
then let val tmpReg1 = M.newReg() in
nonSpillL := (tmpReg1 :: (!nonSpillL)) ;
[M.Arith3(i, tmpReg1, rs, rt), M.Move(rd, tmpReg1)]
end
else [M.Arith3(i, rd, rs, rt)]
| M.Arithi(i,rt,rs,n) =>
if (RS.member(spills, rs) andalso RS.member(spills, rt))
then let val tmpReg1 = M.newReg(); val tmpReg2 = M.newReg() in
nonSpillL := tmpReg1 :: (tmpReg2 :: (!nonSpillL)) ;
[M.Move(tmpReg1, rs), M.Arithi(i, tmpReg2, tmpReg1, n),
M.Move(rt, tmpReg2)]
end
else if RS.member(spills, rs)
then let val tmpReg1 = M.newReg() in
nonSpillL := (tmpReg1 :: (!nonSpillL)) ;
[M.Move(tmpReg1, rs), M.Arithi(i, rt, tmpReg1, n)]
end
else if RS.member(spills, rt)
then let val tmpReg1 = M.newReg() in
nonSpillL := (tmpReg1 :: (!nonSpillL)) ;
[M.Arithi(i, tmpReg1, rs, n), M.Move(rt, tmpReg1)]
end
else [M.Arithi(i, rt, rs, n)]
| M.Li(r,n) =>
if (RS.member(spills, r))
then let val tmpReg1 = M.newReg() in
nonSpillL := (tmpReg1 :: (!nonSpillL)) ;
[M.Li(tmpReg1, n), M.Move(r, tmpReg1)]
end
else [M.Li( r, n)]
| M.La(r,lab) =>
if (RS.member(spills, r))
then let val tmpReg1 = M.newReg() in
nonSpillL := (tmpReg1 :: (!nonSpillL)) ;
[M.La(tmpReg1, lab), M.Move(r, tmpReg1)]
end
else [M.La( r, lab)]
| M.Lw(r,(lab,ra)) =>
if (RS.member(spills, r) andalso RS.member(spills, ra))
then let val tmpReg1 = M.newReg(); val tmpReg2 = M.newReg() in
nonSpillL := tmpReg1 :: (tmpReg2 :: (!nonSpillL)) ;
[M.Move(tmpReg1, ra), M.Lw(tmpReg2, (lab, tmpReg1)), M.Move(r,
tmpReg2)]
end
else if (RS.member(spills, r))
then let val tmpReg1 = M.newReg() in
nonSpillL := (tmpReg1 :: (!nonSpillL)) ;
[M.Lw(tmpReg1, (lab, ra)), M.Move(r, tmpReg1)]
end
else if (RS.member(spills, ra))
then let val tmpReg1 = M.newReg() in
nonSpillL := (tmpReg1 :: (!nonSpillL)) ;
[M.Move(tmpReg1, ra), M.Lw( r, (lab, tmpReg1))]
end
else [M.Lw( r, (lab, ra))]
| M.Sw(r,(lab,ra)) =>
if (RS.member(spills, r) andalso RS.member(spills, ra))
then let val tmpReg1 = M.newReg(); val tmpReg2 = M.newReg() in
nonSpillL := tmpReg1 :: (tmpReg2 :: (!nonSpillL)) ;
[M.Move(tmpReg1, r), M.Move(tmpReg2, ra), M.Sw(tmpReg1, (lab, tmpReg2))]
end
else if (RS.member(spills, r))
then let val tmpReg1 = M.newReg() in
nonSpillL := (tmpReg1 :: (!nonSpillL)) ;
[M.Move(tmpReg1, r), M.Sw(tmpReg1, (lab, ra))]
end
else if (RS.member(spills, ra))
then let val tmpReg1 = M.newReg() in
nonSpillL := (tmpReg1 :: (!nonSpillL)) ;
[M.Move(tmpReg1, ra), M.Sw( r, (lab, tmpReg1))]
end
else [M.Sw( r, (lab, ra))]
| M.Branchz(i,r,lab) =>
if (RS.member(spills, r))
then let val tmpReg1 = M.newReg() in
nonSpillL := (tmpReg1 :: (!nonSpillL)) ;
[M.Move(tmpReg1, r), M.Branchz(i, tmpReg1, lab)]
end
else [M.Branchz(i, r, lab)]
| M.Branch(i,r1,r2,lab) =>
if (RS.member(spills, r1) andalso RS.member(spills, r2))
then let val tmpReg1 = M.newReg(); val tmpReg2 = M.newReg() in
nonSpillL := tmpReg1 :: (tmpReg2 :: (!nonSpillL)) ;
[M.Move(tmpReg1, r1), M.Move(tmpReg2, r2), M.Branch(i, tmpReg1,
tmpReg2, lab)]
end
else if (RS.member(spills, r1))
then let val tmpReg1 = M.newReg() in
nonSpillL := (tmpReg1 :: (!nonSpillL)) ;
[M.Move(tmpReg1, r1), M.Branch(i, tmpReg1, r2, lab)]
end
else if (RS.member(spills, r2))
then let val tmpReg1 = M.newReg() in
nonSpillL := (tmpReg1 :: (!nonSpillL)) ;
[M.Move(tmpReg1, r2), M.Branch(i, r1, tmpReg1, lab)]
end
else [M.Branch(i, r1, r2, lab)]
| M.J(lab) => [M.J lab]
| M.Jal(lab) => [M.Jal lab]
| M.Jr(r,also) =>
if (RS.member(spills, r))
then let val tmpReg1 = M.newReg() in
nonSpillL := (tmpReg1 :: (!nonSpillL)) ;
[M.Move(tmpReg1, r), M.Jr(tmpReg1, also)]
end
else [M.Jr(r, also)]
| M.Jalr(r1,r2,use,def) =>
if (RS.member(spills, r1) andalso RS.member(spills, r2))
then let val tmpReg1 = M.newReg(); val tmpReg2 = M.newReg() in
nonSpillL := tmpReg1 :: (tmpReg2 :: (!nonSpillL)) ;
[M.Move(tmpReg1, r2), M.Jalr(tmpReg2, tmpReg1, use,
def), M.Move(r1,tmpReg2)]
end
else if RS.member(spills, r2)
then let val tmpReg1 = M.newReg() in
nonSpillL := (tmpReg1 :: (!nonSpillL)) ;
[M.Move(tmpReg1, r2), M.Jalr(r1, tmpReg1, use, def)]
end
else if RS.member(spills, r1)
then let val tmpReg1 = M.newReg() in
nonSpillL := (tmpReg1 :: (!nonSpillL)) ;
[M.Jalr(tmpReg1, r2, use, def), M.Move(r1, tmpReg1)]
end
else [M.Jalr(r1, r2, use, def)]
| M.Syscall => [M.Syscall]
| M.Nop => [M.Nop]
| M.Leave => [M.Leave]
| M.Ret => [M.Ret]
| M.Push(r) =>
if RS.member(spills, r)
then let val tmpReg1 = M.newReg() in
nonSpillL := (tmpReg1 :: (!nonSpillL)) ;
[M.Move(tmpReg1, r), M.Push(tmpReg1)]
end
else
[M.Push(r)]
| M.Pop(r) =>
if RS.member(spills, r)
then let val tmpReg1 = M.newReg() in
nonSpillL := (tmpReg1 :: (!nonSpillL)) ;
[M.Pop(tmpReg1), M.Move(r, tmpReg1)]
end
else
[M.Pop(r)]
| M.Branch2(c, l) => [M.Branch2(c, l)]
(* make restore $sp register in function epilog *)
fun make_end (instrL : M.funcode, index) =
case instrL of
(l, instrs)::[] => if (!index) = ~1 then [(l, instrs)] else [(l,
instrs)]
| h::t => h :: make_end (t,index)
fun make_first (insrL : M.instruction list) (first:M.instruction list) index =
case insrL of
instr :: t =>
(case instr of
M.Move(r1, r2) =>
(if(r1 = M.reg "%ebp" andalso r2 = M.reg "%esp")
then
first @ (instr :: ((M.Arithi(M.Addi, M.reg "%esp", M.reg "%esp", M.immed (~((!index + 1)*4)))) :: t))
else
make_first t (first @ [instr]) index)
| _ => make_first t (first @ [instr]) index)
| [] => first
fun alloc(instrL as ((funlab,block)::rest) : M.funcode) =
let
val index = ref (~1)
(* make instruction that was made by 2nd general spill method *)
fun alloc_sub (instrL as ((funlab, block) :: rest)) =
let
val nonSpillL : M.reg list ref = ref []
fun spillCost x = if(List.exists (fn reg => reg = x) (!nonSpillL)) then
Color.spillCostInfinity else 1
val spillL : (int * M.reg) list ref = ref []
val ig = Liveness.interference_graph instrL
val movegraph = IG.newGraph()
val _ = app (fn (_,l) => app (getmove movegraph) l) instrL
(*val _ = print "###### Move graph\n"
val _ = Liveness.printgraph print movegraph*)
val palette = M.list2set (M.reg"%eip"::M.callerSaved @
M.calleeSaved)
val coloring = Color.color {interference = ig, moves=movegraph,
spillCost = spillCost, palette=palette}
val _ = Color.verify{complain=ErrorMsg.impossible, func=instrL,
spillCost=spillCost, palette=palette,
coloring=coloring}
(*val _ = print "Register Allocation verified.\n"*)
val {alloc,spills} = coloring
(*val _ = (print "Spills: ";
RS.app (fn r => (print (M.reg2name r); print " ")) spills;
print "\n")*)
in
if(RS.isEmpty spills) then (instrL, alloc)
else
let
val instrL = List.map (fn (l,instrs) => (l, flat(List.map (rename_spills
(spills, spillL, index, nonSpillL)) instrs))) instrL
val instrL = (List.map (fn (l,instrs) => (l, flat(List.map (after_spills
(spills, spillL, index)) instrs))) instrL);
(* this is loop function when spillset isn't empty. If spillset is empty, it finish. *)
fun loop spillset (finalinstrL, finalAlloc) =
if(RS.isEmpty (spillset)) then (finalinstrL, finalAlloc)
else
let
val ig = Liveness.interference_graph (finalinstrL)
val movegraph = IG.newGraph()
val _ = app (fn (_, l) => app (getmove movegraph) l) (finalinstrL)
val coloring = Color.color {interference = ig, moves=movegraph,
spillCost = spillCost, palette=palette}
val _ = Color.verify{complain=ErrorMsg.impossible, func=(finalinstrL),
spillCost=spillCost, palette=palette,
coloring=coloring}
val {alloc, spills} = coloring
in
if(RS.isEmpty spills) then (finalinstrL, alloc)
else (
let
val instrL = (List.map (fn (l,instrs) => (l, flat(List.map (rename_spills
(spills, spillL, index, nonSpillL)) instrs))) (finalinstrL))
val instrL = (List.map (fn (l,instrs) => (l, flat(List.map (after_spills
(spills, spillL, index)) instrs))) instrL);
(*val _ = print("hi\n") *)
in
loop spills (instrL, alloc)
end )
end
in
loop spills (instrL, alloc)
end
end
in
let
val (finalinstr, finalalloc) = alloc_sub instrL
val finalinstrL = List.map (fn (l, instrs) => (l, List.map (M.rename_regs finalalloc) instrs)) (finalinstr)
in
(* make $sp := $sp - spill registers number *)
let
val ((funlab, block)::rest) = finalinstrL;
val st_instrL =
if (!index) = ~1 then finalinstrL
else (funlab, make_first block [] index)::rest
val ed_instrL = make_end (st_instrL, index)
in
ed_instrL
end
end
end
end