Merge from mainline
[official-gcc.git] / gcc / bitmap.h
blobed402990f11d94f2da17fdcc2e17c28ca91ec8da
1 /* Functions to support general ended bitmaps.
2 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002
3 Free Software Foundation, Inc.
5 This file is part of GCC.
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 2, or (at your option) any later
10 version.
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.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 #ifndef GCC_BITMAP_H
23 #define GCC_BITMAP_H
25 /* Number of words to use for each element in the linked list. */
27 #ifndef BITMAP_ELEMENT_WORDS
28 #define BITMAP_ELEMENT_WORDS 2
29 #endif
31 /* Number of bits in each actual element of a bitmap. We get slightly better
32 code for bit % BITMAP_ELEMENT_ALL_BITS and bit / BITMAP_ELEMENT_ALL_BITS if
33 bits is unsigned, assuming it is a power of 2. */
35 #define BITMAP_ELEMENT_ALL_BITS \
36 ((unsigned) (BITMAP_ELEMENT_WORDS * HOST_BITS_PER_WIDE_INT))
38 /* Bitmap set element. We use a linked list to hold only the bits that
39 are set. This allows for use to grow the bitset dynamically without
40 having to realloc and copy a giant bit array. The `prev' field is
41 undefined for an element on the free list. */
43 typedef struct bitmap_element_def GTY(())
45 struct bitmap_element_def *next; /* Next element. */
46 struct bitmap_element_def *prev; /* Previous element. */
47 unsigned int indx; /* regno/BITMAP_ELEMENT_ALL_BITS. */
48 unsigned HOST_WIDE_INT bits[BITMAP_ELEMENT_WORDS]; /* Bits that are set. */
49 } bitmap_element;
51 /* Head of bitmap linked list. */
52 typedef struct bitmap_head_def GTY(()) {
53 bitmap_element *first; /* First element in linked list. */
54 bitmap_element *current; /* Last element looked at. */
55 unsigned int indx; /* Index of last element looked at. */
56 int using_obstack; /* Are we using an obstack or ggc for
57 allocation? */
58 } bitmap_head;
59 typedef struct bitmap_head_def *bitmap;
61 /* Enumeration giving the various operations we support. */
62 enum bitmap_bits {
63 BITMAP_AND, /* TO = FROM1 & FROM2 */
64 BITMAP_AND_COMPL, /* TO = FROM1 & ~ FROM2 */
65 BITMAP_IOR, /* TO = FROM1 | FROM2 */
66 BITMAP_XOR, /* TO = FROM1 ^ FROM2 */
67 BITMAP_IOR_COMPL /* TO = FROM1 | ~FROM2 */
70 /* Global data */
71 extern bitmap_element bitmap_zero_bits; /* Zero bitmap element */
73 /* Clear a bitmap by freeing up the linked list. */
74 extern void bitmap_clear PARAMS ((bitmap));
76 /* Copy a bitmap to another bitmap. */
77 extern void bitmap_copy PARAMS ((bitmap, bitmap));
79 /* True if two bitmaps are identical. */
80 extern int bitmap_equal_p PARAMS ((bitmap, bitmap));
82 /* Perform an operation on two bitmaps, yielding a third. */
83 extern int bitmap_operation PARAMS ((bitmap, bitmap, bitmap, enum bitmap_bits));
85 /* `or' into one bitmap the `and' of a second bitmap witih the complement
86 of a third. */
87 extern void bitmap_ior_and_compl PARAMS ((bitmap, bitmap, bitmap));
89 /* Clear a single register in a register set. */
90 extern void bitmap_clear_bit PARAMS ((bitmap, int));
92 /* Set a single register in a register set. */
93 extern void bitmap_set_bit PARAMS ((bitmap, int));
95 /* Return true if a register is set in a register set. */
96 extern int bitmap_bit_p PARAMS ((bitmap, int));
98 /* Debug functions to print a bitmap linked list. */
99 extern void debug_bitmap PARAMS ((bitmap));
100 extern void debug_bitmap_file PARAMS ((FILE *, bitmap));
102 /* Print a bitmap */
103 extern void bitmap_print PARAMS ((FILE *, bitmap, const char *, const char *));
105 /* Initialize a bitmap header. If HEAD is NULL, a new header will be
106 allocated. USING_OBSTACK indicates how elements should be allocated. */
107 extern bitmap bitmap_initialize PARAMS ((bitmap head,
108 int using_obstack));
110 /* Release all memory used by the bitmap obstack. */
111 extern void bitmap_release_memory PARAMS ((void));
113 /* A few compatibility/functions macros for compatibility with sbitmaps */
114 #define dump_bitmap(file, bitmap) bitmap_print (file, bitmap, "", "\n")
115 #define bitmap_zero(a) bitmap_clear (a)
116 #define bitmap_a_or_b(a,b,c) bitmap_operation (a, b, c, BITMAP_IOR)
117 #define bitmap_a_and_b(a,b,c) bitmap_operation (a, b, c, BITMAP_AND)
118 extern int bitmap_union_of_diff PARAMS((bitmap, bitmap, bitmap, bitmap));
119 extern int bitmap_first_set_bit PARAMS((bitmap));
120 extern int bitmap_last_set_bit PARAMS((bitmap));
122 /* Allocate a bitmap with oballoc. */
123 #define BITMAP_OBSTACK_ALLOC(OBSTACK) \
124 bitmap_initialize ((bitmap) obstack_alloc (OBSTACK, sizeof (bitmap_head)), 1)
126 /* Allocate a bitmap with ggc_alloc. */
127 #define BITMAP_GGC_ALLOC() \
128 bitmap_initialize (NULL, 0)
130 /* Allocate a bitmap with xmalloc. */
131 #define BITMAP_XMALLOC() \
132 bitmap_initialize ((bitmap) xmalloc (sizeof (bitmap_head)), 1)
134 /* Do any cleanup needed on a bitmap when it is no longer used. */
135 #define BITMAP_FREE(BITMAP) \
136 do { \
137 if (BITMAP) \
139 bitmap_clear (BITMAP); \
140 (BITMAP) = 0; \
142 } while (0)
144 /* Do any cleanup needed on an xmalloced bitmap when it is no longer used. */
145 #define BITMAP_XFREE(BITMAP) \
146 do { \
147 if (BITMAP) \
149 bitmap_clear (BITMAP); \
150 free (BITMAP); \
151 (BITMAP) = 0; \
153 } while (0)
155 /* Do any one-time initializations needed for bitmaps. */
156 #define BITMAP_INIT_ONCE()
158 /* Loop over all bits in BITMAP, starting with MIN, setting BITNUM to the
159 bit number and executing CODE for all bits that are set. */
161 #define EXECUTE_IF_SET_IN_BITMAP(BITMAP, MIN, BITNUM, CODE) \
162 do { \
163 bitmap_element *ptr_ = (BITMAP)->first; \
164 unsigned int indx_ = (MIN) / BITMAP_ELEMENT_ALL_BITS; \
165 unsigned bit_num_ = (MIN) % ((unsigned) HOST_BITS_PER_WIDE_INT); \
166 unsigned word_num_ = (((MIN) / ((unsigned) HOST_BITS_PER_WIDE_INT)) \
167 % BITMAP_ELEMENT_WORDS); \
170 /* Find the block the minimum bit is in. */ \
171 while (ptr_ != 0 && ptr_->indx < indx_) \
172 ptr_ = ptr_->next; \
174 if (ptr_ != 0 && ptr_->indx != indx_) \
176 bit_num_ = 0; \
177 word_num_ = 0; \
180 for (; ptr_ != 0; ptr_ = ptr_->next) \
182 for (; word_num_ < BITMAP_ELEMENT_WORDS; word_num_++) \
184 unsigned HOST_WIDE_INT word_ = ptr_->bits[word_num_]; \
186 if (word_ != 0) \
188 for (; bit_num_ < HOST_BITS_PER_WIDE_INT; bit_num_++) \
190 unsigned HOST_WIDE_INT mask_ \
191 = ((unsigned HOST_WIDE_INT) 1) << bit_num_; \
193 if ((word_ & mask_) != 0) \
195 word_ &= ~ mask_; \
196 (BITNUM) = (ptr_->indx * BITMAP_ELEMENT_ALL_BITS \
197 + word_num_ * HOST_BITS_PER_WIDE_INT \
198 + bit_num_); \
199 CODE; \
201 if (word_ == 0) \
202 break; \
207 bit_num_ = 0; \
210 word_num_ = 0; \
212 } while (0)
214 /* Loop over all bits in BITMAP1 and BITMAP2, starting with MIN, setting
215 BITNUM to the bit number and executing CODE for all bits that are set in
216 the first bitmap and not set in the second. */
218 #define EXECUTE_IF_AND_COMPL_IN_BITMAP(BITMAP1, BITMAP2, MIN, BITNUM, CODE) \
219 do { \
220 bitmap_element *ptr1_ = (BITMAP1)->first; \
221 bitmap_element *ptr2_ = (BITMAP2)->first; \
222 unsigned int indx_ = (MIN) / BITMAP_ELEMENT_ALL_BITS; \
223 unsigned bit_num_ = (MIN) % ((unsigned) HOST_BITS_PER_WIDE_INT); \
224 unsigned word_num_ = (((MIN) / ((unsigned) HOST_BITS_PER_WIDE_INT)) \
225 % BITMAP_ELEMENT_WORDS); \
227 /* Find the block the minimum bit is in in the first bitmap. */ \
228 while (ptr1_ != 0 && ptr1_->indx < indx_) \
229 ptr1_ = ptr1_->next; \
231 if (ptr1_ != 0 && ptr1_->indx != indx_) \
233 bit_num_ = 0; \
234 word_num_ = 0; \
237 for (; ptr1_ != 0 ; ptr1_ = ptr1_->next) \
239 /* Advance BITMAP2 to the equivalent link, using an all \
240 zero element if an equivalent link doesn't exist. */ \
241 bitmap_element *tmp2_; \
243 while (ptr2_ != 0 && ptr2_->indx < ptr1_->indx) \
244 ptr2_ = ptr2_->next; \
246 tmp2_ = ((ptr2_ != 0 && ptr2_->indx == ptr1_->indx) \
247 ? ptr2_ : &bitmap_zero_bits); \
249 for (; word_num_ < BITMAP_ELEMENT_WORDS; word_num_++) \
251 unsigned HOST_WIDE_INT word_ = (ptr1_->bits[word_num_] \
252 & ~ tmp2_->bits[word_num_]); \
253 if (word_ != 0) \
255 for (; bit_num_ < HOST_BITS_PER_WIDE_INT; bit_num_++) \
257 unsigned HOST_WIDE_INT mask_ \
258 = ((unsigned HOST_WIDE_INT)1) << bit_num_; \
260 if ((word_ & mask_) != 0) \
262 word_ &= ~ mask_; \
263 (BITNUM) = (ptr1_->indx * BITMAP_ELEMENT_ALL_BITS \
264 + word_num_ * HOST_BITS_PER_WIDE_INT \
265 + bit_num_); \
267 CODE; \
268 if (word_ == 0) \
269 break; \
274 bit_num_ = 0; \
277 word_num_ = 0; \
279 } while (0)
281 /* Loop over all bits in BITMAP1 and BITMAP2, starting with MIN, setting
282 BITNUM to the bit number and executing CODE for all bits that are set in
283 the both bitmaps. */
285 #define EXECUTE_IF_AND_IN_BITMAP(BITMAP1, BITMAP2, MIN, BITNUM, CODE) \
286 do { \
287 bitmap_element *ptr1_ = (BITMAP1)->first; \
288 bitmap_element *ptr2_ = (BITMAP2)->first; \
289 unsigned int indx_ = (MIN) / BITMAP_ELEMENT_ALL_BITS; \
290 unsigned bit_num_ = (MIN) % ((unsigned) HOST_BITS_PER_WIDE_INT); \
291 unsigned word_num_ = (((MIN) / ((unsigned) HOST_BITS_PER_WIDE_INT)) \
292 % BITMAP_ELEMENT_WORDS); \
294 /* Find the block the minimum bit is in in the first bitmap. */ \
295 while (ptr1_ != 0 && ptr1_->indx < indx_) \
296 ptr1_ = ptr1_->next; \
298 if (ptr1_ != 0 && ptr1_->indx != indx_) \
300 bit_num_ = 0; \
301 word_num_ = 0; \
304 for (; ptr1_ != 0 ; ptr1_ = ptr1_->next) \
306 /* Advance BITMAP2 to the equivalent link */ \
307 while (ptr2_ != 0 && ptr2_->indx < ptr1_->indx) \
308 ptr2_ = ptr2_->next; \
310 if (ptr2_ == 0) \
312 /* If there are no more elements in BITMAP2, exit loop now. */ \
313 ptr1_ = (bitmap_element *)0; \
314 break; \
316 else if (ptr2_->indx > ptr1_->indx) \
318 bit_num_ = word_num_ = 0; \
319 continue; \
322 for (; word_num_ < BITMAP_ELEMENT_WORDS; word_num_++) \
324 unsigned HOST_WIDE_INT word_ = (ptr1_->bits[word_num_] \
325 & ptr2_->bits[word_num_]); \
326 if (word_ != 0) \
328 for (; bit_num_ < HOST_BITS_PER_WIDE_INT; bit_num_++) \
330 unsigned HOST_WIDE_INT mask_ \
331 = ((unsigned HOST_WIDE_INT)1) << bit_num_; \
333 if ((word_ & mask_) != 0) \
335 word_ &= ~ mask_; \
336 (BITNUM) = (ptr1_->indx * BITMAP_ELEMENT_ALL_BITS \
337 + word_num_ * HOST_BITS_PER_WIDE_INT \
338 + bit_num_); \
340 CODE; \
341 if (word_ == 0) \
342 break; \
347 bit_num_ = 0; \
350 word_num_ = 0; \
352 } while (0)
354 #endif /* GCC_BITMAP_H */