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