Home | History | Annotate | Download | only in include
      1 /* Functions to support general ended bitmaps.
      2    Copyright (C) 1997-2013 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_def).  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 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 "hashtab.h"
    131 #include "statistics.h"
    132 #include "obstack.h"
    133 
    134 /* Fundamental storage type for bitmap.  */
    135 
    136 typedef unsigned long BITMAP_WORD;
    137 /* BITMAP_WORD_BITS needs to be unsigned, but cannot contain casts as
    138    it is used in preprocessor directives -- hence the 1u.  */
    139 #define BITMAP_WORD_BITS (CHAR_BIT * SIZEOF_LONG * 1u)
    140 
    141 /* Number of words to use for each element in the linked list.  */
    142 
    143 #ifndef BITMAP_ELEMENT_WORDS
    144 #define BITMAP_ELEMENT_WORDS ((128 + BITMAP_WORD_BITS - 1) / BITMAP_WORD_BITS)
    145 #endif
    146 
    147 /* Number of bits in each actual element of a bitmap.  */
    148 
    149 #define BITMAP_ELEMENT_ALL_BITS (BITMAP_ELEMENT_WORDS * BITMAP_WORD_BITS)
    150 
    151 /* Obstack for allocating bitmaps and elements from.  */
    152 typedef struct GTY (()) bitmap_obstack {
    153   struct bitmap_element_def *elements;
    154   struct bitmap_head_def *heads;
    155   struct obstack GTY ((skip)) obstack;
    156 } bitmap_obstack;
    157 
    158 /* Bitmap set element.  We use a linked list to hold only the bits that
    159    are set.  This allows for use to grow the bitset dynamically without
    160    having to realloc and copy a giant bit array.
    161 
    162    The free list is implemented as a list of lists.  There is one
    163    outer list connected together by prev fields.  Each element of that
    164    outer is an inner list (that may consist only of the outer list
    165    element) that are connected by the next fields.  The prev pointer
    166    is undefined for interior elements.  This allows
    167    bitmap_elt_clear_from to be implemented in unit time rather than
    168    linear in the number of elements to be freed.  */
    169 
    170 typedef struct GTY((chain_next ("%h.next"), chain_prev ("%h.prev"))) bitmap_element_def {
    171   struct bitmap_element_def *next;	/* Next element.  */
    172   struct bitmap_element_def *prev;	/* Previous element.  */
    173   unsigned int indx;			/* regno/BITMAP_ELEMENT_ALL_BITS.  */
    174   BITMAP_WORD bits[BITMAP_ELEMENT_WORDS]; /* Bits that are set.  */
    175 } bitmap_element;
    176 
    177 /* Head of bitmap linked list.  The 'current' member points to something
    178    already pointed to by the chain started by first, so GTY((skip)) it.  */
    179 
    180 typedef struct GTY(()) bitmap_head_def {
    181   unsigned int indx;			/* Index of last element looked at.  */
    182   unsigned int descriptor_id;		/* Unique identifier for the allocation
    183 					   site of this bitmap, for detailed
    184 					   statistics gathering.  */
    185   bitmap_element *first;		/* First element in linked list.  */
    186   bitmap_element * GTY((skip(""))) current; /* Last element looked at.  */
    187   bitmap_obstack *obstack;		/* Obstack to allocate elements from.
    188 					   If NULL, then use GGC allocation.  */
    189 } bitmap_head;
    190 
    191 /* Global data */
    192 extern bitmap_element bitmap_zero_bits;	/* Zero bitmap element */
    193 extern bitmap_obstack bitmap_default_obstack;   /* Default bitmap obstack */
    194 
    195 /* Clear a bitmap by freeing up the linked list.  */
    196 extern void bitmap_clear (bitmap);
    197 
    198 /* Copy a bitmap to another bitmap.  */
    199 extern void bitmap_copy (bitmap, const_bitmap);
    200 
    201 /* True if two bitmaps are identical.  */
    202 extern bool bitmap_equal_p (const_bitmap, const_bitmap);
    203 
    204 /* True if the bitmaps intersect (their AND is non-empty).  */
    205 extern bool bitmap_intersect_p (const_bitmap, const_bitmap);
    206 
    207 /* True if the complement of the second intersects the first (their
    208    AND_COMPL is non-empty).  */
    209 extern bool bitmap_intersect_compl_p (const_bitmap, const_bitmap);
    210 
    211 /* True if MAP is an empty bitmap.  */
    212 inline bool bitmap_empty_p (const_bitmap map)
    213 {
    214   return !map->first;
    215 }
    216 
    217 /* True if the bitmap has only a single bit set.  */
    218 extern bool bitmap_single_bit_set_p (const_bitmap);
    219 
    220 /* Count the number of bits set in the bitmap.  */
    221 extern unsigned long bitmap_count_bits (const_bitmap);
    222 
    223 /* Boolean operations on bitmaps.  The _into variants are two operand
    224    versions that modify the first source operand.  The other variants
    225    are three operand versions that to not destroy the source bitmaps.
    226    The operations supported are &, & ~, |, ^.  */
    227 extern void bitmap_and (bitmap, const_bitmap, const_bitmap);
    228 extern bool bitmap_and_into (bitmap, const_bitmap);
    229 extern bool bitmap_and_compl (bitmap, const_bitmap, const_bitmap);
    230 extern bool bitmap_and_compl_into (bitmap, const_bitmap);
    231 #define bitmap_compl_and(DST, A, B) bitmap_and_compl (DST, B, A)
    232 extern void bitmap_compl_and_into (bitmap, const_bitmap);
    233 extern void bitmap_clear_range (bitmap, unsigned int, unsigned int);
    234 extern void bitmap_set_range (bitmap, unsigned int, unsigned int);
    235 extern bool bitmap_ior (bitmap, const_bitmap, const_bitmap);
    236 extern bool bitmap_ior_into (bitmap, const_bitmap);
    237 extern void bitmap_xor (bitmap, const_bitmap, const_bitmap);
    238 extern void bitmap_xor_into (bitmap, const_bitmap);
    239 
    240 /* DST = A | (B & C).  Return true if DST changes.  */
    241 extern bool bitmap_ior_and_into (bitmap DST, const_bitmap B, const_bitmap C);
    242 /* DST = A | (B & ~C).  Return true if DST changes.  */
    243 extern bool bitmap_ior_and_compl (bitmap DST, const_bitmap A,
    244 				  const_bitmap B, const_bitmap C);
    245 /* A |= (B & ~C).  Return true if A changes.  */
    246 extern bool bitmap_ior_and_compl_into (bitmap A,
    247 				       const_bitmap B, const_bitmap C);
    248 
    249 /* Clear a single bit in a bitmap.  Return true if the bit changed.  */
    250 extern bool bitmap_clear_bit (bitmap, int);
    251 
    252 /* Set a single bit in a bitmap.  Return true if the bit changed.  */
    253 extern bool bitmap_set_bit (bitmap, int);
    254 
    255 /* Return true if a register is set in a register set.  */
    256 extern int bitmap_bit_p (bitmap, int);
    257 
    258 /* Debug functions to print a bitmap linked list.  */
    259 extern void debug_bitmap (const_bitmap);
    260 extern void debug_bitmap_file (FILE *, const_bitmap);
    261 
    262 /* Print a bitmap.  */
    263 extern void bitmap_print (FILE *, const_bitmap, const char *, const char *);
    264 
    265 /* Initialize and release a bitmap obstack.  */
    266 extern void bitmap_obstack_initialize (bitmap_obstack *);
    267 extern void bitmap_obstack_release (bitmap_obstack *);
    268 extern void bitmap_register (bitmap MEM_STAT_DECL);
    269 extern void dump_bitmap_statistics (void);
    270 
    271 /* Initialize a bitmap header.  OBSTACK indicates the bitmap obstack
    272    to allocate from, NULL for GC'd bitmap.  */
    273 
    274 static inline void
    275 bitmap_initialize_stat (bitmap head, bitmap_obstack *obstack MEM_STAT_DECL)
    276 {
    277   head->first = head->current = NULL;
    278   head->obstack = obstack;
    279   if (GATHER_STATISTICS)
    280     bitmap_register (head PASS_MEM_STAT);
    281 }
    282 #define bitmap_initialize(h,o) bitmap_initialize_stat (h,o MEM_STAT_INFO)
    283 
    284 /* Allocate and free bitmaps from obstack, malloc and gc'd memory.  */
    285 extern bitmap bitmap_obstack_alloc_stat (bitmap_obstack *obstack MEM_STAT_DECL);
    286 #define bitmap_obstack_alloc(t) bitmap_obstack_alloc_stat (t MEM_STAT_INFO)
    287 extern bitmap bitmap_gc_alloc_stat (ALONE_MEM_STAT_DECL);
    288 #define bitmap_gc_alloc() bitmap_gc_alloc_stat (ALONE_MEM_STAT_INFO)
    289 extern void bitmap_obstack_free (bitmap);
    290 
    291 /* A few compatibility/functions macros for compatibility with sbitmaps */
    292 inline void dump_bitmap (FILE *file, const_bitmap map)
    293 {
    294   bitmap_print (file, map, "", "\n");
    295 }
    296 
    297 extern unsigned bitmap_first_set_bit (const_bitmap);
    298 extern unsigned bitmap_last_set_bit (const_bitmap);
    299 
    300 /* Compute bitmap hash (for purposes of hashing etc.)  */
    301 extern hashval_t bitmap_hash(const_bitmap);
    302 
    303 /* Allocate a bitmap from a bit obstack.  */
    304 #define BITMAP_ALLOC(OBSTACK) bitmap_obstack_alloc (OBSTACK)
    305 
    306 /* Allocate a gc'd bitmap.  */
    307 #define BITMAP_GGC_ALLOC() bitmap_gc_alloc ()
    308 
    309 /* Do any cleanup needed on a bitmap when it is no longer used.  */
    310 #define BITMAP_FREE(BITMAP) \
    311        ((void) (bitmap_obstack_free ((bitmap) BITMAP), (BITMAP) = (bitmap) NULL))
    312 
    313 /* Iterator for bitmaps.  */
    314 
    315 typedef struct
    316 {
    317   /* Pointer to the current bitmap element.  */
    318   bitmap_element *elt1;
    319 
    320   /* Pointer to 2nd bitmap element when two are involved.  */
    321   bitmap_element *elt2;
    322 
    323   /* Word within the current element.  */
    324   unsigned word_no;
    325 
    326   /* Contents of the actually processed word.  When finding next bit
    327      it is shifted right, so that the actual bit is always the least
    328      significant bit of ACTUAL.  */
    329   BITMAP_WORD bits;
    330 } bitmap_iterator;
    331 
    332 /* Initialize a single bitmap iterator.  START_BIT is the first bit to
    333    iterate from.  */
    334 
    335 static inline void
    336 bmp_iter_set_init (bitmap_iterator *bi, const_bitmap map,
    337 		   unsigned start_bit, unsigned *bit_no)
    338 {
    339   bi->elt1 = map->first;
    340   bi->elt2 = NULL;
    341 
    342   /* Advance elt1 until it is not before the block containing start_bit.  */
    343   while (1)
    344     {
    345       if (!bi->elt1)
    346 	{
    347 	  bi->elt1 = &bitmap_zero_bits;
    348 	  break;
    349 	}
    350 
    351       if (bi->elt1->indx >= start_bit / BITMAP_ELEMENT_ALL_BITS)
    352 	break;
    353       bi->elt1 = bi->elt1->next;
    354     }
    355 
    356   /* We might have gone past the start bit, so reinitialize it.  */
    357   if (bi->elt1->indx != start_bit / BITMAP_ELEMENT_ALL_BITS)
    358     start_bit = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
    359 
    360   /* Initialize for what is now start_bit.  */
    361   bi->word_no = start_bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
    362   bi->bits = bi->elt1->bits[bi->word_no];
    363   bi->bits >>= start_bit % BITMAP_WORD_BITS;
    364 
    365   /* If this word is zero, we must make sure we're not pointing at the
    366      first bit, otherwise our incrementing to the next word boundary
    367      will fail.  It won't matter if this increment moves us into the
    368      next word.  */
    369   start_bit += !bi->bits;
    370 
    371   *bit_no = start_bit;
    372 }
    373 
    374 /* Initialize an iterator to iterate over the intersection of two
    375    bitmaps.  START_BIT is the bit to commence from.  */
    376 
    377 static inline void
    378 bmp_iter_and_init (bitmap_iterator *bi, const_bitmap map1, const_bitmap map2,
    379 		   unsigned start_bit, unsigned *bit_no)
    380 {
    381   bi->elt1 = map1->first;
    382   bi->elt2 = map2->first;
    383 
    384   /* Advance elt1 until it is not before the block containing
    385      start_bit.  */
    386   while (1)
    387     {
    388       if (!bi->elt1)
    389 	{
    390 	  bi->elt2 = NULL;
    391 	  break;
    392 	}
    393 
    394       if (bi->elt1->indx >= start_bit / BITMAP_ELEMENT_ALL_BITS)
    395 	break;
    396       bi->elt1 = bi->elt1->next;
    397     }
    398 
    399   /* Advance elt2 until it is not before elt1.  */
    400   while (1)
    401     {
    402       if (!bi->elt2)
    403 	{
    404 	  bi->elt1 = bi->elt2 = &bitmap_zero_bits;
    405 	  break;
    406 	}
    407 
    408       if (bi->elt2->indx >= bi->elt1->indx)
    409 	break;
    410       bi->elt2 = bi->elt2->next;
    411     }
    412 
    413   /* If we're at the same index, then we have some intersecting bits.  */
    414   if (bi->elt1->indx == bi->elt2->indx)
    415     {
    416       /* We might have advanced beyond the start_bit, so reinitialize
    417 	 for that.  */
    418       if (bi->elt1->indx != start_bit / BITMAP_ELEMENT_ALL_BITS)
    419 	start_bit = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
    420 
    421       bi->word_no = start_bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
    422       bi->bits = bi->elt1->bits[bi->word_no] & bi->elt2->bits[bi->word_no];
    423       bi->bits >>= start_bit % BITMAP_WORD_BITS;
    424     }
    425   else
    426     {
    427       /* Otherwise we must immediately advance elt1, so initialize for
    428 	 that.  */
    429       bi->word_no = BITMAP_ELEMENT_WORDS - 1;
    430       bi->bits = 0;
    431     }
    432 
    433   /* If this word is zero, we must make sure we're not pointing at the
    434      first bit, otherwise our incrementing to the next word boundary
    435      will fail.  It won't matter if this increment moves us into the
    436      next word.  */
    437   start_bit += !bi->bits;
    438 
    439   *bit_no = start_bit;
    440 }
    441 
    442 /* Initialize an iterator to iterate over the bits in MAP1 & ~MAP2.
    443    */
    444 
    445 static inline void
    446 bmp_iter_and_compl_init (bitmap_iterator *bi,
    447 			 const_bitmap map1, const_bitmap map2,
    448 			 unsigned start_bit, unsigned *bit_no)
    449 {
    450   bi->elt1 = map1->first;
    451   bi->elt2 = map2->first;
    452 
    453   /* Advance elt1 until it is not before the block containing start_bit.  */
    454   while (1)
    455     {
    456       if (!bi->elt1)
    457 	{
    458 	  bi->elt1 = &bitmap_zero_bits;
    459 	  break;
    460 	}
    461 
    462       if (bi->elt1->indx >= start_bit / BITMAP_ELEMENT_ALL_BITS)
    463 	break;
    464       bi->elt1 = bi->elt1->next;
    465     }
    466 
    467   /* Advance elt2 until it is not before elt1.  */
    468   while (bi->elt2 && bi->elt2->indx < bi->elt1->indx)
    469     bi->elt2 = bi->elt2->next;
    470 
    471   /* We might have advanced beyond the start_bit, so reinitialize for
    472      that.  */
    473   if (bi->elt1->indx != start_bit / BITMAP_ELEMENT_ALL_BITS)
    474     start_bit = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
    475 
    476   bi->word_no = start_bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
    477   bi->bits = bi->elt1->bits[bi->word_no];
    478   if (bi->elt2 && bi->elt1->indx == bi->elt2->indx)
    479     bi->bits &= ~bi->elt2->bits[bi->word_no];
    480   bi->bits >>= start_bit % BITMAP_WORD_BITS;
    481 
    482   /* If this word is zero, we must make sure we're not pointing at the
    483      first bit, otherwise our incrementing to the next word boundary
    484      will fail.  It won't matter if this increment moves us into the
    485      next word.  */
    486   start_bit += !bi->bits;
    487 
    488   *bit_no = start_bit;
    489 }
    490 
    491 /* Advance to the next bit in BI.  We don't advance to the next
    492    nonzero bit yet.  */
    493 
    494 static inline void
    495 bmp_iter_next (bitmap_iterator *bi, unsigned *bit_no)
    496 {
    497   bi->bits >>= 1;
    498   *bit_no += 1;
    499 }
    500 
    501 /* Advance to first set bit in BI.  */
    502 
    503 static inline void
    504 bmp_iter_next_bit (bitmap_iterator * bi, unsigned *bit_no)
    505 {
    506 #if (GCC_VERSION >= 3004)
    507   {
    508     unsigned int n = __builtin_ctzl (bi->bits);
    509     gcc_assert (sizeof (unsigned long) == sizeof (BITMAP_WORD));
    510     bi->bits >>= n;
    511     *bit_no += n;
    512   }
    513 #else
    514   while (!(bi->bits & 1))
    515     {
    516       bi->bits >>= 1;
    517       *bit_no += 1;
    518     }
    519 #endif
    520 }
    521 
    522 /* Advance to the next nonzero bit of a single bitmap, we will have
    523    already advanced past the just iterated bit.  Return true if there
    524    is a bit to iterate.  */
    525 
    526 static inline bool
    527 bmp_iter_set (bitmap_iterator *bi, unsigned *bit_no)
    528 {
    529   /* If our current word is nonzero, it contains the bit we want.  */
    530   if (bi->bits)
    531     {
    532     next_bit:
    533       bmp_iter_next_bit (bi, bit_no);
    534       return true;
    535     }
    536 
    537   /* Round up to the word boundary.  We might have just iterated past
    538      the end of the last word, hence the -1.  It is not possible for
    539      bit_no to point at the beginning of the now last word.  */
    540   *bit_no = ((*bit_no + BITMAP_WORD_BITS - 1)
    541 	     / BITMAP_WORD_BITS * BITMAP_WORD_BITS);
    542   bi->word_no++;
    543 
    544   while (1)
    545     {
    546       /* Find the next nonzero word in this elt.  */
    547       while (bi->word_no != BITMAP_ELEMENT_WORDS)
    548 	{
    549 	  bi->bits = bi->elt1->bits[bi->word_no];
    550 	  if (bi->bits)
    551 	    goto next_bit;
    552 	  *bit_no += BITMAP_WORD_BITS;
    553 	  bi->word_no++;
    554 	}
    555 
    556       /* Advance to the next element.  */
    557       bi->elt1 = bi->elt1->next;
    558       if (!bi->elt1)
    559 	return false;
    560       *bit_no = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
    561       bi->word_no = 0;
    562     }
    563 }
    564 
    565 /* Advance to the next nonzero bit of an intersecting pair of
    566    bitmaps.  We will have already advanced past the just iterated bit.
    567    Return true if there is a bit to iterate.  */
    568 
    569 static inline bool
    570 bmp_iter_and (bitmap_iterator *bi, unsigned *bit_no)
    571 {
    572   /* If our current word is nonzero, it contains the bit we want.  */
    573   if (bi->bits)
    574     {
    575     next_bit:
    576       bmp_iter_next_bit (bi, bit_no);
    577       return true;
    578     }
    579 
    580   /* Round up to the word boundary.  We might have just iterated past
    581      the end of the last word, hence the -1.  It is not possible for
    582      bit_no to point at the beginning of the now last word.  */
    583   *bit_no = ((*bit_no + BITMAP_WORD_BITS - 1)
    584 	     / BITMAP_WORD_BITS * BITMAP_WORD_BITS);
    585   bi->word_no++;
    586 
    587   while (1)
    588     {
    589       /* Find the next nonzero word in this elt.  */
    590       while (bi->word_no != BITMAP_ELEMENT_WORDS)
    591 	{
    592 	  bi->bits = bi->elt1->bits[bi->word_no] & bi->elt2->bits[bi->word_no];
    593 	  if (bi->bits)
    594 	    goto next_bit;
    595 	  *bit_no += BITMAP_WORD_BITS;
    596 	  bi->word_no++;
    597 	}
    598 
    599       /* Advance to the next identical element.  */
    600       do
    601 	{
    602 	  /* Advance elt1 while it is less than elt2.  We always want
    603 	     to advance one elt.  */
    604 	  do
    605 	    {
    606 	      bi->elt1 = bi->elt1->next;
    607 	      if (!bi->elt1)
    608 		return false;
    609 	    }
    610 	  while (bi->elt1->indx < bi->elt2->indx);
    611 
    612 	  /* Advance elt2 to be no less than elt1.  This might not
    613 	     advance.  */
    614 	  while (bi->elt2->indx < bi->elt1->indx)
    615 	    {
    616 	      bi->elt2 = bi->elt2->next;
    617 	      if (!bi->elt2)
    618 		return false;
    619 	    }
    620 	}
    621       while (bi->elt1->indx != bi->elt2->indx);
    622 
    623       *bit_no = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
    624       bi->word_no = 0;
    625     }
    626 }
    627 
    628 /* Advance to the next nonzero bit in the intersection of
    629    complemented bitmaps.  We will have already advanced past the just
    630    iterated bit.  */
    631 
    632 static inline bool
    633 bmp_iter_and_compl (bitmap_iterator *bi, unsigned *bit_no)
    634 {
    635   /* If our current word is nonzero, it contains the bit we want.  */
    636   if (bi->bits)
    637     {
    638     next_bit:
    639       bmp_iter_next_bit (bi, bit_no);
    640       return true;
    641     }
    642 
    643   /* Round up to the word boundary.  We might have just iterated past
    644      the end of the last word, hence the -1.  It is not possible for
    645      bit_no to point at the beginning of the now last word.  */
    646   *bit_no = ((*bit_no + BITMAP_WORD_BITS - 1)
    647 	     / BITMAP_WORD_BITS * BITMAP_WORD_BITS);
    648   bi->word_no++;
    649 
    650   while (1)
    651     {
    652       /* Find the next nonzero word in this elt.  */
    653       while (bi->word_no != BITMAP_ELEMENT_WORDS)
    654 	{
    655 	  bi->bits = bi->elt1->bits[bi->word_no];
    656 	  if (bi->elt2 && bi->elt2->indx == bi->elt1->indx)
    657 	    bi->bits &= ~bi->elt2->bits[bi->word_no];
    658 	  if (bi->bits)
    659 	    goto next_bit;
    660 	  *bit_no += BITMAP_WORD_BITS;
    661 	  bi->word_no++;
    662 	}
    663 
    664       /* Advance to the next element of elt1.  */
    665       bi->elt1 = bi->elt1->next;
    666       if (!bi->elt1)
    667 	return false;
    668 
    669       /* Advance elt2 until it is no less than elt1.  */
    670       while (bi->elt2 && bi->elt2->indx < bi->elt1->indx)
    671 	bi->elt2 = bi->elt2->next;
    672 
    673       *bit_no = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
    674       bi->word_no = 0;
    675     }
    676 }
    677 
    678 /* Loop over all bits set in BITMAP, starting with MIN and setting
    679    BITNUM to the bit number.  ITER is a bitmap iterator.  BITNUM
    680    should be treated as a read-only variable as it contains loop
    681    state.  */
    682 
    683 #ifndef EXECUTE_IF_SET_IN_BITMAP
    684 /* See sbitmap.h for the other definition of EXECUTE_IF_SET_IN_BITMAP.  */
    685 #define EXECUTE_IF_SET_IN_BITMAP(BITMAP, MIN, BITNUM, ITER)		\
    686   for (bmp_iter_set_init (&(ITER), (BITMAP), (MIN), &(BITNUM));		\
    687        bmp_iter_set (&(ITER), &(BITNUM));				\
    688        bmp_iter_next (&(ITER), &(BITNUM)))
    689 #endif
    690 
    691 /* Loop over all the bits set in BITMAP1 & BITMAP2, starting with MIN
    692    and setting BITNUM to the bit number.  ITER is a bitmap iterator.
    693    BITNUM should be treated as a read-only variable as it contains
    694    loop state.  */
    695 
    696 #define EXECUTE_IF_AND_IN_BITMAP(BITMAP1, BITMAP2, MIN, BITNUM, ITER)	\
    697   for (bmp_iter_and_init (&(ITER), (BITMAP1), (BITMAP2), (MIN),		\
    698 			  &(BITNUM));					\
    699        bmp_iter_and (&(ITER), &(BITNUM));				\
    700        bmp_iter_next (&(ITER), &(BITNUM)))
    701 
    702 /* Loop over all the bits set in BITMAP1 & ~BITMAP2, starting with MIN
    703    and setting BITNUM to the bit number.  ITER is a bitmap iterator.
    704    BITNUM should be treated as a read-only variable as it contains
    705    loop state.  */
    706 
    707 #define EXECUTE_IF_AND_COMPL_IN_BITMAP(BITMAP1, BITMAP2, MIN, BITNUM, ITER) \
    708   for (bmp_iter_and_compl_init (&(ITER), (BITMAP1), (BITMAP2), (MIN),	\
    709 				&(BITNUM));				\
    710        bmp_iter_and_compl (&(ITER), &(BITNUM));				\
    711        bmp_iter_next (&(ITER), &(BITNUM)))
    712 
    713 #endif /* GCC_BITMAP_H */
    714