sbitmap.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /* Simple bitmaps.
  2. Copyright (C) 1999-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_SBITMAP_H
  16. #define GCC_SBITMAP_H
  17. /* Implementation of sets using simple bitmap vectors.
  18. This set representation is suitable for non-sparse sets with a known
  19. (a priori) universe. The set is represented as a simple array of the
  20. host's fastest unsigned integer. For a given member I in the set:
  21. - the element for I will be at sbitmap[I / (bits per element)]
  22. - the position for I within element is I % (bits per element)
  23. This representation is very space-efficient for large non-sparse sets
  24. with random access patterns.
  25. The following operations can be performed in O(1) time:
  26. * set_size : SBITMAP_SIZE
  27. * member_p : bitmap_bit_p
  28. * add_member : bitmap_set_bit
  29. * remove_member : bitmap_clear_bit
  30. Most other operations on this set representation are O(U) where U is
  31. the size of the set universe:
  32. * clear : bitmap_clear
  33. * choose_one : bitmap_first_set_bit /
  34. bitmap_last_set_bit
  35. * forall : EXECUTE_IF_SET_IN_BITMAP
  36. * set_copy : bitmap_copy
  37. * set_intersection : bitmap_and
  38. * set_union : bitmap_ior
  39. * set_difference : bitmap_and_compl
  40. * set_disjuction : (not implemented)
  41. * set_compare : bitmap_equal_p
  42. * bit_in_range_p : bitmap_bit_in_range_p
  43. Some operations on 3 sets that occur frequently in data flow problems
  44. are also implemented:
  45. * A | (B & C) : bitmap_or_and
  46. * A | (B & ~C) : bitmap_ior_and_compl
  47. * A & (B | C) : bitmap_and_or
  48. Most of the set functions have two variants: One that returns non-zero
  49. if members were added or removed from the target set, and one that just
  50. performs the operation without feedback. The former operations are a
  51. bit more expensive but the result can often be used to avoid iterations
  52. on other sets.
  53. Allocating a bitmap is done with sbitmap_alloc, and resizing is
  54. performed with sbitmap_resize.
  55. The storage requirements for simple bitmap sets is O(U) where U is the
  56. size of the set universe (colloquially the number of bits in the bitmap).
  57. This set representation works well for relatively small data flow problems
  58. (there are special routines for that, see sbitmap_vector_*). The set
  59. operations can be vectorized and there is almost no computating overhead,
  60. so that even sparse simple bitmap sets outperform dedicated sparse set
  61. representations like linked-list bitmaps. For larger problems, the size
  62. overhead of simple bitmap sets gets too high and other set representations
  63. have to be used. */
  64. #define SBITMAP_ELT_BITS (HOST_BITS_PER_WIDEST_FAST_INT * 1u)
  65. #define SBITMAP_ELT_TYPE unsigned HOST_WIDEST_FAST_INT
  66. struct simple_bitmap_def
  67. {
  68. unsigned int n_bits; /* Number of bits. */
  69. unsigned int size; /* Size in elements. */
  70. SBITMAP_ELT_TYPE elms[1]; /* The elements. */
  71. };
  72. /* Return the set size needed for N elements. */
  73. #define SBITMAP_SET_SIZE(N) (((N) + SBITMAP_ELT_BITS - 1) / SBITMAP_ELT_BITS)
  74. /* Return the number of bits in BITMAP. */
  75. #define SBITMAP_SIZE(BITMAP) ((BITMAP)->n_bits)
  76. /* Verify that access at INDEX in bitmap MAP is valid. */
  77. static inline void
  78. bitmap_check_index (const_sbitmap map, int index)
  79. {
  80. gcc_checking_assert (index >= 0);
  81. gcc_checking_assert ((unsigned int)index < map->n_bits);
  82. }
  83. /* Verify that bitmaps A and B have same size. */
  84. static inline void
  85. bitmap_check_sizes (const_sbitmap a, const_sbitmap b)
  86. {
  87. gcc_checking_assert (a->n_bits == b->n_bits);
  88. }
  89. /* Test if bit number bitno in the bitmap is set. */
  90. static inline SBITMAP_ELT_TYPE
  91. bitmap_bit_p (const_sbitmap map, int bitno)
  92. {
  93. bitmap_check_index (map, bitno);
  94. size_t i = bitno / SBITMAP_ELT_BITS;
  95. unsigned int s = bitno % SBITMAP_ELT_BITS;
  96. return (map->elms[i] >> s) & (SBITMAP_ELT_TYPE) 1;
  97. }
  98. /* Set bit number BITNO in the sbitmap MAP. */
  99. static inline void
  100. bitmap_set_bit (sbitmap map, int bitno)
  101. {
  102. bitmap_check_index (map, bitno);
  103. map->elms[bitno / SBITMAP_ELT_BITS]
  104. |= (SBITMAP_ELT_TYPE) 1 << (bitno) % SBITMAP_ELT_BITS;
  105. }
  106. /* Reset bit number BITNO in the sbitmap MAP. */
  107. static inline void
  108. bitmap_clear_bit (sbitmap map, int bitno)
  109. {
  110. bitmap_check_index (map, bitno);
  111. map->elms[bitno / SBITMAP_ELT_BITS]
  112. &= ~((SBITMAP_ELT_TYPE) 1 << (bitno) % SBITMAP_ELT_BITS);
  113. }
  114. /* The iterator for sbitmap. */
  115. struct sbitmap_iterator {
  116. /* The pointer to the first word of the bitmap. */
  117. const SBITMAP_ELT_TYPE *ptr;
  118. /* The size of the bitmap. */
  119. unsigned int size;
  120. /* The current word index. */
  121. unsigned int word_num;
  122. /* The current bit index (not modulo SBITMAP_ELT_BITS). */
  123. unsigned int bit_num;
  124. /* The words currently visited. */
  125. SBITMAP_ELT_TYPE word;
  126. };
  127. /* Initialize the iterator I with sbitmap BMP and the initial index
  128. MIN. */
  129. static inline void
  130. bmp_iter_set_init (sbitmap_iterator *i, const_sbitmap bmp,
  131. unsigned int min, unsigned *bit_no ATTRIBUTE_UNUSED)
  132. {
  133. i->word_num = min / (unsigned int) SBITMAP_ELT_BITS;
  134. i->bit_num = min;
  135. i->size = bmp->size;
  136. i->ptr = bmp->elms;
  137. if (i->word_num >= i->size)
  138. i->word = 0;
  139. else
  140. i->word = (i->ptr[i->word_num]
  141. >> (i->bit_num % (unsigned int) SBITMAP_ELT_BITS));
  142. }
  143. /* Return true if we have more bits to visit, in which case *N is set
  144. to the index of the bit to be visited. Otherwise, return
  145. false. */
  146. static inline bool
  147. bmp_iter_set (sbitmap_iterator *i, unsigned int *n)
  148. {
  149. /* Skip words that are zeros. */
  150. for (; i->word == 0; i->word = i->ptr[i->word_num])
  151. {
  152. i->word_num++;
  153. /* If we have reached the end, break. */
  154. if (i->word_num >= i->size)
  155. return false;
  156. i->bit_num = i->word_num * SBITMAP_ELT_BITS;
  157. }
  158. /* Skip bits that are zero. */
  159. for (; (i->word & 1) == 0; i->word >>= 1)
  160. i->bit_num++;
  161. *n = i->bit_num;
  162. return true;
  163. }
  164. /* Advance to the next bit. */
  165. static inline void
  166. bmp_iter_next (sbitmap_iterator *i, unsigned *bit_no ATTRIBUTE_UNUSED)
  167. {
  168. i->word >>= 1;
  169. i->bit_num++;
  170. }
  171. /* Loop over all elements of SBITMAP, starting with MIN. In each
  172. iteration, N is set to the index of the bit being visited. ITER is
  173. an instance of sbitmap_iterator used to iterate the bitmap. */
  174. #ifndef EXECUTE_IF_SET_IN_BITMAP
  175. /* See bitmap.h for the other definition of EXECUTE_IF_SET_IN_BITMAP. */
  176. #define EXECUTE_IF_SET_IN_BITMAP(BITMAP, MIN, BITNUM, ITER) \
  177. for (bmp_iter_set_init (&(ITER), (BITMAP), (MIN), &(BITNUM)); \
  178. bmp_iter_set (&(ITER), &(BITNUM)); \
  179. bmp_iter_next (&(ITER), &(BITNUM)))
  180. #endif
  181. inline void sbitmap_free (sbitmap map)
  182. {
  183. free (map);
  184. }
  185. inline void sbitmap_vector_free (sbitmap * vec)
  186. {
  187. free (vec);
  188. }
  189. extern void dump_bitmap (FILE *, const_sbitmap);
  190. extern void debug_raw (const simple_bitmap_def &ref);
  191. extern void debug_raw (const simple_bitmap_def *ptr);
  192. extern void dump_bitmap_file (FILE *, const_sbitmap);
  193. extern void debug (const simple_bitmap_def &ref);
  194. extern void debug (const simple_bitmap_def *ptr);
  195. extern void dump_bitmap_vector (FILE *, const char *, const char *, sbitmap *,
  196. int);
  197. extern sbitmap sbitmap_alloc (unsigned int);
  198. extern sbitmap *sbitmap_vector_alloc (unsigned int, unsigned int);
  199. extern sbitmap sbitmap_resize (sbitmap, unsigned int, int);
  200. extern void bitmap_copy (sbitmap, const_sbitmap);
  201. extern int bitmap_equal_p (const_sbitmap, const_sbitmap);
  202. extern unsigned int bitmap_count_bits (const_sbitmap);
  203. extern bool bitmap_empty_p (const_sbitmap);
  204. extern void bitmap_clear (sbitmap);
  205. extern void bitmap_clear_range (sbitmap, unsigned, unsigned);
  206. extern void bitmap_set_range (sbitmap, unsigned, unsigned);
  207. extern void bitmap_ones (sbitmap);
  208. extern void bitmap_vector_clear (sbitmap *, unsigned int);
  209. extern void bitmap_vector_ones (sbitmap *, unsigned int);
  210. extern bool bitmap_ior_and_compl (sbitmap, const_sbitmap,
  211. const_sbitmap, const_sbitmap);
  212. extern void bitmap_and_compl (sbitmap, const_sbitmap, const_sbitmap);
  213. extern void bitmap_not (sbitmap, const_sbitmap);
  214. extern bool bitmap_or_and (sbitmap, const_sbitmap,
  215. const_sbitmap, const_sbitmap);
  216. extern bool bitmap_and_or (sbitmap, const_sbitmap,
  217. const_sbitmap, const_sbitmap);
  218. extern bool bitmap_intersect_p (const_sbitmap, const_sbitmap);
  219. extern bool bitmap_and (sbitmap, const_sbitmap, const_sbitmap);
  220. extern bool bitmap_ior (sbitmap, const_sbitmap, const_sbitmap);
  221. extern bool bitmap_xor (sbitmap, const_sbitmap, const_sbitmap);
  222. extern bool bitmap_subset_p (const_sbitmap, const_sbitmap);
  223. extern bool bitmap_bit_in_range_p (const_sbitmap, unsigned int, unsigned int);
  224. extern int bitmap_first_set_bit (const_sbitmap);
  225. extern int bitmap_last_set_bit (const_sbitmap);
  226. extern void debug_bitmap (const_sbitmap);
  227. extern sbitmap sbitmap_realloc (sbitmap, unsigned int);
  228. /* a class that ties the lifetime of a sbitmap to its scope. */
  229. class auto_sbitmap
  230. {
  231. public:
  232. explicit auto_sbitmap (unsigned int size) :
  233. m_bitmap (sbitmap_alloc (size)) {}
  234. ~auto_sbitmap () { sbitmap_free (m_bitmap); }
  235. /* Allow calling sbitmap functions on our bitmap. */
  236. operator sbitmap () { return m_bitmap; }
  237. private:
  238. /* Prevent making a copy that refers to our sbitmap. */
  239. auto_sbitmap (const auto_sbitmap &);
  240. auto_sbitmap &operator = (const auto_sbitmap &);
  241. #if __cplusplus >= 201103L
  242. auto_sbitmap (auto_sbitmap &&);
  243. auto_sbitmap &operator = (auto_sbitmap &&);
  244. #endif
  245. /* The bitmap we are managing. */
  246. sbitmap m_bitmap;
  247. };
  248. #endif /* ! GCC_SBITMAP_H */