* Makefile.in (rtlanal.o): Depend on $(TM_P_H).
[official-gcc.git] / gcc / bitmap.c
blob33cc535fe0b0e2af5bbfc3a6a04ef52816c29c75
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
9 version.
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
14 for more details.
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
19 02111-1307, USA. */
21 #include "config.h"
22 #include "system.h"
23 #include "rtl.h"
24 #include "flags.h"
25 #include "obstack.h"
26 #include "bitmap.h"
28 /* Obstack to allocate bitmap elements from. */
29 static struct obstack bitmap_obstack;
30 static int bitmap_obstack_init = FALSE;
32 #ifndef INLINE
33 #ifndef __GNUC__
34 #define INLINE
35 #else
36 #define INLINE __inline__
37 #endif
38 #endif
40 /* Global data */
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". */
53 static INLINE void
54 bitmap_element_free (head, elt)
55 bitmap head;
56 bitmap_element *elt;
58 bitmap_element *next = elt->next;
59 bitmap_element *prev = elt->prev;
61 if (prev)
62 prev->next = next;
64 if (next)
65 next->prev = prev;
67 if (head->first == elt)
68 head->first = next;
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;
76 bitmap_free = elt;
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;
86 if (bitmap_free != 0)
88 element = bitmap_free;
89 bitmap_free = element->next;
91 else
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
103 #endif
104 /* Let them override the alloc and free routines too. */
105 #ifndef OBSTACK_CHUNK_ALLOC
106 #define OBSTACK_CHUNK_ALLOC xmalloc
107 #endif
108 #ifndef OBSTACK_CHUNK_FREE
109 #define OBSTACK_CHUNK_FREE free
110 #endif
112 #if !defined(__GNUC__) || (__GNUC__ < 2)
113 #define __alignof__(type) 0
114 #endif
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));
128 return element;
131 /* Release any memory allocated by bitmaps. */
133 void
134 bitmap_release_memory ()
136 bitmap_free = 0;
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. */
146 static INLINE int
147 bitmap_element_zerop (element)
148 bitmap_element *element;
150 #if BITMAP_ELEMENT_WORDS == 2
151 return (element->bits[0] | element->bits[1]) == 0;
152 #else
153 int i;
155 for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
156 if (element->bits[i] != 0)
157 return 0;
159 return 1;
160 #endif
163 /* Link the bitmap element into the current bitmap linked list. */
165 static INLINE void
166 bitmap_element_link (head, element)
167 bitmap head;
168 bitmap_element *element;
170 unsigned int indx = element->indx;
171 bitmap_element *ptr;
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;
186 ptr = ptr->prev)
189 if (ptr->prev)
190 ptr->prev->next = element;
191 else
192 head->first = element;
194 element->prev = ptr->prev;
195 element->next = ptr;
196 ptr->prev = element;
199 /* Otherwise, it must go someplace after the current element. */
200 else
202 for (ptr = head->current;
203 ptr->next != 0 && ptr->next->indx < indx;
204 ptr = ptr->next)
207 if (ptr->next)
208 ptr->next->prev = element;
210 element->next = ptr->next;
211 element->prev = ptr;
212 ptr->next = element;
215 /* Set up so this is the first element searched. */
216 head->current = element;
217 head->indx = indx;
220 /* Clear a bitmap by freeing the linked list. */
222 INLINE void
223 bitmap_clear (head)
224 bitmap head;
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. */
240 void
241 bitmap_copy (to, from)
242 bitmap to;
243 bitmap from;
245 bitmap_element *from_ptr, *to_ptr = 0;
246 #if BITMAP_ELEMENT_WORDS != 2
247 int i;
248 #endif
250 bitmap_clear (to);
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];
262 #else
263 for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
264 to_elt->bits[i] = from_ptr->bits[i];
265 #endif
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. */
269 if (to_ptr == 0)
271 to->first = to->current = to_elt;
272 to->indx = from_ptr->indx;
273 to_elt->next = to_elt->prev = 0;
275 else
277 to_elt->prev = to_ptr;
278 to_elt->next = 0;
279 to_ptr->next = to_elt;
282 to_ptr = 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
289 faster. */
291 static INLINE bitmap_element *
292 bitmap_find_bit (head, bit)
293 bitmap head;
294 unsigned int bit;
296 bitmap_element *element;
297 unsigned HOST_WIDE_INT indx = bit / BITMAP_ELEMENT_ALL_BITS;
299 if (head->current == 0)
300 return 0;
302 if (head->indx > indx)
303 for (element = head->current;
304 element->prev != 0 && element->indx > indx;
305 element = element->prev)
308 else
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)
319 element = 0;
321 return element;
324 /* Clear a single bit in a bitmap. */
326 void
327 bitmap_clear_bit (head, bit)
328 bitmap head;
329 int bit;
331 bitmap_element *ptr = bitmap_find_bit (head, bit);
333 if (ptr != 0)
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. */
348 void
349 bitmap_set_bit (head, bit)
350 bitmap head;
351 int bit;
353 bitmap_element *ptr = bitmap_find_bit (head, bit);
354 unsigned word_num
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;
359 if (ptr == 0)
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);
366 else
367 ptr->bits[word_num] |= bit_val;
370 /* Return whether a bit is set within a bitmap. */
373 bitmap_bit_p (head, bit)
374 bitmap head;
375 int bit;
377 bitmap_element *ptr;
378 unsigned bit_num;
379 unsigned word_num;
381 ptr = bitmap_find_bit (head, bit);
382 if (ptr == 0)
383 return 0;
385 bit_num = bit % (unsigned) HOST_BITS_PER_WIDE_INT;
386 word_num
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. */
395 int
396 bitmap_first_set_bit (a)
397 bitmap a;
399 bitmap_element *ptr = a->first;
400 unsigned HOST_WIDE_INT word;
401 unsigned word_num, bit_num;
403 if (ptr == NULL)
404 return -1;
406 #if BITMAP_ELEMENT_WORDS == 2
407 word_num = 0, word = ptr->bits[0];
408 if (word == 0)
409 word_num = 1, word = ptr->bits[1];
410 #else
411 for (word_num = 0; word_num < BITMAP_ELEMENT_WORDS; ++word_num)
412 if ((word = ptr->bits[word_num]) != 0)
413 break;
414 #endif
416 /* Binary search for the first set bit. */
417 /* ??? It'd be nice to know if ffs or ffsl was available. */
419 bit_num = 0;
420 word = word & -word;
422 #if HOST_BITS_PER_WIDE_INT > 64
423 #error "Fill out the table."
424 #endif
425 #if HOST_BITS_PER_WIDE_INT > 32
426 if ((word & 0xffffffff) == 0)
427 word >>= 32, bit_num += 32;
428 #endif
429 if ((word & 0xffff) == 0)
430 word >>= 16, bit_num += 16;
431 if ((word & 0xff) == 0)
432 word >>= 8, bit_num += 8;
433 if (word & 0xf0)
434 bit_num += 4;
435 if (word & 0xcc)
436 bit_num += 2;
437 if (word & 0xaa)
438 bit_num += 1;
440 return (ptr->indx * BITMAP_ELEMENT_ALL_BITS
441 + word_num * HOST_BITS_PER_WIDE_INT
442 + bit_num);
445 /* Return the bit number of the last set bit in the bitmap, or -1
446 if the bitmap is empty. */
448 int
449 bitmap_last_set_bit (a)
450 bitmap a;
452 bitmap_element *ptr = a->first;
453 unsigned HOST_WIDE_INT word;
454 unsigned word_num, bit_num;
456 if (ptr == NULL)
457 return -1;
459 while (ptr->next != NULL)
460 ptr = ptr->next;
462 #if BITMAP_ELEMENT_WORDS == 2
463 word_num = 1, word = ptr->bits[1];
464 if (word == 0)
465 word_num = 0, word = ptr->bits[0];
466 #else
467 for (word_num = BITMAP_ELEMENT_WORDS; word_num-- > 0; )
468 if ((word = ptr->bits[word_num]) != 0)
469 break;
470 #endif
472 /* Binary search for the last set bit. */
474 bit_num = 0;
475 #if HOST_BITS_PER_WIDE_INT > 64
476 #error "Fill out the table."
477 #endif
478 #if HOST_BITS_PER_WIDE_INT > 32
479 if (word & ~ (unsigned HOST_WIDE_INT) 0xffffffff)
480 word >>= 32, bit_num += 32;
481 #endif
482 if (word & 0xffff0000)
483 word >>= 16, bit_num += 16;
484 if (word & 0xff00)
485 word >>= 8, bit_num += 8;
486 if (word & 0xf0)
487 word >>= 4, bit_num += 4;
488 if (word & 0xc)
489 word >>= 2, bit_num += 2;
490 if (word & 0x2)
491 bit_num += 1;
493 return (ptr->indx * BITMAP_ELEMENT_ALL_BITS
494 + word_num * HOST_BITS_PER_WIDE_INT
495 + bit_num);
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)
503 bitmap to;
504 bitmap from1;
505 bitmap from2;
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;
518 unsigned int indx;
519 int changed = 0;
521 #if BITMAP_ELEMENT_WORDS == 2
522 #define DOIT(OP) \
523 do { \
524 unsigned HOST_WIDE_INT t0, t1, f10, f11, f20, f21; \
525 f10 = from1_tmp->bits[0]; \
526 f20 = from2_tmp->bits[0]; \
527 t0 = f10 OP f20; \
528 changed |= (t0 != to_tmp->bits[0]); \
529 f11 = from1_tmp->bits[1]; \
530 f21 = from2_tmp->bits[1]; \
531 t1 = f11 OP f21; \
532 changed |= (t1 != to_tmp->bits[1]); \
533 to_tmp->bits[0] = t0; \
534 to_tmp->bits[1] = t1; \
535 } while (0)
536 #else
537 #define DOIT(OP) \
538 do { \
539 unsigned HOST_WIDE_INT t, f1, f2; \
540 int i; \
541 for (i = 0; i < BITMAP_ELEMENT_WORDS; ++i) \
543 f1 = from1_tmp->bits[i]; \
544 f2 = from2_tmp->bits[i]; \
545 t = f1 OP f2; \
546 changed |= (t != to_tmp->bits[i]); \
547 to_tmp->bits[i] = t; \
549 } while (0)
550 #endif
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
557 missing links. */
558 if (indx1 == indx2)
560 indx = indx1;
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)
570 indx = indx1;
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;
576 else
578 indx = indx2;
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)
589 changed = 1;
590 to_tmp = to_ptr;
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)
597 to_tmp = to_ptr;
598 to_ptr = to_ptr->next;
600 else
601 to_tmp = bitmap_element_allocate ();
603 /* Do the operation, and if any bits are set, link it into the
604 linked list. */
605 switch (operation)
607 default:
608 abort ();
610 case BITMAP_AND:
611 DOIT (&);
612 break;
614 case BITMAP_AND_COMPL:
615 DOIT (&~);
616 break;
618 case BITMAP_IOR:
619 DOIT (|);
620 break;
621 case BITMAP_IOR_COMPL:
622 DOIT (|~);
623 break;
624 case BITMAP_XOR:
625 DOIT (^);
626 break;
629 if (! bitmap_element_zerop (to_tmp))
631 to_tmp->indx = indx;
632 bitmap_element_link (to, to_tmp);
634 else
636 to_tmp->next = bitmap_free;
637 bitmap_free = to_tmp;
641 /* If we have elements of TO left over, free the lot. */
642 if (to_ptr)
644 changed = 1;
645 for (to_tmp = to_ptr; to_tmp->next ; to_tmp = to_tmp->next)
646 continue;
647 to_tmp->next = bitmap_free;
648 bitmap_free = to_ptr;
651 #undef DOIT
653 return changed;
656 /* Return true if two bitmaps are identical. */
659 bitmap_equal_p (a, b)
660 bitmap a;
661 bitmap b;
663 bitmap_head c;
664 int ret;
666 c.first = c.current = 0;
667 ret = ! bitmap_operation (&c, a, b, BITMAP_XOR);
668 bitmap_clear (&c);
670 return ret;
673 /* Or into bitmap TO bitmap FROM1 and'ed with the complement of
674 bitmap FROM2. */
676 void
677 bitmap_ior_and_compl (to, from1, from2)
678 bitmap to;
679 bitmap from1;
680 bitmap from2;
682 bitmap_head tmp;
684 tmp.first = tmp.current = 0;
686 bitmap_operation (&tmp, from1, from2, BITMAP_AND_COMPL);
687 bitmap_operation (to, to, &tmp, BITMAP_IOR);
688 bitmap_clear (&tmp);
692 bitmap_union_of_diff (dst, a, b, c)
693 bitmap dst;
694 bitmap a;
695 bitmap b;
696 bitmap c;
698 bitmap_head tmp;
699 int changed;
701 tmp.first = tmp.current = 0;
703 bitmap_operation (&tmp, b, c, BITMAP_AND_COMPL);
704 changed = bitmap_operation (dst, &tmp, a, BITMAP_IOR);
705 bitmap_clear (&tmp);
707 return changed;
710 /* Initialize a bitmap header. */
712 bitmap
713 bitmap_initialize (head)
714 bitmap head;
716 head->first = head->current = 0;
718 return head;
721 /* Debugging function to print out the contents of a bitmap. */
723 void
724 debug_bitmap_file (file, head)
725 FILE *file;
726 bitmap head;
728 bitmap_element *ptr;
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)
738 int i, j, col = 26;
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)
752 if (col > 70)
754 fprintf (file, "\n\t\t\t");
755 col = 24;
758 fprintf (file, " %u", (ptr->indx * BITMAP_ELEMENT_ALL_BITS
759 + i * HOST_BITS_PER_WIDE_INT + j));
760 col += 4;
763 fprintf (file, " }\n");
767 /* Function to be called from the debugger to print the contents
768 of a bitmap. */
770 void
771 debug_bitmap (head)
772 bitmap head;
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. */
780 void
781 bitmap_print (file, head, prefix, suffix)
782 FILE *file;
783 bitmap head;
784 const char *prefix;
785 const char *suffix;
787 const char *comma = "";
788 int i;
790 fputs (prefix, file);
791 EXECUTE_IF_SET_IN_BITMAP (head, 0, i,
793 fprintf (file, "%s%d", comma, i);
794 comma = ", ";
796 fputs (suffix, file);