2007-03-01 Paul Brook <paul@codesourcery.com>
[official-gcc.git] / gcc / sbitmap.h
blobaacc665f4e70c2103faa257f9c0ed4fad87634ca
1 /* Simple bitmaps.
2 Copyright (C) 1999, 2000, 2002, 2003, 2004 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING. If not, write to the Free
18 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301, USA. */
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 typedef 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. */
45 } *sbitmap;
47 typedef SBITMAP_ELT_TYPE *sbitmap_ptr;
49 /* Return the set size needed for N elements. */
50 #define SBITMAP_SET_SIZE(N) (((N) + SBITMAP_ELT_BITS - 1) / SBITMAP_ELT_BITS)
51 #define SBITMAP_SIZE_BYTES(BITMAP) ((BITMAP)->size * sizeof (SBITMAP_ELT_TYPE))
53 /* Test if bit number bitno in the bitmap is set. */
54 #define TEST_BIT(BITMAP, BITNO) \
55 ((BITMAP)->elms [(BITNO) / SBITMAP_ELT_BITS] >> (BITNO) % SBITMAP_ELT_BITS & 1)
57 /* Set bit number BITNO in the sbitmap MAP. Updates population count
58 if this bitmap has one. */
60 static inline void
61 SET_BIT (sbitmap map, unsigned int bitno)
63 if (map->popcount)
65 bool oldbit;
66 oldbit = TEST_BIT (map, bitno);
67 if (!oldbit)
68 map->popcount[bitno / SBITMAP_ELT_BITS]++;
70 map->elms[bitno / SBITMAP_ELT_BITS]
71 |= (SBITMAP_ELT_TYPE) 1 << (bitno) % SBITMAP_ELT_BITS;
76 /* Reset bit number BITNO in the sbitmap MAP. Updates population
77 count if this bitmap has one. */
79 static inline void
80 RESET_BIT (sbitmap map, unsigned int bitno)
82 if (map->popcount)
84 bool oldbit;
85 oldbit = TEST_BIT (map, bitno);
86 if (oldbit)
87 map->popcount[bitno / SBITMAP_ELT_BITS]--;
89 map->elms[bitno / SBITMAP_ELT_BITS]
90 &= ~((SBITMAP_ELT_TYPE) 1 << (bitno) % SBITMAP_ELT_BITS);
93 /* The iterator for sbitmap. */
94 typedef struct {
95 /* The pointer to the first word of the bitmap. */
96 SBITMAP_ELT_TYPE *ptr;
98 /* The size of the bitmap. */
99 unsigned int size;
101 /* The current word index. */
102 unsigned int word_num;
104 /* The current bit index (not modulo SBITMAP_ELT_BITS). */
105 unsigned int bit_num;
107 /* The words currently visited. */
108 SBITMAP_ELT_TYPE word;
109 } sbitmap_iterator;
111 /* Initialize the iterator I with sbitmap BMP and the initial index
112 MIN. */
114 static inline void
115 sbitmap_iter_init (sbitmap_iterator *i, sbitmap bmp, unsigned int min)
117 i->word_num = min / (unsigned int) SBITMAP_ELT_BITS;
118 i->bit_num = min;
119 i->size = bmp->size;
120 i->ptr = bmp->elms;
122 if (i->word_num >= i->size)
123 i->word = 0;
124 else
125 i->word = (i->ptr[i->word_num]
126 >> (i->bit_num % (unsigned int) SBITMAP_ELT_BITS));
129 /* Return true if we have more bits to visit, in which case *N is set
130 to the index of the bit to be visited. Otherwise, return
131 false. */
133 static inline bool
134 sbitmap_iter_cond (sbitmap_iterator *i, unsigned int *n)
136 /* Skip words that are zeros. */
137 for (; i->word == 0; i->word = i->ptr[i->word_num])
139 i->word_num++;
141 /* If we have reached the end, break. */
142 if (i->word_num >= i->size)
143 return false;
145 i->bit_num = i->word_num * SBITMAP_ELT_BITS;
148 /* Skip bits that are zero. */
149 for (; (i->word & 1) == 0; i->word >>= 1)
150 i->bit_num++;
152 *n = i->bit_num;
154 return true;
157 /* Advance to the next bit. */
159 static inline void
160 sbitmap_iter_next (sbitmap_iterator *i)
162 i->word >>= 1;
163 i->bit_num++;
166 /* Loop over all elements of SBITMAP, starting with MIN. In each
167 iteration, N is set to the index of the bit being visited. ITER is
168 an instance of sbitmap_iterator used to iterate the bitmap. */
170 #define EXECUTE_IF_SET_IN_SBITMAP(SBITMAP, MIN, N, ITER) \
171 for (sbitmap_iter_init (&(ITER), (SBITMAP), (MIN)); \
172 sbitmap_iter_cond (&(ITER), &(N)); \
173 sbitmap_iter_next (&(ITER)))
175 #define EXECUTE_IF_SET_IN_SBITMAP_REV(SBITMAP, N, CODE) \
176 do { \
177 unsigned int word_num_; \
178 unsigned int bit_num_; \
179 unsigned int size_ = (SBITMAP)->size; \
180 SBITMAP_ELT_TYPE *ptr_ = (SBITMAP)->elms; \
182 for (word_num_ = size_; word_num_ > 0; word_num_--) \
184 SBITMAP_ELT_TYPE word_ = ptr_[word_num_ - 1]; \
186 if (word_ != 0) \
187 for (bit_num_ = SBITMAP_ELT_BITS; bit_num_ > 0; bit_num_--) \
189 SBITMAP_ELT_TYPE _mask = (SBITMAP_ELT_TYPE)1 << (bit_num_ - 1);\
191 if ((word_ & _mask) != 0) \
193 word_ &= ~ _mask; \
194 (N) = (word_num_ - 1) * SBITMAP_ELT_BITS + bit_num_ - 1;\
195 CODE; \
196 if (word_ == 0) \
197 break; \
201 } while (0)
203 #define sbitmap_free(MAP) (free((MAP)->popcount), free((MAP)))
204 #define sbitmap_vector_free(VEC) free(VEC)
206 struct int_list;
208 extern void dump_sbitmap (FILE *, sbitmap);
209 extern void dump_sbitmap_file (FILE *, sbitmap);
210 extern void dump_sbitmap_vector (FILE *, const char *, const char *, sbitmap *,
211 int);
212 extern sbitmap sbitmap_alloc (unsigned int);
213 extern sbitmap sbitmap_alloc_with_popcount (unsigned int);
214 extern sbitmap *sbitmap_vector_alloc (unsigned int, unsigned int);
215 extern sbitmap sbitmap_resize (sbitmap, unsigned int, int);
216 extern void sbitmap_copy (sbitmap, sbitmap);
217 extern void sbitmap_copy_n (sbitmap, sbitmap, unsigned int);
218 extern int sbitmap_equal (sbitmap, sbitmap);
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, sbitmap, sbitmap, sbitmap);
225 extern bool sbitmap_union_of_diff_cg (sbitmap, sbitmap, sbitmap, sbitmap);
226 extern void sbitmap_difference (sbitmap, sbitmap, sbitmap);
227 extern void sbitmap_not (sbitmap, sbitmap);
228 extern void sbitmap_a_or_b_and_c (sbitmap, sbitmap, sbitmap, sbitmap);
229 extern bool sbitmap_a_or_b_and_c_cg (sbitmap, sbitmap, sbitmap, sbitmap);
230 extern void sbitmap_a_and_b_or_c (sbitmap, sbitmap, sbitmap, sbitmap);
231 extern bool sbitmap_a_and_b_or_c_cg (sbitmap, sbitmap, sbitmap, sbitmap);
232 extern bool sbitmap_any_common_bits (sbitmap, sbitmap);
233 extern void sbitmap_a_and_b (sbitmap, sbitmap, sbitmap);
234 extern bool sbitmap_a_and_b_cg (sbitmap, sbitmap, sbitmap);
235 extern void sbitmap_a_or_b (sbitmap, sbitmap, sbitmap);
236 extern bool sbitmap_a_or_b_cg (sbitmap, sbitmap, sbitmap);
237 extern void sbitmap_a_xor_b (sbitmap, sbitmap, sbitmap);
238 extern bool sbitmap_a_xor_b_cg (sbitmap, sbitmap, sbitmap);
239 extern bool sbitmap_a_subset_b_p (sbitmap, sbitmap);
241 extern int sbitmap_first_set_bit (sbitmap);
242 extern int sbitmap_last_set_bit (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 (sbitmap);
263 extern sbitmap sbitmap_realloc (sbitmap, unsigned int);
264 extern unsigned long sbitmap_popcount(sbitmap, unsigned long);
265 extern void sbitmap_verify_popcount (sbitmap);
266 #endif /* ! GCC_SBITMAP_H */