src/share/vm/c1/c1_ValueMap.cpp

Wed, 22 Aug 2012 14:29:57 +0200

author
roland
date
Wed, 22 Aug 2012 14:29:57 +0200
changeset 4005
0bfcb7a3e12d
parent 3498
f067b4e0e04b
child 4153
b9a9ed0f8eeb
permissions
-rw-r--r--

7171824: assert(_offset >= 1) failed: illegal call to offset()
Summary: C1 value numbering hits unloaded klass.
Reviewed-by: kvn, twisti

duke@435 1 /*
stefank@2314 2 * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
stefank@2314 25 #include "precompiled.hpp"
stefank@2314 26 #include "c1/c1_Canonicalizer.hpp"
stefank@2314 27 #include "c1/c1_IR.hpp"
stefank@2314 28 #include "c1/c1_ValueMap.hpp"
stefank@2314 29 #include "utilities/bitMap.inline.hpp"
duke@435 30
duke@435 31
duke@435 32 #ifndef PRODUCT
duke@435 33
duke@435 34 int ValueMap::_number_of_finds = 0;
duke@435 35 int ValueMap::_number_of_hits = 0;
duke@435 36 int ValueMap::_number_of_kills = 0;
duke@435 37
duke@435 38 #define TRACE_VALUE_NUMBERING(code) if (PrintValueNumbering) { code; }
duke@435 39
duke@435 40 #else
duke@435 41
duke@435 42 #define TRACE_VALUE_NUMBERING(code)
duke@435 43
duke@435 44 #endif
duke@435 45
duke@435 46
duke@435 47 ValueMap::ValueMap()
duke@435 48 : _nesting(0)
duke@435 49 , _entries(ValueMapInitialSize, NULL)
duke@435 50 , _killed_values()
duke@435 51 , _entry_count(0)
duke@435 52 {
duke@435 53 NOT_PRODUCT(reset_statistics());
duke@435 54 }
duke@435 55
duke@435 56
duke@435 57 ValueMap::ValueMap(ValueMap* old)
duke@435 58 : _nesting(old->_nesting + 1)
duke@435 59 , _entries(old->_entries.length())
duke@435 60 , _killed_values()
duke@435 61 , _entry_count(old->_entry_count)
duke@435 62 {
duke@435 63 for (int i = size() - 1; i >= 0; i--) {
duke@435 64 _entries.at_put(i, old->entry_at(i));
duke@435 65 }
duke@435 66 _killed_values.set_from(&old->_killed_values);
duke@435 67 }
duke@435 68
duke@435 69
duke@435 70 void ValueMap::increase_table_size() {
duke@435 71 int old_size = size();
duke@435 72 int new_size = old_size * 2 + 1;
duke@435 73
duke@435 74 ValueMapEntryList worklist(8);
duke@435 75 ValueMapEntryArray new_entries(new_size, NULL);
duke@435 76 int new_entry_count = 0;
duke@435 77
duke@435 78 TRACE_VALUE_NUMBERING(tty->print_cr("increasing table size from %d to %d", old_size, new_size));
duke@435 79
duke@435 80 for (int i = old_size - 1; i >= 0; i--) {
duke@435 81 ValueMapEntry* entry;
duke@435 82 for (entry = entry_at(i); entry != NULL; entry = entry->next()) {
duke@435 83 if (!is_killed(entry->value())) {
duke@435 84 worklist.push(entry);
duke@435 85 }
duke@435 86 }
duke@435 87
duke@435 88 while (!worklist.is_empty()) {
duke@435 89 entry = worklist.pop();
duke@435 90 int new_index = entry_index(entry->hash(), new_size);
duke@435 91
duke@435 92 if (entry->nesting() != nesting() && new_entries.at(new_index) != entry->next()) {
duke@435 93 // changing entries with a lower nesting than the current nesting of the table
duke@435 94 // is not allowed because then the same entry is contained in multiple value maps.
duke@435 95 // clone entry when next-pointer must be changed
duke@435 96 entry = new ValueMapEntry(entry->hash(), entry->value(), entry->nesting(), NULL);
duke@435 97 }
duke@435 98 entry->set_next(new_entries.at(new_index));
duke@435 99 new_entries.at_put(new_index, entry);
duke@435 100 new_entry_count++;
duke@435 101 }
duke@435 102 }
duke@435 103
duke@435 104 _entries = new_entries;
duke@435 105 _entry_count = new_entry_count;
duke@435 106 }
duke@435 107
duke@435 108
duke@435 109 Value ValueMap::find_insert(Value x) {
duke@435 110 const intx hash = x->hash();
duke@435 111 if (hash != 0) {
duke@435 112 // 0 hash means: exclude from value numbering
duke@435 113 NOT_PRODUCT(_number_of_finds++);
duke@435 114
duke@435 115 for (ValueMapEntry* entry = entry_at(entry_index(hash, size())); entry != NULL; entry = entry->next()) {
duke@435 116 if (entry->hash() == hash) {
duke@435 117 Value f = entry->value();
duke@435 118
duke@435 119 if (!is_killed(f) && f->is_equal(x)) {
duke@435 120 NOT_PRODUCT(_number_of_hits++);
duke@435 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()));
duke@435 122
duke@435 123 if (entry->nesting() != nesting() && f->as_Constant() == NULL) {
duke@435 124 // non-constant values of of another block must be pinned,
duke@435 125 // otherwise it is possible that they are not evaluated
duke@435 126 f->pin(Instruction::PinGlobalValueNumbering);
duke@435 127 }
roland@3498 128 assert(x->type()->tag() == f->type()->tag(), "should have same type");
duke@435 129
duke@435 130 return f;
duke@435 131
duke@435 132 }
duke@435 133 }
duke@435 134 }
duke@435 135
duke@435 136 // x not found, so insert it
duke@435 137 if (entry_count() >= size_threshold()) {
duke@435 138 increase_table_size();
duke@435 139 }
duke@435 140 int idx = entry_index(hash, size());
duke@435 141 _entries.at_put(idx, new ValueMapEntry(hash, x, nesting(), entry_at(idx)));
duke@435 142 _entry_count++;
duke@435 143
duke@435 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()));
duke@435 145 }
duke@435 146
duke@435 147 return x;
duke@435 148 }
duke@435 149
duke@435 150
duke@435 151 #define GENERIC_KILL_VALUE(must_kill_implementation) \
duke@435 152 NOT_PRODUCT(_number_of_kills++); \
duke@435 153 \
duke@435 154 for (int i = size() - 1; i >= 0; i--) { \
duke@435 155 ValueMapEntry* prev_entry = NULL; \
duke@435 156 for (ValueMapEntry* entry = entry_at(i); entry != NULL; entry = entry->next()) { \
duke@435 157 Value value = entry->value(); \
duke@435 158 \
duke@435 159 must_kill_implementation(must_kill, entry, value) \
duke@435 160 \
duke@435 161 if (must_kill) { \
duke@435 162 kill_value(value); \
duke@435 163 \
duke@435 164 if (prev_entry == NULL) { \
duke@435 165 _entries.at_put(i, entry->next()); \
duke@435 166 _entry_count--; \
duke@435 167 } else if (prev_entry->nesting() == nesting()) { \
duke@435 168 prev_entry->set_next(entry->next()); \
duke@435 169 _entry_count--; \
duke@435 170 } else { \
duke@435 171 prev_entry = entry; \
duke@435 172 } \
duke@435 173 \
duke@435 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())); \
duke@435 175 } else { \
duke@435 176 prev_entry = entry; \
duke@435 177 } \
duke@435 178 } \
duke@435 179 } \
duke@435 180
duke@435 181 #define MUST_KILL_MEMORY(must_kill, entry, value) \
duke@435 182 bool must_kill = value->as_LoadField() != NULL || value->as_LoadIndexed() != NULL;
duke@435 183
duke@435 184 #define MUST_KILL_ARRAY(must_kill, entry, value) \
duke@435 185 bool must_kill = value->as_LoadIndexed() != NULL \
duke@435 186 && value->type()->tag() == type->tag();
duke@435 187
duke@435 188 #define MUST_KILL_FIELD(must_kill, entry, value) \
duke@435 189 /* ciField's are not unique; must compare their contents */ \
duke@435 190 LoadField* lf = value->as_LoadField(); \
duke@435 191 bool must_kill = lf != NULL \
duke@435 192 && lf->field()->holder() == field->holder() \
roland@4005 193 && (all_offsets || lf->field()->offset() == field->offset());
duke@435 194
duke@435 195 #define MUST_KILL_EXCEPTION(must_kill, entry, value) \
duke@435 196 assert(entry->nesting() < nesting(), "must not find bigger nesting than current"); \
duke@435 197 bool must_kill = (entry->nesting() == nesting() - 1);
duke@435 198
duke@435 199
duke@435 200 void ValueMap::kill_memory() {
duke@435 201 GENERIC_KILL_VALUE(MUST_KILL_MEMORY);
duke@435 202 }
duke@435 203
duke@435 204 void ValueMap::kill_array(ValueType* type) {
duke@435 205 GENERIC_KILL_VALUE(MUST_KILL_ARRAY);
duke@435 206 }
duke@435 207
roland@4005 208 void ValueMap::kill_field(ciField* field, bool all_offsets) {
duke@435 209 GENERIC_KILL_VALUE(MUST_KILL_FIELD);
duke@435 210 }
duke@435 211
duke@435 212 void ValueMap::kill_exception() {
duke@435 213 GENERIC_KILL_VALUE(MUST_KILL_EXCEPTION);
duke@435 214 }
duke@435 215
duke@435 216
duke@435 217 void ValueMap::kill_map(ValueMap* map) {
duke@435 218 assert(is_global_value_numbering(), "only for global value numbering");
duke@435 219 _killed_values.set_union(&map->_killed_values);
duke@435 220 }
duke@435 221
duke@435 222 void ValueMap::kill_all() {
duke@435 223 assert(is_local_value_numbering(), "only for local value numbering");
duke@435 224 for (int i = size() - 1; i >= 0; i--) {
duke@435 225 _entries.at_put(i, NULL);
duke@435 226 }
duke@435 227 _entry_count = 0;
duke@435 228 }
duke@435 229
duke@435 230
duke@435 231 #ifndef PRODUCT
duke@435 232
duke@435 233 void ValueMap::print() {
duke@435 234 tty->print_cr("(size %d, entries %d, nesting %d)", size(), entry_count(), nesting());
duke@435 235
duke@435 236 int entries = 0;
duke@435 237 for (int i = 0; i < size(); i++) {
duke@435 238 if (entry_at(i) != NULL) {
duke@435 239 tty->print(" %2d: ", i);
duke@435 240 for (ValueMapEntry* entry = entry_at(i); entry != NULL; entry = entry->next()) {
duke@435 241 Value value = entry->value();
duke@435 242 tty->print("%s %c%d (%s%d) -> ", value->name(), value->type()->tchar(), value->id(), is_killed(value) ? "x" : "", entry->nesting());
duke@435 243 entries++;
duke@435 244 }
duke@435 245 tty->print_cr("NULL");
duke@435 246 }
duke@435 247 }
duke@435 248
duke@435 249 _killed_values.print();
duke@435 250 assert(entry_count() == entries, "entry_count incorrect");
duke@435 251 }
duke@435 252
duke@435 253 void ValueMap::reset_statistics() {
duke@435 254 _number_of_finds = 0;
duke@435 255 _number_of_hits = 0;
duke@435 256 _number_of_kills = 0;
duke@435 257 }
duke@435 258
duke@435 259 void ValueMap::print_statistics() {
duke@435 260 float hit_rate = 0;
duke@435 261 if (_number_of_finds != 0) {
duke@435 262 hit_rate = (float)_number_of_hits / _number_of_finds;
duke@435 263 }
duke@435 264
duke@435 265 tty->print_cr("finds:%3d hits:%3d kills:%3d hit rate: %1.4f", _number_of_finds, _number_of_hits, _number_of_kills, hit_rate);
duke@435 266 }
duke@435 267
duke@435 268 #endif
duke@435 269
duke@435 270
duke@435 271
duke@435 272 class ShortLoopOptimizer : public ValueNumberingVisitor {
duke@435 273 private:
duke@435 274 GlobalValueNumbering* _gvn;
duke@435 275 BlockList _loop_blocks;
duke@435 276 bool _too_complicated_loop;
duke@435 277
duke@435 278 // simplified access to methods of GlobalValueNumbering
duke@435 279 ValueMap* current_map() { return _gvn->current_map(); }
duke@435 280 ValueMap* value_map_of(BlockBegin* block) { return _gvn->value_map_of(block); }
duke@435 281
duke@435 282 // implementation for abstract methods of ValueNumberingVisitor
roland@4005 283 void kill_memory() { _too_complicated_loop = true; }
roland@4005 284 void kill_field(ciField* field, bool all_offsets) { current_map()->kill_field(field, all_offsets); };
roland@4005 285 void kill_array(ValueType* type) { current_map()->kill_array(type); };
duke@435 286
duke@435 287 public:
duke@435 288 ShortLoopOptimizer(GlobalValueNumbering* gvn)
duke@435 289 : _gvn(gvn)
duke@435 290 , _loop_blocks(ValueMapMaxLoopSize)
duke@435 291 , _too_complicated_loop(false)
duke@435 292 {
duke@435 293 }
duke@435 294
duke@435 295 bool process(BlockBegin* loop_header);
duke@435 296 };
duke@435 297
duke@435 298
duke@435 299 bool ShortLoopOptimizer::process(BlockBegin* loop_header) {
duke@435 300 TRACE_VALUE_NUMBERING(tty->print_cr("** loop header block"));
duke@435 301
duke@435 302 _too_complicated_loop = false;
duke@435 303 _loop_blocks.clear();
duke@435 304 _loop_blocks.append(loop_header);
duke@435 305
duke@435 306 for (int i = 0; i < _loop_blocks.length(); i++) {
duke@435 307 BlockBegin* block = _loop_blocks.at(i);
duke@435 308 TRACE_VALUE_NUMBERING(tty->print_cr("processing loop block B%d", block->block_id()));
duke@435 309
duke@435 310 if (block->is_set(BlockBegin::exception_entry_flag)) {
duke@435 311 // this would be too complicated
duke@435 312 return false;
duke@435 313 }
duke@435 314
duke@435 315 // add predecessors to worklist
duke@435 316 for (int j = block->number_of_preds() - 1; j >= 0; j--) {
duke@435 317 BlockBegin* pred = block->pred_at(j);
duke@435 318
duke@435 319 ValueMap* pred_map = value_map_of(pred);
duke@435 320 if (pred_map != NULL) {
duke@435 321 current_map()->kill_map(pred_map);
duke@435 322 } else if (!_loop_blocks.contains(pred)) {
duke@435 323 if (_loop_blocks.length() >= ValueMapMaxLoopSize) {
duke@435 324 return false;
duke@435 325 }
duke@435 326 _loop_blocks.append(pred);
duke@435 327 }
duke@435 328 }
duke@435 329
duke@435 330 // use the instruction visitor for killing values
duke@435 331 for (Value instr = block->next(); instr != NULL; instr = instr->next()) {
duke@435 332 instr->visit(this);
duke@435 333 if (_too_complicated_loop) {
duke@435 334 return false;
duke@435 335 }
duke@435 336 }
duke@435 337 }
duke@435 338
duke@435 339 TRACE_VALUE_NUMBERING(tty->print_cr("** loop successfully optimized"));
duke@435 340 return true;
duke@435 341 }
duke@435 342
duke@435 343
duke@435 344 GlobalValueNumbering::GlobalValueNumbering(IR* ir)
duke@435 345 : _current_map(NULL)
duke@435 346 , _value_maps(ir->linear_scan_order()->length(), NULL)
duke@435 347 {
duke@435 348 TRACE_VALUE_NUMBERING(tty->print_cr("****** start of global value numbering"));
duke@435 349
duke@435 350 ShortLoopOptimizer short_loop_optimizer(this);
duke@435 351 int subst_count = 0;
duke@435 352
duke@435 353 BlockList* blocks = ir->linear_scan_order();
duke@435 354 int num_blocks = blocks->length();
duke@435 355
duke@435 356 BlockBegin* start_block = blocks->at(0);
duke@435 357 assert(start_block == ir->start() && start_block->number_of_preds() == 0 && start_block->dominator() == NULL, "must be start block");
duke@435 358 assert(start_block->next()->as_Base() != NULL && start_block->next()->next() == NULL, "start block must not have instructions");
duke@435 359
duke@435 360 // initial, empty value map with nesting 0
duke@435 361 set_value_map_of(start_block, new ValueMap());
duke@435 362
duke@435 363 for (int i = 1; i < num_blocks; i++) {
duke@435 364 BlockBegin* block = blocks->at(i);
duke@435 365 TRACE_VALUE_NUMBERING(tty->print_cr("**** processing block B%d", block->block_id()));
duke@435 366
duke@435 367 int num_preds = block->number_of_preds();
duke@435 368 assert(num_preds > 0, "block must have predecessors");
duke@435 369
duke@435 370 BlockBegin* dominator = block->dominator();
duke@435 371 assert(dominator != NULL, "dominator must exist");
duke@435 372 assert(value_map_of(dominator) != NULL, "value map of dominator must exist");
duke@435 373
duke@435 374 // create new value map with increased nesting
duke@435 375 _current_map = new ValueMap(value_map_of(dominator));
duke@435 376
duke@435 377 if (num_preds == 1) {
duke@435 378 assert(dominator == block->pred_at(0), "dominator must be equal to predecessor");
duke@435 379 // nothing to do here
duke@435 380
duke@435 381 } else if (block->is_set(BlockBegin::linear_scan_loop_header_flag)) {
duke@435 382 // block has incoming backward branches -> try to optimize short loops
duke@435 383 if (!short_loop_optimizer.process(block)) {
duke@435 384 // loop is too complicated, so kill all memory loads because there might be
duke@435 385 // stores to them in the loop
duke@435 386 current_map()->kill_memory();
duke@435 387 }
duke@435 388
duke@435 389 } else {
duke@435 390 // only incoming forward branches that are already processed
duke@435 391 for (int j = 0; j < num_preds; j++) {
duke@435 392 BlockBegin* pred = block->pred_at(j);
duke@435 393 ValueMap* pred_map = value_map_of(pred);
duke@435 394
duke@435 395 if (pred_map != NULL) {
duke@435 396 // propagate killed values of the predecessor to this block
duke@435 397 current_map()->kill_map(value_map_of(pred));
duke@435 398 } else {
duke@435 399 // kill all memory loads because predecessor not yet processed
duke@435 400 // (this can happen with non-natural loops and OSR-compiles)
duke@435 401 current_map()->kill_memory();
duke@435 402 }
duke@435 403 }
duke@435 404 }
duke@435 405
duke@435 406 if (block->is_set(BlockBegin::exception_entry_flag)) {
duke@435 407 current_map()->kill_exception();
duke@435 408 }
duke@435 409
duke@435 410 TRACE_VALUE_NUMBERING(tty->print("value map before processing block: "); current_map()->print());
duke@435 411
duke@435 412 // visit all instructions of this block
duke@435 413 for (Value instr = block->next(); instr != NULL; instr = instr->next()) {
duke@435 414 assert(!instr->has_subst(), "substitution already set");
duke@435 415
duke@435 416 // check if instruction kills any values
duke@435 417 instr->visit(this);
duke@435 418
duke@435 419 if (instr->hash() != 0) {
duke@435 420 Value f = current_map()->find_insert(instr);
duke@435 421 if (f != instr) {
duke@435 422 assert(!f->has_subst(), "can't have a substitution");
duke@435 423 instr->set_subst(f);
duke@435 424 subst_count++;
duke@435 425 }
duke@435 426 }
duke@435 427 }
duke@435 428
duke@435 429 // remember value map for successors
duke@435 430 set_value_map_of(block, current_map());
duke@435 431 }
duke@435 432
duke@435 433 if (subst_count != 0) {
duke@435 434 SubstitutionResolver resolver(ir);
duke@435 435 }
duke@435 436
duke@435 437 TRACE_VALUE_NUMBERING(tty->print("****** end of global value numbering. "); ValueMap::print_statistics());
duke@435 438 }

mercurial