[RS6000] PowerPC64 soft-float
[official-gcc.git] / gcc / bitmap.c
blob025594400f94e7bab583d18485b0c3ed3535d784
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 bitmap_element *bitmap_tree_listify_from (bitmap, bitmap_element *);
31 /* Register new bitmap. */
32 void
33 bitmap_register (bitmap b MEM_STAT_DECL)
35 bitmap_mem_desc.register_descriptor (b, BITMAP_ORIGIN, false
36 FINAL_PASS_MEM_STAT);
39 /* Account the overhead. */
40 static void
41 register_overhead (bitmap b, size_t amount)
43 if (bitmap_mem_desc.contains_descriptor_for_instance (b))
44 bitmap_mem_desc.register_instance_overhead (amount, b);
47 /* Global data */
48 bitmap_element bitmap_zero_bits; /* An element of all zero bits. */
49 bitmap_obstack bitmap_default_obstack; /* The default bitmap obstack. */
50 static int bitmap_default_obstack_depth;
51 static GTY((deletable)) bitmap_element *bitmap_ggc_free; /* Freelist of
52 GC'd elements. */
55 /* Bitmap memory management. */
57 /* Add ELT to the appropriate freelist. */
58 static inline void
59 bitmap_elem_to_freelist (bitmap head, bitmap_element *elt)
61 bitmap_obstack *bit_obstack = head->obstack;
63 if (GATHER_STATISTICS)
64 register_overhead (head, -((int)sizeof (bitmap_element)));
66 elt->next = NULL;
67 elt->indx = -1;
68 if (bit_obstack)
70 elt->prev = bit_obstack->elements;
71 bit_obstack->elements = elt;
73 else
75 elt->prev = bitmap_ggc_free;
76 bitmap_ggc_free = elt;
80 /* Allocate a bitmap element. The bits are cleared, but nothing else is. */
82 static inline bitmap_element *
83 bitmap_element_allocate (bitmap head)
85 bitmap_element *element;
86 bitmap_obstack *bit_obstack = head->obstack;
88 if (bit_obstack)
90 element = bit_obstack->elements;
92 if (element)
93 /* Use up the inner list first before looking at the next
94 element of the outer list. */
95 if (element->next)
97 bit_obstack->elements = element->next;
98 bit_obstack->elements->prev = element->prev;
100 else
101 /* Inner list was just a singleton. */
102 bit_obstack->elements = element->prev;
103 else
104 element = XOBNEW (&bit_obstack->obstack, bitmap_element);
106 else
108 element = bitmap_ggc_free;
109 if (element)
110 /* Use up the inner list first before looking at the next
111 element of the outer list. */
112 if (element->next)
114 bitmap_ggc_free = element->next;
115 bitmap_ggc_free->prev = element->prev;
117 else
118 /* Inner list was just a singleton. */
119 bitmap_ggc_free = element->prev;
120 else
121 element = ggc_alloc<bitmap_element> ();
124 if (GATHER_STATISTICS)
125 register_overhead (head, sizeof (bitmap_element));
127 memset (element->bits, 0, sizeof (element->bits));
129 return element;
132 /* Remove ELT and all following elements from bitmap HEAD.
133 Put the released elements in the freelist for HEAD. */
135 void
136 bitmap_elt_clear_from (bitmap head, bitmap_element *elt)
138 bitmap_element *prev;
139 bitmap_obstack *bit_obstack = head->obstack;
141 if (!elt)
142 return;
144 if (head->tree_form)
145 elt = bitmap_tree_listify_from (head, elt);
147 if (GATHER_STATISTICS)
149 int n = 0;
150 for (prev = elt; prev; prev = prev->next)
151 n++;
152 register_overhead (head, -sizeof (bitmap_element) * n);
155 prev = elt->prev;
156 if (prev)
158 prev->next = NULL;
159 if (head->current->indx > prev->indx)
161 head->current = prev;
162 head->indx = prev->indx;
165 else
167 head->first = NULL;
168 head->current = NULL;
169 head->indx = 0;
172 /* Put the entire list onto the freelist in one operation. */
173 if (bit_obstack)
175 elt->prev = bit_obstack->elements;
176 bit_obstack->elements = elt;
178 else
180 elt->prev = bitmap_ggc_free;
181 bitmap_ggc_free = elt;
185 /* Linked-list view of bitmaps.
187 In this representation, the bitmap elements form a double-linked list
188 with elements sorted by increasing index. */
190 /* Link the bitmap element into the current bitmap linked list. */
192 static inline void
193 bitmap_list_link_element (bitmap head, bitmap_element *element)
195 unsigned int indx = element->indx;
196 bitmap_element *ptr;
198 gcc_checking_assert (!head->tree_form);
200 /* If this is the first and only element, set it in. */
201 if (head->first == 0)
203 element->next = element->prev = 0;
204 head->first = element;
207 /* If this index is less than that of the current element, it goes someplace
208 before the current element. */
209 else if (indx < head->indx)
211 for (ptr = head->current;
212 ptr->prev != 0 && ptr->prev->indx > indx;
213 ptr = ptr->prev)
216 if (ptr->prev)
217 ptr->prev->next = element;
218 else
219 head->first = element;
221 element->prev = ptr->prev;
222 element->next = ptr;
223 ptr->prev = element;
226 /* Otherwise, it must go someplace after the current element. */
227 else
229 for (ptr = head->current;
230 ptr->next != 0 && ptr->next->indx < indx;
231 ptr = ptr->next)
234 if (ptr->next)
235 ptr->next->prev = element;
237 element->next = ptr->next;
238 element->prev = ptr;
239 ptr->next = element;
242 /* Set up so this is the first element searched. */
243 head->current = element;
244 head->indx = indx;
247 /* Unlink the bitmap element from the current bitmap linked list,
248 and return it to the freelist. */
250 static inline void
251 bitmap_list_unlink_element (bitmap head, bitmap_element *element)
253 bitmap_element *next = element->next;
254 bitmap_element *prev = element->prev;
256 gcc_checking_assert (!head->tree_form);
258 if (prev)
259 prev->next = next;
261 if (next)
262 next->prev = prev;
264 if (head->first == element)
265 head->first = next;
267 /* Since the first thing we try is to insert before current,
268 make current the next entry in preference to the previous. */
269 if (head->current == element)
271 head->current = next != 0 ? next : prev;
272 if (head->current)
273 head->indx = head->current->indx;
274 else
275 head->indx = 0;
278 bitmap_elem_to_freelist (head, element);
281 /* Insert a new uninitialized element into bitmap HEAD after element
282 ELT. If ELT is NULL, insert the element at the start. Return the
283 new element. */
285 static bitmap_element *
286 bitmap_list_insert_element_after (bitmap head,
287 bitmap_element *elt, unsigned int indx)
289 bitmap_element *node = bitmap_element_allocate (head);
290 node->indx = indx;
292 gcc_checking_assert (!head->tree_form);
294 if (!elt)
296 if (!head->current)
298 head->current = node;
299 head->indx = indx;
301 node->next = head->first;
302 if (node->next)
303 node->next->prev = node;
304 head->first = node;
305 node->prev = NULL;
307 else
309 gcc_checking_assert (head->current);
310 node->next = elt->next;
311 if (node->next)
312 node->next->prev = node;
313 elt->next = node;
314 node->prev = elt;
316 return node;
319 /* Return the element for INDX, or NULL if the element doesn't exist.
320 Update the `current' field even if we can't find an element that
321 would hold the bitmap's bit to make eventual allocation
322 faster. */
324 static inline bitmap_element *
325 bitmap_list_find_element (bitmap head, unsigned int indx)
327 bitmap_element *element;
329 if (head->current == NULL
330 || head->indx == indx)
331 return head->current;
333 if (head->current == head->first
334 && head->first->next == NULL)
335 return NULL;
337 /* Usage can be NULL due to allocated bitmaps for which we do not
338 call initialize function. */
339 bitmap_usage *usage = NULL;
340 if (GATHER_STATISTICS)
341 usage = bitmap_mem_desc.get_descriptor_for_instance (head);
343 /* This bitmap has more than one element, and we're going to look
344 through the elements list. Count that as a search. */
345 if (GATHER_STATISTICS && usage)
346 usage->m_nsearches++;
348 if (head->indx < indx)
349 /* INDX is beyond head->indx. Search from head->current
350 forward. */
351 for (element = head->current;
352 element->next != 0 && element->indx < indx;
353 element = element->next)
355 if (GATHER_STATISTICS && usage)
356 usage->m_search_iter++;
359 else if (head->indx / 2 < indx)
360 /* INDX is less than head->indx and closer to head->indx than to
361 0. Search from head->current backward. */
362 for (element = head->current;
363 element->prev != 0 && element->indx > indx;
364 element = element->prev)
366 if (GATHER_STATISTICS && usage)
367 usage->m_search_iter++;
370 else
371 /* INDX is less than head->indx and closer to 0 than to
372 head->indx. Search from head->first forward. */
373 for (element = head->first;
374 element->next != 0 && element->indx < indx;
375 element = element->next)
377 if (GATHER_STATISTICS && usage)
378 usage->m_search_iter++;
381 /* `element' is the nearest to the one we want. If it's not the one we
382 want, the one we want doesn't exist. */
383 gcc_checking_assert (element != NULL);
384 head->current = element;
385 head->indx = element->indx;
386 if (element->indx != indx)
387 element = 0;
388 return element;
392 /* Splay-tree view of bitmaps.
394 This is an almost one-to-one the implementatin of the simple top-down
395 splay tree in Sleator and Tarjan's "Self-adjusting Binary Search Trees".
396 It is probably not the most efficient form of splay trees, but it should
397 be good enough to experiment with this idea of bitmaps-as-trees.
399 For all functions below, the variable or function argument "t" is a node
400 in the tree, and "e" is a temporary or new node in the tree. The rest
401 is sufficiently straigh-forward (and very well explained in the paper)
402 that comment would only clutter things. */
404 static inline void
405 bitmap_tree_link_left (bitmap_element * &t, bitmap_element * &l)
407 l->next = t;
408 l = t;
409 t = t->next;
412 static inline void
413 bitmap_tree_link_right (bitmap_element * &t, bitmap_element * &r)
415 r->prev = t;
416 r = t;
417 t = t->prev;
420 static inline void
421 bitmap_tree_rotate_left (bitmap_element * &t)
423 bitmap_element *e = t->next;
424 t->next = t->next->prev;
425 e->prev = t;
426 t = e;
429 static inline void
430 bitmap_tree_rotate_right (bitmap_element * &t)
432 bitmap_element *e = t->prev;
433 t->prev = t->prev->next;
434 e->next = t;
435 t = e;
438 static bitmap_element *
439 bitmap_tree_splay (bitmap head, bitmap_element *t, unsigned int indx)
441 bitmap_element N, *l, *r;
443 if (t == NULL)
444 return NULL;
446 bitmap_usage *usage = NULL;
447 if (GATHER_STATISTICS)
448 usage = bitmap_mem_desc.get_descriptor_for_instance (head);
450 N.prev = N.next = NULL;
451 l = r = &N;
453 while (indx != t->indx)
455 if (GATHER_STATISTICS && usage)
456 usage->m_search_iter++;
458 if (indx < t->indx)
460 if (t->prev != NULL && indx < t->prev->indx)
461 bitmap_tree_rotate_right (t);
462 if (t->prev == NULL)
463 break;
464 bitmap_tree_link_right (t, r);
466 else if (indx > t->indx)
468 if (t->next != NULL && indx > t->next->indx)
469 bitmap_tree_rotate_left (t);
470 if (t->next == NULL)
471 break;
472 bitmap_tree_link_left (t, l);
476 l->next = t->prev;
477 r->prev = t->next;
478 t->prev = N.next;
479 t->next = N.prev;
480 return t;
483 /* Link bitmap element E into the current bitmap splay tree. */
485 static inline void
486 bitmap_tree_link_element (bitmap head, bitmap_element *e)
488 if (head->first == NULL)
489 e->prev = e->next = NULL;
490 else
492 bitmap_element *t = bitmap_tree_splay (head, head->first, e->indx);
493 if (e->indx < t->indx)
495 e->prev = t->prev;
496 e->next = t;
497 t->prev = NULL;
499 else if (e->indx > t->indx)
501 e->next = t->next;
502 e->prev = t;
503 t->next = NULL;
505 else
506 gcc_unreachable ();
508 head->first = e;
509 head->current = e;
510 head->indx = e->indx;
513 /* Unlink bitmap element E from the current bitmap splay tree,
514 and return it to the freelist. */
516 static void
517 bitmap_tree_unlink_element (bitmap head, bitmap_element *e)
519 bitmap_element *t = bitmap_tree_splay (head, head->first, e->indx);
521 gcc_checking_assert (t == e);
523 if (e->prev == NULL)
524 t = e->next;
525 else
527 t = bitmap_tree_splay (head, e->prev, e->indx);
528 t->next = e->next;
530 head->first = t;
531 head->current = t;
532 head->indx = (t != NULL) ? t->indx : 0;
534 bitmap_elem_to_freelist (head, e);
537 /* Return the element for INDX, or NULL if the element doesn't exist. */
539 static inline bitmap_element *
540 bitmap_tree_find_element (bitmap head, unsigned int indx)
542 if (head->current == NULL
543 || head->indx == indx)
544 return head->current;
546 /* Usage can be NULL due to allocated bitmaps for which we do not
547 call initialize function. */
548 bitmap_usage *usage = NULL;
549 if (GATHER_STATISTICS)
550 usage = bitmap_mem_desc.get_descriptor_for_instance (head);
552 /* This bitmap has more than one element, and we're going to look
553 through the elements list. Count that as a search. */
554 if (GATHER_STATISTICS && usage)
555 usage->m_nsearches++;
557 bitmap_element *element = bitmap_tree_splay (head, head->first, indx);
558 gcc_checking_assert (element != NULL);
559 head->first = element;
560 head->current = element;
561 head->indx = element->indx;
562 if (element->indx != indx)
563 element = 0;
564 return element;
567 /* Converting bitmap views from linked-list to tree and vice versa. */
569 /* Splice element E and all elements with a larger index from
570 bitmap HEAD, convert the spliced elements to the linked-list
571 view, and return the head of the list (which should be E again), */
573 static bitmap_element *
574 bitmap_tree_listify_from (bitmap head, bitmap_element *e)
576 bitmap_element *t, *erb;
578 /* Detach the right branch from E (all elements with indx > E->indx),
579 and splay E to the root. */
580 erb = e->next;
581 e->next = NULL;
582 t = bitmap_tree_splay (head, head->first, e->indx);
583 gcc_checking_assert (t == e);
585 /* Because E has no right branch, and we rotated it to the root,
586 the left branch is the new root. */
587 t = e->prev;
588 head->first = t;
589 head->current = t;
590 head->indx = (t != NULL) ? t->indx : 0;
592 /* Detach the tree from E, and re-attach the right branch of E. */
593 e->prev = NULL;
594 e->next = erb;
596 /* The tree is now valid again. Now we need to "un-tree" E.
597 It is imperative that a non-recursive implementation is used
598 for this, because splay trees have a worst case depth of O(N)
599 for a tree with N nodes. A recursive implementation could
600 result in a stack overflow for a sufficiently large, unbalanced
601 bitmap tree. */
603 auto_vec<bitmap_element *, 32> stack;
604 auto_vec<bitmap_element *, 32> sorted_elements;
605 bitmap_element *n = e;
607 while (true)
609 while (n != NULL)
611 stack.safe_push (n);
612 n = n->prev;
615 if (stack.is_empty ())
616 break;
618 n = stack.pop ();
619 sorted_elements.safe_push (n);
620 n = n->next;
623 gcc_assert (sorted_elements[0] == e);
625 bitmap_element *prev = NULL;
626 unsigned ix;
627 FOR_EACH_VEC_ELT (sorted_elements, ix, n)
629 if (prev != NULL)
630 prev->next = n;
631 n->prev = prev;
632 n->next = NULL;
633 prev = n;
636 return e;
639 /* Convert bitmap HEAD from splay-tree view to linked-list view. */
641 void
642 bitmap_list_view (bitmap head)
644 bitmap_element *ptr;
646 gcc_assert (head->tree_form);
648 ptr = head->first;
649 if (ptr)
651 while (ptr->prev)
652 bitmap_tree_rotate_right (ptr);
653 head->first = ptr;
654 head->first = bitmap_tree_listify_from (head, ptr);
657 head->tree_form = false;
660 /* Convert bitmap HEAD from linked-list view to splay-tree view.
661 This is simply a matter of dropping the prev or next pointers
662 and setting the tree_form flag. The tree will balance itself
663 if and when it is used. */
665 void
666 bitmap_tree_view (bitmap head)
668 bitmap_element *ptr;
670 gcc_assert (! head->tree_form);
672 ptr = head->first;
673 while (ptr)
675 ptr->prev = NULL;
676 ptr = ptr->next;
679 head->tree_form = true;
682 /* Clear a bitmap by freeing all its elements. */
684 void
685 bitmap_clear (bitmap head)
687 if (head->first == NULL)
688 return;
689 if (head->tree_form)
691 bitmap_element *e, *t;
692 for (e = head->first; e->prev; e = e->prev)
693 /* Loop to find the element with the smallest index. */ ;
694 t = bitmap_tree_splay (head, head->first, e->indx);
695 gcc_checking_assert (t == e);
696 head->first = t;
698 bitmap_elt_clear_from (head, head->first);
701 /* Initialize a bitmap obstack. If BIT_OBSTACK is NULL, initialize
702 the default bitmap obstack. */
704 void
705 bitmap_obstack_initialize (bitmap_obstack *bit_obstack)
707 if (!bit_obstack)
709 if (bitmap_default_obstack_depth++)
710 return;
711 bit_obstack = &bitmap_default_obstack;
714 #if !defined(__GNUC__) || (__GNUC__ < 2)
715 #define __alignof__(type) 0
716 #endif
718 bit_obstack->elements = NULL;
719 bit_obstack->heads = NULL;
720 obstack_specify_allocation (&bit_obstack->obstack, OBSTACK_CHUNK_SIZE,
721 __alignof__ (bitmap_element),
722 obstack_chunk_alloc,
723 obstack_chunk_free);
726 /* Release the memory from a bitmap obstack. If BIT_OBSTACK is NULL,
727 release the default bitmap obstack. */
729 void
730 bitmap_obstack_release (bitmap_obstack *bit_obstack)
732 if (!bit_obstack)
734 if (--bitmap_default_obstack_depth)
736 gcc_assert (bitmap_default_obstack_depth > 0);
737 return;
739 bit_obstack = &bitmap_default_obstack;
742 bit_obstack->elements = NULL;
743 bit_obstack->heads = NULL;
744 obstack_free (&bit_obstack->obstack, NULL);
747 /* Create a new bitmap on an obstack. If BIT_OBSTACK is NULL, create
748 it on the default bitmap obstack. */
750 bitmap
751 bitmap_alloc (bitmap_obstack *bit_obstack MEM_STAT_DECL)
753 bitmap map;
755 if (!bit_obstack)
756 bit_obstack = &bitmap_default_obstack;
757 map = bit_obstack->heads;
758 if (map)
759 bit_obstack->heads = (struct bitmap_head *) map->first;
760 else
761 map = XOBNEW (&bit_obstack->obstack, bitmap_head);
762 bitmap_initialize (map, bit_obstack PASS_MEM_STAT);
764 if (GATHER_STATISTICS)
765 register_overhead (map, sizeof (bitmap_head));
767 return map;
770 /* Create a new GCd bitmap. */
772 bitmap
773 bitmap_gc_alloc (ALONE_MEM_STAT_DECL)
775 bitmap map;
777 map = ggc_alloc<bitmap_head> ();
778 bitmap_initialize (map, NULL PASS_MEM_STAT);
780 if (GATHER_STATISTICS)
781 register_overhead (map, sizeof (bitmap_head));
783 return map;
786 /* Release an obstack allocated bitmap. */
788 void
789 bitmap_obstack_free (bitmap map)
791 if (map)
793 bitmap_clear (map);
794 map->first = (bitmap_element *) map->obstack->heads;
796 if (GATHER_STATISTICS)
797 register_overhead (map, -((int)sizeof (bitmap_head)));
799 map->obstack->heads = map;
804 /* Return nonzero if all bits in an element are zero. */
806 static inline int
807 bitmap_element_zerop (const bitmap_element *element)
809 #if BITMAP_ELEMENT_WORDS == 2
810 return (element->bits[0] | element->bits[1]) == 0;
811 #else
812 unsigned i;
814 for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
815 if (element->bits[i] != 0)
816 return 0;
818 return 1;
819 #endif
822 /* Copy a bitmap to another bitmap. */
824 void
825 bitmap_copy (bitmap to, const_bitmap from)
827 const bitmap_element *from_ptr;
828 bitmap_element *to_ptr = 0;
830 gcc_checking_assert (!to->tree_form && !from->tree_form);
832 bitmap_clear (to);
834 /* Copy elements in forward direction one at a time. */
835 for (from_ptr = from->first; from_ptr; from_ptr = from_ptr->next)
837 bitmap_element *to_elt = bitmap_element_allocate (to);
839 to_elt->indx = from_ptr->indx;
840 memcpy (to_elt->bits, from_ptr->bits, sizeof (to_elt->bits));
842 /* Here we have a special case of bitmap_list_link_element,
843 for the case where we know the links are being entered
844 in sequence. */
845 if (to_ptr == 0)
847 to->first = to->current = to_elt;
848 to->indx = from_ptr->indx;
849 to_elt->next = to_elt->prev = 0;
851 else
853 to_elt->prev = to_ptr;
854 to_elt->next = 0;
855 to_ptr->next = to_elt;
858 to_ptr = to_elt;
862 /* Move a bitmap to another bitmap. */
864 void
865 bitmap_move (bitmap to, bitmap from)
867 gcc_assert (to->obstack == from->obstack);
869 bitmap_clear (to);
871 *to = *from;
873 if (GATHER_STATISTICS)
875 size_t sz = 0;
876 for (bitmap_element *e = to->first; e; e = e->next)
877 sz += sizeof (bitmap_element);
878 register_overhead (to, sz);
879 register_overhead (from, -sz);
883 /* Clear a single bit in a bitmap. Return true if the bit changed. */
885 bool
886 bitmap_clear_bit (bitmap head, int bit)
888 unsigned int indx = bit / BITMAP_ELEMENT_ALL_BITS;
889 bitmap_element *ptr;
891 if (!head->tree_form)
892 ptr = bitmap_list_find_element (head, indx);
893 else
894 ptr = bitmap_tree_find_element (head, indx);
895 if (ptr != 0)
897 unsigned bit_num = bit % BITMAP_WORD_BITS;
898 unsigned word_num = bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
899 BITMAP_WORD bit_val = ((BITMAP_WORD) 1) << bit_num;
900 bool res = (ptr->bits[word_num] & bit_val) != 0;
901 if (res)
903 ptr->bits[word_num] &= ~bit_val;
904 /* If we cleared the entire word, free up the element. */
905 if (!ptr->bits[word_num]
906 && bitmap_element_zerop (ptr))
908 if (!head->tree_form)
909 bitmap_list_unlink_element (head, ptr);
910 else
911 bitmap_tree_unlink_element (head, ptr);
915 return res;
918 return false;
921 /* Set a single bit in a bitmap. Return true if the bit changed. */
923 bool
924 bitmap_set_bit (bitmap head, int bit)
926 unsigned indx = bit / BITMAP_ELEMENT_ALL_BITS;
927 bitmap_element *ptr;
928 if (!head->tree_form)
929 ptr = bitmap_list_find_element (head, indx);
930 else
931 ptr = bitmap_tree_find_element (head, indx);
932 unsigned word_num = bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
933 unsigned bit_num = bit % BITMAP_WORD_BITS;
934 BITMAP_WORD bit_val = ((BITMAP_WORD) 1) << bit_num;
936 if (ptr != 0)
938 bool res = (ptr->bits[word_num] & bit_val) == 0;
939 if (res)
940 ptr->bits[word_num] |= bit_val;
941 return res;
944 ptr = bitmap_element_allocate (head);
945 ptr->indx = bit / BITMAP_ELEMENT_ALL_BITS;
946 ptr->bits[word_num] = bit_val;
947 if (!head->tree_form)
948 bitmap_list_link_element (head, ptr);
949 else
950 bitmap_tree_link_element (head, ptr);
951 return true;
954 /* Return whether a bit is set within a bitmap. */
957 bitmap_bit_p (bitmap head, int bit)
959 unsigned int indx = bit / BITMAP_ELEMENT_ALL_BITS;
960 bitmap_element *ptr;
961 unsigned bit_num;
962 unsigned word_num;
964 if (!head->tree_form)
965 ptr = bitmap_list_find_element (head, indx);
966 else
967 ptr = bitmap_tree_find_element (head, indx);
968 if (ptr == 0)
969 return 0;
971 bit_num = bit % BITMAP_WORD_BITS;
972 word_num = bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
974 return (ptr->bits[word_num] >> bit_num) & 1;
977 #if GCC_VERSION < 3400
978 /* Table of number of set bits in a character, indexed by value of char. */
979 static const unsigned char popcount_table[] =
981 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,
982 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,
983 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,
984 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,
985 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,
986 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,
987 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,
988 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,
991 static unsigned long
992 bitmap_popcount (BITMAP_WORD a)
994 unsigned long ret = 0;
995 unsigned i;
997 /* Just do this the table way for now */
998 for (i = 0; i < BITMAP_WORD_BITS; i+= 8)
999 ret += popcount_table[(a >> i) & 0xff];
1000 return ret;
1002 #endif
1004 /* Count and return the number of bits set in the bitmap word BITS. */
1005 static unsigned long
1006 bitmap_count_bits_in_word (const BITMAP_WORD *bits)
1008 unsigned long count = 0;
1010 for (unsigned ix = 0; ix != BITMAP_ELEMENT_WORDS; ix++)
1012 #if GCC_VERSION >= 3400
1013 /* Note that popcountl matches BITMAP_WORD in type, so the actual size
1014 of BITMAP_WORD is not material. */
1015 count += __builtin_popcountl (bits[ix]);
1016 #else
1017 count += bitmap_popcount (bits[ix]);
1018 #endif
1020 return count;
1023 /* Count the number of bits set in the bitmap, and return it. */
1025 unsigned long
1026 bitmap_count_bits (const_bitmap a)
1028 unsigned long count = 0;
1029 const bitmap_element *elt;
1031 gcc_checking_assert (!a->tree_form);
1032 for (elt = a->first; elt; elt = elt->next)
1033 count += bitmap_count_bits_in_word (elt->bits);
1035 return count;
1038 /* Count the number of unique bits set in A and B and return it. */
1040 unsigned long
1041 bitmap_count_unique_bits (const_bitmap a, const_bitmap b)
1043 unsigned long count = 0;
1044 const bitmap_element *elt_a, *elt_b;
1046 for (elt_a = a->first, elt_b = b->first; elt_a && elt_b; )
1048 /* If we're at different indices, then count all the bits
1049 in the lower element. If we're at the same index, then
1050 count the bits in the IOR of the two elements. */
1051 if (elt_a->indx < elt_b->indx)
1053 count += bitmap_count_bits_in_word (elt_a->bits);
1054 elt_a = elt_a->next;
1056 else if (elt_b->indx < elt_a->indx)
1058 count += bitmap_count_bits_in_word (elt_b->bits);
1059 elt_b = elt_b->next;
1061 else
1063 BITMAP_WORD bits[BITMAP_ELEMENT_WORDS];
1064 for (unsigned ix = 0; ix != BITMAP_ELEMENT_WORDS; ix++)
1065 bits[ix] = elt_a->bits[ix] | elt_b->bits[ix];
1066 count += bitmap_count_bits_in_word (bits);
1067 elt_a = elt_a->next;
1068 elt_b = elt_b->next;
1071 return count;
1074 /* Return true if the bitmap has a single bit set. Otherwise return
1075 false. */
1077 bool
1078 bitmap_single_bit_set_p (const_bitmap a)
1080 unsigned long count = 0;
1081 const bitmap_element *elt;
1082 unsigned ix;
1084 if (bitmap_empty_p (a))
1085 return false;
1087 elt = a->first;
1089 /* As there are no completely empty bitmap elements, a second one
1090 means we have more than one bit set. */
1091 if (elt->next != NULL
1092 && (!a->tree_form || elt->prev != NULL))
1093 return false;
1095 for (ix = 0; ix != BITMAP_ELEMENT_WORDS; ix++)
1097 #if GCC_VERSION >= 3400
1098 /* Note that popcountl matches BITMAP_WORD in type, so the actual size
1099 of BITMAP_WORD is not material. */
1100 count += __builtin_popcountl (elt->bits[ix]);
1101 #else
1102 count += bitmap_popcount (elt->bits[ix]);
1103 #endif
1104 if (count > 1)
1105 return false;
1108 return count == 1;
1112 /* Return the bit number of the first set bit in the bitmap. The
1113 bitmap must be non-empty. */
1115 unsigned
1116 bitmap_first_set_bit (const_bitmap a)
1118 const bitmap_element *elt = a->first;
1119 unsigned bit_no;
1120 BITMAP_WORD word;
1121 unsigned ix;
1123 gcc_checking_assert (elt);
1125 if (a->tree_form)
1126 while (elt->prev)
1127 elt = elt->prev;
1129 bit_no = elt->indx * BITMAP_ELEMENT_ALL_BITS;
1130 for (ix = 0; ix != BITMAP_ELEMENT_WORDS; ix++)
1132 word = elt->bits[ix];
1133 if (word)
1134 goto found_bit;
1136 gcc_unreachable ();
1137 found_bit:
1138 bit_no += ix * BITMAP_WORD_BITS;
1140 #if GCC_VERSION >= 3004
1141 gcc_assert (sizeof (long) == sizeof (word));
1142 bit_no += __builtin_ctzl (word);
1143 #else
1144 /* Binary search for the first set bit. */
1145 #if BITMAP_WORD_BITS > 64
1146 #error "Fill out the table."
1147 #endif
1148 #if BITMAP_WORD_BITS > 32
1149 if (!(word & 0xffffffff))
1150 word >>= 32, bit_no += 32;
1151 #endif
1152 if (!(word & 0xffff))
1153 word >>= 16, bit_no += 16;
1154 if (!(word & 0xff))
1155 word >>= 8, bit_no += 8;
1156 if (!(word & 0xf))
1157 word >>= 4, bit_no += 4;
1158 if (!(word & 0x3))
1159 word >>= 2, bit_no += 2;
1160 if (!(word & 0x1))
1161 word >>= 1, bit_no += 1;
1163 gcc_checking_assert (word & 1);
1164 #endif
1165 return bit_no;
1168 /* Return the bit number of the first set bit in the bitmap. The
1169 bitmap must be non-empty. */
1171 unsigned
1172 bitmap_last_set_bit (const_bitmap a)
1174 const bitmap_element *elt;
1175 unsigned bit_no;
1176 BITMAP_WORD word;
1177 int ix;
1179 if (a->tree_form)
1180 elt = a->first;
1181 else
1182 elt = a->current ? a->current : a->first;
1183 gcc_checking_assert (elt);
1185 while (elt->next)
1186 elt = elt->next;
1188 bit_no = elt->indx * BITMAP_ELEMENT_ALL_BITS;
1189 for (ix = BITMAP_ELEMENT_WORDS - 1; ix >= 1; ix--)
1191 word = elt->bits[ix];
1192 if (word)
1193 goto found_bit;
1195 gcc_assert (elt->bits[ix] != 0);
1196 found_bit:
1197 bit_no += ix * BITMAP_WORD_BITS;
1198 #if GCC_VERSION >= 3004
1199 gcc_assert (sizeof (long) == sizeof (word));
1200 bit_no += BITMAP_WORD_BITS - __builtin_clzl (word) - 1;
1201 #else
1202 /* Hopefully this is a twos-complement host... */
1203 BITMAP_WORD x = word;
1204 x |= (x >> 1);
1205 x |= (x >> 2);
1206 x |= (x >> 4);
1207 x |= (x >> 8);
1208 x |= (x >> 16);
1209 #if BITMAP_WORD_BITS > 32
1210 x |= (x >> 32);
1211 #endif
1212 bit_no += bitmap_popcount (x) - 1;
1213 #endif
1215 return bit_no;
1219 /* DST = A & B. */
1221 void
1222 bitmap_and (bitmap dst, const_bitmap a, const_bitmap b)
1224 bitmap_element *dst_elt = dst->first;
1225 const bitmap_element *a_elt = a->first;
1226 const bitmap_element *b_elt = b->first;
1227 bitmap_element *dst_prev = NULL;
1229 gcc_checking_assert (!dst->tree_form && !a->tree_form && !b->tree_form);
1230 gcc_assert (dst != a && dst != b);
1232 if (a == b)
1234 bitmap_copy (dst, a);
1235 return;
1238 while (a_elt && b_elt)
1240 if (a_elt->indx < b_elt->indx)
1241 a_elt = a_elt->next;
1242 else if (b_elt->indx < a_elt->indx)
1243 b_elt = b_elt->next;
1244 else
1246 /* Matching elts, generate A & B. */
1247 unsigned ix;
1248 BITMAP_WORD ior = 0;
1250 if (!dst_elt)
1251 dst_elt = bitmap_list_insert_element_after (dst, dst_prev,
1252 a_elt->indx);
1253 else
1254 dst_elt->indx = a_elt->indx;
1255 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1257 BITMAP_WORD r = a_elt->bits[ix] & b_elt->bits[ix];
1259 dst_elt->bits[ix] = r;
1260 ior |= r;
1262 if (ior)
1264 dst_prev = dst_elt;
1265 dst_elt = dst_elt->next;
1267 a_elt = a_elt->next;
1268 b_elt = b_elt->next;
1271 /* Ensure that dst->current is valid. */
1272 dst->current = dst->first;
1273 bitmap_elt_clear_from (dst, dst_elt);
1274 gcc_checking_assert (!dst->current == !dst->first);
1275 if (dst->current)
1276 dst->indx = dst->current->indx;
1279 /* A &= B. Return true if A changed. */
1281 bool
1282 bitmap_and_into (bitmap a, const_bitmap b)
1284 bitmap_element *a_elt = a->first;
1285 const bitmap_element *b_elt = b->first;
1286 bitmap_element *next;
1287 bool changed = false;
1289 gcc_checking_assert (!a->tree_form && !b->tree_form);
1291 if (a == b)
1292 return false;
1294 while (a_elt && b_elt)
1296 if (a_elt->indx < b_elt->indx)
1298 next = a_elt->next;
1299 bitmap_list_unlink_element (a, a_elt);
1300 a_elt = next;
1301 changed = true;
1303 else if (b_elt->indx < a_elt->indx)
1304 b_elt = b_elt->next;
1305 else
1307 /* Matching elts, generate A &= B. */
1308 unsigned ix;
1309 BITMAP_WORD ior = 0;
1311 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1313 BITMAP_WORD r = a_elt->bits[ix] & b_elt->bits[ix];
1314 if (a_elt->bits[ix] != r)
1315 changed = true;
1316 a_elt->bits[ix] = r;
1317 ior |= r;
1319 next = a_elt->next;
1320 if (!ior)
1321 bitmap_list_unlink_element (a, a_elt);
1322 a_elt = next;
1323 b_elt = b_elt->next;
1327 if (a_elt)
1329 changed = true;
1330 bitmap_elt_clear_from (a, a_elt);
1333 gcc_checking_assert (!a->current == !a->first
1334 && (!a->current || a->indx == a->current->indx));
1336 return changed;
1340 /* Insert an element equal to SRC_ELT after DST_PREV, overwriting DST_ELT
1341 if non-NULL. CHANGED is true if the destination bitmap had already been
1342 changed; the new value of CHANGED is returned. */
1344 static inline bool
1345 bitmap_elt_copy (bitmap dst, bitmap_element *dst_elt, bitmap_element *dst_prev,
1346 const bitmap_element *src_elt, bool changed)
1348 if (!changed && dst_elt && dst_elt->indx == src_elt->indx)
1350 unsigned ix;
1352 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1353 if (src_elt->bits[ix] != dst_elt->bits[ix])
1355 dst_elt->bits[ix] = src_elt->bits[ix];
1356 changed = true;
1359 else
1361 changed = true;
1362 if (!dst_elt)
1363 dst_elt = bitmap_list_insert_element_after (dst, dst_prev,
1364 src_elt->indx);
1365 else
1366 dst_elt->indx = src_elt->indx;
1367 memcpy (dst_elt->bits, src_elt->bits, sizeof (dst_elt->bits));
1369 return changed;
1374 /* DST = A & ~B */
1376 bool
1377 bitmap_and_compl (bitmap dst, const_bitmap a, const_bitmap b)
1379 bitmap_element *dst_elt = dst->first;
1380 const bitmap_element *a_elt = a->first;
1381 const bitmap_element *b_elt = b->first;
1382 bitmap_element *dst_prev = NULL;
1383 bitmap_element **dst_prev_pnext = &dst->first;
1384 bool changed = false;
1386 gcc_checking_assert (!dst->tree_form && !a->tree_form && !b->tree_form);
1387 gcc_assert (dst != a && dst != b);
1389 if (a == b)
1391 changed = !bitmap_empty_p (dst);
1392 bitmap_clear (dst);
1393 return changed;
1396 while (a_elt)
1398 while (b_elt && b_elt->indx < a_elt->indx)
1399 b_elt = b_elt->next;
1401 if (!b_elt || b_elt->indx > a_elt->indx)
1403 changed = bitmap_elt_copy (dst, dst_elt, dst_prev, a_elt, changed);
1404 dst_prev = *dst_prev_pnext;
1405 dst_prev_pnext = &dst_prev->next;
1406 dst_elt = *dst_prev_pnext;
1407 a_elt = a_elt->next;
1410 else
1412 /* Matching elts, generate A & ~B. */
1413 unsigned ix;
1414 BITMAP_WORD ior = 0;
1416 if (!changed && dst_elt && dst_elt->indx == a_elt->indx)
1418 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1420 BITMAP_WORD r = a_elt->bits[ix] & ~b_elt->bits[ix];
1422 if (dst_elt->bits[ix] != r)
1424 changed = true;
1425 dst_elt->bits[ix] = r;
1427 ior |= r;
1430 else
1432 bool new_element;
1433 if (!dst_elt || dst_elt->indx > a_elt->indx)
1435 dst_elt = bitmap_list_insert_element_after (dst, dst_prev,
1436 a_elt->indx);
1437 new_element = true;
1439 else
1441 dst_elt->indx = a_elt->indx;
1442 new_element = false;
1445 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1447 BITMAP_WORD r = a_elt->bits[ix] & ~b_elt->bits[ix];
1449 dst_elt->bits[ix] = r;
1450 ior |= r;
1453 if (ior)
1454 changed = true;
1455 else
1457 changed |= !new_element;
1458 bitmap_list_unlink_element (dst, dst_elt);
1459 dst_elt = *dst_prev_pnext;
1463 if (ior)
1465 dst_prev = *dst_prev_pnext;
1466 dst_prev_pnext = &dst_prev->next;
1467 dst_elt = *dst_prev_pnext;
1469 a_elt = a_elt->next;
1470 b_elt = b_elt->next;
1474 /* Ensure that dst->current is valid. */
1475 dst->current = dst->first;
1477 if (dst_elt)
1479 changed = true;
1480 bitmap_elt_clear_from (dst, dst_elt);
1482 gcc_checking_assert (!dst->current == !dst->first);
1483 if (dst->current)
1484 dst->indx = dst->current->indx;
1486 return changed;
1489 /* A &= ~B. Returns true if A changes */
1491 bool
1492 bitmap_and_compl_into (bitmap a, const_bitmap b)
1494 bitmap_element *a_elt = a->first;
1495 const bitmap_element *b_elt = b->first;
1496 bitmap_element *next;
1497 BITMAP_WORD changed = 0;
1499 gcc_checking_assert (!a->tree_form && !b->tree_form);
1501 if (a == b)
1503 if (bitmap_empty_p (a))
1504 return false;
1505 else
1507 bitmap_clear (a);
1508 return true;
1512 while (a_elt && b_elt)
1514 if (a_elt->indx < b_elt->indx)
1515 a_elt = a_elt->next;
1516 else if (b_elt->indx < a_elt->indx)
1517 b_elt = b_elt->next;
1518 else
1520 /* Matching elts, generate A &= ~B. */
1521 unsigned ix;
1522 BITMAP_WORD ior = 0;
1524 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1526 BITMAP_WORD cleared = a_elt->bits[ix] & b_elt->bits[ix];
1527 BITMAP_WORD r = a_elt->bits[ix] ^ cleared;
1529 a_elt->bits[ix] = r;
1530 changed |= cleared;
1531 ior |= r;
1533 next = a_elt->next;
1534 if (!ior)
1535 bitmap_list_unlink_element (a, a_elt);
1536 a_elt = next;
1537 b_elt = b_elt->next;
1540 gcc_checking_assert (!a->current == !a->first
1541 && (!a->current || a->indx == a->current->indx));
1542 return changed != 0;
1545 /* Set COUNT bits from START in HEAD. */
1546 void
1547 bitmap_set_range (bitmap head, unsigned int start, unsigned int count)
1549 unsigned int first_index, end_bit_plus1, last_index;
1550 bitmap_element *elt, *elt_prev;
1551 unsigned int i;
1553 gcc_checking_assert (!head->tree_form);
1555 if (!count)
1556 return;
1558 if (count == 1)
1560 bitmap_set_bit (head, start);
1561 return;
1564 first_index = start / BITMAP_ELEMENT_ALL_BITS;
1565 end_bit_plus1 = start + count;
1566 last_index = (end_bit_plus1 - 1) / BITMAP_ELEMENT_ALL_BITS;
1567 elt = bitmap_list_find_element (head, first_index);
1569 /* If bitmap_list_find_element returns zero, the current is the closest block
1570 to the result. Otherwise, just use bitmap_element_allocate to
1571 ensure ELT is set; in the loop below, ELT == NULL means "insert
1572 at the end of the bitmap". */
1573 if (!elt)
1575 elt = bitmap_element_allocate (head);
1576 elt->indx = first_index;
1577 bitmap_list_link_element (head, elt);
1580 gcc_checking_assert (elt->indx == first_index);
1581 elt_prev = elt->prev;
1582 for (i = first_index; i <= last_index; i++)
1584 unsigned elt_start_bit = i * BITMAP_ELEMENT_ALL_BITS;
1585 unsigned elt_end_bit_plus1 = elt_start_bit + BITMAP_ELEMENT_ALL_BITS;
1587 unsigned int first_word_to_mod;
1588 BITMAP_WORD first_mask;
1589 unsigned int last_word_to_mod;
1590 BITMAP_WORD last_mask;
1591 unsigned int ix;
1593 if (!elt || elt->indx != i)
1594 elt = bitmap_list_insert_element_after (head, elt_prev, i);
1596 if (elt_start_bit <= start)
1598 /* The first bit to turn on is somewhere inside this
1599 elt. */
1600 first_word_to_mod = (start - elt_start_bit) / BITMAP_WORD_BITS;
1602 /* This mask should have 1s in all bits >= start position. */
1603 first_mask =
1604 (((BITMAP_WORD) 1) << ((start % BITMAP_WORD_BITS))) - 1;
1605 first_mask = ~first_mask;
1607 else
1609 /* The first bit to turn on is below this start of this elt. */
1610 first_word_to_mod = 0;
1611 first_mask = ~(BITMAP_WORD) 0;
1614 if (elt_end_bit_plus1 <= end_bit_plus1)
1616 /* The last bit to turn on is beyond this elt. */
1617 last_word_to_mod = BITMAP_ELEMENT_WORDS - 1;
1618 last_mask = ~(BITMAP_WORD) 0;
1620 else
1622 /* The last bit to turn on is inside to this elt. */
1623 last_word_to_mod =
1624 (end_bit_plus1 - elt_start_bit) / BITMAP_WORD_BITS;
1626 /* The last mask should have 1s below the end bit. */
1627 last_mask =
1628 (((BITMAP_WORD) 1) << ((end_bit_plus1 % BITMAP_WORD_BITS))) - 1;
1631 if (first_word_to_mod == last_word_to_mod)
1633 BITMAP_WORD mask = first_mask & last_mask;
1634 elt->bits[first_word_to_mod] |= mask;
1636 else
1638 elt->bits[first_word_to_mod] |= first_mask;
1639 if (BITMAP_ELEMENT_WORDS > 2)
1640 for (ix = first_word_to_mod + 1; ix < last_word_to_mod; ix++)
1641 elt->bits[ix] = ~(BITMAP_WORD) 0;
1642 elt->bits[last_word_to_mod] |= last_mask;
1645 elt_prev = elt;
1646 elt = elt->next;
1649 head->current = elt ? elt : elt_prev;
1650 head->indx = head->current->indx;
1653 /* Clear COUNT bits from START in HEAD. */
1654 void
1655 bitmap_clear_range (bitmap head, unsigned int start, unsigned int count)
1657 unsigned int first_index, end_bit_plus1, last_index;
1658 bitmap_element *elt;
1660 gcc_checking_assert (!head->tree_form);
1662 if (!count)
1663 return;
1665 if (count == 1)
1667 bitmap_clear_bit (head, start);
1668 return;
1671 first_index = start / BITMAP_ELEMENT_ALL_BITS;
1672 end_bit_plus1 = start + count;
1673 last_index = (end_bit_plus1 - 1) / BITMAP_ELEMENT_ALL_BITS;
1674 elt = bitmap_list_find_element (head, first_index);
1676 /* If bitmap_list_find_element returns zero, the current is the closest block
1677 to the result. If the current is less than first index, find the
1678 next one. Otherwise, just set elt to be current. */
1679 if (!elt)
1681 if (head->current)
1683 if (head->indx < first_index)
1685 elt = head->current->next;
1686 if (!elt)
1687 return;
1689 else
1690 elt = head->current;
1692 else
1693 return;
1696 while (elt && (elt->indx <= last_index))
1698 bitmap_element * next_elt = elt->next;
1699 unsigned elt_start_bit = (elt->indx) * BITMAP_ELEMENT_ALL_BITS;
1700 unsigned elt_end_bit_plus1 = elt_start_bit + BITMAP_ELEMENT_ALL_BITS;
1703 if (elt_start_bit >= start && elt_end_bit_plus1 <= end_bit_plus1)
1704 /* Get rid of the entire elt and go to the next one. */
1705 bitmap_list_unlink_element (head, elt);
1706 else
1708 /* Going to have to knock out some bits in this elt. */
1709 unsigned int first_word_to_mod;
1710 BITMAP_WORD first_mask;
1711 unsigned int last_word_to_mod;
1712 BITMAP_WORD last_mask;
1713 unsigned int i;
1714 bool clear = true;
1716 if (elt_start_bit <= start)
1718 /* The first bit to turn off is somewhere inside this
1719 elt. */
1720 first_word_to_mod = (start - elt_start_bit) / BITMAP_WORD_BITS;
1722 /* This mask should have 1s in all bits >= start position. */
1723 first_mask =
1724 (((BITMAP_WORD) 1) << ((start % BITMAP_WORD_BITS))) - 1;
1725 first_mask = ~first_mask;
1727 else
1729 /* The first bit to turn off is below this start of this elt. */
1730 first_word_to_mod = 0;
1731 first_mask = 0;
1732 first_mask = ~first_mask;
1735 if (elt_end_bit_plus1 <= end_bit_plus1)
1737 /* The last bit to turn off is beyond this elt. */
1738 last_word_to_mod = BITMAP_ELEMENT_WORDS - 1;
1739 last_mask = 0;
1740 last_mask = ~last_mask;
1742 else
1744 /* The last bit to turn off is inside to this elt. */
1745 last_word_to_mod =
1746 (end_bit_plus1 - elt_start_bit) / BITMAP_WORD_BITS;
1748 /* The last mask should have 1s below the end bit. */
1749 last_mask =
1750 (((BITMAP_WORD) 1) << (((end_bit_plus1) % BITMAP_WORD_BITS))) - 1;
1754 if (first_word_to_mod == last_word_to_mod)
1756 BITMAP_WORD mask = first_mask & last_mask;
1757 elt->bits[first_word_to_mod] &= ~mask;
1759 else
1761 elt->bits[first_word_to_mod] &= ~first_mask;
1762 if (BITMAP_ELEMENT_WORDS > 2)
1763 for (i = first_word_to_mod + 1; i < last_word_to_mod; i++)
1764 elt->bits[i] = 0;
1765 elt->bits[last_word_to_mod] &= ~last_mask;
1767 for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
1768 if (elt->bits[i])
1770 clear = false;
1771 break;
1773 /* Check to see if there are any bits left. */
1774 if (clear)
1775 bitmap_list_unlink_element (head, elt);
1777 elt = next_elt;
1780 if (elt)
1782 head->current = elt;
1783 head->indx = head->current->indx;
1787 /* A = ~A & B. */
1789 void
1790 bitmap_compl_and_into (bitmap a, const_bitmap b)
1792 bitmap_element *a_elt = a->first;
1793 const bitmap_element *b_elt = b->first;
1794 bitmap_element *a_prev = NULL;
1795 bitmap_element *next;
1797 gcc_checking_assert (!a->tree_form && !b->tree_form);
1798 gcc_assert (a != b);
1800 if (bitmap_empty_p (a))
1802 bitmap_copy (a, b);
1803 return;
1805 if (bitmap_empty_p (b))
1807 bitmap_clear (a);
1808 return;
1811 while (a_elt || b_elt)
1813 if (!b_elt || (a_elt && a_elt->indx < b_elt->indx))
1815 /* A is before B. Remove A */
1816 next = a_elt->next;
1817 a_prev = a_elt->prev;
1818 bitmap_list_unlink_element (a, a_elt);
1819 a_elt = next;
1821 else if (!a_elt || b_elt->indx < a_elt->indx)
1823 /* B is before A. Copy B. */
1824 next = bitmap_list_insert_element_after (a, a_prev, b_elt->indx);
1825 memcpy (next->bits, b_elt->bits, sizeof (next->bits));
1826 a_prev = next;
1827 b_elt = b_elt->next;
1829 else
1831 /* Matching elts, generate A = ~A & B. */
1832 unsigned ix;
1833 BITMAP_WORD ior = 0;
1835 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1837 BITMAP_WORD cleared = a_elt->bits[ix] & b_elt->bits[ix];
1838 BITMAP_WORD r = b_elt->bits[ix] ^ cleared;
1840 a_elt->bits[ix] = r;
1841 ior |= r;
1843 next = a_elt->next;
1844 if (!ior)
1845 bitmap_list_unlink_element (a, a_elt);
1846 else
1847 a_prev = a_elt;
1848 a_elt = next;
1849 b_elt = b_elt->next;
1852 gcc_checking_assert (!a->current == !a->first
1853 && (!a->current || a->indx == a->current->indx));
1854 return;
1858 /* Insert an element corresponding to A_ELT | B_ELT after DST_PREV,
1859 overwriting DST_ELT if non-NULL. CHANGED is true if the destination bitmap
1860 had already been changed; the new value of CHANGED is returned. */
1862 static inline bool
1863 bitmap_elt_ior (bitmap dst, bitmap_element *dst_elt, bitmap_element *dst_prev,
1864 const bitmap_element *a_elt, const bitmap_element *b_elt,
1865 bool changed)
1867 gcc_assert (a_elt || b_elt);
1869 if (a_elt && b_elt && a_elt->indx == b_elt->indx)
1871 /* Matching elts, generate A | B. */
1872 unsigned ix;
1874 if (!changed && dst_elt && dst_elt->indx == a_elt->indx)
1876 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1878 BITMAP_WORD r = a_elt->bits[ix] | b_elt->bits[ix];
1879 if (r != dst_elt->bits[ix])
1881 dst_elt->bits[ix] = r;
1882 changed = true;
1886 else
1888 changed = true;
1889 if (!dst_elt)
1890 dst_elt = bitmap_list_insert_element_after (dst, dst_prev,
1891 a_elt->indx);
1892 else
1893 dst_elt->indx = a_elt->indx;
1894 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1896 BITMAP_WORD r = a_elt->bits[ix] | b_elt->bits[ix];
1897 dst_elt->bits[ix] = r;
1901 else
1903 /* Copy a single element. */
1904 const bitmap_element *src;
1906 if (!b_elt || (a_elt && a_elt->indx < b_elt->indx))
1907 src = a_elt;
1908 else
1909 src = b_elt;
1911 gcc_checking_assert (src);
1912 changed = bitmap_elt_copy (dst, dst_elt, dst_prev, src, changed);
1914 return changed;
1918 /* DST = A | B. Return true if DST changes. */
1920 bool
1921 bitmap_ior (bitmap dst, const_bitmap a, const_bitmap b)
1923 bitmap_element *dst_elt = dst->first;
1924 const bitmap_element *a_elt = a->first;
1925 const bitmap_element *b_elt = b->first;
1926 bitmap_element *dst_prev = NULL;
1927 bitmap_element **dst_prev_pnext = &dst->first;
1928 bool changed = false;
1930 gcc_checking_assert (!dst->tree_form && !a->tree_form && !b->tree_form);
1931 gcc_assert (dst != a && dst != b);
1933 while (a_elt || b_elt)
1935 changed = bitmap_elt_ior (dst, dst_elt, dst_prev, a_elt, b_elt, changed);
1937 if (a_elt && b_elt && a_elt->indx == b_elt->indx)
1939 a_elt = a_elt->next;
1940 b_elt = b_elt->next;
1942 else
1944 if (a_elt && (!b_elt || a_elt->indx <= b_elt->indx))
1945 a_elt = a_elt->next;
1946 else if (b_elt && (!a_elt || b_elt->indx <= a_elt->indx))
1947 b_elt = b_elt->next;
1950 dst_prev = *dst_prev_pnext;
1951 dst_prev_pnext = &dst_prev->next;
1952 dst_elt = *dst_prev_pnext;
1955 if (dst_elt)
1957 changed = true;
1958 /* Ensure that dst->current is valid. */
1959 dst->current = dst->first;
1960 bitmap_elt_clear_from (dst, dst_elt);
1962 gcc_checking_assert (!dst->current == !dst->first);
1963 if (dst->current)
1964 dst->indx = dst->current->indx;
1965 return changed;
1968 /* A |= B. Return true if A changes. */
1970 bool
1971 bitmap_ior_into (bitmap a, const_bitmap b)
1973 bitmap_element *a_elt = a->first;
1974 const bitmap_element *b_elt = b->first;
1975 bitmap_element *a_prev = NULL;
1976 bitmap_element **a_prev_pnext = &a->first;
1977 bool changed = false;
1979 gcc_checking_assert (!a->tree_form && !b->tree_form);
1980 if (a == b)
1981 return false;
1983 while (b_elt)
1985 /* If A lags behind B, just advance it. */
1986 if (!a_elt || a_elt->indx == b_elt->indx)
1988 changed = bitmap_elt_ior (a, a_elt, a_prev, a_elt, b_elt, changed);
1989 b_elt = b_elt->next;
1991 else if (a_elt->indx > b_elt->indx)
1993 changed = bitmap_elt_copy (a, NULL, a_prev, b_elt, changed);
1994 b_elt = b_elt->next;
1997 a_prev = *a_prev_pnext;
1998 a_prev_pnext = &a_prev->next;
1999 a_elt = *a_prev_pnext;
2002 gcc_checking_assert (!a->current == !a->first);
2003 if (a->current)
2004 a->indx = a->current->indx;
2005 return changed;
2008 /* DST = A ^ B */
2010 void
2011 bitmap_xor (bitmap dst, const_bitmap a, const_bitmap b)
2013 bitmap_element *dst_elt = dst->first;
2014 const bitmap_element *a_elt = a->first;
2015 const bitmap_element *b_elt = b->first;
2016 bitmap_element *dst_prev = NULL;
2018 gcc_checking_assert (!dst->tree_form && !a->tree_form && !b->tree_form);
2019 gcc_assert (dst != a && dst != b);
2021 if (a == b)
2023 bitmap_clear (dst);
2024 return;
2027 while (a_elt || b_elt)
2029 if (a_elt && b_elt && a_elt->indx == b_elt->indx)
2031 /* Matching elts, generate A ^ B. */
2032 unsigned ix;
2033 BITMAP_WORD ior = 0;
2035 if (!dst_elt)
2036 dst_elt = bitmap_list_insert_element_after (dst, dst_prev,
2037 a_elt->indx);
2038 else
2039 dst_elt->indx = a_elt->indx;
2040 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
2042 BITMAP_WORD r = a_elt->bits[ix] ^ b_elt->bits[ix];
2044 ior |= r;
2045 dst_elt->bits[ix] = r;
2047 a_elt = a_elt->next;
2048 b_elt = b_elt->next;
2049 if (ior)
2051 dst_prev = dst_elt;
2052 dst_elt = dst_elt->next;
2055 else
2057 /* Copy a single element. */
2058 const bitmap_element *src;
2060 if (!b_elt || (a_elt && a_elt->indx < b_elt->indx))
2062 src = a_elt;
2063 a_elt = a_elt->next;
2065 else
2067 src = b_elt;
2068 b_elt = b_elt->next;
2071 if (!dst_elt)
2072 dst_elt = bitmap_list_insert_element_after (dst, dst_prev,
2073 src->indx);
2074 else
2075 dst_elt->indx = src->indx;
2076 memcpy (dst_elt->bits, src->bits, sizeof (dst_elt->bits));
2077 dst_prev = dst_elt;
2078 dst_elt = dst_elt->next;
2081 /* Ensure that dst->current is valid. */
2082 dst->current = dst->first;
2083 bitmap_elt_clear_from (dst, dst_elt);
2084 gcc_checking_assert (!dst->current == !dst->first);
2085 if (dst->current)
2086 dst->indx = dst->current->indx;
2089 /* A ^= B */
2091 void
2092 bitmap_xor_into (bitmap a, const_bitmap b)
2094 bitmap_element *a_elt = a->first;
2095 const bitmap_element *b_elt = b->first;
2096 bitmap_element *a_prev = NULL;
2098 gcc_checking_assert (!a->tree_form && !b->tree_form);
2100 if (a == b)
2102 bitmap_clear (a);
2103 return;
2106 while (b_elt)
2108 if (!a_elt || b_elt->indx < a_elt->indx)
2110 /* Copy b_elt. */
2111 bitmap_element *dst = bitmap_list_insert_element_after (a, a_prev,
2112 b_elt->indx);
2113 memcpy (dst->bits, b_elt->bits, sizeof (dst->bits));
2114 a_prev = dst;
2115 b_elt = b_elt->next;
2117 else if (a_elt->indx < b_elt->indx)
2119 a_prev = a_elt;
2120 a_elt = a_elt->next;
2122 else
2124 /* Matching elts, generate A ^= B. */
2125 unsigned ix;
2126 BITMAP_WORD ior = 0;
2127 bitmap_element *next = a_elt->next;
2129 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
2131 BITMAP_WORD r = a_elt->bits[ix] ^ b_elt->bits[ix];
2133 ior |= r;
2134 a_elt->bits[ix] = r;
2136 b_elt = b_elt->next;
2137 if (ior)
2138 a_prev = a_elt;
2139 else
2140 bitmap_list_unlink_element (a, a_elt);
2141 a_elt = next;
2144 gcc_checking_assert (!a->current == !a->first);
2145 if (a->current)
2146 a->indx = a->current->indx;
2149 /* Return true if two bitmaps are identical.
2150 We do not bother with a check for pointer equality, as that never
2151 occurs in practice. */
2153 bool
2154 bitmap_equal_p (const_bitmap a, const_bitmap b)
2156 const bitmap_element *a_elt;
2157 const bitmap_element *b_elt;
2158 unsigned ix;
2160 gcc_checking_assert (!a->tree_form && !b->tree_form);
2162 for (a_elt = a->first, b_elt = b->first;
2163 a_elt && b_elt;
2164 a_elt = a_elt->next, b_elt = b_elt->next)
2166 if (a_elt->indx != b_elt->indx)
2167 return false;
2168 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
2169 if (a_elt->bits[ix] != b_elt->bits[ix])
2170 return false;
2172 return !a_elt && !b_elt;
2175 /* Return true if A AND B is not empty. */
2177 bool
2178 bitmap_intersect_p (const_bitmap a, const_bitmap b)
2180 const bitmap_element *a_elt;
2181 const bitmap_element *b_elt;
2182 unsigned ix;
2184 gcc_checking_assert (!a->tree_form && !b->tree_form);
2186 for (a_elt = a->first, b_elt = b->first;
2187 a_elt && b_elt;)
2189 if (a_elt->indx < b_elt->indx)
2190 a_elt = a_elt->next;
2191 else if (b_elt->indx < a_elt->indx)
2192 b_elt = b_elt->next;
2193 else
2195 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
2196 if (a_elt->bits[ix] & b_elt->bits[ix])
2197 return true;
2198 a_elt = a_elt->next;
2199 b_elt = b_elt->next;
2202 return false;
2205 /* Return true if A AND NOT B is not empty. */
2207 bool
2208 bitmap_intersect_compl_p (const_bitmap a, const_bitmap b)
2210 const bitmap_element *a_elt;
2211 const bitmap_element *b_elt;
2212 unsigned ix;
2214 gcc_checking_assert (!a->tree_form && !b->tree_form);
2216 for (a_elt = a->first, b_elt = b->first;
2217 a_elt && b_elt;)
2219 if (a_elt->indx < b_elt->indx)
2220 return true;
2221 else if (b_elt->indx < a_elt->indx)
2222 b_elt = b_elt->next;
2223 else
2225 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
2226 if (a_elt->bits[ix] & ~b_elt->bits[ix])
2227 return true;
2228 a_elt = a_elt->next;
2229 b_elt = b_elt->next;
2232 return a_elt != NULL;
2236 /* DST = A | (FROM1 & ~FROM2). Return true if DST changes. */
2238 bool
2239 bitmap_ior_and_compl (bitmap dst, const_bitmap a, const_bitmap b, const_bitmap kill)
2241 bool changed = false;
2243 bitmap_element *dst_elt = dst->first;
2244 const bitmap_element *a_elt = a->first;
2245 const bitmap_element *b_elt = b->first;
2246 const bitmap_element *kill_elt = kill->first;
2247 bitmap_element *dst_prev = NULL;
2248 bitmap_element **dst_prev_pnext = &dst->first;
2250 gcc_checking_assert (!dst->tree_form && !a->tree_form && !b->tree_form
2251 && !kill->tree_form);
2252 gcc_assert (dst != a && dst != b && dst != kill);
2254 /* Special cases. We don't bother checking for bitmap_equal_p (b, kill). */
2255 if (b == kill || bitmap_empty_p (b))
2257 changed = !bitmap_equal_p (dst, a);
2258 if (changed)
2259 bitmap_copy (dst, a);
2260 return changed;
2262 if (bitmap_empty_p (kill))
2263 return bitmap_ior (dst, a, b);
2264 if (bitmap_empty_p (a))
2265 return bitmap_and_compl (dst, b, kill);
2267 while (a_elt || b_elt)
2269 bool new_element = false;
2271 if (b_elt)
2272 while (kill_elt && kill_elt->indx < b_elt->indx)
2273 kill_elt = kill_elt->next;
2275 if (b_elt && kill_elt && kill_elt->indx == b_elt->indx
2276 && (!a_elt || a_elt->indx >= b_elt->indx))
2278 bitmap_element tmp_elt;
2279 unsigned ix;
2281 BITMAP_WORD ior = 0;
2282 tmp_elt.indx = b_elt->indx;
2283 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
2285 BITMAP_WORD r = b_elt->bits[ix] & ~kill_elt->bits[ix];
2286 ior |= r;
2287 tmp_elt.bits[ix] = r;
2290 if (ior)
2292 changed = bitmap_elt_ior (dst, dst_elt, dst_prev,
2293 a_elt, &tmp_elt, changed);
2294 new_element = true;
2295 if (a_elt && a_elt->indx == b_elt->indx)
2296 a_elt = a_elt->next;
2299 b_elt = b_elt->next;
2300 kill_elt = kill_elt->next;
2302 else
2304 changed = bitmap_elt_ior (dst, dst_elt, dst_prev,
2305 a_elt, b_elt, changed);
2306 new_element = true;
2308 if (a_elt && b_elt && a_elt->indx == b_elt->indx)
2310 a_elt = a_elt->next;
2311 b_elt = b_elt->next;
2313 else
2315 if (a_elt && (!b_elt || a_elt->indx <= b_elt->indx))
2316 a_elt = a_elt->next;
2317 else if (b_elt && (!a_elt || b_elt->indx <= a_elt->indx))
2318 b_elt = b_elt->next;
2322 if (new_element)
2324 dst_prev = *dst_prev_pnext;
2325 dst_prev_pnext = &dst_prev->next;
2326 dst_elt = *dst_prev_pnext;
2330 if (dst_elt)
2332 changed = true;
2333 /* Ensure that dst->current is valid. */
2334 dst->current = dst->first;
2335 bitmap_elt_clear_from (dst, dst_elt);
2337 gcc_checking_assert (!dst->current == !dst->first);
2338 if (dst->current)
2339 dst->indx = dst->current->indx;
2341 return changed;
2344 /* A |= (B & ~C). Return true if A changes. */
2346 bool
2347 bitmap_ior_and_compl_into (bitmap a, const_bitmap b, const_bitmap c)
2349 bitmap_head tmp;
2350 bool changed;
2352 gcc_checking_assert (!a->tree_form && !b->tree_form && !c->tree_form);
2354 bitmap_initialize (&tmp, &bitmap_default_obstack);
2355 bitmap_and_compl (&tmp, b, c);
2356 changed = bitmap_ior_into (a, &tmp);
2357 bitmap_clear (&tmp);
2359 return changed;
2362 /* A |= (B & C). Return true if A changes. */
2364 bool
2365 bitmap_ior_and_into (bitmap a, const_bitmap b, const_bitmap c)
2367 bitmap_element *a_elt = a->first;
2368 const bitmap_element *b_elt = b->first;
2369 const bitmap_element *c_elt = c->first;
2370 bitmap_element and_elt;
2371 bitmap_element *a_prev = NULL;
2372 bitmap_element **a_prev_pnext = &a->first;
2373 bool changed = false;
2374 unsigned ix;
2376 gcc_checking_assert (!a->tree_form && !b->tree_form && !c->tree_form);
2378 if (b == c)
2379 return bitmap_ior_into (a, b);
2380 if (bitmap_empty_p (b) || bitmap_empty_p (c))
2381 return false;
2383 and_elt.indx = -1;
2384 while (b_elt && c_elt)
2386 BITMAP_WORD overall;
2388 /* Find a common item of B and C. */
2389 while (b_elt->indx != c_elt->indx)
2391 if (b_elt->indx < c_elt->indx)
2393 b_elt = b_elt->next;
2394 if (!b_elt)
2395 goto done;
2397 else
2399 c_elt = c_elt->next;
2400 if (!c_elt)
2401 goto done;
2405 overall = 0;
2406 and_elt.indx = b_elt->indx;
2407 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
2409 and_elt.bits[ix] = b_elt->bits[ix] & c_elt->bits[ix];
2410 overall |= and_elt.bits[ix];
2413 b_elt = b_elt->next;
2414 c_elt = c_elt->next;
2415 if (!overall)
2416 continue;
2418 /* Now find a place to insert AND_ELT. */
2421 ix = a_elt ? a_elt->indx : and_elt.indx;
2422 if (ix == and_elt.indx)
2423 changed = bitmap_elt_ior (a, a_elt, a_prev, a_elt, &and_elt, changed);
2424 else if (ix > and_elt.indx)
2425 changed = bitmap_elt_copy (a, NULL, a_prev, &and_elt, changed);
2427 a_prev = *a_prev_pnext;
2428 a_prev_pnext = &a_prev->next;
2429 a_elt = *a_prev_pnext;
2431 /* If A lagged behind B/C, we advanced it so loop once more. */
2433 while (ix < and_elt.indx);
2436 done:
2437 gcc_checking_assert (!a->current == !a->first);
2438 if (a->current)
2439 a->indx = a->current->indx;
2440 return changed;
2443 /* Compute hash of bitmap (for purposes of hashing). */
2445 hashval_t
2446 bitmap_hash (const_bitmap head)
2448 const bitmap_element *ptr;
2449 BITMAP_WORD hash = 0;
2450 int ix;
2452 gcc_checking_assert (!head->tree_form);
2454 for (ptr = head->first; ptr; ptr = ptr->next)
2456 hash ^= ptr->indx;
2457 for (ix = 0; ix != BITMAP_ELEMENT_WORDS; ix++)
2458 hash ^= ptr->bits[ix];
2460 return (hashval_t)hash;
2464 /* Function to obtain a vector of bitmap elements in bit order from
2465 HEAD in tree view. */
2467 static void
2468 bitmap_tree_to_vec (vec<bitmap_element *> &elts, const_bitmap head)
2470 gcc_checking_assert (head->tree_form);
2471 auto_vec<bitmap_element *, 32> stack;
2472 bitmap_element *e = head->first;
2473 while (true)
2475 while (e != NULL)
2477 stack.safe_push (e);
2478 e = e->prev;
2480 if (stack.is_empty ())
2481 break;
2483 e = stack.pop ();
2484 elts.safe_push (e);
2485 e = e->next;
2489 /* Debugging function to print out the contents of a bitmap element. */
2491 DEBUG_FUNCTION void
2492 debug_bitmap_elt_file (FILE *file, const bitmap_element *ptr)
2494 unsigned int i, j, col = 26;
2496 fprintf (file, "\t" HOST_PTR_PRINTF " next = " HOST_PTR_PRINTF
2497 " prev = " HOST_PTR_PRINTF " indx = %u\n\t\tbits = {",
2498 (const void*) ptr, (const void*) ptr->next,
2499 (const void*) ptr->prev, ptr->indx);
2501 for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
2502 for (j = 0; j < BITMAP_WORD_BITS; j++)
2503 if ((ptr->bits[i] >> j) & 1)
2505 if (col > 70)
2507 fprintf (file, "\n\t\t\t");
2508 col = 24;
2511 fprintf (file, " %u", (ptr->indx * BITMAP_ELEMENT_ALL_BITS
2512 + i * BITMAP_WORD_BITS + j));
2513 col += 4;
2516 fprintf (file, " }\n");
2519 /* Debugging function to print out the contents of a bitmap. */
2521 DEBUG_FUNCTION void
2522 debug_bitmap_file (FILE *file, const_bitmap head)
2524 const bitmap_element *ptr;
2526 fprintf (file, "\nfirst = " HOST_PTR_PRINTF
2527 " current = " HOST_PTR_PRINTF " indx = %u\n",
2528 (void *) head->first, (void *) head->current, head->indx);
2530 if (head->tree_form)
2532 auto_vec<bitmap_element *, 32> elts;
2533 bitmap_tree_to_vec (elts, head);
2534 for (unsigned i = 0; i < elts.length (); ++i)
2535 debug_bitmap_elt_file (file, elts[i]);
2537 else
2538 for (ptr = head->first; ptr; ptr = ptr->next)
2539 debug_bitmap_elt_file (file, ptr);
2542 /* Function to be called from the debugger to print the contents
2543 of a bitmap. */
2545 DEBUG_FUNCTION void
2546 debug_bitmap (const_bitmap head)
2548 debug_bitmap_file (stderr, head);
2551 /* Function to print out the contents of a bitmap. Unlike debug_bitmap_file,
2552 it does not print anything but the bits. */
2554 DEBUG_FUNCTION void
2555 bitmap_print (FILE *file, const_bitmap head, const char *prefix,
2556 const char *suffix)
2558 const char *comma = "";
2559 unsigned i;
2561 fputs (prefix, file);
2562 if (head->tree_form)
2564 auto_vec<bitmap_element *, 32> elts;
2565 bitmap_tree_to_vec (elts, head);
2566 for (i = 0; i < elts.length (); ++i)
2567 for (unsigned ix = 0; ix != BITMAP_ELEMENT_WORDS; ++ix)
2569 BITMAP_WORD word = elts[i]->bits[ix];
2570 for (unsigned bit = 0; bit != BITMAP_WORD_BITS; ++bit)
2571 if (word & ((BITMAP_WORD)1 << bit))
2573 fprintf (file, "%s%d", comma,
2574 (bit + BITMAP_WORD_BITS * ix
2575 + elts[i]->indx * BITMAP_ELEMENT_ALL_BITS));
2576 comma = ", ";
2580 else
2582 bitmap_iterator bi;
2583 EXECUTE_IF_SET_IN_BITMAP (head, 0, i, bi)
2585 fprintf (file, "%s%d", comma, i);
2586 comma = ", ";
2589 fputs (suffix, file);
2592 /* Output per-bitmap memory usage statistics. */
2593 void
2594 dump_bitmap_statistics (void)
2596 if (!GATHER_STATISTICS)
2597 return;
2599 bitmap_mem_desc.dump (BITMAP_ORIGIN);
2602 DEBUG_FUNCTION void
2603 debug (const bitmap_head &ref)
2605 dump_bitmap (stderr, &ref);
2608 DEBUG_FUNCTION void
2609 debug (const bitmap_head *ptr)
2611 if (ptr)
2612 debug (*ptr);
2613 else
2614 fprintf (stderr, "<nil>\n");
2617 void
2618 bitmap_head::dump ()
2620 debug (this);
2623 #if CHECKING_P
2625 namespace selftest {
2627 /* Selftests for bitmaps. */
2629 /* Freshly-created bitmaps ought to be empty. */
2631 static void
2632 test_gc_alloc ()
2634 bitmap b = bitmap_gc_alloc ();
2635 ASSERT_TRUE (bitmap_empty_p (b));
2638 /* Verify bitmap_set_range. */
2640 static void
2641 test_set_range ()
2643 bitmap b = bitmap_gc_alloc ();
2644 ASSERT_TRUE (bitmap_empty_p (b));
2646 bitmap_set_range (b, 7, 5);
2647 ASSERT_FALSE (bitmap_empty_p (b));
2648 ASSERT_EQ (5, bitmap_count_bits (b));
2650 /* Verify bitmap_bit_p at the boundaries. */
2651 ASSERT_FALSE (bitmap_bit_p (b, 6));
2652 ASSERT_TRUE (bitmap_bit_p (b, 7));
2653 ASSERT_TRUE (bitmap_bit_p (b, 11));
2654 ASSERT_FALSE (bitmap_bit_p (b, 12));
2657 /* Verify splitting a range into two pieces using bitmap_clear_bit. */
2659 static void
2660 test_clear_bit_in_middle ()
2662 bitmap b = bitmap_gc_alloc ();
2664 /* Set b to [100..200]. */
2665 bitmap_set_range (b, 100, 100);
2666 ASSERT_EQ (100, bitmap_count_bits (b));
2668 /* Clear a bit in the middle. */
2669 bool changed = bitmap_clear_bit (b, 150);
2670 ASSERT_TRUE (changed);
2671 ASSERT_EQ (99, bitmap_count_bits (b));
2672 ASSERT_TRUE (bitmap_bit_p (b, 149));
2673 ASSERT_FALSE (bitmap_bit_p (b, 150));
2674 ASSERT_TRUE (bitmap_bit_p (b, 151));
2677 /* Verify bitmap_copy. */
2679 static void
2680 test_copying ()
2682 bitmap src = bitmap_gc_alloc ();
2683 bitmap_set_range (src, 40, 10);
2685 bitmap dst = bitmap_gc_alloc ();
2686 ASSERT_FALSE (bitmap_equal_p (src, dst));
2687 bitmap_copy (dst, src);
2688 ASSERT_TRUE (bitmap_equal_p (src, dst));
2690 /* Verify that we can make them unequal again... */
2691 bitmap_set_range (src, 70, 5);
2692 ASSERT_FALSE (bitmap_equal_p (src, dst));
2694 /* ...and that changing src after the copy didn't affect
2695 the other: */
2696 ASSERT_FALSE (bitmap_bit_p (dst, 70));
2699 /* Verify bitmap_single_bit_set_p. */
2701 static void
2702 test_bitmap_single_bit_set_p ()
2704 bitmap b = bitmap_gc_alloc ();
2706 ASSERT_FALSE (bitmap_single_bit_set_p (b));
2708 bitmap_set_range (b, 42, 1);
2709 ASSERT_TRUE (bitmap_single_bit_set_p (b));
2710 ASSERT_EQ (42, bitmap_first_set_bit (b));
2712 bitmap_set_range (b, 1066, 1);
2713 ASSERT_FALSE (bitmap_single_bit_set_p (b));
2714 ASSERT_EQ (42, bitmap_first_set_bit (b));
2716 bitmap_clear_range (b, 0, 100);
2717 ASSERT_TRUE (bitmap_single_bit_set_p (b));
2718 ASSERT_EQ (1066, bitmap_first_set_bit (b));
2721 /* Run all of the selftests within this file. */
2723 void
2724 bitmap_c_tests ()
2726 test_gc_alloc ();
2727 test_set_range ();
2728 test_clear_bit_in_middle ();
2729 test_copying ();
2730 test_bitmap_single_bit_set_p ();
2733 } // namespace selftest
2734 #endif /* CHECKING_P */
2736 #include "gt-bitmap.h"