duke@435: /* mikael@6198: * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "c1/c1_Canonicalizer.hpp" stefank@2314: #include "c1/c1_IR.hpp" stefank@2314: #include "c1/c1_ValueMap.hpp" roland@4860: #include "c1/c1_ValueStack.hpp" stefank@2314: #include "utilities/bitMap.inline.hpp" duke@435: duke@435: #ifndef PRODUCT duke@435: duke@435: int ValueMap::_number_of_finds = 0; duke@435: int ValueMap::_number_of_hits = 0; duke@435: int ValueMap::_number_of_kills = 0; duke@435: duke@435: #define TRACE_VALUE_NUMBERING(code) if (PrintValueNumbering) { code; } duke@435: duke@435: #else duke@435: duke@435: #define TRACE_VALUE_NUMBERING(code) duke@435: duke@435: #endif duke@435: duke@435: duke@435: ValueMap::ValueMap() duke@435: : _nesting(0) duke@435: , _entries(ValueMapInitialSize, NULL) duke@435: , _killed_values() duke@435: , _entry_count(0) duke@435: { duke@435: NOT_PRODUCT(reset_statistics()); duke@435: } duke@435: duke@435: duke@435: ValueMap::ValueMap(ValueMap* old) duke@435: : _nesting(old->_nesting + 1) duke@435: , _entries(old->_entries.length()) duke@435: , _killed_values() duke@435: , _entry_count(old->_entry_count) duke@435: { duke@435: for (int i = size() - 1; i >= 0; i--) { duke@435: _entries.at_put(i, old->entry_at(i)); duke@435: } duke@435: _killed_values.set_from(&old->_killed_values); duke@435: } duke@435: duke@435: duke@435: void ValueMap::increase_table_size() { duke@435: int old_size = size(); duke@435: int new_size = old_size * 2 + 1; duke@435: duke@435: ValueMapEntryList worklist(8); duke@435: ValueMapEntryArray new_entries(new_size, NULL); duke@435: int new_entry_count = 0; duke@435: duke@435: TRACE_VALUE_NUMBERING(tty->print_cr("increasing table size from %d to %d", old_size, new_size)); duke@435: duke@435: for (int i = old_size - 1; i >= 0; i--) { duke@435: ValueMapEntry* entry; duke@435: for (entry = entry_at(i); entry != NULL; entry = entry->next()) { duke@435: if (!is_killed(entry->value())) { duke@435: worklist.push(entry); duke@435: } duke@435: } duke@435: duke@435: while (!worklist.is_empty()) { duke@435: entry = worklist.pop(); duke@435: int new_index = entry_index(entry->hash(), new_size); duke@435: duke@435: if (entry->nesting() != nesting() && new_entries.at(new_index) != entry->next()) { duke@435: // changing entries with a lower nesting than the current nesting of the table duke@435: // is not allowed because then the same entry is contained in multiple value maps. duke@435: // clone entry when next-pointer must be changed duke@435: entry = new ValueMapEntry(entry->hash(), entry->value(), entry->nesting(), NULL); duke@435: } duke@435: entry->set_next(new_entries.at(new_index)); duke@435: new_entries.at_put(new_index, entry); duke@435: new_entry_count++; duke@435: } duke@435: } duke@435: duke@435: _entries = new_entries; duke@435: _entry_count = new_entry_count; duke@435: } duke@435: duke@435: duke@435: Value ValueMap::find_insert(Value x) { duke@435: const intx hash = x->hash(); duke@435: if (hash != 0) { duke@435: // 0 hash means: exclude from value numbering duke@435: NOT_PRODUCT(_number_of_finds++); duke@435: duke@435: for (ValueMapEntry* entry = entry_at(entry_index(hash, size())); entry != NULL; entry = entry->next()) { duke@435: if (entry->hash() == hash) { duke@435: Value f = entry->value(); duke@435: duke@435: if (!is_killed(f) && f->is_equal(x)) { duke@435: NOT_PRODUCT(_number_of_hits++); duke@435: 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: duke@435: if (entry->nesting() != nesting() && f->as_Constant() == NULL) { duke@435: // non-constant values of of another block must be pinned, duke@435: // otherwise it is possible that they are not evaluated duke@435: f->pin(Instruction::PinGlobalValueNumbering); duke@435: } roland@3498: assert(x->type()->tag() == f->type()->tag(), "should have same type"); duke@435: duke@435: return f; duke@435: duke@435: } duke@435: } duke@435: } duke@435: duke@435: // x not found, so insert it duke@435: if (entry_count() >= size_threshold()) { duke@435: increase_table_size(); duke@435: } duke@435: int idx = entry_index(hash, size()); duke@435: _entries.at_put(idx, new ValueMapEntry(hash, x, nesting(), entry_at(idx))); duke@435: _entry_count++; duke@435: duke@435: 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: } duke@435: duke@435: return x; duke@435: } duke@435: duke@435: duke@435: #define GENERIC_KILL_VALUE(must_kill_implementation) \ duke@435: NOT_PRODUCT(_number_of_kills++); \ duke@435: \ duke@435: for (int i = size() - 1; i >= 0; i--) { \ duke@435: ValueMapEntry* prev_entry = NULL; \ duke@435: for (ValueMapEntry* entry = entry_at(i); entry != NULL; entry = entry->next()) { \ duke@435: Value value = entry->value(); \ duke@435: \ duke@435: must_kill_implementation(must_kill, entry, value) \ duke@435: \ duke@435: if (must_kill) { \ duke@435: kill_value(value); \ duke@435: \ duke@435: if (prev_entry == NULL) { \ duke@435: _entries.at_put(i, entry->next()); \ duke@435: _entry_count--; \ duke@435: } else if (prev_entry->nesting() == nesting()) { \ duke@435: prev_entry->set_next(entry->next()); \ duke@435: _entry_count--; \ duke@435: } else { \ duke@435: prev_entry = entry; \ duke@435: } \ duke@435: \ duke@435: 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: } else { \ duke@435: prev_entry = entry; \ duke@435: } \ duke@435: } \ duke@435: } \ duke@435: duke@435: #define MUST_KILL_MEMORY(must_kill, entry, value) \ duke@435: bool must_kill = value->as_LoadField() != NULL || value->as_LoadIndexed() != NULL; duke@435: duke@435: #define MUST_KILL_ARRAY(must_kill, entry, value) \ duke@435: bool must_kill = value->as_LoadIndexed() != NULL \ duke@435: && value->type()->tag() == type->tag(); duke@435: duke@435: #define MUST_KILL_FIELD(must_kill, entry, value) \ duke@435: /* ciField's are not unique; must compare their contents */ \ duke@435: LoadField* lf = value->as_LoadField(); \ duke@435: bool must_kill = lf != NULL \ duke@435: && lf->field()->holder() == field->holder() \ roland@4005: && (all_offsets || lf->field()->offset() == field->offset()); duke@435: duke@435: duke@435: void ValueMap::kill_memory() { duke@435: GENERIC_KILL_VALUE(MUST_KILL_MEMORY); duke@435: } duke@435: duke@435: void ValueMap::kill_array(ValueType* type) { duke@435: GENERIC_KILL_VALUE(MUST_KILL_ARRAY); duke@435: } duke@435: roland@4005: void ValueMap::kill_field(ciField* field, bool all_offsets) { duke@435: GENERIC_KILL_VALUE(MUST_KILL_FIELD); duke@435: } duke@435: duke@435: void ValueMap::kill_map(ValueMap* map) { duke@435: assert(is_global_value_numbering(), "only for global value numbering"); duke@435: _killed_values.set_union(&map->_killed_values); duke@435: } duke@435: duke@435: void ValueMap::kill_all() { duke@435: assert(is_local_value_numbering(), "only for local value numbering"); duke@435: for (int i = size() - 1; i >= 0; i--) { duke@435: _entries.at_put(i, NULL); duke@435: } duke@435: _entry_count = 0; duke@435: } duke@435: duke@435: duke@435: #ifndef PRODUCT duke@435: duke@435: void ValueMap::print() { duke@435: tty->print_cr("(size %d, entries %d, nesting %d)", size(), entry_count(), nesting()); duke@435: duke@435: int entries = 0; duke@435: for (int i = 0; i < size(); i++) { duke@435: if (entry_at(i) != NULL) { duke@435: tty->print(" %2d: ", i); duke@435: for (ValueMapEntry* entry = entry_at(i); entry != NULL; entry = entry->next()) { duke@435: Value value = entry->value(); duke@435: tty->print("%s %c%d (%s%d) -> ", value->name(), value->type()->tchar(), value->id(), is_killed(value) ? "x" : "", entry->nesting()); duke@435: entries++; duke@435: } duke@435: tty->print_cr("NULL"); duke@435: } duke@435: } duke@435: duke@435: _killed_values.print(); duke@435: assert(entry_count() == entries, "entry_count incorrect"); duke@435: } duke@435: duke@435: void ValueMap::reset_statistics() { duke@435: _number_of_finds = 0; duke@435: _number_of_hits = 0; duke@435: _number_of_kills = 0; duke@435: } duke@435: duke@435: void ValueMap::print_statistics() { duke@435: float hit_rate = 0; duke@435: if (_number_of_finds != 0) { duke@435: hit_rate = (float)_number_of_hits / _number_of_finds; duke@435: } duke@435: duke@435: 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: } duke@435: duke@435: #endif duke@435: duke@435: duke@435: duke@435: class ShortLoopOptimizer : public ValueNumberingVisitor { duke@435: private: duke@435: GlobalValueNumbering* _gvn; duke@435: BlockList _loop_blocks; duke@435: bool _too_complicated_loop; roland@4860: bool _has_field_store[T_ARRAY + 1]; roland@4860: bool _has_indexed_store[T_ARRAY + 1]; duke@435: duke@435: // simplified access to methods of GlobalValueNumbering duke@435: ValueMap* current_map() { return _gvn->current_map(); } duke@435: ValueMap* value_map_of(BlockBegin* block) { return _gvn->value_map_of(block); } duke@435: duke@435: // implementation for abstract methods of ValueNumberingVisitor roland@4005: void kill_memory() { _too_complicated_loop = true; } roland@4860: void kill_field(ciField* field, bool all_offsets) { roland@4860: current_map()->kill_field(field, all_offsets); roland@4860: assert(field->type()->basic_type() >= 0 && field->type()->basic_type() <= T_ARRAY, "Invalid type"); roland@4860: _has_field_store[field->type()->basic_type()] = true; roland@4860: } roland@4860: void kill_array(ValueType* type) { roland@4860: current_map()->kill_array(type); roland@4860: BasicType basic_type = as_BasicType(type); assert(basic_type >= 0 && basic_type <= T_ARRAY, "Invalid type"); roland@4860: _has_indexed_store[basic_type] = true; roland@4860: } duke@435: duke@435: public: duke@435: ShortLoopOptimizer(GlobalValueNumbering* gvn) duke@435: : _gvn(gvn) duke@435: , _loop_blocks(ValueMapMaxLoopSize) duke@435: , _too_complicated_loop(false) duke@435: { roland@4860: for (int i=0; i<= T_ARRAY; i++){ roland@4860: _has_field_store[i] = false; roland@4860: _has_indexed_store[i] = false; roland@4860: } roland@4860: } roland@4860: roland@4860: bool has_field_store(BasicType type) { roland@4860: assert(type >= 0 && type <= T_ARRAY, "Invalid type"); roland@4860: return _has_field_store[type]; roland@4860: } roland@4860: roland@4860: bool has_indexed_store(BasicType type) { roland@4860: assert(type >= 0 && type <= T_ARRAY, "Invalid type"); roland@4860: return _has_indexed_store[type]; duke@435: } duke@435: duke@435: bool process(BlockBegin* loop_header); duke@435: }; duke@435: roland@4860: class LoopInvariantCodeMotion : public StackObj { roland@4860: private: roland@4860: GlobalValueNumbering* _gvn; roland@4860: ShortLoopOptimizer* _short_loop_optimizer; roland@4860: Instruction* _insertion_point; roland@4860: ValueStack * _state; roland@4909: bool _insert_is_pred; roland@4860: roland@4860: void set_invariant(Value v) const { _gvn->set_processed(v); } roland@4860: bool is_invariant(Value v) const { return _gvn->is_processed(v); } roland@4860: roland@4860: void process_block(BlockBegin* block); roland@4860: roland@4860: public: roland@4860: LoopInvariantCodeMotion(ShortLoopOptimizer *slo, GlobalValueNumbering* gvn, BlockBegin* loop_header, BlockList* loop_blocks); roland@4860: }; roland@4860: roland@4860: LoopInvariantCodeMotion::LoopInvariantCodeMotion(ShortLoopOptimizer *slo, GlobalValueNumbering* gvn, BlockBegin* loop_header, BlockList* loop_blocks) roland@4860: : _gvn(gvn), _short_loop_optimizer(slo) { roland@4860: roland@4860: TRACE_VALUE_NUMBERING(tty->print_cr("using loop invariant code motion loop_header = %d", loop_header->block_id())); roland@4860: TRACE_VALUE_NUMBERING(tty->print_cr("** loop invariant code motion for short loop B%d", loop_header->block_id())); roland@4860: roland@4860: BlockBegin* insertion_block = loop_header->dominator(); roland@4860: if (insertion_block->number_of_preds() == 0) { roland@4860: return; // only the entry block does not have a predecessor roland@4860: } roland@4860: roland@4860: assert(insertion_block->end()->as_Base() == NULL, "cannot insert into entry block"); roland@4860: _insertion_point = insertion_block->end()->prev(); roland@4909: _insert_is_pred = loop_header->is_predecessor(insertion_block); roland@4860: roland@4860: BlockEnd *block_end = insertion_block->end(); roland@4860: _state = block_end->state_before(); roland@4860: roland@4860: if (!_state) { roland@4860: // If, TableSwitch and LookupSwitch always have state_before when roland@4860: // loop invariant code motion happens.. roland@4860: assert(block_end->as_Goto(), "Block has to be goto"); roland@4860: _state = block_end->state(); roland@4860: } roland@4860: roland@4860: // the loop_blocks are filled by going backward from the loop header, so this processing order is best roland@4860: assert(loop_blocks->at(0) == loop_header, "loop header must be first loop block"); roland@4860: process_block(loop_header); roland@4860: for (int i = loop_blocks->length() - 1; i >= 1; i--) { roland@4860: process_block(loop_blocks->at(i)); roland@4860: } roland@4860: } roland@4860: roland@4860: void LoopInvariantCodeMotion::process_block(BlockBegin* block) { roland@4860: TRACE_VALUE_NUMBERING(tty->print_cr("processing block B%d", block->block_id())); roland@4860: roland@4860: Instruction* prev = block; roland@4860: Instruction* cur = block->next(); roland@4860: roland@4860: while (cur != NULL) { roland@4860: roland@4860: // determine if cur instruction is loop invariant roland@4860: // only selected instruction types are processed here roland@4860: bool cur_invariant = false; roland@4860: roland@4860: if (cur->as_Constant() != NULL) { roland@4860: cur_invariant = !cur->can_trap(); roland@4860: } else if (cur->as_ArithmeticOp() != NULL || cur->as_LogicOp() != NULL || cur->as_ShiftOp() != NULL) { roland@4860: assert(cur->as_Op2() != NULL, "must be Op2"); roland@4860: Op2* op2 = (Op2*)cur; roland@4860: cur_invariant = !op2->can_trap() && is_invariant(op2->x()) && is_invariant(op2->y()); roland@4860: } else if (cur->as_LoadField() != NULL) { roland@4860: LoadField* lf = (LoadField*)cur; roland@4860: // deoptimizes on NullPointerException roland@4909: 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: } else if (cur->as_ArrayLength() != NULL) { roland@4860: ArrayLength *length = cur->as_ArrayLength(); roland@4860: cur_invariant = is_invariant(length->array()); roland@4860: } else if (cur->as_LoadIndexed() != NULL) { roland@4860: LoadIndexed *li = (LoadIndexed *)cur->as_LoadIndexed(); roland@4909: 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: } roland@4860: roland@4860: if (cur_invariant) { roland@4860: // perform value numbering and mark instruction as loop-invariant roland@4860: _gvn->substitute(cur); roland@4860: roland@4860: if (cur->as_Constant() == NULL) { roland@4860: // ensure that code for non-constant instructions is always generated roland@4860: cur->pin(); roland@4860: } roland@4860: roland@4860: // remove cur instruction from loop block and append it to block before loop roland@4860: Instruction* next = cur->next(); roland@4860: Instruction* in = _insertion_point->next(); roland@4860: _insertion_point = _insertion_point->set_next(cur); roland@4860: cur->set_next(in); roland@4860: roland@4860: // Deoptimize on exception roland@4860: cur->set_flag(Instruction::DeoptimizeOnException, true); roland@4860: roland@4860: // Clear exception handlers roland@4860: cur->set_exception_handlers(NULL); roland@4860: roland@4860: TRACE_VALUE_NUMBERING(tty->print_cr("Instruction %c%d is loop invariant", cur->type()->tchar(), cur->id())); roland@4860: roland@4860: if (cur->state_before() != NULL) { roland@4860: cur->set_state_before(_state->copy()); roland@4860: } roland@4860: if (cur->exception_state() != NULL) { roland@4860: cur->set_exception_state(_state->copy()); roland@4860: } roland@4860: roland@4860: cur = prev->set_next(next); roland@4860: roland@4860: } else { roland@4860: prev = cur; roland@4860: cur = cur->next(); roland@4860: } roland@4860: } roland@4860: } duke@435: duke@435: bool ShortLoopOptimizer::process(BlockBegin* loop_header) { duke@435: TRACE_VALUE_NUMBERING(tty->print_cr("** loop header block")); duke@435: duke@435: _too_complicated_loop = false; duke@435: _loop_blocks.clear(); duke@435: _loop_blocks.append(loop_header); duke@435: duke@435: for (int i = 0; i < _loop_blocks.length(); i++) { duke@435: BlockBegin* block = _loop_blocks.at(i); duke@435: TRACE_VALUE_NUMBERING(tty->print_cr("processing loop block B%d", block->block_id())); duke@435: duke@435: if (block->is_set(BlockBegin::exception_entry_flag)) { duke@435: // this would be too complicated duke@435: return false; duke@435: } duke@435: duke@435: // add predecessors to worklist duke@435: for (int j = block->number_of_preds() - 1; j >= 0; j--) { duke@435: BlockBegin* pred = block->pred_at(j); duke@435: roland@4860: if (pred->is_set(BlockBegin::osr_entry_flag)) { roland@4860: return false; roland@4860: } roland@4860: duke@435: ValueMap* pred_map = value_map_of(pred); duke@435: if (pred_map != NULL) { duke@435: current_map()->kill_map(pred_map); duke@435: } else if (!_loop_blocks.contains(pred)) { duke@435: if (_loop_blocks.length() >= ValueMapMaxLoopSize) { duke@435: return false; duke@435: } duke@435: _loop_blocks.append(pred); duke@435: } duke@435: } duke@435: duke@435: // use the instruction visitor for killing values duke@435: for (Value instr = block->next(); instr != NULL; instr = instr->next()) { duke@435: instr->visit(this); duke@435: if (_too_complicated_loop) { duke@435: return false; duke@435: } duke@435: } duke@435: } duke@435: roland@4860: bool optimistic = this->_gvn->compilation()->is_optimistic(); roland@4860: roland@4860: if (UseLoopInvariantCodeMotion && optimistic) { roland@4860: LoopInvariantCodeMotion code_motion(this, _gvn, loop_header, &_loop_blocks); roland@4860: } roland@4860: duke@435: TRACE_VALUE_NUMBERING(tty->print_cr("** loop successfully optimized")); duke@435: return true; duke@435: } duke@435: duke@435: duke@435: GlobalValueNumbering::GlobalValueNumbering(IR* ir) duke@435: : _current_map(NULL) duke@435: , _value_maps(ir->linear_scan_order()->length(), NULL) roland@4860: , _compilation(ir->compilation()) duke@435: { duke@435: TRACE_VALUE_NUMBERING(tty->print_cr("****** start of global value numbering")); duke@435: duke@435: ShortLoopOptimizer short_loop_optimizer(this); duke@435: duke@435: BlockList* blocks = ir->linear_scan_order(); duke@435: int num_blocks = blocks->length(); duke@435: duke@435: BlockBegin* start_block = blocks->at(0); duke@435: assert(start_block == ir->start() && start_block->number_of_preds() == 0 && start_block->dominator() == NULL, "must be start block"); duke@435: assert(start_block->next()->as_Base() != NULL && start_block->next()->next() == NULL, "start block must not have instructions"); duke@435: roland@4860: // method parameters are not linked in instructions list, so process them separateley roland@4860: for_each_state_value(start_block->state(), value, roland@4860: assert(value->as_Local() != NULL, "only method parameters allowed"); roland@4860: set_processed(value); roland@4860: ); roland@4860: duke@435: // initial, empty value map with nesting 0 duke@435: set_value_map_of(start_block, new ValueMap()); duke@435: duke@435: for (int i = 1; i < num_blocks; i++) { duke@435: BlockBegin* block = blocks->at(i); duke@435: TRACE_VALUE_NUMBERING(tty->print_cr("**** processing block B%d", block->block_id())); duke@435: duke@435: int num_preds = block->number_of_preds(); duke@435: assert(num_preds > 0, "block must have predecessors"); duke@435: duke@435: BlockBegin* dominator = block->dominator(); duke@435: assert(dominator != NULL, "dominator must exist"); duke@435: assert(value_map_of(dominator) != NULL, "value map of dominator must exist"); duke@435: duke@435: // create new value map with increased nesting duke@435: _current_map = new ValueMap(value_map_of(dominator)); duke@435: roland@4860: if (num_preds == 1 && !block->is_set(BlockBegin::exception_entry_flag)) { duke@435: assert(dominator == block->pred_at(0), "dominator must be equal to predecessor"); duke@435: // nothing to do here duke@435: duke@435: } else if (block->is_set(BlockBegin::linear_scan_loop_header_flag)) { duke@435: // block has incoming backward branches -> try to optimize short loops duke@435: if (!short_loop_optimizer.process(block)) { duke@435: // loop is too complicated, so kill all memory loads because there might be duke@435: // stores to them in the loop duke@435: current_map()->kill_memory(); duke@435: } duke@435: duke@435: } else { duke@435: // only incoming forward branches that are already processed duke@435: for (int j = 0; j < num_preds; j++) { duke@435: BlockBegin* pred = block->pred_at(j); duke@435: ValueMap* pred_map = value_map_of(pred); duke@435: duke@435: if (pred_map != NULL) { duke@435: // propagate killed values of the predecessor to this block duke@435: current_map()->kill_map(value_map_of(pred)); duke@435: } else { duke@435: // kill all memory loads because predecessor not yet processed duke@435: // (this can happen with non-natural loops and OSR-compiles) duke@435: current_map()->kill_memory(); duke@435: } duke@435: } duke@435: } duke@435: roland@4860: // phi functions are not linked in instructions list, so process them separateley roland@4860: for_each_phi_fun(block, phi, roland@4860: set_processed(phi); roland@4860: ); duke@435: duke@435: TRACE_VALUE_NUMBERING(tty->print("value map before processing block: "); current_map()->print()); duke@435: duke@435: // visit all instructions of this block duke@435: for (Value instr = block->next(); instr != NULL; instr = instr->next()) { duke@435: // check if instruction kills any values duke@435: instr->visit(this); roland@4860: // perform actual value numbering roland@4860: substitute(instr); duke@435: } duke@435: duke@435: // remember value map for successors duke@435: set_value_map_of(block, current_map()); duke@435: } duke@435: roland@4860: if (_has_substitutions) { duke@435: SubstitutionResolver resolver(ir); duke@435: } duke@435: duke@435: TRACE_VALUE_NUMBERING(tty->print("****** end of global value numbering. "); ValueMap::print_statistics()); duke@435: } roland@4860: roland@4860: void GlobalValueNumbering::substitute(Instruction* instr) { roland@4860: assert(!instr->has_subst(), "substitution already set"); roland@4860: Value subst = current_map()->find_insert(instr); roland@4860: if (subst != instr) { roland@4860: assert(!subst->has_subst(), "can't have a substitution"); roland@4860: roland@4860: TRACE_VALUE_NUMBERING(tty->print_cr("substitution for %d set to %d", instr->id(), subst->id())); roland@4860: instr->set_subst(subst); roland@4860: _has_substitutions = true; roland@4860: } roland@4860: set_processed(instr); roland@4860: }