src/hotspot/share/opto/indexSet.cpp
author shade
Wed, 23 Oct 2019 17:35:32 +0200
changeset 58759 4242e35767b5
parent 53961 e5b461681b88
child 59081 95a99e617f28
permissions -rw-r--r--
8232882: GCC 4.8.5 build failure after JDK-8211073 Reviewed-by: thartmann
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
51078
fc6cfe40e32a 8207049: Minor improvements of compiler code.
goetz
parents: 47216
diff changeset
     2
 * Copyright (c) 1998, 2018, Oracle and/or its affiliates. All rights reserved.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    21
 * questions.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    25
#include "precompiled.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    26
#include "memory/allocation.inline.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    27
#include "opto/chaitin.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    28
#include "opto/compile.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    29
#include "opto/indexSet.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    30
#include "opto/regmask.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    31
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
// This file defines the IndexSet class, a set of sparse integer indices.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
// This data structure is used by the compiler in its liveness analysis and
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
// during register allocation.  It also defines an iterator for this class.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
//-------------------------------- Initializations ------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
IndexSet::BitBlock  IndexSet::_empty_block     = IndexSet::BitBlock();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
// Initialize statistics counters
8320
544210b4dd48 7017124: Fix some VM stats to avoid 32-bit overflow
kvn
parents: 7397
diff changeset
    42
julong IndexSet::_alloc_new = 0;
544210b4dd48 7017124: Fix some VM stats to avoid 32-bit overflow
kvn
parents: 7397
diff changeset
    43
julong IndexSet::_alloc_total = 0;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
8320
544210b4dd48 7017124: Fix some VM stats to avoid 32-bit overflow
kvn
parents: 7397
diff changeset
    45
julong IndexSet::_total_bits = 0;
544210b4dd48 7017124: Fix some VM stats to avoid 32-bit overflow
kvn
parents: 7397
diff changeset
    46
julong IndexSet::_total_used_blocks = 0;
544210b4dd48 7017124: Fix some VM stats to avoid 32-bit overflow
kvn
parents: 7397
diff changeset
    47
julong IndexSet::_total_unused_blocks = 0;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
// Per set, or all sets operation tracing
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
int IndexSet::_serial_count = 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
//---------------------------- IndexSet::populate_free_list() -----------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
// Populate the free BitBlock list with a batch of BitBlocks.  The BitBlocks
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
// are 32 bit aligned.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
void IndexSet::populate_free_list() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
  Compile *compile = Compile::current();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
  BitBlock *free = (BitBlock*)compile->indexSet_free_block_list();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
  char *mem = (char*)arena()->Amalloc_4(sizeof(BitBlock) *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
                                        bitblock_alloc_chunk_size + 32);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
  // Align the pointer to a 32 bit boundary.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
  BitBlock *new_blocks = (BitBlock*)(((uintptr_t)mem + 32) & ~0x001F);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
  // Add the new blocks to the free list.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
  for (int i = 0; i < bitblock_alloc_chunk_size; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
    new_blocks->set_next(free);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
    free = new_blocks;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
    new_blocks++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
  compile->set_indexSet_free_block_list(free);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
  if (CollectIndexSetStatistics) {
8320
544210b4dd48 7017124: Fix some VM stats to avoid 32-bit overflow
kvn
parents: 7397
diff changeset
    78
    inc_stat_counter(&_alloc_new, bitblock_alloc_chunk_size);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
//---------------------------- IndexSet::alloc_block() ------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
// Allocate a BitBlock from the free list.  If the free list is empty,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
// prime it.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
IndexSet::BitBlock *IndexSet::alloc_block() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
  if (CollectIndexSetStatistics) {
8320
544210b4dd48 7017124: Fix some VM stats to avoid 32-bit overflow
kvn
parents: 7397
diff changeset
    91
    inc_stat_counter(&_alloc_total, 1);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
  Compile *compile = Compile::current();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
  BitBlock* free_list = (BitBlock*)compile->indexSet_free_block_list();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
  if (free_list == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
    populate_free_list();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
    free_list = (BitBlock*)compile->indexSet_free_block_list();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
  BitBlock *block = free_list;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
  compile->set_indexSet_free_block_list(block->next());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
  block->clear();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
  return block;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
//---------------------------- IndexSet::alloc_block_containing() -------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
// Allocate a new BitBlock and put it into the position in the _blocks array
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
// corresponding to element.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
IndexSet::BitBlock *IndexSet::alloc_block_containing(uint element) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
  BitBlock *block = alloc_block();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
  uint bi = get_block_index(element);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
  _blocks[bi] = block;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
  return block;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
//---------------------------- IndexSet::free_block() -------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
// Add a BitBlock to the free list.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
void IndexSet::free_block(uint i) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
  debug_only(check_watch("free block", i));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
  assert(i < _max_blocks, "block index too large");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
  BitBlock *block = _blocks[i];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
  assert(block != &_empty_block, "cannot free the empty block");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
  block->set_next((IndexSet::BitBlock*)Compile::current()->indexSet_free_block_list());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
  Compile::current()->set_indexSet_free_block_list(block);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
  set_block(i,&_empty_block);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
//------------------------------lrg_union--------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
// Compute the union of all elements of one and two which interfere with
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
// the RegMask mask.  If the degree of the union becomes exceeds
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
// fail_degree, the union bails out.  The underlying set is cleared before
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
// the union is performed.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
uint IndexSet::lrg_union(uint lr1, uint lr2,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
                         const uint fail_degree,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
                         const PhaseIFG *ifg,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
                         const RegMask &mask ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
  IndexSet *one = ifg->neighbors(lr1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
  IndexSet *two = ifg->neighbors(lr2);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
  LRG &lrg1 = ifg->lrgs(lr1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
  LRG &lrg2 = ifg->lrgs(lr2);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
  assert(_max_elements == one->_max_elements, "max element mismatch");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
  check_watch("union destination");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
  one->check_watch("union source");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
  two->check_watch("union source");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
  // Compute the degree of the combined live-range.  The combined
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
  // live-range has the union of the original live-ranges' neighbors set as
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
  // well as the neighbors of all intermediate copies, minus those neighbors
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
  // that can not use the intersected allowed-register-set.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
  // Copy the larger set.  Insert the smaller set into the larger.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
  if (two->count() > one->count()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
    IndexSet *temp = one;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
    one = two;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
    two = temp;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
  clear();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
  // Used to compute degree of register-only interferences.  Infinite-stack
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
  // neighbors do not alter colorability, as they can always color to some
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
  // other color.  (A variant of the Briggs assertion)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
  uint reg_degree = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
  uint element;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
  // Load up the combined interference set with the neighbors of one
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
  IndexSetIterator elements(one);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
  while ((element = elements.next()) != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
    LRG &lrg = ifg->lrgs(element);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
    if (mask.overlap(lrg.mask())) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
      insert(element);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
      if( !lrg.mask().is_AllStack() ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
        reg_degree += lrg1.compute_degree(lrg);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
        if( reg_degree >= fail_degree ) return reg_degree;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
      } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
        // !!!!! Danger!  No update to reg_degree despite having a neighbor.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
        // A variant of the Briggs assertion.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
        // Not needed if I simplify during coalesce, ala George/Appel.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
        assert( lrg.lo_degree(), "" );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
  // Add neighbors of two as well
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
  IndexSetIterator elements2(two);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
  while ((element = elements2.next()) != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
    LRG &lrg = ifg->lrgs(element);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
    if (mask.overlap(lrg.mask())) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
      if (insert(element)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
        if( !lrg.mask().is_AllStack() ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
          reg_degree += lrg2.compute_degree(lrg);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
          if( reg_degree >= fail_degree ) return reg_degree;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
        } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
          // !!!!! Danger!  No update to reg_degree despite having a neighbor.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
          // A variant of the Briggs assertion.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
          // Not needed if I simplify during coalesce, ala George/Appel.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
          assert( lrg.lo_degree(), "" );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
  return reg_degree;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
//---------------------------- IndexSet() -----------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
// A deep copy constructor.  This is used when you need a scratch copy of this set.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
IndexSet::IndexSet (IndexSet *set) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
  _serial_number = _serial_count++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
  set->check_watch("copied", _serial_number);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
  check_watch("initialized by copy", set->_serial_number);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
  _max_elements = set->_max_elements;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
  _count = set->_count;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
  _max_blocks = set->_max_blocks;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
  if (_max_blocks <= preallocated_block_list_size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
    _blocks = _preallocated_block_list;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
    _blocks =
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
      (IndexSet::BitBlock**) arena()->Amalloc_4(sizeof(IndexSet::BitBlock**) * _max_blocks);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
  for (uint i = 0; i < _max_blocks; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
    BitBlock *block = set->_blocks[i];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
    if (block == &_empty_block) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
      set_block(i, &_empty_block);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
      BitBlock *new_block = alloc_block();
24425
53764d2358f9 8041415: remove port.{cpp,hpp} files
zgu
parents: 8320
diff changeset
   235
      memcpy(new_block->words(), block->words(), sizeof(uint32_t) * words_per_block);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
      set_block(i, new_block);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
//---------------------------- IndexSet::initialize() -----------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
// Prepare an IndexSet for use.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
void IndexSet::initialize(uint max_elements) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
  _serial_number = _serial_count++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
  check_watch("initialized", max_elements);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
  _max_elements = max_elements;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
  _count = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
  _max_blocks = (max_elements + bits_per_block - 1) / bits_per_block;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
  if (_max_blocks <= preallocated_block_list_size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
    _blocks = _preallocated_block_list;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
  } else {
51078
fc6cfe40e32a 8207049: Minor improvements of compiler code.
goetz
parents: 47216
diff changeset
   256
    _blocks = (IndexSet::BitBlock**) arena()->Amalloc_4(sizeof(IndexSet::BitBlock*) * _max_blocks);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
  for (uint i = 0; i < _max_blocks; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
    set_block(i, &_empty_block);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
//---------------------------- IndexSet::initialize()------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
// Prepare an IndexSet for use.  If it needs to allocate its _blocks array, it does
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
// so from the Arena passed as a parameter.  BitBlock allocation is still done from
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
// the static Arena which was set with reset_memory().
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
void IndexSet::initialize(uint max_elements, Arena *arena) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
  _serial_number = _serial_count++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
  check_watch("initialized2", max_elements);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
  _max_elements = max_elements;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
#endif // ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
  _count = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
  _max_blocks = (max_elements + bits_per_block - 1) / bits_per_block;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
  if (_max_blocks <= preallocated_block_list_size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
    _blocks = _preallocated_block_list;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
  } else {
51078
fc6cfe40e32a 8207049: Minor improvements of compiler code.
goetz
parents: 47216
diff changeset
   280
    _blocks = (IndexSet::BitBlock**) arena->Amalloc_4(sizeof(IndexSet::BitBlock*) * _max_blocks);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
  for (uint i = 0; i < _max_blocks; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
    set_block(i, &_empty_block);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   284
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
//---------------------------- IndexSet::swap() -----------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
// Exchange two IndexSets.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
void IndexSet::swap(IndexSet *set) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
  assert(_max_elements == set->_max_elements, "must have same universe size to swap");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
  check_watch("swap", set->_serial_number);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   294
  set->check_watch("swap", _serial_number);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   295
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
  for (uint i = 0; i < _max_blocks; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   298
    BitBlock *temp = _blocks[i];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
    set_block(i, set->_blocks[i]);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   300
    set->set_block(i, temp);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   301
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   302
  uint temp = _count;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   303
  _count = set->_count;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   304
  set->_count = temp;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   305
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   306
489c9b5090e2 Initial load
duke
parents:
diff changeset
   307
//---------------------------- IndexSet::dump() -----------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   308
// Print this set.  Used for debugging.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
489c9b5090e2 Initial load
duke
parents:
diff changeset
   310
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   311
void IndexSet::dump() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   312
  IndexSetIterator elements(this);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   313
489c9b5090e2 Initial load
duke
parents:
diff changeset
   314
  tty->print("{");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   315
  uint i;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   316
  while ((i = elements.next()) != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   317
    tty->print("L%d ", i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   318
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   319
  tty->print_cr("}");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   320
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   321
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   322
489c9b5090e2 Initial load
duke
parents:
diff changeset
   323
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   324
//---------------------------- IndexSet::tally_iteration_statistics() -----------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   325
// Update block/bit counts to reflect that this set has been iterated over.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   326
489c9b5090e2 Initial load
duke
parents:
diff changeset
   327
void IndexSet::tally_iteration_statistics() const {
8320
544210b4dd48 7017124: Fix some VM stats to avoid 32-bit overflow
kvn
parents: 7397
diff changeset
   328
  inc_stat_counter(&_total_bits, count());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   329
489c9b5090e2 Initial load
duke
parents:
diff changeset
   330
  for (uint i = 0; i < _max_blocks; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   331
    if (_blocks[i] != &_empty_block) {
8320
544210b4dd48 7017124: Fix some VM stats to avoid 32-bit overflow
kvn
parents: 7397
diff changeset
   332
      inc_stat_counter(&_total_used_blocks, 1);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   333
    } else {
8320
544210b4dd48 7017124: Fix some VM stats to avoid 32-bit overflow
kvn
parents: 7397
diff changeset
   334
      inc_stat_counter(&_total_unused_blocks, 1);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   335
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   336
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   337
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   338
489c9b5090e2 Initial load
duke
parents:
diff changeset
   339
//---------------------------- IndexSet::print_statistics() -----------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   340
// Print statistics about IndexSet usage.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   341
489c9b5090e2 Initial load
duke
parents:
diff changeset
   342
void IndexSet::print_statistics() {
8320
544210b4dd48 7017124: Fix some VM stats to avoid 32-bit overflow
kvn
parents: 7397
diff changeset
   343
  julong total_blocks = _total_used_blocks + _total_unused_blocks;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   344
  tty->print_cr ("Accumulated IndexSet usage statistics:");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   345
  tty->print_cr ("--------------------------------------");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   346
  tty->print_cr ("  Iteration:");
8320
544210b4dd48 7017124: Fix some VM stats to avoid 32-bit overflow
kvn
parents: 7397
diff changeset
   347
  tty->print_cr ("    blocks visited: " UINT64_FORMAT, total_blocks);
544210b4dd48 7017124: Fix some VM stats to avoid 32-bit overflow
kvn
parents: 7397
diff changeset
   348
  tty->print_cr ("    blocks empty: %4.2f%%", 100.0*(double)_total_unused_blocks/total_blocks);
544210b4dd48 7017124: Fix some VM stats to avoid 32-bit overflow
kvn
parents: 7397
diff changeset
   349
  tty->print_cr ("    bit density (bits/used blocks): %4.2f", (double)_total_bits/_total_used_blocks);
544210b4dd48 7017124: Fix some VM stats to avoid 32-bit overflow
kvn
parents: 7397
diff changeset
   350
  tty->print_cr ("    bit density (bits/all blocks): %4.2f", (double)_total_bits/total_blocks);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   351
  tty->print_cr ("  Allocation:");
8320
544210b4dd48 7017124: Fix some VM stats to avoid 32-bit overflow
kvn
parents: 7397
diff changeset
   352
  tty->print_cr ("    blocks allocated: " UINT64_FORMAT, _alloc_new);
544210b4dd48 7017124: Fix some VM stats to avoid 32-bit overflow
kvn
parents: 7397
diff changeset
   353
  tty->print_cr ("    blocks used/reused: " UINT64_FORMAT, _alloc_total);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   354
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   355
489c9b5090e2 Initial load
duke
parents:
diff changeset
   356
//---------------------------- IndexSet::verify() -----------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   357
// Expensive test of IndexSet sanity.  Ensure that the count agrees with the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   358
// number of bits in the blocks.  Make sure the iterator is seeing all elements
489c9b5090e2 Initial load
duke
parents:
diff changeset
   359
// of the set.  Meant for use during development.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   360
489c9b5090e2 Initial load
duke
parents:
diff changeset
   361
void IndexSet::verify() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   362
  assert(!member(0), "zero cannot be a member");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   363
  uint count = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   364
  uint i;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   365
  for (i = 1; i < _max_elements; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   366
    if (member(i)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   367
      count++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   368
      assert(count <= _count, "_count is messed up");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   369
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   370
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   371
489c9b5090e2 Initial load
duke
parents:
diff changeset
   372
  IndexSetIterator elements(this);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   373
  count = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   374
  while ((i = elements.next()) != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   375
    count++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   376
    assert(member(i), "returned a non member");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   377
    assert(count <= _count, "iterator returned wrong number of elements");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   378
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   379
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   380
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   381
489c9b5090e2 Initial load
duke
parents:
diff changeset
   382
//---------------------------- IndexSetIterator() -----------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   383
// Create an iterator for a set.  If empty blocks are detected when iterating
489c9b5090e2 Initial load
duke
parents:
diff changeset
   384
// over the set, these blocks are replaced.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   385
489c9b5090e2 Initial load
duke
parents:
diff changeset
   386
IndexSetIterator::IndexSetIterator(IndexSet *set) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   387
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   388
  if (CollectIndexSetStatistics) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   389
    set->tally_iteration_statistics();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   390
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   391
  set->check_watch("traversed", set->count());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   392
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   393
  if (set->is_empty()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   394
    _current = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   395
    _next_word = IndexSet::words_per_block;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   396
    _next_block = 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   397
    _max_blocks = 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   398
489c9b5090e2 Initial load
duke
parents:
diff changeset
   399
    // We don't need the following values when we iterate over an empty set.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   400
    // The commented out code is left here to document that the omission
489c9b5090e2 Initial load
duke
parents:
diff changeset
   401
    // is intentional.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   402
    //
489c9b5090e2 Initial load
duke
parents:
diff changeset
   403
    //_value = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   404
    //_words = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   405
    //_blocks = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   406
    //_set = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   407
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   408
    _current = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   409
    _value = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   410
    _next_block = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   411
    _next_word = IndexSet::words_per_block;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   412
489c9b5090e2 Initial load
duke
parents:
diff changeset
   413
    _max_blocks = set->_max_blocks;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   414
    _words = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   415
    _blocks = set->_blocks;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   416
    _set = set;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   417
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   418
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   419
489c9b5090e2 Initial load
duke
parents:
diff changeset
   420
//---------------------------- IndexSetIterator(const) -----------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   421
// Iterate over a constant IndexSet.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   422
489c9b5090e2 Initial load
duke
parents:
diff changeset
   423
IndexSetIterator::IndexSetIterator(const IndexSet *set) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   424
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   425
  if (CollectIndexSetStatistics) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   426
    set->tally_iteration_statistics();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   427
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   428
  // We don't call check_watch from here to avoid bad recursion.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   429
  //   set->check_watch("traversed const", set->count());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   430
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   431
  if (set->is_empty()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   432
    _current = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   433
    _next_word = IndexSet::words_per_block;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   434
    _next_block = 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   435
    _max_blocks = 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   436
489c9b5090e2 Initial load
duke
parents:
diff changeset
   437
    // We don't need the following values when we iterate over an empty set.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   438
    // The commented out code is left here to document that the omission
489c9b5090e2 Initial load
duke
parents:
diff changeset
   439
    // is intentional.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   440
    //
489c9b5090e2 Initial load
duke
parents:
diff changeset
   441
    //_value = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   442
    //_words = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   443
    //_blocks = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   444
    //_set = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   445
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   446
    _current = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   447
    _value = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   448
    _next_block = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   449
    _next_word = IndexSet::words_per_block;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   450
489c9b5090e2 Initial load
duke
parents:
diff changeset
   451
    _max_blocks = set->_max_blocks;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   452
    _words = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   453
    _blocks = set->_blocks;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   454
    _set = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   455
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   456
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   457
489c9b5090e2 Initial load
duke
parents:
diff changeset
   458
//---------------------------- List16Iterator::advance_and_next() -----------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   459
// Advance to the next non-empty word in the set being iterated over.  Return the next element
489c9b5090e2 Initial load
duke
parents:
diff changeset
   460
// if there is one.  If we are done, return 0.  This method is called from the next() method
489c9b5090e2 Initial load
duke
parents:
diff changeset
   461
// when it gets done with a word.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   462
489c9b5090e2 Initial load
duke
parents:
diff changeset
   463
uint IndexSetIterator::advance_and_next() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   464
  // See if there is another non-empty word in the current block.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   465
  for (uint wi = _next_word; wi < (unsigned)IndexSet::words_per_block; wi++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   466
    if (_words[wi] != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   467
      // Found a non-empty word.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   468
      _value = ((_next_block - 1) * IndexSet::bits_per_block) + (wi * IndexSet::bits_per_word);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   469
      _current = _words[wi];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   470
489c9b5090e2 Initial load
duke
parents:
diff changeset
   471
      _next_word = wi+1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   472
489c9b5090e2 Initial load
duke
parents:
diff changeset
   473
      return next();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   474
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   475
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   476
489c9b5090e2 Initial load
duke
parents:
diff changeset
   477
  // We ran out of words in the current block.  Advance to next non-empty block.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   478
  for (uint bi = _next_block; bi < _max_blocks; bi++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   479
    if (_blocks[bi] != &IndexSet::_empty_block) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   480
      // Found a non-empty block.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   481
489c9b5090e2 Initial load
duke
parents:
diff changeset
   482
      _words = _blocks[bi]->words();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   483
      for (uint wi = 0; wi < (unsigned)IndexSet::words_per_block; wi++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   484
        if (_words[wi] != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   485
          // Found a non-empty word.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   486
          _value = (bi * IndexSet::bits_per_block) + (wi * IndexSet::bits_per_word);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   487
          _current = _words[wi];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   488
489c9b5090e2 Initial load
duke
parents:
diff changeset
   489
          _next_block = bi+1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   490
          _next_word = wi+1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   491
489c9b5090e2 Initial load
duke
parents:
diff changeset
   492
          return next();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   493
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   494
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   495
489c9b5090e2 Initial load
duke
parents:
diff changeset
   496
      // All of the words in the block were empty.  Replace
489c9b5090e2 Initial load
duke
parents:
diff changeset
   497
      // the block with the empty block.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   498
      if (_set) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   499
        _set->free_block(bi);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   500
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   501
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   502
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   503
489c9b5090e2 Initial load
duke
parents:
diff changeset
   504
  // These assignments make redundant calls to next on a finished iterator
489c9b5090e2 Initial load
duke
parents:
diff changeset
   505
  // faster.  Probably not necessary.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   506
  _next_block = _max_blocks;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   507
  _next_word = IndexSet::words_per_block;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   508
489c9b5090e2 Initial load
duke
parents:
diff changeset
   509
  // No more words.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   510
  return 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   511
}