]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/ebitmap.h
This patch implements the unification of the *bitmap interfaces as discussed.
[thirdparty/gcc.git] / gcc / ebitmap.h
1 /* Sparse array based bitmaps.
2 Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
19
20 #ifndef GCC_EBITMAP_H
21 #define GCC_EBITMAP_H
22
23 #include "sbitmap.h"
24
25 #define EBITMAP_ELT_BITS ((unsigned) HOST_BITS_PER_WIDEST_FAST_INT)
26 #define EBITMAP_ELT_TYPE unsigned HOST_WIDEST_FAST_INT
27
28 typedef struct ebitmap_def
29 {
30 unsigned int n_elts; /* number of elements in the array. */
31 sbitmap wordmask; /* wordmask saying which words are
32 nonzero. */
33 unsigned int numwords; /* number of nonzero words. */
34 unsigned int cacheindex; /* which word cache is. */
35 EBITMAP_ELT_TYPE *elts; /* nonzero element array. */
36 EBITMAP_ELT_TYPE *cache; /* last tested element, or NULL. */
37 } *ebitmap;
38
39
40 inline bool bitmap_empty_p (ebitmap map)
41 {
42 return map->numwords == 0;
43 }
44
45 #define ebitmap_free(MAP) (free((MAP)->elts), \
46 sbitmap_free ((MAP)->wordmask), \
47 free((MAP)))
48
49 extern void bitmap_set_bit (ebitmap, unsigned int);
50 extern void bitmap_clear_bit (ebitmap, unsigned int);
51 extern bool bitmap_bit_p (ebitmap, unsigned int);
52 extern void dump_bitmap (FILE *, ebitmap);
53 extern void dump_bitmap_file (FILE *, ebitmap);
54 extern void dump_bitmap_vector (FILE *, const char *, const char *, ebitmap *,
55 int);
56 extern ebitmap ebitmap_alloc (unsigned int);
57 extern ebitmap *ebitmap_vector_alloc (unsigned int, unsigned int);
58 extern void bitmap_copy (ebitmap, ebitmap);
59 extern void bitmap_and (ebitmap, ebitmap, ebitmap);
60 extern void bitmap_and_into (ebitmap, ebitmap);
61 extern bool bitmap_and_compl (ebitmap, ebitmap, ebitmap);
62 extern bool bitmap_and_compl_into (ebitmap, ebitmap);
63 extern bool bitmap_ior_into (ebitmap, ebitmap);
64 extern bool bitmap_ior (ebitmap, ebitmap, ebitmap);
65 extern bool bitmap_ior_and_compl (ebitmap, ebitmap, ebitmap, ebitmap);
66 extern bool bitmap_ior_and_compl_into (ebitmap, ebitmap, ebitmap);
67 extern bool bitmap_equal_p (ebitmap, ebitmap);
68 extern void bitmap_clear (ebitmap);
69 extern int bitmap_last_set_bit (ebitmap);
70 extern void debug_bitmap (ebitmap);
71 extern unsigned long bitmap_popcount(ebitmap, unsigned long);
72
73 /* The iterator for ebitmap. */
74 typedef struct {
75 /* The pointer to the first word of the bitmap. */
76 EBITMAP_ELT_TYPE *ptr;
77
78 /* Element number inside ptr that we are at. */
79 unsigned int eltnum;
80
81 /* The size of the bitmap. */
82 unsigned int size;
83
84 /* Current bit index. */
85 unsigned int bit_num;
86
87 /* The words currently visited. */
88 EBITMAP_ELT_TYPE word;
89
90 /* The word mask iterator. */
91 sbitmap_iterator maskiter;
92 } ebitmap_iterator;
93
94 static inline void
95 ebitmap_iter_init (ebitmap_iterator *i, ebitmap bmp, unsigned int min)
96 {
97 sbitmap_iter_init (&i->maskiter, bmp->wordmask,
98 min / EBITMAP_ELT_BITS);
99 i->size = bmp->numwords;
100 if (i->size == 0)
101 {
102 i->ptr = NULL;
103 i->eltnum = 0;
104 i->bit_num = 0;
105 i->word = 0;
106 return;
107 }
108 i->ptr = bmp->elts;
109 i->bit_num = min;
110 i->eltnum = 0;
111
112 if ((min / EBITMAP_ELT_BITS) >= bmp->wordmask->n_bits)
113 {
114 i->word = 0;
115 }
116 else
117 {
118 if (TEST_BIT (bmp->wordmask, min / EBITMAP_ELT_BITS) == 0)
119 i->word = 0;
120 else
121 {
122 unsigned int wordindex = min / EBITMAP_ELT_BITS;
123 unsigned int count = sbitmap_popcount (bmp->wordmask, wordindex);
124 i->word = i->ptr[count] >> (i->bit_num % (unsigned int)EBITMAP_ELT_BITS);
125 i->eltnum = count + 1;
126 }
127 }
128 }
129
130 static inline bool
131 ebitmap_iter_cond (ebitmap_iterator *i, unsigned int *n)
132 {
133 unsigned int ourn = 0;
134
135 if (i->size == 0)
136 return false;
137
138 if (i->word == 0)
139 {
140 sbitmap_iter_next (&i->maskiter);
141 if (!sbitmap_iter_cond (&i->maskiter, &ourn))
142 return false;
143 i->bit_num = ourn * EBITMAP_ELT_BITS;
144 i->word = i->ptr[i->eltnum++];
145 }
146
147 /* Skip bits that are zero. */
148
149 for (; (i->word & 1) == 0; i->word >>= 1)
150 i->bit_num++;
151
152 *n = i->bit_num;
153 return true;
154 }
155
156 static inline void
157 ebitmap_iter_next (ebitmap_iterator *i)
158 {
159 i->word >>= 1;
160 i->bit_num++;
161 }
162
163 /* Loop over all elements of EBITMAP, starting with MIN. In each
164 iteration, N is set to the index of the bit being visited. ITER is
165 an instance of ebitmap_iterator used to iterate the bitmap. */
166
167 #define EXECUTE_IF_SET_IN_EBITMAP(EBITMAP, MIN, N, ITER) \
168 for (ebitmap_iter_init (&(ITER), (EBITMAP), (MIN)); \
169 ebitmap_iter_cond (&(ITER), &(N)); \
170 ebitmap_iter_next (&(ITER)))
171
172
173 #endif /* ! GCC_EBITMAP_H */