OSDN Git Service

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