1 /* Functions to support general ended bitmaps.
2 Copyright (C) 1997 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
27 #include "basic-block.h"
29 /* The contents of the current function definition are allocated
30 in this obstack, and all are freed at the end of the function.
31 For top-level functions, this is temporary_obstack.
32 Separate obstacks are made for nested functions. */
34 extern struct obstack
*function_obstack
;
41 #define INLINE __inline__
46 bitmap_element bitmap_zero
; /* An element of all zero bits. */
47 bitmap_element
*bitmap_free
; /* Freelist of bitmap elements. */
49 static void bitmap_element_free
PROTO((bitmap
, bitmap_element
*));
50 static bitmap_element
*bitmap_element_allocate
PROTO((bitmap
));
51 static int bitmap_element_zerop
PROTO((bitmap_element
*));
52 static void bitmap_element_link
PROTO((bitmap
, bitmap_element
*));
53 static bitmap_element
*bitmap_find_bit
PROTO((bitmap
, unsigned int));
55 /* Free a bitmap element */
58 bitmap_element_free (head
, elt
)
62 bitmap_element
*next
= elt
->next
;
63 bitmap_element
*prev
= elt
->prev
;
71 if (head
->first
== elt
)
74 /* Since the first thing we try is to insert before current,
75 make current the next entry in preference to the previous. */
76 if (head
->current
== elt
)
77 head
->current
= next
!= 0 ? next
: prev
;
79 elt
->next
= bitmap_free
;
83 /* Allocate a bitmap element. The bits are cleared, but nothing else is. */
85 static INLINE bitmap_element
*
86 bitmap_element_allocate (head
)
89 bitmap_element
*element
;
94 element
= bitmap_free
;
95 bitmap_free
= element
->next
;
98 element
= (bitmap_element
*) obstack_alloc (function_obstack
,
99 sizeof (bitmap_element
));
101 #if BITMAP_ELEMENT_WORDS == 2
102 element
->bits
[0] = element
->bits
[1] = 0;
104 for (i
= 0; i
< BITMAP_ELEMENT_WORDS
; i
++)
105 element
->bits
[i
] = 0;
111 /* Return nonzero if all bits in an element are zero. */
114 bitmap_element_zerop (element
)
115 bitmap_element
*element
;
117 #if BITMAP_ELEMENT_WORDS == 2
118 return (element
->bits
[0] | element
->bits
[1]) == 0;
122 for (i
= 0; i
< BITMAP_ELEMENT_WORDS
; i
++)
123 if (element
->bits
[i
] != 0)
130 /* Link the bitmap element into the current bitmap linked list. */
133 bitmap_element_link (head
, element
)
135 bitmap_element
*element
;
137 unsigned int indx
= element
->indx
;
140 /* If this is the first and only element, set it in. */
141 if (head
->first
== 0)
143 element
->next
= element
->prev
= 0;
144 head
->first
= element
;
147 /* If this index is less than that of the current element, it goes someplace
148 before the current element. */
149 else if (indx
< head
->indx
)
151 for (ptr
= head
->current
;
152 ptr
->prev
!= 0 && ptr
->prev
->indx
> indx
;
157 ptr
->prev
->next
= element
;
159 head
->first
= element
;
161 element
->prev
= ptr
->prev
;
166 /* Otherwise, it must go someplace after the current element. */
169 for (ptr
= head
->current
;
170 ptr
->next
!= 0 && ptr
->next
->indx
< indx
;
175 ptr
->next
->prev
= element
;
177 element
->next
= ptr
->next
;
182 /* Set up so this is the first element searched. */
183 head
->current
= element
;
187 /* Clear a bitmap by freeing the linked list. */
193 bitmap_element
*element
, *next
;
195 for (element
= head
->first
; element
!= 0; element
= next
)
197 next
= element
->next
;
198 element
->next
= bitmap_free
;
199 bitmap_free
= element
;
202 head
->first
= head
->current
= 0;
205 /* Copy a bitmap to another bitmap */
208 bitmap_copy (to
, from
)
212 bitmap_element
*from_ptr
, *to_ptr
= 0;
217 /* Copy elements in forward direction one at a time */
218 for (from_ptr
= from
->first
; from_ptr
; from_ptr
= from_ptr
->next
)
220 bitmap_element
*to_elt
= bitmap_element_allocate (to
);
222 to_elt
->indx
= from_ptr
->indx
;
224 #if BITMAP_ELEMENT_WORDS == 2
225 to_elt
->bits
[0] = from_ptr
->bits
[0];
226 to_elt
->bits
[1] = from_ptr
->bits
[1];
228 for (i
= 0; i
< BITMAP_ELEMENT_WORDS
; i
++)
229 to_elt
->bits
[i
] = from_ptr
->bits
[i
];
232 /* Here we have a special case of bitmap_element_link, for the case
233 where we know the links are being entered in sequence. */
236 to
->first
= to
->current
= to_elt
;
237 to
->indx
= from_ptr
->indx
;
238 to_elt
->next
= to_elt
->prev
= 0;
242 to_elt
->prev
= to_ptr
;
244 to_ptr
->next
= to_elt
;
251 /* Find a bitmap element that would hold a bitmap's bit.
252 Update the `current' field even if we can't find an element that
253 would hold the bitmap's bit to make eventual allocation
256 static INLINE bitmap_element
*
257 bitmap_find_bit (head
, bit
)
261 bitmap_element
*element
;
262 unsigned HOST_WIDE_INT indx
= bit
/ BITMAP_ELEMENT_ALL_BITS
;
264 if (head
->current
== 0)
267 if (head
->indx
> indx
)
268 for (element
= head
->current
;
269 element
->prev
!= 0 && element
->indx
> indx
;
270 element
= element
->prev
)
274 for (element
= head
->current
;
275 element
->next
!= 0 && element
->indx
< indx
;
276 element
= element
->next
)
279 /* `element' is the nearest to the one we want. If it's not the one we
280 want, the one we want doesn't exist. */
281 head
->current
= element
;
282 head
->indx
= element
->indx
;
283 if (element
!= 0 && element
->indx
!= indx
)
289 /* Clear a single bit in a bitmap. */
292 bitmap_clear_bit (head
, bit
)
296 bitmap_element
*ptr
= bitmap_find_bit (head
, bit
);
300 unsigned bit_num
= bit
% (unsigned) HOST_BITS_PER_WIDE_INT
;
301 unsigned word_num
= ((bit
/ (unsigned) HOST_BITS_PER_WIDE_INT
)
302 % BITMAP_ELEMENT_WORDS
);
303 ptr
->bits
[word_num
] &= ~ (((unsigned HOST_WIDE_INT
) 1) << bit_num
);
305 /* If we cleared the entire word, free up the element */
306 if (bitmap_element_zerop (ptr
))
307 bitmap_element_free (head
, ptr
);
312 /* Set a single bit in a bitmap. */
315 bitmap_set_bit (head
, bit
)
319 bitmap_element
*ptr
= bitmap_find_bit (head
, bit
);
321 = ((bit
/ (unsigned) HOST_BITS_PER_WIDE_INT
) % BITMAP_ELEMENT_WORDS
);
322 unsigned bit_num
= bit
% (unsigned) HOST_BITS_PER_WIDE_INT
;
323 unsigned HOST_WIDE_INT bit_val
= ((unsigned HOST_WIDE_INT
) 1) << bit_num
;
327 ptr
= bitmap_element_allocate (head
);
328 ptr
->indx
= bit
/ BITMAP_ELEMENT_ALL_BITS
;
329 ptr
->bits
[word_num
] = bit_val
;
330 bitmap_element_link (head
, ptr
);
333 ptr
->bits
[word_num
] |= bit_val
;
336 /* Return whether a bit is set within a bitmap. */
339 bitmap_bit_p (head
, bit
)
347 ptr
= bitmap_find_bit (head
, bit
);
351 bit_num
= bit
% (unsigned) HOST_BITS_PER_WIDE_INT
;
353 = ((bit
/ (unsigned) HOST_BITS_PER_WIDE_INT
) % BITMAP_ELEMENT_WORDS
);
356 (ptr
->bits
[word_num
] & (((unsigned HOST_WIDE_INT
) 1) << bit_num
)) != 0;
359 /* Store in bitmap TO the result of combining bitmap FROM1 and
360 FROM2 using a specific bit manipulation. */
363 bitmap_operation (to
, from1
, from2
, operation
)
367 enum bitmap_bits operation
;
369 bitmap_element
*delete_list
= 0;
370 bitmap_element
*from1_ptr
= from1
->first
;
371 bitmap_element
*from2_ptr
= from2
->first
;
373 = (from1_ptr
) ? from1_ptr
->indx
: ~ (unsigned HOST_WIDE_INT
) 0;
375 = (from2_ptr
) ? from2_ptr
->indx
: ~ (unsigned HOST_WIDE_INT
) 0;
376 bitmap_element
*to_ptr
= 0;
377 bitmap_element
*from1_tmp
;
378 bitmap_element
*from2_tmp
;
382 /* To simplify things, always create a new list. If the old list was one
383 of the inputs, free it later. Otherwise, free it now. */
384 if (to
== from1
|| to
== from2
)
386 delete_list
= to
->first
;
387 to
->first
= to
->current
= 0;
392 while (from1_ptr
!= 0 || from2_ptr
!= 0)
394 /* Figure out whether we need to substitute zero elements for
399 from1_tmp
= from1_ptr
;
400 from2_tmp
= from2_ptr
;
401 from1_ptr
= from1_ptr
->next
;
402 indx1
= (from1_ptr
) ? from1_ptr
->indx
: ~ (unsigned HOST_WIDE_INT
) 0;
403 from2_ptr
= from2_ptr
->next
;
404 indx2
= (from2_ptr
) ? from2_ptr
->indx
: ~ (unsigned HOST_WIDE_INT
) 0;
406 else if (indx1
< indx2
)
409 from1_tmp
= from1_ptr
;
410 from2_tmp
= &bitmap_zero
;
411 from1_ptr
= from1_ptr
->next
;
412 indx1
= (from1_ptr
) ? from1_ptr
->indx
: ~ (unsigned HOST_WIDE_INT
) 0;
417 from1_tmp
= &bitmap_zero
;
418 from2_tmp
= from2_ptr
;
419 from2_ptr
= from2_ptr
->next
;
420 indx2
= (from2_ptr
) ? from2_ptr
->indx
: ~ (unsigned HOST_WIDE_INT
) 0;
424 to_ptr
= bitmap_element_allocate (to
);
426 /* Do the operation, and if any bits are set, link it into the
434 #if BITMAP_ELEMENT_WORDS == 2
435 to_ptr
->bits
[0] = from1_tmp
->bits
[0] & from2_tmp
->bits
[0];
436 to_ptr
->bits
[1] = from1_tmp
->bits
[1] & from2_tmp
->bits
[1];
438 for (i
= BITMAP_ELEMENT_WORDS
- 1; i
>= 0; i
--)
439 to_ptr
->bits
[i
] = from1_tmp
->bits
[i
] & from2_tmp
->bits
[i
];
443 case BITMAP_AND_COMPL
:
444 #if BITMAP_ELEMENT_WORDS == 2
445 to_ptr
->bits
[0] = from1_tmp
->bits
[0] & ~ from2_tmp
->bits
[0];
446 to_ptr
->bits
[1] = from1_tmp
->bits
[1] & ~ from2_tmp
->bits
[1];
448 for (i
= BITMAP_ELEMENT_WORDS
- 1; i
>= 0; i
--)
449 to_ptr
->bits
[i
] = from1_tmp
->bits
[i
] & ~ from2_tmp
->bits
[i
];
454 #if BITMAP_ELEMENT_WORDS == 2
455 to_ptr
->bits
[0] = from1_tmp
->bits
[0] | from2_tmp
->bits
[0];
456 to_ptr
->bits
[1] = from1_tmp
->bits
[1] | from2_tmp
->bits
[1];
458 for (i
= BITMAP_ELEMENT_WORDS
- 1; i
>= 0; i
--)
459 to_ptr
->bits
[i
] = from1_tmp
->bits
[i
] | from2_tmp
->bits
[i
];
464 if (! bitmap_element_zerop (to_ptr
))
467 bitmap_element_link (to
, to_ptr
);
472 /* If we have an unallocated element due to the last element being 0,
473 release it back to the free pool. Don't bother calling
474 bitmap_element_free since it was never linked into a bitmap. */
477 to_ptr
->next
= bitmap_free
;
478 bitmap_free
= to_ptr
;
481 /* If the output bitmap was one of the inputs, free up its
482 elements now that we're done. */
483 for (; delete_list
!= 0; delete_list
= to_ptr
)
485 to_ptr
= delete_list
->next
;
486 delete_list
->next
= bitmap_free
;
487 bitmap_free
= delete_list
;
491 /* Or into bitmap TO bitmap FROM1 and'ed with the complement of
495 bitmap_ior_and_compl (to
, from1
, from2
)
502 tmp
.first
= tmp
.current
= 0;
504 bitmap_operation (&tmp
, from1
, from2
, BITMAP_AND_COMPL
);
505 bitmap_operation (to
, to
, &tmp
, BITMAP_IOR
);
509 /* Initialize a bitmap header. */
512 bitmap_initialize (head
)
515 head
->first
= head
->current
= 0;
520 /* Debugging function to print out the contents of a bitmap. */
523 bitmap_debug_file (file
, head
)
529 fprintf (file
, "\nfirst = ");
530 fprintf (file
, HOST_PTR_PRINTF
, (HOST_WIDE_INT
) head
->first
);
531 fprintf (file
, " current = ");
532 fprintf (file
, HOST_PTR_PRINTF
, (HOST_WIDE_INT
) head
->current
);
533 fprintf (file
, " indx = %u\n", head
->indx
);
535 for (ptr
= head
->first
; ptr
; ptr
= ptr
->next
)
539 fprintf (file
, "\t");
540 fprintf (file
, HOST_PTR_PRINTF
, (HOST_WIDE_INT
) ptr
);
541 fprintf (file
, " next = ");
542 fprintf (file
, HOST_PTR_PRINTF
, (HOST_WIDE_INT
) ptr
->next
);
543 fprintf (file
, " prev = ");
544 fprintf (file
, HOST_PTR_PRINTF
, (HOST_WIDE_INT
) ptr
->prev
);
545 fprintf (file
, " indx = %u\n\t\tbits = {", ptr
->indx
);
547 for (i
= 0; i
< BITMAP_ELEMENT_WORDS
; i
++)
548 for (j
= 0; j
< HOST_BITS_PER_WIDE_INT
; j
++)
549 if ((ptr
->bits
[i
] & (((unsigned HOST_WIDE_INT
) 1) << j
)) != 0)
553 fprintf (file
, "\n\t\t\t");
557 fprintf (file
, " %u", (ptr
->indx
* BITMAP_ELEMENT_ALL_BITS
558 + i
* HOST_BITS_PER_WIDE_INT
+ j
));
562 fprintf (file
, " }\n");
566 /* Function to be called from the debugger to print the contents
573 bitmap_debug_file (stdout
, head
);
576 /* Release any memory allocated by bitmaps. Since we allocate off of the
577 function_obstack, just zap the free list. */
580 bitmap_release_memory ()