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