bitmap.h 27 KB

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