Clean up need_constraint_copy().
[libfirm] / ir / ana / irloop.c
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    Loop datastructure and access functions -- private stuff.
23  * @author   Goetz Lindenmaier
24  * @date     7.2002
25  * @version  $Id: irloop_t.h 17143 2008-01-02 20:56:33Z beck $
26  */
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #ifdef HAVE_STRING_H
32 # include <string.h>
33 #endif
34 #ifdef HAVE_STDLIB_H
35 # include <stdlib.h>
36 #endif
37
38 #include "irloop_t.h"
39 #include "irprog_t.h"
40
41 void add_loop_son(ir_loop *loop, ir_loop *son) {
42         loop_element lson;
43         assert(loop && loop->kind == k_ir_loop);
44         assert(get_kind(son) == k_ir_loop);
45         lson.son = son;
46         ARR_APP1(loop_element, loop->children, lson);
47         ++loop->n_sons;
48         loop->flags |= loop_outer_loop;
49 }
50
51 void add_loop_node(ir_loop *loop, ir_node *n) {
52         loop_element ln;
53         ln.node = n;
54         assert(loop && loop->kind == k_ir_loop);
55         ARR_APP1(loop_element, loop->children, ln);
56         loop->n_nodes++;
57 }
58
59 void add_loop_irg(ir_loop *loop, ir_graph *irg) {
60         loop_element ln;
61         ln.irg = irg;
62         assert(loop && loop->kind == k_ir_loop);
63         ARR_APP1(loop_element, loop->children, ln);
64         loop->n_nodes++;
65 }
66
67 /**
68  * Mature all loops by removing the flexible arrays of a loop.
69  */
70 void mature_loops(ir_loop *loop, struct obstack *obst) {
71         loop_element *new_children = DUP_ARR_D(loop_element, obst, loop->children);
72         DEL_ARR_F(loop->children);
73         loop->children = new_children;
74
75         if (loop->n_sons > 0) {
76                 /* we have child loops, mature them */
77                 int i;
78
79                 for (i = ARR_LEN(new_children) - 1; i >= 0; --i) {
80                         loop_element child = new_children[i];
81
82                         if (*child.kind == k_ir_loop) {
83                                 mature_loops(child.son, obst);
84                         }
85                 }
86         }
87 }
88
89 /* Returns outer loop, itself if outermost. */
90 ir_loop *(get_loop_outer_loop)(const ir_loop *loop) {
91         return _get_loop_outer_loop(loop);
92 }
93
94 /* Returns nesting depth of this loop */
95 int (get_loop_depth)(const ir_loop *loop) {
96         return _get_loop_depth(loop);
97 }
98
99 /* Returns the number of inner loops */
100 int (get_loop_n_sons)(const ir_loop *loop) {
101         return _get_loop_n_sons(loop);
102 }
103
104 /* Returns the pos`th loop_node-child              *
105  * TODO: This method isn`t very efficient !        *
106  * Returns NULL if there isn`t a pos`th loop_node */
107 ir_loop *get_loop_son(ir_loop *loop, int pos) {
108         int child_nr = 0, loop_nr = -1;
109
110         assert(loop && loop->kind == k_ir_loop);
111         while (child_nr < ARR_LEN(loop->children)) {
112                 if (*(loop->children[child_nr].kind) == k_ir_loop)
113                         loop_nr++;
114                 if (loop_nr == pos)
115                         return loop->children[child_nr].son;
116                 child_nr++;
117         }
118         return NULL;
119 }
120
121 /* Returns the number of nodes in the loop */
122 int get_loop_n_nodes(ir_loop *loop) {
123         assert(loop); assert(loop->kind == k_ir_loop);
124         return loop->n_nodes;
125 }
126
127 /* Returns the pos'th ir_node-child                *
128  * TODO: This method isn't very efficient !        *
129  * Returns NULL if there isn't a pos'th ir_node   */
130 ir_node *get_loop_node(ir_loop *loop, int pos) {
131         int child_nr, node_nr = -1;
132
133         assert(loop && loop->kind == k_ir_loop);
134         assert(pos < get_loop_n_nodes(loop));
135
136         for (child_nr = 0; child_nr < ARR_LEN(loop->children); child_nr++) {
137                 if (*(loop->children[child_nr].kind) == k_ir_node)
138                         node_nr++;
139                 if (node_nr == pos)
140                         return loop -> children[child_nr].node;
141         }
142
143         assert(0 && "no child at pos found");
144         return NULL;
145 }
146
147 /* Returns the number of elements contained in loop.  */
148 int get_loop_n_elements(const ir_loop *loop) {
149         assert(loop && loop->kind == k_ir_loop);
150         return(ARR_LEN(loop->children));
151 }
152
153 /*
154 Returns the pos`th loop element.
155 This may be a loop_node or a ir_node. The caller of this function has
156 to check the *(loop_element.kind) field for "k_ir_node" or "k_ir_loop"
157 and then select the appropriate "loop_element.node" or "loop_element.son".
158 */
159 loop_element get_loop_element(const ir_loop *loop, int pos) {
160         assert(loop && loop->kind == k_ir_loop && pos < ARR_LEN(loop->children));
161         return(loop -> children[pos]);
162 }
163
164 int get_loop_element_pos(const ir_loop *loop, void *le) {
165         int i, n;
166         assert(loop && loop->kind == k_ir_loop);
167
168         n = get_loop_n_elements(loop);
169         for (i = 0; i < n; i++)
170                 if (get_loop_element(loop, i).node == le)
171                         return i;
172         return -1;
173 }
174
175
176 /**
177  * Sets the loop for a node.
178  */
179 void set_irn_loop(ir_node *n, ir_loop *loop) {
180         n->loop = loop;
181 }
182
183 /* Uses temporary information to get the loop */
184 ir_loop *(get_irn_loop)(const ir_node *n) {
185         return _get_irn_loop(n);
186 }
187
188 int get_loop_loop_nr(const ir_loop *loop) {
189         assert(loop && loop->kind == k_ir_loop);
190 #ifdef DEBUG_libfirm
191         return loop->loop_nr;
192 #else
193         return (int)loop;
194 #endif
195 }
196
197 /** A field to connect additional information to a loop.  Only valid
198     if libfirm_debug is set. */
199 void set_loop_link(ir_loop *loop, void *link) {
200         assert(loop && loop->kind == k_ir_loop);
201         loop->link = link;
202 }
203 void *get_loop_link(const ir_loop *loop) {
204         assert(loop && loop->kind == k_ir_loop);
205         return loop->link;
206 }
207
208 int (is_ir_loop)(const void *thing) {
209         return _is_ir_loop(thing);
210 }
211
212 /* The outermost loop is remarked in the surrounding graph. */
213 void (set_irg_loop)(ir_graph *irg, ir_loop *loop) {
214         _set_irg_loop(irg, loop);
215 }
216
217 /* Returns the root loop info (if exists) for an irg. */
218 ir_loop *(get_irg_loop)(ir_graph *irg) {
219         return _get_irg_loop(irg);
220 }
221
222 /*
223  * Allocates a new loop as son of father on the given obstack.
224  * If father is equal NULL, a new root loop is created.
225  */
226 ir_loop *alloc_loop(ir_loop *father, struct obstack *obst) {
227         ir_loop *son;
228
229         son = obstack_alloc(obst, sizeof(*son));
230         memset(son, 0, sizeof(*son));
231         son->kind     = k_ir_loop;
232         son->children = NEW_ARR_F(loop_element, 0);
233         son->n_nodes  = 0;
234         son->n_sons   = 0;
235         son->link     = NULL;
236         if (father) {
237                 son->outer_loop = father;
238                 add_loop_son(father, son);
239                 son->depth = father->depth + 1;
240         } else {  /* The root loop */
241                 son->outer_loop = son;
242                 son->depth      = 0;
243         }
244
245 #ifdef DEBUG_libfirm
246         son->loop_nr = get_irp_new_node_nr();
247 #endif
248
249         return son;
250 }