2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief Loop datastructure and access functions -- private stuff.
23 * @author Goetz Lindenmaier
25 * @version $Id: irloop_t.h 17143 2008-01-02 20:56:33Z beck $
35 void add_loop_son(ir_loop *loop, ir_loop *son) {
37 assert(loop && loop->kind == k_ir_loop);
38 assert(get_kind(son) == k_ir_loop);
40 ARR_APP1(loop_element, loop->children, lson);
42 loop->flags |= loop_outer_loop;
45 void add_loop_node(ir_loop *loop, ir_node *n) {
48 assert(loop && loop->kind == k_ir_loop);
49 ARR_APP1(loop_element, loop->children, ln);
53 void add_loop_irg(ir_loop *loop, ir_graph *irg) {
56 assert(loop && loop->kind == k_ir_loop);
57 ARR_APP1(loop_element, loop->children, ln);
62 * Mature all loops by removing the flexible arrays of a loop.
64 * @param loop the loop to mature
65 * @param obst an obstack, where the new arrays are allocated on
67 void mature_loops(ir_loop *loop, struct obstack *obst) {
68 loop_element *new_children = DUP_ARR_D(loop_element, obst, loop->children);
69 DEL_ARR_F(loop->children);
70 loop->children = new_children;
72 if (loop->n_sons > 0) {
73 /* we have child loops, mature them */
76 for (i = ARR_LEN(new_children) - 1; i >= 0; --i) {
77 loop_element child = new_children[i];
79 if (*child.kind == k_ir_loop) {
80 mature_loops(child.son, obst);
86 /* Returns outer loop, itself if outermost. */
87 ir_loop *(get_loop_outer_loop)(const ir_loop *loop) {
88 return _get_loop_outer_loop(loop);
91 /* Returns nesting depth of this loop */
92 int (get_loop_depth)(const ir_loop *loop) {
93 return _get_loop_depth(loop);
96 /* Returns the number of inner loops */
97 int (get_loop_n_sons)(const ir_loop *loop) {
98 return _get_loop_n_sons(loop);
101 /* Returns the pos`th loop_node-child *
102 * TODO: This method isn`t very efficient ! *
103 * Returns NULL if there isn`t a pos`th loop_node */
104 ir_loop *get_loop_son(ir_loop *loop, int pos) {
105 int child_nr = 0, loop_nr = -1;
107 assert(loop && loop->kind == k_ir_loop);
108 while (child_nr < ARR_LEN(loop->children)) {
109 if (*(loop->children[child_nr].kind) == k_ir_loop)
112 return loop->children[child_nr].son;
118 /* Returns the number of nodes in the loop */
119 int get_loop_n_nodes(const ir_loop *loop) {
120 assert(loop); assert(loop->kind == k_ir_loop);
121 return loop->n_nodes;
124 /* Returns the pos'th ir_node-child *
125 * TODO: This method isn't very efficient ! *
126 * Returns NULL if there isn't a pos'th ir_node */
127 ir_node *get_loop_node(const ir_loop *loop, int pos) {
128 int child_nr, node_nr = -1;
130 assert(loop && loop->kind == k_ir_loop);
131 assert(pos < get_loop_n_nodes(loop));
133 for (child_nr = 0; child_nr < ARR_LEN(loop->children); child_nr++) {
134 if (*(loop->children[child_nr].kind) == k_ir_node)
137 return loop -> children[child_nr].node;
140 assert(0 && "no child at pos found");
144 /* Returns the number of elements contained in loop. */
145 int get_loop_n_elements(const ir_loop *loop) {
146 assert(loop && loop->kind == k_ir_loop);
147 return(ARR_LEN(loop->children));
151 Returns the pos`th loop element.
152 This may be a loop_node or a ir_node. The caller of this function has
153 to check the *(loop_element.kind) field for "k_ir_node" or "k_ir_loop"
154 and then select the appropriate "loop_element.node" or "loop_element.son".
156 loop_element get_loop_element(const ir_loop *loop, int pos) {
157 assert(loop && loop->kind == k_ir_loop && pos < ARR_LEN(loop->children));
158 return(loop -> children[pos]);
161 int get_loop_element_pos(const ir_loop *loop, void *le) {
163 assert(loop && loop->kind == k_ir_loop);
165 n = get_loop_n_elements(loop);
166 for (i = 0; i < n; i++)
167 if (get_loop_element(loop, i).node == le)
174 * Sets the loop for a node.
176 void set_irn_loop(ir_node *n, ir_loop *loop) {
180 ir_loop *(get_irn_loop)(const ir_node *n) {
181 return _get_irn_loop(n);
184 int get_loop_loop_nr(const ir_loop *loop) {
185 assert(loop && loop->kind == k_ir_loop);
187 return loop->loop_nr;
193 void set_loop_link(ir_loop *loop, void *link) {
194 assert(loop && loop->kind == k_ir_loop);
197 void *get_loop_link(const ir_loop *loop) {
198 assert(loop && loop->kind == k_ir_loop);
202 int (is_ir_loop)(const void *thing) {
203 return _is_ir_loop(thing);
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);
211 /* Returns the root loop info (if exists) for an irg. */
212 ir_loop *(get_irg_loop)(const ir_graph *irg) {
213 return _get_irg_loop(irg);
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.
220 ir_loop *alloc_loop(ir_loop *father, struct obstack *obst) {
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);
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;
240 son->loop_nr = get_irp_new_node_nr();