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
);
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
, n
= dst
->size
;
222 sbitmap_ptr dstp
= dst
->elms
;
223 sbitmap_ptr ap
= a
->elms
;
224 sbitmap_ptr bp
= b
->elms
;
226 for (i
= 0; i
< n
; i
++)
227 *dstp
++ = *ap
++ & ~*bp
++;
230 /* Set DST to be (A and B).
231 Return non-zero if any change is made. */
234 sbitmap_a_and_b_cg (dst
, a
, b
)
237 unsigned int i
, n
= dst
->size
;
238 sbitmap_ptr dstp
= dst
->elms
;
239 sbitmap_ptr ap
= a
->elms
;
240 sbitmap_ptr bp
= b
->elms
;
241 SBITMAP_ELT_TYPE changed
= 0;
243 for (i
= 0; i
< n
; i
++)
245 SBITMAP_ELT_TYPE tmp
= *ap
++ & *bp
++;
246 changed
= *dstp
^ tmp
;
254 sbitmap_a_and_b (dst
, a
, b
)
257 unsigned int i
, n
= dst
->size
;
258 sbitmap_ptr dstp
= dst
->elms
;
259 sbitmap_ptr ap
= a
->elms
;
260 sbitmap_ptr bp
= b
->elms
;
262 for (i
= 0; i
< n
; i
++)
263 *dstp
++ = *ap
++ & *bp
++;
266 /* Set DST to be (A xor B)).
267 Return non-zero if any change is made. */
270 sbitmap_a_xor_b_cg (dst
, a
, b
)
273 unsigned int i
, n
= dst
->size
;
274 sbitmap_ptr dstp
= dst
->elms
;
275 sbitmap_ptr ap
= a
->elms
;
276 sbitmap_ptr bp
= b
->elms
;
277 SBITMAP_ELT_TYPE changed
= 0;
279 for (i
= 0; i
< n
; i
++)
281 SBITMAP_ELT_TYPE tmp
= *ap
++ ^ *bp
++;
282 changed
= *dstp
^ tmp
;
290 sbitmap_a_xor_b (dst
, a
, b
)
293 unsigned int i
, n
= dst
->size
;
294 sbitmap_ptr dstp
= dst
->elms
;
295 sbitmap_ptr ap
= a
->elms
;
296 sbitmap_ptr bp
= b
->elms
;
298 for (i
= 0; i
< n
; i
++)
299 *dstp
++ = *ap
++ ^ *bp
++;
302 /* Set DST to be (A or B)).
303 Return non-zero if any change is made. */
306 sbitmap_a_or_b_cg (dst
, a
, b
)
309 unsigned int i
, n
= dst
->size
;
310 sbitmap_ptr dstp
= dst
->elms
;
311 sbitmap_ptr ap
= a
->elms
;
312 sbitmap_ptr bp
= b
->elms
;
313 SBITMAP_ELT_TYPE changed
= 0;
315 for (i
= 0; i
< n
; i
++)
317 SBITMAP_ELT_TYPE tmp
= *ap
++ | *bp
++;
318 changed
= *dstp
^ tmp
;
326 sbitmap_a_or_b (dst
, a
, b
)
329 unsigned int i
, n
= dst
->size
;
330 sbitmap_ptr dstp
= dst
->elms
;
331 sbitmap_ptr ap
= a
->elms
;
332 sbitmap_ptr bp
= b
->elms
;
334 for (i
= 0; i
< n
; i
++)
335 *dstp
++ = *ap
++ | *bp
++;
338 /* Return non-zero if A is a subset of B. */
341 sbitmap_a_subset_b_p (a
, b
)
344 unsigned int i
, n
= a
->size
;
347 for (ap
= a
->elms
, bp
= b
->elms
, i
= 0; i
< n
; i
++, ap
++, bp
++)
348 if ((*ap
| *bp
) != *bp
)
354 /* Set DST to be (A or (B and C)).
355 Return non-zero if any change is made. */
358 sbitmap_a_or_b_and_c_cg (dst
, a
, b
, c
)
359 sbitmap dst
, a
, b
, c
;
361 unsigned int i
, n
= dst
->size
;
362 sbitmap_ptr dstp
= dst
->elms
;
363 sbitmap_ptr ap
= a
->elms
;
364 sbitmap_ptr bp
= b
->elms
;
365 sbitmap_ptr cp
= c
->elms
;
366 SBITMAP_ELT_TYPE changed
= 0;
368 for (i
= 0; i
< n
; i
++)
370 SBITMAP_ELT_TYPE tmp
= *ap
++ | (*bp
++ & *cp
++);
371 changed
|= *dstp
^ tmp
;
379 sbitmap_a_or_b_and_c (dst
, a
, b
, c
)
380 sbitmap dst
, a
, b
, c
;
382 unsigned int i
, n
= dst
->size
;
383 sbitmap_ptr dstp
= dst
->elms
;
384 sbitmap_ptr ap
= a
->elms
;
385 sbitmap_ptr bp
= b
->elms
;
386 sbitmap_ptr cp
= c
->elms
;
388 for (i
= 0; i
< n
; i
++)
389 *dstp
++ = *ap
++ | (*bp
++ & *cp
++);
392 /* Set DST to be (A and (B or C)).
393 Return non-zero if any change is made. */
396 sbitmap_a_and_b_or_c_cg (dst
, a
, b
, c
)
397 sbitmap dst
, a
, b
, c
;
399 unsigned int i
, n
= dst
->size
;
400 sbitmap_ptr dstp
= dst
->elms
;
401 sbitmap_ptr ap
= a
->elms
;
402 sbitmap_ptr bp
= b
->elms
;
403 sbitmap_ptr cp
= c
->elms
;
404 SBITMAP_ELT_TYPE changed
= 0;
406 for (i
= 0; i
< n
; i
++)
408 SBITMAP_ELT_TYPE tmp
= *ap
++ & (*bp
++ | *cp
++);
409 changed
|= *dstp
^ tmp
;
417 sbitmap_a_and_b_or_c (dst
, a
, b
, c
)
418 sbitmap dst
, a
, b
, c
;
420 unsigned int i
, n
= dst
->size
;
421 sbitmap_ptr dstp
= dst
->elms
;
422 sbitmap_ptr ap
= a
->elms
;
423 sbitmap_ptr bp
= b
->elms
;
424 sbitmap_ptr cp
= c
->elms
;
426 for (i
= 0; i
< n
; i
++)
427 *dstp
++ = *ap
++ & (*bp
++ | *cp
++);
431 /* Set the bitmap DST to the intersection of SRC of successors of
432 block number BB, using the new flow graph structures. */
435 sbitmap_intersection_of_succs (dst
, src
, bb
)
440 basic_block b
= BASIC_BLOCK (bb
);
441 unsigned int set_size
= dst
->size
;
444 for (e
= b
->succ
; e
!= 0; e
= e
->succ_next
)
446 if (e
->dest
== EXIT_BLOCK_PTR
)
449 sbitmap_copy (dst
, src
[e
->dest
->index
]);
456 for (e
= e
->succ_next
; e
!= 0; e
= e
->succ_next
)
461 if (e
->dest
== EXIT_BLOCK_PTR
)
464 p
= src
[e
->dest
->index
]->elms
;
466 for (i
= 0; i
< set_size
; i
++)
471 /* Set the bitmap DST to the intersection of SRC of predecessors of
472 block number BB, using the new flow graph structures. */
475 sbitmap_intersection_of_preds (dst
, src
, bb
)
480 basic_block b
= BASIC_BLOCK (bb
);
481 unsigned int set_size
= dst
->size
;
484 for (e
= b
->pred
; e
!= 0; e
= e
->pred_next
)
486 if (e
->src
== ENTRY_BLOCK_PTR
)
489 sbitmap_copy (dst
, src
[e
->src
->index
]);
496 for (e
= e
->pred_next
; e
!= 0; e
= e
->pred_next
)
501 if (e
->src
== ENTRY_BLOCK_PTR
)
504 p
= src
[e
->src
->index
]->elms
;
506 for (i
= 0; i
< set_size
; i
++)
511 /* Set the bitmap DST to the union of SRC of successors of
512 block number BB, using the new flow graph structures. */
515 sbitmap_union_of_succs (dst
, src
, bb
)
520 basic_block b
= BASIC_BLOCK (bb
);
521 unsigned int set_size
= dst
->size
;
524 for (e
= b
->succ
; e
!= 0; e
= e
->succ_next
)
526 if (e
->dest
== EXIT_BLOCK_PTR
)
529 sbitmap_copy (dst
, src
[e
->dest
->index
]);
536 for (e
= e
->succ_next
; e
!= 0; e
= e
->succ_next
)
541 if (e
->dest
== EXIT_BLOCK_PTR
)
544 p
= src
[e
->dest
->index
]->elms
;
546 for (i
= 0; i
< set_size
; i
++)
551 /* Set the bitmap DST to the union of SRC of predecessors of
552 block number BB, using the new flow graph structures. */
555 sbitmap_union_of_preds (dst
, src
, bb
)
560 basic_block b
= BASIC_BLOCK (bb
);
561 unsigned int set_size
= dst
->size
;
564 for (e
= b
->pred
; e
!= 0; e
= e
->pred_next
)
566 if (e
->src
== ENTRY_BLOCK_PTR
)
569 sbitmap_copy (dst
, src
[e
->src
->index
]);
576 for (e
= e
->pred_next
; e
!= 0; e
= e
->pred_next
)
581 if (e
->src
== ENTRY_BLOCK_PTR
)
584 p
= src
[e
->src
->index
]->elms
;
586 for (i
= 0; i
< set_size
; i
++)
592 /* Return number of first bit set in the bitmap, -1 if none. */
595 sbitmap_first_set_bit (bmap
)
600 EXECUTE_IF_SET_IN_SBITMAP (bmap
, 0, n
, { return n
; });
604 /* Return number of last bit set in the bitmap, -1 if none. */
607 sbitmap_last_set_bit (bmap
)
611 SBITMAP_ELT_TYPE
*ptr
= bmap
->elms
;
613 for (i
= bmap
->size
- 1; i
>= 0; i
--)
615 SBITMAP_ELT_TYPE word
= ptr
[i
];
619 unsigned int index
= (i
+ 1) * SBITMAP_ELT_BITS
- 1;
620 SBITMAP_ELT_TYPE mask
621 = (SBITMAP_ELT_TYPE
) 1 << (SBITMAP_ELT_BITS
- 1);
625 if ((word
& mask
) != 0)
638 dump_sbitmap (file
, bmap
)
642 unsigned int i
, n
, j
;
643 unsigned int set_size
= bmap
->size
;
644 unsigned int total_bits
= bmap
->n_bits
;
647 for (i
= n
= 0; i
< set_size
&& n
< total_bits
; i
++)
648 for (j
= 0; j
< SBITMAP_ELT_BITS
&& n
< total_bits
; j
++, n
++)
650 if (n
!= 0 && n
% 10 == 0)
654 (bmap
->elms
[i
] & ((SBITMAP_ELT_TYPE
) 1 << j
)) != 0);
657 fprintf (file
, "\n");
666 fprintf (stderr
, "n_bits = %d, set = {", bmap
->n_bits
);
668 for (pos
= 30, i
= 0; i
< bmap
->n_bits
; i
++)
669 if (TEST_BIT (bmap
, i
))
673 fprintf (stderr
, "\n");
677 fprintf (stderr
, "%d ", i
);
678 pos
+= 1 + (i
>= 10) + (i
>= 100);
681 fprintf (stderr
, "}\n");
685 dump_sbitmap_vector (file
, title
, subtitle
, bmaps
, n_maps
)
687 const char *title
, *subtitle
;
693 fprintf (file
, "%s\n", title
);
694 for (bb
= 0; bb
< n_maps
; bb
++)
696 fprintf (file
, "%s %d\n", subtitle
, bb
);
697 dump_sbitmap (file
, bmaps
[bb
]);
700 fprintf (file
, "\n");