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 int bitmap_equal_p (bitmap, bitmap);
90 #define bitmap_empty_p(MAP) (!(MAP)->first)
92 /* Perform an operation on two bitmaps, yielding a third. */
93 extern int bitmap_operation (bitmap, bitmap, bitmap, enum bitmap_bits);
95 #define bitmap_and(DST,A,B) bitmap_operation (DST,A,B,BITMAP_AND)
96 #define bitmap_and_into(DST_SRC,B) bitmap_operation (DST_SRC,DST_SRC,B,BITMAP_AND)
97 #define bitmap_and_compl(DST,A,B) bitmap_operation (DST,A,B,BITMAP_AND_COMPL)
98 #define bitmap_and_compl_into(DST_SRC,B) bitmap_operation (DST_SRC,DST_SRC,B,BITMAP_AND_COMPL)
99 #define bitmap_ior(DST,A,B) bitmap_operation (DST,A,B,BITMAP_IOR)
100 #define bitmap_ior_into(DST_SRC,B) bitmap_operation (DST_SRC,DST_SRC,B,BITMAP_IOR)
101 #define bitmap_ior_compl(DST,A,B) bitmap_operation (DST,A,B,BITMAP_IOR_COMPL)
102 #define bitmap_xor(DST,A,B) bitmap_operation (DST,A,B,BITMAP_XOR)
103 #define bitmap_xor_into(DST_SRC,B) bitmap_operation (DST_SRC,DST_SRC,B,BITMAP_XOR)
105 /* `or' into one bitmap the `and' of a second bitmap witih the complement
106 of a third. Return nonzero if the bitmap changes. */
107 extern int bitmap_ior_and_compl_into (bitmap, bitmap, bitmap);
109 /* Clear a single register in a register set. */
110 extern void bitmap_clear_bit (bitmap, int);
112 /* Set a single register in a register set. */
113 extern void bitmap_set_bit (bitmap, int);
115 /* Return true if a register is set in a register set. */
116 extern int bitmap_bit_p (bitmap, int);
118 /* Debug functions to print a bitmap linked list. */
119 extern void debug_bitmap (bitmap);
120 extern void debug_bitmap_file (FILE *, bitmap);
122 /* Print a bitmap. */
123 extern void bitmap_print (FILE *, bitmap, const char *, const char *);
125 /* Initialize a bitmap header. If HEAD is NULL, a new header will be
126 allocated. USING_OBSTACK indicates how elements should be allocated. */
127 extern bitmap bitmap_initialize (bitmap head, int using_obstack);
129 /* Release all memory used by the bitmap obstack. */
130 extern void bitmap_release_memory (void);
132 /* A few compatibility/functions macros for compatibility with sbitmaps */
133 #define dump_bitmap(file, bitmap) bitmap_print (file, bitmap, "", "\n")
134 #define bitmap_zero(a) bitmap_clear (a)
135 #define bitmap_a_or_b(a,b,c) bitmap_operation (a, b, c, BITMAP_IOR)
136 #define bitmap_a_and_b(a,b,c) bitmap_operation (a, b, c, BITMAP_AND)
137 extern int bitmap_union_of_diff (bitmap, bitmap, bitmap, bitmap);
138 extern int bitmap_first_set_bit (bitmap);
139 extern int bitmap_last_set_bit (bitmap);
141 /* Allocate a bitmap with oballoc. */
142 #define BITMAP_OBSTACK_ALLOC(OBSTACK) \
143 bitmap_initialize (obstack_alloc (OBSTACK, sizeof (bitmap_head)), 1)
145 /* Allocate a bitmap with ggc_alloc. */
146 #define BITMAP_GGC_ALLOC() \
147 bitmap_initialize (NULL, 0)
149 /* Allocate a bitmap with xmalloc. */
150 #define BITMAP_XMALLOC() \
151 bitmap_initialize (xmalloc (sizeof (bitmap_head)), 1)
153 /* Do any cleanup needed on a bitmap when it is no longer used. */
154 #define BITMAP_FREE(BITMAP) \
158 bitmap_clear (BITMAP); \
163 /* Do any cleanup needed on an xmalloced bitmap when it is no longer used. */
164 #define BITMAP_XFREE(BITMAP) \
168 bitmap_clear (BITMAP); \
174 /* Do any one-time initializations needed for bitmaps. */
175 #define BITMAP_INIT_ONCE()
177 /* Iterator for bitmaps. */
181 /* Actual elements in the bitmaps. */
182 bitmap_element *ptr1, *ptr2;
184 /* Position of an actual word in the elements. */
187 /* Position of a bit corresponding to the start of word. */
190 /* Position of the actual bit. */
193 /* Contents of the actually processed word. When finding next bit
194 it is shifted right, so that the actual bit is always the least
195 significant bit of ACTUAL. */
199 /* Moves the iterator BI to the first set bit on or after the current
200 position in bitmap and returns the bit if available. The bit is
201 found in ACTUAL field only. */
203 static inline unsigned
204 bmp_iter_common_next_1 (bitmap_iterator *bi)
206 while (!(bi->actual & 1))
215 /* Moves the iterator BI to the first set bit on or after the current
216 position in bitmap and returns the bit if available. */
218 static inline unsigned
219 bmp_iter_single_next_1 (bitmap_iterator *bi)
222 return bmp_iter_common_next_1 (bi);
225 bi->word_bit += BITMAP_WORD_BITS;
230 bi->word < BITMAP_ELEMENT_WORDS;
231 bi->word++, bi->word_bit += BITMAP_WORD_BITS)
233 bi->actual = bi->ptr1->bits[bi->word];
236 bi->bit = bi->word_bit;
237 return bmp_iter_common_next_1 (bi);
241 bi->ptr1 = bi->ptr1->next;
246 bi->word_bit = bi->ptr1->indx * BITMAP_ELEMENT_ALL_BITS;
250 /* Initializes a bitmap iterator BI for looping over bits of bitmap
251 BMP, starting with bit MIN. Returns the first bit of BMP greater
252 or equal to MIN if there is any. */
254 static inline unsigned
255 bmp_iter_single_init (bitmap_iterator *bi, bitmap bmp, unsigned min)
257 unsigned indx = min / BITMAP_ELEMENT_ALL_BITS;
259 for (bi->ptr1 = bmp->first;
260 bi->ptr1 && bi->ptr1->indx < indx;
261 bi->ptr1 = bi->ptr1->next)
266 /* To avoid warnings. */
275 if (bi->ptr1->indx == indx)
277 unsigned bit_in_elt = min - BITMAP_ELEMENT_ALL_BITS * indx;
278 unsigned word_in_elt = bit_in_elt / BITMAP_WORD_BITS;
279 unsigned bit_in_word = bit_in_elt % BITMAP_WORD_BITS;
281 bi->word = word_in_elt;
282 bi->word_bit = min - bit_in_word;
284 bi->actual = bi->ptr1->bits[word_in_elt] >> bit_in_word;
289 bi->bit = bi->ptr1->indx * BITMAP_ELEMENT_ALL_BITS;
290 bi->word_bit = bi->bit;
291 bi->actual = bi->ptr1->bits[0];
294 return bmp_iter_single_next_1 (bi);
297 /* Returns true if all elements of the bitmap referred to by iterator BI
301 bmp_iter_end_p (const bitmap_iterator *bi)
303 return bi->ptr1 == NULL;
306 /* Moves the iterator BI to the next bit of bitmap and returns the bit
309 static inline unsigned
310 bmp_iter_single_next (bitmap_iterator *bi)
314 return bmp_iter_single_next_1 (bi);
317 /* Loop over all bits in BITMAP, starting with MIN and setting BITNUM to
318 the bit number. ITER is a bitmap iterator. */
320 #define EXECUTE_IF_SET_IN_BITMAP(BITMAP, MIN, BITNUM, ITER) \
321 for ((BITNUM) = bmp_iter_single_init (&(ITER), (BITMAP), (MIN)); \
322 !bmp_iter_end_p (&(ITER)); \
323 (BITNUM) = bmp_iter_single_next (&(ITER)))
325 /* Moves the iterator BI to the first set bit on or after the current
326 position in difference of bitmaps and returns the bit if available. */
328 static inline unsigned
329 bmp_iter_and_not_next_1 (bitmap_iterator *bi)
332 return bmp_iter_common_next_1 (bi);
335 bi->word_bit += BITMAP_WORD_BITS;
341 if (bi->ptr2 && bi->ptr2->indx == bi->ptr1->indx)
344 snd = &bitmap_zero_bits;
347 bi->word < BITMAP_ELEMENT_WORDS;
348 bi->word++, bi->word_bit += BITMAP_WORD_BITS)
350 bi->actual = (bi->ptr1->bits[bi->word]
351 & ~snd->bits[bi->word]);
354 bi->bit = bi->word_bit;
355 return bmp_iter_common_next_1 (bi);
359 bi->ptr1 = bi->ptr1->next;
364 && bi->ptr2->indx < bi->ptr1->indx)
365 bi->ptr2 = bi->ptr2->next;
368 bi->word_bit = bi->ptr1->indx * BITMAP_ELEMENT_ALL_BITS;
372 /* Initializes a bitmap iterator BI for looping over bits of bitmap
373 BMP1 &~ BMP2, starting with bit MIN. Returns the first bit of
374 BMP1 &~ BMP2 greater or equal to MIN if there is any. */
376 static inline unsigned
377 bmp_iter_and_not_init (bitmap_iterator *bi, bitmap bmp1, bitmap bmp2,
380 unsigned indx = min / BITMAP_ELEMENT_ALL_BITS;
382 for (bi->ptr1 = bmp1->first;
383 bi->ptr1 && bi->ptr1->indx < indx;
384 bi->ptr1 = bi->ptr1->next)
389 /* To avoid warnings. */
398 for (bi->ptr2 = bmp2->first;
399 bi->ptr2 && bi->ptr2->indx < bi->ptr1->indx;
400 bi->ptr2 = bi->ptr2->next)
403 if (bi->ptr1->indx == indx)
405 unsigned bit_in_elt = min - BITMAP_ELEMENT_ALL_BITS * indx;
406 unsigned word_in_elt = bit_in_elt / BITMAP_WORD_BITS;
407 unsigned bit_in_word = bit_in_elt % BITMAP_WORD_BITS;
409 bi->word = word_in_elt;
410 bi->word_bit = min - bit_in_word;
413 if (bi->ptr2 && bi->ptr2->indx == indx)
414 bi->actual = (bi->ptr1->bits[word_in_elt]
415 & ~bi->ptr2->bits[word_in_elt]) >> bit_in_word;
417 bi->actual = bi->ptr1->bits[word_in_elt] >> bit_in_word;
422 bi->bit = bi->ptr1->indx * BITMAP_ELEMENT_ALL_BITS;
423 bi->word_bit = bi->bit;
425 if (bi->ptr2 && bi->ptr2->indx == bi->ptr1->indx)
426 bi->actual = (bi->ptr1->bits[0] & ~bi->ptr2->bits[0]);
428 bi->actual = bi->ptr1->bits[0];
431 return bmp_iter_and_not_next_1 (bi);
434 /* Moves the iterator BI to the next bit of difference of bitmaps and returns
435 the bit if available. */
437 static inline unsigned
438 bmp_iter_and_not_next (bitmap_iterator *bi)
442 return bmp_iter_and_not_next_1 (bi);
445 /* Loop over all bits in BMP1 and BMP2, starting with MIN, setting
446 BITNUM to the bit number for all bits that are set in the first bitmap
447 and not set in the second. ITER is a bitmap iterator. */
449 #define EXECUTE_IF_AND_COMPL_IN_BITMAP(BMP1, BMP2, MIN, BITNUM, ITER) \
450 for ((BITNUM) = bmp_iter_and_not_init (&(ITER), (BMP1), (BMP2), (MIN)); \
451 !bmp_iter_end_p (&(ITER)); \
452 (BITNUM) = bmp_iter_and_not_next (&(ITER)))
454 /* Moves the iterator BI to the first set bit on or after the current
455 position in intersection of bitmaps and returns the bit if available. */
457 static inline unsigned
458 bmp_iter_and_next_1 (bitmap_iterator *bi)
461 return bmp_iter_common_next_1 (bi);
464 bi->word_bit += BITMAP_WORD_BITS;
469 bi->word < BITMAP_ELEMENT_WORDS;
470 bi->word++, bi->word_bit += BITMAP_WORD_BITS)
472 bi->actual = (bi->ptr1->bits[bi->word]
473 & bi->ptr2->bits[bi->word]);
476 bi->bit = bi->word_bit;
477 return bmp_iter_common_next_1 (bi);
483 bi->ptr1 = bi->ptr1->next;
487 while (bi->ptr2->indx < bi->ptr1->indx)
489 bi->ptr2 = bi->ptr2->next;
497 while (bi->ptr1->indx != bi->ptr2->indx);
500 bi->word_bit = bi->ptr1->indx * BITMAP_ELEMENT_ALL_BITS;
504 /* Initializes a bitmap iterator BI for looping over bits of bitmap
505 BMP1 & BMP2, starting with bit MIN. Returns the first bit of
506 BMP1 & BMP2 greater or equal to MIN if there is any. */
508 static inline unsigned
509 bmp_iter_and_init (bitmap_iterator *bi, bitmap bmp1, bitmap bmp2,
512 unsigned indx = min / BITMAP_ELEMENT_ALL_BITS;
514 for (bi->ptr1 = bmp1->first;
515 bi->ptr1 && bi->ptr1->indx < indx;
516 bi->ptr1 = bi->ptr1->next)
522 bi->ptr2 = bmp2->first;
528 while (bi->ptr2->indx < bi->ptr1->indx)
530 bi->ptr2 = bi->ptr2->next;
535 if (bi->ptr1->indx == bi->ptr2->indx)
538 bi->ptr1 = bi->ptr1->next;
543 if (bi->ptr1->indx == indx)
545 unsigned bit_in_elt = min - BITMAP_ELEMENT_ALL_BITS * indx;
546 unsigned word_in_elt = bit_in_elt / BITMAP_WORD_BITS;
547 unsigned bit_in_word = bit_in_elt % BITMAP_WORD_BITS;
549 bi->word = word_in_elt;
550 bi->word_bit = min - bit_in_word;
553 bi->actual = (bi->ptr1->bits[word_in_elt]
554 & bi->ptr2->bits[word_in_elt]) >> bit_in_word;
559 bi->bit = bi->ptr1->indx * BITMAP_ELEMENT_ALL_BITS;
560 bi->word_bit = bi->bit;
562 bi->actual = (bi->ptr1->bits[0] & bi->ptr2->bits[0]);
565 return bmp_iter_and_next_1 (bi);
568 /* To avoid warnings. */
578 /* Moves the iterator BI to the next bit of intersection of bitmaps and returns
579 the bit if available. */
581 static inline unsigned
582 bmp_iter_and_next (bitmap_iterator *bi)
586 return bmp_iter_and_next_1 (bi);
589 /* Loop over all bits in BMP1 and BMP2, starting with MIN, setting
590 BITNUM to the bit number for all bits that are set in both bitmaps.
591 ITER is a bitmap iterator. */
593 #define EXECUTE_IF_AND_IN_BITMAP(BMP1, BMP2, MIN, BITNUM, ITER) \
594 for ((BITNUM) = bmp_iter_and_init (&(ITER), (BMP1), (BMP2), (MIN)); \
595 !bmp_iter_end_p (&(ITER)); \
596 (BITNUM) = bmp_iter_and_next (&(ITER)))
598 #endif /* GCC_BITMAP_H */