sbitmap.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /* Simple bitmaps.
  2. Copyright (C) 1999-2015 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. #ifndef GCC_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. Some operations on 3 sets that occur frequently in in data flow problems
  43. are also implemented:
  44. * A | (B & C) : bitmap_or_and
  45. * A | (B & ~C) : bitmap_ior_and_compl
  46. * A & (B | C) : bitmap_and_or
  47. Most of the set functions have two variants: One that returns non-zero
  48. if members were added or removed from the target set, and one that just
  49. performs the operation without feedback. The former operations are a
  50. bit more expensive but the result can often be used to avoid iterations
  51. on other sets.
  52. Allocating a bitmap is done with sbitmap_alloc, and resizing is
  53. performed with sbitmap_resize.
  54. The storage requirements for simple bitmap sets is O(U) where U is the
  55. size of the set universe (colloquially the number of bits in the bitmap).
  56. This set representation works well for relatively small data flow problems
  57. (there are special routines for that, see sbitmap_vector_*). The set
  58. operations can be vectorized and there is almost no computating overhead,
  59. so that even sparse simple bitmap sets outperform dedicated sparse set
  60. representations like linked-list bitmaps. For larger problems, the size
  61. overhead of simple bitmap sets gets too high and other set representations
  62. have to be used. */
  63. #define SBITMAP_ELT_BITS (HOST_BITS_PER_WIDEST_FAST_INT * 1u)
  64. #define SBITMAP_ELT_TYPE unsigned HOST_WIDEST_FAST_INT
  65. struct simple_bitmap_def
  66. {
  67. unsigned char *popcount; /* Population count. */
  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. /* Test if bit number bitno in the bitmap is set. */
  77. static inline SBITMAP_ELT_TYPE
  78. bitmap_bit_p (const_sbitmap map, int bitno)
  79. {
  80. size_t i = bitno / SBITMAP_ELT_BITS;
  81. unsigned int s = bitno % SBITMAP_ELT_BITS;
  82. return (map->elms[i] >> s) & (SBITMAP_ELT_TYPE) 1;
  83. }
  84. /* Set bit number BITNO in the sbitmap MAP. */
  85. static inline void
  86. bitmap_set_bit (sbitmap map, int bitno)
  87. {
  88. gcc_checking_assert (! map->popcount);
  89. map->elms[bitno / SBITMAP_ELT_BITS]
  90. |= (SBITMAP_ELT_TYPE) 1 << (bitno) % SBITMAP_ELT_BITS;
  91. }
  92. /* Reset bit number BITNO in the sbitmap MAP. */
  93. static inline void
  94. bitmap_clear_bit (sbitmap map, int bitno)
  95. {
  96. gcc_checking_assert (! map->popcount);
  97. map->elms[bitno / SBITMAP_ELT_BITS]
  98. &= ~((SBITMAP_ELT_TYPE) 1 << (bitno) % SBITMAP_ELT_BITS);
  99. }
  100. /* The iterator for sbitmap. */
  101. struct sbitmap_iterator {
  102. /* The pointer to the first word of the bitmap. */
  103. const SBITMAP_ELT_TYPE *ptr;
  104. /* The size of the bitmap. */
  105. unsigned int size;
  106. /* The current word index. */
  107. unsigned int word_num;
  108. /* The current bit index (not modulo SBITMAP_ELT_BITS). */
  109. unsigned int bit_num;
  110. /* The words currently visited. */
  111. SBITMAP_ELT_TYPE word;
  112. };
  113. /* Initialize the iterator I with sbitmap BMP and the initial index
  114. MIN. */
  115. static inline void
  116. bmp_iter_set_init (sbitmap_iterator *i, const_sbitmap bmp,
  117. unsigned int min, unsigned *bit_no ATTRIBUTE_UNUSED)
  118. {
  119. i->word_num = min / (unsigned int) SBITMAP_ELT_BITS;
  120. i->bit_num = min;
  121. i->size = bmp->size;
  122. i->ptr = bmp->elms;
  123. if (i->word_num >= i->size)
  124. i->word = 0;
  125. else
  126. i->word = (i->ptr[i->word_num]
  127. >> (i->bit_num % (unsigned int) SBITMAP_ELT_BITS));
  128. }
  129. /* Return true if we have more bits to visit, in which case *N is set
  130. to the index of the bit to be visited. Otherwise, return
  131. false. */
  132. static inline bool
  133. bmp_iter_set (sbitmap_iterator *i, unsigned int *n)
  134. {
  135. /* Skip words that are zeros. */
  136. for (; i->word == 0; i->word = i->ptr[i->word_num])
  137. {
  138. i->word_num++;
  139. /* If we have reached the end, break. */
  140. if (i->word_num >= i->size)
  141. return false;
  142. i->bit_num = i->word_num * SBITMAP_ELT_BITS;
  143. }
  144. /* Skip bits that are zero. */
  145. for (; (i->word & 1) == 0; i->word >>= 1)
  146. i->bit_num++;
  147. *n = i->bit_num;
  148. return true;
  149. }
  150. /* Advance to the next bit. */
  151. static inline void
  152. bmp_iter_next (sbitmap_iterator *i, unsigned *bit_no ATTRIBUTE_UNUSED)
  153. {
  154. i->word >>= 1;
  155. i->bit_num++;
  156. }
  157. /* Loop over all elements of SBITMAP, starting with MIN. In each
  158. iteration, N is set to the index of the bit being visited. ITER is
  159. an instance of sbitmap_iterator used to iterate the bitmap. */
  160. #ifndef EXECUTE_IF_SET_IN_BITMAP
  161. /* See bitmap.h for the other definition of EXECUTE_IF_SET_IN_BITMAP. */
  162. #define EXECUTE_IF_SET_IN_BITMAP(BITMAP, MIN, BITNUM, ITER) \
  163. for (bmp_iter_set_init (&(ITER), (BITMAP), (MIN), &(BITNUM)); \
  164. bmp_iter_set (&(ITER), &(BITNUM)); \
  165. bmp_iter_next (&(ITER), &(BITNUM)))
  166. #endif
  167. inline void sbitmap_free (sbitmap map)
  168. {
  169. free (map->popcount);
  170. free (map);
  171. }
  172. inline void sbitmap_vector_free (sbitmap * vec)
  173. {
  174. free (vec);
  175. }
  176. extern void dump_bitmap (FILE *, const_sbitmap);
  177. extern void debug_raw (const simple_bitmap_def &ref);
  178. extern void debug_raw (const simple_bitmap_def *ptr);
  179. extern void dump_bitmap_file (FILE *, const_sbitmap);
  180. extern void debug (const simple_bitmap_def &ref);
  181. extern void debug (const simple_bitmap_def *ptr);
  182. extern void dump_bitmap_vector (FILE *, const char *, const char *, sbitmap *,
  183. int);
  184. extern sbitmap sbitmap_alloc (unsigned int);
  185. extern sbitmap sbitmap_alloc_with_popcount (unsigned int);
  186. extern sbitmap *sbitmap_vector_alloc (unsigned int, unsigned int);
  187. extern sbitmap sbitmap_resize (sbitmap, unsigned int, int);
  188. extern void bitmap_copy (sbitmap, const_sbitmap);
  189. extern int bitmap_equal_p (const_sbitmap, const_sbitmap);
  190. extern bool bitmap_empty_p (const_sbitmap);
  191. extern void bitmap_clear (sbitmap);
  192. extern void bitmap_ones (sbitmap);
  193. extern void bitmap_vector_clear (sbitmap *, unsigned int);
  194. extern void bitmap_vector_ones (sbitmap *, unsigned int);
  195. extern bool bitmap_ior_and_compl (sbitmap, const_sbitmap,
  196. const_sbitmap, const_sbitmap);
  197. extern void bitmap_and_compl (sbitmap, const_sbitmap, const_sbitmap);
  198. extern void bitmap_not (sbitmap, const_sbitmap);
  199. extern bool bitmap_or_and (sbitmap, const_sbitmap,
  200. const_sbitmap, const_sbitmap);
  201. extern bool bitmap_and_or (sbitmap, const_sbitmap,
  202. const_sbitmap, const_sbitmap);
  203. extern bool bitmap_intersect_p (const_sbitmap, const_sbitmap);
  204. extern bool bitmap_and (sbitmap, const_sbitmap, const_sbitmap);
  205. extern bool bitmap_ior (sbitmap, const_sbitmap, const_sbitmap);
  206. extern bool bitmap_xor (sbitmap, const_sbitmap, const_sbitmap);
  207. extern bool bitmap_subset_p (const_sbitmap, const_sbitmap);
  208. extern int bitmap_first_set_bit (const_sbitmap);
  209. extern int bitmap_last_set_bit (const_sbitmap);
  210. extern void debug_bitmap (const_sbitmap);
  211. extern sbitmap sbitmap_realloc (sbitmap, unsigned int);
  212. extern unsigned long sbitmap_popcount (const_sbitmap, unsigned long);
  213. #endif /* ! GCC_SBITMAP_H */