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
29 #include "irgraph_t.h"
31 #include "irbackedge_t.h"
32 #include "raw_bitset.h"
34 /*--------------------------------------------------------------------*/
35 /* Backedge information. */
36 /*--------------------------------------------------------------------*/
40 * Returns backarray if the node can have backedges, else returns
43 * Does not assert whether the backarray is correct -- use
46 static bitset_t *mere_get_backarray(const ir_node *n)
48 switch (get_irn_opcode(n)) {
50 if (!get_Block_matured(n)) return NULL;
52 assert(n->attr.block.backedge && "backedge array not allocated!");
53 return n->attr.block.backedge;
55 assert(n->attr.phi.u.backedge && "backedge array not allocated!");
56 return n->attr.phi.u.backedge;
64 * Returns backarray if the node can have backedges, else returns
67 static bitset_t *get_backarray(const ir_node *n)
69 bitset_t *ba = mere_get_backarray(n);
73 size_t bal = bitset_size(ba); /* avoid macro expansion in assertion. */
74 size_t inl = get_irn_arity(n);
75 assert(bal == inl && "backedge array with faulty length");
84 * Returns non-zero if node has no backarray, or
85 * if size of backarray == size of in array.
87 static int legal_backarray(const ir_node *n)
89 bitset_t *ba = mere_get_backarray(n);
90 if (ba && (bitset_size(ba) != (unsigned) get_irn_arity(n)))
96 void fix_backedges(struct obstack *obst, ir_node *n)
98 bitset_t *arr = mere_get_backarray(n);
105 arity = get_irn_arity(n);
106 if (bitset_size(arr) != (unsigned) arity) {
107 arr = new_backedge_arr(obst, arity);
109 opc = get_irn_opcode(n);
111 n->attr.phi.u.backedge = arr;
112 else if (opc == iro_Block) {
113 n->attr.block.backedge = arr;
117 assert(legal_backarray(n));
120 /* Returns non-zero if the predecessor pos is a backedge. */
121 int is_backedge(const ir_node *n, int pos)
123 bitset_t *ba = get_backarray(n);
125 return bitset_is_set(ba, pos);
129 /* Remarks that edge pos is a backedge. */
130 void set_backedge(ir_node *n, int pos)
132 bitset_t *ba = get_backarray(n);
133 assert(ba && "can only set backedges at Phi, Block nodes.");
137 /* Remarks that edge pos is a backedge. */
138 void set_not_backedge(ir_node *n, int pos)
140 bitset_t *ba = get_backarray(n);
141 assert(ba && "can only set backedges at Phi, Block nodes.");
142 bitset_clear(ba, pos);
145 /* Returns non-zero if n has backedges. */
146 int has_backedges(const ir_node *n)
148 bitset_t *ba = get_backarray(n);
150 return !bitset_is_empty(ba);
155 /** Sets all backedge information to zero. */
156 void clear_backedges(ir_node *n)
158 bitset_t *ba = get_backarray(n);
160 bitset_clear_all(ba);
164 /* Allocate a new backedge array on the obstack for given size. */
165 bitset_t *new_backedge_arr(struct obstack *obst, size_t size)
167 return bitset_obstack_alloc(obst, size);