PR c++/3478
[official-gcc.git] / gcc / sbitmap.c
blob1d27a871097b1a6de05d8eefac914c0efce005ed
1 /* Simple bitmaps.
2 Copyright (C) 1999, 2000, 2002, 2003 Free Software Foundation, Inc.
4 This file is part of GCC.
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.
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.
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. */
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"
30 /* Bitmap manipulation routines. */
32 /* Allocate a simple bitmap of N_ELMS bits. */
34 sbitmap
35 sbitmap_alloc (unsigned int n_elms)
37 unsigned int bytes, size, amt;
38 sbitmap bmap;
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;
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. */
55 sbitmap
56 sbitmap_resize (sbitmap bmap, unsigned int n_elms, int def)
58 unsigned int bytes, size, amt;
59 unsigned int last_bit;
61 size = SBITMAP_SET_SIZE (n_elms);
62 bytes = size * sizeof (SBITMAP_ELT_TYPE);
63 if (bytes > bmap->bytes)
65 amt = (sizeof (struct simple_bitmap_def)
66 + bytes - sizeof (SBITMAP_ELT_TYPE));
67 bmap = xrealloc (bmap, amt);
70 if (n_elms > bmap->n_bits)
72 if (def)
74 memset (bmap->elms + bmap->size, -1, bytes - bmap->bytes);
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));
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);
88 else
89 memset (bmap->elms + bmap->size, 0, bytes - bmap->bytes);
91 else if (n_elms < bmap->n_bits)
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);
100 bmap->n_bits = n_elms;
101 bmap->size = size;
102 bmap->bytes = bytes;
103 return bmap;
106 /* Allocate a vector of N_VECS bitmaps of N_ELMS bits. */
108 sbitmap *
109 sbitmap_vector_alloc (unsigned int n_vecs, unsigned int n_elms)
111 unsigned int i, bytes, offset, elm_bytes, size, amt, vector_bytes;
112 sbitmap *bitmap_vector;
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 *);
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. */
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);
132 amt = vector_bytes + (n_vecs * elm_bytes);
133 bitmap_vector = xmalloc (amt);
135 for (i = 0, offset = vector_bytes; i < n_vecs; i++, offset += elm_bytes)
137 sbitmap b = (sbitmap) ((char *) bitmap_vector + offset);
139 bitmap_vector[i] = b;
140 b->n_bits = n_elms;
141 b->size = size;
142 b->bytes = bytes;
145 return bitmap_vector;
148 /* Copy sbitmap SRC to DST. */
150 void
151 sbitmap_copy (sbitmap dst, sbitmap src)
153 memcpy (dst->elms, src->elms, sizeof (SBITMAP_ELT_TYPE) * dst->size);
156 /* Determine if a == b. */
158 sbitmap_equal (sbitmap a, sbitmap b)
160 return !memcmp (a->elms, b->elms, sizeof (SBITMAP_ELT_TYPE) * a->size);
163 /* Zero all elements in a bitmap. */
165 void
166 sbitmap_zero (sbitmap bmap)
168 memset (bmap->elms, 0, bmap->bytes);
171 /* Set all elements in a bitmap to ones. */
173 void
174 sbitmap_ones (sbitmap bmap)
176 unsigned int last_bit;
178 memset (bmap->elms, -1, bmap->bytes);
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);
186 /* Zero a vector of N_VECS bitmaps. */
188 void
189 sbitmap_vector_zero (sbitmap *bmap, unsigned int n_vecs)
191 unsigned int i;
193 for (i = 0; i < n_vecs; i++)
194 sbitmap_zero (bmap[i]);
197 /* Set a vector of N_VECS bitmaps to ones. */
199 void
200 sbitmap_vector_ones (sbitmap *bmap, unsigned int n_vecs)
202 unsigned int i;
204 for (i = 0; i < n_vecs; i++)
205 sbitmap_ones (bmap[i]);
208 /* Set DST to be A union (B - C).
209 DST = A | (B & ~C).
210 Returns true if any change is made. */
212 bool
213 sbitmap_union_of_diff_cg (sbitmap dst, sbitmap a, sbitmap b, sbitmap c)
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;
222 for (i = 0; i < n; i++)
224 SBITMAP_ELT_TYPE tmp = *ap++ | (*bp++ & ~*cp++);
225 changed |= *dstp ^ tmp;
226 *dstp++ = tmp;
229 return changed != 0;
232 void
233 sbitmap_union_of_diff (sbitmap dst, sbitmap a, sbitmap b, sbitmap c)
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;
241 for (i = 0; i < n; i++)
242 *dstp++ = *ap++ | (*bp++ & ~*cp++);
245 /* Set bitmap DST to the bitwise negation of the bitmap SRC. */
247 void
248 sbitmap_not (sbitmap dst, sbitmap src)
250 unsigned int i, n = dst->size;
251 sbitmap_ptr dstp = dst->elms;
252 sbitmap_ptr srcp = src->elms;
253 unsigned int last_bit;
255 for (i = 0; i < n; i++)
256 *dstp++ = ~*srcp++;
258 /* Zero all bits past n_bits, by ANDing dst with sbitmap_ones. */
259 last_bit = src->n_bits % SBITMAP_ELT_BITS;
260 if (last_bit)
261 dst->elms[n-1] = dst->elms[n-1]
262 & ((SBITMAP_ELT_TYPE)-1 >> (SBITMAP_ELT_BITS - last_bit));
265 /* Set the bits in DST to be the difference between the bits
266 in A and the bits in B. i.e. dst = a & (~b). */
268 void
269 sbitmap_difference (sbitmap dst, sbitmap a, sbitmap b)
271 unsigned int i, dst_size = dst->size;
272 unsigned int min_size = dst->size;
273 sbitmap_ptr dstp = dst->elms;
274 sbitmap_ptr ap = a->elms;
275 sbitmap_ptr bp = b->elms;
277 /* A should be at least as large as DEST, to have a defined source. */
278 if (a->size < dst_size)
279 abort ();
280 /* If minuend is smaller, we simply pretend it to be zero bits, i.e.
281 only copy the subtrahend into dest. */
282 if (b->size < min_size)
283 min_size = b->size;
284 for (i = 0; i < min_size; i++)
285 *dstp++ = *ap++ & (~*bp++);
286 /* Now fill the rest of dest from A, if B was too short.
287 This makes sense only when destination and A differ. */
288 if (dst != a && i != dst_size)
289 for (; i < dst_size; i++)
290 *dstp++ = *ap++;
293 /* Set DST to be (A and B).
294 Return nonzero if any change is made. */
296 bool
297 sbitmap_a_and_b_cg (sbitmap dst, sbitmap a, sbitmap b)
299 unsigned int i, n = dst->size;
300 sbitmap_ptr dstp = dst->elms;
301 sbitmap_ptr ap = a->elms;
302 sbitmap_ptr bp = b->elms;
303 SBITMAP_ELT_TYPE changed = 0;
305 for (i = 0; i < n; i++)
307 SBITMAP_ELT_TYPE tmp = *ap++ & *bp++;
308 changed |= *dstp ^ tmp;
309 *dstp++ = tmp;
312 return changed != 0;
315 void
316 sbitmap_a_and_b (sbitmap dst, sbitmap a, sbitmap b)
318 unsigned int i, n = dst->size;
319 sbitmap_ptr dstp = dst->elms;
320 sbitmap_ptr ap = a->elms;
321 sbitmap_ptr bp = b->elms;
323 for (i = 0; i < n; i++)
324 *dstp++ = *ap++ & *bp++;
327 /* Set DST to be (A xor B)).
328 Return nonzero if any change is made. */
330 bool
331 sbitmap_a_xor_b_cg (sbitmap dst, sbitmap a, sbitmap b)
333 unsigned int i, n = dst->size;
334 sbitmap_ptr dstp = dst->elms;
335 sbitmap_ptr ap = a->elms;
336 sbitmap_ptr bp = b->elms;
337 SBITMAP_ELT_TYPE changed = 0;
339 for (i = 0; i < n; i++)
341 SBITMAP_ELT_TYPE tmp = *ap++ ^ *bp++;
342 changed |= *dstp ^ tmp;
343 *dstp++ = tmp;
346 return changed != 0;
349 void
350 sbitmap_a_xor_b (sbitmap dst, sbitmap a, sbitmap b)
352 unsigned int i, n = dst->size;
353 sbitmap_ptr dstp = dst->elms;
354 sbitmap_ptr ap = a->elms;
355 sbitmap_ptr bp = b->elms;
357 for (i = 0; i < n; i++)
358 *dstp++ = *ap++ ^ *bp++;
361 /* Set DST to be (A or B)).
362 Return nonzero if any change is made. */
364 bool
365 sbitmap_a_or_b_cg (sbitmap dst, sbitmap a, sbitmap b)
367 unsigned int i, n = dst->size;
368 sbitmap_ptr dstp = dst->elms;
369 sbitmap_ptr ap = a->elms;
370 sbitmap_ptr bp = b->elms;
371 SBITMAP_ELT_TYPE changed = 0;
373 for (i = 0; i < n; i++)
375 SBITMAP_ELT_TYPE tmp = *ap++ | *bp++;
376 changed |= *dstp ^ tmp;
377 *dstp++ = tmp;
380 return changed != 0;
383 void
384 sbitmap_a_or_b (sbitmap dst, sbitmap a, sbitmap b)
386 unsigned int i, n = dst->size;
387 sbitmap_ptr dstp = dst->elms;
388 sbitmap_ptr ap = a->elms;
389 sbitmap_ptr bp = b->elms;
391 for (i = 0; i < n; i++)
392 *dstp++ = *ap++ | *bp++;
395 /* Return nonzero if A is a subset of B. */
397 bool
398 sbitmap_a_subset_b_p (sbitmap a, sbitmap b)
400 unsigned int i, n = a->size;
401 sbitmap_ptr ap, bp;
403 for (ap = a->elms, bp = b->elms, i = 0; i < n; i++, ap++, bp++)
404 if ((*ap | *bp) != *bp)
405 return false;
407 return true;
410 /* Set DST to be (A or (B and C)).
411 Return nonzero if any change is made. */
413 bool
414 sbitmap_a_or_b_and_c_cg (sbitmap dst, sbitmap a, sbitmap b, sbitmap c)
416 unsigned int i, n = dst->size;
417 sbitmap_ptr dstp = dst->elms;
418 sbitmap_ptr ap = a->elms;
419 sbitmap_ptr bp = b->elms;
420 sbitmap_ptr cp = c->elms;
421 SBITMAP_ELT_TYPE changed = 0;
423 for (i = 0; i < n; i++)
425 SBITMAP_ELT_TYPE tmp = *ap++ | (*bp++ & *cp++);
426 changed |= *dstp ^ tmp;
427 *dstp++ = tmp;
430 return changed != 0;
433 void
434 sbitmap_a_or_b_and_c (sbitmap dst, sbitmap a, sbitmap b, sbitmap c)
436 unsigned int i, n = dst->size;
437 sbitmap_ptr dstp = dst->elms;
438 sbitmap_ptr ap = a->elms;
439 sbitmap_ptr bp = b->elms;
440 sbitmap_ptr cp = c->elms;
442 for (i = 0; i < n; i++)
443 *dstp++ = *ap++ | (*bp++ & *cp++);
446 /* Set DST to be (A and (B or C)).
447 Return nonzero if any change is made. */
449 bool
450 sbitmap_a_and_b_or_c_cg (sbitmap dst, sbitmap a, sbitmap b, sbitmap c)
452 unsigned int i, n = dst->size;
453 sbitmap_ptr dstp = dst->elms;
454 sbitmap_ptr ap = a->elms;
455 sbitmap_ptr bp = b->elms;
456 sbitmap_ptr cp = c->elms;
457 SBITMAP_ELT_TYPE changed = 0;
459 for (i = 0; i < n; i++)
461 SBITMAP_ELT_TYPE tmp = *ap++ & (*bp++ | *cp++);
462 changed |= *dstp ^ tmp;
463 *dstp++ = tmp;
466 return changed != 0;
469 void
470 sbitmap_a_and_b_or_c (sbitmap dst, sbitmap a, sbitmap b, sbitmap c)
472 unsigned int i, n = dst->size;
473 sbitmap_ptr dstp = dst->elms;
474 sbitmap_ptr ap = a->elms;
475 sbitmap_ptr bp = b->elms;
476 sbitmap_ptr cp = c->elms;
478 for (i = 0; i < n; i++)
479 *dstp++ = *ap++ & (*bp++ | *cp++);
482 #ifdef IN_GCC
483 /* Set the bitmap DST to the intersection of SRC of successors of
484 block number BB, using the new flow graph structures. */
486 void
487 sbitmap_intersection_of_succs (sbitmap dst, sbitmap *src, int bb)
489 basic_block b = BASIC_BLOCK (bb);
490 unsigned int set_size = dst->size;
491 edge e;
493 for (e = b->succ; e != 0; e = e->succ_next)
495 if (e->dest == EXIT_BLOCK_PTR)
496 continue;
498 sbitmap_copy (dst, src[e->dest->index]);
499 break;
502 if (e == 0)
503 sbitmap_ones (dst);
504 else
505 for (e = e->succ_next; e != 0; e = e->succ_next)
507 unsigned int i;
508 sbitmap_ptr p, r;
510 if (e->dest == EXIT_BLOCK_PTR)
511 continue;
513 p = src[e->dest->index]->elms;
514 r = dst->elms;
515 for (i = 0; i < set_size; i++)
516 *r++ &= *p++;
520 /* Set the bitmap DST to the intersection of SRC of predecessors of
521 block number BB, using the new flow graph structures. */
523 void
524 sbitmap_intersection_of_preds (sbitmap dst, sbitmap *src, int bb)
526 basic_block b = BASIC_BLOCK (bb);
527 unsigned int set_size = dst->size;
528 edge e;
530 for (e = b->pred; e != 0; e = e->pred_next)
532 if (e->src == ENTRY_BLOCK_PTR)
533 continue;
535 sbitmap_copy (dst, src[e->src->index]);
536 break;
539 if (e == 0)
540 sbitmap_ones (dst);
541 else
542 for (e = e->pred_next; e != 0; e = e->pred_next)
544 unsigned int i;
545 sbitmap_ptr p, r;
547 if (e->src == ENTRY_BLOCK_PTR)
548 continue;
550 p = src[e->src->index]->elms;
551 r = dst->elms;
552 for (i = 0; i < set_size; i++)
553 *r++ &= *p++;
557 /* Set the bitmap DST to the union of SRC of successors of
558 block number BB, using the new flow graph structures. */
560 void
561 sbitmap_union_of_succs (sbitmap dst, sbitmap *src, int bb)
563 basic_block b = BASIC_BLOCK (bb);
564 unsigned int set_size = dst->size;
565 edge e;
567 for (e = b->succ; e != 0; e = e->succ_next)
569 if (e->dest == EXIT_BLOCK_PTR)
570 continue;
572 sbitmap_copy (dst, src[e->dest->index]);
573 break;
576 if (e == 0)
577 sbitmap_zero (dst);
578 else
579 for (e = e->succ_next; e != 0; e = e->succ_next)
581 unsigned int i;
582 sbitmap_ptr p, r;
584 if (e->dest == EXIT_BLOCK_PTR)
585 continue;
587 p = src[e->dest->index]->elms;
588 r = dst->elms;
589 for (i = 0; i < set_size; i++)
590 *r++ |= *p++;
594 /* Set the bitmap DST to the union of SRC of predecessors of
595 block number BB, using the new flow graph structures. */
597 void
598 sbitmap_union_of_preds (sbitmap dst, sbitmap *src, int bb)
600 basic_block b = BASIC_BLOCK (bb);
601 unsigned int set_size = dst->size;
602 edge e;
604 for (e = b->pred; e != 0; e = e->pred_next)
606 if (e->src== ENTRY_BLOCK_PTR)
607 continue;
609 sbitmap_copy (dst, src[e->src->index]);
610 break;
613 if (e == 0)
614 sbitmap_zero (dst);
615 else
616 for (e = e->pred_next; e != 0; e = e->pred_next)
618 unsigned int i;
619 sbitmap_ptr p, r;
621 if (e->src == ENTRY_BLOCK_PTR)
622 continue;
624 p = src[e->src->index]->elms;
625 r = dst->elms;
626 for (i = 0; i < set_size; i++)
627 *r++ |= *p++;
630 #endif
632 /* Return number of first bit set in the bitmap, -1 if none. */
635 sbitmap_first_set_bit (sbitmap bmap)
637 unsigned int n;
639 EXECUTE_IF_SET_IN_SBITMAP (bmap, 0, n, { return n; });
640 return -1;
643 /* Return number of last bit set in the bitmap, -1 if none. */
646 sbitmap_last_set_bit (sbitmap bmap)
648 int i;
649 SBITMAP_ELT_TYPE *ptr = bmap->elms;
651 for (i = bmap->size - 1; i >= 0; i--)
653 SBITMAP_ELT_TYPE word = ptr[i];
655 if (word != 0)
657 unsigned int index = (i + 1) * SBITMAP_ELT_BITS - 1;
658 SBITMAP_ELT_TYPE mask
659 = (SBITMAP_ELT_TYPE) 1 << (SBITMAP_ELT_BITS - 1);
661 while (1)
663 if ((word & mask) != 0)
664 return index;
666 mask >>= 1;
667 index--;
672 return -1;
675 void
676 dump_sbitmap (FILE *file, sbitmap bmap)
678 unsigned int i, n, j;
679 unsigned int set_size = bmap->size;
680 unsigned int total_bits = bmap->n_bits;
682 fprintf (file, " ");
683 for (i = n = 0; i < set_size && n < total_bits; i++)
684 for (j = 0; j < SBITMAP_ELT_BITS && n < total_bits; j++, n++)
686 if (n != 0 && n % 10 == 0)
687 fprintf (file, " ");
689 fprintf (file, "%d",
690 (bmap->elms[i] & ((SBITMAP_ELT_TYPE) 1 << j)) != 0);
693 fprintf (file, "\n");
696 void
697 dump_sbitmap_file (FILE *file, sbitmap bmap)
699 unsigned int i, pos;
701 fprintf (file, "n_bits = %d, set = {", bmap->n_bits);
703 for (pos = 30, i = 0; i < bmap->n_bits; i++)
704 if (TEST_BIT (bmap, i))
706 if (pos > 70)
708 fprintf (file, "\n ");
709 pos = 0;
712 fprintf (file, "%d ", i);
713 pos += 2 + (i >= 10) + (i >= 100) + (i >= 1000);
716 fprintf (file, "}\n");
719 void
720 debug_sbitmap (sbitmap bmap)
722 dump_sbitmap_file (stderr, bmap);
725 void
726 dump_sbitmap_vector (FILE *file, const char *title, const char *subtitle,
727 sbitmap *bmaps, int n_maps)
729 int bb;
731 fprintf (file, "%s\n", title);
732 for (bb = 0; bb < n_maps; bb++)
734 fprintf (file, "%s %d\n", subtitle, bb);
735 dump_sbitmap (file, bmaps[bb]);
738 fprintf (file, "\n");