1 /* Functions to support general ended bitmaps.
2 Copyright (C) 1997-2015 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
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 bitmap_descriptor_d
*);
67 static inline bool equal (const bitmap_descriptor_d
*, const loc
*);
71 bitmap_desc_hasher::hash (const bitmap_descriptor_d
*d
)
73 return htab_hash_pointer (d
->file
) + d
->line
;
77 bitmap_desc_hasher::equal (const bitmap_descriptor_d
*d
, const loc
*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
)
97 bitmap_desc_hash
= new hash_table
<bitmap_desc_hasher
> (10);
100 = bitmap_desc_hash
->find_slot_with_hash (&loc
,
101 htab_hash_pointer (file
) + line
,
106 *slot
= XCNEW (struct bitmap_descriptor_d
);
107 bitmap_descriptors
.safe_push (*slot
);
108 (*slot
)->id
= next_bitmap_desc_id
++;
109 (*slot
)->file
= file
;
110 (*slot
)->function
= function
;
111 (*slot
)->line
= line
;
115 /* Register new bitmap. */
117 bitmap_register (bitmap b MEM_STAT_DECL
)
119 bitmap_descriptor desc
= get_bitmap_descriptor (ALONE_FINAL_PASS_MEM_STAT
);
121 b
->descriptor_id
= desc
->id
;
124 /* Account the overhead. */
126 register_overhead (bitmap b
, int amount
)
128 bitmap_descriptor desc
= bitmap_descriptors
[b
->descriptor_id
];
129 desc
->current
+= amount
;
131 desc
->allocated
+= amount
;
132 if (desc
->peak
< desc
->current
)
133 desc
->peak
= desc
->current
;
137 bitmap_element bitmap_zero_bits
; /* An element of all zero bits. */
138 bitmap_obstack bitmap_default_obstack
; /* The default bitmap obstack. */
139 static int bitmap_default_obstack_depth
;
140 static GTY((deletable
)) bitmap_element
*bitmap_ggc_free
; /* Freelist of
143 static void bitmap_elem_to_freelist (bitmap
, bitmap_element
*);
144 static void bitmap_element_free (bitmap
, bitmap_element
*);
145 static bitmap_element
*bitmap_element_allocate (bitmap
);
146 static int bitmap_element_zerop (const bitmap_element
*);
147 static void bitmap_element_link (bitmap
, bitmap_element
*);
148 static bitmap_element
*bitmap_elt_insert_after (bitmap
, bitmap_element
*, unsigned int);
149 static void bitmap_elt_clear_from (bitmap
, bitmap_element
*);
150 static bitmap_element
*bitmap_find_bit (bitmap
, unsigned int);
153 /* Add ELEM to the appropriate freelist. */
155 bitmap_elem_to_freelist (bitmap head
, bitmap_element
*elt
)
157 bitmap_obstack
*bit_obstack
= head
->obstack
;
162 elt
->prev
= bit_obstack
->elements
;
163 bit_obstack
->elements
= elt
;
167 elt
->prev
= bitmap_ggc_free
;
168 bitmap_ggc_free
= elt
;
172 /* Free a bitmap element. Since these are allocated off the
173 bitmap_obstack, "free" actually means "put onto the freelist". */
176 bitmap_element_free (bitmap head
, bitmap_element
*elt
)
178 bitmap_element
*next
= elt
->next
;
179 bitmap_element
*prev
= elt
->prev
;
187 if (head
->first
== elt
)
190 /* Since the first thing we try is to insert before current,
191 make current the next entry in preference to the previous. */
192 if (head
->current
== elt
)
194 head
->current
= next
!= 0 ? next
: prev
;
196 head
->indx
= head
->current
->indx
;
201 if (GATHER_STATISTICS
)
202 register_overhead (head
, -((int)sizeof (bitmap_element
)));
204 bitmap_elem_to_freelist (head
, elt
);
207 /* Allocate a bitmap element. The bits are cleared, but nothing else is. */
209 static inline bitmap_element
*
210 bitmap_element_allocate (bitmap head
)
212 bitmap_element
*element
;
213 bitmap_obstack
*bit_obstack
= head
->obstack
;
217 element
= bit_obstack
->elements
;
220 /* Use up the inner list first before looking at the next
221 element of the outer list. */
224 bit_obstack
->elements
= element
->next
;
225 bit_obstack
->elements
->prev
= element
->prev
;
228 /* Inner list was just a singleton. */
229 bit_obstack
->elements
= element
->prev
;
231 element
= XOBNEW (&bit_obstack
->obstack
, bitmap_element
);
235 element
= bitmap_ggc_free
;
237 /* Use up the inner list first before looking at the next
238 element of the outer list. */
241 bitmap_ggc_free
= element
->next
;
242 bitmap_ggc_free
->prev
= element
->prev
;
245 /* Inner list was just a singleton. */
246 bitmap_ggc_free
= element
->prev
;
248 element
= ggc_alloc
<bitmap_element
> ();
251 if (GATHER_STATISTICS
)
252 register_overhead (head
, sizeof (bitmap_element
));
254 memset (element
->bits
, 0, sizeof (element
->bits
));
259 /* Remove ELT and all following elements from bitmap HEAD. */
262 bitmap_elt_clear_from (bitmap head
, bitmap_element
*elt
)
264 bitmap_element
*prev
;
265 bitmap_obstack
*bit_obstack
= head
->obstack
;
269 if (GATHER_STATISTICS
)
272 for (prev
= elt
; prev
; prev
= prev
->next
)
274 register_overhead (head
, -sizeof (bitmap_element
) * n
);
281 if (head
->current
->indx
> prev
->indx
)
283 head
->current
= prev
;
284 head
->indx
= prev
->indx
;
290 head
->current
= NULL
;
294 /* Put the entire list onto the free list in one operation. */
297 elt
->prev
= bit_obstack
->elements
;
298 bit_obstack
->elements
= elt
;
302 elt
->prev
= bitmap_ggc_free
;
303 bitmap_ggc_free
= elt
;
307 /* Clear a bitmap by freeing the linked list. */
310 bitmap_clear (bitmap head
)
313 bitmap_elt_clear_from (head
, head
->first
);
316 /* Initialize a bitmap obstack. If BIT_OBSTACK is NULL, initialize
317 the default bitmap obstack. */
320 bitmap_obstack_initialize (bitmap_obstack
*bit_obstack
)
324 if (bitmap_default_obstack_depth
++)
326 bit_obstack
= &bitmap_default_obstack
;
329 #if !defined(__GNUC__) || (__GNUC__ < 2)
330 #define __alignof__(type) 0
333 bit_obstack
->elements
= NULL
;
334 bit_obstack
->heads
= NULL
;
335 obstack_specify_allocation (&bit_obstack
->obstack
, OBSTACK_CHUNK_SIZE
,
336 __alignof__ (bitmap_element
),
341 /* Release the memory from a bitmap obstack. If BIT_OBSTACK is NULL,
342 release the default bitmap obstack. */
345 bitmap_obstack_release (bitmap_obstack
*bit_obstack
)
349 if (--bitmap_default_obstack_depth
)
351 gcc_assert (bitmap_default_obstack_depth
> 0);
354 bit_obstack
= &bitmap_default_obstack
;
357 bit_obstack
->elements
= NULL
;
358 bit_obstack
->heads
= NULL
;
359 obstack_free (&bit_obstack
->obstack
, NULL
);
362 /* Create a new bitmap on an obstack. If BIT_OBSTACK is NULL, create
363 it on the default bitmap obstack. */
366 bitmap_obstack_alloc_stat (bitmap_obstack
*bit_obstack MEM_STAT_DECL
)
371 bit_obstack
= &bitmap_default_obstack
;
372 map
= bit_obstack
->heads
;
374 bit_obstack
->heads
= (struct bitmap_head
*) map
->first
;
376 map
= XOBNEW (&bit_obstack
->obstack
, bitmap_head
);
377 bitmap_initialize_stat (map
, bit_obstack PASS_MEM_STAT
);
379 if (GATHER_STATISTICS
)
380 register_overhead (map
, sizeof (bitmap_head
));
385 /* Create a new GCd bitmap. */
388 bitmap_gc_alloc_stat (ALONE_MEM_STAT_DECL
)
392 map
= ggc_alloc
<bitmap_head
> ();
393 bitmap_initialize_stat (map
, NULL PASS_MEM_STAT
);
395 if (GATHER_STATISTICS
)
396 register_overhead (map
, sizeof (bitmap_head
));
401 /* Release an obstack allocated bitmap. */
404 bitmap_obstack_free (bitmap map
)
409 map
->first
= (bitmap_element
*) map
->obstack
->heads
;
411 if (GATHER_STATISTICS
)
412 register_overhead (map
, -((int)sizeof (bitmap_head
)));
414 map
->obstack
->heads
= map
;
419 /* Return nonzero if all bits in an element are zero. */
422 bitmap_element_zerop (const bitmap_element
*element
)
424 #if BITMAP_ELEMENT_WORDS == 2
425 return (element
->bits
[0] | element
->bits
[1]) == 0;
429 for (i
= 0; i
< BITMAP_ELEMENT_WORDS
; i
++)
430 if (element
->bits
[i
] != 0)
437 /* Link the bitmap element into the current bitmap linked list. */
440 bitmap_element_link (bitmap head
, bitmap_element
*element
)
442 unsigned int indx
= element
->indx
;
445 /* If this is the first and only element, set it in. */
446 if (head
->first
== 0)
448 element
->next
= element
->prev
= 0;
449 head
->first
= element
;
452 /* If this index is less than that of the current element, it goes someplace
453 before the current element. */
454 else if (indx
< head
->indx
)
456 for (ptr
= head
->current
;
457 ptr
->prev
!= 0 && ptr
->prev
->indx
> indx
;
462 ptr
->prev
->next
= element
;
464 head
->first
= element
;
466 element
->prev
= ptr
->prev
;
471 /* Otherwise, it must go someplace after the current element. */
474 for (ptr
= head
->current
;
475 ptr
->next
!= 0 && ptr
->next
->indx
< indx
;
480 ptr
->next
->prev
= element
;
482 element
->next
= ptr
->next
;
487 /* Set up so this is the first element searched. */
488 head
->current
= element
;
492 /* Insert a new uninitialized element into bitmap HEAD after element
493 ELT. If ELT is NULL, insert the element at the start. Return the
496 static bitmap_element
*
497 bitmap_elt_insert_after (bitmap head
, bitmap_element
*elt
, unsigned int indx
)
499 bitmap_element
*node
= bitmap_element_allocate (head
);
506 head
->current
= node
;
509 node
->next
= head
->first
;
511 node
->next
->prev
= node
;
517 gcc_checking_assert (head
->current
);
518 node
->next
= elt
->next
;
520 node
->next
->prev
= node
;
527 /* Copy a bitmap to another bitmap. */
530 bitmap_copy (bitmap to
, const_bitmap from
)
532 const bitmap_element
*from_ptr
;
533 bitmap_element
*to_ptr
= 0;
537 /* Copy elements in forward direction one at a time. */
538 for (from_ptr
= from
->first
; from_ptr
; from_ptr
= from_ptr
->next
)
540 bitmap_element
*to_elt
= bitmap_element_allocate (to
);
542 to_elt
->indx
= from_ptr
->indx
;
543 memcpy (to_elt
->bits
, from_ptr
->bits
, sizeof (to_elt
->bits
));
545 /* Here we have a special case of bitmap_element_link, for the case
546 where we know the links are being entered in sequence. */
549 to
->first
= to
->current
= to_elt
;
550 to
->indx
= from_ptr
->indx
;
551 to_elt
->next
= to_elt
->prev
= 0;
555 to_elt
->prev
= to_ptr
;
557 to_ptr
->next
= to_elt
;
564 /* Find a bitmap element that would hold a bitmap's bit.
565 Update the `current' field even if we can't find an element that
566 would hold the bitmap's bit to make eventual allocation
569 static inline bitmap_element
*
570 bitmap_find_bit (bitmap head
, unsigned int bit
)
572 bitmap_element
*element
;
573 unsigned int indx
= bit
/ BITMAP_ELEMENT_ALL_BITS
;
575 if (head
->current
== NULL
576 || head
->indx
== indx
)
577 return head
->current
;
578 if (head
->current
== head
->first
579 && head
->first
->next
== NULL
)
582 /* This bitmap has more than one element, and we're going to look
583 through the elements list. Count that as a search. */
584 if (GATHER_STATISTICS
)
585 bitmap_descriptors
[head
->descriptor_id
]->nsearches
++;
587 if (head
->indx
< indx
)
588 /* INDX is beyond head->indx. Search from head->current
590 for (element
= head
->current
;
591 element
->next
!= 0 && element
->indx
< indx
;
592 element
= element
->next
)
594 if (GATHER_STATISTICS
)
595 bitmap_descriptors
[head
->descriptor_id
]->search_iter
++;
598 else if (head
->indx
/ 2 < indx
)
599 /* INDX is less than head->indx and closer to head->indx than to
600 0. Search from head->current backward. */
601 for (element
= head
->current
;
602 element
->prev
!= 0 && element
->indx
> indx
;
603 element
= element
->prev
)
605 if (GATHER_STATISTICS
)
606 bitmap_descriptors
[head
->descriptor_id
]->search_iter
++;
610 /* INDX is less than head->indx and closer to 0 than to
611 head->indx. Search from head->first forward. */
612 for (element
= head
->first
;
613 element
->next
!= 0 && element
->indx
< indx
;
614 element
= element
->next
)
615 if (GATHER_STATISTICS
)
617 bitmap_descriptors
[head
->descriptor_id
]->search_iter
++;
620 /* `element' is the nearest to the one we want. If it's not the one we
621 want, the one we want doesn't exist. */
622 head
->current
= element
;
623 head
->indx
= element
->indx
;
624 if (element
!= 0 && element
->indx
!= indx
)
630 /* Clear a single bit in a bitmap. Return true if the bit changed. */
633 bitmap_clear_bit (bitmap head
, int bit
)
635 bitmap_element
*const ptr
= bitmap_find_bit (head
, bit
);
639 unsigned bit_num
= bit
% BITMAP_WORD_BITS
;
640 unsigned word_num
= bit
/ BITMAP_WORD_BITS
% BITMAP_ELEMENT_WORDS
;
641 BITMAP_WORD bit_val
= ((BITMAP_WORD
) 1) << bit_num
;
642 bool res
= (ptr
->bits
[word_num
] & bit_val
) != 0;
645 ptr
->bits
[word_num
] &= ~bit_val
;
646 /* If we cleared the entire word, free up the element. */
647 if (!ptr
->bits
[word_num
]
648 && bitmap_element_zerop (ptr
))
649 bitmap_element_free (head
, ptr
);
658 /* Set a single bit in a bitmap. Return true if the bit changed. */
661 bitmap_set_bit (bitmap head
, int bit
)
663 bitmap_element
*ptr
= bitmap_find_bit (head
, bit
);
664 unsigned word_num
= bit
/ BITMAP_WORD_BITS
% BITMAP_ELEMENT_WORDS
;
665 unsigned bit_num
= bit
% BITMAP_WORD_BITS
;
666 BITMAP_WORD bit_val
= ((BITMAP_WORD
) 1) << bit_num
;
670 ptr
= bitmap_element_allocate (head
);
671 ptr
->indx
= bit
/ BITMAP_ELEMENT_ALL_BITS
;
672 ptr
->bits
[word_num
] = bit_val
;
673 bitmap_element_link (head
, ptr
);
678 bool res
= (ptr
->bits
[word_num
] & bit_val
) == 0;
680 ptr
->bits
[word_num
] |= bit_val
;
685 /* Return whether a bit is set within a bitmap. */
688 bitmap_bit_p (bitmap head
, int bit
)
694 ptr
= bitmap_find_bit (head
, bit
);
698 bit_num
= bit
% BITMAP_WORD_BITS
;
699 word_num
= bit
/ BITMAP_WORD_BITS
% BITMAP_ELEMENT_WORDS
;
701 return (ptr
->bits
[word_num
] >> bit_num
) & 1;
704 #if GCC_VERSION < 3400
705 /* Table of number of set bits in a character, indexed by value of char. */
706 static const unsigned char popcount_table
[] =
708 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,
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 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,
711 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,
712 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,
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 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,
715 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,
719 bitmap_popcount (BITMAP_WORD a
)
721 unsigned long ret
= 0;
724 /* Just do this the table way for now */
725 for (i
= 0; i
< BITMAP_WORD_BITS
; i
+= 8)
726 ret
+= popcount_table
[(a
>> i
) & 0xff];
730 /* Count the number of bits set in the bitmap, and return it. */
733 bitmap_count_bits (const_bitmap a
)
735 unsigned long count
= 0;
736 const bitmap_element
*elt
;
739 for (elt
= a
->first
; elt
; elt
= elt
->next
)
741 for (ix
= 0; ix
!= BITMAP_ELEMENT_WORDS
; ix
++)
743 #if GCC_VERSION >= 3400
744 /* Note that popcountl matches BITMAP_WORD in type, so the actual size
745 of BITMAP_WORD is not material. */
746 count
+= __builtin_popcountl (elt
->bits
[ix
]);
748 count
+= bitmap_popcount (elt
->bits
[ix
]);
755 /* Return true if the bitmap has a single bit set. Otherwise return
759 bitmap_single_bit_set_p (const_bitmap a
)
761 unsigned long count
= 0;
762 const bitmap_element
*elt
;
765 if (bitmap_empty_p (a
))
769 /* As there are no completely empty bitmap elements, a second one
770 means we have more than one bit set. */
771 if (elt
->next
!= NULL
)
774 for (ix
= 0; ix
!= BITMAP_ELEMENT_WORDS
; ix
++)
776 #if GCC_VERSION >= 3400
777 /* Note that popcountl matches BITMAP_WORD in type, so the actual size
778 of BITMAP_WORD is not material. */
779 count
+= __builtin_popcountl (elt
->bits
[ix
]);
781 count
+= bitmap_popcount (elt
->bits
[ix
]);
791 /* Return the bit number of the first set bit in the bitmap. The
792 bitmap must be non-empty. */
795 bitmap_first_set_bit (const_bitmap a
)
797 const bitmap_element
*elt
= a
->first
;
802 gcc_checking_assert (elt
);
803 bit_no
= elt
->indx
* BITMAP_ELEMENT_ALL_BITS
;
804 for (ix
= 0; ix
!= BITMAP_ELEMENT_WORDS
; ix
++)
806 word
= elt
->bits
[ix
];
812 bit_no
+= ix
* BITMAP_WORD_BITS
;
814 #if GCC_VERSION >= 3004
815 gcc_assert (sizeof (long) == sizeof (word
));
816 bit_no
+= __builtin_ctzl (word
);
818 /* Binary search for the first set bit. */
819 #if BITMAP_WORD_BITS > 64
820 #error "Fill out the table."
822 #if BITMAP_WORD_BITS > 32
823 if (!(word
& 0xffffffff))
824 word
>>= 32, bit_no
+= 32;
826 if (!(word
& 0xffff))
827 word
>>= 16, bit_no
+= 16;
829 word
>>= 8, bit_no
+= 8;
831 word
>>= 4, bit_no
+= 4;
833 word
>>= 2, bit_no
+= 2;
835 word
>>= 1, bit_no
+= 1;
837 gcc_checking_assert (word
& 1);
842 /* Return the bit number of the first set bit in the bitmap. The
843 bitmap must be non-empty. */
846 bitmap_last_set_bit (const_bitmap a
)
848 const bitmap_element
*elt
= a
->current
? a
->current
: a
->first
;
853 gcc_checking_assert (elt
);
856 bit_no
= elt
->indx
* BITMAP_ELEMENT_ALL_BITS
;
857 for (ix
= BITMAP_ELEMENT_WORDS
- 1; ix
>= 0; ix
--)
859 word
= elt
->bits
[ix
];
865 bit_no
+= ix
* BITMAP_WORD_BITS
;
866 #if GCC_VERSION >= 3004
867 gcc_assert (sizeof (long) == sizeof (word
));
868 bit_no
+= BITMAP_WORD_BITS
- __builtin_clzl (word
) - 1;
870 /* Hopefully this is a twos-complement host... */
871 BITMAP_WORD x
= word
;
877 #if BITMAP_WORD_BITS > 32
880 bit_no
+= bitmap_popcount (x
) - 1;
890 bitmap_and (bitmap dst
, const_bitmap a
, const_bitmap b
)
892 bitmap_element
*dst_elt
= dst
->first
;
893 const bitmap_element
*a_elt
= a
->first
;
894 const bitmap_element
*b_elt
= b
->first
;
895 bitmap_element
*dst_prev
= NULL
;
897 gcc_assert (dst
!= a
&& dst
!= b
);
901 bitmap_copy (dst
, a
);
905 while (a_elt
&& b_elt
)
907 if (a_elt
->indx
< b_elt
->indx
)
909 else if (b_elt
->indx
< a_elt
->indx
)
913 /* Matching elts, generate A & B. */
918 dst_elt
= bitmap_elt_insert_after (dst
, dst_prev
, a_elt
->indx
);
920 dst_elt
->indx
= a_elt
->indx
;
921 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
923 BITMAP_WORD r
= a_elt
->bits
[ix
] & b_elt
->bits
[ix
];
925 dst_elt
->bits
[ix
] = r
;
931 dst_elt
= dst_elt
->next
;
937 /* Ensure that dst->current is valid. */
938 dst
->current
= dst
->first
;
939 bitmap_elt_clear_from (dst
, dst_elt
);
940 gcc_checking_assert (!dst
->current
== !dst
->first
);
942 dst
->indx
= dst
->current
->indx
;
945 /* A &= B. Return true if A changed. */
948 bitmap_and_into (bitmap a
, const_bitmap b
)
950 bitmap_element
*a_elt
= a
->first
;
951 const bitmap_element
*b_elt
= b
->first
;
952 bitmap_element
*next
;
953 bool changed
= false;
958 while (a_elt
&& b_elt
)
960 if (a_elt
->indx
< b_elt
->indx
)
963 bitmap_element_free (a
, a_elt
);
967 else if (b_elt
->indx
< a_elt
->indx
)
971 /* Matching elts, generate A &= B. */
975 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
977 BITMAP_WORD r
= a_elt
->bits
[ix
] & b_elt
->bits
[ix
];
978 if (a_elt
->bits
[ix
] != r
)
985 bitmap_element_free (a
, a_elt
);
994 bitmap_elt_clear_from (a
, a_elt
);
997 gcc_checking_assert (!a
->current
== !a
->first
998 && (!a
->current
|| a
->indx
== a
->current
->indx
));
1004 /* Insert an element equal to SRC_ELT after DST_PREV, overwriting DST_ELT
1005 if non-NULL. CHANGED is true if the destination bitmap had already been
1006 changed; the new value of CHANGED is returned. */
1009 bitmap_elt_copy (bitmap dst
, bitmap_element
*dst_elt
, bitmap_element
*dst_prev
,
1010 const bitmap_element
*src_elt
, bool changed
)
1012 if (!changed
&& dst_elt
&& dst_elt
->indx
== src_elt
->indx
)
1016 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1017 if (src_elt
->bits
[ix
] != dst_elt
->bits
[ix
])
1019 dst_elt
->bits
[ix
] = src_elt
->bits
[ix
];
1027 dst_elt
= bitmap_elt_insert_after (dst
, dst_prev
, src_elt
->indx
);
1029 dst_elt
->indx
= src_elt
->indx
;
1030 memcpy (dst_elt
->bits
, src_elt
->bits
, sizeof (dst_elt
->bits
));
1040 bitmap_and_compl (bitmap dst
, const_bitmap a
, const_bitmap b
)
1042 bitmap_element
*dst_elt
= dst
->first
;
1043 const bitmap_element
*a_elt
= a
->first
;
1044 const bitmap_element
*b_elt
= b
->first
;
1045 bitmap_element
*dst_prev
= NULL
;
1046 bitmap_element
**dst_prev_pnext
= &dst
->first
;
1047 bool changed
= false;
1049 gcc_assert (dst
!= a
&& dst
!= b
);
1053 changed
= !bitmap_empty_p (dst
);
1060 while (b_elt
&& b_elt
->indx
< a_elt
->indx
)
1061 b_elt
= b_elt
->next
;
1063 if (!b_elt
|| b_elt
->indx
> a_elt
->indx
)
1065 changed
= bitmap_elt_copy (dst
, dst_elt
, dst_prev
, a_elt
, changed
);
1066 dst_prev
= *dst_prev_pnext
;
1067 dst_prev_pnext
= &dst_prev
->next
;
1068 dst_elt
= *dst_prev_pnext
;
1069 a_elt
= a_elt
->next
;
1074 /* Matching elts, generate A & ~B. */
1076 BITMAP_WORD ior
= 0;
1078 if (!changed
&& dst_elt
&& dst_elt
->indx
== a_elt
->indx
)
1080 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1082 BITMAP_WORD r
= a_elt
->bits
[ix
] & ~b_elt
->bits
[ix
];
1084 if (dst_elt
->bits
[ix
] != r
)
1087 dst_elt
->bits
[ix
] = r
;
1095 if (!dst_elt
|| dst_elt
->indx
> a_elt
->indx
)
1097 dst_elt
= bitmap_elt_insert_after (dst
, dst_prev
, a_elt
->indx
);
1102 dst_elt
->indx
= a_elt
->indx
;
1103 new_element
= false;
1106 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1108 BITMAP_WORD r
= a_elt
->bits
[ix
] & ~b_elt
->bits
[ix
];
1110 dst_elt
->bits
[ix
] = r
;
1118 changed
|= !new_element
;
1119 bitmap_element_free (dst
, dst_elt
);
1120 dst_elt
= *dst_prev_pnext
;
1126 dst_prev
= *dst_prev_pnext
;
1127 dst_prev_pnext
= &dst_prev
->next
;
1128 dst_elt
= *dst_prev_pnext
;
1130 a_elt
= a_elt
->next
;
1131 b_elt
= b_elt
->next
;
1135 /* Ensure that dst->current is valid. */
1136 dst
->current
= dst
->first
;
1141 bitmap_elt_clear_from (dst
, dst_elt
);
1143 gcc_checking_assert (!dst
->current
== !dst
->first
);
1145 dst
->indx
= dst
->current
->indx
;
1150 /* A &= ~B. Returns true if A changes */
1153 bitmap_and_compl_into (bitmap a
, const_bitmap b
)
1155 bitmap_element
*a_elt
= a
->first
;
1156 const bitmap_element
*b_elt
= b
->first
;
1157 bitmap_element
*next
;
1158 BITMAP_WORD changed
= 0;
1162 if (bitmap_empty_p (a
))
1171 while (a_elt
&& b_elt
)
1173 if (a_elt
->indx
< b_elt
->indx
)
1174 a_elt
= a_elt
->next
;
1175 else if (b_elt
->indx
< a_elt
->indx
)
1176 b_elt
= b_elt
->next
;
1179 /* Matching elts, generate A &= ~B. */
1181 BITMAP_WORD ior
= 0;
1183 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1185 BITMAP_WORD cleared
= a_elt
->bits
[ix
] & b_elt
->bits
[ix
];
1186 BITMAP_WORD r
= a_elt
->bits
[ix
] ^ cleared
;
1188 a_elt
->bits
[ix
] = r
;
1194 bitmap_element_free (a
, a_elt
);
1196 b_elt
= b_elt
->next
;
1199 gcc_checking_assert (!a
->current
== !a
->first
1200 && (!a
->current
|| a
->indx
== a
->current
->indx
));
1201 return changed
!= 0;
1204 /* Set COUNT bits from START in HEAD. */
1206 bitmap_set_range (bitmap head
, unsigned int start
, unsigned int count
)
1208 unsigned int first_index
, end_bit_plus1
, last_index
;
1209 bitmap_element
*elt
, *elt_prev
;
1217 bitmap_set_bit (head
, start
);
1221 first_index
= start
/ BITMAP_ELEMENT_ALL_BITS
;
1222 end_bit_plus1
= start
+ count
;
1223 last_index
= (end_bit_plus1
- 1) / BITMAP_ELEMENT_ALL_BITS
;
1224 elt
= bitmap_find_bit (head
, start
);
1226 /* If bitmap_find_bit returns zero, the current is the closest block
1227 to the result. Otherwise, just use bitmap_element_allocate to
1228 ensure ELT is set; in the loop below, ELT == NULL means "insert
1229 at the end of the bitmap". */
1232 elt
= bitmap_element_allocate (head
);
1233 elt
->indx
= first_index
;
1234 bitmap_element_link (head
, elt
);
1237 gcc_checking_assert (elt
->indx
== first_index
);
1238 elt_prev
= elt
->prev
;
1239 for (i
= first_index
; i
<= last_index
; i
++)
1241 unsigned elt_start_bit
= i
* BITMAP_ELEMENT_ALL_BITS
;
1242 unsigned elt_end_bit_plus1
= elt_start_bit
+ BITMAP_ELEMENT_ALL_BITS
;
1244 unsigned int first_word_to_mod
;
1245 BITMAP_WORD first_mask
;
1246 unsigned int last_word_to_mod
;
1247 BITMAP_WORD last_mask
;
1250 if (!elt
|| elt
->indx
!= i
)
1251 elt
= bitmap_elt_insert_after (head
, elt_prev
, i
);
1253 if (elt_start_bit
<= start
)
1255 /* The first bit to turn on is somewhere inside this
1257 first_word_to_mod
= (start
- elt_start_bit
) / BITMAP_WORD_BITS
;
1259 /* This mask should have 1s in all bits >= start position. */
1261 (((BITMAP_WORD
) 1) << ((start
% BITMAP_WORD_BITS
))) - 1;
1262 first_mask
= ~first_mask
;
1266 /* The first bit to turn on is below this start of this elt. */
1267 first_word_to_mod
= 0;
1268 first_mask
= ~(BITMAP_WORD
) 0;
1271 if (elt_end_bit_plus1
<= end_bit_plus1
)
1273 /* The last bit to turn on is beyond this elt. */
1274 last_word_to_mod
= BITMAP_ELEMENT_WORDS
- 1;
1275 last_mask
= ~(BITMAP_WORD
) 0;
1279 /* The last bit to turn on is inside to this elt. */
1281 (end_bit_plus1
- elt_start_bit
) / BITMAP_WORD_BITS
;
1283 /* The last mask should have 1s below the end bit. */
1285 (((BITMAP_WORD
) 1) << ((end_bit_plus1
% BITMAP_WORD_BITS
))) - 1;
1288 if (first_word_to_mod
== last_word_to_mod
)
1290 BITMAP_WORD mask
= first_mask
& last_mask
;
1291 elt
->bits
[first_word_to_mod
] |= mask
;
1295 elt
->bits
[first_word_to_mod
] |= first_mask
;
1296 if (BITMAP_ELEMENT_WORDS
> 2)
1297 for (ix
= first_word_to_mod
+ 1; ix
< last_word_to_mod
; ix
++)
1298 elt
->bits
[ix
] = ~(BITMAP_WORD
) 0;
1299 elt
->bits
[last_word_to_mod
] |= last_mask
;
1306 head
->current
= elt
? elt
: elt_prev
;
1307 head
->indx
= head
->current
->indx
;
1310 /* Clear COUNT bits from START in HEAD. */
1312 bitmap_clear_range (bitmap head
, unsigned int start
, unsigned int count
)
1314 unsigned int first_index
, end_bit_plus1
, last_index
;
1315 bitmap_element
*elt
;
1322 bitmap_clear_bit (head
, start
);
1326 first_index
= start
/ BITMAP_ELEMENT_ALL_BITS
;
1327 end_bit_plus1
= start
+ count
;
1328 last_index
= (end_bit_plus1
- 1) / BITMAP_ELEMENT_ALL_BITS
;
1329 elt
= bitmap_find_bit (head
, start
);
1331 /* If bitmap_find_bit returns zero, the current is the closest block
1332 to the result. If the current is less than first index, find the
1333 next one. Otherwise, just set elt to be current. */
1338 if (head
->indx
< first_index
)
1340 elt
= head
->current
->next
;
1345 elt
= head
->current
;
1351 while (elt
&& (elt
->indx
<= last_index
))
1353 bitmap_element
* next_elt
= elt
->next
;
1354 unsigned elt_start_bit
= (elt
->indx
) * BITMAP_ELEMENT_ALL_BITS
;
1355 unsigned elt_end_bit_plus1
= elt_start_bit
+ BITMAP_ELEMENT_ALL_BITS
;
1358 if (elt_start_bit
>= start
&& elt_end_bit_plus1
<= end_bit_plus1
)
1359 /* Get rid of the entire elt and go to the next one. */
1360 bitmap_element_free (head
, elt
);
1363 /* Going to have to knock out some bits in this elt. */
1364 unsigned int first_word_to_mod
;
1365 BITMAP_WORD first_mask
;
1366 unsigned int last_word_to_mod
;
1367 BITMAP_WORD last_mask
;
1371 if (elt_start_bit
<= start
)
1373 /* The first bit to turn off is somewhere inside this
1375 first_word_to_mod
= (start
- elt_start_bit
) / BITMAP_WORD_BITS
;
1377 /* This mask should have 1s in all bits >= start position. */
1379 (((BITMAP_WORD
) 1) << ((start
% BITMAP_WORD_BITS
))) - 1;
1380 first_mask
= ~first_mask
;
1384 /* The first bit to turn off is below this start of this elt. */
1385 first_word_to_mod
= 0;
1387 first_mask
= ~first_mask
;
1390 if (elt_end_bit_plus1
<= end_bit_plus1
)
1392 /* The last bit to turn off is beyond this elt. */
1393 last_word_to_mod
= BITMAP_ELEMENT_WORDS
- 1;
1395 last_mask
= ~last_mask
;
1399 /* The last bit to turn off is inside to this elt. */
1401 (end_bit_plus1
- elt_start_bit
) / BITMAP_WORD_BITS
;
1403 /* The last mask should have 1s below the end bit. */
1405 (((BITMAP_WORD
) 1) << (((end_bit_plus1
) % BITMAP_WORD_BITS
))) - 1;
1409 if (first_word_to_mod
== last_word_to_mod
)
1411 BITMAP_WORD mask
= first_mask
& last_mask
;
1412 elt
->bits
[first_word_to_mod
] &= ~mask
;
1416 elt
->bits
[first_word_to_mod
] &= ~first_mask
;
1417 if (BITMAP_ELEMENT_WORDS
> 2)
1418 for (i
= first_word_to_mod
+ 1; i
< last_word_to_mod
; i
++)
1420 elt
->bits
[last_word_to_mod
] &= ~last_mask
;
1422 for (i
= 0; i
< BITMAP_ELEMENT_WORDS
; i
++)
1428 /* Check to see if there are any bits left. */
1430 bitmap_element_free (head
, elt
);
1437 head
->current
= elt
;
1438 head
->indx
= head
->current
->indx
;
1445 bitmap_compl_and_into (bitmap a
, const_bitmap b
)
1447 bitmap_element
*a_elt
= a
->first
;
1448 const bitmap_element
*b_elt
= b
->first
;
1449 bitmap_element
*a_prev
= NULL
;
1450 bitmap_element
*next
;
1452 gcc_assert (a
!= b
);
1454 if (bitmap_empty_p (a
))
1459 if (bitmap_empty_p (b
))
1465 while (a_elt
|| b_elt
)
1467 if (!b_elt
|| (a_elt
&& a_elt
->indx
< b_elt
->indx
))
1469 /* A is before B. Remove A */
1471 a_prev
= a_elt
->prev
;
1472 bitmap_element_free (a
, a_elt
);
1475 else if (!a_elt
|| b_elt
->indx
< a_elt
->indx
)
1477 /* B is before A. Copy B. */
1478 next
= bitmap_elt_insert_after (a
, a_prev
, b_elt
->indx
);
1479 memcpy (next
->bits
, b_elt
->bits
, sizeof (next
->bits
));
1481 b_elt
= b_elt
->next
;
1485 /* Matching elts, generate A = ~A & B. */
1487 BITMAP_WORD ior
= 0;
1489 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1491 BITMAP_WORD cleared
= a_elt
->bits
[ix
] & b_elt
->bits
[ix
];
1492 BITMAP_WORD r
= b_elt
->bits
[ix
] ^ cleared
;
1494 a_elt
->bits
[ix
] = r
;
1499 bitmap_element_free (a
, a_elt
);
1503 b_elt
= b_elt
->next
;
1506 gcc_checking_assert (!a
->current
== !a
->first
1507 && (!a
->current
|| a
->indx
== a
->current
->indx
));
1512 /* Insert an element corresponding to A_ELT | B_ELT after DST_PREV,
1513 overwriting DST_ELT if non-NULL. CHANGED is true if the destination bitmap
1514 had already been changed; the new value of CHANGED is returned. */
1517 bitmap_elt_ior (bitmap dst
, bitmap_element
*dst_elt
, bitmap_element
*dst_prev
,
1518 const bitmap_element
*a_elt
, const bitmap_element
*b_elt
,
1521 gcc_assert (a_elt
|| b_elt
);
1523 if (a_elt
&& b_elt
&& a_elt
->indx
== b_elt
->indx
)
1525 /* Matching elts, generate A | B. */
1528 if (!changed
&& dst_elt
&& dst_elt
->indx
== a_elt
->indx
)
1530 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1532 BITMAP_WORD r
= a_elt
->bits
[ix
] | b_elt
->bits
[ix
];
1533 if (r
!= dst_elt
->bits
[ix
])
1535 dst_elt
->bits
[ix
] = r
;
1544 dst_elt
= bitmap_elt_insert_after (dst
, dst_prev
, a_elt
->indx
);
1546 dst_elt
->indx
= a_elt
->indx
;
1547 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1549 BITMAP_WORD r
= a_elt
->bits
[ix
] | b_elt
->bits
[ix
];
1550 dst_elt
->bits
[ix
] = r
;
1556 /* Copy a single element. */
1557 const bitmap_element
*src
;
1559 if (!b_elt
|| (a_elt
&& a_elt
->indx
< b_elt
->indx
))
1564 gcc_checking_assert (src
);
1565 changed
= bitmap_elt_copy (dst
, dst_elt
, dst_prev
, src
, changed
);
1571 /* DST = A | B. Return true if DST changes. */
1574 bitmap_ior (bitmap dst
, const_bitmap a
, const_bitmap b
)
1576 bitmap_element
*dst_elt
= dst
->first
;
1577 const bitmap_element
*a_elt
= a
->first
;
1578 const bitmap_element
*b_elt
= b
->first
;
1579 bitmap_element
*dst_prev
= NULL
;
1580 bitmap_element
**dst_prev_pnext
= &dst
->first
;
1581 bool changed
= false;
1583 gcc_assert (dst
!= a
&& dst
!= b
);
1585 while (a_elt
|| b_elt
)
1587 changed
= bitmap_elt_ior (dst
, dst_elt
, dst_prev
, a_elt
, b_elt
, changed
);
1589 if (a_elt
&& b_elt
&& a_elt
->indx
== b_elt
->indx
)
1591 a_elt
= a_elt
->next
;
1592 b_elt
= b_elt
->next
;
1596 if (a_elt
&& (!b_elt
|| a_elt
->indx
<= b_elt
->indx
))
1597 a_elt
= a_elt
->next
;
1598 else if (b_elt
&& (!a_elt
|| b_elt
->indx
<= a_elt
->indx
))
1599 b_elt
= b_elt
->next
;
1602 dst_prev
= *dst_prev_pnext
;
1603 dst_prev_pnext
= &dst_prev
->next
;
1604 dst_elt
= *dst_prev_pnext
;
1610 /* Ensure that dst->current is valid. */
1611 dst
->current
= dst
->first
;
1612 bitmap_elt_clear_from (dst
, dst_elt
);
1614 gcc_checking_assert (!dst
->current
== !dst
->first
);
1616 dst
->indx
= dst
->current
->indx
;
1620 /* A |= B. Return true if A changes. */
1623 bitmap_ior_into (bitmap a
, const_bitmap b
)
1625 bitmap_element
*a_elt
= a
->first
;
1626 const bitmap_element
*b_elt
= b
->first
;
1627 bitmap_element
*a_prev
= NULL
;
1628 bitmap_element
**a_prev_pnext
= &a
->first
;
1629 bool changed
= false;
1636 /* If A lags behind B, just advance it. */
1637 if (!a_elt
|| a_elt
->indx
== b_elt
->indx
)
1639 changed
= bitmap_elt_ior (a
, a_elt
, a_prev
, a_elt
, b_elt
, changed
);
1640 b_elt
= b_elt
->next
;
1642 else if (a_elt
->indx
> b_elt
->indx
)
1644 changed
= bitmap_elt_copy (a
, NULL
, a_prev
, b_elt
, changed
);
1645 b_elt
= b_elt
->next
;
1648 a_prev
= *a_prev_pnext
;
1649 a_prev_pnext
= &a_prev
->next
;
1650 a_elt
= *a_prev_pnext
;
1653 gcc_checking_assert (!a
->current
== !a
->first
);
1655 a
->indx
= a
->current
->indx
;
1662 bitmap_xor (bitmap dst
, const_bitmap a
, const_bitmap b
)
1664 bitmap_element
*dst_elt
= dst
->first
;
1665 const bitmap_element
*a_elt
= a
->first
;
1666 const bitmap_element
*b_elt
= b
->first
;
1667 bitmap_element
*dst_prev
= NULL
;
1669 gcc_assert (dst
!= a
&& dst
!= b
);
1676 while (a_elt
|| b_elt
)
1678 if (a_elt
&& b_elt
&& a_elt
->indx
== b_elt
->indx
)
1680 /* Matching elts, generate A ^ B. */
1682 BITMAP_WORD ior
= 0;
1685 dst_elt
= bitmap_elt_insert_after (dst
, dst_prev
, a_elt
->indx
);
1687 dst_elt
->indx
= a_elt
->indx
;
1688 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1690 BITMAP_WORD r
= a_elt
->bits
[ix
] ^ b_elt
->bits
[ix
];
1693 dst_elt
->bits
[ix
] = r
;
1695 a_elt
= a_elt
->next
;
1696 b_elt
= b_elt
->next
;
1700 dst_elt
= dst_elt
->next
;
1705 /* Copy a single element. */
1706 const bitmap_element
*src
;
1708 if (!b_elt
|| (a_elt
&& a_elt
->indx
< b_elt
->indx
))
1711 a_elt
= a_elt
->next
;
1716 b_elt
= b_elt
->next
;
1720 dst_elt
= bitmap_elt_insert_after (dst
, dst_prev
, src
->indx
);
1722 dst_elt
->indx
= src
->indx
;
1723 memcpy (dst_elt
->bits
, src
->bits
, sizeof (dst_elt
->bits
));
1725 dst_elt
= dst_elt
->next
;
1728 /* Ensure that dst->current is valid. */
1729 dst
->current
= dst
->first
;
1730 bitmap_elt_clear_from (dst
, dst_elt
);
1731 gcc_checking_assert (!dst
->current
== !dst
->first
);
1733 dst
->indx
= dst
->current
->indx
;
1739 bitmap_xor_into (bitmap a
, const_bitmap b
)
1741 bitmap_element
*a_elt
= a
->first
;
1742 const bitmap_element
*b_elt
= b
->first
;
1743 bitmap_element
*a_prev
= NULL
;
1753 if (!a_elt
|| b_elt
->indx
< a_elt
->indx
)
1756 bitmap_element
*dst
= bitmap_elt_insert_after (a
, a_prev
, b_elt
->indx
);
1757 memcpy (dst
->bits
, b_elt
->bits
, sizeof (dst
->bits
));
1759 b_elt
= b_elt
->next
;
1761 else if (a_elt
->indx
< b_elt
->indx
)
1764 a_elt
= a_elt
->next
;
1768 /* Matching elts, generate A ^= B. */
1770 BITMAP_WORD ior
= 0;
1771 bitmap_element
*next
= a_elt
->next
;
1773 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1775 BITMAP_WORD r
= a_elt
->bits
[ix
] ^ b_elt
->bits
[ix
];
1778 a_elt
->bits
[ix
] = r
;
1780 b_elt
= b_elt
->next
;
1784 bitmap_element_free (a
, a_elt
);
1788 gcc_checking_assert (!a
->current
== !a
->first
);
1790 a
->indx
= a
->current
->indx
;
1793 /* Return true if two bitmaps are identical.
1794 We do not bother with a check for pointer equality, as that never
1795 occurs in practice. */
1798 bitmap_equal_p (const_bitmap a
, const_bitmap b
)
1800 const bitmap_element
*a_elt
;
1801 const bitmap_element
*b_elt
;
1804 for (a_elt
= a
->first
, b_elt
= b
->first
;
1806 a_elt
= a_elt
->next
, b_elt
= b_elt
->next
)
1808 if (a_elt
->indx
!= b_elt
->indx
)
1810 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1811 if (a_elt
->bits
[ix
] != b_elt
->bits
[ix
])
1814 return !a_elt
&& !b_elt
;
1817 /* Return true if A AND B is not empty. */
1820 bitmap_intersect_p (const_bitmap a
, const_bitmap b
)
1822 const bitmap_element
*a_elt
;
1823 const bitmap_element
*b_elt
;
1826 for (a_elt
= a
->first
, b_elt
= b
->first
;
1829 if (a_elt
->indx
< b_elt
->indx
)
1830 a_elt
= a_elt
->next
;
1831 else if (b_elt
->indx
< a_elt
->indx
)
1832 b_elt
= b_elt
->next
;
1835 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1836 if (a_elt
->bits
[ix
] & b_elt
->bits
[ix
])
1838 a_elt
= a_elt
->next
;
1839 b_elt
= b_elt
->next
;
1845 /* Return true if A AND NOT B is not empty. */
1848 bitmap_intersect_compl_p (const_bitmap a
, const_bitmap b
)
1850 const bitmap_element
*a_elt
;
1851 const bitmap_element
*b_elt
;
1853 for (a_elt
= a
->first
, b_elt
= b
->first
;
1856 if (a_elt
->indx
< b_elt
->indx
)
1858 else if (b_elt
->indx
< a_elt
->indx
)
1859 b_elt
= b_elt
->next
;
1862 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1863 if (a_elt
->bits
[ix
] & ~b_elt
->bits
[ix
])
1865 a_elt
= a_elt
->next
;
1866 b_elt
= b_elt
->next
;
1869 return a_elt
!= NULL
;
1873 /* DST = A | (FROM1 & ~FROM2). Return true if DST changes. */
1876 bitmap_ior_and_compl (bitmap dst
, const_bitmap a
, const_bitmap b
, const_bitmap kill
)
1878 bool changed
= false;
1880 bitmap_element
*dst_elt
= dst
->first
;
1881 const bitmap_element
*a_elt
= a
->first
;
1882 const bitmap_element
*b_elt
= b
->first
;
1883 const bitmap_element
*kill_elt
= kill
->first
;
1884 bitmap_element
*dst_prev
= NULL
;
1885 bitmap_element
**dst_prev_pnext
= &dst
->first
;
1887 gcc_assert (dst
!= a
&& dst
!= b
&& dst
!= kill
);
1889 /* Special cases. We don't bother checking for bitmap_equal_p (b, kill). */
1890 if (b
== kill
|| bitmap_empty_p (b
))
1892 changed
= !bitmap_equal_p (dst
, a
);
1894 bitmap_copy (dst
, a
);
1897 if (bitmap_empty_p (kill
))
1898 return bitmap_ior (dst
, a
, b
);
1899 if (bitmap_empty_p (a
))
1900 return bitmap_and_compl (dst
, b
, kill
);
1902 while (a_elt
|| b_elt
)
1904 bool new_element
= false;
1907 while (kill_elt
&& kill_elt
->indx
< b_elt
->indx
)
1908 kill_elt
= kill_elt
->next
;
1910 if (b_elt
&& kill_elt
&& kill_elt
->indx
== b_elt
->indx
1911 && (!a_elt
|| a_elt
->indx
>= b_elt
->indx
))
1913 bitmap_element tmp_elt
;
1916 BITMAP_WORD ior
= 0;
1917 tmp_elt
.indx
= b_elt
->indx
;
1918 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1920 BITMAP_WORD r
= b_elt
->bits
[ix
] & ~kill_elt
->bits
[ix
];
1922 tmp_elt
.bits
[ix
] = r
;
1927 changed
= bitmap_elt_ior (dst
, dst_elt
, dst_prev
,
1928 a_elt
, &tmp_elt
, changed
);
1930 if (a_elt
&& a_elt
->indx
== b_elt
->indx
)
1931 a_elt
= a_elt
->next
;
1934 b_elt
= b_elt
->next
;
1935 kill_elt
= kill_elt
->next
;
1939 changed
= bitmap_elt_ior (dst
, dst_elt
, dst_prev
,
1940 a_elt
, b_elt
, changed
);
1943 if (a_elt
&& b_elt
&& a_elt
->indx
== b_elt
->indx
)
1945 a_elt
= a_elt
->next
;
1946 b_elt
= b_elt
->next
;
1950 if (a_elt
&& (!b_elt
|| a_elt
->indx
<= b_elt
->indx
))
1951 a_elt
= a_elt
->next
;
1952 else if (b_elt
&& (!a_elt
|| b_elt
->indx
<= a_elt
->indx
))
1953 b_elt
= b_elt
->next
;
1959 dst_prev
= *dst_prev_pnext
;
1960 dst_prev_pnext
= &dst_prev
->next
;
1961 dst_elt
= *dst_prev_pnext
;
1968 /* Ensure that dst->current is valid. */
1969 dst
->current
= dst
->first
;
1970 bitmap_elt_clear_from (dst
, dst_elt
);
1972 gcc_checking_assert (!dst
->current
== !dst
->first
);
1974 dst
->indx
= dst
->current
->indx
;
1979 /* A |= (FROM1 & ~FROM2). Return true if A changes. */
1982 bitmap_ior_and_compl_into (bitmap a
, const_bitmap from1
, const_bitmap from2
)
1987 bitmap_initialize (&tmp
, &bitmap_default_obstack
);
1988 bitmap_and_compl (&tmp
, from1
, from2
);
1989 changed
= bitmap_ior_into (a
, &tmp
);
1990 bitmap_clear (&tmp
);
1995 /* A |= (B & C). Return true if A changes. */
1998 bitmap_ior_and_into (bitmap a
, const_bitmap b
, const_bitmap c
)
2000 bitmap_element
*a_elt
= a
->first
;
2001 const bitmap_element
*b_elt
= b
->first
;
2002 const bitmap_element
*c_elt
= c
->first
;
2003 bitmap_element and_elt
;
2004 bitmap_element
*a_prev
= NULL
;
2005 bitmap_element
**a_prev_pnext
= &a
->first
;
2006 bool changed
= false;
2010 return bitmap_ior_into (a
, b
);
2011 if (bitmap_empty_p (b
) || bitmap_empty_p (c
))
2015 while (b_elt
&& c_elt
)
2017 BITMAP_WORD overall
;
2019 /* Find a common item of B and C. */
2020 while (b_elt
->indx
!= c_elt
->indx
)
2022 if (b_elt
->indx
< c_elt
->indx
)
2024 b_elt
= b_elt
->next
;
2030 c_elt
= c_elt
->next
;
2037 and_elt
.indx
= b_elt
->indx
;
2038 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
2040 and_elt
.bits
[ix
] = b_elt
->bits
[ix
] & c_elt
->bits
[ix
];
2041 overall
|= and_elt
.bits
[ix
];
2044 b_elt
= b_elt
->next
;
2045 c_elt
= c_elt
->next
;
2049 /* Now find a place to insert AND_ELT. */
2052 ix
= a_elt
? a_elt
->indx
: and_elt
.indx
;
2053 if (ix
== and_elt
.indx
)
2054 changed
= bitmap_elt_ior (a
, a_elt
, a_prev
, a_elt
, &and_elt
, changed
);
2055 else if (ix
> and_elt
.indx
)
2056 changed
= bitmap_elt_copy (a
, NULL
, a_prev
, &and_elt
, changed
);
2058 a_prev
= *a_prev_pnext
;
2059 a_prev_pnext
= &a_prev
->next
;
2060 a_elt
= *a_prev_pnext
;
2062 /* If A lagged behind B/C, we advanced it so loop once more. */
2064 while (ix
< and_elt
.indx
);
2068 gcc_checking_assert (!a
->current
== !a
->first
);
2070 a
->indx
= a
->current
->indx
;
2074 /* Compute hash of bitmap (for purposes of hashing). */
2076 bitmap_hash (const_bitmap head
)
2078 const bitmap_element
*ptr
;
2079 BITMAP_WORD hash
= 0;
2082 for (ptr
= head
->first
; ptr
; ptr
= ptr
->next
)
2085 for (ix
= 0; ix
!= BITMAP_ELEMENT_WORDS
; ix
++)
2086 hash
^= ptr
->bits
[ix
];
2088 return (hashval_t
)hash
;
2092 /* Debugging function to print out the contents of a bitmap. */
2095 debug_bitmap_file (FILE *file
, const_bitmap head
)
2097 const bitmap_element
*ptr
;
2099 fprintf (file
, "\nfirst = " HOST_PTR_PRINTF
2100 " current = " HOST_PTR_PRINTF
" indx = %u\n",
2101 (void *) head
->first
, (void *) head
->current
, head
->indx
);
2103 for (ptr
= head
->first
; ptr
; ptr
= ptr
->next
)
2105 unsigned int i
, j
, col
= 26;
2107 fprintf (file
, "\t" HOST_PTR_PRINTF
" next = " HOST_PTR_PRINTF
2108 " prev = " HOST_PTR_PRINTF
" indx = %u\n\t\tbits = {",
2109 (const void*) ptr
, (const void*) ptr
->next
,
2110 (const void*) ptr
->prev
, ptr
->indx
);
2112 for (i
= 0; i
< BITMAP_ELEMENT_WORDS
; i
++)
2113 for (j
= 0; j
< BITMAP_WORD_BITS
; j
++)
2114 if ((ptr
->bits
[i
] >> j
) & 1)
2118 fprintf (file
, "\n\t\t\t");
2122 fprintf (file
, " %u", (ptr
->indx
* BITMAP_ELEMENT_ALL_BITS
2123 + i
* BITMAP_WORD_BITS
+ j
));
2127 fprintf (file
, " }\n");
2131 /* Function to be called from the debugger to print the contents
2135 debug_bitmap (const_bitmap head
)
2137 debug_bitmap_file (stderr
, head
);
2140 /* Function to print out the contents of a bitmap. Unlike debug_bitmap_file,
2141 it does not print anything but the bits. */
2144 bitmap_print (FILE *file
, const_bitmap head
, const char *prefix
,
2147 const char *comma
= "";
2151 fputs (prefix
, file
);
2152 EXECUTE_IF_SET_IN_BITMAP (head
, 0, i
, bi
)
2154 fprintf (file
, "%s%d", comma
, i
);
2157 fputs (suffix
, file
);
2161 /* Used to accumulate statistics about bitmap sizes. */
2162 struct bitmap_output_info
2168 /* Called via hash_table::traverse. Output bitmap descriptor pointed out by
2169 SLOT and update statistics. */
2171 print_statistics (bitmap_descriptor_d
**slot
, bitmap_output_info
*i
)
2173 bitmap_descriptor d
= *slot
;
2178 const char *s1
= d
->file
;
2180 while ((s2
= strstr (s1
, "gcc/")))
2182 sprintf (s
, "%s:%i (%s)", s1
, d
->line
, d
->function
);
2185 "%-41s %9u %15" PRId64
" %15" PRId64
" %15" PRId64
2186 " %10" PRId64
" %10" PRId64
"\n",
2188 d
->allocated
, d
->peak
, d
->current
,
2189 d
->nsearches
, d
->search_iter
);
2190 i
->size
+= d
->allocated
;
2191 i
->count
+= d
->created
;
2196 /* Output per-bitmap memory usage statistics. */
2198 dump_bitmap_statistics (void)
2200 struct bitmap_output_info info
;
2202 if (! GATHER_STATISTICS
)
2205 if (!bitmap_desc_hash
)
2209 "\n%-41s %9s %15s %15s %15s %10s %10s\n",
2210 "Bitmap", "Overall",
2211 "Allocated", "Peak", "Leak",
2212 "searched", "search_itr");
2213 fprintf (stderr
, "---------------------------------------------------------------------------------\n");
2216 bitmap_desc_hash
->traverse
<bitmap_output_info
*, print_statistics
> (&info
);
2217 fprintf (stderr
, "---------------------------------------------------------------------------------\n");
2219 "%-41s %9" PRId64
" %15" PRId64
"\n",
2220 "Total", info
.count
, info
.size
);
2221 fprintf (stderr
, "---------------------------------------------------------------------------------\n");
2225 debug (const bitmap_head
&ref
)
2227 dump_bitmap (stderr
, &ref
);
2231 debug (const bitmap_head
*ptr
)
2236 fprintf (stderr
, "<nil>\n");
2240 #include "gt-bitmap.h"