2 * Copyright (C) 1995-2011 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 bitset_t *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 bitset_t *get_backarray(ir_node *n)
70 bitset_t *ba = mere_get_backarray(n);
74 size_t bal = bitset_size(ba); /* avoid macro expansion in assertion. */
75 size_t 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 bitset_t *ba = mere_get_backarray(n);
91 if (ba && (bitset_size(ba) != (unsigned) get_irn_arity(n)))
97 void fix_backedges(struct obstack *obst, ir_node *n)
99 bitset_t *arr = mere_get_backarray(n);
106 arity = get_irn_arity(n);
107 if (bitset_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 bitset_t *ba = get_backarray(n);
126 return bitset_is_set(ba, pos);
130 /* Remarks that edge pos is a backedge. */
131 void set_backedge(ir_node *n, int pos)
133 bitset_t *ba = get_backarray(n);
134 assert(ba && "can only set backedges at Phi, Block nodes.");
138 /* Remarks that edge pos is a backedge. */
139 void set_not_backedge(ir_node *n, int pos)
141 bitset_t *ba = get_backarray(n);
142 assert(ba && "can only set backedges at Phi, Block nodes.");
143 bitset_clear(ba, pos);
146 /* Returns non-zero if n has backedges. */
147 int has_backedges(ir_node *n)
149 bitset_t *ba = get_backarray(n);
151 return !bitset_is_empty(ba);
156 /** Sets all backedge information to zero. */
157 void clear_backedges(ir_node *n)
159 bitset_t *ba = get_backarray(n);
161 bitset_clear_all(ba);
165 /* Allocate a new backedge array on the obstack for given size. */
166 bitset_t *new_backedge_arr(struct obstack *obst, size_t size)
168 return bitset_obstack_alloc(obst, size);