Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/c1/c1_ValueMap.cpp
32285 views
/*1* Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "c1/c1_Canonicalizer.hpp"26#include "c1/c1_IR.hpp"27#include "c1/c1_ValueMap.hpp"28#include "c1/c1_ValueStack.hpp"29#include "utilities/bitMap.inline.hpp"3031#ifndef PRODUCT3233int ValueMap::_number_of_finds = 0;34int ValueMap::_number_of_hits = 0;35int ValueMap::_number_of_kills = 0;3637#define TRACE_VALUE_NUMBERING(code) if (PrintValueNumbering) { code; }3839#else4041#define TRACE_VALUE_NUMBERING(code)4243#endif444546ValueMap::ValueMap()47: _nesting(0)48, _entries(ValueMapInitialSize, NULL)49, _killed_values()50, _entry_count(0)51{52NOT_PRODUCT(reset_statistics());53}545556ValueMap::ValueMap(ValueMap* old)57: _nesting(old->_nesting + 1)58, _entries(old->_entries.length())59, _killed_values()60, _entry_count(old->_entry_count)61{62for (int i = size() - 1; i >= 0; i--) {63_entries.at_put(i, old->entry_at(i));64}65_killed_values.set_from(&old->_killed_values);66}676869void ValueMap::increase_table_size() {70int old_size = size();71int new_size = old_size * 2 + 1;7273ValueMapEntryList worklist(8);74ValueMapEntryArray new_entries(new_size, NULL);75int new_entry_count = 0;7677TRACE_VALUE_NUMBERING(tty->print_cr("increasing table size from %d to %d", old_size, new_size));7879for (int i = old_size - 1; i >= 0; i--) {80ValueMapEntry* entry;81for (entry = entry_at(i); entry != NULL; entry = entry->next()) {82if (!is_killed(entry->value())) {83worklist.push(entry);84}85}8687while (!worklist.is_empty()) {88entry = worklist.pop();89int new_index = entry_index(entry->hash(), new_size);9091if (entry->nesting() != nesting() && new_entries.at(new_index) != entry->next()) {92// changing entries with a lower nesting than the current nesting of the table93// is not allowed because then the same entry is contained in multiple value maps.94// clone entry when next-pointer must be changed95entry = new ValueMapEntry(entry->hash(), entry->value(), entry->nesting(), NULL);96}97entry->set_next(new_entries.at(new_index));98new_entries.at_put(new_index, entry);99new_entry_count++;100}101}102103_entries = new_entries;104_entry_count = new_entry_count;105}106107108Value ValueMap::find_insert(Value x) {109const intx hash = x->hash();110if (hash != 0) {111// 0 hash means: exclude from value numbering112NOT_PRODUCT(_number_of_finds++);113114for (ValueMapEntry* entry = entry_at(entry_index(hash, size())); entry != NULL; entry = entry->next()) {115if (entry->hash() == hash) {116Value f = entry->value();117118if (!is_killed(f) && f->is_equal(x)) {119NOT_PRODUCT(_number_of_hits++);120TRACE_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()));121122if (entry->nesting() != nesting() && f->as_Constant() == NULL) {123// non-constant values of of another block must be pinned,124// otherwise it is possible that they are not evaluated125f->pin(Instruction::PinGlobalValueNumbering);126}127assert(x->type()->tag() == f->type()->tag(), "should have same type");128129return f;130131}132}133}134135// x not found, so insert it136if (entry_count() >= size_threshold()) {137increase_table_size();138}139int idx = entry_index(hash, size());140_entries.at_put(idx, new ValueMapEntry(hash, x, nesting(), entry_at(idx)));141_entry_count++;142143TRACE_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()));144}145146return x;147}148149150#define GENERIC_KILL_VALUE(must_kill_implementation) \151NOT_PRODUCT(_number_of_kills++); \152\153for (int i = size() - 1; i >= 0; i--) { \154ValueMapEntry* prev_entry = NULL; \155for (ValueMapEntry* entry = entry_at(i); entry != NULL; entry = entry->next()) { \156Value value = entry->value(); \157\158must_kill_implementation(must_kill, entry, value) \159\160if (must_kill) { \161kill_value(value); \162\163if (prev_entry == NULL) { \164_entries.at_put(i, entry->next()); \165_entry_count--; \166} else if (prev_entry->nesting() == nesting()) { \167prev_entry->set_next(entry->next()); \168_entry_count--; \169} else { \170prev_entry = entry; \171} \172\173TRACE_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())); \174} else { \175prev_entry = entry; \176} \177} \178} \179180#define MUST_KILL_MEMORY(must_kill, entry, value) \181bool must_kill = value->as_LoadField() != NULL || value->as_LoadIndexed() != NULL;182183#define MUST_KILL_ARRAY(must_kill, entry, value) \184bool must_kill = value->as_LoadIndexed() != NULL \185&& value->type()->tag() == type->tag();186187#define MUST_KILL_FIELD(must_kill, entry, value) \188/* ciField's are not unique; must compare their contents */ \189LoadField* lf = value->as_LoadField(); \190bool must_kill = lf != NULL \191&& lf->field()->holder() == field->holder() \192&& (all_offsets || lf->field()->offset() == field->offset());193194195void ValueMap::kill_memory() {196GENERIC_KILL_VALUE(MUST_KILL_MEMORY);197}198199void ValueMap::kill_array(ValueType* type) {200GENERIC_KILL_VALUE(MUST_KILL_ARRAY);201}202203void ValueMap::kill_field(ciField* field, bool all_offsets) {204GENERIC_KILL_VALUE(MUST_KILL_FIELD);205}206207void ValueMap::kill_map(ValueMap* map) {208assert(is_global_value_numbering(), "only for global value numbering");209_killed_values.set_union(&map->_killed_values);210}211212void ValueMap::kill_all() {213assert(is_local_value_numbering(), "only for local value numbering");214for (int i = size() - 1; i >= 0; i--) {215_entries.at_put(i, NULL);216}217_entry_count = 0;218}219220221#ifndef PRODUCT222223void ValueMap::print() {224tty->print_cr("(size %d, entries %d, nesting %d)", size(), entry_count(), nesting());225226int entries = 0;227for (int i = 0; i < size(); i++) {228if (entry_at(i) != NULL) {229tty->print(" %2d: ", i);230for (ValueMapEntry* entry = entry_at(i); entry != NULL; entry = entry->next()) {231Value value = entry->value();232tty->print("%s %c%d (%s%d) -> ", value->name(), value->type()->tchar(), value->id(), is_killed(value) ? "x" : "", entry->nesting());233entries++;234}235tty->print_cr("NULL");236}237}238239_killed_values.print();240assert(entry_count() == entries, "entry_count incorrect");241}242243void ValueMap::reset_statistics() {244_number_of_finds = 0;245_number_of_hits = 0;246_number_of_kills = 0;247}248249void ValueMap::print_statistics() {250float hit_rate = 0;251if (_number_of_finds != 0) {252hit_rate = (float)_number_of_hits / _number_of_finds;253}254255tty->print_cr("finds:%3d hits:%3d kills:%3d hit rate: %1.4f", _number_of_finds, _number_of_hits, _number_of_kills, hit_rate);256}257258#endif259260261262class ShortLoopOptimizer : public ValueNumberingVisitor {263private:264GlobalValueNumbering* _gvn;265BlockList _loop_blocks;266bool _too_complicated_loop;267bool _has_field_store[T_ARRAY + 1];268bool _has_indexed_store[T_ARRAY + 1];269270// simplified access to methods of GlobalValueNumbering271ValueMap* current_map() { return _gvn->current_map(); }272ValueMap* value_map_of(BlockBegin* block) { return _gvn->value_map_of(block); }273274// implementation for abstract methods of ValueNumberingVisitor275void kill_memory() { _too_complicated_loop = true; }276void kill_field(ciField* field, bool all_offsets) {277current_map()->kill_field(field, all_offsets);278assert(field->type()->basic_type() >= 0 && field->type()->basic_type() <= T_ARRAY, "Invalid type");279_has_field_store[field->type()->basic_type()] = true;280}281void kill_array(ValueType* type) {282current_map()->kill_array(type);283BasicType basic_type = as_BasicType(type); assert(basic_type >= 0 && basic_type <= T_ARRAY, "Invalid type");284_has_indexed_store[basic_type] = true;285}286287public:288ShortLoopOptimizer(GlobalValueNumbering* gvn)289: _gvn(gvn)290, _loop_blocks(ValueMapMaxLoopSize)291, _too_complicated_loop(false)292{293for (int i=0; i<= T_ARRAY; i++){294_has_field_store[i] = false;295_has_indexed_store[i] = false;296}297}298299bool has_field_store(BasicType type) {300assert(type >= 0 && type <= T_ARRAY, "Invalid type");301return _has_field_store[type];302}303304bool has_indexed_store(BasicType type) {305assert(type >= 0 && type <= T_ARRAY, "Invalid type");306return _has_indexed_store[type];307}308309bool process(BlockBegin* loop_header);310};311312class LoopInvariantCodeMotion : public StackObj {313private:314GlobalValueNumbering* _gvn;315ShortLoopOptimizer* _short_loop_optimizer;316Instruction* _insertion_point;317ValueStack * _state;318bool _insert_is_pred;319320void set_invariant(Value v) const { _gvn->set_processed(v); }321bool is_invariant(Value v) const { return _gvn->is_processed(v); }322323void process_block(BlockBegin* block);324325public:326LoopInvariantCodeMotion(ShortLoopOptimizer *slo, GlobalValueNumbering* gvn, BlockBegin* loop_header, BlockList* loop_blocks);327};328329LoopInvariantCodeMotion::LoopInvariantCodeMotion(ShortLoopOptimizer *slo, GlobalValueNumbering* gvn, BlockBegin* loop_header, BlockList* loop_blocks)330: _gvn(gvn), _short_loop_optimizer(slo), _insertion_point(NULL), _state(NULL), _insert_is_pred(false) {331332TRACE_VALUE_NUMBERING(tty->print_cr("using loop invariant code motion loop_header = %d", loop_header->block_id()));333TRACE_VALUE_NUMBERING(tty->print_cr("** loop invariant code motion for short loop B%d", loop_header->block_id()));334335BlockBegin* insertion_block = loop_header->dominator();336if (insertion_block->number_of_preds() == 0) {337return; // only the entry block does not have a predecessor338}339340assert(insertion_block->end()->as_Base() == NULL, "cannot insert into entry block");341_insertion_point = insertion_block->end()->prev();342_insert_is_pred = loop_header->is_predecessor(insertion_block);343344BlockEnd *block_end = insertion_block->end();345_state = block_end->state_before();346347if (!_state) {348// If, TableSwitch and LookupSwitch always have state_before when349// loop invariant code motion happens..350assert(block_end->as_Goto(), "Block has to be goto");351_state = block_end->state();352}353354// the loop_blocks are filled by going backward from the loop header, so this processing order is best355assert(loop_blocks->at(0) == loop_header, "loop header must be first loop block");356process_block(loop_header);357for (int i = loop_blocks->length() - 1; i >= 1; i--) {358process_block(loop_blocks->at(i));359}360}361362void LoopInvariantCodeMotion::process_block(BlockBegin* block) {363TRACE_VALUE_NUMBERING(tty->print_cr("processing block B%d", block->block_id()));364365Instruction* prev = block;366Instruction* cur = block->next();367368while (cur != NULL) {369370// determine if cur instruction is loop invariant371// only selected instruction types are processed here372bool cur_invariant = false;373374if (cur->as_Constant() != NULL) {375cur_invariant = !cur->can_trap();376} else if (cur->as_ArithmeticOp() != NULL || cur->as_LogicOp() != NULL || cur->as_ShiftOp() != NULL) {377assert(cur->as_Op2() != NULL, "must be Op2");378Op2* op2 = (Op2*)cur;379cur_invariant = !op2->can_trap() && is_invariant(op2->x()) && is_invariant(op2->y());380} else if (cur->as_LoadField() != NULL) {381LoadField* lf = (LoadField*)cur;382// deoptimizes on NullPointerException383cur_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;384} else if (cur->as_ArrayLength() != NULL) {385ArrayLength *length = cur->as_ArrayLength();386cur_invariant = is_invariant(length->array());387} else if (cur->as_LoadIndexed() != NULL) {388LoadIndexed *li = (LoadIndexed *)cur->as_LoadIndexed();389cur_invariant = !_short_loop_optimizer->has_indexed_store(as_BasicType(cur->type())) && is_invariant(li->array()) && is_invariant(li->index()) && _insert_is_pred;390}391392if (cur_invariant) {393// perform value numbering and mark instruction as loop-invariant394_gvn->substitute(cur);395396if (cur->as_Constant() == NULL) {397// ensure that code for non-constant instructions is always generated398cur->pin();399}400401// remove cur instruction from loop block and append it to block before loop402Instruction* next = cur->next();403Instruction* in = _insertion_point->next();404_insertion_point = _insertion_point->set_next(cur);405cur->set_next(in);406407// Deoptimize on exception408cur->set_flag(Instruction::DeoptimizeOnException, true);409410// Clear exception handlers411cur->set_exception_handlers(NULL);412413TRACE_VALUE_NUMBERING(tty->print_cr("Instruction %c%d is loop invariant", cur->type()->tchar(), cur->id()));414415if (cur->state_before() != NULL) {416cur->set_state_before(_state->copy());417}418if (cur->exception_state() != NULL) {419cur->set_exception_state(_state->copy());420}421422cur = prev->set_next(next);423424} else {425prev = cur;426cur = cur->next();427}428}429}430431bool ShortLoopOptimizer::process(BlockBegin* loop_header) {432TRACE_VALUE_NUMBERING(tty->print_cr("** loop header block"));433434_too_complicated_loop = false;435_loop_blocks.clear();436_loop_blocks.append(loop_header);437438for (int i = 0; i < _loop_blocks.length(); i++) {439BlockBegin* block = _loop_blocks.at(i);440TRACE_VALUE_NUMBERING(tty->print_cr("processing loop block B%d", block->block_id()));441442if (block->is_set(BlockBegin::exception_entry_flag)) {443// this would be too complicated444return false;445}446447// add predecessors to worklist448for (int j = block->number_of_preds() - 1; j >= 0; j--) {449BlockBegin* pred = block->pred_at(j);450451if (pred->is_set(BlockBegin::osr_entry_flag)) {452return false;453}454455ValueMap* pred_map = value_map_of(pred);456if (pred_map != NULL) {457current_map()->kill_map(pred_map);458} else if (!_loop_blocks.contains(pred)) {459if (_loop_blocks.length() >= ValueMapMaxLoopSize) {460return false;461}462_loop_blocks.append(pred);463}464}465466// use the instruction visitor for killing values467for (Value instr = block->next(); instr != NULL; instr = instr->next()) {468instr->visit(this);469if (_too_complicated_loop) {470return false;471}472}473}474475bool optimistic = this->_gvn->compilation()->is_optimistic();476477if (UseLoopInvariantCodeMotion && optimistic) {478LoopInvariantCodeMotion code_motion(this, _gvn, loop_header, &_loop_blocks);479}480481TRACE_VALUE_NUMBERING(tty->print_cr("** loop successfully optimized"));482return true;483}484485486GlobalValueNumbering::GlobalValueNumbering(IR* ir)487: _current_map(NULL)488, _value_maps(ir->linear_scan_order()->length(), NULL)489, _compilation(ir->compilation())490{491TRACE_VALUE_NUMBERING(tty->print_cr("****** start of global value numbering"));492493ShortLoopOptimizer short_loop_optimizer(this);494495BlockList* blocks = ir->linear_scan_order();496int num_blocks = blocks->length();497498BlockBegin* start_block = blocks->at(0);499assert(start_block == ir->start() && start_block->number_of_preds() == 0 && start_block->dominator() == NULL, "must be start block");500assert(start_block->next()->as_Base() != NULL && start_block->next()->next() == NULL, "start block must not have instructions");501502// method parameters are not linked in instructions list, so process them separateley503for_each_state_value(start_block->state(), value,504assert(value->as_Local() != NULL, "only method parameters allowed");505set_processed(value);506);507508// initial, empty value map with nesting 0509set_value_map_of(start_block, new ValueMap());510511for (int i = 1; i < num_blocks; i++) {512BlockBegin* block = blocks->at(i);513TRACE_VALUE_NUMBERING(tty->print_cr("**** processing block B%d", block->block_id()));514515int num_preds = block->number_of_preds();516assert(num_preds > 0, "block must have predecessors");517518BlockBegin* dominator = block->dominator();519assert(dominator != NULL, "dominator must exist");520assert(value_map_of(dominator) != NULL, "value map of dominator must exist");521522// create new value map with increased nesting523_current_map = new ValueMap(value_map_of(dominator));524525if (num_preds == 1 && !block->is_set(BlockBegin::exception_entry_flag)) {526assert(dominator == block->pred_at(0), "dominator must be equal to predecessor");527// nothing to do here528529} else if (block->is_set(BlockBegin::linear_scan_loop_header_flag)) {530// block has incoming backward branches -> try to optimize short loops531if (!short_loop_optimizer.process(block)) {532// loop is too complicated, so kill all memory loads because there might be533// stores to them in the loop534current_map()->kill_memory();535}536537} else {538// only incoming forward branches that are already processed539for (int j = 0; j < num_preds; j++) {540BlockBegin* pred = block->pred_at(j);541ValueMap* pred_map = value_map_of(pred);542543if (pred_map != NULL) {544// propagate killed values of the predecessor to this block545current_map()->kill_map(value_map_of(pred));546} else {547// kill all memory loads because predecessor not yet processed548// (this can happen with non-natural loops and OSR-compiles)549current_map()->kill_memory();550}551}552}553554// phi functions are not linked in instructions list, so process them separateley555for_each_phi_fun(block, phi,556set_processed(phi);557);558559TRACE_VALUE_NUMBERING(tty->print("value map before processing block: "); current_map()->print());560561// visit all instructions of this block562for (Value instr = block->next(); instr != NULL; instr = instr->next()) {563// check if instruction kills any values564instr->visit(this);565// perform actual value numbering566substitute(instr);567}568569// remember value map for successors570set_value_map_of(block, current_map());571}572573if (_has_substitutions) {574SubstitutionResolver resolver(ir);575}576577TRACE_VALUE_NUMBERING(tty->print("****** end of global value numbering. "); ValueMap::print_statistics());578}579580void GlobalValueNumbering::substitute(Instruction* instr) {581assert(!instr->has_subst(), "substitution already set");582Value subst = current_map()->find_insert(instr);583if (subst != instr) {584assert(!subst->has_subst(), "can't have a substitution");585586TRACE_VALUE_NUMBERING(tty->print_cr("substitution for %d set to %d", instr->id(), subst->id()));587instr->set_subst(subst);588_has_substitutions = true;589}590set_processed(instr);591}592593594