[gcc]
[official-gcc.git] / gcc / sbitmap.h
blobbd734f96eddafa1ea4b90247de2f52bd3e413016
1 /* Simple bitmaps.
2 Copyright (C) 1999-2016 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 3, 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 COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #ifndef GCC_SBITMAP_H
21 #define GCC_SBITMAP_H
23 /* Implementation of sets using simple bitmap vectors.
25 This set representation is suitable for non-sparse sets with a known
26 (a priori) universe. The set is represented as a simple array of the
27 host's fastest unsigned integer. For a given member I in the set:
28 - the element for I will be at sbitmap[I / (bits per element)]
29 - the position for I within element is I % (bits per element)
31 This representation is very space-efficient for large non-sparse sets
32 with random access patterns.
34 The following operations can be performed in O(1) time:
36 * set_size : SBITMAP_SIZE
37 * member_p : bitmap_bit_p
38 * add_member : bitmap_set_bit
39 * remove_member : bitmap_clear_bit
41 Most other operations on this set representation are O(U) where U is
42 the size of the set universe:
44 * clear : bitmap_clear
45 * choose_one : bitmap_first_set_bit /
46 bitmap_last_set_bit
47 * forall : EXECUTE_IF_SET_IN_BITMAP
48 * set_copy : bitmap_copy
49 * set_intersection : bitmap_and
50 * set_union : bitmap_ior
51 * set_difference : bitmap_and_compl
52 * set_disjuction : (not implemented)
53 * set_compare : bitmap_equal_p
55 Some operations on 3 sets that occur frequently in data flow problems
56 are also implemented:
58 * A | (B & C) : bitmap_or_and
59 * A | (B & ~C) : bitmap_ior_and_compl
60 * A & (B | C) : bitmap_and_or
62 Most of the set functions have two variants: One that returns non-zero
63 if members were added or removed from the target set, and one that just
64 performs the operation without feedback. The former operations are a
65 bit more expensive but the result can often be used to avoid iterations
66 on other sets.
68 Allocating a bitmap is done with sbitmap_alloc, and resizing is
69 performed with sbitmap_resize.
71 The storage requirements for simple bitmap sets is O(U) where U is the
72 size of the set universe (colloquially the number of bits in the bitmap).
74 This set representation works well for relatively small data flow problems
75 (there are special routines for that, see sbitmap_vector_*). The set
76 operations can be vectorized and there is almost no computating overhead,
77 so that even sparse simple bitmap sets outperform dedicated sparse set
78 representations like linked-list bitmaps. For larger problems, the size
79 overhead of simple bitmap sets gets too high and other set representations
80 have to be used. */
82 #define SBITMAP_ELT_BITS (HOST_BITS_PER_WIDEST_FAST_INT * 1u)
83 #define SBITMAP_ELT_TYPE unsigned HOST_WIDEST_FAST_INT
85 struct simple_bitmap_def
87 unsigned int n_bits; /* Number of bits. */
88 unsigned int size; /* Size in elements. */
89 SBITMAP_ELT_TYPE elms[1]; /* The elements. */
92 /* Return the set size needed for N elements. */
93 #define SBITMAP_SET_SIZE(N) (((N) + SBITMAP_ELT_BITS - 1) / SBITMAP_ELT_BITS)
95 /* Return the number of bits in BITMAP. */
96 #define SBITMAP_SIZE(BITMAP) ((BITMAP)->n_bits)
98 /* Test if bit number bitno in the bitmap is set. */
99 static inline SBITMAP_ELT_TYPE
100 bitmap_bit_p (const_sbitmap map, int bitno)
102 size_t i = bitno / SBITMAP_ELT_BITS;
103 unsigned int s = bitno % SBITMAP_ELT_BITS;
104 return (map->elms[i] >> s) & (SBITMAP_ELT_TYPE) 1;
107 /* Set bit number BITNO in the sbitmap MAP. */
109 static inline void
110 bitmap_set_bit (sbitmap map, int bitno)
112 map->elms[bitno / SBITMAP_ELT_BITS]
113 |= (SBITMAP_ELT_TYPE) 1 << (bitno) % SBITMAP_ELT_BITS;
116 /* Reset bit number BITNO in the sbitmap MAP. */
118 static inline void
119 bitmap_clear_bit (sbitmap map, int bitno)
121 map->elms[bitno / SBITMAP_ELT_BITS]
122 &= ~((SBITMAP_ELT_TYPE) 1 << (bitno) % SBITMAP_ELT_BITS);
125 /* The iterator for sbitmap. */
126 struct sbitmap_iterator {
127 /* The pointer to the first word of the bitmap. */
128 const SBITMAP_ELT_TYPE *ptr;
130 /* The size of the bitmap. */
131 unsigned int size;
133 /* The current word index. */
134 unsigned int word_num;
136 /* The current bit index (not modulo SBITMAP_ELT_BITS). */
137 unsigned int bit_num;
139 /* The words currently visited. */
140 SBITMAP_ELT_TYPE word;
143 /* Initialize the iterator I with sbitmap BMP and the initial index
144 MIN. */
146 static inline void
147 bmp_iter_set_init (sbitmap_iterator *i, const_sbitmap bmp,
148 unsigned int min, unsigned *bit_no ATTRIBUTE_UNUSED)
150 i->word_num = min / (unsigned int) SBITMAP_ELT_BITS;
151 i->bit_num = min;
152 i->size = bmp->size;
153 i->ptr = bmp->elms;
155 if (i->word_num >= i->size)
156 i->word = 0;
157 else
158 i->word = (i->ptr[i->word_num]
159 >> (i->bit_num % (unsigned int) SBITMAP_ELT_BITS));
162 /* Return true if we have more bits to visit, in which case *N is set
163 to the index of the bit to be visited. Otherwise, return
164 false. */
166 static inline bool
167 bmp_iter_set (sbitmap_iterator *i, unsigned int *n)
169 /* Skip words that are zeros. */
170 for (; i->word == 0; i->word = i->ptr[i->word_num])
172 i->word_num++;
174 /* If we have reached the end, break. */
175 if (i->word_num >= i->size)
176 return false;
178 i->bit_num = i->word_num * SBITMAP_ELT_BITS;
181 /* Skip bits that are zero. */
182 for (; (i->word & 1) == 0; i->word >>= 1)
183 i->bit_num++;
185 *n = i->bit_num;
187 return true;
190 /* Advance to the next bit. */
192 static inline void
193 bmp_iter_next (sbitmap_iterator *i, unsigned *bit_no ATTRIBUTE_UNUSED)
195 i->word >>= 1;
196 i->bit_num++;
199 /* Loop over all elements of SBITMAP, starting with MIN. In each
200 iteration, N is set to the index of the bit being visited. ITER is
201 an instance of sbitmap_iterator used to iterate the bitmap. */
203 #ifndef EXECUTE_IF_SET_IN_BITMAP
204 /* See bitmap.h for the other definition of EXECUTE_IF_SET_IN_BITMAP. */
205 #define EXECUTE_IF_SET_IN_BITMAP(BITMAP, MIN, BITNUM, ITER) \
206 for (bmp_iter_set_init (&(ITER), (BITMAP), (MIN), &(BITNUM)); \
207 bmp_iter_set (&(ITER), &(BITNUM)); \
208 bmp_iter_next (&(ITER), &(BITNUM)))
209 #endif
211 inline void sbitmap_free (sbitmap map)
213 free (map);
216 inline void sbitmap_vector_free (sbitmap * vec)
218 free (vec);
221 extern void dump_bitmap (FILE *, const_sbitmap);
222 extern void debug_raw (const simple_bitmap_def &ref);
223 extern void debug_raw (const simple_bitmap_def *ptr);
224 extern void dump_bitmap_file (FILE *, const_sbitmap);
225 extern void debug (const simple_bitmap_def &ref);
226 extern void debug (const simple_bitmap_def *ptr);
227 extern void dump_bitmap_vector (FILE *, const char *, const char *, sbitmap *,
228 int);
229 extern sbitmap sbitmap_alloc (unsigned int);
230 extern sbitmap *sbitmap_vector_alloc (unsigned int, unsigned int);
231 extern sbitmap sbitmap_resize (sbitmap, unsigned int, int);
232 extern void bitmap_copy (sbitmap, const_sbitmap);
233 extern int bitmap_equal_p (const_sbitmap, const_sbitmap);
234 extern bool bitmap_empty_p (const_sbitmap);
235 extern void bitmap_clear (sbitmap);
236 extern void bitmap_ones (sbitmap);
237 extern void bitmap_vector_clear (sbitmap *, unsigned int);
238 extern void bitmap_vector_ones (sbitmap *, unsigned int);
240 extern bool bitmap_ior_and_compl (sbitmap, const_sbitmap,
241 const_sbitmap, const_sbitmap);
242 extern void bitmap_and_compl (sbitmap, const_sbitmap, const_sbitmap);
243 extern void bitmap_not (sbitmap, const_sbitmap);
244 extern bool bitmap_or_and (sbitmap, const_sbitmap,
245 const_sbitmap, const_sbitmap);
246 extern bool bitmap_and_or (sbitmap, const_sbitmap,
247 const_sbitmap, const_sbitmap);
248 extern bool bitmap_intersect_p (const_sbitmap, const_sbitmap);
249 extern bool bitmap_and (sbitmap, const_sbitmap, const_sbitmap);
250 extern bool bitmap_ior (sbitmap, const_sbitmap, const_sbitmap);
251 extern bool bitmap_xor (sbitmap, const_sbitmap, const_sbitmap);
252 extern bool bitmap_subset_p (const_sbitmap, const_sbitmap);
254 extern int bitmap_first_set_bit (const_sbitmap);
255 extern int bitmap_last_set_bit (const_sbitmap);
257 extern void debug_bitmap (const_sbitmap);
258 extern sbitmap sbitmap_realloc (sbitmap, unsigned int);
260 /* a class that ties the lifetime of a sbitmap to its scope. */
261 class auto_sbitmap
263 public:
264 explicit auto_sbitmap (unsigned int size) :
265 m_bitmap (sbitmap_alloc (size)) {}
266 ~auto_sbitmap () { sbitmap_free (m_bitmap); }
268 /* Allow calling sbitmap functions on our bitmap. */
269 operator sbitmap () { return m_bitmap; }
271 private:
272 /* Prevent making a copy that refers to our sbitmap. */
273 auto_sbitmap (const auto_sbitmap &);
274 auto_sbitmap &operator = (const auto_sbitmap &);
275 #if __cplusplus >= 201103L
276 auto_sbitmap (auto_sbitmap &&);
277 auto_sbitmap &operator = (auto_sbitmap &&);
278 #endif
280 /* The bitmap we are managing. */
281 sbitmap m_bitmap;
284 #endif /* ! GCC_SBITMAP_H */