You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1080 lines
36KB

  1. /* Functions to support general ended bitmaps.
  2. Copyright (C) 1997-2020 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. #include "array-traits.h"
  165. /* Bitmap memory usage. */
  166. class bitmap_usage: public mem_usage
  167. {
  168. public:
  169. /* Default contructor. */
  170. bitmap_usage (): m_nsearches (0), m_search_iter (0) {}
  171. /* Constructor. */
  172. bitmap_usage (size_t allocated, size_t times, size_t peak,
  173. uint64_t nsearches, uint64_t search_iter)
  174. : mem_usage (allocated, times, peak),
  175. m_nsearches (nsearches), m_search_iter (search_iter) {}
  176. /* Sum the usage with SECOND usage. */
  177. bitmap_usage
  178. operator+ (const bitmap_usage &second)
  179. {
  180. return bitmap_usage (m_allocated + second.m_allocated,
  181. m_times + second.m_times,
  182. m_peak + second.m_peak,
  183. m_nsearches + second.m_nsearches,
  184. m_search_iter + second.m_search_iter);
  185. }
  186. /* Dump usage coupled to LOC location, where TOTAL is sum of all rows. */
  187. inline void
  188. dump (mem_location *loc, mem_usage &total) const
  189. {
  190. char *location_string = loc->to_string ();
  191. fprintf (stderr, "%-48s " PRsa (9) ":%5.1f%%"
  192. PRsa (9) PRsa (9) ":%5.1f%%"
  193. PRsa (11) PRsa (11) "%10s\n",
  194. location_string, SIZE_AMOUNT (m_allocated),
  195. get_percent (m_allocated, total.m_allocated),
  196. SIZE_AMOUNT (m_peak), SIZE_AMOUNT (m_times),
  197. get_percent (m_times, total.m_times),
  198. SIZE_AMOUNT (m_nsearches), SIZE_AMOUNT (m_search_iter),
  199. loc->m_ggc ? "ggc" : "heap");
  200. free (location_string);
  201. }
  202. /* Dump header with NAME. */
  203. static inline void
  204. dump_header (const char *name)
  205. {
  206. fprintf (stderr, "%-48s %11s%16s%17s%12s%12s%10s\n", name, "Leak", "Peak",
  207. "Times", "N searches", "Search iter", "Type");
  208. }
  209. /* Number search operations. */
  210. uint64_t m_nsearches;
  211. /* Number of search iterations. */
  212. uint64_t m_search_iter;
  213. };
  214. /* Bitmap memory description. */
  215. extern mem_alloc_description<bitmap_usage> bitmap_mem_desc;
  216. /* Fundamental storage type for bitmap. */
  217. typedef unsigned long BITMAP_WORD;
  218. /* BITMAP_WORD_BITS needs to be unsigned, but cannot contain casts as
  219. it is used in preprocessor directives -- hence the 1u. */
  220. #define BITMAP_WORD_BITS (CHAR_BIT * SIZEOF_LONG * 1u)
  221. /* Number of words to use for each element in the linked list. */
  222. #ifndef BITMAP_ELEMENT_WORDS
  223. #define BITMAP_ELEMENT_WORDS ((128 + BITMAP_WORD_BITS - 1) / BITMAP_WORD_BITS)
  224. #endif
  225. /* Number of bits in each actual element of a bitmap. */
  226. #define BITMAP_ELEMENT_ALL_BITS (BITMAP_ELEMENT_WORDS * BITMAP_WORD_BITS)
  227. /* Obstack for allocating bitmaps and elements from. */
  228. struct bitmap_obstack {
  229. struct bitmap_element *elements;
  230. bitmap_head *heads;
  231. struct obstack obstack;
  232. };
  233. /* Bitmap set element. We use a linked list to hold only the bits that
  234. are set. This allows for use to grow the bitset dynamically without
  235. having to realloc and copy a giant bit array.
  236. The free list is implemented as a list of lists. There is one
  237. outer list connected together by prev fields. Each element of that
  238. outer is an inner list (that may consist only of the outer list
  239. element) that are connected by the next fields. The prev pointer
  240. is undefined for interior elements. This allows
  241. bitmap_elt_clear_from to be implemented in unit time rather than
  242. linear in the number of elements to be freed. */
  243. struct GTY((chain_next ("%h.next"))) bitmap_element {
  244. /* In list form, the next element in the linked list;
  245. in tree form, the left child node in the tree. */
  246. struct bitmap_element *next;
  247. /* In list form, the previous element in the linked list;
  248. in tree form, the right child node in the tree. */
  249. struct bitmap_element *prev;
  250. /* regno/BITMAP_ELEMENT_ALL_BITS. */
  251. unsigned int indx;
  252. /* Bits that are set, counting from INDX, inclusive */
  253. BITMAP_WORD bits[BITMAP_ELEMENT_WORDS];
  254. };
  255. /* Head of bitmap linked list. The 'current' member points to something
  256. already pointed to by the chain started by first, so GTY((skip)) it. */
  257. class GTY(()) bitmap_head {
  258. public:
  259. static bitmap_obstack crashme;
  260. /* Poison obstack to not make it not a valid initialized GC bitmap. */
  261. CONSTEXPR bitmap_head()
  262. : indx (0), tree_form (false), padding (0), alloc_descriptor (0), first (NULL),
  263. current (NULL), obstack (&crashme)
  264. {}
  265. /* Index of last element looked at. */
  266. unsigned int indx;
  267. /* False if the bitmap is in list form; true if the bitmap is in tree form.
  268. Bitmap iterators only work on bitmaps in list form. */
  269. unsigned tree_form: 1;
  270. /* Next integer is shifted, so padding is needed. */
  271. unsigned padding: 2;
  272. /* Bitmap UID used for memory allocation statistics. */
  273. unsigned alloc_descriptor: 29;
  274. /* In list form, the first element in the linked list;
  275. in tree form, the root of the tree. */
  276. bitmap_element *first;
  277. /* Last element looked at. */
  278. bitmap_element * GTY((skip(""))) current;
  279. /* Obstack to allocate elements from. If NULL, then use GGC allocation. */
  280. bitmap_obstack * GTY((skip(""))) obstack;
  281. /* Dump bitmap. */
  282. void dump ();
  283. /* Get bitmap descriptor UID casted to an unsigned integer pointer.
  284. Shift the descriptor because pointer_hash<Type>::hash is
  285. doing >> 3 shift operation. */
  286. unsigned *get_descriptor ()
  287. {
  288. return (unsigned *)(ptrdiff_t)(alloc_descriptor << 3);
  289. }
  290. };
  291. /* Global data */
  292. extern bitmap_element bitmap_zero_bits; /* Zero bitmap element */
  293. extern bitmap_obstack bitmap_default_obstack; /* Default bitmap obstack */
  294. /* Change the view of the bitmap to list, or tree. */
  295. void bitmap_list_view (bitmap);
  296. void bitmap_tree_view (bitmap);
  297. /* Clear a bitmap by freeing up the linked list. */
  298. extern void bitmap_clear (bitmap);
  299. /* Copy a bitmap to another bitmap. */
  300. extern void bitmap_copy (bitmap, const_bitmap);
  301. /* Move a bitmap to another bitmap. */
  302. extern void bitmap_move (bitmap, bitmap);
  303. /* True if two bitmaps are identical. */
  304. extern bool bitmap_equal_p (const_bitmap, const_bitmap);
  305. /* True if the bitmaps intersect (their AND is non-empty). */
  306. extern bool bitmap_intersect_p (const_bitmap, const_bitmap);
  307. /* True if the complement of the second intersects the first (their
  308. AND_COMPL is non-empty). */
  309. extern bool bitmap_intersect_compl_p (const_bitmap, const_bitmap);
  310. /* True if MAP is an empty bitmap. */
  311. inline bool bitmap_empty_p (const_bitmap map)
  312. {
  313. return !map->first;
  314. }
  315. /* True if the bitmap has only a single bit set. */
  316. extern bool bitmap_single_bit_set_p (const_bitmap);
  317. /* Count the number of bits set in the bitmap. */
  318. extern unsigned long bitmap_count_bits (const_bitmap);
  319. /* Count the number of unique bits set across the two bitmaps. */
  320. extern unsigned long bitmap_count_unique_bits (const_bitmap, const_bitmap);
  321. /* Boolean operations on bitmaps. The _into variants are two operand
  322. versions that modify the first source operand. The other variants
  323. are three operand versions that to not destroy the source bitmaps.
  324. The operations supported are &, & ~, |, ^. */
  325. extern void bitmap_and (bitmap, const_bitmap, const_bitmap);
  326. extern bool bitmap_and_into (bitmap, const_bitmap);
  327. extern bool bitmap_and_compl (bitmap, const_bitmap, const_bitmap);
  328. extern bool bitmap_and_compl_into (bitmap, const_bitmap);
  329. #define bitmap_compl_and(DST, A, B) bitmap_and_compl (DST, B, A)
  330. extern void bitmap_compl_and_into (bitmap, const_bitmap);
  331. extern void bitmap_clear_range (bitmap, unsigned int, unsigned int);
  332. extern void bitmap_set_range (bitmap, unsigned int, unsigned int);
  333. extern bool bitmap_ior (bitmap, const_bitmap, const_bitmap);
  334. extern bool bitmap_ior_into (bitmap, const_bitmap);
  335. extern bool bitmap_ior_into_and_free (bitmap, bitmap *);
  336. extern void bitmap_xor (bitmap, const_bitmap, const_bitmap);
  337. extern void bitmap_xor_into (bitmap, const_bitmap);
  338. /* DST = A | (B & C). Return true if DST changes. */
  339. extern bool bitmap_ior_and_into (bitmap DST, const_bitmap B, const_bitmap C);
  340. /* DST = A | (B & ~C). Return true if DST changes. */
  341. extern bool bitmap_ior_and_compl (bitmap DST, const_bitmap A,
  342. const_bitmap B, const_bitmap C);
  343. /* A |= (B & ~C). Return true if A changes. */
  344. extern bool bitmap_ior_and_compl_into (bitmap A,
  345. const_bitmap B, const_bitmap C);
  346. /* Clear a single bit in a bitmap. Return true if the bit changed. */
  347. extern bool bitmap_clear_bit (bitmap, int);
  348. /* Set a single bit in a bitmap. Return true if the bit changed. */
  349. extern bool bitmap_set_bit (bitmap, int);
  350. /* Return true if a bit is set in a bitmap. */
  351. extern int bitmap_bit_p (const_bitmap, int);
  352. /* Debug functions to print a bitmap. */
  353. extern void debug_bitmap (const_bitmap);
  354. extern void debug_bitmap_file (FILE *, const_bitmap);
  355. /* Print a bitmap. */
  356. extern void bitmap_print (FILE *, const_bitmap, const char *, const char *);
  357. /* Initialize and release a bitmap obstack. */
  358. extern void bitmap_obstack_initialize (bitmap_obstack *);
  359. extern void bitmap_obstack_release (bitmap_obstack *);
  360. extern void bitmap_register (bitmap MEM_STAT_DECL);
  361. extern void dump_bitmap_statistics (void);
  362. /* Initialize a bitmap header. OBSTACK indicates the bitmap obstack
  363. to allocate from, NULL for GC'd bitmap. */
  364. static inline void
  365. bitmap_initialize (bitmap head, bitmap_obstack *obstack CXX_MEM_STAT_INFO)
  366. {
  367. head->first = head->current = NULL;
  368. head->indx = head->tree_form = 0;
  369. head->padding = 0;
  370. head->alloc_descriptor = 0;
  371. head->obstack = obstack;
  372. if (GATHER_STATISTICS)
  373. bitmap_register (head PASS_MEM_STAT);
  374. }
  375. /* Release a bitmap (but not its head). This is suitable for pairing with
  376. bitmap_initialize. */
  377. static inline void
  378. bitmap_release (bitmap head)
  379. {
  380. bitmap_clear (head);
  381. /* Poison the obstack pointer so the obstack can be safely released.
  382. Do not zero it as the bitmap then becomes initialized GC. */
  383. head->obstack = &bitmap_head::crashme;
  384. }
  385. /* Allocate and free bitmaps from obstack, malloc and gc'd memory. */
  386. extern bitmap bitmap_alloc (bitmap_obstack *obstack CXX_MEM_STAT_INFO);
  387. #define BITMAP_ALLOC bitmap_alloc
  388. extern bitmap bitmap_gc_alloc (ALONE_CXX_MEM_STAT_INFO);
  389. #define BITMAP_GGC_ALLOC bitmap_gc_alloc
  390. extern void bitmap_obstack_free (bitmap);
  391. /* A few compatibility/functions macros for compatibility with sbitmaps */
  392. inline void dump_bitmap (FILE *file, const_bitmap map)
  393. {
  394. bitmap_print (file, map, "", "\n");
  395. }
  396. extern void debug (const bitmap_head &ref);
  397. extern void debug (const bitmap_head *ptr);
  398. extern unsigned bitmap_first_set_bit (const_bitmap);
  399. extern unsigned bitmap_last_set_bit (const_bitmap);
  400. /* Compute bitmap hash (for purposes of hashing etc.) */
  401. extern hashval_t bitmap_hash (const_bitmap);
  402. /* Do any cleanup needed on a bitmap when it is no longer used. */
  403. #define BITMAP_FREE(BITMAP) \
  404. ((void) (bitmap_obstack_free ((bitmap) BITMAP), (BITMAP) = (bitmap) NULL))
  405. /* Iterator for bitmaps. */
  406. struct bitmap_iterator
  407. {
  408. /* Pointer to the current bitmap element. */
  409. bitmap_element *elt1;
  410. /* Pointer to 2nd bitmap element when two are involved. */
  411. bitmap_element *elt2;
  412. /* Word within the current element. */
  413. unsigned word_no;
  414. /* Contents of the actually processed word. When finding next bit
  415. it is shifted right, so that the actual bit is always the least
  416. significant bit of ACTUAL. */
  417. BITMAP_WORD bits;
  418. };
  419. /* Initialize a single bitmap iterator. START_BIT is the first bit to
  420. iterate from. */
  421. static inline void
  422. bmp_iter_set_init (bitmap_iterator *bi, const_bitmap map,
  423. unsigned start_bit, unsigned *bit_no)
  424. {
  425. bi->elt1 = map->first;
  426. bi->elt2 = NULL;
  427. gcc_checking_assert (!map->tree_form);
  428. /* Advance elt1 until it is not before the block containing start_bit. */
  429. while (1)
  430. {
  431. if (!bi->elt1)
  432. {
  433. bi->elt1 = &bitmap_zero_bits;
  434. break;
  435. }
  436. if (bi->elt1->indx >= start_bit / BITMAP_ELEMENT_ALL_BITS)
  437. break;
  438. bi->elt1 = bi->elt1->next;
  439. }
  440. /* We might have gone past the start bit, so reinitialize it. */
  441. if (bi->elt1->indx != start_bit / BITMAP_ELEMENT_ALL_BITS)
  442. start_bit = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
  443. /* Initialize for what is now start_bit. */
  444. bi->word_no = start_bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
  445. bi->bits = bi->elt1->bits[bi->word_no];
  446. bi->bits >>= start_bit % BITMAP_WORD_BITS;
  447. /* If this word is zero, we must make sure we're not pointing at the
  448. first bit, otherwise our incrementing to the next word boundary
  449. will fail. It won't matter if this increment moves us into the
  450. next word. */
  451. start_bit += !bi->bits;
  452. *bit_no = start_bit;
  453. }
  454. /* Initialize an iterator to iterate over the intersection of two
  455. bitmaps. START_BIT is the bit to commence from. */
  456. static inline void
  457. bmp_iter_and_init (bitmap_iterator *bi, const_bitmap map1, const_bitmap map2,
  458. unsigned start_bit, unsigned *bit_no)
  459. {
  460. bi->elt1 = map1->first;
  461. bi->elt2 = map2->first;
  462. gcc_checking_assert (!map1->tree_form && !map2->tree_form);
  463. /* Advance elt1 until it is not before the block containing
  464. start_bit. */
  465. while (1)
  466. {
  467. if (!bi->elt1)
  468. {
  469. bi->elt2 = NULL;
  470. break;
  471. }
  472. if (bi->elt1->indx >= start_bit / BITMAP_ELEMENT_ALL_BITS)
  473. break;
  474. bi->elt1 = bi->elt1->next;
  475. }
  476. /* Advance elt2 until it is not before elt1. */
  477. while (1)
  478. {
  479. if (!bi->elt2)
  480. {
  481. bi->elt1 = bi->elt2 = &bitmap_zero_bits;
  482. break;
  483. }
  484. if (bi->elt2->indx >= bi->elt1->indx)
  485. break;
  486. bi->elt2 = bi->elt2->next;
  487. }
  488. /* If we're at the same index, then we have some intersecting bits. */
  489. if (bi->elt1->indx == bi->elt2->indx)
  490. {
  491. /* We might have advanced beyond the start_bit, so reinitialize
  492. for that. */
  493. if (bi->elt1->indx != start_bit / BITMAP_ELEMENT_ALL_BITS)
  494. start_bit = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
  495. bi->word_no = start_bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
  496. bi->bits = bi->elt1->bits[bi->word_no] & bi->elt2->bits[bi->word_no];
  497. bi->bits >>= start_bit % BITMAP_WORD_BITS;
  498. }
  499. else
  500. {
  501. /* Otherwise we must immediately advance elt1, so initialize for
  502. that. */
  503. bi->word_no = BITMAP_ELEMENT_WORDS - 1;
  504. bi->bits = 0;
  505. }
  506. /* If this word is zero, we must make sure we're not pointing at the
  507. first bit, otherwise our incrementing to the next word boundary
  508. will fail. It won't matter if this increment moves us into the
  509. next word. */
  510. start_bit += !bi->bits;
  511. *bit_no = start_bit;
  512. }
  513. /* Initialize an iterator to iterate over the bits in MAP1 & ~MAP2. */
  514. static inline void
  515. bmp_iter_and_compl_init (bitmap_iterator *bi,
  516. const_bitmap map1, const_bitmap map2,
  517. unsigned start_bit, unsigned *bit_no)
  518. {
  519. bi->elt1 = map1->first;
  520. bi->elt2 = map2->first;
  521. gcc_checking_assert (!map1->tree_form && !map2->tree_form);
  522. /* Advance elt1 until it is not before the block containing start_bit. */
  523. while (1)
  524. {
  525. if (!bi->elt1)
  526. {
  527. bi->elt1 = &bitmap_zero_bits;
  528. break;
  529. }
  530. if (bi->elt1->indx >= start_bit / BITMAP_ELEMENT_ALL_BITS)
  531. break;
  532. bi->elt1 = bi->elt1->next;
  533. }
  534. /* Advance elt2 until it is not before elt1. */
  535. while (bi->elt2 && bi->elt2->indx < bi->elt1->indx)
  536. bi->elt2 = bi->elt2->next;
  537. /* We might have advanced beyond the start_bit, so reinitialize for
  538. that. */
  539. if (bi->elt1->indx != start_bit / BITMAP_ELEMENT_ALL_BITS)
  540. start_bit = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
  541. bi->word_no = start_bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
  542. bi->bits = bi->elt1->bits[bi->word_no];
  543. if (bi->elt2 && bi->elt1->indx == bi->elt2->indx)
  544. bi->bits &= ~bi->elt2->bits[bi->word_no];
  545. bi->bits >>= start_bit % BITMAP_WORD_BITS;
  546. /* If this word is zero, we must make sure we're not pointing at the
  547. first bit, otherwise our incrementing to the next word boundary
  548. will fail. It won't matter if this increment moves us into the
  549. next word. */
  550. start_bit += !bi->bits;
  551. *bit_no = start_bit;
  552. }
  553. /* Advance to the next bit in BI. We don't advance to the next
  554. nonzero bit yet. */
  555. static inline void
  556. bmp_iter_next (bitmap_iterator *bi, unsigned *bit_no)
  557. {
  558. bi->bits >>= 1;
  559. *bit_no += 1;
  560. }
  561. /* Advance to first set bit in BI. */
  562. static inline void
  563. bmp_iter_next_bit (bitmap_iterator * bi, unsigned *bit_no)
  564. {
  565. #if (GCC_VERSION >= 3004)
  566. {
  567. unsigned int n = __builtin_ctzl (bi->bits);
  568. gcc_assert (sizeof (unsigned long) == sizeof (BITMAP_WORD));
  569. bi->bits >>= n;
  570. *bit_no += n;
  571. }
  572. #else
  573. while (!(bi->bits & 1))
  574. {
  575. bi->bits >>= 1;
  576. *bit_no += 1;
  577. }
  578. #endif
  579. }
  580. /* Advance to the next nonzero bit of a single bitmap, we will have
  581. already advanced past the just iterated bit. Return true if there
  582. is a bit to iterate. */
  583. static inline bool
  584. bmp_iter_set (bitmap_iterator *bi, unsigned *bit_no)
  585. {
  586. /* If our current word is nonzero, it contains the bit we want. */
  587. if (bi->bits)
  588. {
  589. next_bit:
  590. bmp_iter_next_bit (bi, bit_no);
  591. return true;
  592. }
  593. /* Round up to the word boundary. We might have just iterated past
  594. the end of the last word, hence the -1. It is not possible for
  595. bit_no to point at the beginning of the now last word. */
  596. *bit_no = ((*bit_no + BITMAP_WORD_BITS - 1)
  597. / BITMAP_WORD_BITS * BITMAP_WORD_BITS);
  598. bi->word_no++;
  599. while (1)
  600. {
  601. /* Find the next nonzero word in this elt. */
  602. while (bi->word_no != BITMAP_ELEMENT_WORDS)
  603. {
  604. bi->bits = bi->elt1->bits[bi->word_no];
  605. if (bi->bits)
  606. goto next_bit;
  607. *bit_no += BITMAP_WORD_BITS;
  608. bi->word_no++;
  609. }
  610. /* Make sure we didn't remove the element while iterating. */
  611. gcc_checking_assert (bi->elt1->indx != -1U);
  612. /* Advance to the next element. */
  613. bi->elt1 = bi->elt1->next;
  614. if (!bi->elt1)
  615. return false;
  616. *bit_no = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
  617. bi->word_no = 0;
  618. }
  619. }
  620. /* Advance to the next nonzero bit of an intersecting pair of
  621. bitmaps. We will have already advanced past the just iterated bit.
  622. Return true if there is a bit to iterate. */
  623. static inline bool
  624. bmp_iter_and (bitmap_iterator *bi, unsigned *bit_no)
  625. {
  626. /* If our current word is nonzero, it contains the bit we want. */
  627. if (bi->bits)
  628. {
  629. next_bit:
  630. bmp_iter_next_bit (bi, bit_no);
  631. return true;
  632. }
  633. /* Round up to the word boundary. We might have just iterated past
  634. the end of the last word, hence the -1. It is not possible for
  635. bit_no to point at the beginning of the now last word. */
  636. *bit_no = ((*bit_no + BITMAP_WORD_BITS - 1)
  637. / BITMAP_WORD_BITS * BITMAP_WORD_BITS);
  638. bi->word_no++;
  639. while (1)
  640. {
  641. /* Find the next nonzero word in this elt. */
  642. while (bi->word_no != BITMAP_ELEMENT_WORDS)
  643. {
  644. bi->bits = bi->elt1->bits[bi->word_no] & bi->elt2->bits[bi->word_no];
  645. if (bi->bits)
  646. goto next_bit;
  647. *bit_no += BITMAP_WORD_BITS;
  648. bi->word_no++;
  649. }
  650. /* Advance to the next identical element. */
  651. do
  652. {
  653. /* Make sure we didn't remove the element while iterating. */
  654. gcc_checking_assert (bi->elt1->indx != -1U);
  655. /* Advance elt1 while it is less than elt2. We always want
  656. to advance one elt. */
  657. do
  658. {
  659. bi->elt1 = bi->elt1->next;
  660. if (!bi->elt1)
  661. return false;
  662. }
  663. while (bi->elt1->indx < bi->elt2->indx);
  664. /* Make sure we didn't remove the element while iterating. */
  665. gcc_checking_assert (bi->elt2->indx != -1U);
  666. /* Advance elt2 to be no less than elt1. This might not
  667. advance. */
  668. while (bi->elt2->indx < bi->elt1->indx)
  669. {
  670. bi->elt2 = bi->elt2->next;
  671. if (!bi->elt2)
  672. return false;
  673. }
  674. }
  675. while (bi->elt1->indx != bi->elt2->indx);
  676. *bit_no = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
  677. bi->word_no = 0;
  678. }
  679. }
  680. /* Advance to the next nonzero bit in the intersection of
  681. complemented bitmaps. We will have already advanced past the just
  682. iterated bit. */
  683. static inline bool
  684. bmp_iter_and_compl (bitmap_iterator *bi, unsigned *bit_no)
  685. {
  686. /* If our current word is nonzero, it contains the bit we want. */
  687. if (bi->bits)
  688. {
  689. next_bit:
  690. bmp_iter_next_bit (bi, bit_no);
  691. return true;
  692. }
  693. /* Round up to the word boundary. We might have just iterated past
  694. the end of the last word, hence the -1. It is not possible for
  695. bit_no to point at the beginning of the now last word. */
  696. *bit_no = ((*bit_no + BITMAP_WORD_BITS - 1)
  697. / BITMAP_WORD_BITS * BITMAP_WORD_BITS);
  698. bi->word_no++;
  699. while (1)
  700. {
  701. /* Find the next nonzero word in this elt. */
  702. while (bi->word_no != BITMAP_ELEMENT_WORDS)
  703. {
  704. bi->bits = bi->elt1->bits[bi->word_no];
  705. if (bi->elt2 && bi->elt2->indx == bi->elt1->indx)
  706. bi->bits &= ~bi->elt2->bits[bi->word_no];
  707. if (bi->bits)
  708. goto next_bit;
  709. *bit_no += BITMAP_WORD_BITS;
  710. bi->word_no++;
  711. }
  712. /* Make sure we didn't remove the element while iterating. */
  713. gcc_checking_assert (bi->elt1->indx != -1U);
  714. /* Advance to the next element of elt1. */
  715. bi->elt1 = bi->elt1->next;
  716. if (!bi->elt1)
  717. return false;
  718. /* Make sure we didn't remove the element while iterating. */
  719. gcc_checking_assert (! bi->elt2 || bi->elt2->indx != -1U);
  720. /* Advance elt2 until it is no less than elt1. */
  721. while (bi->elt2 && bi->elt2->indx < bi->elt1->indx)
  722. bi->elt2 = bi->elt2->next;
  723. *bit_no = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
  724. bi->word_no = 0;
  725. }
  726. }
  727. /* If you are modifying a bitmap you are currently iterating over you
  728. have to ensure to
  729. - never remove the current bit;
  730. - if you set or clear a bit before the current bit this operation
  731. will not affect the set of bits you are visiting during the iteration;
  732. - if you set or clear a bit after the current bit it is unspecified
  733. whether that affects the set of bits you are visiting during the
  734. iteration.
  735. If you want to remove the current bit you can delay this to the next
  736. iteration (and after the iteration in case the last iteration is
  737. affected). */
  738. /* Loop over all bits set in BITMAP, starting with MIN and setting
  739. BITNUM to the bit number. ITER is a bitmap iterator. BITNUM
  740. should be treated as a read-only variable as it contains loop
  741. state. */
  742. #ifndef EXECUTE_IF_SET_IN_BITMAP
  743. /* See sbitmap.h for the other definition of EXECUTE_IF_SET_IN_BITMAP. */
  744. #define EXECUTE_IF_SET_IN_BITMAP(BITMAP, MIN, BITNUM, ITER) \
  745. for (bmp_iter_set_init (&(ITER), (BITMAP), (MIN), &(BITNUM)); \
  746. bmp_iter_set (&(ITER), &(BITNUM)); \
  747. bmp_iter_next (&(ITER), &(BITNUM)))
  748. #endif
  749. /* Loop over all the bits set in BITMAP1 & BITMAP2, starting with MIN
  750. and setting BITNUM to the bit number. ITER is a bitmap iterator.
  751. BITNUM should be treated as a read-only variable as it contains
  752. loop state. */
  753. #define EXECUTE_IF_AND_IN_BITMAP(BITMAP1, BITMAP2, MIN, BITNUM, ITER) \
  754. for (bmp_iter_and_init (&(ITER), (BITMAP1), (BITMAP2), (MIN), \
  755. &(BITNUM)); \
  756. bmp_iter_and (&(ITER), &(BITNUM)); \
  757. bmp_iter_next (&(ITER), &(BITNUM)))
  758. /* Loop over all the bits set in BITMAP1 & ~BITMAP2, starting with MIN
  759. and setting BITNUM to the bit number. ITER is a bitmap iterator.
  760. BITNUM should be treated as a read-only variable as it contains
  761. loop state. */
  762. #define EXECUTE_IF_AND_COMPL_IN_BITMAP(BITMAP1, BITMAP2, MIN, BITNUM, ITER) \
  763. for (bmp_iter_and_compl_init (&(ITER), (BITMAP1), (BITMAP2), (MIN), \
  764. &(BITNUM)); \
  765. bmp_iter_and_compl (&(ITER), &(BITNUM)); \
  766. bmp_iter_next (&(ITER), &(BITNUM)))
  767. /* A class that ties the lifetime of a bitmap to its scope. */
  768. class auto_bitmap
  769. {
  770. public:
  771. auto_bitmap () { bitmap_initialize (&m_bits, &bitmap_default_obstack); }
  772. explicit auto_bitmap (bitmap_obstack *o) { bitmap_initialize (&m_bits, o); }
  773. ~auto_bitmap () { bitmap_clear (&m_bits); }
  774. // Allow calling bitmap functions on our bitmap.
  775. operator bitmap () { return &m_bits; }
  776. private:
  777. // Prevent making a copy that references our bitmap.
  778. auto_bitmap (const auto_bitmap &);
  779. auto_bitmap &operator = (const auto_bitmap &);
  780. #if __cplusplus >= 201103L
  781. auto_bitmap (auto_bitmap &&);
  782. auto_bitmap &operator = (auto_bitmap &&);
  783. #endif
  784. bitmap_head m_bits;
  785. };
  786. /* Base class for bitmap_view; see there for details. */
  787. template<typename T, typename Traits = array_traits<T> >
  788. class base_bitmap_view
  789. {
  790. public:
  791. typedef typename Traits::element_type array_element_type;
  792. base_bitmap_view (const T &, bitmap_element *);
  793. operator const_bitmap () const { return &m_head; }
  794. private:
  795. base_bitmap_view (const base_bitmap_view &);
  796. bitmap_head m_head;
  797. };
  798. /* Provides a read-only bitmap view of a single integer bitmask or a
  799. constant-sized array of integer bitmasks, or of a wrapper around such
  800. bitmasks. */
  801. template<typename T, typename Traits>
  802. class bitmap_view<T, Traits, true> : public base_bitmap_view<T, Traits>
  803. {
  804. public:
  805. bitmap_view (const T &array)
  806. : base_bitmap_view<T, Traits> (array, m_bitmap_elements) {}
  807. private:
  808. /* How many bitmap_elements we need to hold a full T. */
  809. static const size_t num_bitmap_elements
  810. = CEIL (CHAR_BIT
  811. * sizeof (typename Traits::element_type)
  812. * Traits::constant_size,
  813. BITMAP_ELEMENT_ALL_BITS);
  814. bitmap_element m_bitmap_elements[num_bitmap_elements];
  815. };
  816. /* Initialize the view for array ARRAY, using the array of bitmap
  817. elements in BITMAP_ELEMENTS (which is known to contain enough
  818. entries). */
  819. template<typename T, typename Traits>
  820. base_bitmap_view<T, Traits>::base_bitmap_view (const T &array,
  821. bitmap_element *bitmap_elements)
  822. {
  823. m_head.obstack = NULL;
  824. /* The code currently assumes that each element of ARRAY corresponds
  825. to exactly one bitmap_element. */
  826. const size_t array_element_bits = CHAR_BIT * sizeof (array_element_type);
  827. STATIC_ASSERT (BITMAP_ELEMENT_ALL_BITS % array_element_bits == 0);
  828. size_t array_step = BITMAP_ELEMENT_ALL_BITS / array_element_bits;
  829. size_t array_size = Traits::size (array);
  830. /* Process each potential bitmap_element in turn. The loop is written
  831. this way rather than per array element because usually there are
  832. only a small number of array elements per bitmap element (typically
  833. two or four). The inner loops should therefore unroll completely. */
  834. const array_element_type *array_elements = Traits::base (array);
  835. unsigned int indx = 0;
  836. for (size_t array_base = 0;
  837. array_base < array_size;
  838. array_base += array_step, indx += 1)
  839. {
  840. /* How many array elements are in this particular bitmap_element. */
  841. unsigned int array_count
  842. = (STATIC_CONSTANT_P (array_size % array_step == 0)
  843. ? array_step : MIN (array_step, array_size - array_base));
  844. /* See whether we need this bitmap element. */
  845. array_element_type ior = array_elements[array_base];
  846. for (size_t i = 1; i < array_count; ++i)
  847. ior |= array_elements[array_base + i];
  848. if (ior == 0)
  849. continue;
  850. /* Grab the next bitmap element and chain it. */
  851. bitmap_element *bitmap_element = bitmap_elements++;
  852. if (m_head.current)
  853. m_head.current->next = bitmap_element;
  854. else
  855. m_head.first = bitmap_element;
  856. bitmap_element->prev = m_head.current;
  857. bitmap_element->next = NULL;
  858. bitmap_element->indx = indx;
  859. m_head.current = bitmap_element;
  860. m_head.indx = indx;
  861. /* Fill in the bits of the bitmap element. */
  862. if (array_element_bits < BITMAP_WORD_BITS)
  863. {
  864. /* Multiple array elements fit in one element of
  865. bitmap_element->bits. */
  866. size_t array_i = array_base;
  867. for (unsigned int word_i = 0; word_i < BITMAP_ELEMENT_WORDS;
  868. ++word_i)
  869. {
  870. BITMAP_WORD word = 0;
  871. for (unsigned int shift = 0;
  872. shift < BITMAP_WORD_BITS && array_i < array_size;
  873. shift += array_element_bits)
  874. word |= array_elements[array_i++] << shift;
  875. bitmap_element->bits[word_i] = word;
  876. }
  877. }
  878. else
  879. {
  880. /* Array elements are the same size as elements of
  881. bitmap_element->bits, or are an exact multiple of that size. */
  882. unsigned int word_i = 0;
  883. for (unsigned int i = 0; i < array_count; ++i)
  884. for (unsigned int shift = 0; shift < array_element_bits;
  885. shift += BITMAP_WORD_BITS)
  886. bitmap_element->bits[word_i++]
  887. = array_elements[array_base + i] >> shift;
  888. while (word_i < BITMAP_ELEMENT_WORDS)
  889. bitmap_element->bits[word_i++] = 0;
  890. }
  891. }
  892. }
  893. #endif /* GCC_BITMAP_H */