2011-10-07 Tom de Vries <tom@codesourcery.com>
[official-gcc.git] / gcc / sbitmap.h
blobf78e0bf286691aaef94901265d5325482825c08c
1 /* Simple bitmaps.
2 Copyright (C) 1999, 2000, 2002, 2003, 2004, 2006, 2007, 2008, 2010
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 3, 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 COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #ifndef GCC_SBITMAP_H
22 #define GCC_SBITMAP_H
24 /* It's not clear yet whether using bitmap.[ch] will be a win.
25 It should be straightforward to convert so for now we keep things simple
26 while more important issues are dealt with. */
28 #define SBITMAP_ELT_BITS ((unsigned) HOST_BITS_PER_WIDEST_FAST_INT)
29 #define SBITMAP_ELT_TYPE unsigned HOST_WIDEST_FAST_INT
31 /* Can't use SBITMAP_ELT_BITS in this macro because it contains a
32 cast. There is no perfect macro in GCC to test against. This
33 suffices for roughly 99% of the hosts we run on, and the rest
34 don't have 256 bit integers. */
35 #if HOST_BITS_PER_WIDEST_FAST_INT > 255
36 #error Need to increase size of datatype used for popcount
37 #endif
39 struct simple_bitmap_def
41 unsigned char *popcount; /* Population count. */
42 unsigned int n_bits; /* Number of bits. */
43 unsigned int size; /* Size in elements. */
44 SBITMAP_ELT_TYPE elms[1]; /* The elements. */
47 /* Return the set size needed for N elements. */
48 #define SBITMAP_SET_SIZE(N) (((N) + SBITMAP_ELT_BITS - 1) / SBITMAP_ELT_BITS)
49 #define SBITMAP_SIZE_BYTES(BITMAP) ((BITMAP)->size * sizeof (SBITMAP_ELT_TYPE))
51 /* Test if bit number bitno in the bitmap is set. */
52 #define TEST_BIT(BITMAP, BITNO) \
53 ((BITMAP)->elms [(BITNO) / SBITMAP_ELT_BITS] >> (BITNO) % SBITMAP_ELT_BITS & 1)
55 /* Set bit number BITNO in the sbitmap MAP. Updates population count
56 if this bitmap has one. */
58 static inline void
59 SET_BIT (sbitmap map, unsigned int bitno)
61 if (map->popcount)
63 bool oldbit;
64 oldbit = TEST_BIT (map, bitno);
65 if (!oldbit)
66 map->popcount[bitno / SBITMAP_ELT_BITS]++;
68 map->elms[bitno / SBITMAP_ELT_BITS]
69 |= (SBITMAP_ELT_TYPE) 1 << (bitno) % SBITMAP_ELT_BITS;
74 /* Reset bit number BITNO in the sbitmap MAP. Updates population
75 count if this bitmap has one. */
77 static inline void
78 RESET_BIT (sbitmap map, unsigned int bitno)
80 if (map->popcount)
82 bool oldbit;
83 oldbit = TEST_BIT (map, bitno);
84 if (oldbit)
85 map->popcount[bitno / SBITMAP_ELT_BITS]--;
87 map->elms[bitno / SBITMAP_ELT_BITS]
88 &= ~((SBITMAP_ELT_TYPE) 1 << (bitno) % SBITMAP_ELT_BITS);
91 /* The iterator for sbitmap. */
92 typedef struct {
93 /* The pointer to the first word of the bitmap. */
94 const SBITMAP_ELT_TYPE *ptr;
96 /* The size of the bitmap. */
97 unsigned int size;
99 /* The current word index. */
100 unsigned int word_num;
102 /* The current bit index (not modulo SBITMAP_ELT_BITS). */
103 unsigned int bit_num;
105 /* The words currently visited. */
106 SBITMAP_ELT_TYPE word;
107 } sbitmap_iterator;
109 /* Initialize the iterator I with sbitmap BMP and the initial index
110 MIN. */
112 static inline void
113 sbitmap_iter_init (sbitmap_iterator *i, const_sbitmap bmp, unsigned int min)
115 i->word_num = min / (unsigned int) SBITMAP_ELT_BITS;
116 i->bit_num = min;
117 i->size = bmp->size;
118 i->ptr = bmp->elms;
120 if (i->word_num >= i->size)
121 i->word = 0;
122 else
123 i->word = (i->ptr[i->word_num]
124 >> (i->bit_num % (unsigned int) SBITMAP_ELT_BITS));
127 /* Return true if we have more bits to visit, in which case *N is set
128 to the index of the bit to be visited. Otherwise, return
129 false. */
131 static inline bool
132 sbitmap_iter_cond (sbitmap_iterator *i, unsigned int *n)
134 /* Skip words that are zeros. */
135 for (; i->word == 0; i->word = i->ptr[i->word_num])
137 i->word_num++;
139 /* If we have reached the end, break. */
140 if (i->word_num >= i->size)
141 return false;
143 i->bit_num = i->word_num * SBITMAP_ELT_BITS;
146 /* Skip bits that are zero. */
147 for (; (i->word & 1) == 0; i->word >>= 1)
148 i->bit_num++;
150 *n = i->bit_num;
152 return true;
155 /* Advance to the next bit. */
157 static inline void
158 sbitmap_iter_next (sbitmap_iterator *i)
160 i->word >>= 1;
161 i->bit_num++;
164 /* Loop over all elements of SBITMAP, starting with MIN. In each
165 iteration, N is set to the index of the bit being visited. ITER is
166 an instance of sbitmap_iterator used to iterate the bitmap. */
168 #define EXECUTE_IF_SET_IN_SBITMAP(SBITMAP, MIN, N, ITER) \
169 for (sbitmap_iter_init (&(ITER), (SBITMAP), (MIN)); \
170 sbitmap_iter_cond (&(ITER), &(N)); \
171 sbitmap_iter_next (&(ITER)))
173 #define EXECUTE_IF_SET_IN_SBITMAP_REV(SBITMAP, N, CODE) \
174 do { \
175 unsigned int word_num_; \
176 unsigned int bit_num_; \
177 unsigned int size_ = (SBITMAP)->size; \
178 SBITMAP_ELT_TYPE *ptr_ = (SBITMAP)->elms; \
180 for (word_num_ = size_; word_num_ > 0; word_num_--) \
182 SBITMAP_ELT_TYPE word_ = ptr_[word_num_ - 1]; \
184 if (word_ != 0) \
185 for (bit_num_ = SBITMAP_ELT_BITS; bit_num_ > 0; bit_num_--) \
187 SBITMAP_ELT_TYPE _mask = (SBITMAP_ELT_TYPE)1 << (bit_num_ - 1);\
189 if ((word_ & _mask) != 0) \
191 word_ &= ~ _mask; \
192 (N) = (word_num_ - 1) * SBITMAP_ELT_BITS + bit_num_ - 1;\
193 CODE; \
194 if (word_ == 0) \
195 break; \
199 } while (0)
201 #define sbitmap_free(MAP) (free((MAP)->popcount), free((MAP)))
202 #define sbitmap_vector_free(VEC) free(VEC)
204 struct int_list;
206 extern void dump_sbitmap (FILE *, const_sbitmap);
207 extern void dump_sbitmap_file (FILE *, const_sbitmap);
208 extern void dump_sbitmap_vector (FILE *, const char *, const char *, sbitmap *,
209 int);
210 extern sbitmap sbitmap_alloc (unsigned int);
211 extern sbitmap sbitmap_alloc_with_popcount (unsigned int);
212 extern sbitmap *sbitmap_vector_alloc (unsigned int, unsigned int);
213 extern sbitmap sbitmap_resize (sbitmap, unsigned int, int);
214 extern void sbitmap_copy (sbitmap, const_sbitmap);
215 extern void sbitmap_copy_n (sbitmap, const_sbitmap, unsigned int);
216 extern int sbitmap_equal (const_sbitmap, const_sbitmap);
217 extern bool sbitmap_empty_p (const_sbitmap);
218 extern bool sbitmap_range_empty_p (const_sbitmap, unsigned int, unsigned int);
219 extern void sbitmap_zero (sbitmap);
220 extern void sbitmap_ones (sbitmap);
221 extern void sbitmap_vector_zero (sbitmap *, unsigned int);
222 extern void sbitmap_vector_ones (sbitmap *, unsigned int);
224 extern void sbitmap_union_of_diff (sbitmap, const_sbitmap, const_sbitmap, const_sbitmap);
225 extern bool sbitmap_union_of_diff_cg (sbitmap, const_sbitmap, const_sbitmap, const_sbitmap);
226 extern void sbitmap_difference (sbitmap, const_sbitmap, const_sbitmap);
227 extern void sbitmap_not (sbitmap, const_sbitmap);
228 extern void sbitmap_a_or_b_and_c (sbitmap, const_sbitmap, const_sbitmap, const_sbitmap);
229 extern bool sbitmap_a_or_b_and_c_cg (sbitmap, const_sbitmap, const_sbitmap, const_sbitmap);
230 extern void sbitmap_a_and_b_or_c (sbitmap, const_sbitmap, const_sbitmap, const_sbitmap);
231 extern bool sbitmap_a_and_b_or_c_cg (sbitmap, const_sbitmap, const_sbitmap, const_sbitmap);
232 extern bool sbitmap_any_common_bits (const_sbitmap, const_sbitmap);
233 extern void sbitmap_a_and_b (sbitmap, const_sbitmap, const_sbitmap);
234 extern bool sbitmap_a_and_b_cg (sbitmap, const_sbitmap, const_sbitmap);
235 extern void sbitmap_a_or_b (sbitmap, const_sbitmap, const_sbitmap);
236 extern bool sbitmap_a_or_b_cg (sbitmap, const_sbitmap, const_sbitmap);
237 extern void sbitmap_a_xor_b (sbitmap, const_sbitmap, const_sbitmap);
238 extern bool sbitmap_a_xor_b_cg (sbitmap, const_sbitmap, const_sbitmap);
239 extern bool sbitmap_a_subset_b_p (const_sbitmap, const_sbitmap);
241 extern int sbitmap_first_set_bit (const_sbitmap);
242 extern int sbitmap_last_set_bit (const_sbitmap);
244 extern void sbitmap_intersect_of_predsucc (sbitmap, sbitmap *, int,
245 struct int_list **);
246 #define sbitmap_intersect_of_predecessors sbitmap_intersect_of_predsucc
247 #define sbitmap_intersect_of_successors sbitmap_intersect_of_predsucc
249 extern void sbitmap_union_of_predsucc (sbitmap, sbitmap *, int,
250 struct int_list **);
251 #define sbitmap_union_of_predecessors sbitmap_union_of_predsucc
252 #define sbitmap_union_of_successors sbitmap_union_of_predsucc
254 /* Intersection and Union of preds/succs using the new flow graph
255 structure instead of the pred/succ arrays. */
257 extern void sbitmap_intersection_of_succs (sbitmap, sbitmap *, int);
258 extern void sbitmap_intersection_of_preds (sbitmap, sbitmap *, int);
259 extern void sbitmap_union_of_succs (sbitmap, sbitmap *, int);
260 extern void sbitmap_union_of_preds (sbitmap, sbitmap *, int);
262 extern void debug_sbitmap (const_sbitmap);
263 extern sbitmap sbitmap_realloc (sbitmap, unsigned int);
264 extern unsigned long sbitmap_popcount(const_sbitmap, unsigned long);
265 extern void sbitmap_verify_popcount (const_sbitmap);
266 #endif /* ! GCC_SBITMAP_H */