| 1 |
(* |
| 2 |
* This module implements a Chow-Hennessy-style spill heuristic |
| 3 |
*) |
| 4 |
functor ImprovedChowHennessySpillHeur |
| 5 |
(val moveRatio : real) : RA_SPILL_HEURISTICS = |
| 6 |
struct |
| 7 |
|
| 8 |
structure G = RAGraph |
| 9 |
structure Heap = PriorityHeap |
| 10 |
|
| 11 |
open G |
| 12 |
|
| 13 |
exception NoCandidate |
| 14 |
|
| 15 |
val mode = RACore.COMPUTE_SPAN |
| 16 |
|
| 17 |
val cache = ref NONE : (G.node * real) Heap.priority_queue option ref |
| 18 |
|
| 19 |
fun init() = cache := NONE |
| 20 |
|
| 21 |
(* |
| 22 |
* Potential spill phase. |
| 23 |
* Find a cheap node to spill according to Chow Hennessy's heuristic. |
| 24 |
*) |
| 25 |
fun chooseSpillNode{graph as G.GRAPH{span, ...}, |
| 26 |
hasBeenSpilled, spillWkl} = |
| 27 |
let fun chase(NODE{color=ref(ALIASED n),...}) = chase n |
| 28 |
| chase n = n |
| 29 |
|
| 30 |
(* Savings due to coalescing when a node is not spilled *) |
| 31 |
fun moveSavings(NODE{movecnt=ref 0, ...}) = 0.0 |
| 32 |
| moveSavings(NODE{movelist, ...}) = |
| 33 |
let fun loop([], savings) = |
| 34 |
foldr (fn ((_,a),b) => Real.max(a,b)) 0.0 savings |
| 35 |
| loop(MV{status=ref(WORKLIST | GEORGE_MOVE | BRIGGS_MOVE), |
| 36 |
dst, src, cost, ...}::mvs, savings) = |
| 37 |
let fun add(c,[]) = [(c,cost)] |
| 38 |
| add(c,(x as (c':int,s))::savings) = |
| 39 |
if c = c' then (c',s+cost)::savings |
| 40 |
else x::add(c,savings) |
| 41 |
val savings = |
| 42 |
case chase dst of |
| 43 |
NODE{color=ref(COLORED c), ...} => add(c,savings) |
| 44 |
| _ => |
| 45 |
case chase src of |
| 46 |
NODE{color=ref(COLORED c), ...} => add(c,savings) |
| 47 |
| _ => savings |
| 48 |
in loop(mvs, savings) end |
| 49 |
| loop(_::mvs, savings) = loop(mvs, savings) |
| 50 |
in loop(!movelist, []) end |
| 51 |
|
| 52 |
(* The spill worklist is maintained only lazily. So we have |
| 53 |
* to prune away those nodes that are already removed from the |
| 54 |
* interference graph. After pruning the spillWkl, |
| 55 |
* it may be the case that there aren't anything to be |
| 56 |
* spilled after all. |
| 57 |
*) |
| 58 |
fun chowHennessy spills = |
| 59 |
let (* Compute savings due to moves *) |
| 60 |
val spillSavings = RACore.moveSavings graph |
| 61 |
val lookupSpan = IntHashTable.find (Option.valOf(!span)) |
| 62 |
val lookupSpan = |
| 63 |
fn r => case lookupSpan r of SOME s => s | NONE => 0.0 |
| 64 |
val _ = span := NONE |
| 65 |
fun loop([], L, pruned) = (L, pruned) |
| 66 |
| loop(node::rest, L, pruned) = |
| 67 |
(case chase node of |
| 68 |
node as NODE{number, pri, defs, uses, |
| 69 |
degree=ref deg, color=ref PSEUDO,...} => |
| 70 |
if hasBeenSpilled number |
| 71 |
then loop(rest, L, false) |
| 72 |
else |
| 73 |
let fun newnode() = |
| 74 |
let val span = lookupSpan number |
| 75 |
val savings = spillSavings number |
| 76 |
val spillCost = !pri |
| 77 |
val totalCost = spillCost - savings |
| 78 |
(*val rank = ((real totalCost)+0.01) / real(span)*) |
| 79 |
val rank = (totalCost + 0.5 + moveSavings(node)) |
| 80 |
/ (span+ real deg) |
| 81 |
in loop(rest, (node, rank)::L, false) end |
| 82 |
in case (!defs, !uses) of |
| 83 |
(_, []) => (* one def no use *) |
| 84 |
loop(rest, (node, ~1.0 - real(deg))::L, false) |
| 85 |
| ([d], [u]) => (* defs after use; don't use *) |
| 86 |
let fun plus({block,insn},n) = {block=block,insn=insn+n} |
| 87 |
in if d = plus(u,1) orelse d = plus(u,2) then |
| 88 |
loop(rest, L, false) |
| 89 |
else |
| 90 |
newnode() |
| 91 |
end |
| 92 |
| _ => newnode() |
| 93 |
end |
| 94 |
| _ => loop(rest, L, pruned) (* discard node *) |
| 95 |
) |
| 96 |
in loop(spills, [], true) |
| 97 |
end |
| 98 |
|
| 99 |
fun chooseNode heap = |
| 100 |
let fun loop() = |
| 101 |
let val (node,cost) = Heap.deleteMin heap |
| 102 |
in case chase node of |
| 103 |
node as NODE{color=ref PSEUDO, ...} => |
| 104 |
{node=SOME(node), cost=cost, spillWkl=spillWkl} |
| 105 |
| _ => loop() |
| 106 |
end |
| 107 |
in loop() |
| 108 |
end handle _ => {node=NONE, cost=0.0, spillWkl=[]} |
| 109 |
|
| 110 |
in case !cache of |
| 111 |
SOME heap => chooseNode heap |
| 112 |
| NONE => |
| 113 |
let val (L, pruned) = chowHennessy(spillWkl) |
| 114 |
in if pruned then (* done *) |
| 115 |
{node=NONE, cost=0.0, spillWkl=[]} |
| 116 |
else |
| 117 |
(case L of |
| 118 |
[] => raise NoCandidate |
| 119 |
| _ => let fun rank((_,x), (_,y)) = Real.<(x, y) |
| 120 |
val heap = Heap.fromList rank L |
| 121 |
in cache := SOME heap; |
| 122 |
chooseNode heap |
| 123 |
end |
| 124 |
) |
| 125 |
end |
| 126 |
end |
| 127 |
end |