Debian Codegraph @

lib/mlrisc-lib/MLRISC/ir-archive/dj-dataflow.sml

raw
1 (*
2 * Perform elimination based dataflow analysis, using Sreedhar's DJ-graph
3 * based algorithm. I'm using the eager elimination method because in
4 * practice it is linear and is much easier to implement.
5 *)
6 functor DJDataflow(Dom : DOMINATOR_TREE) : DJ_DATAFLOW =
7 struct
8
9 structure Dom = Dom
10 structure G = Graph
11 structure A = Array
12
13 val debug = true
14
15 fun error msg = MLRiscErrorMsg.error("DJDataflow",msg)
16
17 fun analyze {closure, var_elim, fixpoint, compute} (Dom as G.GRAPH dom) =
18 let val L = Dom.max_levels Dom
19 val N = #capacity dom ()
20 val CFG as G.GRAPH cfg = Dom.cfg Dom
21 val levelsMap = Dom.levelsMap Dom
22 val idomsMap = Dom.idomsMap Dom
23
24 (*
25 * These store the current join edges during the graph reduction process
26 *)
27 val joinOutEdges = A.array(N,[])
28 val joinInEdges = A.array(N,[])
29
30 (* Priority lists indexed by levels so that all non-join nodes
31 * always appear in front of the list
32 *)
33 val PriorityList = A.array(L,[])
34
35 (* A node i is a non-join node iff all its predecessors are either
36 * i or idom(i)
37 *)
38 fun isNonJoinNode i =
39 case A.sub(joinInEdges,i) of
40 [] => true
41 | [(j,_,_)] => i = j
42 | _ => false
43
44 (* Remove the join edge y -> z with edge id *)
45 fun removeJoinEdge(y,z,id) =
46 let fun rmvEdges((e as (_,_,id'))::es, es') =
47 if id' = id then List.revAppend(es', es)
48 else rmvEdges(es, e::es')
49 | rmvEdges _ = error "rmvEdges"
50 in A.update(joinOutEdges, y, rmvEdges(A.sub(joinOutEdges,y), []));
51 A.update(joinInEdges, z, rmvEdges(A.sub(joinInEdges, z), []))
52 end
53
54 (* Remove all same level i join edges in nodes *)
55 fun removeAllSameLevelEdges(i,nodes) =
56 let fun rmv([], es') = es'
57 | rmv((e as (x,y,_))::es, es') =
58 rmv(es, if A.sub(levelsMap,y) = i then es' else e::es')
59 fun loop [] = ()
60 | loop(x::xs) =
61 (A.update(joinOutEdges, x, rmv(A.sub(joinOutEdges, x), []));
62 A.update(joinInEdges, x, rmv(A.sub(joinInEdges, x), []));
63 loop xs
64 )
65 in loop nodes
66 end
67
68
69 (* Insert a new join edge *)
70 fun insertJoinEdge(e as (x,y,_)) =
71 (A.update(joinOutEdges, x, e::A.sub(joinOutEdges, x));
72 A.update(joinInEdges, y, e::A.sub(joinInEdges, y))
73 )
74
75 (* Does the edge y -> z exists? *)
76 fun doesn'tHasEdge(y,z) =
77 let fun loop [] = true
78 | loop((_,z',_)::es) = z<>z' andalso loop es
79 in loop(A.sub(joinOutEdges, y)) end
80
81 (* Put all nodes into its level in the priority list.
82 * Initialize the join edges arrays.
83 *)
84 fun initialize() =
85 let val edgeId = ref 0
86 in #forall_nodes cfg
87 (fn (i,_) =>
88 let val lvl = A.sub(levelsMap,i)
89 fun addJoinEdges([],id,outEdges) =
90 (A.update(joinOutEdges,i,outEdges); id)
91 | addJoinEdges((i,j,_)::es,id,outEdges) =
92 if A.sub(idomsMap,j) = i then
93 addJoinEdges(es, id, outEdges)
94 else let val e = (i,j,id)
95 in A.update(joinInEdges,j,e::A.sub(joinInEdges,j));
96 addJoinEdges(es, id+1, e::outEdges)
97 end
98 in A.update(PriorityList,lvl,i::A.sub(PriorityList,lvl));
99 edgeId := addJoinEdges(#out_edges cfg i, !edgeId, [])
100 end
101 )
102 end
103
104 (* All these nodes are on the same level and they cannot be reduced.
105 * Determine the SCC for the nodes at level i
106 *)
107 fun CollapseIrreducible(i, joinNodes) =
108 let fun out_edges y =
109 let fun filter([], es') = es'
110 | filter((e as (y,z,_))::es, es') =
111 filter(es, if A.sub(levelsMap,z) = i then e::es' else es')
112 in filter(A.sub(joinOutEdges, y), []) end
113 fun dumpSCC scc =
114 (print "scc:\n";
115 app (fn x =>
116 (print(Int.toString x^" ");
117 app (fn (x,y,_) =>
118 print(Int.toString x^"->"^Int.toString y^" "))
119 (out_edges x);
120 print "\n"
121 ))
122 scc
123 )
124 fun processSCC(scc) =
125 (if debug then dumpSCC scc else ();
126 fixpoint{scc=scc}
127 )
128 val sccs =
129 GraphSCC.scc'
130 {N = #capacity cfg (),
131 nodes = joinNodes,
132 out_edges = out_edges
133 } op:: []
134 in app processSCC sccs;
135 removeAllSameLevelEdges(i,joinNodes)
136 end
137
138 (* Self loops y->y
139 * Compute the closure H_y : O_y = f^*_y(O_y)
140 * Then delete the edge y->y.
141 * y must be a non-join node before and after this transformation.
142 *)
143 fun Eager1(y,id) =
144 (closure{y=y};
145 removeJoinEdge(y,y,id)
146 )
147
148 (* Same level y->z
149 * Eliminate O_y in H_z by replacing it with the RHS of H_y
150 * Delete the edge y->z;
151 * if (z becomes a non-join node) then put z at the head of the
152 * priority list at level i
153 *)
154 fun Eager2a(y,z,i,id,queue,prune) =
155 (var_elim{y=y,z=z};
156 removeJoinEdge(y,z,id);
157 if isNonJoinNode z then (z::queue,true) else (queue,prune)
158 )
159
160 (* Different levels y->z
161 * Eliminate O_y in H_z by replacing it with RHS of H_y
162 * x = idom(y)
163 * Delete the edge y->z
164 * if (x->z does not exist) then
165 * Insert a new J edge x->z
166 *)
167 fun Eager2b(y,z,id) =
168 (var_elim{y=y,z=z};
169 let val x = A.sub(idomsMap,y)
170 in removeJoinEdge(y,z,id);
171 if doesn'tHasEdge(x,z) then insertJoinEdge(x,z,id)
172 else ()
173 end
174 )
175
176 fun ReduceLevel i =
177 let fun loop([], [], _) = ()
178 | loop([], joinNodes, prune) =
179 let fun filter([], ns) = ns
180 | filter(z::zs, ns) =
181 if isNonJoinNode z then filter(zs, ns)
182 else filter(zs, z::ns)
183 (* If prune is true, then the joinNodes list may
184 * contain processed non-join nodes,
185 * so we have to prune them out first.
186 *)
187 val joinNodes =
188 if prune then filter(joinNodes,[]) else joinNodes
189 in case joinNodes of
190 [] => ()
191 | _ => (CollapseIrreducible(i,joinNodes); ReduceLevel i)
192 end
193 | loop(y::Q, joinNodes, prune) =
194 if isNonJoinNode y then
195 let fun process([], Q, prune) = (Q, prune)
196 | process((y,z,id)::es,Q, prune) =
197 if z = y then (Eager1(y,id); process(es, Q, prune))
198 else if A.sub(levelsMap,z) = i then
199 let val (Q, prune) = Eager2a(y,z,i,id,Q,prune)
200 in process(es, Q, prune) end
201 else (Eager2b(y,z,id); process(es, Q, prune))
202 val (Q, prune) = process(A.sub(joinOutEdges, y),Q,prune)
203 in loop(Q, joinNodes, prune)
204 end
205 else loop(Q, y::joinNodes, prune)
206 in loop(A.sub(PriorityList,i), [], false)
207 end
208
209 (* Propagate the results from the top of the dominator tree
210 * down to its children.
211 *)
212 fun DomTDPropagate() =
213 let val ENTRY = hd(#entries dom ())
214 fun walk y =
215 let fun walkChildren [] = ()
216 | walkChildren((_,z,_)::es) =
217 (compute{y=y, z=z}; walk z; walkChildren es)
218 in walkChildren(#out_edges dom y) end
219 in walk ENTRY
220 end
221
222 fun MainDFA() =
223 let fun loop ~1 = ()
224 | loop i = (ReduceLevel i; loop(i-1))
225 in initialize();
226 loop(L-1);
227 DomTDPropagate()
228 end
229
230 in MainDFA()
231 end
232
233 end