bitmap.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. /* Functions to support general ended bitmaps.
  2. Copyright (C) 1997-2015 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. #ifndef GCC_BITMAP_H
  16. #define GCC_BITMAP_H
  17. /* Implementation of sparse integer sets as a linked list.
  18. This sparse set representation is suitable for sparse sets with an
  19. unknown (a priori) universe. The set is represented as a double-linked
  20. list of container nodes (struct bitmap_element). Each node consists
  21. of an index for the first member that could be held in the container,
  22. a small array of integers that represent the members in the container,
  23. and pointers to the next and previous element in the linked list. The
  24. elements in the list are sorted in ascending order, i.e. the head of
  25. the list holds the element with the smallest member of the set.
  26. For a given member I in the set:
  27. - the element for I will have index is I / (bits per element)
  28. - the position for I within element is I % (bits per element)
  29. This representation is very space-efficient for large sparse sets, and
  30. the size of the set can be changed dynamically without much overhead.
  31. An important parameter is the number of bits per element. In this
  32. implementation, there are 128 bits per element. This results in a
  33. high storage overhead *per element*, but a small overall overhead if
  34. the set is very sparse.
  35. The downside is that many operations are relatively slow because the
  36. linked list has to be traversed to test membership (i.e. member_p/
  37. add_member/remove_member). To improve the performance of this set
  38. representation, the last accessed element and its index are cached.
  39. For membership tests on members close to recently accessed members,
  40. the cached last element improves membership test to a constant-time
  41. operation.
  42. The following operations can always be performed in O(1) time:
  43. * clear : bitmap_clear
  44. * choose_one : (not implemented, but could be
  45. implemented in constant time)
  46. The following operations can be performed in O(E) time worst-case (with
  47. E the number of elements in the linked list), but in O(1) time with a
  48. suitable access patterns:
  49. * member_p : bitmap_bit_p
  50. * add_member : bitmap_set_bit
  51. * remove_member : bitmap_clear_bit
  52. The following operations can be performed in O(E) time:
  53. * cardinality : bitmap_count_bits
  54. * set_size : bitmap_last_set_bit (but this could
  55. in constant time with a pointer to
  56. the last element in the chain)
  57. Additionally, the linked-list sparse set representation supports
  58. enumeration of the members in O(E) time:
  59. * forall : EXECUTE_IF_SET_IN_BITMAP
  60. * set_copy : bitmap_copy
  61. * set_intersection : bitmap_intersect_p /
  62. bitmap_and / bitmap_and_into /
  63. EXECUTE_IF_AND_IN_BITMAP
  64. * set_union : bitmap_ior / bitmap_ior_into
  65. * set_difference : bitmap_intersect_compl_p /
  66. bitmap_and_comp / bitmap_and_comp_into /
  67. EXECUTE_IF_AND_COMPL_IN_BITMAP
  68. * set_disjuction : bitmap_xor_comp / bitmap_xor_comp_into
  69. * set_compare : bitmap_equal_p
  70. Some operations on 3 sets that occur frequently in in data flow problems
  71. are also implemented:
  72. * A | (B & C) : bitmap_ior_and_into
  73. * A | (B & ~C) : bitmap_ior_and_compl /
  74. bitmap_ior_and_compl_into
  75. The storage requirements for linked-list sparse sets are O(E), with E->N
  76. in the worst case (a sparse set with large distances between the values
  77. of the set members).
  78. The linked-list set representation works well for problems involving very
  79. sparse sets. The canonical example in GCC is, of course, the "set of
  80. sets" for some CFG-based data flow problems (liveness analysis, dominance
  81. frontiers, etc.).
  82. This representation also works well for data flow problems where the size
  83. of the set may grow dynamically, but care must be taken that the member_p,
  84. add_member, and remove_member operations occur with a suitable access
  85. pattern.
  86. For random-access sets with a known, relatively small universe size, the
  87. SparseSet or simple bitmap representations may be more efficient than a
  88. linked-list set. For random-access sets of unknown universe, a hash table
  89. or a balanced binary tree representation is likely to be a more suitable
  90. choice.
  91. Traversing linked lists is usually cache-unfriendly, even with the last
  92. accessed element cached.
  93. Cache performance can be improved by keeping the elements in the set
  94. grouped together in memory, using a dedicated obstack for a set (or group
  95. of related sets). Elements allocated on obstacks are released to a
  96. free-list and taken off the free list. If multiple sets are allocated on
  97. the same obstack, elements freed from one set may be re-used for one of
  98. the other sets. This usually helps avoid cache misses.
  99. A single free-list is used for all sets allocated in GGC space. This is
  100. bad for persistent sets, so persistent sets should be allocated on an
  101. obstack whenever possible. */
  102. #include "hashtab.h"
  103. #include "statistics.h"
  104. #include "obstack.h"
  105. /* Fundamental storage type for bitmap. */
  106. typedef unsigned long BITMAP_WORD;
  107. /* BITMAP_WORD_BITS needs to be unsigned, but cannot contain casts as
  108. it is used in preprocessor directives -- hence the 1u. */
  109. #define BITMAP_WORD_BITS (CHAR_BIT * SIZEOF_LONG * 1u)
  110. /* Number of words to use for each element in the linked list. */
  111. #ifndef BITMAP_ELEMENT_WORDS
  112. #define BITMAP_ELEMENT_WORDS ((128 + BITMAP_WORD_BITS - 1) / BITMAP_WORD_BITS)
  113. #endif
  114. /* Number of bits in each actual element of a bitmap. */
  115. #define BITMAP_ELEMENT_ALL_BITS (BITMAP_ELEMENT_WORDS * BITMAP_WORD_BITS)
  116. /* Obstack for allocating bitmaps and elements from. */
  117. struct GTY (()) bitmap_obstack {
  118. struct bitmap_element *elements;
  119. struct bitmap_head *heads;
  120. struct obstack GTY ((skip)) obstack;
  121. };
  122. /* Bitmap set element. We use a linked list to hold only the bits that
  123. are set. This allows for use to grow the bitset dynamically without
  124. having to realloc and copy a giant bit array.
  125. The free list is implemented as a list of lists. There is one
  126. outer list connected together by prev fields. Each element of that
  127. outer is an inner list (that may consist only of the outer list
  128. element) that are connected by the next fields. The prev pointer
  129. is undefined for interior elements. This allows
  130. bitmap_elt_clear_from to be implemented in unit time rather than
  131. linear in the number of elements to be freed. */
  132. struct GTY((chain_next ("%h.next"), chain_prev ("%h.prev"))) bitmap_element {
  133. struct bitmap_element *next; /* Next element. */
  134. struct bitmap_element *prev; /* Previous element. */
  135. unsigned int indx; /* regno/BITMAP_ELEMENT_ALL_BITS. */
  136. BITMAP_WORD bits[BITMAP_ELEMENT_WORDS]; /* Bits that are set. */
  137. };
  138. /* Head of bitmap linked list. The 'current' member points to something
  139. already pointed to by the chain started by first, so GTY((skip)) it. */
  140. struct GTY(()) bitmap_head {
  141. unsigned int indx; /* Index of last element looked at. */
  142. unsigned int descriptor_id; /* Unique identifier for the allocation
  143. site of this bitmap, for detailed
  144. statistics gathering. */
  145. bitmap_element *first; /* First element in linked list. */
  146. bitmap_element * GTY((skip(""))) current; /* Last element looked at. */
  147. bitmap_obstack *obstack; /* Obstack to allocate elements from.
  148. If NULL, then use GGC allocation. */
  149. };
  150. /* Global data */
  151. extern bitmap_element bitmap_zero_bits; /* Zero bitmap element */
  152. extern bitmap_obstack bitmap_default_obstack; /* Default bitmap obstack */
  153. /* Clear a bitmap by freeing up the linked list. */
  154. extern void bitmap_clear (bitmap);
  155. /* Copy a bitmap to another bitmap. */
  156. extern void bitmap_copy (bitmap, const_bitmap);
  157. /* True if two bitmaps are identical. */
  158. extern bool bitmap_equal_p (const_bitmap, const_bitmap);
  159. /* True if the bitmaps intersect (their AND is non-empty). */
  160. extern bool bitmap_intersect_p (const_bitmap, const_bitmap);
  161. /* True if the complement of the second intersects the first (their
  162. AND_COMPL is non-empty). */
  163. extern bool bitmap_intersect_compl_p (const_bitmap, const_bitmap);
  164. /* True if MAP is an empty bitmap. */
  165. inline bool bitmap_empty_p (const_bitmap map)
  166. {
  167. return !map->first;
  168. }
  169. /* True if the bitmap has only a single bit set. */
  170. extern bool bitmap_single_bit_set_p (const_bitmap);
  171. /* Count the number of bits set in the bitmap. */
  172. extern unsigned long bitmap_count_bits (const_bitmap);
  173. /* Boolean operations on bitmaps. The _into variants are two operand
  174. versions that modify the first source operand. The other variants
  175. are three operand versions that to not destroy the source bitmaps.
  176. The operations supported are &, & ~, |, ^. */
  177. extern void bitmap_and (bitmap, const_bitmap, const_bitmap);
  178. extern bool bitmap_and_into (bitmap, const_bitmap);
  179. extern bool bitmap_and_compl (bitmap, const_bitmap, const_bitmap);
  180. extern bool bitmap_and_compl_into (bitmap, const_bitmap);
  181. #define bitmap_compl_and(DST, A, B) bitmap_and_compl (DST, B, A)
  182. extern void bitmap_compl_and_into (bitmap, const_bitmap);
  183. extern void bitmap_clear_range (bitmap, unsigned int, unsigned int);
  184. extern void bitmap_set_range (bitmap, unsigned int, unsigned int);
  185. extern bool bitmap_ior (bitmap, const_bitmap, const_bitmap);
  186. extern bool bitmap_ior_into (bitmap, const_bitmap);
  187. extern void bitmap_xor (bitmap, const_bitmap, const_bitmap);
  188. extern void bitmap_xor_into (bitmap, const_bitmap);
  189. /* DST = A | (B & C). Return true if DST changes. */
  190. extern bool bitmap_ior_and_into (bitmap DST, const_bitmap B, const_bitmap C);
  191. /* DST = A | (B & ~C). Return true if DST changes. */
  192. extern bool bitmap_ior_and_compl (bitmap DST, const_bitmap A,
  193. const_bitmap B, const_bitmap C);
  194. /* A |= (B & ~C). Return true if A changes. */
  195. extern bool bitmap_ior_and_compl_into (bitmap A,
  196. const_bitmap B, const_bitmap C);
  197. /* Clear a single bit in a bitmap. Return true if the bit changed. */
  198. extern bool bitmap_clear_bit (bitmap, int);
  199. /* Set a single bit in a bitmap. Return true if the bit changed. */
  200. extern bool bitmap_set_bit (bitmap, int);
  201. /* Return true if a register is set in a register set. */
  202. extern int bitmap_bit_p (bitmap, int);
  203. /* Debug functions to print a bitmap linked list. */
  204. extern void debug_bitmap (const_bitmap);
  205. extern void debug_bitmap_file (FILE *, const_bitmap);
  206. /* Print a bitmap. */
  207. extern void bitmap_print (FILE *, const_bitmap, const char *, const char *);
  208. /* Initialize and release a bitmap obstack. */
  209. extern void bitmap_obstack_initialize (bitmap_obstack *);
  210. extern void bitmap_obstack_release (bitmap_obstack *);
  211. extern void bitmap_register (bitmap MEM_STAT_DECL);
  212. extern void dump_bitmap_statistics (void);
  213. /* Initialize a bitmap header. OBSTACK indicates the bitmap obstack
  214. to allocate from, NULL for GC'd bitmap. */
  215. static inline void
  216. bitmap_initialize_stat (bitmap head, bitmap_obstack *obstack MEM_STAT_DECL)
  217. {
  218. head->first = head->current = NULL;
  219. head->obstack = obstack;
  220. if (GATHER_STATISTICS)
  221. bitmap_register (head PASS_MEM_STAT);
  222. }
  223. #define bitmap_initialize(h,o) bitmap_initialize_stat (h,o MEM_STAT_INFO)
  224. /* Allocate and free bitmaps from obstack, malloc and gc'd memory. */
  225. extern bitmap bitmap_obstack_alloc_stat (bitmap_obstack *obstack MEM_STAT_DECL);
  226. #define bitmap_obstack_alloc(t) bitmap_obstack_alloc_stat (t MEM_STAT_INFO)
  227. extern bitmap bitmap_gc_alloc_stat (ALONE_MEM_STAT_DECL);
  228. #define bitmap_gc_alloc() bitmap_gc_alloc_stat (ALONE_MEM_STAT_INFO)
  229. extern void bitmap_obstack_free (bitmap);
  230. /* A few compatibility/functions macros for compatibility with sbitmaps */
  231. inline void dump_bitmap (FILE *file, const_bitmap map)
  232. {
  233. bitmap_print (file, map, "", "\n");
  234. }
  235. extern void debug (const bitmap_head &ref);
  236. extern void debug (const bitmap_head *ptr);
  237. extern unsigned bitmap_first_set_bit (const_bitmap);
  238. extern unsigned bitmap_last_set_bit (const_bitmap);
  239. /* Compute bitmap hash (for purposes of hashing etc.) */
  240. extern hashval_t bitmap_hash (const_bitmap);
  241. /* Allocate a bitmap from a bit obstack. */
  242. #define BITMAP_ALLOC(OBSTACK) bitmap_obstack_alloc (OBSTACK)
  243. /* Allocate a gc'd bitmap. */
  244. #define BITMAP_GGC_ALLOC() bitmap_gc_alloc ()
  245. /* Do any cleanup needed on a bitmap when it is no longer used. */
  246. #define BITMAP_FREE(BITMAP) \
  247. ((void) (bitmap_obstack_free ((bitmap) BITMAP), (BITMAP) = (bitmap) NULL))
  248. /* Iterator for bitmaps. */
  249. struct bitmap_iterator
  250. {
  251. /* Pointer to the current bitmap element. */
  252. bitmap_element *elt1;
  253. /* Pointer to 2nd bitmap element when two are involved. */
  254. bitmap_element *elt2;
  255. /* Word within the current element. */
  256. unsigned word_no;
  257. /* Contents of the actually processed word. When finding next bit
  258. it is shifted right, so that the actual bit is always the least
  259. significant bit of ACTUAL. */
  260. BITMAP_WORD bits;
  261. };
  262. /* Initialize a single bitmap iterator. START_BIT is the first bit to
  263. iterate from. */
  264. static inline void
  265. bmp_iter_set_init (bitmap_iterator *bi, const_bitmap map,
  266. unsigned start_bit, unsigned *bit_no)
  267. {
  268. bi->elt1 = map->first;
  269. bi->elt2 = NULL;
  270. /* Advance elt1 until it is not before the block containing start_bit. */
  271. while (1)
  272. {
  273. if (!bi->elt1)
  274. {
  275. bi->elt1 = &bitmap_zero_bits;
  276. break;
  277. }
  278. if (bi->elt1->indx >= start_bit / BITMAP_ELEMENT_ALL_BITS)
  279. break;
  280. bi->elt1 = bi->elt1->next;
  281. }
  282. /* We might have gone past the start bit, so reinitialize it. */
  283. if (bi->elt1->indx != start_bit / BITMAP_ELEMENT_ALL_BITS)
  284. start_bit = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
  285. /* Initialize for what is now start_bit. */
  286. bi->word_no = start_bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
  287. bi->bits = bi->elt1->bits[bi->word_no];
  288. bi->bits >>= start_bit % BITMAP_WORD_BITS;
  289. /* If this word is zero, we must make sure we're not pointing at the
  290. first bit, otherwise our incrementing to the next word boundary
  291. will fail. It won't matter if this increment moves us into the
  292. next word. */
  293. start_bit += !bi->bits;
  294. *bit_no = start_bit;
  295. }
  296. /* Initialize an iterator to iterate over the intersection of two
  297. bitmaps. START_BIT is the bit to commence from. */
  298. static inline void
  299. bmp_iter_and_init (bitmap_iterator *bi, const_bitmap map1, const_bitmap map2,
  300. unsigned start_bit, unsigned *bit_no)
  301. {
  302. bi->elt1 = map1->first;
  303. bi->elt2 = map2->first;
  304. /* Advance elt1 until it is not before the block containing
  305. start_bit. */
  306. while (1)
  307. {
  308. if (!bi->elt1)
  309. {
  310. bi->elt2 = NULL;
  311. break;
  312. }
  313. if (bi->elt1->indx >= start_bit / BITMAP_ELEMENT_ALL_BITS)
  314. break;
  315. bi->elt1 = bi->elt1->next;
  316. }
  317. /* Advance elt2 until it is not before elt1. */
  318. while (1)
  319. {
  320. if (!bi->elt2)
  321. {
  322. bi->elt1 = bi->elt2 = &bitmap_zero_bits;
  323. break;
  324. }
  325. if (bi->elt2->indx >= bi->elt1->indx)
  326. break;
  327. bi->elt2 = bi->elt2->next;
  328. }
  329. /* If we're at the same index, then we have some intersecting bits. */
  330. if (bi->elt1->indx == bi->elt2->indx)
  331. {
  332. /* We might have advanced beyond the start_bit, so reinitialize
  333. for that. */
  334. if (bi->elt1->indx != start_bit / BITMAP_ELEMENT_ALL_BITS)
  335. start_bit = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
  336. bi->word_no = start_bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
  337. bi->bits = bi->elt1->bits[bi->word_no] & bi->elt2->bits[bi->word_no];
  338. bi->bits >>= start_bit % BITMAP_WORD_BITS;
  339. }
  340. else
  341. {
  342. /* Otherwise we must immediately advance elt1, so initialize for
  343. that. */
  344. bi->word_no = BITMAP_ELEMENT_WORDS - 1;
  345. bi->bits = 0;
  346. }
  347. /* If this word is zero, we must make sure we're not pointing at the
  348. first bit, otherwise our incrementing to the next word boundary
  349. will fail. It won't matter if this increment moves us into the
  350. next word. */
  351. start_bit += !bi->bits;
  352. *bit_no = start_bit;
  353. }
  354. /* Initialize an iterator to iterate over the bits in MAP1 & ~MAP2.
  355. */
  356. static inline void
  357. bmp_iter_and_compl_init (bitmap_iterator *bi,
  358. const_bitmap map1, const_bitmap map2,
  359. unsigned start_bit, unsigned *bit_no)
  360. {
  361. bi->elt1 = map1->first;
  362. bi->elt2 = map2->first;
  363. /* Advance elt1 until it is not before the block containing start_bit. */
  364. while (1)
  365. {
  366. if (!bi->elt1)
  367. {
  368. bi->elt1 = &bitmap_zero_bits;
  369. break;
  370. }
  371. if (bi->elt1->indx >= start_bit / BITMAP_ELEMENT_ALL_BITS)
  372. break;
  373. bi->elt1 = bi->elt1->next;
  374. }
  375. /* Advance elt2 until it is not before elt1. */
  376. while (bi->elt2 && bi->elt2->indx < bi->elt1->indx)
  377. bi->elt2 = bi->elt2->next;
  378. /* We might have advanced beyond the start_bit, so reinitialize for
  379. that. */
  380. if (bi->elt1->indx != start_bit / BITMAP_ELEMENT_ALL_BITS)
  381. start_bit = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
  382. bi->word_no = start_bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
  383. bi->bits = bi->elt1->bits[bi->word_no];
  384. if (bi->elt2 && bi->elt1->indx == bi->elt2->indx)
  385. bi->bits &= ~bi->elt2->bits[bi->word_no];
  386. bi->bits >>= start_bit % BITMAP_WORD_BITS;
  387. /* If this word is zero, we must make sure we're not pointing at the
  388. first bit, otherwise our incrementing to the next word boundary
  389. will fail. It won't matter if this increment moves us into the
  390. next word. */
  391. start_bit += !bi->bits;
  392. *bit_no = start_bit;
  393. }
  394. /* Advance to the next bit in BI. We don't advance to the next
  395. nonzero bit yet. */
  396. static inline void
  397. bmp_iter_next (bitmap_iterator *bi, unsigned *bit_no)
  398. {
  399. bi->bits >>= 1;
  400. *bit_no += 1;
  401. }
  402. /* Advance to first set bit in BI. */
  403. static inline void
  404. bmp_iter_next_bit (bitmap_iterator * bi, unsigned *bit_no)
  405. {
  406. #if (GCC_VERSION >= 3004)
  407. {
  408. unsigned int n = __builtin_ctzl (bi->bits);
  409. gcc_assert (sizeof (unsigned long) == sizeof (BITMAP_WORD));
  410. bi->bits >>= n;
  411. *bit_no += n;
  412. }
  413. #else
  414. while (!(bi->bits & 1))
  415. {
  416. bi->bits >>= 1;
  417. *bit_no += 1;
  418. }
  419. #endif
  420. }
  421. /* Advance to the next nonzero bit of a single bitmap, we will have
  422. already advanced past the just iterated bit. Return true if there
  423. is a bit to iterate. */
  424. static inline bool
  425. bmp_iter_set (bitmap_iterator *bi, unsigned *bit_no)
  426. {
  427. /* If our current word is nonzero, it contains the bit we want. */
  428. if (bi->bits)
  429. {
  430. next_bit:
  431. bmp_iter_next_bit (bi, bit_no);
  432. return true;
  433. }
  434. /* Round up to the word boundary. We might have just iterated past
  435. the end of the last word, hence the -1. It is not possible for
  436. bit_no to point at the beginning of the now last word. */
  437. *bit_no = ((*bit_no + BITMAP_WORD_BITS - 1)
  438. / BITMAP_WORD_BITS * BITMAP_WORD_BITS);
  439. bi->word_no++;
  440. while (1)
  441. {
  442. /* Find the next nonzero word in this elt. */
  443. while (bi->word_no != BITMAP_ELEMENT_WORDS)
  444. {
  445. bi->bits = bi->elt1->bits[bi->word_no];
  446. if (bi->bits)
  447. goto next_bit;
  448. *bit_no += BITMAP_WORD_BITS;
  449. bi->word_no++;
  450. }
  451. /* Advance to the next element. */
  452. bi->elt1 = bi->elt1->next;
  453. if (!bi->elt1)
  454. return false;
  455. *bit_no = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
  456. bi->word_no = 0;
  457. }
  458. }
  459. /* Advance to the next nonzero bit of an intersecting pair of
  460. bitmaps. We will have already advanced past the just iterated bit.
  461. Return true if there is a bit to iterate. */
  462. static inline bool
  463. bmp_iter_and (bitmap_iterator *bi, unsigned *bit_no)
  464. {
  465. /* If our current word is nonzero, it contains the bit we want. */
  466. if (bi->bits)
  467. {
  468. next_bit:
  469. bmp_iter_next_bit (bi, bit_no);
  470. return true;
  471. }
  472. /* Round up to the word boundary. We might have just iterated past
  473. the end of the last word, hence the -1. It is not possible for
  474. bit_no to point at the beginning of the now last word. */
  475. *bit_no = ((*bit_no + BITMAP_WORD_BITS - 1)
  476. / BITMAP_WORD_BITS * BITMAP_WORD_BITS);
  477. bi->word_no++;
  478. while (1)
  479. {
  480. /* Find the next nonzero word in this elt. */
  481. while (bi->word_no != BITMAP_ELEMENT_WORDS)
  482. {
  483. bi->bits = bi->elt1->bits[bi->word_no] & bi->elt2->bits[bi->word_no];
  484. if (bi->bits)
  485. goto next_bit;
  486. *bit_no += BITMAP_WORD_BITS;
  487. bi->word_no++;
  488. }
  489. /* Advance to the next identical element. */
  490. do
  491. {
  492. /* Advance elt1 while it is less than elt2. We always want
  493. to advance one elt. */
  494. do
  495. {
  496. bi->elt1 = bi->elt1->next;
  497. if (!bi->elt1)
  498. return false;
  499. }
  500. while (bi->elt1->indx < bi->elt2->indx);
  501. /* Advance elt2 to be no less than elt1. This might not
  502. advance. */
  503. while (bi->elt2->indx < bi->elt1->indx)
  504. {
  505. bi->elt2 = bi->elt2->next;
  506. if (!bi->elt2)
  507. return false;
  508. }
  509. }
  510. while (bi->elt1->indx != bi->elt2->indx);
  511. *bit_no = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
  512. bi->word_no = 0;
  513. }
  514. }
  515. /* Advance to the next nonzero bit in the intersection of
  516. complemented bitmaps. We will have already advanced past the just
  517. iterated bit. */
  518. static inline bool
  519. bmp_iter_and_compl (bitmap_iterator *bi, unsigned *bit_no)
  520. {
  521. /* If our current word is nonzero, it contains the bit we want. */
  522. if (bi->bits)
  523. {
  524. next_bit:
  525. bmp_iter_next_bit (bi, bit_no);
  526. return true;
  527. }
  528. /* Round up to the word boundary. We might have just iterated past
  529. the end of the last word, hence the -1. It is not possible for
  530. bit_no to point at the beginning of the now last word. */
  531. *bit_no = ((*bit_no + BITMAP_WORD_BITS - 1)
  532. / BITMAP_WORD_BITS * BITMAP_WORD_BITS);
  533. bi->word_no++;
  534. while (1)
  535. {
  536. /* Find the next nonzero word in this elt. */
  537. while (bi->word_no != BITMAP_ELEMENT_WORDS)
  538. {
  539. bi->bits = bi->elt1->bits[bi->word_no];
  540. if (bi->elt2 && bi->elt2->indx == bi->elt1->indx)
  541. bi->bits &= ~bi->elt2->bits[bi->word_no];
  542. if (bi->bits)
  543. goto next_bit;
  544. *bit_no += BITMAP_WORD_BITS;
  545. bi->word_no++;
  546. }
  547. /* Advance to the next element of elt1. */
  548. bi->elt1 = bi->elt1->next;
  549. if (!bi->elt1)
  550. return false;
  551. /* Advance elt2 until it is no less than elt1. */
  552. while (bi->elt2 && bi->elt2->indx < bi->elt1->indx)
  553. bi->elt2 = bi->elt2->next;
  554. *bit_no = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
  555. bi->word_no = 0;
  556. }
  557. }
  558. /* Loop over all bits set in BITMAP, starting with MIN and setting
  559. BITNUM to the bit number. ITER is a bitmap iterator. BITNUM
  560. should be treated as a read-only variable as it contains loop
  561. state. */
  562. #ifndef EXECUTE_IF_SET_IN_BITMAP
  563. /* See sbitmap.h for the other definition of EXECUTE_IF_SET_IN_BITMAP. */
  564. #define EXECUTE_IF_SET_IN_BITMAP(BITMAP, MIN, BITNUM, ITER) \
  565. for (bmp_iter_set_init (&(ITER), (BITMAP), (MIN), &(BITNUM)); \
  566. bmp_iter_set (&(ITER), &(BITNUM)); \
  567. bmp_iter_next (&(ITER), &(BITNUM)))
  568. #endif
  569. /* Loop over all the bits set in BITMAP1 & BITMAP2, starting with MIN
  570. and setting BITNUM to the bit number. ITER is a bitmap iterator.
  571. BITNUM should be treated as a read-only variable as it contains
  572. loop state. */
  573. #define EXECUTE_IF_AND_IN_BITMAP(BITMAP1, BITMAP2, MIN, BITNUM, ITER) \
  574. for (bmp_iter_and_init (&(ITER), (BITMAP1), (BITMAP2), (MIN), \
  575. &(BITNUM)); \
  576. bmp_iter_and (&(ITER), &(BITNUM)); \
  577. bmp_iter_next (&(ITER), &(BITNUM)))
  578. /* Loop over all the bits set in BITMAP1 & ~BITMAP2, starting with MIN
  579. and setting BITNUM to the bit number. ITER is a bitmap iterator.
  580. BITNUM should be treated as a read-only variable as it contains
  581. loop state. */
  582. #define EXECUTE_IF_AND_COMPL_IN_BITMAP(BITMAP1, BITMAP2, MIN, BITNUM, ITER) \
  583. for (bmp_iter_and_compl_init (&(ITER), (BITMAP1), (BITMAP2), (MIN), \
  584. &(BITNUM)); \
  585. bmp_iter_and_compl (&(ITER), &(BITNUM)); \
  586. bmp_iter_next (&(ITER), &(BITNUM)))
  587. #endif /* GCC_BITMAP_H */