fixed some Bugs
[libfirm] / ir / ana / irbackedge.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/ana/irbackedge.c
4  * Purpose:     Access function for backedges.
5  * Author:      Goetz Lindenmaier
6  * Modified by:
7  * Created:     7.2002
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 2002-2003 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 #ifdef HAVE_CONFIG_H
14 #include "config.h"
15 #endif
16
17 #include "irnode_t.h"
18 #include "irgraph_t.h"
19 #include "array.h"
20 #include "irbackedge_t.h"
21
22 /*--------------------------------------------------------------------*/
23 /* Backedge information.                                              */
24 /*--------------------------------------------------------------------*/
25
26
27 /**
28  * Returns backarray if the node can have backedges, else returns
29  * NULL.
30  *
31  * Does not assert whether the backarray is correct -- use
32  * very careful!
33  */
34 static INLINE int *mere_get_backarray(ir_node *n) {
35   switch (get_irn_opcode(n)) {
36   case iro_Block:
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;
41     } else {
42       assert(n->attr.block.backedge && "backedge array not allocated!");
43       return n->attr.block.backedge;
44     }
45     break;
46   case iro_Phi:
47       assert(n->attr.phi_backedge && "backedge array not allocated!");
48     return n->attr.phi_backedge;
49     break;
50   case iro_Filter:
51     if (get_interprocedural_view()) {
52       assert(n->attr.filter.backedge && "backedge array not allocated!");
53       return n->attr.filter.backedge;
54     }
55     break;
56   default: ;
57   }
58   return NULL;
59 }
60
61 /**
62  * Returns backarray if the node can have backedges, else returns
63  * NULL.
64  */
65 static INLINE int *get_backarray(ir_node *n) {
66   int *ba = mere_get_backarray(n);
67
68 #ifndef NDEBUG
69   if (ba) {
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");
73   }
74 #endif
75
76   return ba;
77 }
78
79 /**
80  * Returns nin-zero if node has no backarray, or
81  *                  if size of backarray == size of in array.
82  */
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! */
86     return 0;
87   return 1;
88 }
89
90
91 void fix_backedges(struct obstack *obst, ir_node *n) {
92   int *arr = mere_get_backarray(n);
93   ir_opcode opc;
94
95   if (! arr)
96     return;
97
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);
100
101     opc = get_irn_opcode(n);
102     if (opc == iro_Phi)
103       n->attr.phi_backedge = arr;
104     else if (opc == iro_Block) {
105       if (!get_interprocedural_view())
106         n->attr.block.backedge = arr;
107       else
108         n->attr.block.cg_backedge = arr;
109     }
110     else if (opc == iro_Filter)
111       n->attr.filter.backedge = arr;
112   }
113
114   assert(legal_backarray(n));
115
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);
120   }*/
121 }
122
123 int is_inter_backedge(ir_node *n, int pos) {
124   int res;
125   int rem = get_interprocedural_view();
126   set_interprocedural_view(0);
127   res = is_backedge(n, pos);
128   set_interprocedural_view(rem);
129   return res;
130 }
131
132 int is_intra_backedge(ir_node *n, int pos) {
133   int res;
134   int rem = get_interprocedural_view();
135   set_interprocedural_view(1);
136   res = is_backedge(n, pos);
137   set_interprocedural_view(rem);
138   return res;
139 }
140
141
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];
146   return 0;
147 }
148
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.");
153   ba[pos] = 1;
154 }
155
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.");
160   ba[pos] = 0;
161 }
162
163 /* Returns non-zero if n has backedges. */
164 int has_backedges (ir_node *n) {
165   int i;
166   int *ba = get_backarray (n);
167   if (ba) {
168     int arity = get_irn_arity(n);
169     for (i = 0; i < arity; i++)
170       if (ba[i]) return 1;
171   }
172   return 0;
173 }
174
175 /** Sets all backedge information to zero. */
176 void clear_backedges (ir_node *n) {
177   int i, arity;
178   int rem = get_interprocedural_view();
179   int *ba;
180   set_interprocedural_view(0);
181   ba = get_backarray (n);
182   if (ba) {
183     arity = get_irn_arity(n);
184     for (i = 0; i < arity; i++)
185       ba[i] = 0;
186   }
187   set_interprocedural_view(1);
188   ba = get_backarray (n);
189   if (ba) {
190     arity = get_irn_arity(n);
191     for (i = 0; i < arity; i++)
192       ba[i] = 0;
193   }
194   set_interprocedural_view(rem);
195 }
196
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);
200   return res;
201 }
202
203 /* TODO: add an ir_op operation */
204 void new_backedge_info(ir_node *n) {
205   switch(get_irn_opcode(n)) {
206   case iro_Block:
207     n->attr.block.cg_backedge = NULL;
208     n->attr.block.backedge = new_backedge_arr(current_ir_graph->obst, get_irn_arity(n));
209     break;
210   case iro_Phi:
211     n->attr.phi_backedge = new_backedge_arr(current_ir_graph->obst, get_irn_arity(n));
212     break;
213   case iro_Filter:
214     n->attr.filter.backedge = new_backedge_arr(current_ir_graph->obst, get_irn_arity(n));
215     break;
216   default: ;
217   }
218 }