* gcc.dg/torture/tls/tls-reload-1.c: Add tls options.
[official-gcc.git] / gcc / bitmap.c
blob9f0226a1e1fe137a8541dacebe41d4b0598faa08
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"
27 #include "vec.h"
29 /* Store information about each particular bitmap, per allocation site. */
30 struct bitmap_descriptor_d
32 int id;
33 const char *function;
34 const char *file;
35 int line;
36 int created;
37 HOST_WIDEST_INT allocated;
38 HOST_WIDEST_INT peak;
39 HOST_WIDEST_INT current;
40 int nsearches;
41 int search_iter;
44 typedef struct bitmap_descriptor_d *bitmap_descriptor;
45 typedef const struct bitmap_descriptor_d *const_bitmap_descriptor;
47 /* Next available unique id number for bitmap desciptors. */
48 static int next_bitmap_desc_id = 0;
50 /* Vector mapping descriptor ids to descriptors. */
51 static vec<bitmap_descriptor> bitmap_descriptors;
53 /* Hashtable mapping bitmap names to descriptors. */
54 static htab_t bitmap_desc_hash;
56 /* Hashtable helpers. */
57 static hashval_t
58 hash_descriptor (const void *p)
60 const_bitmap_descriptor d = (const_bitmap_descriptor) p;
61 return htab_hash_pointer (d->file) + d->line;
63 struct loc
65 const char *file;
66 const char *function;
67 int line;
69 static int
70 eq_descriptor (const void *p1, const void *p2)
72 const_bitmap_descriptor d = (const_bitmap_descriptor) p1;
73 const struct loc *const l = (const struct loc *) p2;
74 return d->file == l->file && d->function == l->function && d->line == l->line;
77 /* For given file and line, return descriptor, create new if needed. */
78 static bitmap_descriptor
79 get_bitmap_descriptor (const char *file, int line, const char *function)
81 bitmap_descriptor *slot;
82 struct loc loc;
84 loc.file = file;
85 loc.function = function;
86 loc.line = line;
88 if (!bitmap_desc_hash)
89 bitmap_desc_hash = htab_create (10, hash_descriptor, eq_descriptor, NULL);
91 slot = (bitmap_descriptor *)
92 htab_find_slot_with_hash (bitmap_desc_hash, &loc,
93 htab_hash_pointer (file) + line,
94 INSERT);
95 if (*slot)
96 return *slot;
98 *slot = XCNEW (struct bitmap_descriptor_d);
99 bitmap_descriptors.safe_push (*slot);
100 (*slot)->id = next_bitmap_desc_id++;
101 (*slot)->file = file;
102 (*slot)->function = function;
103 (*slot)->line = line;
104 return *slot;
107 /* Register new bitmap. */
108 void
109 bitmap_register (bitmap b MEM_STAT_DECL)
111 bitmap_descriptor desc = get_bitmap_descriptor (ALONE_FINAL_PASS_MEM_STAT);
112 desc->created++;
113 b->descriptor_id = desc->id;
116 /* Account the overhead. */
117 static void
118 register_overhead (bitmap b, int amount)
120 bitmap_descriptor desc = bitmap_descriptors[b->descriptor_id];
121 desc->current += amount;
122 if (amount > 0)
123 desc->allocated += amount;
124 gcc_assert (desc->current >= 0);
125 if (desc->peak < desc->current)
126 desc->peak = desc->current;
129 /* Global data */
130 bitmap_element bitmap_zero_bits; /* An element of all zero bits. */
131 bitmap_obstack bitmap_default_obstack; /* The default bitmap obstack. */
132 static int bitmap_default_obstack_depth;
133 static GTY((deletable)) bitmap_element *bitmap_ggc_free; /* Freelist of
134 GC'd elements. */
136 static void bitmap_elem_to_freelist (bitmap, bitmap_element *);
137 static void bitmap_element_free (bitmap, bitmap_element *);
138 static bitmap_element *bitmap_element_allocate (bitmap);
139 static int bitmap_element_zerop (const bitmap_element *);
140 static void bitmap_element_link (bitmap, bitmap_element *);
141 static bitmap_element *bitmap_elt_insert_after (bitmap, bitmap_element *, unsigned int);
142 static void bitmap_elt_clear_from (bitmap, bitmap_element *);
143 static bitmap_element *bitmap_find_bit (bitmap, unsigned int);
146 /* Add ELEM to the appropriate freelist. */
147 static inline void
148 bitmap_elem_to_freelist (bitmap head, bitmap_element *elt)
150 bitmap_obstack *bit_obstack = head->obstack;
152 elt->next = NULL;
153 if (bit_obstack)
155 elt->prev = bit_obstack->elements;
156 bit_obstack->elements = elt;
158 else
160 elt->prev = bitmap_ggc_free;
161 bitmap_ggc_free = elt;
165 /* Free a bitmap element. Since these are allocated off the
166 bitmap_obstack, "free" actually means "put onto the freelist". */
168 static inline void
169 bitmap_element_free (bitmap head, bitmap_element *elt)
171 bitmap_element *next = elt->next;
172 bitmap_element *prev = elt->prev;
174 if (prev)
175 prev->next = next;
177 if (next)
178 next->prev = prev;
180 if (head->first == elt)
181 head->first = next;
183 /* Since the first thing we try is to insert before current,
184 make current the next entry in preference to the previous. */
185 if (head->current == elt)
187 head->current = next != 0 ? next : prev;
188 if (head->current)
189 head->indx = head->current->indx;
190 else
191 head->indx = 0;
194 if (GATHER_STATISTICS)
195 register_overhead (head, -((int)sizeof (bitmap_element)));
197 bitmap_elem_to_freelist (head, elt);
200 /* Allocate a bitmap element. The bits are cleared, but nothing else is. */
202 static inline bitmap_element *
203 bitmap_element_allocate (bitmap head)
205 bitmap_element *element;
206 bitmap_obstack *bit_obstack = head->obstack;
208 if (bit_obstack)
210 element = bit_obstack->elements;
212 if (element)
213 /* Use up the inner list first before looking at the next
214 element of the outer list. */
215 if (element->next)
217 bit_obstack->elements = element->next;
218 bit_obstack->elements->prev = element->prev;
220 else
221 /* Inner list was just a singleton. */
222 bit_obstack->elements = element->prev;
223 else
224 element = XOBNEW (&bit_obstack->obstack, bitmap_element);
226 else
228 element = bitmap_ggc_free;
229 if (element)
230 /* Use up the inner list first before looking at the next
231 element of the outer list. */
232 if (element->next)
234 bitmap_ggc_free = element->next;
235 bitmap_ggc_free->prev = element->prev;
237 else
238 /* Inner list was just a singleton. */
239 bitmap_ggc_free = element->prev;
240 else
241 element = ggc_alloc_bitmap_element_def ();
244 if (GATHER_STATISTICS)
245 register_overhead (head, sizeof (bitmap_element));
247 memset (element->bits, 0, sizeof (element->bits));
249 return element;
252 /* Remove ELT and all following elements from bitmap HEAD. */
254 void
255 bitmap_elt_clear_from (bitmap head, bitmap_element *elt)
257 bitmap_element *prev;
258 bitmap_obstack *bit_obstack = head->obstack;
260 if (!elt) return;
262 if (GATHER_STATISTICS)
264 int n = 0;
265 for (prev = elt; prev; prev = prev->next)
266 n++;
267 register_overhead (head, -sizeof (bitmap_element) * n);
270 prev = elt->prev;
271 if (prev)
273 prev->next = NULL;
274 if (head->current->indx > prev->indx)
276 head->current = prev;
277 head->indx = prev->indx;
280 else
282 head->first = NULL;
283 head->current = NULL;
284 head->indx = 0;
287 /* Put the entire list onto the free list in one operation. */
288 if (bit_obstack)
290 elt->prev = bit_obstack->elements;
291 bit_obstack->elements = elt;
293 else
295 elt->prev = bitmap_ggc_free;
296 bitmap_ggc_free = elt;
300 /* Clear a bitmap by freeing the linked list. */
302 void
303 bitmap_clear (bitmap head)
305 if (head->first)
306 bitmap_elt_clear_from (head, head->first);
309 /* Initialize a bitmap obstack. If BIT_OBSTACK is NULL, initialize
310 the default bitmap obstack. */
312 void
313 bitmap_obstack_initialize (bitmap_obstack *bit_obstack)
315 if (!bit_obstack)
317 if (bitmap_default_obstack_depth++)
318 return;
319 bit_obstack = &bitmap_default_obstack;
322 #if !defined(__GNUC__) || (__GNUC__ < 2)
323 #define __alignof__(type) 0
324 #endif
326 bit_obstack->elements = NULL;
327 bit_obstack->heads = NULL;
328 obstack_specify_allocation (&bit_obstack->obstack, OBSTACK_CHUNK_SIZE,
329 __alignof__ (bitmap_element),
330 obstack_chunk_alloc,
331 obstack_chunk_free);
334 /* Release the memory from a bitmap obstack. If BIT_OBSTACK is NULL,
335 release the default bitmap obstack. */
337 void
338 bitmap_obstack_release (bitmap_obstack *bit_obstack)
340 if (!bit_obstack)
342 if (--bitmap_default_obstack_depth)
344 gcc_assert (bitmap_default_obstack_depth > 0);
345 return;
347 bit_obstack = &bitmap_default_obstack;
350 bit_obstack->elements = NULL;
351 bit_obstack->heads = NULL;
352 obstack_free (&bit_obstack->obstack, NULL);
355 /* Create a new bitmap on an obstack. If BIT_OBSTACK is NULL, create
356 it on the default bitmap obstack. */
358 bitmap
359 bitmap_obstack_alloc_stat (bitmap_obstack *bit_obstack MEM_STAT_DECL)
361 bitmap map;
363 if (!bit_obstack)
364 bit_obstack = &bitmap_default_obstack;
365 map = bit_obstack->heads;
366 if (map)
367 bit_obstack->heads = (struct bitmap_head_def *) map->first;
368 else
369 map = XOBNEW (&bit_obstack->obstack, bitmap_head);
370 bitmap_initialize_stat (map, bit_obstack PASS_MEM_STAT);
372 if (GATHER_STATISTICS)
373 register_overhead (map, sizeof (bitmap_head));
375 return map;
378 /* Create a new GCd bitmap. */
380 bitmap
381 bitmap_gc_alloc_stat (ALONE_MEM_STAT_DECL)
383 bitmap map;
385 map = ggc_alloc_bitmap_head_def ();
386 bitmap_initialize_stat (map, NULL PASS_MEM_STAT);
388 if (GATHER_STATISTICS)
389 register_overhead (map, sizeof (bitmap_head));
391 return map;
394 /* Release an obstack allocated bitmap. */
396 void
397 bitmap_obstack_free (bitmap map)
399 if (map)
401 bitmap_clear (map);
402 map->first = (bitmap_element *) map->obstack->heads;
404 if (GATHER_STATISTICS)
405 register_overhead (map, -((int)sizeof (bitmap_head)));
407 map->obstack->heads = map;
412 /* Return nonzero if all bits in an element are zero. */
414 static inline int
415 bitmap_element_zerop (const bitmap_element *element)
417 #if BITMAP_ELEMENT_WORDS == 2
418 return (element->bits[0] | element->bits[1]) == 0;
419 #else
420 unsigned i;
422 for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
423 if (element->bits[i] != 0)
424 return 0;
426 return 1;
427 #endif
430 /* Link the bitmap element into the current bitmap linked list. */
432 static inline void
433 bitmap_element_link (bitmap head, bitmap_element *element)
435 unsigned int indx = element->indx;
436 bitmap_element *ptr;
438 /* If this is the first and only element, set it in. */
439 if (head->first == 0)
441 element->next = element->prev = 0;
442 head->first = element;
445 /* If this index is less than that of the current element, it goes someplace
446 before the current element. */
447 else if (indx < head->indx)
449 for (ptr = head->current;
450 ptr->prev != 0 && ptr->prev->indx > indx;
451 ptr = ptr->prev)
454 if (ptr->prev)
455 ptr->prev->next = element;
456 else
457 head->first = element;
459 element->prev = ptr->prev;
460 element->next = ptr;
461 ptr->prev = element;
464 /* Otherwise, it must go someplace after the current element. */
465 else
467 for (ptr = head->current;
468 ptr->next != 0 && ptr->next->indx < indx;
469 ptr = ptr->next)
472 if (ptr->next)
473 ptr->next->prev = element;
475 element->next = ptr->next;
476 element->prev = ptr;
477 ptr->next = element;
480 /* Set up so this is the first element searched. */
481 head->current = element;
482 head->indx = indx;
485 /* Insert a new uninitialized element into bitmap HEAD after element
486 ELT. If ELT is NULL, insert the element at the start. Return the
487 new element. */
489 static bitmap_element *
490 bitmap_elt_insert_after (bitmap head, bitmap_element *elt, unsigned int indx)
492 bitmap_element *node = bitmap_element_allocate (head);
493 node->indx = indx;
495 if (!elt)
497 if (!head->current)
499 head->current = node;
500 head->indx = indx;
502 node->next = head->first;
503 if (node->next)
504 node->next->prev = node;
505 head->first = node;
506 node->prev = NULL;
508 else
510 gcc_checking_assert (head->current);
511 node->next = elt->next;
512 if (node->next)
513 node->next->prev = node;
514 elt->next = node;
515 node->prev = elt;
517 return node;
520 /* Copy a bitmap to another bitmap. */
522 void
523 bitmap_copy (bitmap to, const_bitmap from)
525 const bitmap_element *from_ptr;
526 bitmap_element *to_ptr = 0;
528 bitmap_clear (to);
530 /* Copy elements in forward direction one at a time. */
531 for (from_ptr = from->first; from_ptr; from_ptr = from_ptr->next)
533 bitmap_element *to_elt = bitmap_element_allocate (to);
535 to_elt->indx = from_ptr->indx;
536 memcpy (to_elt->bits, from_ptr->bits, sizeof (to_elt->bits));
538 /* Here we have a special case of bitmap_element_link, for the case
539 where we know the links are being entered in sequence. */
540 if (to_ptr == 0)
542 to->first = to->current = to_elt;
543 to->indx = from_ptr->indx;
544 to_elt->next = to_elt->prev = 0;
546 else
548 to_elt->prev = to_ptr;
549 to_elt->next = 0;
550 to_ptr->next = to_elt;
553 to_ptr = to_elt;
557 /* Find a bitmap element that would hold a bitmap's bit.
558 Update the `current' field even if we can't find an element that
559 would hold the bitmap's bit to make eventual allocation
560 faster. */
562 static inline bitmap_element *
563 bitmap_find_bit (bitmap head, unsigned int bit)
565 bitmap_element *element;
566 unsigned int indx = bit / BITMAP_ELEMENT_ALL_BITS;
568 if (head->current == 0
569 || head->indx == indx)
570 return head->current;
572 if (GATHER_STATISTICS)
573 bitmap_descriptors[head->descriptor_id]->nsearches++;
575 if (head->indx < indx)
576 /* INDX is beyond head->indx. Search from head->current
577 forward. */
578 for (element = head->current;
579 element->next != 0 && element->indx < indx;
580 element = element->next)
582 if (GATHER_STATISTICS)
583 bitmap_descriptors[head->descriptor_id]->search_iter++;
586 else if (head->indx / 2 < indx)
587 /* INDX is less than head->indx and closer to head->indx than to
588 0. Search from head->current backward. */
589 for (element = head->current;
590 element->prev != 0 && element->indx > indx;
591 element = element->prev)
593 if (GATHER_STATISTICS)
594 bitmap_descriptors[head->descriptor_id]->search_iter++;
597 else
598 /* INDX is less than head->indx and closer to 0 than to
599 head->indx. Search from head->first forward. */
600 for (element = head->first;
601 element->next != 0 && element->indx < indx;
602 element = element->next)
603 if (GATHER_STATISTICS)
605 bitmap_descriptors[head->descriptor_id]->search_iter++;
608 /* `element' is the nearest to the one we want. If it's not the one we
609 want, the one we want doesn't exist. */
610 head->current = element;
611 head->indx = element->indx;
612 if (element != 0 && element->indx != indx)
613 element = 0;
615 return element;
618 /* Clear a single bit in a bitmap. Return true if the bit changed. */
620 bool
621 bitmap_clear_bit (bitmap head, int bit)
623 bitmap_element *const ptr = bitmap_find_bit (head, bit);
625 if (ptr != 0)
627 unsigned bit_num = bit % BITMAP_WORD_BITS;
628 unsigned word_num = bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
629 BITMAP_WORD bit_val = ((BITMAP_WORD) 1) << bit_num;
630 bool res = (ptr->bits[word_num] & bit_val) != 0;
631 if (res)
633 ptr->bits[word_num] &= ~bit_val;
634 /* If we cleared the entire word, free up the element. */
635 if (!ptr->bits[word_num]
636 && bitmap_element_zerop (ptr))
637 bitmap_element_free (head, ptr);
640 return res;
643 return false;
646 /* Set a single bit in a bitmap. Return true if the bit changed. */
648 bool
649 bitmap_set_bit (bitmap head, int bit)
651 bitmap_element *ptr = bitmap_find_bit (head, bit);
652 unsigned word_num = bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
653 unsigned bit_num = bit % BITMAP_WORD_BITS;
654 BITMAP_WORD bit_val = ((BITMAP_WORD) 1) << bit_num;
656 if (ptr == 0)
658 ptr = bitmap_element_allocate (head);
659 ptr->indx = bit / BITMAP_ELEMENT_ALL_BITS;
660 ptr->bits[word_num] = bit_val;
661 bitmap_element_link (head, ptr);
662 return true;
664 else
666 bool res = (ptr->bits[word_num] & bit_val) == 0;
667 if (res)
668 ptr->bits[word_num] |= bit_val;
669 return res;
673 /* Return whether a bit is set within a bitmap. */
676 bitmap_bit_p (bitmap head, int bit)
678 bitmap_element *ptr;
679 unsigned bit_num;
680 unsigned word_num;
682 ptr = bitmap_find_bit (head, bit);
683 if (ptr == 0)
684 return 0;
686 bit_num = bit % BITMAP_WORD_BITS;
687 word_num = bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
689 return (ptr->bits[word_num] >> bit_num) & 1;
692 #if GCC_VERSION < 3400
693 /* Table of number of set bits in a character, indexed by value of char. */
694 static const unsigned char popcount_table[] =
696 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,
697 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,
698 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,
699 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,
700 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,
701 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,
702 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,
703 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,
706 static unsigned long
707 bitmap_popcount (BITMAP_WORD a)
709 unsigned long ret = 0;
710 unsigned i;
712 /* Just do this the table way for now */
713 for (i = 0; i < BITMAP_WORD_BITS; i+= 8)
714 ret += popcount_table[(a >> i) & 0xff];
715 return ret;
717 #endif
718 /* Count the number of bits set in the bitmap, and return it. */
720 unsigned long
721 bitmap_count_bits (const_bitmap a)
723 unsigned long count = 0;
724 const bitmap_element *elt;
725 unsigned ix;
727 for (elt = a->first; elt; elt = elt->next)
729 for (ix = 0; ix != BITMAP_ELEMENT_WORDS; ix++)
731 #if GCC_VERSION >= 3400
732 /* Note that popcountl matches BITMAP_WORD in type, so the actual size
733 of BITMAP_WORD is not material. */
734 count += __builtin_popcountl (elt->bits[ix]);
735 #else
736 count += bitmap_popcount (elt->bits[ix]);
737 #endif
740 return count;
743 /* Return true if the bitmap has a single bit set. Otherwise return
744 false. */
746 bool
747 bitmap_single_bit_set_p (const_bitmap a)
749 unsigned long count = 0;
750 const bitmap_element *elt;
751 unsigned ix;
753 if (bitmap_empty_p (a))
754 return false;
756 elt = a->first;
757 /* As there are no completely empty bitmap elements, a second one
758 means we have more than one bit set. */
759 if (elt->next != NULL)
760 return false;
762 for (ix = 0; ix != BITMAP_ELEMENT_WORDS; ix++)
764 #if GCC_VERSION >= 3400
765 /* Note that popcountl matches BITMAP_WORD in type, so the actual size
766 of BITMAP_WORD is not material. */
767 count += __builtin_popcountl (elt->bits[ix]);
768 #else
769 count += bitmap_popcount (elt->bits[ix]);
770 #endif
771 if (count > 1)
772 return false;
775 return count == 1;
779 /* Return the bit number of the first set bit in the bitmap. The
780 bitmap must be non-empty. */
782 unsigned
783 bitmap_first_set_bit (const_bitmap a)
785 const bitmap_element *elt = a->first;
786 unsigned bit_no;
787 BITMAP_WORD word;
788 unsigned ix;
790 gcc_checking_assert (elt);
791 bit_no = elt->indx * BITMAP_ELEMENT_ALL_BITS;
792 for (ix = 0; ix != BITMAP_ELEMENT_WORDS; ix++)
794 word = elt->bits[ix];
795 if (word)
796 goto found_bit;
798 gcc_unreachable ();
799 found_bit:
800 bit_no += ix * BITMAP_WORD_BITS;
802 #if GCC_VERSION >= 3004
803 gcc_assert (sizeof(long) == sizeof (word));
804 bit_no += __builtin_ctzl (word);
805 #else
806 /* Binary search for the first set bit. */
807 #if BITMAP_WORD_BITS > 64
808 #error "Fill out the table."
809 #endif
810 #if BITMAP_WORD_BITS > 32
811 if (!(word & 0xffffffff))
812 word >>= 32, bit_no += 32;
813 #endif
814 if (!(word & 0xffff))
815 word >>= 16, bit_no += 16;
816 if (!(word & 0xff))
817 word >>= 8, bit_no += 8;
818 if (!(word & 0xf))
819 word >>= 4, bit_no += 4;
820 if (!(word & 0x3))
821 word >>= 2, bit_no += 2;
822 if (!(word & 0x1))
823 word >>= 1, bit_no += 1;
825 gcc_checking_assert (word & 1);
826 #endif
827 return bit_no;
830 /* Return the bit number of the first set bit in the bitmap. The
831 bitmap must be non-empty. */
833 unsigned
834 bitmap_last_set_bit (const_bitmap a)
836 const bitmap_element *elt = a->current ? a->current : a->first;
837 unsigned bit_no;
838 BITMAP_WORD word;
839 int ix;
841 gcc_checking_assert (elt);
842 while (elt->next)
843 elt = elt->next;
844 bit_no = elt->indx * BITMAP_ELEMENT_ALL_BITS;
845 for (ix = BITMAP_ELEMENT_WORDS - 1; ix >= 0; ix--)
847 word = elt->bits[ix];
848 if (word)
849 goto found_bit;
851 gcc_unreachable ();
852 found_bit:
853 bit_no += ix * BITMAP_WORD_BITS;
854 #if GCC_VERSION >= 3004
855 gcc_assert (sizeof(long) == sizeof (word));
856 bit_no += BITMAP_WORD_BITS - __builtin_clzl (word) - 1;
857 #else
858 /* Hopefully this is a twos-complement host... */
859 BITMAP_WORD x = word;
860 x |= (x >> 1);
861 x |= (x >> 2);
862 x |= (x >> 4);
863 x |= (x >> 8);
864 x |= (x >> 16);
865 #if BITMAP_WORD_BITS > 32
866 x |= (x >> 32);
867 #endif
868 bit_no += bitmap_popcount (x) - 1;
869 #endif
871 return bit_no;
875 /* DST = A & B. */
877 void
878 bitmap_and (bitmap dst, const_bitmap a, const_bitmap b)
880 bitmap_element *dst_elt = dst->first;
881 const bitmap_element *a_elt = a->first;
882 const bitmap_element *b_elt = b->first;
883 bitmap_element *dst_prev = NULL;
885 gcc_assert (dst != a && dst != b);
887 if (a == b)
889 bitmap_copy (dst, a);
890 return;
893 while (a_elt && b_elt)
895 if (a_elt->indx < b_elt->indx)
896 a_elt = a_elt->next;
897 else if (b_elt->indx < a_elt->indx)
898 b_elt = b_elt->next;
899 else
901 /* Matching elts, generate A & B. */
902 unsigned ix;
903 BITMAP_WORD ior = 0;
905 if (!dst_elt)
906 dst_elt = bitmap_elt_insert_after (dst, dst_prev, a_elt->indx);
907 else
908 dst_elt->indx = a_elt->indx;
909 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
911 BITMAP_WORD r = a_elt->bits[ix] & b_elt->bits[ix];
913 dst_elt->bits[ix] = r;
914 ior |= r;
916 if (ior)
918 dst_prev = dst_elt;
919 dst_elt = dst_elt->next;
921 a_elt = a_elt->next;
922 b_elt = b_elt->next;
925 /* Ensure that dst->current is valid. */
926 dst->current = dst->first;
927 bitmap_elt_clear_from (dst, dst_elt);
928 gcc_checking_assert (!dst->current == !dst->first);
929 if (dst->current)
930 dst->indx = dst->current->indx;
933 /* A &= B. Return true if A changed. */
935 bool
936 bitmap_and_into (bitmap a, const_bitmap b)
938 bitmap_element *a_elt = a->first;
939 const bitmap_element *b_elt = b->first;
940 bitmap_element *next;
941 bool changed = false;
943 if (a == b)
944 return false;
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;
953 changed = true;
955 else if (b_elt->indx < a_elt->indx)
956 b_elt = b_elt->next;
957 else
959 /* Matching elts, generate A &= B. */
960 unsigned ix;
961 BITMAP_WORD ior = 0;
963 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
965 BITMAP_WORD r = a_elt->bits[ix] & b_elt->bits[ix];
966 if (a_elt->bits[ix] != r)
967 changed = true;
968 a_elt->bits[ix] = r;
969 ior |= r;
971 next = a_elt->next;
972 if (!ior)
973 bitmap_element_free (a, a_elt);
974 a_elt = next;
975 b_elt = b_elt->next;
979 if (a_elt)
981 changed = true;
982 bitmap_elt_clear_from (a, a_elt);
985 gcc_checking_assert (!a->current == !a->first
986 && (!a->current || a->indx == a->current->indx));
988 return changed;
992 /* Insert an element equal to SRC_ELT after DST_PREV, overwriting DST_ELT
993 if non-NULL. CHANGED is true if the destination bitmap had already been
994 changed; the new value of CHANGED is returned. */
996 static inline bool
997 bitmap_elt_copy (bitmap dst, bitmap_element *dst_elt, bitmap_element *dst_prev,
998 const bitmap_element *src_elt, bool changed)
1000 if (!changed && dst_elt && dst_elt->indx == src_elt->indx)
1002 unsigned ix;
1004 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1005 if (src_elt->bits[ix] != dst_elt->bits[ix])
1007 dst_elt->bits[ix] = src_elt->bits[ix];
1008 changed = true;
1011 else
1013 changed = true;
1014 if (!dst_elt)
1015 dst_elt = bitmap_elt_insert_after (dst, dst_prev, src_elt->indx);
1016 else
1017 dst_elt->indx = src_elt->indx;
1018 memcpy (dst_elt->bits, src_elt->bits, sizeof (dst_elt->bits));
1020 return changed;
1025 /* DST = A & ~B */
1027 bool
1028 bitmap_and_compl (bitmap dst, const_bitmap a, const_bitmap b)
1030 bitmap_element *dst_elt = dst->first;
1031 const bitmap_element *a_elt = a->first;
1032 const bitmap_element *b_elt = b->first;
1033 bitmap_element *dst_prev = NULL;
1034 bitmap_element **dst_prev_pnext = &dst->first;
1035 bool changed = false;
1037 gcc_assert (dst != a && dst != b);
1039 if (a == b)
1041 changed = !bitmap_empty_p (dst);
1042 bitmap_clear (dst);
1043 return changed;
1046 while (a_elt)
1048 while (b_elt && b_elt->indx < a_elt->indx)
1049 b_elt = b_elt->next;
1051 if (!b_elt || b_elt->indx > a_elt->indx)
1053 changed = bitmap_elt_copy (dst, dst_elt, dst_prev, a_elt, changed);
1054 dst_prev = *dst_prev_pnext;
1055 dst_prev_pnext = &dst_prev->next;
1056 dst_elt = *dst_prev_pnext;
1057 a_elt = a_elt->next;
1060 else
1062 /* Matching elts, generate A & ~B. */
1063 unsigned ix;
1064 BITMAP_WORD ior = 0;
1066 if (!changed && dst_elt && dst_elt->indx == a_elt->indx)
1068 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1070 BITMAP_WORD r = a_elt->bits[ix] & ~b_elt->bits[ix];
1072 if (dst_elt->bits[ix] != r)
1074 changed = true;
1075 dst_elt->bits[ix] = r;
1077 ior |= r;
1080 else
1082 bool new_element;
1083 if (!dst_elt || dst_elt->indx > a_elt->indx)
1085 dst_elt = bitmap_elt_insert_after (dst, dst_prev, a_elt->indx);
1086 new_element = true;
1088 else
1090 dst_elt->indx = a_elt->indx;
1091 new_element = false;
1094 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1096 BITMAP_WORD r = a_elt->bits[ix] & ~b_elt->bits[ix];
1098 dst_elt->bits[ix] = r;
1099 ior |= r;
1102 if (ior)
1103 changed = true;
1104 else
1106 changed |= !new_element;
1107 bitmap_element_free (dst, dst_elt);
1108 dst_elt = *dst_prev_pnext;
1112 if (ior)
1114 dst_prev = *dst_prev_pnext;
1115 dst_prev_pnext = &dst_prev->next;
1116 dst_elt = *dst_prev_pnext;
1118 a_elt = a_elt->next;
1119 b_elt = b_elt->next;
1123 /* Ensure that dst->current is valid. */
1124 dst->current = dst->first;
1126 if (dst_elt)
1128 changed = true;
1129 bitmap_elt_clear_from (dst, dst_elt);
1131 gcc_checking_assert (!dst->current == !dst->first);
1132 if (dst->current)
1133 dst->indx = dst->current->indx;
1135 return changed;
1138 /* A &= ~B. Returns true if A changes */
1140 bool
1141 bitmap_and_compl_into (bitmap a, const_bitmap b)
1143 bitmap_element *a_elt = a->first;
1144 const bitmap_element *b_elt = b->first;
1145 bitmap_element *next;
1146 BITMAP_WORD changed = 0;
1148 if (a == b)
1150 if (bitmap_empty_p (a))
1151 return false;
1152 else
1154 bitmap_clear (a);
1155 return true;
1159 while (a_elt && b_elt)
1161 if (a_elt->indx < b_elt->indx)
1162 a_elt = a_elt->next;
1163 else if (b_elt->indx < a_elt->indx)
1164 b_elt = b_elt->next;
1165 else
1167 /* Matching elts, generate A &= ~B. */
1168 unsigned ix;
1169 BITMAP_WORD ior = 0;
1171 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1173 BITMAP_WORD cleared = a_elt->bits[ix] & b_elt->bits[ix];
1174 BITMAP_WORD r = a_elt->bits[ix] ^ cleared;
1176 a_elt->bits[ix] = r;
1177 changed |= cleared;
1178 ior |= r;
1180 next = a_elt->next;
1181 if (!ior)
1182 bitmap_element_free (a, a_elt);
1183 a_elt = next;
1184 b_elt = b_elt->next;
1187 gcc_checking_assert (!a->current == !a->first
1188 && (!a->current || a->indx == a->current->indx));
1189 return changed != 0;
1192 /* Set COUNT bits from START in HEAD. */
1193 void
1194 bitmap_set_range (bitmap head, unsigned int start, unsigned int count)
1196 unsigned int first_index, end_bit_plus1, last_index;
1197 bitmap_element *elt, *elt_prev;
1198 unsigned int i;
1200 if (!count)
1201 return;
1203 first_index = start / BITMAP_ELEMENT_ALL_BITS;
1204 end_bit_plus1 = start + count;
1205 last_index = (end_bit_plus1 - 1) / BITMAP_ELEMENT_ALL_BITS;
1206 elt = bitmap_find_bit (head, start);
1208 /* If bitmap_find_bit returns zero, the current is the closest block
1209 to the result. Otherwise, just use bitmap_element_allocate to
1210 ensure ELT is set; in the loop below, ELT == NULL means "insert
1211 at the end of the bitmap". */
1212 if (!elt)
1214 elt = bitmap_element_allocate (head);
1215 elt->indx = first_index;
1216 bitmap_element_link (head, elt);
1219 gcc_checking_assert (elt->indx == first_index);
1220 elt_prev = elt->prev;
1221 for (i = first_index; i <= last_index; i++)
1223 unsigned elt_start_bit = i * BITMAP_ELEMENT_ALL_BITS;
1224 unsigned elt_end_bit_plus1 = elt_start_bit + BITMAP_ELEMENT_ALL_BITS;
1226 unsigned int first_word_to_mod;
1227 BITMAP_WORD first_mask;
1228 unsigned int last_word_to_mod;
1229 BITMAP_WORD last_mask;
1230 unsigned int ix;
1232 if (!elt || elt->indx != i)
1233 elt = bitmap_elt_insert_after (head, elt_prev, i);
1235 if (elt_start_bit <= start)
1237 /* The first bit to turn on is somewhere inside this
1238 elt. */
1239 first_word_to_mod = (start - elt_start_bit) / BITMAP_WORD_BITS;
1241 /* This mask should have 1s in all bits >= start position. */
1242 first_mask =
1243 (((BITMAP_WORD) 1) << ((start % BITMAP_WORD_BITS))) - 1;
1244 first_mask = ~first_mask;
1246 else
1248 /* The first bit to turn on is below this start of this elt. */
1249 first_word_to_mod = 0;
1250 first_mask = ~(BITMAP_WORD) 0;
1253 if (elt_end_bit_plus1 <= end_bit_plus1)
1255 /* The last bit to turn on is beyond this elt. */
1256 last_word_to_mod = BITMAP_ELEMENT_WORDS - 1;
1257 last_mask = ~(BITMAP_WORD) 0;
1259 else
1261 /* The last bit to turn on is inside to this elt. */
1262 last_word_to_mod =
1263 (end_bit_plus1 - elt_start_bit) / BITMAP_WORD_BITS;
1265 /* The last mask should have 1s below the end bit. */
1266 last_mask =
1267 (((BITMAP_WORD) 1) << ((end_bit_plus1 % BITMAP_WORD_BITS))) - 1;
1270 if (first_word_to_mod == last_word_to_mod)
1272 BITMAP_WORD mask = first_mask & last_mask;
1273 elt->bits[first_word_to_mod] |= mask;
1275 else
1277 elt->bits[first_word_to_mod] |= first_mask;
1278 if (BITMAP_ELEMENT_WORDS > 2)
1279 for (ix = first_word_to_mod + 1; ix < last_word_to_mod; ix++)
1280 elt->bits[ix] = ~(BITMAP_WORD) 0;
1281 elt->bits[last_word_to_mod] |= last_mask;
1284 elt_prev = elt;
1285 elt = elt->next;
1288 head->current = elt ? elt : elt_prev;
1289 head->indx = head->current->indx;
1292 /* Clear COUNT bits from START in HEAD. */
1293 void
1294 bitmap_clear_range (bitmap head, unsigned int start, unsigned int count)
1296 unsigned int first_index, end_bit_plus1, last_index;
1297 bitmap_element *elt;
1299 if (!count)
1300 return;
1302 first_index = start / BITMAP_ELEMENT_ALL_BITS;
1303 end_bit_plus1 = start + count;
1304 last_index = (end_bit_plus1 - 1) / BITMAP_ELEMENT_ALL_BITS;
1305 elt = bitmap_find_bit (head, start);
1307 /* If bitmap_find_bit returns zero, the current is the closest block
1308 to the result. If the current is less than first index, find the
1309 next one. Otherwise, just set elt to be current. */
1310 if (!elt)
1312 if (head->current)
1314 if (head->indx < first_index)
1316 elt = head->current->next;
1317 if (!elt)
1318 return;
1320 else
1321 elt = head->current;
1323 else
1324 return;
1327 while (elt && (elt->indx <= last_index))
1329 bitmap_element * next_elt = elt->next;
1330 unsigned elt_start_bit = (elt->indx) * BITMAP_ELEMENT_ALL_BITS;
1331 unsigned elt_end_bit_plus1 = elt_start_bit + BITMAP_ELEMENT_ALL_BITS;
1334 if (elt_start_bit >= start && elt_end_bit_plus1 <= end_bit_plus1)
1335 /* Get rid of the entire elt and go to the next one. */
1336 bitmap_element_free (head, elt);
1337 else
1339 /* Going to have to knock out some bits in this elt. */
1340 unsigned int first_word_to_mod;
1341 BITMAP_WORD first_mask;
1342 unsigned int last_word_to_mod;
1343 BITMAP_WORD last_mask;
1344 unsigned int i;
1345 bool clear = true;
1347 if (elt_start_bit <= start)
1349 /* The first bit to turn off is somewhere inside this
1350 elt. */
1351 first_word_to_mod = (start - elt_start_bit) / BITMAP_WORD_BITS;
1353 /* This mask should have 1s in all bits >= start position. */
1354 first_mask =
1355 (((BITMAP_WORD) 1) << ((start % BITMAP_WORD_BITS))) - 1;
1356 first_mask = ~first_mask;
1358 else
1360 /* The first bit to turn off is below this start of this elt. */
1361 first_word_to_mod = 0;
1362 first_mask = 0;
1363 first_mask = ~first_mask;
1366 if (elt_end_bit_plus1 <= end_bit_plus1)
1368 /* The last bit to turn off is beyond this elt. */
1369 last_word_to_mod = BITMAP_ELEMENT_WORDS - 1;
1370 last_mask = 0;
1371 last_mask = ~last_mask;
1373 else
1375 /* The last bit to turn off is inside to this elt. */
1376 last_word_to_mod =
1377 (end_bit_plus1 - elt_start_bit) / BITMAP_WORD_BITS;
1379 /* The last mask should have 1s below the end bit. */
1380 last_mask =
1381 (((BITMAP_WORD) 1) << (((end_bit_plus1) % BITMAP_WORD_BITS))) - 1;
1385 if (first_word_to_mod == last_word_to_mod)
1387 BITMAP_WORD mask = first_mask & last_mask;
1388 elt->bits[first_word_to_mod] &= ~mask;
1390 else
1392 elt->bits[first_word_to_mod] &= ~first_mask;
1393 if (BITMAP_ELEMENT_WORDS > 2)
1394 for (i = first_word_to_mod + 1; i < last_word_to_mod; i++)
1395 elt->bits[i] = 0;
1396 elt->bits[last_word_to_mod] &= ~last_mask;
1398 for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
1399 if (elt->bits[i])
1401 clear = false;
1402 break;
1404 /* Check to see if there are any bits left. */
1405 if (clear)
1406 bitmap_element_free (head, elt);
1408 elt = next_elt;
1411 if (elt)
1413 head->current = elt;
1414 head->indx = head->current->indx;
1418 /* A = ~A & B. */
1420 void
1421 bitmap_compl_and_into (bitmap a, const_bitmap b)
1423 bitmap_element *a_elt = a->first;
1424 const bitmap_element *b_elt = b->first;
1425 bitmap_element *a_prev = NULL;
1426 bitmap_element *next;
1428 gcc_assert (a != b);
1430 if (bitmap_empty_p (a))
1432 bitmap_copy (a, b);
1433 return;
1435 if (bitmap_empty_p (b))
1437 bitmap_clear (a);
1438 return;
1441 while (a_elt || b_elt)
1443 if (!b_elt || (a_elt && a_elt->indx < b_elt->indx))
1445 /* A is before B. Remove A */
1446 next = a_elt->next;
1447 a_prev = a_elt->prev;
1448 bitmap_element_free (a, a_elt);
1449 a_elt = next;
1451 else if (!a_elt || b_elt->indx < a_elt->indx)
1453 /* B is before A. Copy B. */
1454 next = bitmap_elt_insert_after (a, a_prev, b_elt->indx);
1455 memcpy (next->bits, b_elt->bits, sizeof (next->bits));
1456 a_prev = next;
1457 b_elt = b_elt->next;
1459 else
1461 /* Matching elts, generate A = ~A & B. */
1462 unsigned ix;
1463 BITMAP_WORD ior = 0;
1465 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1467 BITMAP_WORD cleared = a_elt->bits[ix] & b_elt->bits[ix];
1468 BITMAP_WORD r = b_elt->bits[ix] ^ cleared;
1470 a_elt->bits[ix] = r;
1471 ior |= r;
1473 next = a_elt->next;
1474 if (!ior)
1475 bitmap_element_free (a, a_elt);
1476 else
1477 a_prev = a_elt;
1478 a_elt = next;
1479 b_elt = b_elt->next;
1482 gcc_checking_assert (!a->current == !a->first
1483 && (!a->current || a->indx == a->current->indx));
1484 return;
1488 /* Insert an element corresponding to A_ELT | B_ELT after DST_PREV,
1489 overwriting DST_ELT if non-NULL. CHANGED is true if the destination bitmap
1490 had already been changed; the new value of CHANGED is returned. */
1492 static inline bool
1493 bitmap_elt_ior (bitmap dst, bitmap_element *dst_elt, bitmap_element *dst_prev,
1494 const bitmap_element *a_elt, const bitmap_element *b_elt,
1495 bool changed)
1497 gcc_assert (a_elt || b_elt);
1499 if (a_elt && b_elt && a_elt->indx == b_elt->indx)
1501 /* Matching elts, generate A | B. */
1502 unsigned ix;
1504 if (!changed && dst_elt && dst_elt->indx == a_elt->indx)
1506 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1508 BITMAP_WORD r = a_elt->bits[ix] | b_elt->bits[ix];
1509 if (r != dst_elt->bits[ix])
1511 dst_elt->bits[ix] = r;
1512 changed = true;
1516 else
1518 changed = true;
1519 if (!dst_elt)
1520 dst_elt = bitmap_elt_insert_after (dst, dst_prev, a_elt->indx);
1521 else
1522 dst_elt->indx = a_elt->indx;
1523 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1525 BITMAP_WORD r = a_elt->bits[ix] | b_elt->bits[ix];
1526 dst_elt->bits[ix] = r;
1530 else
1532 /* Copy a single element. */
1533 const bitmap_element *src;
1535 if (!b_elt || (a_elt && a_elt->indx < b_elt->indx))
1536 src = a_elt;
1537 else
1538 src = b_elt;
1540 gcc_checking_assert (src);
1541 changed = bitmap_elt_copy (dst, dst_elt, dst_prev, src, changed);
1543 return changed;
1547 /* DST = A | B. Return true if DST changes. */
1549 bool
1550 bitmap_ior (bitmap dst, const_bitmap a, const_bitmap b)
1552 bitmap_element *dst_elt = dst->first;
1553 const bitmap_element *a_elt = a->first;
1554 const bitmap_element *b_elt = b->first;
1555 bitmap_element *dst_prev = NULL;
1556 bitmap_element **dst_prev_pnext = &dst->first;
1557 bool changed = false;
1559 gcc_assert (dst != a && dst != b);
1561 while (a_elt || b_elt)
1563 changed = bitmap_elt_ior (dst, dst_elt, dst_prev, a_elt, b_elt, changed);
1565 if (a_elt && b_elt && a_elt->indx == b_elt->indx)
1567 a_elt = a_elt->next;
1568 b_elt = b_elt->next;
1570 else
1572 if (a_elt && (!b_elt || a_elt->indx <= b_elt->indx))
1573 a_elt = a_elt->next;
1574 else if (b_elt && (!a_elt || b_elt->indx <= a_elt->indx))
1575 b_elt = b_elt->next;
1578 dst_prev = *dst_prev_pnext;
1579 dst_prev_pnext = &dst_prev->next;
1580 dst_elt = *dst_prev_pnext;
1583 if (dst_elt)
1585 changed = true;
1586 bitmap_elt_clear_from (dst, dst_elt);
1588 gcc_checking_assert (!dst->current == !dst->first);
1589 if (dst->current)
1590 dst->indx = dst->current->indx;
1591 return changed;
1594 /* A |= B. Return true if A changes. */
1596 bool
1597 bitmap_ior_into (bitmap a, const_bitmap b)
1599 bitmap_element *a_elt = a->first;
1600 const bitmap_element *b_elt = b->first;
1601 bitmap_element *a_prev = NULL;
1602 bitmap_element **a_prev_pnext = &a->first;
1603 bool changed = false;
1605 if (a == b)
1606 return false;
1608 while (b_elt)
1610 /* If A lags behind B, just advance it. */
1611 if (!a_elt || a_elt->indx == b_elt->indx)
1613 changed = bitmap_elt_ior (a, a_elt, a_prev, a_elt, b_elt, changed);
1614 b_elt = b_elt->next;
1616 else if (a_elt->indx > b_elt->indx)
1618 changed = bitmap_elt_copy (a, NULL, a_prev, b_elt, changed);
1619 b_elt = b_elt->next;
1622 a_prev = *a_prev_pnext;
1623 a_prev_pnext = &a_prev->next;
1624 a_elt = *a_prev_pnext;
1627 gcc_checking_assert (!a->current == !a->first);
1628 if (a->current)
1629 a->indx = a->current->indx;
1630 return changed;
1633 /* DST = A ^ B */
1635 void
1636 bitmap_xor (bitmap dst, const_bitmap a, const_bitmap b)
1638 bitmap_element *dst_elt = dst->first;
1639 const bitmap_element *a_elt = a->first;
1640 const bitmap_element *b_elt = b->first;
1641 bitmap_element *dst_prev = NULL;
1643 gcc_assert (dst != a && dst != b);
1644 if (a == b)
1646 bitmap_clear (dst);
1647 return;
1650 while (a_elt || b_elt)
1652 if (a_elt && b_elt && a_elt->indx == b_elt->indx)
1654 /* Matching elts, generate A ^ B. */
1655 unsigned ix;
1656 BITMAP_WORD ior = 0;
1658 if (!dst_elt)
1659 dst_elt = bitmap_elt_insert_after (dst, dst_prev, a_elt->indx);
1660 else
1661 dst_elt->indx = a_elt->indx;
1662 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1664 BITMAP_WORD r = a_elt->bits[ix] ^ b_elt->bits[ix];
1666 ior |= r;
1667 dst_elt->bits[ix] = r;
1669 a_elt = a_elt->next;
1670 b_elt = b_elt->next;
1671 if (ior)
1673 dst_prev = dst_elt;
1674 dst_elt = dst_elt->next;
1677 else
1679 /* Copy a single element. */
1680 const bitmap_element *src;
1682 if (!b_elt || (a_elt && a_elt->indx < b_elt->indx))
1684 src = a_elt;
1685 a_elt = a_elt->next;
1687 else
1689 src = b_elt;
1690 b_elt = b_elt->next;
1693 if (!dst_elt)
1694 dst_elt = bitmap_elt_insert_after (dst, dst_prev, src->indx);
1695 else
1696 dst_elt->indx = src->indx;
1697 memcpy (dst_elt->bits, src->bits, sizeof (dst_elt->bits));
1698 dst_prev = dst_elt;
1699 dst_elt = dst_elt->next;
1702 /* Ensure that dst->current is valid. */
1703 dst->current = dst->first;
1704 bitmap_elt_clear_from (dst, dst_elt);
1705 gcc_checking_assert (!dst->current == !dst->first);
1706 if (dst->current)
1707 dst->indx = dst->current->indx;
1710 /* A ^= B */
1712 void
1713 bitmap_xor_into (bitmap a, const_bitmap b)
1715 bitmap_element *a_elt = a->first;
1716 const bitmap_element *b_elt = b->first;
1717 bitmap_element *a_prev = NULL;
1719 if (a == b)
1721 bitmap_clear (a);
1722 return;
1725 while (b_elt)
1727 if (!a_elt || b_elt->indx < a_elt->indx)
1729 /* Copy b_elt. */
1730 bitmap_element *dst = bitmap_elt_insert_after (a, a_prev, b_elt->indx);
1731 memcpy (dst->bits, b_elt->bits, sizeof (dst->bits));
1732 a_prev = dst;
1733 b_elt = b_elt->next;
1735 else if (a_elt->indx < b_elt->indx)
1737 a_prev = a_elt;
1738 a_elt = a_elt->next;
1740 else
1742 /* Matching elts, generate A ^= B. */
1743 unsigned ix;
1744 BITMAP_WORD ior = 0;
1745 bitmap_element *next = a_elt->next;
1747 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1749 BITMAP_WORD r = a_elt->bits[ix] ^ b_elt->bits[ix];
1751 ior |= r;
1752 a_elt->bits[ix] = r;
1754 b_elt = b_elt->next;
1755 if (ior)
1756 a_prev = a_elt;
1757 else
1758 bitmap_element_free (a, a_elt);
1759 a_elt = next;
1762 gcc_checking_assert (!a->current == !a->first);
1763 if (a->current)
1764 a->indx = a->current->indx;
1767 /* Return true if two bitmaps are identical.
1768 We do not bother with a check for pointer equality, as that never
1769 occurs in practice. */
1771 bool
1772 bitmap_equal_p (const_bitmap a, const_bitmap b)
1774 const bitmap_element *a_elt;
1775 const bitmap_element *b_elt;
1776 unsigned ix;
1778 for (a_elt = a->first, b_elt = b->first;
1779 a_elt && b_elt;
1780 a_elt = a_elt->next, b_elt = b_elt->next)
1782 if (a_elt->indx != b_elt->indx)
1783 return false;
1784 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1785 if (a_elt->bits[ix] != b_elt->bits[ix])
1786 return false;
1788 return !a_elt && !b_elt;
1791 /* Return true if A AND B is not empty. */
1793 bool
1794 bitmap_intersect_p (const_bitmap a, const_bitmap b)
1796 const bitmap_element *a_elt;
1797 const bitmap_element *b_elt;
1798 unsigned ix;
1800 for (a_elt = a->first, b_elt = b->first;
1801 a_elt && b_elt;)
1803 if (a_elt->indx < b_elt->indx)
1804 a_elt = a_elt->next;
1805 else if (b_elt->indx < a_elt->indx)
1806 b_elt = b_elt->next;
1807 else
1809 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1810 if (a_elt->bits[ix] & b_elt->bits[ix])
1811 return true;
1812 a_elt = a_elt->next;
1813 b_elt = b_elt->next;
1816 return false;
1819 /* Return true if A AND NOT B is not empty. */
1821 bool
1822 bitmap_intersect_compl_p (const_bitmap a, const_bitmap b)
1824 const bitmap_element *a_elt;
1825 const bitmap_element *b_elt;
1826 unsigned ix;
1827 for (a_elt = a->first, b_elt = b->first;
1828 a_elt && b_elt;)
1830 if (a_elt->indx < b_elt->indx)
1831 return true;
1832 else if (b_elt->indx < a_elt->indx)
1833 b_elt = b_elt->next;
1834 else
1836 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1837 if (a_elt->bits[ix] & ~b_elt->bits[ix])
1838 return true;
1839 a_elt = a_elt->next;
1840 b_elt = b_elt->next;
1843 return a_elt != NULL;
1847 /* DST = A | (FROM1 & ~FROM2). Return true if DST changes. */
1849 bool
1850 bitmap_ior_and_compl (bitmap dst, const_bitmap a, const_bitmap b, const_bitmap kill)
1852 bool changed = false;
1854 bitmap_element *dst_elt = dst->first;
1855 const bitmap_element *a_elt = a->first;
1856 const bitmap_element *b_elt = b->first;
1857 const bitmap_element *kill_elt = kill->first;
1858 bitmap_element *dst_prev = NULL;
1859 bitmap_element **dst_prev_pnext = &dst->first;
1861 gcc_assert (dst != a && dst != b && dst != kill);
1863 /* Special cases. We don't bother checking for bitmap_equal_p (b, kill). */
1864 if (b == kill || bitmap_empty_p (b))
1866 changed = !bitmap_equal_p (dst, a);
1867 if (changed)
1868 bitmap_copy (dst, a);
1869 return changed;
1871 if (bitmap_empty_p (kill))
1872 return bitmap_ior (dst, a, b);
1873 if (bitmap_empty_p (a))
1874 return bitmap_and_compl (dst, b, kill);
1876 while (a_elt || b_elt)
1878 bool new_element = false;
1880 if (b_elt)
1881 while (kill_elt && kill_elt->indx < b_elt->indx)
1882 kill_elt = kill_elt->next;
1884 if (b_elt && kill_elt && kill_elt->indx == b_elt->indx
1885 && (!a_elt || a_elt->indx >= b_elt->indx))
1887 bitmap_element tmp_elt;
1888 unsigned ix;
1890 BITMAP_WORD ior = 0;
1891 tmp_elt.indx = b_elt->indx;
1892 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1894 BITMAP_WORD r = b_elt->bits[ix] & ~kill_elt->bits[ix];
1895 ior |= r;
1896 tmp_elt.bits[ix] = r;
1899 if (ior)
1901 changed = bitmap_elt_ior (dst, dst_elt, dst_prev,
1902 a_elt, &tmp_elt, changed);
1903 new_element = true;
1904 if (a_elt && a_elt->indx == b_elt->indx)
1905 a_elt = a_elt->next;
1908 b_elt = b_elt->next;
1909 kill_elt = kill_elt->next;
1911 else
1913 changed = bitmap_elt_ior (dst, dst_elt, dst_prev,
1914 a_elt, b_elt, changed);
1915 new_element = true;
1917 if (a_elt && b_elt && a_elt->indx == b_elt->indx)
1919 a_elt = a_elt->next;
1920 b_elt = b_elt->next;
1922 else
1924 if (a_elt && (!b_elt || a_elt->indx <= b_elt->indx))
1925 a_elt = a_elt->next;
1926 else if (b_elt && (!a_elt || b_elt->indx <= a_elt->indx))
1927 b_elt = b_elt->next;
1931 if (new_element)
1933 dst_prev = *dst_prev_pnext;
1934 dst_prev_pnext = &dst_prev->next;
1935 dst_elt = *dst_prev_pnext;
1939 if (dst_elt)
1941 changed = true;
1942 bitmap_elt_clear_from (dst, dst_elt);
1944 gcc_checking_assert (!dst->current == !dst->first);
1945 if (dst->current)
1946 dst->indx = dst->current->indx;
1948 return changed;
1951 /* A |= (FROM1 & ~FROM2). Return true if A changes. */
1953 bool
1954 bitmap_ior_and_compl_into (bitmap a, const_bitmap from1, const_bitmap from2)
1956 bitmap_head tmp;
1957 bool changed;
1959 bitmap_initialize (&tmp, &bitmap_default_obstack);
1960 bitmap_and_compl (&tmp, from1, from2);
1961 changed = bitmap_ior_into (a, &tmp);
1962 bitmap_clear (&tmp);
1964 return changed;
1967 /* A |= (B & C). Return true if A changes. */
1969 bool
1970 bitmap_ior_and_into (bitmap a, const_bitmap b, const_bitmap c)
1972 bitmap_element *a_elt = a->first;
1973 const bitmap_element *b_elt = b->first;
1974 const bitmap_element *c_elt = c->first;
1975 bitmap_element and_elt;
1976 bitmap_element *a_prev = NULL;
1977 bitmap_element **a_prev_pnext = &a->first;
1978 bool changed = false;
1979 unsigned ix;
1981 if (b == c)
1982 return bitmap_ior_into (a, b);
1983 if (bitmap_empty_p (b) || bitmap_empty_p (c))
1984 return false;
1986 and_elt.indx = -1;
1987 while (b_elt && c_elt)
1989 BITMAP_WORD overall;
1991 /* Find a common item of B and C. */
1992 while (b_elt->indx != c_elt->indx)
1994 if (b_elt->indx < c_elt->indx)
1996 b_elt = b_elt->next;
1997 if (!b_elt)
1998 goto done;
2000 else
2002 c_elt = c_elt->next;
2003 if (!c_elt)
2004 goto done;
2008 overall = 0;
2009 and_elt.indx = b_elt->indx;
2010 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
2012 and_elt.bits[ix] = b_elt->bits[ix] & c_elt->bits[ix];
2013 overall |= and_elt.bits[ix];
2016 b_elt = b_elt->next;
2017 c_elt = c_elt->next;
2018 if (!overall)
2019 continue;
2021 /* Now find a place to insert AND_ELT. */
2024 ix = a_elt ? a_elt->indx : and_elt.indx;
2025 if (ix == and_elt.indx)
2026 changed = bitmap_elt_ior (a, a_elt, a_prev, a_elt, &and_elt, changed);
2027 else if (ix > and_elt.indx)
2028 changed = bitmap_elt_copy (a, NULL, a_prev, &and_elt, changed);
2030 a_prev = *a_prev_pnext;
2031 a_prev_pnext = &a_prev->next;
2032 a_elt = *a_prev_pnext;
2034 /* If A lagged behind B/C, we advanced it so loop once more. */
2036 while (ix < and_elt.indx);
2039 done:
2040 gcc_checking_assert (!a->current == !a->first);
2041 if (a->current)
2042 a->indx = a->current->indx;
2043 return changed;
2046 /* Compute hash of bitmap (for purposes of hashing). */
2047 hashval_t
2048 bitmap_hash (const_bitmap head)
2050 const bitmap_element *ptr;
2051 BITMAP_WORD hash = 0;
2052 int ix;
2054 for (ptr = head->first; ptr; ptr = ptr->next)
2056 hash ^= ptr->indx;
2057 for (ix = 0; ix != BITMAP_ELEMENT_WORDS; ix++)
2058 hash ^= ptr->bits[ix];
2060 return (hashval_t)hash;
2064 /* Debugging function to print out the contents of a bitmap. */
2066 DEBUG_FUNCTION void
2067 debug_bitmap_file (FILE *file, const_bitmap head)
2069 const bitmap_element *ptr;
2071 fprintf (file, "\nfirst = " HOST_PTR_PRINTF
2072 " current = " HOST_PTR_PRINTF " indx = %u\n",
2073 (void *) head->first, (void *) head->current, head->indx);
2075 for (ptr = head->first; ptr; ptr = ptr->next)
2077 unsigned int i, j, col = 26;
2079 fprintf (file, "\t" HOST_PTR_PRINTF " next = " HOST_PTR_PRINTF
2080 " prev = " HOST_PTR_PRINTF " indx = %u\n\t\tbits = {",
2081 (const void*) ptr, (const void*) ptr->next,
2082 (const void*) ptr->prev, ptr->indx);
2084 for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
2085 for (j = 0; j < BITMAP_WORD_BITS; j++)
2086 if ((ptr->bits[i] >> j) & 1)
2088 if (col > 70)
2090 fprintf (file, "\n\t\t\t");
2091 col = 24;
2094 fprintf (file, " %u", (ptr->indx * BITMAP_ELEMENT_ALL_BITS
2095 + i * BITMAP_WORD_BITS + j));
2096 col += 4;
2099 fprintf (file, " }\n");
2103 /* Function to be called from the debugger to print the contents
2104 of a bitmap. */
2106 DEBUG_FUNCTION void
2107 debug_bitmap (const_bitmap head)
2109 debug_bitmap_file (stdout, head);
2112 /* Function to print out the contents of a bitmap. Unlike debug_bitmap_file,
2113 it does not print anything but the bits. */
2115 DEBUG_FUNCTION void
2116 bitmap_print (FILE *file, const_bitmap head, const char *prefix, const char *suffix)
2118 const char *comma = "";
2119 unsigned i;
2120 bitmap_iterator bi;
2122 fputs (prefix, file);
2123 EXECUTE_IF_SET_IN_BITMAP (head, 0, i, bi)
2125 fprintf (file, "%s%d", comma, i);
2126 comma = ", ";
2128 fputs (suffix, file);
2132 /* Used to accumulate statistics about bitmap sizes. */
2133 struct output_info
2135 HOST_WIDEST_INT size;
2136 int count;
2139 /* Called via htab_traverse. Output bitmap descriptor pointed out by SLOT
2140 and update statistics. */
2141 static int
2142 print_statistics (void **slot, void *b)
2144 bitmap_descriptor d = (bitmap_descriptor) *slot;
2145 struct output_info *i = (struct output_info *) b;
2146 char s[4096];
2148 if (d->allocated)
2150 const char *s1 = d->file;
2151 const char *s2;
2152 while ((s2 = strstr (s1, "gcc/")))
2153 s1 = s2 + 4;
2154 sprintf (s, "%s:%i (%s)", s1, d->line, d->function);
2155 s[41] = 0;
2156 fprintf (stderr, "%-41s %8d %15"HOST_WIDEST_INT_PRINT"d %15"
2157 HOST_WIDEST_INT_PRINT"d %15"HOST_WIDEST_INT_PRINT"d %10d %10d\n",
2158 s, d->created, d->allocated, d->peak, d->current, d->nsearches,
2159 d->search_iter);
2160 i->size += d->allocated;
2161 i->count += d->created;
2163 return 1;
2166 /* Output per-bitmap memory usage statistics. */
2167 void
2168 dump_bitmap_statistics (void)
2170 struct output_info info;
2172 if (! GATHER_STATISTICS)
2173 return;
2175 if (!bitmap_desc_hash)
2176 return;
2178 fprintf (stderr, "\nBitmap Overall "
2179 " Allocated Peak Leak searched "
2180 " search itr\n");
2181 fprintf (stderr, "---------------------------------------------------------------------------------\n");
2182 info.count = 0;
2183 info.size = 0;
2184 htab_traverse (bitmap_desc_hash, print_statistics, &info);
2185 fprintf (stderr, "---------------------------------------------------------------------------------\n");
2186 fprintf (stderr, "%-40s %9d %15"HOST_WIDEST_INT_PRINT"d\n",
2187 "Total", info.count, info.size);
2188 fprintf (stderr, "---------------------------------------------------------------------------------\n");
2191 #include "gt-bitmap.h"