* gfortran.h (struct gfc_symbol): Add equiv_built.
[official-gcc.git] / gcc / sbitmap.c
blobdfd764528cb05fe608cac46eb6958d03809f372c
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 /* Re-allocate a simple bitmap of N_ELMS bits. New storage is uninitialized. */
108 sbitmap
109 sbitmap_realloc (sbitmap src, unsigned int n_elms)
111 unsigned int bytes, size, amt;
112 sbitmap bmap;
114 size = SBITMAP_SET_SIZE (n_elms);
115 bytes = size * sizeof (SBITMAP_ELT_TYPE);
116 amt = (sizeof (struct simple_bitmap_def)
117 + bytes - sizeof (SBITMAP_ELT_TYPE));
119 if (src->bytes >= bytes)
121 src->n_bits = n_elms;
122 return src;
125 bmap = (sbitmap) xrealloc (src, amt);
126 bmap->n_bits = n_elms;
127 bmap->size = size;
128 bmap->bytes = bytes;
129 return bmap;
132 /* Allocate a vector of N_VECS bitmaps of N_ELMS bits. */
134 sbitmap *
135 sbitmap_vector_alloc (unsigned int n_vecs, unsigned int n_elms)
137 unsigned int i, bytes, offset, elm_bytes, size, amt, vector_bytes;
138 sbitmap *bitmap_vector;
140 size = SBITMAP_SET_SIZE (n_elms);
141 bytes = size * sizeof (SBITMAP_ELT_TYPE);
142 elm_bytes = (sizeof (struct simple_bitmap_def)
143 + bytes - sizeof (SBITMAP_ELT_TYPE));
144 vector_bytes = n_vecs * sizeof (sbitmap *);
146 /* Round up `vector_bytes' to account for the alignment requirements
147 of an sbitmap. One could allocate the vector-table and set of sbitmaps
148 separately, but that requires maintaining two pointers or creating
149 a cover struct to hold both pointers (so our result is still just
150 one pointer). Neither is a bad idea, but this is simpler for now. */
152 /* Based on DEFAULT_ALIGNMENT computation in obstack.c. */
153 struct { char x; SBITMAP_ELT_TYPE y; } align;
154 int alignment = (char *) & align.y - & align.x;
155 vector_bytes = (vector_bytes + alignment - 1) & ~ (alignment - 1);
158 amt = vector_bytes + (n_vecs * elm_bytes);
159 bitmap_vector = xmalloc (amt);
161 for (i = 0, offset = vector_bytes; i < n_vecs; i++, offset += elm_bytes)
163 sbitmap b = (sbitmap) ((char *) bitmap_vector + offset);
165 bitmap_vector[i] = b;
166 b->n_bits = n_elms;
167 b->size = size;
168 b->bytes = bytes;
171 return bitmap_vector;
174 /* Copy sbitmap SRC to DST. */
176 void
177 sbitmap_copy (sbitmap dst, sbitmap src)
179 memcpy (dst->elms, src->elms, sizeof (SBITMAP_ELT_TYPE) * dst->size);
182 /* Determine if a == b. */
184 sbitmap_equal (sbitmap a, sbitmap b)
186 return !memcmp (a->elms, b->elms, sizeof (SBITMAP_ELT_TYPE) * a->size);
189 /* Zero all elements in a bitmap. */
191 void
192 sbitmap_zero (sbitmap bmap)
194 memset (bmap->elms, 0, bmap->bytes);
197 /* Set all elements in a bitmap to ones. */
199 void
200 sbitmap_ones (sbitmap bmap)
202 unsigned int last_bit;
204 memset (bmap->elms, -1, bmap->bytes);
206 last_bit = bmap->n_bits % SBITMAP_ELT_BITS;
207 if (last_bit)
208 bmap->elms[bmap->size - 1]
209 = (SBITMAP_ELT_TYPE)-1 >> (SBITMAP_ELT_BITS - last_bit);
212 /* Zero a vector of N_VECS bitmaps. */
214 void
215 sbitmap_vector_zero (sbitmap *bmap, unsigned int n_vecs)
217 unsigned int i;
219 for (i = 0; i < n_vecs; i++)
220 sbitmap_zero (bmap[i]);
223 /* Set a vector of N_VECS bitmaps to ones. */
225 void
226 sbitmap_vector_ones (sbitmap *bmap, unsigned int n_vecs)
228 unsigned int i;
230 for (i = 0; i < n_vecs; i++)
231 sbitmap_ones (bmap[i]);
234 /* Set DST to be A union (B - C).
235 DST = A | (B & ~C).
236 Returns true if any change is made. */
238 bool
239 sbitmap_union_of_diff_cg (sbitmap dst, sbitmap a, sbitmap b, sbitmap c)
241 unsigned int i, n = dst->size;
242 sbitmap_ptr dstp = dst->elms;
243 sbitmap_ptr ap = a->elms;
244 sbitmap_ptr bp = b->elms;
245 sbitmap_ptr cp = c->elms;
246 SBITMAP_ELT_TYPE changed = 0;
248 for (i = 0; i < n; i++)
250 SBITMAP_ELT_TYPE tmp = *ap++ | (*bp++ & ~*cp++);
251 changed |= *dstp ^ tmp;
252 *dstp++ = tmp;
255 return changed != 0;
258 void
259 sbitmap_union_of_diff (sbitmap dst, sbitmap a, sbitmap b, sbitmap c)
261 unsigned int i, n = dst->size;
262 sbitmap_ptr dstp = dst->elms;
263 sbitmap_ptr ap = a->elms;
264 sbitmap_ptr bp = b->elms;
265 sbitmap_ptr cp = c->elms;
267 for (i = 0; i < n; i++)
268 *dstp++ = *ap++ | (*bp++ & ~*cp++);
271 /* Set bitmap DST to the bitwise negation of the bitmap SRC. */
273 void
274 sbitmap_not (sbitmap dst, sbitmap src)
276 unsigned int i, n = dst->size;
277 sbitmap_ptr dstp = dst->elms;
278 sbitmap_ptr srcp = src->elms;
279 unsigned int last_bit;
281 for (i = 0; i < n; i++)
282 *dstp++ = ~*srcp++;
284 /* Zero all bits past n_bits, by ANDing dst with sbitmap_ones. */
285 last_bit = src->n_bits % SBITMAP_ELT_BITS;
286 if (last_bit)
287 dst->elms[n-1] = dst->elms[n-1]
288 & ((SBITMAP_ELT_TYPE)-1 >> (SBITMAP_ELT_BITS - last_bit));
291 /* Set the bits in DST to be the difference between the bits
292 in A and the bits in B. i.e. dst = a & (~b). */
294 void
295 sbitmap_difference (sbitmap dst, sbitmap a, sbitmap b)
297 unsigned int i, dst_size = dst->size;
298 unsigned int min_size = dst->size;
299 sbitmap_ptr dstp = dst->elms;
300 sbitmap_ptr ap = a->elms;
301 sbitmap_ptr bp = b->elms;
303 /* A should be at least as large as DEST, to have a defined source. */
304 if (a->size < dst_size)
305 abort ();
306 /* If minuend is smaller, we simply pretend it to be zero bits, i.e.
307 only copy the subtrahend into dest. */
308 if (b->size < min_size)
309 min_size = b->size;
310 for (i = 0; i < min_size; i++)
311 *dstp++ = *ap++ & (~*bp++);
312 /* Now fill the rest of dest from A, if B was too short.
313 This makes sense only when destination and A differ. */
314 if (dst != a && i != dst_size)
315 for (; i < dst_size; i++)
316 *dstp++ = *ap++;
319 /* Set DST to be (A and B).
320 Return nonzero if any change is made. */
322 bool
323 sbitmap_a_and_b_cg (sbitmap dst, sbitmap a, sbitmap b)
325 unsigned int i, n = dst->size;
326 sbitmap_ptr dstp = dst->elms;
327 sbitmap_ptr ap = a->elms;
328 sbitmap_ptr bp = b->elms;
329 SBITMAP_ELT_TYPE changed = 0;
331 for (i = 0; i < n; i++)
333 SBITMAP_ELT_TYPE tmp = *ap++ & *bp++;
334 changed |= *dstp ^ tmp;
335 *dstp++ = tmp;
338 return changed != 0;
341 void
342 sbitmap_a_and_b (sbitmap dst, sbitmap a, sbitmap b)
344 unsigned int i, n = dst->size;
345 sbitmap_ptr dstp = dst->elms;
346 sbitmap_ptr ap = a->elms;
347 sbitmap_ptr bp = b->elms;
349 for (i = 0; i < n; i++)
350 *dstp++ = *ap++ & *bp++;
353 /* Set DST to be (A xor B)).
354 Return nonzero if any change is made. */
356 bool
357 sbitmap_a_xor_b_cg (sbitmap dst, sbitmap a, sbitmap b)
359 unsigned int i, n = dst->size;
360 sbitmap_ptr dstp = dst->elms;
361 sbitmap_ptr ap = a->elms;
362 sbitmap_ptr bp = b->elms;
363 SBITMAP_ELT_TYPE changed = 0;
365 for (i = 0; i < n; i++)
367 SBITMAP_ELT_TYPE tmp = *ap++ ^ *bp++;
368 changed |= *dstp ^ tmp;
369 *dstp++ = tmp;
372 return changed != 0;
375 void
376 sbitmap_a_xor_b (sbitmap dst, sbitmap a, sbitmap b)
378 unsigned int i, n = dst->size;
379 sbitmap_ptr dstp = dst->elms;
380 sbitmap_ptr ap = a->elms;
381 sbitmap_ptr bp = b->elms;
383 for (i = 0; i < n; i++)
384 *dstp++ = *ap++ ^ *bp++;
387 /* Set DST to be (A or B)).
388 Return nonzero if any change is made. */
390 bool
391 sbitmap_a_or_b_cg (sbitmap dst, sbitmap a, sbitmap b)
393 unsigned int i, n = dst->size;
394 sbitmap_ptr dstp = dst->elms;
395 sbitmap_ptr ap = a->elms;
396 sbitmap_ptr bp = b->elms;
397 SBITMAP_ELT_TYPE changed = 0;
399 for (i = 0; i < n; i++)
401 SBITMAP_ELT_TYPE tmp = *ap++ | *bp++;
402 changed |= *dstp ^ tmp;
403 *dstp++ = tmp;
406 return changed != 0;
409 void
410 sbitmap_a_or_b (sbitmap dst, sbitmap a, sbitmap b)
412 unsigned int i, n = dst->size;
413 sbitmap_ptr dstp = dst->elms;
414 sbitmap_ptr ap = a->elms;
415 sbitmap_ptr bp = b->elms;
417 for (i = 0; i < n; i++)
418 *dstp++ = *ap++ | *bp++;
421 /* Return nonzero if A is a subset of B. */
423 bool
424 sbitmap_a_subset_b_p (sbitmap a, sbitmap b)
426 unsigned int i, n = a->size;
427 sbitmap_ptr ap, bp;
429 for (ap = a->elms, bp = b->elms, i = 0; i < n; i++, ap++, bp++)
430 if ((*ap | *bp) != *bp)
431 return false;
433 return true;
436 /* Set DST to be (A or (B and C)).
437 Return nonzero if any change is made. */
439 bool
440 sbitmap_a_or_b_and_c_cg (sbitmap dst, sbitmap a, sbitmap b, sbitmap c)
442 unsigned int i, n = dst->size;
443 sbitmap_ptr dstp = dst->elms;
444 sbitmap_ptr ap = a->elms;
445 sbitmap_ptr bp = b->elms;
446 sbitmap_ptr cp = c->elms;
447 SBITMAP_ELT_TYPE changed = 0;
449 for (i = 0; i < n; i++)
451 SBITMAP_ELT_TYPE tmp = *ap++ | (*bp++ & *cp++);
452 changed |= *dstp ^ tmp;
453 *dstp++ = tmp;
456 return changed != 0;
459 void
460 sbitmap_a_or_b_and_c (sbitmap dst, sbitmap a, sbitmap b, sbitmap c)
462 unsigned int i, n = dst->size;
463 sbitmap_ptr dstp = dst->elms;
464 sbitmap_ptr ap = a->elms;
465 sbitmap_ptr bp = b->elms;
466 sbitmap_ptr cp = c->elms;
468 for (i = 0; i < n; i++)
469 *dstp++ = *ap++ | (*bp++ & *cp++);
472 /* Set DST to be (A and (B or C)).
473 Return nonzero if any change is made. */
475 bool
476 sbitmap_a_and_b_or_c_cg (sbitmap dst, sbitmap a, sbitmap b, sbitmap c)
478 unsigned int i, n = dst->size;
479 sbitmap_ptr dstp = dst->elms;
480 sbitmap_ptr ap = a->elms;
481 sbitmap_ptr bp = b->elms;
482 sbitmap_ptr cp = c->elms;
483 SBITMAP_ELT_TYPE changed = 0;
485 for (i = 0; i < n; i++)
487 SBITMAP_ELT_TYPE tmp = *ap++ & (*bp++ | *cp++);
488 changed |= *dstp ^ tmp;
489 *dstp++ = tmp;
492 return changed != 0;
495 void
496 sbitmap_a_and_b_or_c (sbitmap dst, sbitmap a, sbitmap b, sbitmap c)
498 unsigned int i, n = dst->size;
499 sbitmap_ptr dstp = dst->elms;
500 sbitmap_ptr ap = a->elms;
501 sbitmap_ptr bp = b->elms;
502 sbitmap_ptr cp = c->elms;
504 for (i = 0; i < n; i++)
505 *dstp++ = *ap++ & (*bp++ | *cp++);
508 #ifdef IN_GCC
509 /* Set the bitmap DST to the intersection of SRC of successors of
510 block number BB, using the new flow graph structures. */
512 void
513 sbitmap_intersection_of_succs (sbitmap dst, sbitmap *src, int bb)
515 basic_block b = BASIC_BLOCK (bb);
516 unsigned int set_size = dst->size;
517 edge e;
519 for (e = b->succ; e != 0; e = e->succ_next)
521 if (e->dest == EXIT_BLOCK_PTR)
522 continue;
524 sbitmap_copy (dst, src[e->dest->index]);
525 break;
528 if (e == 0)
529 sbitmap_ones (dst);
530 else
531 for (e = e->succ_next; e != 0; e = e->succ_next)
533 unsigned int i;
534 sbitmap_ptr p, r;
536 if (e->dest == EXIT_BLOCK_PTR)
537 continue;
539 p = src[e->dest->index]->elms;
540 r = dst->elms;
541 for (i = 0; i < set_size; i++)
542 *r++ &= *p++;
546 /* Set the bitmap DST to the intersection of SRC of predecessors of
547 block number BB, using the new flow graph structures. */
549 void
550 sbitmap_intersection_of_preds (sbitmap dst, sbitmap *src, int bb)
552 basic_block b = BASIC_BLOCK (bb);
553 unsigned int set_size = dst->size;
554 edge e;
556 for (e = b->pred; e != 0; e = e->pred_next)
558 if (e->src == ENTRY_BLOCK_PTR)
559 continue;
561 sbitmap_copy (dst, src[e->src->index]);
562 break;
565 if (e == 0)
566 sbitmap_ones (dst);
567 else
568 for (e = e->pred_next; e != 0; e = e->pred_next)
570 unsigned int i;
571 sbitmap_ptr p, r;
573 if (e->src == ENTRY_BLOCK_PTR)
574 continue;
576 p = src[e->src->index]->elms;
577 r = dst->elms;
578 for (i = 0; i < set_size; i++)
579 *r++ &= *p++;
583 /* Set the bitmap DST to the union of SRC of successors of
584 block number BB, using the new flow graph structures. */
586 void
587 sbitmap_union_of_succs (sbitmap dst, sbitmap *src, int bb)
589 basic_block b = BASIC_BLOCK (bb);
590 unsigned int set_size = dst->size;
591 edge e;
593 for (e = b->succ; e != 0; e = e->succ_next)
595 if (e->dest == EXIT_BLOCK_PTR)
596 continue;
598 sbitmap_copy (dst, src[e->dest->index]);
599 break;
602 if (e == 0)
603 sbitmap_zero (dst);
604 else
605 for (e = e->succ_next; e != 0; e = e->succ_next)
607 unsigned int i;
608 sbitmap_ptr p, r;
610 if (e->dest == EXIT_BLOCK_PTR)
611 continue;
613 p = src[e->dest->index]->elms;
614 r = dst->elms;
615 for (i = 0; i < set_size; i++)
616 *r++ |= *p++;
620 /* Set the bitmap DST to the union of SRC of predecessors of
621 block number BB, using the new flow graph structures. */
623 void
624 sbitmap_union_of_preds (sbitmap dst, sbitmap *src, int bb)
626 basic_block b = BASIC_BLOCK (bb);
627 unsigned int set_size = dst->size;
628 edge e;
630 for (e = b->pred; e != 0; e = e->pred_next)
632 if (e->src== ENTRY_BLOCK_PTR)
633 continue;
635 sbitmap_copy (dst, src[e->src->index]);
636 break;
639 if (e == 0)
640 sbitmap_zero (dst);
641 else
642 for (e = e->pred_next; e != 0; e = e->pred_next)
644 unsigned int i;
645 sbitmap_ptr p, r;
647 if (e->src == ENTRY_BLOCK_PTR)
648 continue;
650 p = src[e->src->index]->elms;
651 r = dst->elms;
652 for (i = 0; i < set_size; i++)
653 *r++ |= *p++;
656 #endif
658 /* Return number of first bit set in the bitmap, -1 if none. */
661 sbitmap_first_set_bit (sbitmap bmap)
663 unsigned int n;
665 EXECUTE_IF_SET_IN_SBITMAP (bmap, 0, n, { return n; });
666 return -1;
669 /* Return number of last bit set in the bitmap, -1 if none. */
672 sbitmap_last_set_bit (sbitmap bmap)
674 int i;
675 SBITMAP_ELT_TYPE *ptr = bmap->elms;
677 for (i = bmap->size - 1; i >= 0; i--)
679 SBITMAP_ELT_TYPE word = ptr[i];
681 if (word != 0)
683 unsigned int index = (i + 1) * SBITMAP_ELT_BITS - 1;
684 SBITMAP_ELT_TYPE mask
685 = (SBITMAP_ELT_TYPE) 1 << (SBITMAP_ELT_BITS - 1);
687 while (1)
689 if ((word & mask) != 0)
690 return index;
692 mask >>= 1;
693 index--;
698 return -1;
701 void
702 dump_sbitmap (FILE *file, sbitmap bmap)
704 unsigned int i, n, j;
705 unsigned int set_size = bmap->size;
706 unsigned int total_bits = bmap->n_bits;
708 fprintf (file, " ");
709 for (i = n = 0; i < set_size && n < total_bits; i++)
710 for (j = 0; j < SBITMAP_ELT_BITS && n < total_bits; j++, n++)
712 if (n != 0 && n % 10 == 0)
713 fprintf (file, " ");
715 fprintf (file, "%d",
716 (bmap->elms[i] & ((SBITMAP_ELT_TYPE) 1 << j)) != 0);
719 fprintf (file, "\n");
722 void
723 dump_sbitmap_file (FILE *file, sbitmap bmap)
725 unsigned int i, pos;
727 fprintf (file, "n_bits = %d, set = {", bmap->n_bits);
729 for (pos = 30, i = 0; i < bmap->n_bits; i++)
730 if (TEST_BIT (bmap, i))
732 if (pos > 70)
734 fprintf (file, "\n ");
735 pos = 0;
738 fprintf (file, "%d ", i);
739 pos += 2 + (i >= 10) + (i >= 100) + (i >= 1000);
742 fprintf (file, "}\n");
745 void
746 debug_sbitmap (sbitmap bmap)
748 dump_sbitmap_file (stderr, bmap);
751 void
752 dump_sbitmap_vector (FILE *file, const char *title, const char *subtitle,
753 sbitmap *bmaps, int n_maps)
755 int bb;
757 fprintf (file, "%s\n", title);
758 for (bb = 0; bb < n_maps; bb++)
760 fprintf (file, "%s %d\n", subtitle, bb);
761 dump_sbitmap (file, bmaps[bb]);
764 fprintf (file, "\n");