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