1 /* Functions to support general ended bitmaps.
2 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
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
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
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
25 /* Fundamental storage type for bitmap. */
27 typedef unsigned long BITMAP_WORD;
28 /* BITMAP_WORD_BITS needs to be unsigned, but cannot contain casts as
29 it is used in preprocessor directives -- hence the 1u. */
30 #define BITMAP_WORD_BITS (CHAR_BIT * SIZEOF_LONG * 1u)
32 /* Number of words to use for each element in the linked list. */
34 #ifndef BITMAP_ELEMENT_WORDS
35 #define BITMAP_ELEMENT_WORDS ((128 + BITMAP_WORD_BITS - 1) / BITMAP_WORD_BITS)
38 /* Number of bits in each actual element of a bitmap. */
40 #define BITMAP_ELEMENT_ALL_BITS (BITMAP_ELEMENT_WORDS * BITMAP_WORD_BITS)
42 /* Bitmap set element. We use a linked list to hold only the bits that
43 are set. This allows for use to grow the bitset dynamically without
44 having to realloc and copy a giant bit array. The `prev' field is
45 undefined for an element on the free list. */
47 typedef struct bitmap_element_def GTY(())
49 struct bitmap_element_def *next; /* Next element. */
50 struct bitmap_element_def *prev; /* Previous element. */
51 unsigned int indx; /* regno/BITMAP_ELEMENT_ALL_BITS. */
52 BITMAP_WORD bits[BITMAP_ELEMENT_WORDS]; /* Bits that are set. */
55 /* Head of bitmap linked list. */
56 typedef struct bitmap_head_def GTY(()) {
57 bitmap_element *first; /* First element in linked list. */
58 bitmap_element *current; /* Last element looked at. */
59 unsigned int indx; /* Index of last element looked at. */
60 int using_obstack; /* Are we using an obstack or ggc for
63 typedef struct bitmap_head_def *bitmap;
66 extern bitmap_element bitmap_zero_bits; /* Zero bitmap element */
68 /* Clear a bitmap by freeing up the linked list. */
69 extern void bitmap_clear (bitmap);
71 /* Copy a bitmap to another bitmap. */
72 extern void bitmap_copy (bitmap, bitmap);
74 /* True if two bitmaps are identical. */
75 extern bool bitmap_equal_p (bitmap, bitmap);
77 /* True if the bitmaps intersect (their AND is non-empty). */
78 extern bool bitmap_intersect_p (bitmap, bitmap);
80 /* True if the complement of the second intersects the first (their
81 AND_COMPL is non-empty). */
82 extern bool bitmap_intersect_compl_p (bitmap, bitmap);
84 /* True if MAP is an empty bitmap. */
85 #define bitmap_empty_p(MAP) (!(MAP)->first)
87 /* Boolean operations on bitmaps. The _into variants are two operand
88 versions that modify the first source operand. The other variants
89 are three operand versions that to not destroy the source bitmaps.
90 The operations supported are &, & ~, |, ^. */
91 extern void bitmap_and (bitmap, bitmap, bitmap);
92 extern void bitmap_and_into (bitmap, bitmap);
93 extern void bitmap_and_compl (bitmap, bitmap, bitmap);
94 extern void bitmap_and_compl_into (bitmap, bitmap);
95 extern bool bitmap_ior (bitmap, bitmap, bitmap);
96 extern bool bitmap_ior_into (bitmap, bitmap);
97 extern void bitmap_xor (bitmap, bitmap, bitmap);
98 extern void bitmap_xor_into (bitmap, bitmap);
100 /* DST = A | (B & ~C). Return true if DST changes. */
101 extern bool bitmap_ior_and_compl (bitmap DST, bitmap A, bitmap B, bitmap C);
102 /* A |= (B & ~C). Return true if A changes. */
103 extern bool bitmap_ior_and_compl_into (bitmap DST, bitmap B, bitmap C);
105 /* Clear a single register in a register set. */
106 extern void bitmap_clear_bit (bitmap, int);
108 /* Set a single register in a register set. */
109 extern void bitmap_set_bit (bitmap, int);
111 /* Return true if a register is set in a register set. */
112 extern int bitmap_bit_p (bitmap, int);
114 /* Debug functions to print a bitmap linked list. */
115 extern void debug_bitmap (bitmap);
116 extern void debug_bitmap_file (FILE *, bitmap);
118 /* Print a bitmap. */
119 extern void bitmap_print (FILE *, bitmap, const char *, const char *);
121 /* Initialize a bitmap header. If HEAD is NULL, a new header will be
122 allocated. USING_OBSTACK indicates how elements should be allocated. */
123 extern bitmap bitmap_initialize (bitmap head, int using_obstack);
125 /* Release all memory used by the bitmap obstack. */
126 extern void bitmap_release_memory (void);
128 /* A few compatibility/functions macros for compatibility with sbitmaps */
129 #define dump_bitmap(file, bitmap) bitmap_print (file, bitmap, "", "\n")
130 #define bitmap_zero(a) bitmap_clear (a)
131 extern unsigned bitmap_first_set_bit (bitmap);
133 /* Allocate a bitmap with oballoc. */
134 #define BITMAP_OBSTACK_ALLOC(OBSTACK) \
135 bitmap_initialize (obstack_alloc (OBSTACK, sizeof (bitmap_head)), 1)
137 /* Allocate a bitmap with ggc_alloc. */
138 #define BITMAP_GGC_ALLOC() \
139 bitmap_initialize (NULL, 0)
141 /* Allocate a bitmap with xmalloc. */
142 #define BITMAP_XMALLOC() \
143 bitmap_initialize (xmalloc (sizeof (bitmap_head)), 1)
145 /* Do any cleanup needed on a bitmap when it is no longer used. */
146 #define BITMAP_FREE(BITMAP) \
150 bitmap_clear (BITMAP); \
155 /* Do any cleanup needed on an xmalloced bitmap when it is no longer used. */
156 #define BITMAP_XFREE(BITMAP) \
160 bitmap_clear (BITMAP); \
166 /* Do any one-time initializations needed for bitmaps. */
167 #define BITMAP_INIT_ONCE()
169 /* Iterator for bitmaps. */
173 /* Pointer to the current bitmap element. */
174 bitmap_element *elt1;
176 /* Pointer to 2nd bitmap element when two are involved. */
177 bitmap_element *elt2;
179 /* Word within the current element. */
182 /* Contents of the actually processed word. When finding next bit
183 it is shifted right, so that the actual bit is always the least
184 significant bit of ACTUAL. */
188 /* Initialize a single bitmap iterator. START_BIT is the first bit to
192 bmp_iter_set_init (bitmap_iterator *bi, bitmap map,
193 unsigned start_bit, unsigned *bit_no)
195 bi->elt1 = map->first;
198 /* Advance elt1 until it is not before the block containing start_bit. */
203 bi->elt1 = &bitmap_zero_bits;
207 if (bi->elt1->indx >= start_bit / BITMAP_ELEMENT_ALL_BITS)
209 bi->elt1 = bi->elt1->next;
212 /* We might have gone past the start bit, so reinitialize it. */
213 if (bi->elt1->indx != start_bit / BITMAP_ELEMENT_ALL_BITS)
214 start_bit = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
216 /* Initialize for what is now start_bit. */
217 bi->word_no = start_bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
218 bi->bits = bi->elt1->bits[bi->word_no];
219 bi->bits >>= start_bit % BITMAP_WORD_BITS;
221 /* If this word is zero, we must make sure we're not pointing at the
222 first bit, otherwise our incrementing to the next word boundary
223 will fail. It won't matter if this increment moves us into the
225 start_bit += !bi->bits;
230 /* Initialize an iterator to iterate over the intersection of two
231 bitmaps. START_BIT is the bit to commence from. */
234 bmp_iter_and_init (bitmap_iterator *bi, bitmap map1, bitmap map2,
235 unsigned start_bit, unsigned *bit_no)
237 bi->elt1 = map1->first;
238 bi->elt2 = map2->first;
240 /* Advance elt1 until it is not before the block containing
250 if (bi->elt1->indx >= start_bit / BITMAP_ELEMENT_ALL_BITS)
252 bi->elt1 = bi->elt1->next;
255 /* Advance elt2 until it is not before elt1. */
260 bi->elt1 = bi->elt2 = &bitmap_zero_bits;
264 if (bi->elt2->indx >= bi->elt1->indx)
266 bi->elt2 = bi->elt2->next;
269 /* If we're at the same index, then we have some intersecting bits. */
270 if (bi->elt1->indx == bi->elt2->indx)
272 /* We might have advanced beyond the start_bit, so reinitialize
274 if (bi->elt1->indx != start_bit / BITMAP_ELEMENT_ALL_BITS)
275 start_bit = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
277 bi->word_no = start_bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
278 bi->bits = bi->elt1->bits[bi->word_no] & bi->elt2->bits[bi->word_no];
279 bi->bits >>= start_bit % BITMAP_WORD_BITS;
283 /* Otherwise we must immediately advance elt1, so initialize for
285 bi->word_no = BITMAP_ELEMENT_WORDS - 1;
289 /* If this word is zero, we must make sure we're not pointing at the
290 first bit, otherwise our incrementing to the next word boundary
291 will fail. It won't matter if this increment moves us into the
293 start_bit += !bi->bits;
298 /* Initialize an iterator to iterate over the bits in MAP1 & ~MAP2.
302 bmp_iter_and_compl_init (bitmap_iterator *bi, bitmap map1, bitmap map2,
303 unsigned start_bit, unsigned *bit_no)
305 bi->elt1 = map1->first;
306 bi->elt2 = map2->first;
308 /* Advance elt1 until it is not before the block containing start_bit. */
313 bi->elt1 = &bitmap_zero_bits;
317 if (bi->elt1->indx >= start_bit / BITMAP_ELEMENT_ALL_BITS)
319 bi->elt1 = bi->elt1->next;
322 /* Advance elt2 until it is not before elt1. */
323 while (bi->elt2 && bi->elt2->indx < bi->elt1->indx)
324 bi->elt2 = bi->elt2->next;
326 /* We might have advanced beyond the start_bit, so reinitialize for
328 if (bi->elt1->indx != start_bit / BITMAP_ELEMENT_ALL_BITS)
329 start_bit = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
331 bi->word_no = start_bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
332 bi->bits = bi->elt1->bits[bi->word_no];
333 if (bi->elt2 && bi->elt1->indx == bi->elt2->indx)
334 bi->bits &= ~bi->elt2->bits[bi->word_no];
335 bi->bits >>= start_bit % BITMAP_WORD_BITS;
337 /* If this word is zero, we must make sure we're not pointing at the
338 first bit, otherwise our incrementing to the next word boundary
339 will fail. It won't matter if this increment moves us into the
341 start_bit += !bi->bits;
346 /* Advance to the next bit in BI. We don't advance to the next
350 bmp_iter_next (bitmap_iterator *bi, unsigned *bit_no)
356 /* Advance to the next nonzero bit of a single bitmap, we will have
357 already advanced past the just iterated bit. Return true if there
358 is a bit to iterate. */
361 bmp_iter_set (bitmap_iterator *bi, unsigned *bit_no)
363 /* If our current word is nonzero, it contains the bit we want. */
367 while (!(bi->bits & 1))
375 /* Round up to the word boundary. We might have just iterated past
376 the end of the last word, hence the -1. It is not possible for
377 bit_no to point at the beginning of the now last word. */
378 *bit_no = ((*bit_no + BITMAP_WORD_BITS - 1)
379 / BITMAP_WORD_BITS * BITMAP_WORD_BITS);
384 /* Find the next nonzero word in this elt. */
385 while (bi->word_no != BITMAP_ELEMENT_WORDS)
387 bi->bits = bi->elt1->bits[bi->word_no];
390 *bit_no += BITMAP_WORD_BITS;
394 /* Advance to the next element. */
395 bi->elt1 = bi->elt1->next;
398 *bit_no = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
403 /* Advance to the next nonzero bit of an intersecting pair of
404 bitmaps. We will have already advanced past the just iterated bit.
405 Return true if there is a bit to iterate. */
408 bmp_iter_and (bitmap_iterator *bi, unsigned *bit_no)
410 /* If our current word is nonzero, it contains the bit we want. */
414 while (!(bi->bits & 1))
422 /* Round up to the word boundary. We might have just iterated past
423 the end of the last word, hence the -1. It is not possible for
424 bit_no to point at the beginning of the now last word. */
425 *bit_no = ((*bit_no + BITMAP_WORD_BITS - 1)
426 / BITMAP_WORD_BITS * BITMAP_WORD_BITS);
431 /* Find the next nonzero word in this elt. */
432 while (bi->word_no != BITMAP_ELEMENT_WORDS)
434 bi->bits = bi->elt1->bits[bi->word_no] & bi->elt2->bits[bi->word_no];
437 *bit_no += BITMAP_WORD_BITS;
441 /* Advance to the next identical element. */
444 /* Advance elt1 while it is less than elt2. We always want
445 to advance one elt. */
448 bi->elt1 = bi->elt1->next;
452 while (bi->elt1->indx < bi->elt2->indx);
454 /* Advance elt2 to be no less than elt1. This might not
456 while (bi->elt2->indx < bi->elt1->indx)
458 bi->elt2 = bi->elt2->next;
463 while (bi->elt1->indx != bi->elt2->indx);
465 *bit_no = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
470 /* Advance to the next nonzero bit in the intersection of
471 complemented bitmaps. We will have already advanced past the just
475 bmp_iter_and_compl (bitmap_iterator *bi, unsigned *bit_no)
477 /* If our current word is nonzero, it contains the bit we want. */
481 while (!(bi->bits & 1))
489 /* Round up to the word boundary. We might have just iterated past
490 the end of the last word, hence the -1. It is not possible for
491 bit_no to point at the beginning of the now last word. */
492 *bit_no = ((*bit_no + BITMAP_WORD_BITS - 1)
493 / BITMAP_WORD_BITS * BITMAP_WORD_BITS);
498 /* Find the next nonzero word in this elt. */
499 while (bi->word_no != BITMAP_ELEMENT_WORDS)
501 bi->bits = bi->elt1->bits[bi->word_no];
502 if (bi->elt2 && bi->elt2->indx == bi->elt1->indx)
503 bi->bits &= ~bi->elt2->bits[bi->word_no];
506 *bit_no += BITMAP_WORD_BITS;
510 /* Advance to the next element of elt1. */
511 bi->elt1 = bi->elt1->next;
515 /* Advance elt2 until it is no less than elt1. */
516 while (bi->elt2 && bi->elt2->indx < bi->elt1->indx)
517 bi->elt2 = bi->elt2->next;
519 *bit_no = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
524 /* Loop over all bits set in BITMAP, starting with MIN and setting
525 BITNUM to the bit number. ITER is a bitmap iterator. BITNUM
526 should be treated as a read-only variable as it contains loop
529 #define EXECUTE_IF_SET_IN_BITMAP(BITMAP, MIN, BITNUM, ITER) \
530 for (bmp_iter_set_init (&(ITER), (BITMAP), (MIN), &(BITNUM)); \
531 bmp_iter_set (&(ITER), &(BITNUM)); \
532 bmp_iter_next (&(ITER), &(BITNUM)))
534 /* Loop over all the bits set in BITMAP1 & BITMAP2, starting with MIN
535 and setting BITNUM to the bit number. ITER is a bitmap iterator.
536 BITNUM should be treated as a read-only variable as it contains
539 #define EXECUTE_IF_AND_IN_BITMAP(BITMAP1, BITMAP2, MIN, BITNUM, ITER) \
540 for (bmp_iter_and_init (&(ITER), (BITMAP1), (BITMAP2), (MIN), \
542 bmp_iter_and (&(ITER), &(BITNUM)); \
543 bmp_iter_next (&(ITER), &(BITNUM)))
545 /* Loop over all the bits set in BITMAP1 & ~BITMAP2, starting with MIN
546 and setting BITNUM to the bit number. ITER is a bitmap iterator.
547 BITNUM should be treated as a read-only variable as it contains
550 #define EXECUTE_IF_AND_COMPL_IN_BITMAP(BITMAP1, BITMAP2, MIN, BITNUM, ITER) \
551 for (bmp_iter_and_compl_init (&(ITER), (BITMAP1), (BITMAP2), (MIN), \
553 bmp_iter_and_compl (&(ITER), &(BITNUM)); \
554 bmp_iter_next (&(ITER), &(BITNUM)))
556 #endif /* GCC_BITMAP_H */