src/share/vm/opto/callnode.cpp

Fri, 14 Mar 2008 15:26:33 -0700

author
kvn
date
Fri, 14 Mar 2008 15:26:33 -0700
changeset 500
99269dbf4ba8
parent 498
eac007780a58
child 501
6dbf1a175d6b
permissions
-rw-r--r--

6674588: (Escape Analysis) Improve Escape Analysis code
Summary: Current EA code has several problems which have to be fixed.
Reviewed-by: jrose, sgoldman

     1 /*
     2  * Copyright 1997-2006 Sun Microsystems, Inc.  All Rights Reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  *
    19  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
    21  * have any questions.
    22  *
    23  */
    25 // Portions of code courtesy of Clifford Click
    27 // Optimization - Graph Style
    29 #include "incls/_precompiled.incl"
    30 #include "incls/_callnode.cpp.incl"
    32 //=============================================================================
    33 uint StartNode::size_of() const { return sizeof(*this); }
    34 uint StartNode::cmp( const Node &n ) const
    35 { return _domain == ((StartNode&)n)._domain; }
    36 const Type *StartNode::bottom_type() const { return _domain; }
    37 const Type *StartNode::Value(PhaseTransform *phase) const { return _domain; }
    38 #ifndef PRODUCT
    39 void StartNode::dump_spec(outputStream *st) const { st->print(" #"); _domain->dump_on(st);}
    40 #endif
    42 //------------------------------Ideal------------------------------------------
    43 Node *StartNode::Ideal(PhaseGVN *phase, bool can_reshape){
    44   return remove_dead_region(phase, can_reshape) ? this : NULL;
    45 }
    47 //------------------------------calling_convention-----------------------------
    48 void StartNode::calling_convention( BasicType* sig_bt, VMRegPair *parm_regs, uint argcnt ) const {
    49   Matcher::calling_convention( sig_bt, parm_regs, argcnt, false );
    50 }
    52 //------------------------------Registers--------------------------------------
    53 const RegMask &StartNode::in_RegMask(uint) const {
    54   return RegMask::Empty;
    55 }
    57 //------------------------------match------------------------------------------
    58 // Construct projections for incoming parameters, and their RegMask info
    59 Node *StartNode::match( const ProjNode *proj, const Matcher *match ) {
    60   switch (proj->_con) {
    61   case TypeFunc::Control:
    62   case TypeFunc::I_O:
    63   case TypeFunc::Memory:
    64     return new (match->C, 1) MachProjNode(this,proj->_con,RegMask::Empty,MachProjNode::unmatched_proj);
    65   case TypeFunc::FramePtr:
    66     return new (match->C, 1) MachProjNode(this,proj->_con,Matcher::c_frame_ptr_mask, Op_RegP);
    67   case TypeFunc::ReturnAdr:
    68     return new (match->C, 1) MachProjNode(this,proj->_con,match->_return_addr_mask,Op_RegP);
    69   case TypeFunc::Parms:
    70   default: {
    71       uint parm_num = proj->_con - TypeFunc::Parms;
    72       const Type *t = _domain->field_at(proj->_con);
    73       if (t->base() == Type::Half)  // 2nd half of Longs and Doubles
    74         return new (match->C, 1) ConNode(Type::TOP);
    75       uint ideal_reg = Matcher::base2reg[t->base()];
    76       RegMask &rm = match->_calling_convention_mask[parm_num];
    77       return new (match->C, 1) MachProjNode(this,proj->_con,rm,ideal_reg);
    78     }
    79   }
    80   return NULL;
    81 }
    83 //------------------------------StartOSRNode----------------------------------
    84 // The method start node for an on stack replacement adapter
    86 //------------------------------osr_domain-----------------------------
    87 const TypeTuple *StartOSRNode::osr_domain() {
    88   const Type **fields = TypeTuple::fields(2);
    89   fields[TypeFunc::Parms+0] = TypeRawPtr::BOTTOM;  // address of osr buffer
    91   return TypeTuple::make(TypeFunc::Parms+1, fields);
    92 }
    94 //=============================================================================
    95 const char * const ParmNode::names[TypeFunc::Parms+1] = {
    96   "Control", "I_O", "Memory", "FramePtr", "ReturnAdr", "Parms"
    97 };
    99 #ifndef PRODUCT
   100 void ParmNode::dump_spec(outputStream *st) const {
   101   if( _con < TypeFunc::Parms ) {
   102     st->print(names[_con]);
   103   } else {
   104     st->print("Parm%d: ",_con-TypeFunc::Parms);
   105     // Verbose and WizardMode dump bottom_type for all nodes
   106     if( !Verbose && !WizardMode )   bottom_type()->dump_on(st);
   107   }
   108 }
   109 #endif
   111 uint ParmNode::ideal_reg() const {
   112   switch( _con ) {
   113   case TypeFunc::Control  : // fall through
   114   case TypeFunc::I_O      : // fall through
   115   case TypeFunc::Memory   : return 0;
   116   case TypeFunc::FramePtr : // fall through
   117   case TypeFunc::ReturnAdr: return Op_RegP;
   118   default                 : assert( _con > TypeFunc::Parms, "" );
   119     // fall through
   120   case TypeFunc::Parms    : {
   121     // Type of argument being passed
   122     const Type *t = in(0)->as_Start()->_domain->field_at(_con);
   123     return Matcher::base2reg[t->base()];
   124   }
   125   }
   126   ShouldNotReachHere();
   127   return 0;
   128 }
   130 //=============================================================================
   131 ReturnNode::ReturnNode(uint edges, Node *cntrl, Node *i_o, Node *memory, Node *frameptr, Node *retadr ) : Node(edges) {
   132   init_req(TypeFunc::Control,cntrl);
   133   init_req(TypeFunc::I_O,i_o);
   134   init_req(TypeFunc::Memory,memory);
   135   init_req(TypeFunc::FramePtr,frameptr);
   136   init_req(TypeFunc::ReturnAdr,retadr);
   137 }
   139 Node *ReturnNode::Ideal(PhaseGVN *phase, bool can_reshape){
   140   return remove_dead_region(phase, can_reshape) ? this : NULL;
   141 }
   143 const Type *ReturnNode::Value( PhaseTransform *phase ) const {
   144   return ( phase->type(in(TypeFunc::Control)) == Type::TOP)
   145     ? Type::TOP
   146     : Type::BOTTOM;
   147 }
   149 // Do we Match on this edge index or not?  No edges on return nodes
   150 uint ReturnNode::match_edge(uint idx) const {
   151   return 0;
   152 }
   155 #ifndef PRODUCT
   156 void ReturnNode::dump_req() const {
   157   // Dump the required inputs, enclosed in '(' and ')'
   158   uint i;                       // Exit value of loop
   159   for( i=0; i<req(); i++ ) {    // For all required inputs
   160     if( i == TypeFunc::Parms ) tty->print("returns");
   161     if( in(i) ) tty->print("%c%d ", Compile::current()->node_arena()->contains(in(i)) ? ' ' : 'o', in(i)->_idx);
   162     else tty->print("_ ");
   163   }
   164 }
   165 #endif
   167 //=============================================================================
   168 RethrowNode::RethrowNode(
   169   Node* cntrl,
   170   Node* i_o,
   171   Node* memory,
   172   Node* frameptr,
   173   Node* ret_adr,
   174   Node* exception
   175 ) : Node(TypeFunc::Parms + 1) {
   176   init_req(TypeFunc::Control  , cntrl    );
   177   init_req(TypeFunc::I_O      , i_o      );
   178   init_req(TypeFunc::Memory   , memory   );
   179   init_req(TypeFunc::FramePtr , frameptr );
   180   init_req(TypeFunc::ReturnAdr, ret_adr);
   181   init_req(TypeFunc::Parms    , exception);
   182 }
   184 Node *RethrowNode::Ideal(PhaseGVN *phase, bool can_reshape){
   185   return remove_dead_region(phase, can_reshape) ? this : NULL;
   186 }
   188 const Type *RethrowNode::Value( PhaseTransform *phase ) const {
   189   return (phase->type(in(TypeFunc::Control)) == Type::TOP)
   190     ? Type::TOP
   191     : Type::BOTTOM;
   192 }
   194 uint RethrowNode::match_edge(uint idx) const {
   195   return 0;
   196 }
   198 #ifndef PRODUCT
   199 void RethrowNode::dump_req() const {
   200   // Dump the required inputs, enclosed in '(' and ')'
   201   uint i;                       // Exit value of loop
   202   for( i=0; i<req(); i++ ) {    // For all required inputs
   203     if( i == TypeFunc::Parms ) tty->print("exception");
   204     if( in(i) ) tty->print("%c%d ", Compile::current()->node_arena()->contains(in(i)) ? ' ' : 'o', in(i)->_idx);
   205     else tty->print("_ ");
   206   }
   207 }
   208 #endif
   210 //=============================================================================
   211 // Do we Match on this edge index or not?  Match only target address & method
   212 uint TailCallNode::match_edge(uint idx) const {
   213   return TypeFunc::Parms <= idx  &&  idx <= TypeFunc::Parms+1;
   214 }
   216 //=============================================================================
   217 // Do we Match on this edge index or not?  Match only target address & oop
   218 uint TailJumpNode::match_edge(uint idx) const {
   219   return TypeFunc::Parms <= idx  &&  idx <= TypeFunc::Parms+1;
   220 }
   222 //=============================================================================
   223 JVMState::JVMState(ciMethod* method, JVMState* caller) {
   224   assert(method != NULL, "must be valid call site");
   225   _method = method;
   226   debug_only(_bci = -99);  // random garbage value
   227   debug_only(_map = (SafePointNode*)-1);
   228   _caller = caller;
   229   _depth  = 1 + (caller == NULL ? 0 : caller->depth());
   230   _locoff = TypeFunc::Parms;
   231   _stkoff = _locoff + _method->max_locals();
   232   _monoff = _stkoff + _method->max_stack();
   233   _scloff = _monoff;
   234   _endoff = _monoff;
   235   _sp = 0;
   236 }
   237 JVMState::JVMState(int stack_size) {
   238   _method = NULL;
   239   _bci = InvocationEntryBci;
   240   debug_only(_map = (SafePointNode*)-1);
   241   _caller = NULL;
   242   _depth  = 1;
   243   _locoff = TypeFunc::Parms;
   244   _stkoff = _locoff;
   245   _monoff = _stkoff + stack_size;
   246   _scloff = _monoff;
   247   _endoff = _monoff;
   248   _sp = 0;
   249 }
   251 //--------------------------------of_depth-------------------------------------
   252 JVMState* JVMState::of_depth(int d) const {
   253   const JVMState* jvmp = this;
   254   assert(0 < d && (uint)d <= depth(), "oob");
   255   for (int skip = depth() - d; skip > 0; skip--) {
   256     jvmp = jvmp->caller();
   257   }
   258   assert(jvmp->depth() == (uint)d, "found the right one");
   259   return (JVMState*)jvmp;
   260 }
   262 //-----------------------------same_calls_as-----------------------------------
   263 bool JVMState::same_calls_as(const JVMState* that) const {
   264   if (this == that)                    return true;
   265   if (this->depth() != that->depth())  return false;
   266   const JVMState* p = this;
   267   const JVMState* q = that;
   268   for (;;) {
   269     if (p->_method != q->_method)    return false;
   270     if (p->_method == NULL)          return true;   // bci is irrelevant
   271     if (p->_bci    != q->_bci)       return false;
   272     p = p->caller();
   273     q = q->caller();
   274     if (p == q)                      return true;
   275     assert(p != NULL && q != NULL, "depth check ensures we don't run off end");
   276   }
   277 }
   279 //------------------------------debug_start------------------------------------
   280 uint JVMState::debug_start()  const {
   281   debug_only(JVMState* jvmroot = of_depth(1));
   282   assert(jvmroot->locoff() <= this->locoff(), "youngest JVMState must be last");
   283   return of_depth(1)->locoff();
   284 }
   286 //-------------------------------debug_end-------------------------------------
   287 uint JVMState::debug_end() const {
   288   debug_only(JVMState* jvmroot = of_depth(1));
   289   assert(jvmroot->endoff() <= this->endoff(), "youngest JVMState must be last");
   290   return endoff();
   291 }
   293 //------------------------------debug_depth------------------------------------
   294 uint JVMState::debug_depth() const {
   295   uint total = 0;
   296   for (const JVMState* jvmp = this; jvmp != NULL; jvmp = jvmp->caller()) {
   297     total += jvmp->debug_size();
   298   }
   299   return total;
   300 }
   302 #ifndef PRODUCT
   304 //------------------------------format_helper----------------------------------
   305 // Given an allocation (a Chaitin object) and a Node decide if the Node carries
   306 // any defined value or not.  If it does, print out the register or constant.
   307 static void format_helper( PhaseRegAlloc *regalloc, outputStream* st, Node *n, const char *msg, uint i, GrowableArray<SafePointScalarObjectNode*> *scobjs ) {
   308   if (n == NULL) { st->print(" NULL"); return; }
   309   if (n->is_SafePointScalarObject()) {
   310     // Scalar replacement.
   311     SafePointScalarObjectNode* spobj = n->as_SafePointScalarObject();
   312     scobjs->append_if_missing(spobj);
   313     int sco_n = scobjs->find(spobj);
   314     assert(sco_n >= 0, "");
   315     st->print(" %s%d]=#ScObj" INT32_FORMAT, msg, i, sco_n);
   316     return;
   317   }
   318   if( OptoReg::is_valid(regalloc->get_reg_first(n))) { // Check for undefined
   319     char buf[50];
   320     regalloc->dump_register(n,buf);
   321     st->print(" %s%d]=%s",msg,i,buf);
   322   } else {                      // No register, but might be constant
   323     const Type *t = n->bottom_type();
   324     switch (t->base()) {
   325     case Type::Int:
   326       st->print(" %s%d]=#"INT32_FORMAT,msg,i,t->is_int()->get_con());
   327       break;
   328     case Type::AnyPtr:
   329       assert( t == TypePtr::NULL_PTR, "" );
   330       st->print(" %s%d]=#NULL",msg,i);
   331       break;
   332     case Type::AryPtr:
   333     case Type::KlassPtr:
   334     case Type::InstPtr:
   335       st->print(" %s%d]=#Ptr" INTPTR_FORMAT,msg,i,t->isa_oopptr()->const_oop());
   336       break;
   337     case Type::RawPtr:
   338       st->print(" %s%d]=#Raw" INTPTR_FORMAT,msg,i,t->is_rawptr());
   339       break;
   340     case Type::DoubleCon:
   341       st->print(" %s%d]=#%fD",msg,i,t->is_double_constant()->_d);
   342       break;
   343     case Type::FloatCon:
   344       st->print(" %s%d]=#%fF",msg,i,t->is_float_constant()->_f);
   345       break;
   346     case Type::Long:
   347       st->print(" %s%d]=#"INT64_FORMAT,msg,i,t->is_long()->get_con());
   348       break;
   349     case Type::Half:
   350     case Type::Top:
   351       st->print(" %s%d]=_",msg,i);
   352       break;
   353     default: ShouldNotReachHere();
   354     }
   355   }
   356 }
   358 //------------------------------format-----------------------------------------
   359 void JVMState::format(PhaseRegAlloc *regalloc, const Node *n, outputStream* st) const {
   360   st->print("        #");
   361   if( _method ) {
   362     _method->print_short_name(st);
   363     st->print(" @ bci:%d ",_bci);
   364   } else {
   365     st->print_cr(" runtime stub ");
   366     return;
   367   }
   368   if (n->is_MachSafePoint()) {
   369     GrowableArray<SafePointScalarObjectNode*> scobjs;
   370     MachSafePointNode *mcall = n->as_MachSafePoint();
   371     uint i;
   372     // Print locals
   373     for( i = 0; i < (uint)loc_size(); i++ )
   374       format_helper( regalloc, st, mcall->local(this, i), "L[", i, &scobjs );
   375     // Print stack
   376     for (i = 0; i < (uint)stk_size(); i++) {
   377       if ((uint)(_stkoff + i) >= mcall->len())
   378         st->print(" oob ");
   379       else
   380        format_helper( regalloc, st, mcall->stack(this, i), "STK[", i, &scobjs );
   381     }
   382     for (i = 0; (int)i < nof_monitors(); i++) {
   383       Node *box = mcall->monitor_box(this, i);
   384       Node *obj = mcall->monitor_obj(this, i);
   385       if ( OptoReg::is_valid(regalloc->get_reg_first(box)) ) {
   386         while( !box->is_BoxLock() )  box = box->in(1);
   387         format_helper( regalloc, st, box, "MON-BOX[", i, &scobjs );
   388       } else {
   389         OptoReg::Name box_reg = BoxLockNode::stack_slot(box);
   390         st->print(" MON-BOX%d=%s+%d",
   391                    i,
   392                    OptoReg::regname(OptoReg::c_frame_pointer),
   393                    regalloc->reg2offset(box_reg));
   394       }
   395       format_helper( regalloc, st, obj, "MON-OBJ[", i, &scobjs );
   396     }
   398     for (i = 0; i < (uint)scobjs.length(); i++) {
   399       // Scalar replaced objects.
   400       st->print_cr("");
   401       st->print("        # ScObj" INT32_FORMAT " ", i);
   402       SafePointScalarObjectNode* spobj = scobjs.at(i);
   403       ciKlass* cik = spobj->bottom_type()->is_oopptr()->klass();
   404       assert(cik->is_instance_klass() ||
   405              cik->is_array_klass(), "Not supported allocation.");
   406       ciInstanceKlass *iklass = NULL;
   407       if (cik->is_instance_klass()) {
   408         cik->print_name_on(st);
   409         iklass = cik->as_instance_klass();
   410       } else if (cik->is_type_array_klass()) {
   411         cik->as_array_klass()->base_element_type()->print_name_on(st);
   412         st->print("[%d]=", spobj->n_fields());
   413       } else if (cik->is_obj_array_klass()) {
   414         ciType* cie = cik->as_array_klass()->base_element_type();
   415         int ndim = 1;
   416         while (cie->is_obj_array_klass()) {
   417           ndim += 1;
   418           cie = cie->as_array_klass()->base_element_type();
   419         }
   420         cie->print_name_on(st);
   421         while (ndim-- > 0) {
   422           st->print("[]");
   423         }
   424         st->print("[%d]=", spobj->n_fields());
   425       }
   426       st->print("{");
   427       uint nf = spobj->n_fields();
   428       if (nf > 0) {
   429         uint first_ind = spobj->first_index();
   430         Node* fld_node = mcall->in(first_ind);
   431         ciField* cifield;
   432         if (iklass != NULL) {
   433           st->print(" [");
   434           cifield = iklass->nonstatic_field_at(0);
   435           cifield->print_name_on(st);
   436           format_helper( regalloc, st, fld_node, ":", 0, &scobjs );
   437         } else {
   438           format_helper( regalloc, st, fld_node, "[", 0, &scobjs );
   439         }
   440         for (uint j = 1; j < nf; j++) {
   441           fld_node = mcall->in(first_ind+j);
   442           if (iklass != NULL) {
   443             st->print(", [");
   444             cifield = iklass->nonstatic_field_at(j);
   445             cifield->print_name_on(st);
   446             format_helper( regalloc, st, fld_node, ":", j, &scobjs );
   447           } else {
   448             format_helper( regalloc, st, fld_node, ", [", j, &scobjs );
   449           }
   450         }
   451       }
   452       st->print(" }");
   453     }
   454   }
   455   st->print_cr("");
   456   if (caller() != NULL)  caller()->format(regalloc, n, st);
   457 }
   460 void JVMState::dump_spec(outputStream *st) const {
   461   if (_method != NULL) {
   462     bool printed = false;
   463     if (!Verbose) {
   464       // The JVMS dumps make really, really long lines.
   465       // Take out the most boring parts, which are the package prefixes.
   466       char buf[500];
   467       stringStream namest(buf, sizeof(buf));
   468       _method->print_short_name(&namest);
   469       if (namest.count() < sizeof(buf)) {
   470         const char* name = namest.base();
   471         if (name[0] == ' ')  ++name;
   472         const char* endcn = strchr(name, ':');  // end of class name
   473         if (endcn == NULL)  endcn = strchr(name, '(');
   474         if (endcn == NULL)  endcn = name + strlen(name);
   475         while (endcn > name && endcn[-1] != '.' && endcn[-1] != '/')
   476           --endcn;
   477         st->print(" %s", endcn);
   478         printed = true;
   479       }
   480     }
   481     if (!printed)
   482       _method->print_short_name(st);
   483     st->print(" @ bci:%d",_bci);
   484   } else {
   485     st->print(" runtime stub");
   486   }
   487   if (caller() != NULL)  caller()->dump_spec(st);
   488 }
   491 void JVMState::dump_on(outputStream* st) const {
   492   if (_map && !((uintptr_t)_map & 1)) {
   493     if (_map->len() > _map->req()) {  // _map->has_exceptions()
   494       Node* ex = _map->in(_map->req());  // _map->next_exception()
   495       // skip the first one; it's already being printed
   496       while (ex != NULL && ex->len() > ex->req()) {
   497         ex = ex->in(ex->req());  // ex->next_exception()
   498         ex->dump(1);
   499       }
   500     }
   501     _map->dump(2);
   502   }
   503   st->print("JVMS depth=%d loc=%d stk=%d mon=%d scalar=%d end=%d mondepth=%d sp=%d bci=%d method=",
   504              depth(), locoff(), stkoff(), monoff(), scloff(), endoff(), monitor_depth(), sp(), bci());
   505   if (_method == NULL) {
   506     st->print_cr("(none)");
   507   } else {
   508     _method->print_name(st);
   509     st->cr();
   510     if (bci() >= 0 && bci() < _method->code_size()) {
   511       st->print("    bc: ");
   512       _method->print_codes_on(bci(), bci()+1, st);
   513     }
   514   }
   515   if (caller() != NULL) {
   516     caller()->dump_on(st);
   517   }
   518 }
   520 // Extra way to dump a jvms from the debugger,
   521 // to avoid a bug with C++ member function calls.
   522 void dump_jvms(JVMState* jvms) {
   523   jvms->dump();
   524 }
   525 #endif
   527 //--------------------------clone_shallow--------------------------------------
   528 JVMState* JVMState::clone_shallow(Compile* C) const {
   529   JVMState* n = has_method() ? new (C) JVMState(_method, _caller) : new (C) JVMState(0);
   530   n->set_bci(_bci);
   531   n->set_locoff(_locoff);
   532   n->set_stkoff(_stkoff);
   533   n->set_monoff(_monoff);
   534   n->set_scloff(_scloff);
   535   n->set_endoff(_endoff);
   536   n->set_sp(_sp);
   537   n->set_map(_map);
   538   return n;
   539 }
   541 //---------------------------clone_deep----------------------------------------
   542 JVMState* JVMState::clone_deep(Compile* C) const {
   543   JVMState* n = clone_shallow(C);
   544   for (JVMState* p = n; p->_caller != NULL; p = p->_caller) {
   545     p->_caller = p->_caller->clone_shallow(C);
   546   }
   547   assert(n->depth() == depth(), "sanity");
   548   assert(n->debug_depth() == debug_depth(), "sanity");
   549   return n;
   550 }
   552 //=============================================================================
   553 uint CallNode::cmp( const Node &n ) const
   554 { return _tf == ((CallNode&)n)._tf && _jvms == ((CallNode&)n)._jvms; }
   555 #ifndef PRODUCT
   556 void CallNode::dump_req() const {
   557   // Dump the required inputs, enclosed in '(' and ')'
   558   uint i;                       // Exit value of loop
   559   for( i=0; i<req(); i++ ) {    // For all required inputs
   560     if( i == TypeFunc::Parms ) tty->print("(");
   561     if( in(i) ) tty->print("%c%d ", Compile::current()->node_arena()->contains(in(i)) ? ' ' : 'o', in(i)->_idx);
   562     else tty->print("_ ");
   563   }
   564   tty->print(")");
   565 }
   567 void CallNode::dump_spec(outputStream *st) const {
   568   st->print(" ");
   569   tf()->dump_on(st);
   570   if (_cnt != COUNT_UNKNOWN)  st->print(" C=%f",_cnt);
   571   if (jvms() != NULL)  jvms()->dump_spec(st);
   572 }
   573 #endif
   575 const Type *CallNode::bottom_type() const { return tf()->range(); }
   576 const Type *CallNode::Value(PhaseTransform *phase) const {
   577   if (phase->type(in(0)) == Type::TOP)  return Type::TOP;
   578   return tf()->range();
   579 }
   581 //------------------------------calling_convention-----------------------------
   582 void CallNode::calling_convention( BasicType* sig_bt, VMRegPair *parm_regs, uint argcnt ) const {
   583   // Use the standard compiler calling convention
   584   Matcher::calling_convention( sig_bt, parm_regs, argcnt, true );
   585 }
   588 //------------------------------match------------------------------------------
   589 // Construct projections for control, I/O, memory-fields, ..., and
   590 // return result(s) along with their RegMask info
   591 Node *CallNode::match( const ProjNode *proj, const Matcher *match ) {
   592   switch (proj->_con) {
   593   case TypeFunc::Control:
   594   case TypeFunc::I_O:
   595   case TypeFunc::Memory:
   596     return new (match->C, 1) MachProjNode(this,proj->_con,RegMask::Empty,MachProjNode::unmatched_proj);
   598   case TypeFunc::Parms+1:       // For LONG & DOUBLE returns
   599     assert(tf()->_range->field_at(TypeFunc::Parms+1) == Type::HALF, "");
   600     // 2nd half of doubles and longs
   601     return new (match->C, 1) MachProjNode(this,proj->_con, RegMask::Empty, (uint)OptoReg::Bad);
   603   case TypeFunc::Parms: {       // Normal returns
   604     uint ideal_reg = Matcher::base2reg[tf()->range()->field_at(TypeFunc::Parms)->base()];
   605     OptoRegPair regs = is_CallRuntime()
   606       ? match->c_return_value(ideal_reg,true)  // Calls into C runtime
   607       : match->  return_value(ideal_reg,true); // Calls into compiled Java code
   608     RegMask rm = RegMask(regs.first());
   609     if( OptoReg::is_valid(regs.second()) )
   610       rm.Insert( regs.second() );
   611     return new (match->C, 1) MachProjNode(this,proj->_con,rm,ideal_reg);
   612   }
   614   case TypeFunc::ReturnAdr:
   615   case TypeFunc::FramePtr:
   616   default:
   617     ShouldNotReachHere();
   618   }
   619   return NULL;
   620 }
   622 // Do we Match on this edge index or not?  Match no edges
   623 uint CallNode::match_edge(uint idx) const {
   624   return 0;
   625 }
   627 //
   628 // Determine whether the call could modify a memory value  of the
   629 // specified address type
   630 //
   631 bool CallNode::may_modify(const TypePtr *addr_t, PhaseTransform *phase) {
   632   const TypeOopPtr *adrInst_t  = addr_t->isa_oopptr();
   634   // if not an InstPtr or not an instance type, assume the worst
   635   if (adrInst_t == NULL || !adrInst_t->is_instance_field()) {
   636     return true;
   637   }
   638   Compile *C = phase->C;
   639   int offset = adrInst_t->offset();
   640   assert(offset >= 0, "should be valid offset");
   641   assert(addr_t->isa_instptr() || addr_t->isa_aryptr(), "only instances or arrays are expected");
   643   int base_idx = C->get_alias_index(adrInst_t);
   644   ciMethod * meth = is_CallStaticJava() ?  as_CallStaticJava()->method() : NULL;
   645   BCEscapeAnalyzer *bcea = (meth != NULL) ? meth->get_bcea() : NULL;
   647   const TypeTuple * d = tf()->domain();
   648   for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
   649     const Type* t = d->field_at(i);
   650     Node *arg = in(i);
   651     const Type *at = phase->type(arg);
   652     if (at == TypePtr::NULL_PTR || at == Type::TOP)
   653       continue;  // null can't affect anything
   655     const TypeOopPtr *at_ptr = at->isa_oopptr();
   656     if (!arg->is_top() && (t->isa_oopptr() != NULL ||
   657                            t->isa_ptr() && at_ptr != NULL)) {
   658       assert(at_ptr != NULL, "expecting an OopPtr");
   659       // If we have found an argument matching adr_base_t, check if the field
   660       // at the specified offset is modified.  Since we don't know the size,
   661       // assume 8.
   662       int at_idx = C->get_alias_index(at_ptr->add_offset(offset)->isa_oopptr());
   663       if (base_idx == at_idx &&
   664           (bcea == NULL ||
   665            bcea->is_arg_modified(i - TypeFunc::Parms, offset, 8))) {
   666         return true;
   667       }
   668     }
   669   }
   670   return false;
   671 }
   673 // Does this call have a direct reference to n other than debug information?
   674 bool CallNode::has_non_debug_use(Node *n) {
   675   const TypeTuple * d = tf()->domain();
   676   for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
   677     Node *arg = in(i);
   678     if (arg == n) {
   679       return true;
   680     }
   681   }
   682   return false;
   683 }
   685 // Returns the unique CheckCastPP of a call
   686 // or 'this' if there are several CheckCastPP
   687 // or returns NULL if there is no one.
   688 Node *CallNode::result_cast() {
   689   Node *cast = NULL;
   691   Node *p = proj_out(TypeFunc::Parms);
   692   if (p == NULL)
   693     return NULL;
   695   for (DUIterator_Fast imax, i = p->fast_outs(imax); i < imax; i++) {
   696     Node *use = p->fast_out(i);
   697     if (use->is_CheckCastPP()) {
   698       if (cast != NULL) {
   699         return this;  // more than 1 CheckCastPP
   700       }
   701       cast = use;
   702     }
   703   }
   704   return cast;
   705 }
   708 //=============================================================================
   709 uint CallJavaNode::size_of() const { return sizeof(*this); }
   710 uint CallJavaNode::cmp( const Node &n ) const {
   711   CallJavaNode &call = (CallJavaNode&)n;
   712   return CallNode::cmp(call) && _method == call._method;
   713 }
   714 #ifndef PRODUCT
   715 void CallJavaNode::dump_spec(outputStream *st) const {
   716   if( _method ) _method->print_short_name(st);
   717   CallNode::dump_spec(st);
   718 }
   719 #endif
   721 //=============================================================================
   722 uint CallStaticJavaNode::size_of() const { return sizeof(*this); }
   723 uint CallStaticJavaNode::cmp( const Node &n ) const {
   724   CallStaticJavaNode &call = (CallStaticJavaNode&)n;
   725   return CallJavaNode::cmp(call);
   726 }
   728 //----------------------------uncommon_trap_request----------------------------
   729 // If this is an uncommon trap, return the request code, else zero.
   730 int CallStaticJavaNode::uncommon_trap_request() const {
   731   if (_name != NULL && !strcmp(_name, "uncommon_trap")) {
   732     return extract_uncommon_trap_request(this);
   733   }
   734   return 0;
   735 }
   736 int CallStaticJavaNode::extract_uncommon_trap_request(const Node* call) {
   737 #ifndef PRODUCT
   738   if (!(call->req() > TypeFunc::Parms &&
   739         call->in(TypeFunc::Parms) != NULL &&
   740         call->in(TypeFunc::Parms)->is_Con())) {
   741     assert(_in_dump_cnt != 0, "OK if dumping");
   742     tty->print("[bad uncommon trap]");
   743     return 0;
   744   }
   745 #endif
   746   return call->in(TypeFunc::Parms)->bottom_type()->is_int()->get_con();
   747 }
   749 #ifndef PRODUCT
   750 void CallStaticJavaNode::dump_spec(outputStream *st) const {
   751   st->print("# Static ");
   752   if (_name != NULL) {
   753     st->print("%s", _name);
   754     int trap_req = uncommon_trap_request();
   755     if (trap_req != 0) {
   756       char buf[100];
   757       st->print("(%s)",
   758                  Deoptimization::format_trap_request(buf, sizeof(buf),
   759                                                      trap_req));
   760     }
   761     st->print(" ");
   762   }
   763   CallJavaNode::dump_spec(st);
   764 }
   765 #endif
   767 //=============================================================================
   768 uint CallDynamicJavaNode::size_of() const { return sizeof(*this); }
   769 uint CallDynamicJavaNode::cmp( const Node &n ) const {
   770   CallDynamicJavaNode &call = (CallDynamicJavaNode&)n;
   771   return CallJavaNode::cmp(call);
   772 }
   773 #ifndef PRODUCT
   774 void CallDynamicJavaNode::dump_spec(outputStream *st) const {
   775   st->print("# Dynamic ");
   776   CallJavaNode::dump_spec(st);
   777 }
   778 #endif
   780 //=============================================================================
   781 uint CallRuntimeNode::size_of() const { return sizeof(*this); }
   782 uint CallRuntimeNode::cmp( const Node &n ) const {
   783   CallRuntimeNode &call = (CallRuntimeNode&)n;
   784   return CallNode::cmp(call) && !strcmp(_name,call._name);
   785 }
   786 #ifndef PRODUCT
   787 void CallRuntimeNode::dump_spec(outputStream *st) const {
   788   st->print("# ");
   789   st->print(_name);
   790   CallNode::dump_spec(st);
   791 }
   792 #endif
   794 //------------------------------calling_convention-----------------------------
   795 void CallRuntimeNode::calling_convention( BasicType* sig_bt, VMRegPair *parm_regs, uint argcnt ) const {
   796   Matcher::c_calling_convention( sig_bt, parm_regs, argcnt );
   797 }
   799 //=============================================================================
   800 //------------------------------calling_convention-----------------------------
   803 //=============================================================================
   804 #ifndef PRODUCT
   805 void CallLeafNode::dump_spec(outputStream *st) const {
   806   st->print("# ");
   807   st->print(_name);
   808   CallNode::dump_spec(st);
   809 }
   810 #endif
   812 //=============================================================================
   814 void SafePointNode::set_local(JVMState* jvms, uint idx, Node *c) {
   815   assert(verify_jvms(jvms), "jvms must match");
   816   int loc = jvms->locoff() + idx;
   817   if (in(loc)->is_top() && idx > 0 && !c->is_top() ) {
   818     // If current local idx is top then local idx - 1 could
   819     // be a long/double that needs to be killed since top could
   820     // represent the 2nd half ofthe long/double.
   821     uint ideal = in(loc -1)->ideal_reg();
   822     if (ideal == Op_RegD || ideal == Op_RegL) {
   823       // set other (low index) half to top
   824       set_req(loc - 1, in(loc));
   825     }
   826   }
   827   set_req(loc, c);
   828 }
   830 uint SafePointNode::size_of() const { return sizeof(*this); }
   831 uint SafePointNode::cmp( const Node &n ) const {
   832   return (&n == this);          // Always fail except on self
   833 }
   835 //-------------------------set_next_exception----------------------------------
   836 void SafePointNode::set_next_exception(SafePointNode* n) {
   837   assert(n == NULL || n->Opcode() == Op_SafePoint, "correct value for next_exception");
   838   if (len() == req()) {
   839     if (n != NULL)  add_prec(n);
   840   } else {
   841     set_prec(req(), n);
   842   }
   843 }
   846 //----------------------------next_exception-----------------------------------
   847 SafePointNode* SafePointNode::next_exception() const {
   848   if (len() == req()) {
   849     return NULL;
   850   } else {
   851     Node* n = in(req());
   852     assert(n == NULL || n->Opcode() == Op_SafePoint, "no other uses of prec edges");
   853     return (SafePointNode*) n;
   854   }
   855 }
   858 //------------------------------Ideal------------------------------------------
   859 // Skip over any collapsed Regions
   860 Node *SafePointNode::Ideal(PhaseGVN *phase, bool can_reshape) {
   861   if (remove_dead_region(phase, can_reshape))  return this;
   863   return NULL;
   864 }
   866 //------------------------------Identity---------------------------------------
   867 // Remove obviously duplicate safepoints
   868 Node *SafePointNode::Identity( PhaseTransform *phase ) {
   870   // If you have back to back safepoints, remove one
   871   if( in(TypeFunc::Control)->is_SafePoint() )
   872     return in(TypeFunc::Control);
   874   if( in(0)->is_Proj() ) {
   875     Node *n0 = in(0)->in(0);
   876     // Check if he is a call projection (except Leaf Call)
   877     if( n0->is_Catch() ) {
   878       n0 = n0->in(0)->in(0);
   879       assert( n0->is_Call(), "expect a call here" );
   880     }
   881     if( n0->is_Call() && n0->as_Call()->guaranteed_safepoint() ) {
   882       // Useless Safepoint, so remove it
   883       return in(TypeFunc::Control);
   884     }
   885   }
   887   return this;
   888 }
   890 //------------------------------Value------------------------------------------
   891 const Type *SafePointNode::Value( PhaseTransform *phase ) const {
   892   if( phase->type(in(0)) == Type::TOP ) return Type::TOP;
   893   if( phase->eqv( in(0), this ) ) return Type::TOP; // Dead infinite loop
   894   return Type::CONTROL;
   895 }
   897 #ifndef PRODUCT
   898 void SafePointNode::dump_spec(outputStream *st) const {
   899   st->print(" SafePoint ");
   900 }
   901 #endif
   903 const RegMask &SafePointNode::in_RegMask(uint idx) const {
   904   if( idx < TypeFunc::Parms ) return RegMask::Empty;
   905   // Values outside the domain represent debug info
   906   return *(Compile::current()->matcher()->idealreg2debugmask[in(idx)->ideal_reg()]);
   907 }
   908 const RegMask &SafePointNode::out_RegMask() const {
   909   return RegMask::Empty;
   910 }
   913 void SafePointNode::grow_stack(JVMState* jvms, uint grow_by) {
   914   assert((int)grow_by > 0, "sanity");
   915   int monoff = jvms->monoff();
   916   int scloff = jvms->scloff();
   917   int endoff = jvms->endoff();
   918   assert(endoff == (int)req(), "no other states or debug info after me");
   919   Node* top = Compile::current()->top();
   920   for (uint i = 0; i < grow_by; i++) {
   921     ins_req(monoff, top);
   922   }
   923   jvms->set_monoff(monoff + grow_by);
   924   jvms->set_scloff(scloff + grow_by);
   925   jvms->set_endoff(endoff + grow_by);
   926 }
   928 void SafePointNode::push_monitor(const FastLockNode *lock) {
   929   // Add a LockNode, which points to both the original BoxLockNode (the
   930   // stack space for the monitor) and the Object being locked.
   931   const int MonitorEdges = 2;
   932   assert(JVMState::logMonitorEdges == exact_log2(MonitorEdges), "correct MonitorEdges");
   933   assert(req() == jvms()->endoff(), "correct sizing");
   934   int nextmon = jvms()->scloff();
   935   if (GenerateSynchronizationCode) {
   936     add_req(lock->box_node());
   937     add_req(lock->obj_node());
   938   } else {
   939     add_req(NULL);
   940     add_req(NULL);
   941   }
   942   jvms()->set_scloff(nextmon+MonitorEdges);
   943   jvms()->set_endoff(req());
   944 }
   946 void SafePointNode::pop_monitor() {
   947   // Delete last monitor from debug info
   948   debug_only(int num_before_pop = jvms()->nof_monitors());
   949   const int MonitorEdges = (1<<JVMState::logMonitorEdges);
   950   int scloff = jvms()->scloff();
   951   int endoff = jvms()->endoff();
   952   int new_scloff = scloff - MonitorEdges;
   953   int new_endoff = endoff - MonitorEdges;
   954   jvms()->set_scloff(new_scloff);
   955   jvms()->set_endoff(new_endoff);
   956   while (scloff > new_scloff)  del_req(--scloff);
   957   assert(jvms()->nof_monitors() == num_before_pop-1, "");
   958 }
   960 Node *SafePointNode::peek_monitor_box() const {
   961   int mon = jvms()->nof_monitors() - 1;
   962   assert(mon >= 0, "most have a monitor");
   963   return monitor_box(jvms(), mon);
   964 }
   966 Node *SafePointNode::peek_monitor_obj() const {
   967   int mon = jvms()->nof_monitors() - 1;
   968   assert(mon >= 0, "most have a monitor");
   969   return monitor_obj(jvms(), mon);
   970 }
   972 // Do we Match on this edge index or not?  Match no edges
   973 uint SafePointNode::match_edge(uint idx) const {
   974   if( !needs_polling_address_input() )
   975     return 0;
   977   return (TypeFunc::Parms == idx);
   978 }
   980 //==============  SafePointScalarObjectNode  ==============
   982 SafePointScalarObjectNode::SafePointScalarObjectNode(const TypeOopPtr* tp,
   983 #ifdef ASSERT
   984                                                      AllocateNode* alloc,
   985 #endif
   986                                                      uint first_index,
   987                                                      uint n_fields) :
   988   TypeNode(tp, 1), // 1 control input -- seems required.  Get from root.
   989 #ifdef ASSERT
   990   _alloc(alloc),
   991 #endif
   992   _first_index(first_index),
   993   _n_fields(n_fields)
   994 {
   995   init_class_id(Class_SafePointScalarObject);
   996 }
   999 uint SafePointScalarObjectNode::ideal_reg() const {
  1000   return 0; // No matching to machine instruction
  1003 const RegMask &SafePointScalarObjectNode::in_RegMask(uint idx) const {
  1004   return *(Compile::current()->matcher()->idealreg2debugmask[in(idx)->ideal_reg()]);
  1007 const RegMask &SafePointScalarObjectNode::out_RegMask() const {
  1008   return RegMask::Empty;
  1011 uint SafePointScalarObjectNode::match_edge(uint idx) const {
  1012   return 0;
  1015 SafePointScalarObjectNode*
  1016 SafePointScalarObjectNode::clone(int jvms_adj, Dict* sosn_map) const {
  1017   void* cached = (*sosn_map)[(void*)this];
  1018   if (cached != NULL) {
  1019     return (SafePointScalarObjectNode*)cached;
  1021   Compile* C = Compile::current();
  1022   SafePointScalarObjectNode* res = (SafePointScalarObjectNode*)Node::clone();
  1023   res->_first_index += jvms_adj;
  1024   sosn_map->Insert((void*)this, (void*)res);
  1025   return res;
  1029 #ifndef PRODUCT
  1030 void SafePointScalarObjectNode::dump_spec(outputStream *st) const {
  1031   st->print(" # fields@[%d..%d]", first_index(),
  1032              first_index() + n_fields() - 1);
  1035 #endif
  1037 //=============================================================================
  1038 uint AllocateNode::size_of() const { return sizeof(*this); }
  1040 AllocateNode::AllocateNode(Compile* C, const TypeFunc *atype,
  1041                            Node *ctrl, Node *mem, Node *abio,
  1042                            Node *size, Node *klass_node, Node *initial_test)
  1043   : CallNode(atype, NULL, TypeRawPtr::BOTTOM)
  1045   init_class_id(Class_Allocate);
  1046   init_flags(Flag_is_macro);
  1047   _is_scalar_replaceable = false;
  1048   Node *topnode = C->top();
  1050   init_req( TypeFunc::Control  , ctrl );
  1051   init_req( TypeFunc::I_O      , abio );
  1052   init_req( TypeFunc::Memory   , mem );
  1053   init_req( TypeFunc::ReturnAdr, topnode );
  1054   init_req( TypeFunc::FramePtr , topnode );
  1055   init_req( AllocSize          , size);
  1056   init_req( KlassNode          , klass_node);
  1057   init_req( InitialTest        , initial_test);
  1058   init_req( ALength            , topnode);
  1059   C->add_macro_node(this);
  1062 //=============================================================================
  1063 uint AllocateArrayNode::size_of() const { return sizeof(*this); }
  1065 //=============================================================================
  1066 uint LockNode::size_of() const { return sizeof(*this); }
  1068 // Redundant lock elimination
  1069 //
  1070 // There are various patterns of locking where we release and
  1071 // immediately reacquire a lock in a piece of code where no operations
  1072 // occur in between that would be observable.  In those cases we can
  1073 // skip releasing and reacquiring the lock without violating any
  1074 // fairness requirements.  Doing this around a loop could cause a lock
  1075 // to be held for a very long time so we concentrate on non-looping
  1076 // control flow.  We also require that the operations are fully
  1077 // redundant meaning that we don't introduce new lock operations on
  1078 // some paths so to be able to eliminate it on others ala PRE.  This
  1079 // would probably require some more extensive graph manipulation to
  1080 // guarantee that the memory edges were all handled correctly.
  1081 //
  1082 // Assuming p is a simple predicate which can't trap in any way and s
  1083 // is a synchronized method consider this code:
  1084 //
  1085 //   s();
  1086 //   if (p)
  1087 //     s();
  1088 //   else
  1089 //     s();
  1090 //   s();
  1091 //
  1092 // 1. The unlocks of the first call to s can be eliminated if the
  1093 // locks inside the then and else branches are eliminated.
  1094 //
  1095 // 2. The unlocks of the then and else branches can be eliminated if
  1096 // the lock of the final call to s is eliminated.
  1097 //
  1098 // Either of these cases subsumes the simple case of sequential control flow
  1099 //
  1100 // Addtionally we can eliminate versions without the else case:
  1101 //
  1102 //   s();
  1103 //   if (p)
  1104 //     s();
  1105 //   s();
  1106 //
  1107 // 3. In this case we eliminate the unlock of the first s, the lock
  1108 // and unlock in the then case and the lock in the final s.
  1109 //
  1110 // Note also that in all these cases the then/else pieces don't have
  1111 // to be trivial as long as they begin and end with synchronization
  1112 // operations.
  1113 //
  1114 //   s();
  1115 //   if (p)
  1116 //     s();
  1117 //     f();
  1118 //     s();
  1119 //   s();
  1120 //
  1121 // The code will work properly for this case, leaving in the unlock
  1122 // before the call to f and the relock after it.
  1123 //
  1124 // A potentially interesting case which isn't handled here is when the
  1125 // locking is partially redundant.
  1126 //
  1127 //   s();
  1128 //   if (p)
  1129 //     s();
  1130 //
  1131 // This could be eliminated putting unlocking on the else case and
  1132 // eliminating the first unlock and the lock in the then side.
  1133 // Alternatively the unlock could be moved out of the then side so it
  1134 // was after the merge and the first unlock and second lock
  1135 // eliminated.  This might require less manipulation of the memory
  1136 // state to get correct.
  1137 //
  1138 // Additionally we might allow work between a unlock and lock before
  1139 // giving up eliminating the locks.  The current code disallows any
  1140 // conditional control flow between these operations.  A formulation
  1141 // similar to partial redundancy elimination computing the
  1142 // availability of unlocking and the anticipatability of locking at a
  1143 // program point would allow detection of fully redundant locking with
  1144 // some amount of work in between.  I'm not sure how often I really
  1145 // think that would occur though.  Most of the cases I've seen
  1146 // indicate it's likely non-trivial work would occur in between.
  1147 // There may be other more complicated constructs where we could
  1148 // eliminate locking but I haven't seen any others appear as hot or
  1149 // interesting.
  1150 //
  1151 // Locking and unlocking have a canonical form in ideal that looks
  1152 // roughly like this:
  1153 //
  1154 //              <obj>
  1155 //                | \\------+
  1156 //                |  \       \
  1157 //                | BoxLock   \
  1158 //                |  |   |     \
  1159 //                |  |    \     \
  1160 //                |  |   FastLock
  1161 //                |  |   /
  1162 //                |  |  /
  1163 //                |  |  |
  1164 //
  1165 //               Lock
  1166 //                |
  1167 //            Proj #0
  1168 //                |
  1169 //            MembarAcquire
  1170 //                |
  1171 //            Proj #0
  1172 //
  1173 //            MembarRelease
  1174 //                |
  1175 //            Proj #0
  1176 //                |
  1177 //              Unlock
  1178 //                |
  1179 //            Proj #0
  1180 //
  1181 //
  1182 // This code proceeds by processing Lock nodes during PhaseIterGVN
  1183 // and searching back through its control for the proper code
  1184 // patterns.  Once it finds a set of lock and unlock operations to
  1185 // eliminate they are marked as eliminatable which causes the
  1186 // expansion of the Lock and Unlock macro nodes to make the operation a NOP
  1187 //
  1188 //=============================================================================
  1190 //
  1191 // Utility function to skip over uninteresting control nodes.  Nodes skipped are:
  1192 //   - copy regions.  (These may not have been optimized away yet.)
  1193 //   - eliminated locking nodes
  1194 //
  1195 static Node *next_control(Node *ctrl) {
  1196   if (ctrl == NULL)
  1197     return NULL;
  1198   while (1) {
  1199     if (ctrl->is_Region()) {
  1200       RegionNode *r = ctrl->as_Region();
  1201       Node *n = r->is_copy();
  1202       if (n == NULL)
  1203         break;  // hit a region, return it
  1204       else
  1205         ctrl = n;
  1206     } else if (ctrl->is_Proj()) {
  1207       Node *in0 = ctrl->in(0);
  1208       if (in0->is_AbstractLock() && in0->as_AbstractLock()->is_eliminated()) {
  1209         ctrl = in0->in(0);
  1210       } else {
  1211         break;
  1213     } else {
  1214       break; // found an interesting control
  1217   return ctrl;
  1219 //
  1220 // Given a control, see if it's the control projection of an Unlock which
  1221 // operating on the same object as lock.
  1222 //
  1223 bool AbstractLockNode::find_matching_unlock(const Node* ctrl, LockNode* lock,
  1224                                             GrowableArray<AbstractLockNode*> &lock_ops) {
  1225   ProjNode *ctrl_proj = (ctrl->is_Proj()) ? ctrl->as_Proj() : NULL;
  1226   if (ctrl_proj != NULL && ctrl_proj->_con == TypeFunc::Control) {
  1227     Node *n = ctrl_proj->in(0);
  1228     if (n != NULL && n->is_Unlock()) {
  1229       UnlockNode *unlock = n->as_Unlock();
  1230       if ((lock->obj_node() == unlock->obj_node()) &&
  1231           (lock->box_node() == unlock->box_node()) && !unlock->is_eliminated()) {
  1232         lock_ops.append(unlock);
  1233         return true;
  1237   return false;
  1240 //
  1241 // Find the lock matching an unlock.  Returns null if a safepoint
  1242 // or complicated control is encountered first.
  1243 LockNode *AbstractLockNode::find_matching_lock(UnlockNode* unlock) {
  1244   LockNode *lock_result = NULL;
  1245   // find the matching lock, or an intervening safepoint
  1246   Node *ctrl = next_control(unlock->in(0));
  1247   while (1) {
  1248     assert(ctrl != NULL, "invalid control graph");
  1249     assert(!ctrl->is_Start(), "missing lock for unlock");
  1250     if (ctrl->is_top()) break;  // dead control path
  1251     if (ctrl->is_Proj()) ctrl = ctrl->in(0);
  1252     if (ctrl->is_SafePoint()) {
  1253         break;  // found a safepoint (may be the lock we are searching for)
  1254     } else if (ctrl->is_Region()) {
  1255       // Check for a simple diamond pattern.  Punt on anything more complicated
  1256       if (ctrl->req() == 3 && ctrl->in(1) != NULL && ctrl->in(2) != NULL) {
  1257         Node *in1 = next_control(ctrl->in(1));
  1258         Node *in2 = next_control(ctrl->in(2));
  1259         if (((in1->is_IfTrue() && in2->is_IfFalse()) ||
  1260              (in2->is_IfTrue() && in1->is_IfFalse())) && (in1->in(0) == in2->in(0))) {
  1261           ctrl = next_control(in1->in(0)->in(0));
  1262         } else {
  1263           break;
  1265       } else {
  1266         break;
  1268     } else {
  1269       ctrl = next_control(ctrl->in(0));  // keep searching
  1272   if (ctrl->is_Lock()) {
  1273     LockNode *lock = ctrl->as_Lock();
  1274     if ((lock->obj_node() == unlock->obj_node()) &&
  1275             (lock->box_node() == unlock->box_node())) {
  1276       lock_result = lock;
  1279   return lock_result;
  1282 // This code corresponds to case 3 above.
  1284 bool AbstractLockNode::find_lock_and_unlock_through_if(Node* node, LockNode* lock,
  1285                                                        GrowableArray<AbstractLockNode*> &lock_ops) {
  1286   Node* if_node = node->in(0);
  1287   bool  if_true = node->is_IfTrue();
  1289   if (if_node->is_If() && if_node->outcnt() == 2 && (if_true || node->is_IfFalse())) {
  1290     Node *lock_ctrl = next_control(if_node->in(0));
  1291     if (find_matching_unlock(lock_ctrl, lock, lock_ops)) {
  1292       Node* lock1_node = NULL;
  1293       ProjNode* proj = if_node->as_If()->proj_out(!if_true);
  1294       if (if_true) {
  1295         if (proj->is_IfFalse() && proj->outcnt() == 1) {
  1296           lock1_node = proj->unique_out();
  1298       } else {
  1299         if (proj->is_IfTrue() && proj->outcnt() == 1) {
  1300           lock1_node = proj->unique_out();
  1303       if (lock1_node != NULL && lock1_node->is_Lock()) {
  1304         LockNode *lock1 = lock1_node->as_Lock();
  1305         if ((lock->obj_node() == lock1->obj_node()) &&
  1306             (lock->box_node() == lock1->box_node()) && !lock1->is_eliminated()) {
  1307           lock_ops.append(lock1);
  1308           return true;
  1314   lock_ops.trunc_to(0);
  1315   return false;
  1318 bool AbstractLockNode::find_unlocks_for_region(const RegionNode* region, LockNode* lock,
  1319                                GrowableArray<AbstractLockNode*> &lock_ops) {
  1320   // check each control merging at this point for a matching unlock.
  1321   // in(0) should be self edge so skip it.
  1322   for (int i = 1; i < (int)region->req(); i++) {
  1323     Node *in_node = next_control(region->in(i));
  1324     if (in_node != NULL) {
  1325       if (find_matching_unlock(in_node, lock, lock_ops)) {
  1326         // found a match so keep on checking.
  1327         continue;
  1328       } else if (find_lock_and_unlock_through_if(in_node, lock, lock_ops)) {
  1329         continue;
  1332       // If we fall through to here then it was some kind of node we
  1333       // don't understand or there wasn't a matching unlock, so give
  1334       // up trying to merge locks.
  1335       lock_ops.trunc_to(0);
  1336       return false;
  1339   return true;
  1343 #ifndef PRODUCT
  1344 //
  1345 // Create a counter which counts the number of times this lock is acquired
  1346 //
  1347 void AbstractLockNode::create_lock_counter(JVMState* state) {
  1348   _counter = OptoRuntime::new_named_counter(state, NamedCounter::LockCounter);
  1350 #endif
  1352 void AbstractLockNode::set_eliminated() {
  1353   _eliminate = true;
  1354 #ifndef PRODUCT
  1355   if (_counter) {
  1356     // Update the counter to indicate that this lock was eliminated.
  1357     // The counter update code will stay around even though the
  1358     // optimizer will eliminate the lock operation itself.
  1359     _counter->set_tag(NamedCounter::EliminatedLockCounter);
  1361 #endif
  1364 //=============================================================================
  1365 Node *LockNode::Ideal(PhaseGVN *phase, bool can_reshape) {
  1367   // perform any generic optimizations first
  1368   Node *result = SafePointNode::Ideal(phase, can_reshape);
  1370   // Now see if we can optimize away this lock.  We don't actually
  1371   // remove the locking here, we simply set the _eliminate flag which
  1372   // prevents macro expansion from expanding the lock.  Since we don't
  1373   // modify the graph, the value returned from this function is the
  1374   // one computed above.
  1375   if (EliminateLocks && !is_eliminated()) {
  1376     //
  1377     // Try lock coarsening
  1378     //
  1379     PhaseIterGVN* iter = phase->is_IterGVN();
  1380     if (iter != NULL) {
  1382       GrowableArray<AbstractLockNode*>   lock_ops;
  1384       Node *ctrl = next_control(in(0));
  1386       // now search back for a matching Unlock
  1387       if (find_matching_unlock(ctrl, this, lock_ops)) {
  1388         // found an unlock directly preceding this lock.  This is the
  1389         // case of single unlock directly control dependent on a
  1390         // single lock which is the trivial version of case 1 or 2.
  1391       } else if (ctrl->is_Region() ) {
  1392         if (find_unlocks_for_region(ctrl->as_Region(), this, lock_ops)) {
  1393         // found lock preceded by multiple unlocks along all paths
  1394         // joining at this point which is case 3 in description above.
  1396       } else {
  1397         // see if this lock comes from either half of an if and the
  1398         // predecessors merges unlocks and the other half of the if
  1399         // performs a lock.
  1400         if (find_lock_and_unlock_through_if(ctrl, this, lock_ops)) {
  1401           // found unlock splitting to an if with locks on both branches.
  1405       if (lock_ops.length() > 0) {
  1406         // add ourselves to the list of locks to be eliminated.
  1407         lock_ops.append(this);
  1409   #ifndef PRODUCT
  1410         if (PrintEliminateLocks) {
  1411           int locks = 0;
  1412           int unlocks = 0;
  1413           for (int i = 0; i < lock_ops.length(); i++) {
  1414             AbstractLockNode* lock = lock_ops.at(i);
  1415             if (lock->Opcode() == Op_Lock) locks++;
  1416             else                               unlocks++;
  1417             if (Verbose) {
  1418               lock->dump(1);
  1421           tty->print_cr("***Eliminated %d unlocks and %d locks", unlocks, locks);
  1423   #endif
  1425         // for each of the identified locks, mark them
  1426         // as eliminatable
  1427         for (int i = 0; i < lock_ops.length(); i++) {
  1428           AbstractLockNode* lock = lock_ops.at(i);
  1430           // Mark it eliminated to update any counters
  1431           lock->set_eliminated();
  1433       } else if (result != NULL && ctrl->is_Region() &&
  1434                  iter->_worklist.member(ctrl)) {
  1435         // We weren't able to find any opportunities but the region this
  1436         // lock is control dependent on hasn't been processed yet so put
  1437         // this lock back on the worklist so we can check again once any
  1438         // region simplification has occurred.
  1439         iter->_worklist.push(this);
  1444   return result;
  1447 //=============================================================================
  1448 uint UnlockNode::size_of() const { return sizeof(*this); }
  1450 //=============================================================================
  1451 Node *UnlockNode::Ideal(PhaseGVN *phase, bool can_reshape) {
  1453   // perform any generic optimizations first
  1454   Node * result = SafePointNode::Ideal(phase, can_reshape);
  1456   // Now see if we can optimize away this unlock.  We don't actually
  1457   // remove the unlocking here, we simply set the _eliminate flag which
  1458   // prevents macro expansion from expanding the unlock.  Since we don't
  1459   // modify the graph, the value returned from this function is the
  1460   // one computed above.
  1461   if (EliminateLocks && !is_eliminated()) {
  1462     //
  1463     // If we are unlocking an unescaped object, the lock/unlock is unnecessary
  1464     // We can eliminate them if there are no safepoints in the locked region.
  1465     //
  1466     ConnectionGraph *cgr = Compile::current()->congraph();
  1467     if (cgr != NULL && cgr->escape_state(obj_node(), phase) == PointsToNode::NoEscape) {
  1468       GrowableArray<AbstractLockNode*>   lock_ops;
  1469       LockNode *lock = find_matching_lock(this);
  1470       if (lock != NULL) {
  1471         lock_ops.append(this);
  1472         lock_ops.append(lock);
  1473         // find other unlocks which pair with the lock we found and add them
  1474         // to the list
  1475         Node * box = box_node();
  1477         for (DUIterator_Fast imax, i = box->fast_outs(imax); i < imax; i++) {
  1478           Node *use = box->fast_out(i);
  1479           if (use->is_Unlock() && use != this) {
  1480             UnlockNode *unlock1 = use->as_Unlock();
  1481             if (!unlock1->is_eliminated()) {
  1482               LockNode *lock1 = find_matching_lock(unlock1);
  1483               if (lock == lock1)
  1484                 lock_ops.append(unlock1);
  1485               else if (lock1 == NULL) {
  1486                // we can't find a matching lock, we must assume the worst
  1487                 lock_ops.trunc_to(0);
  1488                 break;
  1493         if (lock_ops.length() > 0) {
  1495   #ifndef PRODUCT
  1496           if (PrintEliminateLocks) {
  1497             int locks = 0;
  1498             int unlocks = 0;
  1499             for (int i = 0; i < lock_ops.length(); i++) {
  1500               AbstractLockNode* lock = lock_ops.at(i);
  1501               if (lock->Opcode() == Op_Lock) locks++;
  1502               else                               unlocks++;
  1503               if (Verbose) {
  1504                 lock->dump(1);
  1507             tty->print_cr("***Eliminated %d unescaped unlocks and %d unescaped locks", unlocks, locks);
  1509   #endif
  1511           // for each of the identified locks, mark them
  1512           // as eliminatable
  1513           for (int i = 0; i < lock_ops.length(); i++) {
  1514             AbstractLockNode* lock = lock_ops.at(i);
  1516             // Mark it eliminated to update any counters
  1517             lock->set_eliminated();
  1523   return result;

mercurial