Initial revision
[official-gcc.git] / gcc / bitmap.c
blob0dec06ae82a1fa17aa004bfe26439f46d7b5d894
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)
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 <stdio.h>
22 #include "config.h"
23 #include "rtl.h"
24 #include "flags.h"
25 #include "obstack.h"
26 #include "regs.h"
27 #include "basic-block.h"
29 /* The contents of the current function definition are allocated
30 in this obstack, and all are freed at the end of the function.
31 For top-level functions, this is temporary_obstack.
32 Separate obstacks are made for nested functions. */
34 extern struct obstack *function_obstack;
37 #ifndef INLINE
38 #ifndef __GNUC__
39 #define INLINE
40 #else
41 #define INLINE __inline__
42 #endif
43 #endif
45 /* Global data */
46 bitmap_element bitmap_zero; /* An element of all zero bits. */
47 bitmap_element *bitmap_free; /* Freelist of bitmap elements. */
49 static void bitmap_element_free PROTO((bitmap, bitmap_element *));
50 static bitmap_element *bitmap_element_allocate PROTO((bitmap));
51 static int bitmap_element_zerop PROTO((bitmap_element *));
52 static void bitmap_element_link PROTO((bitmap, bitmap_element *));
53 static bitmap_element *bitmap_find_bit PROTO((bitmap, unsigned int));
55 /* Free a bitmap element */
57 static INLINE void
58 bitmap_element_free (head, elt)
59 bitmap head;
60 bitmap_element *elt;
62 bitmap_element *next = elt->next;
63 bitmap_element *prev = elt->prev;
65 if (prev)
66 prev->next = next;
68 if (next)
69 next->prev = prev;
71 if (head->first == elt)
72 head->first = next;
74 /* Since the first thing we try is to insert before current,
75 make current the next entry in preference to the previous. */
76 if (head->current == elt)
77 head->current = next != 0 ? next : prev;
79 elt->next = bitmap_free;
80 bitmap_free = elt;
83 /* Allocate a bitmap element. The bits are cleared, but nothing else is. */
85 static INLINE bitmap_element *
86 bitmap_element_allocate (head)
87 bitmap head;
89 bitmap_element *element;
90 int i;
92 if (bitmap_free != 0)
94 element = bitmap_free;
95 bitmap_free = element->next;
97 else
98 element = (bitmap_element *) obstack_alloc (function_obstack,
99 sizeof (bitmap_element));
101 #if BITMAP_ELEMENT_WORDS == 2
102 element->bits[0] = element->bits[1] = 0;
103 #else
104 for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
105 element->bits[i] = 0;
106 #endif
108 return element;
111 /* Return nonzero if all bits in an element are zero. */
113 static INLINE int
114 bitmap_element_zerop (element)
115 bitmap_element *element;
117 #if BITMAP_ELEMENT_WORDS == 2
118 return (element->bits[0] | element->bits[1]) == 0;
119 #else
120 int i;
122 for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
123 if (element->bits[i] != 0)
124 return 0;
126 return 1;
127 #endif
130 /* Link the bitmap element into the current bitmap linked list. */
132 static INLINE void
133 bitmap_element_link (head, element)
134 bitmap head;
135 bitmap_element *element;
137 unsigned int indx = element->indx;
138 bitmap_element *ptr;
140 /* If this is the first and only element, set it in. */
141 if (head->first == 0)
143 element->next = element->prev = 0;
144 head->first = element;
147 /* If this index is less than that of the current element, it goes someplace
148 before the current element. */
149 else if (indx < head->indx)
151 for (ptr = head->current;
152 ptr->prev != 0 && ptr->prev->indx > indx;
153 ptr = ptr->prev)
156 if (ptr->prev)
157 ptr->prev->next = element;
158 else
159 head->first = element;
161 element->prev = ptr->prev;
162 element->next = ptr;
163 ptr->prev = element;
166 /* Otherwise, it must go someplace after the current element. */
167 else
169 for (ptr = head->current;
170 ptr->next != 0 && ptr->next->indx < indx;
171 ptr = ptr->next)
174 if (ptr->next)
175 ptr->next->prev = element;
177 element->next = ptr->next;
178 element->prev = ptr;
179 ptr->next = element;
182 /* Set up so this is the first element searched. */
183 head->current = element;
184 head->indx = indx;
187 /* Clear a bitmap by freeing the linked list. */
189 void INLINE
190 bitmap_clear (head)
191 bitmap head;
193 bitmap_element *element, *next;
195 for (element = head->first; element != 0; element = next)
197 next = element->next;
198 element->next = bitmap_free;
199 bitmap_free = element;
202 head->first = head->current = 0;
205 /* Copy a bitmap to another bitmap */
207 void
208 bitmap_copy (to, from)
209 bitmap to;
210 bitmap from;
212 bitmap_element *from_ptr, *to_ptr = 0;
213 int i;
215 bitmap_clear (to);
217 /* Copy elements in forward direction one at a time */
218 for (from_ptr = from->first; from_ptr; from_ptr = from_ptr->next)
220 bitmap_element *to_elt = bitmap_element_allocate (to);
222 to_elt->indx = from_ptr->indx;
224 #if BITMAP_ELEMENT_WORDS == 2
225 to_elt->bits[0] = from_ptr->bits[0];
226 to_elt->bits[1] = from_ptr->bits[1];
227 #else
228 for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
229 to_elt->bits[i] = from_ptr->bits[i];
230 #endif
232 /* Here we have a special case of bitmap_element_link, for the case
233 where we know the links are being entered in sequence. */
234 if (to_ptr == 0)
236 to->first = to->current = to_elt;
237 to->indx = from_ptr->indx;
238 to_elt->next = to_elt->prev = 0;
240 else
242 to_elt->prev = to_ptr;
243 to_elt->next = 0;
244 to_ptr->next = to_elt;
247 to_ptr = to_elt;
251 /* Find a bitmap element that would hold a bitmap's bit.
252 Update the `current' field even if we can't find an element that
253 would hold the bitmap's bit to make eventual allocation
254 faster. */
256 static INLINE bitmap_element *
257 bitmap_find_bit (head, bit)
258 bitmap head;
259 unsigned int bit;
261 bitmap_element *element;
262 unsigned HOST_WIDE_INT indx = bit / BITMAP_ELEMENT_ALL_BITS;
264 if (head->current == 0)
265 return 0;
267 if (head->indx > indx)
268 for (element = head->current;
269 element->prev != 0 && element->indx > indx;
270 element = element->prev)
273 else
274 for (element = head->current;
275 element->next != 0 && element->indx < indx;
276 element = element->next)
279 /* `element' is the nearest to the one we want. If it's not the one we
280 want, the one we want doesn't exist. */
281 head->current = element;
282 head->indx = element->indx;
283 if (element != 0 && element->indx != indx)
284 element = 0;
286 return element;
289 /* Clear a single bit in a bitmap. */
291 void
292 bitmap_clear_bit (head, bit)
293 bitmap head;
294 int bit;
296 bitmap_element *ptr = bitmap_find_bit (head, bit);
298 if (ptr != 0)
300 unsigned bit_num = bit % (unsigned) HOST_BITS_PER_WIDE_INT;
301 unsigned word_num = ((bit / (unsigned) HOST_BITS_PER_WIDE_INT)
302 % BITMAP_ELEMENT_WORDS);
303 ptr->bits[word_num] &= ~ (((unsigned HOST_WIDE_INT) 1) << bit_num);
305 /* If we cleared the entire word, free up the element */
306 if (bitmap_element_zerop (ptr))
307 bitmap_element_free (head, ptr);
312 /* Set a single bit in a bitmap. */
314 void
315 bitmap_set_bit (head, bit)
316 bitmap head;
317 int bit;
319 bitmap_element *ptr = bitmap_find_bit (head, bit);
320 unsigned word_num
321 = ((bit / (unsigned) HOST_BITS_PER_WIDE_INT) % BITMAP_ELEMENT_WORDS);
322 unsigned bit_num = bit % (unsigned) HOST_BITS_PER_WIDE_INT;
323 unsigned HOST_WIDE_INT bit_val = ((unsigned HOST_WIDE_INT) 1) << bit_num;
325 if (ptr == 0)
327 ptr = bitmap_element_allocate (head);
328 ptr->indx = bit / BITMAP_ELEMENT_ALL_BITS;
329 ptr->bits[word_num] = bit_val;
330 bitmap_element_link (head, ptr);
332 else
333 ptr->bits[word_num] |= bit_val;
336 /* Return whether a bit is set within a bitmap. */
339 bitmap_bit_p (head, bit)
340 bitmap head;
341 int bit;
343 bitmap_element *ptr;
344 unsigned bit_num;
345 unsigned word_num;
347 ptr = bitmap_find_bit (head, bit);
348 if (ptr == 0)
349 return 0;
351 bit_num = bit % (unsigned) HOST_BITS_PER_WIDE_INT;
352 word_num
353 = ((bit / (unsigned) HOST_BITS_PER_WIDE_INT) % BITMAP_ELEMENT_WORDS);
355 return
356 (ptr->bits[word_num] & (((unsigned HOST_WIDE_INT) 1) << bit_num)) != 0;
359 /* Store in bitmap TO the result of combining bitmap FROM1 and
360 FROM2 using a specific bit manipulation. */
362 void
363 bitmap_operation (to, from1, from2, operation)
364 bitmap to;
365 bitmap from1;
366 bitmap from2;
367 enum bitmap_bits operation;
369 bitmap_element *delete_list = 0;
370 bitmap_element *from1_ptr = from1->first;
371 bitmap_element *from2_ptr = from2->first;
372 unsigned int indx1
373 = (from1_ptr) ? from1_ptr->indx : ~ (unsigned HOST_WIDE_INT) 0;
374 unsigned int indx2
375 = (from2_ptr) ? from2_ptr->indx : ~ (unsigned HOST_WIDE_INT) 0;
376 bitmap_element *to_ptr = 0;
377 bitmap_element *from1_tmp;
378 bitmap_element *from2_tmp;
379 unsigned int indx;
380 int i;
382 /* To simplify things, always create a new list. If the old list was one
383 of the inputs, free it later. Otherwise, free it now. */
384 if (to == from1 || to == from2)
386 delete_list = to->first;
387 to->first = to->current = 0;
389 else
390 bitmap_clear (to);
392 while (from1_ptr != 0 || from2_ptr != 0)
394 /* Figure out whether we need to substitute zero elements for
395 missing links. */
396 if (indx1 == indx2)
398 indx = indx1;
399 from1_tmp = from1_ptr;
400 from2_tmp = from2_ptr;
401 from1_ptr = from1_ptr->next;
402 indx1 = (from1_ptr) ? from1_ptr->indx : ~ (unsigned HOST_WIDE_INT) 0;
403 from2_ptr = from2_ptr->next;
404 indx2 = (from2_ptr) ? from2_ptr->indx : ~ (unsigned HOST_WIDE_INT) 0;
406 else if (indx1 < indx2)
408 indx = indx1;
409 from1_tmp = from1_ptr;
410 from2_tmp = &bitmap_zero;
411 from1_ptr = from1_ptr->next;
412 indx1 = (from1_ptr) ? from1_ptr->indx : ~ (unsigned HOST_WIDE_INT) 0;
414 else
416 indx = indx2;
417 from1_tmp = &bitmap_zero;
418 from2_tmp = from2_ptr;
419 from2_ptr = from2_ptr->next;
420 indx2 = (from2_ptr) ? from2_ptr->indx : ~ (unsigned HOST_WIDE_INT) 0;
423 if (to_ptr == 0)
424 to_ptr = bitmap_element_allocate (to);
426 /* Do the operation, and if any bits are set, link it into the
427 linked list. */
428 switch (operation)
430 default:
431 abort ();
433 case BITMAP_AND:
434 #if BITMAP_ELEMENT_WORDS == 2
435 to_ptr->bits[0] = from1_tmp->bits[0] & from2_tmp->bits[0];
436 to_ptr->bits[1] = from1_tmp->bits[1] & from2_tmp->bits[1];
437 #else
438 for (i = BITMAP_ELEMENT_WORDS - 1; i >= 0; i--)
439 to_ptr->bits[i] = from1_tmp->bits[i] & from2_tmp->bits[i];
440 #endif
441 break;
443 case BITMAP_AND_COMPL:
444 #if BITMAP_ELEMENT_WORDS == 2
445 to_ptr->bits[0] = from1_tmp->bits[0] & ~ from2_tmp->bits[0];
446 to_ptr->bits[1] = from1_tmp->bits[1] & ~ from2_tmp->bits[1];
447 #else
448 for (i = BITMAP_ELEMENT_WORDS - 1; i >= 0; i--)
449 to_ptr->bits[i] = from1_tmp->bits[i] & ~ from2_tmp->bits[i];
450 #endif
451 break;
453 case BITMAP_IOR:
454 #if BITMAP_ELEMENT_WORDS == 2
455 to_ptr->bits[0] = from1_tmp->bits[0] | from2_tmp->bits[0];
456 to_ptr->bits[1] = from1_tmp->bits[1] | from2_tmp->bits[1];
457 #else
458 for (i = BITMAP_ELEMENT_WORDS - 1; i >= 0; i--)
459 to_ptr->bits[i] = from1_tmp->bits[i] | from2_tmp->bits[i];
460 #endif
461 break;
464 if (! bitmap_element_zerop (to_ptr))
466 to_ptr->indx = indx;
467 bitmap_element_link (to, to_ptr);
468 to_ptr = 0;
472 /* If we have an unallocated element due to the last element being 0,
473 release it back to the free pool. Don't bother calling
474 bitmap_element_free since it was never linked into a bitmap. */
475 if (to_ptr != 0)
477 to_ptr->next = bitmap_free;
478 bitmap_free = to_ptr;
481 /* If the output bitmap was one of the inputs, free up its
482 elements now that we're done. */
483 for (; delete_list != 0; delete_list = to_ptr)
485 to_ptr = delete_list->next;
486 delete_list->next = bitmap_free;
487 bitmap_free = delete_list;
491 /* Or into bitmap TO bitmap FROM1 and'ed with the complement of
492 bitmap FROM2. */
494 void
495 bitmap_ior_and_compl (to, from1, from2)
496 bitmap to;
497 bitmap from1;
498 bitmap from2;
500 bitmap_head tmp;
502 tmp.first = tmp.current = 0;
504 bitmap_operation (&tmp, from1, from2, BITMAP_AND_COMPL);
505 bitmap_operation (to, to, &tmp, BITMAP_IOR);
506 bitmap_clear (&tmp);
509 /* Initialize a bitmap header. */
511 bitmap
512 bitmap_initialize (head)
513 bitmap head;
515 head->first = head->current = 0;
517 return head;
520 /* Debugging function to print out the contents of a bitmap. */
522 void
523 bitmap_debug_file (file, head)
524 FILE *file;
525 bitmap head;
527 bitmap_element *ptr;
529 fprintf (file, "\nfirst = ");
530 fprintf (file, HOST_PTR_PRINTF, (HOST_WIDE_INT) head->first);
531 fprintf (file, " current = ");
532 fprintf (file, HOST_PTR_PRINTF, (HOST_WIDE_INT) head->current);
533 fprintf (file, " indx = %u\n", head->indx);
535 for (ptr = head->first; ptr; ptr = ptr->next)
537 int i, j, col = 26;
539 fprintf (file, "\t");
540 fprintf (file, HOST_PTR_PRINTF, (HOST_WIDE_INT) ptr);
541 fprintf (file, " next = ");
542 fprintf (file, HOST_PTR_PRINTF, (HOST_WIDE_INT) ptr->next);
543 fprintf (file, " prev = ");
544 fprintf (file, HOST_PTR_PRINTF, (HOST_WIDE_INT) ptr->prev);
545 fprintf (file, " indx = %u\n\t\tbits = {", ptr->indx);
547 for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
548 for (j = 0; j < HOST_BITS_PER_WIDE_INT; j++)
549 if ((ptr->bits[i] & (((unsigned HOST_WIDE_INT) 1) << j)) != 0)
551 if (col > 70)
553 fprintf (file, "\n\t\t\t");
554 col = 24;
557 fprintf (file, " %u", (ptr->indx * BITMAP_ELEMENT_ALL_BITS
558 + i * HOST_BITS_PER_WIDE_INT + j));
559 col += 4;
562 fprintf (file, " }\n");
566 /* Function to be called from the debugger to print the contents
567 of a bitmap. */
569 void
570 debug_bitmap (head)
571 bitmap head;
573 bitmap_debug_file (stdout, head);
576 /* Release any memory allocated by bitmaps. Since we allocate off of the
577 function_obstack, just zap the free list. */
579 void
580 bitmap_release_memory ()
582 bitmap_free = 0;