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 extern int bitmap_first_set_bit (bitmap);
145 extern int bitmap_last_set_bit (bitmap);
147 /* Allocate a bitmap with oballoc. */
148 #define BITMAP_OBSTACK_ALLOC(OBSTACK) \
149 bitmap_initialize (obstack_alloc (OBSTACK, sizeof (bitmap_head)), 1)
151 /* Allocate a bitmap with ggc_alloc. */
152 #define BITMAP_GGC_ALLOC() \
153 bitmap_initialize (NULL, 0)
155 /* Allocate a bitmap with xmalloc. */
156 #define BITMAP_XMALLOC() \
157 bitmap_initialize (xmalloc (sizeof (bitmap_head)), 1)
159 /* Do any cleanup needed on a bitmap when it is no longer used. */
160 #define BITMAP_FREE(BITMAP) \
164 bitmap_clear (BITMAP); \
169 /* Do any cleanup needed on an xmalloced bitmap when it is no longer used. */
170 #define BITMAP_XFREE(BITMAP) \
174 bitmap_clear (BITMAP); \
180 /* Do any one-time initializations needed for bitmaps. */
181 #define BITMAP_INIT_ONCE()
183 /* Iterator for bitmaps. */
187 /* Actual elements in the bitmaps. */
188 bitmap_element *ptr1, *ptr2;
190 /* Position of an actual word in the elements. */
193 /* Position of a bit corresponding to the start of word. */
196 /* Position of the actual bit. */
199 /* Contents of the actually processed word. When finding next bit
200 it is shifted right, so that the actual bit is always the least
201 significant bit of ACTUAL. */
205 /* Moves the iterator BI to the first set bit on or after the current
206 position in bitmap and returns the bit if available. The bit is
207 found in ACTUAL field only. */
209 static inline unsigned
210 bmp_iter_common_next_1 (bitmap_iterator *bi)
212 while (!(bi->actual & 1))
221 /* Moves the iterator BI to the first set bit on or after the current
222 position in bitmap and returns the bit if available. */
224 static inline unsigned
225 bmp_iter_single_next_1 (bitmap_iterator *bi)
228 return bmp_iter_common_next_1 (bi);
231 bi->word_bit += BITMAP_WORD_BITS;
236 bi->word < BITMAP_ELEMENT_WORDS;
237 bi->word++, bi->word_bit += BITMAP_WORD_BITS)
239 bi->actual = bi->ptr1->bits[bi->word];
242 bi->bit = bi->word_bit;
243 return bmp_iter_common_next_1 (bi);
247 bi->ptr1 = bi->ptr1->next;
252 bi->word_bit = bi->ptr1->indx * BITMAP_ELEMENT_ALL_BITS;
256 /* Initializes a bitmap iterator BI for looping over bits of bitmap
257 BMP, starting with bit MIN. Returns the first bit of BMP greater
258 or equal to MIN if there is any. */
260 static inline unsigned
261 bmp_iter_single_init (bitmap_iterator *bi, bitmap bmp, unsigned min)
263 unsigned indx = min / BITMAP_ELEMENT_ALL_BITS;
265 for (bi->ptr1 = bmp->first;
266 bi->ptr1 && bi->ptr1->indx < indx;
267 bi->ptr1 = bi->ptr1->next)
272 /* To avoid warnings. */
281 if (bi->ptr1->indx == indx)
283 unsigned bit_in_elt = min - BITMAP_ELEMENT_ALL_BITS * indx;
284 unsigned word_in_elt = bit_in_elt / BITMAP_WORD_BITS;
285 unsigned bit_in_word = bit_in_elt % BITMAP_WORD_BITS;
287 bi->word = word_in_elt;
288 bi->word_bit = min - bit_in_word;
290 bi->actual = bi->ptr1->bits[word_in_elt] >> bit_in_word;
295 bi->bit = bi->ptr1->indx * BITMAP_ELEMENT_ALL_BITS;
296 bi->word_bit = bi->bit;
297 bi->actual = bi->ptr1->bits[0];
300 return bmp_iter_single_next_1 (bi);
303 /* Returns true if all elements of the bitmap referred to by iterator BI
307 bmp_iter_end_p (const bitmap_iterator *bi)
309 return bi->ptr1 == NULL;
312 /* Moves the iterator BI to the next bit of bitmap and returns the bit
315 static inline unsigned
316 bmp_iter_single_next (bitmap_iterator *bi)
320 return bmp_iter_single_next_1 (bi);
323 /* Loop over all bits in BITMAP, starting with MIN and setting BITNUM to
324 the bit number. ITER is a bitmap iterator. */
326 #define EXECUTE_IF_SET_IN_BITMAP(BITMAP, MIN, BITNUM, ITER) \
327 for ((BITNUM) = bmp_iter_single_init (&(ITER), (BITMAP), (MIN)); \
328 !bmp_iter_end_p (&(ITER)); \
329 (BITNUM) = bmp_iter_single_next (&(ITER)))
331 /* Moves the iterator BI to the first set bit on or after the current
332 position in difference of bitmaps and returns the bit if available. */
334 static inline unsigned
335 bmp_iter_and_not_next_1 (bitmap_iterator *bi)
338 return bmp_iter_common_next_1 (bi);
341 bi->word_bit += BITMAP_WORD_BITS;
347 if (bi->ptr2 && bi->ptr2->indx == bi->ptr1->indx)
350 snd = &bitmap_zero_bits;
353 bi->word < BITMAP_ELEMENT_WORDS;
354 bi->word++, bi->word_bit += BITMAP_WORD_BITS)
356 bi->actual = (bi->ptr1->bits[bi->word]
357 & ~snd->bits[bi->word]);
360 bi->bit = bi->word_bit;
361 return bmp_iter_common_next_1 (bi);
365 bi->ptr1 = bi->ptr1->next;
370 && bi->ptr2->indx < bi->ptr1->indx)
371 bi->ptr2 = bi->ptr2->next;
374 bi->word_bit = bi->ptr1->indx * BITMAP_ELEMENT_ALL_BITS;
378 /* Initializes a bitmap iterator BI for looping over bits of bitmap
379 BMP1 &~ BMP2, starting with bit MIN. Returns the first bit of
380 BMP1 &~ BMP2 greater or equal to MIN if there is any. */
382 static inline unsigned
383 bmp_iter_and_not_init (bitmap_iterator *bi, bitmap bmp1, bitmap bmp2,
386 unsigned indx = min / BITMAP_ELEMENT_ALL_BITS;
388 for (bi->ptr1 = bmp1->first;
389 bi->ptr1 && bi->ptr1->indx < indx;
390 bi->ptr1 = bi->ptr1->next)
395 /* To avoid warnings. */
404 for (bi->ptr2 = bmp2->first;
405 bi->ptr2 && bi->ptr2->indx < bi->ptr1->indx;
406 bi->ptr2 = bi->ptr2->next)
409 if (bi->ptr1->indx == indx)
411 unsigned bit_in_elt = min - BITMAP_ELEMENT_ALL_BITS * indx;
412 unsigned word_in_elt = bit_in_elt / BITMAP_WORD_BITS;
413 unsigned bit_in_word = bit_in_elt % BITMAP_WORD_BITS;
415 bi->word = word_in_elt;
416 bi->word_bit = min - bit_in_word;
419 if (bi->ptr2 && bi->ptr2->indx == indx)
420 bi->actual = (bi->ptr1->bits[word_in_elt]
421 & ~bi->ptr2->bits[word_in_elt]) >> bit_in_word;
423 bi->actual = bi->ptr1->bits[word_in_elt] >> bit_in_word;
428 bi->bit = bi->ptr1->indx * BITMAP_ELEMENT_ALL_BITS;
429 bi->word_bit = bi->bit;
431 if (bi->ptr2 && bi->ptr2->indx == bi->ptr1->indx)
432 bi->actual = (bi->ptr1->bits[0] & ~bi->ptr2->bits[0]);
434 bi->actual = bi->ptr1->bits[0];
437 return bmp_iter_and_not_next_1 (bi);
440 /* Moves the iterator BI to the next bit of difference of bitmaps and returns
441 the bit if available. */
443 static inline unsigned
444 bmp_iter_and_not_next (bitmap_iterator *bi)
448 return bmp_iter_and_not_next_1 (bi);
451 /* Loop over all bits in BMP1 and BMP2, starting with MIN, setting
452 BITNUM to the bit number for all bits that are set in the first bitmap
453 and not set in the second. ITER is a bitmap iterator. */
455 #define EXECUTE_IF_AND_COMPL_IN_BITMAP(BMP1, BMP2, MIN, BITNUM, ITER) \
456 for ((BITNUM) = bmp_iter_and_not_init (&(ITER), (BMP1), (BMP2), (MIN)); \
457 !bmp_iter_end_p (&(ITER)); \
458 (BITNUM) = bmp_iter_and_not_next (&(ITER)))
460 /* Moves the iterator BI to the first set bit on or after the current
461 position in intersection of bitmaps and returns the bit if available. */
463 static inline unsigned
464 bmp_iter_and_next_1 (bitmap_iterator *bi)
467 return bmp_iter_common_next_1 (bi);
470 bi->word_bit += BITMAP_WORD_BITS;
475 bi->word < BITMAP_ELEMENT_WORDS;
476 bi->word++, bi->word_bit += BITMAP_WORD_BITS)
478 bi->actual = (bi->ptr1->bits[bi->word]
479 & bi->ptr2->bits[bi->word]);
482 bi->bit = bi->word_bit;
483 return bmp_iter_common_next_1 (bi);
489 bi->ptr1 = bi->ptr1->next;
493 while (bi->ptr2->indx < bi->ptr1->indx)
495 bi->ptr2 = bi->ptr2->next;
503 while (bi->ptr1->indx != bi->ptr2->indx);
506 bi->word_bit = bi->ptr1->indx * BITMAP_ELEMENT_ALL_BITS;
510 /* Initializes a bitmap iterator BI for looping over bits of bitmap
511 BMP1 & BMP2, starting with bit MIN. Returns the first bit of
512 BMP1 & BMP2 greater or equal to MIN if there is any. */
514 static inline unsigned
515 bmp_iter_and_init (bitmap_iterator *bi, bitmap bmp1, bitmap bmp2,
518 unsigned indx = min / BITMAP_ELEMENT_ALL_BITS;
520 for (bi->ptr1 = bmp1->first;
521 bi->ptr1 && bi->ptr1->indx < indx;
522 bi->ptr1 = bi->ptr1->next)
528 bi->ptr2 = bmp2->first;
534 while (bi->ptr2->indx < bi->ptr1->indx)
536 bi->ptr2 = bi->ptr2->next;
541 if (bi->ptr1->indx == bi->ptr2->indx)
544 bi->ptr1 = bi->ptr1->next;
549 if (bi->ptr1->indx == indx)
551 unsigned bit_in_elt = min - BITMAP_ELEMENT_ALL_BITS * indx;
552 unsigned word_in_elt = bit_in_elt / BITMAP_WORD_BITS;
553 unsigned bit_in_word = bit_in_elt % BITMAP_WORD_BITS;
555 bi->word = word_in_elt;
556 bi->word_bit = min - bit_in_word;
559 bi->actual = (bi->ptr1->bits[word_in_elt]
560 & bi->ptr2->bits[word_in_elt]) >> bit_in_word;
565 bi->bit = bi->ptr1->indx * BITMAP_ELEMENT_ALL_BITS;
566 bi->word_bit = bi->bit;
568 bi->actual = (bi->ptr1->bits[0] & bi->ptr2->bits[0]);
571 return bmp_iter_and_next_1 (bi);
574 /* To avoid warnings. */
584 /* Moves the iterator BI to the next bit of intersection of bitmaps and returns
585 the bit if available. */
587 static inline unsigned
588 bmp_iter_and_next (bitmap_iterator *bi)
592 return bmp_iter_and_next_1 (bi);
595 /* Loop over all bits in BMP1 and BMP2, starting with MIN, setting
596 BITNUM to the bit number for all bits that are set in both bitmaps.
597 ITER is a bitmap iterator. */
599 #define EXECUTE_IF_AND_IN_BITMAP(BMP1, BMP2, MIN, BITNUM, ITER) \
600 for ((BITNUM) = bmp_iter_and_init (&(ITER), (BMP1), (BMP2), (MIN)); \
601 !bmp_iter_end_p (&(ITER)); \
602 (BITNUM) = bmp_iter_and_next (&(ITER)))
604 #endif /* GCC_BITMAP_H */