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