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
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 (n_elms
)
38 unsigned int bytes
, size
, amt
;
41 size
= SBITMAP_SET_SIZE (n_elms
);
42 bytes
= size
* sizeof (SBITMAP_ELT_TYPE
);
43 amt
= (sizeof (struct simple_bitmap_def
)
44 + bytes
- sizeof (SBITMAP_ELT_TYPE
));
45 bmap
= (sbitmap
) xmalloc (amt
);
46 bmap
->n_bits
= n_elms
;
52 /* Allocate a vector of N_VECS bitmaps of N_ELMS bits. */
55 sbitmap_vector_alloc (n_vecs
, n_elms
)
56 unsigned int n_vecs
, n_elms
;
58 unsigned int i
, bytes
, offset
, elm_bytes
, size
, amt
, vector_bytes
;
59 sbitmap
*bitmap_vector
;
61 size
= SBITMAP_SET_SIZE (n_elms
);
62 bytes
= size
* sizeof (SBITMAP_ELT_TYPE
);
63 elm_bytes
= (sizeof (struct simple_bitmap_def
)
64 + bytes
- sizeof (SBITMAP_ELT_TYPE
));
65 vector_bytes
= n_vecs
* sizeof (sbitmap
*);
67 /* Round up `vector_bytes' to account for the alignment requirements
68 of an sbitmap. One could allocate the vector-table and set of sbitmaps
69 separately, but that requires maintaining two pointers or creating
70 a cover struct to hold both pointers (so our result is still just
71 one pointer). Neither is a bad idea, but this is simpler for now. */
73 /* Based on DEFAULT_ALIGNMENT computation in obstack.c. */
74 struct { char x
; SBITMAP_ELT_TYPE y
; } align
;
75 int alignment
= (char *) & align
.y
- & align
.x
;
76 vector_bytes
= (vector_bytes
+ alignment
- 1) & ~ (alignment
- 1);
79 amt
= vector_bytes
+ (n_vecs
* elm_bytes
);
80 bitmap_vector
= (sbitmap
*) xmalloc (amt
);
82 for (i
= 0, offset
= vector_bytes
; i
< n_vecs
; i
++, offset
+= elm_bytes
)
84 sbitmap b
= (sbitmap
) ((char *) bitmap_vector
+ offset
);
95 /* Copy sbitmap SRC to DST. */
98 sbitmap_copy (dst
, src
)
101 memcpy (dst
->elms
, src
->elms
, sizeof (SBITMAP_ELT_TYPE
) * dst
->size
);
104 /* Determine if a == b. */
109 return !memcmp (a
->elms
, b
->elms
, sizeof (SBITMAP_ELT_TYPE
) * a
->size
);
112 /* Zero all elements in a bitmap. */
118 memset ((PTR
) bmap
->elms
, 0, bmap
->bytes
);
121 /* Set all elements in a bitmap to ones. */
127 unsigned int last_bit
;
129 memset ((PTR
) bmap
->elms
, -1, bmap
->bytes
);
131 last_bit
= bmap
->n_bits
% SBITMAP_ELT_BITS
;
133 bmap
->elms
[bmap
->size
- 1]
134 = (SBITMAP_ELT_TYPE
)-1 >> (SBITMAP_ELT_BITS
- last_bit
);
137 /* Zero a vector of N_VECS bitmaps. */
140 sbitmap_vector_zero (bmap
, n_vecs
)
146 for (i
= 0; i
< n_vecs
; i
++)
147 sbitmap_zero (bmap
[i
]);
150 /* Set a vector of N_VECS bitmaps to ones. */
153 sbitmap_vector_ones (bmap
, n_vecs
)
159 for (i
= 0; i
< n_vecs
; i
++)
160 sbitmap_ones (bmap
[i
]);
163 /* Set DST to be A union (B - C).
165 Returns true if any change is made. */
168 sbitmap_union_of_diff_cg (dst
, a
, b
, c
)
169 sbitmap dst
, a
, b
, c
;
171 unsigned int i
, n
= dst
->size
;
172 sbitmap_ptr dstp
= dst
->elms
;
173 sbitmap_ptr ap
= a
->elms
;
174 sbitmap_ptr bp
= b
->elms
;
175 sbitmap_ptr cp
= c
->elms
;
176 SBITMAP_ELT_TYPE changed
= 0;
178 for (i
= 0; i
< n
; i
++)
180 SBITMAP_ELT_TYPE tmp
= *ap
++ | (*bp
++ & ~*cp
++);
181 changed
|= *dstp
^ tmp
;
189 sbitmap_union_of_diff (dst
, a
, b
, c
)
190 sbitmap dst
, a
, b
, c
;
192 unsigned int i
, n
= dst
->size
;
193 sbitmap_ptr dstp
= dst
->elms
;
194 sbitmap_ptr ap
= a
->elms
;
195 sbitmap_ptr bp
= b
->elms
;
196 sbitmap_ptr cp
= c
->elms
;
198 for (i
= 0; i
< n
; i
++)
199 *dstp
++ = *ap
++ | (*bp
++ & ~*cp
++);
202 /* Set bitmap DST to the bitwise negation of the bitmap SRC. */
205 sbitmap_not (dst
, src
)
208 unsigned int i
, n
= dst
->size
;
209 sbitmap_ptr dstp
= dst
->elms
;
210 sbitmap_ptr srcp
= src
->elms
;
212 for (i
= 0; i
< n
; i
++)
216 /* Set the bits in DST to be the difference between the bits
217 in A and the bits in B. i.e. dst = a & (~b). */
220 sbitmap_difference (dst
, a
, b
)
223 unsigned int i
, dst_size
= dst
->size
;
224 unsigned int min_size
= dst
->size
;
225 sbitmap_ptr dstp
= dst
->elms
;
226 sbitmap_ptr ap
= a
->elms
;
227 sbitmap_ptr bp
= b
->elms
;
229 /* A should be at least as large as DEST, to have a defined source. */
230 if (a
->size
< dst_size
)
232 /* If minuend is smaller, we simply pretend it to be zero bits, i.e.
233 only copy the subtrahend into dest. */
234 if (b
->size
< min_size
)
236 for (i
= 0; i
< min_size
; i
++)
237 *dstp
++ = *ap
++ & (~*bp
++);
238 /* Now fill the rest of dest from A, if B was too short.
239 This makes sense only when destination and A differ. */
240 if (dst
!= a
&& i
!= dst_size
)
241 for (; i
< dst_size
; i
++)
245 /* Set DST to be (A and B).
246 Return nonzero if any change is made. */
249 sbitmap_a_and_b_cg (dst
, a
, b
)
252 unsigned int i
, n
= dst
->size
;
253 sbitmap_ptr dstp
= dst
->elms
;
254 sbitmap_ptr ap
= a
->elms
;
255 sbitmap_ptr bp
= b
->elms
;
256 SBITMAP_ELT_TYPE changed
= 0;
258 for (i
= 0; i
< n
; i
++)
260 SBITMAP_ELT_TYPE tmp
= *ap
++ & *bp
++;
261 changed
= *dstp
^ tmp
;
269 sbitmap_a_and_b (dst
, a
, b
)
272 unsigned int i
, n
= dst
->size
;
273 sbitmap_ptr dstp
= dst
->elms
;
274 sbitmap_ptr ap
= a
->elms
;
275 sbitmap_ptr bp
= b
->elms
;
277 for (i
= 0; i
< n
; i
++)
278 *dstp
++ = *ap
++ & *bp
++;
281 /* Set DST to be (A xor B)).
282 Return nonzero if any change is made. */
285 sbitmap_a_xor_b_cg (dst
, a
, b
)
288 unsigned int i
, n
= dst
->size
;
289 sbitmap_ptr dstp
= dst
->elms
;
290 sbitmap_ptr ap
= a
->elms
;
291 sbitmap_ptr bp
= b
->elms
;
292 SBITMAP_ELT_TYPE changed
= 0;
294 for (i
= 0; i
< n
; i
++)
296 SBITMAP_ELT_TYPE tmp
= *ap
++ ^ *bp
++;
297 changed
= *dstp
^ tmp
;
305 sbitmap_a_xor_b (dst
, a
, b
)
308 unsigned int i
, n
= dst
->size
;
309 sbitmap_ptr dstp
= dst
->elms
;
310 sbitmap_ptr ap
= a
->elms
;
311 sbitmap_ptr bp
= b
->elms
;
313 for (i
= 0; i
< n
; i
++)
314 *dstp
++ = *ap
++ ^ *bp
++;
317 /* Set DST to be (A or B)).
318 Return nonzero if any change is made. */
321 sbitmap_a_or_b_cg (dst
, a
, 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_or_b (dst
, a
, b
)
344 unsigned int i
, n
= dst
->size
;
345 sbitmap_ptr dstp
= dst
->elms
;
346 sbitmap_ptr ap
= a
->elms
;
347 sbitmap_ptr bp
= b
->elms
;
349 for (i
= 0; i
< n
; i
++)
350 *dstp
++ = *ap
++ | *bp
++;
353 /* Return nonzero if A is a subset of B. */
356 sbitmap_a_subset_b_p (a
, b
)
359 unsigned int i
, n
= a
->size
;
362 for (ap
= a
->elms
, bp
= b
->elms
, i
= 0; i
< n
; i
++, ap
++, bp
++)
363 if ((*ap
| *bp
) != *bp
)
369 /* Set DST to be (A or (B and C)).
370 Return nonzero if any change is made. */
373 sbitmap_a_or_b_and_c_cg (dst
, a
, b
, c
)
374 sbitmap dst
, a
, b
, c
;
376 unsigned int i
, n
= dst
->size
;
377 sbitmap_ptr dstp
= dst
->elms
;
378 sbitmap_ptr ap
= a
->elms
;
379 sbitmap_ptr bp
= b
->elms
;
380 sbitmap_ptr cp
= c
->elms
;
381 SBITMAP_ELT_TYPE changed
= 0;
383 for (i
= 0; i
< n
; i
++)
385 SBITMAP_ELT_TYPE tmp
= *ap
++ | (*bp
++ & *cp
++);
386 changed
|= *dstp
^ tmp
;
394 sbitmap_a_or_b_and_c (dst
, a
, b
, c
)
395 sbitmap dst
, a
, b
, c
;
397 unsigned int i
, n
= dst
->size
;
398 sbitmap_ptr dstp
= dst
->elms
;
399 sbitmap_ptr ap
= a
->elms
;
400 sbitmap_ptr bp
= b
->elms
;
401 sbitmap_ptr cp
= c
->elms
;
403 for (i
= 0; i
< n
; i
++)
404 *dstp
++ = *ap
++ | (*bp
++ & *cp
++);
407 /* Set DST to be (A and (B or C)).
408 Return nonzero if any change is made. */
411 sbitmap_a_and_b_or_c_cg (dst
, a
, b
, c
)
412 sbitmap dst
, a
, b
, c
;
414 unsigned int i
, n
= dst
->size
;
415 sbitmap_ptr dstp
= dst
->elms
;
416 sbitmap_ptr ap
= a
->elms
;
417 sbitmap_ptr bp
= b
->elms
;
418 sbitmap_ptr cp
= c
->elms
;
419 SBITMAP_ELT_TYPE changed
= 0;
421 for (i
= 0; i
< n
; i
++)
423 SBITMAP_ELT_TYPE tmp
= *ap
++ & (*bp
++ | *cp
++);
424 changed
|= *dstp
^ tmp
;
432 sbitmap_a_and_b_or_c (dst
, a
, b
, c
)
433 sbitmap dst
, a
, b
, c
;
435 unsigned int i
, n
= dst
->size
;
436 sbitmap_ptr dstp
= dst
->elms
;
437 sbitmap_ptr ap
= a
->elms
;
438 sbitmap_ptr bp
= b
->elms
;
439 sbitmap_ptr cp
= c
->elms
;
441 for (i
= 0; i
< n
; i
++)
442 *dstp
++ = *ap
++ & (*bp
++ | *cp
++);
446 /* Set the bitmap DST to the intersection of SRC of successors of
447 block number BB, using the new flow graph structures. */
450 sbitmap_intersection_of_succs (dst
, src
, bb
)
455 basic_block b
= BASIC_BLOCK (bb
);
456 unsigned int set_size
= dst
->size
;
459 for (e
= b
->succ
; e
!= 0; e
= e
->succ_next
)
461 if (e
->dest
== EXIT_BLOCK_PTR
)
464 sbitmap_copy (dst
, src
[e
->dest
->index
]);
471 for (e
= e
->succ_next
; e
!= 0; e
= e
->succ_next
)
476 if (e
->dest
== EXIT_BLOCK_PTR
)
479 p
= src
[e
->dest
->index
]->elms
;
481 for (i
= 0; i
< set_size
; i
++)
486 /* Set the bitmap DST to the intersection of SRC of predecessors of
487 block number BB, using the new flow graph structures. */
490 sbitmap_intersection_of_preds (dst
, src
, bb
)
495 basic_block b
= BASIC_BLOCK (bb
);
496 unsigned int set_size
= dst
->size
;
499 for (e
= b
->pred
; e
!= 0; e
= e
->pred_next
)
501 if (e
->src
== ENTRY_BLOCK_PTR
)
504 sbitmap_copy (dst
, src
[e
->src
->index
]);
511 for (e
= e
->pred_next
; e
!= 0; e
= e
->pred_next
)
516 if (e
->src
== ENTRY_BLOCK_PTR
)
519 p
= src
[e
->src
->index
]->elms
;
521 for (i
= 0; i
< set_size
; i
++)
526 /* Set the bitmap DST to the union of SRC of successors of
527 block number BB, using the new flow graph structures. */
530 sbitmap_union_of_succs (dst
, src
, bb
)
535 basic_block b
= BASIC_BLOCK (bb
);
536 unsigned int set_size
= dst
->size
;
539 for (e
= b
->succ
; e
!= 0; e
= e
->succ_next
)
541 if (e
->dest
== EXIT_BLOCK_PTR
)
544 sbitmap_copy (dst
, src
[e
->dest
->index
]);
551 for (e
= e
->succ_next
; e
!= 0; e
= e
->succ_next
)
556 if (e
->dest
== EXIT_BLOCK_PTR
)
559 p
= src
[e
->dest
->index
]->elms
;
561 for (i
= 0; i
< set_size
; i
++)
566 /* Set the bitmap DST to the union of SRC of predecessors of
567 block number BB, using the new flow graph structures. */
570 sbitmap_union_of_preds (dst
, src
, bb
)
575 basic_block b
= BASIC_BLOCK (bb
);
576 unsigned int set_size
= dst
->size
;
579 for (e
= b
->pred
; e
!= 0; e
= e
->pred_next
)
581 if (e
->src
== ENTRY_BLOCK_PTR
)
584 sbitmap_copy (dst
, src
[e
->src
->index
]);
591 for (e
= e
->pred_next
; e
!= 0; e
= e
->pred_next
)
596 if (e
->src
== ENTRY_BLOCK_PTR
)
599 p
= src
[e
->src
->index
]->elms
;
601 for (i
= 0; i
< set_size
; i
++)
607 /* Return number of first bit set in the bitmap, -1 if none. */
610 sbitmap_first_set_bit (bmap
)
615 EXECUTE_IF_SET_IN_SBITMAP (bmap
, 0, n
, { return n
; });
619 /* Return number of last bit set in the bitmap, -1 if none. */
622 sbitmap_last_set_bit (bmap
)
626 SBITMAP_ELT_TYPE
*ptr
= bmap
->elms
;
628 for (i
= bmap
->size
- 1; i
>= 0; i
--)
630 SBITMAP_ELT_TYPE word
= ptr
[i
];
634 unsigned int index
= (i
+ 1) * SBITMAP_ELT_BITS
- 1;
635 SBITMAP_ELT_TYPE mask
636 = (SBITMAP_ELT_TYPE
) 1 << (SBITMAP_ELT_BITS
- 1);
640 if ((word
& mask
) != 0)
653 dump_sbitmap (file
, bmap
)
657 unsigned int i
, n
, j
;
658 unsigned int set_size
= bmap
->size
;
659 unsigned int total_bits
= bmap
->n_bits
;
662 for (i
= n
= 0; i
< set_size
&& n
< total_bits
; i
++)
663 for (j
= 0; j
< SBITMAP_ELT_BITS
&& n
< total_bits
; j
++, n
++)
665 if (n
!= 0 && n
% 10 == 0)
669 (bmap
->elms
[i
] & ((SBITMAP_ELT_TYPE
) 1 << j
)) != 0);
672 fprintf (file
, "\n");
676 dump_sbitmap_file (file
, bmap
)
682 fprintf (file
, "n_bits = %d, set = {", bmap
->n_bits
);
684 for (pos
= 30, i
= 0; i
< bmap
->n_bits
; i
++)
685 if (TEST_BIT (bmap
, i
))
689 fprintf (file
, "\n ");
693 fprintf (file
, "%d ", i
);
694 pos
+= 2 + (i
>= 10) + (i
>= 100) + (i
>= 1000);
697 fprintf (file
, "}\n");
704 dump_sbitmap_file (stderr
, bmap
);
708 dump_sbitmap_vector (file
, title
, subtitle
, bmaps
, n_maps
)
710 const char *title
, *subtitle
;
716 fprintf (file
, "%s\n", title
);
717 for (bb
= 0; bb
< n_maps
; bb
++)
719 fprintf (file
, "%s %d\n", subtitle
, bb
);
720 dump_sbitmap (file
, bmaps
[bb
]);
723 fprintf (file
, "\n");