* config/sh/sh.h: Delete dead GO_IF_LEGITIMATE_INDEX macro.
[official-gcc.git] / gcc / bitmap.c
blob8d7f1b2aa9536e52528449f0211c08fc84b75f1d
1 /* Functions to support general ended bitmaps.
2 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2003, 2004, 2005,
3 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "obstack.h"
25 #include "ggc.h"
26 #include "bitmap.h"
27 #include "hashtab.h"
29 #ifdef GATHER_STATISTICS
31 /* Store information about each particular bitmap. */
32 struct bitmap_descriptor
34 const char *function;
35 const char *file;
36 int line;
37 int created;
38 HOST_WIDEST_INT allocated;
39 HOST_WIDEST_INT peak;
40 HOST_WIDEST_INT current;
41 int nsearches;
42 int search_iter;
45 /* Hashtable mapping bitmap names to descriptors. */
46 static htab_t bitmap_desc_hash;
48 /* Hashtable helpers. */
49 static hashval_t
50 hash_descriptor (const void *p)
52 const struct bitmap_descriptor *const d =
53 (const struct bitmap_descriptor *) p;
54 return htab_hash_pointer (d->file) + d->line;
56 struct loc
58 const char *file;
59 const char *function;
60 int line;
62 static int
63 eq_descriptor (const void *p1, const void *p2)
65 const struct bitmap_descriptor *const d =
66 (const struct bitmap_descriptor *) p1;
67 const struct loc *const l = (const struct loc *) p2;
68 return d->file == l->file && d->function == l->function && d->line == l->line;
71 /* For given file and line, return descriptor, create new if needed. */
72 static struct bitmap_descriptor *
73 bitmap_descriptor (const char *file, const char *function, int line)
75 struct bitmap_descriptor **slot;
76 struct loc loc;
78 loc.file = file;
79 loc.function = function;
80 loc.line = line;
82 if (!bitmap_desc_hash)
83 bitmap_desc_hash = htab_create (10, hash_descriptor, eq_descriptor, NULL);
85 slot = (struct bitmap_descriptor **)
86 htab_find_slot_with_hash (bitmap_desc_hash, &loc,
87 htab_hash_pointer (file) + line,
88 INSERT);
89 if (*slot)
90 return *slot;
91 *slot = XCNEW (struct bitmap_descriptor);
92 (*slot)->file = file;
93 (*slot)->function = function;
94 (*slot)->line = line;
95 return *slot;
98 /* Register new bitmap. */
99 void
100 bitmap_register (bitmap b MEM_STAT_DECL)
102 b->desc = bitmap_descriptor (_loc_name, _loc_function, _loc_line);
103 b->desc->created++;
106 /* Account the overhead. */
107 static void
108 register_overhead (bitmap b, int amount)
110 b->desc->current += amount;
111 if (amount > 0)
112 b->desc->allocated += amount;
113 gcc_assert (b->desc->current >= 0);
114 if (b->desc->peak < b->desc->current)
115 b->desc->peak = b->desc->current;
117 #endif
119 /* Global data */
120 bitmap_element bitmap_zero_bits; /* An element of all zero bits. */
121 bitmap_obstack bitmap_default_obstack; /* The default bitmap obstack. */
122 static int bitmap_default_obstack_depth;
123 static GTY((deletable)) bitmap_element *bitmap_ggc_free; /* Freelist of
124 GC'd elements. */
126 static void bitmap_elem_to_freelist (bitmap, bitmap_element *);
127 static void bitmap_element_free (bitmap, bitmap_element *);
128 static bitmap_element *bitmap_element_allocate (bitmap);
129 static int bitmap_element_zerop (const bitmap_element *);
130 static void bitmap_element_link (bitmap, bitmap_element *);
131 static bitmap_element *bitmap_elt_insert_after (bitmap, bitmap_element *, unsigned int);
132 static void bitmap_elt_clear_from (bitmap, bitmap_element *);
133 static bitmap_element *bitmap_find_bit (bitmap, unsigned int);
136 /* Add ELEM to the appropriate freelist. */
137 static inline void
138 bitmap_elem_to_freelist (bitmap head, bitmap_element *elt)
140 bitmap_obstack *bit_obstack = head->obstack;
142 elt->next = NULL;
143 if (bit_obstack)
145 elt->prev = bit_obstack->elements;
146 bit_obstack->elements = elt;
148 else
150 elt->prev = bitmap_ggc_free;
151 bitmap_ggc_free = elt;
155 /* Free a bitmap element. Since these are allocated off the
156 bitmap_obstack, "free" actually means "put onto the freelist". */
158 static inline void
159 bitmap_element_free (bitmap head, bitmap_element *elt)
161 bitmap_element *next = elt->next;
162 bitmap_element *prev = elt->prev;
164 if (prev)
165 prev->next = next;
167 if (next)
168 next->prev = prev;
170 if (head->first == elt)
171 head->first = next;
173 /* Since the first thing we try is to insert before current,
174 make current the next entry in preference to the previous. */
175 if (head->current == elt)
177 head->current = next != 0 ? next : prev;
178 if (head->current)
179 head->indx = head->current->indx;
180 else
181 head->indx = 0;
183 #ifdef GATHER_STATISTICS
184 register_overhead (head, -((int)sizeof (bitmap_element)));
185 #endif
186 bitmap_elem_to_freelist (head, elt);
189 /* Allocate a bitmap element. The bits are cleared, but nothing else is. */
191 static inline bitmap_element *
192 bitmap_element_allocate (bitmap head)
194 bitmap_element *element;
195 bitmap_obstack *bit_obstack = head->obstack;
197 if (bit_obstack)
199 element = bit_obstack->elements;
201 if (element)
202 /* Use up the inner list first before looking at the next
203 element of the outer list. */
204 if (element->next)
206 bit_obstack->elements = element->next;
207 bit_obstack->elements->prev = element->prev;
209 else
210 /* Inner list was just a singleton. */
211 bit_obstack->elements = element->prev;
212 else
213 element = XOBNEW (&bit_obstack->obstack, bitmap_element);
215 else
217 element = bitmap_ggc_free;
218 if (element)
219 /* Use up the inner list first before looking at the next
220 element of the outer list. */
221 if (element->next)
223 bitmap_ggc_free = element->next;
224 bitmap_ggc_free->prev = element->prev;
226 else
227 /* Inner list was just a singleton. */
228 bitmap_ggc_free = element->prev;
229 else
230 element = ggc_alloc_bitmap_element_def ();
233 #ifdef GATHER_STATISTICS
234 register_overhead (head, sizeof (bitmap_element));
235 #endif
236 memset (element->bits, 0, sizeof (element->bits));
238 return element;
241 /* Remove ELT and all following elements from bitmap HEAD. */
243 void
244 bitmap_elt_clear_from (bitmap head, bitmap_element *elt)
246 bitmap_element *prev;
247 bitmap_obstack *bit_obstack = head->obstack;
248 #ifdef GATHER_STATISTICS
249 int n;
250 #endif
252 if (!elt) return;
253 #ifdef GATHER_STATISTICS
254 n = 0;
255 for (prev = elt; prev; prev = prev->next)
256 n++;
257 register_overhead (head, -sizeof (bitmap_element) * n);
258 #endif
260 prev = elt->prev;
261 if (prev)
263 prev->next = NULL;
264 if (head->current->indx > prev->indx)
266 head->current = prev;
267 head->indx = prev->indx;
270 else
272 head->first = NULL;
273 head->current = NULL;
274 head->indx = 0;
277 /* Put the entire list onto the free list in one operation. */
278 if (bit_obstack)
280 elt->prev = bit_obstack->elements;
281 bit_obstack->elements = elt;
283 else
285 elt->prev = bitmap_ggc_free;
286 bitmap_ggc_free = elt;
290 /* Clear a bitmap by freeing the linked list. */
292 void
293 bitmap_clear (bitmap head)
295 if (head->first)
296 bitmap_elt_clear_from (head, head->first);
299 /* Initialize a bitmap obstack. If BIT_OBSTACK is NULL, initialize
300 the default bitmap obstack. */
302 void
303 bitmap_obstack_initialize (bitmap_obstack *bit_obstack)
305 if (!bit_obstack)
307 if (bitmap_default_obstack_depth++)
308 return;
309 bit_obstack = &bitmap_default_obstack;
312 #if !defined(__GNUC__) || (__GNUC__ < 2)
313 #define __alignof__(type) 0
314 #endif
316 bit_obstack->elements = NULL;
317 bit_obstack->heads = NULL;
318 obstack_specify_allocation (&bit_obstack->obstack, OBSTACK_CHUNK_SIZE,
319 __alignof__ (bitmap_element),
320 obstack_chunk_alloc,
321 obstack_chunk_free);
324 /* Release the memory from a bitmap obstack. If BIT_OBSTACK is NULL,
325 release the default bitmap obstack. */
327 void
328 bitmap_obstack_release (bitmap_obstack *bit_obstack)
330 if (!bit_obstack)
332 if (--bitmap_default_obstack_depth)
334 gcc_assert (bitmap_default_obstack_depth > 0);
335 return;
337 bit_obstack = &bitmap_default_obstack;
340 bit_obstack->elements = NULL;
341 bit_obstack->heads = NULL;
342 obstack_free (&bit_obstack->obstack, NULL);
345 /* Create a new bitmap on an obstack. If BIT_OBSTACK is NULL, create
346 it on the default bitmap obstack. */
348 bitmap
349 bitmap_obstack_alloc_stat (bitmap_obstack *bit_obstack MEM_STAT_DECL)
351 bitmap map;
353 if (!bit_obstack)
354 bit_obstack = &bitmap_default_obstack;
355 map = bit_obstack->heads;
356 if (map)
357 bit_obstack->heads = (struct bitmap_head_def *) map->first;
358 else
359 map = XOBNEW (&bit_obstack->obstack, bitmap_head);
360 bitmap_initialize_stat (map, bit_obstack PASS_MEM_STAT);
361 #ifdef GATHER_STATISTICS
362 register_overhead (map, sizeof (bitmap_head));
363 #endif
365 return map;
368 /* Create a new GCd bitmap. */
370 bitmap
371 bitmap_gc_alloc_stat (ALONE_MEM_STAT_DECL)
373 bitmap map;
375 map = ggc_alloc_bitmap_head_def ();
376 bitmap_initialize_stat (map, NULL PASS_MEM_STAT);
377 #ifdef GATHER_STATISTICS
378 register_overhead (map, sizeof (bitmap_head));
379 #endif
381 return map;
384 /* Release an obstack allocated bitmap. */
386 void
387 bitmap_obstack_free (bitmap map)
389 if (map)
391 bitmap_clear (map);
392 map->first = (bitmap_element *) map->obstack->heads;
393 #ifdef GATHER_STATISTICS
394 register_overhead (map, -((int)sizeof (bitmap_head)));
395 #endif
396 map->obstack->heads = map;
401 /* Return nonzero if all bits in an element are zero. */
403 static inline int
404 bitmap_element_zerop (const bitmap_element *element)
406 #if BITMAP_ELEMENT_WORDS == 2
407 return (element->bits[0] | element->bits[1]) == 0;
408 #else
409 unsigned i;
411 for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
412 if (element->bits[i] != 0)
413 return 0;
415 return 1;
416 #endif
419 /* Link the bitmap element into the current bitmap linked list. */
421 static inline void
422 bitmap_element_link (bitmap head, bitmap_element *element)
424 unsigned int indx = element->indx;
425 bitmap_element *ptr;
427 /* If this is the first and only element, set it in. */
428 if (head->first == 0)
430 element->next = element->prev = 0;
431 head->first = element;
434 /* If this index is less than that of the current element, it goes someplace
435 before the current element. */
436 else if (indx < head->indx)
438 for (ptr = head->current;
439 ptr->prev != 0 && ptr->prev->indx > indx;
440 ptr = ptr->prev)
443 if (ptr->prev)
444 ptr->prev->next = element;
445 else
446 head->first = element;
448 element->prev = ptr->prev;
449 element->next = ptr;
450 ptr->prev = element;
453 /* Otherwise, it must go someplace after the current element. */
454 else
456 for (ptr = head->current;
457 ptr->next != 0 && ptr->next->indx < indx;
458 ptr = ptr->next)
461 if (ptr->next)
462 ptr->next->prev = element;
464 element->next = ptr->next;
465 element->prev = ptr;
466 ptr->next = element;
469 /* Set up so this is the first element searched. */
470 head->current = element;
471 head->indx = indx;
474 /* Insert a new uninitialized element into bitmap HEAD after element
475 ELT. If ELT is NULL, insert the element at the start. Return the
476 new element. */
478 static bitmap_element *
479 bitmap_elt_insert_after (bitmap head, bitmap_element *elt, unsigned int indx)
481 bitmap_element *node = bitmap_element_allocate (head);
482 node->indx = indx;
484 if (!elt)
486 if (!head->current)
488 head->current = node;
489 head->indx = indx;
491 node->next = head->first;
492 if (node->next)
493 node->next->prev = node;
494 head->first = node;
495 node->prev = NULL;
497 else
499 gcc_checking_assert (head->current);
500 node->next = elt->next;
501 if (node->next)
502 node->next->prev = node;
503 elt->next = node;
504 node->prev = elt;
506 return node;
509 /* Copy a bitmap to another bitmap. */
511 void
512 bitmap_copy (bitmap to, const_bitmap from)
514 const bitmap_element *from_ptr;
515 bitmap_element *to_ptr = 0;
517 bitmap_clear (to);
519 /* Copy elements in forward direction one at a time. */
520 for (from_ptr = from->first; from_ptr; from_ptr = from_ptr->next)
522 bitmap_element *to_elt = bitmap_element_allocate (to);
524 to_elt->indx = from_ptr->indx;
525 memcpy (to_elt->bits, from_ptr->bits, sizeof (to_elt->bits));
527 /* Here we have a special case of bitmap_element_link, for the case
528 where we know the links are being entered in sequence. */
529 if (to_ptr == 0)
531 to->first = to->current = to_elt;
532 to->indx = from_ptr->indx;
533 to_elt->next = to_elt->prev = 0;
535 else
537 to_elt->prev = to_ptr;
538 to_elt->next = 0;
539 to_ptr->next = to_elt;
542 to_ptr = to_elt;
546 /* Find a bitmap element that would hold a bitmap's bit.
547 Update the `current' field even if we can't find an element that
548 would hold the bitmap's bit to make eventual allocation
549 faster. */
551 static inline bitmap_element *
552 bitmap_find_bit (bitmap head, unsigned int bit)
554 bitmap_element *element;
555 unsigned int indx = bit / BITMAP_ELEMENT_ALL_BITS;
557 if (head->current == 0
558 || head->indx == indx)
559 return head->current;
560 #ifdef GATHER_STATISTICS
561 head->desc->nsearches++;
562 #endif
564 if (head->indx < indx)
565 /* INDX is beyond head->indx. Search from head->current
566 forward. */
567 for (element = head->current;
568 element->next != 0 && element->indx < indx;
569 element = element->next)
570 #ifdef GATHER_STATISTICS
571 head->desc->search_iter++;
572 #else
574 #endif
576 else if (head->indx / 2 < indx)
577 /* INDX is less than head->indx and closer to head->indx than to
578 0. Search from head->current backward. */
579 for (element = head->current;
580 element->prev != 0 && element->indx > indx;
581 element = element->prev)
582 #ifdef GATHER_STATISTICS
583 head->desc->search_iter++;
584 #else
586 #endif
588 else
589 /* INDX is less than head->indx and closer to 0 than to
590 head->indx. Search from head->first forward. */
591 for (element = head->first;
592 element->next != 0 && element->indx < indx;
593 element = element->next)
594 #ifdef GATHER_STATISTICS
595 head->desc->search_iter++;
596 #else
598 #endif
600 /* `element' is the nearest to the one we want. If it's not the one we
601 want, the one we want doesn't exist. */
602 head->current = element;
603 head->indx = element->indx;
604 if (element != 0 && element->indx != indx)
605 element = 0;
607 return element;
610 /* Clear a single bit in a bitmap. Return true if the bit changed. */
612 bool
613 bitmap_clear_bit (bitmap head, int bit)
615 bitmap_element *const ptr = bitmap_find_bit (head, bit);
617 if (ptr != 0)
619 unsigned bit_num = bit % BITMAP_WORD_BITS;
620 unsigned word_num = bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
621 BITMAP_WORD bit_val = ((BITMAP_WORD) 1) << bit_num;
622 bool res = (ptr->bits[word_num] & bit_val) != 0;
623 if (res)
625 ptr->bits[word_num] &= ~bit_val;
626 /* If we cleared the entire word, free up the element. */
627 if (!ptr->bits[word_num]
628 && bitmap_element_zerop (ptr))
629 bitmap_element_free (head, ptr);
632 return res;
635 return false;
638 /* Set a single bit in a bitmap. Return true if the bit changed. */
640 bool
641 bitmap_set_bit (bitmap head, int bit)
643 bitmap_element *ptr = bitmap_find_bit (head, bit);
644 unsigned word_num = bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
645 unsigned bit_num = bit % BITMAP_WORD_BITS;
646 BITMAP_WORD bit_val = ((BITMAP_WORD) 1) << bit_num;
648 if (ptr == 0)
650 ptr = bitmap_element_allocate (head);
651 ptr->indx = bit / BITMAP_ELEMENT_ALL_BITS;
652 ptr->bits[word_num] = bit_val;
653 bitmap_element_link (head, ptr);
654 return true;
656 else
658 bool res = (ptr->bits[word_num] & bit_val) == 0;
659 if (res)
660 ptr->bits[word_num] |= bit_val;
661 return res;
665 /* Return whether a bit is set within a bitmap. */
668 bitmap_bit_p (bitmap head, int bit)
670 bitmap_element *ptr;
671 unsigned bit_num;
672 unsigned word_num;
674 ptr = bitmap_find_bit (head, bit);
675 if (ptr == 0)
676 return 0;
678 bit_num = bit % BITMAP_WORD_BITS;
679 word_num = bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
681 return (ptr->bits[word_num] >> bit_num) & 1;
684 #if GCC_VERSION < 3400
685 /* Table of number of set bits in a character, indexed by value of char. */
686 static const unsigned char popcount_table[] =
688 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,
689 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,
690 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,
691 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,
692 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,
693 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,
694 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,
695 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,
698 static unsigned long
699 bitmap_popcount (BITMAP_WORD a)
701 unsigned long ret = 0;
702 unsigned i;
704 /* Just do this the table way for now */
705 for (i = 0; i < BITMAP_WORD_BITS; i+= 8)
706 ret += popcount_table[(a >> i) & 0xff];
707 return ret;
709 #endif
710 /* Count the number of bits set in the bitmap, and return it. */
712 unsigned long
713 bitmap_count_bits (const_bitmap a)
715 unsigned long count = 0;
716 const bitmap_element *elt;
717 unsigned ix;
719 for (elt = a->first; elt; elt = elt->next)
721 for (ix = 0; ix != BITMAP_ELEMENT_WORDS; ix++)
723 #if GCC_VERSION >= 3400
724 /* Note that popcountl matches BITMAP_WORD in type, so the actual size
725 of BITMAP_WORD is not material. */
726 count += __builtin_popcountl (elt->bits[ix]);
727 #else
728 count += bitmap_popcount (elt->bits[ix]);
729 #endif
732 return count;
735 /* Return true if the bitmap has a single bit set. Otherwise return
736 false. */
738 bool
739 bitmap_single_bit_set_p (const_bitmap a)
741 unsigned long count = 0;
742 const bitmap_element *elt;
743 unsigned ix;
745 if (bitmap_empty_p (a))
746 return false;
748 elt = a->first;
749 /* As there are no completely empty bitmap elements, a second one
750 means we have more than one bit set. */
751 if (elt->next != NULL)
752 return false;
754 for (ix = 0; ix != BITMAP_ELEMENT_WORDS; ix++)
756 #if GCC_VERSION >= 3400
757 /* Note that popcountl matches BITMAP_WORD in type, so the actual size
758 of BITMAP_WORD is not material. */
759 count += __builtin_popcountl (elt->bits[ix]);
760 #else
761 count += bitmap_popcount (elt->bits[ix]);
762 #endif
763 if (count > 1)
764 return false;
767 return count == 1;
771 /* Return the bit number of the first set bit in the bitmap. The
772 bitmap must be non-empty. */
774 unsigned
775 bitmap_first_set_bit (const_bitmap a)
777 const bitmap_element *elt = a->first;
778 unsigned bit_no;
779 BITMAP_WORD word;
780 unsigned ix;
782 gcc_checking_assert (elt);
783 bit_no = elt->indx * BITMAP_ELEMENT_ALL_BITS;
784 for (ix = 0; ix != BITMAP_ELEMENT_WORDS; ix++)
786 word = elt->bits[ix];
787 if (word)
788 goto found_bit;
790 gcc_unreachable ();
791 found_bit:
792 bit_no += ix * BITMAP_WORD_BITS;
794 #if GCC_VERSION >= 3004
795 gcc_assert (sizeof(long) == sizeof (word));
796 bit_no += __builtin_ctzl (word);
797 #else
798 /* Binary search for the first set bit. */
799 #if BITMAP_WORD_BITS > 64
800 #error "Fill out the table."
801 #endif
802 #if BITMAP_WORD_BITS > 32
803 if (!(word & 0xffffffff))
804 word >>= 32, bit_no += 32;
805 #endif
806 if (!(word & 0xffff))
807 word >>= 16, bit_no += 16;
808 if (!(word & 0xff))
809 word >>= 8, bit_no += 8;
810 if (!(word & 0xf))
811 word >>= 4, bit_no += 4;
812 if (!(word & 0x3))
813 word >>= 2, bit_no += 2;
814 if (!(word & 0x1))
815 word >>= 1, bit_no += 1;
817 gcc_checking_assert (word & 1);
818 #endif
819 return bit_no;
822 /* Return the bit number of the first set bit in the bitmap. The
823 bitmap must be non-empty. */
825 unsigned
826 bitmap_last_set_bit (const_bitmap a)
828 const bitmap_element *elt = a->current ? a->current : a->first;
829 unsigned bit_no;
830 BITMAP_WORD word;
831 int ix;
833 gcc_checking_assert (elt);
834 while (elt->next)
835 elt = elt->next;
836 bit_no = elt->indx * BITMAP_ELEMENT_ALL_BITS;
837 for (ix = BITMAP_ELEMENT_WORDS - 1; ix >= 0; ix--)
839 word = elt->bits[ix];
840 if (word)
841 goto found_bit;
843 gcc_unreachable ();
844 found_bit:
845 bit_no += ix * BITMAP_WORD_BITS;
847 /* Binary search for the last set bit. */
848 #if GCC_VERSION >= 3004
849 gcc_assert (sizeof(long) == sizeof (word));
850 bit_no += sizeof (long) * 8 - __builtin_ctzl (word);
851 #else
852 #if BITMAP_WORD_BITS > 64
853 #error "Fill out the table."
854 #endif
855 #if BITMAP_WORD_BITS > 32
856 if ((word & 0xffffffff00000000))
857 word >>= 32, bit_no += 32;
858 #endif
859 if (word & 0xffff0000)
860 word >>= 16, bit_no += 16;
861 if (!(word & 0xff00))
862 word >>= 8, bit_no += 8;
863 if (!(word & 0xf0))
864 word >>= 4, bit_no += 4;
865 if (!(word & 12))
866 word >>= 2, bit_no += 2;
867 if (!(word & 2))
868 word >>= 1, bit_no += 1;
869 #endif
871 gcc_checking_assert (word & 1);
872 return bit_no;
876 /* DST = A & B. */
878 void
879 bitmap_and (bitmap dst, const_bitmap a, const_bitmap b)
881 bitmap_element *dst_elt = dst->first;
882 const bitmap_element *a_elt = a->first;
883 const bitmap_element *b_elt = b->first;
884 bitmap_element *dst_prev = NULL;
886 gcc_assert (dst != a && dst != b);
888 if (a == b)
890 bitmap_copy (dst, a);
891 return;
894 while (a_elt && b_elt)
896 if (a_elt->indx < b_elt->indx)
897 a_elt = a_elt->next;
898 else if (b_elt->indx < a_elt->indx)
899 b_elt = b_elt->next;
900 else
902 /* Matching elts, generate A & B. */
903 unsigned ix;
904 BITMAP_WORD ior = 0;
906 if (!dst_elt)
907 dst_elt = bitmap_elt_insert_after (dst, dst_prev, a_elt->indx);
908 else
909 dst_elt->indx = a_elt->indx;
910 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
912 BITMAP_WORD r = a_elt->bits[ix] & b_elt->bits[ix];
914 dst_elt->bits[ix] = r;
915 ior |= r;
917 if (ior)
919 dst_prev = dst_elt;
920 dst_elt = dst_elt->next;
922 a_elt = a_elt->next;
923 b_elt = b_elt->next;
926 /* Ensure that dst->current is valid. */
927 dst->current = dst->first;
928 bitmap_elt_clear_from (dst, dst_elt);
929 gcc_checking_assert (!dst->current == !dst->first);
930 if (dst->current)
931 dst->indx = dst->current->indx;
934 /* A &= B. */
936 void
937 bitmap_and_into (bitmap a, const_bitmap b)
939 bitmap_element *a_elt = a->first;
940 const bitmap_element *b_elt = b->first;
941 bitmap_element *next;
943 if (a == b)
944 return;
946 while (a_elt && b_elt)
948 if (a_elt->indx < b_elt->indx)
950 next = a_elt->next;
951 bitmap_element_free (a, a_elt);
952 a_elt = next;
954 else if (b_elt->indx < a_elt->indx)
955 b_elt = b_elt->next;
956 else
958 /* Matching elts, generate A &= B. */
959 unsigned ix;
960 BITMAP_WORD ior = 0;
962 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
964 BITMAP_WORD r = a_elt->bits[ix] & b_elt->bits[ix];
966 a_elt->bits[ix] = r;
967 ior |= r;
969 next = a_elt->next;
970 if (!ior)
971 bitmap_element_free (a, a_elt);
972 a_elt = next;
973 b_elt = b_elt->next;
976 bitmap_elt_clear_from (a, a_elt);
977 gcc_checking_assert (!a->current == !a->first
978 && (!a->current || a->indx == a->current->indx));
982 /* Insert an element equal to SRC_ELT after DST_PREV, overwriting DST_ELT
983 if non-NULL. CHANGED is true if the destination bitmap had already been
984 changed; the new value of CHANGED is returned. */
986 static inline bool
987 bitmap_elt_copy (bitmap dst, bitmap_element *dst_elt, bitmap_element *dst_prev,
988 const bitmap_element *src_elt, bool changed)
990 if (!changed && dst_elt && dst_elt->indx == src_elt->indx)
992 unsigned ix;
994 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
995 if (src_elt->bits[ix] != dst_elt->bits[ix])
997 dst_elt->bits[ix] = src_elt->bits[ix];
998 changed = true;
1001 else
1003 changed = true;
1004 if (!dst_elt)
1005 dst_elt = bitmap_elt_insert_after (dst, dst_prev, src_elt->indx);
1006 else
1007 dst_elt->indx = src_elt->indx;
1008 memcpy (dst_elt->bits, src_elt->bits, sizeof (dst_elt->bits));
1010 return changed;
1015 /* DST = A & ~B */
1017 bool
1018 bitmap_and_compl (bitmap dst, const_bitmap a, const_bitmap b)
1020 bitmap_element *dst_elt = dst->first;
1021 const bitmap_element *a_elt = a->first;
1022 const bitmap_element *b_elt = b->first;
1023 bitmap_element *dst_prev = NULL;
1024 bitmap_element **dst_prev_pnext = &dst->first;
1025 bool changed = false;
1027 gcc_assert (dst != a && dst != b);
1029 if (a == b)
1031 changed = !bitmap_empty_p (dst);
1032 bitmap_clear (dst);
1033 return changed;
1036 while (a_elt)
1038 while (b_elt && b_elt->indx < a_elt->indx)
1039 b_elt = b_elt->next;
1041 if (!b_elt || b_elt->indx > a_elt->indx)
1043 changed = bitmap_elt_copy (dst, dst_elt, dst_prev, a_elt, changed);
1044 dst_prev = *dst_prev_pnext;
1045 dst_prev_pnext = &dst_prev->next;
1046 dst_elt = *dst_prev_pnext;
1047 a_elt = a_elt->next;
1050 else
1052 /* Matching elts, generate A & ~B. */
1053 unsigned ix;
1054 BITMAP_WORD ior = 0;
1056 if (!changed && dst_elt && dst_elt->indx == a_elt->indx)
1058 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1060 BITMAP_WORD r = a_elt->bits[ix] & ~b_elt->bits[ix];
1062 if (dst_elt->bits[ix] != r)
1064 changed = true;
1065 dst_elt->bits[ix] = r;
1067 ior |= r;
1070 else
1072 bool new_element;
1073 if (!dst_elt || dst_elt->indx > a_elt->indx)
1075 dst_elt = bitmap_elt_insert_after (dst, dst_prev, a_elt->indx);
1076 new_element = true;
1078 else
1080 dst_elt->indx = a_elt->indx;
1081 new_element = false;
1084 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1086 BITMAP_WORD r = a_elt->bits[ix] & ~b_elt->bits[ix];
1088 dst_elt->bits[ix] = r;
1089 ior |= r;
1092 if (ior)
1093 changed = true;
1094 else
1096 changed |= !new_element;
1097 bitmap_element_free (dst, dst_elt);
1098 dst_elt = *dst_prev_pnext;
1102 if (ior)
1104 dst_prev = *dst_prev_pnext;
1105 dst_prev_pnext = &dst_prev->next;
1106 dst_elt = *dst_prev_pnext;
1108 a_elt = a_elt->next;
1109 b_elt = b_elt->next;
1113 /* Ensure that dst->current is valid. */
1114 dst->current = dst->first;
1116 if (dst_elt)
1118 changed = true;
1119 bitmap_elt_clear_from (dst, dst_elt);
1121 gcc_checking_assert (!dst->current == !dst->first);
1122 if (dst->current)
1123 dst->indx = dst->current->indx;
1125 return changed;
1128 /* A &= ~B. Returns true if A changes */
1130 bool
1131 bitmap_and_compl_into (bitmap a, const_bitmap b)
1133 bitmap_element *a_elt = a->first;
1134 const bitmap_element *b_elt = b->first;
1135 bitmap_element *next;
1136 BITMAP_WORD changed = 0;
1138 if (a == b)
1140 if (bitmap_empty_p (a))
1141 return false;
1142 else
1144 bitmap_clear (a);
1145 return true;
1149 while (a_elt && b_elt)
1151 if (a_elt->indx < b_elt->indx)
1152 a_elt = a_elt->next;
1153 else if (b_elt->indx < a_elt->indx)
1154 b_elt = b_elt->next;
1155 else
1157 /* Matching elts, generate A &= ~B. */
1158 unsigned ix;
1159 BITMAP_WORD ior = 0;
1161 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1163 BITMAP_WORD cleared = a_elt->bits[ix] & b_elt->bits[ix];
1164 BITMAP_WORD r = a_elt->bits[ix] ^ cleared;
1166 a_elt->bits[ix] = r;
1167 changed |= cleared;
1168 ior |= r;
1170 next = a_elt->next;
1171 if (!ior)
1172 bitmap_element_free (a, a_elt);
1173 a_elt = next;
1174 b_elt = b_elt->next;
1177 gcc_checking_assert (!a->current == !a->first
1178 && (!a->current || a->indx == a->current->indx));
1179 return changed != 0;
1182 /* Set COUNT bits from START in HEAD. */
1183 void
1184 bitmap_set_range (bitmap head, unsigned int start, unsigned int count)
1186 unsigned int first_index, end_bit_plus1, last_index;
1187 bitmap_element *elt, *elt_prev;
1188 unsigned int i;
1190 if (!count)
1191 return;
1193 first_index = start / BITMAP_ELEMENT_ALL_BITS;
1194 end_bit_plus1 = start + count;
1195 last_index = (end_bit_plus1 - 1) / BITMAP_ELEMENT_ALL_BITS;
1196 elt = bitmap_find_bit (head, start);
1198 /* If bitmap_find_bit returns zero, the current is the closest block
1199 to the result. Otherwise, just use bitmap_element_allocate to
1200 ensure ELT is set; in the loop below, ELT == NULL means "insert
1201 at the end of the bitmap". */
1202 if (!elt)
1204 elt = bitmap_element_allocate (head);
1205 elt->indx = first_index;
1206 bitmap_element_link (head, elt);
1209 gcc_checking_assert (elt->indx == first_index);
1210 elt_prev = elt->prev;
1211 for (i = first_index; i <= last_index; i++)
1213 unsigned elt_start_bit = i * BITMAP_ELEMENT_ALL_BITS;
1214 unsigned elt_end_bit_plus1 = elt_start_bit + BITMAP_ELEMENT_ALL_BITS;
1216 unsigned int first_word_to_mod;
1217 BITMAP_WORD first_mask;
1218 unsigned int last_word_to_mod;
1219 BITMAP_WORD last_mask;
1220 unsigned int ix;
1222 if (!elt || elt->indx != i)
1223 elt = bitmap_elt_insert_after (head, elt_prev, i);
1225 if (elt_start_bit <= start)
1227 /* The first bit to turn on is somewhere inside this
1228 elt. */
1229 first_word_to_mod = (start - elt_start_bit) / BITMAP_WORD_BITS;
1231 /* This mask should have 1s in all bits >= start position. */
1232 first_mask =
1233 (((BITMAP_WORD) 1) << ((start % BITMAP_WORD_BITS))) - 1;
1234 first_mask = ~first_mask;
1236 else
1238 /* The first bit to turn on is below this start of this elt. */
1239 first_word_to_mod = 0;
1240 first_mask = ~(BITMAP_WORD) 0;
1243 if (elt_end_bit_plus1 <= end_bit_plus1)
1245 /* The last bit to turn on is beyond this elt. */
1246 last_word_to_mod = BITMAP_ELEMENT_WORDS - 1;
1247 last_mask = ~(BITMAP_WORD) 0;
1249 else
1251 /* The last bit to turn on is inside to this elt. */
1252 last_word_to_mod =
1253 (end_bit_plus1 - elt_start_bit) / BITMAP_WORD_BITS;
1255 /* The last mask should have 1s below the end bit. */
1256 last_mask =
1257 (((BITMAP_WORD) 1) << ((end_bit_plus1 % BITMAP_WORD_BITS))) - 1;
1260 if (first_word_to_mod == last_word_to_mod)
1262 BITMAP_WORD mask = first_mask & last_mask;
1263 elt->bits[first_word_to_mod] |= mask;
1265 else
1267 elt->bits[first_word_to_mod] |= first_mask;
1268 if (BITMAP_ELEMENT_WORDS > 2)
1269 for (ix = first_word_to_mod + 1; ix < last_word_to_mod; ix++)
1270 elt->bits[ix] = ~(BITMAP_WORD) 0;
1271 elt->bits[last_word_to_mod] |= last_mask;
1274 elt_prev = elt;
1275 elt = elt->next;
1278 head->current = elt ? elt : elt_prev;
1279 head->indx = head->current->indx;
1282 /* Clear COUNT bits from START in HEAD. */
1283 void
1284 bitmap_clear_range (bitmap head, unsigned int start, unsigned int count)
1286 unsigned int first_index, end_bit_plus1, last_index;
1287 bitmap_element *elt;
1289 if (!count)
1290 return;
1292 first_index = start / BITMAP_ELEMENT_ALL_BITS;
1293 end_bit_plus1 = start + count;
1294 last_index = (end_bit_plus1 - 1) / BITMAP_ELEMENT_ALL_BITS;
1295 elt = bitmap_find_bit (head, start);
1297 /* If bitmap_find_bit returns zero, the current is the closest block
1298 to the result. If the current is less than first index, find the
1299 next one. Otherwise, just set elt to be current. */
1300 if (!elt)
1302 if (head->current)
1304 if (head->indx < first_index)
1306 elt = head->current->next;
1307 if (!elt)
1308 return;
1310 else
1311 elt = head->current;
1313 else
1314 return;
1317 while (elt && (elt->indx <= last_index))
1319 bitmap_element * next_elt = elt->next;
1320 unsigned elt_start_bit = (elt->indx) * BITMAP_ELEMENT_ALL_BITS;
1321 unsigned elt_end_bit_plus1 = elt_start_bit + BITMAP_ELEMENT_ALL_BITS;
1324 if (elt_start_bit >= start && elt_end_bit_plus1 <= end_bit_plus1)
1325 /* Get rid of the entire elt and go to the next one. */
1326 bitmap_element_free (head, elt);
1327 else
1329 /* Going to have to knock out some bits in this elt. */
1330 unsigned int first_word_to_mod;
1331 BITMAP_WORD first_mask;
1332 unsigned int last_word_to_mod;
1333 BITMAP_WORD last_mask;
1334 unsigned int i;
1335 bool clear = true;
1337 if (elt_start_bit <= start)
1339 /* The first bit to turn off is somewhere inside this
1340 elt. */
1341 first_word_to_mod = (start - elt_start_bit) / BITMAP_WORD_BITS;
1343 /* This mask should have 1s in all bits >= start position. */
1344 first_mask =
1345 (((BITMAP_WORD) 1) << ((start % BITMAP_WORD_BITS))) - 1;
1346 first_mask = ~first_mask;
1348 else
1350 /* The first bit to turn off is below this start of this elt. */
1351 first_word_to_mod = 0;
1352 first_mask = 0;
1353 first_mask = ~first_mask;
1356 if (elt_end_bit_plus1 <= end_bit_plus1)
1358 /* The last bit to turn off is beyond this elt. */
1359 last_word_to_mod = BITMAP_ELEMENT_WORDS - 1;
1360 last_mask = 0;
1361 last_mask = ~last_mask;
1363 else
1365 /* The last bit to turn off is inside to this elt. */
1366 last_word_to_mod =
1367 (end_bit_plus1 - elt_start_bit) / BITMAP_WORD_BITS;
1369 /* The last mask should have 1s below the end bit. */
1370 last_mask =
1371 (((BITMAP_WORD) 1) << (((end_bit_plus1) % BITMAP_WORD_BITS))) - 1;
1375 if (first_word_to_mod == last_word_to_mod)
1377 BITMAP_WORD mask = first_mask & last_mask;
1378 elt->bits[first_word_to_mod] &= ~mask;
1380 else
1382 elt->bits[first_word_to_mod] &= ~first_mask;
1383 if (BITMAP_ELEMENT_WORDS > 2)
1384 for (i = first_word_to_mod + 1; i < last_word_to_mod; i++)
1385 elt->bits[i] = 0;
1386 elt->bits[last_word_to_mod] &= ~last_mask;
1388 for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
1389 if (elt->bits[i])
1391 clear = false;
1392 break;
1394 /* Check to see if there are any bits left. */
1395 if (clear)
1396 bitmap_element_free (head, elt);
1398 elt = next_elt;
1401 if (elt)
1403 head->current = elt;
1404 head->indx = head->current->indx;
1408 /* A = ~A & B. */
1410 void
1411 bitmap_compl_and_into (bitmap a, const_bitmap b)
1413 bitmap_element *a_elt = a->first;
1414 const bitmap_element *b_elt = b->first;
1415 bitmap_element *a_prev = NULL;
1416 bitmap_element *next;
1418 gcc_assert (a != b);
1420 if (bitmap_empty_p (a))
1422 bitmap_copy (a, b);
1423 return;
1425 if (bitmap_empty_p (b))
1427 bitmap_clear (a);
1428 return;
1431 while (a_elt || b_elt)
1433 if (!b_elt || (a_elt && a_elt->indx < b_elt->indx))
1435 /* A is before B. Remove A */
1436 next = a_elt->next;
1437 a_prev = a_elt->prev;
1438 bitmap_element_free (a, a_elt);
1439 a_elt = next;
1441 else if (!a_elt || b_elt->indx < a_elt->indx)
1443 /* B is before A. Copy B. */
1444 next = bitmap_elt_insert_after (a, a_prev, b_elt->indx);
1445 memcpy (next->bits, b_elt->bits, sizeof (next->bits));
1446 a_prev = next;
1447 b_elt = b_elt->next;
1449 else
1451 /* Matching elts, generate A = ~A & B. */
1452 unsigned ix;
1453 BITMAP_WORD ior = 0;
1455 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1457 BITMAP_WORD cleared = a_elt->bits[ix] & b_elt->bits[ix];
1458 BITMAP_WORD r = b_elt->bits[ix] ^ cleared;
1460 a_elt->bits[ix] = r;
1461 ior |= r;
1463 next = a_elt->next;
1464 if (!ior)
1465 bitmap_element_free (a, a_elt);
1466 else
1467 a_prev = a_elt;
1468 a_elt = next;
1469 b_elt = b_elt->next;
1472 gcc_checking_assert (!a->current == !a->first
1473 && (!a->current || a->indx == a->current->indx));
1474 return;
1478 /* Insert an element corresponding to A_ELT | B_ELT after DST_PREV,
1479 overwriting DST_ELT if non-NULL. CHANGED is true if the destination bitmap
1480 had already been changed; the new value of CHANGED is returned. */
1482 static inline bool
1483 bitmap_elt_ior (bitmap dst, bitmap_element *dst_elt, bitmap_element *dst_prev,
1484 const bitmap_element *a_elt, const bitmap_element *b_elt,
1485 bool changed)
1487 gcc_assert (a_elt || b_elt);
1489 if (a_elt && b_elt && a_elt->indx == b_elt->indx)
1491 /* Matching elts, generate A | B. */
1492 unsigned ix;
1494 if (!changed && dst_elt && dst_elt->indx == a_elt->indx)
1496 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1498 BITMAP_WORD r = a_elt->bits[ix] | b_elt->bits[ix];
1499 if (r != dst_elt->bits[ix])
1501 dst_elt->bits[ix] = r;
1502 changed = true;
1506 else
1508 changed = true;
1509 if (!dst_elt)
1510 dst_elt = bitmap_elt_insert_after (dst, dst_prev, a_elt->indx);
1511 else
1512 dst_elt->indx = a_elt->indx;
1513 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1515 BITMAP_WORD r = a_elt->bits[ix] | b_elt->bits[ix];
1516 dst_elt->bits[ix] = r;
1520 else
1522 /* Copy a single element. */
1523 const bitmap_element *src;
1525 if (!b_elt || (a_elt && a_elt->indx < b_elt->indx))
1526 src = a_elt;
1527 else
1528 src = b_elt;
1530 gcc_checking_assert (src);
1531 changed = bitmap_elt_copy (dst, dst_elt, dst_prev, src, changed);
1533 return changed;
1537 /* DST = A | B. Return true if DST changes. */
1539 bool
1540 bitmap_ior (bitmap dst, const_bitmap a, const_bitmap b)
1542 bitmap_element *dst_elt = dst->first;
1543 const bitmap_element *a_elt = a->first;
1544 const bitmap_element *b_elt = b->first;
1545 bitmap_element *dst_prev = NULL;
1546 bitmap_element **dst_prev_pnext = &dst->first;
1547 bool changed = false;
1549 gcc_assert (dst != a && dst != b);
1551 while (a_elt || b_elt)
1553 changed = bitmap_elt_ior (dst, dst_elt, dst_prev, a_elt, b_elt, changed);
1555 if (a_elt && b_elt && a_elt->indx == b_elt->indx)
1557 a_elt = a_elt->next;
1558 b_elt = b_elt->next;
1560 else
1562 if (a_elt && (!b_elt || a_elt->indx <= b_elt->indx))
1563 a_elt = a_elt->next;
1564 else if (b_elt && (!a_elt || b_elt->indx <= a_elt->indx))
1565 b_elt = b_elt->next;
1568 dst_prev = *dst_prev_pnext;
1569 dst_prev_pnext = &dst_prev->next;
1570 dst_elt = *dst_prev_pnext;
1573 if (dst_elt)
1575 changed = true;
1576 bitmap_elt_clear_from (dst, dst_elt);
1578 gcc_checking_assert (!dst->current == !dst->first);
1579 if (dst->current)
1580 dst->indx = dst->current->indx;
1581 return changed;
1584 /* A |= B. Return true if A changes. */
1586 bool
1587 bitmap_ior_into (bitmap a, const_bitmap b)
1589 bitmap_element *a_elt = a->first;
1590 const bitmap_element *b_elt = b->first;
1591 bitmap_element *a_prev = NULL;
1592 bitmap_element **a_prev_pnext = &a->first;
1593 bool changed = false;
1595 if (a == b)
1596 return false;
1598 while (b_elt)
1600 /* If A lags behind B, just advance it. */
1601 if (!a_elt || a_elt->indx == b_elt->indx)
1603 changed = bitmap_elt_ior (a, a_elt, a_prev, a_elt, b_elt, changed);
1604 b_elt = b_elt->next;
1606 else if (a_elt->indx > b_elt->indx)
1608 changed = bitmap_elt_copy (a, NULL, a_prev, b_elt, changed);
1609 b_elt = b_elt->next;
1612 a_prev = *a_prev_pnext;
1613 a_prev_pnext = &a_prev->next;
1614 a_elt = *a_prev_pnext;
1617 gcc_checking_assert (!a->current == !a->first);
1618 if (a->current)
1619 a->indx = a->current->indx;
1620 return changed;
1623 /* DST = A ^ B */
1625 void
1626 bitmap_xor (bitmap dst, const_bitmap a, const_bitmap b)
1628 bitmap_element *dst_elt = dst->first;
1629 const bitmap_element *a_elt = a->first;
1630 const bitmap_element *b_elt = b->first;
1631 bitmap_element *dst_prev = NULL;
1633 gcc_assert (dst != a && dst != b);
1634 if (a == b)
1636 bitmap_clear (dst);
1637 return;
1640 while (a_elt || b_elt)
1642 if (a_elt && b_elt && a_elt->indx == b_elt->indx)
1644 /* Matching elts, generate A ^ B. */
1645 unsigned ix;
1646 BITMAP_WORD ior = 0;
1648 if (!dst_elt)
1649 dst_elt = bitmap_elt_insert_after (dst, dst_prev, a_elt->indx);
1650 else
1651 dst_elt->indx = a_elt->indx;
1652 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1654 BITMAP_WORD r = a_elt->bits[ix] ^ b_elt->bits[ix];
1656 ior |= r;
1657 dst_elt->bits[ix] = r;
1659 a_elt = a_elt->next;
1660 b_elt = b_elt->next;
1661 if (ior)
1663 dst_prev = dst_elt;
1664 dst_elt = dst_elt->next;
1667 else
1669 /* Copy a single element. */
1670 const bitmap_element *src;
1672 if (!b_elt || (a_elt && a_elt->indx < b_elt->indx))
1674 src = a_elt;
1675 a_elt = a_elt->next;
1677 else
1679 src = b_elt;
1680 b_elt = b_elt->next;
1683 if (!dst_elt)
1684 dst_elt = bitmap_elt_insert_after (dst, dst_prev, src->indx);
1685 else
1686 dst_elt->indx = src->indx;
1687 memcpy (dst_elt->bits, src->bits, sizeof (dst_elt->bits));
1688 dst_prev = dst_elt;
1689 dst_elt = dst_elt->next;
1692 /* Ensure that dst->current is valid. */
1693 dst->current = dst->first;
1694 bitmap_elt_clear_from (dst, dst_elt);
1695 gcc_checking_assert (!dst->current == !dst->first);
1696 if (dst->current)
1697 dst->indx = dst->current->indx;
1700 /* A ^= B */
1702 void
1703 bitmap_xor_into (bitmap a, const_bitmap b)
1705 bitmap_element *a_elt = a->first;
1706 const bitmap_element *b_elt = b->first;
1707 bitmap_element *a_prev = NULL;
1709 if (a == b)
1711 bitmap_clear (a);
1712 return;
1715 while (b_elt)
1717 if (!a_elt || b_elt->indx < a_elt->indx)
1719 /* Copy b_elt. */
1720 bitmap_element *dst = bitmap_elt_insert_after (a, a_prev, b_elt->indx);
1721 memcpy (dst->bits, b_elt->bits, sizeof (dst->bits));
1722 a_prev = dst;
1723 b_elt = b_elt->next;
1725 else if (a_elt->indx < b_elt->indx)
1727 a_prev = a_elt;
1728 a_elt = a_elt->next;
1730 else
1732 /* Matching elts, generate A ^= B. */
1733 unsigned ix;
1734 BITMAP_WORD ior = 0;
1735 bitmap_element *next = a_elt->next;
1737 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1739 BITMAP_WORD r = a_elt->bits[ix] ^ b_elt->bits[ix];
1741 ior |= r;
1742 a_elt->bits[ix] = r;
1744 b_elt = b_elt->next;
1745 if (ior)
1746 a_prev = a_elt;
1747 else
1748 bitmap_element_free (a, a_elt);
1749 a_elt = next;
1752 gcc_checking_assert (!a->current == !a->first);
1753 if (a->current)
1754 a->indx = a->current->indx;
1757 /* Return true if two bitmaps are identical.
1758 We do not bother with a check for pointer equality, as that never
1759 occurs in practice. */
1761 bool
1762 bitmap_equal_p (const_bitmap a, const_bitmap b)
1764 const bitmap_element *a_elt;
1765 const bitmap_element *b_elt;
1766 unsigned ix;
1768 for (a_elt = a->first, b_elt = b->first;
1769 a_elt && b_elt;
1770 a_elt = a_elt->next, b_elt = b_elt->next)
1772 if (a_elt->indx != b_elt->indx)
1773 return false;
1774 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1775 if (a_elt->bits[ix] != b_elt->bits[ix])
1776 return false;
1778 return !a_elt && !b_elt;
1781 /* Return true if A AND B is not empty. */
1783 bool
1784 bitmap_intersect_p (const_bitmap a, const_bitmap b)
1786 const bitmap_element *a_elt;
1787 const bitmap_element *b_elt;
1788 unsigned ix;
1790 for (a_elt = a->first, b_elt = b->first;
1791 a_elt && b_elt;)
1793 if (a_elt->indx < b_elt->indx)
1794 a_elt = a_elt->next;
1795 else if (b_elt->indx < a_elt->indx)
1796 b_elt = b_elt->next;
1797 else
1799 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1800 if (a_elt->bits[ix] & b_elt->bits[ix])
1801 return true;
1802 a_elt = a_elt->next;
1803 b_elt = b_elt->next;
1806 return false;
1809 /* Return true if A AND NOT B is not empty. */
1811 bool
1812 bitmap_intersect_compl_p (const_bitmap a, const_bitmap b)
1814 const bitmap_element *a_elt;
1815 const bitmap_element *b_elt;
1816 unsigned ix;
1817 for (a_elt = a->first, b_elt = b->first;
1818 a_elt && b_elt;)
1820 if (a_elt->indx < b_elt->indx)
1821 return true;
1822 else if (b_elt->indx < a_elt->indx)
1823 b_elt = b_elt->next;
1824 else
1826 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1827 if (a_elt->bits[ix] & ~b_elt->bits[ix])
1828 return true;
1829 a_elt = a_elt->next;
1830 b_elt = b_elt->next;
1833 return a_elt != NULL;
1837 /* DST = A | (FROM1 & ~FROM2). Return true if DST changes. */
1839 bool
1840 bitmap_ior_and_compl (bitmap dst, const_bitmap a, const_bitmap b, const_bitmap kill)
1842 bool changed = false;
1844 bitmap_element *dst_elt = dst->first;
1845 const bitmap_element *a_elt = a->first;
1846 const bitmap_element *b_elt = b->first;
1847 const bitmap_element *kill_elt = kill->first;
1848 bitmap_element *dst_prev = NULL;
1849 bitmap_element **dst_prev_pnext = &dst->first;
1851 gcc_assert (dst != a && dst != b && dst != kill);
1853 /* Special cases. We don't bother checking for bitmap_equal_p (b, kill). */
1854 if (b == kill || bitmap_empty_p (b))
1856 changed = !bitmap_equal_p (dst, a);
1857 if (changed)
1858 bitmap_copy (dst, a);
1859 return changed;
1861 if (bitmap_empty_p (kill))
1862 return bitmap_ior (dst, a, b);
1863 if (bitmap_empty_p (a))
1864 return bitmap_and_compl (dst, b, kill);
1866 while (a_elt || b_elt)
1868 bool new_element = false;
1870 if (b_elt)
1871 while (kill_elt && kill_elt->indx < b_elt->indx)
1872 kill_elt = kill_elt->next;
1874 if (b_elt && kill_elt && kill_elt->indx == b_elt->indx
1875 && (!a_elt || a_elt->indx >= b_elt->indx))
1877 bitmap_element tmp_elt;
1878 unsigned ix;
1880 BITMAP_WORD ior = 0;
1881 tmp_elt.indx = b_elt->indx;
1882 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1884 BITMAP_WORD r = b_elt->bits[ix] & ~kill_elt->bits[ix];
1885 ior |= r;
1886 tmp_elt.bits[ix] = r;
1889 if (ior)
1891 changed = bitmap_elt_ior (dst, dst_elt, dst_prev,
1892 a_elt, &tmp_elt, changed);
1893 new_element = true;
1894 if (a_elt && a_elt->indx == b_elt->indx)
1895 a_elt = a_elt->next;
1898 b_elt = b_elt->next;
1899 kill_elt = kill_elt->next;
1901 else
1903 changed = bitmap_elt_ior (dst, dst_elt, dst_prev,
1904 a_elt, b_elt, changed);
1905 new_element = true;
1907 if (a_elt && b_elt && a_elt->indx == b_elt->indx)
1909 a_elt = a_elt->next;
1910 b_elt = b_elt->next;
1912 else
1914 if (a_elt && (!b_elt || a_elt->indx <= b_elt->indx))
1915 a_elt = a_elt->next;
1916 else if (b_elt && (!a_elt || b_elt->indx <= a_elt->indx))
1917 b_elt = b_elt->next;
1921 if (new_element)
1923 dst_prev = *dst_prev_pnext;
1924 dst_prev_pnext = &dst_prev->next;
1925 dst_elt = *dst_prev_pnext;
1929 if (dst_elt)
1931 changed = true;
1932 bitmap_elt_clear_from (dst, dst_elt);
1934 gcc_checking_assert (!dst->current == !dst->first);
1935 if (dst->current)
1936 dst->indx = dst->current->indx;
1938 return changed;
1941 /* A |= (FROM1 & ~FROM2). Return true if A changes. */
1943 bool
1944 bitmap_ior_and_compl_into (bitmap a, const_bitmap from1, const_bitmap from2)
1946 bitmap_head tmp;
1947 bool changed;
1949 bitmap_initialize (&tmp, &bitmap_default_obstack);
1950 bitmap_and_compl (&tmp, from1, from2);
1951 changed = bitmap_ior_into (a, &tmp);
1952 bitmap_clear (&tmp);
1954 return changed;
1957 /* A |= (B & C). Return true if A changes. */
1959 bool
1960 bitmap_ior_and_into (bitmap a, const_bitmap b, const_bitmap c)
1962 bitmap_element *a_elt = a->first;
1963 const bitmap_element *b_elt = b->first;
1964 const bitmap_element *c_elt = c->first;
1965 bitmap_element and_elt;
1966 bitmap_element *a_prev = NULL;
1967 bitmap_element **a_prev_pnext = &a->first;
1968 bool changed = false;
1969 unsigned ix;
1971 if (b == c)
1972 return bitmap_ior_into (a, b);
1973 if (bitmap_empty_p (b) || bitmap_empty_p (c))
1974 return false;
1976 and_elt.indx = -1;
1977 while (b_elt && c_elt)
1979 BITMAP_WORD overall;
1981 /* Find a common item of B and C. */
1982 while (b_elt->indx != c_elt->indx)
1984 if (b_elt->indx < c_elt->indx)
1986 b_elt = b_elt->next;
1987 if (!b_elt)
1988 goto done;
1990 else
1992 c_elt = c_elt->next;
1993 if (!c_elt)
1994 goto done;
1998 overall = 0;
1999 and_elt.indx = b_elt->indx;
2000 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
2002 and_elt.bits[ix] = b_elt->bits[ix] & c_elt->bits[ix];
2003 overall |= and_elt.bits[ix];
2006 b_elt = b_elt->next;
2007 c_elt = c_elt->next;
2008 if (!overall)
2009 continue;
2011 /* Now find a place to insert AND_ELT. */
2014 ix = a_elt ? a_elt->indx : and_elt.indx;
2015 if (ix == and_elt.indx)
2016 changed = bitmap_elt_ior (a, a_elt, a_prev, a_elt, &and_elt, changed);
2017 else if (ix > and_elt.indx)
2018 changed = bitmap_elt_copy (a, NULL, a_prev, &and_elt, changed);
2020 a_prev = *a_prev_pnext;
2021 a_prev_pnext = &a_prev->next;
2022 a_elt = *a_prev_pnext;
2024 /* If A lagged behind B/C, we advanced it so loop once more. */
2026 while (ix < and_elt.indx);
2029 done:
2030 gcc_checking_assert (!a->current == !a->first);
2031 if (a->current)
2032 a->indx = a->current->indx;
2033 return changed;
2036 /* Debugging function to print out the contents of a bitmap. */
2038 DEBUG_FUNCTION void
2039 debug_bitmap_file (FILE *file, const_bitmap head)
2041 const bitmap_element *ptr;
2043 fprintf (file, "\nfirst = " HOST_PTR_PRINTF
2044 " current = " HOST_PTR_PRINTF " indx = %u\n",
2045 (void *) head->first, (void *) head->current, head->indx);
2047 for (ptr = head->first; ptr; ptr = ptr->next)
2049 unsigned int i, j, col = 26;
2051 fprintf (file, "\t" HOST_PTR_PRINTF " next = " HOST_PTR_PRINTF
2052 " prev = " HOST_PTR_PRINTF " indx = %u\n\t\tbits = {",
2053 (const void*) ptr, (const void*) ptr->next,
2054 (const void*) ptr->prev, ptr->indx);
2056 for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
2057 for (j = 0; j < BITMAP_WORD_BITS; j++)
2058 if ((ptr->bits[i] >> j) & 1)
2060 if (col > 70)
2062 fprintf (file, "\n\t\t\t");
2063 col = 24;
2066 fprintf (file, " %u", (ptr->indx * BITMAP_ELEMENT_ALL_BITS
2067 + i * BITMAP_WORD_BITS + j));
2068 col += 4;
2071 fprintf (file, " }\n");
2075 /* Function to be called from the debugger to print the contents
2076 of a bitmap. */
2078 DEBUG_FUNCTION void
2079 debug_bitmap (const_bitmap head)
2081 debug_bitmap_file (stdout, head);
2084 /* Function to print out the contents of a bitmap. Unlike debug_bitmap_file,
2085 it does not print anything but the bits. */
2087 DEBUG_FUNCTION void
2088 bitmap_print (FILE *file, const_bitmap head, const char *prefix, const char *suffix)
2090 const char *comma = "";
2091 unsigned i;
2092 bitmap_iterator bi;
2094 fputs (prefix, file);
2095 EXECUTE_IF_SET_IN_BITMAP (head, 0, i, bi)
2097 fprintf (file, "%s%d", comma, i);
2098 comma = ", ";
2100 fputs (suffix, file);
2102 #ifdef GATHER_STATISTICS
2105 /* Used to accumulate statistics about bitmap sizes. */
2106 struct output_info
2108 HOST_WIDEST_INT size;
2109 int count;
2112 /* Called via htab_traverse. Output bitmap descriptor pointed out by SLOT
2113 and update statistics. */
2114 static int
2115 print_statistics (void **slot, void *b)
2117 struct bitmap_descriptor *d = (struct bitmap_descriptor *) *slot;
2118 struct output_info *i = (struct output_info *) b;
2119 char s[4096];
2121 if (d->allocated)
2123 const char *s1 = d->file;
2124 const char *s2;
2125 while ((s2 = strstr (s1, "gcc/")))
2126 s1 = s2 + 4;
2127 sprintf (s, "%s:%i (%s)", s1, d->line, d->function);
2128 s[41] = 0;
2129 fprintf (stderr, "%-41s %8d %15"HOST_WIDEST_INT_PRINT"d %15"
2130 HOST_WIDEST_INT_PRINT"d %15"HOST_WIDEST_INT_PRINT"d %10d %10d\n",
2131 s, d->created, d->allocated, d->peak, d->current, d->nsearches,
2132 d->search_iter);
2133 i->size += d->allocated;
2134 i->count += d->created;
2136 return 1;
2138 #endif
2139 /* Output per-bitmap memory usage statistics. */
2140 void
2141 dump_bitmap_statistics (void)
2143 #ifdef GATHER_STATISTICS
2144 struct output_info info;
2146 if (!bitmap_desc_hash)
2147 return;
2149 fprintf (stderr, "\nBitmap Overall "
2150 " Allocated Peak Leak searched "
2151 " search itr\n");
2152 fprintf (stderr, "---------------------------------------------------------------------------------\n");
2153 info.count = 0;
2154 info.size = 0;
2155 htab_traverse (bitmap_desc_hash, print_statistics, &info);
2156 fprintf (stderr, "---------------------------------------------------------------------------------\n");
2157 fprintf (stderr, "%-40s %9d %15"HOST_WIDEST_INT_PRINT"d\n",
2158 "Total", info.count, info.size);
2159 fprintf (stderr, "---------------------------------------------------------------------------------\n");
2160 #endif
2163 /* Compute hash of bitmap (for purposes of hashing). */
2164 hashval_t
2165 bitmap_hash (const_bitmap head)
2167 const bitmap_element *ptr;
2168 BITMAP_WORD hash = 0;
2169 int ix;
2171 for (ptr = head->first; ptr; ptr = ptr->next)
2173 hash ^= ptr->indx;
2174 for (ix = 0; ix != BITMAP_ELEMENT_WORDS; ix++)
2175 hash ^= ptr->bits[ix];
2177 return (hashval_t)hash;
2180 #include "gt-bitmap.h"