]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/bitmap.h
C++: more location wrapper nodes (PR c++/43064, PR c++/43486)
[thirdparty/gcc.git] / gcc / bitmap.h
CommitLineData
2f925a60 1/* Functions to support general ended bitmaps.
8e8f6434 2 Copyright (C) 1997-2018 Free Software Foundation, Inc.
2f925a60 3
f12b58b3 4This file is part of GCC.
2f925a60 5
f12b58b3 6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8c4c00c1 8Software Foundation; either version 3, or (at your option) any later
f12b58b3 9version.
2f925a60 10
f12b58b3 11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
2f925a60 15
16You should have received a copy of the GNU General Public License
8c4c00c1 17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
2f925a60 19
2a281353 20#ifndef GCC_BITMAP_H
9342ee68 21#define GCC_BITMAP_H
4f862fce 22
e35f850e 23/* Implementation of sparse integer sets as a linked list or tree.
4f862fce 24
25 This sparse set representation is suitable for sparse sets with an
e35f850e 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
4f862fce 36 the list holds the element with the smallest member of the set.
e35f850e 37 In tree form, nodes to the left have a smaller container index.
4f862fce 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
e35f850e 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).
4f862fce 53
e35f850e 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:
4f862fce 110
111 * clear : bitmap_clear
e35f850e 112 * smallest_member : bitmap_first_set_bit
4f862fce 113 * choose_one : (not implemented, but could be
e35f850e 114 in constant time)
4f862fce 115
e35f850e 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:
4f862fce 119
120 * member_p : bitmap_bit_p
e35f850e 121 * add_member : bitmap_set_bit / bitmap_set_range
122 * remove_member : bitmap_clear_bit / bitmap_clear_range
4f862fce 123
e35f850e 124 The following operations can be performed in O(E) time in list view:
4f862fce 125
126 * cardinality : bitmap_count_bits
e35f850e 127 * largest_member : bitmap_last_set_bit (but this could
4f862fce 128 in constant time with a pointer to
129 the last element in the chain)
e35f850e 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
4f862fce 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
47ae02b7 157 Some operations on 3 sets that occur frequently in data flow problems
4f862fce 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
4f862fce 164
e35f850e 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.
4f862fce 171
e35f850e 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.
4f862fce 177
e35f850e 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).
4f862fce 182
e35f850e 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. */
4f862fce 211
349621f0 212#include "obstack.h"
0ff42de5 213
214/* Bitmap memory usage. */
215struct bitmap_usage: public mem_usage
216{
217 /* Default contructor. */
218 bitmap_usage (): m_nsearches (0), m_search_iter (0) {}
219 /* Constructor. */
220 bitmap_usage (size_t allocated, size_t times, size_t peak,
221 uint64_t nsearches, uint64_t search_iter)
222 : mem_usage (allocated, times, peak),
223 m_nsearches (nsearches), m_search_iter (search_iter) {}
224
225 /* Sum the usage with SECOND usage. */
94302dfa 226 bitmap_usage
227 operator+ (const bitmap_usage &second)
0ff42de5 228 {
229 return bitmap_usage (m_allocated + second.m_allocated,
230 m_times + second.m_times,
231 m_peak + second.m_peak,
232 m_nsearches + second.m_nsearches,
233 m_search_iter + second.m_search_iter);
234 }
235
236 /* Dump usage coupled to LOC location, where TOTAL is sum of all rows. */
94302dfa 237 inline void
238 dump (mem_location *loc, mem_usage &total) const
0ff42de5 239 {
7da284df 240 char *location_string = loc->to_string ();
0ff42de5 241
03fac02c 242 fprintf (stderr, "%-48s " PRsa (9) ":%5.1f%%"
243 PRsa (9) PRsa (9) ":%5.1f%%"
244 PRsa (11) PRsa (11) "%10s\n",
7a413494 245 location_string, SIZE_AMOUNT (m_allocated),
462aa751 246 get_percent (m_allocated, total.m_allocated),
7a413494 247 SIZE_AMOUNT (m_peak), SIZE_AMOUNT (m_times),
0ff42de5 248 get_percent (m_times, total.m_times),
7a413494 249 SIZE_AMOUNT (m_nsearches), SIZE_AMOUNT (m_search_iter),
0ff42de5 250 loc->m_ggc ? "ggc" : "heap");
7da284df 251
252 free (location_string);
0ff42de5 253 }
254
255 /* Dump header with NAME. */
94302dfa 256 static inline void
257 dump_header (const char *name)
0ff42de5 258 {
259 fprintf (stderr, "%-48s %11s%16s%17s%12s%12s%10s\n", name, "Leak", "Peak",
260 "Times", "N searches", "Search iter", "Type");
261 print_dash_line ();
262 }
263
264 /* Number search operations. */
265 uint64_t m_nsearches;
266 /* Number of search iterations. */
267 uint64_t m_search_iter;
268};
269
270/* Bitmap memory description. */
271extern mem_alloc_description<bitmap_usage> bitmap_mem_desc;
f3d96a58 272
e5c8f082 273/* Fundamental storage type for bitmap. */
274
e5c8f082 275typedef unsigned long BITMAP_WORD;
84199e4b 276/* BITMAP_WORD_BITS needs to be unsigned, but cannot contain casts as
277 it is used in preprocessor directives -- hence the 1u. */
278#define BITMAP_WORD_BITS (CHAR_BIT * SIZEOF_LONG * 1u)
e5c8f082 279
2f925a60 280/* Number of words to use for each element in the linked list. */
281
282#ifndef BITMAP_ELEMENT_WORDS
84199e4b 283#define BITMAP_ELEMENT_WORDS ((128 + BITMAP_WORD_BITS - 1) / BITMAP_WORD_BITS)
2f925a60 284#endif
285
84199e4b 286/* Number of bits in each actual element of a bitmap. */
2f925a60 287
84199e4b 288#define BITMAP_ELEMENT_ALL_BITS (BITMAP_ELEMENT_WORDS * BITMAP_WORD_BITS)
2f925a60 289
42fe97ed 290/* Obstack for allocating bitmaps and elements from. */
b3e7c666 291struct GTY (()) bitmap_obstack {
292 struct bitmap_element *elements;
293 struct bitmap_head *heads;
42fe97ed 294 struct obstack GTY ((skip)) obstack;
b3e7c666 295};
42fe97ed 296
2f925a60 297/* Bitmap set element. We use a linked list to hold only the bits that
298 are set. This allows for use to grow the bitset dynamically without
a0c938f0 299 having to realloc and copy a giant bit array.
4bc590db 300
301 The free list is implemented as a list of lists. There is one
302 outer list connected together by prev fields. Each element of that
303 outer is an inner list (that may consist only of the outer list
304 element) that are connected by the next fields. The prev pointer
305 is undefined for interior elements. This allows
306 bitmap_elt_clear_from to be implemented in unit time rather than
307 linear in the number of elements to be freed. */
2f925a60 308
b3e7c666 309struct GTY((chain_next ("%h.next"), chain_prev ("%h.prev"))) bitmap_element {
e35f850e 310 /* In list form, the next element in the linked list;
311 in tree form, the left child node in the tree. */
312 struct bitmap_element *next;
313 /* In list form, the previous element in the linked list;
314 in tree form, the right child node in the tree. */
315 struct bitmap_element *prev;
316 /* regno/BITMAP_ELEMENT_ALL_BITS. */
317 unsigned int indx;
318 /* Bits that are set, counting from INDX, inclusive */
319 BITMAP_WORD bits[BITMAP_ELEMENT_WORDS];
b3e7c666 320};
2f925a60 321
bb4df124 322/* Head of bitmap linked list. The 'current' member points to something
323 already pointed to by the chain started by first, so GTY((skip)) it. */
1e837561 324
b3e7c666 325struct GTY(()) bitmap_head {
7da7f1c6 326 static bitmap_obstack crashme;
327 /* Poison obstack to not make it not a valid initialized GC bitmap. */
328 CONSTEXPR bitmap_head()
329 : indx(0), tree_form(false), first(NULL), current(NULL),
330 obstack (&crashme)
331 {}
e35f850e 332 /* Index of last element looked at. */
333 unsigned int indx;
334 /* False if the bitmap is in list form; true if the bitmap is in tree form.
335 Bitmap iterators only work on bitmaps in list form. */
336 bool tree_form;
337 /* In list form, the first element in the linked list;
338 in tree form, the root of the tree. */
339 bitmap_element *first;
340 /* Last element looked at. */
341 bitmap_element * GTY((skip(""))) current;
342 /* Obstack to allocate elements from. If NULL, then use GGC allocation. */
343 bitmap_obstack *obstack;
be44111e 344 void dump ();
b3e7c666 345};
42fe97ed 346
2f925a60 347/* Global data */
97b330ca 348extern bitmap_element bitmap_zero_bits; /* Zero bitmap element */
42fe97ed 349extern bitmap_obstack bitmap_default_obstack; /* Default bitmap obstack */
2f925a60 350
e35f850e 351/* Change the view of the bitmap to list, or tree. */
352void bitmap_list_view (bitmap);
353void bitmap_tree_view (bitmap);
354
2f925a60 355/* Clear a bitmap by freeing up the linked list. */
aecda0d6 356extern void bitmap_clear (bitmap);
2f925a60 357
a7dce381 358/* Copy a bitmap to another bitmap. */
056755a1 359extern void bitmap_copy (bitmap, const_bitmap);
2f925a60 360
462aa751 361/* Move a bitmap to another bitmap. */
362extern void bitmap_move (bitmap, bitmap);
363
66b5ac3c 364/* True if two bitmaps are identical. */
056755a1 365extern bool bitmap_equal_p (const_bitmap, const_bitmap);
66b5ac3c 366
21576831 367/* True if the bitmaps intersect (their AND is non-empty). */
056755a1 368extern bool bitmap_intersect_p (const_bitmap, const_bitmap);
21576831 369
370/* True if the complement of the second intersects the first (their
371 AND_COMPL is non-empty). */
056755a1 372extern bool bitmap_intersect_compl_p (const_bitmap, const_bitmap);
21576831 373
374/* True if MAP is an empty bitmap. */
53c5d9d4 375inline bool bitmap_empty_p (const_bitmap map)
376{
377 return !map->first;
378}
604efc01 379
6bd6739a 380/* True if the bitmap has only a single bit set. */
381extern bool bitmap_single_bit_set_p (const_bitmap);
382
5669bd62 383/* Count the number of bits set in the bitmap. */
056755a1 384extern unsigned long bitmap_count_bits (const_bitmap);
5669bd62 385
db176279 386/* Count the number of unique bits set across the two bitmaps. */
387extern unsigned long bitmap_count_unique_bits (const_bitmap, const_bitmap);
388
b6e72c17 389/* Boolean operations on bitmaps. The _into variants are two operand
390 versions that modify the first source operand. The other variants
391 are three operand versions that to not destroy the source bitmaps.
392 The operations supported are &, & ~, |, ^. */
056755a1 393extern void bitmap_and (bitmap, const_bitmap, const_bitmap);
ea9538fb 394extern bool bitmap_and_into (bitmap, const_bitmap);
056755a1 395extern bool bitmap_and_compl (bitmap, const_bitmap, const_bitmap);
396extern bool bitmap_and_compl_into (bitmap, const_bitmap);
5669bd62 397#define bitmap_compl_and(DST, A, B) bitmap_and_compl (DST, B, A)
056755a1 398extern void bitmap_compl_and_into (bitmap, const_bitmap);
5669bd62 399extern void bitmap_clear_range (bitmap, unsigned int, unsigned int);
3072d30e 400extern void bitmap_set_range (bitmap, unsigned int, unsigned int);
056755a1 401extern bool bitmap_ior (bitmap, const_bitmap, const_bitmap);
402extern bool bitmap_ior_into (bitmap, const_bitmap);
403extern void bitmap_xor (bitmap, const_bitmap, const_bitmap);
404extern void bitmap_xor_into (bitmap, const_bitmap);
b6e72c17 405
d7f2555b 406/* DST = A | (B & C). Return true if DST changes. */
407extern bool bitmap_ior_and_into (bitmap DST, const_bitmap B, const_bitmap C);
b6e72c17 408/* DST = A | (B & ~C). Return true if DST changes. */
4f862fce 409extern bool bitmap_ior_and_compl (bitmap DST, const_bitmap A,
410 const_bitmap B, const_bitmap C);
b6e72c17 411/* A |= (B & ~C). Return true if A changes. */
4f862fce 412extern bool bitmap_ior_and_compl_into (bitmap A,
413 const_bitmap B, const_bitmap C);
2f925a60 414
b64035d2 415/* Clear a single bit in a bitmap. Return true if the bit changed. */
416extern bool bitmap_clear_bit (bitmap, int);
2f925a60 417
b64035d2 418/* Set a single bit in a bitmap. Return true if the bit changed. */
419extern bool bitmap_set_bit (bitmap, int);
2f925a60 420
e35f850e 421/* Return true if a bit is set in a bitmap. */
aecda0d6 422extern int bitmap_bit_p (bitmap, int);
2f925a60 423
e35f850e 424/* Debug functions to print a bitmap. */
056755a1 425extern void debug_bitmap (const_bitmap);
426extern void debug_bitmap_file (FILE *, const_bitmap);
2f925a60 427
2358393e 428/* Print a bitmap. */
056755a1 429extern void bitmap_print (FILE *, const_bitmap, const char *, const char *);
23ec99a1 430
4bc590db 431/* Initialize and release a bitmap obstack. */
42fe97ed 432extern void bitmap_obstack_initialize (bitmap_obstack *);
433extern void bitmap_obstack_release (bitmap_obstack *);
f65ffe0d 434extern void bitmap_register (bitmap MEM_STAT_DECL);
435extern void dump_bitmap_statistics (void);
2f925a60 436
42fe97ed 437/* Initialize a bitmap header. OBSTACK indicates the bitmap obstack
438 to allocate from, NULL for GC'd bitmap. */
439
440static inline void
076121e0 441bitmap_initialize (bitmap head, bitmap_obstack *obstack CXX_MEM_STAT_INFO)
42fe97ed 442{
443 head->first = head->current = NULL;
e35f850e 444 head->indx = head->tree_form = 0;
42fe97ed 445 head->obstack = obstack;
ecd52ea9 446 if (GATHER_STATISTICS)
447 bitmap_register (head PASS_MEM_STAT);
42fe97ed 448}
449
7da7f1c6 450/* Release a bitmap (but not its head). This is suitable for pairing with
451 bitmap_initialize. */
452
453static inline void
454bitmap_release (bitmap head)
455{
456 bitmap_clear (head);
457 /* Poison the obstack pointer so the obstack can be safely released.
458 Do not zero it as the bitmap then becomes initialized GC. */
459 head->obstack = &bitmap_head::crashme;
460}
461
42fe97ed 462/* Allocate and free bitmaps from obstack, malloc and gc'd memory. */
c163347d 463extern bitmap bitmap_alloc (bitmap_obstack *obstack CXX_MEM_STAT_INFO);
464#define BITMAP_ALLOC bitmap_alloc
465extern bitmap bitmap_gc_alloc (ALONE_CXX_MEM_STAT_INFO);
466#define BITMAP_GGC_ALLOC bitmap_gc_alloc
42fe97ed 467extern void bitmap_obstack_free (bitmap);
2f925a60 468
973b4493 469/* A few compatibility/functions macros for compatibility with sbitmaps */
53c5d9d4 470inline void dump_bitmap (FILE *file, const_bitmap map)
471{
472 bitmap_print (file, map, "", "\n");
473}
b3e7c666 474extern void debug (const bitmap_head &ref);
475extern void debug (const bitmap_head *ptr);
53c5d9d4 476
056755a1 477extern unsigned bitmap_first_set_bit (const_bitmap);
ffa800d1 478extern unsigned bitmap_last_set_bit (const_bitmap);
973b4493 479
844ea20e 480/* Compute bitmap hash (for purposes of hashing etc.) */
9af5ce0c 481extern hashval_t bitmap_hash (const_bitmap);
844ea20e 482
2f925a60 483/* Do any cleanup needed on a bitmap when it is no longer used. */
f3d84bf8 484#define BITMAP_FREE(BITMAP) \
485 ((void) (bitmap_obstack_free ((bitmap) BITMAP), (BITMAP) = (bitmap) NULL))
aeee2a4b 486
0cc4271a 487/* Iterator for bitmaps. */
2f925a60 488
b3e7c666 489struct bitmap_iterator
0cc4271a 490{
262cead1 491 /* Pointer to the current bitmap element. */
492 bitmap_element *elt1;
a0c938f0 493
262cead1 494 /* Pointer to 2nd bitmap element when two are involved. */
495 bitmap_element *elt2;
496
497 /* Word within the current element. */
498 unsigned word_no;
a0c938f0 499
0cc4271a 500 /* Contents of the actually processed word. When finding next bit
501 it is shifted right, so that the actual bit is always the least
502 significant bit of ACTUAL. */
262cead1 503 BITMAP_WORD bits;
b3e7c666 504};
0cc4271a 505
262cead1 506/* Initialize a single bitmap iterator. START_BIT is the first bit to
507 iterate from. */
0cc4271a 508
262cead1 509static inline void
056755a1 510bmp_iter_set_init (bitmap_iterator *bi, const_bitmap map,
262cead1 511 unsigned start_bit, unsigned *bit_no)
0cc4271a 512{
262cead1 513 bi->elt1 = map->first;
514 bi->elt2 = NULL;
515
e35f850e 516 gcc_checking_assert (!map->tree_form);
517
262cead1 518 /* Advance elt1 until it is not before the block containing start_bit. */
519 while (1)
0cc4271a 520 {
262cead1 521 if (!bi->elt1)
522 {
523 bi->elt1 = &bitmap_zero_bits;
524 break;
525 }
a0c938f0 526
262cead1 527 if (bi->elt1->indx >= start_bit / BITMAP_ELEMENT_ALL_BITS)
528 break;
529 bi->elt1 = bi->elt1->next;
0cc4271a 530 }
531
262cead1 532 /* We might have gone past the start bit, so reinitialize it. */
533 if (bi->elt1->indx != start_bit / BITMAP_ELEMENT_ALL_BITS)
534 start_bit = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
a0c938f0 535
262cead1 536 /* Initialize for what is now start_bit. */
537 bi->word_no = start_bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
538 bi->bits = bi->elt1->bits[bi->word_no];
539 bi->bits >>= start_bit % BITMAP_WORD_BITS;
540
541 /* If this word is zero, we must make sure we're not pointing at the
542 first bit, otherwise our incrementing to the next word boundary
543 will fail. It won't matter if this increment moves us into the
544 next word. */
545 start_bit += !bi->bits;
a0c938f0 546
262cead1 547 *bit_no = start_bit;
0cc4271a 548}
549
262cead1 550/* Initialize an iterator to iterate over the intersection of two
551 bitmaps. START_BIT is the bit to commence from. */
0cc4271a 552
262cead1 553static inline void
056755a1 554bmp_iter_and_init (bitmap_iterator *bi, const_bitmap map1, const_bitmap map2,
262cead1 555 unsigned start_bit, unsigned *bit_no)
0cc4271a 556{
262cead1 557 bi->elt1 = map1->first;
558 bi->elt2 = map2->first;
0cc4271a 559
e35f850e 560 gcc_checking_assert (!map1->tree_form && !map2->tree_form);
561
262cead1 562 /* Advance elt1 until it is not before the block containing
563 start_bit. */
0cc4271a 564 while (1)
565 {
262cead1 566 if (!bi->elt1)
0cc4271a 567 {
262cead1 568 bi->elt2 = NULL;
569 break;
0cc4271a 570 }
a0c938f0 571
262cead1 572 if (bi->elt1->indx >= start_bit / BITMAP_ELEMENT_ALL_BITS)
573 break;
574 bi->elt1 = bi->elt1->next;
0cc4271a 575 }
a0c938f0 576
262cead1 577 /* Advance elt2 until it is not before elt1. */
578 while (1)
0cc4271a 579 {
262cead1 580 if (!bi->elt2)
581 {
582 bi->elt1 = bi->elt2 = &bitmap_zero_bits;
583 break;
584 }
a0c938f0 585
262cead1 586 if (bi->elt2->indx >= bi->elt1->indx)
587 break;
588 bi->elt2 = bi->elt2->next;
0cc4271a 589 }
590
621a25ae 591 /* If we're at the same index, then we have some intersecting bits. */
262cead1 592 if (bi->elt1->indx == bi->elt2->indx)
0cc4271a 593 {
262cead1 594 /* We might have advanced beyond the start_bit, so reinitialize
a0c938f0 595 for that. */
262cead1 596 if (bi->elt1->indx != start_bit / BITMAP_ELEMENT_ALL_BITS)
597 start_bit = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
a0c938f0 598
262cead1 599 bi->word_no = start_bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
600 bi->bits = bi->elt1->bits[bi->word_no] & bi->elt2->bits[bi->word_no];
601 bi->bits >>= start_bit % BITMAP_WORD_BITS;
0cc4271a 602 }
603 else
604 {
262cead1 605 /* Otherwise we must immediately advance elt1, so initialize for
606 that. */
607 bi->word_no = BITMAP_ELEMENT_WORDS - 1;
608 bi->bits = 0;
0cc4271a 609 }
a0c938f0 610
262cead1 611 /* If this word is zero, we must make sure we're not pointing at the
612 first bit, otherwise our incrementing to the next word boundary
613 will fail. It won't matter if this increment moves us into the
614 next word. */
615 start_bit += !bi->bits;
a0c938f0 616
262cead1 617 *bit_no = start_bit;
0cc4271a 618}
619
e35f850e 620/* Initialize an iterator to iterate over the bits in MAP1 & ~MAP2. */
0cc4271a 621
262cead1 622static inline void
4f862fce 623bmp_iter_and_compl_init (bitmap_iterator *bi,
624 const_bitmap map1, const_bitmap map2,
262cead1 625 unsigned start_bit, unsigned *bit_no)
0cc4271a 626{
262cead1 627 bi->elt1 = map1->first;
628 bi->elt2 = map2->first;
0cc4271a 629
e35f850e 630 gcc_checking_assert (!map1->tree_form && !map2->tree_form);
631
262cead1 632 /* Advance elt1 until it is not before the block containing start_bit. */
0cc4271a 633 while (1)
634 {
262cead1 635 if (!bi->elt1)
0cc4271a 636 {
262cead1 637 bi->elt1 = &bitmap_zero_bits;
638 break;
0cc4271a 639 }
a0c938f0 640
262cead1 641 if (bi->elt1->indx >= start_bit / BITMAP_ELEMENT_ALL_BITS)
642 break;
643 bi->elt1 = bi->elt1->next;
0cc4271a 644 }
262cead1 645
646 /* Advance elt2 until it is not before elt1. */
647 while (bi->elt2 && bi->elt2->indx < bi->elt1->indx)
648 bi->elt2 = bi->elt2->next;
649
650 /* We might have advanced beyond the start_bit, so reinitialize for
651 that. */
652 if (bi->elt1->indx != start_bit / BITMAP_ELEMENT_ALL_BITS)
653 start_bit = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
a0c938f0 654
262cead1 655 bi->word_no = start_bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
656 bi->bits = bi->elt1->bits[bi->word_no];
657 if (bi->elt2 && bi->elt1->indx == bi->elt2->indx)
658 bi->bits &= ~bi->elt2->bits[bi->word_no];
659 bi->bits >>= start_bit % BITMAP_WORD_BITS;
a0c938f0 660
262cead1 661 /* If this word is zero, we must make sure we're not pointing at the
662 first bit, otherwise our incrementing to the next word boundary
663 will fail. It won't matter if this increment moves us into the
664 next word. */
665 start_bit += !bi->bits;
a0c938f0 666
262cead1 667 *bit_no = start_bit;
0cc4271a 668}
669
262cead1 670/* Advance to the next bit in BI. We don't advance to the next
ed86421a 671 nonzero bit yet. */
0cc4271a 672
262cead1 673static inline void
674bmp_iter_next (bitmap_iterator *bi, unsigned *bit_no)
0cc4271a 675{
262cead1 676 bi->bits >>= 1;
677 *bit_no += 1;
678}
0cc4271a 679
bbcfb57f 680/* Advance to first set bit in BI. */
681
682static inline void
683bmp_iter_next_bit (bitmap_iterator * bi, unsigned *bit_no)
684{
685#if (GCC_VERSION >= 3004)
686 {
687 unsigned int n = __builtin_ctzl (bi->bits);
688 gcc_assert (sizeof (unsigned long) == sizeof (BITMAP_WORD));
689 bi->bits >>= n;
690 *bit_no += n;
691 }
692#else
693 while (!(bi->bits & 1))
694 {
695 bi->bits >>= 1;
696 *bit_no += 1;
697 }
698#endif
699}
700
ed86421a 701/* Advance to the next nonzero bit of a single bitmap, we will have
262cead1 702 already advanced past the just iterated bit. Return true if there
703 is a bit to iterate. */
0cc4271a 704
262cead1 705static inline bool
706bmp_iter_set (bitmap_iterator *bi, unsigned *bit_no)
707{
ed86421a 708 /* If our current word is nonzero, it contains the bit we want. */
262cead1 709 if (bi->bits)
0cc4271a 710 {
262cead1 711 next_bit:
bbcfb57f 712 bmp_iter_next_bit (bi, bit_no);
262cead1 713 return true;
0cc4271a 714 }
715
262cead1 716 /* Round up to the word boundary. We might have just iterated past
717 the end of the last word, hence the -1. It is not possible for
718 bit_no to point at the beginning of the now last word. */
719 *bit_no = ((*bit_no + BITMAP_WORD_BITS - 1)
720 / BITMAP_WORD_BITS * BITMAP_WORD_BITS);
721 bi->word_no++;
0cc4271a 722
262cead1 723 while (1)
0cc4271a 724 {
ed86421a 725 /* Find the next nonzero word in this elt. */
262cead1 726 while (bi->word_no != BITMAP_ELEMENT_WORDS)
727 {
728 bi->bits = bi->elt1->bits[bi->word_no];
729 if (bi->bits)
730 goto next_bit;
731 *bit_no += BITMAP_WORD_BITS;
732 bi->word_no++;
733 }
a0c938f0 734
f79643b7 735 /* Make sure we didn't remove the element while iterating. */
736 gcc_checking_assert (bi->elt1->indx != -1U);
737
262cead1 738 /* Advance to the next element. */
739 bi->elt1 = bi->elt1->next;
740 if (!bi->elt1)
741 return false;
742 *bit_no = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
743 bi->word_no = 0;
0cc4271a 744 }
0cc4271a 745}
746
ed86421a 747/* Advance to the next nonzero bit of an intersecting pair of
748 bitmaps. We will have already advanced past the just iterated bit.
262cead1 749 Return true if there is a bit to iterate. */
0cc4271a 750
262cead1 751static inline bool
752bmp_iter_and (bitmap_iterator *bi, unsigned *bit_no)
0cc4271a 753{
ed86421a 754 /* If our current word is nonzero, it contains the bit we want. */
262cead1 755 if (bi->bits)
756 {
757 next_bit:
bbcfb57f 758 bmp_iter_next_bit (bi, bit_no);
262cead1 759 return true;
760 }
0cc4271a 761
262cead1 762 /* Round up to the word boundary. We might have just iterated past
763 the end of the last word, hence the -1. It is not possible for
764 bit_no to point at the beginning of the now last word. */
765 *bit_no = ((*bit_no + BITMAP_WORD_BITS - 1)
766 / BITMAP_WORD_BITS * BITMAP_WORD_BITS);
767 bi->word_no++;
a0c938f0 768
0cc4271a 769 while (1)
770 {
ed86421a 771 /* Find the next nonzero word in this elt. */
262cead1 772 while (bi->word_no != BITMAP_ELEMENT_WORDS)
0cc4271a 773 {
262cead1 774 bi->bits = bi->elt1->bits[bi->word_no] & bi->elt2->bits[bi->word_no];
775 if (bi->bits)
776 goto next_bit;
777 *bit_no += BITMAP_WORD_BITS;
778 bi->word_no++;
0cc4271a 779 }
a0c938f0 780
262cead1 781 /* Advance to the next identical element. */
0cc4271a 782 do
783 {
f79643b7 784 /* Make sure we didn't remove the element while iterating. */
785 gcc_checking_assert (bi->elt1->indx != -1U);
786
262cead1 787 /* Advance elt1 while it is less than elt2. We always want
788 to advance one elt. */
789 do
0cc4271a 790 {
262cead1 791 bi->elt1 = bi->elt1->next;
792 if (!bi->elt1)
793 return false;
794 }
795 while (bi->elt1->indx < bi->elt2->indx);
a0c938f0 796
f79643b7 797 /* Make sure we didn't remove the element while iterating. */
798 gcc_checking_assert (bi->elt2->indx != -1U);
799
262cead1 800 /* Advance elt2 to be no less than elt1. This might not
801 advance. */
802 while (bi->elt2->indx < bi->elt1->indx)
803 {
804 bi->elt2 = bi->elt2->next;
805 if (!bi->elt2)
806 return false;
0cc4271a 807 }
808 }
262cead1 809 while (bi->elt1->indx != bi->elt2->indx);
a0c938f0 810
262cead1 811 *bit_no = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
812 bi->word_no = 0;
0cc4271a 813 }
814}
815
ed86421a 816/* Advance to the next nonzero bit in the intersection of
262cead1 817 complemented bitmaps. We will have already advanced past the just
818 iterated bit. */
0cc4271a 819
262cead1 820static inline bool
821bmp_iter_and_compl (bitmap_iterator *bi, unsigned *bit_no)
0cc4271a 822{
ed86421a 823 /* If our current word is nonzero, it contains the bit we want. */
262cead1 824 if (bi->bits)
0cc4271a 825 {
262cead1 826 next_bit:
bbcfb57f 827 bmp_iter_next_bit (bi, bit_no);
262cead1 828 return true;
0cc4271a 829 }
830
262cead1 831 /* Round up to the word boundary. We might have just iterated past
832 the end of the last word, hence the -1. It is not possible for
833 bit_no to point at the beginning of the now last word. */
834 *bit_no = ((*bit_no + BITMAP_WORD_BITS - 1)
835 / BITMAP_WORD_BITS * BITMAP_WORD_BITS);
836 bi->word_no++;
0cc4271a 837
262cead1 838 while (1)
0cc4271a 839 {
ed86421a 840 /* Find the next nonzero word in this elt. */
262cead1 841 while (bi->word_no != BITMAP_ELEMENT_WORDS)
842 {
843 bi->bits = bi->elt1->bits[bi->word_no];
844 if (bi->elt2 && bi->elt2->indx == bi->elt1->indx)
845 bi->bits &= ~bi->elt2->bits[bi->word_no];
846 if (bi->bits)
847 goto next_bit;
848 *bit_no += BITMAP_WORD_BITS;
849 bi->word_no++;
850 }
a0c938f0 851
f79643b7 852 /* Make sure we didn't remove the element while iterating. */
853 gcc_checking_assert (bi->elt1->indx != -1U);
854
262cead1 855 /* Advance to the next element of elt1. */
856 bi->elt1 = bi->elt1->next;
857 if (!bi->elt1)
858 return false;
859
f79643b7 860 /* Make sure we didn't remove the element while iterating. */
861 gcc_checking_assert (! bi->elt2 || bi->elt2->indx != -1U);
862
262cead1 863 /* Advance elt2 until it is no less than elt1. */
864 while (bi->elt2 && bi->elt2->indx < bi->elt1->indx)
865 bi->elt2 = bi->elt2->next;
a0c938f0 866
262cead1 867 *bit_no = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
868 bi->word_no = 0;
0cc4271a 869 }
0cc4271a 870}
871
89f41876 872/* If you are modifying a bitmap you are currently iterating over you
873 have to ensure to
874 - never remove the current bit;
875 - if you set or clear a bit before the current bit this operation
876 will not affect the set of bits you are visiting during the iteration;
877 - if you set or clear a bit after the current bit it is unspecified
878 whether that affects the set of bits you are visiting during the
879 iteration.
880 If you want to remove the current bit you can delay this to the next
881 iteration (and after the iteration in case the last iteration is
882 affected). */
883
262cead1 884/* Loop over all bits set in BITMAP, starting with MIN and setting
885 BITNUM to the bit number. ITER is a bitmap iterator. BITNUM
886 should be treated as a read-only variable as it contains loop
887 state. */
0cc4271a 888
0d211963 889#ifndef EXECUTE_IF_SET_IN_BITMAP
890/* See sbitmap.h for the other definition of EXECUTE_IF_SET_IN_BITMAP. */
262cead1 891#define EXECUTE_IF_SET_IN_BITMAP(BITMAP, MIN, BITNUM, ITER) \
892 for (bmp_iter_set_init (&(ITER), (BITMAP), (MIN), &(BITNUM)); \
893 bmp_iter_set (&(ITER), &(BITNUM)); \
894 bmp_iter_next (&(ITER), &(BITNUM)))
0d211963 895#endif
262cead1 896
897/* Loop over all the bits set in BITMAP1 & BITMAP2, starting with MIN
898 and setting BITNUM to the bit number. ITER is a bitmap iterator.
899 BITNUM should be treated as a read-only variable as it contains
900 loop state. */
901
902#define EXECUTE_IF_AND_IN_BITMAP(BITMAP1, BITMAP2, MIN, BITNUM, ITER) \
a0c938f0 903 for (bmp_iter_and_init (&(ITER), (BITMAP1), (BITMAP2), (MIN), \
262cead1 904 &(BITNUM)); \
905 bmp_iter_and (&(ITER), &(BITNUM)); \
906 bmp_iter_next (&(ITER), &(BITNUM)))
907
908/* Loop over all the bits set in BITMAP1 & ~BITMAP2, starting with MIN
909 and setting BITNUM to the bit number. ITER is a bitmap iterator.
910 BITNUM should be treated as a read-only variable as it contains
911 loop state. */
912
913#define EXECUTE_IF_AND_COMPL_IN_BITMAP(BITMAP1, BITMAP2, MIN, BITNUM, ITER) \
914 for (bmp_iter_and_compl_init (&(ITER), (BITMAP1), (BITMAP2), (MIN), \
a0c938f0 915 &(BITNUM)); \
262cead1 916 bmp_iter_and_compl (&(ITER), &(BITNUM)); \
917 bmp_iter_next (&(ITER), &(BITNUM)))
f3d96a58 918
0dfcbb08 919/* A class that ties the lifetime of a bitmap to its scope. */
920class auto_bitmap
921{
922 public:
01e3184e 923 auto_bitmap () { bitmap_initialize (&m_bits, &bitmap_default_obstack); }
3ef87741 924 explicit auto_bitmap (bitmap_obstack *o) { bitmap_initialize (&m_bits, o); }
01e3184e 925 ~auto_bitmap () { bitmap_clear (&m_bits); }
0dfcbb08 926 // Allow calling bitmap functions on our bitmap.
01e3184e 927 operator bitmap () { return &m_bits; }
0dfcbb08 928
929 private:
930 // Prevent making a copy that references our bitmap.
931 auto_bitmap (const auto_bitmap &);
932 auto_bitmap &operator = (const auto_bitmap &);
933#if __cplusplus >= 201103L
934 auto_bitmap (auto_bitmap &&);
935 auto_bitmap &operator = (auto_bitmap &&);
936#endif
937
01e3184e 938 bitmap_head m_bits;
0dfcbb08 939};
940
2a281353 941#endif /* GCC_BITMAP_H */