Merge from mainline (165734:167278).
[official-gcc/graphite-test-results.git] / gcc / ebitmap.c
blobc57d141ddb31c327aa99e6e805b3a9f79ef0bbfa
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
10 version.
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.
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/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "ebitmap.h"
26 /* The ebitmap data structure is a sparse bitmap structure that works
27 by having two pieces:
28 1. An array of all nonzero words in the structures, organized as
29 an array of HOST_WIDE_INT's.
30 2. A non-sparse bitmap saying which bitmap words are present in the
31 array.
33 As a consequence of this representation, testing whether a bit
34 exists in the bitmap is faster than linked-list bitmaps. For bits
35 in words that are all zero, the time to test is O(1). For bits in
36 words that exist, it requires O(n/sizeof(word)) time to perform the
37 test (ignoring the one element cache). It also has much better
38 locality than linked-list bitmaps.
40 To test whether a bit exists, we do the following:
41 1. Test whether the word the bit would appear in exists in the
42 bitmap (O(1) check of the mask). If not, fail.
44 2. Population count the mask up to the word containing the bit, to
45 find the place in the array the word would be (popcount is cached,
46 but this is just as slow as the linked-list bitmap)
47 3. Test the word to see if the bit exists.
49 Just like linked-list bitmaps, we cache the last element that has
50 been tested in order to speed up common access patterns.
52 Most of the other speedups are simply due to better locality of the
53 single contiguous array.
55 As would be expected in a structure like this, insertion into an
56 empty word in the middle requires moving bits to make room for the
57 new word. As most operations that perform sets perform them in
58 order, this is rarely a problem. We also take advantage of the
59 same element cache to make repeated sets to the same word O(1).
61 Operations on the entire bitmap are also more efficient than linked
62 list bitmaps, as they are all operating on contiguous memory, and
63 can be easily vectorized. They are all O(number of words) +
64 O(number of bits that may end up in the destination), as the
65 appropriate operation is first performed on the word mask, and then
66 only those elements that may generate results are touched.
68 Memory wise, the ebitmap starts out using less memory than the
69 linked-list bitmap, but its size in memory is technically bounded
70 by ((index of the highest bit set) / (size of a word) + (index of
71 the highest bit set) / ((size of a word) * (size of a word))) This
72 is because the mask is non-sparse. The mask could be transformed
73 into a sparse bitmap at the cost of making bit testing
74 *theoretically* slower (real world numbers have not been computed
75 yet as to whether it matters or not). */
77 /* #define EBITMAP_DEBUGGING */
79 /* Find the last set bit in ebitmap MAP. */
81 int
82 ebitmap_last_set_bit (ebitmap map)
84 unsigned int i = 0;
85 ebitmap_iterator ebi;
86 bool foundbit = false;
88 /* This is not the fastest way to do this, we could simply look for
89 the popcount, and start there, but this function is not used
90 anywhere speed critical. */
91 EXECUTE_IF_SET_IN_EBITMAP (map, 0, i, ebi)
93 foundbit = true;
97 if (foundbit)
98 return i;
99 return -1;
102 /* Grow or shrink the internal word array for MAP to NEWSIZE
103 elements. */
105 static inline void
106 ebitmap_array_grow (ebitmap map, unsigned int newsize)
108 if (newsize <= map->n_elts)
110 map->n_elts = newsize;
111 return;
114 newsize += newsize / 4;
116 map->n_elts = newsize;
117 map->elts = XRESIZEVEC (EBITMAP_ELT_TYPE, map->elts, newsize);
120 /* Grow the internal word array for MAP so it is at least SIZE
121 elements. Will not shrink the array if it is already big
122 enough. */
124 static inline void
125 ebitmap_array_maybe_grow (ebitmap map, unsigned int size)
127 if (size >= map->n_elts)
128 ebitmap_array_grow (map, size + 1);
131 /* Retrieve the IDX'th element from the word array for MAP. */
133 static inline EBITMAP_ELT_TYPE
134 ebitmap_array_get (ebitmap map, unsigned int idx)
136 gcc_assert (idx < map->n_elts);
137 return map->elts[idx];
140 /* Retrieve a pointer to the IDX'th element from the word array for
141 MAP. If the element index is greater than the size of the array,
142 the array will be grown to the correct size. */
144 static inline EBITMAP_ELT_TYPE *
145 ebitmap_array_grow_get (ebitmap map, unsigned int idx)
147 if (idx >= map->n_elts)
148 ebitmap_array_grow (map, idx + 1);
149 return &map->elts[idx];
152 /* Initialize the internal word array for MAP, so that it is SIZE
153 elements. */
155 static inline void
156 ebitmap_array_init (ebitmap map, unsigned int size)
158 if (size > 0)
160 map->elts = XNEWVEC (EBITMAP_ELT_TYPE, size);
161 map->n_elts = size;
163 else
165 map->n_elts = 0;
166 map->elts = NULL;
170 /* Free the internal word array for MAP. */
172 static inline void
173 ebitmap_array_clear (ebitmap map)
175 if (map->elts)
177 free (map->elts);
178 map->elts = NULL;
180 map->n_elts = 0;
183 /* Clear ebitmap MAP. */
185 void
186 ebitmap_clear (ebitmap map)
188 ebitmap_array_clear (map);
189 sbitmap_zero (map->wordmask);
190 map->wordmask = sbitmap_resize (map->wordmask, 1, 0);
191 map->numwords = 0;
192 map->cache = NULL;
193 map->cacheindex = 0;
196 /* Allocate an ebitmap with enough room for SIZE bits to start out. */
198 ebitmap
199 ebitmap_alloc (unsigned int size)
201 ebitmap ret = XNEW (struct ebitmap_def);
202 if (size == 0)
203 size = EBITMAP_ELT_BITS;
204 ebitmap_array_init (ret, (size + EBITMAP_ELT_BITS - 1) / EBITMAP_ELT_BITS);
205 ret->wordmask = sbitmap_alloc_with_popcount (size);
206 sbitmap_zero (ret->wordmask);
207 ret->numwords = 0;
208 ret->cache = NULL;
209 ret->cacheindex = 0;
211 return ret;
214 /* Clear BIT from ebitmap MAP. */
216 void
217 ebitmap_clear_bit (ebitmap map, unsigned int bit)
219 unsigned int wordindex = bit / EBITMAP_ELT_BITS;
220 unsigned int eltwordindex = 0;
221 unsigned int bitindex, shift;
222 bool have_eltwordindex = false;
223 EBITMAP_ELT_TYPE *elt_ptr;
225 /* If the bit can't exist in our bitmap, just return. */
226 if (map->numwords == 0)
227 return;
229 if (wordindex >= map->wordmask->n_bits
230 || !TEST_BIT (map->wordmask, wordindex))
231 return;
233 if (map->cache != NULL && map->cacheindex == wordindex)
234 elt_ptr = map->cache;
235 else
237 eltwordindex = sbitmap_popcount (map->wordmask, wordindex);
238 elt_ptr = &map->elts[eltwordindex];
239 have_eltwordindex = true;
242 bitindex = bit % EBITMAP_ELT_BITS;
243 shift = bitindex;
245 *(elt_ptr) &= ~(((EBITMAP_ELT_TYPE)1) << shift);
247 /* Clear out the empty words. */
248 if (*(elt_ptr) == 0)
250 if (!have_eltwordindex)
251 eltwordindex = sbitmap_popcount (map->wordmask, wordindex);
253 if (map->cache != NULL)
255 if (map->cacheindex == wordindex)
256 map->cache = NULL;
257 else if (map->cacheindex > wordindex)
258 map->cache = map->cache - 1;
261 RESET_BIT (map->wordmask, wordindex);
263 memmove(&map->elts[eltwordindex], &map->elts[eltwordindex + 1],
264 sizeof (EBITMAP_ELT_TYPE) * (map->numwords - eltwordindex));
265 map->numwords--;
269 /* Set BIT in ebitmap MAP. */
271 void
272 ebitmap_set_bit (ebitmap map, unsigned int bit)
274 unsigned int wordindex = bit / EBITMAP_ELT_BITS;
275 unsigned int eltwordindex;
276 unsigned int bitindex = bit % EBITMAP_ELT_BITS;
278 /* If we have this element cached, just set the bit in it. */
279 if (map->cache && map->cacheindex == wordindex)
281 (*map->cache) |= (EBITMAP_ELT_TYPE)1 << bitindex;
282 return;
285 /* Resize the wordmask if necessary. */
286 if (wordindex >= map->wordmask->n_bits)
287 map->wordmask = sbitmap_resize (map->wordmask, wordindex + 1, 0);
289 /* Allocate a new word in the array and move whatever is in it's
290 place, if necessary. */
291 if (!TEST_BIT (map->wordmask, wordindex))
293 unsigned long count;
294 unsigned int i;
296 SET_BIT (map->wordmask, wordindex);
297 count = sbitmap_popcount (map->wordmask, wordindex);
298 gcc_assert (count <= map->numwords);
300 for (i = map->numwords; i > count; i--)
302 EBITMAP_ELT_TYPE *newelt;
303 newelt = ebitmap_array_grow_get (map, i);
304 *newelt = ebitmap_array_get (map, i - 1);
306 map->numwords++;
307 eltwordindex = count;
308 ebitmap_array_maybe_grow (map, eltwordindex);
309 map->elts[eltwordindex] = 0;
311 else
313 eltwordindex = sbitmap_popcount (map->wordmask, wordindex);
316 /* Set the actual bit. */
317 bitindex = bit % EBITMAP_ELT_BITS;
319 map->elts[eltwordindex] |= (EBITMAP_ELT_TYPE)1 << bitindex;
320 map->cache = &map->elts[eltwordindex];
321 map->cacheindex = wordindex;
325 /* Return true if MAP contains BIT. */
327 bool
328 ebitmap_bit_p (ebitmap map, unsigned int bit)
330 unsigned int wordindex = bit / EBITMAP_ELT_BITS;
331 unsigned int bitindex= bit % EBITMAP_ELT_BITS;
333 /* Trivial empty ebitmap case. */
334 if (map->numwords == 0)
335 return false;
337 if (map->cache && map->cacheindex == wordindex)
338 return ((*map->cache) >> bitindex) & 1;
340 /* If the wordindex is greater than the size of the wordmask, *or*
341 it's not set in the wordmask, this bit can't exist in our
342 ebitmap. */
343 if (wordindex >= map->wordmask->n_bits
344 || !TEST_BIT (map->wordmask, wordindex))
345 return false;
347 /* Find the bit and test it. */
348 map->cacheindex = wordindex;
349 wordindex = sbitmap_popcount (map->wordmask, wordindex);
350 map->cache = &map->elts[wordindex];
352 return (map->elts[wordindex] >> bitindex) & 1;
355 /* Copy ebitmap SRC to DST. */
357 void
358 ebitmap_copy (ebitmap dst, ebitmap src)
360 /* Blow away any existing wordmask, and copy the new one. */
361 sbitmap_free (dst->wordmask);
362 dst->wordmask = sbitmap_alloc_with_popcount (src->wordmask->n_bits);
363 sbitmap_copy (dst->wordmask, src->wordmask);
365 /* Make sure our destination array is big enough, and then copy the
366 actual words. */
367 ebitmap_array_grow (dst, src->numwords);
368 memcpy (dst->elts, src->elts,
369 src->numwords * sizeof (EBITMAP_ELT_TYPE));
370 dst->numwords = src->numwords;
371 dst->cache = NULL;
374 /* Dump ebitmap BMAP to FILE. */
376 void
377 dump_ebitmap (FILE *file, ebitmap bmap)
379 unsigned int pos;
380 unsigned int i;
381 int res;
382 unsigned int size;
384 res = sbitmap_last_set_bit (bmap->wordmask);
385 if (res == -1)
386 size = 0;
387 else
388 size = (res + 1) * EBITMAP_ELT_BITS;
390 fprintf (file, "n_words = %d, set = {", bmap->numwords);
392 for (pos = 30, i = 0; i < size; i++)
393 if (ebitmap_bit_p (bmap, i))
395 if (pos > 70)
397 fprintf (file, "\n ");
398 pos = 0;
401 pos += fprintf (file, "%d ", i);
404 fprintf (file, "}\n");
407 /* Dump ebitmap BMAP to stderr. */
409 DEBUG_FUNCTION void
410 debug_ebitmap (ebitmap bmap)
412 dump_ebitmap (stderr, bmap);
415 /* Perform the operation DST &= SRC. */
417 void
418 ebitmap_and_into (ebitmap dst, ebitmap src)
420 sbitmap_iterator sbi;
421 unsigned int i;
422 unsigned int neweltindex = 0;
423 unsigned int dsteltindex = 0;
424 unsigned int srceltindex = 0;
426 gcc_assert (dst != src);
428 dst->cache = NULL;
430 /* Short circuit the empty bitmap cases. */
431 if (src->numwords == 0 || dst->numwords == 0)
433 ebitmap_clear (dst);
434 return;
437 /* AND the masks, then walk the words that may actually appear in
438 the result, AND'ing them. */
439 sbitmap_a_and_b (dst->wordmask, dst->wordmask, src->wordmask);
441 EXECUTE_IF_SET_IN_SBITMAP (dst->wordmask, 0, i, sbi)
443 EBITMAP_ELT_TYPE tmpword = ebitmap_array_get (src, srceltindex++);
444 tmpword &= ebitmap_array_get (dst, dsteltindex++);
445 if (tmpword != 0)
447 EBITMAP_ELT_TYPE *dstplace;
448 dstplace = ebitmap_array_grow_get (dst, neweltindex++);
449 *dstplace = tmpword;
451 else
452 RESET_BIT (dst->wordmask, i);
454 #ifdef EBITMAP_DEBUGGING
456 unsigned int i;
458 for (i = 0; i < dst->numwords; i++)
459 gcc_assert (dst->elts[i] != 0);
461 sbitmap_verify_popcount (dst->wordmask);
462 gcc_assert (sbitmap_popcount (dst->wordmask,
463 dst->wordmask->n_bits) == dst->numwords);
465 #endif
466 dst->numwords = neweltindex;
469 /* Perform the operation DST = SRC1 & SRC2. */
471 void
472 ebitmap_and (ebitmap dst, ebitmap src1, ebitmap src2)
474 sbitmap_iterator sbi;
475 unsigned int i;
476 unsigned int neweltindex = 0;
477 unsigned int src1eltindex = 0;
478 unsigned int src2eltindex = 0;
480 dst->cache = NULL;
481 if (src1->numwords == 0 || src2->numwords == 0)
483 ebitmap_clear (dst);
484 return;
487 dst->wordmask
488 = sbitmap_resize (dst->wordmask,
489 MIN (src1->wordmask->n_bits, src2->wordmask->n_bits),
491 sbitmap_a_and_b (dst->wordmask, src1->wordmask, src2->wordmask);
493 EXECUTE_IF_SET_IN_SBITMAP (dst->wordmask, 0, i, sbi)
495 bool src1hasword, src2hasword;
497 src1hasword = TEST_BIT (src1->wordmask, i);
498 src2hasword = TEST_BIT (src2->wordmask, i);
500 if (src1hasword && src2hasword)
502 EBITMAP_ELT_TYPE tmpword = ebitmap_array_get (src1, src1eltindex++);
503 tmpword &= ebitmap_array_get (src2, src2eltindex++);
504 if (tmpword != 0)
506 EBITMAP_ELT_TYPE *dstplace;
507 dstplace = ebitmap_array_grow_get (dst, neweltindex++);
508 *dstplace = tmpword;
510 else
511 RESET_BIT (dst->wordmask, i);
513 else if (src1hasword)
514 src1eltindex++;
515 else
516 src2eltindex++;
518 #ifdef EBITMAP_DEBUGGING
520 ebitmap_iterator ebi;
521 unsigned int i;
523 for (i = 0; i < dst->numwords; i++)
524 gcc_assert (dst->elts[i] != 0);
526 EXECUTE_IF_SET_IN_EBITMAP (src1, 0, i, ebi)
527 if (ebitmap_bit_p (src2, i))
528 gcc_assert (ebitmap_bit_p (dst, i));
530 for (i = 0; i < dst->numwords; i++)
531 gcc_assert (dst->elts[i] != 0);
533 sbitmap_verify_popcount (dst->wordmask);
534 gcc_assert (sbitmap_popcount (dst->wordmask,
535 dst->wordmask->n_bits) == dst->numwords);
537 #endif
538 dst->numwords = neweltindex;
541 /* Perform the operation DST |= SRC. Return true if any bits in DST
542 changed. */
544 bool
545 ebitmap_ior_into (ebitmap dst, ebitmap src)
547 unsigned int dstsize = dst->wordmask->n_bits;
548 unsigned int srcsize = src->wordmask->n_bits;
549 sbitmap_iterator sbi;
550 unsigned int i;
551 sbitmap tempmask;
552 unsigned int neweltindex = 0;
553 unsigned int dsteltindex = 0;
554 unsigned int srceltindex = 0;
555 bool changed = false;
556 EBITMAP_ELT_TYPE *newarray;
557 unsigned int newarraysize;
558 #ifdef EBITMAP_DEBUGGING
559 ebitmap dstcopy = ebitmap_alloc (1);
560 ebitmap_copy (dstcopy, dst);
561 #endif
563 dst->cache = NULL;
564 if (dst == src)
565 return false;
567 if (dst->numwords == 0 && src->numwords != 0)
569 ebitmap_copy (dst, src);
570 return true;
572 else if (src->numwords == 0)
573 return false;
575 /* We can do without the temp mask if it's faster, but it would mean
576 touching more words in the actual dense vector. */
577 tempmask = sbitmap_alloc (MAX (srcsize, dstsize));
578 sbitmap_zero (tempmask);
579 if (srcsize == dstsize)
581 sbitmap_a_or_b (tempmask, dst->wordmask, src->wordmask);
583 else
585 dst->wordmask = sbitmap_resize (dst->wordmask, MAX (srcsize, dstsize),
587 if (srcsize >= dstsize)
589 sbitmap_copy_n (tempmask, dst->wordmask, dst->wordmask->size);
590 sbitmap_a_or_b (tempmask, tempmask, src->wordmask);
592 else
594 sbitmap_copy_n (tempmask, src->wordmask, src->wordmask->size);
595 sbitmap_a_or_b (tempmask, tempmask, dst->wordmask);
598 newarraysize = src->numwords + dst->numwords;
599 newarray = XNEWVEC (EBITMAP_ELT_TYPE, newarraysize);
601 EXECUTE_IF_SET_IN_SBITMAP (tempmask, 0, i, sbi)
603 bool dsthasword, srchasword;
605 dsthasword = (i < dst->wordmask->n_bits
606 && TEST_BIT (dst->wordmask, i));
607 srchasword = (i < src->wordmask->n_bits
608 && TEST_BIT (src->wordmask, i));
610 if (dsthasword && srchasword)
612 EBITMAP_ELT_TYPE tmpword = ebitmap_array_get (src, srceltindex++);
613 tmpword |= ebitmap_array_get (dst, dsteltindex);
614 if (!changed)
615 changed |= tmpword != ebitmap_array_get (dst, dsteltindex);
616 dsteltindex++;
617 newarray[neweltindex++] = tmpword;
619 else if (dsthasword)
621 newarray [neweltindex++] = ebitmap_array_get (dst, dsteltindex++);
623 else
625 newarray [neweltindex++] = ebitmap_array_get (src, srceltindex++);
626 gcc_assert (i < dst->wordmask->n_bits);
627 SET_BIT (dst->wordmask, i);
628 changed |= true;
632 sbitmap_free (tempmask);
633 if (changed)
635 dst->numwords = neweltindex;
636 free (dst->elts);
637 dst->elts = newarray;
638 dst->n_elts = newarraysize;
640 else
641 free (newarray);
643 #ifdef EBITMAP_DEBUGGING
645 ebitmap_iterator ebi;
646 unsigned int i;
648 for (i = 0; i < dst->numwords; i++)
649 gcc_assert (dst->elts[i] != 0);
651 EXECUTE_IF_SET_IN_EBITMAP (src, 0, i, ebi)
652 gcc_assert (ebitmap_bit_p (dst, i));
653 EXECUTE_IF_SET_IN_EBITMAP (dstcopy, 0, i, ebi)
654 gcc_assert (ebitmap_bit_p (dst, i));
656 sbitmap_verify_popcount (dst->wordmask);
657 gcc_assert (changed == !ebitmap_equal_p (dst, dstcopy));
658 gcc_assert (sbitmap_popcount (dst->wordmask,
659 dst->wordmask->n_bits) == dst->numwords);
661 #endif
662 return changed;
665 /* Perform the operation DST = SRC1 | SRC2. Return true if any bit
666 in DST has changed. */
668 bool
669 ebitmap_ior (ebitmap dst, ebitmap src1, ebitmap src2)
671 unsigned int src1size = src1->wordmask->n_bits;
672 unsigned int src2size = src2->wordmask->n_bits;
673 sbitmap_iterator sbi;
674 unsigned int i;
675 sbitmap tempmask;
676 unsigned int neweltindex = 0;
677 unsigned int src1eltindex = 0;
678 unsigned int src2eltindex = 0;
679 bool changed = false;
680 EBITMAP_ELT_TYPE *newarray;
681 unsigned int newarraysize;
682 #ifdef EBITMAP_DEBUGGING
683 ebitmap dstcopy = ebitmap_alloc (1);
684 ebitmap_copy (dstcopy, dst);
685 #endif
687 dst->cache = NULL;
688 tempmask = sbitmap_alloc_with_popcount (MAX (src1size, src2size));
689 sbitmap_zero (tempmask);
690 if (src1size == src2size)
692 sbitmap_a_or_b (tempmask, src1->wordmask, src2->wordmask);
694 else
696 if (src1size >= src2size)
698 sbitmap_copy_n (tempmask, src2->wordmask, src2->wordmask->size);
699 sbitmap_a_or_b (tempmask, tempmask, src1->wordmask);
701 else
703 sbitmap_copy_n (tempmask, src1->wordmask, src1->wordmask->size);
704 sbitmap_a_or_b (tempmask, tempmask, src2->wordmask);
707 newarraysize = src1->numwords + src2->numwords;
708 newarray = XNEWVEC (EBITMAP_ELT_TYPE, newarraysize);
710 EXECUTE_IF_SET_IN_SBITMAP (tempmask, 0, i, sbi)
712 bool src1hasword, src2hasword;
713 EBITMAP_ELT_TYPE tmpword;
714 src1hasword = (i < src1->wordmask->n_bits
715 && TEST_BIT (src1->wordmask, i));
716 src2hasword = (i < src2->wordmask->n_bits
717 && TEST_BIT (src2->wordmask, i));
719 if (src1hasword && src2hasword)
721 tmpword = (ebitmap_array_get (src1, src1eltindex++)
722 | ebitmap_array_get (src2, src2eltindex++));
723 newarray[neweltindex++] = tmpword;
725 else if (src1hasword)
727 tmpword = ebitmap_array_get (src1, src1eltindex++);
728 newarray [neweltindex++] = tmpword;
730 else
732 tmpword = ebitmap_array_get (src2, src2eltindex++);
733 newarray [neweltindex++] = tmpword;
736 if (i >= dst->wordmask->n_bits || !TEST_BIT (dst->wordmask, i))
738 changed = true;
740 else if (!changed)
742 unsigned int count = sbitmap_popcount (dst->wordmask, i);
743 changed |= ebitmap_array_get (dst, count) != tmpword;
747 if (changed)
749 sbitmap_free (dst->wordmask);
750 dst->wordmask = tempmask;
751 dst->numwords = neweltindex;
752 free (dst->elts);
753 dst->elts = newarray;
754 dst->n_elts = newarraysize;
756 else
758 sbitmap_free (tempmask);
759 free (newarray);
762 #ifdef EBITMAP_DEBUGGING
764 ebitmap_iterator ebi;
765 unsigned int i;
767 for (i = 0; i < dst->numwords; i++)
768 gcc_assert (dst->elts[i] != 0);
770 EXECUTE_IF_SET_IN_EBITMAP (src1, 0, i, ebi)
771 gcc_assert (ebitmap_bit_p (dst, i));
773 EXECUTE_IF_SET_IN_EBITMAP (src2, 0, i, ebi)
774 gcc_assert (ebitmap_bit_p (dst, i));
776 sbitmap_verify_popcount (dst->wordmask);
777 gcc_assert (changed == !ebitmap_equal_p (dst, dstcopy));
778 gcc_assert (sbitmap_popcount (dst->wordmask,
779 dst->wordmask->n_bits) == dst->numwords);
780 #endif
782 return changed;
785 /* Perform the operation DST &= ~SRC. Return true if any bit in DST
786 has changed. */
788 bool
789 ebitmap_and_compl_into (ebitmap dst, ebitmap src)
791 bool changed = false;
792 unsigned int i;
793 unsigned int neweltindex = 0;
794 unsigned int dsteltindex = 0;
795 sbitmap_iterator sbi;
796 #ifdef EBITMAP_DEBUGGING
797 ebitmap dstcopy = ebitmap_alloc (1);
798 ebitmap_copy (dstcopy, dst);
799 #endif
801 gcc_assert (dst != src);
802 dst->cache = NULL;
803 if (src->numwords == 0)
804 return false;
806 EXECUTE_IF_SET_IN_SBITMAP (dst->wordmask, 0, i, sbi)
808 bool srchasword;
810 srchasword = (i < src->wordmask->n_bits
811 && TEST_BIT (src->wordmask, i));
813 if (srchasword)
815 unsigned int srccount = sbitmap_popcount (src->wordmask, i);
816 EBITMAP_ELT_TYPE tmpword = ebitmap_array_get (dst, dsteltindex);
817 tmpword &= ~(ebitmap_array_get (src, srccount));
818 if (!changed)
819 changed |= ebitmap_array_get (dst, dsteltindex) != tmpword;
820 dsteltindex++;
821 if (tmpword != 0)
823 EBITMAP_ELT_TYPE *dstplace;
824 dstplace = ebitmap_array_grow_get (dst, neweltindex++);
825 *dstplace = tmpword;
827 else
828 RESET_BIT (dst->wordmask, i);
830 else
832 dsteltindex++;
833 neweltindex++;
836 #ifdef EBITMAP_DEBUGGING
838 unsigned int i;
839 ebitmap_iterator ebi;
841 EXECUTE_IF_SET_IN_EBITMAP (dstcopy, 0, i, ebi)
843 if (!ebitmap_bit_p (src, i))
844 gcc_assert (ebitmap_bit_p (dst, i));
847 for (i = 0; i < dst->numwords; i++)
848 gcc_assert (dst->elts[i] != 0);
850 gcc_assert (sbitmap_popcount (dst->wordmask,
851 dst->wordmask->n_bits) == neweltindex);
852 sbitmap_verify_popcount (dst->wordmask);
853 gcc_assert (changed == !ebitmap_equal_p (dst, dstcopy));
854 gcc_assert (sbitmap_popcount (dst->wordmask,
855 dst->wordmask->n_bits) == dst->numwords);
857 #endif
858 dst->numwords = neweltindex;
859 /* sbitmap_popcount (dst->wordmask, -1); */
861 return changed;
864 /* Perform the operation DST = SRC1 & ~SRC2. Return true if any bit
865 in DST has changed. */
867 bool
868 ebitmap_and_compl (ebitmap dst, ebitmap src1, ebitmap src2)
870 unsigned int src1size = src1->wordmask->n_bits;
871 sbitmap_iterator sbi;
872 unsigned int i;
873 sbitmap tempmask;
874 unsigned int neweltindex = 0;
875 unsigned int src1eltindex = 0;
876 bool changed = false;
877 EBITMAP_ELT_TYPE *newarray;
878 unsigned int newarraysize;
880 /* XXX: Optimize like the into version. */
881 dst->cache = NULL;
882 tempmask = sbitmap_alloc_with_popcount (src1size);
883 sbitmap_zero (tempmask);
884 sbitmap_copy (tempmask, src1->wordmask);
886 newarraysize = src1->numwords;
887 newarray = XNEWVEC (EBITMAP_ELT_TYPE, newarraysize);
889 EXECUTE_IF_SET_IN_SBITMAP (src1->wordmask, 0, i, sbi)
891 bool src2hasword;
892 EBITMAP_ELT_TYPE tmpword;
894 src2hasword = (i < src2->wordmask->n_bits
895 && TEST_BIT (src2->wordmask, i));
897 if (src2hasword)
899 unsigned int src2count = sbitmap_popcount (src2->wordmask, i);
900 tmpword = ebitmap_array_get (src1, src1eltindex++)
901 & ~(ebitmap_array_get (src2, src2count));
902 if (tmpword)
904 newarray[neweltindex++] = tmpword;
906 else
907 RESET_BIT (tempmask, i);
910 else
912 tmpword = ebitmap_array_get (src1, src1eltindex++);
913 gcc_assert (tmpword != 0);
914 newarray[neweltindex++] = tmpword;
917 if (i >= dst->wordmask->n_bits || !TEST_BIT (dst->wordmask, i))
919 changed = true;
921 else if (!changed)
923 unsigned int count = sbitmap_popcount (dst->wordmask, i);
924 changed |= ebitmap_array_get (dst, count) != tmpword;
927 if (changed)
929 sbitmap_free (dst->wordmask);
930 dst->wordmask = tempmask;
931 dst->numwords = neweltindex;
932 free (dst->elts);
933 dst->elts = newarray;
934 dst->n_elts = newarraysize;
936 else
938 free (tempmask);
939 free (newarray);
941 #ifdef EBITMAP_DEBUGGING
943 unsigned int i;
944 ebitmap_iterator ebi;
946 EXECUTE_IF_SET_IN_EBITMAP (src1, 0, i, ebi)
948 if (!ebitmap_bit_p (src2, i))
949 gcc_assert (ebitmap_bit_p (dst, i));
951 for (i = 0; i < dst->numwords; i++)
952 gcc_assert (dst->elts[i] != 0);
954 sbitmap_verify_popcount (dst->wordmask);
955 gcc_assert (sbitmap_popcount (dst->wordmask,
956 dst->wordmask->n_bits) == dst->numwords);
958 #endif
959 return changed;
962 /* Perform the operation DST = A | (B & ~C). */
964 bool
965 ebitmap_ior_and_compl (ebitmap dst, ebitmap a, ebitmap b, ebitmap c)
967 bool changed;
968 ebitmap temp = ebitmap_alloc (1);
969 #ifdef EBITMAP_DEBUGGING
970 ebitmap dstcopy = ebitmap_alloc (1);
971 ebitmap_copy (dstcopy, dst);
972 #endif
974 dst->cache = NULL;
975 ebitmap_and_compl (temp, b, c);
976 changed = ebitmap_ior (dst, a, temp);
977 #ifdef EBITMAP_DEBUGGING
979 ebitmap_iterator ebi;
980 unsigned int i;
982 EXECUTE_IF_SET_IN_EBITMAP (a, 0, i, ebi)
983 gcc_assert (ebitmap_bit_p (dst, i));
985 EXECUTE_IF_SET_IN_EBITMAP (b, 0, i, ebi)
986 if (!ebitmap_bit_p (c, i))
987 gcc_assert (ebitmap_bit_p (dst, i));
988 gcc_assert (changed == !ebitmap_equal_p (dst, dstcopy));
990 #endif
991 ebitmap_free (temp);
993 return changed;
996 /* Return true if ebitmap DST is equal to ebitmap SRC. */
998 bool
999 ebitmap_equal_p (ebitmap dst, ebitmap src)
1001 unsigned int which = MIN (dst->wordmask->size, src->wordmask->size);
1003 if (dst->numwords != src->numwords)
1004 return false;
1006 /* sbitmap_equal compares up to the size of the first argument, so
1007 if the two sbitmaps are not equally sized, we need to pass the
1008 smaller one as the first argument, or it will crash. */
1009 if (which == dst->wordmask->size
1010 && !sbitmap_equal (dst->wordmask, src->wordmask))
1011 return false;
1012 else if (which == src->wordmask->size
1013 && !sbitmap_equal (src->wordmask, dst->wordmask))
1014 return false;
1016 return memcmp (dst->elts, src->elts,
1017 dst->numwords * sizeof (EBITMAP_ELT_TYPE)) == 0;
1018 return true;