remove $Id$, it doesn't work with git anyway
[libfirm] / include / libfirm / irdom.h
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief     Construct and access dominator tree.
23  * @author    Goetz Lindenmaier
24  * @date      2.2002
25  * @brief
26  *   This file contains routines to construct and access dominator information.
27  *
28  *   The dominator information is stored in three fields of block nodes:
29  *     - idom: a reference to the block that is the immediate dominator of
30  *       this block.
31  *     - dom_depth: a number giving the depth of the block in the dominator
32  *       tree.
33  *     - pre_num:  Number in preorder traversal.
34  *
35  * We generally presume (like Tarjan) that endless loops do not exist. The
36  * implementation assumes a control dependency from End to loop header.
37  */
38 #ifndef FIRM_ANA_IRDOM_H
39 #define FIRM_ANA_IRDOM_H
40
41 #include "firm_types.h"
42 #include "begin.h"
43
44 /** Accessing the dominator data structure.
45  *
46  * These routines only work properly if the ir_graph is in state
47  * dom_consistent or dom_inconsistent.
48  *
49  * If the block is not reachable from Start, returns a Bad node.
50  */
51 FIRM_API ir_node *get_Block_idom(const ir_node *bl);
52 FIRM_API void set_Block_idom(ir_node *bl, ir_node *n);
53
54 FIRM_API int get_Block_dom_depth(const ir_node *bl);
55 FIRM_API void set_Block_dom_depth(ir_node *bl, int depth);
56
57 FIRM_API int get_Block_dom_pre_num(const ir_node *bl);
58 FIRM_API void set_Block_dom_pre_num(ir_node *bl, int num);
59
60 /** Accessing the post dominator data structure.
61  *
62  * These routines only work properly if the ir_graph is in state
63  * dom_consistent or dom_inconsistent.
64  */
65 FIRM_API ir_node *get_Block_ipostdom(const ir_node *bl);
66 FIRM_API void set_Block_ipostdom(ir_node *bl, ir_node *n);
67
68 FIRM_API int get_Block_postdom_depth(const ir_node *bl);
69 FIRM_API void set_Block_postdom_depth(ir_node *bl, int depth);
70
71 FIRM_API int get_Block_postdom_pre_num(const ir_node *bl);
72 FIRM_API void set_Block_postdom_pre_num(ir_node *bl, int num);
73
74 /**
75  * Get the pre-order number of a block resulting from a
76  * Depth-First-Search walkover the dominator tree.
77  *
78  * @param bl The block.
79  * @return The pre-order number.
80  */
81 FIRM_API unsigned get_Block_dom_tree_pre_num(const ir_node *bl);
82 FIRM_API unsigned get_Block_pdom_tree_pre_num(const ir_node *bl);
83
84 /**
85  * Get the largest pre-order number found in the subtree of the
86  * dominator tree rooted at a given block.
87  * @param bl The block.
88  * @return The largest pre-order number of block's dominator subtree.
89  */
90 FIRM_API unsigned get_Block_dom_max_subtree_pre_num(const ir_node *bl);
91 FIRM_API unsigned get_Block_pdom_max_subtree_pre_num(const ir_node *bl);
92
93 /**
94  * Get the first node in the list of nodes dominated by a given block.
95  *
96  * Each node keeps a list of nodes which it immediately dominates. The
97  * nodes are queued using the @c next pointer in the @c dom_info struct.
98  * Each node keeps a head of this list using the pointer @c first in the
99  * same structure.
100  *
101  * @param bl The block for which to get the first node dominated by @c bl.
102  * @return The first node dominated by @p bl.
103  */
104 FIRM_API ir_node *get_Block_dominated_first(const ir_node *bl);
105 FIRM_API ir_node *get_Block_postdominated_first(const ir_node *bl);
106
107 /**
108  * Get the next node in a list of nodes which are dominated by some
109  * other node.
110  * @see get_Block_dominated_first().
111  * @param dom The previous node.
112  * @return The next node in this list or NULL if it was the last.
113  */
114 FIRM_API ir_node *get_Block_dominated_next(const ir_node *dom);
115 FIRM_API ir_node *get_Block_postdominated_next(const ir_node *dom);
116
117 /**
118  * Iterate over all nodes which are immediately dominated by a given
119  * node.
120  * @param bl   The block whose dominated blocks shall be iterated on.
121  * @param curr An iterator variable of type ir_node*
122  */
123 #define dominates_for_each(bl,curr) \
124         for(curr = get_Block_dominated_first(bl); curr; \
125                         curr = get_Block_dominated_next(curr))
126
127 /**
128  * Iterate over all nodes which are immediately post dominated by a given
129  * node.
130  * @param bl   The block whose post dominated blocks shall be iterated on.
131  * @param curr An iterator variable of type ir_node*
132  */
133 #define postdominates_for_each(bl,curr) \
134         for(curr = get_Block_postdominated_first(bl); curr; \
135                         curr = get_Block_postdominated_next(curr))
136
137 /**
138  * Check, if a block dominates another block.
139  *
140  * @param a   The potential dominator block.
141  * @param b   The potentially dominated block.
142  *
143  * @return 1, if @p a dominates @p b, else 0.
144  */
145 FIRM_API int block_dominates(const ir_node *a, const ir_node *b);
146
147 /**
148  * Check, if a block strictly dominates another block, i.e. a != b.
149  *
150  * @param a The potential dominator block.
151  * @param b The potentially dominated block.
152  *
153  * @return 1, if @p a strictly dominates @p b, else 0.
154  */
155 FIRM_API int block_strictly_dominates(const ir_node *a, const ir_node *b);
156
157 /**
158  * Returns the smallest common dominator block of two nodes.
159  * @param a A node.
160  * @param b Another node.
161  * @return The first block dominating @p a and @p b
162  */
163 FIRM_API ir_node *node_smallest_common_dominator(ir_node *a, ir_node *b);
164
165 /**
166  * Returns the smallest common dominator block of all users of a node
167  * BEWARE: @p irn must not be a block
168  * If on or more users are Phi nodes, one can request special handling
169  * with @p handle_phi = 1.  In this case the cfg predecessor block
170  * corresponding to the position of the irn in the argument list of the
171  * Phi is determined and treated as user.
172  *
173  * @param irn        A node.
174  * @param handle_phi 1 if Phis should be handled different
175  * @return The first block dominating all users of @p irn
176  */
177 FIRM_API ir_node *node_users_smallest_common_dominator(ir_node *irn,
178                                                        int handle_phi);
179
180 /**
181  * Check, if a block post dominates another block.
182  *
183  * @param a The potential post dominator block.
184  * @param b The potentially post dominated block.
185  *
186  * @return 1, if @p a post dominates @p b, else 0.
187  */
188 FIRM_API int block_postdominates(const ir_node *a, const ir_node *b);
189
190 /**
191  * Check, if a block strictly post dominates another block, i.e. a != b.
192  *
193  * @param a The potential post dominator block.
194  * @param b The potentially post dominated block.
195  *
196  * @return 1, if @p a strictly post dominates @p b, else 0.
197  */
198 FIRM_API int block_strictly_postdominates(const ir_node *a, const ir_node *b);
199
200 /**
201  * Visit all nodes in the dominator subtree of a given node.
202  * Call a pre-visitor before descending to the children and call a
203  * post-visitor after returning from them.
204  * @param n The node to start walking from.
205  * @param pre The pre-visitor callback.
206  * @param post The post-visitor callback.
207  * @param env Some custom data passed to the visitors.
208  */
209 FIRM_API void dom_tree_walk(ir_node *n, irg_walk_func *pre,
210                             irg_walk_func *post, void *env);
211
212 /**
213  * Visit all nodes in the post dominator subtree of a given node.
214  * Call a pre-visitor before descending to the children and call a
215  * post-visitor after returning from them.
216  * @param n The node to start walking from.
217  * @param pre The pre-visitor callback.
218  * @param post The post-visitor callback.
219  * @param env Some custom data passed to the visitors.
220  */
221 FIRM_API void postdom_tree_walk(ir_node *n, irg_walk_func *pre,
222                                 irg_walk_func *post, void *env);
223
224 /**
225  * Walk over the dominator tree of an irg starting at the root.
226  * @param irg The graph.
227  * @param pre A pre-visitor to call.
228  * @param post A post-visitor to call.
229  * @param env Some private data to give to the visitors.
230  */
231 FIRM_API void dom_tree_walk_irg(ir_graph *irg, irg_walk_func *pre,
232                                 irg_walk_func *post, void *env);
233
234 /**
235  * Walk over the post dominator tree of an irg starting at the root.
236  * @param irg The graph.
237  * @param pre A pre-visitor to call.
238  * @param post A post-visitor to call.
239  * @param env Some private data to give to the visitors.
240  */
241 FIRM_API void postdom_tree_walk_irg(ir_graph *irg, irg_walk_func *pre,
242                                     irg_walk_func *post, void *env);
243
244 /* ------------ Building and Removing the dominator data structure ----------- */
245
246 /** Computes the dominator trees.
247  *
248  * Sets a flag in irg to "dom_consistent".
249  * If the control flow of the graph is changed this flag must be set to
250  * "dom_inconsistent".
251  * Does not compute dominator information for control dead code.  Blocks
252  * not reachable from Start contain the following information:
253  * @code
254  *   idom = NULL;
255  *   dom_depth = -1;
256  *   pre_num = -1;
257  * @endcode
258  * Also constructs outs information.  As this information is correct after
259  * the run does not free the outs information.
260  */
261 FIRM_API void compute_doms(ir_graph *irg);
262
263 /** Computes the dominator trees on demand, @see compute_doms(). */
264 FIRM_API void assure_doms(ir_graph *irg);
265
266 /** Computes the post dominator trees.
267  *
268  * Sets a flag in irg to "dom_consistent".
269  * If the control flow of the graph is changed this flag must be set to
270  * "dom_inconsistent".
271  * Does not compute post dominator information for endless lops.  Blocks
272  * not reachable from End contain the following information:
273  * @code
274  *   idom = NULL;
275  *   dom_depth = -1;
276  *   pre_num = -1;
277  * @endcode
278  * Also constructs outs information.  As this information is correct after
279  * the run does not free the outs information.
280  */
281 FIRM_API void compute_postdoms(ir_graph *irg);
282
283 /** Computes the dominator trees on demand */
284 FIRM_API void assure_postdoms(ir_graph *irg);
285
286 /** Frees the dominator data structures.  Sets the flag in irg to "dom_none". */
287 FIRM_API void free_dom(ir_graph *irg);
288
289 /**
290  * Frees the post dominator data structures.
291  * Sets the flag in irg to "dom_none".
292  */
293 FIRM_API void free_postdom(ir_graph *irg);
294
295 #include "end.h"
296
297 #endif