OSDN Git Service

* doc/include/gcc-common.texi (version-GCC): Increase to 3.3.
[pf3gnuchains/gcc-fork.git] / gcc / bitmap.h
1 /* Functions to support general ended bitmaps.
2    Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002
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 /* Number of words to use for each element in the linked list.  */
26
27 #ifndef BITMAP_ELEMENT_WORDS
28 #define BITMAP_ELEMENT_WORDS 2
29 #endif
30
31 /* Number of bits in each actual element of a bitmap.  We get slightly better
32    code for bit % BITMAP_ELEMENT_ALL_BITS and bit / BITMAP_ELEMENT_ALL_BITS if
33    bits is unsigned, assuming it is a power of 2.  */
34
35 #define BITMAP_ELEMENT_ALL_BITS \
36   ((unsigned) (BITMAP_ELEMENT_WORDS * HOST_BITS_PER_WIDE_INT))
37
38 /* Bitmap set element.  We use a linked list to hold only the bits that
39    are set.  This allows for use to grow the bitset dynamically without
40    having to realloc and copy a giant bit array.  The `prev' field is
41    undefined for an element on the free list.  */
42
43 typedef struct bitmap_element_def GTY(())
44 {
45   struct bitmap_element_def *next;              /* Next element.  */
46   struct bitmap_element_def *prev;              /* Previous element.  */
47   unsigned int indx;                    /* regno/BITMAP_ELEMENT_ALL_BITS.  */
48   unsigned HOST_WIDE_INT bits[BITMAP_ELEMENT_WORDS]; /* Bits that are set.  */
49 } bitmap_element;
50
51 /* Head of bitmap linked list.  */
52 typedef struct bitmap_head_def GTY(()) {
53   bitmap_element *first;        /* First element in linked list.  */
54   bitmap_element *current;      /* Last element looked at.  */
55   unsigned int indx;            /* Index of last element looked at.  */
56   int using_obstack;            /* Are we using an obstack or ggc for
57                                    allocation?  */
58 } bitmap_head;
59 typedef struct bitmap_head_def *bitmap;
60
61 /* Enumeration giving the various operations we support.  */
62 enum bitmap_bits {
63   BITMAP_AND,                   /* TO = FROM1 & FROM2 */
64   BITMAP_AND_COMPL,             /* TO = FROM1 & ~ FROM2 */
65   BITMAP_IOR,                   /* TO = FROM1 | FROM2 */
66   BITMAP_XOR,                   /* TO = FROM1 ^ FROM2 */
67   BITMAP_IOR_COMPL                      /* TO = FROM1 | ~FROM2 */
68 };
69
70 /* Global data */
71 extern bitmap_element bitmap_zero_bits; /* Zero bitmap element */
72
73 /* Clear a bitmap by freeing up the linked list.  */
74 extern void bitmap_clear PARAMS ((bitmap));
75
76 /* Copy a bitmap to another bitmap.  */
77 extern void bitmap_copy PARAMS ((bitmap, bitmap));
78
79 /* True if two bitmaps are identical.  */
80 extern int bitmap_equal_p PARAMS ((bitmap, bitmap));
81
82 /* Perform an operation on two bitmaps, yielding a third.  */
83 extern int bitmap_operation PARAMS ((bitmap, bitmap, bitmap, enum bitmap_bits));
84
85 /* `or' into one bitmap the `and' of a second bitmap witih the complement
86    of a third.  */
87 extern void bitmap_ior_and_compl PARAMS ((bitmap, bitmap, bitmap));
88
89 /* Clear a single register in a register set.  */
90 extern void bitmap_clear_bit PARAMS ((bitmap, int));
91
92 /* Set a single register in a register set.  */
93 extern void bitmap_set_bit PARAMS ((bitmap, int));
94
95 /* Return true if a register is set in a register set.  */
96 extern int bitmap_bit_p PARAMS ((bitmap, int));
97
98 /* Debug functions to print a bitmap linked list.  */
99 extern void debug_bitmap PARAMS ((bitmap));
100 extern void debug_bitmap_file PARAMS ((FILE *, bitmap));
101
102 /* Print a bitmap */
103 extern void bitmap_print PARAMS ((FILE *, bitmap, const char *, const char *));
104
105 /* Initialize a bitmap header.  If HEAD is NULL, a new header will be
106    allocated.  USING_OBSTACK indicates how elements should be allocated.  */
107 extern bitmap bitmap_initialize PARAMS ((bitmap head, 
108                                          int using_obstack));
109
110 /* Release all memory used by the bitmap obstack.  */
111 extern void bitmap_release_memory PARAMS ((void));
112
113 /* A few compatibility/functions macros for compatibility with sbitmaps */
114 #define dump_bitmap(file, bitmap) bitmap_print (file, bitmap, "", "\n")
115 #define bitmap_zero(a) bitmap_clear (a)
116 #define bitmap_a_or_b(a,b,c) bitmap_operation (a, b, c, BITMAP_IOR)
117 #define bitmap_a_and_b(a,b,c) bitmap_operation (a, b, c, BITMAP_AND)
118 extern int bitmap_union_of_diff PARAMS((bitmap, bitmap, bitmap, bitmap));
119 extern int bitmap_first_set_bit PARAMS((bitmap));
120 extern int bitmap_last_set_bit PARAMS((bitmap));
121
122 /* Allocate a bitmap with oballoc.  */
123 #define BITMAP_OBSTACK_ALLOC(OBSTACK)                           \
124   bitmap_initialize ((bitmap) obstack_alloc (OBSTACK, sizeof (bitmap_head)), 1)
125
126 /* Allocate a bitmap with ggc_alloc.  */
127 #define BITMAP_GGC_ALLOC()                      \
128   bitmap_initialize (NULL, 0)
129
130 /* Allocate a bitmap with xmalloc.  */
131 #define BITMAP_XMALLOC()                                        \
132   bitmap_initialize ((bitmap) xmalloc (sizeof (bitmap_head)), 1)
133
134 /* Do any cleanup needed on a bitmap when it is no longer used.  */
135 #define BITMAP_FREE(BITMAP)                     \
136 do {                                            \
137   if (BITMAP)                                   \
138     {                                           \
139       bitmap_clear (BITMAP);                    \
140       (BITMAP) = 0;                             \
141     }                                           \
142 } while (0)
143
144 /* Do any cleanup needed on an xmalloced bitmap when it is no longer used.  */
145 #define BITMAP_XFREE(BITMAP)                    \
146 do {                                            \
147   if (BITMAP)                                   \
148     {                                           \
149       bitmap_clear (BITMAP);                    \
150       free (BITMAP);                            \
151       (BITMAP) = 0;                             \
152     }                                           \
153 } while (0)
154
155 /* Do any one-time initializations needed for bitmaps.  */
156 #define BITMAP_INIT_ONCE()
157
158 /* Loop over all bits in BITMAP, starting with MIN, setting BITNUM to the
159    bit number and executing CODE for all bits that are set.  */
160
161 #define EXECUTE_IF_SET_IN_BITMAP(BITMAP, MIN, BITNUM, CODE)             \
162 do {                                                                    \
163   bitmap_element *ptr_ = (BITMAP)->first;                               \
164   unsigned int indx_ = (MIN) / BITMAP_ELEMENT_ALL_BITS;                 \
165   unsigned bit_num_ = (MIN) % ((unsigned) HOST_BITS_PER_WIDE_INT);      \
166   unsigned word_num_ = (((MIN) / ((unsigned) HOST_BITS_PER_WIDE_INT))   \
167                         % BITMAP_ELEMENT_WORDS);                        \
168                                                                         \
169                                                                         \
170   /* Find the block the minimum bit is in.  */                          \
171   while (ptr_ != 0 && ptr_->indx < indx_)                               \
172     ptr_ = ptr_->next;                                                  \
173                                                                         \
174   if (ptr_ != 0 && ptr_->indx != indx_)                                 \
175     {                                                                   \
176       bit_num_ = 0;                                                     \
177       word_num_ = 0;                                                    \
178     }                                                                   \
179                                                                         \
180   for (; ptr_ != 0; ptr_ = ptr_->next)                                  \
181     {                                                                   \
182       for (; word_num_ < BITMAP_ELEMENT_WORDS; word_num_++)             \
183         {                                                               \
184           unsigned HOST_WIDE_INT word_ = ptr_->bits[word_num_];         \
185                                                                         \
186           if (word_ != 0)                                               \
187             {                                                           \
188               for (; bit_num_ < HOST_BITS_PER_WIDE_INT; bit_num_++)     \
189                 {                                                       \
190                   unsigned HOST_WIDE_INT mask_                          \
191                     = ((unsigned HOST_WIDE_INT) 1) << bit_num_;         \
192                                                                         \
193                   if ((word_ & mask_) != 0)                             \
194                     {                                                   \
195                       word_ &= ~ mask_;                                 \
196                       (BITNUM) = (ptr_->indx * BITMAP_ELEMENT_ALL_BITS  \
197                                   + word_num_ * HOST_BITS_PER_WIDE_INT  \
198                                   + bit_num_);                          \
199                       CODE;                                             \
200                                                                         \
201                       if (word_ == 0)                                   \
202                         break;                                          \
203                     }                                                   \
204                 }                                                       \
205             }                                                           \
206                                                                         \
207           bit_num_ = 0;                                                 \
208         }                                                               \
209                                                                         \
210       word_num_ = 0;                                                    \
211     }                                                                   \
212 } while (0)
213
214 /* Loop over all bits in BITMAP1 and BITMAP2, starting with MIN, setting
215    BITNUM to the bit number and executing CODE for all bits that are set in
216    the first bitmap and not set in the second.  */
217
218 #define EXECUTE_IF_AND_COMPL_IN_BITMAP(BITMAP1, BITMAP2, MIN, BITNUM, CODE) \
219 do {                                                                    \
220   bitmap_element *ptr1_ = (BITMAP1)->first;                             \
221   bitmap_element *ptr2_ = (BITMAP2)->first;                             \
222   unsigned int indx_ = (MIN) / BITMAP_ELEMENT_ALL_BITS;                 \
223   unsigned bit_num_ = (MIN) % ((unsigned) HOST_BITS_PER_WIDE_INT);      \
224   unsigned word_num_ = (((MIN) / ((unsigned) HOST_BITS_PER_WIDE_INT))   \
225                         % BITMAP_ELEMENT_WORDS);                        \
226                                                                         \
227   /* Find the block the minimum bit is in in the first bitmap.  */      \
228   while (ptr1_ != 0 && ptr1_->indx < indx_)                             \
229     ptr1_ = ptr1_->next;                                                \
230                                                                         \
231   if (ptr1_ != 0 && ptr1_->indx != indx_)                               \
232     {                                                                   \
233       bit_num_ = 0;                                                     \
234       word_num_ = 0;                                                    \
235     }                                                                   \
236                                                                         \
237   for (; ptr1_ != 0 ; ptr1_ = ptr1_->next)                              \
238     {                                                                   \
239       /* Advance BITMAP2 to the equivalent link, using an all           \
240          zero element if an equivalent link doesn't exist.  */          \
241       bitmap_element *tmp2_;                                            \
242                                                                         \
243       while (ptr2_ != 0 && ptr2_->indx < ptr1_->indx)                   \
244         ptr2_ = ptr2_->next;                                            \
245                                                                         \
246       tmp2_ = ((ptr2_ != 0 && ptr2_->indx == ptr1_->indx)               \
247                ? ptr2_ : &bitmap_zero_bits);                            \
248                                                                         \
249       for (; word_num_ < BITMAP_ELEMENT_WORDS; word_num_++)             \
250         {                                                               \
251           unsigned HOST_WIDE_INT word_ = (ptr1_->bits[word_num_]        \
252                                           & ~ tmp2_->bits[word_num_]);  \
253           if (word_ != 0)                                               \
254             {                                                           \
255               for (; bit_num_ < HOST_BITS_PER_WIDE_INT; bit_num_++)     \
256                 {                                                       \
257                   unsigned HOST_WIDE_INT mask_                          \
258                     = ((unsigned HOST_WIDE_INT)1) << bit_num_;          \
259                                                                         \
260                   if ((word_ & mask_) != 0)                             \
261                     {                                                   \
262                       word_ &= ~ mask_;                                 \
263                       (BITNUM) = (ptr1_->indx * BITMAP_ELEMENT_ALL_BITS \
264                                   + word_num_ * HOST_BITS_PER_WIDE_INT  \
265                                   + bit_num_);                          \
266                                                                         \
267                       CODE;                                             \
268                       if (word_ == 0)                                   \
269                         break;                                          \
270                     }                                                   \
271                 }                                                       \
272             }                                                           \
273                                                                         \
274           bit_num_ = 0;                                                 \
275         }                                                               \
276                                                                         \
277       word_num_ = 0;                                                    \
278     }                                                                   \
279 } while (0)
280
281 /* Loop over all bits in BITMAP1 and BITMAP2, starting with MIN, setting
282    BITNUM to the bit number and executing CODE for all bits that are set in
283    the both bitmaps.  */
284
285 #define EXECUTE_IF_AND_IN_BITMAP(BITMAP1, BITMAP2, MIN, BITNUM, CODE)   \
286 do {                                                                    \
287   bitmap_element *ptr1_ = (BITMAP1)->first;                             \
288   bitmap_element *ptr2_ = (BITMAP2)->first;                             \
289   unsigned int indx_ = (MIN) / BITMAP_ELEMENT_ALL_BITS;                 \
290   unsigned bit_num_ = (MIN) % ((unsigned) HOST_BITS_PER_WIDE_INT);      \
291   unsigned word_num_ = (((MIN) / ((unsigned) HOST_BITS_PER_WIDE_INT))   \
292                         % BITMAP_ELEMENT_WORDS);                        \
293                                                                         \
294   /* Find the block the minimum bit is in in the first bitmap.  */      \
295   while (ptr1_ != 0 && ptr1_->indx < indx_)                             \
296     ptr1_ = ptr1_->next;                                                \
297                                                                         \
298   if (ptr1_ != 0 && ptr1_->indx != indx_)                               \
299     {                                                                   \
300       bit_num_ = 0;                                                     \
301       word_num_ = 0;                                                    \
302     }                                                                   \
303                                                                         \
304   for (; ptr1_ != 0 ; ptr1_ = ptr1_->next)                              \
305     {                                                                   \
306       /* Advance BITMAP2 to the equivalent link */                      \
307       while (ptr2_ != 0 && ptr2_->indx < ptr1_->indx)                   \
308         ptr2_ = ptr2_->next;                                            \
309                                                                         \
310       if (ptr2_ == 0)                                                   \
311         {                                                               \
312           /* If there are no more elements in BITMAP2, exit loop now.  */ \
313           ptr1_ = (bitmap_element *)0;                                  \
314           break;                                                        \
315         }                                                               \
316       else if (ptr2_->indx > ptr1_->indx)                               \
317         {                                                               \
318           bit_num_ = word_num_ = 0;                                     \
319           continue;                                                     \
320         }                                                               \
321                                                                         \
322       for (; word_num_ < BITMAP_ELEMENT_WORDS; word_num_++)             \
323         {                                                               \
324           unsigned HOST_WIDE_INT word_ = (ptr1_->bits[word_num_]        \
325                                           & ptr2_->bits[word_num_]);    \
326           if (word_ != 0)                                               \
327             {                                                           \
328               for (; bit_num_ < HOST_BITS_PER_WIDE_INT; bit_num_++)     \
329                 {                                                       \
330                   unsigned HOST_WIDE_INT mask_                          \
331                     = ((unsigned HOST_WIDE_INT)1) << bit_num_;          \
332                                                                         \
333                   if ((word_ & mask_) != 0)                             \
334                     {                                                   \
335                       word_ &= ~ mask_;                                 \
336                       (BITNUM) = (ptr1_->indx * BITMAP_ELEMENT_ALL_BITS \
337                                   + word_num_ * HOST_BITS_PER_WIDE_INT  \
338                                   + bit_num_);                          \
339                                                                         \
340                       CODE;                                             \
341                       if (word_ == 0)                                   \
342                         break;                                          \
343                     }                                                   \
344                 }                                                       \
345             }                                                           \
346                                                                         \
347           bit_num_ = 0;                                                 \
348         }                                                               \
349                                                                         \
350       word_num_ = 0;                                                    \
351     }                                                                   \
352 } while (0)
353
354 #endif /* GCC_BITMAP_H */