Debian Codegraph @

lib/mlrisc-lib/MLRISC/ir-archive/l-djgraph.sml

raw
1 (*
2 * This is my L-compressed DJ-graph data structure for optimal SSA
3 * construction. For the description of this algorithm, see:
4 * http://www.cs.nyu.edu/leunga/my-papers/linear-ssa.ps
5 * --Allen
6 *)
7
8 functor L_DJGraph (Dom : DOMINATOR_TREE) : DJ_GRAPH =
9 struct
10
11 structure G = Graph
12 structure Dom = Dom
13 structure A = Array
14
15 datatype exit_tree =
16 NODE of { name : int,
17 jedges : int list,
18 eedges : exit_tree list,
19 minVisitedLevel : int ref,
20 visited : int ref
21 }
22
23 datatype ('n,'e,'g) dj_graph =
24 DJGRAPH of
25 { dom : ('n,'e,'g) Dom.dominator_tree,
26 trees : exit_tree list A.array,
27 stamp : int ref
28 }
29
30 fun error msg = MLRiscErrorMsg.error("L-DJGraph",msg)
31
32 val stats = false (* collect statistics? *)
33 val visitCount = MLRiscControl.getCounter "dj-visit-count"
34 val idfCount = MLRiscControl.getCounter "dj-IDF-count"
35 val idfSize = MLRiscControl.getCounter "dj-IDF-size"
36 val liveVisitCount = MLRiscControl.getCounter "dj-live-visit-count"
37 val debug = true
38
39 fun DJ(Dom as G.GRAPH dom) =
40 let val G.GRAPH cfg = Dom.cfg Dom
41 val N = #capacity dom ()
42 val [ENTRY] = #entries dom ()
43 val levelsMap = Dom.levelsMap Dom
44 val L = Dom.max_levels Dom
45 val trees = A.array(N, [])
46 val levels' = A.array(L, ~1)
47 val exitLevels' = A.array(L, ~1)
48 val T = A.array(L, [])
49 val J = A.array(L, [])
50
51 fun LTrees a =
52 let (* recurse *)
53 fun foreachDedge([]) = ()
54 | foreachDedge((_,b,_)::es) = (LTrees b; foreachDedge es)
55
56 val _ = foreachDedge(#out_edges dom a)
57
58 val lvl_a = A.sub(levelsMap, a)
59
60 (* partition J-edges *)
61 fun foreachJedge([], levels) = levels
62 | foreachJedge((_,b,_)::es, levels) =
63 let val lvl_b = A.sub(levelsMap, b)
64 in if lvl_b > lvl_a then (* non-J-edge *)
65 foreachJedge(es, levels)
66 else
67 let val _ =
68 if A.sub(exitLevels',lvl_b) = a then
69 A.update(J, lvl_b, b::A.sub(J, lvl_b))
70 else
71 (A.update(exitLevels',lvl_b,a);
72 A.update(J, lvl_b, [b])
73 )
74 val levels =
75 if A.sub(levels',lvl_b) = a then
76 levels
77 else
78 (A.update(T, lvl_b, []);
79 A.update(levels',lvl_b,a);
80 lvl_b::levels
81 )
82 in foreachJedge(es, levels)
83 end
84 end
85
86 val levels = foreachJedge(#out_edges cfg a, [])
87
88 (* partition subtrees *)
89 fun foreachDedge([], levels) = levels
90 | foreachDedge((_,b,_)::es, levels) =
91 let fun foreachTree([], levels) = levels
92 | foreachTree((l,t)::ts, levels) =
93 let val levels =
94 if l <= lvl_a then
95 if A.sub(levels',l) = a then
96 (A.update(T, l, t::A.sub(T, l)); levels)
97 else (A.update(levels',l,a);
98 A.update(T, l, [t]);
99 l::levels)
100 else levels
101 in foreachTree(ts, levels) end
102 val levels = foreachTree(A.sub(trees, b), levels)
103 in foreachDedge(es, levels)
104 end
105 val levels = foreachDedge(#out_edges dom a, levels)
106
107 (* Build Trees(a) *)
108 fun buildTrees([], trees_a) = trees_a
109 | buildTrees(l::levels, trees_a) =
110 let fun makeNode(succs) =
111 let val jedges = if A.sub(exitLevels',l) = a then
112 A.sub(J,l) else []
113 in (l,NODE{name=a, jedges=jedges, eedges=succs,
114 minVisitedLevel=ref 0, visited=ref 0})
115 end
116 val T_l =
117 case A.sub(T,l) of
118 [] => makeNode []
119 | ts as [t] => if A.sub(exitLevels',l) <> a then (l,t)
120 else makeNode ts
121 | ts => makeNode ts
122 in buildTrees(levels, T_l::trees_a)
123 end
124 val trees_a = buildTrees(levels, [])
125 in A.update(trees, a, trees_a)
126 end
127
128 fun bucketSort(trees) =
129 let val buckets = A.array(L, [])
130 val _ =
131 #forall_nodes dom
132 (fn (a,_) =>
133 let fun insert [] = ()
134 | insert((l,t)::ts) =
135 (A.update(buckets,l,(a,t)::A.sub(buckets,l)); insert ts)
136 in insert(A.sub(trees,a))
137 end)
138 val trees = A.array(N, [])
139 fun collect(l) =
140 if l >= L then ()
141 else let fun dist [] = ()
142 | dist((a,t)::ts) =
143 (A.update(trees,a,t::A.sub(trees,a)); dist ts)
144 in dist(A.sub(buckets, l));
145 collect(l+1)
146 end
147 in collect 0;
148 trees
149 end
150
151 val _ = LTrees ENTRY (* build Trees *)
152 val trees = bucketSort(trees) (* sort trees *)
153 in DJGRAPH{dom=Dom, trees=trees, stamp=ref 0}
154 end
155
156 (* Compute dominance frontier *)
157 fun DF _ = error "DF"
158 fun IDFs _ = error "IDFs"
159
160 fun LiveIDFs (DJGRAPH{trees, dom, stamp, ...}) =
161 let val G.GRAPH cfg = Dom.cfg dom
162 val levelsMap = Dom.levelsMap dom
163 val N = #capacity cfg ()
164 val inphi = A.array(N, 0)
165 val inalpha = A.array(N, 0)
166 val live = A.array(N, 0)
167 fun newStamp() =
168 let val s = Word.toIntX(Word.fromInt(!stamp) + 0w1)
169 in stamp := s; s
170 end
171 fun LiveIDFs{defs, localLiveIn=[]} = []
172 | LiveIDFs{defs, localLiveIn} =
173 let val stamp = newStamp()
174
175 fun initDefs([]) = ()
176 | initDefs(x::xs) = (A.update(inalpha, x, stamp); initDefs xs)
177
178 fun markLiveIn(b) =
179 let fun markPred [] = ()
180 | markPred((j,_,_)::es) =
181 (if A.sub(live,j) <> stamp andalso
182 A.sub(inalpha,j) <> stamp then
183 markLiveIn j
184 else ();
185 markPred es
186 )
187 in (* m := !m + 1; *)
188 A.update(live,b,stamp);
189 if stats then liveVisitCount := !liveVisitCount + 1 else ();
190 markPred(#in_edges cfg b)
191 end
192
193 fun initLiveIn [] = ()
194 | initLiveIn(x::xs) = (markLiveIn x; initLiveIn xs)
195
196 fun isLive b = A.sub(live, b) = stamp
197
198 fun unmarked(X,b) = A.sub(X,b) <> stamp
199 fun mark(X,b) = A.update(X,b,stamp)
200
201 fun visit(x, queue, IDF) =
202 let val level_x = A.sub(levelsMap, x)
203
204 fun walk(NODE{name=y, jedges, eedges, visited, minVisitedLevel},
205 queue, IDF) =
206 let fun foreachJedge([], queue, IDF) = (queue, IDF)
207 | foreachJedge(z::es, queue, IDF) =
208 if isLive z andalso unmarked(inphi,z)
209 then (mark(inphi, z);
210 foreachJedge
211 (es,
212 if unmarked(inalpha,z) then z::queue else queue,
213 z::IDF)
214 )
215 else foreachJedge(es, queue, IDF)
216 fun foreachEEdge([], queue, IDF) = (queue, IDF)
217 | foreachEEdge((z as NODE{name=z',...})::es, queue, IDF) =
218 if isLive z' then
219 let val (queue, IDF) = walk(z, queue, IDF)
220 in foreachEEdge(es, queue, IDF) end
221 else
222 foreachEEdge(es, queue, IDF)
223 in if !visited = stamp then (* visited before *)
224 (minVisitedLevel := Int.min(!minVisitedLevel, level_x);
225 (queue, IDF)
226 )
227 else
228 (minVisitedLevel := level_x; (* non-visited *)
229 visited := stamp;
230 let val (queue, IDF) = foreachJedge(jedges, queue, IDF)
231 in foreachEEdge(eedges, queue, IDF) end
232 )
233 end
234
235 fun ancestorHasBeenProcessed
236 (NODE{visited, minVisitedLevel, ...}) =
237 !visited = stamp andalso !minVisitedLevel < level_x
238
239 fun foreachTree([], queue, IDF) = (queue, IDF)
240 | foreachTree(t::ts, queue, IDF) =
241 if ancestorHasBeenProcessed t then (queue, IDF)
242 else let val (queue, IDF) = walk(t, queue, IDF)
243 in foreachTree(ts, queue, IDF) end
244 in foreachTree(A.sub(trees, x), queue, IDF)
245 end
246
247 fun visitAll([], IDF) = IDF
248 | visitAll(x::queue, IDF) =
249 let val (queue, IDF) = visit(x, queue, IDF)
250 in visitAll(queue, IDF) end
251
252 in initDefs defs;
253 initLiveIn localLiveIn;
254 visitAll(defs, [])
255 end
256 in LiveIDFs
257 end
258
259 end