PR target/16286
[official-gcc.git] / gcc / bitmap.h
blob7bf6efe52c74f07a6b8d07ca310ccb6b23587fb4
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
10 version.
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
15 for more details.
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
20 02111-1307, USA. */
22 #ifndef GCC_BITMAP_H
23 #define GCC_BITMAP_H
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)
37 #endif
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. */
57 } bitmap_element;
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
65 allocation? */
66 } bitmap_head;
67 typedef struct bitmap_head_def *bitmap;
69 /* Global data */
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) \
152 do { \
153 if (BITMAP) \
155 bitmap_clear (BITMAP); \
156 (BITMAP) = 0; \
158 } while (0)
160 /* Do any cleanup needed on an xmalloced bitmap when it is no longer used. */
161 #define BITMAP_XFREE(BITMAP) \
162 do { \
163 if (BITMAP) \
165 bitmap_clear (BITMAP); \
166 free (BITMAP); \
167 (BITMAP) = 0; \
169 } while (0)
171 /* Do any one-time initializations needed for bitmaps. */
172 #define BITMAP_INIT_ONCE()
174 /* Iterator for bitmaps. */
176 typedef struct
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. */
185 unsigned word_no;
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. */
190 BITMAP_WORD bits;
191 } bitmap_iterator;
193 /* Initialize a single bitmap iterator. START_BIT is the first bit to
194 iterate from. */
196 static inline void
197 bmp_iter_set_init (bitmap_iterator *bi, bitmap map,
198 unsigned start_bit, unsigned *bit_no)
200 bi->elt1 = map->first;
201 bi->elt2 = NULL;
203 /* Advance elt1 until it is not before the block containing start_bit. */
204 while (1)
206 if (!bi->elt1)
208 bi->elt1 = &bitmap_zero_bits;
209 break;
212 if (bi->elt1->indx >= start_bit / BITMAP_ELEMENT_ALL_BITS)
213 break;
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
229 next word. */
230 start_bit += !bi->bits;
232 *bit_no = start_bit;
235 /* Initialize an iterator to iterate over the intersection of two
236 bitmaps. START_BIT is the bit to commence from. */
238 static inline void
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
246 start_bit. */
247 while (1)
249 if (!bi->elt1)
251 bi->elt2 = NULL;
252 break;
255 if (bi->elt1->indx >= start_bit / BITMAP_ELEMENT_ALL_BITS)
256 break;
257 bi->elt1 = bi->elt1->next;
260 /* Advance elt2 until it is not before elt1. */
261 while (1)
263 if (!bi->elt2)
265 bi->elt1 = bi->elt2 = &bitmap_zero_bits;
266 break;
269 if (bi->elt2->indx >= bi->elt1->indx)
270 break;
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
278 for that. */
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;
286 else
288 /* Otherwise we must immediately advance elt1, so initialize for
289 that. */
290 bi->word_no = BITMAP_ELEMENT_WORDS - 1;
291 bi->bits = 0;
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
297 next word. */
298 start_bit += !bi->bits;
300 *bit_no = start_bit;
303 /* Initialize an iterator to iterate over the bits in MAP1 & ~MAP2.
306 static inline void
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. */
314 while (1)
316 if (!bi->elt1)
318 bi->elt1 = &bitmap_zero_bits;
319 break;
322 if (bi->elt1->indx >= start_bit / BITMAP_ELEMENT_ALL_BITS)
323 break;
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
332 that. */
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
345 next word. */
346 start_bit += !bi->bits;
348 *bit_no = start_bit;
351 /* Advance to the next bit in BI. We don't advance to the next
352 nonzero bit yet. */
354 static inline void
355 bmp_iter_next (bitmap_iterator *bi, unsigned *bit_no)
357 bi->bits >>= 1;
358 *bit_no += 1;
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. */
365 static inline bool
366 bmp_iter_set (bitmap_iterator *bi, unsigned *bit_no)
368 /* If our current word is nonzero, it contains the bit we want. */
369 if (bi->bits)
371 next_bit:
372 while (!(bi->bits & 1))
374 bi->bits >>= 1;
375 *bit_no += 1;
377 return true;
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);
385 bi->word_no++;
387 while (1)
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];
393 if (bi->bits)
394 goto next_bit;
395 *bit_no += BITMAP_WORD_BITS;
396 bi->word_no++;
399 /* Advance to the next element. */
400 bi->elt1 = bi->elt1->next;
401 if (!bi->elt1)
402 return false;
403 *bit_no = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
404 bi->word_no = 0;
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. */
412 static inline bool
413 bmp_iter_and (bitmap_iterator *bi, unsigned *bit_no)
415 /* If our current word is nonzero, it contains the bit we want. */
416 if (bi->bits)
418 next_bit:
419 while (!(bi->bits & 1))
421 bi->bits >>= 1;
422 *bit_no += 1;
424 return true;
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);
432 bi->word_no++;
434 while (1)
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];
440 if (bi->bits)
441 goto next_bit;
442 *bit_no += BITMAP_WORD_BITS;
443 bi->word_no++;
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;
454 if (!bi->elt1)
455 return false;
457 while (bi->elt1->indx < bi->elt2->indx);
459 /* Advance elt2 to be no less than elt1. This might not
460 advance. */
461 while (bi->elt2->indx < bi->elt1->indx)
463 bi->elt2 = bi->elt2->next;
464 if (!bi->elt2)
465 return false;
468 while (bi->elt1->indx != bi->elt2->indx);
470 *bit_no = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
471 bi->word_no = 0;
475 /* Advance to the next nonzero bit in the intersection of
476 complemented bitmaps. We will have already advanced past the just
477 iterated bit. */
479 static inline bool
480 bmp_iter_and_compl (bitmap_iterator *bi, unsigned *bit_no)
482 /* If our current word is nonzero, it contains the bit we want. */
483 if (bi->bits)
485 next_bit:
486 while (!(bi->bits & 1))
488 bi->bits >>= 1;
489 *bit_no += 1;
491 return true;
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);
499 bi->word_no++;
501 while (1)
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];
509 if (bi->bits)
510 goto next_bit;
511 *bit_no += BITMAP_WORD_BITS;
512 bi->word_no++;
515 /* Advance to the next element of elt1. */
516 bi->elt1 = bi->elt1->next;
517 if (!bi->elt1)
518 return false;
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;
525 bi->word_no = 0;
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
532 state. */
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
542 loop state. */
544 #define EXECUTE_IF_AND_IN_BITMAP(BITMAP1, BITMAP2, MIN, BITNUM, ITER) \
545 for (bmp_iter_and_init (&(ITER), (BITMAP1), (BITMAP2), (MIN), \
546 &(BITNUM)); \
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
553 loop state. */
555 #define EXECUTE_IF_AND_COMPL_IN_BITMAP(BITMAP1, BITMAP2, MIN, BITNUM, ITER) \
556 for (bmp_iter_and_compl_init (&(ITER), (BITMAP1), (BITMAP2), (MIN), \
557 &(BITNUM)); \
558 bmp_iter_and_compl (&(ITER), &(BITNUM)); \
559 bmp_iter_next (&(ITER), &(BITNUM)))
561 #endif /* GCC_BITMAP_H */