1 /* Functions to support general ended bitmaps.
2 Copyright (C) 1997-2021 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 head
->current
= head
->first
;
684 head
->indx
= head
->current
? head
->current
->indx
: 0;
688 /* Convert bitmap HEAD from linked-list view to splay-tree view.
689 This is simply a matter of dropping the prev or next pointers
690 and setting the tree_form flag. The tree will balance itself
691 if and when it is used. */
694 bitmap_tree_view (bitmap head
)
698 gcc_assert (! head
->tree_form
);
707 head
->tree_form
= true;
710 /* Clear a bitmap by freeing all its elements. */
713 bitmap_clear (bitmap head
)
715 if (head
->first
== NULL
)
719 bitmap_element
*e
, *t
;
720 for (e
= head
->first
; e
->prev
; e
= e
->prev
)
721 /* Loop to find the element with the smallest index. */ ;
722 t
= bitmap_tree_splay (head
, head
->first
, e
->indx
);
723 gcc_checking_assert (t
== e
);
726 bitmap_elt_clear_from (head
, head
->first
);
729 /* Initialize a bitmap obstack. If BIT_OBSTACK is NULL, initialize
730 the default bitmap obstack. */
733 bitmap_obstack_initialize (bitmap_obstack
*bit_obstack
)
737 if (bitmap_default_obstack_depth
++)
739 bit_obstack
= &bitmap_default_obstack
;
742 #if !defined(__GNUC__) || (__GNUC__ < 2)
743 #define __alignof__(type) 0
746 bit_obstack
->elements
= NULL
;
747 bit_obstack
->heads
= NULL
;
748 obstack_specify_allocation (&bit_obstack
->obstack
, OBSTACK_CHUNK_SIZE
,
749 __alignof__ (bitmap_element
),
754 /* Release the memory from a bitmap obstack. If BIT_OBSTACK is NULL,
755 release the default bitmap obstack. */
758 bitmap_obstack_release (bitmap_obstack
*bit_obstack
)
762 if (--bitmap_default_obstack_depth
)
764 gcc_assert (bitmap_default_obstack_depth
> 0);
767 bit_obstack
= &bitmap_default_obstack
;
770 bit_obstack
->elements
= NULL
;
771 bit_obstack
->heads
= NULL
;
772 obstack_free (&bit_obstack
->obstack
, NULL
);
775 /* Create a new bitmap on an obstack. If BIT_OBSTACK is NULL, create
776 it on the default bitmap obstack. */
779 bitmap_alloc (bitmap_obstack
*bit_obstack MEM_STAT_DECL
)
784 bit_obstack
= &bitmap_default_obstack
;
785 map
= bit_obstack
->heads
;
787 bit_obstack
->heads
= (class bitmap_head
*) map
->first
;
789 map
= XOBNEW (&bit_obstack
->obstack
, bitmap_head
);
790 bitmap_initialize (map
, bit_obstack PASS_MEM_STAT
);
792 if (GATHER_STATISTICS
)
793 register_overhead (map
, sizeof (bitmap_head
));
798 /* Create a new GCd bitmap. */
801 bitmap_gc_alloc (ALONE_MEM_STAT_DECL
)
805 map
= ggc_alloc
<bitmap_head
> ();
806 bitmap_initialize (map
, NULL PASS_MEM_STAT
);
808 if (GATHER_STATISTICS
)
809 register_overhead (map
, sizeof (bitmap_head
));
814 /* Release an obstack allocated bitmap. */
817 bitmap_obstack_free (bitmap map
)
822 map
->first
= (bitmap_element
*) map
->obstack
->heads
;
824 if (GATHER_STATISTICS
)
825 release_overhead (map
, sizeof (bitmap_head
), true);
827 map
->obstack
->heads
= map
;
832 /* Return nonzero if all bits in an element are zero. */
835 bitmap_element_zerop (const bitmap_element
*element
)
837 #if BITMAP_ELEMENT_WORDS == 2
838 return (element
->bits
[0] | element
->bits
[1]) == 0;
842 for (i
= 0; i
< BITMAP_ELEMENT_WORDS
; i
++)
843 if (element
->bits
[i
] != 0)
850 /* Copy a bitmap to another bitmap. */
853 bitmap_copy (bitmap to
, const_bitmap from
)
855 const bitmap_element
*from_ptr
;
856 bitmap_element
*to_ptr
= 0;
858 gcc_checking_assert (!to
->tree_form
&& !from
->tree_form
);
862 /* Copy elements in forward direction one at a time. */
863 for (from_ptr
= from
->first
; from_ptr
; from_ptr
= from_ptr
->next
)
865 bitmap_element
*to_elt
= bitmap_element_allocate (to
);
867 to_elt
->indx
= from_ptr
->indx
;
868 memcpy (to_elt
->bits
, from_ptr
->bits
, sizeof (to_elt
->bits
));
870 /* Here we have a special case of bitmap_list_link_element,
871 for the case where we know the links are being entered
875 to
->first
= to
->current
= to_elt
;
876 to
->indx
= from_ptr
->indx
;
877 to_elt
->next
= to_elt
->prev
= 0;
881 to_elt
->prev
= to_ptr
;
883 to_ptr
->next
= to_elt
;
890 /* Move a bitmap to another bitmap. */
893 bitmap_move (bitmap to
, bitmap from
)
895 gcc_assert (to
->obstack
== from
->obstack
);
900 if (GATHER_STATISTICS
)
902 for (bitmap_element
*e
= to
->first
; e
; e
= e
->next
)
903 sz
+= sizeof (bitmap_element
);
904 register_overhead (to
, sz
);
909 if (GATHER_STATISTICS
)
910 release_overhead (from
, sz
, false);
913 /* Clear a single bit in a bitmap. Return true if the bit changed. */
916 bitmap_clear_bit (bitmap head
, int bit
)
918 unsigned int indx
= bit
/ BITMAP_ELEMENT_ALL_BITS
;
921 if (!head
->tree_form
)
922 ptr
= bitmap_list_find_element (head
, indx
);
924 ptr
= bitmap_tree_find_element (head
, indx
);
927 unsigned bit_num
= bit
% BITMAP_WORD_BITS
;
928 unsigned word_num
= bit
/ BITMAP_WORD_BITS
% BITMAP_ELEMENT_WORDS
;
929 BITMAP_WORD bit_val
= ((BITMAP_WORD
) 1) << bit_num
;
930 bool res
= (ptr
->bits
[word_num
] & bit_val
) != 0;
933 ptr
->bits
[word_num
] &= ~bit_val
;
934 /* If we cleared the entire word, free up the element. */
935 if (!ptr
->bits
[word_num
]
936 && bitmap_element_zerop (ptr
))
938 if (!head
->tree_form
)
939 bitmap_list_unlink_element (head
, ptr
);
941 bitmap_tree_unlink_element (head
, ptr
);
951 /* Set a single bit in a bitmap. Return true if the bit changed. */
954 bitmap_set_bit (bitmap head
, int bit
)
956 unsigned indx
= bit
/ BITMAP_ELEMENT_ALL_BITS
;
958 if (!head
->tree_form
)
959 ptr
= bitmap_list_find_element (head
, indx
);
961 ptr
= bitmap_tree_find_element (head
, indx
);
962 unsigned word_num
= bit
/ BITMAP_WORD_BITS
% BITMAP_ELEMENT_WORDS
;
963 unsigned bit_num
= bit
% BITMAP_WORD_BITS
;
964 BITMAP_WORD bit_val
= ((BITMAP_WORD
) 1) << bit_num
;
968 bool res
= (ptr
->bits
[word_num
] & bit_val
) == 0;
970 ptr
->bits
[word_num
] |= bit_val
;
974 ptr
= bitmap_element_allocate (head
);
975 ptr
->indx
= bit
/ BITMAP_ELEMENT_ALL_BITS
;
976 ptr
->bits
[word_num
] = bit_val
;
977 if (!head
->tree_form
)
978 bitmap_list_link_element (head
, ptr
);
980 bitmap_tree_link_element (head
, ptr
);
984 /* Return whether a bit is set within a bitmap. */
987 bitmap_bit_p (const_bitmap head
, int bit
)
989 unsigned int indx
= bit
/ BITMAP_ELEMENT_ALL_BITS
;
990 const bitmap_element
*ptr
;
994 if (!head
->tree_form
)
995 ptr
= bitmap_list_find_element (const_cast<bitmap
> (head
), indx
);
997 ptr
= bitmap_tree_find_element (const_cast<bitmap
> (head
), indx
);
1001 bit_num
= bit
% BITMAP_WORD_BITS
;
1002 word_num
= bit
/ BITMAP_WORD_BITS
% BITMAP_ELEMENT_WORDS
;
1004 return (ptr
->bits
[word_num
] >> bit_num
) & 1;
1007 /* Set CHUNK_SIZE bits at a time in bitmap HEAD.
1008 Store CHUNK_VALUE starting at bits CHUNK * chunk_size.
1009 This is the set routine for viewing bitmap as a multi-bit sparse array. */
1012 bitmap_set_aligned_chunk (bitmap head
, unsigned int chunk
,
1013 unsigned int chunk_size
, BITMAP_WORD chunk_value
)
1015 // Ensure chunk size is a power of 2 and fits in BITMAP_WORD.
1016 gcc_checking_assert (pow2p_hwi (chunk_size
));
1017 gcc_checking_assert (chunk_size
< (sizeof (BITMAP_WORD
) * CHAR_BIT
));
1019 // Ensure chunk_value is within range of chunk_size bits.
1020 BITMAP_WORD max_value
= (1 << chunk_size
) - 1;
1021 gcc_checking_assert (chunk_value
<= max_value
);
1023 unsigned bit
= chunk
* chunk_size
;
1024 unsigned indx
= bit
/ BITMAP_ELEMENT_ALL_BITS
;
1025 bitmap_element
*ptr
;
1026 if (!head
->tree_form
)
1027 ptr
= bitmap_list_find_element (head
, indx
);
1029 ptr
= bitmap_tree_find_element (head
, indx
);
1030 unsigned word_num
= bit
/ BITMAP_WORD_BITS
% BITMAP_ELEMENT_WORDS
;
1031 unsigned bit_num
= bit
% BITMAP_WORD_BITS
;
1032 BITMAP_WORD bit_val
= chunk_value
<< bit_num
;
1033 BITMAP_WORD mask
= ~(max_value
<< bit_num
);
1037 ptr
->bits
[word_num
] &= mask
;
1038 ptr
->bits
[word_num
] |= bit_val
;
1042 ptr
= bitmap_element_allocate (head
);
1043 ptr
->indx
= bit
/ BITMAP_ELEMENT_ALL_BITS
;
1044 ptr
->bits
[word_num
] = bit_val
;
1045 if (!head
->tree_form
)
1046 bitmap_list_link_element (head
, ptr
);
1048 bitmap_tree_link_element (head
, ptr
);
1051 /* This is the get routine for viewing bitmap as a multi-bit sparse array.
1052 Return a set of CHUNK_SIZE consecutive bits from HEAD, starting at bit
1053 CHUNK * chunk_size. */
1056 bitmap_get_aligned_chunk (const_bitmap head
, unsigned int chunk
,
1057 unsigned int chunk_size
)
1059 // Ensure chunk size is a power of 2, fits in BITMAP_WORD and is in range.
1060 gcc_checking_assert (pow2p_hwi (chunk_size
));
1061 gcc_checking_assert (chunk_size
< (sizeof (BITMAP_WORD
) * CHAR_BIT
));
1063 BITMAP_WORD max_value
= (1 << chunk_size
) - 1;
1064 unsigned bit
= chunk
* chunk_size
;
1065 unsigned int indx
= bit
/ BITMAP_ELEMENT_ALL_BITS
;
1066 const bitmap_element
*ptr
;
1070 if (!head
->tree_form
)
1071 ptr
= bitmap_list_find_element (const_cast<bitmap
> (head
), indx
);
1073 ptr
= bitmap_tree_find_element (const_cast<bitmap
> (head
), indx
);
1077 bit_num
= bit
% BITMAP_WORD_BITS
;
1078 word_num
= bit
/ BITMAP_WORD_BITS
% BITMAP_ELEMENT_WORDS
;
1081 return (ptr
->bits
[word_num
] >> bit_num
) & max_value
;
1084 #if GCC_VERSION < 3400
1085 /* Table of number of set bits in a character, indexed by value of char. */
1086 static const unsigned char popcount_table
[] =
1088 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,
1089 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,
1090 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,
1091 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,
1092 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,
1093 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,
1094 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,
1095 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,
1098 static unsigned long
1099 bitmap_popcount (BITMAP_WORD a
)
1101 unsigned long ret
= 0;
1104 /* Just do this the table way for now */
1105 for (i
= 0; i
< BITMAP_WORD_BITS
; i
+= 8)
1106 ret
+= popcount_table
[(a
>> i
) & 0xff];
1111 /* Count and return the number of bits set in the bitmap word BITS. */
1112 static unsigned long
1113 bitmap_count_bits_in_word (const BITMAP_WORD
*bits
)
1115 unsigned long count
= 0;
1117 for (unsigned ix
= 0; ix
!= BITMAP_ELEMENT_WORDS
; ix
++)
1119 #if GCC_VERSION >= 3400
1120 /* Note that popcountl matches BITMAP_WORD in type, so the actual size
1121 of BITMAP_WORD is not material. */
1122 count
+= __builtin_popcountl (bits
[ix
]);
1124 count
+= bitmap_popcount (bits
[ix
]);
1130 /* Count the number of bits set in the bitmap, and return it. */
1133 bitmap_count_bits (const_bitmap a
)
1135 unsigned long count
= 0;
1136 const bitmap_element
*elt
;
1138 gcc_checking_assert (!a
->tree_form
);
1139 for (elt
= a
->first
; elt
; elt
= elt
->next
)
1140 count
+= bitmap_count_bits_in_word (elt
->bits
);
1145 /* Count the number of unique bits set in A and B and return it. */
1148 bitmap_count_unique_bits (const_bitmap a
, const_bitmap b
)
1150 unsigned long count
= 0;
1151 const bitmap_element
*elt_a
, *elt_b
;
1153 for (elt_a
= a
->first
, elt_b
= b
->first
; elt_a
&& elt_b
; )
1155 /* If we're at different indices, then count all the bits
1156 in the lower element. If we're at the same index, then
1157 count the bits in the IOR of the two elements. */
1158 if (elt_a
->indx
< elt_b
->indx
)
1160 count
+= bitmap_count_bits_in_word (elt_a
->bits
);
1161 elt_a
= elt_a
->next
;
1163 else if (elt_b
->indx
< elt_a
->indx
)
1165 count
+= bitmap_count_bits_in_word (elt_b
->bits
);
1166 elt_b
= elt_b
->next
;
1170 BITMAP_WORD bits
[BITMAP_ELEMENT_WORDS
];
1171 for (unsigned ix
= 0; ix
!= BITMAP_ELEMENT_WORDS
; ix
++)
1172 bits
[ix
] = elt_a
->bits
[ix
] | elt_b
->bits
[ix
];
1173 count
+= bitmap_count_bits_in_word (bits
);
1174 elt_a
= elt_a
->next
;
1175 elt_b
= elt_b
->next
;
1181 /* Return true if the bitmap has a single bit set. Otherwise return
1185 bitmap_single_bit_set_p (const_bitmap a
)
1187 unsigned long count
= 0;
1188 const bitmap_element
*elt
;
1191 if (bitmap_empty_p (a
))
1196 /* As there are no completely empty bitmap elements, a second one
1197 means we have more than one bit set. */
1198 if (elt
->next
!= NULL
1199 && (!a
->tree_form
|| elt
->prev
!= NULL
))
1202 for (ix
= 0; ix
!= BITMAP_ELEMENT_WORDS
; ix
++)
1204 #if GCC_VERSION >= 3400
1205 /* Note that popcountl matches BITMAP_WORD in type, so the actual size
1206 of BITMAP_WORD is not material. */
1207 count
+= __builtin_popcountl (elt
->bits
[ix
]);
1209 count
+= bitmap_popcount (elt
->bits
[ix
]);
1219 /* Return the bit number of the first set bit in the bitmap. The
1220 bitmap must be non-empty. */
1223 bitmap_first_set_bit (const_bitmap a
)
1225 const bitmap_element
*elt
= a
->first
;
1230 gcc_checking_assert (elt
);
1236 bit_no
= elt
->indx
* BITMAP_ELEMENT_ALL_BITS
;
1237 for (ix
= 0; ix
!= BITMAP_ELEMENT_WORDS
; ix
++)
1239 word
= elt
->bits
[ix
];
1245 bit_no
+= ix
* BITMAP_WORD_BITS
;
1247 #if GCC_VERSION >= 3004
1248 gcc_assert (sizeof (long) == sizeof (word
));
1249 bit_no
+= __builtin_ctzl (word
);
1251 /* Binary search for the first set bit. */
1252 #if BITMAP_WORD_BITS > 64
1253 #error "Fill out the table."
1255 #if BITMAP_WORD_BITS > 32
1256 if (!(word
& 0xffffffff))
1257 word
>>= 32, bit_no
+= 32;
1259 if (!(word
& 0xffff))
1260 word
>>= 16, bit_no
+= 16;
1262 word
>>= 8, bit_no
+= 8;
1264 word
>>= 4, bit_no
+= 4;
1266 word
>>= 2, bit_no
+= 2;
1268 word
>>= 1, bit_no
+= 1;
1270 gcc_checking_assert (word
& 1);
1275 /* Return the bit number of the first set bit in the bitmap. The
1276 bitmap must be non-empty. */
1279 bitmap_last_set_bit (const_bitmap a
)
1281 const bitmap_element
*elt
;
1289 elt
= a
->current
? a
->current
: a
->first
;
1290 gcc_checking_assert (elt
);
1295 bit_no
= elt
->indx
* BITMAP_ELEMENT_ALL_BITS
;
1296 for (ix
= BITMAP_ELEMENT_WORDS
- 1; ix
>= 1; ix
--)
1298 word
= elt
->bits
[ix
];
1302 gcc_assert (elt
->bits
[ix
] != 0);
1304 bit_no
+= ix
* BITMAP_WORD_BITS
;
1305 #if GCC_VERSION >= 3004
1306 gcc_assert (sizeof (long) == sizeof (word
));
1307 bit_no
+= BITMAP_WORD_BITS
- __builtin_clzl (word
) - 1;
1309 /* Hopefully this is a twos-complement host... */
1310 BITMAP_WORD x
= word
;
1316 #if BITMAP_WORD_BITS > 32
1319 bit_no
+= bitmap_popcount (x
) - 1;
1329 bitmap_and (bitmap dst
, const_bitmap a
, const_bitmap b
)
1331 bitmap_element
*dst_elt
= dst
->first
;
1332 const bitmap_element
*a_elt
= a
->first
;
1333 const bitmap_element
*b_elt
= b
->first
;
1334 bitmap_element
*dst_prev
= NULL
;
1336 gcc_checking_assert (!dst
->tree_form
&& !a
->tree_form
&& !b
->tree_form
);
1337 gcc_assert (dst
!= a
&& dst
!= b
);
1341 bitmap_copy (dst
, a
);
1345 while (a_elt
&& b_elt
)
1347 if (a_elt
->indx
< b_elt
->indx
)
1348 a_elt
= a_elt
->next
;
1349 else if (b_elt
->indx
< a_elt
->indx
)
1350 b_elt
= b_elt
->next
;
1353 /* Matching elts, generate A & B. */
1355 BITMAP_WORD ior
= 0;
1358 dst_elt
= bitmap_list_insert_element_after (dst
, dst_prev
,
1361 dst_elt
->indx
= a_elt
->indx
;
1362 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1364 BITMAP_WORD r
= a_elt
->bits
[ix
] & b_elt
->bits
[ix
];
1366 dst_elt
->bits
[ix
] = r
;
1372 dst_elt
= dst_elt
->next
;
1374 a_elt
= a_elt
->next
;
1375 b_elt
= b_elt
->next
;
1378 /* Ensure that dst->current is valid. */
1379 dst
->current
= dst
->first
;
1380 bitmap_elt_clear_from (dst
, dst_elt
);
1381 gcc_checking_assert (!dst
->current
== !dst
->first
);
1383 dst
->indx
= dst
->current
->indx
;
1386 /* A &= B. Return true if A changed. */
1389 bitmap_and_into (bitmap a
, const_bitmap b
)
1391 bitmap_element
*a_elt
= a
->first
;
1392 const bitmap_element
*b_elt
= b
->first
;
1393 bitmap_element
*next
;
1394 bool changed
= false;
1396 gcc_checking_assert (!a
->tree_form
&& !b
->tree_form
);
1401 while (a_elt
&& b_elt
)
1403 if (a_elt
->indx
< b_elt
->indx
)
1406 bitmap_list_unlink_element (a
, a_elt
);
1410 else if (b_elt
->indx
< a_elt
->indx
)
1411 b_elt
= b_elt
->next
;
1414 /* Matching elts, generate A &= B. */
1416 BITMAP_WORD ior
= 0;
1418 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1420 BITMAP_WORD r
= a_elt
->bits
[ix
] & b_elt
->bits
[ix
];
1421 if (a_elt
->bits
[ix
] != r
)
1423 a_elt
->bits
[ix
] = r
;
1428 bitmap_list_unlink_element (a
, a_elt
);
1430 b_elt
= b_elt
->next
;
1437 bitmap_elt_clear_from (a
, a_elt
);
1440 gcc_checking_assert (!a
->current
== !a
->first
1441 && (!a
->current
|| a
->indx
== a
->current
->indx
));
1447 /* Insert an element equal to SRC_ELT after DST_PREV, overwriting DST_ELT
1448 if non-NULL. CHANGED is true if the destination bitmap had already been
1449 changed; the new value of CHANGED is returned. */
1452 bitmap_elt_copy (bitmap dst
, bitmap_element
*dst_elt
, bitmap_element
*dst_prev
,
1453 const bitmap_element
*src_elt
, bool changed
)
1455 if (!changed
&& dst_elt
&& dst_elt
->indx
== src_elt
->indx
)
1459 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1460 if (src_elt
->bits
[ix
] != dst_elt
->bits
[ix
])
1462 dst_elt
->bits
[ix
] = src_elt
->bits
[ix
];
1470 dst_elt
= bitmap_list_insert_element_after (dst
, dst_prev
,
1473 dst_elt
->indx
= src_elt
->indx
;
1474 memcpy (dst_elt
->bits
, src_elt
->bits
, sizeof (dst_elt
->bits
));
1484 bitmap_and_compl (bitmap dst
, const_bitmap a
, const_bitmap b
)
1486 bitmap_element
*dst_elt
= dst
->first
;
1487 const bitmap_element
*a_elt
= a
->first
;
1488 const bitmap_element
*b_elt
= b
->first
;
1489 bitmap_element
*dst_prev
= NULL
;
1490 bitmap_element
**dst_prev_pnext
= &dst
->first
;
1491 bool changed
= false;
1493 gcc_checking_assert (!dst
->tree_form
&& !a
->tree_form
&& !b
->tree_form
);
1494 gcc_assert (dst
!= a
&& dst
!= b
);
1498 changed
= !bitmap_empty_p (dst
);
1505 while (b_elt
&& b_elt
->indx
< a_elt
->indx
)
1506 b_elt
= b_elt
->next
;
1508 if (!b_elt
|| b_elt
->indx
> a_elt
->indx
)
1510 changed
= bitmap_elt_copy (dst
, dst_elt
, dst_prev
, a_elt
, changed
);
1511 dst_prev
= *dst_prev_pnext
;
1512 dst_prev_pnext
= &dst_prev
->next
;
1513 dst_elt
= *dst_prev_pnext
;
1514 a_elt
= a_elt
->next
;
1519 /* Matching elts, generate A & ~B. */
1521 BITMAP_WORD ior
= 0;
1523 if (!changed
&& dst_elt
&& dst_elt
->indx
== a_elt
->indx
)
1525 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1527 BITMAP_WORD r
= a_elt
->bits
[ix
] & ~b_elt
->bits
[ix
];
1529 if (dst_elt
->bits
[ix
] != r
)
1532 dst_elt
->bits
[ix
] = r
;
1540 if (!dst_elt
|| dst_elt
->indx
> a_elt
->indx
)
1542 dst_elt
= bitmap_list_insert_element_after (dst
, dst_prev
,
1548 dst_elt
->indx
= a_elt
->indx
;
1549 new_element
= false;
1552 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1554 BITMAP_WORD r
= a_elt
->bits
[ix
] & ~b_elt
->bits
[ix
];
1556 dst_elt
->bits
[ix
] = r
;
1564 changed
|= !new_element
;
1565 bitmap_list_unlink_element (dst
, dst_elt
);
1566 dst_elt
= *dst_prev_pnext
;
1572 dst_prev
= *dst_prev_pnext
;
1573 dst_prev_pnext
= &dst_prev
->next
;
1574 dst_elt
= *dst_prev_pnext
;
1576 a_elt
= a_elt
->next
;
1577 b_elt
= b_elt
->next
;
1581 /* Ensure that dst->current is valid. */
1582 dst
->current
= dst
->first
;
1587 bitmap_elt_clear_from (dst
, dst_elt
);
1589 gcc_checking_assert (!dst
->current
== !dst
->first
);
1591 dst
->indx
= dst
->current
->indx
;
1596 /* A &= ~B. Returns true if A changes */
1599 bitmap_and_compl_into (bitmap a
, const_bitmap b
)
1601 bitmap_element
*a_elt
= a
->first
;
1602 const bitmap_element
*b_elt
= b
->first
;
1603 bitmap_element
*next
;
1604 BITMAP_WORD changed
= 0;
1606 gcc_checking_assert (!a
->tree_form
&& !b
->tree_form
);
1610 if (bitmap_empty_p (a
))
1619 while (a_elt
&& b_elt
)
1621 if (a_elt
->indx
< b_elt
->indx
)
1622 a_elt
= a_elt
->next
;
1623 else if (b_elt
->indx
< a_elt
->indx
)
1624 b_elt
= b_elt
->next
;
1627 /* Matching elts, generate A &= ~B. */
1629 BITMAP_WORD ior
= 0;
1631 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1633 BITMAP_WORD cleared
= a_elt
->bits
[ix
] & b_elt
->bits
[ix
];
1634 BITMAP_WORD r
= a_elt
->bits
[ix
] ^ cleared
;
1636 a_elt
->bits
[ix
] = r
;
1642 bitmap_list_unlink_element (a
, a_elt
);
1644 b_elt
= b_elt
->next
;
1647 gcc_checking_assert (!a
->current
== !a
->first
1648 && (!a
->current
|| a
->indx
== a
->current
->indx
));
1649 return changed
!= 0;
1652 /* Set COUNT bits from START in HEAD. */
1654 bitmap_set_range (bitmap head
, unsigned int start
, unsigned int count
)
1656 unsigned int first_index
, end_bit_plus1
, last_index
;
1657 bitmap_element
*elt
, *elt_prev
;
1660 gcc_checking_assert (!head
->tree_form
);
1667 bitmap_set_bit (head
, start
);
1671 first_index
= start
/ BITMAP_ELEMENT_ALL_BITS
;
1672 end_bit_plus1
= start
+ count
;
1673 last_index
= (end_bit_plus1
- 1) / BITMAP_ELEMENT_ALL_BITS
;
1674 elt
= bitmap_list_find_element (head
, first_index
);
1676 /* If bitmap_list_find_element returns zero, the current is the closest block
1677 to the result. Otherwise, just use bitmap_element_allocate to
1678 ensure ELT is set; in the loop below, ELT == NULL means "insert
1679 at the end of the bitmap". */
1682 elt
= bitmap_element_allocate (head
);
1683 elt
->indx
= first_index
;
1684 bitmap_list_link_element (head
, elt
);
1687 gcc_checking_assert (elt
->indx
== first_index
);
1688 elt_prev
= elt
->prev
;
1689 for (i
= first_index
; i
<= last_index
; i
++)
1691 unsigned elt_start_bit
= i
* BITMAP_ELEMENT_ALL_BITS
;
1692 unsigned elt_end_bit_plus1
= elt_start_bit
+ BITMAP_ELEMENT_ALL_BITS
;
1694 unsigned int first_word_to_mod
;
1695 BITMAP_WORD first_mask
;
1696 unsigned int last_word_to_mod
;
1697 BITMAP_WORD last_mask
;
1700 if (!elt
|| elt
->indx
!= i
)
1701 elt
= bitmap_list_insert_element_after (head
, elt_prev
, i
);
1703 if (elt_start_bit
<= start
)
1705 /* The first bit to turn on is somewhere inside this
1707 first_word_to_mod
= (start
- elt_start_bit
) / BITMAP_WORD_BITS
;
1709 /* This mask should have 1s in all bits >= start position. */
1711 (((BITMAP_WORD
) 1) << ((start
% BITMAP_WORD_BITS
))) - 1;
1712 first_mask
= ~first_mask
;
1716 /* The first bit to turn on is below this start of this elt. */
1717 first_word_to_mod
= 0;
1718 first_mask
= ~(BITMAP_WORD
) 0;
1721 if (elt_end_bit_plus1
<= end_bit_plus1
)
1723 /* The last bit to turn on is beyond this elt. */
1724 last_word_to_mod
= BITMAP_ELEMENT_WORDS
- 1;
1725 last_mask
= ~(BITMAP_WORD
) 0;
1729 /* The last bit to turn on is inside to this elt. */
1731 (end_bit_plus1
- elt_start_bit
) / BITMAP_WORD_BITS
;
1733 /* The last mask should have 1s below the end bit. */
1735 (((BITMAP_WORD
) 1) << ((end_bit_plus1
% BITMAP_WORD_BITS
))) - 1;
1738 if (first_word_to_mod
== last_word_to_mod
)
1740 BITMAP_WORD mask
= first_mask
& last_mask
;
1741 elt
->bits
[first_word_to_mod
] |= mask
;
1745 elt
->bits
[first_word_to_mod
] |= first_mask
;
1746 if (BITMAP_ELEMENT_WORDS
> 2)
1747 for (ix
= first_word_to_mod
+ 1; ix
< last_word_to_mod
; ix
++)
1748 elt
->bits
[ix
] = ~(BITMAP_WORD
) 0;
1749 elt
->bits
[last_word_to_mod
] |= last_mask
;
1756 head
->current
= elt
? elt
: elt_prev
;
1757 head
->indx
= head
->current
->indx
;
1760 /* Clear COUNT bits from START in HEAD. */
1762 bitmap_clear_range (bitmap head
, unsigned int start
, unsigned int count
)
1764 unsigned int first_index
, end_bit_plus1
, last_index
;
1765 bitmap_element
*elt
;
1767 gcc_checking_assert (!head
->tree_form
);
1774 bitmap_clear_bit (head
, start
);
1778 first_index
= start
/ BITMAP_ELEMENT_ALL_BITS
;
1779 end_bit_plus1
= start
+ count
;
1780 last_index
= (end_bit_plus1
- 1) / BITMAP_ELEMENT_ALL_BITS
;
1781 elt
= bitmap_list_find_element (head
, first_index
);
1783 /* If bitmap_list_find_element returns zero, the current is the closest block
1784 to the result. If the current is less than first index, find the
1785 next one. Otherwise, just set elt to be current. */
1790 if (head
->indx
< first_index
)
1792 elt
= head
->current
->next
;
1797 elt
= head
->current
;
1803 while (elt
&& (elt
->indx
<= last_index
))
1805 bitmap_element
* next_elt
= elt
->next
;
1806 unsigned elt_start_bit
= (elt
->indx
) * BITMAP_ELEMENT_ALL_BITS
;
1807 unsigned elt_end_bit_plus1
= elt_start_bit
+ BITMAP_ELEMENT_ALL_BITS
;
1810 if (elt_start_bit
>= start
&& elt_end_bit_plus1
<= end_bit_plus1
)
1811 /* Get rid of the entire elt and go to the next one. */
1812 bitmap_list_unlink_element (head
, elt
);
1815 /* Going to have to knock out some bits in this elt. */
1816 unsigned int first_word_to_mod
;
1817 BITMAP_WORD first_mask
;
1818 unsigned int last_word_to_mod
;
1819 BITMAP_WORD last_mask
;
1823 if (elt_start_bit
<= start
)
1825 /* The first bit to turn off is somewhere inside this
1827 first_word_to_mod
= (start
- elt_start_bit
) / BITMAP_WORD_BITS
;
1829 /* This mask should have 1s in all bits >= start position. */
1831 (((BITMAP_WORD
) 1) << ((start
% BITMAP_WORD_BITS
))) - 1;
1832 first_mask
= ~first_mask
;
1836 /* The first bit to turn off is below this start of this elt. */
1837 first_word_to_mod
= 0;
1839 first_mask
= ~first_mask
;
1842 if (elt_end_bit_plus1
<= end_bit_plus1
)
1844 /* The last bit to turn off is beyond this elt. */
1845 last_word_to_mod
= BITMAP_ELEMENT_WORDS
- 1;
1847 last_mask
= ~last_mask
;
1851 /* The last bit to turn off is inside to this elt. */
1853 (end_bit_plus1
- elt_start_bit
) / BITMAP_WORD_BITS
;
1855 /* The last mask should have 1s below the end bit. */
1857 (((BITMAP_WORD
) 1) << (((end_bit_plus1
) % BITMAP_WORD_BITS
))) - 1;
1861 if (first_word_to_mod
== last_word_to_mod
)
1863 BITMAP_WORD mask
= first_mask
& last_mask
;
1864 elt
->bits
[first_word_to_mod
] &= ~mask
;
1868 elt
->bits
[first_word_to_mod
] &= ~first_mask
;
1869 if (BITMAP_ELEMENT_WORDS
> 2)
1870 for (i
= first_word_to_mod
+ 1; i
< last_word_to_mod
; i
++)
1872 elt
->bits
[last_word_to_mod
] &= ~last_mask
;
1874 for (i
= 0; i
< BITMAP_ELEMENT_WORDS
; i
++)
1880 /* Check to see if there are any bits left. */
1882 bitmap_list_unlink_element (head
, elt
);
1889 head
->current
= elt
;
1890 head
->indx
= head
->current
->indx
;
1897 bitmap_compl_and_into (bitmap a
, const_bitmap b
)
1899 bitmap_element
*a_elt
= a
->first
;
1900 const bitmap_element
*b_elt
= b
->first
;
1901 bitmap_element
*a_prev
= NULL
;
1902 bitmap_element
*next
;
1904 gcc_checking_assert (!a
->tree_form
&& !b
->tree_form
);
1905 gcc_assert (a
!= b
);
1907 if (bitmap_empty_p (a
))
1912 if (bitmap_empty_p (b
))
1918 while (a_elt
|| b_elt
)
1920 if (!b_elt
|| (a_elt
&& a_elt
->indx
< b_elt
->indx
))
1922 /* A is before B. Remove A */
1924 a_prev
= a_elt
->prev
;
1925 bitmap_list_unlink_element (a
, a_elt
);
1928 else if (!a_elt
|| b_elt
->indx
< a_elt
->indx
)
1930 /* B is before A. Copy B. */
1931 next
= bitmap_list_insert_element_after (a
, a_prev
, b_elt
->indx
);
1932 memcpy (next
->bits
, b_elt
->bits
, sizeof (next
->bits
));
1934 b_elt
= b_elt
->next
;
1938 /* Matching elts, generate A = ~A & B. */
1940 BITMAP_WORD ior
= 0;
1942 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1944 BITMAP_WORD cleared
= a_elt
->bits
[ix
] & b_elt
->bits
[ix
];
1945 BITMAP_WORD r
= b_elt
->bits
[ix
] ^ cleared
;
1947 a_elt
->bits
[ix
] = r
;
1952 bitmap_list_unlink_element (a
, a_elt
);
1956 b_elt
= b_elt
->next
;
1959 gcc_checking_assert (!a
->current
== !a
->first
1960 && (!a
->current
|| a
->indx
== a
->current
->indx
));
1965 /* Insert an element corresponding to A_ELT | B_ELT after DST_PREV,
1966 overwriting DST_ELT if non-NULL. CHANGED is true if the destination bitmap
1967 had already been changed; the new value of CHANGED is returned. */
1970 bitmap_elt_ior (bitmap dst
, bitmap_element
*dst_elt
, bitmap_element
*dst_prev
,
1971 const bitmap_element
*a_elt
, const bitmap_element
*b_elt
,
1974 gcc_assert (a_elt
|| b_elt
);
1976 if (a_elt
&& b_elt
&& a_elt
->indx
== b_elt
->indx
)
1978 /* Matching elts, generate A | B. */
1981 if (!changed
&& dst_elt
&& dst_elt
->indx
== a_elt
->indx
)
1983 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1985 BITMAP_WORD r
= a_elt
->bits
[ix
] | b_elt
->bits
[ix
];
1986 if (r
!= dst_elt
->bits
[ix
])
1988 dst_elt
->bits
[ix
] = r
;
1997 dst_elt
= bitmap_list_insert_element_after (dst
, dst_prev
,
2000 dst_elt
->indx
= a_elt
->indx
;
2001 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
2003 BITMAP_WORD r
= a_elt
->bits
[ix
] | b_elt
->bits
[ix
];
2004 dst_elt
->bits
[ix
] = r
;
2010 /* Copy a single element. */
2011 const bitmap_element
*src
;
2013 if (!b_elt
|| (a_elt
&& a_elt
->indx
< b_elt
->indx
))
2018 gcc_checking_assert (src
);
2019 changed
= bitmap_elt_copy (dst
, dst_elt
, dst_prev
, src
, changed
);
2025 /* DST = A | B. Return true if DST changes. */
2028 bitmap_ior (bitmap dst
, const_bitmap a
, const_bitmap b
)
2030 bitmap_element
*dst_elt
= dst
->first
;
2031 const bitmap_element
*a_elt
= a
->first
;
2032 const bitmap_element
*b_elt
= b
->first
;
2033 bitmap_element
*dst_prev
= NULL
;
2034 bitmap_element
**dst_prev_pnext
= &dst
->first
;
2035 bool changed
= false;
2037 gcc_checking_assert (!dst
->tree_form
&& !a
->tree_form
&& !b
->tree_form
);
2038 gcc_assert (dst
!= a
&& dst
!= b
);
2040 while (a_elt
|| b_elt
)
2042 changed
= bitmap_elt_ior (dst
, dst_elt
, dst_prev
, a_elt
, b_elt
, changed
);
2044 if (a_elt
&& b_elt
&& a_elt
->indx
== b_elt
->indx
)
2046 a_elt
= a_elt
->next
;
2047 b_elt
= b_elt
->next
;
2051 if (a_elt
&& (!b_elt
|| a_elt
->indx
<= b_elt
->indx
))
2052 a_elt
= a_elt
->next
;
2053 else if (b_elt
&& (!a_elt
|| b_elt
->indx
<= a_elt
->indx
))
2054 b_elt
= b_elt
->next
;
2057 dst_prev
= *dst_prev_pnext
;
2058 dst_prev_pnext
= &dst_prev
->next
;
2059 dst_elt
= *dst_prev_pnext
;
2065 /* Ensure that dst->current is valid. */
2066 dst
->current
= dst
->first
;
2067 bitmap_elt_clear_from (dst
, dst_elt
);
2069 gcc_checking_assert (!dst
->current
== !dst
->first
);
2071 dst
->indx
= dst
->current
->indx
;
2075 /* A |= B. Return true if A changes. */
2078 bitmap_ior_into (bitmap a
, const_bitmap b
)
2080 bitmap_element
*a_elt
= a
->first
;
2081 const bitmap_element
*b_elt
= b
->first
;
2082 bitmap_element
*a_prev
= NULL
;
2083 bitmap_element
**a_prev_pnext
= &a
->first
;
2084 bool changed
= false;
2086 gcc_checking_assert (!a
->tree_form
&& !b
->tree_form
);
2092 /* If A lags behind B, just advance it. */
2093 if (!a_elt
|| a_elt
->indx
== b_elt
->indx
)
2095 changed
= bitmap_elt_ior (a
, a_elt
, a_prev
, a_elt
, b_elt
, changed
);
2096 b_elt
= b_elt
->next
;
2098 else if (a_elt
->indx
> b_elt
->indx
)
2100 changed
= bitmap_elt_copy (a
, NULL
, a_prev
, b_elt
, changed
);
2101 b_elt
= b_elt
->next
;
2104 a_prev
= *a_prev_pnext
;
2105 a_prev_pnext
= &a_prev
->next
;
2106 a_elt
= *a_prev_pnext
;
2109 gcc_checking_assert (!a
->current
== !a
->first
);
2111 a
->indx
= a
->current
->indx
;
2115 /* A |= B. Return true if A changes. Free B (re-using its storage
2119 bitmap_ior_into_and_free (bitmap a
, bitmap
*b_
)
2122 bitmap_element
*a_elt
= a
->first
;
2123 bitmap_element
*b_elt
= b
->first
;
2124 bitmap_element
*a_prev
= NULL
;
2125 bitmap_element
**a_prev_pnext
= &a
->first
;
2126 bool changed
= false;
2128 gcc_checking_assert (!a
->tree_form
&& !b
->tree_form
);
2129 gcc_assert (a
->obstack
== b
->obstack
);
2135 /* If A lags behind B, just advance it. */
2136 if (!a_elt
|| a_elt
->indx
== b_elt
->indx
)
2138 changed
= bitmap_elt_ior (a
, a_elt
, a_prev
, a_elt
, b_elt
, changed
);
2139 b_elt
= b_elt
->next
;
2141 else if (a_elt
->indx
> b_elt
->indx
)
2143 bitmap_element
*b_elt_next
= b_elt
->next
;
2144 bitmap_list_unlink_element (b
, b_elt
, false);
2145 bitmap_list_insert_element_after (a
, a_prev
, b_elt
->indx
, b_elt
);
2149 a_prev
= *a_prev_pnext
;
2150 a_prev_pnext
= &a_prev
->next
;
2151 a_elt
= *a_prev_pnext
;
2154 gcc_checking_assert (!a
->current
== !a
->first
);
2156 a
->indx
= a
->current
->indx
;
2168 bitmap_xor (bitmap dst
, const_bitmap a
, const_bitmap b
)
2170 bitmap_element
*dst_elt
= dst
->first
;
2171 const bitmap_element
*a_elt
= a
->first
;
2172 const bitmap_element
*b_elt
= b
->first
;
2173 bitmap_element
*dst_prev
= NULL
;
2175 gcc_checking_assert (!dst
->tree_form
&& !a
->tree_form
&& !b
->tree_form
);
2176 gcc_assert (dst
!= a
&& dst
!= b
);
2184 while (a_elt
|| b_elt
)
2186 if (a_elt
&& b_elt
&& a_elt
->indx
== b_elt
->indx
)
2188 /* Matching elts, generate A ^ B. */
2190 BITMAP_WORD ior
= 0;
2193 dst_elt
= bitmap_list_insert_element_after (dst
, dst_prev
,
2196 dst_elt
->indx
= a_elt
->indx
;
2197 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
2199 BITMAP_WORD r
= a_elt
->bits
[ix
] ^ b_elt
->bits
[ix
];
2202 dst_elt
->bits
[ix
] = r
;
2204 a_elt
= a_elt
->next
;
2205 b_elt
= b_elt
->next
;
2209 dst_elt
= dst_elt
->next
;
2214 /* Copy a single element. */
2215 const bitmap_element
*src
;
2217 if (!b_elt
|| (a_elt
&& a_elt
->indx
< b_elt
->indx
))
2220 a_elt
= a_elt
->next
;
2225 b_elt
= b_elt
->next
;
2229 dst_elt
= bitmap_list_insert_element_after (dst
, dst_prev
,
2232 dst_elt
->indx
= src
->indx
;
2233 memcpy (dst_elt
->bits
, src
->bits
, sizeof (dst_elt
->bits
));
2235 dst_elt
= dst_elt
->next
;
2238 /* Ensure that dst->current is valid. */
2239 dst
->current
= dst
->first
;
2240 bitmap_elt_clear_from (dst
, dst_elt
);
2241 gcc_checking_assert (!dst
->current
== !dst
->first
);
2243 dst
->indx
= dst
->current
->indx
;
2249 bitmap_xor_into (bitmap a
, const_bitmap b
)
2251 bitmap_element
*a_elt
= a
->first
;
2252 const bitmap_element
*b_elt
= b
->first
;
2253 bitmap_element
*a_prev
= NULL
;
2255 gcc_checking_assert (!a
->tree_form
&& !b
->tree_form
);
2265 if (!a_elt
|| b_elt
->indx
< a_elt
->indx
)
2268 bitmap_element
*dst
= bitmap_list_insert_element_after (a
, a_prev
,
2270 memcpy (dst
->bits
, b_elt
->bits
, sizeof (dst
->bits
));
2272 b_elt
= b_elt
->next
;
2274 else if (a_elt
->indx
< b_elt
->indx
)
2277 a_elt
= a_elt
->next
;
2281 /* Matching elts, generate A ^= B. */
2283 BITMAP_WORD ior
= 0;
2284 bitmap_element
*next
= a_elt
->next
;
2286 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
2288 BITMAP_WORD r
= a_elt
->bits
[ix
] ^ b_elt
->bits
[ix
];
2291 a_elt
->bits
[ix
] = r
;
2293 b_elt
= b_elt
->next
;
2297 bitmap_list_unlink_element (a
, a_elt
);
2301 gcc_checking_assert (!a
->current
== !a
->first
);
2303 a
->indx
= a
->current
->indx
;
2306 /* Return true if two bitmaps are identical.
2307 We do not bother with a check for pointer equality, as that never
2308 occurs in practice. */
2311 bitmap_equal_p (const_bitmap a
, const_bitmap b
)
2313 const bitmap_element
*a_elt
;
2314 const bitmap_element
*b_elt
;
2317 gcc_checking_assert (!a
->tree_form
&& !b
->tree_form
);
2319 for (a_elt
= a
->first
, b_elt
= b
->first
;
2321 a_elt
= a_elt
->next
, b_elt
= b_elt
->next
)
2323 if (a_elt
->indx
!= b_elt
->indx
)
2325 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
2326 if (a_elt
->bits
[ix
] != b_elt
->bits
[ix
])
2329 return !a_elt
&& !b_elt
;
2332 /* Return true if A AND B is not empty. */
2335 bitmap_intersect_p (const_bitmap a
, const_bitmap b
)
2337 const bitmap_element
*a_elt
;
2338 const bitmap_element
*b_elt
;
2341 gcc_checking_assert (!a
->tree_form
&& !b
->tree_form
);
2343 for (a_elt
= a
->first
, b_elt
= b
->first
;
2346 if (a_elt
->indx
< b_elt
->indx
)
2347 a_elt
= a_elt
->next
;
2348 else if (b_elt
->indx
< a_elt
->indx
)
2349 b_elt
= b_elt
->next
;
2352 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
2353 if (a_elt
->bits
[ix
] & b_elt
->bits
[ix
])
2355 a_elt
= a_elt
->next
;
2356 b_elt
= b_elt
->next
;
2362 /* Return true if A AND NOT B is not empty. */
2365 bitmap_intersect_compl_p (const_bitmap a
, const_bitmap b
)
2367 const bitmap_element
*a_elt
;
2368 const bitmap_element
*b_elt
;
2371 gcc_checking_assert (!a
->tree_form
&& !b
->tree_form
);
2373 for (a_elt
= a
->first
, b_elt
= b
->first
;
2376 if (a_elt
->indx
< b_elt
->indx
)
2378 else if (b_elt
->indx
< a_elt
->indx
)
2379 b_elt
= b_elt
->next
;
2382 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
2383 if (a_elt
->bits
[ix
] & ~b_elt
->bits
[ix
])
2385 a_elt
= a_elt
->next
;
2386 b_elt
= b_elt
->next
;
2389 return a_elt
!= NULL
;
2393 /* DST = A | (FROM1 & ~FROM2). Return true if DST changes. */
2396 bitmap_ior_and_compl (bitmap dst
, const_bitmap a
, const_bitmap b
, const_bitmap kill
)
2398 bool changed
= false;
2400 bitmap_element
*dst_elt
= dst
->first
;
2401 const bitmap_element
*a_elt
= a
->first
;
2402 const bitmap_element
*b_elt
= b
->first
;
2403 const bitmap_element
*kill_elt
= kill
->first
;
2404 bitmap_element
*dst_prev
= NULL
;
2405 bitmap_element
**dst_prev_pnext
= &dst
->first
;
2407 gcc_checking_assert (!dst
->tree_form
&& !a
->tree_form
&& !b
->tree_form
2408 && !kill
->tree_form
);
2409 gcc_assert (dst
!= a
&& dst
!= b
&& dst
!= kill
);
2411 /* Special cases. We don't bother checking for bitmap_equal_p (b, kill). */
2412 if (b
== kill
|| bitmap_empty_p (b
))
2414 changed
= !bitmap_equal_p (dst
, a
);
2416 bitmap_copy (dst
, a
);
2419 if (bitmap_empty_p (kill
))
2420 return bitmap_ior (dst
, a
, b
);
2421 if (bitmap_empty_p (a
))
2422 return bitmap_and_compl (dst
, b
, kill
);
2424 while (a_elt
|| b_elt
)
2426 bool new_element
= false;
2429 while (kill_elt
&& kill_elt
->indx
< b_elt
->indx
)
2430 kill_elt
= kill_elt
->next
;
2432 if (b_elt
&& kill_elt
&& kill_elt
->indx
== b_elt
->indx
2433 && (!a_elt
|| a_elt
->indx
>= b_elt
->indx
))
2435 bitmap_element tmp_elt
;
2438 BITMAP_WORD ior
= 0;
2439 tmp_elt
.indx
= b_elt
->indx
;
2440 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
2442 BITMAP_WORD r
= b_elt
->bits
[ix
] & ~kill_elt
->bits
[ix
];
2444 tmp_elt
.bits
[ix
] = r
;
2449 changed
= bitmap_elt_ior (dst
, dst_elt
, dst_prev
,
2450 a_elt
, &tmp_elt
, changed
);
2452 if (a_elt
&& a_elt
->indx
== b_elt
->indx
)
2453 a_elt
= a_elt
->next
;
2456 b_elt
= b_elt
->next
;
2457 kill_elt
= kill_elt
->next
;
2461 changed
= bitmap_elt_ior (dst
, dst_elt
, dst_prev
,
2462 a_elt
, b_elt
, changed
);
2465 if (a_elt
&& b_elt
&& a_elt
->indx
== b_elt
->indx
)
2467 a_elt
= a_elt
->next
;
2468 b_elt
= b_elt
->next
;
2472 if (a_elt
&& (!b_elt
|| a_elt
->indx
<= b_elt
->indx
))
2473 a_elt
= a_elt
->next
;
2474 else if (b_elt
&& (!a_elt
|| b_elt
->indx
<= a_elt
->indx
))
2475 b_elt
= b_elt
->next
;
2481 dst_prev
= *dst_prev_pnext
;
2482 dst_prev_pnext
= &dst_prev
->next
;
2483 dst_elt
= *dst_prev_pnext
;
2490 /* Ensure that dst->current is valid. */
2491 dst
->current
= dst
->first
;
2492 bitmap_elt_clear_from (dst
, dst_elt
);
2494 gcc_checking_assert (!dst
->current
== !dst
->first
);
2496 dst
->indx
= dst
->current
->indx
;
2501 /* A |= (B & ~C). Return true if A changes. */
2504 bitmap_ior_and_compl_into (bitmap a
, const_bitmap b
, const_bitmap c
)
2506 bitmap_element
*a_elt
= a
->first
;
2507 const bitmap_element
*b_elt
= b
->first
;
2508 const bitmap_element
*c_elt
= c
->first
;
2509 bitmap_element and_elt
;
2510 bitmap_element
*a_prev
= NULL
;
2511 bitmap_element
**a_prev_pnext
= &a
->first
;
2512 bool changed
= false;
2515 gcc_checking_assert (!a
->tree_form
&& !b
->tree_form
&& !c
->tree_form
);
2519 if (bitmap_empty_p (c
))
2520 return bitmap_ior_into (a
, b
);
2521 else if (bitmap_empty_p (a
))
2522 return bitmap_and_compl (a
, b
, c
);
2528 while (c_elt
&& c_elt
->indx
< b_elt
->indx
)
2529 c_elt
= c_elt
->next
;
2531 const bitmap_element
*and_elt_ptr
;
2532 if (c_elt
&& c_elt
->indx
== b_elt
->indx
)
2534 BITMAP_WORD overall
= 0;
2535 and_elt_ptr
= &and_elt
;
2536 and_elt
.indx
= b_elt
->indx
;
2537 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
2539 and_elt
.bits
[ix
] = b_elt
->bits
[ix
] & ~c_elt
->bits
[ix
];
2540 overall
|= and_elt
.bits
[ix
];
2544 b_elt
= b_elt
->next
;
2549 and_elt_ptr
= b_elt
;
2551 b_elt
= b_elt
->next
;
2553 /* Now find a place to insert AND_ELT. */
2556 ix
= a_elt
? a_elt
->indx
: and_elt_ptr
->indx
;
2557 if (ix
== and_elt_ptr
->indx
)
2558 changed
= bitmap_elt_ior (a
, a_elt
, a_prev
, a_elt
,
2559 and_elt_ptr
, changed
);
2560 else if (ix
> and_elt_ptr
->indx
)
2561 changed
= bitmap_elt_copy (a
, NULL
, a_prev
, and_elt_ptr
, changed
);
2563 a_prev
= *a_prev_pnext
;
2564 a_prev_pnext
= &a_prev
->next
;
2565 a_elt
= *a_prev_pnext
;
2567 /* If A lagged behind B/C, we advanced it so loop once more. */
2569 while (ix
< and_elt_ptr
->indx
);
2572 gcc_checking_assert (!a
->current
== !a
->first
);
2574 a
->indx
= a
->current
->indx
;
2578 /* A |= (B & C). Return true if A changes. */
2581 bitmap_ior_and_into (bitmap a
, const_bitmap b
, const_bitmap c
)
2583 bitmap_element
*a_elt
= a
->first
;
2584 const bitmap_element
*b_elt
= b
->first
;
2585 const bitmap_element
*c_elt
= c
->first
;
2586 bitmap_element and_elt
;
2587 bitmap_element
*a_prev
= NULL
;
2588 bitmap_element
**a_prev_pnext
= &a
->first
;
2589 bool changed
= false;
2592 gcc_checking_assert (!a
->tree_form
&& !b
->tree_form
&& !c
->tree_form
);
2595 return bitmap_ior_into (a
, b
);
2596 if (bitmap_empty_p (b
) || bitmap_empty_p (c
))
2600 while (b_elt
&& c_elt
)
2602 BITMAP_WORD overall
;
2604 /* Find a common item of B and C. */
2605 while (b_elt
->indx
!= c_elt
->indx
)
2607 if (b_elt
->indx
< c_elt
->indx
)
2609 b_elt
= b_elt
->next
;
2615 c_elt
= c_elt
->next
;
2622 and_elt
.indx
= b_elt
->indx
;
2623 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
2625 and_elt
.bits
[ix
] = b_elt
->bits
[ix
] & c_elt
->bits
[ix
];
2626 overall
|= and_elt
.bits
[ix
];
2629 b_elt
= b_elt
->next
;
2630 c_elt
= c_elt
->next
;
2634 /* Now find a place to insert AND_ELT. */
2637 ix
= a_elt
? a_elt
->indx
: and_elt
.indx
;
2638 if (ix
== and_elt
.indx
)
2639 changed
= bitmap_elt_ior (a
, a_elt
, a_prev
, a_elt
, &and_elt
, changed
);
2640 else if (ix
> and_elt
.indx
)
2641 changed
= bitmap_elt_copy (a
, NULL
, a_prev
, &and_elt
, changed
);
2643 a_prev
= *a_prev_pnext
;
2644 a_prev_pnext
= &a_prev
->next
;
2645 a_elt
= *a_prev_pnext
;
2647 /* If A lagged behind B/C, we advanced it so loop once more. */
2649 while (ix
< and_elt
.indx
);
2653 gcc_checking_assert (!a
->current
== !a
->first
);
2655 a
->indx
= a
->current
->indx
;
2659 /* Compute hash of bitmap (for purposes of hashing). */
2662 bitmap_hash (const_bitmap head
)
2664 const bitmap_element
*ptr
;
2665 BITMAP_WORD hash
= 0;
2668 gcc_checking_assert (!head
->tree_form
);
2670 for (ptr
= head
->first
; ptr
; ptr
= ptr
->next
)
2673 for (ix
= 0; ix
!= BITMAP_ELEMENT_WORDS
; ix
++)
2674 hash
^= ptr
->bits
[ix
];
2676 return (hashval_t
)hash
;
2680 /* Function to obtain a vector of bitmap elements in bit order from
2681 HEAD in tree view. */
2684 bitmap_tree_to_vec (vec
<bitmap_element
*> &elts
, const_bitmap head
)
2686 gcc_checking_assert (head
->tree_form
);
2687 auto_vec
<bitmap_element
*, 32> stack
;
2688 bitmap_element
*e
= head
->first
;
2693 stack
.safe_push (e
);
2696 if (stack
.is_empty ())
2705 /* Debugging function to print out the contents of a bitmap element. */
2708 debug_bitmap_elt_file (FILE *file
, const bitmap_element
*ptr
)
2710 unsigned int i
, j
, col
= 26;
2712 fprintf (file
, "\t" HOST_PTR_PRINTF
" next = " HOST_PTR_PRINTF
2713 " prev = " HOST_PTR_PRINTF
" indx = %u\n\t\tbits = {",
2714 (const void*) ptr
, (const void*) ptr
->next
,
2715 (const void*) ptr
->prev
, ptr
->indx
);
2717 for (i
= 0; i
< BITMAP_ELEMENT_WORDS
; i
++)
2718 for (j
= 0; j
< BITMAP_WORD_BITS
; j
++)
2719 if ((ptr
->bits
[i
] >> j
) & 1)
2723 fprintf (file
, "\n\t\t\t");
2727 fprintf (file
, " %u", (ptr
->indx
* BITMAP_ELEMENT_ALL_BITS
2728 + i
* BITMAP_WORD_BITS
+ j
));
2732 fprintf (file
, " }\n");
2735 /* Debugging function to print out the contents of a bitmap. */
2738 debug_bitmap_file (FILE *file
, const_bitmap head
)
2740 const bitmap_element
*ptr
;
2742 fprintf (file
, "\nfirst = " HOST_PTR_PRINTF
2743 " current = " HOST_PTR_PRINTF
" indx = %u\n",
2744 (void *) head
->first
, (void *) head
->current
, head
->indx
);
2746 if (head
->tree_form
)
2748 auto_vec
<bitmap_element
*, 32> elts
;
2749 bitmap_tree_to_vec (elts
, head
);
2750 for (unsigned i
= 0; i
< elts
.length (); ++i
)
2751 debug_bitmap_elt_file (file
, elts
[i
]);
2754 for (ptr
= head
->first
; ptr
; ptr
= ptr
->next
)
2755 debug_bitmap_elt_file (file
, ptr
);
2758 /* Function to be called from the debugger to print the contents
2762 debug_bitmap (const_bitmap head
)
2764 debug_bitmap_file (stderr
, head
);
2767 /* Function to print out the contents of a bitmap. Unlike debug_bitmap_file,
2768 it does not print anything but the bits. */
2771 bitmap_print (FILE *file
, const_bitmap head
, const char *prefix
,
2774 const char *comma
= "";
2777 fputs (prefix
, file
);
2778 if (head
->tree_form
)
2780 auto_vec
<bitmap_element
*, 32> elts
;
2781 bitmap_tree_to_vec (elts
, head
);
2782 for (i
= 0; i
< elts
.length (); ++i
)
2783 for (unsigned ix
= 0; ix
!= BITMAP_ELEMENT_WORDS
; ++ix
)
2785 BITMAP_WORD word
= elts
[i
]->bits
[ix
];
2786 for (unsigned bit
= 0; bit
!= BITMAP_WORD_BITS
; ++bit
)
2787 if (word
& ((BITMAP_WORD
)1 << bit
))
2789 fprintf (file
, "%s%d", comma
,
2790 (bit
+ BITMAP_WORD_BITS
* ix
2791 + elts
[i
]->indx
* BITMAP_ELEMENT_ALL_BITS
));
2799 EXECUTE_IF_SET_IN_BITMAP (head
, 0, i
, bi
)
2801 fprintf (file
, "%s%d", comma
, i
);
2805 fputs (suffix
, file
);
2808 /* Output per-bitmap memory usage statistics. */
2810 dump_bitmap_statistics (void)
2812 if (!GATHER_STATISTICS
)
2815 bitmap_mem_desc
.dump (BITMAP_ORIGIN
);
2819 debug (const bitmap_head
&ref
)
2821 dump_bitmap (stderr
, &ref
);
2825 debug (const bitmap_head
*ptr
)
2830 fprintf (stderr
, "<nil>\n");
2834 bitmap_head::dump ()
2841 namespace selftest
{
2843 /* Selftests for bitmaps. */
2845 /* Freshly-created bitmaps ought to be empty. */
2850 bitmap b
= bitmap_gc_alloc ();
2851 ASSERT_TRUE (bitmap_empty_p (b
));
2854 /* Verify bitmap_set_range. */
2859 bitmap b
= bitmap_gc_alloc ();
2860 ASSERT_TRUE (bitmap_empty_p (b
));
2862 bitmap_set_range (b
, 7, 5);
2863 ASSERT_FALSE (bitmap_empty_p (b
));
2864 ASSERT_EQ (5, bitmap_count_bits (b
));
2866 /* Verify bitmap_bit_p at the boundaries. */
2867 ASSERT_FALSE (bitmap_bit_p (b
, 6));
2868 ASSERT_TRUE (bitmap_bit_p (b
, 7));
2869 ASSERT_TRUE (bitmap_bit_p (b
, 11));
2870 ASSERT_FALSE (bitmap_bit_p (b
, 12));
2873 /* Verify splitting a range into two pieces using bitmap_clear_bit. */
2876 test_clear_bit_in_middle ()
2878 bitmap b
= bitmap_gc_alloc ();
2880 /* Set b to [100..200]. */
2881 bitmap_set_range (b
, 100, 100);
2882 ASSERT_EQ (100, bitmap_count_bits (b
));
2884 /* Clear a bit in the middle. */
2885 bool changed
= bitmap_clear_bit (b
, 150);
2886 ASSERT_TRUE (changed
);
2887 ASSERT_EQ (99, bitmap_count_bits (b
));
2888 ASSERT_TRUE (bitmap_bit_p (b
, 149));
2889 ASSERT_FALSE (bitmap_bit_p (b
, 150));
2890 ASSERT_TRUE (bitmap_bit_p (b
, 151));
2893 /* Verify bitmap_copy. */
2898 bitmap src
= bitmap_gc_alloc ();
2899 bitmap_set_range (src
, 40, 10);
2901 bitmap dst
= bitmap_gc_alloc ();
2902 ASSERT_FALSE (bitmap_equal_p (src
, dst
));
2903 bitmap_copy (dst
, src
);
2904 ASSERT_TRUE (bitmap_equal_p (src
, dst
));
2906 /* Verify that we can make them unequal again... */
2907 bitmap_set_range (src
, 70, 5);
2908 ASSERT_FALSE (bitmap_equal_p (src
, dst
));
2910 /* ...and that changing src after the copy didn't affect
2912 ASSERT_FALSE (bitmap_bit_p (dst
, 70));
2915 /* Verify bitmap_single_bit_set_p. */
2918 test_bitmap_single_bit_set_p ()
2920 bitmap b
= bitmap_gc_alloc ();
2922 ASSERT_FALSE (bitmap_single_bit_set_p (b
));
2924 bitmap_set_range (b
, 42, 1);
2925 ASSERT_TRUE (bitmap_single_bit_set_p (b
));
2926 ASSERT_EQ (42, bitmap_first_set_bit (b
));
2928 bitmap_set_range (b
, 1066, 1);
2929 ASSERT_FALSE (bitmap_single_bit_set_p (b
));
2930 ASSERT_EQ (42, bitmap_first_set_bit (b
));
2932 bitmap_clear_range (b
, 0, 100);
2933 ASSERT_TRUE (bitmap_single_bit_set_p (b
));
2934 ASSERT_EQ (1066, bitmap_first_set_bit (b
));
2937 /* Verify accessing aligned bit chunks works as expected. */
2940 test_aligned_chunk (unsigned num_bits
)
2942 bitmap b
= bitmap_gc_alloc ();
2943 int limit
= 2 ^ num_bits
;
2946 for (int x
= 0; x
< limit
; x
++)
2948 bitmap_set_aligned_chunk (b
, index
, num_bits
, (BITMAP_WORD
) x
);
2949 ASSERT_TRUE ((int) bitmap_get_aligned_chunk (b
, index
, num_bits
) == x
);
2950 ASSERT_TRUE ((int) bitmap_get_aligned_chunk (b
, index
+ 1,
2952 ASSERT_TRUE ((int) bitmap_get_aligned_chunk (b
, index
- 1,
2957 for (int x
= 0; x
< limit
; x
++)
2959 ASSERT_TRUE ((int) bitmap_get_aligned_chunk (b
, index
, num_bits
) == x
);
2964 /* Run all of the selftests within this file. */
2971 test_clear_bit_in_middle ();
2973 test_bitmap_single_bit_set_p ();
2974 /* Test 2, 4 and 8 bit aligned chunks. */
2975 test_aligned_chunk (2);
2976 test_aligned_chunk (4);
2977 test_aligned_chunk (8);
2980 } // namespace selftest
2981 #endif /* CHECKING_P */
2983 #include "gt-bitmap.h"