for i in *.[ch]; do sed -e 's/Copyrigth/Copyright/g' -i modeconv.h; done
[libfirm] / ir / ana / irbackedge.c
1 /*
2  * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
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.
10  *
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.
14  *
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
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief     Access function for backedges.
23  * @author    Goetz Lindenmaier
24  * @date      7.2002
25  * @version   $Id$
26  */
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include "irnode_t.h"
32 #include "irgraph_t.h"
33 #include "array.h"
34 #include "irbackedge_t.h"
35
36 /*--------------------------------------------------------------------*/
37 /* Backedge information.                                              */
38 /*--------------------------------------------------------------------*/
39
40
41 /**
42  * Returns backarray if the node can have backedges, else returns
43  * NULL.
44  *
45  * Does not assert whether the backarray is correct -- use
46  * very careful!
47  */
48 static INLINE int *mere_get_backarray(ir_node *n) {
49   switch (get_irn_opcode(n)) {
50   case iro_Block:
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;
55     } else {
56       assert(n->attr.block.backedge && "backedge array not allocated!");
57       return n->attr.block.backedge;
58     }
59     break;
60   case iro_Phi:
61       assert(n->attr.phi_backedge && "backedge array not allocated!");
62     return n->attr.phi_backedge;
63     break;
64   case iro_Filter:
65     if (get_interprocedural_view()) {
66       assert(n->attr.filter.backedge && "backedge array not allocated!");
67       return n->attr.filter.backedge;
68     }
69     break;
70   default: ;
71   }
72   return NULL;
73 }
74
75 /**
76  * Returns backarray if the node can have backedges, else returns
77  * NULL.
78  */
79 static INLINE int *get_backarray(ir_node *n) {
80   int *ba = mere_get_backarray(n);
81
82 #ifndef NDEBUG
83   if (ba) {
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");
87   }
88 #endif
89
90   return ba;
91 }
92
93 /**
94  * Returns nin-zero if node has no backarray, or
95  *                  if size of backarray == size of in array.
96  */
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! */
100     return 0;
101   return 1;
102 }
103
104
105 void fix_backedges(struct obstack *obst, ir_node *n) {
106   int *arr = mere_get_backarray(n);
107   ir_opcode opc;
108
109   if (! arr)
110     return;
111
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);
114
115     opc = get_irn_opcode(n);
116     if (opc == iro_Phi)
117       n->attr.phi_backedge = arr;
118     else if (opc == iro_Block) {
119       if (!get_interprocedural_view())
120         n->attr.block.backedge = arr;
121       else
122         n->attr.block.cg_backedge = arr;
123     }
124     else if (opc == iro_Filter)
125       n->attr.filter.backedge = arr;
126   }
127
128   assert(legal_backarray(n));
129
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);
134   }*/
135 }
136
137 int is_inter_backedge(ir_node *n, int pos) {
138   int res;
139   int rem = get_interprocedural_view();
140   set_interprocedural_view(0);
141   res = is_backedge(n, pos);
142   set_interprocedural_view(rem);
143   return res;
144 }
145
146 int is_intra_backedge(ir_node *n, int pos) {
147   int res;
148   int rem = get_interprocedural_view();
149   set_interprocedural_view(1);
150   res = is_backedge(n, pos);
151   set_interprocedural_view(rem);
152   return res;
153 }
154
155
156 /* Returns non-zero if the predecessor pos is a backedge. */
157 int is_backedge (ir_node *n, int pos) {
158   int *ba = get_backarray (n);
159   if (ba) return ba[pos];
160   return 0;
161 }
162
163 /* Remarks that edge pos is a backedge. */
164 void set_backedge (ir_node *n, int pos) {
165   int *ba = get_backarray (n);
166   assert(ba && "can only set backedges at Phi, Filter, Block nodes.");
167   ba[pos] = 1;
168 }
169
170 /* Remarks that edge pos is a backedge. */
171 void set_not_backedge (ir_node *n, int pos) {
172   int *ba = get_backarray (n);
173   assert(ba && "can only set backedges at Phi, Filter, Block nodes.");
174   ba[pos] = 0;
175 }
176
177 /* Returns non-zero if n has backedges. */
178 int has_backedges (ir_node *n) {
179   int i;
180   int *ba = get_backarray (n);
181   if (ba) {
182     int arity = get_irn_arity(n);
183     for (i = 0; i < arity; i++)
184       if (ba[i]) return 1;
185   }
186   return 0;
187 }
188
189 /** Sets all backedge information to zero. */
190 void clear_backedges (ir_node *n) {
191   int i, arity;
192   int rem = get_interprocedural_view();
193   int *ba;
194   set_interprocedural_view(0);
195   ba = get_backarray (n);
196   if (ba) {
197     arity = get_irn_arity(n);
198     for (i = 0; i < arity; i++)
199       ba[i] = 0;
200   }
201   set_interprocedural_view(1);
202   ba = get_backarray (n);
203   if (ba) {
204     arity = get_irn_arity(n);
205     for (i = 0; i < arity; i++)
206       ba[i] = 0;
207   }
208   set_interprocedural_view(rem);
209 }
210
211 int *new_backedge_arr(struct obstack *obst, int size) {
212   int *res = NEW_ARR_D (int, obst, size);
213   memset(res, 0, sizeof(int) * size);
214   return res;
215 }
216
217 /* TODO: add an ir_op operation */
218 void new_backedge_info(ir_node *n) {
219   switch(get_irn_opcode(n)) {
220   case iro_Block:
221     n->attr.block.cg_backedge = NULL;
222     n->attr.block.backedge = new_backedge_arr(current_ir_graph->obst, get_irn_arity(n));
223     break;
224   case iro_Phi:
225     n->attr.phi_backedge = new_backedge_arr(current_ir_graph->obst, get_irn_arity(n));
226     break;
227   case iro_Filter:
228     n->attr.filter.backedge = new_backedge_arr(current_ir_graph->obst, get_irn_arity(n));
229     break;
230   default: ;
231   }
232 }