match_asm_constraints: Use copy_rtx where needed (PR88001)
[official-gcc.git] / gcc / bitmap.c
blob05525795c344ed36879147808d188cb2bf0a36a4
1 /* Functions to support general ended bitmaps.
2 Copyright (C) 1997-2018 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "bitmap.h"
24 #include "selftest.h"
26 /* Memory allocation statistics purpose instance. */
27 mem_alloc_description<bitmap_usage> bitmap_mem_desc;
29 /* Static zero-initialized bitmap obstack used for default initialization
30 of bitmap_head. */
31 bitmap_obstack bitmap_head::crashme;
33 static bitmap_element *bitmap_tree_listify_from (bitmap, bitmap_element *);
35 /* Register new bitmap. */
36 void
37 bitmap_register (bitmap b MEM_STAT_DECL)
39 bitmap_mem_desc.register_descriptor (b, BITMAP_ORIGIN, false
40 FINAL_PASS_MEM_STAT);
43 /* Account the overhead. */
44 static void
45 register_overhead (bitmap b, size_t amount)
47 if (bitmap_mem_desc.contains_descriptor_for_instance (b))
48 bitmap_mem_desc.register_instance_overhead (amount, b);
51 /* Global data */
52 bitmap_element bitmap_zero_bits; /* An element of all zero bits. */
53 bitmap_obstack bitmap_default_obstack; /* The default bitmap obstack. */
54 static int bitmap_default_obstack_depth;
55 static GTY((deletable)) bitmap_element *bitmap_ggc_free; /* Freelist of
56 GC'd elements. */
59 /* Bitmap memory management. */
61 /* Add ELT to the appropriate freelist. */
62 static inline void
63 bitmap_elem_to_freelist (bitmap head, bitmap_element *elt)
65 bitmap_obstack *bit_obstack = head->obstack;
67 if (GATHER_STATISTICS)
68 register_overhead (head, -((int)sizeof (bitmap_element)));
70 elt->next = NULL;
71 elt->indx = -1;
72 if (bit_obstack)
74 elt->prev = bit_obstack->elements;
75 bit_obstack->elements = elt;
77 else
79 elt->prev = bitmap_ggc_free;
80 bitmap_ggc_free = elt;
84 /* Allocate a bitmap element. The bits are cleared, but nothing else is. */
86 static inline bitmap_element *
87 bitmap_element_allocate (bitmap head)
89 bitmap_element *element;
90 bitmap_obstack *bit_obstack = head->obstack;
92 if (bit_obstack)
94 element = bit_obstack->elements;
96 if (element)
97 /* Use up the inner list first before looking at the next
98 element of the outer list. */
99 if (element->next)
101 bit_obstack->elements = element->next;
102 bit_obstack->elements->prev = element->prev;
104 else
105 /* Inner list was just a singleton. */
106 bit_obstack->elements = element->prev;
107 else
108 element = XOBNEW (&bit_obstack->obstack, bitmap_element);
110 else
112 element = bitmap_ggc_free;
113 if (element)
114 /* Use up the inner list first before looking at the next
115 element of the outer list. */
116 if (element->next)
118 bitmap_ggc_free = element->next;
119 bitmap_ggc_free->prev = element->prev;
121 else
122 /* Inner list was just a singleton. */
123 bitmap_ggc_free = element->prev;
124 else
125 element = ggc_alloc<bitmap_element> ();
128 if (GATHER_STATISTICS)
129 register_overhead (head, sizeof (bitmap_element));
131 memset (element->bits, 0, sizeof (element->bits));
133 return element;
136 /* Remove ELT and all following elements from bitmap HEAD.
137 Put the released elements in the freelist for HEAD. */
139 void
140 bitmap_elt_clear_from (bitmap head, bitmap_element *elt)
142 bitmap_element *prev;
143 bitmap_obstack *bit_obstack = head->obstack;
145 if (!elt)
146 return;
148 if (head->tree_form)
149 elt = bitmap_tree_listify_from (head, elt);
151 if (GATHER_STATISTICS)
153 int n = 0;
154 for (prev = elt; prev; prev = prev->next)
155 n++;
156 register_overhead (head, -sizeof (bitmap_element) * n);
159 prev = elt->prev;
160 if (prev)
162 prev->next = NULL;
163 if (head->current->indx > prev->indx)
165 head->current = prev;
166 head->indx = prev->indx;
169 else
171 head->first = NULL;
172 head->current = NULL;
173 head->indx = 0;
176 /* Put the entire list onto the freelist in one operation. */
177 if (bit_obstack)
179 elt->prev = bit_obstack->elements;
180 bit_obstack->elements = elt;
182 else
184 elt->prev = bitmap_ggc_free;
185 bitmap_ggc_free = elt;
189 /* Linked-list view of bitmaps.
191 In this representation, the bitmap elements form a double-linked list
192 with elements sorted by increasing index. */
194 /* Link the bitmap element into the current bitmap linked list. */
196 static inline void
197 bitmap_list_link_element (bitmap head, bitmap_element *element)
199 unsigned int indx = element->indx;
200 bitmap_element *ptr;
202 gcc_checking_assert (!head->tree_form);
204 /* If this is the first and only element, set it in. */
205 if (head->first == 0)
207 element->next = element->prev = 0;
208 head->first = element;
211 /* If this index is less than that of the current element, it goes someplace
212 before the current element. */
213 else if (indx < head->indx)
215 for (ptr = head->current;
216 ptr->prev != 0 && ptr->prev->indx > indx;
217 ptr = ptr->prev)
220 if (ptr->prev)
221 ptr->prev->next = element;
222 else
223 head->first = element;
225 element->prev = ptr->prev;
226 element->next = ptr;
227 ptr->prev = element;
230 /* Otherwise, it must go someplace after the current element. */
231 else
233 for (ptr = head->current;
234 ptr->next != 0 && ptr->next->indx < indx;
235 ptr = ptr->next)
238 if (ptr->next)
239 ptr->next->prev = element;
241 element->next = ptr->next;
242 element->prev = ptr;
243 ptr->next = element;
246 /* Set up so this is the first element searched. */
247 head->current = element;
248 head->indx = indx;
251 /* Unlink the bitmap element from the current bitmap linked list,
252 and return it to the freelist. */
254 static inline void
255 bitmap_list_unlink_element (bitmap head, bitmap_element *element)
257 bitmap_element *next = element->next;
258 bitmap_element *prev = element->prev;
260 gcc_checking_assert (!head->tree_form);
262 if (prev)
263 prev->next = next;
265 if (next)
266 next->prev = prev;
268 if (head->first == element)
269 head->first = next;
271 /* Since the first thing we try is to insert before current,
272 make current the next entry in preference to the previous. */
273 if (head->current == element)
275 head->current = next != 0 ? next : prev;
276 if (head->current)
277 head->indx = head->current->indx;
278 else
279 head->indx = 0;
282 bitmap_elem_to_freelist (head, element);
285 /* Insert a new uninitialized element into bitmap HEAD after element
286 ELT. If ELT is NULL, insert the element at the start. Return the
287 new element. */
289 static bitmap_element *
290 bitmap_list_insert_element_after (bitmap head,
291 bitmap_element *elt, unsigned int indx)
293 bitmap_element *node = bitmap_element_allocate (head);
294 node->indx = indx;
296 gcc_checking_assert (!head->tree_form);
298 if (!elt)
300 if (!head->current)
302 head->current = node;
303 head->indx = indx;
305 node->next = head->first;
306 if (node->next)
307 node->next->prev = node;
308 head->first = node;
309 node->prev = NULL;
311 else
313 gcc_checking_assert (head->current);
314 node->next = elt->next;
315 if (node->next)
316 node->next->prev = node;
317 elt->next = node;
318 node->prev = elt;
320 return node;
323 /* Return the element for INDX, or NULL if the element doesn't exist.
324 Update the `current' field even if we can't find an element that
325 would hold the bitmap's bit to make eventual allocation
326 faster. */
328 static inline bitmap_element *
329 bitmap_list_find_element (bitmap head, unsigned int indx)
331 bitmap_element *element;
333 if (head->current == NULL
334 || head->indx == indx)
335 return head->current;
337 if (head->current == head->first
338 && head->first->next == NULL)
339 return NULL;
341 /* Usage can be NULL due to allocated bitmaps for which we do not
342 call initialize function. */
343 bitmap_usage *usage = NULL;
344 if (GATHER_STATISTICS)
345 usage = bitmap_mem_desc.get_descriptor_for_instance (head);
347 /* This bitmap has more than one element, and we're going to look
348 through the elements list. Count that as a search. */
349 if (GATHER_STATISTICS && usage)
350 usage->m_nsearches++;
352 if (head->indx < indx)
353 /* INDX is beyond head->indx. Search from head->current
354 forward. */
355 for (element = head->current;
356 element->next != 0 && element->indx < indx;
357 element = element->next)
359 if (GATHER_STATISTICS && usage)
360 usage->m_search_iter++;
363 else if (head->indx / 2 < indx)
364 /* INDX is less than head->indx and closer to head->indx than to
365 0. Search from head->current backward. */
366 for (element = head->current;
367 element->prev != 0 && element->indx > indx;
368 element = element->prev)
370 if (GATHER_STATISTICS && usage)
371 usage->m_search_iter++;
374 else
375 /* INDX is less than head->indx and closer to 0 than to
376 head->indx. Search from head->first forward. */
377 for (element = head->first;
378 element->next != 0 && element->indx < indx;
379 element = element->next)
381 if (GATHER_STATISTICS && usage)
382 usage->m_search_iter++;
385 /* `element' is the nearest to the one we want. If it's not the one we
386 want, the one we want doesn't exist. */
387 gcc_checking_assert (element != NULL);
388 head->current = element;
389 head->indx = element->indx;
390 if (element->indx != indx)
391 element = 0;
392 return element;
396 /* Splay-tree view of bitmaps.
398 This is an almost one-to-one the implementatin of the simple top-down
399 splay tree in Sleator and Tarjan's "Self-adjusting Binary Search Trees".
400 It is probably not the most efficient form of splay trees, but it should
401 be good enough to experiment with this idea of bitmaps-as-trees.
403 For all functions below, the variable or function argument "t" is a node
404 in the tree, and "e" is a temporary or new node in the tree. The rest
405 is sufficiently straigh-forward (and very well explained in the paper)
406 that comment would only clutter things. */
408 static inline void
409 bitmap_tree_link_left (bitmap_element * &t, bitmap_element * &l)
411 l->next = t;
412 l = t;
413 t = t->next;
416 static inline void
417 bitmap_tree_link_right (bitmap_element * &t, bitmap_element * &r)
419 r->prev = t;
420 r = t;
421 t = t->prev;
424 static inline void
425 bitmap_tree_rotate_left (bitmap_element * &t)
427 bitmap_element *e = t->next;
428 t->next = t->next->prev;
429 e->prev = t;
430 t = e;
433 static inline void
434 bitmap_tree_rotate_right (bitmap_element * &t)
436 bitmap_element *e = t->prev;
437 t->prev = t->prev->next;
438 e->next = t;
439 t = e;
442 static bitmap_element *
443 bitmap_tree_splay (bitmap head, bitmap_element *t, unsigned int indx)
445 bitmap_element N, *l, *r;
447 if (t == NULL)
448 return NULL;
450 bitmap_usage *usage = NULL;
451 if (GATHER_STATISTICS)
452 usage = bitmap_mem_desc.get_descriptor_for_instance (head);
454 N.prev = N.next = NULL;
455 l = r = &N;
457 while (indx != t->indx)
459 if (GATHER_STATISTICS && usage)
460 usage->m_search_iter++;
462 if (indx < t->indx)
464 if (t->prev != NULL && indx < t->prev->indx)
465 bitmap_tree_rotate_right (t);
466 if (t->prev == NULL)
467 break;
468 bitmap_tree_link_right (t, r);
470 else if (indx > t->indx)
472 if (t->next != NULL && indx > t->next->indx)
473 bitmap_tree_rotate_left (t);
474 if (t->next == NULL)
475 break;
476 bitmap_tree_link_left (t, l);
480 l->next = t->prev;
481 r->prev = t->next;
482 t->prev = N.next;
483 t->next = N.prev;
484 return t;
487 /* Link bitmap element E into the current bitmap splay tree. */
489 static inline void
490 bitmap_tree_link_element (bitmap head, bitmap_element *e)
492 if (head->first == NULL)
493 e->prev = e->next = NULL;
494 else
496 bitmap_element *t = bitmap_tree_splay (head, head->first, e->indx);
497 if (e->indx < t->indx)
499 e->prev = t->prev;
500 e->next = t;
501 t->prev = NULL;
503 else if (e->indx > t->indx)
505 e->next = t->next;
506 e->prev = t;
507 t->next = NULL;
509 else
510 gcc_unreachable ();
512 head->first = e;
513 head->current = e;
514 head->indx = e->indx;
517 /* Unlink bitmap element E from the current bitmap splay tree,
518 and return it to the freelist. */
520 static void
521 bitmap_tree_unlink_element (bitmap head, bitmap_element *e)
523 bitmap_element *t = bitmap_tree_splay (head, head->first, e->indx);
525 gcc_checking_assert (t == e);
527 if (e->prev == NULL)
528 t = e->next;
529 else
531 t = bitmap_tree_splay (head, e->prev, e->indx);
532 t->next = e->next;
534 head->first = t;
535 head->current = t;
536 head->indx = (t != NULL) ? t->indx : 0;
538 bitmap_elem_to_freelist (head, e);
541 /* Return the element for INDX, or NULL if the element doesn't exist. */
543 static inline bitmap_element *
544 bitmap_tree_find_element (bitmap head, unsigned int indx)
546 if (head->current == NULL
547 || head->indx == indx)
548 return head->current;
550 /* Usage can be NULL due to allocated bitmaps for which we do not
551 call initialize function. */
552 bitmap_usage *usage = NULL;
553 if (GATHER_STATISTICS)
554 usage = bitmap_mem_desc.get_descriptor_for_instance (head);
556 /* This bitmap has more than one element, and we're going to look
557 through the elements list. Count that as a search. */
558 if (GATHER_STATISTICS && usage)
559 usage->m_nsearches++;
561 bitmap_element *element = bitmap_tree_splay (head, head->first, indx);
562 gcc_checking_assert (element != NULL);
563 head->first = element;
564 head->current = element;
565 head->indx = element->indx;
566 if (element->indx != indx)
567 element = 0;
568 return element;
571 /* Converting bitmap views from linked-list to tree and vice versa. */
573 /* Splice element E and all elements with a larger index from
574 bitmap HEAD, convert the spliced elements to the linked-list
575 view, and return the head of the list (which should be E again), */
577 static bitmap_element *
578 bitmap_tree_listify_from (bitmap head, bitmap_element *e)
580 bitmap_element *t, *erb;
582 /* Detach the right branch from E (all elements with indx > E->indx),
583 and splay E to the root. */
584 erb = e->next;
585 e->next = NULL;
586 t = bitmap_tree_splay (head, head->first, e->indx);
587 gcc_checking_assert (t == e);
589 /* Because E has no right branch, and we rotated it to the root,
590 the left branch is the new root. */
591 t = e->prev;
592 head->first = t;
593 head->current = t;
594 head->indx = (t != NULL) ? t->indx : 0;
596 /* Detach the tree from E, and re-attach the right branch of E. */
597 e->prev = NULL;
598 e->next = erb;
600 /* The tree is now valid again. Now we need to "un-tree" E.
601 It is imperative that a non-recursive implementation is used
602 for this, because splay trees have a worst case depth of O(N)
603 for a tree with N nodes. A recursive implementation could
604 result in a stack overflow for a sufficiently large, unbalanced
605 bitmap tree. */
607 auto_vec<bitmap_element *, 32> stack;
608 auto_vec<bitmap_element *, 32> sorted_elements;
609 bitmap_element *n = e;
611 while (true)
613 while (n != NULL)
615 stack.safe_push (n);
616 n = n->prev;
619 if (stack.is_empty ())
620 break;
622 n = stack.pop ();
623 sorted_elements.safe_push (n);
624 n = n->next;
627 gcc_assert (sorted_elements[0] == e);
629 bitmap_element *prev = NULL;
630 unsigned ix;
631 FOR_EACH_VEC_ELT (sorted_elements, ix, n)
633 if (prev != NULL)
634 prev->next = n;
635 n->prev = prev;
636 n->next = NULL;
637 prev = n;
640 return e;
643 /* Convert bitmap HEAD from splay-tree view to linked-list view. */
645 void
646 bitmap_list_view (bitmap head)
648 bitmap_element *ptr;
650 gcc_assert (head->tree_form);
652 ptr = head->first;
653 if (ptr)
655 while (ptr->prev)
656 bitmap_tree_rotate_right (ptr);
657 head->first = ptr;
658 head->first = bitmap_tree_listify_from (head, ptr);
661 head->tree_form = false;
664 /* Convert bitmap HEAD from linked-list view to splay-tree view.
665 This is simply a matter of dropping the prev or next pointers
666 and setting the tree_form flag. The tree will balance itself
667 if and when it is used. */
669 void
670 bitmap_tree_view (bitmap head)
672 bitmap_element *ptr;
674 gcc_assert (! head->tree_form);
676 ptr = head->first;
677 while (ptr)
679 ptr->prev = NULL;
680 ptr = ptr->next;
683 head->tree_form = true;
686 /* Clear a bitmap by freeing all its elements. */
688 void
689 bitmap_clear (bitmap head)
691 if (head->first == NULL)
692 return;
693 if (head->tree_form)
695 bitmap_element *e, *t;
696 for (e = head->first; e->prev; e = e->prev)
697 /* Loop to find the element with the smallest index. */ ;
698 t = bitmap_tree_splay (head, head->first, e->indx);
699 gcc_checking_assert (t == e);
700 head->first = t;
702 bitmap_elt_clear_from (head, head->first);
705 /* Initialize a bitmap obstack. If BIT_OBSTACK is NULL, initialize
706 the default bitmap obstack. */
708 void
709 bitmap_obstack_initialize (bitmap_obstack *bit_obstack)
711 if (!bit_obstack)
713 if (bitmap_default_obstack_depth++)
714 return;
715 bit_obstack = &bitmap_default_obstack;
718 #if !defined(__GNUC__) || (__GNUC__ < 2)
719 #define __alignof__(type) 0
720 #endif
722 bit_obstack->elements = NULL;
723 bit_obstack->heads = NULL;
724 obstack_specify_allocation (&bit_obstack->obstack, OBSTACK_CHUNK_SIZE,
725 __alignof__ (bitmap_element),
726 obstack_chunk_alloc,
727 obstack_chunk_free);
730 /* Release the memory from a bitmap obstack. If BIT_OBSTACK is NULL,
731 release the default bitmap obstack. */
733 void
734 bitmap_obstack_release (bitmap_obstack *bit_obstack)
736 if (!bit_obstack)
738 if (--bitmap_default_obstack_depth)
740 gcc_assert (bitmap_default_obstack_depth > 0);
741 return;
743 bit_obstack = &bitmap_default_obstack;
746 bit_obstack->elements = NULL;
747 bit_obstack->heads = NULL;
748 obstack_free (&bit_obstack->obstack, NULL);
751 /* Create a new bitmap on an obstack. If BIT_OBSTACK is NULL, create
752 it on the default bitmap obstack. */
754 bitmap
755 bitmap_alloc (bitmap_obstack *bit_obstack MEM_STAT_DECL)
757 bitmap map;
759 if (!bit_obstack)
760 bit_obstack = &bitmap_default_obstack;
761 map = bit_obstack->heads;
762 if (map)
763 bit_obstack->heads = (struct bitmap_head *) map->first;
764 else
765 map = XOBNEW (&bit_obstack->obstack, bitmap_head);
766 bitmap_initialize (map, bit_obstack PASS_MEM_STAT);
768 if (GATHER_STATISTICS)
769 register_overhead (map, sizeof (bitmap_head));
771 return map;
774 /* Create a new GCd bitmap. */
776 bitmap
777 bitmap_gc_alloc (ALONE_MEM_STAT_DECL)
779 bitmap map;
781 map = ggc_alloc<bitmap_head> ();
782 bitmap_initialize (map, NULL PASS_MEM_STAT);
784 if (GATHER_STATISTICS)
785 register_overhead (map, sizeof (bitmap_head));
787 return map;
790 /* Release an obstack allocated bitmap. */
792 void
793 bitmap_obstack_free (bitmap map)
795 if (map)
797 bitmap_clear (map);
798 map->first = (bitmap_element *) map->obstack->heads;
800 if (GATHER_STATISTICS)
801 register_overhead (map, -((int)sizeof (bitmap_head)));
803 map->obstack->heads = map;
808 /* Return nonzero if all bits in an element are zero. */
810 static inline int
811 bitmap_element_zerop (const bitmap_element *element)
813 #if BITMAP_ELEMENT_WORDS == 2
814 return (element->bits[0] | element->bits[1]) == 0;
815 #else
816 unsigned i;
818 for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
819 if (element->bits[i] != 0)
820 return 0;
822 return 1;
823 #endif
826 /* Copy a bitmap to another bitmap. */
828 void
829 bitmap_copy (bitmap to, const_bitmap from)
831 const bitmap_element *from_ptr;
832 bitmap_element *to_ptr = 0;
834 gcc_checking_assert (!to->tree_form && !from->tree_form);
836 bitmap_clear (to);
838 /* Copy elements in forward direction one at a time. */
839 for (from_ptr = from->first; from_ptr; from_ptr = from_ptr->next)
841 bitmap_element *to_elt = bitmap_element_allocate (to);
843 to_elt->indx = from_ptr->indx;
844 memcpy (to_elt->bits, from_ptr->bits, sizeof (to_elt->bits));
846 /* Here we have a special case of bitmap_list_link_element,
847 for the case where we know the links are being entered
848 in sequence. */
849 if (to_ptr == 0)
851 to->first = to->current = to_elt;
852 to->indx = from_ptr->indx;
853 to_elt->next = to_elt->prev = 0;
855 else
857 to_elt->prev = to_ptr;
858 to_elt->next = 0;
859 to_ptr->next = to_elt;
862 to_ptr = to_elt;
866 /* Move a bitmap to another bitmap. */
868 void
869 bitmap_move (bitmap to, bitmap from)
871 gcc_assert (to->obstack == from->obstack);
873 bitmap_clear (to);
875 *to = *from;
877 if (GATHER_STATISTICS)
879 size_t sz = 0;
880 for (bitmap_element *e = to->first; e; e = e->next)
881 sz += sizeof (bitmap_element);
882 register_overhead (to, sz);
883 register_overhead (from, -sz);
887 /* Clear a single bit in a bitmap. Return true if the bit changed. */
889 bool
890 bitmap_clear_bit (bitmap head, int bit)
892 unsigned int indx = bit / BITMAP_ELEMENT_ALL_BITS;
893 bitmap_element *ptr;
895 if (!head->tree_form)
896 ptr = bitmap_list_find_element (head, indx);
897 else
898 ptr = bitmap_tree_find_element (head, indx);
899 if (ptr != 0)
901 unsigned bit_num = bit % BITMAP_WORD_BITS;
902 unsigned word_num = bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
903 BITMAP_WORD bit_val = ((BITMAP_WORD) 1) << bit_num;
904 bool res = (ptr->bits[word_num] & bit_val) != 0;
905 if (res)
907 ptr->bits[word_num] &= ~bit_val;
908 /* If we cleared the entire word, free up the element. */
909 if (!ptr->bits[word_num]
910 && bitmap_element_zerop (ptr))
912 if (!head->tree_form)
913 bitmap_list_unlink_element (head, ptr);
914 else
915 bitmap_tree_unlink_element (head, ptr);
919 return res;
922 return false;
925 /* Set a single bit in a bitmap. Return true if the bit changed. */
927 bool
928 bitmap_set_bit (bitmap head, int bit)
930 unsigned indx = bit / BITMAP_ELEMENT_ALL_BITS;
931 bitmap_element *ptr;
932 if (!head->tree_form)
933 ptr = bitmap_list_find_element (head, indx);
934 else
935 ptr = bitmap_tree_find_element (head, indx);
936 unsigned word_num = bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
937 unsigned bit_num = bit % BITMAP_WORD_BITS;
938 BITMAP_WORD bit_val = ((BITMAP_WORD) 1) << bit_num;
940 if (ptr != 0)
942 bool res = (ptr->bits[word_num] & bit_val) == 0;
943 if (res)
944 ptr->bits[word_num] |= bit_val;
945 return res;
948 ptr = bitmap_element_allocate (head);
949 ptr->indx = bit / BITMAP_ELEMENT_ALL_BITS;
950 ptr->bits[word_num] = bit_val;
951 if (!head->tree_form)
952 bitmap_list_link_element (head, ptr);
953 else
954 bitmap_tree_link_element (head, ptr);
955 return true;
958 /* Return whether a bit is set within a bitmap. */
961 bitmap_bit_p (bitmap head, int bit)
963 unsigned int indx = bit / BITMAP_ELEMENT_ALL_BITS;
964 bitmap_element *ptr;
965 unsigned bit_num;
966 unsigned word_num;
968 if (!head->tree_form)
969 ptr = bitmap_list_find_element (head, indx);
970 else
971 ptr = bitmap_tree_find_element (head, indx);
972 if (ptr == 0)
973 return 0;
975 bit_num = bit % BITMAP_WORD_BITS;
976 word_num = bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
978 return (ptr->bits[word_num] >> bit_num) & 1;
981 #if GCC_VERSION < 3400
982 /* Table of number of set bits in a character, indexed by value of char. */
983 static const unsigned char popcount_table[] =
985 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,
986 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,
987 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,
988 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,
989 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,
990 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,
991 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,
992 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,
995 static unsigned long
996 bitmap_popcount (BITMAP_WORD a)
998 unsigned long ret = 0;
999 unsigned i;
1001 /* Just do this the table way for now */
1002 for (i = 0; i < BITMAP_WORD_BITS; i+= 8)
1003 ret += popcount_table[(a >> i) & 0xff];
1004 return ret;
1006 #endif
1008 /* Count and return the number of bits set in the bitmap word BITS. */
1009 static unsigned long
1010 bitmap_count_bits_in_word (const BITMAP_WORD *bits)
1012 unsigned long count = 0;
1014 for (unsigned ix = 0; ix != BITMAP_ELEMENT_WORDS; ix++)
1016 #if GCC_VERSION >= 3400
1017 /* Note that popcountl matches BITMAP_WORD in type, so the actual size
1018 of BITMAP_WORD is not material. */
1019 count += __builtin_popcountl (bits[ix]);
1020 #else
1021 count += bitmap_popcount (bits[ix]);
1022 #endif
1024 return count;
1027 /* Count the number of bits set in the bitmap, and return it. */
1029 unsigned long
1030 bitmap_count_bits (const_bitmap a)
1032 unsigned long count = 0;
1033 const bitmap_element *elt;
1035 gcc_checking_assert (!a->tree_form);
1036 for (elt = a->first; elt; elt = elt->next)
1037 count += bitmap_count_bits_in_word (elt->bits);
1039 return count;
1042 /* Count the number of unique bits set in A and B and return it. */
1044 unsigned long
1045 bitmap_count_unique_bits (const_bitmap a, const_bitmap b)
1047 unsigned long count = 0;
1048 const bitmap_element *elt_a, *elt_b;
1050 for (elt_a = a->first, elt_b = b->first; elt_a && elt_b; )
1052 /* If we're at different indices, then count all the bits
1053 in the lower element. If we're at the same index, then
1054 count the bits in the IOR of the two elements. */
1055 if (elt_a->indx < elt_b->indx)
1057 count += bitmap_count_bits_in_word (elt_a->bits);
1058 elt_a = elt_a->next;
1060 else if (elt_b->indx < elt_a->indx)
1062 count += bitmap_count_bits_in_word (elt_b->bits);
1063 elt_b = elt_b->next;
1065 else
1067 BITMAP_WORD bits[BITMAP_ELEMENT_WORDS];
1068 for (unsigned ix = 0; ix != BITMAP_ELEMENT_WORDS; ix++)
1069 bits[ix] = elt_a->bits[ix] | elt_b->bits[ix];
1070 count += bitmap_count_bits_in_word (bits);
1071 elt_a = elt_a->next;
1072 elt_b = elt_b->next;
1075 return count;
1078 /* Return true if the bitmap has a single bit set. Otherwise return
1079 false. */
1081 bool
1082 bitmap_single_bit_set_p (const_bitmap a)
1084 unsigned long count = 0;
1085 const bitmap_element *elt;
1086 unsigned ix;
1088 if (bitmap_empty_p (a))
1089 return false;
1091 elt = a->first;
1093 /* As there are no completely empty bitmap elements, a second one
1094 means we have more than one bit set. */
1095 if (elt->next != NULL
1096 && (!a->tree_form || elt->prev != NULL))
1097 return false;
1099 for (ix = 0; ix != BITMAP_ELEMENT_WORDS; ix++)
1101 #if GCC_VERSION >= 3400
1102 /* Note that popcountl matches BITMAP_WORD in type, so the actual size
1103 of BITMAP_WORD is not material. */
1104 count += __builtin_popcountl (elt->bits[ix]);
1105 #else
1106 count += bitmap_popcount (elt->bits[ix]);
1107 #endif
1108 if (count > 1)
1109 return false;
1112 return count == 1;
1116 /* Return the bit number of the first set bit in the bitmap. The
1117 bitmap must be non-empty. */
1119 unsigned
1120 bitmap_first_set_bit (const_bitmap a)
1122 const bitmap_element *elt = a->first;
1123 unsigned bit_no;
1124 BITMAP_WORD word;
1125 unsigned ix;
1127 gcc_checking_assert (elt);
1129 if (a->tree_form)
1130 while (elt->prev)
1131 elt = elt->prev;
1133 bit_no = elt->indx * BITMAP_ELEMENT_ALL_BITS;
1134 for (ix = 0; ix != BITMAP_ELEMENT_WORDS; ix++)
1136 word = elt->bits[ix];
1137 if (word)
1138 goto found_bit;
1140 gcc_unreachable ();
1141 found_bit:
1142 bit_no += ix * BITMAP_WORD_BITS;
1144 #if GCC_VERSION >= 3004
1145 gcc_assert (sizeof (long) == sizeof (word));
1146 bit_no += __builtin_ctzl (word);
1147 #else
1148 /* Binary search for the first set bit. */
1149 #if BITMAP_WORD_BITS > 64
1150 #error "Fill out the table."
1151 #endif
1152 #if BITMAP_WORD_BITS > 32
1153 if (!(word & 0xffffffff))
1154 word >>= 32, bit_no += 32;
1155 #endif
1156 if (!(word & 0xffff))
1157 word >>= 16, bit_no += 16;
1158 if (!(word & 0xff))
1159 word >>= 8, bit_no += 8;
1160 if (!(word & 0xf))
1161 word >>= 4, bit_no += 4;
1162 if (!(word & 0x3))
1163 word >>= 2, bit_no += 2;
1164 if (!(word & 0x1))
1165 word >>= 1, bit_no += 1;
1167 gcc_checking_assert (word & 1);
1168 #endif
1169 return bit_no;
1172 /* Return the bit number of the first set bit in the bitmap. The
1173 bitmap must be non-empty. */
1175 unsigned
1176 bitmap_last_set_bit (const_bitmap a)
1178 const bitmap_element *elt;
1179 unsigned bit_no;
1180 BITMAP_WORD word;
1181 int ix;
1183 if (a->tree_form)
1184 elt = a->first;
1185 else
1186 elt = a->current ? a->current : a->first;
1187 gcc_checking_assert (elt);
1189 while (elt->next)
1190 elt = elt->next;
1192 bit_no = elt->indx * BITMAP_ELEMENT_ALL_BITS;
1193 for (ix = BITMAP_ELEMENT_WORDS - 1; ix >= 1; ix--)
1195 word = elt->bits[ix];
1196 if (word)
1197 goto found_bit;
1199 gcc_assert (elt->bits[ix] != 0);
1200 found_bit:
1201 bit_no += ix * BITMAP_WORD_BITS;
1202 #if GCC_VERSION >= 3004
1203 gcc_assert (sizeof (long) == sizeof (word));
1204 bit_no += BITMAP_WORD_BITS - __builtin_clzl (word) - 1;
1205 #else
1206 /* Hopefully this is a twos-complement host... */
1207 BITMAP_WORD x = word;
1208 x |= (x >> 1);
1209 x |= (x >> 2);
1210 x |= (x >> 4);
1211 x |= (x >> 8);
1212 x |= (x >> 16);
1213 #if BITMAP_WORD_BITS > 32
1214 x |= (x >> 32);
1215 #endif
1216 bit_no += bitmap_popcount (x) - 1;
1217 #endif
1219 return bit_no;
1223 /* DST = A & B. */
1225 void
1226 bitmap_and (bitmap dst, const_bitmap a, const_bitmap b)
1228 bitmap_element *dst_elt = dst->first;
1229 const bitmap_element *a_elt = a->first;
1230 const bitmap_element *b_elt = b->first;
1231 bitmap_element *dst_prev = NULL;
1233 gcc_checking_assert (!dst->tree_form && !a->tree_form && !b->tree_form);
1234 gcc_assert (dst != a && dst != b);
1236 if (a == b)
1238 bitmap_copy (dst, a);
1239 return;
1242 while (a_elt && b_elt)
1244 if (a_elt->indx < b_elt->indx)
1245 a_elt = a_elt->next;
1246 else if (b_elt->indx < a_elt->indx)
1247 b_elt = b_elt->next;
1248 else
1250 /* Matching elts, generate A & B. */
1251 unsigned ix;
1252 BITMAP_WORD ior = 0;
1254 if (!dst_elt)
1255 dst_elt = bitmap_list_insert_element_after (dst, dst_prev,
1256 a_elt->indx);
1257 else
1258 dst_elt->indx = a_elt->indx;
1259 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1261 BITMAP_WORD r = a_elt->bits[ix] & b_elt->bits[ix];
1263 dst_elt->bits[ix] = r;
1264 ior |= r;
1266 if (ior)
1268 dst_prev = dst_elt;
1269 dst_elt = dst_elt->next;
1271 a_elt = a_elt->next;
1272 b_elt = b_elt->next;
1275 /* Ensure that dst->current is valid. */
1276 dst->current = dst->first;
1277 bitmap_elt_clear_from (dst, dst_elt);
1278 gcc_checking_assert (!dst->current == !dst->first);
1279 if (dst->current)
1280 dst->indx = dst->current->indx;
1283 /* A &= B. Return true if A changed. */
1285 bool
1286 bitmap_and_into (bitmap a, const_bitmap b)
1288 bitmap_element *a_elt = a->first;
1289 const bitmap_element *b_elt = b->first;
1290 bitmap_element *next;
1291 bool changed = false;
1293 gcc_checking_assert (!a->tree_form && !b->tree_form);
1295 if (a == b)
1296 return false;
1298 while (a_elt && b_elt)
1300 if (a_elt->indx < b_elt->indx)
1302 next = a_elt->next;
1303 bitmap_list_unlink_element (a, a_elt);
1304 a_elt = next;
1305 changed = true;
1307 else if (b_elt->indx < a_elt->indx)
1308 b_elt = b_elt->next;
1309 else
1311 /* Matching elts, generate A &= B. */
1312 unsigned ix;
1313 BITMAP_WORD ior = 0;
1315 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1317 BITMAP_WORD r = a_elt->bits[ix] & b_elt->bits[ix];
1318 if (a_elt->bits[ix] != r)
1319 changed = true;
1320 a_elt->bits[ix] = r;
1321 ior |= r;
1323 next = a_elt->next;
1324 if (!ior)
1325 bitmap_list_unlink_element (a, a_elt);
1326 a_elt = next;
1327 b_elt = b_elt->next;
1331 if (a_elt)
1333 changed = true;
1334 bitmap_elt_clear_from (a, a_elt);
1337 gcc_checking_assert (!a->current == !a->first
1338 && (!a->current || a->indx == a->current->indx));
1340 return changed;
1344 /* Insert an element equal to SRC_ELT after DST_PREV, overwriting DST_ELT
1345 if non-NULL. CHANGED is true if the destination bitmap had already been
1346 changed; the new value of CHANGED is returned. */
1348 static inline bool
1349 bitmap_elt_copy (bitmap dst, bitmap_element *dst_elt, bitmap_element *dst_prev,
1350 const bitmap_element *src_elt, bool changed)
1352 if (!changed && dst_elt && dst_elt->indx == src_elt->indx)
1354 unsigned ix;
1356 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1357 if (src_elt->bits[ix] != dst_elt->bits[ix])
1359 dst_elt->bits[ix] = src_elt->bits[ix];
1360 changed = true;
1363 else
1365 changed = true;
1366 if (!dst_elt)
1367 dst_elt = bitmap_list_insert_element_after (dst, dst_prev,
1368 src_elt->indx);
1369 else
1370 dst_elt->indx = src_elt->indx;
1371 memcpy (dst_elt->bits, src_elt->bits, sizeof (dst_elt->bits));
1373 return changed;
1378 /* DST = A & ~B */
1380 bool
1381 bitmap_and_compl (bitmap dst, const_bitmap a, const_bitmap b)
1383 bitmap_element *dst_elt = dst->first;
1384 const bitmap_element *a_elt = a->first;
1385 const bitmap_element *b_elt = b->first;
1386 bitmap_element *dst_prev = NULL;
1387 bitmap_element **dst_prev_pnext = &dst->first;
1388 bool changed = false;
1390 gcc_checking_assert (!dst->tree_form && !a->tree_form && !b->tree_form);
1391 gcc_assert (dst != a && dst != b);
1393 if (a == b)
1395 changed = !bitmap_empty_p (dst);
1396 bitmap_clear (dst);
1397 return changed;
1400 while (a_elt)
1402 while (b_elt && b_elt->indx < a_elt->indx)
1403 b_elt = b_elt->next;
1405 if (!b_elt || b_elt->indx > a_elt->indx)
1407 changed = bitmap_elt_copy (dst, dst_elt, dst_prev, a_elt, changed);
1408 dst_prev = *dst_prev_pnext;
1409 dst_prev_pnext = &dst_prev->next;
1410 dst_elt = *dst_prev_pnext;
1411 a_elt = a_elt->next;
1414 else
1416 /* Matching elts, generate A & ~B. */
1417 unsigned ix;
1418 BITMAP_WORD ior = 0;
1420 if (!changed && dst_elt && dst_elt->indx == a_elt->indx)
1422 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1424 BITMAP_WORD r = a_elt->bits[ix] & ~b_elt->bits[ix];
1426 if (dst_elt->bits[ix] != r)
1428 changed = true;
1429 dst_elt->bits[ix] = r;
1431 ior |= r;
1434 else
1436 bool new_element;
1437 if (!dst_elt || dst_elt->indx > a_elt->indx)
1439 dst_elt = bitmap_list_insert_element_after (dst, dst_prev,
1440 a_elt->indx);
1441 new_element = true;
1443 else
1445 dst_elt->indx = a_elt->indx;
1446 new_element = false;
1449 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1451 BITMAP_WORD r = a_elt->bits[ix] & ~b_elt->bits[ix];
1453 dst_elt->bits[ix] = r;
1454 ior |= r;
1457 if (ior)
1458 changed = true;
1459 else
1461 changed |= !new_element;
1462 bitmap_list_unlink_element (dst, dst_elt);
1463 dst_elt = *dst_prev_pnext;
1467 if (ior)
1469 dst_prev = *dst_prev_pnext;
1470 dst_prev_pnext = &dst_prev->next;
1471 dst_elt = *dst_prev_pnext;
1473 a_elt = a_elt->next;
1474 b_elt = b_elt->next;
1478 /* Ensure that dst->current is valid. */
1479 dst->current = dst->first;
1481 if (dst_elt)
1483 changed = true;
1484 bitmap_elt_clear_from (dst, dst_elt);
1486 gcc_checking_assert (!dst->current == !dst->first);
1487 if (dst->current)
1488 dst->indx = dst->current->indx;
1490 return changed;
1493 /* A &= ~B. Returns true if A changes */
1495 bool
1496 bitmap_and_compl_into (bitmap a, const_bitmap b)
1498 bitmap_element *a_elt = a->first;
1499 const bitmap_element *b_elt = b->first;
1500 bitmap_element *next;
1501 BITMAP_WORD changed = 0;
1503 gcc_checking_assert (!a->tree_form && !b->tree_form);
1505 if (a == b)
1507 if (bitmap_empty_p (a))
1508 return false;
1509 else
1511 bitmap_clear (a);
1512 return true;
1516 while (a_elt && b_elt)
1518 if (a_elt->indx < b_elt->indx)
1519 a_elt = a_elt->next;
1520 else if (b_elt->indx < a_elt->indx)
1521 b_elt = b_elt->next;
1522 else
1524 /* Matching elts, generate A &= ~B. */
1525 unsigned ix;
1526 BITMAP_WORD ior = 0;
1528 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1530 BITMAP_WORD cleared = a_elt->bits[ix] & b_elt->bits[ix];
1531 BITMAP_WORD r = a_elt->bits[ix] ^ cleared;
1533 a_elt->bits[ix] = r;
1534 changed |= cleared;
1535 ior |= r;
1537 next = a_elt->next;
1538 if (!ior)
1539 bitmap_list_unlink_element (a, a_elt);
1540 a_elt = next;
1541 b_elt = b_elt->next;
1544 gcc_checking_assert (!a->current == !a->first
1545 && (!a->current || a->indx == a->current->indx));
1546 return changed != 0;
1549 /* Set COUNT bits from START in HEAD. */
1550 void
1551 bitmap_set_range (bitmap head, unsigned int start, unsigned int count)
1553 unsigned int first_index, end_bit_plus1, last_index;
1554 bitmap_element *elt, *elt_prev;
1555 unsigned int i;
1557 gcc_checking_assert (!head->tree_form);
1559 if (!count)
1560 return;
1562 if (count == 1)
1564 bitmap_set_bit (head, start);
1565 return;
1568 first_index = start / BITMAP_ELEMENT_ALL_BITS;
1569 end_bit_plus1 = start + count;
1570 last_index = (end_bit_plus1 - 1) / BITMAP_ELEMENT_ALL_BITS;
1571 elt = bitmap_list_find_element (head, first_index);
1573 /* If bitmap_list_find_element returns zero, the current is the closest block
1574 to the result. Otherwise, just use bitmap_element_allocate to
1575 ensure ELT is set; in the loop below, ELT == NULL means "insert
1576 at the end of the bitmap". */
1577 if (!elt)
1579 elt = bitmap_element_allocate (head);
1580 elt->indx = first_index;
1581 bitmap_list_link_element (head, elt);
1584 gcc_checking_assert (elt->indx == first_index);
1585 elt_prev = elt->prev;
1586 for (i = first_index; i <= last_index; i++)
1588 unsigned elt_start_bit = i * BITMAP_ELEMENT_ALL_BITS;
1589 unsigned elt_end_bit_plus1 = elt_start_bit + BITMAP_ELEMENT_ALL_BITS;
1591 unsigned int first_word_to_mod;
1592 BITMAP_WORD first_mask;
1593 unsigned int last_word_to_mod;
1594 BITMAP_WORD last_mask;
1595 unsigned int ix;
1597 if (!elt || elt->indx != i)
1598 elt = bitmap_list_insert_element_after (head, elt_prev, i);
1600 if (elt_start_bit <= start)
1602 /* The first bit to turn on is somewhere inside this
1603 elt. */
1604 first_word_to_mod = (start - elt_start_bit) / BITMAP_WORD_BITS;
1606 /* This mask should have 1s in all bits >= start position. */
1607 first_mask =
1608 (((BITMAP_WORD) 1) << ((start % BITMAP_WORD_BITS))) - 1;
1609 first_mask = ~first_mask;
1611 else
1613 /* The first bit to turn on is below this start of this elt. */
1614 first_word_to_mod = 0;
1615 first_mask = ~(BITMAP_WORD) 0;
1618 if (elt_end_bit_plus1 <= end_bit_plus1)
1620 /* The last bit to turn on is beyond this elt. */
1621 last_word_to_mod = BITMAP_ELEMENT_WORDS - 1;
1622 last_mask = ~(BITMAP_WORD) 0;
1624 else
1626 /* The last bit to turn on is inside to this elt. */
1627 last_word_to_mod =
1628 (end_bit_plus1 - elt_start_bit) / BITMAP_WORD_BITS;
1630 /* The last mask should have 1s below the end bit. */
1631 last_mask =
1632 (((BITMAP_WORD) 1) << ((end_bit_plus1 % BITMAP_WORD_BITS))) - 1;
1635 if (first_word_to_mod == last_word_to_mod)
1637 BITMAP_WORD mask = first_mask & last_mask;
1638 elt->bits[first_word_to_mod] |= mask;
1640 else
1642 elt->bits[first_word_to_mod] |= first_mask;
1643 if (BITMAP_ELEMENT_WORDS > 2)
1644 for (ix = first_word_to_mod + 1; ix < last_word_to_mod; ix++)
1645 elt->bits[ix] = ~(BITMAP_WORD) 0;
1646 elt->bits[last_word_to_mod] |= last_mask;
1649 elt_prev = elt;
1650 elt = elt->next;
1653 head->current = elt ? elt : elt_prev;
1654 head->indx = head->current->indx;
1657 /* Clear COUNT bits from START in HEAD. */
1658 void
1659 bitmap_clear_range (bitmap head, unsigned int start, unsigned int count)
1661 unsigned int first_index, end_bit_plus1, last_index;
1662 bitmap_element *elt;
1664 gcc_checking_assert (!head->tree_form);
1666 if (!count)
1667 return;
1669 if (count == 1)
1671 bitmap_clear_bit (head, start);
1672 return;
1675 first_index = start / BITMAP_ELEMENT_ALL_BITS;
1676 end_bit_plus1 = start + count;
1677 last_index = (end_bit_plus1 - 1) / BITMAP_ELEMENT_ALL_BITS;
1678 elt = bitmap_list_find_element (head, first_index);
1680 /* If bitmap_list_find_element returns zero, the current is the closest block
1681 to the result. If the current is less than first index, find the
1682 next one. Otherwise, just set elt to be current. */
1683 if (!elt)
1685 if (head->current)
1687 if (head->indx < first_index)
1689 elt = head->current->next;
1690 if (!elt)
1691 return;
1693 else
1694 elt = head->current;
1696 else
1697 return;
1700 while (elt && (elt->indx <= last_index))
1702 bitmap_element * next_elt = elt->next;
1703 unsigned elt_start_bit = (elt->indx) * BITMAP_ELEMENT_ALL_BITS;
1704 unsigned elt_end_bit_plus1 = elt_start_bit + BITMAP_ELEMENT_ALL_BITS;
1707 if (elt_start_bit >= start && elt_end_bit_plus1 <= end_bit_plus1)
1708 /* Get rid of the entire elt and go to the next one. */
1709 bitmap_list_unlink_element (head, elt);
1710 else
1712 /* Going to have to knock out some bits in this elt. */
1713 unsigned int first_word_to_mod;
1714 BITMAP_WORD first_mask;
1715 unsigned int last_word_to_mod;
1716 BITMAP_WORD last_mask;
1717 unsigned int i;
1718 bool clear = true;
1720 if (elt_start_bit <= start)
1722 /* The first bit to turn off is somewhere inside this
1723 elt. */
1724 first_word_to_mod = (start - elt_start_bit) / BITMAP_WORD_BITS;
1726 /* This mask should have 1s in all bits >= start position. */
1727 first_mask =
1728 (((BITMAP_WORD) 1) << ((start % BITMAP_WORD_BITS))) - 1;
1729 first_mask = ~first_mask;
1731 else
1733 /* The first bit to turn off is below this start of this elt. */
1734 first_word_to_mod = 0;
1735 first_mask = 0;
1736 first_mask = ~first_mask;
1739 if (elt_end_bit_plus1 <= end_bit_plus1)
1741 /* The last bit to turn off is beyond this elt. */
1742 last_word_to_mod = BITMAP_ELEMENT_WORDS - 1;
1743 last_mask = 0;
1744 last_mask = ~last_mask;
1746 else
1748 /* The last bit to turn off is inside to this elt. */
1749 last_word_to_mod =
1750 (end_bit_plus1 - elt_start_bit) / BITMAP_WORD_BITS;
1752 /* The last mask should have 1s below the end bit. */
1753 last_mask =
1754 (((BITMAP_WORD) 1) << (((end_bit_plus1) % BITMAP_WORD_BITS))) - 1;
1758 if (first_word_to_mod == last_word_to_mod)
1760 BITMAP_WORD mask = first_mask & last_mask;
1761 elt->bits[first_word_to_mod] &= ~mask;
1763 else
1765 elt->bits[first_word_to_mod] &= ~first_mask;
1766 if (BITMAP_ELEMENT_WORDS > 2)
1767 for (i = first_word_to_mod + 1; i < last_word_to_mod; i++)
1768 elt->bits[i] = 0;
1769 elt->bits[last_word_to_mod] &= ~last_mask;
1771 for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
1772 if (elt->bits[i])
1774 clear = false;
1775 break;
1777 /* Check to see if there are any bits left. */
1778 if (clear)
1779 bitmap_list_unlink_element (head, elt);
1781 elt = next_elt;
1784 if (elt)
1786 head->current = elt;
1787 head->indx = head->current->indx;
1791 /* A = ~A & B. */
1793 void
1794 bitmap_compl_and_into (bitmap a, const_bitmap b)
1796 bitmap_element *a_elt = a->first;
1797 const bitmap_element *b_elt = b->first;
1798 bitmap_element *a_prev = NULL;
1799 bitmap_element *next;
1801 gcc_checking_assert (!a->tree_form && !b->tree_form);
1802 gcc_assert (a != b);
1804 if (bitmap_empty_p (a))
1806 bitmap_copy (a, b);
1807 return;
1809 if (bitmap_empty_p (b))
1811 bitmap_clear (a);
1812 return;
1815 while (a_elt || b_elt)
1817 if (!b_elt || (a_elt && a_elt->indx < b_elt->indx))
1819 /* A is before B. Remove A */
1820 next = a_elt->next;
1821 a_prev = a_elt->prev;
1822 bitmap_list_unlink_element (a, a_elt);
1823 a_elt = next;
1825 else if (!a_elt || b_elt->indx < a_elt->indx)
1827 /* B is before A. Copy B. */
1828 next = bitmap_list_insert_element_after (a, a_prev, b_elt->indx);
1829 memcpy (next->bits, b_elt->bits, sizeof (next->bits));
1830 a_prev = next;
1831 b_elt = b_elt->next;
1833 else
1835 /* Matching elts, generate A = ~A & B. */
1836 unsigned ix;
1837 BITMAP_WORD ior = 0;
1839 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1841 BITMAP_WORD cleared = a_elt->bits[ix] & b_elt->bits[ix];
1842 BITMAP_WORD r = b_elt->bits[ix] ^ cleared;
1844 a_elt->bits[ix] = r;
1845 ior |= r;
1847 next = a_elt->next;
1848 if (!ior)
1849 bitmap_list_unlink_element (a, a_elt);
1850 else
1851 a_prev = a_elt;
1852 a_elt = next;
1853 b_elt = b_elt->next;
1856 gcc_checking_assert (!a->current == !a->first
1857 && (!a->current || a->indx == a->current->indx));
1858 return;
1862 /* Insert an element corresponding to A_ELT | B_ELT after DST_PREV,
1863 overwriting DST_ELT if non-NULL. CHANGED is true if the destination bitmap
1864 had already been changed; the new value of CHANGED is returned. */
1866 static inline bool
1867 bitmap_elt_ior (bitmap dst, bitmap_element *dst_elt, bitmap_element *dst_prev,
1868 const bitmap_element *a_elt, const bitmap_element *b_elt,
1869 bool changed)
1871 gcc_assert (a_elt || b_elt);
1873 if (a_elt && b_elt && a_elt->indx == b_elt->indx)
1875 /* Matching elts, generate A | B. */
1876 unsigned ix;
1878 if (!changed && dst_elt && dst_elt->indx == a_elt->indx)
1880 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1882 BITMAP_WORD r = a_elt->bits[ix] | b_elt->bits[ix];
1883 if (r != dst_elt->bits[ix])
1885 dst_elt->bits[ix] = r;
1886 changed = true;
1890 else
1892 changed = true;
1893 if (!dst_elt)
1894 dst_elt = bitmap_list_insert_element_after (dst, dst_prev,
1895 a_elt->indx);
1896 else
1897 dst_elt->indx = a_elt->indx;
1898 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1900 BITMAP_WORD r = a_elt->bits[ix] | b_elt->bits[ix];
1901 dst_elt->bits[ix] = r;
1905 else
1907 /* Copy a single element. */
1908 const bitmap_element *src;
1910 if (!b_elt || (a_elt && a_elt->indx < b_elt->indx))
1911 src = a_elt;
1912 else
1913 src = b_elt;
1915 gcc_checking_assert (src);
1916 changed = bitmap_elt_copy (dst, dst_elt, dst_prev, src, changed);
1918 return changed;
1922 /* DST = A | B. Return true if DST changes. */
1924 bool
1925 bitmap_ior (bitmap dst, const_bitmap a, const_bitmap b)
1927 bitmap_element *dst_elt = dst->first;
1928 const bitmap_element *a_elt = a->first;
1929 const bitmap_element *b_elt = b->first;
1930 bitmap_element *dst_prev = NULL;
1931 bitmap_element **dst_prev_pnext = &dst->first;
1932 bool changed = false;
1934 gcc_checking_assert (!dst->tree_form && !a->tree_form && !b->tree_form);
1935 gcc_assert (dst != a && dst != b);
1937 while (a_elt || b_elt)
1939 changed = bitmap_elt_ior (dst, dst_elt, dst_prev, a_elt, b_elt, changed);
1941 if (a_elt && b_elt && a_elt->indx == b_elt->indx)
1943 a_elt = a_elt->next;
1944 b_elt = b_elt->next;
1946 else
1948 if (a_elt && (!b_elt || a_elt->indx <= b_elt->indx))
1949 a_elt = a_elt->next;
1950 else if (b_elt && (!a_elt || b_elt->indx <= a_elt->indx))
1951 b_elt = b_elt->next;
1954 dst_prev = *dst_prev_pnext;
1955 dst_prev_pnext = &dst_prev->next;
1956 dst_elt = *dst_prev_pnext;
1959 if (dst_elt)
1961 changed = true;
1962 /* Ensure that dst->current is valid. */
1963 dst->current = dst->first;
1964 bitmap_elt_clear_from (dst, dst_elt);
1966 gcc_checking_assert (!dst->current == !dst->first);
1967 if (dst->current)
1968 dst->indx = dst->current->indx;
1969 return changed;
1972 /* A |= B. Return true if A changes. */
1974 bool
1975 bitmap_ior_into (bitmap a, const_bitmap b)
1977 bitmap_element *a_elt = a->first;
1978 const bitmap_element *b_elt = b->first;
1979 bitmap_element *a_prev = NULL;
1980 bitmap_element **a_prev_pnext = &a->first;
1981 bool changed = false;
1983 gcc_checking_assert (!a->tree_form && !b->tree_form);
1984 if (a == b)
1985 return false;
1987 while (b_elt)
1989 /* If A lags behind B, just advance it. */
1990 if (!a_elt || a_elt->indx == b_elt->indx)
1992 changed = bitmap_elt_ior (a, a_elt, a_prev, a_elt, b_elt, changed);
1993 b_elt = b_elt->next;
1995 else if (a_elt->indx > b_elt->indx)
1997 changed = bitmap_elt_copy (a, NULL, a_prev, b_elt, changed);
1998 b_elt = b_elt->next;
2001 a_prev = *a_prev_pnext;
2002 a_prev_pnext = &a_prev->next;
2003 a_elt = *a_prev_pnext;
2006 gcc_checking_assert (!a->current == !a->first);
2007 if (a->current)
2008 a->indx = a->current->indx;
2009 return changed;
2012 /* DST = A ^ B */
2014 void
2015 bitmap_xor (bitmap dst, const_bitmap a, const_bitmap b)
2017 bitmap_element *dst_elt = dst->first;
2018 const bitmap_element *a_elt = a->first;
2019 const bitmap_element *b_elt = b->first;
2020 bitmap_element *dst_prev = NULL;
2022 gcc_checking_assert (!dst->tree_form && !a->tree_form && !b->tree_form);
2023 gcc_assert (dst != a && dst != b);
2025 if (a == b)
2027 bitmap_clear (dst);
2028 return;
2031 while (a_elt || b_elt)
2033 if (a_elt && b_elt && a_elt->indx == b_elt->indx)
2035 /* Matching elts, generate A ^ B. */
2036 unsigned ix;
2037 BITMAP_WORD ior = 0;
2039 if (!dst_elt)
2040 dst_elt = bitmap_list_insert_element_after (dst, dst_prev,
2041 a_elt->indx);
2042 else
2043 dst_elt->indx = a_elt->indx;
2044 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
2046 BITMAP_WORD r = a_elt->bits[ix] ^ b_elt->bits[ix];
2048 ior |= r;
2049 dst_elt->bits[ix] = r;
2051 a_elt = a_elt->next;
2052 b_elt = b_elt->next;
2053 if (ior)
2055 dst_prev = dst_elt;
2056 dst_elt = dst_elt->next;
2059 else
2061 /* Copy a single element. */
2062 const bitmap_element *src;
2064 if (!b_elt || (a_elt && a_elt->indx < b_elt->indx))
2066 src = a_elt;
2067 a_elt = a_elt->next;
2069 else
2071 src = b_elt;
2072 b_elt = b_elt->next;
2075 if (!dst_elt)
2076 dst_elt = bitmap_list_insert_element_after (dst, dst_prev,
2077 src->indx);
2078 else
2079 dst_elt->indx = src->indx;
2080 memcpy (dst_elt->bits, src->bits, sizeof (dst_elt->bits));
2081 dst_prev = dst_elt;
2082 dst_elt = dst_elt->next;
2085 /* Ensure that dst->current is valid. */
2086 dst->current = dst->first;
2087 bitmap_elt_clear_from (dst, dst_elt);
2088 gcc_checking_assert (!dst->current == !dst->first);
2089 if (dst->current)
2090 dst->indx = dst->current->indx;
2093 /* A ^= B */
2095 void
2096 bitmap_xor_into (bitmap a, const_bitmap b)
2098 bitmap_element *a_elt = a->first;
2099 const bitmap_element *b_elt = b->first;
2100 bitmap_element *a_prev = NULL;
2102 gcc_checking_assert (!a->tree_form && !b->tree_form);
2104 if (a == b)
2106 bitmap_clear (a);
2107 return;
2110 while (b_elt)
2112 if (!a_elt || b_elt->indx < a_elt->indx)
2114 /* Copy b_elt. */
2115 bitmap_element *dst = bitmap_list_insert_element_after (a, a_prev,
2116 b_elt->indx);
2117 memcpy (dst->bits, b_elt->bits, sizeof (dst->bits));
2118 a_prev = dst;
2119 b_elt = b_elt->next;
2121 else if (a_elt->indx < b_elt->indx)
2123 a_prev = a_elt;
2124 a_elt = a_elt->next;
2126 else
2128 /* Matching elts, generate A ^= B. */
2129 unsigned ix;
2130 BITMAP_WORD ior = 0;
2131 bitmap_element *next = a_elt->next;
2133 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
2135 BITMAP_WORD r = a_elt->bits[ix] ^ b_elt->bits[ix];
2137 ior |= r;
2138 a_elt->bits[ix] = r;
2140 b_elt = b_elt->next;
2141 if (ior)
2142 a_prev = a_elt;
2143 else
2144 bitmap_list_unlink_element (a, a_elt);
2145 a_elt = next;
2148 gcc_checking_assert (!a->current == !a->first);
2149 if (a->current)
2150 a->indx = a->current->indx;
2153 /* Return true if two bitmaps are identical.
2154 We do not bother with a check for pointer equality, as that never
2155 occurs in practice. */
2157 bool
2158 bitmap_equal_p (const_bitmap a, const_bitmap b)
2160 const bitmap_element *a_elt;
2161 const bitmap_element *b_elt;
2162 unsigned ix;
2164 gcc_checking_assert (!a->tree_form && !b->tree_form);
2166 for (a_elt = a->first, b_elt = b->first;
2167 a_elt && b_elt;
2168 a_elt = a_elt->next, b_elt = b_elt->next)
2170 if (a_elt->indx != b_elt->indx)
2171 return false;
2172 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
2173 if (a_elt->bits[ix] != b_elt->bits[ix])
2174 return false;
2176 return !a_elt && !b_elt;
2179 /* Return true if A AND B is not empty. */
2181 bool
2182 bitmap_intersect_p (const_bitmap a, const_bitmap b)
2184 const bitmap_element *a_elt;
2185 const bitmap_element *b_elt;
2186 unsigned ix;
2188 gcc_checking_assert (!a->tree_form && !b->tree_form);
2190 for (a_elt = a->first, b_elt = b->first;
2191 a_elt && b_elt;)
2193 if (a_elt->indx < b_elt->indx)
2194 a_elt = a_elt->next;
2195 else if (b_elt->indx < a_elt->indx)
2196 b_elt = b_elt->next;
2197 else
2199 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
2200 if (a_elt->bits[ix] & b_elt->bits[ix])
2201 return true;
2202 a_elt = a_elt->next;
2203 b_elt = b_elt->next;
2206 return false;
2209 /* Return true if A AND NOT B is not empty. */
2211 bool
2212 bitmap_intersect_compl_p (const_bitmap a, const_bitmap b)
2214 const bitmap_element *a_elt;
2215 const bitmap_element *b_elt;
2216 unsigned ix;
2218 gcc_checking_assert (!a->tree_form && !b->tree_form);
2220 for (a_elt = a->first, b_elt = b->first;
2221 a_elt && b_elt;)
2223 if (a_elt->indx < b_elt->indx)
2224 return true;
2225 else if (b_elt->indx < a_elt->indx)
2226 b_elt = b_elt->next;
2227 else
2229 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
2230 if (a_elt->bits[ix] & ~b_elt->bits[ix])
2231 return true;
2232 a_elt = a_elt->next;
2233 b_elt = b_elt->next;
2236 return a_elt != NULL;
2240 /* DST = A | (FROM1 & ~FROM2). Return true if DST changes. */
2242 bool
2243 bitmap_ior_and_compl (bitmap dst, const_bitmap a, const_bitmap b, const_bitmap kill)
2245 bool changed = false;
2247 bitmap_element *dst_elt = dst->first;
2248 const bitmap_element *a_elt = a->first;
2249 const bitmap_element *b_elt = b->first;
2250 const bitmap_element *kill_elt = kill->first;
2251 bitmap_element *dst_prev = NULL;
2252 bitmap_element **dst_prev_pnext = &dst->first;
2254 gcc_checking_assert (!dst->tree_form && !a->tree_form && !b->tree_form
2255 && !kill->tree_form);
2256 gcc_assert (dst != a && dst != b && dst != kill);
2258 /* Special cases. We don't bother checking for bitmap_equal_p (b, kill). */
2259 if (b == kill || bitmap_empty_p (b))
2261 changed = !bitmap_equal_p (dst, a);
2262 if (changed)
2263 bitmap_copy (dst, a);
2264 return changed;
2266 if (bitmap_empty_p (kill))
2267 return bitmap_ior (dst, a, b);
2268 if (bitmap_empty_p (a))
2269 return bitmap_and_compl (dst, b, kill);
2271 while (a_elt || b_elt)
2273 bool new_element = false;
2275 if (b_elt)
2276 while (kill_elt && kill_elt->indx < b_elt->indx)
2277 kill_elt = kill_elt->next;
2279 if (b_elt && kill_elt && kill_elt->indx == b_elt->indx
2280 && (!a_elt || a_elt->indx >= b_elt->indx))
2282 bitmap_element tmp_elt;
2283 unsigned ix;
2285 BITMAP_WORD ior = 0;
2286 tmp_elt.indx = b_elt->indx;
2287 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
2289 BITMAP_WORD r = b_elt->bits[ix] & ~kill_elt->bits[ix];
2290 ior |= r;
2291 tmp_elt.bits[ix] = r;
2294 if (ior)
2296 changed = bitmap_elt_ior (dst, dst_elt, dst_prev,
2297 a_elt, &tmp_elt, changed);
2298 new_element = true;
2299 if (a_elt && a_elt->indx == b_elt->indx)
2300 a_elt = a_elt->next;
2303 b_elt = b_elt->next;
2304 kill_elt = kill_elt->next;
2306 else
2308 changed = bitmap_elt_ior (dst, dst_elt, dst_prev,
2309 a_elt, b_elt, changed);
2310 new_element = true;
2312 if (a_elt && b_elt && a_elt->indx == b_elt->indx)
2314 a_elt = a_elt->next;
2315 b_elt = b_elt->next;
2317 else
2319 if (a_elt && (!b_elt || a_elt->indx <= b_elt->indx))
2320 a_elt = a_elt->next;
2321 else if (b_elt && (!a_elt || b_elt->indx <= a_elt->indx))
2322 b_elt = b_elt->next;
2326 if (new_element)
2328 dst_prev = *dst_prev_pnext;
2329 dst_prev_pnext = &dst_prev->next;
2330 dst_elt = *dst_prev_pnext;
2334 if (dst_elt)
2336 changed = true;
2337 /* Ensure that dst->current is valid. */
2338 dst->current = dst->first;
2339 bitmap_elt_clear_from (dst, dst_elt);
2341 gcc_checking_assert (!dst->current == !dst->first);
2342 if (dst->current)
2343 dst->indx = dst->current->indx;
2345 return changed;
2348 /* A |= (B & ~C). Return true if A changes. */
2350 bool
2351 bitmap_ior_and_compl_into (bitmap a, const_bitmap b, const_bitmap c)
2353 bitmap_head tmp;
2354 bool changed;
2356 gcc_checking_assert (!a->tree_form && !b->tree_form && !c->tree_form);
2358 bitmap_initialize (&tmp, &bitmap_default_obstack);
2359 bitmap_and_compl (&tmp, b, c);
2360 changed = bitmap_ior_into (a, &tmp);
2361 bitmap_clear (&tmp);
2363 return changed;
2366 /* A |= (B & C). Return true if A changes. */
2368 bool
2369 bitmap_ior_and_into (bitmap a, const_bitmap b, const_bitmap c)
2371 bitmap_element *a_elt = a->first;
2372 const bitmap_element *b_elt = b->first;
2373 const bitmap_element *c_elt = c->first;
2374 bitmap_element and_elt;
2375 bitmap_element *a_prev = NULL;
2376 bitmap_element **a_prev_pnext = &a->first;
2377 bool changed = false;
2378 unsigned ix;
2380 gcc_checking_assert (!a->tree_form && !b->tree_form && !c->tree_form);
2382 if (b == c)
2383 return bitmap_ior_into (a, b);
2384 if (bitmap_empty_p (b) || bitmap_empty_p (c))
2385 return false;
2387 and_elt.indx = -1;
2388 while (b_elt && c_elt)
2390 BITMAP_WORD overall;
2392 /* Find a common item of B and C. */
2393 while (b_elt->indx != c_elt->indx)
2395 if (b_elt->indx < c_elt->indx)
2397 b_elt = b_elt->next;
2398 if (!b_elt)
2399 goto done;
2401 else
2403 c_elt = c_elt->next;
2404 if (!c_elt)
2405 goto done;
2409 overall = 0;
2410 and_elt.indx = b_elt->indx;
2411 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
2413 and_elt.bits[ix] = b_elt->bits[ix] & c_elt->bits[ix];
2414 overall |= and_elt.bits[ix];
2417 b_elt = b_elt->next;
2418 c_elt = c_elt->next;
2419 if (!overall)
2420 continue;
2422 /* Now find a place to insert AND_ELT. */
2425 ix = a_elt ? a_elt->indx : and_elt.indx;
2426 if (ix == and_elt.indx)
2427 changed = bitmap_elt_ior (a, a_elt, a_prev, a_elt, &and_elt, changed);
2428 else if (ix > and_elt.indx)
2429 changed = bitmap_elt_copy (a, NULL, a_prev, &and_elt, changed);
2431 a_prev = *a_prev_pnext;
2432 a_prev_pnext = &a_prev->next;
2433 a_elt = *a_prev_pnext;
2435 /* If A lagged behind B/C, we advanced it so loop once more. */
2437 while (ix < and_elt.indx);
2440 done:
2441 gcc_checking_assert (!a->current == !a->first);
2442 if (a->current)
2443 a->indx = a->current->indx;
2444 return changed;
2447 /* Compute hash of bitmap (for purposes of hashing). */
2449 hashval_t
2450 bitmap_hash (const_bitmap head)
2452 const bitmap_element *ptr;
2453 BITMAP_WORD hash = 0;
2454 int ix;
2456 gcc_checking_assert (!head->tree_form);
2458 for (ptr = head->first; ptr; ptr = ptr->next)
2460 hash ^= ptr->indx;
2461 for (ix = 0; ix != BITMAP_ELEMENT_WORDS; ix++)
2462 hash ^= ptr->bits[ix];
2464 return (hashval_t)hash;
2468 /* Function to obtain a vector of bitmap elements in bit order from
2469 HEAD in tree view. */
2471 static void
2472 bitmap_tree_to_vec (vec<bitmap_element *> &elts, const_bitmap head)
2474 gcc_checking_assert (head->tree_form);
2475 auto_vec<bitmap_element *, 32> stack;
2476 bitmap_element *e = head->first;
2477 while (true)
2479 while (e != NULL)
2481 stack.safe_push (e);
2482 e = e->prev;
2484 if (stack.is_empty ())
2485 break;
2487 e = stack.pop ();
2488 elts.safe_push (e);
2489 e = e->next;
2493 /* Debugging function to print out the contents of a bitmap element. */
2495 DEBUG_FUNCTION void
2496 debug_bitmap_elt_file (FILE *file, const bitmap_element *ptr)
2498 unsigned int i, j, col = 26;
2500 fprintf (file, "\t" HOST_PTR_PRINTF " next = " HOST_PTR_PRINTF
2501 " prev = " HOST_PTR_PRINTF " indx = %u\n\t\tbits = {",
2502 (const void*) ptr, (const void*) ptr->next,
2503 (const void*) ptr->prev, ptr->indx);
2505 for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
2506 for (j = 0; j < BITMAP_WORD_BITS; j++)
2507 if ((ptr->bits[i] >> j) & 1)
2509 if (col > 70)
2511 fprintf (file, "\n\t\t\t");
2512 col = 24;
2515 fprintf (file, " %u", (ptr->indx * BITMAP_ELEMENT_ALL_BITS
2516 + i * BITMAP_WORD_BITS + j));
2517 col += 4;
2520 fprintf (file, " }\n");
2523 /* Debugging function to print out the contents of a bitmap. */
2525 DEBUG_FUNCTION void
2526 debug_bitmap_file (FILE *file, const_bitmap head)
2528 const bitmap_element *ptr;
2530 fprintf (file, "\nfirst = " HOST_PTR_PRINTF
2531 " current = " HOST_PTR_PRINTF " indx = %u\n",
2532 (void *) head->first, (void *) head->current, head->indx);
2534 if (head->tree_form)
2536 auto_vec<bitmap_element *, 32> elts;
2537 bitmap_tree_to_vec (elts, head);
2538 for (unsigned i = 0; i < elts.length (); ++i)
2539 debug_bitmap_elt_file (file, elts[i]);
2541 else
2542 for (ptr = head->first; ptr; ptr = ptr->next)
2543 debug_bitmap_elt_file (file, ptr);
2546 /* Function to be called from the debugger to print the contents
2547 of a bitmap. */
2549 DEBUG_FUNCTION void
2550 debug_bitmap (const_bitmap head)
2552 debug_bitmap_file (stderr, head);
2555 /* Function to print out the contents of a bitmap. Unlike debug_bitmap_file,
2556 it does not print anything but the bits. */
2558 DEBUG_FUNCTION void
2559 bitmap_print (FILE *file, const_bitmap head, const char *prefix,
2560 const char *suffix)
2562 const char *comma = "";
2563 unsigned i;
2565 fputs (prefix, file);
2566 if (head->tree_form)
2568 auto_vec<bitmap_element *, 32> elts;
2569 bitmap_tree_to_vec (elts, head);
2570 for (i = 0; i < elts.length (); ++i)
2571 for (unsigned ix = 0; ix != BITMAP_ELEMENT_WORDS; ++ix)
2573 BITMAP_WORD word = elts[i]->bits[ix];
2574 for (unsigned bit = 0; bit != BITMAP_WORD_BITS; ++bit)
2575 if (word & ((BITMAP_WORD)1 << bit))
2577 fprintf (file, "%s%d", comma,
2578 (bit + BITMAP_WORD_BITS * ix
2579 + elts[i]->indx * BITMAP_ELEMENT_ALL_BITS));
2580 comma = ", ";
2584 else
2586 bitmap_iterator bi;
2587 EXECUTE_IF_SET_IN_BITMAP (head, 0, i, bi)
2589 fprintf (file, "%s%d", comma, i);
2590 comma = ", ";
2593 fputs (suffix, file);
2596 /* Output per-bitmap memory usage statistics. */
2597 void
2598 dump_bitmap_statistics (void)
2600 if (!GATHER_STATISTICS)
2601 return;
2603 bitmap_mem_desc.dump (BITMAP_ORIGIN);
2606 DEBUG_FUNCTION void
2607 debug (const bitmap_head &ref)
2609 dump_bitmap (stderr, &ref);
2612 DEBUG_FUNCTION void
2613 debug (const bitmap_head *ptr)
2615 if (ptr)
2616 debug (*ptr);
2617 else
2618 fprintf (stderr, "<nil>\n");
2621 void
2622 bitmap_head::dump ()
2624 debug (this);
2627 #if CHECKING_P
2629 namespace selftest {
2631 /* Selftests for bitmaps. */
2633 /* Freshly-created bitmaps ought to be empty. */
2635 static void
2636 test_gc_alloc ()
2638 bitmap b = bitmap_gc_alloc ();
2639 ASSERT_TRUE (bitmap_empty_p (b));
2642 /* Verify bitmap_set_range. */
2644 static void
2645 test_set_range ()
2647 bitmap b = bitmap_gc_alloc ();
2648 ASSERT_TRUE (bitmap_empty_p (b));
2650 bitmap_set_range (b, 7, 5);
2651 ASSERT_FALSE (bitmap_empty_p (b));
2652 ASSERT_EQ (5, bitmap_count_bits (b));
2654 /* Verify bitmap_bit_p at the boundaries. */
2655 ASSERT_FALSE (bitmap_bit_p (b, 6));
2656 ASSERT_TRUE (bitmap_bit_p (b, 7));
2657 ASSERT_TRUE (bitmap_bit_p (b, 11));
2658 ASSERT_FALSE (bitmap_bit_p (b, 12));
2661 /* Verify splitting a range into two pieces using bitmap_clear_bit. */
2663 static void
2664 test_clear_bit_in_middle ()
2666 bitmap b = bitmap_gc_alloc ();
2668 /* Set b to [100..200]. */
2669 bitmap_set_range (b, 100, 100);
2670 ASSERT_EQ (100, bitmap_count_bits (b));
2672 /* Clear a bit in the middle. */
2673 bool changed = bitmap_clear_bit (b, 150);
2674 ASSERT_TRUE (changed);
2675 ASSERT_EQ (99, bitmap_count_bits (b));
2676 ASSERT_TRUE (bitmap_bit_p (b, 149));
2677 ASSERT_FALSE (bitmap_bit_p (b, 150));
2678 ASSERT_TRUE (bitmap_bit_p (b, 151));
2681 /* Verify bitmap_copy. */
2683 static void
2684 test_copying ()
2686 bitmap src = bitmap_gc_alloc ();
2687 bitmap_set_range (src, 40, 10);
2689 bitmap dst = bitmap_gc_alloc ();
2690 ASSERT_FALSE (bitmap_equal_p (src, dst));
2691 bitmap_copy (dst, src);
2692 ASSERT_TRUE (bitmap_equal_p (src, dst));
2694 /* Verify that we can make them unequal again... */
2695 bitmap_set_range (src, 70, 5);
2696 ASSERT_FALSE (bitmap_equal_p (src, dst));
2698 /* ...and that changing src after the copy didn't affect
2699 the other: */
2700 ASSERT_FALSE (bitmap_bit_p (dst, 70));
2703 /* Verify bitmap_single_bit_set_p. */
2705 static void
2706 test_bitmap_single_bit_set_p ()
2708 bitmap b = bitmap_gc_alloc ();
2710 ASSERT_FALSE (bitmap_single_bit_set_p (b));
2712 bitmap_set_range (b, 42, 1);
2713 ASSERT_TRUE (bitmap_single_bit_set_p (b));
2714 ASSERT_EQ (42, bitmap_first_set_bit (b));
2716 bitmap_set_range (b, 1066, 1);
2717 ASSERT_FALSE (bitmap_single_bit_set_p (b));
2718 ASSERT_EQ (42, bitmap_first_set_bit (b));
2720 bitmap_clear_range (b, 0, 100);
2721 ASSERT_TRUE (bitmap_single_bit_set_p (b));
2722 ASSERT_EQ (1066, bitmap_first_set_bit (b));
2725 /* Run all of the selftests within this file. */
2727 void
2728 bitmap_c_tests ()
2730 test_gc_alloc ();
2731 test_set_range ();
2732 test_clear_bit_in_middle ();
2733 test_copying ();
2734 test_bitmap_single_bit_set_p ();
2737 } // namespace selftest
2738 #endif /* CHECKING_P */
2740 #include "gt-bitmap.h"