converted comments to doxygen
[libfirm] / ir / ana / irbackedge.c
1 /* Copyright (C) 2002 by Universitaet Karlsruhe
2 * All rights reserved.
3 *
4 * Authors:  Goetz Lindenmaier
5 *
6 * irbackedges.c  Access function for backedges.
7 *
8 */
9
10 /* $Id$ */
11
12 #include "irnode_t.h"
13 #include "array.h"
14 #include "irbackedge_t.h"
15
16 /**********************************************************************/
17 /** Backedge information.                                            **/
18 /**********************************************************************/
19
20
21 /* Returns backarray if the node can have backedges.  Else returns
22    NULL. Does not assert whether the backarray is correct -- use
23    very careful! */
24 static INLINE int *mere_get_backarray(ir_node *n) {
25   switch(get_irn_opcode(n)) {
26   case iro_Block:
27     if (!get_Block_matured(n)) return NULL;
28     if (interprocedural_view && n->attr.block.in_cg) {
29       assert(n->attr.block.cg_backedge && "backedge array not allocated!");
30       return n->attr.block.cg_backedge;
31     } else {
32       assert(n->attr.block.backedge && "backedge array not allocated!");
33       return n->attr.block.backedge;
34     }
35     break;
36   case iro_Phi:
37       assert(n->attr.phi_backedge && "backedge array not allocated!");
38     return n->attr.phi_backedge;
39     break;
40   case iro_Filter:
41     if (interprocedural_view) {
42       assert(n->attr.filter.backedge && "backedge array not allocated!");
43       return n->attr.filter.backedge;
44     }
45     break;
46   default: ;
47   }
48   return NULL;
49 }
50
51 /* Returns backarray if the node can have backedges.  Else returns
52    NULL. */
53 static INLINE int *get_backarray(ir_node *n) {
54   int *ba = mere_get_backarray(n);
55
56   if (ba) {
57     int bal = ARR_LEN(ba);  /* avoid makro expansion in assertion. */
58     int inl = ARR_LEN(get_irn_in(n)) -1;  /* Use get_irn_in -- sensitive to view! */
59     assert(bal == inl && "backedge array with faulty length");
60   }
61
62   return ba;
63 }
64
65 /* returns true if node has no backarray, or
66                 if size of backarray == size of in array. */
67 static INLINE bool legal_backarray (ir_node *n) {
68   int *ba = mere_get_backarray(n);
69   if (ba && (ARR_LEN(ba) != ARR_LEN(get_irn_in(n))-1))  /* Use get_irn_in -- sensitive to view! */
70     return false;
71   return true;
72 }
73
74
75 INLINE void fix_backedges(struct obstack *obst, ir_node *n) {
76   opcode opc = get_irn_opcode(n);
77   int *arr = mere_get_backarray(n);
78   if (ARR_LEN(arr) == ARR_LEN(get_irn_in(n))-1)
79     return;
80   if (ARR_LEN(arr) != ARR_LEN(get_irn_in(n))-1) {
81     arr = new_backedge_arr(obst, ARR_LEN(get_irn_in(n))-1);
82     if (opc == iro_Phi)    n->attr.phi_backedge = arr;
83     if ((opc == iro_Block) && !interprocedural_view)
84       n->attr.block.backedge = arr;
85     if ((opc == iro_Block) && interprocedural_view)
86       n->attr.block.cg_backedge = arr;
87     if (opc == iro_Filter) n->attr.filter.backedge = arr;
88     return;
89   }
90   assert(legal_backarray(n));
91   // @@@ more efficient in memory consumption, not possible with
92   // array implementation.
93   //if (ARR_LEN(arr) < ARR_LEN(get_irn_in(n))-1) {
94   //  ARR_SETLEN(int, arr, ARR_LEN(get_irn_in(n))-1);
95   //}
96 }
97
98 /* Returns true if the predesessor pos is a backedge. */
99 bool is_backedge (ir_node *n, int pos) {
100   int *ba = get_backarray (n);
101   if (ba) return ba[pos];
102   return false;
103 }
104
105 /* Remarks that edge pos is a backedge. */
106 void set_backedge (ir_node *n, int pos) {
107   int *ba = get_backarray (n);
108   assert(ba && "can only set backedges at Phi, Filter, Block nodes.");
109   ba[pos] = 1;
110 }
111
112 /* Remarks that edge pos is a backedge. */
113 void set_not_backedge (ir_node *n, int pos) {
114   int *ba = get_backarray (n);
115   assert(ba && "can only set backedges at Phi, Filter, Block nodes.");
116   ba[pos] = 0;
117 }
118
119 /* Returns true if n has backedges. */
120 bool has_backedges (ir_node *n) {
121   int i;
122   int *ba = get_backarray (n);
123   if (ba)
124     for (i = 0; i < get_irn_arity(n); i++)
125       if (ba[i]) return true;
126   return false;
127 }
128
129 /* Sets all backedge information to zero. */
130 void clear_backedges (ir_node *n) {
131   int i, rem = interprocedural_view;
132   int *ba;
133   interprocedural_view = 0;
134   ba = get_backarray (n);
135   if (ba)
136     for (i = 0; i < get_irn_arity(n); i++)
137       ba[i] = 0;
138   interprocedural_view = 1;
139   ba = get_backarray (n);
140   if (ba)
141     for (i = 0; i < get_irn_arity(n); i++)
142       ba[i] = 0;
143   interprocedural_view = rem;
144 }