* g++.dg/cpp0x/nullptr21.c: Remove printfs, make self-checking.
[gcc.git] / gcc / bitmap.h
1 /* Functions to support general ended bitmaps.
2 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
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.
11
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.
16
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/>. */
20
21 #ifndef GCC_BITMAP_H
22 #define GCC_BITMAP_H
23 #include "hashtab.h"
24 #include "statistics.h"
25 #include "obstack.h"
26
27 /* Fundamental storage type for bitmap. */
28
29 typedef unsigned long BITMAP_WORD;
30 /* BITMAP_WORD_BITS needs to be unsigned, but cannot contain casts as
31 it is used in preprocessor directives -- hence the 1u. */
32 #define BITMAP_WORD_BITS (CHAR_BIT * SIZEOF_LONG * 1u)
33
34 /* Number of words to use for each element in the linked list. */
35
36 #ifndef BITMAP_ELEMENT_WORDS
37 #define BITMAP_ELEMENT_WORDS ((128 + BITMAP_WORD_BITS - 1) / BITMAP_WORD_BITS)
38 #endif
39
40 /* Number of bits in each actual element of a bitmap. */
41
42 #define BITMAP_ELEMENT_ALL_BITS (BITMAP_ELEMENT_WORDS * BITMAP_WORD_BITS)
43
44 /* Obstack for allocating bitmaps and elements from. */
45 typedef struct GTY (()) bitmap_obstack {
46 struct bitmap_element_def *elements;
47 struct bitmap_head_def *heads;
48 struct obstack GTY ((skip)) obstack;
49 } bitmap_obstack;
50
51 /* Bitmap set element. We use a linked list to hold only the bits that
52 are set. This allows for use to grow the bitset dynamically without
53 having to realloc and copy a giant bit array.
54
55 The free list is implemented as a list of lists. There is one
56 outer list connected together by prev fields. Each element of that
57 outer is an inner list (that may consist only of the outer list
58 element) that are connected by the next fields. The prev pointer
59 is undefined for interior elements. This allows
60 bitmap_elt_clear_from to be implemented in unit time rather than
61 linear in the number of elements to be freed. */
62
63 typedef struct GTY(()) bitmap_element_def {
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;
69
70 struct bitmap_descriptor;
71 /* Head of bitmap linked list. gengtype ignores ifdefs, but for
72 statistics we need to add a bitmap descriptor pointer. As it is
73 not collected, we can just GTY((skip)) it. */
74
75 typedef struct GTY(()) bitmap_head_def {
76 bitmap_element *first; /* First element in linked list. */
77 bitmap_element *current; /* Last element looked at. */
78 unsigned int indx; /* Index of last element looked at. */
79 bitmap_obstack *obstack; /* Obstack to allocate elements from.
80 If NULL, then use GGC allocation. */
81 struct bitmap_descriptor GTY((skip)) *desc;
82 } bitmap_head;
83
84 /* Global data */
85 extern bitmap_element bitmap_zero_bits; /* Zero bitmap element */
86 extern bitmap_obstack bitmap_default_obstack; /* Default bitmap obstack */
87
88 /* Clear a bitmap by freeing up the linked list. */
89 extern void bitmap_clear (bitmap);
90
91 /* Copy a bitmap to another bitmap. */
92 extern void bitmap_copy (bitmap, const_bitmap);
93
94 /* True if two bitmaps are identical. */
95 extern bool bitmap_equal_p (const_bitmap, const_bitmap);
96
97 /* True if the bitmaps intersect (their AND is non-empty). */
98 extern bool bitmap_intersect_p (const_bitmap, const_bitmap);
99
100 /* True if the complement of the second intersects the first (their
101 AND_COMPL is non-empty). */
102 extern bool bitmap_intersect_compl_p (const_bitmap, const_bitmap);
103
104 /* True if MAP is an empty bitmap. */
105 #define bitmap_empty_p(MAP) (!(MAP)->first)
106
107 /* True if the bitmap has only a single bit set. */
108 extern bool bitmap_single_bit_set_p (const_bitmap);
109
110 /* Count the number of bits set in the bitmap. */
111 extern unsigned long bitmap_count_bits (const_bitmap);
112
113 /* Boolean operations on bitmaps. The _into variants are two operand
114 versions that modify the first source operand. The other variants
115 are three operand versions that to not destroy the source bitmaps.
116 The operations supported are &, & ~, |, ^. */
117 extern void bitmap_and (bitmap, const_bitmap, const_bitmap);
118 extern void bitmap_and_into (bitmap, const_bitmap);
119 extern bool bitmap_and_compl (bitmap, const_bitmap, const_bitmap);
120 extern bool bitmap_and_compl_into (bitmap, const_bitmap);
121 #define bitmap_compl_and(DST, A, B) bitmap_and_compl (DST, B, A)
122 extern void bitmap_compl_and_into (bitmap, const_bitmap);
123 extern void bitmap_clear_range (bitmap, unsigned int, unsigned int);
124 extern void bitmap_set_range (bitmap, unsigned int, unsigned int);
125 extern bool bitmap_ior (bitmap, const_bitmap, const_bitmap);
126 extern bool bitmap_ior_into (bitmap, const_bitmap);
127 extern void bitmap_xor (bitmap, const_bitmap, const_bitmap);
128 extern void bitmap_xor_into (bitmap, const_bitmap);
129
130 /* DST = A | (B & C). Return true if DST changes. */
131 extern bool bitmap_ior_and_into (bitmap DST, const_bitmap B, const_bitmap C);
132 /* DST = A | (B & ~C). Return true if DST changes. */
133 extern bool bitmap_ior_and_compl (bitmap DST, const_bitmap A, const_bitmap B, const_bitmap C);
134 /* A |= (B & ~C). Return true if A changes. */
135 extern bool bitmap_ior_and_compl_into (bitmap DST, const_bitmap B, const_bitmap C);
136
137 /* Clear a single bit in a bitmap. Return true if the bit changed. */
138 extern bool bitmap_clear_bit (bitmap, int);
139
140 /* Set a single bit in a bitmap. Return true if the bit changed. */
141 extern bool bitmap_set_bit (bitmap, int);
142
143 /* Return true if a register is set in a register set. */
144 extern int bitmap_bit_p (bitmap, int);
145
146 /* Debug functions to print a bitmap linked list. */
147 extern void debug_bitmap (const_bitmap);
148 extern void debug_bitmap_file (FILE *, const_bitmap);
149
150 /* Print a bitmap. */
151 extern void bitmap_print (FILE *, const_bitmap, const char *, const char *);
152
153 /* Initialize and release a bitmap obstack. */
154 extern void bitmap_obstack_initialize (bitmap_obstack *);
155 extern void bitmap_obstack_release (bitmap_obstack *);
156 extern void bitmap_register (bitmap MEM_STAT_DECL);
157 extern void dump_bitmap_statistics (void);
158
159 /* Initialize a bitmap header. OBSTACK indicates the bitmap obstack
160 to allocate from, NULL for GC'd bitmap. */
161
162 static inline void
163 bitmap_initialize_stat (bitmap head, bitmap_obstack *obstack MEM_STAT_DECL)
164 {
165 head->first = head->current = NULL;
166 head->obstack = obstack;
167 if (GATHER_STATISTICS)
168 bitmap_register (head PASS_MEM_STAT);
169 }
170 #define bitmap_initialize(h,o) bitmap_initialize_stat (h,o MEM_STAT_INFO)
171
172 /* Allocate and free bitmaps from obstack, malloc and gc'd memory. */
173 extern bitmap bitmap_obstack_alloc_stat (bitmap_obstack *obstack MEM_STAT_DECL);
174 #define bitmap_obstack_alloc(t) bitmap_obstack_alloc_stat (t MEM_STAT_INFO)
175 extern bitmap bitmap_gc_alloc_stat (ALONE_MEM_STAT_DECL);
176 #define bitmap_gc_alloc() bitmap_gc_alloc_stat (ALONE_MEM_STAT_INFO)
177 extern void bitmap_obstack_free (bitmap);
178
179 /* A few compatibility/functions macros for compatibility with sbitmaps */
180 #define dump_bitmap(file, bitmap) bitmap_print (file, bitmap, "", "\n")
181 #define bitmap_zero(a) bitmap_clear (a)
182 extern unsigned bitmap_first_set_bit (const_bitmap);
183 extern unsigned bitmap_last_set_bit (const_bitmap);
184
185 /* Compute bitmap hash (for purposes of hashing etc.) */
186 extern hashval_t bitmap_hash(const_bitmap);
187
188 /* Allocate a bitmap from a bit obstack. */
189 #define BITMAP_ALLOC(OBSTACK) bitmap_obstack_alloc (OBSTACK)
190
191 /* Allocate a gc'd bitmap. */
192 #define BITMAP_GGC_ALLOC() bitmap_gc_alloc ()
193
194 /* Do any cleanup needed on a bitmap when it is no longer used. */
195 #define BITMAP_FREE(BITMAP) \
196 ((void) (bitmap_obstack_free ((bitmap) BITMAP), (BITMAP) = (bitmap) NULL))
197
198 /* Iterator for bitmaps. */
199
200 typedef struct
201 {
202 /* Pointer to the current bitmap element. */
203 bitmap_element *elt1;
204
205 /* Pointer to 2nd bitmap element when two are involved. */
206 bitmap_element *elt2;
207
208 /* Word within the current element. */
209 unsigned word_no;
210
211 /* Contents of the actually processed word. When finding next bit
212 it is shifted right, so that the actual bit is always the least
213 significant bit of ACTUAL. */
214 BITMAP_WORD bits;
215 } bitmap_iterator;
216
217 /* Initialize a single bitmap iterator. START_BIT is the first bit to
218 iterate from. */
219
220 static inline void
221 bmp_iter_set_init (bitmap_iterator *bi, const_bitmap map,
222 unsigned start_bit, unsigned *bit_no)
223 {
224 bi->elt1 = map->first;
225 bi->elt2 = NULL;
226
227 /* Advance elt1 until it is not before the block containing start_bit. */
228 while (1)
229 {
230 if (!bi->elt1)
231 {
232 bi->elt1 = &bitmap_zero_bits;
233 break;
234 }
235
236 if (bi->elt1->indx >= start_bit / BITMAP_ELEMENT_ALL_BITS)
237 break;
238 bi->elt1 = bi->elt1->next;
239 }
240
241 /* We might have gone past the start bit, so reinitialize it. */
242 if (bi->elt1->indx != start_bit / BITMAP_ELEMENT_ALL_BITS)
243 start_bit = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
244
245 /* Initialize for what is now start_bit. */
246 bi->word_no = start_bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
247 bi->bits = bi->elt1->bits[bi->word_no];
248 bi->bits >>= start_bit % BITMAP_WORD_BITS;
249
250 /* If this word is zero, we must make sure we're not pointing at the
251 first bit, otherwise our incrementing to the next word boundary
252 will fail. It won't matter if this increment moves us into the
253 next word. */
254 start_bit += !bi->bits;
255
256 *bit_no = start_bit;
257 }
258
259 /* Initialize an iterator to iterate over the intersection of two
260 bitmaps. START_BIT is the bit to commence from. */
261
262 static inline void
263 bmp_iter_and_init (bitmap_iterator *bi, const_bitmap map1, const_bitmap map2,
264 unsigned start_bit, unsigned *bit_no)
265 {
266 bi->elt1 = map1->first;
267 bi->elt2 = map2->first;
268
269 /* Advance elt1 until it is not before the block containing
270 start_bit. */
271 while (1)
272 {
273 if (!bi->elt1)
274 {
275 bi->elt2 = NULL;
276 break;
277 }
278
279 if (bi->elt1->indx >= start_bit / BITMAP_ELEMENT_ALL_BITS)
280 break;
281 bi->elt1 = bi->elt1->next;
282 }
283
284 /* Advance elt2 until it is not before elt1. */
285 while (1)
286 {
287 if (!bi->elt2)
288 {
289 bi->elt1 = bi->elt2 = &bitmap_zero_bits;
290 break;
291 }
292
293 if (bi->elt2->indx >= bi->elt1->indx)
294 break;
295 bi->elt2 = bi->elt2->next;
296 }
297
298 /* If we're at the same index, then we have some intersecting bits. */
299 if (bi->elt1->indx == bi->elt2->indx)
300 {
301 /* We might have advanced beyond the start_bit, so reinitialize
302 for that. */
303 if (bi->elt1->indx != start_bit / BITMAP_ELEMENT_ALL_BITS)
304 start_bit = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
305
306 bi->word_no = start_bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
307 bi->bits = bi->elt1->bits[bi->word_no] & bi->elt2->bits[bi->word_no];
308 bi->bits >>= start_bit % BITMAP_WORD_BITS;
309 }
310 else
311 {
312 /* Otherwise we must immediately advance elt1, so initialize for
313 that. */
314 bi->word_no = BITMAP_ELEMENT_WORDS - 1;
315 bi->bits = 0;
316 }
317
318 /* If this word is zero, we must make sure we're not pointing at the
319 first bit, otherwise our incrementing to the next word boundary
320 will fail. It won't matter if this increment moves us into the
321 next word. */
322 start_bit += !bi->bits;
323
324 *bit_no = start_bit;
325 }
326
327 /* Initialize an iterator to iterate over the bits in MAP1 & ~MAP2.
328 */
329
330 static inline void
331 bmp_iter_and_compl_init (bitmap_iterator *bi, const_bitmap map1, const_bitmap map2,
332 unsigned start_bit, unsigned *bit_no)
333 {
334 bi->elt1 = map1->first;
335 bi->elt2 = map2->first;
336
337 /* Advance elt1 until it is not before the block containing start_bit. */
338 while (1)
339 {
340 if (!bi->elt1)
341 {
342 bi->elt1 = &bitmap_zero_bits;
343 break;
344 }
345
346 if (bi->elt1->indx >= start_bit / BITMAP_ELEMENT_ALL_BITS)
347 break;
348 bi->elt1 = bi->elt1->next;
349 }
350
351 /* Advance elt2 until it is not before elt1. */
352 while (bi->elt2 && bi->elt2->indx < bi->elt1->indx)
353 bi->elt2 = bi->elt2->next;
354
355 /* We might have advanced beyond the start_bit, so reinitialize for
356 that. */
357 if (bi->elt1->indx != start_bit / BITMAP_ELEMENT_ALL_BITS)
358 start_bit = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
359
360 bi->word_no = start_bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
361 bi->bits = bi->elt1->bits[bi->word_no];
362 if (bi->elt2 && bi->elt1->indx == bi->elt2->indx)
363 bi->bits &= ~bi->elt2->bits[bi->word_no];
364 bi->bits >>= start_bit % BITMAP_WORD_BITS;
365
366 /* If this word is zero, we must make sure we're not pointing at the
367 first bit, otherwise our incrementing to the next word boundary
368 will fail. It won't matter if this increment moves us into the
369 next word. */
370 start_bit += !bi->bits;
371
372 *bit_no = start_bit;
373 }
374
375 /* Advance to the next bit in BI. We don't advance to the next
376 nonzero bit yet. */
377
378 static inline void
379 bmp_iter_next (bitmap_iterator *bi, unsigned *bit_no)
380 {
381 bi->bits >>= 1;
382 *bit_no += 1;
383 }
384
385 /* Advance to first set bit in BI. */
386
387 static inline void
388 bmp_iter_next_bit (bitmap_iterator * bi, unsigned *bit_no)
389 {
390 #if (GCC_VERSION >= 3004)
391 {
392 unsigned int n = __builtin_ctzl (bi->bits);
393 gcc_assert (sizeof (unsigned long) == sizeof (BITMAP_WORD));
394 bi->bits >>= n;
395 *bit_no += n;
396 }
397 #else
398 while (!(bi->bits & 1))
399 {
400 bi->bits >>= 1;
401 *bit_no += 1;
402 }
403 #endif
404 }
405
406 /* Advance to the next nonzero bit of a single bitmap, we will have
407 already advanced past the just iterated bit. Return true if there
408 is a bit to iterate. */
409
410 static inline bool
411 bmp_iter_set (bitmap_iterator *bi, unsigned *bit_no)
412 {
413 /* If our current word is nonzero, it contains the bit we want. */
414 if (bi->bits)
415 {
416 next_bit:
417 bmp_iter_next_bit (bi, bit_no);
418 return true;
419 }
420
421 /* Round up to the word boundary. We might have just iterated past
422 the end of the last word, hence the -1. It is not possible for
423 bit_no to point at the beginning of the now last word. */
424 *bit_no = ((*bit_no + BITMAP_WORD_BITS - 1)
425 / BITMAP_WORD_BITS * BITMAP_WORD_BITS);
426 bi->word_no++;
427
428 while (1)
429 {
430 /* Find the next nonzero word in this elt. */
431 while (bi->word_no != BITMAP_ELEMENT_WORDS)
432 {
433 bi->bits = bi->elt1->bits[bi->word_no];
434 if (bi->bits)
435 goto next_bit;
436 *bit_no += BITMAP_WORD_BITS;
437 bi->word_no++;
438 }
439
440 /* Advance to the next element. */
441 bi->elt1 = bi->elt1->next;
442 if (!bi->elt1)
443 return false;
444 *bit_no = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
445 bi->word_no = 0;
446 }
447 }
448
449 /* Advance to the next nonzero bit of an intersecting pair of
450 bitmaps. We will have already advanced past the just iterated bit.
451 Return true if there is a bit to iterate. */
452
453 static inline bool
454 bmp_iter_and (bitmap_iterator *bi, unsigned *bit_no)
455 {
456 /* If our current word is nonzero, it contains the bit we want. */
457 if (bi->bits)
458 {
459 next_bit:
460 bmp_iter_next_bit (bi, bit_no);
461 return true;
462 }
463
464 /* Round up to the word boundary. We might have just iterated past
465 the end of the last word, hence the -1. It is not possible for
466 bit_no to point at the beginning of the now last word. */
467 *bit_no = ((*bit_no + BITMAP_WORD_BITS - 1)
468 / BITMAP_WORD_BITS * BITMAP_WORD_BITS);
469 bi->word_no++;
470
471 while (1)
472 {
473 /* Find the next nonzero word in this elt. */
474 while (bi->word_no != BITMAP_ELEMENT_WORDS)
475 {
476 bi->bits = bi->elt1->bits[bi->word_no] & bi->elt2->bits[bi->word_no];
477 if (bi->bits)
478 goto next_bit;
479 *bit_no += BITMAP_WORD_BITS;
480 bi->word_no++;
481 }
482
483 /* Advance to the next identical element. */
484 do
485 {
486 /* Advance elt1 while it is less than elt2. We always want
487 to advance one elt. */
488 do
489 {
490 bi->elt1 = bi->elt1->next;
491 if (!bi->elt1)
492 return false;
493 }
494 while (bi->elt1->indx < bi->elt2->indx);
495
496 /* Advance elt2 to be no less than elt1. This might not
497 advance. */
498 while (bi->elt2->indx < bi->elt1->indx)
499 {
500 bi->elt2 = bi->elt2->next;
501 if (!bi->elt2)
502 return false;
503 }
504 }
505 while (bi->elt1->indx != bi->elt2->indx);
506
507 *bit_no = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
508 bi->word_no = 0;
509 }
510 }
511
512 /* Advance to the next nonzero bit in the intersection of
513 complemented bitmaps. We will have already advanced past the just
514 iterated bit. */
515
516 static inline bool
517 bmp_iter_and_compl (bitmap_iterator *bi, unsigned *bit_no)
518 {
519 /* If our current word is nonzero, it contains the bit we want. */
520 if (bi->bits)
521 {
522 next_bit:
523 bmp_iter_next_bit (bi, bit_no);
524 return true;
525 }
526
527 /* Round up to the word boundary. We might have just iterated past
528 the end of the last word, hence the -1. It is not possible for
529 bit_no to point at the beginning of the now last word. */
530 *bit_no = ((*bit_no + BITMAP_WORD_BITS - 1)
531 / BITMAP_WORD_BITS * BITMAP_WORD_BITS);
532 bi->word_no++;
533
534 while (1)
535 {
536 /* Find the next nonzero word in this elt. */
537 while (bi->word_no != BITMAP_ELEMENT_WORDS)
538 {
539 bi->bits = bi->elt1->bits[bi->word_no];
540 if (bi->elt2 && bi->elt2->indx == bi->elt1->indx)
541 bi->bits &= ~bi->elt2->bits[bi->word_no];
542 if (bi->bits)
543 goto next_bit;
544 *bit_no += BITMAP_WORD_BITS;
545 bi->word_no++;
546 }
547
548 /* Advance to the next element of elt1. */
549 bi->elt1 = bi->elt1->next;
550 if (!bi->elt1)
551 return false;
552
553 /* Advance elt2 until it is no less than elt1. */
554 while (bi->elt2 && bi->elt2->indx < bi->elt1->indx)
555 bi->elt2 = bi->elt2->next;
556
557 *bit_no = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
558 bi->word_no = 0;
559 }
560 }
561
562 /* Loop over all bits set in BITMAP, starting with MIN and setting
563 BITNUM to the bit number. ITER is a bitmap iterator. BITNUM
564 should be treated as a read-only variable as it contains loop
565 state. */
566
567 #define EXECUTE_IF_SET_IN_BITMAP(BITMAP, MIN, BITNUM, ITER) \
568 for (bmp_iter_set_init (&(ITER), (BITMAP), (MIN), &(BITNUM)); \
569 bmp_iter_set (&(ITER), &(BITNUM)); \
570 bmp_iter_next (&(ITER), &(BITNUM)))
571
572 /* Loop over all the bits set in BITMAP1 & BITMAP2, starting with MIN
573 and setting BITNUM to the bit number. ITER is a bitmap iterator.
574 BITNUM should be treated as a read-only variable as it contains
575 loop state. */
576
577 #define EXECUTE_IF_AND_IN_BITMAP(BITMAP1, BITMAP2, MIN, BITNUM, ITER) \
578 for (bmp_iter_and_init (&(ITER), (BITMAP1), (BITMAP2), (MIN), \
579 &(BITNUM)); \
580 bmp_iter_and (&(ITER), &(BITNUM)); \
581 bmp_iter_next (&(ITER), &(BITNUM)))
582
583 /* Loop over all the bits set in BITMAP1 & ~BITMAP2, starting with MIN
584 and setting BITNUM to the bit number. ITER is a bitmap iterator.
585 BITNUM should be treated as a read-only variable as it contains
586 loop state. */
587
588 #define EXECUTE_IF_AND_COMPL_IN_BITMAP(BITMAP1, BITMAP2, MIN, BITNUM, ITER) \
589 for (bmp_iter_and_compl_init (&(ITER), (BITMAP1), (BITMAP2), (MIN), \
590 &(BITNUM)); \
591 bmp_iter_and_compl (&(ITER), &(BITNUM)); \
592 bmp_iter_next (&(ITER), &(BITNUM)))
593
594 #endif /* GCC_BITMAP_H */