src/share/vm/utilities/bitMap.inline.hpp

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

author
kvn
date
Fri, 16 Aug 2013 14:11:40 -0700
changeset 5543
4b2838704fd5
parent 3454
2e966d967c5c
child 5713
17deed6716af
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

duke@435 1 /*
johnc@3454 2 * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
stefank@2314 25 #ifndef SHARE_VM_UTILITIES_BITMAP_INLINE_HPP
stefank@2314 26 #define SHARE_VM_UTILITIES_BITMAP_INLINE_HPP
stefank@2314 27
stefank@2314 28 #include "runtime/atomic.hpp"
stefank@2314 29 #include "utilities/bitMap.hpp"
stefank@2314 30
jcoomes@1244 31 #ifdef ASSERT
jcoomes@1244 32 inline void BitMap::verify_index(idx_t index) const {
jcoomes@1244 33 assert(index < _size, "BitMap index out of bounds");
jcoomes@1244 34 }
jcoomes@1244 35
jcoomes@1244 36 inline void BitMap::verify_range(idx_t beg_index, idx_t end_index) const {
jcoomes@1244 37 assert(beg_index <= end_index, "BitMap range error");
jcoomes@1244 38 // Note that [0,0) and [size,size) are both valid ranges.
jcoomes@1244 39 if (end_index != _size) verify_index(end_index);
jcoomes@1244 40 }
jcoomes@1244 41 #endif // #ifdef ASSERT
ysr@777 42
ysr@777 43 inline void BitMap::set_bit(idx_t bit) {
ysr@777 44 verify_index(bit);
ysr@777 45 *word_addr(bit) |= bit_mask(bit);
ysr@777 46 }
ysr@777 47
ysr@777 48 inline void BitMap::clear_bit(idx_t bit) {
ysr@777 49 verify_index(bit);
ysr@777 50 *word_addr(bit) &= ~bit_mask(bit);
ysr@777 51 }
ysr@777 52
duke@435 53 inline bool BitMap::par_set_bit(idx_t bit) {
duke@435 54 verify_index(bit);
duke@435 55 volatile idx_t* const addr = word_addr(bit);
duke@435 56 const idx_t mask = bit_mask(bit);
duke@435 57 idx_t old_val = *addr;
duke@435 58
duke@435 59 do {
duke@435 60 const idx_t new_val = old_val | mask;
duke@435 61 if (new_val == old_val) {
duke@435 62 return false; // Someone else beat us to it.
duke@435 63 }
duke@435 64 const idx_t cur_val = (idx_t) Atomic::cmpxchg_ptr((void*) new_val,
duke@435 65 (volatile void*) addr,
duke@435 66 (void*) old_val);
duke@435 67 if (cur_val == old_val) {
duke@435 68 return true; // Success.
duke@435 69 }
duke@435 70 old_val = cur_val; // The value changed, try again.
duke@435 71 } while (true);
duke@435 72 }
duke@435 73
duke@435 74 inline bool BitMap::par_clear_bit(idx_t bit) {
duke@435 75 verify_index(bit);
duke@435 76 volatile idx_t* const addr = word_addr(bit);
duke@435 77 const idx_t mask = ~bit_mask(bit);
duke@435 78 idx_t old_val = *addr;
duke@435 79
duke@435 80 do {
duke@435 81 const idx_t new_val = old_val & mask;
duke@435 82 if (new_val == old_val) {
duke@435 83 return false; // Someone else beat us to it.
duke@435 84 }
duke@435 85 const idx_t cur_val = (idx_t) Atomic::cmpxchg_ptr((void*) new_val,
duke@435 86 (volatile void*) addr,
duke@435 87 (void*) old_val);
duke@435 88 if (cur_val == old_val) {
duke@435 89 return true; // Success.
duke@435 90 }
duke@435 91 old_val = cur_val; // The value changed, try again.
duke@435 92 } while (true);
duke@435 93 }
duke@435 94
ysr@777 95 inline void BitMap::set_range(idx_t beg, idx_t end, RangeSizeHint hint) {
ysr@777 96 if (hint == small_range && end - beg == 1) {
ysr@777 97 set_bit(beg);
ysr@777 98 } else {
ysr@777 99 if (hint == large_range) {
ysr@777 100 set_large_range(beg, end);
ysr@777 101 } else {
ysr@777 102 set_range(beg, end);
ysr@777 103 }
ysr@777 104 }
ysr@777 105 }
ysr@777 106
ysr@777 107 inline void BitMap::clear_range(idx_t beg, idx_t end, RangeSizeHint hint) {
ysr@777 108 if (hint == small_range && end - beg == 1) {
ysr@777 109 clear_bit(beg);
ysr@777 110 } else {
ysr@777 111 if (hint == large_range) {
ysr@777 112 clear_large_range(beg, end);
ysr@777 113 } else {
ysr@777 114 clear_range(beg, end);
ysr@777 115 }
ysr@777 116 }
ysr@777 117 }
ysr@777 118
ysr@777 119 inline void BitMap::par_set_range(idx_t beg, idx_t end, RangeSizeHint hint) {
ysr@777 120 if (hint == small_range && end - beg == 1) {
ysr@777 121 par_at_put(beg, true);
ysr@777 122 } else {
ysr@777 123 if (hint == large_range) {
ysr@777 124 par_at_put_large_range(beg, end, true);
ysr@777 125 } else {
ysr@777 126 par_at_put_range(beg, end, true);
ysr@777 127 }
ysr@777 128 }
ysr@777 129 }
ysr@777 130
ysr@777 131 inline void BitMap::set_range_of_words(idx_t beg, idx_t end) {
ysr@777 132 bm_word_t* map = _map;
ysr@777 133 for (idx_t i = beg; i < end; ++i) map[i] = ~(uintptr_t)0;
ysr@777 134 }
ysr@777 135
ysr@777 136
ysr@777 137 inline void BitMap::clear_range_of_words(idx_t beg, idx_t end) {
ysr@777 138 bm_word_t* map = _map;
ysr@777 139 for (idx_t i = beg; i < end; ++i) map[i] = 0;
ysr@777 140 }
ysr@777 141
ysr@777 142
ysr@777 143 inline void BitMap::clear() {
ysr@777 144 clear_range_of_words(0, size_in_words());
ysr@777 145 }
ysr@777 146
ysr@777 147
ysr@777 148 inline void BitMap::par_clear_range(idx_t beg, idx_t end, RangeSizeHint hint) {
ysr@777 149 if (hint == small_range && end - beg == 1) {
ysr@777 150 par_at_put(beg, false);
ysr@777 151 } else {
ysr@777 152 if (hint == large_range) {
ysr@777 153 par_at_put_large_range(beg, end, false);
ysr@777 154 } else {
ysr@777 155 par_at_put_range(beg, end, false);
ysr@777 156 }
ysr@777 157 }
ysr@777 158 }
ysr@777 159
duke@435 160 inline BitMap::idx_t
ysr@777 161 BitMap::get_next_one_offset_inline(idx_t l_offset, idx_t r_offset) const {
ysr@777 162 assert(l_offset <= size(), "BitMap index out of bounds");
ysr@777 163 assert(r_offset <= size(), "BitMap index out of bounds");
ysr@777 164 assert(l_offset <= r_offset, "l_offset > r_offset ?");
duke@435 165
ysr@777 166 if (l_offset == r_offset) {
ysr@777 167 return l_offset;
duke@435 168 }
ysr@777 169 idx_t index = word_index(l_offset);
ysr@777 170 idx_t r_index = word_index(r_offset-1) + 1;
ysr@777 171 idx_t res_offset = l_offset;
duke@435 172
duke@435 173 // check bits including and to the _left_ of offset's position
ysr@777 174 idx_t pos = bit_in_word(res_offset);
ysr@777 175 idx_t res = map(index) >> pos;
ysr@777 176 if (res != (uintptr_t)NoBits) {
duke@435 177 // find the position of the 1-bit
ysr@777 178 for (; !(res & 1); res_offset++) {
duke@435 179 res = res >> 1;
duke@435 180 }
johnc@3454 181
johnc@3454 182 #ifdef ASSERT
johnc@3454 183 // In the following assert, if r_offset is not bitamp word aligned,
johnc@3454 184 // checking that res_offset is strictly less than r_offset is too
johnc@3454 185 // strong and will trip the assert.
johnc@3454 186 //
johnc@3454 187 // Consider the case where l_offset is bit 15 and r_offset is bit 17
johnc@3454 188 // of the same map word, and where bits [15:16:17:18] == [00:00:00:01].
johnc@3454 189 // All the bits in the range [l_offset:r_offset) are 0.
johnc@3454 190 // The loop that calculates res_offset, above, would yield the offset
johnc@3454 191 // of bit 18 because it's in the same map word as l_offset and there
johnc@3454 192 // is a set bit in that map word above l_offset (i.e. res != NoBits).
johnc@3454 193 //
johnc@3454 194 // In this case, however, we can assert is that res_offset is strictly
johnc@3454 195 // less than size() since we know that there is at least one set bit
johnc@3454 196 // at an offset above, but in the same map word as, r_offset.
johnc@3454 197 // Otherwise, if r_offset is word aligned then it will not be in the
johnc@3454 198 // same map word as l_offset (unless it equals l_offset). So either
johnc@3454 199 // there won't be a set bit between l_offset and the end of it's map
johnc@3454 200 // word (i.e. res == NoBits), or res_offset will be less than r_offset.
johnc@3454 201
johnc@3454 202 idx_t limit = is_word_aligned(r_offset) ? r_offset : size();
johnc@3454 203 assert(res_offset >= l_offset && res_offset < limit, "just checking");
johnc@3454 204 #endif // ASSERT
ysr@777 205 return MIN2(res_offset, r_offset);
duke@435 206 }
duke@435 207 // skip over all word length 0-bit runs
duke@435 208 for (index++; index < r_index; index++) {
duke@435 209 res = map(index);
ysr@777 210 if (res != (uintptr_t)NoBits) {
duke@435 211 // found a 1, return the offset
ysr@777 212 for (res_offset = bit_index(index); !(res & 1); res_offset++) {
duke@435 213 res = res >> 1;
duke@435 214 }
duke@435 215 assert(res & 1, "tautology; see loop condition");
ysr@777 216 assert(res_offset >= l_offset, "just checking");
ysr@777 217 return MIN2(res_offset, r_offset);
duke@435 218 }
duke@435 219 }
ysr@777 220 return r_offset;
duke@435 221 }
ysr@777 222
ysr@777 223 inline BitMap::idx_t
ysr@777 224 BitMap::get_next_zero_offset_inline(idx_t l_offset, idx_t r_offset) const {
ysr@777 225 assert(l_offset <= size(), "BitMap index out of bounds");
ysr@777 226 assert(r_offset <= size(), "BitMap index out of bounds");
ysr@777 227 assert(l_offset <= r_offset, "l_offset > r_offset ?");
ysr@777 228
ysr@777 229 if (l_offset == r_offset) {
ysr@777 230 return l_offset;
ysr@777 231 }
ysr@777 232 idx_t index = word_index(l_offset);
ysr@777 233 idx_t r_index = word_index(r_offset-1) + 1;
ysr@777 234 idx_t res_offset = l_offset;
ysr@777 235
ysr@777 236 // check bits including and to the _left_ of offset's position
ysr@777 237 idx_t pos = res_offset & (BitsPerWord - 1);
ysr@777 238 idx_t res = (map(index) >> pos) | left_n_bits((int)pos);
ysr@777 239
ysr@777 240 if (res != (uintptr_t)AllBits) {
ysr@777 241 // find the position of the 0-bit
ysr@777 242 for (; res & 1; res_offset++) {
ysr@777 243 res = res >> 1;
ysr@777 244 }
ysr@777 245 assert(res_offset >= l_offset, "just checking");
ysr@777 246 return MIN2(res_offset, r_offset);
ysr@777 247 }
ysr@777 248 // skip over all word length 1-bit runs
ysr@777 249 for (index++; index < r_index; index++) {
ysr@777 250 res = map(index);
ysr@777 251 if (res != (uintptr_t)AllBits) {
ysr@777 252 // found a 0, return the offset
ysr@777 253 for (res_offset = index << LogBitsPerWord; res & 1;
ysr@777 254 res_offset++) {
ysr@777 255 res = res >> 1;
ysr@777 256 }
ysr@777 257 assert(!(res & 1), "tautology; see loop condition");
ysr@777 258 assert(res_offset >= l_offset, "just checking");
ysr@777 259 return MIN2(res_offset, r_offset);
ysr@777 260 }
ysr@777 261 }
ysr@777 262 return r_offset;
ysr@777 263 }
ysr@777 264
ysr@777 265 inline BitMap::idx_t
ysr@777 266 BitMap::get_next_one_offset_inline_aligned_right(idx_t l_offset,
ysr@777 267 idx_t r_offset) const
ysr@777 268 {
ysr@777 269 verify_range(l_offset, r_offset);
ysr@777 270 assert(bit_in_word(r_offset) == 0, "r_offset not word-aligned");
ysr@777 271
ysr@777 272 if (l_offset == r_offset) {
ysr@777 273 return l_offset;
ysr@777 274 }
ysr@777 275 idx_t index = word_index(l_offset);
ysr@777 276 idx_t r_index = word_index(r_offset);
ysr@777 277 idx_t res_offset = l_offset;
ysr@777 278
ysr@777 279 // check bits including and to the _left_ of offset's position
ysr@777 280 idx_t res = map(index) >> bit_in_word(res_offset);
ysr@777 281 if (res != (uintptr_t)NoBits) {
ysr@777 282 // find the position of the 1-bit
ysr@777 283 for (; !(res & 1); res_offset++) {
ysr@777 284 res = res >> 1;
ysr@777 285 }
ysr@777 286 assert(res_offset >= l_offset &&
ysr@777 287 res_offset < r_offset, "just checking");
ysr@777 288 return res_offset;
ysr@777 289 }
ysr@777 290 // skip over all word length 0-bit runs
ysr@777 291 for (index++; index < r_index; index++) {
ysr@777 292 res = map(index);
ysr@777 293 if (res != (uintptr_t)NoBits) {
ysr@777 294 // found a 1, return the offset
ysr@777 295 for (res_offset = bit_index(index); !(res & 1); res_offset++) {
ysr@777 296 res = res >> 1;
ysr@777 297 }
ysr@777 298 assert(res & 1, "tautology; see loop condition");
ysr@777 299 assert(res_offset >= l_offset && res_offset < r_offset, "just checking");
ysr@777 300 return res_offset;
ysr@777 301 }
ysr@777 302 }
ysr@777 303 return r_offset;
ysr@777 304 }
ysr@777 305
ysr@777 306
ysr@777 307 // Returns a bit mask for a range of bits [beg, end) within a single word. Each
ysr@777 308 // bit in the mask is 0 if the bit is in the range, 1 if not in the range. The
ysr@777 309 // returned mask can be used directly to clear the range, or inverted to set the
ysr@777 310 // range. Note: end must not be 0.
ysr@777 311 inline BitMap::bm_word_t
ysr@777 312 BitMap::inverted_bit_mask_for_range(idx_t beg, idx_t end) const {
ysr@777 313 assert(end != 0, "does not work when end == 0");
ysr@777 314 assert(beg == end || word_index(beg) == word_index(end - 1),
ysr@777 315 "must be a single-word range");
ysr@777 316 bm_word_t mask = bit_mask(beg) - 1; // low (right) bits
ysr@777 317 if (bit_in_word(end) != 0) {
ysr@777 318 mask |= ~(bit_mask(end) - 1); // high (left) bits
ysr@777 319 }
ysr@777 320 return mask;
ysr@777 321 }
ysr@777 322
ysr@777 323 inline void BitMap::set_large_range_of_words(idx_t beg, idx_t end) {
ysr@777 324 memset(_map + beg, ~(unsigned char)0, (end - beg) * sizeof(uintptr_t));
ysr@777 325 }
ysr@777 326
ysr@777 327 inline void BitMap::clear_large_range_of_words(idx_t beg, idx_t end) {
ysr@777 328 memset(_map + beg, 0, (end - beg) * sizeof(uintptr_t));
ysr@777 329 }
ysr@777 330
ysr@777 331 inline BitMap::idx_t BitMap::word_index_round_up(idx_t bit) const {
ysr@777 332 idx_t bit_rounded_up = bit + (BitsPerWord - 1);
ysr@777 333 // Check for integer arithmetic overflow.
ysr@777 334 return bit_rounded_up > bit ? word_index(bit_rounded_up) : size_in_words();
ysr@777 335 }
ysr@777 336
ysr@777 337 inline BitMap::idx_t BitMap::get_next_one_offset(idx_t l_offset,
ysr@777 338 idx_t r_offset) const {
ysr@777 339 return get_next_one_offset_inline(l_offset, r_offset);
ysr@777 340 }
ysr@777 341
ysr@777 342 inline BitMap::idx_t BitMap::get_next_zero_offset(idx_t l_offset,
ysr@777 343 idx_t r_offset) const {
ysr@777 344 return get_next_zero_offset_inline(l_offset, r_offset);
ysr@777 345 }
ysr@777 346
ysr@777 347 inline void BitMap2D::clear() {
ysr@777 348 _map.clear();
ysr@777 349 }
stefank@2314 350
stefank@2314 351 #endif // SHARE_VM_UTILITIES_BITMAP_INLINE_HPP

mercurial