doxygen comment updated
[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   if (ba) {
69     int bal = ARR_LEN(ba);  /* avoid makro expansion in assertion. */
70     int inl = ARR_LEN(get_irn_in(n)) -1;  /* Use get_irn_in -- sensitive to view! */
71     assert(bal == inl && "backedge array with faulty length");
72   }
73
74   return ba;
75 }
76
77 /**
78  * Returns nin-zero if node has no backarray, or
79  *                  if size of backarray == size of in array.
80  */
81 static INLINE int legal_backarray (ir_node *n) {
82   int *ba = mere_get_backarray(n);
83   if (ba && (ARR_LEN(ba) != ARR_LEN(get_irn_in(n))-1))  /* Use get_irn_in -- sensitive to view! */
84     return 0;
85   return 1;
86 }
87
88
89 void fix_backedges(struct obstack *obst, ir_node *n) {
90   int *arr = mere_get_backarray(n);
91   ir_opcode opc;
92
93   if (! arr)
94     return;
95
96   if (ARR_LEN(arr) != ARR_LEN(get_irn_in(n))-1) {
97     arr = new_backedge_arr(obst, ARR_LEN(get_irn_in(n))-1);
98
99     opc = get_irn_opcode(n);
100     if (opc == iro_Phi)
101       n->attr.phi_backedge = arr;
102     else if (opc == iro_Block) {
103       if (!get_interprocedural_view())
104         n->attr.block.backedge = arr;
105       else
106         n->attr.block.cg_backedge = arr;
107     }
108     else if (opc == iro_Filter)
109       n->attr.filter.backedge = arr;
110   }
111
112   assert(legal_backarray(n));
113
114   /* @@@ more efficient in memory consumption, not possible with
115    array implementation.
116   if (ARR_LEN(arr) < ARR_LEN(get_irn_in(n))-1) {
117     ARR_SETLEN(int, arr, ARR_LEN(get_irn_in(n))-1);
118   }*/
119 }
120
121 int is_inter_backedge(ir_node *n, int pos) {
122   int res;
123   int rem = get_interprocedural_view();
124   set_interprocedural_view(0);
125   res = is_backedge(n, pos);
126   set_interprocedural_view(rem);
127   return res;
128 }
129
130 int is_intra_backedge(ir_node *n, int pos) {
131   int res;
132   int rem = get_interprocedural_view();
133   set_interprocedural_view(1);
134   res = is_backedge(n, pos);
135   set_interprocedural_view(rem);
136   return res;
137 }
138
139
140 /* Returns non-zero if the predecessor pos is a backedge. */
141 int is_backedge (ir_node *n, int pos) {
142   int *ba = get_backarray (n);
143   if (ba) return ba[pos];
144   return 0;
145 }
146
147 /* Remarks that edge pos is a backedge. */
148 void set_backedge (ir_node *n, int pos) {
149   int *ba = get_backarray (n);
150   assert(ba && "can only set backedges at Phi, Filter, Block nodes.");
151   ba[pos] = 1;
152 }
153
154 /* Remarks that edge pos is a backedge. */
155 void set_not_backedge (ir_node *n, int pos) {
156   int *ba = get_backarray (n);
157   assert(ba && "can only set backedges at Phi, Filter, Block nodes.");
158   ba[pos] = 0;
159 }
160
161 /* Returns non-zero if n has backedges. */
162 int has_backedges (ir_node *n) {
163   int i;
164   int *ba = get_backarray (n);
165   if (ba) {
166     int arity = get_irn_arity(n);
167     for (i = 0; i < arity; i++)
168       if (ba[i]) return 1;
169   }
170   return 0;
171 }
172
173 /** Sets all backedge information to zero. */
174 void clear_backedges (ir_node *n) {
175   int i, arity;
176   int rem = get_interprocedural_view();
177   int *ba;
178   set_interprocedural_view(0);
179   ba = get_backarray (n);
180   if (ba) {
181     arity = get_irn_arity(n);
182     for (i = 0; i < arity; i++)
183       ba[i] = 0;
184   }
185   set_interprocedural_view(1);
186   ba = get_backarray (n);
187   if (ba) {
188     arity = get_irn_arity(n);
189     for (i = 0; i < arity; i++)
190       ba[i] = 0;
191   }
192   set_interprocedural_view(rem);
193 }
194
195 int *new_backedge_arr(struct obstack *obst, int size) {
196   int *res = NEW_ARR_D (int, obst, size);
197   memset(res, 0, sizeof(int) * size);
198   return res;
199 }
200
201 /* TODO: add an ir_op operation */
202 void new_backedge_info(ir_node *n) {
203   switch(get_irn_opcode(n)) {
204   case iro_Block:
205     n->attr.block.cg_backedge = NULL;
206     n->attr.block.backedge = new_backedge_arr(current_ir_graph->obst, get_irn_arity(n));
207     break;
208   case iro_Phi:
209     n->attr.phi_backedge = new_backedge_arr(current_ir_graph->obst, get_irn_arity(n));
210     break;
211   case iro_Filter:
212     n->attr.filter.backedge = new_backedge_arr(current_ir_graph->obst, get_irn_arity(n));
213     break;
214   default: ;
215   }
216 }