]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/sbitmap.c
Merge dataflow branch into mainline
[thirdparty/gcc.git] / gcc / sbitmap.c
CommitLineData
152bf224 1/* Simple bitmaps.
3072d30e 2 Copyright (C) 1999, 2000, 2002, 2003, 2004, 2006, 2007
3 Free Software Foundation, Inc.
152bf224 4
f12b58b3 5This file is part of GCC.
152bf224 6
f12b58b3 7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 2, or (at your option) any later
10version.
152bf224 11
f12b58b3 12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
152bf224 16
17You should have received a copy of the GNU General Public License
f12b58b3 18along with GCC; see the file COPYING. If not, write to the Free
67ce556b 19Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
2002110-1301, USA. */
152bf224 21
22#include "config.h"
23#include "system.h"
805e22b2 24#include "coretypes.h"
25#include "tm.h"
152bf224 26#include "rtl.h"
27#include "flags.h"
d6cb6164 28#include "hard-reg-set.h"
42fe97ed 29#include "obstack.h"
152bf224 30#include "basic-block.h"
31
8effb48f 32#if GCC_VERSION >= 3400
33#if HOST_BITS_PER_WIDEST_FAST_INT == HOST_BITS_PER_LONG
34#define do_popcount(x) __builtin_popcountl(x)
bf0e5798 35#elif HOST_BITS_PER_WIDEST_FAST_INT == HOST_BITS_PER_LONGLONG
8effb48f 36#define do_popcount(x) __builtin_popcountll(x)
37#else
38#error "internal error: sbitmap.h and hwint.h are inconsistent"
39#endif
40#else
41static unsigned long sbitmap_elt_popcount (SBITMAP_ELT_TYPE);
42#define do_popcount(x) sbitmap_elt_popcount((x))
43#endif
44
45/* This macro controls debugging that is as expensive as the
46 operations it verifies. */
47
48/* #define BITMAP_DEBUGGING */
49#ifdef BITMAP_DEBUGGING
50
51/* Verify the population count of sbitmap A matches the cached value,
52 if there is a cached value. */
53
54void
55sbitmap_verify_popcount (sbitmap a)
56{
57 unsigned ix;
58 unsigned int lastword;
59
60 if (!a->popcount)
61 return;
62
63 lastword = a->size;
64 for (ix = 0; ix < lastword; ix++)
65 gcc_assert (a->popcount[ix] == do_popcount (a->elms[ix]));
66}
67#endif
68
152bf224 69/* Bitmap manipulation routines. */
70
71/* Allocate a simple bitmap of N_ELMS bits. */
72
73sbitmap
60b8c5b3 74sbitmap_alloc (unsigned int n_elms)
152bf224 75{
43bf47e9 76 unsigned int bytes, size, amt;
152bf224 77 sbitmap bmap;
78
79 size = SBITMAP_SET_SIZE (n_elms);
80 bytes = size * sizeof (SBITMAP_ELT_TYPE);
81 amt = (sizeof (struct simple_bitmap_def)
82 + bytes - sizeof (SBITMAP_ELT_TYPE));
f0af5a88 83 bmap = xmalloc (amt);
152bf224 84 bmap->n_bits = n_elms;
85 bmap->size = size;
8effb48f 86 bmap->popcount = NULL;
87 return bmap;
88}
89
90/* Allocate a simple bitmap of N_ELMS bits, and a popcount array. */
91
92sbitmap
93sbitmap_alloc_with_popcount (unsigned int n_elms)
94{
95 sbitmap bmap;
96
97 bmap = sbitmap_alloc (n_elms);
98 bmap->popcount = xmalloc (bmap->size * sizeof (unsigned char));
152bf224 99 return bmap;
100}
101
e52a3bed 102/* Resize a simple bitmap BMAP to N_ELMS bits. If increasing the
103 size of BMAP, clear the new bits to zero if the DEF argument
104 is zero, and set them to one otherwise. */
105
106sbitmap
60b8c5b3 107sbitmap_resize (sbitmap bmap, unsigned int n_elms, int def)
e52a3bed 108{
109 unsigned int bytes, size, amt;
110 unsigned int last_bit;
111
112 size = SBITMAP_SET_SIZE (n_elms);
113 bytes = size * sizeof (SBITMAP_ELT_TYPE);
8effb48f 114 if (bytes > SBITMAP_SIZE_BYTES (bmap))
e52a3bed 115 {
116 amt = (sizeof (struct simple_bitmap_def)
117 + bytes - sizeof (SBITMAP_ELT_TYPE));
f0af5a88 118 bmap = xrealloc (bmap, amt);
8effb48f 119 if (bmap->popcount)
120 bmap->popcount = xrealloc (bmap->popcount,
121 size * sizeof (unsigned char));
e52a3bed 122 }
123
124 if (n_elms > bmap->n_bits)
125 {
126 if (def)
127 {
8effb48f 128 memset (bmap->elms + bmap->size, -1,
129 bytes - SBITMAP_SIZE_BYTES (bmap));
e52a3bed 130
131 /* Set the new bits if the original last element. */
132 last_bit = bmap->n_bits % SBITMAP_ELT_BITS;
133 if (last_bit)
134 bmap->elms[bmap->size - 1]
135 |= ~((SBITMAP_ELT_TYPE)-1 >> (SBITMAP_ELT_BITS - last_bit));
136
137 /* Clear the unused bit in the new last element. */
138 last_bit = n_elms % SBITMAP_ELT_BITS;
139 if (last_bit)
140 bmap->elms[size - 1]
141 &= (SBITMAP_ELT_TYPE)-1 >> (SBITMAP_ELT_BITS - last_bit);
142 }
143 else
8effb48f 144 {
145 memset (bmap->elms + bmap->size, 0,
146 bytes - SBITMAP_SIZE_BYTES (bmap));
147 if (bmap->popcount)
148 memset (bmap->popcount + bmap->size, 0,
149 (size * sizeof (unsigned char))
150 - (bmap->size * sizeof (unsigned char)));
151
152 }
e52a3bed 153 }
154 else if (n_elms < bmap->n_bits)
155 {
037845e5 156 /* Clear the surplus bits in the last word. */
e52a3bed 157 last_bit = n_elms % SBITMAP_ELT_BITS;
158 if (last_bit)
8effb48f 159 {
160 bmap->elms[size - 1]
161 &= (SBITMAP_ELT_TYPE)-1 >> (SBITMAP_ELT_BITS - last_bit);
162 if (bmap->popcount)
163 bmap->popcount[size - 1] = do_popcount (bmap->elms[size - 1]);
164 }
e52a3bed 165 }
166
167 bmap->n_bits = n_elms;
168 bmap->size = size;
e52a3bed 169 return bmap;
170}
171
dac49aa5 172/* Re-allocate a simple bitmap of N_ELMS bits. New storage is uninitialized. */
4ee9c684 173
174sbitmap
175sbitmap_realloc (sbitmap src, unsigned int n_elms)
176{
177 unsigned int bytes, size, amt;
178 sbitmap bmap;
179
180 size = SBITMAP_SET_SIZE (n_elms);
181 bytes = size * sizeof (SBITMAP_ELT_TYPE);
182 amt = (sizeof (struct simple_bitmap_def)
183 + bytes - sizeof (SBITMAP_ELT_TYPE));
184
8effb48f 185 if (SBITMAP_SIZE_BYTES (src) >= bytes)
4ee9c684 186 {
187 src->n_bits = n_elms;
188 return src;
189 }
190
191 bmap = (sbitmap) xrealloc (src, amt);
192 bmap->n_bits = n_elms;
193 bmap->size = size;
4ee9c684 194 return bmap;
195}
196
152bf224 197/* Allocate a vector of N_VECS bitmaps of N_ELMS bits. */
198
199sbitmap *
60b8c5b3 200sbitmap_vector_alloc (unsigned int n_vecs, unsigned int n_elms)
152bf224 201{
43bf47e9 202 unsigned int i, bytes, offset, elm_bytes, size, amt, vector_bytes;
152bf224 203 sbitmap *bitmap_vector;
204
205 size = SBITMAP_SET_SIZE (n_elms);
206 bytes = size * sizeof (SBITMAP_ELT_TYPE);
207 elm_bytes = (sizeof (struct simple_bitmap_def)
208 + bytes - sizeof (SBITMAP_ELT_TYPE));
209 vector_bytes = n_vecs * sizeof (sbitmap *);
210
211 /* Round up `vector_bytes' to account for the alignment requirements
212 of an sbitmap. One could allocate the vector-table and set of sbitmaps
213 separately, but that requires maintaining two pointers or creating
214 a cover struct to hold both pointers (so our result is still just
215 one pointer). Neither is a bad idea, but this is simpler for now. */
216 {
217 /* Based on DEFAULT_ALIGNMENT computation in obstack.c. */
218 struct { char x; SBITMAP_ELT_TYPE y; } align;
219 int alignment = (char *) & align.y - & align.x;
220 vector_bytes = (vector_bytes + alignment - 1) & ~ (alignment - 1);
221 }
222
223 amt = vector_bytes + (n_vecs * elm_bytes);
f0af5a88 224 bitmap_vector = xmalloc (amt);
152bf224 225
43bf47e9 226 for (i = 0, offset = vector_bytes; i < n_vecs; i++, offset += elm_bytes)
152bf224 227 {
228 sbitmap b = (sbitmap) ((char *) bitmap_vector + offset);
43bf47e9 229
152bf224 230 bitmap_vector[i] = b;
231 b->n_bits = n_elms;
232 b->size = size;
8effb48f 233 b->popcount = NULL;
152bf224 234 }
235
236 return bitmap_vector;
237}
238
239/* Copy sbitmap SRC to DST. */
240
241void
60b8c5b3 242sbitmap_copy (sbitmap dst, sbitmap src)
152bf224 243{
fe766efd 244 memcpy (dst->elms, src->elms, sizeof (SBITMAP_ELT_TYPE) * dst->size);
8effb48f 245 if (dst->popcount)
246 memcpy (dst->popcount, src->popcount, sizeof (unsigned char) * dst->size);
247}
248
249/* Copy the first N elements of sbitmap SRC to DST. */
250
251void
252sbitmap_copy_n (sbitmap dst, sbitmap src, unsigned int n)
253{
254 memcpy (dst->elms, src->elms, sizeof (SBITMAP_ELT_TYPE) * n);
255 if (dst->popcount)
256 memcpy (dst->popcount, src->popcount, sizeof (unsigned char) * n);
152bf224 257}
258
1be87b72 259/* Determine if a == b. */
e0d1ffe3 260int
60b8c5b3 261sbitmap_equal (sbitmap a, sbitmap b)
e0d1ffe3 262{
263 return !memcmp (a->elms, b->elms, sizeof (SBITMAP_ELT_TYPE) * a->size);
264}
739c050b 265
3072d30e 266/* Return true if the bitmap is empty. */
267
268bool
269sbitmap_empty_p (sbitmap bmap)
270{
271 unsigned int i;
272 for (i=0; i<bmap->size; i++)
273 if (bmap->elms[i])
274 return false;
275
276 return true;
277}
278
152bf224 279/* Zero all elements in a bitmap. */
280
281void
60b8c5b3 282sbitmap_zero (sbitmap bmap)
152bf224 283{
8effb48f 284 memset (bmap->elms, 0, SBITMAP_SIZE_BYTES (bmap));
285 if (bmap->popcount)
286 memset (bmap->popcount, 0, bmap->size * sizeof (unsigned char));
152bf224 287}
288
43bf47e9 289/* Set all elements in a bitmap to ones. */
152bf224 290
291void
60b8c5b3 292sbitmap_ones (sbitmap bmap)
152bf224 293{
4076a367 294 unsigned int last_bit;
295
8effb48f 296 memset (bmap->elms, -1, SBITMAP_SIZE_BYTES (bmap));
297 if (bmap->popcount)
298 memset (bmap->popcount, -1, bmap->size * sizeof (unsigned char));
4076a367 299
43bf47e9 300 last_bit = bmap->n_bits % SBITMAP_ELT_BITS;
4076a367 301 if (last_bit)
8effb48f 302 {
303 bmap->elms[bmap->size - 1]
304 = (SBITMAP_ELT_TYPE)-1 >> (SBITMAP_ELT_BITS - last_bit);
305 if (bmap->popcount)
306 bmap->popcount[bmap->size - 1]
307 = do_popcount (bmap->elms[bmap->size - 1]);
308 }
152bf224 309}
310
311/* Zero a vector of N_VECS bitmaps. */
312
313void
60b8c5b3 314sbitmap_vector_zero (sbitmap *bmap, unsigned int n_vecs)
152bf224 315{
43bf47e9 316 unsigned int i;
152bf224 317
318 for (i = 0; i < n_vecs; i++)
319 sbitmap_zero (bmap[i]);
320}
321
43bf47e9 322/* Set a vector of N_VECS bitmaps to ones. */
152bf224 323
324void
60b8c5b3 325sbitmap_vector_ones (sbitmap *bmap, unsigned int n_vecs)
152bf224 326{
43bf47e9 327 unsigned int i;
152bf224 328
329 for (i = 0; i < n_vecs; i++)
330 sbitmap_ones (bmap[i]);
331}
332
333/* Set DST to be A union (B - C).
334 DST = A | (B & ~C).
739c050b 335 Returns true if any change is made. */
152bf224 336
739c050b 337bool
60b8c5b3 338sbitmap_union_of_diff_cg (sbitmap dst, sbitmap a, sbitmap b, sbitmap c)
152bf224 339{
739c050b 340 unsigned int i, n = dst->size;
341 sbitmap_ptr dstp = dst->elms;
342 sbitmap_ptr ap = a->elms;
343 sbitmap_ptr bp = b->elms;
344 sbitmap_ptr cp = c->elms;
345 SBITMAP_ELT_TYPE changed = 0;
346
8effb48f 347 gcc_assert (!dst->popcount);
348
739c050b 349 for (i = 0; i < n; i++)
152bf224 350 {
43bf47e9 351 SBITMAP_ELT_TYPE tmp = *ap++ | (*bp++ & ~*cp++);
739c050b 352 changed |= *dstp ^ tmp;
353 *dstp++ = tmp;
152bf224 354 }
43bf47e9 355
739c050b 356 return changed != 0;
357}
358
359void
60b8c5b3 360sbitmap_union_of_diff (sbitmap dst, sbitmap a, sbitmap b, sbitmap c)
739c050b 361{
362 unsigned int i, n = dst->size;
363 sbitmap_ptr dstp = dst->elms;
364 sbitmap_ptr ap = a->elms;
365 sbitmap_ptr bp = b->elms;
366 sbitmap_ptr cp = c->elms;
367
8effb48f 368 gcc_assert (!dst->popcount && !a->popcount
369 && !b->popcount && !c->popcount);
370
739c050b 371 for (i = 0; i < n; i++)
372 *dstp++ = *ap++ | (*bp++ & ~*cp++);
152bf224 373}
374
375/* Set bitmap DST to the bitwise negation of the bitmap SRC. */
376
377void
60b8c5b3 378sbitmap_not (sbitmap dst, sbitmap src)
152bf224 379{
739c050b 380 unsigned int i, n = dst->size;
381 sbitmap_ptr dstp = dst->elms;
382 sbitmap_ptr srcp = src->elms;
93036b9b 383 unsigned int last_bit;
152bf224 384
8effb48f 385 gcc_assert (!dst->popcount);
386
739c050b 387 for (i = 0; i < n; i++)
388 *dstp++ = ~*srcp++;
93036b9b 389
390 /* Zero all bits past n_bits, by ANDing dst with sbitmap_ones. */
391 last_bit = src->n_bits % SBITMAP_ELT_BITS;
392 if (last_bit)
393 dst->elms[n-1] = dst->elms[n-1]
394 & ((SBITMAP_ELT_TYPE)-1 >> (SBITMAP_ELT_BITS - last_bit));
152bf224 395}
396
397/* Set the bits in DST to be the difference between the bits
43bf47e9 398 in A and the bits in B. i.e. dst = a & (~b). */
152bf224 399
400void
60b8c5b3 401sbitmap_difference (sbitmap dst, sbitmap a, sbitmap b)
152bf224 402{
cb5c5698 403 unsigned int i, dst_size = dst->size;
404 unsigned int min_size = dst->size;
739c050b 405 sbitmap_ptr dstp = dst->elms;
406 sbitmap_ptr ap = a->elms;
407 sbitmap_ptr bp = b->elms;
60b8c5b3 408
8effb48f 409 gcc_assert (!dst->popcount);
410
cb5c5698 411 /* A should be at least as large as DEST, to have a defined source. */
04e579b6 412 gcc_assert (a->size >= dst_size);
cb5c5698 413 /* If minuend is smaller, we simply pretend it to be zero bits, i.e.
414 only copy the subtrahend into dest. */
415 if (b->size < min_size)
416 min_size = b->size;
417 for (i = 0; i < min_size; i++)
418 *dstp++ = *ap++ & (~*bp++);
419 /* Now fill the rest of dest from A, if B was too short.
420 This makes sense only when destination and A differ. */
421 if (dst != a && i != dst_size)
422 for (; i < dst_size; i++)
423 *dstp++ = *ap++;
152bf224 424}
425
7d0585a5 426/* Return true if there are any bits set in A are also set in B.
427 Return false otherwise. */
428
429bool
430sbitmap_any_common_bits (sbitmap a, sbitmap b)
431{
432 sbitmap_ptr ap = a->elms;
433 sbitmap_ptr bp = b->elms;
434 unsigned int i, n;
435
436 n = MIN (a->size, b->size);
437 for (i = 0; i < n; i++)
438 if ((*ap++ & *bp++) != 0)
439 return true;
440
441 return false;
442}
443
4076a367 444/* Set DST to be (A and B).
f712a0dc 445 Return nonzero if any change is made. */
152bf224 446
739c050b 447bool
60b8c5b3 448sbitmap_a_and_b_cg (sbitmap dst, sbitmap a, sbitmap b)
152bf224 449{
739c050b 450 unsigned int i, n = dst->size;
451 sbitmap_ptr dstp = dst->elms;
452 sbitmap_ptr ap = a->elms;
453 sbitmap_ptr bp = b->elms;
454 SBITMAP_ELT_TYPE changed = 0;
152bf224 455
8effb48f 456 gcc_assert (!dst->popcount);
457
739c050b 458 for (i = 0; i < n; i++)
152bf224 459 {
43bf47e9 460 SBITMAP_ELT_TYPE tmp = *ap++ & *bp++;
93036b9b 461 changed |= *dstp ^ tmp;
739c050b 462 *dstp++ = tmp;
152bf224 463 }
43bf47e9 464
739c050b 465 return changed != 0;
466}
467
468void
60b8c5b3 469sbitmap_a_and_b (sbitmap dst, sbitmap a, sbitmap b)
739c050b 470{
471 unsigned int i, n = dst->size;
472 sbitmap_ptr dstp = dst->elms;
473 sbitmap_ptr ap = a->elms;
474 sbitmap_ptr bp = b->elms;
8effb48f 475 bool has_popcount = dst->popcount != NULL;
476 unsigned char *popcountp = dst->popcount;
739c050b 477
478 for (i = 0; i < n; i++)
8effb48f 479 {
480 SBITMAP_ELT_TYPE tmp = *ap++ & *bp++;
481 if (has_popcount)
482 {
483 bool wordchanged = (*dstp ^ tmp) != 0;
484 if (wordchanged)
485 *popcountp = do_popcount (tmp);
486 popcountp++;
487 }
488 *dstp++ = tmp;
489 }
490#ifdef BITMAP_DEBUGGING
491 if (has_popcount)
492 sbitmap_verify_popcount (dst);
493#endif
152bf224 494}
43bf47e9 495
e0d1ffe3 496/* Set DST to be (A xor B)).
f712a0dc 497 Return nonzero if any change is made. */
e0d1ffe3 498
739c050b 499bool
60b8c5b3 500sbitmap_a_xor_b_cg (sbitmap dst, sbitmap a, sbitmap b)
e0d1ffe3 501{
739c050b 502 unsigned int i, n = dst->size;
503 sbitmap_ptr dstp = dst->elms;
504 sbitmap_ptr ap = a->elms;
505 sbitmap_ptr bp = b->elms;
506 SBITMAP_ELT_TYPE changed = 0;
8effb48f 507
508 gcc_assert (!dst->popcount);
739c050b 509
510 for (i = 0; i < n; i++)
e0d1ffe3 511 {
512 SBITMAP_ELT_TYPE tmp = *ap++ ^ *bp++;
93036b9b 513 changed |= *dstp ^ tmp;
739c050b 514 *dstp++ = tmp;
e0d1ffe3 515 }
739c050b 516
517 return changed != 0;
518}
519
520void
60b8c5b3 521sbitmap_a_xor_b (sbitmap dst, sbitmap a, sbitmap b)
739c050b 522{
523 unsigned int i, n = dst->size;
524 sbitmap_ptr dstp = dst->elms;
525 sbitmap_ptr ap = a->elms;
526 sbitmap_ptr bp = b->elms;
8effb48f 527 bool has_popcount = dst->popcount != NULL;
528 unsigned char *popcountp = dst->popcount;
739c050b 529
530 for (i = 0; i < n; i++)
8effb48f 531 {
532 SBITMAP_ELT_TYPE tmp = *ap++ ^ *bp++;
533 if (has_popcount)
534 {
535 bool wordchanged = (*dstp ^ tmp) != 0;
536 if (wordchanged)
537 *popcountp = do_popcount (tmp);
538 popcountp++;
539 }
540 *dstp++ = tmp;
541 }
542#ifdef BITMAP_DEBUGGING
543 if (has_popcount)
544 sbitmap_verify_popcount (dst);
545#endif
e0d1ffe3 546}
547
152bf224 548/* Set DST to be (A or B)).
f712a0dc 549 Return nonzero if any change is made. */
152bf224 550
739c050b 551bool
60b8c5b3 552sbitmap_a_or_b_cg (sbitmap dst, sbitmap a, sbitmap b)
152bf224 553{
739c050b 554 unsigned int i, n = dst->size;
555 sbitmap_ptr dstp = dst->elms;
556 sbitmap_ptr ap = a->elms;
557 sbitmap_ptr bp = b->elms;
558 SBITMAP_ELT_TYPE changed = 0;
152bf224 559
8effb48f 560 gcc_assert (!dst->popcount);
561
739c050b 562 for (i = 0; i < n; i++)
152bf224 563 {
43bf47e9 564 SBITMAP_ELT_TYPE tmp = *ap++ | *bp++;
93036b9b 565 changed |= *dstp ^ tmp;
739c050b 566 *dstp++ = tmp;
152bf224 567 }
43bf47e9 568
739c050b 569 return changed != 0;
570}
571
572void
60b8c5b3 573sbitmap_a_or_b (sbitmap dst, sbitmap a, sbitmap b)
739c050b 574{
575 unsigned int i, n = dst->size;
576 sbitmap_ptr dstp = dst->elms;
577 sbitmap_ptr ap = a->elms;
578 sbitmap_ptr bp = b->elms;
8effb48f 579 bool has_popcount = dst->popcount != NULL;
580 unsigned char *popcountp = dst->popcount;
739c050b 581
582 for (i = 0; i < n; i++)
8effb48f 583 {
584 SBITMAP_ELT_TYPE tmp = *ap++ | *bp++;
585 if (has_popcount)
586 {
587 bool wordchanged = (*dstp ^ tmp) != 0;
588 if (wordchanged)
589 *popcountp = do_popcount (tmp);
590 popcountp++;
591 }
592 *dstp++ = tmp;
593 }
594#ifdef BITMAP_DEBUGGING
595 if (has_popcount)
596 sbitmap_verify_popcount (dst);
597#endif
152bf224 598}
43bf47e9 599
f712a0dc 600/* Return nonzero if A is a subset of B. */
a4d05baf 601
739c050b 602bool
60b8c5b3 603sbitmap_a_subset_b_p (sbitmap a, sbitmap b)
a4d05baf 604{
739c050b 605 unsigned int i, n = a->size;
a4d05baf 606 sbitmap_ptr ap, bp;
607
739c050b 608 for (ap = a->elms, bp = b->elms, i = 0; i < n; i++, ap++, bp++)
af122f0a 609 if ((*ap | *bp) != *bp)
739c050b 610 return false;
43bf47e9 611
739c050b 612 return true;
a4d05baf 613}
152bf224 614
615/* Set DST to be (A or (B and C)).
f712a0dc 616 Return nonzero if any change is made. */
152bf224 617
739c050b 618bool
60b8c5b3 619sbitmap_a_or_b_and_c_cg (sbitmap dst, sbitmap a, sbitmap b, sbitmap c)
152bf224 620{
739c050b 621 unsigned int i, n = dst->size;
622 sbitmap_ptr dstp = dst->elms;
623 sbitmap_ptr ap = a->elms;
624 sbitmap_ptr bp = b->elms;
625 sbitmap_ptr cp = c->elms;
626 SBITMAP_ELT_TYPE changed = 0;
627
8effb48f 628 gcc_assert (!dst->popcount);
629
739c050b 630 for (i = 0; i < n; i++)
152bf224 631 {
43bf47e9 632 SBITMAP_ELT_TYPE tmp = *ap++ | (*bp++ & *cp++);
739c050b 633 changed |= *dstp ^ tmp;
634 *dstp++ = tmp;
152bf224 635 }
43bf47e9 636
739c050b 637 return changed != 0;
638}
639
640void
60b8c5b3 641sbitmap_a_or_b_and_c (sbitmap dst, sbitmap a, sbitmap b, sbitmap c)
739c050b 642{
643 unsigned int i, n = dst->size;
644 sbitmap_ptr dstp = dst->elms;
645 sbitmap_ptr ap = a->elms;
646 sbitmap_ptr bp = b->elms;
647 sbitmap_ptr cp = c->elms;
648
8effb48f 649 gcc_assert (!dst->popcount);
650
739c050b 651 for (i = 0; i < n; i++)
652 *dstp++ = *ap++ | (*bp++ & *cp++);
152bf224 653}
654
43bf47e9 655/* Set DST to be (A and (B or C)).
f712a0dc 656 Return nonzero if any change is made. */
152bf224 657
739c050b 658bool
60b8c5b3 659sbitmap_a_and_b_or_c_cg (sbitmap dst, sbitmap a, sbitmap b, sbitmap c)
152bf224 660{
739c050b 661 unsigned int i, n = dst->size;
662 sbitmap_ptr dstp = dst->elms;
663 sbitmap_ptr ap = a->elms;
664 sbitmap_ptr bp = b->elms;
665 sbitmap_ptr cp = c->elms;
666 SBITMAP_ELT_TYPE changed = 0;
667
8effb48f 668 gcc_assert (!dst->popcount);
669
739c050b 670 for (i = 0; i < n; i++)
152bf224 671 {
43bf47e9 672 SBITMAP_ELT_TYPE tmp = *ap++ & (*bp++ | *cp++);
739c050b 673 changed |= *dstp ^ tmp;
674 *dstp++ = tmp;
152bf224 675 }
43bf47e9 676
739c050b 677 return changed != 0;
678}
679
680void
60b8c5b3 681sbitmap_a_and_b_or_c (sbitmap dst, sbitmap a, sbitmap b, sbitmap c)
739c050b 682{
683 unsigned int i, n = dst->size;
684 sbitmap_ptr dstp = dst->elms;
685 sbitmap_ptr ap = a->elms;
686 sbitmap_ptr bp = b->elms;
687 sbitmap_ptr cp = c->elms;
688
689 for (i = 0; i < n; i++)
690 *dstp++ = *ap++ & (*bp++ | *cp++);
152bf224 691}
692
e0d1ffe3 693#ifdef IN_GCC
cb7e28c3 694/* Set the bitmap DST to the intersection of SRC of successors of
695 block number BB, using the new flow graph structures. */
696
697void
60b8c5b3 698sbitmap_intersection_of_succs (sbitmap dst, sbitmap *src, int bb)
cb7e28c3 699{
700 basic_block b = BASIC_BLOCK (bb);
43bf47e9 701 unsigned int set_size = dst->size;
702 edge e;
cd665a06 703 unsigned ix;
cb7e28c3 704
8effb48f 705 gcc_assert (!dst->popcount);
706
cd665a06 707 for (e = NULL, ix = 0; ix < EDGE_COUNT (b->succs); ix++)
cb7e28c3 708 {
cd665a06 709 e = EDGE_SUCC (b, ix);
cb7e28c3 710 if (e->dest == EXIT_BLOCK_PTR)
40734805 711 continue;
cd665a06 712
b3d6de89 713 sbitmap_copy (dst, src[e->dest->index]);
cb7e28c3 714 break;
715 }
43bf47e9 716
717 if (e == 0)
cb7e28c3 718 sbitmap_ones (dst);
719 else
cd665a06 720 for (++ix; ix < EDGE_COUNT (b->succs); ix++)
43bf47e9 721 {
722 unsigned int i;
723 sbitmap_ptr p, r;
724
cd665a06 725 e = EDGE_SUCC (b, ix);
43bf47e9 726 if (e->dest == EXIT_BLOCK_PTR)
727 continue;
728
b3d6de89 729 p = src[e->dest->index]->elms;
43bf47e9 730 r = dst->elms;
731 for (i = 0; i < set_size; i++)
732 *r++ &= *p++;
733 }
cb7e28c3 734}
735
736/* Set the bitmap DST to the intersection of SRC of predecessors of
737 block number BB, using the new flow graph structures. */
738
739void
60b8c5b3 740sbitmap_intersection_of_preds (sbitmap dst, sbitmap *src, int bb)
cb7e28c3 741{
742 basic_block b = BASIC_BLOCK (bb);
43bf47e9 743 unsigned int set_size = dst->size;
744 edge e;
cd665a06 745 unsigned ix;
cb7e28c3 746
8effb48f 747 gcc_assert (!dst->popcount);
748
cd665a06 749 for (e = NULL, ix = 0; ix < EDGE_COUNT (b->preds); ix++)
cb7e28c3 750 {
cd665a06 751 e = EDGE_PRED (b, ix);
43bf47e9 752 if (e->src == ENTRY_BLOCK_PTR)
40734805 753 continue;
43bf47e9 754
b3d6de89 755 sbitmap_copy (dst, src[e->src->index]);
cb7e28c3 756 break;
757 }
43bf47e9 758
759 if (e == 0)
cb7e28c3 760 sbitmap_ones (dst);
761 else
cd665a06 762 for (++ix; ix < EDGE_COUNT (b->preds); ix++)
43bf47e9 763 {
764 unsigned int i;
765 sbitmap_ptr p, r;
766
cd665a06 767 e = EDGE_PRED (b, ix);
43bf47e9 768 if (e->src == ENTRY_BLOCK_PTR)
769 continue;
770
b3d6de89 771 p = src[e->src->index]->elms;
43bf47e9 772 r = dst->elms;
773 for (i = 0; i < set_size; i++)
774 *r++ &= *p++;
775 }
cb7e28c3 776}
777
778/* Set the bitmap DST to the union of SRC of successors of
779 block number BB, using the new flow graph structures. */
780
781void
60b8c5b3 782sbitmap_union_of_succs (sbitmap dst, sbitmap *src, int bb)
cb7e28c3 783{
784 basic_block b = BASIC_BLOCK (bb);
43bf47e9 785 unsigned int set_size = dst->size;
786 edge e;
cd665a06 787 unsigned ix;
cb7e28c3 788
8effb48f 789 gcc_assert (!dst->popcount);
790
cd665a06 791 for (ix = 0; ix < EDGE_COUNT (b->succs); ix++)
cb7e28c3 792 {
cd665a06 793 e = EDGE_SUCC (b, ix);
cb7e28c3 794 if (e->dest == EXIT_BLOCK_PTR)
40734805 795 continue;
43bf47e9 796
b3d6de89 797 sbitmap_copy (dst, src[e->dest->index]);
cb7e28c3 798 break;
799 }
43bf47e9 800
cd665a06 801 if (ix == EDGE_COUNT (b->succs))
cb7e28c3 802 sbitmap_zero (dst);
803 else
cd665a06 804 for (ix++; ix < EDGE_COUNT (b->succs); ix++)
43bf47e9 805 {
806 unsigned int i;
807 sbitmap_ptr p, r;
808
cd665a06 809 e = EDGE_SUCC (b, ix);
43bf47e9 810 if (e->dest == EXIT_BLOCK_PTR)
811 continue;
812
b3d6de89 813 p = src[e->dest->index]->elms;
43bf47e9 814 r = dst->elms;
815 for (i = 0; i < set_size; i++)
816 *r++ |= *p++;
817 }
cb7e28c3 818}
819
820/* Set the bitmap DST to the union of SRC of predecessors of
821 block number BB, using the new flow graph structures. */
822
823void
60b8c5b3 824sbitmap_union_of_preds (sbitmap dst, sbitmap *src, int bb)
cb7e28c3 825{
826 basic_block b = BASIC_BLOCK (bb);
43bf47e9 827 unsigned int set_size = dst->size;
828 edge e;
cd665a06 829 unsigned ix;
cb7e28c3 830
8effb48f 831 gcc_assert (!dst->popcount);
832
cf141158 833 for (ix = 0; ix < EDGE_COUNT (b->preds); ix++)
cb7e28c3 834 {
8ad786e6 835 e = EDGE_PRED (b, ix);
cb7e28c3 836 if (e->src== ENTRY_BLOCK_PTR)
40734805 837 continue;
43bf47e9 838
b3d6de89 839 sbitmap_copy (dst, src[e->src->index]);
cb7e28c3 840 break;
841 }
43bf47e9 842
cd665a06 843 if (ix == EDGE_COUNT (b->preds))
cb7e28c3 844 sbitmap_zero (dst);
845 else
cd665a06 846 for (ix++; ix < EDGE_COUNT (b->preds); ix++)
43bf47e9 847 {
848 unsigned int i;
849 sbitmap_ptr p, r;
850
cd665a06 851 e = EDGE_PRED (b, ix);
43bf47e9 852 if (e->src == ENTRY_BLOCK_PTR)
853 continue;
b3d6de89 854
855 p = src[e->src->index]->elms;
43bf47e9 856 r = dst->elms;
857 for (i = 0; i < set_size; i++)
858 *r++ |= *p++;
859 }
cb7e28c3 860}
e0d1ffe3 861#endif
cb7e28c3 862
7fcadf62 863/* Return number of first bit set in the bitmap, -1 if none. */
864
865int
60b8c5b3 866sbitmap_first_set_bit (sbitmap bmap)
7fcadf62 867{
86c1585a 868 unsigned int n = 0;
3e790786 869 sbitmap_iterator sbi;
43bf47e9 870
3e790786 871 EXECUTE_IF_SET_IN_SBITMAP (bmap, 0, n, sbi)
872 return n;
7fcadf62 873 return -1;
874}
875
876/* Return number of last bit set in the bitmap, -1 if none. */
877
878int
60b8c5b3 879sbitmap_last_set_bit (sbitmap bmap)
7fcadf62 880{
881 int i;
882 SBITMAP_ELT_TYPE *ptr = bmap->elms;
43bf47e9 883
7fcadf62 884 for (i = bmap->size - 1; i >= 0; i--)
885 {
886 SBITMAP_ELT_TYPE word = ptr[i];
43bf47e9 887
888 if (word != 0)
889 {
890 unsigned int index = (i + 1) * SBITMAP_ELT_BITS - 1;
891 SBITMAP_ELT_TYPE mask
892 = (SBITMAP_ELT_TYPE) 1 << (SBITMAP_ELT_BITS - 1);
893
894 while (1)
895 {
896 if ((word & mask) != 0)
897 return index;
898
899 mask >>= 1;
900 index--;
901 }
902 }
7fcadf62 903 }
43bf47e9 904
7fcadf62 905 return -1;
906}
907
152bf224 908void
60b8c5b3 909dump_sbitmap (FILE *file, sbitmap bmap)
152bf224 910{
43bf47e9 911 unsigned int i, n, j;
912 unsigned int set_size = bmap->size;
913 unsigned int total_bits = bmap->n_bits;
152bf224 914
915 fprintf (file, " ");
916 for (i = n = 0; i < set_size && n < total_bits; i++)
43bf47e9 917 for (j = 0; j < SBITMAP_ELT_BITS && n < total_bits; j++, n++)
918 {
919 if (n != 0 && n % 10 == 0)
920 fprintf (file, " ");
921
922 fprintf (file, "%d",
923 (bmap->elms[i] & ((SBITMAP_ELT_TYPE) 1 << j)) != 0);
924 }
925
152bf224 926 fprintf (file, "\n");
927}
928
43bf47e9 929void
60b8c5b3 930dump_sbitmap_file (FILE *file, sbitmap bmap)
43bf47e9 931{
932 unsigned int i, pos;
933
cb5c5698 934 fprintf (file, "n_bits = %d, set = {", bmap->n_bits);
43bf47e9 935
936 for (pos = 30, i = 0; i < bmap->n_bits; i++)
937 if (TEST_BIT (bmap, i))
938 {
939 if (pos > 70)
940 {
cb5c5698 941 fprintf (file, "\n ");
43bf47e9 942 pos = 0;
943 }
944
cb5c5698 945 fprintf (file, "%d ", i);
946 pos += 2 + (i >= 10) + (i >= 100) + (i >= 1000);
43bf47e9 947 }
948
cb5c5698 949 fprintf (file, "}\n");
950}
951
952void
60b8c5b3 953debug_sbitmap (sbitmap bmap)
cb5c5698 954{
955 dump_sbitmap_file (stderr, bmap);
43bf47e9 956}
957
152bf224 958void
60b8c5b3 959dump_sbitmap_vector (FILE *file, const char *title, const char *subtitle,
960 sbitmap *bmaps, int n_maps)
152bf224 961{
962 int bb;
963
964 fprintf (file, "%s\n", title);
965 for (bb = 0; bb < n_maps; bb++)
966 {
967 fprintf (file, "%s %d\n", subtitle, bb);
968 dump_sbitmap (file, bmaps[bb]);
969 }
43bf47e9 970
152bf224 971 fprintf (file, "\n");
972}
8effb48f 973
974#if GCC_VERSION < 3400
975/* Table of number of set bits in a character, indexed by value of char. */
976static unsigned char popcount_table[] =
977{
978 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,
979 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,
980 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,
981 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,
982 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,
983 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,
984 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,
985 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,
986};
987
988/* Count the bits in an SBITMAP element A. */
989
990static unsigned long
991sbitmap_elt_popcount (SBITMAP_ELT_TYPE a)
992{
993 unsigned long ret = 0;
994 unsigned i;
995
996 if (a == 0)
997 return 0;
998
999 /* Just do this the table way for now */
1000 for (i = 0; i < SBITMAP_ELT_BITS; i += 8)
1001 ret += popcount_table[(a >> i) & 0xff];
1002 return ret;
1003}
1004#endif
1005
1006/* Count the number of bits in SBITMAP a, up to bit MAXBIT. */
1007
1008unsigned long
1009sbitmap_popcount (sbitmap a, unsigned long maxbit)
1010{
1011 unsigned long count = 0;
1012 unsigned ix;
1013 unsigned int lastword;
1014
1015 if (maxbit == 0)
1016 return 0;
1017
1018 if (maxbit >= a->n_bits)
1019 maxbit = a->n_bits;
1020
1021 /* Count the bits in the full word. */
1022 lastword = MIN (a->size, SBITMAP_SET_SIZE (maxbit + 1) - 1);
1023 for (ix = 0; ix < lastword; ix++)
1024 {
1025 if (a->popcount)
1026 {
1027 count += a->popcount[ix];
1028#ifdef BITMAP_DEBUGGING
1029 gcc_assert (a->popcount[ix] == do_popcount (a->elms[ix]));
1030#endif
1031 }
1032 else
1033 count += do_popcount (a->elms[ix]);
1034 }
1035
1036 /* Count the remaining bits. */
1037 if (lastword < a->size)
1038 {
1039 unsigned int bitindex;
1040 SBITMAP_ELT_TYPE theword = a->elms[lastword];
1041
1042 bitindex = maxbit % SBITMAP_ELT_BITS;
1043 if (bitindex != 0)
1044 {
1045 theword &= (SBITMAP_ELT_TYPE)-1 >> (SBITMAP_ELT_BITS - bitindex);
1046 count += do_popcount (theword);
1047 }
1048 }
1049 return count;
1050}
1051