testsuite: Correct requirements for vadsdu*, vslv and vsrv testcases.
[official-gcc.git] / gcc / bitmap.c
blob810b80be1ba78b2d66789023fc1f87326cb8cccf
1 /* Functions to support general ended bitmaps.
2 Copyright (C) 1997-2020 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,
271 bool to_freelist = true)
273 bitmap_element *next = element->next;
274 bitmap_element *prev = element->prev;
276 gcc_checking_assert (!head->tree_form);
278 if (prev)
279 prev->next = next;
281 if (next)
282 next->prev = prev;
284 if (head->first == element)
285 head->first = next;
287 /* Since the first thing we try is to insert before current,
288 make current the next entry in preference to the previous. */
289 if (head->current == element)
291 head->current = next != 0 ? next : prev;
292 if (head->current)
293 head->indx = head->current->indx;
294 else
295 head->indx = 0;
298 if (to_freelist)
299 bitmap_elem_to_freelist (head, element);
302 /* Insert a new uninitialized element (or NODE if not NULL) into bitmap
303 HEAD after element ELT. If ELT is NULL, insert the element at the start.
304 Return the new element. */
306 static bitmap_element *
307 bitmap_list_insert_element_after (bitmap head,
308 bitmap_element *elt, unsigned int indx,
309 bitmap_element *node = NULL)
311 if (!node)
312 node = bitmap_element_allocate (head);
313 node->indx = indx;
315 gcc_checking_assert (!head->tree_form);
317 if (!elt)
319 if (!head->current)
321 head->current = node;
322 head->indx = indx;
324 node->next = head->first;
325 if (node->next)
326 node->next->prev = node;
327 head->first = node;
328 node->prev = NULL;
330 else
332 gcc_checking_assert (head->current);
333 node->next = elt->next;
334 if (node->next)
335 node->next->prev = node;
336 elt->next = node;
337 node->prev = elt;
339 return node;
342 /* Return the element for INDX, or NULL if the element doesn't exist.
343 Update the `current' field even if we can't find an element that
344 would hold the bitmap's bit to make eventual allocation
345 faster. */
347 static inline bitmap_element *
348 bitmap_list_find_element (bitmap head, unsigned int indx)
350 bitmap_element *element;
352 if (head->current == NULL
353 || head->indx == indx)
354 return head->current;
356 if (head->current == head->first
357 && head->first->next == NULL)
358 return NULL;
360 /* Usage can be NULL due to allocated bitmaps for which we do not
361 call initialize function. */
362 bitmap_usage *usage = NULL;
363 if (GATHER_STATISTICS)
364 usage = bitmap_mem_desc.get_descriptor_for_instance (head);
366 /* This bitmap has more than one element, and we're going to look
367 through the elements list. Count that as a search. */
368 if (GATHER_STATISTICS && usage)
369 usage->m_nsearches++;
371 if (head->indx < indx)
372 /* INDX is beyond head->indx. Search from head->current
373 forward. */
374 for (element = head->current;
375 element->next != 0 && element->indx < indx;
376 element = element->next)
378 if (GATHER_STATISTICS && usage)
379 usage->m_search_iter++;
382 else if (head->indx / 2 < indx)
383 /* INDX is less than head->indx and closer to head->indx than to
384 0. Search from head->current backward. */
385 for (element = head->current;
386 element->prev != 0 && element->indx > indx;
387 element = element->prev)
389 if (GATHER_STATISTICS && usage)
390 usage->m_search_iter++;
393 else
394 /* INDX is less than head->indx and closer to 0 than to
395 head->indx. Search from head->first forward. */
396 for (element = head->first;
397 element->next != 0 && element->indx < indx;
398 element = element->next)
400 if (GATHER_STATISTICS && usage)
401 usage->m_search_iter++;
404 /* `element' is the nearest to the one we want. If it's not the one we
405 want, the one we want doesn't exist. */
406 gcc_checking_assert (element != NULL);
407 head->current = element;
408 head->indx = element->indx;
409 if (element->indx != indx)
410 element = 0;
411 return element;
415 /* Splay-tree view of bitmaps.
417 This is an almost one-to-one the implementatin of the simple top-down
418 splay tree in Sleator and Tarjan's "Self-adjusting Binary Search Trees".
419 It is probably not the most efficient form of splay trees, but it should
420 be good enough to experiment with this idea of bitmaps-as-trees.
422 For all functions below, the variable or function argument "t" is a node
423 in the tree, and "e" is a temporary or new node in the tree. The rest
424 is sufficiently straigh-forward (and very well explained in the paper)
425 that comment would only clutter things. */
427 static inline void
428 bitmap_tree_link_left (bitmap_element * &t, bitmap_element * &l)
430 l->next = t;
431 l = t;
432 t = t->next;
435 static inline void
436 bitmap_tree_link_right (bitmap_element * &t, bitmap_element * &r)
438 r->prev = t;
439 r = t;
440 t = t->prev;
443 static inline void
444 bitmap_tree_rotate_left (bitmap_element * &t)
446 bitmap_element *e = t->next;
447 t->next = t->next->prev;
448 e->prev = t;
449 t = e;
452 static inline void
453 bitmap_tree_rotate_right (bitmap_element * &t)
455 bitmap_element *e = t->prev;
456 t->prev = t->prev->next;
457 e->next = t;
458 t = e;
461 static bitmap_element *
462 bitmap_tree_splay (bitmap head, bitmap_element *t, unsigned int indx)
464 bitmap_element N, *l, *r;
466 if (t == NULL)
467 return NULL;
469 bitmap_usage *usage = NULL;
470 if (GATHER_STATISTICS)
471 usage = bitmap_mem_desc.get_descriptor_for_instance (head);
473 N.prev = N.next = NULL;
474 l = r = &N;
476 while (indx != t->indx)
478 if (GATHER_STATISTICS && usage)
479 usage->m_search_iter++;
481 if (indx < t->indx)
483 if (t->prev != NULL && indx < t->prev->indx)
484 bitmap_tree_rotate_right (t);
485 if (t->prev == NULL)
486 break;
487 bitmap_tree_link_right (t, r);
489 else if (indx > t->indx)
491 if (t->next != NULL && indx > t->next->indx)
492 bitmap_tree_rotate_left (t);
493 if (t->next == NULL)
494 break;
495 bitmap_tree_link_left (t, l);
499 l->next = t->prev;
500 r->prev = t->next;
501 t->prev = N.next;
502 t->next = N.prev;
503 return t;
506 /* Link bitmap element E into the current bitmap splay tree. */
508 static inline void
509 bitmap_tree_link_element (bitmap head, bitmap_element *e)
511 if (head->first == NULL)
512 e->prev = e->next = NULL;
513 else
515 bitmap_element *t = bitmap_tree_splay (head, head->first, e->indx);
516 if (e->indx < t->indx)
518 e->prev = t->prev;
519 e->next = t;
520 t->prev = NULL;
522 else if (e->indx > t->indx)
524 e->next = t->next;
525 e->prev = t;
526 t->next = NULL;
528 else
529 gcc_unreachable ();
531 head->first = e;
532 head->current = e;
533 head->indx = e->indx;
536 /* Unlink bitmap element E from the current bitmap splay tree,
537 and return it to the freelist. */
539 static void
540 bitmap_tree_unlink_element (bitmap head, bitmap_element *e)
542 bitmap_element *t = bitmap_tree_splay (head, head->first, e->indx);
544 gcc_checking_assert (t == e);
546 if (e->prev == NULL)
547 t = e->next;
548 else
550 t = bitmap_tree_splay (head, e->prev, e->indx);
551 t->next = e->next;
553 head->first = t;
554 head->current = t;
555 head->indx = (t != NULL) ? t->indx : 0;
557 bitmap_elem_to_freelist (head, e);
560 /* Return the element for INDX, or NULL if the element doesn't exist. */
562 static inline bitmap_element *
563 bitmap_tree_find_element (bitmap head, unsigned int indx)
565 if (head->current == NULL
566 || head->indx == indx)
567 return head->current;
569 /* Usage can be NULL due to allocated bitmaps for which we do not
570 call initialize function. */
571 bitmap_usage *usage = NULL;
572 if (GATHER_STATISTICS)
573 usage = bitmap_mem_desc.get_descriptor_for_instance (head);
575 /* This bitmap has more than one element, and we're going to look
576 through the elements list. Count that as a search. */
577 if (GATHER_STATISTICS && usage)
578 usage->m_nsearches++;
580 bitmap_element *element = bitmap_tree_splay (head, head->first, indx);
581 gcc_checking_assert (element != NULL);
582 head->first = element;
583 head->current = element;
584 head->indx = element->indx;
585 if (element->indx != indx)
586 element = 0;
587 return element;
590 /* Converting bitmap views from linked-list to tree and vice versa. */
592 /* Splice element E and all elements with a larger index from
593 bitmap HEAD, convert the spliced elements to the linked-list
594 view, and return the head of the list (which should be E again), */
596 static bitmap_element *
597 bitmap_tree_listify_from (bitmap head, bitmap_element *e)
599 bitmap_element *t, *erb;
601 /* Detach the right branch from E (all elements with indx > E->indx),
602 and splay E to the root. */
603 erb = e->next;
604 e->next = NULL;
605 t = bitmap_tree_splay (head, head->first, e->indx);
606 gcc_checking_assert (t == e);
608 /* Because E has no right branch, and we rotated it to the root,
609 the left branch is the new root. */
610 t = e->prev;
611 head->first = t;
612 head->current = t;
613 head->indx = (t != NULL) ? t->indx : 0;
615 /* Detach the tree from E, and re-attach the right branch of E. */
616 e->prev = NULL;
617 e->next = erb;
619 /* The tree is now valid again. Now we need to "un-tree" E.
620 It is imperative that a non-recursive implementation is used
621 for this, because splay trees have a worst case depth of O(N)
622 for a tree with N nodes. A recursive implementation could
623 result in a stack overflow for a sufficiently large, unbalanced
624 bitmap tree. */
626 auto_vec<bitmap_element *, 32> stack;
627 auto_vec<bitmap_element *, 32> sorted_elements;
628 bitmap_element *n = e;
630 while (true)
632 while (n != NULL)
634 stack.safe_push (n);
635 n = n->prev;
638 if (stack.is_empty ())
639 break;
641 n = stack.pop ();
642 sorted_elements.safe_push (n);
643 n = n->next;
646 gcc_assert (sorted_elements[0] == e);
648 bitmap_element *prev = NULL;
649 unsigned ix;
650 FOR_EACH_VEC_ELT (sorted_elements, ix, n)
652 if (prev != NULL)
653 prev->next = n;
654 n->prev = prev;
655 n->next = NULL;
656 prev = n;
659 return e;
662 /* Convert bitmap HEAD from splay-tree view to linked-list view. */
664 void
665 bitmap_list_view (bitmap head)
667 bitmap_element *ptr;
669 gcc_assert (head->tree_form);
671 ptr = head->first;
672 if (ptr)
674 while (ptr->prev)
675 bitmap_tree_rotate_right (ptr);
676 head->first = ptr;
677 head->first = bitmap_tree_listify_from (head, ptr);
680 head->tree_form = false;
683 /* Convert bitmap HEAD from linked-list view to splay-tree view.
684 This is simply a matter of dropping the prev or next pointers
685 and setting the tree_form flag. The tree will balance itself
686 if and when it is used. */
688 void
689 bitmap_tree_view (bitmap head)
691 bitmap_element *ptr;
693 gcc_assert (! head->tree_form);
695 ptr = head->first;
696 while (ptr)
698 ptr->prev = NULL;
699 ptr = ptr->next;
702 head->tree_form = true;
705 /* Clear a bitmap by freeing all its elements. */
707 void
708 bitmap_clear (bitmap head)
710 if (head->first == NULL)
711 return;
712 if (head->tree_form)
714 bitmap_element *e, *t;
715 for (e = head->first; e->prev; e = e->prev)
716 /* Loop to find the element with the smallest index. */ ;
717 t = bitmap_tree_splay (head, head->first, e->indx);
718 gcc_checking_assert (t == e);
719 head->first = t;
721 bitmap_elt_clear_from (head, head->first);
724 /* Initialize a bitmap obstack. If BIT_OBSTACK is NULL, initialize
725 the default bitmap obstack. */
727 void
728 bitmap_obstack_initialize (bitmap_obstack *bit_obstack)
730 if (!bit_obstack)
732 if (bitmap_default_obstack_depth++)
733 return;
734 bit_obstack = &bitmap_default_obstack;
737 #if !defined(__GNUC__) || (__GNUC__ < 2)
738 #define __alignof__(type) 0
739 #endif
741 bit_obstack->elements = NULL;
742 bit_obstack->heads = NULL;
743 obstack_specify_allocation (&bit_obstack->obstack, OBSTACK_CHUNK_SIZE,
744 __alignof__ (bitmap_element),
745 obstack_chunk_alloc,
746 obstack_chunk_free);
749 /* Release the memory from a bitmap obstack. If BIT_OBSTACK is NULL,
750 release the default bitmap obstack. */
752 void
753 bitmap_obstack_release (bitmap_obstack *bit_obstack)
755 if (!bit_obstack)
757 if (--bitmap_default_obstack_depth)
759 gcc_assert (bitmap_default_obstack_depth > 0);
760 return;
762 bit_obstack = &bitmap_default_obstack;
765 bit_obstack->elements = NULL;
766 bit_obstack->heads = NULL;
767 obstack_free (&bit_obstack->obstack, NULL);
770 /* Create a new bitmap on an obstack. If BIT_OBSTACK is NULL, create
771 it on the default bitmap obstack. */
773 bitmap
774 bitmap_alloc (bitmap_obstack *bit_obstack MEM_STAT_DECL)
776 bitmap map;
778 if (!bit_obstack)
779 bit_obstack = &bitmap_default_obstack;
780 map = bit_obstack->heads;
781 if (map)
782 bit_obstack->heads = (class bitmap_head *) map->first;
783 else
784 map = XOBNEW (&bit_obstack->obstack, bitmap_head);
785 bitmap_initialize (map, bit_obstack PASS_MEM_STAT);
787 if (GATHER_STATISTICS)
788 register_overhead (map, sizeof (bitmap_head));
790 return map;
793 /* Create a new GCd bitmap. */
795 bitmap
796 bitmap_gc_alloc (ALONE_MEM_STAT_DECL)
798 bitmap map;
800 map = ggc_alloc<bitmap_head> ();
801 bitmap_initialize (map, NULL PASS_MEM_STAT);
803 if (GATHER_STATISTICS)
804 register_overhead (map, sizeof (bitmap_head));
806 return map;
809 /* Release an obstack allocated bitmap. */
811 void
812 bitmap_obstack_free (bitmap map)
814 if (map)
816 bitmap_clear (map);
817 map->first = (bitmap_element *) map->obstack->heads;
819 if (GATHER_STATISTICS)
820 release_overhead (map, sizeof (bitmap_head), true);
822 map->obstack->heads = map;
827 /* Return nonzero if all bits in an element are zero. */
829 static inline int
830 bitmap_element_zerop (const bitmap_element *element)
832 #if BITMAP_ELEMENT_WORDS == 2
833 return (element->bits[0] | element->bits[1]) == 0;
834 #else
835 unsigned i;
837 for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
838 if (element->bits[i] != 0)
839 return 0;
841 return 1;
842 #endif
845 /* Copy a bitmap to another bitmap. */
847 void
848 bitmap_copy (bitmap to, const_bitmap from)
850 const bitmap_element *from_ptr;
851 bitmap_element *to_ptr = 0;
853 gcc_checking_assert (!to->tree_form && !from->tree_form);
855 bitmap_clear (to);
857 /* Copy elements in forward direction one at a time. */
858 for (from_ptr = from->first; from_ptr; from_ptr = from_ptr->next)
860 bitmap_element *to_elt = bitmap_element_allocate (to);
862 to_elt->indx = from_ptr->indx;
863 memcpy (to_elt->bits, from_ptr->bits, sizeof (to_elt->bits));
865 /* Here we have a special case of bitmap_list_link_element,
866 for the case where we know the links are being entered
867 in sequence. */
868 if (to_ptr == 0)
870 to->first = to->current = to_elt;
871 to->indx = from_ptr->indx;
872 to_elt->next = to_elt->prev = 0;
874 else
876 to_elt->prev = to_ptr;
877 to_elt->next = 0;
878 to_ptr->next = to_elt;
881 to_ptr = to_elt;
885 /* Move a bitmap to another bitmap. */
887 void
888 bitmap_move (bitmap to, bitmap from)
890 gcc_assert (to->obstack == from->obstack);
892 bitmap_clear (to);
894 size_t sz = 0;
895 if (GATHER_STATISTICS)
897 for (bitmap_element *e = to->first; e; e = e->next)
898 sz += sizeof (bitmap_element);
899 register_overhead (to, sz);
902 *to = *from;
904 if (GATHER_STATISTICS)
905 release_overhead (from, sz, false);
908 /* Clear a single bit in a bitmap. Return true if the bit changed. */
910 bool
911 bitmap_clear_bit (bitmap head, int bit)
913 unsigned int indx = bit / BITMAP_ELEMENT_ALL_BITS;
914 bitmap_element *ptr;
916 if (!head->tree_form)
917 ptr = bitmap_list_find_element (head, indx);
918 else
919 ptr = bitmap_tree_find_element (head, indx);
920 if (ptr != 0)
922 unsigned bit_num = bit % BITMAP_WORD_BITS;
923 unsigned word_num = bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
924 BITMAP_WORD bit_val = ((BITMAP_WORD) 1) << bit_num;
925 bool res = (ptr->bits[word_num] & bit_val) != 0;
926 if (res)
928 ptr->bits[word_num] &= ~bit_val;
929 /* If we cleared the entire word, free up the element. */
930 if (!ptr->bits[word_num]
931 && bitmap_element_zerop (ptr))
933 if (!head->tree_form)
934 bitmap_list_unlink_element (head, ptr);
935 else
936 bitmap_tree_unlink_element (head, ptr);
940 return res;
943 return false;
946 /* Set a single bit in a bitmap. Return true if the bit changed. */
948 bool
949 bitmap_set_bit (bitmap head, int bit)
951 unsigned indx = bit / BITMAP_ELEMENT_ALL_BITS;
952 bitmap_element *ptr;
953 if (!head->tree_form)
954 ptr = bitmap_list_find_element (head, indx);
955 else
956 ptr = bitmap_tree_find_element (head, indx);
957 unsigned word_num = bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
958 unsigned bit_num = bit % BITMAP_WORD_BITS;
959 BITMAP_WORD bit_val = ((BITMAP_WORD) 1) << bit_num;
961 if (ptr != 0)
963 bool res = (ptr->bits[word_num] & bit_val) == 0;
964 if (res)
965 ptr->bits[word_num] |= bit_val;
966 return res;
969 ptr = bitmap_element_allocate (head);
970 ptr->indx = bit / BITMAP_ELEMENT_ALL_BITS;
971 ptr->bits[word_num] = bit_val;
972 if (!head->tree_form)
973 bitmap_list_link_element (head, ptr);
974 else
975 bitmap_tree_link_element (head, ptr);
976 return true;
979 /* Return whether a bit is set within a bitmap. */
982 bitmap_bit_p (const_bitmap head, int bit)
984 unsigned int indx = bit / BITMAP_ELEMENT_ALL_BITS;
985 const bitmap_element *ptr;
986 unsigned bit_num;
987 unsigned word_num;
989 if (!head->tree_form)
990 ptr = bitmap_list_find_element (const_cast<bitmap> (head), indx);
991 else
992 ptr = bitmap_tree_find_element (const_cast<bitmap> (head), indx);
993 if (ptr == 0)
994 return 0;
996 bit_num = bit % BITMAP_WORD_BITS;
997 word_num = bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
999 return (ptr->bits[word_num] >> bit_num) & 1;
1002 #if GCC_VERSION < 3400
1003 /* Table of number of set bits in a character, indexed by value of char. */
1004 static const unsigned char popcount_table[] =
1006 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,
1007 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,
1008 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,
1009 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,
1010 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,
1011 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,
1012 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,
1013 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,
1016 static unsigned long
1017 bitmap_popcount (BITMAP_WORD a)
1019 unsigned long ret = 0;
1020 unsigned i;
1022 /* Just do this the table way for now */
1023 for (i = 0; i < BITMAP_WORD_BITS; i+= 8)
1024 ret += popcount_table[(a >> i) & 0xff];
1025 return ret;
1027 #endif
1029 /* Count and return the number of bits set in the bitmap word BITS. */
1030 static unsigned long
1031 bitmap_count_bits_in_word (const BITMAP_WORD *bits)
1033 unsigned long count = 0;
1035 for (unsigned ix = 0; ix != BITMAP_ELEMENT_WORDS; ix++)
1037 #if GCC_VERSION >= 3400
1038 /* Note that popcountl matches BITMAP_WORD in type, so the actual size
1039 of BITMAP_WORD is not material. */
1040 count += __builtin_popcountl (bits[ix]);
1041 #else
1042 count += bitmap_popcount (bits[ix]);
1043 #endif
1045 return count;
1048 /* Count the number of bits set in the bitmap, and return it. */
1050 unsigned long
1051 bitmap_count_bits (const_bitmap a)
1053 unsigned long count = 0;
1054 const bitmap_element *elt;
1056 gcc_checking_assert (!a->tree_form);
1057 for (elt = a->first; elt; elt = elt->next)
1058 count += bitmap_count_bits_in_word (elt->bits);
1060 return count;
1063 /* Count the number of unique bits set in A and B and return it. */
1065 unsigned long
1066 bitmap_count_unique_bits (const_bitmap a, const_bitmap b)
1068 unsigned long count = 0;
1069 const bitmap_element *elt_a, *elt_b;
1071 for (elt_a = a->first, elt_b = b->first; elt_a && elt_b; )
1073 /* If we're at different indices, then count all the bits
1074 in the lower element. If we're at the same index, then
1075 count the bits in the IOR of the two elements. */
1076 if (elt_a->indx < elt_b->indx)
1078 count += bitmap_count_bits_in_word (elt_a->bits);
1079 elt_a = elt_a->next;
1081 else if (elt_b->indx < elt_a->indx)
1083 count += bitmap_count_bits_in_word (elt_b->bits);
1084 elt_b = elt_b->next;
1086 else
1088 BITMAP_WORD bits[BITMAP_ELEMENT_WORDS];
1089 for (unsigned ix = 0; ix != BITMAP_ELEMENT_WORDS; ix++)
1090 bits[ix] = elt_a->bits[ix] | elt_b->bits[ix];
1091 count += bitmap_count_bits_in_word (bits);
1092 elt_a = elt_a->next;
1093 elt_b = elt_b->next;
1096 return count;
1099 /* Return true if the bitmap has a single bit set. Otherwise return
1100 false. */
1102 bool
1103 bitmap_single_bit_set_p (const_bitmap a)
1105 unsigned long count = 0;
1106 const bitmap_element *elt;
1107 unsigned ix;
1109 if (bitmap_empty_p (a))
1110 return false;
1112 elt = a->first;
1114 /* As there are no completely empty bitmap elements, a second one
1115 means we have more than one bit set. */
1116 if (elt->next != NULL
1117 && (!a->tree_form || elt->prev != NULL))
1118 return false;
1120 for (ix = 0; ix != BITMAP_ELEMENT_WORDS; ix++)
1122 #if GCC_VERSION >= 3400
1123 /* Note that popcountl matches BITMAP_WORD in type, so the actual size
1124 of BITMAP_WORD is not material. */
1125 count += __builtin_popcountl (elt->bits[ix]);
1126 #else
1127 count += bitmap_popcount (elt->bits[ix]);
1128 #endif
1129 if (count > 1)
1130 return false;
1133 return count == 1;
1137 /* Return the bit number of the first set bit in the bitmap. The
1138 bitmap must be non-empty. */
1140 unsigned
1141 bitmap_first_set_bit (const_bitmap a)
1143 const bitmap_element *elt = a->first;
1144 unsigned bit_no;
1145 BITMAP_WORD word;
1146 unsigned ix;
1148 gcc_checking_assert (elt);
1150 if (a->tree_form)
1151 while (elt->prev)
1152 elt = elt->prev;
1154 bit_no = elt->indx * BITMAP_ELEMENT_ALL_BITS;
1155 for (ix = 0; ix != BITMAP_ELEMENT_WORDS; ix++)
1157 word = elt->bits[ix];
1158 if (word)
1159 goto found_bit;
1161 gcc_unreachable ();
1162 found_bit:
1163 bit_no += ix * BITMAP_WORD_BITS;
1165 #if GCC_VERSION >= 3004
1166 gcc_assert (sizeof (long) == sizeof (word));
1167 bit_no += __builtin_ctzl (word);
1168 #else
1169 /* Binary search for the first set bit. */
1170 #if BITMAP_WORD_BITS > 64
1171 #error "Fill out the table."
1172 #endif
1173 #if BITMAP_WORD_BITS > 32
1174 if (!(word & 0xffffffff))
1175 word >>= 32, bit_no += 32;
1176 #endif
1177 if (!(word & 0xffff))
1178 word >>= 16, bit_no += 16;
1179 if (!(word & 0xff))
1180 word >>= 8, bit_no += 8;
1181 if (!(word & 0xf))
1182 word >>= 4, bit_no += 4;
1183 if (!(word & 0x3))
1184 word >>= 2, bit_no += 2;
1185 if (!(word & 0x1))
1186 word >>= 1, bit_no += 1;
1188 gcc_checking_assert (word & 1);
1189 #endif
1190 return bit_no;
1193 /* Return the bit number of the first set bit in the bitmap. The
1194 bitmap must be non-empty. */
1196 unsigned
1197 bitmap_last_set_bit (const_bitmap a)
1199 const bitmap_element *elt;
1200 unsigned bit_no;
1201 BITMAP_WORD word;
1202 int ix;
1204 if (a->tree_form)
1205 elt = a->first;
1206 else
1207 elt = a->current ? a->current : a->first;
1208 gcc_checking_assert (elt);
1210 while (elt->next)
1211 elt = elt->next;
1213 bit_no = elt->indx * BITMAP_ELEMENT_ALL_BITS;
1214 for (ix = BITMAP_ELEMENT_WORDS - 1; ix >= 1; ix--)
1216 word = elt->bits[ix];
1217 if (word)
1218 goto found_bit;
1220 gcc_assert (elt->bits[ix] != 0);
1221 found_bit:
1222 bit_no += ix * BITMAP_WORD_BITS;
1223 #if GCC_VERSION >= 3004
1224 gcc_assert (sizeof (long) == sizeof (word));
1225 bit_no += BITMAP_WORD_BITS - __builtin_clzl (word) - 1;
1226 #else
1227 /* Hopefully this is a twos-complement host... */
1228 BITMAP_WORD x = word;
1229 x |= (x >> 1);
1230 x |= (x >> 2);
1231 x |= (x >> 4);
1232 x |= (x >> 8);
1233 x |= (x >> 16);
1234 #if BITMAP_WORD_BITS > 32
1235 x |= (x >> 32);
1236 #endif
1237 bit_no += bitmap_popcount (x) - 1;
1238 #endif
1240 return bit_no;
1244 /* DST = A & B. */
1246 void
1247 bitmap_and (bitmap dst, const_bitmap a, const_bitmap b)
1249 bitmap_element *dst_elt = dst->first;
1250 const bitmap_element *a_elt = a->first;
1251 const bitmap_element *b_elt = b->first;
1252 bitmap_element *dst_prev = NULL;
1254 gcc_checking_assert (!dst->tree_form && !a->tree_form && !b->tree_form);
1255 gcc_assert (dst != a && dst != b);
1257 if (a == b)
1259 bitmap_copy (dst, a);
1260 return;
1263 while (a_elt && b_elt)
1265 if (a_elt->indx < b_elt->indx)
1266 a_elt = a_elt->next;
1267 else if (b_elt->indx < a_elt->indx)
1268 b_elt = b_elt->next;
1269 else
1271 /* Matching elts, generate A & B. */
1272 unsigned ix;
1273 BITMAP_WORD ior = 0;
1275 if (!dst_elt)
1276 dst_elt = bitmap_list_insert_element_after (dst, dst_prev,
1277 a_elt->indx);
1278 else
1279 dst_elt->indx = a_elt->indx;
1280 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1282 BITMAP_WORD r = a_elt->bits[ix] & b_elt->bits[ix];
1284 dst_elt->bits[ix] = r;
1285 ior |= r;
1287 if (ior)
1289 dst_prev = dst_elt;
1290 dst_elt = dst_elt->next;
1292 a_elt = a_elt->next;
1293 b_elt = b_elt->next;
1296 /* Ensure that dst->current is valid. */
1297 dst->current = dst->first;
1298 bitmap_elt_clear_from (dst, dst_elt);
1299 gcc_checking_assert (!dst->current == !dst->first);
1300 if (dst->current)
1301 dst->indx = dst->current->indx;
1304 /* A &= B. Return true if A changed. */
1306 bool
1307 bitmap_and_into (bitmap a, const_bitmap b)
1309 bitmap_element *a_elt = a->first;
1310 const bitmap_element *b_elt = b->first;
1311 bitmap_element *next;
1312 bool changed = false;
1314 gcc_checking_assert (!a->tree_form && !b->tree_form);
1316 if (a == b)
1317 return false;
1319 while (a_elt && b_elt)
1321 if (a_elt->indx < b_elt->indx)
1323 next = a_elt->next;
1324 bitmap_list_unlink_element (a, a_elt);
1325 a_elt = next;
1326 changed = true;
1328 else if (b_elt->indx < a_elt->indx)
1329 b_elt = b_elt->next;
1330 else
1332 /* Matching elts, generate A &= B. */
1333 unsigned ix;
1334 BITMAP_WORD ior = 0;
1336 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1338 BITMAP_WORD r = a_elt->bits[ix] & b_elt->bits[ix];
1339 if (a_elt->bits[ix] != r)
1340 changed = true;
1341 a_elt->bits[ix] = r;
1342 ior |= r;
1344 next = a_elt->next;
1345 if (!ior)
1346 bitmap_list_unlink_element (a, a_elt);
1347 a_elt = next;
1348 b_elt = b_elt->next;
1352 if (a_elt)
1354 changed = true;
1355 bitmap_elt_clear_from (a, a_elt);
1358 gcc_checking_assert (!a->current == !a->first
1359 && (!a->current || a->indx == a->current->indx));
1361 return changed;
1365 /* Insert an element equal to SRC_ELT after DST_PREV, overwriting DST_ELT
1366 if non-NULL. CHANGED is true if the destination bitmap had already been
1367 changed; the new value of CHANGED is returned. */
1369 static inline bool
1370 bitmap_elt_copy (bitmap dst, bitmap_element *dst_elt, bitmap_element *dst_prev,
1371 const bitmap_element *src_elt, bool changed)
1373 if (!changed && dst_elt && dst_elt->indx == src_elt->indx)
1375 unsigned ix;
1377 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1378 if (src_elt->bits[ix] != dst_elt->bits[ix])
1380 dst_elt->bits[ix] = src_elt->bits[ix];
1381 changed = true;
1384 else
1386 changed = true;
1387 if (!dst_elt)
1388 dst_elt = bitmap_list_insert_element_after (dst, dst_prev,
1389 src_elt->indx);
1390 else
1391 dst_elt->indx = src_elt->indx;
1392 memcpy (dst_elt->bits, src_elt->bits, sizeof (dst_elt->bits));
1394 return changed;
1399 /* DST = A & ~B */
1401 bool
1402 bitmap_and_compl (bitmap dst, const_bitmap a, const_bitmap b)
1404 bitmap_element *dst_elt = dst->first;
1405 const bitmap_element *a_elt = a->first;
1406 const bitmap_element *b_elt = b->first;
1407 bitmap_element *dst_prev = NULL;
1408 bitmap_element **dst_prev_pnext = &dst->first;
1409 bool changed = false;
1411 gcc_checking_assert (!dst->tree_form && !a->tree_form && !b->tree_form);
1412 gcc_assert (dst != a && dst != b);
1414 if (a == b)
1416 changed = !bitmap_empty_p (dst);
1417 bitmap_clear (dst);
1418 return changed;
1421 while (a_elt)
1423 while (b_elt && b_elt->indx < a_elt->indx)
1424 b_elt = b_elt->next;
1426 if (!b_elt || b_elt->indx > a_elt->indx)
1428 changed = bitmap_elt_copy (dst, dst_elt, dst_prev, a_elt, changed);
1429 dst_prev = *dst_prev_pnext;
1430 dst_prev_pnext = &dst_prev->next;
1431 dst_elt = *dst_prev_pnext;
1432 a_elt = a_elt->next;
1435 else
1437 /* Matching elts, generate A & ~B. */
1438 unsigned ix;
1439 BITMAP_WORD ior = 0;
1441 if (!changed && dst_elt && dst_elt->indx == a_elt->indx)
1443 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1445 BITMAP_WORD r = a_elt->bits[ix] & ~b_elt->bits[ix];
1447 if (dst_elt->bits[ix] != r)
1449 changed = true;
1450 dst_elt->bits[ix] = r;
1452 ior |= r;
1455 else
1457 bool new_element;
1458 if (!dst_elt || dst_elt->indx > a_elt->indx)
1460 dst_elt = bitmap_list_insert_element_after (dst, dst_prev,
1461 a_elt->indx);
1462 new_element = true;
1464 else
1466 dst_elt->indx = a_elt->indx;
1467 new_element = false;
1470 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1472 BITMAP_WORD r = a_elt->bits[ix] & ~b_elt->bits[ix];
1474 dst_elt->bits[ix] = r;
1475 ior |= r;
1478 if (ior)
1479 changed = true;
1480 else
1482 changed |= !new_element;
1483 bitmap_list_unlink_element (dst, dst_elt);
1484 dst_elt = *dst_prev_pnext;
1488 if (ior)
1490 dst_prev = *dst_prev_pnext;
1491 dst_prev_pnext = &dst_prev->next;
1492 dst_elt = *dst_prev_pnext;
1494 a_elt = a_elt->next;
1495 b_elt = b_elt->next;
1499 /* Ensure that dst->current is valid. */
1500 dst->current = dst->first;
1502 if (dst_elt)
1504 changed = true;
1505 bitmap_elt_clear_from (dst, dst_elt);
1507 gcc_checking_assert (!dst->current == !dst->first);
1508 if (dst->current)
1509 dst->indx = dst->current->indx;
1511 return changed;
1514 /* A &= ~B. Returns true if A changes */
1516 bool
1517 bitmap_and_compl_into (bitmap a, const_bitmap b)
1519 bitmap_element *a_elt = a->first;
1520 const bitmap_element *b_elt = b->first;
1521 bitmap_element *next;
1522 BITMAP_WORD changed = 0;
1524 gcc_checking_assert (!a->tree_form && !b->tree_form);
1526 if (a == b)
1528 if (bitmap_empty_p (a))
1529 return false;
1530 else
1532 bitmap_clear (a);
1533 return true;
1537 while (a_elt && b_elt)
1539 if (a_elt->indx < b_elt->indx)
1540 a_elt = a_elt->next;
1541 else if (b_elt->indx < a_elt->indx)
1542 b_elt = b_elt->next;
1543 else
1545 /* Matching elts, generate A &= ~B. */
1546 unsigned ix;
1547 BITMAP_WORD ior = 0;
1549 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1551 BITMAP_WORD cleared = a_elt->bits[ix] & b_elt->bits[ix];
1552 BITMAP_WORD r = a_elt->bits[ix] ^ cleared;
1554 a_elt->bits[ix] = r;
1555 changed |= cleared;
1556 ior |= r;
1558 next = a_elt->next;
1559 if (!ior)
1560 bitmap_list_unlink_element (a, a_elt);
1561 a_elt = next;
1562 b_elt = b_elt->next;
1565 gcc_checking_assert (!a->current == !a->first
1566 && (!a->current || a->indx == a->current->indx));
1567 return changed != 0;
1570 /* Set COUNT bits from START in HEAD. */
1571 void
1572 bitmap_set_range (bitmap head, unsigned int start, unsigned int count)
1574 unsigned int first_index, end_bit_plus1, last_index;
1575 bitmap_element *elt, *elt_prev;
1576 unsigned int i;
1578 gcc_checking_assert (!head->tree_form);
1580 if (!count)
1581 return;
1583 if (count == 1)
1585 bitmap_set_bit (head, start);
1586 return;
1589 first_index = start / BITMAP_ELEMENT_ALL_BITS;
1590 end_bit_plus1 = start + count;
1591 last_index = (end_bit_plus1 - 1) / BITMAP_ELEMENT_ALL_BITS;
1592 elt = bitmap_list_find_element (head, first_index);
1594 /* If bitmap_list_find_element returns zero, the current is the closest block
1595 to the result. Otherwise, just use bitmap_element_allocate to
1596 ensure ELT is set; in the loop below, ELT == NULL means "insert
1597 at the end of the bitmap". */
1598 if (!elt)
1600 elt = bitmap_element_allocate (head);
1601 elt->indx = first_index;
1602 bitmap_list_link_element (head, elt);
1605 gcc_checking_assert (elt->indx == first_index);
1606 elt_prev = elt->prev;
1607 for (i = first_index; i <= last_index; i++)
1609 unsigned elt_start_bit = i * BITMAP_ELEMENT_ALL_BITS;
1610 unsigned elt_end_bit_plus1 = elt_start_bit + BITMAP_ELEMENT_ALL_BITS;
1612 unsigned int first_word_to_mod;
1613 BITMAP_WORD first_mask;
1614 unsigned int last_word_to_mod;
1615 BITMAP_WORD last_mask;
1616 unsigned int ix;
1618 if (!elt || elt->indx != i)
1619 elt = bitmap_list_insert_element_after (head, elt_prev, i);
1621 if (elt_start_bit <= start)
1623 /* The first bit to turn on is somewhere inside this
1624 elt. */
1625 first_word_to_mod = (start - elt_start_bit) / BITMAP_WORD_BITS;
1627 /* This mask should have 1s in all bits >= start position. */
1628 first_mask =
1629 (((BITMAP_WORD) 1) << ((start % BITMAP_WORD_BITS))) - 1;
1630 first_mask = ~first_mask;
1632 else
1634 /* The first bit to turn on is below this start of this elt. */
1635 first_word_to_mod = 0;
1636 first_mask = ~(BITMAP_WORD) 0;
1639 if (elt_end_bit_plus1 <= end_bit_plus1)
1641 /* The last bit to turn on is beyond this elt. */
1642 last_word_to_mod = BITMAP_ELEMENT_WORDS - 1;
1643 last_mask = ~(BITMAP_WORD) 0;
1645 else
1647 /* The last bit to turn on is inside to this elt. */
1648 last_word_to_mod =
1649 (end_bit_plus1 - elt_start_bit) / BITMAP_WORD_BITS;
1651 /* The last mask should have 1s below the end bit. */
1652 last_mask =
1653 (((BITMAP_WORD) 1) << ((end_bit_plus1 % BITMAP_WORD_BITS))) - 1;
1656 if (first_word_to_mod == last_word_to_mod)
1658 BITMAP_WORD mask = first_mask & last_mask;
1659 elt->bits[first_word_to_mod] |= mask;
1661 else
1663 elt->bits[first_word_to_mod] |= first_mask;
1664 if (BITMAP_ELEMENT_WORDS > 2)
1665 for (ix = first_word_to_mod + 1; ix < last_word_to_mod; ix++)
1666 elt->bits[ix] = ~(BITMAP_WORD) 0;
1667 elt->bits[last_word_to_mod] |= last_mask;
1670 elt_prev = elt;
1671 elt = elt->next;
1674 head->current = elt ? elt : elt_prev;
1675 head->indx = head->current->indx;
1678 /* Clear COUNT bits from START in HEAD. */
1679 void
1680 bitmap_clear_range (bitmap head, unsigned int start, unsigned int count)
1682 unsigned int first_index, end_bit_plus1, last_index;
1683 bitmap_element *elt;
1685 gcc_checking_assert (!head->tree_form);
1687 if (!count)
1688 return;
1690 if (count == 1)
1692 bitmap_clear_bit (head, start);
1693 return;
1696 first_index = start / BITMAP_ELEMENT_ALL_BITS;
1697 end_bit_plus1 = start + count;
1698 last_index = (end_bit_plus1 - 1) / BITMAP_ELEMENT_ALL_BITS;
1699 elt = bitmap_list_find_element (head, first_index);
1701 /* If bitmap_list_find_element returns zero, the current is the closest block
1702 to the result. If the current is less than first index, find the
1703 next one. Otherwise, just set elt to be current. */
1704 if (!elt)
1706 if (head->current)
1708 if (head->indx < first_index)
1710 elt = head->current->next;
1711 if (!elt)
1712 return;
1714 else
1715 elt = head->current;
1717 else
1718 return;
1721 while (elt && (elt->indx <= last_index))
1723 bitmap_element * next_elt = elt->next;
1724 unsigned elt_start_bit = (elt->indx) * BITMAP_ELEMENT_ALL_BITS;
1725 unsigned elt_end_bit_plus1 = elt_start_bit + BITMAP_ELEMENT_ALL_BITS;
1728 if (elt_start_bit >= start && elt_end_bit_plus1 <= end_bit_plus1)
1729 /* Get rid of the entire elt and go to the next one. */
1730 bitmap_list_unlink_element (head, elt);
1731 else
1733 /* Going to have to knock out some bits in this elt. */
1734 unsigned int first_word_to_mod;
1735 BITMAP_WORD first_mask;
1736 unsigned int last_word_to_mod;
1737 BITMAP_WORD last_mask;
1738 unsigned int i;
1739 bool clear = true;
1741 if (elt_start_bit <= start)
1743 /* The first bit to turn off is somewhere inside this
1744 elt. */
1745 first_word_to_mod = (start - elt_start_bit) / BITMAP_WORD_BITS;
1747 /* This mask should have 1s in all bits >= start position. */
1748 first_mask =
1749 (((BITMAP_WORD) 1) << ((start % BITMAP_WORD_BITS))) - 1;
1750 first_mask = ~first_mask;
1752 else
1754 /* The first bit to turn off is below this start of this elt. */
1755 first_word_to_mod = 0;
1756 first_mask = 0;
1757 first_mask = ~first_mask;
1760 if (elt_end_bit_plus1 <= end_bit_plus1)
1762 /* The last bit to turn off is beyond this elt. */
1763 last_word_to_mod = BITMAP_ELEMENT_WORDS - 1;
1764 last_mask = 0;
1765 last_mask = ~last_mask;
1767 else
1769 /* The last bit to turn off is inside to this elt. */
1770 last_word_to_mod =
1771 (end_bit_plus1 - elt_start_bit) / BITMAP_WORD_BITS;
1773 /* The last mask should have 1s below the end bit. */
1774 last_mask =
1775 (((BITMAP_WORD) 1) << (((end_bit_plus1) % BITMAP_WORD_BITS))) - 1;
1779 if (first_word_to_mod == last_word_to_mod)
1781 BITMAP_WORD mask = first_mask & last_mask;
1782 elt->bits[first_word_to_mod] &= ~mask;
1784 else
1786 elt->bits[first_word_to_mod] &= ~first_mask;
1787 if (BITMAP_ELEMENT_WORDS > 2)
1788 for (i = first_word_to_mod + 1; i < last_word_to_mod; i++)
1789 elt->bits[i] = 0;
1790 elt->bits[last_word_to_mod] &= ~last_mask;
1792 for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
1793 if (elt->bits[i])
1795 clear = false;
1796 break;
1798 /* Check to see if there are any bits left. */
1799 if (clear)
1800 bitmap_list_unlink_element (head, elt);
1802 elt = next_elt;
1805 if (elt)
1807 head->current = elt;
1808 head->indx = head->current->indx;
1812 /* A = ~A & B. */
1814 void
1815 bitmap_compl_and_into (bitmap a, const_bitmap b)
1817 bitmap_element *a_elt = a->first;
1818 const bitmap_element *b_elt = b->first;
1819 bitmap_element *a_prev = NULL;
1820 bitmap_element *next;
1822 gcc_checking_assert (!a->tree_form && !b->tree_form);
1823 gcc_assert (a != b);
1825 if (bitmap_empty_p (a))
1827 bitmap_copy (a, b);
1828 return;
1830 if (bitmap_empty_p (b))
1832 bitmap_clear (a);
1833 return;
1836 while (a_elt || b_elt)
1838 if (!b_elt || (a_elt && a_elt->indx < b_elt->indx))
1840 /* A is before B. Remove A */
1841 next = a_elt->next;
1842 a_prev = a_elt->prev;
1843 bitmap_list_unlink_element (a, a_elt);
1844 a_elt = next;
1846 else if (!a_elt || b_elt->indx < a_elt->indx)
1848 /* B is before A. Copy B. */
1849 next = bitmap_list_insert_element_after (a, a_prev, b_elt->indx);
1850 memcpy (next->bits, b_elt->bits, sizeof (next->bits));
1851 a_prev = next;
1852 b_elt = b_elt->next;
1854 else
1856 /* Matching elts, generate A = ~A & B. */
1857 unsigned ix;
1858 BITMAP_WORD ior = 0;
1860 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1862 BITMAP_WORD cleared = a_elt->bits[ix] & b_elt->bits[ix];
1863 BITMAP_WORD r = b_elt->bits[ix] ^ cleared;
1865 a_elt->bits[ix] = r;
1866 ior |= r;
1868 next = a_elt->next;
1869 if (!ior)
1870 bitmap_list_unlink_element (a, a_elt);
1871 else
1872 a_prev = a_elt;
1873 a_elt = next;
1874 b_elt = b_elt->next;
1877 gcc_checking_assert (!a->current == !a->first
1878 && (!a->current || a->indx == a->current->indx));
1879 return;
1883 /* Insert an element corresponding to A_ELT | B_ELT after DST_PREV,
1884 overwriting DST_ELT if non-NULL. CHANGED is true if the destination bitmap
1885 had already been changed; the new value of CHANGED is returned. */
1887 static inline bool
1888 bitmap_elt_ior (bitmap dst, bitmap_element *dst_elt, bitmap_element *dst_prev,
1889 const bitmap_element *a_elt, const bitmap_element *b_elt,
1890 bool changed)
1892 gcc_assert (a_elt || b_elt);
1894 if (a_elt && b_elt && a_elt->indx == b_elt->indx)
1896 /* Matching elts, generate A | B. */
1897 unsigned ix;
1899 if (!changed && dst_elt && dst_elt->indx == a_elt->indx)
1901 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1903 BITMAP_WORD r = a_elt->bits[ix] | b_elt->bits[ix];
1904 if (r != dst_elt->bits[ix])
1906 dst_elt->bits[ix] = r;
1907 changed = true;
1911 else
1913 changed = true;
1914 if (!dst_elt)
1915 dst_elt = bitmap_list_insert_element_after (dst, dst_prev,
1916 a_elt->indx);
1917 else
1918 dst_elt->indx = a_elt->indx;
1919 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1921 BITMAP_WORD r = a_elt->bits[ix] | b_elt->bits[ix];
1922 dst_elt->bits[ix] = r;
1926 else
1928 /* Copy a single element. */
1929 const bitmap_element *src;
1931 if (!b_elt || (a_elt && a_elt->indx < b_elt->indx))
1932 src = a_elt;
1933 else
1934 src = b_elt;
1936 gcc_checking_assert (src);
1937 changed = bitmap_elt_copy (dst, dst_elt, dst_prev, src, changed);
1939 return changed;
1943 /* DST = A | B. Return true if DST changes. */
1945 bool
1946 bitmap_ior (bitmap dst, const_bitmap a, const_bitmap b)
1948 bitmap_element *dst_elt = dst->first;
1949 const bitmap_element *a_elt = a->first;
1950 const bitmap_element *b_elt = b->first;
1951 bitmap_element *dst_prev = NULL;
1952 bitmap_element **dst_prev_pnext = &dst->first;
1953 bool changed = false;
1955 gcc_checking_assert (!dst->tree_form && !a->tree_form && !b->tree_form);
1956 gcc_assert (dst != a && dst != b);
1958 while (a_elt || b_elt)
1960 changed = bitmap_elt_ior (dst, dst_elt, dst_prev, a_elt, b_elt, changed);
1962 if (a_elt && b_elt && a_elt->indx == b_elt->indx)
1964 a_elt = a_elt->next;
1965 b_elt = b_elt->next;
1967 else
1969 if (a_elt && (!b_elt || a_elt->indx <= b_elt->indx))
1970 a_elt = a_elt->next;
1971 else if (b_elt && (!a_elt || b_elt->indx <= a_elt->indx))
1972 b_elt = b_elt->next;
1975 dst_prev = *dst_prev_pnext;
1976 dst_prev_pnext = &dst_prev->next;
1977 dst_elt = *dst_prev_pnext;
1980 if (dst_elt)
1982 changed = true;
1983 /* Ensure that dst->current is valid. */
1984 dst->current = dst->first;
1985 bitmap_elt_clear_from (dst, dst_elt);
1987 gcc_checking_assert (!dst->current == !dst->first);
1988 if (dst->current)
1989 dst->indx = dst->current->indx;
1990 return changed;
1993 /* A |= B. Return true if A changes. */
1995 bool
1996 bitmap_ior_into (bitmap a, const_bitmap b)
1998 bitmap_element *a_elt = a->first;
1999 const bitmap_element *b_elt = b->first;
2000 bitmap_element *a_prev = NULL;
2001 bitmap_element **a_prev_pnext = &a->first;
2002 bool changed = false;
2004 gcc_checking_assert (!a->tree_form && !b->tree_form);
2005 if (a == b)
2006 return false;
2008 while (b_elt)
2010 /* If A lags behind B, just advance it. */
2011 if (!a_elt || a_elt->indx == b_elt->indx)
2013 changed = bitmap_elt_ior (a, a_elt, a_prev, a_elt, b_elt, changed);
2014 b_elt = b_elt->next;
2016 else if (a_elt->indx > b_elt->indx)
2018 changed = bitmap_elt_copy (a, NULL, a_prev, b_elt, changed);
2019 b_elt = b_elt->next;
2022 a_prev = *a_prev_pnext;
2023 a_prev_pnext = &a_prev->next;
2024 a_elt = *a_prev_pnext;
2027 gcc_checking_assert (!a->current == !a->first);
2028 if (a->current)
2029 a->indx = a->current->indx;
2030 return changed;
2033 /* A |= B. Return true if A changes. Free B (re-using its storage
2034 for the result). */
2036 bool
2037 bitmap_ior_into_and_free (bitmap a, bitmap *b_)
2039 bitmap b = *b_;
2040 bitmap_element *a_elt = a->first;
2041 bitmap_element *b_elt = b->first;
2042 bitmap_element *a_prev = NULL;
2043 bitmap_element **a_prev_pnext = &a->first;
2044 bool changed = false;
2046 gcc_checking_assert (!a->tree_form && !b->tree_form);
2047 gcc_assert (a->obstack == b->obstack);
2048 if (a == b)
2049 return false;
2051 while (b_elt)
2053 /* If A lags behind B, just advance it. */
2054 if (!a_elt || a_elt->indx == b_elt->indx)
2056 changed = bitmap_elt_ior (a, a_elt, a_prev, a_elt, b_elt, changed);
2057 b_elt = b_elt->next;
2059 else if (a_elt->indx > b_elt->indx)
2061 bitmap_element *b_elt_next = b_elt->next;
2062 bitmap_list_unlink_element (b, b_elt, false);
2063 bitmap_list_insert_element_after (a, a_prev, b_elt->indx, b_elt);
2064 b_elt = b_elt_next;
2067 a_prev = *a_prev_pnext;
2068 a_prev_pnext = &a_prev->next;
2069 a_elt = *a_prev_pnext;
2072 gcc_checking_assert (!a->current == !a->first);
2073 if (a->current)
2074 a->indx = a->current->indx;
2076 if (b->obstack)
2077 BITMAP_FREE (*b_);
2078 else
2079 bitmap_clear (b);
2080 return changed;
2083 /* DST = A ^ B */
2085 void
2086 bitmap_xor (bitmap dst, const_bitmap a, const_bitmap b)
2088 bitmap_element *dst_elt = dst->first;
2089 const bitmap_element *a_elt = a->first;
2090 const bitmap_element *b_elt = b->first;
2091 bitmap_element *dst_prev = NULL;
2093 gcc_checking_assert (!dst->tree_form && !a->tree_form && !b->tree_form);
2094 gcc_assert (dst != a && dst != b);
2096 if (a == b)
2098 bitmap_clear (dst);
2099 return;
2102 while (a_elt || b_elt)
2104 if (a_elt && b_elt && a_elt->indx == b_elt->indx)
2106 /* Matching elts, generate A ^ B. */
2107 unsigned ix;
2108 BITMAP_WORD ior = 0;
2110 if (!dst_elt)
2111 dst_elt = bitmap_list_insert_element_after (dst, dst_prev,
2112 a_elt->indx);
2113 else
2114 dst_elt->indx = a_elt->indx;
2115 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
2117 BITMAP_WORD r = a_elt->bits[ix] ^ b_elt->bits[ix];
2119 ior |= r;
2120 dst_elt->bits[ix] = r;
2122 a_elt = a_elt->next;
2123 b_elt = b_elt->next;
2124 if (ior)
2126 dst_prev = dst_elt;
2127 dst_elt = dst_elt->next;
2130 else
2132 /* Copy a single element. */
2133 const bitmap_element *src;
2135 if (!b_elt || (a_elt && a_elt->indx < b_elt->indx))
2137 src = a_elt;
2138 a_elt = a_elt->next;
2140 else
2142 src = b_elt;
2143 b_elt = b_elt->next;
2146 if (!dst_elt)
2147 dst_elt = bitmap_list_insert_element_after (dst, dst_prev,
2148 src->indx);
2149 else
2150 dst_elt->indx = src->indx;
2151 memcpy (dst_elt->bits, src->bits, sizeof (dst_elt->bits));
2152 dst_prev = dst_elt;
2153 dst_elt = dst_elt->next;
2156 /* Ensure that dst->current is valid. */
2157 dst->current = dst->first;
2158 bitmap_elt_clear_from (dst, dst_elt);
2159 gcc_checking_assert (!dst->current == !dst->first);
2160 if (dst->current)
2161 dst->indx = dst->current->indx;
2164 /* A ^= B */
2166 void
2167 bitmap_xor_into (bitmap a, const_bitmap b)
2169 bitmap_element *a_elt = a->first;
2170 const bitmap_element *b_elt = b->first;
2171 bitmap_element *a_prev = NULL;
2173 gcc_checking_assert (!a->tree_form && !b->tree_form);
2175 if (a == b)
2177 bitmap_clear (a);
2178 return;
2181 while (b_elt)
2183 if (!a_elt || b_elt->indx < a_elt->indx)
2185 /* Copy b_elt. */
2186 bitmap_element *dst = bitmap_list_insert_element_after (a, a_prev,
2187 b_elt->indx);
2188 memcpy (dst->bits, b_elt->bits, sizeof (dst->bits));
2189 a_prev = dst;
2190 b_elt = b_elt->next;
2192 else if (a_elt->indx < b_elt->indx)
2194 a_prev = a_elt;
2195 a_elt = a_elt->next;
2197 else
2199 /* Matching elts, generate A ^= B. */
2200 unsigned ix;
2201 BITMAP_WORD ior = 0;
2202 bitmap_element *next = a_elt->next;
2204 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
2206 BITMAP_WORD r = a_elt->bits[ix] ^ b_elt->bits[ix];
2208 ior |= r;
2209 a_elt->bits[ix] = r;
2211 b_elt = b_elt->next;
2212 if (ior)
2213 a_prev = a_elt;
2214 else
2215 bitmap_list_unlink_element (a, a_elt);
2216 a_elt = next;
2219 gcc_checking_assert (!a->current == !a->first);
2220 if (a->current)
2221 a->indx = a->current->indx;
2224 /* Return true if two bitmaps are identical.
2225 We do not bother with a check for pointer equality, as that never
2226 occurs in practice. */
2228 bool
2229 bitmap_equal_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;
2239 a_elt = a_elt->next, b_elt = b_elt->next)
2241 if (a_elt->indx != b_elt->indx)
2242 return false;
2243 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
2244 if (a_elt->bits[ix] != b_elt->bits[ix])
2245 return false;
2247 return !a_elt && !b_elt;
2250 /* Return true if A AND B is not empty. */
2252 bool
2253 bitmap_intersect_p (const_bitmap a, const_bitmap b)
2255 const bitmap_element *a_elt;
2256 const bitmap_element *b_elt;
2257 unsigned ix;
2259 gcc_checking_assert (!a->tree_form && !b->tree_form);
2261 for (a_elt = a->first, b_elt = b->first;
2262 a_elt && b_elt;)
2264 if (a_elt->indx < b_elt->indx)
2265 a_elt = a_elt->next;
2266 else if (b_elt->indx < a_elt->indx)
2267 b_elt = b_elt->next;
2268 else
2270 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
2271 if (a_elt->bits[ix] & b_elt->bits[ix])
2272 return true;
2273 a_elt = a_elt->next;
2274 b_elt = b_elt->next;
2277 return false;
2280 /* Return true if A AND NOT B is not empty. */
2282 bool
2283 bitmap_intersect_compl_p (const_bitmap a, const_bitmap b)
2285 const bitmap_element *a_elt;
2286 const bitmap_element *b_elt;
2287 unsigned ix;
2289 gcc_checking_assert (!a->tree_form && !b->tree_form);
2291 for (a_elt = a->first, b_elt = b->first;
2292 a_elt && b_elt;)
2294 if (a_elt->indx < b_elt->indx)
2295 return true;
2296 else if (b_elt->indx < a_elt->indx)
2297 b_elt = b_elt->next;
2298 else
2300 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
2301 if (a_elt->bits[ix] & ~b_elt->bits[ix])
2302 return true;
2303 a_elt = a_elt->next;
2304 b_elt = b_elt->next;
2307 return a_elt != NULL;
2311 /* DST = A | (FROM1 & ~FROM2). Return true if DST changes. */
2313 bool
2314 bitmap_ior_and_compl (bitmap dst, const_bitmap a, const_bitmap b, const_bitmap kill)
2316 bool changed = false;
2318 bitmap_element *dst_elt = dst->first;
2319 const bitmap_element *a_elt = a->first;
2320 const bitmap_element *b_elt = b->first;
2321 const bitmap_element *kill_elt = kill->first;
2322 bitmap_element *dst_prev = NULL;
2323 bitmap_element **dst_prev_pnext = &dst->first;
2325 gcc_checking_assert (!dst->tree_form && !a->tree_form && !b->tree_form
2326 && !kill->tree_form);
2327 gcc_assert (dst != a && dst != b && dst != kill);
2329 /* Special cases. We don't bother checking for bitmap_equal_p (b, kill). */
2330 if (b == kill || bitmap_empty_p (b))
2332 changed = !bitmap_equal_p (dst, a);
2333 if (changed)
2334 bitmap_copy (dst, a);
2335 return changed;
2337 if (bitmap_empty_p (kill))
2338 return bitmap_ior (dst, a, b);
2339 if (bitmap_empty_p (a))
2340 return bitmap_and_compl (dst, b, kill);
2342 while (a_elt || b_elt)
2344 bool new_element = false;
2346 if (b_elt)
2347 while (kill_elt && kill_elt->indx < b_elt->indx)
2348 kill_elt = kill_elt->next;
2350 if (b_elt && kill_elt && kill_elt->indx == b_elt->indx
2351 && (!a_elt || a_elt->indx >= b_elt->indx))
2353 bitmap_element tmp_elt;
2354 unsigned ix;
2356 BITMAP_WORD ior = 0;
2357 tmp_elt.indx = b_elt->indx;
2358 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
2360 BITMAP_WORD r = b_elt->bits[ix] & ~kill_elt->bits[ix];
2361 ior |= r;
2362 tmp_elt.bits[ix] = r;
2365 if (ior)
2367 changed = bitmap_elt_ior (dst, dst_elt, dst_prev,
2368 a_elt, &tmp_elt, changed);
2369 new_element = true;
2370 if (a_elt && a_elt->indx == b_elt->indx)
2371 a_elt = a_elt->next;
2374 b_elt = b_elt->next;
2375 kill_elt = kill_elt->next;
2377 else
2379 changed = bitmap_elt_ior (dst, dst_elt, dst_prev,
2380 a_elt, b_elt, changed);
2381 new_element = true;
2383 if (a_elt && b_elt && a_elt->indx == b_elt->indx)
2385 a_elt = a_elt->next;
2386 b_elt = b_elt->next;
2388 else
2390 if (a_elt && (!b_elt || a_elt->indx <= b_elt->indx))
2391 a_elt = a_elt->next;
2392 else if (b_elt && (!a_elt || b_elt->indx <= a_elt->indx))
2393 b_elt = b_elt->next;
2397 if (new_element)
2399 dst_prev = *dst_prev_pnext;
2400 dst_prev_pnext = &dst_prev->next;
2401 dst_elt = *dst_prev_pnext;
2405 if (dst_elt)
2407 changed = true;
2408 /* Ensure that dst->current is valid. */
2409 dst->current = dst->first;
2410 bitmap_elt_clear_from (dst, dst_elt);
2412 gcc_checking_assert (!dst->current == !dst->first);
2413 if (dst->current)
2414 dst->indx = dst->current->indx;
2416 return changed;
2419 /* A |= (B & ~C). Return true if A changes. */
2421 bool
2422 bitmap_ior_and_compl_into (bitmap a, const_bitmap b, const_bitmap c)
2424 bitmap_element *a_elt = a->first;
2425 const bitmap_element *b_elt = b->first;
2426 const bitmap_element *c_elt = c->first;
2427 bitmap_element and_elt;
2428 bitmap_element *a_prev = NULL;
2429 bitmap_element **a_prev_pnext = &a->first;
2430 bool changed = false;
2431 unsigned ix;
2433 gcc_checking_assert (!a->tree_form && !b->tree_form && !c->tree_form);
2435 if (a == b)
2436 return false;
2437 if (bitmap_empty_p (c))
2438 return bitmap_ior_into (a, b);
2439 else if (bitmap_empty_p (a))
2440 return bitmap_and_compl (a, b, c);
2442 and_elt.indx = -1;
2443 while (b_elt)
2445 /* Advance C. */
2446 while (c_elt && c_elt->indx < b_elt->indx)
2447 c_elt = c_elt->next;
2449 const bitmap_element *and_elt_ptr;
2450 if (c_elt && c_elt->indx == b_elt->indx)
2452 BITMAP_WORD overall = 0;
2453 and_elt_ptr = &and_elt;
2454 and_elt.indx = b_elt->indx;
2455 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
2457 and_elt.bits[ix] = b_elt->bits[ix] & ~c_elt->bits[ix];
2458 overall |= and_elt.bits[ix];
2460 if (!overall)
2462 b_elt = b_elt->next;
2463 continue;
2466 else
2467 and_elt_ptr = b_elt;
2469 b_elt = b_elt->next;
2471 /* Now find a place to insert AND_ELT. */
2474 ix = a_elt ? a_elt->indx : and_elt_ptr->indx;
2475 if (ix == and_elt_ptr->indx)
2476 changed = bitmap_elt_ior (a, a_elt, a_prev, a_elt,
2477 and_elt_ptr, changed);
2478 else if (ix > and_elt_ptr->indx)
2479 changed = bitmap_elt_copy (a, NULL, a_prev, and_elt_ptr, changed);
2481 a_prev = *a_prev_pnext;
2482 a_prev_pnext = &a_prev->next;
2483 a_elt = *a_prev_pnext;
2485 /* If A lagged behind B/C, we advanced it so loop once more. */
2487 while (ix < and_elt_ptr->indx);
2490 gcc_checking_assert (!a->current == !a->first);
2491 if (a->current)
2492 a->indx = a->current->indx;
2493 return changed;
2496 /* A |= (B & C). Return true if A changes. */
2498 bool
2499 bitmap_ior_and_into (bitmap a, const_bitmap b, const_bitmap c)
2501 bitmap_element *a_elt = a->first;
2502 const bitmap_element *b_elt = b->first;
2503 const bitmap_element *c_elt = c->first;
2504 bitmap_element and_elt;
2505 bitmap_element *a_prev = NULL;
2506 bitmap_element **a_prev_pnext = &a->first;
2507 bool changed = false;
2508 unsigned ix;
2510 gcc_checking_assert (!a->tree_form && !b->tree_form && !c->tree_form);
2512 if (b == c)
2513 return bitmap_ior_into (a, b);
2514 if (bitmap_empty_p (b) || bitmap_empty_p (c))
2515 return false;
2517 and_elt.indx = -1;
2518 while (b_elt && c_elt)
2520 BITMAP_WORD overall;
2522 /* Find a common item of B and C. */
2523 while (b_elt->indx != c_elt->indx)
2525 if (b_elt->indx < c_elt->indx)
2527 b_elt = b_elt->next;
2528 if (!b_elt)
2529 goto done;
2531 else
2533 c_elt = c_elt->next;
2534 if (!c_elt)
2535 goto done;
2539 overall = 0;
2540 and_elt.indx = b_elt->indx;
2541 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
2543 and_elt.bits[ix] = b_elt->bits[ix] & c_elt->bits[ix];
2544 overall |= and_elt.bits[ix];
2547 b_elt = b_elt->next;
2548 c_elt = c_elt->next;
2549 if (!overall)
2550 continue;
2552 /* Now find a place to insert AND_ELT. */
2555 ix = a_elt ? a_elt->indx : and_elt.indx;
2556 if (ix == and_elt.indx)
2557 changed = bitmap_elt_ior (a, a_elt, a_prev, a_elt, &and_elt, changed);
2558 else if (ix > and_elt.indx)
2559 changed = bitmap_elt_copy (a, NULL, a_prev, &and_elt, changed);
2561 a_prev = *a_prev_pnext;
2562 a_prev_pnext = &a_prev->next;
2563 a_elt = *a_prev_pnext;
2565 /* If A lagged behind B/C, we advanced it so loop once more. */
2567 while (ix < and_elt.indx);
2570 done:
2571 gcc_checking_assert (!a->current == !a->first);
2572 if (a->current)
2573 a->indx = a->current->indx;
2574 return changed;
2577 /* Compute hash of bitmap (for purposes of hashing). */
2579 hashval_t
2580 bitmap_hash (const_bitmap head)
2582 const bitmap_element *ptr;
2583 BITMAP_WORD hash = 0;
2584 int ix;
2586 gcc_checking_assert (!head->tree_form);
2588 for (ptr = head->first; ptr; ptr = ptr->next)
2590 hash ^= ptr->indx;
2591 for (ix = 0; ix != BITMAP_ELEMENT_WORDS; ix++)
2592 hash ^= ptr->bits[ix];
2594 return (hashval_t)hash;
2598 /* Function to obtain a vector of bitmap elements in bit order from
2599 HEAD in tree view. */
2601 static void
2602 bitmap_tree_to_vec (vec<bitmap_element *> &elts, const_bitmap head)
2604 gcc_checking_assert (head->tree_form);
2605 auto_vec<bitmap_element *, 32> stack;
2606 bitmap_element *e = head->first;
2607 while (true)
2609 while (e != NULL)
2611 stack.safe_push (e);
2612 e = e->prev;
2614 if (stack.is_empty ())
2615 break;
2617 e = stack.pop ();
2618 elts.safe_push (e);
2619 e = e->next;
2623 /* Debugging function to print out the contents of a bitmap element. */
2625 DEBUG_FUNCTION void
2626 debug_bitmap_elt_file (FILE *file, const bitmap_element *ptr)
2628 unsigned int i, j, col = 26;
2630 fprintf (file, "\t" HOST_PTR_PRINTF " next = " HOST_PTR_PRINTF
2631 " prev = " HOST_PTR_PRINTF " indx = %u\n\t\tbits = {",
2632 (const void*) ptr, (const void*) ptr->next,
2633 (const void*) ptr->prev, ptr->indx);
2635 for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
2636 for (j = 0; j < BITMAP_WORD_BITS; j++)
2637 if ((ptr->bits[i] >> j) & 1)
2639 if (col > 70)
2641 fprintf (file, "\n\t\t\t");
2642 col = 24;
2645 fprintf (file, " %u", (ptr->indx * BITMAP_ELEMENT_ALL_BITS
2646 + i * BITMAP_WORD_BITS + j));
2647 col += 4;
2650 fprintf (file, " }\n");
2653 /* Debugging function to print out the contents of a bitmap. */
2655 DEBUG_FUNCTION void
2656 debug_bitmap_file (FILE *file, const_bitmap head)
2658 const bitmap_element *ptr;
2660 fprintf (file, "\nfirst = " HOST_PTR_PRINTF
2661 " current = " HOST_PTR_PRINTF " indx = %u\n",
2662 (void *) head->first, (void *) head->current, head->indx);
2664 if (head->tree_form)
2666 auto_vec<bitmap_element *, 32> elts;
2667 bitmap_tree_to_vec (elts, head);
2668 for (unsigned i = 0; i < elts.length (); ++i)
2669 debug_bitmap_elt_file (file, elts[i]);
2671 else
2672 for (ptr = head->first; ptr; ptr = ptr->next)
2673 debug_bitmap_elt_file (file, ptr);
2676 /* Function to be called from the debugger to print the contents
2677 of a bitmap. */
2679 DEBUG_FUNCTION void
2680 debug_bitmap (const_bitmap head)
2682 debug_bitmap_file (stderr, head);
2685 /* Function to print out the contents of a bitmap. Unlike debug_bitmap_file,
2686 it does not print anything but the bits. */
2688 DEBUG_FUNCTION void
2689 bitmap_print (FILE *file, const_bitmap head, const char *prefix,
2690 const char *suffix)
2692 const char *comma = "";
2693 unsigned i;
2695 fputs (prefix, file);
2696 if (head->tree_form)
2698 auto_vec<bitmap_element *, 32> elts;
2699 bitmap_tree_to_vec (elts, head);
2700 for (i = 0; i < elts.length (); ++i)
2701 for (unsigned ix = 0; ix != BITMAP_ELEMENT_WORDS; ++ix)
2703 BITMAP_WORD word = elts[i]->bits[ix];
2704 for (unsigned bit = 0; bit != BITMAP_WORD_BITS; ++bit)
2705 if (word & ((BITMAP_WORD)1 << bit))
2707 fprintf (file, "%s%d", comma,
2708 (bit + BITMAP_WORD_BITS * ix
2709 + elts[i]->indx * BITMAP_ELEMENT_ALL_BITS));
2710 comma = ", ";
2714 else
2716 bitmap_iterator bi;
2717 EXECUTE_IF_SET_IN_BITMAP (head, 0, i, bi)
2719 fprintf (file, "%s%d", comma, i);
2720 comma = ", ";
2723 fputs (suffix, file);
2726 /* Output per-bitmap memory usage statistics. */
2727 void
2728 dump_bitmap_statistics (void)
2730 if (!GATHER_STATISTICS)
2731 return;
2733 bitmap_mem_desc.dump (BITMAP_ORIGIN);
2736 DEBUG_FUNCTION void
2737 debug (const bitmap_head &ref)
2739 dump_bitmap (stderr, &ref);
2742 DEBUG_FUNCTION void
2743 debug (const bitmap_head *ptr)
2745 if (ptr)
2746 debug (*ptr);
2747 else
2748 fprintf (stderr, "<nil>\n");
2751 void
2752 bitmap_head::dump ()
2754 debug (this);
2757 #if CHECKING_P
2759 namespace selftest {
2761 /* Selftests for bitmaps. */
2763 /* Freshly-created bitmaps ought to be empty. */
2765 static void
2766 test_gc_alloc ()
2768 bitmap b = bitmap_gc_alloc ();
2769 ASSERT_TRUE (bitmap_empty_p (b));
2772 /* Verify bitmap_set_range. */
2774 static void
2775 test_set_range ()
2777 bitmap b = bitmap_gc_alloc ();
2778 ASSERT_TRUE (bitmap_empty_p (b));
2780 bitmap_set_range (b, 7, 5);
2781 ASSERT_FALSE (bitmap_empty_p (b));
2782 ASSERT_EQ (5, bitmap_count_bits (b));
2784 /* Verify bitmap_bit_p at the boundaries. */
2785 ASSERT_FALSE (bitmap_bit_p (b, 6));
2786 ASSERT_TRUE (bitmap_bit_p (b, 7));
2787 ASSERT_TRUE (bitmap_bit_p (b, 11));
2788 ASSERT_FALSE (bitmap_bit_p (b, 12));
2791 /* Verify splitting a range into two pieces using bitmap_clear_bit. */
2793 static void
2794 test_clear_bit_in_middle ()
2796 bitmap b = bitmap_gc_alloc ();
2798 /* Set b to [100..200]. */
2799 bitmap_set_range (b, 100, 100);
2800 ASSERT_EQ (100, bitmap_count_bits (b));
2802 /* Clear a bit in the middle. */
2803 bool changed = bitmap_clear_bit (b, 150);
2804 ASSERT_TRUE (changed);
2805 ASSERT_EQ (99, bitmap_count_bits (b));
2806 ASSERT_TRUE (bitmap_bit_p (b, 149));
2807 ASSERT_FALSE (bitmap_bit_p (b, 150));
2808 ASSERT_TRUE (bitmap_bit_p (b, 151));
2811 /* Verify bitmap_copy. */
2813 static void
2814 test_copying ()
2816 bitmap src = bitmap_gc_alloc ();
2817 bitmap_set_range (src, 40, 10);
2819 bitmap dst = bitmap_gc_alloc ();
2820 ASSERT_FALSE (bitmap_equal_p (src, dst));
2821 bitmap_copy (dst, src);
2822 ASSERT_TRUE (bitmap_equal_p (src, dst));
2824 /* Verify that we can make them unequal again... */
2825 bitmap_set_range (src, 70, 5);
2826 ASSERT_FALSE (bitmap_equal_p (src, dst));
2828 /* ...and that changing src after the copy didn't affect
2829 the other: */
2830 ASSERT_FALSE (bitmap_bit_p (dst, 70));
2833 /* Verify bitmap_single_bit_set_p. */
2835 static void
2836 test_bitmap_single_bit_set_p ()
2838 bitmap b = bitmap_gc_alloc ();
2840 ASSERT_FALSE (bitmap_single_bit_set_p (b));
2842 bitmap_set_range (b, 42, 1);
2843 ASSERT_TRUE (bitmap_single_bit_set_p (b));
2844 ASSERT_EQ (42, bitmap_first_set_bit (b));
2846 bitmap_set_range (b, 1066, 1);
2847 ASSERT_FALSE (bitmap_single_bit_set_p (b));
2848 ASSERT_EQ (42, bitmap_first_set_bit (b));
2850 bitmap_clear_range (b, 0, 100);
2851 ASSERT_TRUE (bitmap_single_bit_set_p (b));
2852 ASSERT_EQ (1066, bitmap_first_set_bit (b));
2855 /* Run all of the selftests within this file. */
2857 void
2858 bitmap_c_tests ()
2860 test_gc_alloc ();
2861 test_set_range ();
2862 test_clear_bit_in_middle ();
2863 test_copying ();
2864 test_bitmap_single_bit_set_p ();
2867 } // namespace selftest
2868 #endif /* CHECKING_P */
2870 #include "gt-bitmap.h"