add inline functions
[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 #include "irnode_t.h"
14 #include "irgraph_t.h"
15 #include "array.h"
16 #include "irbackedge_t.h"
17
18 /*--------------------------------------------------------------------*/
19 /* Backedge information.                                              */
20 /*--------------------------------------------------------------------*/
21
22
23 /**
24  * Returns backarray if the node can have backedges, else returns
25  * NULL.
26  *
27  * Does not assert whether the backarray is correct -- use
28  * very careful!
29  */
30 static INLINE int *mere_get_backarray(ir_node *n) {
31   switch (get_irn_opcode(n)) {
32   case iro_Block:
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;
37     } else {
38       assert(n->attr.block.backedge && "backedge array not allocated!");
39       return n->attr.block.backedge;
40     }
41     break;
42   case iro_Phi:
43       assert(n->attr.phi_backedge && "backedge array not allocated!");
44     return n->attr.phi_backedge;
45     break;
46   case iro_Filter:
47     if (get_interprocedural_view()) {
48       assert(n->attr.filter.backedge && "backedge array not allocated!");
49       return n->attr.filter.backedge;
50     }
51     break;
52   default: ;
53   }
54   return NULL;
55 }
56
57 /**
58  * Returns backarray if the node can have backedges, else returns
59  * NULL.
60  */
61 static INLINE int *get_backarray(ir_node *n) {
62   int *ba = mere_get_backarray(n);
63
64   if (ba) {
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");
68   }
69
70   return ba;
71 }
72
73 /**
74  * Returns nin-zero if node has no backarray, or
75  *                  if size of backarray == size of in array.
76  */
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! */
80     return 0;
81   return 1;
82 }
83
84
85 void fix_backedges(struct obstack *obst, ir_node *n) {
86   int *arr = mere_get_backarray(n);
87   opcode opc;
88
89   if (! arr)
90     return;
91
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);
94
95     opc = get_irn_opcode(n);
96     if (opc == iro_Phi)
97       n->attr.phi_backedge = arr;
98     else if (opc == iro_Block) {
99       if (!get_interprocedural_view())
100         n->attr.block.backedge = arr;
101       else
102         n->attr.block.cg_backedge = arr;
103     }
104     else if (opc == iro_Filter)
105       n->attr.filter.backedge = arr;
106   }
107
108   assert(legal_backarray(n));
109
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);
114   }*/
115 }
116
117 int is_inter_backedge(ir_node *n, int pos) {
118   int res;
119   int rem = get_interprocedural_view();
120   set_interprocedural_view(0);
121   res = is_backedge(n, pos);
122   set_interprocedural_view(rem);
123   return res;
124 }
125
126 int is_intra_backedge(ir_node *n, int pos) {
127   int res;
128   int rem = get_interprocedural_view();
129   set_interprocedural_view(1);
130   res = is_backedge(n, pos);
131   set_interprocedural_view(rem);
132   return res;
133 }
134
135
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];
140   return 0;
141 }
142
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.");
147   ba[pos] = 1;
148 }
149
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.");
154   ba[pos] = 0;
155 }
156
157 /* Returns non-zero if n has backedges. */
158 int has_backedges (ir_node *n) {
159   int i;
160   int *ba = get_backarray (n);
161   if (ba) {
162     int arity = get_irn_arity(n);
163     for (i = 0; i < arity; i++)
164       if (ba[i]) return 1;
165   }
166   return 0;
167 }
168
169 /** Sets all backedge information to zero. */
170 void clear_backedges (ir_node *n) {
171   int i, arity;
172   int rem = get_interprocedural_view();
173   int *ba;
174   set_interprocedural_view(0);
175   ba = get_backarray (n);
176   if (ba) {
177     arity = get_irn_arity(n);
178     for (i = 0; i < arity; i++)
179       ba[i] = 0;
180   }
181   set_interprocedural_view(1);
182   ba = get_backarray (n);
183   if (ba) {
184     arity = get_irn_arity(n);
185     for (i = 0; i < arity; i++)
186       ba[i] = 0;
187   }
188   set_interprocedural_view(rem);
189 }
190
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);
194   return res;
195 }
196
197 /* TODO: add an ir_op operation */
198 void new_backedge_info(ir_node *n) {
199   switch(get_irn_opcode(n)) {
200   case iro_Block:
201     n->attr.block.cg_backedge = NULL;
202     n->attr.block.backedge = new_backedge_arr(current_ir_graph->obst, get_irn_arity(n));
203     break;
204   case iro_Phi:
205     n->attr.phi_backedge = new_backedge_arr(current_ir_graph->obst, get_irn_arity(n));
206     break;
207   case iro_Filter:
208     n->attr.filter.backedge = new_backedge_arr(current_ir_graph->obst, get_irn_arity(n));
209     break;
210   default: ;
211   }
212 }