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