OSDN Git Service

2004-11-03 Andrew MacLeod <amacleod@redhat.com>
[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
3    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, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.  */
21
22 #ifndef GCC_BITMAP_H
23 #define GCC_BITMAP_H
24
25 /* Fundamental storage type for bitmap.  */
26
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
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 + nBITMAP_WORD_BITS - 1) / nBITMAP_WORD_BITS)
37 #endif
38
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.  */
42
43 #define BITMAP_ELEMENT_ALL_BITS \
44   ((unsigned) (BITMAP_ELEMENT_WORDS * BITMAP_WORD_BITS))
45
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.  */
50
51 typedef struct bitmap_element_def GTY(())
52 {
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;
58
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;
68
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 */
76 };
77
78 /* Global data */
79 extern bitmap_element bitmap_zero_bits; /* Zero bitmap element */
80
81 /* Clear a bitmap by freeing up the linked list.  */
82 extern void bitmap_clear (bitmap);
83
84 /* Copy a bitmap to another bitmap.  */
85 extern void bitmap_copy (bitmap, bitmap);
86
87 /* True if two bitmaps are identical.  */
88 extern bool bitmap_equal_p (bitmap, bitmap);
89
90 /* True if the bitmaps intersect (their AND is non-empty).  */
91 extern bool bitmap_intersect_p (bitmap, bitmap);
92
93 /* True if the complement of the second intersects the first (their
94    AND_COMPL is non-empty).  */
95 extern bool bitmap_intersect_compl_p (bitmap, bitmap);
96
97 /* True if MAP is an empty bitmap.  */
98 #define bitmap_empty_p(MAP) (!(MAP)->first)
99
100 /* Perform an operation on two bitmaps, yielding a third.  */
101 extern int bitmap_operation (bitmap, bitmap, bitmap, enum bitmap_bits);
102
103 #define bitmap_and(DST,A,B) (void)bitmap_operation (DST,A,B,BITMAP_AND)
104 #define bitmap_and_into(DST_SRC,B) (void)bitmap_operation (DST_SRC,DST_SRC,B,BITMAP_AND)
105 #define bitmap_and_compl(DST,A,B) (void)bitmap_operation (DST,A,B,BITMAP_AND_COMPL)
106 #define bitmap_and_compl_into(DST_SRC,B) (void)bitmap_operation (DST_SRC,DST_SRC,B,BITMAP_AND_COMPL)
107 #define bitmap_ior(DST,A,B) (void)bitmap_operation (DST,A,B,BITMAP_IOR)
108 #define bitmap_ior_into(DST_SRC,B) (void)bitmap_operation (DST_SRC,DST_SRC,B,BITMAP_IOR)
109 #define bitmap_ior_compl(DST,A,B) (void)bitmap_operation (DST,A,Br,BITMAP_IOR_COMPL)
110 #define bitmap_xor(DST,A,B) (void)bitmap_operation (DST,A,B,BITMAP_XOR)
111 #define bitmap_xor_into(DST_SRC,B) (void)bitmap_operation (DST_SRC,DST_SRC,B,BITMAP_XOR)
112
113 /* `or' into one bitmap the `and' of a second bitmap witih the complement
114    of a third. Return nonzero if the bitmap changes.  */
115 extern bool bitmap_ior_and_compl_into (bitmap, bitmap, bitmap);
116 extern bool bitmap_ior_and_compl (bitmap, bitmap, bitmap, bitmap);
117
118 /* Clear a single register in a register set.  */
119 extern void bitmap_clear_bit (bitmap, int);
120
121 /* Set a single register in a register set.  */
122 extern void bitmap_set_bit (bitmap, int);
123
124 /* Return true if a register is set in a register set.  */
125 extern int bitmap_bit_p (bitmap, int);
126
127 /* Debug functions to print a bitmap linked list.  */
128 extern void debug_bitmap (bitmap);
129 extern void debug_bitmap_file (FILE *, bitmap);
130
131 /* Print a bitmap.  */
132 extern void bitmap_print (FILE *, bitmap, const char *, const char *);
133
134 /* Initialize a bitmap header.  If HEAD is NULL, a new header will be
135    allocated.  USING_OBSTACK indicates how elements should be allocated.  */
136 extern bitmap bitmap_initialize (bitmap head, int using_obstack);
137
138 /* Release all memory used by the bitmap obstack.  */
139 extern void bitmap_release_memory (void);
140
141 /* A few compatibility/functions macros for compatibility with sbitmaps */
142 #define dump_bitmap(file, bitmap) bitmap_print (file, bitmap, "", "\n")
143 #define bitmap_zero(a) bitmap_clear (a)
144 #define bitmap_a_or_b(a,b,c) bitmap_operation (a, b, c, BITMAP_IOR)
145 #define bitmap_a_and_b(a,b,c) bitmap_operation (a, b, c, BITMAP_AND)
146 extern int bitmap_first_set_bit (bitmap);
147 extern int bitmap_last_set_bit (bitmap);
148
149 /* Allocate a bitmap with oballoc.  */
150 #define BITMAP_OBSTACK_ALLOC(OBSTACK)                           \
151   bitmap_initialize (obstack_alloc (OBSTACK, sizeof (bitmap_head)), 1)
152
153 /* Allocate a bitmap with ggc_alloc.  */
154 #define BITMAP_GGC_ALLOC()                      \
155   bitmap_initialize (NULL, 0)
156
157 /* Allocate a bitmap with xmalloc.  */
158 #define BITMAP_XMALLOC()                                        \
159   bitmap_initialize (xmalloc (sizeof (bitmap_head)), 1)
160
161 /* Do any cleanup needed on a bitmap when it is no longer used.  */
162 #define BITMAP_FREE(BITMAP)                     \
163 do {                                            \
164   if (BITMAP)                                   \
165     {                                           \
166       bitmap_clear (BITMAP);                    \
167       (BITMAP) = 0;                             \
168     }                                           \
169 } while (0)
170
171 /* Do any cleanup needed on an xmalloced bitmap when it is no longer used.  */
172 #define BITMAP_XFREE(BITMAP)                    \
173 do {                                            \
174   if (BITMAP)                                   \
175     {                                           \
176       bitmap_clear (BITMAP);                    \
177       free (BITMAP);                            \
178       (BITMAP) = 0;                             \
179     }                                           \
180 } while (0)
181
182 /* Do any one-time initializations needed for bitmaps.  */
183 #define BITMAP_INIT_ONCE()
184
185 /* Iterator for bitmaps.  */
186
187 typedef struct
188 {
189   /* Actual elements in the bitmaps.  */
190   bitmap_element *ptr1, *ptr2;
191
192   /* Position of an actual word in the elements.  */
193   unsigned word;
194
195   /* Position of a bit corresponding to the start of word.  */
196   unsigned word_bit;
197
198   /* Position of the actual bit.  */
199   unsigned bit;
200
201   /* Contents of the actually processed word.  When finding next bit
202      it is shifted right, so that the actual bit is always the least
203      significant bit of ACTUAL.  */
204   BITMAP_WORD actual;
205 } bitmap_iterator;
206
207 /* Moves the iterator BI to the first set bit on or after the current
208    position in bitmap and returns the bit if available.  The bit is
209    found in ACTUAL field only.  */
210
211 static inline unsigned
212 bmp_iter_common_next_1 (bitmap_iterator *bi)
213 {
214   while (!(bi->actual & 1))
215     {
216       bi->actual >>= 1;
217       bi->bit++;
218     }
219
220   return bi->bit;
221 }
222
223 /* Moves the iterator BI to the first set bit on or after the current
224    position in bitmap and returns the bit if available.  */
225
226 static inline unsigned
227 bmp_iter_single_next_1 (bitmap_iterator *bi)
228 {
229   if (bi->actual)
230     return bmp_iter_common_next_1 (bi);
231
232   bi->word++;
233   bi->word_bit += BITMAP_WORD_BITS;
234
235   while (1)
236     {
237       for (;
238            bi->word < BITMAP_ELEMENT_WORDS;
239            bi->word++, bi->word_bit += BITMAP_WORD_BITS)
240         {
241           bi->actual = bi->ptr1->bits[bi->word];
242           if (bi->actual)
243             {
244               bi->bit = bi->word_bit;
245               return bmp_iter_common_next_1 (bi);
246             }
247         }
248
249       bi->ptr1 = bi->ptr1->next;
250       if (!bi->ptr1)
251         return 0;
252
253       bi->word = 0;
254       bi->word_bit = bi->ptr1->indx * BITMAP_ELEMENT_ALL_BITS;
255     }
256 }
257
258 /* Initializes a bitmap iterator BI for looping over bits of bitmap
259    BMP, starting with bit MIN.  Returns the first bit of BMP greater
260    or equal to MIN if there is any.  */
261
262 static inline unsigned
263 bmp_iter_single_init (bitmap_iterator *bi, bitmap bmp, unsigned min)
264 {
265   unsigned indx = min / BITMAP_ELEMENT_ALL_BITS;
266
267   for (bi->ptr1 = bmp->first;
268        bi->ptr1 && bi->ptr1->indx < indx;
269        bi->ptr1 = bi->ptr1->next)
270     continue;
271
272   if (!bi->ptr1)
273     {
274       /* To avoid warnings.  */
275       bi->word = 0;
276       bi->bit = 0;
277       bi->word_bit = 0;
278       bi->actual = 0;
279       bi->ptr2 = NULL;
280       return 0;
281     }
282
283   if (bi->ptr1->indx == indx)
284     {
285       unsigned bit_in_elt = min - BITMAP_ELEMENT_ALL_BITS * indx;
286       unsigned word_in_elt = bit_in_elt / BITMAP_WORD_BITS;
287       unsigned bit_in_word = bit_in_elt % BITMAP_WORD_BITS;
288
289       bi->word = word_in_elt;
290       bi->word_bit = min - bit_in_word;
291       bi->bit = min;
292       bi->actual = bi->ptr1->bits[word_in_elt] >> bit_in_word;
293     }
294   else
295     {
296       bi->word = 0;
297       bi->bit = bi->ptr1->indx * BITMAP_ELEMENT_ALL_BITS;
298       bi->word_bit = bi->bit;
299       bi->actual = bi->ptr1->bits[0];
300     }
301
302   return bmp_iter_single_next_1 (bi);
303 }
304
305 /* Returns true if all elements of the bitmap referred to by iterator BI
306    were processed.  */
307
308 static inline bool
309 bmp_iter_end_p (const bitmap_iterator *bi)
310 {
311   return bi->ptr1 == NULL;
312 }
313
314 /* Moves the iterator BI to the next bit of bitmap and returns the bit
315    if available.  */
316
317 static inline unsigned
318 bmp_iter_single_next (bitmap_iterator *bi)
319 {
320   bi->bit++;
321   bi->actual >>= 1;
322   return bmp_iter_single_next_1 (bi);
323 }
324
325 /* Loop over all bits in BITMAP, starting with MIN and setting BITNUM to
326    the bit number.  ITER is a bitmap iterator.  */
327
328 #define EXECUTE_IF_SET_IN_BITMAP(BITMAP, MIN, BITNUM, ITER)             \
329   for ((BITNUM) = bmp_iter_single_init (&(ITER), (BITMAP), (MIN));      \
330        !bmp_iter_end_p (&(ITER));                                       \
331        (BITNUM) = bmp_iter_single_next (&(ITER)))
332
333 /* Moves the iterator BI to the first set bit on or after the current
334    position in difference of bitmaps and returns the bit if available.  */
335
336 static inline unsigned
337 bmp_iter_and_not_next_1 (bitmap_iterator *bi)
338 {
339   if (bi->actual)
340     return bmp_iter_common_next_1 (bi);
341
342   bi->word++;
343   bi->word_bit += BITMAP_WORD_BITS;
344
345   while (1)
346     {
347       bitmap_element *snd;
348
349       if (bi->ptr2 && bi->ptr2->indx == bi->ptr1->indx)
350         snd = bi->ptr2;
351       else
352         snd = &bitmap_zero_bits;
353
354       for (;
355            bi->word < BITMAP_ELEMENT_WORDS;
356            bi->word++, bi->word_bit += BITMAP_WORD_BITS)
357         {
358           bi->actual = (bi->ptr1->bits[bi->word]
359                         & ~snd->bits[bi->word]);
360           if (bi->actual)
361             {
362               bi->bit = bi->word_bit;
363               return bmp_iter_common_next_1 (bi);
364             }
365         }
366
367       bi->ptr1 = bi->ptr1->next;
368       if (!bi->ptr1)
369         return 0;
370
371       while (bi->ptr2
372              && bi->ptr2->indx < bi->ptr1->indx)
373         bi->ptr2 = bi->ptr2->next;
374
375       bi->word = 0;
376       bi->word_bit = bi->ptr1->indx * BITMAP_ELEMENT_ALL_BITS;
377     }
378 }
379
380 /* Initializes a bitmap iterator BI for looping over bits of bitmap
381    BMP1 &~ BMP2, starting with bit MIN.  Returns the first bit of
382    BMP1 &~ BMP2 greater or equal to MIN if there is any.  */
383
384 static inline unsigned
385 bmp_iter_and_not_init (bitmap_iterator *bi, bitmap bmp1, bitmap bmp2,
386                        unsigned min)
387 {
388   unsigned indx = min / BITMAP_ELEMENT_ALL_BITS;
389
390   for (bi->ptr1 = bmp1->first;
391        bi->ptr1 && bi->ptr1->indx < indx;
392        bi->ptr1 = bi->ptr1->next)
393     continue;
394
395   if (!bi->ptr1)
396     {
397       /* To avoid warnings.  */
398       bi->word = 0;
399       bi->bit = 0;
400       bi->word_bit = 0;
401       bi->actual = 0;
402       bi->ptr2 = NULL;
403       return 0;
404     }
405
406   for (bi->ptr2 = bmp2->first;
407        bi->ptr2 && bi->ptr2->indx < bi->ptr1->indx;
408        bi->ptr2 = bi->ptr2->next)
409     continue;
410
411   if (bi->ptr1->indx == indx)
412     {
413       unsigned bit_in_elt = min - BITMAP_ELEMENT_ALL_BITS * indx;
414       unsigned word_in_elt = bit_in_elt / BITMAP_WORD_BITS;
415       unsigned bit_in_word = bit_in_elt % BITMAP_WORD_BITS;
416
417       bi->word = word_in_elt;
418       bi->word_bit = min - bit_in_word;
419       bi->bit = min;
420
421       if (bi->ptr2 && bi->ptr2->indx == indx)
422         bi->actual = (bi->ptr1->bits[word_in_elt]
423                       & ~bi->ptr2->bits[word_in_elt]) >> bit_in_word;
424       else
425        bi->actual = bi->ptr1->bits[word_in_elt] >> bit_in_word;
426     }
427   else
428     {
429       bi->word = 0;
430       bi->bit = bi->ptr1->indx * BITMAP_ELEMENT_ALL_BITS;
431       bi->word_bit = bi->bit;
432
433       if (bi->ptr2 && bi->ptr2->indx == bi->ptr1->indx)
434         bi->actual = (bi->ptr1->bits[0] & ~bi->ptr2->bits[0]);
435       else
436         bi->actual = bi->ptr1->bits[0];
437     }
438
439   return bmp_iter_and_not_next_1 (bi);
440 }
441
442 /* Moves the iterator BI to the next bit of difference of bitmaps and returns
443    the bit if available.  */
444
445 static inline unsigned
446 bmp_iter_and_not_next (bitmap_iterator *bi)
447 {
448   bi->bit++;
449   bi->actual >>= 1;
450   return bmp_iter_and_not_next_1 (bi);
451 }
452
453 /* Loop over all bits in BMP1 and BMP2, starting with MIN, setting
454    BITNUM to the bit number for all bits that are set in the first bitmap
455    and not set in the second.  ITER is a bitmap iterator.  */
456
457 #define EXECUTE_IF_AND_COMPL_IN_BITMAP(BMP1, BMP2, MIN, BITNUM, ITER)   \
458   for ((BITNUM) = bmp_iter_and_not_init (&(ITER), (BMP1), (BMP2), (MIN)); \
459        !bmp_iter_end_p (&(ITER));                                               \
460        (BITNUM) = bmp_iter_and_not_next (&(ITER)))
461
462 /* Moves the iterator BI to the first set bit on or after the current
463    position in intersection of bitmaps and returns the bit if available.  */
464
465 static inline unsigned
466 bmp_iter_and_next_1 (bitmap_iterator *bi)
467 {
468   if (bi->actual)
469     return bmp_iter_common_next_1 (bi);
470
471   bi->word++;
472   bi->word_bit += BITMAP_WORD_BITS;
473
474   while (1)
475     {
476       for (;
477            bi->word < BITMAP_ELEMENT_WORDS;
478            bi->word++, bi->word_bit += BITMAP_WORD_BITS)
479         {
480           bi->actual = (bi->ptr1->bits[bi->word]
481                         & bi->ptr2->bits[bi->word]);
482           if (bi->actual)
483             {
484               bi->bit = bi->word_bit;
485               return bmp_iter_common_next_1 (bi);
486             }
487         }
488
489       do
490         {
491           bi->ptr1 = bi->ptr1->next;
492           if (!bi->ptr1)
493             return 0;
494
495           while (bi->ptr2->indx < bi->ptr1->indx)
496             {
497               bi->ptr2 = bi->ptr2->next;
498               if (!bi->ptr2)
499                 {
500                   bi->ptr1 = NULL;
501                   return 0;
502                 }
503             }
504         }
505       while (bi->ptr1->indx != bi->ptr2->indx);
506
507       bi->word = 0;
508       bi->word_bit = bi->ptr1->indx * BITMAP_ELEMENT_ALL_BITS;
509     }
510 }
511
512 /* Initializes a bitmap iterator BI for looping over bits of bitmap
513    BMP1 & BMP2, starting with bit MIN.  Returns the first bit of
514    BMP1 & BMP2 greater or equal to MIN if there is any.  */
515
516 static inline unsigned
517 bmp_iter_and_init (bitmap_iterator *bi, bitmap bmp1, bitmap bmp2,
518                        unsigned min)
519 {
520   unsigned indx = min / BITMAP_ELEMENT_ALL_BITS;
521
522   for (bi->ptr1 = bmp1->first;
523        bi->ptr1 && bi->ptr1->indx < indx;
524        bi->ptr1 = bi->ptr1->next)
525     continue;
526
527   if (!bi->ptr1)
528     goto empty;
529
530   bi->ptr2 = bmp2->first;
531   if (!bi->ptr2)
532     goto empty;
533
534   while (1)
535     {
536       while (bi->ptr2->indx < bi->ptr1->indx)
537         {
538           bi->ptr2 = bi->ptr2->next;
539           if (!bi->ptr2)
540             goto empty;
541         }
542
543       if (bi->ptr1->indx == bi->ptr2->indx)
544         break;
545
546       bi->ptr1 = bi->ptr1->next;
547       if (!bi->ptr1)
548         goto empty;
549     }
550
551   if (bi->ptr1->indx == indx)
552     {
553       unsigned bit_in_elt = min - BITMAP_ELEMENT_ALL_BITS * indx;
554       unsigned word_in_elt = bit_in_elt / BITMAP_WORD_BITS;
555       unsigned bit_in_word = bit_in_elt % BITMAP_WORD_BITS;
556
557       bi->word = word_in_elt;
558       bi->word_bit = min - bit_in_word;
559       bi->bit = min;
560
561       bi->actual = (bi->ptr1->bits[word_in_elt]
562                     & bi->ptr2->bits[word_in_elt]) >> bit_in_word;
563     }
564   else
565     {
566       bi->word = 0;
567       bi->bit = bi->ptr1->indx * BITMAP_ELEMENT_ALL_BITS;
568       bi->word_bit = bi->bit;
569
570       bi->actual = (bi->ptr1->bits[0] & bi->ptr2->bits[0]);
571     }
572
573   return bmp_iter_and_next_1 (bi);
574
575 empty:
576   /* To avoid warnings.  */
577   bi->word = 0;
578   bi->bit = 0;
579   bi->word_bit = 0;
580   bi->actual = 0;
581   bi->ptr1 = NULL;
582   bi->ptr2 = NULL;
583   return 0;
584 }
585
586 /* Moves the iterator BI to the next bit of intersection of bitmaps and returns
587    the bit if available.  */
588
589 static inline unsigned
590 bmp_iter_and_next (bitmap_iterator *bi)
591 {
592   bi->bit++;
593   bi->actual >>= 1;
594   return bmp_iter_and_next_1 (bi);
595 }
596
597 /* Loop over all bits in BMP1 and BMP2, starting with MIN, setting
598    BITNUM to the bit number for all bits that are set in both bitmaps.
599    ITER is a bitmap iterator.  */
600
601 #define EXECUTE_IF_AND_IN_BITMAP(BMP1, BMP2, MIN, BITNUM, ITER)         \
602   for ((BITNUM) = bmp_iter_and_init (&(ITER), (BMP1), (BMP2), (MIN));   \
603        !bmp_iter_end_p (&(ITER));                                               \
604        (BITNUM) = bmp_iter_and_next (&(ITER)))
605
606 #endif /* GCC_BITMAP_H */