1 /* Functions to support general ended bitmaps.
2 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2003, 2004, 2005
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
24 #include "coretypes.h"
33 bitmap_element bitmap_zero_bits
; /* An element of all zero bits. */
34 bitmap_obstack bitmap_default_obstack
; /* The default bitmap obstack. */
35 static GTY((deletable
)) bitmap_element
*bitmap_ggc_free
; /* Freelist of
38 static void bitmap_elem_to_freelist (bitmap
, bitmap_element
*);
39 static void bitmap_element_free (bitmap
, bitmap_element
*);
40 static bitmap_element
*bitmap_element_allocate (bitmap
);
41 static int bitmap_element_zerop (bitmap_element
*);
42 static void bitmap_element_link (bitmap
, bitmap_element
*);
43 static bitmap_element
*bitmap_elt_insert_after (bitmap
, bitmap_element
*, unsigned int);
44 static void bitmap_elt_clear_from (bitmap
, bitmap_element
*);
45 static bitmap_element
*bitmap_find_bit (bitmap
, unsigned int);
48 /* Add ELEM to the appropriate freelist. */
50 bitmap_elem_to_freelist (bitmap head
, bitmap_element
*elt
)
52 bitmap_obstack
*bit_obstack
= head
->obstack
;
57 elt
->prev
= bit_obstack
->elements
;
58 bit_obstack
->elements
= elt
;
62 elt
->prev
= bitmap_ggc_free
;
63 bitmap_ggc_free
= elt
;
67 /* Free a bitmap element. Since these are allocated off the
68 bitmap_obstack, "free" actually means "put onto the freelist". */
71 bitmap_element_free (bitmap head
, bitmap_element
*elt
)
73 bitmap_element
*next
= elt
->next
;
74 bitmap_element
*prev
= elt
->prev
;
82 if (head
->first
== elt
)
85 /* Since the first thing we try is to insert before current,
86 make current the next entry in preference to the previous. */
87 if (head
->current
== elt
)
89 head
->current
= next
!= 0 ? next
: prev
;
91 head
->indx
= head
->current
->indx
;
95 bitmap_elem_to_freelist (head
, elt
);
98 /* Allocate a bitmap element. The bits are cleared, but nothing else is. */
100 static inline bitmap_element
*
101 bitmap_element_allocate (bitmap head
)
103 bitmap_element
*element
;
104 bitmap_obstack
*bit_obstack
= head
->obstack
;
108 element
= bit_obstack
->elements
;
111 /* Use up the inner list first before looking at the next
112 element of the outer list. */
115 bit_obstack
->elements
= element
->next
;
116 bit_obstack
->elements
->prev
= element
->prev
;
119 /* Inner list was just a singleton. */
120 bit_obstack
->elements
= element
->prev
;
122 element
= XOBNEW (&bit_obstack
->obstack
, bitmap_element
);
126 element
= bitmap_ggc_free
;
128 /* Use up the inner list first before looking at the next
129 element of the outer list. */
132 bitmap_ggc_free
= element
->next
;
133 bitmap_ggc_free
->prev
= element
->prev
;
136 /* Inner list was just a singleton. */
137 bitmap_ggc_free
= element
->prev
;
139 element
= GGC_NEW (bitmap_element
);
142 memset (element
->bits
, 0, sizeof (element
->bits
));
147 /* Remove ELT and all following elements from bitmap HEAD. */
150 bitmap_elt_clear_from (bitmap head
, bitmap_element
*elt
)
152 bitmap_element
*prev
;
153 bitmap_obstack
*bit_obstack
= head
->obstack
;
161 if (head
->current
->indx
> prev
->indx
)
163 head
->current
= prev
;
164 head
->indx
= prev
->indx
;
170 head
->current
= NULL
;
174 /* Put the entire list onto the free list in one operation. */
177 elt
->prev
= bit_obstack
->elements
;
178 bit_obstack
->elements
= elt
;
182 elt
->prev
= bitmap_ggc_free
;
183 bitmap_ggc_free
= elt
;
187 /* Clear a bitmap by freeing the linked list. */
190 bitmap_clear (bitmap head
)
193 bitmap_elt_clear_from (head
, head
->first
);
196 /* Initialize a bitmap obstack. If BIT_OBSTACK is NULL, initialize
197 the default bitmap obstack. */
200 bitmap_obstack_initialize (bitmap_obstack
*bit_obstack
)
203 bit_obstack
= &bitmap_default_obstack
;
205 #if !defined(__GNUC__) || (__GNUC__ < 2)
206 #define __alignof__(type) 0
209 bit_obstack
->elements
= NULL
;
210 bit_obstack
->heads
= NULL
;
211 obstack_specify_allocation (&bit_obstack
->obstack
, OBSTACK_CHUNK_SIZE
,
212 __alignof__ (bitmap_element
),
217 /* Release the memory from a bitmap obstack. If BIT_OBSTACK is NULL,
218 release the default bitmap obstack. */
221 bitmap_obstack_release (bitmap_obstack
*bit_obstack
)
224 bit_obstack
= &bitmap_default_obstack
;
226 bit_obstack
->elements
= NULL
;
227 bit_obstack
->heads
= NULL
;
228 obstack_free (&bit_obstack
->obstack
, NULL
);
231 /* Create a new bitmap on an obstack. If BIT_OBSTACK is NULL, create
232 it on the default bitmap obstack. */
235 bitmap_obstack_alloc (bitmap_obstack
*bit_obstack
)
240 bit_obstack
= &bitmap_default_obstack
;
241 map
= bit_obstack
->heads
;
243 bit_obstack
->heads
= (void *)map
->first
;
245 map
= XOBNEW (&bit_obstack
->obstack
, bitmap_head
);
246 bitmap_initialize (map
, bit_obstack
);
251 /* Create a new GCd bitmap. */
254 bitmap_gc_alloc (void)
258 map
= GGC_NEW (struct bitmap_head_def
);
259 bitmap_initialize (map
, NULL
);
264 /* Release an obstack allocated bitmap. */
267 bitmap_obstack_free (bitmap map
)
272 map
->first
= (void *)map
->obstack
->heads
;
273 map
->obstack
->heads
= map
;
278 /* Return nonzero if all bits in an element are zero. */
281 bitmap_element_zerop (bitmap_element
*element
)
283 #if BITMAP_ELEMENT_WORDS == 2
284 return (element
->bits
[0] | element
->bits
[1]) == 0;
288 for (i
= 0; i
< BITMAP_ELEMENT_WORDS
; i
++)
289 if (element
->bits
[i
] != 0)
296 /* Link the bitmap element into the current bitmap linked list. */
299 bitmap_element_link (bitmap head
, bitmap_element
*element
)
301 unsigned int indx
= element
->indx
;
304 /* If this is the first and only element, set it in. */
305 if (head
->first
== 0)
307 element
->next
= element
->prev
= 0;
308 head
->first
= element
;
311 /* If this index is less than that of the current element, it goes someplace
312 before the current element. */
313 else if (indx
< head
->indx
)
315 for (ptr
= head
->current
;
316 ptr
->prev
!= 0 && ptr
->prev
->indx
> indx
;
321 ptr
->prev
->next
= element
;
323 head
->first
= element
;
325 element
->prev
= ptr
->prev
;
330 /* Otherwise, it must go someplace after the current element. */
333 for (ptr
= head
->current
;
334 ptr
->next
!= 0 && ptr
->next
->indx
< indx
;
339 ptr
->next
->prev
= element
;
341 element
->next
= ptr
->next
;
346 /* Set up so this is the first element searched. */
347 head
->current
= element
;
351 /* Insert a new uninitialized element into bitmap HEAD after element
352 ELT. If ELT is NULL, insert the element at the start. Return the
355 static bitmap_element
*
356 bitmap_elt_insert_after (bitmap head
, bitmap_element
*elt
, unsigned int indx
)
358 bitmap_element
*node
= bitmap_element_allocate (head
);
365 head
->current
= node
;
368 node
->next
= head
->first
;
370 node
->next
->prev
= node
;
376 gcc_assert (head
->current
);
377 node
->next
= elt
->next
;
379 node
->next
->prev
= node
;
386 /* Copy a bitmap to another bitmap. */
389 bitmap_copy (bitmap to
, bitmap from
)
391 bitmap_element
*from_ptr
, *to_ptr
= 0;
395 /* Copy elements in forward direction one at a time. */
396 for (from_ptr
= from
->first
; from_ptr
; from_ptr
= from_ptr
->next
)
398 bitmap_element
*to_elt
= bitmap_element_allocate (to
);
400 to_elt
->indx
= from_ptr
->indx
;
401 memcpy (to_elt
->bits
, from_ptr
->bits
, sizeof (to_elt
->bits
));
403 /* Here we have a special case of bitmap_element_link, for the case
404 where we know the links are being entered in sequence. */
407 to
->first
= to
->current
= to_elt
;
408 to
->indx
= from_ptr
->indx
;
409 to_elt
->next
= to_elt
->prev
= 0;
413 to_elt
->prev
= to_ptr
;
415 to_ptr
->next
= to_elt
;
422 /* Find a bitmap element that would hold a bitmap's bit.
423 Update the `current' field even if we can't find an element that
424 would hold the bitmap's bit to make eventual allocation
427 static inline bitmap_element
*
428 bitmap_find_bit (bitmap head
, unsigned int bit
)
430 bitmap_element
*element
;
431 unsigned int indx
= bit
/ BITMAP_ELEMENT_ALL_BITS
;
433 if (head
->current
== 0
434 || head
->indx
== indx
)
435 return head
->current
;
437 if (head
->indx
< indx
)
438 /* INDX is beyond head->indx. Search from head->current
440 for (element
= head
->current
;
441 element
->next
!= 0 && element
->indx
< indx
;
442 element
= element
->next
)
445 else if (head
->indx
/ 2 < indx
)
446 /* INDX is less than head->indx and closer to head->indx than to
447 0. Search from head->current backward. */
448 for (element
= head
->current
;
449 element
->prev
!= 0 && element
->indx
> indx
;
450 element
= element
->prev
)
454 /* INDX is less than head->indx and closer to 0 than to
455 head->indx. Search from head->first forward. */
456 for (element
= head
->first
;
457 element
->next
!= 0 && element
->indx
< indx
;
458 element
= element
->next
)
461 /* `element' is the nearest to the one we want. If it's not the one we
462 want, the one we want doesn't exist. */
463 head
->current
= element
;
464 head
->indx
= element
->indx
;
465 if (element
!= 0 && element
->indx
!= indx
)
471 /* Clear a single bit in a bitmap. */
474 bitmap_clear_bit (bitmap head
, int bit
)
476 bitmap_element
*ptr
= bitmap_find_bit (head
, bit
);
480 unsigned bit_num
= bit
% BITMAP_WORD_BITS
;
481 unsigned word_num
= bit
/ BITMAP_WORD_BITS
% BITMAP_ELEMENT_WORDS
;
482 ptr
->bits
[word_num
] &= ~ (((BITMAP_WORD
) 1) << bit_num
);
484 /* If we cleared the entire word, free up the element. */
485 if (bitmap_element_zerop (ptr
))
486 bitmap_element_free (head
, ptr
);
490 /* Set a single bit in a bitmap. */
493 bitmap_set_bit (bitmap head
, int bit
)
495 bitmap_element
*ptr
= bitmap_find_bit (head
, bit
);
496 unsigned word_num
= bit
/ BITMAP_WORD_BITS
% BITMAP_ELEMENT_WORDS
;
497 unsigned bit_num
= bit
% BITMAP_WORD_BITS
;
498 BITMAP_WORD bit_val
= ((BITMAP_WORD
) 1) << bit_num
;
502 ptr
= bitmap_element_allocate (head
);
503 ptr
->indx
= bit
/ BITMAP_ELEMENT_ALL_BITS
;
504 ptr
->bits
[word_num
] = bit_val
;
505 bitmap_element_link (head
, ptr
);
508 ptr
->bits
[word_num
] |= bit_val
;
511 /* Return whether a bit is set within a bitmap. */
514 bitmap_bit_p (bitmap head
, int bit
)
520 ptr
= bitmap_find_bit (head
, bit
);
524 bit_num
= bit
% BITMAP_WORD_BITS
;
525 word_num
= bit
/ BITMAP_WORD_BITS
% BITMAP_ELEMENT_WORDS
;
527 return (ptr
->bits
[word_num
] >> bit_num
) & 1;
530 #if GCC_VERSION < 3400
531 /* Table of number of set bits in a character, indexed by value of char. */
532 static unsigned char popcount_table
[] =
534 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,
535 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,
536 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,
537 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,
538 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,
539 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,
540 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,
541 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,
545 bitmap_popcount (BITMAP_WORD a
)
547 unsigned long ret
= 0;
550 /* Just do this the table way for now */
551 for (i
= 0; i
< BITMAP_WORD_BITS
; i
+= 8)
552 ret
+= popcount_table
[(a
>> i
) & 0xff];
556 /* Count the number of bits set in the bitmap, and return it. */
559 bitmap_count_bits (bitmap a
)
561 unsigned long count
= 0;
565 for (elt
= a
->first
; elt
; elt
= elt
->next
)
567 for (ix
= 0; ix
!= BITMAP_ELEMENT_WORDS
; ix
++)
569 #if GCC_VERSION >= 3400
570 /* Note that popcountl matches BITMAP_WORD in type, so the actual size
571 of BITMAP_WORD is not material. */
572 count
+= __builtin_popcountl (elt
->bits
[ix
]);
574 count
+= bitmap_popcount (elt
->bits
[ix
]);
583 /* Return the bit number of the first set bit in the bitmap. The
584 bitmap must be non-empty. */
587 bitmap_first_set_bit (bitmap a
)
589 bitmap_element
*elt
= a
->first
;
595 bit_no
= elt
->indx
* BITMAP_ELEMENT_ALL_BITS
;
596 for (ix
= 0; ix
!= BITMAP_ELEMENT_WORDS
; ix
++)
598 word
= elt
->bits
[ix
];
604 bit_no
+= ix
* BITMAP_WORD_BITS
;
606 #if GCC_VERSION >= 3004
607 gcc_assert (sizeof(long) == sizeof (word
));
608 bit_no
+= __builtin_ctzl (word
);
610 /* Binary search for the first set bit. */
611 #if BITMAP_WORD_BITS > 64
612 #error "Fill out the table."
614 #if BITMAP_WORD_BITS > 32
615 if (!(word
& 0xffffffff))
616 word
>>= 32, bit_no
+= 32;
618 if (!(word
& 0xffff))
619 word
>>= 16, bit_no
+= 16;
621 word
>>= 8, bit_no
+= 8;
623 word
>>= 4, bit_no
+= 4;
625 word
>>= 2, bit_no
+= 2;
627 word
>>= 1, bit_no
+= 1;
629 gcc_assert (word
& 1);
638 bitmap_and (bitmap dst
, bitmap a
, bitmap b
)
640 bitmap_element
*dst_elt
= dst
->first
;
641 bitmap_element
*a_elt
= a
->first
;
642 bitmap_element
*b_elt
= b
->first
;
643 bitmap_element
*dst_prev
= NULL
;
645 gcc_assert (dst
!= a
&& dst
!= b
);
649 bitmap_copy (dst
, a
);
653 while (a_elt
&& b_elt
)
655 if (a_elt
->indx
< b_elt
->indx
)
657 else if (b_elt
->indx
< a_elt
->indx
)
661 /* Matching elts, generate A & B. */
666 dst_elt
= bitmap_elt_insert_after (dst
, dst_prev
, a_elt
->indx
);
668 dst_elt
->indx
= a_elt
->indx
;
669 for (ix
= BITMAP_ELEMENT_WORDS
; ix
--;)
671 BITMAP_WORD r
= a_elt
->bits
[ix
] & b_elt
->bits
[ix
];
673 dst_elt
->bits
[ix
] = r
;
679 dst_elt
= dst_elt
->next
;
685 bitmap_elt_clear_from (dst
, dst_elt
);
686 gcc_assert (!dst
->current
== !dst
->first
);
688 dst
->indx
= dst
->current
->indx
;
694 bitmap_and_into (bitmap a
, bitmap b
)
696 bitmap_element
*a_elt
= a
->first
;
697 bitmap_element
*b_elt
= b
->first
;
698 bitmap_element
*next
;
703 while (a_elt
&& b_elt
)
705 if (a_elt
->indx
< b_elt
->indx
)
708 bitmap_element_free (a
, a_elt
);
711 else if (b_elt
->indx
< a_elt
->indx
)
715 /* Matching elts, generate A &= B. */
719 for (ix
= BITMAP_ELEMENT_WORDS
; ix
--;)
721 BITMAP_WORD r
= a_elt
->bits
[ix
] & b_elt
->bits
[ix
];
728 bitmap_element_free (a
, a_elt
);
733 bitmap_elt_clear_from (a
, a_elt
);
734 gcc_assert (!a
->current
== !a
->first
);
735 gcc_assert (!a
->current
|| a
->indx
== a
->current
->indx
);
741 bitmap_and_compl (bitmap dst
, bitmap a
, bitmap b
)
743 bitmap_element
*dst_elt
= dst
->first
;
744 bitmap_element
*a_elt
= a
->first
;
745 bitmap_element
*b_elt
= b
->first
;
746 bitmap_element
*dst_prev
= NULL
;
748 gcc_assert (dst
!= a
&& dst
!= b
);
758 if (!b_elt
|| a_elt
->indx
< b_elt
->indx
)
762 dst_elt
= bitmap_elt_insert_after (dst
, dst_prev
, a_elt
->indx
);
764 dst_elt
->indx
= a_elt
->indx
;
765 memcpy (dst_elt
->bits
, a_elt
->bits
, sizeof (dst_elt
->bits
));
767 dst_elt
= dst_elt
->next
;
770 else if (b_elt
->indx
< a_elt
->indx
)
774 /* Matching elts, generate A & ~B. */
779 dst_elt
= bitmap_elt_insert_after (dst
, dst_prev
, a_elt
->indx
);
781 dst_elt
->indx
= a_elt
->indx
;
782 for (ix
= BITMAP_ELEMENT_WORDS
; ix
--;)
784 BITMAP_WORD r
= a_elt
->bits
[ix
] & ~b_elt
->bits
[ix
];
786 dst_elt
->bits
[ix
] = r
;
792 dst_elt
= dst_elt
->next
;
798 bitmap_elt_clear_from (dst
, dst_elt
);
799 gcc_assert (!dst
->current
== !dst
->first
);
801 dst
->indx
= dst
->current
->indx
;
804 /* A &= ~B. Returns true if A changes */
807 bitmap_and_compl_into (bitmap a
, bitmap b
)
809 bitmap_element
*a_elt
= a
->first
;
810 bitmap_element
*b_elt
= b
->first
;
811 bitmap_element
*next
;
812 BITMAP_WORD changed
= 0;
816 if (bitmap_empty_p (a
))
825 while (a_elt
&& b_elt
)
827 if (a_elt
->indx
< b_elt
->indx
)
829 else if (b_elt
->indx
< a_elt
->indx
)
833 /* Matching elts, generate A &= ~B. */
837 for (ix
= BITMAP_ELEMENT_WORDS
; ix
--;)
839 BITMAP_WORD cleared
= a_elt
->bits
[ix
] & b_elt
->bits
[ix
];
840 BITMAP_WORD r
= a_elt
->bits
[ix
] ^ cleared
;
848 bitmap_element_free (a
, a_elt
);
853 gcc_assert (!a
->current
== !a
->first
);
854 gcc_assert (!a
->current
|| a
->indx
== a
->current
->indx
);
858 /* Clear COUNT bits from START in HEAD. */
860 bitmap_clear_range (bitmap head
, unsigned int start
, unsigned int count
)
862 unsigned int first_index
= start
/ BITMAP_ELEMENT_ALL_BITS
;
863 unsigned int end_bit_plus1
= start
+ count
;
864 unsigned int end_bit
= end_bit_plus1
- 1;
865 unsigned int last_index
= (end_bit
) / BITMAP_ELEMENT_ALL_BITS
;
866 bitmap_element
*elt
= bitmap_find_bit (head
, start
);
868 /* If bitmap_find_bit returns zero, the current is the closest block
869 to the result. If the current is less than first index, find the
870 next one. Otherwise, just set elt to be current. */
875 if (head
->indx
< first_index
)
877 elt
= head
->current
->next
;
888 while (elt
&& (elt
->indx
<= last_index
))
890 bitmap_element
* next_elt
= elt
->next
;
891 unsigned elt_start_bit
= (elt
->indx
) * BITMAP_ELEMENT_ALL_BITS
;
892 unsigned elt_end_bit_plus1
= elt_start_bit
+ BITMAP_ELEMENT_ALL_BITS
;
895 if (elt_start_bit
>= start
&& elt_end_bit_plus1
<= end_bit_plus1
)
896 /* Get rid of the entire elt and go to the next one. */
897 bitmap_element_free (head
, elt
);
900 /* Going to have to knock out some bits in this elt. */
901 unsigned int first_word_to_mod
;
902 BITMAP_WORD first_mask
;
903 unsigned int last_word_to_mod
;
904 BITMAP_WORD last_mask
;
908 if (elt_start_bit
<= start
)
910 /* The first bit to turn off is somewhere inside this
912 first_word_to_mod
= (start
- elt_start_bit
) / BITMAP_WORD_BITS
;
914 /* This mask should have 1s in all bits >= start position. */
916 (((BITMAP_WORD
) 1) << ((start
% BITMAP_WORD_BITS
))) - 1;
917 first_mask
= ~first_mask
;
921 /* The first bit to turn off is below this start of this elt. */
922 first_word_to_mod
= 0;
924 first_mask
= ~first_mask
;
927 if (elt_end_bit_plus1
<= end_bit_plus1
)
929 /* The last bit to turn off is beyond this elt. */
930 last_word_to_mod
= BITMAP_ELEMENT_WORDS
- 1;
932 last_mask
= ~last_mask
;
936 /* The last bit to turn off is inside to this elt. */
938 (end_bit_plus1
- elt_start_bit
) / BITMAP_WORD_BITS
;
940 /* The last mask should have 1s below the end bit. */
942 (((BITMAP_WORD
) 1) << (((end_bit_plus1
) % BITMAP_WORD_BITS
))) - 1;
946 if (first_word_to_mod
== last_word_to_mod
)
948 BITMAP_WORD mask
= first_mask
& last_mask
;
949 elt
->bits
[first_word_to_mod
] &= ~mask
;
953 elt
->bits
[first_word_to_mod
] &= ~first_mask
;
954 for (i
= first_word_to_mod
+ 1; i
< last_word_to_mod
; i
++)
956 elt
->bits
[last_word_to_mod
] &= ~last_mask
;
958 for (i
= 0; i
< BITMAP_ELEMENT_WORDS
; i
++)
964 /* Check to see if there are any bits left. */
966 bitmap_element_free (head
, elt
);
974 head
->indx
= head
->current
->indx
;
981 bitmap_compl_and_into (bitmap a
, bitmap b
)
983 bitmap_element
*a_elt
= a
->first
;
984 bitmap_element
*b_elt
= b
->first
;
985 bitmap_element
*a_prev
= NULL
;
986 bitmap_element
*next
;
990 if (bitmap_empty_p (a
))
995 if (bitmap_empty_p (b
))
1001 while (a_elt
|| b_elt
)
1003 if (!b_elt
|| (a_elt
&& a_elt
->indx
< b_elt
->indx
))
1005 /* A is before B. Remove A */
1007 a_prev
= a_elt
->prev
;
1008 bitmap_element_free (a
, a_elt
);
1011 else if (!a_elt
|| b_elt
->indx
< a_elt
->indx
)
1013 /* B is before A. Copy B. */
1014 next
= bitmap_elt_insert_after (a
, a_prev
, b_elt
->indx
);
1015 memcpy (next
->bits
, b_elt
->bits
, sizeof (next
->bits
));
1017 b_elt
= b_elt
->next
;
1021 /* Matching elts, generate A = ~A & B. */
1023 BITMAP_WORD ior
= 0;
1025 for (ix
= BITMAP_ELEMENT_WORDS
; ix
--;)
1027 BITMAP_WORD cleared
= a_elt
->bits
[ix
] & b_elt
->bits
[ix
];
1028 BITMAP_WORD r
= b_elt
->bits
[ix
] ^ cleared
;
1030 a_elt
->bits
[ix
] = r
;
1035 bitmap_element_free (a
, a_elt
);
1039 b_elt
= b_elt
->next
;
1042 gcc_assert (!a
->current
== !a
->first
);
1043 gcc_assert (!a
->current
|| a
->indx
== a
->current
->indx
);
1047 /* DST = A | B. Return true if DST changes. */
1050 bitmap_ior (bitmap dst
, bitmap a
, bitmap b
)
1052 bitmap_element
*dst_elt
= dst
->first
;
1053 bitmap_element
*a_elt
= a
->first
;
1054 bitmap_element
*b_elt
= b
->first
;
1055 bitmap_element
*dst_prev
= NULL
;
1056 bool changed
= false;
1058 gcc_assert (dst
!= a
&& dst
!= b
);
1060 while (a_elt
|| b_elt
)
1062 if (a_elt
&& b_elt
&& a_elt
->indx
== b_elt
->indx
)
1064 /* Matching elts, generate A | B. */
1067 if (!changed
&& dst_elt
&& dst_elt
->indx
== a_elt
->indx
)
1069 for (ix
= BITMAP_ELEMENT_WORDS
; ix
--;)
1071 BITMAP_WORD r
= a_elt
->bits
[ix
] | b_elt
->bits
[ix
];
1073 if (r
!= dst_elt
->bits
[ix
])
1075 dst_elt
->bits
[ix
] = r
;
1084 dst_elt
= bitmap_elt_insert_after (dst
, dst_prev
, a_elt
->indx
);
1086 dst_elt
->indx
= a_elt
->indx
;
1087 for (ix
= BITMAP_ELEMENT_WORDS
; ix
--;)
1089 BITMAP_WORD r
= a_elt
->bits
[ix
] | b_elt
->bits
[ix
];
1091 dst_elt
->bits
[ix
] = r
;
1094 a_elt
= a_elt
->next
;
1095 b_elt
= b_elt
->next
;
1097 dst_elt
= dst_elt
->next
;
1101 /* Copy a single element. */
1102 bitmap_element
*src
;
1104 if (!b_elt
|| (a_elt
&& a_elt
->indx
< b_elt
->indx
))
1107 a_elt
= a_elt
->next
;
1112 b_elt
= b_elt
->next
;
1115 if (!changed
&& dst_elt
&& dst_elt
->indx
== src
->indx
)
1119 for (ix
= BITMAP_ELEMENT_WORDS
; ix
--;)
1120 if (src
->bits
[ix
] != dst_elt
->bits
[ix
])
1122 dst_elt
->bits
[ix
] = src
->bits
[ix
];
1130 dst_elt
= bitmap_elt_insert_after (dst
, dst_prev
, src
->indx
);
1132 dst_elt
->indx
= src
->indx
;
1133 memcpy (dst_elt
->bits
, src
->bits
, sizeof (dst_elt
->bits
));
1137 dst_elt
= dst_elt
->next
;
1144 bitmap_elt_clear_from (dst
, dst_elt
);
1146 gcc_assert (!dst
->current
== !dst
->first
);
1148 dst
->indx
= dst
->current
->indx
;
1152 /* A |= B. Return true if A changes. */
1155 bitmap_ior_into (bitmap a
, bitmap b
)
1157 bitmap_element
*a_elt
= a
->first
;
1158 bitmap_element
*b_elt
= b
->first
;
1159 bitmap_element
*a_prev
= NULL
;
1160 bool changed
= false;
1167 if (!a_elt
|| b_elt
->indx
< a_elt
->indx
)
1170 bitmap_element
*dst
= bitmap_elt_insert_after (a
, a_prev
, b_elt
->indx
);
1171 memcpy (dst
->bits
, b_elt
->bits
, sizeof (dst
->bits
));
1173 b_elt
= b_elt
->next
;
1176 else if (a_elt
->indx
< b_elt
->indx
)
1179 a_elt
= a_elt
->next
;
1183 /* Matching elts, generate A |= B. */
1187 for (ix
= BITMAP_ELEMENT_WORDS
; ix
--;)
1189 BITMAP_WORD r
= a_elt
->bits
[ix
] | b_elt
->bits
[ix
];
1191 a_elt
->bits
[ix
] = r
;
1194 for (ix
= BITMAP_ELEMENT_WORDS
; ix
--;)
1196 BITMAP_WORD r
= a_elt
->bits
[ix
] | b_elt
->bits
[ix
];
1198 if (a_elt
->bits
[ix
] != r
)
1201 a_elt
->bits
[ix
] = r
;
1204 b_elt
= b_elt
->next
;
1206 a_elt
= a_elt
->next
;
1209 gcc_assert (!a
->current
== !a
->first
);
1211 a
->indx
= a
->current
->indx
;
1218 bitmap_xor (bitmap dst
, bitmap a
, bitmap b
)
1220 bitmap_element
*dst_elt
= dst
->first
;
1221 bitmap_element
*a_elt
= a
->first
;
1222 bitmap_element
*b_elt
= b
->first
;
1223 bitmap_element
*dst_prev
= NULL
;
1225 gcc_assert (dst
!= a
&& dst
!= b
);
1232 while (a_elt
|| b_elt
)
1234 if (a_elt
&& b_elt
&& a_elt
->indx
== b_elt
->indx
)
1236 /* Matching elts, generate A ^ B. */
1238 BITMAP_WORD ior
= 0;
1241 dst_elt
= bitmap_elt_insert_after (dst
, dst_prev
, a_elt
->indx
);
1243 dst_elt
->indx
= a_elt
->indx
;
1244 for (ix
= BITMAP_ELEMENT_WORDS
; ix
--;)
1246 BITMAP_WORD r
= a_elt
->bits
[ix
] ^ b_elt
->bits
[ix
];
1249 dst_elt
->bits
[ix
] = r
;
1251 a_elt
= a_elt
->next
;
1252 b_elt
= b_elt
->next
;
1256 dst_elt
= dst_elt
->next
;
1261 /* Copy a single element. */
1262 bitmap_element
*src
;
1264 if (!b_elt
|| (a_elt
&& a_elt
->indx
< b_elt
->indx
))
1267 a_elt
= a_elt
->next
;
1272 b_elt
= b_elt
->next
;
1276 dst_elt
= bitmap_elt_insert_after (dst
, dst_prev
, src
->indx
);
1278 dst_elt
->indx
= src
->indx
;
1279 memcpy (dst_elt
->bits
, src
->bits
, sizeof (dst_elt
->bits
));
1281 dst_elt
= dst_elt
->next
;
1284 bitmap_elt_clear_from (dst
, dst_elt
);
1285 gcc_assert (!dst
->current
== !dst
->first
);
1287 dst
->indx
= dst
->current
->indx
;
1293 bitmap_xor_into (bitmap a
, bitmap b
)
1295 bitmap_element
*a_elt
= a
->first
;
1296 bitmap_element
*b_elt
= b
->first
;
1297 bitmap_element
*a_prev
= NULL
;
1307 if (!a_elt
|| b_elt
->indx
< a_elt
->indx
)
1310 bitmap_element
*dst
= bitmap_elt_insert_after (a
, a_prev
, b_elt
->indx
);
1311 memcpy (dst
->bits
, b_elt
->bits
, sizeof (dst
->bits
));
1313 b_elt
= b_elt
->next
;
1315 else if (a_elt
->indx
< b_elt
->indx
)
1318 a_elt
= a_elt
->next
;
1322 /* Matching elts, generate A ^= B. */
1324 BITMAP_WORD ior
= 0;
1325 bitmap_element
*next
= a_elt
->next
;
1327 for (ix
= BITMAP_ELEMENT_WORDS
; ix
--;)
1329 BITMAP_WORD r
= a_elt
->bits
[ix
] ^ b_elt
->bits
[ix
];
1332 a_elt
->bits
[ix
] = r
;
1334 b_elt
= b_elt
->next
;
1338 bitmap_element_free (a
, a_elt
);
1342 gcc_assert (!a
->current
== !a
->first
);
1344 a
->indx
= a
->current
->indx
;
1347 /* Return true if two bitmaps are identical.
1348 We do not bother with a check for pointer equality, as that never
1349 occurs in practice. */
1352 bitmap_equal_p (bitmap a
, bitmap b
)
1354 bitmap_element
*a_elt
;
1355 bitmap_element
*b_elt
;
1358 for (a_elt
= a
->first
, b_elt
= b
->first
;
1360 a_elt
= a_elt
->next
, b_elt
= b_elt
->next
)
1362 if (a_elt
->indx
!= b_elt
->indx
)
1364 for (ix
= BITMAP_ELEMENT_WORDS
; ix
--;)
1365 if (a_elt
->bits
[ix
] != b_elt
->bits
[ix
])
1368 return !a_elt
&& !b_elt
;
1371 /* Return true if A AND B is not empty. */
1374 bitmap_intersect_p (bitmap a
, bitmap b
)
1376 bitmap_element
*a_elt
;
1377 bitmap_element
*b_elt
;
1380 for (a_elt
= a
->first
, b_elt
= b
->first
;
1383 if (a_elt
->indx
< b_elt
->indx
)
1384 a_elt
= a_elt
->next
;
1385 else if (b_elt
->indx
< a_elt
->indx
)
1386 b_elt
= b_elt
->next
;
1389 for (ix
= BITMAP_ELEMENT_WORDS
; ix
--;)
1390 if (a_elt
->bits
[ix
] & b_elt
->bits
[ix
])
1392 a_elt
= a_elt
->next
;
1393 b_elt
= b_elt
->next
;
1399 /* Return true if A AND NOT B is not empty. */
1402 bitmap_intersect_compl_p (bitmap a
, bitmap b
)
1404 bitmap_element
*a_elt
;
1405 bitmap_element
*b_elt
;
1407 for (a_elt
= a
->first
, b_elt
= b
->first
;
1410 if (a_elt
->indx
< b_elt
->indx
)
1412 else if (b_elt
->indx
< a_elt
->indx
)
1413 b_elt
= b_elt
->next
;
1416 for (ix
= BITMAP_ELEMENT_WORDS
; ix
--;)
1417 if (a_elt
->bits
[ix
] & ~b_elt
->bits
[ix
])
1419 a_elt
= a_elt
->next
;
1420 b_elt
= b_elt
->next
;
1423 return a_elt
!= NULL
;
1427 /* DST = A | (FROM1 & ~FROM2). Return true if DST changes. */
1430 bitmap_ior_and_compl (bitmap dst
, bitmap a
, bitmap from1
, bitmap from2
)
1435 bitmap_initialize (&tmp
, &bitmap_default_obstack
);
1436 bitmap_and_compl (&tmp
, from1
, from2
);
1437 changed
= bitmap_ior (dst
, a
, &tmp
);
1438 bitmap_clear (&tmp
);
1443 /* A |= (FROM1 & ~FROM2). Return true if A changes. */
1446 bitmap_ior_and_compl_into (bitmap a
, bitmap from1
, bitmap from2
)
1451 bitmap_initialize (&tmp
, &bitmap_default_obstack
);
1452 bitmap_and_compl (&tmp
, from1
, from2
);
1453 changed
= bitmap_ior_into (a
, &tmp
);
1454 bitmap_clear (&tmp
);
1459 /* Debugging function to print out the contents of a bitmap. */
1462 debug_bitmap_file (FILE *file
, bitmap head
)
1464 bitmap_element
*ptr
;
1466 fprintf (file
, "\nfirst = %p current = %p indx = %u\n",
1467 (void *) head
->first
, (void *) head
->current
, head
->indx
);
1469 for (ptr
= head
->first
; ptr
; ptr
= ptr
->next
)
1471 unsigned int i
, j
, col
= 26;
1473 fprintf (file
, "\t%p next = %p prev = %p indx = %u\n\t\tbits = {",
1474 (void*) ptr
, (void*) ptr
->next
, (void*) ptr
->prev
, ptr
->indx
);
1476 for (i
= 0; i
< BITMAP_ELEMENT_WORDS
; i
++)
1477 for (j
= 0; j
< BITMAP_WORD_BITS
; j
++)
1478 if ((ptr
->bits
[i
] >> j
) & 1)
1482 fprintf (file
, "\n\t\t\t");
1486 fprintf (file
, " %u", (ptr
->indx
* BITMAP_ELEMENT_ALL_BITS
1487 + i
* BITMAP_WORD_BITS
+ j
));
1491 fprintf (file
, " }\n");
1495 /* Function to be called from the debugger to print the contents
1499 debug_bitmap (bitmap head
)
1501 debug_bitmap_file (stdout
, head
);
1504 /* Function to print out the contents of a bitmap. Unlike debug_bitmap_file,
1505 it does not print anything but the bits. */
1508 bitmap_print (FILE *file
, bitmap head
, const char *prefix
, const char *suffix
)
1510 const char *comma
= "";
1514 fputs (prefix
, file
);
1515 EXECUTE_IF_SET_IN_BITMAP (head
, 0, i
, bi
)
1517 fprintf (file
, "%s%d", comma
, i
);
1520 fputs (suffix
, file
);
1523 #include "gt-bitmap.h"