2009-08-05 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / sbitmap.c
blob689cf97557668b26ac4062b45b9ae83976dd5e0e
1 /* Simple bitmaps.
2 Copyright (C) 1999, 2000, 2002, 2003, 2004, 2006, 2007, 2008
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
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 "obstack.h"
29 #include "basic-block.h"
31 #if GCC_VERSION >= 3400
32 #if HOST_BITS_PER_WIDEST_FAST_INT == HOST_BITS_PER_LONG
33 #define do_popcount(x) __builtin_popcountl(x)
34 #elif HOST_BITS_PER_WIDEST_FAST_INT == HOST_BITS_PER_LONGLONG
35 #define do_popcount(x) __builtin_popcountll(x)
36 #else
37 #error "internal error: sbitmap.h and hwint.h are inconsistent"
38 #endif
39 #else
40 static unsigned long sbitmap_elt_popcount (SBITMAP_ELT_TYPE);
41 #define do_popcount(x) sbitmap_elt_popcount((x))
42 #endif
44 /* This macro controls debugging that is as expensive as the
45 operations it verifies. */
47 /* #define BITMAP_DEBUGGING */
48 #ifdef BITMAP_DEBUGGING
50 /* Verify the population count of sbitmap A matches the cached value,
51 if there is a cached value. */
53 void
54 sbitmap_verify_popcount (const_sbitmap a)
56 unsigned ix;
57 unsigned int lastword;
59 if (!a->popcount)
60 return;
62 lastword = a->size;
63 for (ix = 0; ix < lastword; ix++)
64 gcc_assert (a->popcount[ix] == do_popcount (a->elms[ix]));
66 #endif
68 /* Bitmap manipulation routines. */
70 /* Allocate a simple bitmap of N_ELMS bits. */
72 sbitmap
73 sbitmap_alloc (unsigned int n_elms)
75 unsigned int bytes, size, amt;
76 sbitmap bmap;
78 size = SBITMAP_SET_SIZE (n_elms);
79 bytes = size * sizeof (SBITMAP_ELT_TYPE);
80 amt = (sizeof (struct simple_bitmap_def)
81 + bytes - sizeof (SBITMAP_ELT_TYPE));
82 bmap = (sbitmap) xmalloc (amt);
83 bmap->n_bits = n_elms;
84 bmap->size = size;
85 bmap->popcount = NULL;
86 return bmap;
89 /* Allocate a simple bitmap of N_ELMS bits, and a popcount array. */
91 sbitmap
92 sbitmap_alloc_with_popcount (unsigned int n_elms)
94 sbitmap const bmap = sbitmap_alloc (n_elms);
95 bmap->popcount = XNEWVEC (unsigned char, bmap->size);
96 return bmap;
99 /* Resize a simple bitmap BMAP to N_ELMS bits. If increasing the
100 size of BMAP, clear the new bits to zero if the DEF argument
101 is zero, and set them to one otherwise. */
103 sbitmap
104 sbitmap_resize (sbitmap bmap, unsigned int n_elms, int def)
106 unsigned int bytes, size, amt;
107 unsigned int last_bit;
109 size = SBITMAP_SET_SIZE (n_elms);
110 bytes = size * sizeof (SBITMAP_ELT_TYPE);
111 if (bytes > SBITMAP_SIZE_BYTES (bmap))
113 amt = (sizeof (struct simple_bitmap_def)
114 + bytes - sizeof (SBITMAP_ELT_TYPE));
115 bmap = (sbitmap) xrealloc (bmap, amt);
116 if (bmap->popcount)
117 bmap->popcount = XRESIZEVEC (unsigned char, bmap->popcount, size);
120 if (n_elms > bmap->n_bits)
122 if (def)
124 memset (bmap->elms + bmap->size, -1,
125 bytes - SBITMAP_SIZE_BYTES (bmap));
127 /* Set the new bits if the original last element. */
128 last_bit = bmap->n_bits % SBITMAP_ELT_BITS;
129 if (last_bit)
130 bmap->elms[bmap->size - 1]
131 |= ~((SBITMAP_ELT_TYPE)-1 >> (SBITMAP_ELT_BITS - last_bit));
133 /* Clear the unused bit in the new last element. */
134 last_bit = n_elms % SBITMAP_ELT_BITS;
135 if (last_bit)
136 bmap->elms[size - 1]
137 &= (SBITMAP_ELT_TYPE)-1 >> (SBITMAP_ELT_BITS - last_bit);
139 else
141 memset (bmap->elms + bmap->size, 0,
142 bytes - SBITMAP_SIZE_BYTES (bmap));
143 if (bmap->popcount)
144 memset (bmap->popcount + bmap->size, 0,
145 (size * sizeof (unsigned char))
146 - (bmap->size * sizeof (unsigned char)));
150 else if (n_elms < bmap->n_bits)
152 /* Clear the surplus bits in the last word. */
153 last_bit = n_elms % SBITMAP_ELT_BITS;
154 if (last_bit)
156 bmap->elms[size - 1]
157 &= (SBITMAP_ELT_TYPE)-1 >> (SBITMAP_ELT_BITS - last_bit);
158 if (bmap->popcount)
159 bmap->popcount[size - 1] = do_popcount (bmap->elms[size - 1]);
163 bmap->n_bits = n_elms;
164 bmap->size = size;
165 return bmap;
168 /* Re-allocate a simple bitmap of N_ELMS bits. New storage is uninitialized. */
170 sbitmap
171 sbitmap_realloc (sbitmap src, unsigned int n_elms)
173 unsigned int bytes, size, amt;
174 sbitmap bmap;
176 size = SBITMAP_SET_SIZE (n_elms);
177 bytes = size * sizeof (SBITMAP_ELT_TYPE);
178 amt = (sizeof (struct simple_bitmap_def)
179 + bytes - sizeof (SBITMAP_ELT_TYPE));
181 if (SBITMAP_SIZE_BYTES (src) >= bytes)
183 src->n_bits = n_elms;
184 return src;
187 bmap = (sbitmap) xrealloc (src, amt);
188 bmap->n_bits = n_elms;
189 bmap->size = size;
190 return bmap;
193 /* Allocate a vector of N_VECS bitmaps of N_ELMS bits. */
195 sbitmap *
196 sbitmap_vector_alloc (unsigned int n_vecs, unsigned int n_elms)
198 unsigned int i, bytes, offset, elm_bytes, size, amt, vector_bytes;
199 sbitmap *bitmap_vector;
201 size = SBITMAP_SET_SIZE (n_elms);
202 bytes = size * sizeof (SBITMAP_ELT_TYPE);
203 elm_bytes = (sizeof (struct simple_bitmap_def)
204 + bytes - sizeof (SBITMAP_ELT_TYPE));
205 vector_bytes = n_vecs * sizeof (sbitmap *);
207 /* Round up `vector_bytes' to account for the alignment requirements
208 of an sbitmap. One could allocate the vector-table and set of sbitmaps
209 separately, but that requires maintaining two pointers or creating
210 a cover struct to hold both pointers (so our result is still just
211 one pointer). Neither is a bad idea, but this is simpler for now. */
213 /* Based on DEFAULT_ALIGNMENT computation in obstack.c. */
214 struct { char x; SBITMAP_ELT_TYPE y; } align;
215 int alignment = (char *) & align.y - & align.x;
216 vector_bytes = (vector_bytes + alignment - 1) & ~ (alignment - 1);
219 amt = vector_bytes + (n_vecs * elm_bytes);
220 bitmap_vector = (sbitmap *) xmalloc (amt);
222 for (i = 0, offset = vector_bytes; i < n_vecs; i++, offset += elm_bytes)
224 sbitmap b = (sbitmap) ((char *) bitmap_vector + offset);
226 bitmap_vector[i] = b;
227 b->n_bits = n_elms;
228 b->size = size;
229 b->popcount = NULL;
232 return bitmap_vector;
235 /* Copy sbitmap SRC to DST. */
237 void
238 sbitmap_copy (sbitmap dst, const_sbitmap src)
240 memcpy (dst->elms, src->elms, sizeof (SBITMAP_ELT_TYPE) * dst->size);
241 if (dst->popcount)
242 memcpy (dst->popcount, src->popcount, sizeof (unsigned char) * dst->size);
245 /* Copy the first N elements of sbitmap SRC to DST. */
247 void
248 sbitmap_copy_n (sbitmap dst, const_sbitmap src, unsigned int n)
250 memcpy (dst->elms, src->elms, sizeof (SBITMAP_ELT_TYPE) * n);
251 if (dst->popcount)
252 memcpy (dst->popcount, src->popcount, sizeof (unsigned char) * n);
255 /* Determine if a == b. */
257 sbitmap_equal (const_sbitmap a, const_sbitmap b)
259 return !memcmp (a->elms, b->elms, sizeof (SBITMAP_ELT_TYPE) * a->size);
262 /* Return true if the bitmap is empty. */
264 bool
265 sbitmap_empty_p (const_sbitmap bmap)
267 unsigned int i;
268 for (i=0; i<bmap->size; i++)
269 if (bmap->elms[i])
270 return false;
272 return true;
275 /* Return false if any of the N bits are set in MAP starting at
276 START. */
278 bool
279 sbitmap_range_empty_p (const_sbitmap bmap, unsigned int start, unsigned int n)
281 unsigned int i = start / SBITMAP_ELT_BITS;
282 SBITMAP_ELT_TYPE elm;
283 unsigned int shift = start % SBITMAP_ELT_BITS;
285 gcc_assert (bmap->n_bits >= start + n);
287 elm = bmap->elms[i];
288 elm = elm >> shift;
290 if (shift + n <= SBITMAP_ELT_BITS)
292 /* The bits are totally contained in a single element. */
293 if (shift + n < SBITMAP_ELT_BITS)
294 elm &= ((1 << n) - 1);
295 return (elm == 0);
298 if (elm)
299 return false;
301 n -= SBITMAP_ELT_BITS - shift;
302 i++;
304 /* Deal with full elts. */
305 while (n >= SBITMAP_ELT_BITS)
307 if (bmap->elms[i])
308 return false;
309 i++;
310 n -= SBITMAP_ELT_BITS;
313 /* The leftover bits. */
314 if (n)
316 elm = bmap->elms[i];
317 elm &= ((1 << n) - 1);
318 return (elm == 0);
321 return true;
326 /* Zero all elements in a bitmap. */
328 void
329 sbitmap_zero (sbitmap bmap)
331 memset (bmap->elms, 0, SBITMAP_SIZE_BYTES (bmap));
332 if (bmap->popcount)
333 memset (bmap->popcount, 0, bmap->size * sizeof (unsigned char));
336 /* Set all elements in a bitmap to ones. */
338 void
339 sbitmap_ones (sbitmap bmap)
341 unsigned int last_bit;
343 memset (bmap->elms, -1, SBITMAP_SIZE_BYTES (bmap));
344 if (bmap->popcount)
345 memset (bmap->popcount, -1, bmap->size * sizeof (unsigned char));
347 last_bit = bmap->n_bits % SBITMAP_ELT_BITS;
348 if (last_bit)
350 bmap->elms[bmap->size - 1]
351 = (SBITMAP_ELT_TYPE)-1 >> (SBITMAP_ELT_BITS - last_bit);
352 if (bmap->popcount)
353 bmap->popcount[bmap->size - 1]
354 = do_popcount (bmap->elms[bmap->size - 1]);
358 /* Zero a vector of N_VECS bitmaps. */
360 void
361 sbitmap_vector_zero (sbitmap *bmap, unsigned int n_vecs)
363 unsigned int i;
365 for (i = 0; i < n_vecs; i++)
366 sbitmap_zero (bmap[i]);
369 /* Set a vector of N_VECS bitmaps to ones. */
371 void
372 sbitmap_vector_ones (sbitmap *bmap, unsigned int n_vecs)
374 unsigned int i;
376 for (i = 0; i < n_vecs; i++)
377 sbitmap_ones (bmap[i]);
380 /* Set DST to be A union (B - C).
381 DST = A | (B & ~C).
382 Returns true if any change is made. */
384 bool
385 sbitmap_union_of_diff_cg (sbitmap dst, const_sbitmap a, const_sbitmap b, const_sbitmap c)
387 unsigned int i, n = dst->size;
388 sbitmap_ptr dstp = dst->elms;
389 const_sbitmap_ptr ap = a->elms;
390 const_sbitmap_ptr bp = b->elms;
391 const_sbitmap_ptr cp = c->elms;
392 SBITMAP_ELT_TYPE changed = 0;
394 gcc_assert (!dst->popcount);
396 for (i = 0; i < n; i++)
398 const SBITMAP_ELT_TYPE tmp = *ap++ | (*bp++ & ~*cp++);
399 changed |= *dstp ^ tmp;
400 *dstp++ = tmp;
403 return changed != 0;
406 void
407 sbitmap_union_of_diff (sbitmap dst, const_sbitmap a, const_sbitmap b, const_sbitmap c)
409 unsigned int i, n = dst->size;
410 sbitmap_ptr dstp = dst->elms;
411 const_sbitmap_ptr ap = a->elms;
412 const_sbitmap_ptr bp = b->elms;
413 const_sbitmap_ptr cp = c->elms;
415 gcc_assert (!dst->popcount && !a->popcount
416 && !b->popcount && !c->popcount);
418 for (i = 0; i < n; i++)
419 *dstp++ = *ap++ | (*bp++ & ~*cp++);
422 /* Set bitmap DST to the bitwise negation of the bitmap SRC. */
424 void
425 sbitmap_not (sbitmap dst, const_sbitmap src)
427 unsigned int i, n = dst->size;
428 sbitmap_ptr dstp = dst->elms;
429 const_sbitmap_ptr srcp = src->elms;
430 unsigned int last_bit;
432 gcc_assert (!dst->popcount);
434 for (i = 0; i < n; i++)
435 *dstp++ = ~*srcp++;
437 /* Zero all bits past n_bits, by ANDing dst with sbitmap_ones. */
438 last_bit = src->n_bits % SBITMAP_ELT_BITS;
439 if (last_bit)
440 dst->elms[n-1] = dst->elms[n-1]
441 & ((SBITMAP_ELT_TYPE)-1 >> (SBITMAP_ELT_BITS - last_bit));
444 /* Set the bits in DST to be the difference between the bits
445 in A and the bits in B. i.e. dst = a & (~b). */
447 void
448 sbitmap_difference (sbitmap dst, const_sbitmap a, const_sbitmap b)
450 unsigned int i, dst_size = dst->size;
451 unsigned int min_size = dst->size;
452 sbitmap_ptr dstp = dst->elms;
453 const_sbitmap_ptr ap = a->elms;
454 const_sbitmap_ptr bp = b->elms;
456 gcc_assert (!dst->popcount);
458 /* A should be at least as large as DEST, to have a defined source. */
459 gcc_assert (a->size >= dst_size);
460 /* If minuend is smaller, we simply pretend it to be zero bits, i.e.
461 only copy the subtrahend into dest. */
462 if (b->size < min_size)
463 min_size = b->size;
464 for (i = 0; i < min_size; i++)
465 *dstp++ = *ap++ & (~*bp++);
466 /* Now fill the rest of dest from A, if B was too short.
467 This makes sense only when destination and A differ. */
468 if (dst != a && i != dst_size)
469 for (; i < dst_size; i++)
470 *dstp++ = *ap++;
473 /* Return true if there are any bits set in A are also set in B.
474 Return false otherwise. */
476 bool
477 sbitmap_any_common_bits (const_sbitmap a, const_sbitmap b)
479 const_sbitmap_ptr ap = a->elms;
480 const_sbitmap_ptr bp = b->elms;
481 unsigned int i, n;
483 n = MIN (a->size, b->size);
484 for (i = 0; i < n; i++)
485 if ((*ap++ & *bp++) != 0)
486 return true;
488 return false;
491 /* Set DST to be (A and B).
492 Return nonzero if any change is made. */
494 bool
495 sbitmap_a_and_b_cg (sbitmap dst, const_sbitmap a, const_sbitmap b)
497 unsigned int i, n = dst->size;
498 sbitmap_ptr dstp = dst->elms;
499 const_sbitmap_ptr ap = a->elms;
500 const_sbitmap_ptr bp = b->elms;
501 SBITMAP_ELT_TYPE changed = 0;
503 gcc_assert (!dst->popcount);
505 for (i = 0; i < n; i++)
507 const SBITMAP_ELT_TYPE tmp = *ap++ & *bp++;
508 changed |= *dstp ^ tmp;
509 *dstp++ = tmp;
512 return changed != 0;
515 void
516 sbitmap_a_and_b (sbitmap dst, const_sbitmap a, const_sbitmap b)
518 unsigned int i, n = dst->size;
519 sbitmap_ptr dstp = dst->elms;
520 const_sbitmap_ptr ap = a->elms;
521 const_sbitmap_ptr bp = b->elms;
522 bool has_popcount = dst->popcount != NULL;
523 unsigned char *popcountp = dst->popcount;
525 for (i = 0; i < n; i++)
527 const SBITMAP_ELT_TYPE tmp = *ap++ & *bp++;
528 if (has_popcount)
530 bool wordchanged = (*dstp ^ tmp) != 0;
531 if (wordchanged)
532 *popcountp = do_popcount (tmp);
533 popcountp++;
535 *dstp++ = tmp;
537 #ifdef BITMAP_DEBUGGING
538 if (has_popcount)
539 sbitmap_verify_popcount (dst);
540 #endif
543 /* Set DST to be (A xor B)).
544 Return nonzero if any change is made. */
546 bool
547 sbitmap_a_xor_b_cg (sbitmap dst, const_sbitmap a, const_sbitmap b)
549 unsigned int i, n = dst->size;
550 sbitmap_ptr dstp = dst->elms;
551 const_sbitmap_ptr ap = a->elms;
552 const_sbitmap_ptr bp = b->elms;
553 SBITMAP_ELT_TYPE changed = 0;
555 gcc_assert (!dst->popcount);
557 for (i = 0; i < n; i++)
559 const SBITMAP_ELT_TYPE tmp = *ap++ ^ *bp++;
560 changed |= *dstp ^ tmp;
561 *dstp++ = tmp;
564 return changed != 0;
567 void
568 sbitmap_a_xor_b (sbitmap dst, const_sbitmap a, const_sbitmap b)
570 unsigned int i, n = dst->size;
571 sbitmap_ptr dstp = dst->elms;
572 const_sbitmap_ptr ap = a->elms;
573 const_sbitmap_ptr bp = b->elms;
574 bool has_popcount = dst->popcount != NULL;
575 unsigned char *popcountp = dst->popcount;
577 for (i = 0; i < n; i++)
579 const SBITMAP_ELT_TYPE tmp = *ap++ ^ *bp++;
580 if (has_popcount)
582 bool wordchanged = (*dstp ^ tmp) != 0;
583 if (wordchanged)
584 *popcountp = do_popcount (tmp);
585 popcountp++;
587 *dstp++ = tmp;
589 #ifdef BITMAP_DEBUGGING
590 if (has_popcount)
591 sbitmap_verify_popcount (dst);
592 #endif
595 /* Set DST to be (A or B)).
596 Return nonzero if any change is made. */
598 bool
599 sbitmap_a_or_b_cg (sbitmap dst, const_sbitmap a, const_sbitmap b)
601 unsigned int i, n = dst->size;
602 sbitmap_ptr dstp = dst->elms;
603 const_sbitmap_ptr ap = a->elms;
604 const_sbitmap_ptr bp = b->elms;
605 SBITMAP_ELT_TYPE changed = 0;
607 gcc_assert (!dst->popcount);
609 for (i = 0; i < n; i++)
611 const SBITMAP_ELT_TYPE tmp = *ap++ | *bp++;
612 changed |= *dstp ^ tmp;
613 *dstp++ = tmp;
616 return changed != 0;
619 void
620 sbitmap_a_or_b (sbitmap dst, const_sbitmap a, const_sbitmap b)
622 unsigned int i, n = dst->size;
623 sbitmap_ptr dstp = dst->elms;
624 const_sbitmap_ptr ap = a->elms;
625 const_sbitmap_ptr bp = b->elms;
626 bool has_popcount = dst->popcount != NULL;
627 unsigned char *popcountp = dst->popcount;
629 for (i = 0; i < n; i++)
631 const SBITMAP_ELT_TYPE tmp = *ap++ | *bp++;
632 if (has_popcount)
634 bool wordchanged = (*dstp ^ tmp) != 0;
635 if (wordchanged)
636 *popcountp = do_popcount (tmp);
637 popcountp++;
639 *dstp++ = tmp;
641 #ifdef BITMAP_DEBUGGING
642 if (has_popcount)
643 sbitmap_verify_popcount (dst);
644 #endif
647 /* Return nonzero if A is a subset of B. */
649 bool
650 sbitmap_a_subset_b_p (const_sbitmap a, const_sbitmap b)
652 unsigned int i, n = a->size;
653 const_sbitmap_ptr ap, bp;
655 for (ap = a->elms, bp = b->elms, i = 0; i < n; i++, ap++, bp++)
656 if ((*ap | *bp) != *bp)
657 return false;
659 return true;
662 /* Set DST to be (A or (B and C)).
663 Return nonzero if any change is made. */
665 bool
666 sbitmap_a_or_b_and_c_cg (sbitmap dst, const_sbitmap a, const_sbitmap b, const_sbitmap c)
668 unsigned int i, n = dst->size;
669 sbitmap_ptr dstp = dst->elms;
670 const_sbitmap_ptr ap = a->elms;
671 const_sbitmap_ptr bp = b->elms;
672 const_sbitmap_ptr cp = c->elms;
673 SBITMAP_ELT_TYPE changed = 0;
675 gcc_assert (!dst->popcount);
677 for (i = 0; i < n; i++)
679 const SBITMAP_ELT_TYPE tmp = *ap++ | (*bp++ & *cp++);
680 changed |= *dstp ^ tmp;
681 *dstp++ = tmp;
684 return changed != 0;
687 void
688 sbitmap_a_or_b_and_c (sbitmap dst, const_sbitmap a, const_sbitmap b, const_sbitmap c)
690 unsigned int i, n = dst->size;
691 sbitmap_ptr dstp = dst->elms;
692 const_sbitmap_ptr ap = a->elms;
693 const_sbitmap_ptr bp = b->elms;
694 const_sbitmap_ptr cp = c->elms;
696 gcc_assert (!dst->popcount);
698 for (i = 0; i < n; i++)
699 *dstp++ = *ap++ | (*bp++ & *cp++);
702 /* Set DST to be (A and (B or C)).
703 Return nonzero if any change is made. */
705 bool
706 sbitmap_a_and_b_or_c_cg (sbitmap dst, const_sbitmap a, const_sbitmap b, const_sbitmap c)
708 unsigned int i, n = dst->size;
709 sbitmap_ptr dstp = dst->elms;
710 const_sbitmap_ptr ap = a->elms;
711 const_sbitmap_ptr bp = b->elms;
712 const_sbitmap_ptr cp = c->elms;
713 SBITMAP_ELT_TYPE changed = 0;
715 gcc_assert (!dst->popcount);
717 for (i = 0; i < n; i++)
719 const SBITMAP_ELT_TYPE tmp = *ap++ & (*bp++ | *cp++);
720 changed |= *dstp ^ tmp;
721 *dstp++ = tmp;
724 return changed != 0;
727 void
728 sbitmap_a_and_b_or_c (sbitmap dst, const_sbitmap a, const_sbitmap b, const_sbitmap c)
730 unsigned int i, n = dst->size;
731 sbitmap_ptr dstp = dst->elms;
732 const_sbitmap_ptr ap = a->elms;
733 const_sbitmap_ptr bp = b->elms;
734 const_sbitmap_ptr cp = c->elms;
736 for (i = 0; i < n; i++)
737 *dstp++ = *ap++ & (*bp++ | *cp++);
740 #ifdef IN_GCC
741 /* Set the bitmap DST to the intersection of SRC of successors of
742 block number BB, using the new flow graph structures. */
744 void
745 sbitmap_intersection_of_succs (sbitmap dst, sbitmap *src, int bb)
747 basic_block b = BASIC_BLOCK (bb);
748 unsigned int set_size = dst->size;
749 edge e;
750 unsigned ix;
752 gcc_assert (!dst->popcount);
754 for (e = NULL, ix = 0; ix < EDGE_COUNT (b->succs); ix++)
756 e = EDGE_SUCC (b, ix);
757 if (e->dest == EXIT_BLOCK_PTR)
758 continue;
760 sbitmap_copy (dst, src[e->dest->index]);
761 break;
764 if (e == 0)
765 sbitmap_ones (dst);
766 else
767 for (++ix; ix < EDGE_COUNT (b->succs); ix++)
769 unsigned int i;
770 sbitmap_ptr p, r;
772 e = EDGE_SUCC (b, ix);
773 if (e->dest == EXIT_BLOCK_PTR)
774 continue;
776 p = src[e->dest->index]->elms;
777 r = dst->elms;
778 for (i = 0; i < set_size; i++)
779 *r++ &= *p++;
783 /* Set the bitmap DST to the intersection of SRC of predecessors of
784 block number BB, using the new flow graph structures. */
786 void
787 sbitmap_intersection_of_preds (sbitmap dst, sbitmap *src, int bb)
789 basic_block b = BASIC_BLOCK (bb);
790 unsigned int set_size = dst->size;
791 edge e;
792 unsigned ix;
794 gcc_assert (!dst->popcount);
796 for (e = NULL, ix = 0; ix < EDGE_COUNT (b->preds); ix++)
798 e = EDGE_PRED (b, ix);
799 if (e->src == ENTRY_BLOCK_PTR)
800 continue;
802 sbitmap_copy (dst, src[e->src->index]);
803 break;
806 if (e == 0)
807 sbitmap_ones (dst);
808 else
809 for (++ix; ix < EDGE_COUNT (b->preds); ix++)
811 unsigned int i;
812 sbitmap_ptr p, r;
814 e = EDGE_PRED (b, ix);
815 if (e->src == ENTRY_BLOCK_PTR)
816 continue;
818 p = src[e->src->index]->elms;
819 r = dst->elms;
820 for (i = 0; i < set_size; i++)
821 *r++ &= *p++;
825 /* Set the bitmap DST to the union of SRC of successors of
826 block number BB, using the new flow graph structures. */
828 void
829 sbitmap_union_of_succs (sbitmap dst, sbitmap *src, int bb)
831 basic_block b = BASIC_BLOCK (bb);
832 unsigned int set_size = dst->size;
833 edge e;
834 unsigned ix;
836 gcc_assert (!dst->popcount);
838 for (ix = 0; ix < EDGE_COUNT (b->succs); ix++)
840 e = EDGE_SUCC (b, ix);
841 if (e->dest == EXIT_BLOCK_PTR)
842 continue;
844 sbitmap_copy (dst, src[e->dest->index]);
845 break;
848 if (ix == EDGE_COUNT (b->succs))
849 sbitmap_zero (dst);
850 else
851 for (ix++; ix < EDGE_COUNT (b->succs); ix++)
853 unsigned int i;
854 sbitmap_ptr p, r;
856 e = EDGE_SUCC (b, ix);
857 if (e->dest == EXIT_BLOCK_PTR)
858 continue;
860 p = src[e->dest->index]->elms;
861 r = dst->elms;
862 for (i = 0; i < set_size; i++)
863 *r++ |= *p++;
867 /* Set the bitmap DST to the union of SRC of predecessors of
868 block number BB, using the new flow graph structures. */
870 void
871 sbitmap_union_of_preds (sbitmap dst, sbitmap *src, int bb)
873 basic_block b = BASIC_BLOCK (bb);
874 unsigned int set_size = dst->size;
875 edge e;
876 unsigned ix;
878 gcc_assert (!dst->popcount);
880 for (ix = 0; ix < EDGE_COUNT (b->preds); ix++)
882 e = EDGE_PRED (b, ix);
883 if (e->src== ENTRY_BLOCK_PTR)
884 continue;
886 sbitmap_copy (dst, src[e->src->index]);
887 break;
890 if (ix == EDGE_COUNT (b->preds))
891 sbitmap_zero (dst);
892 else
893 for (ix++; ix < EDGE_COUNT (b->preds); ix++)
895 unsigned int i;
896 sbitmap_ptr p, r;
898 e = EDGE_PRED (b, ix);
899 if (e->src == ENTRY_BLOCK_PTR)
900 continue;
902 p = src[e->src->index]->elms;
903 r = dst->elms;
904 for (i = 0; i < set_size; i++)
905 *r++ |= *p++;
908 #endif
910 /* Return number of first bit set in the bitmap, -1 if none. */
913 sbitmap_first_set_bit (const_sbitmap bmap)
915 unsigned int n = 0;
916 sbitmap_iterator sbi;
918 EXECUTE_IF_SET_IN_SBITMAP (bmap, 0, n, sbi)
919 return n;
920 return -1;
923 /* Return number of last bit set in the bitmap, -1 if none. */
926 sbitmap_last_set_bit (const_sbitmap bmap)
928 int i;
929 const SBITMAP_ELT_TYPE *const ptr = bmap->elms;
931 for (i = bmap->size - 1; i >= 0; i--)
933 const SBITMAP_ELT_TYPE word = ptr[i];
935 if (word != 0)
937 unsigned int index = (i + 1) * SBITMAP_ELT_BITS - 1;
938 SBITMAP_ELT_TYPE mask
939 = (SBITMAP_ELT_TYPE) 1 << (SBITMAP_ELT_BITS - 1);
941 while (1)
943 if ((word & mask) != 0)
944 return index;
946 mask >>= 1;
947 index--;
952 return -1;
955 void
956 dump_sbitmap (FILE *file, const_sbitmap bmap)
958 unsigned int i, n, j;
959 unsigned int set_size = bmap->size;
960 unsigned int total_bits = bmap->n_bits;
962 fprintf (file, " ");
963 for (i = n = 0; i < set_size && n < total_bits; i++)
964 for (j = 0; j < SBITMAP_ELT_BITS && n < total_bits; j++, n++)
966 if (n != 0 && n % 10 == 0)
967 fprintf (file, " ");
969 fprintf (file, "%d",
970 (bmap->elms[i] & ((SBITMAP_ELT_TYPE) 1 << j)) != 0);
973 fprintf (file, "\n");
976 void
977 dump_sbitmap_file (FILE *file, const_sbitmap bmap)
979 unsigned int i, pos;
981 fprintf (file, "n_bits = %d, set = {", bmap->n_bits);
983 for (pos = 30, i = 0; i < bmap->n_bits; i++)
984 if (TEST_BIT (bmap, i))
986 if (pos > 70)
988 fprintf (file, "\n ");
989 pos = 0;
992 fprintf (file, "%d ", i);
993 pos += 2 + (i >= 10) + (i >= 100) + (i >= 1000);
996 fprintf (file, "}\n");
999 void
1000 debug_sbitmap (const_sbitmap bmap)
1002 dump_sbitmap_file (stderr, bmap);
1005 void
1006 dump_sbitmap_vector (FILE *file, const char *title, const char *subtitle,
1007 sbitmap *bmaps, int n_maps)
1009 int bb;
1011 fprintf (file, "%s\n", title);
1012 for (bb = 0; bb < n_maps; bb++)
1014 fprintf (file, "%s %d\n", subtitle, bb);
1015 dump_sbitmap (file, bmaps[bb]);
1018 fprintf (file, "\n");
1021 #if GCC_VERSION < 3400
1022 /* Table of number of set bits in a character, indexed by value of char. */
1023 static const unsigned char popcount_table[] =
1025 0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
1026 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
1027 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
1028 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
1029 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
1030 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
1031 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
1032 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8,
1035 /* Count the bits in an SBITMAP element A. */
1037 static unsigned long
1038 sbitmap_elt_popcount (SBITMAP_ELT_TYPE a)
1040 unsigned long ret = 0;
1041 unsigned i;
1043 if (a == 0)
1044 return 0;
1046 /* Just do this the table way for now */
1047 for (i = 0; i < SBITMAP_ELT_BITS; i += 8)
1048 ret += popcount_table[(a >> i) & 0xff];
1049 return ret;
1051 #endif
1053 /* Count the number of bits in SBITMAP a, up to bit MAXBIT. */
1055 unsigned long
1056 sbitmap_popcount (const_sbitmap a, unsigned long maxbit)
1058 unsigned long count = 0;
1059 unsigned ix;
1060 unsigned int lastword;
1062 if (maxbit == 0)
1063 return 0;
1065 if (maxbit >= a->n_bits)
1066 maxbit = a->n_bits;
1068 /* Count the bits in the full word. */
1069 lastword = MIN (a->size, SBITMAP_SET_SIZE (maxbit + 1) - 1);
1070 for (ix = 0; ix < lastword; ix++)
1072 if (a->popcount)
1074 count += a->popcount[ix];
1075 #ifdef BITMAP_DEBUGGING
1076 gcc_assert (a->popcount[ix] == do_popcount (a->elms[ix]));
1077 #endif
1079 else
1080 count += do_popcount (a->elms[ix]);
1083 /* Count the remaining bits. */
1084 if (lastword < a->size)
1086 unsigned int bitindex;
1087 SBITMAP_ELT_TYPE theword = a->elms[lastword];
1089 bitindex = maxbit % SBITMAP_ELT_BITS;
1090 if (bitindex != 0)
1092 theword &= (SBITMAP_ELT_TYPE)-1 >> (SBITMAP_ELT_BITS - bitindex);
1093 count += do_popcount (theword);
1096 return count;