1 /* Functions to support general ended bitmaps.
2 Copyright (C) 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
24 /* Number of words to use for each element in the linked list. */
26 #ifndef BITMAP_ELEMENT_WORDS
27 #define BITMAP_ELEMENT_WORDS 2
30 /* Number of bits in each actual element of a bitmap. We get slightly better
31 code for bit % BITMAP_ELEMENT_ALL_BITS and bit / BITMAP_ELEMENT_ALL_BITS if
32 bits is unsigned, assuming it is a power of 2. */
34 #define BITMAP_ELEMENT_ALL_BITS \
35 ((unsigned) (BITMAP_ELEMENT_WORDS * HOST_BITS_PER_WIDE_INT))
37 /* Bitmap set element. We use a linked list to hold only the bits that
38 are set. This allows for use to grow the bitset dynamically without
39 having to realloc and copy a giant bit array. The `prev' field is
40 undefined for an element on the free list. */
42 typedef struct bitmap_element_def
44 struct bitmap_element_def
*next
; /* Next element. */
45 struct bitmap_element_def
*prev
; /* Previous element. */
46 unsigned int indx
; /* regno/BITMAP_ELEMENT_ALL_BITS. */
47 unsigned HOST_WIDE_INT bits
[BITMAP_ELEMENT_WORDS
]; /* Bits that are set. */
50 /* Head of bitmap linked list. */
51 typedef struct bitmap_head_def
{
52 bitmap_element
*first
; /* First element in linked list. */
53 bitmap_element
*current
; /* Last element looked at. */
54 unsigned int indx
; /* Index of last element looked at. */
55 } bitmap_head
, *bitmap
;
57 /* Enumeration giving the various operations we support. */
59 BITMAP_AND
, /* TO = FROM1 & FROM2 */
60 BITMAP_AND_COMPL
, /* TO = FROM1 & ~ FROM2 */
61 BITMAP_IOR
, /* TO = FROM1 | FROM2 */
62 BITMAP_XOR
/* TO = FROM1 ^ FROM2 */
66 extern bitmap_element
*bitmap_free
; /* Freelist of bitmap elements */
67 extern bitmap_element bitmap_zero
; /* Zero bitmap element */
69 /* Clear a bitmap by freeing up the linked list. */
70 extern void bitmap_clear
PARAMS ((bitmap
));
72 /* Copy a bitmap to another bitmap. */
73 extern void bitmap_copy
PARAMS ((bitmap
, bitmap
));
75 /* True if two bitmaps are identical. */
76 extern int bitmap_equal_p
PARAMS ((bitmap
, bitmap
));
78 /* Perform an operation on two bitmaps, yielding a third. */
79 extern int bitmap_operation
PARAMS ((bitmap
, bitmap
, bitmap
, enum bitmap_bits
));
81 /* `or' into one bitmap the `and' of a second bitmap witih the complement
83 extern void bitmap_ior_and_compl
PARAMS ((bitmap
, bitmap
, bitmap
));
85 /* Clear a single register in a register set. */
86 extern void bitmap_clear_bit
PARAMS ((bitmap
, int));
88 /* Set a single register in a register set. */
89 extern void bitmap_set_bit
PARAMS ((bitmap
, int));
91 /* Return true if a register is set in a register set. */
92 extern int bitmap_bit_p
PARAMS ((bitmap
, int));
94 /* Debug functions to print a bitmap linked list. */
95 extern void debug_bitmap
PARAMS ((bitmap
));
96 extern void debug_bitmap_file
PARAMS ((FILE *, bitmap
));
99 extern void bitmap_print
PARAMS ((FILE *, bitmap
, const char *, const char *));
101 /* Initialize a bitmap header. */
102 extern bitmap bitmap_initialize
PARAMS ((bitmap
));
104 /* Release all memory held by bitmaps. */
105 extern void bitmap_release_memory
PARAMS ((void));
107 /* Allocate a bitmap with oballoc. */
108 #define BITMAP_OBSTACK_ALLOC(OBSTACK) \
109 bitmap_initialize ((bitmap) obstack_alloc (OBSTACK, sizeof (bitmap_head)))
111 /* Allocate a bitmap with alloca. */
112 #define BITMAP_ALLOCA() \
113 bitmap_initialize ((bitmap) alloca (sizeof (bitmap_head)))
115 /* Allocate a bitmap with xmalloc. */
116 #define BITMAP_XMALLOC() \
117 bitmap_initialize ((bitmap) xmalloc (sizeof (bitmap_head)))
119 /* Do any cleanup needed on a bitmap when it is no longer used. */
120 #define BITMAP_FREE(BITMAP) \
124 bitmap_clear (BITMAP); \
129 /* Do any cleanup needed on an xmalloced bitmap when it is no longer used. */
130 #define BITMAP_XFREE(BITMAP) \
134 bitmap_clear (BITMAP); \
140 /* Do any one-time initializations needed for bitmaps. */
141 #define BITMAP_INIT_ONCE()
143 /* Loop over all bits in BITMAP, starting with MIN, setting BITNUM to the
144 bit number and executing CODE for all bits that are set. */
146 #define EXECUTE_IF_SET_IN_BITMAP(BITMAP, MIN, BITNUM, CODE) \
148 bitmap_element *ptr_ = (BITMAP)->first; \
149 unsigned int indx_ = (MIN) / BITMAP_ELEMENT_ALL_BITS; \
150 unsigned bit_num_ = (MIN) % ((unsigned) HOST_BITS_PER_WIDE_INT); \
151 unsigned word_num_ = (((MIN) / ((unsigned) HOST_BITS_PER_WIDE_INT)) \
152 % BITMAP_ELEMENT_WORDS); \
155 /* Find the block the minimum bit is in. */ \
156 while (ptr_ != 0 && ptr_->indx < indx_) \
159 if (ptr_ != 0 && ptr_->indx != indx_) \
165 for (; ptr_ != 0; ptr_ = ptr_->next) \
167 for (; word_num_ < BITMAP_ELEMENT_WORDS; word_num_++) \
169 unsigned HOST_WIDE_INT word_ = ptr_->bits[word_num_]; \
173 for (; bit_num_ < HOST_BITS_PER_WIDE_INT; bit_num_++) \
175 unsigned HOST_WIDE_INT mask_ \
176 = ((unsigned HOST_WIDE_INT) 1) << bit_num_; \
178 if ((word_ & mask_) != 0) \
181 (BITNUM) = (ptr_->indx * BITMAP_ELEMENT_ALL_BITS \
182 + word_num_ * HOST_BITS_PER_WIDE_INT \
199 /* Loop over all bits in BITMAP1 and BITMAP2, starting with MIN, setting
200 BITNUM to the bit number and executing CODE for all bits that are set in
201 the first bitmap and not set in the second. */
203 #define EXECUTE_IF_AND_COMPL_IN_BITMAP(BITMAP1, BITMAP2, MIN, BITNUM, CODE) \
205 bitmap_element *ptr1_ = (BITMAP1)->first; \
206 bitmap_element *ptr2_ = (BITMAP2)->first; \
207 unsigned int indx_ = (MIN) / BITMAP_ELEMENT_ALL_BITS; \
208 unsigned bit_num_ = (MIN) % ((unsigned) HOST_BITS_PER_WIDE_INT); \
209 unsigned word_num_ = (((MIN) / ((unsigned) HOST_BITS_PER_WIDE_INT)) \
210 % BITMAP_ELEMENT_WORDS); \
212 /* Find the block the minimum bit is in in the first bitmap. */ \
213 while (ptr1_ != 0 && ptr1_->indx < indx_) \
214 ptr1_ = ptr1_->next; \
216 if (ptr1_ != 0 && ptr1_->indx != indx_) \
222 for (; ptr1_ != 0 ; ptr1_ = ptr1_->next) \
224 /* Advance BITMAP2 to the equivalent link, using an all \
225 zero element if an equivalent link doesn't exist. */ \
226 bitmap_element *tmp2_; \
228 while (ptr2_ != 0 && ptr2_->indx < ptr1_->indx) \
229 ptr2_ = ptr2_->next; \
231 tmp2_ = ((ptr2_ != 0 && ptr2_->indx == ptr1_->indx) \
232 ? ptr2_ : &bitmap_zero); \
234 for (; word_num_ < BITMAP_ELEMENT_WORDS; word_num_++) \
236 unsigned HOST_WIDE_INT word_ = (ptr1_->bits[word_num_] \
237 & ~ tmp2_->bits[word_num_]); \
240 for (; bit_num_ < HOST_BITS_PER_WIDE_INT; bit_num_++) \
242 unsigned HOST_WIDE_INT mask_ \
243 = ((unsigned HOST_WIDE_INT)1) << bit_num_; \
245 if ((word_ & mask_) != 0) \
248 (BITNUM) = (ptr1_->indx * BITMAP_ELEMENT_ALL_BITS \
249 + word_num_ * HOST_BITS_PER_WIDE_INT \
266 /* Loop over all bits in BITMAP1 and BITMAP2, starting with MIN, setting
267 BITNUM to the bit number and executing CODE for all bits that are set in
270 #define EXECUTE_IF_AND_IN_BITMAP(BITMAP1, BITMAP2, MIN, BITNUM, CODE) \
272 bitmap_element *ptr1_ = (BITMAP1)->first; \
273 bitmap_element *ptr2_ = (BITMAP2)->first; \
274 unsigned int indx_ = (MIN) / BITMAP_ELEMENT_ALL_BITS; \
275 unsigned bit_num_ = (MIN) % ((unsigned) HOST_BITS_PER_WIDE_INT); \
276 unsigned word_num_ = (((MIN) / ((unsigned) HOST_BITS_PER_WIDE_INT)) \
277 % BITMAP_ELEMENT_WORDS); \
279 /* Find the block the minimum bit is in in the first bitmap. */ \
280 while (ptr1_ != 0 && ptr1_->indx < indx_) \
281 ptr1_ = ptr1_->next; \
283 if (ptr1_ != 0 && ptr1_->indx != indx_) \
289 for (; ptr1_ != 0 ; ptr1_ = ptr1_->next) \
291 /* Advance BITMAP2 to the equivalent link */ \
292 while (ptr2_ != 0 && ptr2_->indx < ptr1_->indx) \
293 ptr2_ = ptr2_->next; \
297 /* If there are no more elements in BITMAP2, exit loop now.*/ \
298 ptr1_ = (bitmap_element *)0; \
301 else if (ptr2_->indx > ptr1_->indx) \
303 bit_num_ = word_num_ = 0; \
307 for (; word_num_ < BITMAP_ELEMENT_WORDS; word_num_++) \
309 unsigned HOST_WIDE_INT word_ = (ptr1_->bits[word_num_] \
310 & ptr2_->bits[word_num_]); \
313 for (; bit_num_ < HOST_BITS_PER_WIDE_INT; bit_num_++) \
315 unsigned HOST_WIDE_INT mask_ \
316 = ((unsigned HOST_WIDE_INT)1) << bit_num_; \
318 if ((word_ & mask_) != 0) \
321 (BITNUM) = (ptr1_->indx * BITMAP_ELEMENT_ALL_BITS \
322 + word_num_ * HOST_BITS_PER_WIDE_INT \
339 #endif /* _BITMAP_H */