3 * File name: ir/ana/irbackedge.c
4 * Purpose: Access function for backedges.
5 * Author: Goetz Lindenmaier
9 * Copyright: (c) 2002-2003 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
18 #include "irgraph_t.h"
20 #include "irbackedge_t.h"
22 /*--------------------------------------------------------------------*/
23 /* Backedge information. */
24 /*--------------------------------------------------------------------*/
28 * Returns backarray if the node can have backedges, else returns
31 * Does not assert whether the backarray is correct -- use
34 static INLINE int *mere_get_backarray(ir_node *n) {
35 switch (get_irn_opcode(n)) {
37 if (!get_Block_matured(n)) return NULL;
38 if (get_interprocedural_view() && n->attr.block.in_cg) {
39 assert(n->attr.block.cg_backedge && "backedge array not allocated!");
40 return n->attr.block.cg_backedge;
42 assert(n->attr.block.backedge && "backedge array not allocated!");
43 return n->attr.block.backedge;
47 assert(n->attr.phi_backedge && "backedge array not allocated!");
48 return n->attr.phi_backedge;
51 if (get_interprocedural_view()) {
52 assert(n->attr.filter.backedge && "backedge array not allocated!");
53 return n->attr.filter.backedge;
62 * Returns backarray if the node can have backedges, else returns
65 static INLINE int *get_backarray(ir_node *n) {
66 int *ba = mere_get_backarray(n);
70 int bal = ARR_LEN(ba); /* avoid makro expansion in assertion. */
71 int inl = ARR_LEN(get_irn_in(n)) -1; /* Use get_irn_in -- sensitive to view! */
72 assert(bal == inl && "backedge array with faulty length");
80 * Returns nin-zero if node has no backarray, or
81 * if size of backarray == size of in array.
83 static INLINE int legal_backarray (ir_node *n) {
84 int *ba = mere_get_backarray(n);
85 if (ba && (ARR_LEN(ba) != ARR_LEN(get_irn_in(n))-1)) /* Use get_irn_in -- sensitive to view! */
91 void fix_backedges(struct obstack *obst, ir_node *n) {
92 int *arr = mere_get_backarray(n);
98 if (ARR_LEN(arr) != ARR_LEN(get_irn_in(n))-1) {
99 arr = new_backedge_arr(obst, ARR_LEN(get_irn_in(n))-1);
101 opc = get_irn_opcode(n);
103 n->attr.phi_backedge = arr;
104 else if (opc == iro_Block) {
105 if (!get_interprocedural_view())
106 n->attr.block.backedge = arr;
108 n->attr.block.cg_backedge = arr;
110 else if (opc == iro_Filter)
111 n->attr.filter.backedge = arr;
114 assert(legal_backarray(n));
116 /* @@@ more efficient in memory consumption, not possible with
117 array implementation.
118 if (ARR_LEN(arr) < ARR_LEN(get_irn_in(n))-1) {
119 ARR_SETLEN(int, arr, ARR_LEN(get_irn_in(n))-1);
123 int is_inter_backedge(ir_node *n, int pos) {
125 int rem = get_interprocedural_view();
126 set_interprocedural_view(0);
127 res = is_backedge(n, pos);
128 set_interprocedural_view(rem);
132 int is_intra_backedge(ir_node *n, int pos) {
134 int rem = get_interprocedural_view();
135 set_interprocedural_view(1);
136 res = is_backedge(n, pos);
137 set_interprocedural_view(rem);
142 /* Returns non-zero if the predecessor pos is a backedge. */
143 int is_backedge (ir_node *n, int pos) {
144 int *ba = get_backarray (n);
145 if (ba) return ba[pos];
149 /* Remarks that edge pos is a backedge. */
150 void set_backedge (ir_node *n, int pos) {
151 int *ba = get_backarray (n);
152 assert(ba && "can only set backedges at Phi, Filter, Block nodes.");
156 /* Remarks that edge pos is a backedge. */
157 void set_not_backedge (ir_node *n, int pos) {
158 int *ba = get_backarray (n);
159 assert(ba && "can only set backedges at Phi, Filter, Block nodes.");
163 /* Returns non-zero if n has backedges. */
164 int has_backedges (ir_node *n) {
166 int *ba = get_backarray (n);
168 int arity = get_irn_arity(n);
169 for (i = 0; i < arity; i++)
175 /** Sets all backedge information to zero. */
176 void clear_backedges (ir_node *n) {
178 int rem = get_interprocedural_view();
180 set_interprocedural_view(0);
181 ba = get_backarray (n);
183 arity = get_irn_arity(n);
184 for (i = 0; i < arity; i++)
187 set_interprocedural_view(1);
188 ba = get_backarray (n);
190 arity = get_irn_arity(n);
191 for (i = 0; i < arity; i++)
194 set_interprocedural_view(rem);
197 int *new_backedge_arr(struct obstack *obst, int size) {
198 int *res = NEW_ARR_D (int, obst, size);
199 memset(res, 0, sizeof(int) * size);
203 /* TODO: add an ir_op operation */
204 void new_backedge_info(ir_node *n) {
205 switch(get_irn_opcode(n)) {
207 n->attr.block.cg_backedge = NULL;
208 n->attr.block.backedge = new_backedge_arr(current_ir_graph->obst, get_irn_arity(n));
211 n->attr.phi_backedge = new_backedge_arr(current_ir_graph->obst, get_irn_arity(n));
214 n->attr.filter.backedge = new_backedge_arr(current_ir_graph->obst, get_irn_arity(n));