| 1 |
(* |
| 2 |
* The algorithm for computing iterated dominance frontier. |
| 3 |
* This is the algorithm by Sreedhar, Gao and Lee. |
| 4 |
* |
| 5 |
* --Allen |
| 6 |
*) |
| 7 |
|
| 8 |
functor DJGraph (Dom : DOMINATOR_TREE) : DJ_GRAPH = |
| 9 |
struct |
| 10 |
|
| 11 |
structure G = Graph |
| 12 |
structure Dom = Dom |
| 13 |
structure A = Array |
| 14 |
|
| 15 |
type ('n,'e,'g) dj_graph = ('n,'e,'g) Dom.dominator_tree |
| 16 |
|
| 17 |
fun error msg = MLRiscErrorMsg.error("DJGraph",msg) |
| 18 |
|
| 19 |
val stats = false (* collect statistics? *) |
| 20 |
val visitCount = MLRiscControl.getCounter "dj-visit-count" |
| 21 |
val idfCount = MLRiscControl.getCounter "dj-IDF-count" |
| 22 |
val idfSize = MLRiscControl.getCounter "dj-IDF-size" |
| 23 |
val liveVisitCount = MLRiscControl.getCounter "dj-live-visit-count" |
| 24 |
val maxBlockSize = MLRiscControl.getCounter "dj-max-block-size" |
| 25 |
val totalBlockSize = MLRiscControl.getCounter "dj-total-block-size" |
| 26 |
val debug = false |
| 27 |
|
| 28 |
fun DJ x = x |
| 29 |
|
| 30 |
(* Compute dominance frontier *) |
| 31 |
fun DF (D as G.GRAPH dom) = |
| 32 |
let val G.GRAPH cfg = Dom.cfg D |
| 33 |
val L = Dom.max_levels D |
| 34 |
val N = #capacity dom () |
| 35 |
val levels = Dom.levelsMap D |
| 36 |
val in_phi = A.array(N,0) (* has appeared in the DF set? *) |
| 37 |
val stamp = ref 0 |
| 38 |
fun new_stamp() = let val s = !stamp + 1 in stamp := s; s end |
| 39 |
|
| 40 |
fun unmarked(marked,i,stamp : int) = |
| 41 |
let val s = A.sub(marked,i) |
| 42 |
in if s = stamp then false else (A.update(marked,i,stamp); true) |
| 43 |
end |
| 44 |
|
| 45 |
(* |
| 46 |
* Compute the dominance frontiers of a node |
| 47 |
* Dominance frontier of x: |
| 48 |
* The set of all nodes y such that x dominates a predecessor |
| 49 |
* of y but x doesn't strictly dominates y. |
| 50 |
*) |
| 51 |
fun DF x = |
| 52 |
let val stamp = new_stamp() |
| 53 |
val level_x = A.sub(levels,x) |
| 54 |
fun walk(z, S) = |
| 55 |
let fun scan((_,y,_)::es,S) = |
| 56 |
if A.sub(levels,y) <= level_x andalso |
| 57 |
unmarked(in_phi,y,stamp) then scan(es,y::S) |
| 58 |
else scan(es,S) |
| 59 |
| scan([],S) = S |
| 60 |
val S = scan(#out_edges cfg z,S) |
| 61 |
fun walkList([],S) = S |
| 62 |
| walkList((_,z,_)::es,S) = walkList(es,walk(z,S)) |
| 63 |
in walkList(#out_edges dom z,S) |
| 64 |
end |
| 65 |
in walk(x,[]) |
| 66 |
end |
| 67 |
|
| 68 |
in DF end |
| 69 |
|
| 70 |
(* Compute iterated dominance frontier *) |
| 71 |
fun IDFs (D as G.GRAPH dom) = |
| 72 |
let val G.GRAPH cfg = Dom.cfg D |
| 73 |
val L = Dom.max_levels D |
| 74 |
val N = #capacity dom () |
| 75 |
val levels = Dom.levelsMap D |
| 76 |
val in_phi = A.array(N,0) (* has appeared in the DF set? *) |
| 77 |
val stamp = ref 0 |
| 78 |
fun new_stamp() = let val s = !stamp + 1 in stamp := s; s end |
| 79 |
|
| 80 |
fun unmarked(marked,i,stamp : int) = |
| 81 |
let val s = A.sub(marked,i) |
| 82 |
in if s = stamp then false else (A.update(marked,i,stamp); true) |
| 83 |
end |
| 84 |
|
| 85 |
val in_alpha = A.array(N,0) (* has appeared in N_alpha? *) |
| 86 |
val visited = A.array(N,0) (* has it been visited *) |
| 87 |
val piggybank = A.array(L,[]) (* nodes in the piggy bank *) |
| 88 |
|
| 89 |
val n = ref 0 |
| 90 |
(* |
| 91 |
* This algorithm is described in POPL 95 |
| 92 |
*) |
| 93 |
fun IDFs xs = |
| 94 |
let val stamp = new_stamp() |
| 95 |
val _ = if stats then (idfCount := !idfCount + 1; n := !visitCount) |
| 96 |
else () |
| 97 |
fun init([],l) = l |
| 98 |
| init(x::xs,l) = |
| 99 |
let val l_x = A.sub(levels,x) |
| 100 |
in A.update(in_alpha,x,stamp); |
| 101 |
A.update(piggybank,l_x,x::A.sub(piggybank,l_x)); |
| 102 |
init(xs,if l < l_x then l_x else l) |
| 103 |
end |
| 104 |
fun visit(y,level_x,S) = |
| 105 |
let fun scan([],S) = S |
| 106 |
| scan((_,z,_)::es,S) = |
| 107 |
let val level_z = A.sub(levels,z) |
| 108 |
in if level_z <= level_x andalso unmarked(in_phi,z,stamp) |
| 109 |
then (if A.sub(in_alpha,z) <> stamp |
| 110 |
then A.update(piggybank,level_z, |
| 111 |
z::A.sub(piggybank,level_z)) |
| 112 |
else (); |
| 113 |
scan(es,z::S)) |
| 114 |
else scan(es,S) |
| 115 |
end |
| 116 |
fun visitSucc([],S) = S |
| 117 |
| visitSucc((_,z,_)::es,S) = |
| 118 |
visitSucc(es,if unmarked(visited,z,stamp) |
| 119 |
then visit(z,level_x,S) else S) |
| 120 |
val S = scan(#out_edges cfg y,S) |
| 121 |
in if stats then visitCount := !visitCount + 1 else (); |
| 122 |
visitSucc(#out_edges dom y,S) |
| 123 |
end |
| 124 |
|
| 125 |
fun visitAll(~1,S) = S |
| 126 |
| visitAll(l,S) = |
| 127 |
case A.sub(piggybank,l) of |
| 128 |
[] => visitAll(l-1,S) |
| 129 |
| x::xs => (A.update(visited,x,stamp); |
| 130 |
A.update(piggybank,l,xs); |
| 131 |
visitAll(l,visit(x,A.sub(levels,x),S))) |
| 132 |
|
| 133 |
val L = init(xs,~1) |
| 134 |
val IDF = visitAll(L,[]) |
| 135 |
in if stats then |
| 136 |
(idfSize := !idfSize + length IDF; |
| 137 |
maxBlockSize := Int.max(!maxBlockSize, N); |
| 138 |
totalBlockSize := !totalBlockSize + N |
| 139 |
) |
| 140 |
else (); |
| 141 |
if debug then print("N="^Int.toString N^" visits="^ |
| 142 |
Int.toString(!visitCount - !n)^"\n") else (); |
| 143 |
IDF |
| 144 |
end |
| 145 |
|
| 146 |
in IDFs |
| 147 |
end |
| 148 |
|
| 149 |
fun LiveIDFs(D as G.GRAPH dom) = |
| 150 |
let val G.GRAPH cfg = Dom.cfg D |
| 151 |
val L = Dom.max_levels D |
| 152 |
val N = #capacity dom () |
| 153 |
val levels = Dom.levelsMap D |
| 154 |
|
| 155 |
val in_phi = A.array(N,0) (* has appeared in the DF set? *) |
| 156 |
val stamp = ref 0 |
| 157 |
fun new_stamp() = let val s = !stamp + 2 in stamp := s; s end |
| 158 |
|
| 159 |
val in_alpha = A.array(N,0) (* has appeared in N_alpha? *) |
| 160 |
val piggybank = A.array(L,[]) (* nodes in the piggy bank *) |
| 161 |
val liveIn = A.array(N,0) (* is a variable live in *) |
| 162 |
val visited = A.array(N,0) |
| 163 |
|
| 164 |
fun unmarked(marked,i,stamp : int) = |
| 165 |
let val s = A.sub(marked,i) |
| 166 |
in if s = stamp then false else (A.update(marked,i,stamp); true) |
| 167 |
end |
| 168 |
|
| 169 |
fun LiveIDFs {defs, localLiveIn=[]} = [] (* special case *) |
| 170 |
| LiveIDFs {defs=xs, localLiveIn} = |
| 171 |
let val stamp = new_stamp() |
| 172 |
val _ = if stats then idfCount := !idfCount + 1 else () |
| 173 |
(* val n = ref 0 |
| 174 |
val m = ref 0 *) |
| 175 |
|
| 176 |
fun initDefs([],maxLvl) = maxLvl |
| 177 |
| initDefs(x::xs,maxLvl) = |
| 178 |
let val lvl_x = A.sub(levels,x) |
| 179 |
in A.update(in_alpha,x,stamp); |
| 180 |
A.update(piggybank,lvl_x,x::A.sub(piggybank,lvl_x)); |
| 181 |
initDefs(xs,if maxLvl < lvl_x then lvl_x else maxLvl) |
| 182 |
end |
| 183 |
|
| 184 |
fun markLiveIn(b) = |
| 185 |
let fun markPred [] = () |
| 186 |
| markPred((j,_,_)::es) = |
| 187 |
(if A.sub(liveIn,j) <> stamp andalso |
| 188 |
A.sub(in_alpha,j) <> stamp then |
| 189 |
markLiveIn j |
| 190 |
else (); |
| 191 |
markPred es |
| 192 |
) |
| 193 |
in (* m := !m + 1; *) |
| 194 |
A.update(liveIn,b,stamp); |
| 195 |
if stats then liveVisitCount := !liveVisitCount + 1 else (); |
| 196 |
markPred(#in_edges cfg b) |
| 197 |
end |
| 198 |
|
| 199 |
fun initLiveIn [] = () |
| 200 |
| initLiveIn(x::xs) = (markLiveIn x; initLiveIn xs) |
| 201 |
|
| 202 |
fun isLive b = A.sub(liveIn,b) = stamp |
| 203 |
|
| 204 |
fun visit(y,level_x,S) = |
| 205 |
let fun scan([],S) = S |
| 206 |
| scan((_,z,_)::es,S) = |
| 207 |
let val level_z = A.sub(levels,z) |
| 208 |
in if level_z <= level_x andalso |
| 209 |
isLive z andalso |
| 210 |
unmarked(in_phi,z,stamp) |
| 211 |
then (if A.sub(in_alpha,z) <> stamp |
| 212 |
then A.update(piggybank,level_z, |
| 213 |
z::A.sub(piggybank,level_z)) |
| 214 |
else (); |
| 215 |
scan(es,z::S)) |
| 216 |
else scan(es,S) |
| 217 |
end |
| 218 |
fun visitSucc([],S) = S |
| 219 |
| visitSucc((_,z,_)::es,S) = |
| 220 |
visitSucc(es,if isLive z andalso unmarked(visited,z,stamp) |
| 221 |
then visit(z,level_x,S) else S) |
| 222 |
val S = scan(#out_edges cfg y,S) |
| 223 |
in visitSucc(#out_edges dom y,S) |
| 224 |
end |
| 225 |
|
| 226 |
fun visitAll(~1,S) = S |
| 227 |
| visitAll(l,S) = |
| 228 |
case A.sub(piggybank,l) of |
| 229 |
[] => visitAll(l-1,S) |
| 230 |
| x::xs => (A.update(visited,x,stamp); |
| 231 |
A.update(piggybank,l,xs); |
| 232 |
visitAll(l,visit(x,A.sub(levels,x),S))) |
| 233 |
|
| 234 |
val L = initDefs(xs, ~1) |
| 235 |
in initLiveIn(localLiveIn); |
| 236 |
visitAll(L, []) |
| 237 |
end |
| 238 |
|
| 239 |
in LiveIDFs |
| 240 |
end |
| 241 |
|
| 242 |
end |
| 243 |
|