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"
25 /* Memory allocation statistics purpose instance. */
26 mem_alloc_description
<bitmap_usage
> bitmap_mem_desc
;
28 /* Register new bitmap. */
30 bitmap_register (bitmap b MEM_STAT_DECL
)
32 bitmap_mem_desc
.register_descriptor (b
, BITMAP_ORIGIN
, false
36 /* Account the overhead. */
38 register_overhead (bitmap b
, size_t amount
)
40 if (bitmap_mem_desc
.contains_descriptor_for_instance (b
))
41 bitmap_mem_desc
.register_instance_overhead (amount
, b
);
45 bitmap_element bitmap_zero_bits
; /* An element of all zero bits. */
46 bitmap_obstack bitmap_default_obstack
; /* The default bitmap obstack. */
47 static int bitmap_default_obstack_depth
;
48 static GTY((deletable
)) bitmap_element
*bitmap_ggc_free
; /* Freelist of
51 static void bitmap_elem_to_freelist (bitmap
, bitmap_element
*);
52 static void bitmap_element_free (bitmap
, bitmap_element
*);
53 static bitmap_element
*bitmap_element_allocate (bitmap
);
54 static int bitmap_element_zerop (const bitmap_element
*);
55 static void bitmap_element_link (bitmap
, bitmap_element
*);
56 static bitmap_element
*bitmap_elt_insert_after (bitmap
, bitmap_element
*, unsigned int);
57 static void bitmap_elt_clear_from (bitmap
, bitmap_element
*);
58 static bitmap_element
*bitmap_find_bit (bitmap
, unsigned int);
61 /* Add ELEM to the appropriate freelist. */
63 bitmap_elem_to_freelist (bitmap head
, bitmap_element
*elt
)
65 bitmap_obstack
*bit_obstack
= head
->obstack
;
70 elt
->prev
= bit_obstack
->elements
;
71 bit_obstack
->elements
= elt
;
75 elt
->prev
= bitmap_ggc_free
;
76 bitmap_ggc_free
= elt
;
80 /* Free a bitmap element. Since these are allocated off the
81 bitmap_obstack, "free" actually means "put onto the freelist". */
84 bitmap_element_free (bitmap head
, bitmap_element
*elt
)
86 bitmap_element
*next
= elt
->next
;
87 bitmap_element
*prev
= elt
->prev
;
95 if (head
->first
== elt
)
98 /* Since the first thing we try is to insert before current,
99 make current the next entry in preference to the previous. */
100 if (head
->current
== elt
)
102 head
->current
= next
!= 0 ? next
: prev
;
104 head
->indx
= head
->current
->indx
;
109 if (GATHER_STATISTICS
)
110 register_overhead (head
, -((int)sizeof (bitmap_element
)));
112 bitmap_elem_to_freelist (head
, elt
);
115 /* Allocate a bitmap element. The bits are cleared, but nothing else is. */
117 static inline bitmap_element
*
118 bitmap_element_allocate (bitmap head
)
120 bitmap_element
*element
;
121 bitmap_obstack
*bit_obstack
= head
->obstack
;
125 element
= bit_obstack
->elements
;
128 /* Use up the inner list first before looking at the next
129 element of the outer list. */
132 bit_obstack
->elements
= element
->next
;
133 bit_obstack
->elements
->prev
= element
->prev
;
136 /* Inner list was just a singleton. */
137 bit_obstack
->elements
= element
->prev
;
139 element
= XOBNEW (&bit_obstack
->obstack
, bitmap_element
);
143 element
= bitmap_ggc_free
;
145 /* Use up the inner list first before looking at the next
146 element of the outer list. */
149 bitmap_ggc_free
= element
->next
;
150 bitmap_ggc_free
->prev
= element
->prev
;
153 /* Inner list was just a singleton. */
154 bitmap_ggc_free
= element
->prev
;
156 element
= ggc_alloc
<bitmap_element
> ();
159 if (GATHER_STATISTICS
)
160 register_overhead (head
, sizeof (bitmap_element
));
162 memset (element
->bits
, 0, sizeof (element
->bits
));
167 /* Remove ELT and all following elements from bitmap HEAD. */
170 bitmap_elt_clear_from (bitmap head
, bitmap_element
*elt
)
172 bitmap_element
*prev
;
173 bitmap_obstack
*bit_obstack
= head
->obstack
;
177 if (GATHER_STATISTICS
)
180 for (prev
= elt
; prev
; prev
= prev
->next
)
182 register_overhead (head
, -sizeof (bitmap_element
) * n
);
189 if (head
->current
->indx
> prev
->indx
)
191 head
->current
= prev
;
192 head
->indx
= prev
->indx
;
198 head
->current
= NULL
;
202 /* Put the entire list onto the free list in one operation. */
205 elt
->prev
= bit_obstack
->elements
;
206 bit_obstack
->elements
= elt
;
210 elt
->prev
= bitmap_ggc_free
;
211 bitmap_ggc_free
= elt
;
215 /* Clear a bitmap by freeing the linked list. */
218 bitmap_clear (bitmap head
)
221 bitmap_elt_clear_from (head
, head
->first
);
224 /* Initialize a bitmap obstack. If BIT_OBSTACK is NULL, initialize
225 the default bitmap obstack. */
228 bitmap_obstack_initialize (bitmap_obstack
*bit_obstack
)
232 if (bitmap_default_obstack_depth
++)
234 bit_obstack
= &bitmap_default_obstack
;
237 #if !defined(__GNUC__) || (__GNUC__ < 2)
238 #define __alignof__(type) 0
241 bit_obstack
->elements
= NULL
;
242 bit_obstack
->heads
= NULL
;
243 obstack_specify_allocation (&bit_obstack
->obstack
, OBSTACK_CHUNK_SIZE
,
244 __alignof__ (bitmap_element
),
249 /* Release the memory from a bitmap obstack. If BIT_OBSTACK is NULL,
250 release the default bitmap obstack. */
253 bitmap_obstack_release (bitmap_obstack
*bit_obstack
)
257 if (--bitmap_default_obstack_depth
)
259 gcc_assert (bitmap_default_obstack_depth
> 0);
262 bit_obstack
= &bitmap_default_obstack
;
265 bit_obstack
->elements
= NULL
;
266 bit_obstack
->heads
= NULL
;
267 obstack_free (&bit_obstack
->obstack
, NULL
);
270 /* Create a new bitmap on an obstack. If BIT_OBSTACK is NULL, create
271 it on the default bitmap obstack. */
274 bitmap_obstack_alloc_stat (bitmap_obstack
*bit_obstack MEM_STAT_DECL
)
279 bit_obstack
= &bitmap_default_obstack
;
280 map
= bit_obstack
->heads
;
282 bit_obstack
->heads
= (struct bitmap_head
*) map
->first
;
284 map
= XOBNEW (&bit_obstack
->obstack
, bitmap_head
);
285 bitmap_initialize_stat (map
, bit_obstack PASS_MEM_STAT
);
287 if (GATHER_STATISTICS
)
288 register_overhead (map
, sizeof (bitmap_head
));
293 /* Create a new GCd bitmap. */
296 bitmap_gc_alloc_stat (ALONE_MEM_STAT_DECL
)
300 map
= ggc_alloc
<bitmap_head
> ();
301 bitmap_initialize_stat (map
, NULL PASS_MEM_STAT
);
303 if (GATHER_STATISTICS
)
304 register_overhead (map
, sizeof (bitmap_head
));
309 /* Release an obstack allocated bitmap. */
312 bitmap_obstack_free (bitmap map
)
317 map
->first
= (bitmap_element
*) map
->obstack
->heads
;
319 if (GATHER_STATISTICS
)
320 register_overhead (map
, -((int)sizeof (bitmap_head
)));
322 map
->obstack
->heads
= map
;
327 /* Return nonzero if all bits in an element are zero. */
330 bitmap_element_zerop (const bitmap_element
*element
)
332 #if BITMAP_ELEMENT_WORDS == 2
333 return (element
->bits
[0] | element
->bits
[1]) == 0;
337 for (i
= 0; i
< BITMAP_ELEMENT_WORDS
; i
++)
338 if (element
->bits
[i
] != 0)
345 /* Link the bitmap element into the current bitmap linked list. */
348 bitmap_element_link (bitmap head
, bitmap_element
*element
)
350 unsigned int indx
= element
->indx
;
353 /* If this is the first and only element, set it in. */
354 if (head
->first
== 0)
356 element
->next
= element
->prev
= 0;
357 head
->first
= element
;
360 /* If this index is less than that of the current element, it goes someplace
361 before the current element. */
362 else if (indx
< head
->indx
)
364 for (ptr
= head
->current
;
365 ptr
->prev
!= 0 && ptr
->prev
->indx
> indx
;
370 ptr
->prev
->next
= element
;
372 head
->first
= element
;
374 element
->prev
= ptr
->prev
;
379 /* Otherwise, it must go someplace after the current element. */
382 for (ptr
= head
->current
;
383 ptr
->next
!= 0 && ptr
->next
->indx
< indx
;
388 ptr
->next
->prev
= element
;
390 element
->next
= ptr
->next
;
395 /* Set up so this is the first element searched. */
396 head
->current
= element
;
400 /* Insert a new uninitialized element into bitmap HEAD after element
401 ELT. If ELT is NULL, insert the element at the start. Return the
404 static bitmap_element
*
405 bitmap_elt_insert_after (bitmap head
, bitmap_element
*elt
, unsigned int indx
)
407 bitmap_element
*node
= bitmap_element_allocate (head
);
414 head
->current
= node
;
417 node
->next
= head
->first
;
419 node
->next
->prev
= node
;
425 gcc_checking_assert (head
->current
);
426 node
->next
= elt
->next
;
428 node
->next
->prev
= node
;
435 /* Copy a bitmap to another bitmap. */
438 bitmap_copy (bitmap to
, const_bitmap from
)
440 const bitmap_element
*from_ptr
;
441 bitmap_element
*to_ptr
= 0;
445 /* Copy elements in forward direction one at a time. */
446 for (from_ptr
= from
->first
; from_ptr
; from_ptr
= from_ptr
->next
)
448 bitmap_element
*to_elt
= bitmap_element_allocate (to
);
450 to_elt
->indx
= from_ptr
->indx
;
451 memcpy (to_elt
->bits
, from_ptr
->bits
, sizeof (to_elt
->bits
));
453 /* Here we have a special case of bitmap_element_link, for the case
454 where we know the links are being entered in sequence. */
457 to
->first
= to
->current
= to_elt
;
458 to
->indx
= from_ptr
->indx
;
459 to_elt
->next
= to_elt
->prev
= 0;
463 to_elt
->prev
= to_ptr
;
465 to_ptr
->next
= to_elt
;
472 /* Move a bitmap to another bitmap. */
475 bitmap_move (bitmap to
, bitmap from
)
477 gcc_assert (to
->obstack
== from
->obstack
);
483 if (GATHER_STATISTICS
)
486 for (bitmap_element
*e
= to
->first
; e
; e
= e
->next
)
487 sz
+= sizeof (bitmap_element
);
488 register_overhead (to
, sz
);
489 register_overhead (from
, -sz
);
493 /* Find a bitmap element that would hold a bitmap's bit.
494 Update the `current' field even if we can't find an element that
495 would hold the bitmap's bit to make eventual allocation
498 static inline bitmap_element
*
499 bitmap_find_bit (bitmap head
, unsigned int bit
)
501 bitmap_element
*element
;
502 unsigned int indx
= bit
/ BITMAP_ELEMENT_ALL_BITS
;
504 if (head
->current
== NULL
505 || head
->indx
== indx
)
506 return head
->current
;
507 if (head
->current
== head
->first
508 && head
->first
->next
== NULL
)
511 /* Usage can be NULL due to allocated bitmaps for which we do not
512 call initialize function. */
513 bitmap_usage
*usage
= NULL
;
514 if (GATHER_STATISTICS
)
515 usage
= bitmap_mem_desc
.get_descriptor_for_instance (head
);
517 /* This bitmap has more than one element, and we're going to look
518 through the elements list. Count that as a search. */
519 if (GATHER_STATISTICS
&& usage
)
520 usage
->m_nsearches
++;
522 if (head
->indx
< indx
)
523 /* INDX is beyond head->indx. Search from head->current
525 for (element
= head
->current
;
526 element
->next
!= 0 && element
->indx
< indx
;
527 element
= element
->next
)
529 if (GATHER_STATISTICS
&& usage
)
530 usage
->m_search_iter
++;
533 else if (head
->indx
/ 2 < indx
)
534 /* INDX is less than head->indx and closer to head->indx than to
535 0. Search from head->current backward. */
536 for (element
= head
->current
;
537 element
->prev
!= 0 && element
->indx
> indx
;
538 element
= element
->prev
)
540 if (GATHER_STATISTICS
&& usage
)
541 usage
->m_search_iter
++;
545 /* INDX is less than head->indx and closer to 0 than to
546 head->indx. Search from head->first forward. */
547 for (element
= head
->first
;
548 element
->next
!= 0 && element
->indx
< indx
;
549 element
= element
->next
)
550 if (GATHER_STATISTICS
&& usage
)
552 usage
->m_search_iter
++;
555 /* `element' is the nearest to the one we want. If it's not the one we
556 want, the one we want doesn't exist. */
557 head
->current
= element
;
558 head
->indx
= element
->indx
;
559 if (element
!= 0 && element
->indx
!= indx
)
565 /* Clear a single bit in a bitmap. Return true if the bit changed. */
568 bitmap_clear_bit (bitmap head
, int bit
)
570 bitmap_element
*const ptr
= bitmap_find_bit (head
, bit
);
574 unsigned bit_num
= bit
% BITMAP_WORD_BITS
;
575 unsigned word_num
= bit
/ BITMAP_WORD_BITS
% BITMAP_ELEMENT_WORDS
;
576 BITMAP_WORD bit_val
= ((BITMAP_WORD
) 1) << bit_num
;
577 bool res
= (ptr
->bits
[word_num
] & bit_val
) != 0;
580 ptr
->bits
[word_num
] &= ~bit_val
;
581 /* If we cleared the entire word, free up the element. */
582 if (!ptr
->bits
[word_num
]
583 && bitmap_element_zerop (ptr
))
584 bitmap_element_free (head
, ptr
);
593 /* Set a single bit in a bitmap. Return true if the bit changed. */
596 bitmap_set_bit (bitmap head
, int bit
)
598 bitmap_element
*ptr
= bitmap_find_bit (head
, bit
);
599 unsigned word_num
= bit
/ BITMAP_WORD_BITS
% BITMAP_ELEMENT_WORDS
;
600 unsigned bit_num
= bit
% BITMAP_WORD_BITS
;
601 BITMAP_WORD bit_val
= ((BITMAP_WORD
) 1) << bit_num
;
605 ptr
= bitmap_element_allocate (head
);
606 ptr
->indx
= bit
/ BITMAP_ELEMENT_ALL_BITS
;
607 ptr
->bits
[word_num
] = bit_val
;
608 bitmap_element_link (head
, ptr
);
613 bool res
= (ptr
->bits
[word_num
] & bit_val
) == 0;
615 ptr
->bits
[word_num
] |= bit_val
;
620 /* Return whether a bit is set within a bitmap. */
623 bitmap_bit_p (bitmap head
, int bit
)
629 ptr
= bitmap_find_bit (head
, bit
);
633 bit_num
= bit
% BITMAP_WORD_BITS
;
634 word_num
= bit
/ BITMAP_WORD_BITS
% BITMAP_ELEMENT_WORDS
;
636 return (ptr
->bits
[word_num
] >> bit_num
) & 1;
639 #if GCC_VERSION < 3400
640 /* Table of number of set bits in a character, indexed by value of char. */
641 static const unsigned char popcount_table
[] =
643 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,
644 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,
645 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,
646 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,
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 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,
650 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,
654 bitmap_popcount (BITMAP_WORD a
)
656 unsigned long ret
= 0;
659 /* Just do this the table way for now */
660 for (i
= 0; i
< BITMAP_WORD_BITS
; i
+= 8)
661 ret
+= popcount_table
[(a
>> i
) & 0xff];
666 /* Count and return the number of bits set in the bitmap word BITS. */
668 bitmap_count_bits_in_word (const BITMAP_WORD
*bits
)
670 unsigned long count
= 0;
672 for (unsigned ix
= 0; ix
!= BITMAP_ELEMENT_WORDS
; ix
++)
674 #if GCC_VERSION >= 3400
675 /* Note that popcountl matches BITMAP_WORD in type, so the actual size
676 of BITMAP_WORD is not material. */
677 count
+= __builtin_popcountl (bits
[ix
]);
679 count
+= bitmap_popcount (bits
[ix
]);
685 /* Count the number of bits set in the bitmap, and return it. */
688 bitmap_count_bits (const_bitmap a
)
690 unsigned long count
= 0;
691 const bitmap_element
*elt
;
693 for (elt
= a
->first
; elt
; elt
= elt
->next
)
694 count
+= bitmap_count_bits_in_word (elt
->bits
);
699 /* Count the number of unique bits set in A and B and return it. */
702 bitmap_count_unique_bits (const_bitmap a
, const_bitmap b
)
704 unsigned long count
= 0;
705 const bitmap_element
*elt_a
, *elt_b
;
707 for (elt_a
= a
->first
, elt_b
= b
->first
; elt_a
&& elt_b
; )
709 /* If we're at different indices, then count all the bits
710 in the lower element. If we're at the same index, then
711 count the bits in the IOR of the two elements. */
712 if (elt_a
->indx
< elt_b
->indx
)
714 count
+= bitmap_count_bits_in_word (elt_a
->bits
);
717 else if (elt_b
->indx
< elt_a
->indx
)
719 count
+= bitmap_count_bits_in_word (elt_b
->bits
);
724 BITMAP_WORD bits
[BITMAP_ELEMENT_WORDS
];
725 for (unsigned ix
= 0; ix
!= BITMAP_ELEMENT_WORDS
; ix
++)
726 bits
[ix
] = elt_a
->bits
[ix
] | elt_b
->bits
[ix
];
727 count
+= bitmap_count_bits_in_word (bits
);
735 /* Return true if the bitmap has a single bit set. Otherwise return
739 bitmap_single_bit_set_p (const_bitmap a
)
741 unsigned long count
= 0;
742 const bitmap_element
*elt
;
745 if (bitmap_empty_p (a
))
749 /* As there are no completely empty bitmap elements, a second one
750 means we have more than one bit set. */
751 if (elt
->next
!= NULL
)
754 for (ix
= 0; ix
!= BITMAP_ELEMENT_WORDS
; ix
++)
756 #if GCC_VERSION >= 3400
757 /* Note that popcountl matches BITMAP_WORD in type, so the actual size
758 of BITMAP_WORD is not material. */
759 count
+= __builtin_popcountl (elt
->bits
[ix
]);
761 count
+= bitmap_popcount (elt
->bits
[ix
]);
771 /* Return the bit number of the first set bit in the bitmap. The
772 bitmap must be non-empty. */
775 bitmap_first_set_bit (const_bitmap a
)
777 const bitmap_element
*elt
= a
->first
;
782 gcc_checking_assert (elt
);
783 bit_no
= elt
->indx
* BITMAP_ELEMENT_ALL_BITS
;
784 for (ix
= 0; ix
!= BITMAP_ELEMENT_WORDS
; ix
++)
786 word
= elt
->bits
[ix
];
792 bit_no
+= ix
* BITMAP_WORD_BITS
;
794 #if GCC_VERSION >= 3004
795 gcc_assert (sizeof (long) == sizeof (word
));
796 bit_no
+= __builtin_ctzl (word
);
798 /* Binary search for the first set bit. */
799 #if BITMAP_WORD_BITS > 64
800 #error "Fill out the table."
802 #if BITMAP_WORD_BITS > 32
803 if (!(word
& 0xffffffff))
804 word
>>= 32, bit_no
+= 32;
806 if (!(word
& 0xffff))
807 word
>>= 16, bit_no
+= 16;
809 word
>>= 8, bit_no
+= 8;
811 word
>>= 4, bit_no
+= 4;
813 word
>>= 2, bit_no
+= 2;
815 word
>>= 1, bit_no
+= 1;
817 gcc_checking_assert (word
& 1);
822 /* Return the bit number of the first set bit in the bitmap. The
823 bitmap must be non-empty. */
826 bitmap_last_set_bit (const_bitmap a
)
828 const bitmap_element
*elt
= a
->current
? a
->current
: a
->first
;
833 gcc_checking_assert (elt
);
836 bit_no
= elt
->indx
* BITMAP_ELEMENT_ALL_BITS
;
837 for (ix
= BITMAP_ELEMENT_WORDS
- 1; ix
>= 0; ix
--)
839 word
= elt
->bits
[ix
];
845 bit_no
+= ix
* BITMAP_WORD_BITS
;
846 #if GCC_VERSION >= 3004
847 gcc_assert (sizeof (long) == sizeof (word
));
848 bit_no
+= BITMAP_WORD_BITS
- __builtin_clzl (word
) - 1;
850 /* Hopefully this is a twos-complement host... */
851 BITMAP_WORD x
= word
;
857 #if BITMAP_WORD_BITS > 32
860 bit_no
+= bitmap_popcount (x
) - 1;
870 bitmap_and (bitmap dst
, const_bitmap a
, const_bitmap b
)
872 bitmap_element
*dst_elt
= dst
->first
;
873 const bitmap_element
*a_elt
= a
->first
;
874 const bitmap_element
*b_elt
= b
->first
;
875 bitmap_element
*dst_prev
= NULL
;
877 gcc_assert (dst
!= a
&& dst
!= b
);
881 bitmap_copy (dst
, a
);
885 while (a_elt
&& b_elt
)
887 if (a_elt
->indx
< b_elt
->indx
)
889 else if (b_elt
->indx
< a_elt
->indx
)
893 /* Matching elts, generate A & B. */
898 dst_elt
= bitmap_elt_insert_after (dst
, dst_prev
, a_elt
->indx
);
900 dst_elt
->indx
= a_elt
->indx
;
901 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
903 BITMAP_WORD r
= a_elt
->bits
[ix
] & b_elt
->bits
[ix
];
905 dst_elt
->bits
[ix
] = r
;
911 dst_elt
= dst_elt
->next
;
917 /* Ensure that dst->current is valid. */
918 dst
->current
= dst
->first
;
919 bitmap_elt_clear_from (dst
, dst_elt
);
920 gcc_checking_assert (!dst
->current
== !dst
->first
);
922 dst
->indx
= dst
->current
->indx
;
925 /* A &= B. Return true if A changed. */
928 bitmap_and_into (bitmap a
, const_bitmap b
)
930 bitmap_element
*a_elt
= a
->first
;
931 const bitmap_element
*b_elt
= b
->first
;
932 bitmap_element
*next
;
933 bool changed
= false;
938 while (a_elt
&& b_elt
)
940 if (a_elt
->indx
< b_elt
->indx
)
943 bitmap_element_free (a
, a_elt
);
947 else if (b_elt
->indx
< a_elt
->indx
)
951 /* Matching elts, generate A &= B. */
955 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
957 BITMAP_WORD r
= a_elt
->bits
[ix
] & b_elt
->bits
[ix
];
958 if (a_elt
->bits
[ix
] != r
)
965 bitmap_element_free (a
, a_elt
);
974 bitmap_elt_clear_from (a
, a_elt
);
977 gcc_checking_assert (!a
->current
== !a
->first
978 && (!a
->current
|| a
->indx
== a
->current
->indx
));
984 /* Insert an element equal to SRC_ELT after DST_PREV, overwriting DST_ELT
985 if non-NULL. CHANGED is true if the destination bitmap had already been
986 changed; the new value of CHANGED is returned. */
989 bitmap_elt_copy (bitmap dst
, bitmap_element
*dst_elt
, bitmap_element
*dst_prev
,
990 const bitmap_element
*src_elt
, bool changed
)
992 if (!changed
&& dst_elt
&& dst_elt
->indx
== src_elt
->indx
)
996 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
997 if (src_elt
->bits
[ix
] != dst_elt
->bits
[ix
])
999 dst_elt
->bits
[ix
] = src_elt
->bits
[ix
];
1007 dst_elt
= bitmap_elt_insert_after (dst
, dst_prev
, src_elt
->indx
);
1009 dst_elt
->indx
= src_elt
->indx
;
1010 memcpy (dst_elt
->bits
, src_elt
->bits
, sizeof (dst_elt
->bits
));
1020 bitmap_and_compl (bitmap dst
, const_bitmap a
, const_bitmap b
)
1022 bitmap_element
*dst_elt
= dst
->first
;
1023 const bitmap_element
*a_elt
= a
->first
;
1024 const bitmap_element
*b_elt
= b
->first
;
1025 bitmap_element
*dst_prev
= NULL
;
1026 bitmap_element
**dst_prev_pnext
= &dst
->first
;
1027 bool changed
= false;
1029 gcc_assert (dst
!= a
&& dst
!= b
);
1033 changed
= !bitmap_empty_p (dst
);
1040 while (b_elt
&& b_elt
->indx
< a_elt
->indx
)
1041 b_elt
= b_elt
->next
;
1043 if (!b_elt
|| b_elt
->indx
> a_elt
->indx
)
1045 changed
= bitmap_elt_copy (dst
, dst_elt
, dst_prev
, a_elt
, changed
);
1046 dst_prev
= *dst_prev_pnext
;
1047 dst_prev_pnext
= &dst_prev
->next
;
1048 dst_elt
= *dst_prev_pnext
;
1049 a_elt
= a_elt
->next
;
1054 /* Matching elts, generate A & ~B. */
1056 BITMAP_WORD ior
= 0;
1058 if (!changed
&& dst_elt
&& dst_elt
->indx
== a_elt
->indx
)
1060 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1062 BITMAP_WORD r
= a_elt
->bits
[ix
] & ~b_elt
->bits
[ix
];
1064 if (dst_elt
->bits
[ix
] != r
)
1067 dst_elt
->bits
[ix
] = r
;
1075 if (!dst_elt
|| dst_elt
->indx
> a_elt
->indx
)
1077 dst_elt
= bitmap_elt_insert_after (dst
, dst_prev
, a_elt
->indx
);
1082 dst_elt
->indx
= a_elt
->indx
;
1083 new_element
= false;
1086 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1088 BITMAP_WORD r
= a_elt
->bits
[ix
] & ~b_elt
->bits
[ix
];
1090 dst_elt
->bits
[ix
] = r
;
1098 changed
|= !new_element
;
1099 bitmap_element_free (dst
, dst_elt
);
1100 dst_elt
= *dst_prev_pnext
;
1106 dst_prev
= *dst_prev_pnext
;
1107 dst_prev_pnext
= &dst_prev
->next
;
1108 dst_elt
= *dst_prev_pnext
;
1110 a_elt
= a_elt
->next
;
1111 b_elt
= b_elt
->next
;
1115 /* Ensure that dst->current is valid. */
1116 dst
->current
= dst
->first
;
1121 bitmap_elt_clear_from (dst
, dst_elt
);
1123 gcc_checking_assert (!dst
->current
== !dst
->first
);
1125 dst
->indx
= dst
->current
->indx
;
1130 /* A &= ~B. Returns true if A changes */
1133 bitmap_and_compl_into (bitmap a
, const_bitmap b
)
1135 bitmap_element
*a_elt
= a
->first
;
1136 const bitmap_element
*b_elt
= b
->first
;
1137 bitmap_element
*next
;
1138 BITMAP_WORD changed
= 0;
1142 if (bitmap_empty_p (a
))
1151 while (a_elt
&& b_elt
)
1153 if (a_elt
->indx
< b_elt
->indx
)
1154 a_elt
= a_elt
->next
;
1155 else if (b_elt
->indx
< a_elt
->indx
)
1156 b_elt
= b_elt
->next
;
1159 /* Matching elts, generate A &= ~B. */
1161 BITMAP_WORD ior
= 0;
1163 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1165 BITMAP_WORD cleared
= a_elt
->bits
[ix
] & b_elt
->bits
[ix
];
1166 BITMAP_WORD r
= a_elt
->bits
[ix
] ^ cleared
;
1168 a_elt
->bits
[ix
] = r
;
1174 bitmap_element_free (a
, a_elt
);
1176 b_elt
= b_elt
->next
;
1179 gcc_checking_assert (!a
->current
== !a
->first
1180 && (!a
->current
|| a
->indx
== a
->current
->indx
));
1181 return changed
!= 0;
1184 /* Set COUNT bits from START in HEAD. */
1186 bitmap_set_range (bitmap head
, unsigned int start
, unsigned int count
)
1188 unsigned int first_index
, end_bit_plus1
, last_index
;
1189 bitmap_element
*elt
, *elt_prev
;
1197 bitmap_set_bit (head
, start
);
1201 first_index
= start
/ BITMAP_ELEMENT_ALL_BITS
;
1202 end_bit_plus1
= start
+ count
;
1203 last_index
= (end_bit_plus1
- 1) / BITMAP_ELEMENT_ALL_BITS
;
1204 elt
= bitmap_find_bit (head
, start
);
1206 /* If bitmap_find_bit returns zero, the current is the closest block
1207 to the result. Otherwise, just use bitmap_element_allocate to
1208 ensure ELT is set; in the loop below, ELT == NULL means "insert
1209 at the end of the bitmap". */
1212 elt
= bitmap_element_allocate (head
);
1213 elt
->indx
= first_index
;
1214 bitmap_element_link (head
, elt
);
1217 gcc_checking_assert (elt
->indx
== first_index
);
1218 elt_prev
= elt
->prev
;
1219 for (i
= first_index
; i
<= last_index
; i
++)
1221 unsigned elt_start_bit
= i
* BITMAP_ELEMENT_ALL_BITS
;
1222 unsigned elt_end_bit_plus1
= elt_start_bit
+ BITMAP_ELEMENT_ALL_BITS
;
1224 unsigned int first_word_to_mod
;
1225 BITMAP_WORD first_mask
;
1226 unsigned int last_word_to_mod
;
1227 BITMAP_WORD last_mask
;
1230 if (!elt
|| elt
->indx
!= i
)
1231 elt
= bitmap_elt_insert_after (head
, elt_prev
, i
);
1233 if (elt_start_bit
<= start
)
1235 /* The first bit to turn on is somewhere inside this
1237 first_word_to_mod
= (start
- elt_start_bit
) / BITMAP_WORD_BITS
;
1239 /* This mask should have 1s in all bits >= start position. */
1241 (((BITMAP_WORD
) 1) << ((start
% BITMAP_WORD_BITS
))) - 1;
1242 first_mask
= ~first_mask
;
1246 /* The first bit to turn on is below this start of this elt. */
1247 first_word_to_mod
= 0;
1248 first_mask
= ~(BITMAP_WORD
) 0;
1251 if (elt_end_bit_plus1
<= end_bit_plus1
)
1253 /* The last bit to turn on is beyond this elt. */
1254 last_word_to_mod
= BITMAP_ELEMENT_WORDS
- 1;
1255 last_mask
= ~(BITMAP_WORD
) 0;
1259 /* The last bit to turn on is inside to this elt. */
1261 (end_bit_plus1
- elt_start_bit
) / BITMAP_WORD_BITS
;
1263 /* The last mask should have 1s below the end bit. */
1265 (((BITMAP_WORD
) 1) << ((end_bit_plus1
% BITMAP_WORD_BITS
))) - 1;
1268 if (first_word_to_mod
== last_word_to_mod
)
1270 BITMAP_WORD mask
= first_mask
& last_mask
;
1271 elt
->bits
[first_word_to_mod
] |= mask
;
1275 elt
->bits
[first_word_to_mod
] |= first_mask
;
1276 if (BITMAP_ELEMENT_WORDS
> 2)
1277 for (ix
= first_word_to_mod
+ 1; ix
< last_word_to_mod
; ix
++)
1278 elt
->bits
[ix
] = ~(BITMAP_WORD
) 0;
1279 elt
->bits
[last_word_to_mod
] |= last_mask
;
1286 head
->current
= elt
? elt
: elt_prev
;
1287 head
->indx
= head
->current
->indx
;
1290 /* Clear COUNT bits from START in HEAD. */
1292 bitmap_clear_range (bitmap head
, unsigned int start
, unsigned int count
)
1294 unsigned int first_index
, end_bit_plus1
, last_index
;
1295 bitmap_element
*elt
;
1302 bitmap_clear_bit (head
, start
);
1306 first_index
= start
/ BITMAP_ELEMENT_ALL_BITS
;
1307 end_bit_plus1
= start
+ count
;
1308 last_index
= (end_bit_plus1
- 1) / BITMAP_ELEMENT_ALL_BITS
;
1309 elt
= bitmap_find_bit (head
, start
);
1311 /* If bitmap_find_bit returns zero, the current is the closest block
1312 to the result. If the current is less than first index, find the
1313 next one. Otherwise, just set elt to be current. */
1318 if (head
->indx
< first_index
)
1320 elt
= head
->current
->next
;
1325 elt
= head
->current
;
1331 while (elt
&& (elt
->indx
<= last_index
))
1333 bitmap_element
* next_elt
= elt
->next
;
1334 unsigned elt_start_bit
= (elt
->indx
) * BITMAP_ELEMENT_ALL_BITS
;
1335 unsigned elt_end_bit_plus1
= elt_start_bit
+ BITMAP_ELEMENT_ALL_BITS
;
1338 if (elt_start_bit
>= start
&& elt_end_bit_plus1
<= end_bit_plus1
)
1339 /* Get rid of the entire elt and go to the next one. */
1340 bitmap_element_free (head
, elt
);
1343 /* Going to have to knock out some bits in this elt. */
1344 unsigned int first_word_to_mod
;
1345 BITMAP_WORD first_mask
;
1346 unsigned int last_word_to_mod
;
1347 BITMAP_WORD last_mask
;
1351 if (elt_start_bit
<= start
)
1353 /* The first bit to turn off is somewhere inside this
1355 first_word_to_mod
= (start
- elt_start_bit
) / BITMAP_WORD_BITS
;
1357 /* This mask should have 1s in all bits >= start position. */
1359 (((BITMAP_WORD
) 1) << ((start
% BITMAP_WORD_BITS
))) - 1;
1360 first_mask
= ~first_mask
;
1364 /* The first bit to turn off is below this start of this elt. */
1365 first_word_to_mod
= 0;
1367 first_mask
= ~first_mask
;
1370 if (elt_end_bit_plus1
<= end_bit_plus1
)
1372 /* The last bit to turn off is beyond this elt. */
1373 last_word_to_mod
= BITMAP_ELEMENT_WORDS
- 1;
1375 last_mask
= ~last_mask
;
1379 /* The last bit to turn off is inside to this elt. */
1381 (end_bit_plus1
- elt_start_bit
) / BITMAP_WORD_BITS
;
1383 /* The last mask should have 1s below the end bit. */
1385 (((BITMAP_WORD
) 1) << (((end_bit_plus1
) % BITMAP_WORD_BITS
))) - 1;
1389 if (first_word_to_mod
== last_word_to_mod
)
1391 BITMAP_WORD mask
= first_mask
& last_mask
;
1392 elt
->bits
[first_word_to_mod
] &= ~mask
;
1396 elt
->bits
[first_word_to_mod
] &= ~first_mask
;
1397 if (BITMAP_ELEMENT_WORDS
> 2)
1398 for (i
= first_word_to_mod
+ 1; i
< last_word_to_mod
; i
++)
1400 elt
->bits
[last_word_to_mod
] &= ~last_mask
;
1402 for (i
= 0; i
< BITMAP_ELEMENT_WORDS
; i
++)
1408 /* Check to see if there are any bits left. */
1410 bitmap_element_free (head
, elt
);
1417 head
->current
= elt
;
1418 head
->indx
= head
->current
->indx
;
1425 bitmap_compl_and_into (bitmap a
, const_bitmap b
)
1427 bitmap_element
*a_elt
= a
->first
;
1428 const bitmap_element
*b_elt
= b
->first
;
1429 bitmap_element
*a_prev
= NULL
;
1430 bitmap_element
*next
;
1432 gcc_assert (a
!= b
);
1434 if (bitmap_empty_p (a
))
1439 if (bitmap_empty_p (b
))
1445 while (a_elt
|| b_elt
)
1447 if (!b_elt
|| (a_elt
&& a_elt
->indx
< b_elt
->indx
))
1449 /* A is before B. Remove A */
1451 a_prev
= a_elt
->prev
;
1452 bitmap_element_free (a
, a_elt
);
1455 else if (!a_elt
|| b_elt
->indx
< a_elt
->indx
)
1457 /* B is before A. Copy B. */
1458 next
= bitmap_elt_insert_after (a
, a_prev
, b_elt
->indx
);
1459 memcpy (next
->bits
, b_elt
->bits
, sizeof (next
->bits
));
1461 b_elt
= b_elt
->next
;
1465 /* Matching elts, generate A = ~A & B. */
1467 BITMAP_WORD ior
= 0;
1469 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1471 BITMAP_WORD cleared
= a_elt
->bits
[ix
] & b_elt
->bits
[ix
];
1472 BITMAP_WORD r
= b_elt
->bits
[ix
] ^ cleared
;
1474 a_elt
->bits
[ix
] = r
;
1479 bitmap_element_free (a
, a_elt
);
1483 b_elt
= b_elt
->next
;
1486 gcc_checking_assert (!a
->current
== !a
->first
1487 && (!a
->current
|| a
->indx
== a
->current
->indx
));
1492 /* Insert an element corresponding to A_ELT | B_ELT after DST_PREV,
1493 overwriting DST_ELT if non-NULL. CHANGED is true if the destination bitmap
1494 had already been changed; the new value of CHANGED is returned. */
1497 bitmap_elt_ior (bitmap dst
, bitmap_element
*dst_elt
, bitmap_element
*dst_prev
,
1498 const bitmap_element
*a_elt
, const bitmap_element
*b_elt
,
1501 gcc_assert (a_elt
|| b_elt
);
1503 if (a_elt
&& b_elt
&& a_elt
->indx
== b_elt
->indx
)
1505 /* Matching elts, generate A | B. */
1508 if (!changed
&& dst_elt
&& dst_elt
->indx
== a_elt
->indx
)
1510 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1512 BITMAP_WORD r
= a_elt
->bits
[ix
] | b_elt
->bits
[ix
];
1513 if (r
!= dst_elt
->bits
[ix
])
1515 dst_elt
->bits
[ix
] = r
;
1524 dst_elt
= bitmap_elt_insert_after (dst
, dst_prev
, a_elt
->indx
);
1526 dst_elt
->indx
= a_elt
->indx
;
1527 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1529 BITMAP_WORD r
= a_elt
->bits
[ix
] | b_elt
->bits
[ix
];
1530 dst_elt
->bits
[ix
] = r
;
1536 /* Copy a single element. */
1537 const bitmap_element
*src
;
1539 if (!b_elt
|| (a_elt
&& a_elt
->indx
< b_elt
->indx
))
1544 gcc_checking_assert (src
);
1545 changed
= bitmap_elt_copy (dst
, dst_elt
, dst_prev
, src
, changed
);
1551 /* DST = A | B. Return true if DST changes. */
1554 bitmap_ior (bitmap dst
, const_bitmap a
, const_bitmap b
)
1556 bitmap_element
*dst_elt
= dst
->first
;
1557 const bitmap_element
*a_elt
= a
->first
;
1558 const bitmap_element
*b_elt
= b
->first
;
1559 bitmap_element
*dst_prev
= NULL
;
1560 bitmap_element
**dst_prev_pnext
= &dst
->first
;
1561 bool changed
= false;
1563 gcc_assert (dst
!= a
&& dst
!= b
);
1565 while (a_elt
|| b_elt
)
1567 changed
= bitmap_elt_ior (dst
, dst_elt
, dst_prev
, a_elt
, b_elt
, changed
);
1569 if (a_elt
&& b_elt
&& a_elt
->indx
== b_elt
->indx
)
1571 a_elt
= a_elt
->next
;
1572 b_elt
= b_elt
->next
;
1576 if (a_elt
&& (!b_elt
|| a_elt
->indx
<= b_elt
->indx
))
1577 a_elt
= a_elt
->next
;
1578 else if (b_elt
&& (!a_elt
|| b_elt
->indx
<= a_elt
->indx
))
1579 b_elt
= b_elt
->next
;
1582 dst_prev
= *dst_prev_pnext
;
1583 dst_prev_pnext
= &dst_prev
->next
;
1584 dst_elt
= *dst_prev_pnext
;
1590 /* Ensure that dst->current is valid. */
1591 dst
->current
= dst
->first
;
1592 bitmap_elt_clear_from (dst
, dst_elt
);
1594 gcc_checking_assert (!dst
->current
== !dst
->first
);
1596 dst
->indx
= dst
->current
->indx
;
1600 /* A |= B. Return true if A changes. */
1603 bitmap_ior_into (bitmap a
, const_bitmap b
)
1605 bitmap_element
*a_elt
= a
->first
;
1606 const bitmap_element
*b_elt
= b
->first
;
1607 bitmap_element
*a_prev
= NULL
;
1608 bitmap_element
**a_prev_pnext
= &a
->first
;
1609 bool changed
= false;
1616 /* If A lags behind B, just advance it. */
1617 if (!a_elt
|| a_elt
->indx
== b_elt
->indx
)
1619 changed
= bitmap_elt_ior (a
, a_elt
, a_prev
, a_elt
, b_elt
, changed
);
1620 b_elt
= b_elt
->next
;
1622 else if (a_elt
->indx
> b_elt
->indx
)
1624 changed
= bitmap_elt_copy (a
, NULL
, a_prev
, b_elt
, changed
);
1625 b_elt
= b_elt
->next
;
1628 a_prev
= *a_prev_pnext
;
1629 a_prev_pnext
= &a_prev
->next
;
1630 a_elt
= *a_prev_pnext
;
1633 gcc_checking_assert (!a
->current
== !a
->first
);
1635 a
->indx
= a
->current
->indx
;
1642 bitmap_xor (bitmap dst
, const_bitmap a
, const_bitmap b
)
1644 bitmap_element
*dst_elt
= dst
->first
;
1645 const bitmap_element
*a_elt
= a
->first
;
1646 const bitmap_element
*b_elt
= b
->first
;
1647 bitmap_element
*dst_prev
= NULL
;
1649 gcc_assert (dst
!= a
&& dst
!= b
);
1656 while (a_elt
|| b_elt
)
1658 if (a_elt
&& b_elt
&& a_elt
->indx
== b_elt
->indx
)
1660 /* Matching elts, generate A ^ B. */
1662 BITMAP_WORD ior
= 0;
1665 dst_elt
= bitmap_elt_insert_after (dst
, dst_prev
, a_elt
->indx
);
1667 dst_elt
->indx
= a_elt
->indx
;
1668 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1670 BITMAP_WORD r
= a_elt
->bits
[ix
] ^ b_elt
->bits
[ix
];
1673 dst_elt
->bits
[ix
] = r
;
1675 a_elt
= a_elt
->next
;
1676 b_elt
= b_elt
->next
;
1680 dst_elt
= dst_elt
->next
;
1685 /* Copy a single element. */
1686 const bitmap_element
*src
;
1688 if (!b_elt
|| (a_elt
&& a_elt
->indx
< b_elt
->indx
))
1691 a_elt
= a_elt
->next
;
1696 b_elt
= b_elt
->next
;
1700 dst_elt
= bitmap_elt_insert_after (dst
, dst_prev
, src
->indx
);
1702 dst_elt
->indx
= src
->indx
;
1703 memcpy (dst_elt
->bits
, src
->bits
, sizeof (dst_elt
->bits
));
1705 dst_elt
= dst_elt
->next
;
1708 /* Ensure that dst->current is valid. */
1709 dst
->current
= dst
->first
;
1710 bitmap_elt_clear_from (dst
, dst_elt
);
1711 gcc_checking_assert (!dst
->current
== !dst
->first
);
1713 dst
->indx
= dst
->current
->indx
;
1719 bitmap_xor_into (bitmap a
, const_bitmap b
)
1721 bitmap_element
*a_elt
= a
->first
;
1722 const bitmap_element
*b_elt
= b
->first
;
1723 bitmap_element
*a_prev
= NULL
;
1733 if (!a_elt
|| b_elt
->indx
< a_elt
->indx
)
1736 bitmap_element
*dst
= bitmap_elt_insert_after (a
, a_prev
, b_elt
->indx
);
1737 memcpy (dst
->bits
, b_elt
->bits
, sizeof (dst
->bits
));
1739 b_elt
= b_elt
->next
;
1741 else if (a_elt
->indx
< b_elt
->indx
)
1744 a_elt
= a_elt
->next
;
1748 /* Matching elts, generate A ^= B. */
1750 BITMAP_WORD ior
= 0;
1751 bitmap_element
*next
= a_elt
->next
;
1753 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1755 BITMAP_WORD r
= a_elt
->bits
[ix
] ^ b_elt
->bits
[ix
];
1758 a_elt
->bits
[ix
] = r
;
1760 b_elt
= b_elt
->next
;
1764 bitmap_element_free (a
, a_elt
);
1768 gcc_checking_assert (!a
->current
== !a
->first
);
1770 a
->indx
= a
->current
->indx
;
1773 /* Return true if two bitmaps are identical.
1774 We do not bother with a check for pointer equality, as that never
1775 occurs in practice. */
1778 bitmap_equal_p (const_bitmap a
, const_bitmap b
)
1780 const bitmap_element
*a_elt
;
1781 const bitmap_element
*b_elt
;
1784 for (a_elt
= a
->first
, b_elt
= b
->first
;
1786 a_elt
= a_elt
->next
, b_elt
= b_elt
->next
)
1788 if (a_elt
->indx
!= b_elt
->indx
)
1790 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1791 if (a_elt
->bits
[ix
] != b_elt
->bits
[ix
])
1794 return !a_elt
&& !b_elt
;
1797 /* Return true if A AND B is not empty. */
1800 bitmap_intersect_p (const_bitmap a
, const_bitmap b
)
1802 const bitmap_element
*a_elt
;
1803 const bitmap_element
*b_elt
;
1806 for (a_elt
= a
->first
, b_elt
= b
->first
;
1809 if (a_elt
->indx
< b_elt
->indx
)
1810 a_elt
= a_elt
->next
;
1811 else if (b_elt
->indx
< a_elt
->indx
)
1812 b_elt
= b_elt
->next
;
1815 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1816 if (a_elt
->bits
[ix
] & b_elt
->bits
[ix
])
1818 a_elt
= a_elt
->next
;
1819 b_elt
= b_elt
->next
;
1825 /* Return true if A AND NOT B is not empty. */
1828 bitmap_intersect_compl_p (const_bitmap a
, const_bitmap b
)
1830 const bitmap_element
*a_elt
;
1831 const bitmap_element
*b_elt
;
1833 for (a_elt
= a
->first
, b_elt
= b
->first
;
1836 if (a_elt
->indx
< b_elt
->indx
)
1838 else if (b_elt
->indx
< a_elt
->indx
)
1839 b_elt
= b_elt
->next
;
1842 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1843 if (a_elt
->bits
[ix
] & ~b_elt
->bits
[ix
])
1845 a_elt
= a_elt
->next
;
1846 b_elt
= b_elt
->next
;
1849 return a_elt
!= NULL
;
1853 /* DST = A | (FROM1 & ~FROM2). Return true if DST changes. */
1856 bitmap_ior_and_compl (bitmap dst
, const_bitmap a
, const_bitmap b
, const_bitmap kill
)
1858 bool changed
= false;
1860 bitmap_element
*dst_elt
= dst
->first
;
1861 const bitmap_element
*a_elt
= a
->first
;
1862 const bitmap_element
*b_elt
= b
->first
;
1863 const bitmap_element
*kill_elt
= kill
->first
;
1864 bitmap_element
*dst_prev
= NULL
;
1865 bitmap_element
**dst_prev_pnext
= &dst
->first
;
1867 gcc_assert (dst
!= a
&& dst
!= b
&& dst
!= kill
);
1869 /* Special cases. We don't bother checking for bitmap_equal_p (b, kill). */
1870 if (b
== kill
|| bitmap_empty_p (b
))
1872 changed
= !bitmap_equal_p (dst
, a
);
1874 bitmap_copy (dst
, a
);
1877 if (bitmap_empty_p (kill
))
1878 return bitmap_ior (dst
, a
, b
);
1879 if (bitmap_empty_p (a
))
1880 return bitmap_and_compl (dst
, b
, kill
);
1882 while (a_elt
|| b_elt
)
1884 bool new_element
= false;
1887 while (kill_elt
&& kill_elt
->indx
< b_elt
->indx
)
1888 kill_elt
= kill_elt
->next
;
1890 if (b_elt
&& kill_elt
&& kill_elt
->indx
== b_elt
->indx
1891 && (!a_elt
|| a_elt
->indx
>= b_elt
->indx
))
1893 bitmap_element tmp_elt
;
1896 BITMAP_WORD ior
= 0;
1897 tmp_elt
.indx
= b_elt
->indx
;
1898 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1900 BITMAP_WORD r
= b_elt
->bits
[ix
] & ~kill_elt
->bits
[ix
];
1902 tmp_elt
.bits
[ix
] = r
;
1907 changed
= bitmap_elt_ior (dst
, dst_elt
, dst_prev
,
1908 a_elt
, &tmp_elt
, changed
);
1910 if (a_elt
&& a_elt
->indx
== b_elt
->indx
)
1911 a_elt
= a_elt
->next
;
1914 b_elt
= b_elt
->next
;
1915 kill_elt
= kill_elt
->next
;
1919 changed
= bitmap_elt_ior (dst
, dst_elt
, dst_prev
,
1920 a_elt
, b_elt
, changed
);
1923 if (a_elt
&& b_elt
&& a_elt
->indx
== b_elt
->indx
)
1925 a_elt
= a_elt
->next
;
1926 b_elt
= b_elt
->next
;
1930 if (a_elt
&& (!b_elt
|| a_elt
->indx
<= b_elt
->indx
))
1931 a_elt
= a_elt
->next
;
1932 else if (b_elt
&& (!a_elt
|| b_elt
->indx
<= a_elt
->indx
))
1933 b_elt
= b_elt
->next
;
1939 dst_prev
= *dst_prev_pnext
;
1940 dst_prev_pnext
= &dst_prev
->next
;
1941 dst_elt
= *dst_prev_pnext
;
1948 /* Ensure that dst->current is valid. */
1949 dst
->current
= dst
->first
;
1950 bitmap_elt_clear_from (dst
, dst_elt
);
1952 gcc_checking_assert (!dst
->current
== !dst
->first
);
1954 dst
->indx
= dst
->current
->indx
;
1959 /* A |= (FROM1 & ~FROM2). Return true if A changes. */
1962 bitmap_ior_and_compl_into (bitmap a
, const_bitmap from1
, const_bitmap from2
)
1967 bitmap_initialize (&tmp
, &bitmap_default_obstack
);
1968 bitmap_and_compl (&tmp
, from1
, from2
);
1969 changed
= bitmap_ior_into (a
, &tmp
);
1970 bitmap_clear (&tmp
);
1975 /* A |= (B & C). Return true if A changes. */
1978 bitmap_ior_and_into (bitmap a
, const_bitmap b
, const_bitmap c
)
1980 bitmap_element
*a_elt
= a
->first
;
1981 const bitmap_element
*b_elt
= b
->first
;
1982 const bitmap_element
*c_elt
= c
->first
;
1983 bitmap_element and_elt
;
1984 bitmap_element
*a_prev
= NULL
;
1985 bitmap_element
**a_prev_pnext
= &a
->first
;
1986 bool changed
= false;
1990 return bitmap_ior_into (a
, b
);
1991 if (bitmap_empty_p (b
) || bitmap_empty_p (c
))
1995 while (b_elt
&& c_elt
)
1997 BITMAP_WORD overall
;
1999 /* Find a common item of B and C. */
2000 while (b_elt
->indx
!= c_elt
->indx
)
2002 if (b_elt
->indx
< c_elt
->indx
)
2004 b_elt
= b_elt
->next
;
2010 c_elt
= c_elt
->next
;
2017 and_elt
.indx
= b_elt
->indx
;
2018 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
2020 and_elt
.bits
[ix
] = b_elt
->bits
[ix
] & c_elt
->bits
[ix
];
2021 overall
|= and_elt
.bits
[ix
];
2024 b_elt
= b_elt
->next
;
2025 c_elt
= c_elt
->next
;
2029 /* Now find a place to insert AND_ELT. */
2032 ix
= a_elt
? a_elt
->indx
: and_elt
.indx
;
2033 if (ix
== and_elt
.indx
)
2034 changed
= bitmap_elt_ior (a
, a_elt
, a_prev
, a_elt
, &and_elt
, changed
);
2035 else if (ix
> and_elt
.indx
)
2036 changed
= bitmap_elt_copy (a
, NULL
, a_prev
, &and_elt
, changed
);
2038 a_prev
= *a_prev_pnext
;
2039 a_prev_pnext
= &a_prev
->next
;
2040 a_elt
= *a_prev_pnext
;
2042 /* If A lagged behind B/C, we advanced it so loop once more. */
2044 while (ix
< and_elt
.indx
);
2048 gcc_checking_assert (!a
->current
== !a
->first
);
2050 a
->indx
= a
->current
->indx
;
2054 /* Compute hash of bitmap (for purposes of hashing). */
2056 bitmap_hash (const_bitmap head
)
2058 const bitmap_element
*ptr
;
2059 BITMAP_WORD hash
= 0;
2062 for (ptr
= head
->first
; ptr
; ptr
= ptr
->next
)
2065 for (ix
= 0; ix
!= BITMAP_ELEMENT_WORDS
; ix
++)
2066 hash
^= ptr
->bits
[ix
];
2068 return (hashval_t
)hash
;
2072 /* Debugging function to print out the contents of a bitmap. */
2075 debug_bitmap_file (FILE *file
, const_bitmap head
)
2077 const bitmap_element
*ptr
;
2079 fprintf (file
, "\nfirst = " HOST_PTR_PRINTF
2080 " current = " HOST_PTR_PRINTF
" indx = %u\n",
2081 (void *) head
->first
, (void *) head
->current
, head
->indx
);
2083 for (ptr
= head
->first
; ptr
; ptr
= ptr
->next
)
2085 unsigned int i
, j
, col
= 26;
2087 fprintf (file
, "\t" HOST_PTR_PRINTF
" next = " HOST_PTR_PRINTF
2088 " prev = " HOST_PTR_PRINTF
" indx = %u\n\t\tbits = {",
2089 (const void*) ptr
, (const void*) ptr
->next
,
2090 (const void*) ptr
->prev
, ptr
->indx
);
2092 for (i
= 0; i
< BITMAP_ELEMENT_WORDS
; i
++)
2093 for (j
= 0; j
< BITMAP_WORD_BITS
; j
++)
2094 if ((ptr
->bits
[i
] >> j
) & 1)
2098 fprintf (file
, "\n\t\t\t");
2102 fprintf (file
, " %u", (ptr
->indx
* BITMAP_ELEMENT_ALL_BITS
2103 + i
* BITMAP_WORD_BITS
+ j
));
2107 fprintf (file
, " }\n");
2111 /* Function to be called from the debugger to print the contents
2115 debug_bitmap (const_bitmap head
)
2117 debug_bitmap_file (stderr
, head
);
2120 /* Function to print out the contents of a bitmap. Unlike debug_bitmap_file,
2121 it does not print anything but the bits. */
2124 bitmap_print (FILE *file
, const_bitmap head
, const char *prefix
,
2127 const char *comma
= "";
2131 fputs (prefix
, file
);
2132 EXECUTE_IF_SET_IN_BITMAP (head
, 0, i
, bi
)
2134 fprintf (file
, "%s%d", comma
, i
);
2137 fputs (suffix
, file
);
2140 /* Output per-bitmap memory usage statistics. */
2142 dump_bitmap_statistics (void)
2144 if (!GATHER_STATISTICS
)
2147 bitmap_mem_desc
.dump (BITMAP_ORIGIN
);
2151 debug (const bitmap_head
&ref
)
2153 dump_bitmap (stderr
, &ref
);
2157 debug (const bitmap_head
*ptr
)
2162 fprintf (stderr
, "<nil>\n");
2166 #include "gt-bitmap.h"