00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "config.h"
00023 #include "system.h"
00024 #include "coretypes.h"
00025 #include "tm.h"
00026 #include "rtl.h"
00027 #include "flags.h"
00028 #include "obstack.h"
00029 #include "ggc.h"
00030 #include "bitmap.h"
00031
00032
00033 bitmap_element bitmap_zero_bits;
00034 bitmap_obstack bitmap_default_obstack;
00035 static GTY((deletable)) bitmap_element *bitmap_ggc_free;
00036
00037
00038 static void bitmap_elem_to_freelist (bitmap, bitmap_element *);
00039 static void bitmap_element_free (bitmap, bitmap_element *);
00040 static bitmap_element *bitmap_element_allocate (bitmap);
00041 static int bitmap_element_zerop (bitmap_element *);
00042 static void bitmap_element_link (bitmap, bitmap_element *);
00043 static bitmap_element *bitmap_elt_insert_after (bitmap, bitmap_element *, unsigned int);
00044 static void bitmap_elt_clear_from (bitmap, bitmap_element *);
00045 static bitmap_element *bitmap_find_bit (bitmap, unsigned int);
00046
00047
00048
00049 static inline void
00050 bitmap_elem_to_freelist (bitmap head, bitmap_element *elt)
00051 {
00052 bitmap_obstack *bit_obstack = head->obstack;
00053
00054 elt->next = NULL;
00055 if (bit_obstack)
00056 {
00057 elt->prev = bit_obstack->elements;
00058 bit_obstack->elements = elt;
00059 }
00060 else
00061 {
00062 elt->prev = bitmap_ggc_free;
00063 bitmap_ggc_free = elt;
00064 }
00065 }
00066
00067
00068
00069
00070 static inline void
00071 bitmap_element_free (bitmap head, bitmap_element *elt)
00072 {
00073 bitmap_element *next = elt->next;
00074 bitmap_element *prev = elt->prev;
00075
00076 if (prev)
00077 prev->next = next;
00078
00079 if (next)
00080 next->prev = prev;
00081
00082 if (head->first == elt)
00083 head->first = next;
00084
00085
00086
00087 if (head->current == elt)
00088 {
00089 head->current = next != 0 ? next : prev;
00090 if (head->current)
00091 head->indx = head->current->indx;
00092 else
00093 head->indx = 0;
00094 }
00095 bitmap_elem_to_freelist (head, elt);
00096 }
00097
00098
00099
00100 static inline bitmap_element *
00101 bitmap_element_allocate (bitmap head)
00102 {
00103 bitmap_element *element;
00104 bitmap_obstack *bit_obstack = head->obstack;
00105
00106 if (bit_obstack)
00107 {
00108 element = bit_obstack->elements;
00109
00110 if (element)
00111
00112
00113 if (element->next)
00114 {
00115 bit_obstack->elements = element->next;
00116 bit_obstack->elements->prev = element->prev;
00117 }
00118 else
00119
00120 bit_obstack->elements = element->prev;
00121 else
00122 element = XOBNEW (&bit_obstack->obstack, bitmap_element);
00123 }
00124 else
00125 {
00126 element = bitmap_ggc_free;
00127 if (element)
00128
00129
00130 if (element->next)
00131 {
00132 bitmap_ggc_free = element->next;
00133 bitmap_ggc_free->prev = element->prev;
00134 }
00135 else
00136
00137 bitmap_ggc_free = element->prev;
00138 else
00139 element = GGC_NEW (bitmap_element);
00140 }
00141
00142 memset (element->bits, 0, sizeof (element->bits));
00143
00144 return element;
00145 }
00146
00147
00148
00149 void
00150 bitmap_elt_clear_from (bitmap head, bitmap_element *elt)
00151 {
00152 bitmap_element *prev;
00153 bitmap_obstack *bit_obstack = head->obstack;
00154
00155 if (!elt) return;
00156
00157 prev = elt->prev;
00158 if (prev)
00159 {
00160 prev->next = NULL;
00161 if (head->current->indx > prev->indx)
00162 {
00163 head->current = prev;
00164 head->indx = prev->indx;
00165 }
00166 }
00167 else
00168 {
00169 head->first = NULL;
00170 head->current = NULL;
00171 head->indx = 0;
00172 }
00173
00174
00175 if (bit_obstack)
00176 {
00177 elt->prev = bit_obstack->elements;
00178 bit_obstack->elements = elt;
00179 }
00180 else
00181 {
00182 elt->prev = bitmap_ggc_free;
00183 bitmap_ggc_free = elt;
00184 }
00185 }
00186
00187
00188
00189 inline void
00190 bitmap_clear (bitmap head)
00191 {
00192 if (head->first)
00193 bitmap_elt_clear_from (head, head->first);
00194 }
00195
00196
00197
00198
00199 void
00200 bitmap_obstack_initialize (bitmap_obstack *bit_obstack)
00201 {
00202 if (!bit_obstack)
00203 bit_obstack = &bitmap_default_obstack;
00204
00205 #if !defined(__GNUC__) || (__GNUC__ < 2)
00206 #define __alignof__(type) 0
00207 #endif
00208
00209 bit_obstack->elements = NULL;
00210 bit_obstack->heads = NULL;
00211 obstack_specify_allocation (&bit_obstack->obstack, OBSTACK_CHUNK_SIZE,
00212 __alignof__ (bitmap_element),
00213 obstack_chunk_alloc,
00214 obstack_chunk_free);
00215 }
00216
00217
00218
00219
00220 void
00221 bitmap_obstack_release (bitmap_obstack *bit_obstack)
00222 {
00223 if (!bit_obstack)
00224 bit_obstack = &bitmap_default_obstack;
00225
00226 bit_obstack->elements = NULL;
00227 bit_obstack->heads = NULL;
00228 obstack_free (&bit_obstack->obstack, NULL);
00229 }
00230
00231
00232
00233
00234 bitmap
00235 bitmap_obstack_alloc (bitmap_obstack *bit_obstack)
00236 {
00237 bitmap map;
00238
00239 if (!bit_obstack)
00240 bit_obstack = &bitmap_default_obstack;
00241 map = bit_obstack->heads;
00242 if (map)
00243 bit_obstack->heads = (void *)map->first;
00244 else
00245 map = XOBNEW (&bit_obstack->obstack, bitmap_head);
00246 bitmap_initialize (map, bit_obstack);
00247
00248 return map;
00249 }
00250
00251
00252
00253 bitmap
00254 bitmap_gc_alloc (void)
00255 {
00256 bitmap map;
00257
00258 map = GGC_NEW (struct bitmap_head_def);
00259 bitmap_initialize (map, NULL);
00260
00261 return map;
00262 }
00263
00264
00265
00266 void
00267 bitmap_obstack_free (bitmap map)
00268 {
00269 if (map)
00270 {
00271 bitmap_clear (map);
00272 map->first = (void *)map->obstack->heads;
00273 map->obstack->heads = map;
00274 }
00275 }
00276
00277
00278
00279
00280 static inline int
00281 bitmap_element_zerop (bitmap_element *element)
00282 {
00283 #if BITMAP_ELEMENT_WORDS == 2
00284 return (element->bits[0] | element->bits[1]) == 0;
00285 #else
00286 unsigned i;
00287
00288 for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
00289 if (element->bits[i] != 0)
00290 return 0;
00291
00292 return 1;
00293 #endif
00294 }
00295
00296
00297
00298 static inline void
00299 bitmap_element_link (bitmap head, bitmap_element *element)
00300 {
00301 unsigned int indx = element->indx;
00302 bitmap_element *ptr;
00303
00304
00305 if (head->first == 0)
00306 {
00307 element->next = element->prev = 0;
00308 head->first = element;
00309 }
00310
00311
00312
00313 else if (indx < head->indx)
00314 {
00315 for (ptr = head->current;
00316 ptr->prev != 0 && ptr->prev->indx > indx;
00317 ptr = ptr->prev)
00318 ;
00319
00320 if (ptr->prev)
00321 ptr->prev->next = element;
00322 else
00323 head->first = element;
00324
00325 element->prev = ptr->prev;
00326 element->next = ptr;
00327 ptr->prev = element;
00328 }
00329
00330
00331 else
00332 {
00333 for (ptr = head->current;
00334 ptr->next != 0 && ptr->next->indx < indx;
00335 ptr = ptr->next)
00336 ;
00337
00338 if (ptr->next)
00339 ptr->next->prev = element;
00340
00341 element->next = ptr->next;
00342 element->prev = ptr;
00343 ptr->next = element;
00344 }
00345
00346
00347 head->current = element;
00348 head->indx = indx;
00349 }
00350
00351
00352
00353
00354
00355 static bitmap_element *
00356 bitmap_elt_insert_after (bitmap head, bitmap_element *elt, unsigned int indx)
00357 {
00358 bitmap_element *node = bitmap_element_allocate (head);
00359 node->indx = indx;
00360
00361 if (!elt)
00362 {
00363 if (!head->current)
00364 {
00365 head->current = node;
00366 head->indx = indx;
00367 }
00368 node->next = head->first;
00369 if (node->next)
00370 node->next->prev = node;
00371 head->first = node;
00372 node->prev = NULL;
00373 }
00374 else
00375 {
00376 gcc_assert (head->current);
00377 node->next = elt->next;
00378 if (node->next)
00379 node->next->prev = node;
00380 elt->next = node;
00381 node->prev = elt;
00382 }
00383 return node;
00384 }
00385
00386
00387
00388 void
00389 bitmap_copy (bitmap to, bitmap from)
00390 {
00391 bitmap_element *from_ptr, *to_ptr = 0;
00392
00393 bitmap_clear (to);
00394
00395
00396 for (from_ptr = from->first; from_ptr; from_ptr = from_ptr->next)
00397 {
00398 bitmap_element *to_elt = bitmap_element_allocate (to);
00399
00400 to_elt->indx = from_ptr->indx;
00401 memcpy (to_elt->bits, from_ptr->bits, sizeof (to_elt->bits));
00402
00403
00404
00405 if (to_ptr == 0)
00406 {
00407 to->first = to->current = to_elt;
00408 to->indx = from_ptr->indx;
00409 to_elt->next = to_elt->prev = 0;
00410 }
00411 else
00412 {
00413 to_elt->prev = to_ptr;
00414 to_elt->next = 0;
00415 to_ptr->next = to_elt;
00416 }
00417
00418 to_ptr = to_elt;
00419 }
00420 }
00421
00422
00423
00424
00425
00426
00427 static inline bitmap_element *
00428 bitmap_find_bit (bitmap head, unsigned int bit)
00429 {
00430 bitmap_element *element;
00431 unsigned int indx = bit / BITMAP_ELEMENT_ALL_BITS;
00432
00433 if (head->current == 0
00434 || head->indx == indx)
00435 return head->current;
00436
00437 if (head->indx < indx)
00438
00439
00440 for (element = head->current;
00441 element->next != 0 && element->indx < indx;
00442 element = element->next)
00443 ;
00444
00445 else if (head->indx / 2 < indx)
00446
00447
00448 for (element = head->current;
00449 element->prev != 0 && element->indx > indx;
00450 element = element->prev)
00451 ;
00452
00453 else
00454
00455
00456 for (element = head->first;
00457 element->next != 0 && element->indx < indx;
00458 element = element->next)
00459 ;
00460
00461
00462
00463 head->current = element;
00464 head->indx = element->indx;
00465 if (element != 0 && element->indx != indx)
00466 element = 0;
00467
00468 return element;
00469 }
00470
00471
00472
00473 void
00474 bitmap_clear_bit (bitmap head, int bit)
00475 {
00476 bitmap_element *ptr = bitmap_find_bit (head, bit);
00477
00478 if (ptr != 0)
00479 {
00480 unsigned bit_num = bit % BITMAP_WORD_BITS;
00481 unsigned word_num = bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
00482 ptr->bits[word_num] &= ~ (((BITMAP_WORD) 1) << bit_num);
00483
00484
00485 if (bitmap_element_zerop (ptr))
00486 bitmap_element_free (head, ptr);
00487 }
00488 }
00489
00490
00491
00492 void
00493 bitmap_set_bit (bitmap head, int bit)
00494 {
00495 bitmap_element *ptr = bitmap_find_bit (head, bit);
00496 unsigned word_num = bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
00497 unsigned bit_num = bit % BITMAP_WORD_BITS;
00498 BITMAP_WORD bit_val = ((BITMAP_WORD) 1) << bit_num;
00499
00500 if (ptr == 0)
00501 {
00502 ptr = bitmap_element_allocate (head);
00503 ptr->indx = bit / BITMAP_ELEMENT_ALL_BITS;
00504 ptr->bits[word_num] = bit_val;
00505 bitmap_element_link (head, ptr);
00506 }
00507 else
00508 ptr->bits[word_num] |= bit_val;
00509 }
00510
00511
00512
00513 int
00514 bitmap_bit_p (bitmap head, int bit)
00515 {
00516 bitmap_element *ptr;
00517 unsigned bit_num;
00518 unsigned word_num;
00519
00520 ptr = bitmap_find_bit (head, bit);
00521 if (ptr == 0)
00522 return 0;
00523
00524 bit_num = bit % BITMAP_WORD_BITS;
00525 word_num = bit / BITMAP_WORD_BITS % BITMAP_ELEMENT_WORDS;
00526
00527 return (ptr->bits[word_num] >> bit_num) & 1;
00528 }
00529
00530 #if GCC_VERSION < 3400
00531
00532 static unsigned char popcount_table[] =
00533 {
00534 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,
00535 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,
00536 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,
00537 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,
00538 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,
00539 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,
00540 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,
00541 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,
00542 };
00543
00544 static unsigned long
00545 bitmap_popcount (BITMAP_WORD a)
00546 {
00547 unsigned long ret = 0;
00548 unsigned i;
00549
00550
00551 for (i = 0; i < BITMAP_WORD_BITS; i+= 8)
00552 ret += popcount_table[(a >> i) & 0xff];
00553 return ret;
00554 }
00555 #endif
00556
00557
00558 unsigned long
00559 bitmap_count_bits (bitmap a)
00560 {
00561 unsigned long count = 0;
00562 bitmap_element *elt;
00563 unsigned ix;
00564
00565 for (elt = a->first; elt; elt = elt->next)
00566 {
00567 for (ix = 0; ix != BITMAP_ELEMENT_WORDS; ix++)
00568 {
00569 #if GCC_VERSION >= 3400
00570
00571
00572 count += __builtin_popcountl (elt->bits[ix]);
00573 #else
00574 count += bitmap_popcount (elt->bits[ix]);
00575 #endif
00576 }
00577 }
00578 return count;
00579 }
00580
00581
00582
00583
00584
00585
00586 unsigned
00587 bitmap_first_set_bit (bitmap a)
00588 {
00589 bitmap_element *elt = a->first;
00590 unsigned bit_no;
00591 BITMAP_WORD word;
00592 unsigned ix;
00593
00594 gcc_assert (elt);
00595 bit_no = elt->indx * BITMAP_ELEMENT_ALL_BITS;
00596 for (ix = 0; ix != BITMAP_ELEMENT_WORDS; ix++)
00597 {
00598 word = elt->bits[ix];
00599 if (word)
00600 goto found_bit;
00601 }
00602 gcc_unreachable ();
00603 found_bit:
00604 bit_no += ix * BITMAP_WORD_BITS;
00605
00606 #if GCC_VERSION >= 3004
00607 gcc_assert (sizeof(long) == sizeof (word));
00608 bit_no += __builtin_ctzl (word);
00609 #else
00610
00611 #if BITMAP_WORD_BITS > 64
00612 #error "Fill out the table."
00613 #endif
00614 #if BITMAP_WORD_BITS > 32
00615 if (!(word & 0xffffffff))
00616 word >>= 32, bit_no += 32;
00617 #endif
00618 if (!(word & 0xffff))
00619 word >>= 16, bit_no += 16;
00620 if (!(word & 0xff))
00621 word >>= 8, bit_no += 8;
00622 if (!(word & 0xf))
00623 word >>= 4, bit_no += 4;
00624 if (!(word & 0x3))
00625 word >>= 2, bit_no += 2;
00626 if (!(word & 0x1))
00627 word >>= 1, bit_no += 1;
00628
00629 gcc_assert (word & 1);
00630 #endif
00631 return bit_no;
00632 }
00633
00634
00635
00636
00637 void
00638 bitmap_and (bitmap dst, bitmap a, bitmap b)
00639 {
00640 bitmap_element *dst_elt = dst->first;
00641 bitmap_element *a_elt = a->first;
00642 bitmap_element *b_elt = b->first;
00643 bitmap_element *dst_prev = NULL;
00644
00645 gcc_assert (dst != a && dst != b);
00646
00647 if (a == b)
00648 {
00649 bitmap_copy (dst, a);
00650 return;
00651 }
00652
00653 while (a_elt && b_elt)
00654 {
00655 if (a_elt->indx < b_elt->indx)
00656 a_elt = a_elt->next;
00657 else if (b_elt->indx < a_elt->indx)
00658 b_elt = b_elt->next;
00659 else
00660 {
00661
00662 unsigned ix;
00663 BITMAP_WORD ior = 0;
00664
00665 if (!dst_elt)
00666 dst_elt = bitmap_elt_insert_after (dst, dst_prev, a_elt->indx);
00667 else
00668 dst_elt->indx = a_elt->indx;
00669 for (ix = BITMAP_ELEMENT_WORDS; ix--;)
00670 {
00671 BITMAP_WORD r = a_elt->bits[ix] & b_elt->bits[ix];
00672
00673 dst_elt->bits[ix] = r;
00674 ior |= r;
00675 }
00676 if (ior)
00677 {
00678 dst_prev = dst_elt;
00679 dst_elt = dst_elt->next;
00680 }
00681 a_elt = a_elt->next;
00682 b_elt = b_elt->next;
00683 }
00684 }
00685 bitmap_elt_clear_from (dst, dst_elt);
00686 gcc_assert (!dst->current == !dst->first);
00687 if (dst->current)
00688 dst->indx = dst->current->indx;
00689 }
00690
00691
00692
00693 void
00694 bitmap_and_into (bitmap a, bitmap b)
00695 {
00696 bitmap_element *a_elt = a->first;
00697 bitmap_element *b_elt = b->first;
00698 bitmap_element *next;
00699
00700 if (a == b)
00701 return;
00702
00703 while (a_elt && b_elt)
00704 {
00705 if (a_elt->indx < b_elt->indx)
00706 {
00707 next = a_elt->next;
00708 bitmap_element_free (a, a_elt);
00709 a_elt = next;
00710 }
00711 else if (b_elt->indx < a_elt->indx)
00712 b_elt = b_elt->next;
00713 else
00714 {
00715
00716 unsigned ix;
00717 BITMAP_WORD ior = 0;
00718
00719 for (ix = BITMAP_ELEMENT_WORDS; ix--;)
00720 {
00721 BITMAP_WORD r = a_elt->bits[ix] & b_elt->bits[ix];
00722
00723 a_elt->bits[ix] = r;
00724 ior |= r;
00725 }
00726 next = a_elt->next;
00727 if (!ior)
00728 bitmap_element_free (a, a_elt);
00729 a_elt = next;
00730 b_elt = b_elt->next;
00731 }
00732 }
00733 bitmap_elt_clear_from (a, a_elt);
00734 gcc_assert (!a->current == !a->first);
00735 gcc_assert (!a->current || a->indx == a->current->indx);
00736 }
00737
00738
00739
00740 void
00741 bitmap_and_compl (bitmap dst, bitmap a, bitmap b)
00742 {
00743 bitmap_element *dst_elt = dst->first;
00744 bitmap_element *a_elt = a->first;
00745 bitmap_element *b_elt = b->first;
00746 bitmap_element *dst_prev = NULL;
00747
00748 gcc_assert (dst != a && dst != b);
00749
00750 if (a == b)
00751 {
00752 bitmap_clear (dst);
00753 return;
00754 }
00755
00756 while (a_elt)
00757 {
00758 if (!b_elt || a_elt->indx < b_elt->indx)
00759 {
00760
00761 if (!dst_elt)
00762 dst_elt = bitmap_elt_insert_after (dst, dst_prev, a_elt->indx);
00763 else
00764 dst_elt->indx = a_elt->indx;
00765 memcpy (dst_elt->bits, a_elt->bits, sizeof (dst_elt->bits));
00766 dst_prev = dst_elt;
00767 dst_elt = dst_elt->next;
00768 a_elt = a_elt->next;
00769 }
00770 else if (b_elt->indx < a_elt->indx)
00771 b_elt = b_elt->next;
00772 else
00773 {
00774
00775 unsigned ix;
00776 BITMAP_WORD ior = 0;
00777
00778 if (!dst_elt)
00779 dst_elt = bitmap_elt_insert_after (dst, dst_prev, a_elt->indx);
00780 else
00781 dst_elt->indx = a_elt->indx;
00782 for (ix = BITMAP_ELEMENT_WORDS; ix--;)
00783 {
00784 BITMAP_WORD r = a_elt->bits[ix] & ~b_elt->bits[ix];
00785
00786 dst_elt->bits[ix] = r;
00787 ior |= r;
00788 }
00789 if (ior)
00790 {
00791 dst_prev = dst_elt;
00792 dst_elt = dst_elt->next;
00793 }
00794 a_elt = a_elt->next;
00795 b_elt = b_elt->next;
00796 }
00797 }
00798 bitmap_elt_clear_from (dst, dst_elt);
00799 gcc_assert (!dst->current == !dst->first);
00800 if (dst->current)
00801 dst->indx = dst->current->indx;
00802 }
00803
00804
00805
00806 bool
00807 bitmap_and_compl_into (bitmap a, bitmap b)
00808 {
00809 bitmap_element *a_elt = a->first;
00810 bitmap_element *b_elt = b->first;
00811 bitmap_element *next;
00812 BITMAP_WORD changed = 0;
00813
00814 if (a == b)
00815 {
00816 if (bitmap_empty_p (a))
00817 return false;
00818 else
00819 {
00820 bitmap_clear (a);
00821 return true;
00822 }
00823 }
00824
00825 while (a_elt && b_elt)
00826 {
00827 if (a_elt->indx < b_elt->indx)
00828 a_elt = a_elt->next;
00829 else if (b_elt->indx < a_elt->indx)
00830 b_elt = b_elt->next;
00831 else
00832 {
00833
00834 unsigned ix;
00835 BITMAP_WORD ior = 0;
00836
00837 for (ix = BITMAP_ELEMENT_WORDS; ix--;)
00838 {
00839 BITMAP_WORD cleared = a_elt->bits[ix] & b_elt->bits[ix];
00840 BITMAP_WORD r = a_elt->bits[ix] ^ cleared;
00841
00842 a_elt->bits[ix] = r;
00843 changed |= cleared;
00844 ior |= r;
00845 }
00846 next = a_elt->next;
00847 if (!ior)
00848 bitmap_element_free (a, a_elt);
00849 a_elt = next;
00850 b_elt = b_elt->next;
00851 }
00852 }
00853 gcc_assert (!a->current == !a->first);
00854 gcc_assert (!a->current || a->indx == a->current->indx);
00855 return changed != 0;
00856 }
00857
00858
00859 void
00860 bitmap_clear_range (bitmap head, unsigned int start, unsigned int count)
00861 {
00862 unsigned int first_index = start / BITMAP_ELEMENT_ALL_BITS;
00863 unsigned int end_bit_plus1 = start + count;
00864 unsigned int end_bit = end_bit_plus1 - 1;
00865 unsigned int last_index = (end_bit) / BITMAP_ELEMENT_ALL_BITS;
00866 bitmap_element *elt = bitmap_find_bit (head, start);
00867
00868
00869
00870
00871 if (!elt)
00872 {
00873 if (head->current)
00874 {
00875 if (head->indx < first_index)
00876 {
00877 elt = head->current->next;
00878 if (!elt)
00879 return;
00880 }
00881 else
00882 elt = head->current;
00883 }
00884 else
00885 return;
00886 }
00887
00888 while (elt && (elt->indx <= last_index))
00889 {
00890 bitmap_element * next_elt = elt->next;
00891 unsigned elt_start_bit = (elt->indx) * BITMAP_ELEMENT_ALL_BITS;
00892 unsigned elt_end_bit_plus1 = elt_start_bit + BITMAP_ELEMENT_ALL_BITS;
00893
00894
00895 if (elt_start_bit >= start && elt_end_bit_plus1 <= end_bit_plus1)
00896
00897 bitmap_element_free (head, elt);
00898 else
00899 {
00900
00901 unsigned int first_word_to_mod;
00902 BITMAP_WORD first_mask;
00903 unsigned int last_word_to_mod;
00904 BITMAP_WORD last_mask;
00905 unsigned int i;
00906 bool clear = true;
00907
00908 if (elt_start_bit <= start)
00909 {
00910
00911
00912 first_word_to_mod = (start - elt_start_bit) / BITMAP_WORD_BITS;
00913
00914
00915 first_mask =
00916 (((BITMAP_WORD) 1) << ((start % BITMAP_WORD_BITS))) - 1;
00917 first_mask = ~first_mask;
00918 }
00919 else
00920 {
00921
00922 first_word_to_mod = 0;
00923 first_mask = 0;
00924 first_mask = ~first_mask;
00925 }
00926
00927 if (elt_end_bit_plus1 <= end_bit_plus1)
00928 {
00929
00930 last_word_to_mod = BITMAP_ELEMENT_WORDS - 1;
00931 last_mask = 0;
00932 last_mask = ~last_mask;
00933 }
00934 else
00935 {
00936
00937 last_word_to_mod =
00938 (end_bit_plus1 - elt_start_bit) / BITMAP_WORD_BITS;
00939
00940
00941 last_mask =
00942 (((BITMAP_WORD) 1) << (((end_bit_plus1) % BITMAP_WORD_BITS))) - 1;
00943 }
00944
00945
00946 if (first_word_to_mod == last_word_to_mod)
00947 {
00948 BITMAP_WORD mask = first_mask & last_mask;
00949 elt->bits[first_word_to_mod] &= ~mask;
00950 }
00951 else
00952 {
00953 elt->bits[first_word_to_mod] &= ~first_mask;
00954 for (i = first_word_to_mod + 1; i < last_word_to_mod; i++)
00955 elt->bits[i] = 0;
00956 elt->bits[last_word_to_mod] &= ~last_mask;
00957 }
00958 for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
00959 if (elt->bits[i])
00960 {
00961 clear = false;
00962 break;
00963 }
00964
00965 if (clear)
00966 bitmap_element_free (head, elt);
00967 }
00968 elt = next_elt;
00969 }
00970
00971 if (elt)
00972 {
00973 head->current = elt;
00974 head->indx = head->current->indx;
00975 }
00976 }
00977
00978
00979
00980 void
00981 bitmap_compl_and_into (bitmap a, bitmap b)
00982 {
00983 bitmap_element *a_elt = a->first;
00984 bitmap_element *b_elt = b->first;
00985 bitmap_element *a_prev = NULL;
00986 bitmap_element *next;
00987
00988 gcc_assert (a != b);
00989
00990 if (bitmap_empty_p (a))
00991 {
00992 bitmap_copy (a, b);
00993 return;
00994 }
00995 if (bitmap_empty_p (b))
00996 {
00997 bitmap_clear (a);
00998 return;
00999 }
01000
01001 while (a_elt || b_elt)
01002 {
01003 if (!b_elt || (a_elt && a_elt->indx < b_elt->indx))
01004 {
01005
01006 next = a_elt->next;
01007 a_prev = a_elt->prev;
01008 bitmap_element_free (a, a_elt);
01009 a_elt = next;
01010 }
01011 else if (!a_elt || b_elt->indx < a_elt->indx)
01012 {
01013
01014 next = bitmap_elt_insert_after (a, a_prev, b_elt->indx);
01015 memcpy (next->bits, b_elt->bits, sizeof (next->bits));
01016 a_prev = next;
01017 b_elt = b_elt->next;
01018 }
01019 else
01020 {
01021
01022 unsigned ix;
01023 BITMAP_WORD ior = 0;
01024
01025 for (ix = BITMAP_ELEMENT_WORDS; ix--;)
01026 {
01027 BITMAP_WORD cleared = a_elt->bits[ix] & b_elt->bits[ix];
01028 BITMAP_WORD r = b_elt->bits[ix] ^ cleared;
01029
01030 a_elt->bits[ix] = r;
01031 ior |= r;
01032 }
01033 next = a_elt->next;
01034 if (!ior)
01035 bitmap_element_free (a, a_elt);
01036 else
01037 a_prev = a_elt;
01038 a_elt = next;
01039 b_elt = b_elt->next;
01040 }
01041 }
01042 gcc_assert (!a->current == !a->first);
01043 gcc_assert (!a->current || a->indx == a->current->indx);
01044 return;
01045 }
01046
01047
01048
01049 bool
01050 bitmap_ior (bitmap dst, bitmap a, bitmap b)
01051 {
01052 bitmap_element *dst_elt = dst->first;
01053 bitmap_element *a_elt = a->first;
01054 bitmap_element *b_elt = b->first;
01055 bitmap_element *dst_prev = NULL;
01056 bool changed = false;
01057
01058 gcc_assert (dst != a && dst != b);
01059
01060 while (a_elt || b_elt)
01061 {
01062 if (a_elt && b_elt && a_elt->indx == b_elt->indx)
01063 {
01064
01065 unsigned ix;
01066
01067 if (!changed && dst_elt && dst_elt->indx == a_elt->indx)
01068 {
01069 for (ix = BITMAP_ELEMENT_WORDS; ix--;)
01070 {
01071 BITMAP_WORD r = a_elt->bits[ix] | b_elt->bits[ix];
01072
01073 if (r != dst_elt->bits[ix])
01074 {
01075 dst_elt->bits[ix] = r;
01076 changed = true;
01077 }
01078 }
01079 }
01080 else
01081 {
01082 changed = true;
01083 if (!dst_elt)
01084 dst_elt = bitmap_elt_insert_after (dst, dst_prev, a_elt->indx);
01085 else
01086 dst_elt->indx = a_elt->indx;
01087 for (ix = BITMAP_ELEMENT_WORDS; ix--;)
01088 {
01089 BITMAP_WORD r = a_elt->bits[ix] | b_elt->bits[ix];
01090
01091 dst_elt->bits[ix] = r;
01092 }
01093 }
01094 a_elt = a_elt->next;
01095 b_elt = b_elt->next;
01096 dst_prev = dst_elt;
01097 dst_elt = dst_elt->next;
01098 }
01099 else
01100 {
01101
01102 bitmap_element *src;
01103
01104 if (!b_elt || (a_elt && a_elt->indx < b_elt->indx))
01105 {
01106 src = a_elt;
01107 a_elt = a_elt->next;
01108 }
01109 else
01110 {
01111 src = b_elt;
01112 b_elt = b_elt->next;
01113 }
01114
01115 if (!changed && dst_elt && dst_elt->indx == src->indx)
01116 {
01117 unsigned ix;
01118
01119 for (ix = BITMAP_ELEMENT_WORDS; ix--;)
01120 if (src->bits[ix] != dst_elt->bits[ix])
01121 {
01122 dst_elt->bits[ix] = src->bits[ix];
01123 changed = true;
01124 }
01125 }
01126 else
01127 {
01128 changed = true;
01129 if (!dst_elt)
01130 dst_elt = bitmap_elt_insert_after (dst, dst_prev, src->indx);
01131 else
01132 dst_elt->indx = src->indx;
01133 memcpy (dst_elt->bits, src->bits, sizeof (dst_elt->bits));
01134 }
01135
01136 dst_prev = dst_elt;
01137 dst_elt = dst_elt->next;
01138 }
01139 }
01140
01141 if (dst_elt)
01142 {
01143 changed = true;
01144 bitmap_elt_clear_from (dst, dst_elt);
01145 }
01146 gcc_assert (!dst->current == !dst->first);
01147 if (dst->current)
01148 dst->indx = dst->current->indx;
01149 return changed;
01150 }
01151
01152
01153
01154 bool
01155 bitmap_ior_into (bitmap a, bitmap b)
01156 {
01157 bitmap_element *a_elt = a->first;
01158 bitmap_element *b_elt = b->first;
01159 bitmap_element *a_prev = NULL;
01160 bool changed = false;
01161
01162 if (a == b)
01163 return false;
01164
01165 while (b_elt)
01166 {
01167 if (!a_elt || b_elt->indx < a_elt->indx)
01168 {
01169
01170 bitmap_element *dst = bitmap_elt_insert_after (a, a_prev, b_elt->indx);
01171 memcpy (dst->bits, b_elt->bits, sizeof (dst->bits));
01172 a_prev = dst;
01173 b_elt = b_elt->next;
01174 changed = true;
01175 }
01176 else if (a_elt->indx < b_elt->indx)
01177 {
01178 a_prev = a_elt;
01179 a_elt = a_elt->next;
01180 }
01181 else
01182 {
01183
01184 unsigned ix;
01185
01186 if (changed)
01187 for (ix = BITMAP_ELEMENT_WORDS; ix--;)
01188 {
01189 BITMAP_WORD r = a_elt->bits[ix] | b_elt->bits[ix];
01190
01191 a_elt->bits[ix] = r;
01192 }
01193 else
01194 for (ix = BITMAP_ELEMENT_WORDS; ix--;)
01195 {
01196 BITMAP_WORD r = a_elt->bits[ix] | b_elt->bits[ix];
01197
01198 if (a_elt->bits[ix] != r)
01199 {
01200 changed = true;
01201 a_elt->bits[ix] = r;
01202 }
01203 }
01204 b_elt = b_elt->next;
01205 a_prev = a_elt;
01206 a_elt = a_elt->next;
01207 }
01208 }
01209 gcc_assert (!a->current == !a->first);
01210 if (a->current)
01211 a->indx = a->current->indx;
01212 return changed;
01213 }
01214
01215
01216
01217 void
01218 bitmap_xor (bitmap dst, bitmap a, bitmap b)
01219 {
01220 bitmap_element *dst_elt = dst->first;
01221 bitmap_element *a_elt = a->first;
01222 bitmap_element *b_elt = b->first;
01223 bitmap_element *dst_prev = NULL;
01224
01225 gcc_assert (dst != a && dst != b);
01226 if (a == b)
01227 {
01228 bitmap_clear (dst);
01229 return;
01230 }
01231
01232 while (a_elt || b_elt)
01233 {
01234 if (a_elt && b_elt && a_elt->indx == b_elt->indx)
01235 {
01236
01237 unsigned ix;
01238 BITMAP_WORD ior = 0;
01239
01240 if (!dst_elt)
01241 dst_elt = bitmap_elt_insert_after (dst, dst_prev, a_elt->indx);
01242 else
01243 dst_elt->indx = a_elt->indx;
01244 for (ix = BITMAP_ELEMENT_WORDS; ix--;)
01245 {
01246 BITMAP_WORD r = a_elt->bits[ix] ^ b_elt->bits[ix];
01247
01248 ior |= r;
01249 dst_elt->bits[ix] = r;
01250 }
01251 a_elt = a_elt->next;
01252 b_elt = b_elt->next;
01253 if (ior)
01254 {
01255 dst_prev = dst_elt;
01256 dst_elt = dst_elt->next;
01257 }
01258 }
01259 else
01260 {
01261
01262 bitmap_element *src;
01263
01264 if (!b_elt || (a_elt && a_elt->indx < b_elt->indx))
01265 {
01266 src = a_elt;
01267 a_elt = a_elt->next;
01268 }
01269 else
01270 {
01271 src = b_elt;
01272 b_elt = b_elt->next;
01273 }
01274
01275 if (!dst_elt)
01276 dst_elt = bitmap_elt_insert_after (dst, dst_prev, src->indx);
01277 else
01278 dst_elt->indx = src->indx;
01279 memcpy (dst_elt->bits, src->bits, sizeof (dst_elt->bits));
01280 dst_prev = dst_elt;
01281 dst_elt = dst_elt->next;
01282 }
01283 }
01284 bitmap_elt_clear_from (dst, dst_elt);
01285 gcc_assert (!dst->current == !dst->first);
01286 if (dst->current)
01287 dst->indx = dst->current->indx;
01288 }
01289
01290
01291
01292 void
01293 bitmap_xor_into (bitmap a, bitmap b)
01294 {
01295 bitmap_element *a_elt = a->first;
01296 bitmap_element *b_elt = b->first;
01297 bitmap_element *a_prev = NULL;
01298
01299 if (a == b)
01300 {
01301 bitmap_clear (a);
01302 return;
01303 }
01304
01305 while (b_elt)
01306 {
01307 if (!a_elt || b_elt->indx < a_elt->indx)
01308 {
01309
01310 bitmap_element *dst = bitmap_elt_insert_after (a, a_prev, b_elt->indx);
01311 memcpy (dst->bits, b_elt->bits, sizeof (dst->bits));
01312 a_prev = dst;
01313 b_elt = b_elt->next;
01314 }
01315 else if (a_elt->indx < b_elt->indx)
01316 {
01317 a_prev = a_elt;
01318 a_elt = a_elt->next;
01319 }
01320 else
01321 {
01322
01323 unsigned ix;
01324 BITMAP_WORD ior = 0;
01325 bitmap_element *next = a_elt->next;
01326
01327 for (ix = BITMAP_ELEMENT_WORDS; ix--;)
01328 {
01329 BITMAP_WORD r = a_elt->bits[ix] ^ b_elt->bits[ix];
01330
01331 ior |= r;
01332 a_elt->bits[ix] = r;
01333 }
01334 b_elt = b_elt->next;
01335 if (ior)
01336 a_prev = a_elt;
01337 else
01338 bitmap_element_free (a, a_elt);
01339 a_elt = next;
01340 }
01341 }
01342 gcc_assert (!a->current == !a->first);
01343 if (a->current)
01344 a->indx = a->current->indx;
01345 }
01346
01347
01348
01349
01350
01351 bool
01352 bitmap_equal_p (bitmap a, bitmap b)
01353 {
01354 bitmap_element *a_elt;
01355 bitmap_element *b_elt;
01356 unsigned ix;
01357
01358 for (a_elt = a->first, b_elt = b->first;
01359 a_elt && b_elt;
01360 a_elt = a_elt->next, b_elt = b_elt->next)
01361 {
01362 if (a_elt->indx != b_elt->indx)
01363 return false;
01364 for (ix = BITMAP_ELEMENT_WORDS; ix--;)
01365 if (a_elt->bits[ix] != b_elt->bits[ix])
01366 return false;
01367 }
01368 return !a_elt && !b_elt;
01369 }
01370
01371
01372
01373 bool
01374 bitmap_intersect_p (bitmap a, bitmap b)
01375 {
01376 bitmap_element *a_elt;
01377 bitmap_element *b_elt;
01378 unsigned ix;
01379
01380 for (a_elt = a->first, b_elt = b->first;
01381 a_elt && b_elt;)
01382 {
01383 if (a_elt->indx < b_elt->indx)
01384 a_elt = a_elt->next;
01385 else if (b_elt->indx < a_elt->indx)
01386 b_elt = b_elt->next;
01387 else
01388 {
01389 for (ix = BITMAP_ELEMENT_WORDS; ix--;)
01390 if (a_elt->bits[ix] & b_elt->bits[ix])
01391 return true;
01392 a_elt = a_elt->next;
01393 b_elt = b_elt->next;
01394 }
01395 }
01396 return false;
01397 }
01398
01399
01400
01401 bool
01402 bitmap_intersect_compl_p (bitmap a, bitmap b)
01403 {
01404 bitmap_element *a_elt;
01405 bitmap_element *b_elt;
01406 unsigned ix;
01407 for (a_elt = a->first, b_elt = b->first;
01408 a_elt && b_elt;)
01409 {
01410 if (a_elt->indx < b_elt->indx)
01411 return true;
01412 else if (b_elt->indx < a_elt->indx)
01413 b_elt = b_elt->next;
01414 else
01415 {
01416 for (ix = BITMAP_ELEMENT_WORDS; ix--;)
01417 if (a_elt->bits[ix] & ~b_elt->bits[ix])
01418 return true;
01419 a_elt = a_elt->next;
01420 b_elt = b_elt->next;
01421 }
01422 }
01423 return a_elt != NULL;
01424 }
01425
01426
01427
01428
01429 bool
01430 bitmap_ior_and_compl (bitmap dst, bitmap a, bitmap from1, bitmap from2)
01431 {
01432 bitmap_head tmp;
01433 bool changed;
01434
01435 bitmap_initialize (&tmp, &bitmap_default_obstack);
01436 bitmap_and_compl (&tmp, from1, from2);
01437 changed = bitmap_ior (dst, a, &tmp);
01438 bitmap_clear (&tmp);
01439
01440 return changed;
01441 }
01442
01443
01444
01445 bool
01446 bitmap_ior_and_compl_into (bitmap a, bitmap from1, bitmap from2)
01447 {
01448 bitmap_head tmp;
01449 bool changed;
01450
01451 bitmap_initialize (&tmp, &bitmap_default_obstack);
01452 bitmap_and_compl (&tmp, from1, from2);
01453 changed = bitmap_ior_into (a, &tmp);
01454 bitmap_clear (&tmp);
01455
01456 return changed;
01457 }
01458
01459
01460
01461 void
01462 debug_bitmap_file (FILE *file, bitmap head)
01463 {
01464 bitmap_element *ptr;
01465
01466 fprintf (file, "\nfirst = %p current = %p indx = %u\n",
01467 (void *) head->first, (void *) head->current, head->indx);
01468
01469 for (ptr = head->first; ptr; ptr = ptr->next)
01470 {
01471 unsigned int i, j, col = 26;
01472
01473 fprintf (file, "\t%p next = %p prev = %p indx = %u\n\t\tbits = {",
01474 (void*) ptr, (void*) ptr->next, (void*) ptr->prev, ptr->indx);
01475
01476 for (i = 0; i < BITMAP_ELEMENT_WORDS; i++)
01477 for (j = 0; j < BITMAP_WORD_BITS; j++)
01478 if ((ptr->bits[i] >> j) & 1)
01479 {
01480 if (col > 70)
01481 {
01482 fprintf (file, "\n\t\t\t");
01483 col = 24;
01484 }
01485
01486 fprintf (file, " %u", (ptr->indx * BITMAP_ELEMENT_ALL_BITS
01487 + i * BITMAP_WORD_BITS + j));
01488 col += 4;
01489 }
01490
01491 fprintf (file, " }\n");
01492 }
01493 }
01494
01495
01496
01497
01498 void
01499 debug_bitmap (bitmap head)
01500 {
01501 debug_bitmap_file (stdout, head);
01502 }
01503
01504
01505
01506
01507 void
01508 bitmap_print (FILE *file, bitmap head, const char *prefix, const char *suffix)
01509 {
01510 const char *comma = "";
01511 unsigned i;
01512 bitmap_iterator bi;
01513
01514 fputs (prefix, file);
01515 EXECUTE_IF_SET_IN_BITMAP (head, 0, i, bi)
01516 {
01517 fprintf (file, "%s%d", comma, i);
01518 comma = ", ";
01519 }
01520 fputs (suffix, file);
01521 }
01522
01523
01524 hashval_t
01525 bitmap_hash (bitmap head)
01526 {
01527 bitmap_element *ptr;
01528 BITMAP_WORD hash = 0;
01529 int ix;
01530
01531 for (ptr = head->first; ptr; ptr = ptr->next)
01532 {
01533 hash ^= ptr->indx;
01534 for (ix = 0; ix != BITMAP_ELEMENT_WORDS; ix++)
01535 hash ^= ptr->bits[ix];
01536 }
01537 return (hashval_t)hash;
01538 }
01539
01540 #include "gt-bitmap.h"