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;
65 #ifdef INTERPROCEDURAL_VIEW
66 if (get_interprocedural_view()) {
67 assert(n->attr.filter.backedge && "backedge array not allocated!");
68 return n->attr.filter.backedge;
79 * Returns backarray if the node can have backedges, else returns
82 static unsigned *get_backarray(ir_node *n)
84 unsigned *ba = mere_get_backarray(n);
88 int bal = rbitset_size(ba); /* avoid macro expansion in assertion. */
89 int inl = get_irn_arity(n);
90 assert(bal == inl && "backedge array with faulty length");
99 * Returns non-zero if node has no backarray, or
100 * if size of backarray == size of in array.
102 static int legal_backarray(ir_node *n)
104 unsigned *ba = mere_get_backarray(n);
105 if (ba && (rbitset_size(ba) != (unsigned) get_irn_arity(n)))
111 void fix_backedges(struct obstack *obst, ir_node *n)
113 unsigned *arr = mere_get_backarray(n);
120 arity = get_irn_arity(n);
121 if (rbitset_size(arr) != (unsigned) arity) {
122 arr = new_backedge_arr(obst, arity);
124 opc = get_irn_opcode(n);
126 n->attr.phi.u.backedge = arr;
127 else if (opc == iro_Block) {
128 #ifdef INTERPROCEDURAL_VIEW
129 if (get_interprocedural_view())
130 n->attr.block.cg_backedge = arr;
133 n->attr.block.backedge = arr;
135 else if (opc == iro_Filter)
136 n->attr.filter.backedge = arr;
139 assert(legal_backarray(n));
142 #ifdef INTERPROCEDURAL_VIEW
143 int is_inter_backedge(ir_node *n, int pos)
146 int rem = get_interprocedural_view();
147 set_interprocedural_view(0);
148 res = is_backedge(n, pos);
149 set_interprocedural_view(rem);
153 int is_intra_backedge(ir_node *n, int pos)
156 int rem = get_interprocedural_view();
157 set_interprocedural_view(1);
158 res = is_backedge(n, pos);
159 set_interprocedural_view(rem);
165 /* Returns non-zero if the predecessor pos is a backedge. */
166 int is_backedge(ir_node *n, int pos)
168 unsigned *ba = get_backarray(n);
170 return rbitset_is_set(ba, pos);
174 /* Remarks that edge pos is a backedge. */
175 void set_backedge(ir_node *n, int pos)
177 unsigned *ba = get_backarray(n);
178 assert(ba && "can only set backedges at Phi, Filter, Block nodes.");
179 rbitset_set(ba, pos);
182 /* Remarks that edge pos is a backedge. */
183 void set_not_backedge(ir_node *n, int pos)
185 unsigned *ba = get_backarray(n);
186 assert(ba && "can only set backedges at Phi, Filter, Block nodes.");
187 rbitset_clear(ba, pos);
190 /* Returns non-zero if n has backedges. */
191 int has_backedges(ir_node *n)
193 unsigned *ba = get_backarray(n);
195 int arity = get_irn_arity(n);
196 return !rbitset_is_empty(ba, arity);
201 /** Sets all backedge information to zero. */
202 void clear_backedges(ir_node *n)
206 #ifdef INTERPROCEDURAL_VIEW
207 int rem = get_interprocedural_view();
208 set_interprocedural_view(0);
210 ba = get_backarray(n);
212 arity = get_irn_arity(n);
213 for (i = 0; i < arity; i++)
214 rbitset_clear(ba, i);
216 #ifdef INTERPROCEDURAL_VIEW
217 set_interprocedural_view(1);
218 ba = get_backarray (n);
220 arity = get_irn_arity(n);
221 for (i = 0; i < arity; i++)
222 rbitset_clear(ba, i);
224 set_interprocedural_view(rem);
228 /* Allocate a new backedge array on the obstack for given size. */
229 unsigned *new_backedge_arr(struct obstack *obst, unsigned size)
231 return rbitset_w_size_obstack_alloc(obst, size);