fixed some Bugs
[libfirm] / ir / ana / structure.h
1 /*
2  * Project:     libFIRM
3  * File name:   ir/ana/structure.h
4  * Purpose:     structure analysis
5  * Author:      Michael Beck
6  * Modified by:
7  * Created:     05.04.2007
8  * CVS-ID:      $Id: $
9  * Copyright:   (c) 2007 Universität Karlsruhe
10  * License:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12 #ifndef _FIRM_STRUCTURE_H
13 #define _FIRM_STRUCTURE_H
14
15 #include "firm_types.h"
16
17 /**
18  * This enum describes the different regions constructed by the structural analysis.
19  */
20 typedef enum ir_region_kind {
21         ir_rk_Unknown,         /**< Unknown region kind. */
22         ir_rk_BasicBlock,      /**< A Basic Block simply wraps a firm basic block, needed for the construction. */
23         /*
24          * part0->part1->...->partn->XXX
25          */
26         ir_rk_Sequence,        /**< A sequence of regions. */
27         /*
28          *    part0
29          *    /  |
30          * part1 |
31          *    \  |
32          *     XXX
33          */
34         ir_rk_IfThen,          /**< An if-then. */
35         /*
36          *     part0
37          *     /   \
38          * part1   part2
39          *     \   /
40          *      XXX
41          */
42         ir_rk_IfThenElse,      /**< An if-then-else. */
43         /*
44          *      part0
45          *     /    \
46          * part1 ... partn
47          *     \    /
48          *      XXX
49          */
50         ir_rk_Case,            /**< A Case like in Pascal. No fall through is allowed. */
51         /*
52          *        part0
53          *     /    |    \
54          * part1->part2 partn
55          *     \         /
56          *         XXX
57          */
58         ir_rk_Switch,          /**< A Switch like in C. At least one fall through exists. */
59         ir_rk_Proper,          /**< A proper region. Any other DAG. */
60         ir_rk_TryCatch,        /**< A TryCatch Exception handling. */
61         ir_rk_TryCatchFinally, /**< A TryCatchFinally Exception handling. */
62         /*
63          *  +-+
64          *  v |
65          * part0
66          */
67         ir_rk_SelfLoop,        /**< A self loop. In Firm always a repeat or endless loop. */
68         /*
69          * part0
70          *  | ^
71          *  v |
72          * part1
73          *  |
74          *  v
75          * XXX
76          */
77         ir_rk_RepeatLoop,      /**< A Repeat loop. */
78         /*
79          * part0 ---> XXX
80          *  | ^
81          *  v |
82          * part1
83          */
84         ir_rk_WhileLoop,       /**< A While loop. */
85         /*
86          * Arbitrary loop with single head.
87          */
88         ir_rk_NaturalLoop,     /**< A natural loop. */
89         ir_rk_Improper,        /**< An improper region. May contain everything. */
90 } ir_region_kind;
91
92 /** Returns non-zero if a region contains loops. */
93 #define is_loop_region(type) ((type) >= ir_rk_SelfLoop)
94
95 /**
96  * Returns the link of a region.
97  *
98  * @param reg  the region
99  */
100 void *get_region_link(const ir_region *reg);
101
102 /**
103  * Sets the link of a region.
104  *
105  * @param reg   the region
106  * @param data  the data
107  */
108 void set_region_link(ir_region *reg, void *data);
109
110 /**
111  * Get the immediate region of a block.
112  *
113  * @param block  a block node
114  */
115 ir_region *get_block_region(const ir_node *block);
116
117 /**
118  * Sets the immediate region of a block.
119  *
120  * @param block  a block node
121  * @param reg    the region
122  */
123 void set_block_region(ir_node *block, ir_region *reg);
124
125 /**
126  * Get the immediate region of a node.
127  *
128  * @param n  a Firm IR node
129  */
130 ir_region *get_irn_region(ir_node *n);
131
132 /**
133  * Return non-if a given firm thing is a region.
134  *
135  * @param thing  a Firm object address
136  */
137 int is_region(const void *thing);
138
139 /**
140  * Return the number of predecessors in a region.
141  *
142  * @param reg  the region
143  */
144 int get_region_n_preds(const ir_region *reg);
145
146 /**
147  * Return the predecessor region at position pos.
148  *
149  * @param reg  the region
150  */
151 ir_region *get_region_pred(const ir_region *reg, int pos);
152
153 /**
154  * Set the predecessor region at position pos.
155  *
156  * @param reg  the region
157  * @param pos  the position number
158  * @param n    the new predecessor region
159  */
160 void set_region_pred(ir_region *reg, int pos, ir_region *n);
161
162 /**
163  * Return the number of successors in a region.
164  *
165  * @param reg  the region
166  */
167 int get_region_n_succs(const ir_region *reg);
168
169 /**
170  * Return the successor region at position pos.
171  *
172  * @param reg  the region
173  * @param pos  the position number
174  */
175 ir_region *get_region_succ(const ir_region *reg, int pos);
176
177 /**
178  * Set the successor region at position pos.
179  *
180  * @param reg  the region
181  * @param pos  the position number
182  * @param n    the new successor region
183  */
184 void set_region_succ(ir_region *reg, int pos, ir_region *n);
185
186 /**
187  * Construct the region tree of a graph by doing
188  * structural analysis.
189  *
190  * Uses link fields of nodes.
191  *
192  * @param irg  the graph
193  *
194  * @return the region tree
195  */
196 ir_reg_tree *construct_region_tree(ir_graph *irg);
197
198 /**
199  * Walk over the region tree.
200  *
201  * @param tree  the tree
202  * @param pre   walker function, executed before the children of a tree node are visited
203  * @param post  walker function, executed after the children of a tree node are visited
204  * @param env   environment, passed to pre and post
205  */
206 void region_tree_walk(ir_reg_tree *tree, irg_reg_walk_func *pre, irg_reg_walk_func *post, void *env);
207
208 #endif /* _FIRM_STRUCTURE_H */