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
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
);
110 /* Zero all elements in a bitmap. */
116 memset ((PTR
) bmap
->elms
, 0, bmap
->bytes
);
119 /* Set all elements in a bitmap to ones. */
125 unsigned int last_bit
;
127 memset ((PTR
) bmap
->elms
, -1, bmap
->bytes
);
129 last_bit
= bmap
->n_bits
% SBITMAP_ELT_BITS
;
131 bmap
->elms
[bmap
->size
- 1]
132 = (SBITMAP_ELT_TYPE
)-1 >> (SBITMAP_ELT_BITS
- last_bit
);
135 /* Zero a vector of N_VECS bitmaps. */
138 sbitmap_vector_zero (bmap
, n_vecs
)
144 for (i
= 0; i
< n_vecs
; i
++)
145 sbitmap_zero (bmap
[i
]);
148 /* Set a vector of N_VECS bitmaps to ones. */
151 sbitmap_vector_ones (bmap
, n_vecs
)
157 for (i
= 0; i
< n_vecs
; i
++)
158 sbitmap_ones (bmap
[i
]);
161 /* Set DST to be A union (B - C).
163 Returns true if any change is made. */
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
;
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. */
203 sbitmap_not (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
++)
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). */
218 sbitmap_difference (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
)
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
)
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
++)
243 /* Set DST to be (A and B).
244 Return non-zero if any change is made. */
247 sbitmap_a_and_b_cg (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
;
267 sbitmap_a_and_b (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. */
283 sbitmap_a_xor_b_cg (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
;
303 sbitmap_a_xor_b (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. */
319 sbitmap_a_or_b_cg (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
;
339 sbitmap_a_or_b (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. */
354 sbitmap_a_subset_b_p (a
, b
)
357 unsigned int i
, n
= a
->size
;
360 for (ap
= a
->elms
, bp
= b
->elms
, i
= 0; i
< n
; i
++, ap
++, bp
++)
361 if ((*ap
| *bp
) != *bp
)
367 /* Set DST to be (A or (B and C)).
368 Return non-zero if any change is made. */
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
;
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. */
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
;
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
++);
444 /* Set the bitmap DST to the intersection of SRC of successors of
445 block number BB, using the new flow graph structures. */
448 sbitmap_intersection_of_succs (dst
, src
, bb
)
453 basic_block b
= BASIC_BLOCK (bb
);
454 unsigned int set_size
= dst
->size
;
457 for (e
= b
->succ
; e
!= 0; e
= e
->succ_next
)
459 if (e
->dest
== EXIT_BLOCK_PTR
)
462 sbitmap_copy (dst
, src
[e
->dest
->index
]);
469 for (e
= e
->succ_next
; e
!= 0; e
= e
->succ_next
)
474 if (e
->dest
== EXIT_BLOCK_PTR
)
477 p
= src
[e
->dest
->index
]->elms
;
479 for (i
= 0; i
< set_size
; i
++)
484 /* Set the bitmap DST to the intersection of SRC of predecessors of
485 block number BB, using the new flow graph structures. */
488 sbitmap_intersection_of_preds (dst
, src
, bb
)
493 basic_block b
= BASIC_BLOCK (bb
);
494 unsigned int set_size
= dst
->size
;
497 for (e
= b
->pred
; e
!= 0; e
= e
->pred_next
)
499 if (e
->src
== ENTRY_BLOCK_PTR
)
502 sbitmap_copy (dst
, src
[e
->src
->index
]);
509 for (e
= e
->pred_next
; e
!= 0; e
= e
->pred_next
)
514 if (e
->src
== ENTRY_BLOCK_PTR
)
517 p
= src
[e
->src
->index
]->elms
;
519 for (i
= 0; i
< set_size
; i
++)
524 /* Set the bitmap DST to the union of SRC of successors of
525 block number BB, using the new flow graph structures. */
528 sbitmap_union_of_succs (dst
, src
, bb
)
533 basic_block b
= BASIC_BLOCK (bb
);
534 unsigned int set_size
= dst
->size
;
537 for (e
= b
->succ
; e
!= 0; e
= e
->succ_next
)
539 if (e
->dest
== EXIT_BLOCK_PTR
)
542 sbitmap_copy (dst
, src
[e
->dest
->index
]);
549 for (e
= e
->succ_next
; e
!= 0; e
= e
->succ_next
)
554 if (e
->dest
== EXIT_BLOCK_PTR
)
557 p
= src
[e
->dest
->index
]->elms
;
559 for (i
= 0; i
< set_size
; i
++)
564 /* Set the bitmap DST to the union of SRC of predecessors of
565 block number BB, using the new flow graph structures. */
568 sbitmap_union_of_preds (dst
, src
, bb
)
573 basic_block b
= BASIC_BLOCK (bb
);
574 unsigned int set_size
= dst
->size
;
577 for (e
= b
->pred
; e
!= 0; e
= e
->pred_next
)
579 if (e
->src
== ENTRY_BLOCK_PTR
)
582 sbitmap_copy (dst
, src
[e
->src
->index
]);
589 for (e
= e
->pred_next
; e
!= 0; e
= e
->pred_next
)
594 if (e
->src
== ENTRY_BLOCK_PTR
)
597 p
= src
[e
->src
->index
]->elms
;
599 for (i
= 0; i
< set_size
; i
++)
605 /* Return number of first bit set in the bitmap, -1 if none. */
608 sbitmap_first_set_bit (bmap
)
613 EXECUTE_IF_SET_IN_SBITMAP (bmap
, 0, n
, { return n
; });
617 /* Return number of last bit set in the bitmap, -1 if none. */
620 sbitmap_last_set_bit (bmap
)
624 SBITMAP_ELT_TYPE
*ptr
= bmap
->elms
;
626 for (i
= bmap
->size
- 1; i
>= 0; i
--)
628 SBITMAP_ELT_TYPE word
= ptr
[i
];
632 unsigned int index
= (i
+ 1) * SBITMAP_ELT_BITS
- 1;
633 SBITMAP_ELT_TYPE mask
634 = (SBITMAP_ELT_TYPE
) 1 << (SBITMAP_ELT_BITS
- 1);
638 if ((word
& mask
) != 0)
651 dump_sbitmap (file
, bmap
)
655 unsigned int i
, n
, j
;
656 unsigned int set_size
= bmap
->size
;
657 unsigned int total_bits
= bmap
->n_bits
;
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)
667 (bmap
->elms
[i
] & ((SBITMAP_ELT_TYPE
) 1 << j
)) != 0);
670 fprintf (file
, "\n");
674 dump_sbitmap_file (file
, bmap
)
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
))
687 fprintf (file
, "\n ");
691 fprintf (file
, "%d ", i
);
692 pos
+= 2 + (i
>= 10) + (i
>= 100) + (i
>= 1000);
695 fprintf (file
, "}\n");
702 dump_sbitmap_file (stderr
, bmap
);
706 dump_sbitmap_vector (file
, title
, subtitle
, bmaps
, n_maps
)
708 const char *title
, *subtitle
;
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");