src/share/vm/utilities/bitMap.cpp

Fri, 16 Aug 2013 14:11:40 -0700

author
kvn
date
Fri, 16 Aug 2013 14:11:40 -0700
changeset 5543
4b2838704fd5
parent 5278
b9d151496930
child 6198
55fb97c4c58d
child 6461
bdd155477289
permissions
-rw-r--r--

8021898: Broken JIT compiler optimization for loop unswitching
Summary: fix method clone_projs() to clone all related MachProj nodes.
Reviewed-by: roland, adlertz

     1 /*
     2  * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  *
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    22  *
    23  */
    25 #include "precompiled.hpp"
    26 #include "memory/allocation.inline.hpp"
    27 #include "utilities/bitMap.inline.hpp"
    28 #include "utilities/copy.hpp"
    29 #ifdef TARGET_OS_FAMILY_linux
    30 # include "os_linux.inline.hpp"
    31 #endif
    32 #ifdef TARGET_OS_FAMILY_solaris
    33 # include "os_solaris.inline.hpp"
    34 #endif
    35 #ifdef TARGET_OS_FAMILY_windows
    36 # include "os_windows.inline.hpp"
    37 #endif
    38 #ifdef TARGET_OS_FAMILY_bsd
    39 # include "os_bsd.inline.hpp"
    40 #endif
    43 BitMap::BitMap(bm_word_t* map, idx_t size_in_bits) :
    44   _map(map), _size(size_in_bits), _map_allocator(false)
    45 {
    46   assert(sizeof(bm_word_t) == BytesPerWord, "Implementation assumption.");
    47   assert(size_in_bits >= 0, "just checking");
    48 }
    51 BitMap::BitMap(idx_t size_in_bits, bool in_resource_area) :
    52   _map(NULL), _size(0), _map_allocator(false)
    53 {
    54   assert(sizeof(bm_word_t) == BytesPerWord, "Implementation assumption.");
    55   resize(size_in_bits, in_resource_area);
    56 }
    58 void BitMap::resize(idx_t size_in_bits, bool in_resource_area) {
    59   assert(size_in_bits >= 0, "just checking");
    60   idx_t old_size_in_words = size_in_words();
    61   bm_word_t* old_map = map();
    63   _size = size_in_bits;
    64   idx_t new_size_in_words = size_in_words();
    65   if (in_resource_area) {
    66     _map = NEW_RESOURCE_ARRAY(bm_word_t, new_size_in_words);
    67   } else {
    68     if (old_map != NULL) {
    69       _map_allocator.free();
    70     }
    71     _map = _map_allocator.allocate(new_size_in_words);
    72   }
    73   Copy::disjoint_words((HeapWord*)old_map, (HeapWord*) _map,
    74                        MIN2(old_size_in_words, new_size_in_words));
    75   if (new_size_in_words > old_size_in_words) {
    76     clear_range_of_words(old_size_in_words, size_in_words());
    77   }
    78 }
    80 void BitMap::set_range_within_word(idx_t beg, idx_t end) {
    81   // With a valid range (beg <= end), this test ensures that end != 0, as
    82   // required by inverted_bit_mask_for_range.  Also avoids an unnecessary write.
    83   if (beg != end) {
    84     bm_word_t mask = inverted_bit_mask_for_range(beg, end);
    85     *word_addr(beg) |= ~mask;
    86   }
    87 }
    89 void BitMap::clear_range_within_word(idx_t beg, idx_t end) {
    90   // With a valid range (beg <= end), this test ensures that end != 0, as
    91   // required by inverted_bit_mask_for_range.  Also avoids an unnecessary write.
    92   if (beg != end) {
    93     bm_word_t mask = inverted_bit_mask_for_range(beg, end);
    94     *word_addr(beg) &= mask;
    95   }
    96 }
    98 void BitMap::par_put_range_within_word(idx_t beg, idx_t end, bool value) {
    99   assert(value == 0 || value == 1, "0 for clear, 1 for set");
   100   // With a valid range (beg <= end), this test ensures that end != 0, as
   101   // required by inverted_bit_mask_for_range.  Also avoids an unnecessary write.
   102   if (beg != end) {
   103     intptr_t* pw  = (intptr_t*)word_addr(beg);
   104     intptr_t  w   = *pw;
   105     intptr_t  mr  = (intptr_t)inverted_bit_mask_for_range(beg, end);
   106     intptr_t  nw  = value ? (w | ~mr) : (w & mr);
   107     while (true) {
   108       intptr_t res = Atomic::cmpxchg_ptr(nw, pw, w);
   109       if (res == w) break;
   110       w  = *pw;
   111       nw = value ? (w | ~mr) : (w & mr);
   112     }
   113   }
   114 }
   116 void BitMap::set_range(idx_t beg, idx_t end) {
   117   verify_range(beg, end);
   119   idx_t beg_full_word = word_index_round_up(beg);
   120   idx_t end_full_word = word_index(end);
   122   if (beg_full_word < end_full_word) {
   123     // The range includes at least one full word.
   124     set_range_within_word(beg, bit_index(beg_full_word));
   125     set_range_of_words(beg_full_word, end_full_word);
   126     set_range_within_word(bit_index(end_full_word), end);
   127   } else {
   128     // The range spans at most 2 partial words.
   129     idx_t boundary = MIN2(bit_index(beg_full_word), end);
   130     set_range_within_word(beg, boundary);
   131     set_range_within_word(boundary, end);
   132   }
   133 }
   135 void BitMap::clear_range(idx_t beg, idx_t end) {
   136   verify_range(beg, end);
   138   idx_t beg_full_word = word_index_round_up(beg);
   139   idx_t end_full_word = word_index(end);
   141   if (beg_full_word < end_full_word) {
   142     // The range includes at least one full word.
   143     clear_range_within_word(beg, bit_index(beg_full_word));
   144     clear_range_of_words(beg_full_word, end_full_word);
   145     clear_range_within_word(bit_index(end_full_word), end);
   146   } else {
   147     // The range spans at most 2 partial words.
   148     idx_t boundary = MIN2(bit_index(beg_full_word), end);
   149     clear_range_within_word(beg, boundary);
   150     clear_range_within_word(boundary, end);
   151   }
   152 }
   154 void BitMap::set_large_range(idx_t beg, idx_t end) {
   155   verify_range(beg, end);
   157   idx_t beg_full_word = word_index_round_up(beg);
   158   idx_t end_full_word = word_index(end);
   160   assert(end_full_word - beg_full_word >= 32,
   161          "the range must include at least 32 bytes");
   163   // The range includes at least one full word.
   164   set_range_within_word(beg, bit_index(beg_full_word));
   165   set_large_range_of_words(beg_full_word, end_full_word);
   166   set_range_within_word(bit_index(end_full_word), end);
   167 }
   169 void BitMap::clear_large_range(idx_t beg, idx_t end) {
   170   verify_range(beg, end);
   172   idx_t beg_full_word = word_index_round_up(beg);
   173   idx_t end_full_word = word_index(end);
   175   assert(end_full_word - beg_full_word >= 32,
   176          "the range must include at least 32 bytes");
   178   // The range includes at least one full word.
   179   clear_range_within_word(beg, bit_index(beg_full_word));
   180   clear_large_range_of_words(beg_full_word, end_full_word);
   181   clear_range_within_word(bit_index(end_full_word), end);
   182 }
   184 void BitMap::at_put(idx_t offset, bool value) {
   185   if (value) {
   186     set_bit(offset);
   187   } else {
   188     clear_bit(offset);
   189   }
   190 }
   192 // Return true to indicate that this thread changed
   193 // the bit, false to indicate that someone else did.
   194 // In either case, the requested bit is in the
   195 // requested state some time during the period that
   196 // this thread is executing this call. More importantly,
   197 // if no other thread is executing an action to
   198 // change the requested bit to a state other than
   199 // the one that this thread is trying to set it to,
   200 // then the the bit is in the expected state
   201 // at exit from this method. However, rather than
   202 // make such a strong assertion here, based on
   203 // assuming such constrained use (which though true
   204 // today, could change in the future to service some
   205 // funky parallel algorithm), we encourage callers
   206 // to do such verification, as and when appropriate.
   207 bool BitMap::par_at_put(idx_t bit, bool value) {
   208   return value ? par_set_bit(bit) : par_clear_bit(bit);
   209 }
   211 void BitMap::at_put_grow(idx_t offset, bool value) {
   212   if (offset >= size()) {
   213     resize(2 * MAX2(size(), offset));
   214   }
   215   at_put(offset, value);
   216 }
   218 void BitMap::at_put_range(idx_t start_offset, idx_t end_offset, bool value) {
   219   if (value) {
   220     set_range(start_offset, end_offset);
   221   } else {
   222     clear_range(start_offset, end_offset);
   223   }
   224 }
   226 void BitMap::par_at_put_range(idx_t beg, idx_t end, bool value) {
   227   verify_range(beg, end);
   229   idx_t beg_full_word = word_index_round_up(beg);
   230   idx_t end_full_word = word_index(end);
   232   if (beg_full_word < end_full_word) {
   233     // The range includes at least one full word.
   234     par_put_range_within_word(beg, bit_index(beg_full_word), value);
   235     if (value) {
   236       set_range_of_words(beg_full_word, end_full_word);
   237     } else {
   238       clear_range_of_words(beg_full_word, end_full_word);
   239     }
   240     par_put_range_within_word(bit_index(end_full_word), end, value);
   241   } else {
   242     // The range spans at most 2 partial words.
   243     idx_t boundary = MIN2(bit_index(beg_full_word), end);
   244     par_put_range_within_word(beg, boundary, value);
   245     par_put_range_within_word(boundary, end, value);
   246   }
   248 }
   250 void BitMap::at_put_large_range(idx_t beg, idx_t end, bool value) {
   251   if (value) {
   252     set_large_range(beg, end);
   253   } else {
   254     clear_large_range(beg, end);
   255   }
   256 }
   258 void BitMap::par_at_put_large_range(idx_t beg, idx_t end, bool value) {
   259   verify_range(beg, end);
   261   idx_t beg_full_word = word_index_round_up(beg);
   262   idx_t end_full_word = word_index(end);
   264   assert(end_full_word - beg_full_word >= 32,
   265          "the range must include at least 32 bytes");
   267   // The range includes at least one full word.
   268   par_put_range_within_word(beg, bit_index(beg_full_word), value);
   269   if (value) {
   270     set_large_range_of_words(beg_full_word, end_full_word);
   271   } else {
   272     clear_large_range_of_words(beg_full_word, end_full_word);
   273   }
   274   par_put_range_within_word(bit_index(end_full_word), end, value);
   275 }
   277 bool BitMap::contains(const BitMap other) const {
   278   assert(size() == other.size(), "must have same size");
   279   bm_word_t* dest_map = map();
   280   bm_word_t* other_map = other.map();
   281   idx_t size = size_in_words();
   282   for (idx_t index = 0; index < size_in_words(); index++) {
   283     bm_word_t word_union = dest_map[index] | other_map[index];
   284     // If this has more bits set than dest_map[index], then other is not a
   285     // subset.
   286     if (word_union != dest_map[index]) return false;
   287   }
   288   return true;
   289 }
   291 bool BitMap::intersects(const BitMap other) const {
   292   assert(size() == other.size(), "must have same size");
   293   bm_word_t* dest_map = map();
   294   bm_word_t* other_map = other.map();
   295   idx_t size = size_in_words();
   296   for (idx_t index = 0; index < size_in_words(); index++) {
   297     if ((dest_map[index] & other_map[index]) != 0) return true;
   298   }
   299   // Otherwise, no intersection.
   300   return false;
   301 }
   303 void BitMap::set_union(BitMap other) {
   304   assert(size() == other.size(), "must have same size");
   305   bm_word_t* dest_map = map();
   306   bm_word_t* other_map = other.map();
   307   idx_t size = size_in_words();
   308   for (idx_t index = 0; index < size_in_words(); index++) {
   309     dest_map[index] = dest_map[index] | other_map[index];
   310   }
   311 }
   314 void BitMap::set_difference(BitMap other) {
   315   assert(size() == other.size(), "must have same size");
   316   bm_word_t* dest_map = map();
   317   bm_word_t* other_map = other.map();
   318   idx_t size = size_in_words();
   319   for (idx_t index = 0; index < size_in_words(); index++) {
   320     dest_map[index] = dest_map[index] & ~(other_map[index]);
   321   }
   322 }
   325 void BitMap::set_intersection(BitMap other) {
   326   assert(size() == other.size(), "must have same size");
   327   bm_word_t* dest_map = map();
   328   bm_word_t* other_map = other.map();
   329   idx_t size = size_in_words();
   330   for (idx_t index = 0; index < size; index++) {
   331     dest_map[index]  = dest_map[index] & other_map[index];
   332   }
   333 }
   336 void BitMap::set_intersection_at_offset(BitMap other, idx_t offset) {
   337   assert(other.size() >= offset, "offset not in range");
   338   assert(other.size() - offset >= size(), "other not large enough");
   339   // XXX Ideally, we would remove this restriction.
   340   guarantee((offset % (sizeof(bm_word_t) * BitsPerByte)) == 0,
   341             "Only handle aligned cases so far.");
   342   bm_word_t* dest_map = map();
   343   bm_word_t* other_map = other.map();
   344   idx_t offset_word_ind = word_index(offset);
   345   idx_t size = size_in_words();
   346   for (idx_t index = 0; index < size; index++) {
   347     dest_map[index] = dest_map[index] & other_map[offset_word_ind + index];
   348   }
   349 }
   351 bool BitMap::set_union_with_result(BitMap other) {
   352   assert(size() == other.size(), "must have same size");
   353   bool changed = false;
   354   bm_word_t* dest_map = map();
   355   bm_word_t* other_map = other.map();
   356   idx_t size = size_in_words();
   357   for (idx_t index = 0; index < size; index++) {
   358     idx_t temp = map(index) | other_map[index];
   359     changed = changed || (temp != map(index));
   360     map()[index] = temp;
   361   }
   362   return changed;
   363 }
   366 bool BitMap::set_difference_with_result(BitMap other) {
   367   assert(size() == other.size(), "must have same size");
   368   bool changed = false;
   369   bm_word_t* dest_map = map();
   370   bm_word_t* other_map = other.map();
   371   idx_t size = size_in_words();
   372   for (idx_t index = 0; index < size; index++) {
   373     bm_word_t temp = dest_map[index] & ~(other_map[index]);
   374     changed = changed || (temp != dest_map[index]);
   375     dest_map[index] = temp;
   376   }
   377   return changed;
   378 }
   381 bool BitMap::set_intersection_with_result(BitMap other) {
   382   assert(size() == other.size(), "must have same size");
   383   bool changed = false;
   384   bm_word_t* dest_map = map();
   385   bm_word_t* other_map = other.map();
   386   idx_t size = size_in_words();
   387   for (idx_t index = 0; index < size; index++) {
   388     bm_word_t orig = dest_map[index];
   389     bm_word_t temp = orig & other_map[index];
   390     changed = changed || (temp != orig);
   391     dest_map[index]  = temp;
   392   }
   393   return changed;
   394 }
   397 void BitMap::set_from(BitMap other) {
   398   assert(size() == other.size(), "must have same size");
   399   bm_word_t* dest_map = map();
   400   bm_word_t* other_map = other.map();
   401   idx_t size = size_in_words();
   402   for (idx_t index = 0; index < size; index++) {
   403     dest_map[index] = other_map[index];
   404   }
   405 }
   408 bool BitMap::is_same(BitMap other) {
   409   assert(size() == other.size(), "must have same size");
   410   bm_word_t* dest_map = map();
   411   bm_word_t* other_map = other.map();
   412   idx_t size = size_in_words();
   413   for (idx_t index = 0; index < size; index++) {
   414     if (dest_map[index] != other_map[index]) return false;
   415   }
   416   return true;
   417 }
   419 bool BitMap::is_full() const {
   420   bm_word_t* word = map();
   421   idx_t rest = size();
   422   for (; rest >= (idx_t) BitsPerWord; rest -= BitsPerWord) {
   423     if (*word != (bm_word_t) AllBits) return false;
   424     word++;
   425   }
   426   return rest == 0 || (*word | ~right_n_bits((int)rest)) == (bm_word_t) AllBits;
   427 }
   430 bool BitMap::is_empty() const {
   431   bm_word_t* word = map();
   432   idx_t rest = size();
   433   for (; rest >= (idx_t) BitsPerWord; rest -= BitsPerWord) {
   434     if (*word != (bm_word_t) NoBits) return false;
   435     word++;
   436   }
   437   return rest == 0 || (*word & right_n_bits((int)rest)) == (bm_word_t) NoBits;
   438 }
   440 void BitMap::clear_large() {
   441   clear_large_range_of_words(0, size_in_words());
   442 }
   444 // Note that if the closure itself modifies the bitmap
   445 // then modifications in and to the left of the _bit_ being
   446 // currently sampled will not be seen. Note also that the
   447 // interval [leftOffset, rightOffset) is right open.
   448 bool BitMap::iterate(BitMapClosure* blk, idx_t leftOffset, idx_t rightOffset) {
   449   verify_range(leftOffset, rightOffset);
   451   idx_t startIndex = word_index(leftOffset);
   452   idx_t endIndex   = MIN2(word_index(rightOffset) + 1, size_in_words());
   453   for (idx_t index = startIndex, offset = leftOffset;
   454        offset < rightOffset && index < endIndex;
   455        offset = (++index) << LogBitsPerWord) {
   456     idx_t rest = map(index) >> (offset & (BitsPerWord - 1));
   457     for (; offset < rightOffset && rest != (bm_word_t)NoBits; offset++) {
   458       if (rest & 1) {
   459         if (!blk->do_bit(offset)) return false;
   460         //  resample at each closure application
   461         // (see, for instance, CMS bug 4525989)
   462         rest = map(index) >> (offset & (BitsPerWord -1));
   463       }
   464       rest = rest >> 1;
   465     }
   466   }
   467   return true;
   468 }
   470 BitMap::idx_t* BitMap::_pop_count_table = NULL;
   472 void BitMap::init_pop_count_table() {
   473   if (_pop_count_table == NULL) {
   474     BitMap::idx_t *table = NEW_C_HEAP_ARRAY(idx_t, 256, mtInternal);
   475     for (uint i = 0; i < 256; i++) {
   476       table[i] = num_set_bits(i);
   477     }
   479     intptr_t res = Atomic::cmpxchg_ptr((intptr_t)  table,
   480                                        (intptr_t*) &_pop_count_table,
   481                                        (intptr_t)  NULL_WORD);
   482     if (res != NULL_WORD) {
   483       guarantee( _pop_count_table == (void*) res, "invariant" );
   484       FREE_C_HEAP_ARRAY(bm_word_t, table, mtInternal);
   485     }
   486   }
   487 }
   489 BitMap::idx_t BitMap::num_set_bits(bm_word_t w) {
   490   idx_t bits = 0;
   492   while (w != 0) {
   493     while ((w & 1) == 0) {
   494       w >>= 1;
   495     }
   496     bits++;
   497     w >>= 1;
   498   }
   499   return bits;
   500 }
   502 BitMap::idx_t BitMap::num_set_bits_from_table(unsigned char c) {
   503   assert(_pop_count_table != NULL, "precondition");
   504   return _pop_count_table[c];
   505 }
   507 BitMap::idx_t BitMap::count_one_bits() const {
   508   init_pop_count_table(); // If necessary.
   509   idx_t sum = 0;
   510   typedef unsigned char uchar;
   511   for (idx_t i = 0; i < size_in_words(); i++) {
   512     bm_word_t w = map()[i];
   513     for (size_t j = 0; j < sizeof(bm_word_t); j++) {
   514       sum += num_set_bits_from_table(uchar(w & 255));
   515       w >>= 8;
   516     }
   517   }
   518   return sum;
   519 }
   521 void BitMap::print_on_error(outputStream* st, const char* prefix) const {
   522   st->print_cr("%s[" PTR_FORMAT ", " PTR_FORMAT ")",
   523       prefix, map(), (char*)map() + (size() >> LogBitsPerByte));
   524 }
   526 #ifndef PRODUCT
   528 void BitMap::print_on(outputStream* st) const {
   529   tty->print("Bitmap(%d):", size());
   530   for (idx_t index = 0; index < size(); index++) {
   531     tty->print("%c", at(index) ? '1' : '0');
   532   }
   533   tty->cr();
   534 }
   536 #endif
   539 BitMap2D::BitMap2D(bm_word_t* map, idx_t size_in_slots, idx_t bits_per_slot)
   540   : _bits_per_slot(bits_per_slot)
   541   , _map(map, size_in_slots * bits_per_slot)
   542 {
   543 }
   546 BitMap2D::BitMap2D(idx_t size_in_slots, idx_t bits_per_slot)
   547   : _bits_per_slot(bits_per_slot)
   548   , _map(size_in_slots * bits_per_slot)
   549 {
   550 }

mercurial