Home | History | Annotate | Download | only in include
      1 /* Simple bitmaps.
      2    Copyright (C) 1999, 2000, 2002, 2003, 2004, 2006, 2007, 2008
      3    Free Software Foundation, Inc.
      4 
      5 This file is part of GCC.
      6 
      7 GCC is free software; you can redistribute it and/or modify it under
      8 the terms of the GNU General Public License as published by the Free
      9 Software Foundation; either version 3, or (at your option) any later
     10 version.
     11 
     12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
     13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
     14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
     15 for more details.
     16 
     17 You should have received a copy of the GNU General Public License
     18 along with GCC; see the file COPYING3.  If not see
     19 <http://www.gnu.org/licenses/>.  */
     20 
     21 #ifndef GCC_SBITMAP_H
     22 #define GCC_SBITMAP_H
     23 
     24 /* It's not clear yet whether using bitmap.[ch] will be a win.
     25    It should be straightforward to convert so for now we keep things simple
     26    while more important issues are dealt with.  */
     27 
     28 #define SBITMAP_ELT_BITS ((unsigned) HOST_BITS_PER_WIDEST_FAST_INT)
     29 #define SBITMAP_ELT_TYPE unsigned HOST_WIDEST_FAST_INT
     30 
     31 /* Can't use SBITMAP_ELT_BITS in this macro because it contains a
     32    cast.  There is no perfect macro in GCC to test against.  This
     33    suffices for roughly 99% of the hosts we run on, and the rest
     34    don't have 256 bit integers.  */
     35 #if HOST_BITS_PER_WIDEST_FAST_INT > 255
     36 #error Need to increase size of datatype used for popcount
     37 #endif
     38 
     39 typedef struct simple_bitmap_def
     40 {
     41   unsigned char *popcount;      /* Population count.  */
     42   unsigned int n_bits;		/* Number of bits.  */
     43   unsigned int size;		/* Size in elements.  */
     44   SBITMAP_ELT_TYPE elms[1];	/* The elements.  */
     45 } *sbitmap;
     46 typedef const struct simple_bitmap_def *const_sbitmap;
     47 
     48 typedef SBITMAP_ELT_TYPE *sbitmap_ptr;
     49 typedef const SBITMAP_ELT_TYPE *const_sbitmap_ptr;
     50 
     51 /* Return the set size needed for N elements.  */
     52 #define SBITMAP_SET_SIZE(N) (((N) + SBITMAP_ELT_BITS - 1) / SBITMAP_ELT_BITS)
     53 #define SBITMAP_SIZE_BYTES(BITMAP) ((BITMAP)->size * sizeof (SBITMAP_ELT_TYPE))
     54 
     55 /* Test if bit number bitno in the bitmap is set.  */
     56 #define TEST_BIT(BITMAP, BITNO) \
     57 ((BITMAP)->elms [(BITNO) / SBITMAP_ELT_BITS] >> (BITNO) % SBITMAP_ELT_BITS & 1)
     58 
     59 /* Set bit number BITNO in the sbitmap MAP.  Updates population count
     60    if this bitmap has one.  */
     61 
     62 static inline void
     63 SET_BIT (sbitmap map, unsigned int bitno)
     64 {
     65   if (map->popcount)
     66     {
     67       bool oldbit;
     68       oldbit = TEST_BIT (map, bitno);
     69       if (!oldbit)
     70 	map->popcount[bitno / SBITMAP_ELT_BITS]++;
     71     }
     72   map->elms[bitno / SBITMAP_ELT_BITS]
     73     |= (SBITMAP_ELT_TYPE) 1 << (bitno) % SBITMAP_ELT_BITS;
     74 }
     75 
     76 
     77 
     78 /* Reset bit number BITNO in the sbitmap MAP.  Updates population
     79    count if this bitmap has one.  */
     80 
     81 static inline void
     82 RESET_BIT (sbitmap map,  unsigned int bitno)
     83 {
     84   if (map->popcount)
     85     {
     86       bool oldbit;
     87       oldbit = TEST_BIT (map, bitno);
     88       if (oldbit)
     89 	map->popcount[bitno / SBITMAP_ELT_BITS]--;
     90     }
     91   map->elms[bitno / SBITMAP_ELT_BITS]
     92     &= ~((SBITMAP_ELT_TYPE) 1 << (bitno) % SBITMAP_ELT_BITS);
     93 }
     94 
     95 /* The iterator for sbitmap.  */
     96 typedef struct {
     97   /* The pointer to the first word of the bitmap.  */
     98   const SBITMAP_ELT_TYPE *ptr;
     99 
    100   /* The size of the bitmap.  */
    101   unsigned int size;
    102 
    103   /* The current word index.  */
    104   unsigned int word_num;
    105 
    106   /* The current bit index (not modulo SBITMAP_ELT_BITS).  */
    107   unsigned int bit_num;
    108 
    109   /* The words currently visited.  */
    110   SBITMAP_ELT_TYPE word;
    111 } sbitmap_iterator;
    112 
    113 /* Initialize the iterator I with sbitmap BMP and the initial index
    114    MIN.  */
    115 
    116 static inline void
    117 sbitmap_iter_init (sbitmap_iterator *i, const_sbitmap bmp, unsigned int min)
    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 
    124   if (i->word_num >= i->size)
    125     i->word = 0;
    126   else
    127     i->word = (i->ptr[i->word_num]
    128 	       >> (i->bit_num % (unsigned int) SBITMAP_ELT_BITS));
    129 }
    130 
    131 /* Return true if we have more bits to visit, in which case *N is set
    132    to the index of the bit to be visited.  Otherwise, return
    133    false.  */
    134 
    135 static inline bool
    136 sbitmap_iter_cond (sbitmap_iterator *i, unsigned int *n)
    137 {
    138   /* Skip words that are zeros.  */
    139   for (; i->word == 0; i->word = i->ptr[i->word_num])
    140     {
    141       i->word_num++;
    142 
    143       /* If we have reached the end, break.  */
    144       if (i->word_num >= i->size)
    145 	return false;
    146 
    147       i->bit_num = i->word_num * SBITMAP_ELT_BITS;
    148     }
    149 
    150   /* Skip bits that are zero.  */
    151   for (; (i->word & 1) == 0; i->word >>= 1)
    152     i->bit_num++;
    153 
    154   *n = i->bit_num;
    155 
    156   return true;
    157 }
    158 
    159 /* Advance to the next bit.  */
    160 
    161 static inline void
    162 sbitmap_iter_next (sbitmap_iterator *i)
    163 {
    164   i->word >>= 1;
    165   i->bit_num++;
    166 }
    167 
    168 /* Loop over all elements of SBITMAP, starting with MIN.  In each
    169    iteration, N is set to the index of the bit being visited.  ITER is
    170    an instance of sbitmap_iterator used to iterate the bitmap.  */
    171 
    172 #define EXECUTE_IF_SET_IN_SBITMAP(SBITMAP, MIN, N, ITER)	\
    173   for (sbitmap_iter_init (&(ITER), (SBITMAP), (MIN));		\
    174        sbitmap_iter_cond (&(ITER), &(N));			\
    175        sbitmap_iter_next (&(ITER)))
    176 
    177 #define EXECUTE_IF_SET_IN_SBITMAP_REV(SBITMAP, N, CODE)			\
    178 do {									\
    179   unsigned int word_num_;						\
    180   unsigned int bit_num_;						\
    181   unsigned int size_ = (SBITMAP)->size;					\
    182   SBITMAP_ELT_TYPE *ptr_ = (SBITMAP)->elms;				\
    183 									\
    184   for (word_num_ = size_; word_num_ > 0; word_num_--)			\
    185     {									\
    186       SBITMAP_ELT_TYPE word_ = ptr_[word_num_ - 1];			\
    187 									\
    188       if (word_ != 0)							\
    189 	for (bit_num_ = SBITMAP_ELT_BITS; bit_num_ > 0; bit_num_--)	\
    190 	  {								\
    191 	    SBITMAP_ELT_TYPE _mask = (SBITMAP_ELT_TYPE)1 << (bit_num_ - 1);\
    192 									\
    193 	    if ((word_ & _mask) != 0)					\
    194 	      {								\
    195 		word_ &= ~ _mask;					\
    196 		(N) = (word_num_ - 1) * SBITMAP_ELT_BITS + bit_num_ - 1;\
    197 		CODE;							\
    198 		if (word_ == 0)						\
    199 		  break;						\
    200 	      }								\
    201 	  }								\
    202     }									\
    203 } while (0)
    204 
    205 #define sbitmap_free(MAP)		(free((MAP)->popcount), free((MAP)))
    206 #define sbitmap_vector_free(VEC)	free(VEC)
    207 
    208 struct int_list;
    209 
    210 extern void dump_sbitmap (FILE *, const_sbitmap);
    211 extern void dump_sbitmap_file (FILE *, const_sbitmap);
    212 extern void dump_sbitmap_vector (FILE *, const char *, const char *, sbitmap *,
    213 				 int);
    214 extern sbitmap sbitmap_alloc (unsigned int);
    215 extern sbitmap sbitmap_alloc_with_popcount (unsigned int);
    216 extern sbitmap *sbitmap_vector_alloc (unsigned int, unsigned int);
    217 extern sbitmap sbitmap_resize (sbitmap, unsigned int, int);
    218 extern void sbitmap_copy (sbitmap, const_sbitmap);
    219 extern void sbitmap_copy_n (sbitmap, const_sbitmap, unsigned int);
    220 extern int sbitmap_equal (const_sbitmap, const_sbitmap);
    221 extern bool sbitmap_empty_p (const_sbitmap);
    222 extern bool sbitmap_range_empty_p (const_sbitmap, unsigned int, unsigned int);
    223 extern void sbitmap_zero (sbitmap);
    224 extern void sbitmap_ones (sbitmap);
    225 extern void sbitmap_vector_zero (sbitmap *, unsigned int);
    226 extern void sbitmap_vector_ones (sbitmap *, unsigned int);
    227 
    228 extern void sbitmap_union_of_diff (sbitmap, const_sbitmap, const_sbitmap, const_sbitmap);
    229 extern bool sbitmap_union_of_diff_cg (sbitmap, const_sbitmap, const_sbitmap, const_sbitmap);
    230 extern void sbitmap_difference (sbitmap, const_sbitmap, const_sbitmap);
    231 extern void sbitmap_not (sbitmap, const_sbitmap);
    232 extern void sbitmap_a_or_b_and_c (sbitmap, const_sbitmap, const_sbitmap, const_sbitmap);
    233 extern bool sbitmap_a_or_b_and_c_cg (sbitmap, const_sbitmap, const_sbitmap, const_sbitmap);
    234 extern bool sbitmap_a_or_b_and_c_compl_cg (sbitmap, const_sbitmap, const_sbitmap, const_sbitmap);
    235 extern void sbitmap_a_and_b_or_c (sbitmap, const_sbitmap, const_sbitmap, const_sbitmap);
    236 extern bool sbitmap_a_and_b_or_c_cg (sbitmap, const_sbitmap, const_sbitmap, const_sbitmap);
    237 extern bool sbitmap_any_common_bits (const_sbitmap, const_sbitmap);
    238 extern void sbitmap_a_and_b (sbitmap, const_sbitmap, const_sbitmap);
    239 extern bool sbitmap_a_and_b_cg (sbitmap, const_sbitmap, const_sbitmap);
    240 extern void sbitmap_a_and_not_b (sbitmap, const_sbitmap, const_sbitmap);
    241 extern void sbitmap_a_or_b (sbitmap, const_sbitmap, const_sbitmap);
    242 extern bool sbitmap_a_or_b_cg (sbitmap, const_sbitmap, const_sbitmap);
    243 extern void sbitmap_a_xor_b (sbitmap, const_sbitmap, const_sbitmap);
    244 extern bool sbitmap_a_xor_b_cg (sbitmap, const_sbitmap, const_sbitmap);
    245 extern bool sbitmap_a_subset_b_p (const_sbitmap, const_sbitmap);
    246 
    247 extern int sbitmap_first_set_bit (const_sbitmap);
    248 extern int sbitmap_last_set_bit (const_sbitmap);
    249 
    250 extern void sbitmap_intersect_of_predsucc (sbitmap, sbitmap *, int,
    251 					   struct int_list **);
    252 #define sbitmap_intersect_of_predecessors  sbitmap_intersect_of_predsucc
    253 #define sbitmap_intersect_of_successors    sbitmap_intersect_of_predsucc
    254 
    255 extern void sbitmap_union_of_predsucc (sbitmap, sbitmap *, int,
    256 				       struct int_list **);
    257 #define sbitmap_union_of_predecessors  sbitmap_union_of_predsucc
    258 #define sbitmap_union_of_successors    sbitmap_union_of_predsucc
    259 
    260 /* Intersection and Union of preds/succs using the new flow graph
    261    structure instead of the pred/succ arrays.  */
    262 
    263 extern void sbitmap_intersection_of_succs (sbitmap, sbitmap *, int);
    264 extern void sbitmap_intersection_of_preds (sbitmap, sbitmap *, int);
    265 extern void sbitmap_union_of_succs (sbitmap, sbitmap *, int);
    266 extern void sbitmap_union_of_preds (sbitmap, sbitmap *, int);
    267 
    268 extern void debug_sbitmap (const_sbitmap);
    269 extern sbitmap sbitmap_realloc (sbitmap, unsigned int);
    270 extern unsigned long sbitmap_popcount(const_sbitmap, unsigned long);
    271 extern void sbitmap_verify_popcount (const_sbitmap);
    272 #endif /* ! GCC_SBITMAP_H */
    273