Debian Codegraph @

lib/mlrisc-lib/MLRISC/Doc/latex/compiler-graphs.tex

raw
1 \section{Basic Compiler Graphs}
2
3 \subsection{Introduction}
4 In this section we describe the set of core compiler specific graphs and
5 algorithms implemented in MLRISC.
6 Mostly of these algorithms are parameterized with respect
7 to the actual intermediate representation, and as such they
8 do not provide many facilities that are provided by higher abstraction
9 layers, such as in \href{mlrisc-ir.html}{MLRISC IR},
10 or in \href{SSA.html}{SSA}.
11
12 \subsubsection{Dominator/Post-dominator Trees}
13 \newdef{Dominance}
14 is a fundamental concept in compiler optimizations.
15 Node $A$ $dominates$ $B$
16 iff all paths from the start node
17 to $B$ intersects A. A dual notion is the concept of
18 $post-dominance$:
19 $A$ \newdef{post-dominates} $B$ iff all paths from $B$ to the stop node
20 intersects $A$. A (post-)dominator tree can be used
21 to summarize the dominance/post-dominance relationship.
22
23 \begin{SML}
24 functor \mlrischref{ir/dominator.sml}{DominatorTree}
25 (GraphImpl : GRAPH_IMPLEMENTATION) : DOMINATOR_TREE
26 \end{SML}
27 The functor implements dominator analysis and
28 creates a dominator/post-dominator tree from a graph $G$. A dominator tree is implemented as a graph
29 with the following definition:
30 \begin{SML}
31 signature \mlrischref{ir/dominator.sig}{DOMINATOR_TREE} = sig
32 exception Dominator
33 datatype 'n dom_node =
34 DOM of \{ node : 'n, level : int, preorder : int, postorder : int \}
35 type ('n,'e,'g) dom_info
36 type ('n,'e,'g) dominator_tree = ('n dom_node,unit,('n,'e,'g) dom_info) graph
37 type ('n,'e,'g) postdominator_tree = ('n dom_node,unit,('n,'e,'g) dom_info) graph
38 \end{SML}
39
40 We annotated each node in
41 a dominator tree with three extra fields of information, which
42 is useful for other algorithms:
43 \begin{itemize}
44 \item\sml{level} is the nesting level of the tree. The root
45 node has level 0, children of the root has level 1 and so on.
46 \item\sml{preorder} is the preorder numbering of a node
47 \item\sml{preorder} is the postorder numbering of a node.
48 \end{itemize}
49
50 To create a dominator tree and a postdominator tree
51 from a graph, the following function should be called.
52 \begin{SML}
53 val dominator_trees : ('n,'e,'g) graph ->
54 ('n,'e,'g) dominator_tree * ('n,'e,'g) postdominator_tree
55 \end{SML}
56 We use the algorithm of Tarjan and Lengauer, which
57 runs in time $O(|V+E|\alpha(|V+E|))$ where $\alpha$ is the functional
58 inverse of the Ackermann function.
59
60 To perform many common queries on a dominator tree, we first
61 call the function \sml{methods} to obtain a method object.
62 \begin{SML}
63 val methods : ('n,'e,'g) dominator_tree -> dominator_methods
64 \end{SML}
65
66 The methods are packed into the following type:
67 \begin{SML}
68 type dominator_methods =
69 \{ dominates : node_id * node_id -> bool,
70 immediately_dominates : node_id * node_id -> bool,
71 strictly_dominates : node_id * node_id -> bool,
72 postdominates : node_id * node_id -> bool,
73 immediately_postdominates : node_id * node_id -> bool,
74 strictly_postdominates : node_id * node_id -> bool,
75 control_equivalent : node_id * node_id -> bool,
76 idom : node_id -> node_id, $(* ~1 if none *)$
77 idoms : node_id -> node_id list,
78 doms : node_id -> node_id list,
79 ipdom : node_id -> node_id, $(* ~1 if none *)$
80 ipdoms : node_id -> node_id list,
81 pdoms : node_id -> node_id list,
82 dom_lca : node_id * node_id -> node_id,
83 pdom_lca : node_id * node_id -> node_id,
84 dom_level : node_id -> int,
85 pdom_level : node_id -> int,
86 control_equivalent_partitions : unit -> node_id list list
87 \}
88 \end{SML}
89
90 The query methods are as follows:
91 \begin{methods}
92 dominates($a,b$) & returns true iff $a$ dominates $b$ \\
93 immediately\_dominates($a,b$) & returns true iff $a$ immediately dominates $b$ \\
94 strictly\_dominates($a,b$) & returns true iff $a$ strictly dominates $b$ \\
95 postdominates($a,b$) & returns true iff $a$ post-dominates $b$ \\
96 immediately\_postdominates($a,b$) & returns true iff $a$ immediately post-dominates $b$ \\
97 strictly\_postdominates($a,b$) & returns true iff $a$ strictly post-dominates $b$ \\
98 control\_equivalent($a,b$) &
99 returns true iff $a$ dominates $b$ and vice versa \\
100 idom($a$) & returns the immediate dominator of $a$, or $-1$ if none exists \\
101 idoms($a$) & returns all nodes that $a$ immediately dominates \\
102 doms($a$) & returns all nodes that $a$ dominates (including $a$ itself) \\
103 ipdom($a$) & returns the immediate post-dominator of $a$, or $-1$ if none exists \\
104 ipdoms($a$) & returns all nodes that $a$ immediately post-dominates \\
105 pdoms($a$) & returns all nodes that $a$ post-dominates (including $a$ itself) \\
106 dom\_lca($a,b$) & returns the least common ancestor of $a$ and $b$ in
107 the dominator tree \\
108 pdom\_lca($a,b$) & returns the least common ancestor of $a$ and $b$
109 in the post-dominator tree \\
110 dom\_level($a$) & returns the nesting level of $a$ in the dominator tree \\
111 pdom\_level($b$) & returns the nesting level of $a$ in the post-dominator
112 tree \\
113 control\_equivalent\_partitions & partitions the graph into
114 a set of control equivalent nodes.
115 \end{methods}
116
117 The methods \sml{dom_lca}, \sml{pdom_lca} and
118 \sml{control_equivalent_partitions} executes in $O(n)$ time, where
119 $n$ is the size of the dominator tree. The other methods run in $O(1)$ time.
120
121 \subsubsection{Control Dependence Graph}
122 Given two nodes $A$ and $B$ in a control flow graph $G$,
123 we say that $B$ is \newdef{control dependent} on $A$ iff
124 \begin{itemize}
125 \item $B$ post-dominates a successor of $A$
126 \item $B$ does not strictly post-dominates $A$
127 \end{itemize}
128 Intuitively, $B$ is control dependent on $A$ means that
129 some path in the program that goes through $A$ can by-passed $B$,
130 and furthermore, $A$ is the point in which this divergence can occur.
131 Control dependence is used to various kinds of analysis and optimizations in
132 a compiler, such as code motion and global scheduling~\cite{bernstein-rodeh}.
133
134 To build a control dependence graph, the functor
135 \sml{ControlDependenceGraph} can be used:
136 \begin{SML}
137 signature \mlrischref{ir/cdg.sig}{CONTROL_DEPENDENCE_GRAPH} = sig
138 type ('n,'e,'g) cdg = ('n,'e,'g) graph
139
140 val control_dependence_graph :
141 ('e -> bool) ->
142 ('n,'e,'g) dominator_tree *
143 ('n,'e,'g) postdominator_tree ->
144 ('n,'e,'g) cdg
145 end
146 functor \mlrischref{ir/cdg.sml}{ControlDependenceGraph}
147 (structure Dom : DOMINATOR_TREE
148 structure GraphImpl : GRAPH_IMPLEMENTATION
149 ) : CONTROL_DEPENDENCE_GRAPH
150 \end{SML}
151 The control depedence graph is a subcomponent of the
152 program dependence graph commonly used in
153 modern compiler optimizations.
154
155 \subsubsection{Dominance Frontiers}
156
157 Many algorithms involving the notion of control dependence or dominance
158 can be rephrased in terms of \newdef{dominance frontiers}.
159 A node $A$ is in the dominance frontiers of $B$ iff
160 $B$ dominates a predecessor of $A$ but $B$ does not strictly-dominate $A$.
161 We denote this as $A \in DF(B)$.
162 The dual notion of \newdef{post-dominance frontiers} can be defined
163 analogously using the post-dominator tree\footnote{Control dependence
164 can be defined in terms of post-dominance frontiers.}.
165
166 \begin{SML}
167 functor \mlrischref{ir/dominance-frontier.sml}{DominanceFrontiers}(Dom : DOMINATOR_TREE) : DOMINANCE_FRONTIERS
168 \end{SML}
169 The functor \sml{DominanceFrontiers} can be used to
170 compute all the dominance frontiers of all the nodes in a graph.
171 It has the following signature.
172
173 \begin{SML}
174 signature \mlrischref{ir/dominance-frontier.sig}{DOMINANCE_FRONTIERS} = sig
175 structure Dom : DOMINATOR_TREE
176 type dominance_frontiers = node_id list array
177 val DFs : ('n,'e,'g) Dom.dominator_tree -> dominance_frontiers
178 end
179 \end{SML}
180
181 \subsubsection{Iterated Dominance Frontiers}
182
183 \newdef{Iterated dominance frontiers} (denoted as $DF^+$) are defined
184 as the least fixed point of iterating the operation $DF$. Formally,
185 define the dominance frontiers on a set $S$ as follows:
186 \[
187 DF(S) \defas \Union_{A \in S} DF(A)
188 \]
189 Define iteration of $DF$, denoted as $DF^n$, as follows:
190 \begin{eqnarray*}
191 DF^1(S) & \defas & DF(S) \\
192 DF^{n+1}(S) & \defas & DF(S \union DF^n(S)) \\
193 \end{eqnarray*}
194 The iterated dominance frontiers $DF^+(S)$ on a set $S$ are defined as
195 the limit:
196 \[
197 DF^+(S) \defas \lim_{n \to \infty} DF^n(S)
198 \]
199
200 Iterated dominance frontiers of a set $S$ can be computed in
201 time $O(|S|+|V|+|E|)$ using the
202 algorithm by Sreedhar and Gao~\cite{linear-time-IDF}\footnote{
203 In practice it is often sub-linear in $|V|+|E|$.}.
204
205 \begin{SML}
206 functor \mlrischref{ir/djgraph.sml}{DJGraph}(Dom : DOMINATOR_TREE) : DJ_GRAPH
207 \end{SML}
208 The functor \sml{DJGraph} implements this algorithm.
209 It satisfies the signature below:
210 \begin{SML}
211 signature \mlrischref{ir/djgraph.sig}{DJ_GRAPH} = sig
212 structure Dom : DOMINATOR_TREE
213 type ('n,'e,'g) dj_graph = ('n,'e,'g) Dom.dominator_tree
214 val dj_graph : ('n,'e,'g) dj_graph ->
215 \{ DF : node_id -> node_id list,
216 IDF : node_id -> node_id list,
217 IDFs : node_id list -> node_id list
218 \}
219 end
220 \end{SML}
221 The function \sml{dj_graph} takes a dominator tree and returns
222 three query methods for computing dominance and iterated dominance frontiers.
223 Method \sml{DF} computes $DF(v)$ for a single node $v$.
224 Method \sml{IDF} computes the $DF^+(v)$, and method
225 \sml{IDFs} computes $DF^+(S)$ when given a set of node ids.
226 The dominator tree must not be updated while these operations
227 are being performed.
228
229 Sreedhar's original algorithm is phrased in terms of the
230 DJ-graph, which is a fusion of the dominator tree
231 with its underlying flowgraph. Our variant operates on the
232 dominator tree and the flowgraph at the same time, without
233 building an intermediate data structure.
234
235 Iterated dominance frontiers are used
236 in many algorithms that deal with the notion of dominance.
237 For example, our SSA construction algorithm uses iterated
238 dominance frontiers to identify confluent points in the program
239 where $phi$-functions are to be placed.
240
241 \subsubsection{Loop Nesting Tree}
242
243 A \newdef{natural loop} $L$ in a graph is a maximal
244 strongly connected component
245 such that all nodes in $L$ are dominated by a single node $h$, called
246 the \newdef{loop header}. Loops tend to form good optimization candidates
247 and consequently \newdef{loop detection} is an essential task in a compiler.
248 The functor
249 \begin{SML}
250 functor \mlrischref{ir/loop-structure.sml}{LoopStructure}
251 (structure GraphImpl : GRAPH_IMPLEMENTATION
252 structure Dom : DOMINATOR_TREE
253 ) : LOOP_STRUCTURE
254 \end{SML}
255 recognizes all natural loops in a graph and built a
256 \newdef{loop nesting tree}
257 that describes the loop nesting relationship between graphs.
258
259 \begin{SML}
260 signature \mlrischref{ir/loop-structure.sig}{LOOP_STRUCTURE} = sig
261 structure Dom : DOMINATOR_TREE
262 datatype ('n,'e,'g) loop =
263 LOOP of \{ nesting : int,
264 header : node_id,
265 loop_nodes : node_id list,
266 backedges : 'e edge list,
267 exits : 'e edge list
268 \}
269
270 type ('n,'e,'g) loop_info
271 type ('n,'e,'g) loop_structure = (('n,'e,'g) loop,unit, ('n,'e,'g) loop_info) graph
272
273 val loop_structure : ('n,'e,'g) Dom.dominator_tree -> ('n,'e,'g) loop_structure
274 val nesting_level : ('n,'e,'g) loop_structure -> node_id array
275 val header : ('n,'e,'g) loop_structure -> node_id array
276 end
277 \end{SML}
278
279 Our algorithm computes the loop nesting tree in time
280 $O((|V|+|E|)\alpha(|V|+|E|))$.
281 Each node in this tree represents a loop in the flowgraph, except the
282 root of the tree, which represents the entire graph.
283 Given a flowgraph $G$, the root
284 of the loop nesting tree is defined to be the sole vertex in
285 \sml{#entry} $G$. Other nodes in the tree
286 are indexed by the loop header node ids.
287
288 Loop detection classifies each loop and for
289 each loop $L$, the following information is obtained:
290 \begin{itemize}
291 \item An integer \sml{nesting}. The root of the tree has nesting
292 depth 0. The top level loops have nesting depth 1, etc.
293 \item The node id of the loop \sml{header} $h$.
294 \item A set of \sml{loop_nodes}. Loop nodes are
295 nodes that are in the strongly connected
296 component $L$, but excluding the header $h$
297 and all nodes that are part of any nested loops.
298 Thus all nodes are uniquely partitioned in header nodes and
299 loop nodes, and loop nodes are further partitioned into different
300 sets according to which headers they are immediately nested under.
301 \item A set of \sml{backedges}. A back-edge is an
302 edge that targets the header $h$ and originates from a loop node
303 in $L$.
304 \item A set of loop \sml{exits}. An exit-edge is an edge
305 that originates from a loop node within $L$
306 targets a node outside of $L$. Note that this set does not include
307 any exit-edges contained in loops nested in $L$ but
308 target a node out of $L$.
309 \end{itemize}
310
311 \subsubsection{Static Single Assignment}
312
313 An SSA construction algorithm based on~\cite{SSA,Briggs-SSA,linear-time-IDF}
314 is implemented in the following functor:
315 \begin{SML}
316 functor \mlrischref{ir/ssa.sml}{StaticSingleAssignmentForm}
317 (Dom : DOMINATOR_TREE) : STATIC_SINGLE_ASSIGNMENT_FORM
318 \end{SML}
319
320 SSA-based optimizations in MLRISC
321 are actually implemented on top of a
322 high-level SSA layer described in Section~\ref{sec:ssa}.
323 So it is not necessary to use this module directly. Nevertheless,
324 there can be situations in which this module can be specialized in other
325 ways; for example, in the construction of sparse evaluation graphs.
326
327 \begin{SML}
328 signature \mlrischref{ir/ssa.sig}{STATIC_SINGLE_ASSIGNMENT_FORM} = sig
329 structure Dom : DOMINATOR_TREE
330 type var = int
331 type phi = var * var * var list $(* orig def/def/uses *)$
332 type renamer = \{defs : var list, uses: var list\} ->
333 \{defs : var list, uses: var list\}
334 type copy = \{dst : var list, src: var list\} -> unit
335
336 val compute_ssa :
337 ('n,'e,'g) Dom.dominator_tree ->
338 \{ max_var : var,
339 defs : 'n node -> var list,
340 is_live : var * int -> bool,
341 rename_var : var -> var,
342 rename_stmt : \{rename:renamer,copy:copy\} -> 'n node -> unit,
343 insert_phi : \{block : 'n node,
344 in_edges : 'e edge list,
345 phis : phi list
346 \} -> unit
347 \} -> unit
348 end
349 \end{SML}
350
351 This module defines the function \sml{compute_ssa}, which
352 constructs an SSA graph. It requires
353 the following information from the client:
354 \begin{itemize}
355 \item A dominator tree of the flowgraph.
356 \item \sml{max_var} -- the maximum variable id (integer) that exists
357 in the flowgraph. All variables are assumed to be indexed by non-negative
358 integers.
359 \item \sml{defs}($X$) -- a function that returns $defs(X)$,
360 i.e.~the set of variable names defined in block $X$.
361 If a minimal SSA form is desired, this set should include all the definitions
362 in $X$. If a pruned SSA form is required, this set should
363 include only the set of names that are live-out in $X$.
364 \item \sml{is_live}($v,X$) -- a function that determines if
365 variable $v$ is live-in into block $X$. If not, a $\phi$-function will
366 not be placed in $X$. For example, to compute
367 the minimal-SSA form, this function should always return true.
368 \item \sml{rename_var}($v$) -- a function that returns a new
369 unique name for variable $v$.
370 \item \sml{rename_stmt} -- a function of type
371 \sml{{rename:renamer,copy:copy} -> 'n node -> unit} where
372 \begin{SML}
373 type renamer = \{defs : var list, uses: var list\} ->
374 \{defs : var list, uses: var list\}
375 type copy = \{dst : var list, src: var list\} -> unit
376 \end{SML}
377 Function \sml{rename_stmt} is called for each block
378 in the flowgraph in the order of the dominator tree, and
379 is responsible for renaming all the variables in $X$ by
380 calling the functions \sml{renamer} or \sml{copy}.
381 Function \sml{renamer} renames all definitions and uses of
382 a statement, while function \sml{copy} renames
383 of a set of parallel assignments
384 \item \sml{insert_phi}($X$,$es$,$phis$) --
385 a function that inserts a set of
386 $\phi$-definitions $phis$ in block $X$, where $es$
387 is the list of control flow edges that merge into block $X$.
388 \end{itemize}
389
390 \subsubsection{IDEFS/IUSE sets}
391 Reif and Tarjan define the following useful notions for
392 computing approximate birth-points for expressions, which in turn
393 can be used to drive other optimizations.
394 Given a node $X$, let $idom(X)$ denote the immediate dominator of $X$.
395 Let $def(X)$ ($use(X)$) denote all the definitions (uses) in $X$.
396 Given a path $p \equiv v_1\ldots v_n$, define $def(p)$ ($use(p)$) as
397 \begin{eqnarray*}
398 def(v_1\ldots v_n) & \equiv &\union_{i \in 1 \ldots n} def(v_i) \\
399 use(v_1\ldots v_n) & \equiv &\union_{i \in 1 \ldots n} use(v_i)
400 \end{eqnarray*}
401
402 Let $P(X)$ denotes all the paths from $idom(X)$ to $X$
403 that does not cross $idom(X)$ internally. Then define
404 $idef(X)$ ($iuse(X)$) as:
405 \begin{eqnarray*}
406 idef(X) & \equiv & \Union_{idom(X) v_1 \ldots v_n X \in P(X)}
407 def(v_1\ldots v_n) \\
408 iuse(X) & \equiv & \Union_{idom(X) v_1 \ldots v_n X \in P(X)}
409 use(v_1\ldots v_n)
410 \end{eqnarray*}
411 The sets $ipostdef(X)$ and $ipostuse(X)$ are defined analogously
412 using the postdominator tree.
413
414 \begin{SML}
415 signature \mlrischref{ir/idefs2.sig}{IDEFS} = sig
416 type var = int
417 val compute_idefs :
418 \{def_use : 'n Graph.node -> var list * var list,
419 cfg : ('n,'e,'g) Graph.graph
420 \} ->
421 \{ idefuse : unit -> (RegSet.regset * RegSet.regset) Array.array,
422 ipostdefuse : unit -> (RegSet.regset * RegSet.regset) Array.array
423 \}
424 end
425 structure \mlrischref{ir/idefs2.sml}{IDefs} : IDEFS
426 \end{SML}
427 Structure \sml{IDefs} implements the function
428 \sml{comput_idefs} for computing
429 the $idef$, $iuse$, $ipostdef$ and $ipostuse$ sets of a control flow
430 graph. It takes as arguments a flowgraph and a function \sml{def_use}, which
431 takes a graph node and returns the def/use sets of the node.
432 It returns two functions \sml{idefuse} and \sml{ipostdefuse} which
433 compute the $idef/iuse$ and $ipostdef/ipostuse$ sets. These sets
434 are returned as arrays indexed by node ids.