EnumSet*.class: Regenerate
[official-gcc.git] / gcc / bitmap.h
blob66fed855afee26ed1a7a386d363e458786e031c4
1 /* Functions to support general ended bitmaps.
2 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2006, 2007 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 3, 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 COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #ifndef GCC_BITMAP_H
22 #define GCC_BITMAP_H
23 #include "hashtab.h"
24 #include "statistics.h"
26 /* Fundamental storage type for bitmap. */
28 typedef unsigned long BITMAP_WORD;
29 /* BITMAP_WORD_BITS needs to be unsigned, but cannot contain casts as
30 it is used in preprocessor directives -- hence the 1u. */
31 #define BITMAP_WORD_BITS (CHAR_BIT * SIZEOF_LONG * 1u)
33 /* Number of words to use for each element in the linked list. */
35 #ifndef BITMAP_ELEMENT_WORDS
36 #define BITMAP_ELEMENT_WORDS ((128 + BITMAP_WORD_BITS - 1) / BITMAP_WORD_BITS)
37 #endif
39 /* Number of bits in each actual element of a bitmap. */
41 #define BITMAP_ELEMENT_ALL_BITS (BITMAP_ELEMENT_WORDS * BITMAP_WORD_BITS)
43 /* Obstack for allocating bitmaps and elements from. */
44 typedef struct bitmap_obstack GTY (())
46 struct bitmap_element_def *elements;
47 struct bitmap_head_def *heads;
48 struct obstack GTY ((skip)) obstack;
49 } bitmap_obstack;
51 /* Bitmap set element. We use a linked list to hold only the bits that
52 are set. This allows for use to grow the bitset dynamically without
53 having to realloc and copy a giant bit array.
55 The free list is implemented as a list of lists. There is one
56 outer list connected together by prev fields. Each element of that
57 outer is an inner list (that may consist only of the outer list
58 element) that are connected by the next fields. The prev pointer
59 is undefined for interior elements. This allows
60 bitmap_elt_clear_from to be implemented in unit time rather than
61 linear in the number of elements to be freed. */
63 typedef struct bitmap_element_def GTY(())
65 struct bitmap_element_def *next; /* Next element. */
66 struct bitmap_element_def *prev; /* Previous element. */
67 unsigned int indx; /* regno/BITMAP_ELEMENT_ALL_BITS. */
68 BITMAP_WORD bits[BITMAP_ELEMENT_WORDS]; /* Bits that are set. */
69 } bitmap_element;
71 struct bitmap_descriptor;
72 /* Head of bitmap linked list. gengtype ignores ifdefs, but for
73 statistics we need to add a bitmap descriptor pointer. As it is
74 not collected, we can just GTY((skip)) it. */
76 typedef struct bitmap_head_def GTY(()) {
77 bitmap_element *first; /* First element in linked list. */
78 bitmap_element *current; /* Last element looked at. */
79 unsigned int indx; /* Index of last element looked at. */
80 bitmap_obstack *obstack; /* Obstack to allocate elements from.
81 If NULL, then use ggc_alloc. */
82 #ifdef GATHER_STATISTICS
83 struct bitmap_descriptor GTY((skip)) *desc;
84 #endif
85 } bitmap_head;
87 /* Global data */
88 extern bitmap_element bitmap_zero_bits; /* Zero bitmap element */
89 extern bitmap_obstack bitmap_default_obstack; /* Default bitmap obstack */
91 /* Clear a bitmap by freeing up the linked list. */
92 extern void bitmap_clear (bitmap);
94 /* Copy a bitmap to another bitmap. */
95 extern void bitmap_copy (bitmap, const_bitmap);
97 /* True if two bitmaps are identical. */
98 extern bool bitmap_equal_p (const_bitmap, const_bitmap);
100 /* True if the bitmaps intersect (their AND is non-empty). */
101 extern bool bitmap_intersect_p (const_bitmap, const_bitmap);
103 /* True if the complement of the second intersects the first (their
104 AND_COMPL is non-empty). */
105 extern bool bitmap_intersect_compl_p (const_bitmap, const_bitmap);
107 /* True if MAP is an empty bitmap. */
108 #define bitmap_empty_p(MAP) (!(MAP)->first)
110 /* Count the number of bits set in the bitmap. */
111 extern unsigned long bitmap_count_bits (const_bitmap);
113 /* Boolean operations on bitmaps. The _into variants are two operand
114 versions that modify the first source operand. The other variants
115 are three operand versions that to not destroy the source bitmaps.
116 The operations supported are &, & ~, |, ^. */
117 extern void bitmap_and (bitmap, const_bitmap, const_bitmap);
118 extern void bitmap_and_into (bitmap, const_bitmap);
119 extern bool bitmap_and_compl (bitmap, const_bitmap, const_bitmap);
120 extern bool bitmap_and_compl_into (bitmap, const_bitmap);
121 #define bitmap_compl_and(DST, A, B) bitmap_and_compl (DST, B, A)
122 extern void bitmap_compl_and_into (bitmap, const_bitmap);
123 extern void bitmap_clear_range (bitmap, unsigned int, unsigned int);
124 extern void bitmap_set_range (bitmap, unsigned int, unsigned int);
125 extern bool bitmap_ior (bitmap, const_bitmap, const_bitmap);
126 extern bool bitmap_ior_into (bitmap, const_bitmap);
127 extern void bitmap_xor (bitmap, const_bitmap, const_bitmap);
128 extern void bitmap_xor_into (bitmap, const_bitmap);
130 /* DST = A | (B & ~C). Return true if DST changes. */
131 extern bool bitmap_ior_and_compl (bitmap DST, const_bitmap A, const_bitmap B, const_bitmap C);
132 /* A |= (B & ~C). Return true if A changes. */
133 extern bool bitmap_ior_and_compl_into (bitmap DST, const_bitmap B, const_bitmap C);
135 /* Clear a single register in a register set. */
136 extern void bitmap_clear_bit (bitmap, int);
138 /* Set a single register in a register set. */
139 extern void bitmap_set_bit (bitmap, int);
141 /* Return true if a register is set in a register set. */
142 extern int bitmap_bit_p (bitmap, int);
144 /* Debug functions to print a bitmap linked list. */
145 extern void debug_bitmap (const_bitmap);
146 extern void debug_bitmap_file (FILE *, const_bitmap);
148 /* Print a bitmap. */
149 extern void bitmap_print (FILE *, const_bitmap, const char *, const char *);
151 /* Initialize and release a bitmap obstack. */
152 extern void bitmap_obstack_initialize (bitmap_obstack *);
153 extern void bitmap_obstack_release (bitmap_obstack *);
154 extern void bitmap_register (bitmap MEM_STAT_DECL);
155 extern void dump_bitmap_statistics (void);
157 /* Initialize a bitmap header. OBSTACK indicates the bitmap obstack
158 to allocate from, NULL for GC'd bitmap. */
160 static inline void
161 bitmap_initialize_stat (bitmap head, bitmap_obstack *obstack MEM_STAT_DECL)
163 head->first = head->current = NULL;
164 head->obstack = obstack;
165 #ifdef GATHER_STATISTICS
166 bitmap_register (head PASS_MEM_STAT);
167 #endif
169 #define bitmap_initialize(h,o) bitmap_initialize_stat (h,o MEM_STAT_INFO)
171 /* Allocate and free bitmaps from obstack, malloc and gc'd memory. */
172 extern bitmap bitmap_obstack_alloc_stat (bitmap_obstack *obstack MEM_STAT_DECL);
173 #define bitmap_obstack_alloc(t) bitmap_obstack_alloc_stat (t MEM_STAT_INFO)
174 extern bitmap bitmap_gc_alloc_stat (ALONE_MEM_STAT_DECL);
175 #define bitmap_gc_alloc() bitmap_gc_alloc_stat (ALONE_MEM_STAT_INFO)
176 extern void bitmap_obstack_free (bitmap);
178 /* A few compatibility/functions macros for compatibility with sbitmaps */
179 #define dump_bitmap(file, bitmap) bitmap_print (file, bitmap, "", "\n")
180 #define bitmap_zero(a) bitmap_clear (a)
181 extern unsigned bitmap_first_set_bit (const_bitmap);
183 /* Compute bitmap hash (for purposes of hashing etc.) */
184 extern hashval_t bitmap_hash(const_bitmap);
186 /* Allocate a bitmap from a bit obstack. */
187 #define BITMAP_ALLOC(OBSTACK) bitmap_obstack_alloc (OBSTACK)
189 /* Allocate a gc'd bitmap. */
190 #define BITMAP_GGC_ALLOC() bitmap_gc_alloc ()
192 /* Do any cleanup needed on a bitmap when it is no longer used. */
193 #define BITMAP_FREE(BITMAP) \
194 ((void)(bitmap_obstack_free (BITMAP), (BITMAP) = NULL))
196 /* Iterator for bitmaps. */
198 typedef struct
200 /* Pointer to the current bitmap element. */
201 bitmap_element *elt1;
203 /* Pointer to 2nd bitmap element when two are involved. */
204 bitmap_element *elt2;
206 /* Word within the current element. */
207 unsigned word_no;
209 /* Contents of the actually processed word. When finding next bit
210 it is shifted right, so that the actual bit is always the least
211 significant bit of ACTUAL. */
212 BITMAP_WORD bits;
213 } bitmap_iterator;
215 /* Initialize a single bitmap iterator. START_BIT is the first bit to
216 iterate from. */
218 static inline void
219 bmp_iter_set_init (bitmap_iterator *bi, const_bitmap map,
220 unsigned start_bit, unsigned *bit_no)
222 bi->elt1 = map->first;
223 bi->elt2 = NULL;
225 /* Advance elt1 until it is not before the block containing start_bit. */
226 while (1)
228 if (!bi->elt1)
230 bi->elt1 = &bitmap_zero_bits;
231 break;
234 if (bi->elt1->indx >= start_bit / BITMAP_ELEMENT_ALL_BITS)
235 break;
236 bi->elt1 = bi->elt1->next;
239 /* We might have gone past the start bit, so reinitialize it. */
240 if (bi->elt1->indx != start_bit / BITMAP_ELEMENT_ALL_BITS)
241 start_bit = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
243 /* Initialize for what is now start_bit. */
244 bi->word_no = start_bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
245 bi->bits = bi->elt1->bits[bi->word_no];
246 bi->bits >>= start_bit % BITMAP_WORD_BITS;
248 /* If this word is zero, we must make sure we're not pointing at the
249 first bit, otherwise our incrementing to the next word boundary
250 will fail. It won't matter if this increment moves us into the
251 next word. */
252 start_bit += !bi->bits;
254 *bit_no = start_bit;
257 /* Initialize an iterator to iterate over the intersection of two
258 bitmaps. START_BIT is the bit to commence from. */
260 static inline void
261 bmp_iter_and_init (bitmap_iterator *bi, const_bitmap map1, const_bitmap map2,
262 unsigned start_bit, unsigned *bit_no)
264 bi->elt1 = map1->first;
265 bi->elt2 = map2->first;
267 /* Advance elt1 until it is not before the block containing
268 start_bit. */
269 while (1)
271 if (!bi->elt1)
273 bi->elt2 = NULL;
274 break;
277 if (bi->elt1->indx >= start_bit / BITMAP_ELEMENT_ALL_BITS)
278 break;
279 bi->elt1 = bi->elt1->next;
282 /* Advance elt2 until it is not before elt1. */
283 while (1)
285 if (!bi->elt2)
287 bi->elt1 = bi->elt2 = &bitmap_zero_bits;
288 break;
291 if (bi->elt2->indx >= bi->elt1->indx)
292 break;
293 bi->elt2 = bi->elt2->next;
296 /* If we're at the same index, then we have some intersecting bits. */
297 if (bi->elt1->indx == bi->elt2->indx)
299 /* We might have advanced beyond the start_bit, so reinitialize
300 for that. */
301 if (bi->elt1->indx != start_bit / BITMAP_ELEMENT_ALL_BITS)
302 start_bit = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
304 bi->word_no = start_bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
305 bi->bits = bi->elt1->bits[bi->word_no] & bi->elt2->bits[bi->word_no];
306 bi->bits >>= start_bit % BITMAP_WORD_BITS;
308 else
310 /* Otherwise we must immediately advance elt1, so initialize for
311 that. */
312 bi->word_no = BITMAP_ELEMENT_WORDS - 1;
313 bi->bits = 0;
316 /* If this word is zero, we must make sure we're not pointing at the
317 first bit, otherwise our incrementing to the next word boundary
318 will fail. It won't matter if this increment moves us into the
319 next word. */
320 start_bit += !bi->bits;
322 *bit_no = start_bit;
325 /* Initialize an iterator to iterate over the bits in MAP1 & ~MAP2.
328 static inline void
329 bmp_iter_and_compl_init (bitmap_iterator *bi, const_bitmap map1, const_bitmap map2,
330 unsigned start_bit, unsigned *bit_no)
332 bi->elt1 = map1->first;
333 bi->elt2 = map2->first;
335 /* Advance elt1 until it is not before the block containing start_bit. */
336 while (1)
338 if (!bi->elt1)
340 bi->elt1 = &bitmap_zero_bits;
341 break;
344 if (bi->elt1->indx >= start_bit / BITMAP_ELEMENT_ALL_BITS)
345 break;
346 bi->elt1 = bi->elt1->next;
349 /* Advance elt2 until it is not before elt1. */
350 while (bi->elt2 && bi->elt2->indx < bi->elt1->indx)
351 bi->elt2 = bi->elt2->next;
353 /* We might have advanced beyond the start_bit, so reinitialize for
354 that. */
355 if (bi->elt1->indx != start_bit / BITMAP_ELEMENT_ALL_BITS)
356 start_bit = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
358 bi->word_no = start_bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
359 bi->bits = bi->elt1->bits[bi->word_no];
360 if (bi->elt2 && bi->elt1->indx == bi->elt2->indx)
361 bi->bits &= ~bi->elt2->bits[bi->word_no];
362 bi->bits >>= start_bit % BITMAP_WORD_BITS;
364 /* If this word is zero, we must make sure we're not pointing at the
365 first bit, otherwise our incrementing to the next word boundary
366 will fail. It won't matter if this increment moves us into the
367 next word. */
368 start_bit += !bi->bits;
370 *bit_no = start_bit;
373 /* Advance to the next bit in BI. We don't advance to the next
374 nonzero bit yet. */
376 static inline void
377 bmp_iter_next (bitmap_iterator *bi, unsigned *bit_no)
379 bi->bits >>= 1;
380 *bit_no += 1;
383 /* Advance to the next nonzero bit of a single bitmap, we will have
384 already advanced past the just iterated bit. Return true if there
385 is a bit to iterate. */
387 static inline bool
388 bmp_iter_set (bitmap_iterator *bi, unsigned *bit_no)
390 /* If our current word is nonzero, it contains the bit we want. */
391 if (bi->bits)
393 next_bit:
394 while (!(bi->bits & 1))
396 bi->bits >>= 1;
397 *bit_no += 1;
399 return true;
402 /* Round up to the word boundary. We might have just iterated past
403 the end of the last word, hence the -1. It is not possible for
404 bit_no to point at the beginning of the now last word. */
405 *bit_no = ((*bit_no + BITMAP_WORD_BITS - 1)
406 / BITMAP_WORD_BITS * BITMAP_WORD_BITS);
407 bi->word_no++;
409 while (1)
411 /* Find the next nonzero word in this elt. */
412 while (bi->word_no != BITMAP_ELEMENT_WORDS)
414 bi->bits = bi->elt1->bits[bi->word_no];
415 if (bi->bits)
416 goto next_bit;
417 *bit_no += BITMAP_WORD_BITS;
418 bi->word_no++;
421 /* Advance to the next element. */
422 bi->elt1 = bi->elt1->next;
423 if (!bi->elt1)
424 return false;
425 *bit_no = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
426 bi->word_no = 0;
430 /* Advance to the next nonzero bit of an intersecting pair of
431 bitmaps. We will have already advanced past the just iterated bit.
432 Return true if there is a bit to iterate. */
434 static inline bool
435 bmp_iter_and (bitmap_iterator *bi, unsigned *bit_no)
437 /* If our current word is nonzero, it contains the bit we want. */
438 if (bi->bits)
440 next_bit:
441 while (!(bi->bits & 1))
443 bi->bits >>= 1;
444 *bit_no += 1;
446 return true;
449 /* Round up to the word boundary. We might have just iterated past
450 the end of the last word, hence the -1. It is not possible for
451 bit_no to point at the beginning of the now last word. */
452 *bit_no = ((*bit_no + BITMAP_WORD_BITS - 1)
453 / BITMAP_WORD_BITS * BITMAP_WORD_BITS);
454 bi->word_no++;
456 while (1)
458 /* Find the next nonzero word in this elt. */
459 while (bi->word_no != BITMAP_ELEMENT_WORDS)
461 bi->bits = bi->elt1->bits[bi->word_no] & bi->elt2->bits[bi->word_no];
462 if (bi->bits)
463 goto next_bit;
464 *bit_no += BITMAP_WORD_BITS;
465 bi->word_no++;
468 /* Advance to the next identical element. */
471 /* Advance elt1 while it is less than elt2. We always want
472 to advance one elt. */
475 bi->elt1 = bi->elt1->next;
476 if (!bi->elt1)
477 return false;
479 while (bi->elt1->indx < bi->elt2->indx);
481 /* Advance elt2 to be no less than elt1. This might not
482 advance. */
483 while (bi->elt2->indx < bi->elt1->indx)
485 bi->elt2 = bi->elt2->next;
486 if (!bi->elt2)
487 return false;
490 while (bi->elt1->indx != bi->elt2->indx);
492 *bit_no = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
493 bi->word_no = 0;
497 /* Advance to the next nonzero bit in the intersection of
498 complemented bitmaps. We will have already advanced past the just
499 iterated bit. */
501 static inline bool
502 bmp_iter_and_compl (bitmap_iterator *bi, unsigned *bit_no)
504 /* If our current word is nonzero, it contains the bit we want. */
505 if (bi->bits)
507 next_bit:
508 while (!(bi->bits & 1))
510 bi->bits >>= 1;
511 *bit_no += 1;
513 return true;
516 /* Round up to the word boundary. We might have just iterated past
517 the end of the last word, hence the -1. It is not possible for
518 bit_no to point at the beginning of the now last word. */
519 *bit_no = ((*bit_no + BITMAP_WORD_BITS - 1)
520 / BITMAP_WORD_BITS * BITMAP_WORD_BITS);
521 bi->word_no++;
523 while (1)
525 /* Find the next nonzero word in this elt. */
526 while (bi->word_no != BITMAP_ELEMENT_WORDS)
528 bi->bits = bi->elt1->bits[bi->word_no];
529 if (bi->elt2 && bi->elt2->indx == bi->elt1->indx)
530 bi->bits &= ~bi->elt2->bits[bi->word_no];
531 if (bi->bits)
532 goto next_bit;
533 *bit_no += BITMAP_WORD_BITS;
534 bi->word_no++;
537 /* Advance to the next element of elt1. */
538 bi->elt1 = bi->elt1->next;
539 if (!bi->elt1)
540 return false;
542 /* Advance elt2 until it is no less than elt1. */
543 while (bi->elt2 && bi->elt2->indx < bi->elt1->indx)
544 bi->elt2 = bi->elt2->next;
546 *bit_no = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
547 bi->word_no = 0;
551 /* Loop over all bits set in BITMAP, starting with MIN and setting
552 BITNUM to the bit number. ITER is a bitmap iterator. BITNUM
553 should be treated as a read-only variable as it contains loop
554 state. */
556 #define EXECUTE_IF_SET_IN_BITMAP(BITMAP, MIN, BITNUM, ITER) \
557 for (bmp_iter_set_init (&(ITER), (BITMAP), (MIN), &(BITNUM)); \
558 bmp_iter_set (&(ITER), &(BITNUM)); \
559 bmp_iter_next (&(ITER), &(BITNUM)))
561 /* Loop over all the bits set in BITMAP1 & BITMAP2, starting with MIN
562 and setting BITNUM to the bit number. ITER is a bitmap iterator.
563 BITNUM should be treated as a read-only variable as it contains
564 loop state. */
566 #define EXECUTE_IF_AND_IN_BITMAP(BITMAP1, BITMAP2, MIN, BITNUM, ITER) \
567 for (bmp_iter_and_init (&(ITER), (BITMAP1), (BITMAP2), (MIN), \
568 &(BITNUM)); \
569 bmp_iter_and (&(ITER), &(BITNUM)); \
570 bmp_iter_next (&(ITER), &(BITNUM)))
572 /* Loop over all the bits set in BITMAP1 & ~BITMAP2, starting with MIN
573 and setting BITNUM to the bit number. ITER is a bitmap iterator.
574 BITNUM should be treated as a read-only variable as it contains
575 loop state. */
577 #define EXECUTE_IF_AND_COMPL_IN_BITMAP(BITMAP1, BITMAP2, MIN, BITNUM, ITER) \
578 for (bmp_iter_and_compl_init (&(ITER), (BITMAP1), (BITMAP2), (MIN), \
579 &(BITNUM)); \
580 bmp_iter_and_compl (&(ITER), &(BITNUM)); \
581 bmp_iter_next (&(ITER), &(BITNUM)))
583 #endif /* GCC_BITMAP_H */