* rw.po: Remove.
[official-gcc.git] / gcc / bitmap.h
blobfbc720067e3437918d565b5a9ef6f003c38a1e3d
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
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 COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #ifndef GCC_BITMAP_H
22 #define GCC_BITMAP_H
23 #include "hashtab.h"
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)
36 #endif
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;
48 } bitmap_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. */
68 } bitmap_element;
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. */
77 } bitmap_head;
80 /* Global data */
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. */
150 static inline void
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. */
182 typedef struct
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. */
191 unsigned word_no;
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. */
196 BITMAP_WORD bits;
197 } bitmap_iterator;
199 /* Initialize a single bitmap iterator. START_BIT is the first bit to
200 iterate from. */
202 static inline void
203 bmp_iter_set_init (bitmap_iterator *bi, bitmap map,
204 unsigned start_bit, unsigned *bit_no)
206 bi->elt1 = map->first;
207 bi->elt2 = NULL;
209 /* Advance elt1 until it is not before the block containing start_bit. */
210 while (1)
212 if (!bi->elt1)
214 bi->elt1 = &bitmap_zero_bits;
215 break;
218 if (bi->elt1->indx >= start_bit / BITMAP_ELEMENT_ALL_BITS)
219 break;
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
235 next word. */
236 start_bit += !bi->bits;
238 *bit_no = start_bit;
241 /* Initialize an iterator to iterate over the intersection of two
242 bitmaps. START_BIT is the bit to commence from. */
244 static inline void
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
252 start_bit. */
253 while (1)
255 if (!bi->elt1)
257 bi->elt2 = NULL;
258 break;
261 if (bi->elt1->indx >= start_bit / BITMAP_ELEMENT_ALL_BITS)
262 break;
263 bi->elt1 = bi->elt1->next;
266 /* Advance elt2 until it is not before elt1. */
267 while (1)
269 if (!bi->elt2)
271 bi->elt1 = bi->elt2 = &bitmap_zero_bits;
272 break;
275 if (bi->elt2->indx >= bi->elt1->indx)
276 break;
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
284 for that. */
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;
292 else
294 /* Otherwise we must immediately advance elt1, so initialize for
295 that. */
296 bi->word_no = BITMAP_ELEMENT_WORDS - 1;
297 bi->bits = 0;
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
303 next word. */
304 start_bit += !bi->bits;
306 *bit_no = start_bit;
309 /* Initialize an iterator to iterate over the bits in MAP1 & ~MAP2.
312 static inline void
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. */
320 while (1)
322 if (!bi->elt1)
324 bi->elt1 = &bitmap_zero_bits;
325 break;
328 if (bi->elt1->indx >= start_bit / BITMAP_ELEMENT_ALL_BITS)
329 break;
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
338 that. */
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
351 next word. */
352 start_bit += !bi->bits;
354 *bit_no = start_bit;
357 /* Advance to the next bit in BI. We don't advance to the next
358 nonzero bit yet. */
360 static inline void
361 bmp_iter_next (bitmap_iterator *bi, unsigned *bit_no)
363 bi->bits >>= 1;
364 *bit_no += 1;
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. */
371 static inline bool
372 bmp_iter_set (bitmap_iterator *bi, unsigned *bit_no)
374 /* If our current word is nonzero, it contains the bit we want. */
375 if (bi->bits)
377 next_bit:
378 while (!(bi->bits & 1))
380 bi->bits >>= 1;
381 *bit_no += 1;
383 return true;
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);
391 bi->word_no++;
393 while (1)
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];
399 if (bi->bits)
400 goto next_bit;
401 *bit_no += BITMAP_WORD_BITS;
402 bi->word_no++;
405 /* Advance to the next element. */
406 bi->elt1 = bi->elt1->next;
407 if (!bi->elt1)
408 return false;
409 *bit_no = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
410 bi->word_no = 0;
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. */
418 static inline bool
419 bmp_iter_and (bitmap_iterator *bi, unsigned *bit_no)
421 /* If our current word is nonzero, it contains the bit we want. */
422 if (bi->bits)
424 next_bit:
425 while (!(bi->bits & 1))
427 bi->bits >>= 1;
428 *bit_no += 1;
430 return true;
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);
438 bi->word_no++;
440 while (1)
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];
446 if (bi->bits)
447 goto next_bit;
448 *bit_no += BITMAP_WORD_BITS;
449 bi->word_no++;
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;
460 if (!bi->elt1)
461 return false;
463 while (bi->elt1->indx < bi->elt2->indx);
465 /* Advance elt2 to be no less than elt1. This might not
466 advance. */
467 while (bi->elt2->indx < bi->elt1->indx)
469 bi->elt2 = bi->elt2->next;
470 if (!bi->elt2)
471 return false;
474 while (bi->elt1->indx != bi->elt2->indx);
476 *bit_no = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
477 bi->word_no = 0;
481 /* Advance to the next nonzero bit in the intersection of
482 complemented bitmaps. We will have already advanced past the just
483 iterated bit. */
485 static inline bool
486 bmp_iter_and_compl (bitmap_iterator *bi, unsigned *bit_no)
488 /* If our current word is nonzero, it contains the bit we want. */
489 if (bi->bits)
491 next_bit:
492 while (!(bi->bits & 1))
494 bi->bits >>= 1;
495 *bit_no += 1;
497 return true;
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);
505 bi->word_no++;
507 while (1)
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];
515 if (bi->bits)
516 goto next_bit;
517 *bit_no += BITMAP_WORD_BITS;
518 bi->word_no++;
521 /* Advance to the next element of elt1. */
522 bi->elt1 = bi->elt1->next;
523 if (!bi->elt1)
524 return false;
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;
531 bi->word_no = 0;
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
538 state. */
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
548 loop state. */
550 #define EXECUTE_IF_AND_IN_BITMAP(BITMAP1, BITMAP2, MIN, BITNUM, ITER) \
551 for (bmp_iter_and_init (&(ITER), (BITMAP1), (BITMAP2), (MIN), \
552 &(BITNUM)); \
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
559 loop state. */
561 #define EXECUTE_IF_AND_COMPL_IN_BITMAP(BITMAP1, BITMAP2, MIN, BITNUM, ITER) \
562 for (bmp_iter_and_compl_init (&(ITER), (BITMAP1), (BITMAP2), (MIN), \
563 &(BITNUM)); \
564 bmp_iter_and_compl (&(ITER), &(BITNUM)); \
565 bmp_iter_next (&(ITER), &(BITNUM)))
567 #endif /* GCC_BITMAP_H */