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 /* Pointer to the current bitmap element. */
188 bitmap_element
*elt1
;
190 /* Pointer to 2nd bitmap element when two are involved. */
191 bitmap_element
*elt2
;
193 /* Word within the current element. */
196 /* Contents of the actually processed word. When finding next bit
197 it is shifted right, so that the actual bit is always the least
198 significant bit of ACTUAL. */
202 /* Initialize a single bitmap iterator. START_BIT is the first bit to
206 bmp_iter_set_init (bitmap_iterator
*bi
, bitmap map
,
207 unsigned start_bit
, unsigned *bit_no
)
209 bi
->elt1
= map
->first
;
212 /* Advance elt1 until it is not before the block containing start_bit. */
217 bi
->elt1
= &bitmap_zero_bits
;
221 if (bi
->elt1
->indx
>= start_bit
/ BITMAP_ELEMENT_ALL_BITS
)
223 bi
->elt1
= bi
->elt1
->next
;
226 /* We might have gone past the start bit, so reinitialize it. */
227 if (bi
->elt1
->indx
!= start_bit
/ BITMAP_ELEMENT_ALL_BITS
)
228 start_bit
= bi
->elt1
->indx
* BITMAP_ELEMENT_ALL_BITS
;
230 /* Initialize for what is now start_bit. */
231 bi
->word_no
= start_bit
/ BITMAP_WORD_BITS
% BITMAP_ELEMENT_WORDS
;
232 bi
->bits
= bi
->elt1
->bits
[bi
->word_no
];
233 bi
->bits
>>= start_bit
% BITMAP_WORD_BITS
;
235 /* If this word is zero, we must make sure we're not pointing at the
236 first bit, otherwise our incrementing to the next word boundary
237 will fail. It won't matter if this increment moves us into the
239 start_bit
+= !bi
->bits
;
244 /* Initialize an iterator to iterate over the intersection of two
245 bitmaps. START_BIT is the bit to commence from. */
248 bmp_iter_and_init (bitmap_iterator
*bi
, bitmap map1
, bitmap map2
,
249 unsigned start_bit
, unsigned *bit_no
)
251 bi
->elt1
= map1
->first
;
252 bi
->elt2
= map2
->first
;
254 /* Advance elt1 until it is not before the block containing
264 if (bi
->elt1
->indx
>= start_bit
/ BITMAP_ELEMENT_ALL_BITS
)
266 bi
->elt1
= bi
->elt1
->next
;
269 /* Advance elt2 until it is not before elt1. */
274 bi
->elt1
= bi
->elt2
= &bitmap_zero_bits
;
278 if (bi
->elt2
->indx
>= bi
->elt1
->indx
)
280 bi
->elt2
= bi
->elt2
->next
;
283 /* If we're at the same index, then we have some intersecting bits. */
284 if (bi
->elt1
->indx
== bi
->elt2
->indx
)
286 /* We might have advanced beyond the start_bit, so reinitialize
288 if (bi
->elt1
->indx
!= start_bit
/ BITMAP_ELEMENT_ALL_BITS
)
289 start_bit
= bi
->elt1
->indx
* BITMAP_ELEMENT_ALL_BITS
;
291 bi
->word_no
= start_bit
/ BITMAP_WORD_BITS
% BITMAP_ELEMENT_WORDS
;
292 bi
->bits
= bi
->elt1
->bits
[bi
->word_no
] & bi
->elt2
->bits
[bi
->word_no
];
293 bi
->bits
>>= start_bit
% BITMAP_WORD_BITS
;
297 /* Otherwise we must immediately advance elt1, so initialize for
299 bi
->word_no
= BITMAP_ELEMENT_WORDS
- 1;
303 /* If this word is zero, we must make sure we're not pointing at the
304 first bit, otherwise our incrementing to the next word boundary
305 will fail. It won't matter if this increment moves us into the
307 start_bit
+= !bi
->bits
;
312 /* Initialize an iterator to iterate over the bits in MAP1 & ~MAP2.
316 bmp_iter_and_compl_init (bitmap_iterator
*bi
, bitmap map1
, bitmap map2
,
317 unsigned start_bit
, unsigned *bit_no
)
319 bi
->elt1
= map1
->first
;
320 bi
->elt2
= map2
->first
;
322 /* Advance elt1 until it is not before the block containing start_bit. */
327 bi
->elt1
= &bitmap_zero_bits
;
331 if (bi
->elt1
->indx
>= start_bit
/ BITMAP_ELEMENT_ALL_BITS
)
333 bi
->elt1
= bi
->elt1
->next
;
336 /* Advance elt2 until it is not before elt1. */
337 while (bi
->elt2
&& bi
->elt2
->indx
< bi
->elt1
->indx
)
338 bi
->elt2
= bi
->elt2
->next
;
340 /* We might have advanced beyond the start_bit, so reinitialize for
342 if (bi
->elt1
->indx
!= start_bit
/ BITMAP_ELEMENT_ALL_BITS
)
343 start_bit
= bi
->elt1
->indx
* BITMAP_ELEMENT_ALL_BITS
;
345 bi
->word_no
= start_bit
/ BITMAP_WORD_BITS
% BITMAP_ELEMENT_WORDS
;
346 bi
->bits
= bi
->elt1
->bits
[bi
->word_no
];
347 if (bi
->elt2
&& bi
->elt1
->indx
== bi
->elt2
->indx
)
348 bi
->bits
&= ~bi
->elt2
->bits
[bi
->word_no
];
349 bi
->bits
>>= start_bit
% BITMAP_WORD_BITS
;
351 /* If this word is zero, we must make sure we're not pointing at the
352 first bit, otherwise our incrementing to the next word boundary
353 will fail. It won't matter if this increment moves us into the
355 start_bit
+= !bi
->bits
;
360 /* Advance to the next bit in BI. We don't advance to the next
364 bmp_iter_next (bitmap_iterator
*bi
, unsigned *bit_no
)
370 /* Advance to the next nonzero bit of a single bitmap, we will have
371 already advanced past the just iterated bit. Return true if there
372 is a bit to iterate. */
375 bmp_iter_set (bitmap_iterator
*bi
, unsigned *bit_no
)
377 /* If our current word is nonzero, it contains the bit we want. */
381 while (!(bi
->bits
& 1))
389 /* Round up to the word boundary. We might have just iterated past
390 the end of the last word, hence the -1. It is not possible for
391 bit_no to point at the beginning of the now last word. */
392 *bit_no
= ((*bit_no
+ BITMAP_WORD_BITS
- 1)
393 / BITMAP_WORD_BITS
* BITMAP_WORD_BITS
);
398 /* Find the next nonzero word in this elt. */
399 while (bi
->word_no
!= BITMAP_ELEMENT_WORDS
)
401 bi
->bits
= bi
->elt1
->bits
[bi
->word_no
];
404 *bit_no
+= BITMAP_WORD_BITS
;
408 /* Advance to the next element. */
409 bi
->elt1
= bi
->elt1
->next
;
412 *bit_no
= bi
->elt1
->indx
* BITMAP_ELEMENT_ALL_BITS
;
417 /* Advance to the next nonzero bit of an intersecting pair of
418 bitmaps. We will have already advanced past the just iterated bit.
419 Return true if there is a bit to iterate. */
422 bmp_iter_and (bitmap_iterator
*bi
, unsigned *bit_no
)
424 /* If our current word is nonzero, it contains the bit we want. */
428 while (!(bi
->bits
& 1))
436 /* Round up to the word boundary. We might have just iterated past
437 the end of the last word, hence the -1. It is not possible for
438 bit_no to point at the beginning of the now last word. */
439 *bit_no
= ((*bit_no
+ BITMAP_WORD_BITS
- 1)
440 / BITMAP_WORD_BITS
* BITMAP_WORD_BITS
);
445 /* Find the next nonzero word in this elt. */
446 while (bi
->word_no
!= BITMAP_ELEMENT_WORDS
)
448 bi
->bits
= bi
->elt1
->bits
[bi
->word_no
] & bi
->elt2
->bits
[bi
->word_no
];
451 *bit_no
+= BITMAP_WORD_BITS
;
455 /* Advance to the next identical element. */
458 /* Advance elt1 while it is less than elt2. We always want
459 to advance one elt. */
462 bi
->elt1
= bi
->elt1
->next
;
466 while (bi
->elt1
->indx
< bi
->elt2
->indx
);
468 /* Advance elt2 to be no less than elt1. This might not
470 while (bi
->elt2
->indx
< bi
->elt1
->indx
)
472 bi
->elt2
= bi
->elt2
->next
;
477 while (bi
->elt1
->indx
!= bi
->elt2
->indx
);
479 *bit_no
= bi
->elt1
->indx
* BITMAP_ELEMENT_ALL_BITS
;
484 /* Advance to the next nonzero bit in the intersection of
485 complemented bitmaps. We will have already advanced past the just
489 bmp_iter_and_compl (bitmap_iterator
*bi
, unsigned *bit_no
)
491 /* If our current word is nonzero, it contains the bit we want. */
495 while (!(bi
->bits
& 1))
503 /* Round up to the word boundary. We might have just iterated past
504 the end of the last word, hence the -1. It is not possible for
505 bit_no to point at the beginning of the now last word. */
506 *bit_no
= ((*bit_no
+ BITMAP_WORD_BITS
- 1)
507 / BITMAP_WORD_BITS
* BITMAP_WORD_BITS
);
512 /* Find the next nonzero word in this elt. */
513 while (bi
->word_no
!= BITMAP_ELEMENT_WORDS
)
515 bi
->bits
= bi
->elt1
->bits
[bi
->word_no
];
516 if (bi
->elt2
&& bi
->elt2
->indx
== bi
->elt1
->indx
)
517 bi
->bits
&= ~bi
->elt2
->bits
[bi
->word_no
];
520 *bit_no
+= BITMAP_WORD_BITS
;
524 /* Advance to the next element of elt1. */
525 bi
->elt1
= bi
->elt1
->next
;
529 /* Advance elt2 until it is no less than elt1. */
530 while (bi
->elt2
&& bi
->elt2
->indx
< bi
->elt1
->indx
)
531 bi
->elt2
= bi
->elt2
->next
;
533 *bit_no
= bi
->elt1
->indx
* BITMAP_ELEMENT_ALL_BITS
;
538 /* Loop over all bits set in BITMAP, starting with MIN and setting
539 BITNUM to the bit number. ITER is a bitmap iterator. BITNUM
540 should be treated as a read-only variable as it contains loop
543 #define EXECUTE_IF_SET_IN_BITMAP(BITMAP, MIN, BITNUM, ITER) \
544 for (bmp_iter_set_init (&(ITER), (BITMAP), (MIN), &(BITNUM)); \
545 bmp_iter_set (&(ITER), &(BITNUM)); \
546 bmp_iter_next (&(ITER), &(BITNUM)))
548 /* Loop over all the bits set in BITMAP1 & BITMAP2, starting with MIN
549 and setting BITNUM to the bit number. ITER is a bitmap iterator.
550 BITNUM should be treated as a read-only variable as it contains
553 #define EXECUTE_IF_AND_IN_BITMAP(BITMAP1, BITMAP2, MIN, BITNUM, ITER) \
554 for (bmp_iter_and_init (&(ITER), (BITMAP1), (BITMAP2), (MIN), \
556 bmp_iter_and (&(ITER), &(BITNUM)); \
557 bmp_iter_next (&(ITER), &(BITNUM)))
559 /* Loop over all the bits set in BITMAP1 & ~BITMAP2, starting with MIN
560 and setting BITNUM to the bit number. ITER is a bitmap iterator.
561 BITNUM should be treated as a read-only variable as it contains
564 #define EXECUTE_IF_AND_COMPL_IN_BITMAP(BITMAP1, BITMAP2, MIN, BITNUM, ITER) \
565 for (bmp_iter_and_compl_init (&(ITER), (BITMAP1), (BITMAP2), (MIN), \
567 bmp_iter_and_compl (&(ITER), &(BITNUM)); \
568 bmp_iter_next (&(ITER), &(BITNUM)))
570 #endif /* GCC_BITMAP_H */