2002-04-30 Mark Mitchell <mark@codesourcery.com>
[official-gcc.git] / gcc / sbitmap.c
blobc101cb07441c27e5a6ee61743d5860ea649db429
1 /* Simple bitmaps.
2 Copyright (C) 1999, 2000 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 "rtl.h"
24 #include "flags.h"
25 #include "hard-reg-set.h"
26 #include "basic-block.h"
28 /* Bitmap manipulation routines. */
30 /* Allocate a simple bitmap of N_ELMS bits. */
32 sbitmap
33 sbitmap_alloc (n_elms)
34 unsigned int n_elms;
36 unsigned int bytes, size, amt;
37 sbitmap bmap;
39 size = SBITMAP_SET_SIZE (n_elms);
40 bytes = size * sizeof (SBITMAP_ELT_TYPE);
41 amt = (sizeof (struct simple_bitmap_def)
42 + bytes - sizeof (SBITMAP_ELT_TYPE));
43 bmap = (sbitmap) xmalloc (amt);
44 bmap->n_bits = n_elms;
45 bmap->size = size;
46 bmap->bytes = bytes;
47 return bmap;
50 /* Allocate a vector of N_VECS bitmaps of N_ELMS bits. */
52 sbitmap *
53 sbitmap_vector_alloc (n_vecs, n_elms)
54 unsigned int n_vecs, n_elms;
56 unsigned int i, bytes, offset, elm_bytes, size, amt, vector_bytes;
57 sbitmap *bitmap_vector;
59 size = SBITMAP_SET_SIZE (n_elms);
60 bytes = size * sizeof (SBITMAP_ELT_TYPE);
61 elm_bytes = (sizeof (struct simple_bitmap_def)
62 + bytes - sizeof (SBITMAP_ELT_TYPE));
63 vector_bytes = n_vecs * sizeof (sbitmap *);
65 /* Round up `vector_bytes' to account for the alignment requirements
66 of an sbitmap. One could allocate the vector-table and set of sbitmaps
67 separately, but that requires maintaining two pointers or creating
68 a cover struct to hold both pointers (so our result is still just
69 one pointer). Neither is a bad idea, but this is simpler for now. */
71 /* Based on DEFAULT_ALIGNMENT computation in obstack.c. */
72 struct { char x; SBITMAP_ELT_TYPE y; } align;
73 int alignment = (char *) & align.y - & align.x;
74 vector_bytes = (vector_bytes + alignment - 1) & ~ (alignment - 1);
77 amt = vector_bytes + (n_vecs * elm_bytes);
78 bitmap_vector = (sbitmap *) xmalloc (amt);
80 for (i = 0, offset = vector_bytes; i < n_vecs; i++, offset += elm_bytes)
82 sbitmap b = (sbitmap) ((char *) bitmap_vector + offset);
84 bitmap_vector[i] = b;
85 b->n_bits = n_elms;
86 b->size = size;
87 b->bytes = bytes;
90 return bitmap_vector;
93 /* Copy sbitmap SRC to DST. */
95 void
96 sbitmap_copy (dst, src)
97 sbitmap dst, src;
99 memcpy (dst->elms, src->elms, sizeof (SBITMAP_ELT_TYPE) * dst->size);
102 /* Determine if a == b. */
104 sbitmap_equal (a, b)
105 sbitmap a, b;
107 return !memcmp (a->elms, b->elms, sizeof (SBITMAP_ELT_TYPE) * a->size);
110 /* Zero all elements in a bitmap. */
112 void
113 sbitmap_zero (bmap)
114 sbitmap bmap;
116 memset ((PTR) bmap->elms, 0, bmap->bytes);
119 /* Set all elements in a bitmap to ones. */
121 void
122 sbitmap_ones (bmap)
123 sbitmap bmap;
125 unsigned int last_bit;
127 memset ((PTR) bmap->elms, -1, bmap->bytes);
129 last_bit = bmap->n_bits % SBITMAP_ELT_BITS;
130 if (last_bit)
131 bmap->elms[bmap->size - 1]
132 = (SBITMAP_ELT_TYPE)-1 >> (SBITMAP_ELT_BITS - last_bit);
135 /* Zero a vector of N_VECS bitmaps. */
137 void
138 sbitmap_vector_zero (bmap, n_vecs)
139 sbitmap *bmap;
140 unsigned int n_vecs;
142 unsigned int i;
144 for (i = 0; i < n_vecs; i++)
145 sbitmap_zero (bmap[i]);
148 /* Set a vector of N_VECS bitmaps to ones. */
150 void
151 sbitmap_vector_ones (bmap, n_vecs)
152 sbitmap *bmap;
153 unsigned int n_vecs;
155 unsigned int i;
157 for (i = 0; i < n_vecs; i++)
158 sbitmap_ones (bmap[i]);
161 /* Set DST to be A union (B - C).
162 DST = A | (B & ~C).
163 Returns true if any change is made. */
165 bool
166 sbitmap_union_of_diff_cg (dst, a, b, c)
167 sbitmap dst, a, b, c;
169 unsigned int i, n = dst->size;
170 sbitmap_ptr dstp = dst->elms;
171 sbitmap_ptr ap = a->elms;
172 sbitmap_ptr bp = b->elms;
173 sbitmap_ptr cp = c->elms;
174 SBITMAP_ELT_TYPE changed = 0;
176 for (i = 0; i < n; i++)
178 SBITMAP_ELT_TYPE tmp = *ap++ | (*bp++ & ~*cp++);
179 changed |= *dstp ^ tmp;
180 *dstp++ = tmp;
183 return changed != 0;
186 void
187 sbitmap_union_of_diff (dst, a, b, c)
188 sbitmap dst, a, b, c;
190 unsigned int i, n = dst->size;
191 sbitmap_ptr dstp = dst->elms;
192 sbitmap_ptr ap = a->elms;
193 sbitmap_ptr bp = b->elms;
194 sbitmap_ptr cp = c->elms;
196 for (i = 0; i < n; i++)
197 *dstp++ = *ap++ | (*bp++ & ~*cp++);
200 /* Set bitmap DST to the bitwise negation of the bitmap SRC. */
202 void
203 sbitmap_not (dst, src)
204 sbitmap dst, src;
206 unsigned int i, n = dst->size;
207 sbitmap_ptr dstp = dst->elms;
208 sbitmap_ptr srcp = src->elms;
210 for (i = 0; i < n; i++)
211 *dstp++ = ~*srcp++;
214 /* Set the bits in DST to be the difference between the bits
215 in A and the bits in B. i.e. dst = a & (~b). */
217 void
218 sbitmap_difference (dst, a, b)
219 sbitmap dst, a, b;
221 unsigned int i, n = dst->size;
222 sbitmap_ptr dstp = dst->elms;
223 sbitmap_ptr ap = a->elms;
224 sbitmap_ptr bp = b->elms;
226 for (i = 0; i < n; i++)
227 *dstp++ = *ap++ & ~*bp++;
230 /* Set DST to be (A and B).
231 Return non-zero if any change is made. */
233 bool
234 sbitmap_a_and_b_cg (dst, a, b)
235 sbitmap dst, a, b;
237 unsigned int i, n = dst->size;
238 sbitmap_ptr dstp = dst->elms;
239 sbitmap_ptr ap = a->elms;
240 sbitmap_ptr bp = b->elms;
241 SBITMAP_ELT_TYPE changed = 0;
243 for (i = 0; i < n; i++)
245 SBITMAP_ELT_TYPE tmp = *ap++ & *bp++;
246 changed = *dstp ^ tmp;
247 *dstp++ = tmp;
250 return changed != 0;
253 void
254 sbitmap_a_and_b (dst, a, b)
255 sbitmap dst, a, b;
257 unsigned int i, n = dst->size;
258 sbitmap_ptr dstp = dst->elms;
259 sbitmap_ptr ap = a->elms;
260 sbitmap_ptr bp = b->elms;
262 for (i = 0; i < n; i++)
263 *dstp++ = *ap++ & *bp++;
266 /* Set DST to be (A xor B)).
267 Return non-zero if any change is made. */
269 bool
270 sbitmap_a_xor_b_cg (dst, a, b)
271 sbitmap dst, a, b;
273 unsigned int i, n = dst->size;
274 sbitmap_ptr dstp = dst->elms;
275 sbitmap_ptr ap = a->elms;
276 sbitmap_ptr bp = b->elms;
277 SBITMAP_ELT_TYPE changed = 0;
279 for (i = 0; i < n; i++)
281 SBITMAP_ELT_TYPE tmp = *ap++ ^ *bp++;
282 changed = *dstp ^ tmp;
283 *dstp++ = tmp;
286 return changed != 0;
289 void
290 sbitmap_a_xor_b (dst, a, b)
291 sbitmap dst, a, b;
293 unsigned int i, n = dst->size;
294 sbitmap_ptr dstp = dst->elms;
295 sbitmap_ptr ap = a->elms;
296 sbitmap_ptr bp = b->elms;
298 for (i = 0; i < n; i++)
299 *dstp++ = *ap++ ^ *bp++;
302 /* Set DST to be (A or B)).
303 Return non-zero if any change is made. */
305 bool
306 sbitmap_a_or_b_cg (dst, a, b)
307 sbitmap dst, a, b;
309 unsigned int i, n = dst->size;
310 sbitmap_ptr dstp = dst->elms;
311 sbitmap_ptr ap = a->elms;
312 sbitmap_ptr bp = b->elms;
313 SBITMAP_ELT_TYPE changed = 0;
315 for (i = 0; i < n; i++)
317 SBITMAP_ELT_TYPE tmp = *ap++ | *bp++;
318 changed = *dstp ^ tmp;
319 *dstp++ = tmp;
322 return changed != 0;
325 void
326 sbitmap_a_or_b (dst, a, b)
327 sbitmap dst, a, b;
329 unsigned int i, n = dst->size;
330 sbitmap_ptr dstp = dst->elms;
331 sbitmap_ptr ap = a->elms;
332 sbitmap_ptr bp = b->elms;
334 for (i = 0; i < n; i++)
335 *dstp++ = *ap++ | *bp++;
338 /* Return non-zero if A is a subset of B. */
340 bool
341 sbitmap_a_subset_b_p (a, b)
342 sbitmap a, b;
344 unsigned int i, n = a->size;
345 sbitmap_ptr ap, bp;
347 for (ap = a->elms, bp = b->elms, i = 0; i < n; i++, ap++, bp++)
348 if ((*ap | *bp) != *bp)
349 return false;
351 return true;
354 /* Set DST to be (A or (B and C)).
355 Return non-zero if any change is made. */
357 bool
358 sbitmap_a_or_b_and_c_cg (dst, a, b, c)
359 sbitmap dst, a, b, c;
361 unsigned int i, n = dst->size;
362 sbitmap_ptr dstp = dst->elms;
363 sbitmap_ptr ap = a->elms;
364 sbitmap_ptr bp = b->elms;
365 sbitmap_ptr cp = c->elms;
366 SBITMAP_ELT_TYPE changed = 0;
368 for (i = 0; i < n; i++)
370 SBITMAP_ELT_TYPE tmp = *ap++ | (*bp++ & *cp++);
371 changed |= *dstp ^ tmp;
372 *dstp++ = tmp;
375 return changed != 0;
378 void
379 sbitmap_a_or_b_and_c (dst, a, b, c)
380 sbitmap dst, a, b, c;
382 unsigned int i, n = dst->size;
383 sbitmap_ptr dstp = dst->elms;
384 sbitmap_ptr ap = a->elms;
385 sbitmap_ptr bp = b->elms;
386 sbitmap_ptr cp = c->elms;
388 for (i = 0; i < n; i++)
389 *dstp++ = *ap++ | (*bp++ & *cp++);
392 /* Set DST to be (A and (B or C)).
393 Return non-zero if any change is made. */
395 bool
396 sbitmap_a_and_b_or_c_cg (dst, a, b, c)
397 sbitmap dst, a, b, c;
399 unsigned int i, n = dst->size;
400 sbitmap_ptr dstp = dst->elms;
401 sbitmap_ptr ap = a->elms;
402 sbitmap_ptr bp = b->elms;
403 sbitmap_ptr cp = c->elms;
404 SBITMAP_ELT_TYPE changed = 0;
406 for (i = 0; i < n; i++)
408 SBITMAP_ELT_TYPE tmp = *ap++ & (*bp++ | *cp++);
409 changed |= *dstp ^ tmp;
410 *dstp++ = tmp;
413 return changed != 0;
416 void
417 sbitmap_a_and_b_or_c (dst, a, b, c)
418 sbitmap dst, a, b, c;
420 unsigned int i, n = dst->size;
421 sbitmap_ptr dstp = dst->elms;
422 sbitmap_ptr ap = a->elms;
423 sbitmap_ptr bp = b->elms;
424 sbitmap_ptr cp = c->elms;
426 for (i = 0; i < n; i++)
427 *dstp++ = *ap++ & (*bp++ | *cp++);
430 #ifdef IN_GCC
431 /* Set the bitmap DST to the intersection of SRC of successors of
432 block number BB, using the new flow graph structures. */
434 void
435 sbitmap_intersection_of_succs (dst, src, bb)
436 sbitmap dst;
437 sbitmap *src;
438 int bb;
440 basic_block b = BASIC_BLOCK (bb);
441 unsigned int set_size = dst->size;
442 edge e;
444 for (e = b->succ; e != 0; e = e->succ_next)
446 if (e->dest == EXIT_BLOCK_PTR)
447 continue;
449 sbitmap_copy (dst, src[e->dest->index]);
450 break;
453 if (e == 0)
454 sbitmap_ones (dst);
455 else
456 for (e = e->succ_next; e != 0; e = e->succ_next)
458 unsigned int i;
459 sbitmap_ptr p, r;
461 if (e->dest == EXIT_BLOCK_PTR)
462 continue;
464 p = src[e->dest->index]->elms;
465 r = dst->elms;
466 for (i = 0; i < set_size; i++)
467 *r++ &= *p++;
471 /* Set the bitmap DST to the intersection of SRC of predecessors of
472 block number BB, using the new flow graph structures. */
474 void
475 sbitmap_intersection_of_preds (dst, src, bb)
476 sbitmap dst;
477 sbitmap *src;
478 int bb;
480 basic_block b = BASIC_BLOCK (bb);
481 unsigned int set_size = dst->size;
482 edge e;
484 for (e = b->pred; e != 0; e = e->pred_next)
486 if (e->src == ENTRY_BLOCK_PTR)
487 continue;
489 sbitmap_copy (dst, src[e->src->index]);
490 break;
493 if (e == 0)
494 sbitmap_ones (dst);
495 else
496 for (e = e->pred_next; e != 0; e = e->pred_next)
498 unsigned int i;
499 sbitmap_ptr p, r;
501 if (e->src == ENTRY_BLOCK_PTR)
502 continue;
504 p = src[e->src->index]->elms;
505 r = dst->elms;
506 for (i = 0; i < set_size; i++)
507 *r++ &= *p++;
511 /* Set the bitmap DST to the union of SRC of successors of
512 block number BB, using the new flow graph structures. */
514 void
515 sbitmap_union_of_succs (dst, src, bb)
516 sbitmap dst;
517 sbitmap *src;
518 int bb;
520 basic_block b = BASIC_BLOCK (bb);
521 unsigned int set_size = dst->size;
522 edge e;
524 for (e = b->succ; e != 0; e = e->succ_next)
526 if (e->dest == EXIT_BLOCK_PTR)
527 continue;
529 sbitmap_copy (dst, src[e->dest->index]);
530 break;
533 if (e == 0)
534 sbitmap_zero (dst);
535 else
536 for (e = e->succ_next; e != 0; e = e->succ_next)
538 unsigned int i;
539 sbitmap_ptr p, r;
541 if (e->dest == EXIT_BLOCK_PTR)
542 continue;
544 p = src[e->dest->index]->elms;
545 r = dst->elms;
546 for (i = 0; i < set_size; i++)
547 *r++ |= *p++;
551 /* Set the bitmap DST to the union of SRC of predecessors of
552 block number BB, using the new flow graph structures. */
554 void
555 sbitmap_union_of_preds (dst, src, bb)
556 sbitmap dst;
557 sbitmap *src;
558 int bb;
560 basic_block b = BASIC_BLOCK (bb);
561 unsigned int set_size = dst->size;
562 edge e;
564 for (e = b->pred; e != 0; e = e->pred_next)
566 if (e->src== ENTRY_BLOCK_PTR)
567 continue;
569 sbitmap_copy (dst, src[e->src->index]);
570 break;
573 if (e == 0)
574 sbitmap_zero (dst);
575 else
576 for (e = e->pred_next; e != 0; e = e->pred_next)
578 unsigned int i;
579 sbitmap_ptr p, r;
581 if (e->src == ENTRY_BLOCK_PTR)
582 continue;
584 p = src[e->src->index]->elms;
585 r = dst->elms;
586 for (i = 0; i < set_size; i++)
587 *r++ |= *p++;
590 #endif
592 /* Return number of first bit set in the bitmap, -1 if none. */
595 sbitmap_first_set_bit (bmap)
596 sbitmap bmap;
598 unsigned int n;
600 EXECUTE_IF_SET_IN_SBITMAP (bmap, 0, n, { return n; });
601 return -1;
604 /* Return number of last bit set in the bitmap, -1 if none. */
607 sbitmap_last_set_bit (bmap)
608 sbitmap bmap;
610 int i;
611 SBITMAP_ELT_TYPE *ptr = bmap->elms;
613 for (i = bmap->size - 1; i >= 0; i--)
615 SBITMAP_ELT_TYPE word = ptr[i];
617 if (word != 0)
619 unsigned int index = (i + 1) * SBITMAP_ELT_BITS - 1;
620 SBITMAP_ELT_TYPE mask
621 = (SBITMAP_ELT_TYPE) 1 << (SBITMAP_ELT_BITS - 1);
623 while (1)
625 if ((word & mask) != 0)
626 return index;
628 mask >>= 1;
629 index--;
634 return -1;
637 void
638 dump_sbitmap (file, bmap)
639 FILE *file;
640 sbitmap bmap;
642 unsigned int i, n, j;
643 unsigned int set_size = bmap->size;
644 unsigned int total_bits = bmap->n_bits;
646 fprintf (file, " ");
647 for (i = n = 0; i < set_size && n < total_bits; i++)
648 for (j = 0; j < SBITMAP_ELT_BITS && n < total_bits; j++, n++)
650 if (n != 0 && n % 10 == 0)
651 fprintf (file, " ");
653 fprintf (file, "%d",
654 (bmap->elms[i] & ((SBITMAP_ELT_TYPE) 1 << j)) != 0);
657 fprintf (file, "\n");
660 void
661 debug_sbitmap (bmap)
662 sbitmap bmap;
664 unsigned int i, pos;
666 fprintf (stderr, "n_bits = %d, set = {", bmap->n_bits);
668 for (pos = 30, i = 0; i < bmap->n_bits; i++)
669 if (TEST_BIT (bmap, i))
671 if (pos > 70)
673 fprintf (stderr, "\n");
674 pos = 0;
677 fprintf (stderr, "%d ", i);
678 pos += 1 + (i >= 10) + (i >= 100);
681 fprintf (stderr, "}\n");
684 void
685 dump_sbitmap_vector (file, title, subtitle, bmaps, n_maps)
686 FILE *file;
687 const char *title, *subtitle;
688 sbitmap *bmaps;
689 int n_maps;
691 int bb;
693 fprintf (file, "%s\n", title);
694 for (bb = 0; bb < n_maps; bb++)
696 fprintf (file, "%s %d\n", subtitle, bb);
697 dump_sbitmap (file, bmaps[bb]);
700 fprintf (file, "\n");