2 Copyright (C) 1999, 2000 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
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 memcpy (dst
->elms
, src
->elms
, sizeof (SBITMAP_ELT_TYPE
) * dst
->size
);
102 /* Determine if a == b. */
107 return !memcmp (a
->elms
, b
->elms
, sizeof (SBITMAP_ELT_TYPE
) * a
->size
);
109 /* Zero all elements in a bitmap. */
115 memset ((PTR
) bmap
->elms
, 0, bmap
->bytes
);
118 /* Set all elements in a bitmap to ones. */
124 unsigned int last_bit
;
126 memset ((PTR
) bmap
->elms
, -1, bmap
->bytes
);
128 last_bit
= bmap
->n_bits
% SBITMAP_ELT_BITS
;
130 bmap
->elms
[bmap
->size
- 1]
131 = (SBITMAP_ELT_TYPE
)-1 >> (SBITMAP_ELT_BITS
- last_bit
);
134 /* Zero a vector of N_VECS bitmaps. */
137 sbitmap_vector_zero (bmap
, n_vecs
)
143 for (i
= 0; i
< n_vecs
; i
++)
144 sbitmap_zero (bmap
[i
]);
147 /* Set a vector of N_VECS bitmaps to ones. */
150 sbitmap_vector_ones (bmap
, n_vecs
)
156 for (i
= 0; i
< n_vecs
; i
++)
157 sbitmap_ones (bmap
[i
]);
160 /* Set DST to be A union (B - C).
162 Return non-zero if any change is made. */
165 sbitmap_union_of_diff (dst
, a
, b
, c
)
166 sbitmap dst
, a
, b
, c
;
169 sbitmap_ptr dstp
, ap
, bp
, cp
;
172 for (dstp
= dst
->elms
, ap
= a
->elms
, bp
= b
->elms
, cp
= c
->elms
, i
= 0;
173 i
< dst
->size
; i
++, dstp
++)
175 SBITMAP_ELT_TYPE tmp
= *ap
++ | (*bp
++ & ~*cp
++);
187 /* Set bitmap DST to the bitwise negation of the bitmap SRC. */
190 sbitmap_not (dst
, src
)
194 sbitmap_ptr dstp
, srcp
;
196 for (dstp
= dst
->elms
, srcp
= src
->elms
, i
= 0; i
< dst
->size
; i
++)
197 *dstp
++ = ~(*srcp
++);
200 /* Set the bits in DST to be the difference between the bits
201 in A and the bits in B. i.e. dst = a & (~b). */
204 sbitmap_difference (dst
, a
, b
)
208 sbitmap_ptr dstp
, ap
, bp
;
210 for (dstp
= dst
->elms
, ap
= a
->elms
, bp
= b
->elms
, i
= 0; i
< dst
->size
; i
++)
211 *dstp
++ = *ap
++ & (~*bp
++);
214 /* Set DST to be (A and B).
215 Return non-zero if any change is made. */
218 sbitmap_a_and_b (dst
, a
, b
)
222 sbitmap_ptr dstp
, ap
, bp
;
225 for (dstp
= dst
->elms
, ap
= a
->elms
, bp
= b
->elms
, i
= 0; i
< dst
->size
;
228 SBITMAP_ELT_TYPE tmp
= *ap
++ & *bp
++;
240 /* Set DST to be (A xor B)).
241 Return non-zero if any change is made. */
244 sbitmap_a_xor_b (dst
, a
, b
)
248 sbitmap_ptr dstp
, ap
, bp
;
251 for (dstp
= dst
->elms
, ap
= a
->elms
, bp
= b
->elms
, i
= 0; i
< dst
->size
;
254 SBITMAP_ELT_TYPE tmp
= *ap
++ ^ *bp
++;
265 /* Set DST to be (A or B)).
266 Return non-zero if any change is made. */
269 sbitmap_a_or_b (dst
, a
, b
)
273 sbitmap_ptr dstp
, ap
, bp
;
276 for (dstp
= dst
->elms
, ap
= a
->elms
, bp
= b
->elms
, i
= 0; i
< dst
->size
;
279 SBITMAP_ELT_TYPE tmp
= *ap
++ | *bp
++;
291 /* Return non-zero if A is a subset of B. */
294 sbitmap_a_subset_b_p (a
, b
)
300 for (ap
= a
->elms
, bp
= b
->elms
, i
= 0; i
< a
->size
; i
++, ap
++, bp
++)
301 if ((*ap
| *bp
) != *bp
)
307 /* Set DST to be (A or (B and C)).
308 Return non-zero if any change is made. */
311 sbitmap_a_or_b_and_c (dst
, a
, b
, c
)
312 sbitmap dst
, a
, b
, c
;
315 sbitmap_ptr dstp
, ap
, bp
, cp
;
318 for (dstp
= dst
->elms
, ap
= a
->elms
, bp
= b
->elms
, cp
= c
->elms
, i
= 0;
319 i
< dst
->size
; i
++, dstp
++)
321 SBITMAP_ELT_TYPE tmp
= *ap
++ | (*bp
++ & *cp
++);
333 /* Set DST to be (A and (B or C)).
334 Return non-zero if any change is made. */
337 sbitmap_a_and_b_or_c (dst
, a
, b
, c
)
338 sbitmap dst
, a
, b
, c
;
341 sbitmap_ptr dstp
, ap
, bp
, cp
;
344 for (dstp
= dst
->elms
, ap
= a
->elms
, bp
= b
->elms
, cp
= c
->elms
, i
= 0;
345 i
< dst
->size
; i
++, dstp
++)
347 SBITMAP_ELT_TYPE tmp
= *ap
++ & (*bp
++ | *cp
++);
360 /* Set the bitmap DST to the intersection of SRC of successors of
361 block number BB, using the new flow graph structures. */
364 sbitmap_intersection_of_succs (dst
, src
, bb
)
369 basic_block b
= BASIC_BLOCK (bb
);
370 unsigned int set_size
= dst
->size
;
373 for (e
= b
->succ
; e
!= 0; e
= e
->succ_next
)
375 if (e
->dest
== EXIT_BLOCK_PTR
)
378 sbitmap_copy (dst
, src
[e
->dest
->index
]);
385 for (e
= e
->succ_next
; e
!= 0; e
= e
->succ_next
)
390 if (e
->dest
== EXIT_BLOCK_PTR
)
393 p
= src
[e
->dest
->index
]->elms
;
395 for (i
= 0; i
< set_size
; i
++)
400 /* Set the bitmap DST to the intersection of SRC of predecessors of
401 block number BB, using the new flow graph structures. */
404 sbitmap_intersection_of_preds (dst
, src
, bb
)
409 basic_block b
= BASIC_BLOCK (bb
);
410 unsigned int set_size
= dst
->size
;
413 for (e
= b
->pred
; e
!= 0; e
= e
->pred_next
)
415 if (e
->src
== ENTRY_BLOCK_PTR
)
418 sbitmap_copy (dst
, src
[e
->src
->index
]);
425 for (e
= e
->pred_next
; e
!= 0; e
= e
->pred_next
)
430 if (e
->src
== ENTRY_BLOCK_PTR
)
433 p
= src
[e
->src
->index
]->elms
;
435 for (i
= 0; i
< set_size
; i
++)
440 /* Set the bitmap DST to the union of SRC of successors of
441 block number BB, using the new flow graph structures. */
444 sbitmap_union_of_succs (dst
, src
, bb
)
449 basic_block b
= BASIC_BLOCK (bb
);
450 unsigned int set_size
= dst
->size
;
453 for (e
= b
->succ
; e
!= 0; e
= e
->succ_next
)
455 if (e
->dest
== EXIT_BLOCK_PTR
)
458 sbitmap_copy (dst
, src
[e
->dest
->index
]);
465 for (e
= e
->succ_next
; e
!= 0; e
= e
->succ_next
)
470 if (e
->dest
== EXIT_BLOCK_PTR
)
473 p
= src
[e
->dest
->index
]->elms
;
475 for (i
= 0; i
< set_size
; i
++)
480 /* Set the bitmap DST to the union of SRC of predecessors of
481 block number BB, using the new flow graph structures. */
484 sbitmap_union_of_preds (dst
, src
, bb
)
489 basic_block b
= BASIC_BLOCK (bb
);
490 unsigned int set_size
= dst
->size
;
493 for (e
= b
->pred
; e
!= 0; e
= e
->pred_next
)
495 if (e
->src
== ENTRY_BLOCK_PTR
)
498 sbitmap_copy (dst
, src
[e
->src
->index
]);
505 for (e
= e
->pred_next
; e
!= 0; e
= e
->pred_next
)
510 if (e
->src
== ENTRY_BLOCK_PTR
)
513 p
= src
[e
->src
->index
]->elms
;
515 for (i
= 0; i
< set_size
; i
++)
521 /* Return number of first bit set in the bitmap, -1 if none. */
524 sbitmap_first_set_bit (bmap
)
529 EXECUTE_IF_SET_IN_SBITMAP (bmap
, 0, n
, { return n
; });
533 /* Return number of last bit set in the bitmap, -1 if none. */
536 sbitmap_last_set_bit (bmap
)
540 SBITMAP_ELT_TYPE
*ptr
= bmap
->elms
;
542 for (i
= bmap
->size
- 1; i
>= 0; i
--)
544 SBITMAP_ELT_TYPE word
= ptr
[i
];
548 unsigned int index
= (i
+ 1) * SBITMAP_ELT_BITS
- 1;
549 SBITMAP_ELT_TYPE mask
550 = (SBITMAP_ELT_TYPE
) 1 << (SBITMAP_ELT_BITS
- 1);
554 if ((word
& mask
) != 0)
567 dump_sbitmap (file
, bmap
)
571 unsigned int i
, n
, j
;
572 unsigned int set_size
= bmap
->size
;
573 unsigned int total_bits
= bmap
->n_bits
;
576 for (i
= n
= 0; i
< set_size
&& n
< total_bits
; i
++)
577 for (j
= 0; j
< SBITMAP_ELT_BITS
&& n
< total_bits
; j
++, n
++)
579 if (n
!= 0 && n
% 10 == 0)
583 (bmap
->elms
[i
] & ((SBITMAP_ELT_TYPE
) 1 << j
)) != 0);
586 fprintf (file
, "\n");
595 fprintf (stderr
, "n_bits = %d, set = {", bmap
->n_bits
);
597 for (pos
= 30, i
= 0; i
< bmap
->n_bits
; i
++)
598 if (TEST_BIT (bmap
, i
))
602 fprintf (stderr
, "\n");
606 fprintf (stderr
, "%d ", i
);
607 pos
+= 1 + (i
>= 10) + (i
>= 100);
610 fprintf (stderr
, "}\n");
614 dump_sbitmap_vector (file
, title
, subtitle
, bmaps
, n_maps
)
616 const char *title
, *subtitle
;
622 fprintf (file
, "%s\n", title
);
623 for (bb
= 0; bb
< n_maps
; bb
++)
625 fprintf (file
, "%s %d\n", subtitle
, bb
);
626 dump_sbitmap (file
, bmaps
[bb
]);
629 fprintf (file
, "\n");