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 true if node has no backarray, or
75 * if size of backarray == size of in array.
77 static INLINE bool 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 opcode opc = get_irn_opcode(n);
87 int *arr = mere_get_backarray(n);
88 if (ARR_LEN(arr) == ARR_LEN(get_irn_in(n))-1)
90 if (ARR_LEN(arr) != ARR_LEN(get_irn_in(n))-1) {
91 arr = new_backedge_arr(obst, ARR_LEN(get_irn_in(n))-1);
92 if (opc == iro_Phi) n->attr.phi_backedge = arr;
93 if ((opc == iro_Block) && !get_interprocedural_view())
94 n->attr.block.backedge = arr;
95 if ((opc == iro_Block) && get_interprocedural_view())
96 n->attr.block.cg_backedge = arr;
97 if (opc == iro_Filter) n->attr.filter.backedge = arr;
100 assert(legal_backarray(n));
101 /* @@@ more efficient in memory consumption, not possible with
102 array implementation.
103 if (ARR_LEN(arr) < ARR_LEN(get_irn_in(n))-1) {
104 ARR_SETLEN(int, arr, ARR_LEN(get_irn_in(n))-1);
108 int is_inter_backedge(ir_node *n, int pos) {
110 int rem = get_interprocedural_view();
111 set_interprocedural_view(0);
112 res = is_backedge(n, pos);
113 set_interprocedural_view(rem);
117 int is_intra_backedge(ir_node *n, int pos) {
119 int rem = get_interprocedural_view();
120 set_interprocedural_view(1);
121 res = is_backedge(n, pos);
122 set_interprocedural_view(rem);
127 /** Returns true if the predecessor pos is a backedge. */
128 bool is_backedge (ir_node *n, int pos) {
129 int *ba = get_backarray (n);
130 if (ba) return ba[pos];
134 /** Remarks that edge pos is a backedge. */
135 void set_backedge (ir_node *n, int pos) {
136 int *ba = get_backarray (n);
137 assert(ba && "can only set backedges at Phi, Filter, Block nodes.");
141 /** Remarks that edge pos is a backedge. */
142 void set_not_backedge (ir_node *n, int pos) {
143 int *ba = get_backarray (n);
144 assert(ba && "can only set backedges at Phi, Filter, Block nodes.");
148 /** Returns true if n has backedges. */
149 bool has_backedges (ir_node *n) {
151 int *ba = get_backarray (n);
153 int arity = get_irn_arity(n);
154 for (i = 0; i < arity; i++)
155 if (ba[i]) return true;
160 /** Sets all backedge information to zero. */
161 void clear_backedges (ir_node *n) {
163 int rem = get_interprocedural_view();
165 set_interprocedural_view(false);
166 ba = get_backarray (n);
168 arity = get_irn_arity(n);
169 for (i = 0; i < arity; i++)
172 set_interprocedural_view(true);
173 ba = get_backarray (n);
175 arity = get_irn_arity(n);
176 for (i = 0; i < arity; i++)
179 set_interprocedural_view(rem);