1 /* Functions to support general ended bitmaps.
2 Copyright (C) 1997-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
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
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
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/>. */
23 /* Implementation of sparse integer sets as a linked list.
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.
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)
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.
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
53 The following operations can always be performed in O(1) time:
55 * clear : bitmap_clear
56 * choose_one : (not implemented, but could be
57 implemented in constant time)
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:
63 * member_p : bitmap_bit_p
64 * add_member : bitmap_set_bit
65 * remove_member : bitmap_clear_bit
67 The following operations can be performed in O(E) time:
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)
74 Additionally, the linked-list sparse set representation supports
75 enumeration of the members in O(E) time:
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
89 Some operations on 3 sets that occur frequently in in data flow problems
92 * A | (B & C) : bitmap_ior_and_into
93 * A | (B & ~C) : bitmap_ior_and_compl /
94 bitmap_ior_and_compl_into
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
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
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
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
116 Traversing linked lists is usually cache-unfriendly, even with the last
117 accessed element cached.
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.
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. */
131 #include "statistics.h"
133 #include "mem-stats.h"
135 /* Bitmap memory usage. */
136 struct bitmap_usage
: public mem_usage
138 /* Default contructor. */
139 bitmap_usage (): m_nsearches (0), m_search_iter (0) {}
141 bitmap_usage (size_t allocated
, size_t times
, size_t peak
,
142 uint64_t nsearches
, uint64_t search_iter
)
143 : mem_usage (allocated
, times
, peak
),
144 m_nsearches (nsearches
), m_search_iter (search_iter
) {}
146 /* Sum the usage with SECOND usage. */
147 bitmap_usage
operator+ (const bitmap_usage
&second
)
149 return bitmap_usage (m_allocated
+ second
.m_allocated
,
150 m_times
+ second
.m_times
,
151 m_peak
+ second
.m_peak
,
152 m_nsearches
+ second
.m_nsearches
,
153 m_search_iter
+ second
.m_search_iter
);
156 /* Dump usage coupled to LOC location, where TOTAL is sum of all rows. */
157 inline void dump (mem_location
*loc
, mem_usage
&total
) const
160 sprintf (s
, "%s:%i (%s)", loc
->get_trimmed_filename (),
161 loc
->m_line
, loc
->m_function
);
165 fprintf (stderr
, "%-48s %10li:%5.1f%%%10li%10li:%5.1f%%%12li%12li%10s\n", s
,
166 (long)m_allocated
, get_percent (m_allocated
, total
.m_allocated
),
167 (long)m_peak
, (long)m_times
,
168 get_percent (m_times
, total
.m_times
),
169 (long)m_nsearches
, (long)m_search_iter
,
170 loc
->m_ggc
? "ggc" : "heap");
173 /* Dump header with NAME. */
174 static inline void dump_header (const char *name
)
176 fprintf (stderr
, "%-48s %11s%16s%17s%12s%12s%10s\n", name
, "Leak", "Peak",
177 "Times", "N searches", "Search iter", "Type");
181 /* Number search operations. */
182 uint64_t m_nsearches
;
183 /* Number of search iterations. */
184 uint64_t m_search_iter
;
187 /* Bitmap memory description. */
188 extern mem_alloc_description
<bitmap_usage
> bitmap_mem_desc
;
190 /* Fundamental storage type for bitmap. */
192 typedef unsigned long BITMAP_WORD
;
193 /* BITMAP_WORD_BITS needs to be unsigned, but cannot contain casts as
194 it is used in preprocessor directives -- hence the 1u. */
195 #define BITMAP_WORD_BITS (CHAR_BIT * SIZEOF_LONG * 1u)
197 /* Number of words to use for each element in the linked list. */
199 #ifndef BITMAP_ELEMENT_WORDS
200 #define BITMAP_ELEMENT_WORDS ((128 + BITMAP_WORD_BITS - 1) / BITMAP_WORD_BITS)
203 /* Number of bits in each actual element of a bitmap. */
205 #define BITMAP_ELEMENT_ALL_BITS (BITMAP_ELEMENT_WORDS * BITMAP_WORD_BITS)
207 /* Obstack for allocating bitmaps and elements from. */
208 struct GTY (()) bitmap_obstack
{
209 struct bitmap_element
*elements
;
210 struct bitmap_head
*heads
;
211 struct obstack
GTY ((skip
)) obstack
;
214 /* Bitmap set element. We use a linked list to hold only the bits that
215 are set. This allows for use to grow the bitset dynamically without
216 having to realloc and copy a giant bit array.
218 The free list is implemented as a list of lists. There is one
219 outer list connected together by prev fields. Each element of that
220 outer is an inner list (that may consist only of the outer list
221 element) that are connected by the next fields. The prev pointer
222 is undefined for interior elements. This allows
223 bitmap_elt_clear_from to be implemented in unit time rather than
224 linear in the number of elements to be freed. */
226 struct GTY((chain_next ("%h.next"), chain_prev ("%h.prev"))) bitmap_element
{
227 struct bitmap_element
*next
; /* Next element. */
228 struct bitmap_element
*prev
; /* Previous element. */
229 unsigned int indx
; /* regno/BITMAP_ELEMENT_ALL_BITS. */
230 BITMAP_WORD bits
[BITMAP_ELEMENT_WORDS
]; /* Bits that are set. */
233 /* Head of bitmap linked list. The 'current' member points to something
234 already pointed to by the chain started by first, so GTY((skip)) it. */
236 struct GTY(()) bitmap_head
{
237 unsigned int indx
; /* Index of last element looked at. */
238 unsigned int descriptor_id
; /* Unique identifier for the allocation
239 site of this bitmap, for detailed
240 statistics gathering. */
241 bitmap_element
*first
; /* First element in linked list. */
242 bitmap_element
* GTY((skip(""))) current
; /* Last element looked at. */
243 bitmap_obstack
*obstack
; /* Obstack to allocate elements from.
244 If NULL, then use GGC allocation. */
248 extern bitmap_element bitmap_zero_bits
; /* Zero bitmap element */
249 extern bitmap_obstack bitmap_default_obstack
; /* Default bitmap obstack */
251 /* Clear a bitmap by freeing up the linked list. */
252 extern void bitmap_clear (bitmap
);
254 /* Copy a bitmap to another bitmap. */
255 extern void bitmap_copy (bitmap
, const_bitmap
);
257 /* True if two bitmaps are identical. */
258 extern bool bitmap_equal_p (const_bitmap
, const_bitmap
);
260 /* True if the bitmaps intersect (their AND is non-empty). */
261 extern bool bitmap_intersect_p (const_bitmap
, const_bitmap
);
263 /* True if the complement of the second intersects the first (their
264 AND_COMPL is non-empty). */
265 extern bool bitmap_intersect_compl_p (const_bitmap
, const_bitmap
);
267 /* True if MAP is an empty bitmap. */
268 inline bool bitmap_empty_p (const_bitmap map
)
273 /* True if the bitmap has only a single bit set. */
274 extern bool bitmap_single_bit_set_p (const_bitmap
);
276 /* Count the number of bits set in the bitmap. */
277 extern unsigned long bitmap_count_bits (const_bitmap
);
279 /* Boolean operations on bitmaps. The _into variants are two operand
280 versions that modify the first source operand. The other variants
281 are three operand versions that to not destroy the source bitmaps.
282 The operations supported are &, & ~, |, ^. */
283 extern void bitmap_and (bitmap
, const_bitmap
, const_bitmap
);
284 extern bool bitmap_and_into (bitmap
, const_bitmap
);
285 extern bool bitmap_and_compl (bitmap
, const_bitmap
, const_bitmap
);
286 extern bool bitmap_and_compl_into (bitmap
, const_bitmap
);
287 #define bitmap_compl_and(DST, A, B) bitmap_and_compl (DST, B, A)
288 extern void bitmap_compl_and_into (bitmap
, const_bitmap
);
289 extern void bitmap_clear_range (bitmap
, unsigned int, unsigned int);
290 extern void bitmap_set_range (bitmap
, unsigned int, unsigned int);
291 extern bool bitmap_ior (bitmap
, const_bitmap
, const_bitmap
);
292 extern bool bitmap_ior_into (bitmap
, const_bitmap
);
293 extern void bitmap_xor (bitmap
, const_bitmap
, const_bitmap
);
294 extern void bitmap_xor_into (bitmap
, const_bitmap
);
296 /* DST = A | (B & C). Return true if DST changes. */
297 extern bool bitmap_ior_and_into (bitmap DST
, const_bitmap B
, const_bitmap C
);
298 /* DST = A | (B & ~C). Return true if DST changes. */
299 extern bool bitmap_ior_and_compl (bitmap DST
, const_bitmap A
,
300 const_bitmap B
, const_bitmap C
);
301 /* A |= (B & ~C). Return true if A changes. */
302 extern bool bitmap_ior_and_compl_into (bitmap A
,
303 const_bitmap B
, const_bitmap C
);
305 /* Clear a single bit in a bitmap. Return true if the bit changed. */
306 extern bool bitmap_clear_bit (bitmap
, int);
308 /* Set a single bit in a bitmap. Return true if the bit changed. */
309 extern bool bitmap_set_bit (bitmap
, int);
311 /* Return true if a register is set in a register set. */
312 extern int bitmap_bit_p (bitmap
, int);
314 /* Debug functions to print a bitmap linked list. */
315 extern void debug_bitmap (const_bitmap
);
316 extern void debug_bitmap_file (FILE *, const_bitmap
);
318 /* Print a bitmap. */
319 extern void bitmap_print (FILE *, const_bitmap
, const char *, const char *);
321 /* Initialize and release a bitmap obstack. */
322 extern void bitmap_obstack_initialize (bitmap_obstack
*);
323 extern void bitmap_obstack_release (bitmap_obstack
*);
324 extern void bitmap_register (bitmap MEM_STAT_DECL
);
325 extern void dump_bitmap_statistics (void);
327 /* Initialize a bitmap header. OBSTACK indicates the bitmap obstack
328 to allocate from, NULL for GC'd bitmap. */
331 bitmap_initialize_stat (bitmap head
, bitmap_obstack
*obstack MEM_STAT_DECL
)
333 head
->first
= head
->current
= NULL
;
334 head
->obstack
= obstack
;
335 if (GATHER_STATISTICS
)
336 bitmap_register (head PASS_MEM_STAT
);
338 #define bitmap_initialize(h,o) bitmap_initialize_stat (h,o MEM_STAT_INFO)
340 /* Allocate and free bitmaps from obstack, malloc and gc'd memory. */
341 extern bitmap
bitmap_obstack_alloc_stat (bitmap_obstack
*obstack MEM_STAT_DECL
);
342 #define bitmap_obstack_alloc(t) bitmap_obstack_alloc_stat (t MEM_STAT_INFO)
343 extern bitmap
bitmap_gc_alloc_stat (ALONE_MEM_STAT_DECL
);
344 #define bitmap_gc_alloc() bitmap_gc_alloc_stat (ALONE_MEM_STAT_INFO)
345 extern void bitmap_obstack_free (bitmap
);
347 /* A few compatibility/functions macros for compatibility with sbitmaps */
348 inline void dump_bitmap (FILE *file
, const_bitmap map
)
350 bitmap_print (file
, map
, "", "\n");
352 extern void debug (const bitmap_head
&ref
);
353 extern void debug (const bitmap_head
*ptr
);
355 extern unsigned bitmap_first_set_bit (const_bitmap
);
356 extern unsigned bitmap_last_set_bit (const_bitmap
);
358 /* Compute bitmap hash (for purposes of hashing etc.) */
359 extern hashval_t
bitmap_hash (const_bitmap
);
361 /* Allocate a bitmap from a bit obstack. */
362 #define BITMAP_ALLOC(OBSTACK) bitmap_obstack_alloc (OBSTACK)
364 /* Allocate a gc'd bitmap. */
365 #define BITMAP_GGC_ALLOC() bitmap_gc_alloc ()
367 /* Do any cleanup needed on a bitmap when it is no longer used. */
368 #define BITMAP_FREE(BITMAP) \
369 ((void) (bitmap_obstack_free ((bitmap) BITMAP), (BITMAP) = (bitmap) NULL))
371 /* Iterator for bitmaps. */
373 struct bitmap_iterator
375 /* Pointer to the current bitmap element. */
376 bitmap_element
*elt1
;
378 /* Pointer to 2nd bitmap element when two are involved. */
379 bitmap_element
*elt2
;
381 /* Word within the current element. */
384 /* Contents of the actually processed word. When finding next bit
385 it is shifted right, so that the actual bit is always the least
386 significant bit of ACTUAL. */
390 /* Initialize a single bitmap iterator. START_BIT is the first bit to
394 bmp_iter_set_init (bitmap_iterator
*bi
, const_bitmap map
,
395 unsigned start_bit
, unsigned *bit_no
)
397 bi
->elt1
= map
->first
;
400 /* Advance elt1 until it is not before the block containing start_bit. */
405 bi
->elt1
= &bitmap_zero_bits
;
409 if (bi
->elt1
->indx
>= start_bit
/ BITMAP_ELEMENT_ALL_BITS
)
411 bi
->elt1
= bi
->elt1
->next
;
414 /* We might have gone past the start bit, so reinitialize it. */
415 if (bi
->elt1
->indx
!= start_bit
/ BITMAP_ELEMENT_ALL_BITS
)
416 start_bit
= bi
->elt1
->indx
* BITMAP_ELEMENT_ALL_BITS
;
418 /* Initialize for what is now start_bit. */
419 bi
->word_no
= start_bit
/ BITMAP_WORD_BITS
% BITMAP_ELEMENT_WORDS
;
420 bi
->bits
= bi
->elt1
->bits
[bi
->word_no
];
421 bi
->bits
>>= start_bit
% BITMAP_WORD_BITS
;
423 /* If this word is zero, we must make sure we're not pointing at the
424 first bit, otherwise our incrementing to the next word boundary
425 will fail. It won't matter if this increment moves us into the
427 start_bit
+= !bi
->bits
;
432 /* Initialize an iterator to iterate over the intersection of two
433 bitmaps. START_BIT is the bit to commence from. */
436 bmp_iter_and_init (bitmap_iterator
*bi
, const_bitmap map1
, const_bitmap map2
,
437 unsigned start_bit
, unsigned *bit_no
)
439 bi
->elt1
= map1
->first
;
440 bi
->elt2
= map2
->first
;
442 /* Advance elt1 until it is not before the block containing
452 if (bi
->elt1
->indx
>= start_bit
/ BITMAP_ELEMENT_ALL_BITS
)
454 bi
->elt1
= bi
->elt1
->next
;
457 /* Advance elt2 until it is not before elt1. */
462 bi
->elt1
= bi
->elt2
= &bitmap_zero_bits
;
466 if (bi
->elt2
->indx
>= bi
->elt1
->indx
)
468 bi
->elt2
= bi
->elt2
->next
;
471 /* If we're at the same index, then we have some intersecting bits. */
472 if (bi
->elt1
->indx
== bi
->elt2
->indx
)
474 /* We might have advanced beyond the start_bit, so reinitialize
476 if (bi
->elt1
->indx
!= start_bit
/ BITMAP_ELEMENT_ALL_BITS
)
477 start_bit
= bi
->elt1
->indx
* BITMAP_ELEMENT_ALL_BITS
;
479 bi
->word_no
= start_bit
/ BITMAP_WORD_BITS
% BITMAP_ELEMENT_WORDS
;
480 bi
->bits
= bi
->elt1
->bits
[bi
->word_no
] & bi
->elt2
->bits
[bi
->word_no
];
481 bi
->bits
>>= start_bit
% BITMAP_WORD_BITS
;
485 /* Otherwise we must immediately advance elt1, so initialize for
487 bi
->word_no
= BITMAP_ELEMENT_WORDS
- 1;
491 /* If this word is zero, we must make sure we're not pointing at the
492 first bit, otherwise our incrementing to the next word boundary
493 will fail. It won't matter if this increment moves us into the
495 start_bit
+= !bi
->bits
;
500 /* Initialize an iterator to iterate over the bits in MAP1 & ~MAP2.
504 bmp_iter_and_compl_init (bitmap_iterator
*bi
,
505 const_bitmap map1
, const_bitmap map2
,
506 unsigned start_bit
, unsigned *bit_no
)
508 bi
->elt1
= map1
->first
;
509 bi
->elt2
= map2
->first
;
511 /* Advance elt1 until it is not before the block containing start_bit. */
516 bi
->elt1
= &bitmap_zero_bits
;
520 if (bi
->elt1
->indx
>= start_bit
/ BITMAP_ELEMENT_ALL_BITS
)
522 bi
->elt1
= bi
->elt1
->next
;
525 /* Advance elt2 until it is not before elt1. */
526 while (bi
->elt2
&& bi
->elt2
->indx
< bi
->elt1
->indx
)
527 bi
->elt2
= bi
->elt2
->next
;
529 /* We might have advanced beyond the start_bit, so reinitialize for
531 if (bi
->elt1
->indx
!= start_bit
/ BITMAP_ELEMENT_ALL_BITS
)
532 start_bit
= bi
->elt1
->indx
* BITMAP_ELEMENT_ALL_BITS
;
534 bi
->word_no
= start_bit
/ BITMAP_WORD_BITS
% BITMAP_ELEMENT_WORDS
;
535 bi
->bits
= bi
->elt1
->bits
[bi
->word_no
];
536 if (bi
->elt2
&& bi
->elt1
->indx
== bi
->elt2
->indx
)
537 bi
->bits
&= ~bi
->elt2
->bits
[bi
->word_no
];
538 bi
->bits
>>= start_bit
% BITMAP_WORD_BITS
;
540 /* If this word is zero, we must make sure we're not pointing at the
541 first bit, otherwise our incrementing to the next word boundary
542 will fail. It won't matter if this increment moves us into the
544 start_bit
+= !bi
->bits
;
549 /* Advance to the next bit in BI. We don't advance to the next
553 bmp_iter_next (bitmap_iterator
*bi
, unsigned *bit_no
)
559 /* Advance to first set bit in BI. */
562 bmp_iter_next_bit (bitmap_iterator
* bi
, unsigned *bit_no
)
564 #if (GCC_VERSION >= 3004)
566 unsigned int n
= __builtin_ctzl (bi
->bits
);
567 gcc_assert (sizeof (unsigned long) == sizeof (BITMAP_WORD
));
572 while (!(bi
->bits
& 1))
580 /* Advance to the next nonzero bit of a single bitmap, we will have
581 already advanced past the just iterated bit. Return true if there
582 is a bit to iterate. */
585 bmp_iter_set (bitmap_iterator
*bi
, unsigned *bit_no
)
587 /* If our current word is nonzero, it contains the bit we want. */
591 bmp_iter_next_bit (bi
, bit_no
);
595 /* Round up to the word boundary. We might have just iterated past
596 the end of the last word, hence the -1. It is not possible for
597 bit_no to point at the beginning of the now last word. */
598 *bit_no
= ((*bit_no
+ BITMAP_WORD_BITS
- 1)
599 / BITMAP_WORD_BITS
* BITMAP_WORD_BITS
);
604 /* Find the next nonzero word in this elt. */
605 while (bi
->word_no
!= BITMAP_ELEMENT_WORDS
)
607 bi
->bits
= bi
->elt1
->bits
[bi
->word_no
];
610 *bit_no
+= BITMAP_WORD_BITS
;
614 /* Advance to the next element. */
615 bi
->elt1
= bi
->elt1
->next
;
618 *bit_no
= bi
->elt1
->indx
* BITMAP_ELEMENT_ALL_BITS
;
623 /* Advance to the next nonzero bit of an intersecting pair of
624 bitmaps. We will have already advanced past the just iterated bit.
625 Return true if there is a bit to iterate. */
628 bmp_iter_and (bitmap_iterator
*bi
, unsigned *bit_no
)
630 /* If our current word is nonzero, it contains the bit we want. */
634 bmp_iter_next_bit (bi
, bit_no
);
638 /* Round up to the word boundary. We might have just iterated past
639 the end of the last word, hence the -1. It is not possible for
640 bit_no to point at the beginning of the now last word. */
641 *bit_no
= ((*bit_no
+ BITMAP_WORD_BITS
- 1)
642 / BITMAP_WORD_BITS
* BITMAP_WORD_BITS
);
647 /* Find the next nonzero word in this elt. */
648 while (bi
->word_no
!= BITMAP_ELEMENT_WORDS
)
650 bi
->bits
= bi
->elt1
->bits
[bi
->word_no
] & bi
->elt2
->bits
[bi
->word_no
];
653 *bit_no
+= BITMAP_WORD_BITS
;
657 /* Advance to the next identical element. */
660 /* Advance elt1 while it is less than elt2. We always want
661 to advance one elt. */
664 bi
->elt1
= bi
->elt1
->next
;
668 while (bi
->elt1
->indx
< bi
->elt2
->indx
);
670 /* Advance elt2 to be no less than elt1. This might not
672 while (bi
->elt2
->indx
< bi
->elt1
->indx
)
674 bi
->elt2
= bi
->elt2
->next
;
679 while (bi
->elt1
->indx
!= bi
->elt2
->indx
);
681 *bit_no
= bi
->elt1
->indx
* BITMAP_ELEMENT_ALL_BITS
;
686 /* Advance to the next nonzero bit in the intersection of
687 complemented bitmaps. We will have already advanced past the just
691 bmp_iter_and_compl (bitmap_iterator
*bi
, unsigned *bit_no
)
693 /* If our current word is nonzero, it contains the bit we want. */
697 bmp_iter_next_bit (bi
, bit_no
);
701 /* Round up to the word boundary. We might have just iterated past
702 the end of the last word, hence the -1. It is not possible for
703 bit_no to point at the beginning of the now last word. */
704 *bit_no
= ((*bit_no
+ BITMAP_WORD_BITS
- 1)
705 / BITMAP_WORD_BITS
* BITMAP_WORD_BITS
);
710 /* Find the next nonzero word in this elt. */
711 while (bi
->word_no
!= BITMAP_ELEMENT_WORDS
)
713 bi
->bits
= bi
->elt1
->bits
[bi
->word_no
];
714 if (bi
->elt2
&& bi
->elt2
->indx
== bi
->elt1
->indx
)
715 bi
->bits
&= ~bi
->elt2
->bits
[bi
->word_no
];
718 *bit_no
+= BITMAP_WORD_BITS
;
722 /* Advance to the next element of elt1. */
723 bi
->elt1
= bi
->elt1
->next
;
727 /* Advance elt2 until it is no less than elt1. */
728 while (bi
->elt2
&& bi
->elt2
->indx
< bi
->elt1
->indx
)
729 bi
->elt2
= bi
->elt2
->next
;
731 *bit_no
= bi
->elt1
->indx
* BITMAP_ELEMENT_ALL_BITS
;
736 /* Loop over all bits set in BITMAP, starting with MIN and setting
737 BITNUM to the bit number. ITER is a bitmap iterator. BITNUM
738 should be treated as a read-only variable as it contains loop
741 #ifndef EXECUTE_IF_SET_IN_BITMAP
742 /* See sbitmap.h for the other definition of EXECUTE_IF_SET_IN_BITMAP. */
743 #define EXECUTE_IF_SET_IN_BITMAP(BITMAP, MIN, BITNUM, ITER) \
744 for (bmp_iter_set_init (&(ITER), (BITMAP), (MIN), &(BITNUM)); \
745 bmp_iter_set (&(ITER), &(BITNUM)); \
746 bmp_iter_next (&(ITER), &(BITNUM)))
749 /* Loop over all the bits set in BITMAP1 & BITMAP2, starting with MIN
750 and setting BITNUM to the bit number. ITER is a bitmap iterator.
751 BITNUM should be treated as a read-only variable as it contains
754 #define EXECUTE_IF_AND_IN_BITMAP(BITMAP1, BITMAP2, MIN, BITNUM, ITER) \
755 for (bmp_iter_and_init (&(ITER), (BITMAP1), (BITMAP2), (MIN), \
757 bmp_iter_and (&(ITER), &(BITNUM)); \
758 bmp_iter_next (&(ITER), &(BITNUM)))
760 /* Loop over all the bits set in BITMAP1 & ~BITMAP2, starting with MIN
761 and setting BITNUM to the bit number. ITER is a bitmap iterator.
762 BITNUM should be treated as a read-only variable as it contains
765 #define EXECUTE_IF_AND_COMPL_IN_BITMAP(BITMAP1, BITMAP2, MIN, BITNUM, ITER) \
766 for (bmp_iter_and_compl_init (&(ITER), (BITMAP1), (BITMAP2), (MIN), \
768 bmp_iter_and_compl (&(ITER), &(BITNUM)); \
769 bmp_iter_next (&(ITER), &(BITNUM)))
771 #endif /* GCC_BITMAP_H */