1 /*
  2  * Copyright (c) 2005, 2020, 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  */
 24 
 25 #ifndef SHARE_UTILITIES_BITMAP_INLINE_HPP
 26 #define SHARE_UTILITIES_BITMAP_INLINE_HPP
 27 
 28 #include "utilities/bitMap.hpp"
 29 
 30 #include "memory/iterator.inline.hpp"
 31 #include "runtime/atomic.hpp"
 32 #include "utilities/align.hpp"
 33 #include "utilities/count_trailing_zeros.hpp"
 34 
 35 inline void BitMap::set_bit(idx_t bit) {
 36   verify_index(bit);
 37   *word_addr(bit) |= bit_mask(bit);
 38 }
 39 
 40 inline void BitMap::clear_bit(idx_t bit) {
 41   verify_index(bit);
 42   *word_addr(bit) &= ~bit_mask(bit);
 43 }
 44 
 45 inline const BitMap::bm_word_t BitMap::load_word_ordered(const volatile bm_word_t* const addr, atomic_memory_order memory_order) {
 46   if (memory_order == memory_order_relaxed || memory_order == memory_order_release) {
 47     return Atomic::load(addr);
 48   } else {
 49     assert(memory_order == memory_order_acq_rel ||
 50            memory_order == memory_order_acquire ||
 51            memory_order == memory_order_conservative,
 52            "unexpected memory ordering");
 53     return Atomic::load_acquire(addr);
 54   }
 55 }
 56 
 57 inline bool BitMap::par_at(idx_t index, atomic_memory_order memory_order) const {
 58   verify_index(index);
 59   assert(memory_order == memory_order_acquire ||
 60          memory_order == memory_order_relaxed,
 61          "unexpected memory ordering");
 62   const volatile bm_word_t* const addr = word_addr(index);
 63   return (load_word_ordered(addr, memory_order) & bit_mask(index)) != 0;
 64 }
 65 
 66 inline bool BitMap::par_set_bit(idx_t bit, atomic_memory_order memory_order) {
 67   verify_index(bit);
 68   volatile bm_word_t* const addr = word_addr(bit);
 69   const bm_word_t mask = bit_mask(bit);
 70   bm_word_t old_val = load_word_ordered(addr, memory_order);
 71 
 72   do {
 73     const bm_word_t new_val = old_val | mask;
 74     if (new_val == old_val) {
 75       return false;     // Someone else beat us to it.
 76     }
 77     const bm_word_t cur_val = Atomic::cmpxchg(addr, old_val, new_val, memory_order);
 78     if (cur_val == old_val) {
 79       return true;      // Success.
 80     }
 81     old_val = cur_val;  // The value changed, try again.
 82   } while (true);
 83 }
 84 
 85 inline bool BitMap::par_clear_bit(idx_t bit, atomic_memory_order memory_order) {
 86   verify_index(bit);
 87   volatile bm_word_t* const addr = word_addr(bit);
 88   const bm_word_t mask = ~bit_mask(bit);
 89   bm_word_t old_val = load_word_ordered(addr, memory_order);
 90 
 91   do {
 92     const bm_word_t new_val = old_val & mask;
 93     if (new_val == old_val) {
 94       return false;     // Someone else beat us to it.
 95     }
 96     const bm_word_t cur_val = Atomic::cmpxchg(addr, old_val, new_val, memory_order);
 97     if (cur_val == old_val) {
 98       return true;      // Success.
 99     }
100     old_val = cur_val;  // The value changed, try again.
101   } while (true);
102 }
103 
104 inline void BitMap::set_range(idx_t beg, idx_t end, RangeSizeHint hint) {
105   if (hint == small_range && end - beg == 1) {
106     set_bit(beg);
107   } else {
108     if (hint == large_range) {
109       set_large_range(beg, end);
110     } else {
111       set_range(beg, end);
112     }
113   }
114 }
115 
116 inline void BitMap::clear_range(idx_t beg, idx_t end, RangeSizeHint hint) {
117   if (end - beg == 1) {
118     clear_bit(beg);
119   } else {
120     if (hint == large_range) {
121       clear_large_range(beg, end);
122     } else {
123       clear_range(beg, end);
124     }
125   }
126 }
127 
128 inline void BitMap::par_set_range(idx_t beg, idx_t end, RangeSizeHint hint) {
129   if (hint == small_range && end - beg == 1) {
130     par_at_put(beg, true);
131   } else {
132     if (hint == large_range) {
133       par_at_put_large_range(beg, end, true);
134     } else {
135       par_at_put_range(beg, end, true);
136     }
137   }
138 }
139 
140 inline void BitMap::set_range_of_words(idx_t beg, idx_t end) {
141   bm_word_t* map = _map;
142   for (idx_t i = beg; i < end; ++i) map[i] = ~(bm_word_t)0;
143 }
144 
145 inline void BitMap::clear_range_of_words(bm_word_t* map, idx_t beg, idx_t end) {
146   for (idx_t i = beg; i < end; ++i) map[i] = 0;
147 }
148 
149 inline void BitMap::clear_range_of_words(idx_t beg, idx_t end) {
150   clear_range_of_words(_map, beg, end);
151 }
152 
153 inline void BitMap::clear() {
154   clear_range_of_words(0, size_in_words());
155 }
156 
157 inline void BitMap::par_clear_range(idx_t beg, idx_t end, RangeSizeHint hint) {
158   if (hint == small_range && end - beg == 1) {
159     par_at_put(beg, false);
160   } else {
161     if (hint == large_range) {
162       par_at_put_large_range(beg, end, false);
163     } else {
164       par_at_put_range(beg, end, false);
165     }
166   }
167 }
168 
169 template<BitMap::bm_word_t flip, bool aligned_right>
170 inline BitMap::idx_t BitMap::get_next_bit_impl(idx_t l_index, idx_t r_index) const {
171   STATIC_ASSERT(flip == find_ones_flip || flip == find_zeros_flip);
172   verify_range(l_index, r_index);
173   assert(!aligned_right || is_aligned(r_index, BitsPerWord), "r_index not aligned");
174 
175   // The first word often contains an interesting bit, either due to
176   // density or because of features of the calling algorithm.  So it's
177   // important to examine that first word with a minimum of fuss,
178   // minimizing setup time for later words that will be wasted if the
179   // first word is indeed interesting.
180 
181   // The benefit from aligned_right being true is relatively small.
182   // It saves an operation in the setup for the word search loop.
183   // It also eliminates the range check on the final result.
184   // However, callers often have a comparison with r_index, and
185   // inlining often allows the two comparisons to be combined; it is
186   // important when !aligned_right that return paths either return
187   // r_index or a value dominated by a comparison with r_index.
188   // aligned_right is still helpful when the caller doesn't have a
189   // range check because features of the calling algorithm guarantee
190   // an interesting bit will be present.
191 
192   if (l_index < r_index) {
193     // Get the word containing l_index, and shift out low bits.
194     idx_t index = to_words_align_down(l_index);
195     bm_word_t cword = (map(index) ^ flip) >> bit_in_word(l_index);
196     if ((cword & 1) != 0) {
197       // The first bit is similarly often interesting. When it matters
198       // (density or features of the calling algorithm make it likely
199       // the first bit is set), going straight to the next clause compares
200       // poorly with doing this check first; count_trailing_zeros can be
201       // relatively expensive, plus there is the additional range check.
202       // But when the first bit isn't set, the cost of having tested for
203       // it is relatively small compared to the rest of the search.
204       return l_index;
205     } else if (cword != 0) {
206       // Flipped and shifted first word is non-zero.
207       idx_t result = l_index + count_trailing_zeros(cword);
208       if (aligned_right || (result < r_index)) return result;
209       // Result is beyond range bound; return r_index.
210     } else {
211       // Flipped and shifted first word is zero.  Word search through
212       // aligned up r_index for a non-zero flipped word.
213       idx_t limit = aligned_right
214         ? to_words_align_down(r_index) // Miniscule savings when aligned.
215         : to_words_align_up(r_index);
216       while (++index < limit) {
217         cword = map(index) ^ flip;
218         if (cword != 0) {
219           idx_t result = bit_index(index) + count_trailing_zeros(cword);
220           if (aligned_right || (result < r_index)) return result;
221           // Result is beyond range bound; return r_index.
222           assert((index + 1) == limit, "invariant");
223           break;
224         }
225       }
226       // No bits in range; return r_index.
227     }
228   }
229   return r_index;
230 }
231 
232 inline BitMap::idx_t
233 BitMap::get_next_one_offset(idx_t l_offset, idx_t r_offset) const {
234   return get_next_bit_impl<find_ones_flip, false>(l_offset, r_offset);
235 }
236 
237 inline BitMap::idx_t
238 BitMap::get_next_zero_offset(idx_t l_offset, idx_t r_offset) const {
239   return get_next_bit_impl<find_zeros_flip, false>(l_offset, r_offset);
240 }
241 
242 inline BitMap::idx_t
243 BitMap::get_next_one_offset_aligned_right(idx_t l_offset, idx_t r_offset) const {
244   return get_next_bit_impl<find_ones_flip, true>(l_offset, r_offset);
245 }
246 
247 template <class BitMapClosureType>
248 inline bool BitMap::iterate(BitMapClosureType* cl, idx_t beg, idx_t end) {
249   for (idx_t index = beg; true; ++index) {
250     index = get_next_one_offset(index, end);
251     if (index >= end) {
252       return true;
253     } else if (!Devirtualizer::do_bit(cl, index)) {
254       return false;
255     }
256   }
257 }
258 
259 template <class BitMapClosureType>
260 inline bool BitMap::iterate(BitMapClosureType* cl) {
261   return iterate(cl, 0, size());
262 }
263 
264 // Returns a bit mask for a range of bits [beg, end) within a single word.  Each
265 // bit in the mask is 0 if the bit is in the range, 1 if not in the range.  The
266 // returned mask can be used directly to clear the range, or inverted to set the
267 // range.  Note:  end must not be 0.
268 inline BitMap::bm_word_t
269 BitMap::inverted_bit_mask_for_range(idx_t beg, idx_t end) const {
270   assert(end != 0, "does not work when end == 0");
271   assert(beg == end || to_words_align_down(beg) == to_words_align_down(end - 1),
272          "must be a single-word range");
273   bm_word_t mask = bit_mask(beg) - 1;   // low (right) bits
274   if (bit_in_word(end) != 0) {
275     mask |= ~(bit_mask(end) - 1);       // high (left) bits
276   }
277   return mask;
278 }
279 
280 inline void BitMap::set_large_range_of_words(idx_t beg, idx_t end) {
281   assert(beg <= end, "underflow");
282   memset(_map + beg, ~(unsigned char)0, (end - beg) * sizeof(bm_word_t));
283 }
284 
285 inline void BitMap::clear_large_range_of_words(idx_t beg, idx_t end) {
286   assert(beg <= end, "underflow");
287   memset(_map + beg, 0, (end - beg) * sizeof(bm_word_t));
288 }
289 
290 inline bool BitMap2D::is_valid_index(idx_t slot_index, idx_t bit_within_slot_index) {
291   verify_bit_within_slot_index(bit_within_slot_index);
292   return (bit_index(slot_index, bit_within_slot_index) < size_in_bits());
293 }
294 
295 inline bool BitMap2D::at(idx_t slot_index, idx_t bit_within_slot_index) const {
296   verify_bit_within_slot_index(bit_within_slot_index);
297   return _map.at(bit_index(slot_index, bit_within_slot_index));
298 }
299 
300 inline void BitMap2D::set_bit(idx_t slot_index, idx_t bit_within_slot_index) {
301   verify_bit_within_slot_index(bit_within_slot_index);
302   _map.set_bit(bit_index(slot_index, bit_within_slot_index));
303 }
304 
305 inline void BitMap2D::clear_bit(idx_t slot_index, idx_t bit_within_slot_index) {
306   verify_bit_within_slot_index(bit_within_slot_index);
307   _map.clear_bit(bit_index(slot_index, bit_within_slot_index));
308 }
309 
310 inline void BitMap2D::at_put(idx_t slot_index, idx_t bit_within_slot_index, bool value) {
311   verify_bit_within_slot_index(bit_within_slot_index);
312   _map.at_put(bit_index(slot_index, bit_within_slot_index), value);
313 }
314 
315 inline void BitMap2D::at_put_grow(idx_t slot_index, idx_t bit_within_slot_index, bool value) {
316   verify_bit_within_slot_index(bit_within_slot_index);
317 
318   idx_t bit = bit_index(slot_index, bit_within_slot_index);
319   if (bit >= _map.size()) {
320     _map.resize(2 * MAX2(_map.size(), bit));
321   }
322   _map.at_put(bit, value);
323 }
324 
325 #endif // SHARE_UTILITIES_BITMAP_INLINE_HPP