* arm.md (stack_tie): New insn. Use an idiom that the alias code
[official-gcc.git] / gcc / sbitmap.c
blob170d87df839de7c6933cee2b0392130ccfcd1bbd
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 "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, dst_size = dst->size;
222 unsigned int min_size = dst->size;
223 sbitmap_ptr dstp = dst->elms;
224 sbitmap_ptr ap = a->elms;
225 sbitmap_ptr bp = b->elms;
227 /* A should be at least as large as DEST, to have a defined source. */
228 if (a->size < dst_size)
229 abort ();
230 /* If minuend is smaller, we simply pretend it to be zero bits, i.e.
231 only copy the subtrahend into dest. */
232 if (b->size < min_size)
233 min_size = b->size;
234 for (i = 0; i < min_size; i++)
235 *dstp++ = *ap++ & (~*bp++);
236 /* Now fill the rest of dest from A, if B was too short.
237 This makes sense only when destination and A differ. */
238 if (dst != a && i != dst_size)
239 for (; i < dst_size; i++)
240 *dstp++ = *ap++;
243 /* Set DST to be (A and B).
244 Return non-zero if any change is made. */
246 bool
247 sbitmap_a_and_b_cg (dst, a, b)
248 sbitmap dst, a, b;
250 unsigned int i, n = dst->size;
251 sbitmap_ptr dstp = dst->elms;
252 sbitmap_ptr ap = a->elms;
253 sbitmap_ptr bp = b->elms;
254 SBITMAP_ELT_TYPE changed = 0;
256 for (i = 0; i < n; i++)
258 SBITMAP_ELT_TYPE tmp = *ap++ & *bp++;
259 changed = *dstp ^ tmp;
260 *dstp++ = tmp;
263 return changed != 0;
266 void
267 sbitmap_a_and_b (dst, a, b)
268 sbitmap dst, a, b;
270 unsigned int i, n = dst->size;
271 sbitmap_ptr dstp = dst->elms;
272 sbitmap_ptr ap = a->elms;
273 sbitmap_ptr bp = b->elms;
275 for (i = 0; i < n; i++)
276 *dstp++ = *ap++ & *bp++;
279 /* Set DST to be (A xor B)).
280 Return non-zero if any change is made. */
282 bool
283 sbitmap_a_xor_b_cg (dst, a, b)
284 sbitmap dst, a, b;
286 unsigned int i, n = dst->size;
287 sbitmap_ptr dstp = dst->elms;
288 sbitmap_ptr ap = a->elms;
289 sbitmap_ptr bp = b->elms;
290 SBITMAP_ELT_TYPE changed = 0;
292 for (i = 0; i < n; i++)
294 SBITMAP_ELT_TYPE tmp = *ap++ ^ *bp++;
295 changed = *dstp ^ tmp;
296 *dstp++ = tmp;
299 return changed != 0;
302 void
303 sbitmap_a_xor_b (dst, a, b)
304 sbitmap dst, a, b;
306 unsigned int i, n = dst->size;
307 sbitmap_ptr dstp = dst->elms;
308 sbitmap_ptr ap = a->elms;
309 sbitmap_ptr bp = b->elms;
311 for (i = 0; i < n; i++)
312 *dstp++ = *ap++ ^ *bp++;
315 /* Set DST to be (A or B)).
316 Return non-zero if any change is made. */
318 bool
319 sbitmap_a_or_b_cg (dst, a, b)
320 sbitmap dst, a, b;
322 unsigned int i, n = dst->size;
323 sbitmap_ptr dstp = dst->elms;
324 sbitmap_ptr ap = a->elms;
325 sbitmap_ptr bp = b->elms;
326 SBITMAP_ELT_TYPE changed = 0;
328 for (i = 0; i < n; i++)
330 SBITMAP_ELT_TYPE tmp = *ap++ | *bp++;
331 changed = *dstp ^ tmp;
332 *dstp++ = tmp;
335 return changed != 0;
338 void
339 sbitmap_a_or_b (dst, a, b)
340 sbitmap dst, a, b;
342 unsigned int i, n = dst->size;
343 sbitmap_ptr dstp = dst->elms;
344 sbitmap_ptr ap = a->elms;
345 sbitmap_ptr bp = b->elms;
347 for (i = 0; i < n; i++)
348 *dstp++ = *ap++ | *bp++;
351 /* Return non-zero if A is a subset of B. */
353 bool
354 sbitmap_a_subset_b_p (a, b)
355 sbitmap a, b;
357 unsigned int i, n = a->size;
358 sbitmap_ptr ap, bp;
360 for (ap = a->elms, bp = b->elms, i = 0; i < n; i++, ap++, bp++)
361 if ((*ap | *bp) != *bp)
362 return false;
364 return true;
367 /* Set DST to be (A or (B and C)).
368 Return non-zero if any change is made. */
370 bool
371 sbitmap_a_or_b_and_c_cg (dst, a, b, c)
372 sbitmap dst, a, b, c;
374 unsigned int i, n = dst->size;
375 sbitmap_ptr dstp = dst->elms;
376 sbitmap_ptr ap = a->elms;
377 sbitmap_ptr bp = b->elms;
378 sbitmap_ptr cp = c->elms;
379 SBITMAP_ELT_TYPE changed = 0;
381 for (i = 0; i < n; i++)
383 SBITMAP_ELT_TYPE tmp = *ap++ | (*bp++ & *cp++);
384 changed |= *dstp ^ tmp;
385 *dstp++ = tmp;
388 return changed != 0;
391 void
392 sbitmap_a_or_b_and_c (dst, a, b, c)
393 sbitmap dst, a, b, c;
395 unsigned int i, n = dst->size;
396 sbitmap_ptr dstp = dst->elms;
397 sbitmap_ptr ap = a->elms;
398 sbitmap_ptr bp = b->elms;
399 sbitmap_ptr cp = c->elms;
401 for (i = 0; i < n; i++)
402 *dstp++ = *ap++ | (*bp++ & *cp++);
405 /* Set DST to be (A and (B or C)).
406 Return non-zero if any change is made. */
408 bool
409 sbitmap_a_and_b_or_c_cg (dst, a, b, c)
410 sbitmap dst, a, b, c;
412 unsigned int i, n = dst->size;
413 sbitmap_ptr dstp = dst->elms;
414 sbitmap_ptr ap = a->elms;
415 sbitmap_ptr bp = b->elms;
416 sbitmap_ptr cp = c->elms;
417 SBITMAP_ELT_TYPE changed = 0;
419 for (i = 0; i < n; i++)
421 SBITMAP_ELT_TYPE tmp = *ap++ & (*bp++ | *cp++);
422 changed |= *dstp ^ tmp;
423 *dstp++ = tmp;
426 return changed != 0;
429 void
430 sbitmap_a_and_b_or_c (dst, a, b, c)
431 sbitmap dst, a, b, c;
433 unsigned int i, n = dst->size;
434 sbitmap_ptr dstp = dst->elms;
435 sbitmap_ptr ap = a->elms;
436 sbitmap_ptr bp = b->elms;
437 sbitmap_ptr cp = c->elms;
439 for (i = 0; i < n; i++)
440 *dstp++ = *ap++ & (*bp++ | *cp++);
443 #ifdef IN_GCC
444 /* Set the bitmap DST to the intersection of SRC of successors of
445 block number BB, using the new flow graph structures. */
447 void
448 sbitmap_intersection_of_succs (dst, src, bb)
449 sbitmap dst;
450 sbitmap *src;
451 int bb;
453 basic_block b = BASIC_BLOCK (bb);
454 unsigned int set_size = dst->size;
455 edge e;
457 for (e = b->succ; e != 0; e = e->succ_next)
459 if (e->dest == EXIT_BLOCK_PTR)
460 continue;
462 sbitmap_copy (dst, src[e->dest->index]);
463 break;
466 if (e == 0)
467 sbitmap_ones (dst);
468 else
469 for (e = e->succ_next; e != 0; e = e->succ_next)
471 unsigned int i;
472 sbitmap_ptr p, r;
474 if (e->dest == EXIT_BLOCK_PTR)
475 continue;
477 p = src[e->dest->index]->elms;
478 r = dst->elms;
479 for (i = 0; i < set_size; i++)
480 *r++ &= *p++;
484 /* Set the bitmap DST to the intersection of SRC of predecessors of
485 block number BB, using the new flow graph structures. */
487 void
488 sbitmap_intersection_of_preds (dst, src, bb)
489 sbitmap dst;
490 sbitmap *src;
491 int bb;
493 basic_block b = BASIC_BLOCK (bb);
494 unsigned int set_size = dst->size;
495 edge e;
497 for (e = b->pred; e != 0; e = e->pred_next)
499 if (e->src == ENTRY_BLOCK_PTR)
500 continue;
502 sbitmap_copy (dst, src[e->src->index]);
503 break;
506 if (e == 0)
507 sbitmap_ones (dst);
508 else
509 for (e = e->pred_next; e != 0; e = e->pred_next)
511 unsigned int i;
512 sbitmap_ptr p, r;
514 if (e->src == ENTRY_BLOCK_PTR)
515 continue;
517 p = src[e->src->index]->elms;
518 r = dst->elms;
519 for (i = 0; i < set_size; i++)
520 *r++ &= *p++;
524 /* Set the bitmap DST to the union of SRC of successors of
525 block number BB, using the new flow graph structures. */
527 void
528 sbitmap_union_of_succs (dst, src, bb)
529 sbitmap dst;
530 sbitmap *src;
531 int bb;
533 basic_block b = BASIC_BLOCK (bb);
534 unsigned int set_size = dst->size;
535 edge e;
537 for (e = b->succ; e != 0; e = e->succ_next)
539 if (e->dest == EXIT_BLOCK_PTR)
540 continue;
542 sbitmap_copy (dst, src[e->dest->index]);
543 break;
546 if (e == 0)
547 sbitmap_zero (dst);
548 else
549 for (e = e->succ_next; e != 0; e = e->succ_next)
551 unsigned int i;
552 sbitmap_ptr p, r;
554 if (e->dest == EXIT_BLOCK_PTR)
555 continue;
557 p = src[e->dest->index]->elms;
558 r = dst->elms;
559 for (i = 0; i < set_size; i++)
560 *r++ |= *p++;
564 /* Set the bitmap DST to the union of SRC of predecessors of
565 block number BB, using the new flow graph structures. */
567 void
568 sbitmap_union_of_preds (dst, src, bb)
569 sbitmap dst;
570 sbitmap *src;
571 int bb;
573 basic_block b = BASIC_BLOCK (bb);
574 unsigned int set_size = dst->size;
575 edge e;
577 for (e = b->pred; e != 0; e = e->pred_next)
579 if (e->src== ENTRY_BLOCK_PTR)
580 continue;
582 sbitmap_copy (dst, src[e->src->index]);
583 break;
586 if (e == 0)
587 sbitmap_zero (dst);
588 else
589 for (e = e->pred_next; e != 0; e = e->pred_next)
591 unsigned int i;
592 sbitmap_ptr p, r;
594 if (e->src == ENTRY_BLOCK_PTR)
595 continue;
597 p = src[e->src->index]->elms;
598 r = dst->elms;
599 for (i = 0; i < set_size; i++)
600 *r++ |= *p++;
603 #endif
605 /* Return number of first bit set in the bitmap, -1 if none. */
608 sbitmap_first_set_bit (bmap)
609 sbitmap bmap;
611 unsigned int n;
613 EXECUTE_IF_SET_IN_SBITMAP (bmap, 0, n, { return n; });
614 return -1;
617 /* Return number of last bit set in the bitmap, -1 if none. */
620 sbitmap_last_set_bit (bmap)
621 sbitmap bmap;
623 int i;
624 SBITMAP_ELT_TYPE *ptr = bmap->elms;
626 for (i = bmap->size - 1; i >= 0; i--)
628 SBITMAP_ELT_TYPE word = ptr[i];
630 if (word != 0)
632 unsigned int index = (i + 1) * SBITMAP_ELT_BITS - 1;
633 SBITMAP_ELT_TYPE mask
634 = (SBITMAP_ELT_TYPE) 1 << (SBITMAP_ELT_BITS - 1);
636 while (1)
638 if ((word & mask) != 0)
639 return index;
641 mask >>= 1;
642 index--;
647 return -1;
650 void
651 dump_sbitmap (file, bmap)
652 FILE *file;
653 sbitmap bmap;
655 unsigned int i, n, j;
656 unsigned int set_size = bmap->size;
657 unsigned int total_bits = bmap->n_bits;
659 fprintf (file, " ");
660 for (i = n = 0; i < set_size && n < total_bits; i++)
661 for (j = 0; j < SBITMAP_ELT_BITS && n < total_bits; j++, n++)
663 if (n != 0 && n % 10 == 0)
664 fprintf (file, " ");
666 fprintf (file, "%d",
667 (bmap->elms[i] & ((SBITMAP_ELT_TYPE) 1 << j)) != 0);
670 fprintf (file, "\n");
673 void
674 dump_sbitmap_file (file, bmap)
675 FILE *file;
676 sbitmap bmap;
678 unsigned int i, pos;
680 fprintf (file, "n_bits = %d, set = {", bmap->n_bits);
682 for (pos = 30, i = 0; i < bmap->n_bits; i++)
683 if (TEST_BIT (bmap, i))
685 if (pos > 70)
687 fprintf (file, "\n ");
688 pos = 0;
691 fprintf (file, "%d ", i);
692 pos += 2 + (i >= 10) + (i >= 100) + (i >= 1000);
695 fprintf (file, "}\n");
698 void
699 debug_sbitmap (bmap)
700 sbitmap bmap;
702 dump_sbitmap_file (stderr, bmap);
705 void
706 dump_sbitmap_vector (file, title, subtitle, bmaps, n_maps)
707 FILE *file;
708 const char *title, *subtitle;
709 sbitmap *bmaps;
710 int n_maps;
712 int bb;
714 fprintf (file, "%s\n", title);
715 for (bb = 0; bb < n_maps; bb++)
717 fprintf (file, "%s %d\n", subtitle, bb);
718 dump_sbitmap (file, bmaps[bb]);
721 fprintf (file, "\n");