2 * Copyright (C) 1995-2007 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
32 #include "irgraph_t.h"
34 #include "irbackedge_t.h"
36 /*--------------------------------------------------------------------*/
37 /* Backedge information. */
38 /*--------------------------------------------------------------------*/
42 * Returns backarray if the node can have backedges, else returns
45 * Does not assert whether the backarray is correct -- use
48 static INLINE int *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_backedge && "backedge array not allocated!");
62 return n->attr.phi_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 INLINE int *get_backarray(ir_node *n) {
80 int *ba = mere_get_backarray(n);
84 int bal = ARR_LEN(ba); /* avoid makro expansion in assertion. */
85 int inl = ARR_LEN(get_irn_in(n)) -1; /* Use get_irn_in -- sensitive to view! */
86 assert(bal == inl && "backedge array with faulty length");
94 * Returns nin-zero if node has no backarray, or
95 * if size of backarray == size of in array.
97 static INLINE int legal_backarray (ir_node *n) {
98 int *ba = mere_get_backarray(n);
99 if (ba && (ARR_LEN(ba) != ARR_LEN(get_irn_in(n))-1)) /* Use get_irn_in -- sensitive to view! */
105 void fix_backedges(struct obstack *obst, ir_node *n) {
106 int *arr = mere_get_backarray(n);
112 if (ARR_LEN(arr) != ARR_LEN(get_irn_in(n))-1) {
113 arr = new_backedge_arr(obst, ARR_LEN(get_irn_in(n))-1);
115 opc = get_irn_opcode(n);
117 n->attr.phi_backedge = arr;
118 else if (opc == iro_Block) {
119 if (!get_interprocedural_view())
120 n->attr.block.backedge = arr;
122 n->attr.block.cg_backedge = arr;
124 else if (opc == iro_Filter)
125 n->attr.filter.backedge = arr;
128 assert(legal_backarray(n));
130 /* @@@ more efficient in memory consumption, not possible with
131 array implementation.
132 if (ARR_LEN(arr) < ARR_LEN(get_irn_in(n))-1) {
133 ARR_SETLEN(int, arr, ARR_LEN(get_irn_in(n))-1);
137 #ifdef INTERPROCEDURAL_VIEW
138 int is_inter_backedge(ir_node *n, int pos) {
140 int rem = get_interprocedural_view();
141 set_interprocedural_view(0);
142 res = is_backedge(n, pos);
143 set_interprocedural_view(rem);
147 int is_intra_backedge(ir_node *n, int pos) {
149 int rem = get_interprocedural_view();
150 set_interprocedural_view(1);
151 res = is_backedge(n, pos);
152 set_interprocedural_view(rem);
158 /* Returns non-zero if the predecessor pos is a backedge. */
159 int is_backedge (ir_node *n, int pos) {
160 int *ba = get_backarray (n);
161 if (ba) return ba[pos];
165 /* Remarks that edge pos is a backedge. */
166 void set_backedge (ir_node *n, int pos) {
167 int *ba = get_backarray (n);
168 assert(ba && "can only set backedges at Phi, Filter, Block nodes.");
172 /* Remarks that edge pos is a backedge. */
173 void set_not_backedge (ir_node *n, int pos) {
174 int *ba = get_backarray (n);
175 assert(ba && "can only set backedges at Phi, Filter, Block nodes.");
179 /* Returns non-zero if n has backedges. */
180 int has_backedges (ir_node *n) {
182 int *ba = get_backarray (n);
184 int arity = get_irn_arity(n);
185 for (i = 0; i < arity; i++)
191 /** Sets all backedge information to zero. */
192 void clear_backedges (ir_node *n) {
195 #ifdef INTERPROCEDURAL_VIEW
196 int rem = get_interprocedural_view();
197 set_interprocedural_view(0);
199 ba = get_backarray (n);
201 arity = get_irn_arity(n);
202 for (i = 0; i < arity; i++)
205 #ifdef INTERPROCEDURAL_VIEW
206 set_interprocedural_view(1);
207 ba = get_backarray (n);
209 arity = get_irn_arity(n);
210 for (i = 0; i < arity; i++)
213 set_interprocedural_view(rem);
217 int *new_backedge_arr(struct obstack *obst, int size) {
218 int *res = NEW_ARR_D (int, obst, size);
219 memset(res, 0, sizeof(int) * size);
223 /* TODO: add an ir_op operation */
224 void new_backedge_info(ir_node *n) {
225 switch(get_irn_opcode(n)) {
227 n->attr.block.cg_backedge = NULL;
228 n->attr.block.backedge = new_backedge_arr(current_ir_graph->obst, get_irn_arity(n));
231 n->attr.phi_backedge = new_backedge_arr(current_ir_graph->obst, get_irn_arity(n));
234 n->attr.filter.backedge = new_backedge_arr(current_ir_graph->obst, get_irn_arity(n));