added average calculation of distribution table
[libfirm] / ir / ana / irdom.h
1 /*
2  * Project:     libFIRM
3  * File name:   ir/ana/irdom.h
4  * Purpose:     Construct and access dominator tree.
5  * Author:      Goetz Lindenmaier
6  * Modified by:
7  * Created:     2.2002
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 2002-2003 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 /**
14  * @file irdom.h
15  *
16  *   This file contains routines to construct and access dominator information.
17  *
18  *   The dominator information is stored in three fields of block nodes:
19  *     - idom: a reference to the block that is the immediate dominator of
20  *       this block.
21  *     - dom_depth: a number giving the depth of the block in the dominator
22  *       tree.
23  *     - pre_num:  Number in preorder traversal.
24  *
25  * @author Goetz Lindenmaier
26  */
27 #ifndef _IRDOM_H_
28 #define _IRDOM_H_
29
30 #include "firm_types.h"
31
32
33 /** Accessing the dominator data structure.
34  *
35  * These routines only work properly if the ir_graph is in state
36  * dom_consistent or dom_inconsistent.
37  *
38  * If the block is not reachable from Start, returns a Bad node.
39  */
40 ir_node *get_Block_idom(const ir_node *bl);
41 void set_Block_idom(ir_node *bl, ir_node *n);
42
43 int get_Block_dom_depth(const ir_node *bl);
44 void set_Block_dom_depth(ir_node *bl, int depth);
45
46 int get_Block_dom_pre_num(const ir_node *bl);
47 void set_Block_dom_pre_num(ir_node *bl, int num);
48
49 /** Accessing the post dominator data structure.
50  *
51  * These routines only work properly if the ir_graph is in state
52  * dom_consistent or dom_inconsistent.
53  *
54  * If the block is not reachable from End, returns a Bad node.
55  */
56 ir_node *get_Block_ipostdom(const ir_node *bl);
57 void set_Block_ipostdom(ir_node *bl, ir_node *n);
58
59 int get_Block_postdom_depth(const ir_node *bl);
60 void set_Block_postdom_depth(ir_node *bl, int depth);
61
62 int get_Block_postdom_pre_num(const ir_node *bl);
63 void set_Block_postdom_pre_num(ir_node *bl, int num);
64
65 /**
66  * Get the pre-order number of a block resulting from a
67  * Depth-First-Search walkover the dominator tree.
68  *
69  * @param bl The block.
70  * @return The pre-order number.
71  */
72 unsigned get_Block_dom_tree_pre_num(const ir_node *bl);
73
74 /**
75  * Get the largest pre-order number found in the subtree of the
76  * dominator tree rooted at a given block.
77  * @param bl The block.
78  * @return The largest pre-order number of block's dominator subtree.
79  */
80 unsigned get_Block_dom_max_subtree_pre_num(const ir_node *bl);
81
82 /**
83  * Get the first node in the list of nodes dominated by a given block.
84  *
85  * Each node keeps a list of nodes which it immediately dominates. The
86  * nodes are queued using the @c next pointer in the @c dom_info struct.
87  * Each node keeps a head of this list using the pointer @c first in the
88  * same structure.
89  *
90  * @param bl The block for which to get the first node dominated by @c bl.
91  * @return The first node dominated by @p bl.
92  */
93 ir_node *get_Block_dominated_first(const ir_node *bl);
94
95 /**
96  * Get the next node in a list of nodes which are dominated by some
97  * other node.
98  * @see get_Block_dominated_first().
99  * @param dom The previous node.
100  * @return The next node in this list or NULL if it was the last.
101  */
102 ir_node *get_Block_dominated_next(const ir_node *dom);
103
104 /**
105  * Iterate over all nodes which are immediately dominated by a given
106  * node.
107  * @param bl   The block whose dominated blocks shall be iterated on.
108  * @param curr An iterator variable of type ir_node*
109  */
110 #define dominates_for_each(bl,curr) \
111         for(curr = get_Block_dominated_first(bl); curr; \
112                         curr = get_Block_dominated_next(curr))
113
114 /**
115  * Iterate over all nodes which are immediately post dominated by a given
116  * node.
117  * @param bl   The block whose post dominated blocks shall be iterated on.
118  * @param curr An iterator variable of type ir_node*
119  */
120 #define postdominates_for_each(bl,curr) \
121         for(curr = get_Block_postdominated_first(bl); curr; \
122                         curr = get_Block_postdominated_next(curr))
123
124 /**
125  * Check, if a block dominates another block.
126  * @param a The first block.
127  * @param b The second block.
128  * @return 1, if @p a dominates @p b, else 0.
129  */
130 int block_dominates(const ir_node *a, const ir_node *b);
131
132 /**
133  * Check, if a block post dominates another block.
134  * @param a The first block.
135  * @param b The second block.
136  * @return 1, if @p a post dominates @p b, else 0.
137  */
138 int block_postdominates(const ir_node *a, const ir_node *b);
139
140 /**
141  * Visit all nodes in the dominator subtree of a given node.
142  * Call a pre-visitor before descending to the children and call a
143  * post-visitor after returning from them.
144  * @param n The node to start walking from.
145  * @param pre The pre-visitor callback.
146  * @param post The post-visitor callback.
147  * @param env Some custom data passed to the visitors.
148  */
149 void dom_tree_walk(ir_node *n, irg_walk_func *pre,
150                 irg_walk_func *post, void *env);
151
152 /**
153  * Visit all nodes in the post dominator subtree of a given node.
154  * Call a pre-visitor before descending to the children and call a
155  * post-visitor after returning from them.
156  * @param n The node to start walking from.
157  * @param pre The pre-visitor callback.
158  * @param post The post-visitor callback.
159  * @param env Some custom data passed to the visitors.
160  */
161 void postdom_tree_walk(ir_node *n, irg_walk_func *pre,
162                 irg_walk_func *post, void *env);
163
164 /**
165  * Walk over the dominator tree of an irg starting at the root.
166  * @param irg The graph.
167  * @param pre A pre-visitor to call.
168  * @param post A post-visitor to call.
169  * @param env Some private data to give to the visitors.
170  */
171 void dom_tree_walk_irg(ir_graph *irg, irg_walk_func *pre,
172                 irg_walk_func *post, void *env);
173
174 /**
175  * Walk over the post dominator tree of an irg starting at the root.
176  * @param irg The graph.
177  * @param pre A pre-visitor to call.
178  * @param post A post-visitor to call.
179  * @param env Some private data to give to the visitors.
180  */
181 void postdom_tree_walk_irg(ir_graph *irg, irg_walk_func *pre,
182                 irg_walk_func *post, void *env);
183
184 /* ------------ Building and Removing the dominator data structure ----------- */
185
186 /** Computes the dominator trees.
187  *
188  * Sets a flag in irg to "dom_consistent".
189  * If the control flow of the graph is changed this flag must be set to
190  * "dom_inconsistent".
191  * Does not compute dominator information for control dead code.  Blocks
192  * not reachable from Start contain the following information:
193  * @code
194  *   idom = NULL;
195  *   dom_depth = -1;
196  *   pre_num = -1;
197  * @endcode
198  * Also constructs outs information.  As this information is correct after
199  * the run does not free the outs information.
200  */
201 void compute_doms(ir_graph *irg);
202
203 /** Computes the post dominator trees.
204  *
205  * Sets a flag in irg to "dom_consistent".
206  * If the control flow of the graph is changed this flag must be set to
207  * "dom_inconsistent".
208  * Does not compute post dominator information for endless lops.  Blocks
209  * not reachable from End contain the following information:
210  * @code
211  *   idom = NULL;
212  *   dom_depth = -1;
213  *   pre_num = -1;
214  * @endcode
215  * Also constructs outs information.  As this information is correct after
216  * the run does not free the outs information.
217  */
218 void compute_postdoms(ir_graph *irg);
219
220 /** Frees the dominator data structures.  Sets the flag in irg to "dom_none". */
221 void free_dom(ir_graph *irg);
222
223 /** Frees the post dominator data structures.  Sets the flag in irg to "dom_none". */
224 void free_postdom(ir_graph *irg);
225
226 #endif /* _IRDOM_H_ */