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