1 /* Sparse array-based bitmaps.
2 Copyright (C) 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
3 Contributed by Daniel Berlin <dberlin@dberlin.org>
5 This file is part of GCC.
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 3, or (at your option) any later
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
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
30 /* The ebitmap data structure is a sparse bitmap structure that works
32 1. An array of all nonzero words in the structures, organized as
33 an array of HOST_WIDE_INT's.
34 2. A non-sparse bitmap saying which bitmap words are present in the
37 As a consequence of this representation, testing whether a bit
38 exists in the bitmap is faster than linked-list bitmaps. For bits
39 in words that are all zero, the time to test is O(1). For bits in
40 words that exist, it requires O(n/sizeof(word)) time to perform the
41 test (ignoring the one element cache). It also has much better
42 locality than linked-list bitmaps.
44 To test whether a bit exists, we do the following:
45 1. Test whether the word the bit would appear in exists in the
46 bitmap (O(1) check of the mask). If not, fail.
48 2. Population count the mask up to the word containing the bit, to
49 find the place in the array the word would be (popcount is cached,
50 but this is just as slow as the linked-list bitmap)
51 3. Test the word to see if the bit exists.
53 Just like linked-list bitmaps, we cache the last element that has
54 been tested in order to speed up common access patterns.
56 Most of the other speedups are simply due to better locality of the
57 single contiguous array.
59 As would be expected in a structure like this, insertion into an
60 empty word in the middle requires moving bits to make room for the
61 new word. As most operations that perform sets perform them in
62 order, this is rarely a problem. We also take advantage of the
63 same element cache to make repeated sets to the same word O(1).
65 Operations on the entire bitmap are also more efficient than linked
66 list bitmaps, as they are all operating on contiguous memory, and
67 can be easily vectorized. They are all O(number of words) +
68 O(number of bits that may end up in the destination), as the
69 appropriate operation is first performed on the word mask, and then
70 only those elements that may generate results are touched.
72 Memory wise, the ebitmap starts out using less memory than the
73 linked-list bitmap, but its size in memory is technically bounded
74 by ((index of the highest bit set) / (size of a word) + (index of
75 the highest bit set) / ((size of a word) * (size of a word))) This
76 is because the mask is non-sparse. The mask could be transformed
77 into a sparse bitmap at the cost of making bit testing
78 *theoretically* slower (real world numbers have not been computed
79 yet as to whether it matters or not). */
81 /* #define EBITMAP_DEBUGGING */
83 /* Find the last set bit in ebitmap MAP. */
86 ebitmap_last_set_bit (ebitmap map
)
90 bool foundbit
= false;
92 /* This is not the fastest way to do this, we could simply look for
93 the popcount, and start there, but this function is not used
94 anywhere speed critical. */
95 EXECUTE_IF_SET_IN_EBITMAP (map
, 0, i
, ebi
)
106 /* Grow or shrink the internal word array for MAP to NEWSIZE
110 ebitmap_array_grow (ebitmap map
, unsigned int newsize
)
112 if (newsize
<= map
->n_elts
)
114 map
->n_elts
= newsize
;
118 newsize
+= newsize
/ 4;
120 map
->n_elts
= newsize
;
121 map
->elts
= XRESIZEVEC (EBITMAP_ELT_TYPE
, map
->elts
, newsize
);
124 /* Grow the internal word array for MAP so it is at least SIZE
125 elements. Will not shrink the array if it is already big
129 ebitmap_array_maybe_grow (ebitmap map
, unsigned int size
)
131 if (size
>= map
->n_elts
)
132 ebitmap_array_grow (map
, size
+ 1);
135 /* Retrieve the IDX'th element from the word array for MAP. */
137 static inline EBITMAP_ELT_TYPE
138 ebitmap_array_get (ebitmap map
, unsigned int idx
)
140 gcc_assert (idx
< map
->n_elts
);
141 return map
->elts
[idx
];
144 /* Retrieve a pointer to the IDX'th element from the word array for
145 MAP. If the element index is greater than the size of the array,
146 the array will be grown to the correct size. */
148 static inline EBITMAP_ELT_TYPE
*
149 ebitmap_array_grow_get (ebitmap map
, unsigned int idx
)
151 if (idx
>= map
->n_elts
)
152 ebitmap_array_grow (map
, idx
+ 1);
153 return &map
->elts
[idx
];
156 /* Initialize the internal word array for MAP, so that it is SIZE
160 ebitmap_array_init (ebitmap map
, unsigned int size
)
164 map
->elts
= XNEWVEC (EBITMAP_ELT_TYPE
, size
);
174 /* Free the internal word array for MAP. */
177 ebitmap_array_clear (ebitmap map
)
187 /* Clear ebitmap MAP. */
190 ebitmap_clear (ebitmap map
)
192 ebitmap_array_clear (map
);
193 sbitmap_zero (map
->wordmask
);
194 map
->wordmask
= sbitmap_resize (map
->wordmask
, 1, 0);
200 /* Allocate an ebitmap with enough room for SIZE bits to start out. */
203 ebitmap_alloc (unsigned int size
)
205 ebitmap ret
= XNEW (struct ebitmap_def
);
207 size
= EBITMAP_ELT_BITS
;
208 ebitmap_array_init (ret
, (size
+ EBITMAP_ELT_BITS
- 1) / EBITMAP_ELT_BITS
);
209 ret
->wordmask
= sbitmap_alloc_with_popcount (size
);
210 sbitmap_zero (ret
->wordmask
);
218 /* Clear BIT from ebitmap MAP. */
221 ebitmap_clear_bit (ebitmap map
, unsigned int bit
)
223 unsigned int wordindex
= bit
/ EBITMAP_ELT_BITS
;
224 unsigned int eltwordindex
= 0;
225 unsigned int bitindex
, shift
;
226 bool have_eltwordindex
= false;
227 EBITMAP_ELT_TYPE
*elt_ptr
;
229 /* If the bit can't exist in our bitmap, just return. */
230 if (map
->numwords
== 0)
233 if (wordindex
>= map
->wordmask
->n_bits
234 || !TEST_BIT (map
->wordmask
, wordindex
))
237 if (map
->cache
!= NULL
&& map
->cacheindex
== wordindex
)
238 elt_ptr
= map
->cache
;
241 eltwordindex
= sbitmap_popcount (map
->wordmask
, wordindex
);
242 elt_ptr
= &map
->elts
[eltwordindex
];
243 have_eltwordindex
= true;
246 bitindex
= bit
% EBITMAP_ELT_BITS
;
249 *(elt_ptr
) &= ~(((EBITMAP_ELT_TYPE
)1) << shift
);
251 /* Clear out the empty words. */
254 if (!have_eltwordindex
)
255 eltwordindex
= sbitmap_popcount (map
->wordmask
, wordindex
);
257 if (map
->cache
!= NULL
)
259 if (map
->cacheindex
== wordindex
)
261 else if (map
->cacheindex
> wordindex
)
262 map
->cache
= map
->cache
- 1;
265 RESET_BIT (map
->wordmask
, wordindex
);
267 memmove(&map
->elts
[eltwordindex
], &map
->elts
[eltwordindex
+ 1],
268 sizeof (EBITMAP_ELT_TYPE
) * (map
->numwords
- eltwordindex
));
273 /* Set BIT in ebitmap MAP. */
276 ebitmap_set_bit (ebitmap map
, unsigned int bit
)
278 unsigned int wordindex
= bit
/ EBITMAP_ELT_BITS
;
279 unsigned int eltwordindex
;
280 unsigned int bitindex
= bit
% EBITMAP_ELT_BITS
;
282 /* If we have this element cached, just set the bit in it. */
283 if (map
->cache
&& map
->cacheindex
== wordindex
)
285 (*map
->cache
) |= (EBITMAP_ELT_TYPE
)1 << bitindex
;
289 /* Resize the wordmask if necessary. */
290 if (wordindex
>= map
->wordmask
->n_bits
)
291 map
->wordmask
= sbitmap_resize (map
->wordmask
, wordindex
+ 1, 0);
293 /* Allocate a new word in the array and move whatever is in it's
294 place, if necessary. */
295 if (!TEST_BIT (map
->wordmask
, wordindex
))
300 SET_BIT (map
->wordmask
, wordindex
);
301 count
= sbitmap_popcount (map
->wordmask
, wordindex
);
302 gcc_assert (count
<= map
->numwords
);
304 for (i
= map
->numwords
; i
> count
; i
--)
306 EBITMAP_ELT_TYPE
*newelt
;
307 newelt
= ebitmap_array_grow_get (map
, i
);
308 *newelt
= ebitmap_array_get (map
, i
- 1);
311 eltwordindex
= count
;
312 ebitmap_array_maybe_grow (map
, eltwordindex
);
313 map
->elts
[eltwordindex
] = 0;
317 eltwordindex
= sbitmap_popcount (map
->wordmask
, wordindex
);
320 /* Set the actual bit. */
321 bitindex
= bit
% EBITMAP_ELT_BITS
;
323 map
->elts
[eltwordindex
] |= (EBITMAP_ELT_TYPE
)1 << bitindex
;
324 map
->cache
= &map
->elts
[eltwordindex
];
325 map
->cacheindex
= wordindex
;
329 /* Return true if MAP contains BIT. */
332 ebitmap_bit_p (ebitmap map
, unsigned int bit
)
334 unsigned int wordindex
= bit
/ EBITMAP_ELT_BITS
;
335 unsigned int bitindex
= bit
% EBITMAP_ELT_BITS
;
337 /* Trivial empty ebitmap case. */
338 if (map
->numwords
== 0)
341 if (map
->cache
&& map
->cacheindex
== wordindex
)
342 return ((*map
->cache
) >> bitindex
) & 1;
344 /* If the wordindex is greater than the size of the wordmask, *or*
345 it's not set in the wordmask, this bit can't exist in our
347 if (wordindex
>= map
->wordmask
->n_bits
348 || !TEST_BIT (map
->wordmask
, wordindex
))
351 /* Find the bit and test it. */
352 map
->cacheindex
= wordindex
;
353 wordindex
= sbitmap_popcount (map
->wordmask
, wordindex
);
354 map
->cache
= &map
->elts
[wordindex
];
356 return (map
->elts
[wordindex
] >> bitindex
) & 1;
359 /* Copy ebitmap SRC to DST. */
362 ebitmap_copy (ebitmap dst
, ebitmap src
)
364 /* Blow away any existing wordmask, and copy the new one. */
365 sbitmap_free (dst
->wordmask
);
366 dst
->wordmask
= sbitmap_alloc_with_popcount (src
->wordmask
->n_bits
);
367 sbitmap_copy (dst
->wordmask
, src
->wordmask
);
369 /* Make sure our destination array is big enough, and then copy the
371 ebitmap_array_grow (dst
, src
->numwords
);
372 memcpy (dst
->elts
, src
->elts
,
373 src
->numwords
* sizeof (EBITMAP_ELT_TYPE
));
374 dst
->numwords
= src
->numwords
;
378 /* Dump ebitmap BMAP to FILE. */
381 dump_ebitmap (FILE *file
, ebitmap bmap
)
388 res
= sbitmap_last_set_bit (bmap
->wordmask
);
392 size
= (res
+ 1) * EBITMAP_ELT_BITS
;
394 fprintf (file
, "n_words = %d, set = {", bmap
->numwords
);
396 for (pos
= 30, i
= 0; i
< size
; i
++)
397 if (ebitmap_bit_p (bmap
, i
))
401 fprintf (file
, "\n ");
405 pos
+= fprintf (file
, "%d ", i
);
408 fprintf (file
, "}\n");
411 /* Dump ebitmap BMAP to stderr. */
414 debug_ebitmap (ebitmap bmap
)
416 dump_ebitmap (stderr
, bmap
);
419 /* Perform the operation DST &= SRC. */
422 ebitmap_and_into (ebitmap dst
, ebitmap src
)
424 sbitmap_iterator sbi
;
426 unsigned int neweltindex
= 0;
427 unsigned int dsteltindex
= 0;
428 unsigned int srceltindex
= 0;
430 gcc_assert (dst
!= src
);
434 /* Short circuit the empty bitmap cases. */
435 if (src
->numwords
== 0 || dst
->numwords
== 0)
441 /* AND the masks, then walk the words that may actually appear in
442 the result, AND'ing them. */
443 sbitmap_a_and_b (dst
->wordmask
, dst
->wordmask
, src
->wordmask
);
445 EXECUTE_IF_SET_IN_SBITMAP (dst
->wordmask
, 0, i
, sbi
)
447 EBITMAP_ELT_TYPE tmpword
= ebitmap_array_get (src
, srceltindex
++);
448 tmpword
&= ebitmap_array_get (dst
, dsteltindex
++);
451 EBITMAP_ELT_TYPE
*dstplace
;
452 dstplace
= ebitmap_array_grow_get (dst
, neweltindex
++);
456 RESET_BIT (dst
->wordmask
, i
);
458 #ifdef EBITMAP_DEBUGGING
462 for (i
= 0; i
< dst
->numwords
; i
++)
463 gcc_assert (dst
->elts
[i
] != 0);
465 sbitmap_verify_popcount (dst
->wordmask
);
466 gcc_assert (sbitmap_popcount (dst
->wordmask
,
467 dst
->wordmask
->n_bits
) == dst
->numwords
);
470 dst
->numwords
= neweltindex
;
473 /* Perform the operation DST = SRC1 & SRC2. */
476 ebitmap_and (ebitmap dst
, ebitmap src1
, ebitmap src2
)
478 sbitmap_iterator sbi
;
480 unsigned int neweltindex
= 0;
481 unsigned int src1eltindex
= 0;
482 unsigned int src2eltindex
= 0;
485 if (src1
->numwords
== 0 || src2
->numwords
== 0)
492 = sbitmap_resize (dst
->wordmask
,
493 MIN (src1
->wordmask
->n_bits
, src2
->wordmask
->n_bits
),
495 sbitmap_a_and_b (dst
->wordmask
, src1
->wordmask
, src2
->wordmask
);
497 EXECUTE_IF_SET_IN_SBITMAP (dst
->wordmask
, 0, i
, sbi
)
499 bool src1hasword
, src2hasword
;
501 src1hasword
= TEST_BIT (src1
->wordmask
, i
);
502 src2hasword
= TEST_BIT (src2
->wordmask
, i
);
504 if (src1hasword
&& src2hasword
)
506 EBITMAP_ELT_TYPE tmpword
= ebitmap_array_get (src1
, src1eltindex
++);
507 tmpword
&= ebitmap_array_get (src2
, src2eltindex
++);
510 EBITMAP_ELT_TYPE
*dstplace
;
511 dstplace
= ebitmap_array_grow_get (dst
, neweltindex
++);
515 RESET_BIT (dst
->wordmask
, i
);
517 else if (src1hasword
)
522 #ifdef EBITMAP_DEBUGGING
524 ebitmap_iterator ebi
;
527 for (i
= 0; i
< dst
->numwords
; i
++)
528 gcc_assert (dst
->elts
[i
] != 0);
530 EXECUTE_IF_SET_IN_EBITMAP (src1
, 0, i
, ebi
)
531 if (ebitmap_bit_p (src2
, i
))
532 gcc_assert (ebitmap_bit_p (dst
, i
));
534 for (i
= 0; i
< dst
->numwords
; i
++)
535 gcc_assert (dst
->elts
[i
] != 0);
537 sbitmap_verify_popcount (dst
->wordmask
);
538 gcc_assert (sbitmap_popcount (dst
->wordmask
,
539 dst
->wordmask
->n_bits
) == dst
->numwords
);
542 dst
->numwords
= neweltindex
;
545 /* Perform the operation DST |= SRC. Return true if any bits in DST
549 ebitmap_ior_into (ebitmap dst
, ebitmap src
)
551 unsigned int dstsize
= dst
->wordmask
->n_bits
;
552 unsigned int srcsize
= src
->wordmask
->n_bits
;
553 sbitmap_iterator sbi
;
556 unsigned int neweltindex
= 0;
557 unsigned int dsteltindex
= 0;
558 unsigned int srceltindex
= 0;
559 bool changed
= false;
560 EBITMAP_ELT_TYPE
*newarray
;
561 unsigned int newarraysize
;
562 #ifdef EBITMAP_DEBUGGING
563 ebitmap dstcopy
= ebitmap_alloc (1);
564 ebitmap_copy (dstcopy
, dst
);
571 if (dst
->numwords
== 0 && src
->numwords
!= 0)
573 ebitmap_copy (dst
, src
);
576 else if (src
->numwords
== 0)
579 /* We can do without the temp mask if it's faster, but it would mean
580 touching more words in the actual dense vector. */
581 tempmask
= sbitmap_alloc (MAX (srcsize
, dstsize
));
582 sbitmap_zero (tempmask
);
583 if (srcsize
== dstsize
)
585 sbitmap_a_or_b (tempmask
, dst
->wordmask
, src
->wordmask
);
589 dst
->wordmask
= sbitmap_resize (dst
->wordmask
, MAX (srcsize
, dstsize
),
591 if (srcsize
>= dstsize
)
593 sbitmap_copy_n (tempmask
, dst
->wordmask
, dst
->wordmask
->size
);
594 sbitmap_a_or_b (tempmask
, tempmask
, src
->wordmask
);
598 sbitmap_copy_n (tempmask
, src
->wordmask
, src
->wordmask
->size
);
599 sbitmap_a_or_b (tempmask
, tempmask
, dst
->wordmask
);
602 newarraysize
= src
->numwords
+ dst
->numwords
;
603 newarray
= XNEWVEC (EBITMAP_ELT_TYPE
, newarraysize
);
605 EXECUTE_IF_SET_IN_SBITMAP (tempmask
, 0, i
, sbi
)
607 bool dsthasword
, srchasword
;
609 dsthasword
= (i
< dst
->wordmask
->n_bits
610 && TEST_BIT (dst
->wordmask
, i
));
611 srchasword
= (i
< src
->wordmask
->n_bits
612 && TEST_BIT (src
->wordmask
, i
));
614 if (dsthasword
&& srchasword
)
616 EBITMAP_ELT_TYPE tmpword
= ebitmap_array_get (src
, srceltindex
++);
617 tmpword
|= ebitmap_array_get (dst
, dsteltindex
);
619 changed
|= tmpword
!= ebitmap_array_get (dst
, dsteltindex
);
621 newarray
[neweltindex
++] = tmpword
;
625 newarray
[neweltindex
++] = ebitmap_array_get (dst
, dsteltindex
++);
629 newarray
[neweltindex
++] = ebitmap_array_get (src
, srceltindex
++);
630 gcc_assert (i
< dst
->wordmask
->n_bits
);
631 SET_BIT (dst
->wordmask
, i
);
636 sbitmap_free (tempmask
);
639 dst
->numwords
= neweltindex
;
641 dst
->elts
= newarray
;
642 dst
->n_elts
= newarraysize
;
647 #ifdef EBITMAP_DEBUGGING
649 ebitmap_iterator ebi
;
652 for (i
= 0; i
< dst
->numwords
; i
++)
653 gcc_assert (dst
->elts
[i
] != 0);
655 EXECUTE_IF_SET_IN_EBITMAP (src
, 0, i
, ebi
)
656 gcc_assert (ebitmap_bit_p (dst
, i
));
657 EXECUTE_IF_SET_IN_EBITMAP (dstcopy
, 0, i
, ebi
)
658 gcc_assert (ebitmap_bit_p (dst
, i
));
660 sbitmap_verify_popcount (dst
->wordmask
);
661 gcc_assert (changed
== !ebitmap_equal_p (dst
, dstcopy
));
662 gcc_assert (sbitmap_popcount (dst
->wordmask
,
663 dst
->wordmask
->n_bits
) == dst
->numwords
);
669 /* Perform the operation DST = SRC1 | SRC2. Return true if any bit
670 in DST has changed. */
673 ebitmap_ior (ebitmap dst
, ebitmap src1
, ebitmap src2
)
675 unsigned int src1size
= src1
->wordmask
->n_bits
;
676 unsigned int src2size
= src2
->wordmask
->n_bits
;
677 sbitmap_iterator sbi
;
680 unsigned int neweltindex
= 0;
681 unsigned int src1eltindex
= 0;
682 unsigned int src2eltindex
= 0;
683 bool changed
= false;
684 EBITMAP_ELT_TYPE
*newarray
;
685 unsigned int newarraysize
;
686 #ifdef EBITMAP_DEBUGGING
687 ebitmap dstcopy
= ebitmap_alloc (1);
688 ebitmap_copy (dstcopy
, dst
);
692 tempmask
= sbitmap_alloc_with_popcount (MAX (src1size
, src2size
));
693 sbitmap_zero (tempmask
);
694 if (src1size
== src2size
)
696 sbitmap_a_or_b (tempmask
, src1
->wordmask
, src2
->wordmask
);
700 if (src1size
>= src2size
)
702 sbitmap_copy_n (tempmask
, src2
->wordmask
, src2
->wordmask
->size
);
703 sbitmap_a_or_b (tempmask
, tempmask
, src1
->wordmask
);
707 sbitmap_copy_n (tempmask
, src1
->wordmask
, src1
->wordmask
->size
);
708 sbitmap_a_or_b (tempmask
, tempmask
, src2
->wordmask
);
711 newarraysize
= src1
->numwords
+ src2
->numwords
;
712 newarray
= XNEWVEC (EBITMAP_ELT_TYPE
, newarraysize
);
714 EXECUTE_IF_SET_IN_SBITMAP (tempmask
, 0, i
, sbi
)
716 bool src1hasword
, src2hasword
;
717 EBITMAP_ELT_TYPE tmpword
;
718 src1hasword
= (i
< src1
->wordmask
->n_bits
719 && TEST_BIT (src1
->wordmask
, i
));
720 src2hasword
= (i
< src2
->wordmask
->n_bits
721 && TEST_BIT (src2
->wordmask
, i
));
723 if (src1hasword
&& src2hasword
)
725 tmpword
= (ebitmap_array_get (src1
, src1eltindex
++)
726 | ebitmap_array_get (src2
, src2eltindex
++));
727 newarray
[neweltindex
++] = tmpword
;
729 else if (src1hasword
)
731 tmpword
= ebitmap_array_get (src1
, src1eltindex
++);
732 newarray
[neweltindex
++] = tmpword
;
736 tmpword
= ebitmap_array_get (src2
, src2eltindex
++);
737 newarray
[neweltindex
++] = tmpword
;
740 if (i
>= dst
->wordmask
->n_bits
|| !TEST_BIT (dst
->wordmask
, i
))
746 unsigned int count
= sbitmap_popcount (dst
->wordmask
, i
);
747 changed
|= ebitmap_array_get (dst
, count
) != tmpword
;
753 sbitmap_free (dst
->wordmask
);
754 dst
->wordmask
= tempmask
;
755 dst
->numwords
= neweltindex
;
757 dst
->elts
= newarray
;
758 dst
->n_elts
= newarraysize
;
762 sbitmap_free (tempmask
);
766 #ifdef EBITMAP_DEBUGGING
768 ebitmap_iterator ebi
;
771 for (i
= 0; i
< dst
->numwords
; i
++)
772 gcc_assert (dst
->elts
[i
] != 0);
774 EXECUTE_IF_SET_IN_EBITMAP (src1
, 0, i
, ebi
)
775 gcc_assert (ebitmap_bit_p (dst
, i
));
777 EXECUTE_IF_SET_IN_EBITMAP (src2
, 0, i
, ebi
)
778 gcc_assert (ebitmap_bit_p (dst
, i
));
780 sbitmap_verify_popcount (dst
->wordmask
);
781 gcc_assert (changed
== !ebitmap_equal_p (dst
, dstcopy
));
782 gcc_assert (sbitmap_popcount (dst
->wordmask
,
783 dst
->wordmask
->n_bits
) == dst
->numwords
);
789 /* Perform the operation DST &= ~SRC. Return true if any bit in DST
793 ebitmap_and_compl_into (ebitmap dst
, ebitmap src
)
795 bool changed
= false;
797 unsigned int neweltindex
= 0;
798 unsigned int dsteltindex
= 0;
799 sbitmap_iterator sbi
;
800 #ifdef EBITMAP_DEBUGGING
801 ebitmap dstcopy
= ebitmap_alloc (1);
802 ebitmap_copy (dstcopy
, dst
);
805 gcc_assert (dst
!= src
);
807 if (src
->numwords
== 0)
810 EXECUTE_IF_SET_IN_SBITMAP (dst
->wordmask
, 0, i
, sbi
)
814 srchasword
= (i
< src
->wordmask
->n_bits
815 && TEST_BIT (src
->wordmask
, i
));
819 unsigned int srccount
= sbitmap_popcount (src
->wordmask
, i
);
820 EBITMAP_ELT_TYPE tmpword
= ebitmap_array_get (dst
, dsteltindex
);
821 tmpword
&= ~(ebitmap_array_get (src
, srccount
));
823 changed
|= ebitmap_array_get (dst
, dsteltindex
) != tmpword
;
827 EBITMAP_ELT_TYPE
*dstplace
;
828 dstplace
= ebitmap_array_grow_get (dst
, neweltindex
++);
832 RESET_BIT (dst
->wordmask
, i
);
840 #ifdef EBITMAP_DEBUGGING
843 ebitmap_iterator ebi
;
845 EXECUTE_IF_SET_IN_EBITMAP (dstcopy
, 0, i
, ebi
)
847 if (!ebitmap_bit_p (src
, i
))
848 gcc_assert (ebitmap_bit_p (dst
, i
));
851 for (i
= 0; i
< dst
->numwords
; i
++)
852 gcc_assert (dst
->elts
[i
] != 0);
854 gcc_assert (sbitmap_popcount (dst
->wordmask
,
855 dst
->wordmask
->n_bits
) == neweltindex
);
856 sbitmap_verify_popcount (dst
->wordmask
);
857 gcc_assert (changed
== !ebitmap_equal_p (dst
, dstcopy
));
858 gcc_assert (sbitmap_popcount (dst
->wordmask
,
859 dst
->wordmask
->n_bits
) == dst
->numwords
);
862 dst
->numwords
= neweltindex
;
863 /* sbitmap_popcount (dst->wordmask, -1); */
868 /* Perform the operation DST = SRC1 & ~SRC2. Return true if any bit
869 in DST has changed. */
872 ebitmap_and_compl (ebitmap dst
, ebitmap src1
, ebitmap src2
)
874 unsigned int src1size
= src1
->wordmask
->n_bits
;
875 sbitmap_iterator sbi
;
878 unsigned int neweltindex
= 0;
879 unsigned int src1eltindex
= 0;
880 bool changed
= false;
881 EBITMAP_ELT_TYPE
*newarray
;
882 unsigned int newarraysize
;
884 /* XXX: Optimize like the into version. */
886 tempmask
= sbitmap_alloc_with_popcount (src1size
);
887 sbitmap_zero (tempmask
);
888 sbitmap_copy (tempmask
, src1
->wordmask
);
890 newarraysize
= src1
->numwords
;
891 newarray
= XNEWVEC (EBITMAP_ELT_TYPE
, newarraysize
);
893 EXECUTE_IF_SET_IN_SBITMAP (src1
->wordmask
, 0, i
, sbi
)
896 EBITMAP_ELT_TYPE tmpword
;
898 src2hasword
= (i
< src2
->wordmask
->n_bits
899 && TEST_BIT (src2
->wordmask
, i
));
903 unsigned int src2count
= sbitmap_popcount (src2
->wordmask
, i
);
904 tmpword
= ebitmap_array_get (src1
, src1eltindex
++)
905 & ~(ebitmap_array_get (src2
, src2count
));
908 newarray
[neweltindex
++] = tmpword
;
911 RESET_BIT (tempmask
, i
);
916 tmpword
= ebitmap_array_get (src1
, src1eltindex
++);
917 gcc_assert (tmpword
!= 0);
918 newarray
[neweltindex
++] = tmpword
;
921 if (i
>= dst
->wordmask
->n_bits
|| !TEST_BIT (dst
->wordmask
, i
))
927 unsigned int count
= sbitmap_popcount (dst
->wordmask
, i
);
928 changed
|= ebitmap_array_get (dst
, count
) != tmpword
;
933 sbitmap_free (dst
->wordmask
);
934 dst
->wordmask
= tempmask
;
935 dst
->numwords
= neweltindex
;
937 dst
->elts
= newarray
;
938 dst
->n_elts
= newarraysize
;
945 #ifdef EBITMAP_DEBUGGING
948 ebitmap_iterator ebi
;
950 EXECUTE_IF_SET_IN_EBITMAP (src1
, 0, i
, ebi
)
952 if (!ebitmap_bit_p (src2
, i
))
953 gcc_assert (ebitmap_bit_p (dst
, i
));
955 for (i
= 0; i
< dst
->numwords
; i
++)
956 gcc_assert (dst
->elts
[i
] != 0);
958 sbitmap_verify_popcount (dst
->wordmask
);
959 gcc_assert (sbitmap_popcount (dst
->wordmask
,
960 dst
->wordmask
->n_bits
) == dst
->numwords
);
966 /* Perform the operation DST = A | (B & ~C). */
969 ebitmap_ior_and_compl (ebitmap dst
, ebitmap a
, ebitmap b
, ebitmap c
)
972 ebitmap temp
= ebitmap_alloc (1);
973 #ifdef EBITMAP_DEBUGGING
974 ebitmap dstcopy
= ebitmap_alloc (1);
975 ebitmap_copy (dstcopy
, dst
);
979 ebitmap_and_compl (temp
, b
, c
);
980 changed
= ebitmap_ior (dst
, a
, temp
);
981 #ifdef EBITMAP_DEBUGGING
983 ebitmap_iterator ebi
;
986 EXECUTE_IF_SET_IN_EBITMAP (a
, 0, i
, ebi
)
987 gcc_assert (ebitmap_bit_p (dst
, i
));
989 EXECUTE_IF_SET_IN_EBITMAP (b
, 0, i
, ebi
)
990 if (!ebitmap_bit_p (c
, i
))
991 gcc_assert (ebitmap_bit_p (dst
, i
));
992 gcc_assert (changed
== !ebitmap_equal_p (dst
, dstcopy
));
1000 /* Return true if ebitmap DST is equal to ebitmap SRC. */
1003 ebitmap_equal_p (ebitmap dst
, ebitmap src
)
1005 unsigned int which
= MIN (dst
->wordmask
->size
, src
->wordmask
->size
);
1007 if (dst
->numwords
!= src
->numwords
)
1010 /* sbitmap_equal compares up to the size of the first argument, so
1011 if the two sbitmaps are not equally sized, we need to pass the
1012 smaller one as the first argument, or it will crash. */
1013 if (which
== dst
->wordmask
->size
1014 && !sbitmap_equal (dst
->wordmask
, src
->wordmask
))
1016 else if (which
== src
->wordmask
->size
1017 && !sbitmap_equal (src
->wordmask
, dst
->wordmask
))
1020 return memcmp (dst
->elts
, src
->elts
,
1021 dst
->numwords
* sizeof (EBITMAP_ELT_TYPE
)) == 0;