Updated for libbid move.
[official-gcc.git] / gcc / sbitmap.h
blob86febb4609b7aa59bd4592dff9314ecf98b39a2e
1 /* Simple bitmaps.
2 Copyright (C) 1999, 2000, 2002, 2003, 2004, 2006, 2007
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, USA. */
22 #ifndef GCC_SBITMAP_H
23 #define GCC_SBITMAP_H
25 /* It's not clear yet whether using bitmap.[ch] will be a win.
26 It should be straightforward to convert so for now we keep things simple
27 while more important issues are dealt with. */
29 #define SBITMAP_ELT_BITS ((unsigned) HOST_BITS_PER_WIDEST_FAST_INT)
30 #define SBITMAP_ELT_TYPE unsigned HOST_WIDEST_FAST_INT
32 /* Can't use SBITMAP_ELT_BITS in this macro because it contains a
33 cast. There is no perfect macro in GCC to test against. This
34 suffices for roughly 99% of the hosts we run on, and the rest
35 don't have 256 bit integers. */
36 #if HOST_BITS_PER_WIDEST_FAST_INT > 255
37 #error Need to increase size of datatype used for popcount
38 #endif
40 typedef struct simple_bitmap_def
42 unsigned char *popcount; /* Population count. */
43 unsigned int n_bits; /* Number of bits. */
44 unsigned int size; /* Size in elements. */
45 SBITMAP_ELT_TYPE elms[1]; /* The elements. */
46 } *sbitmap;
48 typedef SBITMAP_ELT_TYPE *sbitmap_ptr;
50 /* Return the set size needed for N elements. */
51 #define SBITMAP_SET_SIZE(N) (((N) + SBITMAP_ELT_BITS - 1) / SBITMAP_ELT_BITS)
52 #define SBITMAP_SIZE_BYTES(BITMAP) ((BITMAP)->size * sizeof (SBITMAP_ELT_TYPE))
54 /* Test if bit number bitno in the bitmap is set. */
55 #define TEST_BIT(BITMAP, BITNO) \
56 ((BITMAP)->elms [(BITNO) / SBITMAP_ELT_BITS] >> (BITNO) % SBITMAP_ELT_BITS & 1)
58 /* Set bit number BITNO in the sbitmap MAP. Updates population count
59 if this bitmap has one. */
61 static inline void
62 SET_BIT (sbitmap map, unsigned int bitno)
64 if (map->popcount)
66 bool oldbit;
67 oldbit = TEST_BIT (map, bitno);
68 if (!oldbit)
69 map->popcount[bitno / SBITMAP_ELT_BITS]++;
71 map->elms[bitno / SBITMAP_ELT_BITS]
72 |= (SBITMAP_ELT_TYPE) 1 << (bitno) % SBITMAP_ELT_BITS;
77 /* Reset bit number BITNO in the sbitmap MAP. Updates population
78 count if this bitmap has one. */
80 static inline void
81 RESET_BIT (sbitmap map, unsigned int bitno)
83 if (map->popcount)
85 bool oldbit;
86 oldbit = TEST_BIT (map, bitno);
87 if (oldbit)
88 map->popcount[bitno / SBITMAP_ELT_BITS]--;
90 map->elms[bitno / SBITMAP_ELT_BITS]
91 &= ~((SBITMAP_ELT_TYPE) 1 << (bitno) % SBITMAP_ELT_BITS);
94 /* The iterator for sbitmap. */
95 typedef struct {
96 /* The pointer to the first word of the bitmap. */
97 SBITMAP_ELT_TYPE *ptr;
99 /* The size of the bitmap. */
100 unsigned int size;
102 /* The current word index. */
103 unsigned int word_num;
105 /* The current bit index (not modulo SBITMAP_ELT_BITS). */
106 unsigned int bit_num;
108 /* The words currently visited. */
109 SBITMAP_ELT_TYPE word;
110 } sbitmap_iterator;
112 /* Initialize the iterator I with sbitmap BMP and the initial index
113 MIN. */
115 static inline void
116 sbitmap_iter_init (sbitmap_iterator *i, sbitmap bmp, unsigned int min)
118 i->word_num = min / (unsigned int) SBITMAP_ELT_BITS;
119 i->bit_num = min;
120 i->size = bmp->size;
121 i->ptr = bmp->elms;
123 if (i->word_num >= i->size)
124 i->word = 0;
125 else
126 i->word = (i->ptr[i->word_num]
127 >> (i->bit_num % (unsigned int) SBITMAP_ELT_BITS));
130 /* Return true if we have more bits to visit, in which case *N is set
131 to the index of the bit to be visited. Otherwise, return
132 false. */
134 static inline bool
135 sbitmap_iter_cond (sbitmap_iterator *i, unsigned int *n)
137 /* Skip words that are zeros. */
138 for (; i->word == 0; i->word = i->ptr[i->word_num])
140 i->word_num++;
142 /* If we have reached the end, break. */
143 if (i->word_num >= i->size)
144 return false;
146 i->bit_num = i->word_num * SBITMAP_ELT_BITS;
149 /* Skip bits that are zero. */
150 for (; (i->word & 1) == 0; i->word >>= 1)
151 i->bit_num++;
153 *n = i->bit_num;
155 return true;
158 /* Advance to the next bit. */
160 static inline void
161 sbitmap_iter_next (sbitmap_iterator *i)
163 i->word >>= 1;
164 i->bit_num++;
167 /* Loop over all elements of SBITMAP, starting with MIN. In each
168 iteration, N is set to the index of the bit being visited. ITER is
169 an instance of sbitmap_iterator used to iterate the bitmap. */
171 #define EXECUTE_IF_SET_IN_SBITMAP(SBITMAP, MIN, N, ITER) \
172 for (sbitmap_iter_init (&(ITER), (SBITMAP), (MIN)); \
173 sbitmap_iter_cond (&(ITER), &(N)); \
174 sbitmap_iter_next (&(ITER)))
176 #define EXECUTE_IF_SET_IN_SBITMAP_REV(SBITMAP, N, CODE) \
177 do { \
178 unsigned int word_num_; \
179 unsigned int bit_num_; \
180 unsigned int size_ = (SBITMAP)->size; \
181 SBITMAP_ELT_TYPE *ptr_ = (SBITMAP)->elms; \
183 for (word_num_ = size_; word_num_ > 0; word_num_--) \
185 SBITMAP_ELT_TYPE word_ = ptr_[word_num_ - 1]; \
187 if (word_ != 0) \
188 for (bit_num_ = SBITMAP_ELT_BITS; bit_num_ > 0; bit_num_--) \
190 SBITMAP_ELT_TYPE _mask = (SBITMAP_ELT_TYPE)1 << (bit_num_ - 1);\
192 if ((word_ & _mask) != 0) \
194 word_ &= ~ _mask; \
195 (N) = (word_num_ - 1) * SBITMAP_ELT_BITS + bit_num_ - 1;\
196 CODE; \
197 if (word_ == 0) \
198 break; \
202 } while (0)
204 #define sbitmap_free(MAP) (free((MAP)->popcount), free((MAP)))
205 #define sbitmap_vector_free(VEC) free(VEC)
207 struct int_list;
209 extern void dump_sbitmap (FILE *, sbitmap);
210 extern void dump_sbitmap_file (FILE *, sbitmap);
211 extern void dump_sbitmap_vector (FILE *, const char *, const char *, sbitmap *,
212 int);
213 extern sbitmap sbitmap_alloc (unsigned int);
214 extern sbitmap sbitmap_alloc_with_popcount (unsigned int);
215 extern sbitmap *sbitmap_vector_alloc (unsigned int, unsigned int);
216 extern sbitmap sbitmap_resize (sbitmap, unsigned int, int);
217 extern void sbitmap_copy (sbitmap, sbitmap);
218 extern void sbitmap_copy_n (sbitmap, sbitmap, unsigned int);
219 extern int sbitmap_equal (sbitmap, sbitmap);
220 extern bool sbitmap_empty_p (sbitmap);
221 extern void sbitmap_zero (sbitmap);
222 extern void sbitmap_ones (sbitmap);
223 extern void sbitmap_vector_zero (sbitmap *, unsigned int);
224 extern void sbitmap_vector_ones (sbitmap *, unsigned int);
226 extern void sbitmap_union_of_diff (sbitmap, sbitmap, sbitmap, sbitmap);
227 extern bool sbitmap_union_of_diff_cg (sbitmap, sbitmap, sbitmap, sbitmap);
228 extern void sbitmap_difference (sbitmap, sbitmap, sbitmap);
229 extern void sbitmap_not (sbitmap, sbitmap);
230 extern void sbitmap_a_or_b_and_c (sbitmap, sbitmap, sbitmap, sbitmap);
231 extern bool sbitmap_a_or_b_and_c_cg (sbitmap, sbitmap, sbitmap, sbitmap);
232 extern void sbitmap_a_and_b_or_c (sbitmap, sbitmap, sbitmap, sbitmap);
233 extern bool sbitmap_a_and_b_or_c_cg (sbitmap, sbitmap, sbitmap, sbitmap);
234 extern bool sbitmap_any_common_bits (sbitmap, sbitmap);
235 extern void sbitmap_a_and_b (sbitmap, sbitmap, sbitmap);
236 extern bool sbitmap_a_and_b_cg (sbitmap, sbitmap, sbitmap);
237 extern void sbitmap_a_or_b (sbitmap, sbitmap, sbitmap);
238 extern bool sbitmap_a_or_b_cg (sbitmap, sbitmap, sbitmap);
239 extern void sbitmap_a_xor_b (sbitmap, sbitmap, sbitmap);
240 extern bool sbitmap_a_xor_b_cg (sbitmap, sbitmap, sbitmap);
241 extern bool sbitmap_a_subset_b_p (sbitmap, sbitmap);
243 extern int sbitmap_first_set_bit (sbitmap);
244 extern int sbitmap_last_set_bit (sbitmap);
246 extern void sbitmap_intersect_of_predsucc (sbitmap, sbitmap *, int,
247 struct int_list **);
248 #define sbitmap_intersect_of_predecessors sbitmap_intersect_of_predsucc
249 #define sbitmap_intersect_of_successors sbitmap_intersect_of_predsucc
251 extern void sbitmap_union_of_predsucc (sbitmap, sbitmap *, int,
252 struct int_list **);
253 #define sbitmap_union_of_predecessors sbitmap_union_of_predsucc
254 #define sbitmap_union_of_successors sbitmap_union_of_predsucc
256 /* Intersection and Union of preds/succs using the new flow graph
257 structure instead of the pred/succ arrays. */
259 extern void sbitmap_intersection_of_succs (sbitmap, sbitmap *, int);
260 extern void sbitmap_intersection_of_preds (sbitmap, sbitmap *, int);
261 extern void sbitmap_union_of_succs (sbitmap, sbitmap *, int);
262 extern void sbitmap_union_of_preds (sbitmap, sbitmap *, int);
264 extern void debug_sbitmap (sbitmap);
265 extern sbitmap sbitmap_realloc (sbitmap, unsigned int);
266 extern unsigned long sbitmap_popcount(sbitmap, unsigned long);
267 extern void sbitmap_verify_popcount (sbitmap);
268 #endif /* ! GCC_SBITMAP_H */