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 GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
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)
73 head->current = next != 0 ? next : prev;
75 elt->next = bitmap_free;
79 /* Allocate a bitmap element. The bits are cleared, but nothing else is. */
81 static INLINE bitmap_element *
82 bitmap_element_allocate ()
84 bitmap_element *element;
88 element = bitmap_free;
89 bitmap_free = element->next;
93 /* We can't use gcc_obstack_init to initialize the obstack since
94 print-rtl.c now calls bitmap functions, and bitmap is linked
95 into the gen* functions. */
96 if (!bitmap_obstack_init)
98 bitmap_obstack_init = TRUE;
100 /* Let particular systems override the size of a chunk. */
101 #ifndef OBSTACK_CHUNK_SIZE
102 #define OBSTACK_CHUNK_SIZE 0
104 /* Let them override the alloc and free routines too. */
105 #ifndef OBSTACK_CHUNK_ALLOC
106 #define OBSTACK_CHUNK_ALLOC xmalloc
108 #ifndef OBSTACK_CHUNK_FREE
109 #define OBSTACK_CHUNK_FREE free
112 #if !defined(__GNUC__) || (__GNUC__ < 2)
113 #define __alignof__(type) 0
116 obstack_specify_allocation (&bitmap_obstack, OBSTACK_CHUNK_SIZE,
117 __alignof__ (bitmap_element),
118 (void *(*) PARAMS ((long))) OBSTACK_CHUNK_ALLOC,
119 (void (*) PARAMS ((void *))) OBSTACK_CHUNK_FREE);
122 element = (bitmap_element *) obstack_alloc (&bitmap_obstack,
123 sizeof (bitmap_element));
126 memset (element->bits, 0, sizeof (element->bits));
131 /* Release any memory allocated by bitmaps. */
134 bitmap_release_memory ()
137 if (bitmap_obstack_init)
139 bitmap_obstack_init = FALSE;
140 obstack_free (&bitmap_obstack, NULL);
144 /* Return nonzero if all bits in an element are zero. */
147 bitmap_element_zerop (element)
148 bitmap_element *element;
150 #if BITMAP_ELEMENT_WORDS == 2
151 return (element->bits[0] | element->bits[1]) == 0;
155 for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
156 if (element->bits[i] != 0)
163 /* Link the bitmap element into the current bitmap linked list. */
166 bitmap_element_link (head, element)
168 bitmap_element *element;
170 unsigned int indx = element->indx;
173 /* If this is the first and only element, set it in. */
174 if (head->first == 0)
176 element->next = element->prev = 0;
177 head->first = element;
180 /* If this index is less than that of the current element, it goes someplace
181 before the current element. */
182 else if (indx < head->indx)
184 for (ptr = head->current;
185 ptr->prev != 0 && ptr->prev->indx > indx;
190 ptr->prev->next = element;
192 head->first = element;
194 element->prev = ptr->prev;
199 /* Otherwise, it must go someplace after the current element. */
202 for (ptr = head->current;
203 ptr->next != 0 && ptr->next->indx < indx;
208 ptr->next->prev = element;
210 element->next = ptr->next;
215 /* Set up so this is the first element searched. */
216 head->current = element;
220 /* Clear a bitmap by freeing the linked list. */
226 bitmap_element *element, *next;
228 for (element = head->first; element != 0; element = next)
230 next = element->next;
231 element->next = bitmap_free;
232 bitmap_free = element;
235 head->first = head->current = 0;
238 /* Copy a bitmap to another bitmap. */
241 bitmap_copy (to, from)
245 bitmap_element *from_ptr, *to_ptr = 0;
246 #if BITMAP_ELEMENT_WORDS != 2
252 /* Copy elements in forward direction one at a time */
253 for (from_ptr = from->first; from_ptr; from_ptr = from_ptr->next)
255 bitmap_element *to_elt = bitmap_element_allocate ();
257 to_elt->indx = from_ptr->indx;
259 #if BITMAP_ELEMENT_WORDS == 2
260 to_elt->bits[0] = from_ptr->bits[0];
261 to_elt->bits[1] = from_ptr->bits[1];
263 for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
264 to_elt->bits[i] = from_ptr->bits[i];
267 /* Here we have a special case of bitmap_element_link, for the case
268 where we know the links are being entered in sequence. */
271 to->first = to->current = to_elt;
272 to->indx = from_ptr->indx;
273 to_elt->next = to_elt->prev = 0;
277 to_elt->prev = to_ptr;
279 to_ptr->next = to_elt;
286 /* Find a bitmap element that would hold a bitmap's bit.
287 Update the `current' field even if we can't find an element that
288 would hold the bitmap's bit to make eventual allocation
291 static INLINE bitmap_element *
292 bitmap_find_bit (head, bit)
296 bitmap_element *element;
297 unsigned HOST_WIDE_INT indx = bit / BITMAP_ELEMENT_ALL_BITS;
299 if (head->current == 0)
302 if (head->indx > indx)
303 for (element = head->current;
304 element->prev != 0 && element->indx > indx;
305 element = element->prev)
309 for (element = head->current;
310 element->next != 0 && element->indx < indx;
311 element = element->next)
314 /* `element' is the nearest to the one we want. If it's not the one we
315 want, the one we want doesn't exist. */
316 head->current = element;
317 head->indx = element->indx;
318 if (element != 0 && element->indx != indx)
324 /* Clear a single bit in a bitmap. */
327 bitmap_clear_bit (head, bit)
331 bitmap_element *ptr = bitmap_find_bit (head, bit);
335 unsigned bit_num = bit % (unsigned) HOST_BITS_PER_WIDE_INT;
336 unsigned word_num = ((bit / (unsigned) HOST_BITS_PER_WIDE_INT)
337 % BITMAP_ELEMENT_WORDS);
338 ptr->bits[word_num] &= ~ (((unsigned HOST_WIDE_INT) 1) << bit_num);
340 /* If we cleared the entire word, free up the element */
341 if (bitmap_element_zerop (ptr))
342 bitmap_element_free (head, ptr);
346 /* Set a single bit in a bitmap. */
349 bitmap_set_bit (head, bit)
353 bitmap_element *ptr = bitmap_find_bit (head, bit);
355 = ((bit / (unsigned) HOST_BITS_PER_WIDE_INT) % BITMAP_ELEMENT_WORDS);
356 unsigned bit_num = bit % (unsigned) HOST_BITS_PER_WIDE_INT;
357 unsigned HOST_WIDE_INT bit_val = ((unsigned HOST_WIDE_INT) 1) << bit_num;
361 ptr = bitmap_element_allocate ();
362 ptr->indx = bit / BITMAP_ELEMENT_ALL_BITS;
363 ptr->bits[word_num] = bit_val;
364 bitmap_element_link (head, ptr);
367 ptr->bits[word_num] |= bit_val;
370 /* Return whether a bit is set within a bitmap. */
373 bitmap_bit_p (head, bit)
381 ptr = bitmap_find_bit (head, bit);
385 bit_num = bit % (unsigned) HOST_BITS_PER_WIDE_INT;
387 = ((bit / (unsigned) HOST_BITS_PER_WIDE_INT) % BITMAP_ELEMENT_WORDS);
389 return (ptr->bits[word_num] >> bit_num) & 1;
392 /* Return the bit number of the first set bit in the bitmap, or -1
393 if the bitmap is empty. */
396 bitmap_first_set_bit (a)
399 bitmap_element *ptr = a->first;
400 unsigned HOST_WIDE_INT word;
401 unsigned word_num, bit_num;
406 #if BITMAP_ELEMENT_WORDS == 2
407 word_num = 0, word = ptr->bits[0];
409 word_num = 1, word = ptr->bits[1];
411 for (word_num = 0; word_num < BITMAP_ELEMENT_WORDS; ++word_num)
412 if ((word = ptr->bits[word_num]) != 0)
416 /* Binary search for the first set bit. */
417 /* ??? It'd be nice to know if ffs or ffsl was available. */
422 #if HOST_BITS_PER_WIDE_INT > 64
423 #error "Fill out the table."
425 #if HOST_BITS_PER_WIDE_INT > 32
426 if ((word & 0xffffffff) == 0)
427 word >>= 32, bit_num += 32;
429 if ((word & 0xffff) == 0)
430 word >>= 16, bit_num += 16;
431 if ((word & 0xff) == 0)
432 word >>= 8, bit_num += 8;
440 return (ptr->indx * BITMAP_ELEMENT_ALL_BITS
441 + word_num * HOST_BITS_PER_WIDE_INT
445 /* Return the bit number of the last set bit in the bitmap, or -1
446 if the bitmap is empty. */
449 bitmap_last_set_bit (a)
452 bitmap_element *ptr = a->first;
453 unsigned HOST_WIDE_INT word;
454 unsigned word_num, bit_num;
459 while (ptr->next != NULL)
462 #if BITMAP_ELEMENT_WORDS == 2
463 word_num = 1, word = ptr->bits[1];
465 word_num = 0, word = ptr->bits[0];
467 for (word_num = BITMAP_ELEMENT_WORDS; word_num-- > 0; )
468 if ((word = ptr->bits[word_num]) != 0)
472 /* Binary search for the last set bit. */
475 #if HOST_BITS_PER_WIDE_INT > 64
476 #error "Fill out the table."
478 #if HOST_BITS_PER_WIDE_INT > 32
479 if (word & ~ (unsigned HOST_WIDE_INT) 0xffffffff)
480 word >>= 32, bit_num += 32;
482 if (word & 0xffff0000)
483 word >>= 16, bit_num += 16;
485 word >>= 8, bit_num += 8;
487 word >>= 4, bit_num += 4;
489 word >>= 2, bit_num += 2;
493 return (ptr->indx * BITMAP_ELEMENT_ALL_BITS
494 + word_num * HOST_BITS_PER_WIDE_INT
498 /* Store in bitmap TO the result of combining bitmap FROM1 and FROM2 using
499 a specific bit manipulation. Return true if TO changes. */
502 bitmap_operation (to, from1, from2, operation)
506 enum bitmap_bits operation;
508 #define HIGHEST_INDEX (unsigned int) ~0
510 bitmap_element *from1_ptr = from1->first;
511 bitmap_element *from2_ptr = from2->first;
512 unsigned int indx1 = (from1_ptr) ? from1_ptr->indx : HIGHEST_INDEX;
513 unsigned int indx2 = (from2_ptr) ? from2_ptr->indx : HIGHEST_INDEX;
514 bitmap_element *to_ptr = to->first;
515 bitmap_element *from1_tmp;
516 bitmap_element *from2_tmp;
517 bitmap_element *to_tmp;
521 #if BITMAP_ELEMENT_WORDS == 2
524 unsigned HOST_WIDE_INT t0, t1, f10, f11, f20, f21; \
525 f10 = from1_tmp->bits[0]; \
526 f20 = from2_tmp->bits[0]; \
528 changed |= (t0 != to_tmp->bits[0]); \
529 f11 = from1_tmp->bits[1]; \
530 f21 = from2_tmp->bits[1]; \
532 changed |= (t1 != to_tmp->bits[1]); \
533 to_tmp->bits[0] = t0; \
534 to_tmp->bits[1] = t1; \
539 unsigned HOST_WIDE_INT t, f1, f2; \
541 for (i = 0; i < BITMAP_ELEMENT_WORDS; ++i) \
543 f1 = from1_tmp->bits[i]; \
544 f2 = from2_tmp->bits[i]; \
546 changed |= (t != to_tmp->bits[i]); \
547 to_tmp->bits[i] = t; \
552 to->first = to->current = 0;
554 while (from1_ptr != 0 || from2_ptr != 0)
556 /* Figure out whether we need to substitute zero elements for
561 from1_tmp = from1_ptr;
562 from2_tmp = from2_ptr;
563 from1_ptr = from1_ptr->next;
564 indx1 = (from1_ptr) ? from1_ptr->indx : HIGHEST_INDEX;
565 from2_ptr = from2_ptr->next;
566 indx2 = (from2_ptr) ? from2_ptr->indx : HIGHEST_INDEX;
568 else if (indx1 < indx2)
571 from1_tmp = from1_ptr;
572 from2_tmp = &bitmap_zero_bits;
573 from1_ptr = from1_ptr->next;
574 indx1 = (from1_ptr) ? from1_ptr->indx : HIGHEST_INDEX;
579 from1_tmp = &bitmap_zero_bits;
580 from2_tmp = from2_ptr;
581 from2_ptr = from2_ptr->next;
582 indx2 = (from2_ptr) ? from2_ptr->indx : HIGHEST_INDEX;
585 /* Find the appropriate element from TO. Begin by discarding
586 elements that we've skipped. */
587 while (to_ptr && to_ptr->indx < indx)
591 to_ptr = to_ptr->next;
592 to_tmp->next = bitmap_free;
593 bitmap_free = to_tmp;
595 if (to_ptr && to_ptr->indx == indx)
598 to_ptr = to_ptr->next;
601 to_tmp = bitmap_element_allocate ();
603 /* Do the operation, and if any bits are set, link it into the
614 case BITMAP_AND_COMPL:
621 case BITMAP_IOR_COMPL:
629 if (! bitmap_element_zerop (to_tmp))
632 bitmap_element_link (to, to_tmp);
636 to_tmp->next = bitmap_free;
637 bitmap_free = to_tmp;
641 /* If we have elements of TO left over, free the lot. */
645 for (to_tmp = to_ptr; to_tmp->next ; to_tmp = to_tmp->next)
647 to_tmp->next = bitmap_free;
648 bitmap_free = to_ptr;
656 /* Return true if two bitmaps are identical. */
659 bitmap_equal_p (a, b)
666 c.first = c.current = 0;
667 ret = ! bitmap_operation (&c, a, b, BITMAP_XOR);
673 /* Or into bitmap TO bitmap FROM1 and'ed with the complement of
677 bitmap_ior_and_compl (to, from1, from2)
684 tmp.first = tmp.current = 0;
686 bitmap_operation (&tmp, from1, from2, BITMAP_AND_COMPL);
687 bitmap_operation (to, to, &tmp, BITMAP_IOR);
692 bitmap_union_of_diff (dst, a, b, c)
701 tmp.first = tmp.current = 0;
703 bitmap_operation (&tmp, b, c, BITMAP_AND_COMPL);
704 changed = bitmap_operation (dst, &tmp, a, BITMAP_IOR);
710 /* Initialize a bitmap header. */
713 bitmap_initialize (head)
716 head->first = head->current = 0;
721 /* Debugging function to print out the contents of a bitmap. */
724 debug_bitmap_file (file, head)
730 fprintf (file, "\nfirst = ");
731 fprintf (file, HOST_PTR_PRINTF, (PTR) head->first);
732 fprintf (file, " current = ");
733 fprintf (file, HOST_PTR_PRINTF, (PTR) head->current);
734 fprintf (file, " indx = %u\n", head->indx);
736 for (ptr = head->first; ptr; ptr = ptr->next)
740 fprintf (file, "\t");
741 fprintf (file, HOST_PTR_PRINTF, (PTR) ptr);
742 fprintf (file, " next = ");
743 fprintf (file, HOST_PTR_PRINTF, (PTR) ptr->next);
744 fprintf (file, " prev = ");
745 fprintf (file, HOST_PTR_PRINTF, (PTR) ptr->prev);
746 fprintf (file, " indx = %u\n\t\tbits = {", ptr->indx);
748 for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
749 for (j = 0; j < HOST_BITS_PER_WIDE_INT; j++)
750 if ((ptr->bits[i] >> j) & 1)
754 fprintf (file, "\n\t\t\t");
758 fprintf (file, " %u", (ptr->indx * BITMAP_ELEMENT_ALL_BITS
759 + i * HOST_BITS_PER_WIDE_INT + j));
763 fprintf (file, " }\n");
767 /* Function to be called from the debugger to print the contents
774 debug_bitmap_file (stdout, head);
777 /* Function to print out the contents of a bitmap. Unlike debug_bitmap_file,
778 it does not print anything but the bits. */
781 bitmap_print (file, head, prefix, suffix)
787 const char *comma = "";
790 fputs (prefix, file);
791 EXECUTE_IF_SET_IN_BITMAP (head, 0, i,
793 fprintf (file, "%s%d", comma, i);
796 fputs (suffix, file);