aoqi@0: /* aoqi@0: * Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #include "precompiled.hpp" aoqi@0: #include "memory/allocation.inline.hpp" aoqi@0: #include "opto/chaitin.hpp" aoqi@0: #include "opto/compile.hpp" aoqi@0: #include "opto/indexSet.hpp" aoqi@0: #include "opto/regmask.hpp" aoqi@0: aoqi@0: // This file defines the IndexSet class, a set of sparse integer indices. aoqi@0: // This data structure is used by the compiler in its liveness analysis and aoqi@0: // during register allocation. It also defines an iterator for this class. aoqi@0: aoqi@0: //-------------------------------- Initializations ------------------------------ aoqi@0: aoqi@0: IndexSet::BitBlock IndexSet::_empty_block = IndexSet::BitBlock(); aoqi@0: aoqi@0: #ifdef ASSERT aoqi@0: // Initialize statistics counters aoqi@0: julong IndexSet::_alloc_new = 0; aoqi@0: julong IndexSet::_alloc_total = 0; aoqi@0: aoqi@0: julong IndexSet::_total_bits = 0; aoqi@0: julong IndexSet::_total_used_blocks = 0; aoqi@0: julong IndexSet::_total_unused_blocks = 0; aoqi@0: aoqi@0: // Per set, or all sets operation tracing aoqi@0: int IndexSet::_serial_count = 1; aoqi@0: #endif aoqi@0: aoqi@0: // What is the first set bit in a 5 bit integer? aoqi@0: const byte IndexSetIterator::_first_bit[32] = { aoqi@0: 0, 0, 1, 0, aoqi@0: 2, 0, 1, 0, aoqi@0: 3, 0, 1, 0, aoqi@0: 2, 0, 1, 0, aoqi@0: 4, 0, 1, 0, aoqi@0: 2, 0, 1, 0, aoqi@0: 3, 0, 1, 0, aoqi@0: 2, 0, 1, 0 aoqi@0: }; aoqi@0: aoqi@0: // What is the second set bit in a 5 bit integer? aoqi@0: const byte IndexSetIterator::_second_bit[32] = { aoqi@0: 5, 5, 5, 1, aoqi@0: 5, 2, 2, 1, aoqi@0: 5, 3, 3, 1, aoqi@0: 3, 2, 2, 1, aoqi@0: 5, 4, 4, 1, aoqi@0: 4, 2, 2, 1, aoqi@0: 4, 3, 3, 1, aoqi@0: 3, 2, 2, 1 aoqi@0: }; aoqi@0: aoqi@0: // I tried implementing the IndexSetIterator with a window_size of 8 and aoqi@0: // didn't seem to get a noticeable speedup. I am leaving in the tables aoqi@0: // in case we want to switch back. aoqi@0: aoqi@0: /*const byte IndexSetIterator::_first_bit[256] = { aoqi@0: 8, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, aoqi@0: 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, aoqi@0: 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, aoqi@0: 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, aoqi@0: 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, aoqi@0: 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, aoqi@0: 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, aoqi@0: 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, aoqi@0: 7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, aoqi@0: 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, aoqi@0: 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, aoqi@0: 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, aoqi@0: 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, aoqi@0: 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, aoqi@0: 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, aoqi@0: 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0 aoqi@0: }; aoqi@0: aoqi@0: const byte IndexSetIterator::_second_bit[256] = { aoqi@0: 8, 8, 8, 1, 8, 2, 2, 1, 8, 3, 3, 1, 3, 2, 2, 1, aoqi@0: 8, 4, 4, 1, 4, 2, 2, 1, 4, 3, 3, 1, 3, 2, 2, 1, aoqi@0: 8, 5, 5, 1, 5, 2, 2, 1, 5, 3, 3, 1, 3, 2, 2, 1, aoqi@0: 5, 4, 4, 1, 4, 2, 2, 1, 4, 3, 3, 1, 3, 2, 2, 1, aoqi@0: 8, 6, 6, 1, 6, 2, 2, 1, 6, 3, 3, 1, 3, 2, 2, 1, aoqi@0: 6, 4, 4, 1, 4, 2, 2, 1, 4, 3, 3, 1, 3, 2, 2, 1, aoqi@0: 6, 5, 5, 1, 5, 2, 2, 1, 5, 3, 3, 1, 3, 2, 2, 1, aoqi@0: 5, 4, 4, 1, 4, 2, 2, 1, 4, 3, 3, 1, 3, 2, 2, 1, aoqi@0: 8, 7, 7, 1, 7, 2, 2, 1, 7, 3, 3, 1, 3, 2, 2, 1, aoqi@0: 7, 4, 4, 1, 4, 2, 2, 1, 4, 3, 3, 1, 3, 2, 2, 1, aoqi@0: 7, 5, 5, 1, 5, 2, 2, 1, 5, 3, 3, 1, 3, 2, 2, 1, aoqi@0: 5, 4, 4, 1, 4, 2, 2, 1, 4, 3, 3, 1, 3, 2, 2, 1, aoqi@0: 7, 6, 6, 1, 6, 2, 2, 1, 6, 3, 3, 1, 3, 2, 2, 1, aoqi@0: 6, 4, 4, 1, 4, 2, 2, 1, 4, 3, 3, 1, 3, 2, 2, 1, aoqi@0: 6, 5, 5, 1, 5, 2, 2, 1, 5, 3, 3, 1, 3, 2, 2, 1, aoqi@0: 5, 4, 4, 1, 4, 2, 2, 1, 4, 3, 3, 1, 3, 2, 2, 1 aoqi@0: };*/ aoqi@0: aoqi@0: //---------------------------- IndexSet::populate_free_list() ----------------------------- aoqi@0: // Populate the free BitBlock list with a batch of BitBlocks. The BitBlocks aoqi@0: // are 32 bit aligned. aoqi@0: aoqi@0: void IndexSet::populate_free_list() { aoqi@0: Compile *compile = Compile::current(); aoqi@0: BitBlock *free = (BitBlock*)compile->indexSet_free_block_list(); aoqi@0: aoqi@0: char *mem = (char*)arena()->Amalloc_4(sizeof(BitBlock) * aoqi@0: bitblock_alloc_chunk_size + 32); aoqi@0: aoqi@0: // Align the pointer to a 32 bit boundary. aoqi@0: BitBlock *new_blocks = (BitBlock*)(((uintptr_t)mem + 32) & ~0x001F); aoqi@0: aoqi@0: // Add the new blocks to the free list. aoqi@0: for (int i = 0; i < bitblock_alloc_chunk_size; i++) { aoqi@0: new_blocks->set_next(free); aoqi@0: free = new_blocks; aoqi@0: new_blocks++; aoqi@0: } aoqi@0: aoqi@0: compile->set_indexSet_free_block_list(free); aoqi@0: aoqi@0: #ifdef ASSERT aoqi@0: if (CollectIndexSetStatistics) { aoqi@0: inc_stat_counter(&_alloc_new, bitblock_alloc_chunk_size); aoqi@0: } aoqi@0: #endif aoqi@0: } aoqi@0: aoqi@0: aoqi@0: //---------------------------- IndexSet::alloc_block() ------------------------ aoqi@0: // Allocate a BitBlock from the free list. If the free list is empty, aoqi@0: // prime it. aoqi@0: aoqi@0: IndexSet::BitBlock *IndexSet::alloc_block() { aoqi@0: #ifdef ASSERT aoqi@0: if (CollectIndexSetStatistics) { aoqi@0: inc_stat_counter(&_alloc_total, 1); aoqi@0: } aoqi@0: #endif aoqi@0: Compile *compile = Compile::current(); aoqi@0: BitBlock* free_list = (BitBlock*)compile->indexSet_free_block_list(); aoqi@0: if (free_list == NULL) { aoqi@0: populate_free_list(); aoqi@0: free_list = (BitBlock*)compile->indexSet_free_block_list(); aoqi@0: } aoqi@0: BitBlock *block = free_list; aoqi@0: compile->set_indexSet_free_block_list(block->next()); aoqi@0: aoqi@0: block->clear(); aoqi@0: return block; aoqi@0: } aoqi@0: aoqi@0: //---------------------------- IndexSet::alloc_block_containing() ------------- aoqi@0: // Allocate a new BitBlock and put it into the position in the _blocks array aoqi@0: // corresponding to element. aoqi@0: aoqi@0: IndexSet::BitBlock *IndexSet::alloc_block_containing(uint element) { aoqi@0: BitBlock *block = alloc_block(); aoqi@0: uint bi = get_block_index(element); aoqi@0: _blocks[bi] = block; aoqi@0: return block; aoqi@0: } aoqi@0: aoqi@0: //---------------------------- IndexSet::free_block() ------------------------- aoqi@0: // Add a BitBlock to the free list. aoqi@0: aoqi@0: void IndexSet::free_block(uint i) { aoqi@0: debug_only(check_watch("free block", i)); aoqi@0: assert(i < _max_blocks, "block index too large"); aoqi@0: BitBlock *block = _blocks[i]; aoqi@0: assert(block != &_empty_block, "cannot free the empty block"); aoqi@0: block->set_next((IndexSet::BitBlock*)Compile::current()->indexSet_free_block_list()); aoqi@0: Compile::current()->set_indexSet_free_block_list(block); aoqi@0: set_block(i,&_empty_block); aoqi@0: } aoqi@0: aoqi@0: //------------------------------lrg_union-------------------------------------- aoqi@0: // Compute the union of all elements of one and two which interfere with aoqi@0: // the RegMask mask. If the degree of the union becomes exceeds aoqi@0: // fail_degree, the union bails out. The underlying set is cleared before aoqi@0: // the union is performed. aoqi@0: aoqi@0: uint IndexSet::lrg_union(uint lr1, uint lr2, aoqi@0: const uint fail_degree, aoqi@0: const PhaseIFG *ifg, aoqi@0: const RegMask &mask ) { aoqi@0: IndexSet *one = ifg->neighbors(lr1); aoqi@0: IndexSet *two = ifg->neighbors(lr2); aoqi@0: LRG &lrg1 = ifg->lrgs(lr1); aoqi@0: LRG &lrg2 = ifg->lrgs(lr2); aoqi@0: #ifdef ASSERT aoqi@0: assert(_max_elements == one->_max_elements, "max element mismatch"); aoqi@0: check_watch("union destination"); aoqi@0: one->check_watch("union source"); aoqi@0: two->check_watch("union source"); aoqi@0: #endif aoqi@0: aoqi@0: // Compute the degree of the combined live-range. The combined aoqi@0: // live-range has the union of the original live-ranges' neighbors set as aoqi@0: // well as the neighbors of all intermediate copies, minus those neighbors aoqi@0: // that can not use the intersected allowed-register-set. aoqi@0: aoqi@0: // Copy the larger set. Insert the smaller set into the larger. aoqi@0: if (two->count() > one->count()) { aoqi@0: IndexSet *temp = one; aoqi@0: one = two; aoqi@0: two = temp; aoqi@0: } aoqi@0: aoqi@0: clear(); aoqi@0: aoqi@0: // Used to compute degree of register-only interferences. Infinite-stack aoqi@0: // neighbors do not alter colorability, as they can always color to some aoqi@0: // other color. (A variant of the Briggs assertion) aoqi@0: uint reg_degree = 0; aoqi@0: aoqi@0: uint element; aoqi@0: // Load up the combined interference set with the neighbors of one aoqi@0: IndexSetIterator elements(one); aoqi@0: while ((element = elements.next()) != 0) { aoqi@0: LRG &lrg = ifg->lrgs(element); aoqi@0: if (mask.overlap(lrg.mask())) { aoqi@0: insert(element); aoqi@0: if( !lrg.mask().is_AllStack() ) { aoqi@0: reg_degree += lrg1.compute_degree(lrg); aoqi@0: if( reg_degree >= fail_degree ) return reg_degree; aoqi@0: } else { aoqi@0: // !!!!! Danger! No update to reg_degree despite having a neighbor. aoqi@0: // A variant of the Briggs assertion. aoqi@0: // Not needed if I simplify during coalesce, ala George/Appel. aoqi@0: assert( lrg.lo_degree(), "" ); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: // Add neighbors of two as well aoqi@0: IndexSetIterator elements2(two); aoqi@0: while ((element = elements2.next()) != 0) { aoqi@0: LRG &lrg = ifg->lrgs(element); aoqi@0: if (mask.overlap(lrg.mask())) { aoqi@0: if (insert(element)) { aoqi@0: if( !lrg.mask().is_AllStack() ) { aoqi@0: reg_degree += lrg2.compute_degree(lrg); aoqi@0: if( reg_degree >= fail_degree ) return reg_degree; aoqi@0: } else { aoqi@0: // !!!!! Danger! No update to reg_degree despite having a neighbor. aoqi@0: // A variant of the Briggs assertion. aoqi@0: // Not needed if I simplify during coalesce, ala George/Appel. aoqi@0: assert( lrg.lo_degree(), "" ); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: return reg_degree; aoqi@0: } aoqi@0: aoqi@0: //---------------------------- IndexSet() ----------------------------- aoqi@0: // A deep copy constructor. This is used when you need a scratch copy of this set. aoqi@0: aoqi@0: IndexSet::IndexSet (IndexSet *set) { aoqi@0: #ifdef ASSERT aoqi@0: _serial_number = _serial_count++; aoqi@0: set->check_watch("copied", _serial_number); aoqi@0: check_watch("initialized by copy", set->_serial_number); aoqi@0: _max_elements = set->_max_elements; aoqi@0: #endif aoqi@0: _count = set->_count; aoqi@0: _max_blocks = set->_max_blocks; aoqi@0: if (_max_blocks <= preallocated_block_list_size) { aoqi@0: _blocks = _preallocated_block_list; aoqi@0: } else { aoqi@0: _blocks = aoqi@0: (IndexSet::BitBlock**) arena()->Amalloc_4(sizeof(IndexSet::BitBlock**) * _max_blocks); aoqi@0: } aoqi@0: for (uint i = 0; i < _max_blocks; i++) { aoqi@0: BitBlock *block = set->_blocks[i]; aoqi@0: if (block == &_empty_block) { aoqi@0: set_block(i, &_empty_block); aoqi@0: } else { aoqi@0: BitBlock *new_block = alloc_block(); aoqi@0: memcpy(new_block->words(), block->words(), sizeof(uint32) * words_per_block); aoqi@0: set_block(i, new_block); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: //---------------------------- IndexSet::initialize() ----------------------------- aoqi@0: // Prepare an IndexSet for use. aoqi@0: aoqi@0: void IndexSet::initialize(uint max_elements) { aoqi@0: #ifdef ASSERT aoqi@0: _serial_number = _serial_count++; aoqi@0: check_watch("initialized", max_elements); aoqi@0: _max_elements = max_elements; aoqi@0: #endif aoqi@0: _count = 0; aoqi@0: _max_blocks = (max_elements + bits_per_block - 1) / bits_per_block; aoqi@0: aoqi@0: if (_max_blocks <= preallocated_block_list_size) { aoqi@0: _blocks = _preallocated_block_list; aoqi@0: } else { aoqi@0: _blocks = (IndexSet::BitBlock**) arena()->Amalloc_4(sizeof(IndexSet::BitBlock**) * _max_blocks); aoqi@0: } aoqi@0: for (uint i = 0; i < _max_blocks; i++) { aoqi@0: set_block(i, &_empty_block); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: //---------------------------- IndexSet::initialize()------------------------------ aoqi@0: // Prepare an IndexSet for use. If it needs to allocate its _blocks array, it does aoqi@0: // so from the Arena passed as a parameter. BitBlock allocation is still done from aoqi@0: // the static Arena which was set with reset_memory(). aoqi@0: aoqi@0: void IndexSet::initialize(uint max_elements, Arena *arena) { aoqi@0: #ifdef ASSERT aoqi@0: _serial_number = _serial_count++; aoqi@0: check_watch("initialized2", max_elements); aoqi@0: _max_elements = max_elements; aoqi@0: #endif // ASSERT aoqi@0: _count = 0; aoqi@0: _max_blocks = (max_elements + bits_per_block - 1) / bits_per_block; aoqi@0: aoqi@0: if (_max_blocks <= preallocated_block_list_size) { aoqi@0: _blocks = _preallocated_block_list; aoqi@0: } else { aoqi@0: _blocks = (IndexSet::BitBlock**) arena->Amalloc_4(sizeof(IndexSet::BitBlock**) * _max_blocks); aoqi@0: } aoqi@0: for (uint i = 0; i < _max_blocks; i++) { aoqi@0: set_block(i, &_empty_block); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: //---------------------------- IndexSet::swap() ----------------------------- aoqi@0: // Exchange two IndexSets. aoqi@0: aoqi@0: void IndexSet::swap(IndexSet *set) { aoqi@0: #ifdef ASSERT aoqi@0: assert(_max_elements == set->_max_elements, "must have same universe size to swap"); aoqi@0: check_watch("swap", set->_serial_number); aoqi@0: set->check_watch("swap", _serial_number); aoqi@0: #endif aoqi@0: aoqi@0: for (uint i = 0; i < _max_blocks; i++) { aoqi@0: BitBlock *temp = _blocks[i]; aoqi@0: set_block(i, set->_blocks[i]); aoqi@0: set->set_block(i, temp); aoqi@0: } aoqi@0: uint temp = _count; aoqi@0: _count = set->_count; aoqi@0: set->_count = temp; aoqi@0: } aoqi@0: aoqi@0: //---------------------------- IndexSet::dump() ----------------------------- aoqi@0: // Print this set. Used for debugging. aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: void IndexSet::dump() const { aoqi@0: IndexSetIterator elements(this); aoqi@0: aoqi@0: tty->print("{"); aoqi@0: uint i; aoqi@0: while ((i = elements.next()) != 0) { aoqi@0: tty->print("L%d ", i); aoqi@0: } aoqi@0: tty->print_cr("}"); aoqi@0: } aoqi@0: #endif aoqi@0: aoqi@0: #ifdef ASSERT aoqi@0: //---------------------------- IndexSet::tally_iteration_statistics() ----------------------------- aoqi@0: // Update block/bit counts to reflect that this set has been iterated over. aoqi@0: aoqi@0: void IndexSet::tally_iteration_statistics() const { aoqi@0: inc_stat_counter(&_total_bits, count()); aoqi@0: aoqi@0: for (uint i = 0; i < _max_blocks; i++) { aoqi@0: if (_blocks[i] != &_empty_block) { aoqi@0: inc_stat_counter(&_total_used_blocks, 1); aoqi@0: } else { aoqi@0: inc_stat_counter(&_total_unused_blocks, 1); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: //---------------------------- IndexSet::print_statistics() ----------------------------- aoqi@0: // Print statistics about IndexSet usage. aoqi@0: aoqi@0: void IndexSet::print_statistics() { aoqi@0: julong total_blocks = _total_used_blocks + _total_unused_blocks; aoqi@0: tty->print_cr ("Accumulated IndexSet usage statistics:"); aoqi@0: tty->print_cr ("--------------------------------------"); aoqi@0: tty->print_cr (" Iteration:"); aoqi@0: tty->print_cr (" blocks visited: " UINT64_FORMAT, total_blocks); aoqi@0: tty->print_cr (" blocks empty: %4.2f%%", 100.0*(double)_total_unused_blocks/total_blocks); aoqi@0: tty->print_cr (" bit density (bits/used blocks): %4.2f", (double)_total_bits/_total_used_blocks); aoqi@0: tty->print_cr (" bit density (bits/all blocks): %4.2f", (double)_total_bits/total_blocks); aoqi@0: tty->print_cr (" Allocation:"); aoqi@0: tty->print_cr (" blocks allocated: " UINT64_FORMAT, _alloc_new); aoqi@0: tty->print_cr (" blocks used/reused: " UINT64_FORMAT, _alloc_total); aoqi@0: } aoqi@0: aoqi@0: //---------------------------- IndexSet::verify() ----------------------------- aoqi@0: // Expensive test of IndexSet sanity. Ensure that the count agrees with the aoqi@0: // number of bits in the blocks. Make sure the iterator is seeing all elements aoqi@0: // of the set. Meant for use during development. aoqi@0: aoqi@0: void IndexSet::verify() const { aoqi@0: assert(!member(0), "zero cannot be a member"); aoqi@0: uint count = 0; aoqi@0: uint i; aoqi@0: for (i = 1; i < _max_elements; i++) { aoqi@0: if (member(i)) { aoqi@0: count++; aoqi@0: assert(count <= _count, "_count is messed up"); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: IndexSetIterator elements(this); aoqi@0: count = 0; aoqi@0: while ((i = elements.next()) != 0) { aoqi@0: count++; aoqi@0: assert(member(i), "returned a non member"); aoqi@0: assert(count <= _count, "iterator returned wrong number of elements"); aoqi@0: } aoqi@0: } aoqi@0: #endif aoqi@0: aoqi@0: //---------------------------- IndexSetIterator() ----------------------------- aoqi@0: // Create an iterator for a set. If empty blocks are detected when iterating aoqi@0: // over the set, these blocks are replaced. aoqi@0: aoqi@0: IndexSetIterator::IndexSetIterator(IndexSet *set) { aoqi@0: #ifdef ASSERT aoqi@0: if (CollectIndexSetStatistics) { aoqi@0: set->tally_iteration_statistics(); aoqi@0: } aoqi@0: set->check_watch("traversed", set->count()); aoqi@0: #endif aoqi@0: if (set->is_empty()) { aoqi@0: _current = 0; aoqi@0: _next_word = IndexSet::words_per_block; aoqi@0: _next_block = 1; aoqi@0: _max_blocks = 1; aoqi@0: aoqi@0: // We don't need the following values when we iterate over an empty set. aoqi@0: // The commented out code is left here to document that the omission aoqi@0: // is intentional. aoqi@0: // aoqi@0: //_value = 0; aoqi@0: //_words = NULL; aoqi@0: //_blocks = NULL; aoqi@0: //_set = NULL; aoqi@0: } else { aoqi@0: _current = 0; aoqi@0: _value = 0; aoqi@0: _next_block = 0; aoqi@0: _next_word = IndexSet::words_per_block; aoqi@0: aoqi@0: _max_blocks = set->_max_blocks; aoqi@0: _words = NULL; aoqi@0: _blocks = set->_blocks; aoqi@0: _set = set; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: //---------------------------- IndexSetIterator(const) ----------------------------- aoqi@0: // Iterate over a constant IndexSet. aoqi@0: aoqi@0: IndexSetIterator::IndexSetIterator(const IndexSet *set) { aoqi@0: #ifdef ASSERT aoqi@0: if (CollectIndexSetStatistics) { aoqi@0: set->tally_iteration_statistics(); aoqi@0: } aoqi@0: // We don't call check_watch from here to avoid bad recursion. aoqi@0: // set->check_watch("traversed const", set->count()); aoqi@0: #endif aoqi@0: if (set->is_empty()) { aoqi@0: _current = 0; aoqi@0: _next_word = IndexSet::words_per_block; aoqi@0: _next_block = 1; aoqi@0: _max_blocks = 1; aoqi@0: aoqi@0: // We don't need the following values when we iterate over an empty set. aoqi@0: // The commented out code is left here to document that the omission aoqi@0: // is intentional. aoqi@0: // aoqi@0: //_value = 0; aoqi@0: //_words = NULL; aoqi@0: //_blocks = NULL; aoqi@0: //_set = NULL; aoqi@0: } else { aoqi@0: _current = 0; aoqi@0: _value = 0; aoqi@0: _next_block = 0; aoqi@0: _next_word = IndexSet::words_per_block; aoqi@0: aoqi@0: _max_blocks = set->_max_blocks; aoqi@0: _words = NULL; aoqi@0: _blocks = set->_blocks; aoqi@0: _set = NULL; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: //---------------------------- List16Iterator::advance_and_next() ----------------------------- aoqi@0: // Advance to the next non-empty word in the set being iterated over. Return the next element aoqi@0: // if there is one. If we are done, return 0. This method is called from the next() method aoqi@0: // when it gets done with a word. aoqi@0: aoqi@0: uint IndexSetIterator::advance_and_next() { aoqi@0: // See if there is another non-empty word in the current block. aoqi@0: for (uint wi = _next_word; wi < (unsigned)IndexSet::words_per_block; wi++) { aoqi@0: if (_words[wi] != 0) { aoqi@0: // Found a non-empty word. aoqi@0: _value = ((_next_block - 1) * IndexSet::bits_per_block) + (wi * IndexSet::bits_per_word); aoqi@0: _current = _words[wi]; aoqi@0: aoqi@0: _next_word = wi+1; aoqi@0: aoqi@0: return next(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // We ran out of words in the current block. Advance to next non-empty block. aoqi@0: for (uint bi = _next_block; bi < _max_blocks; bi++) { aoqi@0: if (_blocks[bi] != &IndexSet::_empty_block) { aoqi@0: // Found a non-empty block. aoqi@0: aoqi@0: _words = _blocks[bi]->words(); aoqi@0: for (uint wi = 0; wi < (unsigned)IndexSet::words_per_block; wi++) { aoqi@0: if (_words[wi] != 0) { aoqi@0: // Found a non-empty word. aoqi@0: _value = (bi * IndexSet::bits_per_block) + (wi * IndexSet::bits_per_word); aoqi@0: _current = _words[wi]; aoqi@0: aoqi@0: _next_block = bi+1; aoqi@0: _next_word = wi+1; aoqi@0: aoqi@0: return next(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // All of the words in the block were empty. Replace aoqi@0: // the block with the empty block. aoqi@0: if (_set) { aoqi@0: _set->free_block(bi); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // These assignments make redundant calls to next on a finished iterator aoqi@0: // faster. Probably not necessary. aoqi@0: _next_block = _max_blocks; aoqi@0: _next_word = IndexSet::words_per_block; aoqi@0: aoqi@0: // No more words. aoqi@0: return 0; aoqi@0: }