1 /* Functions to support general ended bitmaps.
2 Copyright (C) 1997 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"
33 #ifdef NEED_DECLARATION_FREE
34 extern void free PROTO((void *));
37 /* Obstack to allocate bitmap elements from. */
38 static struct obstack bitmap_obstack;
39 static int bitmap_obstack_init = FALSE;
46 #define INLINE __inline__
51 bitmap_element bitmap_zero; /* An element of all zero bits. */
52 bitmap_element *bitmap_free; /* Freelist of bitmap elements. */
54 static void bitmap_element_free PROTO((bitmap, bitmap_element *));
55 static bitmap_element *bitmap_element_allocate PROTO((bitmap));
56 static int bitmap_element_zerop PROTO((bitmap_element *));
57 static void bitmap_element_link PROTO((bitmap, bitmap_element *));
58 static bitmap_element *bitmap_find_bit PROTO((bitmap, unsigned int));
60 /* Free a bitmap element */
63 bitmap_element_free (head, elt)
67 bitmap_element *next = elt->next;
68 bitmap_element *prev = elt->prev;
76 if (head->first == elt)
79 /* Since the first thing we try is to insert before current,
80 make current the next entry in preference to the previous. */
81 if (head->current == elt)
82 head->current = next != 0 ? next : prev;
84 elt->next = bitmap_free;
88 /* Allocate a bitmap element. The bits are cleared, but nothing else is. */
90 static INLINE bitmap_element *
91 bitmap_element_allocate (head)
94 bitmap_element *element;
99 element = bitmap_free;
100 bitmap_free = element->next;
104 /* We can't use gcc_obstack_init to initialize the obstack since
105 print-rtl.c now calls bitmap functions, and bitmap is linked
106 into the gen* functions. */
107 if (!bitmap_obstack_init)
109 bitmap_obstack_init = TRUE;
111 /* Let particular systems override the size of a chunk. */
112 #ifndef OBSTACK_CHUNK_SIZE
113 #define OBSTACK_CHUNK_SIZE 0
115 /* Let them override the alloc and free routines too. */
116 #ifndef OBSTACK_CHUNK_ALLOC
117 #define OBSTACK_CHUNK_ALLOC xmalloc
119 #ifndef OBSTACK_CHUNK_FREE
120 #define OBSTACK_CHUNK_FREE free
123 #if !defined(__GNUC__) || (__GNUC__ < 2)
124 #define __alignof__(type) 0
127 obstack_specify_allocation (&bitmap_obstack, OBSTACK_CHUNK_SIZE,
128 __alignof__ (bitmap_element),
129 (void *(*) ()) OBSTACK_CHUNK_ALLOC,
130 (void (*) ()) OBSTACK_CHUNK_FREE);
133 element = (bitmap_element *) obstack_alloc (&bitmap_obstack,
134 sizeof (bitmap_element));
137 #if BITMAP_ELEMENT_WORDS == 2
138 element->bits[0] = element->bits[1] = 0;
140 for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
141 element->bits[i] = 0;
147 /* Return nonzero if all bits in an element are zero. */
150 bitmap_element_zerop (element)
151 bitmap_element *element;
153 #if BITMAP_ELEMENT_WORDS == 2
154 return (element->bits[0] | element->bits[1]) == 0;
158 for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
159 if (element->bits[i] != 0)
166 /* Link the bitmap element into the current bitmap linked list. */
169 bitmap_element_link (head, element)
171 bitmap_element *element;
173 unsigned int indx = element->indx;
176 /* If this is the first and only element, set it in. */
177 if (head->first == 0)
179 element->next = element->prev = 0;
180 head->first = element;
183 /* If this index is less than that of the current element, it goes someplace
184 before the current element. */
185 else if (indx < head->indx)
187 for (ptr = head->current;
188 ptr->prev != 0 && ptr->prev->indx > indx;
193 ptr->prev->next = element;
195 head->first = element;
197 element->prev = ptr->prev;
202 /* Otherwise, it must go someplace after the current element. */
205 for (ptr = head->current;
206 ptr->next != 0 && ptr->next->indx < indx;
211 ptr->next->prev = element;
213 element->next = ptr->next;
218 /* Set up so this is the first element searched. */
219 head->current = element;
223 /* Clear a bitmap by freeing the linked list. */
229 bitmap_element *element, *next;
231 for (element = head->first; element != 0; element = next)
233 next = element->next;
234 element->next = bitmap_free;
235 bitmap_free = element;
238 head->first = head->current = 0;
241 /* Copy a bitmap to another bitmap */
244 bitmap_copy (to, from)
248 bitmap_element *from_ptr, *to_ptr = 0;
253 /* Copy elements in forward direction one at a time */
254 for (from_ptr = from->first; from_ptr; from_ptr = from_ptr->next)
256 bitmap_element *to_elt = bitmap_element_allocate (to);
258 to_elt->indx = from_ptr->indx;
260 #if BITMAP_ELEMENT_WORDS == 2
261 to_elt->bits[0] = from_ptr->bits[0];
262 to_elt->bits[1] = from_ptr->bits[1];
264 for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
265 to_elt->bits[i] = from_ptr->bits[i];
268 /* Here we have a special case of bitmap_element_link, for the case
269 where we know the links are being entered in sequence. */
272 to->first = to->current = to_elt;
273 to->indx = from_ptr->indx;
274 to_elt->next = to_elt->prev = 0;
278 to_elt->prev = to_ptr;
280 to_ptr->next = to_elt;
287 /* Find a bitmap element that would hold a bitmap's bit.
288 Update the `current' field even if we can't find an element that
289 would hold the bitmap's bit to make eventual allocation
292 static INLINE bitmap_element *
293 bitmap_find_bit (head, bit)
297 bitmap_element *element;
298 unsigned HOST_WIDE_INT indx = bit / BITMAP_ELEMENT_ALL_BITS;
300 if (head->current == 0)
303 if (head->indx > indx)
304 for (element = head->current;
305 element->prev != 0 && element->indx > indx;
306 element = element->prev)
310 for (element = head->current;
311 element->next != 0 && element->indx < indx;
312 element = element->next)
315 /* `element' is the nearest to the one we want. If it's not the one we
316 want, the one we want doesn't exist. */
317 head->current = element;
318 head->indx = element->indx;
319 if (element != 0 && element->indx != indx)
325 /* Clear a single bit in a bitmap. */
328 bitmap_clear_bit (head, bit)
332 bitmap_element *ptr = bitmap_find_bit (head, bit);
336 unsigned bit_num = bit % (unsigned) HOST_BITS_PER_WIDE_INT;
337 unsigned word_num = ((bit / (unsigned) HOST_BITS_PER_WIDE_INT)
338 % BITMAP_ELEMENT_WORDS);
339 ptr->bits[word_num] &= ~ (((unsigned HOST_WIDE_INT) 1) << bit_num);
341 /* If we cleared the entire word, free up the element */
342 if (bitmap_element_zerop (ptr))
343 bitmap_element_free (head, ptr);
348 /* Set a single bit in a bitmap. */
351 bitmap_set_bit (head, bit)
355 bitmap_element *ptr = bitmap_find_bit (head, bit);
357 = ((bit / (unsigned) HOST_BITS_PER_WIDE_INT) % BITMAP_ELEMENT_WORDS);
358 unsigned bit_num = bit % (unsigned) HOST_BITS_PER_WIDE_INT;
359 unsigned HOST_WIDE_INT bit_val = ((unsigned HOST_WIDE_INT) 1) << bit_num;
363 ptr = bitmap_element_allocate (head);
364 ptr->indx = bit / BITMAP_ELEMENT_ALL_BITS;
365 ptr->bits[word_num] = bit_val;
366 bitmap_element_link (head, ptr);
369 ptr->bits[word_num] |= bit_val;
372 /* Return whether a bit is set within a bitmap. */
375 bitmap_bit_p (head, bit)
383 ptr = bitmap_find_bit (head, bit);
387 bit_num = bit % (unsigned) HOST_BITS_PER_WIDE_INT;
389 = ((bit / (unsigned) HOST_BITS_PER_WIDE_INT) % BITMAP_ELEMENT_WORDS);
392 (ptr->bits[word_num] & (((unsigned HOST_WIDE_INT) 1) << bit_num)) != 0;
395 /* Store in bitmap TO the result of combining bitmap FROM1 and
396 FROM2 using a specific bit manipulation. */
399 bitmap_operation (to, from1, from2, operation)
403 enum bitmap_bits operation;
405 bitmap_element *delete_list = 0;
406 bitmap_element *from1_ptr = from1->first;
407 bitmap_element *from2_ptr = from2->first;
409 = (from1_ptr) ? from1_ptr->indx : ~ (unsigned HOST_WIDE_INT) 0;
411 = (from2_ptr) ? from2_ptr->indx : ~ (unsigned HOST_WIDE_INT) 0;
412 bitmap_element *to_ptr = 0;
413 bitmap_element *from1_tmp;
414 bitmap_element *from2_tmp;
418 /* To simplify things, always create a new list. If the old list was one
419 of the inputs, free it later. Otherwise, free it now. */
420 if (to == from1 || to == from2)
422 delete_list = to->first;
423 to->first = to->current = 0;
428 while (from1_ptr != 0 || from2_ptr != 0)
430 /* Figure out whether we need to substitute zero elements for
435 from1_tmp = from1_ptr;
436 from2_tmp = from2_ptr;
437 from1_ptr = from1_ptr->next;
438 indx1 = (from1_ptr) ? from1_ptr->indx : ~ (unsigned HOST_WIDE_INT) 0;
439 from2_ptr = from2_ptr->next;
440 indx2 = (from2_ptr) ? from2_ptr->indx : ~ (unsigned HOST_WIDE_INT) 0;
442 else if (indx1 < indx2)
445 from1_tmp = from1_ptr;
446 from2_tmp = &bitmap_zero;
447 from1_ptr = from1_ptr->next;
448 indx1 = (from1_ptr) ? from1_ptr->indx : ~ (unsigned HOST_WIDE_INT) 0;
453 from1_tmp = &bitmap_zero;
454 from2_tmp = from2_ptr;
455 from2_ptr = from2_ptr->next;
456 indx2 = (from2_ptr) ? from2_ptr->indx : ~ (unsigned HOST_WIDE_INT) 0;
460 to_ptr = bitmap_element_allocate (to);
462 /* Do the operation, and if any bits are set, link it into the
470 #if BITMAP_ELEMENT_WORDS == 2
471 to_ptr->bits[0] = from1_tmp->bits[0] & from2_tmp->bits[0];
472 to_ptr->bits[1] = from1_tmp->bits[1] & from2_tmp->bits[1];
474 for (i = BITMAP_ELEMENT_WORDS - 1; i >= 0; i--)
475 to_ptr->bits[i] = from1_tmp->bits[i] & from2_tmp->bits[i];
479 case BITMAP_AND_COMPL:
480 #if BITMAP_ELEMENT_WORDS == 2
481 to_ptr->bits[0] = from1_tmp->bits[0] & ~ from2_tmp->bits[0];
482 to_ptr->bits[1] = from1_tmp->bits[1] & ~ from2_tmp->bits[1];
484 for (i = BITMAP_ELEMENT_WORDS - 1; i >= 0; i--)
485 to_ptr->bits[i] = from1_tmp->bits[i] & ~ from2_tmp->bits[i];
490 #if BITMAP_ELEMENT_WORDS == 2
491 to_ptr->bits[0] = from1_tmp->bits[0] | from2_tmp->bits[0];
492 to_ptr->bits[1] = from1_tmp->bits[1] | from2_tmp->bits[1];
494 for (i = BITMAP_ELEMENT_WORDS - 1; i >= 0; i--)
495 to_ptr->bits[i] = from1_tmp->bits[i] | from2_tmp->bits[i];
500 if (! bitmap_element_zerop (to_ptr))
503 bitmap_element_link (to, to_ptr);
508 /* If we have an unallocated element due to the last element being 0,
509 release it back to the free pool. Don't bother calling
510 bitmap_element_free since it was never linked into a bitmap. */
513 to_ptr->next = bitmap_free;
514 bitmap_free = to_ptr;
517 /* If the output bitmap was one of the inputs, free up its
518 elements now that we're done. */
519 for (; delete_list != 0; delete_list = to_ptr)
521 to_ptr = delete_list->next;
522 delete_list->next = bitmap_free;
523 bitmap_free = delete_list;
527 /* Or into bitmap TO bitmap FROM1 and'ed with the complement of
531 bitmap_ior_and_compl (to, from1, from2)
538 tmp.first = tmp.current = 0;
540 bitmap_operation (&tmp, from1, from2, BITMAP_AND_COMPL);
541 bitmap_operation (to, to, &tmp, BITMAP_IOR);
545 /* Initialize a bitmap header. */
548 bitmap_initialize (head)
551 head->first = head->current = 0;
556 /* Debugging function to print out the contents of a bitmap. */
559 bitmap_debug_file (file, head)
565 fprintf (file, "\nfirst = ");
566 fprintf (file, HOST_PTR_PRINTF, (HOST_WIDE_INT) head->first);
567 fprintf (file, " current = ");
568 fprintf (file, HOST_PTR_PRINTF, (HOST_WIDE_INT) head->current);
569 fprintf (file, " indx = %u\n", head->indx);
571 for (ptr = head->first; ptr; ptr = ptr->next)
575 fprintf (file, "\t");
576 fprintf (file, HOST_PTR_PRINTF, (HOST_WIDE_INT) ptr);
577 fprintf (file, " next = ");
578 fprintf (file, HOST_PTR_PRINTF, (HOST_WIDE_INT) ptr->next);
579 fprintf (file, " prev = ");
580 fprintf (file, HOST_PTR_PRINTF, (HOST_WIDE_INT) ptr->prev);
581 fprintf (file, " indx = %u\n\t\tbits = {", ptr->indx);
583 for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
584 for (j = 0; j < HOST_BITS_PER_WIDE_INT; j++)
585 if ((ptr->bits[i] & (((unsigned HOST_WIDE_INT) 1) << j)) != 0)
589 fprintf (file, "\n\t\t\t");
593 fprintf (file, " %u", (ptr->indx * BITMAP_ELEMENT_ALL_BITS
594 + i * HOST_BITS_PER_WIDE_INT + j));
598 fprintf (file, " }\n");
602 /* Function to be called from the debugger to print the contents
609 bitmap_debug_file (stdout, head);
612 /* Function to print out the contents of a bitmap. Unlike bitmap_debug_file,
613 it does not print anything but the bits. */
616 bitmap_print (file, head, prefix, suffix)
625 fputs (prefix, file);
626 EXECUTE_IF_SET_IN_BITMAP (head, 0, i,
628 fprintf (file, "%s%d", comma, i);
631 fputs (suffix, file);
634 /* Release any memory allocated by bitmaps. */
637 bitmap_release_memory ()
640 if (bitmap_obstack_init)
642 bitmap_obstack_init = FALSE;
643 obstack_free (&bitmap_obstack, NULL_PTR);