OSDN Git Service

f46dcf4c2ba8da46411cff30dd94689f1730bcce
[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 extern int bitmap_first_set_bit (bitmap);
145 extern int bitmap_last_set_bit (bitmap);
146
147 /* Allocate a bitmap with oballoc.  */
148 #define BITMAP_OBSTACK_ALLOC(OBSTACK)                           \
149   bitmap_initialize (obstack_alloc (OBSTACK, sizeof (bitmap_head)), 1)
150
151 /* Allocate a bitmap with ggc_alloc.  */
152 #define BITMAP_GGC_ALLOC()                      \
153   bitmap_initialize (NULL, 0)
154
155 /* Allocate a bitmap with xmalloc.  */
156 #define BITMAP_XMALLOC()                                        \
157   bitmap_initialize (xmalloc (sizeof (bitmap_head)), 1)
158
159 /* Do any cleanup needed on a bitmap when it is no longer used.  */
160 #define BITMAP_FREE(BITMAP)                     \
161 do {                                            \
162   if (BITMAP)                                   \
163     {                                           \
164       bitmap_clear (BITMAP);                    \
165       (BITMAP) = 0;                             \
166     }                                           \
167 } while (0)
168
169 /* Do any cleanup needed on an xmalloced bitmap when it is no longer used.  */
170 #define BITMAP_XFREE(BITMAP)                    \
171 do {                                            \
172   if (BITMAP)                                   \
173     {                                           \
174       bitmap_clear (BITMAP);                    \
175       free (BITMAP);                            \
176       (BITMAP) = 0;                             \
177     }                                           \
178 } while (0)
179
180 /* Do any one-time initializations needed for bitmaps.  */
181 #define BITMAP_INIT_ONCE()
182
183 /* Iterator for bitmaps.  */
184
185 typedef struct
186 {
187   /* Pointer to the current bitmap element.  */
188   bitmap_element *elt1;
189   
190   /* Pointer to 2nd bitmap element when two are involved.  */
191   bitmap_element *elt2;
192
193   /* Word within the current element.  */
194   unsigned word_no;
195   
196   /* Contents of the actually processed word.  When finding next bit
197      it is shifted right, so that the actual bit is always the least
198      significant bit of ACTUAL.  */
199   BITMAP_WORD bits;
200 } bitmap_iterator;
201
202 /* Initialize a single bitmap iterator.  START_BIT is the first bit to
203    iterate from.  */
204
205 static inline void
206 bmp_iter_set_init (bitmap_iterator *bi, bitmap map,
207                    unsigned start_bit, unsigned *bit_no)
208 {
209   bi->elt1 = map->first;
210   bi->elt2 = NULL;
211
212   /* Advance elt1 until it is not before the block containing start_bit.  */
213   while (1)
214     {
215       if (!bi->elt1)
216         {
217           bi->elt1 = &bitmap_zero_bits;
218           break;
219         }
220       
221       if (bi->elt1->indx >= start_bit / BITMAP_ELEMENT_ALL_BITS)
222         break;
223       bi->elt1 = bi->elt1->next;
224     }
225
226   /* We might have gone past the start bit, so reinitialize it.  */
227   if (bi->elt1->indx != start_bit / BITMAP_ELEMENT_ALL_BITS)
228     start_bit = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
229   
230   /* Initialize for what is now start_bit.  */
231   bi->word_no = start_bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
232   bi->bits = bi->elt1->bits[bi->word_no];
233   bi->bits >>= start_bit % BITMAP_WORD_BITS;
234
235   /* If this word is zero, we must make sure we're not pointing at the
236      first bit, otherwise our incrementing to the next word boundary
237      will fail.  It won't matter if this increment moves us into the
238      next word.  */
239   start_bit += !bi->bits;
240   
241   *bit_no = start_bit;
242 }
243
244 /* Initialize an iterator to iterate over the intersection of two
245    bitmaps.  START_BIT is the bit to commence from.  */
246
247 static inline void
248 bmp_iter_and_init (bitmap_iterator *bi, bitmap map1, bitmap map2,
249                    unsigned start_bit, unsigned *bit_no)
250 {
251   bi->elt1 = map1->first;
252   bi->elt2 = map2->first;
253
254   /* Advance elt1 until it is not before the block containing
255      start_bit.  */
256   while (1)
257     {
258       if (!bi->elt1)
259         {
260           bi->elt2 = NULL;
261           break;
262         }
263       
264       if (bi->elt1->indx >= start_bit / BITMAP_ELEMENT_ALL_BITS)
265         break;
266       bi->elt1 = bi->elt1->next;
267     }
268   
269   /* Advance elt2 until it is not before elt1.  */
270   while (1)
271     {
272       if (!bi->elt2)
273         {
274           bi->elt1 = bi->elt2 = &bitmap_zero_bits;
275           break;
276         }
277       
278       if (bi->elt2->indx >= bi->elt1->indx)
279         break;
280       bi->elt2 = bi->elt2->next;
281     }
282
283   /* If we're at the same index, then we have some intersecting bits.   */
284   if (bi->elt1->indx == bi->elt2->indx)
285     {
286       /* We might have advanced beyond the start_bit, so reinitialize
287          for that.  */
288       if (bi->elt1->indx != start_bit / BITMAP_ELEMENT_ALL_BITS)
289         start_bit = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
290       
291       bi->word_no = start_bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
292       bi->bits = bi->elt1->bits[bi->word_no] & bi->elt2->bits[bi->word_no];
293       bi->bits >>= start_bit % BITMAP_WORD_BITS;
294     }
295   else
296     {
297       /* Otherwise we must immediately advance elt1, so initialize for
298          that.  */
299       bi->word_no = BITMAP_ELEMENT_WORDS - 1;
300       bi->bits = 0;
301     }
302   
303   /* If this word is zero, we must make sure we're not pointing at the
304      first bit, otherwise our incrementing to the next word boundary
305      will fail.  It won't matter if this increment moves us into the
306      next word.  */
307   start_bit += !bi->bits;
308   
309   *bit_no = start_bit;
310 }
311
312 /* Initialize an iterator to iterate over the bits in MAP1 & ~MAP2.
313    */
314
315 static inline void
316 bmp_iter_and_compl_init (bitmap_iterator *bi, bitmap map1, bitmap map2,
317                          unsigned start_bit, unsigned *bit_no)
318 {
319   bi->elt1 = map1->first;
320   bi->elt2 = map2->first;
321
322   /* Advance elt1 until it is not before the block containing start_bit.  */
323   while (1)
324     {
325       if (!bi->elt1)
326         {
327           bi->elt1 = &bitmap_zero_bits;
328           break;
329         }
330       
331       if (bi->elt1->indx >= start_bit / BITMAP_ELEMENT_ALL_BITS)
332         break;
333       bi->elt1 = bi->elt1->next;
334     }
335
336   /* Advance elt2 until it is not before elt1.  */
337   while (bi->elt2 && bi->elt2->indx < bi->elt1->indx)
338     bi->elt2 = bi->elt2->next;
339
340   /* We might have advanced beyond the start_bit, so reinitialize for
341      that.  */
342   if (bi->elt1->indx != start_bit / BITMAP_ELEMENT_ALL_BITS)
343     start_bit = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
344   
345   bi->word_no = start_bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
346   bi->bits = bi->elt1->bits[bi->word_no];
347   if (bi->elt2 && bi->elt1->indx == bi->elt2->indx)
348     bi->bits &= ~bi->elt2->bits[bi->word_no];
349   bi->bits >>= start_bit % BITMAP_WORD_BITS;
350   
351   /* If this word is zero, we must make sure we're not pointing at the
352      first bit, otherwise our incrementing to the next word boundary
353      will fail.  It won't matter if this increment moves us into the
354      next word.  */
355   start_bit += !bi->bits;
356   
357   *bit_no = start_bit;
358 }
359
360 /* Advance to the next bit in BI.  We don't advance to the next
361    non-zero bit yet.  */
362
363 static inline void
364 bmp_iter_next (bitmap_iterator *bi, unsigned *bit_no)
365 {
366   bi->bits >>= 1;
367   *bit_no += 1;
368 }
369
370 /* Advance to the next non-zero bit of a single bitmap, we will have
371    already advanced past the just iterated bit.  Return true if there
372    is a bit to iterate.  */
373
374 static inline bool
375 bmp_iter_set (bitmap_iterator *bi, unsigned *bit_no)
376 {
377   /* If our current word is non-zero, it contains the bit we want.  */
378   if (bi->bits)
379     {
380     next_bit:
381       while (!(bi->bits & 1))
382         {
383           bi->bits >>= 1;
384           *bit_no += 1;
385         }
386       return true;
387     }
388
389   /* Round up to the word boundary.  We might have just iterated past
390      the end of the last word, hence the -1.  It is not possible for
391      bit_no to point at the beginning of the now last word.  */
392   *bit_no = ((*bit_no + BITMAP_WORD_BITS - 1)
393              / BITMAP_WORD_BITS * BITMAP_WORD_BITS);
394   bi->word_no++;
395
396   while (1)
397     {
398       /* Find the next non-zero word in this elt.  */
399       while (bi->word_no != BITMAP_ELEMENT_WORDS)
400         {
401           bi->bits = bi->elt1->bits[bi->word_no];
402           if (bi->bits)
403             goto next_bit;
404           *bit_no += BITMAP_WORD_BITS;
405           bi->word_no++;
406         }
407   
408       /* Advance to the next element.  */
409       bi->elt1 = bi->elt1->next;
410       if (!bi->elt1)
411         return false;
412       *bit_no = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
413       bi->word_no = 0;
414     }
415 }
416
417 /* Advance to the next non-zero bit of an intersecting pair of
418    bitmaps.  We will have alreadt advanced past the just iterated bit.
419    Return true if there is a bit to iterate.  */
420
421 static inline bool
422 bmp_iter_and (bitmap_iterator *bi, unsigned *bit_no)
423 {
424   /* If our current word is non-zero, it contains the bit we want.  */
425   if (bi->bits)
426     {
427     next_bit:
428       while (!(bi->bits & 1))
429         {
430           bi->bits >>= 1;
431           *bit_no += 1;
432         }
433       return true;
434     }
435
436   /* Round up to the word boundary.  We might have just iterated past
437      the end of the last word, hence the -1.  It is not possible for
438      bit_no to point at the beginning of the now last word.  */
439   *bit_no = ((*bit_no + BITMAP_WORD_BITS - 1)
440              / BITMAP_WORD_BITS * BITMAP_WORD_BITS);
441   bi->word_no++;
442   
443   while (1)
444     {
445       /* Find the next non-zero word in this elt.  */
446       while (bi->word_no != BITMAP_ELEMENT_WORDS)
447         {
448           bi->bits = bi->elt1->bits[bi->word_no] & bi->elt2->bits[bi->word_no];
449           if (bi->bits)
450             goto next_bit;
451           *bit_no += BITMAP_WORD_BITS;
452           bi->word_no++;
453         }
454   
455       /* Advance to the next identical element.  */
456       do
457         {
458           /* Advance elt1 while it is less than elt2.  We always want
459              to advance one elt.  */
460           do
461             {
462               bi->elt1 = bi->elt1->next;
463               if (!bi->elt1)
464                 return false;
465             }
466           while (bi->elt1->indx < bi->elt2->indx);
467         
468           /* Advance elt2 to be no less than elt1.  This might not
469              advance.  */
470           while (bi->elt2->indx < bi->elt1->indx)
471             {
472               bi->elt2 = bi->elt2->next;
473               if (!bi->elt2)
474                 return false;
475             }
476         }
477       while (bi->elt1->indx != bi->elt2->indx);
478   
479       *bit_no = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
480       bi->word_no = 0;
481     }
482 }
483
484 /* Advance to the next non-zero bit in the intersection of
485    complemented bitmaps.  We will have already advanced past the just
486    iterated bit.  */
487
488 static inline bool
489 bmp_iter_and_compl (bitmap_iterator *bi, unsigned *bit_no)
490 {
491   /* If our current word is non-zero, it contains the bit we want.  */
492   if (bi->bits)
493     {
494     next_bit:
495       while (!(bi->bits & 1))
496         {
497           bi->bits >>= 1;
498           *bit_no += 1;
499         }
500       return true;
501     }
502
503   /* Round up to the word boundary.  We might have just iterated past
504      the end of the last word, hence the -1.  It is not possible for
505      bit_no to point at the beginning of the now last word.  */
506   *bit_no = ((*bit_no + BITMAP_WORD_BITS - 1)
507              / BITMAP_WORD_BITS * BITMAP_WORD_BITS);
508   bi->word_no++;
509
510   while (1)
511     {
512       /* Find the next non-zero word in this elt.  */
513       while (bi->word_no != BITMAP_ELEMENT_WORDS)
514         {
515           bi->bits = bi->elt1->bits[bi->word_no];
516           if (bi->elt2 && bi->elt2->indx == bi->elt1->indx)
517             bi->bits &= ~bi->elt2->bits[bi->word_no];
518           if (bi->bits)
519             goto next_bit;
520           *bit_no += BITMAP_WORD_BITS;
521           bi->word_no++;
522         }
523   
524       /* Advance to the next element of elt1.  */
525       bi->elt1 = bi->elt1->next;
526       if (!bi->elt1)
527         return false;
528
529       /* Advance elt2 until it is no less than elt1.  */
530       while (bi->elt2 && bi->elt2->indx < bi->elt1->indx)
531         bi->elt2 = bi->elt2->next;
532       
533       *bit_no = bi->elt1->indx * BITMAP_ELEMENT_ALL_BITS;
534       bi->word_no = 0;
535     }
536 }
537
538 /* Loop over all bits set in BITMAP, starting with MIN and setting
539    BITNUM to the bit number.  ITER is a bitmap iterator.  BITNUM
540    should be treated as a read-only variable as it contains loop
541    state.  */
542
543 #define EXECUTE_IF_SET_IN_BITMAP(BITMAP, MIN, BITNUM, ITER)             \
544   for (bmp_iter_set_init (&(ITER), (BITMAP), (MIN), &(BITNUM));         \
545        bmp_iter_set (&(ITER), &(BITNUM));                               \
546        bmp_iter_next (&(ITER), &(BITNUM)))
547
548 /* Loop over all the bits set in BITMAP1 & BITMAP2, starting with MIN
549    and setting BITNUM to the bit number.  ITER is a bitmap iterator.
550    BITNUM should be treated as a read-only variable as it contains
551    loop state.  */
552
553 #define EXECUTE_IF_AND_IN_BITMAP(BITMAP1, BITMAP2, MIN, BITNUM, ITER)   \
554   for (bmp_iter_and_init (&(ITER), (BITMAP1), (BITMAP2), (MIN),         \
555                           &(BITNUM));                                   \
556        bmp_iter_and (&(ITER), &(BITNUM));                               \
557        bmp_iter_next (&(ITER), &(BITNUM)))
558
559 /* Loop over all the bits set in BITMAP1 & ~BITMAP2, starting with MIN
560    and setting BITNUM to the bit number.  ITER is a bitmap iterator.
561    BITNUM should be treated as a read-only variable as it contains
562    loop state.  */
563
564 #define EXECUTE_IF_AND_COMPL_IN_BITMAP(BITMAP1, BITMAP2, MIN, BITNUM, ITER) \
565   for (bmp_iter_and_compl_init (&(ITER), (BITMAP1), (BITMAP2), (MIN),   \
566                                 &(BITNUM));                             \
567        bmp_iter_and_compl (&(ITER), &(BITNUM));                         \
568        bmp_iter_next (&(ITER), &(BITNUM)))
569
570 #endif /* GCC_BITMAP_H */