src/share/vm/c1/c1_ValueMap.cpp

Tue, 24 Feb 2015 15:04:52 -0500

author
dlong
date
Tue, 24 Feb 2015 15:04:52 -0500
changeset 7598
ddce0b7cee93
parent 6198
55fb97c4c58d
child 6876
710a3c8b516e
permissions
-rw-r--r--

8072383: resolve conflicts between open and closed ports
Summary: refactor close to remove references to closed ports
Reviewed-by: kvn, simonis, sgehwolf, dholmes

duke@435 1 /*
mikael@6198 2 * Copyright (c) 1999, 2013, 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"
roland@4860 29 #include "c1/c1_ValueStack.hpp"
stefank@2314 30 #include "utilities/bitMap.inline.hpp"
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
duke@435 196 void ValueMap::kill_memory() {
duke@435 197 GENERIC_KILL_VALUE(MUST_KILL_MEMORY);
duke@435 198 }
duke@435 199
duke@435 200 void ValueMap::kill_array(ValueType* type) {
duke@435 201 GENERIC_KILL_VALUE(MUST_KILL_ARRAY);
duke@435 202 }
duke@435 203
roland@4005 204 void ValueMap::kill_field(ciField* field, bool all_offsets) {
duke@435 205 GENERIC_KILL_VALUE(MUST_KILL_FIELD);
duke@435 206 }
duke@435 207
duke@435 208 void ValueMap::kill_map(ValueMap* map) {
duke@435 209 assert(is_global_value_numbering(), "only for global value numbering");
duke@435 210 _killed_values.set_union(&map->_killed_values);
duke@435 211 }
duke@435 212
duke@435 213 void ValueMap::kill_all() {
duke@435 214 assert(is_local_value_numbering(), "only for local value numbering");
duke@435 215 for (int i = size() - 1; i >= 0; i--) {
duke@435 216 _entries.at_put(i, NULL);
duke@435 217 }
duke@435 218 _entry_count = 0;
duke@435 219 }
duke@435 220
duke@435 221
duke@435 222 #ifndef PRODUCT
duke@435 223
duke@435 224 void ValueMap::print() {
duke@435 225 tty->print_cr("(size %d, entries %d, nesting %d)", size(), entry_count(), nesting());
duke@435 226
duke@435 227 int entries = 0;
duke@435 228 for (int i = 0; i < size(); i++) {
duke@435 229 if (entry_at(i) != NULL) {
duke@435 230 tty->print(" %2d: ", i);
duke@435 231 for (ValueMapEntry* entry = entry_at(i); entry != NULL; entry = entry->next()) {
duke@435 232 Value value = entry->value();
duke@435 233 tty->print("%s %c%d (%s%d) -> ", value->name(), value->type()->tchar(), value->id(), is_killed(value) ? "x" : "", entry->nesting());
duke@435 234 entries++;
duke@435 235 }
duke@435 236 tty->print_cr("NULL");
duke@435 237 }
duke@435 238 }
duke@435 239
duke@435 240 _killed_values.print();
duke@435 241 assert(entry_count() == entries, "entry_count incorrect");
duke@435 242 }
duke@435 243
duke@435 244 void ValueMap::reset_statistics() {
duke@435 245 _number_of_finds = 0;
duke@435 246 _number_of_hits = 0;
duke@435 247 _number_of_kills = 0;
duke@435 248 }
duke@435 249
duke@435 250 void ValueMap::print_statistics() {
duke@435 251 float hit_rate = 0;
duke@435 252 if (_number_of_finds != 0) {
duke@435 253 hit_rate = (float)_number_of_hits / _number_of_finds;
duke@435 254 }
duke@435 255
duke@435 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);
duke@435 257 }
duke@435 258
duke@435 259 #endif
duke@435 260
duke@435 261
duke@435 262
duke@435 263 class ShortLoopOptimizer : public ValueNumberingVisitor {
duke@435 264 private:
duke@435 265 GlobalValueNumbering* _gvn;
duke@435 266 BlockList _loop_blocks;
duke@435 267 bool _too_complicated_loop;
roland@4860 268 bool _has_field_store[T_ARRAY + 1];
roland@4860 269 bool _has_indexed_store[T_ARRAY + 1];
duke@435 270
duke@435 271 // simplified access to methods of GlobalValueNumbering
duke@435 272 ValueMap* current_map() { return _gvn->current_map(); }
duke@435 273 ValueMap* value_map_of(BlockBegin* block) { return _gvn->value_map_of(block); }
duke@435 274
duke@435 275 // implementation for abstract methods of ValueNumberingVisitor
roland@4005 276 void kill_memory() { _too_complicated_loop = true; }
roland@4860 277 void kill_field(ciField* field, bool all_offsets) {
roland@4860 278 current_map()->kill_field(field, all_offsets);
roland@4860 279 assert(field->type()->basic_type() >= 0 && field->type()->basic_type() <= T_ARRAY, "Invalid type");
roland@4860 280 _has_field_store[field->type()->basic_type()] = true;
roland@4860 281 }
roland@4860 282 void kill_array(ValueType* type) {
roland@4860 283 current_map()->kill_array(type);
roland@4860 284 BasicType basic_type = as_BasicType(type); assert(basic_type >= 0 && basic_type <= T_ARRAY, "Invalid type");
roland@4860 285 _has_indexed_store[basic_type] = true;
roland@4860 286 }
duke@435 287
duke@435 288 public:
duke@435 289 ShortLoopOptimizer(GlobalValueNumbering* gvn)
duke@435 290 : _gvn(gvn)
duke@435 291 , _loop_blocks(ValueMapMaxLoopSize)
duke@435 292 , _too_complicated_loop(false)
duke@435 293 {
roland@4860 294 for (int i=0; i<= T_ARRAY; i++){
roland@4860 295 _has_field_store[i] = false;
roland@4860 296 _has_indexed_store[i] = false;
roland@4860 297 }
roland@4860 298 }
roland@4860 299
roland@4860 300 bool has_field_store(BasicType type) {
roland@4860 301 assert(type >= 0 && type <= T_ARRAY, "Invalid type");
roland@4860 302 return _has_field_store[type];
roland@4860 303 }
roland@4860 304
roland@4860 305 bool has_indexed_store(BasicType type) {
roland@4860 306 assert(type >= 0 && type <= T_ARRAY, "Invalid type");
roland@4860 307 return _has_indexed_store[type];
duke@435 308 }
duke@435 309
duke@435 310 bool process(BlockBegin* loop_header);
duke@435 311 };
duke@435 312
roland@4860 313 class LoopInvariantCodeMotion : public StackObj {
roland@4860 314 private:
roland@4860 315 GlobalValueNumbering* _gvn;
roland@4860 316 ShortLoopOptimizer* _short_loop_optimizer;
roland@4860 317 Instruction* _insertion_point;
roland@4860 318 ValueStack * _state;
roland@4909 319 bool _insert_is_pred;
roland@4860 320
roland@4860 321 void set_invariant(Value v) const { _gvn->set_processed(v); }
roland@4860 322 bool is_invariant(Value v) const { return _gvn->is_processed(v); }
roland@4860 323
roland@4860 324 void process_block(BlockBegin* block);
roland@4860 325
roland@4860 326 public:
roland@4860 327 LoopInvariantCodeMotion(ShortLoopOptimizer *slo, GlobalValueNumbering* gvn, BlockBegin* loop_header, BlockList* loop_blocks);
roland@4860 328 };
roland@4860 329
roland@4860 330 LoopInvariantCodeMotion::LoopInvariantCodeMotion(ShortLoopOptimizer *slo, GlobalValueNumbering* gvn, BlockBegin* loop_header, BlockList* loop_blocks)
roland@4860 331 : _gvn(gvn), _short_loop_optimizer(slo) {
roland@4860 332
roland@4860 333 TRACE_VALUE_NUMBERING(tty->print_cr("using loop invariant code motion loop_header = %d", loop_header->block_id()));
roland@4860 334 TRACE_VALUE_NUMBERING(tty->print_cr("** loop invariant code motion for short loop B%d", loop_header->block_id()));
roland@4860 335
roland@4860 336 BlockBegin* insertion_block = loop_header->dominator();
roland@4860 337 if (insertion_block->number_of_preds() == 0) {
roland@4860 338 return; // only the entry block does not have a predecessor
roland@4860 339 }
roland@4860 340
roland@4860 341 assert(insertion_block->end()->as_Base() == NULL, "cannot insert into entry block");
roland@4860 342 _insertion_point = insertion_block->end()->prev();
roland@4909 343 _insert_is_pred = loop_header->is_predecessor(insertion_block);
roland@4860 344
roland@4860 345 BlockEnd *block_end = insertion_block->end();
roland@4860 346 _state = block_end->state_before();
roland@4860 347
roland@4860 348 if (!_state) {
roland@4860 349 // If, TableSwitch and LookupSwitch always have state_before when
roland@4860 350 // loop invariant code motion happens..
roland@4860 351 assert(block_end->as_Goto(), "Block has to be goto");
roland@4860 352 _state = block_end->state();
roland@4860 353 }
roland@4860 354
roland@4860 355 // the loop_blocks are filled by going backward from the loop header, so this processing order is best
roland@4860 356 assert(loop_blocks->at(0) == loop_header, "loop header must be first loop block");
roland@4860 357 process_block(loop_header);
roland@4860 358 for (int i = loop_blocks->length() - 1; i >= 1; i--) {
roland@4860 359 process_block(loop_blocks->at(i));
roland@4860 360 }
roland@4860 361 }
roland@4860 362
roland@4860 363 void LoopInvariantCodeMotion::process_block(BlockBegin* block) {
roland@4860 364 TRACE_VALUE_NUMBERING(tty->print_cr("processing block B%d", block->block_id()));
roland@4860 365
roland@4860 366 Instruction* prev = block;
roland@4860 367 Instruction* cur = block->next();
roland@4860 368
roland@4860 369 while (cur != NULL) {
roland@4860 370
roland@4860 371 // determine if cur instruction is loop invariant
roland@4860 372 // only selected instruction types are processed here
roland@4860 373 bool cur_invariant = false;
roland@4860 374
roland@4860 375 if (cur->as_Constant() != NULL) {
roland@4860 376 cur_invariant = !cur->can_trap();
roland@4860 377 } else if (cur->as_ArithmeticOp() != NULL || cur->as_LogicOp() != NULL || cur->as_ShiftOp() != NULL) {
roland@4860 378 assert(cur->as_Op2() != NULL, "must be Op2");
roland@4860 379 Op2* op2 = (Op2*)cur;
roland@4860 380 cur_invariant = !op2->can_trap() && is_invariant(op2->x()) && is_invariant(op2->y());
roland@4860 381 } else if (cur->as_LoadField() != NULL) {
roland@4860 382 LoadField* lf = (LoadField*)cur;
roland@4860 383 // deoptimizes on NullPointerException
roland@4909 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;
roland@4860 385 } else if (cur->as_ArrayLength() != NULL) {
roland@4860 386 ArrayLength *length = cur->as_ArrayLength();
roland@4860 387 cur_invariant = is_invariant(length->array());
roland@4860 388 } else if (cur->as_LoadIndexed() != NULL) {
roland@4860 389 LoadIndexed *li = (LoadIndexed *)cur->as_LoadIndexed();
roland@4909 390 cur_invariant = !_short_loop_optimizer->has_indexed_store(as_BasicType(cur->type())) && is_invariant(li->array()) && is_invariant(li->index()) && _insert_is_pred;
roland@4860 391 }
roland@4860 392
roland@4860 393 if (cur_invariant) {
roland@4860 394 // perform value numbering and mark instruction as loop-invariant
roland@4860 395 _gvn->substitute(cur);
roland@4860 396
roland@4860 397 if (cur->as_Constant() == NULL) {
roland@4860 398 // ensure that code for non-constant instructions is always generated
roland@4860 399 cur->pin();
roland@4860 400 }
roland@4860 401
roland@4860 402 // remove cur instruction from loop block and append it to block before loop
roland@4860 403 Instruction* next = cur->next();
roland@4860 404 Instruction* in = _insertion_point->next();
roland@4860 405 _insertion_point = _insertion_point->set_next(cur);
roland@4860 406 cur->set_next(in);
roland@4860 407
roland@4860 408 // Deoptimize on exception
roland@4860 409 cur->set_flag(Instruction::DeoptimizeOnException, true);
roland@4860 410
roland@4860 411 // Clear exception handlers
roland@4860 412 cur->set_exception_handlers(NULL);
roland@4860 413
roland@4860 414 TRACE_VALUE_NUMBERING(tty->print_cr("Instruction %c%d is loop invariant", cur->type()->tchar(), cur->id()));
roland@4860 415
roland@4860 416 if (cur->state_before() != NULL) {
roland@4860 417 cur->set_state_before(_state->copy());
roland@4860 418 }
roland@4860 419 if (cur->exception_state() != NULL) {
roland@4860 420 cur->set_exception_state(_state->copy());
roland@4860 421 }
roland@4860 422
roland@4860 423 cur = prev->set_next(next);
roland@4860 424
roland@4860 425 } else {
roland@4860 426 prev = cur;
roland@4860 427 cur = cur->next();
roland@4860 428 }
roland@4860 429 }
roland@4860 430 }
duke@435 431
duke@435 432 bool ShortLoopOptimizer::process(BlockBegin* loop_header) {
duke@435 433 TRACE_VALUE_NUMBERING(tty->print_cr("** loop header block"));
duke@435 434
duke@435 435 _too_complicated_loop = false;
duke@435 436 _loop_blocks.clear();
duke@435 437 _loop_blocks.append(loop_header);
duke@435 438
duke@435 439 for (int i = 0; i < _loop_blocks.length(); i++) {
duke@435 440 BlockBegin* block = _loop_blocks.at(i);
duke@435 441 TRACE_VALUE_NUMBERING(tty->print_cr("processing loop block B%d", block->block_id()));
duke@435 442
duke@435 443 if (block->is_set(BlockBegin::exception_entry_flag)) {
duke@435 444 // this would be too complicated
duke@435 445 return false;
duke@435 446 }
duke@435 447
duke@435 448 // add predecessors to worklist
duke@435 449 for (int j = block->number_of_preds() - 1; j >= 0; j--) {
duke@435 450 BlockBegin* pred = block->pred_at(j);
duke@435 451
roland@4860 452 if (pred->is_set(BlockBegin::osr_entry_flag)) {
roland@4860 453 return false;
roland@4860 454 }
roland@4860 455
duke@435 456 ValueMap* pred_map = value_map_of(pred);
duke@435 457 if (pred_map != NULL) {
duke@435 458 current_map()->kill_map(pred_map);
duke@435 459 } else if (!_loop_blocks.contains(pred)) {
duke@435 460 if (_loop_blocks.length() >= ValueMapMaxLoopSize) {
duke@435 461 return false;
duke@435 462 }
duke@435 463 _loop_blocks.append(pred);
duke@435 464 }
duke@435 465 }
duke@435 466
duke@435 467 // use the instruction visitor for killing values
duke@435 468 for (Value instr = block->next(); instr != NULL; instr = instr->next()) {
duke@435 469 instr->visit(this);
duke@435 470 if (_too_complicated_loop) {
duke@435 471 return false;
duke@435 472 }
duke@435 473 }
duke@435 474 }
duke@435 475
roland@4860 476 bool optimistic = this->_gvn->compilation()->is_optimistic();
roland@4860 477
roland@4860 478 if (UseLoopInvariantCodeMotion && optimistic) {
roland@4860 479 LoopInvariantCodeMotion code_motion(this, _gvn, loop_header, &_loop_blocks);
roland@4860 480 }
roland@4860 481
duke@435 482 TRACE_VALUE_NUMBERING(tty->print_cr("** loop successfully optimized"));
duke@435 483 return true;
duke@435 484 }
duke@435 485
duke@435 486
duke@435 487 GlobalValueNumbering::GlobalValueNumbering(IR* ir)
duke@435 488 : _current_map(NULL)
duke@435 489 , _value_maps(ir->linear_scan_order()->length(), NULL)
roland@4860 490 , _compilation(ir->compilation())
duke@435 491 {
duke@435 492 TRACE_VALUE_NUMBERING(tty->print_cr("****** start of global value numbering"));
duke@435 493
duke@435 494 ShortLoopOptimizer short_loop_optimizer(this);
duke@435 495
duke@435 496 BlockList* blocks = ir->linear_scan_order();
duke@435 497 int num_blocks = blocks->length();
duke@435 498
duke@435 499 BlockBegin* start_block = blocks->at(0);
duke@435 500 assert(start_block == ir->start() && start_block->number_of_preds() == 0 && start_block->dominator() == NULL, "must be start block");
duke@435 501 assert(start_block->next()->as_Base() != NULL && start_block->next()->next() == NULL, "start block must not have instructions");
duke@435 502
roland@4860 503 // method parameters are not linked in instructions list, so process them separateley
roland@4860 504 for_each_state_value(start_block->state(), value,
roland@4860 505 assert(value->as_Local() != NULL, "only method parameters allowed");
roland@4860 506 set_processed(value);
roland@4860 507 );
roland@4860 508
duke@435 509 // initial, empty value map with nesting 0
duke@435 510 set_value_map_of(start_block, new ValueMap());
duke@435 511
duke@435 512 for (int i = 1; i < num_blocks; i++) {
duke@435 513 BlockBegin* block = blocks->at(i);
duke@435 514 TRACE_VALUE_NUMBERING(tty->print_cr("**** processing block B%d", block->block_id()));
duke@435 515
duke@435 516 int num_preds = block->number_of_preds();
duke@435 517 assert(num_preds > 0, "block must have predecessors");
duke@435 518
duke@435 519 BlockBegin* dominator = block->dominator();
duke@435 520 assert(dominator != NULL, "dominator must exist");
duke@435 521 assert(value_map_of(dominator) != NULL, "value map of dominator must exist");
duke@435 522
duke@435 523 // create new value map with increased nesting
duke@435 524 _current_map = new ValueMap(value_map_of(dominator));
duke@435 525
roland@4860 526 if (num_preds == 1 && !block->is_set(BlockBegin::exception_entry_flag)) {
duke@435 527 assert(dominator == block->pred_at(0), "dominator must be equal to predecessor");
duke@435 528 // nothing to do here
duke@435 529
duke@435 530 } else if (block->is_set(BlockBegin::linear_scan_loop_header_flag)) {
duke@435 531 // block has incoming backward branches -> try to optimize short loops
duke@435 532 if (!short_loop_optimizer.process(block)) {
duke@435 533 // loop is too complicated, so kill all memory loads because there might be
duke@435 534 // stores to them in the loop
duke@435 535 current_map()->kill_memory();
duke@435 536 }
duke@435 537
duke@435 538 } else {
duke@435 539 // only incoming forward branches that are already processed
duke@435 540 for (int j = 0; j < num_preds; j++) {
duke@435 541 BlockBegin* pred = block->pred_at(j);
duke@435 542 ValueMap* pred_map = value_map_of(pred);
duke@435 543
duke@435 544 if (pred_map != NULL) {
duke@435 545 // propagate killed values of the predecessor to this block
duke@435 546 current_map()->kill_map(value_map_of(pred));
duke@435 547 } else {
duke@435 548 // kill all memory loads because predecessor not yet processed
duke@435 549 // (this can happen with non-natural loops and OSR-compiles)
duke@435 550 current_map()->kill_memory();
duke@435 551 }
duke@435 552 }
duke@435 553 }
duke@435 554
roland@4860 555 // phi functions are not linked in instructions list, so process them separateley
roland@4860 556 for_each_phi_fun(block, phi,
roland@4860 557 set_processed(phi);
roland@4860 558 );
duke@435 559
duke@435 560 TRACE_VALUE_NUMBERING(tty->print("value map before processing block: "); current_map()->print());
duke@435 561
duke@435 562 // visit all instructions of this block
duke@435 563 for (Value instr = block->next(); instr != NULL; instr = instr->next()) {
duke@435 564 // check if instruction kills any values
duke@435 565 instr->visit(this);
roland@4860 566 // perform actual value numbering
roland@4860 567 substitute(instr);
duke@435 568 }
duke@435 569
duke@435 570 // remember value map for successors
duke@435 571 set_value_map_of(block, current_map());
duke@435 572 }
duke@435 573
roland@4860 574 if (_has_substitutions) {
duke@435 575 SubstitutionResolver resolver(ir);
duke@435 576 }
duke@435 577
duke@435 578 TRACE_VALUE_NUMBERING(tty->print("****** end of global value numbering. "); ValueMap::print_statistics());
duke@435 579 }
roland@4860 580
roland@4860 581 void GlobalValueNumbering::substitute(Instruction* instr) {
roland@4860 582 assert(!instr->has_subst(), "substitution already set");
roland@4860 583 Value subst = current_map()->find_insert(instr);
roland@4860 584 if (subst != instr) {
roland@4860 585 assert(!subst->has_subst(), "can't have a substitution");
roland@4860 586
roland@4860 587 TRACE_VALUE_NUMBERING(tty->print_cr("substitution for %d set to %d", instr->id(), subst->id()));
roland@4860 588 instr->set_subst(subst);
roland@4860 589 _has_substitutions = true;
roland@4860 590 }
roland@4860 591 set_processed(instr);
roland@4860 592 }

mercurial