irloop: remove get_loop_son, get_loop_node
[libfirm] / ir / ana / irloop.c
1 /*
2  * Copyright (C) 1995-2011 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 #include "error.h"
35
36 void add_loop_son(ir_loop *loop, ir_loop *son)
37 {
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->flags |= loop_outer_loop;
44 }
45
46 void add_loop_node(ir_loop *loop, ir_node *n)
47 {
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 }
53
54 void add_loop_irg(ir_loop *loop, ir_graph *irg)
55 {
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 }
61
62 /**
63  * Mature all loops by removing the flexible arrays of a loop.
64  *
65  * @param loop  the loop to mature
66  * @param obst  an obstack, where the new arrays are allocated on
67  */
68 void mature_loops(ir_loop *loop, struct obstack *obst)
69 {
70         size_t i;
71
72         loop_element *new_children = DUP_ARR_D(loop_element, obst, loop->children);
73         DEL_ARR_F(loop->children);
74         loop->children = new_children;
75
76         /* mature child loops */
77         for (i = ARR_LEN(new_children); i > 0;) {
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 /* Returns outer loop, itself if outermost. */
87 ir_loop *(get_loop_outer_loop)(const ir_loop *loop)
88 {
89         return _get_loop_outer_loop(loop);
90 }
91
92 /* Returns nesting depth of this loop */
93 unsigned (get_loop_depth)(const ir_loop *loop)
94 {
95         return _get_loop_depth(loop);
96 }
97
98 /* Returns the number of elements contained in loop.  */
99 size_t get_loop_n_elements(const ir_loop *loop)
100 {
101         assert(loop && loop->kind == k_ir_loop);
102         return(ARR_LEN(loop->children));
103 }
104
105 loop_element get_loop_element(const ir_loop *loop, size_t pos)
106 {
107         assert(loop && loop->kind == k_ir_loop && pos < ARR_LEN(loop->children));
108         return(loop -> children[pos]);
109 }
110
111 /**
112  * Sets the loop for a node.
113  */
114 void set_irn_loop(ir_node *n, ir_loop *loop)
115 {
116         n->loop = loop;
117 }
118
119 ir_loop *(get_irn_loop)(const ir_node *n)
120 {
121         return _get_irn_loop(n);
122 }
123
124 long get_loop_loop_nr(const ir_loop *loop)
125 {
126         assert(loop && loop->kind == k_ir_loop);
127 #ifdef DEBUG_libfirm
128         return loop->loop_nr;
129 #else
130         return (long)loop;
131 #endif
132 }
133
134 void set_loop_link(ir_loop *loop, void *link)
135 {
136         assert(loop && loop->kind == k_ir_loop);
137         loop->link = link;
138 }
139
140 void *get_loop_link(const ir_loop *loop)
141 {
142         assert(loop && loop->kind == k_ir_loop);
143         return loop->link;
144 }
145
146 int (is_ir_loop)(const void *thing)
147 {
148         return _is_ir_loop(thing);
149 }
150
151 /* The outermost loop is remarked in the surrounding graph. */
152 void (set_irg_loop)(ir_graph *irg, ir_loop *loop)
153 {
154         _set_irg_loop(irg, loop);
155 }
156
157 /* Returns the root loop info (if exists) for an irg. */
158 ir_loop *(get_irg_loop)(const ir_graph *irg)
159 {
160         return _get_irg_loop(irg);
161 }
162
163 /*
164  * Allocates a new loop as son of father on the given obstack.
165  * If father is equal NULL, a new root loop is created.
166  */
167 ir_loop *alloc_loop(ir_loop *father, struct obstack *obst)
168 {
169         ir_loop *son;
170
171         son = OALLOCZ(obst, ir_loop);
172         son->kind     = k_ir_loop;
173         son->children = NEW_ARR_F(loop_element, 0);
174         son->link     = NULL;
175         if (father) {
176                 son->outer_loop = father;
177                 add_loop_son(father, son);
178                 son->depth = father->depth + 1;
179         } else {  /* The root loop */
180                 son->outer_loop = son;
181                 son->depth      = 0;
182         }
183
184 #ifdef DEBUG_libfirm
185         son->loop_nr = get_irp_new_node_nr();
186 #endif
187
188         return son;
189 }