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 if (get_interprocedural_view() && n->attr.block.in_cg) {
53 assert(n->attr.block.cg_backedge && "backedge array not allocated!");
54 return n->attr.block.cg_backedge;
56 assert(n->attr.block.backedge && "backedge array not allocated!");
57 return n->attr.block.backedge;
61 assert(n->attr.phi.u.backedge && "backedge array not allocated!");
62 return n->attr.phi.u.backedge;
65 if (get_interprocedural_view()) {
66 assert(n->attr.filter.backedge && "backedge array not allocated!");
67 return n->attr.filter.backedge;
76 * Returns backarray if the node can have backedges, else returns
79 static unsigned *get_backarray(ir_node *n)
81 unsigned *ba = mere_get_backarray(n);
85 int bal = rbitset_size(ba); /* avoid macro expansion in assertion. */
86 int inl = get_irn_arity(n);
87 assert(bal == inl && "backedge array with faulty length");
96 * Returns non-zero if node has no backarray, or
97 * if size of backarray == size of in array.
99 static int legal_backarray(ir_node *n)
101 unsigned *ba = mere_get_backarray(n);
102 if (ba && (rbitset_size(ba) != (unsigned) get_irn_arity(n)))
108 void fix_backedges(struct obstack *obst, ir_node *n)
110 unsigned *arr = mere_get_backarray(n);
117 arity = get_irn_arity(n);
118 if (rbitset_size(arr) != (unsigned) arity) {
119 arr = new_backedge_arr(obst, arity);
121 opc = get_irn_opcode(n);
123 n->attr.phi.u.backedge = arr;
124 else if (opc == iro_Block) {
125 if (!get_interprocedural_view())
126 n->attr.block.backedge = arr;
128 n->attr.block.cg_backedge = arr;
130 else if (opc == iro_Filter)
131 n->attr.filter.backedge = arr;
134 assert(legal_backarray(n));
137 #ifdef INTERPROCEDURAL_VIEW
138 int is_inter_backedge(ir_node *n, int pos)
141 int rem = get_interprocedural_view();
142 set_interprocedural_view(0);
143 res = is_backedge(n, pos);
144 set_interprocedural_view(rem);
148 int is_intra_backedge(ir_node *n, int pos)
151 int rem = get_interprocedural_view();
152 set_interprocedural_view(1);
153 res = is_backedge(n, pos);
154 set_interprocedural_view(rem);
160 /* Returns non-zero if the predecessor pos is a backedge. */
161 int is_backedge(ir_node *n, int pos)
163 unsigned *ba = get_backarray(n);
165 return rbitset_is_set(ba, pos);
169 /* Remarks that edge pos is a backedge. */
170 void set_backedge(ir_node *n, int pos)
172 unsigned *ba = get_backarray(n);
173 assert(ba && "can only set backedges at Phi, Filter, Block nodes.");
174 rbitset_set(ba, pos);
177 /* Remarks that edge pos is a backedge. */
178 void set_not_backedge(ir_node *n, int pos)
180 unsigned *ba = get_backarray(n);
181 assert(ba && "can only set backedges at Phi, Filter, Block nodes.");
182 rbitset_clear(ba, pos);
185 /* Returns non-zero if n has backedges. */
186 int has_backedges(ir_node *n)
188 unsigned *ba = get_backarray(n);
190 int arity = get_irn_arity(n);
191 return !rbitset_is_empty(ba, arity);
196 /** Sets all backedge information to zero. */
197 void clear_backedges(ir_node *n)
201 #ifdef INTERPROCEDURAL_VIEW
202 int rem = get_interprocedural_view();
203 set_interprocedural_view(0);
205 ba = get_backarray(n);
207 arity = get_irn_arity(n);
208 for (i = 0; i < arity; i++)
209 rbitset_clear(ba, i);
211 #ifdef INTERPROCEDURAL_VIEW
212 set_interprocedural_view(1);
213 ba = get_backarray (n);
215 arity = get_irn_arity(n);
216 for (i = 0; i < arity; i++)
217 rbitset_clear(ba, i);
219 set_interprocedural_view(rem);
223 /* Allocate a new backedge array on the obstack for given size. */
224 unsigned *new_backedge_arr(struct obstack *obst, unsigned size)
226 return rbitset_w_size_obstack_alloc(obst, size);
229 /* TODO: add an ir_op operation */
230 void new_backedge_info(ir_node *n)
232 switch (get_irn_opcode(n)) {
234 n->attr.block.cg_backedge = NULL;
235 n->attr.block.backedge = new_backedge_arr(current_ir_graph->obst, get_irn_arity(n));
238 n->attr.phi.u.backedge = new_backedge_arr(current_ir_graph->obst, get_irn_arity(n));
241 n->attr.filter.backedge = new_backedge_arr(current_ir_graph->obst, get_irn_arity(n));