]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/bitmap.c
bitmap.c (bitmap_descriptor): Adjust order of parameters to match MEM_STAT_DECL.
[thirdparty/gcc.git] / gcc / bitmap.c
1 /* Functions to support general ended bitmaps.
2 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2003, 2004, 2005,
3 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "obstack.h"
25 #include "ggc.h"
26 #include "bitmap.h"
27 #include "hashtab.h"
28
29 /* Store information about each particular bitmap. */
30 struct bitmap_descriptor
31 {
32 const char *function;
33 const char *file;
34 int line;
35 int created;
36 HOST_WIDEST_INT allocated;
37 HOST_WIDEST_INT peak;
38 HOST_WIDEST_INT current;
39 int nsearches;
40 int search_iter;
41 };
42
43 /* Hashtable mapping bitmap names to descriptors. */
44 static htab_t bitmap_desc_hash;
45
46 /* Hashtable helpers. */
47 static hashval_t
48 hash_descriptor (const void *p)
49 {
50 const struct bitmap_descriptor *const d =
51 (const struct bitmap_descriptor *) p;
52 return htab_hash_pointer (d->file) + d->line;
53 }
54 struct loc
55 {
56 const char *file;
57 const char *function;
58 int line;
59 };
60 static int
61 eq_descriptor (const void *p1, const void *p2)
62 {
63 const struct bitmap_descriptor *const d =
64 (const struct bitmap_descriptor *) p1;
65 const struct loc *const l = (const struct loc *) p2;
66 return d->file == l->file && d->function == l->function && d->line == l->line;
67 }
68
69 /* For given file and line, return descriptor, create new if needed. */
70 static struct bitmap_descriptor *
71 bitmap_descriptor (const char *file, int line, const char *function)
72 {
73 struct bitmap_descriptor **slot;
74 struct loc loc;
75
76 loc.file = file;
77 loc.function = function;
78 loc.line = line;
79
80 if (!bitmap_desc_hash)
81 bitmap_desc_hash = htab_create (10, hash_descriptor, eq_descriptor, NULL);
82
83 slot = (struct bitmap_descriptor **)
84 htab_find_slot_with_hash (bitmap_desc_hash, &loc,
85 htab_hash_pointer (file) + line,
86 INSERT);
87 if (*slot)
88 return *slot;
89 *slot = XCNEW (struct bitmap_descriptor);
90 (*slot)->file = file;
91 (*slot)->function = function;
92 (*slot)->line = line;
93 return *slot;
94 }
95
96 /* Register new bitmap. */
97 void
98 bitmap_register (bitmap b MEM_STAT_DECL)
99 {
100 b->desc = bitmap_descriptor (ALONE_FINAL_PASS_MEM_STAT);
101 b->desc->created++;
102 }
103
104 /* Account the overhead. */
105 static void
106 register_overhead (bitmap b, int amount)
107 {
108 b->desc->current += amount;
109 if (amount > 0)
110 b->desc->allocated += amount;
111 gcc_assert (b->desc->current >= 0);
112 if (b->desc->peak < b->desc->current)
113 b->desc->peak = b->desc->current;
114 }
115
116 /* Global data */
117 bitmap_element bitmap_zero_bits; /* An element of all zero bits. */
118 bitmap_obstack bitmap_default_obstack; /* The default bitmap obstack. */
119 static int bitmap_default_obstack_depth;
120 static GTY((deletable)) bitmap_element *bitmap_ggc_free; /* Freelist of
121 GC'd elements. */
122
123 static void bitmap_elem_to_freelist (bitmap, bitmap_element *);
124 static void bitmap_element_free (bitmap, bitmap_element *);
125 static bitmap_element *bitmap_element_allocate (bitmap);
126 static int bitmap_element_zerop (const bitmap_element *);
127 static void bitmap_element_link (bitmap, bitmap_element *);
128 static bitmap_element *bitmap_elt_insert_after (bitmap, bitmap_element *, unsigned int);
129 static void bitmap_elt_clear_from (bitmap, bitmap_element *);
130 static bitmap_element *bitmap_find_bit (bitmap, unsigned int);
131 \f
132
133 /* Add ELEM to the appropriate freelist. */
134 static inline void
135 bitmap_elem_to_freelist (bitmap head, bitmap_element *elt)
136 {
137 bitmap_obstack *bit_obstack = head->obstack;
138
139 elt->next = NULL;
140 if (bit_obstack)
141 {
142 elt->prev = bit_obstack->elements;
143 bit_obstack->elements = elt;
144 }
145 else
146 {
147 elt->prev = bitmap_ggc_free;
148 bitmap_ggc_free = elt;
149 }
150 }
151
152 /* Free a bitmap element. Since these are allocated off the
153 bitmap_obstack, "free" actually means "put onto the freelist". */
154
155 static inline void
156 bitmap_element_free (bitmap head, bitmap_element *elt)
157 {
158 bitmap_element *next = elt->next;
159 bitmap_element *prev = elt->prev;
160
161 if (prev)
162 prev->next = next;
163
164 if (next)
165 next->prev = prev;
166
167 if (head->first == elt)
168 head->first = next;
169
170 /* Since the first thing we try is to insert before current,
171 make current the next entry in preference to the previous. */
172 if (head->current == elt)
173 {
174 head->current = next != 0 ? next : prev;
175 if (head->current)
176 head->indx = head->current->indx;
177 else
178 head->indx = 0;
179 }
180
181 if (GATHER_STATISTICS)
182 register_overhead (head, -((int)sizeof (bitmap_element)));
183
184 bitmap_elem_to_freelist (head, elt);
185 }
186 \f
187 /* Allocate a bitmap element. The bits are cleared, but nothing else is. */
188
189 static inline bitmap_element *
190 bitmap_element_allocate (bitmap head)
191 {
192 bitmap_element *element;
193 bitmap_obstack *bit_obstack = head->obstack;
194
195 if (bit_obstack)
196 {
197 element = bit_obstack->elements;
198
199 if (element)
200 /* Use up the inner list first before looking at the next
201 element of the outer list. */
202 if (element->next)
203 {
204 bit_obstack->elements = element->next;
205 bit_obstack->elements->prev = element->prev;
206 }
207 else
208 /* Inner list was just a singleton. */
209 bit_obstack->elements = element->prev;
210 else
211 element = XOBNEW (&bit_obstack->obstack, bitmap_element);
212 }
213 else
214 {
215 element = bitmap_ggc_free;
216 if (element)
217 /* Use up the inner list first before looking at the next
218 element of the outer list. */
219 if (element->next)
220 {
221 bitmap_ggc_free = element->next;
222 bitmap_ggc_free->prev = element->prev;
223 }
224 else
225 /* Inner list was just a singleton. */
226 bitmap_ggc_free = element->prev;
227 else
228 element = ggc_alloc_bitmap_element_def ();
229 }
230
231 if (GATHER_STATISTICS)
232 register_overhead (head, sizeof (bitmap_element));
233
234 memset (element->bits, 0, sizeof (element->bits));
235
236 return element;
237 }
238
239 /* Remove ELT and all following elements from bitmap HEAD. */
240
241 void
242 bitmap_elt_clear_from (bitmap head, bitmap_element *elt)
243 {
244 bitmap_element *prev;
245 bitmap_obstack *bit_obstack = head->obstack;
246
247 if (!elt) return;
248
249 if (GATHER_STATISTICS)
250 {
251 int n = 0;
252 for (prev = elt; prev; prev = prev->next)
253 n++;
254 register_overhead (head, -sizeof (bitmap_element) * n);
255 }
256
257 prev = elt->prev;
258 if (prev)
259 {
260 prev->next = NULL;
261 if (head->current->indx > prev->indx)
262 {
263 head->current = prev;
264 head->indx = prev->indx;
265 }
266 }
267 else
268 {
269 head->first = NULL;
270 head->current = NULL;
271 head->indx = 0;
272 }
273
274 /* Put the entire list onto the free list in one operation. */
275 if (bit_obstack)
276 {
277 elt->prev = bit_obstack->elements;
278 bit_obstack->elements = elt;
279 }
280 else
281 {
282 elt->prev = bitmap_ggc_free;
283 bitmap_ggc_free = elt;
284 }
285 }
286
287 /* Clear a bitmap by freeing the linked list. */
288
289 void
290 bitmap_clear (bitmap head)
291 {
292 if (head->first)
293 bitmap_elt_clear_from (head, head->first);
294 }
295 \f
296 /* Initialize a bitmap obstack. If BIT_OBSTACK is NULL, initialize
297 the default bitmap obstack. */
298
299 void
300 bitmap_obstack_initialize (bitmap_obstack *bit_obstack)
301 {
302 if (!bit_obstack)
303 {
304 if (bitmap_default_obstack_depth++)
305 return;
306 bit_obstack = &bitmap_default_obstack;
307 }
308
309 #if !defined(__GNUC__) || (__GNUC__ < 2)
310 #define __alignof__(type) 0
311 #endif
312
313 bit_obstack->elements = NULL;
314 bit_obstack->heads = NULL;
315 obstack_specify_allocation (&bit_obstack->obstack, OBSTACK_CHUNK_SIZE,
316 __alignof__ (bitmap_element),
317 obstack_chunk_alloc,
318 obstack_chunk_free);
319 }
320
321 /* Release the memory from a bitmap obstack. If BIT_OBSTACK is NULL,
322 release the default bitmap obstack. */
323
324 void
325 bitmap_obstack_release (bitmap_obstack *bit_obstack)
326 {
327 if (!bit_obstack)
328 {
329 if (--bitmap_default_obstack_depth)
330 {
331 gcc_assert (bitmap_default_obstack_depth > 0);
332 return;
333 }
334 bit_obstack = &bitmap_default_obstack;
335 }
336
337 bit_obstack->elements = NULL;
338 bit_obstack->heads = NULL;
339 obstack_free (&bit_obstack->obstack, NULL);
340 }
341
342 /* Create a new bitmap on an obstack. If BIT_OBSTACK is NULL, create
343 it on the default bitmap obstack. */
344
345 bitmap
346 bitmap_obstack_alloc_stat (bitmap_obstack *bit_obstack MEM_STAT_DECL)
347 {
348 bitmap map;
349
350 if (!bit_obstack)
351 bit_obstack = &bitmap_default_obstack;
352 map = bit_obstack->heads;
353 if (map)
354 bit_obstack->heads = (struct bitmap_head_def *) map->first;
355 else
356 map = XOBNEW (&bit_obstack->obstack, bitmap_head);
357 bitmap_initialize_stat (map, bit_obstack PASS_MEM_STAT);
358
359 if (GATHER_STATISTICS)
360 register_overhead (map, sizeof (bitmap_head));
361
362 return map;
363 }
364
365 /* Create a new GCd bitmap. */
366
367 bitmap
368 bitmap_gc_alloc_stat (ALONE_MEM_STAT_DECL)
369 {
370 bitmap map;
371
372 map = ggc_alloc_bitmap_head_def ();
373 bitmap_initialize_stat (map, NULL PASS_MEM_STAT);
374
375 if (GATHER_STATISTICS)
376 register_overhead (map, sizeof (bitmap_head));
377
378 return map;
379 }
380
381 /* Release an obstack allocated bitmap. */
382
383 void
384 bitmap_obstack_free (bitmap map)
385 {
386 if (map)
387 {
388 bitmap_clear (map);
389 map->first = (bitmap_element *) map->obstack->heads;
390
391 if (GATHER_STATISTICS)
392 register_overhead (map, -((int)sizeof (bitmap_head)));
393
394 map->obstack->heads = map;
395 }
396 }
397
398 \f
399 /* Return nonzero if all bits in an element are zero. */
400
401 static inline int
402 bitmap_element_zerop (const bitmap_element *element)
403 {
404 #if BITMAP_ELEMENT_WORDS == 2
405 return (element->bits[0] | element->bits[1]) == 0;
406 #else
407 unsigned i;
408
409 for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
410 if (element->bits[i] != 0)
411 return 0;
412
413 return 1;
414 #endif
415 }
416 \f
417 /* Link the bitmap element into the current bitmap linked list. */
418
419 static inline void
420 bitmap_element_link (bitmap head, bitmap_element *element)
421 {
422 unsigned int indx = element->indx;
423 bitmap_element *ptr;
424
425 /* If this is the first and only element, set it in. */
426 if (head->first == 0)
427 {
428 element->next = element->prev = 0;
429 head->first = element;
430 }
431
432 /* If this index is less than that of the current element, it goes someplace
433 before the current element. */
434 else if (indx < head->indx)
435 {
436 for (ptr = head->current;
437 ptr->prev != 0 && ptr->prev->indx > indx;
438 ptr = ptr->prev)
439 ;
440
441 if (ptr->prev)
442 ptr->prev->next = element;
443 else
444 head->first = element;
445
446 element->prev = ptr->prev;
447 element->next = ptr;
448 ptr->prev = element;
449 }
450
451 /* Otherwise, it must go someplace after the current element. */
452 else
453 {
454 for (ptr = head->current;
455 ptr->next != 0 && ptr->next->indx < indx;
456 ptr = ptr->next)
457 ;
458
459 if (ptr->next)
460 ptr->next->prev = element;
461
462 element->next = ptr->next;
463 element->prev = ptr;
464 ptr->next = element;
465 }
466
467 /* Set up so this is the first element searched. */
468 head->current = element;
469 head->indx = indx;
470 }
471
472 /* Insert a new uninitialized element into bitmap HEAD after element
473 ELT. If ELT is NULL, insert the element at the start. Return the
474 new element. */
475
476 static bitmap_element *
477 bitmap_elt_insert_after (bitmap head, bitmap_element *elt, unsigned int indx)
478 {
479 bitmap_element *node = bitmap_element_allocate (head);
480 node->indx = indx;
481
482 if (!elt)
483 {
484 if (!head->current)
485 {
486 head->current = node;
487 head->indx = indx;
488 }
489 node->next = head->first;
490 if (node->next)
491 node->next->prev = node;
492 head->first = node;
493 node->prev = NULL;
494 }
495 else
496 {
497 gcc_checking_assert (head->current);
498 node->next = elt->next;
499 if (node->next)
500 node->next->prev = node;
501 elt->next = node;
502 node->prev = elt;
503 }
504 return node;
505 }
506 \f
507 /* Copy a bitmap to another bitmap. */
508
509 void
510 bitmap_copy (bitmap to, const_bitmap from)
511 {
512 const bitmap_element *from_ptr;
513 bitmap_element *to_ptr = 0;
514
515 bitmap_clear (to);
516
517 /* Copy elements in forward direction one at a time. */
518 for (from_ptr = from->first; from_ptr; from_ptr = from_ptr->next)
519 {
520 bitmap_element *to_elt = bitmap_element_allocate (to);
521
522 to_elt->indx = from_ptr->indx;
523 memcpy (to_elt->bits, from_ptr->bits, sizeof (to_elt->bits));
524
525 /* Here we have a special case of bitmap_element_link, for the case
526 where we know the links are being entered in sequence. */
527 if (to_ptr == 0)
528 {
529 to->first = to->current = to_elt;
530 to->indx = from_ptr->indx;
531 to_elt->next = to_elt->prev = 0;
532 }
533 else
534 {
535 to_elt->prev = to_ptr;
536 to_elt->next = 0;
537 to_ptr->next = to_elt;
538 }
539
540 to_ptr = to_elt;
541 }
542 }
543 \f
544 /* Find a bitmap element that would hold a bitmap's bit.
545 Update the `current' field even if we can't find an element that
546 would hold the bitmap's bit to make eventual allocation
547 faster. */
548
549 static inline bitmap_element *
550 bitmap_find_bit (bitmap head, unsigned int bit)
551 {
552 bitmap_element *element;
553 unsigned int indx = bit / BITMAP_ELEMENT_ALL_BITS;
554
555 if (head->current == 0
556 || head->indx == indx)
557 return head->current;
558
559 if (GATHER_STATISTICS)
560 head->desc->nsearches++;
561
562 if (head->indx < indx)
563 /* INDX is beyond head->indx. Search from head->current
564 forward. */
565 for (element = head->current;
566 element->next != 0 && element->indx < indx;
567 element = element->next)
568 {
569 if (GATHER_STATISTICS)
570 head->desc->search_iter++;
571 }
572
573 else if (head->indx / 2 < indx)
574 /* INDX is less than head->indx and closer to head->indx than to
575 0. Search from head->current backward. */
576 for (element = head->current;
577 element->prev != 0 && element->indx > indx;
578 element = element->prev)
579 {
580 if (GATHER_STATISTICS)
581 head->desc->search_iter++;
582 }
583
584 else
585 /* INDX is less than head->indx and closer to 0 than to
586 head->indx. Search from head->first forward. */
587 for (element = head->first;
588 element->next != 0 && element->indx < indx;
589 element = element->next)
590 if (GATHER_STATISTICS)
591 {
592 head->desc->search_iter++;
593 }
594
595 /* `element' is the nearest to the one we want. If it's not the one we
596 want, the one we want doesn't exist. */
597 head->current = element;
598 head->indx = element->indx;
599 if (element != 0 && element->indx != indx)
600 element = 0;
601
602 return element;
603 }
604 \f
605 /* Clear a single bit in a bitmap. Return true if the bit changed. */
606
607 bool
608 bitmap_clear_bit (bitmap head, int bit)
609 {
610 bitmap_element *const ptr = bitmap_find_bit (head, bit);
611
612 if (ptr != 0)
613 {
614 unsigned bit_num = bit % BITMAP_WORD_BITS;
615 unsigned word_num = bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
616 BITMAP_WORD bit_val = ((BITMAP_WORD) 1) << bit_num;
617 bool res = (ptr->bits[word_num] & bit_val) != 0;
618 if (res)
619 {
620 ptr->bits[word_num] &= ~bit_val;
621 /* If we cleared the entire word, free up the element. */
622 if (!ptr->bits[word_num]
623 && bitmap_element_zerop (ptr))
624 bitmap_element_free (head, ptr);
625 }
626
627 return res;
628 }
629
630 return false;
631 }
632
633 /* Set a single bit in a bitmap. Return true if the bit changed. */
634
635 bool
636 bitmap_set_bit (bitmap head, int bit)
637 {
638 bitmap_element *ptr = bitmap_find_bit (head, bit);
639 unsigned word_num = bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
640 unsigned bit_num = bit % BITMAP_WORD_BITS;
641 BITMAP_WORD bit_val = ((BITMAP_WORD) 1) << bit_num;
642
643 if (ptr == 0)
644 {
645 ptr = bitmap_element_allocate (head);
646 ptr->indx = bit / BITMAP_ELEMENT_ALL_BITS;
647 ptr->bits[word_num] = bit_val;
648 bitmap_element_link (head, ptr);
649 return true;
650 }
651 else
652 {
653 bool res = (ptr->bits[word_num] & bit_val) == 0;
654 if (res)
655 ptr->bits[word_num] |= bit_val;
656 return res;
657 }
658 }
659
660 /* Return whether a bit is set within a bitmap. */
661
662 int
663 bitmap_bit_p (bitmap head, int bit)
664 {
665 bitmap_element *ptr;
666 unsigned bit_num;
667 unsigned word_num;
668
669 ptr = bitmap_find_bit (head, bit);
670 if (ptr == 0)
671 return 0;
672
673 bit_num = bit % BITMAP_WORD_BITS;
674 word_num = bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
675
676 return (ptr->bits[word_num] >> bit_num) & 1;
677 }
678 \f
679 #if GCC_VERSION < 3400
680 /* Table of number of set bits in a character, indexed by value of char. */
681 static const unsigned char popcount_table[] =
682 {
683 0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
684 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
685 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
686 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
687 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
688 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
689 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
690 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8,
691 };
692
693 static unsigned long
694 bitmap_popcount (BITMAP_WORD a)
695 {
696 unsigned long ret = 0;
697 unsigned i;
698
699 /* Just do this the table way for now */
700 for (i = 0; i < BITMAP_WORD_BITS; i+= 8)
701 ret += popcount_table[(a >> i) & 0xff];
702 return ret;
703 }
704 #endif
705 /* Count the number of bits set in the bitmap, and return it. */
706
707 unsigned long
708 bitmap_count_bits (const_bitmap a)
709 {
710 unsigned long count = 0;
711 const bitmap_element *elt;
712 unsigned ix;
713
714 for (elt = a->first; elt; elt = elt->next)
715 {
716 for (ix = 0; ix != BITMAP_ELEMENT_WORDS; ix++)
717 {
718 #if GCC_VERSION >= 3400
719 /* Note that popcountl matches BITMAP_WORD in type, so the actual size
720 of BITMAP_WORD is not material. */
721 count += __builtin_popcountl (elt->bits[ix]);
722 #else
723 count += bitmap_popcount (elt->bits[ix]);
724 #endif
725 }
726 }
727 return count;
728 }
729
730 /* Return true if the bitmap has a single bit set. Otherwise return
731 false. */
732
733 bool
734 bitmap_single_bit_set_p (const_bitmap a)
735 {
736 unsigned long count = 0;
737 const bitmap_element *elt;
738 unsigned ix;
739
740 if (bitmap_empty_p (a))
741 return false;
742
743 elt = a->first;
744 /* As there are no completely empty bitmap elements, a second one
745 means we have more than one bit set. */
746 if (elt->next != NULL)
747 return false;
748
749 for (ix = 0; ix != BITMAP_ELEMENT_WORDS; ix++)
750 {
751 #if GCC_VERSION >= 3400
752 /* Note that popcountl matches BITMAP_WORD in type, so the actual size
753 of BITMAP_WORD is not material. */
754 count += __builtin_popcountl (elt->bits[ix]);
755 #else
756 count += bitmap_popcount (elt->bits[ix]);
757 #endif
758 if (count > 1)
759 return false;
760 }
761
762 return count == 1;
763 }
764
765
766 /* Return the bit number of the first set bit in the bitmap. The
767 bitmap must be non-empty. */
768
769 unsigned
770 bitmap_first_set_bit (const_bitmap a)
771 {
772 const bitmap_element *elt = a->first;
773 unsigned bit_no;
774 BITMAP_WORD word;
775 unsigned ix;
776
777 gcc_checking_assert (elt);
778 bit_no = elt->indx * BITMAP_ELEMENT_ALL_BITS;
779 for (ix = 0; ix != BITMAP_ELEMENT_WORDS; ix++)
780 {
781 word = elt->bits[ix];
782 if (word)
783 goto found_bit;
784 }
785 gcc_unreachable ();
786 found_bit:
787 bit_no += ix * BITMAP_WORD_BITS;
788
789 #if GCC_VERSION >= 3004
790 gcc_assert (sizeof(long) == sizeof (word));
791 bit_no += __builtin_ctzl (word);
792 #else
793 /* Binary search for the first set bit. */
794 #if BITMAP_WORD_BITS > 64
795 #error "Fill out the table."
796 #endif
797 #if BITMAP_WORD_BITS > 32
798 if (!(word & 0xffffffff))
799 word >>= 32, bit_no += 32;
800 #endif
801 if (!(word & 0xffff))
802 word >>= 16, bit_no += 16;
803 if (!(word & 0xff))
804 word >>= 8, bit_no += 8;
805 if (!(word & 0xf))
806 word >>= 4, bit_no += 4;
807 if (!(word & 0x3))
808 word >>= 2, bit_no += 2;
809 if (!(word & 0x1))
810 word >>= 1, bit_no += 1;
811
812 gcc_checking_assert (word & 1);
813 #endif
814 return bit_no;
815 }
816
817 /* Return the bit number of the first set bit in the bitmap. The
818 bitmap must be non-empty. */
819
820 unsigned
821 bitmap_last_set_bit (const_bitmap a)
822 {
823 const bitmap_element *elt = a->current ? a->current : a->first;
824 unsigned bit_no;
825 BITMAP_WORD word;
826 int ix;
827
828 gcc_checking_assert (elt);
829 while (elt->next)
830 elt = elt->next;
831 bit_no = elt->indx * BITMAP_ELEMENT_ALL_BITS;
832 for (ix = BITMAP_ELEMENT_WORDS - 1; ix >= 0; ix--)
833 {
834 word = elt->bits[ix];
835 if (word)
836 goto found_bit;
837 }
838 gcc_unreachable ();
839 found_bit:
840 bit_no += ix * BITMAP_WORD_BITS;
841
842 /* Binary search for the last set bit. */
843 #if GCC_VERSION >= 3004
844 gcc_assert (sizeof(long) == sizeof (word));
845 bit_no += sizeof (long) * 8 - __builtin_ctzl (word);
846 #else
847 #if BITMAP_WORD_BITS > 64
848 #error "Fill out the table."
849 #endif
850 #if BITMAP_WORD_BITS > 32
851 if ((word & 0xffffffff00000000))
852 word >>= 32, bit_no += 32;
853 #endif
854 if (word & 0xffff0000)
855 word >>= 16, bit_no += 16;
856 if (!(word & 0xff00))
857 word >>= 8, bit_no += 8;
858 if (!(word & 0xf0))
859 word >>= 4, bit_no += 4;
860 if (!(word & 12))
861 word >>= 2, bit_no += 2;
862 if (!(word & 2))
863 word >>= 1, bit_no += 1;
864 #endif
865
866 gcc_checking_assert (word & 1);
867 return bit_no;
868 }
869 \f
870
871 /* DST = A & B. */
872
873 void
874 bitmap_and (bitmap dst, const_bitmap a, const_bitmap b)
875 {
876 bitmap_element *dst_elt = dst->first;
877 const bitmap_element *a_elt = a->first;
878 const bitmap_element *b_elt = b->first;
879 bitmap_element *dst_prev = NULL;
880
881 gcc_assert (dst != a && dst != b);
882
883 if (a == b)
884 {
885 bitmap_copy (dst, a);
886 return;
887 }
888
889 while (a_elt && b_elt)
890 {
891 if (a_elt->indx < b_elt->indx)
892 a_elt = a_elt->next;
893 else if (b_elt->indx < a_elt->indx)
894 b_elt = b_elt->next;
895 else
896 {
897 /* Matching elts, generate A & B. */
898 unsigned ix;
899 BITMAP_WORD ior = 0;
900
901 if (!dst_elt)
902 dst_elt = bitmap_elt_insert_after (dst, dst_prev, a_elt->indx);
903 else
904 dst_elt->indx = a_elt->indx;
905 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
906 {
907 BITMAP_WORD r = a_elt->bits[ix] & b_elt->bits[ix];
908
909 dst_elt->bits[ix] = r;
910 ior |= r;
911 }
912 if (ior)
913 {
914 dst_prev = dst_elt;
915 dst_elt = dst_elt->next;
916 }
917 a_elt = a_elt->next;
918 b_elt = b_elt->next;
919 }
920 }
921 /* Ensure that dst->current is valid. */
922 dst->current = dst->first;
923 bitmap_elt_clear_from (dst, dst_elt);
924 gcc_checking_assert (!dst->current == !dst->first);
925 if (dst->current)
926 dst->indx = dst->current->indx;
927 }
928
929 /* A &= B. */
930
931 void
932 bitmap_and_into (bitmap a, const_bitmap b)
933 {
934 bitmap_element *a_elt = a->first;
935 const bitmap_element *b_elt = b->first;
936 bitmap_element *next;
937
938 if (a == b)
939 return;
940
941 while (a_elt && b_elt)
942 {
943 if (a_elt->indx < b_elt->indx)
944 {
945 next = a_elt->next;
946 bitmap_element_free (a, a_elt);
947 a_elt = next;
948 }
949 else if (b_elt->indx < a_elt->indx)
950 b_elt = b_elt->next;
951 else
952 {
953 /* Matching elts, generate A &= B. */
954 unsigned ix;
955 BITMAP_WORD ior = 0;
956
957 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
958 {
959 BITMAP_WORD r = a_elt->bits[ix] & b_elt->bits[ix];
960
961 a_elt->bits[ix] = r;
962 ior |= r;
963 }
964 next = a_elt->next;
965 if (!ior)
966 bitmap_element_free (a, a_elt);
967 a_elt = next;
968 b_elt = b_elt->next;
969 }
970 }
971 bitmap_elt_clear_from (a, a_elt);
972 gcc_checking_assert (!a->current == !a->first
973 && (!a->current || a->indx == a->current->indx));
974 }
975
976
977 /* Insert an element equal to SRC_ELT after DST_PREV, overwriting DST_ELT
978 if non-NULL. CHANGED is true if the destination bitmap had already been
979 changed; the new value of CHANGED is returned. */
980
981 static inline bool
982 bitmap_elt_copy (bitmap dst, bitmap_element *dst_elt, bitmap_element *dst_prev,
983 const bitmap_element *src_elt, bool changed)
984 {
985 if (!changed && dst_elt && dst_elt->indx == src_elt->indx)
986 {
987 unsigned ix;
988
989 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
990 if (src_elt->bits[ix] != dst_elt->bits[ix])
991 {
992 dst_elt->bits[ix] = src_elt->bits[ix];
993 changed = true;
994 }
995 }
996 else
997 {
998 changed = true;
999 if (!dst_elt)
1000 dst_elt = bitmap_elt_insert_after (dst, dst_prev, src_elt->indx);
1001 else
1002 dst_elt->indx = src_elt->indx;
1003 memcpy (dst_elt->bits, src_elt->bits, sizeof (dst_elt->bits));
1004 }
1005 return changed;
1006 }
1007
1008
1009
1010 /* DST = A & ~B */
1011
1012 bool
1013 bitmap_and_compl (bitmap dst, const_bitmap a, const_bitmap b)
1014 {
1015 bitmap_element *dst_elt = dst->first;
1016 const bitmap_element *a_elt = a->first;
1017 const bitmap_element *b_elt = b->first;
1018 bitmap_element *dst_prev = NULL;
1019 bitmap_element **dst_prev_pnext = &dst->first;
1020 bool changed = false;
1021
1022 gcc_assert (dst != a && dst != b);
1023
1024 if (a == b)
1025 {
1026 changed = !bitmap_empty_p (dst);
1027 bitmap_clear (dst);
1028 return changed;
1029 }
1030
1031 while (a_elt)
1032 {
1033 while (b_elt && b_elt->indx < a_elt->indx)
1034 b_elt = b_elt->next;
1035
1036 if (!b_elt || b_elt->indx > a_elt->indx)
1037 {
1038 changed = bitmap_elt_copy (dst, dst_elt, dst_prev, a_elt, changed);
1039 dst_prev = *dst_prev_pnext;
1040 dst_prev_pnext = &dst_prev->next;
1041 dst_elt = *dst_prev_pnext;
1042 a_elt = a_elt->next;
1043 }
1044
1045 else
1046 {
1047 /* Matching elts, generate A & ~B. */
1048 unsigned ix;
1049 BITMAP_WORD ior = 0;
1050
1051 if (!changed && dst_elt && dst_elt->indx == a_elt->indx)
1052 {
1053 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1054 {
1055 BITMAP_WORD r = a_elt->bits[ix] & ~b_elt->bits[ix];
1056
1057 if (dst_elt->bits[ix] != r)
1058 {
1059 changed = true;
1060 dst_elt->bits[ix] = r;
1061 }
1062 ior |= r;
1063 }
1064 }
1065 else
1066 {
1067 bool new_element;
1068 if (!dst_elt || dst_elt->indx > a_elt->indx)
1069 {
1070 dst_elt = bitmap_elt_insert_after (dst, dst_prev, a_elt->indx);
1071 new_element = true;
1072 }
1073 else
1074 {
1075 dst_elt->indx = a_elt->indx;
1076 new_element = false;
1077 }
1078
1079 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1080 {
1081 BITMAP_WORD r = a_elt->bits[ix] & ~b_elt->bits[ix];
1082
1083 dst_elt->bits[ix] = r;
1084 ior |= r;
1085 }
1086
1087 if (ior)
1088 changed = true;
1089 else
1090 {
1091 changed |= !new_element;
1092 bitmap_element_free (dst, dst_elt);
1093 dst_elt = *dst_prev_pnext;
1094 }
1095 }
1096
1097 if (ior)
1098 {
1099 dst_prev = *dst_prev_pnext;
1100 dst_prev_pnext = &dst_prev->next;
1101 dst_elt = *dst_prev_pnext;
1102 }
1103 a_elt = a_elt->next;
1104 b_elt = b_elt->next;
1105 }
1106 }
1107
1108 /* Ensure that dst->current is valid. */
1109 dst->current = dst->first;
1110
1111 if (dst_elt)
1112 {
1113 changed = true;
1114 bitmap_elt_clear_from (dst, dst_elt);
1115 }
1116 gcc_checking_assert (!dst->current == !dst->first);
1117 if (dst->current)
1118 dst->indx = dst->current->indx;
1119
1120 return changed;
1121 }
1122
1123 /* A &= ~B. Returns true if A changes */
1124
1125 bool
1126 bitmap_and_compl_into (bitmap a, const_bitmap b)
1127 {
1128 bitmap_element *a_elt = a->first;
1129 const bitmap_element *b_elt = b->first;
1130 bitmap_element *next;
1131 BITMAP_WORD changed = 0;
1132
1133 if (a == b)
1134 {
1135 if (bitmap_empty_p (a))
1136 return false;
1137 else
1138 {
1139 bitmap_clear (a);
1140 return true;
1141 }
1142 }
1143
1144 while (a_elt && b_elt)
1145 {
1146 if (a_elt->indx < b_elt->indx)
1147 a_elt = a_elt->next;
1148 else if (b_elt->indx < a_elt->indx)
1149 b_elt = b_elt->next;
1150 else
1151 {
1152 /* Matching elts, generate A &= ~B. */
1153 unsigned ix;
1154 BITMAP_WORD ior = 0;
1155
1156 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1157 {
1158 BITMAP_WORD cleared = a_elt->bits[ix] & b_elt->bits[ix];
1159 BITMAP_WORD r = a_elt->bits[ix] ^ cleared;
1160
1161 a_elt->bits[ix] = r;
1162 changed |= cleared;
1163 ior |= r;
1164 }
1165 next = a_elt->next;
1166 if (!ior)
1167 bitmap_element_free (a, a_elt);
1168 a_elt = next;
1169 b_elt = b_elt->next;
1170 }
1171 }
1172 gcc_checking_assert (!a->current == !a->first
1173 && (!a->current || a->indx == a->current->indx));
1174 return changed != 0;
1175 }
1176
1177 /* Set COUNT bits from START in HEAD. */
1178 void
1179 bitmap_set_range (bitmap head, unsigned int start, unsigned int count)
1180 {
1181 unsigned int first_index, end_bit_plus1, last_index;
1182 bitmap_element *elt, *elt_prev;
1183 unsigned int i;
1184
1185 if (!count)
1186 return;
1187
1188 first_index = start / BITMAP_ELEMENT_ALL_BITS;
1189 end_bit_plus1 = start + count;
1190 last_index = (end_bit_plus1 - 1) / BITMAP_ELEMENT_ALL_BITS;
1191 elt = bitmap_find_bit (head, start);
1192
1193 /* If bitmap_find_bit returns zero, the current is the closest block
1194 to the result. Otherwise, just use bitmap_element_allocate to
1195 ensure ELT is set; in the loop below, ELT == NULL means "insert
1196 at the end of the bitmap". */
1197 if (!elt)
1198 {
1199 elt = bitmap_element_allocate (head);
1200 elt->indx = first_index;
1201 bitmap_element_link (head, elt);
1202 }
1203
1204 gcc_checking_assert (elt->indx == first_index);
1205 elt_prev = elt->prev;
1206 for (i = first_index; i <= last_index; i++)
1207 {
1208 unsigned elt_start_bit = i * BITMAP_ELEMENT_ALL_BITS;
1209 unsigned elt_end_bit_plus1 = elt_start_bit + BITMAP_ELEMENT_ALL_BITS;
1210
1211 unsigned int first_word_to_mod;
1212 BITMAP_WORD first_mask;
1213 unsigned int last_word_to_mod;
1214 BITMAP_WORD last_mask;
1215 unsigned int ix;
1216
1217 if (!elt || elt->indx != i)
1218 elt = bitmap_elt_insert_after (head, elt_prev, i);
1219
1220 if (elt_start_bit <= start)
1221 {
1222 /* The first bit to turn on is somewhere inside this
1223 elt. */
1224 first_word_to_mod = (start - elt_start_bit) / BITMAP_WORD_BITS;
1225
1226 /* This mask should have 1s in all bits >= start position. */
1227 first_mask =
1228 (((BITMAP_WORD) 1) << ((start % BITMAP_WORD_BITS))) - 1;
1229 first_mask = ~first_mask;
1230 }
1231 else
1232 {
1233 /* The first bit to turn on is below this start of this elt. */
1234 first_word_to_mod = 0;
1235 first_mask = ~(BITMAP_WORD) 0;
1236 }
1237
1238 if (elt_end_bit_plus1 <= end_bit_plus1)
1239 {
1240 /* The last bit to turn on is beyond this elt. */
1241 last_word_to_mod = BITMAP_ELEMENT_WORDS - 1;
1242 last_mask = ~(BITMAP_WORD) 0;
1243 }
1244 else
1245 {
1246 /* The last bit to turn on is inside to this elt. */
1247 last_word_to_mod =
1248 (end_bit_plus1 - elt_start_bit) / BITMAP_WORD_BITS;
1249
1250 /* The last mask should have 1s below the end bit. */
1251 last_mask =
1252 (((BITMAP_WORD) 1) << ((end_bit_plus1 % BITMAP_WORD_BITS))) - 1;
1253 }
1254
1255 if (first_word_to_mod == last_word_to_mod)
1256 {
1257 BITMAP_WORD mask = first_mask & last_mask;
1258 elt->bits[first_word_to_mod] |= mask;
1259 }
1260 else
1261 {
1262 elt->bits[first_word_to_mod] |= first_mask;
1263 if (BITMAP_ELEMENT_WORDS > 2)
1264 for (ix = first_word_to_mod + 1; ix < last_word_to_mod; ix++)
1265 elt->bits[ix] = ~(BITMAP_WORD) 0;
1266 elt->bits[last_word_to_mod] |= last_mask;
1267 }
1268
1269 elt_prev = elt;
1270 elt = elt->next;
1271 }
1272
1273 head->current = elt ? elt : elt_prev;
1274 head->indx = head->current->indx;
1275 }
1276
1277 /* Clear COUNT bits from START in HEAD. */
1278 void
1279 bitmap_clear_range (bitmap head, unsigned int start, unsigned int count)
1280 {
1281 unsigned int first_index, end_bit_plus1, last_index;
1282 bitmap_element *elt;
1283
1284 if (!count)
1285 return;
1286
1287 first_index = start / BITMAP_ELEMENT_ALL_BITS;
1288 end_bit_plus1 = start + count;
1289 last_index = (end_bit_plus1 - 1) / BITMAP_ELEMENT_ALL_BITS;
1290 elt = bitmap_find_bit (head, start);
1291
1292 /* If bitmap_find_bit returns zero, the current is the closest block
1293 to the result. If the current is less than first index, find the
1294 next one. Otherwise, just set elt to be current. */
1295 if (!elt)
1296 {
1297 if (head->current)
1298 {
1299 if (head->indx < first_index)
1300 {
1301 elt = head->current->next;
1302 if (!elt)
1303 return;
1304 }
1305 else
1306 elt = head->current;
1307 }
1308 else
1309 return;
1310 }
1311
1312 while (elt && (elt->indx <= last_index))
1313 {
1314 bitmap_element * next_elt = elt->next;
1315 unsigned elt_start_bit = (elt->indx) * BITMAP_ELEMENT_ALL_BITS;
1316 unsigned elt_end_bit_plus1 = elt_start_bit + BITMAP_ELEMENT_ALL_BITS;
1317
1318
1319 if (elt_start_bit >= start && elt_end_bit_plus1 <= end_bit_plus1)
1320 /* Get rid of the entire elt and go to the next one. */
1321 bitmap_element_free (head, elt);
1322 else
1323 {
1324 /* Going to have to knock out some bits in this elt. */
1325 unsigned int first_word_to_mod;
1326 BITMAP_WORD first_mask;
1327 unsigned int last_word_to_mod;
1328 BITMAP_WORD last_mask;
1329 unsigned int i;
1330 bool clear = true;
1331
1332 if (elt_start_bit <= start)
1333 {
1334 /* The first bit to turn off is somewhere inside this
1335 elt. */
1336 first_word_to_mod = (start - elt_start_bit) / BITMAP_WORD_BITS;
1337
1338 /* This mask should have 1s in all bits >= start position. */
1339 first_mask =
1340 (((BITMAP_WORD) 1) << ((start % BITMAP_WORD_BITS))) - 1;
1341 first_mask = ~first_mask;
1342 }
1343 else
1344 {
1345 /* The first bit to turn off is below this start of this elt. */
1346 first_word_to_mod = 0;
1347 first_mask = 0;
1348 first_mask = ~first_mask;
1349 }
1350
1351 if (elt_end_bit_plus1 <= end_bit_plus1)
1352 {
1353 /* The last bit to turn off is beyond this elt. */
1354 last_word_to_mod = BITMAP_ELEMENT_WORDS - 1;
1355 last_mask = 0;
1356 last_mask = ~last_mask;
1357 }
1358 else
1359 {
1360 /* The last bit to turn off is inside to this elt. */
1361 last_word_to_mod =
1362 (end_bit_plus1 - elt_start_bit) / BITMAP_WORD_BITS;
1363
1364 /* The last mask should have 1s below the end bit. */
1365 last_mask =
1366 (((BITMAP_WORD) 1) << (((end_bit_plus1) % BITMAP_WORD_BITS))) - 1;
1367 }
1368
1369
1370 if (first_word_to_mod == last_word_to_mod)
1371 {
1372 BITMAP_WORD mask = first_mask & last_mask;
1373 elt->bits[first_word_to_mod] &= ~mask;
1374 }
1375 else
1376 {
1377 elt->bits[first_word_to_mod] &= ~first_mask;
1378 if (BITMAP_ELEMENT_WORDS > 2)
1379 for (i = first_word_to_mod + 1; i < last_word_to_mod; i++)
1380 elt->bits[i] = 0;
1381 elt->bits[last_word_to_mod] &= ~last_mask;
1382 }
1383 for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
1384 if (elt->bits[i])
1385 {
1386 clear = false;
1387 break;
1388 }
1389 /* Check to see if there are any bits left. */
1390 if (clear)
1391 bitmap_element_free (head, elt);
1392 }
1393 elt = next_elt;
1394 }
1395
1396 if (elt)
1397 {
1398 head->current = elt;
1399 head->indx = head->current->indx;
1400 }
1401 }
1402
1403 /* A = ~A & B. */
1404
1405 void
1406 bitmap_compl_and_into (bitmap a, const_bitmap b)
1407 {
1408 bitmap_element *a_elt = a->first;
1409 const bitmap_element *b_elt = b->first;
1410 bitmap_element *a_prev = NULL;
1411 bitmap_element *next;
1412
1413 gcc_assert (a != b);
1414
1415 if (bitmap_empty_p (a))
1416 {
1417 bitmap_copy (a, b);
1418 return;
1419 }
1420 if (bitmap_empty_p (b))
1421 {
1422 bitmap_clear (a);
1423 return;
1424 }
1425
1426 while (a_elt || b_elt)
1427 {
1428 if (!b_elt || (a_elt && a_elt->indx < b_elt->indx))
1429 {
1430 /* A is before B. Remove A */
1431 next = a_elt->next;
1432 a_prev = a_elt->prev;
1433 bitmap_element_free (a, a_elt);
1434 a_elt = next;
1435 }
1436 else if (!a_elt || b_elt->indx < a_elt->indx)
1437 {
1438 /* B is before A. Copy B. */
1439 next = bitmap_elt_insert_after (a, a_prev, b_elt->indx);
1440 memcpy (next->bits, b_elt->bits, sizeof (next->bits));
1441 a_prev = next;
1442 b_elt = b_elt->next;
1443 }
1444 else
1445 {
1446 /* Matching elts, generate A = ~A & B. */
1447 unsigned ix;
1448 BITMAP_WORD ior = 0;
1449
1450 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1451 {
1452 BITMAP_WORD cleared = a_elt->bits[ix] & b_elt->bits[ix];
1453 BITMAP_WORD r = b_elt->bits[ix] ^ cleared;
1454
1455 a_elt->bits[ix] = r;
1456 ior |= r;
1457 }
1458 next = a_elt->next;
1459 if (!ior)
1460 bitmap_element_free (a, a_elt);
1461 else
1462 a_prev = a_elt;
1463 a_elt = next;
1464 b_elt = b_elt->next;
1465 }
1466 }
1467 gcc_checking_assert (!a->current == !a->first
1468 && (!a->current || a->indx == a->current->indx));
1469 return;
1470 }
1471
1472
1473 /* Insert an element corresponding to A_ELT | B_ELT after DST_PREV,
1474 overwriting DST_ELT if non-NULL. CHANGED is true if the destination bitmap
1475 had already been changed; the new value of CHANGED is returned. */
1476
1477 static inline bool
1478 bitmap_elt_ior (bitmap dst, bitmap_element *dst_elt, bitmap_element *dst_prev,
1479 const bitmap_element *a_elt, const bitmap_element *b_elt,
1480 bool changed)
1481 {
1482 gcc_assert (a_elt || b_elt);
1483
1484 if (a_elt && b_elt && a_elt->indx == b_elt->indx)
1485 {
1486 /* Matching elts, generate A | B. */
1487 unsigned ix;
1488
1489 if (!changed && dst_elt && dst_elt->indx == a_elt->indx)
1490 {
1491 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1492 {
1493 BITMAP_WORD r = a_elt->bits[ix] | b_elt->bits[ix];
1494 if (r != dst_elt->bits[ix])
1495 {
1496 dst_elt->bits[ix] = r;
1497 changed = true;
1498 }
1499 }
1500 }
1501 else
1502 {
1503 changed = true;
1504 if (!dst_elt)
1505 dst_elt = bitmap_elt_insert_after (dst, dst_prev, a_elt->indx);
1506 else
1507 dst_elt->indx = a_elt->indx;
1508 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1509 {
1510 BITMAP_WORD r = a_elt->bits[ix] | b_elt->bits[ix];
1511 dst_elt->bits[ix] = r;
1512 }
1513 }
1514 }
1515 else
1516 {
1517 /* Copy a single element. */
1518 const bitmap_element *src;
1519
1520 if (!b_elt || (a_elt && a_elt->indx < b_elt->indx))
1521 src = a_elt;
1522 else
1523 src = b_elt;
1524
1525 gcc_checking_assert (src);
1526 changed = bitmap_elt_copy (dst, dst_elt, dst_prev, src, changed);
1527 }
1528 return changed;
1529 }
1530
1531
1532 /* DST = A | B. Return true if DST changes. */
1533
1534 bool
1535 bitmap_ior (bitmap dst, const_bitmap a, const_bitmap b)
1536 {
1537 bitmap_element *dst_elt = dst->first;
1538 const bitmap_element *a_elt = a->first;
1539 const bitmap_element *b_elt = b->first;
1540 bitmap_element *dst_prev = NULL;
1541 bitmap_element **dst_prev_pnext = &dst->first;
1542 bool changed = false;
1543
1544 gcc_assert (dst != a && dst != b);
1545
1546 while (a_elt || b_elt)
1547 {
1548 changed = bitmap_elt_ior (dst, dst_elt, dst_prev, a_elt, b_elt, changed);
1549
1550 if (a_elt && b_elt && a_elt->indx == b_elt->indx)
1551 {
1552 a_elt = a_elt->next;
1553 b_elt = b_elt->next;
1554 }
1555 else
1556 {
1557 if (a_elt && (!b_elt || a_elt->indx <= b_elt->indx))
1558 a_elt = a_elt->next;
1559 else if (b_elt && (!a_elt || b_elt->indx <= a_elt->indx))
1560 b_elt = b_elt->next;
1561 }
1562
1563 dst_prev = *dst_prev_pnext;
1564 dst_prev_pnext = &dst_prev->next;
1565 dst_elt = *dst_prev_pnext;
1566 }
1567
1568 if (dst_elt)
1569 {
1570 changed = true;
1571 bitmap_elt_clear_from (dst, dst_elt);
1572 }
1573 gcc_checking_assert (!dst->current == !dst->first);
1574 if (dst->current)
1575 dst->indx = dst->current->indx;
1576 return changed;
1577 }
1578
1579 /* A |= B. Return true if A changes. */
1580
1581 bool
1582 bitmap_ior_into (bitmap a, const_bitmap b)
1583 {
1584 bitmap_element *a_elt = a->first;
1585 const bitmap_element *b_elt = b->first;
1586 bitmap_element *a_prev = NULL;
1587 bitmap_element **a_prev_pnext = &a->first;
1588 bool changed = false;
1589
1590 if (a == b)
1591 return false;
1592
1593 while (b_elt)
1594 {
1595 /* If A lags behind B, just advance it. */
1596 if (!a_elt || a_elt->indx == b_elt->indx)
1597 {
1598 changed = bitmap_elt_ior (a, a_elt, a_prev, a_elt, b_elt, changed);
1599 b_elt = b_elt->next;
1600 }
1601 else if (a_elt->indx > b_elt->indx)
1602 {
1603 changed = bitmap_elt_copy (a, NULL, a_prev, b_elt, changed);
1604 b_elt = b_elt->next;
1605 }
1606
1607 a_prev = *a_prev_pnext;
1608 a_prev_pnext = &a_prev->next;
1609 a_elt = *a_prev_pnext;
1610 }
1611
1612 gcc_checking_assert (!a->current == !a->first);
1613 if (a->current)
1614 a->indx = a->current->indx;
1615 return changed;
1616 }
1617
1618 /* DST = A ^ B */
1619
1620 void
1621 bitmap_xor (bitmap dst, const_bitmap a, const_bitmap b)
1622 {
1623 bitmap_element *dst_elt = dst->first;
1624 const bitmap_element *a_elt = a->first;
1625 const bitmap_element *b_elt = b->first;
1626 bitmap_element *dst_prev = NULL;
1627
1628 gcc_assert (dst != a && dst != b);
1629 if (a == b)
1630 {
1631 bitmap_clear (dst);
1632 return;
1633 }
1634
1635 while (a_elt || b_elt)
1636 {
1637 if (a_elt && b_elt && a_elt->indx == b_elt->indx)
1638 {
1639 /* Matching elts, generate A ^ B. */
1640 unsigned ix;
1641 BITMAP_WORD ior = 0;
1642
1643 if (!dst_elt)
1644 dst_elt = bitmap_elt_insert_after (dst, dst_prev, a_elt->indx);
1645 else
1646 dst_elt->indx = a_elt->indx;
1647 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1648 {
1649 BITMAP_WORD r = a_elt->bits[ix] ^ b_elt->bits[ix];
1650
1651 ior |= r;
1652 dst_elt->bits[ix] = r;
1653 }
1654 a_elt = a_elt->next;
1655 b_elt = b_elt->next;
1656 if (ior)
1657 {
1658 dst_prev = dst_elt;
1659 dst_elt = dst_elt->next;
1660 }
1661 }
1662 else
1663 {
1664 /* Copy a single element. */
1665 const bitmap_element *src;
1666
1667 if (!b_elt || (a_elt && a_elt->indx < b_elt->indx))
1668 {
1669 src = a_elt;
1670 a_elt = a_elt->next;
1671 }
1672 else
1673 {
1674 src = b_elt;
1675 b_elt = b_elt->next;
1676 }
1677
1678 if (!dst_elt)
1679 dst_elt = bitmap_elt_insert_after (dst, dst_prev, src->indx);
1680 else
1681 dst_elt->indx = src->indx;
1682 memcpy (dst_elt->bits, src->bits, sizeof (dst_elt->bits));
1683 dst_prev = dst_elt;
1684 dst_elt = dst_elt->next;
1685 }
1686 }
1687 /* Ensure that dst->current is valid. */
1688 dst->current = dst->first;
1689 bitmap_elt_clear_from (dst, dst_elt);
1690 gcc_checking_assert (!dst->current == !dst->first);
1691 if (dst->current)
1692 dst->indx = dst->current->indx;
1693 }
1694
1695 /* A ^= B */
1696
1697 void
1698 bitmap_xor_into (bitmap a, const_bitmap b)
1699 {
1700 bitmap_element *a_elt = a->first;
1701 const bitmap_element *b_elt = b->first;
1702 bitmap_element *a_prev = NULL;
1703
1704 if (a == b)
1705 {
1706 bitmap_clear (a);
1707 return;
1708 }
1709
1710 while (b_elt)
1711 {
1712 if (!a_elt || b_elt->indx < a_elt->indx)
1713 {
1714 /* Copy b_elt. */
1715 bitmap_element *dst = bitmap_elt_insert_after (a, a_prev, b_elt->indx);
1716 memcpy (dst->bits, b_elt->bits, sizeof (dst->bits));
1717 a_prev = dst;
1718 b_elt = b_elt->next;
1719 }
1720 else if (a_elt->indx < b_elt->indx)
1721 {
1722 a_prev = a_elt;
1723 a_elt = a_elt->next;
1724 }
1725 else
1726 {
1727 /* Matching elts, generate A ^= B. */
1728 unsigned ix;
1729 BITMAP_WORD ior = 0;
1730 bitmap_element *next = a_elt->next;
1731
1732 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1733 {
1734 BITMAP_WORD r = a_elt->bits[ix] ^ b_elt->bits[ix];
1735
1736 ior |= r;
1737 a_elt->bits[ix] = r;
1738 }
1739 b_elt = b_elt->next;
1740 if (ior)
1741 a_prev = a_elt;
1742 else
1743 bitmap_element_free (a, a_elt);
1744 a_elt = next;
1745 }
1746 }
1747 gcc_checking_assert (!a->current == !a->first);
1748 if (a->current)
1749 a->indx = a->current->indx;
1750 }
1751
1752 /* Return true if two bitmaps are identical.
1753 We do not bother with a check for pointer equality, as that never
1754 occurs in practice. */
1755
1756 bool
1757 bitmap_equal_p (const_bitmap a, const_bitmap b)
1758 {
1759 const bitmap_element *a_elt;
1760 const bitmap_element *b_elt;
1761 unsigned ix;
1762
1763 for (a_elt = a->first, b_elt = b->first;
1764 a_elt && b_elt;
1765 a_elt = a_elt->next, b_elt = b_elt->next)
1766 {
1767 if (a_elt->indx != b_elt->indx)
1768 return false;
1769 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1770 if (a_elt->bits[ix] != b_elt->bits[ix])
1771 return false;
1772 }
1773 return !a_elt && !b_elt;
1774 }
1775
1776 /* Return true if A AND B is not empty. */
1777
1778 bool
1779 bitmap_intersect_p (const_bitmap a, const_bitmap b)
1780 {
1781 const bitmap_element *a_elt;
1782 const bitmap_element *b_elt;
1783 unsigned ix;
1784
1785 for (a_elt = a->first, b_elt = b->first;
1786 a_elt && b_elt;)
1787 {
1788 if (a_elt->indx < b_elt->indx)
1789 a_elt = a_elt->next;
1790 else if (b_elt->indx < a_elt->indx)
1791 b_elt = b_elt->next;
1792 else
1793 {
1794 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1795 if (a_elt->bits[ix] & b_elt->bits[ix])
1796 return true;
1797 a_elt = a_elt->next;
1798 b_elt = b_elt->next;
1799 }
1800 }
1801 return false;
1802 }
1803
1804 /* Return true if A AND NOT B is not empty. */
1805
1806 bool
1807 bitmap_intersect_compl_p (const_bitmap a, const_bitmap b)
1808 {
1809 const bitmap_element *a_elt;
1810 const bitmap_element *b_elt;
1811 unsigned ix;
1812 for (a_elt = a->first, b_elt = b->first;
1813 a_elt && b_elt;)
1814 {
1815 if (a_elt->indx < b_elt->indx)
1816 return true;
1817 else if (b_elt->indx < a_elt->indx)
1818 b_elt = b_elt->next;
1819 else
1820 {
1821 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1822 if (a_elt->bits[ix] & ~b_elt->bits[ix])
1823 return true;
1824 a_elt = a_elt->next;
1825 b_elt = b_elt->next;
1826 }
1827 }
1828 return a_elt != NULL;
1829 }
1830
1831 \f
1832 /* DST = A | (FROM1 & ~FROM2). Return true if DST changes. */
1833
1834 bool
1835 bitmap_ior_and_compl (bitmap dst, const_bitmap a, const_bitmap b, const_bitmap kill)
1836 {
1837 bool changed = false;
1838
1839 bitmap_element *dst_elt = dst->first;
1840 const bitmap_element *a_elt = a->first;
1841 const bitmap_element *b_elt = b->first;
1842 const bitmap_element *kill_elt = kill->first;
1843 bitmap_element *dst_prev = NULL;
1844 bitmap_element **dst_prev_pnext = &dst->first;
1845
1846 gcc_assert (dst != a && dst != b && dst != kill);
1847
1848 /* Special cases. We don't bother checking for bitmap_equal_p (b, kill). */
1849 if (b == kill || bitmap_empty_p (b))
1850 {
1851 changed = !bitmap_equal_p (dst, a);
1852 if (changed)
1853 bitmap_copy (dst, a);
1854 return changed;
1855 }
1856 if (bitmap_empty_p (kill))
1857 return bitmap_ior (dst, a, b);
1858 if (bitmap_empty_p (a))
1859 return bitmap_and_compl (dst, b, kill);
1860
1861 while (a_elt || b_elt)
1862 {
1863 bool new_element = false;
1864
1865 if (b_elt)
1866 while (kill_elt && kill_elt->indx < b_elt->indx)
1867 kill_elt = kill_elt->next;
1868
1869 if (b_elt && kill_elt && kill_elt->indx == b_elt->indx
1870 && (!a_elt || a_elt->indx >= b_elt->indx))
1871 {
1872 bitmap_element tmp_elt;
1873 unsigned ix;
1874
1875 BITMAP_WORD ior = 0;
1876 tmp_elt.indx = b_elt->indx;
1877 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1878 {
1879 BITMAP_WORD r = b_elt->bits[ix] & ~kill_elt->bits[ix];
1880 ior |= r;
1881 tmp_elt.bits[ix] = r;
1882 }
1883
1884 if (ior)
1885 {
1886 changed = bitmap_elt_ior (dst, dst_elt, dst_prev,
1887 a_elt, &tmp_elt, changed);
1888 new_element = true;
1889 if (a_elt && a_elt->indx == b_elt->indx)
1890 a_elt = a_elt->next;
1891 }
1892
1893 b_elt = b_elt->next;
1894 kill_elt = kill_elt->next;
1895 }
1896 else
1897 {
1898 changed = bitmap_elt_ior (dst, dst_elt, dst_prev,
1899 a_elt, b_elt, changed);
1900 new_element = true;
1901
1902 if (a_elt && b_elt && a_elt->indx == b_elt->indx)
1903 {
1904 a_elt = a_elt->next;
1905 b_elt = b_elt->next;
1906 }
1907 else
1908 {
1909 if (a_elt && (!b_elt || a_elt->indx <= b_elt->indx))
1910 a_elt = a_elt->next;
1911 else if (b_elt && (!a_elt || b_elt->indx <= a_elt->indx))
1912 b_elt = b_elt->next;
1913 }
1914 }
1915
1916 if (new_element)
1917 {
1918 dst_prev = *dst_prev_pnext;
1919 dst_prev_pnext = &dst_prev->next;
1920 dst_elt = *dst_prev_pnext;
1921 }
1922 }
1923
1924 if (dst_elt)
1925 {
1926 changed = true;
1927 bitmap_elt_clear_from (dst, dst_elt);
1928 }
1929 gcc_checking_assert (!dst->current == !dst->first);
1930 if (dst->current)
1931 dst->indx = dst->current->indx;
1932
1933 return changed;
1934 }
1935
1936 /* A |= (FROM1 & ~FROM2). Return true if A changes. */
1937
1938 bool
1939 bitmap_ior_and_compl_into (bitmap a, const_bitmap from1, const_bitmap from2)
1940 {
1941 bitmap_head tmp;
1942 bool changed;
1943
1944 bitmap_initialize (&tmp, &bitmap_default_obstack);
1945 bitmap_and_compl (&tmp, from1, from2);
1946 changed = bitmap_ior_into (a, &tmp);
1947 bitmap_clear (&tmp);
1948
1949 return changed;
1950 }
1951
1952 /* A |= (B & C). Return true if A changes. */
1953
1954 bool
1955 bitmap_ior_and_into (bitmap a, const_bitmap b, const_bitmap c)
1956 {
1957 bitmap_element *a_elt = a->first;
1958 const bitmap_element *b_elt = b->first;
1959 const bitmap_element *c_elt = c->first;
1960 bitmap_element and_elt;
1961 bitmap_element *a_prev = NULL;
1962 bitmap_element **a_prev_pnext = &a->first;
1963 bool changed = false;
1964 unsigned ix;
1965
1966 if (b == c)
1967 return bitmap_ior_into (a, b);
1968 if (bitmap_empty_p (b) || bitmap_empty_p (c))
1969 return false;
1970
1971 and_elt.indx = -1;
1972 while (b_elt && c_elt)
1973 {
1974 BITMAP_WORD overall;
1975
1976 /* Find a common item of B and C. */
1977 while (b_elt->indx != c_elt->indx)
1978 {
1979 if (b_elt->indx < c_elt->indx)
1980 {
1981 b_elt = b_elt->next;
1982 if (!b_elt)
1983 goto done;
1984 }
1985 else
1986 {
1987 c_elt = c_elt->next;
1988 if (!c_elt)
1989 goto done;
1990 }
1991 }
1992
1993 overall = 0;
1994 and_elt.indx = b_elt->indx;
1995 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1996 {
1997 and_elt.bits[ix] = b_elt->bits[ix] & c_elt->bits[ix];
1998 overall |= and_elt.bits[ix];
1999 }
2000
2001 b_elt = b_elt->next;
2002 c_elt = c_elt->next;
2003 if (!overall)
2004 continue;
2005
2006 /* Now find a place to insert AND_ELT. */
2007 do
2008 {
2009 ix = a_elt ? a_elt->indx : and_elt.indx;
2010 if (ix == and_elt.indx)
2011 changed = bitmap_elt_ior (a, a_elt, a_prev, a_elt, &and_elt, changed);
2012 else if (ix > and_elt.indx)
2013 changed = bitmap_elt_copy (a, NULL, a_prev, &and_elt, changed);
2014
2015 a_prev = *a_prev_pnext;
2016 a_prev_pnext = &a_prev->next;
2017 a_elt = *a_prev_pnext;
2018
2019 /* If A lagged behind B/C, we advanced it so loop once more. */
2020 }
2021 while (ix < and_elt.indx);
2022 }
2023
2024 done:
2025 gcc_checking_assert (!a->current == !a->first);
2026 if (a->current)
2027 a->indx = a->current->indx;
2028 return changed;
2029 }
2030
2031 /* Compute hash of bitmap (for purposes of hashing). */
2032 hashval_t
2033 bitmap_hash (const_bitmap head)
2034 {
2035 const bitmap_element *ptr;
2036 BITMAP_WORD hash = 0;
2037 int ix;
2038
2039 for (ptr = head->first; ptr; ptr = ptr->next)
2040 {
2041 hash ^= ptr->indx;
2042 for (ix = 0; ix != BITMAP_ELEMENT_WORDS; ix++)
2043 hash ^= ptr->bits[ix];
2044 }
2045 return (hashval_t)hash;
2046 }
2047
2048 \f
2049 /* Debugging function to print out the contents of a bitmap. */
2050
2051 DEBUG_FUNCTION void
2052 debug_bitmap_file (FILE *file, const_bitmap head)
2053 {
2054 const bitmap_element *ptr;
2055
2056 fprintf (file, "\nfirst = " HOST_PTR_PRINTF
2057 " current = " HOST_PTR_PRINTF " indx = %u\n",
2058 (void *) head->first, (void *) head->current, head->indx);
2059
2060 for (ptr = head->first; ptr; ptr = ptr->next)
2061 {
2062 unsigned int i, j, col = 26;
2063
2064 fprintf (file, "\t" HOST_PTR_PRINTF " next = " HOST_PTR_PRINTF
2065 " prev = " HOST_PTR_PRINTF " indx = %u\n\t\tbits = {",
2066 (const void*) ptr, (const void*) ptr->next,
2067 (const void*) ptr->prev, ptr->indx);
2068
2069 for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
2070 for (j = 0; j < BITMAP_WORD_BITS; j++)
2071 if ((ptr->bits[i] >> j) & 1)
2072 {
2073 if (col > 70)
2074 {
2075 fprintf (file, "\n\t\t\t");
2076 col = 24;
2077 }
2078
2079 fprintf (file, " %u", (ptr->indx * BITMAP_ELEMENT_ALL_BITS
2080 + i * BITMAP_WORD_BITS + j));
2081 col += 4;
2082 }
2083
2084 fprintf (file, " }\n");
2085 }
2086 }
2087
2088 /* Function to be called from the debugger to print the contents
2089 of a bitmap. */
2090
2091 DEBUG_FUNCTION void
2092 debug_bitmap (const_bitmap head)
2093 {
2094 debug_bitmap_file (stdout, head);
2095 }
2096
2097 /* Function to print out the contents of a bitmap. Unlike debug_bitmap_file,
2098 it does not print anything but the bits. */
2099
2100 DEBUG_FUNCTION void
2101 bitmap_print (FILE *file, const_bitmap head, const char *prefix, const char *suffix)
2102 {
2103 const char *comma = "";
2104 unsigned i;
2105 bitmap_iterator bi;
2106
2107 fputs (prefix, file);
2108 EXECUTE_IF_SET_IN_BITMAP (head, 0, i, bi)
2109 {
2110 fprintf (file, "%s%d", comma, i);
2111 comma = ", ";
2112 }
2113 fputs (suffix, file);
2114 }
2115
2116
2117 /* Used to accumulate statistics about bitmap sizes. */
2118 struct output_info
2119 {
2120 HOST_WIDEST_INT size;
2121 int count;
2122 };
2123
2124 /* Called via htab_traverse. Output bitmap descriptor pointed out by SLOT
2125 and update statistics. */
2126 static int
2127 print_statistics (void **slot, void *b)
2128 {
2129 struct bitmap_descriptor *d = (struct bitmap_descriptor *) *slot;
2130 struct output_info *i = (struct output_info *) b;
2131 char s[4096];
2132
2133 if (d->allocated)
2134 {
2135 const char *s1 = d->file;
2136 const char *s2;
2137 while ((s2 = strstr (s1, "gcc/")))
2138 s1 = s2 + 4;
2139 sprintf (s, "%s:%i (%s)", s1, d->line, d->function);
2140 s[41] = 0;
2141 fprintf (stderr, "%-41s %8d %15"HOST_WIDEST_INT_PRINT"d %15"
2142 HOST_WIDEST_INT_PRINT"d %15"HOST_WIDEST_INT_PRINT"d %10d %10d\n",
2143 s, d->created, d->allocated, d->peak, d->current, d->nsearches,
2144 d->search_iter);
2145 i->size += d->allocated;
2146 i->count += d->created;
2147 }
2148 return 1;
2149 }
2150
2151 /* Output per-bitmap memory usage statistics. */
2152 void
2153 dump_bitmap_statistics (void)
2154 {
2155 struct output_info info;
2156
2157 if (! GATHER_STATISTICS)
2158 return;
2159
2160 if (!bitmap_desc_hash)
2161 return;
2162
2163 fprintf (stderr, "\nBitmap Overall "
2164 " Allocated Peak Leak searched "
2165 " search itr\n");
2166 fprintf (stderr, "---------------------------------------------------------------------------------\n");
2167 info.count = 0;
2168 info.size = 0;
2169 htab_traverse (bitmap_desc_hash, print_statistics, &info);
2170 fprintf (stderr, "---------------------------------------------------------------------------------\n");
2171 fprintf (stderr, "%-40s %9d %15"HOST_WIDEST_INT_PRINT"d\n",
2172 "Total", info.count, info.size);
2173 fprintf (stderr, "---------------------------------------------------------------------------------\n");
2174 }
2175
2176 #include "gt-bitmap.h"