OSDN Git Service

* alias.c (nonlocal_reference_p): Add static prototype.
[pf3gnuchains/gcc-fork.git] / gcc / bitmap.c
1 /* Functions to support general ended bitmaps.
2    Copyright (C) 1997, 1998 Free Software Foundation, Inc.
3
4 This file is part of GNU CC.
5
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.
10
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.
15
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.  */
20
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"
28
29 /* Obstack to allocate bitmap elements from.  */
30 static struct obstack bitmap_obstack;
31 static int bitmap_obstack_init = FALSE;
32 \f
33 #ifndef INLINE
34 #ifndef __GNUC__
35 #define INLINE
36 #else
37 #define INLINE __inline__
38 #endif
39 #endif
40
41 /* Global data */
42 bitmap_element bitmap_zero;             /* An element of all zero bits. */
43 bitmap_element *bitmap_free;            /* Freelist of bitmap elements. */
44
45 static void bitmap_element_free         PROTO((bitmap, bitmap_element *));
46 static bitmap_element *bitmap_element_allocate PROTO((void));
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));
50 \f
51 /* Free a bitmap element */
52
53 static INLINE void
54 bitmap_element_free (head, elt)
55      bitmap head;
56      bitmap_element *elt;
57 {
58   bitmap_element *next = elt->next;
59   bitmap_element *prev = elt->prev;
60
61   if (prev)
62     prev->next = next;
63
64   if (next)
65     next->prev = prev;
66
67   if (head->first == elt)
68     head->first = next;
69
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;
74
75   elt->next = bitmap_free;
76   bitmap_free = elt;
77 }
78 \f
79 /* Allocate a bitmap element.  The bits are cleared, but nothing else is.  */
80
81 static INLINE bitmap_element *
82 bitmap_element_allocate ()
83 {
84   bitmap_element *element;
85 #if BITMAP_ELEMENT_WORDS != 2
86   int i;
87 #endif
88
89   if (bitmap_free != 0)
90     {
91       element = bitmap_free;
92       bitmap_free = element->next;
93     }
94   else
95     {
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)
100         {
101           bitmap_obstack_init = TRUE;
102
103           /* Let particular systems override the size of a chunk.  */
104 #ifndef OBSTACK_CHUNK_SIZE
105 #define OBSTACK_CHUNK_SIZE 0
106 #endif
107           /* Let them override the alloc and free routines too.  */
108 #ifndef OBSTACK_CHUNK_ALLOC
109 #define OBSTACK_CHUNK_ALLOC xmalloc
110 #endif
111 #ifndef OBSTACK_CHUNK_FREE
112 #define OBSTACK_CHUNK_FREE free
113 #endif
114
115 #if !defined(__GNUC__) || (__GNUC__ < 2)
116 #define __alignof__(type) 0
117 #endif
118
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);
123         }
124
125       element = (bitmap_element *) obstack_alloc (&bitmap_obstack,
126                                                   sizeof (bitmap_element));
127     }
128
129 #if BITMAP_ELEMENT_WORDS == 2
130   element->bits[0] = element->bits[1] = 0;
131 #else
132   for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
133     element->bits[i] = 0;
134 #endif
135
136   return element;
137 }
138
139 /* Return nonzero if all bits in an element are zero.  */
140
141 static INLINE int
142 bitmap_element_zerop (element)
143      bitmap_element *element;
144 {
145 #if BITMAP_ELEMENT_WORDS == 2
146   return (element->bits[0] | element->bits[1]) == 0;
147 #else
148   int i;
149
150   for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
151     if (element->bits[i] != 0)
152       return 0;
153
154   return 1;
155 #endif
156 }
157 \f
158 /* Link the bitmap element into the current bitmap linked list.  */
159
160 static INLINE void
161 bitmap_element_link (head, element)
162      bitmap head;
163      bitmap_element *element;
164 {
165   unsigned int indx = element->indx;
166   bitmap_element *ptr;
167
168   /* If this is the first and only element, set it in.  */
169   if (head->first == 0)
170     {
171       element->next = element->prev = 0;
172       head->first = element;
173     }
174
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)
178     {
179       for (ptr = head->current;
180            ptr->prev != 0 && ptr->prev->indx > indx;
181            ptr = ptr->prev)
182         ;
183
184       if (ptr->prev)
185         ptr->prev->next = element;
186       else
187         head->first = element;
188
189       element->prev = ptr->prev;
190       element->next = ptr;
191       ptr->prev = element;
192     }
193
194   /* Otherwise, it must go someplace after the current element.  */
195   else
196     {
197       for (ptr = head->current;
198            ptr->next != 0 && ptr->next->indx < indx;
199            ptr = ptr->next)
200         ;
201
202       if (ptr->next)
203         ptr->next->prev = element;
204
205       element->next = ptr->next;
206       element->prev = ptr;
207       ptr->next = element;
208     }
209
210   /* Set up so this is the first element searched.  */
211   head->current = element;
212   head->indx = indx;
213 }
214 \f
215 /* Clear a bitmap by freeing the linked list.  */
216
217 INLINE void
218 bitmap_clear (head)
219      bitmap head;
220 {
221   bitmap_element *element, *next;
222
223   for (element = head->first; element != 0; element = next)
224     {
225       next = element->next;
226       element->next = bitmap_free;
227       bitmap_free = element;
228     }
229
230   head->first = head->current =  0;
231 }
232 \f
233 /* Copy a bitmap to another bitmap */
234
235 void
236 bitmap_copy (to, from)
237      bitmap to;
238      bitmap from;
239 {
240   bitmap_element *from_ptr, *to_ptr = 0;
241 #if BITMAP_ELEMENT_WORDS != 2
242   int i;
243 #endif
244
245   bitmap_clear (to);
246
247   /* Copy elements in forward direction one at a time */
248   for (from_ptr = from->first; from_ptr; from_ptr = from_ptr->next)
249     {
250       bitmap_element *to_elt = bitmap_element_allocate ();
251
252       to_elt->indx = from_ptr->indx;
253
254 #if BITMAP_ELEMENT_WORDS == 2
255       to_elt->bits[0] = from_ptr->bits[0];
256       to_elt->bits[1] = from_ptr->bits[1];
257 #else
258       for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
259         to_elt->bits[i] = from_ptr->bits[i];
260 #endif
261
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.  */
264       if (to_ptr == 0)
265         {
266           to->first = to->current = to_elt;
267           to->indx = from_ptr->indx;
268           to_elt->next = to_elt->prev = 0;
269         }
270       else
271         {
272           to_elt->prev = to_ptr;
273           to_elt->next = 0;
274           to_ptr->next = to_elt;
275         }
276
277       to_ptr = to_elt;
278     }
279 }
280 \f
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
284    faster.  */
285
286 static INLINE bitmap_element *
287 bitmap_find_bit (head, bit)
288      bitmap head;
289      unsigned int bit;
290 {
291   bitmap_element *element;
292   unsigned HOST_WIDE_INT indx = bit / BITMAP_ELEMENT_ALL_BITS;
293
294   if (head->current == 0)
295     return 0;
296
297   if (head->indx > indx)
298     for (element = head->current;
299          element->prev != 0 && element->indx > indx;
300          element = element->prev)
301       ;
302
303   else
304     for (element = head->current;
305          element->next != 0 && element->indx < indx;
306          element = element->next)
307       ;
308
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)
314     element = 0;
315
316   return element;
317 }
318 \f
319 /* Clear a single bit in a bitmap.  */
320
321 void
322 bitmap_clear_bit (head, bit)
323      bitmap head;
324      int bit;
325 {
326   bitmap_element *ptr = bitmap_find_bit (head, bit);
327
328   if (ptr != 0)
329     {
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);
334
335       /* If we cleared the entire word, free up the element */
336       if (bitmap_element_zerop (ptr))
337         bitmap_element_free (head, ptr);
338     }
339 }
340
341 \f
342 /* Set a single bit in a bitmap.  */
343
344 void
345 bitmap_set_bit (head, bit)
346      bitmap head;
347      int bit;
348 {
349   bitmap_element *ptr = bitmap_find_bit (head, bit);
350   unsigned word_num
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;
354
355   if (ptr == 0)
356     {
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);
361     }
362   else
363     ptr->bits[word_num] |= bit_val;
364 }
365 \f
366 /* Return whether a bit is set within a bitmap.  */
367
368 int
369 bitmap_bit_p (head, bit)
370      bitmap head;
371      int bit;
372 {
373   bitmap_element *ptr;
374   unsigned bit_num;
375   unsigned word_num;
376
377   ptr = bitmap_find_bit (head, bit);
378   if (ptr == 0)
379     return 0;
380
381   bit_num = bit % (unsigned) HOST_BITS_PER_WIDE_INT;
382   word_num
383     = ((bit / (unsigned) HOST_BITS_PER_WIDE_INT) % BITMAP_ELEMENT_WORDS);
384
385   return
386     (ptr->bits[word_num] & (((unsigned HOST_WIDE_INT) 1) << bit_num)) != 0;
387 }
388 \f
389 /* Store in bitmap TO the result of combining bitmap FROM1 and
390    FROM2 using a specific bit manipulation.  */
391
392 void
393 bitmap_operation (to, from1, from2, operation)
394      bitmap to;
395      bitmap from1;
396      bitmap from2;
397      enum bitmap_bits operation;
398 {
399   bitmap_element *delete_list = 0;
400   bitmap_element *from1_ptr = from1->first;
401   bitmap_element *from2_ptr = from2->first;
402   unsigned int indx1
403     = (from1_ptr) ? from1_ptr->indx : ~ (unsigned HOST_WIDE_INT) 0;
404   unsigned int indx2
405     = (from2_ptr) ? from2_ptr->indx : ~ (unsigned HOST_WIDE_INT) 0;
406   bitmap_element *to_ptr = 0;
407   bitmap_element *from1_tmp;
408   bitmap_element *from2_tmp;
409   unsigned int indx;
410 #if BITMAP_ELEMENT_WORDS != 2
411   int i;
412 #endif
413
414   /* To simplify things, always create a new list.  If the old list was one
415      of the inputs, free it later.  Otherwise, free it now.  */
416   if (to == from1 || to == from2)
417     {
418       delete_list = to->first;
419       to->first = to->current = 0;
420     }
421   else
422     bitmap_clear (to);
423
424   while (from1_ptr != 0 || from2_ptr != 0)
425     {
426       /* Figure out whether we need to substitute zero elements for
427          missing links.  */
428       if (indx1 == indx2)
429         {
430           indx = indx1;
431           from1_tmp = from1_ptr;
432           from2_tmp = from2_ptr;
433           from1_ptr = from1_ptr->next;
434           indx1 = (from1_ptr) ? from1_ptr->indx : ~ (unsigned HOST_WIDE_INT) 0;
435           from2_ptr = from2_ptr->next;
436           indx2 = (from2_ptr) ? from2_ptr->indx : ~ (unsigned HOST_WIDE_INT) 0;
437         }
438       else if (indx1 < indx2)
439         {
440           indx = indx1;
441           from1_tmp = from1_ptr;
442           from2_tmp = &bitmap_zero;
443           from1_ptr = from1_ptr->next;
444           indx1 = (from1_ptr) ? from1_ptr->indx : ~ (unsigned HOST_WIDE_INT) 0;
445         }
446       else
447         {
448           indx = indx2;
449           from1_tmp = &bitmap_zero;
450           from2_tmp = from2_ptr;
451           from2_ptr = from2_ptr->next;
452           indx2 = (from2_ptr) ? from2_ptr->indx : ~ (unsigned HOST_WIDE_INT) 0;
453         }
454
455       if (to_ptr == 0)
456         to_ptr = bitmap_element_allocate ();
457
458       /* Do the operation, and if any bits are set, link it into the
459          linked list.  */
460       switch (operation)
461         {
462         default:
463           abort ();
464
465         case BITMAP_AND:
466 #if BITMAP_ELEMENT_WORDS == 2
467           to_ptr->bits[0] = from1_tmp->bits[0] & from2_tmp->bits[0];
468           to_ptr->bits[1] = from1_tmp->bits[1] & from2_tmp->bits[1];
469 #else
470           for (i = BITMAP_ELEMENT_WORDS - 1; i >= 0; i--)
471             to_ptr->bits[i] = from1_tmp->bits[i] & from2_tmp->bits[i];
472 #endif
473           break;
474
475         case BITMAP_AND_COMPL:
476 #if BITMAP_ELEMENT_WORDS == 2
477           to_ptr->bits[0] = from1_tmp->bits[0] & ~ from2_tmp->bits[0];
478           to_ptr->bits[1] = from1_tmp->bits[1] & ~ from2_tmp->bits[1];
479 #else
480           for (i = BITMAP_ELEMENT_WORDS - 1; i >= 0; i--)
481             to_ptr->bits[i] = from1_tmp->bits[i] & ~ from2_tmp->bits[i];
482 #endif
483           break;
484
485         case BITMAP_IOR:
486 #if BITMAP_ELEMENT_WORDS == 2
487           to_ptr->bits[0] = from1_tmp->bits[0] | from2_tmp->bits[0];
488           to_ptr->bits[1] = from1_tmp->bits[1] | from2_tmp->bits[1];
489 #else
490           for (i = BITMAP_ELEMENT_WORDS - 1; i >= 0; i--)
491             to_ptr->bits[i] = from1_tmp->bits[i] | from2_tmp->bits[i];
492 #endif
493           break;
494         }
495
496       if (! bitmap_element_zerop (to_ptr))
497         {
498           to_ptr->indx = indx;
499           bitmap_element_link (to, to_ptr);
500           to_ptr = 0;
501         }
502     }
503
504   /* If we have an unallocated element due to the last element being 0,
505      release it back to the free pool.  Don't bother calling
506      bitmap_element_free since it was never linked into a bitmap.  */
507   if (to_ptr != 0)
508     {
509       to_ptr->next = bitmap_free;
510       bitmap_free = to_ptr;
511     }
512
513   /* If the output bitmap was one of the inputs, free up its
514      elements now that we're done.  */
515   for (; delete_list != 0; delete_list = to_ptr)
516     {
517       to_ptr = delete_list->next;
518       delete_list->next = bitmap_free;
519       bitmap_free = delete_list;
520     }
521 }
522 \f
523 /* Or into bitmap TO bitmap FROM1 and'ed with the complement of
524    bitmap FROM2. */
525
526 void
527 bitmap_ior_and_compl (to, from1, from2)
528      bitmap to;
529      bitmap from1;
530      bitmap from2;
531 {
532   bitmap_head tmp;
533
534   tmp.first = tmp.current = 0;
535
536   bitmap_operation (&tmp, from1, from2, BITMAP_AND_COMPL);
537   bitmap_operation (to, to, &tmp, BITMAP_IOR);
538   bitmap_clear (&tmp);
539 }
540 \f
541 /* Initialize a bitmap header.  */
542
543 bitmap
544 bitmap_initialize (head)
545      bitmap head;
546 {
547   head->first = head->current = 0;
548
549   return head;
550 }
551 \f
552 /* Debugging function to print out the contents of a bitmap.  */
553
554 void
555 debug_bitmap_file (file, head)
556      FILE *file;
557      bitmap head;
558 {
559   bitmap_element *ptr;
560
561   fprintf (file, "\nfirst = ");
562   fprintf (file, HOST_PTR_PRINTF, head->first);
563   fprintf (file, " current = ");
564   fprintf (file, HOST_PTR_PRINTF, head->current);
565   fprintf (file, " indx = %u\n", head->indx);
566
567   for (ptr = head->first; ptr; ptr = ptr->next)
568     {
569       int i, j, col = 26;
570
571       fprintf (file, "\t");
572       fprintf (file, HOST_PTR_PRINTF, ptr);
573       fprintf (file, " next = ");
574       fprintf (file, HOST_PTR_PRINTF, ptr->next);
575       fprintf (file, " prev = ");
576       fprintf (file, HOST_PTR_PRINTF, ptr->prev);
577       fprintf (file, " indx = %u\n\t\tbits = {", ptr->indx);
578
579       for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
580         for (j = 0; j < HOST_BITS_PER_WIDE_INT; j++)
581           if ((ptr->bits[i] & (((unsigned HOST_WIDE_INT) 1) << j)) != 0)
582             {
583               if (col > 70)
584                 {
585                   fprintf (file, "\n\t\t\t");
586                   col = 24;
587                 }
588
589               fprintf (file, " %u", (ptr->indx * BITMAP_ELEMENT_ALL_BITS
590                                      + i * HOST_BITS_PER_WIDE_INT + j));
591               col += 4;
592             }
593
594       fprintf (file, " }\n");
595     }
596 }
597 \f
598 /* Function to be called from the debugger to print the contents
599    of a bitmap.  */
600
601 void
602 debug_bitmap (head)
603      bitmap head;
604 {
605   debug_bitmap_file (stdout, head);
606 }
607 \f
608 /* Function to print out the contents of a bitmap.  Unlike debug_bitmap_file,
609    it does not print anything but the bits.  */
610
611 void
612 bitmap_print (file, head, prefix, suffix)
613      FILE *file;
614      bitmap head;
615      const char *prefix;
616      const char *suffix;
617 {
618   const char *comma = "";
619   int i;
620
621   fputs (prefix, file);
622   EXECUTE_IF_SET_IN_BITMAP (head, 0, i,
623                             {
624                               fprintf (file, "%s%d", comma, i);
625                               comma = ", ";
626                             });
627   fputs (suffix, file);
628 }
629 \f
630 /* Release any memory allocated by bitmaps.  */
631
632 void
633 bitmap_release_memory ()
634 {
635   bitmap_free = 0;
636   if (bitmap_obstack_init)
637     {
638       bitmap_obstack_init = FALSE;
639       obstack_free (&bitmap_obstack, NULL_PTR);
640     }
641 }