Import final gcc2 snapshot (990109)
[official-gcc.git] / gcc / bitmap.c
bloba862a5ec12bb03635c5195a36c9e72eed3dd0724
1 /* Functions to support general ended bitmaps.
2 Copyright (C) 1997, 1998 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)
9 any later version.
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. */
21 #include "config.h"
22 #include "system.h"
23 #include "rtl.h"
24 #include "flags.h"
25 #include "obstack.h"
26 #include "regs.h"
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;
33 #ifndef INLINE
34 #ifndef __GNUC__
35 #define INLINE
36 #else
37 #define INLINE __inline__
38 #endif
39 #endif
41 /* Global data */
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 PROTO((bitmap, bitmap_element *));
46 static bitmap_element *bitmap_element_allocate PROTO((bitmap));
47 static int bitmap_element_zerop PROTO((bitmap_element *));
48 static void bitmap_element_link PROTO((bitmap, bitmap_element *));
49 static bitmap_element *bitmap_find_bit PROTO((bitmap, unsigned int));
51 /* Free a bitmap element */
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 (head)
83 bitmap head;
85 bitmap_element *element;
86 int i;
88 if (bitmap_free != 0)
90 element = bitmap_free;
91 bitmap_free = element->next;
93 else
95 /* We can't use gcc_obstack_init to initialize the obstack since
96 print-rtl.c now calls bitmap functions, and bitmap is linked
97 into the gen* functions. */
98 if (!bitmap_obstack_init)
100 bitmap_obstack_init = TRUE;
102 /* Let particular systems override the size of a chunk. */
103 #ifndef OBSTACK_CHUNK_SIZE
104 #define OBSTACK_CHUNK_SIZE 0
105 #endif
106 /* Let them override the alloc and free routines too. */
107 #ifndef OBSTACK_CHUNK_ALLOC
108 #define OBSTACK_CHUNK_ALLOC xmalloc
109 #endif
110 #ifndef OBSTACK_CHUNK_FREE
111 #define OBSTACK_CHUNK_FREE free
112 #endif
114 #if !defined(__GNUC__) || (__GNUC__ < 2)
115 #define __alignof__(type) 0
116 #endif
118 obstack_specify_allocation (&bitmap_obstack, OBSTACK_CHUNK_SIZE,
119 __alignof__ (bitmap_element),
120 (void *(*) ()) OBSTACK_CHUNK_ALLOC,
121 (void (*) ()) OBSTACK_CHUNK_FREE);
124 element = (bitmap_element *) obstack_alloc (&bitmap_obstack,
125 sizeof (bitmap_element));
128 #if BITMAP_ELEMENT_WORDS == 2
129 element->bits[0] = element->bits[1] = 0;
130 #else
131 for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
132 element->bits[i] = 0;
133 #endif
135 return element;
138 /* Return nonzero if all bits in an element are zero. */
140 static INLINE int
141 bitmap_element_zerop (element)
142 bitmap_element *element;
144 #if BITMAP_ELEMENT_WORDS == 2
145 return (element->bits[0] | element->bits[1]) == 0;
146 #else
147 int i;
149 for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
150 if (element->bits[i] != 0)
151 return 0;
153 return 1;
154 #endif
157 /* Link the bitmap element into the current bitmap linked list. */
159 static INLINE void
160 bitmap_element_link (head, element)
161 bitmap head;
162 bitmap_element *element;
164 unsigned int indx = element->indx;
165 bitmap_element *ptr;
167 /* If this is the first and only element, set it in. */
168 if (head->first == 0)
170 element->next = element->prev = 0;
171 head->first = element;
174 /* If this index is less than that of the current element, it goes someplace
175 before the current element. */
176 else if (indx < head->indx)
178 for (ptr = head->current;
179 ptr->prev != 0 && ptr->prev->indx > indx;
180 ptr = ptr->prev)
183 if (ptr->prev)
184 ptr->prev->next = element;
185 else
186 head->first = element;
188 element->prev = ptr->prev;
189 element->next = ptr;
190 ptr->prev = element;
193 /* Otherwise, it must go someplace after the current element. */
194 else
196 for (ptr = head->current;
197 ptr->next != 0 && ptr->next->indx < indx;
198 ptr = ptr->next)
201 if (ptr->next)
202 ptr->next->prev = element;
204 element->next = ptr->next;
205 element->prev = ptr;
206 ptr->next = element;
209 /* Set up so this is the first element searched. */
210 head->current = element;
211 head->indx = indx;
214 /* Clear a bitmap by freeing the linked list. */
216 void INLINE
217 bitmap_clear (head)
218 bitmap head;
220 bitmap_element *element, *next;
222 for (element = head->first; element != 0; element = next)
224 next = element->next;
225 element->next = bitmap_free;
226 bitmap_free = element;
229 head->first = head->current = 0;
232 /* Copy a bitmap to another bitmap */
234 void
235 bitmap_copy (to, from)
236 bitmap to;
237 bitmap from;
239 bitmap_element *from_ptr, *to_ptr = 0;
240 int i;
242 bitmap_clear (to);
244 /* Copy elements in forward direction one at a time */
245 for (from_ptr = from->first; from_ptr; from_ptr = from_ptr->next)
247 bitmap_element *to_elt = bitmap_element_allocate (to);
249 to_elt->indx = from_ptr->indx;
251 #if BITMAP_ELEMENT_WORDS == 2
252 to_elt->bits[0] = from_ptr->bits[0];
253 to_elt->bits[1] = from_ptr->bits[1];
254 #else
255 for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
256 to_elt->bits[i] = from_ptr->bits[i];
257 #endif
259 /* Here we have a special case of bitmap_element_link, for the case
260 where we know the links are being entered in sequence. */
261 if (to_ptr == 0)
263 to->first = to->current = to_elt;
264 to->indx = from_ptr->indx;
265 to_elt->next = to_elt->prev = 0;
267 else
269 to_elt->prev = to_ptr;
270 to_elt->next = 0;
271 to_ptr->next = to_elt;
274 to_ptr = to_elt;
278 /* Find a bitmap element that would hold a bitmap's bit.
279 Update the `current' field even if we can't find an element that
280 would hold the bitmap's bit to make eventual allocation
281 faster. */
283 static INLINE bitmap_element *
284 bitmap_find_bit (head, bit)
285 bitmap head;
286 unsigned int bit;
288 bitmap_element *element;
289 unsigned HOST_WIDE_INT indx = bit / BITMAP_ELEMENT_ALL_BITS;
291 if (head->current == 0)
292 return 0;
294 if (head->indx > indx)
295 for (element = head->current;
296 element->prev != 0 && element->indx > indx;
297 element = element->prev)
300 else
301 for (element = head->current;
302 element->next != 0 && element->indx < indx;
303 element = element->next)
306 /* `element' is the nearest to the one we want. If it's not the one we
307 want, the one we want doesn't exist. */
308 head->current = element;
309 head->indx = element->indx;
310 if (element != 0 && element->indx != indx)
311 element = 0;
313 return element;
316 /* Clear a single bit in a bitmap. */
318 void
319 bitmap_clear_bit (head, bit)
320 bitmap head;
321 int bit;
323 bitmap_element *ptr = bitmap_find_bit (head, bit);
325 if (ptr != 0)
327 unsigned bit_num = bit % (unsigned) HOST_BITS_PER_WIDE_INT;
328 unsigned word_num = ((bit / (unsigned) HOST_BITS_PER_WIDE_INT)
329 % BITMAP_ELEMENT_WORDS);
330 ptr->bits[word_num] &= ~ (((unsigned HOST_WIDE_INT) 1) << bit_num);
332 /* If we cleared the entire word, free up the element */
333 if (bitmap_element_zerop (ptr))
334 bitmap_element_free (head, ptr);
339 /* Set a single bit in a bitmap. */
341 void
342 bitmap_set_bit (head, bit)
343 bitmap head;
344 int bit;
346 bitmap_element *ptr = bitmap_find_bit (head, bit);
347 unsigned word_num
348 = ((bit / (unsigned) HOST_BITS_PER_WIDE_INT) % BITMAP_ELEMENT_WORDS);
349 unsigned bit_num = bit % (unsigned) HOST_BITS_PER_WIDE_INT;
350 unsigned HOST_WIDE_INT bit_val = ((unsigned HOST_WIDE_INT) 1) << bit_num;
352 if (ptr == 0)
354 ptr = bitmap_element_allocate (head);
355 ptr->indx = bit / BITMAP_ELEMENT_ALL_BITS;
356 ptr->bits[word_num] = bit_val;
357 bitmap_element_link (head, ptr);
359 else
360 ptr->bits[word_num] |= bit_val;
363 /* Return whether a bit is set within a bitmap. */
366 bitmap_bit_p (head, bit)
367 bitmap head;
368 int bit;
370 bitmap_element *ptr;
371 unsigned bit_num;
372 unsigned word_num;
374 ptr = bitmap_find_bit (head, bit);
375 if (ptr == 0)
376 return 0;
378 bit_num = bit % (unsigned) HOST_BITS_PER_WIDE_INT;
379 word_num
380 = ((bit / (unsigned) HOST_BITS_PER_WIDE_INT) % BITMAP_ELEMENT_WORDS);
382 return
383 (ptr->bits[word_num] & (((unsigned HOST_WIDE_INT) 1) << bit_num)) != 0;
386 /* Store in bitmap TO the result of combining bitmap FROM1 and
387 FROM2 using a specific bit manipulation. */
389 void
390 bitmap_operation (to, from1, from2, operation)
391 bitmap to;
392 bitmap from1;
393 bitmap from2;
394 enum bitmap_bits operation;
396 bitmap_element *delete_list = 0;
397 bitmap_element *from1_ptr = from1->first;
398 bitmap_element *from2_ptr = from2->first;
399 unsigned int indx1
400 = (from1_ptr) ? from1_ptr->indx : ~ (unsigned HOST_WIDE_INT) 0;
401 unsigned int indx2
402 = (from2_ptr) ? from2_ptr->indx : ~ (unsigned HOST_WIDE_INT) 0;
403 bitmap_element *to_ptr = 0;
404 bitmap_element *from1_tmp;
405 bitmap_element *from2_tmp;
406 unsigned int indx;
407 int i;
409 /* To simplify things, always create a new list. If the old list was one
410 of the inputs, free it later. Otherwise, free it now. */
411 if (to == from1 || to == from2)
413 delete_list = to->first;
414 to->first = to->current = 0;
416 else
417 bitmap_clear (to);
419 while (from1_ptr != 0 || from2_ptr != 0)
421 /* Figure out whether we need to substitute zero elements for
422 missing links. */
423 if (indx1 == indx2)
425 indx = indx1;
426 from1_tmp = from1_ptr;
427 from2_tmp = from2_ptr;
428 from1_ptr = from1_ptr->next;
429 indx1 = (from1_ptr) ? from1_ptr->indx : ~ (unsigned HOST_WIDE_INT) 0;
430 from2_ptr = from2_ptr->next;
431 indx2 = (from2_ptr) ? from2_ptr->indx : ~ (unsigned HOST_WIDE_INT) 0;
433 else if (indx1 < indx2)
435 indx = indx1;
436 from1_tmp = from1_ptr;
437 from2_tmp = &bitmap_zero;
438 from1_ptr = from1_ptr->next;
439 indx1 = (from1_ptr) ? from1_ptr->indx : ~ (unsigned HOST_WIDE_INT) 0;
441 else
443 indx = indx2;
444 from1_tmp = &bitmap_zero;
445 from2_tmp = from2_ptr;
446 from2_ptr = from2_ptr->next;
447 indx2 = (from2_ptr) ? from2_ptr->indx : ~ (unsigned HOST_WIDE_INT) 0;
450 if (to_ptr == 0)
451 to_ptr = bitmap_element_allocate (to);
453 /* Do the operation, and if any bits are set, link it into the
454 linked list. */
455 switch (operation)
457 default:
458 abort ();
460 case BITMAP_AND:
461 #if BITMAP_ELEMENT_WORDS == 2
462 to_ptr->bits[0] = from1_tmp->bits[0] & from2_tmp->bits[0];
463 to_ptr->bits[1] = from1_tmp->bits[1] & from2_tmp->bits[1];
464 #else
465 for (i = BITMAP_ELEMENT_WORDS - 1; i >= 0; i--)
466 to_ptr->bits[i] = from1_tmp->bits[i] & from2_tmp->bits[i];
467 #endif
468 break;
470 case BITMAP_AND_COMPL:
471 #if BITMAP_ELEMENT_WORDS == 2
472 to_ptr->bits[0] = from1_tmp->bits[0] & ~ from2_tmp->bits[0];
473 to_ptr->bits[1] = from1_tmp->bits[1] & ~ from2_tmp->bits[1];
474 #else
475 for (i = BITMAP_ELEMENT_WORDS - 1; i >= 0; i--)
476 to_ptr->bits[i] = from1_tmp->bits[i] & ~ from2_tmp->bits[i];
477 #endif
478 break;
480 case BITMAP_IOR:
481 #if BITMAP_ELEMENT_WORDS == 2
482 to_ptr->bits[0] = from1_tmp->bits[0] | from2_tmp->bits[0];
483 to_ptr->bits[1] = from1_tmp->bits[1] | from2_tmp->bits[1];
484 #else
485 for (i = BITMAP_ELEMENT_WORDS - 1; i >= 0; i--)
486 to_ptr->bits[i] = from1_tmp->bits[i] | from2_tmp->bits[i];
487 #endif
488 break;
491 if (! bitmap_element_zerop (to_ptr))
493 to_ptr->indx = indx;
494 bitmap_element_link (to, to_ptr);
495 to_ptr = 0;
499 /* If we have an unallocated element due to the last element being 0,
500 release it back to the free pool. Don't bother calling
501 bitmap_element_free since it was never linked into a bitmap. */
502 if (to_ptr != 0)
504 to_ptr->next = bitmap_free;
505 bitmap_free = to_ptr;
508 /* If the output bitmap was one of the inputs, free up its
509 elements now that we're done. */
510 for (; delete_list != 0; delete_list = to_ptr)
512 to_ptr = delete_list->next;
513 delete_list->next = bitmap_free;
514 bitmap_free = delete_list;
518 /* Or into bitmap TO bitmap FROM1 and'ed with the complement of
519 bitmap FROM2. */
521 void
522 bitmap_ior_and_compl (to, from1, from2)
523 bitmap to;
524 bitmap from1;
525 bitmap from2;
527 bitmap_head tmp;
529 tmp.first = tmp.current = 0;
531 bitmap_operation (&tmp, from1, from2, BITMAP_AND_COMPL);
532 bitmap_operation (to, to, &tmp, BITMAP_IOR);
533 bitmap_clear (&tmp);
536 /* Initialize a bitmap header. */
538 bitmap
539 bitmap_initialize (head)
540 bitmap head;
542 head->first = head->current = 0;
544 return head;
547 /* Debugging function to print out the contents of a bitmap. */
549 void
550 bitmap_debug_file (file, head)
551 FILE *file;
552 bitmap head;
554 bitmap_element *ptr;
556 fprintf (file, "\nfirst = ");
557 fprintf (file, HOST_PTR_PRINTF, (HOST_WIDE_INT) head->first);
558 fprintf (file, " current = ");
559 fprintf (file, HOST_PTR_PRINTF, (HOST_WIDE_INT) head->current);
560 fprintf (file, " indx = %u\n", head->indx);
562 for (ptr = head->first; ptr; ptr = ptr->next)
564 int i, j, col = 26;
566 fprintf (file, "\t");
567 fprintf (file, HOST_PTR_PRINTF, (HOST_WIDE_INT) ptr);
568 fprintf (file, " next = ");
569 fprintf (file, HOST_PTR_PRINTF, (HOST_WIDE_INT) ptr->next);
570 fprintf (file, " prev = ");
571 fprintf (file, HOST_PTR_PRINTF, (HOST_WIDE_INT) ptr->prev);
572 fprintf (file, " indx = %u\n\t\tbits = {", ptr->indx);
574 for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
575 for (j = 0; j < HOST_BITS_PER_WIDE_INT; j++)
576 if ((ptr->bits[i] & (((unsigned HOST_WIDE_INT) 1) << j)) != 0)
578 if (col > 70)
580 fprintf (file, "\n\t\t\t");
581 col = 24;
584 fprintf (file, " %u", (ptr->indx * BITMAP_ELEMENT_ALL_BITS
585 + i * HOST_BITS_PER_WIDE_INT + j));
586 col += 4;
589 fprintf (file, " }\n");
593 /* Function to be called from the debugger to print the contents
594 of a bitmap. */
596 void
597 debug_bitmap (head)
598 bitmap head;
600 bitmap_debug_file (stdout, head);
603 /* Function to print out the contents of a bitmap. Unlike bitmap_debug_file,
604 it does not print anything but the bits. */
606 void
607 bitmap_print (file, head, prefix, suffix)
608 FILE *file;
609 bitmap head;
610 char *prefix;
611 char *suffix;
613 char *comma = "";
614 int i;
616 fputs (prefix, file);
617 EXECUTE_IF_SET_IN_BITMAP (head, 0, i,
619 fprintf (file, "%s%d", comma, i);
620 comma = ", ";
622 fputs (suffix, file);
625 /* Release any memory allocated by bitmaps. */
627 void
628 bitmap_release_memory ()
630 bitmap_free = 0;
631 if (bitmap_obstack_init)
633 bitmap_obstack_init = FALSE;
634 obstack_free (&bitmap_obstack, NULL_PTR);