1*38fd1498Szrj /* Simple bitmaps.
2*38fd1498Szrj Copyright (C) 1999-2018 Free Software Foundation, Inc.
3*38fd1498Szrj
4*38fd1498Szrj This file is part of GCC.
5*38fd1498Szrj
6*38fd1498Szrj GCC is free software; you can redistribute it and/or modify it under
7*38fd1498Szrj the terms of the GNU General Public License as published by the Free
8*38fd1498Szrj Software Foundation; either version 3, or (at your option) any later
9*38fd1498Szrj version.
10*38fd1498Szrj
11*38fd1498Szrj GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12*38fd1498Szrj WARRANTY; without even the implied warranty of MERCHANTABILITY or
13*38fd1498Szrj FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14*38fd1498Szrj for more details.
15*38fd1498Szrj
16*38fd1498Szrj You should have received a copy of the GNU General Public License
17*38fd1498Szrj along with GCC; see the file COPYING3. If not see
18*38fd1498Szrj <http://www.gnu.org/licenses/>. */
19*38fd1498Szrj
20*38fd1498Szrj #ifndef GCC_SBITMAP_H
21*38fd1498Szrj #define GCC_SBITMAP_H
22*38fd1498Szrj
23*38fd1498Szrj /* Implementation of sets using simple bitmap vectors.
24*38fd1498Szrj
25*38fd1498Szrj This set representation is suitable for non-sparse sets with a known
26*38fd1498Szrj (a priori) universe. The set is represented as a simple array of the
27*38fd1498Szrj host's fastest unsigned integer. For a given member I in the set:
28*38fd1498Szrj - the element for I will be at sbitmap[I / (bits per element)]
29*38fd1498Szrj - the position for I within element is I % (bits per element)
30*38fd1498Szrj
31*38fd1498Szrj This representation is very space-efficient for large non-sparse sets
32*38fd1498Szrj with random access patterns.
33*38fd1498Szrj
34*38fd1498Szrj The following operations can be performed in O(1) time:
35*38fd1498Szrj
36*38fd1498Szrj * set_size : SBITMAP_SIZE
37*38fd1498Szrj * member_p : bitmap_bit_p
38*38fd1498Szrj * add_member : bitmap_set_bit
39*38fd1498Szrj * remove_member : bitmap_clear_bit
40*38fd1498Szrj
41*38fd1498Szrj Most other operations on this set representation are O(U) where U is
42*38fd1498Szrj the size of the set universe:
43*38fd1498Szrj
44*38fd1498Szrj * clear : bitmap_clear
45*38fd1498Szrj * choose_one : bitmap_first_set_bit /
46*38fd1498Szrj bitmap_last_set_bit
47*38fd1498Szrj * forall : EXECUTE_IF_SET_IN_BITMAP
48*38fd1498Szrj * set_copy : bitmap_copy
49*38fd1498Szrj * set_intersection : bitmap_and
50*38fd1498Szrj * set_union : bitmap_ior
51*38fd1498Szrj * set_difference : bitmap_and_compl
52*38fd1498Szrj * set_disjuction : (not implemented)
53*38fd1498Szrj * set_compare : bitmap_equal_p
54*38fd1498Szrj * bit_in_range_p : bitmap_bit_in_range_p
55*38fd1498Szrj
56*38fd1498Szrj Some operations on 3 sets that occur frequently in data flow problems
57*38fd1498Szrj are also implemented:
58*38fd1498Szrj
59*38fd1498Szrj * A | (B & C) : bitmap_or_and
60*38fd1498Szrj * A | (B & ~C) : bitmap_ior_and_compl
61*38fd1498Szrj * A & (B | C) : bitmap_and_or
62*38fd1498Szrj
63*38fd1498Szrj Most of the set functions have two variants: One that returns non-zero
64*38fd1498Szrj if members were added or removed from the target set, and one that just
65*38fd1498Szrj performs the operation without feedback. The former operations are a
66*38fd1498Szrj bit more expensive but the result can often be used to avoid iterations
67*38fd1498Szrj on other sets.
68*38fd1498Szrj
69*38fd1498Szrj Allocating a bitmap is done with sbitmap_alloc, and resizing is
70*38fd1498Szrj performed with sbitmap_resize.
71*38fd1498Szrj
72*38fd1498Szrj The storage requirements for simple bitmap sets is O(U) where U is the
73*38fd1498Szrj size of the set universe (colloquially the number of bits in the bitmap).
74*38fd1498Szrj
75*38fd1498Szrj This set representation works well for relatively small data flow problems
76*38fd1498Szrj (there are special routines for that, see sbitmap_vector_*). The set
77*38fd1498Szrj operations can be vectorized and there is almost no computating overhead,
78*38fd1498Szrj so that even sparse simple bitmap sets outperform dedicated sparse set
79*38fd1498Szrj representations like linked-list bitmaps. For larger problems, the size
80*38fd1498Szrj overhead of simple bitmap sets gets too high and other set representations
81*38fd1498Szrj have to be used. */
82*38fd1498Szrj
83*38fd1498Szrj #define SBITMAP_ELT_BITS (HOST_BITS_PER_WIDEST_FAST_INT * 1u)
84*38fd1498Szrj #define SBITMAP_ELT_TYPE unsigned HOST_WIDEST_FAST_INT
85*38fd1498Szrj
86*38fd1498Szrj struct simple_bitmap_def
87*38fd1498Szrj {
88*38fd1498Szrj unsigned int n_bits; /* Number of bits. */
89*38fd1498Szrj unsigned int size; /* Size in elements. */
90*38fd1498Szrj SBITMAP_ELT_TYPE elms[1]; /* The elements. */
91*38fd1498Szrj };
92*38fd1498Szrj
93*38fd1498Szrj /* Return the set size needed for N elements. */
94*38fd1498Szrj #define SBITMAP_SET_SIZE(N) (((N) + SBITMAP_ELT_BITS - 1) / SBITMAP_ELT_BITS)
95*38fd1498Szrj
96*38fd1498Szrj /* Return the number of bits in BITMAP. */
97*38fd1498Szrj #define SBITMAP_SIZE(BITMAP) ((BITMAP)->n_bits)
98*38fd1498Szrj
99*38fd1498Szrj /* Verify that access at INDEX in bitmap MAP is valid. */
100*38fd1498Szrj
101*38fd1498Szrj static inline void
bitmap_check_index(const_sbitmap map,int index)102*38fd1498Szrj bitmap_check_index (const_sbitmap map, int index)
103*38fd1498Szrj {
104*38fd1498Szrj gcc_checking_assert (index >= 0);
105*38fd1498Szrj gcc_checking_assert ((unsigned int)index < map->n_bits);
106*38fd1498Szrj }
107*38fd1498Szrj
108*38fd1498Szrj /* Verify that bitmaps A and B have same size. */
109*38fd1498Szrj
110*38fd1498Szrj static inline void
bitmap_check_sizes(const_sbitmap a,const_sbitmap b)111*38fd1498Szrj bitmap_check_sizes (const_sbitmap a, const_sbitmap b)
112*38fd1498Szrj {
113*38fd1498Szrj gcc_checking_assert (a->n_bits == b->n_bits);
114*38fd1498Szrj }
115*38fd1498Szrj
116*38fd1498Szrj /* Test if bit number bitno in the bitmap is set. */
117*38fd1498Szrj static inline SBITMAP_ELT_TYPE
bitmap_bit_p(const_sbitmap map,int bitno)118*38fd1498Szrj bitmap_bit_p (const_sbitmap map, int bitno)
119*38fd1498Szrj {
120*38fd1498Szrj bitmap_check_index (map, bitno);
121*38fd1498Szrj
122*38fd1498Szrj size_t i = bitno / SBITMAP_ELT_BITS;
123*38fd1498Szrj unsigned int s = bitno % SBITMAP_ELT_BITS;
124*38fd1498Szrj return (map->elms[i] >> s) & (SBITMAP_ELT_TYPE) 1;
125*38fd1498Szrj }
126*38fd1498Szrj
127*38fd1498Szrj /* Set bit number BITNO in the sbitmap MAP. */
128*38fd1498Szrj
129*38fd1498Szrj static inline void
bitmap_set_bit(sbitmap map,int bitno)130*38fd1498Szrj bitmap_set_bit (sbitmap map, int bitno)
131*38fd1498Szrj {
132*38fd1498Szrj bitmap_check_index (map, bitno);
133*38fd1498Szrj
134*38fd1498Szrj map->elms[bitno / SBITMAP_ELT_BITS]
135*38fd1498Szrj |= (SBITMAP_ELT_TYPE) 1 << (bitno) % SBITMAP_ELT_BITS;
136*38fd1498Szrj }
137*38fd1498Szrj
138*38fd1498Szrj /* Reset bit number BITNO in the sbitmap MAP. */
139*38fd1498Szrj
140*38fd1498Szrj static inline void
bitmap_clear_bit(sbitmap map,int bitno)141*38fd1498Szrj bitmap_clear_bit (sbitmap map, int bitno)
142*38fd1498Szrj {
143*38fd1498Szrj bitmap_check_index (map, bitno);
144*38fd1498Szrj
145*38fd1498Szrj map->elms[bitno / SBITMAP_ELT_BITS]
146*38fd1498Szrj &= ~((SBITMAP_ELT_TYPE) 1 << (bitno) % SBITMAP_ELT_BITS);
147*38fd1498Szrj }
148*38fd1498Szrj
149*38fd1498Szrj /* The iterator for sbitmap. */
150*38fd1498Szrj struct sbitmap_iterator {
151*38fd1498Szrj /* The pointer to the first word of the bitmap. */
152*38fd1498Szrj const SBITMAP_ELT_TYPE *ptr;
153*38fd1498Szrj
154*38fd1498Szrj /* The size of the bitmap. */
155*38fd1498Szrj unsigned int size;
156*38fd1498Szrj
157*38fd1498Szrj /* The current word index. */
158*38fd1498Szrj unsigned int word_num;
159*38fd1498Szrj
160*38fd1498Szrj /* The current bit index (not modulo SBITMAP_ELT_BITS). */
161*38fd1498Szrj unsigned int bit_num;
162*38fd1498Szrj
163*38fd1498Szrj /* The words currently visited. */
164*38fd1498Szrj SBITMAP_ELT_TYPE word;
165*38fd1498Szrj };
166*38fd1498Szrj
167*38fd1498Szrj /* Initialize the iterator I with sbitmap BMP and the initial index
168*38fd1498Szrj MIN. */
169*38fd1498Szrj
170*38fd1498Szrj static inline void
bmp_iter_set_init(sbitmap_iterator * i,const_sbitmap bmp,unsigned int min,unsigned * bit_no ATTRIBUTE_UNUSED)171*38fd1498Szrj bmp_iter_set_init (sbitmap_iterator *i, const_sbitmap bmp,
172*38fd1498Szrj unsigned int min, unsigned *bit_no ATTRIBUTE_UNUSED)
173*38fd1498Szrj {
174*38fd1498Szrj i->word_num = min / (unsigned int) SBITMAP_ELT_BITS;
175*38fd1498Szrj i->bit_num = min;
176*38fd1498Szrj i->size = bmp->size;
177*38fd1498Szrj i->ptr = bmp->elms;
178*38fd1498Szrj
179*38fd1498Szrj if (i->word_num >= i->size)
180*38fd1498Szrj i->word = 0;
181*38fd1498Szrj else
182*38fd1498Szrj i->word = (i->ptr[i->word_num]
183*38fd1498Szrj >> (i->bit_num % (unsigned int) SBITMAP_ELT_BITS));
184*38fd1498Szrj }
185*38fd1498Szrj
186*38fd1498Szrj /* Return true if we have more bits to visit, in which case *N is set
187*38fd1498Szrj to the index of the bit to be visited. Otherwise, return
188*38fd1498Szrj false. */
189*38fd1498Szrj
190*38fd1498Szrj static inline bool
bmp_iter_set(sbitmap_iterator * i,unsigned int * n)191*38fd1498Szrj bmp_iter_set (sbitmap_iterator *i, unsigned int *n)
192*38fd1498Szrj {
193*38fd1498Szrj /* Skip words that are zeros. */
194*38fd1498Szrj for (; i->word == 0; i->word = i->ptr[i->word_num])
195*38fd1498Szrj {
196*38fd1498Szrj i->word_num++;
197*38fd1498Szrj
198*38fd1498Szrj /* If we have reached the end, break. */
199*38fd1498Szrj if (i->word_num >= i->size)
200*38fd1498Szrj return false;
201*38fd1498Szrj
202*38fd1498Szrj i->bit_num = i->word_num * SBITMAP_ELT_BITS;
203*38fd1498Szrj }
204*38fd1498Szrj
205*38fd1498Szrj /* Skip bits that are zero. */
206*38fd1498Szrj for (; (i->word & 1) == 0; i->word >>= 1)
207*38fd1498Szrj i->bit_num++;
208*38fd1498Szrj
209*38fd1498Szrj *n = i->bit_num;
210*38fd1498Szrj
211*38fd1498Szrj return true;
212*38fd1498Szrj }
213*38fd1498Szrj
214*38fd1498Szrj /* Advance to the next bit. */
215*38fd1498Szrj
216*38fd1498Szrj static inline void
bmp_iter_next(sbitmap_iterator * i,unsigned * bit_no ATTRIBUTE_UNUSED)217*38fd1498Szrj bmp_iter_next (sbitmap_iterator *i, unsigned *bit_no ATTRIBUTE_UNUSED)
218*38fd1498Szrj {
219*38fd1498Szrj i->word >>= 1;
220*38fd1498Szrj i->bit_num++;
221*38fd1498Szrj }
222*38fd1498Szrj
223*38fd1498Szrj /* Loop over all elements of SBITMAP, starting with MIN. In each
224*38fd1498Szrj iteration, N is set to the index of the bit being visited. ITER is
225*38fd1498Szrj an instance of sbitmap_iterator used to iterate the bitmap. */
226*38fd1498Szrj
227*38fd1498Szrj #ifndef EXECUTE_IF_SET_IN_BITMAP
228*38fd1498Szrj /* See bitmap.h for the other definition of EXECUTE_IF_SET_IN_BITMAP. */
229*38fd1498Szrj #define EXECUTE_IF_SET_IN_BITMAP(BITMAP, MIN, BITNUM, ITER) \
230*38fd1498Szrj for (bmp_iter_set_init (&(ITER), (BITMAP), (MIN), &(BITNUM)); \
231*38fd1498Szrj bmp_iter_set (&(ITER), &(BITNUM)); \
232*38fd1498Szrj bmp_iter_next (&(ITER), &(BITNUM)))
233*38fd1498Szrj #endif
234*38fd1498Szrj
sbitmap_free(sbitmap map)235*38fd1498Szrj inline void sbitmap_free (sbitmap map)
236*38fd1498Szrj {
237*38fd1498Szrj free (map);
238*38fd1498Szrj }
239*38fd1498Szrj
sbitmap_vector_free(sbitmap * vec)240*38fd1498Szrj inline void sbitmap_vector_free (sbitmap * vec)
241*38fd1498Szrj {
242*38fd1498Szrj free (vec);
243*38fd1498Szrj }
244*38fd1498Szrj
245*38fd1498Szrj extern void dump_bitmap (FILE *, const_sbitmap);
246*38fd1498Szrj extern void debug_raw (const simple_bitmap_def &ref);
247*38fd1498Szrj extern void debug_raw (const simple_bitmap_def *ptr);
248*38fd1498Szrj extern void dump_bitmap_file (FILE *, const_sbitmap);
249*38fd1498Szrj extern void debug (const simple_bitmap_def &ref);
250*38fd1498Szrj extern void debug (const simple_bitmap_def *ptr);
251*38fd1498Szrj extern void dump_bitmap_vector (FILE *, const char *, const char *, sbitmap *,
252*38fd1498Szrj int);
253*38fd1498Szrj extern sbitmap sbitmap_alloc (unsigned int);
254*38fd1498Szrj extern sbitmap *sbitmap_vector_alloc (unsigned int, unsigned int);
255*38fd1498Szrj extern sbitmap sbitmap_resize (sbitmap, unsigned int, int);
256*38fd1498Szrj extern void bitmap_copy (sbitmap, const_sbitmap);
257*38fd1498Szrj extern int bitmap_equal_p (const_sbitmap, const_sbitmap);
258*38fd1498Szrj extern unsigned int bitmap_count_bits (const_sbitmap);
259*38fd1498Szrj extern bool bitmap_empty_p (const_sbitmap);
260*38fd1498Szrj extern void bitmap_clear (sbitmap);
261*38fd1498Szrj extern void bitmap_clear_range (sbitmap, unsigned, unsigned);
262*38fd1498Szrj extern void bitmap_set_range (sbitmap, unsigned, unsigned);
263*38fd1498Szrj extern void bitmap_ones (sbitmap);
264*38fd1498Szrj extern void bitmap_vector_clear (sbitmap *, unsigned int);
265*38fd1498Szrj extern void bitmap_vector_ones (sbitmap *, unsigned int);
266*38fd1498Szrj
267*38fd1498Szrj extern bool bitmap_ior_and_compl (sbitmap, const_sbitmap,
268*38fd1498Szrj const_sbitmap, const_sbitmap);
269*38fd1498Szrj extern void bitmap_and_compl (sbitmap, const_sbitmap, const_sbitmap);
270*38fd1498Szrj extern void bitmap_not (sbitmap, const_sbitmap);
271*38fd1498Szrj extern bool bitmap_or_and (sbitmap, const_sbitmap,
272*38fd1498Szrj const_sbitmap, const_sbitmap);
273*38fd1498Szrj extern bool bitmap_and_or (sbitmap, const_sbitmap,
274*38fd1498Szrj const_sbitmap, const_sbitmap);
275*38fd1498Szrj extern bool bitmap_intersect_p (const_sbitmap, const_sbitmap);
276*38fd1498Szrj extern bool bitmap_and (sbitmap, const_sbitmap, const_sbitmap);
277*38fd1498Szrj extern bool bitmap_ior (sbitmap, const_sbitmap, const_sbitmap);
278*38fd1498Szrj extern bool bitmap_xor (sbitmap, const_sbitmap, const_sbitmap);
279*38fd1498Szrj extern bool bitmap_subset_p (const_sbitmap, const_sbitmap);
280*38fd1498Szrj extern bool bitmap_bit_in_range_p (const_sbitmap, unsigned int, unsigned int);
281*38fd1498Szrj
282*38fd1498Szrj extern int bitmap_first_set_bit (const_sbitmap);
283*38fd1498Szrj extern int bitmap_last_set_bit (const_sbitmap);
284*38fd1498Szrj
285*38fd1498Szrj extern void debug_bitmap (const_sbitmap);
286*38fd1498Szrj extern sbitmap sbitmap_realloc (sbitmap, unsigned int);
287*38fd1498Szrj
288*38fd1498Szrj /* a class that ties the lifetime of a sbitmap to its scope. */
289*38fd1498Szrj class auto_sbitmap
290*38fd1498Szrj {
291*38fd1498Szrj public:
auto_sbitmap(unsigned int size)292*38fd1498Szrj explicit auto_sbitmap (unsigned int size) :
293*38fd1498Szrj m_bitmap (sbitmap_alloc (size)) {}
~auto_sbitmap()294*38fd1498Szrj ~auto_sbitmap () { sbitmap_free (m_bitmap); }
295*38fd1498Szrj
296*38fd1498Szrj /* Allow calling sbitmap functions on our bitmap. */
sbitmap()297*38fd1498Szrj operator sbitmap () { return m_bitmap; }
298*38fd1498Szrj
299*38fd1498Szrj private:
300*38fd1498Szrj /* Prevent making a copy that refers to our sbitmap. */
301*38fd1498Szrj auto_sbitmap (const auto_sbitmap &);
302*38fd1498Szrj auto_sbitmap &operator = (const auto_sbitmap &);
303*38fd1498Szrj #if __cplusplus >= 201103L
304*38fd1498Szrj auto_sbitmap (auto_sbitmap &&);
305*38fd1498Szrj auto_sbitmap &operator = (auto_sbitmap &&);
306*38fd1498Szrj #endif
307*38fd1498Szrj
308*38fd1498Szrj /* The bitmap we are managing. */
309*38fd1498Szrj sbitmap m_bitmap;
310*38fd1498Szrj };
311*38fd1498Szrj
312*38fd1498Szrj #endif /* ! GCC_SBITMAP_H */
313