| 1 |
(* |
| 2 |
* SCC based global value numbering algorithm (L Taylor Simpson's algorithm) |
| 3 |
* |
| 4 |
* -- Allen ([email protected]) |
| 5 |
*) |
| 6 |
|
| 7 |
functor SSAGlobalValueNumbering |
| 8 |
(CF : SSA_CONSTANT_FOLDING) : SSA_GLOBAL_VALUE_NUMBERING = |
| 9 |
struct |
| 10 |
|
| 11 |
structure SSA = CF.SSA |
| 12 |
structure CF = CF |
| 13 |
structure SP = SSA.SP |
| 14 |
structure CFG = SSA.CFG |
| 15 |
structure Dom = SSA.Dom |
| 16 |
structure I = SSA.I |
| 17 |
structure RTL = SSA.RTL |
| 18 |
structure T = RTL.T |
| 19 |
structure G = Graph |
| 20 |
structure A = Array |
| 21 |
structure H = HashTable |
| 22 |
|
| 23 |
fun error msg = MLRiscErrorMsg.error("SSAGlobalValueNumbering",msg) |
| 24 |
|
| 25 |
val top = CF.top |
| 26 |
|
| 27 |
val dump = MLRiscControl.getFlag "ssa-dump-value-numbers" |
| 28 |
(* |
| 29 |
* SCC based value numbering/constant folding |
| 30 |
*) |
| 31 |
fun computeValueNumbers (SSA as G.GRAPH ssa) = |
| 32 |
let val CFG as G.GRAPH cfg = SSA.cfg SSA |
| 33 |
val Dom as G.GRAPH dom = SSA.dom SSA |
| 34 |
val {sources,phis,ops,sinks} = SSA.nodes SSA |
| 35 |
val N = #capacity ssa () (* number of instructions *) |
| 36 |
val M = #capacity cfg () (* control flow graph *) |
| 37 |
val V = SSA.maxVar SSA (* number of variables *) |
| 38 |
val defsTbl = SSA.defsTbl SSA |
| 39 |
val usesTbl = SSA.usesTbl SSA |
| 40 |
val rtlTbl = SSA.rtlTbl SSA |
| 41 |
val blockTbl = SSA.blockTbl SSA |
| 42 |
val defSiteTbl = SSA.defSiteTbl SSA |
| 43 |
val showOp = SSA.showOp SSA |
| 44 |
val showVal = SSA.showVal SSA |
| 45 |
|
| 46 |
(* |
| 47 |
* Table mapping variables -> value numbers |
| 48 |
*) |
| 49 |
val VN = A.array(V,CF.top) (* value numbers *) |
| 50 |
val DomN = A.array(N,~1) (* dominator numbers *) |
| 51 |
val visited = BitSet.create M |
| 52 |
fun walk(b,n) = |
| 53 |
let fun number([],n) = n |
| 54 |
| number(i::ops,n) = |
| 55 |
(A.update(DomN,i,n); number(ops,n+1)) |
| 56 |
val n = number(A.sub(sources,b),n) |
| 57 |
val n = number(A.sub(phis,b),n) |
| 58 |
val n = number(A.sub(ops,b),n) |
| 59 |
val n = number(A.sub(sinks,b),n) |
| 60 |
fun walkSucc([],n) = n |
| 61 |
| walkSucc((_,b',_)::es,n) = walkSucc(es,walk(b',n)) |
| 62 |
in walkSucc(#out_edges dom b,n) end |
| 63 |
|
| 64 |
val _ = walk(hd(#entries dom ()),0) |
| 65 |
|
| 66 |
exception NotFound |
| 67 |
val validTable = CF.hashTable(V,NotFound) |
| 68 |
val optimisticTable = CF.hashTable(V,NotFound) |
| 69 |
val validLookup = H.lookup validTable |
| 70 |
val validInsert = H.insert validTable |
| 71 |
val optimisticLookup = H.lookup optimisticTable |
| 72 |
val optimisticInsert = H.insert optimisticTable |
| 73 |
|
| 74 |
fun bad(T.PHI _,operands) = List.all (fn r => r = top) operands |
| 75 |
| bad(_,operands) = List.exists (fn r => r = top) operands |
| 76 |
|
| 77 |
fun check(e,operands) = |
| 78 |
(if bad(e,operands) then |
| 79 |
print("Bad rtl: "^RTL.rtlToString e^" "^ |
| 80 |
String.concat(map (fn r => Int.toString r^" ") operands) |
| 81 |
^"\n") |
| 82 |
else (); |
| 83 |
(e,operands)) |
| 84 |
|
| 85 |
(* lookup value number; create new vn if not found *) |
| 86 |
val validSearch = CF.constantFolding SSA |
| 87 |
(fn (e,operands,p,t) => |
| 88 |
validLookup(e,operands,p) handle NotFound => |
| 89 |
(validInsert((e,operands,p),t); t)) |
| 90 |
|
| 91 |
val optimisticSearch = CF.constantFolding SSA |
| 92 |
(fn (e,operands,p,t) => |
| 93 |
optimisticLookup(e,operands,p) handle NotFound => |
| 94 |
(optimisticInsert((e,operands,p),t); t)) |
| 95 |
|
| 96 |
fun dumpSCC ops = |
| 97 |
let fun printVN(i,rtl) = |
| 98 |
let fun pr(t) = |
| 99 |
let val vn = A.sub(VN,t) |
| 100 |
in if vn <> t then print(" VN="^showVal vn^"\n") else () |
| 101 |
end |
| 102 |
in print("\t("^Int.toString(A.sub(DomN,i))^") "^showOp i); |
| 103 |
case A.sub(defsTbl,i) of |
| 104 |
[t] => pr t |
| 105 |
| _ => (); |
| 106 |
print "\n" |
| 107 |
end |
| 108 |
in print "SCC=\n"; |
| 109 |
app printVN ops |
| 110 |
end |
| 111 |
|
| 112 |
fun dumpVN() = |
| 113 |
let fun pr(r,vn) = |
| 114 |
if vn > top andalso vn <> r then |
| 115 |
let val i = A.sub(defSiteTbl, r) |
| 116 |
in print("VN["^showVal r^"] = "^showVal vn^" "^showOp i^"\n") |
| 117 |
end |
| 118 |
else () |
| 119 |
in A.appi pr (VN,0,NONE) |
| 120 |
end |
| 121 |
|
| 122 |
(* |
| 123 |
* compute the fixpoint of an scc |
| 124 |
*) |
| 125 |
fun unique ts = app (fn t => A.update(VN,t,t)) ts |
| 126 |
|
| 127 |
fun isVolatile r = List.exists (fn r' => r' = r) SP.volatile |
| 128 |
|
| 129 |
val zeroR = case I.C.zeroReg I.C.GP of |
| 130 |
SOME zeroR => zeroR |
| 131 |
| NONE => CF.top |
| 132 |
|
| 133 |
fun initSource(t,t') = |
| 134 |
let fun init(t::ts,t'::ts') = |
| 135 |
(A.update(VN,t, |
| 136 |
if t = zeroR then CF.zero |
| 137 |
else if isVolatile t' then CF.volatile |
| 138 |
else t); init(ts,ts')) |
| 139 |
| init _ = () |
| 140 |
in init(t,t') end |
| 141 |
|
| 142 |
fun processSCC (scc,()) = |
| 143 |
let fun init t = A.update(VN,t,top) |
| 144 |
fun inits [] = () |
| 145 |
| inits (t::ts) = (init t; inits ts) |
| 146 |
fun initialize([],ops) = ops |
| 147 |
| initialize(i::is,ops) = |
| 148 |
let val i' = A.sub(rtlTbl, i) |
| 149 |
val t = A.sub(defsTbl, i) |
| 150 |
in case i' of |
| 151 |
T.SOURCE{liveIn, ...} => initSource(t,liveIn) |
| 152 |
| T.SINK _ => () |
| 153 |
| T.COPY _ => inits t |
| 154 |
| T.PHI _ => inits t |
| 155 |
| _ => inits t; |
| 156 |
initialize(is,(i,i')::ops) |
| 157 |
end |
| 158 |
|
| 159 |
val ops = initialize(scc,[]) |
| 160 |
fun byDomN((i,_),(j,_)) = A.sub(DomN,i) > A.sub(DomN,j) |
| 161 |
val ops = ListMergeSort.sort byDomN ops |
| 162 |
|
| 163 |
fun loop([],look,more) = more |
| 164 |
| loop((_,T.SOURCE _)::ops,look,more) = loop(ops,look,more) |
| 165 |
| loop((_,T.SINK _)::ops,look,more) = loop(ops,look,more) |
| 166 |
| loop((i,T.COPY _)::ops, look,more) = |
| 167 |
loop(ops,look, |
| 168 |
processCopy(A.sub(defsTbl,i),A.sub(usesTbl,i),more)) |
| 169 |
| loop((i,e)::ops,look,more) = |
| 170 |
loop(ops,look, |
| 171 |
process(look,e,A.sub(defsTbl,i),A.sub(usesTbl,i),more)) |
| 172 |
|
| 173 |
and compute_vn [] = [] |
| 174 |
| compute_vn (r::rs) = |
| 175 |
(if r < 0 then r else A.sub(VN,r))::compute_vn rs |
| 176 |
|
| 177 |
and processOne(look,e,t,vns,p,changed) = |
| 178 |
let val n = look(e, vns, p, t) |
| 179 |
in (* if RTL.isConditionalBranch e then |
| 180 |
print(RTL.rtlToString e^" vn="^Int.toString n^"\n") |
| 181 |
else (); *) |
| 182 |
if A.sub(VN,t) = n then changed |
| 183 |
else (A.update(VN,t,n); true) |
| 184 |
end |
| 185 |
|
| 186 |
and process(look,e,ts,ss,changed) = |
| 187 |
let val vns = compute_vn ss |
| 188 |
fun processIth([],p,changed) = changed |
| 189 |
| processIth(t::ts,p,changed) = |
| 190 |
processIth(ts, p+1, processOne(look,e,t,vns,p,changed)) |
| 191 |
in processIth(ts,0,changed) |
| 192 |
end |
| 193 |
|
| 194 |
and processCopy(t,s,changed) = |
| 195 |
let val vn = map (fn r => A.sub(VN,r)) s |
| 196 |
fun update(t::ts,vn::vns,changed) = |
| 197 |
if A.sub(VN,t) = vn then update(ts,vns,changed) |
| 198 |
else (A.update(VN,t,vn); update(ts,vns,true)) |
| 199 |
| update(_,_,changed) = changed |
| 200 |
in update(t,vn,changed) end |
| 201 |
|
| 202 |
in case ops of |
| 203 |
[i] => (loop(ops,validSearch,false); (* dumpSCC ops; *) ()) |
| 204 |
| _ => let fun iterate count = |
| 205 |
if loop(ops,optimisticSearch,false) then |
| 206 |
iterate(count+1) |
| 207 |
else count |
| 208 |
val count = iterate 1 |
| 209 |
in (* dumpSCC ops; |
| 210 |
print("["^Int.toString(length ops)^":"^ |
| 211 |
Int.toString(count)^"]"); *) |
| 212 |
loop(ops,validSearch,false); () |
| 213 |
end |
| 214 |
end |
| 215 |
|
| 216 |
(* |
| 217 |
* Initialize all value numbers |
| 218 |
*) |
| 219 |
fun initializeValueNumbers() = |
| 220 |
let val ENTRY = hd(#entries dom ()) |
| 221 |
fun init s = |
| 222 |
let val T.SOURCE{block,...} = A.sub(rtlTbl,s) |
| 223 |
val t = A.sub(defsTbl,s) |
| 224 |
in unique t; |
| 225 |
if block = ENTRY then app initEdge(#out_edges ssa s) else () |
| 226 |
end |
| 227 |
and initEdge(_,_,r) = A.update(VN,r,r) |
| 228 |
in app init (#entries ssa ()); |
| 229 |
case I.C.zeroReg I.C.GP of |
| 230 |
SOME zeroR => A.update(VN,zeroR,CF.zero) |
| 231 |
| NONE => () |
| 232 |
end |
| 233 |
|
| 234 |
in initializeValueNumbers(); |
| 235 |
GraphSCC.scc (ReversedGraphView.rev_view SSA) processSCC (); |
| 236 |
if !dump then dumpVN() else (); |
| 237 |
VN |
| 238 |
end |
| 239 |
|
| 240 |
end |