* config/avr/avr-protos.h (avr_mode_code_base_reg_class): New prototype.
[official-gcc.git] / gcc / sbitmap.c
blob23d459ec30935300c0926c0593dfa8adf4ae4f10
1 /* Simple bitmaps.
2 Copyright (C) 1999, 2000, 2002, 2003, 2004, 2006, 2007, 2008, 2010
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 "sbitmap.h"
26 #ifdef IN_GCC
27 /* FIXME: sbitmap is just a data structure, but we define dataflow functions
28 here also. This is conditional on IN_GCC (see second #ifdef IN_GCC
29 further down).
30 For now, also only conditionally include basic-block.h, but we should
31 find a better place for the dataflow functions. Perhaps cfganal.c? */
32 #include "basic-block.h"
33 #endif
35 #if GCC_VERSION >= 3400
36 # if HOST_BITS_PER_WIDEST_FAST_INT == HOST_BITS_PER_LONG
37 # define do_popcount(x) __builtin_popcountl(x)
38 # elif HOST_BITS_PER_WIDEST_FAST_INT == HOST_BITS_PER_LONGLONG
39 # define do_popcount(x) __builtin_popcountll(x)
40 # else
41 # error "internal error: sbitmap.h and hwint.h are inconsistent"
42 # endif
43 #else
44 static unsigned long sbitmap_elt_popcount (SBITMAP_ELT_TYPE);
45 # define do_popcount(x) sbitmap_elt_popcount((x))
46 #endif
48 typedef SBITMAP_ELT_TYPE *sbitmap_ptr;
49 typedef const SBITMAP_ELT_TYPE *const_sbitmap_ptr;
51 /* This macro controls debugging that is as expensive as the
52 operations it verifies. */
54 /* #define BITMAP_DEBUGGING */
55 #ifdef BITMAP_DEBUGGING
57 /* Verify the population count of sbitmap A matches the cached value,
58 if there is a cached value. */
60 void
61 sbitmap_verify_popcount (const_sbitmap a)
63 unsigned ix;
64 unsigned int lastword;
66 if (!a->popcount)
67 return;
69 lastword = a->size;
70 for (ix = 0; ix < lastword; ix++)
71 gcc_assert (a->popcount[ix] == do_popcount (a->elms[ix]));
73 #endif
75 /* Bitmap manipulation routines. */
77 /* Allocate a simple bitmap of N_ELMS bits. */
79 sbitmap
80 sbitmap_alloc (unsigned int n_elms)
82 unsigned int bytes, size, amt;
83 sbitmap bmap;
85 size = SBITMAP_SET_SIZE (n_elms);
86 bytes = size * sizeof (SBITMAP_ELT_TYPE);
87 amt = (sizeof (struct simple_bitmap_def)
88 + bytes - sizeof (SBITMAP_ELT_TYPE));
89 bmap = (sbitmap) xmalloc (amt);
90 bmap->n_bits = n_elms;
91 bmap->size = size;
92 bmap->popcount = NULL;
93 return bmap;
96 /* Allocate a simple bitmap of N_ELMS bits, and a popcount array. */
98 sbitmap
99 sbitmap_alloc_with_popcount (unsigned int n_elms)
101 sbitmap const bmap = sbitmap_alloc (n_elms);
102 bmap->popcount = XNEWVEC (unsigned char, bmap->size);
103 return bmap;
106 /* Resize a simple bitmap BMAP to N_ELMS bits. If increasing the
107 size of BMAP, clear the new bits to zero if the DEF argument
108 is zero, and set them to one otherwise. */
110 sbitmap
111 sbitmap_resize (sbitmap bmap, unsigned int n_elms, int def)
113 unsigned int bytes, size, amt;
114 unsigned int last_bit;
116 size = SBITMAP_SET_SIZE (n_elms);
117 bytes = size * sizeof (SBITMAP_ELT_TYPE);
118 if (bytes > SBITMAP_SIZE_BYTES (bmap))
120 amt = (sizeof (struct simple_bitmap_def)
121 + bytes - sizeof (SBITMAP_ELT_TYPE));
122 bmap = (sbitmap) xrealloc (bmap, amt);
123 if (bmap->popcount)
124 bmap->popcount = XRESIZEVEC (unsigned char, bmap->popcount, size);
127 if (n_elms > bmap->n_bits)
129 if (def)
131 memset (bmap->elms + bmap->size, -1,
132 bytes - SBITMAP_SIZE_BYTES (bmap));
134 /* Set the new bits if the original last element. */
135 last_bit = bmap->n_bits % SBITMAP_ELT_BITS;
136 if (last_bit)
137 bmap->elms[bmap->size - 1]
138 |= ~((SBITMAP_ELT_TYPE)-1 >> (SBITMAP_ELT_BITS - last_bit));
140 /* Clear the unused bit in the new last element. */
141 last_bit = n_elms % SBITMAP_ELT_BITS;
142 if (last_bit)
143 bmap->elms[size - 1]
144 &= (SBITMAP_ELT_TYPE)-1 >> (SBITMAP_ELT_BITS - last_bit);
146 else
148 memset (bmap->elms + bmap->size, 0,
149 bytes - SBITMAP_SIZE_BYTES (bmap));
150 if (bmap->popcount)
151 memset (bmap->popcount + bmap->size, 0,
152 (size * sizeof (unsigned char))
153 - (bmap->size * sizeof (unsigned char)));
157 else if (n_elms < bmap->n_bits)
159 /* Clear the surplus bits in the last word. */
160 last_bit = n_elms % SBITMAP_ELT_BITS;
161 if (last_bit)
163 bmap->elms[size - 1]
164 &= (SBITMAP_ELT_TYPE)-1 >> (SBITMAP_ELT_BITS - last_bit);
165 if (bmap->popcount)
166 bmap->popcount[size - 1] = do_popcount (bmap->elms[size - 1]);
170 bmap->n_bits = n_elms;
171 bmap->size = size;
172 return bmap;
175 /* Re-allocate a simple bitmap of N_ELMS bits. New storage is uninitialized. */
177 sbitmap
178 sbitmap_realloc (sbitmap src, unsigned int n_elms)
180 unsigned int bytes, size, amt;
181 sbitmap bmap;
183 size = SBITMAP_SET_SIZE (n_elms);
184 bytes = size * sizeof (SBITMAP_ELT_TYPE);
185 amt = (sizeof (struct simple_bitmap_def)
186 + bytes - sizeof (SBITMAP_ELT_TYPE));
188 if (SBITMAP_SIZE_BYTES (src) >= bytes)
190 src->n_bits = n_elms;
191 return src;
194 bmap = (sbitmap) xrealloc (src, amt);
195 bmap->n_bits = n_elms;
196 bmap->size = size;
197 return bmap;
200 /* Allocate a vector of N_VECS bitmaps of N_ELMS bits. */
202 sbitmap *
203 sbitmap_vector_alloc (unsigned int n_vecs, unsigned int n_elms)
205 unsigned int i, bytes, offset, elm_bytes, size, amt, vector_bytes;
206 sbitmap *bitmap_vector;
208 size = SBITMAP_SET_SIZE (n_elms);
209 bytes = size * sizeof (SBITMAP_ELT_TYPE);
210 elm_bytes = (sizeof (struct simple_bitmap_def)
211 + bytes - sizeof (SBITMAP_ELT_TYPE));
212 vector_bytes = n_vecs * sizeof (sbitmap *);
214 /* Round up `vector_bytes' to account for the alignment requirements
215 of an sbitmap. One could allocate the vector-table and set of sbitmaps
216 separately, but that requires maintaining two pointers or creating
217 a cover struct to hold both pointers (so our result is still just
218 one pointer). Neither is a bad idea, but this is simpler for now. */
220 /* Based on DEFAULT_ALIGNMENT computation in obstack.c. */
221 struct { char x; SBITMAP_ELT_TYPE y; } align;
222 int alignment = (char *) & align.y - & align.x;
223 vector_bytes = (vector_bytes + alignment - 1) & ~ (alignment - 1);
226 amt = vector_bytes + (n_vecs * elm_bytes);
227 bitmap_vector = (sbitmap *) xmalloc (amt);
229 for (i = 0, offset = vector_bytes; i < n_vecs; i++, offset += elm_bytes)
231 sbitmap b = (sbitmap) ((char *) bitmap_vector + offset);
233 bitmap_vector[i] = b;
234 b->n_bits = n_elms;
235 b->size = size;
236 b->popcount = NULL;
239 return bitmap_vector;
242 /* Copy sbitmap SRC to DST. */
244 void
245 sbitmap_copy (sbitmap dst, const_sbitmap src)
247 memcpy (dst->elms, src->elms, sizeof (SBITMAP_ELT_TYPE) * dst->size);
248 if (dst->popcount)
249 memcpy (dst->popcount, src->popcount, sizeof (unsigned char) * dst->size);
252 /* Copy the first N elements of sbitmap SRC to DST. */
254 void
255 sbitmap_copy_n (sbitmap dst, const_sbitmap src, unsigned int n)
257 memcpy (dst->elms, src->elms, sizeof (SBITMAP_ELT_TYPE) * n);
258 if (dst->popcount)
259 memcpy (dst->popcount, src->popcount, sizeof (unsigned char) * n);
262 /* Determine if a == b. */
264 sbitmap_equal (const_sbitmap a, const_sbitmap b)
266 return !memcmp (a->elms, b->elms, sizeof (SBITMAP_ELT_TYPE) * a->size);
269 /* Return true if the bitmap is empty. */
271 bool
272 sbitmap_empty_p (const_sbitmap bmap)
274 unsigned int i;
275 for (i=0; i<bmap->size; i++)
276 if (bmap->elms[i])
277 return false;
279 return true;
282 /* Return false if any of the N bits are set in MAP starting at
283 START. */
285 bool
286 sbitmap_range_empty_p (const_sbitmap bmap, unsigned int start, unsigned int n)
288 unsigned int i = start / SBITMAP_ELT_BITS;
289 SBITMAP_ELT_TYPE elm;
290 unsigned int shift = start % SBITMAP_ELT_BITS;
292 gcc_assert (bmap->n_bits >= start + n);
294 elm = bmap->elms[i];
295 elm = elm >> shift;
297 if (shift + n <= SBITMAP_ELT_BITS)
299 /* The bits are totally contained in a single element. */
300 if (shift + n < SBITMAP_ELT_BITS)
301 elm &= ((1 << n) - 1);
302 return (elm == 0);
305 if (elm)
306 return false;
308 n -= SBITMAP_ELT_BITS - shift;
309 i++;
311 /* Deal with full elts. */
312 while (n >= SBITMAP_ELT_BITS)
314 if (bmap->elms[i])
315 return false;
316 i++;
317 n -= SBITMAP_ELT_BITS;
320 /* The leftover bits. */
321 if (n)
323 elm = bmap->elms[i];
324 elm &= ((1 << n) - 1);
325 return (elm == 0);
328 return true;
333 /* Zero all elements in a bitmap. */
335 void
336 sbitmap_zero (sbitmap bmap)
338 memset (bmap->elms, 0, SBITMAP_SIZE_BYTES (bmap));
339 if (bmap->popcount)
340 memset (bmap->popcount, 0, bmap->size * sizeof (unsigned char));
343 /* Set all elements in a bitmap to ones. */
345 void
346 sbitmap_ones (sbitmap bmap)
348 unsigned int last_bit;
350 memset (bmap->elms, -1, SBITMAP_SIZE_BYTES (bmap));
351 if (bmap->popcount)
352 memset (bmap->popcount, -1, bmap->size * sizeof (unsigned char));
354 last_bit = bmap->n_bits % SBITMAP_ELT_BITS;
355 if (last_bit)
357 bmap->elms[bmap->size - 1]
358 = (SBITMAP_ELT_TYPE)-1 >> (SBITMAP_ELT_BITS - last_bit);
359 if (bmap->popcount)
360 bmap->popcount[bmap->size - 1]
361 = do_popcount (bmap->elms[bmap->size - 1]);
365 /* Zero a vector of N_VECS bitmaps. */
367 void
368 sbitmap_vector_zero (sbitmap *bmap, unsigned int n_vecs)
370 unsigned int i;
372 for (i = 0; i < n_vecs; i++)
373 sbitmap_zero (bmap[i]);
376 /* Set a vector of N_VECS bitmaps to ones. */
378 void
379 sbitmap_vector_ones (sbitmap *bmap, unsigned int n_vecs)
381 unsigned int i;
383 for (i = 0; i < n_vecs; i++)
384 sbitmap_ones (bmap[i]);
387 /* Set DST to be A union (B - C).
388 DST = A | (B & ~C).
389 Returns true if any change is made. */
391 bool
392 sbitmap_union_of_diff_cg (sbitmap dst, const_sbitmap a, const_sbitmap b, const_sbitmap c)
394 unsigned int i, n = dst->size;
395 sbitmap_ptr dstp = dst->elms;
396 const_sbitmap_ptr ap = a->elms;
397 const_sbitmap_ptr bp = b->elms;
398 const_sbitmap_ptr cp = c->elms;
399 SBITMAP_ELT_TYPE changed = 0;
401 gcc_assert (!dst->popcount);
403 for (i = 0; i < n; i++)
405 const SBITMAP_ELT_TYPE tmp = *ap++ | (*bp++ & ~*cp++);
406 changed |= *dstp ^ tmp;
407 *dstp++ = tmp;
410 return changed != 0;
413 void
414 sbitmap_union_of_diff (sbitmap dst, const_sbitmap a, const_sbitmap b, const_sbitmap c)
416 unsigned int i, n = dst->size;
417 sbitmap_ptr dstp = dst->elms;
418 const_sbitmap_ptr ap = a->elms;
419 const_sbitmap_ptr bp = b->elms;
420 const_sbitmap_ptr cp = c->elms;
422 gcc_assert (!dst->popcount && !a->popcount
423 && !b->popcount && !c->popcount);
425 for (i = 0; i < n; i++)
426 *dstp++ = *ap++ | (*bp++ & ~*cp++);
429 /* Set bitmap DST to the bitwise negation of the bitmap SRC. */
431 void
432 sbitmap_not (sbitmap dst, const_sbitmap src)
434 unsigned int i, n = dst->size;
435 sbitmap_ptr dstp = dst->elms;
436 const_sbitmap_ptr srcp = src->elms;
437 unsigned int last_bit;
439 gcc_assert (!dst->popcount);
441 for (i = 0; i < n; i++)
442 *dstp++ = ~*srcp++;
444 /* Zero all bits past n_bits, by ANDing dst with sbitmap_ones. */
445 last_bit = src->n_bits % SBITMAP_ELT_BITS;
446 if (last_bit)
447 dst->elms[n-1] = dst->elms[n-1]
448 & ((SBITMAP_ELT_TYPE)-1 >> (SBITMAP_ELT_BITS - last_bit));
451 /* Set the bits in DST to be the difference between the bits
452 in A and the bits in B. i.e. dst = a & (~b). */
454 void
455 sbitmap_difference (sbitmap dst, const_sbitmap a, const_sbitmap b)
457 unsigned int i, dst_size = dst->size;
458 unsigned int min_size = dst->size;
459 sbitmap_ptr dstp = dst->elms;
460 const_sbitmap_ptr ap = a->elms;
461 const_sbitmap_ptr bp = b->elms;
463 gcc_assert (!dst->popcount);
465 /* A should be at least as large as DEST, to have a defined source. */
466 gcc_assert (a->size >= dst_size);
467 /* If minuend is smaller, we simply pretend it to be zero bits, i.e.
468 only copy the subtrahend into dest. */
469 if (b->size < min_size)
470 min_size = b->size;
471 for (i = 0; i < min_size; i++)
472 *dstp++ = *ap++ & (~*bp++);
473 /* Now fill the rest of dest from A, if B was too short.
474 This makes sense only when destination and A differ. */
475 if (dst != a && i != dst_size)
476 for (; i < dst_size; i++)
477 *dstp++ = *ap++;
480 /* Return true if there are any bits set in A are also set in B.
481 Return false otherwise. */
483 bool
484 sbitmap_any_common_bits (const_sbitmap a, const_sbitmap b)
486 const_sbitmap_ptr ap = a->elms;
487 const_sbitmap_ptr bp = b->elms;
488 unsigned int i, n;
490 n = MIN (a->size, b->size);
491 for (i = 0; i < n; i++)
492 if ((*ap++ & *bp++) != 0)
493 return true;
495 return false;
498 /* Set DST to be (A and B).
499 Return nonzero if any change is made. */
501 bool
502 sbitmap_a_and_b_cg (sbitmap dst, const_sbitmap a, const_sbitmap b)
504 unsigned int i, n = dst->size;
505 sbitmap_ptr dstp = dst->elms;
506 const_sbitmap_ptr ap = a->elms;
507 const_sbitmap_ptr bp = b->elms;
508 SBITMAP_ELT_TYPE changed = 0;
510 gcc_assert (!dst->popcount);
512 for (i = 0; i < n; i++)
514 const SBITMAP_ELT_TYPE tmp = *ap++ & *bp++;
515 changed |= *dstp ^ tmp;
516 *dstp++ = tmp;
519 return changed != 0;
522 void
523 sbitmap_a_and_b (sbitmap dst, const_sbitmap a, const_sbitmap b)
525 unsigned int i, n = dst->size;
526 sbitmap_ptr dstp = dst->elms;
527 const_sbitmap_ptr ap = a->elms;
528 const_sbitmap_ptr bp = b->elms;
529 bool has_popcount = dst->popcount != NULL;
530 unsigned char *popcountp = dst->popcount;
532 for (i = 0; i < n; i++)
534 const SBITMAP_ELT_TYPE tmp = *ap++ & *bp++;
535 if (has_popcount)
537 bool wordchanged = (*dstp ^ tmp) != 0;
538 if (wordchanged)
539 *popcountp = do_popcount (tmp);
540 popcountp++;
542 *dstp++ = tmp;
544 #ifdef BITMAP_DEBUGGING
545 if (has_popcount)
546 sbitmap_verify_popcount (dst);
547 #endif
550 /* Set DST to be (A xor B)).
551 Return nonzero if any change is made. */
553 bool
554 sbitmap_a_xor_b_cg (sbitmap dst, const_sbitmap a, const_sbitmap b)
556 unsigned int i, n = dst->size;
557 sbitmap_ptr dstp = dst->elms;
558 const_sbitmap_ptr ap = a->elms;
559 const_sbitmap_ptr bp = b->elms;
560 SBITMAP_ELT_TYPE changed = 0;
562 gcc_assert (!dst->popcount);
564 for (i = 0; i < n; i++)
566 const SBITMAP_ELT_TYPE tmp = *ap++ ^ *bp++;
567 changed |= *dstp ^ tmp;
568 *dstp++ = tmp;
571 return changed != 0;
574 void
575 sbitmap_a_xor_b (sbitmap dst, const_sbitmap a, const_sbitmap b)
577 unsigned int i, n = dst->size;
578 sbitmap_ptr dstp = dst->elms;
579 const_sbitmap_ptr ap = a->elms;
580 const_sbitmap_ptr bp = b->elms;
581 bool has_popcount = dst->popcount != NULL;
582 unsigned char *popcountp = dst->popcount;
584 for (i = 0; i < n; i++)
586 const SBITMAP_ELT_TYPE tmp = *ap++ ^ *bp++;
587 if (has_popcount)
589 bool wordchanged = (*dstp ^ tmp) != 0;
590 if (wordchanged)
591 *popcountp = do_popcount (tmp);
592 popcountp++;
594 *dstp++ = tmp;
596 #ifdef BITMAP_DEBUGGING
597 if (has_popcount)
598 sbitmap_verify_popcount (dst);
599 #endif
602 /* Set DST to be (A or B)).
603 Return nonzero if any change is made. */
605 bool
606 sbitmap_a_or_b_cg (sbitmap dst, const_sbitmap a, const_sbitmap b)
608 unsigned int i, n = dst->size;
609 sbitmap_ptr dstp = dst->elms;
610 const_sbitmap_ptr ap = a->elms;
611 const_sbitmap_ptr bp = b->elms;
612 SBITMAP_ELT_TYPE changed = 0;
614 gcc_assert (!dst->popcount);
616 for (i = 0; i < n; i++)
618 const SBITMAP_ELT_TYPE tmp = *ap++ | *bp++;
619 changed |= *dstp ^ tmp;
620 *dstp++ = tmp;
623 return changed != 0;
626 void
627 sbitmap_a_or_b (sbitmap dst, const_sbitmap a, const_sbitmap b)
629 unsigned int i, n = dst->size;
630 sbitmap_ptr dstp = dst->elms;
631 const_sbitmap_ptr ap = a->elms;
632 const_sbitmap_ptr bp = b->elms;
633 bool has_popcount = dst->popcount != NULL;
634 unsigned char *popcountp = dst->popcount;
636 for (i = 0; i < n; i++)
638 const SBITMAP_ELT_TYPE tmp = *ap++ | *bp++;
639 if (has_popcount)
641 bool wordchanged = (*dstp ^ tmp) != 0;
642 if (wordchanged)
643 *popcountp = do_popcount (tmp);
644 popcountp++;
646 *dstp++ = tmp;
648 #ifdef BITMAP_DEBUGGING
649 if (has_popcount)
650 sbitmap_verify_popcount (dst);
651 #endif
654 /* Return nonzero if A is a subset of B. */
656 bool
657 sbitmap_a_subset_b_p (const_sbitmap a, const_sbitmap b)
659 unsigned int i, n = a->size;
660 const_sbitmap_ptr ap, bp;
662 for (ap = a->elms, bp = b->elms, i = 0; i < n; i++, ap++, bp++)
663 if ((*ap | *bp) != *bp)
664 return false;
666 return true;
669 /* Set DST to be (A or (B and C)).
670 Return nonzero if any change is made. */
672 bool
673 sbitmap_a_or_b_and_c_cg (sbitmap dst, const_sbitmap a, const_sbitmap b, const_sbitmap c)
675 unsigned int i, n = dst->size;
676 sbitmap_ptr dstp = dst->elms;
677 const_sbitmap_ptr ap = a->elms;
678 const_sbitmap_ptr bp = b->elms;
679 const_sbitmap_ptr cp = c->elms;
680 SBITMAP_ELT_TYPE changed = 0;
682 gcc_assert (!dst->popcount);
684 for (i = 0; i < n; i++)
686 const SBITMAP_ELT_TYPE tmp = *ap++ | (*bp++ & *cp++);
687 changed |= *dstp ^ tmp;
688 *dstp++ = tmp;
691 return changed != 0;
694 void
695 sbitmap_a_or_b_and_c (sbitmap dst, const_sbitmap a, const_sbitmap b, const_sbitmap c)
697 unsigned int i, n = dst->size;
698 sbitmap_ptr dstp = dst->elms;
699 const_sbitmap_ptr ap = a->elms;
700 const_sbitmap_ptr bp = b->elms;
701 const_sbitmap_ptr cp = c->elms;
703 gcc_assert (!dst->popcount);
705 for (i = 0; i < n; i++)
706 *dstp++ = *ap++ | (*bp++ & *cp++);
709 /* Set DST to be (A and (B or C)).
710 Return nonzero if any change is made. */
712 bool
713 sbitmap_a_and_b_or_c_cg (sbitmap dst, const_sbitmap a, const_sbitmap b, const_sbitmap c)
715 unsigned int i, n = dst->size;
716 sbitmap_ptr dstp = dst->elms;
717 const_sbitmap_ptr ap = a->elms;
718 const_sbitmap_ptr bp = b->elms;
719 const_sbitmap_ptr cp = c->elms;
720 SBITMAP_ELT_TYPE changed = 0;
722 gcc_assert (!dst->popcount);
724 for (i = 0; i < n; i++)
726 const SBITMAP_ELT_TYPE tmp = *ap++ & (*bp++ | *cp++);
727 changed |= *dstp ^ tmp;
728 *dstp++ = tmp;
731 return changed != 0;
734 void
735 sbitmap_a_and_b_or_c (sbitmap dst, const_sbitmap a, const_sbitmap b, const_sbitmap c)
737 unsigned int i, n = dst->size;
738 sbitmap_ptr dstp = dst->elms;
739 const_sbitmap_ptr ap = a->elms;
740 const_sbitmap_ptr bp = b->elms;
741 const_sbitmap_ptr cp = c->elms;
743 for (i = 0; i < n; i++)
744 *dstp++ = *ap++ & (*bp++ | *cp++);
747 #ifdef IN_GCC
748 /* FIXME: depends on basic-block.h, see comment at start of this file.
750 Ironically, the comments before the functions below suggest they do
751 dataflow using the "new flow graph structures", but that's the *old*
752 new data structures. The functions receive basic block numbers and
753 use BASIC_BLOCK(idx) to get the basic block. They should receive
754 the basic block directly, *sigh*. */
756 /* Set the bitmap DST to the intersection of SRC of successors of
757 block number BB, using the new flow graph structures. */
759 void
760 sbitmap_intersection_of_succs (sbitmap dst, sbitmap *src, int bb)
762 basic_block b = BASIC_BLOCK (bb);
763 unsigned int set_size = dst->size;
764 edge e;
765 unsigned ix;
767 gcc_assert (!dst->popcount);
769 for (e = NULL, ix = 0; ix < EDGE_COUNT (b->succs); ix++)
771 e = EDGE_SUCC (b, ix);
772 if (e->dest == EXIT_BLOCK_PTR)
773 continue;
775 sbitmap_copy (dst, src[e->dest->index]);
776 break;
779 if (e == 0)
780 sbitmap_ones (dst);
781 else
782 for (++ix; ix < EDGE_COUNT (b->succs); ix++)
784 unsigned int i;
785 sbitmap_ptr p, r;
787 e = EDGE_SUCC (b, ix);
788 if (e->dest == EXIT_BLOCK_PTR)
789 continue;
791 p = src[e->dest->index]->elms;
792 r = dst->elms;
793 for (i = 0; i < set_size; i++)
794 *r++ &= *p++;
798 /* Set the bitmap DST to the intersection of SRC of predecessors of
799 block number BB, using the new flow graph structures. */
801 void
802 sbitmap_intersection_of_preds (sbitmap dst, sbitmap *src, int bb)
804 basic_block b = BASIC_BLOCK (bb);
805 unsigned int set_size = dst->size;
806 edge e;
807 unsigned ix;
809 gcc_assert (!dst->popcount);
811 for (e = NULL, ix = 0; ix < EDGE_COUNT (b->preds); ix++)
813 e = EDGE_PRED (b, ix);
814 if (e->src == ENTRY_BLOCK_PTR)
815 continue;
817 sbitmap_copy (dst, src[e->src->index]);
818 break;
821 if (e == 0)
822 sbitmap_ones (dst);
823 else
824 for (++ix; ix < EDGE_COUNT (b->preds); ix++)
826 unsigned int i;
827 sbitmap_ptr p, r;
829 e = EDGE_PRED (b, ix);
830 if (e->src == ENTRY_BLOCK_PTR)
831 continue;
833 p = src[e->src->index]->elms;
834 r = dst->elms;
835 for (i = 0; i < set_size; i++)
836 *r++ &= *p++;
840 /* Set the bitmap DST to the union of SRC of successors of
841 block number BB, using the new flow graph structures. */
843 void
844 sbitmap_union_of_succs (sbitmap dst, sbitmap *src, int bb)
846 basic_block b = BASIC_BLOCK (bb);
847 unsigned int set_size = dst->size;
848 edge e;
849 unsigned ix;
851 gcc_assert (!dst->popcount);
853 for (ix = 0; ix < EDGE_COUNT (b->succs); ix++)
855 e = EDGE_SUCC (b, ix);
856 if (e->dest == EXIT_BLOCK_PTR)
857 continue;
859 sbitmap_copy (dst, src[e->dest->index]);
860 break;
863 if (ix == EDGE_COUNT (b->succs))
864 sbitmap_zero (dst);
865 else
866 for (ix++; ix < EDGE_COUNT (b->succs); ix++)
868 unsigned int i;
869 sbitmap_ptr p, r;
871 e = EDGE_SUCC (b, ix);
872 if (e->dest == EXIT_BLOCK_PTR)
873 continue;
875 p = src[e->dest->index]->elms;
876 r = dst->elms;
877 for (i = 0; i < set_size; i++)
878 *r++ |= *p++;
882 /* Set the bitmap DST to the union of SRC of predecessors of
883 block number BB, using the new flow graph structures. */
885 void
886 sbitmap_union_of_preds (sbitmap dst, sbitmap *src, int bb)
888 basic_block b = BASIC_BLOCK (bb);
889 unsigned int set_size = dst->size;
890 edge e;
891 unsigned ix;
893 gcc_assert (!dst->popcount);
895 for (ix = 0; ix < EDGE_COUNT (b->preds); ix++)
897 e = EDGE_PRED (b, ix);
898 if (e->src== ENTRY_BLOCK_PTR)
899 continue;
901 sbitmap_copy (dst, src[e->src->index]);
902 break;
905 if (ix == EDGE_COUNT (b->preds))
906 sbitmap_zero (dst);
907 else
908 for (ix++; ix < EDGE_COUNT (b->preds); ix++)
910 unsigned int i;
911 sbitmap_ptr p, r;
913 e = EDGE_PRED (b, ix);
914 if (e->src == ENTRY_BLOCK_PTR)
915 continue;
917 p = src[e->src->index]->elms;
918 r = dst->elms;
919 for (i = 0; i < set_size; i++)
920 *r++ |= *p++;
923 #endif
925 /* Return number of first bit set in the bitmap, -1 if none. */
928 sbitmap_first_set_bit (const_sbitmap bmap)
930 unsigned int n = 0;
931 sbitmap_iterator sbi;
933 EXECUTE_IF_SET_IN_SBITMAP (bmap, 0, n, sbi)
934 return n;
935 return -1;
938 /* Return number of last bit set in the bitmap, -1 if none. */
941 sbitmap_last_set_bit (const_sbitmap bmap)
943 int i;
944 const SBITMAP_ELT_TYPE *const ptr = bmap->elms;
946 for (i = bmap->size - 1; i >= 0; i--)
948 const SBITMAP_ELT_TYPE word = ptr[i];
950 if (word != 0)
952 unsigned int index = (i + 1) * SBITMAP_ELT_BITS - 1;
953 SBITMAP_ELT_TYPE mask
954 = (SBITMAP_ELT_TYPE) 1 << (SBITMAP_ELT_BITS - 1);
956 while (1)
958 if ((word & mask) != 0)
959 return index;
961 mask >>= 1;
962 index--;
967 return -1;
970 void
971 dump_sbitmap (FILE *file, const_sbitmap bmap)
973 unsigned int i, n, j;
974 unsigned int set_size = bmap->size;
975 unsigned int total_bits = bmap->n_bits;
977 fprintf (file, " ");
978 for (i = n = 0; i < set_size && n < total_bits; i++)
979 for (j = 0; j < SBITMAP_ELT_BITS && n < total_bits; j++, n++)
981 if (n != 0 && n % 10 == 0)
982 fprintf (file, " ");
984 fprintf (file, "%d",
985 (bmap->elms[i] & ((SBITMAP_ELT_TYPE) 1 << j)) != 0);
988 fprintf (file, "\n");
991 void
992 dump_sbitmap_file (FILE *file, const_sbitmap bmap)
994 unsigned int i, pos;
996 fprintf (file, "n_bits = %d, set = {", bmap->n_bits);
998 for (pos = 30, i = 0; i < bmap->n_bits; i++)
999 if (TEST_BIT (bmap, i))
1001 if (pos > 70)
1003 fprintf (file, "\n ");
1004 pos = 0;
1007 fprintf (file, "%d ", i);
1008 pos += 2 + (i >= 10) + (i >= 100) + (i >= 1000);
1011 fprintf (file, "}\n");
1014 DEBUG_FUNCTION void
1015 debug_sbitmap (const_sbitmap bmap)
1017 dump_sbitmap_file (stderr, bmap);
1020 void
1021 dump_sbitmap_vector (FILE *file, const char *title, const char *subtitle,
1022 sbitmap *bmaps, int n_maps)
1024 int bb;
1026 fprintf (file, "%s\n", title);
1027 for (bb = 0; bb < n_maps; bb++)
1029 fprintf (file, "%s %d\n", subtitle, bb);
1030 dump_sbitmap (file, bmaps[bb]);
1033 fprintf (file, "\n");
1036 #if GCC_VERSION < 3400
1037 /* Table of number of set bits in a character, indexed by value of char. */
1038 static const unsigned char popcount_table[] =
1040 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,
1041 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,
1042 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,
1043 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,
1044 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,
1045 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,
1046 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,
1047 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,
1050 /* Count the bits in an SBITMAP element A. */
1052 static unsigned long
1053 sbitmap_elt_popcount (SBITMAP_ELT_TYPE a)
1055 unsigned long ret = 0;
1056 unsigned i;
1058 if (a == 0)
1059 return 0;
1061 /* Just do this the table way for now */
1062 for (i = 0; i < SBITMAP_ELT_BITS; i += 8)
1063 ret += popcount_table[(a >> i) & 0xff];
1064 return ret;
1066 #endif
1068 /* Count the number of bits in SBITMAP a, up to bit MAXBIT. */
1070 unsigned long
1071 sbitmap_popcount (const_sbitmap a, unsigned long maxbit)
1073 unsigned long count = 0;
1074 unsigned ix;
1075 unsigned int lastword;
1077 if (maxbit == 0)
1078 return 0;
1080 if (maxbit >= a->n_bits)
1081 maxbit = a->n_bits;
1083 /* Count the bits in the full word. */
1084 lastword = MIN (a->size, SBITMAP_SET_SIZE (maxbit + 1) - 1);
1085 for (ix = 0; ix < lastword; ix++)
1087 if (a->popcount)
1089 count += a->popcount[ix];
1090 #ifdef BITMAP_DEBUGGING
1091 gcc_assert (a->popcount[ix] == do_popcount (a->elms[ix]));
1092 #endif
1094 else
1095 count += do_popcount (a->elms[ix]);
1098 /* Count the remaining bits. */
1099 if (lastword < a->size)
1101 unsigned int bitindex;
1102 SBITMAP_ELT_TYPE theword = a->elms[lastword];
1104 bitindex = maxbit % SBITMAP_ELT_BITS;
1105 if (bitindex != 0)
1107 theword &= (SBITMAP_ELT_TYPE)-1 >> (SBITMAP_ELT_BITS - bitindex);
1108 count += do_popcount (theword);
1111 return count;