| 1 |
(* |
| 2 |
* Annotate GC safety-invariants before performing SSA optimizations |
| 3 |
* |
| 4 |
* -- Allen ([email protected]) |
| 5 |
*) |
| 6 |
functor SSAGCInvariants |
| 7 |
(structure SSA : SSA |
| 8 |
structure TypeSys : GC_TYPE_SYSTEM |
| 9 |
sharing SSA.RTL = TypeSys.RTL |
| 10 |
) : SSA_OPTIMIZATION = |
| 11 |
struct |
| 12 |
structure SSA = SSA |
| 13 |
structure CFG = SSA.CFG |
| 14 |
structure I = SSA.I |
| 15 |
structure C = I.C |
| 16 |
structure RTL = SSA.RTL |
| 17 |
structure T = RTL.T |
| 18 |
structure GCMap = TypeSys.GCMap |
| 19 |
structure GC = GCMap.GC |
| 20 |
structure G = Graph |
| 21 |
structure A = Array |
| 22 |
|
| 23 |
type flowgraph = SSA.ssa |
| 24 |
|
| 25 |
val name = "GC Safety-Invariants" |
| 26 |
|
| 27 |
val debug = MLRiscControl.getFlag "ssa-debug-gc" |
| 28 |
|
| 29 |
fun error msg = MLRiscErrorMsg.error("SSAGCInvariants",msg) |
| 30 |
|
| 31 |
fun run SSA = |
| 32 |
let val CFG = SSA.cfg SSA |
| 33 |
in case #get GCMap.GCMAP (!(CFG.annotations CFG)) of |
| 34 |
NONE => SSA |
| 35 |
| SOME gcmap => doTheWork(SSA, CFG, gcmap) |
| 36 |
end |
| 37 |
|
| 38 |
and doTheWork(SSA, CFG, gcmap) = |
| 39 |
let val G.GRAPH ssa = SSA |
| 40 |
val V = SSA.maxVar SSA (* maximum encoding of variables *) |
| 41 |
val N = #capacity ssa () |
| 42 |
val const = SSA.const SSA (* constants map *) |
| 43 |
val defSiteTbl = SSA.defSiteTbl SSA (* definition site *) |
| 44 |
val cellKindMap = SSA.cellKindTbl SSA (* cellKind map *) |
| 45 |
val defsTbl = SSA.defsTbl SSA |
| 46 |
val usesTbl = SSA.usesTbl SSA |
| 47 |
val rtlTbl = SSA.rtlTbl SSA |
| 48 |
val cellKind = IntHashTable.find cellKindMap |
| 49 |
val cellKind = fn r => case cellKind r of SOME k => k | NONE => C.GP |
| 50 |
val updateTy = IntHashTable.insert gcmap |
| 51 |
val zeroR = case C.zeroReg C.GP of |
| 52 |
SOME z => z |
| 53 |
| NONE => ~1 |
| 54 |
|
| 55 |
val gcTypes = A.array(V, GC.BOT) |
| 56 |
val onQueue = BitSet.create N |
| 57 |
val hasType = BitSet.create V |
| 58 |
|
| 59 |
fun joins [] = GC.BOT |
| 60 |
| joins [x] = x |
| 61 |
| joins (x::xs) = GC.join(x, joins xs) |
| 62 |
|
| 63 |
fun initializeTypes() = |
| 64 |
(IntHashTable.appi (fn (r,t) => |
| 65 |
(BitSet.set(hasType,r); A.update(gcTypes, r, t))) gcmap; |
| 66 |
if zeroR >= 0 then A.update(gcTypes, zeroR, GC.CONST 0) else () |
| 67 |
) |
| 68 |
|
| 69 |
fun enqueueInsn(j, WL) = |
| 70 |
if BitSet.markAndTest(onQueue, j) then WL |
| 71 |
else j::WL |
| 72 |
|
| 73 |
fun enqueue(r, WL) = |
| 74 |
let val i = A.sub(defSiteTbl,r) |
| 75 |
fun ins([], WL) = WL |
| 76 |
| ins((_,j,_)::es, WL) = ins(es, enqueueInsn(j, WL)) |
| 77 |
in ins(#out_edges ssa i, WL) end |
| 78 |
|
| 79 |
fun update(r, t, WL) = |
| 80 |
if r = zeroR orelse BitSet.contains(hasType, r) |
| 81 |
then WL |
| 82 |
else let val t' = A.sub(gcTypes, r) |
| 83 |
in if GC.==(t,t') then WL |
| 84 |
else |
| 85 |
((* print("r"^Int.toString r^":"^GC.toString t^"\n");*) |
| 86 |
A.update(gcTypes, r, t); |
| 87 |
enqueue(r, WL)) |
| 88 |
end |
| 89 |
|
| 90 |
fun lookup r = A.sub(gcTypes, r) |
| 91 |
|
| 92 |
val effectOf = TypeSys.effectOf {lookup=lookup, update=update} |
| 93 |
|
| 94 |
fun iterate([]) = () |
| 95 |
| iterate(i::WL) = |
| 96 |
let val _ = BitSet.reset(onQueue,i) |
| 97 |
in iterate(process(i, WL)) end |
| 98 |
|
| 99 |
and process(i,WL) = |
| 100 |
let val rtl = A.sub(rtlTbl,i) |
| 101 |
val defs = A.sub(defsTbl,i) |
| 102 |
val uses = A.sub(usesTbl,i) |
| 103 |
in case (rtl,defs,uses) of |
| 104 |
(T.PHI _,[t],s) => phi(t,s,WL) |
| 105 |
| (T.SOURCE _,t,_) => |
| 106 |
let fun init([],WL) = WL |
| 107 |
| init(t::ts,WL) = init(ts,setTop(t,WL)) |
| 108 |
and setTop(t,WL) = |
| 109 |
if BitSet.contains(hasType, t) then WL |
| 110 |
else (update(t, GC.TOP, WL)) |
| 111 |
in init(t, WL) end |
| 112 |
| (T.SINK _, _, _) => WL |
| 113 |
| (e, t, s) => |
| 114 |
let fun lookupN n = |
| 115 |
let val r = List.nth(s,n) |
| 116 |
in if r < 0 then GC.INT else A.sub(gcTypes,r) end |
| 117 |
fun updateN(n, ty, WL) = update(List.nth(t,n), ty, WL) |
| 118 |
in TypeSys.effectOf{lookup=lookupN, update=updateN} |
| 119 |
{action=e,src=s,dst=t,effect=WL} |
| 120 |
handle e => (print("["^Int.toString i^"] "^ |
| 121 |
SSA.showOp SSA i^"\n"); raise e) |
| 122 |
end |
| 123 |
end |
| 124 |
|
| 125 |
and phi(t, s, WL) = |
| 126 |
if BitSet.contains(hasType, t) then WL |
| 127 |
else let val tys = map lookup s |
| 128 |
val ty = joins tys |
| 129 |
in update(t, ty, WL) end |
| 130 |
|
| 131 |
fun updateTypes() = |
| 132 |
A.appi (fn (r,t) => if GC.==(t,GC.TOP) then () |
| 133 |
else if GC.==(t,GC.BOT) then () |
| 134 |
else updateTy(r,t)) (gcTypes, 0, NONE) |
| 135 |
|
| 136 |
fun typeInference() = |
| 137 |
let val WL = map #1 (#nodes ssa ()) |
| 138 |
in app (fn i => BitSet.set(onQueue,i)) WL; |
| 139 |
iterate WL |
| 140 |
end |
| 141 |
|
| 142 |
fun markConstraints() = |
| 143 |
let val G.GRAPH dom = SSA.dom SSA |
| 144 |
val {ops, ...} = SSA.nodes SSA |
| 145 |
val showOp = SSA.showOp SSA |
| 146 |
fun walk b = |
| 147 |
let fun markAsNonMovable i = |
| 148 |
(if !debug then |
| 149 |
print("Block "^Int.toString b^" can't move: "^ |
| 150 |
showOp i^"\n") |
| 151 |
else (); |
| 152 |
A.update(rtlTbl, i, RTL.pin(A.sub(rtlTbl, i))) |
| 153 |
) |
| 154 |
fun mark(i) = |
| 155 |
let fun isRecoverable [] = true |
| 156 |
| isRecoverable(t::ts) = |
| 157 |
(cellKind t = C.MEM orelse |
| 158 |
cellKind t = C.CTRL orelse |
| 159 |
TypeSys.isRecoverable(A.sub(gcTypes,t))) andalso |
| 160 |
isRecoverable ts |
| 161 |
in case A.sub(rtlTbl,i) of |
| 162 |
(T.PHI _ | T.SOURCE _ | T.SINK _) => () |
| 163 |
| _ => |
| 164 |
if isRecoverable (A.sub(defsTbl,i)) then () (* okay *) |
| 165 |
else markAsNonMovable i |
| 166 |
end |
| 167 |
in app mark (A.sub(ops, b)); |
| 168 |
app walk (#succ dom b) |
| 169 |
end |
| 170 |
|
| 171 |
in walk(hd(#entries dom ())) |
| 172 |
end |
| 173 |
|
| 174 |
in print "GC type inference\n"; |
| 175 |
initializeTypes(); |
| 176 |
typeInference(); |
| 177 |
updateTypes(); |
| 178 |
print "GC type inference done\n"; |
| 179 |
markConstraints(); |
| 180 |
SSA |
| 181 |
end |
| 182 |
|
| 183 |
end |