Convert diagnostics to use quoting flag q 2/n
[official-gcc.git] / gcc / bitmap.h
blobcac84c11fba4a4e0497f5f0e5e5f3f5fc1d997ed
1 /* Functions to support general ended bitmaps.
2 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003
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 /* Enumeration giving the various operations we support. */
70 enum bitmap_bits {
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 */
78 /* Global data */
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 int bitmap_equal_p (bitmap, bitmap);
90 /* Perform an operation on two bitmaps, yielding a third. */
91 extern int bitmap_operation (bitmap, bitmap, bitmap, enum bitmap_bits);
93 /* `or' into one bitmap the `and' of a second bitmap witih the complement
94 of a third. */
95 extern void bitmap_ior_and_compl (bitmap, bitmap, bitmap);
97 /* Clear a single register in a register set. */
98 extern void bitmap_clear_bit (bitmap, int);
100 /* Set a single register in a register set. */
101 extern void bitmap_set_bit (bitmap, int);
103 /* Return true if a register is set in a register set. */
104 extern int bitmap_bit_p (bitmap, int);
106 /* Debug functions to print a bitmap linked list. */
107 extern void debug_bitmap (bitmap);
108 extern void debug_bitmap_file (FILE *, bitmap);
110 /* Print a bitmap. */
111 extern void bitmap_print (FILE *, bitmap, const char *, const char *);
113 /* Initialize a bitmap header. If HEAD is NULL, a new header will be
114 allocated. USING_OBSTACK indicates how elements should be allocated. */
115 extern bitmap bitmap_initialize (bitmap head, int using_obstack);
117 /* Release all memory used by the bitmap obstack. */
118 extern void bitmap_release_memory (void);
120 /* A few compatibility/functions macros for compatibility with sbitmaps */
121 #define dump_bitmap(file, bitmap) bitmap_print (file, bitmap, "", "\n")
122 #define bitmap_zero(a) bitmap_clear (a)
123 #define bitmap_a_or_b(a,b,c) bitmap_operation (a, b, c, BITMAP_IOR)
124 #define bitmap_a_and_b(a,b,c) bitmap_operation (a, b, c, BITMAP_AND)
125 extern int bitmap_union_of_diff (bitmap, bitmap, bitmap, bitmap);
126 extern int bitmap_first_set_bit (bitmap);
127 extern int bitmap_last_set_bit (bitmap);
129 /* Allocate a bitmap with oballoc. */
130 #define BITMAP_OBSTACK_ALLOC(OBSTACK) \
131 bitmap_initialize (obstack_alloc (OBSTACK, sizeof (bitmap_head)), 1)
133 /* Allocate a bitmap with ggc_alloc. */
134 #define BITMAP_GGC_ALLOC() \
135 bitmap_initialize (NULL, 0)
137 /* Allocate a bitmap with xmalloc. */
138 #define BITMAP_XMALLOC() \
139 bitmap_initialize (xmalloc (sizeof (bitmap_head)), 1)
141 /* Do any cleanup needed on a bitmap when it is no longer used. */
142 #define BITMAP_FREE(BITMAP) \
143 do { \
144 if (BITMAP) \
146 bitmap_clear (BITMAP); \
147 (BITMAP) = 0; \
149 } while (0)
151 /* Do any cleanup needed on an xmalloced bitmap when it is no longer used. */
152 #define BITMAP_XFREE(BITMAP) \
153 do { \
154 if (BITMAP) \
156 bitmap_clear (BITMAP); \
157 free (BITMAP); \
158 (BITMAP) = 0; \
160 } while (0)
162 /* Do any one-time initializations needed for bitmaps. */
163 #define BITMAP_INIT_ONCE()
165 /* Iterator for bitmaps. */
167 typedef struct
169 /* Actual elements in the bitmaps. */
170 bitmap_element *ptr1, *ptr2;
172 /* Position of an actual word in the elements. */
173 unsigned word;
175 /* Position of a bit corresponding to the start of word. */
176 unsigned word_bit;
178 /* Position of the actual bit. */
179 unsigned bit;
181 /* Contents of the actually processed word. When finding next bit
182 it is shifted right, so that the actual bit is always the least
183 significant bit of ACTUAL. */
184 BITMAP_WORD actual;
185 } bitmap_iterator;
187 /* Moves the iterator BI to the first set bit on or after the current
188 position in bitmap and returns the bit if available. The bit is
189 found in ACTUAL field only. */
191 static inline unsigned
192 bmp_iter_common_next_1 (bitmap_iterator *bi)
194 while (!(bi->actual & 1))
196 bi->actual >>= 1;
197 bi->bit++;
200 return bi->bit;
203 /* Moves the iterator BI to the first set bit on or after the current
204 position in bitmap and returns the bit if available. */
206 static inline unsigned
207 bmp_iter_single_next_1 (bitmap_iterator *bi)
209 if (bi->actual)
210 return bmp_iter_common_next_1 (bi);
212 bi->word++;
213 bi->word_bit += BITMAP_WORD_BITS;
215 while (1)
217 for (;
218 bi->word < BITMAP_ELEMENT_WORDS;
219 bi->word++, bi->word_bit += BITMAP_WORD_BITS)
221 bi->actual = bi->ptr1->bits[bi->word];
222 if (bi->actual)
224 bi->bit = bi->word_bit;
225 return bmp_iter_common_next_1 (bi);
229 bi->ptr1 = bi->ptr1->next;
230 if (!bi->ptr1)
231 return 0;
233 bi->word = 0;
234 bi->word_bit = bi->ptr1->indx * BITMAP_ELEMENT_ALL_BITS;
238 /* Initializes a bitmap iterator BI for looping over bits of bitmap
239 BMP, starting with bit MIN. Returns the first bit of BMP greater
240 or equal to MIN if there is any. */
242 static inline unsigned
243 bmp_iter_single_init (bitmap_iterator *bi, bitmap bmp, unsigned min)
245 unsigned indx = min / BITMAP_ELEMENT_ALL_BITS;
247 for (bi->ptr1 = bmp->first;
248 bi->ptr1 && bi->ptr1->indx < indx;
249 bi->ptr1 = bi->ptr1->next)
250 continue;
252 if (!bi->ptr1)
254 /* To avoid warnings. */
255 bi->word = 0;
256 bi->bit = 0;
257 bi->word_bit = 0;
258 bi->actual = 0;
259 bi->ptr2 = NULL;
260 return 0;
263 if (bi->ptr1->indx == indx)
265 unsigned bit_in_elt = min - BITMAP_ELEMENT_ALL_BITS * indx;
266 unsigned word_in_elt = bit_in_elt / BITMAP_WORD_BITS;
267 unsigned bit_in_word = bit_in_elt % BITMAP_WORD_BITS;
269 bi->word = word_in_elt;
270 bi->word_bit = min - bit_in_word;
271 bi->bit = min;
272 bi->actual = bi->ptr1->bits[word_in_elt] >> bit_in_word;
274 else
276 bi->word = 0;
277 bi->bit = bi->ptr1->indx * BITMAP_ELEMENT_ALL_BITS;
278 bi->word_bit = bi->bit;
279 bi->actual = bi->ptr1->bits[0];
282 return bmp_iter_single_next_1 (bi);
285 /* Returns true if all elements of the bitmap referred to by iterator BI
286 were processed. */
288 static inline bool
289 bmp_iter_end_p (bitmap_iterator bi)
291 return bi.ptr1 == NULL;
294 /* Moves the iterator BI to the next bit of bitmap and returns the bit
295 if available. */
297 static inline unsigned
298 bmp_iter_single_next (bitmap_iterator *bi)
300 bi->bit++;
301 bi->actual >>= 1;
302 return bmp_iter_single_next_1 (bi);
305 /* Loop over all bits in BITMAP, starting with MIN and setting BITNUM to
306 the bit number. ITER is a bitmap iterator. */
308 #define EXECUTE_IF_SET_IN_BITMAP(BITMAP, MIN, BITNUM, ITER) \
309 for ((BITNUM) = bmp_iter_single_init (&(ITER), (BITMAP), (MIN)); \
310 !bmp_iter_end_p (ITER); \
311 (BITNUM) = bmp_iter_single_next (&(ITER)))
313 /* Moves the iterator BI to the first set bit on or after the current
314 position in difference of bitmaps and returns the bit if available. */
316 static inline unsigned
317 bmp_iter_and_not_next_1 (bitmap_iterator *bi)
319 if (bi->actual)
320 return bmp_iter_common_next_1 (bi);
322 bi->word++;
323 bi->word_bit += BITMAP_WORD_BITS;
325 while (1)
327 bitmap_element *snd;
329 if (bi->ptr2 && bi->ptr2->indx == bi->ptr1->indx)
330 snd = bi->ptr2;
331 else
332 snd = &bitmap_zero_bits;
334 for (;
335 bi->word < BITMAP_ELEMENT_WORDS;
336 bi->word++, bi->word_bit += BITMAP_WORD_BITS)
338 bi->actual = (bi->ptr1->bits[bi->word]
339 & ~snd->bits[bi->word]);
340 if (bi->actual)
342 bi->bit = bi->word_bit;
343 return bmp_iter_common_next_1 (bi);
347 bi->ptr1 = bi->ptr1->next;
348 if (!bi->ptr1)
349 return 0;
351 while (bi->ptr2
352 && bi->ptr2->indx < bi->ptr1->indx)
353 bi->ptr2 = bi->ptr2->next;
355 bi->word = 0;
356 bi->word_bit = bi->ptr1->indx * BITMAP_ELEMENT_ALL_BITS;
360 /* Initializes a bitmap iterator BI for looping over bits of bitmap
361 BMP1 &~ BMP2, starting with bit MIN. Returns the first bit of
362 BMP1 &~ BMP2 greater or equal to MIN if there is any. */
364 static inline unsigned
365 bmp_iter_and_not_init (bitmap_iterator *bi, bitmap bmp1, bitmap bmp2,
366 unsigned min)
368 unsigned indx = min / BITMAP_ELEMENT_ALL_BITS;
370 for (bi->ptr1 = bmp1->first;
371 bi->ptr1 && bi->ptr1->indx < indx;
372 bi->ptr1 = bi->ptr1->next)
373 continue;
375 if (!bi->ptr1)
377 /* To avoid warnings. */
378 bi->word = 0;
379 bi->bit = 0;
380 bi->word_bit = 0;
381 bi->actual = 0;
382 bi->ptr2 = NULL;
383 return 0;
386 for (bi->ptr2 = bmp2->first;
387 bi->ptr2 && bi->ptr2->indx < bi->ptr1->indx;
388 bi->ptr2 = bi->ptr2->next)
389 continue;
391 if (bi->ptr1->indx == indx)
393 unsigned bit_in_elt = min - BITMAP_ELEMENT_ALL_BITS * indx;
394 unsigned word_in_elt = bit_in_elt / BITMAP_WORD_BITS;
395 unsigned bit_in_word = bit_in_elt % BITMAP_WORD_BITS;
397 bi->word = word_in_elt;
398 bi->word_bit = min - bit_in_word;
399 bi->bit = min;
401 if (bi->ptr2 && bi->ptr2->indx == indx)
402 bi->actual = (bi->ptr1->bits[word_in_elt]
403 & ~bi->ptr2->bits[word_in_elt]) >> bit_in_word;
404 else
405 bi->actual = bi->ptr1->bits[word_in_elt] >> bit_in_word;
407 else
409 bi->word = 0;
410 bi->bit = bi->ptr1->indx * BITMAP_ELEMENT_ALL_BITS;
411 bi->word_bit = bi->bit;
413 if (bi->ptr2 && bi->ptr2->indx == bi->ptr1->indx)
414 bi->actual = (bi->ptr1->bits[0] & ~bi->ptr2->bits[0]);
415 else
416 bi->actual = bi->ptr1->bits[0];
419 return bmp_iter_and_not_next_1 (bi);
422 /* Moves the iterator BI to the next bit of difference of bitmaps and returns
423 the bit if available. */
425 static inline unsigned
426 bmp_iter_and_not_next (bitmap_iterator *bi)
428 bi->bit++;
429 bi->actual >>= 1;
430 return bmp_iter_and_not_next_1 (bi);
433 /* Loop over all bits in BMP1 and BMP2, starting with MIN, setting
434 BITNUM to the bit number for all bits that are set in the first bitmap
435 and not set in the second. ITER is a bitmap iterator. */
437 #define EXECUTE_IF_AND_COMPL_IN_BITMAP(BMP1, BMP2, MIN, BITNUM, ITER) \
438 for ((BITNUM) = bmp_iter_and_not_init (&(ITER), (BMP1), (BMP2), (MIN)); \
439 !bmp_iter_end_p (ITER); \
440 (BITNUM) = bmp_iter_and_not_next (&(ITER)))
442 /* Moves the iterator BI to the first set bit on or after the current
443 position in intersection of bitmaps and returns the bit if available. */
445 static inline unsigned
446 bmp_iter_and_next_1 (bitmap_iterator *bi)
448 if (bi->actual)
449 return bmp_iter_common_next_1 (bi);
451 bi->word++;
452 bi->word_bit += BITMAP_WORD_BITS;
454 while (1)
456 for (;
457 bi->word < BITMAP_ELEMENT_WORDS;
458 bi->word++, bi->word_bit += BITMAP_WORD_BITS)
460 bi->actual = (bi->ptr1->bits[bi->word]
461 & bi->ptr2->bits[bi->word]);
462 if (bi->actual)
464 bi->bit = bi->word_bit;
465 return bmp_iter_common_next_1 (bi);
471 bi->ptr1 = bi->ptr1->next;
472 if (!bi->ptr1)
473 return 0;
475 while (bi->ptr2->indx < bi->ptr1->indx)
477 bi->ptr2 = bi->ptr2->next;
478 if (!bi->ptr2)
480 bi->ptr1 = NULL;
481 return 0;
485 while (bi->ptr1->indx != bi->ptr2->indx);
487 bi->word = 0;
488 bi->word_bit = bi->ptr1->indx * BITMAP_ELEMENT_ALL_BITS;
492 /* Initializes a bitmap iterator BI for looping over bits of bitmap
493 BMP1 & BMP2, starting with bit MIN. Returns the first bit of
494 BMP1 & BMP2 greater or equal to MIN if there is any. */
496 static inline unsigned
497 bmp_iter_and_init (bitmap_iterator *bi, bitmap bmp1, bitmap bmp2,
498 unsigned min)
500 unsigned indx = min / BITMAP_ELEMENT_ALL_BITS;
502 for (bi->ptr1 = bmp1->first;
503 bi->ptr1 && bi->ptr1->indx < indx;
504 bi->ptr1 = bi->ptr1->next)
505 continue;
507 if (!bi->ptr1)
508 goto empty;
510 bi->ptr2 = bmp2->first;
511 if (!bi->ptr2)
512 goto empty;
514 while (1)
516 while (bi->ptr2->indx < bi->ptr1->indx)
518 bi->ptr2 = bi->ptr2->next;
519 if (!bi->ptr2)
520 goto empty;
523 if (bi->ptr1->indx == bi->ptr2->indx)
524 break;
526 bi->ptr1 = bi->ptr1->next;
527 if (!bi->ptr1)
528 goto empty;
531 if (bi->ptr1->indx == indx)
533 unsigned bit_in_elt = min - BITMAP_ELEMENT_ALL_BITS * indx;
534 unsigned word_in_elt = bit_in_elt / BITMAP_WORD_BITS;
535 unsigned bit_in_word = bit_in_elt % BITMAP_WORD_BITS;
537 bi->word = word_in_elt;
538 bi->word_bit = min - bit_in_word;
539 bi->bit = min;
541 bi->actual = (bi->ptr1->bits[word_in_elt]
542 & bi->ptr2->bits[word_in_elt]) >> bit_in_word;
544 else
546 bi->word = 0;
547 bi->bit = bi->ptr1->indx * BITMAP_ELEMENT_ALL_BITS;
548 bi->word_bit = bi->bit;
550 bi->actual = (bi->ptr1->bits[0] & bi->ptr2->bits[0]);
553 return bmp_iter_and_next_1 (bi);
555 empty:
556 /* To avoid warnings. */
557 bi->word = 0;
558 bi->bit = 0;
559 bi->word_bit = 0;
560 bi->actual = 0;
561 bi->ptr1 = NULL;
562 bi->ptr2 = NULL;
563 return 0;
566 /* Moves the iterator BI to the next bit of intersection of bitmaps and returns
567 the bit if available. */
569 static inline unsigned
570 bmp_iter_and_next (bitmap_iterator *bi)
572 bi->bit++;
573 bi->actual >>= 1;
574 return bmp_iter_and_next_1 (bi);
577 /* Loop over all bits in BMP1 and BMP2, starting with MIN, setting
578 BITNUM to the bit number for all bits that are set in both bitmaps.
579 ITER is a bitmap iterator. */
581 #define EXECUTE_IF_AND_IN_BITMAP(BMP1, BMP2, MIN, BITNUM, ITER) \
582 for ((BITNUM) = bmp_iter_and_init (&(ITER), (BMP1), (BMP2), (MIN)); \
583 !bmp_iter_end_p (ITER); \
584 (BITNUM) = bmp_iter_and_next (&(ITER)))
586 #endif /* GCC_BITMAP_H */