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.
14 #include "irgraph_t.h"
16 #include "irbackedge_t.h"
18 /*--------------------------------------------------------------------*/
19 /* Backedge information. */
20 /*--------------------------------------------------------------------*/
24 * Returns backarray if the node can have backedges, else returns
27 * Does not assert whether the backarray is correct -- use
30 static INLINE int *mere_get_backarray(ir_node *n) {
31 switch (get_irn_opcode(n)) {
33 if (!get_Block_matured(n)) return NULL;
34 if (get_interprocedural_view() && n->attr.block.in_cg) {
35 assert(n->attr.block.cg_backedge && "backedge array not allocated!");
36 return n->attr.block.cg_backedge;
38 assert(n->attr.block.backedge && "backedge array not allocated!");
39 return n->attr.block.backedge;
43 assert(n->attr.phi_backedge && "backedge array not allocated!");
44 return n->attr.phi_backedge;
47 if (get_interprocedural_view()) {
48 assert(n->attr.filter.backedge && "backedge array not allocated!");
49 return n->attr.filter.backedge;
58 * Returns backarray if the node can have backedges, else returns
61 static INLINE int *get_backarray(ir_node *n) {
62 int *ba = mere_get_backarray(n);
65 int bal = ARR_LEN(ba); /* avoid makro expansion in assertion. */
66 int inl = ARR_LEN(get_irn_in(n)) -1; /* Use get_irn_in -- sensitive to view! */
67 assert(bal == inl && "backedge array with faulty length");
74 * Returns nin-zero if node has no backarray, or
75 * if size of backarray == size of in array.
77 static INLINE int legal_backarray (ir_node *n) {
78 int *ba = mere_get_backarray(n);
79 if (ba && (ARR_LEN(ba) != ARR_LEN(get_irn_in(n))-1)) /* Use get_irn_in -- sensitive to view! */
85 void fix_backedges(struct obstack *obst, ir_node *n) {
86 int *arr = mere_get_backarray(n);
92 if (ARR_LEN(arr) != ARR_LEN(get_irn_in(n))-1) {
93 arr = new_backedge_arr(obst, ARR_LEN(get_irn_in(n))-1);
95 opc = get_irn_opcode(n);
97 n->attr.phi_backedge = arr;
98 else if (opc == iro_Block) {
99 if (!get_interprocedural_view())
100 n->attr.block.backedge = arr;
102 n->attr.block.cg_backedge = arr;
104 else if (opc == iro_Filter)
105 n->attr.filter.backedge = arr;
108 assert(legal_backarray(n));
110 /* @@@ more efficient in memory consumption, not possible with
111 array implementation.
112 if (ARR_LEN(arr) < ARR_LEN(get_irn_in(n))-1) {
113 ARR_SETLEN(int, arr, ARR_LEN(get_irn_in(n))-1);
117 int is_inter_backedge(ir_node *n, int pos) {
119 int rem = get_interprocedural_view();
120 set_interprocedural_view(0);
121 res = is_backedge(n, pos);
122 set_interprocedural_view(rem);
126 int is_intra_backedge(ir_node *n, int pos) {
128 int rem = get_interprocedural_view();
129 set_interprocedural_view(1);
130 res = is_backedge(n, pos);
131 set_interprocedural_view(rem);
136 /* Returns non-zero if the predecessor pos is a backedge. */
137 int is_backedge (ir_node *n, int pos) {
138 int *ba = get_backarray (n);
139 if (ba) return ba[pos];
143 /* Remarks that edge pos is a backedge. */
144 void set_backedge (ir_node *n, int pos) {
145 int *ba = get_backarray (n);
146 assert(ba && "can only set backedges at Phi, Filter, Block nodes.");
150 /* Remarks that edge pos is a backedge. */
151 void set_not_backedge (ir_node *n, int pos) {
152 int *ba = get_backarray (n);
153 assert(ba && "can only set backedges at Phi, Filter, Block nodes.");
157 /* Returns non-zero if n has backedges. */
158 int has_backedges (ir_node *n) {
160 int *ba = get_backarray (n);
162 int arity = get_irn_arity(n);
163 for (i = 0; i < arity; i++)
169 /** Sets all backedge information to zero. */
170 void clear_backedges (ir_node *n) {
172 int rem = get_interprocedural_view();
174 set_interprocedural_view(0);
175 ba = get_backarray (n);
177 arity = get_irn_arity(n);
178 for (i = 0; i < arity; i++)
181 set_interprocedural_view(1);
182 ba = get_backarray (n);
184 arity = get_irn_arity(n);
185 for (i = 0; i < arity; i++)
188 set_interprocedural_view(rem);
191 int *new_backedge_arr(struct obstack *obst, int size) {
192 int *res = NEW_ARR_D (int, obst, size);
193 memset(res, 0, sizeof(int) * size);
197 /* TODO: add an ir_op operation */
198 void new_backedge_info(ir_node *n) {
199 switch(get_irn_opcode(n)) {
201 n->attr.block.cg_backedge = NULL;
202 n->attr.block.backedge = new_backedge_arr(current_ir_graph->obst, get_irn_arity(n));
205 n->attr.phi_backedge = new_backedge_arr(current_ir_graph->obst, get_irn_arity(n));
208 n->attr.filter.backedge = new_backedge_arr(current_ir_graph->obst, get_irn_arity(n));