hotspot/src/share/vm/c1/c1_ValueMap.cpp
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 1 489c9b5090e2
child 5547 f4b087cbb361
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1999-2005 Sun Microsystems, Inc.  All Rights Reserved.
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
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
#include "incls/_precompiled.incl"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
#include "incls/_c1_ValueMap.cpp.incl"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
  int ValueMap::_number_of_finds = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
  int ValueMap::_number_of_hits = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
  int ValueMap::_number_of_kills = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
  #define TRACE_VALUE_NUMBERING(code) if (PrintValueNumbering) { code; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
#else
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
  #define TRACE_VALUE_NUMBERING(code)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
ValueMap::ValueMap()
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
  : _nesting(0)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
  , _entries(ValueMapInitialSize, NULL)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
  , _killed_values()
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
  , _entry_count(0)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
  NOT_PRODUCT(reset_statistics());
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
ValueMap::ValueMap(ValueMap* old)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
  : _nesting(old->_nesting + 1)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
  , _entries(old->_entries.length())
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
  , _killed_values()
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
  , _entry_count(old->_entry_count)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
  for (int i = size() - 1; i >= 0; i--) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
    _entries.at_put(i, old->entry_at(i));
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
  _killed_values.set_from(&old->_killed_values);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
void ValueMap::increase_table_size() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
  int old_size = size();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
  int new_size = old_size * 2 + 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
  ValueMapEntryList worklist(8);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
  ValueMapEntryArray new_entries(new_size, NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
  int new_entry_count = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
  TRACE_VALUE_NUMBERING(tty->print_cr("increasing table size from %d to %d", old_size, new_size));
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
  for (int i = old_size - 1; i >= 0; i--) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
    ValueMapEntry* entry;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
    for (entry = entry_at(i); entry != NULL; entry = entry->next()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
      if (!is_killed(entry->value())) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
        worklist.push(entry);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
    while (!worklist.is_empty()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
      entry = worklist.pop();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
      int new_index = entry_index(entry->hash(), new_size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
      if (entry->nesting() != nesting() && new_entries.at(new_index) != entry->next()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
        // changing entries with a lower nesting than the current nesting of the table
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
        // is not allowed because then the same entry is contained in multiple value maps.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
        // clone entry when next-pointer must be changed
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
        entry = new ValueMapEntry(entry->hash(), entry->value(), entry->nesting(), NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
      entry->set_next(new_entries.at(new_index));
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
      new_entries.at_put(new_index, entry);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
      new_entry_count++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
  _entries = new_entries;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
  _entry_count = new_entry_count;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
Value ValueMap::find_insert(Value x) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
  const intx hash = x->hash();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
  if (hash != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
    // 0 hash means: exclude from value numbering
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
    NOT_PRODUCT(_number_of_finds++);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
    for (ValueMapEntry* entry = entry_at(entry_index(hash, size())); entry != NULL; entry = entry->next()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
      if (entry->hash() == hash) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
        Value f = entry->value();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
        if (!is_killed(f) && f->is_equal(x)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
          NOT_PRODUCT(_number_of_hits++);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
          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
   119
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
          if (entry->nesting() != nesting() && f->as_Constant() == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
            // non-constant values of of another block must be pinned,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
            // otherwise it is possible that they are not evaluated
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
            f->pin(Instruction::PinGlobalValueNumbering);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
          }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
          return f;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
    // x not found, so insert it
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
    if (entry_count() >= size_threshold()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
      increase_table_size();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
    int idx = entry_index(hash, size());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
    _entries.at_put(idx, new ValueMapEntry(hash, x, nesting(), entry_at(idx)));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
    _entry_count++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
    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
   141
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
  return x;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
#define GENERIC_KILL_VALUE(must_kill_implementation)                                     \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
  NOT_PRODUCT(_number_of_kills++);                                                       \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
                                                                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
  for (int i = size() - 1; i >= 0; i--) {                                                \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
    ValueMapEntry* prev_entry = NULL;                                                    \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
    for (ValueMapEntry* entry = entry_at(i); entry != NULL; entry = entry->next()) {     \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
      Value value = entry->value();                                                      \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
                                                                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
      must_kill_implementation(must_kill, entry, value)                                  \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
                                                                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
      if (must_kill) {                                                                   \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
        kill_value(value);                                                               \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
                                                                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
        if (prev_entry == NULL) {                                                        \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
          _entries.at_put(i, entry->next());                                             \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
          _entry_count--;                                                                \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
        } else if (prev_entry->nesting() == nesting()) {                                 \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
          prev_entry->set_next(entry->next());                                           \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
          _entry_count--;                                                                \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
        } else {                                                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
          prev_entry = entry;                                                            \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
        }                                                                                \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
                                                                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
        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
   171
      } else {                                                                           \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
        prev_entry = entry;                                                              \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
      }                                                                                  \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
    }                                                                                    \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
  }                                                                                      \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
#define MUST_KILL_MEMORY(must_kill, entry, value)                                        \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
  bool must_kill = value->as_LoadField() != NULL || value->as_LoadIndexed() != NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
#define MUST_KILL_ARRAY(must_kill, entry, value)                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
  bool must_kill = value->as_LoadIndexed() != NULL                                       \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
                   && value->type()->tag() == type->tag();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
#define MUST_KILL_FIELD(must_kill, entry, value)                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
  /* ciField's are not unique; must compare their contents */                            \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
  LoadField* lf = value->as_LoadField();                                                 \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
  bool must_kill = lf != NULL                                                            \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
                   && lf->field()->holder() == field->holder()                           \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
                   && lf->field()->offset() == field->offset();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
#define MUST_KILL_EXCEPTION(must_kill, entry, value)                                     \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
  assert(entry->nesting() < nesting(), "must not find bigger nesting than current");     \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
  bool must_kill = (entry->nesting() == nesting() - 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
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
void ValueMap::kill_field(ciField* field) {
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_exception() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
  GENERIC_KILL_VALUE(MUST_KILL_EXCEPTION);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
}
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_map(ValueMap* map) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
  assert(is_global_value_numbering(), "only for global value numbering");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
  _killed_values.set_union(&map->_killed_values);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
void ValueMap::kill_all() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
  assert(is_local_value_numbering(), "only for local value numbering");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
  for (int i = size() - 1; i >= 0; i--) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
    _entries.at_put(i, NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
  _entry_count = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
void ValueMap::print() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
  tty->print_cr("(size %d, entries %d, nesting %d)", size(), entry_count(), nesting());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
  int entries = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
  for (int i = 0; i < size(); i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
    if (entry_at(i) != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
      tty->print("  %2d: ", i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
      for (ValueMapEntry* entry = entry_at(i); entry != NULL; entry = entry->next()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
        Value value = entry->value();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
        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
   239
        entries++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
      tty->print_cr("NULL");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
  _killed_values.print();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
  assert(entry_count() == entries, "entry_count incorrect");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
void ValueMap::reset_statistics() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
  _number_of_finds = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
  _number_of_hits = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
  _number_of_kills = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
void ValueMap::print_statistics() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
  float hit_rate = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
  if (_number_of_finds != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
    hit_rate = (float)_number_of_hits / _number_of_finds;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
  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
   262
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
class ShortLoopOptimizer : public ValueNumberingVisitor {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
  GlobalValueNumbering* _gvn;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
  BlockList             _loop_blocks;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
  bool                  _too_complicated_loop;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
  // simplified access to methods of GlobalValueNumbering
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
  ValueMap* current_map()                        { return _gvn->current_map(); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
  ValueMap* value_map_of(BlockBegin* block)      { return _gvn->value_map_of(block); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
  // implementation for abstract methods of ValueNumberingVisitor
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
  void      kill_memory()                        { _too_complicated_loop = true; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
  void      kill_field(ciField* field)           { current_map()->kill_field(field); };
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
  void      kill_array(ValueType* type)          { current_map()->kill_array(type); };
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   284
  ShortLoopOptimizer(GlobalValueNumbering* gvn)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
    : _gvn(gvn)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
    , _loop_blocks(ValueMapMaxLoopSize)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
    , _too_complicated_loop(false)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
  {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
  bool process(BlockBegin* loop_header);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
489c9b5090e2 Initial load
duke
parents:
diff changeset
   294
489c9b5090e2 Initial load
duke
parents:
diff changeset
   295
bool ShortLoopOptimizer::process(BlockBegin* loop_header) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
  TRACE_VALUE_NUMBERING(tty->print_cr("** loop header block"));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
489c9b5090e2 Initial load
duke
parents:
diff changeset
   298
  _too_complicated_loop = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
  _loop_blocks.clear();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   300
  _loop_blocks.append(loop_header);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   301
489c9b5090e2 Initial load
duke
parents:
diff changeset
   302
  for (int i = 0; i < _loop_blocks.length(); i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   303
    BlockBegin* block = _loop_blocks.at(i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   304
    TRACE_VALUE_NUMBERING(tty->print_cr("processing loop block B%d", block->block_id()));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   305
489c9b5090e2 Initial load
duke
parents:
diff changeset
   306
    if (block->is_set(BlockBegin::exception_entry_flag)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   307
      // this would be too complicated
489c9b5090e2 Initial load
duke
parents:
diff changeset
   308
      return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   310
489c9b5090e2 Initial load
duke
parents:
diff changeset
   311
    // add predecessors to worklist
489c9b5090e2 Initial load
duke
parents:
diff changeset
   312
    for (int j = block->number_of_preds() - 1; j >= 0; j--) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   313
      BlockBegin* pred = block->pred_at(j);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   314
489c9b5090e2 Initial load
duke
parents:
diff changeset
   315
      ValueMap* pred_map = value_map_of(pred);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   316
      if (pred_map != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   317
        current_map()->kill_map(pred_map);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   318
      } else if (!_loop_blocks.contains(pred)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   319
        if (_loop_blocks.length() >= ValueMapMaxLoopSize) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   320
          return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   321
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   322
        _loop_blocks.append(pred);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   323
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   324
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   325
489c9b5090e2 Initial load
duke
parents:
diff changeset
   326
    // use the instruction visitor for killing values
489c9b5090e2 Initial load
duke
parents:
diff changeset
   327
    for (Value instr = block->next(); instr != NULL; instr = instr->next()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   328
      instr->visit(this);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   329
      if (_too_complicated_loop) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   330
        return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   331
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   332
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   333
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   334
489c9b5090e2 Initial load
duke
parents:
diff changeset
   335
  TRACE_VALUE_NUMBERING(tty->print_cr("** loop successfully optimized"));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   336
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   337
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   338
489c9b5090e2 Initial load
duke
parents:
diff changeset
   339
489c9b5090e2 Initial load
duke
parents:
diff changeset
   340
GlobalValueNumbering::GlobalValueNumbering(IR* ir)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   341
  : _current_map(NULL)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   342
  , _value_maps(ir->linear_scan_order()->length(), NULL)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   343
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
   344
  TRACE_VALUE_NUMBERING(tty->print_cr("****** start of global value numbering"));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   345
489c9b5090e2 Initial load
duke
parents:
diff changeset
   346
  ShortLoopOptimizer short_loop_optimizer(this);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   347
  int subst_count = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   348
489c9b5090e2 Initial load
duke
parents:
diff changeset
   349
  BlockList* blocks = ir->linear_scan_order();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   350
  int num_blocks = blocks->length();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   351
489c9b5090e2 Initial load
duke
parents:
diff changeset
   352
  BlockBegin* start_block = blocks->at(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   353
  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
   354
  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
   355
489c9b5090e2 Initial load
duke
parents:
diff changeset
   356
  // initial, empty value map with nesting 0
489c9b5090e2 Initial load
duke
parents:
diff changeset
   357
  set_value_map_of(start_block, new ValueMap());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   358
489c9b5090e2 Initial load
duke
parents:
diff changeset
   359
  for (int i = 1; i < num_blocks; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   360
    BlockBegin* block = blocks->at(i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   361
    TRACE_VALUE_NUMBERING(tty->print_cr("**** processing block B%d", block->block_id()));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   362
489c9b5090e2 Initial load
duke
parents:
diff changeset
   363
    int num_preds = block->number_of_preds();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   364
    assert(num_preds > 0, "block must have predecessors");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   365
489c9b5090e2 Initial load
duke
parents:
diff changeset
   366
    BlockBegin* dominator = block->dominator();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   367
    assert(dominator != NULL, "dominator must exist");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   368
    assert(value_map_of(dominator) != NULL, "value map of dominator must exist");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   369
489c9b5090e2 Initial load
duke
parents:
diff changeset
   370
    // create new value map with increased nesting
489c9b5090e2 Initial load
duke
parents:
diff changeset
   371
    _current_map = new ValueMap(value_map_of(dominator));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   372
489c9b5090e2 Initial load
duke
parents:
diff changeset
   373
    if (num_preds == 1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   374
      assert(dominator == block->pred_at(0), "dominator must be equal to predecessor");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   375
      // nothing to do here
489c9b5090e2 Initial load
duke
parents:
diff changeset
   376
489c9b5090e2 Initial load
duke
parents:
diff changeset
   377
    } else if (block->is_set(BlockBegin::linear_scan_loop_header_flag)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   378
      // block has incoming backward branches -> try to optimize short loops
489c9b5090e2 Initial load
duke
parents:
diff changeset
   379
      if (!short_loop_optimizer.process(block)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   380
        // loop is too complicated, so kill all memory loads because there might be
489c9b5090e2 Initial load
duke
parents:
diff changeset
   381
        // stores to them in the loop
489c9b5090e2 Initial load
duke
parents:
diff changeset
   382
        current_map()->kill_memory();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   383
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   384
489c9b5090e2 Initial load
duke
parents:
diff changeset
   385
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   386
      // only incoming forward branches that are already processed
489c9b5090e2 Initial load
duke
parents:
diff changeset
   387
      for (int j = 0; j < num_preds; j++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   388
        BlockBegin* pred = block->pred_at(j);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   389
        ValueMap* pred_map = value_map_of(pred);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   390
489c9b5090e2 Initial load
duke
parents:
diff changeset
   391
        if (pred_map != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   392
          // propagate killed values of the predecessor to this block
489c9b5090e2 Initial load
duke
parents:
diff changeset
   393
          current_map()->kill_map(value_map_of(pred));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   394
        } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   395
          // kill all memory loads because predecessor not yet processed
489c9b5090e2 Initial load
duke
parents:
diff changeset
   396
          // (this can happen with non-natural loops and OSR-compiles)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   397
          current_map()->kill_memory();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   398
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   399
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   400
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   401
489c9b5090e2 Initial load
duke
parents:
diff changeset
   402
    if (block->is_set(BlockBegin::exception_entry_flag)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   403
      current_map()->kill_exception();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   404
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   405
489c9b5090e2 Initial load
duke
parents:
diff changeset
   406
    TRACE_VALUE_NUMBERING(tty->print("value map before processing block: "); current_map()->print());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   407
489c9b5090e2 Initial load
duke
parents:
diff changeset
   408
    // visit all instructions of this block
489c9b5090e2 Initial load
duke
parents:
diff changeset
   409
    for (Value instr = block->next(); instr != NULL; instr = instr->next()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   410
      assert(!instr->has_subst(), "substitution already set");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   411
489c9b5090e2 Initial load
duke
parents:
diff changeset
   412
      // check if instruction kills any values
489c9b5090e2 Initial load
duke
parents:
diff changeset
   413
      instr->visit(this);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   414
489c9b5090e2 Initial load
duke
parents:
diff changeset
   415
      if (instr->hash() != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   416
        Value f = current_map()->find_insert(instr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   417
        if (f != instr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   418
          assert(!f->has_subst(), "can't have a substitution");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   419
          instr->set_subst(f);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   420
          subst_count++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   421
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   422
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   423
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   424
489c9b5090e2 Initial load
duke
parents:
diff changeset
   425
    // remember value map for successors
489c9b5090e2 Initial load
duke
parents:
diff changeset
   426
    set_value_map_of(block, current_map());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   427
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   428
489c9b5090e2 Initial load
duke
parents:
diff changeset
   429
  if (subst_count != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   430
    SubstitutionResolver resolver(ir);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   431
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   432
489c9b5090e2 Initial load
duke
parents:
diff changeset
   433
  TRACE_VALUE_NUMBERING(tty->print("****** end of global value numbering. "); ValueMap::print_statistics());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   434
}