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