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