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 HOST_WIDE_INT BITMAP_WORD; */
28 /* #define nBITMAP_WORD_BITS HOST_BITS_PER_WIDE_INT */
29 typedef unsigned long BITMAP_WORD;
30 #define nBITMAP_WORD_BITS (CHAR_BIT * SIZEOF_LONG)
31 #define BITMAP_WORD_BITS (unsigned) nBITMAP_WORD_BITS
33 /* Number of words to use for each element in the linked list. */
35 #ifndef BITMAP_ELEMENT_WORDS
36 #define BITMAP_ELEMENT_WORDS ((128 + nBITMAP_WORD_BITS - 1) / nBITMAP_WORD_BITS)
39 /* Number of bits in each actual element of a bitmap. We get slightly better
40 code for bit % BITMAP_ELEMENT_ALL_BITS and bit / BITMAP_ELEMENT_ALL_BITS if
41 bits is unsigned, assuming it is a power of 2. */
43 #define BITMAP_ELEMENT_ALL_BITS \
44 ((unsigned) (BITMAP_ELEMENT_WORDS * BITMAP_WORD_BITS))
46 /* Bitmap set element. We use a linked list to hold only the bits that
47 are set. This allows for use to grow the bitset dynamically without
48 having to realloc and copy a giant bit array. The `prev' field is
49 undefined for an element on the free list. */
51 typedef struct bitmap_element_def GTY(())
53 struct bitmap_element_def *next; /* Next element. */
54 struct bitmap_element_def *prev; /* Previous element. */
55 unsigned int indx; /* regno/BITMAP_ELEMENT_ALL_BITS. */
56 BITMAP_WORD bits[BITMAP_ELEMENT_WORDS]; /* Bits that are set. */
59 /* Head of bitmap linked list. */
60 typedef struct bitmap_head_def GTY(()) {
61 bitmap_element *first; /* First element in linked list. */
62 bitmap_element *current; /* Last element looked at. */
63 unsigned int indx; /* Index of last element looked at. */
64 int using_obstack; /* Are we using an obstack or ggc for
67 typedef struct bitmap_head_def *bitmap;
69 /* Enumeration giving the various operations we support. */
71 BITMAP_AND, /* TO = FROM1 & FROM2 */
72 BITMAP_AND_COMPL, /* TO = FROM1 & ~ FROM2 */
73 BITMAP_IOR, /* TO = FROM1 | FROM2 */
74 BITMAP_XOR, /* TO = FROM1 ^ FROM2 */
75 BITMAP_IOR_COMPL /* TO = FROM1 | ~FROM2 */
79 extern bitmap_element bitmap_zero_bits; /* Zero bitmap element */
81 /* Clear a bitmap by freeing up the linked list. */
82 extern void bitmap_clear (bitmap);
84 /* Copy a bitmap to another bitmap. */
85 extern void bitmap_copy (bitmap, bitmap);
87 /* True if two bitmaps are identical. */
88 extern bool bitmap_equal_p (bitmap, bitmap);
90 /* True if the bitmaps intersect (their AND is non-empty). */
91 extern bool bitmap_intersect_p (bitmap, bitmap);
93 /* True if the complement of the second intersects the first (their
94 AND_COMPL is non-empty). */
95 extern bool bitmap_intersect_compl_p (bitmap, bitmap);
97 /* True if MAP is an empty bitmap. */
98 #define bitmap_empty_p(MAP) (!(MAP)->first)
100 /* Perform an operation on two bitmaps, yielding a third. */
101 extern int bitmap_operation (bitmap, bitmap, bitmap, enum bitmap_bits);
103 #define bitmap_and(DST,A,B) (void)bitmap_operation (DST,A,B,BITMAP_AND)
104 #define bitmap_and_into(DST_SRC,B) (void)bitmap_operation (DST_SRC,DST_SRC,B,BITMAP_AND)
105 #define bitmap_and_compl(DST,A,B) (void)bitmap_operation (DST,A,B,BITMAP_AND_COMPL)
106 #define bitmap_and_compl_into(DST_SRC,B) (void)bitmap_operation (DST_SRC,DST_SRC,B,BITMAP_AND_COMPL)
107 #define bitmap_ior(DST,A,B) (void)bitmap_operation (DST,A,B,BITMAP_IOR)
108 #define bitmap_ior_into(DST_SRC,B) (void)bitmap_operation (DST_SRC,DST_SRC,B,BITMAP_IOR)
109 #define bitmap_ior_compl(DST,A,B) (void)bitmap_operation (DST,A,Br,BITMAP_IOR_COMPL)
110 #define bitmap_xor(DST,A,B) (void)bitmap_operation (DST,A,B,BITMAP_XOR)
111 #define bitmap_xor_into(DST_SRC,B) (void)bitmap_operation (DST_SRC,DST_SRC,B,BITMAP_XOR)
113 /* `or' into one bitmap the `and' of a second bitmap witih the complement
114 of a third. Return nonzero if the bitmap changes. */
115 extern bool bitmap_ior_and_compl_into (bitmap, bitmap, bitmap);
116 extern bool bitmap_ior_and_compl (bitmap, bitmap, bitmap, bitmap);
118 /* Clear a single register in a register set. */
119 extern void bitmap_clear_bit (bitmap, int);
121 /* Set a single register in a register set. */
122 extern void bitmap_set_bit (bitmap, int);
124 /* Return true if a register is set in a register set. */
125 extern int bitmap_bit_p (bitmap, int);
127 /* Debug functions to print a bitmap linked list. */
128 extern void debug_bitmap (bitmap);
129 extern void debug_bitmap_file (FILE *, bitmap);
131 /* Print a bitmap. */
132 extern void bitmap_print (FILE *, bitmap, const char *, const char *);
134 /* Initialize a bitmap header. If HEAD is NULL, a new header will be
135 allocated. USING_OBSTACK indicates how elements should be allocated. */
136 extern bitmap bitmap_initialize (bitmap head, int using_obstack);
138 /* Release all memory used by the bitmap obstack. */
139 extern void bitmap_release_memory (void);
141 /* A few compatibility/functions macros for compatibility with sbitmaps */
142 #define dump_bitmap(file, bitmap) bitmap_print (file, bitmap, "", "\n")
143 #define bitmap_zero(a) bitmap_clear (a)
144 #define bitmap_a_or_b(a,b,c) bitmap_operation (a, b, c, BITMAP_IOR)
145 #define bitmap_a_and_b(a,b,c) bitmap_operation (a, b, c, BITMAP_AND)
146 extern int bitmap_first_set_bit (bitmap);
147 extern int bitmap_last_set_bit (bitmap);
149 /* Allocate a bitmap with oballoc. */
150 #define BITMAP_OBSTACK_ALLOC(OBSTACK) \
151 bitmap_initialize (obstack_alloc (OBSTACK, sizeof (bitmap_head)), 1)
153 /* Allocate a bitmap with ggc_alloc. */
154 #define BITMAP_GGC_ALLOC() \
155 bitmap_initialize (NULL, 0)
157 /* Allocate a bitmap with xmalloc. */
158 #define BITMAP_XMALLOC() \
159 bitmap_initialize (xmalloc (sizeof (bitmap_head)), 1)
161 /* Do any cleanup needed on a bitmap when it is no longer used. */
162 #define BITMAP_FREE(BITMAP) \
166 bitmap_clear (BITMAP); \
171 /* Do any cleanup needed on an xmalloced bitmap when it is no longer used. */
172 #define BITMAP_XFREE(BITMAP) \
176 bitmap_clear (BITMAP); \
182 /* Do any one-time initializations needed for bitmaps. */
183 #define BITMAP_INIT_ONCE()
185 /* Iterator for bitmaps. */
189 /* Actual elements in the bitmaps. */
190 bitmap_element *ptr1, *ptr2;
192 /* Position of an actual word in the elements. */
195 /* Position of a bit corresponding to the start of word. */
198 /* Position of the actual bit. */
201 /* Contents of the actually processed word. When finding next bit
202 it is shifted right, so that the actual bit is always the least
203 significant bit of ACTUAL. */
207 /* Moves the iterator BI to the first set bit on or after the current
208 position in bitmap and returns the bit if available. The bit is
209 found in ACTUAL field only. */
211 static inline unsigned
212 bmp_iter_common_next_1 (bitmap_iterator *bi)
214 while (!(bi->actual & 1))
223 /* Moves the iterator BI to the first set bit on or after the current
224 position in bitmap and returns the bit if available. */
226 static inline unsigned
227 bmp_iter_single_next_1 (bitmap_iterator *bi)
230 return bmp_iter_common_next_1 (bi);
233 bi->word_bit += BITMAP_WORD_BITS;
238 bi->word < BITMAP_ELEMENT_WORDS;
239 bi->word++, bi->word_bit += BITMAP_WORD_BITS)
241 bi->actual = bi->ptr1->bits[bi->word];
244 bi->bit = bi->word_bit;
245 return bmp_iter_common_next_1 (bi);
249 bi->ptr1 = bi->ptr1->next;
254 bi->word_bit = bi->ptr1->indx * BITMAP_ELEMENT_ALL_BITS;
258 /* Initializes a bitmap iterator BI for looping over bits of bitmap
259 BMP, starting with bit MIN. Returns the first bit of BMP greater
260 or equal to MIN if there is any. */
262 static inline unsigned
263 bmp_iter_single_init (bitmap_iterator *bi, bitmap bmp, unsigned min)
265 unsigned indx = min / BITMAP_ELEMENT_ALL_BITS;
267 for (bi->ptr1 = bmp->first;
268 bi->ptr1 && bi->ptr1->indx < indx;
269 bi->ptr1 = bi->ptr1->next)
274 /* To avoid warnings. */
283 if (bi->ptr1->indx == indx)
285 unsigned bit_in_elt = min - BITMAP_ELEMENT_ALL_BITS * indx;
286 unsigned word_in_elt = bit_in_elt / BITMAP_WORD_BITS;
287 unsigned bit_in_word = bit_in_elt % BITMAP_WORD_BITS;
289 bi->word = word_in_elt;
290 bi->word_bit = min - bit_in_word;
292 bi->actual = bi->ptr1->bits[word_in_elt] >> bit_in_word;
297 bi->bit = bi->ptr1->indx * BITMAP_ELEMENT_ALL_BITS;
298 bi->word_bit = bi->bit;
299 bi->actual = bi->ptr1->bits[0];
302 return bmp_iter_single_next_1 (bi);
305 /* Returns true if all elements of the bitmap referred to by iterator BI
309 bmp_iter_end_p (const bitmap_iterator *bi)
311 return bi->ptr1 == NULL;
314 /* Moves the iterator BI to the next bit of bitmap and returns the bit
317 static inline unsigned
318 bmp_iter_single_next (bitmap_iterator *bi)
322 return bmp_iter_single_next_1 (bi);
325 /* Loop over all bits in BITMAP, starting with MIN and setting BITNUM to
326 the bit number. ITER is a bitmap iterator. */
328 #define EXECUTE_IF_SET_IN_BITMAP(BITMAP, MIN, BITNUM, ITER) \
329 for ((BITNUM) = bmp_iter_single_init (&(ITER), (BITMAP), (MIN)); \
330 !bmp_iter_end_p (&(ITER)); \
331 (BITNUM) = bmp_iter_single_next (&(ITER)))
333 /* Moves the iterator BI to the first set bit on or after the current
334 position in difference of bitmaps and returns the bit if available. */
336 static inline unsigned
337 bmp_iter_and_not_next_1 (bitmap_iterator *bi)
340 return bmp_iter_common_next_1 (bi);
343 bi->word_bit += BITMAP_WORD_BITS;
349 if (bi->ptr2 && bi->ptr2->indx == bi->ptr1->indx)
352 snd = &bitmap_zero_bits;
355 bi->word < BITMAP_ELEMENT_WORDS;
356 bi->word++, bi->word_bit += BITMAP_WORD_BITS)
358 bi->actual = (bi->ptr1->bits[bi->word]
359 & ~snd->bits[bi->word]);
362 bi->bit = bi->word_bit;
363 return bmp_iter_common_next_1 (bi);
367 bi->ptr1 = bi->ptr1->next;
372 && bi->ptr2->indx < bi->ptr1->indx)
373 bi->ptr2 = bi->ptr2->next;
376 bi->word_bit = bi->ptr1->indx * BITMAP_ELEMENT_ALL_BITS;
380 /* Initializes a bitmap iterator BI for looping over bits of bitmap
381 BMP1 &~ BMP2, starting with bit MIN. Returns the first bit of
382 BMP1 &~ BMP2 greater or equal to MIN if there is any. */
384 static inline unsigned
385 bmp_iter_and_not_init (bitmap_iterator *bi, bitmap bmp1, bitmap bmp2,
388 unsigned indx = min / BITMAP_ELEMENT_ALL_BITS;
390 for (bi->ptr1 = bmp1->first;
391 bi->ptr1 && bi->ptr1->indx < indx;
392 bi->ptr1 = bi->ptr1->next)
397 /* To avoid warnings. */
406 for (bi->ptr2 = bmp2->first;
407 bi->ptr2 && bi->ptr2->indx < bi->ptr1->indx;
408 bi->ptr2 = bi->ptr2->next)
411 if (bi->ptr1->indx == indx)
413 unsigned bit_in_elt = min - BITMAP_ELEMENT_ALL_BITS * indx;
414 unsigned word_in_elt = bit_in_elt / BITMAP_WORD_BITS;
415 unsigned bit_in_word = bit_in_elt % BITMAP_WORD_BITS;
417 bi->word = word_in_elt;
418 bi->word_bit = min - bit_in_word;
421 if (bi->ptr2 && bi->ptr2->indx == indx)
422 bi->actual = (bi->ptr1->bits[word_in_elt]
423 & ~bi->ptr2->bits[word_in_elt]) >> bit_in_word;
425 bi->actual = bi->ptr1->bits[word_in_elt] >> bit_in_word;
430 bi->bit = bi->ptr1->indx * BITMAP_ELEMENT_ALL_BITS;
431 bi->word_bit = bi->bit;
433 if (bi->ptr2 && bi->ptr2->indx == bi->ptr1->indx)
434 bi->actual = (bi->ptr1->bits[0] & ~bi->ptr2->bits[0]);
436 bi->actual = bi->ptr1->bits[0];
439 return bmp_iter_and_not_next_1 (bi);
442 /* Moves the iterator BI to the next bit of difference of bitmaps and returns
443 the bit if available. */
445 static inline unsigned
446 bmp_iter_and_not_next (bitmap_iterator *bi)
450 return bmp_iter_and_not_next_1 (bi);
453 /* Loop over all bits in BMP1 and BMP2, starting with MIN, setting
454 BITNUM to the bit number for all bits that are set in the first bitmap
455 and not set in the second. ITER is a bitmap iterator. */
457 #define EXECUTE_IF_AND_COMPL_IN_BITMAP(BMP1, BMP2, MIN, BITNUM, ITER) \
458 for ((BITNUM) = bmp_iter_and_not_init (&(ITER), (BMP1), (BMP2), (MIN)); \
459 !bmp_iter_end_p (&(ITER)); \
460 (BITNUM) = bmp_iter_and_not_next (&(ITER)))
462 /* Moves the iterator BI to the first set bit on or after the current
463 position in intersection of bitmaps and returns the bit if available. */
465 static inline unsigned
466 bmp_iter_and_next_1 (bitmap_iterator *bi)
469 return bmp_iter_common_next_1 (bi);
472 bi->word_bit += BITMAP_WORD_BITS;
477 bi->word < BITMAP_ELEMENT_WORDS;
478 bi->word++, bi->word_bit += BITMAP_WORD_BITS)
480 bi->actual = (bi->ptr1->bits[bi->word]
481 & bi->ptr2->bits[bi->word]);
484 bi->bit = bi->word_bit;
485 return bmp_iter_common_next_1 (bi);
491 bi->ptr1 = bi->ptr1->next;
495 while (bi->ptr2->indx < bi->ptr1->indx)
497 bi->ptr2 = bi->ptr2->next;
505 while (bi->ptr1->indx != bi->ptr2->indx);
508 bi->word_bit = bi->ptr1->indx * BITMAP_ELEMENT_ALL_BITS;
512 /* Initializes a bitmap iterator BI for looping over bits of bitmap
513 BMP1 & BMP2, starting with bit MIN. Returns the first bit of
514 BMP1 & BMP2 greater or equal to MIN if there is any. */
516 static inline unsigned
517 bmp_iter_and_init (bitmap_iterator *bi, bitmap bmp1, bitmap bmp2,
520 unsigned indx = min / BITMAP_ELEMENT_ALL_BITS;
522 for (bi->ptr1 = bmp1->first;
523 bi->ptr1 && bi->ptr1->indx < indx;
524 bi->ptr1 = bi->ptr1->next)
530 bi->ptr2 = bmp2->first;
536 while (bi->ptr2->indx < bi->ptr1->indx)
538 bi->ptr2 = bi->ptr2->next;
543 if (bi->ptr1->indx == bi->ptr2->indx)
546 bi->ptr1 = bi->ptr1->next;
551 if (bi->ptr1->indx == indx)
553 unsigned bit_in_elt = min - BITMAP_ELEMENT_ALL_BITS * indx;
554 unsigned word_in_elt = bit_in_elt / BITMAP_WORD_BITS;
555 unsigned bit_in_word = bit_in_elt % BITMAP_WORD_BITS;
557 bi->word = word_in_elt;
558 bi->word_bit = min - bit_in_word;
561 bi->actual = (bi->ptr1->bits[word_in_elt]
562 & bi->ptr2->bits[word_in_elt]) >> bit_in_word;
567 bi->bit = bi->ptr1->indx * BITMAP_ELEMENT_ALL_BITS;
568 bi->word_bit = bi->bit;
570 bi->actual = (bi->ptr1->bits[0] & bi->ptr2->bits[0]);
573 return bmp_iter_and_next_1 (bi);
576 /* To avoid warnings. */
586 /* Moves the iterator BI to the next bit of intersection of bitmaps and returns
587 the bit if available. */
589 static inline unsigned
590 bmp_iter_and_next (bitmap_iterator *bi)
594 return bmp_iter_and_next_1 (bi);
597 /* Loop over all bits in BMP1 and BMP2, starting with MIN, setting
598 BITNUM to the bit number for all bits that are set in both bitmaps.
599 ITER is a bitmap iterator. */
601 #define EXECUTE_IF_AND_IN_BITMAP(BMP1, BMP2, MIN, BITNUM, ITER) \
602 for ((BITNUM) = bmp_iter_and_init (&(ITER), (BMP1), (BMP2), (MIN)); \
603 !bmp_iter_end_p (&(ITER)); \
604 (BITNUM) = bmp_iter_and_next (&(ITER)))
606 #endif /* GCC_BITMAP_H */