verify: Clarify assertion message.
[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  */
26 #include "config.h"
27
28 #include <string.h>
29 #include <stdlib.h>
30
31 #include "irloop_t.h"
32 #include "irprog_t.h"
33 #include "error.h"
34
35 void add_loop_son(ir_loop *loop, ir_loop *son)
36 {
37         loop_element lson;
38         assert(loop && loop->kind == k_ir_loop);
39         assert(get_kind(son) == k_ir_loop);
40         lson.son = son;
41         ARR_APP1(loop_element, loop->children, lson);
42 }
43
44 void add_loop_node(ir_loop *loop, ir_node *n)
45 {
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 }
51
52 void add_loop_irg(ir_loop *loop, ir_graph *irg)
53 {
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 }
59
60 void mature_loops(ir_loop *loop, struct obstack *obst)
61 {
62         size_t i;
63
64         loop_element *new_children = DUP_ARR_D(loop_element, obst, loop->children);
65         DEL_ARR_F(loop->children);
66         loop->children = new_children;
67
68         /* mature child loops */
69         for (i = ARR_LEN(new_children); i > 0;) {
70                 loop_element child = new_children[--i];
71
72                 if (*child.kind == k_ir_loop) {
73                         mature_loops(child.son, obst);
74                 }
75         }
76 }
77
78 ir_loop *(get_loop_outer_loop)(const ir_loop *loop)
79 {
80         return _get_loop_outer_loop(loop);
81 }
82
83 unsigned (get_loop_depth)(const ir_loop *loop)
84 {
85         return _get_loop_depth(loop);
86 }
87
88 size_t get_loop_n_elements(const ir_loop *loop)
89 {
90         assert(loop && loop->kind == k_ir_loop);
91         return(ARR_LEN(loop->children));
92 }
93
94 loop_element get_loop_element(const ir_loop *loop, size_t pos)
95 {
96         assert(loop && loop->kind == k_ir_loop && pos < ARR_LEN(loop->children));
97         return(loop -> children[pos]);
98 }
99
100 void set_irn_loop(ir_node *n, ir_loop *loop)
101 {
102         n->loop = loop;
103 }
104
105 ir_loop *(get_irn_loop)(const ir_node *n)
106 {
107         return _get_irn_loop(n);
108 }
109
110 long get_loop_loop_nr(const ir_loop *loop)
111 {
112         assert(loop && loop->kind == k_ir_loop);
113 #ifdef DEBUG_libfirm
114         return loop->loop_nr;
115 #else
116         return (long)loop;
117 #endif
118 }
119
120 void set_loop_link(ir_loop *loop, void *link)
121 {
122         assert(loop && loop->kind == k_ir_loop);
123         loop->link = link;
124 }
125
126 void *get_loop_link(const ir_loop *loop)
127 {
128         assert(loop && loop->kind == k_ir_loop);
129         return loop->link;
130 }
131
132 int (is_ir_loop)(const void *thing)
133 {
134         return _is_ir_loop(thing);
135 }
136
137 void (set_irg_loop)(ir_graph *irg, ir_loop *loop)
138 {
139         _set_irg_loop(irg, loop);
140 }
141
142 ir_loop *(get_irg_loop)(const ir_graph *irg)
143 {
144         return _get_irg_loop(irg);
145 }
146
147 ir_loop *alloc_loop(ir_loop *father, struct obstack *obst)
148 {
149         ir_loop *son;
150
151         son = OALLOCZ(obst, ir_loop);
152         son->kind     = k_ir_loop;
153         son->children = NEW_ARR_F(loop_element, 0);
154         son->link     = NULL;
155         if (father) {
156                 son->outer_loop = father;
157                 add_loop_son(father, son);
158                 son->depth = father->depth + 1;
159         } else {  /* The root loop */
160                 son->outer_loop = son;
161                 son->depth      = 0;
162         }
163
164 #ifdef DEBUG_libfirm
165         son->loop_nr = get_irp_new_node_nr();
166 #endif
167
168         return son;
169 }