OSDN Git Service

* longlong.h (umul_ppmm): Add ColdFire support.
[pf3gnuchains/gcc-fork.git] / gcc / sbitmap.c
1 /* Simple bitmaps.
2    Copyright (C) 1999, 2000, 2002, 2003 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
9 version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING.  If not, write to the Free
18 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA.  */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "rtl.h"
26 #include "flags.h"
27 #include "hard-reg-set.h"
28 #include "basic-block.h"
29
30 /* Bitmap manipulation routines.  */
31
32 /* Allocate a simple bitmap of N_ELMS bits.  */
33
34 sbitmap
35 sbitmap_alloc (unsigned int n_elms)
36 {
37   unsigned int bytes, size, amt;
38   sbitmap bmap;
39
40   size = SBITMAP_SET_SIZE (n_elms);
41   bytes = size * sizeof (SBITMAP_ELT_TYPE);
42   amt = (sizeof (struct simple_bitmap_def)
43          + bytes - sizeof (SBITMAP_ELT_TYPE));
44   bmap = xmalloc (amt);
45   bmap->n_bits = n_elms;
46   bmap->size = size;
47   bmap->bytes = bytes;
48   return bmap;
49 }
50
51 /* Resize a simple bitmap BMAP to N_ELMS bits.  If increasing the
52    size of BMAP, clear the new bits to zero if the DEF argument
53    is zero, and set them to one otherwise.  */
54
55 sbitmap
56 sbitmap_resize (sbitmap bmap, unsigned int n_elms, int def)
57 {
58   unsigned int bytes, size, amt;
59   unsigned int last_bit;
60
61   size = SBITMAP_SET_SIZE (n_elms);
62   bytes = size * sizeof (SBITMAP_ELT_TYPE);
63   if (bytes > bmap->bytes)
64     {
65       amt = (sizeof (struct simple_bitmap_def)
66             + bytes - sizeof (SBITMAP_ELT_TYPE));
67       bmap = xrealloc (bmap, amt);
68     }
69
70   if (n_elms > bmap->n_bits)
71     {
72       if (def)
73         {
74           memset (bmap->elms + bmap->size, -1, bytes - bmap->bytes);
75
76           /* Set the new bits if the original last element.  */
77           last_bit = bmap->n_bits % SBITMAP_ELT_BITS;
78           if (last_bit)
79             bmap->elms[bmap->size - 1]
80               |= ~((SBITMAP_ELT_TYPE)-1 >> (SBITMAP_ELT_BITS - last_bit));
81
82           /* Clear the unused bit in the new last element.  */
83           last_bit = n_elms % SBITMAP_ELT_BITS;
84           if (last_bit)
85             bmap->elms[size - 1]
86               &= (SBITMAP_ELT_TYPE)-1 >> (SBITMAP_ELT_BITS - last_bit);
87         }
88       else
89         memset (bmap->elms + bmap->size, 0, bytes - bmap->bytes);
90     }
91   else if (n_elms < bmap->n_bits)
92     {
93       /* Clear the surplus bits in the last word.  */
94       last_bit = n_elms % SBITMAP_ELT_BITS;
95       if (last_bit)
96         bmap->elms[size - 1]
97           &= (SBITMAP_ELT_TYPE)-1 >> (SBITMAP_ELT_BITS - last_bit);
98     }
99
100   bmap->n_bits = n_elms;
101   bmap->size = size;
102   bmap->bytes = bytes;
103   return bmap;
104 }
105
106 /* Allocate a vector of N_VECS bitmaps of N_ELMS bits.  */
107
108 sbitmap *
109 sbitmap_vector_alloc (unsigned int n_vecs, unsigned int n_elms)
110 {
111   unsigned int i, bytes, offset, elm_bytes, size, amt, vector_bytes;
112   sbitmap *bitmap_vector;
113
114   size = SBITMAP_SET_SIZE (n_elms);
115   bytes = size * sizeof (SBITMAP_ELT_TYPE);
116   elm_bytes = (sizeof (struct simple_bitmap_def)
117                + bytes - sizeof (SBITMAP_ELT_TYPE));
118   vector_bytes = n_vecs * sizeof (sbitmap *);
119
120   /* Round up `vector_bytes' to account for the alignment requirements
121      of an sbitmap.  One could allocate the vector-table and set of sbitmaps
122      separately, but that requires maintaining two pointers or creating
123      a cover struct to hold both pointers (so our result is still just
124      one pointer).  Neither is a bad idea, but this is simpler for now.  */
125   {
126     /* Based on DEFAULT_ALIGNMENT computation in obstack.c.  */
127     struct { char x; SBITMAP_ELT_TYPE y; } align;
128     int alignment = (char *) & align.y - & align.x;
129     vector_bytes = (vector_bytes + alignment - 1) & ~ (alignment - 1);
130   }
131
132   amt = vector_bytes + (n_vecs * elm_bytes);
133   bitmap_vector = xmalloc (amt);
134
135   for (i = 0, offset = vector_bytes; i < n_vecs; i++, offset += elm_bytes)
136     {
137       sbitmap b = (sbitmap) ((char *) bitmap_vector + offset);
138
139       bitmap_vector[i] = b;
140       b->n_bits = n_elms;
141       b->size = size;
142       b->bytes = bytes;
143     }
144
145   return bitmap_vector;
146 }
147
148 /* Copy sbitmap SRC to DST.  */
149
150 void
151 sbitmap_copy (sbitmap dst, sbitmap src)
152 {
153   memcpy (dst->elms, src->elms, sizeof (SBITMAP_ELT_TYPE) * dst->size);
154 }
155
156 /* Determine if a == b.  */
157 int
158 sbitmap_equal (sbitmap a, sbitmap b)
159 {
160   return !memcmp (a->elms, b->elms, sizeof (SBITMAP_ELT_TYPE) * a->size);
161 }
162
163 /* Zero all elements in a bitmap.  */
164
165 void
166 sbitmap_zero (sbitmap bmap)
167 {
168   memset (bmap->elms, 0, bmap->bytes);
169 }
170
171 /* Set all elements in a bitmap to ones.  */
172
173 void
174 sbitmap_ones (sbitmap bmap)
175 {
176   unsigned int last_bit;
177
178   memset (bmap->elms, -1, bmap->bytes);
179
180   last_bit = bmap->n_bits % SBITMAP_ELT_BITS;
181   if (last_bit)
182     bmap->elms[bmap->size - 1]
183       = (SBITMAP_ELT_TYPE)-1 >> (SBITMAP_ELT_BITS - last_bit);
184 }
185
186 /* Zero a vector of N_VECS bitmaps.  */
187
188 void
189 sbitmap_vector_zero (sbitmap *bmap, unsigned int n_vecs)
190 {
191   unsigned int i;
192
193   for (i = 0; i < n_vecs; i++)
194     sbitmap_zero (bmap[i]);
195 }
196
197 /* Set a vector of N_VECS bitmaps to ones.  */
198
199 void
200 sbitmap_vector_ones (sbitmap *bmap, unsigned int n_vecs)
201 {
202   unsigned int i;
203
204   for (i = 0; i < n_vecs; i++)
205     sbitmap_ones (bmap[i]);
206 }
207
208 /* Set DST to be A union (B - C).
209    DST = A | (B & ~C).
210    Returns true if any change is made.  */
211
212 bool
213 sbitmap_union_of_diff_cg (sbitmap dst, sbitmap a, sbitmap b, sbitmap c)
214 {
215   unsigned int i, n = dst->size;
216   sbitmap_ptr dstp = dst->elms;
217   sbitmap_ptr ap = a->elms;
218   sbitmap_ptr bp = b->elms;
219   sbitmap_ptr cp = c->elms;
220   SBITMAP_ELT_TYPE changed = 0;
221
222   for (i = 0; i < n; i++)
223     {
224       SBITMAP_ELT_TYPE tmp = *ap++ | (*bp++ & ~*cp++);
225       changed |= *dstp ^ tmp;
226       *dstp++ = tmp;
227     }
228
229   return changed != 0;
230 }
231
232 void
233 sbitmap_union_of_diff (sbitmap dst, sbitmap a, sbitmap b, sbitmap c)
234 {
235   unsigned int i, n = dst->size;
236   sbitmap_ptr dstp = dst->elms;
237   sbitmap_ptr ap = a->elms;
238   sbitmap_ptr bp = b->elms;
239   sbitmap_ptr cp = c->elms;
240
241   for (i = 0; i < n; i++)
242     *dstp++ = *ap++ | (*bp++ & ~*cp++);
243 }
244
245 /* Set bitmap DST to the bitwise negation of the bitmap SRC.  */
246
247 void
248 sbitmap_not (sbitmap dst, sbitmap src)
249 {
250   unsigned int i, n = dst->size;
251   sbitmap_ptr dstp = dst->elms;
252   sbitmap_ptr srcp = src->elms;
253
254   for (i = 0; i < n; i++)
255     *dstp++ = ~*srcp++;
256 }
257
258 /* Set the bits in DST to be the difference between the bits
259    in A and the bits in B. i.e. dst = a & (~b).  */
260
261 void
262 sbitmap_difference (sbitmap dst, sbitmap a, sbitmap b)
263 {
264   unsigned int i, dst_size = dst->size;
265   unsigned int min_size = dst->size;
266   sbitmap_ptr dstp = dst->elms;
267   sbitmap_ptr ap = a->elms;
268   sbitmap_ptr bp = b->elms;
269
270   /* A should be at least as large as DEST, to have a defined source.  */
271   if (a->size < dst_size)
272     abort ();
273   /* If minuend is smaller, we simply pretend it to be zero bits, i.e.
274      only copy the subtrahend into dest.  */
275   if (b->size < min_size)
276     min_size = b->size;
277   for (i = 0; i < min_size; i++)
278     *dstp++ = *ap++ & (~*bp++);
279   /* Now fill the rest of dest from A, if B was too short.
280      This makes sense only when destination and A differ.  */
281   if (dst != a && i != dst_size)
282     for (; i < dst_size; i++)
283       *dstp++ = *ap++;
284 }
285
286 /* Set DST to be (A and B).
287    Return nonzero if any change is made.  */
288
289 bool
290 sbitmap_a_and_b_cg (sbitmap dst, sbitmap a, sbitmap b)
291 {
292   unsigned int i, n = dst->size;
293   sbitmap_ptr dstp = dst->elms;
294   sbitmap_ptr ap = a->elms;
295   sbitmap_ptr bp = b->elms;
296   SBITMAP_ELT_TYPE changed = 0;
297
298   for (i = 0; i < n; i++)
299     {
300       SBITMAP_ELT_TYPE tmp = *ap++ & *bp++;
301       changed = *dstp ^ tmp;
302       *dstp++ = tmp;
303     }
304
305   return changed != 0;
306 }
307
308 void
309 sbitmap_a_and_b (sbitmap dst, sbitmap a, sbitmap b)
310 {
311   unsigned int i, n = dst->size;
312   sbitmap_ptr dstp = dst->elms;
313   sbitmap_ptr ap = a->elms;
314   sbitmap_ptr bp = b->elms;
315
316   for (i = 0; i < n; i++)
317     *dstp++ = *ap++ & *bp++;
318 }
319
320 /* Set DST to be (A xor B)).
321    Return nonzero if any change is made.  */
322
323 bool
324 sbitmap_a_xor_b_cg (sbitmap dst, sbitmap a, sbitmap b)
325 {
326   unsigned int i, n = dst->size;
327   sbitmap_ptr dstp = dst->elms;
328   sbitmap_ptr ap = a->elms;
329   sbitmap_ptr bp = b->elms;
330   SBITMAP_ELT_TYPE changed = 0;
331
332   for (i = 0; i < n; i++)
333     {
334       SBITMAP_ELT_TYPE tmp = *ap++ ^ *bp++;
335       changed = *dstp ^ tmp;
336       *dstp++ = tmp;
337     }
338
339   return changed != 0;
340 }
341
342 void
343 sbitmap_a_xor_b (sbitmap dst, sbitmap a, sbitmap b)
344 {
345   unsigned int i, n = dst->size;
346   sbitmap_ptr dstp = dst->elms;
347   sbitmap_ptr ap = a->elms;
348   sbitmap_ptr bp = b->elms;
349
350   for (i = 0; i < n; i++)
351     *dstp++ = *ap++ ^ *bp++;
352 }
353
354 /* Set DST to be (A or B)).
355    Return nonzero if any change is made.  */
356
357 bool
358 sbitmap_a_or_b_cg (sbitmap dst, sbitmap a, sbitmap b)
359 {
360   unsigned int i, n = dst->size;
361   sbitmap_ptr dstp = dst->elms;
362   sbitmap_ptr ap = a->elms;
363   sbitmap_ptr bp = b->elms;
364   SBITMAP_ELT_TYPE changed = 0;
365
366   for (i = 0; i < n; i++)
367     {
368       SBITMAP_ELT_TYPE tmp = *ap++ | *bp++;
369       changed = *dstp ^ tmp;
370       *dstp++ = tmp;
371     }
372
373   return changed != 0;
374 }
375
376 void
377 sbitmap_a_or_b (sbitmap dst, sbitmap a, sbitmap b)
378 {
379   unsigned int i, n = dst->size;
380   sbitmap_ptr dstp = dst->elms;
381   sbitmap_ptr ap = a->elms;
382   sbitmap_ptr bp = b->elms;
383
384   for (i = 0; i < n; i++)
385     *dstp++ = *ap++ | *bp++;
386 }
387
388 /* Return nonzero if A is a subset of B.  */
389
390 bool
391 sbitmap_a_subset_b_p (sbitmap a, sbitmap b)
392 {
393   unsigned int i, n = a->size;
394   sbitmap_ptr ap, bp;
395
396   for (ap = a->elms, bp = b->elms, i = 0; i < n; i++, ap++, bp++)
397     if ((*ap | *bp) != *bp)
398       return false;
399
400   return true;
401 }
402
403 /* Set DST to be (A or (B and C)).
404    Return nonzero if any change is made.  */
405
406 bool
407 sbitmap_a_or_b_and_c_cg (sbitmap dst, sbitmap a, sbitmap b, sbitmap c)
408 {
409   unsigned int i, n = dst->size;
410   sbitmap_ptr dstp = dst->elms;
411   sbitmap_ptr ap = a->elms;
412   sbitmap_ptr bp = b->elms;
413   sbitmap_ptr cp = c->elms;
414   SBITMAP_ELT_TYPE changed = 0;
415
416   for (i = 0; i < n; i++)
417     {
418       SBITMAP_ELT_TYPE tmp = *ap++ | (*bp++ & *cp++);
419       changed |= *dstp ^ tmp;
420       *dstp++ = tmp;
421     }
422
423   return changed != 0;
424 }
425
426 void
427 sbitmap_a_or_b_and_c (sbitmap dst, sbitmap a, sbitmap b, sbitmap c)
428 {
429   unsigned int i, n = dst->size;
430   sbitmap_ptr dstp = dst->elms;
431   sbitmap_ptr ap = a->elms;
432   sbitmap_ptr bp = b->elms;
433   sbitmap_ptr cp = c->elms;
434
435   for (i = 0; i < n; i++)
436     *dstp++ = *ap++ | (*bp++ & *cp++);
437 }
438
439 /* Set DST to be (A and (B or C)).
440    Return nonzero if any change is made.  */
441
442 bool
443 sbitmap_a_and_b_or_c_cg (sbitmap dst, sbitmap a, sbitmap b, sbitmap c)
444 {
445   unsigned int i, n = dst->size;
446   sbitmap_ptr dstp = dst->elms;
447   sbitmap_ptr ap = a->elms;
448   sbitmap_ptr bp = b->elms;
449   sbitmap_ptr cp = c->elms;
450   SBITMAP_ELT_TYPE changed = 0;
451
452   for (i = 0; i < n; i++)
453     {
454       SBITMAP_ELT_TYPE tmp = *ap++ & (*bp++ | *cp++);
455       changed |= *dstp ^ tmp;
456       *dstp++ = tmp;
457     }
458
459   return changed != 0;
460 }
461
462 void
463 sbitmap_a_and_b_or_c (sbitmap dst, sbitmap a, sbitmap b, sbitmap c)
464 {
465   unsigned int i, n = dst->size;
466   sbitmap_ptr dstp = dst->elms;
467   sbitmap_ptr ap = a->elms;
468   sbitmap_ptr bp = b->elms;
469   sbitmap_ptr cp = c->elms;
470
471   for (i = 0; i < n; i++)
472     *dstp++ = *ap++ & (*bp++ | *cp++);
473 }
474
475 #ifdef IN_GCC
476 /* Set the bitmap DST to the intersection of SRC of successors of
477    block number BB, using the new flow graph structures.  */
478
479 void
480 sbitmap_intersection_of_succs (sbitmap dst, sbitmap *src, int bb)
481 {
482   basic_block b = BASIC_BLOCK (bb);
483   unsigned int set_size = dst->size;
484   edge e;
485
486   for (e = b->succ; e != 0; e = e->succ_next)
487     {
488       if (e->dest == EXIT_BLOCK_PTR)
489         continue;
490
491       sbitmap_copy (dst, src[e->dest->index]);
492       break;
493     }
494
495   if (e == 0)
496     sbitmap_ones (dst);
497   else
498     for (e = e->succ_next; e != 0; e = e->succ_next)
499       {
500         unsigned int i;
501         sbitmap_ptr p, r;
502
503         if (e->dest == EXIT_BLOCK_PTR)
504           continue;
505
506         p = src[e->dest->index]->elms;
507         r = dst->elms;
508         for (i = 0; i < set_size; i++)
509           *r++ &= *p++;
510       }
511 }
512
513 /* Set the bitmap DST to the intersection of SRC of predecessors of
514    block number BB, using the new flow graph structures.  */
515
516 void
517 sbitmap_intersection_of_preds (sbitmap dst, sbitmap *src, int bb)
518 {
519   basic_block b = BASIC_BLOCK (bb);
520   unsigned int set_size = dst->size;
521   edge e;
522
523   for (e = b->pred; e != 0; e = e->pred_next)
524     {
525       if (e->src == ENTRY_BLOCK_PTR)
526         continue;
527
528       sbitmap_copy (dst, src[e->src->index]);
529       break;
530     }
531
532   if (e == 0)
533     sbitmap_ones (dst);
534   else
535     for (e = e->pred_next; e != 0; e = e->pred_next)
536       {
537         unsigned int i;
538         sbitmap_ptr p, r;
539
540         if (e->src == ENTRY_BLOCK_PTR)
541           continue;
542
543         p = src[e->src->index]->elms;
544         r = dst->elms;
545         for (i = 0; i < set_size; i++)
546           *r++ &= *p++;
547       }
548 }
549
550 /* Set the bitmap DST to the union of SRC of successors of
551    block number BB, using the new flow graph structures.  */
552
553 void
554 sbitmap_union_of_succs (sbitmap dst, sbitmap *src, int bb)
555 {
556   basic_block b = BASIC_BLOCK (bb);
557   unsigned int set_size = dst->size;
558   edge e;
559
560   for (e = b->succ; e != 0; e = e->succ_next)
561     {
562       if (e->dest == EXIT_BLOCK_PTR)
563         continue;
564
565       sbitmap_copy (dst, src[e->dest->index]);
566       break;
567     }
568
569   if (e == 0)
570     sbitmap_zero (dst);
571   else
572     for (e = e->succ_next; e != 0; e = e->succ_next)
573       {
574         unsigned int i;
575         sbitmap_ptr p, r;
576
577         if (e->dest == EXIT_BLOCK_PTR)
578           continue;
579
580         p = src[e->dest->index]->elms;
581         r = dst->elms;
582         for (i = 0; i < set_size; i++)
583           *r++ |= *p++;
584       }
585 }
586
587 /* Set the bitmap DST to the union of SRC of predecessors of
588    block number BB, using the new flow graph structures.  */
589
590 void
591 sbitmap_union_of_preds (sbitmap dst, sbitmap *src, int bb)
592 {
593   basic_block b = BASIC_BLOCK (bb);
594   unsigned int set_size = dst->size;
595   edge e;
596
597   for (e = b->pred; e != 0; e = e->pred_next)
598     {
599       if (e->src== ENTRY_BLOCK_PTR)
600         continue;
601
602       sbitmap_copy (dst, src[e->src->index]);
603       break;
604     }
605
606   if (e == 0)
607     sbitmap_zero (dst);
608   else
609     for (e = e->pred_next; e != 0; e = e->pred_next)
610       {
611         unsigned int i;
612         sbitmap_ptr p, r;
613
614         if (e->src == ENTRY_BLOCK_PTR)
615           continue;
616
617         p = src[e->src->index]->elms;
618         r = dst->elms;
619         for (i = 0; i < set_size; i++)
620           *r++ |= *p++;
621       }
622 }
623 #endif
624
625 /* Return number of first bit set in the bitmap, -1 if none.  */
626
627 int
628 sbitmap_first_set_bit (sbitmap bmap)
629 {
630   unsigned int n;
631
632   EXECUTE_IF_SET_IN_SBITMAP (bmap, 0, n, { return n; });
633   return -1;
634 }
635
636 /* Return number of last bit set in the bitmap, -1 if none.  */
637
638 int
639 sbitmap_last_set_bit (sbitmap bmap)
640 {
641   int i;
642   SBITMAP_ELT_TYPE *ptr = bmap->elms;
643
644   for (i = bmap->size - 1; i >= 0; i--)
645     {
646       SBITMAP_ELT_TYPE word = ptr[i];
647
648       if (word != 0)
649         {
650           unsigned int index = (i + 1) * SBITMAP_ELT_BITS - 1;
651           SBITMAP_ELT_TYPE mask
652             = (SBITMAP_ELT_TYPE) 1 << (SBITMAP_ELT_BITS - 1);
653
654           while (1)
655             {
656               if ((word & mask) != 0)
657                 return index;
658
659               mask >>= 1;
660               index--;
661             }
662         }
663     }
664
665   return -1;
666 }
667
668 void
669 dump_sbitmap (FILE *file, sbitmap bmap)
670 {
671   unsigned int i, n, j;
672   unsigned int set_size = bmap->size;
673   unsigned int total_bits = bmap->n_bits;
674
675   fprintf (file, "  ");
676   for (i = n = 0; i < set_size && n < total_bits; i++)
677     for (j = 0; j < SBITMAP_ELT_BITS && n < total_bits; j++, n++)
678       {
679         if (n != 0 && n % 10 == 0)
680           fprintf (file, " ");
681
682         fprintf (file, "%d",
683                  (bmap->elms[i] & ((SBITMAP_ELT_TYPE) 1 << j)) != 0);
684       }
685
686   fprintf (file, "\n");
687 }
688
689 void
690 dump_sbitmap_file (FILE *file, sbitmap bmap)
691 {
692   unsigned int i, pos;
693
694   fprintf (file, "n_bits = %d, set = {", bmap->n_bits);
695
696   for (pos = 30, i = 0; i < bmap->n_bits; i++)
697     if (TEST_BIT (bmap, i))
698       {
699         if (pos > 70)
700           {
701             fprintf (file, "\n  ");
702             pos = 0;
703           }
704
705         fprintf (file, "%d ", i);
706         pos += 2 + (i >= 10) + (i >= 100) + (i >= 1000);
707       }
708
709   fprintf (file, "}\n");
710 }
711
712 void
713 debug_sbitmap (sbitmap bmap)
714 {
715   dump_sbitmap_file (stderr, bmap);
716 }
717
718 void
719 dump_sbitmap_vector (FILE *file, const char *title, const char *subtitle,
720                      sbitmap *bmaps, int n_maps)
721 {
722   int bb;
723
724   fprintf (file, "%s\n", title);
725   for (bb = 0; bb < n_maps; bb++)
726     {
727       fprintf (file, "%s %d\n", subtitle, bb);
728       dump_sbitmap (file, bmaps[bb]);
729     }
730
731   fprintf (file, "\n");
732 }