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