1 /* Functions to support general ended bitmaps.
2 Copyright (C) 1997, 1998, 1999, 2000 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. */
27 #include "basic-block.h"
29 /* Obstack to allocate bitmap elements from. */
30 static struct obstack bitmap_obstack
;
31 static int bitmap_obstack_init
= FALSE
;
37 #define INLINE __inline__
42 bitmap_element bitmap_zero
; /* An element of all zero bits. */
43 bitmap_element
*bitmap_free
; /* Freelist of bitmap elements. */
45 static void bitmap_element_free
PARAMS ((bitmap
, bitmap_element
*));
46 static bitmap_element
*bitmap_element_allocate
PARAMS ((void));
47 static int bitmap_element_zerop
PARAMS ((bitmap_element
*));
48 static void bitmap_element_link
PARAMS ((bitmap
, bitmap_element
*));
49 static bitmap_element
*bitmap_find_bit
PARAMS ((bitmap
, unsigned int));
51 /* Free a bitmap element */
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
;
85 #if BITMAP_ELEMENT_WORDS != 2
91 element
= bitmap_free
;
92 bitmap_free
= element
->next
;
96 /* We can't use gcc_obstack_init to initialize the obstack since
97 print-rtl.c now calls bitmap functions, and bitmap is linked
98 into the gen* functions. */
99 if (!bitmap_obstack_init
)
101 bitmap_obstack_init
= TRUE
;
103 /* Let particular systems override the size of a chunk. */
104 #ifndef OBSTACK_CHUNK_SIZE
105 #define OBSTACK_CHUNK_SIZE 0
107 /* Let them override the alloc and free routines too. */
108 #ifndef OBSTACK_CHUNK_ALLOC
109 #define OBSTACK_CHUNK_ALLOC xmalloc
111 #ifndef OBSTACK_CHUNK_FREE
112 #define OBSTACK_CHUNK_FREE free
115 #if !defined(__GNUC__) || (__GNUC__ < 2)
116 #define __alignof__(type) 0
119 obstack_specify_allocation (&bitmap_obstack
, OBSTACK_CHUNK_SIZE
,
120 __alignof__ (bitmap_element
),
121 (void *(*) PARAMS ((long))) OBSTACK_CHUNK_ALLOC
,
122 (void (*) PARAMS ((void *))) OBSTACK_CHUNK_FREE
);
125 element
= (bitmap_element
*) obstack_alloc (&bitmap_obstack
,
126 sizeof (bitmap_element
));
129 #if BITMAP_ELEMENT_WORDS == 2
130 element
->bits
[0] = element
->bits
[1] = 0;
132 for (i
= 0; i
< BITMAP_ELEMENT_WORDS
; i
++)
133 element
->bits
[i
] = 0;
139 /* Return nonzero if all bits in an element are zero. */
142 bitmap_element_zerop (element
)
143 bitmap_element
*element
;
145 #if BITMAP_ELEMENT_WORDS == 2
146 return (element
->bits
[0] | element
->bits
[1]) == 0;
150 for (i
= 0; i
< BITMAP_ELEMENT_WORDS
; i
++)
151 if (element
->bits
[i
] != 0)
158 /* Link the bitmap element into the current bitmap linked list. */
161 bitmap_element_link (head
, element
)
163 bitmap_element
*element
;
165 unsigned int indx
= element
->indx
;
168 /* If this is the first and only element, set it in. */
169 if (head
->first
== 0)
171 element
->next
= element
->prev
= 0;
172 head
->first
= element
;
175 /* If this index is less than that of the current element, it goes someplace
176 before the current element. */
177 else if (indx
< head
->indx
)
179 for (ptr
= head
->current
;
180 ptr
->prev
!= 0 && ptr
->prev
->indx
> indx
;
185 ptr
->prev
->next
= element
;
187 head
->first
= element
;
189 element
->prev
= ptr
->prev
;
194 /* Otherwise, it must go someplace after the current element. */
197 for (ptr
= head
->current
;
198 ptr
->next
!= 0 && ptr
->next
->indx
< indx
;
203 ptr
->next
->prev
= element
;
205 element
->next
= ptr
->next
;
210 /* Set up so this is the first element searched. */
211 head
->current
= element
;
215 /* Clear a bitmap by freeing the linked list. */
221 bitmap_element
*element
, *next
;
223 for (element
= head
->first
; element
!= 0; element
= next
)
225 next
= element
->next
;
226 element
->next
= bitmap_free
;
227 bitmap_free
= element
;
230 head
->first
= head
->current
= 0;
233 /* Copy a bitmap to another bitmap */
236 bitmap_copy (to
, from
)
240 bitmap_element
*from_ptr
, *to_ptr
= 0;
241 #if BITMAP_ELEMENT_WORDS != 2
247 /* Copy elements in forward direction one at a time */
248 for (from_ptr
= from
->first
; from_ptr
; from_ptr
= from_ptr
->next
)
250 bitmap_element
*to_elt
= bitmap_element_allocate ();
252 to_elt
->indx
= from_ptr
->indx
;
254 #if BITMAP_ELEMENT_WORDS == 2
255 to_elt
->bits
[0] = from_ptr
->bits
[0];
256 to_elt
->bits
[1] = from_ptr
->bits
[1];
258 for (i
= 0; i
< BITMAP_ELEMENT_WORDS
; i
++)
259 to_elt
->bits
[i
] = from_ptr
->bits
[i
];
262 /* Here we have a special case of bitmap_element_link, for the case
263 where we know the links are being entered in sequence. */
266 to
->first
= to
->current
= to_elt
;
267 to
->indx
= from_ptr
->indx
;
268 to_elt
->next
= to_elt
->prev
= 0;
272 to_elt
->prev
= to_ptr
;
274 to_ptr
->next
= to_elt
;
281 /* Find a bitmap element that would hold a bitmap's bit.
282 Update the `current' field even if we can't find an element that
283 would hold the bitmap's bit to make eventual allocation
286 static INLINE bitmap_element
*
287 bitmap_find_bit (head
, bit
)
291 bitmap_element
*element
;
292 unsigned HOST_WIDE_INT indx
= bit
/ BITMAP_ELEMENT_ALL_BITS
;
294 if (head
->current
== 0)
297 if (head
->indx
> indx
)
298 for (element
= head
->current
;
299 element
->prev
!= 0 && element
->indx
> indx
;
300 element
= element
->prev
)
304 for (element
= head
->current
;
305 element
->next
!= 0 && element
->indx
< indx
;
306 element
= element
->next
)
309 /* `element' is the nearest to the one we want. If it's not the one we
310 want, the one we want doesn't exist. */
311 head
->current
= element
;
312 head
->indx
= element
->indx
;
313 if (element
!= 0 && element
->indx
!= indx
)
319 /* Clear a single bit in a bitmap. */
322 bitmap_clear_bit (head
, bit
)
326 bitmap_element
*ptr
= bitmap_find_bit (head
, bit
);
330 unsigned bit_num
= bit
% (unsigned) HOST_BITS_PER_WIDE_INT
;
331 unsigned word_num
= ((bit
/ (unsigned) HOST_BITS_PER_WIDE_INT
)
332 % BITMAP_ELEMENT_WORDS
);
333 ptr
->bits
[word_num
] &= ~ (((unsigned HOST_WIDE_INT
) 1) << bit_num
);
335 /* If we cleared the entire word, free up the element */
336 if (bitmap_element_zerop (ptr
))
337 bitmap_element_free (head
, ptr
);
342 /* Set a single bit in a bitmap. */
345 bitmap_set_bit (head
, bit
)
349 bitmap_element
*ptr
= bitmap_find_bit (head
, bit
);
351 = ((bit
/ (unsigned) HOST_BITS_PER_WIDE_INT
) % BITMAP_ELEMENT_WORDS
);
352 unsigned bit_num
= bit
% (unsigned) HOST_BITS_PER_WIDE_INT
;
353 unsigned HOST_WIDE_INT bit_val
= ((unsigned HOST_WIDE_INT
) 1) << bit_num
;
357 ptr
= bitmap_element_allocate ();
358 ptr
->indx
= bit
/ BITMAP_ELEMENT_ALL_BITS
;
359 ptr
->bits
[word_num
] = bit_val
;
360 bitmap_element_link (head
, ptr
);
363 ptr
->bits
[word_num
] |= bit_val
;
366 /* Return whether a bit is set within a bitmap. */
369 bitmap_bit_p (head
, bit
)
377 ptr
= bitmap_find_bit (head
, bit
);
381 bit_num
= bit
% (unsigned) HOST_BITS_PER_WIDE_INT
;
383 = ((bit
/ (unsigned) HOST_BITS_PER_WIDE_INT
) % BITMAP_ELEMENT_WORDS
);
385 return (ptr
->bits
[word_num
] >> bit_num
) & 1;
388 /* Store in bitmap TO the result of combining bitmap FROM1 and FROM2 using
389 a specific bit manipulation. Return true if TO changes. */
392 bitmap_operation (to
, from1
, from2
, operation
)
396 enum bitmap_bits operation
;
398 #define HIGHEST_INDEX (unsigned int) ~0
400 bitmap_element
*from1_ptr
= from1
->first
;
401 bitmap_element
*from2_ptr
= from2
->first
;
402 unsigned int indx1
= (from1_ptr
) ? from1_ptr
->indx
: HIGHEST_INDEX
;
403 unsigned int indx2
= (from2_ptr
) ? from2_ptr
->indx
: HIGHEST_INDEX
;
404 bitmap_element
*to_ptr
= to
->first
;
405 bitmap_element
*from1_tmp
;
406 bitmap_element
*from2_tmp
;
407 bitmap_element
*to_tmp
;
411 #if BITMAP_ELEMENT_WORDS == 2
414 unsigned HOST_WIDE_INT t0, t1, f10, f11, f20, f21; \
415 f10 = from1_tmp->bits[0]; \
416 f20 = from2_tmp->bits[0]; \
418 changed |= (t0 != to_tmp->bits[0]); \
419 f11 = from1_tmp->bits[1]; \
420 f21 = from2_tmp->bits[1]; \
422 changed |= (t1 != to_tmp->bits[1]); \
423 to_tmp->bits[0] = t0; \
424 to_tmp->bits[1] = t1; \
429 unsigned HOST_WIDE_INT t, f1, f2; \
431 for (i = 0; i < BITMAP_ELEMENT_WORDS; ++i) \
433 f1 = from1_tmp->bits[i]; \
434 f2 = from2_tmp->bits[i]; \
436 changed |= (t != to_tmp->bits[i]); \
437 to_tmp->bits[i] = t; \
442 to
->first
= to
->current
= 0;
444 while (from1_ptr
!= 0 || from2_ptr
!= 0)
446 /* Figure out whether we need to substitute zero elements for
451 from1_tmp
= from1_ptr
;
452 from2_tmp
= from2_ptr
;
453 from1_ptr
= from1_ptr
->next
;
454 indx1
= (from1_ptr
) ? from1_ptr
->indx
: HIGHEST_INDEX
;
455 from2_ptr
= from2_ptr
->next
;
456 indx2
= (from2_ptr
) ? from2_ptr
->indx
: HIGHEST_INDEX
;
458 else if (indx1
< indx2
)
461 from1_tmp
= from1_ptr
;
462 from2_tmp
= &bitmap_zero
;
463 from1_ptr
= from1_ptr
->next
;
464 indx1
= (from1_ptr
) ? from1_ptr
->indx
: HIGHEST_INDEX
;
469 from1_tmp
= &bitmap_zero
;
470 from2_tmp
= from2_ptr
;
471 from2_ptr
= from2_ptr
->next
;
472 indx2
= (from2_ptr
) ? from2_ptr
->indx
: HIGHEST_INDEX
;
475 /* Find the appropriate element from TO. Begin by discarding
476 elements that we've skipped. */
477 while (to_ptr
&& to_ptr
->indx
< indx
)
481 to_ptr
= to_ptr
->next
;
482 to_tmp
->next
= bitmap_free
;
483 bitmap_free
= to_tmp
;
485 if (to_ptr
&& to_ptr
->indx
== indx
)
488 to_ptr
= to_ptr
->next
;
491 to_tmp
= bitmap_element_allocate ();
493 /* Do the operation, and if any bits are set, link it into the
504 case BITMAP_AND_COMPL
:
517 if (! bitmap_element_zerop (to_tmp
))
520 bitmap_element_link (to
, to_tmp
);
524 to_tmp
->next
= bitmap_free
;
525 bitmap_free
= to_tmp
;
529 /* If we have elements of TO left over, free the lot. */
533 for (to_tmp
= to_ptr
; to_tmp
->next
; to_tmp
= to_tmp
->next
)
535 to_tmp
->next
= bitmap_free
;
536 bitmap_free
= to_ptr
;
544 /* Return true if two bitmaps are identical. */
547 bitmap_equal_p (a
, b
)
554 c
.first
= c
.current
= 0;
555 ret
= ! bitmap_operation (&c
, a
, b
, BITMAP_XOR
);
561 /* Or into bitmap TO bitmap FROM1 and'ed with the complement of
565 bitmap_ior_and_compl (to
, from1
, from2
)
572 tmp
.first
= tmp
.current
= 0;
574 bitmap_operation (&tmp
, from1
, from2
, BITMAP_AND_COMPL
);
575 bitmap_operation (to
, to
, &tmp
, BITMAP_IOR
);
579 /* Initialize a bitmap header. */
582 bitmap_initialize (head
)
585 head
->first
= head
->current
= 0;
590 /* Debugging function to print out the contents of a bitmap. */
593 debug_bitmap_file (file
, head
)
599 fprintf (file
, "\nfirst = ");
600 fprintf (file
, HOST_PTR_PRINTF
, head
->first
);
601 fprintf (file
, " current = ");
602 fprintf (file
, HOST_PTR_PRINTF
, head
->current
);
603 fprintf (file
, " indx = %u\n", head
->indx
);
605 for (ptr
= head
->first
; ptr
; ptr
= ptr
->next
)
609 fprintf (file
, "\t");
610 fprintf (file
, HOST_PTR_PRINTF
, ptr
);
611 fprintf (file
, " next = ");
612 fprintf (file
, HOST_PTR_PRINTF
, ptr
->next
);
613 fprintf (file
, " prev = ");
614 fprintf (file
, HOST_PTR_PRINTF
, ptr
->prev
);
615 fprintf (file
, " indx = %u\n\t\tbits = {", ptr
->indx
);
617 for (i
= 0; i
< BITMAP_ELEMENT_WORDS
; i
++)
618 for (j
= 0; j
< HOST_BITS_PER_WIDE_INT
; j
++)
619 if ((ptr
->bits
[i
] >> j
) & 1)
623 fprintf (file
, "\n\t\t\t");
627 fprintf (file
, " %u", (ptr
->indx
* BITMAP_ELEMENT_ALL_BITS
628 + i
* HOST_BITS_PER_WIDE_INT
+ j
));
632 fprintf (file
, " }\n");
636 /* Function to be called from the debugger to print the contents
643 debug_bitmap_file (stdout
, head
);
646 /* Function to print out the contents of a bitmap. Unlike debug_bitmap_file,
647 it does not print anything but the bits. */
650 bitmap_print (file
, head
, prefix
, suffix
)
656 const char *comma
= "";
659 fputs (prefix
, file
);
660 EXECUTE_IF_SET_IN_BITMAP (head
, 0, i
,
662 fprintf (file
, "%s%d", comma
, i
);
665 fputs (suffix
, file
);
668 /* Release any memory allocated by bitmaps. */
671 bitmap_release_memory ()
674 if (bitmap_obstack_init
)
676 bitmap_obstack_init
= FALSE
;
677 obstack_free (&bitmap_obstack
, NULL_PTR
);