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