]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/bitmap.c
Update copyright years.
[thirdparty/gcc.git] / gcc / bitmap.c
1 /* Functions to support general ended bitmaps.
2 Copyright (C) 1997-2021 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 #include "selftest.h"
25
26 /* Memory allocation statistics purpose instance. */
27 mem_alloc_description<bitmap_usage> bitmap_mem_desc;
28
29 /* Static zero-initialized bitmap obstack used for default initialization
30 of bitmap_head. */
31 bitmap_obstack bitmap_head::crashme;
32
33 static bitmap_element *bitmap_tree_listify_from (bitmap, bitmap_element *);
34
35 /* Register new bitmap. */
36 void
37 bitmap_register (bitmap b MEM_STAT_DECL)
38 {
39 static unsigned alloc_descriptor_max_uid = 1;
40 gcc_assert (b->alloc_descriptor == 0);
41 b->alloc_descriptor = alloc_descriptor_max_uid++;
42
43 bitmap_mem_desc.register_descriptor (b->get_descriptor (), BITMAP_ORIGIN,
44 false FINAL_PASS_MEM_STAT);
45 }
46
47 /* Account the overhead. */
48 static void
49 register_overhead (bitmap b, size_t amount)
50 {
51 unsigned *d = b->get_descriptor ();
52 if (bitmap_mem_desc.contains_descriptor_for_instance (d))
53 bitmap_mem_desc.register_instance_overhead (amount, d);
54 }
55
56 /* Release the overhead. */
57 static void
58 release_overhead (bitmap b, size_t amount, bool remove_from_map)
59 {
60 unsigned *d = b->get_descriptor ();
61 if (bitmap_mem_desc.contains_descriptor_for_instance (d))
62 bitmap_mem_desc.release_instance_overhead (d, amount, remove_from_map);
63 }
64
65
66 /* Global data */
67 bitmap_element bitmap_zero_bits; /* An element of all zero bits. */
68 bitmap_obstack bitmap_default_obstack; /* The default bitmap obstack. */
69 static int bitmap_default_obstack_depth;
70 static GTY((deletable)) bitmap_element *bitmap_ggc_free; /* Freelist of
71 GC'd elements. */
72
73 \f
74 /* Bitmap memory management. */
75
76 /* Add ELT to the appropriate freelist. */
77 static inline void
78 bitmap_elem_to_freelist (bitmap head, bitmap_element *elt)
79 {
80 bitmap_obstack *bit_obstack = head->obstack;
81
82 if (GATHER_STATISTICS)
83 release_overhead (head, sizeof (bitmap_element), false);
84
85 elt->next = NULL;
86 elt->indx = -1;
87 if (bit_obstack)
88 {
89 elt->prev = bit_obstack->elements;
90 bit_obstack->elements = elt;
91 }
92 else
93 {
94 elt->prev = bitmap_ggc_free;
95 bitmap_ggc_free = elt;
96 }
97 }
98
99 /* Allocate a bitmap element. The bits are cleared, but nothing else is. */
100
101 static inline bitmap_element *
102 bitmap_element_allocate (bitmap head)
103 {
104 bitmap_element *element;
105 bitmap_obstack *bit_obstack = head->obstack;
106
107 if (bit_obstack)
108 {
109 element = bit_obstack->elements;
110
111 if (element)
112 /* Use up the inner list first before looking at the next
113 element of the outer list. */
114 if (element->next)
115 {
116 bit_obstack->elements = element->next;
117 bit_obstack->elements->prev = element->prev;
118 }
119 else
120 /* Inner list was just a singleton. */
121 bit_obstack->elements = element->prev;
122 else
123 element = XOBNEW (&bit_obstack->obstack, bitmap_element);
124 }
125 else
126 {
127 element = bitmap_ggc_free;
128 if (element)
129 /* Use up the inner list first before looking at the next
130 element of the outer list. */
131 if (element->next)
132 {
133 bitmap_ggc_free = element->next;
134 bitmap_ggc_free->prev = element->prev;
135 }
136 else
137 /* Inner list was just a singleton. */
138 bitmap_ggc_free = element->prev;
139 else
140 element = ggc_alloc<bitmap_element> ();
141 }
142
143 if (GATHER_STATISTICS)
144 register_overhead (head, sizeof (bitmap_element));
145
146 memset (element->bits, 0, sizeof (element->bits));
147
148 return element;
149 }
150
151 /* Remove ELT and all following elements from bitmap HEAD.
152 Put the released elements in the freelist for HEAD. */
153
154 void
155 bitmap_elt_clear_from (bitmap head, bitmap_element *elt)
156 {
157 bitmap_element *prev;
158 bitmap_obstack *bit_obstack = head->obstack;
159
160 if (!elt)
161 return;
162
163 if (head->tree_form)
164 elt = bitmap_tree_listify_from (head, elt);
165
166 if (GATHER_STATISTICS)
167 {
168 int n = 0;
169 for (prev = elt; prev; prev = prev->next)
170 n++;
171 release_overhead (head, sizeof (bitmap_element) * n, false);
172 }
173
174 prev = elt->prev;
175 if (prev)
176 {
177 prev->next = NULL;
178 if (head->current->indx > prev->indx)
179 {
180 head->current = prev;
181 head->indx = prev->indx;
182 }
183 }
184 else
185 {
186 head->first = NULL;
187 head->current = NULL;
188 head->indx = 0;
189 }
190
191 /* Put the entire list onto the freelist in one operation. */
192 if (bit_obstack)
193 {
194 elt->prev = bit_obstack->elements;
195 bit_obstack->elements = elt;
196 }
197 else
198 {
199 elt->prev = bitmap_ggc_free;
200 bitmap_ggc_free = elt;
201 }
202 }
203 \f
204 /* Linked-list view of bitmaps.
205
206 In this representation, the bitmap elements form a double-linked list
207 with elements sorted by increasing index. */
208
209 /* Link the bitmap element into the current bitmap linked list. */
210
211 static inline void
212 bitmap_list_link_element (bitmap head, bitmap_element *element)
213 {
214 unsigned int indx = element->indx;
215 bitmap_element *ptr;
216
217 gcc_checking_assert (!head->tree_form);
218
219 /* If this is the first and only element, set it in. */
220 if (head->first == 0)
221 {
222 element->next = element->prev = 0;
223 head->first = element;
224 }
225
226 /* If this index is less than that of the current element, it goes someplace
227 before the current element. */
228 else if (indx < head->indx)
229 {
230 for (ptr = head->current;
231 ptr->prev != 0 && ptr->prev->indx > indx;
232 ptr = ptr->prev)
233 ;
234
235 if (ptr->prev)
236 ptr->prev->next = element;
237 else
238 head->first = element;
239
240 element->prev = ptr->prev;
241 element->next = ptr;
242 ptr->prev = element;
243 }
244
245 /* Otherwise, it must go someplace after the current element. */
246 else
247 {
248 for (ptr = head->current;
249 ptr->next != 0 && ptr->next->indx < indx;
250 ptr = ptr->next)
251 ;
252
253 if (ptr->next)
254 ptr->next->prev = element;
255
256 element->next = ptr->next;
257 element->prev = ptr;
258 ptr->next = element;
259 }
260
261 /* Set up so this is the first element searched. */
262 head->current = element;
263 head->indx = indx;
264 }
265
266 /* Unlink the bitmap element from the current bitmap linked list,
267 and return it to the freelist. */
268
269 static inline void
270 bitmap_list_unlink_element (bitmap head, bitmap_element *element,
271 bool to_freelist = true)
272 {
273 bitmap_element *next = element->next;
274 bitmap_element *prev = element->prev;
275
276 gcc_checking_assert (!head->tree_form);
277
278 if (prev)
279 prev->next = next;
280
281 if (next)
282 next->prev = prev;
283
284 if (head->first == element)
285 head->first = next;
286
287 /* Since the first thing we try is to insert before current,
288 make current the next entry in preference to the previous. */
289 if (head->current == element)
290 {
291 head->current = next != 0 ? next : prev;
292 if (head->current)
293 head->indx = head->current->indx;
294 else
295 head->indx = 0;
296 }
297
298 if (to_freelist)
299 bitmap_elem_to_freelist (head, element);
300 }
301
302 /* Insert a new uninitialized element (or NODE if not NULL) into bitmap
303 HEAD after element ELT. If ELT is NULL, insert the element at the start.
304 Return the new element. */
305
306 static bitmap_element *
307 bitmap_list_insert_element_after (bitmap head,
308 bitmap_element *elt, unsigned int indx,
309 bitmap_element *node = NULL)
310 {
311 if (!node)
312 node = bitmap_element_allocate (head);
313 node->indx = indx;
314
315 gcc_checking_assert (!head->tree_form);
316
317 if (!elt)
318 {
319 if (!head->current)
320 {
321 head->current = node;
322 head->indx = indx;
323 }
324 node->next = head->first;
325 if (node->next)
326 node->next->prev = node;
327 head->first = node;
328 node->prev = NULL;
329 }
330 else
331 {
332 gcc_checking_assert (head->current);
333 node->next = elt->next;
334 if (node->next)
335 node->next->prev = node;
336 elt->next = node;
337 node->prev = elt;
338 }
339 return node;
340 }
341
342 /* Return the element for INDX, or NULL if the element doesn't exist.
343 Update the `current' field even if we can't find an element that
344 would hold the bitmap's bit to make eventual allocation
345 faster. */
346
347 static inline bitmap_element *
348 bitmap_list_find_element (bitmap head, unsigned int indx)
349 {
350 bitmap_element *element;
351
352 if (head->current == NULL
353 || head->indx == indx)
354 return head->current;
355
356 if (head->current == head->first
357 && head->first->next == NULL)
358 return NULL;
359
360 /* Usage can be NULL due to allocated bitmaps for which we do not
361 call initialize function. */
362 bitmap_usage *usage = NULL;
363 if (GATHER_STATISTICS)
364 usage = bitmap_mem_desc.get_descriptor_for_instance (head);
365
366 /* This bitmap has more than one element, and we're going to look
367 through the elements list. Count that as a search. */
368 if (GATHER_STATISTICS && usage)
369 usage->m_nsearches++;
370
371 if (head->indx < indx)
372 /* INDX is beyond head->indx. Search from head->current
373 forward. */
374 for (element = head->current;
375 element->next != 0 && element->indx < indx;
376 element = element->next)
377 {
378 if (GATHER_STATISTICS && usage)
379 usage->m_search_iter++;
380 }
381
382 else if (head->indx / 2 < indx)
383 /* INDX is less than head->indx and closer to head->indx than to
384 0. Search from head->current backward. */
385 for (element = head->current;
386 element->prev != 0 && element->indx > indx;
387 element = element->prev)
388 {
389 if (GATHER_STATISTICS && usage)
390 usage->m_search_iter++;
391 }
392
393 else
394 /* INDX is less than head->indx and closer to 0 than to
395 head->indx. Search from head->first forward. */
396 for (element = head->first;
397 element->next != 0 && element->indx < indx;
398 element = element->next)
399 {
400 if (GATHER_STATISTICS && usage)
401 usage->m_search_iter++;
402 }
403
404 /* `element' is the nearest to the one we want. If it's not the one we
405 want, the one we want doesn't exist. */
406 gcc_checking_assert (element != NULL);
407 head->current = element;
408 head->indx = element->indx;
409 if (element->indx != indx)
410 element = 0;
411 return element;
412 }
413
414 \f
415 /* Splay-tree view of bitmaps.
416
417 This is an almost one-to-one the implementatin of the simple top-down
418 splay tree in Sleator and Tarjan's "Self-adjusting Binary Search Trees".
419 It is probably not the most efficient form of splay trees, but it should
420 be good enough to experiment with this idea of bitmaps-as-trees.
421
422 For all functions below, the variable or function argument "t" is a node
423 in the tree, and "e" is a temporary or new node in the tree. The rest
424 is sufficiently straigh-forward (and very well explained in the paper)
425 that comment would only clutter things. */
426
427 static inline void
428 bitmap_tree_link_left (bitmap_element * &t, bitmap_element * &l)
429 {
430 l->next = t;
431 l = t;
432 t = t->next;
433 }
434
435 static inline void
436 bitmap_tree_link_right (bitmap_element * &t, bitmap_element * &r)
437 {
438 r->prev = t;
439 r = t;
440 t = t->prev;
441 }
442
443 static inline void
444 bitmap_tree_rotate_left (bitmap_element * &t)
445 {
446 bitmap_element *e = t->next;
447 t->next = t->next->prev;
448 e->prev = t;
449 t = e;
450 }
451
452 static inline void
453 bitmap_tree_rotate_right (bitmap_element * &t)
454 {
455 bitmap_element *e = t->prev;
456 t->prev = t->prev->next;
457 e->next = t;
458 t = e;
459 }
460
461 static bitmap_element *
462 bitmap_tree_splay (bitmap head, bitmap_element *t, unsigned int indx)
463 {
464 bitmap_element N, *l, *r;
465
466 if (t == NULL)
467 return NULL;
468
469 bitmap_usage *usage = NULL;
470 if (GATHER_STATISTICS)
471 usage = bitmap_mem_desc.get_descriptor_for_instance (head);
472
473 N.prev = N.next = NULL;
474 l = r = &N;
475
476 while (indx != t->indx)
477 {
478 if (GATHER_STATISTICS && usage)
479 usage->m_search_iter++;
480
481 if (indx < t->indx)
482 {
483 if (t->prev != NULL && indx < t->prev->indx)
484 bitmap_tree_rotate_right (t);
485 if (t->prev == NULL)
486 break;
487 bitmap_tree_link_right (t, r);
488 }
489 else if (indx > t->indx)
490 {
491 if (t->next != NULL && indx > t->next->indx)
492 bitmap_tree_rotate_left (t);
493 if (t->next == NULL)
494 break;
495 bitmap_tree_link_left (t, l);
496 }
497 }
498
499 l->next = t->prev;
500 r->prev = t->next;
501 t->prev = N.next;
502 t->next = N.prev;
503 return t;
504 }
505
506 /* Link bitmap element E into the current bitmap splay tree. */
507
508 static inline void
509 bitmap_tree_link_element (bitmap head, bitmap_element *e)
510 {
511 if (head->first == NULL)
512 e->prev = e->next = NULL;
513 else
514 {
515 bitmap_element *t = bitmap_tree_splay (head, head->first, e->indx);
516 if (e->indx < t->indx)
517 {
518 e->prev = t->prev;
519 e->next = t;
520 t->prev = NULL;
521 }
522 else if (e->indx > t->indx)
523 {
524 e->next = t->next;
525 e->prev = t;
526 t->next = NULL;
527 }
528 else
529 gcc_unreachable ();
530 }
531 head->first = e;
532 head->current = e;
533 head->indx = e->indx;
534 }
535
536 /* Unlink bitmap element E from the current bitmap splay tree,
537 and return it to the freelist. */
538
539 static void
540 bitmap_tree_unlink_element (bitmap head, bitmap_element *e)
541 {
542 bitmap_element *t = bitmap_tree_splay (head, head->first, e->indx);
543
544 gcc_checking_assert (t == e);
545
546 if (e->prev == NULL)
547 t = e->next;
548 else
549 {
550 t = bitmap_tree_splay (head, e->prev, e->indx);
551 t->next = e->next;
552 }
553 head->first = t;
554 head->current = t;
555 head->indx = (t != NULL) ? t->indx : 0;
556
557 bitmap_elem_to_freelist (head, e);
558 }
559
560 /* Return the element for INDX, or NULL if the element doesn't exist. */
561
562 static inline bitmap_element *
563 bitmap_tree_find_element (bitmap head, unsigned int indx)
564 {
565 if (head->current == NULL
566 || head->indx == indx)
567 return head->current;
568
569 /* Usage can be NULL due to allocated bitmaps for which we do not
570 call initialize function. */
571 bitmap_usage *usage = NULL;
572 if (GATHER_STATISTICS)
573 usage = bitmap_mem_desc.get_descriptor_for_instance (head);
574
575 /* This bitmap has more than one element, and we're going to look
576 through the elements list. Count that as a search. */
577 if (GATHER_STATISTICS && usage)
578 usage->m_nsearches++;
579
580 bitmap_element *element = bitmap_tree_splay (head, head->first, indx);
581 gcc_checking_assert (element != NULL);
582 head->first = element;
583 head->current = element;
584 head->indx = element->indx;
585 if (element->indx != indx)
586 element = 0;
587 return element;
588 }
589 \f
590 /* Converting bitmap views from linked-list to tree and vice versa. */
591
592 /* Splice element E and all elements with a larger index from
593 bitmap HEAD, convert the spliced elements to the linked-list
594 view, and return the head of the list (which should be E again), */
595
596 static bitmap_element *
597 bitmap_tree_listify_from (bitmap head, bitmap_element *e)
598 {
599 bitmap_element *t, *erb;
600
601 /* Detach the right branch from E (all elements with indx > E->indx),
602 and splay E to the root. */
603 erb = e->next;
604 e->next = NULL;
605 t = bitmap_tree_splay (head, head->first, e->indx);
606 gcc_checking_assert (t == e);
607
608 /* Because E has no right branch, and we rotated it to the root,
609 the left branch is the new root. */
610 t = e->prev;
611 head->first = t;
612 head->current = t;
613 head->indx = (t != NULL) ? t->indx : 0;
614
615 /* Detach the tree from E, and re-attach the right branch of E. */
616 e->prev = NULL;
617 e->next = erb;
618
619 /* The tree is now valid again. Now we need to "un-tree" E.
620 It is imperative that a non-recursive implementation is used
621 for this, because splay trees have a worst case depth of O(N)
622 for a tree with N nodes. A recursive implementation could
623 result in a stack overflow for a sufficiently large, unbalanced
624 bitmap tree. */
625
626 auto_vec<bitmap_element *, 32> stack;
627 auto_vec<bitmap_element *, 32> sorted_elements;
628 bitmap_element *n = e;
629
630 while (true)
631 {
632 while (n != NULL)
633 {
634 stack.safe_push (n);
635 n = n->prev;
636 }
637
638 if (stack.is_empty ())
639 break;
640
641 n = stack.pop ();
642 sorted_elements.safe_push (n);
643 n = n->next;
644 }
645
646 gcc_assert (sorted_elements[0] == e);
647
648 bitmap_element *prev = NULL;
649 unsigned ix;
650 FOR_EACH_VEC_ELT (sorted_elements, ix, n)
651 {
652 if (prev != NULL)
653 prev->next = n;
654 n->prev = prev;
655 n->next = NULL;
656 prev = n;
657 }
658
659 return e;
660 }
661
662 /* Convert bitmap HEAD from splay-tree view to linked-list view. */
663
664 void
665 bitmap_list_view (bitmap head)
666 {
667 bitmap_element *ptr;
668
669 gcc_assert (head->tree_form);
670
671 ptr = head->first;
672 if (ptr)
673 {
674 while (ptr->prev)
675 bitmap_tree_rotate_right (ptr);
676 head->first = ptr;
677 head->first = bitmap_tree_listify_from (head, ptr);
678 }
679
680 head->tree_form = false;
681 if (!head->current)
682 {
683 head->current = head->first;
684 head->indx = head->current ? head->current->indx : 0;
685 }
686 }
687
688 /* Convert bitmap HEAD from linked-list view to splay-tree view.
689 This is simply a matter of dropping the prev or next pointers
690 and setting the tree_form flag. The tree will balance itself
691 if and when it is used. */
692
693 void
694 bitmap_tree_view (bitmap head)
695 {
696 bitmap_element *ptr;
697
698 gcc_assert (! head->tree_form);
699
700 ptr = head->first;
701 while (ptr)
702 {
703 ptr->prev = NULL;
704 ptr = ptr->next;
705 }
706
707 head->tree_form = true;
708 }
709 \f
710 /* Clear a bitmap by freeing all its elements. */
711
712 void
713 bitmap_clear (bitmap head)
714 {
715 if (head->first == NULL)
716 return;
717 if (head->tree_form)
718 {
719 bitmap_element *e, *t;
720 for (e = head->first; e->prev; e = e->prev)
721 /* Loop to find the element with the smallest index. */ ;
722 t = bitmap_tree_splay (head, head->first, e->indx);
723 gcc_checking_assert (t == e);
724 head->first = t;
725 }
726 bitmap_elt_clear_from (head, head->first);
727 }
728 \f
729 /* Initialize a bitmap obstack. If BIT_OBSTACK is NULL, initialize
730 the default bitmap obstack. */
731
732 void
733 bitmap_obstack_initialize (bitmap_obstack *bit_obstack)
734 {
735 if (!bit_obstack)
736 {
737 if (bitmap_default_obstack_depth++)
738 return;
739 bit_obstack = &bitmap_default_obstack;
740 }
741
742 #if !defined(__GNUC__) || (__GNUC__ < 2)
743 #define __alignof__(type) 0
744 #endif
745
746 bit_obstack->elements = NULL;
747 bit_obstack->heads = NULL;
748 obstack_specify_allocation (&bit_obstack->obstack, OBSTACK_CHUNK_SIZE,
749 __alignof__ (bitmap_element),
750 obstack_chunk_alloc,
751 obstack_chunk_free);
752 }
753
754 /* Release the memory from a bitmap obstack. If BIT_OBSTACK is NULL,
755 release the default bitmap obstack. */
756
757 void
758 bitmap_obstack_release (bitmap_obstack *bit_obstack)
759 {
760 if (!bit_obstack)
761 {
762 if (--bitmap_default_obstack_depth)
763 {
764 gcc_assert (bitmap_default_obstack_depth > 0);
765 return;
766 }
767 bit_obstack = &bitmap_default_obstack;
768 }
769
770 bit_obstack->elements = NULL;
771 bit_obstack->heads = NULL;
772 obstack_free (&bit_obstack->obstack, NULL);
773 }
774
775 /* Create a new bitmap on an obstack. If BIT_OBSTACK is NULL, create
776 it on the default bitmap obstack. */
777
778 bitmap
779 bitmap_alloc (bitmap_obstack *bit_obstack MEM_STAT_DECL)
780 {
781 bitmap map;
782
783 if (!bit_obstack)
784 bit_obstack = &bitmap_default_obstack;
785 map = bit_obstack->heads;
786 if (map)
787 bit_obstack->heads = (class bitmap_head *) map->first;
788 else
789 map = XOBNEW (&bit_obstack->obstack, bitmap_head);
790 bitmap_initialize (map, bit_obstack PASS_MEM_STAT);
791
792 if (GATHER_STATISTICS)
793 register_overhead (map, sizeof (bitmap_head));
794
795 return map;
796 }
797
798 /* Create a new GCd bitmap. */
799
800 bitmap
801 bitmap_gc_alloc (ALONE_MEM_STAT_DECL)
802 {
803 bitmap map;
804
805 map = ggc_alloc<bitmap_head> ();
806 bitmap_initialize (map, NULL PASS_MEM_STAT);
807
808 if (GATHER_STATISTICS)
809 register_overhead (map, sizeof (bitmap_head));
810
811 return map;
812 }
813
814 /* Release an obstack allocated bitmap. */
815
816 void
817 bitmap_obstack_free (bitmap map)
818 {
819 if (map)
820 {
821 bitmap_clear (map);
822 map->first = (bitmap_element *) map->obstack->heads;
823
824 if (GATHER_STATISTICS)
825 release_overhead (map, sizeof (bitmap_head), true);
826
827 map->obstack->heads = map;
828 }
829 }
830
831 \f
832 /* Return nonzero if all bits in an element are zero. */
833
834 static inline int
835 bitmap_element_zerop (const bitmap_element *element)
836 {
837 #if BITMAP_ELEMENT_WORDS == 2
838 return (element->bits[0] | element->bits[1]) == 0;
839 #else
840 unsigned i;
841
842 for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
843 if (element->bits[i] != 0)
844 return 0;
845
846 return 1;
847 #endif
848 }
849 \f
850 /* Copy a bitmap to another bitmap. */
851
852 void
853 bitmap_copy (bitmap to, const_bitmap from)
854 {
855 const bitmap_element *from_ptr;
856 bitmap_element *to_ptr = 0;
857
858 gcc_checking_assert (!to->tree_form && !from->tree_form);
859
860 bitmap_clear (to);
861
862 /* Copy elements in forward direction one at a time. */
863 for (from_ptr = from->first; from_ptr; from_ptr = from_ptr->next)
864 {
865 bitmap_element *to_elt = bitmap_element_allocate (to);
866
867 to_elt->indx = from_ptr->indx;
868 memcpy (to_elt->bits, from_ptr->bits, sizeof (to_elt->bits));
869
870 /* Here we have a special case of bitmap_list_link_element,
871 for the case where we know the links are being entered
872 in sequence. */
873 if (to_ptr == 0)
874 {
875 to->first = to->current = to_elt;
876 to->indx = from_ptr->indx;
877 to_elt->next = to_elt->prev = 0;
878 }
879 else
880 {
881 to_elt->prev = to_ptr;
882 to_elt->next = 0;
883 to_ptr->next = to_elt;
884 }
885
886 to_ptr = to_elt;
887 }
888 }
889
890 /* Move a bitmap to another bitmap. */
891
892 void
893 bitmap_move (bitmap to, bitmap from)
894 {
895 gcc_assert (to->obstack == from->obstack);
896
897 bitmap_clear (to);
898
899 size_t sz = 0;
900 if (GATHER_STATISTICS)
901 {
902 for (bitmap_element *e = to->first; e; e = e->next)
903 sz += sizeof (bitmap_element);
904 register_overhead (to, sz);
905 }
906
907 *to = *from;
908
909 if (GATHER_STATISTICS)
910 release_overhead (from, sz, false);
911 }
912 \f
913 /* Clear a single bit in a bitmap. Return true if the bit changed. */
914
915 bool
916 bitmap_clear_bit (bitmap head, int bit)
917 {
918 unsigned int indx = bit / BITMAP_ELEMENT_ALL_BITS;
919 bitmap_element *ptr;
920
921 if (!head->tree_form)
922 ptr = bitmap_list_find_element (head, indx);
923 else
924 ptr = bitmap_tree_find_element (head, indx);
925 if (ptr != 0)
926 {
927 unsigned bit_num = bit % BITMAP_WORD_BITS;
928 unsigned word_num = bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
929 BITMAP_WORD bit_val = ((BITMAP_WORD) 1) << bit_num;
930 bool res = (ptr->bits[word_num] & bit_val) != 0;
931 if (res)
932 {
933 ptr->bits[word_num] &= ~bit_val;
934 /* If we cleared the entire word, free up the element. */
935 if (!ptr->bits[word_num]
936 && bitmap_element_zerop (ptr))
937 {
938 if (!head->tree_form)
939 bitmap_list_unlink_element (head, ptr);
940 else
941 bitmap_tree_unlink_element (head, ptr);
942 }
943 }
944
945 return res;
946 }
947
948 return false;
949 }
950
951 /* Set a single bit in a bitmap. Return true if the bit changed. */
952
953 bool
954 bitmap_set_bit (bitmap head, int bit)
955 {
956 unsigned indx = bit / BITMAP_ELEMENT_ALL_BITS;
957 bitmap_element *ptr;
958 if (!head->tree_form)
959 ptr = bitmap_list_find_element (head, indx);
960 else
961 ptr = bitmap_tree_find_element (head, indx);
962 unsigned word_num = bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
963 unsigned bit_num = bit % BITMAP_WORD_BITS;
964 BITMAP_WORD bit_val = ((BITMAP_WORD) 1) << bit_num;
965
966 if (ptr != 0)
967 {
968 bool res = (ptr->bits[word_num] & bit_val) == 0;
969 if (res)
970 ptr->bits[word_num] |= bit_val;
971 return res;
972 }
973
974 ptr = bitmap_element_allocate (head);
975 ptr->indx = bit / BITMAP_ELEMENT_ALL_BITS;
976 ptr->bits[word_num] = bit_val;
977 if (!head->tree_form)
978 bitmap_list_link_element (head, ptr);
979 else
980 bitmap_tree_link_element (head, ptr);
981 return true;
982 }
983
984 /* Return whether a bit is set within a bitmap. */
985
986 int
987 bitmap_bit_p (const_bitmap head, int bit)
988 {
989 unsigned int indx = bit / BITMAP_ELEMENT_ALL_BITS;
990 const bitmap_element *ptr;
991 unsigned bit_num;
992 unsigned word_num;
993
994 if (!head->tree_form)
995 ptr = bitmap_list_find_element (const_cast<bitmap> (head), indx);
996 else
997 ptr = bitmap_tree_find_element (const_cast<bitmap> (head), indx);
998 if (ptr == 0)
999 return 0;
1000
1001 bit_num = bit % BITMAP_WORD_BITS;
1002 word_num = bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
1003
1004 return (ptr->bits[word_num] >> bit_num) & 1;
1005 }
1006 \f
1007 #if GCC_VERSION < 3400
1008 /* Table of number of set bits in a character, indexed by value of char. */
1009 static const unsigned char popcount_table[] =
1010 {
1011 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,
1012 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,
1013 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,
1014 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,
1015 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,
1016 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,
1017 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,
1018 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,
1019 };
1020
1021 static unsigned long
1022 bitmap_popcount (BITMAP_WORD a)
1023 {
1024 unsigned long ret = 0;
1025 unsigned i;
1026
1027 /* Just do this the table way for now */
1028 for (i = 0; i < BITMAP_WORD_BITS; i+= 8)
1029 ret += popcount_table[(a >> i) & 0xff];
1030 return ret;
1031 }
1032 #endif
1033
1034 /* Count and return the number of bits set in the bitmap word BITS. */
1035 static unsigned long
1036 bitmap_count_bits_in_word (const BITMAP_WORD *bits)
1037 {
1038 unsigned long count = 0;
1039
1040 for (unsigned ix = 0; ix != BITMAP_ELEMENT_WORDS; ix++)
1041 {
1042 #if GCC_VERSION >= 3400
1043 /* Note that popcountl matches BITMAP_WORD in type, so the actual size
1044 of BITMAP_WORD is not material. */
1045 count += __builtin_popcountl (bits[ix]);
1046 #else
1047 count += bitmap_popcount (bits[ix]);
1048 #endif
1049 }
1050 return count;
1051 }
1052
1053 /* Count the number of bits set in the bitmap, and return it. */
1054
1055 unsigned long
1056 bitmap_count_bits (const_bitmap a)
1057 {
1058 unsigned long count = 0;
1059 const bitmap_element *elt;
1060
1061 gcc_checking_assert (!a->tree_form);
1062 for (elt = a->first; elt; elt = elt->next)
1063 count += bitmap_count_bits_in_word (elt->bits);
1064
1065 return count;
1066 }
1067
1068 /* Count the number of unique bits set in A and B and return it. */
1069
1070 unsigned long
1071 bitmap_count_unique_bits (const_bitmap a, const_bitmap b)
1072 {
1073 unsigned long count = 0;
1074 const bitmap_element *elt_a, *elt_b;
1075
1076 for (elt_a = a->first, elt_b = b->first; elt_a && elt_b; )
1077 {
1078 /* If we're at different indices, then count all the bits
1079 in the lower element. If we're at the same index, then
1080 count the bits in the IOR of the two elements. */
1081 if (elt_a->indx < elt_b->indx)
1082 {
1083 count += bitmap_count_bits_in_word (elt_a->bits);
1084 elt_a = elt_a->next;
1085 }
1086 else if (elt_b->indx < elt_a->indx)
1087 {
1088 count += bitmap_count_bits_in_word (elt_b->bits);
1089 elt_b = elt_b->next;
1090 }
1091 else
1092 {
1093 BITMAP_WORD bits[BITMAP_ELEMENT_WORDS];
1094 for (unsigned ix = 0; ix != BITMAP_ELEMENT_WORDS; ix++)
1095 bits[ix] = elt_a->bits[ix] | elt_b->bits[ix];
1096 count += bitmap_count_bits_in_word (bits);
1097 elt_a = elt_a->next;
1098 elt_b = elt_b->next;
1099 }
1100 }
1101 return count;
1102 }
1103
1104 /* Return true if the bitmap has a single bit set. Otherwise return
1105 false. */
1106
1107 bool
1108 bitmap_single_bit_set_p (const_bitmap a)
1109 {
1110 unsigned long count = 0;
1111 const bitmap_element *elt;
1112 unsigned ix;
1113
1114 if (bitmap_empty_p (a))
1115 return false;
1116
1117 elt = a->first;
1118
1119 /* As there are no completely empty bitmap elements, a second one
1120 means we have more than one bit set. */
1121 if (elt->next != NULL
1122 && (!a->tree_form || elt->prev != NULL))
1123 return false;
1124
1125 for (ix = 0; ix != BITMAP_ELEMENT_WORDS; ix++)
1126 {
1127 #if GCC_VERSION >= 3400
1128 /* Note that popcountl matches BITMAP_WORD in type, so the actual size
1129 of BITMAP_WORD is not material. */
1130 count += __builtin_popcountl (elt->bits[ix]);
1131 #else
1132 count += bitmap_popcount (elt->bits[ix]);
1133 #endif
1134 if (count > 1)
1135 return false;
1136 }
1137
1138 return count == 1;
1139 }
1140
1141
1142 /* Return the bit number of the first set bit in the bitmap. The
1143 bitmap must be non-empty. */
1144
1145 unsigned
1146 bitmap_first_set_bit (const_bitmap a)
1147 {
1148 const bitmap_element *elt = a->first;
1149 unsigned bit_no;
1150 BITMAP_WORD word;
1151 unsigned ix;
1152
1153 gcc_checking_assert (elt);
1154
1155 if (a->tree_form)
1156 while (elt->prev)
1157 elt = elt->prev;
1158
1159 bit_no = elt->indx * BITMAP_ELEMENT_ALL_BITS;
1160 for (ix = 0; ix != BITMAP_ELEMENT_WORDS; ix++)
1161 {
1162 word = elt->bits[ix];
1163 if (word)
1164 goto found_bit;
1165 }
1166 gcc_unreachable ();
1167 found_bit:
1168 bit_no += ix * BITMAP_WORD_BITS;
1169
1170 #if GCC_VERSION >= 3004
1171 gcc_assert (sizeof (long) == sizeof (word));
1172 bit_no += __builtin_ctzl (word);
1173 #else
1174 /* Binary search for the first set bit. */
1175 #if BITMAP_WORD_BITS > 64
1176 #error "Fill out the table."
1177 #endif
1178 #if BITMAP_WORD_BITS > 32
1179 if (!(word & 0xffffffff))
1180 word >>= 32, bit_no += 32;
1181 #endif
1182 if (!(word & 0xffff))
1183 word >>= 16, bit_no += 16;
1184 if (!(word & 0xff))
1185 word >>= 8, bit_no += 8;
1186 if (!(word & 0xf))
1187 word >>= 4, bit_no += 4;
1188 if (!(word & 0x3))
1189 word >>= 2, bit_no += 2;
1190 if (!(word & 0x1))
1191 word >>= 1, bit_no += 1;
1192
1193 gcc_checking_assert (word & 1);
1194 #endif
1195 return bit_no;
1196 }
1197
1198 /* Return the bit number of the first set bit in the bitmap. The
1199 bitmap must be non-empty. */
1200
1201 unsigned
1202 bitmap_last_set_bit (const_bitmap a)
1203 {
1204 const bitmap_element *elt;
1205 unsigned bit_no;
1206 BITMAP_WORD word;
1207 int ix;
1208
1209 if (a->tree_form)
1210 elt = a->first;
1211 else
1212 elt = a->current ? a->current : a->first;
1213 gcc_checking_assert (elt);
1214
1215 while (elt->next)
1216 elt = elt->next;
1217
1218 bit_no = elt->indx * BITMAP_ELEMENT_ALL_BITS;
1219 for (ix = BITMAP_ELEMENT_WORDS - 1; ix >= 1; ix--)
1220 {
1221 word = elt->bits[ix];
1222 if (word)
1223 goto found_bit;
1224 }
1225 gcc_assert (elt->bits[ix] != 0);
1226 found_bit:
1227 bit_no += ix * BITMAP_WORD_BITS;
1228 #if GCC_VERSION >= 3004
1229 gcc_assert (sizeof (long) == sizeof (word));
1230 bit_no += BITMAP_WORD_BITS - __builtin_clzl (word) - 1;
1231 #else
1232 /* Hopefully this is a twos-complement host... */
1233 BITMAP_WORD x = word;
1234 x |= (x >> 1);
1235 x |= (x >> 2);
1236 x |= (x >> 4);
1237 x |= (x >> 8);
1238 x |= (x >> 16);
1239 #if BITMAP_WORD_BITS > 32
1240 x |= (x >> 32);
1241 #endif
1242 bit_no += bitmap_popcount (x) - 1;
1243 #endif
1244
1245 return bit_no;
1246 }
1247 \f
1248
1249 /* DST = A & B. */
1250
1251 void
1252 bitmap_and (bitmap dst, const_bitmap a, const_bitmap b)
1253 {
1254 bitmap_element *dst_elt = dst->first;
1255 const bitmap_element *a_elt = a->first;
1256 const bitmap_element *b_elt = b->first;
1257 bitmap_element *dst_prev = NULL;
1258
1259 gcc_checking_assert (!dst->tree_form && !a->tree_form && !b->tree_form);
1260 gcc_assert (dst != a && dst != b);
1261
1262 if (a == b)
1263 {
1264 bitmap_copy (dst, a);
1265 return;
1266 }
1267
1268 while (a_elt && b_elt)
1269 {
1270 if (a_elt->indx < b_elt->indx)
1271 a_elt = a_elt->next;
1272 else if (b_elt->indx < a_elt->indx)
1273 b_elt = b_elt->next;
1274 else
1275 {
1276 /* Matching elts, generate A & B. */
1277 unsigned ix;
1278 BITMAP_WORD ior = 0;
1279
1280 if (!dst_elt)
1281 dst_elt = bitmap_list_insert_element_after (dst, dst_prev,
1282 a_elt->indx);
1283 else
1284 dst_elt->indx = a_elt->indx;
1285 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1286 {
1287 BITMAP_WORD r = a_elt->bits[ix] & b_elt->bits[ix];
1288
1289 dst_elt->bits[ix] = r;
1290 ior |= r;
1291 }
1292 if (ior)
1293 {
1294 dst_prev = dst_elt;
1295 dst_elt = dst_elt->next;
1296 }
1297 a_elt = a_elt->next;
1298 b_elt = b_elt->next;
1299 }
1300 }
1301 /* Ensure that dst->current is valid. */
1302 dst->current = dst->first;
1303 bitmap_elt_clear_from (dst, dst_elt);
1304 gcc_checking_assert (!dst->current == !dst->first);
1305 if (dst->current)
1306 dst->indx = dst->current->indx;
1307 }
1308
1309 /* A &= B. Return true if A changed. */
1310
1311 bool
1312 bitmap_and_into (bitmap a, const_bitmap b)
1313 {
1314 bitmap_element *a_elt = a->first;
1315 const bitmap_element *b_elt = b->first;
1316 bitmap_element *next;
1317 bool changed = false;
1318
1319 gcc_checking_assert (!a->tree_form && !b->tree_form);
1320
1321 if (a == b)
1322 return false;
1323
1324 while (a_elt && b_elt)
1325 {
1326 if (a_elt->indx < b_elt->indx)
1327 {
1328 next = a_elt->next;
1329 bitmap_list_unlink_element (a, a_elt);
1330 a_elt = next;
1331 changed = true;
1332 }
1333 else if (b_elt->indx < a_elt->indx)
1334 b_elt = b_elt->next;
1335 else
1336 {
1337 /* Matching elts, generate A &= B. */
1338 unsigned ix;
1339 BITMAP_WORD ior = 0;
1340
1341 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1342 {
1343 BITMAP_WORD r = a_elt->bits[ix] & b_elt->bits[ix];
1344 if (a_elt->bits[ix] != r)
1345 changed = true;
1346 a_elt->bits[ix] = r;
1347 ior |= r;
1348 }
1349 next = a_elt->next;
1350 if (!ior)
1351 bitmap_list_unlink_element (a, a_elt);
1352 a_elt = next;
1353 b_elt = b_elt->next;
1354 }
1355 }
1356
1357 if (a_elt)
1358 {
1359 changed = true;
1360 bitmap_elt_clear_from (a, a_elt);
1361 }
1362
1363 gcc_checking_assert (!a->current == !a->first
1364 && (!a->current || a->indx == a->current->indx));
1365
1366 return changed;
1367 }
1368
1369
1370 /* Insert an element equal to SRC_ELT after DST_PREV, overwriting DST_ELT
1371 if non-NULL. CHANGED is true if the destination bitmap had already been
1372 changed; the new value of CHANGED is returned. */
1373
1374 static inline bool
1375 bitmap_elt_copy (bitmap dst, bitmap_element *dst_elt, bitmap_element *dst_prev,
1376 const bitmap_element *src_elt, bool changed)
1377 {
1378 if (!changed && dst_elt && dst_elt->indx == src_elt->indx)
1379 {
1380 unsigned ix;
1381
1382 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1383 if (src_elt->bits[ix] != dst_elt->bits[ix])
1384 {
1385 dst_elt->bits[ix] = src_elt->bits[ix];
1386 changed = true;
1387 }
1388 }
1389 else
1390 {
1391 changed = true;
1392 if (!dst_elt)
1393 dst_elt = bitmap_list_insert_element_after (dst, dst_prev,
1394 src_elt->indx);
1395 else
1396 dst_elt->indx = src_elt->indx;
1397 memcpy (dst_elt->bits, src_elt->bits, sizeof (dst_elt->bits));
1398 }
1399 return changed;
1400 }
1401
1402
1403
1404 /* DST = A & ~B */
1405
1406 bool
1407 bitmap_and_compl (bitmap dst, const_bitmap a, const_bitmap b)
1408 {
1409 bitmap_element *dst_elt = dst->first;
1410 const bitmap_element *a_elt = a->first;
1411 const bitmap_element *b_elt = b->first;
1412 bitmap_element *dst_prev = NULL;
1413 bitmap_element **dst_prev_pnext = &dst->first;
1414 bool changed = false;
1415
1416 gcc_checking_assert (!dst->tree_form && !a->tree_form && !b->tree_form);
1417 gcc_assert (dst != a && dst != b);
1418
1419 if (a == b)
1420 {
1421 changed = !bitmap_empty_p (dst);
1422 bitmap_clear (dst);
1423 return changed;
1424 }
1425
1426 while (a_elt)
1427 {
1428 while (b_elt && b_elt->indx < a_elt->indx)
1429 b_elt = b_elt->next;
1430
1431 if (!b_elt || b_elt->indx > a_elt->indx)
1432 {
1433 changed = bitmap_elt_copy (dst, dst_elt, dst_prev, a_elt, changed);
1434 dst_prev = *dst_prev_pnext;
1435 dst_prev_pnext = &dst_prev->next;
1436 dst_elt = *dst_prev_pnext;
1437 a_elt = a_elt->next;
1438 }
1439
1440 else
1441 {
1442 /* Matching elts, generate A & ~B. */
1443 unsigned ix;
1444 BITMAP_WORD ior = 0;
1445
1446 if (!changed && dst_elt && dst_elt->indx == a_elt->indx)
1447 {
1448 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1449 {
1450 BITMAP_WORD r = a_elt->bits[ix] & ~b_elt->bits[ix];
1451
1452 if (dst_elt->bits[ix] != r)
1453 {
1454 changed = true;
1455 dst_elt->bits[ix] = r;
1456 }
1457 ior |= r;
1458 }
1459 }
1460 else
1461 {
1462 bool new_element;
1463 if (!dst_elt || dst_elt->indx > a_elt->indx)
1464 {
1465 dst_elt = bitmap_list_insert_element_after (dst, dst_prev,
1466 a_elt->indx);
1467 new_element = true;
1468 }
1469 else
1470 {
1471 dst_elt->indx = a_elt->indx;
1472 new_element = false;
1473 }
1474
1475 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1476 {
1477 BITMAP_WORD r = a_elt->bits[ix] & ~b_elt->bits[ix];
1478
1479 dst_elt->bits[ix] = r;
1480 ior |= r;
1481 }
1482
1483 if (ior)
1484 changed = true;
1485 else
1486 {
1487 changed |= !new_element;
1488 bitmap_list_unlink_element (dst, dst_elt);
1489 dst_elt = *dst_prev_pnext;
1490 }
1491 }
1492
1493 if (ior)
1494 {
1495 dst_prev = *dst_prev_pnext;
1496 dst_prev_pnext = &dst_prev->next;
1497 dst_elt = *dst_prev_pnext;
1498 }
1499 a_elt = a_elt->next;
1500 b_elt = b_elt->next;
1501 }
1502 }
1503
1504 /* Ensure that dst->current is valid. */
1505 dst->current = dst->first;
1506
1507 if (dst_elt)
1508 {
1509 changed = true;
1510 bitmap_elt_clear_from (dst, dst_elt);
1511 }
1512 gcc_checking_assert (!dst->current == !dst->first);
1513 if (dst->current)
1514 dst->indx = dst->current->indx;
1515
1516 return changed;
1517 }
1518
1519 /* A &= ~B. Returns true if A changes */
1520
1521 bool
1522 bitmap_and_compl_into (bitmap a, const_bitmap b)
1523 {
1524 bitmap_element *a_elt = a->first;
1525 const bitmap_element *b_elt = b->first;
1526 bitmap_element *next;
1527 BITMAP_WORD changed = 0;
1528
1529 gcc_checking_assert (!a->tree_form && !b->tree_form);
1530
1531 if (a == b)
1532 {
1533 if (bitmap_empty_p (a))
1534 return false;
1535 else
1536 {
1537 bitmap_clear (a);
1538 return true;
1539 }
1540 }
1541
1542 while (a_elt && b_elt)
1543 {
1544 if (a_elt->indx < b_elt->indx)
1545 a_elt = a_elt->next;
1546 else if (b_elt->indx < a_elt->indx)
1547 b_elt = b_elt->next;
1548 else
1549 {
1550 /* Matching elts, generate A &= ~B. */
1551 unsigned ix;
1552 BITMAP_WORD ior = 0;
1553
1554 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1555 {
1556 BITMAP_WORD cleared = a_elt->bits[ix] & b_elt->bits[ix];
1557 BITMAP_WORD r = a_elt->bits[ix] ^ cleared;
1558
1559 a_elt->bits[ix] = r;
1560 changed |= cleared;
1561 ior |= r;
1562 }
1563 next = a_elt->next;
1564 if (!ior)
1565 bitmap_list_unlink_element (a, a_elt);
1566 a_elt = next;
1567 b_elt = b_elt->next;
1568 }
1569 }
1570 gcc_checking_assert (!a->current == !a->first
1571 && (!a->current || a->indx == a->current->indx));
1572 return changed != 0;
1573 }
1574
1575 /* Set COUNT bits from START in HEAD. */
1576 void
1577 bitmap_set_range (bitmap head, unsigned int start, unsigned int count)
1578 {
1579 unsigned int first_index, end_bit_plus1, last_index;
1580 bitmap_element *elt, *elt_prev;
1581 unsigned int i;
1582
1583 gcc_checking_assert (!head->tree_form);
1584
1585 if (!count)
1586 return;
1587
1588 if (count == 1)
1589 {
1590 bitmap_set_bit (head, start);
1591 return;
1592 }
1593
1594 first_index = start / BITMAP_ELEMENT_ALL_BITS;
1595 end_bit_plus1 = start + count;
1596 last_index = (end_bit_plus1 - 1) / BITMAP_ELEMENT_ALL_BITS;
1597 elt = bitmap_list_find_element (head, first_index);
1598
1599 /* If bitmap_list_find_element returns zero, the current is the closest block
1600 to the result. Otherwise, just use bitmap_element_allocate to
1601 ensure ELT is set; in the loop below, ELT == NULL means "insert
1602 at the end of the bitmap". */
1603 if (!elt)
1604 {
1605 elt = bitmap_element_allocate (head);
1606 elt->indx = first_index;
1607 bitmap_list_link_element (head, elt);
1608 }
1609
1610 gcc_checking_assert (elt->indx == first_index);
1611 elt_prev = elt->prev;
1612 for (i = first_index; i <= last_index; i++)
1613 {
1614 unsigned elt_start_bit = i * BITMAP_ELEMENT_ALL_BITS;
1615 unsigned elt_end_bit_plus1 = elt_start_bit + BITMAP_ELEMENT_ALL_BITS;
1616
1617 unsigned int first_word_to_mod;
1618 BITMAP_WORD first_mask;
1619 unsigned int last_word_to_mod;
1620 BITMAP_WORD last_mask;
1621 unsigned int ix;
1622
1623 if (!elt || elt->indx != i)
1624 elt = bitmap_list_insert_element_after (head, elt_prev, i);
1625
1626 if (elt_start_bit <= start)
1627 {
1628 /* The first bit to turn on is somewhere inside this
1629 elt. */
1630 first_word_to_mod = (start - elt_start_bit) / BITMAP_WORD_BITS;
1631
1632 /* This mask should have 1s in all bits >= start position. */
1633 first_mask =
1634 (((BITMAP_WORD) 1) << ((start % BITMAP_WORD_BITS))) - 1;
1635 first_mask = ~first_mask;
1636 }
1637 else
1638 {
1639 /* The first bit to turn on is below this start of this elt. */
1640 first_word_to_mod = 0;
1641 first_mask = ~(BITMAP_WORD) 0;
1642 }
1643
1644 if (elt_end_bit_plus1 <= end_bit_plus1)
1645 {
1646 /* The last bit to turn on is beyond this elt. */
1647 last_word_to_mod = BITMAP_ELEMENT_WORDS - 1;
1648 last_mask = ~(BITMAP_WORD) 0;
1649 }
1650 else
1651 {
1652 /* The last bit to turn on is inside to this elt. */
1653 last_word_to_mod =
1654 (end_bit_plus1 - elt_start_bit) / BITMAP_WORD_BITS;
1655
1656 /* The last mask should have 1s below the end bit. */
1657 last_mask =
1658 (((BITMAP_WORD) 1) << ((end_bit_plus1 % BITMAP_WORD_BITS))) - 1;
1659 }
1660
1661 if (first_word_to_mod == last_word_to_mod)
1662 {
1663 BITMAP_WORD mask = first_mask & last_mask;
1664 elt->bits[first_word_to_mod] |= mask;
1665 }
1666 else
1667 {
1668 elt->bits[first_word_to_mod] |= first_mask;
1669 if (BITMAP_ELEMENT_WORDS > 2)
1670 for (ix = first_word_to_mod + 1; ix < last_word_to_mod; ix++)
1671 elt->bits[ix] = ~(BITMAP_WORD) 0;
1672 elt->bits[last_word_to_mod] |= last_mask;
1673 }
1674
1675 elt_prev = elt;
1676 elt = elt->next;
1677 }
1678
1679 head->current = elt ? elt : elt_prev;
1680 head->indx = head->current->indx;
1681 }
1682
1683 /* Clear COUNT bits from START in HEAD. */
1684 void
1685 bitmap_clear_range (bitmap head, unsigned int start, unsigned int count)
1686 {
1687 unsigned int first_index, end_bit_plus1, last_index;
1688 bitmap_element *elt;
1689
1690 gcc_checking_assert (!head->tree_form);
1691
1692 if (!count)
1693 return;
1694
1695 if (count == 1)
1696 {
1697 bitmap_clear_bit (head, start);
1698 return;
1699 }
1700
1701 first_index = start / BITMAP_ELEMENT_ALL_BITS;
1702 end_bit_plus1 = start + count;
1703 last_index = (end_bit_plus1 - 1) / BITMAP_ELEMENT_ALL_BITS;
1704 elt = bitmap_list_find_element (head, first_index);
1705
1706 /* If bitmap_list_find_element returns zero, the current is the closest block
1707 to the result. If the current is less than first index, find the
1708 next one. Otherwise, just set elt to be current. */
1709 if (!elt)
1710 {
1711 if (head->current)
1712 {
1713 if (head->indx < first_index)
1714 {
1715 elt = head->current->next;
1716 if (!elt)
1717 return;
1718 }
1719 else
1720 elt = head->current;
1721 }
1722 else
1723 return;
1724 }
1725
1726 while (elt && (elt->indx <= last_index))
1727 {
1728 bitmap_element * next_elt = elt->next;
1729 unsigned elt_start_bit = (elt->indx) * BITMAP_ELEMENT_ALL_BITS;
1730 unsigned elt_end_bit_plus1 = elt_start_bit + BITMAP_ELEMENT_ALL_BITS;
1731
1732
1733 if (elt_start_bit >= start && elt_end_bit_plus1 <= end_bit_plus1)
1734 /* Get rid of the entire elt and go to the next one. */
1735 bitmap_list_unlink_element (head, elt);
1736 else
1737 {
1738 /* Going to have to knock out some bits in this elt. */
1739 unsigned int first_word_to_mod;
1740 BITMAP_WORD first_mask;
1741 unsigned int last_word_to_mod;
1742 BITMAP_WORD last_mask;
1743 unsigned int i;
1744 bool clear = true;
1745
1746 if (elt_start_bit <= start)
1747 {
1748 /* The first bit to turn off is somewhere inside this
1749 elt. */
1750 first_word_to_mod = (start - elt_start_bit) / BITMAP_WORD_BITS;
1751
1752 /* This mask should have 1s in all bits >= start position. */
1753 first_mask =
1754 (((BITMAP_WORD) 1) << ((start % BITMAP_WORD_BITS))) - 1;
1755 first_mask = ~first_mask;
1756 }
1757 else
1758 {
1759 /* The first bit to turn off is below this start of this elt. */
1760 first_word_to_mod = 0;
1761 first_mask = 0;
1762 first_mask = ~first_mask;
1763 }
1764
1765 if (elt_end_bit_plus1 <= end_bit_plus1)
1766 {
1767 /* The last bit to turn off is beyond this elt. */
1768 last_word_to_mod = BITMAP_ELEMENT_WORDS - 1;
1769 last_mask = 0;
1770 last_mask = ~last_mask;
1771 }
1772 else
1773 {
1774 /* The last bit to turn off is inside to this elt. */
1775 last_word_to_mod =
1776 (end_bit_plus1 - elt_start_bit) / BITMAP_WORD_BITS;
1777
1778 /* The last mask should have 1s below the end bit. */
1779 last_mask =
1780 (((BITMAP_WORD) 1) << (((end_bit_plus1) % BITMAP_WORD_BITS))) - 1;
1781 }
1782
1783
1784 if (first_word_to_mod == last_word_to_mod)
1785 {
1786 BITMAP_WORD mask = first_mask & last_mask;
1787 elt->bits[first_word_to_mod] &= ~mask;
1788 }
1789 else
1790 {
1791 elt->bits[first_word_to_mod] &= ~first_mask;
1792 if (BITMAP_ELEMENT_WORDS > 2)
1793 for (i = first_word_to_mod + 1; i < last_word_to_mod; i++)
1794 elt->bits[i] = 0;
1795 elt->bits[last_word_to_mod] &= ~last_mask;
1796 }
1797 for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
1798 if (elt->bits[i])
1799 {
1800 clear = false;
1801 break;
1802 }
1803 /* Check to see if there are any bits left. */
1804 if (clear)
1805 bitmap_list_unlink_element (head, elt);
1806 }
1807 elt = next_elt;
1808 }
1809
1810 if (elt)
1811 {
1812 head->current = elt;
1813 head->indx = head->current->indx;
1814 }
1815 }
1816
1817 /* A = ~A & B. */
1818
1819 void
1820 bitmap_compl_and_into (bitmap a, const_bitmap b)
1821 {
1822 bitmap_element *a_elt = a->first;
1823 const bitmap_element *b_elt = b->first;
1824 bitmap_element *a_prev = NULL;
1825 bitmap_element *next;
1826
1827 gcc_checking_assert (!a->tree_form && !b->tree_form);
1828 gcc_assert (a != b);
1829
1830 if (bitmap_empty_p (a))
1831 {
1832 bitmap_copy (a, b);
1833 return;
1834 }
1835 if (bitmap_empty_p (b))
1836 {
1837 bitmap_clear (a);
1838 return;
1839 }
1840
1841 while (a_elt || b_elt)
1842 {
1843 if (!b_elt || (a_elt && a_elt->indx < b_elt->indx))
1844 {
1845 /* A is before B. Remove A */
1846 next = a_elt->next;
1847 a_prev = a_elt->prev;
1848 bitmap_list_unlink_element (a, a_elt);
1849 a_elt = next;
1850 }
1851 else if (!a_elt || b_elt->indx < a_elt->indx)
1852 {
1853 /* B is before A. Copy B. */
1854 next = bitmap_list_insert_element_after (a, a_prev, b_elt->indx);
1855 memcpy (next->bits, b_elt->bits, sizeof (next->bits));
1856 a_prev = next;
1857 b_elt = b_elt->next;
1858 }
1859 else
1860 {
1861 /* Matching elts, generate A = ~A & B. */
1862 unsigned ix;
1863 BITMAP_WORD ior = 0;
1864
1865 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1866 {
1867 BITMAP_WORD cleared = a_elt->bits[ix] & b_elt->bits[ix];
1868 BITMAP_WORD r = b_elt->bits[ix] ^ cleared;
1869
1870 a_elt->bits[ix] = r;
1871 ior |= r;
1872 }
1873 next = a_elt->next;
1874 if (!ior)
1875 bitmap_list_unlink_element (a, a_elt);
1876 else
1877 a_prev = a_elt;
1878 a_elt = next;
1879 b_elt = b_elt->next;
1880 }
1881 }
1882 gcc_checking_assert (!a->current == !a->first
1883 && (!a->current || a->indx == a->current->indx));
1884 return;
1885 }
1886
1887
1888 /* Insert an element corresponding to A_ELT | B_ELT after DST_PREV,
1889 overwriting DST_ELT if non-NULL. CHANGED is true if the destination bitmap
1890 had already been changed; the new value of CHANGED is returned. */
1891
1892 static inline bool
1893 bitmap_elt_ior (bitmap dst, bitmap_element *dst_elt, bitmap_element *dst_prev,
1894 const bitmap_element *a_elt, const bitmap_element *b_elt,
1895 bool changed)
1896 {
1897 gcc_assert (a_elt || b_elt);
1898
1899 if (a_elt && b_elt && a_elt->indx == b_elt->indx)
1900 {
1901 /* Matching elts, generate A | B. */
1902 unsigned ix;
1903
1904 if (!changed && dst_elt && dst_elt->indx == a_elt->indx)
1905 {
1906 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1907 {
1908 BITMAP_WORD r = a_elt->bits[ix] | b_elt->bits[ix];
1909 if (r != dst_elt->bits[ix])
1910 {
1911 dst_elt->bits[ix] = r;
1912 changed = true;
1913 }
1914 }
1915 }
1916 else
1917 {
1918 changed = true;
1919 if (!dst_elt)
1920 dst_elt = bitmap_list_insert_element_after (dst, dst_prev,
1921 a_elt->indx);
1922 else
1923 dst_elt->indx = a_elt->indx;
1924 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1925 {
1926 BITMAP_WORD r = a_elt->bits[ix] | b_elt->bits[ix];
1927 dst_elt->bits[ix] = r;
1928 }
1929 }
1930 }
1931 else
1932 {
1933 /* Copy a single element. */
1934 const bitmap_element *src;
1935
1936 if (!b_elt || (a_elt && a_elt->indx < b_elt->indx))
1937 src = a_elt;
1938 else
1939 src = b_elt;
1940
1941 gcc_checking_assert (src);
1942 changed = bitmap_elt_copy (dst, dst_elt, dst_prev, src, changed);
1943 }
1944 return changed;
1945 }
1946
1947
1948 /* DST = A | B. Return true if DST changes. */
1949
1950 bool
1951 bitmap_ior (bitmap dst, const_bitmap a, const_bitmap b)
1952 {
1953 bitmap_element *dst_elt = dst->first;
1954 const bitmap_element *a_elt = a->first;
1955 const bitmap_element *b_elt = b->first;
1956 bitmap_element *dst_prev = NULL;
1957 bitmap_element **dst_prev_pnext = &dst->first;
1958 bool changed = false;
1959
1960 gcc_checking_assert (!dst->tree_form && !a->tree_form && !b->tree_form);
1961 gcc_assert (dst != a && dst != b);
1962
1963 while (a_elt || b_elt)
1964 {
1965 changed = bitmap_elt_ior (dst, dst_elt, dst_prev, a_elt, b_elt, changed);
1966
1967 if (a_elt && b_elt && a_elt->indx == b_elt->indx)
1968 {
1969 a_elt = a_elt->next;
1970 b_elt = b_elt->next;
1971 }
1972 else
1973 {
1974 if (a_elt && (!b_elt || a_elt->indx <= b_elt->indx))
1975 a_elt = a_elt->next;
1976 else if (b_elt && (!a_elt || b_elt->indx <= a_elt->indx))
1977 b_elt = b_elt->next;
1978 }
1979
1980 dst_prev = *dst_prev_pnext;
1981 dst_prev_pnext = &dst_prev->next;
1982 dst_elt = *dst_prev_pnext;
1983 }
1984
1985 if (dst_elt)
1986 {
1987 changed = true;
1988 /* Ensure that dst->current is valid. */
1989 dst->current = dst->first;
1990 bitmap_elt_clear_from (dst, dst_elt);
1991 }
1992 gcc_checking_assert (!dst->current == !dst->first);
1993 if (dst->current)
1994 dst->indx = dst->current->indx;
1995 return changed;
1996 }
1997
1998 /* A |= B. Return true if A changes. */
1999
2000 bool
2001 bitmap_ior_into (bitmap a, const_bitmap b)
2002 {
2003 bitmap_element *a_elt = a->first;
2004 const bitmap_element *b_elt = b->first;
2005 bitmap_element *a_prev = NULL;
2006 bitmap_element **a_prev_pnext = &a->first;
2007 bool changed = false;
2008
2009 gcc_checking_assert (!a->tree_form && !b->tree_form);
2010 if (a == b)
2011 return false;
2012
2013 while (b_elt)
2014 {
2015 /* If A lags behind B, just advance it. */
2016 if (!a_elt || a_elt->indx == b_elt->indx)
2017 {
2018 changed = bitmap_elt_ior (a, a_elt, a_prev, a_elt, b_elt, changed);
2019 b_elt = b_elt->next;
2020 }
2021 else if (a_elt->indx > b_elt->indx)
2022 {
2023 changed = bitmap_elt_copy (a, NULL, a_prev, b_elt, changed);
2024 b_elt = b_elt->next;
2025 }
2026
2027 a_prev = *a_prev_pnext;
2028 a_prev_pnext = &a_prev->next;
2029 a_elt = *a_prev_pnext;
2030 }
2031
2032 gcc_checking_assert (!a->current == !a->first);
2033 if (a->current)
2034 a->indx = a->current->indx;
2035 return changed;
2036 }
2037
2038 /* A |= B. Return true if A changes. Free B (re-using its storage
2039 for the result). */
2040
2041 bool
2042 bitmap_ior_into_and_free (bitmap a, bitmap *b_)
2043 {
2044 bitmap b = *b_;
2045 bitmap_element *a_elt = a->first;
2046 bitmap_element *b_elt = b->first;
2047 bitmap_element *a_prev = NULL;
2048 bitmap_element **a_prev_pnext = &a->first;
2049 bool changed = false;
2050
2051 gcc_checking_assert (!a->tree_form && !b->tree_form);
2052 gcc_assert (a->obstack == b->obstack);
2053 if (a == b)
2054 return false;
2055
2056 while (b_elt)
2057 {
2058 /* If A lags behind B, just advance it. */
2059 if (!a_elt || a_elt->indx == b_elt->indx)
2060 {
2061 changed = bitmap_elt_ior (a, a_elt, a_prev, a_elt, b_elt, changed);
2062 b_elt = b_elt->next;
2063 }
2064 else if (a_elt->indx > b_elt->indx)
2065 {
2066 bitmap_element *b_elt_next = b_elt->next;
2067 bitmap_list_unlink_element (b, b_elt, false);
2068 bitmap_list_insert_element_after (a, a_prev, b_elt->indx, b_elt);
2069 b_elt = b_elt_next;
2070 }
2071
2072 a_prev = *a_prev_pnext;
2073 a_prev_pnext = &a_prev->next;
2074 a_elt = *a_prev_pnext;
2075 }
2076
2077 gcc_checking_assert (!a->current == !a->first);
2078 if (a->current)
2079 a->indx = a->current->indx;
2080
2081 if (b->obstack)
2082 BITMAP_FREE (*b_);
2083 else
2084 bitmap_clear (b);
2085 return changed;
2086 }
2087
2088 /* DST = A ^ B */
2089
2090 void
2091 bitmap_xor (bitmap dst, const_bitmap a, const_bitmap b)
2092 {
2093 bitmap_element *dst_elt = dst->first;
2094 const bitmap_element *a_elt = a->first;
2095 const bitmap_element *b_elt = b->first;
2096 bitmap_element *dst_prev = NULL;
2097
2098 gcc_checking_assert (!dst->tree_form && !a->tree_form && !b->tree_form);
2099 gcc_assert (dst != a && dst != b);
2100
2101 if (a == b)
2102 {
2103 bitmap_clear (dst);
2104 return;
2105 }
2106
2107 while (a_elt || b_elt)
2108 {
2109 if (a_elt && b_elt && a_elt->indx == b_elt->indx)
2110 {
2111 /* Matching elts, generate A ^ B. */
2112 unsigned ix;
2113 BITMAP_WORD ior = 0;
2114
2115 if (!dst_elt)
2116 dst_elt = bitmap_list_insert_element_after (dst, dst_prev,
2117 a_elt->indx);
2118 else
2119 dst_elt->indx = a_elt->indx;
2120 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
2121 {
2122 BITMAP_WORD r = a_elt->bits[ix] ^ b_elt->bits[ix];
2123
2124 ior |= r;
2125 dst_elt->bits[ix] = r;
2126 }
2127 a_elt = a_elt->next;
2128 b_elt = b_elt->next;
2129 if (ior)
2130 {
2131 dst_prev = dst_elt;
2132 dst_elt = dst_elt->next;
2133 }
2134 }
2135 else
2136 {
2137 /* Copy a single element. */
2138 const bitmap_element *src;
2139
2140 if (!b_elt || (a_elt && a_elt->indx < b_elt->indx))
2141 {
2142 src = a_elt;
2143 a_elt = a_elt->next;
2144 }
2145 else
2146 {
2147 src = b_elt;
2148 b_elt = b_elt->next;
2149 }
2150
2151 if (!dst_elt)
2152 dst_elt = bitmap_list_insert_element_after (dst, dst_prev,
2153 src->indx);
2154 else
2155 dst_elt->indx = src->indx;
2156 memcpy (dst_elt->bits, src->bits, sizeof (dst_elt->bits));
2157 dst_prev = dst_elt;
2158 dst_elt = dst_elt->next;
2159 }
2160 }
2161 /* Ensure that dst->current is valid. */
2162 dst->current = dst->first;
2163 bitmap_elt_clear_from (dst, dst_elt);
2164 gcc_checking_assert (!dst->current == !dst->first);
2165 if (dst->current)
2166 dst->indx = dst->current->indx;
2167 }
2168
2169 /* A ^= B */
2170
2171 void
2172 bitmap_xor_into (bitmap a, const_bitmap b)
2173 {
2174 bitmap_element *a_elt = a->first;
2175 const bitmap_element *b_elt = b->first;
2176 bitmap_element *a_prev = NULL;
2177
2178 gcc_checking_assert (!a->tree_form && !b->tree_form);
2179
2180 if (a == b)
2181 {
2182 bitmap_clear (a);
2183 return;
2184 }
2185
2186 while (b_elt)
2187 {
2188 if (!a_elt || b_elt->indx < a_elt->indx)
2189 {
2190 /* Copy b_elt. */
2191 bitmap_element *dst = bitmap_list_insert_element_after (a, a_prev,
2192 b_elt->indx);
2193 memcpy (dst->bits, b_elt->bits, sizeof (dst->bits));
2194 a_prev = dst;
2195 b_elt = b_elt->next;
2196 }
2197 else if (a_elt->indx < b_elt->indx)
2198 {
2199 a_prev = a_elt;
2200 a_elt = a_elt->next;
2201 }
2202 else
2203 {
2204 /* Matching elts, generate A ^= B. */
2205 unsigned ix;
2206 BITMAP_WORD ior = 0;
2207 bitmap_element *next = a_elt->next;
2208
2209 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
2210 {
2211 BITMAP_WORD r = a_elt->bits[ix] ^ b_elt->bits[ix];
2212
2213 ior |= r;
2214 a_elt->bits[ix] = r;
2215 }
2216 b_elt = b_elt->next;
2217 if (ior)
2218 a_prev = a_elt;
2219 else
2220 bitmap_list_unlink_element (a, a_elt);
2221 a_elt = next;
2222 }
2223 }
2224 gcc_checking_assert (!a->current == !a->first);
2225 if (a->current)
2226 a->indx = a->current->indx;
2227 }
2228
2229 /* Return true if two bitmaps are identical.
2230 We do not bother with a check for pointer equality, as that never
2231 occurs in practice. */
2232
2233 bool
2234 bitmap_equal_p (const_bitmap a, const_bitmap b)
2235 {
2236 const bitmap_element *a_elt;
2237 const bitmap_element *b_elt;
2238 unsigned ix;
2239
2240 gcc_checking_assert (!a->tree_form && !b->tree_form);
2241
2242 for (a_elt = a->first, b_elt = b->first;
2243 a_elt && b_elt;
2244 a_elt = a_elt->next, b_elt = b_elt->next)
2245 {
2246 if (a_elt->indx != b_elt->indx)
2247 return false;
2248 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
2249 if (a_elt->bits[ix] != b_elt->bits[ix])
2250 return false;
2251 }
2252 return !a_elt && !b_elt;
2253 }
2254
2255 /* Return true if A AND B is not empty. */
2256
2257 bool
2258 bitmap_intersect_p (const_bitmap a, const_bitmap b)
2259 {
2260 const bitmap_element *a_elt;
2261 const bitmap_element *b_elt;
2262 unsigned ix;
2263
2264 gcc_checking_assert (!a->tree_form && !b->tree_form);
2265
2266 for (a_elt = a->first, b_elt = b->first;
2267 a_elt && b_elt;)
2268 {
2269 if (a_elt->indx < b_elt->indx)
2270 a_elt = a_elt->next;
2271 else if (b_elt->indx < a_elt->indx)
2272 b_elt = b_elt->next;
2273 else
2274 {
2275 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
2276 if (a_elt->bits[ix] & b_elt->bits[ix])
2277 return true;
2278 a_elt = a_elt->next;
2279 b_elt = b_elt->next;
2280 }
2281 }
2282 return false;
2283 }
2284
2285 /* Return true if A AND NOT B is not empty. */
2286
2287 bool
2288 bitmap_intersect_compl_p (const_bitmap a, const_bitmap b)
2289 {
2290 const bitmap_element *a_elt;
2291 const bitmap_element *b_elt;
2292 unsigned ix;
2293
2294 gcc_checking_assert (!a->tree_form && !b->tree_form);
2295
2296 for (a_elt = a->first, b_elt = b->first;
2297 a_elt && b_elt;)
2298 {
2299 if (a_elt->indx < b_elt->indx)
2300 return true;
2301 else if (b_elt->indx < a_elt->indx)
2302 b_elt = b_elt->next;
2303 else
2304 {
2305 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
2306 if (a_elt->bits[ix] & ~b_elt->bits[ix])
2307 return true;
2308 a_elt = a_elt->next;
2309 b_elt = b_elt->next;
2310 }
2311 }
2312 return a_elt != NULL;
2313 }
2314
2315 \f
2316 /* DST = A | (FROM1 & ~FROM2). Return true if DST changes. */
2317
2318 bool
2319 bitmap_ior_and_compl (bitmap dst, const_bitmap a, const_bitmap b, const_bitmap kill)
2320 {
2321 bool changed = false;
2322
2323 bitmap_element *dst_elt = dst->first;
2324 const bitmap_element *a_elt = a->first;
2325 const bitmap_element *b_elt = b->first;
2326 const bitmap_element *kill_elt = kill->first;
2327 bitmap_element *dst_prev = NULL;
2328 bitmap_element **dst_prev_pnext = &dst->first;
2329
2330 gcc_checking_assert (!dst->tree_form && !a->tree_form && !b->tree_form
2331 && !kill->tree_form);
2332 gcc_assert (dst != a && dst != b && dst != kill);
2333
2334 /* Special cases. We don't bother checking for bitmap_equal_p (b, kill). */
2335 if (b == kill || bitmap_empty_p (b))
2336 {
2337 changed = !bitmap_equal_p (dst, a);
2338 if (changed)
2339 bitmap_copy (dst, a);
2340 return changed;
2341 }
2342 if (bitmap_empty_p (kill))
2343 return bitmap_ior (dst, a, b);
2344 if (bitmap_empty_p (a))
2345 return bitmap_and_compl (dst, b, kill);
2346
2347 while (a_elt || b_elt)
2348 {
2349 bool new_element = false;
2350
2351 if (b_elt)
2352 while (kill_elt && kill_elt->indx < b_elt->indx)
2353 kill_elt = kill_elt->next;
2354
2355 if (b_elt && kill_elt && kill_elt->indx == b_elt->indx
2356 && (!a_elt || a_elt->indx >= b_elt->indx))
2357 {
2358 bitmap_element tmp_elt;
2359 unsigned ix;
2360
2361 BITMAP_WORD ior = 0;
2362 tmp_elt.indx = b_elt->indx;
2363 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
2364 {
2365 BITMAP_WORD r = b_elt->bits[ix] & ~kill_elt->bits[ix];
2366 ior |= r;
2367 tmp_elt.bits[ix] = r;
2368 }
2369
2370 if (ior)
2371 {
2372 changed = bitmap_elt_ior (dst, dst_elt, dst_prev,
2373 a_elt, &tmp_elt, changed);
2374 new_element = true;
2375 if (a_elt && a_elt->indx == b_elt->indx)
2376 a_elt = a_elt->next;
2377 }
2378
2379 b_elt = b_elt->next;
2380 kill_elt = kill_elt->next;
2381 }
2382 else
2383 {
2384 changed = bitmap_elt_ior (dst, dst_elt, dst_prev,
2385 a_elt, b_elt, changed);
2386 new_element = true;
2387
2388 if (a_elt && b_elt && a_elt->indx == b_elt->indx)
2389 {
2390 a_elt = a_elt->next;
2391 b_elt = b_elt->next;
2392 }
2393 else
2394 {
2395 if (a_elt && (!b_elt || a_elt->indx <= b_elt->indx))
2396 a_elt = a_elt->next;
2397 else if (b_elt && (!a_elt || b_elt->indx <= a_elt->indx))
2398 b_elt = b_elt->next;
2399 }
2400 }
2401
2402 if (new_element)
2403 {
2404 dst_prev = *dst_prev_pnext;
2405 dst_prev_pnext = &dst_prev->next;
2406 dst_elt = *dst_prev_pnext;
2407 }
2408 }
2409
2410 if (dst_elt)
2411 {
2412 changed = true;
2413 /* Ensure that dst->current is valid. */
2414 dst->current = dst->first;
2415 bitmap_elt_clear_from (dst, dst_elt);
2416 }
2417 gcc_checking_assert (!dst->current == !dst->first);
2418 if (dst->current)
2419 dst->indx = dst->current->indx;
2420
2421 return changed;
2422 }
2423
2424 /* A |= (B & ~C). Return true if A changes. */
2425
2426 bool
2427 bitmap_ior_and_compl_into (bitmap a, const_bitmap b, const_bitmap c)
2428 {
2429 bitmap_element *a_elt = a->first;
2430 const bitmap_element *b_elt = b->first;
2431 const bitmap_element *c_elt = c->first;
2432 bitmap_element and_elt;
2433 bitmap_element *a_prev = NULL;
2434 bitmap_element **a_prev_pnext = &a->first;
2435 bool changed = false;
2436 unsigned ix;
2437
2438 gcc_checking_assert (!a->tree_form && !b->tree_form && !c->tree_form);
2439
2440 if (a == b)
2441 return false;
2442 if (bitmap_empty_p (c))
2443 return bitmap_ior_into (a, b);
2444 else if (bitmap_empty_p (a))
2445 return bitmap_and_compl (a, b, c);
2446
2447 and_elt.indx = -1;
2448 while (b_elt)
2449 {
2450 /* Advance C. */
2451 while (c_elt && c_elt->indx < b_elt->indx)
2452 c_elt = c_elt->next;
2453
2454 const bitmap_element *and_elt_ptr;
2455 if (c_elt && c_elt->indx == b_elt->indx)
2456 {
2457 BITMAP_WORD overall = 0;
2458 and_elt_ptr = &and_elt;
2459 and_elt.indx = b_elt->indx;
2460 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
2461 {
2462 and_elt.bits[ix] = b_elt->bits[ix] & ~c_elt->bits[ix];
2463 overall |= and_elt.bits[ix];
2464 }
2465 if (!overall)
2466 {
2467 b_elt = b_elt->next;
2468 continue;
2469 }
2470 }
2471 else
2472 and_elt_ptr = b_elt;
2473
2474 b_elt = b_elt->next;
2475
2476 /* Now find a place to insert AND_ELT. */
2477 do
2478 {
2479 ix = a_elt ? a_elt->indx : and_elt_ptr->indx;
2480 if (ix == and_elt_ptr->indx)
2481 changed = bitmap_elt_ior (a, a_elt, a_prev, a_elt,
2482 and_elt_ptr, changed);
2483 else if (ix > and_elt_ptr->indx)
2484 changed = bitmap_elt_copy (a, NULL, a_prev, and_elt_ptr, changed);
2485
2486 a_prev = *a_prev_pnext;
2487 a_prev_pnext = &a_prev->next;
2488 a_elt = *a_prev_pnext;
2489
2490 /* If A lagged behind B/C, we advanced it so loop once more. */
2491 }
2492 while (ix < and_elt_ptr->indx);
2493 }
2494
2495 gcc_checking_assert (!a->current == !a->first);
2496 if (a->current)
2497 a->indx = a->current->indx;
2498 return changed;
2499 }
2500
2501 /* A |= (B & C). Return true if A changes. */
2502
2503 bool
2504 bitmap_ior_and_into (bitmap a, const_bitmap b, const_bitmap c)
2505 {
2506 bitmap_element *a_elt = a->first;
2507 const bitmap_element *b_elt = b->first;
2508 const bitmap_element *c_elt = c->first;
2509 bitmap_element and_elt;
2510 bitmap_element *a_prev = NULL;
2511 bitmap_element **a_prev_pnext = &a->first;
2512 bool changed = false;
2513 unsigned ix;
2514
2515 gcc_checking_assert (!a->tree_form && !b->tree_form && !c->tree_form);
2516
2517 if (b == c)
2518 return bitmap_ior_into (a, b);
2519 if (bitmap_empty_p (b) || bitmap_empty_p (c))
2520 return false;
2521
2522 and_elt.indx = -1;
2523 while (b_elt && c_elt)
2524 {
2525 BITMAP_WORD overall;
2526
2527 /* Find a common item of B and C. */
2528 while (b_elt->indx != c_elt->indx)
2529 {
2530 if (b_elt->indx < c_elt->indx)
2531 {
2532 b_elt = b_elt->next;
2533 if (!b_elt)
2534 goto done;
2535 }
2536 else
2537 {
2538 c_elt = c_elt->next;
2539 if (!c_elt)
2540 goto done;
2541 }
2542 }
2543
2544 overall = 0;
2545 and_elt.indx = b_elt->indx;
2546 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
2547 {
2548 and_elt.bits[ix] = b_elt->bits[ix] & c_elt->bits[ix];
2549 overall |= and_elt.bits[ix];
2550 }
2551
2552 b_elt = b_elt->next;
2553 c_elt = c_elt->next;
2554 if (!overall)
2555 continue;
2556
2557 /* Now find a place to insert AND_ELT. */
2558 do
2559 {
2560 ix = a_elt ? a_elt->indx : and_elt.indx;
2561 if (ix == and_elt.indx)
2562 changed = bitmap_elt_ior (a, a_elt, a_prev, a_elt, &and_elt, changed);
2563 else if (ix > and_elt.indx)
2564 changed = bitmap_elt_copy (a, NULL, a_prev, &and_elt, changed);
2565
2566 a_prev = *a_prev_pnext;
2567 a_prev_pnext = &a_prev->next;
2568 a_elt = *a_prev_pnext;
2569
2570 /* If A lagged behind B/C, we advanced it so loop once more. */
2571 }
2572 while (ix < and_elt.indx);
2573 }
2574
2575 done:
2576 gcc_checking_assert (!a->current == !a->first);
2577 if (a->current)
2578 a->indx = a->current->indx;
2579 return changed;
2580 }
2581
2582 /* Compute hash of bitmap (for purposes of hashing). */
2583
2584 hashval_t
2585 bitmap_hash (const_bitmap head)
2586 {
2587 const bitmap_element *ptr;
2588 BITMAP_WORD hash = 0;
2589 int ix;
2590
2591 gcc_checking_assert (!head->tree_form);
2592
2593 for (ptr = head->first; ptr; ptr = ptr->next)
2594 {
2595 hash ^= ptr->indx;
2596 for (ix = 0; ix != BITMAP_ELEMENT_WORDS; ix++)
2597 hash ^= ptr->bits[ix];
2598 }
2599 return (hashval_t)hash;
2600 }
2601
2602 \f
2603 /* Function to obtain a vector of bitmap elements in bit order from
2604 HEAD in tree view. */
2605
2606 static void
2607 bitmap_tree_to_vec (vec<bitmap_element *> &elts, const_bitmap head)
2608 {
2609 gcc_checking_assert (head->tree_form);
2610 auto_vec<bitmap_element *, 32> stack;
2611 bitmap_element *e = head->first;
2612 while (true)
2613 {
2614 while (e != NULL)
2615 {
2616 stack.safe_push (e);
2617 e = e->prev;
2618 }
2619 if (stack.is_empty ())
2620 break;
2621
2622 e = stack.pop ();
2623 elts.safe_push (e);
2624 e = e->next;
2625 }
2626 }
2627
2628 /* Debugging function to print out the contents of a bitmap element. */
2629
2630 DEBUG_FUNCTION void
2631 debug_bitmap_elt_file (FILE *file, const bitmap_element *ptr)
2632 {
2633 unsigned int i, j, col = 26;
2634
2635 fprintf (file, "\t" HOST_PTR_PRINTF " next = " HOST_PTR_PRINTF
2636 " prev = " HOST_PTR_PRINTF " indx = %u\n\t\tbits = {",
2637 (const void*) ptr, (const void*) ptr->next,
2638 (const void*) ptr->prev, ptr->indx);
2639
2640 for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
2641 for (j = 0; j < BITMAP_WORD_BITS; j++)
2642 if ((ptr->bits[i] >> j) & 1)
2643 {
2644 if (col > 70)
2645 {
2646 fprintf (file, "\n\t\t\t");
2647 col = 24;
2648 }
2649
2650 fprintf (file, " %u", (ptr->indx * BITMAP_ELEMENT_ALL_BITS
2651 + i * BITMAP_WORD_BITS + j));
2652 col += 4;
2653 }
2654
2655 fprintf (file, " }\n");
2656 }
2657
2658 /* Debugging function to print out the contents of a bitmap. */
2659
2660 DEBUG_FUNCTION void
2661 debug_bitmap_file (FILE *file, const_bitmap head)
2662 {
2663 const bitmap_element *ptr;
2664
2665 fprintf (file, "\nfirst = " HOST_PTR_PRINTF
2666 " current = " HOST_PTR_PRINTF " indx = %u\n",
2667 (void *) head->first, (void *) head->current, head->indx);
2668
2669 if (head->tree_form)
2670 {
2671 auto_vec<bitmap_element *, 32> elts;
2672 bitmap_tree_to_vec (elts, head);
2673 for (unsigned i = 0; i < elts.length (); ++i)
2674 debug_bitmap_elt_file (file, elts[i]);
2675 }
2676 else
2677 for (ptr = head->first; ptr; ptr = ptr->next)
2678 debug_bitmap_elt_file (file, ptr);
2679 }
2680
2681 /* Function to be called from the debugger to print the contents
2682 of a bitmap. */
2683
2684 DEBUG_FUNCTION void
2685 debug_bitmap (const_bitmap head)
2686 {
2687 debug_bitmap_file (stderr, head);
2688 }
2689
2690 /* Function to print out the contents of a bitmap. Unlike debug_bitmap_file,
2691 it does not print anything but the bits. */
2692
2693 DEBUG_FUNCTION void
2694 bitmap_print (FILE *file, const_bitmap head, const char *prefix,
2695 const char *suffix)
2696 {
2697 const char *comma = "";
2698 unsigned i;
2699
2700 fputs (prefix, file);
2701 if (head->tree_form)
2702 {
2703 auto_vec<bitmap_element *, 32> elts;
2704 bitmap_tree_to_vec (elts, head);
2705 for (i = 0; i < elts.length (); ++i)
2706 for (unsigned ix = 0; ix != BITMAP_ELEMENT_WORDS; ++ix)
2707 {
2708 BITMAP_WORD word = elts[i]->bits[ix];
2709 for (unsigned bit = 0; bit != BITMAP_WORD_BITS; ++bit)
2710 if (word & ((BITMAP_WORD)1 << bit))
2711 {
2712 fprintf (file, "%s%d", comma,
2713 (bit + BITMAP_WORD_BITS * ix
2714 + elts[i]->indx * BITMAP_ELEMENT_ALL_BITS));
2715 comma = ", ";
2716 }
2717 }
2718 }
2719 else
2720 {
2721 bitmap_iterator bi;
2722 EXECUTE_IF_SET_IN_BITMAP (head, 0, i, bi)
2723 {
2724 fprintf (file, "%s%d", comma, i);
2725 comma = ", ";
2726 }
2727 }
2728 fputs (suffix, file);
2729 }
2730
2731 /* Output per-bitmap memory usage statistics. */
2732 void
2733 dump_bitmap_statistics (void)
2734 {
2735 if (!GATHER_STATISTICS)
2736 return;
2737
2738 bitmap_mem_desc.dump (BITMAP_ORIGIN);
2739 }
2740
2741 DEBUG_FUNCTION void
2742 debug (const bitmap_head &ref)
2743 {
2744 dump_bitmap (stderr, &ref);
2745 }
2746
2747 DEBUG_FUNCTION void
2748 debug (const bitmap_head *ptr)
2749 {
2750 if (ptr)
2751 debug (*ptr);
2752 else
2753 fprintf (stderr, "<nil>\n");
2754 }
2755
2756 void
2757 bitmap_head::dump ()
2758 {
2759 debug (this);
2760 }
2761
2762 #if CHECKING_P
2763
2764 namespace selftest {
2765
2766 /* Selftests for bitmaps. */
2767
2768 /* Freshly-created bitmaps ought to be empty. */
2769
2770 static void
2771 test_gc_alloc ()
2772 {
2773 bitmap b = bitmap_gc_alloc ();
2774 ASSERT_TRUE (bitmap_empty_p (b));
2775 }
2776
2777 /* Verify bitmap_set_range. */
2778
2779 static void
2780 test_set_range ()
2781 {
2782 bitmap b = bitmap_gc_alloc ();
2783 ASSERT_TRUE (bitmap_empty_p (b));
2784
2785 bitmap_set_range (b, 7, 5);
2786 ASSERT_FALSE (bitmap_empty_p (b));
2787 ASSERT_EQ (5, bitmap_count_bits (b));
2788
2789 /* Verify bitmap_bit_p at the boundaries. */
2790 ASSERT_FALSE (bitmap_bit_p (b, 6));
2791 ASSERT_TRUE (bitmap_bit_p (b, 7));
2792 ASSERT_TRUE (bitmap_bit_p (b, 11));
2793 ASSERT_FALSE (bitmap_bit_p (b, 12));
2794 }
2795
2796 /* Verify splitting a range into two pieces using bitmap_clear_bit. */
2797
2798 static void
2799 test_clear_bit_in_middle ()
2800 {
2801 bitmap b = bitmap_gc_alloc ();
2802
2803 /* Set b to [100..200]. */
2804 bitmap_set_range (b, 100, 100);
2805 ASSERT_EQ (100, bitmap_count_bits (b));
2806
2807 /* Clear a bit in the middle. */
2808 bool changed = bitmap_clear_bit (b, 150);
2809 ASSERT_TRUE (changed);
2810 ASSERT_EQ (99, bitmap_count_bits (b));
2811 ASSERT_TRUE (bitmap_bit_p (b, 149));
2812 ASSERT_FALSE (bitmap_bit_p (b, 150));
2813 ASSERT_TRUE (bitmap_bit_p (b, 151));
2814 }
2815
2816 /* Verify bitmap_copy. */
2817
2818 static void
2819 test_copying ()
2820 {
2821 bitmap src = bitmap_gc_alloc ();
2822 bitmap_set_range (src, 40, 10);
2823
2824 bitmap dst = bitmap_gc_alloc ();
2825 ASSERT_FALSE (bitmap_equal_p (src, dst));
2826 bitmap_copy (dst, src);
2827 ASSERT_TRUE (bitmap_equal_p (src, dst));
2828
2829 /* Verify that we can make them unequal again... */
2830 bitmap_set_range (src, 70, 5);
2831 ASSERT_FALSE (bitmap_equal_p (src, dst));
2832
2833 /* ...and that changing src after the copy didn't affect
2834 the other: */
2835 ASSERT_FALSE (bitmap_bit_p (dst, 70));
2836 }
2837
2838 /* Verify bitmap_single_bit_set_p. */
2839
2840 static void
2841 test_bitmap_single_bit_set_p ()
2842 {
2843 bitmap b = bitmap_gc_alloc ();
2844
2845 ASSERT_FALSE (bitmap_single_bit_set_p (b));
2846
2847 bitmap_set_range (b, 42, 1);
2848 ASSERT_TRUE (bitmap_single_bit_set_p (b));
2849 ASSERT_EQ (42, bitmap_first_set_bit (b));
2850
2851 bitmap_set_range (b, 1066, 1);
2852 ASSERT_FALSE (bitmap_single_bit_set_p (b));
2853 ASSERT_EQ (42, bitmap_first_set_bit (b));
2854
2855 bitmap_clear_range (b, 0, 100);
2856 ASSERT_TRUE (bitmap_single_bit_set_p (b));
2857 ASSERT_EQ (1066, bitmap_first_set_bit (b));
2858 }
2859
2860 /* Run all of the selftests within this file. */
2861
2862 void
2863 bitmap_c_tests ()
2864 {
2865 test_gc_alloc ();
2866 test_set_range ();
2867 test_clear_bit_in_middle ();
2868 test_copying ();
2869 test_bitmap_single_bit_set_p ();
2870 }
2871
2872 } // namespace selftest
2873 #endif /* CHECKING_P */
2874
2875 #include "gt-bitmap.h"