* gcc.c-torture/execute/20041113-1.c: New test.
[official-gcc.git] / gcc / bitmap.c
bloba067984457ec7c6920171295bc795e940a349c6b
1 /* Functions to support general ended bitmaps.
2 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2003, 2004
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
10 version.
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
15 for more details.
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, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "rtl.h"
27 #include "flags.h"
28 #include "obstack.h"
29 #include "ggc.h"
30 #include "bitmap.h"
32 /* Obstack to allocate bitmap elements from. */
33 static struct obstack bitmap_obstack;
34 static int bitmap_obstack_init = FALSE;
36 #ifndef INLINE
37 #ifndef __GNUC__
38 #define INLINE
39 #else
40 #define INLINE __inline__
41 #endif
42 #endif
44 /* Global data */
45 bitmap_element bitmap_zero_bits; /* An element of all zero bits. */
46 static bitmap_element *bitmap_free; /* Freelist of bitmap elements. */
47 static GTY((deletable)) bitmap_element *bitmap_ggc_free;
49 static void bitmap_elem_to_freelist (bitmap, bitmap_element *);
50 static void bitmap_element_free (bitmap, bitmap_element *);
51 static bitmap_element *bitmap_element_allocate (bitmap);
52 static int bitmap_element_zerop (bitmap_element *);
53 static void bitmap_element_link (bitmap, bitmap_element *);
54 static bitmap_element *bitmap_elt_insert_after (bitmap, bitmap_element *);
55 static void bitmap_elt_clear_from (bitmap, bitmap_element *);
56 static bitmap_element *bitmap_find_bit (bitmap, unsigned int);
59 /* Add ELEM to the appropriate freelist. */
60 static INLINE void
61 bitmap_elem_to_freelist (bitmap head, bitmap_element *elt)
63 if (head->using_obstack)
65 elt->next = bitmap_free;
66 bitmap_free = elt;
68 else
70 elt->next = bitmap_ggc_free;
71 bitmap_ggc_free = elt;
75 /* Free a bitmap element. Since these are allocated off the
76 bitmap_obstack, "free" actually means "put onto the freelist". */
78 static INLINE void
79 bitmap_element_free (bitmap head, bitmap_element *elt)
81 bitmap_element *next = elt->next;
82 bitmap_element *prev = elt->prev;
84 if (prev)
85 prev->next = next;
87 if (next)
88 next->prev = prev;
90 if (head->first == elt)
91 head->first = next;
93 /* Since the first thing we try is to insert before current,
94 make current the next entry in preference to the previous. */
95 if (head->current == elt)
97 head->current = next != 0 ? next : prev;
98 if (head->current)
99 head->indx = head->current->indx;
101 bitmap_elem_to_freelist (head, elt);
104 /* Allocate a bitmap element. The bits are cleared, but nothing else is. */
106 static INLINE bitmap_element *
107 bitmap_element_allocate (bitmap head)
109 bitmap_element *element;
111 if (head->using_obstack)
113 if (bitmap_free != 0)
115 element = bitmap_free;
116 bitmap_free = element->next;
118 else
120 /* We can't use gcc_obstack_init to initialize the obstack since
121 print-rtl.c now calls bitmap functions, and bitmap is linked
122 into the gen* functions. */
123 if (!bitmap_obstack_init)
125 bitmap_obstack_init = TRUE;
127 #if !defined(__GNUC__) || (__GNUC__ < 2)
128 #define __alignof__(type) 0
129 #endif
131 obstack_specify_allocation (&bitmap_obstack, OBSTACK_CHUNK_SIZE,
132 __alignof__ (bitmap_element),
133 obstack_chunk_alloc,
134 obstack_chunk_free);
137 element = XOBNEW (&bitmap_obstack, bitmap_element);
140 else
142 if (bitmap_ggc_free != NULL)
144 element = bitmap_ggc_free;
145 bitmap_ggc_free = element->next;
147 else
148 element = GGC_NEW (bitmap_element);
151 memset (element->bits, 0, sizeof (element->bits));
153 return element;
156 /* Release any memory allocated by bitmaps. */
158 void
159 bitmap_release_memory (void)
161 bitmap_free = 0;
162 if (bitmap_obstack_init)
164 bitmap_obstack_init = FALSE;
165 obstack_free (&bitmap_obstack, NULL);
169 /* Return nonzero if all bits in an element are zero. */
171 static INLINE int
172 bitmap_element_zerop (bitmap_element *element)
174 #if BITMAP_ELEMENT_WORDS == 2
175 return (element->bits[0] | element->bits[1]) == 0;
176 #else
177 unsigned i;
179 for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
180 if (element->bits[i] != 0)
181 return 0;
183 return 1;
184 #endif
187 /* Link the bitmap element into the current bitmap linked list. */
189 static INLINE void
190 bitmap_element_link (bitmap head, bitmap_element *element)
192 unsigned int indx = element->indx;
193 bitmap_element *ptr;
195 /* If this is the first and only element, set it in. */
196 if (head->first == 0)
198 element->next = element->prev = 0;
199 head->first = element;
202 /* If this index is less than that of the current element, it goes someplace
203 before the current element. */
204 else if (indx < head->indx)
206 for (ptr = head->current;
207 ptr->prev != 0 && ptr->prev->indx > indx;
208 ptr = ptr->prev)
211 if (ptr->prev)
212 ptr->prev->next = element;
213 else
214 head->first = element;
216 element->prev = ptr->prev;
217 element->next = ptr;
218 ptr->prev = element;
221 /* Otherwise, it must go someplace after the current element. */
222 else
224 for (ptr = head->current;
225 ptr->next != 0 && ptr->next->indx < indx;
226 ptr = ptr->next)
229 if (ptr->next)
230 ptr->next->prev = element;
232 element->next = ptr->next;
233 element->prev = ptr;
234 ptr->next = element;
237 /* Set up so this is the first element searched. */
238 head->current = element;
239 head->indx = indx;
242 /* Insert a new uninitialized element into bitmap HEAD after element
243 ELT. If ELT is NULL, insert the element at the start. Return the
244 new element. */
246 static bitmap_element *
247 bitmap_elt_insert_after (bitmap head, bitmap_element *elt)
249 bitmap_element *node = bitmap_element_allocate (head);
251 if (!elt)
253 if (!head->current)
254 head->current = node;
255 node->next = head->first;
256 if (node->next)
257 node->next->prev = node;
258 head->first = node;
259 node->prev = NULL;
261 else
263 gcc_assert (head->current);
264 node->next = elt->next;
265 if (node->next)
266 node->next->prev = node;
267 elt->next = node;
268 node->prev = elt;
270 return node;
273 /* Remove ELT and all following elements from bitmap HEAD. */
275 void
276 bitmap_elt_clear_from (bitmap head, bitmap_element *elt)
278 bitmap_element *next;
280 while (elt)
282 next = elt->next;
283 bitmap_element_free (head, elt);
284 elt = next;
289 /* Clear a bitmap by freeing the linked list. */
291 INLINE void
292 bitmap_clear (bitmap head)
294 bitmap_element *element, *next;
296 for (element = head->first; element != 0; element = next)
298 next = element->next;
299 bitmap_elem_to_freelist (head, element);
302 head->first = head->current = 0;
305 /* Copy a bitmap to another bitmap. */
307 void
308 bitmap_copy (bitmap to, bitmap from)
310 bitmap_element *from_ptr, *to_ptr = 0;
311 #if BITMAP_ELEMENT_WORDS != 2
312 unsigned i;
313 #endif
315 bitmap_clear (to);
317 /* Copy elements in forward direction one at a time. */
318 for (from_ptr = from->first; from_ptr; from_ptr = from_ptr->next)
320 bitmap_element *to_elt = bitmap_element_allocate (to);
322 to_elt->indx = from_ptr->indx;
324 #if BITMAP_ELEMENT_WORDS == 2
325 to_elt->bits[0] = from_ptr->bits[0];
326 to_elt->bits[1] = from_ptr->bits[1];
327 #else
328 for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
329 to_elt->bits[i] = from_ptr->bits[i];
330 #endif
332 /* Here we have a special case of bitmap_element_link, for the case
333 where we know the links are being entered in sequence. */
334 if (to_ptr == 0)
336 to->first = to->current = to_elt;
337 to->indx = from_ptr->indx;
338 to_elt->next = to_elt->prev = 0;
340 else
342 to_elt->prev = to_ptr;
343 to_elt->next = 0;
344 to_ptr->next = to_elt;
347 to_ptr = to_elt;
351 /* Find a bitmap element that would hold a bitmap's bit.
352 Update the `current' field even if we can't find an element that
353 would hold the bitmap's bit to make eventual allocation
354 faster. */
356 static INLINE bitmap_element *
357 bitmap_find_bit (bitmap head, unsigned int bit)
359 bitmap_element *element;
360 unsigned int indx = bit / BITMAP_ELEMENT_ALL_BITS;
362 if (head->current == 0
363 || head->indx == indx)
364 return head->current;
366 if (head->indx > indx)
367 for (element = head->current;
368 element->prev != 0 && element->indx > indx;
369 element = element->prev)
372 else
373 for (element = head->current;
374 element->next != 0 && element->indx < indx;
375 element = element->next)
378 /* `element' is the nearest to the one we want. If it's not the one we
379 want, the one we want doesn't exist. */
380 head->current = element;
381 head->indx = element->indx;
382 if (element != 0 && element->indx != indx)
383 element = 0;
385 return element;
388 /* Clear a single bit in a bitmap. */
390 void
391 bitmap_clear_bit (bitmap head, int bit)
393 bitmap_element *ptr = bitmap_find_bit (head, bit);
395 if (ptr != 0)
397 unsigned bit_num = bit % BITMAP_WORD_BITS;
398 unsigned word_num = bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
399 ptr->bits[word_num] &= ~ (((BITMAP_WORD) 1) << bit_num);
401 /* If we cleared the entire word, free up the element. */
402 if (bitmap_element_zerop (ptr))
403 bitmap_element_free (head, ptr);
407 /* Set a single bit in a bitmap. */
409 void
410 bitmap_set_bit (bitmap head, int bit)
412 bitmap_element *ptr = bitmap_find_bit (head, bit);
413 unsigned word_num = bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
414 unsigned bit_num = bit % BITMAP_WORD_BITS;
415 BITMAP_WORD bit_val = ((BITMAP_WORD) 1) << bit_num;
417 if (ptr == 0)
419 ptr = bitmap_element_allocate (head);
420 ptr->indx = bit / BITMAP_ELEMENT_ALL_BITS;
421 ptr->bits[word_num] = bit_val;
422 bitmap_element_link (head, ptr);
424 else
425 ptr->bits[word_num] |= bit_val;
428 /* Return whether a bit is set within a bitmap. */
431 bitmap_bit_p (bitmap head, int bit)
433 bitmap_element *ptr;
434 unsigned bit_num;
435 unsigned word_num;
437 ptr = bitmap_find_bit (head, bit);
438 if (ptr == 0)
439 return 0;
441 bit_num = bit % BITMAP_WORD_BITS;
442 word_num = bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
444 return (ptr->bits[word_num] >> bit_num) & 1;
447 /* Return the bit number of the first set bit in the bitmap. The
448 bitmap must be non-empty. */
450 unsigned
451 bitmap_first_set_bit (bitmap a)
453 bitmap_element *elt = a->first;
454 unsigned bit_no;
455 BITMAP_WORD word;
456 unsigned ix;
458 gcc_assert (elt);
459 bit_no = elt->indx * BITMAP_ELEMENT_ALL_BITS;
460 for (ix = 0; ix != BITMAP_ELEMENT_WORDS; ix++)
462 word = elt->bits[ix];
463 if (word)
464 goto found_bit;
466 gcc_unreachable ();
467 found_bit:
468 bit_no += ix * BITMAP_WORD_BITS;
470 #if GCC_VERSION >= 3004
471 gcc_assert (sizeof(long) == sizeof (word));
472 bit_no += __builtin_ctzl (word);
473 #else
474 /* Binary search for the first set bit. */
475 #if BITMAP_WORD_BITS > 64
476 #error "Fill out the table."
477 #endif
478 #if BITMAP_WORD_BITS > 32
479 if (!(word & 0xffffffff))
480 word >>= 32, bit_no += 32;
481 #endif
482 if (!(word & 0xffff))
483 word >>= 16, bit_no += 16;
484 if (!(word & 0xff))
485 word >>= 8, bit_no += 8;
486 if (!(word & 0xf))
487 word >>= 4, bit_no += 4;
488 if (!(word & 0x3))
489 word >>= 2, bit_no += 2;
490 if (!(word & 0x1))
491 word >>= 1, bit_no += 1;
493 gcc_assert (word & 1);
494 #endif
495 return bit_no;
499 /* DST = A & B. */
501 void
502 bitmap_and (bitmap dst, bitmap a, bitmap b)
504 bitmap_element *dst_elt = dst->first;
505 bitmap_element *a_elt = a->first;
506 bitmap_element *b_elt = b->first;
507 bitmap_element *dst_prev = NULL;
509 gcc_assert (dst != a && dst != b && a != b);
510 while (a_elt && b_elt)
512 if (a_elt->indx < b_elt->indx)
513 a_elt = a_elt->next;
514 else if (b_elt->indx < a_elt->indx)
515 b_elt = b_elt->next;
516 else
518 /* Matching elts, generate A & B. */
519 unsigned ix;
520 BITMAP_WORD ior = 0;
522 if (!dst_elt)
523 dst_elt = bitmap_elt_insert_after (dst, dst_prev);
525 dst_elt->indx = a_elt->indx;
526 for (ix = BITMAP_ELEMENT_WORDS; ix--;)
528 BITMAP_WORD r = a_elt->bits[ix] & b_elt->bits[ix];
530 dst_elt->bits[ix] = r;
531 ior |= r;
533 if (ior)
535 dst_prev = dst_elt;
536 dst_elt = dst_elt->next;
538 a_elt = a_elt->next;
539 b_elt = b_elt->next;
542 bitmap_elt_clear_from (dst, dst_elt);
543 gcc_assert (!dst->current == !dst->first);
544 if (dst->current)
545 dst->indx = dst->current->indx;
548 /* A &= B. */
550 void
551 bitmap_and_into (bitmap a, bitmap b)
553 bitmap_element *a_elt = a->first;
554 bitmap_element *b_elt = b->first;
555 bitmap_element *next;
557 gcc_assert (a != b);
558 while (a_elt && b_elt)
560 if (a_elt->indx < b_elt->indx)
562 next = a_elt->next;
563 bitmap_element_free (a, a_elt);
564 a_elt = next;
566 else if (b_elt->indx < a_elt->indx)
567 b_elt = b_elt->next;
568 else
570 /* Matching elts, generate A &= B. */
571 unsigned ix;
572 BITMAP_WORD ior = 0;
574 for (ix = BITMAP_ELEMENT_WORDS; ix--;)
576 BITMAP_WORD r = a_elt->bits[ix] & b_elt->bits[ix];
578 a_elt->bits[ix] = r;
579 ior |= r;
581 next = a_elt->next;
582 if (!ior)
583 bitmap_element_free (a, a_elt);
584 a_elt = next;
585 b_elt = b_elt->next;
588 bitmap_elt_clear_from (a, a_elt);
589 gcc_assert (!a->current == !a->first);
590 gcc_assert (!a->current || a->indx == a->current->indx);
593 /* DST = A & ~B */
595 void
596 bitmap_and_compl (bitmap dst, bitmap a, bitmap b)
598 bitmap_element *dst_elt = dst->first;
599 bitmap_element *a_elt = a->first;
600 bitmap_element *b_elt = b->first;
601 bitmap_element *dst_prev = NULL;
603 gcc_assert (dst != a && dst != b && a != b);
605 while (a_elt)
607 if (!b_elt || a_elt->indx < b_elt->indx)
609 /* Copy a_elt. */
610 if (!dst_elt)
611 dst_elt = bitmap_elt_insert_after (dst, dst_prev);
613 dst_elt->indx = a_elt->indx;
614 memcpy (dst_elt->bits, a_elt->bits, sizeof (dst_elt->bits));
615 dst_prev = dst_elt;
616 dst_elt = dst_elt->next;
617 a_elt = a_elt->next;
619 else if (b_elt->indx < a_elt->indx)
620 b_elt = b_elt->next;
621 else
623 /* Matching elts, generate A & ~B. */
624 unsigned ix;
625 BITMAP_WORD ior = 0;
627 if (!dst_elt)
628 dst_elt = bitmap_elt_insert_after (dst, dst_prev);
630 dst_elt->indx = a_elt->indx;
631 for (ix = BITMAP_ELEMENT_WORDS; ix--;)
633 BITMAP_WORD r = a_elt->bits[ix] & ~b_elt->bits[ix];
635 dst_elt->bits[ix] = r;
636 ior |= r;
638 if (ior)
640 dst_prev = dst_elt;
641 dst_elt = dst_elt->next;
643 a_elt = a_elt->next;
644 b_elt = b_elt->next;
647 bitmap_elt_clear_from (dst, dst_elt);
648 gcc_assert (!dst->current == !dst->first);
649 if (dst->current)
650 dst->indx = dst->current->indx;
653 /* A &= ~B */
655 void
656 bitmap_and_compl_into (bitmap a, bitmap b)
658 bitmap_element *a_elt = a->first;
659 bitmap_element *b_elt = b->first;
660 bitmap_element *next;
662 gcc_assert (a != b);
663 while (a_elt && b_elt)
665 if (a_elt->indx < b_elt->indx)
666 a_elt = a_elt->next;
667 else if (b_elt->indx < a_elt->indx)
668 b_elt = b_elt->next;
669 else
671 /* Matching elts, generate A &= ~B. */
672 unsigned ix;
673 BITMAP_WORD ior = 0;
675 for (ix = BITMAP_ELEMENT_WORDS; ix--;)
677 BITMAP_WORD r = a_elt->bits[ix] & ~b_elt->bits[ix];
679 a_elt->bits[ix] = r;
680 ior |= r;
682 next = a_elt->next;
683 if (!ior)
684 bitmap_element_free (a, a_elt);
685 a_elt = next;
686 b_elt = b_elt->next;
689 gcc_assert (!a->current == !a->first);
690 gcc_assert (!a->current || a->indx == a->current->indx);
693 /* DST = A | B. Return true if DST changes. */
695 bool
696 bitmap_ior (bitmap dst, bitmap a, bitmap b)
698 bitmap_element *dst_elt = dst->first;
699 bitmap_element *a_elt = a->first;
700 bitmap_element *b_elt = b->first;
701 bitmap_element *dst_prev = NULL;
702 bool changed = false;
704 gcc_assert (dst != a && dst != b && a != b);
705 while (a_elt || b_elt)
707 if (a_elt && b_elt && a_elt->indx == b_elt->indx)
709 /* Matching elts, generate A | B. */
710 unsigned ix;
712 if (!changed && dst_elt && dst_elt->indx == a_elt->indx)
714 for (ix = BITMAP_ELEMENT_WORDS; ix--;)
716 BITMAP_WORD r = a_elt->bits[ix] | b_elt->bits[ix];
718 if (r != dst_elt->bits[ix])
720 dst_elt->bits[ix] = r;
721 changed = true;
725 else
727 changed = true;
728 if (!dst_elt)
729 dst_elt = bitmap_elt_insert_after (dst, dst_prev);
730 dst_elt->indx = a_elt->indx;
731 for (ix = BITMAP_ELEMENT_WORDS; ix--;)
733 BITMAP_WORD r = a_elt->bits[ix] | b_elt->bits[ix];
735 dst_elt->bits[ix] = r;
738 a_elt = a_elt->next;
739 b_elt = b_elt->next;
740 dst_prev = dst_elt;
741 dst_elt = dst_elt->next;
743 else
745 /* Copy a single element. */
746 bitmap_element *src;
748 if (!b_elt || (a_elt && a_elt->indx < b_elt->indx))
750 src = a_elt;
751 a_elt = a_elt->next;
753 else
755 src = b_elt;
756 b_elt = b_elt->next;
759 if (!changed && dst_elt && dst_elt->indx == src->indx)
761 unsigned ix;
763 for (ix = BITMAP_ELEMENT_WORDS; ix--;)
764 if (src->bits[ix] != dst_elt->bits[ix])
766 dst_elt->bits[ix] = src->bits[ix];
767 changed = true;
770 else
772 changed = true;
773 if (!dst_elt)
774 dst_elt = bitmap_elt_insert_after (dst, dst_prev);
775 dst_elt->indx = src->indx;
776 memcpy (dst_elt->bits, src->bits, sizeof (dst_elt->bits));
779 dst_prev = dst_elt;
780 dst_elt = dst_elt->next;
784 if (dst_elt)
786 changed = true;
787 bitmap_elt_clear_from (dst, dst_elt);
789 gcc_assert (!dst->current == !dst->first);
790 if (dst->current)
791 dst->indx = dst->current->indx;
792 return changed;
795 /* A |= B. Return true if A changes. */
797 bool
798 bitmap_ior_into (bitmap a, bitmap b)
800 bitmap_element *a_elt = a->first;
801 bitmap_element *b_elt = b->first;
802 bitmap_element *a_prev = NULL;
803 bool changed = false;
805 gcc_assert (a != b);
806 while (b_elt)
808 if (!a_elt || b_elt->indx < a_elt->indx)
810 /* Copy b_elt. */
811 bitmap_element *dst = bitmap_elt_insert_after (a, a_prev);
813 dst->indx = b_elt->indx;
814 memcpy (dst->bits, b_elt->bits, sizeof (dst->bits));
815 a_prev = dst;
816 b_elt = b_elt->next;
817 changed = true;
819 else if (a_elt->indx < b_elt->indx)
821 a_prev = a_elt;
822 a_elt = a_elt->next;
824 else
826 /* Matching elts, generate A |= B. */
827 unsigned ix;
829 if (changed)
830 for (ix = BITMAP_ELEMENT_WORDS; ix--;)
832 BITMAP_WORD r = a_elt->bits[ix] | b_elt->bits[ix];
834 a_elt->bits[ix] = r;
836 else
837 for (ix = BITMAP_ELEMENT_WORDS; ix--;)
839 BITMAP_WORD r = a_elt->bits[ix] | b_elt->bits[ix];
841 if (a_elt->bits[ix] != r)
843 changed = true;
844 a_elt->bits[ix] = r;
847 b_elt = b_elt->next;
848 a_prev = a_elt;
849 a_elt = a_elt->next;
852 gcc_assert (!a->current == !a->first);
853 if (a->current)
854 a->indx = a->current->indx;
855 return changed;
858 /* DST = A ^ B */
860 void
861 bitmap_xor (bitmap dst, bitmap a, bitmap b)
863 bitmap_element *dst_elt = dst->first;
864 bitmap_element *a_elt = a->first;
865 bitmap_element *b_elt = b->first;
866 bitmap_element *dst_prev = NULL;
868 gcc_assert (dst != a && dst != b && a != b);
869 while (a_elt || b_elt)
871 if (a_elt && b_elt && a_elt->indx == b_elt->indx)
873 /* Matching elts, generate A ^ B. */
874 unsigned ix;
875 BITMAP_WORD ior = 0;
877 if (!dst_elt)
878 dst_elt = bitmap_elt_insert_after (dst, dst_prev);
880 dst_elt->indx = a_elt->indx;
881 for (ix = BITMAP_ELEMENT_WORDS; ix--;)
883 BITMAP_WORD r = a_elt->bits[ix] ^ b_elt->bits[ix];
885 ior |= r;
886 dst_elt->bits[ix] = r;
888 a_elt = a_elt->next;
889 b_elt = b_elt->next;
890 if (ior)
892 dst_prev = dst_elt;
893 dst_elt = dst_elt->next;
896 else
898 /* Copy a single element. */
899 bitmap_element *src;
901 if (!b_elt || (a_elt && a_elt->indx < b_elt->indx))
903 src = a_elt;
904 a_elt = a_elt->next;
906 else
908 src = b_elt;
909 b_elt = b_elt->next;
912 if (!dst_elt)
913 dst_elt = bitmap_elt_insert_after (dst, dst_prev);
915 dst_elt->indx = src->indx;
916 memcpy (dst_elt->bits, src->bits, sizeof (dst_elt->bits));
917 dst_prev = dst_elt;
918 dst_elt = dst_elt->next;
921 bitmap_elt_clear_from (dst, dst_elt);
922 gcc_assert (!dst->current == !dst->first);
923 if (dst->current)
924 dst->indx = dst->current->indx;
927 /* A ^= B */
929 void
930 bitmap_xor_into (bitmap a, bitmap b)
932 bitmap_element *a_elt = a->first;
933 bitmap_element *b_elt = b->first;
934 bitmap_element *a_prev = NULL;
936 gcc_assert (a != b);
937 while (b_elt)
939 if (!a_elt || b_elt->indx < a_elt->indx)
941 /* Copy b_elt. */
942 bitmap_element *dst = bitmap_elt_insert_after (a, a_prev);
944 dst->indx = b_elt->indx;
945 memcpy (dst->bits, b_elt->bits, sizeof (dst->bits));
946 a_prev = dst;
947 b_elt = b_elt->next;
949 else if (a_elt->indx < b_elt->indx)
951 a_prev = a_elt;
952 a_elt = a_elt->next;
954 else
956 /* Matching elts, generate A ^= B. */
957 unsigned ix;
958 BITMAP_WORD ior = 0;
959 bitmap_element *next = a_elt->next;
961 for (ix = BITMAP_ELEMENT_WORDS; ix--;)
963 BITMAP_WORD r = a_elt->bits[ix] ^ b_elt->bits[ix];
965 ior |= r;
966 a_elt->bits[ix] = r;
968 b_elt = b_elt->next;
969 if (ior)
970 a_prev = a_elt;
971 else
972 bitmap_element_free (a, a_elt);
973 a_elt = next;
976 gcc_assert (!a->current == !a->first);
977 if (a->current)
978 a->indx = a->current->indx;
981 /* Return true if two bitmaps are identical.
982 We do not bother with a check for pointer equality, as that never
983 occurs in practice. */
985 bool
986 bitmap_equal_p (bitmap a, bitmap b)
988 bitmap_element *a_elt;
989 bitmap_element *b_elt;
990 unsigned ix;
992 for (a_elt = a->first, b_elt = b->first;
993 a_elt && b_elt;
994 a_elt = a_elt->next, b_elt = b_elt->next)
996 if (a_elt->indx != b_elt->indx)
997 return false;
998 for (ix = BITMAP_ELEMENT_WORDS; ix--;)
999 if (a_elt->bits[ix] != b_elt->bits[ix])
1000 return false;
1002 return !a_elt && !b_elt;
1005 /* Return true if A AND B is not empty. */
1007 bool
1008 bitmap_intersect_p (bitmap a, bitmap b)
1010 bitmap_element *a_elt;
1011 bitmap_element *b_elt;
1012 unsigned ix;
1014 for (a_elt = a->first, b_elt = b->first;
1015 a_elt && b_elt;)
1017 if (a_elt->indx < b_elt->indx)
1018 a_elt = a_elt->next;
1019 else if (b_elt->indx < a_elt->indx)
1020 b_elt = b_elt->next;
1021 else
1023 for (ix = BITMAP_ELEMENT_WORDS; ix--;)
1024 if (a_elt->bits[ix] & b_elt->bits[ix])
1025 return true;
1026 a_elt = a_elt->next;
1027 b_elt = b_elt->next;
1030 return false;
1033 /* Return true if A AND NOT B is not empty. */
1035 bool
1036 bitmap_intersect_compl_p (bitmap a, bitmap b)
1038 bitmap_element *a_elt;
1039 bitmap_element *b_elt;
1040 unsigned ix;
1041 for (a_elt = a->first, b_elt = b->first;
1042 a_elt && b_elt;)
1044 if (a_elt->indx < b_elt->indx)
1045 return true;
1046 else if (b_elt->indx < a_elt->indx)
1047 b_elt = b_elt->next;
1048 else
1050 for (ix = BITMAP_ELEMENT_WORDS; ix--;)
1051 if (a_elt->bits[ix] & ~b_elt->bits[ix])
1052 return true;
1053 a_elt = a_elt->next;
1054 b_elt = b_elt->next;
1057 return a_elt != NULL;
1061 /* DST = A | (FROM1 & ~FROM2). Return true if DST changes. */
1063 bool
1064 bitmap_ior_and_compl (bitmap dst, bitmap a, bitmap from1, bitmap from2)
1066 bitmap_head tmp;
1067 bool changed;
1069 tmp.first = tmp.current = 0;
1070 tmp.using_obstack = 0;
1071 bitmap_and_compl (&tmp, from1, from2);
1072 changed = bitmap_ior (dst, a, &tmp);
1073 bitmap_clear (&tmp);
1075 return changed;
1078 /* A |= (FROM1 & ~FROM2). Return true if A changes. */
1080 bool
1081 bitmap_ior_and_compl_into (bitmap a, bitmap from1, bitmap from2)
1083 bitmap_head tmp;
1084 bool changed;
1086 tmp.first = tmp.current = 0;
1087 tmp.using_obstack = 0;
1088 bitmap_and_compl (&tmp, from1, from2);
1089 changed = bitmap_ior_into (a, &tmp);
1090 bitmap_clear (&tmp);
1092 return changed;
1095 /* Initialize a bitmap header. */
1097 bitmap
1098 bitmap_initialize (bitmap head, int using_obstack)
1100 if (head == NULL && ! using_obstack)
1101 head = GGC_NEW (struct bitmap_head_def);
1103 head->first = head->current = 0;
1104 head->using_obstack = using_obstack;
1106 return head;
1109 /* Debugging function to print out the contents of a bitmap. */
1111 void
1112 debug_bitmap_file (FILE *file, bitmap head)
1114 bitmap_element *ptr;
1116 fprintf (file, "\nfirst = " HOST_PTR_PRINTF
1117 " current = " HOST_PTR_PRINTF " indx = %u\n",
1118 (void *) head->first, (void *) head->current, head->indx);
1120 for (ptr = head->first; ptr; ptr = ptr->next)
1122 unsigned int i, j, col = 26;
1124 fprintf (file, "\t" HOST_PTR_PRINTF " next = " HOST_PTR_PRINTF
1125 " prev = " HOST_PTR_PRINTF " indx = %u\n\t\tbits = {",
1126 (void*) ptr, (void*) ptr->next, (void*) ptr->prev, ptr->indx);
1128 for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
1129 for (j = 0; j < BITMAP_WORD_BITS; j++)
1130 if ((ptr->bits[i] >> j) & 1)
1132 if (col > 70)
1134 fprintf (file, "\n\t\t\t");
1135 col = 24;
1138 fprintf (file, " %u", (ptr->indx * BITMAP_ELEMENT_ALL_BITS
1139 + i * BITMAP_WORD_BITS + j));
1140 col += 4;
1143 fprintf (file, " }\n");
1147 /* Function to be called from the debugger to print the contents
1148 of a bitmap. */
1150 void
1151 debug_bitmap (bitmap head)
1153 debug_bitmap_file (stdout, head);
1156 /* Function to print out the contents of a bitmap. Unlike debug_bitmap_file,
1157 it does not print anything but the bits. */
1159 void
1160 bitmap_print (FILE *file, bitmap head, const char *prefix, const char *suffix)
1162 const char *comma = "";
1163 unsigned i;
1164 bitmap_iterator bi;
1166 fputs (prefix, file);
1167 EXECUTE_IF_SET_IN_BITMAP (head, 0, i, bi)
1169 fprintf (file, "%s%d", comma, i);
1170 comma = ", ";
1172 fputs (suffix, file);
1175 #include "gt-bitmap.h"