28cd09e2a0f68752e8ef3779cc898be9e8762bc7
[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 (interprocedural_view && n->attr.block.in_cg) {
28       assert(n->attr.block.cg_backedge && "backedge array not allocated!");
29       return n->attr.block.cg_backedge;
30     } else {
31       assert(n->attr.block.backedge && "backedge array not allocated!");
32       return n->attr.block.backedge;
33     }
34     break;
35   case iro_Phi:
36       assert(n->attr.phi_backedge && "backedge array not allocated!");
37     return n->attr.phi_backedge;
38     break;
39   case iro_Filter:
40     if (interprocedural_view) {
41       assert(n->attr.filter.backedge && "backedge array not allocated!");
42       return n->attr.filter.backedge;
43     }
44     break;
45   default: ;
46   }
47   return NULL;
48 }
49
50 /* Returns backarray if the node can have backedges.  Else returns
51    NULL. */
52 static INLINE int *get_backarray(ir_node *n) {
53   int *ba = mere_get_backarray(n);
54
55   if (ba) {
56     int bal = ARR_LEN(ba);  /* avoid makro expansion in assertion. */
57     int inl = ARR_LEN(get_irn_in(n)) -1;  /* Use get_irn_in -- sensitive to view! */
58     assert(bal == inl && "backedge array with faulty length");
59   }
60
61   return ba;
62 }
63
64 /* returns true if node has no backarray, or
65                 if size of backarray == size of in array. */
66 static INLINE bool legal_backarray (ir_node *n) {
67   int *ba = mere_get_backarray(n);
68   if (ba && (ARR_LEN(ba) != ARR_LEN(get_irn_in(n))-1))  /* Use get_irn_in -- sensitive to view! */
69     return false;
70   return true;
71 }
72
73
74 INLINE void fix_backedges(struct obstack *obst, ir_node *n) {
75   opcode opc = get_irn_opcode(n);
76   int *arr = mere_get_backarray(n);
77   if (ARR_LEN(arr) == ARR_LEN(get_irn_in(n))-1)
78     return;
79   if (ARR_LEN(arr) != ARR_LEN(get_irn_in(n))-1) {
80     arr = new_backedge_arr(obst, ARR_LEN(get_irn_in(n))-1);
81     if (opc == iro_Phi)    n->attr.phi_backedge = arr;
82     if ((opc == iro_Block) && !interprocedural_view)
83       n->attr.block.backedge = arr;
84     if ((opc == iro_Block) && interprocedural_view)
85       n->attr.block.cg_backedge = arr;
86     if (opc == iro_Filter) n->attr.filter.backedge = arr;
87     return;
88   }
89   assert(legal_backarray(n));
90   // @@@ more efficient in memory consumption, not possible with
91   // array implementation.
92   //if (ARR_LEN(arr) < ARR_LEN(get_irn_in(n))-1) {
93   //  ARR_SETLEN(int, arr, ARR_LEN(get_irn_in(n))-1);
94   //}
95 }
96
97 /* Returns true if the predesessor pos is a backedge. */
98 bool is_backedge (ir_node *n, int pos) {
99   int *ba = get_backarray (n);
100   if (ba) return ba[pos];
101   return false;
102 }
103
104 /* Remarks that edge pos is a backedge. */
105 void set_backedge (ir_node *n, int pos) {
106   int *ba = get_backarray (n);
107   assert(ba && "can only set backedges at Phi, Filter, Block nodes.");
108   ba[pos] = 1;
109 }
110
111 /* Remarks that edge pos is a backedge. */
112 void set_not_backedge (ir_node *n, int pos) {
113   int *ba = get_backarray (n);
114   assert(ba && "can only set backedges at Phi, Filter, Block nodes.");
115   ba[pos] = 0;
116 }
117
118 /* Returns true if n has backedges. */
119 bool has_backedges (ir_node *n) {
120   int i;
121   int *ba = get_backarray (n);
122   if (ba)
123     for (i = 0; i < get_irn_arity(n); i++)
124       if (ba[i]) return true;
125   return false;
126 }
127
128 /* Sets all backedge information to zero. */
129 void clear_backedges (ir_node *n) {
130   int i, rem = interprocedural_view;
131   int *ba;
132   interprocedural_view = 0;
133   ba = get_backarray (n);
134   if (ba)
135     for (i = 0; i < get_irn_arity(n); i++)
136       ba[i] = 0;
137   interprocedural_view = 1;
138   ba = get_backarray (n);
139   if (ba)
140     for (i = 0; i < get_irn_arity(n); i++)
141       ba[i] = 0;
142   interprocedural_view = rem;
143 }