Debian Codegraph @

lib/mlrisc-lib/MLRISC/ra/chaitin-spillheur.sml

raw
1 (*
2 * This module implements the Chaitin heuristic (but weighted by
3 * priorities)
4 *)
5 structure ChaitinSpillHeur : RA_SPILL_HEURISTICS =
6 struct
7
8 structure G = RAGraph
9
10 open G
11
12 exception NoCandidate
13
14 val mode = RACore.NO_OPTIMIZATION
15
16 fun init() = ()
17
18 (*
19 * Potential spill phase.
20 * Find a cheap node to spill according to Chaitin's heuristic.
21 *)
22 fun chooseSpillNode{graph, hasBeenSpilled, spillWkl} =
23 let fun chase(NODE{color=ref(ALIASED n),...}) = chase n
24 | chase n = n
25 val infiniteCost = 123456789.0
26 val don'tUse = 223456789.0
27
28 (* The spill worklist is maintained only lazily. So we have
29 * to prune away those nodes that are already removed from the
30 * interference graph. After pruning the spillWkl,
31 * it may be the case that there aren't anything to be
32 * spilled after all.
33 *)
34
35 (*
36 * Choose node with the lowest cost and have the maximal degree
37 *)
38 fun chaitin([], best, lowestCost, spillWkl) =
39 (best, lowestCost, spillWkl)
40 | chaitin(node::rest, best, lowestCost, spillWkl) =
41 (case chase node of
42 node as NODE{number, pri, defs, uses,
43 degree=ref deg, color=ref PSEUDO,...} =>
44 let fun cost() = !pri / real deg
45 val cost =
46 case (!defs, !uses) of
47 (_,[]) => (* defs but no use *)
48 ~1.0 - real deg
49 | ([d],[u]) => (* defs after use; don't use *)
50 let fun plus({block,insn},n) = {block=block,insn=insn+n}
51 in if d = plus(u,1) orelse d = plus(u,2)
52 then don'tUse else cost()
53 end
54 | _ => cost()
55 in if cost < lowestCost andalso not(hasBeenSpilled number)
56 then
57 (case best of
58 NONE => chaitin(rest, SOME node, cost, spillWkl)
59 | SOME best =>
60 chaitin(rest, SOME node, cost, best::spillWkl)
61 )
62 else
63 chaitin(rest, best, lowestCost, node::spillWkl)
64 end
65 | _ => (* discard node *)
66 chaitin(rest, best, lowestCost, spillWkl)
67 )
68
69 (* val _ = print("["^Int.toString(length spillWkl)^"]") *)
70
71 val (potentialSpillNode, cost, newSpillWkl) =
72 chaitin(spillWkl, NONE, infiniteCost, [])
73 in case (potentialSpillNode, newSpillWkl) of
74 (NONE, []) => {node=NONE, cost=cost, spillWkl=[]}
75 | (NONE, _) => raise NoCandidate
76 | (SOME node, spillWkl) =>
77 {node=SOME node, cost=cost, spillWkl=spillWkl}
78 end
79 end