Use new tail-calling mechanism on ARM.
[official-gcc.git] / gcc / sbitmap.c
bloba6f710e0cc2adaa7da6b4f2186a373e0cffb707b
1 /* Simple bitmaps.
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)
9 any later version.
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. */
21 #include "config.h"
22 #include "system.h"
23 #include "rtl.h"
24 #include "flags.h"
25 #include "basic-block.h"
27 /* Bitmap manipulation routines. */
29 /* Allocate a simple bitmap of N_ELMS bits. */
31 sbitmap
32 sbitmap_alloc (n_elms)
33 unsigned int n_elms;
35 unsigned int bytes, size, amt;
36 sbitmap bmap;
38 size = SBITMAP_SET_SIZE (n_elms);
39 bytes = size * sizeof (SBITMAP_ELT_TYPE);
40 amt = (sizeof (struct simple_bitmap_def)
41 + bytes - sizeof (SBITMAP_ELT_TYPE));
42 bmap = (sbitmap) xmalloc (amt);
43 bmap->n_bits = n_elms;
44 bmap->size = size;
45 bmap->bytes = bytes;
46 return bmap;
49 /* Allocate a vector of N_VECS bitmaps of N_ELMS bits. */
51 sbitmap *
52 sbitmap_vector_alloc (n_vecs, n_elms)
53 unsigned int n_vecs, n_elms;
55 unsigned int i, bytes, offset, elm_bytes, size, amt, vector_bytes;
56 sbitmap *bitmap_vector;
58 size = SBITMAP_SET_SIZE (n_elms);
59 bytes = size * sizeof (SBITMAP_ELT_TYPE);
60 elm_bytes = (sizeof (struct simple_bitmap_def)
61 + bytes - sizeof (SBITMAP_ELT_TYPE));
62 vector_bytes = n_vecs * sizeof (sbitmap *);
64 /* Round up `vector_bytes' to account for the alignment requirements
65 of an sbitmap. One could allocate the vector-table and set of sbitmaps
66 separately, but that requires maintaining two pointers or creating
67 a cover struct to hold both pointers (so our result is still just
68 one pointer). Neither is a bad idea, but this is simpler for now. */
70 /* Based on DEFAULT_ALIGNMENT computation in obstack.c. */
71 struct { char x; SBITMAP_ELT_TYPE y; } align;
72 int alignment = (char *) & align.y - & align.x;
73 vector_bytes = (vector_bytes + alignment - 1) & ~ (alignment - 1);
76 amt = vector_bytes + (n_vecs * elm_bytes);
77 bitmap_vector = (sbitmap *) xmalloc (amt);
79 for (i = 0, offset = vector_bytes; i < n_vecs; i++, offset += elm_bytes)
81 sbitmap b = (sbitmap) ((char *) bitmap_vector + offset);
83 bitmap_vector[i] = b;
84 b->n_bits = n_elms;
85 b->size = size;
86 b->bytes = bytes;
89 return bitmap_vector;
92 /* Copy sbitmap SRC to DST. */
94 void
95 sbitmap_copy (dst, src)
96 sbitmap dst, src;
98 bcopy ((PTR) src->elms, (PTR) dst->elms,
99 sizeof (SBITMAP_ELT_TYPE) * dst->size);
102 /* Zero all elements in a bitmap. */
104 void
105 sbitmap_zero (bmap)
106 sbitmap bmap;
108 bzero ((PTR) bmap->elms, bmap->bytes);
111 /* Set all elements in a bitmap to ones. */
113 void
114 sbitmap_ones (bmap)
115 sbitmap bmap;
117 unsigned int last_bit;
119 memset ((PTR) bmap->elms, -1, bmap->bytes);
121 last_bit = bmap->n_bits % SBITMAP_ELT_BITS;
122 if (last_bit)
123 bmap->elms[bmap->size - 1]
124 = (SBITMAP_ELT_TYPE)-1 >> (SBITMAP_ELT_BITS - last_bit);
127 /* Zero a vector of N_VECS bitmaps. */
129 void
130 sbitmap_vector_zero (bmap, n_vecs)
131 sbitmap *bmap;
132 unsigned int n_vecs;
134 unsigned int i;
136 for (i = 0; i < n_vecs; i++)
137 sbitmap_zero (bmap[i]);
140 /* Set a vector of N_VECS bitmaps to ones. */
142 void
143 sbitmap_vector_ones (bmap, n_vecs)
144 sbitmap *bmap;
145 unsigned int n_vecs;
147 unsigned int i;
149 for (i = 0; i < n_vecs; i++)
150 sbitmap_ones (bmap[i]);
153 /* Set DST to be A union (B - C).
154 DST = A | (B & ~C).
155 Return non-zero if any change is made. */
158 sbitmap_union_of_diff (dst, a, b, c)
159 sbitmap dst, a, b, c;
161 unsigned int i;
162 sbitmap_ptr dstp, ap, bp, cp;
163 int changed = 0;
165 for (dstp = dst->elms, ap = a->elms, bp = b->elms, cp = c->elms, i = 0;
166 i < dst->size; i++, dstp++)
168 SBITMAP_ELT_TYPE tmp = *ap++ | (*bp++ & ~*cp++);
170 if (*dstp != tmp)
172 changed = 1;
173 *dstp = tmp;
177 return changed;
180 /* Set bitmap DST to the bitwise negation of the bitmap SRC. */
182 void
183 sbitmap_not (dst, src)
184 sbitmap dst, src;
186 unsigned int i;
187 sbitmap_ptr dstp, srcp;
189 for (dstp = dst->elms, srcp = src->elms, i = 0; i < dst->size; i++)
190 *dstp++ = ~(*srcp++);
193 /* Set the bits in DST to be the difference between the bits
194 in A and the bits in B. i.e. dst = a & (~b). */
196 void
197 sbitmap_difference (dst, a, b)
198 sbitmap dst, a, b;
200 unsigned int i;
201 sbitmap_ptr dstp, ap, bp;
203 for (dstp = dst->elms, ap = a->elms, bp = b->elms, i = 0; i < dst->size; i++)
204 *dstp++ = *ap++ & (~*bp++);
207 /* Set DST to be (A and B).
208 Return non-zero if any change is made. */
211 sbitmap_a_and_b (dst, a, b)
212 sbitmap dst, a, b;
214 unsigned int i;
215 sbitmap_ptr dstp, ap, bp;
216 int changed = 0;
218 for (dstp = dst->elms, ap = a->elms, bp = b->elms, i = 0; i < dst->size;
219 i++, dstp++)
221 SBITMAP_ELT_TYPE tmp = *ap++ & *bp++;
223 if (*dstp != tmp)
225 changed = 1;
226 *dstp = tmp;
230 return changed;
233 /* Set DST to be (A or B)).
234 Return non-zero if any change is made. */
237 sbitmap_a_or_b (dst, a, b)
238 sbitmap dst, a, b;
240 unsigned int i;
241 sbitmap_ptr dstp, ap, bp;
242 int changed = 0;
244 for (dstp = dst->elms, ap = a->elms, bp = b->elms, i = 0; i < dst->size;
245 i++, dstp++)
247 SBITMAP_ELT_TYPE tmp = *ap++ | *bp++;
249 if (*dstp != tmp)
251 changed = 1;
252 *dstp = tmp;
256 return changed;
259 /* Return non-zero if A is a subset of B. */
262 sbitmap_a_subset_b_p (a, b)
263 sbitmap a, b;
265 unsigned int i;
266 sbitmap_ptr ap, bp;
268 for (ap = a->elms, bp = b->elms, i = 0; i < a->size; i++, ap++, bp++)
269 if ((*ap | *bp) != *bp)
270 return 0;
272 return 1;
275 /* Set DST to be (A or (B and C)).
276 Return non-zero if any change is made. */
279 sbitmap_a_or_b_and_c (dst, a, b, c)
280 sbitmap dst, a, b, c;
282 unsigned int i;
283 sbitmap_ptr dstp, ap, bp, cp;
284 int changed = 0;
286 for (dstp = dst->elms, ap = a->elms, bp = b->elms, cp = c->elms, i = 0;
287 i < dst->size; i++, dstp++)
289 SBITMAP_ELT_TYPE tmp = *ap++ | (*bp++ & *cp++);
291 if (*dstp != tmp)
293 changed = 1;
294 *dstp = tmp;
298 return changed;
301 /* Set DST to be (A and (B or C)).
302 Return non-zero if any change is made. */
305 sbitmap_a_and_b_or_c (dst, a, b, c)
306 sbitmap dst, a, b, c;
308 unsigned int i;
309 sbitmap_ptr dstp, ap, bp, cp;
310 int changed = 0;
312 for (dstp = dst->elms, ap = a->elms, bp = b->elms, cp = c->elms, i = 0;
313 i < dst->size; i++, dstp++)
315 SBITMAP_ELT_TYPE tmp = *ap++ & (*bp++ | *cp++);
317 if (*dstp != tmp)
319 changed = 1;
320 *dstp = tmp;
324 return changed;
327 /* Set the bitmap DST to the intersection of SRC of successors of
328 block number BB, using the new flow graph structures. */
330 void
331 sbitmap_intersection_of_succs (dst, src, bb)
332 sbitmap dst;
333 sbitmap *src;
334 int bb;
336 basic_block b = BASIC_BLOCK (bb);
337 unsigned int set_size = dst->size;
338 edge e;
340 for (e = b->succ; e != 0; e = e->succ_next)
342 if (e->dest == EXIT_BLOCK_PTR)
343 continue;
345 sbitmap_copy (dst, src[e->dest->index]);
346 break;
349 if (e == 0)
350 sbitmap_ones (dst);
351 else
352 for (e = e->succ_next; e != 0; e = e->succ_next)
354 unsigned int i;
355 sbitmap_ptr p, r;
357 if (e->dest == EXIT_BLOCK_PTR)
358 continue;
360 p = src[e->dest->index]->elms;
361 r = dst->elms;
362 for (i = 0; i < set_size; i++)
363 *r++ &= *p++;
367 /* Set the bitmap DST to the intersection of SRC of predecessors of
368 block number BB, using the new flow graph structures. */
370 void
371 sbitmap_intersection_of_preds (dst, src, bb)
372 sbitmap dst;
373 sbitmap *src;
374 int bb;
376 basic_block b = BASIC_BLOCK (bb);
377 unsigned int set_size = dst->size;
378 edge e;
380 for (e = b->pred; e != 0; e = e->pred_next)
382 if (e->src == ENTRY_BLOCK_PTR)
383 continue;
385 sbitmap_copy (dst, src[e->src->index]);
386 break;
389 if (e == 0)
390 sbitmap_ones (dst);
391 else
392 for (e = e->pred_next; e != 0; e = e->pred_next)
394 unsigned int i;
395 sbitmap_ptr p, r;
397 if (e->src == ENTRY_BLOCK_PTR)
398 continue;
400 p = src[e->src->index]->elms;
401 r = dst->elms;
402 for (i = 0; i < set_size; i++)
403 *r++ &= *p++;
407 /* Set the bitmap DST to the union of SRC of successors of
408 block number BB, using the new flow graph structures. */
410 void
411 sbitmap_union_of_succs (dst, src, bb)
412 sbitmap dst;
413 sbitmap *src;
414 int bb;
416 basic_block b = BASIC_BLOCK (bb);
417 unsigned int set_size = dst->size;
418 edge e;
420 for (e = b->succ; e != 0; e = e->succ_next)
422 if (e->dest == EXIT_BLOCK_PTR)
423 continue;
425 sbitmap_copy (dst, src[e->dest->index]);
426 break;
429 if (e == 0)
430 sbitmap_zero (dst);
431 else
432 for (e = e->succ_next; e != 0; e = e->succ_next)
434 unsigned int i;
435 sbitmap_ptr p, r;
437 if (e->dest == EXIT_BLOCK_PTR)
438 continue;
440 p = src[e->dest->index]->elms;
441 r = dst->elms;
442 for (i = 0; i < set_size; i++)
443 *r++ |= *p++;
447 /* Set the bitmap DST to the union of SRC of predecessors of
448 block number BB, using the new flow graph structures. */
450 void
451 sbitmap_union_of_preds (dst, src, bb)
452 sbitmap dst;
453 sbitmap *src;
454 int bb;
456 basic_block b = BASIC_BLOCK (bb);
457 unsigned int set_size = dst->size;
458 edge e;
460 for (e = b->pred; e != 0; e = e->pred_next)
462 if (e->src== ENTRY_BLOCK_PTR)
463 continue;
465 sbitmap_copy (dst, src[e->src->index]);
466 break;
469 if (e == 0)
470 sbitmap_zero (dst);
471 else
472 for (e = e->pred_next; e != 0; e = e->pred_next)
474 unsigned int i;
475 sbitmap_ptr p, r;
477 if (e->src == ENTRY_BLOCK_PTR)
478 continue;
480 p = src[e->src->index]->elms;
481 r = dst->elms;
482 for (i = 0; i < set_size; i++)
483 *r++ |= *p++;
487 /* Return number of first bit set in the bitmap, -1 if none. */
490 sbitmap_first_set_bit (bmap)
491 sbitmap bmap;
493 unsigned int n;
495 EXECUTE_IF_SET_IN_SBITMAP (bmap, 0, n, { return n; });
496 return -1;
499 /* Return number of last bit set in the bitmap, -1 if none. */
502 sbitmap_last_set_bit (bmap)
503 sbitmap bmap;
505 int i;
506 SBITMAP_ELT_TYPE *ptr = bmap->elms;
508 for (i = bmap->size - 1; i >= 0; i--)
510 SBITMAP_ELT_TYPE word = ptr[i];
512 if (word != 0)
514 unsigned int index = (i + 1) * SBITMAP_ELT_BITS - 1;
515 SBITMAP_ELT_TYPE mask
516 = (SBITMAP_ELT_TYPE) 1 << (SBITMAP_ELT_BITS - 1);
518 while (1)
520 if ((word & mask) != 0)
521 return index;
523 mask >>= 1;
524 index--;
529 return -1;
532 void
533 dump_sbitmap (file, bmap)
534 FILE *file;
535 sbitmap bmap;
537 unsigned int i, n, j;
538 unsigned int set_size = bmap->size;
539 unsigned int total_bits = bmap->n_bits;
541 fprintf (file, " ");
542 for (i = n = 0; i < set_size && n < total_bits; i++)
543 for (j = 0; j < SBITMAP_ELT_BITS && n < total_bits; j++, n++)
545 if (n != 0 && n % 10 == 0)
546 fprintf (file, " ");
548 fprintf (file, "%d",
549 (bmap->elms[i] & ((SBITMAP_ELT_TYPE) 1 << j)) != 0);
552 fprintf (file, "\n");
555 void
556 debug_sbitmap (bmap)
557 sbitmap bmap;
559 unsigned int i, pos;
561 fprintf (stderr, "n_bits = %d, set = {", bmap->n_bits);
563 for (pos = 30, i = 0; i < bmap->n_bits; i++)
564 if (TEST_BIT (bmap, i))
566 if (pos > 70)
568 fprintf (stderr, "\n");
569 pos = 0;
572 fprintf (stderr, "%d ", i);
573 pos += 1 + (i >= 10) + (i >= 100);
576 fprintf (stderr, "}\n");
579 void
580 dump_sbitmap_vector (file, title, subtitle, bmaps, n_maps)
581 FILE *file;
582 const char *title, *subtitle;
583 sbitmap *bmaps;
584 int n_maps;
586 int bb;
588 fprintf (file, "%s\n", title);
589 for (bb = 0; bb < n_maps; bb++)
591 fprintf (file, "%s %d\n", subtitle, bb);
592 dump_sbitmap (file, bmaps[bb]);
595 fprintf (file, "\n");