Remove snprintf from <avx512>_(load|store)<mode>_mask
[official-gcc.git] / gcc / bitmap.c
blob0a94e6488079fbf66c5407c0aabc59d9a51efc92
1 /* Functions to support general ended bitmaps.
2 Copyright (C) 1997-2016 Free Software Foundation, Inc.
4 This file is part of GCC.
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.
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.
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/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "bitmap.h"
25 /* Memory allocation statistics purpose instance. */
26 mem_alloc_description<bitmap_usage> bitmap_mem_desc;
28 /* Register new bitmap. */
29 void
30 bitmap_register (bitmap b MEM_STAT_DECL)
32 bitmap_mem_desc.register_descriptor (b, BITMAP_ORIGIN, false
33 FINAL_PASS_MEM_STAT);
36 /* Account the overhead. */
37 static void
38 register_overhead (bitmap b, int amount)
40 if (bitmap_mem_desc.contains_descriptor_for_instance (b))
41 bitmap_mem_desc.register_instance_overhead (amount, b);
44 /* Global data */
45 bitmap_element bitmap_zero_bits; /* An element of all zero bits. */
46 bitmap_obstack bitmap_default_obstack; /* The default bitmap obstack. */
47 static int bitmap_default_obstack_depth;
48 static GTY((deletable)) bitmap_element *bitmap_ggc_free; /* Freelist of
49 GC'd elements. */
51 static void bitmap_elem_to_freelist (bitmap, bitmap_element *);
52 static void bitmap_element_free (bitmap, bitmap_element *);
53 static bitmap_element *bitmap_element_allocate (bitmap);
54 static int bitmap_element_zerop (const bitmap_element *);
55 static void bitmap_element_link (bitmap, bitmap_element *);
56 static bitmap_element *bitmap_elt_insert_after (bitmap, bitmap_element *, unsigned int);
57 static void bitmap_elt_clear_from (bitmap, bitmap_element *);
58 static bitmap_element *bitmap_find_bit (bitmap, unsigned int);
61 /* Add ELEM to the appropriate freelist. */
62 static inline void
63 bitmap_elem_to_freelist (bitmap head, bitmap_element *elt)
65 bitmap_obstack *bit_obstack = head->obstack;
67 elt->next = NULL;
68 if (bit_obstack)
70 elt->prev = bit_obstack->elements;
71 bit_obstack->elements = elt;
73 else
75 elt->prev = bitmap_ggc_free;
76 bitmap_ggc_free = elt;
80 /* Free a bitmap element. Since these are allocated off the
81 bitmap_obstack, "free" actually means "put onto the freelist". */
83 static inline void
84 bitmap_element_free (bitmap head, bitmap_element *elt)
86 bitmap_element *next = elt->next;
87 bitmap_element *prev = elt->prev;
89 if (prev)
90 prev->next = next;
92 if (next)
93 next->prev = prev;
95 if (head->first == elt)
96 head->first = next;
98 /* Since the first thing we try is to insert before current,
99 make current the next entry in preference to the previous. */
100 if (head->current == elt)
102 head->current = next != 0 ? next : prev;
103 if (head->current)
104 head->indx = head->current->indx;
105 else
106 head->indx = 0;
109 if (GATHER_STATISTICS)
110 register_overhead (head, -((int)sizeof (bitmap_element)));
112 bitmap_elem_to_freelist (head, elt);
115 /* Allocate a bitmap element. The bits are cleared, but nothing else is. */
117 static inline bitmap_element *
118 bitmap_element_allocate (bitmap head)
120 bitmap_element *element;
121 bitmap_obstack *bit_obstack = head->obstack;
123 if (bit_obstack)
125 element = bit_obstack->elements;
127 if (element)
128 /* Use up the inner list first before looking at the next
129 element of the outer list. */
130 if (element->next)
132 bit_obstack->elements = element->next;
133 bit_obstack->elements->prev = element->prev;
135 else
136 /* Inner list was just a singleton. */
137 bit_obstack->elements = element->prev;
138 else
139 element = XOBNEW (&bit_obstack->obstack, bitmap_element);
141 else
143 element = bitmap_ggc_free;
144 if (element)
145 /* Use up the inner list first before looking at the next
146 element of the outer list. */
147 if (element->next)
149 bitmap_ggc_free = element->next;
150 bitmap_ggc_free->prev = element->prev;
152 else
153 /* Inner list was just a singleton. */
154 bitmap_ggc_free = element->prev;
155 else
156 element = ggc_alloc<bitmap_element> ();
159 if (GATHER_STATISTICS)
160 register_overhead (head, sizeof (bitmap_element));
162 memset (element->bits, 0, sizeof (element->bits));
164 return element;
167 /* Remove ELT and all following elements from bitmap HEAD. */
169 void
170 bitmap_elt_clear_from (bitmap head, bitmap_element *elt)
172 bitmap_element *prev;
173 bitmap_obstack *bit_obstack = head->obstack;
175 if (!elt) return;
177 if (GATHER_STATISTICS)
179 int n = 0;
180 for (prev = elt; prev; prev = prev->next)
181 n++;
182 register_overhead (head, -sizeof (bitmap_element) * n);
185 prev = elt->prev;
186 if (prev)
188 prev->next = NULL;
189 if (head->current->indx > prev->indx)
191 head->current = prev;
192 head->indx = prev->indx;
195 else
197 head->first = NULL;
198 head->current = NULL;
199 head->indx = 0;
202 /* Put the entire list onto the free list in one operation. */
203 if (bit_obstack)
205 elt->prev = bit_obstack->elements;
206 bit_obstack->elements = elt;
208 else
210 elt->prev = bitmap_ggc_free;
211 bitmap_ggc_free = elt;
215 /* Clear a bitmap by freeing the linked list. */
217 void
218 bitmap_clear (bitmap head)
220 if (head->first)
221 bitmap_elt_clear_from (head, head->first);
224 /* Initialize a bitmap obstack. If BIT_OBSTACK is NULL, initialize
225 the default bitmap obstack. */
227 void
228 bitmap_obstack_initialize (bitmap_obstack *bit_obstack)
230 if (!bit_obstack)
232 if (bitmap_default_obstack_depth++)
233 return;
234 bit_obstack = &bitmap_default_obstack;
237 #if !defined(__GNUC__) || (__GNUC__ < 2)
238 #define __alignof__(type) 0
239 #endif
241 bit_obstack->elements = NULL;
242 bit_obstack->heads = NULL;
243 obstack_specify_allocation (&bit_obstack->obstack, OBSTACK_CHUNK_SIZE,
244 __alignof__ (bitmap_element),
245 obstack_chunk_alloc,
246 obstack_chunk_free);
249 /* Release the memory from a bitmap obstack. If BIT_OBSTACK is NULL,
250 release the default bitmap obstack. */
252 void
253 bitmap_obstack_release (bitmap_obstack *bit_obstack)
255 if (!bit_obstack)
257 if (--bitmap_default_obstack_depth)
259 gcc_assert (bitmap_default_obstack_depth > 0);
260 return;
262 bit_obstack = &bitmap_default_obstack;
265 bit_obstack->elements = NULL;
266 bit_obstack->heads = NULL;
267 obstack_free (&bit_obstack->obstack, NULL);
270 /* Create a new bitmap on an obstack. If BIT_OBSTACK is NULL, create
271 it on the default bitmap obstack. */
273 bitmap
274 bitmap_obstack_alloc_stat (bitmap_obstack *bit_obstack MEM_STAT_DECL)
276 bitmap map;
278 if (!bit_obstack)
279 bit_obstack = &bitmap_default_obstack;
280 map = bit_obstack->heads;
281 if (map)
282 bit_obstack->heads = (struct bitmap_head *) map->first;
283 else
284 map = XOBNEW (&bit_obstack->obstack, bitmap_head);
285 bitmap_initialize_stat (map, bit_obstack PASS_MEM_STAT);
287 if (GATHER_STATISTICS)
288 register_overhead (map, sizeof (bitmap_head));
290 return map;
293 /* Create a new GCd bitmap. */
295 bitmap
296 bitmap_gc_alloc_stat (ALONE_MEM_STAT_DECL)
298 bitmap map;
300 map = ggc_alloc<bitmap_head> ();
301 bitmap_initialize_stat (map, NULL PASS_MEM_STAT);
303 if (GATHER_STATISTICS)
304 register_overhead (map, sizeof (bitmap_head));
306 return map;
309 /* Release an obstack allocated bitmap. */
311 void
312 bitmap_obstack_free (bitmap map)
314 if (map)
316 bitmap_clear (map);
317 map->first = (bitmap_element *) map->obstack->heads;
319 if (GATHER_STATISTICS)
320 register_overhead (map, -((int)sizeof (bitmap_head)));
322 map->obstack->heads = map;
327 /* Return nonzero if all bits in an element are zero. */
329 static inline int
330 bitmap_element_zerop (const bitmap_element *element)
332 #if BITMAP_ELEMENT_WORDS == 2
333 return (element->bits[0] | element->bits[1]) == 0;
334 #else
335 unsigned i;
337 for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
338 if (element->bits[i] != 0)
339 return 0;
341 return 1;
342 #endif
345 /* Link the bitmap element into the current bitmap linked list. */
347 static inline void
348 bitmap_element_link (bitmap head, bitmap_element *element)
350 unsigned int indx = element->indx;
351 bitmap_element *ptr;
353 /* If this is the first and only element, set it in. */
354 if (head->first == 0)
356 element->next = element->prev = 0;
357 head->first = element;
360 /* If this index is less than that of the current element, it goes someplace
361 before the current element. */
362 else if (indx < head->indx)
364 for (ptr = head->current;
365 ptr->prev != 0 && ptr->prev->indx > indx;
366 ptr = ptr->prev)
369 if (ptr->prev)
370 ptr->prev->next = element;
371 else
372 head->first = element;
374 element->prev = ptr->prev;
375 element->next = ptr;
376 ptr->prev = element;
379 /* Otherwise, it must go someplace after the current element. */
380 else
382 for (ptr = head->current;
383 ptr->next != 0 && ptr->next->indx < indx;
384 ptr = ptr->next)
387 if (ptr->next)
388 ptr->next->prev = element;
390 element->next = ptr->next;
391 element->prev = ptr;
392 ptr->next = element;
395 /* Set up so this is the first element searched. */
396 head->current = element;
397 head->indx = indx;
400 /* Insert a new uninitialized element into bitmap HEAD after element
401 ELT. If ELT is NULL, insert the element at the start. Return the
402 new element. */
404 static bitmap_element *
405 bitmap_elt_insert_after (bitmap head, bitmap_element *elt, unsigned int indx)
407 bitmap_element *node = bitmap_element_allocate (head);
408 node->indx = indx;
410 if (!elt)
412 if (!head->current)
414 head->current = node;
415 head->indx = indx;
417 node->next = head->first;
418 if (node->next)
419 node->next->prev = node;
420 head->first = node;
421 node->prev = NULL;
423 else
425 gcc_checking_assert (head->current);
426 node->next = elt->next;
427 if (node->next)
428 node->next->prev = node;
429 elt->next = node;
430 node->prev = elt;
432 return node;
435 /* Copy a bitmap to another bitmap. */
437 void
438 bitmap_copy (bitmap to, const_bitmap from)
440 const bitmap_element *from_ptr;
441 bitmap_element *to_ptr = 0;
443 bitmap_clear (to);
445 /* Copy elements in forward direction one at a time. */
446 for (from_ptr = from->first; from_ptr; from_ptr = from_ptr->next)
448 bitmap_element *to_elt = bitmap_element_allocate (to);
450 to_elt->indx = from_ptr->indx;
451 memcpy (to_elt->bits, from_ptr->bits, sizeof (to_elt->bits));
453 /* Here we have a special case of bitmap_element_link, for the case
454 where we know the links are being entered in sequence. */
455 if (to_ptr == 0)
457 to->first = to->current = to_elt;
458 to->indx = from_ptr->indx;
459 to_elt->next = to_elt->prev = 0;
461 else
463 to_elt->prev = to_ptr;
464 to_elt->next = 0;
465 to_ptr->next = to_elt;
468 to_ptr = to_elt;
472 /* Find a bitmap element that would hold a bitmap's bit.
473 Update the `current' field even if we can't find an element that
474 would hold the bitmap's bit to make eventual allocation
475 faster. */
477 static inline bitmap_element *
478 bitmap_find_bit (bitmap head, unsigned int bit)
480 bitmap_element *element;
481 unsigned int indx = bit / BITMAP_ELEMENT_ALL_BITS;
483 if (head->current == NULL
484 || head->indx == indx)
485 return head->current;
486 if (head->current == head->first
487 && head->first->next == NULL)
488 return NULL;
490 /* Usage can be NULL due to allocated bitmaps for which we do not
491 call initialize function. */
492 bitmap_usage *usage = NULL;
493 if (GATHER_STATISTICS)
494 usage = bitmap_mem_desc.get_descriptor_for_instance (head);
496 /* This bitmap has more than one element, and we're going to look
497 through the elements list. Count that as a search. */
498 if (GATHER_STATISTICS && usage)
499 usage->m_nsearches++;
501 if (head->indx < indx)
502 /* INDX is beyond head->indx. Search from head->current
503 forward. */
504 for (element = head->current;
505 element->next != 0 && element->indx < indx;
506 element = element->next)
508 if (GATHER_STATISTICS && usage)
509 usage->m_search_iter++;
512 else if (head->indx / 2 < indx)
513 /* INDX is less than head->indx and closer to head->indx than to
514 0. Search from head->current backward. */
515 for (element = head->current;
516 element->prev != 0 && element->indx > indx;
517 element = element->prev)
519 if (GATHER_STATISTICS && usage)
520 usage->m_search_iter++;
523 else
524 /* INDX is less than head->indx and closer to 0 than to
525 head->indx. Search from head->first forward. */
526 for (element = head->first;
527 element->next != 0 && element->indx < indx;
528 element = element->next)
529 if (GATHER_STATISTICS && usage)
531 usage->m_search_iter++;
534 /* `element' is the nearest to the one we want. If it's not the one we
535 want, the one we want doesn't exist. */
536 head->current = element;
537 head->indx = element->indx;
538 if (element != 0 && element->indx != indx)
539 element = 0;
541 return element;
544 /* Clear a single bit in a bitmap. Return true if the bit changed. */
546 bool
547 bitmap_clear_bit (bitmap head, int bit)
549 bitmap_element *const ptr = bitmap_find_bit (head, bit);
551 if (ptr != 0)
553 unsigned bit_num = bit % BITMAP_WORD_BITS;
554 unsigned word_num = bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
555 BITMAP_WORD bit_val = ((BITMAP_WORD) 1) << bit_num;
556 bool res = (ptr->bits[word_num] & bit_val) != 0;
557 if (res)
559 ptr->bits[word_num] &= ~bit_val;
560 /* If we cleared the entire word, free up the element. */
561 if (!ptr->bits[word_num]
562 && bitmap_element_zerop (ptr))
563 bitmap_element_free (head, ptr);
566 return res;
569 return false;
572 /* Set a single bit in a bitmap. Return true if the bit changed. */
574 bool
575 bitmap_set_bit (bitmap head, int bit)
577 bitmap_element *ptr = bitmap_find_bit (head, bit);
578 unsigned word_num = bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
579 unsigned bit_num = bit % BITMAP_WORD_BITS;
580 BITMAP_WORD bit_val = ((BITMAP_WORD) 1) << bit_num;
582 if (ptr == 0)
584 ptr = bitmap_element_allocate (head);
585 ptr->indx = bit / BITMAP_ELEMENT_ALL_BITS;
586 ptr->bits[word_num] = bit_val;
587 bitmap_element_link (head, ptr);
588 return true;
590 else
592 bool res = (ptr->bits[word_num] & bit_val) == 0;
593 if (res)
594 ptr->bits[word_num] |= bit_val;
595 return res;
599 /* Return whether a bit is set within a bitmap. */
602 bitmap_bit_p (bitmap head, int bit)
604 bitmap_element *ptr;
605 unsigned bit_num;
606 unsigned word_num;
608 ptr = bitmap_find_bit (head, bit);
609 if (ptr == 0)
610 return 0;
612 bit_num = bit % BITMAP_WORD_BITS;
613 word_num = bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
615 return (ptr->bits[word_num] >> bit_num) & 1;
618 #if GCC_VERSION < 3400
619 /* Table of number of set bits in a character, indexed by value of char. */
620 static const unsigned char popcount_table[] =
622 0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
623 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
624 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
625 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
626 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
627 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
628 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
629 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8,
632 static unsigned long
633 bitmap_popcount (BITMAP_WORD a)
635 unsigned long ret = 0;
636 unsigned i;
638 /* Just do this the table way for now */
639 for (i = 0; i < BITMAP_WORD_BITS; i+= 8)
640 ret += popcount_table[(a >> i) & 0xff];
641 return ret;
643 #endif
644 /* Count the number of bits set in the bitmap, and return it. */
646 unsigned long
647 bitmap_count_bits (const_bitmap a)
649 unsigned long count = 0;
650 const bitmap_element *elt;
651 unsigned ix;
653 for (elt = a->first; elt; elt = elt->next)
655 for (ix = 0; ix != BITMAP_ELEMENT_WORDS; ix++)
657 #if GCC_VERSION >= 3400
658 /* Note that popcountl matches BITMAP_WORD in type, so the actual size
659 of BITMAP_WORD is not material. */
660 count += __builtin_popcountl (elt->bits[ix]);
661 #else
662 count += bitmap_popcount (elt->bits[ix]);
663 #endif
666 return count;
669 /* Return true if the bitmap has a single bit set. Otherwise return
670 false. */
672 bool
673 bitmap_single_bit_set_p (const_bitmap a)
675 unsigned long count = 0;
676 const bitmap_element *elt;
677 unsigned ix;
679 if (bitmap_empty_p (a))
680 return false;
682 elt = a->first;
683 /* As there are no completely empty bitmap elements, a second one
684 means we have more than one bit set. */
685 if (elt->next != NULL)
686 return false;
688 for (ix = 0; ix != BITMAP_ELEMENT_WORDS; ix++)
690 #if GCC_VERSION >= 3400
691 /* Note that popcountl matches BITMAP_WORD in type, so the actual size
692 of BITMAP_WORD is not material. */
693 count += __builtin_popcountl (elt->bits[ix]);
694 #else
695 count += bitmap_popcount (elt->bits[ix]);
696 #endif
697 if (count > 1)
698 return false;
701 return count == 1;
705 /* Return the bit number of the first set bit in the bitmap. The
706 bitmap must be non-empty. */
708 unsigned
709 bitmap_first_set_bit (const_bitmap a)
711 const bitmap_element *elt = a->first;
712 unsigned bit_no;
713 BITMAP_WORD word;
714 unsigned ix;
716 gcc_checking_assert (elt);
717 bit_no = elt->indx * BITMAP_ELEMENT_ALL_BITS;
718 for (ix = 0; ix != BITMAP_ELEMENT_WORDS; ix++)
720 word = elt->bits[ix];
721 if (word)
722 goto found_bit;
724 gcc_unreachable ();
725 found_bit:
726 bit_no += ix * BITMAP_WORD_BITS;
728 #if GCC_VERSION >= 3004
729 gcc_assert (sizeof (long) == sizeof (word));
730 bit_no += __builtin_ctzl (word);
731 #else
732 /* Binary search for the first set bit. */
733 #if BITMAP_WORD_BITS > 64
734 #error "Fill out the table."
735 #endif
736 #if BITMAP_WORD_BITS > 32
737 if (!(word & 0xffffffff))
738 word >>= 32, bit_no += 32;
739 #endif
740 if (!(word & 0xffff))
741 word >>= 16, bit_no += 16;
742 if (!(word & 0xff))
743 word >>= 8, bit_no += 8;
744 if (!(word & 0xf))
745 word >>= 4, bit_no += 4;
746 if (!(word & 0x3))
747 word >>= 2, bit_no += 2;
748 if (!(word & 0x1))
749 word >>= 1, bit_no += 1;
751 gcc_checking_assert (word & 1);
752 #endif
753 return bit_no;
756 /* Return the bit number of the first set bit in the bitmap. The
757 bitmap must be non-empty. */
759 unsigned
760 bitmap_last_set_bit (const_bitmap a)
762 const bitmap_element *elt = a->current ? a->current : a->first;
763 unsigned bit_no;
764 BITMAP_WORD word;
765 int ix;
767 gcc_checking_assert (elt);
768 while (elt->next)
769 elt = elt->next;
770 bit_no = elt->indx * BITMAP_ELEMENT_ALL_BITS;
771 for (ix = BITMAP_ELEMENT_WORDS - 1; ix >= 0; ix--)
773 word = elt->bits[ix];
774 if (word)
775 goto found_bit;
777 gcc_unreachable ();
778 found_bit:
779 bit_no += ix * BITMAP_WORD_BITS;
780 #if GCC_VERSION >= 3004
781 gcc_assert (sizeof (long) == sizeof (word));
782 bit_no += BITMAP_WORD_BITS - __builtin_clzl (word) - 1;
783 #else
784 /* Hopefully this is a twos-complement host... */
785 BITMAP_WORD x = word;
786 x |= (x >> 1);
787 x |= (x >> 2);
788 x |= (x >> 4);
789 x |= (x >> 8);
790 x |= (x >> 16);
791 #if BITMAP_WORD_BITS > 32
792 x |= (x >> 32);
793 #endif
794 bit_no += bitmap_popcount (x) - 1;
795 #endif
797 return bit_no;
801 /* DST = A & B. */
803 void
804 bitmap_and (bitmap dst, const_bitmap a, const_bitmap b)
806 bitmap_element *dst_elt = dst->first;
807 const bitmap_element *a_elt = a->first;
808 const bitmap_element *b_elt = b->first;
809 bitmap_element *dst_prev = NULL;
811 gcc_assert (dst != a && dst != b);
813 if (a == b)
815 bitmap_copy (dst, a);
816 return;
819 while (a_elt && b_elt)
821 if (a_elt->indx < b_elt->indx)
822 a_elt = a_elt->next;
823 else if (b_elt->indx < a_elt->indx)
824 b_elt = b_elt->next;
825 else
827 /* Matching elts, generate A & B. */
828 unsigned ix;
829 BITMAP_WORD ior = 0;
831 if (!dst_elt)
832 dst_elt = bitmap_elt_insert_after (dst, dst_prev, a_elt->indx);
833 else
834 dst_elt->indx = a_elt->indx;
835 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
837 BITMAP_WORD r = a_elt->bits[ix] & b_elt->bits[ix];
839 dst_elt->bits[ix] = r;
840 ior |= r;
842 if (ior)
844 dst_prev = dst_elt;
845 dst_elt = dst_elt->next;
847 a_elt = a_elt->next;
848 b_elt = b_elt->next;
851 /* Ensure that dst->current is valid. */
852 dst->current = dst->first;
853 bitmap_elt_clear_from (dst, dst_elt);
854 gcc_checking_assert (!dst->current == !dst->first);
855 if (dst->current)
856 dst->indx = dst->current->indx;
859 /* A &= B. Return true if A changed. */
861 bool
862 bitmap_and_into (bitmap a, const_bitmap b)
864 bitmap_element *a_elt = a->first;
865 const bitmap_element *b_elt = b->first;
866 bitmap_element *next;
867 bool changed = false;
869 if (a == b)
870 return false;
872 while (a_elt && b_elt)
874 if (a_elt->indx < b_elt->indx)
876 next = a_elt->next;
877 bitmap_element_free (a, a_elt);
878 a_elt = next;
879 changed = true;
881 else if (b_elt->indx < a_elt->indx)
882 b_elt = b_elt->next;
883 else
885 /* Matching elts, generate A &= B. */
886 unsigned ix;
887 BITMAP_WORD ior = 0;
889 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
891 BITMAP_WORD r = a_elt->bits[ix] & b_elt->bits[ix];
892 if (a_elt->bits[ix] != r)
893 changed = true;
894 a_elt->bits[ix] = r;
895 ior |= r;
897 next = a_elt->next;
898 if (!ior)
899 bitmap_element_free (a, a_elt);
900 a_elt = next;
901 b_elt = b_elt->next;
905 if (a_elt)
907 changed = true;
908 bitmap_elt_clear_from (a, a_elt);
911 gcc_checking_assert (!a->current == !a->first
912 && (!a->current || a->indx == a->current->indx));
914 return changed;
918 /* Insert an element equal to SRC_ELT after DST_PREV, overwriting DST_ELT
919 if non-NULL. CHANGED is true if the destination bitmap had already been
920 changed; the new value of CHANGED is returned. */
922 static inline bool
923 bitmap_elt_copy (bitmap dst, bitmap_element *dst_elt, bitmap_element *dst_prev,
924 const bitmap_element *src_elt, bool changed)
926 if (!changed && dst_elt && dst_elt->indx == src_elt->indx)
928 unsigned ix;
930 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
931 if (src_elt->bits[ix] != dst_elt->bits[ix])
933 dst_elt->bits[ix] = src_elt->bits[ix];
934 changed = true;
937 else
939 changed = true;
940 if (!dst_elt)
941 dst_elt = bitmap_elt_insert_after (dst, dst_prev, src_elt->indx);
942 else
943 dst_elt->indx = src_elt->indx;
944 memcpy (dst_elt->bits, src_elt->bits, sizeof (dst_elt->bits));
946 return changed;
951 /* DST = A & ~B */
953 bool
954 bitmap_and_compl (bitmap dst, const_bitmap a, const_bitmap b)
956 bitmap_element *dst_elt = dst->first;
957 const bitmap_element *a_elt = a->first;
958 const bitmap_element *b_elt = b->first;
959 bitmap_element *dst_prev = NULL;
960 bitmap_element **dst_prev_pnext = &dst->first;
961 bool changed = false;
963 gcc_assert (dst != a && dst != b);
965 if (a == b)
967 changed = !bitmap_empty_p (dst);
968 bitmap_clear (dst);
969 return changed;
972 while (a_elt)
974 while (b_elt && b_elt->indx < a_elt->indx)
975 b_elt = b_elt->next;
977 if (!b_elt || b_elt->indx > a_elt->indx)
979 changed = bitmap_elt_copy (dst, dst_elt, dst_prev, a_elt, changed);
980 dst_prev = *dst_prev_pnext;
981 dst_prev_pnext = &dst_prev->next;
982 dst_elt = *dst_prev_pnext;
983 a_elt = a_elt->next;
986 else
988 /* Matching elts, generate A & ~B. */
989 unsigned ix;
990 BITMAP_WORD ior = 0;
992 if (!changed && dst_elt && dst_elt->indx == a_elt->indx)
994 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
996 BITMAP_WORD r = a_elt->bits[ix] & ~b_elt->bits[ix];
998 if (dst_elt->bits[ix] != r)
1000 changed = true;
1001 dst_elt->bits[ix] = r;
1003 ior |= r;
1006 else
1008 bool new_element;
1009 if (!dst_elt || dst_elt->indx > a_elt->indx)
1011 dst_elt = bitmap_elt_insert_after (dst, dst_prev, a_elt->indx);
1012 new_element = true;
1014 else
1016 dst_elt->indx = a_elt->indx;
1017 new_element = false;
1020 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1022 BITMAP_WORD r = a_elt->bits[ix] & ~b_elt->bits[ix];
1024 dst_elt->bits[ix] = r;
1025 ior |= r;
1028 if (ior)
1029 changed = true;
1030 else
1032 changed |= !new_element;
1033 bitmap_element_free (dst, dst_elt);
1034 dst_elt = *dst_prev_pnext;
1038 if (ior)
1040 dst_prev = *dst_prev_pnext;
1041 dst_prev_pnext = &dst_prev->next;
1042 dst_elt = *dst_prev_pnext;
1044 a_elt = a_elt->next;
1045 b_elt = b_elt->next;
1049 /* Ensure that dst->current is valid. */
1050 dst->current = dst->first;
1052 if (dst_elt)
1054 changed = true;
1055 bitmap_elt_clear_from (dst, dst_elt);
1057 gcc_checking_assert (!dst->current == !dst->first);
1058 if (dst->current)
1059 dst->indx = dst->current->indx;
1061 return changed;
1064 /* A &= ~B. Returns true if A changes */
1066 bool
1067 bitmap_and_compl_into (bitmap a, const_bitmap b)
1069 bitmap_element *a_elt = a->first;
1070 const bitmap_element *b_elt = b->first;
1071 bitmap_element *next;
1072 BITMAP_WORD changed = 0;
1074 if (a == b)
1076 if (bitmap_empty_p (a))
1077 return false;
1078 else
1080 bitmap_clear (a);
1081 return true;
1085 while (a_elt && b_elt)
1087 if (a_elt->indx < b_elt->indx)
1088 a_elt = a_elt->next;
1089 else if (b_elt->indx < a_elt->indx)
1090 b_elt = b_elt->next;
1091 else
1093 /* Matching elts, generate A &= ~B. */
1094 unsigned ix;
1095 BITMAP_WORD ior = 0;
1097 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1099 BITMAP_WORD cleared = a_elt->bits[ix] & b_elt->bits[ix];
1100 BITMAP_WORD r = a_elt->bits[ix] ^ cleared;
1102 a_elt->bits[ix] = r;
1103 changed |= cleared;
1104 ior |= r;
1106 next = a_elt->next;
1107 if (!ior)
1108 bitmap_element_free (a, a_elt);
1109 a_elt = next;
1110 b_elt = b_elt->next;
1113 gcc_checking_assert (!a->current == !a->first
1114 && (!a->current || a->indx == a->current->indx));
1115 return changed != 0;
1118 /* Set COUNT bits from START in HEAD. */
1119 void
1120 bitmap_set_range (bitmap head, unsigned int start, unsigned int count)
1122 unsigned int first_index, end_bit_plus1, last_index;
1123 bitmap_element *elt, *elt_prev;
1124 unsigned int i;
1126 if (!count)
1127 return;
1129 if (count == 1)
1131 bitmap_set_bit (head, start);
1132 return;
1135 first_index = start / BITMAP_ELEMENT_ALL_BITS;
1136 end_bit_plus1 = start + count;
1137 last_index = (end_bit_plus1 - 1) / BITMAP_ELEMENT_ALL_BITS;
1138 elt = bitmap_find_bit (head, start);
1140 /* If bitmap_find_bit returns zero, the current is the closest block
1141 to the result. Otherwise, just use bitmap_element_allocate to
1142 ensure ELT is set; in the loop below, ELT == NULL means "insert
1143 at the end of the bitmap". */
1144 if (!elt)
1146 elt = bitmap_element_allocate (head);
1147 elt->indx = first_index;
1148 bitmap_element_link (head, elt);
1151 gcc_checking_assert (elt->indx == first_index);
1152 elt_prev = elt->prev;
1153 for (i = first_index; i <= last_index; i++)
1155 unsigned elt_start_bit = i * BITMAP_ELEMENT_ALL_BITS;
1156 unsigned elt_end_bit_plus1 = elt_start_bit + BITMAP_ELEMENT_ALL_BITS;
1158 unsigned int first_word_to_mod;
1159 BITMAP_WORD first_mask;
1160 unsigned int last_word_to_mod;
1161 BITMAP_WORD last_mask;
1162 unsigned int ix;
1164 if (!elt || elt->indx != i)
1165 elt = bitmap_elt_insert_after (head, elt_prev, i);
1167 if (elt_start_bit <= start)
1169 /* The first bit to turn on is somewhere inside this
1170 elt. */
1171 first_word_to_mod = (start - elt_start_bit) / BITMAP_WORD_BITS;
1173 /* This mask should have 1s in all bits >= start position. */
1174 first_mask =
1175 (((BITMAP_WORD) 1) << ((start % BITMAP_WORD_BITS))) - 1;
1176 first_mask = ~first_mask;
1178 else
1180 /* The first bit to turn on is below this start of this elt. */
1181 first_word_to_mod = 0;
1182 first_mask = ~(BITMAP_WORD) 0;
1185 if (elt_end_bit_plus1 <= end_bit_plus1)
1187 /* The last bit to turn on is beyond this elt. */
1188 last_word_to_mod = BITMAP_ELEMENT_WORDS - 1;
1189 last_mask = ~(BITMAP_WORD) 0;
1191 else
1193 /* The last bit to turn on is inside to this elt. */
1194 last_word_to_mod =
1195 (end_bit_plus1 - elt_start_bit) / BITMAP_WORD_BITS;
1197 /* The last mask should have 1s below the end bit. */
1198 last_mask =
1199 (((BITMAP_WORD) 1) << ((end_bit_plus1 % BITMAP_WORD_BITS))) - 1;
1202 if (first_word_to_mod == last_word_to_mod)
1204 BITMAP_WORD mask = first_mask & last_mask;
1205 elt->bits[first_word_to_mod] |= mask;
1207 else
1209 elt->bits[first_word_to_mod] |= first_mask;
1210 if (BITMAP_ELEMENT_WORDS > 2)
1211 for (ix = first_word_to_mod + 1; ix < last_word_to_mod; ix++)
1212 elt->bits[ix] = ~(BITMAP_WORD) 0;
1213 elt->bits[last_word_to_mod] |= last_mask;
1216 elt_prev = elt;
1217 elt = elt->next;
1220 head->current = elt ? elt : elt_prev;
1221 head->indx = head->current->indx;
1224 /* Clear COUNT bits from START in HEAD. */
1225 void
1226 bitmap_clear_range (bitmap head, unsigned int start, unsigned int count)
1228 unsigned int first_index, end_bit_plus1, last_index;
1229 bitmap_element *elt;
1231 if (!count)
1232 return;
1234 if (count == 1)
1236 bitmap_clear_bit (head, start);
1237 return;
1240 first_index = start / BITMAP_ELEMENT_ALL_BITS;
1241 end_bit_plus1 = start + count;
1242 last_index = (end_bit_plus1 - 1) / BITMAP_ELEMENT_ALL_BITS;
1243 elt = bitmap_find_bit (head, start);
1245 /* If bitmap_find_bit returns zero, the current is the closest block
1246 to the result. If the current is less than first index, find the
1247 next one. Otherwise, just set elt to be current. */
1248 if (!elt)
1250 if (head->current)
1252 if (head->indx < first_index)
1254 elt = head->current->next;
1255 if (!elt)
1256 return;
1258 else
1259 elt = head->current;
1261 else
1262 return;
1265 while (elt && (elt->indx <= last_index))
1267 bitmap_element * next_elt = elt->next;
1268 unsigned elt_start_bit = (elt->indx) * BITMAP_ELEMENT_ALL_BITS;
1269 unsigned elt_end_bit_plus1 = elt_start_bit + BITMAP_ELEMENT_ALL_BITS;
1272 if (elt_start_bit >= start && elt_end_bit_plus1 <= end_bit_plus1)
1273 /* Get rid of the entire elt and go to the next one. */
1274 bitmap_element_free (head, elt);
1275 else
1277 /* Going to have to knock out some bits in this elt. */
1278 unsigned int first_word_to_mod;
1279 BITMAP_WORD first_mask;
1280 unsigned int last_word_to_mod;
1281 BITMAP_WORD last_mask;
1282 unsigned int i;
1283 bool clear = true;
1285 if (elt_start_bit <= start)
1287 /* The first bit to turn off is somewhere inside this
1288 elt. */
1289 first_word_to_mod = (start - elt_start_bit) / BITMAP_WORD_BITS;
1291 /* This mask should have 1s in all bits >= start position. */
1292 first_mask =
1293 (((BITMAP_WORD) 1) << ((start % BITMAP_WORD_BITS))) - 1;
1294 first_mask = ~first_mask;
1296 else
1298 /* The first bit to turn off is below this start of this elt. */
1299 first_word_to_mod = 0;
1300 first_mask = 0;
1301 first_mask = ~first_mask;
1304 if (elt_end_bit_plus1 <= end_bit_plus1)
1306 /* The last bit to turn off is beyond this elt. */
1307 last_word_to_mod = BITMAP_ELEMENT_WORDS - 1;
1308 last_mask = 0;
1309 last_mask = ~last_mask;
1311 else
1313 /* The last bit to turn off is inside to this elt. */
1314 last_word_to_mod =
1315 (end_bit_plus1 - elt_start_bit) / BITMAP_WORD_BITS;
1317 /* The last mask should have 1s below the end bit. */
1318 last_mask =
1319 (((BITMAP_WORD) 1) << (((end_bit_plus1) % BITMAP_WORD_BITS))) - 1;
1323 if (first_word_to_mod == last_word_to_mod)
1325 BITMAP_WORD mask = first_mask & last_mask;
1326 elt->bits[first_word_to_mod] &= ~mask;
1328 else
1330 elt->bits[first_word_to_mod] &= ~first_mask;
1331 if (BITMAP_ELEMENT_WORDS > 2)
1332 for (i = first_word_to_mod + 1; i < last_word_to_mod; i++)
1333 elt->bits[i] = 0;
1334 elt->bits[last_word_to_mod] &= ~last_mask;
1336 for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
1337 if (elt->bits[i])
1339 clear = false;
1340 break;
1342 /* Check to see if there are any bits left. */
1343 if (clear)
1344 bitmap_element_free (head, elt);
1346 elt = next_elt;
1349 if (elt)
1351 head->current = elt;
1352 head->indx = head->current->indx;
1356 /* A = ~A & B. */
1358 void
1359 bitmap_compl_and_into (bitmap a, const_bitmap b)
1361 bitmap_element *a_elt = a->first;
1362 const bitmap_element *b_elt = b->first;
1363 bitmap_element *a_prev = NULL;
1364 bitmap_element *next;
1366 gcc_assert (a != b);
1368 if (bitmap_empty_p (a))
1370 bitmap_copy (a, b);
1371 return;
1373 if (bitmap_empty_p (b))
1375 bitmap_clear (a);
1376 return;
1379 while (a_elt || b_elt)
1381 if (!b_elt || (a_elt && a_elt->indx < b_elt->indx))
1383 /* A is before B. Remove A */
1384 next = a_elt->next;
1385 a_prev = a_elt->prev;
1386 bitmap_element_free (a, a_elt);
1387 a_elt = next;
1389 else if (!a_elt || b_elt->indx < a_elt->indx)
1391 /* B is before A. Copy B. */
1392 next = bitmap_elt_insert_after (a, a_prev, b_elt->indx);
1393 memcpy (next->bits, b_elt->bits, sizeof (next->bits));
1394 a_prev = next;
1395 b_elt = b_elt->next;
1397 else
1399 /* Matching elts, generate A = ~A & B. */
1400 unsigned ix;
1401 BITMAP_WORD ior = 0;
1403 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1405 BITMAP_WORD cleared = a_elt->bits[ix] & b_elt->bits[ix];
1406 BITMAP_WORD r = b_elt->bits[ix] ^ cleared;
1408 a_elt->bits[ix] = r;
1409 ior |= r;
1411 next = a_elt->next;
1412 if (!ior)
1413 bitmap_element_free (a, a_elt);
1414 else
1415 a_prev = a_elt;
1416 a_elt = next;
1417 b_elt = b_elt->next;
1420 gcc_checking_assert (!a->current == !a->first
1421 && (!a->current || a->indx == a->current->indx));
1422 return;
1426 /* Insert an element corresponding to A_ELT | B_ELT after DST_PREV,
1427 overwriting DST_ELT if non-NULL. CHANGED is true if the destination bitmap
1428 had already been changed; the new value of CHANGED is returned. */
1430 static inline bool
1431 bitmap_elt_ior (bitmap dst, bitmap_element *dst_elt, bitmap_element *dst_prev,
1432 const bitmap_element *a_elt, const bitmap_element *b_elt,
1433 bool changed)
1435 gcc_assert (a_elt || b_elt);
1437 if (a_elt && b_elt && a_elt->indx == b_elt->indx)
1439 /* Matching elts, generate A | B. */
1440 unsigned ix;
1442 if (!changed && dst_elt && dst_elt->indx == a_elt->indx)
1444 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1446 BITMAP_WORD r = a_elt->bits[ix] | b_elt->bits[ix];
1447 if (r != dst_elt->bits[ix])
1449 dst_elt->bits[ix] = r;
1450 changed = true;
1454 else
1456 changed = true;
1457 if (!dst_elt)
1458 dst_elt = bitmap_elt_insert_after (dst, dst_prev, a_elt->indx);
1459 else
1460 dst_elt->indx = a_elt->indx;
1461 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1463 BITMAP_WORD r = a_elt->bits[ix] | b_elt->bits[ix];
1464 dst_elt->bits[ix] = r;
1468 else
1470 /* Copy a single element. */
1471 const bitmap_element *src;
1473 if (!b_elt || (a_elt && a_elt->indx < b_elt->indx))
1474 src = a_elt;
1475 else
1476 src = b_elt;
1478 gcc_checking_assert (src);
1479 changed = bitmap_elt_copy (dst, dst_elt, dst_prev, src, changed);
1481 return changed;
1485 /* DST = A | B. Return true if DST changes. */
1487 bool
1488 bitmap_ior (bitmap dst, const_bitmap a, const_bitmap b)
1490 bitmap_element *dst_elt = dst->first;
1491 const bitmap_element *a_elt = a->first;
1492 const bitmap_element *b_elt = b->first;
1493 bitmap_element *dst_prev = NULL;
1494 bitmap_element **dst_prev_pnext = &dst->first;
1495 bool changed = false;
1497 gcc_assert (dst != a && dst != b);
1499 while (a_elt || b_elt)
1501 changed = bitmap_elt_ior (dst, dst_elt, dst_prev, a_elt, b_elt, changed);
1503 if (a_elt && b_elt && a_elt->indx == b_elt->indx)
1505 a_elt = a_elt->next;
1506 b_elt = b_elt->next;
1508 else
1510 if (a_elt && (!b_elt || a_elt->indx <= b_elt->indx))
1511 a_elt = a_elt->next;
1512 else if (b_elt && (!a_elt || b_elt->indx <= a_elt->indx))
1513 b_elt = b_elt->next;
1516 dst_prev = *dst_prev_pnext;
1517 dst_prev_pnext = &dst_prev->next;
1518 dst_elt = *dst_prev_pnext;
1521 if (dst_elt)
1523 changed = true;
1524 /* Ensure that dst->current is valid. */
1525 dst->current = dst->first;
1526 bitmap_elt_clear_from (dst, dst_elt);
1528 gcc_checking_assert (!dst->current == !dst->first);
1529 if (dst->current)
1530 dst->indx = dst->current->indx;
1531 return changed;
1534 /* A |= B. Return true if A changes. */
1536 bool
1537 bitmap_ior_into (bitmap a, const_bitmap b)
1539 bitmap_element *a_elt = a->first;
1540 const bitmap_element *b_elt = b->first;
1541 bitmap_element *a_prev = NULL;
1542 bitmap_element **a_prev_pnext = &a->first;
1543 bool changed = false;
1545 if (a == b)
1546 return false;
1548 while (b_elt)
1550 /* If A lags behind B, just advance it. */
1551 if (!a_elt || a_elt->indx == b_elt->indx)
1553 changed = bitmap_elt_ior (a, a_elt, a_prev, a_elt, b_elt, changed);
1554 b_elt = b_elt->next;
1556 else if (a_elt->indx > b_elt->indx)
1558 changed = bitmap_elt_copy (a, NULL, a_prev, b_elt, changed);
1559 b_elt = b_elt->next;
1562 a_prev = *a_prev_pnext;
1563 a_prev_pnext = &a_prev->next;
1564 a_elt = *a_prev_pnext;
1567 gcc_checking_assert (!a->current == !a->first);
1568 if (a->current)
1569 a->indx = a->current->indx;
1570 return changed;
1573 /* DST = A ^ B */
1575 void
1576 bitmap_xor (bitmap dst, const_bitmap a, const_bitmap b)
1578 bitmap_element *dst_elt = dst->first;
1579 const bitmap_element *a_elt = a->first;
1580 const bitmap_element *b_elt = b->first;
1581 bitmap_element *dst_prev = NULL;
1583 gcc_assert (dst != a && dst != b);
1584 if (a == b)
1586 bitmap_clear (dst);
1587 return;
1590 while (a_elt || b_elt)
1592 if (a_elt && b_elt && a_elt->indx == b_elt->indx)
1594 /* Matching elts, generate A ^ B. */
1595 unsigned ix;
1596 BITMAP_WORD ior = 0;
1598 if (!dst_elt)
1599 dst_elt = bitmap_elt_insert_after (dst, dst_prev, a_elt->indx);
1600 else
1601 dst_elt->indx = a_elt->indx;
1602 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1604 BITMAP_WORD r = a_elt->bits[ix] ^ b_elt->bits[ix];
1606 ior |= r;
1607 dst_elt->bits[ix] = r;
1609 a_elt = a_elt->next;
1610 b_elt = b_elt->next;
1611 if (ior)
1613 dst_prev = dst_elt;
1614 dst_elt = dst_elt->next;
1617 else
1619 /* Copy a single element. */
1620 const bitmap_element *src;
1622 if (!b_elt || (a_elt && a_elt->indx < b_elt->indx))
1624 src = a_elt;
1625 a_elt = a_elt->next;
1627 else
1629 src = b_elt;
1630 b_elt = b_elt->next;
1633 if (!dst_elt)
1634 dst_elt = bitmap_elt_insert_after (dst, dst_prev, src->indx);
1635 else
1636 dst_elt->indx = src->indx;
1637 memcpy (dst_elt->bits, src->bits, sizeof (dst_elt->bits));
1638 dst_prev = dst_elt;
1639 dst_elt = dst_elt->next;
1642 /* Ensure that dst->current is valid. */
1643 dst->current = dst->first;
1644 bitmap_elt_clear_from (dst, dst_elt);
1645 gcc_checking_assert (!dst->current == !dst->first);
1646 if (dst->current)
1647 dst->indx = dst->current->indx;
1650 /* A ^= B */
1652 void
1653 bitmap_xor_into (bitmap a, const_bitmap b)
1655 bitmap_element *a_elt = a->first;
1656 const bitmap_element *b_elt = b->first;
1657 bitmap_element *a_prev = NULL;
1659 if (a == b)
1661 bitmap_clear (a);
1662 return;
1665 while (b_elt)
1667 if (!a_elt || b_elt->indx < a_elt->indx)
1669 /* Copy b_elt. */
1670 bitmap_element *dst = bitmap_elt_insert_after (a, a_prev, b_elt->indx);
1671 memcpy (dst->bits, b_elt->bits, sizeof (dst->bits));
1672 a_prev = dst;
1673 b_elt = b_elt->next;
1675 else if (a_elt->indx < b_elt->indx)
1677 a_prev = a_elt;
1678 a_elt = a_elt->next;
1680 else
1682 /* Matching elts, generate A ^= B. */
1683 unsigned ix;
1684 BITMAP_WORD ior = 0;
1685 bitmap_element *next = a_elt->next;
1687 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1689 BITMAP_WORD r = a_elt->bits[ix] ^ b_elt->bits[ix];
1691 ior |= r;
1692 a_elt->bits[ix] = r;
1694 b_elt = b_elt->next;
1695 if (ior)
1696 a_prev = a_elt;
1697 else
1698 bitmap_element_free (a, a_elt);
1699 a_elt = next;
1702 gcc_checking_assert (!a->current == !a->first);
1703 if (a->current)
1704 a->indx = a->current->indx;
1707 /* Return true if two bitmaps are identical.
1708 We do not bother with a check for pointer equality, as that never
1709 occurs in practice. */
1711 bool
1712 bitmap_equal_p (const_bitmap a, const_bitmap b)
1714 const bitmap_element *a_elt;
1715 const bitmap_element *b_elt;
1716 unsigned ix;
1718 for (a_elt = a->first, b_elt = b->first;
1719 a_elt && b_elt;
1720 a_elt = a_elt->next, b_elt = b_elt->next)
1722 if (a_elt->indx != b_elt->indx)
1723 return false;
1724 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1725 if (a_elt->bits[ix] != b_elt->bits[ix])
1726 return false;
1728 return !a_elt && !b_elt;
1731 /* Return true if A AND B is not empty. */
1733 bool
1734 bitmap_intersect_p (const_bitmap a, const_bitmap b)
1736 const bitmap_element *a_elt;
1737 const bitmap_element *b_elt;
1738 unsigned ix;
1740 for (a_elt = a->first, b_elt = b->first;
1741 a_elt && b_elt;)
1743 if (a_elt->indx < b_elt->indx)
1744 a_elt = a_elt->next;
1745 else if (b_elt->indx < a_elt->indx)
1746 b_elt = b_elt->next;
1747 else
1749 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1750 if (a_elt->bits[ix] & b_elt->bits[ix])
1751 return true;
1752 a_elt = a_elt->next;
1753 b_elt = b_elt->next;
1756 return false;
1759 /* Return true if A AND NOT B is not empty. */
1761 bool
1762 bitmap_intersect_compl_p (const_bitmap a, const_bitmap b)
1764 const bitmap_element *a_elt;
1765 const bitmap_element *b_elt;
1766 unsigned ix;
1767 for (a_elt = a->first, b_elt = b->first;
1768 a_elt && b_elt;)
1770 if (a_elt->indx < b_elt->indx)
1771 return true;
1772 else if (b_elt->indx < a_elt->indx)
1773 b_elt = b_elt->next;
1774 else
1776 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1777 if (a_elt->bits[ix] & ~b_elt->bits[ix])
1778 return true;
1779 a_elt = a_elt->next;
1780 b_elt = b_elt->next;
1783 return a_elt != NULL;
1787 /* DST = A | (FROM1 & ~FROM2). Return true if DST changes. */
1789 bool
1790 bitmap_ior_and_compl (bitmap dst, const_bitmap a, const_bitmap b, const_bitmap kill)
1792 bool changed = false;
1794 bitmap_element *dst_elt = dst->first;
1795 const bitmap_element *a_elt = a->first;
1796 const bitmap_element *b_elt = b->first;
1797 const bitmap_element *kill_elt = kill->first;
1798 bitmap_element *dst_prev = NULL;
1799 bitmap_element **dst_prev_pnext = &dst->first;
1801 gcc_assert (dst != a && dst != b && dst != kill);
1803 /* Special cases. We don't bother checking for bitmap_equal_p (b, kill). */
1804 if (b == kill || bitmap_empty_p (b))
1806 changed = !bitmap_equal_p (dst, a);
1807 if (changed)
1808 bitmap_copy (dst, a);
1809 return changed;
1811 if (bitmap_empty_p (kill))
1812 return bitmap_ior (dst, a, b);
1813 if (bitmap_empty_p (a))
1814 return bitmap_and_compl (dst, b, kill);
1816 while (a_elt || b_elt)
1818 bool new_element = false;
1820 if (b_elt)
1821 while (kill_elt && kill_elt->indx < b_elt->indx)
1822 kill_elt = kill_elt->next;
1824 if (b_elt && kill_elt && kill_elt->indx == b_elt->indx
1825 && (!a_elt || a_elt->indx >= b_elt->indx))
1827 bitmap_element tmp_elt;
1828 unsigned ix;
1830 BITMAP_WORD ior = 0;
1831 tmp_elt.indx = b_elt->indx;
1832 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1834 BITMAP_WORD r = b_elt->bits[ix] & ~kill_elt->bits[ix];
1835 ior |= r;
1836 tmp_elt.bits[ix] = r;
1839 if (ior)
1841 changed = bitmap_elt_ior (dst, dst_elt, dst_prev,
1842 a_elt, &tmp_elt, changed);
1843 new_element = true;
1844 if (a_elt && a_elt->indx == b_elt->indx)
1845 a_elt = a_elt->next;
1848 b_elt = b_elt->next;
1849 kill_elt = kill_elt->next;
1851 else
1853 changed = bitmap_elt_ior (dst, dst_elt, dst_prev,
1854 a_elt, b_elt, changed);
1855 new_element = true;
1857 if (a_elt && b_elt && a_elt->indx == b_elt->indx)
1859 a_elt = a_elt->next;
1860 b_elt = b_elt->next;
1862 else
1864 if (a_elt && (!b_elt || a_elt->indx <= b_elt->indx))
1865 a_elt = a_elt->next;
1866 else if (b_elt && (!a_elt || b_elt->indx <= a_elt->indx))
1867 b_elt = b_elt->next;
1871 if (new_element)
1873 dst_prev = *dst_prev_pnext;
1874 dst_prev_pnext = &dst_prev->next;
1875 dst_elt = *dst_prev_pnext;
1879 if (dst_elt)
1881 changed = true;
1882 /* Ensure that dst->current is valid. */
1883 dst->current = dst->first;
1884 bitmap_elt_clear_from (dst, dst_elt);
1886 gcc_checking_assert (!dst->current == !dst->first);
1887 if (dst->current)
1888 dst->indx = dst->current->indx;
1890 return changed;
1893 /* A |= (FROM1 & ~FROM2). Return true if A changes. */
1895 bool
1896 bitmap_ior_and_compl_into (bitmap a, const_bitmap from1, const_bitmap from2)
1898 bitmap_head tmp;
1899 bool changed;
1901 bitmap_initialize (&tmp, &bitmap_default_obstack);
1902 bitmap_and_compl (&tmp, from1, from2);
1903 changed = bitmap_ior_into (a, &tmp);
1904 bitmap_clear (&tmp);
1906 return changed;
1909 /* A |= (B & C). Return true if A changes. */
1911 bool
1912 bitmap_ior_and_into (bitmap a, const_bitmap b, const_bitmap c)
1914 bitmap_element *a_elt = a->first;
1915 const bitmap_element *b_elt = b->first;
1916 const bitmap_element *c_elt = c->first;
1917 bitmap_element and_elt;
1918 bitmap_element *a_prev = NULL;
1919 bitmap_element **a_prev_pnext = &a->first;
1920 bool changed = false;
1921 unsigned ix;
1923 if (b == c)
1924 return bitmap_ior_into (a, b);
1925 if (bitmap_empty_p (b) || bitmap_empty_p (c))
1926 return false;
1928 and_elt.indx = -1;
1929 while (b_elt && c_elt)
1931 BITMAP_WORD overall;
1933 /* Find a common item of B and C. */
1934 while (b_elt->indx != c_elt->indx)
1936 if (b_elt->indx < c_elt->indx)
1938 b_elt = b_elt->next;
1939 if (!b_elt)
1940 goto done;
1942 else
1944 c_elt = c_elt->next;
1945 if (!c_elt)
1946 goto done;
1950 overall = 0;
1951 and_elt.indx = b_elt->indx;
1952 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1954 and_elt.bits[ix] = b_elt->bits[ix] & c_elt->bits[ix];
1955 overall |= and_elt.bits[ix];
1958 b_elt = b_elt->next;
1959 c_elt = c_elt->next;
1960 if (!overall)
1961 continue;
1963 /* Now find a place to insert AND_ELT. */
1966 ix = a_elt ? a_elt->indx : and_elt.indx;
1967 if (ix == and_elt.indx)
1968 changed = bitmap_elt_ior (a, a_elt, a_prev, a_elt, &and_elt, changed);
1969 else if (ix > and_elt.indx)
1970 changed = bitmap_elt_copy (a, NULL, a_prev, &and_elt, changed);
1972 a_prev = *a_prev_pnext;
1973 a_prev_pnext = &a_prev->next;
1974 a_elt = *a_prev_pnext;
1976 /* If A lagged behind B/C, we advanced it so loop once more. */
1978 while (ix < and_elt.indx);
1981 done:
1982 gcc_checking_assert (!a->current == !a->first);
1983 if (a->current)
1984 a->indx = a->current->indx;
1985 return changed;
1988 /* Compute hash of bitmap (for purposes of hashing). */
1989 hashval_t
1990 bitmap_hash (const_bitmap head)
1992 const bitmap_element *ptr;
1993 BITMAP_WORD hash = 0;
1994 int ix;
1996 for (ptr = head->first; ptr; ptr = ptr->next)
1998 hash ^= ptr->indx;
1999 for (ix = 0; ix != BITMAP_ELEMENT_WORDS; ix++)
2000 hash ^= ptr->bits[ix];
2002 return (hashval_t)hash;
2006 /* Debugging function to print out the contents of a bitmap. */
2008 DEBUG_FUNCTION void
2009 debug_bitmap_file (FILE *file, const_bitmap head)
2011 const bitmap_element *ptr;
2013 fprintf (file, "\nfirst = " HOST_PTR_PRINTF
2014 " current = " HOST_PTR_PRINTF " indx = %u\n",
2015 (void *) head->first, (void *) head->current, head->indx);
2017 for (ptr = head->first; ptr; ptr = ptr->next)
2019 unsigned int i, j, col = 26;
2021 fprintf (file, "\t" HOST_PTR_PRINTF " next = " HOST_PTR_PRINTF
2022 " prev = " HOST_PTR_PRINTF " indx = %u\n\t\tbits = {",
2023 (const void*) ptr, (const void*) ptr->next,
2024 (const void*) ptr->prev, ptr->indx);
2026 for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
2027 for (j = 0; j < BITMAP_WORD_BITS; j++)
2028 if ((ptr->bits[i] >> j) & 1)
2030 if (col > 70)
2032 fprintf (file, "\n\t\t\t");
2033 col = 24;
2036 fprintf (file, " %u", (ptr->indx * BITMAP_ELEMENT_ALL_BITS
2037 + i * BITMAP_WORD_BITS + j));
2038 col += 4;
2041 fprintf (file, " }\n");
2045 /* Function to be called from the debugger to print the contents
2046 of a bitmap. */
2048 DEBUG_FUNCTION void
2049 debug_bitmap (const_bitmap head)
2051 debug_bitmap_file (stderr, head);
2054 /* Function to print out the contents of a bitmap. Unlike debug_bitmap_file,
2055 it does not print anything but the bits. */
2057 DEBUG_FUNCTION void
2058 bitmap_print (FILE *file, const_bitmap head, const char *prefix,
2059 const char *suffix)
2061 const char *comma = "";
2062 unsigned i;
2063 bitmap_iterator bi;
2065 fputs (prefix, file);
2066 EXECUTE_IF_SET_IN_BITMAP (head, 0, i, bi)
2068 fprintf (file, "%s%d", comma, i);
2069 comma = ", ";
2071 fputs (suffix, file);
2074 /* Output per-bitmap memory usage statistics. */
2075 void
2076 dump_bitmap_statistics (void)
2078 if (!GATHER_STATISTICS)
2079 return;
2081 bitmap_mem_desc.dump (BITMAP_ORIGIN);
2084 DEBUG_FUNCTION void
2085 debug (const bitmap_head &ref)
2087 dump_bitmap (stderr, &ref);
2090 DEBUG_FUNCTION void
2091 debug (const bitmap_head *ptr)
2093 if (ptr)
2094 debug (*ptr);
2095 else
2096 fprintf (stderr, "<nil>\n");
2100 #include "gt-bitmap.h"