Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/opto/indexSet.cpp
32285 views
/*1* Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "memory/allocation.inline.hpp"26#include "opto/chaitin.hpp"27#include "opto/compile.hpp"28#include "opto/indexSet.hpp"29#include "opto/regmask.hpp"3031// This file defines the IndexSet class, a set of sparse integer indices.32// This data structure is used by the compiler in its liveness analysis and33// during register allocation. It also defines an iterator for this class.3435//-------------------------------- Initializations ------------------------------3637IndexSet::BitBlock IndexSet::_empty_block = IndexSet::BitBlock();3839#ifdef ASSERT40// Initialize statistics counters41julong IndexSet::_alloc_new = 0;42julong IndexSet::_alloc_total = 0;4344julong IndexSet::_total_bits = 0;45julong IndexSet::_total_used_blocks = 0;46julong IndexSet::_total_unused_blocks = 0;4748// Per set, or all sets operation tracing49int IndexSet::_serial_count = 1;50#endif5152// What is the first set bit in a 5 bit integer?53const byte IndexSetIterator::_first_bit[32] = {540, 0, 1, 0,552, 0, 1, 0,563, 0, 1, 0,572, 0, 1, 0,584, 0, 1, 0,592, 0, 1, 0,603, 0, 1, 0,612, 0, 1, 062};6364// What is the second set bit in a 5 bit integer?65const byte IndexSetIterator::_second_bit[32] = {665, 5, 5, 1,675, 2, 2, 1,685, 3, 3, 1,693, 2, 2, 1,705, 4, 4, 1,714, 2, 2, 1,724, 3, 3, 1,733, 2, 2, 174};7576// I tried implementing the IndexSetIterator with a window_size of 8 and77// didn't seem to get a noticeable speedup. I am leaving in the tables78// in case we want to switch back.7980/*const byte IndexSetIterator::_first_bit[256] = {818, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,824, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,835, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,844, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,856, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,864, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,875, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,884, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,897, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,904, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,915, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,924, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,936, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,944, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,955, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,964, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 097};9899const byte IndexSetIterator::_second_bit[256] = {1008, 8, 8, 1, 8, 2, 2, 1, 8, 3, 3, 1, 3, 2, 2, 1,1018, 4, 4, 1, 4, 2, 2, 1, 4, 3, 3, 1, 3, 2, 2, 1,1028, 5, 5, 1, 5, 2, 2, 1, 5, 3, 3, 1, 3, 2, 2, 1,1035, 4, 4, 1, 4, 2, 2, 1, 4, 3, 3, 1, 3, 2, 2, 1,1048, 6, 6, 1, 6, 2, 2, 1, 6, 3, 3, 1, 3, 2, 2, 1,1056, 4, 4, 1, 4, 2, 2, 1, 4, 3, 3, 1, 3, 2, 2, 1,1066, 5, 5, 1, 5, 2, 2, 1, 5, 3, 3, 1, 3, 2, 2, 1,1075, 4, 4, 1, 4, 2, 2, 1, 4, 3, 3, 1, 3, 2, 2, 1,1088, 7, 7, 1, 7, 2, 2, 1, 7, 3, 3, 1, 3, 2, 2, 1,1097, 4, 4, 1, 4, 2, 2, 1, 4, 3, 3, 1, 3, 2, 2, 1,1107, 5, 5, 1, 5, 2, 2, 1, 5, 3, 3, 1, 3, 2, 2, 1,1115, 4, 4, 1, 4, 2, 2, 1, 4, 3, 3, 1, 3, 2, 2, 1,1127, 6, 6, 1, 6, 2, 2, 1, 6, 3, 3, 1, 3, 2, 2, 1,1136, 4, 4, 1, 4, 2, 2, 1, 4, 3, 3, 1, 3, 2, 2, 1,1146, 5, 5, 1, 5, 2, 2, 1, 5, 3, 3, 1, 3, 2, 2, 1,1155, 4, 4, 1, 4, 2, 2, 1, 4, 3, 3, 1, 3, 2, 2, 1116};*/117118//---------------------------- IndexSet::populate_free_list() -----------------------------119// Populate the free BitBlock list with a batch of BitBlocks. The BitBlocks120// are 32 bit aligned.121122void IndexSet::populate_free_list() {123Compile *compile = Compile::current();124BitBlock *free = (BitBlock*)compile->indexSet_free_block_list();125126char *mem = (char*)arena()->Amalloc_4(sizeof(BitBlock) *127bitblock_alloc_chunk_size + 32);128129// Align the pointer to a 32 bit boundary.130BitBlock *new_blocks = (BitBlock*)(((uintptr_t)mem + 32) & ~0x001F);131132// Add the new blocks to the free list.133for (int i = 0; i < bitblock_alloc_chunk_size; i++) {134new_blocks->set_next(free);135free = new_blocks;136new_blocks++;137}138139compile->set_indexSet_free_block_list(free);140141#ifdef ASSERT142if (CollectIndexSetStatistics) {143inc_stat_counter(&_alloc_new, bitblock_alloc_chunk_size);144}145#endif146}147148149//---------------------------- IndexSet::alloc_block() ------------------------150// Allocate a BitBlock from the free list. If the free list is empty,151// prime it.152153IndexSet::BitBlock *IndexSet::alloc_block() {154#ifdef ASSERT155if (CollectIndexSetStatistics) {156inc_stat_counter(&_alloc_total, 1);157}158#endif159Compile *compile = Compile::current();160BitBlock* free_list = (BitBlock*)compile->indexSet_free_block_list();161if (free_list == NULL) {162populate_free_list();163free_list = (BitBlock*)compile->indexSet_free_block_list();164}165BitBlock *block = free_list;166compile->set_indexSet_free_block_list(block->next());167168block->clear();169return block;170}171172//---------------------------- IndexSet::alloc_block_containing() -------------173// Allocate a new BitBlock and put it into the position in the _blocks array174// corresponding to element.175176IndexSet::BitBlock *IndexSet::alloc_block_containing(uint element) {177BitBlock *block = alloc_block();178uint bi = get_block_index(element);179_blocks[bi] = block;180return block;181}182183//---------------------------- IndexSet::free_block() -------------------------184// Add a BitBlock to the free list.185186void IndexSet::free_block(uint i) {187debug_only(check_watch("free block", i));188assert(i < _max_blocks, "block index too large");189BitBlock *block = _blocks[i];190assert(block != &_empty_block, "cannot free the empty block");191block->set_next((IndexSet::BitBlock*)Compile::current()->indexSet_free_block_list());192Compile::current()->set_indexSet_free_block_list(block);193set_block(i,&_empty_block);194}195196//------------------------------lrg_union--------------------------------------197// Compute the union of all elements of one and two which interfere with198// the RegMask mask. If the degree of the union becomes exceeds199// fail_degree, the union bails out. The underlying set is cleared before200// the union is performed.201202uint IndexSet::lrg_union(uint lr1, uint lr2,203const uint fail_degree,204const PhaseIFG *ifg,205const RegMask &mask ) {206IndexSet *one = ifg->neighbors(lr1);207IndexSet *two = ifg->neighbors(lr2);208LRG &lrg1 = ifg->lrgs(lr1);209LRG &lrg2 = ifg->lrgs(lr2);210#ifdef ASSERT211assert(_max_elements == one->_max_elements, "max element mismatch");212check_watch("union destination");213one->check_watch("union source");214two->check_watch("union source");215#endif216217// Compute the degree of the combined live-range. The combined218// live-range has the union of the original live-ranges' neighbors set as219// well as the neighbors of all intermediate copies, minus those neighbors220// that can not use the intersected allowed-register-set.221222// Copy the larger set. Insert the smaller set into the larger.223if (two->count() > one->count()) {224IndexSet *temp = one;225one = two;226two = temp;227}228229clear();230231// Used to compute degree of register-only interferences. Infinite-stack232// neighbors do not alter colorability, as they can always color to some233// other color. (A variant of the Briggs assertion)234uint reg_degree = 0;235236uint element;237// Load up the combined interference set with the neighbors of one238IndexSetIterator elements(one);239while ((element = elements.next()) != 0) {240LRG &lrg = ifg->lrgs(element);241if (mask.overlap(lrg.mask())) {242insert(element);243if( !lrg.mask().is_AllStack() ) {244reg_degree += lrg1.compute_degree(lrg);245if( reg_degree >= fail_degree ) return reg_degree;246} else {247// !!!!! Danger! No update to reg_degree despite having a neighbor.248// A variant of the Briggs assertion.249// Not needed if I simplify during coalesce, ala George/Appel.250assert( lrg.lo_degree(), "" );251}252}253}254// Add neighbors of two as well255IndexSetIterator elements2(two);256while ((element = elements2.next()) != 0) {257LRG &lrg = ifg->lrgs(element);258if (mask.overlap(lrg.mask())) {259if (insert(element)) {260if( !lrg.mask().is_AllStack() ) {261reg_degree += lrg2.compute_degree(lrg);262if( reg_degree >= fail_degree ) return reg_degree;263} else {264// !!!!! Danger! No update to reg_degree despite having a neighbor.265// A variant of the Briggs assertion.266// Not needed if I simplify during coalesce, ala George/Appel.267assert( lrg.lo_degree(), "" );268}269}270}271}272273return reg_degree;274}275276//---------------------------- IndexSet() -----------------------------277// A deep copy constructor. This is used when you need a scratch copy of this set.278279IndexSet::IndexSet (IndexSet *set) {280#ifdef ASSERT281_serial_number = _serial_count++;282set->check_watch("copied", _serial_number);283check_watch("initialized by copy", set->_serial_number);284_max_elements = set->_max_elements;285#endif286_count = set->_count;287_max_blocks = set->_max_blocks;288if (_max_blocks <= preallocated_block_list_size) {289_blocks = _preallocated_block_list;290} else {291_blocks =292(IndexSet::BitBlock**) arena()->Amalloc_4(sizeof(IndexSet::BitBlock**) * _max_blocks);293}294for (uint i = 0; i < _max_blocks; i++) {295BitBlock *block = set->_blocks[i];296if (block == &_empty_block) {297set_block(i, &_empty_block);298} else {299BitBlock *new_block = alloc_block();300memcpy(new_block->words(), block->words(), sizeof(uint32) * words_per_block);301set_block(i, new_block);302}303}304}305306//---------------------------- IndexSet::initialize() -----------------------------307// Prepare an IndexSet for use.308309void IndexSet::initialize(uint max_elements) {310#ifdef ASSERT311_serial_number = _serial_count++;312check_watch("initialized", max_elements);313_max_elements = max_elements;314#endif315_count = 0;316_max_blocks = (max_elements + bits_per_block - 1) / bits_per_block;317318if (_max_blocks <= preallocated_block_list_size) {319_blocks = _preallocated_block_list;320} else {321_blocks = (IndexSet::BitBlock**) arena()->Amalloc_4(sizeof(IndexSet::BitBlock**) * _max_blocks);322}323for (uint i = 0; i < _max_blocks; i++) {324set_block(i, &_empty_block);325}326}327328//---------------------------- IndexSet::initialize()------------------------------329// Prepare an IndexSet for use. If it needs to allocate its _blocks array, it does330// so from the Arena passed as a parameter. BitBlock allocation is still done from331// the static Arena which was set with reset_memory().332333void IndexSet::initialize(uint max_elements, Arena *arena) {334#ifdef ASSERT335_serial_number = _serial_count++;336check_watch("initialized2", max_elements);337_max_elements = max_elements;338#endif // ASSERT339_count = 0;340_max_blocks = (max_elements + bits_per_block - 1) / bits_per_block;341342if (_max_blocks <= preallocated_block_list_size) {343_blocks = _preallocated_block_list;344} else {345_blocks = (IndexSet::BitBlock**) arena->Amalloc_4(sizeof(IndexSet::BitBlock**) * _max_blocks);346}347for (uint i = 0; i < _max_blocks; i++) {348set_block(i, &_empty_block);349}350}351352//---------------------------- IndexSet::swap() -----------------------------353// Exchange two IndexSets.354355void IndexSet::swap(IndexSet *set) {356#ifdef ASSERT357assert(_max_elements == set->_max_elements, "must have same universe size to swap");358check_watch("swap", set->_serial_number);359set->check_watch("swap", _serial_number);360#endif361362for (uint i = 0; i < _max_blocks; i++) {363BitBlock *temp = _blocks[i];364set_block(i, set->_blocks[i]);365set->set_block(i, temp);366}367uint temp = _count;368_count = set->_count;369set->_count = temp;370}371372//---------------------------- IndexSet::dump() -----------------------------373// Print this set. Used for debugging.374375#ifndef PRODUCT376void IndexSet::dump() const {377IndexSetIterator elements(this);378379tty->print("{");380uint i;381while ((i = elements.next()) != 0) {382tty->print("L%d ", i);383}384tty->print_cr("}");385}386#endif387388#ifdef ASSERT389//---------------------------- IndexSet::tally_iteration_statistics() -----------------------------390// Update block/bit counts to reflect that this set has been iterated over.391392void IndexSet::tally_iteration_statistics() const {393inc_stat_counter(&_total_bits, count());394395for (uint i = 0; i < _max_blocks; i++) {396if (_blocks[i] != &_empty_block) {397inc_stat_counter(&_total_used_blocks, 1);398} else {399inc_stat_counter(&_total_unused_blocks, 1);400}401}402}403404//---------------------------- IndexSet::print_statistics() -----------------------------405// Print statistics about IndexSet usage.406407void IndexSet::print_statistics() {408julong total_blocks = _total_used_blocks + _total_unused_blocks;409tty->print_cr ("Accumulated IndexSet usage statistics:");410tty->print_cr ("--------------------------------------");411tty->print_cr (" Iteration:");412tty->print_cr (" blocks visited: " UINT64_FORMAT, total_blocks);413tty->print_cr (" blocks empty: %4.2f%%", 100.0*(double)_total_unused_blocks/total_blocks);414tty->print_cr (" bit density (bits/used blocks): %4.2f", (double)_total_bits/_total_used_blocks);415tty->print_cr (" bit density (bits/all blocks): %4.2f", (double)_total_bits/total_blocks);416tty->print_cr (" Allocation:");417tty->print_cr (" blocks allocated: " UINT64_FORMAT, _alloc_new);418tty->print_cr (" blocks used/reused: " UINT64_FORMAT, _alloc_total);419}420421//---------------------------- IndexSet::verify() -----------------------------422// Expensive test of IndexSet sanity. Ensure that the count agrees with the423// number of bits in the blocks. Make sure the iterator is seeing all elements424// of the set. Meant for use during development.425426void IndexSet::verify() const {427assert(!member(0), "zero cannot be a member");428uint count = 0;429uint i;430for (i = 1; i < _max_elements; i++) {431if (member(i)) {432count++;433assert(count <= _count, "_count is messed up");434}435}436437IndexSetIterator elements(this);438count = 0;439while ((i = elements.next()) != 0) {440count++;441assert(member(i), "returned a non member");442assert(count <= _count, "iterator returned wrong number of elements");443}444}445#endif446447//---------------------------- IndexSetIterator() -----------------------------448// Create an iterator for a set. If empty blocks are detected when iterating449// over the set, these blocks are replaced.450451IndexSetIterator::IndexSetIterator(IndexSet *set) {452#ifdef ASSERT453if (CollectIndexSetStatistics) {454set->tally_iteration_statistics();455}456set->check_watch("traversed", set->count());457#endif458if (set->is_empty()) {459_current = 0;460_next_word = IndexSet::words_per_block;461_next_block = 1;462_max_blocks = 1;463464// We don't need the following values when we iterate over an empty set.465// The commented out code is left here to document that the omission466// is intentional.467//468//_value = 0;469//_words = NULL;470//_blocks = NULL;471//_set = NULL;472} else {473_current = 0;474_value = 0;475_next_block = 0;476_next_word = IndexSet::words_per_block;477478_max_blocks = set->_max_blocks;479_words = NULL;480_blocks = set->_blocks;481_set = set;482}483}484485//---------------------------- IndexSetIterator(const) -----------------------------486// Iterate over a constant IndexSet.487488IndexSetIterator::IndexSetIterator(const IndexSet *set) {489#ifdef ASSERT490if (CollectIndexSetStatistics) {491set->tally_iteration_statistics();492}493// We don't call check_watch from here to avoid bad recursion.494// set->check_watch("traversed const", set->count());495#endif496if (set->is_empty()) {497_current = 0;498_next_word = IndexSet::words_per_block;499_next_block = 1;500_max_blocks = 1;501502// We don't need the following values when we iterate over an empty set.503// The commented out code is left here to document that the omission504// is intentional.505//506//_value = 0;507//_words = NULL;508//_blocks = NULL;509//_set = NULL;510} else {511_current = 0;512_value = 0;513_next_block = 0;514_next_word = IndexSet::words_per_block;515516_max_blocks = set->_max_blocks;517_words = NULL;518_blocks = set->_blocks;519_set = NULL;520}521}522523//---------------------------- List16Iterator::advance_and_next() -----------------------------524// Advance to the next non-empty word in the set being iterated over. Return the next element525// if there is one. If we are done, return 0. This method is called from the next() method526// when it gets done with a word.527528uint IndexSetIterator::advance_and_next() {529// See if there is another non-empty word in the current block.530for (uint wi = _next_word; wi < (unsigned)IndexSet::words_per_block; wi++) {531if (_words[wi] != 0) {532// Found a non-empty word.533_value = ((_next_block - 1) * IndexSet::bits_per_block) + (wi * IndexSet::bits_per_word);534_current = _words[wi];535536_next_word = wi+1;537538return next();539}540}541542// We ran out of words in the current block. Advance to next non-empty block.543for (uint bi = _next_block; bi < _max_blocks; bi++) {544if (_blocks[bi] != &IndexSet::_empty_block) {545// Found a non-empty block.546547_words = _blocks[bi]->words();548for (uint wi = 0; wi < (unsigned)IndexSet::words_per_block; wi++) {549if (_words[wi] != 0) {550// Found a non-empty word.551_value = (bi * IndexSet::bits_per_block) + (wi * IndexSet::bits_per_word);552_current = _words[wi];553554_next_block = bi+1;555_next_word = wi+1;556557return next();558}559}560561// All of the words in the block were empty. Replace562// the block with the empty block.563if (_set) {564_set->free_block(bi);565}566}567}568569// These assignments make redundant calls to next on a finished iterator570// faster. Probably not necessary.571_next_block = _max_blocks;572_next_word = IndexSet::words_per_block;573574// No more words.575return 0;576}577578579