Remove duplicate implementation of foreach_set().
[libfirm] / ir / ana / irbackedge.c
1 /*
2  * Copyright (C) 1995-2011 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief     Access function for backedges.
23  * @author    Goetz Lindenmaier
24  * @date      7.2002
25  */
26 #include "config.h"
27
28 #include "irnode_t.h"
29 #include "irgraph_t.h"
30 #include "array.h"
31 #include "irbackedge_t.h"
32 #include "raw_bitset.h"
33
34 /*--------------------------------------------------------------------*/
35 /* Backedge information.                                              */
36 /*--------------------------------------------------------------------*/
37
38
39 /**
40  * Returns backarray if the node can have backedges, else returns
41  * NULL.
42  *
43  * Does not assert whether the backarray is correct -- use
44  * very careful!
45  */
46 static bitset_t *mere_get_backarray(const ir_node *n)
47 {
48         switch (get_irn_opcode(n)) {
49         case iro_Block:
50                 if (!get_Block_matured(n)) return NULL;
51
52                 assert(n->attr.block.backedge && "backedge array not allocated!");
53                 return n->attr.block.backedge;
54         case iro_Phi:
55                 assert(n->attr.phi.u.backedge && "backedge array not allocated!");
56                 return n->attr.phi.u.backedge;
57         default:
58                 break;
59         }
60         return NULL;
61 }
62
63 /**
64  * Returns backarray if the node can have backedges, else returns
65  * NULL.
66  */
67 static bitset_t *get_backarray(const ir_node *n)
68 {
69         bitset_t *ba = mere_get_backarray(n);
70
71 #ifndef NDEBUG
72         if (ba) {
73                 size_t bal = bitset_size(ba);  /* avoid macro expansion in assertion. */
74                 size_t inl = get_irn_arity(n);
75                 assert(bal == inl && "backedge array with faulty length");
76         }
77 #endif
78
79         return ba;
80 }
81
82 #ifndef NDEBUG
83 /**
84  * Returns non-zero if node has no backarray, or
85  *                  if size of backarray == size of in array.
86  */
87 static int legal_backarray(const ir_node *n)
88 {
89         bitset_t *ba = mere_get_backarray(n);
90         if (ba && (bitset_size(ba) != (unsigned) get_irn_arity(n)))
91                 return 0;
92         return 1;
93 }
94 #endif
95
96 void fix_backedges(struct obstack *obst, ir_node *n)
97 {
98         bitset_t *arr = mere_get_backarray(n);
99         unsigned opc;
100         int arity;
101
102         if (! arr)
103                 return;
104
105         arity = get_irn_arity(n);
106         if (bitset_size(arr) != (unsigned) arity) {
107                 arr = new_backedge_arr(obst, arity);
108
109                 opc = get_irn_opcode(n);
110                 if (opc == iro_Phi)
111                         n->attr.phi.u.backedge = arr;
112                 else if (opc == iro_Block) {
113                         n->attr.block.backedge = arr;
114                 }
115         }
116
117         assert(legal_backarray(n));
118 }
119
120 int is_backedge(const ir_node *n, int pos)
121 {
122         bitset_t *ba = get_backarray(n);
123         if (ba)
124                 return bitset_is_set(ba, pos);
125         return 0;
126 }
127
128 void set_backedge(ir_node *n, int pos)
129 {
130         bitset_t *ba = get_backarray(n);
131         assert(ba && "can only set backedges at Phi, Block nodes.");
132         bitset_set(ba, pos);
133 }
134
135 void set_not_backedge(ir_node *n, int pos)
136 {
137         bitset_t *ba = get_backarray(n);
138         assert(ba && "can only set backedges at Phi, Block nodes.");
139         bitset_clear(ba, pos);
140 }
141
142 int has_backedges(const ir_node *n)
143 {
144         bitset_t *ba = get_backarray(n);
145         if (ba != NULL) {
146                 return !bitset_is_empty(ba);
147         }
148         return 0;
149 }
150
151 void clear_backedges(ir_node *n)
152 {
153         bitset_t *ba = get_backarray(n);
154         if (ba != NULL) {
155                 bitset_clear_all(ba);
156         }
157 }
158
159 bitset_t *new_backedge_arr(struct obstack *obst, size_t size)
160 {
161         return bitset_obstack_alloc(obst, size);
162 }