]>
Commit | Line | Data |
---|---|---|
096ab9ea | 1 | /* Functions to support general ended bitmaps. |
85ec4feb | 2 | Copyright (C) 1997-2018 Free Software Foundation, Inc. |
096ab9ea | 3 | |
1322177d | 4 | This file is part of GCC. |
096ab9ea | 5 | |
1322177d LB |
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 | |
9dcd6f09 | 8 | Software Foundation; either version 3, or (at your option) any later |
1322177d | 9 | version. |
096ab9ea | 10 | |
1322177d LB |
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. | |
096ab9ea RK |
15 | |
16 | You should have received a copy of the GNU General Public License | |
9dcd6f09 NC |
17 | along with GCC; see the file COPYING3. If not see |
18 | <http://www.gnu.org/licenses/>. */ | |
096ab9ea | 19 | |
88657302 | 20 | #ifndef GCC_BITMAP_H |
ca7fd9cd | 21 | #define GCC_BITMAP_H |
0263463d SB |
22 | |
23 | /* Implementation of sparse integer sets as a linked list. | |
24 | ||
25 | This sparse set representation is suitable for sparse sets with an | |
26 | unknown (a priori) universe. The set is represented as a double-linked | |
84562394 | 27 | list of container nodes (struct bitmap_element). Each node consists |
0263463d SB |
28 | of an index for the first member that could be held in the container, |
29 | a small array of integers that represent the members in the container, | |
30 | and pointers to the next and previous element in the linked list. The | |
31 | elements in the list are sorted in ascending order, i.e. the head of | |
32 | the list holds the element with the smallest member of the set. | |
33 | ||
34 | For a given member I in the set: | |
35 | - the element for I will have index is I / (bits per element) | |
36 | - the position for I within element is I % (bits per element) | |
37 | ||
38 | This representation is very space-efficient for large sparse sets, and | |
39 | the size of the set can be changed dynamically without much overhead. | |
40 | An important parameter is the number of bits per element. In this | |
41 | implementation, there are 128 bits per element. This results in a | |
42 | high storage overhead *per element*, but a small overall overhead if | |
43 | the set is very sparse. | |
44 | ||
45 | The downside is that many operations are relatively slow because the | |
46 | linked list has to be traversed to test membership (i.e. member_p/ | |
47 | add_member/remove_member). To improve the performance of this set | |
48 | representation, the last accessed element and its index are cached. | |
49 | For membership tests on members close to recently accessed members, | |
50 | the cached last element improves membership test to a constant-time | |
51 | operation. | |
52 | ||
53 | The following operations can always be performed in O(1) time: | |
54 | ||
55 | * clear : bitmap_clear | |
56 | * choose_one : (not implemented, but could be | |
57 | implemented in constant time) | |
58 | ||
59 | The following operations can be performed in O(E) time worst-case (with | |
60 | E the number of elements in the linked list), but in O(1) time with a | |
61 | suitable access patterns: | |
62 | ||
63 | * member_p : bitmap_bit_p | |
64 | * add_member : bitmap_set_bit | |
65 | * remove_member : bitmap_clear_bit | |
66 | ||
67 | The following operations can be performed in O(E) time: | |
68 | ||
69 | * cardinality : bitmap_count_bits | |
70 | * set_size : bitmap_last_set_bit (but this could | |
71 | in constant time with a pointer to | |
72 | the last element in the chain) | |
73 | ||
74 | Additionally, the linked-list sparse set representation supports | |
75 | enumeration of the members in O(E) time: | |
76 | ||
77 | * forall : EXECUTE_IF_SET_IN_BITMAP | |
78 | * set_copy : bitmap_copy | |
79 | * set_intersection : bitmap_intersect_p / | |
80 | bitmap_and / bitmap_and_into / | |
81 | EXECUTE_IF_AND_IN_BITMAP | |
82 | * set_union : bitmap_ior / bitmap_ior_into | |
83 | * set_difference : bitmap_intersect_compl_p / | |
84 | bitmap_and_comp / bitmap_and_comp_into / | |
85 | EXECUTE_IF_AND_COMPL_IN_BITMAP | |
86 | * set_disjuction : bitmap_xor_comp / bitmap_xor_comp_into | |
87 | * set_compare : bitmap_equal_p | |
88 | ||
026c3cfd | 89 | Some operations on 3 sets that occur frequently in data flow problems |
0263463d SB |
90 | are also implemented: |
91 | ||
92 | * A | (B & C) : bitmap_ior_and_into | |
93 | * A | (B & ~C) : bitmap_ior_and_compl / | |
94 | bitmap_ior_and_compl_into | |
95 | ||
96 | The storage requirements for linked-list sparse sets are O(E), with E->N | |
97 | in the worst case (a sparse set with large distances between the values | |
98 | of the set members). | |
99 | ||
100 | The linked-list set representation works well for problems involving very | |
101 | sparse sets. The canonical example in GCC is, of course, the "set of | |
102 | sets" for some CFG-based data flow problems (liveness analysis, dominance | |
103 | frontiers, etc.). | |
104 | ||
105 | This representation also works well for data flow problems where the size | |
106 | of the set may grow dynamically, but care must be taken that the member_p, | |
107 | add_member, and remove_member operations occur with a suitable access | |
108 | pattern. | |
109 | ||
110 | For random-access sets with a known, relatively small universe size, the | |
111 | SparseSet or simple bitmap representations may be more efficient than a | |
112 | linked-list set. For random-access sets of unknown universe, a hash table | |
113 | or a balanced binary tree representation is likely to be a more suitable | |
114 | choice. | |
115 | ||
116 | Traversing linked lists is usually cache-unfriendly, even with the last | |
117 | accessed element cached. | |
118 | ||
119 | Cache performance can be improved by keeping the elements in the set | |
120 | grouped together in memory, using a dedicated obstack for a set (or group | |
121 | of related sets). Elements allocated on obstacks are released to a | |
122 | free-list and taken off the free list. If multiple sets are allocated on | |
123 | the same obstack, elements freed from one set may be re-used for one of | |
124 | the other sets. This usually helps avoid cache misses. | |
125 | ||
126 | A single free-list is used for all sets allocated in GGC space. This is | |
127 | bad for persistent sets, so persistent sets should be allocated on an | |
128 | obstack whenever possible. */ | |
129 | ||
b60db1ba | 130 | #include "obstack.h" |
2d44c7de ML |
131 | |
132 | /* Bitmap memory usage. */ | |
133 | struct bitmap_usage: public mem_usage | |
134 | { | |
135 | /* Default contructor. */ | |
136 | bitmap_usage (): m_nsearches (0), m_search_iter (0) {} | |
137 | /* Constructor. */ | |
138 | bitmap_usage (size_t allocated, size_t times, size_t peak, | |
139 | uint64_t nsearches, uint64_t search_iter) | |
140 | : mem_usage (allocated, times, peak), | |
141 | m_nsearches (nsearches), m_search_iter (search_iter) {} | |
142 | ||
143 | /* Sum the usage with SECOND usage. */ | |
80a4fe78 ML |
144 | bitmap_usage |
145 | operator+ (const bitmap_usage &second) | |
2d44c7de ML |
146 | { |
147 | return bitmap_usage (m_allocated + second.m_allocated, | |
148 | m_times + second.m_times, | |
149 | m_peak + second.m_peak, | |
150 | m_nsearches + second.m_nsearches, | |
151 | m_search_iter + second.m_search_iter); | |
152 | } | |
153 | ||
154 | /* Dump usage coupled to LOC location, where TOTAL is sum of all rows. */ | |
80a4fe78 ML |
155 | inline void |
156 | dump (mem_location *loc, mem_usage &total) const | |
2d44c7de | 157 | { |
ac059261 | 158 | char *location_string = loc->to_string (); |
2d44c7de | 159 | |
43331dfb RB |
160 | fprintf (stderr, "%-48s %10" PRIu64 ":%5.1f%%" |
161 | "%10" PRIu64 "%10" PRIu64 ":%5.1f%%" | |
162 | "%12" PRIu64 "%12" PRIu64 "%10s\n", | |
163 | location_string, (uint64_t)m_allocated, | |
164 | get_percent (m_allocated, total.m_allocated), | |
165 | (uint64_t)m_peak, (uint64_t)m_times, | |
2d44c7de | 166 | get_percent (m_times, total.m_times), |
43331dfb | 167 | m_nsearches, m_search_iter, |
2d44c7de | 168 | loc->m_ggc ? "ggc" : "heap"); |
ac059261 ML |
169 | |
170 | free (location_string); | |
2d44c7de ML |
171 | } |
172 | ||
173 | /* Dump header with NAME. */ | |
80a4fe78 ML |
174 | static inline void |
175 | dump_header (const char *name) | |
2d44c7de ML |
176 | { |
177 | fprintf (stderr, "%-48s %11s%16s%17s%12s%12s%10s\n", name, "Leak", "Peak", | |
178 | "Times", "N searches", "Search iter", "Type"); | |
179 | print_dash_line (); | |
180 | } | |
181 | ||
182 | /* Number search operations. */ | |
183 | uint64_t m_nsearches; | |
184 | /* Number of search iterations. */ | |
185 | uint64_t m_search_iter; | |
186 | }; | |
187 | ||
188 | /* Bitmap memory description. */ | |
189 | extern mem_alloc_description<bitmap_usage> bitmap_mem_desc; | |
a05924f9 | 190 | |
72e42e26 SB |
191 | /* Fundamental storage type for bitmap. */ |
192 | ||
72e42e26 | 193 | typedef unsigned long BITMAP_WORD; |
65a6f342 NS |
194 | /* BITMAP_WORD_BITS needs to be unsigned, but cannot contain casts as |
195 | it is used in preprocessor directives -- hence the 1u. */ | |
196 | #define BITMAP_WORD_BITS (CHAR_BIT * SIZEOF_LONG * 1u) | |
72e42e26 | 197 | |
096ab9ea RK |
198 | /* Number of words to use for each element in the linked list. */ |
199 | ||
200 | #ifndef BITMAP_ELEMENT_WORDS | |
65a6f342 | 201 | #define BITMAP_ELEMENT_WORDS ((128 + BITMAP_WORD_BITS - 1) / BITMAP_WORD_BITS) |
096ab9ea RK |
202 | #endif |
203 | ||
65a6f342 | 204 | /* Number of bits in each actual element of a bitmap. */ |
096ab9ea | 205 | |
65a6f342 | 206 | #define BITMAP_ELEMENT_ALL_BITS (BITMAP_ELEMENT_WORDS * BITMAP_WORD_BITS) |
096ab9ea | 207 | |
7932a3db | 208 | /* Obstack for allocating bitmaps and elements from. */ |
84562394 OE |
209 | struct GTY (()) bitmap_obstack { |
210 | struct bitmap_element *elements; | |
211 | struct bitmap_head *heads; | |
7932a3db | 212 | struct obstack GTY ((skip)) obstack; |
84562394 | 213 | }; |
7932a3db | 214 | |
096ab9ea RK |
215 | /* Bitmap set element. We use a linked list to hold only the bits that |
216 | are set. This allows for use to grow the bitset dynamically without | |
c22cacf3 | 217 | having to realloc and copy a giant bit array. |
5765e552 KZ |
218 | |
219 | The free list is implemented as a list of lists. There is one | |
220 | outer list connected together by prev fields. Each element of that | |
221 | outer is an inner list (that may consist only of the outer list | |
222 | element) that are connected by the next fields. The prev pointer | |
223 | is undefined for interior elements. This allows | |
224 | bitmap_elt_clear_from to be implemented in unit time rather than | |
225 | linear in the number of elements to be freed. */ | |
096ab9ea | 226 | |
84562394 OE |
227 | struct GTY((chain_next ("%h.next"), chain_prev ("%h.prev"))) bitmap_element { |
228 | struct bitmap_element *next; /* Next element. */ | |
229 | struct bitmap_element *prev; /* Previous element. */ | |
eebedaa5 | 230 | unsigned int indx; /* regno/BITMAP_ELEMENT_ALL_BITS. */ |
72e42e26 | 231 | BITMAP_WORD bits[BITMAP_ELEMENT_WORDS]; /* Bits that are set. */ |
84562394 | 232 | }; |
096ab9ea | 233 | |
3c53f55a SB |
234 | /* Head of bitmap linked list. The 'current' member points to something |
235 | already pointed to by the chain started by first, so GTY((skip)) it. */ | |
01d419ae | 236 | |
84562394 | 237 | struct GTY(()) bitmap_head { |
3c53f55a SB |
238 | unsigned int indx; /* Index of last element looked at. */ |
239 | unsigned int descriptor_id; /* Unique identifier for the allocation | |
240 | site of this bitmap, for detailed | |
241 | statistics gathering. */ | |
7f3f8d3f RG |
242 | bitmap_element *first; /* First element in linked list. */ |
243 | bitmap_element * GTY((skip(""))) current; /* Last element looked at. */ | |
7f3f8d3f RG |
244 | bitmap_obstack *obstack; /* Obstack to allocate elements from. |
245 | If NULL, then use GGC allocation. */ | |
84562394 | 246 | }; |
7932a3db | 247 | |
096ab9ea | 248 | /* Global data */ |
ae0ed63a | 249 | extern bitmap_element bitmap_zero_bits; /* Zero bitmap element */ |
7932a3db | 250 | extern bitmap_obstack bitmap_default_obstack; /* Default bitmap obstack */ |
096ab9ea RK |
251 | |
252 | /* Clear a bitmap by freeing up the linked list. */ | |
4682ae04 | 253 | extern void bitmap_clear (bitmap); |
096ab9ea | 254 | |
eebedaa5 | 255 | /* Copy a bitmap to another bitmap. */ |
e326eeb5 | 256 | extern void bitmap_copy (bitmap, const_bitmap); |
096ab9ea | 257 | |
43331dfb RB |
258 | /* Move a bitmap to another bitmap. */ |
259 | extern void bitmap_move (bitmap, bitmap); | |
260 | ||
8229306b | 261 | /* True if two bitmaps are identical. */ |
e326eeb5 | 262 | extern bool bitmap_equal_p (const_bitmap, const_bitmap); |
8229306b | 263 | |
55994078 | 264 | /* True if the bitmaps intersect (their AND is non-empty). */ |
e326eeb5 | 265 | extern bool bitmap_intersect_p (const_bitmap, const_bitmap); |
55994078 NS |
266 | |
267 | /* True if the complement of the second intersects the first (their | |
268 | AND_COMPL is non-empty). */ | |
e326eeb5 | 269 | extern bool bitmap_intersect_compl_p (const_bitmap, const_bitmap); |
55994078 NS |
270 | |
271 | /* True if MAP is an empty bitmap. */ | |
f61e445a LC |
272 | inline bool bitmap_empty_p (const_bitmap map) |
273 | { | |
274 | return !map->first; | |
275 | } | |
eb59b8de | 276 | |
76e910c6 RG |
277 | /* True if the bitmap has only a single bit set. */ |
278 | extern bool bitmap_single_bit_set_p (const_bitmap); | |
279 | ||
1bc40c7e | 280 | /* Count the number of bits set in the bitmap. */ |
e326eeb5 | 281 | extern unsigned long bitmap_count_bits (const_bitmap); |
1bc40c7e | 282 | |
478baf91 JL |
283 | /* Count the number of unique bits set across the two bitmaps. */ |
284 | extern unsigned long bitmap_count_unique_bits (const_bitmap, const_bitmap); | |
285 | ||
88c4f655 NS |
286 | /* Boolean operations on bitmaps. The _into variants are two operand |
287 | versions that modify the first source operand. The other variants | |
288 | are three operand versions that to not destroy the source bitmaps. | |
289 | The operations supported are &, & ~, |, ^. */ | |
e326eeb5 | 290 | extern void bitmap_and (bitmap, const_bitmap, const_bitmap); |
7b19209f | 291 | extern bool bitmap_and_into (bitmap, const_bitmap); |
e326eeb5 KG |
292 | extern bool bitmap_and_compl (bitmap, const_bitmap, const_bitmap); |
293 | extern bool bitmap_and_compl_into (bitmap, const_bitmap); | |
1bc40c7e | 294 | #define bitmap_compl_and(DST, A, B) bitmap_and_compl (DST, B, A) |
e326eeb5 | 295 | extern void bitmap_compl_and_into (bitmap, const_bitmap); |
1bc40c7e | 296 | extern void bitmap_clear_range (bitmap, unsigned int, unsigned int); |
6fb5fa3c | 297 | extern void bitmap_set_range (bitmap, unsigned int, unsigned int); |
e326eeb5 KG |
298 | extern bool bitmap_ior (bitmap, const_bitmap, const_bitmap); |
299 | extern bool bitmap_ior_into (bitmap, const_bitmap); | |
300 | extern void bitmap_xor (bitmap, const_bitmap, const_bitmap); | |
301 | extern void bitmap_xor_into (bitmap, const_bitmap); | |
88c4f655 | 302 | |
7ff23740 PB |
303 | /* DST = A | (B & C). Return true if DST changes. */ |
304 | extern bool bitmap_ior_and_into (bitmap DST, const_bitmap B, const_bitmap C); | |
88c4f655 | 305 | /* DST = A | (B & ~C). Return true if DST changes. */ |
0263463d SB |
306 | extern bool bitmap_ior_and_compl (bitmap DST, const_bitmap A, |
307 | const_bitmap B, const_bitmap C); | |
88c4f655 | 308 | /* A |= (B & ~C). Return true if A changes. */ |
0263463d SB |
309 | extern bool bitmap_ior_and_compl_into (bitmap A, |
310 | const_bitmap B, const_bitmap C); | |
096ab9ea | 311 | |
5f0d975b RG |
312 | /* Clear a single bit in a bitmap. Return true if the bit changed. */ |
313 | extern bool bitmap_clear_bit (bitmap, int); | |
096ab9ea | 314 | |
5f0d975b RG |
315 | /* Set a single bit in a bitmap. Return true if the bit changed. */ |
316 | extern bool bitmap_set_bit (bitmap, int); | |
096ab9ea RK |
317 | |
318 | /* Return true if a register is set in a register set. */ | |
4682ae04 | 319 | extern int bitmap_bit_p (bitmap, int); |
096ab9ea RK |
320 | |
321 | /* Debug functions to print a bitmap linked list. */ | |
e326eeb5 KG |
322 | extern void debug_bitmap (const_bitmap); |
323 | extern void debug_bitmap_file (FILE *, const_bitmap); | |
096ab9ea | 324 | |
f9da5064 | 325 | /* Print a bitmap. */ |
e326eeb5 | 326 | extern void bitmap_print (FILE *, const_bitmap, const char *, const char *); |
22fa5b8a | 327 | |
5765e552 | 328 | /* Initialize and release a bitmap obstack. */ |
7932a3db NS |
329 | extern void bitmap_obstack_initialize (bitmap_obstack *); |
330 | extern void bitmap_obstack_release (bitmap_obstack *); | |
f75709c6 JH |
331 | extern void bitmap_register (bitmap MEM_STAT_DECL); |
332 | extern void dump_bitmap_statistics (void); | |
096ab9ea | 333 | |
7932a3db NS |
334 | /* Initialize a bitmap header. OBSTACK indicates the bitmap obstack |
335 | to allocate from, NULL for GC'd bitmap. */ | |
336 | ||
337 | static inline void | |
2a1a5f30 | 338 | bitmap_initialize (bitmap head, bitmap_obstack *obstack CXX_MEM_STAT_INFO) |
7932a3db NS |
339 | { |
340 | head->first = head->current = NULL; | |
341 | head->obstack = obstack; | |
7aa6d18a SB |
342 | if (GATHER_STATISTICS) |
343 | bitmap_register (head PASS_MEM_STAT); | |
7932a3db NS |
344 | } |
345 | ||
346 | /* Allocate and free bitmaps from obstack, malloc and gc'd memory. */ | |
3fe793df TS |
347 | extern bitmap bitmap_alloc (bitmap_obstack *obstack CXX_MEM_STAT_INFO); |
348 | #define BITMAP_ALLOC bitmap_alloc | |
349 | extern bitmap bitmap_gc_alloc (ALONE_CXX_MEM_STAT_INFO); | |
350 | #define BITMAP_GGC_ALLOC bitmap_gc_alloc | |
7932a3db | 351 | extern void bitmap_obstack_free (bitmap); |
096ab9ea | 352 | |
ea193996 | 353 | /* A few compatibility/functions macros for compatibility with sbitmaps */ |
f61e445a LC |
354 | inline void dump_bitmap (FILE *file, const_bitmap map) |
355 | { | |
356 | bitmap_print (file, map, "", "\n"); | |
357 | } | |
84562394 OE |
358 | extern void debug (const bitmap_head &ref); |
359 | extern void debug (const bitmap_head *ptr); | |
f61e445a | 360 | |
e326eeb5 | 361 | extern unsigned bitmap_first_set_bit (const_bitmap); |
12802c2b | 362 | extern unsigned bitmap_last_set_bit (const_bitmap); |
ea193996 | 363 | |
1af4bba8 | 364 | /* Compute bitmap hash (for purposes of hashing etc.) */ |
c3284718 | 365 | extern hashval_t bitmap_hash (const_bitmap); |
1af4bba8 | 366 | |
096ab9ea | 367 | /* Do any cleanup needed on a bitmap when it is no longer used. */ |
61ad0914 BE |
368 | #define BITMAP_FREE(BITMAP) \ |
369 | ((void) (bitmap_obstack_free ((bitmap) BITMAP), (BITMAP) = (bitmap) NULL)) | |
e7749837 | 370 | |
87c476a2 | 371 | /* Iterator for bitmaps. */ |
096ab9ea | 372 | |
84562394 | 373 | struct bitmap_iterator |
87c476a2 | 374 | { |
e90ea8cb NS |
375 | /* Pointer to the current bitmap element. */ |
376 | bitmap_element *elt1; | |
c22cacf3 | 377 | |
e90ea8cb NS |
378 | /* Pointer to 2nd bitmap element when two are involved. */ |
379 | bitmap_element *elt2; | |
380 | ||
381 | /* Word within the current element. */ | |
382 | unsigned word_no; | |
c22cacf3 | 383 | |
87c476a2 ZD |
384 | /* Contents of the actually processed word. When finding next bit |
385 | it is shifted right, so that the actual bit is always the least | |
386 | significant bit of ACTUAL. */ | |
e90ea8cb | 387 | BITMAP_WORD bits; |
84562394 | 388 | }; |
87c476a2 | 389 | |
e90ea8cb NS |
390 | /* Initialize a single bitmap iterator. START_BIT is the first bit to |
391 | iterate from. */ | |
87c476a2 | 392 | |
e90ea8cb | 393 | static inline void |
e326eeb5 | 394 | bmp_iter_set_init (bitmap_iterator *bi, const_bitmap map, |
e90ea8cb | 395 | unsigned start_bit, unsigned *bit_no) |
87c476a2 | 396 | { |
e90ea8cb NS |
397 | bi->elt1 = map->first; |
398 | bi->elt2 = NULL; | |
399 | ||
400 | /* Advance elt1 until it is not before the block containing start_bit. */ | |
401 | while (1) | |
87c476a2 | 402 | { |
e90ea8cb NS |
403 | if (!bi->elt1) |
404 | { | |
405 | bi->elt1 = &bitmap_zero_bits; | |
406 | break; | |
407 | } | |
c22cacf3 | 408 | |
e90ea8cb NS |
409 | if (bi->elt1->indx >= start_bit / BITMAP_ELEMENT_ALL_BITS) |
410 | break; | |
411 | bi->elt1 = bi->elt1->next; | |
87c476a2 ZD |
412 | } |
413 | ||
e90ea8cb NS |
414 | /* We might have gone past the start bit, so reinitialize it. */ |
415 | if (bi->elt1->indx != start_bit / BITMAP_ELEMENT_ALL_BITS) | |
416 | start_bit = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS; | |
c22cacf3 | 417 | |
e90ea8cb NS |
418 | /* Initialize for what is now start_bit. */ |
419 | bi->word_no = start_bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS; | |
420 | bi->bits = bi->elt1->bits[bi->word_no]; | |
421 | bi->bits >>= start_bit % BITMAP_WORD_BITS; | |
422 | ||
423 | /* If this word is zero, we must make sure we're not pointing at the | |
424 | first bit, otherwise our incrementing to the next word boundary | |
425 | will fail. It won't matter if this increment moves us into the | |
426 | next word. */ | |
427 | start_bit += !bi->bits; | |
c22cacf3 | 428 | |
e90ea8cb | 429 | *bit_no = start_bit; |
87c476a2 ZD |
430 | } |
431 | ||
e90ea8cb NS |
432 | /* Initialize an iterator to iterate over the intersection of two |
433 | bitmaps. START_BIT is the bit to commence from. */ | |
87c476a2 | 434 | |
e90ea8cb | 435 | static inline void |
e326eeb5 | 436 | bmp_iter_and_init (bitmap_iterator *bi, const_bitmap map1, const_bitmap map2, |
e90ea8cb | 437 | unsigned start_bit, unsigned *bit_no) |
87c476a2 | 438 | { |
e90ea8cb NS |
439 | bi->elt1 = map1->first; |
440 | bi->elt2 = map2->first; | |
87c476a2 | 441 | |
e90ea8cb NS |
442 | /* Advance elt1 until it is not before the block containing |
443 | start_bit. */ | |
87c476a2 ZD |
444 | while (1) |
445 | { | |
e90ea8cb | 446 | if (!bi->elt1) |
87c476a2 | 447 | { |
e90ea8cb NS |
448 | bi->elt2 = NULL; |
449 | break; | |
87c476a2 | 450 | } |
c22cacf3 | 451 | |
e90ea8cb NS |
452 | if (bi->elt1->indx >= start_bit / BITMAP_ELEMENT_ALL_BITS) |
453 | break; | |
454 | bi->elt1 = bi->elt1->next; | |
87c476a2 | 455 | } |
c22cacf3 | 456 | |
e90ea8cb NS |
457 | /* Advance elt2 until it is not before elt1. */ |
458 | while (1) | |
87c476a2 | 459 | { |
e90ea8cb NS |
460 | if (!bi->elt2) |
461 | { | |
462 | bi->elt1 = bi->elt2 = &bitmap_zero_bits; | |
463 | break; | |
464 | } | |
c22cacf3 | 465 | |
e90ea8cb NS |
466 | if (bi->elt2->indx >= bi->elt1->indx) |
467 | break; | |
468 | bi->elt2 = bi->elt2->next; | |
87c476a2 ZD |
469 | } |
470 | ||
e28d0cfb | 471 | /* If we're at the same index, then we have some intersecting bits. */ |
e90ea8cb | 472 | if (bi->elt1->indx == bi->elt2->indx) |
87c476a2 | 473 | { |
e90ea8cb | 474 | /* We might have advanced beyond the start_bit, so reinitialize |
c22cacf3 | 475 | for that. */ |
e90ea8cb NS |
476 | if (bi->elt1->indx != start_bit / BITMAP_ELEMENT_ALL_BITS) |
477 | start_bit = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS; | |
c22cacf3 | 478 | |
e90ea8cb NS |
479 | bi->word_no = start_bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS; |
480 | bi->bits = bi->elt1->bits[bi->word_no] & bi->elt2->bits[bi->word_no]; | |
481 | bi->bits >>= start_bit % BITMAP_WORD_BITS; | |
87c476a2 ZD |
482 | } |
483 | else | |
484 | { | |
e90ea8cb NS |
485 | /* Otherwise we must immediately advance elt1, so initialize for |
486 | that. */ | |
487 | bi->word_no = BITMAP_ELEMENT_WORDS - 1; | |
488 | bi->bits = 0; | |
87c476a2 | 489 | } |
c22cacf3 | 490 | |
e90ea8cb NS |
491 | /* If this word is zero, we must make sure we're not pointing at the |
492 | first bit, otherwise our incrementing to the next word boundary | |
493 | will fail. It won't matter if this increment moves us into the | |
494 | next word. */ | |
495 | start_bit += !bi->bits; | |
c22cacf3 | 496 | |
e90ea8cb | 497 | *bit_no = start_bit; |
87c476a2 ZD |
498 | } |
499 | ||
e90ea8cb NS |
500 | /* Initialize an iterator to iterate over the bits in MAP1 & ~MAP2. |
501 | */ | |
87c476a2 | 502 | |
e90ea8cb | 503 | static inline void |
0263463d SB |
504 | bmp_iter_and_compl_init (bitmap_iterator *bi, |
505 | const_bitmap map1, const_bitmap map2, | |
e90ea8cb | 506 | unsigned start_bit, unsigned *bit_no) |
87c476a2 | 507 | { |
e90ea8cb NS |
508 | bi->elt1 = map1->first; |
509 | bi->elt2 = map2->first; | |
87c476a2 | 510 | |
e90ea8cb | 511 | /* Advance elt1 until it is not before the block containing start_bit. */ |
87c476a2 ZD |
512 | while (1) |
513 | { | |
e90ea8cb | 514 | if (!bi->elt1) |
87c476a2 | 515 | { |
e90ea8cb NS |
516 | bi->elt1 = &bitmap_zero_bits; |
517 | break; | |
87c476a2 | 518 | } |
c22cacf3 | 519 | |
e90ea8cb NS |
520 | if (bi->elt1->indx >= start_bit / BITMAP_ELEMENT_ALL_BITS) |
521 | break; | |
522 | bi->elt1 = bi->elt1->next; | |
87c476a2 | 523 | } |
e90ea8cb NS |
524 | |
525 | /* Advance elt2 until it is not before elt1. */ | |
526 | while (bi->elt2 && bi->elt2->indx < bi->elt1->indx) | |
527 | bi->elt2 = bi->elt2->next; | |
528 | ||
529 | /* We might have advanced beyond the start_bit, so reinitialize for | |
530 | that. */ | |
531 | if (bi->elt1->indx != start_bit / BITMAP_ELEMENT_ALL_BITS) | |
532 | start_bit = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS; | |
c22cacf3 | 533 | |
e90ea8cb NS |
534 | bi->word_no = start_bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS; |
535 | bi->bits = bi->elt1->bits[bi->word_no]; | |
536 | if (bi->elt2 && bi->elt1->indx == bi->elt2->indx) | |
537 | bi->bits &= ~bi->elt2->bits[bi->word_no]; | |
538 | bi->bits >>= start_bit % BITMAP_WORD_BITS; | |
c22cacf3 | 539 | |
e90ea8cb NS |
540 | /* If this word is zero, we must make sure we're not pointing at the |
541 | first bit, otherwise our incrementing to the next word boundary | |
542 | will fail. It won't matter if this increment moves us into the | |
543 | next word. */ | |
544 | start_bit += !bi->bits; | |
c22cacf3 | 545 | |
e90ea8cb | 546 | *bit_no = start_bit; |
87c476a2 ZD |
547 | } |
548 | ||
e90ea8cb | 549 | /* Advance to the next bit in BI. We don't advance to the next |
d46aed51 | 550 | nonzero bit yet. */ |
87c476a2 | 551 | |
e90ea8cb NS |
552 | static inline void |
553 | bmp_iter_next (bitmap_iterator *bi, unsigned *bit_no) | |
87c476a2 | 554 | { |
e90ea8cb NS |
555 | bi->bits >>= 1; |
556 | *bit_no += 1; | |
557 | } | |
87c476a2 | 558 | |
d5568f03 JH |
559 | /* Advance to first set bit in BI. */ |
560 | ||
561 | static inline void | |
562 | bmp_iter_next_bit (bitmap_iterator * bi, unsigned *bit_no) | |
563 | { | |
564 | #if (GCC_VERSION >= 3004) | |
565 | { | |
566 | unsigned int n = __builtin_ctzl (bi->bits); | |
567 | gcc_assert (sizeof (unsigned long) == sizeof (BITMAP_WORD)); | |
568 | bi->bits >>= n; | |
569 | *bit_no += n; | |
570 | } | |
571 | #else | |
572 | while (!(bi->bits & 1)) | |
573 | { | |
574 | bi->bits >>= 1; | |
575 | *bit_no += 1; | |
576 | } | |
577 | #endif | |
578 | } | |
579 | ||
d46aed51 | 580 | /* Advance to the next nonzero bit of a single bitmap, we will have |
e90ea8cb NS |
581 | already advanced past the just iterated bit. Return true if there |
582 | is a bit to iterate. */ | |
87c476a2 | 583 | |
e90ea8cb NS |
584 | static inline bool |
585 | bmp_iter_set (bitmap_iterator *bi, unsigned *bit_no) | |
586 | { | |
d46aed51 | 587 | /* If our current word is nonzero, it contains the bit we want. */ |
e90ea8cb | 588 | if (bi->bits) |
87c476a2 | 589 | { |
e90ea8cb | 590 | next_bit: |
d5568f03 | 591 | bmp_iter_next_bit (bi, bit_no); |
e90ea8cb | 592 | return true; |
87c476a2 ZD |
593 | } |
594 | ||
e90ea8cb NS |
595 | /* Round up to the word boundary. We might have just iterated past |
596 | the end of the last word, hence the -1. It is not possible for | |
597 | bit_no to point at the beginning of the now last word. */ | |
598 | *bit_no = ((*bit_no + BITMAP_WORD_BITS - 1) | |
599 | / BITMAP_WORD_BITS * BITMAP_WORD_BITS); | |
600 | bi->word_no++; | |
87c476a2 | 601 | |
e90ea8cb | 602 | while (1) |
87c476a2 | 603 | { |
d46aed51 | 604 | /* Find the next nonzero word in this elt. */ |
e90ea8cb NS |
605 | while (bi->word_no != BITMAP_ELEMENT_WORDS) |
606 | { | |
607 | bi->bits = bi->elt1->bits[bi->word_no]; | |
608 | if (bi->bits) | |
609 | goto next_bit; | |
610 | *bit_no += BITMAP_WORD_BITS; | |
611 | bi->word_no++; | |
612 | } | |
c22cacf3 | 613 | |
a30fe4b6 RB |
614 | /* Make sure we didn't remove the element while iterating. */ |
615 | gcc_checking_assert (bi->elt1->indx != -1U); | |
616 | ||
e90ea8cb NS |
617 | /* Advance to the next element. */ |
618 | bi->elt1 = bi->elt1->next; | |
619 | if (!bi->elt1) | |
620 | return false; | |
621 | *bit_no = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS; | |
622 | bi->word_no = 0; | |
87c476a2 | 623 | } |
87c476a2 ZD |
624 | } |
625 | ||
d46aed51 KH |
626 | /* Advance to the next nonzero bit of an intersecting pair of |
627 | bitmaps. We will have already advanced past the just iterated bit. | |
e90ea8cb | 628 | Return true if there is a bit to iterate. */ |
87c476a2 | 629 | |
e90ea8cb NS |
630 | static inline bool |
631 | bmp_iter_and (bitmap_iterator *bi, unsigned *bit_no) | |
87c476a2 | 632 | { |
d46aed51 | 633 | /* If our current word is nonzero, it contains the bit we want. */ |
e90ea8cb NS |
634 | if (bi->bits) |
635 | { | |
636 | next_bit: | |
d5568f03 | 637 | bmp_iter_next_bit (bi, bit_no); |
e90ea8cb NS |
638 | return true; |
639 | } | |
87c476a2 | 640 | |
e90ea8cb NS |
641 | /* Round up to the word boundary. We might have just iterated past |
642 | the end of the last word, hence the -1. It is not possible for | |
643 | bit_no to point at the beginning of the now last word. */ | |
644 | *bit_no = ((*bit_no + BITMAP_WORD_BITS - 1) | |
645 | / BITMAP_WORD_BITS * BITMAP_WORD_BITS); | |
646 | bi->word_no++; | |
c22cacf3 | 647 | |
87c476a2 ZD |
648 | while (1) |
649 | { | |
d46aed51 | 650 | /* Find the next nonzero word in this elt. */ |
e90ea8cb | 651 | while (bi->word_no != BITMAP_ELEMENT_WORDS) |
87c476a2 | 652 | { |
e90ea8cb NS |
653 | bi->bits = bi->elt1->bits[bi->word_no] & bi->elt2->bits[bi->word_no]; |
654 | if (bi->bits) | |
655 | goto next_bit; | |
656 | *bit_no += BITMAP_WORD_BITS; | |
657 | bi->word_no++; | |
87c476a2 | 658 | } |
c22cacf3 | 659 | |
e90ea8cb | 660 | /* Advance to the next identical element. */ |
87c476a2 ZD |
661 | do |
662 | { | |
a30fe4b6 RB |
663 | /* Make sure we didn't remove the element while iterating. */ |
664 | gcc_checking_assert (bi->elt1->indx != -1U); | |
665 | ||
e90ea8cb NS |
666 | /* Advance elt1 while it is less than elt2. We always want |
667 | to advance one elt. */ | |
668 | do | |
87c476a2 | 669 | { |
e90ea8cb NS |
670 | bi->elt1 = bi->elt1->next; |
671 | if (!bi->elt1) | |
672 | return false; | |
673 | } | |
674 | while (bi->elt1->indx < bi->elt2->indx); | |
c22cacf3 | 675 | |
a30fe4b6 RB |
676 | /* Make sure we didn't remove the element while iterating. */ |
677 | gcc_checking_assert (bi->elt2->indx != -1U); | |
678 | ||
e90ea8cb NS |
679 | /* Advance elt2 to be no less than elt1. This might not |
680 | advance. */ | |
681 | while (bi->elt2->indx < bi->elt1->indx) | |
682 | { | |
683 | bi->elt2 = bi->elt2->next; | |
684 | if (!bi->elt2) | |
685 | return false; | |
87c476a2 ZD |
686 | } |
687 | } | |
e90ea8cb | 688 | while (bi->elt1->indx != bi->elt2->indx); |
c22cacf3 | 689 | |
e90ea8cb NS |
690 | *bit_no = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS; |
691 | bi->word_no = 0; | |
87c476a2 ZD |
692 | } |
693 | } | |
694 | ||
d46aed51 | 695 | /* Advance to the next nonzero bit in the intersection of |
e90ea8cb NS |
696 | complemented bitmaps. We will have already advanced past the just |
697 | iterated bit. */ | |
87c476a2 | 698 | |
e90ea8cb NS |
699 | static inline bool |
700 | bmp_iter_and_compl (bitmap_iterator *bi, unsigned *bit_no) | |
87c476a2 | 701 | { |
d46aed51 | 702 | /* If our current word is nonzero, it contains the bit we want. */ |
e90ea8cb | 703 | if (bi->bits) |
87c476a2 | 704 | { |
e90ea8cb | 705 | next_bit: |
d5568f03 | 706 | bmp_iter_next_bit (bi, bit_no); |
e90ea8cb | 707 | return true; |
87c476a2 ZD |
708 | } |
709 | ||
e90ea8cb NS |
710 | /* Round up to the word boundary. We might have just iterated past |
711 | the end of the last word, hence the -1. It is not possible for | |
712 | bit_no to point at the beginning of the now last word. */ | |
713 | *bit_no = ((*bit_no + BITMAP_WORD_BITS - 1) | |
714 | / BITMAP_WORD_BITS * BITMAP_WORD_BITS); | |
715 | bi->word_no++; | |
87c476a2 | 716 | |
e90ea8cb | 717 | while (1) |
87c476a2 | 718 | { |
d46aed51 | 719 | /* Find the next nonzero word in this elt. */ |
e90ea8cb NS |
720 | while (bi->word_no != BITMAP_ELEMENT_WORDS) |
721 | { | |
722 | bi->bits = bi->elt1->bits[bi->word_no]; | |
723 | if (bi->elt2 && bi->elt2->indx == bi->elt1->indx) | |
724 | bi->bits &= ~bi->elt2->bits[bi->word_no]; | |
725 | if (bi->bits) | |
726 | goto next_bit; | |
727 | *bit_no += BITMAP_WORD_BITS; | |
728 | bi->word_no++; | |
729 | } | |
c22cacf3 | 730 | |
a30fe4b6 RB |
731 | /* Make sure we didn't remove the element while iterating. */ |
732 | gcc_checking_assert (bi->elt1->indx != -1U); | |
733 | ||
e90ea8cb NS |
734 | /* Advance to the next element of elt1. */ |
735 | bi->elt1 = bi->elt1->next; | |
736 | if (!bi->elt1) | |
737 | return false; | |
738 | ||
a30fe4b6 RB |
739 | /* Make sure we didn't remove the element while iterating. */ |
740 | gcc_checking_assert (! bi->elt2 || bi->elt2->indx != -1U); | |
741 | ||
e90ea8cb NS |
742 | /* Advance elt2 until it is no less than elt1. */ |
743 | while (bi->elt2 && bi->elt2->indx < bi->elt1->indx) | |
744 | bi->elt2 = bi->elt2->next; | |
c22cacf3 | 745 | |
e90ea8cb NS |
746 | *bit_no = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS; |
747 | bi->word_no = 0; | |
87c476a2 | 748 | } |
87c476a2 ZD |
749 | } |
750 | ||
7a18d752 RB |
751 | /* If you are modifying a bitmap you are currently iterating over you |
752 | have to ensure to | |
753 | - never remove the current bit; | |
754 | - if you set or clear a bit before the current bit this operation | |
755 | will not affect the set of bits you are visiting during the iteration; | |
756 | - if you set or clear a bit after the current bit it is unspecified | |
757 | whether that affects the set of bits you are visiting during the | |
758 | iteration. | |
759 | If you want to remove the current bit you can delay this to the next | |
760 | iteration (and after the iteration in case the last iteration is | |
761 | affected). */ | |
762 | ||
e90ea8cb NS |
763 | /* Loop over all bits set in BITMAP, starting with MIN and setting |
764 | BITNUM to the bit number. ITER is a bitmap iterator. BITNUM | |
765 | should be treated as a read-only variable as it contains loop | |
766 | state. */ | |
87c476a2 | 767 | |
d4ac4ce2 LC |
768 | #ifndef EXECUTE_IF_SET_IN_BITMAP |
769 | /* See sbitmap.h for the other definition of EXECUTE_IF_SET_IN_BITMAP. */ | |
e90ea8cb NS |
770 | #define EXECUTE_IF_SET_IN_BITMAP(BITMAP, MIN, BITNUM, ITER) \ |
771 | for (bmp_iter_set_init (&(ITER), (BITMAP), (MIN), &(BITNUM)); \ | |
772 | bmp_iter_set (&(ITER), &(BITNUM)); \ | |
773 | bmp_iter_next (&(ITER), &(BITNUM))) | |
d4ac4ce2 | 774 | #endif |
e90ea8cb NS |
775 | |
776 | /* Loop over all the bits set in BITMAP1 & BITMAP2, starting with MIN | |
777 | and setting BITNUM to the bit number. ITER is a bitmap iterator. | |
778 | BITNUM should be treated as a read-only variable as it contains | |
779 | loop state. */ | |
780 | ||
781 | #define EXECUTE_IF_AND_IN_BITMAP(BITMAP1, BITMAP2, MIN, BITNUM, ITER) \ | |
c22cacf3 | 782 | for (bmp_iter_and_init (&(ITER), (BITMAP1), (BITMAP2), (MIN), \ |
e90ea8cb NS |
783 | &(BITNUM)); \ |
784 | bmp_iter_and (&(ITER), &(BITNUM)); \ | |
785 | bmp_iter_next (&(ITER), &(BITNUM))) | |
786 | ||
787 | /* Loop over all the bits set in BITMAP1 & ~BITMAP2, starting with MIN | |
788 | and setting BITNUM to the bit number. ITER is a bitmap iterator. | |
789 | BITNUM should be treated as a read-only variable as it contains | |
790 | loop state. */ | |
791 | ||
792 | #define EXECUTE_IF_AND_COMPL_IN_BITMAP(BITMAP1, BITMAP2, MIN, BITNUM, ITER) \ | |
793 | for (bmp_iter_and_compl_init (&(ITER), (BITMAP1), (BITMAP2), (MIN), \ | |
c22cacf3 | 794 | &(BITNUM)); \ |
e90ea8cb NS |
795 | bmp_iter_and_compl (&(ITER), &(BITNUM)); \ |
796 | bmp_iter_next (&(ITER), &(BITNUM))) | |
a05924f9 | 797 | |
8b670f93 AH |
798 | /* A class that ties the lifetime of a bitmap to its scope. */ |
799 | class auto_bitmap | |
800 | { | |
801 | public: | |
a4d51bfb | 802 | auto_bitmap () { bitmap_initialize (&m_bits, &bitmap_default_obstack); } |
4b5c84f4 | 803 | explicit auto_bitmap (bitmap_obstack *o) { bitmap_initialize (&m_bits, o); } |
a4d51bfb | 804 | ~auto_bitmap () { bitmap_clear (&m_bits); } |
8b670f93 | 805 | // Allow calling bitmap functions on our bitmap. |
a4d51bfb | 806 | operator bitmap () { return &m_bits; } |
8b670f93 AH |
807 | |
808 | private: | |
809 | // Prevent making a copy that references our bitmap. | |
810 | auto_bitmap (const auto_bitmap &); | |
811 | auto_bitmap &operator = (const auto_bitmap &); | |
812 | #if __cplusplus >= 201103L | |
813 | auto_bitmap (auto_bitmap &&); | |
814 | auto_bitmap &operator = (auto_bitmap &&); | |
815 | #endif | |
816 | ||
a4d51bfb | 817 | bitmap_head m_bits; |
8b670f93 AH |
818 | }; |
819 | ||
88657302 | 820 | #endif /* GCC_BITMAP_H */ |