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;
53 assert(n->attr.block.backedge && "backedge array not allocated!");
54 return n->attr.block.backedge;
56 assert(n->attr.phi.u.backedge && "backedge array not allocated!");
57 return n->attr.phi.u.backedge;
65 * Returns backarray if the node can have backedges, else returns
68 static unsigned *get_backarray(ir_node *n)
70 unsigned *ba = mere_get_backarray(n);
74 int bal = rbitset_size(ba); /* avoid macro expansion in assertion. */
75 int inl = get_irn_arity(n);
76 assert(bal == inl && "backedge array with faulty length");
85 * Returns non-zero if node has no backarray, or
86 * if size of backarray == size of in array.
88 static int legal_backarray(ir_node *n)
90 unsigned *ba = mere_get_backarray(n);
91 if (ba && (rbitset_size(ba) != (unsigned) get_irn_arity(n)))
97 void fix_backedges(struct obstack *obst, ir_node *n)
99 unsigned *arr = mere_get_backarray(n);
106 arity = get_irn_arity(n);
107 if (rbitset_size(arr) != (unsigned) arity) {
108 arr = new_backedge_arr(obst, arity);
110 opc = get_irn_opcode(n);
112 n->attr.phi.u.backedge = arr;
113 else if (opc == iro_Block) {
114 n->attr.block.backedge = arr;
118 assert(legal_backarray(n));
121 /* Returns non-zero if the predecessor pos is a backedge. */
122 int is_backedge(ir_node *n, int pos)
124 unsigned *ba = get_backarray(n);
126 return rbitset_is_set(ba, pos);
130 /* Remarks that edge pos is a backedge. */
131 void set_backedge(ir_node *n, int pos)
133 unsigned *ba = get_backarray(n);
134 assert(ba && "can only set backedges at Phi, Block nodes.");
135 rbitset_set(ba, pos);
138 /* Remarks that edge pos is a backedge. */
139 void set_not_backedge(ir_node *n, int pos)
141 unsigned *ba = get_backarray(n);
142 assert(ba && "can only set backedges at Phi, Block nodes.");
143 rbitset_clear(ba, pos);
146 /* Returns non-zero if n has backedges. */
147 int has_backedges(ir_node *n)
149 unsigned *ba = get_backarray(n);
151 int arity = get_irn_arity(n);
152 return !rbitset_is_empty(ba, arity);
157 /** Sets all backedge information to zero. */
158 void clear_backedges(ir_node *n)
162 ba = get_backarray(n);
164 arity = get_irn_arity(n);
165 for (i = 0; i < arity; i++)
166 rbitset_clear(ba, i);
170 /* Allocate a new backedge array on the obstack for given size. */
171 unsigned *new_backedge_arr(struct obstack *obst, unsigned size)
173 return rbitset_w_size_obstack_alloc(obst, size);