OSDN Git Service

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