1 /*
   2  * Copyright (c) 1997, 2019, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "compiler/compileLog.hpp"
  27 #include "ci/bcEscapeAnalyzer.hpp"
  28 #include "compiler/oopMap.hpp"
  29 #include "gc/shared/barrierSet.hpp"
  30 #include "gc/shared/c2/barrierSetC2.hpp"
  31 #include "interpreter/interpreter.hpp"
  32 #include "opto/callGenerator.hpp"
  33 #include "opto/callnode.hpp"
  34 #include "opto/castnode.hpp"
  35 #include "opto/convertnode.hpp"
  36 #include "opto/escape.hpp"
  37 #include "opto/locknode.hpp"
  38 #include "opto/machnode.hpp"
  39 #include "opto/matcher.hpp"
  40 #include "opto/parse.hpp"
  41 #include "opto/regalloc.hpp"
  42 #include "opto/regmask.hpp"
  43 #include "opto/rootnode.hpp"
  44 #include "opto/runtime.hpp"
  45 #include "opto/valuetypenode.hpp"
  46 #include "runtime/sharedRuntime.hpp"
  47 
  48 // Portions of code courtesy of Clifford Click
  49 
  50 // Optimization - Graph Style
  51 
  52 //=============================================================================
  53 uint StartNode::size_of() const { return sizeof(*this); }
  54 bool StartNode::cmp( const Node &n ) const
  55 { return _domain == ((StartNode&)n)._domain; }
  56 const Type *StartNode::bottom_type() const { return _domain; }
  57 const Type* StartNode::Value(PhaseGVN* phase) const { return _domain; }
  58 #ifndef PRODUCT
  59 void StartNode::dump_spec(outputStream *st) const { st->print(" #"); _domain->dump_on(st);}
  60 void StartNode::dump_compact_spec(outputStream *st) const { /* empty */ }
  61 #endif
  62 
  63 //------------------------------Ideal------------------------------------------
  64 Node *StartNode::Ideal(PhaseGVN *phase, bool can_reshape){
  65   return remove_dead_region(phase, can_reshape) ? this : NULL;
  66 }
  67 
  68 //------------------------------calling_convention-----------------------------
  69 void StartNode::calling_convention( BasicType* sig_bt, VMRegPair *parm_regs, uint argcnt ) const {
  70   Matcher::calling_convention( sig_bt, parm_regs, argcnt, false );
  71 }
  72 
  73 //------------------------------Registers--------------------------------------
  74 const RegMask &StartNode::in_RegMask(uint) const {
  75   return RegMask::Empty;
  76 }
  77 
  78 //------------------------------match------------------------------------------
  79 // Construct projections for incoming parameters, and their RegMask info
  80 Node *StartNode::match(const ProjNode *proj, const Matcher *match, const RegMask* mask) {
  81   switch (proj->_con) {
  82   case TypeFunc::Control:
  83   case TypeFunc::I_O:
  84   case TypeFunc::Memory:
  85     return new MachProjNode(this,proj->_con,RegMask::Empty,MachProjNode::unmatched_proj);
  86   case TypeFunc::FramePtr:
  87     return new MachProjNode(this,proj->_con,Matcher::c_frame_ptr_mask, Op_RegP);
  88   case TypeFunc::ReturnAdr:
  89     return new MachProjNode(this,proj->_con,match->_return_addr_mask,Op_RegP);
  90   case TypeFunc::Parms:
  91   default: {
  92       uint parm_num = proj->_con - TypeFunc::Parms;
  93       const Type *t = _domain->field_at(proj->_con);
  94       if (t->base() == Type::Half)  // 2nd half of Longs and Doubles
  95         return new ConNode(Type::TOP);
  96       uint ideal_reg = t->ideal_reg();
  97       RegMask &rm = match->_calling_convention_mask[parm_num];
  98       return new MachProjNode(this,proj->_con,rm,ideal_reg);
  99     }
 100   }
 101   return NULL;
 102 }
 103 
 104 //------------------------------StartOSRNode----------------------------------
 105 // The method start node for an on stack replacement adapter
 106 
 107 //------------------------------osr_domain-----------------------------
 108 const TypeTuple *StartOSRNode::osr_domain() {
 109   const Type **fields = TypeTuple::fields(2);
 110   fields[TypeFunc::Parms+0] = TypeRawPtr::BOTTOM;  // address of osr buffer
 111 
 112   return TypeTuple::make(TypeFunc::Parms+1, fields);
 113 }
 114 
 115 //=============================================================================
 116 const char * const ParmNode::names[TypeFunc::Parms+1] = {
 117   "Control", "I_O", "Memory", "FramePtr", "ReturnAdr", "Parms"
 118 };
 119 
 120 #ifndef PRODUCT
 121 void ParmNode::dump_spec(outputStream *st) const {
 122   if( _con < TypeFunc::Parms ) {
 123     st->print("%s", names[_con]);
 124   } else {
 125     st->print("Parm%d: ",_con-TypeFunc::Parms);
 126     // Verbose and WizardMode dump bottom_type for all nodes
 127     if( !Verbose && !WizardMode )   bottom_type()->dump_on(st);
 128   }
 129 }
 130 
 131 void ParmNode::dump_compact_spec(outputStream *st) const {
 132   if (_con < TypeFunc::Parms) {
 133     st->print("%s", names[_con]);
 134   } else {
 135     st->print("%d:", _con-TypeFunc::Parms);
 136     // unconditionally dump bottom_type
 137     bottom_type()->dump_on(st);
 138   }
 139 }
 140 
 141 // For a ParmNode, all immediate inputs and outputs are considered relevant
 142 // both in compact and standard representation.
 143 void ParmNode::related(GrowableArray<Node*> *in_rel, GrowableArray<Node*> *out_rel, bool compact) const {
 144   this->collect_nodes(in_rel, 1, false, false);
 145   this->collect_nodes(out_rel, -1, false, false);
 146 }
 147 #endif
 148 
 149 uint ParmNode::ideal_reg() const {
 150   switch( _con ) {
 151   case TypeFunc::Control  : // fall through
 152   case TypeFunc::I_O      : // fall through
 153   case TypeFunc::Memory   : return 0;
 154   case TypeFunc::FramePtr : // fall through
 155   case TypeFunc::ReturnAdr: return Op_RegP;
 156   default                 : assert( _con > TypeFunc::Parms, "" );
 157     // fall through
 158   case TypeFunc::Parms    : {
 159     // Type of argument being passed
 160     const Type *t = in(0)->as_Start()->_domain->field_at(_con);
 161     return t->ideal_reg();
 162   }
 163   }
 164   ShouldNotReachHere();
 165   return 0;
 166 }
 167 
 168 //=============================================================================
 169 ReturnNode::ReturnNode(uint edges, Node *cntrl, Node *i_o, Node *memory, Node *frameptr, Node *retadr ) : Node(edges) {
 170   init_req(TypeFunc::Control,cntrl);
 171   init_req(TypeFunc::I_O,i_o);
 172   init_req(TypeFunc::Memory,memory);
 173   init_req(TypeFunc::FramePtr,frameptr);
 174   init_req(TypeFunc::ReturnAdr,retadr);
 175 }
 176 
 177 Node *ReturnNode::Ideal(PhaseGVN *phase, bool can_reshape){
 178   return remove_dead_region(phase, can_reshape) ? this : NULL;
 179 }
 180 
 181 const Type* ReturnNode::Value(PhaseGVN* phase) const {
 182   return ( phase->type(in(TypeFunc::Control)) == Type::TOP)
 183     ? Type::TOP
 184     : Type::BOTTOM;
 185 }
 186 
 187 // Do we Match on this edge index or not?  No edges on return nodes
 188 uint ReturnNode::match_edge(uint idx) const {
 189   return 0;
 190 }
 191 
 192 
 193 #ifndef PRODUCT
 194 void ReturnNode::dump_req(outputStream *st) const {
 195   // Dump the required inputs, enclosed in '(' and ')'
 196   uint i;                       // Exit value of loop
 197   for (i = 0; i < req(); i++) {    // For all required inputs
 198     if (i == TypeFunc::Parms) st->print("returns");
 199     if (in(i)) st->print("%c%d ", Compile::current()->node_arena()->contains(in(i)) ? ' ' : 'o', in(i)->_idx);
 200     else st->print("_ ");
 201   }
 202 }
 203 #endif
 204 
 205 //=============================================================================
 206 RethrowNode::RethrowNode(
 207   Node* cntrl,
 208   Node* i_o,
 209   Node* memory,
 210   Node* frameptr,
 211   Node* ret_adr,
 212   Node* exception
 213 ) : Node(TypeFunc::Parms + 1) {
 214   init_req(TypeFunc::Control  , cntrl    );
 215   init_req(TypeFunc::I_O      , i_o      );
 216   init_req(TypeFunc::Memory   , memory   );
 217   init_req(TypeFunc::FramePtr , frameptr );
 218   init_req(TypeFunc::ReturnAdr, ret_adr);
 219   init_req(TypeFunc::Parms    , exception);
 220 }
 221 
 222 Node *RethrowNode::Ideal(PhaseGVN *phase, bool can_reshape){
 223   return remove_dead_region(phase, can_reshape) ? this : NULL;
 224 }
 225 
 226 const Type* RethrowNode::Value(PhaseGVN* phase) const {
 227   return (phase->type(in(TypeFunc::Control)) == Type::TOP)
 228     ? Type::TOP
 229     : Type::BOTTOM;
 230 }
 231 
 232 uint RethrowNode::match_edge(uint idx) const {
 233   return 0;
 234 }
 235 
 236 #ifndef PRODUCT
 237 void RethrowNode::dump_req(outputStream *st) const {
 238   // Dump the required inputs, enclosed in '(' and ')'
 239   uint i;                       // Exit value of loop
 240   for (i = 0; i < req(); i++) {    // For all required inputs
 241     if (i == TypeFunc::Parms) st->print("exception");
 242     if (in(i)) st->print("%c%d ", Compile::current()->node_arena()->contains(in(i)) ? ' ' : 'o', in(i)->_idx);
 243     else st->print("_ ");
 244   }
 245 }
 246 #endif
 247 
 248 //=============================================================================
 249 // Do we Match on this edge index or not?  Match only target address & method
 250 uint TailCallNode::match_edge(uint idx) const {
 251   return TypeFunc::Parms <= idx  &&  idx <= TypeFunc::Parms+1;
 252 }
 253 
 254 //=============================================================================
 255 // Do we Match on this edge index or not?  Match only target address & oop
 256 uint TailJumpNode::match_edge(uint idx) const {
 257   return TypeFunc::Parms <= idx  &&  idx <= TypeFunc::Parms+1;
 258 }
 259 
 260 //=============================================================================
 261 JVMState::JVMState(ciMethod* method, JVMState* caller) :
 262   _method(method) {
 263   assert(method != NULL, "must be valid call site");
 264   _bci = InvocationEntryBci;
 265   _reexecute = Reexecute_Undefined;
 266   debug_only(_bci = -99);  // random garbage value
 267   debug_only(_map = (SafePointNode*)-1);
 268   _caller = caller;
 269   _depth  = 1 + (caller == NULL ? 0 : caller->depth());
 270   _locoff = TypeFunc::Parms;
 271   _stkoff = _locoff + _method->max_locals();
 272   _monoff = _stkoff + _method->max_stack();
 273   _scloff = _monoff;
 274   _endoff = _monoff;
 275   _sp = 0;
 276 }
 277 JVMState::JVMState(int stack_size) :
 278   _method(NULL) {
 279   _bci = InvocationEntryBci;
 280   _reexecute = Reexecute_Undefined;
 281   debug_only(_map = (SafePointNode*)-1);
 282   _caller = NULL;
 283   _depth  = 1;
 284   _locoff = TypeFunc::Parms;
 285   _stkoff = _locoff;
 286   _monoff = _stkoff + stack_size;
 287   _scloff = _monoff;
 288   _endoff = _monoff;
 289   _sp = 0;
 290 }
 291 
 292 //--------------------------------of_depth-------------------------------------
 293 JVMState* JVMState::of_depth(int d) const {
 294   const JVMState* jvmp = this;
 295   assert(0 < d && (uint)d <= depth(), "oob");
 296   for (int skip = depth() - d; skip > 0; skip--) {
 297     jvmp = jvmp->caller();
 298   }
 299   assert(jvmp->depth() == (uint)d, "found the right one");
 300   return (JVMState*)jvmp;
 301 }
 302 
 303 //-----------------------------same_calls_as-----------------------------------
 304 bool JVMState::same_calls_as(const JVMState* that) const {
 305   if (this == that)                    return true;
 306   if (this->depth() != that->depth())  return false;
 307   const JVMState* p = this;
 308   const JVMState* q = that;
 309   for (;;) {
 310     if (p->_method != q->_method)    return false;
 311     if (p->_method == NULL)          return true;   // bci is irrelevant
 312     if (p->_bci    != q->_bci)       return false;
 313     if (p->_reexecute != q->_reexecute)  return false;
 314     p = p->caller();
 315     q = q->caller();
 316     if (p == q)                      return true;
 317     assert(p != NULL && q != NULL, "depth check ensures we don't run off end");
 318   }
 319 }
 320 
 321 //------------------------------debug_start------------------------------------
 322 uint JVMState::debug_start()  const {
 323   debug_only(JVMState* jvmroot = of_depth(1));
 324   assert(jvmroot->locoff() <= this->locoff(), "youngest JVMState must be last");
 325   return of_depth(1)->locoff();
 326 }
 327 
 328 //-------------------------------debug_end-------------------------------------
 329 uint JVMState::debug_end() const {
 330   debug_only(JVMState* jvmroot = of_depth(1));
 331   assert(jvmroot->endoff() <= this->endoff(), "youngest JVMState must be last");
 332   return endoff();
 333 }
 334 
 335 //------------------------------debug_depth------------------------------------
 336 uint JVMState::debug_depth() const {
 337   uint total = 0;
 338   for (const JVMState* jvmp = this; jvmp != NULL; jvmp = jvmp->caller()) {
 339     total += jvmp->debug_size();
 340   }
 341   return total;
 342 }
 343 
 344 #ifndef PRODUCT
 345 
 346 //------------------------------format_helper----------------------------------
 347 // Given an allocation (a Chaitin object) and a Node decide if the Node carries
 348 // any defined value or not.  If it does, print out the register or constant.
 349 static void format_helper( PhaseRegAlloc *regalloc, outputStream* st, Node *n, const char *msg, uint i, GrowableArray<SafePointScalarObjectNode*> *scobjs ) {
 350   if (n == NULL) { st->print(" NULL"); return; }
 351   if (n->is_SafePointScalarObject()) {
 352     // Scalar replacement.
 353     SafePointScalarObjectNode* spobj = n->as_SafePointScalarObject();
 354     scobjs->append_if_missing(spobj);
 355     int sco_n = scobjs->find(spobj);
 356     assert(sco_n >= 0, "");
 357     st->print(" %s%d]=#ScObj" INT32_FORMAT, msg, i, sco_n);
 358     return;
 359   }
 360   if (regalloc->node_regs_max_index() > 0 &&
 361       OptoReg::is_valid(regalloc->get_reg_first(n))) { // Check for undefined
 362     char buf[50];
 363     regalloc->dump_register(n,buf);
 364     st->print(" %s%d]=%s",msg,i,buf);
 365   } else {                      // No register, but might be constant
 366     const Type *t = n->bottom_type();
 367     switch (t->base()) {
 368     case Type::Int:
 369       st->print(" %s%d]=#" INT32_FORMAT,msg,i,t->is_int()->get_con());
 370       break;
 371     case Type::AnyPtr:
 372       assert( t == TypePtr::NULL_PTR || n->in_dump(), "" );
 373       st->print(" %s%d]=#NULL",msg,i);
 374       break;
 375     case Type::AryPtr:
 376     case Type::InstPtr:
 377       st->print(" %s%d]=#Ptr" INTPTR_FORMAT,msg,i,p2i(t->isa_oopptr()->const_oop()));
 378       break;
 379     case Type::KlassPtr:
 380       st->print(" %s%d]=#Ptr" INTPTR_FORMAT,msg,i,p2i(t->make_ptr()->isa_klassptr()->klass()));
 381       break;
 382     case Type::MetadataPtr:
 383       st->print(" %s%d]=#Ptr" INTPTR_FORMAT,msg,i,p2i(t->make_ptr()->isa_metadataptr()->metadata()));
 384       break;
 385     case Type::NarrowOop:
 386       st->print(" %s%d]=#Ptr" INTPTR_FORMAT,msg,i,p2i(t->make_ptr()->isa_oopptr()->const_oop()));
 387       break;
 388     case Type::RawPtr:
 389       st->print(" %s%d]=#Raw" INTPTR_FORMAT,msg,i,p2i(t->is_rawptr()));
 390       break;
 391     case Type::DoubleCon:
 392       st->print(" %s%d]=#%fD",msg,i,t->is_double_constant()->_d);
 393       break;
 394     case Type::FloatCon:
 395       st->print(" %s%d]=#%fF",msg,i,t->is_float_constant()->_f);
 396       break;
 397     case Type::Long:
 398       st->print(" %s%d]=#" INT64_FORMAT,msg,i,(int64_t)(t->is_long()->get_con()));
 399       break;
 400     case Type::Half:
 401     case Type::Top:
 402       st->print(" %s%d]=_",msg,i);
 403       break;
 404     default: ShouldNotReachHere();
 405     }
 406   }
 407 }
 408 
 409 //------------------------------format-----------------------------------------
 410 void JVMState::format(PhaseRegAlloc *regalloc, const Node *n, outputStream* st) const {
 411   st->print("        #");
 412   if (_method) {
 413     _method->print_short_name(st);
 414     st->print(" @ bci:%d ",_bci);
 415   } else {
 416     st->print_cr(" runtime stub ");
 417     return;
 418   }
 419   if (n->is_MachSafePoint()) {
 420     GrowableArray<SafePointScalarObjectNode*> scobjs;
 421     MachSafePointNode *mcall = n->as_MachSafePoint();
 422     uint i;
 423     // Print locals
 424     for (i = 0; i < (uint)loc_size(); i++)
 425       format_helper(regalloc, st, mcall->local(this, i), "L[", i, &scobjs);
 426     // Print stack
 427     for (i = 0; i < (uint)stk_size(); i++) {
 428       if ((uint)(_stkoff + i) >= mcall->len())
 429         st->print(" oob ");
 430       else
 431        format_helper(regalloc, st, mcall->stack(this, i), "STK[", i, &scobjs);
 432     }
 433     for (i = 0; (int)i < nof_monitors(); i++) {
 434       Node *box = mcall->monitor_box(this, i);
 435       Node *obj = mcall->monitor_obj(this, i);
 436       if (regalloc->node_regs_max_index() > 0 &&
 437           OptoReg::is_valid(regalloc->get_reg_first(box))) {
 438         box = BoxLockNode::box_node(box);
 439         format_helper(regalloc, st, box, "MON-BOX[", i, &scobjs);
 440       } else {
 441         OptoReg::Name box_reg = BoxLockNode::reg(box);
 442         st->print(" MON-BOX%d=%s+%d",
 443                    i,
 444                    OptoReg::regname(OptoReg::c_frame_pointer),
 445                    regalloc->reg2offset(box_reg));
 446       }
 447       const char* obj_msg = "MON-OBJ[";
 448       if (EliminateLocks) {
 449         if (BoxLockNode::box_node(box)->is_eliminated())
 450           obj_msg = "MON-OBJ(LOCK ELIMINATED)[";
 451       }
 452       format_helper(regalloc, st, obj, obj_msg, i, &scobjs);
 453     }
 454 
 455     for (i = 0; i < (uint)scobjs.length(); i++) {
 456       // Scalar replaced objects.
 457       st->cr();
 458       st->print("        # ScObj" INT32_FORMAT " ", i);
 459       SafePointScalarObjectNode* spobj = scobjs.at(i);
 460       ciKlass* cik = spobj->bottom_type()->is_oopptr()->klass();
 461       assert(cik->is_instance_klass() ||
 462              cik->is_array_klass(), "Not supported allocation.");
 463       ciInstanceKlass *iklass = NULL;
 464       if (cik->is_instance_klass()) {
 465         cik->print_name_on(st);
 466         iklass = cik->as_instance_klass();
 467       } else if (cik->is_type_array_klass()) {
 468         cik->as_array_klass()->base_element_type()->print_name_on(st);
 469         st->print("[%d]", spobj->n_fields());
 470       } else if (cik->is_obj_array_klass()) {
 471         ciKlass* cie = cik->as_obj_array_klass()->base_element_klass();
 472         if (cie->is_instance_klass()) {
 473           cie->print_name_on(st);
 474         } else if (cie->is_type_array_klass()) {
 475           cie->as_array_klass()->base_element_type()->print_name_on(st);
 476         } else {
 477           ShouldNotReachHere();
 478         }
 479         st->print("[%d]", spobj->n_fields());
 480         int ndim = cik->as_array_klass()->dimension() - 1;
 481         while (ndim-- > 0) {
 482           st->print("[]");
 483         }
 484       } else if (cik->is_value_array_klass()) {
 485         ciKlass* cie = cik->as_value_array_klass()->base_element_klass();
 486         cie->print_name_on(st);
 487         st->print("[%d]", spobj->n_fields());
 488         int ndim = cik->as_array_klass()->dimension() - 1;
 489         while (ndim-- > 0) {
 490           st->print("[]");
 491         }
 492       }
 493       st->print("={");
 494       uint nf = spobj->n_fields();
 495       if (nf > 0) {
 496         uint first_ind = spobj->first_index(mcall->jvms());
 497         Node* fld_node = mcall->in(first_ind);
 498         ciField* cifield;
 499         if (iklass != NULL) {
 500           st->print(" [");
 501           cifield = iklass->nonstatic_field_at(0);
 502           cifield->print_name_on(st);
 503           format_helper(regalloc, st, fld_node, ":", 0, &scobjs);
 504         } else {
 505           format_helper(regalloc, st, fld_node, "[", 0, &scobjs);
 506         }
 507         for (uint j = 1; j < nf; j++) {
 508           fld_node = mcall->in(first_ind+j);
 509           if (iklass != NULL) {
 510             st->print(", [");
 511             cifield = iklass->nonstatic_field_at(j);
 512             cifield->print_name_on(st);
 513             format_helper(regalloc, st, fld_node, ":", j, &scobjs);
 514           } else {
 515             format_helper(regalloc, st, fld_node, ", [", j, &scobjs);
 516           }
 517         }
 518       }
 519       st->print(" }");
 520     }
 521   }
 522   st->cr();
 523   if (caller() != NULL) caller()->format(regalloc, n, st);
 524 }
 525 
 526 
 527 void JVMState::dump_spec(outputStream *st) const {
 528   if (_method != NULL) {
 529     bool printed = false;
 530     if (!Verbose) {
 531       // The JVMS dumps make really, really long lines.
 532       // Take out the most boring parts, which are the package prefixes.
 533       char buf[500];
 534       stringStream namest(buf, sizeof(buf));
 535       _method->print_short_name(&namest);
 536       if (namest.count() < sizeof(buf)) {
 537         const char* name = namest.base();
 538         if (name[0] == ' ')  ++name;
 539         const char* endcn = strchr(name, ':');  // end of class name
 540         if (endcn == NULL)  endcn = strchr(name, '(');
 541         if (endcn == NULL)  endcn = name + strlen(name);
 542         while (endcn > name && endcn[-1] != '.' && endcn[-1] != '/')
 543           --endcn;
 544         st->print(" %s", endcn);
 545         printed = true;
 546       }
 547     }
 548     if (!printed)
 549       _method->print_short_name(st);
 550     st->print(" @ bci:%d",_bci);
 551     if(_reexecute == Reexecute_True)
 552       st->print(" reexecute");
 553   } else {
 554     st->print(" runtime stub");
 555   }
 556   if (caller() != NULL)  caller()->dump_spec(st);
 557 }
 558 
 559 
 560 void JVMState::dump_on(outputStream* st) const {
 561   bool print_map = _map && !((uintptr_t)_map & 1) &&
 562                   ((caller() == NULL) || (caller()->map() != _map));
 563   if (print_map) {
 564     if (_map->len() > _map->req()) {  // _map->has_exceptions()
 565       Node* ex = _map->in(_map->req());  // _map->next_exception()
 566       // skip the first one; it's already being printed
 567       while (ex != NULL && ex->len() > ex->req()) {
 568         ex = ex->in(ex->req());  // ex->next_exception()
 569         ex->dump(1);
 570       }
 571     }
 572     _map->dump(Verbose ? 2 : 1);
 573   }
 574   if (caller() != NULL) {
 575     caller()->dump_on(st);
 576   }
 577   st->print("JVMS depth=%d loc=%d stk=%d arg=%d mon=%d scalar=%d end=%d mondepth=%d sp=%d bci=%d reexecute=%s method=",
 578              depth(), locoff(), stkoff(), argoff(), monoff(), scloff(), endoff(), monitor_depth(), sp(), bci(), should_reexecute()?"true":"false");
 579   if (_method == NULL) {
 580     st->print_cr("(none)");
 581   } else {
 582     _method->print_name(st);
 583     st->cr();
 584     if (bci() >= 0 && bci() < _method->code_size()) {
 585       st->print("    bc: ");
 586       _method->print_codes_on(bci(), bci()+1, st);
 587     }
 588   }
 589 }
 590 
 591 // Extra way to dump a jvms from the debugger,
 592 // to avoid a bug with C++ member function calls.
 593 void dump_jvms(JVMState* jvms) {
 594   jvms->dump();
 595 }
 596 #endif
 597 
 598 //--------------------------clone_shallow--------------------------------------
 599 JVMState* JVMState::clone_shallow(Compile* C) const {
 600   JVMState* n = has_method() ? new (C) JVMState(_method, _caller) : new (C) JVMState(0);
 601   n->set_bci(_bci);
 602   n->_reexecute = _reexecute;
 603   n->set_locoff(_locoff);
 604   n->set_stkoff(_stkoff);
 605   n->set_monoff(_monoff);
 606   n->set_scloff(_scloff);
 607   n->set_endoff(_endoff);
 608   n->set_sp(_sp);
 609   n->set_map(_map);
 610   return n;
 611 }
 612 
 613 //---------------------------clone_deep----------------------------------------
 614 JVMState* JVMState::clone_deep(Compile* C) const {
 615   JVMState* n = clone_shallow(C);
 616   for (JVMState* p = n; p->_caller != NULL; p = p->_caller) {
 617     p->_caller = p->_caller->clone_shallow(C);
 618   }
 619   assert(n->depth() == depth(), "sanity");
 620   assert(n->debug_depth() == debug_depth(), "sanity");
 621   return n;
 622 }
 623 
 624 /**
 625  * Reset map for all callers
 626  */
 627 void JVMState::set_map_deep(SafePointNode* map) {
 628   for (JVMState* p = this; p->_caller != NULL; p = p->_caller) {
 629     p->set_map(map);
 630   }
 631 }
 632 
 633 // Adapt offsets in in-array after adding or removing an edge.
 634 // Prerequisite is that the JVMState is used by only one node.
 635 void JVMState::adapt_position(int delta) {
 636   for (JVMState* jvms = this; jvms != NULL; jvms = jvms->caller()) {
 637     jvms->set_locoff(jvms->locoff() + delta);
 638     jvms->set_stkoff(jvms->stkoff() + delta);
 639     jvms->set_monoff(jvms->monoff() + delta);
 640     jvms->set_scloff(jvms->scloff() + delta);
 641     jvms->set_endoff(jvms->endoff() + delta);
 642   }
 643 }
 644 
 645 // Mirror the stack size calculation in the deopt code
 646 // How much stack space would we need at this point in the program in
 647 // case of deoptimization?
 648 int JVMState::interpreter_frame_size() const {
 649   const JVMState* jvms = this;
 650   int size = 0;
 651   int callee_parameters = 0;
 652   int callee_locals = 0;
 653   int extra_args = method()->max_stack() - stk_size();
 654 
 655   while (jvms != NULL) {
 656     int locks = jvms->nof_monitors();
 657     int temps = jvms->stk_size();
 658     bool is_top_frame = (jvms == this);
 659     ciMethod* method = jvms->method();
 660 
 661     int frame_size = BytesPerWord * Interpreter::size_activation(method->max_stack(),
 662                                                                  temps + callee_parameters,
 663                                                                  extra_args,
 664                                                                  locks,
 665                                                                  callee_parameters,
 666                                                                  callee_locals,
 667                                                                  is_top_frame);
 668     size += frame_size;
 669 
 670     callee_parameters = method->size_of_parameters();
 671     callee_locals = method->max_locals();
 672     extra_args = 0;
 673     jvms = jvms->caller();
 674   }
 675   return size + Deoptimization::last_frame_adjust(0, callee_locals) * BytesPerWord;
 676 }
 677 
 678 //=============================================================================
 679 bool CallNode::cmp( const Node &n ) const
 680 { return _tf == ((CallNode&)n)._tf && _jvms == ((CallNode&)n)._jvms; }
 681 #ifndef PRODUCT
 682 void CallNode::dump_req(outputStream *st) const {
 683   // Dump the required inputs, enclosed in '(' and ')'
 684   uint i;                       // Exit value of loop
 685   for (i = 0; i < req(); i++) {    // For all required inputs
 686     if (i == TypeFunc::Parms) st->print("(");
 687     if (in(i)) st->print("%c%d ", Compile::current()->node_arena()->contains(in(i)) ? ' ' : 'o', in(i)->_idx);
 688     else st->print("_ ");
 689   }
 690   st->print(")");
 691 }
 692 
 693 void CallNode::dump_spec(outputStream *st) const {
 694   st->print(" ");
 695   if (tf() != NULL)  tf()->dump_on(st);
 696   if (_cnt != COUNT_UNKNOWN)  st->print(" C=%f",_cnt);
 697   if (jvms() != NULL)  jvms()->dump_spec(st);
 698 }
 699 #endif
 700 
 701 const Type *CallNode::bottom_type() const { return tf()->range_cc(); }
 702 const Type* CallNode::Value(PhaseGVN* phase) const {
 703   if (!in(0) || phase->type(in(0)) == Type::TOP) {
 704     return Type::TOP;
 705   }
 706   return tf()->range_cc();
 707 }
 708 
 709 //------------------------------calling_convention-----------------------------
 710 void CallNode::calling_convention(BasicType* sig_bt, VMRegPair *parm_regs, uint argcnt) const {
 711   if (_entry_point == StubRoutines::store_value_type_fields_to_buf()) {
 712     // The call to that stub is a special case: its inputs are
 713     // multiple values returned from a call and so it should follow
 714     // the return convention.
 715     SharedRuntime::java_return_convention(sig_bt, parm_regs, argcnt);
 716     return;
 717   }
 718   // Use the standard compiler calling convention
 719   Matcher::calling_convention( sig_bt, parm_regs, argcnt, true );
 720 }
 721 
 722 
 723 //------------------------------match------------------------------------------
 724 // Construct projections for control, I/O, memory-fields, ..., and
 725 // return result(s) along with their RegMask info
 726 Node *CallNode::match(const ProjNode *proj, const Matcher *match, const RegMask* mask) {
 727   uint con = proj->_con;
 728   const TypeTuple *range_cc = tf()->range_cc();
 729   if (con >= TypeFunc::Parms) {
 730     if (is_CallRuntime()) {
 731       if (con == TypeFunc::Parms) {
 732         uint ideal_reg = range_cc->field_at(TypeFunc::Parms)->ideal_reg();
 733         OptoRegPair regs = match->c_return_value(ideal_reg,true);
 734         RegMask rm = RegMask(regs.first());
 735         if (OptoReg::is_valid(regs.second())) {
 736           rm.Insert(regs.second());
 737         }
 738         return new MachProjNode(this,con,rm,ideal_reg);
 739       } else {
 740         assert(con == TypeFunc::Parms+1, "only one return value");
 741         assert(range_cc->field_at(TypeFunc::Parms+1) == Type::HALF, "");
 742         return new MachProjNode(this,con, RegMask::Empty, (uint)OptoReg::Bad);
 743       }
 744     } else {
 745       // The Call may return multiple values (value type fields): we
 746       // create one projection per returned values.
 747       assert(con <= TypeFunc::Parms+1 || ValueTypeReturnedAsFields, "only for multi value return");
 748       uint ideal_reg = range_cc->field_at(con)->ideal_reg();
 749       return new MachProjNode(this, con, mask[con-TypeFunc::Parms], ideal_reg);
 750     }
 751   }
 752 
 753   switch (con) {
 754   case TypeFunc::Control:
 755   case TypeFunc::I_O:
 756   case TypeFunc::Memory:
 757     return new MachProjNode(this,proj->_con,RegMask::Empty,MachProjNode::unmatched_proj);
 758 
 759   case TypeFunc::ReturnAdr:
 760   case TypeFunc::FramePtr:
 761   default:
 762     ShouldNotReachHere();
 763   }
 764   return NULL;
 765 }
 766 
 767 // Do we Match on this edge index or not?  Match no edges
 768 uint CallNode::match_edge(uint idx) const {
 769   return 0;
 770 }
 771 
 772 //
 773 // Determine whether the call could modify the field of the specified
 774 // instance at the specified offset.
 775 //
 776 bool CallNode::may_modify(const TypeOopPtr *t_oop, PhaseTransform *phase) {
 777   assert((t_oop != NULL), "sanity");
 778   if (is_call_to_arraycopystub() && strcmp(_name, "unsafe_arraycopy") != 0) {
 779     const TypeTuple* args = _tf->domain_sig();
 780     Node* dest = NULL;
 781     // Stubs that can be called once an ArrayCopyNode is expanded have
 782     // different signatures. Look for the second pointer argument,
 783     // that is the destination of the copy.
 784     for (uint i = TypeFunc::Parms, j = 0; i < args->cnt(); i++) {
 785       if (args->field_at(i)->isa_ptr()) {
 786         j++;
 787         if (j == 2) {
 788           dest = in(i);
 789           break;
 790         }
 791       }
 792     }
 793     guarantee(dest != NULL, "Call had only one ptr in, broken IR!");
 794     if (!dest->is_top() && may_modify_arraycopy_helper(phase->type(dest)->is_oopptr(), t_oop, phase)) {
 795       return true;
 796     }
 797     return false;
 798   }
 799   if (t_oop->is_known_instance()) {
 800     // The instance_id is set only for scalar-replaceable allocations which
 801     // are not passed as arguments according to Escape Analysis.
 802     return false;
 803   }
 804   if (t_oop->is_ptr_to_boxed_value()) {
 805     ciKlass* boxing_klass = t_oop->klass();
 806     if (is_CallStaticJava() && as_CallStaticJava()->is_boxing_method()) {
 807       // Skip unrelated boxing methods.
 808       Node* proj = proj_out_or_null(TypeFunc::Parms);
 809       if ((proj == NULL) || (phase->type(proj)->is_instptr()->klass() != boxing_klass)) {
 810         return false;
 811       }
 812     }
 813     if (is_CallJava() && as_CallJava()->method() != NULL) {
 814       ciMethod* meth = as_CallJava()->method();
 815       if (meth->is_getter()) {
 816         return false;
 817       }
 818       // May modify (by reflection) if an boxing object is passed
 819       // as argument or returned.
 820       Node* proj = returns_pointer() ? proj_out_or_null(TypeFunc::Parms) : NULL;
 821       if (proj != NULL) {
 822         const TypeInstPtr* inst_t = phase->type(proj)->isa_instptr();
 823         if ((inst_t != NULL) && (!inst_t->klass_is_exact() ||
 824                                  (inst_t->klass() == boxing_klass))) {
 825           return true;
 826         }
 827       }
 828       const TypeTuple* d = tf()->domain_cc();
 829       for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
 830         const TypeInstPtr* inst_t = d->field_at(i)->isa_instptr();
 831         if ((inst_t != NULL) && (!inst_t->klass_is_exact() ||
 832                                  (inst_t->klass() == boxing_klass))) {
 833           return true;
 834         }
 835       }
 836       return false;
 837     }
 838   }
 839   return true;
 840 }
 841 
 842 // Does this call have a direct reference to n other than debug information?
 843 bool CallNode::has_non_debug_use(Node *n) {
 844   const TypeTuple * d = tf()->domain_cc();
 845   for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
 846     Node *arg = in(i);
 847     if (arg == n) {
 848       return true;
 849     }
 850   }
 851   return false;
 852 }
 853 
 854 bool CallNode::has_debug_use(Node *n) {
 855   assert(jvms() != NULL, "jvms should not be null");
 856   for (uint i = jvms()->debug_start(); i < jvms()->debug_end(); i++) {
 857     Node *arg = in(i);
 858     if (arg == n) {
 859       return true;
 860     }
 861   }
 862   return false;
 863 }
 864 
 865 // Returns the unique CheckCastPP of a call
 866 // or 'this' if there are several CheckCastPP or unexpected uses
 867 // or returns NULL if there is no one.
 868 Node *CallNode::result_cast() {
 869   Node *cast = NULL;
 870 
 871   Node *p = proj_out_or_null(TypeFunc::Parms);
 872   if (p == NULL)
 873     return NULL;
 874 
 875   for (DUIterator_Fast imax, i = p->fast_outs(imax); i < imax; i++) {
 876     Node *use = p->fast_out(i);
 877     if (use->is_CheckCastPP()) {
 878       if (cast != NULL) {
 879         return this;  // more than 1 CheckCastPP
 880       }
 881       cast = use;
 882     } else if (!use->is_Initialize() &&
 883                !use->is_AddP() &&
 884                use->Opcode() != Op_MemBarStoreStore) {
 885       // Expected uses are restricted to a CheckCastPP, an Initialize
 886       // node, a MemBarStoreStore (clone) and AddP nodes. If we
 887       // encounter any other use (a Phi node can be seen in rare
 888       // cases) return this to prevent incorrect optimizations.
 889       return this;
 890     }
 891   }
 892   return cast;
 893 }
 894 
 895 
 896 CallProjections* CallNode::extract_projections(bool separate_io_proj, bool do_asserts) {
 897   uint max_res = TypeFunc::Parms-1;
 898   for (DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++) {
 899     ProjNode *pn = fast_out(i)->as_Proj();
 900     max_res = MAX2(max_res, pn->_con);
 901   }
 902 
 903   assert(max_res < _tf->range_cc()->cnt(), "result out of bounds");
 904 
 905   uint projs_size = sizeof(CallProjections);
 906   if (max_res > TypeFunc::Parms) {
 907     projs_size += (max_res-TypeFunc::Parms)*sizeof(Node*);
 908   }
 909   char* projs_storage = resource_allocate_bytes(projs_size);
 910   CallProjections* projs = new(projs_storage)CallProjections(max_res - TypeFunc::Parms + 1);
 911 
 912   for (DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++) {
 913     ProjNode *pn = fast_out(i)->as_Proj();
 914     if (pn->outcnt() == 0) continue;
 915     switch (pn->_con) {
 916     case TypeFunc::Control:
 917       {
 918         // For Control (fallthrough) and I_O (catch_all_index) we have CatchProj -> Catch -> Proj
 919         projs->fallthrough_proj = pn;
 920         DUIterator_Fast jmax, j = pn->fast_outs(jmax);
 921         const Node *cn = pn->fast_out(j);
 922         if (cn->is_Catch()) {
 923           ProjNode *cpn = NULL;
 924           for (DUIterator_Fast kmax, k = cn->fast_outs(kmax); k < kmax; k++) {
 925             cpn = cn->fast_out(k)->as_Proj();
 926             assert(cpn->is_CatchProj(), "must be a CatchProjNode");
 927             if (cpn->_con == CatchProjNode::fall_through_index)
 928               projs->fallthrough_catchproj = cpn;
 929             else {
 930               assert(cpn->_con == CatchProjNode::catch_all_index, "must be correct index.");
 931               projs->catchall_catchproj = cpn;
 932             }
 933           }
 934         }
 935         break;
 936       }
 937     case TypeFunc::I_O:
 938       if (pn->_is_io_use)
 939         projs->catchall_ioproj = pn;
 940       else
 941         projs->fallthrough_ioproj = pn;
 942       for (DUIterator j = pn->outs(); pn->has_out(j); j++) {
 943         Node* e = pn->out(j);
 944         if (e->Opcode() == Op_CreateEx && e->in(0)->is_CatchProj() && e->outcnt() > 0) {
 945           assert(projs->exobj == NULL, "only one");
 946           projs->exobj = e;
 947         }
 948       }
 949       break;
 950     case TypeFunc::Memory:
 951       if (pn->_is_io_use)
 952         projs->catchall_memproj = pn;
 953       else
 954         projs->fallthrough_memproj = pn;
 955       break;
 956     case TypeFunc::Parms:
 957       projs->resproj[0] = pn;
 958       break;
 959     default:
 960       assert(pn->_con <= max_res, "unexpected projection from allocation node.");
 961       projs->resproj[pn->_con-TypeFunc::Parms] = pn;
 962       break;
 963     }
 964   }
 965 
 966   // The resproj may not exist because the result could be ignored
 967   // and the exception object may not exist if an exception handler
 968   // swallows the exception but all the other must exist and be found.
 969   do_asserts = do_asserts && !Compile::current()->inlining_incrementally();
 970   assert(!do_asserts || projs->fallthrough_proj      != NULL, "must be found");
 971   assert(!do_asserts || projs->fallthrough_catchproj != NULL, "must be found");
 972   assert(!do_asserts || projs->fallthrough_memproj   != NULL, "must be found");
 973   assert(!do_asserts || projs->fallthrough_ioproj    != NULL, "must be found");
 974   assert(!do_asserts || projs->catchall_catchproj    != NULL, "must be found");
 975   if (separate_io_proj) {
 976     assert(!do_asserts || projs->catchall_memproj    != NULL, "must be found");
 977     assert(!do_asserts || projs->catchall_ioproj     != NULL, "must be found");
 978   }
 979   return projs;
 980 }
 981 
 982 Node *CallNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 983   CallGenerator* cg = generator();
 984   if (can_reshape && cg != NULL && cg->is_mh_late_inline() && !cg->already_attempted()) {
 985     // Check whether this MH handle call becomes a candidate for inlining
 986     ciMethod* callee = cg->method();
 987     vmIntrinsics::ID iid = callee->intrinsic_id();
 988     if (iid == vmIntrinsics::_invokeBasic) {
 989       if (in(TypeFunc::Parms)->Opcode() == Op_ConP) {
 990         phase->C->prepend_late_inline(cg);
 991         set_generator(NULL);
 992       }
 993     } else {
 994       assert(callee->has_member_arg(), "wrong type of call?");
 995       if (in(TypeFunc::Parms + callee->arg_size() - 1)->Opcode() == Op_ConP) {
 996         phase->C->prepend_late_inline(cg);
 997         set_generator(NULL);
 998       }
 999     }
1000   }
1001   return SafePointNode::Ideal(phase, can_reshape);
1002 }
1003 
1004 bool CallNode::is_call_to_arraycopystub() const {
1005   if (_name != NULL && strstr(_name, "arraycopy") != 0) {
1006     return true;
1007   }
1008   return false;
1009 }
1010 
1011 //=============================================================================
1012 uint CallJavaNode::size_of() const { return sizeof(*this); }
1013 bool CallJavaNode::cmp( const Node &n ) const {
1014   CallJavaNode &call = (CallJavaNode&)n;
1015   return CallNode::cmp(call) && _method == call._method &&
1016          _override_symbolic_info == call._override_symbolic_info;
1017 }
1018 
1019 void CallJavaNode::copy_call_debug_info(PhaseIterGVN* phase, CallNode *oldcall) {
1020   // Copy debug information and adjust JVMState information
1021   uint old_dbg_start = oldcall->tf()->domain_sig()->cnt();
1022   uint new_dbg_start = tf()->domain_sig()->cnt();
1023   int jvms_adj  = new_dbg_start - old_dbg_start;
1024   assert (new_dbg_start == req(), "argument count mismatch");
1025   Compile* C = phase->C;
1026   
1027   // SafePointScalarObject node could be referenced several times in debug info.
1028   // Use Dict to record cloned nodes.
1029   Dict* sosn_map = new Dict(cmpkey,hashkey);
1030   for (uint i = old_dbg_start; i < oldcall->req(); i++) {
1031     Node* old_in = oldcall->in(i);
1032     // Clone old SafePointScalarObjectNodes, adjusting their field contents.
1033     if (old_in != NULL && old_in->is_SafePointScalarObject()) {
1034       SafePointScalarObjectNode* old_sosn = old_in->as_SafePointScalarObject();
1035       uint old_unique = C->unique();
1036       Node* new_in = old_sosn->clone(sosn_map);
1037       if (old_unique != C->unique()) { // New node?
1038         new_in->set_req(0, C->root()); // reset control edge
1039         new_in = phase->transform(new_in); // Register new node.
1040       }
1041       old_in = new_in;
1042     }
1043     add_req(old_in);
1044   }
1045 
1046   // JVMS may be shared so clone it before we modify it
1047   set_jvms(oldcall->jvms() != NULL ? oldcall->jvms()->clone_deep(C) : NULL);
1048   for (JVMState *jvms = this->jvms(); jvms != NULL; jvms = jvms->caller()) {
1049     jvms->set_map(this);
1050     jvms->set_locoff(jvms->locoff()+jvms_adj);
1051     jvms->set_stkoff(jvms->stkoff()+jvms_adj);
1052     jvms->set_monoff(jvms->monoff()+jvms_adj);
1053     jvms->set_scloff(jvms->scloff()+jvms_adj);
1054     jvms->set_endoff(jvms->endoff()+jvms_adj);
1055   }
1056 }
1057 
1058 #ifdef ASSERT
1059 bool CallJavaNode::validate_symbolic_info() const {
1060   if (method() == NULL) {
1061     return true; // call into runtime or uncommon trap
1062   }
1063   Bytecodes::Code bc = jvms()->method()->java_code_at_bci(_bci);
1064   if (EnableValhalla && (bc == Bytecodes::_if_acmpeq || bc == Bytecodes::_if_acmpne)) {
1065     return true;
1066   }
1067   ciMethod* symbolic_info = jvms()->method()->get_method_at_bci(_bci);
1068   ciMethod* callee = method();
1069   if (symbolic_info->is_method_handle_intrinsic() && !callee->is_method_handle_intrinsic()) {
1070     assert(override_symbolic_info(), "should be set");
1071   }
1072   assert(ciMethod::is_consistent_info(symbolic_info, callee), "inconsistent info");
1073   return true;
1074 }
1075 #endif
1076 
1077 #ifndef PRODUCT
1078 void CallJavaNode::dump_spec(outputStream *st) const {
1079   if( _method ) _method->print_short_name(st);
1080   CallNode::dump_spec(st);
1081 }
1082 
1083 void CallJavaNode::dump_compact_spec(outputStream* st) const {
1084   if (_method) {
1085     _method->print_short_name(st);
1086   } else {
1087     st->print("<?>");
1088   }
1089 }
1090 #endif
1091 
1092 //=============================================================================
1093 uint CallStaticJavaNode::size_of() const { return sizeof(*this); }
1094 bool CallStaticJavaNode::cmp( const Node &n ) const {
1095   CallStaticJavaNode &call = (CallStaticJavaNode&)n;
1096   return CallJavaNode::cmp(call);
1097 }
1098 
1099 //----------------------------uncommon_trap_request----------------------------
1100 // If this is an uncommon trap, return the request code, else zero.
1101 int CallStaticJavaNode::uncommon_trap_request() const {
1102   if (_name != NULL && !strcmp(_name, "uncommon_trap")) {
1103     return extract_uncommon_trap_request(this);
1104   }
1105   return 0;
1106 }
1107 int CallStaticJavaNode::extract_uncommon_trap_request(const Node* call) {
1108 #ifndef PRODUCT
1109   if (!(call->req() > TypeFunc::Parms &&
1110         call->in(TypeFunc::Parms) != NULL &&
1111         call->in(TypeFunc::Parms)->is_Con() &&
1112         call->in(TypeFunc::Parms)->bottom_type()->isa_int())) {
1113     assert(in_dump() != 0, "OK if dumping");
1114     tty->print("[bad uncommon trap]");
1115     return 0;
1116   }
1117 #endif
1118   return call->in(TypeFunc::Parms)->bottom_type()->is_int()->get_con();
1119 }
1120 
1121 bool CallStaticJavaNode::remove_useless_allocation(PhaseGVN *phase, Node* ctl, Node* mem, Node* unc_arg) {
1122   // Split if can cause the flattened array branch of an array load to
1123   // end in an uncommon trap. In that case, the allocation of the
1124   // loaded value and its initialization is useless. Eliminate it. use
1125   // the jvm state of the allocation to create a new uncommon trap
1126   // call at the load.
1127   if (ctl == NULL || ctl->is_top() || mem == NULL || mem->is_top() || !mem->is_MergeMem()) {
1128     return false;
1129   }
1130   PhaseIterGVN* igvn = phase->is_IterGVN();
1131   if (ctl->is_Region()) {
1132     bool res = false;
1133     for (uint i = 1; i < ctl->req(); i++) {
1134       MergeMemNode* mm = mem->clone()->as_MergeMem();
1135       for (MergeMemStream mms(mm); mms.next_non_empty(); ) {
1136         Node* m = mms.memory();
1137         if (m->is_Phi() && m->in(0) == ctl) {
1138           mms.set_memory(m->in(i));
1139         }
1140       }
1141       if (remove_useless_allocation(phase, ctl->in(i), mm, unc_arg)) {
1142         res = true;
1143         if (!ctl->in(i)->is_Region()) {
1144           igvn->replace_input_of(ctl, i, phase->C->top());
1145         }
1146       }
1147       igvn->remove_dead_node(mm);
1148     }
1149     return res;
1150   }
1151   // verify the control flow is ok
1152   Node* c = ctl;
1153   Node* copy = NULL;
1154   Node* alloc = NULL;
1155   for (;;) {
1156     if (c == NULL || c->is_top()) {
1157       return false;
1158     }
1159     if (c->is_Proj() || c->is_Catch() || c->is_MemBar()) {
1160       c = c->in(0);
1161     } else if (c->Opcode() == Op_CallLeaf &&
1162                c->as_Call()->entry_point() == CAST_FROM_FN_PTR(address, OptoRuntime::load_unknown_value)) {
1163       copy = c;
1164       c = c->in(0);
1165     } else if (c->is_Allocate()) {
1166       Node* new_obj = c->as_Allocate()->result_cast();
1167       if (copy == NULL || new_obj == NULL) {
1168         return false;
1169       }
1170       Node* copy_dest = copy->in(TypeFunc::Parms + 2);
1171       if (copy_dest != new_obj) {
1172         return false;
1173       }
1174       alloc = c;
1175       break;
1176     } else {
1177       return false;
1178     }
1179   }
1180   
1181   JVMState* jvms = alloc->jvms();
1182   if (phase->C->too_many_traps(jvms->method(), jvms->bci(), Deoptimization::trap_request_reason(uncommon_trap_request()))) {
1183     return false;
1184   }
1185   
1186   Node* alloc_mem = alloc->in(TypeFunc::Memory);
1187   if (alloc_mem == NULL || alloc_mem->is_top()) {
1188     return false;
1189   }
1190   if (!alloc_mem->is_MergeMem()) {
1191     alloc_mem = MergeMemNode::make(alloc_mem);
1192   }
1193   
1194   // and that there's no unexpected side effect
1195   for (MergeMemStream mms2(mem->as_MergeMem(), alloc_mem->as_MergeMem()); mms2.next_non_empty2(); ) {
1196     Node* m1 = mms2.is_empty() ? mms2.base_memory() : mms2.memory();
1197     Node* m2 = mms2.memory2();
1198     
1199     for (uint i = 0; i < 100; i++) {
1200       if (m1 == m2) {
1201         break;
1202       } else if (m1->is_Proj()) {
1203         m1 = m1->in(0);
1204       } else if (m1->is_MemBar()) {
1205         m1 = m1->in(TypeFunc::Memory);
1206       } else if (m1->Opcode() == Op_CallLeaf &&
1207                  m1->as_Call()->entry_point() == CAST_FROM_FN_PTR(address, OptoRuntime::load_unknown_value)) {
1208         if (m1 != copy) {
1209           return false;
1210         }
1211         m1 = m1->in(TypeFunc::Memory);
1212       } else if (m1->is_Allocate()) {
1213         if (m1 != alloc) {
1214           return false;
1215         }
1216         break;
1217       } else {
1218         return false;
1219       }
1220     }
1221   }
1222   if (alloc_mem->outcnt() == 0) {
1223     igvn->remove_dead_node(alloc_mem);
1224   }
1225 
1226   address call_addr = SharedRuntime::uncommon_trap_blob()->entry_point();
1227   CallNode* unc = new CallStaticJavaNode(OptoRuntime::uncommon_trap_Type(), call_addr, "uncommon_trap",
1228                                          jvms->bci(), NULL);
1229   unc->init_req(TypeFunc::Control, alloc->in(0));
1230   unc->init_req(TypeFunc::I_O, alloc->in(TypeFunc::I_O));
1231   unc->init_req(TypeFunc::Memory, alloc->in(TypeFunc::Memory));
1232   unc->init_req(TypeFunc::FramePtr,  alloc->in(TypeFunc::FramePtr));
1233   unc->init_req(TypeFunc::ReturnAdr, alloc->in(TypeFunc::ReturnAdr));
1234   unc->init_req(TypeFunc::Parms+0, unc_arg);
1235   unc->set_cnt(PROB_UNLIKELY_MAG(4));
1236   unc->copy_call_debug_info(igvn, alloc->as_Allocate());
1237   
1238   igvn->replace_input_of(alloc, 0, phase->C->top());
1239   
1240   igvn->register_new_node_with_optimizer(unc);
1241   
1242   Node* ctrl = phase->transform(new ProjNode(unc, TypeFunc::Control));
1243   Node* halt = phase->transform(new HaltNode(ctrl, alloc->in(TypeFunc::FramePtr)));
1244   phase->C->root()->add_req(halt);
1245 
1246   return true;
1247 }
1248 
1249 
1250 Node* CallStaticJavaNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1251   if (can_reshape && uncommon_trap_request() != 0) {
1252     if (remove_useless_allocation(phase, in(0), in(TypeFunc::Memory), in(TypeFunc::Parms))) {
1253       if (!in(0)->is_Region()) {
1254         PhaseIterGVN* igvn = phase->is_IterGVN();
1255         igvn->replace_input_of(this, 0, phase->C->top());
1256       }
1257       return this;
1258     }
1259   }
1260   return CallNode::Ideal(phase, can_reshape);
1261 }
1262 
1263 
1264 #ifndef PRODUCT
1265 void CallStaticJavaNode::dump_spec(outputStream *st) const {
1266   st->print("# Static ");
1267   if (_name != NULL) {
1268     st->print("%s", _name);
1269     int trap_req = uncommon_trap_request();
1270     if (trap_req != 0) {
1271       char buf[100];
1272       st->print("(%s)",
1273                  Deoptimization::format_trap_request(buf, sizeof(buf),
1274                                                      trap_req));
1275     }
1276     st->print(" ");
1277   }
1278   CallJavaNode::dump_spec(st);
1279 }
1280 
1281 void CallStaticJavaNode::dump_compact_spec(outputStream* st) const {
1282   if (_method) {
1283     _method->print_short_name(st);
1284   } else if (_name) {
1285     st->print("%s", _name);
1286   } else {
1287     st->print("<?>");
1288   }
1289 }
1290 #endif
1291 
1292 //=============================================================================
1293 uint CallDynamicJavaNode::size_of() const { return sizeof(*this); }
1294 bool CallDynamicJavaNode::cmp( const Node &n ) const {
1295   CallDynamicJavaNode &call = (CallDynamicJavaNode&)n;
1296   return CallJavaNode::cmp(call);
1297 }
1298 #ifndef PRODUCT
1299 void CallDynamicJavaNode::dump_spec(outputStream *st) const {
1300   st->print("# Dynamic ");
1301   CallJavaNode::dump_spec(st);
1302 }
1303 #endif
1304 
1305 //=============================================================================
1306 uint CallRuntimeNode::size_of() const { return sizeof(*this); }
1307 bool CallRuntimeNode::cmp( const Node &n ) const {
1308   CallRuntimeNode &call = (CallRuntimeNode&)n;
1309   return CallNode::cmp(call) && !strcmp(_name,call._name);
1310 }
1311 #ifndef PRODUCT
1312 void CallRuntimeNode::dump_spec(outputStream *st) const {
1313   st->print("# ");
1314   st->print("%s", _name);
1315   CallNode::dump_spec(st);
1316 }
1317 #endif
1318 
1319 //------------------------------calling_convention-----------------------------
1320 void CallRuntimeNode::calling_convention( BasicType* sig_bt, VMRegPair *parm_regs, uint argcnt ) const {
1321   if (_entry_point == NULL) {
1322     // The call to that stub is a special case: its inputs are
1323     // multiple values returned from a call and so it should follow
1324     // the return convention.
1325     SharedRuntime::java_return_convention(sig_bt, parm_regs, argcnt);
1326     return;
1327   }
1328   Matcher::c_calling_convention( sig_bt, parm_regs, argcnt );
1329 }
1330 
1331 //=============================================================================
1332 //------------------------------calling_convention-----------------------------
1333 
1334 
1335 //=============================================================================
1336 #ifndef PRODUCT
1337 void CallLeafNode::dump_spec(outputStream *st) const {
1338   st->print("# ");
1339   st->print("%s", _name);
1340   CallNode::dump_spec(st);
1341 }
1342 #endif
1343 
1344 uint CallLeafNoFPNode::match_edge(uint idx) const {
1345   // Null entry point is a special case for which the target is in a
1346   // register. Need to match that edge.
1347   return entry_point() == NULL && idx == TypeFunc::Parms;
1348 }
1349 
1350 //=============================================================================
1351 
1352 void SafePointNode::set_local(JVMState* jvms, uint idx, Node *c) {
1353   assert(verify_jvms(jvms), "jvms must match");
1354   int loc = jvms->locoff() + idx;
1355   if (in(loc)->is_top() && idx > 0 && !c->is_top() ) {
1356     // If current local idx is top then local idx - 1 could
1357     // be a long/double that needs to be killed since top could
1358     // represent the 2nd half ofthe long/double.
1359     uint ideal = in(loc -1)->ideal_reg();
1360     if (ideal == Op_RegD || ideal == Op_RegL) {
1361       // set other (low index) half to top
1362       set_req(loc - 1, in(loc));
1363     }
1364   }
1365   set_req(loc, c);
1366 }
1367 
1368 uint SafePointNode::size_of() const { return sizeof(*this); }
1369 bool SafePointNode::cmp( const Node &n ) const {
1370   return (&n == this);          // Always fail except on self
1371 }
1372 
1373 //-------------------------set_next_exception----------------------------------
1374 void SafePointNode::set_next_exception(SafePointNode* n) {
1375   assert(n == NULL || n->Opcode() == Op_SafePoint, "correct value for next_exception");
1376   if (len() == req()) {
1377     if (n != NULL)  add_prec(n);
1378   } else {
1379     set_prec(req(), n);
1380   }
1381 }
1382 
1383 
1384 //----------------------------next_exception-----------------------------------
1385 SafePointNode* SafePointNode::next_exception() const {
1386   if (len() == req()) {
1387     return NULL;
1388   } else {
1389     Node* n = in(req());
1390     assert(n == NULL || n->Opcode() == Op_SafePoint, "no other uses of prec edges");
1391     return (SafePointNode*) n;
1392   }
1393 }
1394 
1395 
1396 //------------------------------Ideal------------------------------------------
1397 // Skip over any collapsed Regions
1398 Node *SafePointNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1399   return remove_dead_region(phase, can_reshape) ? this : NULL;
1400 }
1401 
1402 //------------------------------Identity---------------------------------------
1403 // Remove obviously duplicate safepoints
1404 Node* SafePointNode::Identity(PhaseGVN* phase) {
1405 
1406   // If you have back to back safepoints, remove one
1407   if( in(TypeFunc::Control)->is_SafePoint() )
1408     return in(TypeFunc::Control);
1409 
1410   if( in(0)->is_Proj() ) {
1411     Node *n0 = in(0)->in(0);
1412     // Check if he is a call projection (except Leaf Call)
1413     if( n0->is_Catch() ) {
1414       n0 = n0->in(0)->in(0);
1415       assert( n0->is_Call(), "expect a call here" );
1416     }
1417     if( n0->is_Call() && n0->as_Call()->guaranteed_safepoint() ) {
1418       // Don't remove a safepoint belonging to an OuterStripMinedLoopEndNode.
1419       // If the loop dies, they will be removed together.
1420       if (has_out_with(Op_OuterStripMinedLoopEnd)) {
1421         return this;
1422       }
1423       // Useless Safepoint, so remove it
1424       return in(TypeFunc::Control);
1425     }
1426   }
1427 
1428   return this;
1429 }
1430 
1431 //------------------------------Value------------------------------------------
1432 const Type* SafePointNode::Value(PhaseGVN* phase) const {
1433   if( phase->type(in(0)) == Type::TOP ) return Type::TOP;
1434   if( phase->eqv( in(0), this ) ) return Type::TOP; // Dead infinite loop
1435   return Type::CONTROL;
1436 }
1437 
1438 #ifndef PRODUCT
1439 void SafePointNode::dump_spec(outputStream *st) const {
1440   st->print(" SafePoint ");
1441   _replaced_nodes.dump(st);
1442 }
1443 
1444 // The related nodes of a SafepointNode are all data inputs, excluding the
1445 // control boundary, as well as all outputs till level 2 (to include projection
1446 // nodes and targets). In compact mode, just include inputs till level 1 and
1447 // outputs as before.
1448 void SafePointNode::related(GrowableArray<Node*> *in_rel, GrowableArray<Node*> *out_rel, bool compact) const {
1449   if (compact) {
1450     this->collect_nodes(in_rel, 1, false, false);
1451   } else {
1452     this->collect_nodes_in_all_data(in_rel, false);
1453   }
1454   this->collect_nodes(out_rel, -2, false, false);
1455 }
1456 #endif
1457 
1458 const RegMask &SafePointNode::in_RegMask(uint idx) const {
1459   if( idx < TypeFunc::Parms ) return RegMask::Empty;
1460   // Values outside the domain represent debug info
1461   return *(Compile::current()->matcher()->idealreg2debugmask[in(idx)->ideal_reg()]);
1462 }
1463 const RegMask &SafePointNode::out_RegMask() const {
1464   return RegMask::Empty;
1465 }
1466 
1467 
1468 void SafePointNode::grow_stack(JVMState* jvms, uint grow_by) {
1469   assert((int)grow_by > 0, "sanity");
1470   int monoff = jvms->monoff();
1471   int scloff = jvms->scloff();
1472   int endoff = jvms->endoff();
1473   assert(endoff == (int)req(), "no other states or debug info after me");
1474   Node* top = Compile::current()->top();
1475   for (uint i = 0; i < grow_by; i++) {
1476     ins_req(monoff, top);
1477   }
1478   jvms->set_monoff(monoff + grow_by);
1479   jvms->set_scloff(scloff + grow_by);
1480   jvms->set_endoff(endoff + grow_by);
1481 }
1482 
1483 void SafePointNode::push_monitor(const FastLockNode *lock) {
1484   // Add a LockNode, which points to both the original BoxLockNode (the
1485   // stack space for the monitor) and the Object being locked.
1486   const int MonitorEdges = 2;
1487   assert(JVMState::logMonitorEdges == exact_log2(MonitorEdges), "correct MonitorEdges");
1488   assert(req() == jvms()->endoff(), "correct sizing");
1489   int nextmon = jvms()->scloff();
1490   if (GenerateSynchronizationCode) {
1491     ins_req(nextmon,   lock->box_node());
1492     ins_req(nextmon+1, lock->obj_node());
1493   } else {
1494     Node* top = Compile::current()->top();
1495     ins_req(nextmon, top);
1496     ins_req(nextmon, top);
1497   }
1498   jvms()->set_scloff(nextmon + MonitorEdges);
1499   jvms()->set_endoff(req());
1500 }
1501 
1502 void SafePointNode::pop_monitor() {
1503   // Delete last monitor from debug info
1504   debug_only(int num_before_pop = jvms()->nof_monitors());
1505   const int MonitorEdges = 2;
1506   assert(JVMState::logMonitorEdges == exact_log2(MonitorEdges), "correct MonitorEdges");
1507   int scloff = jvms()->scloff();
1508   int endoff = jvms()->endoff();
1509   int new_scloff = scloff - MonitorEdges;
1510   int new_endoff = endoff - MonitorEdges;
1511   jvms()->set_scloff(new_scloff);
1512   jvms()->set_endoff(new_endoff);
1513   while (scloff > new_scloff)  del_req_ordered(--scloff);
1514   assert(jvms()->nof_monitors() == num_before_pop-1, "");
1515 }
1516 
1517 Node *SafePointNode::peek_monitor_box() const {
1518   int mon = jvms()->nof_monitors() - 1;
1519   assert(mon >= 0, "must have a monitor");
1520   return monitor_box(jvms(), mon);
1521 }
1522 
1523 Node *SafePointNode::peek_monitor_obj() const {
1524   int mon = jvms()->nof_monitors() - 1;
1525   assert(mon >= 0, "must have a monitor");
1526   return monitor_obj(jvms(), mon);
1527 }
1528 
1529 // Do we Match on this edge index or not?  Match no edges
1530 uint SafePointNode::match_edge(uint idx) const {
1531   if( !needs_polling_address_input() )
1532     return 0;
1533 
1534   return (TypeFunc::Parms == idx);
1535 }
1536 
1537 void SafePointNode::disconnect_from_root(PhaseIterGVN *igvn) {
1538   assert(Opcode() == Op_SafePoint, "only value for safepoint in loops");
1539   int nb = igvn->C->root()->find_prec_edge(this);
1540   if (nb != -1) {
1541     igvn->C->root()->rm_prec(nb);
1542   }
1543 }
1544 
1545 //==============  SafePointScalarObjectNode  ==============
1546 
1547 SafePointScalarObjectNode::SafePointScalarObjectNode(const TypeOopPtr* tp,
1548 #ifdef ASSERT
1549                                                      AllocateNode* alloc,
1550 #endif
1551                                                      uint first_index,
1552                                                      uint n_fields) :
1553   TypeNode(tp, 1), // 1 control input -- seems required.  Get from root.
1554   _first_index(first_index),
1555   _n_fields(n_fields)
1556 #ifdef ASSERT
1557   , _alloc(alloc)
1558 #endif
1559 {
1560   init_class_id(Class_SafePointScalarObject);
1561 }
1562 
1563 // Do not allow value-numbering for SafePointScalarObject node.
1564 uint SafePointScalarObjectNode::hash() const { return NO_HASH; }
1565 bool SafePointScalarObjectNode::cmp( const Node &n ) const {
1566   return (&n == this); // Always fail except on self
1567 }
1568 
1569 uint SafePointScalarObjectNode::ideal_reg() const {
1570   return 0; // No matching to machine instruction
1571 }
1572 
1573 const RegMask &SafePointScalarObjectNode::in_RegMask(uint idx) const {
1574   return *(Compile::current()->matcher()->idealreg2debugmask[in(idx)->ideal_reg()]);
1575 }
1576 
1577 const RegMask &SafePointScalarObjectNode::out_RegMask() const {
1578   return RegMask::Empty;
1579 }
1580 
1581 uint SafePointScalarObjectNode::match_edge(uint idx) const {
1582   return 0;
1583 }
1584 
1585 SafePointScalarObjectNode*
1586 SafePointScalarObjectNode::clone(Dict* sosn_map) const {
1587   void* cached = (*sosn_map)[(void*)this];
1588   if (cached != NULL) {
1589     return (SafePointScalarObjectNode*)cached;
1590   }
1591   SafePointScalarObjectNode* res = (SafePointScalarObjectNode*)Node::clone();
1592   sosn_map->Insert((void*)this, (void*)res);
1593   return res;
1594 }
1595 
1596 
1597 #ifndef PRODUCT
1598 void SafePointScalarObjectNode::dump_spec(outputStream *st) const {
1599   st->print(" # fields@[%d..%d]", first_index(),
1600              first_index() + n_fields() - 1);
1601 }
1602 
1603 #endif
1604 
1605 //=============================================================================
1606 uint AllocateNode::size_of() const { return sizeof(*this); }
1607 
1608 AllocateNode::AllocateNode(Compile* C, const TypeFunc *atype,
1609                            Node *ctrl, Node *mem, Node *abio,
1610                            Node *size, Node *klass_node,
1611                            Node* initial_test,
1612                            ValueTypeBaseNode* value_node)
1613   : CallNode(atype, NULL, TypeRawPtr::BOTTOM)
1614 {
1615   init_class_id(Class_Allocate);
1616   init_flags(Flag_is_macro);
1617   _is_scalar_replaceable = false;
1618   _is_non_escaping = false;
1619   _is_allocation_MemBar_redundant = false;
1620   _larval = false;
1621   Node *topnode = C->top();
1622 
1623   init_req( TypeFunc::Control  , ctrl );
1624   init_req( TypeFunc::I_O      , abio );
1625   init_req( TypeFunc::Memory   , mem );
1626   init_req( TypeFunc::ReturnAdr, topnode );
1627   init_req( TypeFunc::FramePtr , topnode );
1628   init_req( AllocSize          , size);
1629   init_req( KlassNode          , klass_node);
1630   init_req( InitialTest        , initial_test);
1631   init_req( ALength            , topnode);
1632   init_req( ValueNode          , value_node);
1633   // DefaultValue defaults to NULL
1634   // RawDefaultValue defaults to NULL
1635   // StorageProperties defaults to NULL
1636   C->add_macro_node(this);
1637 }
1638 
1639 void AllocateNode::compute_MemBar_redundancy(ciMethod* initializer)
1640 {
1641   assert(initializer != NULL &&
1642          initializer->is_object_constructor_or_class_initializer(),
1643          "unexpected initializer method");
1644   BCEscapeAnalyzer* analyzer = initializer->get_bcea();
1645   if (analyzer == NULL) {
1646     return;
1647   }
1648 
1649   // Allocation node is first parameter in its initializer
1650   if (analyzer->is_arg_stack(0) || analyzer->is_arg_local(0)) {
1651     _is_allocation_MemBar_redundant = true;
1652   }
1653 }
1654 
1655 Node* AllocateNode::Ideal(PhaseGVN* phase, bool can_reshape) {
1656   // Check for unused value type allocation
1657   if (can_reshape && in(AllocateNode::ValueNode) != NULL &&
1658       outcnt() != 0 && result_cast() == NULL) {
1659     // Remove allocation by replacing the projection nodes with its inputs
1660     InitializeNode* init = initialization();
1661     PhaseIterGVN* igvn = phase->is_IterGVN();
1662     CallProjections* projs = extract_projections(true, false);
1663     assert(projs->nb_resproj <= 1, "unexpected number of results");
1664     if (projs->fallthrough_catchproj != NULL) {
1665       igvn->replace_node(projs->fallthrough_catchproj, in(TypeFunc::Control));
1666     }
1667     if (projs->fallthrough_memproj != NULL) {
1668       igvn->replace_node(projs->fallthrough_memproj, in(TypeFunc::Memory));
1669     }
1670     if (projs->catchall_memproj != NULL) {
1671       igvn->replace_node(projs->catchall_memproj, phase->C->top());
1672     }
1673     if (projs->fallthrough_ioproj != NULL) {
1674       igvn->replace_node(projs->fallthrough_ioproj, in(TypeFunc::I_O));
1675     }
1676     if (projs->catchall_ioproj != NULL) {
1677       igvn->replace_node(projs->catchall_ioproj, phase->C->top());
1678     }
1679     if (projs->catchall_catchproj != NULL) {
1680       igvn->replace_node(projs->catchall_catchproj, phase->C->top());
1681     }
1682     if (projs->resproj[0] != NULL) {
1683       igvn->replace_node(projs->resproj[0], phase->C->top());
1684     }
1685     igvn->replace_node(this, phase->C->top());
1686     if (init != NULL) {
1687       Node* ctrl_proj = init->proj_out_or_null(TypeFunc::Control);
1688       Node* mem_proj = init->proj_out_or_null(TypeFunc::Memory);
1689       if (ctrl_proj != NULL) {
1690         igvn->replace_node(ctrl_proj, init->in(TypeFunc::Control));
1691       }
1692       if (mem_proj != NULL) {
1693         igvn->replace_node(mem_proj, init->in(TypeFunc::Memory));
1694       }
1695     }
1696     return NULL;
1697   }
1698 
1699   return CallNode::Ideal(phase, can_reshape);
1700 }
1701 
1702 Node *AllocateNode::make_ideal_mark(PhaseGVN *phase, Node* obj, Node* control, Node* mem, Node* klass_node) {
1703   Node* mark_node = NULL;
1704   // For now only enable fast locking for non-array types
1705   if ((EnableValhalla || UseBiasedLocking) && Opcode() == Op_Allocate) {
1706     if (klass_node == NULL) {
1707       Node* k_adr = phase->transform(new AddPNode(obj, obj, phase->MakeConX(oopDesc::klass_offset_in_bytes())));
1708       klass_node = phase->transform(LoadKlassNode::make(*phase, NULL, phase->C->immutable_memory(), k_adr, phase->type(k_adr)->is_ptr()));
1709     }
1710     Node* proto_adr = phase->transform(new AddPNode(klass_node, klass_node, phase->MakeConX(in_bytes(Klass::prototype_header_offset()))));
1711     mark_node = LoadNode::make(*phase, control, mem, proto_adr, TypeRawPtr::BOTTOM, TypeX_X, TypeX_X->basic_type(), MemNode::unordered);
1712   } else {
1713     mark_node = phase->MakeConX((jlong)markWord::prototype().value());
1714   }
1715   mark_node = phase->transform(mark_node);
1716   // Avoid returning a constant (old node) here because this method is used by LoadNode::Ideal
1717   return new OrXNode(mark_node, phase->MakeConX(_larval ? markWord::larval_state_pattern : 0));
1718 }
1719 
1720 
1721 //=============================================================================
1722 Node* AllocateArrayNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1723   Node* res = SafePointNode::Ideal(phase, can_reshape);
1724   if (res != NULL) {
1725     return res;
1726   }
1727   // Don't bother trying to transform a dead node
1728   if (in(0) && in(0)->is_top())  return NULL;
1729 
1730   const Type* type = phase->type(Ideal_length());
1731   if (type->isa_int() && type->is_int()->_hi < 0) {
1732     if (can_reshape) {
1733       PhaseIterGVN *igvn = phase->is_IterGVN();
1734       // Unreachable fall through path (negative array length),
1735       // the allocation can only throw so disconnect it.
1736       Node* proj = proj_out_or_null(TypeFunc::Control);
1737       Node* catchproj = NULL;
1738       if (proj != NULL) {
1739         for (DUIterator_Fast imax, i = proj->fast_outs(imax); i < imax; i++) {
1740           Node *cn = proj->fast_out(i);
1741           if (cn->is_Catch()) {
1742             catchproj = cn->as_Multi()->proj_out_or_null(CatchProjNode::fall_through_index);
1743             break;
1744           }
1745         }
1746       }
1747       if (catchproj != NULL && catchproj->outcnt() > 0 &&
1748           (catchproj->outcnt() > 1 ||
1749            catchproj->unique_out()->Opcode() != Op_Halt)) {
1750         assert(catchproj->is_CatchProj(), "must be a CatchProjNode");
1751         Node* nproj = catchproj->clone();
1752         igvn->register_new_node_with_optimizer(nproj);
1753 
1754         Node *frame = new ParmNode( phase->C->start(), TypeFunc::FramePtr );
1755         frame = phase->transform(frame);
1756         // Halt & Catch Fire
1757         Node *halt = new HaltNode( nproj, frame );
1758         phase->C->root()->add_req(halt);
1759         phase->transform(halt);
1760 
1761         igvn->replace_node(catchproj, phase->C->top());
1762         return this;
1763       }
1764     } else {
1765       // Can't correct it during regular GVN so register for IGVN
1766       phase->C->record_for_igvn(this);
1767     }
1768   }
1769   return NULL;
1770 }
1771 
1772 // Retrieve the length from the AllocateArrayNode. Narrow the type with a
1773 // CastII, if appropriate.  If we are not allowed to create new nodes, and
1774 // a CastII is appropriate, return NULL.
1775 Node *AllocateArrayNode::make_ideal_length(const TypeOopPtr* oop_type, PhaseTransform *phase, bool allow_new_nodes) {
1776   Node *length = in(AllocateNode::ALength);
1777   assert(length != NULL, "length is not null");
1778 
1779   const TypeInt* length_type = phase->find_int_type(length);
1780   const TypeAryPtr* ary_type = oop_type->isa_aryptr();
1781 
1782   if (ary_type != NULL && length_type != NULL) {
1783     const TypeInt* narrow_length_type = ary_type->narrow_size_type(length_type);
1784     if (narrow_length_type != length_type) {
1785       // Assert one of:
1786       //   - the narrow_length is 0
1787       //   - the narrow_length is not wider than length
1788       assert(narrow_length_type == TypeInt::ZERO ||
1789              length_type->is_con() && narrow_length_type->is_con() &&
1790                 (narrow_length_type->_hi <= length_type->_lo) ||
1791              (narrow_length_type->_hi <= length_type->_hi &&
1792               narrow_length_type->_lo >= length_type->_lo),
1793              "narrow type must be narrower than length type");
1794 
1795       // Return NULL if new nodes are not allowed
1796       if (!allow_new_nodes) return NULL;
1797       // Create a cast which is control dependent on the initialization to
1798       // propagate the fact that the array length must be positive.
1799       InitializeNode* init = initialization();
1800       assert(init != NULL, "initialization not found");
1801       length = new CastIINode(length, narrow_length_type);
1802       length->set_req(0, init->proj_out_or_null(0));
1803     }
1804   }
1805 
1806   return length;
1807 }
1808 
1809 //=============================================================================
1810 uint LockNode::size_of() const { return sizeof(*this); }
1811 
1812 // Redundant lock elimination
1813 //
1814 // There are various patterns of locking where we release and
1815 // immediately reacquire a lock in a piece of code where no operations
1816 // occur in between that would be observable.  In those cases we can
1817 // skip releasing and reacquiring the lock without violating any
1818 // fairness requirements.  Doing this around a loop could cause a lock
1819 // to be held for a very long time so we concentrate on non-looping
1820 // control flow.  We also require that the operations are fully
1821 // redundant meaning that we don't introduce new lock operations on
1822 // some paths so to be able to eliminate it on others ala PRE.  This
1823 // would probably require some more extensive graph manipulation to
1824 // guarantee that the memory edges were all handled correctly.
1825 //
1826 // Assuming p is a simple predicate which can't trap in any way and s
1827 // is a synchronized method consider this code:
1828 //
1829 //   s();
1830 //   if (p)
1831 //     s();
1832 //   else
1833 //     s();
1834 //   s();
1835 //
1836 // 1. The unlocks of the first call to s can be eliminated if the
1837 // locks inside the then and else branches are eliminated.
1838 //
1839 // 2. The unlocks of the then and else branches can be eliminated if
1840 // the lock of the final call to s is eliminated.
1841 //
1842 // Either of these cases subsumes the simple case of sequential control flow
1843 //
1844 // Addtionally we can eliminate versions without the else case:
1845 //
1846 //   s();
1847 //   if (p)
1848 //     s();
1849 //   s();
1850 //
1851 // 3. In this case we eliminate the unlock of the first s, the lock
1852 // and unlock in the then case and the lock in the final s.
1853 //
1854 // Note also that in all these cases the then/else pieces don't have
1855 // to be trivial as long as they begin and end with synchronization
1856 // operations.
1857 //
1858 //   s();
1859 //   if (p)
1860 //     s();
1861 //     f();
1862 //     s();
1863 //   s();
1864 //
1865 // The code will work properly for this case, leaving in the unlock
1866 // before the call to f and the relock after it.
1867 //
1868 // A potentially interesting case which isn't handled here is when the
1869 // locking is partially redundant.
1870 //
1871 //   s();
1872 //   if (p)
1873 //     s();
1874 //
1875 // This could be eliminated putting unlocking on the else case and
1876 // eliminating the first unlock and the lock in the then side.
1877 // Alternatively the unlock could be moved out of the then side so it
1878 // was after the merge and the first unlock and second lock
1879 // eliminated.  This might require less manipulation of the memory
1880 // state to get correct.
1881 //
1882 // Additionally we might allow work between a unlock and lock before
1883 // giving up eliminating the locks.  The current code disallows any
1884 // conditional control flow between these operations.  A formulation
1885 // similar to partial redundancy elimination computing the
1886 // availability of unlocking and the anticipatability of locking at a
1887 // program point would allow detection of fully redundant locking with
1888 // some amount of work in between.  I'm not sure how often I really
1889 // think that would occur though.  Most of the cases I've seen
1890 // indicate it's likely non-trivial work would occur in between.
1891 // There may be other more complicated constructs where we could
1892 // eliminate locking but I haven't seen any others appear as hot or
1893 // interesting.
1894 //
1895 // Locking and unlocking have a canonical form in ideal that looks
1896 // roughly like this:
1897 //
1898 //              <obj>
1899 //                | \\------+
1900 //                |  \       \
1901 //                | BoxLock   \
1902 //                |  |   |     \
1903 //                |  |    \     \
1904 //                |  |   FastLock
1905 //                |  |   /
1906 //                |  |  /
1907 //                |  |  |
1908 //
1909 //               Lock
1910 //                |
1911 //            Proj #0
1912 //                |
1913 //            MembarAcquire
1914 //                |
1915 //            Proj #0
1916 //
1917 //            MembarRelease
1918 //                |
1919 //            Proj #0
1920 //                |
1921 //              Unlock
1922 //                |
1923 //            Proj #0
1924 //
1925 //
1926 // This code proceeds by processing Lock nodes during PhaseIterGVN
1927 // and searching back through its control for the proper code
1928 // patterns.  Once it finds a set of lock and unlock operations to
1929 // eliminate they are marked as eliminatable which causes the
1930 // expansion of the Lock and Unlock macro nodes to make the operation a NOP
1931 //
1932 //=============================================================================
1933 
1934 //
1935 // Utility function to skip over uninteresting control nodes.  Nodes skipped are:
1936 //   - copy regions.  (These may not have been optimized away yet.)
1937 //   - eliminated locking nodes
1938 //
1939 static Node *next_control(Node *ctrl) {
1940   if (ctrl == NULL)
1941     return NULL;
1942   while (1) {
1943     if (ctrl->is_Region()) {
1944       RegionNode *r = ctrl->as_Region();
1945       Node *n = r->is_copy();
1946       if (n == NULL)
1947         break;  // hit a region, return it
1948       else
1949         ctrl = n;
1950     } else if (ctrl->is_Proj()) {
1951       Node *in0 = ctrl->in(0);
1952       if (in0->is_AbstractLock() && in0->as_AbstractLock()->is_eliminated()) {
1953         ctrl = in0->in(0);
1954       } else {
1955         break;
1956       }
1957     } else {
1958       break; // found an interesting control
1959     }
1960   }
1961   return ctrl;
1962 }
1963 //
1964 // Given a control, see if it's the control projection of an Unlock which
1965 // operating on the same object as lock.
1966 //
1967 bool AbstractLockNode::find_matching_unlock(const Node* ctrl, LockNode* lock,
1968                                             GrowableArray<AbstractLockNode*> &lock_ops) {
1969   ProjNode *ctrl_proj = (ctrl->is_Proj()) ? ctrl->as_Proj() : NULL;
1970   if (ctrl_proj != NULL && ctrl_proj->_con == TypeFunc::Control) {
1971     Node *n = ctrl_proj->in(0);
1972     if (n != NULL && n->is_Unlock()) {
1973       UnlockNode *unlock = n->as_Unlock();
1974       BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
1975       Node* lock_obj = bs->step_over_gc_barrier(lock->obj_node());
1976       Node* unlock_obj = bs->step_over_gc_barrier(unlock->obj_node());
1977       if (lock_obj->eqv_uncast(unlock_obj) &&
1978           BoxLockNode::same_slot(lock->box_node(), unlock->box_node()) &&
1979           !unlock->is_eliminated()) {
1980         lock_ops.append(unlock);
1981         return true;
1982       }
1983     }
1984   }
1985   return false;
1986 }
1987 
1988 //
1989 // Find the lock matching an unlock.  Returns null if a safepoint
1990 // or complicated control is encountered first.
1991 LockNode *AbstractLockNode::find_matching_lock(UnlockNode* unlock) {
1992   LockNode *lock_result = NULL;
1993   // find the matching lock, or an intervening safepoint
1994   Node *ctrl = next_control(unlock->in(0));
1995   while (1) {
1996     assert(ctrl != NULL, "invalid control graph");
1997     assert(!ctrl->is_Start(), "missing lock for unlock");
1998     if (ctrl->is_top()) break;  // dead control path
1999     if (ctrl->is_Proj()) ctrl = ctrl->in(0);
2000     if (ctrl->is_SafePoint()) {
2001         break;  // found a safepoint (may be the lock we are searching for)
2002     } else if (ctrl->is_Region()) {
2003       // Check for a simple diamond pattern.  Punt on anything more complicated
2004       if (ctrl->req() == 3 && ctrl->in(1) != NULL && ctrl->in(2) != NULL) {
2005         Node *in1 = next_control(ctrl->in(1));
2006         Node *in2 = next_control(ctrl->in(2));
2007         if (((in1->is_IfTrue() && in2->is_IfFalse()) ||
2008              (in2->is_IfTrue() && in1->is_IfFalse())) && (in1->in(0) == in2->in(0))) {
2009           ctrl = next_control(in1->in(0)->in(0));
2010         } else {
2011           break;
2012         }
2013       } else {
2014         break;
2015       }
2016     } else {
2017       ctrl = next_control(ctrl->in(0));  // keep searching
2018     }
2019   }
2020   if (ctrl->is_Lock()) {
2021     LockNode *lock = ctrl->as_Lock();
2022     BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
2023     Node* lock_obj = bs->step_over_gc_barrier(lock->obj_node());
2024     Node* unlock_obj = bs->step_over_gc_barrier(unlock->obj_node());
2025     if (lock_obj->eqv_uncast(unlock_obj) &&
2026         BoxLockNode::same_slot(lock->box_node(), unlock->box_node())) {
2027       lock_result = lock;
2028     }
2029   }
2030   return lock_result;
2031 }
2032 
2033 // This code corresponds to case 3 above.
2034 
2035 bool AbstractLockNode::find_lock_and_unlock_through_if(Node* node, LockNode* lock,
2036                                                        GrowableArray<AbstractLockNode*> &lock_ops) {
2037   Node* if_node = node->in(0);
2038   bool  if_true = node->is_IfTrue();
2039 
2040   if (if_node->is_If() && if_node->outcnt() == 2 && (if_true || node->is_IfFalse())) {
2041     Node *lock_ctrl = next_control(if_node->in(0));
2042     if (find_matching_unlock(lock_ctrl, lock, lock_ops)) {
2043       Node* lock1_node = NULL;
2044       ProjNode* proj = if_node->as_If()->proj_out(!if_true);
2045       if (if_true) {
2046         if (proj->is_IfFalse() && proj->outcnt() == 1) {
2047           lock1_node = proj->unique_out();
2048         }
2049       } else {
2050         if (proj->is_IfTrue() && proj->outcnt() == 1) {
2051           lock1_node = proj->unique_out();
2052         }
2053       }
2054       if (lock1_node != NULL && lock1_node->is_Lock()) {
2055         LockNode *lock1 = lock1_node->as_Lock();
2056         BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
2057         Node* lock_obj = bs->step_over_gc_barrier(lock->obj_node());
2058         Node* lock1_obj = bs->step_over_gc_barrier(lock1->obj_node());
2059         if (lock_obj->eqv_uncast(lock1_obj) &&
2060             BoxLockNode::same_slot(lock->box_node(), lock1->box_node()) &&
2061             !lock1->is_eliminated()) {
2062           lock_ops.append(lock1);
2063           return true;
2064         }
2065       }
2066     }
2067   }
2068 
2069   lock_ops.trunc_to(0);
2070   return false;
2071 }
2072 
2073 bool AbstractLockNode::find_unlocks_for_region(const RegionNode* region, LockNode* lock,
2074                                GrowableArray<AbstractLockNode*> &lock_ops) {
2075   // check each control merging at this point for a matching unlock.
2076   // in(0) should be self edge so skip it.
2077   for (int i = 1; i < (int)region->req(); i++) {
2078     Node *in_node = next_control(region->in(i));
2079     if (in_node != NULL) {
2080       if (find_matching_unlock(in_node, lock, lock_ops)) {
2081         // found a match so keep on checking.
2082         continue;
2083       } else if (find_lock_and_unlock_through_if(in_node, lock, lock_ops)) {
2084         continue;
2085       }
2086 
2087       // If we fall through to here then it was some kind of node we
2088       // don't understand or there wasn't a matching unlock, so give
2089       // up trying to merge locks.
2090       lock_ops.trunc_to(0);
2091       return false;
2092     }
2093   }
2094   return true;
2095 
2096 }
2097 
2098 #ifndef PRODUCT
2099 //
2100 // Create a counter which counts the number of times this lock is acquired
2101 //
2102 void AbstractLockNode::create_lock_counter(JVMState* state) {
2103   _counter = OptoRuntime::new_named_counter(state, NamedCounter::LockCounter);
2104 }
2105 
2106 void AbstractLockNode::set_eliminated_lock_counter() {
2107   if (_counter) {
2108     // Update the counter to indicate that this lock was eliminated.
2109     // The counter update code will stay around even though the
2110     // optimizer will eliminate the lock operation itself.
2111     _counter->set_tag(NamedCounter::EliminatedLockCounter);
2112   }
2113 }
2114 
2115 const char* AbstractLockNode::_kind_names[] = {"Regular", "NonEscObj", "Coarsened", "Nested"};
2116 
2117 void AbstractLockNode::dump_spec(outputStream* st) const {
2118   st->print("%s ", _kind_names[_kind]);
2119   CallNode::dump_spec(st);
2120 }
2121 
2122 void AbstractLockNode::dump_compact_spec(outputStream* st) const {
2123   st->print("%s", _kind_names[_kind]);
2124 }
2125 
2126 // The related set of lock nodes includes the control boundary.
2127 void AbstractLockNode::related(GrowableArray<Node*> *in_rel, GrowableArray<Node*> *out_rel, bool compact) const {
2128   if (compact) {
2129       this->collect_nodes(in_rel, 1, false, false);
2130     } else {
2131       this->collect_nodes_in_all_data(in_rel, true);
2132     }
2133     this->collect_nodes(out_rel, -2, false, false);
2134 }
2135 #endif
2136 
2137 //=============================================================================
2138 Node *LockNode::Ideal(PhaseGVN *phase, bool can_reshape) {
2139 
2140   // perform any generic optimizations first (returns 'this' or NULL)
2141   Node *result = SafePointNode::Ideal(phase, can_reshape);
2142   if (result != NULL)  return result;
2143   // Don't bother trying to transform a dead node
2144   if (in(0) && in(0)->is_top())  return NULL;
2145 
2146   // Now see if we can optimize away this lock.  We don't actually
2147   // remove the locking here, we simply set the _eliminate flag which
2148   // prevents macro expansion from expanding the lock.  Since we don't
2149   // modify the graph, the value returned from this function is the
2150   // one computed above.
2151   const Type* obj_type = phase->type(obj_node());
2152   if (can_reshape && EliminateLocks && !is_non_esc_obj() &&
2153       !obj_type->isa_valuetype() && !obj_type->is_valuetypeptr()) {
2154     //
2155     // If we are locking an unescaped object, the lock/unlock is unnecessary
2156     //
2157     ConnectionGraph *cgr = phase->C->congraph();
2158     if (cgr != NULL && cgr->not_global_escape(obj_node())) {
2159       assert(!is_eliminated() || is_coarsened(), "sanity");
2160       // The lock could be marked eliminated by lock coarsening
2161       // code during first IGVN before EA. Replace coarsened flag
2162       // to eliminate all associated locks/unlocks.
2163 #ifdef ASSERT
2164       this->log_lock_optimization(phase->C,"eliminate_lock_set_non_esc1");
2165 #endif
2166       this->set_non_esc_obj();
2167       return result;
2168     }
2169 
2170     //
2171     // Try lock coarsening
2172     //
2173     PhaseIterGVN* iter = phase->is_IterGVN();
2174     if (iter != NULL && !is_eliminated()) {
2175 
2176       GrowableArray<AbstractLockNode*>   lock_ops;
2177 
2178       Node *ctrl = next_control(in(0));
2179 
2180       // now search back for a matching Unlock
2181       if (find_matching_unlock(ctrl, this, lock_ops)) {
2182         // found an unlock directly preceding this lock.  This is the
2183         // case of single unlock directly control dependent on a
2184         // single lock which is the trivial version of case 1 or 2.
2185       } else if (ctrl->is_Region() ) {
2186         if (find_unlocks_for_region(ctrl->as_Region(), this, lock_ops)) {
2187         // found lock preceded by multiple unlocks along all paths
2188         // joining at this point which is case 3 in description above.
2189         }
2190       } else {
2191         // see if this lock comes from either half of an if and the
2192         // predecessors merges unlocks and the other half of the if
2193         // performs a lock.
2194         if (find_lock_and_unlock_through_if(ctrl, this, lock_ops)) {
2195           // found unlock splitting to an if with locks on both branches.
2196         }
2197       }
2198 
2199       if (lock_ops.length() > 0) {
2200         // add ourselves to the list of locks to be eliminated.
2201         lock_ops.append(this);
2202 
2203   #ifndef PRODUCT
2204         if (PrintEliminateLocks) {
2205           int locks = 0;
2206           int unlocks = 0;
2207           for (int i = 0; i < lock_ops.length(); i++) {
2208             AbstractLockNode* lock = lock_ops.at(i);
2209             if (lock->Opcode() == Op_Lock)
2210               locks++;
2211             else
2212               unlocks++;
2213             if (Verbose) {
2214               lock->dump(1);
2215             }
2216           }
2217           tty->print_cr("***Eliminated %d unlocks and %d locks", unlocks, locks);
2218         }
2219   #endif
2220 
2221         // for each of the identified locks, mark them
2222         // as eliminatable
2223         for (int i = 0; i < lock_ops.length(); i++) {
2224           AbstractLockNode* lock = lock_ops.at(i);
2225 
2226           // Mark it eliminated by coarsening and update any counters
2227 #ifdef ASSERT
2228           lock->log_lock_optimization(phase->C, "eliminate_lock_set_coarsened");
2229 #endif
2230           lock->set_coarsened();
2231         }
2232       } else if (ctrl->is_Region() &&
2233                  iter->_worklist.member(ctrl)) {
2234         // We weren't able to find any opportunities but the region this
2235         // lock is control dependent on hasn't been processed yet so put
2236         // this lock back on the worklist so we can check again once any
2237         // region simplification has occurred.
2238         iter->_worklist.push(this);
2239       }
2240     }
2241   }
2242 
2243   return result;
2244 }
2245 
2246 //=============================================================================
2247 bool LockNode::is_nested_lock_region() {
2248   return is_nested_lock_region(NULL);
2249 }
2250 
2251 // p is used for access to compilation log; no logging if NULL
2252 bool LockNode::is_nested_lock_region(Compile * c) {
2253   BoxLockNode* box = box_node()->as_BoxLock();
2254   int stk_slot = box->stack_slot();
2255   if (stk_slot <= 0) {
2256 #ifdef ASSERT
2257     this->log_lock_optimization(c, "eliminate_lock_INLR_1");
2258 #endif
2259     return false; // External lock or it is not Box (Phi node).
2260   }
2261 
2262   // Ignore complex cases: merged locks or multiple locks.
2263   Node* obj = obj_node();
2264   LockNode* unique_lock = NULL;
2265   if (!box->is_simple_lock_region(&unique_lock, obj)) {
2266 #ifdef ASSERT
2267     this->log_lock_optimization(c, "eliminate_lock_INLR_2a");
2268 #endif
2269     return false;
2270   }
2271   if (unique_lock != this) {
2272 #ifdef ASSERT
2273     this->log_lock_optimization(c, "eliminate_lock_INLR_2b");
2274 #endif
2275     return false;
2276   }
2277 
2278   BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
2279   obj = bs->step_over_gc_barrier(obj);
2280   // Look for external lock for the same object.
2281   SafePointNode* sfn = this->as_SafePoint();
2282   JVMState* youngest_jvms = sfn->jvms();
2283   int max_depth = youngest_jvms->depth();
2284   for (int depth = 1; depth <= max_depth; depth++) {
2285     JVMState* jvms = youngest_jvms->of_depth(depth);
2286     int num_mon  = jvms->nof_monitors();
2287     // Loop over monitors
2288     for (int idx = 0; idx < num_mon; idx++) {
2289       Node* obj_node = sfn->monitor_obj(jvms, idx);
2290       obj_node = bs->step_over_gc_barrier(obj_node);
2291       BoxLockNode* box_node = sfn->monitor_box(jvms, idx)->as_BoxLock();
2292       if ((box_node->stack_slot() < stk_slot) && obj_node->eqv_uncast(obj)) {
2293         return true;
2294       }
2295     }
2296   }
2297 #ifdef ASSERT
2298   this->log_lock_optimization(c, "eliminate_lock_INLR_3");
2299 #endif
2300   return false;
2301 }
2302 
2303 //=============================================================================
2304 uint UnlockNode::size_of() const { return sizeof(*this); }
2305 
2306 //=============================================================================
2307 Node *UnlockNode::Ideal(PhaseGVN *phase, bool can_reshape) {
2308 
2309   // perform any generic optimizations first (returns 'this' or NULL)
2310   Node *result = SafePointNode::Ideal(phase, can_reshape);
2311   if (result != NULL)  return result;
2312   // Don't bother trying to transform a dead node
2313   if (in(0) && in(0)->is_top())  return NULL;
2314 
2315   // Now see if we can optimize away this unlock.  We don't actually
2316   // remove the unlocking here, we simply set the _eliminate flag which
2317   // prevents macro expansion from expanding the unlock.  Since we don't
2318   // modify the graph, the value returned from this function is the
2319   // one computed above.
2320   // Escape state is defined after Parse phase.
2321   const Type* obj_type = phase->type(obj_node());
2322   if (can_reshape && EliminateLocks && !is_non_esc_obj() &&
2323       !obj_type->isa_valuetype() && !obj_type->is_valuetypeptr()) {
2324     //
2325     // If we are unlocking an unescaped object, the lock/unlock is unnecessary.
2326     //
2327     ConnectionGraph *cgr = phase->C->congraph();
2328     if (cgr != NULL && cgr->not_global_escape(obj_node())) {
2329       assert(!is_eliminated() || is_coarsened(), "sanity");
2330       // The lock could be marked eliminated by lock coarsening
2331       // code during first IGVN before EA. Replace coarsened flag
2332       // to eliminate all associated locks/unlocks.
2333 #ifdef ASSERT
2334       this->log_lock_optimization(phase->C, "eliminate_lock_set_non_esc2");
2335 #endif
2336       this->set_non_esc_obj();
2337     }
2338   }
2339   return result;
2340 }
2341 
2342 const char * AbstractLockNode::kind_as_string() const {
2343   return is_coarsened()   ? "coarsened" :
2344          is_nested()      ? "nested" :
2345          is_non_esc_obj() ? "non_escaping" :
2346          "?";
2347 }
2348 
2349 void AbstractLockNode::log_lock_optimization(Compile *C, const char * tag)  const {
2350   if (C == NULL) {
2351     return;
2352   }
2353   CompileLog* log = C->log();
2354   if (log != NULL) {
2355     log->begin_head("%s lock='%d' compile_id='%d' class_id='%s' kind='%s'",
2356           tag, is_Lock(), C->compile_id(),
2357           is_Unlock() ? "unlock" : is_Lock() ? "lock" : "?",
2358           kind_as_string());
2359     log->stamp();
2360     log->end_head();
2361     JVMState* p = is_Unlock() ? (as_Unlock()->dbg_jvms()) : jvms();
2362     while (p != NULL) {
2363       log->elem("jvms bci='%d' method='%d'", p->bci(), log->identify(p->method()));
2364       p = p->caller();
2365     }
2366     log->tail(tag);
2367   }
2368 }
2369 
2370 bool CallNode::may_modify_arraycopy_helper(const TypeOopPtr* dest_t, const TypeOopPtr *t_oop, PhaseTransform *phase) {
2371   if (dest_t->is_known_instance() && t_oop->is_known_instance()) {
2372     return dest_t->instance_id() == t_oop->instance_id();
2373   }
2374 
2375   if (dest_t->isa_instptr() && !dest_t->klass()->equals(phase->C->env()->Object_klass())) {
2376     // clone
2377     if (t_oop->isa_aryptr()) {
2378       return false;
2379     }
2380     if (!t_oop->isa_instptr()) {
2381       return true;
2382     }
2383     if (dest_t->klass()->is_subtype_of(t_oop->klass()) || t_oop->klass()->is_subtype_of(dest_t->klass())) {
2384       return true;
2385     }
2386     // unrelated
2387     return false;
2388   }
2389 
2390   if (dest_t->isa_aryptr()) {
2391     // arraycopy or array clone
2392     if (t_oop->isa_instptr()) {
2393       return false;
2394     }
2395     if (!t_oop->isa_aryptr()) {
2396       return true;
2397     }
2398 
2399     const Type* elem = dest_t->is_aryptr()->elem();
2400     if (elem == Type::BOTTOM) {
2401       // An array but we don't know what elements are
2402       return true;
2403     }
2404 
2405     dest_t = dest_t->is_aryptr()->with_field_offset(Type::OffsetBot)->add_offset(Type::OffsetBot)->is_oopptr();
2406     t_oop = t_oop->is_aryptr()->with_field_offset(Type::OffsetBot);
2407     uint dest_alias = phase->C->get_alias_index(dest_t);
2408     uint t_oop_alias = phase->C->get_alias_index(t_oop);
2409 
2410     return dest_alias == t_oop_alias;
2411   }
2412 
2413   return true;
2414 }