tree-vect-data-refs.c (vect_find_stmt_data_reference): Handle even zero DR_OFFSET...
[official-gcc.git] / gcc / bitmap.c
blob894aefa13de6bd04c3ead40dd9dae9ea23003527
1 /* Functions to support general ended bitmaps.
2 Copyright (C) 1997-2019 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 static unsigned alloc_descriptor_max_uid = 1;
40 gcc_assert (b->alloc_descriptor == 0);
41 b->alloc_descriptor = alloc_descriptor_max_uid++;
43 bitmap_mem_desc.register_descriptor (b->get_descriptor (), BITMAP_ORIGIN,
44 false FINAL_PASS_MEM_STAT);
47 /* Account the overhead. */
48 static void
49 register_overhead (bitmap b, size_t amount)
51 unsigned *d = b->get_descriptor ();
52 if (bitmap_mem_desc.contains_descriptor_for_instance (d))
53 bitmap_mem_desc.register_instance_overhead (amount, d);
56 /* Release the overhead. */
57 static void
58 release_overhead (bitmap b, size_t amount, bool remove_from_map)
60 unsigned *d = b->get_descriptor ();
61 if (bitmap_mem_desc.contains_descriptor_for_instance (d))
62 bitmap_mem_desc.release_instance_overhead (d, amount, remove_from_map);
66 /* Global data */
67 bitmap_element bitmap_zero_bits; /* An element of all zero bits. */
68 bitmap_obstack bitmap_default_obstack; /* The default bitmap obstack. */
69 static int bitmap_default_obstack_depth;
70 static GTY((deletable)) bitmap_element *bitmap_ggc_free; /* Freelist of
71 GC'd elements. */
74 /* Bitmap memory management. */
76 /* Add ELT to the appropriate freelist. */
77 static inline void
78 bitmap_elem_to_freelist (bitmap head, bitmap_element *elt)
80 bitmap_obstack *bit_obstack = head->obstack;
82 if (GATHER_STATISTICS)
83 release_overhead (head, sizeof (bitmap_element), false);
85 elt->next = NULL;
86 elt->indx = -1;
87 if (bit_obstack)
89 elt->prev = bit_obstack->elements;
90 bit_obstack->elements = elt;
92 else
94 elt->prev = bitmap_ggc_free;
95 bitmap_ggc_free = elt;
99 /* Allocate a bitmap element. The bits are cleared, but nothing else is. */
101 static inline bitmap_element *
102 bitmap_element_allocate (bitmap head)
104 bitmap_element *element;
105 bitmap_obstack *bit_obstack = head->obstack;
107 if (bit_obstack)
109 element = bit_obstack->elements;
111 if (element)
112 /* Use up the inner list first before looking at the next
113 element of the outer list. */
114 if (element->next)
116 bit_obstack->elements = element->next;
117 bit_obstack->elements->prev = element->prev;
119 else
120 /* Inner list was just a singleton. */
121 bit_obstack->elements = element->prev;
122 else
123 element = XOBNEW (&bit_obstack->obstack, bitmap_element);
125 else
127 element = bitmap_ggc_free;
128 if (element)
129 /* Use up the inner list first before looking at the next
130 element of the outer list. */
131 if (element->next)
133 bitmap_ggc_free = element->next;
134 bitmap_ggc_free->prev = element->prev;
136 else
137 /* Inner list was just a singleton. */
138 bitmap_ggc_free = element->prev;
139 else
140 element = ggc_alloc<bitmap_element> ();
143 if (GATHER_STATISTICS)
144 register_overhead (head, sizeof (bitmap_element));
146 memset (element->bits, 0, sizeof (element->bits));
148 return element;
151 /* Remove ELT and all following elements from bitmap HEAD.
152 Put the released elements in the freelist for HEAD. */
154 void
155 bitmap_elt_clear_from (bitmap head, bitmap_element *elt)
157 bitmap_element *prev;
158 bitmap_obstack *bit_obstack = head->obstack;
160 if (!elt)
161 return;
163 if (head->tree_form)
164 elt = bitmap_tree_listify_from (head, elt);
166 if (GATHER_STATISTICS)
168 int n = 0;
169 for (prev = elt; prev; prev = prev->next)
170 n++;
171 release_overhead (head, sizeof (bitmap_element) * n, false);
174 prev = elt->prev;
175 if (prev)
177 prev->next = NULL;
178 if (head->current->indx > prev->indx)
180 head->current = prev;
181 head->indx = prev->indx;
184 else
186 head->first = NULL;
187 head->current = NULL;
188 head->indx = 0;
191 /* Put the entire list onto the freelist in one operation. */
192 if (bit_obstack)
194 elt->prev = bit_obstack->elements;
195 bit_obstack->elements = elt;
197 else
199 elt->prev = bitmap_ggc_free;
200 bitmap_ggc_free = elt;
204 /* Linked-list view of bitmaps.
206 In this representation, the bitmap elements form a double-linked list
207 with elements sorted by increasing index. */
209 /* Link the bitmap element into the current bitmap linked list. */
211 static inline void
212 bitmap_list_link_element (bitmap head, bitmap_element *element)
214 unsigned int indx = element->indx;
215 bitmap_element *ptr;
217 gcc_checking_assert (!head->tree_form);
219 /* If this is the first and only element, set it in. */
220 if (head->first == 0)
222 element->next = element->prev = 0;
223 head->first = element;
226 /* If this index is less than that of the current element, it goes someplace
227 before the current element. */
228 else if (indx < head->indx)
230 for (ptr = head->current;
231 ptr->prev != 0 && ptr->prev->indx > indx;
232 ptr = ptr->prev)
235 if (ptr->prev)
236 ptr->prev->next = element;
237 else
238 head->first = element;
240 element->prev = ptr->prev;
241 element->next = ptr;
242 ptr->prev = element;
245 /* Otherwise, it must go someplace after the current element. */
246 else
248 for (ptr = head->current;
249 ptr->next != 0 && ptr->next->indx < indx;
250 ptr = ptr->next)
253 if (ptr->next)
254 ptr->next->prev = element;
256 element->next = ptr->next;
257 element->prev = ptr;
258 ptr->next = element;
261 /* Set up so this is the first element searched. */
262 head->current = element;
263 head->indx = indx;
266 /* Unlink the bitmap element from the current bitmap linked list,
267 and return it to the freelist. */
269 static inline void
270 bitmap_list_unlink_element (bitmap head, bitmap_element *element)
272 bitmap_element *next = element->next;
273 bitmap_element *prev = element->prev;
275 gcc_checking_assert (!head->tree_form);
277 if (prev)
278 prev->next = next;
280 if (next)
281 next->prev = prev;
283 if (head->first == element)
284 head->first = next;
286 /* Since the first thing we try is to insert before current,
287 make current the next entry in preference to the previous. */
288 if (head->current == element)
290 head->current = next != 0 ? next : prev;
291 if (head->current)
292 head->indx = head->current->indx;
293 else
294 head->indx = 0;
297 bitmap_elem_to_freelist (head, element);
300 /* Insert a new uninitialized element into bitmap HEAD after element
301 ELT. If ELT is NULL, insert the element at the start. Return the
302 new element. */
304 static bitmap_element *
305 bitmap_list_insert_element_after (bitmap head,
306 bitmap_element *elt, unsigned int indx)
308 bitmap_element *node = bitmap_element_allocate (head);
309 node->indx = indx;
311 gcc_checking_assert (!head->tree_form);
313 if (!elt)
315 if (!head->current)
317 head->current = node;
318 head->indx = indx;
320 node->next = head->first;
321 if (node->next)
322 node->next->prev = node;
323 head->first = node;
324 node->prev = NULL;
326 else
328 gcc_checking_assert (head->current);
329 node->next = elt->next;
330 if (node->next)
331 node->next->prev = node;
332 elt->next = node;
333 node->prev = elt;
335 return node;
338 /* Return the element for INDX, or NULL if the element doesn't exist.
339 Update the `current' field even if we can't find an element that
340 would hold the bitmap's bit to make eventual allocation
341 faster. */
343 static inline bitmap_element *
344 bitmap_list_find_element (bitmap head, unsigned int indx)
346 bitmap_element *element;
348 if (head->current == NULL
349 || head->indx == indx)
350 return head->current;
352 if (head->current == head->first
353 && head->first->next == NULL)
354 return NULL;
356 /* Usage can be NULL due to allocated bitmaps for which we do not
357 call initialize function. */
358 bitmap_usage *usage = NULL;
359 if (GATHER_STATISTICS)
360 usage = bitmap_mem_desc.get_descriptor_for_instance (head);
362 /* This bitmap has more than one element, and we're going to look
363 through the elements list. Count that as a search. */
364 if (GATHER_STATISTICS && usage)
365 usage->m_nsearches++;
367 if (head->indx < indx)
368 /* INDX is beyond head->indx. Search from head->current
369 forward. */
370 for (element = head->current;
371 element->next != 0 && element->indx < indx;
372 element = element->next)
374 if (GATHER_STATISTICS && usage)
375 usage->m_search_iter++;
378 else if (head->indx / 2 < indx)
379 /* INDX is less than head->indx and closer to head->indx than to
380 0. Search from head->current backward. */
381 for (element = head->current;
382 element->prev != 0 && element->indx > indx;
383 element = element->prev)
385 if (GATHER_STATISTICS && usage)
386 usage->m_search_iter++;
389 else
390 /* INDX is less than head->indx and closer to 0 than to
391 head->indx. Search from head->first forward. */
392 for (element = head->first;
393 element->next != 0 && element->indx < indx;
394 element = element->next)
396 if (GATHER_STATISTICS && usage)
397 usage->m_search_iter++;
400 /* `element' is the nearest to the one we want. If it's not the one we
401 want, the one we want doesn't exist. */
402 gcc_checking_assert (element != NULL);
403 head->current = element;
404 head->indx = element->indx;
405 if (element->indx != indx)
406 element = 0;
407 return element;
411 /* Splay-tree view of bitmaps.
413 This is an almost one-to-one the implementatin of the simple top-down
414 splay tree in Sleator and Tarjan's "Self-adjusting Binary Search Trees".
415 It is probably not the most efficient form of splay trees, but it should
416 be good enough to experiment with this idea of bitmaps-as-trees.
418 For all functions below, the variable or function argument "t" is a node
419 in the tree, and "e" is a temporary or new node in the tree. The rest
420 is sufficiently straigh-forward (and very well explained in the paper)
421 that comment would only clutter things. */
423 static inline void
424 bitmap_tree_link_left (bitmap_element * &t, bitmap_element * &l)
426 l->next = t;
427 l = t;
428 t = t->next;
431 static inline void
432 bitmap_tree_link_right (bitmap_element * &t, bitmap_element * &r)
434 r->prev = t;
435 r = t;
436 t = t->prev;
439 static inline void
440 bitmap_tree_rotate_left (bitmap_element * &t)
442 bitmap_element *e = t->next;
443 t->next = t->next->prev;
444 e->prev = t;
445 t = e;
448 static inline void
449 bitmap_tree_rotate_right (bitmap_element * &t)
451 bitmap_element *e = t->prev;
452 t->prev = t->prev->next;
453 e->next = t;
454 t = e;
457 static bitmap_element *
458 bitmap_tree_splay (bitmap head, bitmap_element *t, unsigned int indx)
460 bitmap_element N, *l, *r;
462 if (t == NULL)
463 return NULL;
465 bitmap_usage *usage = NULL;
466 if (GATHER_STATISTICS)
467 usage = bitmap_mem_desc.get_descriptor_for_instance (head);
469 N.prev = N.next = NULL;
470 l = r = &N;
472 while (indx != t->indx)
474 if (GATHER_STATISTICS && usage)
475 usage->m_search_iter++;
477 if (indx < t->indx)
479 if (t->prev != NULL && indx < t->prev->indx)
480 bitmap_tree_rotate_right (t);
481 if (t->prev == NULL)
482 break;
483 bitmap_tree_link_right (t, r);
485 else if (indx > t->indx)
487 if (t->next != NULL && indx > t->next->indx)
488 bitmap_tree_rotate_left (t);
489 if (t->next == NULL)
490 break;
491 bitmap_tree_link_left (t, l);
495 l->next = t->prev;
496 r->prev = t->next;
497 t->prev = N.next;
498 t->next = N.prev;
499 return t;
502 /* Link bitmap element E into the current bitmap splay tree. */
504 static inline void
505 bitmap_tree_link_element (bitmap head, bitmap_element *e)
507 if (head->first == NULL)
508 e->prev = e->next = NULL;
509 else
511 bitmap_element *t = bitmap_tree_splay (head, head->first, e->indx);
512 if (e->indx < t->indx)
514 e->prev = t->prev;
515 e->next = t;
516 t->prev = NULL;
518 else if (e->indx > t->indx)
520 e->next = t->next;
521 e->prev = t;
522 t->next = NULL;
524 else
525 gcc_unreachable ();
527 head->first = e;
528 head->current = e;
529 head->indx = e->indx;
532 /* Unlink bitmap element E from the current bitmap splay tree,
533 and return it to the freelist. */
535 static void
536 bitmap_tree_unlink_element (bitmap head, bitmap_element *e)
538 bitmap_element *t = bitmap_tree_splay (head, head->first, e->indx);
540 gcc_checking_assert (t == e);
542 if (e->prev == NULL)
543 t = e->next;
544 else
546 t = bitmap_tree_splay (head, e->prev, e->indx);
547 t->next = e->next;
549 head->first = t;
550 head->current = t;
551 head->indx = (t != NULL) ? t->indx : 0;
553 bitmap_elem_to_freelist (head, e);
556 /* Return the element for INDX, or NULL if the element doesn't exist. */
558 static inline bitmap_element *
559 bitmap_tree_find_element (bitmap head, unsigned int indx)
561 if (head->current == NULL
562 || head->indx == indx)
563 return head->current;
565 /* Usage can be NULL due to allocated bitmaps for which we do not
566 call initialize function. */
567 bitmap_usage *usage = NULL;
568 if (GATHER_STATISTICS)
569 usage = bitmap_mem_desc.get_descriptor_for_instance (head);
571 /* This bitmap has more than one element, and we're going to look
572 through the elements list. Count that as a search. */
573 if (GATHER_STATISTICS && usage)
574 usage->m_nsearches++;
576 bitmap_element *element = bitmap_tree_splay (head, head->first, indx);
577 gcc_checking_assert (element != NULL);
578 head->first = element;
579 head->current = element;
580 head->indx = element->indx;
581 if (element->indx != indx)
582 element = 0;
583 return element;
586 /* Converting bitmap views from linked-list to tree and vice versa. */
588 /* Splice element E and all elements with a larger index from
589 bitmap HEAD, convert the spliced elements to the linked-list
590 view, and return the head of the list (which should be E again), */
592 static bitmap_element *
593 bitmap_tree_listify_from (bitmap head, bitmap_element *e)
595 bitmap_element *t, *erb;
597 /* Detach the right branch from E (all elements with indx > E->indx),
598 and splay E to the root. */
599 erb = e->next;
600 e->next = NULL;
601 t = bitmap_tree_splay (head, head->first, e->indx);
602 gcc_checking_assert (t == e);
604 /* Because E has no right branch, and we rotated it to the root,
605 the left branch is the new root. */
606 t = e->prev;
607 head->first = t;
608 head->current = t;
609 head->indx = (t != NULL) ? t->indx : 0;
611 /* Detach the tree from E, and re-attach the right branch of E. */
612 e->prev = NULL;
613 e->next = erb;
615 /* The tree is now valid again. Now we need to "un-tree" E.
616 It is imperative that a non-recursive implementation is used
617 for this, because splay trees have a worst case depth of O(N)
618 for a tree with N nodes. A recursive implementation could
619 result in a stack overflow for a sufficiently large, unbalanced
620 bitmap tree. */
622 auto_vec<bitmap_element *, 32> stack;
623 auto_vec<bitmap_element *, 32> sorted_elements;
624 bitmap_element *n = e;
626 while (true)
628 while (n != NULL)
630 stack.safe_push (n);
631 n = n->prev;
634 if (stack.is_empty ())
635 break;
637 n = stack.pop ();
638 sorted_elements.safe_push (n);
639 n = n->next;
642 gcc_assert (sorted_elements[0] == e);
644 bitmap_element *prev = NULL;
645 unsigned ix;
646 FOR_EACH_VEC_ELT (sorted_elements, ix, n)
648 if (prev != NULL)
649 prev->next = n;
650 n->prev = prev;
651 n->next = NULL;
652 prev = n;
655 return e;
658 /* Convert bitmap HEAD from splay-tree view to linked-list view. */
660 void
661 bitmap_list_view (bitmap head)
663 bitmap_element *ptr;
665 gcc_assert (head->tree_form);
667 ptr = head->first;
668 if (ptr)
670 while (ptr->prev)
671 bitmap_tree_rotate_right (ptr);
672 head->first = ptr;
673 head->first = bitmap_tree_listify_from (head, ptr);
676 head->tree_form = false;
679 /* Convert bitmap HEAD from linked-list view to splay-tree view.
680 This is simply a matter of dropping the prev or next pointers
681 and setting the tree_form flag. The tree will balance itself
682 if and when it is used. */
684 void
685 bitmap_tree_view (bitmap head)
687 bitmap_element *ptr;
689 gcc_assert (! head->tree_form);
691 ptr = head->first;
692 while (ptr)
694 ptr->prev = NULL;
695 ptr = ptr->next;
698 head->tree_form = true;
701 /* Clear a bitmap by freeing all its elements. */
703 void
704 bitmap_clear (bitmap head)
706 if (head->first == NULL)
707 return;
708 if (head->tree_form)
710 bitmap_element *e, *t;
711 for (e = head->first; e->prev; e = e->prev)
712 /* Loop to find the element with the smallest index. */ ;
713 t = bitmap_tree_splay (head, head->first, e->indx);
714 gcc_checking_assert (t == e);
715 head->first = t;
717 bitmap_elt_clear_from (head, head->first);
720 /* Initialize a bitmap obstack. If BIT_OBSTACK is NULL, initialize
721 the default bitmap obstack. */
723 void
724 bitmap_obstack_initialize (bitmap_obstack *bit_obstack)
726 if (!bit_obstack)
728 if (bitmap_default_obstack_depth++)
729 return;
730 bit_obstack = &bitmap_default_obstack;
733 #if !defined(__GNUC__) || (__GNUC__ < 2)
734 #define __alignof__(type) 0
735 #endif
737 bit_obstack->elements = NULL;
738 bit_obstack->heads = NULL;
739 obstack_specify_allocation (&bit_obstack->obstack, OBSTACK_CHUNK_SIZE,
740 __alignof__ (bitmap_element),
741 obstack_chunk_alloc,
742 obstack_chunk_free);
745 /* Release the memory from a bitmap obstack. If BIT_OBSTACK is NULL,
746 release the default bitmap obstack. */
748 void
749 bitmap_obstack_release (bitmap_obstack *bit_obstack)
751 if (!bit_obstack)
753 if (--bitmap_default_obstack_depth)
755 gcc_assert (bitmap_default_obstack_depth > 0);
756 return;
758 bit_obstack = &bitmap_default_obstack;
761 bit_obstack->elements = NULL;
762 bit_obstack->heads = NULL;
763 obstack_free (&bit_obstack->obstack, NULL);
766 /* Create a new bitmap on an obstack. If BIT_OBSTACK is NULL, create
767 it on the default bitmap obstack. */
769 bitmap
770 bitmap_alloc (bitmap_obstack *bit_obstack MEM_STAT_DECL)
772 bitmap map;
774 if (!bit_obstack)
775 bit_obstack = &bitmap_default_obstack;
776 map = bit_obstack->heads;
777 if (map)
778 bit_obstack->heads = (struct bitmap_head *) map->first;
779 else
780 map = XOBNEW (&bit_obstack->obstack, bitmap_head);
781 bitmap_initialize (map, bit_obstack PASS_MEM_STAT);
783 if (GATHER_STATISTICS)
784 register_overhead (map, sizeof (bitmap_head));
786 return map;
789 /* Create a new GCd bitmap. */
791 bitmap
792 bitmap_gc_alloc (ALONE_MEM_STAT_DECL)
794 bitmap map;
796 map = ggc_alloc<bitmap_head> ();
797 bitmap_initialize (map, NULL PASS_MEM_STAT);
799 if (GATHER_STATISTICS)
800 register_overhead (map, sizeof (bitmap_head));
802 return map;
805 /* Release an obstack allocated bitmap. */
807 void
808 bitmap_obstack_free (bitmap map)
810 if (map)
812 bitmap_clear (map);
813 map->first = (bitmap_element *) map->obstack->heads;
815 if (GATHER_STATISTICS)
816 release_overhead (map, sizeof (bitmap_head), true);
818 map->obstack->heads = map;
823 /* Return nonzero if all bits in an element are zero. */
825 static inline int
826 bitmap_element_zerop (const bitmap_element *element)
828 #if BITMAP_ELEMENT_WORDS == 2
829 return (element->bits[0] | element->bits[1]) == 0;
830 #else
831 unsigned i;
833 for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
834 if (element->bits[i] != 0)
835 return 0;
837 return 1;
838 #endif
841 /* Copy a bitmap to another bitmap. */
843 void
844 bitmap_copy (bitmap to, const_bitmap from)
846 const bitmap_element *from_ptr;
847 bitmap_element *to_ptr = 0;
849 gcc_checking_assert (!to->tree_form && !from->tree_form);
851 bitmap_clear (to);
853 /* Copy elements in forward direction one at a time. */
854 for (from_ptr = from->first; from_ptr; from_ptr = from_ptr->next)
856 bitmap_element *to_elt = bitmap_element_allocate (to);
858 to_elt->indx = from_ptr->indx;
859 memcpy (to_elt->bits, from_ptr->bits, sizeof (to_elt->bits));
861 /* Here we have a special case of bitmap_list_link_element,
862 for the case where we know the links are being entered
863 in sequence. */
864 if (to_ptr == 0)
866 to->first = to->current = to_elt;
867 to->indx = from_ptr->indx;
868 to_elt->next = to_elt->prev = 0;
870 else
872 to_elt->prev = to_ptr;
873 to_elt->next = 0;
874 to_ptr->next = to_elt;
877 to_ptr = to_elt;
881 /* Move a bitmap to another bitmap. */
883 void
884 bitmap_move (bitmap to, bitmap from)
886 gcc_assert (to->obstack == from->obstack);
888 bitmap_clear (to);
890 size_t sz = 0;
891 if (GATHER_STATISTICS)
893 for (bitmap_element *e = to->first; e; e = e->next)
894 sz += sizeof (bitmap_element);
895 register_overhead (to, sz);
898 *to = *from;
900 if (GATHER_STATISTICS)
901 release_overhead (from, sz, false);
904 /* Clear a single bit in a bitmap. Return true if the bit changed. */
906 bool
907 bitmap_clear_bit (bitmap head, int bit)
909 unsigned int indx = bit / BITMAP_ELEMENT_ALL_BITS;
910 bitmap_element *ptr;
912 if (!head->tree_form)
913 ptr = bitmap_list_find_element (head, indx);
914 else
915 ptr = bitmap_tree_find_element (head, indx);
916 if (ptr != 0)
918 unsigned bit_num = bit % BITMAP_WORD_BITS;
919 unsigned word_num = bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
920 BITMAP_WORD bit_val = ((BITMAP_WORD) 1) << bit_num;
921 bool res = (ptr->bits[word_num] & bit_val) != 0;
922 if (res)
924 ptr->bits[word_num] &= ~bit_val;
925 /* If we cleared the entire word, free up the element. */
926 if (!ptr->bits[word_num]
927 && bitmap_element_zerop (ptr))
929 if (!head->tree_form)
930 bitmap_list_unlink_element (head, ptr);
931 else
932 bitmap_tree_unlink_element (head, ptr);
936 return res;
939 return false;
942 /* Set a single bit in a bitmap. Return true if the bit changed. */
944 bool
945 bitmap_set_bit (bitmap head, int bit)
947 unsigned indx = bit / BITMAP_ELEMENT_ALL_BITS;
948 bitmap_element *ptr;
949 if (!head->tree_form)
950 ptr = bitmap_list_find_element (head, indx);
951 else
952 ptr = bitmap_tree_find_element (head, indx);
953 unsigned word_num = bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
954 unsigned bit_num = bit % BITMAP_WORD_BITS;
955 BITMAP_WORD bit_val = ((BITMAP_WORD) 1) << bit_num;
957 if (ptr != 0)
959 bool res = (ptr->bits[word_num] & bit_val) == 0;
960 if (res)
961 ptr->bits[word_num] |= bit_val;
962 return res;
965 ptr = bitmap_element_allocate (head);
966 ptr->indx = bit / BITMAP_ELEMENT_ALL_BITS;
967 ptr->bits[word_num] = bit_val;
968 if (!head->tree_form)
969 bitmap_list_link_element (head, ptr);
970 else
971 bitmap_tree_link_element (head, ptr);
972 return true;
975 /* Return whether a bit is set within a bitmap. */
978 bitmap_bit_p (bitmap head, int bit)
980 unsigned int indx = bit / BITMAP_ELEMENT_ALL_BITS;
981 bitmap_element *ptr;
982 unsigned bit_num;
983 unsigned word_num;
985 if (!head->tree_form)
986 ptr = bitmap_list_find_element (head, indx);
987 else
988 ptr = bitmap_tree_find_element (head, indx);
989 if (ptr == 0)
990 return 0;
992 bit_num = bit % BITMAP_WORD_BITS;
993 word_num = bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
995 return (ptr->bits[word_num] >> bit_num) & 1;
998 #if GCC_VERSION < 3400
999 /* Table of number of set bits in a character, indexed by value of char. */
1000 static const unsigned char popcount_table[] =
1002 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,
1003 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,
1004 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,
1005 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,
1006 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,
1007 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,
1008 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,
1009 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,
1012 static unsigned long
1013 bitmap_popcount (BITMAP_WORD a)
1015 unsigned long ret = 0;
1016 unsigned i;
1018 /* Just do this the table way for now */
1019 for (i = 0; i < BITMAP_WORD_BITS; i+= 8)
1020 ret += popcount_table[(a >> i) & 0xff];
1021 return ret;
1023 #endif
1025 /* Count and return the number of bits set in the bitmap word BITS. */
1026 static unsigned long
1027 bitmap_count_bits_in_word (const BITMAP_WORD *bits)
1029 unsigned long count = 0;
1031 for (unsigned ix = 0; ix != BITMAP_ELEMENT_WORDS; ix++)
1033 #if GCC_VERSION >= 3400
1034 /* Note that popcountl matches BITMAP_WORD in type, so the actual size
1035 of BITMAP_WORD is not material. */
1036 count += __builtin_popcountl (bits[ix]);
1037 #else
1038 count += bitmap_popcount (bits[ix]);
1039 #endif
1041 return count;
1044 /* Count the number of bits set in the bitmap, and return it. */
1046 unsigned long
1047 bitmap_count_bits (const_bitmap a)
1049 unsigned long count = 0;
1050 const bitmap_element *elt;
1052 gcc_checking_assert (!a->tree_form);
1053 for (elt = a->first; elt; elt = elt->next)
1054 count += bitmap_count_bits_in_word (elt->bits);
1056 return count;
1059 /* Count the number of unique bits set in A and B and return it. */
1061 unsigned long
1062 bitmap_count_unique_bits (const_bitmap a, const_bitmap b)
1064 unsigned long count = 0;
1065 const bitmap_element *elt_a, *elt_b;
1067 for (elt_a = a->first, elt_b = b->first; elt_a && elt_b; )
1069 /* If we're at different indices, then count all the bits
1070 in the lower element. If we're at the same index, then
1071 count the bits in the IOR of the two elements. */
1072 if (elt_a->indx < elt_b->indx)
1074 count += bitmap_count_bits_in_word (elt_a->bits);
1075 elt_a = elt_a->next;
1077 else if (elt_b->indx < elt_a->indx)
1079 count += bitmap_count_bits_in_word (elt_b->bits);
1080 elt_b = elt_b->next;
1082 else
1084 BITMAP_WORD bits[BITMAP_ELEMENT_WORDS];
1085 for (unsigned ix = 0; ix != BITMAP_ELEMENT_WORDS; ix++)
1086 bits[ix] = elt_a->bits[ix] | elt_b->bits[ix];
1087 count += bitmap_count_bits_in_word (bits);
1088 elt_a = elt_a->next;
1089 elt_b = elt_b->next;
1092 return count;
1095 /* Return true if the bitmap has a single bit set. Otherwise return
1096 false. */
1098 bool
1099 bitmap_single_bit_set_p (const_bitmap a)
1101 unsigned long count = 0;
1102 const bitmap_element *elt;
1103 unsigned ix;
1105 if (bitmap_empty_p (a))
1106 return false;
1108 elt = a->first;
1110 /* As there are no completely empty bitmap elements, a second one
1111 means we have more than one bit set. */
1112 if (elt->next != NULL
1113 && (!a->tree_form || elt->prev != NULL))
1114 return false;
1116 for (ix = 0; ix != BITMAP_ELEMENT_WORDS; ix++)
1118 #if GCC_VERSION >= 3400
1119 /* Note that popcountl matches BITMAP_WORD in type, so the actual size
1120 of BITMAP_WORD is not material. */
1121 count += __builtin_popcountl (elt->bits[ix]);
1122 #else
1123 count += bitmap_popcount (elt->bits[ix]);
1124 #endif
1125 if (count > 1)
1126 return false;
1129 return count == 1;
1133 /* Return the bit number of the first set bit in the bitmap. The
1134 bitmap must be non-empty. */
1136 unsigned
1137 bitmap_first_set_bit (const_bitmap a)
1139 const bitmap_element *elt = a->first;
1140 unsigned bit_no;
1141 BITMAP_WORD word;
1142 unsigned ix;
1144 gcc_checking_assert (elt);
1146 if (a->tree_form)
1147 while (elt->prev)
1148 elt = elt->prev;
1150 bit_no = elt->indx * BITMAP_ELEMENT_ALL_BITS;
1151 for (ix = 0; ix != BITMAP_ELEMENT_WORDS; ix++)
1153 word = elt->bits[ix];
1154 if (word)
1155 goto found_bit;
1157 gcc_unreachable ();
1158 found_bit:
1159 bit_no += ix * BITMAP_WORD_BITS;
1161 #if GCC_VERSION >= 3004
1162 gcc_assert (sizeof (long) == sizeof (word));
1163 bit_no += __builtin_ctzl (word);
1164 #else
1165 /* Binary search for the first set bit. */
1166 #if BITMAP_WORD_BITS > 64
1167 #error "Fill out the table."
1168 #endif
1169 #if BITMAP_WORD_BITS > 32
1170 if (!(word & 0xffffffff))
1171 word >>= 32, bit_no += 32;
1172 #endif
1173 if (!(word & 0xffff))
1174 word >>= 16, bit_no += 16;
1175 if (!(word & 0xff))
1176 word >>= 8, bit_no += 8;
1177 if (!(word & 0xf))
1178 word >>= 4, bit_no += 4;
1179 if (!(word & 0x3))
1180 word >>= 2, bit_no += 2;
1181 if (!(word & 0x1))
1182 word >>= 1, bit_no += 1;
1184 gcc_checking_assert (word & 1);
1185 #endif
1186 return bit_no;
1189 /* Return the bit number of the first set bit in the bitmap. The
1190 bitmap must be non-empty. */
1192 unsigned
1193 bitmap_last_set_bit (const_bitmap a)
1195 const bitmap_element *elt;
1196 unsigned bit_no;
1197 BITMAP_WORD word;
1198 int ix;
1200 if (a->tree_form)
1201 elt = a->first;
1202 else
1203 elt = a->current ? a->current : a->first;
1204 gcc_checking_assert (elt);
1206 while (elt->next)
1207 elt = elt->next;
1209 bit_no = elt->indx * BITMAP_ELEMENT_ALL_BITS;
1210 for (ix = BITMAP_ELEMENT_WORDS - 1; ix >= 1; ix--)
1212 word = elt->bits[ix];
1213 if (word)
1214 goto found_bit;
1216 gcc_assert (elt->bits[ix] != 0);
1217 found_bit:
1218 bit_no += ix * BITMAP_WORD_BITS;
1219 #if GCC_VERSION >= 3004
1220 gcc_assert (sizeof (long) == sizeof (word));
1221 bit_no += BITMAP_WORD_BITS - __builtin_clzl (word) - 1;
1222 #else
1223 /* Hopefully this is a twos-complement host... */
1224 BITMAP_WORD x = word;
1225 x |= (x >> 1);
1226 x |= (x >> 2);
1227 x |= (x >> 4);
1228 x |= (x >> 8);
1229 x |= (x >> 16);
1230 #if BITMAP_WORD_BITS > 32
1231 x |= (x >> 32);
1232 #endif
1233 bit_no += bitmap_popcount (x) - 1;
1234 #endif
1236 return bit_no;
1240 /* DST = A & B. */
1242 void
1243 bitmap_and (bitmap dst, const_bitmap a, const_bitmap b)
1245 bitmap_element *dst_elt = dst->first;
1246 const bitmap_element *a_elt = a->first;
1247 const bitmap_element *b_elt = b->first;
1248 bitmap_element *dst_prev = NULL;
1250 gcc_checking_assert (!dst->tree_form && !a->tree_form && !b->tree_form);
1251 gcc_assert (dst != a && dst != b);
1253 if (a == b)
1255 bitmap_copy (dst, a);
1256 return;
1259 while (a_elt && b_elt)
1261 if (a_elt->indx < b_elt->indx)
1262 a_elt = a_elt->next;
1263 else if (b_elt->indx < a_elt->indx)
1264 b_elt = b_elt->next;
1265 else
1267 /* Matching elts, generate A & B. */
1268 unsigned ix;
1269 BITMAP_WORD ior = 0;
1271 if (!dst_elt)
1272 dst_elt = bitmap_list_insert_element_after (dst, dst_prev,
1273 a_elt->indx);
1274 else
1275 dst_elt->indx = a_elt->indx;
1276 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1278 BITMAP_WORD r = a_elt->bits[ix] & b_elt->bits[ix];
1280 dst_elt->bits[ix] = r;
1281 ior |= r;
1283 if (ior)
1285 dst_prev = dst_elt;
1286 dst_elt = dst_elt->next;
1288 a_elt = a_elt->next;
1289 b_elt = b_elt->next;
1292 /* Ensure that dst->current is valid. */
1293 dst->current = dst->first;
1294 bitmap_elt_clear_from (dst, dst_elt);
1295 gcc_checking_assert (!dst->current == !dst->first);
1296 if (dst->current)
1297 dst->indx = dst->current->indx;
1300 /* A &= B. Return true if A changed. */
1302 bool
1303 bitmap_and_into (bitmap a, const_bitmap b)
1305 bitmap_element *a_elt = a->first;
1306 const bitmap_element *b_elt = b->first;
1307 bitmap_element *next;
1308 bool changed = false;
1310 gcc_checking_assert (!a->tree_form && !b->tree_form);
1312 if (a == b)
1313 return false;
1315 while (a_elt && b_elt)
1317 if (a_elt->indx < b_elt->indx)
1319 next = a_elt->next;
1320 bitmap_list_unlink_element (a, a_elt);
1321 a_elt = next;
1322 changed = true;
1324 else if (b_elt->indx < a_elt->indx)
1325 b_elt = b_elt->next;
1326 else
1328 /* Matching elts, generate A &= B. */
1329 unsigned ix;
1330 BITMAP_WORD ior = 0;
1332 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1334 BITMAP_WORD r = a_elt->bits[ix] & b_elt->bits[ix];
1335 if (a_elt->bits[ix] != r)
1336 changed = true;
1337 a_elt->bits[ix] = r;
1338 ior |= r;
1340 next = a_elt->next;
1341 if (!ior)
1342 bitmap_list_unlink_element (a, a_elt);
1343 a_elt = next;
1344 b_elt = b_elt->next;
1348 if (a_elt)
1350 changed = true;
1351 bitmap_elt_clear_from (a, a_elt);
1354 gcc_checking_assert (!a->current == !a->first
1355 && (!a->current || a->indx == a->current->indx));
1357 return changed;
1361 /* Insert an element equal to SRC_ELT after DST_PREV, overwriting DST_ELT
1362 if non-NULL. CHANGED is true if the destination bitmap had already been
1363 changed; the new value of CHANGED is returned. */
1365 static inline bool
1366 bitmap_elt_copy (bitmap dst, bitmap_element *dst_elt, bitmap_element *dst_prev,
1367 const bitmap_element *src_elt, bool changed)
1369 if (!changed && dst_elt && dst_elt->indx == src_elt->indx)
1371 unsigned ix;
1373 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1374 if (src_elt->bits[ix] != dst_elt->bits[ix])
1376 dst_elt->bits[ix] = src_elt->bits[ix];
1377 changed = true;
1380 else
1382 changed = true;
1383 if (!dst_elt)
1384 dst_elt = bitmap_list_insert_element_after (dst, dst_prev,
1385 src_elt->indx);
1386 else
1387 dst_elt->indx = src_elt->indx;
1388 memcpy (dst_elt->bits, src_elt->bits, sizeof (dst_elt->bits));
1390 return changed;
1395 /* DST = A & ~B */
1397 bool
1398 bitmap_and_compl (bitmap dst, const_bitmap a, const_bitmap b)
1400 bitmap_element *dst_elt = dst->first;
1401 const bitmap_element *a_elt = a->first;
1402 const bitmap_element *b_elt = b->first;
1403 bitmap_element *dst_prev = NULL;
1404 bitmap_element **dst_prev_pnext = &dst->first;
1405 bool changed = false;
1407 gcc_checking_assert (!dst->tree_form && !a->tree_form && !b->tree_form);
1408 gcc_assert (dst != a && dst != b);
1410 if (a == b)
1412 changed = !bitmap_empty_p (dst);
1413 bitmap_clear (dst);
1414 return changed;
1417 while (a_elt)
1419 while (b_elt && b_elt->indx < a_elt->indx)
1420 b_elt = b_elt->next;
1422 if (!b_elt || b_elt->indx > a_elt->indx)
1424 changed = bitmap_elt_copy (dst, dst_elt, dst_prev, a_elt, changed);
1425 dst_prev = *dst_prev_pnext;
1426 dst_prev_pnext = &dst_prev->next;
1427 dst_elt = *dst_prev_pnext;
1428 a_elt = a_elt->next;
1431 else
1433 /* Matching elts, generate A & ~B. */
1434 unsigned ix;
1435 BITMAP_WORD ior = 0;
1437 if (!changed && dst_elt && dst_elt->indx == a_elt->indx)
1439 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1441 BITMAP_WORD r = a_elt->bits[ix] & ~b_elt->bits[ix];
1443 if (dst_elt->bits[ix] != r)
1445 changed = true;
1446 dst_elt->bits[ix] = r;
1448 ior |= r;
1451 else
1453 bool new_element;
1454 if (!dst_elt || dst_elt->indx > a_elt->indx)
1456 dst_elt = bitmap_list_insert_element_after (dst, dst_prev,
1457 a_elt->indx);
1458 new_element = true;
1460 else
1462 dst_elt->indx = a_elt->indx;
1463 new_element = false;
1466 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1468 BITMAP_WORD r = a_elt->bits[ix] & ~b_elt->bits[ix];
1470 dst_elt->bits[ix] = r;
1471 ior |= r;
1474 if (ior)
1475 changed = true;
1476 else
1478 changed |= !new_element;
1479 bitmap_list_unlink_element (dst, dst_elt);
1480 dst_elt = *dst_prev_pnext;
1484 if (ior)
1486 dst_prev = *dst_prev_pnext;
1487 dst_prev_pnext = &dst_prev->next;
1488 dst_elt = *dst_prev_pnext;
1490 a_elt = a_elt->next;
1491 b_elt = b_elt->next;
1495 /* Ensure that dst->current is valid. */
1496 dst->current = dst->first;
1498 if (dst_elt)
1500 changed = true;
1501 bitmap_elt_clear_from (dst, dst_elt);
1503 gcc_checking_assert (!dst->current == !dst->first);
1504 if (dst->current)
1505 dst->indx = dst->current->indx;
1507 return changed;
1510 /* A &= ~B. Returns true if A changes */
1512 bool
1513 bitmap_and_compl_into (bitmap a, const_bitmap b)
1515 bitmap_element *a_elt = a->first;
1516 const bitmap_element *b_elt = b->first;
1517 bitmap_element *next;
1518 BITMAP_WORD changed = 0;
1520 gcc_checking_assert (!a->tree_form && !b->tree_form);
1522 if (a == b)
1524 if (bitmap_empty_p (a))
1525 return false;
1526 else
1528 bitmap_clear (a);
1529 return true;
1533 while (a_elt && b_elt)
1535 if (a_elt->indx < b_elt->indx)
1536 a_elt = a_elt->next;
1537 else if (b_elt->indx < a_elt->indx)
1538 b_elt = b_elt->next;
1539 else
1541 /* Matching elts, generate A &= ~B. */
1542 unsigned ix;
1543 BITMAP_WORD ior = 0;
1545 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1547 BITMAP_WORD cleared = a_elt->bits[ix] & b_elt->bits[ix];
1548 BITMAP_WORD r = a_elt->bits[ix] ^ cleared;
1550 a_elt->bits[ix] = r;
1551 changed |= cleared;
1552 ior |= r;
1554 next = a_elt->next;
1555 if (!ior)
1556 bitmap_list_unlink_element (a, a_elt);
1557 a_elt = next;
1558 b_elt = b_elt->next;
1561 gcc_checking_assert (!a->current == !a->first
1562 && (!a->current || a->indx == a->current->indx));
1563 return changed != 0;
1566 /* Set COUNT bits from START in HEAD. */
1567 void
1568 bitmap_set_range (bitmap head, unsigned int start, unsigned int count)
1570 unsigned int first_index, end_bit_plus1, last_index;
1571 bitmap_element *elt, *elt_prev;
1572 unsigned int i;
1574 gcc_checking_assert (!head->tree_form);
1576 if (!count)
1577 return;
1579 if (count == 1)
1581 bitmap_set_bit (head, start);
1582 return;
1585 first_index = start / BITMAP_ELEMENT_ALL_BITS;
1586 end_bit_plus1 = start + count;
1587 last_index = (end_bit_plus1 - 1) / BITMAP_ELEMENT_ALL_BITS;
1588 elt = bitmap_list_find_element (head, first_index);
1590 /* If bitmap_list_find_element returns zero, the current is the closest block
1591 to the result. Otherwise, just use bitmap_element_allocate to
1592 ensure ELT is set; in the loop below, ELT == NULL means "insert
1593 at the end of the bitmap". */
1594 if (!elt)
1596 elt = bitmap_element_allocate (head);
1597 elt->indx = first_index;
1598 bitmap_list_link_element (head, elt);
1601 gcc_checking_assert (elt->indx == first_index);
1602 elt_prev = elt->prev;
1603 for (i = first_index; i <= last_index; i++)
1605 unsigned elt_start_bit = i * BITMAP_ELEMENT_ALL_BITS;
1606 unsigned elt_end_bit_plus1 = elt_start_bit + BITMAP_ELEMENT_ALL_BITS;
1608 unsigned int first_word_to_mod;
1609 BITMAP_WORD first_mask;
1610 unsigned int last_word_to_mod;
1611 BITMAP_WORD last_mask;
1612 unsigned int ix;
1614 if (!elt || elt->indx != i)
1615 elt = bitmap_list_insert_element_after (head, elt_prev, i);
1617 if (elt_start_bit <= start)
1619 /* The first bit to turn on is somewhere inside this
1620 elt. */
1621 first_word_to_mod = (start - elt_start_bit) / BITMAP_WORD_BITS;
1623 /* This mask should have 1s in all bits >= start position. */
1624 first_mask =
1625 (((BITMAP_WORD) 1) << ((start % BITMAP_WORD_BITS))) - 1;
1626 first_mask = ~first_mask;
1628 else
1630 /* The first bit to turn on is below this start of this elt. */
1631 first_word_to_mod = 0;
1632 first_mask = ~(BITMAP_WORD) 0;
1635 if (elt_end_bit_plus1 <= end_bit_plus1)
1637 /* The last bit to turn on is beyond this elt. */
1638 last_word_to_mod = BITMAP_ELEMENT_WORDS - 1;
1639 last_mask = ~(BITMAP_WORD) 0;
1641 else
1643 /* The last bit to turn on is inside to this elt. */
1644 last_word_to_mod =
1645 (end_bit_plus1 - elt_start_bit) / BITMAP_WORD_BITS;
1647 /* The last mask should have 1s below the end bit. */
1648 last_mask =
1649 (((BITMAP_WORD) 1) << ((end_bit_plus1 % BITMAP_WORD_BITS))) - 1;
1652 if (first_word_to_mod == last_word_to_mod)
1654 BITMAP_WORD mask = first_mask & last_mask;
1655 elt->bits[first_word_to_mod] |= mask;
1657 else
1659 elt->bits[first_word_to_mod] |= first_mask;
1660 if (BITMAP_ELEMENT_WORDS > 2)
1661 for (ix = first_word_to_mod + 1; ix < last_word_to_mod; ix++)
1662 elt->bits[ix] = ~(BITMAP_WORD) 0;
1663 elt->bits[last_word_to_mod] |= last_mask;
1666 elt_prev = elt;
1667 elt = elt->next;
1670 head->current = elt ? elt : elt_prev;
1671 head->indx = head->current->indx;
1674 /* Clear COUNT bits from START in HEAD. */
1675 void
1676 bitmap_clear_range (bitmap head, unsigned int start, unsigned int count)
1678 unsigned int first_index, end_bit_plus1, last_index;
1679 bitmap_element *elt;
1681 gcc_checking_assert (!head->tree_form);
1683 if (!count)
1684 return;
1686 if (count == 1)
1688 bitmap_clear_bit (head, start);
1689 return;
1692 first_index = start / BITMAP_ELEMENT_ALL_BITS;
1693 end_bit_plus1 = start + count;
1694 last_index = (end_bit_plus1 - 1) / BITMAP_ELEMENT_ALL_BITS;
1695 elt = bitmap_list_find_element (head, first_index);
1697 /* If bitmap_list_find_element returns zero, the current is the closest block
1698 to the result. If the current is less than first index, find the
1699 next one. Otherwise, just set elt to be current. */
1700 if (!elt)
1702 if (head->current)
1704 if (head->indx < first_index)
1706 elt = head->current->next;
1707 if (!elt)
1708 return;
1710 else
1711 elt = head->current;
1713 else
1714 return;
1717 while (elt && (elt->indx <= last_index))
1719 bitmap_element * next_elt = elt->next;
1720 unsigned elt_start_bit = (elt->indx) * BITMAP_ELEMENT_ALL_BITS;
1721 unsigned elt_end_bit_plus1 = elt_start_bit + BITMAP_ELEMENT_ALL_BITS;
1724 if (elt_start_bit >= start && elt_end_bit_plus1 <= end_bit_plus1)
1725 /* Get rid of the entire elt and go to the next one. */
1726 bitmap_list_unlink_element (head, elt);
1727 else
1729 /* Going to have to knock out some bits in this elt. */
1730 unsigned int first_word_to_mod;
1731 BITMAP_WORD first_mask;
1732 unsigned int last_word_to_mod;
1733 BITMAP_WORD last_mask;
1734 unsigned int i;
1735 bool clear = true;
1737 if (elt_start_bit <= start)
1739 /* The first bit to turn off is somewhere inside this
1740 elt. */
1741 first_word_to_mod = (start - elt_start_bit) / BITMAP_WORD_BITS;
1743 /* This mask should have 1s in all bits >= start position. */
1744 first_mask =
1745 (((BITMAP_WORD) 1) << ((start % BITMAP_WORD_BITS))) - 1;
1746 first_mask = ~first_mask;
1748 else
1750 /* The first bit to turn off is below this start of this elt. */
1751 first_word_to_mod = 0;
1752 first_mask = 0;
1753 first_mask = ~first_mask;
1756 if (elt_end_bit_plus1 <= end_bit_plus1)
1758 /* The last bit to turn off is beyond this elt. */
1759 last_word_to_mod = BITMAP_ELEMENT_WORDS - 1;
1760 last_mask = 0;
1761 last_mask = ~last_mask;
1763 else
1765 /* The last bit to turn off is inside to this elt. */
1766 last_word_to_mod =
1767 (end_bit_plus1 - elt_start_bit) / BITMAP_WORD_BITS;
1769 /* The last mask should have 1s below the end bit. */
1770 last_mask =
1771 (((BITMAP_WORD) 1) << (((end_bit_plus1) % BITMAP_WORD_BITS))) - 1;
1775 if (first_word_to_mod == last_word_to_mod)
1777 BITMAP_WORD mask = first_mask & last_mask;
1778 elt->bits[first_word_to_mod] &= ~mask;
1780 else
1782 elt->bits[first_word_to_mod] &= ~first_mask;
1783 if (BITMAP_ELEMENT_WORDS > 2)
1784 for (i = first_word_to_mod + 1; i < last_word_to_mod; i++)
1785 elt->bits[i] = 0;
1786 elt->bits[last_word_to_mod] &= ~last_mask;
1788 for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
1789 if (elt->bits[i])
1791 clear = false;
1792 break;
1794 /* Check to see if there are any bits left. */
1795 if (clear)
1796 bitmap_list_unlink_element (head, elt);
1798 elt = next_elt;
1801 if (elt)
1803 head->current = elt;
1804 head->indx = head->current->indx;
1808 /* A = ~A & B. */
1810 void
1811 bitmap_compl_and_into (bitmap a, const_bitmap b)
1813 bitmap_element *a_elt = a->first;
1814 const bitmap_element *b_elt = b->first;
1815 bitmap_element *a_prev = NULL;
1816 bitmap_element *next;
1818 gcc_checking_assert (!a->tree_form && !b->tree_form);
1819 gcc_assert (a != b);
1821 if (bitmap_empty_p (a))
1823 bitmap_copy (a, b);
1824 return;
1826 if (bitmap_empty_p (b))
1828 bitmap_clear (a);
1829 return;
1832 while (a_elt || b_elt)
1834 if (!b_elt || (a_elt && a_elt->indx < b_elt->indx))
1836 /* A is before B. Remove A */
1837 next = a_elt->next;
1838 a_prev = a_elt->prev;
1839 bitmap_list_unlink_element (a, a_elt);
1840 a_elt = next;
1842 else if (!a_elt || b_elt->indx < a_elt->indx)
1844 /* B is before A. Copy B. */
1845 next = bitmap_list_insert_element_after (a, a_prev, b_elt->indx);
1846 memcpy (next->bits, b_elt->bits, sizeof (next->bits));
1847 a_prev = next;
1848 b_elt = b_elt->next;
1850 else
1852 /* Matching elts, generate A = ~A & B. */
1853 unsigned ix;
1854 BITMAP_WORD ior = 0;
1856 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1858 BITMAP_WORD cleared = a_elt->bits[ix] & b_elt->bits[ix];
1859 BITMAP_WORD r = b_elt->bits[ix] ^ cleared;
1861 a_elt->bits[ix] = r;
1862 ior |= r;
1864 next = a_elt->next;
1865 if (!ior)
1866 bitmap_list_unlink_element (a, a_elt);
1867 else
1868 a_prev = a_elt;
1869 a_elt = next;
1870 b_elt = b_elt->next;
1873 gcc_checking_assert (!a->current == !a->first
1874 && (!a->current || a->indx == a->current->indx));
1875 return;
1879 /* Insert an element corresponding to A_ELT | B_ELT after DST_PREV,
1880 overwriting DST_ELT if non-NULL. CHANGED is true if the destination bitmap
1881 had already been changed; the new value of CHANGED is returned. */
1883 static inline bool
1884 bitmap_elt_ior (bitmap dst, bitmap_element *dst_elt, bitmap_element *dst_prev,
1885 const bitmap_element *a_elt, const bitmap_element *b_elt,
1886 bool changed)
1888 gcc_assert (a_elt || b_elt);
1890 if (a_elt && b_elt && a_elt->indx == b_elt->indx)
1892 /* Matching elts, generate A | B. */
1893 unsigned ix;
1895 if (!changed && dst_elt && dst_elt->indx == a_elt->indx)
1897 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1899 BITMAP_WORD r = a_elt->bits[ix] | b_elt->bits[ix];
1900 if (r != dst_elt->bits[ix])
1902 dst_elt->bits[ix] = r;
1903 changed = true;
1907 else
1909 changed = true;
1910 if (!dst_elt)
1911 dst_elt = bitmap_list_insert_element_after (dst, dst_prev,
1912 a_elt->indx);
1913 else
1914 dst_elt->indx = a_elt->indx;
1915 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1917 BITMAP_WORD r = a_elt->bits[ix] | b_elt->bits[ix];
1918 dst_elt->bits[ix] = r;
1922 else
1924 /* Copy a single element. */
1925 const bitmap_element *src;
1927 if (!b_elt || (a_elt && a_elt->indx < b_elt->indx))
1928 src = a_elt;
1929 else
1930 src = b_elt;
1932 gcc_checking_assert (src);
1933 changed = bitmap_elt_copy (dst, dst_elt, dst_prev, src, changed);
1935 return changed;
1939 /* DST = A | B. Return true if DST changes. */
1941 bool
1942 bitmap_ior (bitmap dst, const_bitmap a, const_bitmap b)
1944 bitmap_element *dst_elt = dst->first;
1945 const bitmap_element *a_elt = a->first;
1946 const bitmap_element *b_elt = b->first;
1947 bitmap_element *dst_prev = NULL;
1948 bitmap_element **dst_prev_pnext = &dst->first;
1949 bool changed = false;
1951 gcc_checking_assert (!dst->tree_form && !a->tree_form && !b->tree_form);
1952 gcc_assert (dst != a && dst != b);
1954 while (a_elt || b_elt)
1956 changed = bitmap_elt_ior (dst, dst_elt, dst_prev, a_elt, b_elt, changed);
1958 if (a_elt && b_elt && a_elt->indx == b_elt->indx)
1960 a_elt = a_elt->next;
1961 b_elt = b_elt->next;
1963 else
1965 if (a_elt && (!b_elt || a_elt->indx <= b_elt->indx))
1966 a_elt = a_elt->next;
1967 else if (b_elt && (!a_elt || b_elt->indx <= a_elt->indx))
1968 b_elt = b_elt->next;
1971 dst_prev = *dst_prev_pnext;
1972 dst_prev_pnext = &dst_prev->next;
1973 dst_elt = *dst_prev_pnext;
1976 if (dst_elt)
1978 changed = true;
1979 /* Ensure that dst->current is valid. */
1980 dst->current = dst->first;
1981 bitmap_elt_clear_from (dst, dst_elt);
1983 gcc_checking_assert (!dst->current == !dst->first);
1984 if (dst->current)
1985 dst->indx = dst->current->indx;
1986 return changed;
1989 /* A |= B. Return true if A changes. */
1991 bool
1992 bitmap_ior_into (bitmap a, const_bitmap b)
1994 bitmap_element *a_elt = a->first;
1995 const bitmap_element *b_elt = b->first;
1996 bitmap_element *a_prev = NULL;
1997 bitmap_element **a_prev_pnext = &a->first;
1998 bool changed = false;
2000 gcc_checking_assert (!a->tree_form && !b->tree_form);
2001 if (a == b)
2002 return false;
2004 while (b_elt)
2006 /* If A lags behind B, just advance it. */
2007 if (!a_elt || a_elt->indx == b_elt->indx)
2009 changed = bitmap_elt_ior (a, a_elt, a_prev, a_elt, b_elt, changed);
2010 b_elt = b_elt->next;
2012 else if (a_elt->indx > b_elt->indx)
2014 changed = bitmap_elt_copy (a, NULL, a_prev, b_elt, changed);
2015 b_elt = b_elt->next;
2018 a_prev = *a_prev_pnext;
2019 a_prev_pnext = &a_prev->next;
2020 a_elt = *a_prev_pnext;
2023 gcc_checking_assert (!a->current == !a->first);
2024 if (a->current)
2025 a->indx = a->current->indx;
2026 return changed;
2029 /* DST = A ^ B */
2031 void
2032 bitmap_xor (bitmap dst, const_bitmap a, const_bitmap b)
2034 bitmap_element *dst_elt = dst->first;
2035 const bitmap_element *a_elt = a->first;
2036 const bitmap_element *b_elt = b->first;
2037 bitmap_element *dst_prev = NULL;
2039 gcc_checking_assert (!dst->tree_form && !a->tree_form && !b->tree_form);
2040 gcc_assert (dst != a && dst != b);
2042 if (a == b)
2044 bitmap_clear (dst);
2045 return;
2048 while (a_elt || b_elt)
2050 if (a_elt && b_elt && a_elt->indx == b_elt->indx)
2052 /* Matching elts, generate A ^ B. */
2053 unsigned ix;
2054 BITMAP_WORD ior = 0;
2056 if (!dst_elt)
2057 dst_elt = bitmap_list_insert_element_after (dst, dst_prev,
2058 a_elt->indx);
2059 else
2060 dst_elt->indx = a_elt->indx;
2061 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
2063 BITMAP_WORD r = a_elt->bits[ix] ^ b_elt->bits[ix];
2065 ior |= r;
2066 dst_elt->bits[ix] = r;
2068 a_elt = a_elt->next;
2069 b_elt = b_elt->next;
2070 if (ior)
2072 dst_prev = dst_elt;
2073 dst_elt = dst_elt->next;
2076 else
2078 /* Copy a single element. */
2079 const bitmap_element *src;
2081 if (!b_elt || (a_elt && a_elt->indx < b_elt->indx))
2083 src = a_elt;
2084 a_elt = a_elt->next;
2086 else
2088 src = b_elt;
2089 b_elt = b_elt->next;
2092 if (!dst_elt)
2093 dst_elt = bitmap_list_insert_element_after (dst, dst_prev,
2094 src->indx);
2095 else
2096 dst_elt->indx = src->indx;
2097 memcpy (dst_elt->bits, src->bits, sizeof (dst_elt->bits));
2098 dst_prev = dst_elt;
2099 dst_elt = dst_elt->next;
2102 /* Ensure that dst->current is valid. */
2103 dst->current = dst->first;
2104 bitmap_elt_clear_from (dst, dst_elt);
2105 gcc_checking_assert (!dst->current == !dst->first);
2106 if (dst->current)
2107 dst->indx = dst->current->indx;
2110 /* A ^= B */
2112 void
2113 bitmap_xor_into (bitmap a, const_bitmap b)
2115 bitmap_element *a_elt = a->first;
2116 const bitmap_element *b_elt = b->first;
2117 bitmap_element *a_prev = NULL;
2119 gcc_checking_assert (!a->tree_form && !b->tree_form);
2121 if (a == b)
2123 bitmap_clear (a);
2124 return;
2127 while (b_elt)
2129 if (!a_elt || b_elt->indx < a_elt->indx)
2131 /* Copy b_elt. */
2132 bitmap_element *dst = bitmap_list_insert_element_after (a, a_prev,
2133 b_elt->indx);
2134 memcpy (dst->bits, b_elt->bits, sizeof (dst->bits));
2135 a_prev = dst;
2136 b_elt = b_elt->next;
2138 else if (a_elt->indx < b_elt->indx)
2140 a_prev = a_elt;
2141 a_elt = a_elt->next;
2143 else
2145 /* Matching elts, generate A ^= B. */
2146 unsigned ix;
2147 BITMAP_WORD ior = 0;
2148 bitmap_element *next = a_elt->next;
2150 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
2152 BITMAP_WORD r = a_elt->bits[ix] ^ b_elt->bits[ix];
2154 ior |= r;
2155 a_elt->bits[ix] = r;
2157 b_elt = b_elt->next;
2158 if (ior)
2159 a_prev = a_elt;
2160 else
2161 bitmap_list_unlink_element (a, a_elt);
2162 a_elt = next;
2165 gcc_checking_assert (!a->current == !a->first);
2166 if (a->current)
2167 a->indx = a->current->indx;
2170 /* Return true if two bitmaps are identical.
2171 We do not bother with a check for pointer equality, as that never
2172 occurs in practice. */
2174 bool
2175 bitmap_equal_p (const_bitmap a, const_bitmap b)
2177 const bitmap_element *a_elt;
2178 const bitmap_element *b_elt;
2179 unsigned ix;
2181 gcc_checking_assert (!a->tree_form && !b->tree_form);
2183 for (a_elt = a->first, b_elt = b->first;
2184 a_elt && b_elt;
2185 a_elt = a_elt->next, b_elt = b_elt->next)
2187 if (a_elt->indx != b_elt->indx)
2188 return false;
2189 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
2190 if (a_elt->bits[ix] != b_elt->bits[ix])
2191 return false;
2193 return !a_elt && !b_elt;
2196 /* Return true if A AND B is not empty. */
2198 bool
2199 bitmap_intersect_p (const_bitmap a, const_bitmap b)
2201 const bitmap_element *a_elt;
2202 const bitmap_element *b_elt;
2203 unsigned ix;
2205 gcc_checking_assert (!a->tree_form && !b->tree_form);
2207 for (a_elt = a->first, b_elt = b->first;
2208 a_elt && b_elt;)
2210 if (a_elt->indx < b_elt->indx)
2211 a_elt = a_elt->next;
2212 else if (b_elt->indx < a_elt->indx)
2213 b_elt = b_elt->next;
2214 else
2216 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
2217 if (a_elt->bits[ix] & b_elt->bits[ix])
2218 return true;
2219 a_elt = a_elt->next;
2220 b_elt = b_elt->next;
2223 return false;
2226 /* Return true if A AND NOT B is not empty. */
2228 bool
2229 bitmap_intersect_compl_p (const_bitmap a, const_bitmap b)
2231 const bitmap_element *a_elt;
2232 const bitmap_element *b_elt;
2233 unsigned ix;
2235 gcc_checking_assert (!a->tree_form && !b->tree_form);
2237 for (a_elt = a->first, b_elt = b->first;
2238 a_elt && b_elt;)
2240 if (a_elt->indx < b_elt->indx)
2241 return true;
2242 else if (b_elt->indx < a_elt->indx)
2243 b_elt = b_elt->next;
2244 else
2246 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
2247 if (a_elt->bits[ix] & ~b_elt->bits[ix])
2248 return true;
2249 a_elt = a_elt->next;
2250 b_elt = b_elt->next;
2253 return a_elt != NULL;
2257 /* DST = A | (FROM1 & ~FROM2). Return true if DST changes. */
2259 bool
2260 bitmap_ior_and_compl (bitmap dst, const_bitmap a, const_bitmap b, const_bitmap kill)
2262 bool changed = false;
2264 bitmap_element *dst_elt = dst->first;
2265 const bitmap_element *a_elt = a->first;
2266 const bitmap_element *b_elt = b->first;
2267 const bitmap_element *kill_elt = kill->first;
2268 bitmap_element *dst_prev = NULL;
2269 bitmap_element **dst_prev_pnext = &dst->first;
2271 gcc_checking_assert (!dst->tree_form && !a->tree_form && !b->tree_form
2272 && !kill->tree_form);
2273 gcc_assert (dst != a && dst != b && dst != kill);
2275 /* Special cases. We don't bother checking for bitmap_equal_p (b, kill). */
2276 if (b == kill || bitmap_empty_p (b))
2278 changed = !bitmap_equal_p (dst, a);
2279 if (changed)
2280 bitmap_copy (dst, a);
2281 return changed;
2283 if (bitmap_empty_p (kill))
2284 return bitmap_ior (dst, a, b);
2285 if (bitmap_empty_p (a))
2286 return bitmap_and_compl (dst, b, kill);
2288 while (a_elt || b_elt)
2290 bool new_element = false;
2292 if (b_elt)
2293 while (kill_elt && kill_elt->indx < b_elt->indx)
2294 kill_elt = kill_elt->next;
2296 if (b_elt && kill_elt && kill_elt->indx == b_elt->indx
2297 && (!a_elt || a_elt->indx >= b_elt->indx))
2299 bitmap_element tmp_elt;
2300 unsigned ix;
2302 BITMAP_WORD ior = 0;
2303 tmp_elt.indx = b_elt->indx;
2304 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
2306 BITMAP_WORD r = b_elt->bits[ix] & ~kill_elt->bits[ix];
2307 ior |= r;
2308 tmp_elt.bits[ix] = r;
2311 if (ior)
2313 changed = bitmap_elt_ior (dst, dst_elt, dst_prev,
2314 a_elt, &tmp_elt, changed);
2315 new_element = true;
2316 if (a_elt && a_elt->indx == b_elt->indx)
2317 a_elt = a_elt->next;
2320 b_elt = b_elt->next;
2321 kill_elt = kill_elt->next;
2323 else
2325 changed = bitmap_elt_ior (dst, dst_elt, dst_prev,
2326 a_elt, b_elt, changed);
2327 new_element = true;
2329 if (a_elt && b_elt && a_elt->indx == b_elt->indx)
2331 a_elt = a_elt->next;
2332 b_elt = b_elt->next;
2334 else
2336 if (a_elt && (!b_elt || a_elt->indx <= b_elt->indx))
2337 a_elt = a_elt->next;
2338 else if (b_elt && (!a_elt || b_elt->indx <= a_elt->indx))
2339 b_elt = b_elt->next;
2343 if (new_element)
2345 dst_prev = *dst_prev_pnext;
2346 dst_prev_pnext = &dst_prev->next;
2347 dst_elt = *dst_prev_pnext;
2351 if (dst_elt)
2353 changed = true;
2354 /* Ensure that dst->current is valid. */
2355 dst->current = dst->first;
2356 bitmap_elt_clear_from (dst, dst_elt);
2358 gcc_checking_assert (!dst->current == !dst->first);
2359 if (dst->current)
2360 dst->indx = dst->current->indx;
2362 return changed;
2365 /* A |= (B & ~C). Return true if A changes. */
2367 bool
2368 bitmap_ior_and_compl_into (bitmap a, const_bitmap b, const_bitmap c)
2370 bitmap_head tmp;
2371 bool changed;
2373 gcc_checking_assert (!a->tree_form && !b->tree_form && !c->tree_form);
2375 bitmap_initialize (&tmp, &bitmap_default_obstack);
2376 bitmap_and_compl (&tmp, b, c);
2377 changed = bitmap_ior_into (a, &tmp);
2378 bitmap_clear (&tmp);
2380 return changed;
2383 /* A |= (B & C). Return true if A changes. */
2385 bool
2386 bitmap_ior_and_into (bitmap a, const_bitmap b, const_bitmap c)
2388 bitmap_element *a_elt = a->first;
2389 const bitmap_element *b_elt = b->first;
2390 const bitmap_element *c_elt = c->first;
2391 bitmap_element and_elt;
2392 bitmap_element *a_prev = NULL;
2393 bitmap_element **a_prev_pnext = &a->first;
2394 bool changed = false;
2395 unsigned ix;
2397 gcc_checking_assert (!a->tree_form && !b->tree_form && !c->tree_form);
2399 if (b == c)
2400 return bitmap_ior_into (a, b);
2401 if (bitmap_empty_p (b) || bitmap_empty_p (c))
2402 return false;
2404 and_elt.indx = -1;
2405 while (b_elt && c_elt)
2407 BITMAP_WORD overall;
2409 /* Find a common item of B and C. */
2410 while (b_elt->indx != c_elt->indx)
2412 if (b_elt->indx < c_elt->indx)
2414 b_elt = b_elt->next;
2415 if (!b_elt)
2416 goto done;
2418 else
2420 c_elt = c_elt->next;
2421 if (!c_elt)
2422 goto done;
2426 overall = 0;
2427 and_elt.indx = b_elt->indx;
2428 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
2430 and_elt.bits[ix] = b_elt->bits[ix] & c_elt->bits[ix];
2431 overall |= and_elt.bits[ix];
2434 b_elt = b_elt->next;
2435 c_elt = c_elt->next;
2436 if (!overall)
2437 continue;
2439 /* Now find a place to insert AND_ELT. */
2442 ix = a_elt ? a_elt->indx : and_elt.indx;
2443 if (ix == and_elt.indx)
2444 changed = bitmap_elt_ior (a, a_elt, a_prev, a_elt, &and_elt, changed);
2445 else if (ix > and_elt.indx)
2446 changed = bitmap_elt_copy (a, NULL, a_prev, &and_elt, changed);
2448 a_prev = *a_prev_pnext;
2449 a_prev_pnext = &a_prev->next;
2450 a_elt = *a_prev_pnext;
2452 /* If A lagged behind B/C, we advanced it so loop once more. */
2454 while (ix < and_elt.indx);
2457 done:
2458 gcc_checking_assert (!a->current == !a->first);
2459 if (a->current)
2460 a->indx = a->current->indx;
2461 return changed;
2464 /* Compute hash of bitmap (for purposes of hashing). */
2466 hashval_t
2467 bitmap_hash (const_bitmap head)
2469 const bitmap_element *ptr;
2470 BITMAP_WORD hash = 0;
2471 int ix;
2473 gcc_checking_assert (!head->tree_form);
2475 for (ptr = head->first; ptr; ptr = ptr->next)
2477 hash ^= ptr->indx;
2478 for (ix = 0; ix != BITMAP_ELEMENT_WORDS; ix++)
2479 hash ^= ptr->bits[ix];
2481 return (hashval_t)hash;
2485 /* Function to obtain a vector of bitmap elements in bit order from
2486 HEAD in tree view. */
2488 static void
2489 bitmap_tree_to_vec (vec<bitmap_element *> &elts, const_bitmap head)
2491 gcc_checking_assert (head->tree_form);
2492 auto_vec<bitmap_element *, 32> stack;
2493 bitmap_element *e = head->first;
2494 while (true)
2496 while (e != NULL)
2498 stack.safe_push (e);
2499 e = e->prev;
2501 if (stack.is_empty ())
2502 break;
2504 e = stack.pop ();
2505 elts.safe_push (e);
2506 e = e->next;
2510 /* Debugging function to print out the contents of a bitmap element. */
2512 DEBUG_FUNCTION void
2513 debug_bitmap_elt_file (FILE *file, const bitmap_element *ptr)
2515 unsigned int i, j, col = 26;
2517 fprintf (file, "\t" HOST_PTR_PRINTF " next = " HOST_PTR_PRINTF
2518 " prev = " HOST_PTR_PRINTF " indx = %u\n\t\tbits = {",
2519 (const void*) ptr, (const void*) ptr->next,
2520 (const void*) ptr->prev, ptr->indx);
2522 for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
2523 for (j = 0; j < BITMAP_WORD_BITS; j++)
2524 if ((ptr->bits[i] >> j) & 1)
2526 if (col > 70)
2528 fprintf (file, "\n\t\t\t");
2529 col = 24;
2532 fprintf (file, " %u", (ptr->indx * BITMAP_ELEMENT_ALL_BITS
2533 + i * BITMAP_WORD_BITS + j));
2534 col += 4;
2537 fprintf (file, " }\n");
2540 /* Debugging function to print out the contents of a bitmap. */
2542 DEBUG_FUNCTION void
2543 debug_bitmap_file (FILE *file, const_bitmap head)
2545 const bitmap_element *ptr;
2547 fprintf (file, "\nfirst = " HOST_PTR_PRINTF
2548 " current = " HOST_PTR_PRINTF " indx = %u\n",
2549 (void *) head->first, (void *) head->current, head->indx);
2551 if (head->tree_form)
2553 auto_vec<bitmap_element *, 32> elts;
2554 bitmap_tree_to_vec (elts, head);
2555 for (unsigned i = 0; i < elts.length (); ++i)
2556 debug_bitmap_elt_file (file, elts[i]);
2558 else
2559 for (ptr = head->first; ptr; ptr = ptr->next)
2560 debug_bitmap_elt_file (file, ptr);
2563 /* Function to be called from the debugger to print the contents
2564 of a bitmap. */
2566 DEBUG_FUNCTION void
2567 debug_bitmap (const_bitmap head)
2569 debug_bitmap_file (stderr, head);
2572 /* Function to print out the contents of a bitmap. Unlike debug_bitmap_file,
2573 it does not print anything but the bits. */
2575 DEBUG_FUNCTION void
2576 bitmap_print (FILE *file, const_bitmap head, const char *prefix,
2577 const char *suffix)
2579 const char *comma = "";
2580 unsigned i;
2582 fputs (prefix, file);
2583 if (head->tree_form)
2585 auto_vec<bitmap_element *, 32> elts;
2586 bitmap_tree_to_vec (elts, head);
2587 for (i = 0; i < elts.length (); ++i)
2588 for (unsigned ix = 0; ix != BITMAP_ELEMENT_WORDS; ++ix)
2590 BITMAP_WORD word = elts[i]->bits[ix];
2591 for (unsigned bit = 0; bit != BITMAP_WORD_BITS; ++bit)
2592 if (word & ((BITMAP_WORD)1 << bit))
2594 fprintf (file, "%s%d", comma,
2595 (bit + BITMAP_WORD_BITS * ix
2596 + elts[i]->indx * BITMAP_ELEMENT_ALL_BITS));
2597 comma = ", ";
2601 else
2603 bitmap_iterator bi;
2604 EXECUTE_IF_SET_IN_BITMAP (head, 0, i, bi)
2606 fprintf (file, "%s%d", comma, i);
2607 comma = ", ";
2610 fputs (suffix, file);
2613 /* Output per-bitmap memory usage statistics. */
2614 void
2615 dump_bitmap_statistics (void)
2617 if (!GATHER_STATISTICS)
2618 return;
2620 bitmap_mem_desc.dump (BITMAP_ORIGIN);
2623 DEBUG_FUNCTION void
2624 debug (const bitmap_head &ref)
2626 dump_bitmap (stderr, &ref);
2629 DEBUG_FUNCTION void
2630 debug (const bitmap_head *ptr)
2632 if (ptr)
2633 debug (*ptr);
2634 else
2635 fprintf (stderr, "<nil>\n");
2638 void
2639 bitmap_head::dump ()
2641 debug (this);
2644 #if CHECKING_P
2646 namespace selftest {
2648 /* Selftests for bitmaps. */
2650 /* Freshly-created bitmaps ought to be empty. */
2652 static void
2653 test_gc_alloc ()
2655 bitmap b = bitmap_gc_alloc ();
2656 ASSERT_TRUE (bitmap_empty_p (b));
2659 /* Verify bitmap_set_range. */
2661 static void
2662 test_set_range ()
2664 bitmap b = bitmap_gc_alloc ();
2665 ASSERT_TRUE (bitmap_empty_p (b));
2667 bitmap_set_range (b, 7, 5);
2668 ASSERT_FALSE (bitmap_empty_p (b));
2669 ASSERT_EQ (5, bitmap_count_bits (b));
2671 /* Verify bitmap_bit_p at the boundaries. */
2672 ASSERT_FALSE (bitmap_bit_p (b, 6));
2673 ASSERT_TRUE (bitmap_bit_p (b, 7));
2674 ASSERT_TRUE (bitmap_bit_p (b, 11));
2675 ASSERT_FALSE (bitmap_bit_p (b, 12));
2678 /* Verify splitting a range into two pieces using bitmap_clear_bit. */
2680 static void
2681 test_clear_bit_in_middle ()
2683 bitmap b = bitmap_gc_alloc ();
2685 /* Set b to [100..200]. */
2686 bitmap_set_range (b, 100, 100);
2687 ASSERT_EQ (100, bitmap_count_bits (b));
2689 /* Clear a bit in the middle. */
2690 bool changed = bitmap_clear_bit (b, 150);
2691 ASSERT_TRUE (changed);
2692 ASSERT_EQ (99, bitmap_count_bits (b));
2693 ASSERT_TRUE (bitmap_bit_p (b, 149));
2694 ASSERT_FALSE (bitmap_bit_p (b, 150));
2695 ASSERT_TRUE (bitmap_bit_p (b, 151));
2698 /* Verify bitmap_copy. */
2700 static void
2701 test_copying ()
2703 bitmap src = bitmap_gc_alloc ();
2704 bitmap_set_range (src, 40, 10);
2706 bitmap dst = bitmap_gc_alloc ();
2707 ASSERT_FALSE (bitmap_equal_p (src, dst));
2708 bitmap_copy (dst, src);
2709 ASSERT_TRUE (bitmap_equal_p (src, dst));
2711 /* Verify that we can make them unequal again... */
2712 bitmap_set_range (src, 70, 5);
2713 ASSERT_FALSE (bitmap_equal_p (src, dst));
2715 /* ...and that changing src after the copy didn't affect
2716 the other: */
2717 ASSERT_FALSE (bitmap_bit_p (dst, 70));
2720 /* Verify bitmap_single_bit_set_p. */
2722 static void
2723 test_bitmap_single_bit_set_p ()
2725 bitmap b = bitmap_gc_alloc ();
2727 ASSERT_FALSE (bitmap_single_bit_set_p (b));
2729 bitmap_set_range (b, 42, 1);
2730 ASSERT_TRUE (bitmap_single_bit_set_p (b));
2731 ASSERT_EQ (42, bitmap_first_set_bit (b));
2733 bitmap_set_range (b, 1066, 1);
2734 ASSERT_FALSE (bitmap_single_bit_set_p (b));
2735 ASSERT_EQ (42, bitmap_first_set_bit (b));
2737 bitmap_clear_range (b, 0, 100);
2738 ASSERT_TRUE (bitmap_single_bit_set_p (b));
2739 ASSERT_EQ (1066, bitmap_first_set_bit (b));
2742 /* Run all of the selftests within this file. */
2744 void
2745 bitmap_c_tests ()
2747 test_gc_alloc ();
2748 test_set_range ();
2749 test_clear_bit_in_middle ();
2750 test_copying ();
2751 test_bitmap_single_bit_set_p ();
2754 } // namespace selftest
2755 #endif /* CHECKING_P */
2757 #include "gt-bitmap.h"