Home My Page Projects Code Snippets Project Openings SML/NJ
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files

SCM Repository

[smlnj] Annotation of /sml/trunk/src/MLRISC/ra/ra-core.sml
ViewVC logotype

Annotation of /sml/trunk/src/MLRISC/ra/ra-core.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 498 - (view) (download)
Original Path: sml/branches/SMLNJ/src/MLRISC/ra/ra-core.sml

1 : monnier 427 (*
2 :     *
3 : monnier 469 * Overview
4 :     * ========
5 :     * This implementation of iterated coalescing differ from the old one in
6 :     * various substantial ways:
7 :     *
8 :     * 1. The move list is prioritized. Higher ranking moves are coalesced first.
9 :     * This tends to favor coalescing of moves that has higher priority.
10 :     *
11 :     * 2. The freeze list is prioritized. Lower ranking nodes are unfrozen
12 :     * first. Since freeze disable moves, this tends to disable moves
13 :     * of low priority.
14 :     *
15 :     * 3. The simplify worklist is not kept explicitly during the
16 :     * simplify/coalesce/freeze phases. Instead, whenever a non-move
17 :     * related node with degree < K is discovered, we call simplify
18 :     * to remove it from the graph immediately.
19 :     *
20 :     * I think this has a few advantages.
21 :     * (a) There is less bookkeeping.
22 :     * (b) Simplify adds coalescable moves to the move list.
23 :     * By doing simplify eagerly, moves are added to the move list
24 :     * faster, allowing higher ranking moves to ``preempt'' low
25 :     * ranking moves.
26 :     *
27 :     * 4. Support for register pairs
28 :     *
29 :     * Important Invariants
30 :     * ====================
31 :     * 1. Adjacency list
32 :     * a. All nodes on the adjacency list are distinct
33 :     * b. nodes with color ALIASED or REMOVED are NOT consider to be
34 :     * on the adjacency list
35 :     * c. If a node x is COLORED, then we DON'T keep track of
36 :     * its adjacency list
37 :     * d. When a node has been removed, there aren't any moves associated
38 :     * with it.
39 :     * 2. Moves
40 :     * a. Moves marked WORKLIST are on the worklist.
41 :     * b. Moves marked MOVE are NOT on the worklist.
42 :     * c. Moves marked LOST are frozen and are in fact never considered again.
43 :     * d. Moves marked CONSTRAINED cannot be coalesced because the src and dst
44 :     * interfere
45 :     * e. Moves marked COALESCED have been coalesced.
46 :     * f. The movecnt in a node is always the number of nodes
47 :     * currently marked as WORKLIST or MOVE, i.e. the moves that
48 :     * are associated with the node. When this is zero, the node is
49 :     * considered to be non-move related.
50 :     * g. Moves on the move worklist are always distinct.
51 :     * 3.
52 :     *
53 :     * Allen.
54 :     *
55 : monnier 427 *)
56 :    
57 :     structure RACore : RA_CORE =
58 :     struct
59 :    
60 : monnier 469 structure G = RAGraph
61 :     structure A = Array
62 :     structure W = Word
63 :     structure W8A = Word8Array
64 :     structure W8 = Word8
65 : monnier 427
66 : monnier 469 (* For debugging, uncomment Unsafe. *)
67 :     structure UA = Unsafe.Array
68 :     structure UW8A = Unsafe.Word8Array
69 : monnier 427
70 : monnier 469 open G
71 : monnier 427
72 : monnier 498 val debug = false
73 :     (* val tally = false *)
74 :    
75 :    
76 : monnier 475 val verbose = MLRiscControl.getFlag "ra-verbose"
77 :     val ra_spill_coal = MLRiscControl.getCounter "ra-spill-coalescing"
78 :     val ra_spill_prop = MLRiscControl.getCounter "ra-spill-propagation"
79 : monnier 469
80 : monnier 498 (*
81 :     val good_briggs = MLRiscControl.getCounter "good-briggs"
82 :     val bad_briggs = MLRiscControl.getCounter "bad-briggs"
83 :     val good_george = MLRiscControl.getCounter "good-george"
84 :     val bad_george = MLRiscControl.getCounter "bad-george"
85 :     val good_freeze = MLRiscControl.getCounter "good-freeze"
86 :     val bad_freeze = MLRiscControl.getCounter "bad-freeze"
87 :     *)
88 :    
89 :     val NO_OPTIMIZATION = 0w0
90 :     val BIASED_SELECTION = 0w1
91 :     val DEAD_COPY_ELIM = 0w2
92 :     val COMPUTE_SPAN = 0w4
93 :     val SAVE_COPY_TEMPS = 0w8
94 :    
95 :     local
96 :    
97 :     fun isOn(flag,mask) = Word.andb(flag,mask) <> 0w0
98 :    
99 : monnier 469 fun error msg = MLRiscErrorMsg.error("RACore", msg)
100 :    
101 :     (* No overflow checking necessary here *)
102 :     fun x + y = W.toIntX(W.+(W.fromInt x, W.fromInt y))
103 :     fun x - y = W.toIntX(W.-(W.fromInt x, W.fromInt y))
104 :    
105 : monnier 475 fun concat([], b) = b
106 :     | concat(x::a, b) = concat(a, x::b)
107 :    
108 : monnier 498 in
109 :    
110 : monnier 469 (*
111 :     * Bit Matrix routines
112 : monnier 427 *)
113 : monnier 469 structure BM =
114 :     struct
115 :     fun hashFun(i, j, shift, size) =
116 : monnier 498 let val i = W.fromInt i
117 :     val j = W.fromInt j
118 :     val h = W.+(W.<<(i, shift), W.+(i, j))
119 :     val mask = W.-(W.fromInt size, 0w1)
120 :     in W.toIntX(W.andb(h, mask)) end
121 : monnier 427
122 : monnier 469 val empty = BM{table=SMALL(ref(A.array(0, [])), 0w0), elems=ref 0, edges=0}
123 : monnier 427
124 : monnier 469 (*
125 :     val indices = A.array(1024,0)
126 : monnier 427
127 : monnier 469 fun init(i,j) =
128 :     if i < 1024 then
129 :     (A.update(indices, i, j); init(i+1, i+j+1))
130 :     else ()
131 :    
132 :     val _ = init(0, 0)
133 :     *)
134 :    
135 :     fun edges(BM{table=SMALL(ref table, _), ...}) = A.length table
136 :     | edges(BM{table=LARGE(ref table, _), ...}) = A.length table
137 :     (*| edges(BM{table=BITMATRIX _, edges, ...}) = edges *)
138 :    
139 :     fun member(BM{table=SMALL(table, shift), ...}) =
140 : monnier 498 (fn (i, j) =>
141 : monnier 469 let val (i,j) = if i < j then (i, j) else (j, i)
142 :     val k = W.+(W.<<(W.fromInt i, 0w15), W.fromInt j)
143 :     fun find [] = false
144 :     | find(k'::b) = k = k' orelse find b
145 :     val tab = !table
146 :     in find(UA.sub(tab, hashFun(i, j, shift, A.length tab))) end
147 :     )
148 :     | member(BM{table=LARGE(table, shift), ...}) =
149 : monnier 498 (fn (i, j) =>
150 : monnier 469 let val (i,j) = if i < j then (i, j) else (j, i)
151 :     fun find NIL = false
152 :     | find(B(i',j',b)) = i = i' andalso j = j' orelse find b
153 :     val tab = !table
154 :     in find(UA.sub(tab, hashFun(i, j, shift, A.length tab))) end
155 :     )
156 : monnier 498 (*
157 : monnier 469 | member(BM{table=BITMATRIX table, ...}) =
158 : monnier 498 (fn (i, j) =>
159 : monnier 469 let val (i,j) = if i > j then (i, j) else (j, i)
160 :     val bit = W.fromInt(UA.sub(indices, i) + j)
161 :     val index = W.toIntX(W.>>(bit, 0w3))
162 :     val mask = W.<<(0w1, W.andb(bit, 0w7))
163 :     in W.andb(W.fromInt(W8.toInt(UW8A.sub(table, index))), mask) <> 0w0
164 :     end
165 :     )
166 : monnier 498 *)
167 : monnier 469
168 :     fun add (BM{table=SMALL(table, shift), elems, ...}) =
169 :     let fun insert(i, j) =
170 :     let val (i,j) = if i < j then (i, j) else (j, i)
171 :     val tab = !table
172 :     val len = A.length tab
173 :     in if !elems < len then
174 :     let val index = hashFun(i, j, shift, len)
175 :     val k = W.+(W.<<(W.fromInt i, 0w15), W.fromInt j)
176 :     fun find [] = false
177 :     | find(k'::b) = k = k' orelse find b
178 :     val b = UA.sub(tab, index)
179 :     in if find b then false
180 :     else (UA.update(tab, index, k::b);
181 :     elems := !elems + 1; true)
182 :     end
183 :     else (* grow table *)
184 :     let val oldTable = tab
185 :     val oldSize = A.length oldTable
186 :     val newSize = oldSize + oldSize
187 :     val newTable = A.array(newSize,[])
188 :     fun enter n =
189 :     if n < oldSize then
190 :     let fun loop([],a,b) =
191 :     (UA.update(newTable, n, a);
192 :     UA.update(newTable, n + oldSize, b);
193 :     enter(n+1))
194 :     | loop(k::l,a,b) =
195 :     let val i = W.toIntX(W.>>(k, 0w15))
196 :     val j = W.toIntX(W.-(k,W.<<(W.fromInt i, 0w15)))
197 :     in if hashFun(i, j, shift, newSize) = n
198 :     then loop(l, k::a, b)
199 :     else loop(l, a, k::b)
200 :     end
201 :     in loop(UA.sub(oldTable, n), [], []) end
202 :     else ()
203 :     in table := newTable;
204 :     enter 0;
205 :     insert(i, j)
206 :     end
207 :     end
208 :     in insert
209 :     end
210 :     | add (BM{table=LARGE(table, shift), elems, ...}) =
211 :     let fun insert(i, j) =
212 :     let val (i,j) = if i < j then (i, j) else (j, i)
213 :     val tab = !table
214 :     val len = A.length tab
215 :     in if !elems < len then
216 : monnier 498 let fun hashFun(i, j, shift, size) =
217 :     let val i = W.fromInt i
218 :     val j = W.fromInt j
219 :     val h = W.+(W.<<(i, shift), W.+(i, j))
220 :     val mask = W.-(W.fromInt size, 0w1)
221 :     in W.toIntX(W.andb(h, mask)) end
222 :     val index = hashFun(i, j, shift, len)
223 : monnier 469 fun find NIL = false
224 :     | find(B(i',j',b)) = i = i' andalso j = j' orelse find b
225 :     val b = UA.sub(tab, index)
226 :     in if find b then false
227 :     else (UA.update(tab, index, B(i,j,b));
228 :     elems := !elems + 1; true)
229 :     end
230 :     else (* grow table *)
231 :     let val oldTable = tab
232 :     val oldSize = A.length oldTable
233 :     val newSize = oldSize + oldSize
234 :     val newTable = A.array(newSize,NIL)
235 :     fun enter n =
236 :     if n < oldSize then
237 :     let fun loop(NIL,a,b) =
238 :     (UA.update(newTable, n, a);
239 :     UA.update(newTable, n + oldSize, b);
240 :     enter(n+1))
241 :     | loop(B(i,j,l),a,b) =
242 :     if hashFun(i, j, shift, newSize) = n
243 :     then loop(l, B(i,j,a), b)
244 :     else loop(l, a, B(i,j,b))
245 :     in loop(UA.sub(oldTable, n), NIL, NIL) end
246 :     else ()
247 :     in table := newTable;
248 :     enter 0;
249 :     insert(i, j)
250 :     end
251 :     end
252 :     in insert
253 :     end
254 : monnier 498 (*
255 : monnier 469 | add(BM{table=BITMATRIX table, ...}) =
256 :     (fn (i, j) =>
257 :     let val (i,j) = if i > j then (i, j) else (j, i)
258 :     val bit = W.fromInt(UA.sub(indices, i) + j)
259 :     val index = W.toIntX(W.>>(bit, 0w3))
260 :     val mask = W.<<(0w1, W.andb(bit, 0w7))
261 :     val value = W.fromInt(W8.toInt(UW8A.sub(table, index)))
262 :     in if W.andb(value, mask) <> 0w0 then false
263 :     else (UW8A.update(table, index,
264 :     W8.fromInt(W.toIntX(W.orb(value, mask)))); true)
265 :     end
266 :     )
267 : monnier 498 *)
268 : monnier 469
269 :     fun delete (BM{table=SMALL(table, shift), elems, ...}) =
270 :     (fn (i,j) =>
271 :     let val k = W.+(W.<<(W.fromInt i, 0w15), W.fromInt j)
272 :     fun find [] = []
273 :     | find(k'::b) =
274 :     if k = k' then (elems := !elems - 1; b) else find b
275 :     val tab = !table
276 :     val index = hashFun(i, j, shift, A.length tab)
277 :     val n = !elems
278 :     in UA.update(tab, index, find(UA.sub(tab, index)));
279 :     !elems <> n
280 :     end
281 :     )
282 :     | delete (BM{table=LARGE(table, shift), elems, ...}) =
283 :     (fn (i,j) =>
284 :     let fun find NIL = NIL
285 :     | find(B(i', j', b)) =
286 :     if i = i' andalso j = j' then (elems := !elems - 1; b)
287 :     else B(i', j', find b)
288 :     val tab = !table
289 :     val index = hashFun(i, j, shift, A.length tab)
290 :     val n = !elems
291 :     in UA.update(tab, index, find(UA.sub(tab, index)));
292 :     !elems <> n
293 :     end
294 :     )
295 :     (*
296 :     | delete(BM{table=BITMATRIX table, ...}) =
297 :     (fn (i, j) =>
298 :     let val (i,j) = if i > j then (i, j) else (j, i)
299 :     val bit = W.fromInt(UA.sub(indices, i) + j)
300 :     val index = W.toIntX(W.>>(bit, 0w3))
301 :     val mask = W.-(W.<<(0w1, W.andb(bit, 0w7)), 0w1)
302 :     val value = W.fromInt(W8.toInt(UW8A.sub(table, index)))
303 :     in if W.andb(value, mask) = 0w0 then false
304 :     else (UW8A.update(table, index,
305 :     W8.fromInt(W.toIntX(W.andb(value,W.notb mask))));
306 :     true)
307 :     end
308 :     )
309 :     *)
310 :     end
311 :    
312 :    
313 :     (*
314 :     * Priority Queue. Let's hope the compiler will inline it for performance
315 :     *)
316 :     functor PriQueue(type elem val less : elem * elem -> bool) =
317 :     struct
318 :    
319 :    
320 :     (* A leftist tree is a binary tree with priority ordering
321 :     * with the invariant that the left branch is always the taller one
322 :     *)
323 :     type elem = elem
324 : monnier 498 datatype pri_queue = TREE of elem * int * pri_queue * pri_queue | EMPTY
325 : monnier 469
326 :     fun merge'(EMPTY, EMPTY) = (EMPTY, 0)
327 :     | merge'(EMPTY, a as TREE(_, d, _, _)) = (a, d)
328 :     | merge'(a as TREE(_, d, _, _), EMPTY) = (a, d)
329 :     | merge'(a as TREE(x, d, l, r), b as TREE(y, d', l', r')) =
330 :     let val (root, l, r1, r2) =
331 :     if less(x, y) then (x, l, r, b) else (y, l', r', a)
332 :     val (r, d_r) = merge'(r1, r2)
333 : monnier 498 val d_l = case l of EMPTY => 0 | TREE(_, d, _, _) => d
334 :     val (l, r, d_t) = if d_l >= d_r then (l, r, d_l+1) else (r, l, d_r+1)
335 : monnier 469 in (TREE(root, d_t, l, r), d_t) end
336 :    
337 : monnier 498 fun merge(a, b) = #1(merge'(a, b))
338 : monnier 469
339 :     fun add (x, EMPTY) = TREE(x, 1, EMPTY, EMPTY)
340 :     | add (x, b as TREE(y, d', l', r')) =
341 :     if less(x,y) then TREE(x, d'+1, b, EMPTY)
342 :     else #1(merge'(TREE(x, 1, EMPTY, EMPTY), b))
343 :     end
344 :    
345 :     structure FZ = PriQueue
346 :     (type elem=node
347 :     fun less(NODE{movecost=ref p1,...}, NODE{movecost=ref p2,...}) = p1 < p2
348 :     )
349 :     structure MV = PriQueue
350 :     (type elem=G.move
351 :     fun less(MV{cost=p1,...}, MV{cost=p2,...}) = p1 > p2
352 :     )
353 : monnier 498
354 : monnier 469 type move_queue = MV.pri_queue
355 :     type freeze_queue = FZ.pri_queue
356 :    
357 :    
358 :     (*
359 :     * Utility functions
360 :     *)
361 : monnier 427 fun chase(NODE{color=ref(ALIASED r), ...}) = chase r
362 :     | chase x = x
363 :    
364 : monnier 469 fun colorOf(G.GRAPH{showReg,...}) (NODE{number, color, pri,...}) =
365 :     showReg number^
366 :     (case !color of
367 : monnier 498 PSEUDO => ""
368 :     | REMOVED => "r"
369 :     | ALIASED _ => "a"
370 :     | COLORED c => "["^showReg c^"]"
371 :     | SPILLED ~1 => "s"
372 :     | SPILLED c => (if c >= 0 then "m" else "s")^
373 :     (if c >= 0 andalso number = c then ""
374 :     else "{"^Int.toString c^"}")
375 : monnier 469 )
376 :    
377 :     fun show G (node as NODE{pri,...}) =
378 :     colorOf G node^(if !verbose then "("^Int.toString(!pri)^")" else "")
379 :    
380 :     (*
381 :     * Dump the interference graph
382 :     *)
383 :     fun dumpGraph(G as G.GRAPH{nodes, showReg, K,...}) stream =
384 :     let fun pr s = TextIO.output(stream, s)
385 :     val show = show G
386 :     val colorOf = colorOf G
387 : monnier 498 fun prMove(MV{src, dst, status=ref(WORKLIST | BRIGGS_MOVE | GEORGE_MOVE),
388 :     cost,...}) =
389 : monnier 469 pr(colorOf(chase dst)^" <- "^colorOf(chase src)^
390 :     "("^Int.toString(cost)^") ")
391 :     | prMove _ = ()
392 : monnier 498
393 :     fun prAdj(n,n' as NODE{adj, degree, uses, defs,
394 :     color, pri, movecnt, movelist, ...}) =
395 :     (pr(show n');
396 :     if !verbose then pr(" deg="^Int.toString(!degree)) else ();
397 :     (case !color of
398 :     ALIASED n => (pr " => "; pr(show n); pr "\n")
399 :     | _ =>
400 :     (pr(" <-->");
401 :     app (fn n => (pr " "; pr(show n))) (!adj); pr "\n";
402 :     if !verbose andalso !movecnt > 0 then
403 :     (pr("\tmoves "^Int.toString(!movecnt)^": ");
404 :     app prMove (!movelist);
405 :     pr "\n"
406 :     )
407 :     else ();
408 :     if n = 10 then
409 :     (pr "defs="; app (fn p => pr(Int.toString p^" ")) (!defs);pr"\n";
410 :     pr "uses="; app (fn p => pr(Int.toString p^" ")) (!uses);pr"\n"
411 :     ) else ()
412 :     )
413 :     )
414 :     )
415 : monnier 469
416 :     in pr("=========== K="^Int.toString K^" ===========\n");
417 :     app prAdj (ListMergeSort.sort (fn ((x, _),(y, _)) => x > y)
418 :     (Intmap.intMapToList nodes))
419 : monnier 427 end
420 :    
421 : monnier 469
422 :     (*
423 :     * Function to create new nodes.
424 :     * Note: it is up to the caller to remove all dedicated registers.
425 :     *)
426 :     fun newNodes(G.GRAPH{nodes, firstPseudoR, ...}) =
427 :     let val getnode = Intmap.map nodes
428 :     val addnode = Intmap.add nodes
429 :    
430 :     fun defUse{defs, uses, pt, cost} =
431 :     let fun def reg =
432 :     let val node as NODE{pri, defs,...} = getnode reg
433 :     in pri := !pri + cost;(* increment the priority by the cost *)
434 :     defs := pt :: !defs;
435 :     node
436 :     end
437 :     handle _ =>
438 :     let val col = if reg < firstPseudoR then COLORED(reg) else PSEUDO
439 :     val node =
440 :     NODE{number=reg, color=ref col, degree=ref 0,
441 :     adj=ref [], movecnt=ref 0, movelist = ref [],
442 :     movecost=ref 0, (* pair=false, *) pri=ref cost,
443 :     defs=ref [pt], uses=ref []}
444 :     in addnode(reg, node); node
445 :     end
446 :     fun use reg =
447 :     let val node as NODE{pri, uses,...} = getnode reg
448 :     in pri := !pri + cost; (* increment the priority by the cost *)
449 :     uses := pt :: !uses
450 :     end
451 :     handle _ =>
452 :     let val col = if reg < firstPseudoR then COLORED(reg) else PSEUDO
453 :     val node =
454 :     NODE{number=reg, color=ref col, degree=ref 0,
455 :     adj=ref [], movecnt=ref 0, movelist = ref [],
456 :     movecost=ref 0, (* pair=false, *)
457 :     pri=ref cost, defs=ref [], uses=ref[pt]
458 :     }
459 :     in addnode(reg, node)
460 :     end
461 :     fun defAll([],ds) = ds | defAll(r::rs,ds) = defAll(rs,def r::ds)
462 :     fun useAll [] = () | useAll(r::rs) = (use r; useAll rs)
463 :     val defs = defAll(defs,[])
464 :     val _ = useAll uses
465 :     in defs
466 :     end
467 :     in defUse
468 : monnier 427 end
469 :    
470 :    
471 : monnier 469 (*
472 :     * Add an edge (x, y) to the interference graph.
473 :     * Nop if the edge already exists.
474 :     * Note: adjacency lists of colored nodes are not stored
475 :     * within the interference graph to save space.
476 : monnier 498 * Now we allow spilled node to be added to the edge; these do not
477 :     * count toward the degree.
478 : monnier 469 *)
479 :     fun addEdge(GRAPH{bitMatrix,...}) =
480 :     let val addBitMatrix = BM.add(!bitMatrix)
481 : monnier 498 in fn (x as NODE{number=xn, color=colx, adj=adjx, degree=degx, ...},
482 :     y as NODE{number=yn, color=coly, adj=adjy, degree=degy, ...}) =>
483 : monnier 427 if xn = yn then ()
484 : monnier 469 else if addBitMatrix(xn, yn) then
485 : monnier 498 (case (!colx, !coly) of
486 :     (PSEUDO, PSEUDO) => (adjx := y :: !adjx; degx := !degx + 1;
487 :     adjy := x :: !adjy; degy := !degy + 1)
488 :     | (PSEUDO, COLORED _) => (adjx := y :: !adjx; degx := !degx + 1)
489 :     | (PSEUDO, SPILLED _) => (adjx := y :: !adjx; adjy := x :: !adjy)
490 :     | (COLORED _, PSEUDO) => (adjy := x :: !adjy; degy := !degy + 1)
491 :     | (COLORED _, COLORED _) => ()
492 :     | (COLORED _, SPILLED _) => ()
493 :     | (SPILLED _, PSEUDO) => (adjx := y :: !adjx; adjy := x :: !adjy)
494 :     | (SPILLED _, COLORED _) => ()
495 :     | (SPILLED _, SPILLED _) => ()
496 :     | _ => error "addEdge"
497 :     )
498 :     else () (* edge already there *)
499 : monnier 427 end
500 :    
501 :     (*
502 : monnier 469 * Remove an edge from the bitmatrix
503 : monnier 427 *)
504 : monnier 469 fun removeEdge(GRAPH{bitMatrix,...}) =
505 :     let val rmvBitMatrix = BM.delete(!bitMatrix)
506 :     fun filter(c,[], adj') = adj'
507 :     | filter(c,(n as NODE{color,...})::adj, adj') =
508 :     filter(c, adj, if c = color then adj' else n::adj')
509 :     fun rmv(NODE{color, adj, degree, (* pair=p1, *)...},
510 :     s as NODE{(* pair=p2, *) color=c2,...}) =
511 :     (case !color of
512 :     PSEUDO => (adj := filter(c2,!adj,[]);
513 :     (* check for pair <-> pair interference *)
514 :     (* if p1 andalso p2 then degree := !degree - 2
515 :     else *) degree := !degree - 1
516 :     )
517 :     | COLORED _ => () (* not stored *)
518 :     | ALIASED _ => error "removeEdge: ALIASED"
519 :     | REMOVED => error "removeEdge: REMOVED"
520 :     | SPILLED _ => error "removeEdge: SPILLED"
521 :     )
522 :     in fn (x as NODE{number=xn, ...}, y as NODE{number=yn, ...}) =>
523 :     if xn = yn then ()
524 :     else if rmvBitMatrix(if xn < yn then (xn, yn) else (yn, xn)) then
525 :     (rmv(x, y); rmv(y, x))
526 :     else ()
527 : monnier 427 end
528 :    
529 :     (*
530 : monnier 469 * Initialize a list of worklists
531 : monnier 427 *)
532 : monnier 469 fun initWorkLists
533 : monnier 498 (GRAPH{nodes, K, bitMatrix, regmap, pseudoCount, blockedCount,
534 :     firstPseudoR, deadCopies, memMoves, mode, ...}) {moves} =
535 : monnier 469 let (* Filter moves that already have an interference
536 :     * Also initialize the movelist and movecnt fields at this time.
537 :     *)
538 :     val member = BM.member(!bitMatrix)
539 : monnier 427
540 : monnier 469 fun setInfo(NODE{color=ref PSEUDO, movecost, movecnt, movelist,...},
541 :     mv, cost) =
542 :     (movelist := mv :: !movelist;
543 :     movecnt := !movecnt + 1;
544 :     movecost := !movecost + cost
545 :     )
546 :     | setInfo _ = ()
547 : monnier 427
548 : monnier 498 fun filter([], mvs', mem) = (mvs', mem)
549 :     | filter((mv as MV{src as NODE{number=x, color=ref colSrc,...},
550 :     dst as NODE{number=y, color=ref colDst,...},
551 :     cost, ...})::mvs,
552 :     mvs', mem) =
553 :     (case (colSrc, colDst) of
554 :     (COLORED _, COLORED _) => filter(mvs, mvs', mem)
555 :     | (SPILLED _, SPILLED _) => filter(mvs, mvs', mem)
556 :     | (SPILLED _, _) => filter(mvs, mvs', mv::mem)
557 :     | (_, SPILLED _) => filter(mvs, mvs', mv::mem)
558 :     | _ =>
559 :     if member(x, y) (* moves that interfere *)
560 :     then filter(mvs, mvs', mem)
561 : monnier 469 else (setInfo(src, mv, cost);
562 :     setInfo(dst, mv, cost);
563 : monnier 498 filter(mvs, MV.add(mv, mvs'), mem)
564 : monnier 469 )
565 : monnier 498 )
566 : monnier 427
567 : monnier 498 fun filter'([], mvs', mem, dead) = (mvs', mem, dead)
568 :     | filter'((mv as
569 :     MV{src as NODE{number=x, color as ref colSrc,
570 :     pri, adj, uses,...},
571 :     dst as NODE{number=y, color=ref colDst,
572 :     defs=dstDefs, uses=dstUses,...},
573 :     cost, ...})::mvs,
574 :     mvs', mem, dead) =
575 :     (case (colSrc, colDst, dstDefs, dstUses) of
576 :     (COLORED _, COLORED _, _, _) => filter'(mvs, mvs', mem, dead)
577 :     | (SPILLED _, SPILLED _, _, _) => filter'(mvs, mvs', mem, dead)
578 :     | (SPILLED _, _, _, _) => filter'(mvs, mvs', mv::mem, dead)
579 :     | (_, SPILLED _, _, _) => filter'(mvs, mvs', mv::mem, dead)
580 :     | (_, PSEUDO, ref [pt], ref []) =>
581 : monnier 469 (* eliminate dead copy *)
582 : monnier 498 let fun decDegree [] = ()
583 :     | decDegree(NODE{color=ref PSEUDO, degree, ...}::adj) =
584 :     (degree := !degree - 1; decDegree adj)
585 :     | decDegree(_::adj) = decDegree adj
586 :     fun elimUses([], _, uses, pri, cost) = (uses, pri)
587 :     | elimUses(pt::pts, pt' : int, uses, pri, cost) =
588 :     if pt = pt' then elimUses(pts, pt', uses, pri-cost, cost)
589 :     else elimUses(pts, pt', pt::uses, pri, cost)
590 :     val (uses', pri') = elimUses(!uses, pt, [], !pri, cost);
591 : monnier 469 in pri := pri';
592 :     uses := uses';
593 :     color := ALIASED src;
594 :     decDegree(!adj);
595 : monnier 498 filter'(mvs, mvs', mem, y::dead)
596 : monnier 427 end
597 : monnier 498 | _ => (* normal moves *)
598 :     if member(x, y) (* moves that interfere *)
599 :     then filter'(mvs, mvs', mem, dead)
600 : monnier 469 else (setInfo(src, mv, cost);
601 :     setInfo(dst, mv, cost);
602 : monnier 498 filter'(mvs, MV.add(mv, mvs'), mem, dead)
603 : monnier 469 )
604 : monnier 498 )
605 :    
606 : monnier 469 (*
607 :     * Scan all nodes in the graph and check which worklist they should
608 :     * go into.
609 :     *)
610 : monnier 498 fun collect([], simp, fz, moves, spill, pseudos, blocked) =
611 :     (pseudoCount := pseudos;
612 :     blockedCount := blocked;
613 : monnier 469 {simplifyWkl = simp,
614 :     moveWkl = moves,
615 :     freezeWkl = fz,
616 :     spillWkl = spill
617 :     }
618 : monnier 498 )
619 :     | collect(node::rest, simp, fz, moves, spill, pseudos, blocked) =
620 : monnier 469 (case node of
621 :     NODE{color=ref PSEUDO, movecnt, degree, ...} =>
622 :     if !degree >= K then
623 : monnier 498 collect(rest, simp, fz, moves, node::spill,
624 :     pseudos+1, blocked)
625 : monnier 469 else if !movecnt > 0 then
626 : monnier 498 collect(rest, simp, FZ.add(node, fz),
627 :     moves, spill, pseudos+1, blocked+1)
628 : monnier 469 else
629 : monnier 498 collect(rest, node::simp, fz, moves, spill,
630 :     pseudos+1, blocked)
631 :     | _ => collect(rest, simp, fz, moves, spill, pseudos, blocked)
632 : monnier 469 )
633 : monnier 427
634 : monnier 469 (* First build the move priqueue *)
635 : monnier 498 val (mvs, mem) =
636 :     if isOn(mode, DEAD_COPY_ELIM) then
637 :     let val (mvs, mem, dead) = filter'(moves, MV.EMPTY, [], [])
638 :     in deadCopies := dead; (mvs, mem)
639 : monnier 469 end
640 : monnier 498 else filter(moves, MV.EMPTY, [])
641 : monnier 469
642 :     (* if copy propagation was done prior to register allocation
643 :     * then some nodes may already be aliased. This function updates the
644 :     * aliasing.
645 :     *)
646 :     fun updateAliases() =
647 :     let val alias = Intmap.mapInt regmap
648 :     val getnode = Intmap.map nodes
649 :     fun fixup(num, NODE{color, ...}) =
650 :     if num < firstPseudoR then ()
651 :     else let val reg = alias num
652 :     in if reg=num then () else
653 :     color := ALIASED(getnode reg)
654 :     end
655 :     in Intmap.app fixup nodes end
656 :    
657 :     in (* updateAliases(); *)
658 : monnier 498 memMoves := mem; (* memory moves *)
659 :     collect(Intmap.values nodes, [], FZ.EMPTY, mvs, [], 0, 0)
660 : monnier 469 end
661 :    
662 :     (*
663 :     * Return a regmap that reflects the current interference graph.
664 :     * Spilled registers are given the special value ~1
665 :     *)
666 :     fun regmap(G.GRAPH{nodes,...}) =
667 :     let val getnode = Intmap.map nodes
668 :     fun num(NODE{color=ref(COLORED r),...}) = r
669 :     | num(NODE{color=ref(ALIASED n),...}) = num n
670 : monnier 498 | num(NODE{color=ref(SPILLED s),...}) = if s >= 0 then s else ~1
671 : monnier 469 | num(NODE{number, color=ref PSEUDO,...}) = number
672 :     | num _ = error "regmap"
673 :     fun lookup r = num(getnode r) handle _ => r (* XXX *)
674 :     in lookup
675 :     end
676 :    
677 :     (*
678 :     * Return a regmap that reflects the current interference graph,
679 :     * during spilling.
680 :     *)
681 :     fun spillRegmap(G.GRAPH{nodes,...}) =
682 :     let val getnode = Intmap.map nodes
683 :     fun num(NODE{color=ref(COLORED r),...}) = r
684 :     | num(NODE{color=ref(ALIASED n),...}) = num n
685 :     | num(NODE{color=ref(SPILLED _),number,...}) = number
686 :     | num(NODE{number, color=ref PSEUDO,...}) = number
687 :     | num _ = error "spillRegmap"
688 :     fun lookup r = num(getnode r) handle _ => r (* XXX *)
689 :     in lookup
690 :     end
691 :    
692 :     (*
693 :     * Return a regmap that returns the current spill location
694 :     * during spilling.
695 :     *)
696 :     fun spillLoc(G.GRAPH{nodes,...}) =
697 :     let val getnode = Intmap.map nodes
698 :     fun num(NODE{color=ref(ALIASED n), ...}) = num n
699 :     | num(NODE{color=ref(SPILLED ~1), number, ...}) = number
700 :     | num(NODE{color=ref(SPILLED c), ...}) = c
701 :     | num(NODE{number, ...}) = number
702 :     fun lookup r = num(getnode r) handle _ => r (* XXX *)
703 :     in lookup
704 :     end
705 :    
706 :     (*
707 :     * Core phases:
708 :     * Simplify, coalesce, freeze.
709 :     *
710 :     * NOTE: When a node's color is REMOVED or ALIASED,
711 :     * it is not considered to be part of the adjacency list
712 :     *
713 :     * 1. The move list has no duplicate
714 :     * 2. The freeze list may have duplicates
715 :     *)
716 :     fun iteratedCoalescingPhases
717 : monnier 498 (G as GRAPH{K, bitMatrix, spillFlag, trail, stamp,
718 :     pseudoCount, blockedCount, ...}) =
719 : monnier 469 let val member = BM.member(!bitMatrix)
720 : monnier 427 val addEdge = addEdge G
721 : monnier 469 val show = show G
722 : monnier 498 val blocked = blockedCount
723 : monnier 427
724 : monnier 469 (*
725 :     * SIMPLIFY node:
726 :     * precondition: node must be part of the interference graph (PSEUDO)
727 :     *)
728 : monnier 498 fun simplify(node as NODE{color, number, adj, degree, (*pair,*)...},
729 :     mv, fz, stack) =
730 : monnier 469 let val _ = if debug then print("Simplifying "^show node^"\n") else ()
731 :     fun forallAdj([], mv, fz, stack) = (mv, fz, stack)
732 : monnier 498 | forallAdj((n as NODE{color=ref PSEUDO, degree as ref d,...})::adj,
733 :     mv, fz, stack) =
734 :     if d = K then
735 :     let val (mv, fz, stack) = lowDegree(n, mv, fz, stack)
736 :     in forallAdj(adj, mv, fz, stack) end
737 :     else (degree := d - 1; forallAdj(adj, mv, fz, stack))
738 :     | forallAdj(_::adj, mv, fz, stack) = forallAdj(adj, mv, fz, stack)
739 : monnier 469 in color := REMOVED;
740 : monnier 498 pseudoCount := !pseudoCount - 1;
741 : monnier 469 forallAdj(!adj, mv, fz, node::stack) (* push onto stack *)
742 :     end (* simplify *)
743 :    
744 : monnier 498 and simplifyAll([], mv, fz, stack) = (mv, fz, stack)
745 :     | simplifyAll(node::simp, mv, fz, stack) =
746 :     let val (mv, fz, stack) = simplify(node, mv, fz, stack)
747 :     in simplifyAll(simp, mv, fz, stack) end
748 :    
749 : monnier 469 (*
750 :     * Decrement the degree of a pseudo node.
751 :     * precondition: node must be part of the interference graph
752 :     * If the degree of the node is now K-1.
753 :     * Then if (a) the node is move related, freeze it.
754 :     * (b) the node is non-move related, simplify it
755 :     *
756 :     * node -- the node to decrement degree
757 :     * mv -- queue of move candidates to be coalesced
758 :     * fz -- queue of freeze candidates
759 :     * stack -- stack of removed nodes
760 :     *)
761 : monnier 498 and lowDegree(node as NODE{degree as ref d, movecnt, adj, color,...},
762 :     (* false, *) mv, fz, stack) =
763 :     (* normal edge *)
764 : monnier 469 (if debug then
765 :     print("DecDegree "^show node^" d="^Int.toString(d-1)^"\n") else ();
766 : monnier 498 degree := K - 1;
767 :     (* node is now low degree!!! *)
768 :     let val mv = enableMoves(!adj, mv)
769 :     in if !movecnt > 0 then (* move related *)
770 :     (blocked := !blocked + 1; (mv, FZ.add(node, fz), stack))
771 :     else (* non-move related, simplify now! *)
772 :     simplify(node, mv, fz, stack)
773 :     end
774 : monnier 469 )
775 :     (*
776 :     | decDegree(node as NODE{degree as ref d, movecnt, adj, color,...},
777 :     true, mv, fz, stack) = (* register pair edge *)
778 :     (degree := d - 2;
779 :     if d >= K andalso !degree < K then
780 :     (* node is now low degree!!! *)
781 :     let val mv = enableMoves(node :: !adj, mv)
782 :     in if !movecnt > 0 then (* move related *)
783 : monnier 498 (blocked := !blocked + 1; (mv, FZ.add(node, fz), stack))
784 : monnier 469 else (* non-move related, simplify now! *)
785 :     simplify(node, mv, fz, stack)
786 :     end
787 :     else
788 :     (mv, fz, stack)
789 :     )
790 :     *)
791 :    
792 :     (*
793 :     * Enable moves:
794 :     * given: a list of nodes (some of which are not in the graph)
795 :     * do: all moves associated with these nodes are inserted
796 :     * into the move worklist
797 :     *)
798 :     and enableMoves([], mv) = mv
799 :     | enableMoves(n::ns, mv) =
800 :     let (* add valid moves onto the worklist.
801 :     * there are no duplicates on the move worklist!
802 : monnier 427 *)
803 : monnier 498 fun addMv([], ns, mv) = enableMoves(ns, mv)
804 :     | addMv((m as MV{status, hicount as ref hi, ...})::rest,
805 :     ns, mv) =
806 : monnier 469 (case !status of
807 : monnier 498 (BRIGGS_MOVE | GEORGE_MOVE) =>
808 :     (* decrements hi, when hi <= 0 enable move *)
809 :     if hi <= 1 then
810 :     (status := WORKLIST; addMv(rest, ns, MV.add(m, mv)))
811 :     else
812 :     (hicount := hi-1; addMv(rest, ns, mv))
813 :     | _ => addMv(rest, ns, mv)
814 : monnier 469 )
815 :     in (* make sure the nodes are actually in the graph *)
816 :     case n of
817 :     NODE{movelist, color=ref PSEUDO, movecnt,...} =>
818 :     if !movecnt > 0 then (* is it move related? *)
819 : monnier 498 addMv(!movelist, ns, mv)
820 : monnier 469 else
821 :     enableMoves(ns, mv)
822 :     | _ => enableMoves(ns, mv)
823 :     end (* enableMoves *)
824 :    
825 :     (*
826 :     * Brigg's conservative coalescing test:
827 :     * given: an unconstrained move (x, y)
828 :     * return: true or false
829 :     *)
830 : monnier 498 fun conservative(hicount,
831 :     x as NODE{degree=ref dx, adj=xadj, (* pair=px, *) ...},
832 : monnier 469 y as NODE{degree=ref dy, adj=yadj, (* pair=py, *) ...}) =
833 :     dx + dy < K orelse
834 :     let (*
835 : monnier 498 * hi -- is the number of nodes with deg > K (without duplicates)
836 :     * n -- the number of nodes that have deg = K but not neighbors
837 :     * of both x and y
838 : monnier 469 * We use the movecnt as a flag indicating whether
839 :     * a node has been visited. A negative count is used to mark
840 :     * a visited node.
841 : monnier 427 *)
842 : monnier 498 fun undo([], extraHi) =
843 :     extraHi <= 0 orelse (hicount := extraHi; false)
844 :     | undo(movecnt::tr, extraHi) =
845 :     (movecnt := ~1 - !movecnt; undo(tr, extraHi))
846 :     fun loop([], [], hi, n, tr) = undo(tr, (hi + n) - K + 1)
847 :     | loop([], yadj, hi, n, tr) = loop(yadj, [], hi, n, tr)
848 : monnier 469 | loop(NODE{color, movecnt as ref m, degree=ref deg, ...}::vs,
849 : monnier 498 yadj, hi, n, tr) =
850 :     (case !color of
851 :     COLORED _ =>
852 :     if m < 0 then
853 :     (* node has been visited before *)
854 :     loop(vs, yadj, hi, n, tr)
855 :     else
856 :     (movecnt := ~1 - m; (* mark as visited *)
857 :     loop(vs, yadj, hi+1, n, movecnt::tr))
858 :     | PSEUDO =>
859 :     if deg < K then loop(vs, yadj, hi, n, tr)
860 :     else if m >= 0 then
861 :     (* node has never been visited before *)
862 :     (movecnt := ~1 - m; (* mark as visited *)
863 :     if deg = K
864 :     then loop(vs, yadj, hi, n+1, movecnt::tr)
865 :     else loop(vs, yadj, hi+1, n, movecnt::tr)
866 :     )
867 :     else
868 :     (* node has been visited before *)
869 :     if deg = K then loop(vs, yadj, hi, n-1, tr)
870 :     else loop(vs, yadj, hi, n, tr)
871 :     | _ => loop(vs, yadj, hi, n, tr) (* REMOVED/ALIASED *)
872 :     )
873 :     in loop(!xadj, !yadj, 0, 0, []) end
874 : monnier 469
875 :     (*
876 :     * Heuristic used to determine whether a pseudo and machine register
877 :     * can be coalesced.
878 :     * Precondition:
879 :     * The two nodes are assumed not to interfere.
880 :     *)
881 : monnier 498 fun safe(hicount, reg, NODE{adj, ...}) =
882 :     let fun loop([], hi) = hi = 0 orelse (hicount := hi; false)
883 :     | loop(n::adj, hi) =
884 : monnier 469 (case n of
885 : monnier 498 (* Note: Actively we only have to consider pseudo nodes and not
886 :     * nodes that are removed, since removed nodes either have
887 :     * deg < K or else optimistic spilling must be in effect!
888 :     *)
889 :     NODE{degree,number,color=ref(PSEUDO | REMOVED), ...} =>
890 :     if !degree < K orelse member(reg, number) then loop(adj, hi)
891 :     else loop(adj, hi+1)
892 :     | _ => loop(adj, hi)
893 : monnier 469 )
894 : monnier 498 in loop(!adj, 0) end
895 : monnier 469
896 :     (*
897 :     * Decrement the active move count of a node.
898 :     * When the move count reaches 0 and the degree < K
899 :     * simplify the node immediately.
900 :     * Precondition: node must be a node in the interference graph
901 :     * The node can become a non-move related node.
902 :     *)
903 :     fun decMoveCnt
904 :     (node as NODE{movecnt, color=ref PSEUDO, degree, movecost,...},
905 :     cnt, cost, mv, fz, stack) =
906 :     let val newCnt = !movecnt - cnt
907 :     in movecnt := newCnt;
908 :     movecost := !movecost - cost;
909 :     if newCnt = 0 andalso !degree < K (* low degree and movecnt = 0 *)
910 : monnier 498 then (blocked := !blocked - 1; simplify(node, mv, fz, stack))
911 : monnier 469 else (mv, fz, stack)
912 :     end
913 :     | decMoveCnt(_, _, _, mv, fz, stack) = (mv, fz, stack)
914 :    
915 :     (*
916 :     * Combine two nodes u and v into one.
917 :     * v is replaced by u
918 :     * u is the new combined node
919 :     * Precondition: u <> v and u and v must be unconstrained
920 :     *
921 :     * u, v -- two nodes to be merged, must be distinct!
922 : monnier 498 * coloingv -- is u a colored node?
923 : monnier 469 * mvcost -- the cost of the move that has been eliminated
924 :     * mv -- the queue of moves
925 :     * fz -- the queue of freeze candidates
926 :     * stack -- stack of removed nodes
927 :     *)
928 : monnier 498 fun combine(u, v, coloringv, mvcost, mv, fz, stack) =
929 : monnier 469 let val NODE{color=vcol, pri=pv, movecnt=cntv, movelist=movev, adj=adjv,
930 : monnier 498 defs=defsv, uses=usesv, degree=degv, ...} = v
931 : monnier 469 val NODE{color=ucol, pri=pu, movecnt=cntu, movelist=moveu, adj=adju,
932 : monnier 498 defs=defsu, uses=usesu, degree=degu, ...} = u
933 :    
934 : monnier 469 (* merge movelists together, taking the opportunity
935 :     * to prune the lists
936 :     *)
937 :     fun mergeMoveList([], mv) = mv
938 : monnier 498 | mergeMoveList((m as MV{status,hicount,...})::rest, mv) =
939 : monnier 469 (case !status of
940 : monnier 498 BRIGGS_MOVE =>
941 :     (* if we are changing a copy from v <-> w to uv <-> w
942 :     * makes sure we reset its trigger count, so that it
943 :     * will be tested next.
944 :     *)
945 :     (if coloringv then (status := GEORGE_MOVE; hicount := 0)
946 :     else ();
947 :     mergeMoveList(rest, m::mv)
948 :     )
949 :     | GEORGE_MOVE =>
950 :     (* if u is colored and v is not, then the move v <-> w
951 :     * becomes uv <-> w where w is colored. This can always
952 :     * be discarded.
953 :     *)
954 :     (if coloringv then mergeMoveList(rest, mv)
955 :     else mergeMoveList(rest, m::mv)
956 :     )
957 :     | WORKLIST => mergeMoveList(rest, m::mv)
958 : monnier 469 | _ => mergeMoveList(rest, mv)
959 :     )
960 :    
961 :     (* Form combined node; add the adjacency list of v to u *)
962 :     fun union([], mv, fz, stack) = (mv, fz, stack)
963 : monnier 498 | union((t as NODE{color, (* pair=pt, *)degree, ...})::adj,
964 :     mv, fz, stack) =
965 : monnier 469 (case !color of
966 : monnier 498 (COLORED _ | SPILLED _) =>
967 :     (addEdge(t, u); union(adj, mv, fz, stack))
968 : monnier 469 | PSEUDO =>
969 :     (addEdge(t, u);
970 : monnier 498 let val d = !degree
971 :     in if d = K then
972 :     let val (mv, fz, stack) = lowDegree(t, mv, fz, stack)
973 :     in union(adj, mv, fz, stack) end
974 :     else (degree := d - 1; union(adj, mv, fz, stack))
975 :     end
976 : monnier 469 )
977 :     | _ => union(adj, mv, fz, stack)
978 :     )
979 : monnier 498
980 : monnier 469 in vcol := ALIASED u;
981 :     (* combine the priority of both:
982 :     * note that since the mvcost has been counted twice
983 :     * in the original priority, we substract it twice
984 :     * from the new priority.
985 :     *)
986 : monnier 498 pu := !pu + !pv - mvcost - mvcost;
987 : monnier 469 (* combine the def/use pts of both nodes.
988 :     * Strictly speaking, the def/use points of the move
989 :     * should also be removed. But since we never spill
990 :     * a coalesced node and only spilling makes use of these
991 : monnier 475 * def/use points, we are safe for now.
992 :     *
993 :     * New comment: with spill propagation, it is necessary
994 :     * to keep track of the spilled program points.
995 : monnier 469 *)
996 : monnier 475 defsu := concat(!defsu, !defsv);
997 :     usesu := concat(!usesu, !usesv);
998 : monnier 469 case !ucol of
999 :     PSEUDO =>
1000 : monnier 498 (if !cntv > 0 then
1001 :     (if !cntu > 0 then blocked := !blocked - 1 else ();
1002 :     moveu := mergeMoveList(!movev, !moveu)
1003 :     )
1004 :     else ();
1005 : monnier 469 movev := []; (* XXX kill the list to free space *)
1006 :     cntu := !cntu + !cntv
1007 :     )
1008 :     | _ => ()
1009 :     ;
1010 :     cntv := 0;
1011 :    
1012 : monnier 498 let val removingHi = !degv >= K andalso (!degu >= K orelse coloringv)
1013 : monnier 469 (* Update the move count of the combined node *)
1014 : monnier 498 val (mv, fz, stack) = union(!adjv, mv, fz, stack)
1015 :     val (mv, fz, stack) =
1016 :     decMoveCnt(u, 2, mvcost + mvcost, mv, fz, stack)
1017 :     (* If either v or u are high degree then at least one high degree
1018 :     * node is removed from the neighbors of uv after coalescing
1019 :     *)
1020 :     val mv = if removingHi then enableMoves(!adju, mv) else mv
1021 :     in coalesce(mv, fz, stack)
1022 : monnier 469 end
1023 :     end
1024 :    
1025 :     (*
1026 :     * COALESCE:
1027 :     * Repeat coalescing and simplification until mv is empty.
1028 :     *)
1029 : monnier 498 and coalesce(MV.EMPTY, fz, stack) = (fz, stack)
1030 :     | coalesce(MV.TREE(MV{src, dst, status, hicount, cost, ...}, _, l, r),
1031 :     fz, stack) =
1032 :     let (* val _ = coalesce_count := !coalesce_count + 1 *)
1033 :     val u = chase src
1034 : monnier 469 val v as NODE{color=ref vcol, ...} = chase dst
1035 :     (* make u the colored one *)
1036 :     val (u as NODE{number=u', color=ref ucol, ...},
1037 :     v as NODE{number=v', color=ref vcol, ...}) =
1038 :     case vcol of
1039 :     COLORED _ => (v, u)
1040 :     | _ => (u, v)
1041 :     val _ = if debug then print ("Coalescing "^show u^"<->"^show v
1042 :     ^" ("^Int.toString cost^")") else ()
1043 :     val mv = MV.merge(l, r)
1044 : monnier 498 fun coalesceIt(status, v) =
1045 :     (status := COALESCED;
1046 :     if !spillFlag then trail := UNDO(v, status, !trail) else ()
1047 :     )
1048 : monnier 469 in if u' = v' then (* trivial move *)
1049 : monnier 498 let val _ = if debug then print(" Trivial\n") else ()
1050 :     val _ = coalesceIt(status, v)
1051 :     in coalesce(decMoveCnt(u, 2, cost+cost, mv, fz, stack))
1052 :     end
1053 : monnier 469 else
1054 :     (case vcol of
1055 :     COLORED _ =>
1056 :     (* two colored nodes cannot be coalesced *)
1057 : monnier 498 (status := CONSTRAINED;
1058 :     if debug then print(" Both Colored\n") else ();
1059 :     coalesce(mv, fz, stack))
1060 : monnier 469 | _ =>
1061 : monnier 498 if member(u', v') then
1062 : monnier 469 (* u and v interfere *)
1063 : monnier 498 let val _ = status := CONSTRAINED
1064 :     val _ = if debug then print(" Interfere\n") else ();
1065 :     val (mv, fz, stack) =
1066 :     decMoveCnt(u, 1, cost, mv, fz, stack)
1067 :     in coalesce(decMoveCnt(v, 1, cost, mv, fz, stack)) end
1068 : monnier 469 else
1069 :     case ucol of
1070 :     COLORED _ => (* u is colored, v is not *)
1071 : monnier 498 if safe(hicount, u', v) then
1072 :     (if debug then print(" Safe\n") else ();
1073 :     (*if tally then good_george := !good_george+1 else ();*)
1074 :     coalesceIt(status, v);
1075 :     combine(u, v, true, cost, mv, fz, stack)
1076 :     )
1077 : monnier 469 else
1078 : monnier 498 ((* remove it from the move list *)
1079 :     status := GEORGE_MOVE;
1080 :     (*if tally then bad_george := !bad_george + 1 else ();*)
1081 : monnier 469 if debug then print(" Unsafe\n") else ();
1082 : monnier 498 coalesce(mv, fz, stack)
1083 : monnier 469 )
1084 :     | _ => (* u, v are not colored *)
1085 : monnier 498 if conservative(hicount, u, v) then
1086 :     (if debug then print(" OK\n") else ();
1087 :     (*if tally then good_briggs := !good_briggs+1 else ();*)
1088 :     coalesceIt(status, v);
1089 :     combine(u, v, false, cost, mv, fz, stack)
1090 :     )
1091 : monnier 469 else (* conservative test failed *)
1092 : monnier 498 ((* remove it from the move list *)
1093 :     status := BRIGGS_MOVE;
1094 :     (*if tally then bad_briggs := !bad_briggs + 1 else ();*)
1095 : monnier 469 if debug then print(" Non-conservative\n") else ();
1096 : monnier 498 coalesce(mv, fz, stack)
1097 : monnier 469 )
1098 :     )
1099 :     end
1100 :    
1101 :     (* mark a node n as frozen:
1102 :     * Go thru all the moves (n, m), decrement the move count of m
1103 :     * precondition: degree must be < K
1104 :     * movecnt must be > 0
1105 :     * node -- the node to be frozen
1106 :     * fz -- a queue of freeze candidates
1107 :     * stack -- stack of removed nodes
1108 :     *)
1109 : monnier 498 fun markAsFrozen(
1110 :     node as NODE{number=me, degree,
1111 :     adj, movelist, movecnt as ref mc,...},
1112 :     fz, stack) =
1113 : monnier 469 let val _ = if debug then print("Mark as frozen "^Int.toString me^"\n")
1114 :     else ()
1115 :     (* eliminate all moves, return a list of nodes that
1116 :     * can be simplified
1117 :     *)
1118 :     fun elimMoves([], simp) = simp
1119 :     | elimMoves(MV{status, src, dst, ...}::mvs, simp) =
1120 :     case !status of
1121 :     WORKLIST => error "elimMoves"
1122 : monnier 498 | (BRIGGS_MOVE | GEORGE_MOVE) => (* mark move as lost *)
1123 : monnier 469 let val _ = status := LOST
1124 :     val src as NODE{number=s,...} = chase src
1125 : monnier 498 val you = if s = me then chase dst else src
1126 : monnier 469 in case you of
1127 :     NODE{color=ref(COLORED _),...} =>
1128 :     elimMoves(mvs, simp)
1129 :     | NODE{movecnt as ref c, degree, ...} => (* pseudo *)
1130 :     (movecnt := c - 1;
1131 :     if c = 1 andalso !degree < K then
1132 : monnier 498 (blocked := !blocked - 1; elimMoves(mvs, you::simp))
1133 : monnier 469 else
1134 :     elimMoves(mvs, simp)
1135 :     )
1136 :     end
1137 :     | _ => elimMoves(mvs, simp)
1138 :    
1139 : monnier 498 (* Note:
1140 :     * We are removing a high degree node, so try to enable all moves
1141 :     * associated with its neighbors.
1142 :     *)
1143 :    
1144 :     val mv = if !degree >= K then enableMoves(!adj, MV.EMPTY)
1145 :     else MV.EMPTY
1146 :    
1147 :     in if mc = 0
1148 :     then simplify(node, mv, fz, stack)
1149 :     else
1150 :     (movecnt := 0;
1151 :     simplifyAll(node::elimMoves(!movelist, []), mv, fz, stack)
1152 :     )
1153 : monnier 427 end
1154 :    
1155 : monnier 469 (*
1156 :     * FREEZE:
1157 :     * Repeat picking
1158 :     * a node with degree < K from the freeze list and freeze it.
1159 :     * fz -- queue of freezable nodes
1160 :     * stack -- stack of removed nodes
1161 :     * undo -- trail of coalesced moves after potential spill
1162 : monnier 427 *)
1163 : monnier 498 fun freeze(fz, stack) =
1164 :     let fun loop(FZ.EMPTY, FZ.EMPTY, stack) = stack
1165 :     | loop(FZ.EMPTY, newFz, _) = error "no freeze candidate"
1166 :     | loop(FZ.TREE(node, _, l, r), newFz, stack) =
1167 : monnier 469 let val fz = FZ.merge(l, r)
1168 :     in case node of
1169 :     (* This node has not been simplified
1170 :     * This must be a move-related node.
1171 :     *)
1172 : monnier 498 NODE{color=ref PSEUDO, degree, ...} =>
1173 :     if !degree >= K (* can't be frozen yet? *)
1174 :     then
1175 :     ((*if tally then bad_freeze := !bad_freeze+1 else ();*)
1176 :     loop(fz, newFz, stack))
1177 : monnier 469 else (* freeze node *)
1178 : monnier 498 let val _ =
1179 :     if debug then print("Freezing "^show node^"\n")
1180 :     else ()
1181 :     (*val _ =
1182 :     if tally then good_freeze := !good_freeze + 1
1183 :     else ()*)
1184 :     val _ = blocked := !blocked - 1;
1185 : monnier 469 val (mv, fz, stack) = markAsFrozen(node, fz, stack)
1186 : monnier 498 val (fz, stack) = coalesce(mv, fz, stack)
1187 :     in if !blocked = 0
1188 :     then ((* print "[no freezing again]"; *) stack)
1189 :     else ((* print("[freezing again "^
1190 :     Int.toString(!blocked)^"]"); *)
1191 :     loop(FZ.merge(fz, newFz), FZ.EMPTY, stack))
1192 : monnier 469 end
1193 : monnier 498 | _ =>
1194 :     ((*if tally then bad_freeze := !bad_freeze + 1 else ();*)
1195 :     loop(fz, newFz, stack))
1196 : monnier 469 end
1197 : monnier 498 in if !blocked = 0 then ((* print "[no freezing]"; *) stack)
1198 :     else ((* print("[freezing "^Int.toString(!blocked)^"]"); *)
1199 :     loop(fz, FZ.EMPTY, stack))
1200 : monnier 427 end
1201 : monnier 469
1202 : monnier 498 (*
1203 :     * Sort simplify worklist in increasing degree.
1204 :     * Matula and Beck suggests that we should always remove the
1205 :     * node with the lowest degree first. This is an approximation of
1206 :     * the idea.
1207 :     *)
1208 : monnier 469 (*
1209 : monnier 498 val buckets = A.array(K, []) : G.node list A.array
1210 :     fun sortByDegree nodes =
1211 :     let fun insert [] = ()
1212 :     | insert((n as NODE{degree=ref deg, ...})::rest) =
1213 :     (UA.update(buckets, deg, n::UA.sub(buckets, deg)); insert rest)
1214 :     fun collect(~1, L) = L
1215 :     | collect(deg, L) = collect(deg-1, concat(UA.sub(buckets, deg), L))
1216 :     in insert nodes;
1217 :     collect(K-1, [])
1218 :     end
1219 :     *)
1220 :    
1221 :     (*
1222 : monnier 469 * Iterate over simplify, coalesce, freeze
1223 :     *)
1224 :     fun iterate{simplifyWkl, moveWkl, freezeWkl, stack} =
1225 :     let (* simplify everything *)
1226 : monnier 498 val (mv, fz, stack) =
1227 :     simplifyAll((* sortByDegree *) simplifyWkl,
1228 :     moveWkl, freezeWkl, stack)
1229 :     val (fz, stack) = coalesce(mv, fz, stack)
1230 :     val stack = freeze(fz, stack)
1231 :     in {stack=stack}
1232 : monnier 469 end
1233 :     in {markAsFrozen=markAsFrozen, iterate=iterate}
1234 : monnier 427 end
1235 :    
1236 : monnier 469 (*
1237 :     * The main entry point for the iterated coalescing algorithm
1238 :     *)
1239 :     fun iteratedCoalescing G =
1240 :     let val {iterate,...} = iteratedCoalescingPhases G
1241 :     in iterate end
1242 : monnier 427
1243 : monnier 469
1244 :     (*
1245 :     * Potential Spill:
1246 :     * Find some node on the spill list and just optimistically
1247 :     * remove it from the graph.
1248 :     *)
1249 :     fun potentialSpillNode (G as G.GRAPH{spillFlag,...}) =
1250 :     let val {markAsFrozen,...} = iteratedCoalescingPhases G
1251 :     in fn {node, stack} =>
1252 :     let val _ = spillFlag := true (* potential spill found *)
1253 : monnier 498 val (mv, fz, stack) = markAsFrozen(node, FZ.EMPTY, stack)
1254 : monnier 469 in {moveWkl=mv, freezeWkl=fz, stack=stack}
1255 :     end
1256 :     end
1257 :    
1258 :    
1259 :    
1260 :     (*
1261 :     * SELECT:
1262 :     * Using optimistic spilling
1263 :     *)
1264 :     fun select(G as GRAPH{getreg, getpair, trail, firstPseudoR, stamp,
1265 : monnier 498 spillFlag, proh, mode, ...}) {stack} =
1266 : monnier 469 let fun undoCoalesced END = ()
1267 :     | undoCoalesced(UNDO(NODE{number, color, ...}, status, trail)) =
1268 : monnier 498 (status := BRIGGS_MOVE;
1269 : monnier 469 if number < firstPseudoR then () else color := PSEUDO;
1270 :     undoCoalesced trail
1271 :     )
1272 :     val show = show G
1273 :    
1274 :     (* Fast coloring, assume no spilling can occur *)
1275 :     fun fastcoloring([], stamp) = ([], stamp)
1276 :     | fastcoloring((node as NODE{color, (* pair, *) adj, ...})::stack,
1277 :     stamp) =
1278 :     let (* set up the proh array *)
1279 : monnier 427 fun neighbors [] = ()
1280 : monnier 469 | neighbors(r::rs) =
1281 :     let fun mark(NODE{color=ref(COLORED c), ...}) =
1282 :     (UA.update(proh, c, stamp); neighbors rs)
1283 :     | mark(NODE{color=ref(ALIASED n), ...}) = mark n
1284 :     | mark _ = neighbors rs
1285 :     in mark r end
1286 : monnier 427 val _ = neighbors(!adj)
1287 : monnier 469 in color := COLORED(getreg{pref=[], proh=proh, stamp=stamp});
1288 :     fastcoloring(stack, stamp+1)
1289 :     end
1290 :    
1291 :     (* Briggs' optimistic spilling heuristic *)
1292 :     fun optimistic([], spills, stamp) = (spills, stamp)
1293 :     | optimistic((node as NODE{color, (* pair, *) adj, ...})::stack,
1294 :     spills, stamp) =
1295 :     let (* set up the proh array *)
1296 :     fun neighbors [] = ()
1297 :     | neighbors(r::rs) =
1298 :     let fun mark(NODE{color=ref(COLORED c), ...}) =
1299 :     (UA.update(proh, c, stamp); neighbors rs)
1300 :     | mark(NODE{color=ref(ALIASED n), ...}) = mark n
1301 :     | mark _ = neighbors rs
1302 :     in mark r end
1303 :     val _ = neighbors(!adj)
1304 : monnier 427 val spills =
1305 : monnier 469 let val col = getreg{pref=[], proh=proh, stamp=stamp}
1306 :     in color := COLORED col; spills
1307 : monnier 427 end handle _ => node::spills
1308 : monnier 469 in optimistic(stack, spills, stamp+1) end
1309 :    
1310 :     (* Briggs' optimistic spilling heuristic, with biased coloring *)
1311 :     fun biasedColoring([], spills, stamp) = (spills, stamp)
1312 :     | biasedColoring(
1313 :     (node as NODE{number, color, adj,
1314 :     (* pair, *) movecnt, movelist,...})::stack,
1315 :     spills, stamp) =
1316 :     let (* set up the proh array *)
1317 :     fun neighbors [] = ()
1318 :     | neighbors(r::rs) =
1319 :     (case chase r of
1320 :     NODE{color=ref(COLORED c), ...} =>
1321 :     (UA.update(proh, c, stamp); neighbors rs)
1322 :     | _ => neighbors rs
1323 :     )
1324 :     (*
1325 :     * Look at lost moves and see if it is possible to
1326 :     * color the move with the same color
1327 :     *)
1328 :     fun getPref([], pref) = pref
1329 : monnier 498 | getPref(MV{status=ref(LOST | BRIGGS_MOVE | GEORGE_MOVE),
1330 :     src, dst, ...}::mvs, pref) =
1331 : monnier 469 let val src as NODE{number=s,...} = chase src
1332 : monnier 498 val other = if s = number then chase dst else src
1333 : monnier 469 in case other of
1334 :     NODE{color=ref(COLORED c),...} => getPref(mvs, c::pref)
1335 :     | _ => getPref(mvs, pref)
1336 :     end
1337 :     | getPref(_::mvs, pref) = getPref(mvs, pref)
1338 :    
1339 :     val _ = neighbors(!adj)
1340 :     val pref = getPref(!movelist,[])
1341 :     val spills =
1342 :     let val col = getreg{pref=[], proh=proh, stamp=stamp}
1343 :     in color := COLORED col; spills
1344 :     end handle _ => node::spills
1345 :     in biasedColoring(stack, spills, stamp+1) end
1346 : monnier 498 val (spills, st) = if isOn(mode, BIASED_SELECTION)
1347 : monnier 469 then biasedColoring(stack, [], !stamp)
1348 :     else if !spillFlag then
1349 :     optimistic(stack, [], !stamp)
1350 :     else
1351 :     fastcoloring(stack, !stamp)
1352 :     in stamp := st;
1353 :     case spills of
1354 :     [] => {spills=[]}
1355 :     | spills =>
1356 :     (app (fn node as NODE{color,...} => color := PSEUDO) stack;
1357 :     undoCoalesced (!trail);
1358 :     trail := END;
1359 :     {spills=spills}
1360 :     )
1361 :     end
1362 :    
1363 :     (*
1364 : monnier 498 * Incorporate memory<->register moves into the interference graph
1365 :     *)
1366 :     fun initMemMoves(GRAPH{memMoves, ...}) =
1367 :     let fun move(NODE{movelist, movecost, ...}, mv, cost) =
1368 :     (movelist := mv :: !movelist;
1369 :     movecost := cost + !movecost
1370 :     )
1371 :    
1372 :     fun setMove(dst, src, mv, cost) =
1373 :     (move(dst, mv, cost); move(src, mv, cost))
1374 :    
1375 :     fun init [] = ()
1376 :     | init((mv as MV{dst, src, cost, ...})::mvs) =
1377 :     let val dst as NODE{color=dstCol, ...} = chase dst
1378 :     val src as NODE{color=srcCol, ...} = chase src
1379 :     in case (!dstCol, !srcCol) of
1380 :     (SPILLED x, SPILLED y) => setMove(dst, src, mv, cost)
1381 :     | (SPILLED _, PSEUDO) => setMove(dst, src, mv, cost)
1382 :     | (PSEUDO, SPILLED _) => setMove(dst, src, mv, cost)
1383 :     | (SPILLED _, COLORED _) => () (* skip *)
1384 :     | (COLORED _, SPILLED _) => () (* skip *)
1385 :     | _ => error "initMemMoves" ;
1386 :     init mvs
1387 :     end
1388 :     val moves = !memMoves
1389 :     in memMoves := [];
1390 :     init moves
1391 :     end
1392 :    
1393 :     (*
1394 : monnier 469 * Spill coalescing.
1395 :     * Coalesce non-interfering moves between spilled nodes,
1396 :     * in non-increasing order of move cost.
1397 :     *)
1398 : monnier 498 fun spillCoalescing(GRAPH{bitMatrix, ...}) =
1399 :     let val member = BM.member(!bitMatrix)
1400 :     val addEdge = BM.add(!bitMatrix)
1401 :     in fn nodesToSpill =>
1402 :     let
1403 :     (* Find moves between two spilled nodes *)
1404 :     fun collectMoves([], mv') = mv'
1405 :     | collectMoves(NODE{movelist, color=ref(SPILLED _), ...}::ns, mv') =
1406 :     let fun ins([], mv') = collectMoves(ns, mv')
1407 :     | ins(MV{status=ref(COALESCED | CONSTRAINED), ...}::mvs,
1408 :     mv') = ins(mvs, mv')
1409 :     | ins((mv as MV{dst, src, ...})::mvs, mv') =
1410 :     (case (chase dst, chase src) of
1411 :     (NODE{color=ref(SPILLED x), number=d, ...},
1412 :     NODE{color=ref(SPILLED y), number=s, ...}) =>
1413 :     if d = s orelse (* trival move *)
1414 :     (x >= 0 andalso y >= 0) (* both are fixed *)
1415 :     then ins(mvs, mv')
1416 :     else ins(mvs, MV.add(mv, mv'))
1417 :     | _ => ins(mvs, mv')
1418 :     )
1419 :     in ins(!movelist, mv') end
1420 :     | collectMoves(_::ns, mv') = collectMoves(ns, mv')
1421 : monnier 475
1422 : monnier 498 val mvs = collectMoves(nodesToSpill, MV.EMPTY)
1423 : monnier 475
1424 : monnier 498 (* Coalesce moves between two spilled nodes *)
1425 :     fun coalesceMoves(MV.EMPTY) = ()
1426 :     | coalesceMoves(MV.TREE(MV{dst, src, cost, ...}, _, l, r)) =
1427 :     let val dst as NODE{color=colorDst, ...} = chase dst
1428 :     val src = chase src
1429 :    
1430 :     (* Make sure that dst is the non-mem reg node *)
1431 :     val (dst, src) =
1432 :     case !colorDst of
1433 :     SPILLED ~1 => (dst, src)
1434 :     | _ => (src, dst)
1435 :    
1436 :     val dst as NODE{number=d, color=colorDst, adj=adjDst,
1437 :     defs=defsDst, uses=usesDst, ...} = dst
1438 :     val src as NODE{number=s, color=colorSrc, adj=adjSrc,
1439 :     defs=defsSrc, uses=usesSrc, ...} = src
1440 : monnier 475
1441 : monnier 498 (* combine adjacency lists *)
1442 :     fun union([], adjSrc) = adjSrc
1443 :     | union((n as NODE{color, adj=adjT,
1444 :     number=t, ...})::adjDst, adjSrc) =
1445 :     (case !color of
1446 :     (SPILLED _ | PSEUDO) =>
1447 :     if addEdge(s, t) then
1448 :     (adjT := src :: !adjT; union(adjDst, n::adjSrc))
1449 :     else union(adjDst, adjSrc)
1450 :     | COLORED _ =>
1451 :     if addEdge(s, t) then union(adjDst, n::adjSrc)
1452 :     else union(adjDst, adjSrc)
1453 :     | _ => union(adjDst, adjSrc)
1454 :     )
1455 :     val mvs = MV.merge(l,r)
1456 :     in if d = s then (* trivial *)
1457 :     coalesceMoves mvs
1458 :     else
1459 :     (case !colorDst of
1460 :     SPILLED x =>
1461 :     if x >= 0 orelse (* both dst and src are mem regs *)
1462 :     member(d, s) (* they interfere *)
1463 :     then
1464 :     ((* print("Bad "^Int.toString d ^
1465 :     "<->"^Int.toString s^"\n")*))
1466 :     else
1467 :     ((* print(Int.toString d ^"<->"^Int.toString s^"\n");*)
1468 :     ra_spill_coal := !ra_spill_coal + 1;
1469 :     (* unify *)
1470 :     colorDst := ALIASED src;
1471 :     adjSrc := union(!adjDst, !adjSrc);
1472 :     if x >= 0 then ()
1473 :     else
1474 :     (defsSrc := concat(!defsDst, !defsSrc);
1475 :     usesSrc := concat(!usesDst, !usesSrc))
1476 :     )
1477 :     | _ => error "coalesceMoves";
1478 :     coalesceMoves mvs
1479 : monnier 469 )
1480 : monnier 498 end
1481 :     in coalesceMoves mvs
1482 :     end
1483 : monnier 427 end
1484 :    
1485 :     (*
1486 : monnier 475 * Spill propagation.
1487 :     *)
1488 : monnier 498 fun spillPropagation(G as GRAPH{bitMatrix, memRegs, ...}) nodesToSpill =
1489 : monnier 475 let val spillCoalescing = spillCoalescing G
1490 :     exception SpillProp
1491 :     val visited = Intmap.new(32, SpillProp) : bool Intmap.intmap
1492 :     val hasBeenVisited = Intmap.mapWithDefault (visited, false)
1493 :     val markAsVisited = Intmap.add visited
1494 : monnier 498 val member = BM.member(!bitMatrix)
1495 : monnier 475
1496 :     (* compute savings due to spill coalescing.
1497 :     * The move list must be associated with a colorable node.
1498 : monnier 498 * The pinned flag is to prevent the spill node from coalescing
1499 :     * two different fixed memory registers.
1500 : monnier 475 *)
1501 : monnier 498 fun coalescingSavings([], pinned, sc) = (pinned, sc+sc)
1502 :     | coalescingSavings(MV{status=ref(CONSTRAINED | COALESCED), ...}::mvs,
1503 :     pinned, sc) = coalescingSavings(mvs, pinned, sc)
1504 :     | coalescingSavings(MV{dst, src, cost, ...}::mvs, pinned, sc) =
1505 :     let val NODE{number=d, color=dstCol, ...} = chase dst
1506 :     val NODE{number=s, color=srcCol, ...} = chase src
1507 :     fun savings(x) =
1508 :     if member(d, s) then coalescingSavings(mvs, pinned, sc)
1509 :     else if x = ~1 then coalescingSavings(mvs, pinned, sc+cost)
1510 :     else if pinned >= 0 andalso pinned <> x then
1511 :     (* already coalesced with another mem reg *)
1512 :     coalescingSavings(mvs, pinned, sc)
1513 :     else
1514 :     (* coalescingSavings(mvs, x, sc+cost) *) (* XXX *)
1515 :     coalescingSavings(mvs, x, sc+cost)
1516 :     in if d = s then
1517 :     coalescingSavings(mvs, pinned, sc)
1518 :     else
1519 :     case (!dstCol, !srcCol) of
1520 :     (SPILLED x, PSEUDO) => savings(x)
1521 :     | (PSEUDO, SPILLED x) => savings(x)
1522 :     | _ => coalescingSavings(mvs, pinned, sc)
1523 :     end
1524 : monnier 475
1525 : monnier 498 (* Insert all spillable neighbors onto the worklist *)
1526 : monnier 475 fun insert([], worklist) = worklist
1527 :     | insert((node as NODE{color=ref PSEUDO, number, ...})::adj, worklist) =
1528 : monnier 498 if hasBeenVisited number
1529 :     then insert(adj, worklist)
1530 : monnier 475 else (markAsVisited (number, true);
1531 :     insert(adj, node::worklist))
1532 :     | insert(_::adj, worklist) = insert(adj, worklist)
1533 :    
1534 :     val marker = SPILLED(~1)
1535 :    
1536 :     (* Process all nodes from the worklist *)
1537 : monnier 498 fun propagate([], spilled) = spilled
1538 :     | propagate((node as NODE{color as ref PSEUDO,
1539 :     pri=ref spillcost, number,
1540 :     adj, movelist, ...})::worklist,
1541 :     spilled) =
1542 :     let val (pinned, savings) = coalescingSavings(!movelist, ~1, 0)
1543 :     in if (if pinned >= 0 then savings > spillcost
1544 :     else savings >= spillcost) (* XXX *)
1545 :     then (* propagate spill *)
1546 : monnier 475 (ra_spill_prop := !ra_spill_prop + 1;
1547 :     color := marker; (* spill the node *)
1548 : monnier 498 (* print("Propagating "^Int.toString number^" "^
1549 :     "savings="^Int.toString(savings)^
1550 :     " cost="^Int.toString spillcost^"\n"); *)
1551 :     (* run spill coalescing *)
1552 :     spillCoalescing [node];
1553 :     propagate(insert(!adj, worklist), node::spilled)
1554 : monnier 475 )
1555 :     else
1556 : monnier 498 propagate(worklist, spilled)
1557 : monnier 475 end
1558 : monnier 498 | propagate(_::worklist, spilled) =
1559 :     propagate(worklist, spilled)
1560 : monnier 475
1561 :     (* Initialize worklist *)
1562 :     fun init([], worklist) = worklist
1563 : monnier 498 | init(NODE{adj, color=ref(SPILLED _), ...}::rest, worklist) =
1564 : monnier 475 init(rest, insert(!adj, worklist))
1565 :     | init(_::rest, worklist) = init(rest, worklist)
1566 :    
1567 :     (*
1568 :     * Iterate between spill coalescing and propagation
1569 :     *)
1570 :     fun iterate(spillWorkList, spilled) =
1571 : monnier 498 let (* run one round of coalescing first *)
1572 :     val _ = spillCoalescing spillWorkList
1573 : monnier 475 val propagationWorkList = init(spillWorkList, [])
1574 : monnier 498 (* iterate on our own spill nodes *)
1575 :     val spilled = propagate(propagationWorkList, spilled)
1576 :     (* try the memory registers too *)
1577 :     val spilled = propagate(!memRegs, spilled)
1578 :     in spilled
1579 : monnier 475 end
1580 : monnier 498
1581 : monnier 475 in iterate(nodesToSpill, nodesToSpill)
1582 :     end
1583 :    
1584 :     (*
1585 : monnier 469 * Spill coloring.
1586 :     * Assign logical spill locations to all the spill nodes.
1587 : monnier 427 *)
1588 : monnier 469 fun spillColoring(GRAPH{spillLoc, ...}) nodesToSpill =
1589 :     let val proh = A.array(length nodesToSpill, ~1)
1590 :     val firstLoc = !spillLoc
1591 :     val _ = spillLoc := firstLoc - 1 (* allocate one location *)
1592 :     fun selectColor([], currLoc) = ()
1593 : monnier 498 | selectColor(NODE{color as ref(SPILLED ~1), number, adj, ...}::nodes,
1594 : monnier 469 currLoc) =
1595 : monnier 498 let fun neighbors [] = ()
1596 : monnier 469 | neighbors(n::ns) =
1597 : monnier 498 let fun mark(NODE{color=ref(SPILLED loc), ...}) =
1598 :     (if loc >= ~1 then () (* no location yet *)
1599 :     else A.update(proh, firstLoc - loc, number);
1600 :     neighbors ns
1601 :     )
1602 :     | mark(NODE{color=ref(ALIASED n), ...}) = mark n
1603 :     | mark _ = neighbors ns
1604 :     in mark n end
1605 : monnier 469 val _ = neighbors(!adj)
1606 :     fun findColor(loc, startingPoint) =
1607 :     let val loc = if loc < firstLoc then !spillLoc + 1 else loc
1608 :     in if A.sub(proh, firstLoc - loc) <> number then loc (* ok *)
1609 :     else if loc = startingPoint then (* new location *)
1610 :     let val loc = !spillLoc
1611 :     in spillLoc := loc - 1; loc end
1612 :     else findColor(loc - 1, startingPoint)
1613 :     end
1614 :     val currLoc = if currLoc < firstLoc then !spillLoc + 1
1615 :     else currLoc
1616 :     val loc = findColor(currLoc, currLoc)
1617 : monnier 498 (* val _ = print("Spill("^Int.toString number^")="^
1618 :     Int.toString loc^"\n") *)
1619 : monnier 469 in color := SPILLED loc;
1620 :     selectColor(nodes, loc - 1)
1621 :     end
1622 :     | selectColor(_::nodes, currLoc) = selectColor(nodes, currLoc)
1623 :     in selectColor(nodesToSpill, firstLoc)
1624 : monnier 427 end
1625 :    
1626 : monnier 469 (*
1627 :     * Update the regmap, after finishing register allocation.
1628 :     * All nodes must have been colored.
1629 :     *)
1630 :     fun finishRA(GRAPH{regmap, nodes, deadCopies, ...}) =
1631 :     let val enter = Intmap.add regmap
1632 :     fun set(r, NODE{color=ref(COLORED c),...}) = enter(r, c)
1633 :     | set(r, NODE{color=ref(ALIASED n),...}) = set(r, n)
1634 : monnier 498 | set(r, NODE{color=ref(SPILLED s),...}) =
1635 :     enter(r,if s >= 0 then s else ~1) (* XXX *)
1636 :     | set(r, _) = error("finishRA "^Int.toString r)
1637 : monnier 469 in Intmap.app set nodes;
1638 :     case !deadCopies of
1639 :     [] => ()
1640 :     | dead => app (fn r => enter(r, ~1)) dead
1641 :     end
1642 : monnier 427
1643 : monnier 469 (*
1644 :     * Update the regmap, after copy propagation
1645 :     *)
1646 :     fun finishCP(GRAPH{regmap, nodes,...}) =
1647 :     let val enter = Intmap.add regmap
1648 :     in Intmap.app
1649 :     (fn (r, node as NODE{color as ref(ALIASED _),...}) =>
1650 :     (case chase node of
1651 :     NODE{color=ref(COLORED c),...} => enter(r, c)
1652 :     | NODE{color=ref PSEUDO, number,...} => enter(r, number)
1653 :     | NODE{color=ref REMOVED, number,...} => enter(r, number)
1654 :     | _ => error "finishCP"
1655 :     )
1656 :     | _ => ()
1657 :     ) nodes
1658 :     end
1659 :    
1660 :     (*
1661 :     * Clear the interference graph, but keep the nodes
1662 :     *)
1663 : monnier 498 fun clearGraph(GRAPH{bitMatrix, maxRegs, trail, spillFlag,
1664 :     deadCopies, memMoves, ...}) =
1665 : monnier 469 let val edges = BM.edges(!bitMatrix)
1666 :     in trail := END;
1667 :     spillFlag := false;
1668 :     bitMatrix := BM.empty;
1669 :     deadCopies := [];
1670 : monnier 498 memMoves := [];
1671 : monnier 469 bitMatrix := G.newBitMatrix{edges=edges, maxRegs=maxRegs()}
1672 :     end
1673 :    
1674 :     fun clearNodes(GRAPH{nodes,...}) =
1675 :     let fun init(_, NODE{pri, degree, adj, movecnt, movelist,
1676 :     movecost, defs, uses, ...}) =
1677 :     (pri := 0; degree := 0; adj := []; movecnt := 0; movelist := [];
1678 :     defs := []; uses := []; movecost := 0)
1679 :     in Intmap.app init nodes
1680 :     end
1681 :    
1682 : monnier 498 end (* local *)
1683 :    
1684 : monnier 469 end

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0