1 /*
2  * Copyright (c) 1998, 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 #include "precompiled.hpp"
26 #include "memory/allocation.inline.hpp"
27 #include "opto/chaitin.hpp"
28 #include "opto/compile.hpp"
29 #include "opto/indexSet.hpp"
30 #include "opto/regmask.hpp"
31 
32 // This file defines the IndexSet class, a set of sparse integer indices.
33 // This data structure is used by the compiler in its liveness analysis and
34 // during register allocation.  It also defines an iterator for this class.
35 
36 //-------------------------------- Initializations ------------------------------
37 
38 IndexSet::BitBlock  IndexSet::_empty_block     = IndexSet::BitBlock();
39 
40 #ifdef ASSERT
41 // Initialize statistics counters
42 julong IndexSet::_alloc_new = 0;
43 julong IndexSet::_alloc_total = 0;
44 
45 julong IndexSet::_total_bits = 0;
46 julong IndexSet::_total_used_blocks = 0;
47 julong IndexSet::_total_unused_blocks = 0;
48 
49 // Per set, or all sets operation tracing
50 int IndexSet::_serial_count = 1;
51 #endif
52 
53 //---------------------------- IndexSet::populate_free_list() -----------------------------
54 // Populate the free BitBlock list with a batch of BitBlocks.  The BitBlocks
55 // are 32 bit aligned.
56 
populate_free_list()57 void IndexSet::populate_free_list() {
58   Compile *compile = Compile::current();
59   BitBlock *free = (BitBlock*)compile->indexSet_free_block_list();
60 
61   char *mem = (char*)arena()->Amalloc_4(sizeof(BitBlock) *
62                                         bitblock_alloc_chunk_size + 32);
63 
64   // Align the pointer to a 32 bit boundary.
65   BitBlock *new_blocks = (BitBlock*)(((uintptr_t)mem + 32) & ~0x001F);
66 
67   // Add the new blocks to the free list.
68   for (int i = 0; i < bitblock_alloc_chunk_size; i++) {
69     new_blocks->set_next(free);
70     free = new_blocks;
71     new_blocks++;
72   }
73 
74   compile->set_indexSet_free_block_list(free);
75 
76 #ifdef ASSERT
77   if (CollectIndexSetStatistics) {
78     inc_stat_counter(&_alloc_new, bitblock_alloc_chunk_size);
79   }
80 #endif
81 }
82 
83 
84 //---------------------------- IndexSet::alloc_block() ------------------------
85 // Allocate a BitBlock from the free list.  If the free list is empty,
86 // prime it.
87 
alloc_block()88 IndexSet::BitBlock *IndexSet::alloc_block() {
89 #ifdef ASSERT
90   if (CollectIndexSetStatistics) {
91     inc_stat_counter(&_alloc_total, 1);
92   }
93 #endif
94   Compile *compile = Compile::current();
95   BitBlock* free_list = (BitBlock*)compile->indexSet_free_block_list();
96   if (free_list == NULL) {
97     populate_free_list();
98     free_list = (BitBlock*)compile->indexSet_free_block_list();
99   }
100   BitBlock *block = free_list;
101   compile->set_indexSet_free_block_list(block->next());
102 
103   block->clear();
104   return block;
105 }
106 
107 //---------------------------- IndexSet::alloc_block_containing() -------------
108 // Allocate a new BitBlock and put it into the position in the _blocks array
109 // corresponding to element.
110 
alloc_block_containing(uint element)111 IndexSet::BitBlock *IndexSet::alloc_block_containing(uint element) {
112   BitBlock *block = alloc_block();
113   uint bi = get_block_index(element);
114   if (bi >= _current_block_limit) {
115     _current_block_limit = bi + 1;
116   }
117   _blocks[bi] = block;
118   return block;
119 }
120 
121 //---------------------------- IndexSet::free_block() -------------------------
122 // Add a BitBlock to the free list.
123 
free_block(uint i)124 void IndexSet::free_block(uint i) {
125   debug_only(check_watch("free block", i));
126   assert(i < _max_blocks, "block index too large");
127   BitBlock *block = _blocks[i];
128   assert(block != &_empty_block, "cannot free the empty block");
129   block->set_next((IndexSet::BitBlock*)Compile::current()->indexSet_free_block_list());
130   Compile::current()->set_indexSet_free_block_list(block);
131   set_block(i, &_empty_block);
132 }
133 
134 //------------------------------lrg_union--------------------------------------
135 // Compute the union of all elements of one and two which interfere with
136 // the RegMask mask.  If the degree of the union becomes exceeds
137 // fail_degree, the union bails out.  The underlying set is cleared before
138 // the union is performed.
139 
lrg_union(uint lr1,uint lr2,const uint fail_degree,const PhaseIFG * ifg,const RegMask & mask)140 uint IndexSet::lrg_union(uint lr1, uint lr2,
141                          const uint fail_degree,
142                          const PhaseIFG *ifg,
143                          const RegMask &mask ) {
144   IndexSet *one = ifg->neighbors(lr1);
145   IndexSet *two = ifg->neighbors(lr2);
146   LRG &lrg1 = ifg->lrgs(lr1);
147   LRG &lrg2 = ifg->lrgs(lr2);
148 #ifdef ASSERT
149   assert(_max_elements == one->_max_elements, "max element mismatch");
150   check_watch("union destination");
151   one->check_watch("union source");
152   two->check_watch("union source");
153 #endif
154 
155   // Compute the degree of the combined live-range.  The combined
156   // live-range has the union of the original live-ranges' neighbors set as
157   // well as the neighbors of all intermediate copies, minus those neighbors
158   // that can not use the intersected allowed-register-set.
159 
160   // Copy the larger set.  Insert the smaller set into the larger.
161   if (two->count() > one->count()) {
162     IndexSet *temp = one;
163     one = two;
164     two = temp;
165   }
166 
167   clear();
168 
169   // Used to compute degree of register-only interferences.  Infinite-stack
170   // neighbors do not alter colorability, as they can always color to some
171   // other color.  (A variant of the Briggs assertion)
172   uint reg_degree = 0;
173 
174   uint element = 0;
175   // Load up the combined interference set with the neighbors of one
176   if (!one->is_empty()) {
177     IndexSetIterator elements(one);
178     while ((element = elements.next()) != 0) {
179       LRG &lrg = ifg->lrgs(element);
180       if (mask.overlap(lrg.mask())) {
181         insert(element);
182         if (!lrg.mask().is_AllStack()) {
183           reg_degree += lrg1.compute_degree(lrg);
184           if (reg_degree >= fail_degree) return reg_degree;
185         } else {
186           // !!!!! Danger!  No update to reg_degree despite having a neighbor.
187           // A variant of the Briggs assertion.
188           // Not needed if I simplify during coalesce, ala George/Appel.
189           assert(lrg.lo_degree(), "");
190         }
191       }
192     }
193   }
194   // Add neighbors of two as well
195 
196   if (!two->is_empty()) {
197     IndexSetIterator elements2(two);
198     while ((element = elements2.next()) != 0) {
199       LRG &lrg = ifg->lrgs(element);
200       if (mask.overlap(lrg.mask())) {
201         if (insert(element)) {
202           if (!lrg.mask().is_AllStack()) {
203             reg_degree += lrg2.compute_degree(lrg);
204             if (reg_degree >= fail_degree) return reg_degree;
205           } else {
206             // !!!!! Danger!  No update to reg_degree despite having a neighbor.
207             // A variant of the Briggs assertion.
208             // Not needed if I simplify during coalesce, ala George/Appel.
209             assert(lrg.lo_degree(), "");
210           }
211         }
212       }
213     }
214   }
215 
216   return reg_degree;
217 }
218 
219 //---------------------------- IndexSet() -----------------------------
220 // A deep copy constructor.  This is used when you need a scratch copy of this set.
221 
IndexSet(IndexSet * set)222 IndexSet::IndexSet (IndexSet *set) {
223 #ifdef ASSERT
224   _serial_number = _serial_count++;
225   set->check_watch("copied", _serial_number);
226   check_watch("initialized by copy", set->_serial_number);
227   _max_elements = set->_max_elements;
228 #endif
229   _count = set->_count;
230   _current_block_limit = set->_current_block_limit;
231   _max_blocks = set->_max_blocks;
232   if (_max_blocks <= preallocated_block_list_size) {
233     _blocks = _preallocated_block_list;
234   } else {
235     _blocks =
236       (IndexSet::BitBlock**) arena()->Amalloc_4(sizeof(IndexSet::BitBlock**) * _max_blocks);
237   }
238   for (uint i = 0; i < _max_blocks; i++) {
239     BitBlock *block = set->_blocks[i];
240     if (block == &_empty_block) {
241       set_block(i, &_empty_block);
242     } else {
243       BitBlock *new_block = alloc_block();
244       memcpy(new_block->words(), block->words(), sizeof(uintptr_t) * words_per_block);
245       set_block(i, new_block);
246     }
247   }
248 }
249 
250 //---------------------------- IndexSet::initialize() -----------------------------
251 // Prepare an IndexSet for use.
252 
initialize(uint max_elements)253 void IndexSet::initialize(uint max_elements) {
254 #ifdef ASSERT
255   _serial_number = _serial_count++;
256   check_watch("initialized", max_elements);
257   _max_elements = max_elements;
258 #endif
259   _count = 0;
260   _current_block_limit = 0;
261   _max_blocks = (max_elements + bits_per_block - 1) / bits_per_block;
262 
263   if (_max_blocks <= preallocated_block_list_size) {
264     _blocks = _preallocated_block_list;
265   } else {
266     _blocks = (IndexSet::BitBlock**) arena()->Amalloc_4(sizeof(IndexSet::BitBlock*) * _max_blocks);
267   }
268   for (uint i = 0; i < _max_blocks; i++) {
269     set_block(i, &_empty_block);
270   }
271 }
272 
273 //---------------------------- IndexSet::initialize()------------------------------
274 // Prepare an IndexSet for use.  If it needs to allocate its _blocks array, it does
275 // so from the Arena passed as a parameter.  BitBlock allocation is still done from
276 // the static Arena which was set with reset_memory().
277 
initialize(uint max_elements,Arena * arena)278 void IndexSet::initialize(uint max_elements, Arena *arena) {
279 #ifdef ASSERT
280   _serial_number = _serial_count++;
281   check_watch("initialized2", max_elements);
282   _max_elements = max_elements;
283 #endif // ASSERT
284   _count = 0;
285   _current_block_limit = 0;
286   _max_blocks = (max_elements + bits_per_block - 1) / bits_per_block;
287 
288   if (_max_blocks <= preallocated_block_list_size) {
289     _blocks = _preallocated_block_list;
290   } else {
291     _blocks = (IndexSet::BitBlock**) arena->Amalloc_4(sizeof(IndexSet::BitBlock*) * _max_blocks);
292   }
293   for (uint i = 0; i < _max_blocks; i++) {
294     set_block(i, &_empty_block);
295   }
296 }
297 
298 //---------------------------- IndexSet::swap() -----------------------------
299 // Exchange two IndexSets.
300 
swap(IndexSet * set)301 void IndexSet::swap(IndexSet *set) {
302 #ifdef ASSERT
303   assert(_max_elements == set->_max_elements, "must have same universe size to swap");
304   check_watch("swap", set->_serial_number);
305   set->check_watch("swap", _serial_number);
306 #endif
307 
308   uint max = MAX2(_current_block_limit, set->_current_block_limit);
309   for (uint i = 0; i < max; i++) {
310     BitBlock *temp = _blocks[i];
311     set_block(i, set->_blocks[i]);
312     set->set_block(i, temp);
313   }
314   uint temp = _count;
315   _count = set->_count;
316   set->_count = temp;
317 
318   temp = _current_block_limit;
319   _current_block_limit = set->_current_block_limit;
320   set->_current_block_limit = temp;
321 
322 }
323 
324 //---------------------------- IndexSet::dump() -----------------------------
325 // Print this set.  Used for debugging.
326 
327 #ifndef PRODUCT
dump() const328 void IndexSet::dump() const {
329   IndexSetIterator elements(this);
330 
331   tty->print("{");
332   uint i;
333   while ((i = elements.next()) != 0) {
334     tty->print("L%d ", i);
335   }
336   tty->print_cr("}");
337 }
338 #endif
339 
340 #ifdef ASSERT
341 //---------------------------- IndexSet::tally_iteration_statistics() -----------------------------
342 // Update block/bit counts to reflect that this set has been iterated over.
343 
tally_iteration_statistics() const344 void IndexSet::tally_iteration_statistics() const {
345   inc_stat_counter(&_total_bits, count());
346 
347   for (uint i = 0; i < _max_blocks; i++) {
348     if (_blocks[i] != &_empty_block) {
349       inc_stat_counter(&_total_used_blocks, 1);
350     } else {
351       inc_stat_counter(&_total_unused_blocks, 1);
352     }
353   }
354 }
355 
356 //---------------------------- IndexSet::print_statistics() -----------------------------
357 // Print statistics about IndexSet usage.
358 
print_statistics()359 void IndexSet::print_statistics() {
360   julong total_blocks = _total_used_blocks + _total_unused_blocks;
361   tty->print_cr ("Accumulated IndexSet usage statistics:");
362   tty->print_cr ("--------------------------------------");
363   tty->print_cr ("  Iteration:");
364   tty->print_cr ("    blocks visited: " UINT64_FORMAT, total_blocks);
365   tty->print_cr ("    blocks empty: %4.2f%%", 100.0*(double)_total_unused_blocks/total_blocks);
366   tty->print_cr ("    bit density (bits/used blocks): %4.2f", (double)_total_bits/_total_used_blocks);
367   tty->print_cr ("    bit density (bits/all blocks): %4.2f", (double)_total_bits/total_blocks);
368   tty->print_cr ("  Allocation:");
369   tty->print_cr ("    blocks allocated: " UINT64_FORMAT, _alloc_new);
370   tty->print_cr ("    blocks used/reused: " UINT64_FORMAT, _alloc_total);
371 }
372 
373 //---------------------------- IndexSet::verify() -----------------------------
374 // Expensive test of IndexSet sanity.  Ensure that the count agrees with the
375 // number of bits in the blocks.  Make sure the iterator is seeing all elements
376 // of the set.  Meant for use during development.
377 
verify() const378 void IndexSet::verify() const {
379   assert(!member(0), "zero cannot be a member");
380   uint count = 0;
381   uint i;
382   for (i = 1; i < _max_elements; i++) {
383     if (member(i)) {
384       count++;
385       assert(count <= _count, "_count is messed up");
386     }
387   }
388 
389   IndexSetIterator elements(this);
390   count = 0;
391   while ((i = elements.next()) != 0) {
392     count++;
393     assert(member(i), "returned a non member");
394     assert(count <= _count, "iterator returned wrong number of elements");
395   }
396 }
397 #endif
398 
399 //---------------------------- IndexSetIterator() -----------------------------
400 // Create an iterator for a set.  If empty blocks are detected when iterating
401 // over the set, these blocks are replaced.
402 
403 //---------------------------- List16Iterator::advance_and_next() -----------------------------
404 // Advance to the next non-empty word in the set being iterated over.  Return the next element
405 // if there is one.  If we are done, return 0.  This method is called from the next() method
406 // when it gets done with a word.
407 
advance_and_next()408 uint IndexSetIterator::advance_and_next() {
409   // See if there is another non-empty word in the current block.
410   for (uint wi = _next_word; wi < (unsigned)IndexSet::words_per_block; wi++) {
411     if (_words[wi] != 0) {
412       // Found a non-empty word.
413       _value = ((_next_block - 1) * IndexSet::bits_per_block) + (wi * IndexSet::bits_per_word);
414       _current = _words[wi];
415       _next_word = wi + 1;
416       return next_value();
417     }
418   }
419 
420   // We ran out of words in the current block.  Advance to next non-empty block.
421   for (uint bi = _next_block; bi < _max_blocks; bi++) {
422     if (_blocks[bi] != &IndexSet::_empty_block) {
423       // Found a non-empty block.
424 
425       _words = _blocks[bi]->words();
426       for (uint wi = 0; wi < (unsigned)IndexSet::words_per_block; wi++) {
427         if (_words[wi] != 0) {
428           // Found a non-empty word.
429           _value = (bi * IndexSet::bits_per_block) + (wi * IndexSet::bits_per_word);
430           _current = _words[wi];
431 
432           _next_block = bi+1;
433           _next_word = wi+1;
434           return next_value();
435         }
436       }
437 
438       // All of the words in the block were empty.  Replace
439       // the block with the empty block.
440       if (_set) {
441         _set->free_block(bi);
442       }
443     }
444   }
445 
446   // No more words.
447   return 0;
448 }
449