1 /* Functions to support general ended bitmaps.
2 Copyright (C) 1997, 1998, 1999, 2000, 2001 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 2, 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 COPYING. If not, write to the Free
18 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
28 /* Obstack to allocate bitmap elements from. */
29 static struct obstack bitmap_obstack;
30 static int bitmap_obstack_init = FALSE;
36 #define INLINE __inline__
41 bitmap_element bitmap_zero_bits; /* An element of all zero bits. */
42 static bitmap_element *bitmap_free; /* Freelist of bitmap elements. */
44 static void bitmap_element_free PARAMS ((bitmap, bitmap_element *));
45 static bitmap_element *bitmap_element_allocate PARAMS ((void));
46 static int bitmap_element_zerop PARAMS ((bitmap_element *));
47 static void bitmap_element_link PARAMS ((bitmap, bitmap_element *));
48 static bitmap_element *bitmap_find_bit PARAMS ((bitmap, unsigned int));
50 /* Free a bitmap element. Since these are allocated off the
51 bitmap_obstack, "free" actually means "put onto the freelist". */
54 bitmap_element_free (head, elt)
58 bitmap_element *next = elt->next;
59 bitmap_element *prev = elt->prev;
67 if (head->first == elt)
70 /* Since the first thing we try is to insert before current,
71 make current the next entry in preference to the previous. */
72 if (head->current == elt)
74 head->current = next != 0 ? next : prev;
76 head->indx = head->current->indx;
79 elt->next = bitmap_free;
83 /* Allocate a bitmap element. The bits are cleared, but nothing else is. */
85 static INLINE bitmap_element *
86 bitmap_element_allocate ()
88 bitmap_element *element;
92 element = bitmap_free;
93 bitmap_free = element->next;
97 /* We can't use gcc_obstack_init to initialize the obstack since
98 print-rtl.c now calls bitmap functions, and bitmap is linked
99 into the gen* functions. */
100 if (!bitmap_obstack_init)
102 bitmap_obstack_init = TRUE;
104 /* Let particular systems override the size of a chunk. */
105 #ifndef OBSTACK_CHUNK_SIZE
106 #define OBSTACK_CHUNK_SIZE 0
108 /* Let them override the alloc and free routines too. */
109 #ifndef OBSTACK_CHUNK_ALLOC
110 #define OBSTACK_CHUNK_ALLOC xmalloc
112 #ifndef OBSTACK_CHUNK_FREE
113 #define OBSTACK_CHUNK_FREE free
116 #if !defined(__GNUC__) || (__GNUC__ < 2)
117 #define __alignof__(type) 0
120 obstack_specify_allocation (&bitmap_obstack, OBSTACK_CHUNK_SIZE,
121 __alignof__ (bitmap_element),
122 (void *(*) PARAMS ((long))) OBSTACK_CHUNK_ALLOC,
123 (void (*) PARAMS ((void *))) OBSTACK_CHUNK_FREE);
126 element = (bitmap_element *) obstack_alloc (&bitmap_obstack,
127 sizeof (bitmap_element));
130 memset (element->bits, 0, sizeof (element->bits));
135 /* Release any memory allocated by bitmaps. */
138 bitmap_release_memory ()
141 if (bitmap_obstack_init)
143 bitmap_obstack_init = FALSE;
144 obstack_free (&bitmap_obstack, NULL);
148 /* Return nonzero if all bits in an element are zero. */
151 bitmap_element_zerop (element)
152 bitmap_element *element;
154 #if BITMAP_ELEMENT_WORDS == 2
155 return (element->bits[0] | element->bits[1]) == 0;
159 for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
160 if (element->bits[i] != 0)
167 /* Link the bitmap element into the current bitmap linked list. */
170 bitmap_element_link (head, element)
172 bitmap_element *element;
174 unsigned int indx = element->indx;
177 /* If this is the first and only element, set it in. */
178 if (head->first == 0)
180 element->next = element->prev = 0;
181 head->first = element;
184 /* If this index is less than that of the current element, it goes someplace
185 before the current element. */
186 else if (indx < head->indx)
188 for (ptr = head->current;
189 ptr->prev != 0 && ptr->prev->indx > indx;
194 ptr->prev->next = element;
196 head->first = element;
198 element->prev = ptr->prev;
203 /* Otherwise, it must go someplace after the current element. */
206 for (ptr = head->current;
207 ptr->next != 0 && ptr->next->indx < indx;
212 ptr->next->prev = element;
214 element->next = ptr->next;
219 /* Set up so this is the first element searched. */
220 head->current = element;
224 /* Clear a bitmap by freeing the linked list. */
230 bitmap_element *element, *next;
232 for (element = head->first; element != 0; element = next)
234 next = element->next;
235 element->next = bitmap_free;
236 bitmap_free = element;
239 head->first = head->current = 0;
242 /* Copy a bitmap to another bitmap. */
245 bitmap_copy (to, from)
249 bitmap_element *from_ptr, *to_ptr = 0;
250 #if BITMAP_ELEMENT_WORDS != 2
256 /* Copy elements in forward direction one at a time */
257 for (from_ptr = from->first; from_ptr; from_ptr = from_ptr->next)
259 bitmap_element *to_elt = bitmap_element_allocate ();
261 to_elt->indx = from_ptr->indx;
263 #if BITMAP_ELEMENT_WORDS == 2
264 to_elt->bits[0] = from_ptr->bits[0];
265 to_elt->bits[1] = from_ptr->bits[1];
267 for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
268 to_elt->bits[i] = from_ptr->bits[i];
271 /* Here we have a special case of bitmap_element_link, for the case
272 where we know the links are being entered in sequence. */
275 to->first = to->current = to_elt;
276 to->indx = from_ptr->indx;
277 to_elt->next = to_elt->prev = 0;
281 to_elt->prev = to_ptr;
283 to_ptr->next = to_elt;
290 /* Find a bitmap element that would hold a bitmap's bit.
291 Update the `current' field even if we can't find an element that
292 would hold the bitmap's bit to make eventual allocation
295 static INLINE bitmap_element *
296 bitmap_find_bit (head, bit)
300 bitmap_element *element;
301 unsigned HOST_WIDE_INT indx = bit / BITMAP_ELEMENT_ALL_BITS;
303 if (head->current == 0
304 || head->indx == indx)
305 return head->current;
307 if (head->indx > indx)
308 for (element = head->current;
309 element->prev != 0 && element->indx > indx;
310 element = element->prev)
314 for (element = head->current;
315 element->next != 0 && element->indx < indx;
316 element = element->next)
319 /* `element' is the nearest to the one we want. If it's not the one we
320 want, the one we want doesn't exist. */
321 head->current = element;
322 head->indx = element->indx;
323 if (element != 0 && element->indx != indx)
329 /* Clear a single bit in a bitmap. */
332 bitmap_clear_bit (head, bit)
336 bitmap_element *ptr = bitmap_find_bit (head, bit);
340 unsigned bit_num = bit % (unsigned) HOST_BITS_PER_WIDE_INT;
341 unsigned word_num = ((bit / (unsigned) HOST_BITS_PER_WIDE_INT)
342 % BITMAP_ELEMENT_WORDS);
343 ptr->bits[word_num] &= ~ (((unsigned HOST_WIDE_INT) 1) << bit_num);
345 /* If we cleared the entire word, free up the element */
346 if (bitmap_element_zerop (ptr))
347 bitmap_element_free (head, ptr);
351 /* Set a single bit in a bitmap. */
354 bitmap_set_bit (head, bit)
358 bitmap_element *ptr = bitmap_find_bit (head, bit);
360 = ((bit / (unsigned) HOST_BITS_PER_WIDE_INT) % BITMAP_ELEMENT_WORDS);
361 unsigned bit_num = bit % (unsigned) HOST_BITS_PER_WIDE_INT;
362 unsigned HOST_WIDE_INT bit_val = ((unsigned HOST_WIDE_INT) 1) << bit_num;
366 ptr = bitmap_element_allocate ();
367 ptr->indx = bit / BITMAP_ELEMENT_ALL_BITS;
368 ptr->bits[word_num] = bit_val;
369 bitmap_element_link (head, ptr);
372 ptr->bits[word_num] |= bit_val;
375 /* Return whether a bit is set within a bitmap. */
378 bitmap_bit_p (head, bit)
386 ptr = bitmap_find_bit (head, bit);
390 bit_num = bit % (unsigned) HOST_BITS_PER_WIDE_INT;
392 = ((bit / (unsigned) HOST_BITS_PER_WIDE_INT) % BITMAP_ELEMENT_WORDS);
394 return (ptr->bits[word_num] >> bit_num) & 1;
397 /* Return the bit number of the first set bit in the bitmap, or -1
398 if the bitmap is empty. */
401 bitmap_first_set_bit (a)
404 bitmap_element *ptr = a->first;
405 unsigned HOST_WIDE_INT word;
406 unsigned word_num, bit_num;
411 #if BITMAP_ELEMENT_WORDS == 2
412 word_num = 0, word = ptr->bits[0];
414 word_num = 1, word = ptr->bits[1];
416 for (word_num = 0; word_num < BITMAP_ELEMENT_WORDS; ++word_num)
417 if ((word = ptr->bits[word_num]) != 0)
421 /* Binary search for the first set bit. */
422 /* ??? It'd be nice to know if ffs or ffsl was available. */
427 #if HOST_BITS_PER_WIDE_INT > 64
428 #error "Fill out the table."
430 #if HOST_BITS_PER_WIDE_INT > 32
431 if ((word & 0xffffffff) == 0)
432 word >>= 32, bit_num += 32;
434 if ((word & 0xffff) == 0)
435 word >>= 16, bit_num += 16;
436 if ((word & 0xff) == 0)
437 word >>= 8, bit_num += 8;
445 return (ptr->indx * BITMAP_ELEMENT_ALL_BITS
446 + word_num * HOST_BITS_PER_WIDE_INT
450 /* Return the bit number of the last set bit in the bitmap, or -1
451 if the bitmap is empty. */
454 bitmap_last_set_bit (a)
457 bitmap_element *ptr = a->first;
458 unsigned HOST_WIDE_INT word;
459 unsigned word_num, bit_num;
464 while (ptr->next != NULL)
467 #if BITMAP_ELEMENT_WORDS == 2
468 word_num = 1, word = ptr->bits[1];
470 word_num = 0, word = ptr->bits[0];
472 for (word_num = BITMAP_ELEMENT_WORDS; word_num-- > 0; )
473 if ((word = ptr->bits[word_num]) != 0)
477 /* Binary search for the last set bit. */
480 #if HOST_BITS_PER_WIDE_INT > 64
481 #error "Fill out the table."
483 #if HOST_BITS_PER_WIDE_INT > 32
484 if (word & ~ (unsigned HOST_WIDE_INT) 0xffffffff)
485 word >>= 32, bit_num += 32;
487 if (word & 0xffff0000)
488 word >>= 16, bit_num += 16;
490 word >>= 8, bit_num += 8;
492 word >>= 4, bit_num += 4;
494 word >>= 2, bit_num += 2;
498 return (ptr->indx * BITMAP_ELEMENT_ALL_BITS
499 + word_num * HOST_BITS_PER_WIDE_INT
503 /* Store in bitmap TO the result of combining bitmap FROM1 and FROM2 using
504 a specific bit manipulation. Return true if TO changes. */
507 bitmap_operation (to, from1, from2, operation)
511 enum bitmap_bits operation;
513 #define HIGHEST_INDEX (unsigned int) ~0
515 bitmap_element *from1_ptr = from1->first;
516 bitmap_element *from2_ptr = from2->first;
517 unsigned int indx1 = (from1_ptr) ? from1_ptr->indx : HIGHEST_INDEX;
518 unsigned int indx2 = (from2_ptr) ? from2_ptr->indx : HIGHEST_INDEX;
519 bitmap_element *to_ptr = to->first;
520 bitmap_element *from1_tmp;
521 bitmap_element *from2_tmp;
522 bitmap_element *to_tmp;
526 #if BITMAP_ELEMENT_WORDS == 2
529 unsigned HOST_WIDE_INT t0, t1, f10, f11, f20, f21; \
530 f10 = from1_tmp->bits[0]; \
531 f20 = from2_tmp->bits[0]; \
533 changed |= (t0 != to_tmp->bits[0]); \
534 f11 = from1_tmp->bits[1]; \
535 f21 = from2_tmp->bits[1]; \
537 changed |= (t1 != to_tmp->bits[1]); \
538 to_tmp->bits[0] = t0; \
539 to_tmp->bits[1] = t1; \
544 unsigned HOST_WIDE_INT t, f1, f2; \
546 for (i = 0; i < BITMAP_ELEMENT_WORDS; ++i) \
548 f1 = from1_tmp->bits[i]; \
549 f2 = from2_tmp->bits[i]; \
551 changed |= (t != to_tmp->bits[i]); \
552 to_tmp->bits[i] = t; \
557 to->first = to->current = 0;
559 while (from1_ptr != 0 || from2_ptr != 0)
561 /* Figure out whether we need to substitute zero elements for
566 from1_tmp = from1_ptr;
567 from2_tmp = from2_ptr;
568 from1_ptr = from1_ptr->next;
569 indx1 = (from1_ptr) ? from1_ptr->indx : HIGHEST_INDEX;
570 from2_ptr = from2_ptr->next;
571 indx2 = (from2_ptr) ? from2_ptr->indx : HIGHEST_INDEX;
573 else if (indx1 < indx2)
576 from1_tmp = from1_ptr;
577 from2_tmp = &bitmap_zero_bits;
578 from1_ptr = from1_ptr->next;
579 indx1 = (from1_ptr) ? from1_ptr->indx : HIGHEST_INDEX;
584 from1_tmp = &bitmap_zero_bits;
585 from2_tmp = from2_ptr;
586 from2_ptr = from2_ptr->next;
587 indx2 = (from2_ptr) ? from2_ptr->indx : HIGHEST_INDEX;
590 /* Find the appropriate element from TO. Begin by discarding
591 elements that we've skipped. */
592 while (to_ptr && to_ptr->indx < indx)
596 to_ptr = to_ptr->next;
597 to_tmp->next = bitmap_free;
598 bitmap_free = to_tmp;
600 if (to_ptr && to_ptr->indx == indx)
603 to_ptr = to_ptr->next;
606 to_tmp = bitmap_element_allocate ();
608 /* Do the operation, and if any bits are set, link it into the
619 case BITMAP_AND_COMPL:
626 case BITMAP_IOR_COMPL:
634 if (! bitmap_element_zerop (to_tmp))
637 bitmap_element_link (to, to_tmp);
641 to_tmp->next = bitmap_free;
642 bitmap_free = to_tmp;
646 /* If we have elements of TO left over, free the lot. */
650 for (to_tmp = to_ptr; to_tmp->next ; to_tmp = to_tmp->next)
652 to_tmp->next = bitmap_free;
653 bitmap_free = to_ptr;
661 /* Return true if two bitmaps are identical. */
664 bitmap_equal_p (a, b)
671 c.first = c.current = 0;
672 ret = ! bitmap_operation (&c, a, b, BITMAP_XOR);
678 /* Or into bitmap TO bitmap FROM1 and'ed with the complement of
682 bitmap_ior_and_compl (to, from1, from2)
689 tmp.first = tmp.current = 0;
691 bitmap_operation (&tmp, from1, from2, BITMAP_AND_COMPL);
692 bitmap_operation (to, to, &tmp, BITMAP_IOR);
697 bitmap_union_of_diff (dst, a, b, c)
706 tmp.first = tmp.current = 0;
708 bitmap_operation (&tmp, b, c, BITMAP_AND_COMPL);
709 changed = bitmap_operation (dst, &tmp, a, BITMAP_IOR);
715 /* Initialize a bitmap header. */
718 bitmap_initialize (head)
721 head->first = head->current = 0;
726 /* Debugging function to print out the contents of a bitmap. */
729 debug_bitmap_file (file, head)
735 fprintf (file, "\nfirst = ");
736 fprintf (file, HOST_PTR_PRINTF, (PTR) head->first);
737 fprintf (file, " current = ");
738 fprintf (file, HOST_PTR_PRINTF, (PTR) head->current);
739 fprintf (file, " indx = %u\n", head->indx);
741 for (ptr = head->first; ptr; ptr = ptr->next)
745 fprintf (file, "\t");
746 fprintf (file, HOST_PTR_PRINTF, (PTR) ptr);
747 fprintf (file, " next = ");
748 fprintf (file, HOST_PTR_PRINTF, (PTR) ptr->next);
749 fprintf (file, " prev = ");
750 fprintf (file, HOST_PTR_PRINTF, (PTR) ptr->prev);
751 fprintf (file, " indx = %u\n\t\tbits = {", ptr->indx);
753 for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
754 for (j = 0; j < HOST_BITS_PER_WIDE_INT; j++)
755 if ((ptr->bits[i] >> j) & 1)
759 fprintf (file, "\n\t\t\t");
763 fprintf (file, " %u", (ptr->indx * BITMAP_ELEMENT_ALL_BITS
764 + i * HOST_BITS_PER_WIDE_INT + j));
768 fprintf (file, " }\n");
772 /* Function to be called from the debugger to print the contents
779 debug_bitmap_file (stdout, head);
782 /* Function to print out the contents of a bitmap. Unlike debug_bitmap_file,
783 it does not print anything but the bits. */
786 bitmap_print (file, head, prefix, suffix)
792 const char *comma = "";
795 fputs (prefix, file);
796 EXECUTE_IF_SET_IN_BITMAP (head, 0, i,
798 fprintf (file, "%s%d", comma, i);
801 fputs (suffix, file);