duke@435: /* xdono@1279: * Copyright 1997-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: // Portions of code courtesy of Clifford Click duke@435: duke@435: // Optimization - Graph Style duke@435: duke@435: #include "incls/_precompiled.incl" duke@435: #include "incls/_callnode.cpp.incl" duke@435: duke@435: //============================================================================= duke@435: uint StartNode::size_of() const { return sizeof(*this); } duke@435: uint StartNode::cmp( const Node &n ) const duke@435: { return _domain == ((StartNode&)n)._domain; } duke@435: const Type *StartNode::bottom_type() const { return _domain; } duke@435: const Type *StartNode::Value(PhaseTransform *phase) const { return _domain; } duke@435: #ifndef PRODUCT duke@435: void StartNode::dump_spec(outputStream *st) const { st->print(" #"); _domain->dump_on(st);} duke@435: #endif duke@435: duke@435: //------------------------------Ideal------------------------------------------ duke@435: Node *StartNode::Ideal(PhaseGVN *phase, bool can_reshape){ duke@435: return remove_dead_region(phase, can_reshape) ? this : NULL; duke@435: } duke@435: duke@435: //------------------------------calling_convention----------------------------- duke@435: void StartNode::calling_convention( BasicType* sig_bt, VMRegPair *parm_regs, uint argcnt ) const { duke@435: Matcher::calling_convention( sig_bt, parm_regs, argcnt, false ); duke@435: } duke@435: duke@435: //------------------------------Registers-------------------------------------- duke@435: const RegMask &StartNode::in_RegMask(uint) const { duke@435: return RegMask::Empty; duke@435: } duke@435: duke@435: //------------------------------match------------------------------------------ duke@435: // Construct projections for incoming parameters, and their RegMask info duke@435: Node *StartNode::match( const ProjNode *proj, const Matcher *match ) { duke@435: switch (proj->_con) { duke@435: case TypeFunc::Control: duke@435: case TypeFunc::I_O: duke@435: case TypeFunc::Memory: duke@435: return new (match->C, 1) MachProjNode(this,proj->_con,RegMask::Empty,MachProjNode::unmatched_proj); duke@435: case TypeFunc::FramePtr: duke@435: return new (match->C, 1) MachProjNode(this,proj->_con,Matcher::c_frame_ptr_mask, Op_RegP); duke@435: case TypeFunc::ReturnAdr: duke@435: return new (match->C, 1) MachProjNode(this,proj->_con,match->_return_addr_mask,Op_RegP); duke@435: case TypeFunc::Parms: duke@435: default: { duke@435: uint parm_num = proj->_con - TypeFunc::Parms; duke@435: const Type *t = _domain->field_at(proj->_con); duke@435: if (t->base() == Type::Half) // 2nd half of Longs and Doubles duke@435: return new (match->C, 1) ConNode(Type::TOP); duke@435: uint ideal_reg = Matcher::base2reg[t->base()]; duke@435: RegMask &rm = match->_calling_convention_mask[parm_num]; duke@435: return new (match->C, 1) MachProjNode(this,proj->_con,rm,ideal_reg); duke@435: } duke@435: } duke@435: return NULL; duke@435: } duke@435: duke@435: //------------------------------StartOSRNode---------------------------------- duke@435: // The method start node for an on stack replacement adapter duke@435: duke@435: //------------------------------osr_domain----------------------------- duke@435: const TypeTuple *StartOSRNode::osr_domain() { duke@435: const Type **fields = TypeTuple::fields(2); duke@435: fields[TypeFunc::Parms+0] = TypeRawPtr::BOTTOM; // address of osr buffer duke@435: duke@435: return TypeTuple::make(TypeFunc::Parms+1, fields); duke@435: } duke@435: duke@435: //============================================================================= duke@435: const char * const ParmNode::names[TypeFunc::Parms+1] = { duke@435: "Control", "I_O", "Memory", "FramePtr", "ReturnAdr", "Parms" duke@435: }; duke@435: duke@435: #ifndef PRODUCT duke@435: void ParmNode::dump_spec(outputStream *st) const { duke@435: if( _con < TypeFunc::Parms ) { duke@435: st->print(names[_con]); duke@435: } else { duke@435: st->print("Parm%d: ",_con-TypeFunc::Parms); duke@435: // Verbose and WizardMode dump bottom_type for all nodes duke@435: if( !Verbose && !WizardMode ) bottom_type()->dump_on(st); duke@435: } duke@435: } duke@435: #endif duke@435: duke@435: uint ParmNode::ideal_reg() const { duke@435: switch( _con ) { duke@435: case TypeFunc::Control : // fall through duke@435: case TypeFunc::I_O : // fall through duke@435: case TypeFunc::Memory : return 0; duke@435: case TypeFunc::FramePtr : // fall through duke@435: case TypeFunc::ReturnAdr: return Op_RegP; duke@435: default : assert( _con > TypeFunc::Parms, "" ); duke@435: // fall through duke@435: case TypeFunc::Parms : { duke@435: // Type of argument being passed duke@435: const Type *t = in(0)->as_Start()->_domain->field_at(_con); duke@435: return Matcher::base2reg[t->base()]; duke@435: } duke@435: } duke@435: ShouldNotReachHere(); duke@435: return 0; duke@435: } duke@435: duke@435: //============================================================================= duke@435: ReturnNode::ReturnNode(uint edges, Node *cntrl, Node *i_o, Node *memory, Node *frameptr, Node *retadr ) : Node(edges) { duke@435: init_req(TypeFunc::Control,cntrl); duke@435: init_req(TypeFunc::I_O,i_o); duke@435: init_req(TypeFunc::Memory,memory); duke@435: init_req(TypeFunc::FramePtr,frameptr); duke@435: init_req(TypeFunc::ReturnAdr,retadr); duke@435: } duke@435: duke@435: Node *ReturnNode::Ideal(PhaseGVN *phase, bool can_reshape){ duke@435: return remove_dead_region(phase, can_reshape) ? this : NULL; duke@435: } duke@435: duke@435: const Type *ReturnNode::Value( PhaseTransform *phase ) const { duke@435: return ( phase->type(in(TypeFunc::Control)) == Type::TOP) duke@435: ? Type::TOP duke@435: : Type::BOTTOM; duke@435: } duke@435: duke@435: // Do we Match on this edge index or not? No edges on return nodes duke@435: uint ReturnNode::match_edge(uint idx) const { duke@435: return 0; duke@435: } duke@435: duke@435: duke@435: #ifndef PRODUCT duke@435: void ReturnNode::dump_req() const { duke@435: // Dump the required inputs, enclosed in '(' and ')' duke@435: uint i; // Exit value of loop duke@435: for( i=0; iprint("returns"); duke@435: if( in(i) ) tty->print("%c%d ", Compile::current()->node_arena()->contains(in(i)) ? ' ' : 'o', in(i)->_idx); duke@435: else tty->print("_ "); duke@435: } duke@435: } duke@435: #endif duke@435: duke@435: //============================================================================= duke@435: RethrowNode::RethrowNode( duke@435: Node* cntrl, duke@435: Node* i_o, duke@435: Node* memory, duke@435: Node* frameptr, duke@435: Node* ret_adr, duke@435: Node* exception duke@435: ) : Node(TypeFunc::Parms + 1) { duke@435: init_req(TypeFunc::Control , cntrl ); duke@435: init_req(TypeFunc::I_O , i_o ); duke@435: init_req(TypeFunc::Memory , memory ); duke@435: init_req(TypeFunc::FramePtr , frameptr ); duke@435: init_req(TypeFunc::ReturnAdr, ret_adr); duke@435: init_req(TypeFunc::Parms , exception); duke@435: } duke@435: duke@435: Node *RethrowNode::Ideal(PhaseGVN *phase, bool can_reshape){ duke@435: return remove_dead_region(phase, can_reshape) ? this : NULL; duke@435: } duke@435: duke@435: const Type *RethrowNode::Value( PhaseTransform *phase ) const { duke@435: return (phase->type(in(TypeFunc::Control)) == Type::TOP) duke@435: ? Type::TOP duke@435: : Type::BOTTOM; duke@435: } duke@435: duke@435: uint RethrowNode::match_edge(uint idx) const { duke@435: return 0; duke@435: } duke@435: duke@435: #ifndef PRODUCT duke@435: void RethrowNode::dump_req() const { duke@435: // Dump the required inputs, enclosed in '(' and ')' duke@435: uint i; // Exit value of loop duke@435: for( i=0; iprint("exception"); duke@435: if( in(i) ) tty->print("%c%d ", Compile::current()->node_arena()->contains(in(i)) ? ' ' : 'o', in(i)->_idx); duke@435: else tty->print("_ "); duke@435: } duke@435: } duke@435: #endif duke@435: duke@435: //============================================================================= duke@435: // Do we Match on this edge index or not? Match only target address & method duke@435: uint TailCallNode::match_edge(uint idx) const { duke@435: return TypeFunc::Parms <= idx && idx <= TypeFunc::Parms+1; duke@435: } duke@435: duke@435: //============================================================================= duke@435: // Do we Match on this edge index or not? Match only target address & oop duke@435: uint TailJumpNode::match_edge(uint idx) const { duke@435: return TypeFunc::Parms <= idx && idx <= TypeFunc::Parms+1; duke@435: } duke@435: duke@435: //============================================================================= duke@435: JVMState::JVMState(ciMethod* method, JVMState* caller) { duke@435: assert(method != NULL, "must be valid call site"); duke@435: _method = method; duke@435: debug_only(_bci = -99); // random garbage value duke@435: debug_only(_map = (SafePointNode*)-1); duke@435: _caller = caller; duke@435: _depth = 1 + (caller == NULL ? 0 : caller->depth()); duke@435: _locoff = TypeFunc::Parms; duke@435: _stkoff = _locoff + _method->max_locals(); duke@435: _monoff = _stkoff + _method->max_stack(); kvn@498: _scloff = _monoff; duke@435: _endoff = _monoff; duke@435: _sp = 0; duke@435: } duke@435: JVMState::JVMState(int stack_size) { duke@435: _method = NULL; duke@435: _bci = InvocationEntryBci; duke@435: debug_only(_map = (SafePointNode*)-1); duke@435: _caller = NULL; duke@435: _depth = 1; duke@435: _locoff = TypeFunc::Parms; duke@435: _stkoff = _locoff; duke@435: _monoff = _stkoff + stack_size; kvn@498: _scloff = _monoff; duke@435: _endoff = _monoff; duke@435: _sp = 0; duke@435: } duke@435: duke@435: //--------------------------------of_depth------------------------------------- duke@435: JVMState* JVMState::of_depth(int d) const { duke@435: const JVMState* jvmp = this; duke@435: assert(0 < d && (uint)d <= depth(), "oob"); duke@435: for (int skip = depth() - d; skip > 0; skip--) { duke@435: jvmp = jvmp->caller(); duke@435: } duke@435: assert(jvmp->depth() == (uint)d, "found the right one"); duke@435: return (JVMState*)jvmp; duke@435: } duke@435: duke@435: //-----------------------------same_calls_as----------------------------------- duke@435: bool JVMState::same_calls_as(const JVMState* that) const { duke@435: if (this == that) return true; duke@435: if (this->depth() != that->depth()) return false; duke@435: const JVMState* p = this; duke@435: const JVMState* q = that; duke@435: for (;;) { duke@435: if (p->_method != q->_method) return false; duke@435: if (p->_method == NULL) return true; // bci is irrelevant duke@435: if (p->_bci != q->_bci) return false; duke@435: p = p->caller(); duke@435: q = q->caller(); duke@435: if (p == q) return true; duke@435: assert(p != NULL && q != NULL, "depth check ensures we don't run off end"); duke@435: } duke@435: } duke@435: duke@435: //------------------------------debug_start------------------------------------ duke@435: uint JVMState::debug_start() const { duke@435: debug_only(JVMState* jvmroot = of_depth(1)); duke@435: assert(jvmroot->locoff() <= this->locoff(), "youngest JVMState must be last"); duke@435: return of_depth(1)->locoff(); duke@435: } duke@435: duke@435: //-------------------------------debug_end------------------------------------- duke@435: uint JVMState::debug_end() const { duke@435: debug_only(JVMState* jvmroot = of_depth(1)); duke@435: assert(jvmroot->endoff() <= this->endoff(), "youngest JVMState must be last"); duke@435: return endoff(); duke@435: } duke@435: duke@435: //------------------------------debug_depth------------------------------------ duke@435: uint JVMState::debug_depth() const { duke@435: uint total = 0; duke@435: for (const JVMState* jvmp = this; jvmp != NULL; jvmp = jvmp->caller()) { duke@435: total += jvmp->debug_size(); duke@435: } duke@435: return total; duke@435: } duke@435: kvn@498: #ifndef PRODUCT kvn@498: duke@435: //------------------------------format_helper---------------------------------- duke@435: // Given an allocation (a Chaitin object) and a Node decide if the Node carries duke@435: // any defined value or not. If it does, print out the register or constant. kvn@498: static void format_helper( PhaseRegAlloc *regalloc, outputStream* st, Node *n, const char *msg, uint i, GrowableArray *scobjs ) { duke@435: if (n == NULL) { st->print(" NULL"); return; } kvn@498: if (n->is_SafePointScalarObject()) { kvn@498: // Scalar replacement. kvn@498: SafePointScalarObjectNode* spobj = n->as_SafePointScalarObject(); kvn@498: scobjs->append_if_missing(spobj); kvn@498: int sco_n = scobjs->find(spobj); kvn@498: assert(sco_n >= 0, ""); kvn@498: st->print(" %s%d]=#ScObj" INT32_FORMAT, msg, i, sco_n); kvn@498: return; kvn@498: } duke@435: if( OptoReg::is_valid(regalloc->get_reg_first(n))) { // Check for undefined duke@435: char buf[50]; duke@435: regalloc->dump_register(n,buf); duke@435: st->print(" %s%d]=%s",msg,i,buf); duke@435: } else { // No register, but might be constant duke@435: const Type *t = n->bottom_type(); duke@435: switch (t->base()) { duke@435: case Type::Int: duke@435: st->print(" %s%d]=#"INT32_FORMAT,msg,i,t->is_int()->get_con()); duke@435: break; duke@435: case Type::AnyPtr: duke@435: assert( t == TypePtr::NULL_PTR, "" ); duke@435: st->print(" %s%d]=#NULL",msg,i); duke@435: break; duke@435: case Type::AryPtr: duke@435: case Type::KlassPtr: duke@435: case Type::InstPtr: duke@435: st->print(" %s%d]=#Ptr" INTPTR_FORMAT,msg,i,t->isa_oopptr()->const_oop()); duke@435: break; kvn@766: case Type::NarrowOop: kvn@766: st->print(" %s%d]=#Ptr" INTPTR_FORMAT,msg,i,t->make_ptr()->isa_oopptr()->const_oop()); kvn@766: break; duke@435: case Type::RawPtr: duke@435: st->print(" %s%d]=#Raw" INTPTR_FORMAT,msg,i,t->is_rawptr()); duke@435: break; duke@435: case Type::DoubleCon: duke@435: st->print(" %s%d]=#%fD",msg,i,t->is_double_constant()->_d); duke@435: break; duke@435: case Type::FloatCon: duke@435: st->print(" %s%d]=#%fF",msg,i,t->is_float_constant()->_f); duke@435: break; duke@435: case Type::Long: duke@435: st->print(" %s%d]=#"INT64_FORMAT,msg,i,t->is_long()->get_con()); duke@435: break; duke@435: case Type::Half: duke@435: case Type::Top: duke@435: st->print(" %s%d]=_",msg,i); duke@435: break; duke@435: default: ShouldNotReachHere(); duke@435: } duke@435: } duke@435: } duke@435: duke@435: //------------------------------format----------------------------------------- duke@435: void JVMState::format(PhaseRegAlloc *regalloc, const Node *n, outputStream* st) const { duke@435: st->print(" #"); duke@435: if( _method ) { duke@435: _method->print_short_name(st); duke@435: st->print(" @ bci:%d ",_bci); duke@435: } else { duke@435: st->print_cr(" runtime stub "); duke@435: return; duke@435: } duke@435: if (n->is_MachSafePoint()) { kvn@498: GrowableArray scobjs; duke@435: MachSafePointNode *mcall = n->as_MachSafePoint(); duke@435: uint i; duke@435: // Print locals duke@435: for( i = 0; i < (uint)loc_size(); i++ ) kvn@498: format_helper( regalloc, st, mcall->local(this, i), "L[", i, &scobjs ); duke@435: // Print stack duke@435: for (i = 0; i < (uint)stk_size(); i++) { duke@435: if ((uint)(_stkoff + i) >= mcall->len()) duke@435: st->print(" oob "); duke@435: else kvn@498: format_helper( regalloc, st, mcall->stack(this, i), "STK[", i, &scobjs ); duke@435: } duke@435: for (i = 0; (int)i < nof_monitors(); i++) { duke@435: Node *box = mcall->monitor_box(this, i); duke@435: Node *obj = mcall->monitor_obj(this, i); duke@435: if ( OptoReg::is_valid(regalloc->get_reg_first(box)) ) { duke@435: while( !box->is_BoxLock() ) box = box->in(1); kvn@498: format_helper( regalloc, st, box, "MON-BOX[", i, &scobjs ); duke@435: } else { duke@435: OptoReg::Name box_reg = BoxLockNode::stack_slot(box); duke@435: st->print(" MON-BOX%d=%s+%d", duke@435: i, duke@435: OptoReg::regname(OptoReg::c_frame_pointer), duke@435: regalloc->reg2offset(box_reg)); duke@435: } kvn@895: const char* obj_msg = "MON-OBJ["; kvn@895: if (EliminateLocks) { kvn@895: while( !box->is_BoxLock() ) box = box->in(1); kvn@895: if (box->as_BoxLock()->is_eliminated()) kvn@895: obj_msg = "MON-OBJ(LOCK ELIMINATED)["; kvn@895: } kvn@895: format_helper( regalloc, st, obj, obj_msg, i, &scobjs ); kvn@498: } kvn@498: kvn@498: for (i = 0; i < (uint)scobjs.length(); i++) { kvn@498: // Scalar replaced objects. kvn@498: st->print_cr(""); kvn@498: st->print(" # ScObj" INT32_FORMAT " ", i); kvn@498: SafePointScalarObjectNode* spobj = scobjs.at(i); kvn@498: ciKlass* cik = spobj->bottom_type()->is_oopptr()->klass(); kvn@498: assert(cik->is_instance_klass() || kvn@498: cik->is_array_klass(), "Not supported allocation."); kvn@498: ciInstanceKlass *iklass = NULL; kvn@498: if (cik->is_instance_klass()) { kvn@498: cik->print_name_on(st); kvn@498: iklass = cik->as_instance_klass(); kvn@498: } else if (cik->is_type_array_klass()) { kvn@498: cik->as_array_klass()->base_element_type()->print_name_on(st); kvn@498: st->print("[%d]=", spobj->n_fields()); kvn@498: } else if (cik->is_obj_array_klass()) { kvn@498: ciType* cie = cik->as_array_klass()->base_element_type(); kvn@498: int ndim = 1; kvn@498: while (cie->is_obj_array_klass()) { kvn@498: ndim += 1; kvn@498: cie = cie->as_array_klass()->base_element_type(); kvn@498: } kvn@498: cie->print_name_on(st); kvn@498: while (ndim-- > 0) { kvn@498: st->print("[]"); kvn@498: } kvn@498: st->print("[%d]=", spobj->n_fields()); kvn@498: } kvn@498: st->print("{"); kvn@498: uint nf = spobj->n_fields(); kvn@498: if (nf > 0) { kvn@498: uint first_ind = spobj->first_index(); kvn@498: Node* fld_node = mcall->in(first_ind); kvn@498: ciField* cifield; kvn@498: if (iklass != NULL) { kvn@498: st->print(" ["); kvn@498: cifield = iklass->nonstatic_field_at(0); kvn@498: cifield->print_name_on(st); kvn@498: format_helper( regalloc, st, fld_node, ":", 0, &scobjs ); kvn@498: } else { kvn@498: format_helper( regalloc, st, fld_node, "[", 0, &scobjs ); kvn@498: } kvn@498: for (uint j = 1; j < nf; j++) { kvn@498: fld_node = mcall->in(first_ind+j); kvn@498: if (iklass != NULL) { kvn@498: st->print(", ["); kvn@498: cifield = iklass->nonstatic_field_at(j); kvn@498: cifield->print_name_on(st); kvn@498: format_helper( regalloc, st, fld_node, ":", j, &scobjs ); kvn@498: } else { kvn@498: format_helper( regalloc, st, fld_node, ", [", j, &scobjs ); kvn@498: } kvn@498: } kvn@498: } kvn@498: st->print(" }"); duke@435: } duke@435: } duke@435: st->print_cr(""); duke@435: if (caller() != NULL) caller()->format(regalloc, n, st); duke@435: } duke@435: kvn@498: duke@435: void JVMState::dump_spec(outputStream *st) const { duke@435: if (_method != NULL) { duke@435: bool printed = false; duke@435: if (!Verbose) { duke@435: // The JVMS dumps make really, really long lines. duke@435: // Take out the most boring parts, which are the package prefixes. duke@435: char buf[500]; duke@435: stringStream namest(buf, sizeof(buf)); duke@435: _method->print_short_name(&namest); duke@435: if (namest.count() < sizeof(buf)) { duke@435: const char* name = namest.base(); duke@435: if (name[0] == ' ') ++name; duke@435: const char* endcn = strchr(name, ':'); // end of class name duke@435: if (endcn == NULL) endcn = strchr(name, '('); duke@435: if (endcn == NULL) endcn = name + strlen(name); duke@435: while (endcn > name && endcn[-1] != '.' && endcn[-1] != '/') duke@435: --endcn; duke@435: st->print(" %s", endcn); duke@435: printed = true; duke@435: } duke@435: } duke@435: if (!printed) duke@435: _method->print_short_name(st); duke@435: st->print(" @ bci:%d",_bci); duke@435: } else { duke@435: st->print(" runtime stub"); duke@435: } duke@435: if (caller() != NULL) caller()->dump_spec(st); duke@435: } duke@435: kvn@498: duke@435: void JVMState::dump_on(outputStream* st) const { duke@435: if (_map && !((uintptr_t)_map & 1)) { duke@435: if (_map->len() > _map->req()) { // _map->has_exceptions() duke@435: Node* ex = _map->in(_map->req()); // _map->next_exception() duke@435: // skip the first one; it's already being printed duke@435: while (ex != NULL && ex->len() > ex->req()) { duke@435: ex = ex->in(ex->req()); // ex->next_exception() duke@435: ex->dump(1); duke@435: } duke@435: } duke@435: _map->dump(2); duke@435: } kvn@498: st->print("JVMS depth=%d loc=%d stk=%d mon=%d scalar=%d end=%d mondepth=%d sp=%d bci=%d method=", kvn@498: depth(), locoff(), stkoff(), monoff(), scloff(), endoff(), monitor_depth(), sp(), bci()); duke@435: if (_method == NULL) { duke@435: st->print_cr("(none)"); duke@435: } else { duke@435: _method->print_name(st); duke@435: st->cr(); duke@435: if (bci() >= 0 && bci() < _method->code_size()) { duke@435: st->print(" bc: "); duke@435: _method->print_codes_on(bci(), bci()+1, st); duke@435: } duke@435: } duke@435: if (caller() != NULL) { duke@435: caller()->dump_on(st); duke@435: } duke@435: } duke@435: duke@435: // Extra way to dump a jvms from the debugger, duke@435: // to avoid a bug with C++ member function calls. duke@435: void dump_jvms(JVMState* jvms) { duke@435: jvms->dump(); duke@435: } duke@435: #endif duke@435: duke@435: //--------------------------clone_shallow-------------------------------------- duke@435: JVMState* JVMState::clone_shallow(Compile* C) const { duke@435: JVMState* n = has_method() ? new (C) JVMState(_method, _caller) : new (C) JVMState(0); duke@435: n->set_bci(_bci); duke@435: n->set_locoff(_locoff); duke@435: n->set_stkoff(_stkoff); duke@435: n->set_monoff(_monoff); kvn@498: n->set_scloff(_scloff); duke@435: n->set_endoff(_endoff); duke@435: n->set_sp(_sp); duke@435: n->set_map(_map); duke@435: return n; duke@435: } duke@435: duke@435: //---------------------------clone_deep---------------------------------------- duke@435: JVMState* JVMState::clone_deep(Compile* C) const { duke@435: JVMState* n = clone_shallow(C); duke@435: for (JVMState* p = n; p->_caller != NULL; p = p->_caller) { duke@435: p->_caller = p->_caller->clone_shallow(C); duke@435: } duke@435: assert(n->depth() == depth(), "sanity"); duke@435: assert(n->debug_depth() == debug_depth(), "sanity"); duke@435: return n; duke@435: } duke@435: duke@435: //============================================================================= duke@435: uint CallNode::cmp( const Node &n ) const duke@435: { return _tf == ((CallNode&)n)._tf && _jvms == ((CallNode&)n)._jvms; } duke@435: #ifndef PRODUCT duke@435: void CallNode::dump_req() const { duke@435: // Dump the required inputs, enclosed in '(' and ')' duke@435: uint i; // Exit value of loop duke@435: for( i=0; iprint("("); duke@435: if( in(i) ) tty->print("%c%d ", Compile::current()->node_arena()->contains(in(i)) ? ' ' : 'o', in(i)->_idx); duke@435: else tty->print("_ "); duke@435: } duke@435: tty->print(")"); duke@435: } duke@435: duke@435: void CallNode::dump_spec(outputStream *st) const { duke@435: st->print(" "); duke@435: tf()->dump_on(st); duke@435: if (_cnt != COUNT_UNKNOWN) st->print(" C=%f",_cnt); duke@435: if (jvms() != NULL) jvms()->dump_spec(st); duke@435: } duke@435: #endif duke@435: duke@435: const Type *CallNode::bottom_type() const { return tf()->range(); } duke@435: const Type *CallNode::Value(PhaseTransform *phase) const { duke@435: if (phase->type(in(0)) == Type::TOP) return Type::TOP; duke@435: return tf()->range(); duke@435: } duke@435: duke@435: //------------------------------calling_convention----------------------------- duke@435: void CallNode::calling_convention( BasicType* sig_bt, VMRegPair *parm_regs, uint argcnt ) const { duke@435: // Use the standard compiler calling convention duke@435: Matcher::calling_convention( sig_bt, parm_regs, argcnt, true ); duke@435: } duke@435: duke@435: duke@435: //------------------------------match------------------------------------------ duke@435: // Construct projections for control, I/O, memory-fields, ..., and duke@435: // return result(s) along with their RegMask info duke@435: Node *CallNode::match( const ProjNode *proj, const Matcher *match ) { duke@435: switch (proj->_con) { duke@435: case TypeFunc::Control: duke@435: case TypeFunc::I_O: duke@435: case TypeFunc::Memory: duke@435: return new (match->C, 1) MachProjNode(this,proj->_con,RegMask::Empty,MachProjNode::unmatched_proj); duke@435: duke@435: case TypeFunc::Parms+1: // For LONG & DOUBLE returns duke@435: assert(tf()->_range->field_at(TypeFunc::Parms+1) == Type::HALF, ""); duke@435: // 2nd half of doubles and longs duke@435: return new (match->C, 1) MachProjNode(this,proj->_con, RegMask::Empty, (uint)OptoReg::Bad); duke@435: duke@435: case TypeFunc::Parms: { // Normal returns duke@435: uint ideal_reg = Matcher::base2reg[tf()->range()->field_at(TypeFunc::Parms)->base()]; duke@435: OptoRegPair regs = is_CallRuntime() duke@435: ? match->c_return_value(ideal_reg,true) // Calls into C runtime duke@435: : match-> return_value(ideal_reg,true); // Calls into compiled Java code duke@435: RegMask rm = RegMask(regs.first()); duke@435: if( OptoReg::is_valid(regs.second()) ) duke@435: rm.Insert( regs.second() ); duke@435: return new (match->C, 1) MachProjNode(this,proj->_con,rm,ideal_reg); duke@435: } duke@435: duke@435: case TypeFunc::ReturnAdr: duke@435: case TypeFunc::FramePtr: duke@435: default: duke@435: ShouldNotReachHere(); duke@435: } duke@435: return NULL; duke@435: } duke@435: duke@435: // Do we Match on this edge index or not? Match no edges duke@435: uint CallNode::match_edge(uint idx) const { duke@435: return 0; duke@435: } duke@435: kvn@500: // kvn@509: // Determine whether the call could modify the field of the specified kvn@509: // instance at the specified offset. kvn@500: // kvn@500: bool CallNode::may_modify(const TypePtr *addr_t, PhaseTransform *phase) { kvn@500: const TypeOopPtr *adrInst_t = addr_t->isa_oopptr(); kvn@500: kvn@682: // If not an OopPtr or not an instance type, assume the worst. kvn@682: // Note: currently this method is called only for instance types. kvn@682: if (adrInst_t == NULL || !adrInst_t->is_known_instance()) { kvn@500: return true; kvn@500: } kvn@682: // The instance_id is set only for scalar-replaceable allocations which kvn@682: // are not passed as arguments according to Escape Analysis. kvn@500: return false; kvn@500: } kvn@500: kvn@500: // Does this call have a direct reference to n other than debug information? kvn@500: bool CallNode::has_non_debug_use(Node *n) { kvn@500: const TypeTuple * d = tf()->domain(); kvn@500: for (uint i = TypeFunc::Parms; i < d->cnt(); i++) { kvn@500: Node *arg = in(i); kvn@500: if (arg == n) { kvn@500: return true; kvn@500: } kvn@500: } kvn@500: return false; kvn@500: } kvn@500: kvn@500: // Returns the unique CheckCastPP of a call kvn@500: // or 'this' if there are several CheckCastPP kvn@500: // or returns NULL if there is no one. kvn@500: Node *CallNode::result_cast() { kvn@500: Node *cast = NULL; kvn@500: kvn@500: Node *p = proj_out(TypeFunc::Parms); kvn@500: if (p == NULL) kvn@500: return NULL; kvn@500: kvn@500: for (DUIterator_Fast imax, i = p->fast_outs(imax); i < imax; i++) { kvn@500: Node *use = p->fast_out(i); kvn@500: if (use->is_CheckCastPP()) { kvn@500: if (cast != NULL) { kvn@500: return this; // more than 1 CheckCastPP kvn@500: } kvn@500: cast = use; kvn@500: } kvn@500: } kvn@500: return cast; kvn@500: } kvn@500: kvn@500: duke@435: //============================================================================= duke@435: uint CallJavaNode::size_of() const { return sizeof(*this); } duke@435: uint CallJavaNode::cmp( const Node &n ) const { duke@435: CallJavaNode &call = (CallJavaNode&)n; duke@435: return CallNode::cmp(call) && _method == call._method; duke@435: } duke@435: #ifndef PRODUCT duke@435: void CallJavaNode::dump_spec(outputStream *st) const { duke@435: if( _method ) _method->print_short_name(st); duke@435: CallNode::dump_spec(st); duke@435: } duke@435: #endif duke@435: duke@435: //============================================================================= duke@435: uint CallStaticJavaNode::size_of() const { return sizeof(*this); } duke@435: uint CallStaticJavaNode::cmp( const Node &n ) const { duke@435: CallStaticJavaNode &call = (CallStaticJavaNode&)n; duke@435: return CallJavaNode::cmp(call); duke@435: } duke@435: duke@435: //----------------------------uncommon_trap_request---------------------------- duke@435: // If this is an uncommon trap, return the request code, else zero. duke@435: int CallStaticJavaNode::uncommon_trap_request() const { duke@435: if (_name != NULL && !strcmp(_name, "uncommon_trap")) { duke@435: return extract_uncommon_trap_request(this); duke@435: } duke@435: return 0; duke@435: } duke@435: int CallStaticJavaNode::extract_uncommon_trap_request(const Node* call) { duke@435: #ifndef PRODUCT duke@435: if (!(call->req() > TypeFunc::Parms && duke@435: call->in(TypeFunc::Parms) != NULL && duke@435: call->in(TypeFunc::Parms)->is_Con())) { duke@435: assert(_in_dump_cnt != 0, "OK if dumping"); duke@435: tty->print("[bad uncommon trap]"); duke@435: return 0; duke@435: } duke@435: #endif duke@435: return call->in(TypeFunc::Parms)->bottom_type()->is_int()->get_con(); duke@435: } duke@435: duke@435: #ifndef PRODUCT duke@435: void CallStaticJavaNode::dump_spec(outputStream *st) const { duke@435: st->print("# Static "); duke@435: if (_name != NULL) { duke@435: st->print("%s", _name); duke@435: int trap_req = uncommon_trap_request(); duke@435: if (trap_req != 0) { duke@435: char buf[100]; duke@435: st->print("(%s)", duke@435: Deoptimization::format_trap_request(buf, sizeof(buf), duke@435: trap_req)); duke@435: } duke@435: st->print(" "); duke@435: } duke@435: CallJavaNode::dump_spec(st); duke@435: } duke@435: #endif duke@435: duke@435: //============================================================================= duke@435: uint CallDynamicJavaNode::size_of() const { return sizeof(*this); } duke@435: uint CallDynamicJavaNode::cmp( const Node &n ) const { duke@435: CallDynamicJavaNode &call = (CallDynamicJavaNode&)n; duke@435: return CallJavaNode::cmp(call); duke@435: } duke@435: #ifndef PRODUCT duke@435: void CallDynamicJavaNode::dump_spec(outputStream *st) const { duke@435: st->print("# Dynamic "); duke@435: CallJavaNode::dump_spec(st); duke@435: } duke@435: #endif duke@435: duke@435: //============================================================================= duke@435: uint CallRuntimeNode::size_of() const { return sizeof(*this); } duke@435: uint CallRuntimeNode::cmp( const Node &n ) const { duke@435: CallRuntimeNode &call = (CallRuntimeNode&)n; duke@435: return CallNode::cmp(call) && !strcmp(_name,call._name); duke@435: } duke@435: #ifndef PRODUCT duke@435: void CallRuntimeNode::dump_spec(outputStream *st) const { duke@435: st->print("# "); duke@435: st->print(_name); duke@435: CallNode::dump_spec(st); duke@435: } duke@435: #endif duke@435: duke@435: //------------------------------calling_convention----------------------------- duke@435: void CallRuntimeNode::calling_convention( BasicType* sig_bt, VMRegPair *parm_regs, uint argcnt ) const { duke@435: Matcher::c_calling_convention( sig_bt, parm_regs, argcnt ); duke@435: } duke@435: duke@435: //============================================================================= duke@435: //------------------------------calling_convention----------------------------- duke@435: duke@435: duke@435: //============================================================================= duke@435: #ifndef PRODUCT duke@435: void CallLeafNode::dump_spec(outputStream *st) const { duke@435: st->print("# "); duke@435: st->print(_name); duke@435: CallNode::dump_spec(st); duke@435: } duke@435: #endif duke@435: duke@435: //============================================================================= duke@435: duke@435: void SafePointNode::set_local(JVMState* jvms, uint idx, Node *c) { duke@435: assert(verify_jvms(jvms), "jvms must match"); duke@435: int loc = jvms->locoff() + idx; duke@435: if (in(loc)->is_top() && idx > 0 && !c->is_top() ) { duke@435: // If current local idx is top then local idx - 1 could duke@435: // be a long/double that needs to be killed since top could duke@435: // represent the 2nd half ofthe long/double. duke@435: uint ideal = in(loc -1)->ideal_reg(); duke@435: if (ideal == Op_RegD || ideal == Op_RegL) { duke@435: // set other (low index) half to top duke@435: set_req(loc - 1, in(loc)); duke@435: } duke@435: } duke@435: set_req(loc, c); duke@435: } duke@435: duke@435: uint SafePointNode::size_of() const { return sizeof(*this); } duke@435: uint SafePointNode::cmp( const Node &n ) const { duke@435: return (&n == this); // Always fail except on self duke@435: } duke@435: duke@435: //-------------------------set_next_exception---------------------------------- duke@435: void SafePointNode::set_next_exception(SafePointNode* n) { duke@435: assert(n == NULL || n->Opcode() == Op_SafePoint, "correct value for next_exception"); duke@435: if (len() == req()) { duke@435: if (n != NULL) add_prec(n); duke@435: } else { duke@435: set_prec(req(), n); duke@435: } duke@435: } duke@435: duke@435: duke@435: //----------------------------next_exception----------------------------------- duke@435: SafePointNode* SafePointNode::next_exception() const { duke@435: if (len() == req()) { duke@435: return NULL; duke@435: } else { duke@435: Node* n = in(req()); duke@435: assert(n == NULL || n->Opcode() == Op_SafePoint, "no other uses of prec edges"); duke@435: return (SafePointNode*) n; duke@435: } duke@435: } duke@435: duke@435: duke@435: //------------------------------Ideal------------------------------------------ duke@435: // Skip over any collapsed Regions duke@435: Node *SafePointNode::Ideal(PhaseGVN *phase, bool can_reshape) { kvn@740: return remove_dead_region(phase, can_reshape) ? this : NULL; duke@435: } duke@435: duke@435: //------------------------------Identity--------------------------------------- duke@435: // Remove obviously duplicate safepoints duke@435: Node *SafePointNode::Identity( PhaseTransform *phase ) { duke@435: duke@435: // If you have back to back safepoints, remove one duke@435: if( in(TypeFunc::Control)->is_SafePoint() ) duke@435: return in(TypeFunc::Control); duke@435: duke@435: if( in(0)->is_Proj() ) { duke@435: Node *n0 = in(0)->in(0); duke@435: // Check if he is a call projection (except Leaf Call) duke@435: if( n0->is_Catch() ) { duke@435: n0 = n0->in(0)->in(0); duke@435: assert( n0->is_Call(), "expect a call here" ); duke@435: } duke@435: if( n0->is_Call() && n0->as_Call()->guaranteed_safepoint() ) { duke@435: // Useless Safepoint, so remove it duke@435: return in(TypeFunc::Control); duke@435: } duke@435: } duke@435: duke@435: return this; duke@435: } duke@435: duke@435: //------------------------------Value------------------------------------------ duke@435: const Type *SafePointNode::Value( PhaseTransform *phase ) const { duke@435: if( phase->type(in(0)) == Type::TOP ) return Type::TOP; duke@435: if( phase->eqv( in(0), this ) ) return Type::TOP; // Dead infinite loop duke@435: return Type::CONTROL; duke@435: } duke@435: duke@435: #ifndef PRODUCT duke@435: void SafePointNode::dump_spec(outputStream *st) const { duke@435: st->print(" SafePoint "); duke@435: } duke@435: #endif duke@435: duke@435: const RegMask &SafePointNode::in_RegMask(uint idx) const { duke@435: if( idx < TypeFunc::Parms ) return RegMask::Empty; duke@435: // Values outside the domain represent debug info duke@435: return *(Compile::current()->matcher()->idealreg2debugmask[in(idx)->ideal_reg()]); duke@435: } duke@435: const RegMask &SafePointNode::out_RegMask() const { duke@435: return RegMask::Empty; duke@435: } duke@435: duke@435: duke@435: void SafePointNode::grow_stack(JVMState* jvms, uint grow_by) { duke@435: assert((int)grow_by > 0, "sanity"); duke@435: int monoff = jvms->monoff(); kvn@498: int scloff = jvms->scloff(); duke@435: int endoff = jvms->endoff(); duke@435: assert(endoff == (int)req(), "no other states or debug info after me"); duke@435: Node* top = Compile::current()->top(); duke@435: for (uint i = 0; i < grow_by; i++) { duke@435: ins_req(monoff, top); duke@435: } duke@435: jvms->set_monoff(monoff + grow_by); kvn@498: jvms->set_scloff(scloff + grow_by); duke@435: jvms->set_endoff(endoff + grow_by); duke@435: } duke@435: duke@435: void SafePointNode::push_monitor(const FastLockNode *lock) { duke@435: // Add a LockNode, which points to both the original BoxLockNode (the duke@435: // stack space for the monitor) and the Object being locked. duke@435: const int MonitorEdges = 2; duke@435: assert(JVMState::logMonitorEdges == exact_log2(MonitorEdges), "correct MonitorEdges"); duke@435: assert(req() == jvms()->endoff(), "correct sizing"); kvn@498: int nextmon = jvms()->scloff(); duke@435: if (GenerateSynchronizationCode) { duke@435: add_req(lock->box_node()); duke@435: add_req(lock->obj_node()); duke@435: } else { kvn@895: Node* top = Compile::current()->top(); kvn@895: add_req(top); kvn@895: add_req(top); duke@435: } kvn@498: jvms()->set_scloff(nextmon+MonitorEdges); duke@435: jvms()->set_endoff(req()); duke@435: } duke@435: duke@435: void SafePointNode::pop_monitor() { duke@435: // Delete last monitor from debug info duke@435: debug_only(int num_before_pop = jvms()->nof_monitors()); duke@435: const int MonitorEdges = (1<scloff(); duke@435: int endoff = jvms()->endoff(); kvn@498: int new_scloff = scloff - MonitorEdges; duke@435: int new_endoff = endoff - MonitorEdges; kvn@498: jvms()->set_scloff(new_scloff); duke@435: jvms()->set_endoff(new_endoff); kvn@498: while (scloff > new_scloff) del_req(--scloff); duke@435: assert(jvms()->nof_monitors() == num_before_pop-1, ""); duke@435: } duke@435: duke@435: Node *SafePointNode::peek_monitor_box() const { duke@435: int mon = jvms()->nof_monitors() - 1; duke@435: assert(mon >= 0, "most have a monitor"); duke@435: return monitor_box(jvms(), mon); duke@435: } duke@435: duke@435: Node *SafePointNode::peek_monitor_obj() const { duke@435: int mon = jvms()->nof_monitors() - 1; duke@435: assert(mon >= 0, "most have a monitor"); duke@435: return monitor_obj(jvms(), mon); duke@435: } duke@435: duke@435: // Do we Match on this edge index or not? Match no edges duke@435: uint SafePointNode::match_edge(uint idx) const { duke@435: if( !needs_polling_address_input() ) duke@435: return 0; duke@435: duke@435: return (TypeFunc::Parms == idx); duke@435: } duke@435: kvn@498: //============== SafePointScalarObjectNode ============== kvn@498: kvn@498: SafePointScalarObjectNode::SafePointScalarObjectNode(const TypeOopPtr* tp, kvn@498: #ifdef ASSERT kvn@498: AllocateNode* alloc, kvn@498: #endif kvn@498: uint first_index, kvn@498: uint n_fields) : kvn@498: TypeNode(tp, 1), // 1 control input -- seems required. Get from root. kvn@498: #ifdef ASSERT kvn@498: _alloc(alloc), kvn@498: #endif kvn@498: _first_index(first_index), kvn@498: _n_fields(n_fields) kvn@498: { kvn@498: init_class_id(Class_SafePointScalarObject); kvn@498: } kvn@498: kvn@855: bool SafePointScalarObjectNode::pinned() const { return true; } kvn@1036: bool SafePointScalarObjectNode::depends_only_on_test() const { return false; } kvn@498: kvn@498: uint SafePointScalarObjectNode::ideal_reg() const { kvn@498: return 0; // No matching to machine instruction kvn@498: } kvn@498: kvn@498: const RegMask &SafePointScalarObjectNode::in_RegMask(uint idx) const { kvn@498: return *(Compile::current()->matcher()->idealreg2debugmask[in(idx)->ideal_reg()]); kvn@498: } kvn@498: kvn@498: const RegMask &SafePointScalarObjectNode::out_RegMask() const { kvn@498: return RegMask::Empty; kvn@498: } kvn@498: kvn@498: uint SafePointScalarObjectNode::match_edge(uint idx) const { kvn@498: return 0; kvn@498: } kvn@498: kvn@498: SafePointScalarObjectNode* kvn@498: SafePointScalarObjectNode::clone(int jvms_adj, Dict* sosn_map) const { kvn@498: void* cached = (*sosn_map)[(void*)this]; kvn@498: if (cached != NULL) { kvn@498: return (SafePointScalarObjectNode*)cached; kvn@498: } kvn@498: Compile* C = Compile::current(); kvn@498: SafePointScalarObjectNode* res = (SafePointScalarObjectNode*)Node::clone(); kvn@498: res->_first_index += jvms_adj; kvn@498: sosn_map->Insert((void*)this, (void*)res); kvn@498: return res; kvn@498: } kvn@498: kvn@498: kvn@498: #ifndef PRODUCT kvn@498: void SafePointScalarObjectNode::dump_spec(outputStream *st) const { kvn@498: st->print(" # fields@[%d..%d]", first_index(), kvn@498: first_index() + n_fields() - 1); kvn@498: } kvn@498: kvn@498: #endif kvn@498: duke@435: //============================================================================= duke@435: uint AllocateNode::size_of() const { return sizeof(*this); } duke@435: duke@435: AllocateNode::AllocateNode(Compile* C, const TypeFunc *atype, duke@435: Node *ctrl, Node *mem, Node *abio, duke@435: Node *size, Node *klass_node, Node *initial_test) duke@435: : CallNode(atype, NULL, TypeRawPtr::BOTTOM) duke@435: { duke@435: init_class_id(Class_Allocate); duke@435: init_flags(Flag_is_macro); kvn@474: _is_scalar_replaceable = false; duke@435: Node *topnode = C->top(); duke@435: duke@435: init_req( TypeFunc::Control , ctrl ); duke@435: init_req( TypeFunc::I_O , abio ); duke@435: init_req( TypeFunc::Memory , mem ); duke@435: init_req( TypeFunc::ReturnAdr, topnode ); duke@435: init_req( TypeFunc::FramePtr , topnode ); duke@435: init_req( AllocSize , size); duke@435: init_req( KlassNode , klass_node); duke@435: init_req( InitialTest , initial_test); duke@435: init_req( ALength , topnode); duke@435: C->add_macro_node(this); duke@435: } duke@435: duke@435: //============================================================================= duke@435: uint AllocateArrayNode::size_of() const { return sizeof(*this); } duke@435: kvn@1139: Node* AllocateArrayNode::Ideal(PhaseGVN *phase, bool can_reshape) { kvn@1139: if (remove_dead_region(phase, can_reshape)) return this; kvn@1139: kvn@1139: const Type* type = phase->type(Ideal_length()); kvn@1139: if (type->isa_int() && type->is_int()->_hi < 0) { kvn@1139: if (can_reshape) { kvn@1139: PhaseIterGVN *igvn = phase->is_IterGVN(); kvn@1139: // Unreachable fall through path (negative array length), kvn@1139: // the allocation can only throw so disconnect it. kvn@1139: Node* proj = proj_out(TypeFunc::Control); kvn@1139: Node* catchproj = NULL; kvn@1139: if (proj != NULL) { kvn@1139: for (DUIterator_Fast imax, i = proj->fast_outs(imax); i < imax; i++) { kvn@1139: Node *cn = proj->fast_out(i); kvn@1139: if (cn->is_Catch()) { kvn@1139: catchproj = cn->as_Multi()->proj_out(CatchProjNode::fall_through_index); kvn@1139: break; kvn@1139: } kvn@1139: } kvn@1139: } kvn@1139: if (catchproj != NULL && catchproj->outcnt() > 0 && kvn@1139: (catchproj->outcnt() > 1 || kvn@1139: catchproj->unique_out()->Opcode() != Op_Halt)) { kvn@1139: assert(catchproj->is_CatchProj(), "must be a CatchProjNode"); kvn@1139: Node* nproj = catchproj->clone(); kvn@1139: igvn->register_new_node_with_optimizer(nproj); kvn@1139: kvn@1139: Node *frame = new (phase->C, 1) ParmNode( phase->C->start(), TypeFunc::FramePtr ); kvn@1139: frame = phase->transform(frame); kvn@1139: // Halt & Catch Fire kvn@1139: Node *halt = new (phase->C, TypeFunc::Parms) HaltNode( nproj, frame ); kvn@1139: phase->C->root()->add_req(halt); kvn@1139: phase->transform(halt); kvn@1139: kvn@1139: igvn->replace_node(catchproj, phase->C->top()); kvn@1139: return this; kvn@1139: } kvn@1139: } else { kvn@1139: // Can't correct it during regular GVN so register for IGVN kvn@1139: phase->C->record_for_igvn(this); kvn@1139: } kvn@1139: } kvn@1139: return NULL; kvn@1139: } kvn@1139: rasbold@801: // Retrieve the length from the AllocateArrayNode. Narrow the type with a rasbold@801: // CastII, if appropriate. If we are not allowed to create new nodes, and rasbold@801: // a CastII is appropriate, return NULL. rasbold@801: Node *AllocateArrayNode::make_ideal_length(const TypeOopPtr* oop_type, PhaseTransform *phase, bool allow_new_nodes) { rasbold@801: Node *length = in(AllocateNode::ALength); rasbold@801: assert(length != NULL, "length is not null"); rasbold@801: rasbold@801: const TypeInt* length_type = phase->find_int_type(length); rasbold@801: const TypeAryPtr* ary_type = oop_type->isa_aryptr(); rasbold@801: rasbold@801: if (ary_type != NULL && length_type != NULL) { rasbold@801: const TypeInt* narrow_length_type = ary_type->narrow_size_type(length_type); rasbold@801: if (narrow_length_type != length_type) { rasbold@801: // Assert one of: rasbold@801: // - the narrow_length is 0 rasbold@801: // - the narrow_length is not wider than length rasbold@801: assert(narrow_length_type == TypeInt::ZERO || rasbold@801: (narrow_length_type->_hi <= length_type->_hi && rasbold@801: narrow_length_type->_lo >= length_type->_lo), rasbold@801: "narrow type must be narrower than length type"); rasbold@801: rasbold@801: // Return NULL if new nodes are not allowed rasbold@801: if (!allow_new_nodes) return NULL; rasbold@801: // Create a cast which is control dependent on the initialization to rasbold@801: // propagate the fact that the array length must be positive. rasbold@801: length = new (phase->C, 2) CastIINode(length, narrow_length_type); rasbold@801: length->set_req(0, initialization()->proj_out(0)); rasbold@801: } rasbold@801: } rasbold@801: rasbold@801: return length; rasbold@801: } rasbold@801: duke@435: //============================================================================= duke@435: uint LockNode::size_of() const { return sizeof(*this); } duke@435: duke@435: // Redundant lock elimination duke@435: // duke@435: // There are various patterns of locking where we release and duke@435: // immediately reacquire a lock in a piece of code where no operations duke@435: // occur in between that would be observable. In those cases we can duke@435: // skip releasing and reacquiring the lock without violating any duke@435: // fairness requirements. Doing this around a loop could cause a lock duke@435: // to be held for a very long time so we concentrate on non-looping duke@435: // control flow. We also require that the operations are fully duke@435: // redundant meaning that we don't introduce new lock operations on duke@435: // some paths so to be able to eliminate it on others ala PRE. This duke@435: // would probably require some more extensive graph manipulation to duke@435: // guarantee that the memory edges were all handled correctly. duke@435: // duke@435: // Assuming p is a simple predicate which can't trap in any way and s duke@435: // is a synchronized method consider this code: duke@435: // duke@435: // s(); duke@435: // if (p) duke@435: // s(); duke@435: // else duke@435: // s(); duke@435: // s(); duke@435: // duke@435: // 1. The unlocks of the first call to s can be eliminated if the duke@435: // locks inside the then and else branches are eliminated. duke@435: // duke@435: // 2. The unlocks of the then and else branches can be eliminated if duke@435: // the lock of the final call to s is eliminated. duke@435: // duke@435: // Either of these cases subsumes the simple case of sequential control flow duke@435: // duke@435: // Addtionally we can eliminate versions without the else case: duke@435: // duke@435: // s(); duke@435: // if (p) duke@435: // s(); duke@435: // s(); duke@435: // duke@435: // 3. In this case we eliminate the unlock of the first s, the lock duke@435: // and unlock in the then case and the lock in the final s. duke@435: // duke@435: // Note also that in all these cases the then/else pieces don't have duke@435: // to be trivial as long as they begin and end with synchronization duke@435: // operations. duke@435: // duke@435: // s(); duke@435: // if (p) duke@435: // s(); duke@435: // f(); duke@435: // s(); duke@435: // s(); duke@435: // duke@435: // The code will work properly for this case, leaving in the unlock duke@435: // before the call to f and the relock after it. duke@435: // duke@435: // A potentially interesting case which isn't handled here is when the duke@435: // locking is partially redundant. duke@435: // duke@435: // s(); duke@435: // if (p) duke@435: // s(); duke@435: // duke@435: // This could be eliminated putting unlocking on the else case and duke@435: // eliminating the first unlock and the lock in the then side. duke@435: // Alternatively the unlock could be moved out of the then side so it duke@435: // was after the merge and the first unlock and second lock duke@435: // eliminated. This might require less manipulation of the memory duke@435: // state to get correct. duke@435: // duke@435: // Additionally we might allow work between a unlock and lock before duke@435: // giving up eliminating the locks. The current code disallows any duke@435: // conditional control flow between these operations. A formulation duke@435: // similar to partial redundancy elimination computing the duke@435: // availability of unlocking and the anticipatability of locking at a duke@435: // program point would allow detection of fully redundant locking with duke@435: // some amount of work in between. I'm not sure how often I really duke@435: // think that would occur though. Most of the cases I've seen duke@435: // indicate it's likely non-trivial work would occur in between. duke@435: // There may be other more complicated constructs where we could duke@435: // eliminate locking but I haven't seen any others appear as hot or duke@435: // interesting. duke@435: // duke@435: // Locking and unlocking have a canonical form in ideal that looks duke@435: // roughly like this: duke@435: // duke@435: // duke@435: // | \\------+ duke@435: // | \ \ duke@435: // | BoxLock \ duke@435: // | | | \ duke@435: // | | \ \ duke@435: // | | FastLock duke@435: // | | / duke@435: // | | / duke@435: // | | | duke@435: // duke@435: // Lock duke@435: // | duke@435: // Proj #0 duke@435: // | duke@435: // MembarAcquire duke@435: // | duke@435: // Proj #0 duke@435: // duke@435: // MembarRelease duke@435: // | duke@435: // Proj #0 duke@435: // | duke@435: // Unlock duke@435: // | duke@435: // Proj #0 duke@435: // duke@435: // duke@435: // This code proceeds by processing Lock nodes during PhaseIterGVN duke@435: // and searching back through its control for the proper code duke@435: // patterns. Once it finds a set of lock and unlock operations to duke@435: // eliminate they are marked as eliminatable which causes the duke@435: // expansion of the Lock and Unlock macro nodes to make the operation a NOP duke@435: // duke@435: //============================================================================= duke@435: duke@435: // duke@435: // Utility function to skip over uninteresting control nodes. Nodes skipped are: duke@435: // - copy regions. (These may not have been optimized away yet.) duke@435: // - eliminated locking nodes duke@435: // duke@435: static Node *next_control(Node *ctrl) { duke@435: if (ctrl == NULL) duke@435: return NULL; duke@435: while (1) { duke@435: if (ctrl->is_Region()) { duke@435: RegionNode *r = ctrl->as_Region(); duke@435: Node *n = r->is_copy(); duke@435: if (n == NULL) duke@435: break; // hit a region, return it duke@435: else duke@435: ctrl = n; duke@435: } else if (ctrl->is_Proj()) { duke@435: Node *in0 = ctrl->in(0); duke@435: if (in0->is_AbstractLock() && in0->as_AbstractLock()->is_eliminated()) { duke@435: ctrl = in0->in(0); duke@435: } else { duke@435: break; duke@435: } duke@435: } else { duke@435: break; // found an interesting control duke@435: } duke@435: } duke@435: return ctrl; duke@435: } duke@435: // duke@435: // Given a control, see if it's the control projection of an Unlock which duke@435: // operating on the same object as lock. duke@435: // duke@435: bool AbstractLockNode::find_matching_unlock(const Node* ctrl, LockNode* lock, duke@435: GrowableArray &lock_ops) { duke@435: ProjNode *ctrl_proj = (ctrl->is_Proj()) ? ctrl->as_Proj() : NULL; duke@435: if (ctrl_proj != NULL && ctrl_proj->_con == TypeFunc::Control) { duke@435: Node *n = ctrl_proj->in(0); duke@435: if (n != NULL && n->is_Unlock()) { duke@435: UnlockNode *unlock = n->as_Unlock(); duke@435: if ((lock->obj_node() == unlock->obj_node()) && duke@435: (lock->box_node() == unlock->box_node()) && !unlock->is_eliminated()) { duke@435: lock_ops.append(unlock); duke@435: return true; duke@435: } duke@435: } duke@435: } duke@435: return false; duke@435: } duke@435: duke@435: // duke@435: // Find the lock matching an unlock. Returns null if a safepoint duke@435: // or complicated control is encountered first. duke@435: LockNode *AbstractLockNode::find_matching_lock(UnlockNode* unlock) { duke@435: LockNode *lock_result = NULL; duke@435: // find the matching lock, or an intervening safepoint duke@435: Node *ctrl = next_control(unlock->in(0)); duke@435: while (1) { duke@435: assert(ctrl != NULL, "invalid control graph"); duke@435: assert(!ctrl->is_Start(), "missing lock for unlock"); duke@435: if (ctrl->is_top()) break; // dead control path duke@435: if (ctrl->is_Proj()) ctrl = ctrl->in(0); duke@435: if (ctrl->is_SafePoint()) { duke@435: break; // found a safepoint (may be the lock we are searching for) duke@435: } else if (ctrl->is_Region()) { duke@435: // Check for a simple diamond pattern. Punt on anything more complicated duke@435: if (ctrl->req() == 3 && ctrl->in(1) != NULL && ctrl->in(2) != NULL) { duke@435: Node *in1 = next_control(ctrl->in(1)); duke@435: Node *in2 = next_control(ctrl->in(2)); duke@435: if (((in1->is_IfTrue() && in2->is_IfFalse()) || duke@435: (in2->is_IfTrue() && in1->is_IfFalse())) && (in1->in(0) == in2->in(0))) { duke@435: ctrl = next_control(in1->in(0)->in(0)); duke@435: } else { duke@435: break; duke@435: } duke@435: } else { duke@435: break; duke@435: } duke@435: } else { duke@435: ctrl = next_control(ctrl->in(0)); // keep searching duke@435: } duke@435: } duke@435: if (ctrl->is_Lock()) { duke@435: LockNode *lock = ctrl->as_Lock(); duke@435: if ((lock->obj_node() == unlock->obj_node()) && duke@435: (lock->box_node() == unlock->box_node())) { duke@435: lock_result = lock; duke@435: } duke@435: } duke@435: return lock_result; duke@435: } duke@435: duke@435: // This code corresponds to case 3 above. duke@435: duke@435: bool AbstractLockNode::find_lock_and_unlock_through_if(Node* node, LockNode* lock, duke@435: GrowableArray &lock_ops) { duke@435: Node* if_node = node->in(0); duke@435: bool if_true = node->is_IfTrue(); duke@435: duke@435: if (if_node->is_If() && if_node->outcnt() == 2 && (if_true || node->is_IfFalse())) { duke@435: Node *lock_ctrl = next_control(if_node->in(0)); duke@435: if (find_matching_unlock(lock_ctrl, lock, lock_ops)) { duke@435: Node* lock1_node = NULL; duke@435: ProjNode* proj = if_node->as_If()->proj_out(!if_true); duke@435: if (if_true) { duke@435: if (proj->is_IfFalse() && proj->outcnt() == 1) { duke@435: lock1_node = proj->unique_out(); duke@435: } duke@435: } else { duke@435: if (proj->is_IfTrue() && proj->outcnt() == 1) { duke@435: lock1_node = proj->unique_out(); duke@435: } duke@435: } duke@435: if (lock1_node != NULL && lock1_node->is_Lock()) { duke@435: LockNode *lock1 = lock1_node->as_Lock(); duke@435: if ((lock->obj_node() == lock1->obj_node()) && duke@435: (lock->box_node() == lock1->box_node()) && !lock1->is_eliminated()) { duke@435: lock_ops.append(lock1); duke@435: return true; duke@435: } duke@435: } duke@435: } duke@435: } duke@435: duke@435: lock_ops.trunc_to(0); duke@435: return false; duke@435: } duke@435: duke@435: bool AbstractLockNode::find_unlocks_for_region(const RegionNode* region, LockNode* lock, duke@435: GrowableArray &lock_ops) { duke@435: // check each control merging at this point for a matching unlock. duke@435: // in(0) should be self edge so skip it. duke@435: for (int i = 1; i < (int)region->req(); i++) { duke@435: Node *in_node = next_control(region->in(i)); duke@435: if (in_node != NULL) { duke@435: if (find_matching_unlock(in_node, lock, lock_ops)) { duke@435: // found a match so keep on checking. duke@435: continue; duke@435: } else if (find_lock_and_unlock_through_if(in_node, lock, lock_ops)) { duke@435: continue; duke@435: } duke@435: duke@435: // If we fall through to here then it was some kind of node we duke@435: // don't understand or there wasn't a matching unlock, so give duke@435: // up trying to merge locks. duke@435: lock_ops.trunc_to(0); duke@435: return false; duke@435: } duke@435: } duke@435: return true; duke@435: duke@435: } duke@435: duke@435: #ifndef PRODUCT duke@435: // duke@435: // Create a counter which counts the number of times this lock is acquired duke@435: // duke@435: void AbstractLockNode::create_lock_counter(JVMState* state) { duke@435: _counter = OptoRuntime::new_named_counter(state, NamedCounter::LockCounter); duke@435: } duke@435: #endif duke@435: duke@435: void AbstractLockNode::set_eliminated() { duke@435: _eliminate = true; duke@435: #ifndef PRODUCT duke@435: if (_counter) { duke@435: // Update the counter to indicate that this lock was eliminated. duke@435: // The counter update code will stay around even though the duke@435: // optimizer will eliminate the lock operation itself. duke@435: _counter->set_tag(NamedCounter::EliminatedLockCounter); duke@435: } duke@435: #endif duke@435: } duke@435: duke@435: //============================================================================= duke@435: Node *LockNode::Ideal(PhaseGVN *phase, bool can_reshape) { duke@435: kvn@501: // perform any generic optimizations first (returns 'this' or NULL) duke@435: Node *result = SafePointNode::Ideal(phase, can_reshape); duke@435: duke@435: // Now see if we can optimize away this lock. We don't actually duke@435: // remove the locking here, we simply set the _eliminate flag which duke@435: // prevents macro expansion from expanding the lock. Since we don't duke@435: // modify the graph, the value returned from this function is the duke@435: // one computed above. kvn@501: if (result == NULL && can_reshape && EliminateLocks && !is_eliminated()) { kvn@501: // kvn@501: // If we are locking an unescaped object, the lock/unlock is unnecessary kvn@501: // kvn@895: ConnectionGraph *cgr = phase->C->congraph(); kvn@501: PointsToNode::EscapeState es = PointsToNode::GlobalEscape; kvn@501: if (cgr != NULL) kvn@501: es = cgr->escape_state(obj_node(), phase); kvn@501: if (es != PointsToNode::UnknownEscape && es != PointsToNode::GlobalEscape) { kvn@501: // Mark it eliminated to update any counters kvn@501: this->set_eliminated(); kvn@501: return result; kvn@501: } kvn@501: duke@435: // duke@435: // Try lock coarsening duke@435: // duke@435: PhaseIterGVN* iter = phase->is_IterGVN(); duke@435: if (iter != NULL) { duke@435: duke@435: GrowableArray lock_ops; duke@435: duke@435: Node *ctrl = next_control(in(0)); duke@435: duke@435: // now search back for a matching Unlock duke@435: if (find_matching_unlock(ctrl, this, lock_ops)) { duke@435: // found an unlock directly preceding this lock. This is the duke@435: // case of single unlock directly control dependent on a duke@435: // single lock which is the trivial version of case 1 or 2. duke@435: } else if (ctrl->is_Region() ) { duke@435: if (find_unlocks_for_region(ctrl->as_Region(), this, lock_ops)) { duke@435: // found lock preceded by multiple unlocks along all paths duke@435: // joining at this point which is case 3 in description above. duke@435: } duke@435: } else { duke@435: // see if this lock comes from either half of an if and the duke@435: // predecessors merges unlocks and the other half of the if duke@435: // performs a lock. duke@435: if (find_lock_and_unlock_through_if(ctrl, this, lock_ops)) { duke@435: // found unlock splitting to an if with locks on both branches. duke@435: } duke@435: } duke@435: duke@435: if (lock_ops.length() > 0) { duke@435: // add ourselves to the list of locks to be eliminated. duke@435: lock_ops.append(this); duke@435: duke@435: #ifndef PRODUCT duke@435: if (PrintEliminateLocks) { duke@435: int locks = 0; duke@435: int unlocks = 0; duke@435: for (int i = 0; i < lock_ops.length(); i++) { duke@435: AbstractLockNode* lock = lock_ops.at(i); kvn@501: if (lock->Opcode() == Op_Lock) kvn@501: locks++; kvn@501: else kvn@501: unlocks++; duke@435: if (Verbose) { duke@435: lock->dump(1); duke@435: } duke@435: } duke@435: tty->print_cr("***Eliminated %d unlocks and %d locks", unlocks, locks); duke@435: } duke@435: #endif duke@435: duke@435: // for each of the identified locks, mark them duke@435: // as eliminatable duke@435: for (int i = 0; i < lock_ops.length(); i++) { duke@435: AbstractLockNode* lock = lock_ops.at(i); duke@435: duke@435: // Mark it eliminated to update any counters duke@435: lock->set_eliminated(); kvn@895: lock->set_coarsened(); duke@435: } duke@435: } else if (result != NULL && ctrl->is_Region() && duke@435: iter->_worklist.member(ctrl)) { duke@435: // We weren't able to find any opportunities but the region this duke@435: // lock is control dependent on hasn't been processed yet so put duke@435: // this lock back on the worklist so we can check again once any duke@435: // region simplification has occurred. duke@435: iter->_worklist.push(this); duke@435: } duke@435: } duke@435: } duke@435: duke@435: return result; duke@435: } duke@435: duke@435: //============================================================================= duke@435: uint UnlockNode::size_of() const { return sizeof(*this); } duke@435: duke@435: //============================================================================= duke@435: Node *UnlockNode::Ideal(PhaseGVN *phase, bool can_reshape) { duke@435: kvn@501: // perform any generic optimizations first (returns 'this' or NULL) duke@435: Node * result = SafePointNode::Ideal(phase, can_reshape); duke@435: duke@435: // Now see if we can optimize away this unlock. We don't actually duke@435: // remove the unlocking here, we simply set the _eliminate flag which duke@435: // prevents macro expansion from expanding the unlock. Since we don't duke@435: // modify the graph, the value returned from this function is the duke@435: // one computed above. kvn@501: // Escape state is defined after Parse phase. kvn@501: if (result == NULL && can_reshape && EliminateLocks && !is_eliminated()) { duke@435: // kvn@501: // If we are unlocking an unescaped object, the lock/unlock is unnecessary. duke@435: // kvn@895: ConnectionGraph *cgr = phase->C->congraph(); kvn@501: PointsToNode::EscapeState es = PointsToNode::GlobalEscape; kvn@501: if (cgr != NULL) kvn@501: es = cgr->escape_state(obj_node(), phase); kvn@501: if (es != PointsToNode::UnknownEscape && es != PointsToNode::GlobalEscape) { kvn@501: // Mark it eliminated to update any counters kvn@501: this->set_eliminated(); duke@435: } duke@435: } duke@435: return result; duke@435: }