* tree-ssa-loop-prefetch.c (determine_unroll_factor): Bound the unroll
[official-gcc.git] / gcc / bitmap.h
blob515527c08e8fcd0e7d3d96e1513f4bd000c546f1
1 /* Functions to support general ended bitmaps.
2 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
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, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, USA. */
22 #ifndef GCC_BITMAP_H
23 #define GCC_BITMAP_H
24 #include "hashtab.h"
25 #include "statistics.h"
27 /* Fundamental storage type for bitmap. */
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)
34 /* Number of words to use for each element in the linked list. */
36 #ifndef BITMAP_ELEMENT_WORDS
37 #define BITMAP_ELEMENT_WORDS ((128 + BITMAP_WORD_BITS - 1) / BITMAP_WORD_BITS)
38 #endif
40 /* Number of bits in each actual element of a bitmap. */
42 #define BITMAP_ELEMENT_ALL_BITS (BITMAP_ELEMENT_WORDS * BITMAP_WORD_BITS)
44 /* Obstack for allocating bitmaps and elements from. */
45 typedef struct bitmap_obstack GTY (())
47 struct bitmap_element_def *elements;
48 struct bitmap_head_def *heads;
49 struct obstack GTY ((skip)) obstack;
50 } bitmap_obstack;
52 /* Bitmap set element. We use a linked list to hold only the bits that
53 are set. This allows for use to grow the bitset dynamically without
54 having to realloc and copy a giant bit array.
56 The free list is implemented as a list of lists. There is one
57 outer list connected together by prev fields. Each element of that
58 outer is an inner list (that may consist only of the outer list
59 element) that are connected by the next fields. The prev pointer
60 is undefined for interior elements. This allows
61 bitmap_elt_clear_from to be implemented in unit time rather than
62 linear in the number of elements to be freed. */
64 typedef struct bitmap_element_def GTY(())
66 struct bitmap_element_def *next; /* Next element. */
67 struct bitmap_element_def *prev; /* Previous element. */
68 unsigned int indx; /* regno/BITMAP_ELEMENT_ALL_BITS. */
69 BITMAP_WORD bits[BITMAP_ELEMENT_WORDS]; /* Bits that are set. */
70 } bitmap_element;
72 struct bitmap_descriptor;
73 /* Head of bitmap linked list.
74 The gengtype doesn't cope with ifdefs inside the definition,
75 but for statistics we need bitmap descriptor pointer in.
76 Trick it by two copies of the definition. This is safe
77 because the bitmap descriptor is not grabagecollected. */
78 #ifndef GATHER_STATISTICS
79 typedef struct bitmap_head_def GTY(()) {
80 bitmap_element *first; /* First element in linked list. */
81 bitmap_element *current; /* Last element looked at. */
82 unsigned int indx; /* Index of last element looked at. */
83 bitmap_obstack *obstack; /* Obstack to allocate elements from.
84 If NULL, then use ggc_alloc. */
85 } bitmap_head;
86 #else
87 typedef struct bitmap_head_def {
88 bitmap_element *first; /* First element in linked list. */
89 bitmap_element *current; /* Last element looked at. */
90 unsigned int indx; /* Index of last element looked at. */
91 bitmap_obstack *obstack; /* Obstack to allocate elements from.
92 If NULL, then use ggc_alloc. */
93 struct bitmap_descriptor *desc;
94 } bitmap_head;
95 #endif
97 /* Global data */
98 extern bitmap_element bitmap_zero_bits; /* Zero bitmap element */
99 extern bitmap_obstack bitmap_default_obstack; /* Default bitmap obstack */
101 /* Clear a bitmap by freeing up the linked list. */
102 extern void bitmap_clear (bitmap);
104 /* Copy a bitmap to another bitmap. */
105 extern void bitmap_copy (bitmap, bitmap);
107 /* True if two bitmaps are identical. */
108 extern bool bitmap_equal_p (bitmap, bitmap);
110 /* True if the bitmaps intersect (their AND is non-empty). */
111 extern bool bitmap_intersect_p (bitmap, bitmap);
113 /* True if the complement of the second intersects the first (their
114 AND_COMPL is non-empty). */
115 extern bool bitmap_intersect_compl_p (bitmap, bitmap);
117 /* True if MAP is an empty bitmap. */
118 #define bitmap_empty_p(MAP) (!(MAP)->first)
120 /* Count the number of bits set in the bitmap. */
121 extern unsigned long bitmap_count_bits (bitmap);
123 /* Boolean operations on bitmaps. The _into variants are two operand
124 versions that modify the first source operand. The other variants
125 are three operand versions that to not destroy the source bitmaps.
126 The operations supported are &, & ~, |, ^. */
127 extern void bitmap_and (bitmap, bitmap, bitmap);
128 extern void bitmap_and_into (bitmap, bitmap);
129 extern void bitmap_and_compl (bitmap, bitmap, bitmap);
130 extern bool bitmap_and_compl_into (bitmap, bitmap);
131 #define bitmap_compl_and(DST, A, B) bitmap_and_compl (DST, B, A)
132 extern void bitmap_compl_and_into (bitmap, bitmap);
133 extern void bitmap_clear_range (bitmap, unsigned int, unsigned int);
134 extern bool bitmap_ior (bitmap, bitmap, bitmap);
135 extern bool bitmap_ior_into (bitmap, bitmap);
136 extern void bitmap_xor (bitmap, bitmap, bitmap);
137 extern void bitmap_xor_into (bitmap, bitmap);
139 /* DST = A | (B & ~C). Return true if DST changes. */
140 extern bool bitmap_ior_and_compl (bitmap DST, bitmap A, bitmap B, bitmap C);
141 /* A |= (B & ~C). Return true if A changes. */
142 extern bool bitmap_ior_and_compl_into (bitmap DST, bitmap B, bitmap C);
144 /* Clear a single register in a register set. */
145 extern void bitmap_clear_bit (bitmap, int);
147 /* Set a single register in a register set. */
148 extern void bitmap_set_bit (bitmap, int);
150 /* Return true if a register is set in a register set. */
151 extern int bitmap_bit_p (bitmap, int);
153 /* Debug functions to print a bitmap linked list. */
154 extern void debug_bitmap (bitmap);
155 extern void debug_bitmap_file (FILE *, bitmap);
157 /* Print a bitmap. */
158 extern void bitmap_print (FILE *, bitmap, const char *, const char *);
160 /* Initialize and release a bitmap obstack. */
161 extern void bitmap_obstack_initialize (bitmap_obstack *);
162 extern void bitmap_obstack_release (bitmap_obstack *);
163 extern void bitmap_register (bitmap MEM_STAT_DECL);
164 extern void dump_bitmap_statistics (void);
166 /* Initialize a bitmap header. OBSTACK indicates the bitmap obstack
167 to allocate from, NULL for GC'd bitmap. */
169 static inline void
170 bitmap_initialize_stat (bitmap head, bitmap_obstack *obstack MEM_STAT_DECL)
172 head->first = head->current = NULL;
173 head->obstack = obstack;
174 #ifdef GATHER_STATISTICS
175 bitmap_register (head PASS_MEM_STAT);
176 #endif
178 #define bitmap_initialize(h,o) bitmap_initialize_stat (h,o MEM_STAT_INFO)
180 /* Allocate and free bitmaps from obstack, malloc and gc'd memory. */
181 extern bitmap bitmap_obstack_alloc_stat (bitmap_obstack *obstack MEM_STAT_DECL);
182 #define bitmap_obstack_alloc(t) bitmap_obstack_alloc_stat (t MEM_STAT_INFO)
183 extern bitmap bitmap_gc_alloc_stat (ALONE_MEM_STAT_DECL);
184 #define bitmap_gc_alloc() bitmap_gc_alloc_stat (ALONE_MEM_STAT_INFO)
185 extern void bitmap_obstack_free (bitmap);
187 /* A few compatibility/functions macros for compatibility with sbitmaps */
188 #define dump_bitmap(file, bitmap) bitmap_print (file, bitmap, "", "\n")
189 #define bitmap_zero(a) bitmap_clear (a)
190 extern unsigned bitmap_first_set_bit (bitmap);
192 /* Compute bitmap hash (for purposes of hashing etc.) */
193 extern hashval_t bitmap_hash(bitmap);
195 /* Allocate a bitmap from a bit obstack. */
196 #define BITMAP_ALLOC(OBSTACK) bitmap_obstack_alloc (OBSTACK)
198 /* Allocate a gc'd bitmap. */
199 #define BITMAP_GGC_ALLOC() bitmap_gc_alloc ()
201 /* Do any cleanup needed on a bitmap when it is no longer used. */
202 #define BITMAP_FREE(BITMAP) \
203 ((void)(bitmap_obstack_free (BITMAP), (BITMAP) = NULL))
205 /* Iterator for bitmaps. */
207 typedef struct
209 /* Pointer to the current bitmap element. */
210 bitmap_element *elt1;
212 /* Pointer to 2nd bitmap element when two are involved. */
213 bitmap_element *elt2;
215 /* Word within the current element. */
216 unsigned word_no;
218 /* Contents of the actually processed word. When finding next bit
219 it is shifted right, so that the actual bit is always the least
220 significant bit of ACTUAL. */
221 BITMAP_WORD bits;
222 } bitmap_iterator;
224 /* Initialize a single bitmap iterator. START_BIT is the first bit to
225 iterate from. */
227 static inline void
228 bmp_iter_set_init (bitmap_iterator *bi, bitmap map,
229 unsigned start_bit, unsigned *bit_no)
231 bi->elt1 = map->first;
232 bi->elt2 = NULL;
234 /* Advance elt1 until it is not before the block containing start_bit. */
235 while (1)
237 if (!bi->elt1)
239 bi->elt1 = &bitmap_zero_bits;
240 break;
243 if (bi->elt1->indx >= start_bit / BITMAP_ELEMENT_ALL_BITS)
244 break;
245 bi->elt1 = bi->elt1->next;
248 /* We might have gone past the start bit, so reinitialize it. */
249 if (bi->elt1->indx != start_bit / BITMAP_ELEMENT_ALL_BITS)
250 start_bit = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
252 /* Initialize for what is now start_bit. */
253 bi->word_no = start_bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
254 bi->bits = bi->elt1->bits[bi->word_no];
255 bi->bits >>= start_bit % BITMAP_WORD_BITS;
257 /* If this word is zero, we must make sure we're not pointing at the
258 first bit, otherwise our incrementing to the next word boundary
259 will fail. It won't matter if this increment moves us into the
260 next word. */
261 start_bit += !bi->bits;
263 *bit_no = start_bit;
266 /* Initialize an iterator to iterate over the intersection of two
267 bitmaps. START_BIT is the bit to commence from. */
269 static inline void
270 bmp_iter_and_init (bitmap_iterator *bi, bitmap map1, bitmap map2,
271 unsigned start_bit, unsigned *bit_no)
273 bi->elt1 = map1->first;
274 bi->elt2 = map2->first;
276 /* Advance elt1 until it is not before the block containing
277 start_bit. */
278 while (1)
280 if (!bi->elt1)
282 bi->elt2 = NULL;
283 break;
286 if (bi->elt1->indx >= start_bit / BITMAP_ELEMENT_ALL_BITS)
287 break;
288 bi->elt1 = bi->elt1->next;
291 /* Advance elt2 until it is not before elt1. */
292 while (1)
294 if (!bi->elt2)
296 bi->elt1 = bi->elt2 = &bitmap_zero_bits;
297 break;
300 if (bi->elt2->indx >= bi->elt1->indx)
301 break;
302 bi->elt2 = bi->elt2->next;
305 /* If we're at the same index, then we have some intersecting bits. */
306 if (bi->elt1->indx == bi->elt2->indx)
308 /* We might have advanced beyond the start_bit, so reinitialize
309 for that. */
310 if (bi->elt1->indx != start_bit / BITMAP_ELEMENT_ALL_BITS)
311 start_bit = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
313 bi->word_no = start_bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
314 bi->bits = bi->elt1->bits[bi->word_no] & bi->elt2->bits[bi->word_no];
315 bi->bits >>= start_bit % BITMAP_WORD_BITS;
317 else
319 /* Otherwise we must immediately advance elt1, so initialize for
320 that. */
321 bi->word_no = BITMAP_ELEMENT_WORDS - 1;
322 bi->bits = 0;
325 /* If this word is zero, we must make sure we're not pointing at the
326 first bit, otherwise our incrementing to the next word boundary
327 will fail. It won't matter if this increment moves us into the
328 next word. */
329 start_bit += !bi->bits;
331 *bit_no = start_bit;
334 /* Initialize an iterator to iterate over the bits in MAP1 & ~MAP2.
337 static inline void
338 bmp_iter_and_compl_init (bitmap_iterator *bi, bitmap map1, bitmap map2,
339 unsigned start_bit, unsigned *bit_no)
341 bi->elt1 = map1->first;
342 bi->elt2 = map2->first;
344 /* Advance elt1 until it is not before the block containing start_bit. */
345 while (1)
347 if (!bi->elt1)
349 bi->elt1 = &bitmap_zero_bits;
350 break;
353 if (bi->elt1->indx >= start_bit / BITMAP_ELEMENT_ALL_BITS)
354 break;
355 bi->elt1 = bi->elt1->next;
358 /* Advance elt2 until it is not before elt1. */
359 while (bi->elt2 && bi->elt2->indx < bi->elt1->indx)
360 bi->elt2 = bi->elt2->next;
362 /* We might have advanced beyond the start_bit, so reinitialize for
363 that. */
364 if (bi->elt1->indx != start_bit / BITMAP_ELEMENT_ALL_BITS)
365 start_bit = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
367 bi->word_no = start_bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
368 bi->bits = bi->elt1->bits[bi->word_no];
369 if (bi->elt2 && bi->elt1->indx == bi->elt2->indx)
370 bi->bits &= ~bi->elt2->bits[bi->word_no];
371 bi->bits >>= start_bit % BITMAP_WORD_BITS;
373 /* If this word is zero, we must make sure we're not pointing at the
374 first bit, otherwise our incrementing to the next word boundary
375 will fail. It won't matter if this increment moves us into the
376 next word. */
377 start_bit += !bi->bits;
379 *bit_no = start_bit;
382 /* Advance to the next bit in BI. We don't advance to the next
383 nonzero bit yet. */
385 static inline void
386 bmp_iter_next (bitmap_iterator *bi, unsigned *bit_no)
388 bi->bits >>= 1;
389 *bit_no += 1;
392 /* Advance to the next nonzero bit of a single bitmap, we will have
393 already advanced past the just iterated bit. Return true if there
394 is a bit to iterate. */
396 static inline bool
397 bmp_iter_set (bitmap_iterator *bi, unsigned *bit_no)
399 /* If our current word is nonzero, it contains the bit we want. */
400 if (bi->bits)
402 next_bit:
403 while (!(bi->bits & 1))
405 bi->bits >>= 1;
406 *bit_no += 1;
408 return true;
411 /* Round up to the word boundary. We might have just iterated past
412 the end of the last word, hence the -1. It is not possible for
413 bit_no to point at the beginning of the now last word. */
414 *bit_no = ((*bit_no + BITMAP_WORD_BITS - 1)
415 / BITMAP_WORD_BITS * BITMAP_WORD_BITS);
416 bi->word_no++;
418 while (1)
420 /* Find the next nonzero word in this elt. */
421 while (bi->word_no != BITMAP_ELEMENT_WORDS)
423 bi->bits = bi->elt1->bits[bi->word_no];
424 if (bi->bits)
425 goto next_bit;
426 *bit_no += BITMAP_WORD_BITS;
427 bi->word_no++;
430 /* Advance to the next element. */
431 bi->elt1 = bi->elt1->next;
432 if (!bi->elt1)
433 return false;
434 *bit_no = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
435 bi->word_no = 0;
439 /* Advance to the next nonzero bit of an intersecting pair of
440 bitmaps. We will have already advanced past the just iterated bit.
441 Return true if there is a bit to iterate. */
443 static inline bool
444 bmp_iter_and (bitmap_iterator *bi, unsigned *bit_no)
446 /* If our current word is nonzero, it contains the bit we want. */
447 if (bi->bits)
449 next_bit:
450 while (!(bi->bits & 1))
452 bi->bits >>= 1;
453 *bit_no += 1;
455 return true;
458 /* Round up to the word boundary. We might have just iterated past
459 the end of the last word, hence the -1. It is not possible for
460 bit_no to point at the beginning of the now last word. */
461 *bit_no = ((*bit_no + BITMAP_WORD_BITS - 1)
462 / BITMAP_WORD_BITS * BITMAP_WORD_BITS);
463 bi->word_no++;
465 while (1)
467 /* Find the next nonzero word in this elt. */
468 while (bi->word_no != BITMAP_ELEMENT_WORDS)
470 bi->bits = bi->elt1->bits[bi->word_no] & bi->elt2->bits[bi->word_no];
471 if (bi->bits)
472 goto next_bit;
473 *bit_no += BITMAP_WORD_BITS;
474 bi->word_no++;
477 /* Advance to the next identical element. */
480 /* Advance elt1 while it is less than elt2. We always want
481 to advance one elt. */
484 bi->elt1 = bi->elt1->next;
485 if (!bi->elt1)
486 return false;
488 while (bi->elt1->indx < bi->elt2->indx);
490 /* Advance elt2 to be no less than elt1. This might not
491 advance. */
492 while (bi->elt2->indx < bi->elt1->indx)
494 bi->elt2 = bi->elt2->next;
495 if (!bi->elt2)
496 return false;
499 while (bi->elt1->indx != bi->elt2->indx);
501 *bit_no = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
502 bi->word_no = 0;
506 /* Advance to the next nonzero bit in the intersection of
507 complemented bitmaps. We will have already advanced past the just
508 iterated bit. */
510 static inline bool
511 bmp_iter_and_compl (bitmap_iterator *bi, unsigned *bit_no)
513 /* If our current word is nonzero, it contains the bit we want. */
514 if (bi->bits)
516 next_bit:
517 while (!(bi->bits & 1))
519 bi->bits >>= 1;
520 *bit_no += 1;
522 return true;
525 /* Round up to the word boundary. We might have just iterated past
526 the end of the last word, hence the -1. It is not possible for
527 bit_no to point at the beginning of the now last word. */
528 *bit_no = ((*bit_no + BITMAP_WORD_BITS - 1)
529 / BITMAP_WORD_BITS * BITMAP_WORD_BITS);
530 bi->word_no++;
532 while (1)
534 /* Find the next nonzero word in this elt. */
535 while (bi->word_no != BITMAP_ELEMENT_WORDS)
537 bi->bits = bi->elt1->bits[bi->word_no];
538 if (bi->elt2 && bi->elt2->indx == bi->elt1->indx)
539 bi->bits &= ~bi->elt2->bits[bi->word_no];
540 if (bi->bits)
541 goto next_bit;
542 *bit_no += BITMAP_WORD_BITS;
543 bi->word_no++;
546 /* Advance to the next element of elt1. */
547 bi->elt1 = bi->elt1->next;
548 if (!bi->elt1)
549 return false;
551 /* Advance elt2 until it is no less than elt1. */
552 while (bi->elt2 && bi->elt2->indx < bi->elt1->indx)
553 bi->elt2 = bi->elt2->next;
555 *bit_no = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
556 bi->word_no = 0;
560 /* Loop over all bits set in BITMAP, starting with MIN and setting
561 BITNUM to the bit number. ITER is a bitmap iterator. BITNUM
562 should be treated as a read-only variable as it contains loop
563 state. */
565 #define EXECUTE_IF_SET_IN_BITMAP(BITMAP, MIN, BITNUM, ITER) \
566 for (bmp_iter_set_init (&(ITER), (BITMAP), (MIN), &(BITNUM)); \
567 bmp_iter_set (&(ITER), &(BITNUM)); \
568 bmp_iter_next (&(ITER), &(BITNUM)))
570 /* Loop over all the bits set in BITMAP1 & BITMAP2, starting with MIN
571 and setting BITNUM to the bit number. ITER is a bitmap iterator.
572 BITNUM should be treated as a read-only variable as it contains
573 loop state. */
575 #define EXECUTE_IF_AND_IN_BITMAP(BITMAP1, BITMAP2, MIN, BITNUM, ITER) \
576 for (bmp_iter_and_init (&(ITER), (BITMAP1), (BITMAP2), (MIN), \
577 &(BITNUM)); \
578 bmp_iter_and (&(ITER), &(BITNUM)); \
579 bmp_iter_next (&(ITER), &(BITNUM)))
581 /* Loop over all the bits set in BITMAP1 & ~BITMAP2, starting with MIN
582 and setting BITNUM to the bit number. ITER is a bitmap iterator.
583 BITNUM should be treated as a read-only variable as it contains
584 loop state. */
586 #define EXECUTE_IF_AND_COMPL_IN_BITMAP(BITMAP1, BITMAP2, MIN, BITNUM, ITER) \
587 for (bmp_iter_and_compl_init (&(ITER), (BITMAP1), (BITMAP2), (MIN), \
588 &(BITNUM)); \
589 bmp_iter_and_compl (&(ITER), &(BITNUM)); \
590 bmp_iter_next (&(ITER), &(BITNUM)))
592 #endif /* GCC_BITMAP_H */