1 /* Functions to support general ended bitmaps.
2 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007
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 3, 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 COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
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 /* Obstack for allocating bitmaps and elements from. */
43 typedef struct bitmap_obstack
GTY (())
45 struct bitmap_element_def
*elements
;
46 struct bitmap_head_def
*heads
;
47 struct obstack
GTY ((skip
)) obstack
;
50 /* Bitmap set element. We use a linked list to hold only the bits that
51 are set. This allows for use to grow the bitset dynamically without
52 having to realloc and copy a giant bit array.
54 The free list is implemented as a list of lists. There is one
55 outer list connected together by prev fields. Each element of that
56 outer is an inner list (that may consist only of the outer list
57 element) that are connected by the next fields. The prev pointer
58 is undefined for interior elements. This allows
59 bitmap_elt_clear_from to be implemented in unit time rather than
60 linear in the number of elements to be freed. */
62 typedef struct bitmap_element_def
GTY(())
64 struct bitmap_element_def
*next
; /* Next element. */
65 struct bitmap_element_def
*prev
; /* Previous element. */
66 unsigned int indx
; /* regno/BITMAP_ELEMENT_ALL_BITS. */
67 BITMAP_WORD bits
[BITMAP_ELEMENT_WORDS
]; /* Bits that are set. */
70 /* Head of bitmap linked list. */
71 typedef struct bitmap_head_def
GTY(()) {
72 bitmap_element
*first
; /* First element in linked list. */
73 bitmap_element
*current
; /* Last element looked at. */
74 unsigned int indx
; /* Index of last element looked at. */
75 bitmap_obstack
*obstack
; /* Obstack to allocate elements from.
76 If NULL, then use ggc_alloc. */
81 extern bitmap_element bitmap_zero_bits
; /* Zero bitmap element */
82 extern bitmap_obstack bitmap_default_obstack
; /* Default bitmap obstack */
84 /* Clear a bitmap by freeing up the linked list. */
85 extern void bitmap_clear (bitmap
);
87 /* Copy a bitmap to another bitmap. */
88 extern void bitmap_copy (bitmap
, bitmap
);
90 /* True if two bitmaps are identical. */
91 extern bool bitmap_equal_p (bitmap
, bitmap
);
93 /* True if the bitmaps intersect (their AND is non-empty). */
94 extern bool bitmap_intersect_p (bitmap
, bitmap
);
96 /* True if the complement of the second intersects the first (their
97 AND_COMPL is non-empty). */
98 extern bool bitmap_intersect_compl_p (bitmap
, bitmap
);
100 /* True if MAP is an empty bitmap. */
101 #define bitmap_empty_p(MAP) (!(MAP)->first)
103 /* Count the number of bits set in the bitmap. */
104 extern unsigned long bitmap_count_bits (bitmap
);
106 /* Boolean operations on bitmaps. The _into variants are two operand
107 versions that modify the first source operand. The other variants
108 are three operand versions that to not destroy the source bitmaps.
109 The operations supported are &, & ~, |, ^. */
110 extern void bitmap_and (bitmap
, bitmap
, bitmap
);
111 extern void bitmap_and_into (bitmap
, bitmap
);
112 extern void bitmap_and_compl (bitmap
, bitmap
, bitmap
);
113 extern bool bitmap_and_compl_into (bitmap
, bitmap
);
114 #define bitmap_compl_and(DST, A, B) bitmap_and_compl (DST, B, A)
115 extern void bitmap_compl_and_into (bitmap
, bitmap
);
116 extern void bitmap_clear_range (bitmap
, unsigned int, unsigned int);
117 extern bool bitmap_ior (bitmap
, bitmap
, bitmap
);
118 extern bool bitmap_ior_into (bitmap
, bitmap
);
119 extern void bitmap_xor (bitmap
, bitmap
, bitmap
);
120 extern void bitmap_xor_into (bitmap
, bitmap
);
122 /* DST = A | (B & ~C). Return true if DST changes. */
123 extern bool bitmap_ior_and_compl (bitmap DST
, bitmap A
, bitmap B
, bitmap C
);
124 /* A |= (B & ~C). Return true if A changes. */
125 extern bool bitmap_ior_and_compl_into (bitmap DST
, bitmap B
, bitmap C
);
127 /* Clear a single register in a register set. */
128 extern void bitmap_clear_bit (bitmap
, int);
130 /* Set a single register in a register set. */
131 extern void bitmap_set_bit (bitmap
, int);
133 /* Return true if a register is set in a register set. */
134 extern int bitmap_bit_p (bitmap
, int);
136 /* Debug functions to print a bitmap linked list. */
137 extern void debug_bitmap (bitmap
);
138 extern void debug_bitmap_file (FILE *, bitmap
);
140 /* Print a bitmap. */
141 extern void bitmap_print (FILE *, bitmap
, const char *, const char *);
143 /* Initialize and release a bitmap obstack. */
144 extern void bitmap_obstack_initialize (bitmap_obstack
*);
145 extern void bitmap_obstack_release (bitmap_obstack
*);
147 /* Initialize a bitmap header. OBSTACK indicates the bitmap obstack
148 to allocate from, NULL for GC'd bitmap. */
151 bitmap_initialize (bitmap head
, bitmap_obstack
*obstack
)
153 head
->first
= head
->current
= NULL
;
154 head
->obstack
= obstack
;
157 /* Allocate and free bitmaps from obstack, malloc and gc'd memory. */
158 extern bitmap
bitmap_obstack_alloc (bitmap_obstack
*obstack
);
159 extern bitmap
bitmap_gc_alloc (void);
160 extern void bitmap_obstack_free (bitmap
);
162 /* A few compatibility/functions macros for compatibility with sbitmaps */
163 #define dump_bitmap(file, bitmap) bitmap_print (file, bitmap, "", "\n")
164 #define bitmap_zero(a) bitmap_clear (a)
165 extern unsigned bitmap_first_set_bit (bitmap
);
167 /* Compute bitmap hash (for purposes of hashing etc.) */
168 extern hashval_t
bitmap_hash(bitmap
);
170 /* Allocate a bitmap from a bit obstack. */
171 #define BITMAP_ALLOC(OBSTACK) bitmap_obstack_alloc (OBSTACK)
173 /* Allocate a gc'd bitmap. */
174 #define BITMAP_GGC_ALLOC() bitmap_gc_alloc ()
176 /* Do any cleanup needed on a bitmap when it is no longer used. */
177 #define BITMAP_FREE(BITMAP) \
178 ((void)(bitmap_obstack_free (BITMAP), (BITMAP) = NULL))
180 /* Iterator for bitmaps. */
184 /* Pointer to the current bitmap element. */
185 bitmap_element
*elt1
;
187 /* Pointer to 2nd bitmap element when two are involved. */
188 bitmap_element
*elt2
;
190 /* Word within the current element. */
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 /* Initialize a single bitmap iterator. START_BIT is the first bit to
203 bmp_iter_set_init (bitmap_iterator
*bi
, bitmap map
,
204 unsigned start_bit
, unsigned *bit_no
)
206 bi
->elt1
= map
->first
;
209 /* Advance elt1 until it is not before the block containing start_bit. */
214 bi
->elt1
= &bitmap_zero_bits
;
218 if (bi
->elt1
->indx
>= start_bit
/ BITMAP_ELEMENT_ALL_BITS
)
220 bi
->elt1
= bi
->elt1
->next
;
223 /* We might have gone past the start bit, so reinitialize it. */
224 if (bi
->elt1
->indx
!= start_bit
/ BITMAP_ELEMENT_ALL_BITS
)
225 start_bit
= bi
->elt1
->indx
* BITMAP_ELEMENT_ALL_BITS
;
227 /* Initialize for what is now start_bit. */
228 bi
->word_no
= start_bit
/ BITMAP_WORD_BITS
% BITMAP_ELEMENT_WORDS
;
229 bi
->bits
= bi
->elt1
->bits
[bi
->word_no
];
230 bi
->bits
>>= start_bit
% BITMAP_WORD_BITS
;
232 /* If this word is zero, we must make sure we're not pointing at the
233 first bit, otherwise our incrementing to the next word boundary
234 will fail. It won't matter if this increment moves us into the
236 start_bit
+= !bi
->bits
;
241 /* Initialize an iterator to iterate over the intersection of two
242 bitmaps. START_BIT is the bit to commence from. */
245 bmp_iter_and_init (bitmap_iterator
*bi
, bitmap map1
, bitmap map2
,
246 unsigned start_bit
, unsigned *bit_no
)
248 bi
->elt1
= map1
->first
;
249 bi
->elt2
= map2
->first
;
251 /* Advance elt1 until it is not before the block containing
261 if (bi
->elt1
->indx
>= start_bit
/ BITMAP_ELEMENT_ALL_BITS
)
263 bi
->elt1
= bi
->elt1
->next
;
266 /* Advance elt2 until it is not before elt1. */
271 bi
->elt1
= bi
->elt2
= &bitmap_zero_bits
;
275 if (bi
->elt2
->indx
>= bi
->elt1
->indx
)
277 bi
->elt2
= bi
->elt2
->next
;
280 /* If we're at the same index, then we have some intersecting bits. */
281 if (bi
->elt1
->indx
== bi
->elt2
->indx
)
283 /* We might have advanced beyond the start_bit, so reinitialize
285 if (bi
->elt1
->indx
!= start_bit
/ BITMAP_ELEMENT_ALL_BITS
)
286 start_bit
= bi
->elt1
->indx
* BITMAP_ELEMENT_ALL_BITS
;
288 bi
->word_no
= start_bit
/ BITMAP_WORD_BITS
% BITMAP_ELEMENT_WORDS
;
289 bi
->bits
= bi
->elt1
->bits
[bi
->word_no
] & bi
->elt2
->bits
[bi
->word_no
];
290 bi
->bits
>>= start_bit
% BITMAP_WORD_BITS
;
294 /* Otherwise we must immediately advance elt1, so initialize for
296 bi
->word_no
= BITMAP_ELEMENT_WORDS
- 1;
300 /* If this word is zero, we must make sure we're not pointing at the
301 first bit, otherwise our incrementing to the next word boundary
302 will fail. It won't matter if this increment moves us into the
304 start_bit
+= !bi
->bits
;
309 /* Initialize an iterator to iterate over the bits in MAP1 & ~MAP2.
313 bmp_iter_and_compl_init (bitmap_iterator
*bi
, bitmap map1
, bitmap map2
,
314 unsigned start_bit
, unsigned *bit_no
)
316 bi
->elt1
= map1
->first
;
317 bi
->elt2
= map2
->first
;
319 /* Advance elt1 until it is not before the block containing start_bit. */
324 bi
->elt1
= &bitmap_zero_bits
;
328 if (bi
->elt1
->indx
>= start_bit
/ BITMAP_ELEMENT_ALL_BITS
)
330 bi
->elt1
= bi
->elt1
->next
;
333 /* Advance elt2 until it is not before elt1. */
334 while (bi
->elt2
&& bi
->elt2
->indx
< bi
->elt1
->indx
)
335 bi
->elt2
= bi
->elt2
->next
;
337 /* We might have advanced beyond the start_bit, so reinitialize for
339 if (bi
->elt1
->indx
!= start_bit
/ BITMAP_ELEMENT_ALL_BITS
)
340 start_bit
= bi
->elt1
->indx
* BITMAP_ELEMENT_ALL_BITS
;
342 bi
->word_no
= start_bit
/ BITMAP_WORD_BITS
% BITMAP_ELEMENT_WORDS
;
343 bi
->bits
= bi
->elt1
->bits
[bi
->word_no
];
344 if (bi
->elt2
&& bi
->elt1
->indx
== bi
->elt2
->indx
)
345 bi
->bits
&= ~bi
->elt2
->bits
[bi
->word_no
];
346 bi
->bits
>>= start_bit
% BITMAP_WORD_BITS
;
348 /* If this word is zero, we must make sure we're not pointing at the
349 first bit, otherwise our incrementing to the next word boundary
350 will fail. It won't matter if this increment moves us into the
352 start_bit
+= !bi
->bits
;
357 /* Advance to the next bit in BI. We don't advance to the next
361 bmp_iter_next (bitmap_iterator
*bi
, unsigned *bit_no
)
367 /* Advance to the next nonzero bit of a single bitmap, we will have
368 already advanced past the just iterated bit. Return true if there
369 is a bit to iterate. */
372 bmp_iter_set (bitmap_iterator
*bi
, unsigned *bit_no
)
374 /* If our current word is nonzero, it contains the bit we want. */
378 while (!(bi
->bits
& 1))
386 /* Round up to the word boundary. We might have just iterated past
387 the end of the last word, hence the -1. It is not possible for
388 bit_no to point at the beginning of the now last word. */
389 *bit_no
= ((*bit_no
+ BITMAP_WORD_BITS
- 1)
390 / BITMAP_WORD_BITS
* BITMAP_WORD_BITS
);
395 /* Find the next nonzero word in this elt. */
396 while (bi
->word_no
!= BITMAP_ELEMENT_WORDS
)
398 bi
->bits
= bi
->elt1
->bits
[bi
->word_no
];
401 *bit_no
+= BITMAP_WORD_BITS
;
405 /* Advance to the next element. */
406 bi
->elt1
= bi
->elt1
->next
;
409 *bit_no
= bi
->elt1
->indx
* BITMAP_ELEMENT_ALL_BITS
;
414 /* Advance to the next nonzero bit of an intersecting pair of
415 bitmaps. We will have already advanced past the just iterated bit.
416 Return true if there is a bit to iterate. */
419 bmp_iter_and (bitmap_iterator
*bi
, unsigned *bit_no
)
421 /* If our current word is nonzero, it contains the bit we want. */
425 while (!(bi
->bits
& 1))
433 /* Round up to the word boundary. We might have just iterated past
434 the end of the last word, hence the -1. It is not possible for
435 bit_no to point at the beginning of the now last word. */
436 *bit_no
= ((*bit_no
+ BITMAP_WORD_BITS
- 1)
437 / BITMAP_WORD_BITS
* BITMAP_WORD_BITS
);
442 /* Find the next nonzero word in this elt. */
443 while (bi
->word_no
!= BITMAP_ELEMENT_WORDS
)
445 bi
->bits
= bi
->elt1
->bits
[bi
->word_no
] & bi
->elt2
->bits
[bi
->word_no
];
448 *bit_no
+= BITMAP_WORD_BITS
;
452 /* Advance to the next identical element. */
455 /* Advance elt1 while it is less than elt2. We always want
456 to advance one elt. */
459 bi
->elt1
= bi
->elt1
->next
;
463 while (bi
->elt1
->indx
< bi
->elt2
->indx
);
465 /* Advance elt2 to be no less than elt1. This might not
467 while (bi
->elt2
->indx
< bi
->elt1
->indx
)
469 bi
->elt2
= bi
->elt2
->next
;
474 while (bi
->elt1
->indx
!= bi
->elt2
->indx
);
476 *bit_no
= bi
->elt1
->indx
* BITMAP_ELEMENT_ALL_BITS
;
481 /* Advance to the next nonzero bit in the intersection of
482 complemented bitmaps. We will have already advanced past the just
486 bmp_iter_and_compl (bitmap_iterator
*bi
, unsigned *bit_no
)
488 /* If our current word is nonzero, it contains the bit we want. */
492 while (!(bi
->bits
& 1))
500 /* Round up to the word boundary. We might have just iterated past
501 the end of the last word, hence the -1. It is not possible for
502 bit_no to point at the beginning of the now last word. */
503 *bit_no
= ((*bit_no
+ BITMAP_WORD_BITS
- 1)
504 / BITMAP_WORD_BITS
* BITMAP_WORD_BITS
);
509 /* Find the next nonzero word in this elt. */
510 while (bi
->word_no
!= BITMAP_ELEMENT_WORDS
)
512 bi
->bits
= bi
->elt1
->bits
[bi
->word_no
];
513 if (bi
->elt2
&& bi
->elt2
->indx
== bi
->elt1
->indx
)
514 bi
->bits
&= ~bi
->elt2
->bits
[bi
->word_no
];
517 *bit_no
+= BITMAP_WORD_BITS
;
521 /* Advance to the next element of elt1. */
522 bi
->elt1
= bi
->elt1
->next
;
526 /* Advance elt2 until it is no less than elt1. */
527 while (bi
->elt2
&& bi
->elt2
->indx
< bi
->elt1
->indx
)
528 bi
->elt2
= bi
->elt2
->next
;
530 *bit_no
= bi
->elt1
->indx
* BITMAP_ELEMENT_ALL_BITS
;
535 /* Loop over all bits set in BITMAP, starting with MIN and setting
536 BITNUM to the bit number. ITER is a bitmap iterator. BITNUM
537 should be treated as a read-only variable as it contains loop
540 #define EXECUTE_IF_SET_IN_BITMAP(BITMAP, MIN, BITNUM, ITER) \
541 for (bmp_iter_set_init (&(ITER), (BITMAP), (MIN), &(BITNUM)); \
542 bmp_iter_set (&(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_IN_BITMAP(BITMAP1, BITMAP2, MIN, BITNUM, ITER) \
551 for (bmp_iter_and_init (&(ITER), (BITMAP1), (BITMAP2), (MIN), \
553 bmp_iter_and (&(ITER), &(BITNUM)); \
554 bmp_iter_next (&(ITER), &(BITNUM)))
556 /* Loop over all the bits set in BITMAP1 & ~BITMAP2, starting with MIN
557 and setting BITNUM to the bit number. ITER is a bitmap iterator.
558 BITNUM should be treated as a read-only variable as it contains
561 #define EXECUTE_IF_AND_COMPL_IN_BITMAP(BITMAP1, BITMAP2, MIN, BITNUM, ITER) \
562 for (bmp_iter_and_compl_init (&(ITER), (BITMAP1), (BITMAP2), (MIN), \
564 bmp_iter_and_compl (&(ITER), &(BITNUM)); \
565 bmp_iter_next (&(ITER), &(BITNUM)))
567 #endif /* GCC_BITMAP_H */