1 /* Functions to support general ended bitmaps.
2 Copyright (C) 1997-2012 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"
29 /* Store information about each particular bitmap, per allocation site. */
30 struct bitmap_descriptor_d
37 HOST_WIDEST_INT allocated
;
39 HOST_WIDEST_INT current
;
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 mapping bitmap names to descriptors. */
54 static htab_t bitmap_desc_hash
;
56 /* Hashtable helpers. */
58 hash_descriptor (const void *p
)
60 const_bitmap_descriptor d
= (const_bitmap_descriptor
) p
;
61 return htab_hash_pointer (d
->file
) + d
->line
;
70 eq_descriptor (const void *p1
, const void *p2
)
72 const_bitmap_descriptor d
= (const_bitmap_descriptor
) p1
;
73 const struct loc
*const l
= (const struct loc
*) p2
;
74 return d
->file
== l
->file
&& d
->function
== l
->function
&& d
->line
== l
->line
;
77 /* For given file and line, return descriptor, create new if needed. */
78 static bitmap_descriptor
79 get_bitmap_descriptor (const char *file
, int line
, const char *function
)
81 bitmap_descriptor
*slot
;
85 loc
.function
= function
;
88 if (!bitmap_desc_hash
)
89 bitmap_desc_hash
= htab_create (10, hash_descriptor
, eq_descriptor
, NULL
);
91 slot
= (bitmap_descriptor
*)
92 htab_find_slot_with_hash (bitmap_desc_hash
, &loc
,
93 htab_hash_pointer (file
) + line
,
98 *slot
= XCNEW (struct bitmap_descriptor_d
);
99 bitmap_descriptors
.safe_push (*slot
);
100 (*slot
)->id
= next_bitmap_desc_id
++;
101 (*slot
)->file
= file
;
102 (*slot
)->function
= function
;
103 (*slot
)->line
= line
;
107 /* Register new bitmap. */
109 bitmap_register (bitmap b MEM_STAT_DECL
)
111 bitmap_descriptor desc
= get_bitmap_descriptor (ALONE_FINAL_PASS_MEM_STAT
);
113 b
->descriptor_id
= desc
->id
;
116 /* Account the overhead. */
118 register_overhead (bitmap b
, int amount
)
120 bitmap_descriptor desc
= bitmap_descriptors
[b
->descriptor_id
];
121 desc
->current
+= amount
;
123 desc
->allocated
+= amount
;
124 gcc_assert (desc
->current
>= 0);
125 if (desc
->peak
< desc
->current
)
126 desc
->peak
= desc
->current
;
130 bitmap_element bitmap_zero_bits
; /* An element of all zero bits. */
131 bitmap_obstack bitmap_default_obstack
; /* The default bitmap obstack. */
132 static int bitmap_default_obstack_depth
;
133 static GTY((deletable
)) bitmap_element
*bitmap_ggc_free
; /* Freelist of
136 static void bitmap_elem_to_freelist (bitmap
, bitmap_element
*);
137 static void bitmap_element_free (bitmap
, bitmap_element
*);
138 static bitmap_element
*bitmap_element_allocate (bitmap
);
139 static int bitmap_element_zerop (const bitmap_element
*);
140 static void bitmap_element_link (bitmap
, bitmap_element
*);
141 static bitmap_element
*bitmap_elt_insert_after (bitmap
, bitmap_element
*, unsigned int);
142 static void bitmap_elt_clear_from (bitmap
, bitmap_element
*);
143 static bitmap_element
*bitmap_find_bit (bitmap
, unsigned int);
146 /* Add ELEM to the appropriate freelist. */
148 bitmap_elem_to_freelist (bitmap head
, bitmap_element
*elt
)
150 bitmap_obstack
*bit_obstack
= head
->obstack
;
155 elt
->prev
= bit_obstack
->elements
;
156 bit_obstack
->elements
= elt
;
160 elt
->prev
= bitmap_ggc_free
;
161 bitmap_ggc_free
= elt
;
165 /* Free a bitmap element. Since these are allocated off the
166 bitmap_obstack, "free" actually means "put onto the freelist". */
169 bitmap_element_free (bitmap head
, bitmap_element
*elt
)
171 bitmap_element
*next
= elt
->next
;
172 bitmap_element
*prev
= elt
->prev
;
180 if (head
->first
== elt
)
183 /* Since the first thing we try is to insert before current,
184 make current the next entry in preference to the previous. */
185 if (head
->current
== elt
)
187 head
->current
= next
!= 0 ? next
: prev
;
189 head
->indx
= head
->current
->indx
;
194 if (GATHER_STATISTICS
)
195 register_overhead (head
, -((int)sizeof (bitmap_element
)));
197 bitmap_elem_to_freelist (head
, elt
);
200 /* Allocate a bitmap element. The bits are cleared, but nothing else is. */
202 static inline bitmap_element
*
203 bitmap_element_allocate (bitmap head
)
205 bitmap_element
*element
;
206 bitmap_obstack
*bit_obstack
= head
->obstack
;
210 element
= bit_obstack
->elements
;
213 /* Use up the inner list first before looking at the next
214 element of the outer list. */
217 bit_obstack
->elements
= element
->next
;
218 bit_obstack
->elements
->prev
= element
->prev
;
221 /* Inner list was just a singleton. */
222 bit_obstack
->elements
= element
->prev
;
224 element
= XOBNEW (&bit_obstack
->obstack
, bitmap_element
);
228 element
= bitmap_ggc_free
;
230 /* Use up the inner list first before looking at the next
231 element of the outer list. */
234 bitmap_ggc_free
= element
->next
;
235 bitmap_ggc_free
->prev
= element
->prev
;
238 /* Inner list was just a singleton. */
239 bitmap_ggc_free
= element
->prev
;
241 element
= ggc_alloc_bitmap_element_def ();
244 if (GATHER_STATISTICS
)
245 register_overhead (head
, sizeof (bitmap_element
));
247 memset (element
->bits
, 0, sizeof (element
->bits
));
252 /* Remove ELT and all following elements from bitmap HEAD. */
255 bitmap_elt_clear_from (bitmap head
, bitmap_element
*elt
)
257 bitmap_element
*prev
;
258 bitmap_obstack
*bit_obstack
= head
->obstack
;
262 if (GATHER_STATISTICS
)
265 for (prev
= elt
; prev
; prev
= prev
->next
)
267 register_overhead (head
, -sizeof (bitmap_element
) * n
);
274 if (head
->current
->indx
> prev
->indx
)
276 head
->current
= prev
;
277 head
->indx
= prev
->indx
;
283 head
->current
= NULL
;
287 /* Put the entire list onto the free list in one operation. */
290 elt
->prev
= bit_obstack
->elements
;
291 bit_obstack
->elements
= elt
;
295 elt
->prev
= bitmap_ggc_free
;
296 bitmap_ggc_free
= elt
;
300 /* Clear a bitmap by freeing the linked list. */
303 bitmap_clear (bitmap head
)
306 bitmap_elt_clear_from (head
, head
->first
);
309 /* Initialize a bitmap obstack. If BIT_OBSTACK is NULL, initialize
310 the default bitmap obstack. */
313 bitmap_obstack_initialize (bitmap_obstack
*bit_obstack
)
317 if (bitmap_default_obstack_depth
++)
319 bit_obstack
= &bitmap_default_obstack
;
322 #if !defined(__GNUC__) || (__GNUC__ < 2)
323 #define __alignof__(type) 0
326 bit_obstack
->elements
= NULL
;
327 bit_obstack
->heads
= NULL
;
328 obstack_specify_allocation (&bit_obstack
->obstack
, OBSTACK_CHUNK_SIZE
,
329 __alignof__ (bitmap_element
),
334 /* Release the memory from a bitmap obstack. If BIT_OBSTACK is NULL,
335 release the default bitmap obstack. */
338 bitmap_obstack_release (bitmap_obstack
*bit_obstack
)
342 if (--bitmap_default_obstack_depth
)
344 gcc_assert (bitmap_default_obstack_depth
> 0);
347 bit_obstack
= &bitmap_default_obstack
;
350 bit_obstack
->elements
= NULL
;
351 bit_obstack
->heads
= NULL
;
352 obstack_free (&bit_obstack
->obstack
, NULL
);
355 /* Create a new bitmap on an obstack. If BIT_OBSTACK is NULL, create
356 it on the default bitmap obstack. */
359 bitmap_obstack_alloc_stat (bitmap_obstack
*bit_obstack MEM_STAT_DECL
)
364 bit_obstack
= &bitmap_default_obstack
;
365 map
= bit_obstack
->heads
;
367 bit_obstack
->heads
= (struct bitmap_head_def
*) map
->first
;
369 map
= XOBNEW (&bit_obstack
->obstack
, bitmap_head
);
370 bitmap_initialize_stat (map
, bit_obstack PASS_MEM_STAT
);
372 if (GATHER_STATISTICS
)
373 register_overhead (map
, sizeof (bitmap_head
));
378 /* Create a new GCd bitmap. */
381 bitmap_gc_alloc_stat (ALONE_MEM_STAT_DECL
)
385 map
= ggc_alloc_bitmap_head_def ();
386 bitmap_initialize_stat (map
, NULL PASS_MEM_STAT
);
388 if (GATHER_STATISTICS
)
389 register_overhead (map
, sizeof (bitmap_head
));
394 /* Release an obstack allocated bitmap. */
397 bitmap_obstack_free (bitmap map
)
402 map
->first
= (bitmap_element
*) map
->obstack
->heads
;
404 if (GATHER_STATISTICS
)
405 register_overhead (map
, -((int)sizeof (bitmap_head
)));
407 map
->obstack
->heads
= map
;
412 /* Return nonzero if all bits in an element are zero. */
415 bitmap_element_zerop (const bitmap_element
*element
)
417 #if BITMAP_ELEMENT_WORDS == 2
418 return (element
->bits
[0] | element
->bits
[1]) == 0;
422 for (i
= 0; i
< BITMAP_ELEMENT_WORDS
; i
++)
423 if (element
->bits
[i
] != 0)
430 /* Link the bitmap element into the current bitmap linked list. */
433 bitmap_element_link (bitmap head
, bitmap_element
*element
)
435 unsigned int indx
= element
->indx
;
438 /* If this is the first and only element, set it in. */
439 if (head
->first
== 0)
441 element
->next
= element
->prev
= 0;
442 head
->first
= element
;
445 /* If this index is less than that of the current element, it goes someplace
446 before the current element. */
447 else if (indx
< head
->indx
)
449 for (ptr
= head
->current
;
450 ptr
->prev
!= 0 && ptr
->prev
->indx
> indx
;
455 ptr
->prev
->next
= element
;
457 head
->first
= element
;
459 element
->prev
= ptr
->prev
;
464 /* Otherwise, it must go someplace after the current element. */
467 for (ptr
= head
->current
;
468 ptr
->next
!= 0 && ptr
->next
->indx
< indx
;
473 ptr
->next
->prev
= element
;
475 element
->next
= ptr
->next
;
480 /* Set up so this is the first element searched. */
481 head
->current
= element
;
485 /* Insert a new uninitialized element into bitmap HEAD after element
486 ELT. If ELT is NULL, insert the element at the start. Return the
489 static bitmap_element
*
490 bitmap_elt_insert_after (bitmap head
, bitmap_element
*elt
, unsigned int indx
)
492 bitmap_element
*node
= bitmap_element_allocate (head
);
499 head
->current
= node
;
502 node
->next
= head
->first
;
504 node
->next
->prev
= node
;
510 gcc_checking_assert (head
->current
);
511 node
->next
= elt
->next
;
513 node
->next
->prev
= node
;
520 /* Copy a bitmap to another bitmap. */
523 bitmap_copy (bitmap to
, const_bitmap from
)
525 const bitmap_element
*from_ptr
;
526 bitmap_element
*to_ptr
= 0;
530 /* Copy elements in forward direction one at a time. */
531 for (from_ptr
= from
->first
; from_ptr
; from_ptr
= from_ptr
->next
)
533 bitmap_element
*to_elt
= bitmap_element_allocate (to
);
535 to_elt
->indx
= from_ptr
->indx
;
536 memcpy (to_elt
->bits
, from_ptr
->bits
, sizeof (to_elt
->bits
));
538 /* Here we have a special case of bitmap_element_link, for the case
539 where we know the links are being entered in sequence. */
542 to
->first
= to
->current
= to_elt
;
543 to
->indx
= from_ptr
->indx
;
544 to_elt
->next
= to_elt
->prev
= 0;
548 to_elt
->prev
= to_ptr
;
550 to_ptr
->next
= to_elt
;
557 /* Find a bitmap element that would hold a bitmap's bit.
558 Update the `current' field even if we can't find an element that
559 would hold the bitmap's bit to make eventual allocation
562 static inline bitmap_element
*
563 bitmap_find_bit (bitmap head
, unsigned int bit
)
565 bitmap_element
*element
;
566 unsigned int indx
= bit
/ BITMAP_ELEMENT_ALL_BITS
;
568 if (head
->current
== 0
569 || head
->indx
== indx
)
570 return head
->current
;
572 if (GATHER_STATISTICS
)
573 bitmap_descriptors
[head
->descriptor_id
]->nsearches
++;
575 if (head
->indx
< indx
)
576 /* INDX is beyond head->indx. Search from head->current
578 for (element
= head
->current
;
579 element
->next
!= 0 && element
->indx
< indx
;
580 element
= element
->next
)
582 if (GATHER_STATISTICS
)
583 bitmap_descriptors
[head
->descriptor_id
]->search_iter
++;
586 else if (head
->indx
/ 2 < indx
)
587 /* INDX is less than head->indx and closer to head->indx than to
588 0. Search from head->current backward. */
589 for (element
= head
->current
;
590 element
->prev
!= 0 && element
->indx
> indx
;
591 element
= element
->prev
)
593 if (GATHER_STATISTICS
)
594 bitmap_descriptors
[head
->descriptor_id
]->search_iter
++;
598 /* INDX is less than head->indx and closer to 0 than to
599 head->indx. Search from head->first forward. */
600 for (element
= head
->first
;
601 element
->next
!= 0 && element
->indx
< indx
;
602 element
= element
->next
)
603 if (GATHER_STATISTICS
)
605 bitmap_descriptors
[head
->descriptor_id
]->search_iter
++;
608 /* `element' is the nearest to the one we want. If it's not the one we
609 want, the one we want doesn't exist. */
610 head
->current
= element
;
611 head
->indx
= element
->indx
;
612 if (element
!= 0 && element
->indx
!= indx
)
618 /* Clear a single bit in a bitmap. Return true if the bit changed. */
621 bitmap_clear_bit (bitmap head
, int bit
)
623 bitmap_element
*const ptr
= bitmap_find_bit (head
, bit
);
627 unsigned bit_num
= bit
% BITMAP_WORD_BITS
;
628 unsigned word_num
= bit
/ BITMAP_WORD_BITS
% BITMAP_ELEMENT_WORDS
;
629 BITMAP_WORD bit_val
= ((BITMAP_WORD
) 1) << bit_num
;
630 bool res
= (ptr
->bits
[word_num
] & bit_val
) != 0;
633 ptr
->bits
[word_num
] &= ~bit_val
;
634 /* If we cleared the entire word, free up the element. */
635 if (!ptr
->bits
[word_num
]
636 && bitmap_element_zerop (ptr
))
637 bitmap_element_free (head
, ptr
);
646 /* Set a single bit in a bitmap. Return true if the bit changed. */
649 bitmap_set_bit (bitmap head
, int bit
)
651 bitmap_element
*ptr
= bitmap_find_bit (head
, bit
);
652 unsigned word_num
= bit
/ BITMAP_WORD_BITS
% BITMAP_ELEMENT_WORDS
;
653 unsigned bit_num
= bit
% BITMAP_WORD_BITS
;
654 BITMAP_WORD bit_val
= ((BITMAP_WORD
) 1) << bit_num
;
658 ptr
= bitmap_element_allocate (head
);
659 ptr
->indx
= bit
/ BITMAP_ELEMENT_ALL_BITS
;
660 ptr
->bits
[word_num
] = bit_val
;
661 bitmap_element_link (head
, ptr
);
666 bool res
= (ptr
->bits
[word_num
] & bit_val
) == 0;
668 ptr
->bits
[word_num
] |= bit_val
;
673 /* Return whether a bit is set within a bitmap. */
676 bitmap_bit_p (bitmap head
, int bit
)
682 ptr
= bitmap_find_bit (head
, bit
);
686 bit_num
= bit
% BITMAP_WORD_BITS
;
687 word_num
= bit
/ BITMAP_WORD_BITS
% BITMAP_ELEMENT_WORDS
;
689 return (ptr
->bits
[word_num
] >> bit_num
) & 1;
692 #if GCC_VERSION < 3400
693 /* Table of number of set bits in a character, indexed by value of char. */
694 static const unsigned char popcount_table
[] =
696 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,
697 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,
698 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,
699 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,
700 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,
701 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,
702 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,
703 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,
707 bitmap_popcount (BITMAP_WORD a
)
709 unsigned long ret
= 0;
712 /* Just do this the table way for now */
713 for (i
= 0; i
< BITMAP_WORD_BITS
; i
+= 8)
714 ret
+= popcount_table
[(a
>> i
) & 0xff];
718 /* Count the number of bits set in the bitmap, and return it. */
721 bitmap_count_bits (const_bitmap a
)
723 unsigned long count
= 0;
724 const bitmap_element
*elt
;
727 for (elt
= a
->first
; elt
; elt
= elt
->next
)
729 for (ix
= 0; ix
!= BITMAP_ELEMENT_WORDS
; ix
++)
731 #if GCC_VERSION >= 3400
732 /* Note that popcountl matches BITMAP_WORD in type, so the actual size
733 of BITMAP_WORD is not material. */
734 count
+= __builtin_popcountl (elt
->bits
[ix
]);
736 count
+= bitmap_popcount (elt
->bits
[ix
]);
743 /* Return true if the bitmap has a single bit set. Otherwise return
747 bitmap_single_bit_set_p (const_bitmap a
)
749 unsigned long count
= 0;
750 const bitmap_element
*elt
;
753 if (bitmap_empty_p (a
))
757 /* As there are no completely empty bitmap elements, a second one
758 means we have more than one bit set. */
759 if (elt
->next
!= NULL
)
762 for (ix
= 0; ix
!= BITMAP_ELEMENT_WORDS
; ix
++)
764 #if GCC_VERSION >= 3400
765 /* Note that popcountl matches BITMAP_WORD in type, so the actual size
766 of BITMAP_WORD is not material. */
767 count
+= __builtin_popcountl (elt
->bits
[ix
]);
769 count
+= bitmap_popcount (elt
->bits
[ix
]);
779 /* Return the bit number of the first set bit in the bitmap. The
780 bitmap must be non-empty. */
783 bitmap_first_set_bit (const_bitmap a
)
785 const bitmap_element
*elt
= a
->first
;
790 gcc_checking_assert (elt
);
791 bit_no
= elt
->indx
* BITMAP_ELEMENT_ALL_BITS
;
792 for (ix
= 0; ix
!= BITMAP_ELEMENT_WORDS
; ix
++)
794 word
= elt
->bits
[ix
];
800 bit_no
+= ix
* BITMAP_WORD_BITS
;
802 #if GCC_VERSION >= 3004
803 gcc_assert (sizeof(long) == sizeof (word
));
804 bit_no
+= __builtin_ctzl (word
);
806 /* Binary search for the first set bit. */
807 #if BITMAP_WORD_BITS > 64
808 #error "Fill out the table."
810 #if BITMAP_WORD_BITS > 32
811 if (!(word
& 0xffffffff))
812 word
>>= 32, bit_no
+= 32;
814 if (!(word
& 0xffff))
815 word
>>= 16, bit_no
+= 16;
817 word
>>= 8, bit_no
+= 8;
819 word
>>= 4, bit_no
+= 4;
821 word
>>= 2, bit_no
+= 2;
823 word
>>= 1, bit_no
+= 1;
825 gcc_checking_assert (word
& 1);
830 /* Return the bit number of the first set bit in the bitmap. The
831 bitmap must be non-empty. */
834 bitmap_last_set_bit (const_bitmap a
)
836 const bitmap_element
*elt
= a
->current
? a
->current
: a
->first
;
841 gcc_checking_assert (elt
);
844 bit_no
= elt
->indx
* BITMAP_ELEMENT_ALL_BITS
;
845 for (ix
= BITMAP_ELEMENT_WORDS
- 1; ix
>= 0; ix
--)
847 word
= elt
->bits
[ix
];
853 bit_no
+= ix
* BITMAP_WORD_BITS
;
854 #if GCC_VERSION >= 3004
855 gcc_assert (sizeof(long) == sizeof (word
));
856 bit_no
+= BITMAP_WORD_BITS
- __builtin_clzl (word
) - 1;
858 /* Hopefully this is a twos-complement host... */
859 BITMAP_WORD x
= word
;
865 #if BITMAP_WORD_BITS > 32
868 bit_no
+= bitmap_popcount (x
) - 1;
878 bitmap_and (bitmap dst
, const_bitmap a
, const_bitmap b
)
880 bitmap_element
*dst_elt
= dst
->first
;
881 const bitmap_element
*a_elt
= a
->first
;
882 const bitmap_element
*b_elt
= b
->first
;
883 bitmap_element
*dst_prev
= NULL
;
885 gcc_assert (dst
!= a
&& dst
!= b
);
889 bitmap_copy (dst
, a
);
893 while (a_elt
&& b_elt
)
895 if (a_elt
->indx
< b_elt
->indx
)
897 else if (b_elt
->indx
< a_elt
->indx
)
901 /* Matching elts, generate A & B. */
906 dst_elt
= bitmap_elt_insert_after (dst
, dst_prev
, a_elt
->indx
);
908 dst_elt
->indx
= a_elt
->indx
;
909 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
911 BITMAP_WORD r
= a_elt
->bits
[ix
] & b_elt
->bits
[ix
];
913 dst_elt
->bits
[ix
] = r
;
919 dst_elt
= dst_elt
->next
;
925 /* Ensure that dst->current is valid. */
926 dst
->current
= dst
->first
;
927 bitmap_elt_clear_from (dst
, dst_elt
);
928 gcc_checking_assert (!dst
->current
== !dst
->first
);
930 dst
->indx
= dst
->current
->indx
;
933 /* A &= B. Return true if A changed. */
936 bitmap_and_into (bitmap a
, const_bitmap b
)
938 bitmap_element
*a_elt
= a
->first
;
939 const bitmap_element
*b_elt
= b
->first
;
940 bitmap_element
*next
;
941 bool changed
= false;
946 while (a_elt
&& b_elt
)
948 if (a_elt
->indx
< b_elt
->indx
)
951 bitmap_element_free (a
, a_elt
);
955 else if (b_elt
->indx
< a_elt
->indx
)
959 /* Matching elts, generate A &= B. */
963 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
965 BITMAP_WORD r
= a_elt
->bits
[ix
] & b_elt
->bits
[ix
];
966 if (a_elt
->bits
[ix
] != r
)
973 bitmap_element_free (a
, a_elt
);
982 bitmap_elt_clear_from (a
, a_elt
);
985 gcc_checking_assert (!a
->current
== !a
->first
986 && (!a
->current
|| a
->indx
== a
->current
->indx
));
992 /* Insert an element equal to SRC_ELT after DST_PREV, overwriting DST_ELT
993 if non-NULL. CHANGED is true if the destination bitmap had already been
994 changed; the new value of CHANGED is returned. */
997 bitmap_elt_copy (bitmap dst
, bitmap_element
*dst_elt
, bitmap_element
*dst_prev
,
998 const bitmap_element
*src_elt
, bool changed
)
1000 if (!changed
&& dst_elt
&& dst_elt
->indx
== src_elt
->indx
)
1004 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1005 if (src_elt
->bits
[ix
] != dst_elt
->bits
[ix
])
1007 dst_elt
->bits
[ix
] = src_elt
->bits
[ix
];
1015 dst_elt
= bitmap_elt_insert_after (dst
, dst_prev
, src_elt
->indx
);
1017 dst_elt
->indx
= src_elt
->indx
;
1018 memcpy (dst_elt
->bits
, src_elt
->bits
, sizeof (dst_elt
->bits
));
1028 bitmap_and_compl (bitmap dst
, const_bitmap a
, const_bitmap b
)
1030 bitmap_element
*dst_elt
= dst
->first
;
1031 const bitmap_element
*a_elt
= a
->first
;
1032 const bitmap_element
*b_elt
= b
->first
;
1033 bitmap_element
*dst_prev
= NULL
;
1034 bitmap_element
**dst_prev_pnext
= &dst
->first
;
1035 bool changed
= false;
1037 gcc_assert (dst
!= a
&& dst
!= b
);
1041 changed
= !bitmap_empty_p (dst
);
1048 while (b_elt
&& b_elt
->indx
< a_elt
->indx
)
1049 b_elt
= b_elt
->next
;
1051 if (!b_elt
|| b_elt
->indx
> a_elt
->indx
)
1053 changed
= bitmap_elt_copy (dst
, dst_elt
, dst_prev
, a_elt
, changed
);
1054 dst_prev
= *dst_prev_pnext
;
1055 dst_prev_pnext
= &dst_prev
->next
;
1056 dst_elt
= *dst_prev_pnext
;
1057 a_elt
= a_elt
->next
;
1062 /* Matching elts, generate A & ~B. */
1064 BITMAP_WORD ior
= 0;
1066 if (!changed
&& dst_elt
&& dst_elt
->indx
== a_elt
->indx
)
1068 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1070 BITMAP_WORD r
= a_elt
->bits
[ix
] & ~b_elt
->bits
[ix
];
1072 if (dst_elt
->bits
[ix
] != r
)
1075 dst_elt
->bits
[ix
] = r
;
1083 if (!dst_elt
|| dst_elt
->indx
> a_elt
->indx
)
1085 dst_elt
= bitmap_elt_insert_after (dst
, dst_prev
, a_elt
->indx
);
1090 dst_elt
->indx
= a_elt
->indx
;
1091 new_element
= false;
1094 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1096 BITMAP_WORD r
= a_elt
->bits
[ix
] & ~b_elt
->bits
[ix
];
1098 dst_elt
->bits
[ix
] = r
;
1106 changed
|= !new_element
;
1107 bitmap_element_free (dst
, dst_elt
);
1108 dst_elt
= *dst_prev_pnext
;
1114 dst_prev
= *dst_prev_pnext
;
1115 dst_prev_pnext
= &dst_prev
->next
;
1116 dst_elt
= *dst_prev_pnext
;
1118 a_elt
= a_elt
->next
;
1119 b_elt
= b_elt
->next
;
1123 /* Ensure that dst->current is valid. */
1124 dst
->current
= dst
->first
;
1129 bitmap_elt_clear_from (dst
, dst_elt
);
1131 gcc_checking_assert (!dst
->current
== !dst
->first
);
1133 dst
->indx
= dst
->current
->indx
;
1138 /* A &= ~B. Returns true if A changes */
1141 bitmap_and_compl_into (bitmap a
, const_bitmap b
)
1143 bitmap_element
*a_elt
= a
->first
;
1144 const bitmap_element
*b_elt
= b
->first
;
1145 bitmap_element
*next
;
1146 BITMAP_WORD changed
= 0;
1150 if (bitmap_empty_p (a
))
1159 while (a_elt
&& b_elt
)
1161 if (a_elt
->indx
< b_elt
->indx
)
1162 a_elt
= a_elt
->next
;
1163 else if (b_elt
->indx
< a_elt
->indx
)
1164 b_elt
= b_elt
->next
;
1167 /* Matching elts, generate A &= ~B. */
1169 BITMAP_WORD ior
= 0;
1171 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1173 BITMAP_WORD cleared
= a_elt
->bits
[ix
] & b_elt
->bits
[ix
];
1174 BITMAP_WORD r
= a_elt
->bits
[ix
] ^ cleared
;
1176 a_elt
->bits
[ix
] = r
;
1182 bitmap_element_free (a
, a_elt
);
1184 b_elt
= b_elt
->next
;
1187 gcc_checking_assert (!a
->current
== !a
->first
1188 && (!a
->current
|| a
->indx
== a
->current
->indx
));
1189 return changed
!= 0;
1192 /* Set COUNT bits from START in HEAD. */
1194 bitmap_set_range (bitmap head
, unsigned int start
, unsigned int count
)
1196 unsigned int first_index
, end_bit_plus1
, last_index
;
1197 bitmap_element
*elt
, *elt_prev
;
1203 first_index
= start
/ BITMAP_ELEMENT_ALL_BITS
;
1204 end_bit_plus1
= start
+ count
;
1205 last_index
= (end_bit_plus1
- 1) / BITMAP_ELEMENT_ALL_BITS
;
1206 elt
= bitmap_find_bit (head
, start
);
1208 /* If bitmap_find_bit returns zero, the current is the closest block
1209 to the result. Otherwise, just use bitmap_element_allocate to
1210 ensure ELT is set; in the loop below, ELT == NULL means "insert
1211 at the end of the bitmap". */
1214 elt
= bitmap_element_allocate (head
);
1215 elt
->indx
= first_index
;
1216 bitmap_element_link (head
, elt
);
1219 gcc_checking_assert (elt
->indx
== first_index
);
1220 elt_prev
= elt
->prev
;
1221 for (i
= first_index
; i
<= last_index
; i
++)
1223 unsigned elt_start_bit
= i
* BITMAP_ELEMENT_ALL_BITS
;
1224 unsigned elt_end_bit_plus1
= elt_start_bit
+ BITMAP_ELEMENT_ALL_BITS
;
1226 unsigned int first_word_to_mod
;
1227 BITMAP_WORD first_mask
;
1228 unsigned int last_word_to_mod
;
1229 BITMAP_WORD last_mask
;
1232 if (!elt
|| elt
->indx
!= i
)
1233 elt
= bitmap_elt_insert_after (head
, elt_prev
, i
);
1235 if (elt_start_bit
<= start
)
1237 /* The first bit to turn on is somewhere inside this
1239 first_word_to_mod
= (start
- elt_start_bit
) / BITMAP_WORD_BITS
;
1241 /* This mask should have 1s in all bits >= start position. */
1243 (((BITMAP_WORD
) 1) << ((start
% BITMAP_WORD_BITS
))) - 1;
1244 first_mask
= ~first_mask
;
1248 /* The first bit to turn on is below this start of this elt. */
1249 first_word_to_mod
= 0;
1250 first_mask
= ~(BITMAP_WORD
) 0;
1253 if (elt_end_bit_plus1
<= end_bit_plus1
)
1255 /* The last bit to turn on is beyond this elt. */
1256 last_word_to_mod
= BITMAP_ELEMENT_WORDS
- 1;
1257 last_mask
= ~(BITMAP_WORD
) 0;
1261 /* The last bit to turn on is inside to this elt. */
1263 (end_bit_plus1
- elt_start_bit
) / BITMAP_WORD_BITS
;
1265 /* The last mask should have 1s below the end bit. */
1267 (((BITMAP_WORD
) 1) << ((end_bit_plus1
% BITMAP_WORD_BITS
))) - 1;
1270 if (first_word_to_mod
== last_word_to_mod
)
1272 BITMAP_WORD mask
= first_mask
& last_mask
;
1273 elt
->bits
[first_word_to_mod
] |= mask
;
1277 elt
->bits
[first_word_to_mod
] |= first_mask
;
1278 if (BITMAP_ELEMENT_WORDS
> 2)
1279 for (ix
= first_word_to_mod
+ 1; ix
< last_word_to_mod
; ix
++)
1280 elt
->bits
[ix
] = ~(BITMAP_WORD
) 0;
1281 elt
->bits
[last_word_to_mod
] |= last_mask
;
1288 head
->current
= elt
? elt
: elt_prev
;
1289 head
->indx
= head
->current
->indx
;
1292 /* Clear COUNT bits from START in HEAD. */
1294 bitmap_clear_range (bitmap head
, unsigned int start
, unsigned int count
)
1296 unsigned int first_index
, end_bit_plus1
, last_index
;
1297 bitmap_element
*elt
;
1302 first_index
= start
/ BITMAP_ELEMENT_ALL_BITS
;
1303 end_bit_plus1
= start
+ count
;
1304 last_index
= (end_bit_plus1
- 1) / BITMAP_ELEMENT_ALL_BITS
;
1305 elt
= bitmap_find_bit (head
, start
);
1307 /* If bitmap_find_bit returns zero, the current is the closest block
1308 to the result. If the current is less than first index, find the
1309 next one. Otherwise, just set elt to be current. */
1314 if (head
->indx
< first_index
)
1316 elt
= head
->current
->next
;
1321 elt
= head
->current
;
1327 while (elt
&& (elt
->indx
<= last_index
))
1329 bitmap_element
* next_elt
= elt
->next
;
1330 unsigned elt_start_bit
= (elt
->indx
) * BITMAP_ELEMENT_ALL_BITS
;
1331 unsigned elt_end_bit_plus1
= elt_start_bit
+ BITMAP_ELEMENT_ALL_BITS
;
1334 if (elt_start_bit
>= start
&& elt_end_bit_plus1
<= end_bit_plus1
)
1335 /* Get rid of the entire elt and go to the next one. */
1336 bitmap_element_free (head
, elt
);
1339 /* Going to have to knock out some bits in this elt. */
1340 unsigned int first_word_to_mod
;
1341 BITMAP_WORD first_mask
;
1342 unsigned int last_word_to_mod
;
1343 BITMAP_WORD last_mask
;
1347 if (elt_start_bit
<= start
)
1349 /* The first bit to turn off is somewhere inside this
1351 first_word_to_mod
= (start
- elt_start_bit
) / BITMAP_WORD_BITS
;
1353 /* This mask should have 1s in all bits >= start position. */
1355 (((BITMAP_WORD
) 1) << ((start
% BITMAP_WORD_BITS
))) - 1;
1356 first_mask
= ~first_mask
;
1360 /* The first bit to turn off is below this start of this elt. */
1361 first_word_to_mod
= 0;
1363 first_mask
= ~first_mask
;
1366 if (elt_end_bit_plus1
<= end_bit_plus1
)
1368 /* The last bit to turn off is beyond this elt. */
1369 last_word_to_mod
= BITMAP_ELEMENT_WORDS
- 1;
1371 last_mask
= ~last_mask
;
1375 /* The last bit to turn off is inside to this elt. */
1377 (end_bit_plus1
- elt_start_bit
) / BITMAP_WORD_BITS
;
1379 /* The last mask should have 1s below the end bit. */
1381 (((BITMAP_WORD
) 1) << (((end_bit_plus1
) % BITMAP_WORD_BITS
))) - 1;
1385 if (first_word_to_mod
== last_word_to_mod
)
1387 BITMAP_WORD mask
= first_mask
& last_mask
;
1388 elt
->bits
[first_word_to_mod
] &= ~mask
;
1392 elt
->bits
[first_word_to_mod
] &= ~first_mask
;
1393 if (BITMAP_ELEMENT_WORDS
> 2)
1394 for (i
= first_word_to_mod
+ 1; i
< last_word_to_mod
; i
++)
1396 elt
->bits
[last_word_to_mod
] &= ~last_mask
;
1398 for (i
= 0; i
< BITMAP_ELEMENT_WORDS
; i
++)
1404 /* Check to see if there are any bits left. */
1406 bitmap_element_free (head
, elt
);
1413 head
->current
= elt
;
1414 head
->indx
= head
->current
->indx
;
1421 bitmap_compl_and_into (bitmap a
, const_bitmap b
)
1423 bitmap_element
*a_elt
= a
->first
;
1424 const bitmap_element
*b_elt
= b
->first
;
1425 bitmap_element
*a_prev
= NULL
;
1426 bitmap_element
*next
;
1428 gcc_assert (a
!= b
);
1430 if (bitmap_empty_p (a
))
1435 if (bitmap_empty_p (b
))
1441 while (a_elt
|| b_elt
)
1443 if (!b_elt
|| (a_elt
&& a_elt
->indx
< b_elt
->indx
))
1445 /* A is before B. Remove A */
1447 a_prev
= a_elt
->prev
;
1448 bitmap_element_free (a
, a_elt
);
1451 else if (!a_elt
|| b_elt
->indx
< a_elt
->indx
)
1453 /* B is before A. Copy B. */
1454 next
= bitmap_elt_insert_after (a
, a_prev
, b_elt
->indx
);
1455 memcpy (next
->bits
, b_elt
->bits
, sizeof (next
->bits
));
1457 b_elt
= b_elt
->next
;
1461 /* Matching elts, generate A = ~A & B. */
1463 BITMAP_WORD ior
= 0;
1465 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1467 BITMAP_WORD cleared
= a_elt
->bits
[ix
] & b_elt
->bits
[ix
];
1468 BITMAP_WORD r
= b_elt
->bits
[ix
] ^ cleared
;
1470 a_elt
->bits
[ix
] = r
;
1475 bitmap_element_free (a
, a_elt
);
1479 b_elt
= b_elt
->next
;
1482 gcc_checking_assert (!a
->current
== !a
->first
1483 && (!a
->current
|| a
->indx
== a
->current
->indx
));
1488 /* Insert an element corresponding to A_ELT | B_ELT after DST_PREV,
1489 overwriting DST_ELT if non-NULL. CHANGED is true if the destination bitmap
1490 had already been changed; the new value of CHANGED is returned. */
1493 bitmap_elt_ior (bitmap dst
, bitmap_element
*dst_elt
, bitmap_element
*dst_prev
,
1494 const bitmap_element
*a_elt
, const bitmap_element
*b_elt
,
1497 gcc_assert (a_elt
|| b_elt
);
1499 if (a_elt
&& b_elt
&& a_elt
->indx
== b_elt
->indx
)
1501 /* Matching elts, generate A | B. */
1504 if (!changed
&& dst_elt
&& dst_elt
->indx
== a_elt
->indx
)
1506 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1508 BITMAP_WORD r
= a_elt
->bits
[ix
] | b_elt
->bits
[ix
];
1509 if (r
!= dst_elt
->bits
[ix
])
1511 dst_elt
->bits
[ix
] = r
;
1520 dst_elt
= bitmap_elt_insert_after (dst
, dst_prev
, a_elt
->indx
);
1522 dst_elt
->indx
= a_elt
->indx
;
1523 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1525 BITMAP_WORD r
= a_elt
->bits
[ix
] | b_elt
->bits
[ix
];
1526 dst_elt
->bits
[ix
] = r
;
1532 /* Copy a single element. */
1533 const bitmap_element
*src
;
1535 if (!b_elt
|| (a_elt
&& a_elt
->indx
< b_elt
->indx
))
1540 gcc_checking_assert (src
);
1541 changed
= bitmap_elt_copy (dst
, dst_elt
, dst_prev
, src
, changed
);
1547 /* DST = A | B. Return true if DST changes. */
1550 bitmap_ior (bitmap dst
, const_bitmap a
, const_bitmap b
)
1552 bitmap_element
*dst_elt
= dst
->first
;
1553 const bitmap_element
*a_elt
= a
->first
;
1554 const bitmap_element
*b_elt
= b
->first
;
1555 bitmap_element
*dst_prev
= NULL
;
1556 bitmap_element
**dst_prev_pnext
= &dst
->first
;
1557 bool changed
= false;
1559 gcc_assert (dst
!= a
&& dst
!= b
);
1561 while (a_elt
|| b_elt
)
1563 changed
= bitmap_elt_ior (dst
, dst_elt
, dst_prev
, a_elt
, b_elt
, changed
);
1565 if (a_elt
&& b_elt
&& a_elt
->indx
== b_elt
->indx
)
1567 a_elt
= a_elt
->next
;
1568 b_elt
= b_elt
->next
;
1572 if (a_elt
&& (!b_elt
|| a_elt
->indx
<= b_elt
->indx
))
1573 a_elt
= a_elt
->next
;
1574 else if (b_elt
&& (!a_elt
|| b_elt
->indx
<= a_elt
->indx
))
1575 b_elt
= b_elt
->next
;
1578 dst_prev
= *dst_prev_pnext
;
1579 dst_prev_pnext
= &dst_prev
->next
;
1580 dst_elt
= *dst_prev_pnext
;
1586 bitmap_elt_clear_from (dst
, dst_elt
);
1588 gcc_checking_assert (!dst
->current
== !dst
->first
);
1590 dst
->indx
= dst
->current
->indx
;
1594 /* A |= B. Return true if A changes. */
1597 bitmap_ior_into (bitmap a
, const_bitmap b
)
1599 bitmap_element
*a_elt
= a
->first
;
1600 const bitmap_element
*b_elt
= b
->first
;
1601 bitmap_element
*a_prev
= NULL
;
1602 bitmap_element
**a_prev_pnext
= &a
->first
;
1603 bool changed
= false;
1610 /* If A lags behind B, just advance it. */
1611 if (!a_elt
|| a_elt
->indx
== b_elt
->indx
)
1613 changed
= bitmap_elt_ior (a
, a_elt
, a_prev
, a_elt
, b_elt
, changed
);
1614 b_elt
= b_elt
->next
;
1616 else if (a_elt
->indx
> b_elt
->indx
)
1618 changed
= bitmap_elt_copy (a
, NULL
, a_prev
, b_elt
, changed
);
1619 b_elt
= b_elt
->next
;
1622 a_prev
= *a_prev_pnext
;
1623 a_prev_pnext
= &a_prev
->next
;
1624 a_elt
= *a_prev_pnext
;
1627 gcc_checking_assert (!a
->current
== !a
->first
);
1629 a
->indx
= a
->current
->indx
;
1636 bitmap_xor (bitmap dst
, const_bitmap a
, const_bitmap b
)
1638 bitmap_element
*dst_elt
= dst
->first
;
1639 const bitmap_element
*a_elt
= a
->first
;
1640 const bitmap_element
*b_elt
= b
->first
;
1641 bitmap_element
*dst_prev
= NULL
;
1643 gcc_assert (dst
!= a
&& dst
!= b
);
1650 while (a_elt
|| b_elt
)
1652 if (a_elt
&& b_elt
&& a_elt
->indx
== b_elt
->indx
)
1654 /* Matching elts, generate A ^ B. */
1656 BITMAP_WORD ior
= 0;
1659 dst_elt
= bitmap_elt_insert_after (dst
, dst_prev
, a_elt
->indx
);
1661 dst_elt
->indx
= a_elt
->indx
;
1662 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1664 BITMAP_WORD r
= a_elt
->bits
[ix
] ^ b_elt
->bits
[ix
];
1667 dst_elt
->bits
[ix
] = r
;
1669 a_elt
= a_elt
->next
;
1670 b_elt
= b_elt
->next
;
1674 dst_elt
= dst_elt
->next
;
1679 /* Copy a single element. */
1680 const bitmap_element
*src
;
1682 if (!b_elt
|| (a_elt
&& a_elt
->indx
< b_elt
->indx
))
1685 a_elt
= a_elt
->next
;
1690 b_elt
= b_elt
->next
;
1694 dst_elt
= bitmap_elt_insert_after (dst
, dst_prev
, src
->indx
);
1696 dst_elt
->indx
= src
->indx
;
1697 memcpy (dst_elt
->bits
, src
->bits
, sizeof (dst_elt
->bits
));
1699 dst_elt
= dst_elt
->next
;
1702 /* Ensure that dst->current is valid. */
1703 dst
->current
= dst
->first
;
1704 bitmap_elt_clear_from (dst
, dst_elt
);
1705 gcc_checking_assert (!dst
->current
== !dst
->first
);
1707 dst
->indx
= dst
->current
->indx
;
1713 bitmap_xor_into (bitmap a
, const_bitmap b
)
1715 bitmap_element
*a_elt
= a
->first
;
1716 const bitmap_element
*b_elt
= b
->first
;
1717 bitmap_element
*a_prev
= NULL
;
1727 if (!a_elt
|| b_elt
->indx
< a_elt
->indx
)
1730 bitmap_element
*dst
= bitmap_elt_insert_after (a
, a_prev
, b_elt
->indx
);
1731 memcpy (dst
->bits
, b_elt
->bits
, sizeof (dst
->bits
));
1733 b_elt
= b_elt
->next
;
1735 else if (a_elt
->indx
< b_elt
->indx
)
1738 a_elt
= a_elt
->next
;
1742 /* Matching elts, generate A ^= B. */
1744 BITMAP_WORD ior
= 0;
1745 bitmap_element
*next
= a_elt
->next
;
1747 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1749 BITMAP_WORD r
= a_elt
->bits
[ix
] ^ b_elt
->bits
[ix
];
1752 a_elt
->bits
[ix
] = r
;
1754 b_elt
= b_elt
->next
;
1758 bitmap_element_free (a
, a_elt
);
1762 gcc_checking_assert (!a
->current
== !a
->first
);
1764 a
->indx
= a
->current
->indx
;
1767 /* Return true if two bitmaps are identical.
1768 We do not bother with a check for pointer equality, as that never
1769 occurs in practice. */
1772 bitmap_equal_p (const_bitmap a
, const_bitmap b
)
1774 const bitmap_element
*a_elt
;
1775 const bitmap_element
*b_elt
;
1778 for (a_elt
= a
->first
, b_elt
= b
->first
;
1780 a_elt
= a_elt
->next
, b_elt
= b_elt
->next
)
1782 if (a_elt
->indx
!= b_elt
->indx
)
1784 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1785 if (a_elt
->bits
[ix
] != b_elt
->bits
[ix
])
1788 return !a_elt
&& !b_elt
;
1791 /* Return true if A AND B is not empty. */
1794 bitmap_intersect_p (const_bitmap a
, const_bitmap b
)
1796 const bitmap_element
*a_elt
;
1797 const bitmap_element
*b_elt
;
1800 for (a_elt
= a
->first
, b_elt
= b
->first
;
1803 if (a_elt
->indx
< b_elt
->indx
)
1804 a_elt
= a_elt
->next
;
1805 else if (b_elt
->indx
< a_elt
->indx
)
1806 b_elt
= b_elt
->next
;
1809 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1810 if (a_elt
->bits
[ix
] & b_elt
->bits
[ix
])
1812 a_elt
= a_elt
->next
;
1813 b_elt
= b_elt
->next
;
1819 /* Return true if A AND NOT B is not empty. */
1822 bitmap_intersect_compl_p (const_bitmap a
, const_bitmap b
)
1824 const bitmap_element
*a_elt
;
1825 const bitmap_element
*b_elt
;
1827 for (a_elt
= a
->first
, b_elt
= b
->first
;
1830 if (a_elt
->indx
< b_elt
->indx
)
1832 else if (b_elt
->indx
< a_elt
->indx
)
1833 b_elt
= b_elt
->next
;
1836 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1837 if (a_elt
->bits
[ix
] & ~b_elt
->bits
[ix
])
1839 a_elt
= a_elt
->next
;
1840 b_elt
= b_elt
->next
;
1843 return a_elt
!= NULL
;
1847 /* DST = A | (FROM1 & ~FROM2). Return true if DST changes. */
1850 bitmap_ior_and_compl (bitmap dst
, const_bitmap a
, const_bitmap b
, const_bitmap kill
)
1852 bool changed
= false;
1854 bitmap_element
*dst_elt
= dst
->first
;
1855 const bitmap_element
*a_elt
= a
->first
;
1856 const bitmap_element
*b_elt
= b
->first
;
1857 const bitmap_element
*kill_elt
= kill
->first
;
1858 bitmap_element
*dst_prev
= NULL
;
1859 bitmap_element
**dst_prev_pnext
= &dst
->first
;
1861 gcc_assert (dst
!= a
&& dst
!= b
&& dst
!= kill
);
1863 /* Special cases. We don't bother checking for bitmap_equal_p (b, kill). */
1864 if (b
== kill
|| bitmap_empty_p (b
))
1866 changed
= !bitmap_equal_p (dst
, a
);
1868 bitmap_copy (dst
, a
);
1871 if (bitmap_empty_p (kill
))
1872 return bitmap_ior (dst
, a
, b
);
1873 if (bitmap_empty_p (a
))
1874 return bitmap_and_compl (dst
, b
, kill
);
1876 while (a_elt
|| b_elt
)
1878 bool new_element
= false;
1881 while (kill_elt
&& kill_elt
->indx
< b_elt
->indx
)
1882 kill_elt
= kill_elt
->next
;
1884 if (b_elt
&& kill_elt
&& kill_elt
->indx
== b_elt
->indx
1885 && (!a_elt
|| a_elt
->indx
>= b_elt
->indx
))
1887 bitmap_element tmp_elt
;
1890 BITMAP_WORD ior
= 0;
1891 tmp_elt
.indx
= b_elt
->indx
;
1892 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1894 BITMAP_WORD r
= b_elt
->bits
[ix
] & ~kill_elt
->bits
[ix
];
1896 tmp_elt
.bits
[ix
] = r
;
1901 changed
= bitmap_elt_ior (dst
, dst_elt
, dst_prev
,
1902 a_elt
, &tmp_elt
, changed
);
1904 if (a_elt
&& a_elt
->indx
== b_elt
->indx
)
1905 a_elt
= a_elt
->next
;
1908 b_elt
= b_elt
->next
;
1909 kill_elt
= kill_elt
->next
;
1913 changed
= bitmap_elt_ior (dst
, dst_elt
, dst_prev
,
1914 a_elt
, b_elt
, changed
);
1917 if (a_elt
&& b_elt
&& a_elt
->indx
== b_elt
->indx
)
1919 a_elt
= a_elt
->next
;
1920 b_elt
= b_elt
->next
;
1924 if (a_elt
&& (!b_elt
|| a_elt
->indx
<= b_elt
->indx
))
1925 a_elt
= a_elt
->next
;
1926 else if (b_elt
&& (!a_elt
|| b_elt
->indx
<= a_elt
->indx
))
1927 b_elt
= b_elt
->next
;
1933 dst_prev
= *dst_prev_pnext
;
1934 dst_prev_pnext
= &dst_prev
->next
;
1935 dst_elt
= *dst_prev_pnext
;
1942 bitmap_elt_clear_from (dst
, dst_elt
);
1944 gcc_checking_assert (!dst
->current
== !dst
->first
);
1946 dst
->indx
= dst
->current
->indx
;
1951 /* A |= (FROM1 & ~FROM2). Return true if A changes. */
1954 bitmap_ior_and_compl_into (bitmap a
, const_bitmap from1
, const_bitmap from2
)
1959 bitmap_initialize (&tmp
, &bitmap_default_obstack
);
1960 bitmap_and_compl (&tmp
, from1
, from2
);
1961 changed
= bitmap_ior_into (a
, &tmp
);
1962 bitmap_clear (&tmp
);
1967 /* A |= (B & C). Return true if A changes. */
1970 bitmap_ior_and_into (bitmap a
, const_bitmap b
, const_bitmap c
)
1972 bitmap_element
*a_elt
= a
->first
;
1973 const bitmap_element
*b_elt
= b
->first
;
1974 const bitmap_element
*c_elt
= c
->first
;
1975 bitmap_element and_elt
;
1976 bitmap_element
*a_prev
= NULL
;
1977 bitmap_element
**a_prev_pnext
= &a
->first
;
1978 bool changed
= false;
1982 return bitmap_ior_into (a
, b
);
1983 if (bitmap_empty_p (b
) || bitmap_empty_p (c
))
1987 while (b_elt
&& c_elt
)
1989 BITMAP_WORD overall
;
1991 /* Find a common item of B and C. */
1992 while (b_elt
->indx
!= c_elt
->indx
)
1994 if (b_elt
->indx
< c_elt
->indx
)
1996 b_elt
= b_elt
->next
;
2002 c_elt
= c_elt
->next
;
2009 and_elt
.indx
= b_elt
->indx
;
2010 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
2012 and_elt
.bits
[ix
] = b_elt
->bits
[ix
] & c_elt
->bits
[ix
];
2013 overall
|= and_elt
.bits
[ix
];
2016 b_elt
= b_elt
->next
;
2017 c_elt
= c_elt
->next
;
2021 /* Now find a place to insert AND_ELT. */
2024 ix
= a_elt
? a_elt
->indx
: and_elt
.indx
;
2025 if (ix
== and_elt
.indx
)
2026 changed
= bitmap_elt_ior (a
, a_elt
, a_prev
, a_elt
, &and_elt
, changed
);
2027 else if (ix
> and_elt
.indx
)
2028 changed
= bitmap_elt_copy (a
, NULL
, a_prev
, &and_elt
, changed
);
2030 a_prev
= *a_prev_pnext
;
2031 a_prev_pnext
= &a_prev
->next
;
2032 a_elt
= *a_prev_pnext
;
2034 /* If A lagged behind B/C, we advanced it so loop once more. */
2036 while (ix
< and_elt
.indx
);
2040 gcc_checking_assert (!a
->current
== !a
->first
);
2042 a
->indx
= a
->current
->indx
;
2046 /* Compute hash of bitmap (for purposes of hashing). */
2048 bitmap_hash (const_bitmap head
)
2050 const bitmap_element
*ptr
;
2051 BITMAP_WORD hash
= 0;
2054 for (ptr
= head
->first
; ptr
; ptr
= ptr
->next
)
2057 for (ix
= 0; ix
!= BITMAP_ELEMENT_WORDS
; ix
++)
2058 hash
^= ptr
->bits
[ix
];
2060 return (hashval_t
)hash
;
2064 /* Debugging function to print out the contents of a bitmap. */
2067 debug_bitmap_file (FILE *file
, const_bitmap head
)
2069 const bitmap_element
*ptr
;
2071 fprintf (file
, "\nfirst = " HOST_PTR_PRINTF
2072 " current = " HOST_PTR_PRINTF
" indx = %u\n",
2073 (void *) head
->first
, (void *) head
->current
, head
->indx
);
2075 for (ptr
= head
->first
; ptr
; ptr
= ptr
->next
)
2077 unsigned int i
, j
, col
= 26;
2079 fprintf (file
, "\t" HOST_PTR_PRINTF
" next = " HOST_PTR_PRINTF
2080 " prev = " HOST_PTR_PRINTF
" indx = %u\n\t\tbits = {",
2081 (const void*) ptr
, (const void*) ptr
->next
,
2082 (const void*) ptr
->prev
, ptr
->indx
);
2084 for (i
= 0; i
< BITMAP_ELEMENT_WORDS
; i
++)
2085 for (j
= 0; j
< BITMAP_WORD_BITS
; j
++)
2086 if ((ptr
->bits
[i
] >> j
) & 1)
2090 fprintf (file
, "\n\t\t\t");
2094 fprintf (file
, " %u", (ptr
->indx
* BITMAP_ELEMENT_ALL_BITS
2095 + i
* BITMAP_WORD_BITS
+ j
));
2099 fprintf (file
, " }\n");
2103 /* Function to be called from the debugger to print the contents
2107 debug_bitmap (const_bitmap head
)
2109 debug_bitmap_file (stdout
, head
);
2112 /* Function to print out the contents of a bitmap. Unlike debug_bitmap_file,
2113 it does not print anything but the bits. */
2116 bitmap_print (FILE *file
, const_bitmap head
, const char *prefix
, const char *suffix
)
2118 const char *comma
= "";
2122 fputs (prefix
, file
);
2123 EXECUTE_IF_SET_IN_BITMAP (head
, 0, i
, bi
)
2125 fprintf (file
, "%s%d", comma
, i
);
2128 fputs (suffix
, file
);
2132 /* Used to accumulate statistics about bitmap sizes. */
2135 HOST_WIDEST_INT size
;
2139 /* Called via htab_traverse. Output bitmap descriptor pointed out by SLOT
2140 and update statistics. */
2142 print_statistics (void **slot
, void *b
)
2144 bitmap_descriptor d
= (bitmap_descriptor
) *slot
;
2145 struct output_info
*i
= (struct output_info
*) b
;
2150 const char *s1
= d
->file
;
2152 while ((s2
= strstr (s1
, "gcc/")))
2154 sprintf (s
, "%s:%i (%s)", s1
, d
->line
, d
->function
);
2156 fprintf (stderr
, "%-41s %8d %15"HOST_WIDEST_INT_PRINT
"d %15"
2157 HOST_WIDEST_INT_PRINT
"d %15"HOST_WIDEST_INT_PRINT
"d %10d %10d\n",
2158 s
, d
->created
, d
->allocated
, d
->peak
, d
->current
, d
->nsearches
,
2160 i
->size
+= d
->allocated
;
2161 i
->count
+= d
->created
;
2166 /* Output per-bitmap memory usage statistics. */
2168 dump_bitmap_statistics (void)
2170 struct output_info info
;
2172 if (! GATHER_STATISTICS
)
2175 if (!bitmap_desc_hash
)
2178 fprintf (stderr
, "\nBitmap Overall "
2179 " Allocated Peak Leak searched "
2181 fprintf (stderr
, "---------------------------------------------------------------------------------\n");
2184 htab_traverse (bitmap_desc_hash
, print_statistics
, &info
);
2185 fprintf (stderr
, "---------------------------------------------------------------------------------\n");
2186 fprintf (stderr
, "%-40s %9d %15"HOST_WIDEST_INT_PRINT
"d\n",
2187 "Total", info
.count
, info
.size
);
2188 fprintf (stderr
, "---------------------------------------------------------------------------------\n");
2191 #include "gt-bitmap.h"