1 /* Functions to support general ended bitmaps.
2 Copyright (C) 1997-2016 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 /* Register new bitmap. */
31 bitmap_register (bitmap b MEM_STAT_DECL
)
33 bitmap_mem_desc
.register_descriptor (b
, BITMAP_ORIGIN
, false
37 /* Account the overhead. */
39 register_overhead (bitmap b
, size_t amount
)
41 if (bitmap_mem_desc
.contains_descriptor_for_instance (b
))
42 bitmap_mem_desc
.register_instance_overhead (amount
, b
);
46 bitmap_element bitmap_zero_bits
; /* An element of all zero bits. */
47 bitmap_obstack bitmap_default_obstack
; /* The default bitmap obstack. */
48 static int bitmap_default_obstack_depth
;
49 static GTY((deletable
)) bitmap_element
*bitmap_ggc_free
; /* Freelist of
52 static void bitmap_elem_to_freelist (bitmap
, bitmap_element
*);
53 static void bitmap_element_free (bitmap
, bitmap_element
*);
54 static bitmap_element
*bitmap_element_allocate (bitmap
);
55 static int bitmap_element_zerop (const bitmap_element
*);
56 static void bitmap_element_link (bitmap
, bitmap_element
*);
57 static bitmap_element
*bitmap_elt_insert_after (bitmap
, bitmap_element
*, unsigned int);
58 static void bitmap_elt_clear_from (bitmap
, bitmap_element
*);
59 static bitmap_element
*bitmap_find_bit (bitmap
, unsigned int);
62 /* Add ELEM to the appropriate freelist. */
64 bitmap_elem_to_freelist (bitmap head
, bitmap_element
*elt
)
66 bitmap_obstack
*bit_obstack
= head
->obstack
;
72 elt
->prev
= bit_obstack
->elements
;
73 bit_obstack
->elements
= elt
;
77 elt
->prev
= bitmap_ggc_free
;
78 bitmap_ggc_free
= elt
;
82 /* Free a bitmap element. Since these are allocated off the
83 bitmap_obstack, "free" actually means "put onto the freelist". */
86 bitmap_element_free (bitmap head
, bitmap_element
*elt
)
88 bitmap_element
*next
= elt
->next
;
89 bitmap_element
*prev
= elt
->prev
;
97 if (head
->first
== elt
)
100 /* Since the first thing we try is to insert before current,
101 make current the next entry in preference to the previous. */
102 if (head
->current
== elt
)
104 head
->current
= next
!= 0 ? next
: prev
;
106 head
->indx
= head
->current
->indx
;
111 if (GATHER_STATISTICS
)
112 register_overhead (head
, -((int)sizeof (bitmap_element
)));
114 bitmap_elem_to_freelist (head
, elt
);
117 /* Allocate a bitmap element. The bits are cleared, but nothing else is. */
119 static inline bitmap_element
*
120 bitmap_element_allocate (bitmap head
)
122 bitmap_element
*element
;
123 bitmap_obstack
*bit_obstack
= head
->obstack
;
127 element
= bit_obstack
->elements
;
130 /* Use up the inner list first before looking at the next
131 element of the outer list. */
134 bit_obstack
->elements
= element
->next
;
135 bit_obstack
->elements
->prev
= element
->prev
;
138 /* Inner list was just a singleton. */
139 bit_obstack
->elements
= element
->prev
;
141 element
= XOBNEW (&bit_obstack
->obstack
, bitmap_element
);
145 element
= bitmap_ggc_free
;
147 /* Use up the inner list first before looking at the next
148 element of the outer list. */
151 bitmap_ggc_free
= element
->next
;
152 bitmap_ggc_free
->prev
= element
->prev
;
155 /* Inner list was just a singleton. */
156 bitmap_ggc_free
= element
->prev
;
158 element
= ggc_alloc
<bitmap_element
> ();
161 if (GATHER_STATISTICS
)
162 register_overhead (head
, sizeof (bitmap_element
));
164 memset (element
->bits
, 0, sizeof (element
->bits
));
169 /* Remove ELT and all following elements from bitmap HEAD. */
172 bitmap_elt_clear_from (bitmap head
, bitmap_element
*elt
)
174 bitmap_element
*prev
;
175 bitmap_obstack
*bit_obstack
= head
->obstack
;
179 if (GATHER_STATISTICS
)
182 for (prev
= elt
; prev
; prev
= prev
->next
)
184 register_overhead (head
, -sizeof (bitmap_element
) * n
);
191 if (head
->current
->indx
> prev
->indx
)
193 head
->current
= prev
;
194 head
->indx
= prev
->indx
;
200 head
->current
= NULL
;
204 /* Put the entire list onto the free list in one operation. */
207 elt
->prev
= bit_obstack
->elements
;
208 bit_obstack
->elements
= elt
;
212 elt
->prev
= bitmap_ggc_free
;
213 bitmap_ggc_free
= elt
;
217 /* Clear a bitmap by freeing the linked list. */
220 bitmap_clear (bitmap head
)
223 bitmap_elt_clear_from (head
, head
->first
);
226 /* Initialize a bitmap obstack. If BIT_OBSTACK is NULL, initialize
227 the default bitmap obstack. */
230 bitmap_obstack_initialize (bitmap_obstack
*bit_obstack
)
234 if (bitmap_default_obstack_depth
++)
236 bit_obstack
= &bitmap_default_obstack
;
239 #if !defined(__GNUC__) || (__GNUC__ < 2)
240 #define __alignof__(type) 0
243 bit_obstack
->elements
= NULL
;
244 bit_obstack
->heads
= NULL
;
245 obstack_specify_allocation (&bit_obstack
->obstack
, OBSTACK_CHUNK_SIZE
,
246 __alignof__ (bitmap_element
),
251 /* Release the memory from a bitmap obstack. If BIT_OBSTACK is NULL,
252 release the default bitmap obstack. */
255 bitmap_obstack_release (bitmap_obstack
*bit_obstack
)
259 if (--bitmap_default_obstack_depth
)
261 gcc_assert (bitmap_default_obstack_depth
> 0);
264 bit_obstack
= &bitmap_default_obstack
;
267 bit_obstack
->elements
= NULL
;
268 bit_obstack
->heads
= NULL
;
269 obstack_free (&bit_obstack
->obstack
, NULL
);
272 /* Create a new bitmap on an obstack. If BIT_OBSTACK is NULL, create
273 it on the default bitmap obstack. */
276 bitmap_obstack_alloc_stat (bitmap_obstack
*bit_obstack MEM_STAT_DECL
)
281 bit_obstack
= &bitmap_default_obstack
;
282 map
= bit_obstack
->heads
;
284 bit_obstack
->heads
= (struct bitmap_head
*) map
->first
;
286 map
= XOBNEW (&bit_obstack
->obstack
, bitmap_head
);
287 bitmap_initialize_stat (map
, bit_obstack PASS_MEM_STAT
);
289 if (GATHER_STATISTICS
)
290 register_overhead (map
, sizeof (bitmap_head
));
295 /* Create a new GCd bitmap. */
298 bitmap_gc_alloc_stat (ALONE_MEM_STAT_DECL
)
302 map
= ggc_alloc
<bitmap_head
> ();
303 bitmap_initialize_stat (map
, NULL PASS_MEM_STAT
);
305 if (GATHER_STATISTICS
)
306 register_overhead (map
, sizeof (bitmap_head
));
311 /* Release an obstack allocated bitmap. */
314 bitmap_obstack_free (bitmap map
)
319 map
->first
= (bitmap_element
*) map
->obstack
->heads
;
321 if (GATHER_STATISTICS
)
322 register_overhead (map
, -((int)sizeof (bitmap_head
)));
324 map
->obstack
->heads
= map
;
329 /* Return nonzero if all bits in an element are zero. */
332 bitmap_element_zerop (const bitmap_element
*element
)
334 #if BITMAP_ELEMENT_WORDS == 2
335 return (element
->bits
[0] | element
->bits
[1]) == 0;
339 for (i
= 0; i
< BITMAP_ELEMENT_WORDS
; i
++)
340 if (element
->bits
[i
] != 0)
347 /* Link the bitmap element into the current bitmap linked list. */
350 bitmap_element_link (bitmap head
, bitmap_element
*element
)
352 unsigned int indx
= element
->indx
;
355 /* If this is the first and only element, set it in. */
356 if (head
->first
== 0)
358 element
->next
= element
->prev
= 0;
359 head
->first
= element
;
362 /* If this index is less than that of the current element, it goes someplace
363 before the current element. */
364 else if (indx
< head
->indx
)
366 for (ptr
= head
->current
;
367 ptr
->prev
!= 0 && ptr
->prev
->indx
> indx
;
372 ptr
->prev
->next
= element
;
374 head
->first
= element
;
376 element
->prev
= ptr
->prev
;
381 /* Otherwise, it must go someplace after the current element. */
384 for (ptr
= head
->current
;
385 ptr
->next
!= 0 && ptr
->next
->indx
< indx
;
390 ptr
->next
->prev
= element
;
392 element
->next
= ptr
->next
;
397 /* Set up so this is the first element searched. */
398 head
->current
= element
;
402 /* Insert a new uninitialized element into bitmap HEAD after element
403 ELT. If ELT is NULL, insert the element at the start. Return the
406 static bitmap_element
*
407 bitmap_elt_insert_after (bitmap head
, bitmap_element
*elt
, unsigned int indx
)
409 bitmap_element
*node
= bitmap_element_allocate (head
);
416 head
->current
= node
;
419 node
->next
= head
->first
;
421 node
->next
->prev
= node
;
427 gcc_checking_assert (head
->current
);
428 node
->next
= elt
->next
;
430 node
->next
->prev
= node
;
437 /* Copy a bitmap to another bitmap. */
440 bitmap_copy (bitmap to
, const_bitmap from
)
442 const bitmap_element
*from_ptr
;
443 bitmap_element
*to_ptr
= 0;
447 /* Copy elements in forward direction one at a time. */
448 for (from_ptr
= from
->first
; from_ptr
; from_ptr
= from_ptr
->next
)
450 bitmap_element
*to_elt
= bitmap_element_allocate (to
);
452 to_elt
->indx
= from_ptr
->indx
;
453 memcpy (to_elt
->bits
, from_ptr
->bits
, sizeof (to_elt
->bits
));
455 /* Here we have a special case of bitmap_element_link, for the case
456 where we know the links are being entered in sequence. */
459 to
->first
= to
->current
= to_elt
;
460 to
->indx
= from_ptr
->indx
;
461 to_elt
->next
= to_elt
->prev
= 0;
465 to_elt
->prev
= to_ptr
;
467 to_ptr
->next
= to_elt
;
474 /* Move a bitmap to another bitmap. */
477 bitmap_move (bitmap to
, bitmap from
)
479 gcc_assert (to
->obstack
== from
->obstack
);
485 if (GATHER_STATISTICS
)
488 for (bitmap_element
*e
= to
->first
; e
; e
= e
->next
)
489 sz
+= sizeof (bitmap_element
);
490 register_overhead (to
, sz
);
491 register_overhead (from
, -sz
);
495 /* Find a bitmap element that would hold a bitmap's bit.
496 Update the `current' field even if we can't find an element that
497 would hold the bitmap's bit to make eventual allocation
500 static inline bitmap_element
*
501 bitmap_find_bit (bitmap head
, unsigned int bit
)
503 bitmap_element
*element
;
504 unsigned int indx
= bit
/ BITMAP_ELEMENT_ALL_BITS
;
506 if (head
->current
== NULL
507 || head
->indx
== indx
)
508 return head
->current
;
509 if (head
->current
== head
->first
510 && head
->first
->next
== NULL
)
513 /* Usage can be NULL due to allocated bitmaps for which we do not
514 call initialize function. */
515 bitmap_usage
*usage
= NULL
;
516 if (GATHER_STATISTICS
)
517 usage
= bitmap_mem_desc
.get_descriptor_for_instance (head
);
519 /* This bitmap has more than one element, and we're going to look
520 through the elements list. Count that as a search. */
521 if (GATHER_STATISTICS
&& usage
)
522 usage
->m_nsearches
++;
524 if (head
->indx
< indx
)
525 /* INDX is beyond head->indx. Search from head->current
527 for (element
= head
->current
;
528 element
->next
!= 0 && element
->indx
< indx
;
529 element
= element
->next
)
531 if (GATHER_STATISTICS
&& usage
)
532 usage
->m_search_iter
++;
535 else if (head
->indx
/ 2 < indx
)
536 /* INDX is less than head->indx and closer to head->indx than to
537 0. Search from head->current backward. */
538 for (element
= head
->current
;
539 element
->prev
!= 0 && element
->indx
> indx
;
540 element
= element
->prev
)
542 if (GATHER_STATISTICS
&& usage
)
543 usage
->m_search_iter
++;
547 /* INDX is less than head->indx and closer to 0 than to
548 head->indx. Search from head->first forward. */
549 for (element
= head
->first
;
550 element
->next
!= 0 && element
->indx
< indx
;
551 element
= element
->next
)
552 if (GATHER_STATISTICS
&& usage
)
554 usage
->m_search_iter
++;
557 /* `element' is the nearest to the one we want. If it's not the one we
558 want, the one we want doesn't exist. */
559 head
->current
= element
;
560 head
->indx
= element
->indx
;
561 if (element
->indx
!= indx
)
567 /* Clear a single bit in a bitmap. Return true if the bit changed. */
570 bitmap_clear_bit (bitmap head
, int bit
)
572 bitmap_element
*const ptr
= bitmap_find_bit (head
, bit
);
576 unsigned bit_num
= bit
% BITMAP_WORD_BITS
;
577 unsigned word_num
= bit
/ BITMAP_WORD_BITS
% BITMAP_ELEMENT_WORDS
;
578 BITMAP_WORD bit_val
= ((BITMAP_WORD
) 1) << bit_num
;
579 bool res
= (ptr
->bits
[word_num
] & bit_val
) != 0;
582 ptr
->bits
[word_num
] &= ~bit_val
;
583 /* If we cleared the entire word, free up the element. */
584 if (!ptr
->bits
[word_num
]
585 && bitmap_element_zerop (ptr
))
586 bitmap_element_free (head
, ptr
);
595 /* Set a single bit in a bitmap. Return true if the bit changed. */
598 bitmap_set_bit (bitmap head
, int bit
)
600 bitmap_element
*ptr
= bitmap_find_bit (head
, bit
);
601 unsigned word_num
= bit
/ BITMAP_WORD_BITS
% BITMAP_ELEMENT_WORDS
;
602 unsigned bit_num
= bit
% BITMAP_WORD_BITS
;
603 BITMAP_WORD bit_val
= ((BITMAP_WORD
) 1) << bit_num
;
607 ptr
= bitmap_element_allocate (head
);
608 ptr
->indx
= bit
/ BITMAP_ELEMENT_ALL_BITS
;
609 ptr
->bits
[word_num
] = bit_val
;
610 bitmap_element_link (head
, ptr
);
615 bool res
= (ptr
->bits
[word_num
] & bit_val
) == 0;
617 ptr
->bits
[word_num
] |= bit_val
;
622 /* Return whether a bit is set within a bitmap. */
625 bitmap_bit_p (bitmap head
, int bit
)
631 ptr
= bitmap_find_bit (head
, bit
);
635 bit_num
= bit
% BITMAP_WORD_BITS
;
636 word_num
= bit
/ BITMAP_WORD_BITS
% BITMAP_ELEMENT_WORDS
;
638 return (ptr
->bits
[word_num
] >> bit_num
) & 1;
641 #if GCC_VERSION < 3400
642 /* Table of number of set bits in a character, indexed by value of char. */
643 static const unsigned char popcount_table
[] =
645 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,
646 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,
647 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,
648 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,
649 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,
650 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,
651 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,
652 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,
656 bitmap_popcount (BITMAP_WORD a
)
658 unsigned long ret
= 0;
661 /* Just do this the table way for now */
662 for (i
= 0; i
< BITMAP_WORD_BITS
; i
+= 8)
663 ret
+= popcount_table
[(a
>> i
) & 0xff];
668 /* Count and return the number of bits set in the bitmap word BITS. */
670 bitmap_count_bits_in_word (const BITMAP_WORD
*bits
)
672 unsigned long count
= 0;
674 for (unsigned ix
= 0; ix
!= BITMAP_ELEMENT_WORDS
; ix
++)
676 #if GCC_VERSION >= 3400
677 /* Note that popcountl matches BITMAP_WORD in type, so the actual size
678 of BITMAP_WORD is not material. */
679 count
+= __builtin_popcountl (bits
[ix
]);
681 count
+= bitmap_popcount (bits
[ix
]);
687 /* Count the number of bits set in the bitmap, and return it. */
690 bitmap_count_bits (const_bitmap a
)
692 unsigned long count
= 0;
693 const bitmap_element
*elt
;
695 for (elt
= a
->first
; elt
; elt
= elt
->next
)
696 count
+= bitmap_count_bits_in_word (elt
->bits
);
701 /* Count the number of unique bits set in A and B and return it. */
704 bitmap_count_unique_bits (const_bitmap a
, const_bitmap b
)
706 unsigned long count
= 0;
707 const bitmap_element
*elt_a
, *elt_b
;
709 for (elt_a
= a
->first
, elt_b
= b
->first
; elt_a
&& elt_b
; )
711 /* If we're at different indices, then count all the bits
712 in the lower element. If we're at the same index, then
713 count the bits in the IOR of the two elements. */
714 if (elt_a
->indx
< elt_b
->indx
)
716 count
+= bitmap_count_bits_in_word (elt_a
->bits
);
719 else if (elt_b
->indx
< elt_a
->indx
)
721 count
+= bitmap_count_bits_in_word (elt_b
->bits
);
726 BITMAP_WORD bits
[BITMAP_ELEMENT_WORDS
];
727 for (unsigned ix
= 0; ix
!= BITMAP_ELEMENT_WORDS
; ix
++)
728 bits
[ix
] = elt_a
->bits
[ix
] | elt_b
->bits
[ix
];
729 count
+= bitmap_count_bits_in_word (bits
);
737 /* Return true if the bitmap has a single bit set. Otherwise return
741 bitmap_single_bit_set_p (const_bitmap a
)
743 unsigned long count
= 0;
744 const bitmap_element
*elt
;
747 if (bitmap_empty_p (a
))
751 /* As there are no completely empty bitmap elements, a second one
752 means we have more than one bit set. */
753 if (elt
->next
!= NULL
)
756 for (ix
= 0; ix
!= BITMAP_ELEMENT_WORDS
; ix
++)
758 #if GCC_VERSION >= 3400
759 /* Note that popcountl matches BITMAP_WORD in type, so the actual size
760 of BITMAP_WORD is not material. */
761 count
+= __builtin_popcountl (elt
->bits
[ix
]);
763 count
+= bitmap_popcount (elt
->bits
[ix
]);
773 /* Return the bit number of the first set bit in the bitmap. The
774 bitmap must be non-empty. */
777 bitmap_first_set_bit (const_bitmap a
)
779 const bitmap_element
*elt
= a
->first
;
784 gcc_checking_assert (elt
);
785 bit_no
= elt
->indx
* BITMAP_ELEMENT_ALL_BITS
;
786 for (ix
= 0; ix
!= BITMAP_ELEMENT_WORDS
; ix
++)
788 word
= elt
->bits
[ix
];
794 bit_no
+= ix
* BITMAP_WORD_BITS
;
796 #if GCC_VERSION >= 3004
797 gcc_assert (sizeof (long) == sizeof (word
));
798 bit_no
+= __builtin_ctzl (word
);
800 /* Binary search for the first set bit. */
801 #if BITMAP_WORD_BITS > 64
802 #error "Fill out the table."
804 #if BITMAP_WORD_BITS > 32
805 if (!(word
& 0xffffffff))
806 word
>>= 32, bit_no
+= 32;
808 if (!(word
& 0xffff))
809 word
>>= 16, bit_no
+= 16;
811 word
>>= 8, bit_no
+= 8;
813 word
>>= 4, bit_no
+= 4;
815 word
>>= 2, bit_no
+= 2;
817 word
>>= 1, bit_no
+= 1;
819 gcc_checking_assert (word
& 1);
824 /* Return the bit number of the first set bit in the bitmap. The
825 bitmap must be non-empty. */
828 bitmap_last_set_bit (const_bitmap a
)
830 const bitmap_element
*elt
= a
->current
? a
->current
: a
->first
;
835 gcc_checking_assert (elt
);
838 bit_no
= elt
->indx
* BITMAP_ELEMENT_ALL_BITS
;
839 for (ix
= BITMAP_ELEMENT_WORDS
- 1; ix
>= 0; ix
--)
841 word
= elt
->bits
[ix
];
847 bit_no
+= ix
* BITMAP_WORD_BITS
;
848 #if GCC_VERSION >= 3004
849 gcc_assert (sizeof (long) == sizeof (word
));
850 bit_no
+= BITMAP_WORD_BITS
- __builtin_clzl (word
) - 1;
852 /* Hopefully this is a twos-complement host... */
853 BITMAP_WORD x
= word
;
859 #if BITMAP_WORD_BITS > 32
862 bit_no
+= bitmap_popcount (x
) - 1;
872 bitmap_and (bitmap dst
, const_bitmap a
, const_bitmap b
)
874 bitmap_element
*dst_elt
= dst
->first
;
875 const bitmap_element
*a_elt
= a
->first
;
876 const bitmap_element
*b_elt
= b
->first
;
877 bitmap_element
*dst_prev
= NULL
;
879 gcc_assert (dst
!= a
&& dst
!= b
);
883 bitmap_copy (dst
, a
);
887 while (a_elt
&& b_elt
)
889 if (a_elt
->indx
< b_elt
->indx
)
891 else if (b_elt
->indx
< a_elt
->indx
)
895 /* Matching elts, generate A & B. */
900 dst_elt
= bitmap_elt_insert_after (dst
, dst_prev
, a_elt
->indx
);
902 dst_elt
->indx
= a_elt
->indx
;
903 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
905 BITMAP_WORD r
= a_elt
->bits
[ix
] & b_elt
->bits
[ix
];
907 dst_elt
->bits
[ix
] = r
;
913 dst_elt
= dst_elt
->next
;
919 /* Ensure that dst->current is valid. */
920 dst
->current
= dst
->first
;
921 bitmap_elt_clear_from (dst
, dst_elt
);
922 gcc_checking_assert (!dst
->current
== !dst
->first
);
924 dst
->indx
= dst
->current
->indx
;
927 /* A &= B. Return true if A changed. */
930 bitmap_and_into (bitmap a
, const_bitmap b
)
932 bitmap_element
*a_elt
= a
->first
;
933 const bitmap_element
*b_elt
= b
->first
;
934 bitmap_element
*next
;
935 bool changed
= false;
940 while (a_elt
&& b_elt
)
942 if (a_elt
->indx
< b_elt
->indx
)
945 bitmap_element_free (a
, a_elt
);
949 else if (b_elt
->indx
< a_elt
->indx
)
953 /* Matching elts, generate A &= B. */
957 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
959 BITMAP_WORD r
= a_elt
->bits
[ix
] & b_elt
->bits
[ix
];
960 if (a_elt
->bits
[ix
] != r
)
967 bitmap_element_free (a
, a_elt
);
976 bitmap_elt_clear_from (a
, a_elt
);
979 gcc_checking_assert (!a
->current
== !a
->first
980 && (!a
->current
|| a
->indx
== a
->current
->indx
));
986 /* Insert an element equal to SRC_ELT after DST_PREV, overwriting DST_ELT
987 if non-NULL. CHANGED is true if the destination bitmap had already been
988 changed; the new value of CHANGED is returned. */
991 bitmap_elt_copy (bitmap dst
, bitmap_element
*dst_elt
, bitmap_element
*dst_prev
,
992 const bitmap_element
*src_elt
, bool changed
)
994 if (!changed
&& dst_elt
&& dst_elt
->indx
== src_elt
->indx
)
998 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
999 if (src_elt
->bits
[ix
] != dst_elt
->bits
[ix
])
1001 dst_elt
->bits
[ix
] = src_elt
->bits
[ix
];
1009 dst_elt
= bitmap_elt_insert_after (dst
, dst_prev
, src_elt
->indx
);
1011 dst_elt
->indx
= src_elt
->indx
;
1012 memcpy (dst_elt
->bits
, src_elt
->bits
, sizeof (dst_elt
->bits
));
1022 bitmap_and_compl (bitmap dst
, const_bitmap a
, const_bitmap b
)
1024 bitmap_element
*dst_elt
= dst
->first
;
1025 const bitmap_element
*a_elt
= a
->first
;
1026 const bitmap_element
*b_elt
= b
->first
;
1027 bitmap_element
*dst_prev
= NULL
;
1028 bitmap_element
**dst_prev_pnext
= &dst
->first
;
1029 bool changed
= false;
1031 gcc_assert (dst
!= a
&& dst
!= b
);
1035 changed
= !bitmap_empty_p (dst
);
1042 while (b_elt
&& b_elt
->indx
< a_elt
->indx
)
1043 b_elt
= b_elt
->next
;
1045 if (!b_elt
|| b_elt
->indx
> a_elt
->indx
)
1047 changed
= bitmap_elt_copy (dst
, dst_elt
, dst_prev
, a_elt
, changed
);
1048 dst_prev
= *dst_prev_pnext
;
1049 dst_prev_pnext
= &dst_prev
->next
;
1050 dst_elt
= *dst_prev_pnext
;
1051 a_elt
= a_elt
->next
;
1056 /* Matching elts, generate A & ~B. */
1058 BITMAP_WORD ior
= 0;
1060 if (!changed
&& dst_elt
&& dst_elt
->indx
== a_elt
->indx
)
1062 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1064 BITMAP_WORD r
= a_elt
->bits
[ix
] & ~b_elt
->bits
[ix
];
1066 if (dst_elt
->bits
[ix
] != r
)
1069 dst_elt
->bits
[ix
] = r
;
1077 if (!dst_elt
|| dst_elt
->indx
> a_elt
->indx
)
1079 dst_elt
= bitmap_elt_insert_after (dst
, dst_prev
, a_elt
->indx
);
1084 dst_elt
->indx
= a_elt
->indx
;
1085 new_element
= false;
1088 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1090 BITMAP_WORD r
= a_elt
->bits
[ix
] & ~b_elt
->bits
[ix
];
1092 dst_elt
->bits
[ix
] = r
;
1100 changed
|= !new_element
;
1101 bitmap_element_free (dst
, dst_elt
);
1102 dst_elt
= *dst_prev_pnext
;
1108 dst_prev
= *dst_prev_pnext
;
1109 dst_prev_pnext
= &dst_prev
->next
;
1110 dst_elt
= *dst_prev_pnext
;
1112 a_elt
= a_elt
->next
;
1113 b_elt
= b_elt
->next
;
1117 /* Ensure that dst->current is valid. */
1118 dst
->current
= dst
->first
;
1123 bitmap_elt_clear_from (dst
, dst_elt
);
1125 gcc_checking_assert (!dst
->current
== !dst
->first
);
1127 dst
->indx
= dst
->current
->indx
;
1132 /* A &= ~B. Returns true if A changes */
1135 bitmap_and_compl_into (bitmap a
, const_bitmap b
)
1137 bitmap_element
*a_elt
= a
->first
;
1138 const bitmap_element
*b_elt
= b
->first
;
1139 bitmap_element
*next
;
1140 BITMAP_WORD changed
= 0;
1144 if (bitmap_empty_p (a
))
1153 while (a_elt
&& b_elt
)
1155 if (a_elt
->indx
< b_elt
->indx
)
1156 a_elt
= a_elt
->next
;
1157 else if (b_elt
->indx
< a_elt
->indx
)
1158 b_elt
= b_elt
->next
;
1161 /* Matching elts, generate A &= ~B. */
1163 BITMAP_WORD ior
= 0;
1165 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1167 BITMAP_WORD cleared
= a_elt
->bits
[ix
] & b_elt
->bits
[ix
];
1168 BITMAP_WORD r
= a_elt
->bits
[ix
] ^ cleared
;
1170 a_elt
->bits
[ix
] = r
;
1176 bitmap_element_free (a
, a_elt
);
1178 b_elt
= b_elt
->next
;
1181 gcc_checking_assert (!a
->current
== !a
->first
1182 && (!a
->current
|| a
->indx
== a
->current
->indx
));
1183 return changed
!= 0;
1186 /* Set COUNT bits from START in HEAD. */
1188 bitmap_set_range (bitmap head
, unsigned int start
, unsigned int count
)
1190 unsigned int first_index
, end_bit_plus1
, last_index
;
1191 bitmap_element
*elt
, *elt_prev
;
1199 bitmap_set_bit (head
, start
);
1203 first_index
= start
/ BITMAP_ELEMENT_ALL_BITS
;
1204 end_bit_plus1
= start
+ count
;
1205 last_index
= (end_bit_plus1
- 1) / BITMAP_ELEMENT_ALL_BITS
;
1206 elt
= bitmap_find_bit (head
, start
);
1208 /* If bitmap_find_bit returns zero, the current is the closest block
1209 to the result. Otherwise, just use bitmap_element_allocate to
1210 ensure ELT is set; in the loop below, ELT == NULL means "insert
1211 at the end of the bitmap". */
1214 elt
= bitmap_element_allocate (head
);
1215 elt
->indx
= first_index
;
1216 bitmap_element_link (head
, elt
);
1219 gcc_checking_assert (elt
->indx
== first_index
);
1220 elt_prev
= elt
->prev
;
1221 for (i
= first_index
; i
<= last_index
; i
++)
1223 unsigned elt_start_bit
= i
* BITMAP_ELEMENT_ALL_BITS
;
1224 unsigned elt_end_bit_plus1
= elt_start_bit
+ BITMAP_ELEMENT_ALL_BITS
;
1226 unsigned int first_word_to_mod
;
1227 BITMAP_WORD first_mask
;
1228 unsigned int last_word_to_mod
;
1229 BITMAP_WORD last_mask
;
1232 if (!elt
|| elt
->indx
!= i
)
1233 elt
= bitmap_elt_insert_after (head
, elt_prev
, i
);
1235 if (elt_start_bit
<= start
)
1237 /* The first bit to turn on is somewhere inside this
1239 first_word_to_mod
= (start
- elt_start_bit
) / BITMAP_WORD_BITS
;
1241 /* This mask should have 1s in all bits >= start position. */
1243 (((BITMAP_WORD
) 1) << ((start
% BITMAP_WORD_BITS
))) - 1;
1244 first_mask
= ~first_mask
;
1248 /* The first bit to turn on is below this start of this elt. */
1249 first_word_to_mod
= 0;
1250 first_mask
= ~(BITMAP_WORD
) 0;
1253 if (elt_end_bit_plus1
<= end_bit_plus1
)
1255 /* The last bit to turn on is beyond this elt. */
1256 last_word_to_mod
= BITMAP_ELEMENT_WORDS
- 1;
1257 last_mask
= ~(BITMAP_WORD
) 0;
1261 /* The last bit to turn on is inside to this elt. */
1263 (end_bit_plus1
- elt_start_bit
) / BITMAP_WORD_BITS
;
1265 /* The last mask should have 1s below the end bit. */
1267 (((BITMAP_WORD
) 1) << ((end_bit_plus1
% BITMAP_WORD_BITS
))) - 1;
1270 if (first_word_to_mod
== last_word_to_mod
)
1272 BITMAP_WORD mask
= first_mask
& last_mask
;
1273 elt
->bits
[first_word_to_mod
] |= mask
;
1277 elt
->bits
[first_word_to_mod
] |= first_mask
;
1278 if (BITMAP_ELEMENT_WORDS
> 2)
1279 for (ix
= first_word_to_mod
+ 1; ix
< last_word_to_mod
; ix
++)
1280 elt
->bits
[ix
] = ~(BITMAP_WORD
) 0;
1281 elt
->bits
[last_word_to_mod
] |= last_mask
;
1288 head
->current
= elt
? elt
: elt_prev
;
1289 head
->indx
= head
->current
->indx
;
1292 /* Clear COUNT bits from START in HEAD. */
1294 bitmap_clear_range (bitmap head
, unsigned int start
, unsigned int count
)
1296 unsigned int first_index
, end_bit_plus1
, last_index
;
1297 bitmap_element
*elt
;
1304 bitmap_clear_bit (head
, start
);
1308 first_index
= start
/ BITMAP_ELEMENT_ALL_BITS
;
1309 end_bit_plus1
= start
+ count
;
1310 last_index
= (end_bit_plus1
- 1) / BITMAP_ELEMENT_ALL_BITS
;
1311 elt
= bitmap_find_bit (head
, start
);
1313 /* If bitmap_find_bit returns zero, the current is the closest block
1314 to the result. If the current is less than first index, find the
1315 next one. Otherwise, just set elt to be current. */
1320 if (head
->indx
< first_index
)
1322 elt
= head
->current
->next
;
1327 elt
= head
->current
;
1333 while (elt
&& (elt
->indx
<= last_index
))
1335 bitmap_element
* next_elt
= elt
->next
;
1336 unsigned elt_start_bit
= (elt
->indx
) * BITMAP_ELEMENT_ALL_BITS
;
1337 unsigned elt_end_bit_plus1
= elt_start_bit
+ BITMAP_ELEMENT_ALL_BITS
;
1340 if (elt_start_bit
>= start
&& elt_end_bit_plus1
<= end_bit_plus1
)
1341 /* Get rid of the entire elt and go to the next one. */
1342 bitmap_element_free (head
, elt
);
1345 /* Going to have to knock out some bits in this elt. */
1346 unsigned int first_word_to_mod
;
1347 BITMAP_WORD first_mask
;
1348 unsigned int last_word_to_mod
;
1349 BITMAP_WORD last_mask
;
1353 if (elt_start_bit
<= start
)
1355 /* The first bit to turn off is somewhere inside this
1357 first_word_to_mod
= (start
- elt_start_bit
) / BITMAP_WORD_BITS
;
1359 /* This mask should have 1s in all bits >= start position. */
1361 (((BITMAP_WORD
) 1) << ((start
% BITMAP_WORD_BITS
))) - 1;
1362 first_mask
= ~first_mask
;
1366 /* The first bit to turn off is below this start of this elt. */
1367 first_word_to_mod
= 0;
1369 first_mask
= ~first_mask
;
1372 if (elt_end_bit_plus1
<= end_bit_plus1
)
1374 /* The last bit to turn off is beyond this elt. */
1375 last_word_to_mod
= BITMAP_ELEMENT_WORDS
- 1;
1377 last_mask
= ~last_mask
;
1381 /* The last bit to turn off is inside to this elt. */
1383 (end_bit_plus1
- elt_start_bit
) / BITMAP_WORD_BITS
;
1385 /* The last mask should have 1s below the end bit. */
1387 (((BITMAP_WORD
) 1) << (((end_bit_plus1
) % BITMAP_WORD_BITS
))) - 1;
1391 if (first_word_to_mod
== last_word_to_mod
)
1393 BITMAP_WORD mask
= first_mask
& last_mask
;
1394 elt
->bits
[first_word_to_mod
] &= ~mask
;
1398 elt
->bits
[first_word_to_mod
] &= ~first_mask
;
1399 if (BITMAP_ELEMENT_WORDS
> 2)
1400 for (i
= first_word_to_mod
+ 1; i
< last_word_to_mod
; i
++)
1402 elt
->bits
[last_word_to_mod
] &= ~last_mask
;
1404 for (i
= 0; i
< BITMAP_ELEMENT_WORDS
; i
++)
1410 /* Check to see if there are any bits left. */
1412 bitmap_element_free (head
, elt
);
1419 head
->current
= elt
;
1420 head
->indx
= head
->current
->indx
;
1427 bitmap_compl_and_into (bitmap a
, const_bitmap b
)
1429 bitmap_element
*a_elt
= a
->first
;
1430 const bitmap_element
*b_elt
= b
->first
;
1431 bitmap_element
*a_prev
= NULL
;
1432 bitmap_element
*next
;
1434 gcc_assert (a
!= b
);
1436 if (bitmap_empty_p (a
))
1441 if (bitmap_empty_p (b
))
1447 while (a_elt
|| b_elt
)
1449 if (!b_elt
|| (a_elt
&& a_elt
->indx
< b_elt
->indx
))
1451 /* A is before B. Remove A */
1453 a_prev
= a_elt
->prev
;
1454 bitmap_element_free (a
, a_elt
);
1457 else if (!a_elt
|| b_elt
->indx
< a_elt
->indx
)
1459 /* B is before A. Copy B. */
1460 next
= bitmap_elt_insert_after (a
, a_prev
, b_elt
->indx
);
1461 memcpy (next
->bits
, b_elt
->bits
, sizeof (next
->bits
));
1463 b_elt
= b_elt
->next
;
1467 /* Matching elts, generate A = ~A & B. */
1469 BITMAP_WORD ior
= 0;
1471 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1473 BITMAP_WORD cleared
= a_elt
->bits
[ix
] & b_elt
->bits
[ix
];
1474 BITMAP_WORD r
= b_elt
->bits
[ix
] ^ cleared
;
1476 a_elt
->bits
[ix
] = r
;
1481 bitmap_element_free (a
, a_elt
);
1485 b_elt
= b_elt
->next
;
1488 gcc_checking_assert (!a
->current
== !a
->first
1489 && (!a
->current
|| a
->indx
== a
->current
->indx
));
1494 /* Insert an element corresponding to A_ELT | B_ELT after DST_PREV,
1495 overwriting DST_ELT if non-NULL. CHANGED is true if the destination bitmap
1496 had already been changed; the new value of CHANGED is returned. */
1499 bitmap_elt_ior (bitmap dst
, bitmap_element
*dst_elt
, bitmap_element
*dst_prev
,
1500 const bitmap_element
*a_elt
, const bitmap_element
*b_elt
,
1503 gcc_assert (a_elt
|| b_elt
);
1505 if (a_elt
&& b_elt
&& a_elt
->indx
== b_elt
->indx
)
1507 /* Matching elts, generate A | B. */
1510 if (!changed
&& dst_elt
&& dst_elt
->indx
== a_elt
->indx
)
1512 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1514 BITMAP_WORD r
= a_elt
->bits
[ix
] | b_elt
->bits
[ix
];
1515 if (r
!= dst_elt
->bits
[ix
])
1517 dst_elt
->bits
[ix
] = r
;
1526 dst_elt
= bitmap_elt_insert_after (dst
, dst_prev
, a_elt
->indx
);
1528 dst_elt
->indx
= a_elt
->indx
;
1529 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1531 BITMAP_WORD r
= a_elt
->bits
[ix
] | b_elt
->bits
[ix
];
1532 dst_elt
->bits
[ix
] = r
;
1538 /* Copy a single element. */
1539 const bitmap_element
*src
;
1541 if (!b_elt
|| (a_elt
&& a_elt
->indx
< b_elt
->indx
))
1546 gcc_checking_assert (src
);
1547 changed
= bitmap_elt_copy (dst
, dst_elt
, dst_prev
, src
, changed
);
1553 /* DST = A | B. Return true if DST changes. */
1556 bitmap_ior (bitmap dst
, const_bitmap a
, const_bitmap b
)
1558 bitmap_element
*dst_elt
= dst
->first
;
1559 const bitmap_element
*a_elt
= a
->first
;
1560 const bitmap_element
*b_elt
= b
->first
;
1561 bitmap_element
*dst_prev
= NULL
;
1562 bitmap_element
**dst_prev_pnext
= &dst
->first
;
1563 bool changed
= false;
1565 gcc_assert (dst
!= a
&& dst
!= b
);
1567 while (a_elt
|| b_elt
)
1569 changed
= bitmap_elt_ior (dst
, dst_elt
, dst_prev
, a_elt
, b_elt
, changed
);
1571 if (a_elt
&& b_elt
&& a_elt
->indx
== b_elt
->indx
)
1573 a_elt
= a_elt
->next
;
1574 b_elt
= b_elt
->next
;
1578 if (a_elt
&& (!b_elt
|| a_elt
->indx
<= b_elt
->indx
))
1579 a_elt
= a_elt
->next
;
1580 else if (b_elt
&& (!a_elt
|| b_elt
->indx
<= a_elt
->indx
))
1581 b_elt
= b_elt
->next
;
1584 dst_prev
= *dst_prev_pnext
;
1585 dst_prev_pnext
= &dst_prev
->next
;
1586 dst_elt
= *dst_prev_pnext
;
1592 /* Ensure that dst->current is valid. */
1593 dst
->current
= dst
->first
;
1594 bitmap_elt_clear_from (dst
, dst_elt
);
1596 gcc_checking_assert (!dst
->current
== !dst
->first
);
1598 dst
->indx
= dst
->current
->indx
;
1602 /* A |= B. Return true if A changes. */
1605 bitmap_ior_into (bitmap a
, const_bitmap b
)
1607 bitmap_element
*a_elt
= a
->first
;
1608 const bitmap_element
*b_elt
= b
->first
;
1609 bitmap_element
*a_prev
= NULL
;
1610 bitmap_element
**a_prev_pnext
= &a
->first
;
1611 bool changed
= false;
1618 /* If A lags behind B, just advance it. */
1619 if (!a_elt
|| a_elt
->indx
== b_elt
->indx
)
1621 changed
= bitmap_elt_ior (a
, a_elt
, a_prev
, a_elt
, b_elt
, changed
);
1622 b_elt
= b_elt
->next
;
1624 else if (a_elt
->indx
> b_elt
->indx
)
1626 changed
= bitmap_elt_copy (a
, NULL
, a_prev
, b_elt
, changed
);
1627 b_elt
= b_elt
->next
;
1630 a_prev
= *a_prev_pnext
;
1631 a_prev_pnext
= &a_prev
->next
;
1632 a_elt
= *a_prev_pnext
;
1635 gcc_checking_assert (!a
->current
== !a
->first
);
1637 a
->indx
= a
->current
->indx
;
1644 bitmap_xor (bitmap dst
, const_bitmap a
, const_bitmap b
)
1646 bitmap_element
*dst_elt
= dst
->first
;
1647 const bitmap_element
*a_elt
= a
->first
;
1648 const bitmap_element
*b_elt
= b
->first
;
1649 bitmap_element
*dst_prev
= NULL
;
1651 gcc_assert (dst
!= a
&& dst
!= b
);
1658 while (a_elt
|| b_elt
)
1660 if (a_elt
&& b_elt
&& a_elt
->indx
== b_elt
->indx
)
1662 /* Matching elts, generate A ^ B. */
1664 BITMAP_WORD ior
= 0;
1667 dst_elt
= bitmap_elt_insert_after (dst
, dst_prev
, a_elt
->indx
);
1669 dst_elt
->indx
= a_elt
->indx
;
1670 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1672 BITMAP_WORD r
= a_elt
->bits
[ix
] ^ b_elt
->bits
[ix
];
1675 dst_elt
->bits
[ix
] = r
;
1677 a_elt
= a_elt
->next
;
1678 b_elt
= b_elt
->next
;
1682 dst_elt
= dst_elt
->next
;
1687 /* Copy a single element. */
1688 const bitmap_element
*src
;
1690 if (!b_elt
|| (a_elt
&& a_elt
->indx
< b_elt
->indx
))
1693 a_elt
= a_elt
->next
;
1698 b_elt
= b_elt
->next
;
1702 dst_elt
= bitmap_elt_insert_after (dst
, dst_prev
, src
->indx
);
1704 dst_elt
->indx
= src
->indx
;
1705 memcpy (dst_elt
->bits
, src
->bits
, sizeof (dst_elt
->bits
));
1707 dst_elt
= dst_elt
->next
;
1710 /* Ensure that dst->current is valid. */
1711 dst
->current
= dst
->first
;
1712 bitmap_elt_clear_from (dst
, dst_elt
);
1713 gcc_checking_assert (!dst
->current
== !dst
->first
);
1715 dst
->indx
= dst
->current
->indx
;
1721 bitmap_xor_into (bitmap a
, const_bitmap b
)
1723 bitmap_element
*a_elt
= a
->first
;
1724 const bitmap_element
*b_elt
= b
->first
;
1725 bitmap_element
*a_prev
= NULL
;
1735 if (!a_elt
|| b_elt
->indx
< a_elt
->indx
)
1738 bitmap_element
*dst
= bitmap_elt_insert_after (a
, a_prev
, b_elt
->indx
);
1739 memcpy (dst
->bits
, b_elt
->bits
, sizeof (dst
->bits
));
1741 b_elt
= b_elt
->next
;
1743 else if (a_elt
->indx
< b_elt
->indx
)
1746 a_elt
= a_elt
->next
;
1750 /* Matching elts, generate A ^= B. */
1752 BITMAP_WORD ior
= 0;
1753 bitmap_element
*next
= a_elt
->next
;
1755 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1757 BITMAP_WORD r
= a_elt
->bits
[ix
] ^ b_elt
->bits
[ix
];
1760 a_elt
->bits
[ix
] = r
;
1762 b_elt
= b_elt
->next
;
1766 bitmap_element_free (a
, a_elt
);
1770 gcc_checking_assert (!a
->current
== !a
->first
);
1772 a
->indx
= a
->current
->indx
;
1775 /* Return true if two bitmaps are identical.
1776 We do not bother with a check for pointer equality, as that never
1777 occurs in practice. */
1780 bitmap_equal_p (const_bitmap a
, const_bitmap b
)
1782 const bitmap_element
*a_elt
;
1783 const bitmap_element
*b_elt
;
1786 for (a_elt
= a
->first
, b_elt
= b
->first
;
1788 a_elt
= a_elt
->next
, b_elt
= b_elt
->next
)
1790 if (a_elt
->indx
!= b_elt
->indx
)
1792 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1793 if (a_elt
->bits
[ix
] != b_elt
->bits
[ix
])
1796 return !a_elt
&& !b_elt
;
1799 /* Return true if A AND B is not empty. */
1802 bitmap_intersect_p (const_bitmap a
, const_bitmap b
)
1804 const bitmap_element
*a_elt
;
1805 const bitmap_element
*b_elt
;
1808 for (a_elt
= a
->first
, b_elt
= b
->first
;
1811 if (a_elt
->indx
< b_elt
->indx
)
1812 a_elt
= a_elt
->next
;
1813 else if (b_elt
->indx
< a_elt
->indx
)
1814 b_elt
= b_elt
->next
;
1817 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1818 if (a_elt
->bits
[ix
] & b_elt
->bits
[ix
])
1820 a_elt
= a_elt
->next
;
1821 b_elt
= b_elt
->next
;
1827 /* Return true if A AND NOT B is not empty. */
1830 bitmap_intersect_compl_p (const_bitmap a
, const_bitmap b
)
1832 const bitmap_element
*a_elt
;
1833 const bitmap_element
*b_elt
;
1835 for (a_elt
= a
->first
, b_elt
= b
->first
;
1838 if (a_elt
->indx
< b_elt
->indx
)
1840 else if (b_elt
->indx
< a_elt
->indx
)
1841 b_elt
= b_elt
->next
;
1844 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1845 if (a_elt
->bits
[ix
] & ~b_elt
->bits
[ix
])
1847 a_elt
= a_elt
->next
;
1848 b_elt
= b_elt
->next
;
1851 return a_elt
!= NULL
;
1855 /* DST = A | (FROM1 & ~FROM2). Return true if DST changes. */
1858 bitmap_ior_and_compl (bitmap dst
, const_bitmap a
, const_bitmap b
, const_bitmap kill
)
1860 bool changed
= false;
1862 bitmap_element
*dst_elt
= dst
->first
;
1863 const bitmap_element
*a_elt
= a
->first
;
1864 const bitmap_element
*b_elt
= b
->first
;
1865 const bitmap_element
*kill_elt
= kill
->first
;
1866 bitmap_element
*dst_prev
= NULL
;
1867 bitmap_element
**dst_prev_pnext
= &dst
->first
;
1869 gcc_assert (dst
!= a
&& dst
!= b
&& dst
!= kill
);
1871 /* Special cases. We don't bother checking for bitmap_equal_p (b, kill). */
1872 if (b
== kill
|| bitmap_empty_p (b
))
1874 changed
= !bitmap_equal_p (dst
, a
);
1876 bitmap_copy (dst
, a
);
1879 if (bitmap_empty_p (kill
))
1880 return bitmap_ior (dst
, a
, b
);
1881 if (bitmap_empty_p (a
))
1882 return bitmap_and_compl (dst
, b
, kill
);
1884 while (a_elt
|| b_elt
)
1886 bool new_element
= false;
1889 while (kill_elt
&& kill_elt
->indx
< b_elt
->indx
)
1890 kill_elt
= kill_elt
->next
;
1892 if (b_elt
&& kill_elt
&& kill_elt
->indx
== b_elt
->indx
1893 && (!a_elt
|| a_elt
->indx
>= b_elt
->indx
))
1895 bitmap_element tmp_elt
;
1898 BITMAP_WORD ior
= 0;
1899 tmp_elt
.indx
= b_elt
->indx
;
1900 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1902 BITMAP_WORD r
= b_elt
->bits
[ix
] & ~kill_elt
->bits
[ix
];
1904 tmp_elt
.bits
[ix
] = r
;
1909 changed
= bitmap_elt_ior (dst
, dst_elt
, dst_prev
,
1910 a_elt
, &tmp_elt
, changed
);
1912 if (a_elt
&& a_elt
->indx
== b_elt
->indx
)
1913 a_elt
= a_elt
->next
;
1916 b_elt
= b_elt
->next
;
1917 kill_elt
= kill_elt
->next
;
1921 changed
= bitmap_elt_ior (dst
, dst_elt
, dst_prev
,
1922 a_elt
, b_elt
, changed
);
1925 if (a_elt
&& b_elt
&& a_elt
->indx
== b_elt
->indx
)
1927 a_elt
= a_elt
->next
;
1928 b_elt
= b_elt
->next
;
1932 if (a_elt
&& (!b_elt
|| a_elt
->indx
<= b_elt
->indx
))
1933 a_elt
= a_elt
->next
;
1934 else if (b_elt
&& (!a_elt
|| b_elt
->indx
<= a_elt
->indx
))
1935 b_elt
= b_elt
->next
;
1941 dst_prev
= *dst_prev_pnext
;
1942 dst_prev_pnext
= &dst_prev
->next
;
1943 dst_elt
= *dst_prev_pnext
;
1950 /* Ensure that dst->current is valid. */
1951 dst
->current
= dst
->first
;
1952 bitmap_elt_clear_from (dst
, dst_elt
);
1954 gcc_checking_assert (!dst
->current
== !dst
->first
);
1956 dst
->indx
= dst
->current
->indx
;
1961 /* A |= (FROM1 & ~FROM2). Return true if A changes. */
1964 bitmap_ior_and_compl_into (bitmap a
, const_bitmap from1
, const_bitmap from2
)
1969 bitmap_initialize (&tmp
, &bitmap_default_obstack
);
1970 bitmap_and_compl (&tmp
, from1
, from2
);
1971 changed
= bitmap_ior_into (a
, &tmp
);
1972 bitmap_clear (&tmp
);
1977 /* A |= (B & C). Return true if A changes. */
1980 bitmap_ior_and_into (bitmap a
, const_bitmap b
, const_bitmap c
)
1982 bitmap_element
*a_elt
= a
->first
;
1983 const bitmap_element
*b_elt
= b
->first
;
1984 const bitmap_element
*c_elt
= c
->first
;
1985 bitmap_element and_elt
;
1986 bitmap_element
*a_prev
= NULL
;
1987 bitmap_element
**a_prev_pnext
= &a
->first
;
1988 bool changed
= false;
1992 return bitmap_ior_into (a
, b
);
1993 if (bitmap_empty_p (b
) || bitmap_empty_p (c
))
1997 while (b_elt
&& c_elt
)
1999 BITMAP_WORD overall
;
2001 /* Find a common item of B and C. */
2002 while (b_elt
->indx
!= c_elt
->indx
)
2004 if (b_elt
->indx
< c_elt
->indx
)
2006 b_elt
= b_elt
->next
;
2012 c_elt
= c_elt
->next
;
2019 and_elt
.indx
= b_elt
->indx
;
2020 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
2022 and_elt
.bits
[ix
] = b_elt
->bits
[ix
] & c_elt
->bits
[ix
];
2023 overall
|= and_elt
.bits
[ix
];
2026 b_elt
= b_elt
->next
;
2027 c_elt
= c_elt
->next
;
2031 /* Now find a place to insert AND_ELT. */
2034 ix
= a_elt
? a_elt
->indx
: and_elt
.indx
;
2035 if (ix
== and_elt
.indx
)
2036 changed
= bitmap_elt_ior (a
, a_elt
, a_prev
, a_elt
, &and_elt
, changed
);
2037 else if (ix
> and_elt
.indx
)
2038 changed
= bitmap_elt_copy (a
, NULL
, a_prev
, &and_elt
, changed
);
2040 a_prev
= *a_prev_pnext
;
2041 a_prev_pnext
= &a_prev
->next
;
2042 a_elt
= *a_prev_pnext
;
2044 /* If A lagged behind B/C, we advanced it so loop once more. */
2046 while (ix
< and_elt
.indx
);
2050 gcc_checking_assert (!a
->current
== !a
->first
);
2052 a
->indx
= a
->current
->indx
;
2056 /* Compute hash of bitmap (for purposes of hashing). */
2058 bitmap_hash (const_bitmap head
)
2060 const bitmap_element
*ptr
;
2061 BITMAP_WORD hash
= 0;
2064 for (ptr
= head
->first
; ptr
; ptr
= ptr
->next
)
2067 for (ix
= 0; ix
!= BITMAP_ELEMENT_WORDS
; ix
++)
2068 hash
^= ptr
->bits
[ix
];
2070 return (hashval_t
)hash
;
2074 /* Debugging function to print out the contents of a bitmap. */
2077 debug_bitmap_file (FILE *file
, const_bitmap head
)
2079 const bitmap_element
*ptr
;
2081 fprintf (file
, "\nfirst = " HOST_PTR_PRINTF
2082 " current = " HOST_PTR_PRINTF
" indx = %u\n",
2083 (void *) head
->first
, (void *) head
->current
, head
->indx
);
2085 for (ptr
= head
->first
; ptr
; ptr
= ptr
->next
)
2087 unsigned int i
, j
, col
= 26;
2089 fprintf (file
, "\t" HOST_PTR_PRINTF
" next = " HOST_PTR_PRINTF
2090 " prev = " HOST_PTR_PRINTF
" indx = %u\n\t\tbits = {",
2091 (const void*) ptr
, (const void*) ptr
->next
,
2092 (const void*) ptr
->prev
, ptr
->indx
);
2094 for (i
= 0; i
< BITMAP_ELEMENT_WORDS
; i
++)
2095 for (j
= 0; j
< BITMAP_WORD_BITS
; j
++)
2096 if ((ptr
->bits
[i
] >> j
) & 1)
2100 fprintf (file
, "\n\t\t\t");
2104 fprintf (file
, " %u", (ptr
->indx
* BITMAP_ELEMENT_ALL_BITS
2105 + i
* BITMAP_WORD_BITS
+ j
));
2109 fprintf (file
, " }\n");
2113 /* Function to be called from the debugger to print the contents
2117 debug_bitmap (const_bitmap head
)
2119 debug_bitmap_file (stderr
, head
);
2122 /* Function to print out the contents of a bitmap. Unlike debug_bitmap_file,
2123 it does not print anything but the bits. */
2126 bitmap_print (FILE *file
, const_bitmap head
, const char *prefix
,
2129 const char *comma
= "";
2133 fputs (prefix
, file
);
2134 EXECUTE_IF_SET_IN_BITMAP (head
, 0, i
, bi
)
2136 fprintf (file
, "%s%d", comma
, i
);
2139 fputs (suffix
, file
);
2142 /* Output per-bitmap memory usage statistics. */
2144 dump_bitmap_statistics (void)
2146 if (!GATHER_STATISTICS
)
2149 bitmap_mem_desc
.dump (BITMAP_ORIGIN
);
2153 debug (const bitmap_head
&ref
)
2155 dump_bitmap (stderr
, &ref
);
2159 debug (const bitmap_head
*ptr
)
2164 fprintf (stderr
, "<nil>\n");
2169 namespace selftest
{
2171 /* Selftests for bitmaps. */
2173 /* Freshly-created bitmaps ought to be empty. */
2178 bitmap b
= bitmap_gc_alloc ();
2179 ASSERT_TRUE (bitmap_empty_p (b
));
2182 /* Verify bitmap_set_range. */
2187 bitmap b
= bitmap_gc_alloc ();
2188 ASSERT_TRUE (bitmap_empty_p (b
));
2190 bitmap_set_range (b
, 7, 5);
2191 ASSERT_FALSE (bitmap_empty_p (b
));
2192 ASSERT_EQ (5, bitmap_count_bits (b
));
2194 /* Verify bitmap_bit_p at the boundaries. */
2195 ASSERT_FALSE (bitmap_bit_p (b
, 6));
2196 ASSERT_TRUE (bitmap_bit_p (b
, 7));
2197 ASSERT_TRUE (bitmap_bit_p (b
, 11));
2198 ASSERT_FALSE (bitmap_bit_p (b
, 12));
2201 /* Verify splitting a range into two pieces using bitmap_clear_bit. */
2204 test_clear_bit_in_middle ()
2206 bitmap b
= bitmap_gc_alloc ();
2208 /* Set b to [100..200]. */
2209 bitmap_set_range (b
, 100, 100);
2210 ASSERT_EQ (100, bitmap_count_bits (b
));
2212 /* Clear a bit in the middle. */
2213 bool changed
= bitmap_clear_bit (b
, 150);
2214 ASSERT_TRUE (changed
);
2215 ASSERT_EQ (99, bitmap_count_bits (b
));
2216 ASSERT_TRUE (bitmap_bit_p (b
, 149));
2217 ASSERT_FALSE (bitmap_bit_p (b
, 150));
2218 ASSERT_TRUE (bitmap_bit_p (b
, 151));
2221 /* Verify bitmap_copy. */
2226 bitmap src
= bitmap_gc_alloc ();
2227 bitmap_set_range (src
, 40, 10);
2229 bitmap dst
= bitmap_gc_alloc ();
2230 ASSERT_FALSE (bitmap_equal_p (src
, dst
));
2231 bitmap_copy (dst
, src
);
2232 ASSERT_TRUE (bitmap_equal_p (src
, dst
));
2234 /* Verify that we can make them unequal again... */
2235 bitmap_set_range (src
, 70, 5);
2236 ASSERT_FALSE (bitmap_equal_p (src
, dst
));
2238 /* ...and that changing src after the copy didn't affect
2240 ASSERT_FALSE (bitmap_bit_p (dst
, 70));
2243 /* Verify bitmap_single_bit_set_p. */
2246 test_bitmap_single_bit_set_p ()
2248 bitmap b
= bitmap_gc_alloc ();
2250 ASSERT_FALSE (bitmap_single_bit_set_p (b
));
2252 bitmap_set_range (b
, 42, 1);
2253 ASSERT_TRUE (bitmap_single_bit_set_p (b
));
2254 ASSERT_EQ (42, bitmap_first_set_bit (b
));
2256 bitmap_set_range (b
, 1066, 1);
2257 ASSERT_FALSE (bitmap_single_bit_set_p (b
));
2258 ASSERT_EQ (42, bitmap_first_set_bit (b
));
2260 bitmap_clear_range (b
, 0, 100);
2261 ASSERT_TRUE (bitmap_single_bit_set_p (b
));
2262 ASSERT_EQ (1066, bitmap_first_set_bit (b
));
2265 /* Run all of the selftests within this file. */
2272 test_clear_bit_in_middle ();
2274 test_bitmap_single_bit_set_p ();
2277 } // namespace selftest
2278 #endif /* CHECKING_P */
2280 #include "gt-bitmap.h"