0N/A/*
2122N/A * Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A *
0N/A */
0N/A
1879N/A#ifndef SHARE_VM_OPTO_INDEXSET_HPP
1879N/A#define SHARE_VM_OPTO_INDEXSET_HPP
1879N/A
1879N/A#include "memory/allocation.hpp"
1879N/A#include "memory/resourceArea.hpp"
1879N/A#include "opto/compile.hpp"
1879N/A#include "opto/regmask.hpp"
1879N/A
0N/A// This file defines the IndexSet class, a set of sparse integer indices.
0N/A// This data structure is used by the compiler in its liveness analysis and
0N/A// during register allocation.
0N/A
0N/A//-------------------------------- class IndexSet ----------------------------
0N/A// An IndexSet is a piece-wise bitvector. At the top level, we have an array
0N/A// of pointers to bitvector chunks called BitBlocks. Each BitBlock has a fixed
0N/A// size and is allocated from a shared free list. The bits which are set in
0N/A// each BitBlock correspond to the elements of the set.
0N/A
0N/Aclass IndexSet : public ResourceObj {
0N/A friend class IndexSetIterator;
0N/A
0N/A public:
0N/A // When we allocate an IndexSet, it starts off with an array of top level block
0N/A // pointers of a set length. This size is intended to be large enough for the
0N/A // majority of IndexSets. In the cases when this size is not large enough,
0N/A // a separately allocated array is used.
0N/A
0N/A // The length of the preallocated top level block array
0N/A enum { preallocated_block_list_size = 16 };
0N/A
0N/A // Elements of a IndexSet get decomposed into three fields. The highest order
0N/A // bits are the block index, which tell which high level block holds the element.
0N/A // Within that block, the word index indicates which word holds the element.
0N/A // Finally, the bit index determines which single bit within that word indicates
0N/A // membership of the element in the set.
0N/A
0N/A // The lengths of the index bitfields
0N/A enum { bit_index_length = 5,
0N/A word_index_length = 3,
0N/A block_index_length = 8 // not used
0N/A };
0N/A
0N/A // Derived constants used for manipulating the index bitfields
0N/A enum {
0N/A bit_index_offset = 0, // not used
0N/A word_index_offset = bit_index_length,
0N/A block_index_offset = bit_index_length + word_index_length,
0N/A
0N/A bits_per_word = 1 << bit_index_length,
0N/A words_per_block = 1 << word_index_length,
0N/A bits_per_block = bits_per_word * words_per_block,
0N/A
0N/A bit_index_mask = right_n_bits(bit_index_length),
0N/A word_index_mask = right_n_bits(word_index_length)
0N/A };
0N/A
0N/A // These routines are used for extracting the block, word, and bit index
0N/A // from an element.
0N/A static uint get_block_index(uint element) {
0N/A return element >> block_index_offset;
0N/A }
0N/A static uint get_word_index(uint element) {
0N/A return mask_bits(element >> word_index_offset,word_index_mask);
0N/A }
0N/A static uint get_bit_index(uint element) {
0N/A return mask_bits(element,bit_index_mask);
0N/A }
0N/A
0N/A //------------------------------ class BitBlock ----------------------------
0N/A // The BitBlock class is a segment of a bitvector set.
0N/A
0N/A class BitBlock : public ResourceObj {
0N/A friend class IndexSetIterator;
0N/A friend class IndexSet;
0N/A
0N/A private:
0N/A // All of BitBlocks fields and methods are declared private. We limit
0N/A // access to IndexSet and IndexSetIterator.
0N/A
0N/A // A BitBlock is composed of some number of 32 bit words. When a BitBlock
0N/A // is not in use by any IndexSet, it is stored on a free list. The next field
0N/A // is used by IndexSet to mainting this free list.
0N/A
0N/A union {
0N/A uint32 _words[words_per_block];
0N/A BitBlock *_next;
0N/A } _data;
0N/A
0N/A // accessors
0N/A uint32 *words() { return _data._words; }
0N/A void set_next(BitBlock *next) { _data._next = next; }
0N/A BitBlock *next() { return _data._next; }
0N/A
0N/A // Operations. A BitBlock supports four simple operations,
0N/A // clear(), member(), insert(), and remove(). These methods do
0N/A // not assume that the block index has been masked out.
0N/A
0N/A void clear() {
0N/A memset(words(), 0, sizeof(uint32) * words_per_block);
0N/A }
0N/A
0N/A bool member(uint element) {
0N/A uint word_index = IndexSet::get_word_index(element);
0N/A uint bit_index = IndexSet::get_bit_index(element);
0N/A
0N/A return ((words()[word_index] & (uint32)(0x1 << bit_index)) != 0);
0N/A }
0N/A
0N/A bool insert(uint element) {
0N/A uint word_index = IndexSet::get_word_index(element);
0N/A uint bit_index = IndexSet::get_bit_index(element);
0N/A
0N/A uint32 bit = (0x1 << bit_index);
0N/A uint32 before = words()[word_index];
0N/A words()[word_index] = before | bit;
0N/A return ((before & bit) != 0);
0N/A }
0N/A
0N/A bool remove(uint element) {
0N/A uint word_index = IndexSet::get_word_index(element);
0N/A uint bit_index = IndexSet::get_bit_index(element);
0N/A
0N/A uint32 bit = (0x1 << bit_index);
0N/A uint32 before = words()[word_index];
0N/A words()[word_index] = before & ~bit;
0N/A return ((before & bit) != 0);
0N/A }
0N/A };
0N/A
0N/A //-------------------------- BitBlock allocation ---------------------------
0N/A private:
0N/A
0N/A // All IndexSets share an arena from which they allocate BitBlocks. Unused
0N/A // BitBlocks are placed on a free list.
0N/A
0N/A // The number of BitBlocks to allocate at a time
0N/A enum { bitblock_alloc_chunk_size = 50 };
0N/A
0N/A static Arena *arena() { return Compile::current()->indexSet_arena(); }
0N/A
0N/A static void populate_free_list();
0N/A
0N/A public:
0N/A
0N/A // Invalidate the current free BitBlock list and begin allocation
0N/A // from a new arena. It is essential that this method is called whenever
0N/A // the Arena being used for BitBlock allocation is reset.
0N/A static void reset_memory(Compile* compile, Arena *arena) {
0N/A compile->set_indexSet_free_block_list(NULL);
0N/A compile->set_indexSet_arena(arena);
0N/A
0N/A // This should probably be done in a static initializer
0N/A _empty_block.clear();
0N/A }
0N/A
0N/A private:
0N/A friend class BitBlock;
0N/A // A distinguished BitBlock which always remains empty. When a new IndexSet is
0N/A // created, all of its top level BitBlock pointers are initialized to point to
0N/A // this.
0N/A static BitBlock _empty_block;
0N/A
0N/A //-------------------------- Members ------------------------------------------
0N/A
0N/A // The number of elements in the set
0N/A uint _count;
0N/A
0N/A // Our top level array of bitvector segments
0N/A BitBlock **_blocks;
0N/A
0N/A BitBlock *_preallocated_block_list[preallocated_block_list_size];
0N/A
0N/A // The number of top level array entries in use
0N/A uint _max_blocks;
0N/A
0N/A // Our assertions need to know the maximum number allowed in the set
0N/A#ifdef ASSERT
0N/A uint _max_elements;
0N/A#endif
0N/A
0N/A // The next IndexSet on the free list (not used at same time as count)
0N/A IndexSet *_next;
0N/A
0N/A public:
0N/A //-------------------------- Free list operations ------------------------------
0N/A // Individual IndexSets can be placed on a free list. This is done in PhaseLive.
0N/A
0N/A IndexSet *next() {
0N/A#ifdef ASSERT
0N/A if( VerifyOpto ) {
0N/A check_watch("removed from free list?", ((_next == NULL) ? 0 : _next->_serial_number));
0N/A }
0N/A#endif
0N/A return _next;
0N/A }
0N/A
0N/A void set_next(IndexSet *next) {
0N/A#ifdef ASSERT
0N/A if( VerifyOpto ) {
0N/A check_watch("put on free list?", ((next == NULL) ? 0 : next->_serial_number));
0N/A }
0N/A#endif
0N/A _next = next;
0N/A }
0N/A
0N/A private:
0N/A //-------------------------- Utility methods -----------------------------------
0N/A
0N/A // Get the block which holds element
0N/A BitBlock *get_block_containing(uint element) const {
0N/A assert(element < _max_elements, "element out of bounds");
0N/A return _blocks[get_block_index(element)];
0N/A }
0N/A
0N/A // Set a block in the top level array
0N/A void set_block(uint index, BitBlock *block) {
0N/A#ifdef ASSERT
0N/A if( VerifyOpto )
0N/A check_watch("set block", index);
0N/A#endif
0N/A _blocks[index] = block;
0N/A }
0N/A
0N/A // Get a BitBlock from the free list
0N/A BitBlock *alloc_block();
0N/A
0N/A // Get a BitBlock from the free list and place it in the top level array
0N/A BitBlock *alloc_block_containing(uint element);
0N/A
0N/A // Free a block from the top level array, placing it on the free BitBlock list
0N/A void free_block(uint i);
0N/A
0N/A public:
0N/A //-------------------------- Primitive set operations --------------------------
0N/A
0N/A void clear() {
0N/A#ifdef ASSERT
0N/A if( VerifyOpto )
0N/A check_watch("clear");
0N/A#endif
0N/A _count = 0;
0N/A for (uint i = 0; i < _max_blocks; i++) {
0N/A BitBlock *block = _blocks[i];
0N/A if (block != &_empty_block) {
0N/A free_block(i);
0N/A }
0N/A }
0N/A }
0N/A
0N/A uint count() const { return _count; }
0N/A
0N/A bool is_empty() const { return _count == 0; }
0N/A
0N/A bool member(uint element) const {
0N/A return get_block_containing(element)->member(element);
0N/A }
0N/A
0N/A bool insert(uint element) {
0N/A#ifdef ASSERT
0N/A if( VerifyOpto )
0N/A check_watch("insert", element);
0N/A#endif
0N/A if (element == 0) {
0N/A return 0;
0N/A }
0N/A BitBlock *block = get_block_containing(element);
0N/A if (block == &_empty_block) {
0N/A block = alloc_block_containing(element);
0N/A }
0N/A bool present = block->insert(element);
0N/A if (!present) {
0N/A _count++;
0N/A }
0N/A return !present;
0N/A }
0N/A
0N/A bool remove(uint element) {
0N/A#ifdef ASSERT
0N/A if( VerifyOpto )
0N/A check_watch("remove", element);
0N/A#endif
0N/A
0N/A BitBlock *block = get_block_containing(element);
0N/A bool present = block->remove(element);
0N/A if (present) {
0N/A _count--;
0N/A }
0N/A return present;
0N/A }
0N/A
0N/A //-------------------------- Compound set operations ------------------------
0N/A // Compute the union of all elements of one and two which interfere
0N/A // with the RegMask mask. If the degree of the union becomes
0N/A // exceeds fail_degree, the union bails out. The underlying set is
0N/A // cleared before the union is performed.
0N/A uint lrg_union(uint lr1, uint lr2,
0N/A const uint fail_degree,
0N/A const class PhaseIFG *ifg,
0N/A const RegMask &mask);
0N/A
0N/A
0N/A //------------------------- Construction, initialization -----------------------
0N/A
0N/A IndexSet() {}
0N/A
0N/A // This constructor is used for making a deep copy of a IndexSet.
0N/A IndexSet(IndexSet *set);
0N/A
0N/A // Perform initialization on a IndexSet
0N/A void initialize(uint max_element);
0N/A
0N/A // Initialize a IndexSet. If the top level BitBlock array needs to be
0N/A // allocated, do it from the proffered arena. BitBlocks are still allocated
0N/A // from the static Arena member.
0N/A void initialize(uint max_element, Arena *arena);
0N/A
0N/A // Exchange two sets
0N/A void swap(IndexSet *set);
0N/A
0N/A //-------------------------- Debugging and statistics --------------------------
0N/A
0N/A#ifndef PRODUCT
0N/A // Output a IndexSet for debugging
0N/A void dump() const;
0N/A#endif
0N/A
0N/A#ifdef ASSERT
0N/A void tally_iteration_statistics() const;
0N/A
0N/A // BitBlock allocation statistics
2122N/A static julong _alloc_new;
2122N/A static julong _alloc_total;
0N/A
0N/A // Block density statistics
2122N/A static julong _total_bits;
2122N/A static julong _total_used_blocks;
2122N/A static julong _total_unused_blocks;
0N/A
0N/A // Sanity tests
0N/A void verify() const;
0N/A
0N/A static int _serial_count;
0N/A int _serial_number;
0N/A
0N/A // Check to see if the serial number of the current set is the one we're tracing.
0N/A // If it is, print a message.
0N/A void check_watch(const char *operation, uint operand) const {
0N/A if (IndexSetWatch != 0) {
0N/A if (IndexSetWatch == -1 || _serial_number == IndexSetWatch) {
0N/A tty->print_cr("IndexSet %d : %s ( %d )", _serial_number, operation, operand);
0N/A }
0N/A }
0N/A }
0N/A void check_watch(const char *operation) const {
0N/A if (IndexSetWatch != 0) {
0N/A if (IndexSetWatch == -1 || _serial_number == IndexSetWatch) {
0N/A tty->print_cr("IndexSet %d : %s", _serial_number, operation);
0N/A }
0N/A }
0N/A }
0N/A
0N/A public:
0N/A static void print_statistics();
0N/A
0N/A#endif
0N/A};
0N/A
0N/A
0N/A//-------------------------------- class IndexSetIterator --------------------
0N/A// An iterator for IndexSets.
0N/A
0N/Aclass IndexSetIterator VALUE_OBJ_CLASS_SPEC {
0N/A friend class IndexSet;
0N/A
0N/A public:
0N/A
0N/A // We walk over the bits in a word in chunks of size window_size.
0N/A enum { window_size = 5,
0N/A window_mask = right_n_bits(window_size),
0N/A table_size = (1 << window_size) };
0N/A
0N/A // For an integer of length window_size, what is the first set bit?
0N/A static const byte _first_bit[table_size];
0N/A
0N/A // For an integer of length window_size, what is the second set bit?
0N/A static const byte _second_bit[table_size];
0N/A
0N/A private:
0N/A // The current word we are inspecting
0N/A uint32 _current;
0N/A
0N/A // What element number are we currently on?
0N/A uint _value;
0N/A
0N/A // The index of the next word we will inspect
0N/A uint _next_word;
0N/A
0N/A // A pointer to the contents of the current block
0N/A uint32 *_words;
0N/A
0N/A // The index of the next block we will inspect
0N/A uint _next_block;
0N/A
0N/A // A pointer to the blocks in our set
0N/A IndexSet::BitBlock **_blocks;
0N/A
0N/A // The number of blocks in the set
0N/A uint _max_blocks;
0N/A
0N/A // If the iterator was created from a non-const set, we replace
0N/A // non-canonical empty blocks with the _empty_block pointer. If
0N/A // _set is NULL, we do no replacement.
0N/A IndexSet *_set;
0N/A
0N/A // Advance to the next non-empty word and return the next
0N/A // element in the set.
0N/A uint advance_and_next();
0N/A
0N/A
0N/A public:
0N/A
0N/A // If an iterator is built from a constant set then empty blocks
0N/A // are not canonicalized.
0N/A IndexSetIterator(IndexSet *set);
0N/A IndexSetIterator(const IndexSet *set);
0N/A
0N/A // Return the next element of the set. Return 0 when done.
0N/A uint next() {
0N/A uint current = _current;
0N/A if (current != 0) {
0N/A uint value = _value;
0N/A while (mask_bits(current,window_mask) == 0) {
0N/A current >>= window_size;
0N/A value += window_size;
0N/A }
0N/A
0N/A uint advance = _second_bit[mask_bits(current,window_mask)];
0N/A _current = current >> advance;
0N/A _value = value + advance;
0N/A return value + _first_bit[mask_bits(current,window_mask)];
0N/A } else {
0N/A return advance_and_next();
0N/A }
0N/A }
0N/A};
1879N/A
1879N/A#endif // SHARE_VM_OPTO_INDEXSET_HPP