| 1 |
(* |
| 2 |
* This is my E-compressed DJ-graph data structure |
| 3 |
* --Allen |
| 4 |
*) |
| 5 |
|
| 6 |
functor E_DJGraph (Dom : DOMINATOR_TREE) : DJ_GRAPH = |
| 7 |
struct |
| 8 |
|
| 9 |
structure G = Graph |
| 10 |
structure Dom = Dom |
| 11 |
structure A = Array |
| 12 |
|
| 13 |
fun error msg = MLRiscErrorMsg.error("E-DJGraph",msg) |
| 14 |
|
| 15 |
val stats = false (* collect statistics? *) |
| 16 |
val visitCount = MLRiscControl.getCounter "dj-visit-count" |
| 17 |
val idfCount = MLRiscControl.getCounter "dj-IDF-count" |
| 18 |
val idfSize = MLRiscControl.getCounter "dj-IDF-size" |
| 19 |
val liveVisitCount = MLRiscControl.getCounter "dj-live-visit-count" |
| 20 |
|
| 21 |
datatype tree = NODE of int * tree list |
| 22 |
|
| 23 |
datatype ('n,'e,'g) dj_graph = |
| 24 |
DJGRAPH of |
| 25 |
{ dom : ('n,'e,'g) Dom.dominator_tree, |
| 26 |
trees : tree option A.array, |
| 27 |
jedges : int list A.array |
| 28 |
} |
| 29 |
|
| 30 |
fun DJ(Dom as G.GRAPH dom) = |
| 31 |
let val G.GRAPH cfg = Dom.cfg Dom |
| 32 |
val L = Dom.max_levels Dom |
| 33 |
val N = #capacity dom () |
| 34 |
val levelsMap = Dom.levelsMap Dom |
| 35 |
val rank_J = A.array(N, 0) |
| 36 |
val trees = A.array(N, NONE) |
| 37 |
val jedges = A.array(N, []) |
| 38 |
val buckets = A.array(L, []) |
| 39 |
|
| 40 |
fun ExitTrees a = |
| 41 |
let fun foreachDedge [] = () |
| 42 |
| foreachDedge((_,b,_)::es) = (ExitTrees b; foreachDedge es) |
| 43 |
val _ = foreachDedge (#out_edges dom a) |
| 44 |
val lvl_a = A.sub(levelsMap, a) |
| 45 |
fun foreachJedge([], rank) = A.update(rank_J,a,rank) |
| 46 |
| foreachJedge((a,b,_)::es, rank) = |
| 47 |
let val lvl_b = A.sub(levelsMap, b) |
| 48 |
in if lvl_b <= lvl_a then |
| 49 |
foreachJedge(es, if lvl_b < rank then lvl_b else rank) |
| 50 |
else |
| 51 |
foreachJedge(es, rank) |
| 52 |
end |
| 53 |
val _ = foreachJedge (#out_edges cfg a, L+1) |
| 54 |
fun buildTree([], succ) = NODE(a,succ) |
| 55 |
| buildTree((_,b,_)::es, succ) = |
| 56 |
(case A.sub(trees, b) of |
| 57 |
NONE => buildTree(es, succ) |
| 58 |
| SOME t => buildTree(es, t::succ) |
| 59 |
) |
| 60 |
val t_a = buildTree(#out_edges dom a, []) |
| 61 |
in A.update(trees, a, pruneTree(A.sub(levelsMap, a), t_a)) |
| 62 |
end |
| 63 |
|
| 64 |
and pruneTree(lvl_a, NODE(x,succ)) = |
| 65 |
let fun foreachSucc([], subtrees) = subtrees |
| 66 |
| foreachSucc(t::ts, subtrees) = |
| 67 |
foreachSucc(ts, |
| 68 |
case pruneTree(lvl_a, t) of |
| 69 |
NONE => subtrees |
| 70 |
| SOME t => t::subtrees |
| 71 |
) |
| 72 |
val subtrees = foreachSucc(succ, []) |
| 73 |
in case (A.sub(rank_J,x) <= lvl_a, subtrees) of |
| 74 |
(false,[]) => NONE |
| 75 |
| (false,[t]) => SOME t |
| 76 |
| (_,ts) => SOME(NODE(x,ts)) |
| 77 |
end |
| 78 |
|
| 79 |
fun fillJedges l = |
| 80 |
if l < 0 then () else |
| 81 |
let fun fill [] = () |
| 82 |
| fill ((a,b)::es) = |
| 83 |
(A.update(jedges, a, b::A.sub(jedges, a)); fill es) |
| 84 |
in fill(A.sub(buckets, l)); |
| 85 |
fillJedges(l-1) |
| 86 |
end |
| 87 |
|
| 88 |
val [ENTRY] = #entries dom () |
| 89 |
in ExitTrees ENTRY; |
| 90 |
fillJedges(L-1); |
| 91 |
DJGRAPH{dom=Dom, trees=trees, jedges=jedges} |
| 92 |
end |
| 93 |
|
| 94 |
(* Compute dominance frontier *) |
| 95 |
fun DF _ = error "DF" |
| 96 |
|
| 97 |
(* Compute iterated dominance frontier *) |
| 98 |
fun IDFs _ = error "IDFs" |
| 99 |
|
| 100 |
(* Compute iterated dominance frontier with liveness *) |
| 101 |
fun LiveIDFs(DJGRAPH{dom=Dom as G.GRAPH dom, jedges, trees}) = |
| 102 |
let val G.GRAPH cfg = Dom.cfg Dom |
| 103 |
val L = Dom.max_levels Dom |
| 104 |
val N = #capacity dom () |
| 105 |
val levels = Dom.levelsMap Dom |
| 106 |
val in_phi = A.array(N,0) (* has appeared in the DF set? *) |
| 107 |
val liveIn = A.array(N,0) |
| 108 |
val stamp = ref 0 |
| 109 |
fun new_stamp() = let val s = !stamp + 1 in stamp := s; s end |
| 110 |
|
| 111 |
val in_alpha = A.array(N,0) (* has appeared in N_alpha? *) |
| 112 |
val visited = A.array(N,0) (* has it been visited *) |
| 113 |
val piggybank = A.array(L,[]) (* nodes in the piggy bank *) |
| 114 |
|
| 115 |
fun LiveIDFs{defs=xs, localLiveIn=[]} = [] |
| 116 |
| LiveIDFs{defs=xs, localLiveIn} = |
| 117 |
let val stamp = new_stamp() |
| 118 |
val _ = if stats then idfCount := !idfCount + 1 else () |
| 119 |
fun init([],l) = l |
| 120 |
| init(x::xs,l) = |
| 121 |
let val l_x = A.sub(levels,x) |
| 122 |
in A.update(in_alpha,x,stamp); |
| 123 |
A.update(piggybank,l_x,x::A.sub(piggybank,l_x)); |
| 124 |
init(xs,if l < l_x then l_x else l) |
| 125 |
end |
| 126 |
|
| 127 |
fun markLiveIn(b) = |
| 128 |
let fun markPred [] = () |
| 129 |
| markPred((j,_,_)::es) = |
| 130 |
(if A.sub(liveIn,j) <> stamp andalso |
| 131 |
A.sub(in_alpha,j) <> stamp then |
| 132 |
markLiveIn j |
| 133 |
else (); |
| 134 |
markPred es |
| 135 |
) |
| 136 |
in A.update(liveIn,b,stamp); |
| 137 |
if stats then liveVisitCount := !liveVisitCount + 1 else (); |
| 138 |
markPred(#in_edges cfg b) |
| 139 |
end |
| 140 |
|
| 141 |
fun initLiveIn [] = () |
| 142 |
| initLiveIn(x::xs) = (markLiveIn x; initLiveIn xs) |
| 143 |
|
| 144 |
fun isLive b = A.sub(liveIn,b) = stamp |
| 145 |
|
| 146 |
fun visit(x,S) = |
| 147 |
case A.sub(trees,x) of |
| 148 |
NONE => S |
| 149 |
| SOME t => walk(t,A.sub(levels,x),S) |
| 150 |
|
| 151 |
and walk(NODE(y,succ_y),level_x,S) = |
| 152 |
if A.sub(visited,y) <> stamp then |
| 153 |
let val _ = A.update(visited,y,stamp) |
| 154 |
fun foreachJedge([],S) = S |
| 155 |
| foreachJedge(z::zs,S) = |
| 156 |
let val level_z = A.sub(levels,z) |
| 157 |
in if level_z <= level_x then |
| 158 |
if isLive z andalso A.sub(in_phi,z) <> stamp |
| 159 |
(* z is a new IDF^+ candidate; |
| 160 |
* make sure it is live. |
| 161 |
*) |
| 162 |
then (A.update(in_phi,z,stamp); |
| 163 |
if A.sub(in_alpha,z) <> stamp |
| 164 |
then A.update(piggybank,level_z, |
| 165 |
z::A.sub(piggybank,level_z)) |
| 166 |
else (); |
| 167 |
foreachJedge(zs,z::S) |
| 168 |
) |
| 169 |
else foreachJedge(zs,S) |
| 170 |
else S |
| 171 |
end |
| 172 |
fun foreachEedge([], S) = S |
| 173 |
| foreachEedge((t as NODE(z,_))::ts,S) = |
| 174 |
foreachEedge(ts,if isLive z then walk(t,level_x,S) else S) |
| 175 |
|
| 176 |
val _ = if stats then visitCount := !visitCount + 1 else (); |
| 177 |
in foreachEedge(succ_y, foreachJedge(A.sub(jedges, y),S)) |
| 178 |
end |
| 179 |
else S |
| 180 |
|
| 181 |
fun visitAll(~1,S) = S |
| 182 |
| visitAll(l,S) = |
| 183 |
case A.sub(piggybank,l) of |
| 184 |
[] => visitAll(l-1,S) |
| 185 |
| x::xs => (A.update(piggybank,l,xs); |
| 186 |
visitAll(l,visit(x,S))) |
| 187 |
|
| 188 |
val L = init(xs,~1) |
| 189 |
val _ = initLiveIn localLiveIn |
| 190 |
val IDF = visitAll(L,[]) |
| 191 |
in if stats then idfSize := !idfSize + length IDF else (); |
| 192 |
IDF |
| 193 |
end |
| 194 |
|
| 195 |
in LiveIDFs |
| 196 |
end |
| 197 |
|
| 198 |
end |