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"
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 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 if (desc
->peak
< desc
->current
)
125 desc
->peak
= desc
->current
;
129 bitmap_element bitmap_zero_bits
; /* An element of all zero bits. */
130 bitmap_obstack bitmap_default_obstack
; /* The default bitmap obstack. */
131 static int bitmap_default_obstack_depth
;
132 static GTY((deletable
)) bitmap_element
*bitmap_ggc_free
; /* Freelist of
135 static void bitmap_elem_to_freelist (bitmap
, bitmap_element
*);
136 static void bitmap_element_free (bitmap
, bitmap_element
*);
137 static bitmap_element
*bitmap_element_allocate (bitmap
);
138 static int bitmap_element_zerop (const bitmap_element
*);
139 static void bitmap_element_link (bitmap
, bitmap_element
*);
140 static bitmap_element
*bitmap_elt_insert_after (bitmap
, bitmap_element
*, unsigned int);
141 static void bitmap_elt_clear_from (bitmap
, bitmap_element
*);
142 static bitmap_element
*bitmap_find_bit (bitmap
, unsigned int);
145 /* Add ELEM to the appropriate freelist. */
147 bitmap_elem_to_freelist (bitmap head
, bitmap_element
*elt
)
149 bitmap_obstack
*bit_obstack
= head
->obstack
;
154 elt
->prev
= bit_obstack
->elements
;
155 bit_obstack
->elements
= elt
;
159 elt
->prev
= bitmap_ggc_free
;
160 bitmap_ggc_free
= elt
;
164 /* Free a bitmap element. Since these are allocated off the
165 bitmap_obstack, "free" actually means "put onto the freelist". */
168 bitmap_element_free (bitmap head
, bitmap_element
*elt
)
170 bitmap_element
*next
= elt
->next
;
171 bitmap_element
*prev
= elt
->prev
;
179 if (head
->first
== elt
)
182 /* Since the first thing we try is to insert before current,
183 make current the next entry in preference to the previous. */
184 if (head
->current
== elt
)
186 head
->current
= next
!= 0 ? next
: prev
;
188 head
->indx
= head
->current
->indx
;
193 if (GATHER_STATISTICS
)
194 register_overhead (head
, -((int)sizeof (bitmap_element
)));
196 bitmap_elem_to_freelist (head
, elt
);
199 /* Allocate a bitmap element. The bits are cleared, but nothing else is. */
201 static inline bitmap_element
*
202 bitmap_element_allocate (bitmap head
)
204 bitmap_element
*element
;
205 bitmap_obstack
*bit_obstack
= head
->obstack
;
209 element
= bit_obstack
->elements
;
212 /* Use up the inner list first before looking at the next
213 element of the outer list. */
216 bit_obstack
->elements
= element
->next
;
217 bit_obstack
->elements
->prev
= element
->prev
;
220 /* Inner list was just a singleton. */
221 bit_obstack
->elements
= element
->prev
;
223 element
= XOBNEW (&bit_obstack
->obstack
, bitmap_element
);
227 element
= bitmap_ggc_free
;
229 /* Use up the inner list first before looking at the next
230 element of the outer list. */
233 bitmap_ggc_free
= element
->next
;
234 bitmap_ggc_free
->prev
= element
->prev
;
237 /* Inner list was just a singleton. */
238 bitmap_ggc_free
= element
->prev
;
240 element
= ggc_alloc_bitmap_element_def ();
243 if (GATHER_STATISTICS
)
244 register_overhead (head
, sizeof (bitmap_element
));
246 memset (element
->bits
, 0, sizeof (element
->bits
));
251 /* Remove ELT and all following elements from bitmap HEAD. */
254 bitmap_elt_clear_from (bitmap head
, bitmap_element
*elt
)
256 bitmap_element
*prev
;
257 bitmap_obstack
*bit_obstack
= head
->obstack
;
261 if (GATHER_STATISTICS
)
264 for (prev
= elt
; prev
; prev
= prev
->next
)
266 register_overhead (head
, -sizeof (bitmap_element
) * n
);
273 if (head
->current
->indx
> prev
->indx
)
275 head
->current
= prev
;
276 head
->indx
= prev
->indx
;
282 head
->current
= NULL
;
286 /* Put the entire list onto the free list in one operation. */
289 elt
->prev
= bit_obstack
->elements
;
290 bit_obstack
->elements
= elt
;
294 elt
->prev
= bitmap_ggc_free
;
295 bitmap_ggc_free
= elt
;
299 /* Clear a bitmap by freeing the linked list. */
302 bitmap_clear (bitmap head
)
305 bitmap_elt_clear_from (head
, head
->first
);
308 /* Initialize a bitmap obstack. If BIT_OBSTACK is NULL, initialize
309 the default bitmap obstack. */
312 bitmap_obstack_initialize (bitmap_obstack
*bit_obstack
)
316 if (bitmap_default_obstack_depth
++)
318 bit_obstack
= &bitmap_default_obstack
;
321 #if !defined(__GNUC__) || (__GNUC__ < 2)
322 #define __alignof__(type) 0
325 bit_obstack
->elements
= NULL
;
326 bit_obstack
->heads
= NULL
;
327 obstack_specify_allocation (&bit_obstack
->obstack
, OBSTACK_CHUNK_SIZE
,
328 __alignof__ (bitmap_element
),
333 /* Release the memory from a bitmap obstack. If BIT_OBSTACK is NULL,
334 release the default bitmap obstack. */
337 bitmap_obstack_release (bitmap_obstack
*bit_obstack
)
341 if (--bitmap_default_obstack_depth
)
343 gcc_assert (bitmap_default_obstack_depth
> 0);
346 bit_obstack
= &bitmap_default_obstack
;
349 bit_obstack
->elements
= NULL
;
350 bit_obstack
->heads
= NULL
;
351 obstack_free (&bit_obstack
->obstack
, NULL
);
354 /* Create a new bitmap on an obstack. If BIT_OBSTACK is NULL, create
355 it on the default bitmap obstack. */
358 bitmap_obstack_alloc_stat (bitmap_obstack
*bit_obstack MEM_STAT_DECL
)
363 bit_obstack
= &bitmap_default_obstack
;
364 map
= bit_obstack
->heads
;
366 bit_obstack
->heads
= (struct bitmap_head_def
*) map
->first
;
368 map
= XOBNEW (&bit_obstack
->obstack
, bitmap_head
);
369 bitmap_initialize_stat (map
, bit_obstack PASS_MEM_STAT
);
371 if (GATHER_STATISTICS
)
372 register_overhead (map
, sizeof (bitmap_head
));
377 /* Create a new GCd bitmap. */
380 bitmap_gc_alloc_stat (ALONE_MEM_STAT_DECL
)
384 map
= ggc_alloc_bitmap_head_def ();
385 bitmap_initialize_stat (map
, NULL PASS_MEM_STAT
);
387 if (GATHER_STATISTICS
)
388 register_overhead (map
, sizeof (bitmap_head
));
393 /* Release an obstack allocated bitmap. */
396 bitmap_obstack_free (bitmap map
)
401 map
->first
= (bitmap_element
*) map
->obstack
->heads
;
403 if (GATHER_STATISTICS
)
404 register_overhead (map
, -((int)sizeof (bitmap_head
)));
406 map
->obstack
->heads
= map
;
411 /* Return nonzero if all bits in an element are zero. */
414 bitmap_element_zerop (const bitmap_element
*element
)
416 #if BITMAP_ELEMENT_WORDS == 2
417 return (element
->bits
[0] | element
->bits
[1]) == 0;
421 for (i
= 0; i
< BITMAP_ELEMENT_WORDS
; i
++)
422 if (element
->bits
[i
] != 0)
429 /* Link the bitmap element into the current bitmap linked list. */
432 bitmap_element_link (bitmap head
, bitmap_element
*element
)
434 unsigned int indx
= element
->indx
;
437 /* If this is the first and only element, set it in. */
438 if (head
->first
== 0)
440 element
->next
= element
->prev
= 0;
441 head
->first
= element
;
444 /* If this index is less than that of the current element, it goes someplace
445 before the current element. */
446 else if (indx
< head
->indx
)
448 for (ptr
= head
->current
;
449 ptr
->prev
!= 0 && ptr
->prev
->indx
> indx
;
454 ptr
->prev
->next
= element
;
456 head
->first
= element
;
458 element
->prev
= ptr
->prev
;
463 /* Otherwise, it must go someplace after the current element. */
466 for (ptr
= head
->current
;
467 ptr
->next
!= 0 && ptr
->next
->indx
< indx
;
472 ptr
->next
->prev
= element
;
474 element
->next
= ptr
->next
;
479 /* Set up so this is the first element searched. */
480 head
->current
= element
;
484 /* Insert a new uninitialized element into bitmap HEAD after element
485 ELT. If ELT is NULL, insert the element at the start. Return the
488 static bitmap_element
*
489 bitmap_elt_insert_after (bitmap head
, bitmap_element
*elt
, unsigned int indx
)
491 bitmap_element
*node
= bitmap_element_allocate (head
);
498 head
->current
= node
;
501 node
->next
= head
->first
;
503 node
->next
->prev
= node
;
509 gcc_checking_assert (head
->current
);
510 node
->next
= elt
->next
;
512 node
->next
->prev
= node
;
519 /* Copy a bitmap to another bitmap. */
522 bitmap_copy (bitmap to
, const_bitmap from
)
524 const bitmap_element
*from_ptr
;
525 bitmap_element
*to_ptr
= 0;
529 /* Copy elements in forward direction one at a time. */
530 for (from_ptr
= from
->first
; from_ptr
; from_ptr
= from_ptr
->next
)
532 bitmap_element
*to_elt
= bitmap_element_allocate (to
);
534 to_elt
->indx
= from_ptr
->indx
;
535 memcpy (to_elt
->bits
, from_ptr
->bits
, sizeof (to_elt
->bits
));
537 /* Here we have a special case of bitmap_element_link, for the case
538 where we know the links are being entered in sequence. */
541 to
->first
= to
->current
= to_elt
;
542 to
->indx
= from_ptr
->indx
;
543 to_elt
->next
= to_elt
->prev
= 0;
547 to_elt
->prev
= to_ptr
;
549 to_ptr
->next
= to_elt
;
556 /* Find a bitmap element that would hold a bitmap's bit.
557 Update the `current' field even if we can't find an element that
558 would hold the bitmap's bit to make eventual allocation
561 static inline bitmap_element
*
562 bitmap_find_bit (bitmap head
, unsigned int bit
)
564 bitmap_element
*element
;
565 unsigned int indx
= bit
/ BITMAP_ELEMENT_ALL_BITS
;
567 if (head
->current
== NULL
568 || head
->indx
== indx
)
569 return head
->current
;
570 if (head
->current
== head
->first
571 && head
->first
->next
== NULL
)
574 /* This bitmap has more than one element, and we're going to look
575 through the elements list. Count that as a search. */
576 if (GATHER_STATISTICS
)
577 bitmap_descriptors
[head
->descriptor_id
]->nsearches
++;
579 if (head
->indx
< indx
)
580 /* INDX is beyond head->indx. Search from head->current
582 for (element
= head
->current
;
583 element
->next
!= 0 && element
->indx
< indx
;
584 element
= element
->next
)
586 if (GATHER_STATISTICS
)
587 bitmap_descriptors
[head
->descriptor_id
]->search_iter
++;
590 else if (head
->indx
/ 2 < indx
)
591 /* INDX is less than head->indx and closer to head->indx than to
592 0. Search from head->current backward. */
593 for (element
= head
->current
;
594 element
->prev
!= 0 && element
->indx
> indx
;
595 element
= element
->prev
)
597 if (GATHER_STATISTICS
)
598 bitmap_descriptors
[head
->descriptor_id
]->search_iter
++;
602 /* INDX is less than head->indx and closer to 0 than to
603 head->indx. Search from head->first forward. */
604 for (element
= head
->first
;
605 element
->next
!= 0 && element
->indx
< indx
;
606 element
= element
->next
)
607 if (GATHER_STATISTICS
)
609 bitmap_descriptors
[head
->descriptor_id
]->search_iter
++;
612 /* `element' is the nearest to the one we want. If it's not the one we
613 want, the one we want doesn't exist. */
614 head
->current
= element
;
615 head
->indx
= element
->indx
;
616 if (element
!= 0 && element
->indx
!= indx
)
622 /* Clear a single bit in a bitmap. Return true if the bit changed. */
625 bitmap_clear_bit (bitmap head
, int bit
)
627 bitmap_element
*const ptr
= bitmap_find_bit (head
, bit
);
631 unsigned bit_num
= bit
% BITMAP_WORD_BITS
;
632 unsigned word_num
= bit
/ BITMAP_WORD_BITS
% BITMAP_ELEMENT_WORDS
;
633 BITMAP_WORD bit_val
= ((BITMAP_WORD
) 1) << bit_num
;
634 bool res
= (ptr
->bits
[word_num
] & bit_val
) != 0;
637 ptr
->bits
[word_num
] &= ~bit_val
;
638 /* If we cleared the entire word, free up the element. */
639 if (!ptr
->bits
[word_num
]
640 && bitmap_element_zerop (ptr
))
641 bitmap_element_free (head
, ptr
);
650 /* Set a single bit in a bitmap. Return true if the bit changed. */
653 bitmap_set_bit (bitmap head
, int bit
)
655 bitmap_element
*ptr
= bitmap_find_bit (head
, bit
);
656 unsigned word_num
= bit
/ BITMAP_WORD_BITS
% BITMAP_ELEMENT_WORDS
;
657 unsigned bit_num
= bit
% BITMAP_WORD_BITS
;
658 BITMAP_WORD bit_val
= ((BITMAP_WORD
) 1) << bit_num
;
662 ptr
= bitmap_element_allocate (head
);
663 ptr
->indx
= bit
/ BITMAP_ELEMENT_ALL_BITS
;
664 ptr
->bits
[word_num
] = bit_val
;
665 bitmap_element_link (head
, ptr
);
670 bool res
= (ptr
->bits
[word_num
] & bit_val
) == 0;
672 ptr
->bits
[word_num
] |= bit_val
;
677 /* Return whether a bit is set within a bitmap. */
680 bitmap_bit_p (bitmap head
, int bit
)
686 ptr
= bitmap_find_bit (head
, bit
);
690 bit_num
= bit
% BITMAP_WORD_BITS
;
691 word_num
= bit
/ BITMAP_WORD_BITS
% BITMAP_ELEMENT_WORDS
;
693 return (ptr
->bits
[word_num
] >> bit_num
) & 1;
696 #if GCC_VERSION < 3400
697 /* Table of number of set bits in a character, indexed by value of char. */
698 static const unsigned char popcount_table
[] =
700 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,
701 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,
702 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,
703 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,
704 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,
705 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,
706 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,
707 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,
711 bitmap_popcount (BITMAP_WORD a
)
713 unsigned long ret
= 0;
716 /* Just do this the table way for now */
717 for (i
= 0; i
< BITMAP_WORD_BITS
; i
+= 8)
718 ret
+= popcount_table
[(a
>> i
) & 0xff];
722 /* Count the number of bits set in the bitmap, and return it. */
725 bitmap_count_bits (const_bitmap a
)
727 unsigned long count
= 0;
728 const bitmap_element
*elt
;
731 for (elt
= a
->first
; elt
; elt
= elt
->next
)
733 for (ix
= 0; ix
!= BITMAP_ELEMENT_WORDS
; ix
++)
735 #if GCC_VERSION >= 3400
736 /* Note that popcountl matches BITMAP_WORD in type, so the actual size
737 of BITMAP_WORD is not material. */
738 count
+= __builtin_popcountl (elt
->bits
[ix
]);
740 count
+= bitmap_popcount (elt
->bits
[ix
]);
747 /* Return true if the bitmap has a single bit set. Otherwise return
751 bitmap_single_bit_set_p (const_bitmap a
)
753 unsigned long count
= 0;
754 const bitmap_element
*elt
;
757 if (bitmap_empty_p (a
))
761 /* As there are no completely empty bitmap elements, a second one
762 means we have more than one bit set. */
763 if (elt
->next
!= NULL
)
766 for (ix
= 0; ix
!= BITMAP_ELEMENT_WORDS
; ix
++)
768 #if GCC_VERSION >= 3400
769 /* Note that popcountl matches BITMAP_WORD in type, so the actual size
770 of BITMAP_WORD is not material. */
771 count
+= __builtin_popcountl (elt
->bits
[ix
]);
773 count
+= bitmap_popcount (elt
->bits
[ix
]);
783 /* Return the bit number of the first set bit in the bitmap. The
784 bitmap must be non-empty. */
787 bitmap_first_set_bit (const_bitmap a
)
789 const bitmap_element
*elt
= a
->first
;
794 gcc_checking_assert (elt
);
795 bit_no
= elt
->indx
* BITMAP_ELEMENT_ALL_BITS
;
796 for (ix
= 0; ix
!= BITMAP_ELEMENT_WORDS
; ix
++)
798 word
= elt
->bits
[ix
];
804 bit_no
+= ix
* BITMAP_WORD_BITS
;
806 #if GCC_VERSION >= 3004
807 gcc_assert (sizeof(long) == sizeof (word
));
808 bit_no
+= __builtin_ctzl (word
);
810 /* Binary search for the first set bit. */
811 #if BITMAP_WORD_BITS > 64
812 #error "Fill out the table."
814 #if BITMAP_WORD_BITS > 32
815 if (!(word
& 0xffffffff))
816 word
>>= 32, bit_no
+= 32;
818 if (!(word
& 0xffff))
819 word
>>= 16, bit_no
+= 16;
821 word
>>= 8, bit_no
+= 8;
823 word
>>= 4, bit_no
+= 4;
825 word
>>= 2, bit_no
+= 2;
827 word
>>= 1, bit_no
+= 1;
829 gcc_checking_assert (word
& 1);
834 /* Return the bit number of the first set bit in the bitmap. The
835 bitmap must be non-empty. */
838 bitmap_last_set_bit (const_bitmap a
)
840 const bitmap_element
*elt
= a
->current
? a
->current
: a
->first
;
845 gcc_checking_assert (elt
);
848 bit_no
= elt
->indx
* BITMAP_ELEMENT_ALL_BITS
;
849 for (ix
= BITMAP_ELEMENT_WORDS
- 1; ix
>= 0; ix
--)
851 word
= elt
->bits
[ix
];
857 bit_no
+= ix
* BITMAP_WORD_BITS
;
858 #if GCC_VERSION >= 3004
859 gcc_assert (sizeof(long) == sizeof (word
));
860 bit_no
+= BITMAP_WORD_BITS
- __builtin_clzl (word
) - 1;
862 /* Hopefully this is a twos-complement host... */
863 BITMAP_WORD x
= word
;
869 #if BITMAP_WORD_BITS > 32
872 bit_no
+= bitmap_popcount (x
) - 1;
882 bitmap_and (bitmap dst
, const_bitmap a
, const_bitmap b
)
884 bitmap_element
*dst_elt
= dst
->first
;
885 const bitmap_element
*a_elt
= a
->first
;
886 const bitmap_element
*b_elt
= b
->first
;
887 bitmap_element
*dst_prev
= NULL
;
889 gcc_assert (dst
!= a
&& dst
!= b
);
893 bitmap_copy (dst
, a
);
897 while (a_elt
&& b_elt
)
899 if (a_elt
->indx
< b_elt
->indx
)
901 else if (b_elt
->indx
< a_elt
->indx
)
905 /* Matching elts, generate A & B. */
910 dst_elt
= bitmap_elt_insert_after (dst
, dst_prev
, a_elt
->indx
);
912 dst_elt
->indx
= a_elt
->indx
;
913 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
915 BITMAP_WORD r
= a_elt
->bits
[ix
] & b_elt
->bits
[ix
];
917 dst_elt
->bits
[ix
] = r
;
923 dst_elt
= dst_elt
->next
;
929 /* Ensure that dst->current is valid. */
930 dst
->current
= dst
->first
;
931 bitmap_elt_clear_from (dst
, dst_elt
);
932 gcc_checking_assert (!dst
->current
== !dst
->first
);
934 dst
->indx
= dst
->current
->indx
;
937 /* A &= B. Return true if A changed. */
940 bitmap_and_into (bitmap a
, const_bitmap b
)
942 bitmap_element
*a_elt
= a
->first
;
943 const bitmap_element
*b_elt
= b
->first
;
944 bitmap_element
*next
;
945 bool changed
= false;
950 while (a_elt
&& b_elt
)
952 if (a_elt
->indx
< b_elt
->indx
)
955 bitmap_element_free (a
, a_elt
);
959 else if (b_elt
->indx
< a_elt
->indx
)
963 /* Matching elts, generate A &= B. */
967 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
969 BITMAP_WORD r
= a_elt
->bits
[ix
] & b_elt
->bits
[ix
];
970 if (a_elt
->bits
[ix
] != r
)
977 bitmap_element_free (a
, a_elt
);
986 bitmap_elt_clear_from (a
, a_elt
);
989 gcc_checking_assert (!a
->current
== !a
->first
990 && (!a
->current
|| a
->indx
== a
->current
->indx
));
996 /* Insert an element equal to SRC_ELT after DST_PREV, overwriting DST_ELT
997 if non-NULL. CHANGED is true if the destination bitmap had already been
998 changed; the new value of CHANGED is returned. */
1001 bitmap_elt_copy (bitmap dst
, bitmap_element
*dst_elt
, bitmap_element
*dst_prev
,
1002 const bitmap_element
*src_elt
, bool changed
)
1004 if (!changed
&& dst_elt
&& dst_elt
->indx
== src_elt
->indx
)
1008 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1009 if (src_elt
->bits
[ix
] != dst_elt
->bits
[ix
])
1011 dst_elt
->bits
[ix
] = src_elt
->bits
[ix
];
1019 dst_elt
= bitmap_elt_insert_after (dst
, dst_prev
, src_elt
->indx
);
1021 dst_elt
->indx
= src_elt
->indx
;
1022 memcpy (dst_elt
->bits
, src_elt
->bits
, sizeof (dst_elt
->bits
));
1032 bitmap_and_compl (bitmap dst
, const_bitmap a
, const_bitmap b
)
1034 bitmap_element
*dst_elt
= dst
->first
;
1035 const bitmap_element
*a_elt
= a
->first
;
1036 const bitmap_element
*b_elt
= b
->first
;
1037 bitmap_element
*dst_prev
= NULL
;
1038 bitmap_element
**dst_prev_pnext
= &dst
->first
;
1039 bool changed
= false;
1041 gcc_assert (dst
!= a
&& dst
!= b
);
1045 changed
= !bitmap_empty_p (dst
);
1052 while (b_elt
&& b_elt
->indx
< a_elt
->indx
)
1053 b_elt
= b_elt
->next
;
1055 if (!b_elt
|| b_elt
->indx
> a_elt
->indx
)
1057 changed
= bitmap_elt_copy (dst
, dst_elt
, dst_prev
, a_elt
, changed
);
1058 dst_prev
= *dst_prev_pnext
;
1059 dst_prev_pnext
= &dst_prev
->next
;
1060 dst_elt
= *dst_prev_pnext
;
1061 a_elt
= a_elt
->next
;
1066 /* Matching elts, generate A & ~B. */
1068 BITMAP_WORD ior
= 0;
1070 if (!changed
&& dst_elt
&& dst_elt
->indx
== a_elt
->indx
)
1072 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1074 BITMAP_WORD r
= a_elt
->bits
[ix
] & ~b_elt
->bits
[ix
];
1076 if (dst_elt
->bits
[ix
] != r
)
1079 dst_elt
->bits
[ix
] = r
;
1087 if (!dst_elt
|| dst_elt
->indx
> a_elt
->indx
)
1089 dst_elt
= bitmap_elt_insert_after (dst
, dst_prev
, a_elt
->indx
);
1094 dst_elt
->indx
= a_elt
->indx
;
1095 new_element
= false;
1098 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1100 BITMAP_WORD r
= a_elt
->bits
[ix
] & ~b_elt
->bits
[ix
];
1102 dst_elt
->bits
[ix
] = r
;
1110 changed
|= !new_element
;
1111 bitmap_element_free (dst
, dst_elt
);
1112 dst_elt
= *dst_prev_pnext
;
1118 dst_prev
= *dst_prev_pnext
;
1119 dst_prev_pnext
= &dst_prev
->next
;
1120 dst_elt
= *dst_prev_pnext
;
1122 a_elt
= a_elt
->next
;
1123 b_elt
= b_elt
->next
;
1127 /* Ensure that dst->current is valid. */
1128 dst
->current
= dst
->first
;
1133 bitmap_elt_clear_from (dst
, dst_elt
);
1135 gcc_checking_assert (!dst
->current
== !dst
->first
);
1137 dst
->indx
= dst
->current
->indx
;
1142 /* A &= ~B. Returns true if A changes */
1145 bitmap_and_compl_into (bitmap a
, const_bitmap b
)
1147 bitmap_element
*a_elt
= a
->first
;
1148 const bitmap_element
*b_elt
= b
->first
;
1149 bitmap_element
*next
;
1150 BITMAP_WORD changed
= 0;
1154 if (bitmap_empty_p (a
))
1163 while (a_elt
&& b_elt
)
1165 if (a_elt
->indx
< b_elt
->indx
)
1166 a_elt
= a_elt
->next
;
1167 else if (b_elt
->indx
< a_elt
->indx
)
1168 b_elt
= b_elt
->next
;
1171 /* Matching elts, generate A &= ~B. */
1173 BITMAP_WORD ior
= 0;
1175 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1177 BITMAP_WORD cleared
= a_elt
->bits
[ix
] & b_elt
->bits
[ix
];
1178 BITMAP_WORD r
= a_elt
->bits
[ix
] ^ cleared
;
1180 a_elt
->bits
[ix
] = r
;
1186 bitmap_element_free (a
, a_elt
);
1188 b_elt
= b_elt
->next
;
1191 gcc_checking_assert (!a
->current
== !a
->first
1192 && (!a
->current
|| a
->indx
== a
->current
->indx
));
1193 return changed
!= 0;
1196 /* Set COUNT bits from START in HEAD. */
1198 bitmap_set_range (bitmap head
, unsigned int start
, unsigned int count
)
1200 unsigned int first_index
, end_bit_plus1
, last_index
;
1201 bitmap_element
*elt
, *elt_prev
;
1207 first_index
= start
/ BITMAP_ELEMENT_ALL_BITS
;
1208 end_bit_plus1
= start
+ count
;
1209 last_index
= (end_bit_plus1
- 1) / BITMAP_ELEMENT_ALL_BITS
;
1210 elt
= bitmap_find_bit (head
, start
);
1212 /* If bitmap_find_bit returns zero, the current is the closest block
1213 to the result. Otherwise, just use bitmap_element_allocate to
1214 ensure ELT is set; in the loop below, ELT == NULL means "insert
1215 at the end of the bitmap". */
1218 elt
= bitmap_element_allocate (head
);
1219 elt
->indx
= first_index
;
1220 bitmap_element_link (head
, elt
);
1223 gcc_checking_assert (elt
->indx
== first_index
);
1224 elt_prev
= elt
->prev
;
1225 for (i
= first_index
; i
<= last_index
; i
++)
1227 unsigned elt_start_bit
= i
* BITMAP_ELEMENT_ALL_BITS
;
1228 unsigned elt_end_bit_plus1
= elt_start_bit
+ BITMAP_ELEMENT_ALL_BITS
;
1230 unsigned int first_word_to_mod
;
1231 BITMAP_WORD first_mask
;
1232 unsigned int last_word_to_mod
;
1233 BITMAP_WORD last_mask
;
1236 if (!elt
|| elt
->indx
!= i
)
1237 elt
= bitmap_elt_insert_after (head
, elt_prev
, i
);
1239 if (elt_start_bit
<= start
)
1241 /* The first bit to turn on is somewhere inside this
1243 first_word_to_mod
= (start
- elt_start_bit
) / BITMAP_WORD_BITS
;
1245 /* This mask should have 1s in all bits >= start position. */
1247 (((BITMAP_WORD
) 1) << ((start
% BITMAP_WORD_BITS
))) - 1;
1248 first_mask
= ~first_mask
;
1252 /* The first bit to turn on is below this start of this elt. */
1253 first_word_to_mod
= 0;
1254 first_mask
= ~(BITMAP_WORD
) 0;
1257 if (elt_end_bit_plus1
<= end_bit_plus1
)
1259 /* The last bit to turn on is beyond this elt. */
1260 last_word_to_mod
= BITMAP_ELEMENT_WORDS
- 1;
1261 last_mask
= ~(BITMAP_WORD
) 0;
1265 /* The last bit to turn on is inside to this elt. */
1267 (end_bit_plus1
- elt_start_bit
) / BITMAP_WORD_BITS
;
1269 /* The last mask should have 1s below the end bit. */
1271 (((BITMAP_WORD
) 1) << ((end_bit_plus1
% BITMAP_WORD_BITS
))) - 1;
1274 if (first_word_to_mod
== last_word_to_mod
)
1276 BITMAP_WORD mask
= first_mask
& last_mask
;
1277 elt
->bits
[first_word_to_mod
] |= mask
;
1281 elt
->bits
[first_word_to_mod
] |= first_mask
;
1282 if (BITMAP_ELEMENT_WORDS
> 2)
1283 for (ix
= first_word_to_mod
+ 1; ix
< last_word_to_mod
; ix
++)
1284 elt
->bits
[ix
] = ~(BITMAP_WORD
) 0;
1285 elt
->bits
[last_word_to_mod
] |= last_mask
;
1292 head
->current
= elt
? elt
: elt_prev
;
1293 head
->indx
= head
->current
->indx
;
1296 /* Clear COUNT bits from START in HEAD. */
1298 bitmap_clear_range (bitmap head
, unsigned int start
, unsigned int count
)
1300 unsigned int first_index
, end_bit_plus1
, last_index
;
1301 bitmap_element
*elt
;
1306 first_index
= start
/ BITMAP_ELEMENT_ALL_BITS
;
1307 end_bit_plus1
= start
+ count
;
1308 last_index
= (end_bit_plus1
- 1) / BITMAP_ELEMENT_ALL_BITS
;
1309 elt
= bitmap_find_bit (head
, start
);
1311 /* If bitmap_find_bit returns zero, the current is the closest block
1312 to the result. If the current is less than first index, find the
1313 next one. Otherwise, just set elt to be current. */
1318 if (head
->indx
< first_index
)
1320 elt
= head
->current
->next
;
1325 elt
= head
->current
;
1331 while (elt
&& (elt
->indx
<= last_index
))
1333 bitmap_element
* next_elt
= elt
->next
;
1334 unsigned elt_start_bit
= (elt
->indx
) * BITMAP_ELEMENT_ALL_BITS
;
1335 unsigned elt_end_bit_plus1
= elt_start_bit
+ BITMAP_ELEMENT_ALL_BITS
;
1338 if (elt_start_bit
>= start
&& elt_end_bit_plus1
<= end_bit_plus1
)
1339 /* Get rid of the entire elt and go to the next one. */
1340 bitmap_element_free (head
, elt
);
1343 /* Going to have to knock out some bits in this elt. */
1344 unsigned int first_word_to_mod
;
1345 BITMAP_WORD first_mask
;
1346 unsigned int last_word_to_mod
;
1347 BITMAP_WORD last_mask
;
1351 if (elt_start_bit
<= start
)
1353 /* The first bit to turn off is somewhere inside this
1355 first_word_to_mod
= (start
- elt_start_bit
) / BITMAP_WORD_BITS
;
1357 /* This mask should have 1s in all bits >= start position. */
1359 (((BITMAP_WORD
) 1) << ((start
% BITMAP_WORD_BITS
))) - 1;
1360 first_mask
= ~first_mask
;
1364 /* The first bit to turn off is below this start of this elt. */
1365 first_word_to_mod
= 0;
1367 first_mask
= ~first_mask
;
1370 if (elt_end_bit_plus1
<= end_bit_plus1
)
1372 /* The last bit to turn off is beyond this elt. */
1373 last_word_to_mod
= BITMAP_ELEMENT_WORDS
- 1;
1375 last_mask
= ~last_mask
;
1379 /* The last bit to turn off is inside to this elt. */
1381 (end_bit_plus1
- elt_start_bit
) / BITMAP_WORD_BITS
;
1383 /* The last mask should have 1s below the end bit. */
1385 (((BITMAP_WORD
) 1) << (((end_bit_plus1
) % BITMAP_WORD_BITS
))) - 1;
1389 if (first_word_to_mod
== last_word_to_mod
)
1391 BITMAP_WORD mask
= first_mask
& last_mask
;
1392 elt
->bits
[first_word_to_mod
] &= ~mask
;
1396 elt
->bits
[first_word_to_mod
] &= ~first_mask
;
1397 if (BITMAP_ELEMENT_WORDS
> 2)
1398 for (i
= first_word_to_mod
+ 1; i
< last_word_to_mod
; i
++)
1400 elt
->bits
[last_word_to_mod
] &= ~last_mask
;
1402 for (i
= 0; i
< BITMAP_ELEMENT_WORDS
; i
++)
1408 /* Check to see if there are any bits left. */
1410 bitmap_element_free (head
, elt
);
1417 head
->current
= elt
;
1418 head
->indx
= head
->current
->indx
;
1425 bitmap_compl_and_into (bitmap a
, const_bitmap b
)
1427 bitmap_element
*a_elt
= a
->first
;
1428 const bitmap_element
*b_elt
= b
->first
;
1429 bitmap_element
*a_prev
= NULL
;
1430 bitmap_element
*next
;
1432 gcc_assert (a
!= b
);
1434 if (bitmap_empty_p (a
))
1439 if (bitmap_empty_p (b
))
1445 while (a_elt
|| b_elt
)
1447 if (!b_elt
|| (a_elt
&& a_elt
->indx
< b_elt
->indx
))
1449 /* A is before B. Remove A */
1451 a_prev
= a_elt
->prev
;
1452 bitmap_element_free (a
, a_elt
);
1455 else if (!a_elt
|| b_elt
->indx
< a_elt
->indx
)
1457 /* B is before A. Copy B. */
1458 next
= bitmap_elt_insert_after (a
, a_prev
, b_elt
->indx
);
1459 memcpy (next
->bits
, b_elt
->bits
, sizeof (next
->bits
));
1461 b_elt
= b_elt
->next
;
1465 /* Matching elts, generate A = ~A & B. */
1467 BITMAP_WORD ior
= 0;
1469 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1471 BITMAP_WORD cleared
= a_elt
->bits
[ix
] & b_elt
->bits
[ix
];
1472 BITMAP_WORD r
= b_elt
->bits
[ix
] ^ cleared
;
1474 a_elt
->bits
[ix
] = r
;
1479 bitmap_element_free (a
, a_elt
);
1483 b_elt
= b_elt
->next
;
1486 gcc_checking_assert (!a
->current
== !a
->first
1487 && (!a
->current
|| a
->indx
== a
->current
->indx
));
1492 /* Insert an element corresponding to A_ELT | B_ELT after DST_PREV,
1493 overwriting DST_ELT if non-NULL. CHANGED is true if the destination bitmap
1494 had already been changed; the new value of CHANGED is returned. */
1497 bitmap_elt_ior (bitmap dst
, bitmap_element
*dst_elt
, bitmap_element
*dst_prev
,
1498 const bitmap_element
*a_elt
, const bitmap_element
*b_elt
,
1501 gcc_assert (a_elt
|| b_elt
);
1503 if (a_elt
&& b_elt
&& a_elt
->indx
== b_elt
->indx
)
1505 /* Matching elts, generate A | B. */
1508 if (!changed
&& dst_elt
&& dst_elt
->indx
== a_elt
->indx
)
1510 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1512 BITMAP_WORD r
= a_elt
->bits
[ix
] | b_elt
->bits
[ix
];
1513 if (r
!= dst_elt
->bits
[ix
])
1515 dst_elt
->bits
[ix
] = r
;
1524 dst_elt
= bitmap_elt_insert_after (dst
, dst_prev
, a_elt
->indx
);
1526 dst_elt
->indx
= a_elt
->indx
;
1527 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1529 BITMAP_WORD r
= a_elt
->bits
[ix
] | b_elt
->bits
[ix
];
1530 dst_elt
->bits
[ix
] = r
;
1536 /* Copy a single element. */
1537 const bitmap_element
*src
;
1539 if (!b_elt
|| (a_elt
&& a_elt
->indx
< b_elt
->indx
))
1544 gcc_checking_assert (src
);
1545 changed
= bitmap_elt_copy (dst
, dst_elt
, dst_prev
, src
, changed
);
1551 /* DST = A | B. Return true if DST changes. */
1554 bitmap_ior (bitmap dst
, const_bitmap a
, const_bitmap b
)
1556 bitmap_element
*dst_elt
= dst
->first
;
1557 const bitmap_element
*a_elt
= a
->first
;
1558 const bitmap_element
*b_elt
= b
->first
;
1559 bitmap_element
*dst_prev
= NULL
;
1560 bitmap_element
**dst_prev_pnext
= &dst
->first
;
1561 bool changed
= false;
1563 gcc_assert (dst
!= a
&& dst
!= b
);
1565 while (a_elt
|| b_elt
)
1567 changed
= bitmap_elt_ior (dst
, dst_elt
, dst_prev
, a_elt
, b_elt
, changed
);
1569 if (a_elt
&& b_elt
&& a_elt
->indx
== b_elt
->indx
)
1571 a_elt
= a_elt
->next
;
1572 b_elt
= b_elt
->next
;
1576 if (a_elt
&& (!b_elt
|| a_elt
->indx
<= b_elt
->indx
))
1577 a_elt
= a_elt
->next
;
1578 else if (b_elt
&& (!a_elt
|| b_elt
->indx
<= a_elt
->indx
))
1579 b_elt
= b_elt
->next
;
1582 dst_prev
= *dst_prev_pnext
;
1583 dst_prev_pnext
= &dst_prev
->next
;
1584 dst_elt
= *dst_prev_pnext
;
1590 bitmap_elt_clear_from (dst
, dst_elt
);
1592 gcc_checking_assert (!dst
->current
== !dst
->first
);
1594 dst
->indx
= dst
->current
->indx
;
1598 /* A |= B. Return true if A changes. */
1601 bitmap_ior_into (bitmap a
, const_bitmap b
)
1603 bitmap_element
*a_elt
= a
->first
;
1604 const bitmap_element
*b_elt
= b
->first
;
1605 bitmap_element
*a_prev
= NULL
;
1606 bitmap_element
**a_prev_pnext
= &a
->first
;
1607 bool changed
= false;
1614 /* If A lags behind B, just advance it. */
1615 if (!a_elt
|| a_elt
->indx
== b_elt
->indx
)
1617 changed
= bitmap_elt_ior (a
, a_elt
, a_prev
, a_elt
, b_elt
, changed
);
1618 b_elt
= b_elt
->next
;
1620 else if (a_elt
->indx
> b_elt
->indx
)
1622 changed
= bitmap_elt_copy (a
, NULL
, a_prev
, b_elt
, changed
);
1623 b_elt
= b_elt
->next
;
1626 a_prev
= *a_prev_pnext
;
1627 a_prev_pnext
= &a_prev
->next
;
1628 a_elt
= *a_prev_pnext
;
1631 gcc_checking_assert (!a
->current
== !a
->first
);
1633 a
->indx
= a
->current
->indx
;
1640 bitmap_xor (bitmap dst
, const_bitmap a
, const_bitmap b
)
1642 bitmap_element
*dst_elt
= dst
->first
;
1643 const bitmap_element
*a_elt
= a
->first
;
1644 const bitmap_element
*b_elt
= b
->first
;
1645 bitmap_element
*dst_prev
= NULL
;
1647 gcc_assert (dst
!= a
&& dst
!= b
);
1654 while (a_elt
|| b_elt
)
1656 if (a_elt
&& b_elt
&& a_elt
->indx
== b_elt
->indx
)
1658 /* Matching elts, generate A ^ B. */
1660 BITMAP_WORD ior
= 0;
1663 dst_elt
= bitmap_elt_insert_after (dst
, dst_prev
, a_elt
->indx
);
1665 dst_elt
->indx
= a_elt
->indx
;
1666 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1668 BITMAP_WORD r
= a_elt
->bits
[ix
] ^ b_elt
->bits
[ix
];
1671 dst_elt
->bits
[ix
] = r
;
1673 a_elt
= a_elt
->next
;
1674 b_elt
= b_elt
->next
;
1678 dst_elt
= dst_elt
->next
;
1683 /* Copy a single element. */
1684 const bitmap_element
*src
;
1686 if (!b_elt
|| (a_elt
&& a_elt
->indx
< b_elt
->indx
))
1689 a_elt
= a_elt
->next
;
1694 b_elt
= b_elt
->next
;
1698 dst_elt
= bitmap_elt_insert_after (dst
, dst_prev
, src
->indx
);
1700 dst_elt
->indx
= src
->indx
;
1701 memcpy (dst_elt
->bits
, src
->bits
, sizeof (dst_elt
->bits
));
1703 dst_elt
= dst_elt
->next
;
1706 /* Ensure that dst->current is valid. */
1707 dst
->current
= dst
->first
;
1708 bitmap_elt_clear_from (dst
, dst_elt
);
1709 gcc_checking_assert (!dst
->current
== !dst
->first
);
1711 dst
->indx
= dst
->current
->indx
;
1717 bitmap_xor_into (bitmap a
, const_bitmap b
)
1719 bitmap_element
*a_elt
= a
->first
;
1720 const bitmap_element
*b_elt
= b
->first
;
1721 bitmap_element
*a_prev
= NULL
;
1731 if (!a_elt
|| b_elt
->indx
< a_elt
->indx
)
1734 bitmap_element
*dst
= bitmap_elt_insert_after (a
, a_prev
, b_elt
->indx
);
1735 memcpy (dst
->bits
, b_elt
->bits
, sizeof (dst
->bits
));
1737 b_elt
= b_elt
->next
;
1739 else if (a_elt
->indx
< b_elt
->indx
)
1742 a_elt
= a_elt
->next
;
1746 /* Matching elts, generate A ^= B. */
1748 BITMAP_WORD ior
= 0;
1749 bitmap_element
*next
= a_elt
->next
;
1751 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1753 BITMAP_WORD r
= a_elt
->bits
[ix
] ^ b_elt
->bits
[ix
];
1756 a_elt
->bits
[ix
] = r
;
1758 b_elt
= b_elt
->next
;
1762 bitmap_element_free (a
, a_elt
);
1766 gcc_checking_assert (!a
->current
== !a
->first
);
1768 a
->indx
= a
->current
->indx
;
1771 /* Return true if two bitmaps are identical.
1772 We do not bother with a check for pointer equality, as that never
1773 occurs in practice. */
1776 bitmap_equal_p (const_bitmap a
, const_bitmap b
)
1778 const bitmap_element
*a_elt
;
1779 const bitmap_element
*b_elt
;
1782 for (a_elt
= a
->first
, b_elt
= b
->first
;
1784 a_elt
= a_elt
->next
, b_elt
= b_elt
->next
)
1786 if (a_elt
->indx
!= b_elt
->indx
)
1788 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1789 if (a_elt
->bits
[ix
] != b_elt
->bits
[ix
])
1792 return !a_elt
&& !b_elt
;
1795 /* Return true if A AND B is not empty. */
1798 bitmap_intersect_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
;
1807 if (a_elt
->indx
< b_elt
->indx
)
1808 a_elt
= a_elt
->next
;
1809 else if (b_elt
->indx
< a_elt
->indx
)
1810 b_elt
= b_elt
->next
;
1813 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1814 if (a_elt
->bits
[ix
] & b_elt
->bits
[ix
])
1816 a_elt
= a_elt
->next
;
1817 b_elt
= b_elt
->next
;
1823 /* Return true if A AND NOT B is not empty. */
1826 bitmap_intersect_compl_p (const_bitmap a
, const_bitmap b
)
1828 const bitmap_element
*a_elt
;
1829 const bitmap_element
*b_elt
;
1831 for (a_elt
= a
->first
, b_elt
= b
->first
;
1834 if (a_elt
->indx
< b_elt
->indx
)
1836 else if (b_elt
->indx
< a_elt
->indx
)
1837 b_elt
= b_elt
->next
;
1840 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1841 if (a_elt
->bits
[ix
] & ~b_elt
->bits
[ix
])
1843 a_elt
= a_elt
->next
;
1844 b_elt
= b_elt
->next
;
1847 return a_elt
!= NULL
;
1851 /* DST = A | (FROM1 & ~FROM2). Return true if DST changes. */
1854 bitmap_ior_and_compl (bitmap dst
, const_bitmap a
, const_bitmap b
, const_bitmap kill
)
1856 bool changed
= false;
1858 bitmap_element
*dst_elt
= dst
->first
;
1859 const bitmap_element
*a_elt
= a
->first
;
1860 const bitmap_element
*b_elt
= b
->first
;
1861 const bitmap_element
*kill_elt
= kill
->first
;
1862 bitmap_element
*dst_prev
= NULL
;
1863 bitmap_element
**dst_prev_pnext
= &dst
->first
;
1865 gcc_assert (dst
!= a
&& dst
!= b
&& dst
!= kill
);
1867 /* Special cases. We don't bother checking for bitmap_equal_p (b, kill). */
1868 if (b
== kill
|| bitmap_empty_p (b
))
1870 changed
= !bitmap_equal_p (dst
, a
);
1872 bitmap_copy (dst
, a
);
1875 if (bitmap_empty_p (kill
))
1876 return bitmap_ior (dst
, a
, b
);
1877 if (bitmap_empty_p (a
))
1878 return bitmap_and_compl (dst
, b
, kill
);
1880 while (a_elt
|| b_elt
)
1882 bool new_element
= false;
1885 while (kill_elt
&& kill_elt
->indx
< b_elt
->indx
)
1886 kill_elt
= kill_elt
->next
;
1888 if (b_elt
&& kill_elt
&& kill_elt
->indx
== b_elt
->indx
1889 && (!a_elt
|| a_elt
->indx
>= b_elt
->indx
))
1891 bitmap_element tmp_elt
;
1894 BITMAP_WORD ior
= 0;
1895 tmp_elt
.indx
= b_elt
->indx
;
1896 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
1898 BITMAP_WORD r
= b_elt
->bits
[ix
] & ~kill_elt
->bits
[ix
];
1900 tmp_elt
.bits
[ix
] = r
;
1905 changed
= bitmap_elt_ior (dst
, dst_elt
, dst_prev
,
1906 a_elt
, &tmp_elt
, changed
);
1908 if (a_elt
&& a_elt
->indx
== b_elt
->indx
)
1909 a_elt
= a_elt
->next
;
1912 b_elt
= b_elt
->next
;
1913 kill_elt
= kill_elt
->next
;
1917 changed
= bitmap_elt_ior (dst
, dst_elt
, dst_prev
,
1918 a_elt
, b_elt
, changed
);
1921 if (a_elt
&& b_elt
&& a_elt
->indx
== b_elt
->indx
)
1923 a_elt
= a_elt
->next
;
1924 b_elt
= b_elt
->next
;
1928 if (a_elt
&& (!b_elt
|| a_elt
->indx
<= b_elt
->indx
))
1929 a_elt
= a_elt
->next
;
1930 else if (b_elt
&& (!a_elt
|| b_elt
->indx
<= a_elt
->indx
))
1931 b_elt
= b_elt
->next
;
1937 dst_prev
= *dst_prev_pnext
;
1938 dst_prev_pnext
= &dst_prev
->next
;
1939 dst_elt
= *dst_prev_pnext
;
1946 bitmap_elt_clear_from (dst
, dst_elt
);
1948 gcc_checking_assert (!dst
->current
== !dst
->first
);
1950 dst
->indx
= dst
->current
->indx
;
1955 /* A |= (FROM1 & ~FROM2). Return true if A changes. */
1958 bitmap_ior_and_compl_into (bitmap a
, const_bitmap from1
, const_bitmap from2
)
1963 bitmap_initialize (&tmp
, &bitmap_default_obstack
);
1964 bitmap_and_compl (&tmp
, from1
, from2
);
1965 changed
= bitmap_ior_into (a
, &tmp
);
1966 bitmap_clear (&tmp
);
1971 /* A |= (B & C). Return true if A changes. */
1974 bitmap_ior_and_into (bitmap a
, const_bitmap b
, const_bitmap c
)
1976 bitmap_element
*a_elt
= a
->first
;
1977 const bitmap_element
*b_elt
= b
->first
;
1978 const bitmap_element
*c_elt
= c
->first
;
1979 bitmap_element and_elt
;
1980 bitmap_element
*a_prev
= NULL
;
1981 bitmap_element
**a_prev_pnext
= &a
->first
;
1982 bool changed
= false;
1986 return bitmap_ior_into (a
, b
);
1987 if (bitmap_empty_p (b
) || bitmap_empty_p (c
))
1991 while (b_elt
&& c_elt
)
1993 BITMAP_WORD overall
;
1995 /* Find a common item of B and C. */
1996 while (b_elt
->indx
!= c_elt
->indx
)
1998 if (b_elt
->indx
< c_elt
->indx
)
2000 b_elt
= b_elt
->next
;
2006 c_elt
= c_elt
->next
;
2013 and_elt
.indx
= b_elt
->indx
;
2014 for (ix
= 0; ix
< BITMAP_ELEMENT_WORDS
; ix
++)
2016 and_elt
.bits
[ix
] = b_elt
->bits
[ix
] & c_elt
->bits
[ix
];
2017 overall
|= and_elt
.bits
[ix
];
2020 b_elt
= b_elt
->next
;
2021 c_elt
= c_elt
->next
;
2025 /* Now find a place to insert AND_ELT. */
2028 ix
= a_elt
? a_elt
->indx
: and_elt
.indx
;
2029 if (ix
== and_elt
.indx
)
2030 changed
= bitmap_elt_ior (a
, a_elt
, a_prev
, a_elt
, &and_elt
, changed
);
2031 else if (ix
> and_elt
.indx
)
2032 changed
= bitmap_elt_copy (a
, NULL
, a_prev
, &and_elt
, changed
);
2034 a_prev
= *a_prev_pnext
;
2035 a_prev_pnext
= &a_prev
->next
;
2036 a_elt
= *a_prev_pnext
;
2038 /* If A lagged behind B/C, we advanced it so loop once more. */
2040 while (ix
< and_elt
.indx
);
2044 gcc_checking_assert (!a
->current
== !a
->first
);
2046 a
->indx
= a
->current
->indx
;
2050 /* Compute hash of bitmap (for purposes of hashing). */
2052 bitmap_hash (const_bitmap head
)
2054 const bitmap_element
*ptr
;
2055 BITMAP_WORD hash
= 0;
2058 for (ptr
= head
->first
; ptr
; ptr
= ptr
->next
)
2061 for (ix
= 0; ix
!= BITMAP_ELEMENT_WORDS
; ix
++)
2062 hash
^= ptr
->bits
[ix
];
2064 return (hashval_t
)hash
;
2068 /* Debugging function to print out the contents of a bitmap. */
2071 debug_bitmap_file (FILE *file
, const_bitmap head
)
2073 const bitmap_element
*ptr
;
2075 fprintf (file
, "\nfirst = " HOST_PTR_PRINTF
2076 " current = " HOST_PTR_PRINTF
" indx = %u\n",
2077 (void *) head
->first
, (void *) head
->current
, head
->indx
);
2079 for (ptr
= head
->first
; ptr
; ptr
= ptr
->next
)
2081 unsigned int i
, j
, col
= 26;
2083 fprintf (file
, "\t" HOST_PTR_PRINTF
" next = " HOST_PTR_PRINTF
2084 " prev = " HOST_PTR_PRINTF
" indx = %u\n\t\tbits = {",
2085 (const void*) ptr
, (const void*) ptr
->next
,
2086 (const void*) ptr
->prev
, ptr
->indx
);
2088 for (i
= 0; i
< BITMAP_ELEMENT_WORDS
; i
++)
2089 for (j
= 0; j
< BITMAP_WORD_BITS
; j
++)
2090 if ((ptr
->bits
[i
] >> j
) & 1)
2094 fprintf (file
, "\n\t\t\t");
2098 fprintf (file
, " %u", (ptr
->indx
* BITMAP_ELEMENT_ALL_BITS
2099 + i
* BITMAP_WORD_BITS
+ j
));
2103 fprintf (file
, " }\n");
2107 /* Function to be called from the debugger to print the contents
2111 debug_bitmap (const_bitmap head
)
2113 debug_bitmap_file (stdout
, head
);
2116 /* Function to print out the contents of a bitmap. Unlike debug_bitmap_file,
2117 it does not print anything but the bits. */
2120 bitmap_print (FILE *file
, const_bitmap head
, const char *prefix
,
2123 const char *comma
= "";
2127 fputs (prefix
, file
);
2128 EXECUTE_IF_SET_IN_BITMAP (head
, 0, i
, bi
)
2130 fprintf (file
, "%s%d", comma
, i
);
2133 fputs (suffix
, file
);
2137 /* Used to accumulate statistics about bitmap sizes. */
2140 unsigned HOST_WIDEST_INT size
;
2141 unsigned HOST_WIDEST_INT count
;
2144 /* Called via htab_traverse. Output bitmap descriptor pointed out by SLOT
2145 and update statistics. */
2147 print_statistics (void **slot
, void *b
)
2149 bitmap_descriptor d
= (bitmap_descriptor
) *slot
;
2150 struct output_info
*i
= (struct output_info
*) b
;
2155 const char *s1
= d
->file
;
2157 while ((s2
= strstr (s1
, "gcc/")))
2159 sprintf (s
, "%s:%i (%s)", s1
, d
->line
, d
->function
);
2163 " %15"HOST_WIDEST_INT_PRINT
"d %15"HOST_WIDEST_INT_PRINT
"d"
2164 " %15"HOST_WIDEST_INT_PRINT
"d"
2165 " %10"HOST_WIDEST_INT_PRINT
"d %10"HOST_WIDEST_INT_PRINT
"d\n",
2167 d
->allocated
, d
->peak
, d
->current
,
2168 d
->nsearches
, d
->search_iter
);
2169 i
->size
+= d
->allocated
;
2170 i
->count
+= d
->created
;
2175 /* Output per-bitmap memory usage statistics. */
2177 dump_bitmap_statistics (void)
2179 struct output_info info
;
2181 if (! GATHER_STATISTICS
)
2184 if (!bitmap_desc_hash
)
2188 "\n%-41s %9s %15s %15s %15s %10s %10s\n",
2189 "Bitmap", "Overall",
2190 "Allocated", "Peak", "Leak",
2191 "searched", "search_itr");
2192 fprintf (stderr
, "---------------------------------------------------------------------------------\n");
2195 htab_traverse (bitmap_desc_hash
, print_statistics
, &info
);
2196 fprintf (stderr
, "---------------------------------------------------------------------------------\n");
2198 "%-41s %9"HOST_WIDEST_INT_PRINT
"d %15"HOST_WIDEST_INT_PRINT
"d\n",
2199 "Total", info
.count
, info
.size
);
2200 fprintf (stderr
, "---------------------------------------------------------------------------------\n");
2204 debug (const bitmap_head_def
&ref
)
2206 dump_bitmap (stderr
, &ref
);
2210 debug (const bitmap_head_def
*ptr
)
2215 fprintf (stderr
, "<nil>\n");
2219 #include "gt-bitmap.h"