hotspot/src/share/vm/c1/c1_ValueMap.cpp
author roland
Wed, 10 Apr 2013 09:52:49 +0200
changeset 16690 a428299c08cb
parent 16611 6807a703dd6b
child 22234 da823d78ad65
permissions -rw-r--r--
8011706: specjvm2008 test xml.transform gets array bound exception with c1 Summary: loop invariant code motion may move load before store to the same field Reviewed-by: kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
13963
e5b53c306fb5 7197424: update copyright year to match last edit in jdk8 hotspot repository
mikael
parents: 13489
diff changeset
     2
 * Copyright (c) 1999, 2012, 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 "c1/c1_Canonicalizer.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    27
#include "c1/c1_IR.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    28
#include "c1/c1_ValueMap.hpp"
16611
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
    29
#include "c1/c1_ValueStack.hpp"
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    30
#include "utilities/bitMap.inline.hpp"
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
  int ValueMap::_number_of_finds = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
  int ValueMap::_number_of_hits = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
  int ValueMap::_number_of_kills = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
  #define TRACE_VALUE_NUMBERING(code) if (PrintValueNumbering) { code; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
#else
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
  #define TRACE_VALUE_NUMBERING(code)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
ValueMap::ValueMap()
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
  : _nesting(0)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
  , _entries(ValueMapInitialSize, NULL)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
  , _killed_values()
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
  , _entry_count(0)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
  NOT_PRODUCT(reset_statistics());
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
ValueMap::ValueMap(ValueMap* old)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
  : _nesting(old->_nesting + 1)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
  , _entries(old->_entries.length())
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
  , _killed_values()
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
  , _entry_count(old->_entry_count)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
  for (int i = size() - 1; i >= 0; i--) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
    _entries.at_put(i, old->entry_at(i));
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
  _killed_values.set_from(&old->_killed_values);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
void ValueMap::increase_table_size() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
  int old_size = size();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
  int new_size = old_size * 2 + 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
  ValueMapEntryList worklist(8);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
  ValueMapEntryArray new_entries(new_size, NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
  int new_entry_count = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
  TRACE_VALUE_NUMBERING(tty->print_cr("increasing table size from %d to %d", old_size, new_size));
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
  for (int i = old_size - 1; i >= 0; i--) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
    ValueMapEntry* entry;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
    for (entry = entry_at(i); entry != NULL; entry = entry->next()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
      if (!is_killed(entry->value())) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
        worklist.push(entry);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
    while (!worklist.is_empty()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
      entry = worklist.pop();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
      int new_index = entry_index(entry->hash(), new_size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
      if (entry->nesting() != nesting() && new_entries.at(new_index) != entry->next()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
        // changing entries with a lower nesting than the current nesting of the table
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
        // is not allowed because then the same entry is contained in multiple value maps.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
        // clone entry when next-pointer must be changed
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
        entry = new ValueMapEntry(entry->hash(), entry->value(), entry->nesting(), NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
      entry->set_next(new_entries.at(new_index));
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
      new_entries.at_put(new_index, entry);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
      new_entry_count++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
  _entries = new_entries;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
  _entry_count = new_entry_count;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
Value ValueMap::find_insert(Value x) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
  const intx hash = x->hash();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
  if (hash != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
    // 0 hash means: exclude from value numbering
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
    NOT_PRODUCT(_number_of_finds++);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
    for (ValueMapEntry* entry = entry_at(entry_index(hash, size())); entry != NULL; entry = entry->next()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
      if (entry->hash() == hash) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
        Value f = entry->value();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
        if (!is_killed(f) && f->is_equal(x)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
          NOT_PRODUCT(_number_of_hits++);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   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()));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
          if (entry->nesting() != nesting() && f->as_Constant() == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
            // non-constant values of of another block must be pinned,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
            // otherwise it is possible that they are not evaluated
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
            f->pin(Instruction::PinGlobalValueNumbering);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
          }
11635
5a16856f871f 7090976: Eclipse/CDT causes a JVM crash while indexing C++ code
roland
parents: 7397
diff changeset
   128
          assert(x->type()->tag() == f->type()->tag(), "should have same type");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
          return f;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
    // x not found, so insert it
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
    if (entry_count() >= size_threshold()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
      increase_table_size();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
    int idx = entry_index(hash, size());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
    _entries.at_put(idx, new ValueMapEntry(hash, x, nesting(), entry_at(idx)));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
    _entry_count++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
    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()));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
  return x;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
#define GENERIC_KILL_VALUE(must_kill_implementation)                                     \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
  NOT_PRODUCT(_number_of_kills++);                                                       \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
                                                                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
  for (int i = size() - 1; i >= 0; i--) {                                                \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
    ValueMapEntry* prev_entry = NULL;                                                    \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
    for (ValueMapEntry* entry = entry_at(i); entry != NULL; entry = entry->next()) {     \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
      Value value = entry->value();                                                      \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
                                                                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
      must_kill_implementation(must_kill, entry, value)                                  \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
                                                                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
      if (must_kill) {                                                                   \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
        kill_value(value);                                                               \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
                                                                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
        if (prev_entry == NULL) {                                                        \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
          _entries.at_put(i, entry->next());                                             \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
          _entry_count--;                                                                \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
        } else if (prev_entry->nesting() == nesting()) {                                 \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
          prev_entry->set_next(entry->next());                                           \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
          _entry_count--;                                                                \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
        } else {                                                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
          prev_entry = entry;                                                            \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
        }                                                                                \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
                                                                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
        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()));   \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
      } else {                                                                           \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
        prev_entry = entry;                                                              \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
      }                                                                                  \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
    }                                                                                    \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
  }                                                                                      \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
#define MUST_KILL_MEMORY(must_kill, entry, value)                                        \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
  bool must_kill = value->as_LoadField() != NULL || value->as_LoadIndexed() != NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
#define MUST_KILL_ARRAY(must_kill, entry, value)                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
  bool must_kill = value->as_LoadIndexed() != NULL                                       \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
                   && value->type()->tag() == type->tag();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
#define MUST_KILL_FIELD(must_kill, entry, value)                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
  /* ciField's are not unique; must compare their contents */                            \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
  LoadField* lf = value->as_LoadField();                                                 \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
  bool must_kill = lf != NULL                                                            \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
                   && lf->field()->holder() == field->holder()                           \
13489
51d8afc9439e 7171824: assert(_offset >= 1) failed: illegal call to offset()
roland
parents: 11635
diff changeset
   193
                   && (all_offsets || lf->field()->offset() == field->offset());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
void ValueMap::kill_memory() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
  GENERIC_KILL_VALUE(MUST_KILL_MEMORY);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
void ValueMap::kill_array(ValueType* type) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
  GENERIC_KILL_VALUE(MUST_KILL_ARRAY);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
13489
51d8afc9439e 7171824: assert(_offset >= 1) failed: illegal call to offset()
roland
parents: 11635
diff changeset
   204
void ValueMap::kill_field(ciField* field, bool all_offsets) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
  GENERIC_KILL_VALUE(MUST_KILL_FIELD);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
void ValueMap::kill_map(ValueMap* map) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
  assert(is_global_value_numbering(), "only for global value numbering");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
  _killed_values.set_union(&map->_killed_values);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
void ValueMap::kill_all() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
  assert(is_local_value_numbering(), "only for local value numbering");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
  for (int i = size() - 1; i >= 0; i--) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
    _entries.at_put(i, NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
  _entry_count = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
void ValueMap::print() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
  tty->print_cr("(size %d, entries %d, nesting %d)", size(), entry_count(), nesting());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
  int entries = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
  for (int i = 0; i < size(); i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
    if (entry_at(i) != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
      tty->print("  %2d: ", i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
      for (ValueMapEntry* entry = entry_at(i); entry != NULL; entry = entry->next()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
        Value value = entry->value();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
        tty->print("%s %c%d (%s%d) -> ", value->name(), value->type()->tchar(), value->id(), is_killed(value) ? "x" : "", entry->nesting());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
        entries++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
      tty->print_cr("NULL");
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
  _killed_values.print();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
  assert(entry_count() == entries, "entry_count incorrect");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
void ValueMap::reset_statistics() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
  _number_of_finds = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
  _number_of_hits = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
  _number_of_kills = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
void ValueMap::print_statistics() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
  float hit_rate = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
  if (_number_of_finds != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
    hit_rate = (float)_number_of_hits / _number_of_finds;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
  tty->print_cr("finds:%3d  hits:%3d   kills:%3d  hit rate: %1.4f", _number_of_finds, _number_of_hits, _number_of_kills, hit_rate);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
#endif
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
class ShortLoopOptimizer : public ValueNumberingVisitor {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
  GlobalValueNumbering* _gvn;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
  BlockList             _loop_blocks;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
  bool                  _too_complicated_loop;
16611
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   268
  bool                  _has_field_store[T_ARRAY + 1];
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   269
  bool                  _has_indexed_store[T_ARRAY + 1];
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
  // simplified access to methods of GlobalValueNumbering
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
  ValueMap* current_map()                        { return _gvn->current_map(); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
  ValueMap* value_map_of(BlockBegin* block)      { return _gvn->value_map_of(block); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
  // implementation for abstract methods of ValueNumberingVisitor
13489
51d8afc9439e 7171824: assert(_offset >= 1) failed: illegal call to offset()
roland
parents: 11635
diff changeset
   276
  void      kill_memory()                                 { _too_complicated_loop = true; }
16611
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   277
  void      kill_field(ciField* field, bool all_offsets)  {
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   278
    current_map()->kill_field(field, all_offsets);
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   279
    assert(field->type()->basic_type() >= 0 && field->type()->basic_type() <= T_ARRAY, "Invalid type");
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   280
    _has_field_store[field->type()->basic_type()] = true;
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   281
  }
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   282
  void      kill_array(ValueType* type)                   {
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   283
    current_map()->kill_array(type);
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   284
    BasicType basic_type = as_BasicType(type); assert(basic_type >= 0 && basic_type <= T_ARRAY, "Invalid type");
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   285
    _has_indexed_store[basic_type] = true;
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   286
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
  ShortLoopOptimizer(GlobalValueNumbering* gvn)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
    : _gvn(gvn)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
    , _loop_blocks(ValueMapMaxLoopSize)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
    , _too_complicated_loop(false)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
  {
16611
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   294
    for (int i=0; i<= T_ARRAY; i++){
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   295
      _has_field_store[i] = false;
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   296
      _has_indexed_store[i] = false;
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   297
    }
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   298
  }
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   299
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   300
  bool has_field_store(BasicType type) {
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   301
    assert(type >= 0 && type <= T_ARRAY, "Invalid type");
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   302
    return _has_field_store[type];
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   303
  }
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   304
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   305
  bool has_indexed_store(BasicType type) {
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   306
    assert(type >= 0 && type <= T_ARRAY, "Invalid type");
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   307
    return _has_indexed_store[type];
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   308
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
489c9b5090e2 Initial load
duke
parents:
diff changeset
   310
  bool process(BlockBegin* loop_header);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   311
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   312
16611
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   313
class LoopInvariantCodeMotion : public StackObj  {
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   314
 private:
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   315
  GlobalValueNumbering* _gvn;
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   316
  ShortLoopOptimizer*   _short_loop_optimizer;
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   317
  Instruction*          _insertion_point;
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   318
  ValueStack *          _state;
16690
a428299c08cb 8011706: specjvm2008 test xml.transform gets array bound exception with c1
roland
parents: 16611
diff changeset
   319
  bool                  _insert_is_pred;
16611
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   320
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   321
  void set_invariant(Value v) const    { _gvn->set_processed(v); }
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   322
  bool is_invariant(Value v) const     { return _gvn->is_processed(v); }
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   323
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   324
  void process_block(BlockBegin* block);
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   325
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   326
 public:
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   327
  LoopInvariantCodeMotion(ShortLoopOptimizer *slo, GlobalValueNumbering* gvn, BlockBegin* loop_header, BlockList* loop_blocks);
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   328
};
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   329
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   330
LoopInvariantCodeMotion::LoopInvariantCodeMotion(ShortLoopOptimizer *slo, GlobalValueNumbering* gvn, BlockBegin* loop_header, BlockList* loop_blocks)
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   331
  : _gvn(gvn), _short_loop_optimizer(slo) {
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   332
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   333
  TRACE_VALUE_NUMBERING(tty->print_cr("using loop invariant code motion loop_header = %d", loop_header->block_id()));
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   334
  TRACE_VALUE_NUMBERING(tty->print_cr("** loop invariant code motion for short loop B%d", loop_header->block_id()));
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   335
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   336
  BlockBegin* insertion_block = loop_header->dominator();
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   337
  if (insertion_block->number_of_preds() == 0) {
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   338
    return;  // only the entry block does not have a predecessor
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   339
  }
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   340
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   341
  assert(insertion_block->end()->as_Base() == NULL, "cannot insert into entry block");
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   342
  _insertion_point = insertion_block->end()->prev();
16690
a428299c08cb 8011706: specjvm2008 test xml.transform gets array bound exception with c1
roland
parents: 16611
diff changeset
   343
  _insert_is_pred = loop_header->is_predecessor(insertion_block);
16611
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   344
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   345
  BlockEnd *block_end = insertion_block->end();
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   346
  _state = block_end->state_before();
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   347
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   348
  if (!_state) {
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   349
    // If, TableSwitch and LookupSwitch always have state_before when
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   350
    // loop invariant code motion happens..
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   351
    assert(block_end->as_Goto(), "Block has to be goto");
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   352
    _state = block_end->state();
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   353
  }
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   354
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   355
  // the loop_blocks are filled by going backward from the loop header, so this processing order is best
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   356
  assert(loop_blocks->at(0) == loop_header, "loop header must be first loop block");
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   357
  process_block(loop_header);
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   358
  for (int i = loop_blocks->length() - 1; i >= 1; i--) {
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   359
    process_block(loop_blocks->at(i));
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   360
  }
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   361
}
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   362
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   363
void LoopInvariantCodeMotion::process_block(BlockBegin* block) {
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   364
  TRACE_VALUE_NUMBERING(tty->print_cr("processing block B%d", block->block_id()));
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   365
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   366
  Instruction* prev = block;
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   367
  Instruction* cur = block->next();
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   368
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   369
  while (cur != NULL) {
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   370
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   371
    // determine if cur instruction is loop invariant
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   372
    // only selected instruction types are processed here
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   373
    bool cur_invariant = false;
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   374
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   375
    if (cur->as_Constant() != NULL) {
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   376
      cur_invariant = !cur->can_trap();
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   377
    } else if (cur->as_ArithmeticOp() != NULL || cur->as_LogicOp() != NULL || cur->as_ShiftOp() != NULL) {
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   378
      assert(cur->as_Op2() != NULL, "must be Op2");
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   379
      Op2* op2 = (Op2*)cur;
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   380
      cur_invariant = !op2->can_trap() && is_invariant(op2->x()) && is_invariant(op2->y());
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   381
    } else if (cur->as_LoadField() != NULL) {
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   382
      LoadField* lf = (LoadField*)cur;
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   383
      // deoptimizes on NullPointerException
16690
a428299c08cb 8011706: specjvm2008 test xml.transform gets array bound exception with c1
roland
parents: 16611
diff changeset
   384
      cur_invariant = !lf->needs_patching() && !lf->field()->is_volatile() && !_short_loop_optimizer->has_field_store(lf->field()->type()->basic_type()) && is_invariant(lf->obj()) && _insert_is_pred;
16611
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   385
    } else if (cur->as_ArrayLength() != NULL) {
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   386
      ArrayLength *length = cur->as_ArrayLength();
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   387
      cur_invariant = is_invariant(length->array());
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   388
    } else if (cur->as_LoadIndexed() != NULL) {
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   389
      LoadIndexed *li = (LoadIndexed *)cur->as_LoadIndexed();
16690
a428299c08cb 8011706: specjvm2008 test xml.transform gets array bound exception with c1
roland
parents: 16611
diff changeset
   390
      cur_invariant = !_short_loop_optimizer->has_indexed_store(as_BasicType(cur->type())) && is_invariant(li->array()) && is_invariant(li->index()) && _insert_is_pred;
16611
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   391
    }
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   392
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   393
    if (cur_invariant) {
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   394
      // perform value numbering and mark instruction as loop-invariant
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   395
      _gvn->substitute(cur);
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   396
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   397
      if (cur->as_Constant() == NULL) {
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   398
        // ensure that code for non-constant instructions is always generated
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   399
        cur->pin();
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   400
      }
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   401
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   402
      // remove cur instruction from loop block and append it to block before loop
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   403
      Instruction* next = cur->next();
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   404
      Instruction* in = _insertion_point->next();
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   405
      _insertion_point = _insertion_point->set_next(cur);
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   406
      cur->set_next(in);
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   407
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   408
      //  Deoptimize on exception
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   409
      cur->set_flag(Instruction::DeoptimizeOnException, true);
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   410
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   411
      //  Clear exception handlers
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   412
      cur->set_exception_handlers(NULL);
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   413
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   414
      TRACE_VALUE_NUMBERING(tty->print_cr("Instruction %c%d is loop invariant", cur->type()->tchar(), cur->id()));
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   415
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   416
      if (cur->state_before() != NULL) {
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   417
        cur->set_state_before(_state->copy());
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   418
      }
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   419
      if (cur->exception_state() != NULL) {
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   420
        cur->set_exception_state(_state->copy());
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   421
      }
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   422
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   423
      cur = prev->set_next(next);
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   424
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   425
    } else {
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   426
      prev = cur;
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   427
      cur = cur->next();
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   428
    }
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   429
  }
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   430
}
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   431
489c9b5090e2 Initial load
duke
parents:
diff changeset
   432
bool ShortLoopOptimizer::process(BlockBegin* loop_header) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   433
  TRACE_VALUE_NUMBERING(tty->print_cr("** loop header block"));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   434
489c9b5090e2 Initial load
duke
parents:
diff changeset
   435
  _too_complicated_loop = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   436
  _loop_blocks.clear();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   437
  _loop_blocks.append(loop_header);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   438
489c9b5090e2 Initial load
duke
parents:
diff changeset
   439
  for (int i = 0; i < _loop_blocks.length(); i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   440
    BlockBegin* block = _loop_blocks.at(i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   441
    TRACE_VALUE_NUMBERING(tty->print_cr("processing loop block B%d", block->block_id()));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   442
489c9b5090e2 Initial load
duke
parents:
diff changeset
   443
    if (block->is_set(BlockBegin::exception_entry_flag)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   444
      // this would be too complicated
489c9b5090e2 Initial load
duke
parents:
diff changeset
   445
      return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   446
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   447
489c9b5090e2 Initial load
duke
parents:
diff changeset
   448
    // add predecessors to worklist
489c9b5090e2 Initial load
duke
parents:
diff changeset
   449
    for (int j = block->number_of_preds() - 1; j >= 0; j--) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   450
      BlockBegin* pred = block->pred_at(j);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   451
16611
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   452
      if (pred->is_set(BlockBegin::osr_entry_flag)) {
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   453
        return false;
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   454
      }
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   455
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   456
      ValueMap* pred_map = value_map_of(pred);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   457
      if (pred_map != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   458
        current_map()->kill_map(pred_map);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   459
      } else if (!_loop_blocks.contains(pred)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   460
        if (_loop_blocks.length() >= ValueMapMaxLoopSize) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   461
          return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   462
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   463
        _loop_blocks.append(pred);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   464
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   465
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   466
489c9b5090e2 Initial load
duke
parents:
diff changeset
   467
    // use the instruction visitor for killing values
489c9b5090e2 Initial load
duke
parents:
diff changeset
   468
    for (Value instr = block->next(); instr != NULL; instr = instr->next()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   469
      instr->visit(this);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   470
      if (_too_complicated_loop) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   471
        return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   472
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   473
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   474
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   475
16611
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   476
  bool optimistic = this->_gvn->compilation()->is_optimistic();
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   477
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   478
  if (UseLoopInvariantCodeMotion && optimistic) {
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   479
    LoopInvariantCodeMotion code_motion(this, _gvn, loop_header, &_loop_blocks);
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   480
  }
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   481
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   482
  TRACE_VALUE_NUMBERING(tty->print_cr("** loop successfully optimized"));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   483
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   484
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   485
489c9b5090e2 Initial load
duke
parents:
diff changeset
   486
489c9b5090e2 Initial load
duke
parents:
diff changeset
   487
GlobalValueNumbering::GlobalValueNumbering(IR* ir)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   488
  : _current_map(NULL)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   489
  , _value_maps(ir->linear_scan_order()->length(), NULL)
16611
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   490
  , _compilation(ir->compilation())
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   491
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
   492
  TRACE_VALUE_NUMBERING(tty->print_cr("****** start of global value numbering"));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   493
489c9b5090e2 Initial load
duke
parents:
diff changeset
   494
  ShortLoopOptimizer short_loop_optimizer(this);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   495
489c9b5090e2 Initial load
duke
parents:
diff changeset
   496
  BlockList* blocks = ir->linear_scan_order();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   497
  int num_blocks = blocks->length();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   498
489c9b5090e2 Initial load
duke
parents:
diff changeset
   499
  BlockBegin* start_block = blocks->at(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   500
  assert(start_block == ir->start() && start_block->number_of_preds() == 0 && start_block->dominator() == NULL, "must be start block");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   501
  assert(start_block->next()->as_Base() != NULL && start_block->next()->next() == NULL, "start block must not have instructions");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   502
16611
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   503
  // method parameters are not linked in instructions list, so process them separateley
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   504
  for_each_state_value(start_block->state(), value,
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   505
     assert(value->as_Local() != NULL, "only method parameters allowed");
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   506
     set_processed(value);
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   507
  );
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   508
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   509
  // initial, empty value map with nesting 0
489c9b5090e2 Initial load
duke
parents:
diff changeset
   510
  set_value_map_of(start_block, new ValueMap());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   511
489c9b5090e2 Initial load
duke
parents:
diff changeset
   512
  for (int i = 1; i < num_blocks; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   513
    BlockBegin* block = blocks->at(i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   514
    TRACE_VALUE_NUMBERING(tty->print_cr("**** processing block B%d", block->block_id()));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   515
489c9b5090e2 Initial load
duke
parents:
diff changeset
   516
    int num_preds = block->number_of_preds();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   517
    assert(num_preds > 0, "block must have predecessors");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   518
489c9b5090e2 Initial load
duke
parents:
diff changeset
   519
    BlockBegin* dominator = block->dominator();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   520
    assert(dominator != NULL, "dominator must exist");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   521
    assert(value_map_of(dominator) != NULL, "value map of dominator must exist");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   522
489c9b5090e2 Initial load
duke
parents:
diff changeset
   523
    // create new value map with increased nesting
489c9b5090e2 Initial load
duke
parents:
diff changeset
   524
    _current_map = new ValueMap(value_map_of(dominator));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   525
16611
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   526
    if (num_preds == 1 && !block->is_set(BlockBegin::exception_entry_flag)) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   527
      assert(dominator == block->pred_at(0), "dominator must be equal to predecessor");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   528
      // nothing to do here
489c9b5090e2 Initial load
duke
parents:
diff changeset
   529
489c9b5090e2 Initial load
duke
parents:
diff changeset
   530
    } else if (block->is_set(BlockBegin::linear_scan_loop_header_flag)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   531
      // block has incoming backward branches -> try to optimize short loops
489c9b5090e2 Initial load
duke
parents:
diff changeset
   532
      if (!short_loop_optimizer.process(block)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   533
        // loop is too complicated, so kill all memory loads because there might be
489c9b5090e2 Initial load
duke
parents:
diff changeset
   534
        // stores to them in the loop
489c9b5090e2 Initial load
duke
parents:
diff changeset
   535
        current_map()->kill_memory();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   536
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   537
489c9b5090e2 Initial load
duke
parents:
diff changeset
   538
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   539
      // only incoming forward branches that are already processed
489c9b5090e2 Initial load
duke
parents:
diff changeset
   540
      for (int j = 0; j < num_preds; j++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   541
        BlockBegin* pred = block->pred_at(j);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   542
        ValueMap* pred_map = value_map_of(pred);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   543
489c9b5090e2 Initial load
duke
parents:
diff changeset
   544
        if (pred_map != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   545
          // propagate killed values of the predecessor to this block
489c9b5090e2 Initial load
duke
parents:
diff changeset
   546
          current_map()->kill_map(value_map_of(pred));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   547
        } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   548
          // kill all memory loads because predecessor not yet processed
489c9b5090e2 Initial load
duke
parents:
diff changeset
   549
          // (this can happen with non-natural loops and OSR-compiles)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   550
          current_map()->kill_memory();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   551
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   552
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   553
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   554
16611
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   555
    // phi functions are not linked in instructions list, so process them separateley
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   556
    for_each_phi_fun(block, phi,
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   557
      set_processed(phi);
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   558
    );
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   559
489c9b5090e2 Initial load
duke
parents:
diff changeset
   560
    TRACE_VALUE_NUMBERING(tty->print("value map before processing block: "); current_map()->print());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   561
489c9b5090e2 Initial load
duke
parents:
diff changeset
   562
    // visit all instructions of this block
489c9b5090e2 Initial load
duke
parents:
diff changeset
   563
    for (Value instr = block->next(); instr != NULL; instr = instr->next()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   564
      // check if instruction kills any values
489c9b5090e2 Initial load
duke
parents:
diff changeset
   565
      instr->visit(this);
16611
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   566
      // perform actual value numbering
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   567
      substitute(instr);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   568
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   569
489c9b5090e2 Initial load
duke
parents:
diff changeset
   570
    // remember value map for successors
489c9b5090e2 Initial load
duke
parents:
diff changeset
   571
    set_value_map_of(block, current_map());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   572
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   573
16611
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   574
  if (_has_substitutions) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   575
    SubstitutionResolver resolver(ir);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   576
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   577
489c9b5090e2 Initial load
duke
parents:
diff changeset
   578
  TRACE_VALUE_NUMBERING(tty->print("****** end of global value numbering. "); ValueMap::print_statistics());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   579
}
16611
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   580
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   581
void GlobalValueNumbering::substitute(Instruction* instr) {
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   582
  assert(!instr->has_subst(), "substitution already set");
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   583
  Value subst = current_map()->find_insert(instr);
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   584
  if (subst != instr) {
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   585
    assert(!subst->has_subst(), "can't have a substitution");
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   586
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   587
    TRACE_VALUE_NUMBERING(tty->print_cr("substitution for %d set to %d", instr->id(), subst->id()));
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   588
    instr->set_subst(subst);
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   589
    _has_substitutions = true;
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   590
  }
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   591
  set_processed(instr);
6807a703dd6b 7153771: array bound check elimination for c1
roland
parents: 13963
diff changeset
   592
}