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 /* Allocate a vector of N_VECS bitmaps of N_ELMS bits. */
109 sbitmap_vector_alloc (unsigned int n_vecs
, unsigned int n_elms
)
111 unsigned int i
, bytes
, offset
, elm_bytes
, size
, amt
, vector_bytes
;
112 sbitmap
*bitmap_vector
;
114 size
= SBITMAP_SET_SIZE (n_elms
);
115 bytes
= size
* sizeof (SBITMAP_ELT_TYPE
);
116 elm_bytes
= (sizeof (struct simple_bitmap_def
)
117 + bytes
- sizeof (SBITMAP_ELT_TYPE
));
118 vector_bytes
= n_vecs
* sizeof (sbitmap
*);
120 /* Round up `vector_bytes' to account for the alignment requirements
121 of an sbitmap. One could allocate the vector-table and set of sbitmaps
122 separately, but that requires maintaining two pointers or creating
123 a cover struct to hold both pointers (so our result is still just
124 one pointer). Neither is a bad idea, but this is simpler for now. */
126 /* Based on DEFAULT_ALIGNMENT computation in obstack.c. */
127 struct { char x
; SBITMAP_ELT_TYPE y
; } align
;
128 int alignment
= (char *) & align
.y
- & align
.x
;
129 vector_bytes
= (vector_bytes
+ alignment
- 1) & ~ (alignment
- 1);
132 amt
= vector_bytes
+ (n_vecs
* elm_bytes
);
133 bitmap_vector
= xmalloc (amt
);
135 for (i
= 0, offset
= vector_bytes
; i
< n_vecs
; i
++, offset
+= elm_bytes
)
137 sbitmap b
= (sbitmap
) ((char *) bitmap_vector
+ offset
);
139 bitmap_vector
[i
] = b
;
145 return bitmap_vector
;
148 /* Copy sbitmap SRC to DST. */
151 sbitmap_copy (sbitmap dst
, sbitmap src
)
153 memcpy (dst
->elms
, src
->elms
, sizeof (SBITMAP_ELT_TYPE
) * dst
->size
);
156 /* Determine if a == b. */
158 sbitmap_equal (sbitmap a
, sbitmap b
)
160 return !memcmp (a
->elms
, b
->elms
, sizeof (SBITMAP_ELT_TYPE
) * a
->size
);
163 /* Zero all elements in a bitmap. */
166 sbitmap_zero (sbitmap bmap
)
168 memset (bmap
->elms
, 0, bmap
->bytes
);
171 /* Set all elements in a bitmap to ones. */
174 sbitmap_ones (sbitmap bmap
)
176 unsigned int last_bit
;
178 memset (bmap
->elms
, -1, bmap
->bytes
);
180 last_bit
= bmap
->n_bits
% SBITMAP_ELT_BITS
;
182 bmap
->elms
[bmap
->size
- 1]
183 = (SBITMAP_ELT_TYPE
)-1 >> (SBITMAP_ELT_BITS
- last_bit
);
186 /* Zero a vector of N_VECS bitmaps. */
189 sbitmap_vector_zero (sbitmap
*bmap
, unsigned int n_vecs
)
193 for (i
= 0; i
< n_vecs
; i
++)
194 sbitmap_zero (bmap
[i
]);
197 /* Set a vector of N_VECS bitmaps to ones. */
200 sbitmap_vector_ones (sbitmap
*bmap
, unsigned int n_vecs
)
204 for (i
= 0; i
< n_vecs
; i
++)
205 sbitmap_ones (bmap
[i
]);
208 /* Set DST to be A union (B - C).
210 Returns true if any change is made. */
213 sbitmap_union_of_diff_cg (sbitmap dst
, sbitmap a
, sbitmap b
, sbitmap c
)
215 unsigned int i
, n
= dst
->size
;
216 sbitmap_ptr dstp
= dst
->elms
;
217 sbitmap_ptr ap
= a
->elms
;
218 sbitmap_ptr bp
= b
->elms
;
219 sbitmap_ptr cp
= c
->elms
;
220 SBITMAP_ELT_TYPE changed
= 0;
222 for (i
= 0; i
< n
; i
++)
224 SBITMAP_ELT_TYPE tmp
= *ap
++ | (*bp
++ & ~*cp
++);
225 changed
|= *dstp
^ tmp
;
233 sbitmap_union_of_diff (sbitmap dst
, sbitmap a
, sbitmap b
, sbitmap c
)
235 unsigned int i
, n
= dst
->size
;
236 sbitmap_ptr dstp
= dst
->elms
;
237 sbitmap_ptr ap
= a
->elms
;
238 sbitmap_ptr bp
= b
->elms
;
239 sbitmap_ptr cp
= c
->elms
;
241 for (i
= 0; i
< n
; i
++)
242 *dstp
++ = *ap
++ | (*bp
++ & ~*cp
++);
245 /* Set bitmap DST to the bitwise negation of the bitmap SRC. */
248 sbitmap_not (sbitmap dst
, sbitmap src
)
250 unsigned int i
, n
= dst
->size
;
251 sbitmap_ptr dstp
= dst
->elms
;
252 sbitmap_ptr srcp
= src
->elms
;
253 unsigned int last_bit
;
255 for (i
= 0; i
< n
; i
++)
258 /* Zero all bits past n_bits, by ANDing dst with sbitmap_ones. */
259 last_bit
= src
->n_bits
% SBITMAP_ELT_BITS
;
261 dst
->elms
[n
-1] = dst
->elms
[n
-1]
262 & ((SBITMAP_ELT_TYPE
)-1 >> (SBITMAP_ELT_BITS
- last_bit
));
265 /* Set the bits in DST to be the difference between the bits
266 in A and the bits in B. i.e. dst = a & (~b). */
269 sbitmap_difference (sbitmap dst
, sbitmap a
, sbitmap b
)
271 unsigned int i
, dst_size
= dst
->size
;
272 unsigned int min_size
= dst
->size
;
273 sbitmap_ptr dstp
= dst
->elms
;
274 sbitmap_ptr ap
= a
->elms
;
275 sbitmap_ptr bp
= b
->elms
;
277 /* A should be at least as large as DEST, to have a defined source. */
278 if (a
->size
< dst_size
)
280 /* If minuend is smaller, we simply pretend it to be zero bits, i.e.
281 only copy the subtrahend into dest. */
282 if (b
->size
< min_size
)
284 for (i
= 0; i
< min_size
; i
++)
285 *dstp
++ = *ap
++ & (~*bp
++);
286 /* Now fill the rest of dest from A, if B was too short.
287 This makes sense only when destination and A differ. */
288 if (dst
!= a
&& i
!= dst_size
)
289 for (; i
< dst_size
; i
++)
293 /* Set DST to be (A and B).
294 Return nonzero if any change is made. */
297 sbitmap_a_and_b_cg (sbitmap dst
, sbitmap a
, sbitmap b
)
299 unsigned int i
, n
= dst
->size
;
300 sbitmap_ptr dstp
= dst
->elms
;
301 sbitmap_ptr ap
= a
->elms
;
302 sbitmap_ptr bp
= b
->elms
;
303 SBITMAP_ELT_TYPE changed
= 0;
305 for (i
= 0; i
< n
; i
++)
307 SBITMAP_ELT_TYPE tmp
= *ap
++ & *bp
++;
308 changed
|= *dstp
^ tmp
;
316 sbitmap_a_and_b (sbitmap dst
, sbitmap a
, sbitmap b
)
318 unsigned int i
, n
= dst
->size
;
319 sbitmap_ptr dstp
= dst
->elms
;
320 sbitmap_ptr ap
= a
->elms
;
321 sbitmap_ptr bp
= b
->elms
;
323 for (i
= 0; i
< n
; i
++)
324 *dstp
++ = *ap
++ & *bp
++;
327 /* Set DST to be (A xor B)).
328 Return nonzero if any change is made. */
331 sbitmap_a_xor_b_cg (sbitmap dst
, sbitmap a
, sbitmap b
)
333 unsigned int i
, n
= dst
->size
;
334 sbitmap_ptr dstp
= dst
->elms
;
335 sbitmap_ptr ap
= a
->elms
;
336 sbitmap_ptr bp
= b
->elms
;
337 SBITMAP_ELT_TYPE changed
= 0;
339 for (i
= 0; i
< n
; i
++)
341 SBITMAP_ELT_TYPE tmp
= *ap
++ ^ *bp
++;
342 changed
|= *dstp
^ tmp
;
350 sbitmap_a_xor_b (sbitmap dst
, sbitmap a
, sbitmap b
)
352 unsigned int i
, n
= dst
->size
;
353 sbitmap_ptr dstp
= dst
->elms
;
354 sbitmap_ptr ap
= a
->elms
;
355 sbitmap_ptr bp
= b
->elms
;
357 for (i
= 0; i
< n
; i
++)
358 *dstp
++ = *ap
++ ^ *bp
++;
361 /* Set DST to be (A or B)).
362 Return nonzero if any change is made. */
365 sbitmap_a_or_b_cg (sbitmap dst
, sbitmap a
, sbitmap b
)
367 unsigned int i
, n
= dst
->size
;
368 sbitmap_ptr dstp
= dst
->elms
;
369 sbitmap_ptr ap
= a
->elms
;
370 sbitmap_ptr bp
= b
->elms
;
371 SBITMAP_ELT_TYPE changed
= 0;
373 for (i
= 0; i
< n
; i
++)
375 SBITMAP_ELT_TYPE tmp
= *ap
++ | *bp
++;
376 changed
|= *dstp
^ tmp
;
384 sbitmap_a_or_b (sbitmap dst
, sbitmap a
, sbitmap b
)
386 unsigned int i
, n
= dst
->size
;
387 sbitmap_ptr dstp
= dst
->elms
;
388 sbitmap_ptr ap
= a
->elms
;
389 sbitmap_ptr bp
= b
->elms
;
391 for (i
= 0; i
< n
; i
++)
392 *dstp
++ = *ap
++ | *bp
++;
395 /* Return nonzero if A is a subset of B. */
398 sbitmap_a_subset_b_p (sbitmap a
, sbitmap b
)
400 unsigned int i
, n
= a
->size
;
403 for (ap
= a
->elms
, bp
= b
->elms
, i
= 0; i
< n
; i
++, ap
++, bp
++)
404 if ((*ap
| *bp
) != *bp
)
410 /* Set DST to be (A or (B and C)).
411 Return nonzero if any change is made. */
414 sbitmap_a_or_b_and_c_cg (sbitmap dst
, sbitmap a
, sbitmap b
, sbitmap c
)
416 unsigned int i
, n
= dst
->size
;
417 sbitmap_ptr dstp
= dst
->elms
;
418 sbitmap_ptr ap
= a
->elms
;
419 sbitmap_ptr bp
= b
->elms
;
420 sbitmap_ptr cp
= c
->elms
;
421 SBITMAP_ELT_TYPE changed
= 0;
423 for (i
= 0; i
< n
; i
++)
425 SBITMAP_ELT_TYPE tmp
= *ap
++ | (*bp
++ & *cp
++);
426 changed
|= *dstp
^ tmp
;
434 sbitmap_a_or_b_and_c (sbitmap dst
, sbitmap a
, sbitmap b
, sbitmap c
)
436 unsigned int i
, n
= dst
->size
;
437 sbitmap_ptr dstp
= dst
->elms
;
438 sbitmap_ptr ap
= a
->elms
;
439 sbitmap_ptr bp
= b
->elms
;
440 sbitmap_ptr cp
= c
->elms
;
442 for (i
= 0; i
< n
; i
++)
443 *dstp
++ = *ap
++ | (*bp
++ & *cp
++);
446 /* Set DST to be (A and (B or C)).
447 Return nonzero if any change is made. */
450 sbitmap_a_and_b_or_c_cg (sbitmap dst
, sbitmap a
, sbitmap b
, sbitmap c
)
452 unsigned int i
, n
= dst
->size
;
453 sbitmap_ptr dstp
= dst
->elms
;
454 sbitmap_ptr ap
= a
->elms
;
455 sbitmap_ptr bp
= b
->elms
;
456 sbitmap_ptr cp
= c
->elms
;
457 SBITMAP_ELT_TYPE changed
= 0;
459 for (i
= 0; i
< n
; i
++)
461 SBITMAP_ELT_TYPE tmp
= *ap
++ & (*bp
++ | *cp
++);
462 changed
|= *dstp
^ tmp
;
470 sbitmap_a_and_b_or_c (sbitmap dst
, sbitmap a
, sbitmap b
, sbitmap c
)
472 unsigned int i
, n
= dst
->size
;
473 sbitmap_ptr dstp
= dst
->elms
;
474 sbitmap_ptr ap
= a
->elms
;
475 sbitmap_ptr bp
= b
->elms
;
476 sbitmap_ptr cp
= c
->elms
;
478 for (i
= 0; i
< n
; i
++)
479 *dstp
++ = *ap
++ & (*bp
++ | *cp
++);
483 /* Set the bitmap DST to the intersection of SRC of successors of
484 block number BB, using the new flow graph structures. */
487 sbitmap_intersection_of_succs (sbitmap dst
, sbitmap
*src
, int bb
)
489 basic_block b
= BASIC_BLOCK (bb
);
490 unsigned int set_size
= dst
->size
;
493 for (e
= b
->succ
; e
!= 0; e
= e
->succ_next
)
495 if (e
->dest
== EXIT_BLOCK_PTR
)
498 sbitmap_copy (dst
, src
[e
->dest
->index
]);
505 for (e
= e
->succ_next
; e
!= 0; e
= e
->succ_next
)
510 if (e
->dest
== EXIT_BLOCK_PTR
)
513 p
= src
[e
->dest
->index
]->elms
;
515 for (i
= 0; i
< set_size
; i
++)
520 /* Set the bitmap DST to the intersection of SRC of predecessors of
521 block number BB, using the new flow graph structures. */
524 sbitmap_intersection_of_preds (sbitmap dst
, sbitmap
*src
, int bb
)
526 basic_block b
= BASIC_BLOCK (bb
);
527 unsigned int set_size
= dst
->size
;
530 for (e
= b
->pred
; e
!= 0; e
= e
->pred_next
)
532 if (e
->src
== ENTRY_BLOCK_PTR
)
535 sbitmap_copy (dst
, src
[e
->src
->index
]);
542 for (e
= e
->pred_next
; e
!= 0; e
= e
->pred_next
)
547 if (e
->src
== ENTRY_BLOCK_PTR
)
550 p
= src
[e
->src
->index
]->elms
;
552 for (i
= 0; i
< set_size
; i
++)
557 /* Set the bitmap DST to the union of SRC of successors of
558 block number BB, using the new flow graph structures. */
561 sbitmap_union_of_succs (sbitmap dst
, sbitmap
*src
, int bb
)
563 basic_block b
= BASIC_BLOCK (bb
);
564 unsigned int set_size
= dst
->size
;
567 for (e
= b
->succ
; e
!= 0; e
= e
->succ_next
)
569 if (e
->dest
== EXIT_BLOCK_PTR
)
572 sbitmap_copy (dst
, src
[e
->dest
->index
]);
579 for (e
= e
->succ_next
; e
!= 0; e
= e
->succ_next
)
584 if (e
->dest
== EXIT_BLOCK_PTR
)
587 p
= src
[e
->dest
->index
]->elms
;
589 for (i
= 0; i
< set_size
; i
++)
594 /* Set the bitmap DST to the union of SRC of predecessors of
595 block number BB, using the new flow graph structures. */
598 sbitmap_union_of_preds (sbitmap dst
, sbitmap
*src
, int bb
)
600 basic_block b
= BASIC_BLOCK (bb
);
601 unsigned int set_size
= dst
->size
;
604 for (e
= b
->pred
; e
!= 0; e
= e
->pred_next
)
606 if (e
->src
== ENTRY_BLOCK_PTR
)
609 sbitmap_copy (dst
, src
[e
->src
->index
]);
616 for (e
= e
->pred_next
; e
!= 0; e
= e
->pred_next
)
621 if (e
->src
== ENTRY_BLOCK_PTR
)
624 p
= src
[e
->src
->index
]->elms
;
626 for (i
= 0; i
< set_size
; i
++)
632 /* Return number of first bit set in the bitmap, -1 if none. */
635 sbitmap_first_set_bit (sbitmap bmap
)
639 EXECUTE_IF_SET_IN_SBITMAP (bmap
, 0, n
, { return n
; });
643 /* Return number of last bit set in the bitmap, -1 if none. */
646 sbitmap_last_set_bit (sbitmap bmap
)
649 SBITMAP_ELT_TYPE
*ptr
= bmap
->elms
;
651 for (i
= bmap
->size
- 1; i
>= 0; i
--)
653 SBITMAP_ELT_TYPE word
= ptr
[i
];
657 unsigned int index
= (i
+ 1) * SBITMAP_ELT_BITS
- 1;
658 SBITMAP_ELT_TYPE mask
659 = (SBITMAP_ELT_TYPE
) 1 << (SBITMAP_ELT_BITS
- 1);
663 if ((word
& mask
) != 0)
676 dump_sbitmap (FILE *file
, sbitmap bmap
)
678 unsigned int i
, n
, j
;
679 unsigned int set_size
= bmap
->size
;
680 unsigned int total_bits
= bmap
->n_bits
;
683 for (i
= n
= 0; i
< set_size
&& n
< total_bits
; i
++)
684 for (j
= 0; j
< SBITMAP_ELT_BITS
&& n
< total_bits
; j
++, n
++)
686 if (n
!= 0 && n
% 10 == 0)
690 (bmap
->elms
[i
] & ((SBITMAP_ELT_TYPE
) 1 << j
)) != 0);
693 fprintf (file
, "\n");
697 dump_sbitmap_file (FILE *file
, sbitmap bmap
)
701 fprintf (file
, "n_bits = %d, set = {", bmap
->n_bits
);
703 for (pos
= 30, i
= 0; i
< bmap
->n_bits
; i
++)
704 if (TEST_BIT (bmap
, i
))
708 fprintf (file
, "\n ");
712 fprintf (file
, "%d ", i
);
713 pos
+= 2 + (i
>= 10) + (i
>= 100) + (i
>= 1000);
716 fprintf (file
, "}\n");
720 debug_sbitmap (sbitmap bmap
)
722 dump_sbitmap_file (stderr
, bmap
);
726 dump_sbitmap_vector (FILE *file
, const char *title
, const char *subtitle
,
727 sbitmap
*bmaps
, int n_maps
)
731 fprintf (file
, "%s\n", title
);
732 for (bb
= 0; bb
< n_maps
; bb
++)
734 fprintf (file
, "%s %d\n", subtitle
, bb
);
735 dump_sbitmap (file
, bmaps
[bb
]);
738 fprintf (file
, "\n");