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 Access function for backedges.
23 * @author Goetz Lindenmaier
30 #include "irgraph_t.h"
32 #include "irbackedge_t.h"
33 #include "raw_bitset.h"
35 /*--------------------------------------------------------------------*/
36 /* Backedge information. */
37 /*--------------------------------------------------------------------*/
41 * Returns backarray if the node can have backedges, else returns
44 * Does not assert whether the backarray is correct -- use
47 static unsigned *mere_get_backarray(ir_node *n) {
48 switch (get_irn_opcode(n)) {
50 if (!get_Block_matured(n)) return NULL;
51 if (get_interprocedural_view() && n->attr.block.in_cg) {
52 assert(n->attr.block.cg_backedge && "backedge array not allocated!");
53 return n->attr.block.cg_backedge;
55 assert(n->attr.block.backedge && "backedge array not allocated!");
56 return n->attr.block.backedge;
60 assert(n->attr.phi.u.backedge && "backedge array not allocated!");
61 return n->attr.phi.u.backedge;
64 if (get_interprocedural_view()) {
65 assert(n->attr.filter.backedge && "backedge array not allocated!");
66 return n->attr.filter.backedge;
75 * Returns backarray if the node can have backedges, else returns
78 static unsigned *get_backarray(ir_node *n) {
79 unsigned *ba = mere_get_backarray(n);
83 int bal = rbitset_size(ba); /* avoid macro expansion in assertion. */
84 int inl = get_irn_arity(n);
85 assert(bal == inl && "backedge array with faulty length");
94 * Returns non-zero if node has no backarray, or
95 * if size of backarray == size of in array.
97 static int legal_backarray(ir_node *n) {
98 unsigned *ba = mere_get_backarray(n);
99 if (ba && (rbitset_size(ba) != (unsigned) get_irn_arity(n)))
105 void fix_backedges(struct obstack *obst, ir_node *n) {
106 unsigned *arr = mere_get_backarray(n);
113 arity = get_irn_arity(n);
114 if (rbitset_size(arr) != (unsigned) arity) {
115 arr = new_backedge_arr(obst, arity);
117 opc = get_irn_opcode(n);
119 n->attr.phi.u.backedge = arr;
120 else if (opc == iro_Block) {
121 if (!get_interprocedural_view())
122 n->attr.block.backedge = arr;
124 n->attr.block.cg_backedge = arr;
126 else if (opc == iro_Filter)
127 n->attr.filter.backedge = arr;
130 assert(legal_backarray(n));
133 #ifdef INTERPROCEDURAL_VIEW
134 int is_inter_backedge(ir_node *n, int pos) {
136 int rem = get_interprocedural_view();
137 set_interprocedural_view(0);
138 res = is_backedge(n, pos);
139 set_interprocedural_view(rem);
143 int is_intra_backedge(ir_node *n, int pos) {
145 int rem = get_interprocedural_view();
146 set_interprocedural_view(1);
147 res = is_backedge(n, pos);
148 set_interprocedural_view(rem);
154 /* Returns non-zero if the predecessor pos is a backedge. */
155 int is_backedge(ir_node *n, int pos) {
156 unsigned *ba = get_backarray(n);
158 return rbitset_is_set(ba, pos);
162 /* Remarks that edge pos is a backedge. */
163 void set_backedge(ir_node *n, int pos) {
164 unsigned *ba = get_backarray(n);
165 assert(ba && "can only set backedges at Phi, Filter, Block nodes.");
166 rbitset_set(ba, pos);
169 /* Remarks that edge pos is a backedge. */
170 void set_not_backedge(ir_node *n, int pos) {
171 unsigned *ba = get_backarray(n);
172 assert(ba && "can only set backedges at Phi, Filter, Block nodes.");
173 rbitset_clear(ba, pos);
176 /* Returns non-zero if n has backedges. */
177 int has_backedges(ir_node *n) {
178 unsigned *ba = get_backarray(n);
180 int arity = get_irn_arity(n);
181 return !rbitset_is_empty(ba, arity);
186 /** Sets all backedge information to zero. */
187 void clear_backedges(ir_node *n) {
190 #ifdef INTERPROCEDURAL_VIEW
191 int rem = get_interprocedural_view();
192 set_interprocedural_view(0);
194 ba = get_backarray(n);
196 arity = get_irn_arity(n);
197 for (i = 0; i < arity; i++)
198 rbitset_clear(ba, i);
200 #ifdef INTERPROCEDURAL_VIEW
201 set_interprocedural_view(1);
202 ba = get_backarray (n);
204 arity = get_irn_arity(n);
205 for (i = 0; i < arity; i++)
206 rbitset_clear(ba, i);
208 set_interprocedural_view(rem);
212 /* Allocate a new backedge array on the obstack for given size. */
213 unsigned *new_backedge_arr(struct obstack *obst, unsigned size) {
214 return rbitset_w_size_obstack_alloc(obst, size);
217 /* TODO: add an ir_op operation */
218 void new_backedge_info(ir_node *n) {
219 switch (get_irn_opcode(n)) {
221 n->attr.block.cg_backedge = NULL;
222 n->attr.block.backedge = new_backedge_arr(current_ir_graph->obst, get_irn_arity(n));
225 n->attr.phi.u.backedge = new_backedge_arr(current_ir_graph->obst, get_irn_arity(n));
228 n->attr.filter.backedge = new_backedge_arr(current_ir_graph->obst, get_irn_arity(n));