1 /* Functions to support general ended bitmaps.
2 Copyright (C) 1997-2013 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 #include "coretypes.h"
26 #include "hash-table.h"
29 /* Store information about each particular bitmap, per allocation site. */
30 struct bitmap_descriptor_d
37 unsigned HOST_WIDEST_INT allocated
;
38 unsigned HOST_WIDEST_INT peak
;
39 unsigned HOST_WIDEST_INT current
;
40 unsigned HOST_WIDEST_INT nsearches
;
41 unsigned HOST_WIDEST_INT search_iter
;
44 typedef struct bitmap_descriptor_d
*bitmap_descriptor
;
45 typedef const struct bitmap_descriptor_d
*const_bitmap_descriptor
;
47 /* Next available unique id number for bitmap desciptors. */
48 static int next_bitmap_desc_id
= 0;
50 /* Vector mapping descriptor ids to descriptors. */
51 static vec
<bitmap_descriptor
> bitmap_descriptors
;
53 /* Hashtable helpers. */
62 struct bitmap_desc_hasher
: typed_noop_remove
<bitmap_descriptor_d
>
64 typedef bitmap_descriptor_d value_type
;
65 typedef loc compare_type
;
66 static inline hashval_t
hash (const value_type
*);
67 static inline bool equal (const value_type
*, const compare_type
*);
71 bitmap_desc_hasher::hash (const value_type
*d
)
73 return htab_hash_pointer (d
->file
) + d
->line
;
77 bitmap_desc_hasher::equal (const value_type
*d
, const compare_type
*l
)
79 return d
->file
== l
->file
&& d
->function
== l
->function
&& d
->line
== l
->line
;
82 /* Hashtable mapping bitmap names to descriptors. */
83 static hash_table
<bitmap_desc_hasher
> bitmap_desc_hash
;
85 /* For given file and line, return descriptor, create new if needed. */
86 static bitmap_descriptor
87 get_bitmap_descriptor (const char *file
, int line
, const char *function
)
89 bitmap_descriptor_d
**slot
;
93 loc
.function
= function
;
96 if (!bitmap_desc_hash
.is_created ())
97 bitmap_desc_hash
.create (10);
99 slot
= bitmap_desc_hash
.find_slot_with_hash (&loc
,
100 htab_hash_pointer (file
) + line
,
105 *slot
= XCNEW (struct bitmap_descriptor_d
);
106 bitmap_descriptors
.safe_push (*slot
);
107 (*slot
)->id
= next_bitmap_desc_id
++;
108 (*slot
)->file
= file
;
109 (*slot
)->function
= function
;
110 (*slot
)->line
= line
;
114 /* Register new bitmap. */
116 bitmap_register (bitmap b MEM_STAT_DECL
)
118 bitmap_descriptor desc
= get_bitmap_descriptor (ALONE_FINAL_PASS_MEM_STAT
);
120 b
->descriptor_id
= desc
->id
;
123 /* Account the overhead. */
125 register_overhead (bitmap b
, int amount
)
127 bitmap_descriptor desc
= bitmap_descriptors
[b
->descriptor_id
];
128 desc
->current
+= amount
;
130 desc
->allocated
+= amount
;
131 if (desc
->peak
< desc
->current
)
132 desc
->peak
= desc
->current
;
136 bitmap_element bitmap_zero_bits
; /* An element of all zero bits. */
137 bitmap_obstack bitmap_default_obstack
; /* The default bitmap obstack. */
138 static int bitmap_default_obstack_depth
;
139 static GTY((deletable
)) bitmap_element
*bitmap_ggc_free
; /* Freelist of
142 static void bitmap_elem_to_freelist (bitmap
, bitmap_element
*);
143 static void bitmap_element_free (bitmap
, bitmap_element
*);
144 static bitmap_element
*bitmap_element_allocate (bitmap
);
145 static int bitmap_element_zerop (const bitmap_element
*);
146 static void bitmap_element_link (bitmap
, bitmap_element
*);
147 static bitmap_element
*bitmap_elt_insert_after (bitmap
, bitmap_element
*, unsigned int);
148 static void bitmap_elt_clear_from (bitmap
, bitmap_element
*);
149 static bitmap_element
*bitmap_find_bit (bitmap
, unsigned int);
152 /* Add ELEM to the appropriate freelist. */
154 bitmap_elem_to_freelist (bitmap head
, bitmap_element
*elt
)
156 bitmap_obstack
*bit_obstack
= head
->obstack
;
161 elt
->prev
= bit_obstack
->elements
;
162 bit_obstack
->elements
= elt
;
166 elt
->prev
= bitmap_ggc_free
;
167 bitmap_ggc_free
= elt
;
171 /* Free a bitmap element. Since these are allocated off the
172 bitmap_obstack, "free" actually means "put onto the freelist". */
175 bitmap_element_free (bitmap head
, bitmap_element
*elt
)
177 bitmap_element
*next
= elt
->next
;
178 bitmap_element
*prev
= elt
->prev
;
186 if (head
->first
== elt
)
189 /* Since the first thing we try is to insert before current,
190 make current the next entry in preference to the previous. */
191 if (head
->current
== elt
)
193 head
->current
= next
!= 0 ? next
: prev
;
195 head
->indx
= head
->current
->indx
;
200 if (GATHER_STATISTICS
)
201 register_overhead (head
, -((int)sizeof (bitmap_element
)));
203 bitmap_elem_to_freelist (head
, elt
);
206 /* Allocate a bitmap element. The bits are cleared, but nothing else is. */
208 static inline bitmap_element
*
209 bitmap_element_allocate (bitmap head
)
211 bitmap_element
*element
;
212 bitmap_obstack
*bit_obstack
= head
->obstack
;
216 element
= bit_obstack
->elements
;
219 /* Use up the inner list first before looking at the next
220 element of the outer list. */
223 bit_obstack
->elements
= element
->next
;
224 bit_obstack
->elements
->prev
= element
->prev
;
227 /* Inner list was just a singleton. */
228 bit_obstack
->elements
= element
->prev
;
230 element
= XOBNEW (&bit_obstack
->obstack
, bitmap_element
);
234 element
= bitmap_ggc_free
;
236 /* Use up the inner list first before looking at the next
237 element of the outer list. */
240 bitmap_ggc_free
= element
->next
;
241 bitmap_ggc_free
->prev
= element
->prev
;
244 /* Inner list was just a singleton. */
245 bitmap_ggc_free
= element
->prev
;
247 element
= ggc_alloc_bitmap_element_def ();
250 if (GATHER_STATISTICS
)
251 register_overhead (head
, sizeof (bitmap_element
));
253 memset (element
->bits
, 0, sizeof (element
->bits
));
258 /* Remove ELT and all following elements from bitmap HEAD. */
261 bitmap_elt_clear_from (bitmap head
, bitmap_element
*elt
)
263 bitmap_element
*prev
;
264 bitmap_obstack
*bit_obstack
= head
->obstack
;
268 if (GATHER_STATISTICS
)
271 for (prev
= elt
; prev
; prev
= prev
->next
)
273 register_overhead (head
, -sizeof (bitmap_element
) * n
);
280 if (head
->current
->indx
> prev
->indx
)
282 head
->current
= prev
;
283 head
->indx
= prev
->indx
;
289 head
->current
= NULL
;
293 /* Put the entire list onto the free list in one operation. */
296 elt
->prev
= bit_obstack
->elements
;
297 bit_obstack
->elements
= elt
;
301 elt
->prev
= bitmap_ggc_free
;
302 bitmap_ggc_free
= elt
;
306 /* Clear a bitmap by freeing the linked list. */
309 bitmap_clear (bitmap head
)
312 bitmap_elt_clear_from (head
, head
->first
);
315 /* Initialize a bitmap obstack. If BIT_OBSTACK is NULL, initialize
316 the default bitmap obstack. */
319 bitmap_obstack_initialize (bitmap_obstack
*bit_obstack
)
323 if (bitmap_default_obstack_depth
++)
325 bit_obstack
= &bitmap_default_obstack
;
328 #if !defined(__GNUC__) || (__GNUC__ < 2)
329 #define __alignof__(type) 0
332 bit_obstack
->elements
= NULL
;
333 bit_obstack
->heads
= NULL
;
334 obstack_specify_allocation (&bit_obstack
->obstack
, OBSTACK_CHUNK_SIZE
,
335 __alignof__ (bitmap_element
),
340 /* Release the memory from a bitmap obstack. If BIT_OBSTACK is NULL,
341 release the default bitmap obstack. */
344 bitmap_obstack_release (bitmap_obstack
*bit_obstack
)
348 if (--bitmap_default_obstack_depth
)
350 gcc_assert (bitmap_default_obstack_depth
> 0);
353 bit_obstack
= &bitmap_default_obstack
;
356 bit_obstack
->elements
= NULL
;
357 bit_obstack
->heads
= NULL
;
358 obstack_free (&bit_obstack
->obstack
, NULL
);
361 /* Create a new bitmap on an obstack. If BIT_OBSTACK is NULL, create
362 it on the default bitmap obstack. */
365 bitmap_obstack_alloc_stat (bitmap_obstack
*bit_obstack MEM_STAT_DECL
)
370 bit_obstack
= &bitmap_default_obstack
;
371 map
= bit_obstack
->heads
;
373 bit_obstack
->heads
= (struct bitmap_head_def
*) map
->first
;
375 map
= XOBNEW (&bit_obstack
->obstack
, bitmap_head
);
376 bitmap_initialize_stat (map
, bit_obstack PASS_MEM_STAT
);
378 if (GATHER_STATISTICS
)
379 register_overhead (map
, sizeof (bitmap_head
));
384 /* Create a new GCd bitmap. */
387 bitmap_gc_alloc_stat (ALONE_MEM_STAT_DECL
)
391 map
= ggc_alloc_bitmap_head_def ();
392 bitmap_initialize_stat (map
, NULL PASS_MEM_STAT
);
394 if (GATHER_STATISTICS
)
395 register_overhead (map
, sizeof (bitmap_head
));
400 /* Release an obstack allocated bitmap. */
403 bitmap_obstack_free (bitmap map
)
408 map
->first
= (bitmap_element
*) map
->obstack
->heads
;
410 if (GATHER_STATISTICS
)
411 register_overhead (map
, -((int)sizeof (bitmap_head
)));
413 map
->obstack
->heads
= map
;
418 /* Return nonzero if all bits in an element are zero. */
421 bitmap_element_zerop (const bitmap_element
*element
)
423 #if BITMAP_ELEMENT_WORDS == 2
424 return (element
->bits
[0] | element
->bits
[1]) == 0;
428 for (i
= 0; i
< BITMAP_ELEMENT_WORDS
; i
++)
429 if (element
->bits
[i
] != 0)
436 /* Link the bitmap element into the current bitmap linked list. */
439 bitmap_element_link (bitmap head
, bitmap_element
*element
)
441 unsigned int indx
= element
->indx
;
444 /* If this is the first and only element, set it in. */
445 if (head
->first
== 0)
447 element
->next
= element
->prev
= 0;
448 head
->first
= element
;
451 /* If this index is less than that of the current element, it goes someplace
452 before the current element. */
453 else if (indx
< head
->indx
)
455 for (ptr
= head
->current
;
456 ptr
->prev
!= 0 && ptr
->prev
->indx
> indx
;
461 ptr
->prev
->next
= element
;
463 head
->first
= element
;
465 element
->prev
= ptr
->prev
;
470 /* Otherwise, it must go someplace after the current element. */
473 for (ptr
= head
->current
;
474 ptr
->next
!= 0 && ptr
->next
->indx
< indx
;
479 ptr
->next
->prev
= element
;
481 element
->next
= ptr
->next
;
486 /* Set up so this is the first element searched. */
487 head
->current
= element
;
491 /* Insert a new uninitialized element into bitmap HEAD after element
492 ELT. If ELT is NULL, insert the element at the start. Return the
495 static bitmap_element
*
496 bitmap_elt_insert_after (bitmap head
, bitmap_element
*elt
, unsigned int indx
)
498 bitmap_element
*node
= bitmap_element_allocate (head
);
505 head
->current
= node
;
508 node
->next
= head
->first
;
510 node
->next
->prev
= node
;
516 gcc_checking_assert (head
->current
);
517 node
->next
= elt
->next
;
519 node
->next
->prev
= node
;
526 /* Copy a bitmap to another bitmap. */
529 bitmap_copy (bitmap to
, const_bitmap from
)
531 const bitmap_element
*from_ptr
;
532 bitmap_element
*to_ptr
= 0;
536 /* Copy elements in forward direction one at a time. */
537 for (from_ptr
= from
->first
; from_ptr
; from_ptr
= from_ptr
->next
)
539 bitmap_element
*to_elt
= bitmap_element_allocate (to
);
541 to_elt
->indx
= from_ptr
->indx
;
542 memcpy (to_elt
->bits
, from_ptr
->bits
, sizeof (to_elt
->bits
));
544 /* Here we have a special case of bitmap_element_link, for the case
545 where we know the links are being entered in sequence. */
548 to
->first
= to
->current
= to_elt
;
549 to
->indx
= from_ptr
->indx
;
550 to_elt
->next
= to_elt
->prev
= 0;
554 to_elt
->prev
= to_ptr
;
556 to_ptr
->next
= to_elt
;
563 /* Find a bitmap element that would hold a bitmap's bit.
564 Update the `current' field even if we can't find an element that
565 would hold the bitmap's bit to make eventual allocation
568 static inline bitmap_element
*
569 bitmap_find_bit (bitmap head
, unsigned int bit
)
571 bitmap_element
*element
;
572 unsigned int indx
= bit
/ BITMAP_ELEMENT_ALL_BITS
;
574 if (head
->current
== NULL
575 || head
->indx
== indx
)
576 return head
->current
;
577 if (head
->current
== head
->first
578 && head
->first
->next
== NULL
)
581 /* This bitmap has more than one element, and we're going to look
582 through the elements list. Count that as a search. */
583 if (GATHER_STATISTICS
)
584 bitmap_descriptors
[head
->descriptor_id
]->nsearches
++;
586 if (head
->indx
< indx
)
587 /* INDX is beyond head->indx. Search from head->current
589 for (element
= head
->current
;
590 element
->next
!= 0 && element
->indx
< indx
;
591 element
= element
->next
)
593 if (GATHER_STATISTICS
)
594 bitmap_descriptors
[head
->descriptor_id
]->search_iter
++;
597 else if (head
->indx
/ 2 < indx
)
598 /* INDX is less than head->indx and closer to head->indx than to
599 0. Search from head->current backward. */
600 for (element
= head
->current
;
601 element
->prev
!= 0 && element
->indx
> indx
;
602 element
= element
->prev
)
604 if (GATHER_STATISTICS
)
605 bitmap_descriptors
[head
->descriptor_id
]->search_iter
++;
609 /* INDX is less than head->indx and closer to 0 than to
610 head->indx. Search from head->first forward. */
611 for (element
= head
->first
;
612 element
->next
!= 0 && element
->indx
< indx
;
613 element
= element
->next
)
614 if (GATHER_STATISTICS
)
616 bitmap_descriptors
[head
->descriptor_id
]->search_iter
++;
619 /* `element' is the nearest to the one we want. If it's not the one we
620 want, the one we want doesn't exist. */
621 head
->current
= element
;
622 head
->indx
= element
->indx
;
623 if (element
!= 0 && element
->indx
!= indx
)
629 /* Clear a single bit in a bitmap. Return true if the bit changed. */
632 bitmap_clear_bit (bitmap head
, int bit
)
634 bitmap_element
*const ptr
= bitmap_find_bit (head
, bit
);
638 unsigned bit_num
= bit
% BITMAP_WORD_BITS
;
639 unsigned word_num
= bit
/ BITMAP_WORD_BITS
% BITMAP_ELEMENT_WORDS
;
640 BITMAP_WORD bit_val
= ((BITMAP_WORD
) 1) << bit_num
;
641 bool res
= (ptr
->bits
[word_num
] & bit_val
) != 0;
644 ptr
->bits
[word_num
] &= ~bit_val
;
645 /* If we cleared the entire word, free up the element. */
646 if (!ptr
->bits
[word_num
]
647 && bitmap_element_zerop (ptr
))
648 bitmap_element_free (head
, ptr
);
657 /* Set a single bit in a bitmap. Return true if the bit changed. */
660 bitmap_set_bit (bitmap head
, int bit
)
662 bitmap_element
*ptr
= bitmap_find_bit (head
, bit
);
663 unsigned word_num
= bit
/ BITMAP_WORD_BITS
% BITMAP_ELEMENT_WORDS
;
664 unsigned bit_num
= bit
% BITMAP_WORD_BITS
;
665 BITMAP_WORD bit_val
= ((BITMAP_WORD
) 1) << bit_num
;
669 ptr
= bitmap_element_allocate (head
);
670 ptr
->indx
= bit
/ BITMAP_ELEMENT_ALL_BITS
;
671 ptr
->bits
[word_num
] = bit_val
;
672 bitmap_element_link (head
, ptr
);
677 bool res
= (ptr
->bits
[word_num
] & bit_val
) == 0;
679 ptr
->bits
[word_num
] |= bit_val
;
684 /* Return whether a bit is set within a bitmap. */
687 bitmap_bit_p (bitmap head
, int bit
)
693 ptr
= bitmap_find_bit (head
, bit
);
697 bit_num
= bit
% BITMAP_WORD_BITS
;
698 word_num
= bit
/ BITMAP_WORD_BITS
% BITMAP_ELEMENT_WORDS
;
700 return (ptr
->bits
[word_num
] >> bit_num
) & 1;
703 #if GCC_VERSION < 3400
704 /* Table of number of set bits in a character, indexed by value of char. */
705 static const unsigned char popcount_table
[] =
707 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,
708 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,
709 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,
710 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,
711 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,
712 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,
713 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,
714 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,
718 bitmap_popcount (BITMAP_WORD a
)
720 unsigned long ret
= 0;
723 /* Just do this the table way for now */
724 for (i
= 0; i
< BITMAP_WORD_BITS
; i
+= 8)
725 ret
+= popcount_table
[(a
>> i
) & 0xff];
729 /* Count the number of bits set in the bitmap, and return it. */
732 bitmap_count_bits (const_bitmap a
)
734 unsigned long count
= 0;
735 const bitmap_element
*elt
;
738 for (elt
= a
->first
; elt
; elt
= elt
->next
)
740 for (ix
= 0; ix
!= BITMAP_ELEMENT_WORDS
; ix
++)
742 #if GCC_VERSION >= 3400
743 /* Note that popcountl matches BITMAP_WORD in type, so the actual size
744 of BITMAP_WORD is not material. */
745 count
+= __builtin_popcountl (elt
->bits
[ix
]);
747 count
+= bitmap_popcount (elt
->bits
[ix
]);
754 /* Return true if the bitmap has a single bit set. Otherwise return
758 bitmap_single_bit_set_p (const_bitmap a
)
760 unsigned long count
= 0;
761 const bitmap_element
*elt
;
764 if (bitmap_empty_p (a
))
768 /* As there are no completely empty bitmap elements, a second one
769 means we have more than one bit set. */
770 if (elt
->next
!= NULL
)
773 for (ix
= 0; ix
!= BITMAP_ELEMENT_WORDS
; ix
++)
775 #if GCC_VERSION >= 3400
776 /* Note that popcountl matches BITMAP_WORD in type, so the actual size
777 of BITMAP_WORD is not material. */
778 count
+= __builtin_popcountl (elt
->bits
[ix
]);
780 count
+= bitmap_popcount (elt
->bits
[ix
]);
790 /* Return the bit number of the first set bit in the bitmap. The
791 bitmap must be non-empty. */
794 bitmap_first_set_bit (const_bitmap a
)
796 const bitmap_element
*elt
= a
->first
;
801 gcc_checking_assert (elt
);
802 bit_no
= elt
->indx
* BITMAP_ELEMENT_ALL_BITS
;
803 for (ix
= 0; ix
!= BITMAP_ELEMENT_WORDS
; ix
++)
805 word
= elt
->bits
[ix
];
811 bit_no
+= ix
* BITMAP_WORD_BITS
;
813 #if GCC_VERSION >= 3004
814 gcc_assert (sizeof(long) == sizeof (word
));
815 bit_no
+= __builtin_ctzl (word
);
817 /* Binary search for the first set bit. */
818 #if BITMAP_WORD_BITS > 64
819 #error "Fill out the table."
821 #if BITMAP_WORD_BITS > 32
822 if (!(word
& 0xffffffff))
823 word
>>= 32, bit_no
+= 32;
825 if (!(word
& 0xffff))
826 word
>>= 16, bit_no
+= 16;
828 word
>>= 8, bit_no
+= 8;
830 word
>>= 4, bit_no
+= 4;
832 word
>>= 2, bit_no
+= 2;
834 word
>>= 1, bit_no
+= 1;
836 gcc_checking_assert (word
& 1);
841 /* Return the bit number of the first set bit in the bitmap. The
842 bitmap must be non-empty. */
845 bitmap_last_set_bit (const_bitmap a
)
847 const bitmap_element
*elt
= a
->current
? a
->current
: a
->first
;
852 gcc_checking_assert (elt
);
855 bit_no
= elt
->indx
* BITMAP_ELEMENT_ALL_BITS
;
856 for (ix
= BITMAP_ELEMENT_WORDS
- 1; ix
>= 0; ix
--)
858 word
= elt
->bits
[ix
];
864 bit_no
+= ix
* BITMAP_WORD_BITS
;
865 #if GCC_VERSION >= 3004
866 gcc_assert (sizeof(long) == sizeof (word
));
867 bit_no
+= BITMAP_WORD_BITS
- __builtin_clzl (word
) - 1;
869 /* Hopefully this is a twos-complement host... */
870 BITMAP_WORD x
= word
;
876 #if BITMAP_WORD_BITS > 32
879 bit_no
+= bitmap_popcount (x
) - 1;
889 bitmap_and (bitmap dst
, const_bitmap a
, const_bitmap b
)
891 bitmap_element
*dst_elt
= dst
->first
;
892 const bitmap_element
*a_elt
= a
->first
;
893 const bitmap_element
*b_elt
= b
->first
;
894 bitmap_element
*dst_prev
= NULL
;
896 gcc_assert (dst
!= a
&& dst
!= b
);
900 bitmap_copy (dst
, a
);
904 while (a_elt
&& b_elt
)
906 if (a_elt
->indx
< b_elt
->indx
)
908 else if (b_elt
->indx
< a_elt
->indx
)
912 /* Matching elts, generate A & B. */
917 dst_elt
= bitmap_elt_insert_after (dst
, dst_prev
, a_elt
->indx
);
919 dst_elt
->indx
= a_elt
->indx
;
920 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
922 BITMAP_WORD r
= a_elt
->bits
[ix
] & b_elt
->bits
[ix
];
924 dst_elt
->bits
[ix
] = r
;
930 dst_elt
= dst_elt
->next
;
936 /* Ensure that dst->current is valid. */
937 dst
->current
= dst
->first
;
938 bitmap_elt_clear_from (dst
, dst_elt
);
939 gcc_checking_assert (!dst
->current
== !dst
->first
);
941 dst
->indx
= dst
->current
->indx
;
944 /* A &= B. Return true if A changed. */
947 bitmap_and_into (bitmap a
, const_bitmap b
)
949 bitmap_element
*a_elt
= a
->first
;
950 const bitmap_element
*b_elt
= b
->first
;
951 bitmap_element
*next
;
952 bool changed
= false;
957 while (a_elt
&& b_elt
)
959 if (a_elt
->indx
< b_elt
->indx
)
962 bitmap_element_free (a
, a_elt
);
966 else if (b_elt
->indx
< a_elt
->indx
)
970 /* Matching elts, generate A &= B. */
974 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
976 BITMAP_WORD r
= a_elt
->bits
[ix
] & b_elt
->bits
[ix
];
977 if (a_elt
->bits
[ix
] != r
)
984 bitmap_element_free (a
, a_elt
);
993 bitmap_elt_clear_from (a
, a_elt
);
996 gcc_checking_assert (!a
->current
== !a
->first
997 && (!a
->current
|| a
->indx
== a
->current
->indx
));
1003 /* Insert an element equal to SRC_ELT after DST_PREV, overwriting DST_ELT
1004 if non-NULL. CHANGED is true if the destination bitmap had already been
1005 changed; the new value of CHANGED is returned. */
1008 bitmap_elt_copy (bitmap dst
, bitmap_element
*dst_elt
, bitmap_element
*dst_prev
,
1009 const bitmap_element
*src_elt
, bool changed
)
1011 if (!changed
&& dst_elt
&& dst_elt
->indx
== src_elt
->indx
)
1015 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1016 if (src_elt
->bits
[ix
] != dst_elt
->bits
[ix
])
1018 dst_elt
->bits
[ix
] = src_elt
->bits
[ix
];
1026 dst_elt
= bitmap_elt_insert_after (dst
, dst_prev
, src_elt
->indx
);
1028 dst_elt
->indx
= src_elt
->indx
;
1029 memcpy (dst_elt
->bits
, src_elt
->bits
, sizeof (dst_elt
->bits
));
1039 bitmap_and_compl (bitmap dst
, const_bitmap a
, const_bitmap b
)
1041 bitmap_element
*dst_elt
= dst
->first
;
1042 const bitmap_element
*a_elt
= a
->first
;
1043 const bitmap_element
*b_elt
= b
->first
;
1044 bitmap_element
*dst_prev
= NULL
;
1045 bitmap_element
**dst_prev_pnext
= &dst
->first
;
1046 bool changed
= false;
1048 gcc_assert (dst
!= a
&& dst
!= b
);
1052 changed
= !bitmap_empty_p (dst
);
1059 while (b_elt
&& b_elt
->indx
< a_elt
->indx
)
1060 b_elt
= b_elt
->next
;
1062 if (!b_elt
|| b_elt
->indx
> a_elt
->indx
)
1064 changed
= bitmap_elt_copy (dst
, dst_elt
, dst_prev
, a_elt
, changed
);
1065 dst_prev
= *dst_prev_pnext
;
1066 dst_prev_pnext
= &dst_prev
->next
;
1067 dst_elt
= *dst_prev_pnext
;
1068 a_elt
= a_elt
->next
;
1073 /* Matching elts, generate A & ~B. */
1075 BITMAP_WORD ior
= 0;
1077 if (!changed
&& dst_elt
&& dst_elt
->indx
== a_elt
->indx
)
1079 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1081 BITMAP_WORD r
= a_elt
->bits
[ix
] & ~b_elt
->bits
[ix
];
1083 if (dst_elt
->bits
[ix
] != r
)
1086 dst_elt
->bits
[ix
] = r
;
1094 if (!dst_elt
|| dst_elt
->indx
> a_elt
->indx
)
1096 dst_elt
= bitmap_elt_insert_after (dst
, dst_prev
, a_elt
->indx
);
1101 dst_elt
->indx
= a_elt
->indx
;
1102 new_element
= false;
1105 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1107 BITMAP_WORD r
= a_elt
->bits
[ix
] & ~b_elt
->bits
[ix
];
1109 dst_elt
->bits
[ix
] = r
;
1117 changed
|= !new_element
;
1118 bitmap_element_free (dst
, dst_elt
);
1119 dst_elt
= *dst_prev_pnext
;
1125 dst_prev
= *dst_prev_pnext
;
1126 dst_prev_pnext
= &dst_prev
->next
;
1127 dst_elt
= *dst_prev_pnext
;
1129 a_elt
= a_elt
->next
;
1130 b_elt
= b_elt
->next
;
1134 /* Ensure that dst->current is valid. */
1135 dst
->current
= dst
->first
;
1140 bitmap_elt_clear_from (dst
, dst_elt
);
1142 gcc_checking_assert (!dst
->current
== !dst
->first
);
1144 dst
->indx
= dst
->current
->indx
;
1149 /* A &= ~B. Returns true if A changes */
1152 bitmap_and_compl_into (bitmap a
, const_bitmap b
)
1154 bitmap_element
*a_elt
= a
->first
;
1155 const bitmap_element
*b_elt
= b
->first
;
1156 bitmap_element
*next
;
1157 BITMAP_WORD changed
= 0;
1161 if (bitmap_empty_p (a
))
1170 while (a_elt
&& b_elt
)
1172 if (a_elt
->indx
< b_elt
->indx
)
1173 a_elt
= a_elt
->next
;
1174 else if (b_elt
->indx
< a_elt
->indx
)
1175 b_elt
= b_elt
->next
;
1178 /* Matching elts, generate A &= ~B. */
1180 BITMAP_WORD ior
= 0;
1182 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1184 BITMAP_WORD cleared
= a_elt
->bits
[ix
] & b_elt
->bits
[ix
];
1185 BITMAP_WORD r
= a_elt
->bits
[ix
] ^ cleared
;
1187 a_elt
->bits
[ix
] = r
;
1193 bitmap_element_free (a
, a_elt
);
1195 b_elt
= b_elt
->next
;
1198 gcc_checking_assert (!a
->current
== !a
->first
1199 && (!a
->current
|| a
->indx
== a
->current
->indx
));
1200 return changed
!= 0;
1203 /* Set COUNT bits from START in HEAD. */
1205 bitmap_set_range (bitmap head
, unsigned int start
, unsigned int count
)
1207 unsigned int first_index
, end_bit_plus1
, last_index
;
1208 bitmap_element
*elt
, *elt_prev
;
1214 first_index
= start
/ BITMAP_ELEMENT_ALL_BITS
;
1215 end_bit_plus1
= start
+ count
;
1216 last_index
= (end_bit_plus1
- 1) / BITMAP_ELEMENT_ALL_BITS
;
1217 elt
= bitmap_find_bit (head
, start
);
1219 /* If bitmap_find_bit returns zero, the current is the closest block
1220 to the result. Otherwise, just use bitmap_element_allocate to
1221 ensure ELT is set; in the loop below, ELT == NULL means "insert
1222 at the end of the bitmap". */
1225 elt
= bitmap_element_allocate (head
);
1226 elt
->indx
= first_index
;
1227 bitmap_element_link (head
, elt
);
1230 gcc_checking_assert (elt
->indx
== first_index
);
1231 elt_prev
= elt
->prev
;
1232 for (i
= first_index
; i
<= last_index
; i
++)
1234 unsigned elt_start_bit
= i
* BITMAP_ELEMENT_ALL_BITS
;
1235 unsigned elt_end_bit_plus1
= elt_start_bit
+ BITMAP_ELEMENT_ALL_BITS
;
1237 unsigned int first_word_to_mod
;
1238 BITMAP_WORD first_mask
;
1239 unsigned int last_word_to_mod
;
1240 BITMAP_WORD last_mask
;
1243 if (!elt
|| elt
->indx
!= i
)
1244 elt
= bitmap_elt_insert_after (head
, elt_prev
, i
);
1246 if (elt_start_bit
<= start
)
1248 /* The first bit to turn on is somewhere inside this
1250 first_word_to_mod
= (start
- elt_start_bit
) / BITMAP_WORD_BITS
;
1252 /* This mask should have 1s in all bits >= start position. */
1254 (((BITMAP_WORD
) 1) << ((start
% BITMAP_WORD_BITS
))) - 1;
1255 first_mask
= ~first_mask
;
1259 /* The first bit to turn on is below this start of this elt. */
1260 first_word_to_mod
= 0;
1261 first_mask
= ~(BITMAP_WORD
) 0;
1264 if (elt_end_bit_plus1
<= end_bit_plus1
)
1266 /* The last bit to turn on is beyond this elt. */
1267 last_word_to_mod
= BITMAP_ELEMENT_WORDS
- 1;
1268 last_mask
= ~(BITMAP_WORD
) 0;
1272 /* The last bit to turn on is inside to this elt. */
1274 (end_bit_plus1
- elt_start_bit
) / BITMAP_WORD_BITS
;
1276 /* The last mask should have 1s below the end bit. */
1278 (((BITMAP_WORD
) 1) << ((end_bit_plus1
% BITMAP_WORD_BITS
))) - 1;
1281 if (first_word_to_mod
== last_word_to_mod
)
1283 BITMAP_WORD mask
= first_mask
& last_mask
;
1284 elt
->bits
[first_word_to_mod
] |= mask
;
1288 elt
->bits
[first_word_to_mod
] |= first_mask
;
1289 if (BITMAP_ELEMENT_WORDS
> 2)
1290 for (ix
= first_word_to_mod
+ 1; ix
< last_word_to_mod
; ix
++)
1291 elt
->bits
[ix
] = ~(BITMAP_WORD
) 0;
1292 elt
->bits
[last_word_to_mod
] |= last_mask
;
1299 head
->current
= elt
? elt
: elt_prev
;
1300 head
->indx
= head
->current
->indx
;
1303 /* Clear COUNT bits from START in HEAD. */
1305 bitmap_clear_range (bitmap head
, unsigned int start
, unsigned int count
)
1307 unsigned int first_index
, end_bit_plus1
, last_index
;
1308 bitmap_element
*elt
;
1313 first_index
= start
/ BITMAP_ELEMENT_ALL_BITS
;
1314 end_bit_plus1
= start
+ count
;
1315 last_index
= (end_bit_plus1
- 1) / BITMAP_ELEMENT_ALL_BITS
;
1316 elt
= bitmap_find_bit (head
, start
);
1318 /* If bitmap_find_bit returns zero, the current is the closest block
1319 to the result. If the current is less than first index, find the
1320 next one. Otherwise, just set elt to be current. */
1325 if (head
->indx
< first_index
)
1327 elt
= head
->current
->next
;
1332 elt
= head
->current
;
1338 while (elt
&& (elt
->indx
<= last_index
))
1340 bitmap_element
* next_elt
= elt
->next
;
1341 unsigned elt_start_bit
= (elt
->indx
) * BITMAP_ELEMENT_ALL_BITS
;
1342 unsigned elt_end_bit_plus1
= elt_start_bit
+ BITMAP_ELEMENT_ALL_BITS
;
1345 if (elt_start_bit
>= start
&& elt_end_bit_plus1
<= end_bit_plus1
)
1346 /* Get rid of the entire elt and go to the next one. */
1347 bitmap_element_free (head
, elt
);
1350 /* Going to have to knock out some bits in this elt. */
1351 unsigned int first_word_to_mod
;
1352 BITMAP_WORD first_mask
;
1353 unsigned int last_word_to_mod
;
1354 BITMAP_WORD last_mask
;
1358 if (elt_start_bit
<= start
)
1360 /* The first bit to turn off is somewhere inside this
1362 first_word_to_mod
= (start
- elt_start_bit
) / BITMAP_WORD_BITS
;
1364 /* This mask should have 1s in all bits >= start position. */
1366 (((BITMAP_WORD
) 1) << ((start
% BITMAP_WORD_BITS
))) - 1;
1367 first_mask
= ~first_mask
;
1371 /* The first bit to turn off is below this start of this elt. */
1372 first_word_to_mod
= 0;
1374 first_mask
= ~first_mask
;
1377 if (elt_end_bit_plus1
<= end_bit_plus1
)
1379 /* The last bit to turn off is beyond this elt. */
1380 last_word_to_mod
= BITMAP_ELEMENT_WORDS
- 1;
1382 last_mask
= ~last_mask
;
1386 /* The last bit to turn off is inside to this elt. */
1388 (end_bit_plus1
- elt_start_bit
) / BITMAP_WORD_BITS
;
1390 /* The last mask should have 1s below the end bit. */
1392 (((BITMAP_WORD
) 1) << (((end_bit_plus1
) % BITMAP_WORD_BITS
))) - 1;
1396 if (first_word_to_mod
== last_word_to_mod
)
1398 BITMAP_WORD mask
= first_mask
& last_mask
;
1399 elt
->bits
[first_word_to_mod
] &= ~mask
;
1403 elt
->bits
[first_word_to_mod
] &= ~first_mask
;
1404 if (BITMAP_ELEMENT_WORDS
> 2)
1405 for (i
= first_word_to_mod
+ 1; i
< last_word_to_mod
; i
++)
1407 elt
->bits
[last_word_to_mod
] &= ~last_mask
;
1409 for (i
= 0; i
< BITMAP_ELEMENT_WORDS
; i
++)
1415 /* Check to see if there are any bits left. */
1417 bitmap_element_free (head
, elt
);
1424 head
->current
= elt
;
1425 head
->indx
= head
->current
->indx
;
1432 bitmap_compl_and_into (bitmap a
, const_bitmap b
)
1434 bitmap_element
*a_elt
= a
->first
;
1435 const bitmap_element
*b_elt
= b
->first
;
1436 bitmap_element
*a_prev
= NULL
;
1437 bitmap_element
*next
;
1439 gcc_assert (a
!= b
);
1441 if (bitmap_empty_p (a
))
1446 if (bitmap_empty_p (b
))
1452 while (a_elt
|| b_elt
)
1454 if (!b_elt
|| (a_elt
&& a_elt
->indx
< b_elt
->indx
))
1456 /* A is before B. Remove A */
1458 a_prev
= a_elt
->prev
;
1459 bitmap_element_free (a
, a_elt
);
1462 else if (!a_elt
|| b_elt
->indx
< a_elt
->indx
)
1464 /* B is before A. Copy B. */
1465 next
= bitmap_elt_insert_after (a
, a_prev
, b_elt
->indx
);
1466 memcpy (next
->bits
, b_elt
->bits
, sizeof (next
->bits
));
1468 b_elt
= b_elt
->next
;
1472 /* Matching elts, generate A = ~A & B. */
1474 BITMAP_WORD ior
= 0;
1476 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1478 BITMAP_WORD cleared
= a_elt
->bits
[ix
] & b_elt
->bits
[ix
];
1479 BITMAP_WORD r
= b_elt
->bits
[ix
] ^ cleared
;
1481 a_elt
->bits
[ix
] = r
;
1486 bitmap_element_free (a
, a_elt
);
1490 b_elt
= b_elt
->next
;
1493 gcc_checking_assert (!a
->current
== !a
->first
1494 && (!a
->current
|| a
->indx
== a
->current
->indx
));
1499 /* Insert an element corresponding to A_ELT | B_ELT after DST_PREV,
1500 overwriting DST_ELT if non-NULL. CHANGED is true if the destination bitmap
1501 had already been changed; the new value of CHANGED is returned. */
1504 bitmap_elt_ior (bitmap dst
, bitmap_element
*dst_elt
, bitmap_element
*dst_prev
,
1505 const bitmap_element
*a_elt
, const bitmap_element
*b_elt
,
1508 gcc_assert (a_elt
|| b_elt
);
1510 if (a_elt
&& b_elt
&& a_elt
->indx
== b_elt
->indx
)
1512 /* Matching elts, generate A | B. */
1515 if (!changed
&& dst_elt
&& dst_elt
->indx
== a_elt
->indx
)
1517 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1519 BITMAP_WORD r
= a_elt
->bits
[ix
] | b_elt
->bits
[ix
];
1520 if (r
!= dst_elt
->bits
[ix
])
1522 dst_elt
->bits
[ix
] = r
;
1531 dst_elt
= bitmap_elt_insert_after (dst
, dst_prev
, a_elt
->indx
);
1533 dst_elt
->indx
= a_elt
->indx
;
1534 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1536 BITMAP_WORD r
= a_elt
->bits
[ix
] | b_elt
->bits
[ix
];
1537 dst_elt
->bits
[ix
] = r
;
1543 /* Copy a single element. */
1544 const bitmap_element
*src
;
1546 if (!b_elt
|| (a_elt
&& a_elt
->indx
< b_elt
->indx
))
1551 gcc_checking_assert (src
);
1552 changed
= bitmap_elt_copy (dst
, dst_elt
, dst_prev
, src
, changed
);
1558 /* DST = A | B. Return true if DST changes. */
1561 bitmap_ior (bitmap dst
, const_bitmap a
, const_bitmap b
)
1563 bitmap_element
*dst_elt
= dst
->first
;
1564 const bitmap_element
*a_elt
= a
->first
;
1565 const bitmap_element
*b_elt
= b
->first
;
1566 bitmap_element
*dst_prev
= NULL
;
1567 bitmap_element
**dst_prev_pnext
= &dst
->first
;
1568 bool changed
= false;
1570 gcc_assert (dst
!= a
&& dst
!= b
);
1572 while (a_elt
|| b_elt
)
1574 changed
= bitmap_elt_ior (dst
, dst_elt
, dst_prev
, a_elt
, b_elt
, changed
);
1576 if (a_elt
&& b_elt
&& a_elt
->indx
== b_elt
->indx
)
1578 a_elt
= a_elt
->next
;
1579 b_elt
= b_elt
->next
;
1583 if (a_elt
&& (!b_elt
|| a_elt
->indx
<= b_elt
->indx
))
1584 a_elt
= a_elt
->next
;
1585 else if (b_elt
&& (!a_elt
|| b_elt
->indx
<= a_elt
->indx
))
1586 b_elt
= b_elt
->next
;
1589 dst_prev
= *dst_prev_pnext
;
1590 dst_prev_pnext
= &dst_prev
->next
;
1591 dst_elt
= *dst_prev_pnext
;
1597 bitmap_elt_clear_from (dst
, dst_elt
);
1599 gcc_checking_assert (!dst
->current
== !dst
->first
);
1601 dst
->indx
= dst
->current
->indx
;
1605 /* A |= B. Return true if A changes. */
1608 bitmap_ior_into (bitmap a
, const_bitmap b
)
1610 bitmap_element
*a_elt
= a
->first
;
1611 const bitmap_element
*b_elt
= b
->first
;
1612 bitmap_element
*a_prev
= NULL
;
1613 bitmap_element
**a_prev_pnext
= &a
->first
;
1614 bool changed
= false;
1621 /* If A lags behind B, just advance it. */
1622 if (!a_elt
|| a_elt
->indx
== b_elt
->indx
)
1624 changed
= bitmap_elt_ior (a
, a_elt
, a_prev
, a_elt
, b_elt
, changed
);
1625 b_elt
= b_elt
->next
;
1627 else if (a_elt
->indx
> b_elt
->indx
)
1629 changed
= bitmap_elt_copy (a
, NULL
, a_prev
, b_elt
, changed
);
1630 b_elt
= b_elt
->next
;
1633 a_prev
= *a_prev_pnext
;
1634 a_prev_pnext
= &a_prev
->next
;
1635 a_elt
= *a_prev_pnext
;
1638 gcc_checking_assert (!a
->current
== !a
->first
);
1640 a
->indx
= a
->current
->indx
;
1647 bitmap_xor (bitmap dst
, const_bitmap a
, const_bitmap b
)
1649 bitmap_element
*dst_elt
= dst
->first
;
1650 const bitmap_element
*a_elt
= a
->first
;
1651 const bitmap_element
*b_elt
= b
->first
;
1652 bitmap_element
*dst_prev
= NULL
;
1654 gcc_assert (dst
!= a
&& dst
!= b
);
1661 while (a_elt
|| b_elt
)
1663 if (a_elt
&& b_elt
&& a_elt
->indx
== b_elt
->indx
)
1665 /* Matching elts, generate A ^ B. */
1667 BITMAP_WORD ior
= 0;
1670 dst_elt
= bitmap_elt_insert_after (dst
, dst_prev
, a_elt
->indx
);
1672 dst_elt
->indx
= a_elt
->indx
;
1673 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1675 BITMAP_WORD r
= a_elt
->bits
[ix
] ^ b_elt
->bits
[ix
];
1678 dst_elt
->bits
[ix
] = r
;
1680 a_elt
= a_elt
->next
;
1681 b_elt
= b_elt
->next
;
1685 dst_elt
= dst_elt
->next
;
1690 /* Copy a single element. */
1691 const bitmap_element
*src
;
1693 if (!b_elt
|| (a_elt
&& a_elt
->indx
< b_elt
->indx
))
1696 a_elt
= a_elt
->next
;
1701 b_elt
= b_elt
->next
;
1705 dst_elt
= bitmap_elt_insert_after (dst
, dst_prev
, src
->indx
);
1707 dst_elt
->indx
= src
->indx
;
1708 memcpy (dst_elt
->bits
, src
->bits
, sizeof (dst_elt
->bits
));
1710 dst_elt
= dst_elt
->next
;
1713 /* Ensure that dst->current is valid. */
1714 dst
->current
= dst
->first
;
1715 bitmap_elt_clear_from (dst
, dst_elt
);
1716 gcc_checking_assert (!dst
->current
== !dst
->first
);
1718 dst
->indx
= dst
->current
->indx
;
1724 bitmap_xor_into (bitmap a
, const_bitmap b
)
1726 bitmap_element
*a_elt
= a
->first
;
1727 const bitmap_element
*b_elt
= b
->first
;
1728 bitmap_element
*a_prev
= NULL
;
1738 if (!a_elt
|| b_elt
->indx
< a_elt
->indx
)
1741 bitmap_element
*dst
= bitmap_elt_insert_after (a
, a_prev
, b_elt
->indx
);
1742 memcpy (dst
->bits
, b_elt
->bits
, sizeof (dst
->bits
));
1744 b_elt
= b_elt
->next
;
1746 else if (a_elt
->indx
< b_elt
->indx
)
1749 a_elt
= a_elt
->next
;
1753 /* Matching elts, generate A ^= B. */
1755 BITMAP_WORD ior
= 0;
1756 bitmap_element
*next
= a_elt
->next
;
1758 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1760 BITMAP_WORD r
= a_elt
->bits
[ix
] ^ b_elt
->bits
[ix
];
1763 a_elt
->bits
[ix
] = r
;
1765 b_elt
= b_elt
->next
;
1769 bitmap_element_free (a
, a_elt
);
1773 gcc_checking_assert (!a
->current
== !a
->first
);
1775 a
->indx
= a
->current
->indx
;
1778 /* Return true if two bitmaps are identical.
1779 We do not bother with a check for pointer equality, as that never
1780 occurs in practice. */
1783 bitmap_equal_p (const_bitmap a
, const_bitmap b
)
1785 const bitmap_element
*a_elt
;
1786 const bitmap_element
*b_elt
;
1789 for (a_elt
= a
->first
, b_elt
= b
->first
;
1791 a_elt
= a_elt
->next
, b_elt
= b_elt
->next
)
1793 if (a_elt
->indx
!= b_elt
->indx
)
1795 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1796 if (a_elt
->bits
[ix
] != b_elt
->bits
[ix
])
1799 return !a_elt
&& !b_elt
;
1802 /* Return true if A AND B is not empty. */
1805 bitmap_intersect_p (const_bitmap a
, const_bitmap b
)
1807 const bitmap_element
*a_elt
;
1808 const bitmap_element
*b_elt
;
1811 for (a_elt
= a
->first
, b_elt
= b
->first
;
1814 if (a_elt
->indx
< b_elt
->indx
)
1815 a_elt
= a_elt
->next
;
1816 else if (b_elt
->indx
< a_elt
->indx
)
1817 b_elt
= b_elt
->next
;
1820 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1821 if (a_elt
->bits
[ix
] & b_elt
->bits
[ix
])
1823 a_elt
= a_elt
->next
;
1824 b_elt
= b_elt
->next
;
1830 /* Return true if A AND NOT B is not empty. */
1833 bitmap_intersect_compl_p (const_bitmap a
, const_bitmap b
)
1835 const bitmap_element
*a_elt
;
1836 const bitmap_element
*b_elt
;
1838 for (a_elt
= a
->first
, b_elt
= b
->first
;
1841 if (a_elt
->indx
< b_elt
->indx
)
1843 else if (b_elt
->indx
< a_elt
->indx
)
1844 b_elt
= b_elt
->next
;
1847 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1848 if (a_elt
->bits
[ix
] & ~b_elt
->bits
[ix
])
1850 a_elt
= a_elt
->next
;
1851 b_elt
= b_elt
->next
;
1854 return a_elt
!= NULL
;
1858 /* DST = A | (FROM1 & ~FROM2). Return true if DST changes. */
1861 bitmap_ior_and_compl (bitmap dst
, const_bitmap a
, const_bitmap b
, const_bitmap kill
)
1863 bool changed
= false;
1865 bitmap_element
*dst_elt
= dst
->first
;
1866 const bitmap_element
*a_elt
= a
->first
;
1867 const bitmap_element
*b_elt
= b
->first
;
1868 const bitmap_element
*kill_elt
= kill
->first
;
1869 bitmap_element
*dst_prev
= NULL
;
1870 bitmap_element
**dst_prev_pnext
= &dst
->first
;
1872 gcc_assert (dst
!= a
&& dst
!= b
&& dst
!= kill
);
1874 /* Special cases. We don't bother checking for bitmap_equal_p (b, kill). */
1875 if (b
== kill
|| bitmap_empty_p (b
))
1877 changed
= !bitmap_equal_p (dst
, a
);
1879 bitmap_copy (dst
, a
);
1882 if (bitmap_empty_p (kill
))
1883 return bitmap_ior (dst
, a
, b
);
1884 if (bitmap_empty_p (a
))
1885 return bitmap_and_compl (dst
, b
, kill
);
1887 while (a_elt
|| b_elt
)
1889 bool new_element
= false;
1892 while (kill_elt
&& kill_elt
->indx
< b_elt
->indx
)
1893 kill_elt
= kill_elt
->next
;
1895 if (b_elt
&& kill_elt
&& kill_elt
->indx
== b_elt
->indx
1896 && (!a_elt
|| a_elt
->indx
>= b_elt
->indx
))
1898 bitmap_element tmp_elt
;
1901 BITMAP_WORD ior
= 0;
1902 tmp_elt
.indx
= b_elt
->indx
;
1903 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1905 BITMAP_WORD r
= b_elt
->bits
[ix
] & ~kill_elt
->bits
[ix
];
1907 tmp_elt
.bits
[ix
] = r
;
1912 changed
= bitmap_elt_ior (dst
, dst_elt
, dst_prev
,
1913 a_elt
, &tmp_elt
, changed
);
1915 if (a_elt
&& a_elt
->indx
== b_elt
->indx
)
1916 a_elt
= a_elt
->next
;
1919 b_elt
= b_elt
->next
;
1920 kill_elt
= kill_elt
->next
;
1924 changed
= bitmap_elt_ior (dst
, dst_elt
, dst_prev
,
1925 a_elt
, b_elt
, changed
);
1928 if (a_elt
&& b_elt
&& a_elt
->indx
== b_elt
->indx
)
1930 a_elt
= a_elt
->next
;
1931 b_elt
= b_elt
->next
;
1935 if (a_elt
&& (!b_elt
|| a_elt
->indx
<= b_elt
->indx
))
1936 a_elt
= a_elt
->next
;
1937 else if (b_elt
&& (!a_elt
|| b_elt
->indx
<= a_elt
->indx
))
1938 b_elt
= b_elt
->next
;
1944 dst_prev
= *dst_prev_pnext
;
1945 dst_prev_pnext
= &dst_prev
->next
;
1946 dst_elt
= *dst_prev_pnext
;
1953 bitmap_elt_clear_from (dst
, dst_elt
);
1955 gcc_checking_assert (!dst
->current
== !dst
->first
);
1957 dst
->indx
= dst
->current
->indx
;
1962 /* A |= (FROM1 & ~FROM2). Return true if A changes. */
1965 bitmap_ior_and_compl_into (bitmap a
, const_bitmap from1
, const_bitmap from2
)
1970 bitmap_initialize (&tmp
, &bitmap_default_obstack
);
1971 bitmap_and_compl (&tmp
, from1
, from2
);
1972 changed
= bitmap_ior_into (a
, &tmp
);
1973 bitmap_clear (&tmp
);
1978 /* A |= (B & C). Return true if A changes. */
1981 bitmap_ior_and_into (bitmap a
, const_bitmap b
, const_bitmap c
)
1983 bitmap_element
*a_elt
= a
->first
;
1984 const bitmap_element
*b_elt
= b
->first
;
1985 const bitmap_element
*c_elt
= c
->first
;
1986 bitmap_element and_elt
;
1987 bitmap_element
*a_prev
= NULL
;
1988 bitmap_element
**a_prev_pnext
= &a
->first
;
1989 bool changed
= false;
1993 return bitmap_ior_into (a
, b
);
1994 if (bitmap_empty_p (b
) || bitmap_empty_p (c
))
1998 while (b_elt
&& c_elt
)
2000 BITMAP_WORD overall
;
2002 /* Find a common item of B and C. */
2003 while (b_elt
->indx
!= c_elt
->indx
)
2005 if (b_elt
->indx
< c_elt
->indx
)
2007 b_elt
= b_elt
->next
;
2013 c_elt
= c_elt
->next
;
2020 and_elt
.indx
= b_elt
->indx
;
2021 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
2023 and_elt
.bits
[ix
] = b_elt
->bits
[ix
] & c_elt
->bits
[ix
];
2024 overall
|= and_elt
.bits
[ix
];
2027 b_elt
= b_elt
->next
;
2028 c_elt
= c_elt
->next
;
2032 /* Now find a place to insert AND_ELT. */
2035 ix
= a_elt
? a_elt
->indx
: and_elt
.indx
;
2036 if (ix
== and_elt
.indx
)
2037 changed
= bitmap_elt_ior (a
, a_elt
, a_prev
, a_elt
, &and_elt
, changed
);
2038 else if (ix
> and_elt
.indx
)
2039 changed
= bitmap_elt_copy (a
, NULL
, a_prev
, &and_elt
, changed
);
2041 a_prev
= *a_prev_pnext
;
2042 a_prev_pnext
= &a_prev
->next
;
2043 a_elt
= *a_prev_pnext
;
2045 /* If A lagged behind B/C, we advanced it so loop once more. */
2047 while (ix
< and_elt
.indx
);
2051 gcc_checking_assert (!a
->current
== !a
->first
);
2053 a
->indx
= a
->current
->indx
;
2057 /* Compute hash of bitmap (for purposes of hashing). */
2059 bitmap_hash (const_bitmap head
)
2061 const bitmap_element
*ptr
;
2062 BITMAP_WORD hash
= 0;
2065 for (ptr
= head
->first
; ptr
; ptr
= ptr
->next
)
2068 for (ix
= 0; ix
!= BITMAP_ELEMENT_WORDS
; ix
++)
2069 hash
^= ptr
->bits
[ix
];
2071 return (hashval_t
)hash
;
2075 /* Debugging function to print out the contents of a bitmap. */
2078 debug_bitmap_file (FILE *file
, const_bitmap head
)
2080 const bitmap_element
*ptr
;
2082 fprintf (file
, "\nfirst = " HOST_PTR_PRINTF
2083 " current = " HOST_PTR_PRINTF
" indx = %u\n",
2084 (void *) head
->first
, (void *) head
->current
, head
->indx
);
2086 for (ptr
= head
->first
; ptr
; ptr
= ptr
->next
)
2088 unsigned int i
, j
, col
= 26;
2090 fprintf (file
, "\t" HOST_PTR_PRINTF
" next = " HOST_PTR_PRINTF
2091 " prev = " HOST_PTR_PRINTF
" indx = %u\n\t\tbits = {",
2092 (const void*) ptr
, (const void*) ptr
->next
,
2093 (const void*) ptr
->prev
, ptr
->indx
);
2095 for (i
= 0; i
< BITMAP_ELEMENT_WORDS
; i
++)
2096 for (j
= 0; j
< BITMAP_WORD_BITS
; j
++)
2097 if ((ptr
->bits
[i
] >> j
) & 1)
2101 fprintf (file
, "\n\t\t\t");
2105 fprintf (file
, " %u", (ptr
->indx
* BITMAP_ELEMENT_ALL_BITS
2106 + i
* BITMAP_WORD_BITS
+ j
));
2110 fprintf (file
, " }\n");
2114 /* Function to be called from the debugger to print the contents
2118 debug_bitmap (const_bitmap head
)
2120 debug_bitmap_file (stdout
, head
);
2123 /* Function to print out the contents of a bitmap. Unlike debug_bitmap_file,
2124 it does not print anything but the bits. */
2127 bitmap_print (FILE *file
, const_bitmap head
, const char *prefix
,
2130 const char *comma
= "";
2134 fputs (prefix
, file
);
2135 EXECUTE_IF_SET_IN_BITMAP (head
, 0, i
, bi
)
2137 fprintf (file
, "%s%d", comma
, i
);
2140 fputs (suffix
, file
);
2144 /* Used to accumulate statistics about bitmap sizes. */
2147 unsigned HOST_WIDEST_INT size
;
2148 unsigned HOST_WIDEST_INT count
;
2151 /* Called via hash_table::traverse. Output bitmap descriptor pointed out by
2152 SLOT and update statistics. */
2154 print_statistics (bitmap_descriptor_d
**slot
, output_info
*i
)
2156 bitmap_descriptor d
= *slot
;
2161 const char *s1
= d
->file
;
2163 while ((s2
= strstr (s1
, "gcc/")))
2165 sprintf (s
, "%s:%i (%s)", s1
, d
->line
, d
->function
);
2169 " %15"HOST_WIDEST_INT_PRINT
"d %15"HOST_WIDEST_INT_PRINT
"d"
2170 " %15"HOST_WIDEST_INT_PRINT
"d"
2171 " %10"HOST_WIDEST_INT_PRINT
"d %10"HOST_WIDEST_INT_PRINT
"d\n",
2173 d
->allocated
, d
->peak
, d
->current
,
2174 d
->nsearches
, d
->search_iter
);
2175 i
->size
+= d
->allocated
;
2176 i
->count
+= d
->created
;
2181 /* Output per-bitmap memory usage statistics. */
2183 dump_bitmap_statistics (void)
2185 struct output_info info
;
2187 if (! GATHER_STATISTICS
)
2190 if (!bitmap_desc_hash
.is_created ())
2194 "\n%-41s %9s %15s %15s %15s %10s %10s\n",
2195 "Bitmap", "Overall",
2196 "Allocated", "Peak", "Leak",
2197 "searched", "search_itr");
2198 fprintf (stderr
, "---------------------------------------------------------------------------------\n");
2201 bitmap_desc_hash
.traverse
<output_info
*, print_statistics
> (&info
);
2202 fprintf (stderr
, "---------------------------------------------------------------------------------\n");
2204 "%-41s %9"HOST_WIDEST_INT_PRINT
"d %15"HOST_WIDEST_INT_PRINT
"d\n",
2205 "Total", info
.count
, info
.size
);
2206 fprintf (stderr
, "---------------------------------------------------------------------------------\n");
2210 debug (const bitmap_head_def
&ref
)
2212 dump_bitmap (stderr
, &ref
);
2216 debug (const bitmap_head_def
*ptr
)
2221 fprintf (stderr
, "<nil>\n");
2225 #include "gt-bitmap.h"