* predict.c (estimate_bb_frequencies): Correctly set
[official-gcc.git] / gcc / bitmap.c
blob4575f62a531614d58778422207c20b9a229e9599
1 /* Functions to support general ended bitmaps.
2 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2003
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 PARAMS ((bitmap, bitmap_element *));
50 static void bitmap_element_free PARAMS ((bitmap, bitmap_element *));
51 static bitmap_element *bitmap_element_allocate PARAMS ((bitmap));
52 static int bitmap_element_zerop PARAMS ((bitmap_element *));
53 static void bitmap_element_link PARAMS ((bitmap, bitmap_element *));
54 static bitmap_element *bitmap_find_bit PARAMS ((bitmap, unsigned int));
56 /* Add ELEM to the appropriate freelist. */
57 static INLINE void
58 bitmap_elem_to_freelist (head, elt)
59 bitmap head;
60 bitmap_element *elt;
62 if (head->using_obstack)
64 elt->next = bitmap_free;
65 bitmap_free = elt;
67 else
69 elt->next = bitmap_ggc_free;
70 bitmap_ggc_free = elt;
74 /* Free a bitmap element. Since these are allocated off the
75 bitmap_obstack, "free" actually means "put onto the freelist". */
77 static INLINE void
78 bitmap_element_free (head, elt)
79 bitmap head;
80 bitmap_element *elt;
82 bitmap_element *next = elt->next;
83 bitmap_element *prev = elt->prev;
85 if (prev)
86 prev->next = next;
88 if (next)
89 next->prev = prev;
91 if (head->first == elt)
92 head->first = next;
94 /* Since the first thing we try is to insert before current,
95 make current the next entry in preference to the previous. */
96 if (head->current == elt)
98 head->current = next != 0 ? next : prev;
99 if (head->current)
100 head->indx = head->current->indx;
102 bitmap_elem_to_freelist (head, elt);
105 /* Allocate a bitmap element. The bits are cleared, but nothing else is. */
107 static INLINE bitmap_element *
108 bitmap_element_allocate (head)
109 bitmap head;
111 bitmap_element *element;
113 if (head->using_obstack)
115 if (bitmap_free != 0)
117 element = bitmap_free;
118 bitmap_free = element->next;
120 else
122 /* We can't use gcc_obstack_init to initialize the obstack since
123 print-rtl.c now calls bitmap functions, and bitmap is linked
124 into the gen* functions. */
125 if (!bitmap_obstack_init)
127 bitmap_obstack_init = TRUE;
129 /* Let particular systems override the size of a chunk. */
130 #ifndef OBSTACK_CHUNK_SIZE
131 #define OBSTACK_CHUNK_SIZE 0
132 #endif
133 /* Let them override the alloc and free routines too. */
134 #ifndef OBSTACK_CHUNK_ALLOC
135 #define OBSTACK_CHUNK_ALLOC xmalloc
136 #endif
137 #ifndef OBSTACK_CHUNK_FREE
138 #define OBSTACK_CHUNK_FREE free
139 #endif
141 #if !defined(__GNUC__) || (__GNUC__ < 2)
142 #define __alignof__(type) 0
143 #endif
145 obstack_specify_allocation (&bitmap_obstack, OBSTACK_CHUNK_SIZE,
146 __alignof__ (bitmap_element),
147 (void *(*) PARAMS ((long))) OBSTACK_CHUNK_ALLOC,
148 (void (*) PARAMS ((void *))) OBSTACK_CHUNK_FREE);
151 element = (bitmap_element *) obstack_alloc (&bitmap_obstack,
152 sizeof (bitmap_element));
155 else
157 if (bitmap_ggc_free != NULL)
159 element = bitmap_ggc_free;
160 bitmap_ggc_free = element->next;
162 else
163 element = ggc_alloc (sizeof (bitmap_element));
166 memset (element->bits, 0, sizeof (element->bits));
168 return element;
171 /* Release any memory allocated by bitmaps. */
173 void
174 bitmap_release_memory ()
176 bitmap_free = 0;
177 if (bitmap_obstack_init)
179 bitmap_obstack_init = FALSE;
180 obstack_free (&bitmap_obstack, NULL);
184 /* Return nonzero if all bits in an element are zero. */
186 static INLINE int
187 bitmap_element_zerop (element)
188 bitmap_element *element;
190 #if BITMAP_ELEMENT_WORDS == 2
191 return (element->bits[0] | element->bits[1]) == 0;
192 #else
193 int i;
195 for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
196 if (element->bits[i] != 0)
197 return 0;
199 return 1;
200 #endif
203 /* Link the bitmap element into the current bitmap linked list. */
205 static INLINE void
206 bitmap_element_link (head, element)
207 bitmap head;
208 bitmap_element *element;
210 unsigned int indx = element->indx;
211 bitmap_element *ptr;
213 /* If this is the first and only element, set it in. */
214 if (head->first == 0)
216 element->next = element->prev = 0;
217 head->first = element;
220 /* If this index is less than that of the current element, it goes someplace
221 before the current element. */
222 else if (indx < head->indx)
224 for (ptr = head->current;
225 ptr->prev != 0 && ptr->prev->indx > indx;
226 ptr = ptr->prev)
229 if (ptr->prev)
230 ptr->prev->next = element;
231 else
232 head->first = element;
234 element->prev = ptr->prev;
235 element->next = ptr;
236 ptr->prev = element;
239 /* Otherwise, it must go someplace after the current element. */
240 else
242 for (ptr = head->current;
243 ptr->next != 0 && ptr->next->indx < indx;
244 ptr = ptr->next)
247 if (ptr->next)
248 ptr->next->prev = element;
250 element->next = ptr->next;
251 element->prev = ptr;
252 ptr->next = element;
255 /* Set up so this is the first element searched. */
256 head->current = element;
257 head->indx = indx;
260 /* Clear a bitmap by freeing the linked list. */
262 INLINE void
263 bitmap_clear (head)
264 bitmap head;
266 bitmap_element *element, *next;
268 for (element = head->first; element != 0; element = next)
270 next = element->next;
271 bitmap_elem_to_freelist (head, element);
274 head->first = head->current = 0;
277 /* Copy a bitmap to another bitmap. */
279 void
280 bitmap_copy (to, from)
281 bitmap to;
282 bitmap from;
284 bitmap_element *from_ptr, *to_ptr = 0;
285 #if BITMAP_ELEMENT_WORDS != 2
286 int i;
287 #endif
289 bitmap_clear (to);
291 /* Copy elements in forward direction one at a time */
292 for (from_ptr = from->first; from_ptr; from_ptr = from_ptr->next)
294 bitmap_element *to_elt = bitmap_element_allocate (to);
296 to_elt->indx = from_ptr->indx;
298 #if BITMAP_ELEMENT_WORDS == 2
299 to_elt->bits[0] = from_ptr->bits[0];
300 to_elt->bits[1] = from_ptr->bits[1];
301 #else
302 for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
303 to_elt->bits[i] = from_ptr->bits[i];
304 #endif
306 /* Here we have a special case of bitmap_element_link, for the case
307 where we know the links are being entered in sequence. */
308 if (to_ptr == 0)
310 to->first = to->current = to_elt;
311 to->indx = from_ptr->indx;
312 to_elt->next = to_elt->prev = 0;
314 else
316 to_elt->prev = to_ptr;
317 to_elt->next = 0;
318 to_ptr->next = to_elt;
321 to_ptr = to_elt;
325 /* Find a bitmap element that would hold a bitmap's bit.
326 Update the `current' field even if we can't find an element that
327 would hold the bitmap's bit to make eventual allocation
328 faster. */
330 static INLINE bitmap_element *
331 bitmap_find_bit (head, bit)
332 bitmap head;
333 unsigned int bit;
335 bitmap_element *element;
336 unsigned int indx = bit / BITMAP_ELEMENT_ALL_BITS;
338 if (head->current == 0
339 || head->indx == indx)
340 return head->current;
342 if (head->indx > indx)
343 for (element = head->current;
344 element->prev != 0 && element->indx > indx;
345 element = element->prev)
348 else
349 for (element = head->current;
350 element->next != 0 && element->indx < indx;
351 element = element->next)
354 /* `element' is the nearest to the one we want. If it's not the one we
355 want, the one we want doesn't exist. */
356 head->current = element;
357 head->indx = element->indx;
358 if (element != 0 && element->indx != indx)
359 element = 0;
361 return element;
364 /* Clear a single bit in a bitmap. */
366 void
367 bitmap_clear_bit (head, bit)
368 bitmap head;
369 int bit;
371 bitmap_element *ptr = bitmap_find_bit (head, bit);
373 if (ptr != 0)
375 unsigned bit_num = bit % BITMAP_WORD_BITS;
376 unsigned word_num = bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
377 ptr->bits[word_num] &= ~ (((BITMAP_WORD) 1) << bit_num);
379 /* If we cleared the entire word, free up the element */
380 if (bitmap_element_zerop (ptr))
381 bitmap_element_free (head, ptr);
385 /* Set a single bit in a bitmap. */
387 void
388 bitmap_set_bit (head, bit)
389 bitmap head;
390 int bit;
392 bitmap_element *ptr = bitmap_find_bit (head, bit);
393 unsigned word_num = bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
394 unsigned bit_num = bit % BITMAP_WORD_BITS;
395 BITMAP_WORD bit_val = ((BITMAP_WORD) 1) << bit_num;
397 if (ptr == 0)
399 ptr = bitmap_element_allocate (head);
400 ptr->indx = bit / BITMAP_ELEMENT_ALL_BITS;
401 ptr->bits[word_num] = bit_val;
402 bitmap_element_link (head, ptr);
404 else
405 ptr->bits[word_num] |= bit_val;
408 /* Return whether a bit is set within a bitmap. */
411 bitmap_bit_p (head, bit)
412 bitmap head;
413 int bit;
415 bitmap_element *ptr;
416 unsigned bit_num;
417 unsigned word_num;
419 ptr = bitmap_find_bit (head, bit);
420 if (ptr == 0)
421 return 0;
423 bit_num = bit % BITMAP_WORD_BITS;
424 word_num = bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
426 return (ptr->bits[word_num] >> bit_num) & 1;
429 /* Return the bit number of the first set bit in the bitmap, or -1
430 if the bitmap is empty. */
433 bitmap_first_set_bit (a)
434 bitmap a;
436 bitmap_element *ptr = a->first;
437 BITMAP_WORD word;
438 unsigned word_num, bit_num;
440 if (ptr == NULL)
441 return -1;
443 #if BITMAP_ELEMENT_WORDS == 2
444 word_num = 0, word = ptr->bits[0];
445 if (word == 0)
446 word_num = 1, word = ptr->bits[1];
447 #else
448 for (word_num = 0; word_num < BITMAP_ELEMENT_WORDS; ++word_num)
449 if ((word = ptr->bits[word_num]) != 0)
450 break;
451 #endif
453 /* Binary search for the first set bit. */
454 /* ??? It'd be nice to know if ffs or ffsl was available. */
456 bit_num = 0;
457 word = word & -word;
459 #if nBITMAP_WORD_BITS > 64
460 #error "Fill out the table."
461 #endif
462 #if nBITMAP_WORD_BITS > 32
463 if ((word & 0xffffffff) == 0)
464 word >>= 32, bit_num += 32;
465 #endif
466 if ((word & 0xffff) == 0)
467 word >>= 16, bit_num += 16;
468 if ((word & 0xff) == 0)
469 word >>= 8, bit_num += 8;
470 if (word & 0xf0)
471 bit_num += 4;
472 if (word & 0xcc)
473 bit_num += 2;
474 if (word & 0xaa)
475 bit_num += 1;
477 return (ptr->indx * BITMAP_ELEMENT_ALL_BITS
478 + word_num * BITMAP_WORD_BITS
479 + bit_num);
482 /* Return the bit number of the last set bit in the bitmap, or -1
483 if the bitmap is empty. */
486 bitmap_last_set_bit (a)
487 bitmap a;
489 bitmap_element *ptr = a->first;
490 BITMAP_WORD word;
491 unsigned word_num, bit_num;
493 if (ptr == NULL)
494 return -1;
496 while (ptr->next != NULL)
497 ptr = ptr->next;
499 #if BITMAP_ELEMENT_WORDS == 2
500 word_num = 1, word = ptr->bits[1];
501 if (word == 0)
502 word_num = 0, word = ptr->bits[0];
503 #else
504 for (word_num = BITMAP_ELEMENT_WORDS; word_num-- > 0; )
505 if ((word = ptr->bits[word_num]) != 0)
506 break;
507 #endif
509 /* Binary search for the last set bit. */
511 bit_num = 0;
512 #if nBITMAP_WORD_BITS > 64
513 #error "Fill out the table."
514 #endif
515 #if nBITMAP_WORD_BITS > 32
516 if (word & ~(BITMAP_WORD)0xffffffff)
517 word >>= 32, bit_num += 32;
518 #endif
519 if (word & 0xffff0000)
520 word >>= 16, bit_num += 16;
521 if (word & 0xff00)
522 word >>= 8, bit_num += 8;
523 if (word & 0xf0)
524 word >>= 4, bit_num += 4;
525 if (word & 0xc)
526 word >>= 2, bit_num += 2;
527 if (word & 0x2)
528 bit_num += 1;
530 return (ptr->indx * BITMAP_ELEMENT_ALL_BITS
531 + word_num * BITMAP_WORD_BITS
532 + bit_num);
535 /* Store in bitmap TO the result of combining bitmap FROM1 and FROM2 using
536 a specific bit manipulation. Return true if TO changes. */
539 bitmap_operation (to, from1, from2, operation)
540 bitmap to;
541 bitmap from1;
542 bitmap from2;
543 enum bitmap_bits operation;
545 #define HIGHEST_INDEX (unsigned int) ~0
547 bitmap_element *from1_ptr = from1->first;
548 bitmap_element *from2_ptr = from2->first;
549 unsigned int indx1 = (from1_ptr) ? from1_ptr->indx : HIGHEST_INDEX;
550 unsigned int indx2 = (from2_ptr) ? from2_ptr->indx : HIGHEST_INDEX;
551 bitmap_element *to_ptr = to->first;
552 bitmap_element *from1_tmp;
553 bitmap_element *from2_tmp;
554 bitmap_element *to_tmp;
555 unsigned int indx;
556 int changed = 0;
558 #if BITMAP_ELEMENT_WORDS == 2
559 #define DOIT(OP) \
560 do { \
561 BITMAP_WORD t0, t1, f10, f11, f20, f21; \
562 f10 = from1_tmp->bits[0]; \
563 f20 = from2_tmp->bits[0]; \
564 t0 = f10 OP f20; \
565 changed |= (t0 != to_tmp->bits[0]); \
566 f11 = from1_tmp->bits[1]; \
567 f21 = from2_tmp->bits[1]; \
568 t1 = f11 OP f21; \
569 changed |= (t1 != to_tmp->bits[1]); \
570 to_tmp->bits[0] = t0; \
571 to_tmp->bits[1] = t1; \
572 } while (0)
573 #else
574 #define DOIT(OP) \
575 do { \
576 BITMAP_WORD t, f1, f2; \
577 int i; \
578 for (i = 0; i < BITMAP_ELEMENT_WORDS; ++i) \
580 f1 = from1_tmp->bits[i]; \
581 f2 = from2_tmp->bits[i]; \
582 t = f1 OP f2; \
583 changed |= (t != to_tmp->bits[i]); \
584 to_tmp->bits[i] = t; \
586 } while (0)
587 #endif
589 to->first = to->current = 0;
591 while (from1_ptr != 0 || from2_ptr != 0)
593 /* Figure out whether we need to substitute zero elements for
594 missing links. */
595 if (indx1 == indx2)
597 indx = indx1;
598 from1_tmp = from1_ptr;
599 from2_tmp = from2_ptr;
600 from1_ptr = from1_ptr->next;
601 indx1 = (from1_ptr) ? from1_ptr->indx : HIGHEST_INDEX;
602 from2_ptr = from2_ptr->next;
603 indx2 = (from2_ptr) ? from2_ptr->indx : HIGHEST_INDEX;
605 else if (indx1 < indx2)
607 indx = indx1;
608 from1_tmp = from1_ptr;
609 from2_tmp = &bitmap_zero_bits;
610 from1_ptr = from1_ptr->next;
611 indx1 = (from1_ptr) ? from1_ptr->indx : HIGHEST_INDEX;
613 else
615 indx = indx2;
616 from1_tmp = &bitmap_zero_bits;
617 from2_tmp = from2_ptr;
618 from2_ptr = from2_ptr->next;
619 indx2 = (from2_ptr) ? from2_ptr->indx : HIGHEST_INDEX;
622 /* Find the appropriate element from TO. Begin by discarding
623 elements that we've skipped. */
624 while (to_ptr && to_ptr->indx < indx)
626 changed = 1;
627 to_tmp = to_ptr;
628 to_ptr = to_ptr->next;
629 bitmap_elem_to_freelist (to, to_tmp);
631 if (to_ptr && to_ptr->indx == indx)
633 to_tmp = to_ptr;
634 to_ptr = to_ptr->next;
636 else
637 to_tmp = bitmap_element_allocate (to);
639 /* Do the operation, and if any bits are set, link it into the
640 linked list. */
641 switch (operation)
643 default:
644 abort ();
646 case BITMAP_AND:
647 DOIT (&);
648 break;
650 case BITMAP_AND_COMPL:
651 DOIT (&~);
652 break;
654 case BITMAP_IOR:
655 DOIT (|);
656 break;
657 case BITMAP_IOR_COMPL:
658 DOIT (|~);
659 break;
660 case BITMAP_XOR:
661 DOIT (^);
662 break;
665 if (! bitmap_element_zerop (to_tmp))
667 to_tmp->indx = indx;
668 bitmap_element_link (to, to_tmp);
670 else
672 bitmap_elem_to_freelist (to, to_tmp);
676 /* If we have elements of TO left over, free the lot. */
677 if (to_ptr)
679 changed = 1;
680 for (to_tmp = to_ptr; to_tmp->next ; to_tmp = to_tmp->next)
681 continue;
682 if (to->using_obstack)
684 to_tmp->next = bitmap_free;
685 bitmap_free = to_ptr;
687 else
689 to_tmp->next = bitmap_ggc_free;
690 bitmap_ggc_free = to_ptr;
694 #undef DOIT
696 return changed;
699 /* Return true if two bitmaps are identical. */
702 bitmap_equal_p (a, b)
703 bitmap a;
704 bitmap b;
706 bitmap_head c;
707 int ret;
709 memset (&c, 0, sizeof (c));
710 ret = ! bitmap_operation (&c, a, b, BITMAP_XOR);
711 bitmap_clear (&c);
713 return ret;
716 /* Or into bitmap TO bitmap FROM1 and'ed with the complement of
717 bitmap FROM2. */
719 void
720 bitmap_ior_and_compl (to, from1, from2)
721 bitmap to;
722 bitmap from1;
723 bitmap from2;
725 bitmap_head tmp;
727 tmp.first = tmp.current = 0;
728 tmp.using_obstack = 0;
730 bitmap_operation (&tmp, from1, from2, BITMAP_AND_COMPL);
731 bitmap_operation (to, to, &tmp, BITMAP_IOR);
732 bitmap_clear (&tmp);
736 bitmap_union_of_diff (dst, a, b, c)
737 bitmap dst;
738 bitmap a;
739 bitmap b;
740 bitmap c;
742 bitmap_head tmp;
743 int changed;
745 tmp.first = tmp.current = 0;
746 tmp.using_obstack = 0;
748 bitmap_operation (&tmp, b, c, BITMAP_AND_COMPL);
749 changed = bitmap_operation (dst, &tmp, a, BITMAP_IOR);
750 bitmap_clear (&tmp);
752 return changed;
755 /* Initialize a bitmap header. */
757 bitmap
758 bitmap_initialize (head, using_obstack)
759 bitmap head;
760 int using_obstack;
762 if (head == NULL && ! using_obstack)
763 head = ggc_alloc (sizeof (*head));
765 head->first = head->current = 0;
766 head->using_obstack = using_obstack;
768 return head;
771 /* Debugging function to print out the contents of a bitmap. */
773 void
774 debug_bitmap_file (file, head)
775 FILE *file;
776 bitmap head;
778 bitmap_element *ptr;
780 fprintf (file, "\nfirst = ");
781 fprintf (file, HOST_PTR_PRINTF, (PTR) head->first);
782 fprintf (file, " current = ");
783 fprintf (file, HOST_PTR_PRINTF, (PTR) head->current);
784 fprintf (file, " indx = %u\n", head->indx);
786 for (ptr = head->first; ptr; ptr = ptr->next)
788 unsigned int i, j, col = 26;
790 fprintf (file, "\t");
791 fprintf (file, HOST_PTR_PRINTF, (PTR) ptr);
792 fprintf (file, " next = ");
793 fprintf (file, HOST_PTR_PRINTF, (PTR) ptr->next);
794 fprintf (file, " prev = ");
795 fprintf (file, HOST_PTR_PRINTF, (PTR) ptr->prev);
796 fprintf (file, " indx = %u\n\t\tbits = {", ptr->indx);
798 for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
799 for (j = 0; j < BITMAP_WORD_BITS; j++)
800 if ((ptr->bits[i] >> j) & 1)
802 if (col > 70)
804 fprintf (file, "\n\t\t\t");
805 col = 24;
808 fprintf (file, " %u", (ptr->indx * BITMAP_ELEMENT_ALL_BITS
809 + i * BITMAP_WORD_BITS + j));
810 col += 4;
813 fprintf (file, " }\n");
817 /* Function to be called from the debugger to print the contents
818 of a bitmap. */
820 void
821 debug_bitmap (head)
822 bitmap head;
824 debug_bitmap_file (stdout, head);
827 /* Function to print out the contents of a bitmap. Unlike debug_bitmap_file,
828 it does not print anything but the bits. */
830 void
831 bitmap_print (file, head, prefix, suffix)
832 FILE *file;
833 bitmap head;
834 const char *prefix;
835 const char *suffix;
837 const char *comma = "";
838 int i;
840 fputs (prefix, file);
841 EXECUTE_IF_SET_IN_BITMAP (head, 0, i,
843 fprintf (file, "%s%d", comma, i);
844 comma = ", ";
846 fputs (suffix, file);
849 #include "gt-bitmap.h"