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)
49 switch (get_irn_opcode(n)) {
51 if (!get_Block_matured(n)) return NULL;
52 #ifdef INTERPROCEDURAL_VIEW
53 if (get_interprocedural_view() && n->attr.block.in_cg) {
54 assert(n->attr.block.cg_backedge && "backedge array not allocated!");
55 return n->attr.block.cg_backedge;
59 assert(n->attr.block.backedge && "backedge array not allocated!");
60 return n->attr.block.backedge;
62 assert(n->attr.phi.u.backedge && "backedge array not allocated!");
63 return n->attr.phi.u.backedge;
66 #ifdef INTERPROCEDURAL_VIEW
67 if (get_interprocedural_view()) {
68 assert(n->attr.filter.backedge && "backedge array not allocated!");
69 return n->attr.filter.backedge;
80 * Returns backarray if the node can have backedges, else returns
83 static unsigned *get_backarray(ir_node *n)
85 unsigned *ba = mere_get_backarray(n);
89 int bal = rbitset_size(ba); /* avoid macro expansion in assertion. */
90 int inl = get_irn_arity(n);
91 assert(bal == inl && "backedge array with faulty length");
100 * Returns non-zero if node has no backarray, or
101 * if size of backarray == size of in array.
103 static int legal_backarray(ir_node *n)
105 unsigned *ba = mere_get_backarray(n);
106 if (ba && (rbitset_size(ba) != (unsigned) get_irn_arity(n)))
112 void fix_backedges(struct obstack *obst, ir_node *n)
114 unsigned *arr = mere_get_backarray(n);
121 arity = get_irn_arity(n);
122 if (rbitset_size(arr) != (unsigned) arity) {
123 arr = new_backedge_arr(obst, arity);
125 opc = get_irn_opcode(n);
127 n->attr.phi.u.backedge = arr;
128 else if (opc == iro_Block) {
129 #ifdef INTERPROCEDURAL_VIEW
130 if (!get_interprocedural_view())
131 n->attr.block.backedge = arr;
134 n->attr.block.cg_backedge = arr;
136 else if (opc == iro_Filter)
137 n->attr.filter.backedge = arr;
140 assert(legal_backarray(n));
143 #ifdef INTERPROCEDURAL_VIEW
144 int is_inter_backedge(ir_node *n, int pos)
147 int rem = get_interprocedural_view();
148 set_interprocedural_view(0);
149 res = is_backedge(n, pos);
150 set_interprocedural_view(rem);
154 int is_intra_backedge(ir_node *n, int pos)
157 int rem = get_interprocedural_view();
158 set_interprocedural_view(1);
159 res = is_backedge(n, pos);
160 set_interprocedural_view(rem);
166 /* Returns non-zero if the predecessor pos is a backedge. */
167 int is_backedge(ir_node *n, int pos)
169 unsigned *ba = get_backarray(n);
171 return rbitset_is_set(ba, pos);
175 /* Remarks that edge pos is a backedge. */
176 void set_backedge(ir_node *n, int pos)
178 unsigned *ba = get_backarray(n);
179 assert(ba && "can only set backedges at Phi, Filter, Block nodes.");
180 rbitset_set(ba, pos);
183 /* Remarks that edge pos is a backedge. */
184 void set_not_backedge(ir_node *n, int pos)
186 unsigned *ba = get_backarray(n);
187 assert(ba && "can only set backedges at Phi, Filter, Block nodes.");
188 rbitset_clear(ba, pos);
191 /* Returns non-zero if n has backedges. */
192 int has_backedges(ir_node *n)
194 unsigned *ba = get_backarray(n);
196 int arity = get_irn_arity(n);
197 return !rbitset_is_empty(ba, arity);
202 /** Sets all backedge information to zero. */
203 void clear_backedges(ir_node *n)
207 #ifdef INTERPROCEDURAL_VIEW
208 int rem = get_interprocedural_view();
209 set_interprocedural_view(0);
211 ba = get_backarray(n);
213 arity = get_irn_arity(n);
214 for (i = 0; i < arity; i++)
215 rbitset_clear(ba, i);
217 #ifdef INTERPROCEDURAL_VIEW
218 set_interprocedural_view(1);
219 ba = get_backarray (n);
221 arity = get_irn_arity(n);
222 for (i = 0; i < arity; i++)
223 rbitset_clear(ba, i);
225 set_interprocedural_view(rem);
229 /* Allocate a new backedge array on the obstack for given size. */
230 unsigned *new_backedge_arr(struct obstack *obst, unsigned size)
232 return rbitset_w_size_obstack_alloc(obst, size);