Mercurial > hg > graal-compiler
comparison src/share/vm/utilities/bitMap.hpp @ 360:5d254928c888
Merge
author | ysr |
---|---|
date | Wed, 27 Aug 2008 11:20:46 -0700 |
parents | 37f87013dfd8 |
children | 6e2afda171db |
comparison
equal
deleted
inserted
replaced
341:d60e4e6d7f72 | 360:5d254928c888 |
---|---|
20 * CA 95054 USA or visit www.sun.com if you need additional information or | 20 * CA 95054 USA or visit www.sun.com if you need additional information or |
21 * have any questions. | 21 * have any questions. |
22 * | 22 * |
23 */ | 23 */ |
24 | 24 |
25 // Closure for iterating over BitMaps | 25 // Forward decl; |
26 | 26 class BitMapClosure; |
27 class BitMapClosure VALUE_OBJ_CLASS_SPEC { | 27 |
28 public: | 28 // Operations for bitmaps represented as arrays of unsigned integers. |
29 // Callback when bit in map is set | 29 // Bit offsets are numbered from 0 to size-1. |
30 virtual void do_bit(size_t offset) = 0; | |
31 }; | |
32 | |
33 | |
34 // Operations for bitmaps represented as arrays of unsigned 32- or 64-bit | |
35 // integers (uintptr_t). | |
36 // | |
37 // Bit offsets are numbered from 0 to size-1 | |
38 | 30 |
39 class BitMap VALUE_OBJ_CLASS_SPEC { | 31 class BitMap VALUE_OBJ_CLASS_SPEC { |
40 friend class BitMap2D; | 32 friend class BitMap2D; |
41 | 33 |
42 public: | 34 public: |
43 typedef size_t idx_t; // Type used for bit and word indices. | 35 typedef size_t idx_t; // Type used for bit and word indices. |
36 typedef uintptr_t bm_word_t; // Element type of array that represents | |
37 // the bitmap. | |
44 | 38 |
45 // Hints for range sizes. | 39 // Hints for range sizes. |
46 typedef enum { | 40 typedef enum { |
47 unknown_range, small_range, large_range | 41 unknown_range, small_range, large_range |
48 } RangeSizeHint; | 42 } RangeSizeHint; |
49 | 43 |
50 private: | 44 private: |
51 idx_t* _map; // First word in bitmap | 45 bm_word_t* _map; // First word in bitmap |
52 idx_t _size; // Size of bitmap (in bits) | 46 idx_t _size; // Size of bitmap (in bits) |
53 | 47 |
54 // Puts the given value at the given offset, using resize() to size | 48 // Puts the given value at the given offset, using resize() to size |
55 // the bitmap appropriately if needed using factor-of-two expansion. | 49 // the bitmap appropriately if needed using factor-of-two expansion. |
56 void at_put_grow(idx_t index, bool value); | 50 void at_put_grow(idx_t index, bool value); |
57 | 51 |
60 // bitmap words are 32 bits, return a number 0 <= n <= 31). | 54 // bitmap words are 32 bits, return a number 0 <= n <= 31). |
61 static idx_t bit_in_word(idx_t bit) { return bit & (BitsPerWord - 1); } | 55 static idx_t bit_in_word(idx_t bit) { return bit & (BitsPerWord - 1); } |
62 | 56 |
63 // Return a mask that will select the specified bit, when applied to the word | 57 // Return a mask that will select the specified bit, when applied to the word |
64 // containing the bit. | 58 // containing the bit. |
65 static idx_t bit_mask(idx_t bit) { return (idx_t)1 << bit_in_word(bit); } | 59 static bm_word_t bit_mask(idx_t bit) { return (bm_word_t)1 << bit_in_word(bit); } |
66 | 60 |
67 // Return the index of the word containing the specified bit. | 61 // Return the index of the word containing the specified bit. |
68 static idx_t word_index(idx_t bit) { return bit >> LogBitsPerWord; } | 62 static idx_t word_index(idx_t bit) { return bit >> LogBitsPerWord; } |
69 | 63 |
70 // Return the bit number of the first bit in the specified word. | 64 // Return the bit number of the first bit in the specified word. |
71 static idx_t bit_index(idx_t word) { return word << LogBitsPerWord; } | 65 static idx_t bit_index(idx_t word) { return word << LogBitsPerWord; } |
72 | 66 |
73 // Return the array of bitmap words, or a specific word from it. | 67 // Return the array of bitmap words, or a specific word from it. |
74 idx_t* map() const { return _map; } | 68 bm_word_t* map() const { return _map; } |
75 idx_t map(idx_t word) const { return _map[word]; } | 69 bm_word_t map(idx_t word) const { return _map[word]; } |
76 | 70 |
77 // Return a pointer to the word containing the specified bit. | 71 // Return a pointer to the word containing the specified bit. |
78 idx_t* word_addr(idx_t bit) const { return map() + word_index(bit); } | 72 bm_word_t* word_addr(idx_t bit) const { return map() + word_index(bit); } |
79 | 73 |
80 // Set a word to a specified value or to all ones; clear a word. | 74 // Set a word to a specified value or to all ones; clear a word. |
81 void set_word (idx_t word, idx_t val) { _map[word] = val; } | 75 void set_word (idx_t word, bm_word_t val) { _map[word] = val; } |
82 void set_word (idx_t word) { set_word(word, ~(uintptr_t)0); } | 76 void set_word (idx_t word) { set_word(word, ~(uintptr_t)0); } |
83 void clear_word(idx_t word) { _map[word] = 0; } | 77 void clear_word(idx_t word) { _map[word] = 0; } |
84 | 78 |
85 // Utilities for ranges of bits. Ranges are half-open [beg, end). | 79 // Utilities for ranges of bits. Ranges are half-open [beg, end). |
86 | 80 |
87 // Ranges within a single word. | 81 // Ranges within a single word. |
88 inline idx_t inverted_bit_mask_for_range(idx_t beg, idx_t end) const; | 82 bm_word_t inverted_bit_mask_for_range(idx_t beg, idx_t end) const; |
89 inline void set_range_within_word (idx_t beg, idx_t end); | 83 void set_range_within_word (idx_t beg, idx_t end); |
90 inline void clear_range_within_word (idx_t beg, idx_t end); | 84 void clear_range_within_word (idx_t beg, idx_t end); |
91 inline void par_put_range_within_word (idx_t beg, idx_t end, bool value); | 85 void par_put_range_within_word (idx_t beg, idx_t end, bool value); |
92 | 86 |
93 // Ranges spanning entire words. | 87 // Ranges spanning entire words. |
94 inline void set_range_of_words (idx_t beg, idx_t end); | 88 void set_range_of_words (idx_t beg, idx_t end); |
95 inline void clear_range_of_words (idx_t beg, idx_t end); | 89 void clear_range_of_words (idx_t beg, idx_t end); |
96 inline void set_large_range_of_words (idx_t beg, idx_t end); | 90 void set_large_range_of_words (idx_t beg, idx_t end); |
97 inline void clear_large_range_of_words (idx_t beg, idx_t end); | 91 void clear_large_range_of_words (idx_t beg, idx_t end); |
98 | 92 |
99 // The index of the first full word in a range. | 93 // The index of the first full word in a range. |
100 inline idx_t word_index_round_up(idx_t bit) const; | 94 idx_t word_index_round_up(idx_t bit) const; |
101 | 95 |
102 // Verification, statistics. | 96 // Verification, statistics. |
103 void verify_index(idx_t index) const { | 97 void verify_index(idx_t index) const; |
104 assert(index < _size, "BitMap index out of bounds"); | 98 void verify_range(idx_t beg_index, idx_t end_index) const; |
105 } | 99 |
106 | 100 static idx_t* _pop_count_table; |
107 void verify_range(idx_t beg_index, idx_t end_index) const { | 101 static void init_pop_count_table(); |
108 #ifdef ASSERT | 102 static idx_t num_set_bits(bm_word_t w); |
109 assert(beg_index <= end_index, "BitMap range error"); | 103 static idx_t num_set_bits_from_table(unsigned char c); |
110 // Note that [0,0) and [size,size) are both valid ranges. | |
111 if (end_index != _size) verify_index(end_index); | |
112 #endif | |
113 } | |
114 | 104 |
115 public: | 105 public: |
116 | 106 |
117 // Constructs a bitmap with no map, and size 0. | 107 // Constructs a bitmap with no map, and size 0. |
118 BitMap() : _map(NULL), _size(0) {} | 108 BitMap() : _map(NULL), _size(0) {} |
119 | 109 |
120 // Construction | 110 // Constructs a bitmap with the given map and size. |
121 BitMap(idx_t* map, idx_t size_in_bits); | 111 BitMap(bm_word_t* map, idx_t size_in_bits); |
122 | 112 |
123 // Allocates necessary data structure in resource area | 113 // Constructs an empty bitmap of the given size (that is, this clears the |
124 BitMap(idx_t size_in_bits); | 114 // new bitmap). Allocates the map array in resource area if |
125 | 115 // "in_resource_area" is true, else in the C heap. |
126 void set_map(idx_t* map) { _map = map; } | 116 BitMap(idx_t size_in_bits, bool in_resource_area = true); |
117 | |
118 // Set the map and size. | |
119 void set_map(bm_word_t* map) { _map = map; } | |
127 void set_size(idx_t size_in_bits) { _size = size_in_bits; } | 120 void set_size(idx_t size_in_bits) { _size = size_in_bits; } |
128 | 121 |
129 // Allocates necessary data structure in resource area. | 122 // Allocates necessary data structure, either in the resource area |
123 // or in the C heap, as indicated by "in_resource_area." | |
130 // Preserves state currently in bit map by copying data. | 124 // Preserves state currently in bit map by copying data. |
131 // Zeros any newly-addressable bits. | 125 // Zeros any newly-addressable bits. |
132 // Does not perform any frees (i.e., of current _map). | 126 // If "in_resource_area" is false, frees the current map. |
133 void resize(idx_t size_in_bits); | 127 // (Note that this assumes that all calls to "resize" on the same BitMap |
128 // use the same value for "in_resource_area".) | |
129 void resize(idx_t size_in_bits, bool in_resource_area = true); | |
134 | 130 |
135 // Accessing | 131 // Accessing |
136 idx_t size() const { return _size; } | 132 idx_t size() const { return _size; } |
137 idx_t size_in_words() const { | 133 idx_t size_in_words() const { |
138 return word_index(size() + BitsPerWord - 1); | 134 return word_index(size() + BitsPerWord - 1); |
155 return word_align_up(bit) == bit; | 151 return word_align_up(bit) == bit; |
156 } | 152 } |
157 | 153 |
158 // Set or clear the specified bit. | 154 // Set or clear the specified bit. |
159 inline void set_bit(idx_t bit); | 155 inline void set_bit(idx_t bit); |
160 inline void clear_bit(idx_t bit); | 156 void clear_bit(idx_t bit); |
161 | 157 |
162 // Atomically set or clear the specified bit. | 158 // Atomically set or clear the specified bit. |
163 inline bool par_set_bit(idx_t bit); | 159 bool par_set_bit(idx_t bit); |
164 inline bool par_clear_bit(idx_t bit); | 160 bool par_clear_bit(idx_t bit); |
165 | 161 |
166 // Put the given value at the given offset. The parallel version | 162 // Put the given value at the given offset. The parallel version |
167 // will CAS the value into the bitmap and is quite a bit slower. | 163 // will CAS the value into the bitmap and is quite a bit slower. |
168 // The parallel version also returns a value indicating if the | 164 // The parallel version also returns a value indicating if the |
169 // calling thread was the one that changed the value of the bit. | 165 // calling thread was the one that changed the value of the bit. |
181 void par_at_put_large_range(idx_t beg, idx_t end, bool value); | 177 void par_at_put_large_range(idx_t beg, idx_t end, bool value); |
182 | 178 |
183 // Update a range of bits, using a hint about the size. Currently only | 179 // Update a range of bits, using a hint about the size. Currently only |
184 // inlines the predominant case of a 1-bit range. Works best when hint is a | 180 // inlines the predominant case of a 1-bit range. Works best when hint is a |
185 // compile-time constant. | 181 // compile-time constant. |
186 inline void set_range(idx_t beg, idx_t end, RangeSizeHint hint); | 182 void set_range(idx_t beg, idx_t end, RangeSizeHint hint); |
187 inline void clear_range(idx_t beg, idx_t end, RangeSizeHint hint); | 183 void clear_range(idx_t beg, idx_t end, RangeSizeHint hint); |
188 inline void par_set_range(idx_t beg, idx_t end, RangeSizeHint hint); | 184 void par_set_range(idx_t beg, idx_t end, RangeSizeHint hint); |
189 inline void par_clear_range (idx_t beg, idx_t end, RangeSizeHint hint); | 185 void par_clear_range (idx_t beg, idx_t end, RangeSizeHint hint); |
186 | |
187 // It performs the union operation between subsets of equal length | |
188 // of two bitmaps (the target bitmap of the method and the | |
189 // from_bitmap) and stores the result to the target bitmap. The | |
190 // from_start_index represents the first bit index of the subrange | |
191 // of the from_bitmap. The to_start_index is the equivalent of the | |
192 // target bitmap. Both indexes should be word-aligned, i.e. they | |
193 // should correspond to the first bit on a bitmap word (it's up to | |
194 // the caller to ensure this; the method does check it). The length | |
195 // of the subset is specified with word_num and it is in number of | |
196 // bitmap words. The caller should ensure that this is at least 2 | |
197 // (smaller ranges are not support to save extra checks). Again, | |
198 // this is checked in the method. | |
199 // | |
200 // Atomicity concerns: it is assumed that any contention on the | |
201 // target bitmap with other threads will happen on the first and | |
202 // last words; the ones in between will be "owned" exclusively by | |
203 // the calling thread and, in fact, they will already be 0. So, the | |
204 // method performs a CAS on the first word, copies the next | |
205 // word_num-2 words, and finally performs a CAS on the last word. | |
206 void mostly_disjoint_range_union(BitMap* from_bitmap, | |
207 idx_t from_start_index, | |
208 idx_t to_start_index, | |
209 size_t word_num); | |
210 | |
190 | 211 |
191 // Clearing | 212 // Clearing |
192 void clear(); | |
193 void clear_large(); | 213 void clear_large(); |
194 | 214 inline void clear(); |
195 // Iteration support | 215 |
196 void iterate(BitMapClosure* blk, idx_t leftIndex, idx_t rightIndex); | 216 // Iteration support. Returns "true" if the iteration completed, false |
197 inline void iterate(BitMapClosure* blk) { | 217 // if the iteration terminated early (because the closure "blk" returned |
218 // false). | |
219 bool iterate(BitMapClosure* blk, idx_t leftIndex, idx_t rightIndex); | |
220 bool iterate(BitMapClosure* blk) { | |
198 // call the version that takes an interval | 221 // call the version that takes an interval |
199 iterate(blk, 0, size()); | 222 return iterate(blk, 0, size()); |
200 } | 223 } |
201 | 224 |
202 // Looking for 1's and 0's to the "right" | 225 // Looking for 1's and 0's at indices equal to or greater than "l_index", |
226 // stopping if none has been found before "r_index", and returning | |
227 // "r_index" (which must be at most "size") in that case. | |
228 idx_t get_next_one_offset_inline (idx_t l_index, idx_t r_index) const; | |
229 idx_t get_next_zero_offset_inline(idx_t l_index, idx_t r_index) const; | |
230 | |
231 // Like "get_next_one_offset_inline", except requires that "r_index" is | |
232 // aligned to bitsizeof(bm_word_t). | |
233 idx_t get_next_one_offset_inline_aligned_right(idx_t l_index, | |
234 idx_t r_index) const; | |
235 | |
236 // Non-inline versionsof the above. | |
203 idx_t get_next_one_offset (idx_t l_index, idx_t r_index) const; | 237 idx_t get_next_one_offset (idx_t l_index, idx_t r_index) const; |
204 idx_t get_next_zero_offset(idx_t l_index, idx_t r_index) const; | 238 idx_t get_next_zero_offset(idx_t l_index, idx_t r_index) const; |
205 | 239 |
206 idx_t get_next_one_offset(idx_t offset) const { | 240 idx_t get_next_one_offset(idx_t offset) const { |
207 return get_next_one_offset(offset, size()); | 241 return get_next_one_offset(offset, size()); |
208 } | 242 } |
209 idx_t get_next_zero_offset(idx_t offset) const { | 243 idx_t get_next_zero_offset(idx_t offset) const { |
210 return get_next_zero_offset(offset, size()); | 244 return get_next_zero_offset(offset, size()); |
211 } | 245 } |
212 | 246 |
213 | 247 // Returns the number of bits set in the bitmap. |
214 | 248 idx_t count_one_bits() const; |
215 // Find the next one bit in the range [beg_bit, end_bit), or return end_bit if | |
216 // no one bit is found. Equivalent to get_next_one_offset(), but inline for | |
217 // use in performance-critical code. | |
218 inline idx_t find_next_one_bit(idx_t beg_bit, idx_t end_bit) const; | |
219 | 249 |
220 // Set operations. | 250 // Set operations. |
221 void set_union(BitMap bits); | 251 void set_union(BitMap bits); |
222 void set_difference(BitMap bits); | 252 void set_difference(BitMap bits); |
223 void set_intersection(BitMap bits); | 253 void set_intersection(BitMap bits); |
230 // during the operation | 260 // during the operation |
231 bool set_union_with_result(BitMap bits); | 261 bool set_union_with_result(BitMap bits); |
232 bool set_difference_with_result(BitMap bits); | 262 bool set_difference_with_result(BitMap bits); |
233 bool set_intersection_with_result(BitMap bits); | 263 bool set_intersection_with_result(BitMap bits); |
234 | 264 |
265 // Requires the submap of "bits" starting at offset to be at least as | |
266 // large as "this". Modifies "this" to be the intersection of its | |
267 // current contents and the submap of "bits" starting at "offset" of the | |
268 // same length as "this." | |
269 // (For expedience, currently requires the offset to be aligned to the | |
270 // bitsize of a uintptr_t. This should go away in the future though it | |
271 // will probably remain a good case to optimize.) | |
272 void set_intersection_at_offset(BitMap bits, idx_t offset); | |
273 | |
235 void set_from(BitMap bits); | 274 void set_from(BitMap bits); |
236 | 275 |
237 bool is_same(BitMap bits); | 276 bool is_same(BitMap bits); |
238 | 277 |
239 // Test if all bits are set or cleared | 278 // Test if all bits are set or cleared |
246 // Printing | 285 // Printing |
247 void print_on(outputStream* st) const; | 286 void print_on(outputStream* st) const; |
248 #endif | 287 #endif |
249 }; | 288 }; |
250 | 289 |
251 inline void BitMap::set_bit(idx_t bit) { | |
252 verify_index(bit); | |
253 *word_addr(bit) |= bit_mask(bit); | |
254 } | |
255 | |
256 inline void BitMap::clear_bit(idx_t bit) { | |
257 verify_index(bit); | |
258 *word_addr(bit) &= ~bit_mask(bit); | |
259 } | |
260 | |
261 inline void BitMap::set_range(idx_t beg, idx_t end, RangeSizeHint hint) { | |
262 if (hint == small_range && end - beg == 1) { | |
263 set_bit(beg); | |
264 } else { | |
265 if (hint == large_range) { | |
266 set_large_range(beg, end); | |
267 } else { | |
268 set_range(beg, end); | |
269 } | |
270 } | |
271 } | |
272 | |
273 inline void BitMap::clear_range(idx_t beg, idx_t end, RangeSizeHint hint) { | |
274 if (hint == small_range && end - beg == 1) { | |
275 clear_bit(beg); | |
276 } else { | |
277 if (hint == large_range) { | |
278 clear_large_range(beg, end); | |
279 } else { | |
280 clear_range(beg, end); | |
281 } | |
282 } | |
283 } | |
284 | |
285 inline void BitMap::par_set_range(idx_t beg, idx_t end, RangeSizeHint hint) { | |
286 if (hint == small_range && end - beg == 1) { | |
287 par_at_put(beg, true); | |
288 } else { | |
289 if (hint == large_range) { | |
290 par_at_put_large_range(beg, end, true); | |
291 } else { | |
292 par_at_put_range(beg, end, true); | |
293 } | |
294 } | |
295 } | |
296 | |
297 | 290 |
298 // Convenience class wrapping BitMap which provides multiple bits per slot. | 291 // Convenience class wrapping BitMap which provides multiple bits per slot. |
299 class BitMap2D VALUE_OBJ_CLASS_SPEC { | 292 class BitMap2D VALUE_OBJ_CLASS_SPEC { |
300 public: | 293 public: |
301 typedef size_t idx_t; // Type used for bit and word indices. | 294 typedef BitMap::idx_t idx_t; // Type used for bit and word indices. |
302 | 295 typedef BitMap::bm_word_t bm_word_t; // Element type of array that |
296 // represents the bitmap. | |
303 private: | 297 private: |
304 BitMap _map; | 298 BitMap _map; |
305 idx_t _bits_per_slot; | 299 idx_t _bits_per_slot; |
306 | 300 |
307 idx_t bit_index(idx_t slot_index, idx_t bit_within_slot_index) const { | 301 idx_t bit_index(idx_t slot_index, idx_t bit_within_slot_index) const { |
312 assert(index < _bits_per_slot, "bit_within_slot index out of bounds"); | 306 assert(index < _bits_per_slot, "bit_within_slot index out of bounds"); |
313 } | 307 } |
314 | 308 |
315 public: | 309 public: |
316 // Construction. bits_per_slot must be greater than 0. | 310 // Construction. bits_per_slot must be greater than 0. |
317 BitMap2D(uintptr_t* map, idx_t size_in_slots, idx_t bits_per_slot); | 311 BitMap2D(bm_word_t* map, idx_t size_in_slots, idx_t bits_per_slot); |
318 | 312 |
319 // Allocates necessary data structure in resource area. bits_per_slot must be greater than 0. | 313 // Allocates necessary data structure in resource area. bits_per_slot must be greater than 0. |
320 BitMap2D(idx_t size_in_slots, idx_t bits_per_slot); | 314 BitMap2D(idx_t size_in_slots, idx_t bits_per_slot); |
321 | 315 |
322 idx_t size_in_bits() { | 316 idx_t size_in_bits() { |
357 void at_put_grow(idx_t slot_index, idx_t bit_within_slot_index, bool value) { | 351 void at_put_grow(idx_t slot_index, idx_t bit_within_slot_index, bool value) { |
358 verify_bit_within_slot_index(bit_within_slot_index); | 352 verify_bit_within_slot_index(bit_within_slot_index); |
359 _map.at_put_grow(bit_index(slot_index, bit_within_slot_index), value); | 353 _map.at_put_grow(bit_index(slot_index, bit_within_slot_index), value); |
360 } | 354 } |
361 | 355 |
362 void clear() { | 356 void clear(); |
363 _map.clear(); | |
364 } | |
365 }; | 357 }; |
366 | 358 |
367 | 359 // Closure for iterating over BitMaps |
368 | 360 |
369 inline void BitMap::set_range_of_words(idx_t beg, idx_t end) { | 361 class BitMapClosure VALUE_OBJ_CLASS_SPEC { |
370 uintptr_t* map = _map; | 362 public: |
371 for (idx_t i = beg; i < end; ++i) map[i] = ~(uintptr_t)0; | 363 // Callback when bit in map is set. Should normally return "true"; |
372 } | 364 // return of false indicates that the bitmap iteration should terminate. |
373 | 365 virtual bool do_bit(BitMap::idx_t offset) = 0; |
374 | 366 }; |
375 inline void BitMap::clear_range_of_words(idx_t beg, idx_t end) { | |
376 uintptr_t* map = _map; | |
377 for (idx_t i = beg; i < end; ++i) map[i] = 0; | |
378 } | |
379 | |
380 | |
381 inline void BitMap::clear() { | |
382 clear_range_of_words(0, size_in_words()); | |
383 } | |
384 | |
385 | |
386 inline void BitMap::par_clear_range(idx_t beg, idx_t end, RangeSizeHint hint) { | |
387 if (hint == small_range && end - beg == 1) { | |
388 par_at_put(beg, false); | |
389 } else { | |
390 if (hint == large_range) { | |
391 par_at_put_large_range(beg, end, false); | |
392 } else { | |
393 par_at_put_range(beg, end, false); | |
394 } | |
395 } | |
396 } |