Debian Codegraph @

lib/mlrisc-lib/MLRISC/ra/ra-spill.sml

raw
1 (*
2 * This module manages the spill/reload process.
3 * The reason this is detached from the main module is that
4 * I can't understand the old code.
5 *
6 * Okay, now I understand the code.
7 *
8 * The new code does things slightly differently.
9 * Here, we are given an instruction and a list of registers to spill
10 * and reload. We rewrite the instruction until all instances of these
11 * registers are rewritten.
12 *
13 * (12/13/99) Some major caveats when spill coalescing/coloring is used:
14 * When parallel copies are generated and spill coalescing/coloring is used,
15 * two special cases have to be identified:
16 *
17 * Case 1 (spillLoc dst = spillLoc src)
18 * Suppose we have a parallel copy
19 * (u,v) <- (x,y)
20 * where u has to be spilled and y has to reloaded. When both
21 * u and y are mapped to location M. The following wrong code may
22 * be generated:
23 * M <- x (spill u)
24 * v <- M (reload y)
25 * This is incorrect. Instead, we generate a dummy copy and
26 * delay the spill after the reload, like this:
27 *
28 * tmp <- x (save value of u)
29 * v <- M (reload y)
30 * M <- tmp (spill u)
31 * Case 2 (spillLoc copyTmp = spillLoc src)
32 * Another case that can cause problems is when the spill location of
33 * the copy temporary is the same as that of one of the sources:
34 *
35 * (a, b, v) <- (b, a, u) where spillLoc(u) = spillLoc(tmp) = v
36 *
37 * The incorrect code is
38 * (a, b) <- (b, a)
39 * v <- M
40 * But then the shuffle code for the copy can clobber the location M.
41 *
42 * tmp <- M
43 * (a, b) <- (b, a)
44 * v <- tmp
45 *
46 * (Note that spillLoc copyTmp = spillLoc src can never happen)
47 *
48 * -- Allen
49 *)
50
51 functor RASpill
52 (structure InsnProps : INSN_PROPERTIES
53 structure Asm : INSTRUCTION_EMITTER (* where I = InsnProps.I *)
54 where type I.addressing_mode = InsnProps.I.addressing_mode
55 and type I.ea = InsnProps.I.ea
56 and type I.instr = InsnProps.I.instr
57 and type I.instruction = InsnProps.I.instruction
58 and type I.operand = InsnProps.I.operand
59 ) : RA_SPILL =
60 struct
61 val debug = false
62
63 structure I = InsnProps.I
64 structure P = InsnProps
65 structure C = I.C
66 structure Core = RACore
67 structure G = Core.G
68 structure CB = CellsBasis
69
70 fun error msg = MLRiscErrorMsg.error("RASpill",msg)
71
72 val keep_dead_copies =
73 MLRiscControl.mkFlag
74 ("ra-preserve-dead-copies",
75 "Dead copies are not removed when spilling")
76
77 fun dec1 n = Word.toIntX(Word.fromInt n - 0w1)
78 fun dec{block,insn} = {block=block,insn=dec1 insn}
79
80 structure T = RASpillTypes(I)
81 open T
82
83 fun uniq s = CB.SortedCells.return(CB.SortedCells.uniq s)
84 val i2s = Int.toString
85 fun pt2s{block,insn} = "b"^i2s block^":"^i2s insn
86
87 val Asm.S.STREAM{emit, ...} = Asm.makeStream[]
88
89 (* val spilledCopyTmps = MLRiscControl.getCounter "ra-spilled-copy-temps" *)
90
91 (*
92 * The following function performs spilling.
93 *)
94 fun spillRewrite
95 {graph=G as G.GRAPH{showReg, spilledRegs, nodes, mode, ...},
96 spill : spill,
97 spillCopyTmp : spillCopyTmp,
98 spillSrc : spillSrc,
99 renameSrc : renameSrc,
100 reload : reload,
101 reloadDst : reloadDst,
102 copyInstr : copyInstr,
103 cellkind,
104 spillSet, reloadSet, killSet
105 } =
106 let
107 (* Must do this to make sure the interference graph is
108 * reflected to the cells
109 *)
110 val _ = Core.updateCellAliases G
111
112 val getSpillLoc = Core.spillLoc G
113 fun spillLocOf(CB.CELL{id, ...}) = getSpillLoc id
114 val spillLocsOf = map spillLocOf
115 val getnode = IntHashTable.lookup nodes
116 val getnode = fn CB.CELL{id, ...} => getnode id
117
118 val insnDefUse = P.defUse cellkind
119
120 (* Merge prohibited registers *)
121 val enterSpill = IntHashTable.insert spilledRegs
122 val addProh = app (fn c => enterSpill(CellsBasis.registerId c,true))
123
124 val getSpills = G.PPtHashTable.find spillSet
125 val getSpills = fn p => case getSpills p of SOME s => s | NONE => []
126 val getReloads = G.PPtHashTable.find reloadSet
127 val getReloads = fn p => case getReloads p of SOME s => s | NONE => []
128 val getKills = G.PPtHashTable.find killSet
129 val getKills = fn p => case getKills p of SOME s => s | NONE => []
130
131 fun getLoc(G.NODE{color=ref(G.ALIASED n), ...}) = getLoc n
132 | getLoc(G.NODE{color=ref(G.MEMREG(_, m)), ...}) = G.MEM_REG m
133 | getLoc(G.NODE{color=ref(G.SPILL_LOC s), ...}) = G.FRAME s
134 | getLoc(G.NODE{color=ref(G.SPILLED), number, ...}) = G.FRAME number
135 | getLoc(G.NODE{color=ref(G.PSEUDO), number, ...}) = G.FRAME number
136 | getLoc _ = error "getLoc"
137
138 fun printRegs regs =
139 app (fn r => print(CellsBasis.toString r^" ["^
140 Core.spillLocToString G (CellsBasis.cellId r)^"] ")) regs
141
142 val parallelCopies = Word.andb(Core.HAS_PARALLEL_COPIES, mode) <> 0w0
143
144 fun chase(CB.CELL{col=ref(CB.ALIASED c), ...}) = chase c
145 | chase c = c
146
147 fun cellId(CB.CELL{id, ...}) = id
148
149 fun sameCell(CB.CELL{id=x,...}, CB.CELL{id=y, ...}) = x=y
150
151 fun same(x,regToSpill) = sameCell(chase x,regToSpill)
152
153 (*
154 * Rewrite the instruction given that a bunch of registers have
155 * to be spilled and reloaded.
156 *)
157 fun spillRewrite{pt, instrs, annotations} =
158 let
159 (*
160 * Insert reloading code for an instruction.
161 * Note: reload code goes after the instruction, if any.
162 *)
163 fun reloadInstr(instr,regToSpill,spillLoc) =
164 let val {code, proh, newReg} =
165 reload{instr=instr,reg=regToSpill,
166 spillLoc=spillLoc,annotations=annotations}
167 in addProh(proh);
168 code
169 end
170
171 (*
172 * Renaming the source for an instruction.
173 *)
174 fun renameInstr(instr,regToSpill,toSrc) =
175 let val {code, proh, newReg} =
176 renameSrc{instr=instr, fromSrc=regToSpill,toSrc=toSrc}
177 in addProh(proh);
178 code
179 end
180
181 (*
182 * Remove uses of regToSpill from a set of parallel copies.
183 * If there are multiple uses, then return multiple moves.
184 *)
185 fun extractUses(regToSpill, rds, rss) =
186 let fun loop(rd::rds, rs::rss, newRds, rds', rss') =
187 if same(rs,regToSpill) then
188 loop(rds, rss, rd::newRds, rds', rss')
189 else
190 loop(rds, rss, newRds, rd::rds', rs::rss')
191 | loop(_, _, newRds, rds', rss') = (newRds, rds', rss')
192 in loop(rds, rss, [], [], []) end
193
194 (*
195 * Insert reload code for the sources of a copy.
196 * Transformation:
197 * d1..dn <- s1..sn
198 * =>
199 * d1..dn/r <- s1...sn/r.
200 * reload code
201 * reload copies
202 *
203 *)
204 fun reloadCopySrc(instr,regToSpill,spillLoc) =
205 let val (dst, src) = P.moveDstSrc instr
206 val (rds, copyDst, copySrc) = extractUses(regToSpill, dst, src)
207 fun processMoves([], reloadCode) = reloadCode
208 | processMoves(rd::rds, reloadCode) =
209 let val code =
210 reloadDst{spillLoc=spillLoc,reg=regToSpill,
211 dst=rd,annotations=annotations}
212 in processMoves(rds, code@reloadCode)
213 end
214 val reloadCode = processMoves(rds, [])
215 in case copyDst of
216 [] => reloadCode
217 | _ => copyInstr((copyDst, copySrc), instr) @ reloadCode
218 end
219
220 (*
221 * Insert reload code
222 *)
223 fun reload(instr,regToSpill,spillLoc) =
224 if P.moveInstr instr then
225 reloadCopySrc(instr,regToSpill,spillLoc)
226 else
227 reloadInstr(instr,regToSpill,spillLoc)
228
229 (*
230 * Check whether the id is in a list
231 *)
232 fun containsId(id,[]) = false
233 | containsId(id:CB.cell_id,r::rs) = r = id orelse containsId(id,rs)
234 fun spillConflict(G.FRAME loc, rs) = containsId(~loc, rs)
235 | spillConflict(G.MEM_REG(CB.CELL{id, ...}), rs) =
236 containsId(id, rs)
237
238 fun contains(r',[]) = false
239 | contains(r',r::rs) = sameCell(r',r) orelse contains(r',rs)
240
241 (*
242 * Insert spill code for an instruction.
243 * Spill code occur after the instruction.
244 * If the value in regToSpill is never used, the client also
245 * has the opportunity to remove the instruction.
246 *)
247 fun spillInstr(instr,regToSpill,spillLoc,kill) =
248 let val {code, proh, newReg} =
249 spill{instr=instr,
250 kill=kill, spillLoc=spillLoc,
251 reg=regToSpill, annotations=annotations}
252 in addProh(proh);
253 code
254 end
255
256 (* Remove the definition regToSpill <- from
257 * parallel copies rds <- rss.
258 * Note, there is a guarantee that regToSpill is not aliased
259 * to another register in the rds set.
260 *)
261 fun extractDef(regToSpill,rds,rss,kill) =
262 let fun loop(rd::rds, rs::rss, rds', rss') =
263 if spillLocOf rd = spillLocOf rs then
264 (rs, rds@rds', rss@rss', true)
265 else if same(rd, regToSpill) then
266 (rs, rds@rds', rss@rss', kill)
267 else loop(rds, rss, rd::rds', rs::rss')
268 | loop _ =
269 (print("rds=");
270 app (fn r => print(CellsBasis.toString r^":"^
271 i2s(spillLocOf r)^" ")) rds;
272 print("\nrss=");
273 app (fn r => print(CellsBasis.toString r^":"^
274 i2s(spillLocOf r)^" ")) rss;
275 print "\n";
276 error("extractDef: "^CellsBasis.toString regToSpill))
277 in loop(rds, rss, [], []) end
278
279 (*
280 * Insert spill code for a destination of a copy
281 * suppose d = r and we have a copy d <- s in
282 * d1...dn <- s1...sn
283 *
284 * d1...dn <- s1...sn
285 * =>
286 * spill s to spillLoc
287 * d1...dn/d <- s1...sn/s
288 *
289 * However, if the spill code may ovewrite the spill location
290 * shared by other uses, we do the following less
291 * efficient scheme:
292 *
293 * /* save the result of d */
294 * d1...dn, tmp <- s1...sn, s
295 * spill tmp to spillLoc /* spill d */
296 *
297 *)
298 fun spillCopyDst(instr,regToSpill,spillLoc,kill,don'tOverwrite) =
299 let val (dst, src) = P.moveDstSrc instr
300 val (mvSrc,copyDst,copySrc,kill) =
301 extractDef(regToSpill,dst,src,kill)
302 val copy = case copyDst of
303 [] => []
304 | _ => copyInstr((copyDst,copySrc),instr)
305 in
306 if kill andalso not(!keep_dead_copies)
307 then (* kill the move *)
308 ((* print ("Copy "^Int.toString(hd mvDst)^" <- "^
309 Int.toString(hd mvSrc)^" removed\n"); *)
310 copy
311 )
312 else (* normal spill *)
313 if spillConflict(spillLoc, don'tOverwrite) then
314 let (* cycle found *)
315 (*val _ = print("Register r"^Int.toString regToSpill^
316 " overwrites ["^Int.toString spillLoc^"]\n")*)
317 val tmp = C.newVar regToSpill (* new temporary *)
318 val copy = copyInstr((tmp::copyDst, mvSrc::copySrc),
319 instr)
320 val spillCode = spillSrc{src=tmp,reg=regToSpill,
321 spillLoc=spillLoc,
322 annotations=annotations}
323 in copy @ spillCode
324 end
325 else
326 let (* spill the move instruction *)
327 val spillCode = spillSrc{src=mvSrc,reg=regToSpill,
328 spillLoc=spillLoc,
329 annotations=annotations}
330 in spillCode @ copy
331 end
332 end
333
334 (*
335 * Insert spill code for a copy
336 *)
337 fun spillCopy(instr,regToSpill,spillLoc,kill,don'tOverwrite) =
338 case P.moveTmpR instr of
339 NONE => spillCopyDst(instr,regToSpill,spillLoc,kill,
340 don'tOverwrite)
341 | SOME tmp =>
342 if same(tmp, regToSpill)
343 then ((* spilledCopyTmps := !spilledCopyTmps + 1; *)
344 [spillCopyTmp{copy=instr, reg=regToSpill,
345 spillLoc=spillLoc,
346 annotations=annotations}])
347 else spillCopyDst(instr,regToSpill,spillLoc,kill,
348 don'tOverwrite)
349
350 (*
351 * Insert spill code
352 *)
353 fun spill(instr,regToSpill,spillLoc,killSet,don'tOverwrite) =
354 let val kill = contains(regToSpill,killSet)
355 in if P.moveInstr instr then
356 spillCopy(instr,regToSpill,spillLoc,kill,don'tOverwrite)
357 else
358 spillInstr(instr,regToSpill,spillLoc,kill)
359 end
360
361 fun contains([],reg) = false
362 | contains(r::rs,reg) = same(r,reg) orelse contains(rs,reg)
363 fun hasDef(i,reg) = contains(#1(insnDefUse i),reg)
364 fun hasUse(i,reg) = contains(#2(insnDefUse i),reg)
365
366 fun spillOneReg([],_,_,_,_) = []
367 | spillOneReg(i::instrs,r,spillLoc,killSet,don'tOverwrite) =
368 if hasDef(i,r)
369 then
370 spillOneReg(spill(i,r,spillLoc,killSet,don'tOverwrite)@instrs,
371 r,spillLoc,killSet,don'tOverwrite)
372 else i::spillOneReg(instrs,r,spillLoc,killSet,don'tOverwrite)
373
374 fun reloadOneReg([],_,_) = []
375 | reloadOneReg(i::instrs,r,spillLoc) =
376 if hasUse(i,r)
377 then reloadOneReg(reload(i,r,spillLoc)@instrs,
378 r,spillLoc)
379 else i::reloadOneReg(instrs,r,spillLoc)
380
381 (* This function spills a set of registers for an instruction *)
382 fun spillAll(instrs,[],killSet,don'tOverwrite) = instrs
383 | spillAll(instrs,r::rs,killSet,don'tOverwrite) =
384 let val node = getnode r
385 val spillLoc = getLoc node
386 in spillAll(
387 spillOneReg(instrs,r,spillLoc,killSet,don'tOverwrite),
388 rs,killSet,don'tOverwrite)
389 end
390
391 (* This function reloads a set of registers for an instruction *)
392 fun reloadAll(instrs,[]) = instrs
393 | reloadAll(instrs,r::rs) =
394 let val node = getnode r
395 val spillLoc = getLoc node
396 in reloadAll(reloadOneReg(instrs,r,spillLoc),rs)
397 end
398
399 fun loop([], pt, newInstrs) = newInstrs
400 | loop(instr::rest, pt, newInstrs) =
401 let val spillRegs = getSpills pt
402 val reloadRegs = getReloads pt
403 in case (spillRegs, reloadRegs) of
404 ([], []) => loop(rest, dec pt, instr::newInstrs)
405 | _ =>
406 (* Eliminate duplicates from the spill/reload candidates *)
407 let val killRegs = getKills pt
408 val spillRegs = uniq spillRegs
409 val reloadRegs = uniq reloadRegs
410
411 (* spill locations that we can't overwrite if we
412 * are spilling a parallel copy
413 *)
414 val don'tOverwrite =
415 if parallelCopies then spillLocsOf reloadRegs
416 else []
417
418 val instrs = spillAll([instr],spillRegs,killRegs,
419 don'tOverwrite)
420
421 val _ = if debug then
422 (print("pt="^pt2s pt^"\n");
423 case spillRegs of
424 [] => ()
425 | _ => (print("Spilling ");
426 printRegs spillRegs;
427 print "\n");
428 case reloadRegs of
429 [] => ()
430 | _ => (print("Reloading ");
431 printRegs reloadRegs;
432 print "\n");
433 print "Before:"; emit instr
434 ) else ()
435
436 val instrs = reloadAll(instrs,reloadRegs)
437
438 val _ = if debug then
439 (print "After:"; app emit instrs;
440 print "------------------\n")
441 else ()
442
443 fun concat([], l) = l
444 | concat(a::b, l) = concat(b, a::l)
445 in loop(rest, dec pt, concat(instrs, newInstrs))
446 end
447 end
448 in loop(rev instrs, pt, [])
449 end
450 in spillRewrite
451 end
452 end