1 /* Functions to support general ended bitmaps.
2 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2003, 2004, 2005,
3 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
29 #ifdef GATHER_STATISTICS
31 /* Store information about each particular bitmap. */
32 struct bitmap_descriptor
38 HOST_WIDEST_INT allocated
;
40 HOST_WIDEST_INT current
;
45 /* Hashtable mapping bitmap names to descriptors. */
46 static htab_t bitmap_desc_hash
;
48 /* Hashtable helpers. */
50 hash_descriptor (const void *p
)
52 const struct bitmap_descriptor
*const d
=
53 (const struct bitmap_descriptor
*) p
;
54 return htab_hash_pointer (d
->file
) + d
->line
;
63 eq_descriptor (const void *p1
, const void *p2
)
65 const struct bitmap_descriptor
*const d
=
66 (const struct bitmap_descriptor
*) p1
;
67 const struct loc
*const l
= (const struct loc
*) p2
;
68 return d
->file
== l
->file
&& d
->function
== l
->function
&& d
->line
== l
->line
;
71 /* For given file and line, return descriptor, create new if needed. */
72 static struct bitmap_descriptor
*
73 bitmap_descriptor (const char *file
, const char *function
, int line
)
75 struct bitmap_descriptor
**slot
;
79 loc
.function
= function
;
82 if (!bitmap_desc_hash
)
83 bitmap_desc_hash
= htab_create (10, hash_descriptor
, eq_descriptor
, NULL
);
85 slot
= (struct bitmap_descriptor
**)
86 htab_find_slot_with_hash (bitmap_desc_hash
, &loc
,
87 htab_hash_pointer (file
) + line
,
91 *slot
= XCNEW (struct bitmap_descriptor
);
93 (*slot
)->function
= function
;
98 /* Register new bitmap. */
100 bitmap_register (bitmap b MEM_STAT_DECL
)
102 b
->desc
= bitmap_descriptor (_loc_name
, _loc_function
, _loc_line
);
106 /* Account the overhead. */
108 register_overhead (bitmap b
, int amount
)
110 b
->desc
->current
+= amount
;
112 b
->desc
->allocated
+= amount
;
113 gcc_assert (b
->desc
->current
>= 0);
114 if (b
->desc
->peak
< b
->desc
->current
)
115 b
->desc
->peak
= b
->desc
->current
;
120 bitmap_element bitmap_zero_bits
; /* An element of all zero bits. */
121 bitmap_obstack bitmap_default_obstack
; /* The default bitmap obstack. */
122 static int bitmap_default_obstack_depth
;
123 static GTY((deletable
)) bitmap_element
*bitmap_ggc_free
; /* Freelist of
126 static void bitmap_elem_to_freelist (bitmap
, bitmap_element
*);
127 static void bitmap_element_free (bitmap
, bitmap_element
*);
128 static bitmap_element
*bitmap_element_allocate (bitmap
);
129 static int bitmap_element_zerop (const bitmap_element
*);
130 static void bitmap_element_link (bitmap
, bitmap_element
*);
131 static bitmap_element
*bitmap_elt_insert_after (bitmap
, bitmap_element
*, unsigned int);
132 static void bitmap_elt_clear_from (bitmap
, bitmap_element
*);
133 static bitmap_element
*bitmap_find_bit (bitmap
, unsigned int);
136 /* Add ELEM to the appropriate freelist. */
138 bitmap_elem_to_freelist (bitmap head
, bitmap_element
*elt
)
140 bitmap_obstack
*bit_obstack
= head
->obstack
;
145 elt
->prev
= bit_obstack
->elements
;
146 bit_obstack
->elements
= elt
;
150 elt
->prev
= bitmap_ggc_free
;
151 bitmap_ggc_free
= elt
;
155 /* Free a bitmap element. Since these are allocated off the
156 bitmap_obstack, "free" actually means "put onto the freelist". */
159 bitmap_element_free (bitmap head
, bitmap_element
*elt
)
161 bitmap_element
*next
= elt
->next
;
162 bitmap_element
*prev
= elt
->prev
;
170 if (head
->first
== elt
)
173 /* Since the first thing we try is to insert before current,
174 make current the next entry in preference to the previous. */
175 if (head
->current
== elt
)
177 head
->current
= next
!= 0 ? next
: prev
;
179 head
->indx
= head
->current
->indx
;
183 #ifdef GATHER_STATISTICS
184 register_overhead (head
, -((int)sizeof (bitmap_element
)));
186 bitmap_elem_to_freelist (head
, elt
);
189 /* Allocate a bitmap element. The bits are cleared, but nothing else is. */
191 static inline bitmap_element
*
192 bitmap_element_allocate (bitmap head
)
194 bitmap_element
*element
;
195 bitmap_obstack
*bit_obstack
= head
->obstack
;
199 element
= bit_obstack
->elements
;
202 /* Use up the inner list first before looking at the next
203 element of the outer list. */
206 bit_obstack
->elements
= element
->next
;
207 bit_obstack
->elements
->prev
= element
->prev
;
210 /* Inner list was just a singleton. */
211 bit_obstack
->elements
= element
->prev
;
213 element
= XOBNEW (&bit_obstack
->obstack
, bitmap_element
);
217 element
= bitmap_ggc_free
;
219 /* Use up the inner list first before looking at the next
220 element of the outer list. */
223 bitmap_ggc_free
= element
->next
;
224 bitmap_ggc_free
->prev
= element
->prev
;
227 /* Inner list was just a singleton. */
228 bitmap_ggc_free
= element
->prev
;
230 element
= ggc_alloc_bitmap_element_def ();
233 #ifdef GATHER_STATISTICS
234 register_overhead (head
, sizeof (bitmap_element
));
236 memset (element
->bits
, 0, sizeof (element
->bits
));
241 /* Remove ELT and all following elements from bitmap HEAD. */
244 bitmap_elt_clear_from (bitmap head
, bitmap_element
*elt
)
246 bitmap_element
*prev
;
247 bitmap_obstack
*bit_obstack
= head
->obstack
;
248 #ifdef GATHER_STATISTICS
253 #ifdef GATHER_STATISTICS
255 for (prev
= elt
; prev
; prev
= prev
->next
)
257 register_overhead (head
, -sizeof (bitmap_element
) * n
);
264 if (head
->current
->indx
> prev
->indx
)
266 head
->current
= prev
;
267 head
->indx
= prev
->indx
;
273 head
->current
= NULL
;
277 /* Put the entire list onto the free list in one operation. */
280 elt
->prev
= bit_obstack
->elements
;
281 bit_obstack
->elements
= elt
;
285 elt
->prev
= bitmap_ggc_free
;
286 bitmap_ggc_free
= elt
;
290 /* Clear a bitmap by freeing the linked list. */
293 bitmap_clear (bitmap head
)
296 bitmap_elt_clear_from (head
, head
->first
);
299 /* Initialize a bitmap obstack. If BIT_OBSTACK is NULL, initialize
300 the default bitmap obstack. */
303 bitmap_obstack_initialize (bitmap_obstack
*bit_obstack
)
307 if (bitmap_default_obstack_depth
++)
309 bit_obstack
= &bitmap_default_obstack
;
312 #if !defined(__GNUC__) || (__GNUC__ < 2)
313 #define __alignof__(type) 0
316 bit_obstack
->elements
= NULL
;
317 bit_obstack
->heads
= NULL
;
318 obstack_specify_allocation (&bit_obstack
->obstack
, OBSTACK_CHUNK_SIZE
,
319 __alignof__ (bitmap_element
),
324 /* Release the memory from a bitmap obstack. If BIT_OBSTACK is NULL,
325 release the default bitmap obstack. */
328 bitmap_obstack_release (bitmap_obstack
*bit_obstack
)
332 if (--bitmap_default_obstack_depth
)
334 gcc_assert (bitmap_default_obstack_depth
> 0);
337 bit_obstack
= &bitmap_default_obstack
;
340 bit_obstack
->elements
= NULL
;
341 bit_obstack
->heads
= NULL
;
342 obstack_free (&bit_obstack
->obstack
, NULL
);
345 /* Create a new bitmap on an obstack. If BIT_OBSTACK is NULL, create
346 it on the default bitmap obstack. */
349 bitmap_obstack_alloc_stat (bitmap_obstack
*bit_obstack MEM_STAT_DECL
)
354 bit_obstack
= &bitmap_default_obstack
;
355 map
= bit_obstack
->heads
;
357 bit_obstack
->heads
= (struct bitmap_head_def
*) map
->first
;
359 map
= XOBNEW (&bit_obstack
->obstack
, bitmap_head
);
360 bitmap_initialize_stat (map
, bit_obstack PASS_MEM_STAT
);
361 #ifdef GATHER_STATISTICS
362 register_overhead (map
, sizeof (bitmap_head
));
368 /* Create a new GCd bitmap. */
371 bitmap_gc_alloc_stat (ALONE_MEM_STAT_DECL
)
375 map
= ggc_alloc_bitmap_head_def ();
376 bitmap_initialize_stat (map
, NULL PASS_MEM_STAT
);
377 #ifdef GATHER_STATISTICS
378 register_overhead (map
, sizeof (bitmap_head
));
384 /* Release an obstack allocated bitmap. */
387 bitmap_obstack_free (bitmap map
)
392 map
->first
= (bitmap_element
*) map
->obstack
->heads
;
393 #ifdef GATHER_STATISTICS
394 register_overhead (map
, -((int)sizeof (bitmap_head
)));
396 map
->obstack
->heads
= map
;
401 /* Return nonzero if all bits in an element are zero. */
404 bitmap_element_zerop (const bitmap_element
*element
)
406 #if BITMAP_ELEMENT_WORDS == 2
407 return (element
->bits
[0] | element
->bits
[1]) == 0;
411 for (i
= 0; i
< BITMAP_ELEMENT_WORDS
; i
++)
412 if (element
->bits
[i
] != 0)
419 /* Link the bitmap element into the current bitmap linked list. */
422 bitmap_element_link (bitmap head
, bitmap_element
*element
)
424 unsigned int indx
= element
->indx
;
427 /* If this is the first and only element, set it in. */
428 if (head
->first
== 0)
430 element
->next
= element
->prev
= 0;
431 head
->first
= element
;
434 /* If this index is less than that of the current element, it goes someplace
435 before the current element. */
436 else if (indx
< head
->indx
)
438 for (ptr
= head
->current
;
439 ptr
->prev
!= 0 && ptr
->prev
->indx
> indx
;
444 ptr
->prev
->next
= element
;
446 head
->first
= element
;
448 element
->prev
= ptr
->prev
;
453 /* Otherwise, it must go someplace after the current element. */
456 for (ptr
= head
->current
;
457 ptr
->next
!= 0 && ptr
->next
->indx
< indx
;
462 ptr
->next
->prev
= element
;
464 element
->next
= ptr
->next
;
469 /* Set up so this is the first element searched. */
470 head
->current
= element
;
474 /* Insert a new uninitialized element into bitmap HEAD after element
475 ELT. If ELT is NULL, insert the element at the start. Return the
478 static bitmap_element
*
479 bitmap_elt_insert_after (bitmap head
, bitmap_element
*elt
, unsigned int indx
)
481 bitmap_element
*node
= bitmap_element_allocate (head
);
488 head
->current
= node
;
491 node
->next
= head
->first
;
493 node
->next
->prev
= node
;
499 gcc_checking_assert (head
->current
);
500 node
->next
= elt
->next
;
502 node
->next
->prev
= node
;
509 /* Copy a bitmap to another bitmap. */
512 bitmap_copy (bitmap to
, const_bitmap from
)
514 const bitmap_element
*from_ptr
;
515 bitmap_element
*to_ptr
= 0;
519 /* Copy elements in forward direction one at a time. */
520 for (from_ptr
= from
->first
; from_ptr
; from_ptr
= from_ptr
->next
)
522 bitmap_element
*to_elt
= bitmap_element_allocate (to
);
524 to_elt
->indx
= from_ptr
->indx
;
525 memcpy (to_elt
->bits
, from_ptr
->bits
, sizeof (to_elt
->bits
));
527 /* Here we have a special case of bitmap_element_link, for the case
528 where we know the links are being entered in sequence. */
531 to
->first
= to
->current
= to_elt
;
532 to
->indx
= from_ptr
->indx
;
533 to_elt
->next
= to_elt
->prev
= 0;
537 to_elt
->prev
= to_ptr
;
539 to_ptr
->next
= to_elt
;
546 /* Find a bitmap element that would hold a bitmap's bit.
547 Update the `current' field even if we can't find an element that
548 would hold the bitmap's bit to make eventual allocation
551 static inline bitmap_element
*
552 bitmap_find_bit (bitmap head
, unsigned int bit
)
554 bitmap_element
*element
;
555 unsigned int indx
= bit
/ BITMAP_ELEMENT_ALL_BITS
;
557 if (head
->current
== 0
558 || head
->indx
== indx
)
559 return head
->current
;
560 #ifdef GATHER_STATISTICS
561 head
->desc
->nsearches
++;
564 if (head
->indx
< indx
)
565 /* INDX is beyond head->indx. Search from head->current
567 for (element
= head
->current
;
568 element
->next
!= 0 && element
->indx
< indx
;
569 element
= element
->next
)
570 #ifdef GATHER_STATISTICS
571 head
->desc
->search_iter
++;
576 else if (head
->indx
/ 2 < indx
)
577 /* INDX is less than head->indx and closer to head->indx than to
578 0. Search from head->current backward. */
579 for (element
= head
->current
;
580 element
->prev
!= 0 && element
->indx
> indx
;
581 element
= element
->prev
)
582 #ifdef GATHER_STATISTICS
583 head
->desc
->search_iter
++;
589 /* INDX is less than head->indx and closer to 0 than to
590 head->indx. Search from head->first forward. */
591 for (element
= head
->first
;
592 element
->next
!= 0 && element
->indx
< indx
;
593 element
= element
->next
)
594 #ifdef GATHER_STATISTICS
595 head
->desc
->search_iter
++;
600 /* `element' is the nearest to the one we want. If it's not the one we
601 want, the one we want doesn't exist. */
602 head
->current
= element
;
603 head
->indx
= element
->indx
;
604 if (element
!= 0 && element
->indx
!= indx
)
610 /* Clear a single bit in a bitmap. Return true if the bit changed. */
613 bitmap_clear_bit (bitmap head
, int bit
)
615 bitmap_element
*const ptr
= bitmap_find_bit (head
, bit
);
619 unsigned bit_num
= bit
% BITMAP_WORD_BITS
;
620 unsigned word_num
= bit
/ BITMAP_WORD_BITS
% BITMAP_ELEMENT_WORDS
;
621 BITMAP_WORD bit_val
= ((BITMAP_WORD
) 1) << bit_num
;
622 bool res
= (ptr
->bits
[word_num
] & bit_val
) != 0;
625 ptr
->bits
[word_num
] &= ~bit_val
;
626 /* If we cleared the entire word, free up the element. */
627 if (!ptr
->bits
[word_num
]
628 && bitmap_element_zerop (ptr
))
629 bitmap_element_free (head
, ptr
);
638 /* Set a single bit in a bitmap. Return true if the bit changed. */
641 bitmap_set_bit (bitmap head
, int bit
)
643 bitmap_element
*ptr
= bitmap_find_bit (head
, bit
);
644 unsigned word_num
= bit
/ BITMAP_WORD_BITS
% BITMAP_ELEMENT_WORDS
;
645 unsigned bit_num
= bit
% BITMAP_WORD_BITS
;
646 BITMAP_WORD bit_val
= ((BITMAP_WORD
) 1) << bit_num
;
650 ptr
= bitmap_element_allocate (head
);
651 ptr
->indx
= bit
/ BITMAP_ELEMENT_ALL_BITS
;
652 ptr
->bits
[word_num
] = bit_val
;
653 bitmap_element_link (head
, ptr
);
658 bool res
= (ptr
->bits
[word_num
] & bit_val
) == 0;
660 ptr
->bits
[word_num
] |= bit_val
;
665 /* Return whether a bit is set within a bitmap. */
668 bitmap_bit_p (bitmap head
, int bit
)
674 ptr
= bitmap_find_bit (head
, bit
);
678 bit_num
= bit
% BITMAP_WORD_BITS
;
679 word_num
= bit
/ BITMAP_WORD_BITS
% BITMAP_ELEMENT_WORDS
;
681 return (ptr
->bits
[word_num
] >> bit_num
) & 1;
684 #if GCC_VERSION < 3400
685 /* Table of number of set bits in a character, indexed by value of char. */
686 static const unsigned char popcount_table
[] =
688 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,
689 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,
690 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,
691 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,
692 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,
693 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,
694 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,
695 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,
699 bitmap_popcount (BITMAP_WORD a
)
701 unsigned long ret
= 0;
704 /* Just do this the table way for now */
705 for (i
= 0; i
< BITMAP_WORD_BITS
; i
+= 8)
706 ret
+= popcount_table
[(a
>> i
) & 0xff];
710 /* Count the number of bits set in the bitmap, and return it. */
713 bitmap_count_bits (const_bitmap a
)
715 unsigned long count
= 0;
716 const bitmap_element
*elt
;
719 for (elt
= a
->first
; elt
; elt
= elt
->next
)
721 for (ix
= 0; ix
!= BITMAP_ELEMENT_WORDS
; ix
++)
723 #if GCC_VERSION >= 3400
724 /* Note that popcountl matches BITMAP_WORD in type, so the actual size
725 of BITMAP_WORD is not material. */
726 count
+= __builtin_popcountl (elt
->bits
[ix
]);
728 count
+= bitmap_popcount (elt
->bits
[ix
]);
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
;
847 /* Binary search for the last set bit. */
848 #if GCC_VERSION >= 3004
849 gcc_assert (sizeof(long) == sizeof (word
));
850 bit_no
+= sizeof (long) * 8 - __builtin_ctzl (word
);
852 #if BITMAP_WORD_BITS > 64
853 #error "Fill out the table."
855 #if BITMAP_WORD_BITS > 32
856 if ((word
& 0xffffffff00000000))
857 word
>>= 32, bit_no
+= 32;
859 if (word
& 0xffff0000)
860 word
>>= 16, bit_no
+= 16;
861 if (!(word
& 0xff00))
862 word
>>= 8, bit_no
+= 8;
864 word
>>= 4, bit_no
+= 4;
866 word
>>= 2, bit_no
+= 2;
868 word
>>= 1, bit_no
+= 1;
871 gcc_checking_assert (word
& 1);
879 bitmap_and (bitmap dst
, const_bitmap a
, const_bitmap b
)
881 bitmap_element
*dst_elt
= dst
->first
;
882 const bitmap_element
*a_elt
= a
->first
;
883 const bitmap_element
*b_elt
= b
->first
;
884 bitmap_element
*dst_prev
= NULL
;
886 gcc_assert (dst
!= a
&& dst
!= b
);
890 bitmap_copy (dst
, a
);
894 while (a_elt
&& b_elt
)
896 if (a_elt
->indx
< b_elt
->indx
)
898 else if (b_elt
->indx
< a_elt
->indx
)
902 /* Matching elts, generate A & B. */
907 dst_elt
= bitmap_elt_insert_after (dst
, dst_prev
, a_elt
->indx
);
909 dst_elt
->indx
= a_elt
->indx
;
910 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
912 BITMAP_WORD r
= a_elt
->bits
[ix
] & b_elt
->bits
[ix
];
914 dst_elt
->bits
[ix
] = r
;
920 dst_elt
= dst_elt
->next
;
926 /* Ensure that dst->current is valid. */
927 dst
->current
= dst
->first
;
928 bitmap_elt_clear_from (dst
, dst_elt
);
929 gcc_checking_assert (!dst
->current
== !dst
->first
);
931 dst
->indx
= dst
->current
->indx
;
937 bitmap_and_into (bitmap a
, const_bitmap b
)
939 bitmap_element
*a_elt
= a
->first
;
940 const bitmap_element
*b_elt
= b
->first
;
941 bitmap_element
*next
;
946 while (a_elt
&& b_elt
)
948 if (a_elt
->indx
< b_elt
->indx
)
951 bitmap_element_free (a
, a_elt
);
954 else if (b_elt
->indx
< a_elt
->indx
)
958 /* Matching elts, generate A &= B. */
962 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
964 BITMAP_WORD r
= a_elt
->bits
[ix
] & b_elt
->bits
[ix
];
971 bitmap_element_free (a
, a_elt
);
976 bitmap_elt_clear_from (a
, a_elt
);
977 gcc_checking_assert (!a
->current
== !a
->first
978 && (!a
->current
|| a
->indx
== a
->current
->indx
));
982 /* Insert an element equal to SRC_ELT after DST_PREV, overwriting DST_ELT
983 if non-NULL. CHANGED is true if the destination bitmap had already been
984 changed; the new value of CHANGED is returned. */
987 bitmap_elt_copy (bitmap dst
, bitmap_element
*dst_elt
, bitmap_element
*dst_prev
,
988 const bitmap_element
*src_elt
, bool changed
)
990 if (!changed
&& dst_elt
&& dst_elt
->indx
== src_elt
->indx
)
994 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
995 if (src_elt
->bits
[ix
] != dst_elt
->bits
[ix
])
997 dst_elt
->bits
[ix
] = src_elt
->bits
[ix
];
1005 dst_elt
= bitmap_elt_insert_after (dst
, dst_prev
, src_elt
->indx
);
1007 dst_elt
->indx
= src_elt
->indx
;
1008 memcpy (dst_elt
->bits
, src_elt
->bits
, sizeof (dst_elt
->bits
));
1018 bitmap_and_compl (bitmap dst
, const_bitmap a
, const_bitmap b
)
1020 bitmap_element
*dst_elt
= dst
->first
;
1021 const bitmap_element
*a_elt
= a
->first
;
1022 const bitmap_element
*b_elt
= b
->first
;
1023 bitmap_element
*dst_prev
= NULL
;
1024 bitmap_element
**dst_prev_pnext
= &dst
->first
;
1025 bool changed
= false;
1027 gcc_assert (dst
!= a
&& dst
!= b
);
1031 changed
= !bitmap_empty_p (dst
);
1038 while (b_elt
&& b_elt
->indx
< a_elt
->indx
)
1039 b_elt
= b_elt
->next
;
1041 if (!b_elt
|| b_elt
->indx
> a_elt
->indx
)
1043 changed
= bitmap_elt_copy (dst
, dst_elt
, dst_prev
, a_elt
, changed
);
1044 dst_prev
= *dst_prev_pnext
;
1045 dst_prev_pnext
= &dst_prev
->next
;
1046 dst_elt
= *dst_prev_pnext
;
1047 a_elt
= a_elt
->next
;
1052 /* Matching elts, generate A & ~B. */
1054 BITMAP_WORD ior
= 0;
1056 if (!changed
&& dst_elt
&& dst_elt
->indx
== a_elt
->indx
)
1058 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1060 BITMAP_WORD r
= a_elt
->bits
[ix
] & ~b_elt
->bits
[ix
];
1062 if (dst_elt
->bits
[ix
] != r
)
1065 dst_elt
->bits
[ix
] = r
;
1073 if (!dst_elt
|| dst_elt
->indx
> a_elt
->indx
)
1075 dst_elt
= bitmap_elt_insert_after (dst
, dst_prev
, a_elt
->indx
);
1080 dst_elt
->indx
= a_elt
->indx
;
1081 new_element
= false;
1084 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1086 BITMAP_WORD r
= a_elt
->bits
[ix
] & ~b_elt
->bits
[ix
];
1088 dst_elt
->bits
[ix
] = r
;
1096 changed
|= !new_element
;
1097 bitmap_element_free (dst
, dst_elt
);
1098 dst_elt
= *dst_prev_pnext
;
1104 dst_prev
= *dst_prev_pnext
;
1105 dst_prev_pnext
= &dst_prev
->next
;
1106 dst_elt
= *dst_prev_pnext
;
1108 a_elt
= a_elt
->next
;
1109 b_elt
= b_elt
->next
;
1113 /* Ensure that dst->current is valid. */
1114 dst
->current
= dst
->first
;
1119 bitmap_elt_clear_from (dst
, dst_elt
);
1121 gcc_checking_assert (!dst
->current
== !dst
->first
);
1123 dst
->indx
= dst
->current
->indx
;
1128 /* A &= ~B. Returns true if A changes */
1131 bitmap_and_compl_into (bitmap a
, const_bitmap b
)
1133 bitmap_element
*a_elt
= a
->first
;
1134 const bitmap_element
*b_elt
= b
->first
;
1135 bitmap_element
*next
;
1136 BITMAP_WORD changed
= 0;
1140 if (bitmap_empty_p (a
))
1149 while (a_elt
&& b_elt
)
1151 if (a_elt
->indx
< b_elt
->indx
)
1152 a_elt
= a_elt
->next
;
1153 else if (b_elt
->indx
< a_elt
->indx
)
1154 b_elt
= b_elt
->next
;
1157 /* Matching elts, generate A &= ~B. */
1159 BITMAP_WORD ior
= 0;
1161 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1163 BITMAP_WORD cleared
= a_elt
->bits
[ix
] & b_elt
->bits
[ix
];
1164 BITMAP_WORD r
= a_elt
->bits
[ix
] ^ cleared
;
1166 a_elt
->bits
[ix
] = r
;
1172 bitmap_element_free (a
, a_elt
);
1174 b_elt
= b_elt
->next
;
1177 gcc_checking_assert (!a
->current
== !a
->first
1178 && (!a
->current
|| a
->indx
== a
->current
->indx
));
1179 return changed
!= 0;
1182 /* Set COUNT bits from START in HEAD. */
1184 bitmap_set_range (bitmap head
, unsigned int start
, unsigned int count
)
1186 unsigned int first_index
, end_bit_plus1
, last_index
;
1187 bitmap_element
*elt
, *elt_prev
;
1193 first_index
= start
/ BITMAP_ELEMENT_ALL_BITS
;
1194 end_bit_plus1
= start
+ count
;
1195 last_index
= (end_bit_plus1
- 1) / BITMAP_ELEMENT_ALL_BITS
;
1196 elt
= bitmap_find_bit (head
, start
);
1198 /* If bitmap_find_bit returns zero, the current is the closest block
1199 to the result. Otherwise, just use bitmap_element_allocate to
1200 ensure ELT is set; in the loop below, ELT == NULL means "insert
1201 at the end of the bitmap". */
1204 elt
= bitmap_element_allocate (head
);
1205 elt
->indx
= first_index
;
1206 bitmap_element_link (head
, elt
);
1209 gcc_checking_assert (elt
->indx
== first_index
);
1210 elt_prev
= elt
->prev
;
1211 for (i
= first_index
; i
<= last_index
; i
++)
1213 unsigned elt_start_bit
= i
* BITMAP_ELEMENT_ALL_BITS
;
1214 unsigned elt_end_bit_plus1
= elt_start_bit
+ BITMAP_ELEMENT_ALL_BITS
;
1216 unsigned int first_word_to_mod
;
1217 BITMAP_WORD first_mask
;
1218 unsigned int last_word_to_mod
;
1219 BITMAP_WORD last_mask
;
1222 if (!elt
|| elt
->indx
!= i
)
1223 elt
= bitmap_elt_insert_after (head
, elt_prev
, i
);
1225 if (elt_start_bit
<= start
)
1227 /* The first bit to turn on is somewhere inside this
1229 first_word_to_mod
= (start
- elt_start_bit
) / BITMAP_WORD_BITS
;
1231 /* This mask should have 1s in all bits >= start position. */
1233 (((BITMAP_WORD
) 1) << ((start
% BITMAP_WORD_BITS
))) - 1;
1234 first_mask
= ~first_mask
;
1238 /* The first bit to turn on is below this start of this elt. */
1239 first_word_to_mod
= 0;
1240 first_mask
= ~(BITMAP_WORD
) 0;
1243 if (elt_end_bit_plus1
<= end_bit_plus1
)
1245 /* The last bit to turn on is beyond this elt. */
1246 last_word_to_mod
= BITMAP_ELEMENT_WORDS
- 1;
1247 last_mask
= ~(BITMAP_WORD
) 0;
1251 /* The last bit to turn on is inside to this elt. */
1253 (end_bit_plus1
- elt_start_bit
) / BITMAP_WORD_BITS
;
1255 /* The last mask should have 1s below the end bit. */
1257 (((BITMAP_WORD
) 1) << ((end_bit_plus1
% BITMAP_WORD_BITS
))) - 1;
1260 if (first_word_to_mod
== last_word_to_mod
)
1262 BITMAP_WORD mask
= first_mask
& last_mask
;
1263 elt
->bits
[first_word_to_mod
] |= mask
;
1267 elt
->bits
[first_word_to_mod
] |= first_mask
;
1268 if (BITMAP_ELEMENT_WORDS
> 2)
1269 for (ix
= first_word_to_mod
+ 1; ix
< last_word_to_mod
; ix
++)
1270 elt
->bits
[ix
] = ~(BITMAP_WORD
) 0;
1271 elt
->bits
[last_word_to_mod
] |= last_mask
;
1278 head
->current
= elt
? elt
: elt_prev
;
1279 head
->indx
= head
->current
->indx
;
1282 /* Clear COUNT bits from START in HEAD. */
1284 bitmap_clear_range (bitmap head
, unsigned int start
, unsigned int count
)
1286 unsigned int first_index
, end_bit_plus1
, last_index
;
1287 bitmap_element
*elt
;
1292 first_index
= start
/ BITMAP_ELEMENT_ALL_BITS
;
1293 end_bit_plus1
= start
+ count
;
1294 last_index
= (end_bit_plus1
- 1) / BITMAP_ELEMENT_ALL_BITS
;
1295 elt
= bitmap_find_bit (head
, start
);
1297 /* If bitmap_find_bit returns zero, the current is the closest block
1298 to the result. If the current is less than first index, find the
1299 next one. Otherwise, just set elt to be current. */
1304 if (head
->indx
< first_index
)
1306 elt
= head
->current
->next
;
1311 elt
= head
->current
;
1317 while (elt
&& (elt
->indx
<= last_index
))
1319 bitmap_element
* next_elt
= elt
->next
;
1320 unsigned elt_start_bit
= (elt
->indx
) * BITMAP_ELEMENT_ALL_BITS
;
1321 unsigned elt_end_bit_plus1
= elt_start_bit
+ BITMAP_ELEMENT_ALL_BITS
;
1324 if (elt_start_bit
>= start
&& elt_end_bit_plus1
<= end_bit_plus1
)
1325 /* Get rid of the entire elt and go to the next one. */
1326 bitmap_element_free (head
, elt
);
1329 /* Going to have to knock out some bits in this elt. */
1330 unsigned int first_word_to_mod
;
1331 BITMAP_WORD first_mask
;
1332 unsigned int last_word_to_mod
;
1333 BITMAP_WORD last_mask
;
1337 if (elt_start_bit
<= start
)
1339 /* The first bit to turn off is somewhere inside this
1341 first_word_to_mod
= (start
- elt_start_bit
) / BITMAP_WORD_BITS
;
1343 /* This mask should have 1s in all bits >= start position. */
1345 (((BITMAP_WORD
) 1) << ((start
% BITMAP_WORD_BITS
))) - 1;
1346 first_mask
= ~first_mask
;
1350 /* The first bit to turn off is below this start of this elt. */
1351 first_word_to_mod
= 0;
1353 first_mask
= ~first_mask
;
1356 if (elt_end_bit_plus1
<= end_bit_plus1
)
1358 /* The last bit to turn off is beyond this elt. */
1359 last_word_to_mod
= BITMAP_ELEMENT_WORDS
- 1;
1361 last_mask
= ~last_mask
;
1365 /* The last bit to turn off is inside to this elt. */
1367 (end_bit_plus1
- elt_start_bit
) / BITMAP_WORD_BITS
;
1369 /* The last mask should have 1s below the end bit. */
1371 (((BITMAP_WORD
) 1) << (((end_bit_plus1
) % BITMAP_WORD_BITS
))) - 1;
1375 if (first_word_to_mod
== last_word_to_mod
)
1377 BITMAP_WORD mask
= first_mask
& last_mask
;
1378 elt
->bits
[first_word_to_mod
] &= ~mask
;
1382 elt
->bits
[first_word_to_mod
] &= ~first_mask
;
1383 if (BITMAP_ELEMENT_WORDS
> 2)
1384 for (i
= first_word_to_mod
+ 1; i
< last_word_to_mod
; i
++)
1386 elt
->bits
[last_word_to_mod
] &= ~last_mask
;
1388 for (i
= 0; i
< BITMAP_ELEMENT_WORDS
; i
++)
1394 /* Check to see if there are any bits left. */
1396 bitmap_element_free (head
, elt
);
1403 head
->current
= elt
;
1404 head
->indx
= head
->current
->indx
;
1411 bitmap_compl_and_into (bitmap a
, const_bitmap b
)
1413 bitmap_element
*a_elt
= a
->first
;
1414 const bitmap_element
*b_elt
= b
->first
;
1415 bitmap_element
*a_prev
= NULL
;
1416 bitmap_element
*next
;
1418 gcc_assert (a
!= b
);
1420 if (bitmap_empty_p (a
))
1425 if (bitmap_empty_p (b
))
1431 while (a_elt
|| b_elt
)
1433 if (!b_elt
|| (a_elt
&& a_elt
->indx
< b_elt
->indx
))
1435 /* A is before B. Remove A */
1437 a_prev
= a_elt
->prev
;
1438 bitmap_element_free (a
, a_elt
);
1441 else if (!a_elt
|| b_elt
->indx
< a_elt
->indx
)
1443 /* B is before A. Copy B. */
1444 next
= bitmap_elt_insert_after (a
, a_prev
, b_elt
->indx
);
1445 memcpy (next
->bits
, b_elt
->bits
, sizeof (next
->bits
));
1447 b_elt
= b_elt
->next
;
1451 /* Matching elts, generate A = ~A & B. */
1453 BITMAP_WORD ior
= 0;
1455 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1457 BITMAP_WORD cleared
= a_elt
->bits
[ix
] & b_elt
->bits
[ix
];
1458 BITMAP_WORD r
= b_elt
->bits
[ix
] ^ cleared
;
1460 a_elt
->bits
[ix
] = r
;
1465 bitmap_element_free (a
, a_elt
);
1469 b_elt
= b_elt
->next
;
1472 gcc_checking_assert (!a
->current
== !a
->first
1473 && (!a
->current
|| a
->indx
== a
->current
->indx
));
1478 /* Insert an element corresponding to A_ELT | B_ELT after DST_PREV,
1479 overwriting DST_ELT if non-NULL. CHANGED is true if the destination bitmap
1480 had already been changed; the new value of CHANGED is returned. */
1483 bitmap_elt_ior (bitmap dst
, bitmap_element
*dst_elt
, bitmap_element
*dst_prev
,
1484 const bitmap_element
*a_elt
, const bitmap_element
*b_elt
,
1487 gcc_assert (a_elt
|| b_elt
);
1489 if (a_elt
&& b_elt
&& a_elt
->indx
== b_elt
->indx
)
1491 /* Matching elts, generate A | B. */
1494 if (!changed
&& dst_elt
&& dst_elt
->indx
== a_elt
->indx
)
1496 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1498 BITMAP_WORD r
= a_elt
->bits
[ix
] | b_elt
->bits
[ix
];
1499 if (r
!= dst_elt
->bits
[ix
])
1501 dst_elt
->bits
[ix
] = r
;
1510 dst_elt
= bitmap_elt_insert_after (dst
, dst_prev
, a_elt
->indx
);
1512 dst_elt
->indx
= a_elt
->indx
;
1513 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1515 BITMAP_WORD r
= a_elt
->bits
[ix
] | b_elt
->bits
[ix
];
1516 dst_elt
->bits
[ix
] = r
;
1522 /* Copy a single element. */
1523 const bitmap_element
*src
;
1525 if (!b_elt
|| (a_elt
&& a_elt
->indx
< b_elt
->indx
))
1530 gcc_checking_assert (src
);
1531 changed
= bitmap_elt_copy (dst
, dst_elt
, dst_prev
, src
, changed
);
1537 /* DST = A | B. Return true if DST changes. */
1540 bitmap_ior (bitmap dst
, const_bitmap a
, const_bitmap b
)
1542 bitmap_element
*dst_elt
= dst
->first
;
1543 const bitmap_element
*a_elt
= a
->first
;
1544 const bitmap_element
*b_elt
= b
->first
;
1545 bitmap_element
*dst_prev
= NULL
;
1546 bitmap_element
**dst_prev_pnext
= &dst
->first
;
1547 bool changed
= false;
1549 gcc_assert (dst
!= a
&& dst
!= b
);
1551 while (a_elt
|| b_elt
)
1553 changed
= bitmap_elt_ior (dst
, dst_elt
, dst_prev
, a_elt
, b_elt
, changed
);
1555 if (a_elt
&& b_elt
&& a_elt
->indx
== b_elt
->indx
)
1557 a_elt
= a_elt
->next
;
1558 b_elt
= b_elt
->next
;
1562 if (a_elt
&& (!b_elt
|| a_elt
->indx
<= b_elt
->indx
))
1563 a_elt
= a_elt
->next
;
1564 else if (b_elt
&& (!a_elt
|| b_elt
->indx
<= a_elt
->indx
))
1565 b_elt
= b_elt
->next
;
1568 dst_prev
= *dst_prev_pnext
;
1569 dst_prev_pnext
= &dst_prev
->next
;
1570 dst_elt
= *dst_prev_pnext
;
1576 bitmap_elt_clear_from (dst
, dst_elt
);
1578 gcc_checking_assert (!dst
->current
== !dst
->first
);
1580 dst
->indx
= dst
->current
->indx
;
1584 /* A |= B. Return true if A changes. */
1587 bitmap_ior_into (bitmap a
, const_bitmap b
)
1589 bitmap_element
*a_elt
= a
->first
;
1590 const bitmap_element
*b_elt
= b
->first
;
1591 bitmap_element
*a_prev
= NULL
;
1592 bitmap_element
**a_prev_pnext
= &a
->first
;
1593 bool changed
= false;
1600 /* If A lags behind B, just advance it. */
1601 if (!a_elt
|| a_elt
->indx
== b_elt
->indx
)
1603 changed
= bitmap_elt_ior (a
, a_elt
, a_prev
, a_elt
, b_elt
, changed
);
1604 b_elt
= b_elt
->next
;
1606 else if (a_elt
->indx
> b_elt
->indx
)
1608 changed
= bitmap_elt_copy (a
, NULL
, a_prev
, b_elt
, changed
);
1609 b_elt
= b_elt
->next
;
1612 a_prev
= *a_prev_pnext
;
1613 a_prev_pnext
= &a_prev
->next
;
1614 a_elt
= *a_prev_pnext
;
1617 gcc_checking_assert (!a
->current
== !a
->first
);
1619 a
->indx
= a
->current
->indx
;
1626 bitmap_xor (bitmap dst
, const_bitmap a
, const_bitmap b
)
1628 bitmap_element
*dst_elt
= dst
->first
;
1629 const bitmap_element
*a_elt
= a
->first
;
1630 const bitmap_element
*b_elt
= b
->first
;
1631 bitmap_element
*dst_prev
= NULL
;
1633 gcc_assert (dst
!= a
&& dst
!= b
);
1640 while (a_elt
|| b_elt
)
1642 if (a_elt
&& b_elt
&& a_elt
->indx
== b_elt
->indx
)
1644 /* Matching elts, generate A ^ B. */
1646 BITMAP_WORD ior
= 0;
1649 dst_elt
= bitmap_elt_insert_after (dst
, dst_prev
, a_elt
->indx
);
1651 dst_elt
->indx
= a_elt
->indx
;
1652 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1654 BITMAP_WORD r
= a_elt
->bits
[ix
] ^ b_elt
->bits
[ix
];
1657 dst_elt
->bits
[ix
] = r
;
1659 a_elt
= a_elt
->next
;
1660 b_elt
= b_elt
->next
;
1664 dst_elt
= dst_elt
->next
;
1669 /* Copy a single element. */
1670 const bitmap_element
*src
;
1672 if (!b_elt
|| (a_elt
&& a_elt
->indx
< b_elt
->indx
))
1675 a_elt
= a_elt
->next
;
1680 b_elt
= b_elt
->next
;
1684 dst_elt
= bitmap_elt_insert_after (dst
, dst_prev
, src
->indx
);
1686 dst_elt
->indx
= src
->indx
;
1687 memcpy (dst_elt
->bits
, src
->bits
, sizeof (dst_elt
->bits
));
1689 dst_elt
= dst_elt
->next
;
1692 /* Ensure that dst->current is valid. */
1693 dst
->current
= dst
->first
;
1694 bitmap_elt_clear_from (dst
, dst_elt
);
1695 gcc_checking_assert (!dst
->current
== !dst
->first
);
1697 dst
->indx
= dst
->current
->indx
;
1703 bitmap_xor_into (bitmap a
, const_bitmap b
)
1705 bitmap_element
*a_elt
= a
->first
;
1706 const bitmap_element
*b_elt
= b
->first
;
1707 bitmap_element
*a_prev
= NULL
;
1717 if (!a_elt
|| b_elt
->indx
< a_elt
->indx
)
1720 bitmap_element
*dst
= bitmap_elt_insert_after (a
, a_prev
, b_elt
->indx
);
1721 memcpy (dst
->bits
, b_elt
->bits
, sizeof (dst
->bits
));
1723 b_elt
= b_elt
->next
;
1725 else if (a_elt
->indx
< b_elt
->indx
)
1728 a_elt
= a_elt
->next
;
1732 /* Matching elts, generate A ^= B. */
1734 BITMAP_WORD ior
= 0;
1735 bitmap_element
*next
= a_elt
->next
;
1737 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1739 BITMAP_WORD r
= a_elt
->bits
[ix
] ^ b_elt
->bits
[ix
];
1742 a_elt
->bits
[ix
] = r
;
1744 b_elt
= b_elt
->next
;
1748 bitmap_element_free (a
, a_elt
);
1752 gcc_checking_assert (!a
->current
== !a
->first
);
1754 a
->indx
= a
->current
->indx
;
1757 /* Return true if two bitmaps are identical.
1758 We do not bother with a check for pointer equality, as that never
1759 occurs in practice. */
1762 bitmap_equal_p (const_bitmap a
, const_bitmap b
)
1764 const bitmap_element
*a_elt
;
1765 const bitmap_element
*b_elt
;
1768 for (a_elt
= a
->first
, b_elt
= b
->first
;
1770 a_elt
= a_elt
->next
, b_elt
= b_elt
->next
)
1772 if (a_elt
->indx
!= b_elt
->indx
)
1774 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1775 if (a_elt
->bits
[ix
] != b_elt
->bits
[ix
])
1778 return !a_elt
&& !b_elt
;
1781 /* Return true if A AND B is not empty. */
1784 bitmap_intersect_p (const_bitmap a
, const_bitmap b
)
1786 const bitmap_element
*a_elt
;
1787 const bitmap_element
*b_elt
;
1790 for (a_elt
= a
->first
, b_elt
= b
->first
;
1793 if (a_elt
->indx
< b_elt
->indx
)
1794 a_elt
= a_elt
->next
;
1795 else if (b_elt
->indx
< a_elt
->indx
)
1796 b_elt
= b_elt
->next
;
1799 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1800 if (a_elt
->bits
[ix
] & b_elt
->bits
[ix
])
1802 a_elt
= a_elt
->next
;
1803 b_elt
= b_elt
->next
;
1809 /* Return true if A AND NOT B is not empty. */
1812 bitmap_intersect_compl_p (const_bitmap a
, const_bitmap b
)
1814 const bitmap_element
*a_elt
;
1815 const bitmap_element
*b_elt
;
1817 for (a_elt
= a
->first
, b_elt
= b
->first
;
1820 if (a_elt
->indx
< b_elt
->indx
)
1822 else if (b_elt
->indx
< a_elt
->indx
)
1823 b_elt
= b_elt
->next
;
1826 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1827 if (a_elt
->bits
[ix
] & ~b_elt
->bits
[ix
])
1829 a_elt
= a_elt
->next
;
1830 b_elt
= b_elt
->next
;
1833 return a_elt
!= NULL
;
1837 /* DST = A | (FROM1 & ~FROM2). Return true if DST changes. */
1840 bitmap_ior_and_compl (bitmap dst
, const_bitmap a
, const_bitmap b
, const_bitmap kill
)
1842 bool changed
= false;
1844 bitmap_element
*dst_elt
= dst
->first
;
1845 const bitmap_element
*a_elt
= a
->first
;
1846 const bitmap_element
*b_elt
= b
->first
;
1847 const bitmap_element
*kill_elt
= kill
->first
;
1848 bitmap_element
*dst_prev
= NULL
;
1849 bitmap_element
**dst_prev_pnext
= &dst
->first
;
1851 gcc_assert (dst
!= a
&& dst
!= b
&& dst
!= kill
);
1853 /* Special cases. We don't bother checking for bitmap_equal_p (b, kill). */
1854 if (b
== kill
|| bitmap_empty_p (b
))
1856 changed
= !bitmap_equal_p (dst
, a
);
1858 bitmap_copy (dst
, a
);
1861 if (bitmap_empty_p (kill
))
1862 return bitmap_ior (dst
, a
, b
);
1863 if (bitmap_empty_p (a
))
1864 return bitmap_and_compl (dst
, b
, kill
);
1866 while (a_elt
|| b_elt
)
1868 bool new_element
= false;
1871 while (kill_elt
&& kill_elt
->indx
< b_elt
->indx
)
1872 kill_elt
= kill_elt
->next
;
1874 if (b_elt
&& kill_elt
&& kill_elt
->indx
== b_elt
->indx
1875 && (!a_elt
|| a_elt
->indx
>= b_elt
->indx
))
1877 bitmap_element tmp_elt
;
1880 BITMAP_WORD ior
= 0;
1881 tmp_elt
.indx
= b_elt
->indx
;
1882 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1884 BITMAP_WORD r
= b_elt
->bits
[ix
] & ~kill_elt
->bits
[ix
];
1886 tmp_elt
.bits
[ix
] = r
;
1891 changed
= bitmap_elt_ior (dst
, dst_elt
, dst_prev
,
1892 a_elt
, &tmp_elt
, changed
);
1894 if (a_elt
&& a_elt
->indx
== b_elt
->indx
)
1895 a_elt
= a_elt
->next
;
1898 b_elt
= b_elt
->next
;
1899 kill_elt
= kill_elt
->next
;
1903 changed
= bitmap_elt_ior (dst
, dst_elt
, dst_prev
,
1904 a_elt
, b_elt
, changed
);
1907 if (a_elt
&& b_elt
&& a_elt
->indx
== b_elt
->indx
)
1909 a_elt
= a_elt
->next
;
1910 b_elt
= b_elt
->next
;
1914 if (a_elt
&& (!b_elt
|| a_elt
->indx
<= b_elt
->indx
))
1915 a_elt
= a_elt
->next
;
1916 else if (b_elt
&& (!a_elt
|| b_elt
->indx
<= a_elt
->indx
))
1917 b_elt
= b_elt
->next
;
1923 dst_prev
= *dst_prev_pnext
;
1924 dst_prev_pnext
= &dst_prev
->next
;
1925 dst_elt
= *dst_prev_pnext
;
1932 bitmap_elt_clear_from (dst
, dst_elt
);
1934 gcc_checking_assert (!dst
->current
== !dst
->first
);
1936 dst
->indx
= dst
->current
->indx
;
1941 /* A |= (FROM1 & ~FROM2). Return true if A changes. */
1944 bitmap_ior_and_compl_into (bitmap a
, const_bitmap from1
, const_bitmap from2
)
1949 bitmap_initialize (&tmp
, &bitmap_default_obstack
);
1950 bitmap_and_compl (&tmp
, from1
, from2
);
1951 changed
= bitmap_ior_into (a
, &tmp
);
1952 bitmap_clear (&tmp
);
1957 /* A |= (B & C). Return true if A changes. */
1960 bitmap_ior_and_into (bitmap a
, const_bitmap b
, const_bitmap c
)
1962 bitmap_element
*a_elt
= a
->first
;
1963 const bitmap_element
*b_elt
= b
->first
;
1964 const bitmap_element
*c_elt
= c
->first
;
1965 bitmap_element and_elt
;
1966 bitmap_element
*a_prev
= NULL
;
1967 bitmap_element
**a_prev_pnext
= &a
->first
;
1968 bool changed
= false;
1972 return bitmap_ior_into (a
, b
);
1973 if (bitmap_empty_p (b
) || bitmap_empty_p (c
))
1977 while (b_elt
&& c_elt
)
1979 BITMAP_WORD overall
;
1981 /* Find a common item of B and C. */
1982 while (b_elt
->indx
!= c_elt
->indx
)
1984 if (b_elt
->indx
< c_elt
->indx
)
1986 b_elt
= b_elt
->next
;
1992 c_elt
= c_elt
->next
;
1999 and_elt
.indx
= b_elt
->indx
;
2000 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
2002 and_elt
.bits
[ix
] = b_elt
->bits
[ix
] & c_elt
->bits
[ix
];
2003 overall
|= and_elt
.bits
[ix
];
2006 b_elt
= b_elt
->next
;
2007 c_elt
= c_elt
->next
;
2011 /* Now find a place to insert AND_ELT. */
2014 ix
= a_elt
? a_elt
->indx
: and_elt
.indx
;
2015 if (ix
== and_elt
.indx
)
2016 changed
= bitmap_elt_ior (a
, a_elt
, a_prev
, a_elt
, &and_elt
, changed
);
2017 else if (ix
> and_elt
.indx
)
2018 changed
= bitmap_elt_copy (a
, NULL
, a_prev
, &and_elt
, changed
);
2020 a_prev
= *a_prev_pnext
;
2021 a_prev_pnext
= &a_prev
->next
;
2022 a_elt
= *a_prev_pnext
;
2024 /* If A lagged behind B/C, we advanced it so loop once more. */
2026 while (ix
< and_elt
.indx
);
2030 gcc_checking_assert (!a
->current
== !a
->first
);
2032 a
->indx
= a
->current
->indx
;
2036 /* Debugging function to print out the contents of a bitmap. */
2039 debug_bitmap_file (FILE *file
, const_bitmap head
)
2041 const bitmap_element
*ptr
;
2043 fprintf (file
, "\nfirst = " HOST_PTR_PRINTF
2044 " current = " HOST_PTR_PRINTF
" indx = %u\n",
2045 (void *) head
->first
, (void *) head
->current
, head
->indx
);
2047 for (ptr
= head
->first
; ptr
; ptr
= ptr
->next
)
2049 unsigned int i
, j
, col
= 26;
2051 fprintf (file
, "\t" HOST_PTR_PRINTF
" next = " HOST_PTR_PRINTF
2052 " prev = " HOST_PTR_PRINTF
" indx = %u\n\t\tbits = {",
2053 (const void*) ptr
, (const void*) ptr
->next
,
2054 (const void*) ptr
->prev
, ptr
->indx
);
2056 for (i
= 0; i
< BITMAP_ELEMENT_WORDS
; i
++)
2057 for (j
= 0; j
< BITMAP_WORD_BITS
; j
++)
2058 if ((ptr
->bits
[i
] >> j
) & 1)
2062 fprintf (file
, "\n\t\t\t");
2066 fprintf (file
, " %u", (ptr
->indx
* BITMAP_ELEMENT_ALL_BITS
2067 + i
* BITMAP_WORD_BITS
+ j
));
2071 fprintf (file
, " }\n");
2075 /* Function to be called from the debugger to print the contents
2079 debug_bitmap (const_bitmap head
)
2081 debug_bitmap_file (stdout
, head
);
2084 /* Function to print out the contents of a bitmap. Unlike debug_bitmap_file,
2085 it does not print anything but the bits. */
2088 bitmap_print (FILE *file
, const_bitmap head
, const char *prefix
, const char *suffix
)
2090 const char *comma
= "";
2094 fputs (prefix
, file
);
2095 EXECUTE_IF_SET_IN_BITMAP (head
, 0, i
, bi
)
2097 fprintf (file
, "%s%d", comma
, i
);
2100 fputs (suffix
, file
);
2102 #ifdef GATHER_STATISTICS
2105 /* Used to accumulate statistics about bitmap sizes. */
2108 HOST_WIDEST_INT size
;
2112 /* Called via htab_traverse. Output bitmap descriptor pointed out by SLOT
2113 and update statistics. */
2115 print_statistics (void **slot
, void *b
)
2117 struct bitmap_descriptor
*d
= (struct bitmap_descriptor
*) *slot
;
2118 struct output_info
*i
= (struct output_info
*) b
;
2123 const char *s1
= d
->file
;
2125 while ((s2
= strstr (s1
, "gcc/")))
2127 sprintf (s
, "%s:%i (%s)", s1
, d
->line
, d
->function
);
2129 fprintf (stderr
, "%-41s %8d %15"HOST_WIDEST_INT_PRINT
"d %15"
2130 HOST_WIDEST_INT_PRINT
"d %15"HOST_WIDEST_INT_PRINT
"d %10d %10d\n",
2131 s
, d
->created
, d
->allocated
, d
->peak
, d
->current
, d
->nsearches
,
2133 i
->size
+= d
->allocated
;
2134 i
->count
+= d
->created
;
2139 /* Output per-bitmap memory usage statistics. */
2141 dump_bitmap_statistics (void)
2143 #ifdef GATHER_STATISTICS
2144 struct output_info info
;
2146 if (!bitmap_desc_hash
)
2149 fprintf (stderr
, "\nBitmap Overall "
2150 " Allocated Peak Leak searched "
2152 fprintf (stderr
, "---------------------------------------------------------------------------------\n");
2155 htab_traverse (bitmap_desc_hash
, print_statistics
, &info
);
2156 fprintf (stderr
, "---------------------------------------------------------------------------------\n");
2157 fprintf (stderr
, "%-40s %9d %15"HOST_WIDEST_INT_PRINT
"d\n",
2158 "Total", info
.count
, info
.size
);
2159 fprintf (stderr
, "---------------------------------------------------------------------------------\n");
2163 /* Compute hash of bitmap (for purposes of hashing). */
2165 bitmap_hash (const_bitmap head
)
2167 const bitmap_element
*ptr
;
2168 BITMAP_WORD hash
= 0;
2171 for (ptr
= head
->first
; ptr
; ptr
= ptr
->next
)
2174 for (ix
= 0; ix
!= BITMAP_ELEMENT_WORDS
; ix
++)
2175 hash
^= ptr
->bits
[ix
];
2177 return (hashval_t
)hash
;
2180 #include "gt-bitmap.h"