]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/bitmap.h
bitmap.c (bitmap_head::dump): New.
[thirdparty/gcc.git] / gcc / bitmap.h
1 /* Functions to support general ended bitmaps.
2 Copyright (C) 1997-2018 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10
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.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
19
20 #ifndef GCC_BITMAP_H
21 #define GCC_BITMAP_H
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
27 list of container nodes (struct bitmap_element). Each node consists
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
89 Some operations on 3 sets that occur frequently in data flow problems
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
130 #include "obstack.h"
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. */
144 bitmap_usage
145 operator+ (const bitmap_usage &second)
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. */
155 inline void
156 dump (mem_location *loc, mem_usage &total) const
157 {
158 char *location_string = loc->to_string ();
159
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,
166 get_percent (m_times, total.m_times),
167 m_nsearches, m_search_iter,
168 loc->m_ggc ? "ggc" : "heap");
169
170 free (location_string);
171 }
172
173 /* Dump header with NAME. */
174 static inline void
175 dump_header (const char *name)
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;
190
191 /* Fundamental storage type for bitmap. */
192
193 typedef unsigned long BITMAP_WORD;
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)
197
198 /* Number of words to use for each element in the linked list. */
199
200 #ifndef BITMAP_ELEMENT_WORDS
201 #define BITMAP_ELEMENT_WORDS ((128 + BITMAP_WORD_BITS - 1) / BITMAP_WORD_BITS)
202 #endif
203
204 /* Number of bits in each actual element of a bitmap. */
205
206 #define BITMAP_ELEMENT_ALL_BITS (BITMAP_ELEMENT_WORDS * BITMAP_WORD_BITS)
207
208 /* Obstack for allocating bitmaps and elements from. */
209 struct GTY (()) bitmap_obstack {
210 struct bitmap_element *elements;
211 struct bitmap_head *heads;
212 struct obstack GTY ((skip)) obstack;
213 };
214
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
217 having to realloc and copy a giant bit array.
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. */
226
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. */
230 unsigned int indx; /* regno/BITMAP_ELEMENT_ALL_BITS. */
231 BITMAP_WORD bits[BITMAP_ELEMENT_WORDS]; /* Bits that are set. */
232 };
233
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. */
236
237 struct GTY(()) bitmap_head {
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. */
242 bitmap_element *first; /* First element in linked list. */
243 bitmap_element * GTY((skip(""))) current; /* Last element looked at. */
244 bitmap_obstack *obstack; /* Obstack to allocate elements from.
245 If NULL, then use GGC allocation. */
246 void dump ();
247 };
248
249 /* Global data */
250 extern bitmap_element bitmap_zero_bits; /* Zero bitmap element */
251 extern bitmap_obstack bitmap_default_obstack; /* Default bitmap obstack */
252
253 /* Clear a bitmap by freeing up the linked list. */
254 extern void bitmap_clear (bitmap);
255
256 /* Copy a bitmap to another bitmap. */
257 extern void bitmap_copy (bitmap, const_bitmap);
258
259 /* Move a bitmap to another bitmap. */
260 extern void bitmap_move (bitmap, bitmap);
261
262 /* True if two bitmaps are identical. */
263 extern bool bitmap_equal_p (const_bitmap, const_bitmap);
264
265 /* True if the bitmaps intersect (their AND is non-empty). */
266 extern bool bitmap_intersect_p (const_bitmap, const_bitmap);
267
268 /* True if the complement of the second intersects the first (their
269 AND_COMPL is non-empty). */
270 extern bool bitmap_intersect_compl_p (const_bitmap, const_bitmap);
271
272 /* True if MAP is an empty bitmap. */
273 inline bool bitmap_empty_p (const_bitmap map)
274 {
275 return !map->first;
276 }
277
278 /* True if the bitmap has only a single bit set. */
279 extern bool bitmap_single_bit_set_p (const_bitmap);
280
281 /* Count the number of bits set in the bitmap. */
282 extern unsigned long bitmap_count_bits (const_bitmap);
283
284 /* Count the number of unique bits set across the two bitmaps. */
285 extern unsigned long bitmap_count_unique_bits (const_bitmap, const_bitmap);
286
287 /* Boolean operations on bitmaps. The _into variants are two operand
288 versions that modify the first source operand. The other variants
289 are three operand versions that to not destroy the source bitmaps.
290 The operations supported are &, & ~, |, ^. */
291 extern void bitmap_and (bitmap, const_bitmap, const_bitmap);
292 extern bool bitmap_and_into (bitmap, const_bitmap);
293 extern bool bitmap_and_compl (bitmap, const_bitmap, const_bitmap);
294 extern bool bitmap_and_compl_into (bitmap, const_bitmap);
295 #define bitmap_compl_and(DST, A, B) bitmap_and_compl (DST, B, A)
296 extern void bitmap_compl_and_into (bitmap, const_bitmap);
297 extern void bitmap_clear_range (bitmap, unsigned int, unsigned int);
298 extern void bitmap_set_range (bitmap, unsigned int, unsigned int);
299 extern bool bitmap_ior (bitmap, const_bitmap, const_bitmap);
300 extern bool bitmap_ior_into (bitmap, const_bitmap);
301 extern void bitmap_xor (bitmap, const_bitmap, const_bitmap);
302 extern void bitmap_xor_into (bitmap, const_bitmap);
303
304 /* DST = A | (B & C). Return true if DST changes. */
305 extern bool bitmap_ior_and_into (bitmap DST, const_bitmap B, const_bitmap C);
306 /* DST = A | (B & ~C). Return true if DST changes. */
307 extern bool bitmap_ior_and_compl (bitmap DST, const_bitmap A,
308 const_bitmap B, const_bitmap C);
309 /* A |= (B & ~C). Return true if A changes. */
310 extern bool bitmap_ior_and_compl_into (bitmap A,
311 const_bitmap B, const_bitmap C);
312
313 /* Clear a single bit in a bitmap. Return true if the bit changed. */
314 extern bool bitmap_clear_bit (bitmap, int);
315
316 /* Set a single bit in a bitmap. Return true if the bit changed. */
317 extern bool bitmap_set_bit (bitmap, int);
318
319 /* Return true if a register is set in a register set. */
320 extern int bitmap_bit_p (bitmap, int);
321
322 /* Debug functions to print a bitmap linked list. */
323 extern void debug_bitmap (const_bitmap);
324 extern void debug_bitmap_file (FILE *, const_bitmap);
325
326 /* Print a bitmap. */
327 extern void bitmap_print (FILE *, const_bitmap, const char *, const char *);
328
329 /* Initialize and release a bitmap obstack. */
330 extern void bitmap_obstack_initialize (bitmap_obstack *);
331 extern void bitmap_obstack_release (bitmap_obstack *);
332 extern void bitmap_register (bitmap MEM_STAT_DECL);
333 extern void dump_bitmap_statistics (void);
334
335 /* Initialize a bitmap header. OBSTACK indicates the bitmap obstack
336 to allocate from, NULL for GC'd bitmap. */
337
338 static inline void
339 bitmap_initialize (bitmap head, bitmap_obstack *obstack CXX_MEM_STAT_INFO)
340 {
341 head->first = head->current = NULL;
342 head->obstack = obstack;
343 if (GATHER_STATISTICS)
344 bitmap_register (head PASS_MEM_STAT);
345 }
346
347 /* Allocate and free bitmaps from obstack, malloc and gc'd memory. */
348 extern bitmap bitmap_alloc (bitmap_obstack *obstack CXX_MEM_STAT_INFO);
349 #define BITMAP_ALLOC bitmap_alloc
350 extern bitmap bitmap_gc_alloc (ALONE_CXX_MEM_STAT_INFO);
351 #define BITMAP_GGC_ALLOC bitmap_gc_alloc
352 extern void bitmap_obstack_free (bitmap);
353
354 /* A few compatibility/functions macros for compatibility with sbitmaps */
355 inline void dump_bitmap (FILE *file, const_bitmap map)
356 {
357 bitmap_print (file, map, "", "\n");
358 }
359 extern void debug (const bitmap_head &ref);
360 extern void debug (const bitmap_head *ptr);
361
362 extern unsigned bitmap_first_set_bit (const_bitmap);
363 extern unsigned bitmap_last_set_bit (const_bitmap);
364
365 /* Compute bitmap hash (for purposes of hashing etc.) */
366 extern hashval_t bitmap_hash (const_bitmap);
367
368 /* Do any cleanup needed on a bitmap when it is no longer used. */
369 #define BITMAP_FREE(BITMAP) \
370 ((void) (bitmap_obstack_free ((bitmap) BITMAP), (BITMAP) = (bitmap) NULL))
371
372 /* Iterator for bitmaps. */
373
374 struct bitmap_iterator
375 {
376 /* Pointer to the current bitmap element. */
377 bitmap_element *elt1;
378
379 /* Pointer to 2nd bitmap element when two are involved. */
380 bitmap_element *elt2;
381
382 /* Word within the current element. */
383 unsigned word_no;
384
385 /* Contents of the actually processed word. When finding next bit
386 it is shifted right, so that the actual bit is always the least
387 significant bit of ACTUAL. */
388 BITMAP_WORD bits;
389 };
390
391 /* Initialize a single bitmap iterator. START_BIT is the first bit to
392 iterate from. */
393
394 static inline void
395 bmp_iter_set_init (bitmap_iterator *bi, const_bitmap map,
396 unsigned start_bit, unsigned *bit_no)
397 {
398 bi->elt1 = map->first;
399 bi->elt2 = NULL;
400
401 /* Advance elt1 until it is not before the block containing start_bit. */
402 while (1)
403 {
404 if (!bi->elt1)
405 {
406 bi->elt1 = &bitmap_zero_bits;
407 break;
408 }
409
410 if (bi->elt1->indx >= start_bit / BITMAP_ELEMENT_ALL_BITS)
411 break;
412 bi->elt1 = bi->elt1->next;
413 }
414
415 /* We might have gone past the start bit, so reinitialize it. */
416 if (bi->elt1->indx != start_bit / BITMAP_ELEMENT_ALL_BITS)
417 start_bit = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
418
419 /* Initialize for what is now start_bit. */
420 bi->word_no = start_bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
421 bi->bits = bi->elt1->bits[bi->word_no];
422 bi->bits >>= start_bit % BITMAP_WORD_BITS;
423
424 /* If this word is zero, we must make sure we're not pointing at the
425 first bit, otherwise our incrementing to the next word boundary
426 will fail. It won't matter if this increment moves us into the
427 next word. */
428 start_bit += !bi->bits;
429
430 *bit_no = start_bit;
431 }
432
433 /* Initialize an iterator to iterate over the intersection of two
434 bitmaps. START_BIT is the bit to commence from. */
435
436 static inline void
437 bmp_iter_and_init (bitmap_iterator *bi, const_bitmap map1, const_bitmap map2,
438 unsigned start_bit, unsigned *bit_no)
439 {
440 bi->elt1 = map1->first;
441 bi->elt2 = map2->first;
442
443 /* Advance elt1 until it is not before the block containing
444 start_bit. */
445 while (1)
446 {
447 if (!bi->elt1)
448 {
449 bi->elt2 = NULL;
450 break;
451 }
452
453 if (bi->elt1->indx >= start_bit / BITMAP_ELEMENT_ALL_BITS)
454 break;
455 bi->elt1 = bi->elt1->next;
456 }
457
458 /* Advance elt2 until it is not before elt1. */
459 while (1)
460 {
461 if (!bi->elt2)
462 {
463 bi->elt1 = bi->elt2 = &bitmap_zero_bits;
464 break;
465 }
466
467 if (bi->elt2->indx >= bi->elt1->indx)
468 break;
469 bi->elt2 = bi->elt2->next;
470 }
471
472 /* If we're at the same index, then we have some intersecting bits. */
473 if (bi->elt1->indx == bi->elt2->indx)
474 {
475 /* We might have advanced beyond the start_bit, so reinitialize
476 for that. */
477 if (bi->elt1->indx != start_bit / BITMAP_ELEMENT_ALL_BITS)
478 start_bit = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
479
480 bi->word_no = start_bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
481 bi->bits = bi->elt1->bits[bi->word_no] & bi->elt2->bits[bi->word_no];
482 bi->bits >>= start_bit % BITMAP_WORD_BITS;
483 }
484 else
485 {
486 /* Otherwise we must immediately advance elt1, so initialize for
487 that. */
488 bi->word_no = BITMAP_ELEMENT_WORDS - 1;
489 bi->bits = 0;
490 }
491
492 /* If this word is zero, we must make sure we're not pointing at the
493 first bit, otherwise our incrementing to the next word boundary
494 will fail. It won't matter if this increment moves us into the
495 next word. */
496 start_bit += !bi->bits;
497
498 *bit_no = start_bit;
499 }
500
501 /* Initialize an iterator to iterate over the bits in MAP1 & ~MAP2.
502 */
503
504 static inline void
505 bmp_iter_and_compl_init (bitmap_iterator *bi,
506 const_bitmap map1, const_bitmap map2,
507 unsigned start_bit, unsigned *bit_no)
508 {
509 bi->elt1 = map1->first;
510 bi->elt2 = map2->first;
511
512 /* Advance elt1 until it is not before the block containing start_bit. */
513 while (1)
514 {
515 if (!bi->elt1)
516 {
517 bi->elt1 = &bitmap_zero_bits;
518 break;
519 }
520
521 if (bi->elt1->indx >= start_bit / BITMAP_ELEMENT_ALL_BITS)
522 break;
523 bi->elt1 = bi->elt1->next;
524 }
525
526 /* Advance elt2 until it is not before elt1. */
527 while (bi->elt2 && bi->elt2->indx < bi->elt1->indx)
528 bi->elt2 = bi->elt2->next;
529
530 /* We might have advanced beyond the start_bit, so reinitialize for
531 that. */
532 if (bi->elt1->indx != start_bit / BITMAP_ELEMENT_ALL_BITS)
533 start_bit = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
534
535 bi->word_no = start_bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
536 bi->bits = bi->elt1->bits[bi->word_no];
537 if (bi->elt2 && bi->elt1->indx == bi->elt2->indx)
538 bi->bits &= ~bi->elt2->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;
546
547 *bit_no = start_bit;
548 }
549
550 /* Advance to the next bit in BI. We don't advance to the next
551 nonzero bit yet. */
552
553 static inline void
554 bmp_iter_next (bitmap_iterator *bi, unsigned *bit_no)
555 {
556 bi->bits >>= 1;
557 *bit_no += 1;
558 }
559
560 /* Advance to first set bit in BI. */
561
562 static inline void
563 bmp_iter_next_bit (bitmap_iterator * bi, unsigned *bit_no)
564 {
565 #if (GCC_VERSION >= 3004)
566 {
567 unsigned int n = __builtin_ctzl (bi->bits);
568 gcc_assert (sizeof (unsigned long) == sizeof (BITMAP_WORD));
569 bi->bits >>= n;
570 *bit_no += n;
571 }
572 #else
573 while (!(bi->bits & 1))
574 {
575 bi->bits >>= 1;
576 *bit_no += 1;
577 }
578 #endif
579 }
580
581 /* Advance to the next nonzero bit of a single bitmap, we will have
582 already advanced past the just iterated bit. Return true if there
583 is a bit to iterate. */
584
585 static inline bool
586 bmp_iter_set (bitmap_iterator *bi, unsigned *bit_no)
587 {
588 /* If our current word is nonzero, it contains the bit we want. */
589 if (bi->bits)
590 {
591 next_bit:
592 bmp_iter_next_bit (bi, bit_no);
593 return true;
594 }
595
596 /* Round up to the word boundary. We might have just iterated past
597 the end of the last word, hence the -1. It is not possible for
598 bit_no to point at the beginning of the now last word. */
599 *bit_no = ((*bit_no + BITMAP_WORD_BITS - 1)
600 / BITMAP_WORD_BITS * BITMAP_WORD_BITS);
601 bi->word_no++;
602
603 while (1)
604 {
605 /* Find the next nonzero word in this elt. */
606 while (bi->word_no != BITMAP_ELEMENT_WORDS)
607 {
608 bi->bits = bi->elt1->bits[bi->word_no];
609 if (bi->bits)
610 goto next_bit;
611 *bit_no += BITMAP_WORD_BITS;
612 bi->word_no++;
613 }
614
615 /* Make sure we didn't remove the element while iterating. */
616 gcc_checking_assert (bi->elt1->indx != -1U);
617
618 /* Advance to the next element. */
619 bi->elt1 = bi->elt1->next;
620 if (!bi->elt1)
621 return false;
622 *bit_no = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
623 bi->word_no = 0;
624 }
625 }
626
627 /* Advance to the next nonzero bit of an intersecting pair of
628 bitmaps. We will have already advanced past the just iterated bit.
629 Return true if there is a bit to iterate. */
630
631 static inline bool
632 bmp_iter_and (bitmap_iterator *bi, unsigned *bit_no)
633 {
634 /* If our current word is nonzero, it contains the bit we want. */
635 if (bi->bits)
636 {
637 next_bit:
638 bmp_iter_next_bit (bi, bit_no);
639 return true;
640 }
641
642 /* Round up to the word boundary. We might have just iterated past
643 the end of the last word, hence the -1. It is not possible for
644 bit_no to point at the beginning of the now last word. */
645 *bit_no = ((*bit_no + BITMAP_WORD_BITS - 1)
646 / BITMAP_WORD_BITS * BITMAP_WORD_BITS);
647 bi->word_no++;
648
649 while (1)
650 {
651 /* Find the next nonzero word in this elt. */
652 while (bi->word_no != BITMAP_ELEMENT_WORDS)
653 {
654 bi->bits = bi->elt1->bits[bi->word_no] & bi->elt2->bits[bi->word_no];
655 if (bi->bits)
656 goto next_bit;
657 *bit_no += BITMAP_WORD_BITS;
658 bi->word_no++;
659 }
660
661 /* Advance to the next identical element. */
662 do
663 {
664 /* Make sure we didn't remove the element while iterating. */
665 gcc_checking_assert (bi->elt1->indx != -1U);
666
667 /* Advance elt1 while it is less than elt2. We always want
668 to advance one elt. */
669 do
670 {
671 bi->elt1 = bi->elt1->next;
672 if (!bi->elt1)
673 return false;
674 }
675 while (bi->elt1->indx < bi->elt2->indx);
676
677 /* Make sure we didn't remove the element while iterating. */
678 gcc_checking_assert (bi->elt2->indx != -1U);
679
680 /* Advance elt2 to be no less than elt1. This might not
681 advance. */
682 while (bi->elt2->indx < bi->elt1->indx)
683 {
684 bi->elt2 = bi->elt2->next;
685 if (!bi->elt2)
686 return false;
687 }
688 }
689 while (bi->elt1->indx != bi->elt2->indx);
690
691 *bit_no = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
692 bi->word_no = 0;
693 }
694 }
695
696 /* Advance to the next nonzero bit in the intersection of
697 complemented bitmaps. We will have already advanced past the just
698 iterated bit. */
699
700 static inline bool
701 bmp_iter_and_compl (bitmap_iterator *bi, unsigned *bit_no)
702 {
703 /* If our current word is nonzero, it contains the bit we want. */
704 if (bi->bits)
705 {
706 next_bit:
707 bmp_iter_next_bit (bi, bit_no);
708 return true;
709 }
710
711 /* Round up to the word boundary. We might have just iterated past
712 the end of the last word, hence the -1. It is not possible for
713 bit_no to point at the beginning of the now last word. */
714 *bit_no = ((*bit_no + BITMAP_WORD_BITS - 1)
715 / BITMAP_WORD_BITS * BITMAP_WORD_BITS);
716 bi->word_no++;
717
718 while (1)
719 {
720 /* Find the next nonzero word in this elt. */
721 while (bi->word_no != BITMAP_ELEMENT_WORDS)
722 {
723 bi->bits = bi->elt1->bits[bi->word_no];
724 if (bi->elt2 && bi->elt2->indx == bi->elt1->indx)
725 bi->bits &= ~bi->elt2->bits[bi->word_no];
726 if (bi->bits)
727 goto next_bit;
728 *bit_no += BITMAP_WORD_BITS;
729 bi->word_no++;
730 }
731
732 /* Make sure we didn't remove the element while iterating. */
733 gcc_checking_assert (bi->elt1->indx != -1U);
734
735 /* Advance to the next element of elt1. */
736 bi->elt1 = bi->elt1->next;
737 if (!bi->elt1)
738 return false;
739
740 /* Make sure we didn't remove the element while iterating. */
741 gcc_checking_assert (! bi->elt2 || bi->elt2->indx != -1U);
742
743 /* Advance elt2 until it is no less than elt1. */
744 while (bi->elt2 && bi->elt2->indx < bi->elt1->indx)
745 bi->elt2 = bi->elt2->next;
746
747 *bit_no = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
748 bi->word_no = 0;
749 }
750 }
751
752 /* If you are modifying a bitmap you are currently iterating over you
753 have to ensure to
754 - never remove the current bit;
755 - if you set or clear a bit before the current bit this operation
756 will not affect the set of bits you are visiting during the iteration;
757 - if you set or clear a bit after the current bit it is unspecified
758 whether that affects the set of bits you are visiting during the
759 iteration.
760 If you want to remove the current bit you can delay this to the next
761 iteration (and after the iteration in case the last iteration is
762 affected). */
763
764 /* Loop over all bits set in BITMAP, starting with MIN and setting
765 BITNUM to the bit number. ITER is a bitmap iterator. BITNUM
766 should be treated as a read-only variable as it contains loop
767 state. */
768
769 #ifndef EXECUTE_IF_SET_IN_BITMAP
770 /* See sbitmap.h for the other definition of EXECUTE_IF_SET_IN_BITMAP. */
771 #define EXECUTE_IF_SET_IN_BITMAP(BITMAP, MIN, BITNUM, ITER) \
772 for (bmp_iter_set_init (&(ITER), (BITMAP), (MIN), &(BITNUM)); \
773 bmp_iter_set (&(ITER), &(BITNUM)); \
774 bmp_iter_next (&(ITER), &(BITNUM)))
775 #endif
776
777 /* Loop over all the bits set in BITMAP1 & BITMAP2, starting with MIN
778 and setting BITNUM to the bit number. ITER is a bitmap iterator.
779 BITNUM should be treated as a read-only variable as it contains
780 loop state. */
781
782 #define EXECUTE_IF_AND_IN_BITMAP(BITMAP1, BITMAP2, MIN, BITNUM, ITER) \
783 for (bmp_iter_and_init (&(ITER), (BITMAP1), (BITMAP2), (MIN), \
784 &(BITNUM)); \
785 bmp_iter_and (&(ITER), &(BITNUM)); \
786 bmp_iter_next (&(ITER), &(BITNUM)))
787
788 /* Loop over all the bits set in BITMAP1 & ~BITMAP2, starting with MIN
789 and setting BITNUM to the bit number. ITER is a bitmap iterator.
790 BITNUM should be treated as a read-only variable as it contains
791 loop state. */
792
793 #define EXECUTE_IF_AND_COMPL_IN_BITMAP(BITMAP1, BITMAP2, MIN, BITNUM, ITER) \
794 for (bmp_iter_and_compl_init (&(ITER), (BITMAP1), (BITMAP2), (MIN), \
795 &(BITNUM)); \
796 bmp_iter_and_compl (&(ITER), &(BITNUM)); \
797 bmp_iter_next (&(ITER), &(BITNUM)))
798
799 /* A class that ties the lifetime of a bitmap to its scope. */
800 class auto_bitmap
801 {
802 public:
803 auto_bitmap () { bitmap_initialize (&m_bits, &bitmap_default_obstack); }
804 explicit auto_bitmap (bitmap_obstack *o) { bitmap_initialize (&m_bits, o); }
805 ~auto_bitmap () { bitmap_clear (&m_bits); }
806 // Allow calling bitmap functions on our bitmap.
807 operator bitmap () { return &m_bits; }
808
809 private:
810 // Prevent making a copy that references our bitmap.
811 auto_bitmap (const auto_bitmap &);
812 auto_bitmap &operator = (const auto_bitmap &);
813 #if __cplusplus >= 201103L
814 auto_bitmap (auto_bitmap &&);
815 auto_bitmap &operator = (auto_bitmap &&);
816 #endif
817
818 bitmap_head m_bits;
819 };
820
821 #endif /* GCC_BITMAP_H */