]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/bitmap.c
Correct a function pre/postcondition [PR102403].
[thirdparty/gcc.git] / gcc / bitmap.c
CommitLineData
096ab9ea 1/* Functions to support general ended bitmaps.
99dee823 2 Copyright (C) 1997-2021 Free Software Foundation, Inc.
096ab9ea 3
1322177d 4This file is part of GCC.
096ab9ea 5
1322177d
LB
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
9dcd6f09 8Software Foundation; either version 3, or (at your option) any later
1322177d 9version.
096ab9ea 10
1322177d
LB
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
096ab9ea
RK
15
16You should have received a copy of the GNU General Public License
9dcd6f09
NC
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
096ab9ea 19
096ab9ea 20#include "config.h"
670ee920 21#include "system.h"
4977bab6 22#include "coretypes.h"
efc9bd41 23#include "bitmap.h"
d9b950dd 24#include "selftest.h"
f75709c6 25
2d44c7de
ML
26/* Memory allocation statistics purpose instance. */
27mem_alloc_description<bitmap_usage> bitmap_mem_desc;
f75709c6 28
1c252ef3
RB
29/* Static zero-initialized bitmap obstack used for default initialization
30 of bitmap_head. */
31bitmap_obstack bitmap_head::crashme;
32
d1e14d97
SB
33static bitmap_element *bitmap_tree_listify_from (bitmap, bitmap_element *);
34
f75709c6
JH
35/* Register new bitmap. */
36void
37bitmap_register (bitmap b MEM_STAT_DECL)
38{
7664eeb7
ML
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);
f75709c6
JH
45}
46
47/* Account the overhead. */
48static void
43331dfb 49register_overhead (bitmap b, size_t amount)
f75709c6 50{
7664eeb7
ML
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. */
57static void
58release_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);
f75709c6 63}
096ab9ea 64
7664eeb7 65
096ab9ea 66/* Global data */
7932a3db
NS
67bitmap_element bitmap_zero_bits; /* An element of all zero bits. */
68bitmap_obstack bitmap_default_obstack; /* The default bitmap obstack. */
a68ab351 69static int bitmap_default_obstack_depth;
7932a3db
NS
70static GTY((deletable)) bitmap_element *bitmap_ggc_free; /* Freelist of
71 GC'd elements. */
096ab9ea 72
096ab9ea 73\f
d1e14d97 74/* Bitmap memory management. */
88c4f655 75
d1e14d97 76/* Add ELT to the appropriate freelist. */
47c321d4 77static inline void
4682ae04 78bitmap_elem_to_freelist (bitmap head, bitmap_element *elt)
e2500fed 79{
7932a3db 80 bitmap_obstack *bit_obstack = head->obstack;
c22cacf3 81
d1e14d97 82 if (GATHER_STATISTICS)
7664eeb7 83 release_overhead (head, sizeof (bitmap_element), false);
d1e14d97 84
5765e552 85 elt->next = NULL;
a30fe4b6 86 elt->indx = -1;
7932a3db 87 if (bit_obstack)
e2500fed 88 {
5765e552 89 elt->prev = bit_obstack->elements;
7932a3db 90 bit_obstack->elements = elt;
e2500fed
GK
91 }
92 else
93 {
5765e552 94 elt->prev = bitmap_ggc_free;
e2500fed
GK
95 bitmap_ggc_free = elt;
96 }
97}
98
096ab9ea
RK
99/* Allocate a bitmap element. The bits are cleared, but nothing else is. */
100
47c321d4 101static inline bitmap_element *
4682ae04 102bitmap_element_allocate (bitmap head)
096ab9ea
RK
103{
104 bitmap_element *element;
7932a3db 105 bitmap_obstack *bit_obstack = head->obstack;
c22cacf3 106
7932a3db 107 if (bit_obstack)
22fa5b8a 108 {
7932a3db 109 element = bit_obstack->elements;
c22cacf3 110
7932a3db 111 if (element)
5765e552
KZ
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;
e2500fed 122 else
7932a3db 123 element = XOBNEW (&bit_obstack->obstack, bitmap_element);
e2500fed
GK
124 }
125 else
126 {
7932a3db
NS
127 element = bitmap_ggc_free;
128 if (element)
5765e552
KZ
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;
e2500fed 139 else
766090c2 140 element = ggc_alloc<bitmap_element> ();
22fa5b8a 141 }
096ab9ea 142
7aa6d18a
SB
143 if (GATHER_STATISTICS)
144 register_overhead (head, sizeof (bitmap_element));
145
a615c28a 146 memset (element->bits, 0, sizeof (element->bits));
096ab9ea
RK
147
148 return element;
149}
150
d1e14d97
SB
151/* Remove ELT and all following elements from bitmap HEAD.
152 Put the released elements in the freelist for HEAD. */
87e08c69
RH
153
154void
7932a3db
NS
155bitmap_elt_clear_from (bitmap head, bitmap_element *elt)
156{
5765e552
KZ
157 bitmap_element *prev;
158 bitmap_obstack *bit_obstack = head->obstack;
159
d1e14d97
SB
160 if (!elt)
161 return;
162
163 if (head->tree_form)
164 elt = bitmap_tree_listify_from (head, elt);
7aa6d18a
SB
165
166 if (GATHER_STATISTICS)
167 {
168 int n = 0;
169 for (prev = elt; prev; prev = prev->next)
170 n++;
7664eeb7 171 release_overhead (head, sizeof (bitmap_element) * n, false);
7aa6d18a 172 }
7932a3db 173
5765e552
KZ
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 }
c22cacf3 183 }
5765e552
KZ
184 else
185 {
186 head->first = NULL;
187 head->current = NULL;
188 head->indx = 0;
189 }
190
d1e14d97 191 /* Put the entire list onto the freelist in one operation. */
5765e552 192 if (bit_obstack)
7932a3db 193 {
c22cacf3 194 elt->prev = bit_obstack->elements;
5765e552
KZ
195 bit_obstack->elements = elt;
196 }
197 else
198 {
d1e14d97
SB
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
211static inline void
212bitmap_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
269static inline void
029ca388
RB
270bitmap_list_unlink_element (bitmap head, bitmap_element *element,
271 bool to_freelist = true)
d1e14d97
SB
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
029ca388
RB
298 if (to_freelist)
299 bitmap_elem_to_freelist (head, element);
d1e14d97
SB
300}
301
029ca388
RB
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. */
d1e14d97
SB
305
306static bitmap_element *
307bitmap_list_insert_element_after (bitmap head,
029ca388
RB
308 bitmap_element *elt, unsigned int indx,
309 bitmap_element *node = NULL)
d1e14d97 310{
029ca388
RB
311 if (!node)
312 node = bitmap_element_allocate (head);
d1e14d97
SB
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
347static inline bitmap_element *
348bitmap_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
427static inline void
428bitmap_tree_link_left (bitmap_element * &t, bitmap_element * &l)
429{
430 l->next = t;
431 l = t;
432 t = t->next;
433}
434
435static inline void
436bitmap_tree_link_right (bitmap_element * &t, bitmap_element * &r)
437{
438 r->prev = t;
439 r = t;
440 t = t->prev;
441}
442
443static inline void
444bitmap_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
452static inline void
453bitmap_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
461static bitmap_element *
462bitmap_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
508static inline void
509bitmap_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
539static void
540bitmap_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
562static inline bitmap_element *
563bitmap_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
596static bitmap_element *
597bitmap_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
664void
665bitmap_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);
7932a3db 678 }
d1e14d97
SB
679
680 head->tree_form = false;
896db49a
RB
681 if (!head->current)
682 {
683 head->current = head->first;
684 head->indx = head->current ? head->current->indx : 0;
685 }
7932a3db
NS
686}
687
d1e14d97
SB
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
693void
694bitmap_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. */
7932a3db 711
5fd8300b 712void
7932a3db 713bitmap_clear (bitmap head)
87e08c69 714{
d1e14d97
SB
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);
7932a3db
NS
727}
728\f
729/* Initialize a bitmap obstack. If BIT_OBSTACK is NULL, initialize
730 the default bitmap obstack. */
731
732void
733bitmap_obstack_initialize (bitmap_obstack *bit_obstack)
734{
735 if (!bit_obstack)
a68ab351
JJ
736 {
737 if (bitmap_default_obstack_depth++)
738 return;
739 bit_obstack = &bitmap_default_obstack;
740 }
7932a3db
NS
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);
87e08c69
RH
752}
753
7932a3db
NS
754/* Release the memory from a bitmap obstack. If BIT_OBSTACK is NULL,
755 release the default bitmap obstack. */
756
757void
758bitmap_obstack_release (bitmap_obstack *bit_obstack)
759{
760 if (!bit_obstack)
a68ab351
JJ
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 }
c22cacf3 769
7932a3db
NS
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
778bitmap
3fe793df 779bitmap_alloc (bitmap_obstack *bit_obstack MEM_STAT_DECL)
7932a3db
NS
780{
781 bitmap map;
782
783 if (!bit_obstack)
784 bit_obstack = &bitmap_default_obstack;
785 map = bit_obstack->heads;
786 if (map)
99b1c316 787 bit_obstack->heads = (class bitmap_head *) map->first;
7932a3db
NS
788 else
789 map = XOBNEW (&bit_obstack->obstack, bitmap_head);
2a1a5f30 790 bitmap_initialize (map, bit_obstack PASS_MEM_STAT);
7aa6d18a
SB
791
792 if (GATHER_STATISTICS)
793 register_overhead (map, sizeof (bitmap_head));
7932a3db
NS
794
795 return map;
796}
797
798/* Create a new GCd bitmap. */
799
800bitmap
3fe793df 801bitmap_gc_alloc (ALONE_MEM_STAT_DECL)
7932a3db
NS
802{
803 bitmap map;
804
766090c2 805 map = ggc_alloc<bitmap_head> ();
2a1a5f30 806 bitmap_initialize (map, NULL PASS_MEM_STAT);
7aa6d18a
SB
807
808 if (GATHER_STATISTICS)
809 register_overhead (map, sizeof (bitmap_head));
7932a3db
NS
810
811 return map;
812}
813
7932a3db
NS
814/* Release an obstack allocated bitmap. */
815
816void
817bitmap_obstack_free (bitmap map)
818{
cc175e7c
NS
819 if (map)
820 {
821 bitmap_clear (map);
dba2cc0c 822 map->first = (bitmap_element *) map->obstack->heads;
7aa6d18a
SB
823
824 if (GATHER_STATISTICS)
7664eeb7 825 release_overhead (map, sizeof (bitmap_head), true);
7aa6d18a 826
cc175e7c
NS
827 map->obstack->heads = map;
828 }
7932a3db
NS
829}
830
7932a3db 831\f
096ab9ea
RK
832/* Return nonzero if all bits in an element are zero. */
833
47c321d4 834static inline int
e326eeb5 835bitmap_element_zerop (const bitmap_element *element)
096ab9ea
RK
836{
837#if BITMAP_ELEMENT_WORDS == 2
838 return (element->bits[0] | element->bits[1]) == 0;
839#else
65a6f342 840 unsigned i;
096ab9ea
RK
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
a615c28a 850/* Copy a bitmap to another bitmap. */
096ab9ea
RK
851
852void
e326eeb5 853bitmap_copy (bitmap to, const_bitmap from)
096ab9ea 854{
e326eeb5
KG
855 const bitmap_element *from_ptr;
856 bitmap_element *to_ptr = 0;
096ab9ea 857
d1e14d97
SB
858 gcc_checking_assert (!to->tree_form && !from->tree_form);
859
096ab9ea
RK
860 bitmap_clear (to);
861
f9da5064 862 /* Copy elements in forward direction one at a time. */
096ab9ea
RK
863 for (from_ptr = from->first; from_ptr; from_ptr = from_ptr->next)
864 {
e2500fed 865 bitmap_element *to_elt = bitmap_element_allocate (to);
096ab9ea
RK
866
867 to_elt->indx = from_ptr->indx;
fd6132db 868 memcpy (to_elt->bits, from_ptr->bits, sizeof (to_elt->bits));
096ab9ea 869
d1e14d97
SB
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. */
096ab9ea
RK
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}
43331dfb
RB
889
890/* Move a bitmap to another bitmap. */
891
892void
893bitmap_move (bitmap to, bitmap from)
894{
895 gcc_assert (to->obstack == from->obstack);
896
897 bitmap_clear (to);
898
7664eeb7 899 size_t sz = 0;
43331dfb
RB
900 if (GATHER_STATISTICS)
901 {
43331dfb
RB
902 for (bitmap_element *e = to->first; e; e = e->next)
903 sz += sizeof (bitmap_element);
904 register_overhead (to, sz);
43331dfb 905 }
7664eeb7
ML
906
907 *to = *from;
908
909 if (GATHER_STATISTICS)
910 release_overhead (from, sz, false);
43331dfb 911}
096ab9ea 912\f
5f0d975b 913/* Clear a single bit in a bitmap. Return true if the bit changed. */
096ab9ea 914
5f0d975b 915bool
4682ae04 916bitmap_clear_bit (bitmap head, int bit)
096ab9ea 917{
d1e14d97
SB
918 unsigned int indx = bit / BITMAP_ELEMENT_ALL_BITS;
919 bitmap_element *ptr;
096ab9ea 920
d1e14d97
SB
921 if (!head->tree_form)
922 ptr = bitmap_list_find_element (head, indx);
923 else
924 ptr = bitmap_tree_find_element (head, indx);
096ab9ea
RK
925 if (ptr != 0)
926 {
72e42e26
SB
927 unsigned bit_num = bit % BITMAP_WORD_BITS;
928 unsigned word_num = bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
5f0d975b
RG
929 BITMAP_WORD bit_val = ((BITMAP_WORD) 1) << bit_num;
930 bool res = (ptr->bits[word_num] & bit_val) != 0;
931 if (res)
07309d58
UB
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))
d1e14d97
SB
937 {
938 if (!head->tree_form)
939 bitmap_list_unlink_element (head, ptr);
940 else
941 bitmap_tree_unlink_element (head, ptr);
942 }
07309d58 943 }
5f0d975b
RG
944
945 return res;
096ab9ea 946 }
5f0d975b
RG
947
948 return false;
096ab9ea
RK
949}
950
5f0d975b 951/* Set a single bit in a bitmap. Return true if the bit changed. */
096ab9ea 952
5f0d975b 953bool
4682ae04 954bitmap_set_bit (bitmap head, int bit)
096ab9ea 955{
d1e14d97
SB
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);
72e42e26
SB
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;
096ab9ea 965
d1e14d97 966 if (ptr != 0)
5f0d975b
RG
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 }
d1e14d97
SB
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;
096ab9ea 982}
a615c28a 983
096ab9ea
RK
984/* Return whether a bit is set within a bitmap. */
985
986int
148909bc 987bitmap_bit_p (const_bitmap head, int bit)
096ab9ea 988{
d1e14d97 989 unsigned int indx = bit / BITMAP_ELEMENT_ALL_BITS;
148909bc 990 const bitmap_element *ptr;
096ab9ea
RK
991 unsigned bit_num;
992 unsigned word_num;
993
d1e14d97 994 if (!head->tree_form)
148909bc 995 ptr = bitmap_list_find_element (const_cast<bitmap> (head), indx);
d1e14d97 996 else
148909bc 997 ptr = bitmap_tree_find_element (const_cast<bitmap> (head), indx);
096ab9ea
RK
998 if (ptr == 0)
999 return 0;
1000
72e42e26
SB
1001 bit_num = bit % BITMAP_WORD_BITS;
1002 word_num = bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
096ab9ea 1003
8229306b 1004 return (ptr->bits[word_num] >> bit_num) & 1;
096ab9ea
RK
1005}
1006\f
5ad089a3
AM
1007/* Set CHUNK_SIZE bits at a time in bitmap HEAD.
1008 Store CHUNK_VALUE starting at bits CHUNK * chunk_size.
1009 This is the set routine for viewing bitmap as a multi-bit sparse array. */
1010
1011void
1012bitmap_set_aligned_chunk (bitmap head, unsigned int chunk,
1013 unsigned int chunk_size, BITMAP_WORD chunk_value)
1014{
1015 // Ensure chunk size is a power of 2 and fits in BITMAP_WORD.
1016 gcc_checking_assert (pow2p_hwi (chunk_size));
1017 gcc_checking_assert (chunk_size < (sizeof (BITMAP_WORD) * CHAR_BIT));
1018
1019 // Ensure chunk_value is within range of chunk_size bits.
1020 BITMAP_WORD max_value = (1 << chunk_size) - 1;
1021 gcc_checking_assert (chunk_value <= max_value);
1022
1023 unsigned bit = chunk * chunk_size;
1024 unsigned indx = bit / BITMAP_ELEMENT_ALL_BITS;
1025 bitmap_element *ptr;
1026 if (!head->tree_form)
1027 ptr = bitmap_list_find_element (head, indx);
1028 else
1029 ptr = bitmap_tree_find_element (head, indx);
1030 unsigned word_num = bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
1031 unsigned bit_num = bit % BITMAP_WORD_BITS;
1032 BITMAP_WORD bit_val = chunk_value << bit_num;
1033 BITMAP_WORD mask = ~(max_value << bit_num);
1034
1035 if (ptr != 0)
1036 {
1037 ptr->bits[word_num] &= mask;
1038 ptr->bits[word_num] |= bit_val;
1039 return;
1040 }
1041
1042 ptr = bitmap_element_allocate (head);
1043 ptr->indx = bit / BITMAP_ELEMENT_ALL_BITS;
1044 ptr->bits[word_num] = bit_val;
1045 if (!head->tree_form)
1046 bitmap_list_link_element (head, ptr);
1047 else
1048 bitmap_tree_link_element (head, ptr);
1049}
1050
1051/* This is the get routine for viewing bitmap as a multi-bit sparse array.
1052 Return a set of CHUNK_SIZE consecutive bits from HEAD, starting at bit
1053 CHUNK * chunk_size. */
1054
1055BITMAP_WORD
1056bitmap_get_aligned_chunk (const_bitmap head, unsigned int chunk,
1057 unsigned int chunk_size)
1058{
1059 // Ensure chunk size is a power of 2, fits in BITMAP_WORD and is in range.
1060 gcc_checking_assert (pow2p_hwi (chunk_size));
1061 gcc_checking_assert (chunk_size < (sizeof (BITMAP_WORD) * CHAR_BIT));
1062
1063 BITMAP_WORD max_value = (1 << chunk_size) - 1;
1064 unsigned bit = chunk * chunk_size;
1065 unsigned int indx = bit / BITMAP_ELEMENT_ALL_BITS;
1066 const bitmap_element *ptr;
1067 unsigned bit_num;
1068 unsigned word_num;
1069
1070 if (!head->tree_form)
1071 ptr = bitmap_list_find_element (const_cast<bitmap> (head), indx);
1072 else
1073 ptr = bitmap_tree_find_element (const_cast<bitmap> (head), indx);
1074 if (ptr == 0)
1075 return 0;
1076
1077 bit_num = bit % BITMAP_WORD_BITS;
1078 word_num = bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
1079
1080 // Return 4 bits.
1081 return (ptr->bits[word_num] >> bit_num) & max_value;
1082}
1083\f
1bc40c7e
KZ
1084#if GCC_VERSION < 3400
1085/* Table of number of set bits in a character, indexed by value of char. */
e326eeb5 1086static const unsigned char popcount_table[] =
1bc40c7e
KZ
1087{
1088 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,
1089 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,
1090 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,
1091 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,
1092 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,
1093 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,
1094 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,
1095 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,
1096};
1097
1098static unsigned long
1099bitmap_popcount (BITMAP_WORD a)
1100{
1101 unsigned long ret = 0;
1102 unsigned i;
1103
1104 /* Just do this the table way for now */
1105 for (i = 0; i < BITMAP_WORD_BITS; i+= 8)
1106 ret += popcount_table[(a >> i) & 0xff];
1107 return ret;
1108}
1109#endif
478baf91
JL
1110
1111/* Count and return the number of bits set in the bitmap word BITS. */
1112static unsigned long
1113bitmap_count_bits_in_word (const BITMAP_WORD *bits)
1114{
1115 unsigned long count = 0;
1116
1117 for (unsigned ix = 0; ix != BITMAP_ELEMENT_WORDS; ix++)
1118 {
1119#if GCC_VERSION >= 3400
1120 /* Note that popcountl matches BITMAP_WORD in type, so the actual size
1121 of BITMAP_WORD is not material. */
1122 count += __builtin_popcountl (bits[ix]);
1123#else
1124 count += bitmap_popcount (bits[ix]);
1125#endif
1126 }
1127 return count;
1128}
1129
1bc40c7e
KZ
1130/* Count the number of bits set in the bitmap, and return it. */
1131
1132unsigned long
e326eeb5 1133bitmap_count_bits (const_bitmap a)
1bc40c7e
KZ
1134{
1135 unsigned long count = 0;
e326eeb5 1136 const bitmap_element *elt;
1bc40c7e 1137
d1e14d97 1138 gcc_checking_assert (!a->tree_form);
1bc40c7e 1139 for (elt = a->first; elt; elt = elt->next)
478baf91
JL
1140 count += bitmap_count_bits_in_word (elt->bits);
1141
1142 return count;
1143}
1144
1145/* Count the number of unique bits set in A and B and return it. */
1146
1147unsigned long
1148bitmap_count_unique_bits (const_bitmap a, const_bitmap b)
1149{
1150 unsigned long count = 0;
1151 const bitmap_element *elt_a, *elt_b;
1152
1153 for (elt_a = a->first, elt_b = b->first; elt_a && elt_b; )
1bc40c7e 1154 {
478baf91
JL
1155 /* If we're at different indices, then count all the bits
1156 in the lower element. If we're at the same index, then
1157 count the bits in the IOR of the two elements. */
1158 if (elt_a->indx < elt_b->indx)
1bc40c7e 1159 {
478baf91
JL
1160 count += bitmap_count_bits_in_word (elt_a->bits);
1161 elt_a = elt_a->next;
1162 }
1163 else if (elt_b->indx < elt_a->indx)
1164 {
1165 count += bitmap_count_bits_in_word (elt_b->bits);
1166 elt_b = elt_b->next;
1167 }
1168 else
1169 {
1170 BITMAP_WORD bits[BITMAP_ELEMENT_WORDS];
1171 for (unsigned ix = 0; ix != BITMAP_ELEMENT_WORDS; ix++)
1172 bits[ix] = elt_a->bits[ix] | elt_b->bits[ix];
1173 count += bitmap_count_bits_in_word (bits);
1174 elt_a = elt_a->next;
1175 elt_b = elt_b->next;
1bc40c7e
KZ
1176 }
1177 }
1178 return count;
1179}
c22cacf3 1180
76e910c6
RG
1181/* Return true if the bitmap has a single bit set. Otherwise return
1182 false. */
1183
1184bool
1185bitmap_single_bit_set_p (const_bitmap a)
1186{
1187 unsigned long count = 0;
1188 const bitmap_element *elt;
1189 unsigned ix;
1190
1191 if (bitmap_empty_p (a))
1192 return false;
1193
1194 elt = a->first;
d1e14d97 1195
76e910c6
RG
1196 /* As there are no completely empty bitmap elements, a second one
1197 means we have more than one bit set. */
d1e14d97
SB
1198 if (elt->next != NULL
1199 && (!a->tree_form || elt->prev != NULL))
76e910c6
RG
1200 return false;
1201
1202 for (ix = 0; ix != BITMAP_ELEMENT_WORDS; ix++)
1203 {
1204#if GCC_VERSION >= 3400
1205 /* Note that popcountl matches BITMAP_WORD in type, so the actual size
1206 of BITMAP_WORD is not material. */
1207 count += __builtin_popcountl (elt->bits[ix]);
1208#else
1209 count += bitmap_popcount (elt->bits[ix]);
1210#endif
1211 if (count > 1)
1212 return false;
1213 }
1214
1215 return count == 1;
1216}
1bc40c7e
KZ
1217
1218
65a6f342
NS
1219/* Return the bit number of the first set bit in the bitmap. The
1220 bitmap must be non-empty. */
87e08c69 1221
65a6f342 1222unsigned
e326eeb5 1223bitmap_first_set_bit (const_bitmap a)
87e08c69 1224{
e326eeb5 1225 const bitmap_element *elt = a->first;
65a6f342 1226 unsigned bit_no;
72e42e26 1227 BITMAP_WORD word;
65a6f342 1228 unsigned ix;
c22cacf3 1229
377002a9 1230 gcc_checking_assert (elt);
d1e14d97
SB
1231
1232 if (a->tree_form)
1233 while (elt->prev)
1234 elt = elt->prev;
1235
65a6f342
NS
1236 bit_no = elt->indx * BITMAP_ELEMENT_ALL_BITS;
1237 for (ix = 0; ix != BITMAP_ELEMENT_WORDS; ix++)
1238 {
1239 word = elt->bits[ix];
1240 if (word)
1241 goto found_bit;
1242 }
298e6adc 1243 gcc_unreachable ();
65a6f342
NS
1244 found_bit:
1245 bit_no += ix * BITMAP_WORD_BITS;
87e08c69 1246
65a6f342 1247#if GCC_VERSION >= 3004
c3284718 1248 gcc_assert (sizeof (long) == sizeof (word));
65a6f342 1249 bit_no += __builtin_ctzl (word);
87e08c69 1250#else
65a6f342
NS
1251 /* Binary search for the first set bit. */
1252#if BITMAP_WORD_BITS > 64
1253#error "Fill out the table."
87e08c69 1254#endif
65a6f342
NS
1255#if BITMAP_WORD_BITS > 32
1256 if (!(word & 0xffffffff))
1257 word >>= 32, bit_no += 32;
87e08c69 1258#endif
65a6f342
NS
1259 if (!(word & 0xffff))
1260 word >>= 16, bit_no += 16;
1261 if (!(word & 0xff))
1262 word >>= 8, bit_no += 8;
1263 if (!(word & 0xf))
1264 word >>= 4, bit_no += 4;
1265 if (!(word & 0x3))
1266 word >>= 2, bit_no += 2;
1267 if (!(word & 0x1))
1268 word >>= 1, bit_no += 1;
c22cacf3 1269
377002a9 1270 gcc_checking_assert (word & 1);
87e08c69 1271#endif
65a6f342 1272 return bit_no;
87e08c69 1273}
12802c2b
JH
1274
1275/* Return the bit number of the first set bit in the bitmap. The
1276 bitmap must be non-empty. */
1277
1278unsigned
1279bitmap_last_set_bit (const_bitmap a)
1280{
d1e14d97 1281 const bitmap_element *elt;
12802c2b
JH
1282 unsigned bit_no;
1283 BITMAP_WORD word;
1284 int ix;
1285
d1e14d97
SB
1286 if (a->tree_form)
1287 elt = a->first;
1288 else
1289 elt = a->current ? a->current : a->first;
377002a9 1290 gcc_checking_assert (elt);
d1e14d97 1291
12802c2b
JH
1292 while (elt->next)
1293 elt = elt->next;
d1e14d97 1294
12802c2b 1295 bit_no = elt->indx * BITMAP_ELEMENT_ALL_BITS;
566422e0 1296 for (ix = BITMAP_ELEMENT_WORDS - 1; ix >= 1; ix--)
12802c2b
JH
1297 {
1298 word = elt->bits[ix];
1299 if (word)
1300 goto found_bit;
1301 }
566422e0 1302 gcc_assert (elt->bits[ix] != 0);
12802c2b
JH
1303 found_bit:
1304 bit_no += ix * BITMAP_WORD_BITS;
12802c2b 1305#if GCC_VERSION >= 3004
c3284718 1306 gcc_assert (sizeof (long) == sizeof (word));
d630245f 1307 bit_no += BITMAP_WORD_BITS - __builtin_clzl (word) - 1;
12802c2b 1308#else
d630245f
SB
1309 /* Hopefully this is a twos-complement host... */
1310 BITMAP_WORD x = word;
1311 x |= (x >> 1);
1312 x |= (x >> 2);
1313 x |= (x >> 4);
1314 x |= (x >> 8);
1315 x |= (x >> 16);
12802c2b 1316#if BITMAP_WORD_BITS > 32
d630245f 1317 x |= (x >> 32);
12802c2b 1318#endif
d630245f 1319 bit_no += bitmap_popcount (x) - 1;
12802c2b
JH
1320#endif
1321
d630245f 1322 return bit_no;
12802c2b 1323}
87e08c69 1324\f
096ab9ea 1325
e28d0cfb 1326/* DST = A & B. */
88c4f655
NS
1327
1328void
e326eeb5 1329bitmap_and (bitmap dst, const_bitmap a, const_bitmap b)
096ab9ea 1330{
88c4f655 1331 bitmap_element *dst_elt = dst->first;
e326eeb5
KG
1332 const bitmap_element *a_elt = a->first;
1333 const bitmap_element *b_elt = b->first;
88c4f655 1334 bitmap_element *dst_prev = NULL;
8229306b 1335
d1e14d97 1336 gcc_checking_assert (!dst->tree_form && !a->tree_form && !b->tree_form);
08a3c5cd
KZ
1337 gcc_assert (dst != a && dst != b);
1338
1339 if (a == b)
1340 {
1341 bitmap_copy (dst, a);
1342 return;
1343 }
1344
88c4f655
NS
1345 while (a_elt && b_elt)
1346 {
1347 if (a_elt->indx < b_elt->indx)
1348 a_elt = a_elt->next;
1349 else if (b_elt->indx < a_elt->indx)
1350 b_elt = b_elt->next;
1351 else
1352 {
1353 /* Matching elts, generate A & B. */
1354 unsigned ix;
1355 BITMAP_WORD ior = 0;
1356
1357 if (!dst_elt)
d1e14d97
SB
1358 dst_elt = bitmap_list_insert_element_after (dst, dst_prev,
1359 a_elt->indx);
c22cacf3 1360 else
1bc40c7e 1361 dst_elt->indx = a_elt->indx;
a2b709cc 1362 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
88c4f655
NS
1363 {
1364 BITMAP_WORD r = a_elt->bits[ix] & b_elt->bits[ix];
096ab9ea 1365
88c4f655
NS
1366 dst_elt->bits[ix] = r;
1367 ior |= r;
1368 }
1369 if (ior)
1370 {
1371 dst_prev = dst_elt;
1372 dst_elt = dst_elt->next;
1373 }
1374 a_elt = a_elt->next;
1375 b_elt = b_elt->next;
1376 }
1377 }
5ce02e40
SP
1378 /* Ensure that dst->current is valid. */
1379 dst->current = dst->first;
88c4f655 1380 bitmap_elt_clear_from (dst, dst_elt);
7a40b8b1 1381 gcc_checking_assert (!dst->current == !dst->first);
88c4f655
NS
1382 if (dst->current)
1383 dst->indx = dst->current->indx;
1384}
1385
7b19209f 1386/* A &= B. Return true if A changed. */
88c4f655 1387
7b19209f 1388bool
e326eeb5 1389bitmap_and_into (bitmap a, const_bitmap b)
88c4f655
NS
1390{
1391 bitmap_element *a_elt = a->first;
e326eeb5 1392 const bitmap_element *b_elt = b->first;
88c4f655 1393 bitmap_element *next;
7b19209f 1394 bool changed = false;
096ab9ea 1395
d1e14d97
SB
1396 gcc_checking_assert (!a->tree_form && !b->tree_form);
1397
c22cacf3 1398 if (a == b)
7b19209f 1399 return false;
08a3c5cd 1400
88c4f655 1401 while (a_elt && b_elt)
096ab9ea 1402 {
88c4f655
NS
1403 if (a_elt->indx < b_elt->indx)
1404 {
1405 next = a_elt->next;
d1e14d97 1406 bitmap_list_unlink_element (a, a_elt);
88c4f655 1407 a_elt = next;
7b19209f 1408 changed = true;
88c4f655
NS
1409 }
1410 else if (b_elt->indx < a_elt->indx)
1411 b_elt = b_elt->next;
1412 else
096ab9ea 1413 {
88c4f655
NS
1414 /* Matching elts, generate A &= B. */
1415 unsigned ix;
1416 BITMAP_WORD ior = 0;
1417
a2b709cc 1418 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
88c4f655
NS
1419 {
1420 BITMAP_WORD r = a_elt->bits[ix] & b_elt->bits[ix];
7b19209f
SB
1421 if (a_elt->bits[ix] != r)
1422 changed = true;
88c4f655
NS
1423 a_elt->bits[ix] = r;
1424 ior |= r;
1425 }
1426 next = a_elt->next;
1427 if (!ior)
d1e14d97 1428 bitmap_list_unlink_element (a, a_elt);
88c4f655
NS
1429 a_elt = next;
1430 b_elt = b_elt->next;
096ab9ea 1431 }
88c4f655 1432 }
7b19209f
SB
1433
1434 if (a_elt)
1435 {
1436 changed = true;
1437 bitmap_elt_clear_from (a, a_elt);
1438 }
1439
7a40b8b1
JH
1440 gcc_checking_assert (!a->current == !a->first
1441 && (!a->current || a->indx == a->current->indx));
7b19209f
SB
1442
1443 return changed;
88c4f655
NS
1444}
1445
6fb5fa3c
DB
1446
1447/* Insert an element equal to SRC_ELT after DST_PREV, overwriting DST_ELT
1448 if non-NULL. CHANGED is true if the destination bitmap had already been
1449 changed; the new value of CHANGED is returned. */
1450
1451static inline bool
1452bitmap_elt_copy (bitmap dst, bitmap_element *dst_elt, bitmap_element *dst_prev,
e326eeb5 1453 const bitmap_element *src_elt, bool changed)
6fb5fa3c
DB
1454{
1455 if (!changed && dst_elt && dst_elt->indx == src_elt->indx)
1456 {
1457 unsigned ix;
1458
a2b709cc 1459 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
6fb5fa3c
DB
1460 if (src_elt->bits[ix] != dst_elt->bits[ix])
1461 {
1462 dst_elt->bits[ix] = src_elt->bits[ix];
1463 changed = true;
1464 }
1465 }
1466 else
1467 {
1468 changed = true;
1469 if (!dst_elt)
d1e14d97
SB
1470 dst_elt = bitmap_list_insert_element_after (dst, dst_prev,
1471 src_elt->indx);
6fb5fa3c
DB
1472 else
1473 dst_elt->indx = src_elt->indx;
1474 memcpy (dst_elt->bits, src_elt->bits, sizeof (dst_elt->bits));
1475 }
1476 return changed;
1477}
1478
1479
1480
88c4f655
NS
1481/* DST = A & ~B */
1482
6fb5fa3c 1483bool
e326eeb5 1484bitmap_and_compl (bitmap dst, const_bitmap a, const_bitmap b)
88c4f655
NS
1485{
1486 bitmap_element *dst_elt = dst->first;
e326eeb5
KG
1487 const bitmap_element *a_elt = a->first;
1488 const bitmap_element *b_elt = b->first;
88c4f655 1489 bitmap_element *dst_prev = NULL;
6fb5fa3c
DB
1490 bitmap_element **dst_prev_pnext = &dst->first;
1491 bool changed = false;
88c4f655 1492
d1e14d97 1493 gcc_checking_assert (!dst->tree_form && !a->tree_form && !b->tree_form);
08a3c5cd 1494 gcc_assert (dst != a && dst != b);
c22cacf3 1495
08a3c5cd
KZ
1496 if (a == b)
1497 {
6fb5fa3c 1498 changed = !bitmap_empty_p (dst);
08a3c5cd 1499 bitmap_clear (dst);
6fb5fa3c 1500 return changed;
08a3c5cd
KZ
1501 }
1502
88c4f655
NS
1503 while (a_elt)
1504 {
6fb5fa3c
DB
1505 while (b_elt && b_elt->indx < a_elt->indx)
1506 b_elt = b_elt->next;
1507
1508 if (!b_elt || b_elt->indx > a_elt->indx)
096ab9ea 1509 {
6fb5fa3c
DB
1510 changed = bitmap_elt_copy (dst, dst_elt, dst_prev, a_elt, changed);
1511 dst_prev = *dst_prev_pnext;
1512 dst_prev_pnext = &dst_prev->next;
1513 dst_elt = *dst_prev_pnext;
88c4f655 1514 a_elt = a_elt->next;
096ab9ea 1515 }
6fb5fa3c 1516
096ab9ea
RK
1517 else
1518 {
88c4f655
NS
1519 /* Matching elts, generate A & ~B. */
1520 unsigned ix;
1521 BITMAP_WORD ior = 0;
1522
6fb5fa3c
DB
1523 if (!changed && dst_elt && dst_elt->indx == a_elt->indx)
1524 {
a2b709cc 1525 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
6fb5fa3c
DB
1526 {
1527 BITMAP_WORD r = a_elt->bits[ix] & ~b_elt->bits[ix];
1528
1529 if (dst_elt->bits[ix] != r)
1530 {
1531 changed = true;
1532 dst_elt->bits[ix] = r;
1533 }
1534 ior |= r;
1535 }
1536 }
c22cacf3 1537 else
88c4f655 1538 {
6fb5fa3c
DB
1539 bool new_element;
1540 if (!dst_elt || dst_elt->indx > a_elt->indx)
1541 {
d1e14d97
SB
1542 dst_elt = bitmap_list_insert_element_after (dst, dst_prev,
1543 a_elt->indx);
6fb5fa3c
DB
1544 new_element = true;
1545 }
1546 else
1547 {
1548 dst_elt->indx = a_elt->indx;
1549 new_element = false;
1550 }
88c4f655 1551
a2b709cc 1552 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
6fb5fa3c
DB
1553 {
1554 BITMAP_WORD r = a_elt->bits[ix] & ~b_elt->bits[ix];
1555
1556 dst_elt->bits[ix] = r;
1557 ior |= r;
1558 }
1559
1560 if (ior)
1561 changed = true;
1562 else
1563 {
1564 changed |= !new_element;
d1e14d97 1565 bitmap_list_unlink_element (dst, dst_elt);
6fb5fa3c
DB
1566 dst_elt = *dst_prev_pnext;
1567 }
88c4f655 1568 }
6fb5fa3c 1569
88c4f655
NS
1570 if (ior)
1571 {
6fb5fa3c
DB
1572 dst_prev = *dst_prev_pnext;
1573 dst_prev_pnext = &dst_prev->next;
1574 dst_elt = *dst_prev_pnext;
88c4f655
NS
1575 }
1576 a_elt = a_elt->next;
1577 b_elt = b_elt->next;
096ab9ea 1578 }
88c4f655 1579 }
6fb5fa3c 1580
5ce02e40
SP
1581 /* Ensure that dst->current is valid. */
1582 dst->current = dst->first;
6fb5fa3c
DB
1583
1584 if (dst_elt)
1585 {
1586 changed = true;
1587 bitmap_elt_clear_from (dst, dst_elt);
1588 }
7a40b8b1 1589 gcc_checking_assert (!dst->current == !dst->first);
88c4f655
NS
1590 if (dst->current)
1591 dst->indx = dst->current->indx;
6fb5fa3c
DB
1592
1593 return changed;
88c4f655
NS
1594}
1595
90bb1c1f 1596/* A &= ~B. Returns true if A changes */
096ab9ea 1597
90bb1c1f 1598bool
e326eeb5 1599bitmap_and_compl_into (bitmap a, const_bitmap b)
88c4f655
NS
1600{
1601 bitmap_element *a_elt = a->first;
e326eeb5 1602 const bitmap_element *b_elt = b->first;
88c4f655 1603 bitmap_element *next;
90bb1c1f 1604 BITMAP_WORD changed = 0;
88c4f655 1605
d1e14d97
SB
1606 gcc_checking_assert (!a->tree_form && !b->tree_form);
1607
08a3c5cd
KZ
1608 if (a == b)
1609 {
1610 if (bitmap_empty_p (a))
1611 return false;
1612 else
1613 {
1614 bitmap_clear (a);
1615 return true;
1616 }
1617 }
1618
88c4f655
NS
1619 while (a_elt && b_elt)
1620 {
1621 if (a_elt->indx < b_elt->indx)
1622 a_elt = a_elt->next;
1623 else if (b_elt->indx < a_elt->indx)
1624 b_elt = b_elt->next;
1625 else
8229306b 1626 {
88c4f655
NS
1627 /* Matching elts, generate A &= ~B. */
1628 unsigned ix;
1629 BITMAP_WORD ior = 0;
1630
a2b709cc 1631 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
88c4f655 1632 {
90bb1c1f
NS
1633 BITMAP_WORD cleared = a_elt->bits[ix] & b_elt->bits[ix];
1634 BITMAP_WORD r = a_elt->bits[ix] ^ cleared;
88c4f655
NS
1635
1636 a_elt->bits[ix] = r;
90bb1c1f 1637 changed |= cleared;
88c4f655
NS
1638 ior |= r;
1639 }
1640 next = a_elt->next;
1641 if (!ior)
d1e14d97 1642 bitmap_list_unlink_element (a, a_elt);
88c4f655
NS
1643 a_elt = next;
1644 b_elt = b_elt->next;
8229306b 1645 }
88c4f655 1646 }
7a40b8b1
JH
1647 gcc_checking_assert (!a->current == !a->first
1648 && (!a->current || a->indx == a->current->indx));
90bb1c1f 1649 return changed != 0;
88c4f655
NS
1650}
1651
6fb5fa3c
DB
1652/* Set COUNT bits from START in HEAD. */
1653void
1654bitmap_set_range (bitmap head, unsigned int start, unsigned int count)
1655{
1656 unsigned int first_index, end_bit_plus1, last_index;
1657 bitmap_element *elt, *elt_prev;
1658 unsigned int i;
1659
d1e14d97
SB
1660 gcc_checking_assert (!head->tree_form);
1661
6fb5fa3c
DB
1662 if (!count)
1663 return;
1664
07a737f3
RS
1665 if (count == 1)
1666 {
1667 bitmap_set_bit (head, start);
1668 return;
1669 }
1670
6fb5fa3c
DB
1671 first_index = start / BITMAP_ELEMENT_ALL_BITS;
1672 end_bit_plus1 = start + count;
1673 last_index = (end_bit_plus1 - 1) / BITMAP_ELEMENT_ALL_BITS;
d1e14d97 1674 elt = bitmap_list_find_element (head, first_index);
6fb5fa3c 1675
d1e14d97 1676 /* If bitmap_list_find_element returns zero, the current is the closest block
6fb5fa3c
DB
1677 to the result. Otherwise, just use bitmap_element_allocate to
1678 ensure ELT is set; in the loop below, ELT == NULL means "insert
1679 at the end of the bitmap". */
1680 if (!elt)
1681 {
1682 elt = bitmap_element_allocate (head);
1683 elt->indx = first_index;
d1e14d97 1684 bitmap_list_link_element (head, elt);
6fb5fa3c
DB
1685 }
1686
377002a9 1687 gcc_checking_assert (elt->indx == first_index);
6fb5fa3c
DB
1688 elt_prev = elt->prev;
1689 for (i = first_index; i <= last_index; i++)
1690 {
1691 unsigned elt_start_bit = i * BITMAP_ELEMENT_ALL_BITS;
1692 unsigned elt_end_bit_plus1 = elt_start_bit + BITMAP_ELEMENT_ALL_BITS;
1693
1694 unsigned int first_word_to_mod;
1695 BITMAP_WORD first_mask;
1696 unsigned int last_word_to_mod;
1697 BITMAP_WORD last_mask;
1698 unsigned int ix;
1699
1700 if (!elt || elt->indx != i)
d1e14d97 1701 elt = bitmap_list_insert_element_after (head, elt_prev, i);
6fb5fa3c
DB
1702
1703 if (elt_start_bit <= start)
1704 {
1705 /* The first bit to turn on is somewhere inside this
1706 elt. */
1707 first_word_to_mod = (start - elt_start_bit) / BITMAP_WORD_BITS;
1708
1709 /* This mask should have 1s in all bits >= start position. */
1710 first_mask =
1711 (((BITMAP_WORD) 1) << ((start % BITMAP_WORD_BITS))) - 1;
1712 first_mask = ~first_mask;
1713 }
1714 else
1715 {
1716 /* The first bit to turn on is below this start of this elt. */
1717 first_word_to_mod = 0;
1718 first_mask = ~(BITMAP_WORD) 0;
1719 }
1720
1721 if (elt_end_bit_plus1 <= end_bit_plus1)
1722 {
1723 /* The last bit to turn on is beyond this elt. */
1724 last_word_to_mod = BITMAP_ELEMENT_WORDS - 1;
1725 last_mask = ~(BITMAP_WORD) 0;
1726 }
1727 else
1728 {
1729 /* The last bit to turn on is inside to this elt. */
1730 last_word_to_mod =
1731 (end_bit_plus1 - elt_start_bit) / BITMAP_WORD_BITS;
1732
1733 /* The last mask should have 1s below the end bit. */
1734 last_mask =
1735 (((BITMAP_WORD) 1) << ((end_bit_plus1 % BITMAP_WORD_BITS))) - 1;
1736 }
1737
1738 if (first_word_to_mod == last_word_to_mod)
1739 {
1740 BITMAP_WORD mask = first_mask & last_mask;
1741 elt->bits[first_word_to_mod] |= mask;
1742 }
1743 else
1744 {
1745 elt->bits[first_word_to_mod] |= first_mask;
1746 if (BITMAP_ELEMENT_WORDS > 2)
1747 for (ix = first_word_to_mod + 1; ix < last_word_to_mod; ix++)
1748 elt->bits[ix] = ~(BITMAP_WORD) 0;
1749 elt->bits[last_word_to_mod] |= last_mask;
1750 }
1751
1752 elt_prev = elt;
1753 elt = elt->next;
1754 }
1755
1756 head->current = elt ? elt : elt_prev;
1757 head->indx = head->current->indx;
1758}
1759
1bc40c7e
KZ
1760/* Clear COUNT bits from START in HEAD. */
1761void
1762bitmap_clear_range (bitmap head, unsigned int start, unsigned int count)
1763{
6fb5fa3c
DB
1764 unsigned int first_index, end_bit_plus1, last_index;
1765 bitmap_element *elt;
1766
d1e14d97
SB
1767 gcc_checking_assert (!head->tree_form);
1768
6fb5fa3c
DB
1769 if (!count)
1770 return;
1771
07a737f3
RS
1772 if (count == 1)
1773 {
1774 bitmap_clear_bit (head, start);
1775 return;
1776 }
1777
6fb5fa3c
DB
1778 first_index = start / BITMAP_ELEMENT_ALL_BITS;
1779 end_bit_plus1 = start + count;
1780 last_index = (end_bit_plus1 - 1) / BITMAP_ELEMENT_ALL_BITS;
d1e14d97 1781 elt = bitmap_list_find_element (head, first_index);
1bc40c7e 1782
d1e14d97 1783 /* If bitmap_list_find_element returns zero, the current is the closest block
1bc40c7e
KZ
1784 to the result. If the current is less than first index, find the
1785 next one. Otherwise, just set elt to be current. */
1786 if (!elt)
c22cacf3 1787 {
1bc40c7e
KZ
1788 if (head->current)
1789 {
1790 if (head->indx < first_index)
1791 {
1792 elt = head->current->next;
1793 if (!elt)
1794 return;
1795 }
c22cacf3 1796 else
1bc40c7e
KZ
1797 elt = head->current;
1798 }
1799 else
1800 return;
1801 }
1802
1803 while (elt && (elt->indx <= last_index))
1804 {
1805 bitmap_element * next_elt = elt->next;
1806 unsigned elt_start_bit = (elt->indx) * BITMAP_ELEMENT_ALL_BITS;
1807 unsigned elt_end_bit_plus1 = elt_start_bit + BITMAP_ELEMENT_ALL_BITS;
1808
1809
1810 if (elt_start_bit >= start && elt_end_bit_plus1 <= end_bit_plus1)
1811 /* Get rid of the entire elt and go to the next one. */
d1e14d97 1812 bitmap_list_unlink_element (head, elt);
c22cacf3 1813 else
1bc40c7e
KZ
1814 {
1815 /* Going to have to knock out some bits in this elt. */
c22cacf3
MS
1816 unsigned int first_word_to_mod;
1817 BITMAP_WORD first_mask;
1bc40c7e
KZ
1818 unsigned int last_word_to_mod;
1819 BITMAP_WORD last_mask;
1820 unsigned int i;
1821 bool clear = true;
1822
1823 if (elt_start_bit <= start)
1824 {
1825 /* The first bit to turn off is somewhere inside this
1826 elt. */
1827 first_word_to_mod = (start - elt_start_bit) / BITMAP_WORD_BITS;
1828
1829 /* This mask should have 1s in all bits >= start position. */
c22cacf3 1830 first_mask =
1bc40c7e
KZ
1831 (((BITMAP_WORD) 1) << ((start % BITMAP_WORD_BITS))) - 1;
1832 first_mask = ~first_mask;
1833 }
1834 else
1835 {
1836 /* The first bit to turn off is below this start of this elt. */
1837 first_word_to_mod = 0;
1838 first_mask = 0;
1839 first_mask = ~first_mask;
c22cacf3
MS
1840 }
1841
1bc40c7e
KZ
1842 if (elt_end_bit_plus1 <= end_bit_plus1)
1843 {
1844 /* The last bit to turn off is beyond this elt. */
1845 last_word_to_mod = BITMAP_ELEMENT_WORDS - 1;
1846 last_mask = 0;
1847 last_mask = ~last_mask;
1848 }
1849 else
1850 {
1851 /* The last bit to turn off is inside to this elt. */
c22cacf3 1852 last_word_to_mod =
1bc40c7e
KZ
1853 (end_bit_plus1 - elt_start_bit) / BITMAP_WORD_BITS;
1854
1855 /* The last mask should have 1s below the end bit. */
c22cacf3 1856 last_mask =
1bc40c7e
KZ
1857 (((BITMAP_WORD) 1) << (((end_bit_plus1) % BITMAP_WORD_BITS))) - 1;
1858 }
1859
1860
1861 if (first_word_to_mod == last_word_to_mod)
1862 {
1863 BITMAP_WORD mask = first_mask & last_mask;
1864 elt->bits[first_word_to_mod] &= ~mask;
1865 }
1866 else
1867 {
1868 elt->bits[first_word_to_mod] &= ~first_mask;
6fb5fa3c
DB
1869 if (BITMAP_ELEMENT_WORDS > 2)
1870 for (i = first_word_to_mod + 1; i < last_word_to_mod; i++)
1871 elt->bits[i] = 0;
1bc40c7e
KZ
1872 elt->bits[last_word_to_mod] &= ~last_mask;
1873 }
1874 for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
1875 if (elt->bits[i])
1876 {
1877 clear = false;
1878 break;
1879 }
1880 /* Check to see if there are any bits left. */
1881 if (clear)
d1e14d97 1882 bitmap_list_unlink_element (head, elt);
1bc40c7e
KZ
1883 }
1884 elt = next_elt;
1885 }
c22cacf3 1886
1bc40c7e
KZ
1887 if (elt)
1888 {
1889 head->current = elt;
1890 head->indx = head->current->indx;
1891 }
1892}
1893
1894/* A = ~A & B. */
1895
1896void
e326eeb5 1897bitmap_compl_and_into (bitmap a, const_bitmap b)
1bc40c7e
KZ
1898{
1899 bitmap_element *a_elt = a->first;
e326eeb5 1900 const bitmap_element *b_elt = b->first;
1bc40c7e
KZ
1901 bitmap_element *a_prev = NULL;
1902 bitmap_element *next;
1903
d1e14d97 1904 gcc_checking_assert (!a->tree_form && !b->tree_form);
1bc40c7e
KZ
1905 gcc_assert (a != b);
1906
1907 if (bitmap_empty_p (a))
1908 {
1909 bitmap_copy (a, b);
1910 return;
1911 }
1912 if (bitmap_empty_p (b))
1913 {
1914 bitmap_clear (a);
1915 return;
1916 }
1917
1918 while (a_elt || b_elt)
1919 {
1920 if (!b_elt || (a_elt && a_elt->indx < b_elt->indx))
1921 {
1922 /* A is before B. Remove A */
1923 next = a_elt->next;
1924 a_prev = a_elt->prev;
d1e14d97 1925 bitmap_list_unlink_element (a, a_elt);
1bc40c7e
KZ
1926 a_elt = next;
1927 }
1928 else if (!a_elt || b_elt->indx < a_elt->indx)
1929 {
1930 /* B is before A. Copy B. */
d1e14d97 1931 next = bitmap_list_insert_element_after (a, a_prev, b_elt->indx);
1bc40c7e
KZ
1932 memcpy (next->bits, b_elt->bits, sizeof (next->bits));
1933 a_prev = next;
1934 b_elt = b_elt->next;
1935 }
1936 else
1937 {
1938 /* Matching elts, generate A = ~A & B. */
1939 unsigned ix;
1940 BITMAP_WORD ior = 0;
1941
a2b709cc 1942 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
1bc40c7e
KZ
1943 {
1944 BITMAP_WORD cleared = a_elt->bits[ix] & b_elt->bits[ix];
1945 BITMAP_WORD r = b_elt->bits[ix] ^ cleared;
1946
1947 a_elt->bits[ix] = r;
1948 ior |= r;
1949 }
1950 next = a_elt->next;
1951 if (!ior)
d1e14d97 1952 bitmap_list_unlink_element (a, a_elt);
1bc40c7e
KZ
1953 else
1954 a_prev = a_elt;
1955 a_elt = next;
1956 b_elt = b_elt->next;
1957 }
1958 }
7a40b8b1
JH
1959 gcc_checking_assert (!a->current == !a->first
1960 && (!a->current || a->indx == a->current->indx));
1bc40c7e
KZ
1961 return;
1962}
1963
6fb5fa3c
DB
1964
1965/* Insert an element corresponding to A_ELT | B_ELT after DST_PREV,
1966 overwriting DST_ELT if non-NULL. CHANGED is true if the destination bitmap
1967 had already been changed; the new value of CHANGED is returned. */
1968
1969static inline bool
1970bitmap_elt_ior (bitmap dst, bitmap_element *dst_elt, bitmap_element *dst_prev,
e326eeb5 1971 const bitmap_element *a_elt, const bitmap_element *b_elt,
6fb5fa3c
DB
1972 bool changed)
1973{
1974 gcc_assert (a_elt || b_elt);
1975
1976 if (a_elt && b_elt && a_elt->indx == b_elt->indx)
1977 {
1978 /* Matching elts, generate A | B. */
1979 unsigned ix;
1980
1981 if (!changed && dst_elt && dst_elt->indx == a_elt->indx)
1982 {
a2b709cc 1983 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
6fb5fa3c
DB
1984 {
1985 BITMAP_WORD r = a_elt->bits[ix] | b_elt->bits[ix];
1986 if (r != dst_elt->bits[ix])
1987 {
1988 dst_elt->bits[ix] = r;
1989 changed = true;
1990 }
1991 }
1992 }
1993 else
1994 {
1995 changed = true;
1996 if (!dst_elt)
d1e14d97
SB
1997 dst_elt = bitmap_list_insert_element_after (dst, dst_prev,
1998 a_elt->indx);
6fb5fa3c
DB
1999 else
2000 dst_elt->indx = a_elt->indx;
a2b709cc 2001 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
6fb5fa3c
DB
2002 {
2003 BITMAP_WORD r = a_elt->bits[ix] | b_elt->bits[ix];
2004 dst_elt->bits[ix] = r;
2005 }
2006 }
2007 }
2008 else
2009 {
2010 /* Copy a single element. */
e326eeb5 2011 const bitmap_element *src;
6fb5fa3c
DB
2012
2013 if (!b_elt || (a_elt && a_elt->indx < b_elt->indx))
2014 src = a_elt;
2015 else
2016 src = b_elt;
2017
377002a9 2018 gcc_checking_assert (src);
6fb5fa3c
DB
2019 changed = bitmap_elt_copy (dst, dst_elt, dst_prev, src, changed);
2020 }
2021 return changed;
2022}
2023
2024
88c4f655
NS
2025/* DST = A | B. Return true if DST changes. */
2026
2027bool
e326eeb5 2028bitmap_ior (bitmap dst, const_bitmap a, const_bitmap b)
88c4f655
NS
2029{
2030 bitmap_element *dst_elt = dst->first;
e326eeb5
KG
2031 const bitmap_element *a_elt = a->first;
2032 const bitmap_element *b_elt = b->first;
88c4f655 2033 bitmap_element *dst_prev = NULL;
6fb5fa3c 2034 bitmap_element **dst_prev_pnext = &dst->first;
c22cacf3 2035 bool changed = false;
88c4f655 2036
d1e14d97 2037 gcc_checking_assert (!dst->tree_form && !a->tree_form && !b->tree_form);
08a3c5cd
KZ
2038 gcc_assert (dst != a && dst != b);
2039
88c4f655
NS
2040 while (a_elt || b_elt)
2041 {
6fb5fa3c
DB
2042 changed = bitmap_elt_ior (dst, dst_elt, dst_prev, a_elt, b_elt, changed);
2043
88c4f655 2044 if (a_elt && b_elt && a_elt->indx == b_elt->indx)
8229306b 2045 {
88c4f655
NS
2046 a_elt = a_elt->next;
2047 b_elt = b_elt->next;
8229306b
RH
2048 }
2049 else
096ab9ea 2050 {
6fb5fa3c
DB
2051 if (a_elt && (!b_elt || a_elt->indx <= b_elt->indx))
2052 a_elt = a_elt->next;
2053 else if (b_elt && (!a_elt || b_elt->indx <= a_elt->indx))
2054 b_elt = b_elt->next;
096ab9ea 2055 }
6fb5fa3c
DB
2056
2057 dst_prev = *dst_prev_pnext;
2058 dst_prev_pnext = &dst_prev->next;
2059 dst_elt = *dst_prev_pnext;
88c4f655
NS
2060 }
2061
2062 if (dst_elt)
2063 {
2064 changed = true;
9e5d3a2c
MS
2065 /* Ensure that dst->current is valid. */
2066 dst->current = dst->first;
88c4f655
NS
2067 bitmap_elt_clear_from (dst, dst_elt);
2068 }
7a40b8b1 2069 gcc_checking_assert (!dst->current == !dst->first);
88c4f655
NS
2070 if (dst->current)
2071 dst->indx = dst->current->indx;
2072 return changed;
2073}
096ab9ea 2074
88c4f655
NS
2075/* A |= B. Return true if A changes. */
2076
2077bool
e326eeb5 2078bitmap_ior_into (bitmap a, const_bitmap b)
88c4f655
NS
2079{
2080 bitmap_element *a_elt = a->first;
e326eeb5 2081 const bitmap_element *b_elt = b->first;
88c4f655 2082 bitmap_element *a_prev = NULL;
6fb5fa3c 2083 bitmap_element **a_prev_pnext = &a->first;
88c4f655
NS
2084 bool changed = false;
2085
d1e14d97 2086 gcc_checking_assert (!a->tree_form && !b->tree_form);
08a3c5cd
KZ
2087 if (a == b)
2088 return false;
2089
88c4f655
NS
2090 while (b_elt)
2091 {
6fb5fa3c
DB
2092 /* If A lags behind B, just advance it. */
2093 if (!a_elt || a_elt->indx == b_elt->indx)
88c4f655 2094 {
6fb5fa3c 2095 changed = bitmap_elt_ior (a, a_elt, a_prev, a_elt, b_elt, changed);
88c4f655 2096 b_elt = b_elt->next;
b9a73e32 2097 }
6fb5fa3c 2098 else if (a_elt->indx > b_elt->indx)
b9a73e32 2099 {
6fb5fa3c 2100 changed = bitmap_elt_copy (a, NULL, a_prev, b_elt, changed);
88c4f655 2101 b_elt = b_elt->next;
096ab9ea 2102 }
6fb5fa3c
DB
2103
2104 a_prev = *a_prev_pnext;
2105 a_prev_pnext = &a_prev->next;
2106 a_elt = *a_prev_pnext;
096ab9ea 2107 }
6fb5fa3c 2108
7a40b8b1 2109 gcc_checking_assert (!a->current == !a->first);
88c4f655
NS
2110 if (a->current)
2111 a->indx = a->current->indx;
2112 return changed;
2113}
2114
029ca388
RB
2115/* A |= B. Return true if A changes. Free B (re-using its storage
2116 for the result). */
2117
2118bool
2119bitmap_ior_into_and_free (bitmap a, bitmap *b_)
2120{
2121 bitmap b = *b_;
2122 bitmap_element *a_elt = a->first;
2123 bitmap_element *b_elt = b->first;
2124 bitmap_element *a_prev = NULL;
2125 bitmap_element **a_prev_pnext = &a->first;
2126 bool changed = false;
2127
2128 gcc_checking_assert (!a->tree_form && !b->tree_form);
2129 gcc_assert (a->obstack == b->obstack);
2130 if (a == b)
2131 return false;
2132
2133 while (b_elt)
2134 {
2135 /* If A lags behind B, just advance it. */
2136 if (!a_elt || a_elt->indx == b_elt->indx)
2137 {
2138 changed = bitmap_elt_ior (a, a_elt, a_prev, a_elt, b_elt, changed);
2139 b_elt = b_elt->next;
2140 }
2141 else if (a_elt->indx > b_elt->indx)
2142 {
2143 bitmap_element *b_elt_next = b_elt->next;
2144 bitmap_list_unlink_element (b, b_elt, false);
2145 bitmap_list_insert_element_after (a, a_prev, b_elt->indx, b_elt);
2146 b_elt = b_elt_next;
2147 }
2148
2149 a_prev = *a_prev_pnext;
2150 a_prev_pnext = &a_prev->next;
2151 a_elt = *a_prev_pnext;
2152 }
2153
2154 gcc_checking_assert (!a->current == !a->first);
2155 if (a->current)
2156 a->indx = a->current->indx;
2157
2158 if (b->obstack)
2159 BITMAP_FREE (*b_);
2160 else
2161 bitmap_clear (b);
2162 return changed;
2163}
2164
88c4f655 2165/* DST = A ^ B */
096ab9ea 2166
88c4f655 2167void
e326eeb5 2168bitmap_xor (bitmap dst, const_bitmap a, const_bitmap b)
88c4f655
NS
2169{
2170 bitmap_element *dst_elt = dst->first;
e326eeb5
KG
2171 const bitmap_element *a_elt = a->first;
2172 const bitmap_element *b_elt = b->first;
88c4f655
NS
2173 bitmap_element *dst_prev = NULL;
2174
d1e14d97 2175 gcc_checking_assert (!dst->tree_form && !a->tree_form && !b->tree_form);
08a3c5cd 2176 gcc_assert (dst != a && dst != b);
d1e14d97 2177
08a3c5cd
KZ
2178 if (a == b)
2179 {
2180 bitmap_clear (dst);
2181 return;
2182 }
2183
88c4f655 2184 while (a_elt || b_elt)
096ab9ea 2185 {
88c4f655 2186 if (a_elt && b_elt && a_elt->indx == b_elt->indx)
e2500fed 2187 {
88c4f655
NS
2188 /* Matching elts, generate A ^ B. */
2189 unsigned ix;
2190 BITMAP_WORD ior = 0;
2191
2192 if (!dst_elt)
d1e14d97
SB
2193 dst_elt = bitmap_list_insert_element_after (dst, dst_prev,
2194 a_elt->indx);
1bc40c7e
KZ
2195 else
2196 dst_elt->indx = a_elt->indx;
a2b709cc 2197 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
88c4f655
NS
2198 {
2199 BITMAP_WORD r = a_elt->bits[ix] ^ b_elt->bits[ix];
2200
2201 ior |= r;
2202 dst_elt->bits[ix] = r;
2203 }
2204 a_elt = a_elt->next;
2205 b_elt = b_elt->next;
2206 if (ior)
2207 {
2208 dst_prev = dst_elt;
2209 dst_elt = dst_elt->next;
2210 }
e2500fed
GK
2211 }
2212 else
2213 {
88c4f655 2214 /* Copy a single element. */
e326eeb5 2215 const bitmap_element *src;
88c4f655
NS
2216
2217 if (!b_elt || (a_elt && a_elt->indx < b_elt->indx))
2218 {
2219 src = a_elt;
2220 a_elt = a_elt->next;
2221 }
2222 else
2223 {
2224 src = b_elt;
2225 b_elt = b_elt->next;
2226 }
2227
2228 if (!dst_elt)
d1e14d97
SB
2229 dst_elt = bitmap_list_insert_element_after (dst, dst_prev,
2230 src->indx);
c22cacf3 2231 else
1bc40c7e 2232 dst_elt->indx = src->indx;
88c4f655
NS
2233 memcpy (dst_elt->bits, src->bits, sizeof (dst_elt->bits));
2234 dst_prev = dst_elt;
2235 dst_elt = dst_elt->next;
e2500fed 2236 }
096ab9ea 2237 }
5ce02e40
SP
2238 /* Ensure that dst->current is valid. */
2239 dst->current = dst->first;
88c4f655 2240 bitmap_elt_clear_from (dst, dst_elt);
7a40b8b1 2241 gcc_checking_assert (!dst->current == !dst->first);
88c4f655
NS
2242 if (dst->current)
2243 dst->indx = dst->current->indx;
2244}
096ab9ea 2245
88c4f655 2246/* A ^= B */
8229306b 2247
88c4f655 2248void
e326eeb5 2249bitmap_xor_into (bitmap a, const_bitmap b)
88c4f655
NS
2250{
2251 bitmap_element *a_elt = a->first;
e326eeb5 2252 const bitmap_element *b_elt = b->first;
88c4f655
NS
2253 bitmap_element *a_prev = NULL;
2254
d1e14d97
SB
2255 gcc_checking_assert (!a->tree_form && !b->tree_form);
2256
08a3c5cd
KZ
2257 if (a == b)
2258 {
2259 bitmap_clear (a);
2260 return;
2261 }
2262
88c4f655
NS
2263 while (b_elt)
2264 {
2265 if (!a_elt || b_elt->indx < a_elt->indx)
2266 {
2267 /* Copy b_elt. */
d1e14d97
SB
2268 bitmap_element *dst = bitmap_list_insert_element_after (a, a_prev,
2269 b_elt->indx);
88c4f655
NS
2270 memcpy (dst->bits, b_elt->bits, sizeof (dst->bits));
2271 a_prev = dst;
2272 b_elt = b_elt->next;
2273 }
2274 else if (a_elt->indx < b_elt->indx)
2275 {
2276 a_prev = a_elt;
2277 a_elt = a_elt->next;
2278 }
2279 else
2280 {
2281 /* Matching elts, generate A ^= B. */
2282 unsigned ix;
2283 BITMAP_WORD ior = 0;
2284 bitmap_element *next = a_elt->next;
2285
a2b709cc 2286 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
88c4f655
NS
2287 {
2288 BITMAP_WORD r = a_elt->bits[ix] ^ b_elt->bits[ix];
2289
2290 ior |= r;
2291 a_elt->bits[ix] = r;
2292 }
2293 b_elt = b_elt->next;
2294 if (ior)
2295 a_prev = a_elt;
2296 else
d1e14d97 2297 bitmap_list_unlink_element (a, a_elt);
88c4f655
NS
2298 a_elt = next;
2299 }
2300 }
7a40b8b1 2301 gcc_checking_assert (!a->current == !a->first);
88c4f655
NS
2302 if (a->current)
2303 a->indx = a->current->indx;
8229306b
RH
2304}
2305
55994078
NS
2306/* Return true if two bitmaps are identical.
2307 We do not bother with a check for pointer equality, as that never
2308 occurs in practice. */
8229306b 2309
55994078 2310bool
e326eeb5 2311bitmap_equal_p (const_bitmap a, const_bitmap b)
8229306b 2312{
e326eeb5
KG
2313 const bitmap_element *a_elt;
2314 const bitmap_element *b_elt;
55994078 2315 unsigned ix;
c22cacf3 2316
d1e14d97
SB
2317 gcc_checking_assert (!a->tree_form && !b->tree_form);
2318
55994078
NS
2319 for (a_elt = a->first, b_elt = b->first;
2320 a_elt && b_elt;
2321 a_elt = a_elt->next, b_elt = b_elt->next)
2322 {
2323 if (a_elt->indx != b_elt->indx)
2324 return false;
a2b709cc 2325 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
55994078
NS
2326 if (a_elt->bits[ix] != b_elt->bits[ix])
2327 return false;
2328 }
2329 return !a_elt && !b_elt;
2330}
2331
2332/* Return true if A AND B is not empty. */
2333
2334bool
e326eeb5 2335bitmap_intersect_p (const_bitmap a, const_bitmap b)
55994078 2336{
e326eeb5
KG
2337 const bitmap_element *a_elt;
2338 const bitmap_element *b_elt;
55994078 2339 unsigned ix;
c22cacf3 2340
d1e14d97
SB
2341 gcc_checking_assert (!a->tree_form && !b->tree_form);
2342
55994078
NS
2343 for (a_elt = a->first, b_elt = b->first;
2344 a_elt && b_elt;)
2345 {
2346 if (a_elt->indx < b_elt->indx)
2347 a_elt = a_elt->next;
2348 else if (b_elt->indx < a_elt->indx)
2349 b_elt = b_elt->next;
2350 else
2351 {
a2b709cc 2352 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
55994078
NS
2353 if (a_elt->bits[ix] & b_elt->bits[ix])
2354 return true;
2355 a_elt = a_elt->next;
2356 b_elt = b_elt->next;
2357 }
2358 }
2359 return false;
2360}
8229306b 2361
55994078 2362/* Return true if A AND NOT B is not empty. */
8229306b 2363
55994078 2364bool
e326eeb5 2365bitmap_intersect_compl_p (const_bitmap a, const_bitmap b)
55994078 2366{
e326eeb5
KG
2367 const bitmap_element *a_elt;
2368 const bitmap_element *b_elt;
55994078 2369 unsigned ix;
d1e14d97
SB
2370
2371 gcc_checking_assert (!a->tree_form && !b->tree_form);
2372
55994078
NS
2373 for (a_elt = a->first, b_elt = b->first;
2374 a_elt && b_elt;)
2375 {
2376 if (a_elt->indx < b_elt->indx)
2377 return true;
2378 else if (b_elt->indx < a_elt->indx)
2379 b_elt = b_elt->next;
2380 else
2381 {
a2b709cc 2382 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
55994078
NS
2383 if (a_elt->bits[ix] & ~b_elt->bits[ix])
2384 return true;
2385 a_elt = a_elt->next;
2386 b_elt = b_elt->next;
2387 }
2388 }
2389 return a_elt != NULL;
096ab9ea 2390}
55994078 2391
096ab9ea 2392\f
88c4f655 2393/* DST = A | (FROM1 & ~FROM2). Return true if DST changes. */
096ab9ea 2394
7ef7b345 2395bool
e326eeb5 2396bitmap_ior_and_compl (bitmap dst, const_bitmap a, const_bitmap b, const_bitmap kill)
096ab9ea 2397{
6fb5fa3c 2398 bool changed = false;
7932a3db 2399
6fb5fa3c 2400 bitmap_element *dst_elt = dst->first;
e326eeb5
KG
2401 const bitmap_element *a_elt = a->first;
2402 const bitmap_element *b_elt = b->first;
2403 const bitmap_element *kill_elt = kill->first;
6fb5fa3c
DB
2404 bitmap_element *dst_prev = NULL;
2405 bitmap_element **dst_prev_pnext = &dst->first;
2406
d1e14d97
SB
2407 gcc_checking_assert (!dst->tree_form && !a->tree_form && !b->tree_form
2408 && !kill->tree_form);
6fb5fa3c
DB
2409 gcc_assert (dst != a && dst != b && dst != kill);
2410
2411 /* Special cases. We don't bother checking for bitmap_equal_p (b, kill). */
2412 if (b == kill || bitmap_empty_p (b))
2413 {
2414 changed = !bitmap_equal_p (dst, a);
2415 if (changed)
2416 bitmap_copy (dst, a);
2417 return changed;
2418 }
2419 if (bitmap_empty_p (kill))
2420 return bitmap_ior (dst, a, b);
2421 if (bitmap_empty_p (a))
2422 return bitmap_and_compl (dst, b, kill);
2423
2424 while (a_elt || b_elt)
2425 {
2426 bool new_element = false;
2427
2428 if (b_elt)
2429 while (kill_elt && kill_elt->indx < b_elt->indx)
2430 kill_elt = kill_elt->next;
2431
2432 if (b_elt && kill_elt && kill_elt->indx == b_elt->indx
2433 && (!a_elt || a_elt->indx >= b_elt->indx))
2434 {
2435 bitmap_element tmp_elt;
2436 unsigned ix;
2437
2438 BITMAP_WORD ior = 0;
2439 tmp_elt.indx = b_elt->indx;
a2b709cc 2440 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
6fb5fa3c
DB
2441 {
2442 BITMAP_WORD r = b_elt->bits[ix] & ~kill_elt->bits[ix];
2443 ior |= r;
2444 tmp_elt.bits[ix] = r;
2445 }
2446
2447 if (ior)
2448 {
2449 changed = bitmap_elt_ior (dst, dst_elt, dst_prev,
2450 a_elt, &tmp_elt, changed);
2451 new_element = true;
2452 if (a_elt && a_elt->indx == b_elt->indx)
2453 a_elt = a_elt->next;
2454 }
2455
2456 b_elt = b_elt->next;
2457 kill_elt = kill_elt->next;
2458 }
2459 else
2460 {
2461 changed = bitmap_elt_ior (dst, dst_elt, dst_prev,
2462 a_elt, b_elt, changed);
2463 new_element = true;
2464
2465 if (a_elt && b_elt && a_elt->indx == b_elt->indx)
2466 {
2467 a_elt = a_elt->next;
2468 b_elt = b_elt->next;
2469 }
2470 else
2471 {
2472 if (a_elt && (!b_elt || a_elt->indx <= b_elt->indx))
2473 a_elt = a_elt->next;
2474 else if (b_elt && (!a_elt || b_elt->indx <= a_elt->indx))
2475 b_elt = b_elt->next;
2476 }
2477 }
2478
2479 if (new_element)
2480 {
2481 dst_prev = *dst_prev_pnext;
2482 dst_prev_pnext = &dst_prev->next;
2483 dst_elt = *dst_prev_pnext;
2484 }
2485 }
2486
2487 if (dst_elt)
2488 {
2489 changed = true;
9e5d3a2c
MS
2490 /* Ensure that dst->current is valid. */
2491 dst->current = dst->first;
6fb5fa3c
DB
2492 bitmap_elt_clear_from (dst, dst_elt);
2493 }
7a40b8b1 2494 gcc_checking_assert (!dst->current == !dst->first);
6fb5fa3c
DB
2495 if (dst->current)
2496 dst->indx = dst->current->indx;
88c4f655 2497
eb59b8de 2498 return changed;
096ab9ea 2499}
87e08c69 2500
d1e14d97 2501/* A |= (B & ~C). Return true if A changes. */
7ef7b345
NS
2502
2503bool
d1e14d97 2504bitmap_ior_and_compl_into (bitmap a, const_bitmap b, const_bitmap c)
87e08c69 2505{
0e5b369e
RB
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;
c22cacf3 2514
d1e14d97
SB
2515 gcc_checking_assert (!a->tree_form && !b->tree_form && !c->tree_form);
2516
0e5b369e
RB
2517 if (a == b)
2518 return false;
2519 if (bitmap_empty_p (c))
2520 return bitmap_ior_into (a, b);
2521 else if (bitmap_empty_p (a))
2522 return bitmap_and_compl (a, b, c);
87e08c69 2523
0e5b369e
RB
2524 and_elt.indx = -1;
2525 while (b_elt)
2526 {
2527 /* Advance C. */
2528 while (c_elt && c_elt->indx < b_elt->indx)
2529 c_elt = c_elt->next;
2530
2531 const bitmap_element *and_elt_ptr;
2532 if (c_elt && c_elt->indx == b_elt->indx)
2533 {
2534 BITMAP_WORD overall = 0;
2535 and_elt_ptr = &and_elt;
2536 and_elt.indx = b_elt->indx;
2537 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
2538 {
2539 and_elt.bits[ix] = b_elt->bits[ix] & ~c_elt->bits[ix];
2540 overall |= and_elt.bits[ix];
2541 }
2542 if (!overall)
2543 {
2544 b_elt = b_elt->next;
2545 continue;
2546 }
2547 }
2548 else
2549 and_elt_ptr = b_elt;
2550
2551 b_elt = b_elt->next;
2552
2553 /* Now find a place to insert AND_ELT. */
2554 do
2555 {
2556 ix = a_elt ? a_elt->indx : and_elt_ptr->indx;
2557 if (ix == and_elt_ptr->indx)
2558 changed = bitmap_elt_ior (a, a_elt, a_prev, a_elt,
2559 and_elt_ptr, changed);
2560 else if (ix > and_elt_ptr->indx)
2561 changed = bitmap_elt_copy (a, NULL, a_prev, and_elt_ptr, changed);
2562
2563 a_prev = *a_prev_pnext;
2564 a_prev_pnext = &a_prev->next;
2565 a_elt = *a_prev_pnext;
2566
2567 /* If A lagged behind B/C, we advanced it so loop once more. */
2568 }
2569 while (ix < and_elt_ptr->indx);
2570 }
2571
2572 gcc_checking_assert (!a->current == !a->first);
2573 if (a->current)
2574 a->indx = a->current->indx;
87e08c69
RH
2575 return changed;
2576}
6fb5fa3c 2577
7ff23740
PB
2578/* A |= (B & C). Return true if A changes. */
2579
2580bool
2581bitmap_ior_and_into (bitmap a, const_bitmap b, const_bitmap c)
2582{
2583 bitmap_element *a_elt = a->first;
2584 const bitmap_element *b_elt = b->first;
2585 const bitmap_element *c_elt = c->first;
2586 bitmap_element and_elt;
2587 bitmap_element *a_prev = NULL;
2588 bitmap_element **a_prev_pnext = &a->first;
2589 bool changed = false;
2590 unsigned ix;
2591
d1e14d97
SB
2592 gcc_checking_assert (!a->tree_form && !b->tree_form && !c->tree_form);
2593
7ff23740
PB
2594 if (b == c)
2595 return bitmap_ior_into (a, b);
2596 if (bitmap_empty_p (b) || bitmap_empty_p (c))
2597 return false;
2598
2599 and_elt.indx = -1;
2600 while (b_elt && c_elt)
2601 {
2602 BITMAP_WORD overall;
2603
2604 /* Find a common item of B and C. */
2605 while (b_elt->indx != c_elt->indx)
2606 {
2607 if (b_elt->indx < c_elt->indx)
2608 {
2609 b_elt = b_elt->next;
2610 if (!b_elt)
2611 goto done;
2612 }
2613 else
2614 {
2615 c_elt = c_elt->next;
2616 if (!c_elt)
2617 goto done;
2618 }
2619 }
2620
2621 overall = 0;
2622 and_elt.indx = b_elt->indx;
a2b709cc 2623 for (ix = 0; ix < BITMAP_ELEMENT_WORDS; ix++)
7ff23740
PB
2624 {
2625 and_elt.bits[ix] = b_elt->bits[ix] & c_elt->bits[ix];
2626 overall |= and_elt.bits[ix];
2627 }
2628
2629 b_elt = b_elt->next;
2630 c_elt = c_elt->next;
2631 if (!overall)
2632 continue;
2633
2634 /* Now find a place to insert AND_ELT. */
2635 do
2636 {
2637 ix = a_elt ? a_elt->indx : and_elt.indx;
2638 if (ix == and_elt.indx)
2639 changed = bitmap_elt_ior (a, a_elt, a_prev, a_elt, &and_elt, changed);
2640 else if (ix > and_elt.indx)
2641 changed = bitmap_elt_copy (a, NULL, a_prev, &and_elt, changed);
2642
2643 a_prev = *a_prev_pnext;
2644 a_prev_pnext = &a_prev->next;
2645 a_elt = *a_prev_pnext;
2646
2647 /* If A lagged behind B/C, we advanced it so loop once more. */
2648 }
2649 while (ix < and_elt.indx);
2650 }
2651
2652 done:
7a40b8b1 2653 gcc_checking_assert (!a->current == !a->first);
7ff23740
PB
2654 if (a->current)
2655 a->indx = a->current->indx;
2656 return changed;
2657}
7aa6d18a
SB
2658
2659/* Compute hash of bitmap (for purposes of hashing). */
d1e14d97 2660
7aa6d18a
SB
2661hashval_t
2662bitmap_hash (const_bitmap head)
2663{
2664 const bitmap_element *ptr;
2665 BITMAP_WORD hash = 0;
2666 int ix;
2667
d1e14d97
SB
2668 gcc_checking_assert (!head->tree_form);
2669
7aa6d18a
SB
2670 for (ptr = head->first; ptr; ptr = ptr->next)
2671 {
2672 hash ^= ptr->indx;
2673 for (ix = 0; ix != BITMAP_ELEMENT_WORDS; ix++)
2674 hash ^= ptr->bits[ix];
2675 }
2676 return (hashval_t)hash;
2677}
2678
096ab9ea 2679\f
d1e14d97
SB
2680/* Function to obtain a vector of bitmap elements in bit order from
2681 HEAD in tree view. */
2682
2683static void
2684bitmap_tree_to_vec (vec<bitmap_element *> &elts, const_bitmap head)
2685{
2686 gcc_checking_assert (head->tree_form);
2687 auto_vec<bitmap_element *, 32> stack;
2688 bitmap_element *e = head->first;
2689 while (true)
2690 {
2691 while (e != NULL)
2692 {
2693 stack.safe_push (e);
2694 e = e->prev;
2695 }
2696 if (stack.is_empty ())
2697 break;
2698
2699 e = stack.pop ();
2700 elts.safe_push (e);
2701 e = e->next;
2702 }
2703}
2704
2705/* Debugging function to print out the contents of a bitmap element. */
2706
2707DEBUG_FUNCTION void
2708debug_bitmap_elt_file (FILE *file, const bitmap_element *ptr)
2709{
2710 unsigned int i, j, col = 26;
2711
2712 fprintf (file, "\t" HOST_PTR_PRINTF " next = " HOST_PTR_PRINTF
2713 " prev = " HOST_PTR_PRINTF " indx = %u\n\t\tbits = {",
2714 (const void*) ptr, (const void*) ptr->next,
2715 (const void*) ptr->prev, ptr->indx);
2716
2717 for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
2718 for (j = 0; j < BITMAP_WORD_BITS; j++)
2719 if ((ptr->bits[i] >> j) & 1)
2720 {
2721 if (col > 70)
2722 {
2723 fprintf (file, "\n\t\t\t");
2724 col = 24;
2725 }
2726
2727 fprintf (file, " %u", (ptr->indx * BITMAP_ELEMENT_ALL_BITS
2728 + i * BITMAP_WORD_BITS + j));
2729 col += 4;
2730 }
2731
2732 fprintf (file, " }\n");
2733}
2734
096ab9ea
RK
2735/* Debugging function to print out the contents of a bitmap. */
2736
24e47c76 2737DEBUG_FUNCTION void
e326eeb5 2738debug_bitmap_file (FILE *file, const_bitmap head)
096ab9ea 2739{
e326eeb5 2740 const bitmap_element *ptr;
096ab9ea 2741
edb89024
DR
2742 fprintf (file, "\nfirst = " HOST_PTR_PRINTF
2743 " current = " HOST_PTR_PRINTF " indx = %u\n",
75b6f3fd 2744 (void *) head->first, (void *) head->current, head->indx);
096ab9ea 2745
d1e14d97 2746 if (head->tree_form)
096ab9ea 2747 {
d1e14d97
SB
2748 auto_vec<bitmap_element *, 32> elts;
2749 bitmap_tree_to_vec (elts, head);
2750 for (unsigned i = 0; i < elts.length (); ++i)
2751 debug_bitmap_elt_file (file, elts[i]);
096ab9ea 2752 }
d1e14d97
SB
2753 else
2754 for (ptr = head->first; ptr; ptr = ptr->next)
2755 debug_bitmap_elt_file (file, ptr);
096ab9ea 2756}
a615c28a 2757
096ab9ea
RK
2758/* Function to be called from the debugger to print the contents
2759 of a bitmap. */
2760
24e47c76 2761DEBUG_FUNCTION void
e326eeb5 2762debug_bitmap (const_bitmap head)
096ab9ea 2763{
3d224d46 2764 debug_bitmap_file (stderr, head);
096ab9ea 2765}
a615c28a 2766
bfd38496 2767/* Function to print out the contents of a bitmap. Unlike debug_bitmap_file,
22fa5b8a
MM
2768 it does not print anything but the bits. */
2769
24e47c76 2770DEBUG_FUNCTION void
7b3b6ae4
LC
2771bitmap_print (FILE *file, const_bitmap head, const char *prefix,
2772 const char *suffix)
22fa5b8a 2773{
5d5993dd 2774 const char *comma = "";
3cd8c58a 2775 unsigned i;
22fa5b8a
MM
2776
2777 fputs (prefix, file);
d1e14d97
SB
2778 if (head->tree_form)
2779 {
2780 auto_vec<bitmap_element *, 32> elts;
2781 bitmap_tree_to_vec (elts, head);
2782 for (i = 0; i < elts.length (); ++i)
2783 for (unsigned ix = 0; ix != BITMAP_ELEMENT_WORDS; ++ix)
2784 {
2785 BITMAP_WORD word = elts[i]->bits[ix];
2786 for (unsigned bit = 0; bit != BITMAP_WORD_BITS; ++bit)
2787 if (word & ((BITMAP_WORD)1 << bit))
2788 {
2789 fprintf (file, "%s%d", comma,
2790 (bit + BITMAP_WORD_BITS * ix
2791 + elts[i]->indx * BITMAP_ELEMENT_ALL_BITS));
2792 comma = ", ";
2793 }
2794 }
2795 }
2796 else
87c476a2 2797 {
d1e14d97
SB
2798 bitmap_iterator bi;
2799 EXECUTE_IF_SET_IN_BITMAP (head, 0, i, bi)
2800 {
2801 fprintf (file, "%s%d", comma, i);
2802 comma = ", ";
2803 }
87c476a2 2804 }
22fa5b8a
MM
2805 fputs (suffix, file);
2806}
f75709c6 2807
f75709c6
JH
2808/* Output per-bitmap memory usage statistics. */
2809void
2810dump_bitmap_statistics (void)
2811{
ca30789c 2812 if (!GATHER_STATISTICS)
7aa6d18a
SB
2813 return;
2814
643e0a30 2815 bitmap_mem_desc.dump (BITMAP_ORIGIN);
1af4bba8
JH
2816}
2817
7b3b6ae4 2818DEBUG_FUNCTION void
84562394 2819debug (const bitmap_head &ref)
7b3b6ae4
LC
2820{
2821 dump_bitmap (stderr, &ref);
2822}
2823
2824DEBUG_FUNCTION void
84562394 2825debug (const bitmap_head *ptr)
7b3b6ae4
LC
2826{
2827 if (ptr)
2828 debug (*ptr);
2829 else
2830 fprintf (stderr, "<nil>\n");
2831}
2832
54994253
AH
2833void
2834bitmap_head::dump ()
2835{
2836 debug (this);
2837}
2838
d9b950dd
DM
2839#if CHECKING_P
2840
2841namespace selftest {
2842
2843/* Selftests for bitmaps. */
2844
2845/* Freshly-created bitmaps ought to be empty. */
2846
2847static void
2848test_gc_alloc ()
2849{
2850 bitmap b = bitmap_gc_alloc ();
2851 ASSERT_TRUE (bitmap_empty_p (b));
2852}
2853
2854/* Verify bitmap_set_range. */
2855
2856static void
2857test_set_range ()
2858{
2859 bitmap b = bitmap_gc_alloc ();
2860 ASSERT_TRUE (bitmap_empty_p (b));
2861
2862 bitmap_set_range (b, 7, 5);
2863 ASSERT_FALSE (bitmap_empty_p (b));
2864 ASSERT_EQ (5, bitmap_count_bits (b));
2865
2866 /* Verify bitmap_bit_p at the boundaries. */
2867 ASSERT_FALSE (bitmap_bit_p (b, 6));
2868 ASSERT_TRUE (bitmap_bit_p (b, 7));
2869 ASSERT_TRUE (bitmap_bit_p (b, 11));
2870 ASSERT_FALSE (bitmap_bit_p (b, 12));
2871}
2872
2873/* Verify splitting a range into two pieces using bitmap_clear_bit. */
2874
2875static void
2876test_clear_bit_in_middle ()
2877{
2878 bitmap b = bitmap_gc_alloc ();
2879
2880 /* Set b to [100..200]. */
2881 bitmap_set_range (b, 100, 100);
2882 ASSERT_EQ (100, bitmap_count_bits (b));
2883
2884 /* Clear a bit in the middle. */
2885 bool changed = bitmap_clear_bit (b, 150);
2886 ASSERT_TRUE (changed);
2887 ASSERT_EQ (99, bitmap_count_bits (b));
2888 ASSERT_TRUE (bitmap_bit_p (b, 149));
2889 ASSERT_FALSE (bitmap_bit_p (b, 150));
2890 ASSERT_TRUE (bitmap_bit_p (b, 151));
2891}
2892
2893/* Verify bitmap_copy. */
2894
2895static void
2896test_copying ()
2897{
2898 bitmap src = bitmap_gc_alloc ();
2899 bitmap_set_range (src, 40, 10);
2900
2901 bitmap dst = bitmap_gc_alloc ();
2902 ASSERT_FALSE (bitmap_equal_p (src, dst));
2903 bitmap_copy (dst, src);
2904 ASSERT_TRUE (bitmap_equal_p (src, dst));
2905
2906 /* Verify that we can make them unequal again... */
2907 bitmap_set_range (src, 70, 5);
2908 ASSERT_FALSE (bitmap_equal_p (src, dst));
2909
2910 /* ...and that changing src after the copy didn't affect
2911 the other: */
2912 ASSERT_FALSE (bitmap_bit_p (dst, 70));
2913}
2914
2915/* Verify bitmap_single_bit_set_p. */
2916
2917static void
2918test_bitmap_single_bit_set_p ()
2919{
2920 bitmap b = bitmap_gc_alloc ();
2921
2922 ASSERT_FALSE (bitmap_single_bit_set_p (b));
2923
2924 bitmap_set_range (b, 42, 1);
2925 ASSERT_TRUE (bitmap_single_bit_set_p (b));
2926 ASSERT_EQ (42, bitmap_first_set_bit (b));
2927
2928 bitmap_set_range (b, 1066, 1);
2929 ASSERT_FALSE (bitmap_single_bit_set_p (b));
2930 ASSERT_EQ (42, bitmap_first_set_bit (b));
2931
2932 bitmap_clear_range (b, 0, 100);
2933 ASSERT_TRUE (bitmap_single_bit_set_p (b));
2934 ASSERT_EQ (1066, bitmap_first_set_bit (b));
2935}
2936
5ad089a3
AM
2937/* Verify accessing aligned bit chunks works as expected. */
2938
2939static void
2940test_aligned_chunk (unsigned num_bits)
2941{
2942 bitmap b = bitmap_gc_alloc ();
2943 int limit = 2 ^ num_bits;
2944
2945 int index = 3;
2946 for (int x = 0; x < limit; x++)
2947 {
2948 bitmap_set_aligned_chunk (b, index, num_bits, (BITMAP_WORD) x);
2949 ASSERT_TRUE ((int) bitmap_get_aligned_chunk (b, index, num_bits) == x);
2950 ASSERT_TRUE ((int) bitmap_get_aligned_chunk (b, index + 1,
2951 num_bits) == 0);
2952 ASSERT_TRUE ((int) bitmap_get_aligned_chunk (b, index - 1,
2953 num_bits) == 0);
2954 index += 3;
2955 }
2956 index = 3;
2957 for (int x = 0; x < limit ; x++)
2958 {
2959 ASSERT_TRUE ((int) bitmap_get_aligned_chunk (b, index, num_bits) == x);
2960 index += 3;
2961 }
2962}
2963
d9b950dd
DM
2964/* Run all of the selftests within this file. */
2965
2966void
2967bitmap_c_tests ()
2968{
2969 test_gc_alloc ();
2970 test_set_range ();
2971 test_clear_bit_in_middle ();
2972 test_copying ();
2973 test_bitmap_single_bit_set_p ();
5ad089a3
AM
2974 /* Test 2, 4 and 8 bit aligned chunks. */
2975 test_aligned_chunk (2);
2976 test_aligned_chunk (4);
2977 test_aligned_chunk (8);
d9b950dd
DM
2978}
2979
2980} // namespace selftest
2981#endif /* CHECKING_P */
7b3b6ae4 2982
e2500fed 2983#include "gt-bitmap.h"