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
;
70 extern bitmap_element bitmap_zero_bits
; /* Zero bitmap element */
72 /* Clear a bitmap by freeing up the linked list. */
73 extern void bitmap_clear (bitmap
);
75 /* Copy a bitmap to another bitmap. */
76 extern void bitmap_copy (bitmap
, bitmap
);
78 /* True if two bitmaps are identical. */
79 extern bool bitmap_equal_p (bitmap
, bitmap
);
81 /* True if the bitmaps intersect (their AND is non-empty). */
82 extern bool bitmap_intersect_p (bitmap
, bitmap
);
84 /* True if the complement of the second intersects the first (their
85 AND_COMPL is non-empty). */
86 extern bool bitmap_intersect_compl_p (bitmap
, bitmap
);
88 /* True if MAP is an empty bitmap. */
89 #define bitmap_empty_p(MAP) (!(MAP)->first)
91 /* Boolean operations on bitmaps. The _into variants are two operand
92 versions that modify the first source operand. The other variants
93 are three operand versions that to not destroy the source bitmaps.
94 The operations supported are &, & ~, |, ^. */
95 extern void bitmap_and (bitmap
, bitmap
, bitmap
);
96 extern void bitmap_and_into (bitmap
, bitmap
);
97 extern void bitmap_and_compl (bitmap
, bitmap
, bitmap
);
98 extern void bitmap_and_compl_into (bitmap
, bitmap
);
99 extern bool bitmap_ior (bitmap
, bitmap
, bitmap
);
100 extern bool bitmap_ior_into (bitmap
, bitmap
);
101 extern void bitmap_xor (bitmap
, bitmap
, bitmap
);
102 extern void bitmap_xor_into (bitmap
, bitmap
);
104 /* DST = A | (B & ~C). Return true if DST changes. */
105 extern bool bitmap_ior_and_compl (bitmap DST
, bitmap A
, bitmap B
, bitmap C
);
106 /* A |= (B & ~C). Return true if A changes. */
107 extern bool bitmap_ior_and_compl_into (bitmap DST
, bitmap B
, bitmap C
);
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 extern int bitmap_first_set_bit (bitmap
);
136 extern int bitmap_last_set_bit (bitmap
);
138 /* Allocate a bitmap with oballoc. */
139 #define BITMAP_OBSTACK_ALLOC(OBSTACK) \
140 bitmap_initialize (obstack_alloc (OBSTACK, sizeof (bitmap_head)), 1)
142 /* Allocate a bitmap with ggc_alloc. */
143 #define BITMAP_GGC_ALLOC() \
144 bitmap_initialize (NULL, 0)
146 /* Allocate a bitmap with xmalloc. */
147 #define BITMAP_XMALLOC() \
148 bitmap_initialize (xmalloc (sizeof (bitmap_head)), 1)
150 /* Do any cleanup needed on a bitmap when it is no longer used. */
151 #define BITMAP_FREE(BITMAP) \
155 bitmap_clear (BITMAP); \
160 /* Do any cleanup needed on an xmalloced bitmap when it is no longer used. */
161 #define BITMAP_XFREE(BITMAP) \
165 bitmap_clear (BITMAP); \
171 /* Do any one-time initializations needed for bitmaps. */
172 #define BITMAP_INIT_ONCE()
174 /* Iterator for bitmaps. */
178 /* Pointer to the current bitmap element. */
179 bitmap_element
*elt1
;
181 /* Pointer to 2nd bitmap element when two are involved. */
182 bitmap_element
*elt2
;
184 /* Word within the current element. */
187 /* Contents of the actually processed word. When finding next bit
188 it is shifted right, so that the actual bit is always the least
189 significant bit of ACTUAL. */
193 /* Initialize a single bitmap iterator. START_BIT is the first bit to
197 bmp_iter_set_init (bitmap_iterator
*bi
, bitmap map
,
198 unsigned start_bit
, unsigned *bit_no
)
200 bi
->elt1
= map
->first
;
203 /* Advance elt1 until it is not before the block containing start_bit. */
208 bi
->elt1
= &bitmap_zero_bits
;
212 if (bi
->elt1
->indx
>= start_bit
/ BITMAP_ELEMENT_ALL_BITS
)
214 bi
->elt1
= bi
->elt1
->next
;
217 /* We might have gone past the start bit, so reinitialize it. */
218 if (bi
->elt1
->indx
!= start_bit
/ BITMAP_ELEMENT_ALL_BITS
)
219 start_bit
= bi
->elt1
->indx
* BITMAP_ELEMENT_ALL_BITS
;
221 /* Initialize for what is now start_bit. */
222 bi
->word_no
= start_bit
/ BITMAP_WORD_BITS
% BITMAP_ELEMENT_WORDS
;
223 bi
->bits
= bi
->elt1
->bits
[bi
->word_no
];
224 bi
->bits
>>= start_bit
% BITMAP_WORD_BITS
;
226 /* If this word is zero, we must make sure we're not pointing at the
227 first bit, otherwise our incrementing to the next word boundary
228 will fail. It won't matter if this increment moves us into the
230 start_bit
+= !bi
->bits
;
235 /* Initialize an iterator to iterate over the intersection of two
236 bitmaps. START_BIT is the bit to commence from. */
239 bmp_iter_and_init (bitmap_iterator
*bi
, bitmap map1
, bitmap map2
,
240 unsigned start_bit
, unsigned *bit_no
)
242 bi
->elt1
= map1
->first
;
243 bi
->elt2
= map2
->first
;
245 /* Advance elt1 until it is not before the block containing
255 if (bi
->elt1
->indx
>= start_bit
/ BITMAP_ELEMENT_ALL_BITS
)
257 bi
->elt1
= bi
->elt1
->next
;
260 /* Advance elt2 until it is not before elt1. */
265 bi
->elt1
= bi
->elt2
= &bitmap_zero_bits
;
269 if (bi
->elt2
->indx
>= bi
->elt1
->indx
)
271 bi
->elt2
= bi
->elt2
->next
;
274 /* If we're at the same index, then we have some intersecting bits. */
275 if (bi
->elt1
->indx
== bi
->elt2
->indx
)
277 /* We might have advanced beyond the start_bit, so reinitialize
279 if (bi
->elt1
->indx
!= start_bit
/ BITMAP_ELEMENT_ALL_BITS
)
280 start_bit
= bi
->elt1
->indx
* BITMAP_ELEMENT_ALL_BITS
;
282 bi
->word_no
= start_bit
/ BITMAP_WORD_BITS
% BITMAP_ELEMENT_WORDS
;
283 bi
->bits
= bi
->elt1
->bits
[bi
->word_no
] & bi
->elt2
->bits
[bi
->word_no
];
284 bi
->bits
>>= start_bit
% BITMAP_WORD_BITS
;
288 /* Otherwise we must immediately advance elt1, so initialize for
290 bi
->word_no
= BITMAP_ELEMENT_WORDS
- 1;
294 /* If this word is zero, we must make sure we're not pointing at the
295 first bit, otherwise our incrementing to the next word boundary
296 will fail. It won't matter if this increment moves us into the
298 start_bit
+= !bi
->bits
;
303 /* Initialize an iterator to iterate over the bits in MAP1 & ~MAP2.
307 bmp_iter_and_compl_init (bitmap_iterator
*bi
, bitmap map1
, bitmap map2
,
308 unsigned start_bit
, unsigned *bit_no
)
310 bi
->elt1
= map1
->first
;
311 bi
->elt2
= map2
->first
;
313 /* Advance elt1 until it is not before the block containing start_bit. */
318 bi
->elt1
= &bitmap_zero_bits
;
322 if (bi
->elt1
->indx
>= start_bit
/ BITMAP_ELEMENT_ALL_BITS
)
324 bi
->elt1
= bi
->elt1
->next
;
327 /* Advance elt2 until it is not before elt1. */
328 while (bi
->elt2
&& bi
->elt2
->indx
< bi
->elt1
->indx
)
329 bi
->elt2
= bi
->elt2
->next
;
331 /* We might have advanced beyond the start_bit, so reinitialize for
333 if (bi
->elt1
->indx
!= start_bit
/ BITMAP_ELEMENT_ALL_BITS
)
334 start_bit
= bi
->elt1
->indx
* BITMAP_ELEMENT_ALL_BITS
;
336 bi
->word_no
= start_bit
/ BITMAP_WORD_BITS
% BITMAP_ELEMENT_WORDS
;
337 bi
->bits
= bi
->elt1
->bits
[bi
->word_no
];
338 if (bi
->elt2
&& bi
->elt1
->indx
== bi
->elt2
->indx
)
339 bi
->bits
&= ~bi
->elt2
->bits
[bi
->word_no
];
340 bi
->bits
>>= start_bit
% BITMAP_WORD_BITS
;
342 /* If this word is zero, we must make sure we're not pointing at the
343 first bit, otherwise our incrementing to the next word boundary
344 will fail. It won't matter if this increment moves us into the
346 start_bit
+= !bi
->bits
;
351 /* Advance to the next bit in BI. We don't advance to the next
355 bmp_iter_next (bitmap_iterator
*bi
, unsigned *bit_no
)
361 /* Advance to the next nonzero bit of a single bitmap, we will have
362 already advanced past the just iterated bit. Return true if there
363 is a bit to iterate. */
366 bmp_iter_set (bitmap_iterator
*bi
, unsigned *bit_no
)
368 /* If our current word is nonzero, it contains the bit we want. */
372 while (!(bi
->bits
& 1))
380 /* Round up to the word boundary. We might have just iterated past
381 the end of the last word, hence the -1. It is not possible for
382 bit_no to point at the beginning of the now last word. */
383 *bit_no
= ((*bit_no
+ BITMAP_WORD_BITS
- 1)
384 / BITMAP_WORD_BITS
* BITMAP_WORD_BITS
);
389 /* Find the next nonzero word in this elt. */
390 while (bi
->word_no
!= BITMAP_ELEMENT_WORDS
)
392 bi
->bits
= bi
->elt1
->bits
[bi
->word_no
];
395 *bit_no
+= BITMAP_WORD_BITS
;
399 /* Advance to the next element. */
400 bi
->elt1
= bi
->elt1
->next
;
403 *bit_no
= bi
->elt1
->indx
* BITMAP_ELEMENT_ALL_BITS
;
408 /* Advance to the next nonzero bit of an intersecting pair of
409 bitmaps. We will have already advanced past the just iterated bit.
410 Return true if there is a bit to iterate. */
413 bmp_iter_and (bitmap_iterator
*bi
, unsigned *bit_no
)
415 /* If our current word is nonzero, it contains the bit we want. */
419 while (!(bi
->bits
& 1))
427 /* Round up to the word boundary. We might have just iterated past
428 the end of the last word, hence the -1. It is not possible for
429 bit_no to point at the beginning of the now last word. */
430 *bit_no
= ((*bit_no
+ BITMAP_WORD_BITS
- 1)
431 / BITMAP_WORD_BITS
* BITMAP_WORD_BITS
);
436 /* Find the next nonzero word in this elt. */
437 while (bi
->word_no
!= BITMAP_ELEMENT_WORDS
)
439 bi
->bits
= bi
->elt1
->bits
[bi
->word_no
] & bi
->elt2
->bits
[bi
->word_no
];
442 *bit_no
+= BITMAP_WORD_BITS
;
446 /* Advance to the next identical element. */
449 /* Advance elt1 while it is less than elt2. We always want
450 to advance one elt. */
453 bi
->elt1
= bi
->elt1
->next
;
457 while (bi
->elt1
->indx
< bi
->elt2
->indx
);
459 /* Advance elt2 to be no less than elt1. This might not
461 while (bi
->elt2
->indx
< bi
->elt1
->indx
)
463 bi
->elt2
= bi
->elt2
->next
;
468 while (bi
->elt1
->indx
!= bi
->elt2
->indx
);
470 *bit_no
= bi
->elt1
->indx
* BITMAP_ELEMENT_ALL_BITS
;
475 /* Advance to the next nonzero bit in the intersection of
476 complemented bitmaps. We will have already advanced past the just
480 bmp_iter_and_compl (bitmap_iterator
*bi
, unsigned *bit_no
)
482 /* If our current word is nonzero, it contains the bit we want. */
486 while (!(bi
->bits
& 1))
494 /* Round up to the word boundary. We might have just iterated past
495 the end of the last word, hence the -1. It is not possible for
496 bit_no to point at the beginning of the now last word. */
497 *bit_no
= ((*bit_no
+ BITMAP_WORD_BITS
- 1)
498 / BITMAP_WORD_BITS
* BITMAP_WORD_BITS
);
503 /* Find the next nonzero word in this elt. */
504 while (bi
->word_no
!= BITMAP_ELEMENT_WORDS
)
506 bi
->bits
= bi
->elt1
->bits
[bi
->word_no
];
507 if (bi
->elt2
&& bi
->elt2
->indx
== bi
->elt1
->indx
)
508 bi
->bits
&= ~bi
->elt2
->bits
[bi
->word_no
];
511 *bit_no
+= BITMAP_WORD_BITS
;
515 /* Advance to the next element of elt1. */
516 bi
->elt1
= bi
->elt1
->next
;
520 /* Advance elt2 until it is no less than elt1. */
521 while (bi
->elt2
&& bi
->elt2
->indx
< bi
->elt1
->indx
)
522 bi
->elt2
= bi
->elt2
->next
;
524 *bit_no
= bi
->elt1
->indx
* BITMAP_ELEMENT_ALL_BITS
;
529 /* Loop over all bits set in BITMAP, starting with MIN and setting
530 BITNUM to the bit number. ITER is a bitmap iterator. BITNUM
531 should be treated as a read-only variable as it contains loop
534 #define EXECUTE_IF_SET_IN_BITMAP(BITMAP, MIN, BITNUM, ITER) \
535 for (bmp_iter_set_init (&(ITER), (BITMAP), (MIN), &(BITNUM)); \
536 bmp_iter_set (&(ITER), &(BITNUM)); \
537 bmp_iter_next (&(ITER), &(BITNUM)))
539 /* Loop over all the bits set in BITMAP1 & BITMAP2, starting with MIN
540 and setting BITNUM to the bit number. ITER is a bitmap iterator.
541 BITNUM should be treated as a read-only variable as it contains
544 #define EXECUTE_IF_AND_IN_BITMAP(BITMAP1, BITMAP2, MIN, BITNUM, ITER) \
545 for (bmp_iter_and_init (&(ITER), (BITMAP1), (BITMAP2), (MIN), \
547 bmp_iter_and (&(ITER), &(BITNUM)); \
548 bmp_iter_next (&(ITER), &(BITNUM)))
550 /* Loop over all the bits set in BITMAP1 & ~BITMAP2, starting with MIN
551 and setting BITNUM to the bit number. ITER is a bitmap iterator.
552 BITNUM should be treated as a read-only variable as it contains
555 #define EXECUTE_IF_AND_COMPL_IN_BITMAP(BITMAP1, BITMAP2, MIN, BITNUM, ITER) \
556 for (bmp_iter_and_compl_init (&(ITER), (BITMAP1), (BITMAP2), (MIN), \
558 bmp_iter_and_compl (&(ITER), &(BITNUM)); \
559 bmp_iter_next (&(ITER), &(BITNUM)))
561 #endif /* GCC_BITMAP_H */