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