Debian Codegraph @

lib/mlrisc-lib/MLRISC/ir-archive/loop-structure.sml

raw
1 (*
2 * This module is responsible for locating loop structures (intervals).
3 * All loops have only one single entry (via the header) but
4 * potentially multiple exits, i.e. the header dominates all nodes.
5 * Basically this is Tarjan's algorithm.
6 *
7 * The old version is broken as reported by William Chen.
8 * This is a rewrite.
9 *)
10
11 functor LoopStructure (structure GraphImpl : GRAPH_IMPLEMENTATION
12 structure Dom : DOMINATOR_TREE)
13 : LOOP_STRUCTURE =
14 struct
15
16 structure G = Graph
17 structure GI = GraphImpl
18 structure Dom = Dom
19 structure A = Array
20 structure U = URef
21
22 datatype ('n,'e,'g) loop =
23 LOOP of { nesting : int,
24 header : G.node_id,
25 loop_nodes : G.node_id list,
26 backedges : 'e G.edge list,
27 exits : 'e G.edge list
28 }
29
30 datatype ('n,'e,'g) loop_info =
31 INFO of { dom : ('n,'e,'g) Dom.dominator_tree }
32
33 type ('n,'e,'g) loop_structure =
34 (('n,'e,'g) loop, unit, ('n,'e,'g) loop_info) Graph.graph
35
36 fun dom(G.GRAPH{graph_info=INFO{dom,...},...}) = dom
37
38 fun loop_structure DOM =
39 let
40 val info = INFO{ dom = DOM }
41 val G.GRAPH cfg = Dom.cfg DOM
42 val G.GRAPH dom = DOM
43 val N = #capacity dom ()
44 val dominates = Dom.dominates DOM
45 val LS as G.GRAPH ls = GI.graph ("Loop structure",info,N)
46 val ENTRY = case #entries cfg () of
47 [ENTRY] => ENTRY
48 | _ => raise Graph.NotSingleEntry
49
50 (* mapping from node id -> header *)
51 val headers = A.array(N, ~1)
52
53 (* mapping from header -> previous header in the loop *)
54 val lastHeaders = A.array(N, ~1)
55
56 (* mark all visited nodes during construction *)
57 val visited = A.array(N, ~1)
58
59 (* mapping from nodes id -> collapsed header during construction *)
60 val P = A.tabulate(N, U.uRef)
61
62 (* walk the dominator tree and return a list of loops *)
63 fun walk (X, loops) =
64 let
65 (* Look for backedges *)
66 val backedges = List.filter
67 (fn (Y, X, _) => dominates(X, Y)) (#in_edges cfg X)
68 (* X is a header iff it has backedges or X is the ENTRY *)
69 val is_header = case backedges of [] => X = ENTRY | _ => true
70
71 (* Walk the dominator tree first *)
72 val loops = List.foldr walk loops (#succ dom X)
73 in
74 (* If X is a header node then collaspe all the nodes within
75 * the loop into the header. The entry node has to be
76 * treated specially, unfortunately.
77 *)
78 if is_header then
79 let val L = mark(X, X, [])
80 val L = if X = ENTRY then find_entry_loop_nodes [] else L
81 val () = collapse(X, L)
82 val exits = find_exits(L, [])
83 in (* Create a new loop node *)
84 (X, backedges, L, exits)::loops
85 end
86 else
87 loops
88 end
89
90
91 (* mark all the nodes that are within the loop identified
92 * by the header. Return a list of loop nodes.
93 *)
94 and mark(X, header, L) =
95 if A.sub(visited, X) <> header then
96 let
97 (* mark X as visited *)
98 val _ = A.update(visited, X, header)
99
100 (* header of X *)
101 val H_X = A.sub(headers, X)
102
103 val L = if H_X = ~1 then (* X has no header yet *)
104 X::L
105 else if H_X = X andalso A.sub(lastHeaders, X) = ~1 then
106 (* Add loop edge *)
107 (A.update(lastHeaders, X, header);
108 #add_edge ls (header, X, ());
109 L
110 )
111 else L
112 in List.foldr (fn ((Y, _, _), L) =>
113 let val Y = U.!! (A.sub(P, Y))
114 in if dominates(header, Y) then mark(Y, header, L) else L
115 end) L (#in_edges cfg X)
116 end
117 else L
118
119 (* collapse all nodes in L to the header H *)
120 and collapse(H, L) =
121 let val h = A.sub(P, H)
122 in List.app (fn X =>
123 (U.link (A.sub(P, X), h);
124 if A.sub(headers, X) = ~1 then
125 A.update(headers, X, H)
126 else ())) L
127 end
128
129 (* find all nodes that are not part of any loops *)
130 and find_entry_loop_nodes L =
131 List.foldr (fn ((X, _), L) =>
132 if A.sub(headers, X) = ~1 then
133 X::L
134 else if X <> ENTRY andalso
135 A.sub(headers, X) = X andalso
136 A.sub(lastHeaders, X) = ~1 then
137 (#add_edge ls (ENTRY, X, ());
138 A.update(lastHeaders, X, ENTRY);
139 L
140 )
141 else
142 L
143 ) L (#nodes cfg ())
144
145
146 (* find all edges that can exit from the loop H *)
147 and find_exits([],exits) = exits
148 | find_exits(X::Xs,exits) =
149 let fun f((e as (X,Y,_))::es,exits) =
150 if A.sub(headers,Y) = ~1
151 then f(es,e::exits)
152 else f(es,exits)
153 | f([], exits) = exits
154 in find_exits(Xs, f(#out_edges cfg X, exits))
155 end
156
157 (* walk tree and create edges *)
158 val loops = walk (ENTRY, [])
159
160 (* create nodes *)
161 val () = List.app (fn (H, backedges, loop_nodes, exits) =>
162 let val last = A.sub(lastHeaders, H)
163 val nesting = if last = ~1 then 0
164 else
165 let val LOOP{nesting, ...} =
166 #node_info ls last
167 in nesting+1 end
168 in #add_node ls (H, LOOP{nesting = nesting,
169 header = H,
170 backedges = backedges,
171 loop_nodes = loop_nodes,
172 exits = exits})
173 end) loops
174 in
175 LS
176 end
177
178 fun nesting_level(G.GRAPH L) = let
179 val INFO{dom=G.GRAPH dom,...} = #graph_info L
180 val N = #capacity dom ()
181 val levels = A.array(N,0)
182 fun tabulate(_,LOOP{nesting,header,loop_nodes,...}) =
183 (A.update(levels,header,nesting);
184 app (fn i => A.update(levels,i,nesting)) loop_nodes)
185 in
186 #forall_nodes L tabulate; levels
187 end
188
189 fun header(G.GRAPH L) = let
190 val INFO{dom=G.GRAPH dom,...} = #graph_info L
191 val N = #capacity dom ()
192 val headers = A.array(N,0)
193 fun tabulate(_,LOOP{header,loop_nodes,...}) =
194 (A.update(headers,header,header);
195 app (fn i => A.update(headers,i,header)) loop_nodes)
196 in
197 #forall_nodes L tabulate; headers
198 end
199
200 fun entryEdges(Loop as G.GRAPH L) = let
201 val dom = dom Loop
202 val G.GRAPH cfg = Dom.cfg dom
203 val dominates = Dom.dominates dom
204 fun entryEdges(header) =
205 if #has_node L header then
206 List.filter (fn (i,j,_) => not(dominates(j,i)))
207 (#in_edges cfg header)
208 else []
209 in entryEdges
210 end
211
212 fun isBackEdge(Loop as G.GRAPH L) =
213 let val dom = Dom.dominates(dom Loop)
214 in fn (v,w) => #has_node L w andalso dom(w,v)
215 end
216 end
217