aoqi@0: /* aoqi@0: * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #include "precompiled.hpp" aoqi@0: #include "c1/c1_Canonicalizer.hpp" aoqi@0: #include "c1/c1_Optimizer.hpp" aoqi@0: #include "c1/c1_ValueMap.hpp" aoqi@0: #include "c1/c1_ValueSet.hpp" aoqi@0: #include "c1/c1_ValueStack.hpp" aoqi@0: #include "utilities/bitMap.inline.hpp" aoqi@0: #include "compiler/compileLog.hpp" aoqi@0: aoqi@0: define_array(ValueSetArray, ValueSet*); aoqi@0: define_stack(ValueSetList, ValueSetArray); aoqi@0: aoqi@0: aoqi@0: Optimizer::Optimizer(IR* ir) { aoqi@0: assert(ir->is_valid(), "IR must be valid"); aoqi@0: _ir = ir; aoqi@0: } aoqi@0: aoqi@0: class CE_Eliminator: public BlockClosure { aoqi@0: private: aoqi@0: IR* _hir; aoqi@0: int _cee_count; // the number of CEs successfully eliminated aoqi@0: int _ifop_count; // the number of IfOps successfully simplified aoqi@0: int _has_substitution; aoqi@0: aoqi@0: public: aoqi@0: CE_Eliminator(IR* hir) : _cee_count(0), _ifop_count(0), _hir(hir) { aoqi@0: _has_substitution = false; aoqi@0: _hir->iterate_preorder(this); aoqi@0: if (_has_substitution) { aoqi@0: // substituted some ifops/phis, so resolve the substitution aoqi@0: SubstitutionResolver sr(_hir); aoqi@0: } aoqi@0: aoqi@0: CompileLog* log = _hir->compilation()->log(); aoqi@0: if (log != NULL) aoqi@0: log->set_context("optimize name='cee'"); aoqi@0: } aoqi@0: aoqi@0: ~CE_Eliminator() { aoqi@0: CompileLog* log = _hir->compilation()->log(); aoqi@0: if (log != NULL) aoqi@0: log->clear_context(); // skip marker if nothing was printed aoqi@0: } aoqi@0: aoqi@0: int cee_count() const { return _cee_count; } aoqi@0: int ifop_count() const { return _ifop_count; } aoqi@0: aoqi@0: void adjust_exception_edges(BlockBegin* block, BlockBegin* sux) { aoqi@0: int e = sux->number_of_exception_handlers(); aoqi@0: for (int i = 0; i < e; i++) { aoqi@0: BlockBegin* xhandler = sux->exception_handler_at(i); aoqi@0: block->add_exception_handler(xhandler); aoqi@0: aoqi@0: assert(xhandler->is_predecessor(sux), "missing predecessor"); aoqi@0: if (sux->number_of_preds() == 0) { aoqi@0: // sux is disconnected from graph so disconnect from exception handlers aoqi@0: xhandler->remove_predecessor(sux); aoqi@0: } aoqi@0: if (!xhandler->is_predecessor(block)) { aoqi@0: xhandler->add_predecessor(block); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: virtual void block_do(BlockBegin* block); aoqi@0: aoqi@0: private: aoqi@0: Value make_ifop(Value x, Instruction::Condition cond, Value y, Value tval, Value fval); aoqi@0: }; aoqi@0: aoqi@0: void CE_Eliminator::block_do(BlockBegin* block) { aoqi@0: // 1) find conditional expression aoqi@0: // check if block ends with an If aoqi@0: If* if_ = block->end()->as_If(); aoqi@0: if (if_ == NULL) return; aoqi@0: aoqi@0: // check if If works on int or object types aoqi@0: // (we cannot handle If's working on long, float or doubles yet, aoqi@0: // since IfOp doesn't support them - these If's show up if cmp aoqi@0: // operations followed by If's are eliminated) aoqi@0: ValueType* if_type = if_->x()->type(); aoqi@0: if (!if_type->is_int() && !if_type->is_object()) return; aoqi@0: aoqi@0: BlockBegin* t_block = if_->tsux(); aoqi@0: BlockBegin* f_block = if_->fsux(); aoqi@0: Instruction* t_cur = t_block->next(); aoqi@0: Instruction* f_cur = f_block->next(); aoqi@0: aoqi@0: // one Constant may be present between BlockBegin and BlockEnd aoqi@0: Value t_const = NULL; aoqi@0: Value f_const = NULL; aoqi@0: if (t_cur->as_Constant() != NULL && !t_cur->can_trap()) { aoqi@0: t_const = t_cur; aoqi@0: t_cur = t_cur->next(); aoqi@0: } aoqi@0: if (f_cur->as_Constant() != NULL && !f_cur->can_trap()) { aoqi@0: f_const = f_cur; aoqi@0: f_cur = f_cur->next(); aoqi@0: } aoqi@0: aoqi@0: // check if both branches end with a goto aoqi@0: Goto* t_goto = t_cur->as_Goto(); aoqi@0: if (t_goto == NULL) return; aoqi@0: Goto* f_goto = f_cur->as_Goto(); aoqi@0: if (f_goto == NULL) return; aoqi@0: aoqi@0: // check if both gotos merge into the same block aoqi@0: BlockBegin* sux = t_goto->default_sux(); aoqi@0: if (sux != f_goto->default_sux()) return; aoqi@0: aoqi@0: // check if at least one word was pushed on sux_state aoqi@0: // inlining depths must match aoqi@0: ValueStack* if_state = if_->state(); aoqi@0: ValueStack* sux_state = sux->state(); aoqi@0: if (if_state->scope()->level() > sux_state->scope()->level()) { aoqi@0: while (sux_state->scope() != if_state->scope()) { aoqi@0: if_state = if_state->caller_state(); aoqi@0: assert(if_state != NULL, "states do not match up"); aoqi@0: } aoqi@0: } else if (if_state->scope()->level() < sux_state->scope()->level()) { aoqi@0: while (sux_state->scope() != if_state->scope()) { aoqi@0: sux_state = sux_state->caller_state(); aoqi@0: assert(sux_state != NULL, "states do not match up"); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: if (sux_state->stack_size() <= if_state->stack_size()) return; aoqi@0: aoqi@0: // check if phi function is present at end of successor stack and that aoqi@0: // only this phi was pushed on the stack aoqi@0: Value sux_phi = sux_state->stack_at(if_state->stack_size()); aoqi@0: if (sux_phi == NULL || sux_phi->as_Phi() == NULL || sux_phi->as_Phi()->block() != sux) return; aoqi@0: if (sux_phi->type()->size() != sux_state->stack_size() - if_state->stack_size()) return; aoqi@0: aoqi@0: // get the values that were pushed in the true- and false-branch aoqi@0: Value t_value = t_goto->state()->stack_at(if_state->stack_size()); aoqi@0: Value f_value = f_goto->state()->stack_at(if_state->stack_size()); aoqi@0: aoqi@0: // backend does not support floats aoqi@0: assert(t_value->type()->base() == f_value->type()->base(), "incompatible types"); aoqi@0: if (t_value->type()->is_float_kind()) return; aoqi@0: aoqi@0: // check that successor has no other phi functions but sux_phi aoqi@0: // this can happen when t_block or f_block contained additonal stores to local variables aoqi@0: // that are no longer represented by explicit instructions aoqi@0: for_each_phi_fun(sux, phi, aoqi@0: if (phi != sux_phi) return; aoqi@0: ); aoqi@0: // true and false blocks can't have phis aoqi@0: for_each_phi_fun(t_block, phi, return; ); aoqi@0: for_each_phi_fun(f_block, phi, return; ); aoqi@0: aoqi@0: // 2) substitute conditional expression aoqi@0: // with an IfOp followed by a Goto aoqi@0: // cut if_ away and get node before aoqi@0: Instruction* cur_end = if_->prev(); aoqi@0: aoqi@0: // append constants of true- and false-block if necessary aoqi@0: // clone constants because original block must not be destroyed aoqi@0: assert((t_value != f_const && f_value != t_const) || t_const == f_const, "mismatch"); aoqi@0: if (t_value == t_const) { aoqi@0: t_value = new Constant(t_const->type()); aoqi@0: NOT_PRODUCT(t_value->set_printable_bci(if_->printable_bci())); aoqi@0: cur_end = cur_end->set_next(t_value); aoqi@0: } aoqi@0: if (f_value == f_const) { aoqi@0: f_value = new Constant(f_const->type()); aoqi@0: NOT_PRODUCT(f_value->set_printable_bci(if_->printable_bci())); aoqi@0: cur_end = cur_end->set_next(f_value); aoqi@0: } aoqi@0: aoqi@0: Value result = make_ifop(if_->x(), if_->cond(), if_->y(), t_value, f_value); aoqi@0: assert(result != NULL, "make_ifop must return a non-null instruction"); aoqi@0: if (!result->is_linked() && result->can_be_linked()) { aoqi@0: NOT_PRODUCT(result->set_printable_bci(if_->printable_bci())); aoqi@0: cur_end = cur_end->set_next(result); aoqi@0: } aoqi@0: aoqi@0: // append Goto to successor aoqi@0: ValueStack* state_before = if_->state_before(); aoqi@0: Goto* goto_ = new Goto(sux, state_before, if_->is_safepoint() || t_goto->is_safepoint() || f_goto->is_safepoint()); aoqi@0: aoqi@0: // prepare state for Goto aoqi@0: ValueStack* goto_state = if_state; aoqi@0: goto_state = goto_state->copy(ValueStack::StateAfter, goto_state->bci()); aoqi@0: goto_state->push(result->type(), result); aoqi@0: assert(goto_state->is_same(sux_state), "states must match now"); aoqi@0: goto_->set_state(goto_state); aoqi@0: aoqi@0: cur_end = cur_end->set_next(goto_, goto_state->bci()); aoqi@0: aoqi@0: // Adjust control flow graph aoqi@0: BlockBegin::disconnect_edge(block, t_block); aoqi@0: BlockBegin::disconnect_edge(block, f_block); aoqi@0: if (t_block->number_of_preds() == 0) { aoqi@0: BlockBegin::disconnect_edge(t_block, sux); aoqi@0: } aoqi@0: adjust_exception_edges(block, t_block); aoqi@0: if (f_block->number_of_preds() == 0) { aoqi@0: BlockBegin::disconnect_edge(f_block, sux); aoqi@0: } aoqi@0: adjust_exception_edges(block, f_block); aoqi@0: aoqi@0: // update block end aoqi@0: block->set_end(goto_); aoqi@0: aoqi@0: // substitute the phi if possible aoqi@0: if (sux_phi->as_Phi()->operand_count() == 1) { aoqi@0: assert(sux_phi->as_Phi()->operand_at(0) == result, "screwed up phi"); aoqi@0: sux_phi->set_subst(result); aoqi@0: _has_substitution = true; aoqi@0: } aoqi@0: aoqi@0: // 3) successfully eliminated a conditional expression aoqi@0: _cee_count++; aoqi@0: if (PrintCEE) { aoqi@0: tty->print_cr("%d. CEE in B%d (B%d B%d)", cee_count(), block->block_id(), t_block->block_id(), f_block->block_id()); aoqi@0: tty->print_cr("%d. IfOp in B%d", ifop_count(), block->block_id()); aoqi@0: } aoqi@0: aoqi@0: _hir->verify(); aoqi@0: } aoqi@0: aoqi@0: Value CE_Eliminator::make_ifop(Value x, Instruction::Condition cond, Value y, Value tval, Value fval) { aoqi@0: if (!OptimizeIfOps) { aoqi@0: return new IfOp(x, cond, y, tval, fval); aoqi@0: } aoqi@0: aoqi@0: tval = tval->subst(); aoqi@0: fval = fval->subst(); aoqi@0: if (tval == fval) { aoqi@0: _ifop_count++; aoqi@0: return tval; aoqi@0: } aoqi@0: aoqi@0: x = x->subst(); aoqi@0: y = y->subst(); aoqi@0: aoqi@0: Constant* y_const = y->as_Constant(); aoqi@0: if (y_const != NULL) { aoqi@0: IfOp* x_ifop = x->as_IfOp(); aoqi@0: if (x_ifop != NULL) { // x is an ifop, y is a constant aoqi@0: Constant* x_tval_const = x_ifop->tval()->subst()->as_Constant(); aoqi@0: Constant* x_fval_const = x_ifop->fval()->subst()->as_Constant(); aoqi@0: aoqi@0: if (x_tval_const != NULL && x_fval_const != NULL) { aoqi@0: Instruction::Condition x_ifop_cond = x_ifop->cond(); aoqi@0: aoqi@0: Constant::CompareResult t_compare_res = x_tval_const->compare(cond, y_const); aoqi@0: Constant::CompareResult f_compare_res = x_fval_const->compare(cond, y_const); aoqi@0: aoqi@0: // not_comparable here is a valid return in case we're comparing unloaded oop constants aoqi@0: if (t_compare_res != Constant::not_comparable && f_compare_res != Constant::not_comparable) { aoqi@0: Value new_tval = t_compare_res == Constant::cond_true ? tval : fval; aoqi@0: Value new_fval = f_compare_res == Constant::cond_true ? tval : fval; aoqi@0: aoqi@0: _ifop_count++; aoqi@0: if (new_tval == new_fval) { aoqi@0: return new_tval; aoqi@0: } else { aoqi@0: return new IfOp(x_ifop->x(), x_ifop_cond, x_ifop->y(), new_tval, new_fval); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } else { aoqi@0: Constant* x_const = x->as_Constant(); aoqi@0: if (x_const != NULL) { // x and y are constants aoqi@0: Constant::CompareResult x_compare_res = x_const->compare(cond, y_const); aoqi@0: // not_comparable here is a valid return in case we're comparing unloaded oop constants aoqi@0: if (x_compare_res != Constant::not_comparable) { aoqi@0: _ifop_count++; aoqi@0: return x_compare_res == Constant::cond_true ? tval : fval; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: return new IfOp(x, cond, y, tval, fval); aoqi@0: } aoqi@0: aoqi@0: void Optimizer::eliminate_conditional_expressions() { aoqi@0: // find conditional expressions & replace them with IfOps aoqi@0: CE_Eliminator ce(ir()); aoqi@0: } aoqi@0: aoqi@0: class BlockMerger: public BlockClosure { aoqi@0: private: aoqi@0: IR* _hir; aoqi@0: int _merge_count; // the number of block pairs successfully merged aoqi@0: aoqi@0: public: aoqi@0: BlockMerger(IR* hir) aoqi@0: : _hir(hir) aoqi@0: , _merge_count(0) aoqi@0: { aoqi@0: _hir->iterate_preorder(this); aoqi@0: CompileLog* log = _hir->compilation()->log(); aoqi@0: if (log != NULL) aoqi@0: log->set_context("optimize name='eliminate_blocks'"); aoqi@0: } aoqi@0: aoqi@0: ~BlockMerger() { aoqi@0: CompileLog* log = _hir->compilation()->log(); aoqi@0: if (log != NULL) aoqi@0: log->clear_context(); // skip marker if nothing was printed aoqi@0: } aoqi@0: aoqi@0: bool try_merge(BlockBegin* block) { aoqi@0: BlockEnd* end = block->end(); aoqi@0: if (end->as_Goto() != NULL) { aoqi@0: assert(end->number_of_sux() == 1, "end must have exactly one successor"); aoqi@0: // Note: It would be sufficient to check for the number of successors (= 1) aoqi@0: // in order to decide if this block can be merged potentially. That aoqi@0: // would then also include switch statements w/ only a default case. aoqi@0: // However, in that case we would need to make sure the switch tag aoqi@0: // expression is executed if it can produce observable side effects. aoqi@0: // We should probably have the canonicalizer simplifying such switch aoqi@0: // statements and then we are sure we don't miss these merge opportunities aoqi@0: // here (was bug - gri 7/7/99). aoqi@0: BlockBegin* sux = end->default_sux(); aoqi@0: if (sux->number_of_preds() == 1 && !sux->is_entry_block() && !end->is_safepoint()) { aoqi@0: // merge the two blocks aoqi@0: aoqi@0: #ifdef ASSERT aoqi@0: // verify that state at the end of block and at the beginning of sux are equal aoqi@0: // no phi functions must be present at beginning of sux aoqi@0: ValueStack* sux_state = sux->state(); aoqi@0: ValueStack* end_state = end->state(); aoqi@0: aoqi@0: assert(end_state->scope() == sux_state->scope(), "scopes must match"); aoqi@0: assert(end_state->stack_size() == sux_state->stack_size(), "stack not equal"); aoqi@0: assert(end_state->locals_size() == sux_state->locals_size(), "locals not equal"); aoqi@0: aoqi@0: int index; aoqi@0: Value sux_value; aoqi@0: for_each_stack_value(sux_state, index, sux_value) { aoqi@0: assert(sux_value == end_state->stack_at(index), "stack not equal"); aoqi@0: } aoqi@0: for_each_local_value(sux_state, index, sux_value) { aoqi@0: assert(sux_value == end_state->local_at(index), "locals not equal"); aoqi@0: } aoqi@0: assert(sux_state->caller_state() == end_state->caller_state(), "caller not equal"); aoqi@0: #endif aoqi@0: aoqi@0: // find instruction before end & append first instruction of sux block aoqi@0: Instruction* prev = end->prev(); aoqi@0: Instruction* next = sux->next(); aoqi@0: assert(prev->as_BlockEnd() == NULL, "must not be a BlockEnd"); aoqi@0: prev->set_next(next); aoqi@0: prev->fixup_block_pointers(); aoqi@0: sux->disconnect_from_graph(); aoqi@0: block->set_end(sux->end()); aoqi@0: // add exception handlers of deleted block, if any aoqi@0: for (int k = 0; k < sux->number_of_exception_handlers(); k++) { aoqi@0: BlockBegin* xhandler = sux->exception_handler_at(k); aoqi@0: block->add_exception_handler(xhandler); aoqi@0: aoqi@0: // also substitute predecessor of exception handler aoqi@0: assert(xhandler->is_predecessor(sux), "missing predecessor"); aoqi@0: xhandler->remove_predecessor(sux); aoqi@0: if (!xhandler->is_predecessor(block)) { aoqi@0: xhandler->add_predecessor(block); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // debugging output aoqi@0: _merge_count++; aoqi@0: if (PrintBlockElimination) { aoqi@0: tty->print_cr("%d. merged B%d & B%d (stack size = %d)", aoqi@0: _merge_count, block->block_id(), sux->block_id(), sux->state()->stack_size()); aoqi@0: } aoqi@0: aoqi@0: _hir->verify(); aoqi@0: aoqi@0: If* if_ = block->end()->as_If(); aoqi@0: if (if_) { aoqi@0: IfOp* ifop = if_->x()->as_IfOp(); aoqi@0: Constant* con = if_->y()->as_Constant(); aoqi@0: bool swapped = false; aoqi@0: if (!con || !ifop) { aoqi@0: ifop = if_->y()->as_IfOp(); aoqi@0: con = if_->x()->as_Constant(); aoqi@0: swapped = true; aoqi@0: } aoqi@0: if (con && ifop) { aoqi@0: Constant* tval = ifop->tval()->as_Constant(); aoqi@0: Constant* fval = ifop->fval()->as_Constant(); aoqi@0: if (tval && fval) { aoqi@0: // Find the instruction before if_, starting with ifop. aoqi@0: // When if_ and ifop are not in the same block, prev aoqi@0: // becomes NULL In such (rare) cases it is not aoqi@0: // profitable to perform the optimization. aoqi@0: Value prev = ifop; aoqi@0: while (prev != NULL && prev->next() != if_) { aoqi@0: prev = prev->next(); aoqi@0: } aoqi@0: aoqi@0: if (prev != NULL) { aoqi@0: Instruction::Condition cond = if_->cond(); aoqi@0: BlockBegin* tsux = if_->tsux(); aoqi@0: BlockBegin* fsux = if_->fsux(); aoqi@0: if (swapped) { aoqi@0: cond = Instruction::mirror(cond); aoqi@0: } aoqi@0: aoqi@0: BlockBegin* tblock = tval->compare(cond, con, tsux, fsux); aoqi@0: BlockBegin* fblock = fval->compare(cond, con, tsux, fsux); aoqi@0: if (tblock != fblock && !if_->is_safepoint()) { aoqi@0: If* newif = new If(ifop->x(), ifop->cond(), false, ifop->y(), aoqi@0: tblock, fblock, if_->state_before(), if_->is_safepoint()); aoqi@0: newif->set_state(if_->state()->copy()); aoqi@0: aoqi@0: assert(prev->next() == if_, "must be guaranteed by above search"); aoqi@0: NOT_PRODUCT(newif->set_printable_bci(if_->printable_bci())); aoqi@0: prev->set_next(newif); aoqi@0: block->set_end(newif); aoqi@0: aoqi@0: _merge_count++; aoqi@0: if (PrintBlockElimination) { aoqi@0: tty->print_cr("%d. replaced If and IfOp at end of B%d with single If", _merge_count, block->block_id()); aoqi@0: } aoqi@0: aoqi@0: _hir->verify(); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: return true; aoqi@0: } aoqi@0: } aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: virtual void block_do(BlockBegin* block) { aoqi@0: _hir->verify(); aoqi@0: // repeat since the same block may merge again aoqi@0: while (try_merge(block)) { aoqi@0: _hir->verify(); aoqi@0: } aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: aoqi@0: void Optimizer::eliminate_blocks() { aoqi@0: // merge blocks if possible aoqi@0: BlockMerger bm(ir()); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: class NullCheckEliminator; aoqi@0: class NullCheckVisitor: public InstructionVisitor { aoqi@0: private: aoqi@0: NullCheckEliminator* _nce; aoqi@0: NullCheckEliminator* nce() { return _nce; } aoqi@0: aoqi@0: public: aoqi@0: NullCheckVisitor() {} aoqi@0: aoqi@0: void set_eliminator(NullCheckEliminator* nce) { _nce = nce; } aoqi@0: aoqi@0: void do_Phi (Phi* x); aoqi@0: void do_Local (Local* x); aoqi@0: void do_Constant (Constant* x); aoqi@0: void do_LoadField (LoadField* x); aoqi@0: void do_StoreField (StoreField* x); aoqi@0: void do_ArrayLength (ArrayLength* x); aoqi@0: void do_LoadIndexed (LoadIndexed* x); aoqi@0: void do_StoreIndexed (StoreIndexed* x); aoqi@0: void do_NegateOp (NegateOp* x); aoqi@0: void do_ArithmeticOp (ArithmeticOp* x); aoqi@0: void do_ShiftOp (ShiftOp* x); aoqi@0: void do_LogicOp (LogicOp* x); aoqi@0: void do_CompareOp (CompareOp* x); aoqi@0: void do_IfOp (IfOp* x); aoqi@0: void do_Convert (Convert* x); aoqi@0: void do_NullCheck (NullCheck* x); aoqi@0: void do_TypeCast (TypeCast* x); aoqi@0: void do_Invoke (Invoke* x); aoqi@0: void do_NewInstance (NewInstance* x); aoqi@0: void do_NewTypeArray (NewTypeArray* x); aoqi@0: void do_NewObjectArray (NewObjectArray* x); aoqi@0: void do_NewMultiArray (NewMultiArray* x); aoqi@0: void do_CheckCast (CheckCast* x); aoqi@0: void do_InstanceOf (InstanceOf* x); aoqi@0: void do_MonitorEnter (MonitorEnter* x); aoqi@0: void do_MonitorExit (MonitorExit* x); aoqi@0: void do_Intrinsic (Intrinsic* x); aoqi@0: void do_BlockBegin (BlockBegin* x); aoqi@0: void do_Goto (Goto* x); aoqi@0: void do_If (If* x); aoqi@0: void do_IfInstanceOf (IfInstanceOf* x); aoqi@0: void do_TableSwitch (TableSwitch* x); aoqi@0: void do_LookupSwitch (LookupSwitch* x); aoqi@0: void do_Return (Return* x); aoqi@0: void do_Throw (Throw* x); aoqi@0: void do_Base (Base* x); aoqi@0: void do_OsrEntry (OsrEntry* x); aoqi@0: void do_ExceptionObject(ExceptionObject* x); aoqi@0: void do_RoundFP (RoundFP* x); aoqi@0: void do_UnsafeGetRaw (UnsafeGetRaw* x); aoqi@0: void do_UnsafePutRaw (UnsafePutRaw* x); aoqi@0: void do_UnsafeGetObject(UnsafeGetObject* x); aoqi@0: void do_UnsafePutObject(UnsafePutObject* x); aoqi@0: void do_UnsafeGetAndSetObject(UnsafeGetAndSetObject* x); aoqi@0: void do_UnsafePrefetchRead (UnsafePrefetchRead* x); aoqi@0: void do_UnsafePrefetchWrite(UnsafePrefetchWrite* x); aoqi@0: void do_ProfileCall (ProfileCall* x); aoqi@0: void do_ProfileReturnType (ProfileReturnType* x); aoqi@0: void do_ProfileInvoke (ProfileInvoke* x); aoqi@0: void do_RuntimeCall (RuntimeCall* x); aoqi@0: void do_MemBar (MemBar* x); aoqi@0: void do_RangeCheckPredicate(RangeCheckPredicate* x); aoqi@0: #ifdef ASSERT aoqi@0: void do_Assert (Assert* x); aoqi@0: #endif aoqi@0: }; aoqi@0: aoqi@0: aoqi@0: // Because of a static contained within (for the purpose of iteration aoqi@0: // over instructions), it is only valid to have one of these active at aoqi@0: // a time aoqi@0: class NullCheckEliminator: public ValueVisitor { aoqi@0: private: aoqi@0: Optimizer* _opt; aoqi@0: aoqi@0: ValueSet* _visitable_instructions; // Visit each instruction only once per basic block aoqi@0: BlockList* _work_list; // Basic blocks to visit aoqi@0: aoqi@0: bool visitable(Value x) { aoqi@0: assert(_visitable_instructions != NULL, "check"); aoqi@0: return _visitable_instructions->contains(x); aoqi@0: } aoqi@0: void mark_visited(Value x) { aoqi@0: assert(_visitable_instructions != NULL, "check"); aoqi@0: _visitable_instructions->remove(x); aoqi@0: } aoqi@0: void mark_visitable(Value x) { aoqi@0: assert(_visitable_instructions != NULL, "check"); aoqi@0: _visitable_instructions->put(x); aoqi@0: } aoqi@0: void clear_visitable_state() { aoqi@0: assert(_visitable_instructions != NULL, "check"); aoqi@0: _visitable_instructions->clear(); aoqi@0: } aoqi@0: aoqi@0: ValueSet* _set; // current state, propagated to subsequent BlockBegins aoqi@0: ValueSetList _block_states; // BlockBegin null-check states for all processed blocks aoqi@0: NullCheckVisitor _visitor; aoqi@0: NullCheck* _last_explicit_null_check; aoqi@0: aoqi@0: bool set_contains(Value x) { assert(_set != NULL, "check"); return _set->contains(x); } aoqi@0: void set_put (Value x) { assert(_set != NULL, "check"); _set->put(x); } aoqi@0: void set_remove (Value x) { assert(_set != NULL, "check"); _set->remove(x); } aoqi@0: aoqi@0: BlockList* work_list() { return _work_list; } aoqi@0: aoqi@0: void iterate_all(); aoqi@0: void iterate_one(BlockBegin* block); aoqi@0: aoqi@0: ValueSet* state() { return _set; } aoqi@0: void set_state_from (ValueSet* state) { _set->set_from(state); } aoqi@0: ValueSet* state_for (BlockBegin* block) { return _block_states[block->block_id()]; } aoqi@0: void set_state_for (BlockBegin* block, ValueSet* stack) { _block_states[block->block_id()] = stack; } aoqi@0: // Returns true if caused a change in the block's state. aoqi@0: bool merge_state_for(BlockBegin* block, aoqi@0: ValueSet* incoming_state); aoqi@0: aoqi@0: public: aoqi@0: // constructor aoqi@0: NullCheckEliminator(Optimizer* opt) aoqi@0: : _opt(opt) aoqi@0: , _set(new ValueSet()) aoqi@0: , _last_explicit_null_check(NULL) aoqi@0: , _block_states(BlockBegin::number_of_blocks(), NULL) aoqi@0: , _work_list(new BlockList()) { aoqi@0: _visitable_instructions = new ValueSet(); aoqi@0: _visitor.set_eliminator(this); aoqi@0: CompileLog* log = _opt->ir()->compilation()->log(); aoqi@0: if (log != NULL) aoqi@0: log->set_context("optimize name='null_check_elimination'"); aoqi@0: } aoqi@0: aoqi@0: ~NullCheckEliminator() { aoqi@0: CompileLog* log = _opt->ir()->compilation()->log(); aoqi@0: if (log != NULL) aoqi@0: log->clear_context(); // skip marker if nothing was printed aoqi@0: } aoqi@0: aoqi@0: Optimizer* opt() { return _opt; } aoqi@0: IR* ir () { return opt()->ir(); } aoqi@0: aoqi@0: // Process a graph aoqi@0: void iterate(BlockBegin* root); aoqi@0: aoqi@0: void visit(Value* f); aoqi@0: aoqi@0: // In some situations (like NullCheck(x); getfield(x)) the debug aoqi@0: // information from the explicit NullCheck can be used to populate aoqi@0: // the getfield, even if the two instructions are in different aoqi@0: // scopes; this allows implicit null checks to be used but the aoqi@0: // correct exception information to be generated. We must clear the aoqi@0: // last-traversed NullCheck when we reach a potentially-exception- aoqi@0: // throwing instruction, as well as in some other cases. aoqi@0: void set_last_explicit_null_check(NullCheck* check) { _last_explicit_null_check = check; } aoqi@0: NullCheck* last_explicit_null_check() { return _last_explicit_null_check; } aoqi@0: Value last_explicit_null_check_obj() { return (_last_explicit_null_check aoqi@0: ? _last_explicit_null_check->obj() aoqi@0: : NULL); } aoqi@0: NullCheck* consume_last_explicit_null_check() { aoqi@0: _last_explicit_null_check->unpin(Instruction::PinExplicitNullCheck); aoqi@0: _last_explicit_null_check->set_can_trap(false); aoqi@0: return _last_explicit_null_check; aoqi@0: } aoqi@0: void clear_last_explicit_null_check() { _last_explicit_null_check = NULL; } aoqi@0: aoqi@0: // Handlers for relevant instructions aoqi@0: // (separated out from NullCheckVisitor for clarity) aoqi@0: aoqi@0: // The basic contract is that these must leave the instruction in aoqi@0: // the desired state; must not assume anything about the state of aoqi@0: // the instruction. We make multiple passes over some basic blocks aoqi@0: // and the last pass is the only one whose result is valid. aoqi@0: void handle_AccessField (AccessField* x); aoqi@0: void handle_ArrayLength (ArrayLength* x); aoqi@0: void handle_LoadIndexed (LoadIndexed* x); aoqi@0: void handle_StoreIndexed (StoreIndexed* x); aoqi@0: void handle_NullCheck (NullCheck* x); aoqi@0: void handle_Invoke (Invoke* x); aoqi@0: void handle_NewInstance (NewInstance* x); aoqi@0: void handle_NewArray (NewArray* x); aoqi@0: void handle_AccessMonitor (AccessMonitor* x); aoqi@0: void handle_Intrinsic (Intrinsic* x); aoqi@0: void handle_ExceptionObject (ExceptionObject* x); aoqi@0: void handle_Phi (Phi* x); aoqi@0: void handle_ProfileCall (ProfileCall* x); aoqi@0: void handle_ProfileReturnType (ProfileReturnType* x); aoqi@0: }; aoqi@0: aoqi@0: aoqi@0: // NEEDS_CLEANUP aoqi@0: // There may be other instructions which need to clear the last aoqi@0: // explicit null check. Anything across which we can not hoist the aoqi@0: // debug information for a NullCheck instruction must clear it. It aoqi@0: // might be safer to pattern match "NullCheck ; {AccessField, aoqi@0: // ArrayLength, LoadIndexed}" but it is more easily structured this way. aoqi@0: // Should test to see performance hit of clearing it for all handlers aoqi@0: // with empty bodies below. If it is negligible then we should leave aoqi@0: // that in for safety, otherwise should think more about it. aoqi@0: void NullCheckVisitor::do_Phi (Phi* x) { nce()->handle_Phi(x); } aoqi@0: void NullCheckVisitor::do_Local (Local* x) {} aoqi@0: void NullCheckVisitor::do_Constant (Constant* x) { /* FIXME: handle object constants */ } aoqi@0: void NullCheckVisitor::do_LoadField (LoadField* x) { nce()->handle_AccessField(x); } aoqi@0: void NullCheckVisitor::do_StoreField (StoreField* x) { nce()->handle_AccessField(x); } aoqi@0: void NullCheckVisitor::do_ArrayLength (ArrayLength* x) { nce()->handle_ArrayLength(x); } aoqi@0: void NullCheckVisitor::do_LoadIndexed (LoadIndexed* x) { nce()->handle_LoadIndexed(x); } aoqi@0: void NullCheckVisitor::do_StoreIndexed (StoreIndexed* x) { nce()->handle_StoreIndexed(x); } aoqi@0: void NullCheckVisitor::do_NegateOp (NegateOp* x) {} aoqi@0: void NullCheckVisitor::do_ArithmeticOp (ArithmeticOp* x) { if (x->can_trap()) nce()->clear_last_explicit_null_check(); } aoqi@0: void NullCheckVisitor::do_ShiftOp (ShiftOp* x) {} aoqi@0: void NullCheckVisitor::do_LogicOp (LogicOp* x) {} aoqi@0: void NullCheckVisitor::do_CompareOp (CompareOp* x) {} aoqi@0: void NullCheckVisitor::do_IfOp (IfOp* x) {} aoqi@0: void NullCheckVisitor::do_Convert (Convert* x) {} aoqi@0: void NullCheckVisitor::do_NullCheck (NullCheck* x) { nce()->handle_NullCheck(x); } aoqi@0: void NullCheckVisitor::do_TypeCast (TypeCast* x) {} aoqi@0: void NullCheckVisitor::do_Invoke (Invoke* x) { nce()->handle_Invoke(x); } aoqi@0: void NullCheckVisitor::do_NewInstance (NewInstance* x) { nce()->handle_NewInstance(x); } aoqi@0: void NullCheckVisitor::do_NewTypeArray (NewTypeArray* x) { nce()->handle_NewArray(x); } aoqi@0: void NullCheckVisitor::do_NewObjectArray (NewObjectArray* x) { nce()->handle_NewArray(x); } aoqi@0: void NullCheckVisitor::do_NewMultiArray (NewMultiArray* x) { nce()->handle_NewArray(x); } aoqi@0: void NullCheckVisitor::do_CheckCast (CheckCast* x) { nce()->clear_last_explicit_null_check(); } aoqi@0: void NullCheckVisitor::do_InstanceOf (InstanceOf* x) {} aoqi@0: void NullCheckVisitor::do_MonitorEnter (MonitorEnter* x) { nce()->handle_AccessMonitor(x); } aoqi@0: void NullCheckVisitor::do_MonitorExit (MonitorExit* x) { nce()->handle_AccessMonitor(x); } aoqi@0: void NullCheckVisitor::do_Intrinsic (Intrinsic* x) { nce()->handle_Intrinsic(x); } aoqi@0: void NullCheckVisitor::do_BlockBegin (BlockBegin* x) {} aoqi@0: void NullCheckVisitor::do_Goto (Goto* x) {} aoqi@0: void NullCheckVisitor::do_If (If* x) {} aoqi@0: void NullCheckVisitor::do_IfInstanceOf (IfInstanceOf* x) {} aoqi@0: void NullCheckVisitor::do_TableSwitch (TableSwitch* x) {} aoqi@0: void NullCheckVisitor::do_LookupSwitch (LookupSwitch* x) {} aoqi@0: void NullCheckVisitor::do_Return (Return* x) {} aoqi@0: void NullCheckVisitor::do_Throw (Throw* x) { nce()->clear_last_explicit_null_check(); } aoqi@0: void NullCheckVisitor::do_Base (Base* x) {} aoqi@0: void NullCheckVisitor::do_OsrEntry (OsrEntry* x) {} aoqi@0: void NullCheckVisitor::do_ExceptionObject(ExceptionObject* x) { nce()->handle_ExceptionObject(x); } aoqi@0: void NullCheckVisitor::do_RoundFP (RoundFP* x) {} aoqi@0: void NullCheckVisitor::do_UnsafeGetRaw (UnsafeGetRaw* x) {} aoqi@0: void NullCheckVisitor::do_UnsafePutRaw (UnsafePutRaw* x) {} aoqi@0: void NullCheckVisitor::do_UnsafeGetObject(UnsafeGetObject* x) {} aoqi@0: void NullCheckVisitor::do_UnsafePutObject(UnsafePutObject* x) {} aoqi@0: void NullCheckVisitor::do_UnsafeGetAndSetObject(UnsafeGetAndSetObject* x) {} aoqi@0: void NullCheckVisitor::do_UnsafePrefetchRead (UnsafePrefetchRead* x) {} aoqi@0: void NullCheckVisitor::do_UnsafePrefetchWrite(UnsafePrefetchWrite* x) {} aoqi@0: void NullCheckVisitor::do_ProfileCall (ProfileCall* x) { nce()->clear_last_explicit_null_check(); aoqi@0: nce()->handle_ProfileCall(x); } aoqi@0: void NullCheckVisitor::do_ProfileReturnType (ProfileReturnType* x) { nce()->handle_ProfileReturnType(x); } aoqi@0: void NullCheckVisitor::do_ProfileInvoke (ProfileInvoke* x) {} aoqi@0: void NullCheckVisitor::do_RuntimeCall (RuntimeCall* x) {} aoqi@0: void NullCheckVisitor::do_MemBar (MemBar* x) {} aoqi@0: void NullCheckVisitor::do_RangeCheckPredicate(RangeCheckPredicate* x) {} aoqi@0: #ifdef ASSERT aoqi@0: void NullCheckVisitor::do_Assert (Assert* x) {} aoqi@0: #endif aoqi@0: aoqi@0: void NullCheckEliminator::visit(Value* p) { aoqi@0: assert(*p != NULL, "should not find NULL instructions"); aoqi@0: if (visitable(*p)) { aoqi@0: mark_visited(*p); aoqi@0: (*p)->visit(&_visitor); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: bool NullCheckEliminator::merge_state_for(BlockBegin* block, ValueSet* incoming_state) { aoqi@0: ValueSet* state = state_for(block); aoqi@0: if (state == NULL) { aoqi@0: state = incoming_state->copy(); aoqi@0: set_state_for(block, state); aoqi@0: return true; aoqi@0: } else { aoqi@0: bool changed = state->set_intersect(incoming_state); aoqi@0: if (PrintNullCheckElimination && changed) { aoqi@0: tty->print_cr("Block %d's null check state changed", block->block_id()); aoqi@0: } aoqi@0: return changed; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void NullCheckEliminator::iterate_all() { aoqi@0: while (work_list()->length() > 0) { aoqi@0: iterate_one(work_list()->pop()); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void NullCheckEliminator::iterate_one(BlockBegin* block) { aoqi@0: clear_visitable_state(); aoqi@0: // clear out an old explicit null checks aoqi@0: set_last_explicit_null_check(NULL); aoqi@0: aoqi@0: if (PrintNullCheckElimination) { aoqi@0: tty->print_cr(" ...iterating block %d in null check elimination for %s::%s%s", aoqi@0: block->block_id(), aoqi@0: ir()->method()->holder()->name()->as_utf8(), aoqi@0: ir()->method()->name()->as_utf8(), aoqi@0: ir()->method()->signature()->as_symbol()->as_utf8()); aoqi@0: } aoqi@0: aoqi@0: // Create new state if none present (only happens at root) aoqi@0: if (state_for(block) == NULL) { aoqi@0: ValueSet* tmp_state = new ValueSet(); aoqi@0: set_state_for(block, tmp_state); aoqi@0: // Initial state is that local 0 (receiver) is non-null for aoqi@0: // non-static methods aoqi@0: ValueStack* stack = block->state(); aoqi@0: IRScope* scope = stack->scope(); aoqi@0: ciMethod* method = scope->method(); aoqi@0: if (!method->is_static()) { aoqi@0: Local* local0 = stack->local_at(0)->as_Local(); aoqi@0: assert(local0 != NULL, "must be"); aoqi@0: assert(local0->type() == objectType, "invalid type of receiver"); aoqi@0: aoqi@0: if (local0 != NULL) { aoqi@0: // Local 0 is used in this scope aoqi@0: tmp_state->put(local0); aoqi@0: if (PrintNullCheckElimination) { aoqi@0: tty->print_cr("Local 0 (value %d) proven non-null upon entry", local0->id()); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Must copy block's state to avoid mutating it during iteration aoqi@0: // through the block -- otherwise "not-null" states can accidentally aoqi@0: // propagate "up" through the block during processing of backward aoqi@0: // branches and algorithm is incorrect (and does not converge) aoqi@0: set_state_from(state_for(block)); aoqi@0: aoqi@0: // allow visiting of Phis belonging to this block aoqi@0: for_each_phi_fun(block, phi, aoqi@0: mark_visitable(phi); aoqi@0: ); aoqi@0: aoqi@0: BlockEnd* e = block->end(); aoqi@0: assert(e != NULL, "incomplete graph"); aoqi@0: int i; aoqi@0: aoqi@0: // Propagate the state before this block into the exception aoqi@0: // handlers. They aren't true successors since we aren't guaranteed aoqi@0: // to execute the whole block before executing them. Also putting aoqi@0: // them on first seems to help reduce the amount of iteration to aoqi@0: // reach a fixed point. aoqi@0: for (i = 0; i < block->number_of_exception_handlers(); i++) { aoqi@0: BlockBegin* next = block->exception_handler_at(i); aoqi@0: if (merge_state_for(next, state())) { aoqi@0: if (!work_list()->contains(next)) { aoqi@0: work_list()->push(next); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Iterate through block, updating state. aoqi@0: for (Instruction* instr = block; instr != NULL; instr = instr->next()) { aoqi@0: // Mark instructions in this block as visitable as they are seen aoqi@0: // in the instruction list. This keeps the iteration from aoqi@0: // visiting instructions which are references in other blocks or aoqi@0: // visiting instructions more than once. aoqi@0: mark_visitable(instr); aoqi@0: if (instr->is_pinned() || instr->can_trap() || (instr->as_NullCheck() != NULL)) { aoqi@0: mark_visited(instr); aoqi@0: instr->input_values_do(this); aoqi@0: instr->visit(&_visitor); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Propagate state to successors if necessary aoqi@0: for (i = 0; i < e->number_of_sux(); i++) { aoqi@0: BlockBegin* next = e->sux_at(i); aoqi@0: if (merge_state_for(next, state())) { aoqi@0: if (!work_list()->contains(next)) { aoqi@0: work_list()->push(next); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void NullCheckEliminator::iterate(BlockBegin* block) { aoqi@0: work_list()->push(block); aoqi@0: iterate_all(); aoqi@0: } aoqi@0: aoqi@0: void NullCheckEliminator::handle_AccessField(AccessField* x) { aoqi@0: if (x->is_static()) { aoqi@0: if (x->as_LoadField() != NULL) { aoqi@0: // If the field is a non-null static final object field (as is aoqi@0: // often the case for sun.misc.Unsafe), put this LoadField into aoqi@0: // the non-null map aoqi@0: ciField* field = x->field(); aoqi@0: if (field->is_constant()) { aoqi@0: ciConstant field_val = field->constant_value(); aoqi@0: BasicType field_type = field_val.basic_type(); aoqi@0: if (field_type == T_OBJECT || field_type == T_ARRAY) { aoqi@0: ciObject* obj_val = field_val.as_object(); aoqi@0: if (!obj_val->is_null_object()) { aoqi@0: if (PrintNullCheckElimination) { aoqi@0: tty->print_cr("AccessField %d proven non-null by static final non-null oop check", aoqi@0: x->id()); aoqi@0: } aoqi@0: set_put(x); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: // Be conservative aoqi@0: clear_last_explicit_null_check(); aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: Value obj = x->obj(); aoqi@0: if (set_contains(obj)) { aoqi@0: // Value is non-null => update AccessField aoqi@0: if (last_explicit_null_check_obj() == obj && !x->needs_patching()) { aoqi@0: x->set_explicit_null_check(consume_last_explicit_null_check()); aoqi@0: x->set_needs_null_check(true); aoqi@0: if (PrintNullCheckElimination) { aoqi@0: tty->print_cr("Folded NullCheck %d into AccessField %d's null check for value %d", aoqi@0: x->explicit_null_check()->id(), x->id(), obj->id()); aoqi@0: } aoqi@0: } else { aoqi@0: x->set_explicit_null_check(NULL); aoqi@0: x->set_needs_null_check(false); aoqi@0: if (PrintNullCheckElimination) { aoqi@0: tty->print_cr("Eliminated AccessField %d's null check for value %d", x->id(), obj->id()); aoqi@0: } aoqi@0: } aoqi@0: } else { aoqi@0: set_put(obj); aoqi@0: if (PrintNullCheckElimination) { aoqi@0: tty->print_cr("AccessField %d of value %d proves value to be non-null", x->id(), obj->id()); aoqi@0: } aoqi@0: // Ensure previous passes do not cause wrong state aoqi@0: x->set_needs_null_check(true); aoqi@0: x->set_explicit_null_check(NULL); aoqi@0: } aoqi@0: clear_last_explicit_null_check(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void NullCheckEliminator::handle_ArrayLength(ArrayLength* x) { aoqi@0: Value array = x->array(); aoqi@0: if (set_contains(array)) { aoqi@0: // Value is non-null => update AccessArray aoqi@0: if (last_explicit_null_check_obj() == array) { aoqi@0: x->set_explicit_null_check(consume_last_explicit_null_check()); aoqi@0: x->set_needs_null_check(true); aoqi@0: if (PrintNullCheckElimination) { aoqi@0: tty->print_cr("Folded NullCheck %d into ArrayLength %d's null check for value %d", aoqi@0: x->explicit_null_check()->id(), x->id(), array->id()); aoqi@0: } aoqi@0: } else { aoqi@0: x->set_explicit_null_check(NULL); aoqi@0: x->set_needs_null_check(false); aoqi@0: if (PrintNullCheckElimination) { aoqi@0: tty->print_cr("Eliminated ArrayLength %d's null check for value %d", x->id(), array->id()); aoqi@0: } aoqi@0: } aoqi@0: } else { aoqi@0: set_put(array); aoqi@0: if (PrintNullCheckElimination) { aoqi@0: tty->print_cr("ArrayLength %d of value %d proves value to be non-null", x->id(), array->id()); aoqi@0: } aoqi@0: // Ensure previous passes do not cause wrong state aoqi@0: x->set_needs_null_check(true); aoqi@0: x->set_explicit_null_check(NULL); aoqi@0: } aoqi@0: clear_last_explicit_null_check(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void NullCheckEliminator::handle_LoadIndexed(LoadIndexed* x) { aoqi@0: Value array = x->array(); aoqi@0: if (set_contains(array)) { aoqi@0: // Value is non-null => update AccessArray aoqi@0: if (last_explicit_null_check_obj() == array) { aoqi@0: x->set_explicit_null_check(consume_last_explicit_null_check()); aoqi@0: x->set_needs_null_check(true); aoqi@0: if (PrintNullCheckElimination) { aoqi@0: tty->print_cr("Folded NullCheck %d into LoadIndexed %d's null check for value %d", aoqi@0: x->explicit_null_check()->id(), x->id(), array->id()); aoqi@0: } aoqi@0: } else { aoqi@0: x->set_explicit_null_check(NULL); aoqi@0: x->set_needs_null_check(false); aoqi@0: if (PrintNullCheckElimination) { aoqi@0: tty->print_cr("Eliminated LoadIndexed %d's null check for value %d", x->id(), array->id()); aoqi@0: } aoqi@0: } aoqi@0: } else { aoqi@0: set_put(array); aoqi@0: if (PrintNullCheckElimination) { aoqi@0: tty->print_cr("LoadIndexed %d of value %d proves value to be non-null", x->id(), array->id()); aoqi@0: } aoqi@0: // Ensure previous passes do not cause wrong state aoqi@0: x->set_needs_null_check(true); aoqi@0: x->set_explicit_null_check(NULL); aoqi@0: } aoqi@0: clear_last_explicit_null_check(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void NullCheckEliminator::handle_StoreIndexed(StoreIndexed* x) { aoqi@0: Value array = x->array(); aoqi@0: if (set_contains(array)) { aoqi@0: // Value is non-null => update AccessArray aoqi@0: if (PrintNullCheckElimination) { aoqi@0: tty->print_cr("Eliminated StoreIndexed %d's null check for value %d", x->id(), array->id()); aoqi@0: } aoqi@0: x->set_needs_null_check(false); aoqi@0: } else { aoqi@0: set_put(array); aoqi@0: if (PrintNullCheckElimination) { aoqi@0: tty->print_cr("StoreIndexed %d of value %d proves value to be non-null", x->id(), array->id()); aoqi@0: } aoqi@0: // Ensure previous passes do not cause wrong state aoqi@0: x->set_needs_null_check(true); aoqi@0: } aoqi@0: clear_last_explicit_null_check(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void NullCheckEliminator::handle_NullCheck(NullCheck* x) { aoqi@0: Value obj = x->obj(); aoqi@0: if (set_contains(obj)) { aoqi@0: // Already proven to be non-null => this NullCheck is useless aoqi@0: if (PrintNullCheckElimination) { aoqi@0: tty->print_cr("Eliminated NullCheck %d for value %d", x->id(), obj->id()); aoqi@0: } aoqi@0: // Don't unpin since that may shrink obj's live range and make it unavailable for debug info. aoqi@0: // The code generator won't emit LIR for a NullCheck that cannot trap. aoqi@0: x->set_can_trap(false); aoqi@0: } else { aoqi@0: // May be null => add to map and set last explicit NullCheck aoqi@0: x->set_can_trap(true); aoqi@0: // make sure it's pinned if it can trap aoqi@0: x->pin(Instruction::PinExplicitNullCheck); aoqi@0: set_put(obj); aoqi@0: set_last_explicit_null_check(x); aoqi@0: if (PrintNullCheckElimination) { aoqi@0: tty->print_cr("NullCheck %d of value %d proves value to be non-null", x->id(), obj->id()); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void NullCheckEliminator::handle_Invoke(Invoke* x) { aoqi@0: if (!x->has_receiver()) { aoqi@0: // Be conservative aoqi@0: clear_last_explicit_null_check(); aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: Value recv = x->receiver(); aoqi@0: if (!set_contains(recv)) { aoqi@0: set_put(recv); aoqi@0: if (PrintNullCheckElimination) { aoqi@0: tty->print_cr("Invoke %d of value %d proves value to be non-null", x->id(), recv->id()); aoqi@0: } aoqi@0: } aoqi@0: clear_last_explicit_null_check(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void NullCheckEliminator::handle_NewInstance(NewInstance* x) { aoqi@0: set_put(x); aoqi@0: if (PrintNullCheckElimination) { aoqi@0: tty->print_cr("NewInstance %d is non-null", x->id()); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void NullCheckEliminator::handle_NewArray(NewArray* x) { aoqi@0: set_put(x); aoqi@0: if (PrintNullCheckElimination) { aoqi@0: tty->print_cr("NewArray %d is non-null", x->id()); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void NullCheckEliminator::handle_ExceptionObject(ExceptionObject* x) { aoqi@0: set_put(x); aoqi@0: if (PrintNullCheckElimination) { aoqi@0: tty->print_cr("ExceptionObject %d is non-null", x->id()); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void NullCheckEliminator::handle_AccessMonitor(AccessMonitor* x) { aoqi@0: Value obj = x->obj(); aoqi@0: if (set_contains(obj)) { aoqi@0: // Value is non-null => update AccessMonitor aoqi@0: if (PrintNullCheckElimination) { aoqi@0: tty->print_cr("Eliminated AccessMonitor %d's null check for value %d", x->id(), obj->id()); aoqi@0: } aoqi@0: x->set_needs_null_check(false); aoqi@0: } else { aoqi@0: set_put(obj); aoqi@0: if (PrintNullCheckElimination) { aoqi@0: tty->print_cr("AccessMonitor %d of value %d proves value to be non-null", x->id(), obj->id()); aoqi@0: } aoqi@0: // Ensure previous passes do not cause wrong state aoqi@0: x->set_needs_null_check(true); aoqi@0: } aoqi@0: clear_last_explicit_null_check(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void NullCheckEliminator::handle_Intrinsic(Intrinsic* x) { aoqi@0: if (!x->has_receiver()) { aoqi@0: if (x->id() == vmIntrinsics::_arraycopy) { aoqi@0: for (int i = 0; i < x->number_of_arguments(); i++) { aoqi@0: x->set_arg_needs_null_check(i, !set_contains(x->argument_at(i))); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Be conservative aoqi@0: clear_last_explicit_null_check(); aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: Value recv = x->receiver(); aoqi@0: if (set_contains(recv)) { aoqi@0: // Value is non-null => update Intrinsic aoqi@0: if (PrintNullCheckElimination) { aoqi@0: tty->print_cr("Eliminated Intrinsic %d's null check for value %d", x->id(), recv->id()); aoqi@0: } aoqi@0: x->set_needs_null_check(false); aoqi@0: } else { aoqi@0: set_put(recv); aoqi@0: if (PrintNullCheckElimination) { aoqi@0: tty->print_cr("Intrinsic %d of value %d proves value to be non-null", x->id(), recv->id()); aoqi@0: } aoqi@0: // Ensure previous passes do not cause wrong state aoqi@0: x->set_needs_null_check(true); aoqi@0: } aoqi@0: clear_last_explicit_null_check(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void NullCheckEliminator::handle_Phi(Phi* x) { aoqi@0: int i; aoqi@0: bool all_non_null = true; aoqi@0: if (x->is_illegal()) { aoqi@0: all_non_null = false; aoqi@0: } else { aoqi@0: for (i = 0; i < x->operand_count(); i++) { aoqi@0: Value input = x->operand_at(i); aoqi@0: if (!set_contains(input)) { aoqi@0: all_non_null = false; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: if (all_non_null) { aoqi@0: // Value is non-null => update Phi aoqi@0: if (PrintNullCheckElimination) { aoqi@0: tty->print_cr("Eliminated Phi %d's null check for phifun because all inputs are non-null", x->id()); aoqi@0: } aoqi@0: x->set_needs_null_check(false); aoqi@0: } else if (set_contains(x)) { aoqi@0: set_remove(x); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void NullCheckEliminator::handle_ProfileCall(ProfileCall* x) { aoqi@0: for (int i = 0; i < x->nb_profiled_args(); i++) { aoqi@0: x->set_arg_needs_null_check(i, !set_contains(x->profiled_arg_at(i))); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void NullCheckEliminator::handle_ProfileReturnType(ProfileReturnType* x) { aoqi@0: x->set_needs_null_check(!set_contains(x->ret())); aoqi@0: } aoqi@0: aoqi@0: void Optimizer::eliminate_null_checks() { aoqi@0: ResourceMark rm; aoqi@0: aoqi@0: NullCheckEliminator nce(this); aoqi@0: aoqi@0: if (PrintNullCheckElimination) { aoqi@0: tty->print_cr("Starting null check elimination for method %s::%s%s", aoqi@0: ir()->method()->holder()->name()->as_utf8(), aoqi@0: ir()->method()->name()->as_utf8(), aoqi@0: ir()->method()->signature()->as_symbol()->as_utf8()); aoqi@0: } aoqi@0: aoqi@0: // Apply to graph aoqi@0: nce.iterate(ir()->start()); aoqi@0: aoqi@0: // walk over the graph looking for exception aoqi@0: // handlers and iterate over them as well aoqi@0: int nblocks = BlockBegin::number_of_blocks(); aoqi@0: BlockList blocks(nblocks); aoqi@0: boolArray visited_block(nblocks, false); aoqi@0: aoqi@0: blocks.push(ir()->start()); aoqi@0: visited_block[ir()->start()->block_id()] = true; aoqi@0: for (int i = 0; i < blocks.length(); i++) { aoqi@0: BlockBegin* b = blocks[i]; aoqi@0: // exception handlers need to be treated as additional roots aoqi@0: for (int e = b->number_of_exception_handlers(); e-- > 0; ) { aoqi@0: BlockBegin* excp = b->exception_handler_at(e); aoqi@0: int id = excp->block_id(); aoqi@0: if (!visited_block[id]) { aoqi@0: blocks.push(excp); aoqi@0: visited_block[id] = true; aoqi@0: nce.iterate(excp); aoqi@0: } aoqi@0: } aoqi@0: // traverse successors aoqi@0: BlockEnd *end = b->end(); aoqi@0: for (int s = end->number_of_sux(); s-- > 0; ) { aoqi@0: BlockBegin* next = end->sux_at(s); aoqi@0: int id = next->block_id(); aoqi@0: if (!visited_block[id]) { aoqi@0: blocks.push(next); aoqi@0: visited_block[id] = true; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: if (PrintNullCheckElimination) { aoqi@0: tty->print_cr("Done with null check elimination for method %s::%s%s", aoqi@0: ir()->method()->holder()->name()->as_utf8(), aoqi@0: ir()->method()->name()->as_utf8(), aoqi@0: ir()->method()->signature()->as_symbol()->as_utf8()); aoqi@0: } aoqi@0: }