]>
Commit | Line | Data |
---|---|---|
096ab9ea | 1 | /* Functions to support general ended bitmaps. |
99dee823 | 2 | Copyright (C) 1997-2021 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 | 22 | |
d1e14d97 | 23 | /* Implementation of sparse integer sets as a linked list or tree. |
0263463d SB |
24 | |
25 | This sparse set representation is suitable for sparse sets with an | |
d1e14d97 SB |
26 | unknown (a priori) universe. |
27 | ||
28 | Sets are represented as double-linked lists of container nodes of | |
29 | type "struct bitmap_element" or as a binary trees of the same | |
30 | container nodes. Each container node consists of an index for the | |
31 | first member that could be held in the container, a small array of | |
32 | integers that represent the members in the container, and pointers | |
33 | to the next and previous element in the linked list, or left and | |
34 | right children in the tree. In linked-list form, the container | |
35 | nodes in the list are sorted in ascending order, i.e. the head of | |
0263463d | 36 | the list holds the element with the smallest member of the set. |
d1e14d97 | 37 | In tree form, nodes to the left have a smaller container index. |
0263463d SB |
38 | |
39 | For a given member I in the set: | |
40 | - the element for I will have index is I / (bits per element) | |
41 | - the position for I within element is I % (bits per element) | |
42 | ||
43 | This representation is very space-efficient for large sparse sets, and | |
44 | the size of the set can be changed dynamically without much overhead. | |
45 | An important parameter is the number of bits per element. In this | |
46 | implementation, there are 128 bits per element. This results in a | |
47 | high storage overhead *per element*, but a small overall overhead if | |
48 | the set is very sparse. | |
49 | ||
d1e14d97 SB |
50 | The storage requirements for linked-list sparse sets are O(E), with E->N |
51 | in the worst case (a sparse set with large distances between the values | |
52 | of the set members). | |
0263463d | 53 | |
d1e14d97 SB |
54 | This representation also works well for data flow problems where the size |
55 | of the set may grow dynamically, but care must be taken that the member_p, | |
56 | add_member, and remove_member operations occur with a suitable access | |
57 | pattern. | |
58 | ||
59 | The linked-list set representation works well for problems involving very | |
60 | sparse sets. The canonical example in GCC is, of course, the "set of | |
61 | sets" for some CFG-based data flow problems (liveness analysis, dominance | |
62 | frontiers, etc.). | |
63 | ||
64 | For random-access sparse sets of unknown universe, the binary tree | |
65 | representation is likely to be a more suitable choice. Theoretical | |
66 | access times for the binary tree representation are better than those | |
67 | for the linked-list, but in practice this is only true for truely | |
68 | random access. | |
69 | ||
70 | Often the most suitable representation during construction of the set | |
71 | is not the best choice for the usage of the set. For such cases, the | |
72 | "view" of the set can be changed from one representation to the other. | |
73 | This is an O(E) operation: | |
74 | ||
75 | * from list to tree view : bitmap_tree_view | |
76 | * from tree to list view : bitmap_list_view | |
77 | ||
78 | Traversing linked lists or trees can be cache-unfriendly. Performance | |
79 | can be improved by keeping container nodes in the set grouped together | |
80 | in memory, using a dedicated obstack for a set (or group of related | |
81 | sets). Elements allocated on obstacks are released to a free-list and | |
82 | taken off the free list. If multiple sets are allocated on the same | |
83 | obstack, elements freed from one set may be re-used for one of the other | |
84 | sets. This usually helps avoid cache misses. | |
85 | ||
86 | A single free-list is used for all sets allocated in GGC space. This is | |
87 | bad for persistent sets, so persistent sets should be allocated on an | |
88 | obstack whenever possible. | |
89 | ||
90 | For random-access sets with a known, relatively small universe size, the | |
91 | SparseSet or simple bitmap representations may be more efficient than a | |
92 | linked-list set. | |
93 | ||
94 | ||
95 | LINKED LIST FORM | |
96 | ================ | |
97 | ||
98 | In linked-list form, in-order iterations of the set can be executed | |
99 | efficiently. The downside is that many random-access operations are | |
100 | relatively slow, because the linked list has to be traversed to test | |
101 | membership (i.e. member_p/ add_member/remove_member). | |
102 | ||
103 | To improve the performance of this set representation, the last | |
104 | accessed element and its index are cached. For membership tests on | |
105 | members close to recently accessed members, the cached last element | |
106 | improves membership test to a constant-time operation. | |
107 | ||
108 | The following operations can always be performed in O(1) time in | |
109 | list view: | |
0263463d SB |
110 | |
111 | * clear : bitmap_clear | |
d1e14d97 | 112 | * smallest_member : bitmap_first_set_bit |
0263463d | 113 | * choose_one : (not implemented, but could be |
d1e14d97 | 114 | in constant time) |
0263463d | 115 | |
d1e14d97 SB |
116 | The following operations can be performed in O(E) time worst-case in |
117 | list view (with E the number of elements in the linked list), but in | |
118 | O(1) time with a suitable access patterns: | |
0263463d SB |
119 | |
120 | * member_p : bitmap_bit_p | |
d1e14d97 SB |
121 | * add_member : bitmap_set_bit / bitmap_set_range |
122 | * remove_member : bitmap_clear_bit / bitmap_clear_range | |
0263463d | 123 | |
d1e14d97 | 124 | The following operations can be performed in O(E) time in list view: |
0263463d SB |
125 | |
126 | * cardinality : bitmap_count_bits | |
d1e14d97 | 127 | * largest_member : bitmap_last_set_bit (but this could |
0263463d SB |
128 | in constant time with a pointer to |
129 | the last element in the chain) | |
d1e14d97 SB |
130 | * set_size : bitmap_last_set_bit |
131 | ||
132 | In tree view the following operations can all be performed in O(log E) | |
133 | amortized time with O(E) worst-case behavior. | |
134 | ||
135 | * smallest_member | |
136 | * largest_member | |
137 | * set_size | |
138 | * member_p | |
139 | * add_member | |
140 | * remove_member | |
0263463d SB |
141 | |
142 | Additionally, the linked-list sparse set representation supports | |
143 | enumeration of the members in O(E) time: | |
144 | ||
145 | * forall : EXECUTE_IF_SET_IN_BITMAP | |
146 | * set_copy : bitmap_copy | |
147 | * set_intersection : bitmap_intersect_p / | |
148 | bitmap_and / bitmap_and_into / | |
149 | EXECUTE_IF_AND_IN_BITMAP | |
150 | * set_union : bitmap_ior / bitmap_ior_into | |
151 | * set_difference : bitmap_intersect_compl_p / | |
152 | bitmap_and_comp / bitmap_and_comp_into / | |
153 | EXECUTE_IF_AND_COMPL_IN_BITMAP | |
154 | * set_disjuction : bitmap_xor_comp / bitmap_xor_comp_into | |
155 | * set_compare : bitmap_equal_p | |
156 | ||
026c3cfd | 157 | Some operations on 3 sets that occur frequently in data flow problems |
0263463d SB |
158 | are also implemented: |
159 | ||
160 | * A | (B & C) : bitmap_ior_and_into | |
161 | * A | (B & ~C) : bitmap_ior_and_compl / | |
162 | bitmap_ior_and_compl_into | |
163 | ||
0263463d | 164 | |
d1e14d97 SB |
165 | BINARY TREE FORM |
166 | ================ | |
167 | An alternate "view" of a bitmap is its binary tree representation. | |
168 | For this representation, splay trees are used because they can be | |
169 | implemented using the same data structures as the linked list, with | |
170 | no overhead for meta-data (like color, or rank) on the tree nodes. | |
0263463d | 171 | |
d1e14d97 SB |
172 | In binary tree form, random-access to the set is much more efficient |
173 | than for the linked-list representation. Downsides are the high cost | |
174 | of clearing the set, and the relatively large number of operations | |
175 | necessary to balance the tree. Also, iterating the set members is | |
176 | not supported. | |
0263463d | 177 | |
d1e14d97 SB |
178 | As for the linked-list representation, the last accessed element and |
179 | its index are cached, so that membership tests on the latest accessed | |
180 | members is a constant-time operation. Other lookups take O(logE) | |
181 | time amortized (but O(E) time worst-case). | |
0263463d | 182 | |
d1e14d97 SB |
183 | The following operations can always be performed in O(1) time: |
184 | ||
185 | * choose_one : (not implemented, but could be | |
186 | implemented in constant time) | |
187 | ||
188 | The following operations can be performed in O(logE) time amortized | |
189 | but O(E) time worst-case, but in O(1) time if the same element is | |
190 | accessed. | |
191 | ||
192 | * member_p : bitmap_bit_p | |
193 | * add_member : bitmap_set_bit | |
194 | * remove_member : bitmap_clear_bit | |
195 | ||
196 | The following operations can be performed in O(logE) time amortized | |
197 | but O(E) time worst-case: | |
198 | ||
199 | * smallest_member : bitmap_first_set_bit | |
200 | * largest_member : bitmap_last_set_bit | |
201 | * set_size : bitmap_last_set_bit | |
202 | ||
203 | The following operations can be performed in O(E) time: | |
204 | ||
205 | * clear : bitmap_clear | |
206 | ||
207 | The binary tree sparse set representation does *not* support any form | |
208 | of enumeration, and does also *not* support logical operations on sets. | |
209 | The binary tree representation is only supposed to be used for sets | |
210 | on which many random-access membership tests will happen. */ | |
0263463d | 211 | |
b60db1ba | 212 | #include "obstack.h" |
148909bc | 213 | #include "array-traits.h" |
2d44c7de ML |
214 | |
215 | /* Bitmap memory usage. */ | |
6c1dae73 | 216 | class bitmap_usage: public mem_usage |
2d44c7de | 217 | { |
6c1dae73 | 218 | public: |
2d44c7de ML |
219 | /* Default contructor. */ |
220 | bitmap_usage (): m_nsearches (0), m_search_iter (0) {} | |
221 | /* Constructor. */ | |
222 | bitmap_usage (size_t allocated, size_t times, size_t peak, | |
223 | uint64_t nsearches, uint64_t search_iter) | |
224 | : mem_usage (allocated, times, peak), | |
225 | m_nsearches (nsearches), m_search_iter (search_iter) {} | |
226 | ||
227 | /* Sum the usage with SECOND usage. */ | |
80a4fe78 ML |
228 | bitmap_usage |
229 | operator+ (const bitmap_usage &second) | |
2d44c7de ML |
230 | { |
231 | return bitmap_usage (m_allocated + second.m_allocated, | |
232 | m_times + second.m_times, | |
233 | m_peak + second.m_peak, | |
234 | m_nsearches + second.m_nsearches, | |
235 | m_search_iter + second.m_search_iter); | |
236 | } | |
237 | ||
238 | /* Dump usage coupled to LOC location, where TOTAL is sum of all rows. */ | |
80a4fe78 | 239 | inline void |
d73d45f1 | 240 | dump (mem_location *loc, const mem_usage &total) const |
2d44c7de | 241 | { |
ac059261 | 242 | char *location_string = loc->to_string (); |
2d44c7de | 243 | |
a0b48080 MM |
244 | fprintf (stderr, "%-48s " PRsa (9) ":%5.1f%%" |
245 | PRsa (9) PRsa (9) ":%5.1f%%" | |
246 | PRsa (11) PRsa (11) "%10s\n", | |
40ce7fa6 | 247 | location_string, SIZE_AMOUNT (m_allocated), |
43331dfb | 248 | get_percent (m_allocated, total.m_allocated), |
40ce7fa6 | 249 | SIZE_AMOUNT (m_peak), SIZE_AMOUNT (m_times), |
2d44c7de | 250 | get_percent (m_times, total.m_times), |
40ce7fa6 | 251 | SIZE_AMOUNT (m_nsearches), SIZE_AMOUNT (m_search_iter), |
2d44c7de | 252 | loc->m_ggc ? "ggc" : "heap"); |
ac059261 ML |
253 | |
254 | free (location_string); | |
2d44c7de ML |
255 | } |
256 | ||
257 | /* Dump header with NAME. */ | |
80a4fe78 ML |
258 | static inline void |
259 | dump_header (const char *name) | |
2d44c7de ML |
260 | { |
261 | fprintf (stderr, "%-48s %11s%16s%17s%12s%12s%10s\n", name, "Leak", "Peak", | |
262 | "Times", "N searches", "Search iter", "Type"); | |
2d44c7de ML |
263 | } |
264 | ||
265 | /* Number search operations. */ | |
266 | uint64_t m_nsearches; | |
267 | /* Number of search iterations. */ | |
268 | uint64_t m_search_iter; | |
269 | }; | |
270 | ||
271 | /* Bitmap memory description. */ | |
272 | extern mem_alloc_description<bitmap_usage> bitmap_mem_desc; | |
a05924f9 | 273 | |
72e42e26 SB |
274 | /* Fundamental storage type for bitmap. */ |
275 | ||
72e42e26 | 276 | typedef unsigned long BITMAP_WORD; |
65a6f342 NS |
277 | /* BITMAP_WORD_BITS needs to be unsigned, but cannot contain casts as |
278 | it is used in preprocessor directives -- hence the 1u. */ | |
279 | #define BITMAP_WORD_BITS (CHAR_BIT * SIZEOF_LONG * 1u) | |
72e42e26 | 280 | |
096ab9ea RK |
281 | /* Number of words to use for each element in the linked list. */ |
282 | ||
283 | #ifndef BITMAP_ELEMENT_WORDS | |
65a6f342 | 284 | #define BITMAP_ELEMENT_WORDS ((128 + BITMAP_WORD_BITS - 1) / BITMAP_WORD_BITS) |
096ab9ea RK |
285 | #endif |
286 | ||
65a6f342 | 287 | /* Number of bits in each actual element of a bitmap. */ |
096ab9ea | 288 | |
65a6f342 | 289 | #define BITMAP_ELEMENT_ALL_BITS (BITMAP_ELEMENT_WORDS * BITMAP_WORD_BITS) |
096ab9ea | 290 | |
7932a3db | 291 | /* Obstack for allocating bitmaps and elements from. */ |
7eeb6fc2 | 292 | struct bitmap_obstack { |
84562394 | 293 | struct bitmap_element *elements; |
99b1c316 | 294 | bitmap_head *heads; |
7eeb6fc2 | 295 | struct obstack obstack; |
84562394 | 296 | }; |
7932a3db | 297 | |
096ab9ea RK |
298 | /* Bitmap set element. We use a linked list to hold only the bits that |
299 | are set. This allows for use to grow the bitset dynamically without | |
c22cacf3 | 300 | having to realloc and copy a giant bit array. |
5765e552 KZ |
301 | |
302 | The free list is implemented as a list of lists. There is one | |
303 | outer list connected together by prev fields. Each element of that | |
304 | outer is an inner list (that may consist only of the outer list | |
305 | element) that are connected by the next fields. The prev pointer | |
306 | is undefined for interior elements. This allows | |
307 | bitmap_elt_clear_from to be implemented in unit time rather than | |
308 | linear in the number of elements to be freed. */ | |
096ab9ea | 309 | |
7eeb6fc2 | 310 | struct GTY((chain_next ("%h.next"))) bitmap_element { |
d1e14d97 SB |
311 | /* In list form, the next element in the linked list; |
312 | in tree form, the left child node in the tree. */ | |
313 | struct bitmap_element *next; | |
314 | /* In list form, the previous element in the linked list; | |
315 | in tree form, the right child node in the tree. */ | |
316 | struct bitmap_element *prev; | |
317 | /* regno/BITMAP_ELEMENT_ALL_BITS. */ | |
318 | unsigned int indx; | |
319 | /* Bits that are set, counting from INDX, inclusive */ | |
320 | BITMAP_WORD bits[BITMAP_ELEMENT_WORDS]; | |
84562394 | 321 | }; |
096ab9ea | 322 | |
3c53f55a SB |
323 | /* Head of bitmap linked list. The 'current' member points to something |
324 | already pointed to by the chain started by first, so GTY((skip)) it. */ | |
01d419ae | 325 | |
6c1dae73 MS |
326 | class GTY(()) bitmap_head { |
327 | public: | |
1c252ef3 RB |
328 | static bitmap_obstack crashme; |
329 | /* Poison obstack to not make it not a valid initialized GC bitmap. */ | |
330 | CONSTEXPR bitmap_head() | |
7664eeb7 ML |
331 | : indx (0), tree_form (false), padding (0), alloc_descriptor (0), first (NULL), |
332 | current (NULL), obstack (&crashme) | |
1c252ef3 | 333 | {} |
d1e14d97 SB |
334 | /* Index of last element looked at. */ |
335 | unsigned int indx; | |
336 | /* False if the bitmap is in list form; true if the bitmap is in tree form. | |
337 | Bitmap iterators only work on bitmaps in list form. */ | |
7664eeb7 ML |
338 | unsigned tree_form: 1; |
339 | /* Next integer is shifted, so padding is needed. */ | |
340 | unsigned padding: 2; | |
341 | /* Bitmap UID used for memory allocation statistics. */ | |
342 | unsigned alloc_descriptor: 29; | |
d1e14d97 SB |
343 | /* In list form, the first element in the linked list; |
344 | in tree form, the root of the tree. */ | |
345 | bitmap_element *first; | |
346 | /* Last element looked at. */ | |
347 | bitmap_element * GTY((skip(""))) current; | |
348 | /* Obstack to allocate elements from. If NULL, then use GGC allocation. */ | |
7eeb6fc2 | 349 | bitmap_obstack * GTY((skip(""))) obstack; |
7664eeb7 ML |
350 | |
351 | /* Dump bitmap. */ | |
54994253 | 352 | void dump (); |
7664eeb7 ML |
353 | |
354 | /* Get bitmap descriptor UID casted to an unsigned integer pointer. | |
355 | Shift the descriptor because pointer_hash<Type>::hash is | |
356 | doing >> 3 shift operation. */ | |
357 | unsigned *get_descriptor () | |
358 | { | |
359 | return (unsigned *)(ptrdiff_t)(alloc_descriptor << 3); | |
360 | } | |
84562394 | 361 | }; |
7932a3db | 362 | |
096ab9ea | 363 | /* Global data */ |
ae0ed63a | 364 | extern bitmap_element bitmap_zero_bits; /* Zero bitmap element */ |
7932a3db | 365 | extern bitmap_obstack bitmap_default_obstack; /* Default bitmap obstack */ |
096ab9ea | 366 | |
d1e14d97 SB |
367 | /* Change the view of the bitmap to list, or tree. */ |
368 | void bitmap_list_view (bitmap); | |
369 | void bitmap_tree_view (bitmap); | |
370 | ||
096ab9ea | 371 | /* Clear a bitmap by freeing up the linked list. */ |
4682ae04 | 372 | extern void bitmap_clear (bitmap); |
096ab9ea | 373 | |
eebedaa5 | 374 | /* Copy a bitmap to another bitmap. */ |
e326eeb5 | 375 | extern void bitmap_copy (bitmap, const_bitmap); |
096ab9ea | 376 | |
43331dfb RB |
377 | /* Move a bitmap to another bitmap. */ |
378 | extern void bitmap_move (bitmap, bitmap); | |
379 | ||
8229306b | 380 | /* True if two bitmaps are identical. */ |
e326eeb5 | 381 | extern bool bitmap_equal_p (const_bitmap, const_bitmap); |
8229306b | 382 | |
55994078 | 383 | /* True if the bitmaps intersect (their AND is non-empty). */ |
e326eeb5 | 384 | extern bool bitmap_intersect_p (const_bitmap, const_bitmap); |
55994078 NS |
385 | |
386 | /* True if the complement of the second intersects the first (their | |
387 | AND_COMPL is non-empty). */ | |
e326eeb5 | 388 | extern bool bitmap_intersect_compl_p (const_bitmap, const_bitmap); |
55994078 NS |
389 | |
390 | /* True if MAP is an empty bitmap. */ | |
f61e445a LC |
391 | inline bool bitmap_empty_p (const_bitmap map) |
392 | { | |
393 | return !map->first; | |
394 | } | |
eb59b8de | 395 | |
76e910c6 RG |
396 | /* True if the bitmap has only a single bit set. */ |
397 | extern bool bitmap_single_bit_set_p (const_bitmap); | |
398 | ||
1bc40c7e | 399 | /* Count the number of bits set in the bitmap. */ |
e326eeb5 | 400 | extern unsigned long bitmap_count_bits (const_bitmap); |
1bc40c7e | 401 | |
478baf91 JL |
402 | /* Count the number of unique bits set across the two bitmaps. */ |
403 | extern unsigned long bitmap_count_unique_bits (const_bitmap, const_bitmap); | |
404 | ||
88c4f655 NS |
405 | /* Boolean operations on bitmaps. The _into variants are two operand |
406 | versions that modify the first source operand. The other variants | |
407 | are three operand versions that to not destroy the source bitmaps. | |
408 | The operations supported are &, & ~, |, ^. */ | |
e326eeb5 | 409 | extern void bitmap_and (bitmap, const_bitmap, const_bitmap); |
7b19209f | 410 | extern bool bitmap_and_into (bitmap, const_bitmap); |
e326eeb5 KG |
411 | extern bool bitmap_and_compl (bitmap, const_bitmap, const_bitmap); |
412 | extern bool bitmap_and_compl_into (bitmap, const_bitmap); | |
1bc40c7e | 413 | #define bitmap_compl_and(DST, A, B) bitmap_and_compl (DST, B, A) |
e326eeb5 | 414 | extern void bitmap_compl_and_into (bitmap, const_bitmap); |
1bc40c7e | 415 | extern void bitmap_clear_range (bitmap, unsigned int, unsigned int); |
6fb5fa3c | 416 | extern void bitmap_set_range (bitmap, unsigned int, unsigned int); |
e326eeb5 KG |
417 | extern bool bitmap_ior (bitmap, const_bitmap, const_bitmap); |
418 | extern bool bitmap_ior_into (bitmap, const_bitmap); | |
029ca388 | 419 | extern bool bitmap_ior_into_and_free (bitmap, bitmap *); |
e326eeb5 KG |
420 | extern void bitmap_xor (bitmap, const_bitmap, const_bitmap); |
421 | extern void bitmap_xor_into (bitmap, const_bitmap); | |
88c4f655 | 422 | |
7ff23740 PB |
423 | /* DST = A | (B & C). Return true if DST changes. */ |
424 | extern bool bitmap_ior_and_into (bitmap DST, const_bitmap B, const_bitmap C); | |
88c4f655 | 425 | /* DST = A | (B & ~C). Return true if DST changes. */ |
0263463d SB |
426 | extern bool bitmap_ior_and_compl (bitmap DST, const_bitmap A, |
427 | const_bitmap B, const_bitmap C); | |
88c4f655 | 428 | /* A |= (B & ~C). Return true if A changes. */ |
0263463d SB |
429 | extern bool bitmap_ior_and_compl_into (bitmap A, |
430 | const_bitmap B, const_bitmap C); | |
096ab9ea | 431 | |
5f0d975b RG |
432 | /* Clear a single bit in a bitmap. Return true if the bit changed. */ |
433 | extern bool bitmap_clear_bit (bitmap, int); | |
096ab9ea | 434 | |
5f0d975b RG |
435 | /* Set a single bit in a bitmap. Return true if the bit changed. */ |
436 | extern bool bitmap_set_bit (bitmap, int); | |
096ab9ea | 437 | |
d1e14d97 | 438 | /* Return true if a bit is set in a bitmap. */ |
148909bc | 439 | extern int bitmap_bit_p (const_bitmap, int); |
096ab9ea | 440 | |
5ad089a3 AM |
441 | /* Set and get multiple bit values in a sparse bitmap. This allows a bitmap to |
442 | function as a sparse array of bit patterns where the patterns are | |
443 | multiples of power of 2. This is more efficient than performing this as | |
444 | multiple individual operations. */ | |
445 | void bitmap_set_aligned_chunk (bitmap, unsigned int, unsigned int, BITMAP_WORD); | |
446 | BITMAP_WORD bitmap_get_aligned_chunk (const_bitmap, unsigned int, unsigned int); | |
447 | ||
d1e14d97 | 448 | /* Debug functions to print a bitmap. */ |
e326eeb5 KG |
449 | extern void debug_bitmap (const_bitmap); |
450 | extern void debug_bitmap_file (FILE *, const_bitmap); | |
096ab9ea | 451 | |
f9da5064 | 452 | /* Print a bitmap. */ |
e326eeb5 | 453 | extern void bitmap_print (FILE *, const_bitmap, const char *, const char *); |
22fa5b8a | 454 | |
5765e552 | 455 | /* Initialize and release a bitmap obstack. */ |
7932a3db NS |
456 | extern void bitmap_obstack_initialize (bitmap_obstack *); |
457 | extern void bitmap_obstack_release (bitmap_obstack *); | |
f75709c6 JH |
458 | extern void bitmap_register (bitmap MEM_STAT_DECL); |
459 | extern void dump_bitmap_statistics (void); | |
096ab9ea | 460 | |
7932a3db NS |
461 | /* Initialize a bitmap header. OBSTACK indicates the bitmap obstack |
462 | to allocate from, NULL for GC'd bitmap. */ | |
463 | ||
464 | static inline void | |
2a1a5f30 | 465 | bitmap_initialize (bitmap head, bitmap_obstack *obstack CXX_MEM_STAT_INFO) |
7932a3db NS |
466 | { |
467 | head->first = head->current = NULL; | |
d1e14d97 | 468 | head->indx = head->tree_form = 0; |
7664eeb7 ML |
469 | head->padding = 0; |
470 | head->alloc_descriptor = 0; | |
7932a3db | 471 | head->obstack = obstack; |
7aa6d18a SB |
472 | if (GATHER_STATISTICS) |
473 | bitmap_register (head PASS_MEM_STAT); | |
7932a3db NS |
474 | } |
475 | ||
1c252ef3 RB |
476 | /* Release a bitmap (but not its head). This is suitable for pairing with |
477 | bitmap_initialize. */ | |
478 | ||
479 | static inline void | |
480 | bitmap_release (bitmap head) | |
481 | { | |
482 | bitmap_clear (head); | |
483 | /* Poison the obstack pointer so the obstack can be safely released. | |
484 | Do not zero it as the bitmap then becomes initialized GC. */ | |
485 | head->obstack = &bitmap_head::crashme; | |
486 | } | |
487 | ||
7932a3db | 488 | /* Allocate and free bitmaps from obstack, malloc and gc'd memory. */ |
3fe793df TS |
489 | extern bitmap bitmap_alloc (bitmap_obstack *obstack CXX_MEM_STAT_INFO); |
490 | #define BITMAP_ALLOC bitmap_alloc | |
491 | extern bitmap bitmap_gc_alloc (ALONE_CXX_MEM_STAT_INFO); | |
492 | #define BITMAP_GGC_ALLOC bitmap_gc_alloc | |
7932a3db | 493 | extern void bitmap_obstack_free (bitmap); |
096ab9ea | 494 | |
ea193996 | 495 | /* A few compatibility/functions macros for compatibility with sbitmaps */ |
f61e445a LC |
496 | inline void dump_bitmap (FILE *file, const_bitmap map) |
497 | { | |
498 | bitmap_print (file, map, "", "\n"); | |
499 | } | |
84562394 OE |
500 | extern void debug (const bitmap_head &ref); |
501 | extern void debug (const bitmap_head *ptr); | |
f61e445a | 502 | |
e326eeb5 | 503 | extern unsigned bitmap_first_set_bit (const_bitmap); |
12802c2b | 504 | extern unsigned bitmap_last_set_bit (const_bitmap); |
ea193996 | 505 | |
1af4bba8 | 506 | /* Compute bitmap hash (for purposes of hashing etc.) */ |
c3284718 | 507 | extern hashval_t bitmap_hash (const_bitmap); |
1af4bba8 | 508 | |
096ab9ea | 509 | /* Do any cleanup needed on a bitmap when it is no longer used. */ |
61ad0914 BE |
510 | #define BITMAP_FREE(BITMAP) \ |
511 | ((void) (bitmap_obstack_free ((bitmap) BITMAP), (BITMAP) = (bitmap) NULL)) | |
e7749837 | 512 | |
87c476a2 | 513 | /* Iterator for bitmaps. */ |
096ab9ea | 514 | |
84562394 | 515 | struct bitmap_iterator |
87c476a2 | 516 | { |
e90ea8cb NS |
517 | /* Pointer to the current bitmap element. */ |
518 | bitmap_element *elt1; | |
c22cacf3 | 519 | |
e90ea8cb NS |
520 | /* Pointer to 2nd bitmap element when two are involved. */ |
521 | bitmap_element *elt2; | |
522 | ||
523 | /* Word within the current element. */ | |
524 | unsigned word_no; | |
c22cacf3 | 525 | |
87c476a2 ZD |
526 | /* Contents of the actually processed word. When finding next bit |
527 | it is shifted right, so that the actual bit is always the least | |
528 | significant bit of ACTUAL. */ | |
e90ea8cb | 529 | BITMAP_WORD bits; |
84562394 | 530 | }; |
87c476a2 | 531 | |
e90ea8cb NS |
532 | /* Initialize a single bitmap iterator. START_BIT is the first bit to |
533 | iterate from. */ | |
87c476a2 | 534 | |
e90ea8cb | 535 | static inline void |
e326eeb5 | 536 | bmp_iter_set_init (bitmap_iterator *bi, const_bitmap map, |
e90ea8cb | 537 | unsigned start_bit, unsigned *bit_no) |
87c476a2 | 538 | { |
e90ea8cb NS |
539 | bi->elt1 = map->first; |
540 | bi->elt2 = NULL; | |
541 | ||
d1e14d97 SB |
542 | gcc_checking_assert (!map->tree_form); |
543 | ||
e90ea8cb NS |
544 | /* Advance elt1 until it is not before the block containing start_bit. */ |
545 | while (1) | |
87c476a2 | 546 | { |
e90ea8cb NS |
547 | if (!bi->elt1) |
548 | { | |
549 | bi->elt1 = &bitmap_zero_bits; | |
550 | break; | |
551 | } | |
c22cacf3 | 552 | |
e90ea8cb NS |
553 | if (bi->elt1->indx >= start_bit / BITMAP_ELEMENT_ALL_BITS) |
554 | break; | |
555 | bi->elt1 = bi->elt1->next; | |
87c476a2 ZD |
556 | } |
557 | ||
e90ea8cb NS |
558 | /* We might have gone past the start bit, so reinitialize it. */ |
559 | if (bi->elt1->indx != start_bit / BITMAP_ELEMENT_ALL_BITS) | |
560 | start_bit = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS; | |
c22cacf3 | 561 | |
e90ea8cb NS |
562 | /* Initialize for what is now start_bit. */ |
563 | bi->word_no = start_bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS; | |
564 | bi->bits = bi->elt1->bits[bi->word_no]; | |
565 | bi->bits >>= start_bit % BITMAP_WORD_BITS; | |
566 | ||
567 | /* If this word is zero, we must make sure we're not pointing at the | |
568 | first bit, otherwise our incrementing to the next word boundary | |
569 | will fail. It won't matter if this increment moves us into the | |
570 | next word. */ | |
571 | start_bit += !bi->bits; | |
c22cacf3 | 572 | |
e90ea8cb | 573 | *bit_no = start_bit; |
87c476a2 ZD |
574 | } |
575 | ||
e90ea8cb NS |
576 | /* Initialize an iterator to iterate over the intersection of two |
577 | bitmaps. START_BIT is the bit to commence from. */ | |
87c476a2 | 578 | |
e90ea8cb | 579 | static inline void |
e326eeb5 | 580 | bmp_iter_and_init (bitmap_iterator *bi, const_bitmap map1, const_bitmap map2, |
e90ea8cb | 581 | unsigned start_bit, unsigned *bit_no) |
87c476a2 | 582 | { |
e90ea8cb NS |
583 | bi->elt1 = map1->first; |
584 | bi->elt2 = map2->first; | |
87c476a2 | 585 | |
d1e14d97 SB |
586 | gcc_checking_assert (!map1->tree_form && !map2->tree_form); |
587 | ||
e90ea8cb NS |
588 | /* Advance elt1 until it is not before the block containing |
589 | start_bit. */ | |
87c476a2 ZD |
590 | while (1) |
591 | { | |
e90ea8cb | 592 | if (!bi->elt1) |
87c476a2 | 593 | { |
e90ea8cb NS |
594 | bi->elt2 = NULL; |
595 | break; | |
87c476a2 | 596 | } |
c22cacf3 | 597 | |
e90ea8cb NS |
598 | if (bi->elt1->indx >= start_bit / BITMAP_ELEMENT_ALL_BITS) |
599 | break; | |
600 | bi->elt1 = bi->elt1->next; | |
87c476a2 | 601 | } |
c22cacf3 | 602 | |
e90ea8cb NS |
603 | /* Advance elt2 until it is not before elt1. */ |
604 | while (1) | |
87c476a2 | 605 | { |
e90ea8cb NS |
606 | if (!bi->elt2) |
607 | { | |
608 | bi->elt1 = bi->elt2 = &bitmap_zero_bits; | |
609 | break; | |
610 | } | |
c22cacf3 | 611 | |
e90ea8cb NS |
612 | if (bi->elt2->indx >= bi->elt1->indx) |
613 | break; | |
614 | bi->elt2 = bi->elt2->next; | |
87c476a2 ZD |
615 | } |
616 | ||
e28d0cfb | 617 | /* If we're at the same index, then we have some intersecting bits. */ |
e90ea8cb | 618 | if (bi->elt1->indx == bi->elt2->indx) |
87c476a2 | 619 | { |
e90ea8cb | 620 | /* We might have advanced beyond the start_bit, so reinitialize |
c22cacf3 | 621 | for that. */ |
e90ea8cb NS |
622 | if (bi->elt1->indx != start_bit / BITMAP_ELEMENT_ALL_BITS) |
623 | start_bit = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS; | |
c22cacf3 | 624 | |
e90ea8cb NS |
625 | bi->word_no = start_bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS; |
626 | bi->bits = bi->elt1->bits[bi->word_no] & bi->elt2->bits[bi->word_no]; | |
627 | bi->bits >>= start_bit % BITMAP_WORD_BITS; | |
87c476a2 ZD |
628 | } |
629 | else | |
630 | { | |
e90ea8cb NS |
631 | /* Otherwise we must immediately advance elt1, so initialize for |
632 | that. */ | |
633 | bi->word_no = BITMAP_ELEMENT_WORDS - 1; | |
634 | bi->bits = 0; | |
87c476a2 | 635 | } |
c22cacf3 | 636 | |
e90ea8cb NS |
637 | /* If this word is zero, we must make sure we're not pointing at the |
638 | first bit, otherwise our incrementing to the next word boundary | |
639 | will fail. It won't matter if this increment moves us into the | |
640 | next word. */ | |
641 | start_bit += !bi->bits; | |
c22cacf3 | 642 | |
e90ea8cb | 643 | *bit_no = start_bit; |
87c476a2 ZD |
644 | } |
645 | ||
d1e14d97 | 646 | /* Initialize an iterator to iterate over the bits in MAP1 & ~MAP2. */ |
87c476a2 | 647 | |
e90ea8cb | 648 | static inline void |
0263463d SB |
649 | bmp_iter_and_compl_init (bitmap_iterator *bi, |
650 | const_bitmap map1, const_bitmap map2, | |
e90ea8cb | 651 | unsigned start_bit, unsigned *bit_no) |
87c476a2 | 652 | { |
e90ea8cb NS |
653 | bi->elt1 = map1->first; |
654 | bi->elt2 = map2->first; | |
87c476a2 | 655 | |
d1e14d97 SB |
656 | gcc_checking_assert (!map1->tree_form && !map2->tree_form); |
657 | ||
e90ea8cb | 658 | /* Advance elt1 until it is not before the block containing start_bit. */ |
87c476a2 ZD |
659 | while (1) |
660 | { | |
e90ea8cb | 661 | if (!bi->elt1) |
87c476a2 | 662 | { |
e90ea8cb NS |
663 | bi->elt1 = &bitmap_zero_bits; |
664 | break; | |
87c476a2 | 665 | } |
c22cacf3 | 666 | |
e90ea8cb NS |
667 | if (bi->elt1->indx >= start_bit / BITMAP_ELEMENT_ALL_BITS) |
668 | break; | |
669 | bi->elt1 = bi->elt1->next; | |
87c476a2 | 670 | } |
e90ea8cb NS |
671 | |
672 | /* Advance elt2 until it is not before elt1. */ | |
673 | while (bi->elt2 && bi->elt2->indx < bi->elt1->indx) | |
674 | bi->elt2 = bi->elt2->next; | |
675 | ||
676 | /* We might have advanced beyond the start_bit, so reinitialize for | |
677 | that. */ | |
678 | if (bi->elt1->indx != start_bit / BITMAP_ELEMENT_ALL_BITS) | |
679 | start_bit = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS; | |
c22cacf3 | 680 | |
e90ea8cb NS |
681 | bi->word_no = start_bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS; |
682 | bi->bits = bi->elt1->bits[bi->word_no]; | |
683 | if (bi->elt2 && bi->elt1->indx == bi->elt2->indx) | |
684 | bi->bits &= ~bi->elt2->bits[bi->word_no]; | |
685 | bi->bits >>= start_bit % BITMAP_WORD_BITS; | |
c22cacf3 | 686 | |
e90ea8cb NS |
687 | /* If this word is zero, we must make sure we're not pointing at the |
688 | first bit, otherwise our incrementing to the next word boundary | |
689 | will fail. It won't matter if this increment moves us into the | |
690 | next word. */ | |
691 | start_bit += !bi->bits; | |
c22cacf3 | 692 | |
e90ea8cb | 693 | *bit_no = start_bit; |
87c476a2 ZD |
694 | } |
695 | ||
e90ea8cb | 696 | /* Advance to the next bit in BI. We don't advance to the next |
d46aed51 | 697 | nonzero bit yet. */ |
87c476a2 | 698 | |
e90ea8cb NS |
699 | static inline void |
700 | bmp_iter_next (bitmap_iterator *bi, unsigned *bit_no) | |
87c476a2 | 701 | { |
e90ea8cb NS |
702 | bi->bits >>= 1; |
703 | *bit_no += 1; | |
704 | } | |
87c476a2 | 705 | |
d5568f03 JH |
706 | /* Advance to first set bit in BI. */ |
707 | ||
708 | static inline void | |
709 | bmp_iter_next_bit (bitmap_iterator * bi, unsigned *bit_no) | |
710 | { | |
711 | #if (GCC_VERSION >= 3004) | |
712 | { | |
713 | unsigned int n = __builtin_ctzl (bi->bits); | |
714 | gcc_assert (sizeof (unsigned long) == sizeof (BITMAP_WORD)); | |
715 | bi->bits >>= n; | |
716 | *bit_no += n; | |
717 | } | |
718 | #else | |
719 | while (!(bi->bits & 1)) | |
720 | { | |
721 | bi->bits >>= 1; | |
722 | *bit_no += 1; | |
723 | } | |
724 | #endif | |
725 | } | |
726 | ||
d46aed51 | 727 | /* Advance to the next nonzero bit of a single bitmap, we will have |
e90ea8cb NS |
728 | already advanced past the just iterated bit. Return true if there |
729 | is a bit to iterate. */ | |
87c476a2 | 730 | |
e90ea8cb NS |
731 | static inline bool |
732 | bmp_iter_set (bitmap_iterator *bi, unsigned *bit_no) | |
733 | { | |
d46aed51 | 734 | /* If our current word is nonzero, it contains the bit we want. */ |
e90ea8cb | 735 | if (bi->bits) |
87c476a2 | 736 | { |
e90ea8cb | 737 | next_bit: |
d5568f03 | 738 | bmp_iter_next_bit (bi, bit_no); |
e90ea8cb | 739 | return true; |
87c476a2 ZD |
740 | } |
741 | ||
e90ea8cb NS |
742 | /* Round up to the word boundary. We might have just iterated past |
743 | the end of the last word, hence the -1. It is not possible for | |
744 | bit_no to point at the beginning of the now last word. */ | |
745 | *bit_no = ((*bit_no + BITMAP_WORD_BITS - 1) | |
746 | / BITMAP_WORD_BITS * BITMAP_WORD_BITS); | |
747 | bi->word_no++; | |
87c476a2 | 748 | |
e90ea8cb | 749 | while (1) |
87c476a2 | 750 | { |
d46aed51 | 751 | /* Find the next nonzero word in this elt. */ |
e90ea8cb NS |
752 | while (bi->word_no != BITMAP_ELEMENT_WORDS) |
753 | { | |
754 | bi->bits = bi->elt1->bits[bi->word_no]; | |
755 | if (bi->bits) | |
756 | goto next_bit; | |
757 | *bit_no += BITMAP_WORD_BITS; | |
758 | bi->word_no++; | |
759 | } | |
c22cacf3 | 760 | |
a30fe4b6 RB |
761 | /* Make sure we didn't remove the element while iterating. */ |
762 | gcc_checking_assert (bi->elt1->indx != -1U); | |
763 | ||
e90ea8cb NS |
764 | /* Advance to the next element. */ |
765 | bi->elt1 = bi->elt1->next; | |
766 | if (!bi->elt1) | |
767 | return false; | |
768 | *bit_no = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS; | |
769 | bi->word_no = 0; | |
87c476a2 | 770 | } |
87c476a2 ZD |
771 | } |
772 | ||
d46aed51 KH |
773 | /* Advance to the next nonzero bit of an intersecting pair of |
774 | bitmaps. We will have already advanced past the just iterated bit. | |
e90ea8cb | 775 | Return true if there is a bit to iterate. */ |
87c476a2 | 776 | |
e90ea8cb NS |
777 | static inline bool |
778 | bmp_iter_and (bitmap_iterator *bi, unsigned *bit_no) | |
87c476a2 | 779 | { |
d46aed51 | 780 | /* If our current word is nonzero, it contains the bit we want. */ |
e90ea8cb NS |
781 | if (bi->bits) |
782 | { | |
783 | next_bit: | |
d5568f03 | 784 | bmp_iter_next_bit (bi, bit_no); |
e90ea8cb NS |
785 | return true; |
786 | } | |
87c476a2 | 787 | |
e90ea8cb NS |
788 | /* Round up to the word boundary. We might have just iterated past |
789 | the end of the last word, hence the -1. It is not possible for | |
790 | bit_no to point at the beginning of the now last word. */ | |
791 | *bit_no = ((*bit_no + BITMAP_WORD_BITS - 1) | |
792 | / BITMAP_WORD_BITS * BITMAP_WORD_BITS); | |
793 | bi->word_no++; | |
c22cacf3 | 794 | |
87c476a2 ZD |
795 | while (1) |
796 | { | |
d46aed51 | 797 | /* Find the next nonzero word in this elt. */ |
e90ea8cb | 798 | while (bi->word_no != BITMAP_ELEMENT_WORDS) |
87c476a2 | 799 | { |
e90ea8cb NS |
800 | bi->bits = bi->elt1->bits[bi->word_no] & bi->elt2->bits[bi->word_no]; |
801 | if (bi->bits) | |
802 | goto next_bit; | |
803 | *bit_no += BITMAP_WORD_BITS; | |
804 | bi->word_no++; | |
87c476a2 | 805 | } |
c22cacf3 | 806 | |
e90ea8cb | 807 | /* Advance to the next identical element. */ |
87c476a2 ZD |
808 | do |
809 | { | |
a30fe4b6 RB |
810 | /* Make sure we didn't remove the element while iterating. */ |
811 | gcc_checking_assert (bi->elt1->indx != -1U); | |
812 | ||
e90ea8cb NS |
813 | /* Advance elt1 while it is less than elt2. We always want |
814 | to advance one elt. */ | |
815 | do | |
87c476a2 | 816 | { |
e90ea8cb NS |
817 | bi->elt1 = bi->elt1->next; |
818 | if (!bi->elt1) | |
819 | return false; | |
820 | } | |
821 | while (bi->elt1->indx < bi->elt2->indx); | |
c22cacf3 | 822 | |
a30fe4b6 RB |
823 | /* Make sure we didn't remove the element while iterating. */ |
824 | gcc_checking_assert (bi->elt2->indx != -1U); | |
825 | ||
e90ea8cb NS |
826 | /* Advance elt2 to be no less than elt1. This might not |
827 | advance. */ | |
828 | while (bi->elt2->indx < bi->elt1->indx) | |
829 | { | |
830 | bi->elt2 = bi->elt2->next; | |
831 | if (!bi->elt2) | |
832 | return false; | |
87c476a2 ZD |
833 | } |
834 | } | |
e90ea8cb | 835 | while (bi->elt1->indx != bi->elt2->indx); |
c22cacf3 | 836 | |
e90ea8cb NS |
837 | *bit_no = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS; |
838 | bi->word_no = 0; | |
87c476a2 ZD |
839 | } |
840 | } | |
841 | ||
d46aed51 | 842 | /* Advance to the next nonzero bit in the intersection of |
e90ea8cb NS |
843 | complemented bitmaps. We will have already advanced past the just |
844 | iterated bit. */ | |
87c476a2 | 845 | |
e90ea8cb NS |
846 | static inline bool |
847 | bmp_iter_and_compl (bitmap_iterator *bi, unsigned *bit_no) | |
87c476a2 | 848 | { |
d46aed51 | 849 | /* If our current word is nonzero, it contains the bit we want. */ |
e90ea8cb | 850 | if (bi->bits) |
87c476a2 | 851 | { |
e90ea8cb | 852 | next_bit: |
d5568f03 | 853 | bmp_iter_next_bit (bi, bit_no); |
e90ea8cb | 854 | return true; |
87c476a2 ZD |
855 | } |
856 | ||
e90ea8cb NS |
857 | /* Round up to the word boundary. We might have just iterated past |
858 | the end of the last word, hence the -1. It is not possible for | |
859 | bit_no to point at the beginning of the now last word. */ | |
860 | *bit_no = ((*bit_no + BITMAP_WORD_BITS - 1) | |
861 | / BITMAP_WORD_BITS * BITMAP_WORD_BITS); | |
862 | bi->word_no++; | |
87c476a2 | 863 | |
e90ea8cb | 864 | while (1) |
87c476a2 | 865 | { |
d46aed51 | 866 | /* Find the next nonzero word in this elt. */ |
e90ea8cb NS |
867 | while (bi->word_no != BITMAP_ELEMENT_WORDS) |
868 | { | |
869 | bi->bits = bi->elt1->bits[bi->word_no]; | |
870 | if (bi->elt2 && bi->elt2->indx == bi->elt1->indx) | |
871 | bi->bits &= ~bi->elt2->bits[bi->word_no]; | |
872 | if (bi->bits) | |
873 | goto next_bit; | |
874 | *bit_no += BITMAP_WORD_BITS; | |
875 | bi->word_no++; | |
876 | } | |
c22cacf3 | 877 | |
a30fe4b6 RB |
878 | /* Make sure we didn't remove the element while iterating. */ |
879 | gcc_checking_assert (bi->elt1->indx != -1U); | |
880 | ||
e90ea8cb NS |
881 | /* Advance to the next element of elt1. */ |
882 | bi->elt1 = bi->elt1->next; | |
883 | if (!bi->elt1) | |
884 | return false; | |
885 | ||
a30fe4b6 RB |
886 | /* Make sure we didn't remove the element while iterating. */ |
887 | gcc_checking_assert (! bi->elt2 || bi->elt2->indx != -1U); | |
888 | ||
e90ea8cb NS |
889 | /* Advance elt2 until it is no less than elt1. */ |
890 | while (bi->elt2 && bi->elt2->indx < bi->elt1->indx) | |
891 | bi->elt2 = bi->elt2->next; | |
c22cacf3 | 892 | |
e90ea8cb NS |
893 | *bit_no = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS; |
894 | bi->word_no = 0; | |
87c476a2 | 895 | } |
87c476a2 ZD |
896 | } |
897 | ||
7a18d752 RB |
898 | /* If you are modifying a bitmap you are currently iterating over you |
899 | have to ensure to | |
900 | - never remove the current bit; | |
901 | - if you set or clear a bit before the current bit this operation | |
902 | will not affect the set of bits you are visiting during the iteration; | |
903 | - if you set or clear a bit after the current bit it is unspecified | |
904 | whether that affects the set of bits you are visiting during the | |
905 | iteration. | |
906 | If you want to remove the current bit you can delay this to the next | |
907 | iteration (and after the iteration in case the last iteration is | |
908 | affected). */ | |
909 | ||
e90ea8cb NS |
910 | /* Loop over all bits set in BITMAP, starting with MIN and setting |
911 | BITNUM to the bit number. ITER is a bitmap iterator. BITNUM | |
912 | should be treated as a read-only variable as it contains loop | |
913 | state. */ | |
87c476a2 | 914 | |
d4ac4ce2 LC |
915 | #ifndef EXECUTE_IF_SET_IN_BITMAP |
916 | /* See sbitmap.h for the other definition of EXECUTE_IF_SET_IN_BITMAP. */ | |
e90ea8cb NS |
917 | #define EXECUTE_IF_SET_IN_BITMAP(BITMAP, MIN, BITNUM, ITER) \ |
918 | for (bmp_iter_set_init (&(ITER), (BITMAP), (MIN), &(BITNUM)); \ | |
919 | bmp_iter_set (&(ITER), &(BITNUM)); \ | |
920 | bmp_iter_next (&(ITER), &(BITNUM))) | |
d4ac4ce2 | 921 | #endif |
e90ea8cb NS |
922 | |
923 | /* Loop over all the bits set in BITMAP1 & BITMAP2, starting with MIN | |
924 | and setting BITNUM to the bit number. ITER is a bitmap iterator. | |
925 | BITNUM should be treated as a read-only variable as it contains | |
926 | loop state. */ | |
927 | ||
928 | #define EXECUTE_IF_AND_IN_BITMAP(BITMAP1, BITMAP2, MIN, BITNUM, ITER) \ | |
c22cacf3 | 929 | for (bmp_iter_and_init (&(ITER), (BITMAP1), (BITMAP2), (MIN), \ |
e90ea8cb NS |
930 | &(BITNUM)); \ |
931 | bmp_iter_and (&(ITER), &(BITNUM)); \ | |
932 | bmp_iter_next (&(ITER), &(BITNUM))) | |
933 | ||
934 | /* Loop over all the bits set in BITMAP1 & ~BITMAP2, starting with MIN | |
935 | and setting BITNUM to the bit number. ITER is a bitmap iterator. | |
936 | BITNUM should be treated as a read-only variable as it contains | |
937 | loop state. */ | |
938 | ||
939 | #define EXECUTE_IF_AND_COMPL_IN_BITMAP(BITMAP1, BITMAP2, MIN, BITNUM, ITER) \ | |
940 | for (bmp_iter_and_compl_init (&(ITER), (BITMAP1), (BITMAP2), (MIN), \ | |
c22cacf3 | 941 | &(BITNUM)); \ |
e90ea8cb NS |
942 | bmp_iter_and_compl (&(ITER), &(BITNUM)); \ |
943 | bmp_iter_next (&(ITER), &(BITNUM))) | |
a05924f9 | 944 | |
8b670f93 AH |
945 | /* A class that ties the lifetime of a bitmap to its scope. */ |
946 | class auto_bitmap | |
947 | { | |
948 | public: | |
d7bd009a RB |
949 | auto_bitmap (ALONE_CXX_MEM_STAT_INFO) |
950 | { bitmap_initialize (&m_bits, &bitmap_default_obstack PASS_MEM_STAT); } | |
951 | explicit auto_bitmap (bitmap_obstack *o CXX_MEM_STAT_INFO) | |
952 | { bitmap_initialize (&m_bits, o PASS_MEM_STAT); } | |
a4d51bfb | 953 | ~auto_bitmap () { bitmap_clear (&m_bits); } |
8b670f93 | 954 | // Allow calling bitmap functions on our bitmap. |
a4d51bfb | 955 | operator bitmap () { return &m_bits; } |
8b670f93 AH |
956 | |
957 | private: | |
958 | // Prevent making a copy that references our bitmap. | |
959 | auto_bitmap (const auto_bitmap &); | |
960 | auto_bitmap &operator = (const auto_bitmap &); | |
8b670f93 AH |
961 | auto_bitmap (auto_bitmap &&); |
962 | auto_bitmap &operator = (auto_bitmap &&); | |
8b670f93 | 963 | |
a4d51bfb | 964 | bitmap_head m_bits; |
8b670f93 AH |
965 | }; |
966 | ||
148909bc RS |
967 | /* Base class for bitmap_view; see there for details. */ |
968 | template<typename T, typename Traits = array_traits<T> > | |
969 | class base_bitmap_view | |
970 | { | |
971 | public: | |
972 | typedef typename Traits::element_type array_element_type; | |
973 | ||
974 | base_bitmap_view (const T &, bitmap_element *); | |
975 | operator const_bitmap () const { return &m_head; } | |
976 | ||
977 | private: | |
978 | base_bitmap_view (const base_bitmap_view &); | |
979 | ||
980 | bitmap_head m_head; | |
981 | }; | |
982 | ||
983 | /* Provides a read-only bitmap view of a single integer bitmask or a | |
984 | constant-sized array of integer bitmasks, or of a wrapper around such | |
985 | bitmasks. */ | |
986 | template<typename T, typename Traits> | |
987 | class bitmap_view<T, Traits, true> : public base_bitmap_view<T, Traits> | |
988 | { | |
989 | public: | |
990 | bitmap_view (const T &array) | |
991 | : base_bitmap_view<T, Traits> (array, m_bitmap_elements) {} | |
992 | ||
993 | private: | |
994 | /* How many bitmap_elements we need to hold a full T. */ | |
995 | static const size_t num_bitmap_elements | |
996 | = CEIL (CHAR_BIT | |
997 | * sizeof (typename Traits::element_type) | |
998 | * Traits::constant_size, | |
999 | BITMAP_ELEMENT_ALL_BITS); | |
1000 | bitmap_element m_bitmap_elements[num_bitmap_elements]; | |
1001 | }; | |
1002 | ||
1003 | /* Initialize the view for array ARRAY, using the array of bitmap | |
1004 | elements in BITMAP_ELEMENTS (which is known to contain enough | |
1005 | entries). */ | |
1006 | template<typename T, typename Traits> | |
1007 | base_bitmap_view<T, Traits>::base_bitmap_view (const T &array, | |
1008 | bitmap_element *bitmap_elements) | |
1009 | { | |
1010 | m_head.obstack = NULL; | |
1011 | ||
1012 | /* The code currently assumes that each element of ARRAY corresponds | |
1013 | to exactly one bitmap_element. */ | |
1014 | const size_t array_element_bits = CHAR_BIT * sizeof (array_element_type); | |
1015 | STATIC_ASSERT (BITMAP_ELEMENT_ALL_BITS % array_element_bits == 0); | |
1016 | size_t array_step = BITMAP_ELEMENT_ALL_BITS / array_element_bits; | |
1017 | size_t array_size = Traits::size (array); | |
1018 | ||
1019 | /* Process each potential bitmap_element in turn. The loop is written | |
1020 | this way rather than per array element because usually there are | |
1021 | only a small number of array elements per bitmap element (typically | |
1022 | two or four). The inner loops should therefore unroll completely. */ | |
1023 | const array_element_type *array_elements = Traits::base (array); | |
1024 | unsigned int indx = 0; | |
1025 | for (size_t array_base = 0; | |
1026 | array_base < array_size; | |
1027 | array_base += array_step, indx += 1) | |
1028 | { | |
1029 | /* How many array elements are in this particular bitmap_element. */ | |
1030 | unsigned int array_count | |
1031 | = (STATIC_CONSTANT_P (array_size % array_step == 0) | |
1032 | ? array_step : MIN (array_step, array_size - array_base)); | |
1033 | ||
1034 | /* See whether we need this bitmap element. */ | |
1035 | array_element_type ior = array_elements[array_base]; | |
1036 | for (size_t i = 1; i < array_count; ++i) | |
1037 | ior |= array_elements[array_base + i]; | |
1038 | if (ior == 0) | |
1039 | continue; | |
1040 | ||
1041 | /* Grab the next bitmap element and chain it. */ | |
1042 | bitmap_element *bitmap_element = bitmap_elements++; | |
1043 | if (m_head.current) | |
1044 | m_head.current->next = bitmap_element; | |
1045 | else | |
1046 | m_head.first = bitmap_element; | |
1047 | bitmap_element->prev = m_head.current; | |
1048 | bitmap_element->next = NULL; | |
1049 | bitmap_element->indx = indx; | |
1050 | m_head.current = bitmap_element; | |
1051 | m_head.indx = indx; | |
1052 | ||
1053 | /* Fill in the bits of the bitmap element. */ | |
1054 | if (array_element_bits < BITMAP_WORD_BITS) | |
1055 | { | |
1056 | /* Multiple array elements fit in one element of | |
1057 | bitmap_element->bits. */ | |
1058 | size_t array_i = array_base; | |
1059 | for (unsigned int word_i = 0; word_i < BITMAP_ELEMENT_WORDS; | |
1060 | ++word_i) | |
1061 | { | |
1062 | BITMAP_WORD word = 0; | |
1063 | for (unsigned int shift = 0; | |
1064 | shift < BITMAP_WORD_BITS && array_i < array_size; | |
1065 | shift += array_element_bits) | |
1066 | word |= array_elements[array_i++] << shift; | |
1067 | bitmap_element->bits[word_i] = word; | |
1068 | } | |
1069 | } | |
1070 | else | |
1071 | { | |
1072 | /* Array elements are the same size as elements of | |
1073 | bitmap_element->bits, or are an exact multiple of that size. */ | |
1074 | unsigned int word_i = 0; | |
1075 | for (unsigned int i = 0; i < array_count; ++i) | |
1076 | for (unsigned int shift = 0; shift < array_element_bits; | |
1077 | shift += BITMAP_WORD_BITS) | |
1078 | bitmap_element->bits[word_i++] | |
1079 | = array_elements[array_base + i] >> shift; | |
1080 | while (word_i < BITMAP_ELEMENT_WORDS) | |
1081 | bitmap_element->bits[word_i++] = 0; | |
1082 | } | |
1083 | } | |
1084 | } | |
1085 | ||
88657302 | 1086 | #endif /* GCC_BITMAP_H */ |