2 Copyright (C) 1999, 2000, 2002, 2003 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
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
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
23 #include "coretypes.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. */
35 sbitmap_alloc (unsigned int n_elms
)
37 unsigned int bytes
, size
, amt
;
40 size
= SBITMAP_SET_SIZE (n_elms
);
41 bytes
= size
* sizeof (SBITMAP_ELT_TYPE
);
42 amt
= (sizeof (struct simple_bitmap_def
)
43 + bytes
- sizeof (SBITMAP_ELT_TYPE
));
45 bmap
->n_bits
= n_elms
;
51 /* Resize a simple bitmap BMAP to N_ELMS bits. If increasing the
52 size of BMAP, clear the new bits to zero if the DEF argument
53 is zero, and set them to one otherwise. */
56 sbitmap_resize (sbitmap bmap
, unsigned int n_elms
, int def
)
58 unsigned int bytes
, size
, amt
;
59 unsigned int last_bit
;
61 size
= SBITMAP_SET_SIZE (n_elms
);
62 bytes
= size
* sizeof (SBITMAP_ELT_TYPE
);
63 if (bytes
> bmap
->bytes
)
65 amt
= (sizeof (struct simple_bitmap_def
)
66 + bytes
- sizeof (SBITMAP_ELT_TYPE
));
67 bmap
= xrealloc (bmap
, amt
);
70 if (n_elms
> bmap
->n_bits
)
74 memset (bmap
->elms
+ bmap
->size
, -1, bytes
- bmap
->bytes
);
76 /* Set the new bits if the original last element. */
77 last_bit
= bmap
->n_bits
% SBITMAP_ELT_BITS
;
79 bmap
->elms
[bmap
->size
- 1]
80 |= ~((SBITMAP_ELT_TYPE
)-1 >> (SBITMAP_ELT_BITS
- last_bit
));
82 /* Clear the unused bit in the new last element. */
83 last_bit
= n_elms
% SBITMAP_ELT_BITS
;
86 &= (SBITMAP_ELT_TYPE
)-1 >> (SBITMAP_ELT_BITS
- last_bit
);
89 memset (bmap
->elms
+ bmap
->size
, 0, bytes
- bmap
->bytes
);
91 else if (n_elms
< bmap
->n_bits
)
93 /* Clear the surplus bits in the last word. */
94 last_bit
= n_elms
% SBITMAP_ELT_BITS
;
97 &= (SBITMAP_ELT_TYPE
)-1 >> (SBITMAP_ELT_BITS
- last_bit
);
100 bmap
->n_bits
= n_elms
;
106 /* Re-allocate a simple bitmap of N_ELMS bits. New storage is uninitialized. */
109 sbitmap_realloc (sbitmap src
, unsigned int n_elms
)
111 unsigned int bytes
, size
, amt
;
114 size
= SBITMAP_SET_SIZE (n_elms
);
115 bytes
= size
* sizeof (SBITMAP_ELT_TYPE
);
116 amt
= (sizeof (struct simple_bitmap_def
)
117 + bytes
- sizeof (SBITMAP_ELT_TYPE
));
119 if (src
->bytes
>= bytes
)
121 src
->n_bits
= n_elms
;
125 bmap
= (sbitmap
) xrealloc (src
, amt
);
126 bmap
->n_bits
= n_elms
;
132 /* Allocate a vector of N_VECS bitmaps of N_ELMS bits. */
135 sbitmap_vector_alloc (unsigned int n_vecs
, unsigned int n_elms
)
137 unsigned int i
, bytes
, offset
, elm_bytes
, size
, amt
, vector_bytes
;
138 sbitmap
*bitmap_vector
;
140 size
= SBITMAP_SET_SIZE (n_elms
);
141 bytes
= size
* sizeof (SBITMAP_ELT_TYPE
);
142 elm_bytes
= (sizeof (struct simple_bitmap_def
)
143 + bytes
- sizeof (SBITMAP_ELT_TYPE
));
144 vector_bytes
= n_vecs
* sizeof (sbitmap
*);
146 /* Round up `vector_bytes' to account for the alignment requirements
147 of an sbitmap. One could allocate the vector-table and set of sbitmaps
148 separately, but that requires maintaining two pointers or creating
149 a cover struct to hold both pointers (so our result is still just
150 one pointer). Neither is a bad idea, but this is simpler for now. */
152 /* Based on DEFAULT_ALIGNMENT computation in obstack.c. */
153 struct { char x
; SBITMAP_ELT_TYPE y
; } align
;
154 int alignment
= (char *) & align
.y
- & align
.x
;
155 vector_bytes
= (vector_bytes
+ alignment
- 1) & ~ (alignment
- 1);
158 amt
= vector_bytes
+ (n_vecs
* elm_bytes
);
159 bitmap_vector
= xmalloc (amt
);
161 for (i
= 0, offset
= vector_bytes
; i
< n_vecs
; i
++, offset
+= elm_bytes
)
163 sbitmap b
= (sbitmap
) ((char *) bitmap_vector
+ offset
);
165 bitmap_vector
[i
] = b
;
171 return bitmap_vector
;
174 /* Copy sbitmap SRC to DST. */
177 sbitmap_copy (sbitmap dst
, sbitmap src
)
179 memcpy (dst
->elms
, src
->elms
, sizeof (SBITMAP_ELT_TYPE
) * dst
->size
);
182 /* Determine if a == b. */
184 sbitmap_equal (sbitmap a
, sbitmap b
)
186 return !memcmp (a
->elms
, b
->elms
, sizeof (SBITMAP_ELT_TYPE
) * a
->size
);
189 /* Zero all elements in a bitmap. */
192 sbitmap_zero (sbitmap bmap
)
194 memset (bmap
->elms
, 0, bmap
->bytes
);
197 /* Set all elements in a bitmap to ones. */
200 sbitmap_ones (sbitmap bmap
)
202 unsigned int last_bit
;
204 memset (bmap
->elms
, -1, bmap
->bytes
);
206 last_bit
= bmap
->n_bits
% SBITMAP_ELT_BITS
;
208 bmap
->elms
[bmap
->size
- 1]
209 = (SBITMAP_ELT_TYPE
)-1 >> (SBITMAP_ELT_BITS
- last_bit
);
212 /* Zero a vector of N_VECS bitmaps. */
215 sbitmap_vector_zero (sbitmap
*bmap
, unsigned int n_vecs
)
219 for (i
= 0; i
< n_vecs
; i
++)
220 sbitmap_zero (bmap
[i
]);
223 /* Set a vector of N_VECS bitmaps to ones. */
226 sbitmap_vector_ones (sbitmap
*bmap
, unsigned int n_vecs
)
230 for (i
= 0; i
< n_vecs
; i
++)
231 sbitmap_ones (bmap
[i
]);
234 /* Set DST to be A union (B - C).
236 Returns true if any change is made. */
239 sbitmap_union_of_diff_cg (sbitmap dst
, sbitmap a
, sbitmap b
, sbitmap c
)
241 unsigned int i
, n
= dst
->size
;
242 sbitmap_ptr dstp
= dst
->elms
;
243 sbitmap_ptr ap
= a
->elms
;
244 sbitmap_ptr bp
= b
->elms
;
245 sbitmap_ptr cp
= c
->elms
;
246 SBITMAP_ELT_TYPE changed
= 0;
248 for (i
= 0; i
< n
; i
++)
250 SBITMAP_ELT_TYPE tmp
= *ap
++ | (*bp
++ & ~*cp
++);
251 changed
|= *dstp
^ tmp
;
259 sbitmap_union_of_diff (sbitmap dst
, sbitmap a
, sbitmap b
, sbitmap c
)
261 unsigned int i
, n
= dst
->size
;
262 sbitmap_ptr dstp
= dst
->elms
;
263 sbitmap_ptr ap
= a
->elms
;
264 sbitmap_ptr bp
= b
->elms
;
265 sbitmap_ptr cp
= c
->elms
;
267 for (i
= 0; i
< n
; i
++)
268 *dstp
++ = *ap
++ | (*bp
++ & ~*cp
++);
271 /* Set bitmap DST to the bitwise negation of the bitmap SRC. */
274 sbitmap_not (sbitmap dst
, sbitmap src
)
276 unsigned int i
, n
= dst
->size
;
277 sbitmap_ptr dstp
= dst
->elms
;
278 sbitmap_ptr srcp
= src
->elms
;
279 unsigned int last_bit
;
281 for (i
= 0; i
< n
; i
++)
284 /* Zero all bits past n_bits, by ANDing dst with sbitmap_ones. */
285 last_bit
= src
->n_bits
% SBITMAP_ELT_BITS
;
287 dst
->elms
[n
-1] = dst
->elms
[n
-1]
288 & ((SBITMAP_ELT_TYPE
)-1 >> (SBITMAP_ELT_BITS
- last_bit
));
291 /* Set the bits in DST to be the difference between the bits
292 in A and the bits in B. i.e. dst = a & (~b). */
295 sbitmap_difference (sbitmap dst
, sbitmap a
, sbitmap b
)
297 unsigned int i
, dst_size
= dst
->size
;
298 unsigned int min_size
= dst
->size
;
299 sbitmap_ptr dstp
= dst
->elms
;
300 sbitmap_ptr ap
= a
->elms
;
301 sbitmap_ptr bp
= b
->elms
;
303 /* A should be at least as large as DEST, to have a defined source. */
304 gcc_assert (a
->size
>= dst_size
);
305 /* If minuend is smaller, we simply pretend it to be zero bits, i.e.
306 only copy the subtrahend into dest. */
307 if (b
->size
< min_size
)
309 for (i
= 0; i
< min_size
; i
++)
310 *dstp
++ = *ap
++ & (~*bp
++);
311 /* Now fill the rest of dest from A, if B was too short.
312 This makes sense only when destination and A differ. */
313 if (dst
!= a
&& i
!= dst_size
)
314 for (; i
< dst_size
; i
++)
318 /* Set DST to be (A and B).
319 Return nonzero if any change is made. */
322 sbitmap_a_and_b_cg (sbitmap dst
, sbitmap a
, sbitmap 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
;
341 sbitmap_a_and_b (sbitmap dst
, sbitmap a
, sbitmap b
)
343 unsigned int i
, n
= dst
->size
;
344 sbitmap_ptr dstp
= dst
->elms
;
345 sbitmap_ptr ap
= a
->elms
;
346 sbitmap_ptr bp
= b
->elms
;
348 for (i
= 0; i
< n
; i
++)
349 *dstp
++ = *ap
++ & *bp
++;
352 /* Set DST to be (A xor B)).
353 Return nonzero if any change is made. */
356 sbitmap_a_xor_b_cg (sbitmap dst
, sbitmap a
, sbitmap b
)
358 unsigned int i
, n
= dst
->size
;
359 sbitmap_ptr dstp
= dst
->elms
;
360 sbitmap_ptr ap
= a
->elms
;
361 sbitmap_ptr bp
= b
->elms
;
362 SBITMAP_ELT_TYPE changed
= 0;
364 for (i
= 0; i
< n
; i
++)
366 SBITMAP_ELT_TYPE tmp
= *ap
++ ^ *bp
++;
367 changed
|= *dstp
^ tmp
;
375 sbitmap_a_xor_b (sbitmap dst
, sbitmap a
, sbitmap b
)
377 unsigned int i
, n
= dst
->size
;
378 sbitmap_ptr dstp
= dst
->elms
;
379 sbitmap_ptr ap
= a
->elms
;
380 sbitmap_ptr bp
= b
->elms
;
382 for (i
= 0; i
< n
; i
++)
383 *dstp
++ = *ap
++ ^ *bp
++;
386 /* Set DST to be (A or B)).
387 Return nonzero if any change is made. */
390 sbitmap_a_or_b_cg (sbitmap dst
, sbitmap a
, sbitmap b
)
392 unsigned int i
, n
= dst
->size
;
393 sbitmap_ptr dstp
= dst
->elms
;
394 sbitmap_ptr ap
= a
->elms
;
395 sbitmap_ptr bp
= b
->elms
;
396 SBITMAP_ELT_TYPE changed
= 0;
398 for (i
= 0; i
< n
; i
++)
400 SBITMAP_ELT_TYPE tmp
= *ap
++ | *bp
++;
401 changed
|= *dstp
^ tmp
;
409 sbitmap_a_or_b (sbitmap dst
, sbitmap a
, sbitmap b
)
411 unsigned int i
, n
= dst
->size
;
412 sbitmap_ptr dstp
= dst
->elms
;
413 sbitmap_ptr ap
= a
->elms
;
414 sbitmap_ptr bp
= b
->elms
;
416 for (i
= 0; i
< n
; i
++)
417 *dstp
++ = *ap
++ | *bp
++;
420 /* Return nonzero if A is a subset of B. */
423 sbitmap_a_subset_b_p (sbitmap a
, sbitmap b
)
425 unsigned int i
, n
= a
->size
;
428 for (ap
= a
->elms
, bp
= b
->elms
, i
= 0; i
< n
; i
++, ap
++, bp
++)
429 if ((*ap
| *bp
) != *bp
)
435 /* Set DST to be (A or (B and C)).
436 Return nonzero if any change is made. */
439 sbitmap_a_or_b_and_c_cg (sbitmap dst
, sbitmap a
, sbitmap b
, sbitmap c
)
441 unsigned int i
, n
= dst
->size
;
442 sbitmap_ptr dstp
= dst
->elms
;
443 sbitmap_ptr ap
= a
->elms
;
444 sbitmap_ptr bp
= b
->elms
;
445 sbitmap_ptr cp
= c
->elms
;
446 SBITMAP_ELT_TYPE changed
= 0;
448 for (i
= 0; i
< n
; i
++)
450 SBITMAP_ELT_TYPE tmp
= *ap
++ | (*bp
++ & *cp
++);
451 changed
|= *dstp
^ tmp
;
459 sbitmap_a_or_b_and_c (sbitmap dst
, sbitmap a
, sbitmap b
, sbitmap c
)
461 unsigned int i
, n
= dst
->size
;
462 sbitmap_ptr dstp
= dst
->elms
;
463 sbitmap_ptr ap
= a
->elms
;
464 sbitmap_ptr bp
= b
->elms
;
465 sbitmap_ptr cp
= c
->elms
;
467 for (i
= 0; i
< n
; i
++)
468 *dstp
++ = *ap
++ | (*bp
++ & *cp
++);
471 /* Set DST to be (A and (B or C)).
472 Return nonzero if any change is made. */
475 sbitmap_a_and_b_or_c_cg (sbitmap dst
, sbitmap a
, sbitmap b
, sbitmap c
)
477 unsigned int i
, n
= dst
->size
;
478 sbitmap_ptr dstp
= dst
->elms
;
479 sbitmap_ptr ap
= a
->elms
;
480 sbitmap_ptr bp
= b
->elms
;
481 sbitmap_ptr cp
= c
->elms
;
482 SBITMAP_ELT_TYPE changed
= 0;
484 for (i
= 0; i
< n
; i
++)
486 SBITMAP_ELT_TYPE tmp
= *ap
++ & (*bp
++ | *cp
++);
487 changed
|= *dstp
^ tmp
;
495 sbitmap_a_and_b_or_c (sbitmap dst
, sbitmap a
, sbitmap b
, sbitmap c
)
497 unsigned int i
, n
= dst
->size
;
498 sbitmap_ptr dstp
= dst
->elms
;
499 sbitmap_ptr ap
= a
->elms
;
500 sbitmap_ptr bp
= b
->elms
;
501 sbitmap_ptr cp
= c
->elms
;
503 for (i
= 0; i
< n
; i
++)
504 *dstp
++ = *ap
++ & (*bp
++ | *cp
++);
508 /* Set the bitmap DST to the intersection of SRC of successors of
509 block number BB, using the new flow graph structures. */
512 sbitmap_intersection_of_succs (sbitmap dst
, sbitmap
*src
, int bb
)
514 basic_block b
= BASIC_BLOCK (bb
);
515 unsigned int set_size
= dst
->size
;
518 for (e
= b
->succ
; e
!= 0; e
= e
->succ_next
)
520 if (e
->dest
== EXIT_BLOCK_PTR
)
523 sbitmap_copy (dst
, src
[e
->dest
->index
]);
530 for (e
= e
->succ_next
; e
!= 0; e
= e
->succ_next
)
535 if (e
->dest
== EXIT_BLOCK_PTR
)
538 p
= src
[e
->dest
->index
]->elms
;
540 for (i
= 0; i
< set_size
; i
++)
545 /* Set the bitmap DST to the intersection of SRC of predecessors of
546 block number BB, using the new flow graph structures. */
549 sbitmap_intersection_of_preds (sbitmap dst
, sbitmap
*src
, int bb
)
551 basic_block b
= BASIC_BLOCK (bb
);
552 unsigned int set_size
= dst
->size
;
555 for (e
= b
->pred
; e
!= 0; e
= e
->pred_next
)
557 if (e
->src
== ENTRY_BLOCK_PTR
)
560 sbitmap_copy (dst
, src
[e
->src
->index
]);
567 for (e
= e
->pred_next
; e
!= 0; e
= e
->pred_next
)
572 if (e
->src
== ENTRY_BLOCK_PTR
)
575 p
= src
[e
->src
->index
]->elms
;
577 for (i
= 0; i
< set_size
; i
++)
582 /* Set the bitmap DST to the union of SRC of successors of
583 block number BB, using the new flow graph structures. */
586 sbitmap_union_of_succs (sbitmap dst
, sbitmap
*src
, int bb
)
588 basic_block b
= BASIC_BLOCK (bb
);
589 unsigned int set_size
= dst
->size
;
592 for (e
= b
->succ
; e
!= 0; e
= e
->succ_next
)
594 if (e
->dest
== EXIT_BLOCK_PTR
)
597 sbitmap_copy (dst
, src
[e
->dest
->index
]);
604 for (e
= e
->succ_next
; e
!= 0; e
= e
->succ_next
)
609 if (e
->dest
== EXIT_BLOCK_PTR
)
612 p
= src
[e
->dest
->index
]->elms
;
614 for (i
= 0; i
< set_size
; i
++)
619 /* Set the bitmap DST to the union of SRC of predecessors of
620 block number BB, using the new flow graph structures. */
623 sbitmap_union_of_preds (sbitmap dst
, sbitmap
*src
, int bb
)
625 basic_block b
= BASIC_BLOCK (bb
);
626 unsigned int set_size
= dst
->size
;
629 for (e
= b
->pred
; e
!= 0; e
= e
->pred_next
)
631 if (e
->src
== ENTRY_BLOCK_PTR
)
634 sbitmap_copy (dst
, src
[e
->src
->index
]);
641 for (e
= e
->pred_next
; e
!= 0; e
= e
->pred_next
)
646 if (e
->src
== ENTRY_BLOCK_PTR
)
649 p
= src
[e
->src
->index
]->elms
;
651 for (i
= 0; i
< set_size
; i
++)
657 /* Return number of first bit set in the bitmap, -1 if none. */
660 sbitmap_first_set_bit (sbitmap bmap
)
664 EXECUTE_IF_SET_IN_SBITMAP (bmap
, 0, n
, { return n
; });
668 /* Return number of last bit set in the bitmap, -1 if none. */
671 sbitmap_last_set_bit (sbitmap bmap
)
674 SBITMAP_ELT_TYPE
*ptr
= bmap
->elms
;
676 for (i
= bmap
->size
- 1; i
>= 0; i
--)
678 SBITMAP_ELT_TYPE word
= ptr
[i
];
682 unsigned int index
= (i
+ 1) * SBITMAP_ELT_BITS
- 1;
683 SBITMAP_ELT_TYPE mask
684 = (SBITMAP_ELT_TYPE
) 1 << (SBITMAP_ELT_BITS
- 1);
688 if ((word
& mask
) != 0)
701 dump_sbitmap (FILE *file
, sbitmap bmap
)
703 unsigned int i
, n
, j
;
704 unsigned int set_size
= bmap
->size
;
705 unsigned int total_bits
= bmap
->n_bits
;
708 for (i
= n
= 0; i
< set_size
&& n
< total_bits
; i
++)
709 for (j
= 0; j
< SBITMAP_ELT_BITS
&& n
< total_bits
; j
++, n
++)
711 if (n
!= 0 && n
% 10 == 0)
715 (bmap
->elms
[i
] & ((SBITMAP_ELT_TYPE
) 1 << j
)) != 0);
718 fprintf (file
, "\n");
722 dump_sbitmap_file (FILE *file
, sbitmap bmap
)
726 fprintf (file
, "n_bits = %d, set = {", bmap
->n_bits
);
728 for (pos
= 30, i
= 0; i
< bmap
->n_bits
; i
++)
729 if (TEST_BIT (bmap
, i
))
733 fprintf (file
, "\n ");
737 fprintf (file
, "%d ", i
);
738 pos
+= 2 + (i
>= 10) + (i
>= 100) + (i
>= 1000);
741 fprintf (file
, "}\n");
745 debug_sbitmap (sbitmap bmap
)
747 dump_sbitmap_file (stderr
, bmap
);
751 dump_sbitmap_vector (FILE *file
, const char *title
, const char *subtitle
,
752 sbitmap
*bmaps
, int n_maps
)
756 fprintf (file
, "%s\n", title
);
757 for (bb
= 0; bb
< n_maps
; bb
++)
759 fprintf (file
, "%s %d\n", subtitle
, bb
);
760 dump_sbitmap (file
, bmaps
[bb
]);
763 fprintf (file
, "\n");