OSDN Git Service

2007-07-08 Tobias Burnus <burnus@net-b.de>
[pf3gnuchains/gcc-fork.git] / gcc / sbitmap.h
1 /* Simple bitmaps.
2    Copyright (C) 1999, 2000, 2002, 2003, 2004 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
9 version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING.  If not, write to the Free
18 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301, USA.  */
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
47 typedef SBITMAP_ELT_TYPE *sbitmap_ptr;
48
49 /* Return the set size needed for N elements.  */
50 #define SBITMAP_SET_SIZE(N) (((N) + SBITMAP_ELT_BITS - 1) / SBITMAP_ELT_BITS)
51 #define SBITMAP_SIZE_BYTES(BITMAP) ((BITMAP)->size * sizeof (SBITMAP_ELT_TYPE))
52
53 /* Test if bit number bitno in the bitmap is set.  */
54 #define TEST_BIT(BITMAP, BITNO) \
55 ((BITMAP)->elms [(BITNO) / SBITMAP_ELT_BITS] >> (BITNO) % SBITMAP_ELT_BITS & 1)
56
57 /* Set bit number BITNO in the sbitmap MAP.  Updates population count
58    if this bitmap has one.  */
59
60 static inline void
61 SET_BIT (sbitmap map, unsigned int bitno)
62 {
63   if (map->popcount)
64     {
65       bool oldbit;
66       oldbit = TEST_BIT (map, bitno);
67       if (!oldbit)
68         map->popcount[bitno / SBITMAP_ELT_BITS]++;
69     }
70   map->elms[bitno / SBITMAP_ELT_BITS]
71     |= (SBITMAP_ELT_TYPE) 1 << (bitno) % SBITMAP_ELT_BITS;
72 }
73
74
75
76 /* Reset bit number BITNO in the sbitmap MAP.  Updates population
77    count if this bitmap has one.  */
78
79 static inline void
80 RESET_BIT (sbitmap map,  unsigned int bitno)
81 {
82   if (map->popcount)
83     {
84       bool oldbit;
85       oldbit = TEST_BIT (map, bitno);
86       if (oldbit)
87         map->popcount[bitno / SBITMAP_ELT_BITS]--;
88     }
89   map->elms[bitno / SBITMAP_ELT_BITS]
90     &= ~((SBITMAP_ELT_TYPE) 1 << (bitno) % SBITMAP_ELT_BITS);
91 }
92
93 /* The iterator for sbitmap.  */
94 typedef struct {
95   /* The pointer to the first word of the bitmap.  */
96   SBITMAP_ELT_TYPE *ptr;
97
98   /* The size of the bitmap.  */
99   unsigned int size;
100
101   /* The current word index.  */
102   unsigned int word_num;
103
104   /* The current bit index (not modulo SBITMAP_ELT_BITS).  */
105   unsigned int bit_num;
106
107   /* The words currently visited.  */
108   SBITMAP_ELT_TYPE word;
109 } sbitmap_iterator;
110
111 /* Initialize the iterator I with sbitmap BMP and the initial index
112    MIN.  */
113
114 static inline void
115 sbitmap_iter_init (sbitmap_iterator *i, sbitmap bmp, unsigned int min)
116 {
117   i->word_num = min / (unsigned int) SBITMAP_ELT_BITS;
118   i->bit_num = min;
119   i->size = bmp->size;
120   i->ptr = bmp->elms;
121
122   if (i->word_num >= i->size)
123     i->word = 0;
124   else
125     i->word = (i->ptr[i->word_num]
126                >> (i->bit_num % (unsigned int) SBITMAP_ELT_BITS));
127 }
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
133 static inline bool
134 sbitmap_iter_cond (sbitmap_iterator *i, unsigned int *n)
135 {
136   /* Skip words that are zeros.  */
137   for (; i->word == 0; i->word = i->ptr[i->word_num])
138     {
139       i->word_num++;
140
141       /* If we have reached the end, break.  */
142       if (i->word_num >= i->size)
143         return false;
144
145       i->bit_num = i->word_num * SBITMAP_ELT_BITS;
146     }
147
148   /* Skip bits that are zero.  */
149   for (; (i->word & 1) == 0; i->word >>= 1)
150     i->bit_num++;
151
152   *n = i->bit_num;
153
154   return true;
155 }
156
157 /* Advance to the next bit.  */
158
159 static inline void
160 sbitmap_iter_next (sbitmap_iterator *i)
161 {
162   i->word >>= 1;
163   i->bit_num++;
164 }
165
166 /* Loop over all elements of SBITMAP, starting with MIN.  In each
167    iteration, N is set to the index of the bit being visited.  ITER is
168    an instance of sbitmap_iterator used to iterate the bitmap.  */
169
170 #define EXECUTE_IF_SET_IN_SBITMAP(SBITMAP, MIN, N, ITER)        \
171   for (sbitmap_iter_init (&(ITER), (SBITMAP), (MIN));           \
172        sbitmap_iter_cond (&(ITER), &(N));                       \
173        sbitmap_iter_next (&(ITER)))
174
175 #define EXECUTE_IF_SET_IN_SBITMAP_REV(SBITMAP, N, CODE)                 \
176 do {                                                                    \
177   unsigned int word_num_;                                               \
178   unsigned int bit_num_;                                                \
179   unsigned int size_ = (SBITMAP)->size;                                 \
180   SBITMAP_ELT_TYPE *ptr_ = (SBITMAP)->elms;                             \
181                                                                         \
182   for (word_num_ = size_; word_num_ > 0; word_num_--)                   \
183     {                                                                   \
184       SBITMAP_ELT_TYPE word_ = ptr_[word_num_ - 1];                     \
185                                                                         \
186       if (word_ != 0)                                                   \
187         for (bit_num_ = SBITMAP_ELT_BITS; bit_num_ > 0; bit_num_--)     \
188           {                                                             \
189             SBITMAP_ELT_TYPE _mask = (SBITMAP_ELT_TYPE)1 << (bit_num_ - 1);\
190                                                                         \
191             if ((word_ & _mask) != 0)                                   \
192               {                                                         \
193                 word_ &= ~ _mask;                                       \
194                 (N) = (word_num_ - 1) * SBITMAP_ELT_BITS + bit_num_ - 1;\
195                 CODE;                                                   \
196                 if (word_ == 0)                                         \
197                   break;                                                \
198               }                                                         \
199           }                                                             \
200     }                                                                   \
201 } while (0)
202
203 #define sbitmap_free(MAP)               (free((MAP)->popcount), free((MAP)))
204 #define sbitmap_vector_free(VEC)        free(VEC)
205
206 struct int_list;
207
208 extern void dump_sbitmap (FILE *, sbitmap);
209 extern void dump_sbitmap_file (FILE *, sbitmap);
210 extern void dump_sbitmap_vector (FILE *, const char *, const char *, sbitmap *,
211                                  int);
212 extern sbitmap sbitmap_alloc (unsigned int);
213 extern sbitmap sbitmap_alloc_with_popcount (unsigned int);
214 extern sbitmap *sbitmap_vector_alloc (unsigned int, unsigned int);
215 extern sbitmap sbitmap_resize (sbitmap, unsigned int, int);
216 extern void sbitmap_copy (sbitmap, sbitmap);
217 extern void sbitmap_copy_n (sbitmap, sbitmap, unsigned int);
218 extern int sbitmap_equal (sbitmap, sbitmap);
219 extern void sbitmap_zero (sbitmap);
220 extern void sbitmap_ones (sbitmap);
221 extern void sbitmap_vector_zero (sbitmap *, unsigned int);
222 extern void sbitmap_vector_ones (sbitmap *, unsigned int);
223
224 extern void sbitmap_union_of_diff (sbitmap, sbitmap, sbitmap, sbitmap);
225 extern bool sbitmap_union_of_diff_cg (sbitmap, sbitmap, sbitmap, sbitmap);
226 extern void sbitmap_difference (sbitmap, sbitmap, sbitmap);
227 extern void sbitmap_not (sbitmap, sbitmap);
228 extern void sbitmap_a_or_b_and_c (sbitmap, sbitmap, sbitmap, sbitmap);
229 extern bool sbitmap_a_or_b_and_c_cg (sbitmap, sbitmap, sbitmap, sbitmap);
230 extern void sbitmap_a_and_b_or_c (sbitmap, sbitmap, sbitmap, sbitmap);
231 extern bool sbitmap_a_and_b_or_c_cg (sbitmap, sbitmap, sbitmap, sbitmap);
232 extern bool sbitmap_any_common_bits (sbitmap, sbitmap);
233 extern void sbitmap_a_and_b (sbitmap, sbitmap, sbitmap);
234 extern bool sbitmap_a_and_b_cg (sbitmap, sbitmap, sbitmap);
235 extern void sbitmap_a_or_b (sbitmap, sbitmap, sbitmap);
236 extern bool sbitmap_a_or_b_cg (sbitmap, sbitmap, sbitmap);
237 extern void sbitmap_a_xor_b (sbitmap, sbitmap, sbitmap);
238 extern bool sbitmap_a_xor_b_cg (sbitmap, sbitmap, sbitmap);
239 extern bool sbitmap_a_subset_b_p (sbitmap, sbitmap);
240
241 extern int sbitmap_first_set_bit (sbitmap);
242 extern int sbitmap_last_set_bit (sbitmap);
243
244 extern void sbitmap_intersect_of_predsucc (sbitmap, sbitmap *, int,
245                                            struct int_list **);
246 #define sbitmap_intersect_of_predecessors  sbitmap_intersect_of_predsucc
247 #define sbitmap_intersect_of_successors    sbitmap_intersect_of_predsucc
248
249 extern void sbitmap_union_of_predsucc (sbitmap, sbitmap *, int,
250                                        struct int_list **);
251 #define sbitmap_union_of_predecessors  sbitmap_union_of_predsucc
252 #define sbitmap_union_of_successors    sbitmap_union_of_predsucc
253
254 /* Intersection and Union of preds/succs using the new flow graph
255    structure instead of the pred/succ arrays.  */
256
257 extern void sbitmap_intersection_of_succs (sbitmap, sbitmap *, int);
258 extern void sbitmap_intersection_of_preds (sbitmap, sbitmap *, int);
259 extern void sbitmap_union_of_succs (sbitmap, sbitmap *, int);
260 extern void sbitmap_union_of_preds (sbitmap, sbitmap *, int);
261
262 extern void debug_sbitmap (sbitmap);
263 extern sbitmap sbitmap_realloc (sbitmap, unsigned int);
264 extern unsigned long sbitmap_popcount(sbitmap, unsigned long);
265 extern void sbitmap_verify_popcount (sbitmap);
266 #endif /* ! GCC_SBITMAP_H */