src/share/vm/c1/c1_ValueMap.cpp

changeset 435
a61af66fc99e
child 1907
c18cbe5936b8
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/c1/c1_ValueMap.cpp	Sat Dec 01 00:00:00 2007 +0000
     1.3 @@ -0,0 +1,434 @@
     1.4 +/*
     1.5 + * Copyright 1999-2005 Sun Microsystems, Inc.  All Rights Reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.24 + * have any questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +#include "incls/_precompiled.incl"
    1.29 +#include "incls/_c1_ValueMap.cpp.incl"
    1.30 +
    1.31 +
    1.32 +#ifndef PRODUCT
    1.33 +
    1.34 +  int ValueMap::_number_of_finds = 0;
    1.35 +  int ValueMap::_number_of_hits = 0;
    1.36 +  int ValueMap::_number_of_kills = 0;
    1.37 +
    1.38 +  #define TRACE_VALUE_NUMBERING(code) if (PrintValueNumbering) { code; }
    1.39 +
    1.40 +#else
    1.41 +
    1.42 +  #define TRACE_VALUE_NUMBERING(code)
    1.43 +
    1.44 +#endif
    1.45 +
    1.46 +
    1.47 +ValueMap::ValueMap()
    1.48 +  : _nesting(0)
    1.49 +  , _entries(ValueMapInitialSize, NULL)
    1.50 +  , _killed_values()
    1.51 +  , _entry_count(0)
    1.52 +{
    1.53 +  NOT_PRODUCT(reset_statistics());
    1.54 +}
    1.55 +
    1.56 +
    1.57 +ValueMap::ValueMap(ValueMap* old)
    1.58 +  : _nesting(old->_nesting + 1)
    1.59 +  , _entries(old->_entries.length())
    1.60 +  , _killed_values()
    1.61 +  , _entry_count(old->_entry_count)
    1.62 +{
    1.63 +  for (int i = size() - 1; i >= 0; i--) {
    1.64 +    _entries.at_put(i, old->entry_at(i));
    1.65 +  }
    1.66 +  _killed_values.set_from(&old->_killed_values);
    1.67 +}
    1.68 +
    1.69 +
    1.70 +void ValueMap::increase_table_size() {
    1.71 +  int old_size = size();
    1.72 +  int new_size = old_size * 2 + 1;
    1.73 +
    1.74 +  ValueMapEntryList worklist(8);
    1.75 +  ValueMapEntryArray new_entries(new_size, NULL);
    1.76 +  int new_entry_count = 0;
    1.77 +
    1.78 +  TRACE_VALUE_NUMBERING(tty->print_cr("increasing table size from %d to %d", old_size, new_size));
    1.79 +
    1.80 +  for (int i = old_size - 1; i >= 0; i--) {
    1.81 +    ValueMapEntry* entry;
    1.82 +    for (entry = entry_at(i); entry != NULL; entry = entry->next()) {
    1.83 +      if (!is_killed(entry->value())) {
    1.84 +        worklist.push(entry);
    1.85 +      }
    1.86 +    }
    1.87 +
    1.88 +    while (!worklist.is_empty()) {
    1.89 +      entry = worklist.pop();
    1.90 +      int new_index = entry_index(entry->hash(), new_size);
    1.91 +
    1.92 +      if (entry->nesting() != nesting() && new_entries.at(new_index) != entry->next()) {
    1.93 +        // changing entries with a lower nesting than the current nesting of the table
    1.94 +        // is not allowed because then the same entry is contained in multiple value maps.
    1.95 +        // clone entry when next-pointer must be changed
    1.96 +        entry = new ValueMapEntry(entry->hash(), entry->value(), entry->nesting(), NULL);
    1.97 +      }
    1.98 +      entry->set_next(new_entries.at(new_index));
    1.99 +      new_entries.at_put(new_index, entry);
   1.100 +      new_entry_count++;
   1.101 +    }
   1.102 +  }
   1.103 +
   1.104 +  _entries = new_entries;
   1.105 +  _entry_count = new_entry_count;
   1.106 +}
   1.107 +
   1.108 +
   1.109 +Value ValueMap::find_insert(Value x) {
   1.110 +  const intx hash = x->hash();
   1.111 +  if (hash != 0) {
   1.112 +    // 0 hash means: exclude from value numbering
   1.113 +    NOT_PRODUCT(_number_of_finds++);
   1.114 +
   1.115 +    for (ValueMapEntry* entry = entry_at(entry_index(hash, size())); entry != NULL; entry = entry->next()) {
   1.116 +      if (entry->hash() == hash) {
   1.117 +        Value f = entry->value();
   1.118 +
   1.119 +        if (!is_killed(f) && f->is_equal(x)) {
   1.120 +          NOT_PRODUCT(_number_of_hits++);
   1.121 +          TRACE_VALUE_NUMBERING(tty->print_cr("Value Numbering: %s %c%d equal to %c%d  (size %d, entries %d, nesting-diff %d)", x->name(), x->type()->tchar(), x->id(), f->type()->tchar(), f->id(), size(), entry_count(), nesting() - entry->nesting()));
   1.122 +
   1.123 +          if (entry->nesting() != nesting() && f->as_Constant() == NULL) {
   1.124 +            // non-constant values of of another block must be pinned,
   1.125 +            // otherwise it is possible that they are not evaluated
   1.126 +            f->pin(Instruction::PinGlobalValueNumbering);
   1.127 +          }
   1.128 +
   1.129 +          return f;
   1.130 +
   1.131 +        }
   1.132 +      }
   1.133 +    }
   1.134 +
   1.135 +    // x not found, so insert it
   1.136 +    if (entry_count() >= size_threshold()) {
   1.137 +      increase_table_size();
   1.138 +    }
   1.139 +    int idx = entry_index(hash, size());
   1.140 +    _entries.at_put(idx, new ValueMapEntry(hash, x, nesting(), entry_at(idx)));
   1.141 +    _entry_count++;
   1.142 +
   1.143 +    TRACE_VALUE_NUMBERING(tty->print_cr("Value Numbering: insert %s %c%d  (size %d, entries %d, nesting %d)", x->name(), x->type()->tchar(), x->id(), size(), entry_count(), nesting()));
   1.144 +  }
   1.145 +
   1.146 +  return x;
   1.147 +}
   1.148 +
   1.149 +
   1.150 +#define GENERIC_KILL_VALUE(must_kill_implementation)                                     \
   1.151 +  NOT_PRODUCT(_number_of_kills++);                                                       \
   1.152 +                                                                                         \
   1.153 +  for (int i = size() - 1; i >= 0; i--) {                                                \
   1.154 +    ValueMapEntry* prev_entry = NULL;                                                    \
   1.155 +    for (ValueMapEntry* entry = entry_at(i); entry != NULL; entry = entry->next()) {     \
   1.156 +      Value value = entry->value();                                                      \
   1.157 +                                                                                         \
   1.158 +      must_kill_implementation(must_kill, entry, value)                                  \
   1.159 +                                                                                         \
   1.160 +      if (must_kill) {                                                                   \
   1.161 +        kill_value(value);                                                               \
   1.162 +                                                                                         \
   1.163 +        if (prev_entry == NULL) {                                                        \
   1.164 +          _entries.at_put(i, entry->next());                                             \
   1.165 +          _entry_count--;                                                                \
   1.166 +        } else if (prev_entry->nesting() == nesting()) {                                 \
   1.167 +          prev_entry->set_next(entry->next());                                           \
   1.168 +          _entry_count--;                                                                \
   1.169 +        } else {                                                                         \
   1.170 +          prev_entry = entry;                                                            \
   1.171 +        }                                                                                \
   1.172 +                                                                                         \
   1.173 +        TRACE_VALUE_NUMBERING(tty->print_cr("Value Numbering: killed %s %c%d  (size %d, entries %d, nesting-diff %d)", value->name(), value->type()->tchar(), value->id(), size(), entry_count(), nesting() - entry->nesting()));   \
   1.174 +      } else {                                                                           \
   1.175 +        prev_entry = entry;                                                              \
   1.176 +      }                                                                                  \
   1.177 +    }                                                                                    \
   1.178 +  }                                                                                      \
   1.179 +
   1.180 +#define MUST_KILL_MEMORY(must_kill, entry, value)                                        \
   1.181 +  bool must_kill = value->as_LoadField() != NULL || value->as_LoadIndexed() != NULL;
   1.182 +
   1.183 +#define MUST_KILL_ARRAY(must_kill, entry, value)                                         \
   1.184 +  bool must_kill = value->as_LoadIndexed() != NULL                                       \
   1.185 +                   && value->type()->tag() == type->tag();
   1.186 +
   1.187 +#define MUST_KILL_FIELD(must_kill, entry, value)                                         \
   1.188 +  /* ciField's are not unique; must compare their contents */                            \
   1.189 +  LoadField* lf = value->as_LoadField();                                                 \
   1.190 +  bool must_kill = lf != NULL                                                            \
   1.191 +                   && lf->field()->holder() == field->holder()                           \
   1.192 +                   && lf->field()->offset() == field->offset();
   1.193 +
   1.194 +#define MUST_KILL_EXCEPTION(must_kill, entry, value)                                     \
   1.195 +  assert(entry->nesting() < nesting(), "must not find bigger nesting than current");     \
   1.196 +  bool must_kill = (entry->nesting() == nesting() - 1);
   1.197 +
   1.198 +
   1.199 +void ValueMap::kill_memory() {
   1.200 +  GENERIC_KILL_VALUE(MUST_KILL_MEMORY);
   1.201 +}
   1.202 +
   1.203 +void ValueMap::kill_array(ValueType* type) {
   1.204 +  GENERIC_KILL_VALUE(MUST_KILL_ARRAY);
   1.205 +}
   1.206 +
   1.207 +void ValueMap::kill_field(ciField* field) {
   1.208 +  GENERIC_KILL_VALUE(MUST_KILL_FIELD);
   1.209 +}
   1.210 +
   1.211 +void ValueMap::kill_exception() {
   1.212 +  GENERIC_KILL_VALUE(MUST_KILL_EXCEPTION);
   1.213 +}
   1.214 +
   1.215 +
   1.216 +void ValueMap::kill_map(ValueMap* map) {
   1.217 +  assert(is_global_value_numbering(), "only for global value numbering");
   1.218 +  _killed_values.set_union(&map->_killed_values);
   1.219 +}
   1.220 +
   1.221 +void ValueMap::kill_all() {
   1.222 +  assert(is_local_value_numbering(), "only for local value numbering");
   1.223 +  for (int i = size() - 1; i >= 0; i--) {
   1.224 +    _entries.at_put(i, NULL);
   1.225 +  }
   1.226 +  _entry_count = 0;
   1.227 +}
   1.228 +
   1.229 +
   1.230 +#ifndef PRODUCT
   1.231 +
   1.232 +void ValueMap::print() {
   1.233 +  tty->print_cr("(size %d, entries %d, nesting %d)", size(), entry_count(), nesting());
   1.234 +
   1.235 +  int entries = 0;
   1.236 +  for (int i = 0; i < size(); i++) {
   1.237 +    if (entry_at(i) != NULL) {
   1.238 +      tty->print("  %2d: ", i);
   1.239 +      for (ValueMapEntry* entry = entry_at(i); entry != NULL; entry = entry->next()) {
   1.240 +        Value value = entry->value();
   1.241 +        tty->print("%s %c%d (%s%d) -> ", value->name(), value->type()->tchar(), value->id(), is_killed(value) ? "x" : "", entry->nesting());
   1.242 +        entries++;
   1.243 +      }
   1.244 +      tty->print_cr("NULL");
   1.245 +    }
   1.246 +  }
   1.247 +
   1.248 +  _killed_values.print();
   1.249 +  assert(entry_count() == entries, "entry_count incorrect");
   1.250 +}
   1.251 +
   1.252 +void ValueMap::reset_statistics() {
   1.253 +  _number_of_finds = 0;
   1.254 +  _number_of_hits = 0;
   1.255 +  _number_of_kills = 0;
   1.256 +}
   1.257 +
   1.258 +void ValueMap::print_statistics() {
   1.259 +  float hit_rate = 0;
   1.260 +  if (_number_of_finds != 0) {
   1.261 +    hit_rate = (float)_number_of_hits / _number_of_finds;
   1.262 +  }
   1.263 +
   1.264 +  tty->print_cr("finds:%3d  hits:%3d   kills:%3d  hit rate: %1.4f", _number_of_finds, _number_of_hits, _number_of_kills, hit_rate);
   1.265 +}
   1.266 +
   1.267 +#endif
   1.268 +
   1.269 +
   1.270 +
   1.271 +class ShortLoopOptimizer : public ValueNumberingVisitor {
   1.272 + private:
   1.273 +  GlobalValueNumbering* _gvn;
   1.274 +  BlockList             _loop_blocks;
   1.275 +  bool                  _too_complicated_loop;
   1.276 +
   1.277 +  // simplified access to methods of GlobalValueNumbering
   1.278 +  ValueMap* current_map()                        { return _gvn->current_map(); }
   1.279 +  ValueMap* value_map_of(BlockBegin* block)      { return _gvn->value_map_of(block); }
   1.280 +
   1.281 +  // implementation for abstract methods of ValueNumberingVisitor
   1.282 +  void      kill_memory()                        { _too_complicated_loop = true; }
   1.283 +  void      kill_field(ciField* field)           { current_map()->kill_field(field); };
   1.284 +  void      kill_array(ValueType* type)          { current_map()->kill_array(type); };
   1.285 +
   1.286 + public:
   1.287 +  ShortLoopOptimizer(GlobalValueNumbering* gvn)
   1.288 +    : _gvn(gvn)
   1.289 +    , _loop_blocks(ValueMapMaxLoopSize)
   1.290 +    , _too_complicated_loop(false)
   1.291 +  {
   1.292 +  }
   1.293 +
   1.294 +  bool process(BlockBegin* loop_header);
   1.295 +};
   1.296 +
   1.297 +
   1.298 +bool ShortLoopOptimizer::process(BlockBegin* loop_header) {
   1.299 +  TRACE_VALUE_NUMBERING(tty->print_cr("** loop header block"));
   1.300 +
   1.301 +  _too_complicated_loop = false;
   1.302 +  _loop_blocks.clear();
   1.303 +  _loop_blocks.append(loop_header);
   1.304 +
   1.305 +  for (int i = 0; i < _loop_blocks.length(); i++) {
   1.306 +    BlockBegin* block = _loop_blocks.at(i);
   1.307 +    TRACE_VALUE_NUMBERING(tty->print_cr("processing loop block B%d", block->block_id()));
   1.308 +
   1.309 +    if (block->is_set(BlockBegin::exception_entry_flag)) {
   1.310 +      // this would be too complicated
   1.311 +      return false;
   1.312 +    }
   1.313 +
   1.314 +    // add predecessors to worklist
   1.315 +    for (int j = block->number_of_preds() - 1; j >= 0; j--) {
   1.316 +      BlockBegin* pred = block->pred_at(j);
   1.317 +
   1.318 +      ValueMap* pred_map = value_map_of(pred);
   1.319 +      if (pred_map != NULL) {
   1.320 +        current_map()->kill_map(pred_map);
   1.321 +      } else if (!_loop_blocks.contains(pred)) {
   1.322 +        if (_loop_blocks.length() >= ValueMapMaxLoopSize) {
   1.323 +          return false;
   1.324 +        }
   1.325 +        _loop_blocks.append(pred);
   1.326 +      }
   1.327 +    }
   1.328 +
   1.329 +    // use the instruction visitor for killing values
   1.330 +    for (Value instr = block->next(); instr != NULL; instr = instr->next()) {
   1.331 +      instr->visit(this);
   1.332 +      if (_too_complicated_loop) {
   1.333 +        return false;
   1.334 +      }
   1.335 +    }
   1.336 +  }
   1.337 +
   1.338 +  TRACE_VALUE_NUMBERING(tty->print_cr("** loop successfully optimized"));
   1.339 +  return true;
   1.340 +}
   1.341 +
   1.342 +
   1.343 +GlobalValueNumbering::GlobalValueNumbering(IR* ir)
   1.344 +  : _current_map(NULL)
   1.345 +  , _value_maps(ir->linear_scan_order()->length(), NULL)
   1.346 +{
   1.347 +  TRACE_VALUE_NUMBERING(tty->print_cr("****** start of global value numbering"));
   1.348 +
   1.349 +  ShortLoopOptimizer short_loop_optimizer(this);
   1.350 +  int subst_count = 0;
   1.351 +
   1.352 +  BlockList* blocks = ir->linear_scan_order();
   1.353 +  int num_blocks = blocks->length();
   1.354 +
   1.355 +  BlockBegin* start_block = blocks->at(0);
   1.356 +  assert(start_block == ir->start() && start_block->number_of_preds() == 0 && start_block->dominator() == NULL, "must be start block");
   1.357 +  assert(start_block->next()->as_Base() != NULL && start_block->next()->next() == NULL, "start block must not have instructions");
   1.358 +
   1.359 +  // initial, empty value map with nesting 0
   1.360 +  set_value_map_of(start_block, new ValueMap());
   1.361 +
   1.362 +  for (int i = 1; i < num_blocks; i++) {
   1.363 +    BlockBegin* block = blocks->at(i);
   1.364 +    TRACE_VALUE_NUMBERING(tty->print_cr("**** processing block B%d", block->block_id()));
   1.365 +
   1.366 +    int num_preds = block->number_of_preds();
   1.367 +    assert(num_preds > 0, "block must have predecessors");
   1.368 +
   1.369 +    BlockBegin* dominator = block->dominator();
   1.370 +    assert(dominator != NULL, "dominator must exist");
   1.371 +    assert(value_map_of(dominator) != NULL, "value map of dominator must exist");
   1.372 +
   1.373 +    // create new value map with increased nesting
   1.374 +    _current_map = new ValueMap(value_map_of(dominator));
   1.375 +
   1.376 +    if (num_preds == 1) {
   1.377 +      assert(dominator == block->pred_at(0), "dominator must be equal to predecessor");
   1.378 +      // nothing to do here
   1.379 +
   1.380 +    } else if (block->is_set(BlockBegin::linear_scan_loop_header_flag)) {
   1.381 +      // block has incoming backward branches -> try to optimize short loops
   1.382 +      if (!short_loop_optimizer.process(block)) {
   1.383 +        // loop is too complicated, so kill all memory loads because there might be
   1.384 +        // stores to them in the loop
   1.385 +        current_map()->kill_memory();
   1.386 +      }
   1.387 +
   1.388 +    } else {
   1.389 +      // only incoming forward branches that are already processed
   1.390 +      for (int j = 0; j < num_preds; j++) {
   1.391 +        BlockBegin* pred = block->pred_at(j);
   1.392 +        ValueMap* pred_map = value_map_of(pred);
   1.393 +
   1.394 +        if (pred_map != NULL) {
   1.395 +          // propagate killed values of the predecessor to this block
   1.396 +          current_map()->kill_map(value_map_of(pred));
   1.397 +        } else {
   1.398 +          // kill all memory loads because predecessor not yet processed
   1.399 +          // (this can happen with non-natural loops and OSR-compiles)
   1.400 +          current_map()->kill_memory();
   1.401 +        }
   1.402 +      }
   1.403 +    }
   1.404 +
   1.405 +    if (block->is_set(BlockBegin::exception_entry_flag)) {
   1.406 +      current_map()->kill_exception();
   1.407 +    }
   1.408 +
   1.409 +    TRACE_VALUE_NUMBERING(tty->print("value map before processing block: "); current_map()->print());
   1.410 +
   1.411 +    // visit all instructions of this block
   1.412 +    for (Value instr = block->next(); instr != NULL; instr = instr->next()) {
   1.413 +      assert(!instr->has_subst(), "substitution already set");
   1.414 +
   1.415 +      // check if instruction kills any values
   1.416 +      instr->visit(this);
   1.417 +
   1.418 +      if (instr->hash() != 0) {
   1.419 +        Value f = current_map()->find_insert(instr);
   1.420 +        if (f != instr) {
   1.421 +          assert(!f->has_subst(), "can't have a substitution");
   1.422 +          instr->set_subst(f);
   1.423 +          subst_count++;
   1.424 +        }
   1.425 +      }
   1.426 +    }
   1.427 +
   1.428 +    // remember value map for successors
   1.429 +    set_value_map_of(block, current_map());
   1.430 +  }
   1.431 +
   1.432 +  if (subst_count != 0) {
   1.433 +    SubstitutionResolver resolver(ir);
   1.434 +  }
   1.435 +
   1.436 +  TRACE_VALUE_NUMBERING(tty->print("****** end of global value numbering. "); ValueMap::print_statistics());
   1.437 +}

mercurial