2 Copyright (C) 1999, 2000 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
25 #include "hard-reg-set.h"
26 #include "basic-block.h"
28 /* Bitmap manipulation routines. */
30 /* Allocate a simple bitmap of N_ELMS bits. */
33 sbitmap_alloc (n_elms)
36 unsigned int bytes, size, amt;
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;
50 /* Allocate a vector of N_VECS bitmaps of N_ELMS bits. */
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);
93 /* Copy sbitmap SRC to DST. */
96 sbitmap_copy (dst, src)
99 bcopy ((PTR) src->elms, (PTR) dst->elms,
100 sizeof (SBITMAP_ELT_TYPE) * dst->size);
103 /* Zero all elements in a bitmap. */
109 memset ((PTR) bmap->elms, 0, bmap->bytes);
112 /* Set all elements in a bitmap to ones. */
118 unsigned int last_bit;
120 memset ((PTR) bmap->elms, -1, bmap->bytes);
122 last_bit = bmap->n_bits % SBITMAP_ELT_BITS;
124 bmap->elms[bmap->size - 1]
125 = (SBITMAP_ELT_TYPE)-1 >> (SBITMAP_ELT_BITS - last_bit);
128 /* Zero a vector of N_VECS bitmaps. */
131 sbitmap_vector_zero (bmap, n_vecs)
137 for (i = 0; i < n_vecs; i++)
138 sbitmap_zero (bmap[i]);
141 /* Set a vector of N_VECS bitmaps to ones. */
144 sbitmap_vector_ones (bmap, n_vecs)
150 for (i = 0; i < n_vecs; i++)
151 sbitmap_ones (bmap[i]);
154 /* Set DST to be A union (B - C).
156 Return non-zero if any change is made. */
159 sbitmap_union_of_diff (dst, a, b, c)
160 sbitmap dst, a, b, c;
163 sbitmap_ptr dstp, ap, bp, cp;
166 for (dstp = dst->elms, ap = a->elms, bp = b->elms, cp = c->elms, i = 0;
167 i < dst->size; i++, dstp++)
169 SBITMAP_ELT_TYPE tmp = *ap++ | (*bp++ & ~*cp++);
181 /* Set bitmap DST to the bitwise negation of the bitmap SRC. */
184 sbitmap_not (dst, src)
188 sbitmap_ptr dstp, srcp;
190 for (dstp = dst->elms, srcp = src->elms, i = 0; i < dst->size; i++)
191 *dstp++ = ~(*srcp++);
194 /* Set the bits in DST to be the difference between the bits
195 in A and the bits in B. i.e. dst = a & (~b). */
198 sbitmap_difference (dst, a, b)
202 sbitmap_ptr dstp, ap, bp;
204 for (dstp = dst->elms, ap = a->elms, bp = b->elms, i = 0; i < dst->size; i++)
205 *dstp++ = *ap++ & (~*bp++);
208 /* Set DST to be (A and B).
209 Return non-zero if any change is made. */
212 sbitmap_a_and_b (dst, a, b)
216 sbitmap_ptr dstp, ap, bp;
219 for (dstp = dst->elms, ap = a->elms, bp = b->elms, i = 0; i < dst->size;
222 SBITMAP_ELT_TYPE tmp = *ap++ & *bp++;
234 /* Set DST to be (A or B)).
235 Return non-zero if any change is made. */
238 sbitmap_a_or_b (dst, a, b)
242 sbitmap_ptr dstp, ap, bp;
245 for (dstp = dst->elms, ap = a->elms, bp = b->elms, i = 0; i < dst->size;
248 SBITMAP_ELT_TYPE tmp = *ap++ | *bp++;
260 /* Return non-zero if A is a subset of B. */
263 sbitmap_a_subset_b_p (a, b)
269 for (ap = a->elms, bp = b->elms, i = 0; i < a->size; i++, ap++, bp++)
270 if ((*ap | *bp) != *bp)
276 /* Set DST to be (A or (B and C)).
277 Return non-zero if any change is made. */
280 sbitmap_a_or_b_and_c (dst, a, b, c)
281 sbitmap dst, a, b, c;
284 sbitmap_ptr dstp, ap, bp, cp;
287 for (dstp = dst->elms, ap = a->elms, bp = b->elms, cp = c->elms, i = 0;
288 i < dst->size; i++, dstp++)
290 SBITMAP_ELT_TYPE tmp = *ap++ | (*bp++ & *cp++);
302 /* Set DST to be (A and (B or C)).
303 Return non-zero if any change is made. */
306 sbitmap_a_and_b_or_c (dst, a, b, c)
307 sbitmap dst, a, b, c;
310 sbitmap_ptr dstp, ap, bp, cp;
313 for (dstp = dst->elms, ap = a->elms, bp = b->elms, cp = c->elms, i = 0;
314 i < dst->size; i++, dstp++)
316 SBITMAP_ELT_TYPE tmp = *ap++ & (*bp++ | *cp++);
328 /* Set the bitmap DST to the intersection of SRC of successors of
329 block number BB, using the new flow graph structures. */
332 sbitmap_intersection_of_succs (dst, src, bb)
337 basic_block b = BASIC_BLOCK (bb);
338 unsigned int set_size = dst->size;
341 for (e = b->succ; e != 0; e = e->succ_next)
343 if (e->dest == EXIT_BLOCK_PTR)
346 sbitmap_copy (dst, src[e->dest->index]);
353 for (e = e->succ_next; e != 0; e = e->succ_next)
358 if (e->dest == EXIT_BLOCK_PTR)
361 p = src[e->dest->index]->elms;
363 for (i = 0; i < set_size; i++)
368 /* Set the bitmap DST to the intersection of SRC of predecessors of
369 block number BB, using the new flow graph structures. */
372 sbitmap_intersection_of_preds (dst, src, bb)
377 basic_block b = BASIC_BLOCK (bb);
378 unsigned int set_size = dst->size;
381 for (e = b->pred; e != 0; e = e->pred_next)
383 if (e->src == ENTRY_BLOCK_PTR)
386 sbitmap_copy (dst, src[e->src->index]);
393 for (e = e->pred_next; e != 0; e = e->pred_next)
398 if (e->src == ENTRY_BLOCK_PTR)
401 p = src[e->src->index]->elms;
403 for (i = 0; i < set_size; i++)
408 /* Set the bitmap DST to the union of SRC of successors of
409 block number BB, using the new flow graph structures. */
412 sbitmap_union_of_succs (dst, src, bb)
417 basic_block b = BASIC_BLOCK (bb);
418 unsigned int set_size = dst->size;
421 for (e = b->succ; e != 0; e = e->succ_next)
423 if (e->dest == EXIT_BLOCK_PTR)
426 sbitmap_copy (dst, src[e->dest->index]);
433 for (e = e->succ_next; e != 0; e = e->succ_next)
438 if (e->dest == EXIT_BLOCK_PTR)
441 p = src[e->dest->index]->elms;
443 for (i = 0; i < set_size; i++)
448 /* Set the bitmap DST to the union of SRC of predecessors of
449 block number BB, using the new flow graph structures. */
452 sbitmap_union_of_preds (dst, src, bb)
457 basic_block b = BASIC_BLOCK (bb);
458 unsigned int set_size = dst->size;
461 for (e = b->pred; e != 0; e = e->pred_next)
463 if (e->src== ENTRY_BLOCK_PTR)
466 sbitmap_copy (dst, src[e->src->index]);
473 for (e = e->pred_next; e != 0; e = e->pred_next)
478 if (e->src == ENTRY_BLOCK_PTR)
481 p = src[e->src->index]->elms;
483 for (i = 0; i < set_size; i++)
488 /* Return number of first bit set in the bitmap, -1 if none. */
491 sbitmap_first_set_bit (bmap)
496 EXECUTE_IF_SET_IN_SBITMAP (bmap, 0, n, { return n; });
500 /* Return number of last bit set in the bitmap, -1 if none. */
503 sbitmap_last_set_bit (bmap)
507 SBITMAP_ELT_TYPE *ptr = bmap->elms;
509 for (i = bmap->size - 1; i >= 0; i--)
511 SBITMAP_ELT_TYPE word = ptr[i];
515 unsigned int index = (i + 1) * SBITMAP_ELT_BITS - 1;
516 SBITMAP_ELT_TYPE mask
517 = (SBITMAP_ELT_TYPE) 1 << (SBITMAP_ELT_BITS - 1);
521 if ((word & mask) != 0)
534 dump_sbitmap (file, bmap)
538 unsigned int i, n, j;
539 unsigned int set_size = bmap->size;
540 unsigned int total_bits = bmap->n_bits;
543 for (i = n = 0; i < set_size && n < total_bits; i++)
544 for (j = 0; j < SBITMAP_ELT_BITS && n < total_bits; j++, n++)
546 if (n != 0 && n % 10 == 0)
550 (bmap->elms[i] & ((SBITMAP_ELT_TYPE) 1 << j)) != 0);
553 fprintf (file, "\n");
562 fprintf (stderr, "n_bits = %d, set = {", bmap->n_bits);
564 for (pos = 30, i = 0; i < bmap->n_bits; i++)
565 if (TEST_BIT (bmap, i))
569 fprintf (stderr, "\n");
573 fprintf (stderr, "%d ", i);
574 pos += 1 + (i >= 10) + (i >= 100);
577 fprintf (stderr, "}\n");
581 dump_sbitmap_vector (file, title, subtitle, bmaps, n_maps)
583 const char *title, *subtitle;
589 fprintf (file, "%s\n", title);
590 for (bb = 0; bb < n_maps; bb++)
592 fprintf (file, "%s %d\n", subtitle, bb);
593 dump_sbitmap (file, bmaps[bb]);
596 fprintf (file, "\n");