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
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
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/>. */
22 #include "coretypes.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
31 bitmap_obstack
bitmap_head::crashme
;
33 static bitmap_element
*bitmap_tree_listify_from (bitmap
, bitmap_element
*);
35 /* Register new bitmap. */
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. */
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. */
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
);
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
74 /* Bitmap memory management. */
76 /* Add ELT to the appropriate freelist. */
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);
89 elt
->prev
= bit_obstack
->elements
;
90 bit_obstack
->elements
= elt
;
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
;
109 element
= bit_obstack
->elements
;
112 /* Use up the inner list first before looking at the next
113 element of the outer list. */
116 bit_obstack
->elements
= element
->next
;
117 bit_obstack
->elements
->prev
= element
->prev
;
120 /* Inner list was just a singleton. */
121 bit_obstack
->elements
= element
->prev
;
123 element
= XOBNEW (&bit_obstack
->obstack
, bitmap_element
);
127 element
= bitmap_ggc_free
;
129 /* Use up the inner list first before looking at the next
130 element of the outer list. */
133 bitmap_ggc_free
= element
->next
;
134 bitmap_ggc_free
->prev
= element
->prev
;
137 /* Inner list was just a singleton. */
138 bitmap_ggc_free
= element
->prev
;
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
));
151 /* Remove ELT and all following elements from bitmap HEAD.
152 Put the released elements in the freelist for HEAD. */
155 bitmap_elt_clear_from (bitmap head
, bitmap_element
*elt
)
157 bitmap_element
*prev
;
158 bitmap_obstack
*bit_obstack
= head
->obstack
;
164 elt
= bitmap_tree_listify_from (head
, elt
);
166 if (GATHER_STATISTICS
)
169 for (prev
= elt
; prev
; prev
= prev
->next
)
171 release_overhead (head
, sizeof (bitmap_element
) * n
, false);
178 if (head
->current
->indx
> prev
->indx
)
180 head
->current
= prev
;
181 head
->indx
= prev
->indx
;
187 head
->current
= NULL
;
191 /* Put the entire list onto the freelist in one operation. */
194 elt
->prev
= bit_obstack
->elements
;
195 bit_obstack
->elements
= elt
;
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. */
212 bitmap_list_link_element (bitmap head
, bitmap_element
*element
)
214 unsigned int indx
= element
->indx
;
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
;
236 ptr
->prev
->next
= element
;
238 head
->first
= element
;
240 element
->prev
= ptr
->prev
;
245 /* Otherwise, it must go someplace after the current element. */
248 for (ptr
= head
->current
;
249 ptr
->next
!= 0 && ptr
->next
->indx
< indx
;
254 ptr
->next
->prev
= element
;
256 element
->next
= ptr
->next
;
261 /* Set up so this is the first element searched. */
262 head
->current
= element
;
266 /* Unlink the bitmap element from the current bitmap linked list,
267 and return it to the freelist. */
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
);
284 if (head
->first
== element
)
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
;
293 head
->indx
= head
->current
->indx
;
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
)
312 node
= bitmap_element_allocate (head
);
315 gcc_checking_assert (!head
->tree_form
);
321 head
->current
= node
;
324 node
->next
= head
->first
;
326 node
->next
->prev
= node
;
332 gcc_checking_assert (head
->current
);
333 node
->next
= elt
->next
;
335 node
->next
->prev
= 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
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
)
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
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
++;
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
)
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. */
428 bitmap_tree_link_left (bitmap_element
* &t
, bitmap_element
* &l
)
436 bitmap_tree_link_right (bitmap_element
* &t
, bitmap_element
* &r
)
444 bitmap_tree_rotate_left (bitmap_element
* &t
)
446 bitmap_element
*e
= t
->next
;
447 t
->next
= t
->next
->prev
;
453 bitmap_tree_rotate_right (bitmap_element
* &t
)
455 bitmap_element
*e
= t
->prev
;
456 t
->prev
= t
->prev
->next
;
461 static bitmap_element
*
462 bitmap_tree_splay (bitmap head
, bitmap_element
*t
, unsigned int indx
)
464 bitmap_element N
, *l
, *r
;
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
;
476 while (indx
!= t
->indx
)
478 if (GATHER_STATISTICS
&& usage
)
479 usage
->m_search_iter
++;
483 if (t
->prev
!= NULL
&& indx
< t
->prev
->indx
)
484 bitmap_tree_rotate_right (t
);
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
);
495 bitmap_tree_link_left (t
, l
);
506 /* Link bitmap element E into the current bitmap splay tree. */
509 bitmap_tree_link_element (bitmap head
, bitmap_element
*e
)
511 if (head
->first
== NULL
)
512 e
->prev
= e
->next
= NULL
;
515 bitmap_element
*t
= bitmap_tree_splay (head
, head
->first
, e
->indx
);
516 if (e
->indx
< t
->indx
)
522 else if (e
->indx
> t
->indx
)
533 head
->indx
= e
->indx
;
536 /* Unlink bitmap element E from the current bitmap splay tree,
537 and return it to the freelist. */
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
);
550 t
= bitmap_tree_splay (head
, e
->prev
, e
->indx
);
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
)
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. */
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. */
613 head
->indx
= (t
!= NULL
) ? t
->indx
: 0;
615 /* Detach the tree from E, and re-attach the right branch of E. */
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
626 auto_vec
<bitmap_element
*, 32> stack
;
627 auto_vec
<bitmap_element
*, 32> sorted_elements
;
628 bitmap_element
*n
= e
;
638 if (stack
.is_empty ())
642 sorted_elements
.safe_push (n
);
646 gcc_assert (sorted_elements
[0] == e
);
648 bitmap_element
*prev
= NULL
;
650 FOR_EACH_VEC_ELT (sorted_elements
, ix
, n
)
662 /* Convert bitmap HEAD from splay-tree view to linked-list view. */
665 bitmap_list_view (bitmap head
)
669 gcc_assert (head
->tree_form
);
675 bitmap_tree_rotate_right (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. */
689 bitmap_tree_view (bitmap head
)
693 gcc_assert (! head
->tree_form
);
702 head
->tree_form
= true;
705 /* Clear a bitmap by freeing all its elements. */
708 bitmap_clear (bitmap head
)
710 if (head
->first
== NULL
)
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
);
721 bitmap_elt_clear_from (head
, head
->first
);
724 /* Initialize a bitmap obstack. If BIT_OBSTACK is NULL, initialize
725 the default bitmap obstack. */
728 bitmap_obstack_initialize (bitmap_obstack
*bit_obstack
)
732 if (bitmap_default_obstack_depth
++)
734 bit_obstack
= &bitmap_default_obstack
;
737 #if !defined(__GNUC__) || (__GNUC__ < 2)
738 #define __alignof__(type) 0
741 bit_obstack
->elements
= NULL
;
742 bit_obstack
->heads
= NULL
;
743 obstack_specify_allocation (&bit_obstack
->obstack
, OBSTACK_CHUNK_SIZE
,
744 __alignof__ (bitmap_element
),
749 /* Release the memory from a bitmap obstack. If BIT_OBSTACK is NULL,
750 release the default bitmap obstack. */
753 bitmap_obstack_release (bitmap_obstack
*bit_obstack
)
757 if (--bitmap_default_obstack_depth
)
759 gcc_assert (bitmap_default_obstack_depth
> 0);
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. */
774 bitmap_alloc (bitmap_obstack
*bit_obstack MEM_STAT_DECL
)
779 bit_obstack
= &bitmap_default_obstack
;
780 map
= bit_obstack
->heads
;
782 bit_obstack
->heads
= (class bitmap_head
*) map
->first
;
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
));
793 /* Create a new GCd bitmap. */
796 bitmap_gc_alloc (ALONE_MEM_STAT_DECL
)
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
));
809 /* Release an obstack allocated bitmap. */
812 bitmap_obstack_free (bitmap 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. */
830 bitmap_element_zerop (const bitmap_element
*element
)
832 #if BITMAP_ELEMENT_WORDS == 2
833 return (element
->bits
[0] | element
->bits
[1]) == 0;
837 for (i
= 0; i
< BITMAP_ELEMENT_WORDS
; i
++)
838 if (element
->bits
[i
] != 0)
845 /* Copy a bitmap to another bitmap. */
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
);
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
870 to
->first
= to
->current
= to_elt
;
871 to
->indx
= from_ptr
->indx
;
872 to_elt
->next
= to_elt
->prev
= 0;
876 to_elt
->prev
= to_ptr
;
878 to_ptr
->next
= to_elt
;
885 /* Move a bitmap to another bitmap. */
888 bitmap_move (bitmap to
, bitmap from
)
890 gcc_assert (to
->obstack
== from
->obstack
);
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
);
904 if (GATHER_STATISTICS
)
905 release_overhead (from
, sz
, false);
908 /* Clear a single bit in a bitmap. Return true if the bit changed. */
911 bitmap_clear_bit (bitmap head
, int bit
)
913 unsigned int indx
= bit
/ BITMAP_ELEMENT_ALL_BITS
;
916 if (!head
->tree_form
)
917 ptr
= bitmap_list_find_element (head
, indx
);
919 ptr
= bitmap_tree_find_element (head
, indx
);
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;
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
);
936 bitmap_tree_unlink_element (head
, ptr
);
946 /* Set a single bit in a bitmap. Return true if the bit changed. */
949 bitmap_set_bit (bitmap head
, int bit
)
951 unsigned indx
= bit
/ BITMAP_ELEMENT_ALL_BITS
;
953 if (!head
->tree_form
)
954 ptr
= bitmap_list_find_element (head
, indx
);
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
;
963 bool res
= (ptr
->bits
[word_num
] & bit_val
) == 0;
965 ptr
->bits
[word_num
] |= bit_val
;
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
);
975 bitmap_tree_link_element (head
, ptr
);
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
;
989 if (!head
->tree_form
)
990 ptr
= bitmap_list_find_element (const_cast<bitmap
> (head
), indx
);
992 ptr
= bitmap_tree_find_element (const_cast<bitmap
> (head
), indx
);
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;
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];
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
]);
1042 count
+= bitmap_popcount (bits
[ix
]);
1048 /* Count the number of bits set in the bitmap, and return it. */
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
);
1063 /* Count the number of unique bits set in A and B and return it. */
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
;
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
;
1099 /* Return true if the bitmap has a single bit set. Otherwise return
1103 bitmap_single_bit_set_p (const_bitmap a
)
1105 unsigned long count
= 0;
1106 const bitmap_element
*elt
;
1109 if (bitmap_empty_p (a
))
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
))
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
]);
1127 count
+= bitmap_popcount (elt
->bits
[ix
]);
1137 /* Return the bit number of the first set bit in the bitmap. The
1138 bitmap must be non-empty. */
1141 bitmap_first_set_bit (const_bitmap a
)
1143 const bitmap_element
*elt
= a
->first
;
1148 gcc_checking_assert (elt
);
1154 bit_no
= elt
->indx
* BITMAP_ELEMENT_ALL_BITS
;
1155 for (ix
= 0; ix
!= BITMAP_ELEMENT_WORDS
; ix
++)
1157 word
= elt
->bits
[ix
];
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
);
1169 /* Binary search for the first set bit. */
1170 #if BITMAP_WORD_BITS > 64
1171 #error "Fill out the table."
1173 #if BITMAP_WORD_BITS > 32
1174 if (!(word
& 0xffffffff))
1175 word
>>= 32, bit_no
+= 32;
1177 if (!(word
& 0xffff))
1178 word
>>= 16, bit_no
+= 16;
1180 word
>>= 8, bit_no
+= 8;
1182 word
>>= 4, bit_no
+= 4;
1184 word
>>= 2, bit_no
+= 2;
1186 word
>>= 1, bit_no
+= 1;
1188 gcc_checking_assert (word
& 1);
1193 /* Return the bit number of the first set bit in the bitmap. The
1194 bitmap must be non-empty. */
1197 bitmap_last_set_bit (const_bitmap a
)
1199 const bitmap_element
*elt
;
1207 elt
= a
->current
? a
->current
: a
->first
;
1208 gcc_checking_assert (elt
);
1213 bit_no
= elt
->indx
* BITMAP_ELEMENT_ALL_BITS
;
1214 for (ix
= BITMAP_ELEMENT_WORDS
- 1; ix
>= 1; ix
--)
1216 word
= elt
->bits
[ix
];
1220 gcc_assert (elt
->bits
[ix
] != 0);
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;
1227 /* Hopefully this is a twos-complement host... */
1228 BITMAP_WORD x
= word
;
1234 #if BITMAP_WORD_BITS > 32
1237 bit_no
+= bitmap_popcount (x
) - 1;
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
);
1259 bitmap_copy (dst
, a
);
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
;
1271 /* Matching elts, generate A & B. */
1273 BITMAP_WORD ior
= 0;
1276 dst_elt
= bitmap_list_insert_element_after (dst
, dst_prev
,
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
;
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
);
1301 dst
->indx
= dst
->current
->indx
;
1304 /* A &= B. Return true if A changed. */
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
);
1319 while (a_elt
&& b_elt
)
1321 if (a_elt
->indx
< b_elt
->indx
)
1324 bitmap_list_unlink_element (a
, a_elt
);
1328 else if (b_elt
->indx
< a_elt
->indx
)
1329 b_elt
= b_elt
->next
;
1332 /* Matching elts, generate A &= B. */
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
)
1341 a_elt
->bits
[ix
] = r
;
1346 bitmap_list_unlink_element (a
, a_elt
);
1348 b_elt
= b_elt
->next
;
1355 bitmap_elt_clear_from (a
, a_elt
);
1358 gcc_checking_assert (!a
->current
== !a
->first
1359 && (!a
->current
|| a
->indx
== a
->current
->indx
));
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. */
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
)
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
];
1388 dst_elt
= bitmap_list_insert_element_after (dst
, dst_prev
,
1391 dst_elt
->indx
= src_elt
->indx
;
1392 memcpy (dst_elt
->bits
, src_elt
->bits
, sizeof (dst_elt
->bits
));
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
);
1416 changed
= !bitmap_empty_p (dst
);
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
;
1437 /* Matching elts, generate A & ~B. */
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
)
1450 dst_elt
->bits
[ix
] = r
;
1458 if (!dst_elt
|| dst_elt
->indx
> a_elt
->indx
)
1460 dst_elt
= bitmap_list_insert_element_after (dst
, dst_prev
,
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
;
1482 changed
|= !new_element
;
1483 bitmap_list_unlink_element (dst
, dst_elt
);
1484 dst_elt
= *dst_prev_pnext
;
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
;
1505 bitmap_elt_clear_from (dst
, dst_elt
);
1507 gcc_checking_assert (!dst
->current
== !dst
->first
);
1509 dst
->indx
= dst
->current
->indx
;
1514 /* A &= ~B. Returns true if A changes */
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
);
1528 if (bitmap_empty_p (a
))
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
;
1545 /* Matching elts, generate A &= ~B. */
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
;
1560 bitmap_list_unlink_element (a
, a_elt
);
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. */
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
;
1578 gcc_checking_assert (!head
->tree_form
);
1585 bitmap_set_bit (head
, start
);
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". */
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
;
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
1625 first_word_to_mod
= (start
- elt_start_bit
) / BITMAP_WORD_BITS
;
1627 /* This mask should have 1s in all bits >= start position. */
1629 (((BITMAP_WORD
) 1) << ((start
% BITMAP_WORD_BITS
))) - 1;
1630 first_mask
= ~first_mask
;
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;
1647 /* The last bit to turn on is inside to this elt. */
1649 (end_bit_plus1
- elt_start_bit
) / BITMAP_WORD_BITS
;
1651 /* The last mask should have 1s below the end bit. */
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
;
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
;
1674 head
->current
= elt
? elt
: elt_prev
;
1675 head
->indx
= head
->current
->indx
;
1678 /* Clear COUNT bits from START in HEAD. */
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
);
1692 bitmap_clear_bit (head
, start
);
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. */
1708 if (head
->indx
< first_index
)
1710 elt
= head
->current
->next
;
1715 elt
= head
->current
;
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
);
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
;
1741 if (elt_start_bit
<= start
)
1743 /* The first bit to turn off is somewhere inside this
1745 first_word_to_mod
= (start
- elt_start_bit
) / BITMAP_WORD_BITS
;
1747 /* This mask should have 1s in all bits >= start position. */
1749 (((BITMAP_WORD
) 1) << ((start
% BITMAP_WORD_BITS
))) - 1;
1750 first_mask
= ~first_mask
;
1754 /* The first bit to turn off is below this start of this elt. */
1755 first_word_to_mod
= 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;
1765 last_mask
= ~last_mask
;
1769 /* The last bit to turn off is inside to this elt. */
1771 (end_bit_plus1
- elt_start_bit
) / BITMAP_WORD_BITS
;
1773 /* The last mask should have 1s below the end bit. */
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
;
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
++)
1790 elt
->bits
[last_word_to_mod
] &= ~last_mask
;
1792 for (i
= 0; i
< BITMAP_ELEMENT_WORDS
; i
++)
1798 /* Check to see if there are any bits left. */
1800 bitmap_list_unlink_element (head
, elt
);
1807 head
->current
= elt
;
1808 head
->indx
= head
->current
->indx
;
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
))
1830 if (bitmap_empty_p (b
))
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 */
1842 a_prev
= a_elt
->prev
;
1843 bitmap_list_unlink_element (a
, a_elt
);
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
));
1852 b_elt
= b_elt
->next
;
1856 /* Matching elts, generate A = ~A & B. */
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
;
1870 bitmap_list_unlink_element (a
, a_elt
);
1874 b_elt
= b_elt
->next
;
1877 gcc_checking_assert (!a
->current
== !a
->first
1878 && (!a
->current
|| a
->indx
== a
->current
->indx
));
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. */
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
,
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. */
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
;
1915 dst_elt
= bitmap_list_insert_element_after (dst
, dst_prev
,
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
;
1928 /* Copy a single element. */
1929 const bitmap_element
*src
;
1931 if (!b_elt
|| (a_elt
&& a_elt
->indx
< b_elt
->indx
))
1936 gcc_checking_assert (src
);
1937 changed
= bitmap_elt_copy (dst
, dst_elt
, dst_prev
, src
, changed
);
1943 /* DST = A | B. Return true if DST changes. */
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
;
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
;
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
);
1989 dst
->indx
= dst
->current
->indx
;
1993 /* A |= B. Return true if A changes. */
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
);
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
);
2029 a
->indx
= a
->current
->indx
;
2033 /* A |= B. Return true if A changes. Free B (re-using its storage
2037 bitmap_ior_into_and_free (bitmap a
, bitmap
*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
);
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
);
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
);
2074 a
->indx
= a
->current
->indx
;
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
);
2102 while (a_elt
|| b_elt
)
2104 if (a_elt
&& b_elt
&& a_elt
->indx
== b_elt
->indx
)
2106 /* Matching elts, generate A ^ B. */
2108 BITMAP_WORD ior
= 0;
2111 dst_elt
= bitmap_list_insert_element_after (dst
, dst_prev
,
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
];
2120 dst_elt
->bits
[ix
] = r
;
2122 a_elt
= a_elt
->next
;
2123 b_elt
= b_elt
->next
;
2127 dst_elt
= dst_elt
->next
;
2132 /* Copy a single element. */
2133 const bitmap_element
*src
;
2135 if (!b_elt
|| (a_elt
&& a_elt
->indx
< b_elt
->indx
))
2138 a_elt
= a_elt
->next
;
2143 b_elt
= b_elt
->next
;
2147 dst_elt
= bitmap_list_insert_element_after (dst
, dst_prev
,
2150 dst_elt
->indx
= src
->indx
;
2151 memcpy (dst_elt
->bits
, src
->bits
, sizeof (dst_elt
->bits
));
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
);
2161 dst
->indx
= dst
->current
->indx
;
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
);
2183 if (!a_elt
|| b_elt
->indx
< a_elt
->indx
)
2186 bitmap_element
*dst
= bitmap_list_insert_element_after (a
, a_prev
,
2188 memcpy (dst
->bits
, b_elt
->bits
, sizeof (dst
->bits
));
2190 b_elt
= b_elt
->next
;
2192 else if (a_elt
->indx
< b_elt
->indx
)
2195 a_elt
= a_elt
->next
;
2199 /* Matching elts, generate A ^= B. */
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
];
2209 a_elt
->bits
[ix
] = r
;
2211 b_elt
= b_elt
->next
;
2215 bitmap_list_unlink_element (a
, a_elt
);
2219 gcc_checking_assert (!a
->current
== !a
->first
);
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. */
2229 bitmap_equal_p (const_bitmap a
, const_bitmap b
)
2231 const bitmap_element
*a_elt
;
2232 const bitmap_element
*b_elt
;
2235 gcc_checking_assert (!a
->tree_form
&& !b
->tree_form
);
2237 for (a_elt
= a
->first
, b_elt
= b
->first
;
2239 a_elt
= a_elt
->next
, b_elt
= b_elt
->next
)
2241 if (a_elt
->indx
!= b_elt
->indx
)
2243 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
2244 if (a_elt
->bits
[ix
] != b_elt
->bits
[ix
])
2247 return !a_elt
&& !b_elt
;
2250 /* Return true if A AND B is not empty. */
2253 bitmap_intersect_p (const_bitmap a
, const_bitmap b
)
2255 const bitmap_element
*a_elt
;
2256 const bitmap_element
*b_elt
;
2259 gcc_checking_assert (!a
->tree_form
&& !b
->tree_form
);
2261 for (a_elt
= a
->first
, b_elt
= b
->first
;
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
;
2270 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
2271 if (a_elt
->bits
[ix
] & b_elt
->bits
[ix
])
2273 a_elt
= a_elt
->next
;
2274 b_elt
= b_elt
->next
;
2280 /* Return true if A AND NOT B is not empty. */
2283 bitmap_intersect_compl_p (const_bitmap a
, const_bitmap b
)
2285 const bitmap_element
*a_elt
;
2286 const bitmap_element
*b_elt
;
2289 gcc_checking_assert (!a
->tree_form
&& !b
->tree_form
);
2291 for (a_elt
= a
->first
, b_elt
= b
->first
;
2294 if (a_elt
->indx
< b_elt
->indx
)
2296 else if (b_elt
->indx
< a_elt
->indx
)
2297 b_elt
= b_elt
->next
;
2300 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
2301 if (a_elt
->bits
[ix
] & ~b_elt
->bits
[ix
])
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. */
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
);
2334 bitmap_copy (dst
, a
);
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;
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
;
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
];
2362 tmp_elt
.bits
[ix
] = r
;
2367 changed
= bitmap_elt_ior (dst
, dst_elt
, dst_prev
,
2368 a_elt
, &tmp_elt
, changed
);
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
;
2379 changed
= bitmap_elt_ior (dst
, dst_elt
, dst_prev
,
2380 a_elt
, b_elt
, changed
);
2383 if (a_elt
&& b_elt
&& a_elt
->indx
== b_elt
->indx
)
2385 a_elt
= a_elt
->next
;
2386 b_elt
= b_elt
->next
;
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
;
2399 dst_prev
= *dst_prev_pnext
;
2400 dst_prev_pnext
= &dst_prev
->next
;
2401 dst_elt
= *dst_prev_pnext
;
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
);
2414 dst
->indx
= dst
->current
->indx
;
2419 /* A |= (B & ~C). Return true if A changes. */
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;
2433 gcc_checking_assert (!a
->tree_form
&& !b
->tree_form
&& !c
->tree_form
);
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
);
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
];
2462 b_elt
= b_elt
->next
;
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
);
2492 a
->indx
= a
->current
->indx
;
2496 /* A |= (B & C). Return true if A changes. */
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;
2510 gcc_checking_assert (!a
->tree_form
&& !b
->tree_form
&& !c
->tree_form
);
2513 return bitmap_ior_into (a
, b
);
2514 if (bitmap_empty_p (b
) || bitmap_empty_p (c
))
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
;
2533 c_elt
= c_elt
->next
;
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
;
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
);
2571 gcc_checking_assert (!a
->current
== !a
->first
);
2573 a
->indx
= a
->current
->indx
;
2577 /* Compute hash of bitmap (for purposes of hashing). */
2580 bitmap_hash (const_bitmap head
)
2582 const bitmap_element
*ptr
;
2583 BITMAP_WORD hash
= 0;
2586 gcc_checking_assert (!head
->tree_form
);
2588 for (ptr
= head
->first
; ptr
; ptr
= ptr
->next
)
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. */
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
;
2611 stack
.safe_push (e
);
2614 if (stack
.is_empty ())
2623 /* Debugging function to print out the contents of a bitmap element. */
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)
2641 fprintf (file
, "\n\t\t\t");
2645 fprintf (file
, " %u", (ptr
->indx
* BITMAP_ELEMENT_ALL_BITS
2646 + i
* BITMAP_WORD_BITS
+ j
));
2650 fprintf (file
, " }\n");
2653 /* Debugging function to print out the contents of a bitmap. */
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
]);
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
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. */
2689 bitmap_print (FILE *file
, const_bitmap head
, const char *prefix
,
2692 const char *comma
= "";
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
));
2717 EXECUTE_IF_SET_IN_BITMAP (head
, 0, i
, bi
)
2719 fprintf (file
, "%s%d", comma
, i
);
2723 fputs (suffix
, file
);
2726 /* Output per-bitmap memory usage statistics. */
2728 dump_bitmap_statistics (void)
2730 if (!GATHER_STATISTICS
)
2733 bitmap_mem_desc
.dump (BITMAP_ORIGIN
);
2737 debug (const bitmap_head
&ref
)
2739 dump_bitmap (stderr
, &ref
);
2743 debug (const bitmap_head
*ptr
)
2748 fprintf (stderr
, "<nil>\n");
2752 bitmap_head::dump ()
2759 namespace selftest
{
2761 /* Selftests for bitmaps. */
2763 /* Freshly-created bitmaps ought to be empty. */
2768 bitmap b
= bitmap_gc_alloc ();
2769 ASSERT_TRUE (bitmap_empty_p (b
));
2772 /* Verify bitmap_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. */
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. */
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
2830 ASSERT_FALSE (bitmap_bit_p (dst
, 70));
2833 /* Verify bitmap_single_bit_set_p. */
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. */
2862 test_clear_bit_in_middle ();
2864 test_bitmap_single_bit_set_p ();
2867 } // namespace selftest
2868 #endif /* CHECKING_P */
2870 #include "gt-bitmap.h"