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