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
, int 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 /* Find a bitmap element that would hold a bitmap's bit.
473 Update the `current' field even if we can't find an element that
474 would hold the bitmap's bit to make eventual allocation
477 static inline bitmap_element
*
478 bitmap_find_bit (bitmap head
, unsigned int bit
)
480 bitmap_element
*element
;
481 unsigned int indx
= bit
/ BITMAP_ELEMENT_ALL_BITS
;
483 if (head
->current
== NULL
484 || head
->indx
== indx
)
485 return head
->current
;
486 if (head
->current
== head
->first
487 && head
->first
->next
== NULL
)
490 /* Usage can be NULL due to allocated bitmaps for which we do not
491 call initialize function. */
492 bitmap_usage
*usage
= NULL
;
493 if (GATHER_STATISTICS
)
494 usage
= bitmap_mem_desc
.get_descriptor_for_instance (head
);
496 /* This bitmap has more than one element, and we're going to look
497 through the elements list. Count that as a search. */
498 if (GATHER_STATISTICS
&& usage
)
499 usage
->m_nsearches
++;
501 if (head
->indx
< indx
)
502 /* INDX is beyond head->indx. Search from head->current
504 for (element
= head
->current
;
505 element
->next
!= 0 && element
->indx
< indx
;
506 element
= element
->next
)
508 if (GATHER_STATISTICS
&& usage
)
509 usage
->m_search_iter
++;
512 else if (head
->indx
/ 2 < indx
)
513 /* INDX is less than head->indx and closer to head->indx than to
514 0. Search from head->current backward. */
515 for (element
= head
->current
;
516 element
->prev
!= 0 && element
->indx
> indx
;
517 element
= element
->prev
)
519 if (GATHER_STATISTICS
&& usage
)
520 usage
->m_search_iter
++;
524 /* INDX is less than head->indx and closer to 0 than to
525 head->indx. Search from head->first forward. */
526 for (element
= head
->first
;
527 element
->next
!= 0 && element
->indx
< indx
;
528 element
= element
->next
)
529 if (GATHER_STATISTICS
&& usage
)
531 usage
->m_search_iter
++;
534 /* `element' is the nearest to the one we want. If it's not the one we
535 want, the one we want doesn't exist. */
536 head
->current
= element
;
537 head
->indx
= element
->indx
;
538 if (element
!= 0 && element
->indx
!= indx
)
544 /* Clear a single bit in a bitmap. Return true if the bit changed. */
547 bitmap_clear_bit (bitmap head
, int bit
)
549 bitmap_element
*const ptr
= bitmap_find_bit (head
, bit
);
553 unsigned bit_num
= bit
% BITMAP_WORD_BITS
;
554 unsigned word_num
= bit
/ BITMAP_WORD_BITS
% BITMAP_ELEMENT_WORDS
;
555 BITMAP_WORD bit_val
= ((BITMAP_WORD
) 1) << bit_num
;
556 bool res
= (ptr
->bits
[word_num
] & bit_val
) != 0;
559 ptr
->bits
[word_num
] &= ~bit_val
;
560 /* If we cleared the entire word, free up the element. */
561 if (!ptr
->bits
[word_num
]
562 && bitmap_element_zerop (ptr
))
563 bitmap_element_free (head
, ptr
);
572 /* Set a single bit in a bitmap. Return true if the bit changed. */
575 bitmap_set_bit (bitmap head
, int bit
)
577 bitmap_element
*ptr
= bitmap_find_bit (head
, bit
);
578 unsigned word_num
= bit
/ BITMAP_WORD_BITS
% BITMAP_ELEMENT_WORDS
;
579 unsigned bit_num
= bit
% BITMAP_WORD_BITS
;
580 BITMAP_WORD bit_val
= ((BITMAP_WORD
) 1) << bit_num
;
584 ptr
= bitmap_element_allocate (head
);
585 ptr
->indx
= bit
/ BITMAP_ELEMENT_ALL_BITS
;
586 ptr
->bits
[word_num
] = bit_val
;
587 bitmap_element_link (head
, ptr
);
592 bool res
= (ptr
->bits
[word_num
] & bit_val
) == 0;
594 ptr
->bits
[word_num
] |= bit_val
;
599 /* Return whether a bit is set within a bitmap. */
602 bitmap_bit_p (bitmap head
, int bit
)
608 ptr
= bitmap_find_bit (head
, bit
);
612 bit_num
= bit
% BITMAP_WORD_BITS
;
613 word_num
= bit
/ BITMAP_WORD_BITS
% BITMAP_ELEMENT_WORDS
;
615 return (ptr
->bits
[word_num
] >> bit_num
) & 1;
618 #if GCC_VERSION < 3400
619 /* Table of number of set bits in a character, indexed by value of char. */
620 static const unsigned char popcount_table
[] =
622 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,
623 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,
624 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,
625 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,
626 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,
627 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,
628 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,
629 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,
633 bitmap_popcount (BITMAP_WORD a
)
635 unsigned long ret
= 0;
638 /* Just do this the table way for now */
639 for (i
= 0; i
< BITMAP_WORD_BITS
; i
+= 8)
640 ret
+= popcount_table
[(a
>> i
) & 0xff];
644 /* Count the number of bits set in the bitmap, and return it. */
647 bitmap_count_bits (const_bitmap a
)
649 unsigned long count
= 0;
650 const bitmap_element
*elt
;
653 for (elt
= a
->first
; elt
; elt
= elt
->next
)
655 for (ix
= 0; ix
!= BITMAP_ELEMENT_WORDS
; ix
++)
657 #if GCC_VERSION >= 3400
658 /* Note that popcountl matches BITMAP_WORD in type, so the actual size
659 of BITMAP_WORD is not material. */
660 count
+= __builtin_popcountl (elt
->bits
[ix
]);
662 count
+= bitmap_popcount (elt
->bits
[ix
]);
669 /* Return true if the bitmap has a single bit set. Otherwise return
673 bitmap_single_bit_set_p (const_bitmap a
)
675 unsigned long count
= 0;
676 const bitmap_element
*elt
;
679 if (bitmap_empty_p (a
))
683 /* As there are no completely empty bitmap elements, a second one
684 means we have more than one bit set. */
685 if (elt
->next
!= NULL
)
688 for (ix
= 0; ix
!= BITMAP_ELEMENT_WORDS
; ix
++)
690 #if GCC_VERSION >= 3400
691 /* Note that popcountl matches BITMAP_WORD in type, so the actual size
692 of BITMAP_WORD is not material. */
693 count
+= __builtin_popcountl (elt
->bits
[ix
]);
695 count
+= bitmap_popcount (elt
->bits
[ix
]);
705 /* Return the bit number of the first set bit in the bitmap. The
706 bitmap must be non-empty. */
709 bitmap_first_set_bit (const_bitmap a
)
711 const bitmap_element
*elt
= a
->first
;
716 gcc_checking_assert (elt
);
717 bit_no
= elt
->indx
* BITMAP_ELEMENT_ALL_BITS
;
718 for (ix
= 0; ix
!= BITMAP_ELEMENT_WORDS
; ix
++)
720 word
= elt
->bits
[ix
];
726 bit_no
+= ix
* BITMAP_WORD_BITS
;
728 #if GCC_VERSION >= 3004
729 gcc_assert (sizeof (long) == sizeof (word
));
730 bit_no
+= __builtin_ctzl (word
);
732 /* Binary search for the first set bit. */
733 #if BITMAP_WORD_BITS > 64
734 #error "Fill out the table."
736 #if BITMAP_WORD_BITS > 32
737 if (!(word
& 0xffffffff))
738 word
>>= 32, bit_no
+= 32;
740 if (!(word
& 0xffff))
741 word
>>= 16, bit_no
+= 16;
743 word
>>= 8, bit_no
+= 8;
745 word
>>= 4, bit_no
+= 4;
747 word
>>= 2, bit_no
+= 2;
749 word
>>= 1, bit_no
+= 1;
751 gcc_checking_assert (word
& 1);
756 /* Return the bit number of the first set bit in the bitmap. The
757 bitmap must be non-empty. */
760 bitmap_last_set_bit (const_bitmap a
)
762 const bitmap_element
*elt
= a
->current
? a
->current
: a
->first
;
767 gcc_checking_assert (elt
);
770 bit_no
= elt
->indx
* BITMAP_ELEMENT_ALL_BITS
;
771 for (ix
= BITMAP_ELEMENT_WORDS
- 1; ix
>= 0; ix
--)
773 word
= elt
->bits
[ix
];
779 bit_no
+= ix
* BITMAP_WORD_BITS
;
780 #if GCC_VERSION >= 3004
781 gcc_assert (sizeof (long) == sizeof (word
));
782 bit_no
+= BITMAP_WORD_BITS
- __builtin_clzl (word
) - 1;
784 /* Hopefully this is a twos-complement host... */
785 BITMAP_WORD x
= word
;
791 #if BITMAP_WORD_BITS > 32
794 bit_no
+= bitmap_popcount (x
) - 1;
804 bitmap_and (bitmap dst
, const_bitmap a
, const_bitmap b
)
806 bitmap_element
*dst_elt
= dst
->first
;
807 const bitmap_element
*a_elt
= a
->first
;
808 const bitmap_element
*b_elt
= b
->first
;
809 bitmap_element
*dst_prev
= NULL
;
811 gcc_assert (dst
!= a
&& dst
!= b
);
815 bitmap_copy (dst
, a
);
819 while (a_elt
&& b_elt
)
821 if (a_elt
->indx
< b_elt
->indx
)
823 else if (b_elt
->indx
< a_elt
->indx
)
827 /* Matching elts, generate A & B. */
832 dst_elt
= bitmap_elt_insert_after (dst
, dst_prev
, a_elt
->indx
);
834 dst_elt
->indx
= a_elt
->indx
;
835 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
837 BITMAP_WORD r
= a_elt
->bits
[ix
] & b_elt
->bits
[ix
];
839 dst_elt
->bits
[ix
] = r
;
845 dst_elt
= dst_elt
->next
;
851 /* Ensure that dst->current is valid. */
852 dst
->current
= dst
->first
;
853 bitmap_elt_clear_from (dst
, dst_elt
);
854 gcc_checking_assert (!dst
->current
== !dst
->first
);
856 dst
->indx
= dst
->current
->indx
;
859 /* A &= B. Return true if A changed. */
862 bitmap_and_into (bitmap a
, const_bitmap b
)
864 bitmap_element
*a_elt
= a
->first
;
865 const bitmap_element
*b_elt
= b
->first
;
866 bitmap_element
*next
;
867 bool changed
= false;
872 while (a_elt
&& b_elt
)
874 if (a_elt
->indx
< b_elt
->indx
)
877 bitmap_element_free (a
, a_elt
);
881 else if (b_elt
->indx
< a_elt
->indx
)
885 /* Matching elts, generate A &= B. */
889 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
891 BITMAP_WORD r
= a_elt
->bits
[ix
] & b_elt
->bits
[ix
];
892 if (a_elt
->bits
[ix
] != r
)
899 bitmap_element_free (a
, a_elt
);
908 bitmap_elt_clear_from (a
, a_elt
);
911 gcc_checking_assert (!a
->current
== !a
->first
912 && (!a
->current
|| a
->indx
== a
->current
->indx
));
918 /* Insert an element equal to SRC_ELT after DST_PREV, overwriting DST_ELT
919 if non-NULL. CHANGED is true if the destination bitmap had already been
920 changed; the new value of CHANGED is returned. */
923 bitmap_elt_copy (bitmap dst
, bitmap_element
*dst_elt
, bitmap_element
*dst_prev
,
924 const bitmap_element
*src_elt
, bool changed
)
926 if (!changed
&& dst_elt
&& dst_elt
->indx
== src_elt
->indx
)
930 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
931 if (src_elt
->bits
[ix
] != dst_elt
->bits
[ix
])
933 dst_elt
->bits
[ix
] = src_elt
->bits
[ix
];
941 dst_elt
= bitmap_elt_insert_after (dst
, dst_prev
, src_elt
->indx
);
943 dst_elt
->indx
= src_elt
->indx
;
944 memcpy (dst_elt
->bits
, src_elt
->bits
, sizeof (dst_elt
->bits
));
954 bitmap_and_compl (bitmap dst
, const_bitmap a
, const_bitmap b
)
956 bitmap_element
*dst_elt
= dst
->first
;
957 const bitmap_element
*a_elt
= a
->first
;
958 const bitmap_element
*b_elt
= b
->first
;
959 bitmap_element
*dst_prev
= NULL
;
960 bitmap_element
**dst_prev_pnext
= &dst
->first
;
961 bool changed
= false;
963 gcc_assert (dst
!= a
&& dst
!= b
);
967 changed
= !bitmap_empty_p (dst
);
974 while (b_elt
&& b_elt
->indx
< a_elt
->indx
)
977 if (!b_elt
|| b_elt
->indx
> a_elt
->indx
)
979 changed
= bitmap_elt_copy (dst
, dst_elt
, dst_prev
, a_elt
, changed
);
980 dst_prev
= *dst_prev_pnext
;
981 dst_prev_pnext
= &dst_prev
->next
;
982 dst_elt
= *dst_prev_pnext
;
988 /* Matching elts, generate A & ~B. */
992 if (!changed
&& dst_elt
&& dst_elt
->indx
== a_elt
->indx
)
994 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
996 BITMAP_WORD r
= a_elt
->bits
[ix
] & ~b_elt
->bits
[ix
];
998 if (dst_elt
->bits
[ix
] != r
)
1001 dst_elt
->bits
[ix
] = r
;
1009 if (!dst_elt
|| dst_elt
->indx
> a_elt
->indx
)
1011 dst_elt
= bitmap_elt_insert_after (dst
, dst_prev
, a_elt
->indx
);
1016 dst_elt
->indx
= a_elt
->indx
;
1017 new_element
= false;
1020 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1022 BITMAP_WORD r
= a_elt
->bits
[ix
] & ~b_elt
->bits
[ix
];
1024 dst_elt
->bits
[ix
] = r
;
1032 changed
|= !new_element
;
1033 bitmap_element_free (dst
, dst_elt
);
1034 dst_elt
= *dst_prev_pnext
;
1040 dst_prev
= *dst_prev_pnext
;
1041 dst_prev_pnext
= &dst_prev
->next
;
1042 dst_elt
= *dst_prev_pnext
;
1044 a_elt
= a_elt
->next
;
1045 b_elt
= b_elt
->next
;
1049 /* Ensure that dst->current is valid. */
1050 dst
->current
= dst
->first
;
1055 bitmap_elt_clear_from (dst
, dst_elt
);
1057 gcc_checking_assert (!dst
->current
== !dst
->first
);
1059 dst
->indx
= dst
->current
->indx
;
1064 /* A &= ~B. Returns true if A changes */
1067 bitmap_and_compl_into (bitmap a
, const_bitmap b
)
1069 bitmap_element
*a_elt
= a
->first
;
1070 const bitmap_element
*b_elt
= b
->first
;
1071 bitmap_element
*next
;
1072 BITMAP_WORD changed
= 0;
1076 if (bitmap_empty_p (a
))
1085 while (a_elt
&& b_elt
)
1087 if (a_elt
->indx
< b_elt
->indx
)
1088 a_elt
= a_elt
->next
;
1089 else if (b_elt
->indx
< a_elt
->indx
)
1090 b_elt
= b_elt
->next
;
1093 /* Matching elts, generate A &= ~B. */
1095 BITMAP_WORD ior
= 0;
1097 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1099 BITMAP_WORD cleared
= a_elt
->bits
[ix
] & b_elt
->bits
[ix
];
1100 BITMAP_WORD r
= a_elt
->bits
[ix
] ^ cleared
;
1102 a_elt
->bits
[ix
] = r
;
1108 bitmap_element_free (a
, a_elt
);
1110 b_elt
= b_elt
->next
;
1113 gcc_checking_assert (!a
->current
== !a
->first
1114 && (!a
->current
|| a
->indx
== a
->current
->indx
));
1115 return changed
!= 0;
1118 /* Set COUNT bits from START in HEAD. */
1120 bitmap_set_range (bitmap head
, unsigned int start
, unsigned int count
)
1122 unsigned int first_index
, end_bit_plus1
, last_index
;
1123 bitmap_element
*elt
, *elt_prev
;
1131 bitmap_set_bit (head
, start
);
1135 first_index
= start
/ BITMAP_ELEMENT_ALL_BITS
;
1136 end_bit_plus1
= start
+ count
;
1137 last_index
= (end_bit_plus1
- 1) / BITMAP_ELEMENT_ALL_BITS
;
1138 elt
= bitmap_find_bit (head
, start
);
1140 /* If bitmap_find_bit returns zero, the current is the closest block
1141 to the result. Otherwise, just use bitmap_element_allocate to
1142 ensure ELT is set; in the loop below, ELT == NULL means "insert
1143 at the end of the bitmap". */
1146 elt
= bitmap_element_allocate (head
);
1147 elt
->indx
= first_index
;
1148 bitmap_element_link (head
, elt
);
1151 gcc_checking_assert (elt
->indx
== first_index
);
1152 elt_prev
= elt
->prev
;
1153 for (i
= first_index
; i
<= last_index
; i
++)
1155 unsigned elt_start_bit
= i
* BITMAP_ELEMENT_ALL_BITS
;
1156 unsigned elt_end_bit_plus1
= elt_start_bit
+ BITMAP_ELEMENT_ALL_BITS
;
1158 unsigned int first_word_to_mod
;
1159 BITMAP_WORD first_mask
;
1160 unsigned int last_word_to_mod
;
1161 BITMAP_WORD last_mask
;
1164 if (!elt
|| elt
->indx
!= i
)
1165 elt
= bitmap_elt_insert_after (head
, elt_prev
, i
);
1167 if (elt_start_bit
<= start
)
1169 /* The first bit to turn on is somewhere inside this
1171 first_word_to_mod
= (start
- elt_start_bit
) / BITMAP_WORD_BITS
;
1173 /* This mask should have 1s in all bits >= start position. */
1175 (((BITMAP_WORD
) 1) << ((start
% BITMAP_WORD_BITS
))) - 1;
1176 first_mask
= ~first_mask
;
1180 /* The first bit to turn on is below this start of this elt. */
1181 first_word_to_mod
= 0;
1182 first_mask
= ~(BITMAP_WORD
) 0;
1185 if (elt_end_bit_plus1
<= end_bit_plus1
)
1187 /* The last bit to turn on is beyond this elt. */
1188 last_word_to_mod
= BITMAP_ELEMENT_WORDS
- 1;
1189 last_mask
= ~(BITMAP_WORD
) 0;
1193 /* The last bit to turn on is inside to this elt. */
1195 (end_bit_plus1
- elt_start_bit
) / BITMAP_WORD_BITS
;
1197 /* The last mask should have 1s below the end bit. */
1199 (((BITMAP_WORD
) 1) << ((end_bit_plus1
% BITMAP_WORD_BITS
))) - 1;
1202 if (first_word_to_mod
== last_word_to_mod
)
1204 BITMAP_WORD mask
= first_mask
& last_mask
;
1205 elt
->bits
[first_word_to_mod
] |= mask
;
1209 elt
->bits
[first_word_to_mod
] |= first_mask
;
1210 if (BITMAP_ELEMENT_WORDS
> 2)
1211 for (ix
= first_word_to_mod
+ 1; ix
< last_word_to_mod
; ix
++)
1212 elt
->bits
[ix
] = ~(BITMAP_WORD
) 0;
1213 elt
->bits
[last_word_to_mod
] |= last_mask
;
1220 head
->current
= elt
? elt
: elt_prev
;
1221 head
->indx
= head
->current
->indx
;
1224 /* Clear COUNT bits from START in HEAD. */
1226 bitmap_clear_range (bitmap head
, unsigned int start
, unsigned int count
)
1228 unsigned int first_index
, end_bit_plus1
, last_index
;
1229 bitmap_element
*elt
;
1236 bitmap_clear_bit (head
, start
);
1240 first_index
= start
/ BITMAP_ELEMENT_ALL_BITS
;
1241 end_bit_plus1
= start
+ count
;
1242 last_index
= (end_bit_plus1
- 1) / BITMAP_ELEMENT_ALL_BITS
;
1243 elt
= bitmap_find_bit (head
, start
);
1245 /* If bitmap_find_bit returns zero, the current is the closest block
1246 to the result. If the current is less than first index, find the
1247 next one. Otherwise, just set elt to be current. */
1252 if (head
->indx
< first_index
)
1254 elt
= head
->current
->next
;
1259 elt
= head
->current
;
1265 while (elt
&& (elt
->indx
<= last_index
))
1267 bitmap_element
* next_elt
= elt
->next
;
1268 unsigned elt_start_bit
= (elt
->indx
) * BITMAP_ELEMENT_ALL_BITS
;
1269 unsigned elt_end_bit_plus1
= elt_start_bit
+ BITMAP_ELEMENT_ALL_BITS
;
1272 if (elt_start_bit
>= start
&& elt_end_bit_plus1
<= end_bit_plus1
)
1273 /* Get rid of the entire elt and go to the next one. */
1274 bitmap_element_free (head
, elt
);
1277 /* Going to have to knock out some bits in this elt. */
1278 unsigned int first_word_to_mod
;
1279 BITMAP_WORD first_mask
;
1280 unsigned int last_word_to_mod
;
1281 BITMAP_WORD last_mask
;
1285 if (elt_start_bit
<= start
)
1287 /* The first bit to turn off is somewhere inside this
1289 first_word_to_mod
= (start
- elt_start_bit
) / BITMAP_WORD_BITS
;
1291 /* This mask should have 1s in all bits >= start position. */
1293 (((BITMAP_WORD
) 1) << ((start
% BITMAP_WORD_BITS
))) - 1;
1294 first_mask
= ~first_mask
;
1298 /* The first bit to turn off is below this start of this elt. */
1299 first_word_to_mod
= 0;
1301 first_mask
= ~first_mask
;
1304 if (elt_end_bit_plus1
<= end_bit_plus1
)
1306 /* The last bit to turn off is beyond this elt. */
1307 last_word_to_mod
= BITMAP_ELEMENT_WORDS
- 1;
1309 last_mask
= ~last_mask
;
1313 /* The last bit to turn off is inside to this elt. */
1315 (end_bit_plus1
- elt_start_bit
) / BITMAP_WORD_BITS
;
1317 /* The last mask should have 1s below the end bit. */
1319 (((BITMAP_WORD
) 1) << (((end_bit_plus1
) % BITMAP_WORD_BITS
))) - 1;
1323 if (first_word_to_mod
== last_word_to_mod
)
1325 BITMAP_WORD mask
= first_mask
& last_mask
;
1326 elt
->bits
[first_word_to_mod
] &= ~mask
;
1330 elt
->bits
[first_word_to_mod
] &= ~first_mask
;
1331 if (BITMAP_ELEMENT_WORDS
> 2)
1332 for (i
= first_word_to_mod
+ 1; i
< last_word_to_mod
; i
++)
1334 elt
->bits
[last_word_to_mod
] &= ~last_mask
;
1336 for (i
= 0; i
< BITMAP_ELEMENT_WORDS
; i
++)
1342 /* Check to see if there are any bits left. */
1344 bitmap_element_free (head
, elt
);
1351 head
->current
= elt
;
1352 head
->indx
= head
->current
->indx
;
1359 bitmap_compl_and_into (bitmap a
, const_bitmap b
)
1361 bitmap_element
*a_elt
= a
->first
;
1362 const bitmap_element
*b_elt
= b
->first
;
1363 bitmap_element
*a_prev
= NULL
;
1364 bitmap_element
*next
;
1366 gcc_assert (a
!= b
);
1368 if (bitmap_empty_p (a
))
1373 if (bitmap_empty_p (b
))
1379 while (a_elt
|| b_elt
)
1381 if (!b_elt
|| (a_elt
&& a_elt
->indx
< b_elt
->indx
))
1383 /* A is before B. Remove A */
1385 a_prev
= a_elt
->prev
;
1386 bitmap_element_free (a
, a_elt
);
1389 else if (!a_elt
|| b_elt
->indx
< a_elt
->indx
)
1391 /* B is before A. Copy B. */
1392 next
= bitmap_elt_insert_after (a
, a_prev
, b_elt
->indx
);
1393 memcpy (next
->bits
, b_elt
->bits
, sizeof (next
->bits
));
1395 b_elt
= b_elt
->next
;
1399 /* Matching elts, generate A = ~A & B. */
1401 BITMAP_WORD ior
= 0;
1403 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1405 BITMAP_WORD cleared
= a_elt
->bits
[ix
] & b_elt
->bits
[ix
];
1406 BITMAP_WORD r
= b_elt
->bits
[ix
] ^ cleared
;
1408 a_elt
->bits
[ix
] = r
;
1413 bitmap_element_free (a
, a_elt
);
1417 b_elt
= b_elt
->next
;
1420 gcc_checking_assert (!a
->current
== !a
->first
1421 && (!a
->current
|| a
->indx
== a
->current
->indx
));
1426 /* Insert an element corresponding to A_ELT | B_ELT after DST_PREV,
1427 overwriting DST_ELT if non-NULL. CHANGED is true if the destination bitmap
1428 had already been changed; the new value of CHANGED is returned. */
1431 bitmap_elt_ior (bitmap dst
, bitmap_element
*dst_elt
, bitmap_element
*dst_prev
,
1432 const bitmap_element
*a_elt
, const bitmap_element
*b_elt
,
1435 gcc_assert (a_elt
|| b_elt
);
1437 if (a_elt
&& b_elt
&& a_elt
->indx
== b_elt
->indx
)
1439 /* Matching elts, generate A | B. */
1442 if (!changed
&& dst_elt
&& dst_elt
->indx
== a_elt
->indx
)
1444 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1446 BITMAP_WORD r
= a_elt
->bits
[ix
] | b_elt
->bits
[ix
];
1447 if (r
!= dst_elt
->bits
[ix
])
1449 dst_elt
->bits
[ix
] = r
;
1458 dst_elt
= bitmap_elt_insert_after (dst
, dst_prev
, a_elt
->indx
);
1460 dst_elt
->indx
= a_elt
->indx
;
1461 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1463 BITMAP_WORD r
= a_elt
->bits
[ix
] | b_elt
->bits
[ix
];
1464 dst_elt
->bits
[ix
] = r
;
1470 /* Copy a single element. */
1471 const bitmap_element
*src
;
1473 if (!b_elt
|| (a_elt
&& a_elt
->indx
< b_elt
->indx
))
1478 gcc_checking_assert (src
);
1479 changed
= bitmap_elt_copy (dst
, dst_elt
, dst_prev
, src
, changed
);
1485 /* DST = A | B. Return true if DST changes. */
1488 bitmap_ior (bitmap dst
, const_bitmap a
, const_bitmap b
)
1490 bitmap_element
*dst_elt
= dst
->first
;
1491 const bitmap_element
*a_elt
= a
->first
;
1492 const bitmap_element
*b_elt
= b
->first
;
1493 bitmap_element
*dst_prev
= NULL
;
1494 bitmap_element
**dst_prev_pnext
= &dst
->first
;
1495 bool changed
= false;
1497 gcc_assert (dst
!= a
&& dst
!= b
);
1499 while (a_elt
|| b_elt
)
1501 changed
= bitmap_elt_ior (dst
, dst_elt
, dst_prev
, a_elt
, b_elt
, changed
);
1503 if (a_elt
&& b_elt
&& a_elt
->indx
== b_elt
->indx
)
1505 a_elt
= a_elt
->next
;
1506 b_elt
= b_elt
->next
;
1510 if (a_elt
&& (!b_elt
|| a_elt
->indx
<= b_elt
->indx
))
1511 a_elt
= a_elt
->next
;
1512 else if (b_elt
&& (!a_elt
|| b_elt
->indx
<= a_elt
->indx
))
1513 b_elt
= b_elt
->next
;
1516 dst_prev
= *dst_prev_pnext
;
1517 dst_prev_pnext
= &dst_prev
->next
;
1518 dst_elt
= *dst_prev_pnext
;
1524 /* Ensure that dst->current is valid. */
1525 dst
->current
= dst
->first
;
1526 bitmap_elt_clear_from (dst
, dst_elt
);
1528 gcc_checking_assert (!dst
->current
== !dst
->first
);
1530 dst
->indx
= dst
->current
->indx
;
1534 /* A |= B. Return true if A changes. */
1537 bitmap_ior_into (bitmap a
, const_bitmap b
)
1539 bitmap_element
*a_elt
= a
->first
;
1540 const bitmap_element
*b_elt
= b
->first
;
1541 bitmap_element
*a_prev
= NULL
;
1542 bitmap_element
**a_prev_pnext
= &a
->first
;
1543 bool changed
= false;
1550 /* If A lags behind B, just advance it. */
1551 if (!a_elt
|| a_elt
->indx
== b_elt
->indx
)
1553 changed
= bitmap_elt_ior (a
, a_elt
, a_prev
, a_elt
, b_elt
, changed
);
1554 b_elt
= b_elt
->next
;
1556 else if (a_elt
->indx
> b_elt
->indx
)
1558 changed
= bitmap_elt_copy (a
, NULL
, a_prev
, b_elt
, changed
);
1559 b_elt
= b_elt
->next
;
1562 a_prev
= *a_prev_pnext
;
1563 a_prev_pnext
= &a_prev
->next
;
1564 a_elt
= *a_prev_pnext
;
1567 gcc_checking_assert (!a
->current
== !a
->first
);
1569 a
->indx
= a
->current
->indx
;
1576 bitmap_xor (bitmap dst
, const_bitmap a
, const_bitmap b
)
1578 bitmap_element
*dst_elt
= dst
->first
;
1579 const bitmap_element
*a_elt
= a
->first
;
1580 const bitmap_element
*b_elt
= b
->first
;
1581 bitmap_element
*dst_prev
= NULL
;
1583 gcc_assert (dst
!= a
&& dst
!= b
);
1590 while (a_elt
|| b_elt
)
1592 if (a_elt
&& b_elt
&& a_elt
->indx
== b_elt
->indx
)
1594 /* Matching elts, generate A ^ B. */
1596 BITMAP_WORD ior
= 0;
1599 dst_elt
= bitmap_elt_insert_after (dst
, dst_prev
, a_elt
->indx
);
1601 dst_elt
->indx
= a_elt
->indx
;
1602 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1604 BITMAP_WORD r
= a_elt
->bits
[ix
] ^ b_elt
->bits
[ix
];
1607 dst_elt
->bits
[ix
] = r
;
1609 a_elt
= a_elt
->next
;
1610 b_elt
= b_elt
->next
;
1614 dst_elt
= dst_elt
->next
;
1619 /* Copy a single element. */
1620 const bitmap_element
*src
;
1622 if (!b_elt
|| (a_elt
&& a_elt
->indx
< b_elt
->indx
))
1625 a_elt
= a_elt
->next
;
1630 b_elt
= b_elt
->next
;
1634 dst_elt
= bitmap_elt_insert_after (dst
, dst_prev
, src
->indx
);
1636 dst_elt
->indx
= src
->indx
;
1637 memcpy (dst_elt
->bits
, src
->bits
, sizeof (dst_elt
->bits
));
1639 dst_elt
= dst_elt
->next
;
1642 /* Ensure that dst->current is valid. */
1643 dst
->current
= dst
->first
;
1644 bitmap_elt_clear_from (dst
, dst_elt
);
1645 gcc_checking_assert (!dst
->current
== !dst
->first
);
1647 dst
->indx
= dst
->current
->indx
;
1653 bitmap_xor_into (bitmap a
, const_bitmap b
)
1655 bitmap_element
*a_elt
= a
->first
;
1656 const bitmap_element
*b_elt
= b
->first
;
1657 bitmap_element
*a_prev
= NULL
;
1667 if (!a_elt
|| b_elt
->indx
< a_elt
->indx
)
1670 bitmap_element
*dst
= bitmap_elt_insert_after (a
, a_prev
, b_elt
->indx
);
1671 memcpy (dst
->bits
, b_elt
->bits
, sizeof (dst
->bits
));
1673 b_elt
= b_elt
->next
;
1675 else if (a_elt
->indx
< b_elt
->indx
)
1678 a_elt
= a_elt
->next
;
1682 /* Matching elts, generate A ^= B. */
1684 BITMAP_WORD ior
= 0;
1685 bitmap_element
*next
= a_elt
->next
;
1687 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1689 BITMAP_WORD r
= a_elt
->bits
[ix
] ^ b_elt
->bits
[ix
];
1692 a_elt
->bits
[ix
] = r
;
1694 b_elt
= b_elt
->next
;
1698 bitmap_element_free (a
, a_elt
);
1702 gcc_checking_assert (!a
->current
== !a
->first
);
1704 a
->indx
= a
->current
->indx
;
1707 /* Return true if two bitmaps are identical.
1708 We do not bother with a check for pointer equality, as that never
1709 occurs in practice. */
1712 bitmap_equal_p (const_bitmap a
, const_bitmap b
)
1714 const bitmap_element
*a_elt
;
1715 const bitmap_element
*b_elt
;
1718 for (a_elt
= a
->first
, b_elt
= b
->first
;
1720 a_elt
= a_elt
->next
, b_elt
= b_elt
->next
)
1722 if (a_elt
->indx
!= b_elt
->indx
)
1724 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1725 if (a_elt
->bits
[ix
] != b_elt
->bits
[ix
])
1728 return !a_elt
&& !b_elt
;
1731 /* Return true if A AND B is not empty. */
1734 bitmap_intersect_p (const_bitmap a
, const_bitmap b
)
1736 const bitmap_element
*a_elt
;
1737 const bitmap_element
*b_elt
;
1740 for (a_elt
= a
->first
, b_elt
= b
->first
;
1743 if (a_elt
->indx
< b_elt
->indx
)
1744 a_elt
= a_elt
->next
;
1745 else if (b_elt
->indx
< a_elt
->indx
)
1746 b_elt
= b_elt
->next
;
1749 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1750 if (a_elt
->bits
[ix
] & b_elt
->bits
[ix
])
1752 a_elt
= a_elt
->next
;
1753 b_elt
= b_elt
->next
;
1759 /* Return true if A AND NOT B is not empty. */
1762 bitmap_intersect_compl_p (const_bitmap a
, const_bitmap b
)
1764 const bitmap_element
*a_elt
;
1765 const bitmap_element
*b_elt
;
1767 for (a_elt
= a
->first
, b_elt
= b
->first
;
1770 if (a_elt
->indx
< b_elt
->indx
)
1772 else if (b_elt
->indx
< a_elt
->indx
)
1773 b_elt
= b_elt
->next
;
1776 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1777 if (a_elt
->bits
[ix
] & ~b_elt
->bits
[ix
])
1779 a_elt
= a_elt
->next
;
1780 b_elt
= b_elt
->next
;
1783 return a_elt
!= NULL
;
1787 /* DST = A | (FROM1 & ~FROM2). Return true if DST changes. */
1790 bitmap_ior_and_compl (bitmap dst
, const_bitmap a
, const_bitmap b
, const_bitmap kill
)
1792 bool changed
= false;
1794 bitmap_element
*dst_elt
= dst
->first
;
1795 const bitmap_element
*a_elt
= a
->first
;
1796 const bitmap_element
*b_elt
= b
->first
;
1797 const bitmap_element
*kill_elt
= kill
->first
;
1798 bitmap_element
*dst_prev
= NULL
;
1799 bitmap_element
**dst_prev_pnext
= &dst
->first
;
1801 gcc_assert (dst
!= a
&& dst
!= b
&& dst
!= kill
);
1803 /* Special cases. We don't bother checking for bitmap_equal_p (b, kill). */
1804 if (b
== kill
|| bitmap_empty_p (b
))
1806 changed
= !bitmap_equal_p (dst
, a
);
1808 bitmap_copy (dst
, a
);
1811 if (bitmap_empty_p (kill
))
1812 return bitmap_ior (dst
, a
, b
);
1813 if (bitmap_empty_p (a
))
1814 return bitmap_and_compl (dst
, b
, kill
);
1816 while (a_elt
|| b_elt
)
1818 bool new_element
= false;
1821 while (kill_elt
&& kill_elt
->indx
< b_elt
->indx
)
1822 kill_elt
= kill_elt
->next
;
1824 if (b_elt
&& kill_elt
&& kill_elt
->indx
== b_elt
->indx
1825 && (!a_elt
|| a_elt
->indx
>= b_elt
->indx
))
1827 bitmap_element tmp_elt
;
1830 BITMAP_WORD ior
= 0;
1831 tmp_elt
.indx
= b_elt
->indx
;
1832 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1834 BITMAP_WORD r
= b_elt
->bits
[ix
] & ~kill_elt
->bits
[ix
];
1836 tmp_elt
.bits
[ix
] = r
;
1841 changed
= bitmap_elt_ior (dst
, dst_elt
, dst_prev
,
1842 a_elt
, &tmp_elt
, changed
);
1844 if (a_elt
&& a_elt
->indx
== b_elt
->indx
)
1845 a_elt
= a_elt
->next
;
1848 b_elt
= b_elt
->next
;
1849 kill_elt
= kill_elt
->next
;
1853 changed
= bitmap_elt_ior (dst
, dst_elt
, dst_prev
,
1854 a_elt
, b_elt
, changed
);
1857 if (a_elt
&& b_elt
&& a_elt
->indx
== b_elt
->indx
)
1859 a_elt
= a_elt
->next
;
1860 b_elt
= b_elt
->next
;
1864 if (a_elt
&& (!b_elt
|| a_elt
->indx
<= b_elt
->indx
))
1865 a_elt
= a_elt
->next
;
1866 else if (b_elt
&& (!a_elt
|| b_elt
->indx
<= a_elt
->indx
))
1867 b_elt
= b_elt
->next
;
1873 dst_prev
= *dst_prev_pnext
;
1874 dst_prev_pnext
= &dst_prev
->next
;
1875 dst_elt
= *dst_prev_pnext
;
1882 /* Ensure that dst->current is valid. */
1883 dst
->current
= dst
->first
;
1884 bitmap_elt_clear_from (dst
, dst_elt
);
1886 gcc_checking_assert (!dst
->current
== !dst
->first
);
1888 dst
->indx
= dst
->current
->indx
;
1893 /* A |= (FROM1 & ~FROM2). Return true if A changes. */
1896 bitmap_ior_and_compl_into (bitmap a
, const_bitmap from1
, const_bitmap from2
)
1901 bitmap_initialize (&tmp
, &bitmap_default_obstack
);
1902 bitmap_and_compl (&tmp
, from1
, from2
);
1903 changed
= bitmap_ior_into (a
, &tmp
);
1904 bitmap_clear (&tmp
);
1909 /* A |= (B & C). Return true if A changes. */
1912 bitmap_ior_and_into (bitmap a
, const_bitmap b
, const_bitmap c
)
1914 bitmap_element
*a_elt
= a
->first
;
1915 const bitmap_element
*b_elt
= b
->first
;
1916 const bitmap_element
*c_elt
= c
->first
;
1917 bitmap_element and_elt
;
1918 bitmap_element
*a_prev
= NULL
;
1919 bitmap_element
**a_prev_pnext
= &a
->first
;
1920 bool changed
= false;
1924 return bitmap_ior_into (a
, b
);
1925 if (bitmap_empty_p (b
) || bitmap_empty_p (c
))
1929 while (b_elt
&& c_elt
)
1931 BITMAP_WORD overall
;
1933 /* Find a common item of B and C. */
1934 while (b_elt
->indx
!= c_elt
->indx
)
1936 if (b_elt
->indx
< c_elt
->indx
)
1938 b_elt
= b_elt
->next
;
1944 c_elt
= c_elt
->next
;
1951 and_elt
.indx
= b_elt
->indx
;
1952 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1954 and_elt
.bits
[ix
] = b_elt
->bits
[ix
] & c_elt
->bits
[ix
];
1955 overall
|= and_elt
.bits
[ix
];
1958 b_elt
= b_elt
->next
;
1959 c_elt
= c_elt
->next
;
1963 /* Now find a place to insert AND_ELT. */
1966 ix
= a_elt
? a_elt
->indx
: and_elt
.indx
;
1967 if (ix
== and_elt
.indx
)
1968 changed
= bitmap_elt_ior (a
, a_elt
, a_prev
, a_elt
, &and_elt
, changed
);
1969 else if (ix
> and_elt
.indx
)
1970 changed
= bitmap_elt_copy (a
, NULL
, a_prev
, &and_elt
, changed
);
1972 a_prev
= *a_prev_pnext
;
1973 a_prev_pnext
= &a_prev
->next
;
1974 a_elt
= *a_prev_pnext
;
1976 /* If A lagged behind B/C, we advanced it so loop once more. */
1978 while (ix
< and_elt
.indx
);
1982 gcc_checking_assert (!a
->current
== !a
->first
);
1984 a
->indx
= a
->current
->indx
;
1988 /* Compute hash of bitmap (for purposes of hashing). */
1990 bitmap_hash (const_bitmap head
)
1992 const bitmap_element
*ptr
;
1993 BITMAP_WORD hash
= 0;
1996 for (ptr
= head
->first
; ptr
; ptr
= ptr
->next
)
1999 for (ix
= 0; ix
!= BITMAP_ELEMENT_WORDS
; ix
++)
2000 hash
^= ptr
->bits
[ix
];
2002 return (hashval_t
)hash
;
2006 /* Debugging function to print out the contents of a bitmap. */
2009 debug_bitmap_file (FILE *file
, const_bitmap head
)
2011 const bitmap_element
*ptr
;
2013 fprintf (file
, "\nfirst = " HOST_PTR_PRINTF
2014 " current = " HOST_PTR_PRINTF
" indx = %u\n",
2015 (void *) head
->first
, (void *) head
->current
, head
->indx
);
2017 for (ptr
= head
->first
; ptr
; ptr
= ptr
->next
)
2019 unsigned int i
, j
, col
= 26;
2021 fprintf (file
, "\t" HOST_PTR_PRINTF
" next = " HOST_PTR_PRINTF
2022 " prev = " HOST_PTR_PRINTF
" indx = %u\n\t\tbits = {",
2023 (const void*) ptr
, (const void*) ptr
->next
,
2024 (const void*) ptr
->prev
, ptr
->indx
);
2026 for (i
= 0; i
< BITMAP_ELEMENT_WORDS
; i
++)
2027 for (j
= 0; j
< BITMAP_WORD_BITS
; j
++)
2028 if ((ptr
->bits
[i
] >> j
) & 1)
2032 fprintf (file
, "\n\t\t\t");
2036 fprintf (file
, " %u", (ptr
->indx
* BITMAP_ELEMENT_ALL_BITS
2037 + i
* BITMAP_WORD_BITS
+ j
));
2041 fprintf (file
, " }\n");
2045 /* Function to be called from the debugger to print the contents
2049 debug_bitmap (const_bitmap head
)
2051 debug_bitmap_file (stderr
, head
);
2054 /* Function to print out the contents of a bitmap. Unlike debug_bitmap_file,
2055 it does not print anything but the bits. */
2058 bitmap_print (FILE *file
, const_bitmap head
, const char *prefix
,
2061 const char *comma
= "";
2065 fputs (prefix
, file
);
2066 EXECUTE_IF_SET_IN_BITMAP (head
, 0, i
, bi
)
2068 fprintf (file
, "%s%d", comma
, i
);
2071 fputs (suffix
, file
);
2074 /* Output per-bitmap memory usage statistics. */
2076 dump_bitmap_statistics (void)
2078 if (!GATHER_STATISTICS
)
2081 bitmap_mem_desc
.dump (BITMAP_ORIGIN
);
2085 debug (const bitmap_head
&ref
)
2087 dump_bitmap (stderr
, &ref
);
2091 debug (const bitmap_head
*ptr
)
2096 fprintf (stderr
, "<nil>\n");
2100 #include "gt-bitmap.h"