configure.in (GLIBCPP_ENABLE_CXX_FLAGS): Do not pass arguments, let the defaults...
[official-gcc.git] / gcc / sbitmap.c
blob8514a22122745b1305c91341ec02a0a7e8e3b17e
1 /* Simple bitmaps.
2 Copyright (C) 1999, 2000, 2002 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 (n_elms)
36 unsigned int n_elms;
38 unsigned int bytes, size, amt;
39 sbitmap bmap;
41 size = SBITMAP_SET_SIZE (n_elms);
42 bytes = size * sizeof (SBITMAP_ELT_TYPE);
43 amt = (sizeof (struct simple_bitmap_def)
44 + bytes - sizeof (SBITMAP_ELT_TYPE));
45 bmap = (sbitmap) xmalloc (amt);
46 bmap->n_bits = n_elms;
47 bmap->size = size;
48 bmap->bytes = bytes;
49 return bmap;
52 /* Allocate a vector of N_VECS bitmaps of N_ELMS bits. */
54 sbitmap *
55 sbitmap_vector_alloc (n_vecs, n_elms)
56 unsigned int n_vecs, n_elms;
58 unsigned int i, bytes, offset, elm_bytes, size, amt, vector_bytes;
59 sbitmap *bitmap_vector;
61 size = SBITMAP_SET_SIZE (n_elms);
62 bytes = size * sizeof (SBITMAP_ELT_TYPE);
63 elm_bytes = (sizeof (struct simple_bitmap_def)
64 + bytes - sizeof (SBITMAP_ELT_TYPE));
65 vector_bytes = n_vecs * sizeof (sbitmap *);
67 /* Round up `vector_bytes' to account for the alignment requirements
68 of an sbitmap. One could allocate the vector-table and set of sbitmaps
69 separately, but that requires maintaining two pointers or creating
70 a cover struct to hold both pointers (so our result is still just
71 one pointer). Neither is a bad idea, but this is simpler for now. */
73 /* Based on DEFAULT_ALIGNMENT computation in obstack.c. */
74 struct { char x; SBITMAP_ELT_TYPE y; } align;
75 int alignment = (char *) & align.y - & align.x;
76 vector_bytes = (vector_bytes + alignment - 1) & ~ (alignment - 1);
79 amt = vector_bytes + (n_vecs * elm_bytes);
80 bitmap_vector = (sbitmap *) xmalloc (amt);
82 for (i = 0, offset = vector_bytes; i < n_vecs; i++, offset += elm_bytes)
84 sbitmap b = (sbitmap) ((char *) bitmap_vector + offset);
86 bitmap_vector[i] = b;
87 b->n_bits = n_elms;
88 b->size = size;
89 b->bytes = bytes;
92 return bitmap_vector;
95 /* Copy sbitmap SRC to DST. */
97 void
98 sbitmap_copy (dst, src)
99 sbitmap dst, src;
101 memcpy (dst->elms, src->elms, sizeof (SBITMAP_ELT_TYPE) * dst->size);
104 /* Determine if a == b. */
106 sbitmap_equal (a, b)
107 sbitmap a, b;
109 return !memcmp (a->elms, b->elms, sizeof (SBITMAP_ELT_TYPE) * a->size);
112 /* Zero all elements in a bitmap. */
114 void
115 sbitmap_zero (bmap)
116 sbitmap bmap;
118 memset ((PTR) bmap->elms, 0, bmap->bytes);
121 /* Set all elements in a bitmap to ones. */
123 void
124 sbitmap_ones (bmap)
125 sbitmap bmap;
127 unsigned int last_bit;
129 memset ((PTR) bmap->elms, -1, bmap->bytes);
131 last_bit = bmap->n_bits % SBITMAP_ELT_BITS;
132 if (last_bit)
133 bmap->elms[bmap->size - 1]
134 = (SBITMAP_ELT_TYPE)-1 >> (SBITMAP_ELT_BITS - last_bit);
137 /* Zero a vector of N_VECS bitmaps. */
139 void
140 sbitmap_vector_zero (bmap, n_vecs)
141 sbitmap *bmap;
142 unsigned int n_vecs;
144 unsigned int i;
146 for (i = 0; i < n_vecs; i++)
147 sbitmap_zero (bmap[i]);
150 /* Set a vector of N_VECS bitmaps to ones. */
152 void
153 sbitmap_vector_ones (bmap, n_vecs)
154 sbitmap *bmap;
155 unsigned int n_vecs;
157 unsigned int i;
159 for (i = 0; i < n_vecs; i++)
160 sbitmap_ones (bmap[i]);
163 /* Set DST to be A union (B - C).
164 DST = A | (B & ~C).
165 Returns true if any change is made. */
167 bool
168 sbitmap_union_of_diff_cg (dst, a, b, c)
169 sbitmap dst, a, b, c;
171 unsigned int i, n = dst->size;
172 sbitmap_ptr dstp = dst->elms;
173 sbitmap_ptr ap = a->elms;
174 sbitmap_ptr bp = b->elms;
175 sbitmap_ptr cp = c->elms;
176 SBITMAP_ELT_TYPE changed = 0;
178 for (i = 0; i < n; i++)
180 SBITMAP_ELT_TYPE tmp = *ap++ | (*bp++ & ~*cp++);
181 changed |= *dstp ^ tmp;
182 *dstp++ = tmp;
185 return changed != 0;
188 void
189 sbitmap_union_of_diff (dst, a, b, c)
190 sbitmap dst, a, b, c;
192 unsigned int i, n = dst->size;
193 sbitmap_ptr dstp = dst->elms;
194 sbitmap_ptr ap = a->elms;
195 sbitmap_ptr bp = b->elms;
196 sbitmap_ptr cp = c->elms;
198 for (i = 0; i < n; i++)
199 *dstp++ = *ap++ | (*bp++ & ~*cp++);
202 /* Set bitmap DST to the bitwise negation of the bitmap SRC. */
204 void
205 sbitmap_not (dst, src)
206 sbitmap dst, src;
208 unsigned int i, n = dst->size;
209 sbitmap_ptr dstp = dst->elms;
210 sbitmap_ptr srcp = src->elms;
212 for (i = 0; i < n; i++)
213 *dstp++ = ~*srcp++;
216 /* Set the bits in DST to be the difference between the bits
217 in A and the bits in B. i.e. dst = a & (~b). */
219 void
220 sbitmap_difference (dst, a, b)
221 sbitmap dst, a, b;
223 unsigned int i, dst_size = dst->size;
224 unsigned int min_size = dst->size;
225 sbitmap_ptr dstp = dst->elms;
226 sbitmap_ptr ap = a->elms;
227 sbitmap_ptr bp = b->elms;
229 /* A should be at least as large as DEST, to have a defined source. */
230 if (a->size < dst_size)
231 abort ();
232 /* If minuend is smaller, we simply pretend it to be zero bits, i.e.
233 only copy the subtrahend into dest. */
234 if (b->size < min_size)
235 min_size = b->size;
236 for (i = 0; i < min_size; i++)
237 *dstp++ = *ap++ & (~*bp++);
238 /* Now fill the rest of dest from A, if B was too short.
239 This makes sense only when destination and A differ. */
240 if (dst != a && i != dst_size)
241 for (; i < dst_size; i++)
242 *dstp++ = *ap++;
245 /* Set DST to be (A and B).
246 Return nonzero if any change is made. */
248 bool
249 sbitmap_a_and_b_cg (dst, a, b)
250 sbitmap dst, a, b;
252 unsigned int i, n = dst->size;
253 sbitmap_ptr dstp = dst->elms;
254 sbitmap_ptr ap = a->elms;
255 sbitmap_ptr bp = b->elms;
256 SBITMAP_ELT_TYPE changed = 0;
258 for (i = 0; i < n; i++)
260 SBITMAP_ELT_TYPE tmp = *ap++ & *bp++;
261 changed = *dstp ^ tmp;
262 *dstp++ = tmp;
265 return changed != 0;
268 void
269 sbitmap_a_and_b (dst, a, b)
270 sbitmap dst, a, b;
272 unsigned int i, n = dst->size;
273 sbitmap_ptr dstp = dst->elms;
274 sbitmap_ptr ap = a->elms;
275 sbitmap_ptr bp = b->elms;
277 for (i = 0; i < n; i++)
278 *dstp++ = *ap++ & *bp++;
281 /* Set DST to be (A xor B)).
282 Return nonzero if any change is made. */
284 bool
285 sbitmap_a_xor_b_cg (dst, a, b)
286 sbitmap dst, a, b;
288 unsigned int i, n = dst->size;
289 sbitmap_ptr dstp = dst->elms;
290 sbitmap_ptr ap = a->elms;
291 sbitmap_ptr bp = b->elms;
292 SBITMAP_ELT_TYPE changed = 0;
294 for (i = 0; i < n; i++)
296 SBITMAP_ELT_TYPE tmp = *ap++ ^ *bp++;
297 changed = *dstp ^ tmp;
298 *dstp++ = tmp;
301 return changed != 0;
304 void
305 sbitmap_a_xor_b (dst, a, b)
306 sbitmap dst, a, b;
308 unsigned int i, n = dst->size;
309 sbitmap_ptr dstp = dst->elms;
310 sbitmap_ptr ap = a->elms;
311 sbitmap_ptr bp = b->elms;
313 for (i = 0; i < n; i++)
314 *dstp++ = *ap++ ^ *bp++;
317 /* Set DST to be (A or B)).
318 Return nonzero if any change is made. */
320 bool
321 sbitmap_a_or_b_cg (dst, a, b)
322 sbitmap dst, a, b;
324 unsigned int i, n = dst->size;
325 sbitmap_ptr dstp = dst->elms;
326 sbitmap_ptr ap = a->elms;
327 sbitmap_ptr bp = b->elms;
328 SBITMAP_ELT_TYPE changed = 0;
330 for (i = 0; i < n; i++)
332 SBITMAP_ELT_TYPE tmp = *ap++ | *bp++;
333 changed = *dstp ^ tmp;
334 *dstp++ = tmp;
337 return changed != 0;
340 void
341 sbitmap_a_or_b (dst, a, b)
342 sbitmap dst, a, 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 /* Return nonzero if A is a subset of B. */
355 bool
356 sbitmap_a_subset_b_p (a, b)
357 sbitmap a, b;
359 unsigned int i, n = a->size;
360 sbitmap_ptr ap, bp;
362 for (ap = a->elms, bp = b->elms, i = 0; i < n; i++, ap++, bp++)
363 if ((*ap | *bp) != *bp)
364 return false;
366 return true;
369 /* Set DST to be (A or (B and C)).
370 Return nonzero if any change is made. */
372 bool
373 sbitmap_a_or_b_and_c_cg (dst, a, b, c)
374 sbitmap dst, a, b, c;
376 unsigned int i, n = dst->size;
377 sbitmap_ptr dstp = dst->elms;
378 sbitmap_ptr ap = a->elms;
379 sbitmap_ptr bp = b->elms;
380 sbitmap_ptr cp = c->elms;
381 SBITMAP_ELT_TYPE changed = 0;
383 for (i = 0; i < n; i++)
385 SBITMAP_ELT_TYPE tmp = *ap++ | (*bp++ & *cp++);
386 changed |= *dstp ^ tmp;
387 *dstp++ = tmp;
390 return changed != 0;
393 void
394 sbitmap_a_or_b_and_c (dst, a, b, c)
395 sbitmap dst, a, b, c;
397 unsigned int i, n = dst->size;
398 sbitmap_ptr dstp = dst->elms;
399 sbitmap_ptr ap = a->elms;
400 sbitmap_ptr bp = b->elms;
401 sbitmap_ptr cp = c->elms;
403 for (i = 0; i < n; i++)
404 *dstp++ = *ap++ | (*bp++ & *cp++);
407 /* Set DST to be (A and (B or C)).
408 Return nonzero if any change is made. */
410 bool
411 sbitmap_a_and_b_or_c_cg (dst, a, b, c)
412 sbitmap dst, a, b, c;
414 unsigned int i, n = dst->size;
415 sbitmap_ptr dstp = dst->elms;
416 sbitmap_ptr ap = a->elms;
417 sbitmap_ptr bp = b->elms;
418 sbitmap_ptr cp = c->elms;
419 SBITMAP_ELT_TYPE changed = 0;
421 for (i = 0; i < n; i++)
423 SBITMAP_ELT_TYPE tmp = *ap++ & (*bp++ | *cp++);
424 changed |= *dstp ^ tmp;
425 *dstp++ = tmp;
428 return changed != 0;
431 void
432 sbitmap_a_and_b_or_c (dst, a, b, c)
433 sbitmap dst, a, b, c;
435 unsigned int i, n = dst->size;
436 sbitmap_ptr dstp = dst->elms;
437 sbitmap_ptr ap = a->elms;
438 sbitmap_ptr bp = b->elms;
439 sbitmap_ptr cp = c->elms;
441 for (i = 0; i < n; i++)
442 *dstp++ = *ap++ & (*bp++ | *cp++);
445 #ifdef IN_GCC
446 /* Set the bitmap DST to the intersection of SRC of successors of
447 block number BB, using the new flow graph structures. */
449 void
450 sbitmap_intersection_of_succs (dst, src, bb)
451 sbitmap dst;
452 sbitmap *src;
453 int bb;
455 basic_block b = BASIC_BLOCK (bb);
456 unsigned int set_size = dst->size;
457 edge e;
459 for (e = b->succ; e != 0; e = e->succ_next)
461 if (e->dest == EXIT_BLOCK_PTR)
462 continue;
464 sbitmap_copy (dst, src[e->dest->index]);
465 break;
468 if (e == 0)
469 sbitmap_ones (dst);
470 else
471 for (e = e->succ_next; e != 0; e = e->succ_next)
473 unsigned int i;
474 sbitmap_ptr p, r;
476 if (e->dest == EXIT_BLOCK_PTR)
477 continue;
479 p = src[e->dest->index]->elms;
480 r = dst->elms;
481 for (i = 0; i < set_size; i++)
482 *r++ &= *p++;
486 /* Set the bitmap DST to the intersection of SRC of predecessors of
487 block number BB, using the new flow graph structures. */
489 void
490 sbitmap_intersection_of_preds (dst, src, bb)
491 sbitmap dst;
492 sbitmap *src;
493 int bb;
495 basic_block b = BASIC_BLOCK (bb);
496 unsigned int set_size = dst->size;
497 edge e;
499 for (e = b->pred; e != 0; e = e->pred_next)
501 if (e->src == ENTRY_BLOCK_PTR)
502 continue;
504 sbitmap_copy (dst, src[e->src->index]);
505 break;
508 if (e == 0)
509 sbitmap_ones (dst);
510 else
511 for (e = e->pred_next; e != 0; e = e->pred_next)
513 unsigned int i;
514 sbitmap_ptr p, r;
516 if (e->src == ENTRY_BLOCK_PTR)
517 continue;
519 p = src[e->src->index]->elms;
520 r = dst->elms;
521 for (i = 0; i < set_size; i++)
522 *r++ &= *p++;
526 /* Set the bitmap DST to the union of SRC of successors of
527 block number BB, using the new flow graph structures. */
529 void
530 sbitmap_union_of_succs (dst, src, bb)
531 sbitmap dst;
532 sbitmap *src;
533 int bb;
535 basic_block b = BASIC_BLOCK (bb);
536 unsigned int set_size = dst->size;
537 edge e;
539 for (e = b->succ; e != 0; e = e->succ_next)
541 if (e->dest == EXIT_BLOCK_PTR)
542 continue;
544 sbitmap_copy (dst, src[e->dest->index]);
545 break;
548 if (e == 0)
549 sbitmap_zero (dst);
550 else
551 for (e = e->succ_next; e != 0; e = e->succ_next)
553 unsigned int i;
554 sbitmap_ptr p, r;
556 if (e->dest == EXIT_BLOCK_PTR)
557 continue;
559 p = src[e->dest->index]->elms;
560 r = dst->elms;
561 for (i = 0; i < set_size; i++)
562 *r++ |= *p++;
566 /* Set the bitmap DST to the union of SRC of predecessors of
567 block number BB, using the new flow graph structures. */
569 void
570 sbitmap_union_of_preds (dst, src, bb)
571 sbitmap dst;
572 sbitmap *src;
573 int bb;
575 basic_block b = BASIC_BLOCK (bb);
576 unsigned int set_size = dst->size;
577 edge e;
579 for (e = b->pred; e != 0; e = e->pred_next)
581 if (e->src== ENTRY_BLOCK_PTR)
582 continue;
584 sbitmap_copy (dst, src[e->src->index]);
585 break;
588 if (e == 0)
589 sbitmap_zero (dst);
590 else
591 for (e = e->pred_next; e != 0; e = e->pred_next)
593 unsigned int i;
594 sbitmap_ptr p, r;
596 if (e->src == ENTRY_BLOCK_PTR)
597 continue;
599 p = src[e->src->index]->elms;
600 r = dst->elms;
601 for (i = 0; i < set_size; i++)
602 *r++ |= *p++;
605 #endif
607 /* Return number of first bit set in the bitmap, -1 if none. */
610 sbitmap_first_set_bit (bmap)
611 sbitmap bmap;
613 unsigned int n;
615 EXECUTE_IF_SET_IN_SBITMAP (bmap, 0, n, { return n; });
616 return -1;
619 /* Return number of last bit set in the bitmap, -1 if none. */
622 sbitmap_last_set_bit (bmap)
623 sbitmap bmap;
625 int i;
626 SBITMAP_ELT_TYPE *ptr = bmap->elms;
628 for (i = bmap->size - 1; i >= 0; i--)
630 SBITMAP_ELT_TYPE word = ptr[i];
632 if (word != 0)
634 unsigned int index = (i + 1) * SBITMAP_ELT_BITS - 1;
635 SBITMAP_ELT_TYPE mask
636 = (SBITMAP_ELT_TYPE) 1 << (SBITMAP_ELT_BITS - 1);
638 while (1)
640 if ((word & mask) != 0)
641 return index;
643 mask >>= 1;
644 index--;
649 return -1;
652 void
653 dump_sbitmap (file, bmap)
654 FILE *file;
655 sbitmap bmap;
657 unsigned int i, n, j;
658 unsigned int set_size = bmap->size;
659 unsigned int total_bits = bmap->n_bits;
661 fprintf (file, " ");
662 for (i = n = 0; i < set_size && n < total_bits; i++)
663 for (j = 0; j < SBITMAP_ELT_BITS && n < total_bits; j++, n++)
665 if (n != 0 && n % 10 == 0)
666 fprintf (file, " ");
668 fprintf (file, "%d",
669 (bmap->elms[i] & ((SBITMAP_ELT_TYPE) 1 << j)) != 0);
672 fprintf (file, "\n");
675 void
676 dump_sbitmap_file (file, bmap)
677 FILE *file;
678 sbitmap bmap;
680 unsigned int i, pos;
682 fprintf (file, "n_bits = %d, set = {", bmap->n_bits);
684 for (pos = 30, i = 0; i < bmap->n_bits; i++)
685 if (TEST_BIT (bmap, i))
687 if (pos > 70)
689 fprintf (file, "\n ");
690 pos = 0;
693 fprintf (file, "%d ", i);
694 pos += 2 + (i >= 10) + (i >= 100) + (i >= 1000);
697 fprintf (file, "}\n");
700 void
701 debug_sbitmap (bmap)
702 sbitmap bmap;
704 dump_sbitmap_file (stderr, bmap);
707 void
708 dump_sbitmap_vector (file, title, subtitle, bmaps, n_maps)
709 FILE *file;
710 const char *title, *subtitle;
711 sbitmap *bmaps;
712 int n_maps;
714 int bb;
716 fprintf (file, "%s\n", title);
717 for (bb = 0; bb < n_maps; bb++)
719 fprintf (file, "%s %d\n", subtitle, bb);
720 dump_sbitmap (file, bmaps[bb]);
723 fprintf (file, "\n");