bitmap.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  1. /* Functions to support general ended bitmaps.
  2. Copyright (C) 1997-2019 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 or tree.
  18. This sparse set representation is suitable for sparse sets with an
  19. unknown (a priori) universe.
  20. Sets are represented as double-linked lists of container nodes of
  21. type "struct bitmap_element" or as a binary trees of the same
  22. container nodes. Each container node consists of an index for the
  23. first member that could be held in the container, a small array of
  24. integers that represent the members in the container, and pointers
  25. to the next and previous element in the linked list, or left and
  26. right children in the tree. In linked-list form, the container
  27. nodes in the list are sorted in ascending order, i.e. the head of
  28. the list holds the element with the smallest member of the set.
  29. In tree form, nodes to the left have a smaller container index.
  30. For a given member I in the set:
  31. - the element for I will have index is I / (bits per element)
  32. - the position for I within element is I % (bits per element)
  33. This representation is very space-efficient for large sparse sets, and
  34. the size of the set can be changed dynamically without much overhead.
  35. An important parameter is the number of bits per element. In this
  36. implementation, there are 128 bits per element. This results in a
  37. high storage overhead *per element*, but a small overall overhead if
  38. the set is very sparse.
  39. The storage requirements for linked-list sparse sets are O(E), with E->N
  40. in the worst case (a sparse set with large distances between the values
  41. of the set members).
  42. This representation also works well for data flow problems where the size
  43. of the set may grow dynamically, but care must be taken that the member_p,
  44. add_member, and remove_member operations occur with a suitable access
  45. pattern.
  46. The linked-list set representation works well for problems involving very
  47. sparse sets. The canonical example in GCC is, of course, the "set of
  48. sets" for some CFG-based data flow problems (liveness analysis, dominance
  49. frontiers, etc.).
  50. For random-access sparse sets of unknown universe, the binary tree
  51. representation is likely to be a more suitable choice. Theoretical
  52. access times for the binary tree representation are better than those
  53. for the linked-list, but in practice this is only true for truely
  54. random access.
  55. Often the most suitable representation during construction of the set
  56. is not the best choice for the usage of the set. For such cases, the
  57. "view" of the set can be changed from one representation to the other.
  58. This is an O(E) operation:
  59. * from list to tree view : bitmap_tree_view
  60. * from tree to list view : bitmap_list_view
  61. Traversing linked lists or trees can be cache-unfriendly. Performance
  62. can be improved by keeping container nodes in the set grouped together
  63. in memory, using a dedicated obstack for a set (or group of related
  64. sets). Elements allocated on obstacks are released to a free-list and
  65. taken off the free list. If multiple sets are allocated on the same
  66. obstack, elements freed from one set may be re-used for one of the other
  67. sets. This usually helps avoid cache misses.
  68. A single free-list is used for all sets allocated in GGC space. This is
  69. bad for persistent sets, so persistent sets should be allocated on an
  70. obstack whenever possible.
  71. For random-access sets with a known, relatively small universe size, the
  72. SparseSet or simple bitmap representations may be more efficient than a
  73. linked-list set.
  74. LINKED LIST FORM
  75. ================
  76. In linked-list form, in-order iterations of the set can be executed
  77. efficiently. The downside is that many random-access operations are
  78. relatively slow, because the linked list has to be traversed to test
  79. membership (i.e. member_p/ add_member/remove_member).
  80. To improve the performance of this set representation, the last
  81. accessed element and its index are cached. For membership tests on
  82. members close to recently accessed members, the cached last element
  83. improves membership test to a constant-time operation.
  84. The following operations can always be performed in O(1) time in
  85. list view:
  86. * clear : bitmap_clear
  87. * smallest_member : bitmap_first_set_bit
  88. * choose_one : (not implemented, but could be
  89. in constant time)
  90. The following operations can be performed in O(E) time worst-case in
  91. list view (with E the number of elements in the linked list), but in
  92. O(1) time with a suitable access patterns:
  93. * member_p : bitmap_bit_p
  94. * add_member : bitmap_set_bit / bitmap_set_range
  95. * remove_member : bitmap_clear_bit / bitmap_clear_range
  96. The following operations can be performed in O(E) time in list view:
  97. * cardinality : bitmap_count_bits
  98. * largest_member : bitmap_last_set_bit (but this could
  99. in constant time with a pointer to
  100. the last element in the chain)
  101. * set_size : bitmap_last_set_bit
  102. In tree view the following operations can all be performed in O(log E)
  103. amortized time with O(E) worst-case behavior.
  104. * smallest_member
  105. * largest_member
  106. * set_size
  107. * member_p
  108. * add_member
  109. * remove_member
  110. Additionally, the linked-list sparse set representation supports
  111. enumeration of the members in O(E) time:
  112. * forall : EXECUTE_IF_SET_IN_BITMAP
  113. * set_copy : bitmap_copy
  114. * set_intersection : bitmap_intersect_p /
  115. bitmap_and / bitmap_and_into /
  116. EXECUTE_IF_AND_IN_BITMAP
  117. * set_union : bitmap_ior / bitmap_ior_into
  118. * set_difference : bitmap_intersect_compl_p /
  119. bitmap_and_comp / bitmap_and_comp_into /
  120. EXECUTE_IF_AND_COMPL_IN_BITMAP
  121. * set_disjuction : bitmap_xor_comp / bitmap_xor_comp_into
  122. * set_compare : bitmap_equal_p
  123. Some operations on 3 sets that occur frequently in data flow problems
  124. are also implemented:
  125. * A | (B & C) : bitmap_ior_and_into
  126. * A | (B & ~C) : bitmap_ior_and_compl /
  127. bitmap_ior_and_compl_into
  128. BINARY TREE FORM
  129. ================
  130. An alternate "view" of a bitmap is its binary tree representation.
  131. For this representation, splay trees are used because they can be
  132. implemented using the same data structures as the linked list, with
  133. no overhead for meta-data (like color, or rank) on the tree nodes.
  134. In binary tree form, random-access to the set is much more efficient
  135. than for the linked-list representation. Downsides are the high cost
  136. of clearing the set, and the relatively large number of operations
  137. necessary to balance the tree. Also, iterating the set members is
  138. not supported.
  139. As for the linked-list representation, the last accessed element and
  140. its index are cached, so that membership tests on the latest accessed
  141. members is a constant-time operation. Other lookups take O(logE)
  142. time amortized (but O(E) time worst-case).
  143. The following operations can always be performed in O(1) time:
  144. * choose_one : (not implemented, but could be
  145. implemented in constant time)
  146. The following operations can be performed in O(logE) time amortized
  147. but O(E) time worst-case, but in O(1) time if the same element is
  148. accessed.
  149. * member_p : bitmap_bit_p
  150. * add_member : bitmap_set_bit
  151. * remove_member : bitmap_clear_bit
  152. The following operations can be performed in O(logE) time amortized
  153. but O(E) time worst-case:
  154. * smallest_member : bitmap_first_set_bit
  155. * largest_member : bitmap_last_set_bit
  156. * set_size : bitmap_last_set_bit
  157. The following operations can be performed in O(E) time:
  158. * clear : bitmap_clear
  159. The binary tree sparse set representation does *not* support any form
  160. of enumeration, and does also *not* support logical operations on sets.
  161. The binary tree representation is only supposed to be used for sets
  162. on which many random-access membership tests will happen. */
  163. #include "obstack.h"
  164. /* Bitmap memory usage. */
  165. struct bitmap_usage: public mem_usage
  166. {
  167. /* Default contructor. */
  168. bitmap_usage (): m_nsearches (0), m_search_iter (0) {}
  169. /* Constructor. */
  170. bitmap_usage (size_t allocated, size_t times, size_t peak,
  171. uint64_t nsearches, uint64_t search_iter)
  172. : mem_usage (allocated, times, peak),
  173. m_nsearches (nsearches), m_search_iter (search_iter) {}
  174. /* Sum the usage with SECOND usage. */
  175. bitmap_usage
  176. operator+ (const bitmap_usage &second)
  177. {
  178. return bitmap_usage (m_allocated + second.m_allocated,
  179. m_times + second.m_times,
  180. m_peak + second.m_peak,
  181. m_nsearches + second.m_nsearches,
  182. m_search_iter + second.m_search_iter);
  183. }
  184. /* Dump usage coupled to LOC location, where TOTAL is sum of all rows. */
  185. inline void
  186. dump (mem_location *loc, mem_usage &total) const
  187. {
  188. char *location_string = loc->to_string ();
  189. fprintf (stderr, "%-48s " PRsa (9) ":%5.1f%%"
  190. PRsa (9) PRsa (9) ":%5.1f%%"
  191. PRsa (11) PRsa (11) "%10s\n",
  192. location_string, SIZE_AMOUNT (m_allocated),
  193. get_percent (m_allocated, total.m_allocated),
  194. SIZE_AMOUNT (m_peak), SIZE_AMOUNT (m_times),
  195. get_percent (m_times, total.m_times),
  196. SIZE_AMOUNT (m_nsearches), SIZE_AMOUNT (m_search_iter),
  197. loc->m_ggc ? "ggc" : "heap");
  198. free (location_string);
  199. }
  200. /* Dump header with NAME. */
  201. static inline void
  202. dump_header (const char *name)
  203. {
  204. fprintf (stderr, "%-48s %11s%16s%17s%12s%12s%10s\n", name, "Leak", "Peak",
  205. "Times", "N searches", "Search iter", "Type");
  206. }
  207. /* Number search operations. */
  208. uint64_t m_nsearches;
  209. /* Number of search iterations. */
  210. uint64_t m_search_iter;
  211. };
  212. /* Bitmap memory description. */
  213. extern mem_alloc_description<bitmap_usage> bitmap_mem_desc;
  214. /* Fundamental storage type for bitmap. */
  215. typedef unsigned long BITMAP_WORD;
  216. /* BITMAP_WORD_BITS needs to be unsigned, but cannot contain casts as
  217. it is used in preprocessor directives -- hence the 1u. */
  218. #define BITMAP_WORD_BITS (CHAR_BIT * SIZEOF_LONG * 1u)
  219. /* Number of words to use for each element in the linked list. */
  220. #ifndef BITMAP_ELEMENT_WORDS
  221. #define BITMAP_ELEMENT_WORDS ((128 + BITMAP_WORD_BITS - 1) / BITMAP_WORD_BITS)
  222. #endif
  223. /* Number of bits in each actual element of a bitmap. */
  224. #define BITMAP_ELEMENT_ALL_BITS (BITMAP_ELEMENT_WORDS * BITMAP_WORD_BITS)
  225. /* Obstack for allocating bitmaps and elements from. */
  226. struct bitmap_obstack {
  227. struct bitmap_element *elements;
  228. struct bitmap_head *heads;
  229. struct obstack obstack;
  230. };
  231. /* Bitmap set element. We use a linked list to hold only the bits that
  232. are set. This allows for use to grow the bitset dynamically without
  233. having to realloc and copy a giant bit array.
  234. The free list is implemented as a list of lists. There is one
  235. outer list connected together by prev fields. Each element of that
  236. outer is an inner list (that may consist only of the outer list
  237. element) that are connected by the next fields. The prev pointer
  238. is undefined for interior elements. This allows
  239. bitmap_elt_clear_from to be implemented in unit time rather than
  240. linear in the number of elements to be freed. */
  241. struct GTY((chain_next ("%h.next"))) bitmap_element {
  242. /* In list form, the next element in the linked list;
  243. in tree form, the left child node in the tree. */
  244. struct bitmap_element *next;
  245. /* In list form, the previous element in the linked list;
  246. in tree form, the right child node in the tree. */
  247. struct bitmap_element *prev;
  248. /* regno/BITMAP_ELEMENT_ALL_BITS. */
  249. unsigned int indx;
  250. /* Bits that are set, counting from INDX, inclusive */
  251. BITMAP_WORD bits[BITMAP_ELEMENT_WORDS];
  252. };
  253. /* Head of bitmap linked list. The 'current' member points to something
  254. already pointed to by the chain started by first, so GTY((skip)) it. */
  255. struct GTY(()) bitmap_head {
  256. static bitmap_obstack crashme;
  257. /* Poison obstack to not make it not a valid initialized GC bitmap. */
  258. CONSTEXPR bitmap_head()
  259. : indx(0), tree_form(false), first(NULL), current(NULL),
  260. obstack (&crashme)
  261. {}
  262. /* Index of last element looked at. */
  263. unsigned int indx;
  264. /* False if the bitmap is in list form; true if the bitmap is in tree form.
  265. Bitmap iterators only work on bitmaps in list form. */
  266. bool tree_form;
  267. /* In list form, the first element in the linked list;
  268. in tree form, the root of the tree. */
  269. bitmap_element *first;
  270. /* Last element looked at. */
  271. bitmap_element * GTY((skip(""))) current;
  272. /* Obstack to allocate elements from. If NULL, then use GGC allocation. */
  273. bitmap_obstack * GTY((skip(""))) obstack;
  274. void dump ();
  275. };
  276. /* Global data */
  277. extern bitmap_element bitmap_zero_bits; /* Zero bitmap element */
  278. extern bitmap_obstack bitmap_default_obstack; /* Default bitmap obstack */
  279. /* Change the view of the bitmap to list, or tree. */
  280. void bitmap_list_view (bitmap);
  281. void bitmap_tree_view (bitmap);
  282. /* Clear a bitmap by freeing up the linked list. */
  283. extern void bitmap_clear (bitmap);
  284. /* Copy a bitmap to another bitmap. */
  285. extern void bitmap_copy (bitmap, const_bitmap);
  286. /* Move a bitmap to another bitmap. */
  287. extern void bitmap_move (bitmap, bitmap);
  288. /* True if two bitmaps are identical. */
  289. extern bool bitmap_equal_p (const_bitmap, const_bitmap);
  290. /* True if the bitmaps intersect (their AND is non-empty). */
  291. extern bool bitmap_intersect_p (const_bitmap, const_bitmap);
  292. /* True if the complement of the second intersects the first (their
  293. AND_COMPL is non-empty). */
  294. extern bool bitmap_intersect_compl_p (const_bitmap, const_bitmap);
  295. /* True if MAP is an empty bitmap. */
  296. inline bool bitmap_empty_p (const_bitmap map)
  297. {
  298. return !map->first;
  299. }
  300. /* True if the bitmap has only a single bit set. */
  301. extern bool bitmap_single_bit_set_p (const_bitmap);
  302. /* Count the number of bits set in the bitmap. */
  303. extern unsigned long bitmap_count_bits (const_bitmap);
  304. /* Count the number of unique bits set across the two bitmaps. */
  305. extern unsigned long bitmap_count_unique_bits (const_bitmap, const_bitmap);
  306. /* Boolean operations on bitmaps. The _into variants are two operand
  307. versions that modify the first source operand. The other variants
  308. are three operand versions that to not destroy the source bitmaps.
  309. The operations supported are &, & ~, |, ^. */
  310. extern void bitmap_and (bitmap, const_bitmap, const_bitmap);
  311. extern bool bitmap_and_into (bitmap, const_bitmap);
  312. extern bool bitmap_and_compl (bitmap, const_bitmap, const_bitmap);
  313. extern bool bitmap_and_compl_into (bitmap, const_bitmap);
  314. #define bitmap_compl_and(DST, A, B) bitmap_and_compl (DST, B, A)
  315. extern void bitmap_compl_and_into (bitmap, const_bitmap);
  316. extern void bitmap_clear_range (bitmap, unsigned int, unsigned int);
  317. extern void bitmap_set_range (bitmap, unsigned int, unsigned int);
  318. extern bool bitmap_ior (bitmap, const_bitmap, const_bitmap);
  319. extern bool bitmap_ior_into (bitmap, const_bitmap);
  320. extern void bitmap_xor (bitmap, const_bitmap, const_bitmap);
  321. extern void bitmap_xor_into (bitmap, const_bitmap);
  322. /* DST = A | (B & C). Return true if DST changes. */
  323. extern bool bitmap_ior_and_into (bitmap DST, const_bitmap B, const_bitmap C);
  324. /* DST = A | (B & ~C). Return true if DST changes. */
  325. extern bool bitmap_ior_and_compl (bitmap DST, const_bitmap A,
  326. const_bitmap B, const_bitmap C);
  327. /* A |= (B & ~C). Return true if A changes. */
  328. extern bool bitmap_ior_and_compl_into (bitmap A,
  329. const_bitmap B, const_bitmap C);
  330. /* Clear a single bit in a bitmap. Return true if the bit changed. */
  331. extern bool bitmap_clear_bit (bitmap, int);
  332. /* Set a single bit in a bitmap. Return true if the bit changed. */
  333. extern bool bitmap_set_bit (bitmap, int);
  334. /* Return true if a bit is set in a bitmap. */
  335. extern int bitmap_bit_p (bitmap, int);
  336. /* Debug functions to print a bitmap. */
  337. extern void debug_bitmap (const_bitmap);
  338. extern void debug_bitmap_file (FILE *, const_bitmap);
  339. /* Print a bitmap. */
  340. extern void bitmap_print (FILE *, const_bitmap, const char *, const char *);
  341. /* Initialize and release a bitmap obstack. */
  342. extern void bitmap_obstack_initialize (bitmap_obstack *);
  343. extern void bitmap_obstack_release (bitmap_obstack *);
  344. extern void bitmap_register (bitmap MEM_STAT_DECL);
  345. extern void dump_bitmap_statistics (void);
  346. /* Initialize a bitmap header. OBSTACK indicates the bitmap obstack
  347. to allocate from, NULL for GC'd bitmap. */
  348. static inline void
  349. bitmap_initialize (bitmap head, bitmap_obstack *obstack CXX_MEM_STAT_INFO)
  350. {
  351. head->first = head->current = NULL;
  352. head->indx = head->tree_form = 0;
  353. head->obstack = obstack;
  354. if (GATHER_STATISTICS)
  355. bitmap_register (head PASS_MEM_STAT);
  356. }
  357. /* Release a bitmap (but not its head). This is suitable for pairing with
  358. bitmap_initialize. */
  359. static inline void
  360. bitmap_release (bitmap head)
  361. {
  362. bitmap_clear (head);
  363. /* Poison the obstack pointer so the obstack can be safely released.
  364. Do not zero it as the bitmap then becomes initialized GC. */
  365. head->obstack = &bitmap_head::crashme;
  366. }
  367. /* Allocate and free bitmaps from obstack, malloc and gc'd memory. */
  368. extern bitmap bitmap_alloc (bitmap_obstack *obstack CXX_MEM_STAT_INFO);
  369. #define BITMAP_ALLOC bitmap_alloc
  370. extern bitmap bitmap_gc_alloc (ALONE_CXX_MEM_STAT_INFO);
  371. #define BITMAP_GGC_ALLOC bitmap_gc_alloc
  372. extern void bitmap_obstack_free (bitmap);
  373. /* A few compatibility/functions macros for compatibility with sbitmaps */
  374. inline void dump_bitmap (FILE *file, const_bitmap map)
  375. {
  376. bitmap_print (file, map, "", "\n");
  377. }
  378. extern void debug (const bitmap_head &ref);
  379. extern void debug (const bitmap_head *ptr);
  380. extern unsigned bitmap_first_set_bit (const_bitmap);
  381. extern unsigned bitmap_last_set_bit (const_bitmap);
  382. /* Compute bitmap hash (for purposes of hashing etc.) */
  383. extern hashval_t bitmap_hash (const_bitmap);
  384. /* Do any cleanup needed on a bitmap when it is no longer used. */
  385. #define BITMAP_FREE(BITMAP) \
  386. ((void) (bitmap_obstack_free ((bitmap) BITMAP), (BITMAP) = (bitmap) NULL))
  387. /* Iterator for bitmaps. */
  388. struct bitmap_iterator
  389. {
  390. /* Pointer to the current bitmap element. */
  391. bitmap_element *elt1;
  392. /* Pointer to 2nd bitmap element when two are involved. */
  393. bitmap_element *elt2;
  394. /* Word within the current element. */
  395. unsigned word_no;
  396. /* Contents of the actually processed word. When finding next bit
  397. it is shifted right, so that the actual bit is always the least
  398. significant bit of ACTUAL. */
  399. BITMAP_WORD bits;
  400. };
  401. /* Initialize a single bitmap iterator. START_BIT is the first bit to
  402. iterate from. */
  403. static inline void
  404. bmp_iter_set_init (bitmap_iterator *bi, const_bitmap map,
  405. unsigned start_bit, unsigned *bit_no)
  406. {
  407. bi->elt1 = map->first;
  408. bi->elt2 = NULL;
  409. gcc_checking_assert (!map->tree_form);
  410. /* Advance elt1 until it is not before the block containing start_bit. */
  411. while (1)
  412. {
  413. if (!bi->elt1)
  414. {
  415. bi->elt1 = &bitmap_zero_bits;
  416. break;
  417. }
  418. if (bi->elt1->indx >= start_bit / BITMAP_ELEMENT_ALL_BITS)
  419. break;
  420. bi->elt1 = bi->elt1->next;
  421. }
  422. /* We might have gone past the start bit, so reinitialize it. */
  423. if (bi->elt1->indx != start_bit / BITMAP_ELEMENT_ALL_BITS)
  424. start_bit = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
  425. /* Initialize for what is now start_bit. */
  426. bi->word_no = start_bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
  427. bi->bits = bi->elt1->bits[bi->word_no];
  428. bi->bits >>= start_bit % BITMAP_WORD_BITS;
  429. /* If this word is zero, we must make sure we're not pointing at the
  430. first bit, otherwise our incrementing to the next word boundary
  431. will fail. It won't matter if this increment moves us into the
  432. next word. */
  433. start_bit += !bi->bits;
  434. *bit_no = start_bit;
  435. }
  436. /* Initialize an iterator to iterate over the intersection of two
  437. bitmaps. START_BIT is the bit to commence from. */
  438. static inline void
  439. bmp_iter_and_init (bitmap_iterator *bi, const_bitmap map1, const_bitmap map2,
  440. unsigned start_bit, unsigned *bit_no)
  441. {
  442. bi->elt1 = map1->first;
  443. bi->elt2 = map2->first;
  444. gcc_checking_assert (!map1->tree_form && !map2->tree_form);
  445. /* Advance elt1 until it is not before the block containing
  446. start_bit. */
  447. while (1)
  448. {
  449. if (!bi->elt1)
  450. {
  451. bi->elt2 = NULL;
  452. break;
  453. }
  454. if (bi->elt1->indx >= start_bit / BITMAP_ELEMENT_ALL_BITS)
  455. break;
  456. bi->elt1 = bi->elt1->next;
  457. }
  458. /* Advance elt2 until it is not before elt1. */
  459. while (1)
  460. {
  461. if (!bi->elt2)
  462. {
  463. bi->elt1 = bi->elt2 = &bitmap_zero_bits;
  464. break;
  465. }
  466. if (bi->elt2->indx >= bi->elt1->indx)
  467. break;
  468. bi->elt2 = bi->elt2->next;
  469. }
  470. /* If we're at the same index, then we have some intersecting bits. */
  471. if (bi->elt1->indx == bi->elt2->indx)
  472. {
  473. /* We might have advanced beyond the start_bit, so reinitialize
  474. for that. */
  475. if (bi->elt1->indx != start_bit / BITMAP_ELEMENT_ALL_BITS)
  476. start_bit = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
  477. bi->word_no = start_bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
  478. bi->bits = bi->elt1->bits[bi->word_no] & bi->elt2->bits[bi->word_no];
  479. bi->bits >>= start_bit % BITMAP_WORD_BITS;
  480. }
  481. else
  482. {
  483. /* Otherwise we must immediately advance elt1, so initialize for
  484. that. */
  485. bi->word_no = BITMAP_ELEMENT_WORDS - 1;
  486. bi->bits = 0;
  487. }
  488. /* If this word is zero, we must make sure we're not pointing at the
  489. first bit, otherwise our incrementing to the next word boundary
  490. will fail. It won't matter if this increment moves us into the
  491. next word. */
  492. start_bit += !bi->bits;
  493. *bit_no = start_bit;
  494. }
  495. /* Initialize an iterator to iterate over the bits in MAP1 & ~MAP2. */
  496. static inline void
  497. bmp_iter_and_compl_init (bitmap_iterator *bi,
  498. const_bitmap map1, const_bitmap map2,
  499. unsigned start_bit, unsigned *bit_no)
  500. {
  501. bi->elt1 = map1->first;
  502. bi->elt2 = map2->first;
  503. gcc_checking_assert (!map1->tree_form && !map2->tree_form);
  504. /* Advance elt1 until it is not before the block containing start_bit. */
  505. while (1)
  506. {
  507. if (!bi->elt1)
  508. {
  509. bi->elt1 = &bitmap_zero_bits;
  510. break;
  511. }
  512. if (bi->elt1->indx >= start_bit / BITMAP_ELEMENT_ALL_BITS)
  513. break;
  514. bi->elt1 = bi->elt1->next;
  515. }
  516. /* Advance elt2 until it is not before elt1. */
  517. while (bi->elt2 && bi->elt2->indx < bi->elt1->indx)
  518. bi->elt2 = bi->elt2->next;
  519. /* We might have advanced beyond the start_bit, so reinitialize for
  520. that. */
  521. if (bi->elt1->indx != start_bit / BITMAP_ELEMENT_ALL_BITS)
  522. start_bit = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
  523. bi->word_no = start_bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
  524. bi->bits = bi->elt1->bits[bi->word_no];
  525. if (bi->elt2 && bi->elt1->indx == bi->elt2->indx)
  526. bi->bits &= ~bi->elt2->bits[bi->word_no];
  527. bi->bits >>= start_bit % BITMAP_WORD_BITS;
  528. /* If this word is zero, we must make sure we're not pointing at the
  529. first bit, otherwise our incrementing to the next word boundary
  530. will fail. It won't matter if this increment moves us into the
  531. next word. */
  532. start_bit += !bi->bits;
  533. *bit_no = start_bit;
  534. }
  535. /* Advance to the next bit in BI. We don't advance to the next
  536. nonzero bit yet. */
  537. static inline void
  538. bmp_iter_next (bitmap_iterator *bi, unsigned *bit_no)
  539. {
  540. bi->bits >>= 1;
  541. *bit_no += 1;
  542. }
  543. /* Advance to first set bit in BI. */
  544. static inline void
  545. bmp_iter_next_bit (bitmap_iterator * bi, unsigned *bit_no)
  546. {
  547. #if (GCC_VERSION >= 3004)
  548. {
  549. unsigned int n = __builtin_ctzl (bi->bits);
  550. gcc_assert (sizeof (unsigned long) == sizeof (BITMAP_WORD));
  551. bi->bits >>= n;
  552. *bit_no += n;
  553. }
  554. #else
  555. while (!(bi->bits & 1))
  556. {
  557. bi->bits >>= 1;
  558. *bit_no += 1;
  559. }
  560. #endif
  561. }
  562. /* Advance to the next nonzero bit of a single bitmap, we will have
  563. already advanced past the just iterated bit. Return true if there
  564. is a bit to iterate. */
  565. static inline bool
  566. bmp_iter_set (bitmap_iterator *bi, unsigned *bit_no)
  567. {
  568. /* If our current word is nonzero, it contains the bit we want. */
  569. if (bi->bits)
  570. {
  571. next_bit:
  572. bmp_iter_next_bit (bi, bit_no);
  573. return true;
  574. }
  575. /* Round up to the word boundary. We might have just iterated past
  576. the end of the last word, hence the -1. It is not possible for
  577. bit_no to point at the beginning of the now last word. */
  578. *bit_no = ((*bit_no + BITMAP_WORD_BITS - 1)
  579. / BITMAP_WORD_BITS * BITMAP_WORD_BITS);
  580. bi->word_no++;
  581. while (1)
  582. {
  583. /* Find the next nonzero word in this elt. */
  584. while (bi->word_no != BITMAP_ELEMENT_WORDS)
  585. {
  586. bi->bits = bi->elt1->bits[bi->word_no];
  587. if (bi->bits)
  588. goto next_bit;
  589. *bit_no += BITMAP_WORD_BITS;
  590. bi->word_no++;
  591. }
  592. /* Make sure we didn't remove the element while iterating. */
  593. gcc_checking_assert (bi->elt1->indx != -1U);
  594. /* Advance to the next element. */
  595. bi->elt1 = bi->elt1->next;
  596. if (!bi->elt1)
  597. return false;
  598. *bit_no = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
  599. bi->word_no = 0;
  600. }
  601. }
  602. /* Advance to the next nonzero bit of an intersecting pair of
  603. bitmaps. We will have already advanced past the just iterated bit.
  604. Return true if there is a bit to iterate. */
  605. static inline bool
  606. bmp_iter_and (bitmap_iterator *bi, unsigned *bit_no)
  607. {
  608. /* If our current word is nonzero, it contains the bit we want. */
  609. if (bi->bits)
  610. {
  611. next_bit:
  612. bmp_iter_next_bit (bi, bit_no);
  613. return true;
  614. }
  615. /* Round up to the word boundary. We might have just iterated past
  616. the end of the last word, hence the -1. It is not possible for
  617. bit_no to point at the beginning of the now last word. */
  618. *bit_no = ((*bit_no + BITMAP_WORD_BITS - 1)
  619. / BITMAP_WORD_BITS * BITMAP_WORD_BITS);
  620. bi->word_no++;
  621. while (1)
  622. {
  623. /* Find the next nonzero word in this elt. */
  624. while (bi->word_no != BITMAP_ELEMENT_WORDS)
  625. {
  626. bi->bits = bi->elt1->bits[bi->word_no] & bi->elt2->bits[bi->word_no];
  627. if (bi->bits)
  628. goto next_bit;
  629. *bit_no += BITMAP_WORD_BITS;
  630. bi->word_no++;
  631. }
  632. /* Advance to the next identical element. */
  633. do
  634. {
  635. /* Make sure we didn't remove the element while iterating. */
  636. gcc_checking_assert (bi->elt1->indx != -1U);
  637. /* Advance elt1 while it is less than elt2. We always want
  638. to advance one elt. */
  639. do
  640. {
  641. bi->elt1 = bi->elt1->next;
  642. if (!bi->elt1)
  643. return false;
  644. }
  645. while (bi->elt1->indx < bi->elt2->indx);
  646. /* Make sure we didn't remove the element while iterating. */
  647. gcc_checking_assert (bi->elt2->indx != -1U);
  648. /* Advance elt2 to be no less than elt1. This might not
  649. advance. */
  650. while (bi->elt2->indx < bi->elt1->indx)
  651. {
  652. bi->elt2 = bi->elt2->next;
  653. if (!bi->elt2)
  654. return false;
  655. }
  656. }
  657. while (bi->elt1->indx != bi->elt2->indx);
  658. *bit_no = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
  659. bi->word_no = 0;
  660. }
  661. }
  662. /* Advance to the next nonzero bit in the intersection of
  663. complemented bitmaps. We will have already advanced past the just
  664. iterated bit. */
  665. static inline bool
  666. bmp_iter_and_compl (bitmap_iterator *bi, unsigned *bit_no)
  667. {
  668. /* If our current word is nonzero, it contains the bit we want. */
  669. if (bi->bits)
  670. {
  671. next_bit:
  672. bmp_iter_next_bit (bi, bit_no);
  673. return true;
  674. }
  675. /* Round up to the word boundary. We might have just iterated past
  676. the end of the last word, hence the -1. It is not possible for
  677. bit_no to point at the beginning of the now last word. */
  678. *bit_no = ((*bit_no + BITMAP_WORD_BITS - 1)
  679. / BITMAP_WORD_BITS * BITMAP_WORD_BITS);
  680. bi->word_no++;
  681. while (1)
  682. {
  683. /* Find the next nonzero word in this elt. */
  684. while (bi->word_no != BITMAP_ELEMENT_WORDS)
  685. {
  686. bi->bits = bi->elt1->bits[bi->word_no];
  687. if (bi->elt2 && bi->elt2->indx == bi->elt1->indx)
  688. bi->bits &= ~bi->elt2->bits[bi->word_no];
  689. if (bi->bits)
  690. goto next_bit;
  691. *bit_no += BITMAP_WORD_BITS;
  692. bi->word_no++;
  693. }
  694. /* Make sure we didn't remove the element while iterating. */
  695. gcc_checking_assert (bi->elt1->indx != -1U);
  696. /* Advance to the next element of elt1. */
  697. bi->elt1 = bi->elt1->next;
  698. if (!bi->elt1)
  699. return false;
  700. /* Make sure we didn't remove the element while iterating. */
  701. gcc_checking_assert (! bi->elt2 || bi->elt2->indx != -1U);
  702. /* Advance elt2 until it is no less than elt1. */
  703. while (bi->elt2 && bi->elt2->indx < bi->elt1->indx)
  704. bi->elt2 = bi->elt2->next;
  705. *bit_no = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
  706. bi->word_no = 0;
  707. }
  708. }
  709. /* If you are modifying a bitmap you are currently iterating over you
  710. have to ensure to
  711. - never remove the current bit;
  712. - if you set or clear a bit before the current bit this operation
  713. will not affect the set of bits you are visiting during the iteration;
  714. - if you set or clear a bit after the current bit it is unspecified
  715. whether that affects the set of bits you are visiting during the
  716. iteration.
  717. If you want to remove the current bit you can delay this to the next
  718. iteration (and after the iteration in case the last iteration is
  719. affected). */
  720. /* Loop over all bits set in BITMAP, starting with MIN and setting
  721. BITNUM to the bit number. ITER is a bitmap iterator. BITNUM
  722. should be treated as a read-only variable as it contains loop
  723. state. */
  724. #ifndef EXECUTE_IF_SET_IN_BITMAP
  725. /* See sbitmap.h for the other definition of EXECUTE_IF_SET_IN_BITMAP. */
  726. #define EXECUTE_IF_SET_IN_BITMAP(BITMAP, MIN, BITNUM, ITER) \
  727. for (bmp_iter_set_init (&(ITER), (BITMAP), (MIN), &(BITNUM)); \
  728. bmp_iter_set (&(ITER), &(BITNUM)); \
  729. bmp_iter_next (&(ITER), &(BITNUM)))
  730. #endif
  731. /* Loop over all the bits set in BITMAP1 & BITMAP2, starting with MIN
  732. and setting BITNUM to the bit number. ITER is a bitmap iterator.
  733. BITNUM should be treated as a read-only variable as it contains
  734. loop state. */
  735. #define EXECUTE_IF_AND_IN_BITMAP(BITMAP1, BITMAP2, MIN, BITNUM, ITER) \
  736. for (bmp_iter_and_init (&(ITER), (BITMAP1), (BITMAP2), (MIN), \
  737. &(BITNUM)); \
  738. bmp_iter_and (&(ITER), &(BITNUM)); \
  739. bmp_iter_next (&(ITER), &(BITNUM)))
  740. /* Loop over all the bits set in BITMAP1 & ~BITMAP2, starting with MIN
  741. and setting BITNUM to the bit number. ITER is a bitmap iterator.
  742. BITNUM should be treated as a read-only variable as it contains
  743. loop state. */
  744. #define EXECUTE_IF_AND_COMPL_IN_BITMAP(BITMAP1, BITMAP2, MIN, BITNUM, ITER) \
  745. for (bmp_iter_and_compl_init (&(ITER), (BITMAP1), (BITMAP2), (MIN), \
  746. &(BITNUM)); \
  747. bmp_iter_and_compl (&(ITER), &(BITNUM)); \
  748. bmp_iter_next (&(ITER), &(BITNUM)))
  749. /* A class that ties the lifetime of a bitmap to its scope. */
  750. class auto_bitmap
  751. {
  752. public:
  753. auto_bitmap () { bitmap_initialize (&m_bits, &bitmap_default_obstack); }
  754. explicit auto_bitmap (bitmap_obstack *o) { bitmap_initialize (&m_bits, o); }
  755. ~auto_bitmap () { bitmap_clear (&m_bits); }
  756. // Allow calling bitmap functions on our bitmap.
  757. operator bitmap () { return &m_bits; }
  758. private:
  759. // Prevent making a copy that references our bitmap.
  760. auto_bitmap (const auto_bitmap &);
  761. auto_bitmap &operator = (const auto_bitmap &);
  762. #if __cplusplus >= 201103L
  763. auto_bitmap (auto_bitmap &&);
  764. auto_bitmap &operator = (auto_bitmap &&);
  765. #endif
  766. bitmap_head m_bits;
  767. };
  768. #endif /* GCC_BITMAP_H */