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 624 - (view) (download)

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

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