PR c++/18747
[official-gcc.git] / gcc / bitmap.c
blob1a28788bc3e5f9a2a9e4d9895a19a445e6b003e8
1 /* Functions to support general ended bitmaps.
2 Copyright (C) 1997-2012 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 "obstack.h"
24 #include "ggc.h"
25 #include "bitmap.h"
26 #include "hashtab.h"
28 /* Store information about each particular bitmap. */
29 struct bitmap_descriptor
31 const char *function;
32 const char *file;
33 int line;
34 int created;
35 HOST_WIDEST_INT allocated;
36 HOST_WIDEST_INT peak;
37 HOST_WIDEST_INT current;
38 int nsearches;
39 int search_iter;
42 /* Hashtable mapping bitmap names to descriptors. */
43 static htab_t bitmap_desc_hash;
45 /* Hashtable helpers. */
46 static hashval_t
47 hash_descriptor (const void *p)
49 const struct bitmap_descriptor *const d =
50 (const struct bitmap_descriptor *) p;
51 return htab_hash_pointer (d->file) + d->line;
53 struct loc
55 const char *file;
56 const char *function;
57 int line;
59 static int
60 eq_descriptor (const void *p1, const void *p2)
62 const struct bitmap_descriptor *const d =
63 (const struct bitmap_descriptor *) p1;
64 const struct loc *const l = (const struct loc *) p2;
65 return d->file == l->file && d->function == l->function && d->line == l->line;
68 /* For given file and line, return descriptor, create new if needed. */
69 static struct bitmap_descriptor *
70 bitmap_descriptor (const char *file, int line, const char *function)
72 struct bitmap_descriptor **slot;
73 struct loc loc;
75 loc.file = file;
76 loc.function = function;
77 loc.line = line;
79 if (!bitmap_desc_hash)
80 bitmap_desc_hash = htab_create (10, hash_descriptor, eq_descriptor, NULL);
82 slot = (struct bitmap_descriptor **)
83 htab_find_slot_with_hash (bitmap_desc_hash, &loc,
84 htab_hash_pointer (file) + line,
85 INSERT);
86 if (*slot)
87 return *slot;
88 *slot = XCNEW (struct bitmap_descriptor);
89 (*slot)->file = file;
90 (*slot)->function = function;
91 (*slot)->line = line;
92 return *slot;
95 /* Register new bitmap. */
96 void
97 bitmap_register (bitmap b MEM_STAT_DECL)
99 b->desc = bitmap_descriptor (ALONE_FINAL_PASS_MEM_STAT);
100 b->desc->created++;
103 /* Account the overhead. */
104 static void
105 register_overhead (bitmap b, int amount)
107 b->desc->current += amount;
108 if (amount > 0)
109 b->desc->allocated += amount;
110 gcc_assert (b->desc->current >= 0);
111 if (b->desc->peak < b->desc->current)
112 b->desc->peak = b->desc->current;
115 /* Global data */
116 bitmap_element bitmap_zero_bits; /* An element of all zero bits. */
117 bitmap_obstack bitmap_default_obstack; /* The default bitmap obstack. */
118 static int bitmap_default_obstack_depth;
119 static GTY((deletable)) bitmap_element *bitmap_ggc_free; /* Freelist of
120 GC'd elements. */
122 static void bitmap_elem_to_freelist (bitmap, bitmap_element *);
123 static void bitmap_element_free (bitmap, bitmap_element *);
124 static bitmap_element *bitmap_element_allocate (bitmap);
125 static int bitmap_element_zerop (const bitmap_element *);
126 static void bitmap_element_link (bitmap, bitmap_element *);
127 static bitmap_element *bitmap_elt_insert_after (bitmap, bitmap_element *, unsigned int);
128 static void bitmap_elt_clear_from (bitmap, bitmap_element *);
129 static bitmap_element *bitmap_find_bit (bitmap, unsigned int);
132 /* Add ELEM to the appropriate freelist. */
133 static inline void
134 bitmap_elem_to_freelist (bitmap head, bitmap_element *elt)
136 bitmap_obstack *bit_obstack = head->obstack;
138 elt->next = NULL;
139 if (bit_obstack)
141 elt->prev = bit_obstack->elements;
142 bit_obstack->elements = elt;
144 else
146 elt->prev = bitmap_ggc_free;
147 bitmap_ggc_free = elt;
151 /* Free a bitmap element. Since these are allocated off the
152 bitmap_obstack, "free" actually means "put onto the freelist". */
154 static inline void
155 bitmap_element_free (bitmap head, bitmap_element *elt)
157 bitmap_element *next = elt->next;
158 bitmap_element *prev = elt->prev;
160 if (prev)
161 prev->next = next;
163 if (next)
164 next->prev = prev;
166 if (head->first == elt)
167 head->first = next;
169 /* Since the first thing we try is to insert before current,
170 make current the next entry in preference to the previous. */
171 if (head->current == elt)
173 head->current = next != 0 ? next : prev;
174 if (head->current)
175 head->indx = head->current->indx;
176 else
177 head->indx = 0;
180 if (GATHER_STATISTICS)
181 register_overhead (head, -((int)sizeof (bitmap_element)));
183 bitmap_elem_to_freelist (head, elt);
186 /* Allocate a bitmap element. The bits are cleared, but nothing else is. */
188 static inline bitmap_element *
189 bitmap_element_allocate (bitmap head)
191 bitmap_element *element;
192 bitmap_obstack *bit_obstack = head->obstack;
194 if (bit_obstack)
196 element = bit_obstack->elements;
198 if (element)
199 /* Use up the inner list first before looking at the next
200 element of the outer list. */
201 if (element->next)
203 bit_obstack->elements = element->next;
204 bit_obstack->elements->prev = element->prev;
206 else
207 /* Inner list was just a singleton. */
208 bit_obstack->elements = element->prev;
209 else
210 element = XOBNEW (&bit_obstack->obstack, bitmap_element);
212 else
214 element = bitmap_ggc_free;
215 if (element)
216 /* Use up the inner list first before looking at the next
217 element of the outer list. */
218 if (element->next)
220 bitmap_ggc_free = element->next;
221 bitmap_ggc_free->prev = element->prev;
223 else
224 /* Inner list was just a singleton. */
225 bitmap_ggc_free = element->prev;
226 else
227 element = ggc_alloc_bitmap_element_def ();
230 if (GATHER_STATISTICS)
231 register_overhead (head, sizeof (bitmap_element));
233 memset (element->bits, 0, sizeof (element->bits));
235 return element;
238 /* Remove ELT and all following elements from bitmap HEAD. */
240 void
241 bitmap_elt_clear_from (bitmap head, bitmap_element *elt)
243 bitmap_element *prev;
244 bitmap_obstack *bit_obstack = head->obstack;
246 if (!elt) return;
248 if (GATHER_STATISTICS)
250 int n = 0;
251 for (prev = elt; prev; prev = prev->next)
252 n++;
253 register_overhead (head, -sizeof (bitmap_element) * n);
256 prev = elt->prev;
257 if (prev)
259 prev->next = NULL;
260 if (head->current->indx > prev->indx)
262 head->current = prev;
263 head->indx = prev->indx;
266 else
268 head->first = NULL;
269 head->current = NULL;
270 head->indx = 0;
273 /* Put the entire list onto the free list in one operation. */
274 if (bit_obstack)
276 elt->prev = bit_obstack->elements;
277 bit_obstack->elements = elt;
279 else
281 elt->prev = bitmap_ggc_free;
282 bitmap_ggc_free = elt;
286 /* Clear a bitmap by freeing the linked list. */
288 void
289 bitmap_clear (bitmap head)
291 if (head->first)
292 bitmap_elt_clear_from (head, head->first);
295 /* Initialize a bitmap obstack. If BIT_OBSTACK is NULL, initialize
296 the default bitmap obstack. */
298 void
299 bitmap_obstack_initialize (bitmap_obstack *bit_obstack)
301 if (!bit_obstack)
303 if (bitmap_default_obstack_depth++)
304 return;
305 bit_obstack = &bitmap_default_obstack;
308 #if !defined(__GNUC__) || (__GNUC__ < 2)
309 #define __alignof__(type) 0
310 #endif
312 bit_obstack->elements = NULL;
313 bit_obstack->heads = NULL;
314 obstack_specify_allocation (&bit_obstack->obstack, OBSTACK_CHUNK_SIZE,
315 __alignof__ (bitmap_element),
316 obstack_chunk_alloc,
317 obstack_chunk_free);
320 /* Release the memory from a bitmap obstack. If BIT_OBSTACK is NULL,
321 release the default bitmap obstack. */
323 void
324 bitmap_obstack_release (bitmap_obstack *bit_obstack)
326 if (!bit_obstack)
328 if (--bitmap_default_obstack_depth)
330 gcc_assert (bitmap_default_obstack_depth > 0);
331 return;
333 bit_obstack = &bitmap_default_obstack;
336 bit_obstack->elements = NULL;
337 bit_obstack->heads = NULL;
338 obstack_free (&bit_obstack->obstack, NULL);
341 /* Create a new bitmap on an obstack. If BIT_OBSTACK is NULL, create
342 it on the default bitmap obstack. */
344 bitmap
345 bitmap_obstack_alloc_stat (bitmap_obstack *bit_obstack MEM_STAT_DECL)
347 bitmap map;
349 if (!bit_obstack)
350 bit_obstack = &bitmap_default_obstack;
351 map = bit_obstack->heads;
352 if (map)
353 bit_obstack->heads = (struct bitmap_head_def *) map->first;
354 else
355 map = XOBNEW (&bit_obstack->obstack, bitmap_head);
356 bitmap_initialize_stat (map, bit_obstack PASS_MEM_STAT);
358 if (GATHER_STATISTICS)
359 register_overhead (map, sizeof (bitmap_head));
361 return map;
364 /* Create a new GCd bitmap. */
366 bitmap
367 bitmap_gc_alloc_stat (ALONE_MEM_STAT_DECL)
369 bitmap map;
371 map = ggc_alloc_bitmap_head_def ();
372 bitmap_initialize_stat (map, NULL PASS_MEM_STAT);
374 if (GATHER_STATISTICS)
375 register_overhead (map, sizeof (bitmap_head));
377 return map;
380 /* Release an obstack allocated bitmap. */
382 void
383 bitmap_obstack_free (bitmap map)
385 if (map)
387 bitmap_clear (map);
388 map->first = (bitmap_element *) map->obstack->heads;
390 if (GATHER_STATISTICS)
391 register_overhead (map, -((int)sizeof (bitmap_head)));
393 map->obstack->heads = map;
398 /* Return nonzero if all bits in an element are zero. */
400 static inline int
401 bitmap_element_zerop (const bitmap_element *element)
403 #if BITMAP_ELEMENT_WORDS == 2
404 return (element->bits[0] | element->bits[1]) == 0;
405 #else
406 unsigned i;
408 for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
409 if (element->bits[i] != 0)
410 return 0;
412 return 1;
413 #endif
416 /* Link the bitmap element into the current bitmap linked list. */
418 static inline void
419 bitmap_element_link (bitmap head, bitmap_element *element)
421 unsigned int indx = element->indx;
422 bitmap_element *ptr;
424 /* If this is the first and only element, set it in. */
425 if (head->first == 0)
427 element->next = element->prev = 0;
428 head->first = element;
431 /* If this index is less than that of the current element, it goes someplace
432 before the current element. */
433 else if (indx < head->indx)
435 for (ptr = head->current;
436 ptr->prev != 0 && ptr->prev->indx > indx;
437 ptr = ptr->prev)
440 if (ptr->prev)
441 ptr->prev->next = element;
442 else
443 head->first = element;
445 element->prev = ptr->prev;
446 element->next = ptr;
447 ptr->prev = element;
450 /* Otherwise, it must go someplace after the current element. */
451 else
453 for (ptr = head->current;
454 ptr->next != 0 && ptr->next->indx < indx;
455 ptr = ptr->next)
458 if (ptr->next)
459 ptr->next->prev = element;
461 element->next = ptr->next;
462 element->prev = ptr;
463 ptr->next = element;
466 /* Set up so this is the first element searched. */
467 head->current = element;
468 head->indx = indx;
471 /* Insert a new uninitialized element into bitmap HEAD after element
472 ELT. If ELT is NULL, insert the element at the start. Return the
473 new element. */
475 static bitmap_element *
476 bitmap_elt_insert_after (bitmap head, bitmap_element *elt, unsigned int indx)
478 bitmap_element *node = bitmap_element_allocate (head);
479 node->indx = indx;
481 if (!elt)
483 if (!head->current)
485 head->current = node;
486 head->indx = indx;
488 node->next = head->first;
489 if (node->next)
490 node->next->prev = node;
491 head->first = node;
492 node->prev = NULL;
494 else
496 gcc_checking_assert (head->current);
497 node->next = elt->next;
498 if (node->next)
499 node->next->prev = node;
500 elt->next = node;
501 node->prev = elt;
503 return node;
506 /* Copy a bitmap to another bitmap. */
508 void
509 bitmap_copy (bitmap to, const_bitmap from)
511 const bitmap_element *from_ptr;
512 bitmap_element *to_ptr = 0;
514 bitmap_clear (to);
516 /* Copy elements in forward direction one at a time. */
517 for (from_ptr = from->first; from_ptr; from_ptr = from_ptr->next)
519 bitmap_element *to_elt = bitmap_element_allocate (to);
521 to_elt->indx = from_ptr->indx;
522 memcpy (to_elt->bits, from_ptr->bits, sizeof (to_elt->bits));
524 /* Here we have a special case of bitmap_element_link, for the case
525 where we know the links are being entered in sequence. */
526 if (to_ptr == 0)
528 to->first = to->current = to_elt;
529 to->indx = from_ptr->indx;
530 to_elt->next = to_elt->prev = 0;
532 else
534 to_elt->prev = to_ptr;
535 to_elt->next = 0;
536 to_ptr->next = to_elt;
539 to_ptr = to_elt;
543 /* Find a bitmap element that would hold a bitmap's bit.
544 Update the `current' field even if we can't find an element that
545 would hold the bitmap's bit to make eventual allocation
546 faster. */
548 static inline bitmap_element *
549 bitmap_find_bit (bitmap head, unsigned int bit)
551 bitmap_element *element;
552 unsigned int indx = bit / BITMAP_ELEMENT_ALL_BITS;
554 if (head->current == 0
555 || head->indx == indx)
556 return head->current;
558 if (GATHER_STATISTICS)
559 head->desc->nsearches++;
561 if (head->indx < indx)
562 /* INDX is beyond head->indx. Search from head->current
563 forward. */
564 for (element = head->current;
565 element->next != 0 && element->indx < indx;
566 element = element->next)
568 if (GATHER_STATISTICS)
569 head->desc->search_iter++;
572 else if (head->indx / 2 < indx)
573 /* INDX is less than head->indx and closer to head->indx than to
574 0. Search from head->current backward. */
575 for (element = head->current;
576 element->prev != 0 && element->indx > indx;
577 element = element->prev)
579 if (GATHER_STATISTICS)
580 head->desc->search_iter++;
583 else
584 /* INDX is less than head->indx and closer to 0 than to
585 head->indx. Search from head->first forward. */
586 for (element = head->first;
587 element->next != 0 && element->indx < indx;
588 element = element->next)
589 if (GATHER_STATISTICS)
591 head->desc->search_iter++;
594 /* `element' is the nearest to the one we want. If it's not the one we
595 want, the one we want doesn't exist. */
596 head->current = element;
597 head->indx = element->indx;
598 if (element != 0 && element->indx != indx)
599 element = 0;
601 return element;
604 /* Clear a single bit in a bitmap. Return true if the bit changed. */
606 bool
607 bitmap_clear_bit (bitmap head, int bit)
609 bitmap_element *const ptr = bitmap_find_bit (head, bit);
611 if (ptr != 0)
613 unsigned bit_num = bit % BITMAP_WORD_BITS;
614 unsigned word_num = bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
615 BITMAP_WORD bit_val = ((BITMAP_WORD) 1) << bit_num;
616 bool res = (ptr->bits[word_num] & bit_val) != 0;
617 if (res)
619 ptr->bits[word_num] &= ~bit_val;
620 /* If we cleared the entire word, free up the element. */
621 if (!ptr->bits[word_num]
622 && bitmap_element_zerop (ptr))
623 bitmap_element_free (head, ptr);
626 return res;
629 return false;
632 /* Set a single bit in a bitmap. Return true if the bit changed. */
634 bool
635 bitmap_set_bit (bitmap head, int bit)
637 bitmap_element *ptr = bitmap_find_bit (head, bit);
638 unsigned word_num = bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
639 unsigned bit_num = bit % BITMAP_WORD_BITS;
640 BITMAP_WORD bit_val = ((BITMAP_WORD) 1) << bit_num;
642 if (ptr == 0)
644 ptr = bitmap_element_allocate (head);
645 ptr->indx = bit / BITMAP_ELEMENT_ALL_BITS;
646 ptr->bits[word_num] = bit_val;
647 bitmap_element_link (head, ptr);
648 return true;
650 else
652 bool res = (ptr->bits[word_num] & bit_val) == 0;
653 if (res)
654 ptr->bits[word_num] |= bit_val;
655 return res;
659 /* Return whether a bit is set within a bitmap. */
662 bitmap_bit_p (bitmap head, int bit)
664 bitmap_element *ptr;
665 unsigned bit_num;
666 unsigned word_num;
668 ptr = bitmap_find_bit (head, bit);
669 if (ptr == 0)
670 return 0;
672 bit_num = bit % BITMAP_WORD_BITS;
673 word_num = bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
675 return (ptr->bits[word_num] >> bit_num) & 1;
678 #if GCC_VERSION < 3400
679 /* Table of number of set bits in a character, indexed by value of char. */
680 static const unsigned char popcount_table[] =
682 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,
683 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,
684 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,
685 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,
686 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,
687 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,
688 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,
689 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,
692 static unsigned long
693 bitmap_popcount (BITMAP_WORD a)
695 unsigned long ret = 0;
696 unsigned i;
698 /* Just do this the table way for now */
699 for (i = 0; i < BITMAP_WORD_BITS; i+= 8)
700 ret += popcount_table[(a >> i) & 0xff];
701 return ret;
703 #endif
704 /* Count the number of bits set in the bitmap, and return it. */
706 unsigned long
707 bitmap_count_bits (const_bitmap a)
709 unsigned long count = 0;
710 const bitmap_element *elt;
711 unsigned ix;
713 for (elt = a->first; elt; elt = elt->next)
715 for (ix = 0; ix != BITMAP_ELEMENT_WORDS; ix++)
717 #if GCC_VERSION >= 3400
718 /* Note that popcountl matches BITMAP_WORD in type, so the actual size
719 of BITMAP_WORD is not material. */
720 count += __builtin_popcountl (elt->bits[ix]);
721 #else
722 count += bitmap_popcount (elt->bits[ix]);
723 #endif
726 return count;
729 /* Return true if the bitmap has a single bit set. Otherwise return
730 false. */
732 bool
733 bitmap_single_bit_set_p (const_bitmap a)
735 unsigned long count = 0;
736 const bitmap_element *elt;
737 unsigned ix;
739 if (bitmap_empty_p (a))
740 return false;
742 elt = a->first;
743 /* As there are no completely empty bitmap elements, a second one
744 means we have more than one bit set. */
745 if (elt->next != NULL)
746 return false;
748 for (ix = 0; ix != BITMAP_ELEMENT_WORDS; ix++)
750 #if GCC_VERSION >= 3400
751 /* Note that popcountl matches BITMAP_WORD in type, so the actual size
752 of BITMAP_WORD is not material. */
753 count += __builtin_popcountl (elt->bits[ix]);
754 #else
755 count += bitmap_popcount (elt->bits[ix]);
756 #endif
757 if (count > 1)
758 return false;
761 return count == 1;
765 /* Return the bit number of the first set bit in the bitmap. The
766 bitmap must be non-empty. */
768 unsigned
769 bitmap_first_set_bit (const_bitmap a)
771 const bitmap_element *elt = a->first;
772 unsigned bit_no;
773 BITMAP_WORD word;
774 unsigned ix;
776 gcc_checking_assert (elt);
777 bit_no = elt->indx * BITMAP_ELEMENT_ALL_BITS;
778 for (ix = 0; ix != BITMAP_ELEMENT_WORDS; ix++)
780 word = elt->bits[ix];
781 if (word)
782 goto found_bit;
784 gcc_unreachable ();
785 found_bit:
786 bit_no += ix * BITMAP_WORD_BITS;
788 #if GCC_VERSION >= 3004
789 gcc_assert (sizeof(long) == sizeof (word));
790 bit_no += __builtin_ctzl (word);
791 #else
792 /* Binary search for the first set bit. */
793 #if BITMAP_WORD_BITS > 64
794 #error "Fill out the table."
795 #endif
796 #if BITMAP_WORD_BITS > 32
797 if (!(word & 0xffffffff))
798 word >>= 32, bit_no += 32;
799 #endif
800 if (!(word & 0xffff))
801 word >>= 16, bit_no += 16;
802 if (!(word & 0xff))
803 word >>= 8, bit_no += 8;
804 if (!(word & 0xf))
805 word >>= 4, bit_no += 4;
806 if (!(word & 0x3))
807 word >>= 2, bit_no += 2;
808 if (!(word & 0x1))
809 word >>= 1, bit_no += 1;
811 gcc_checking_assert (word & 1);
812 #endif
813 return bit_no;
816 /* Return the bit number of the first set bit in the bitmap. The
817 bitmap must be non-empty. */
819 unsigned
820 bitmap_last_set_bit (const_bitmap a)
822 const bitmap_element *elt = a->current ? a->current : a->first;
823 unsigned bit_no;
824 BITMAP_WORD word;
825 int ix;
827 gcc_checking_assert (elt);
828 while (elt->next)
829 elt = elt->next;
830 bit_no = elt->indx * BITMAP_ELEMENT_ALL_BITS;
831 for (ix = BITMAP_ELEMENT_WORDS - 1; ix >= 0; ix--)
833 word = elt->bits[ix];
834 if (word)
835 goto found_bit;
837 gcc_unreachable ();
838 found_bit:
839 bit_no += ix * BITMAP_WORD_BITS;
841 /* Binary search for the last set bit. */
842 #if GCC_VERSION >= 3004
843 gcc_assert (sizeof(long) == sizeof (word));
844 bit_no += sizeof (long) * 8 - __builtin_ctzl (word);
845 #else
846 #if BITMAP_WORD_BITS > 64
847 #error "Fill out the table."
848 #endif
849 #if BITMAP_WORD_BITS > 32
850 if ((word & 0xffffffff00000000))
851 word >>= 32, bit_no += 32;
852 #endif
853 if (word & 0xffff0000)
854 word >>= 16, bit_no += 16;
855 if (!(word & 0xff00))
856 word >>= 8, bit_no += 8;
857 if (!(word & 0xf0))
858 word >>= 4, bit_no += 4;
859 if (!(word & 12))
860 word >>= 2, bit_no += 2;
861 if (!(word & 2))
862 word >>= 1, bit_no += 1;
863 #endif
865 gcc_checking_assert (word & 1);
866 return bit_no;
870 /* DST = A & B. */
872 void
873 bitmap_and (bitmap dst, const_bitmap a, const_bitmap b)
875 bitmap_element *dst_elt = dst->first;
876 const bitmap_element *a_elt = a->first;
877 const bitmap_element *b_elt = b->first;
878 bitmap_element *dst_prev = NULL;
880 gcc_assert (dst != a && dst != b);
882 if (a == b)
884 bitmap_copy (dst, a);
885 return;
888 while (a_elt && b_elt)
890 if (a_elt->indx < b_elt->indx)
891 a_elt = a_elt->next;
892 else if (b_elt->indx < a_elt->indx)
893 b_elt = b_elt->next;
894 else
896 /* Matching elts, generate A & B. */
897 unsigned ix;
898 BITMAP_WORD ior = 0;
900 if (!dst_elt)
901 dst_elt = bitmap_elt_insert_after (dst, dst_prev, a_elt->indx);
902 else
903 dst_elt->indx = a_elt->indx;
904 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
906 BITMAP_WORD r = a_elt->bits[ix] & b_elt->bits[ix];
908 dst_elt->bits[ix] = r;
909 ior |= r;
911 if (ior)
913 dst_prev = dst_elt;
914 dst_elt = dst_elt->next;
916 a_elt = a_elt->next;
917 b_elt = b_elt->next;
920 /* Ensure that dst->current is valid. */
921 dst->current = dst->first;
922 bitmap_elt_clear_from (dst, dst_elt);
923 gcc_checking_assert (!dst->current == !dst->first);
924 if (dst->current)
925 dst->indx = dst->current->indx;
928 /* A &= B. */
930 void
931 bitmap_and_into (bitmap a, const_bitmap b)
933 bitmap_element *a_elt = a->first;
934 const bitmap_element *b_elt = b->first;
935 bitmap_element *next;
937 if (a == b)
938 return;
940 while (a_elt && b_elt)
942 if (a_elt->indx < b_elt->indx)
944 next = a_elt->next;
945 bitmap_element_free (a, a_elt);
946 a_elt = next;
948 else if (b_elt->indx < a_elt->indx)
949 b_elt = b_elt->next;
950 else
952 /* Matching elts, generate A &= B. */
953 unsigned ix;
954 BITMAP_WORD ior = 0;
956 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
958 BITMAP_WORD r = a_elt->bits[ix] & b_elt->bits[ix];
960 a_elt->bits[ix] = r;
961 ior |= r;
963 next = a_elt->next;
964 if (!ior)
965 bitmap_element_free (a, a_elt);
966 a_elt = next;
967 b_elt = b_elt->next;
970 bitmap_elt_clear_from (a, a_elt);
971 gcc_checking_assert (!a->current == !a->first
972 && (!a->current || a->indx == a->current->indx));
976 /* Insert an element equal to SRC_ELT after DST_PREV, overwriting DST_ELT
977 if non-NULL. CHANGED is true if the destination bitmap had already been
978 changed; the new value of CHANGED is returned. */
980 static inline bool
981 bitmap_elt_copy (bitmap dst, bitmap_element *dst_elt, bitmap_element *dst_prev,
982 const bitmap_element *src_elt, bool changed)
984 if (!changed && dst_elt && dst_elt->indx == src_elt->indx)
986 unsigned ix;
988 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
989 if (src_elt->bits[ix] != dst_elt->bits[ix])
991 dst_elt->bits[ix] = src_elt->bits[ix];
992 changed = true;
995 else
997 changed = true;
998 if (!dst_elt)
999 dst_elt = bitmap_elt_insert_after (dst, dst_prev, src_elt->indx);
1000 else
1001 dst_elt->indx = src_elt->indx;
1002 memcpy (dst_elt->bits, src_elt->bits, sizeof (dst_elt->bits));
1004 return changed;
1009 /* DST = A & ~B */
1011 bool
1012 bitmap_and_compl (bitmap dst, const_bitmap a, const_bitmap b)
1014 bitmap_element *dst_elt = dst->first;
1015 const bitmap_element *a_elt = a->first;
1016 const bitmap_element *b_elt = b->first;
1017 bitmap_element *dst_prev = NULL;
1018 bitmap_element **dst_prev_pnext = &dst->first;
1019 bool changed = false;
1021 gcc_assert (dst != a && dst != b);
1023 if (a == b)
1025 changed = !bitmap_empty_p (dst);
1026 bitmap_clear (dst);
1027 return changed;
1030 while (a_elt)
1032 while (b_elt && b_elt->indx < a_elt->indx)
1033 b_elt = b_elt->next;
1035 if (!b_elt || b_elt->indx > a_elt->indx)
1037 changed = bitmap_elt_copy (dst, dst_elt, dst_prev, a_elt, changed);
1038 dst_prev = *dst_prev_pnext;
1039 dst_prev_pnext = &dst_prev->next;
1040 dst_elt = *dst_prev_pnext;
1041 a_elt = a_elt->next;
1044 else
1046 /* Matching elts, generate A & ~B. */
1047 unsigned ix;
1048 BITMAP_WORD ior = 0;
1050 if (!changed && dst_elt && dst_elt->indx == a_elt->indx)
1052 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1054 BITMAP_WORD r = a_elt->bits[ix] & ~b_elt->bits[ix];
1056 if (dst_elt->bits[ix] != r)
1058 changed = true;
1059 dst_elt->bits[ix] = r;
1061 ior |= r;
1064 else
1066 bool new_element;
1067 if (!dst_elt || dst_elt->indx > a_elt->indx)
1069 dst_elt = bitmap_elt_insert_after (dst, dst_prev, a_elt->indx);
1070 new_element = true;
1072 else
1074 dst_elt->indx = a_elt->indx;
1075 new_element = false;
1078 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1080 BITMAP_WORD r = a_elt->bits[ix] & ~b_elt->bits[ix];
1082 dst_elt->bits[ix] = r;
1083 ior |= r;
1086 if (ior)
1087 changed = true;
1088 else
1090 changed |= !new_element;
1091 bitmap_element_free (dst, dst_elt);
1092 dst_elt = *dst_prev_pnext;
1096 if (ior)
1098 dst_prev = *dst_prev_pnext;
1099 dst_prev_pnext = &dst_prev->next;
1100 dst_elt = *dst_prev_pnext;
1102 a_elt = a_elt->next;
1103 b_elt = b_elt->next;
1107 /* Ensure that dst->current is valid. */
1108 dst->current = dst->first;
1110 if (dst_elt)
1112 changed = true;
1113 bitmap_elt_clear_from (dst, dst_elt);
1115 gcc_checking_assert (!dst->current == !dst->first);
1116 if (dst->current)
1117 dst->indx = dst->current->indx;
1119 return changed;
1122 /* A &= ~B. Returns true if A changes */
1124 bool
1125 bitmap_and_compl_into (bitmap a, const_bitmap b)
1127 bitmap_element *a_elt = a->first;
1128 const bitmap_element *b_elt = b->first;
1129 bitmap_element *next;
1130 BITMAP_WORD changed = 0;
1132 if (a == b)
1134 if (bitmap_empty_p (a))
1135 return false;
1136 else
1138 bitmap_clear (a);
1139 return true;
1143 while (a_elt && b_elt)
1145 if (a_elt->indx < b_elt->indx)
1146 a_elt = a_elt->next;
1147 else if (b_elt->indx < a_elt->indx)
1148 b_elt = b_elt->next;
1149 else
1151 /* Matching elts, generate A &= ~B. */
1152 unsigned ix;
1153 BITMAP_WORD ior = 0;
1155 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1157 BITMAP_WORD cleared = a_elt->bits[ix] & b_elt->bits[ix];
1158 BITMAP_WORD r = a_elt->bits[ix] ^ cleared;
1160 a_elt->bits[ix] = r;
1161 changed |= cleared;
1162 ior |= r;
1164 next = a_elt->next;
1165 if (!ior)
1166 bitmap_element_free (a, a_elt);
1167 a_elt = next;
1168 b_elt = b_elt->next;
1171 gcc_checking_assert (!a->current == !a->first
1172 && (!a->current || a->indx == a->current->indx));
1173 return changed != 0;
1176 /* Set COUNT bits from START in HEAD. */
1177 void
1178 bitmap_set_range (bitmap head, unsigned int start, unsigned int count)
1180 unsigned int first_index, end_bit_plus1, last_index;
1181 bitmap_element *elt, *elt_prev;
1182 unsigned int i;
1184 if (!count)
1185 return;
1187 first_index = start / BITMAP_ELEMENT_ALL_BITS;
1188 end_bit_plus1 = start + count;
1189 last_index = (end_bit_plus1 - 1) / BITMAP_ELEMENT_ALL_BITS;
1190 elt = bitmap_find_bit (head, start);
1192 /* If bitmap_find_bit returns zero, the current is the closest block
1193 to the result. Otherwise, just use bitmap_element_allocate to
1194 ensure ELT is set; in the loop below, ELT == NULL means "insert
1195 at the end of the bitmap". */
1196 if (!elt)
1198 elt = bitmap_element_allocate (head);
1199 elt->indx = first_index;
1200 bitmap_element_link (head, elt);
1203 gcc_checking_assert (elt->indx == first_index);
1204 elt_prev = elt->prev;
1205 for (i = first_index; i <= last_index; i++)
1207 unsigned elt_start_bit = i * BITMAP_ELEMENT_ALL_BITS;
1208 unsigned elt_end_bit_plus1 = elt_start_bit + BITMAP_ELEMENT_ALL_BITS;
1210 unsigned int first_word_to_mod;
1211 BITMAP_WORD first_mask;
1212 unsigned int last_word_to_mod;
1213 BITMAP_WORD last_mask;
1214 unsigned int ix;
1216 if (!elt || elt->indx != i)
1217 elt = bitmap_elt_insert_after (head, elt_prev, i);
1219 if (elt_start_bit <= start)
1221 /* The first bit to turn on is somewhere inside this
1222 elt. */
1223 first_word_to_mod = (start - elt_start_bit) / BITMAP_WORD_BITS;
1225 /* This mask should have 1s in all bits >= start position. */
1226 first_mask =
1227 (((BITMAP_WORD) 1) << ((start % BITMAP_WORD_BITS))) - 1;
1228 first_mask = ~first_mask;
1230 else
1232 /* The first bit to turn on is below this start of this elt. */
1233 first_word_to_mod = 0;
1234 first_mask = ~(BITMAP_WORD) 0;
1237 if (elt_end_bit_plus1 <= end_bit_plus1)
1239 /* The last bit to turn on is beyond this elt. */
1240 last_word_to_mod = BITMAP_ELEMENT_WORDS - 1;
1241 last_mask = ~(BITMAP_WORD) 0;
1243 else
1245 /* The last bit to turn on is inside to this elt. */
1246 last_word_to_mod =
1247 (end_bit_plus1 - elt_start_bit) / BITMAP_WORD_BITS;
1249 /* The last mask should have 1s below the end bit. */
1250 last_mask =
1251 (((BITMAP_WORD) 1) << ((end_bit_plus1 % BITMAP_WORD_BITS))) - 1;
1254 if (first_word_to_mod == last_word_to_mod)
1256 BITMAP_WORD mask = first_mask & last_mask;
1257 elt->bits[first_word_to_mod] |= mask;
1259 else
1261 elt->bits[first_word_to_mod] |= first_mask;
1262 if (BITMAP_ELEMENT_WORDS > 2)
1263 for (ix = first_word_to_mod + 1; ix < last_word_to_mod; ix++)
1264 elt->bits[ix] = ~(BITMAP_WORD) 0;
1265 elt->bits[last_word_to_mod] |= last_mask;
1268 elt_prev = elt;
1269 elt = elt->next;
1272 head->current = elt ? elt : elt_prev;
1273 head->indx = head->current->indx;
1276 /* Clear COUNT bits from START in HEAD. */
1277 void
1278 bitmap_clear_range (bitmap head, unsigned int start, unsigned int count)
1280 unsigned int first_index, end_bit_plus1, last_index;
1281 bitmap_element *elt;
1283 if (!count)
1284 return;
1286 first_index = start / BITMAP_ELEMENT_ALL_BITS;
1287 end_bit_plus1 = start + count;
1288 last_index = (end_bit_plus1 - 1) / BITMAP_ELEMENT_ALL_BITS;
1289 elt = bitmap_find_bit (head, start);
1291 /* If bitmap_find_bit returns zero, the current is the closest block
1292 to the result. If the current is less than first index, find the
1293 next one. Otherwise, just set elt to be current. */
1294 if (!elt)
1296 if (head->current)
1298 if (head->indx < first_index)
1300 elt = head->current->next;
1301 if (!elt)
1302 return;
1304 else
1305 elt = head->current;
1307 else
1308 return;
1311 while (elt && (elt->indx <= last_index))
1313 bitmap_element * next_elt = elt->next;
1314 unsigned elt_start_bit = (elt->indx) * BITMAP_ELEMENT_ALL_BITS;
1315 unsigned elt_end_bit_plus1 = elt_start_bit + BITMAP_ELEMENT_ALL_BITS;
1318 if (elt_start_bit >= start && elt_end_bit_plus1 <= end_bit_plus1)
1319 /* Get rid of the entire elt and go to the next one. */
1320 bitmap_element_free (head, elt);
1321 else
1323 /* Going to have to knock out some bits in this elt. */
1324 unsigned int first_word_to_mod;
1325 BITMAP_WORD first_mask;
1326 unsigned int last_word_to_mod;
1327 BITMAP_WORD last_mask;
1328 unsigned int i;
1329 bool clear = true;
1331 if (elt_start_bit <= start)
1333 /* The first bit to turn off is somewhere inside this
1334 elt. */
1335 first_word_to_mod = (start - elt_start_bit) / BITMAP_WORD_BITS;
1337 /* This mask should have 1s in all bits >= start position. */
1338 first_mask =
1339 (((BITMAP_WORD) 1) << ((start % BITMAP_WORD_BITS))) - 1;
1340 first_mask = ~first_mask;
1342 else
1344 /* The first bit to turn off is below this start of this elt. */
1345 first_word_to_mod = 0;
1346 first_mask = 0;
1347 first_mask = ~first_mask;
1350 if (elt_end_bit_plus1 <= end_bit_plus1)
1352 /* The last bit to turn off is beyond this elt. */
1353 last_word_to_mod = BITMAP_ELEMENT_WORDS - 1;
1354 last_mask = 0;
1355 last_mask = ~last_mask;
1357 else
1359 /* The last bit to turn off is inside to this elt. */
1360 last_word_to_mod =
1361 (end_bit_plus1 - elt_start_bit) / BITMAP_WORD_BITS;
1363 /* The last mask should have 1s below the end bit. */
1364 last_mask =
1365 (((BITMAP_WORD) 1) << (((end_bit_plus1) % BITMAP_WORD_BITS))) - 1;
1369 if (first_word_to_mod == last_word_to_mod)
1371 BITMAP_WORD mask = first_mask & last_mask;
1372 elt->bits[first_word_to_mod] &= ~mask;
1374 else
1376 elt->bits[first_word_to_mod] &= ~first_mask;
1377 if (BITMAP_ELEMENT_WORDS > 2)
1378 for (i = first_word_to_mod + 1; i < last_word_to_mod; i++)
1379 elt->bits[i] = 0;
1380 elt->bits[last_word_to_mod] &= ~last_mask;
1382 for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
1383 if (elt->bits[i])
1385 clear = false;
1386 break;
1388 /* Check to see if there are any bits left. */
1389 if (clear)
1390 bitmap_element_free (head, elt);
1392 elt = next_elt;
1395 if (elt)
1397 head->current = elt;
1398 head->indx = head->current->indx;
1402 /* A = ~A & B. */
1404 void
1405 bitmap_compl_and_into (bitmap a, const_bitmap b)
1407 bitmap_element *a_elt = a->first;
1408 const bitmap_element *b_elt = b->first;
1409 bitmap_element *a_prev = NULL;
1410 bitmap_element *next;
1412 gcc_assert (a != b);
1414 if (bitmap_empty_p (a))
1416 bitmap_copy (a, b);
1417 return;
1419 if (bitmap_empty_p (b))
1421 bitmap_clear (a);
1422 return;
1425 while (a_elt || b_elt)
1427 if (!b_elt || (a_elt && a_elt->indx < b_elt->indx))
1429 /* A is before B. Remove A */
1430 next = a_elt->next;
1431 a_prev = a_elt->prev;
1432 bitmap_element_free (a, a_elt);
1433 a_elt = next;
1435 else if (!a_elt || b_elt->indx < a_elt->indx)
1437 /* B is before A. Copy B. */
1438 next = bitmap_elt_insert_after (a, a_prev, b_elt->indx);
1439 memcpy (next->bits, b_elt->bits, sizeof (next->bits));
1440 a_prev = next;
1441 b_elt = b_elt->next;
1443 else
1445 /* Matching elts, generate A = ~A & B. */
1446 unsigned ix;
1447 BITMAP_WORD ior = 0;
1449 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1451 BITMAP_WORD cleared = a_elt->bits[ix] & b_elt->bits[ix];
1452 BITMAP_WORD r = b_elt->bits[ix] ^ cleared;
1454 a_elt->bits[ix] = r;
1455 ior |= r;
1457 next = a_elt->next;
1458 if (!ior)
1459 bitmap_element_free (a, a_elt);
1460 else
1461 a_prev = a_elt;
1462 a_elt = next;
1463 b_elt = b_elt->next;
1466 gcc_checking_assert (!a->current == !a->first
1467 && (!a->current || a->indx == a->current->indx));
1468 return;
1472 /* Insert an element corresponding to A_ELT | B_ELT after DST_PREV,
1473 overwriting DST_ELT if non-NULL. CHANGED is true if the destination bitmap
1474 had already been changed; the new value of CHANGED is returned. */
1476 static inline bool
1477 bitmap_elt_ior (bitmap dst, bitmap_element *dst_elt, bitmap_element *dst_prev,
1478 const bitmap_element *a_elt, const bitmap_element *b_elt,
1479 bool changed)
1481 gcc_assert (a_elt || b_elt);
1483 if (a_elt && b_elt && a_elt->indx == b_elt->indx)
1485 /* Matching elts, generate A | B. */
1486 unsigned ix;
1488 if (!changed && dst_elt && dst_elt->indx == a_elt->indx)
1490 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1492 BITMAP_WORD r = a_elt->bits[ix] | b_elt->bits[ix];
1493 if (r != dst_elt->bits[ix])
1495 dst_elt->bits[ix] = r;
1496 changed = true;
1500 else
1502 changed = true;
1503 if (!dst_elt)
1504 dst_elt = bitmap_elt_insert_after (dst, dst_prev, a_elt->indx);
1505 else
1506 dst_elt->indx = a_elt->indx;
1507 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1509 BITMAP_WORD r = a_elt->bits[ix] | b_elt->bits[ix];
1510 dst_elt->bits[ix] = r;
1514 else
1516 /* Copy a single element. */
1517 const bitmap_element *src;
1519 if (!b_elt || (a_elt && a_elt->indx < b_elt->indx))
1520 src = a_elt;
1521 else
1522 src = b_elt;
1524 gcc_checking_assert (src);
1525 changed = bitmap_elt_copy (dst, dst_elt, dst_prev, src, changed);
1527 return changed;
1531 /* DST = A | B. Return true if DST changes. */
1533 bool
1534 bitmap_ior (bitmap dst, const_bitmap a, const_bitmap b)
1536 bitmap_element *dst_elt = dst->first;
1537 const bitmap_element *a_elt = a->first;
1538 const bitmap_element *b_elt = b->first;
1539 bitmap_element *dst_prev = NULL;
1540 bitmap_element **dst_prev_pnext = &dst->first;
1541 bool changed = false;
1543 gcc_assert (dst != a && dst != b);
1545 while (a_elt || b_elt)
1547 changed = bitmap_elt_ior (dst, dst_elt, dst_prev, a_elt, b_elt, changed);
1549 if (a_elt && b_elt && a_elt->indx == b_elt->indx)
1551 a_elt = a_elt->next;
1552 b_elt = b_elt->next;
1554 else
1556 if (a_elt && (!b_elt || a_elt->indx <= b_elt->indx))
1557 a_elt = a_elt->next;
1558 else if (b_elt && (!a_elt || b_elt->indx <= a_elt->indx))
1559 b_elt = b_elt->next;
1562 dst_prev = *dst_prev_pnext;
1563 dst_prev_pnext = &dst_prev->next;
1564 dst_elt = *dst_prev_pnext;
1567 if (dst_elt)
1569 changed = true;
1570 bitmap_elt_clear_from (dst, dst_elt);
1572 gcc_checking_assert (!dst->current == !dst->first);
1573 if (dst->current)
1574 dst->indx = dst->current->indx;
1575 return changed;
1578 /* A |= B. Return true if A changes. */
1580 bool
1581 bitmap_ior_into (bitmap a, const_bitmap b)
1583 bitmap_element *a_elt = a->first;
1584 const bitmap_element *b_elt = b->first;
1585 bitmap_element *a_prev = NULL;
1586 bitmap_element **a_prev_pnext = &a->first;
1587 bool changed = false;
1589 if (a == b)
1590 return false;
1592 while (b_elt)
1594 /* If A lags behind B, just advance it. */
1595 if (!a_elt || a_elt->indx == b_elt->indx)
1597 changed = bitmap_elt_ior (a, a_elt, a_prev, a_elt, b_elt, changed);
1598 b_elt = b_elt->next;
1600 else if (a_elt->indx > b_elt->indx)
1602 changed = bitmap_elt_copy (a, NULL, a_prev, b_elt, changed);
1603 b_elt = b_elt->next;
1606 a_prev = *a_prev_pnext;
1607 a_prev_pnext = &a_prev->next;
1608 a_elt = *a_prev_pnext;
1611 gcc_checking_assert (!a->current == !a->first);
1612 if (a->current)
1613 a->indx = a->current->indx;
1614 return changed;
1617 /* DST = A ^ B */
1619 void
1620 bitmap_xor (bitmap dst, const_bitmap a, const_bitmap b)
1622 bitmap_element *dst_elt = dst->first;
1623 const bitmap_element *a_elt = a->first;
1624 const bitmap_element *b_elt = b->first;
1625 bitmap_element *dst_prev = NULL;
1627 gcc_assert (dst != a && dst != b);
1628 if (a == b)
1630 bitmap_clear (dst);
1631 return;
1634 while (a_elt || b_elt)
1636 if (a_elt && b_elt && a_elt->indx == b_elt->indx)
1638 /* Matching elts, generate A ^ B. */
1639 unsigned ix;
1640 BITMAP_WORD ior = 0;
1642 if (!dst_elt)
1643 dst_elt = bitmap_elt_insert_after (dst, dst_prev, a_elt->indx);
1644 else
1645 dst_elt->indx = a_elt->indx;
1646 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1648 BITMAP_WORD r = a_elt->bits[ix] ^ b_elt->bits[ix];
1650 ior |= r;
1651 dst_elt->bits[ix] = r;
1653 a_elt = a_elt->next;
1654 b_elt = b_elt->next;
1655 if (ior)
1657 dst_prev = dst_elt;
1658 dst_elt = dst_elt->next;
1661 else
1663 /* Copy a single element. */
1664 const bitmap_element *src;
1666 if (!b_elt || (a_elt && a_elt->indx < b_elt->indx))
1668 src = a_elt;
1669 a_elt = a_elt->next;
1671 else
1673 src = b_elt;
1674 b_elt = b_elt->next;
1677 if (!dst_elt)
1678 dst_elt = bitmap_elt_insert_after (dst, dst_prev, src->indx);
1679 else
1680 dst_elt->indx = src->indx;
1681 memcpy (dst_elt->bits, src->bits, sizeof (dst_elt->bits));
1682 dst_prev = dst_elt;
1683 dst_elt = dst_elt->next;
1686 /* Ensure that dst->current is valid. */
1687 dst->current = dst->first;
1688 bitmap_elt_clear_from (dst, dst_elt);
1689 gcc_checking_assert (!dst->current == !dst->first);
1690 if (dst->current)
1691 dst->indx = dst->current->indx;
1694 /* A ^= B */
1696 void
1697 bitmap_xor_into (bitmap a, const_bitmap b)
1699 bitmap_element *a_elt = a->first;
1700 const bitmap_element *b_elt = b->first;
1701 bitmap_element *a_prev = NULL;
1703 if (a == b)
1705 bitmap_clear (a);
1706 return;
1709 while (b_elt)
1711 if (!a_elt || b_elt->indx < a_elt->indx)
1713 /* Copy b_elt. */
1714 bitmap_element *dst = bitmap_elt_insert_after (a, a_prev, b_elt->indx);
1715 memcpy (dst->bits, b_elt->bits, sizeof (dst->bits));
1716 a_prev = dst;
1717 b_elt = b_elt->next;
1719 else if (a_elt->indx < b_elt->indx)
1721 a_prev = a_elt;
1722 a_elt = a_elt->next;
1724 else
1726 /* Matching elts, generate A ^= B. */
1727 unsigned ix;
1728 BITMAP_WORD ior = 0;
1729 bitmap_element *next = a_elt->next;
1731 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1733 BITMAP_WORD r = a_elt->bits[ix] ^ b_elt->bits[ix];
1735 ior |= r;
1736 a_elt->bits[ix] = r;
1738 b_elt = b_elt->next;
1739 if (ior)
1740 a_prev = a_elt;
1741 else
1742 bitmap_element_free (a, a_elt);
1743 a_elt = next;
1746 gcc_checking_assert (!a->current == !a->first);
1747 if (a->current)
1748 a->indx = a->current->indx;
1751 /* Return true if two bitmaps are identical.
1752 We do not bother with a check for pointer equality, as that never
1753 occurs in practice. */
1755 bool
1756 bitmap_equal_p (const_bitmap a, const_bitmap b)
1758 const bitmap_element *a_elt;
1759 const bitmap_element *b_elt;
1760 unsigned ix;
1762 for (a_elt = a->first, b_elt = b->first;
1763 a_elt && b_elt;
1764 a_elt = a_elt->next, b_elt = b_elt->next)
1766 if (a_elt->indx != b_elt->indx)
1767 return false;
1768 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1769 if (a_elt->bits[ix] != b_elt->bits[ix])
1770 return false;
1772 return !a_elt && !b_elt;
1775 /* Return true if A AND B is not empty. */
1777 bool
1778 bitmap_intersect_p (const_bitmap a, const_bitmap b)
1780 const bitmap_element *a_elt;
1781 const bitmap_element *b_elt;
1782 unsigned ix;
1784 for (a_elt = a->first, b_elt = b->first;
1785 a_elt && b_elt;)
1787 if (a_elt->indx < b_elt->indx)
1788 a_elt = a_elt->next;
1789 else if (b_elt->indx < a_elt->indx)
1790 b_elt = b_elt->next;
1791 else
1793 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1794 if (a_elt->bits[ix] & b_elt->bits[ix])
1795 return true;
1796 a_elt = a_elt->next;
1797 b_elt = b_elt->next;
1800 return false;
1803 /* Return true if A AND NOT B is not empty. */
1805 bool
1806 bitmap_intersect_compl_p (const_bitmap a, const_bitmap b)
1808 const bitmap_element *a_elt;
1809 const bitmap_element *b_elt;
1810 unsigned ix;
1811 for (a_elt = a->first, b_elt = b->first;
1812 a_elt && b_elt;)
1814 if (a_elt->indx < b_elt->indx)
1815 return true;
1816 else if (b_elt->indx < a_elt->indx)
1817 b_elt = b_elt->next;
1818 else
1820 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1821 if (a_elt->bits[ix] & ~b_elt->bits[ix])
1822 return true;
1823 a_elt = a_elt->next;
1824 b_elt = b_elt->next;
1827 return a_elt != NULL;
1831 /* DST = A | (FROM1 & ~FROM2). Return true if DST changes. */
1833 bool
1834 bitmap_ior_and_compl (bitmap dst, const_bitmap a, const_bitmap b, const_bitmap kill)
1836 bool changed = false;
1838 bitmap_element *dst_elt = dst->first;
1839 const bitmap_element *a_elt = a->first;
1840 const bitmap_element *b_elt = b->first;
1841 const bitmap_element *kill_elt = kill->first;
1842 bitmap_element *dst_prev = NULL;
1843 bitmap_element **dst_prev_pnext = &dst->first;
1845 gcc_assert (dst != a && dst != b && dst != kill);
1847 /* Special cases. We don't bother checking for bitmap_equal_p (b, kill). */
1848 if (b == kill || bitmap_empty_p (b))
1850 changed = !bitmap_equal_p (dst, a);
1851 if (changed)
1852 bitmap_copy (dst, a);
1853 return changed;
1855 if (bitmap_empty_p (kill))
1856 return bitmap_ior (dst, a, b);
1857 if (bitmap_empty_p (a))
1858 return bitmap_and_compl (dst, b, kill);
1860 while (a_elt || b_elt)
1862 bool new_element = false;
1864 if (b_elt)
1865 while (kill_elt && kill_elt->indx < b_elt->indx)
1866 kill_elt = kill_elt->next;
1868 if (b_elt && kill_elt && kill_elt->indx == b_elt->indx
1869 && (!a_elt || a_elt->indx >= b_elt->indx))
1871 bitmap_element tmp_elt;
1872 unsigned ix;
1874 BITMAP_WORD ior = 0;
1875 tmp_elt.indx = b_elt->indx;
1876 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1878 BITMAP_WORD r = b_elt->bits[ix] & ~kill_elt->bits[ix];
1879 ior |= r;
1880 tmp_elt.bits[ix] = r;
1883 if (ior)
1885 changed = bitmap_elt_ior (dst, dst_elt, dst_prev,
1886 a_elt, &tmp_elt, changed);
1887 new_element = true;
1888 if (a_elt && a_elt->indx == b_elt->indx)
1889 a_elt = a_elt->next;
1892 b_elt = b_elt->next;
1893 kill_elt = kill_elt->next;
1895 else
1897 changed = bitmap_elt_ior (dst, dst_elt, dst_prev,
1898 a_elt, b_elt, changed);
1899 new_element = true;
1901 if (a_elt && b_elt && a_elt->indx == b_elt->indx)
1903 a_elt = a_elt->next;
1904 b_elt = b_elt->next;
1906 else
1908 if (a_elt && (!b_elt || a_elt->indx <= b_elt->indx))
1909 a_elt = a_elt->next;
1910 else if (b_elt && (!a_elt || b_elt->indx <= a_elt->indx))
1911 b_elt = b_elt->next;
1915 if (new_element)
1917 dst_prev = *dst_prev_pnext;
1918 dst_prev_pnext = &dst_prev->next;
1919 dst_elt = *dst_prev_pnext;
1923 if (dst_elt)
1925 changed = true;
1926 bitmap_elt_clear_from (dst, dst_elt);
1928 gcc_checking_assert (!dst->current == !dst->first);
1929 if (dst->current)
1930 dst->indx = dst->current->indx;
1932 return changed;
1935 /* A |= (FROM1 & ~FROM2). Return true if A changes. */
1937 bool
1938 bitmap_ior_and_compl_into (bitmap a, const_bitmap from1, const_bitmap from2)
1940 bitmap_head tmp;
1941 bool changed;
1943 bitmap_initialize (&tmp, &bitmap_default_obstack);
1944 bitmap_and_compl (&tmp, from1, from2);
1945 changed = bitmap_ior_into (a, &tmp);
1946 bitmap_clear (&tmp);
1948 return changed;
1951 /* A |= (B & C). Return true if A changes. */
1953 bool
1954 bitmap_ior_and_into (bitmap a, const_bitmap b, const_bitmap c)
1956 bitmap_element *a_elt = a->first;
1957 const bitmap_element *b_elt = b->first;
1958 const bitmap_element *c_elt = c->first;
1959 bitmap_element and_elt;
1960 bitmap_element *a_prev = NULL;
1961 bitmap_element **a_prev_pnext = &a->first;
1962 bool changed = false;
1963 unsigned ix;
1965 if (b == c)
1966 return bitmap_ior_into (a, b);
1967 if (bitmap_empty_p (b) || bitmap_empty_p (c))
1968 return false;
1970 and_elt.indx = -1;
1971 while (b_elt && c_elt)
1973 BITMAP_WORD overall;
1975 /* Find a common item of B and C. */
1976 while (b_elt->indx != c_elt->indx)
1978 if (b_elt->indx < c_elt->indx)
1980 b_elt = b_elt->next;
1981 if (!b_elt)
1982 goto done;
1984 else
1986 c_elt = c_elt->next;
1987 if (!c_elt)
1988 goto done;
1992 overall = 0;
1993 and_elt.indx = b_elt->indx;
1994 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1996 and_elt.bits[ix] = b_elt->bits[ix] & c_elt->bits[ix];
1997 overall |= and_elt.bits[ix];
2000 b_elt = b_elt->next;
2001 c_elt = c_elt->next;
2002 if (!overall)
2003 continue;
2005 /* Now find a place to insert AND_ELT. */
2008 ix = a_elt ? a_elt->indx : and_elt.indx;
2009 if (ix == and_elt.indx)
2010 changed = bitmap_elt_ior (a, a_elt, a_prev, a_elt, &and_elt, changed);
2011 else if (ix > and_elt.indx)
2012 changed = bitmap_elt_copy (a, NULL, a_prev, &and_elt, changed);
2014 a_prev = *a_prev_pnext;
2015 a_prev_pnext = &a_prev->next;
2016 a_elt = *a_prev_pnext;
2018 /* If A lagged behind B/C, we advanced it so loop once more. */
2020 while (ix < and_elt.indx);
2023 done:
2024 gcc_checking_assert (!a->current == !a->first);
2025 if (a->current)
2026 a->indx = a->current->indx;
2027 return changed;
2030 /* Compute hash of bitmap (for purposes of hashing). */
2031 hashval_t
2032 bitmap_hash (const_bitmap head)
2034 const bitmap_element *ptr;
2035 BITMAP_WORD hash = 0;
2036 int ix;
2038 for (ptr = head->first; ptr; ptr = ptr->next)
2040 hash ^= ptr->indx;
2041 for (ix = 0; ix != BITMAP_ELEMENT_WORDS; ix++)
2042 hash ^= ptr->bits[ix];
2044 return (hashval_t)hash;
2048 /* Debugging function to print out the contents of a bitmap. */
2050 DEBUG_FUNCTION void
2051 debug_bitmap_file (FILE *file, const_bitmap head)
2053 const bitmap_element *ptr;
2055 fprintf (file, "\nfirst = " HOST_PTR_PRINTF
2056 " current = " HOST_PTR_PRINTF " indx = %u\n",
2057 (void *) head->first, (void *) head->current, head->indx);
2059 for (ptr = head->first; ptr; ptr = ptr->next)
2061 unsigned int i, j, col = 26;
2063 fprintf (file, "\t" HOST_PTR_PRINTF " next = " HOST_PTR_PRINTF
2064 " prev = " HOST_PTR_PRINTF " indx = %u\n\t\tbits = {",
2065 (const void*) ptr, (const void*) ptr->next,
2066 (const void*) ptr->prev, ptr->indx);
2068 for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
2069 for (j = 0; j < BITMAP_WORD_BITS; j++)
2070 if ((ptr->bits[i] >> j) & 1)
2072 if (col > 70)
2074 fprintf (file, "\n\t\t\t");
2075 col = 24;
2078 fprintf (file, " %u", (ptr->indx * BITMAP_ELEMENT_ALL_BITS
2079 + i * BITMAP_WORD_BITS + j));
2080 col += 4;
2083 fprintf (file, " }\n");
2087 /* Function to be called from the debugger to print the contents
2088 of a bitmap. */
2090 DEBUG_FUNCTION void
2091 debug_bitmap (const_bitmap head)
2093 debug_bitmap_file (stdout, head);
2096 /* Function to print out the contents of a bitmap. Unlike debug_bitmap_file,
2097 it does not print anything but the bits. */
2099 DEBUG_FUNCTION void
2100 bitmap_print (FILE *file, const_bitmap head, const char *prefix, const char *suffix)
2102 const char *comma = "";
2103 unsigned i;
2104 bitmap_iterator bi;
2106 fputs (prefix, file);
2107 EXECUTE_IF_SET_IN_BITMAP (head, 0, i, bi)
2109 fprintf (file, "%s%d", comma, i);
2110 comma = ", ";
2112 fputs (suffix, file);
2116 /* Used to accumulate statistics about bitmap sizes. */
2117 struct output_info
2119 HOST_WIDEST_INT size;
2120 int count;
2123 /* Called via htab_traverse. Output bitmap descriptor pointed out by SLOT
2124 and update statistics. */
2125 static int
2126 print_statistics (void **slot, void *b)
2128 struct bitmap_descriptor *d = (struct bitmap_descriptor *) *slot;
2129 struct output_info *i = (struct output_info *) b;
2130 char s[4096];
2132 if (d->allocated)
2134 const char *s1 = d->file;
2135 const char *s2;
2136 while ((s2 = strstr (s1, "gcc/")))
2137 s1 = s2 + 4;
2138 sprintf (s, "%s:%i (%s)", s1, d->line, d->function);
2139 s[41] = 0;
2140 fprintf (stderr, "%-41s %8d %15"HOST_WIDEST_INT_PRINT"d %15"
2141 HOST_WIDEST_INT_PRINT"d %15"HOST_WIDEST_INT_PRINT"d %10d %10d\n",
2142 s, d->created, d->allocated, d->peak, d->current, d->nsearches,
2143 d->search_iter);
2144 i->size += d->allocated;
2145 i->count += d->created;
2147 return 1;
2150 /* Output per-bitmap memory usage statistics. */
2151 void
2152 dump_bitmap_statistics (void)
2154 struct output_info info;
2156 if (! GATHER_STATISTICS)
2157 return;
2159 if (!bitmap_desc_hash)
2160 return;
2162 fprintf (stderr, "\nBitmap Overall "
2163 " Allocated Peak Leak searched "
2164 " search itr\n");
2165 fprintf (stderr, "---------------------------------------------------------------------------------\n");
2166 info.count = 0;
2167 info.size = 0;
2168 htab_traverse (bitmap_desc_hash, print_statistics, &info);
2169 fprintf (stderr, "---------------------------------------------------------------------------------\n");
2170 fprintf (stderr, "%-40s %9d %15"HOST_WIDEST_INT_PRINT"d\n",
2171 "Total", info.count, info.size);
2172 fprintf (stderr, "---------------------------------------------------------------------------------\n");
2175 #include "gt-bitmap.h"