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