1 /*
   2  * Copyright (c) 1997, 2024, 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 : nullptr;
  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 nullptr;
 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 : nullptr;
 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 : nullptr;
 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 != nullptr, "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 == nullptr ? 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(nullptr) {
 283   _bci = InvocationEntryBci;
 284   _reexecute = Reexecute_Undefined;
 285   debug_only(_map = (SafePointNode*)-1);
 286   _caller = nullptr;
 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 == nullptr)       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 != nullptr && q != nullptr, "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 != nullptr; 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 == nullptr) { 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,sizeof(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 = nullptr;
 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 != nullptr) {
 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 != nullptr) {
 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() != nullptr) caller()->format(regalloc, n, st);
 533 }
 534 
 535 
 536 void JVMState::dump_spec(outputStream *st) const {
 537   if (_method != nullptr) {
 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 == nullptr)  endcn = strchr(name, '(');
 550         if (endcn == nullptr)  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() != nullptr)  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() == nullptr) || (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 != nullptr && 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() != nullptr) {
 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 == nullptr) {
 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 != nullptr; 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 != nullptr; 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 != nullptr; 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 != nullptr) {
 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() != nullptr)  tf()->dump_on(st);
 714   if (_cnt != COUNT_UNKNOWN)  st->print(" C=%f",_cnt);
 715   if (jvms() != nullptr)  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 nullptr;
 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, PhaseValues* phase) {
 790   assert((t_oop != nullptr), "sanity");
 791   if (is_call_to_arraycopystub() && strcmp(_name, "unsafe_arraycopy") != 0) {
 792     const TypeTuple* args = _tf->domain();
 793     Node* dest = nullptr;
 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 != nullptr, "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 == nullptr) || (phase->type(proj)->is_instptr()->instance_klass() != boxing_klass)) {
 823         return false;
 824       }
 825     }
 826     if (is_CallJava() && as_CallJava()->method() != nullptr) {
 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) : nullptr;
 834       if (proj != nullptr) {
 835         const TypeInstPtr* inst_t = phase->type(proj)->isa_instptr();
 836         if ((inst_t != nullptr) && (!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 != nullptr) && (!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 = nullptr;
 872 
 873   Node *p = proj_out_or_null(TypeFunc::Parms);
 874   if (p == nullptr)
 875     return nullptr;
 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 != nullptr) {
 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      = nullptr;
 900   projs->fallthrough_catchproj = nullptr;
 901   projs->fallthrough_ioproj    = nullptr;
 902   projs->catchall_ioproj       = nullptr;
 903   projs->catchall_catchproj    = nullptr;
 904   projs->fallthrough_memproj   = nullptr;
 905   projs->catchall_memproj      = nullptr;
 906   projs->resproj               = nullptr;
 907   projs->exobj                 = nullptr;
 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 != nullptr && cn->is_Catch()) {
 919           ProjNode *cpn = nullptr;
 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 == nullptr, "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      != nullptr, "must be found");
 964   do_asserts = do_asserts && !Compile::current()->inlining_incrementally();
 965   assert(!do_asserts || projs->fallthrough_catchproj != nullptr, "must be found");
 966   assert(!do_asserts || projs->fallthrough_memproj   != nullptr, "must be found");
 967   assert(!do_asserts || projs->fallthrough_ioproj    != nullptr, "must be found");
 968   assert(!do_asserts || projs->catchall_catchproj    != nullptr, "must be found");
 969   if (separate_io_proj) {
 970     assert(!do_asserts || projs->catchall_memproj    != nullptr, "must be found");
 971     assert(!do_asserts || projs->catchall_ioproj     != nullptr, "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 != nullptr) {
 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 != nullptr && 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 != nullptr && 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() != nullptr ? sfpt->jvms()->clone_deep(C) : nullptr);
1031   for (JVMState *jvms = this->jvms(); jvms != nullptr; 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() == nullptr) {
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 != nullptr) {
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(nullptr);
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(nullptr);
1100       }
1101     }
1102   }
1103   return CallNode::Ideal(phase, can_reshape);
1104 }
1105 
1106 //----------------------------is_uncommon_trap----------------------------
1107 // Returns true if this is an uncommon trap.
1108 bool CallStaticJavaNode::is_uncommon_trap() const {
1109   return (_name != nullptr && !strcmp(_name, "uncommon_trap"));
1110 }
1111 
1112 //----------------------------uncommon_trap_request----------------------------
1113 // If this is an uncommon trap, return the request code, else zero.
1114 int CallStaticJavaNode::uncommon_trap_request() const {
1115   return is_uncommon_trap() ? extract_uncommon_trap_request(this) : 0;
1116 }
1117 int CallStaticJavaNode::extract_uncommon_trap_request(const Node* call) {
1118 #ifndef PRODUCT
1119   if (!(call->req() > TypeFunc::Parms &&
1120         call->in(TypeFunc::Parms) != nullptr &&
1121         call->in(TypeFunc::Parms)->is_Con() &&
1122         call->in(TypeFunc::Parms)->bottom_type()->isa_int())) {
1123     assert(in_dump() != 0, "OK if dumping");
1124     tty->print("[bad uncommon trap]");
1125     return 0;
1126   }
1127 #endif
1128   return call->in(TypeFunc::Parms)->bottom_type()->is_int()->get_con();
1129 }
1130 
1131 #ifndef PRODUCT
1132 void CallStaticJavaNode::dump_spec(outputStream *st) const {
1133   st->print("# Static ");
1134   if (_name != nullptr) {
1135     st->print("%s", _name);
1136     int trap_req = uncommon_trap_request();
1137     if (trap_req != 0) {
1138       char buf[100];
1139       st->print("(%s)",
1140                  Deoptimization::format_trap_request(buf, sizeof(buf),
1141                                                      trap_req));
1142     }
1143     st->print(" ");
1144   }
1145   CallJavaNode::dump_spec(st);
1146 }
1147 
1148 void CallStaticJavaNode::dump_compact_spec(outputStream* st) const {
1149   if (_method) {
1150     _method->print_short_name(st);
1151   } else if (_name) {
1152     st->print("%s", _name);
1153   } else {
1154     st->print("<?>");
1155   }
1156 }
1157 #endif
1158 
1159 //=============================================================================
1160 uint CallDynamicJavaNode::size_of() const { return sizeof(*this); }
1161 bool CallDynamicJavaNode::cmp( const Node &n ) const {
1162   CallDynamicJavaNode &call = (CallDynamicJavaNode&)n;
1163   return CallJavaNode::cmp(call);
1164 }
1165 
1166 Node* CallDynamicJavaNode::Ideal(PhaseGVN* phase, bool can_reshape) {
1167   CallGenerator* cg = generator();
1168   if (can_reshape && cg != nullptr) {
1169     assert(IncrementalInlineVirtual, "required");
1170     assert(cg->call_node() == this, "mismatch");
1171     assert(cg->is_virtual_late_inline(), "not virtual");
1172 
1173     // Recover symbolic info for method resolution.
1174     ciMethod* caller = jvms()->method();
1175     ciBytecodeStream iter(caller);
1176     iter.force_bci(jvms()->bci());
1177 
1178     bool             not_used1;
1179     ciSignature*     not_used2;
1180     ciMethod*        orig_callee  = iter.get_method(not_used1, &not_used2);  // callee in the bytecode
1181     ciKlass*         holder       = iter.get_declared_method_holder();
1182     if (orig_callee->is_method_handle_intrinsic()) {
1183       assert(_override_symbolic_info, "required");
1184       orig_callee = method();
1185       holder = method()->holder();
1186     }
1187 
1188     ciInstanceKlass* klass = ciEnv::get_instance_klass_for_declared_method_holder(holder);
1189 
1190     Node* receiver_node = in(TypeFunc::Parms);
1191     const TypeOopPtr* receiver_type = phase->type(receiver_node)->isa_oopptr();
1192 
1193     int  not_used3;
1194     bool call_does_dispatch;
1195     ciMethod* callee = phase->C->optimize_virtual_call(caller, klass, holder, orig_callee, receiver_type, true /*is_virtual*/,
1196                                                        call_does_dispatch, not_used3);  // out-parameters
1197     if (!call_does_dispatch) {
1198       // Register for late inlining.
1199       cg->set_callee_method(callee);
1200       phase->C->prepend_late_inline(cg); // MH late inlining prepends to the list, so do the same
1201       set_generator(nullptr);
1202     }
1203   }
1204   return CallNode::Ideal(phase, can_reshape);
1205 }
1206 
1207 #ifndef PRODUCT
1208 void CallDynamicJavaNode::dump_spec(outputStream *st) const {
1209   st->print("# Dynamic ");
1210   CallJavaNode::dump_spec(st);
1211 }
1212 #endif
1213 
1214 //=============================================================================
1215 uint CallRuntimeNode::size_of() const { return sizeof(*this); }
1216 bool CallRuntimeNode::cmp( const Node &n ) const {
1217   CallRuntimeNode &call = (CallRuntimeNode&)n;
1218   return CallNode::cmp(call) && !strcmp(_name,call._name);
1219 }
1220 #ifndef PRODUCT
1221 void CallRuntimeNode::dump_spec(outputStream *st) const {
1222   st->print("# ");
1223   st->print("%s", _name);
1224   CallNode::dump_spec(st);
1225 }
1226 #endif
1227 uint CallLeafVectorNode::size_of() const { return sizeof(*this); }
1228 bool CallLeafVectorNode::cmp( const Node &n ) const {
1229   CallLeafVectorNode &call = (CallLeafVectorNode&)n;
1230   return CallLeafNode::cmp(call) && _num_bits == call._num_bits;
1231 }
1232 
1233 //------------------------------calling_convention-----------------------------
1234 void CallRuntimeNode::calling_convention(BasicType* sig_bt, VMRegPair *parm_regs, uint argcnt) const {
1235   SharedRuntime::c_calling_convention(sig_bt, parm_regs, argcnt);
1236 }
1237 
1238 void CallLeafVectorNode::calling_convention( BasicType* sig_bt, VMRegPair *parm_regs, uint argcnt ) const {
1239 #ifdef ASSERT
1240   assert(tf()->range()->field_at(TypeFunc::Parms)->is_vect()->length_in_bytes() * BitsPerByte == _num_bits,
1241          "return vector size must match");
1242   const TypeTuple* d = tf()->domain();
1243   for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
1244     Node* arg = in(i);
1245     assert(arg->bottom_type()->is_vect()->length_in_bytes() * BitsPerByte == _num_bits,
1246            "vector argument size must match");
1247   }
1248 #endif
1249 
1250   SharedRuntime::vector_calling_convention(parm_regs, _num_bits, argcnt);
1251 }
1252 
1253 //=============================================================================
1254 //------------------------------calling_convention-----------------------------
1255 
1256 
1257 //=============================================================================
1258 #ifndef PRODUCT
1259 void CallLeafNode::dump_spec(outputStream *st) const {
1260   st->print("# ");
1261   st->print("%s", _name);
1262   CallNode::dump_spec(st);
1263 }
1264 #endif
1265 
1266 //=============================================================================
1267 
1268 void SafePointNode::set_local(JVMState* jvms, uint idx, Node *c) {
1269   assert(verify_jvms(jvms), "jvms must match");
1270   int loc = jvms->locoff() + idx;
1271   if (in(loc)->is_top() && idx > 0 && !c->is_top() ) {
1272     // If current local idx is top then local idx - 1 could
1273     // be a long/double that needs to be killed since top could
1274     // represent the 2nd half of the long/double.
1275     uint ideal = in(loc -1)->ideal_reg();
1276     if (ideal == Op_RegD || ideal == Op_RegL) {
1277       // set other (low index) half to top
1278       set_req(loc - 1, in(loc));
1279     }
1280   }
1281   set_req(loc, c);
1282 }
1283 
1284 uint SafePointNode::size_of() const { return sizeof(*this); }
1285 bool SafePointNode::cmp( const Node &n ) const {
1286   return (&n == this);          // Always fail except on self
1287 }
1288 
1289 //-------------------------set_next_exception----------------------------------
1290 void SafePointNode::set_next_exception(SafePointNode* n) {
1291   assert(n == nullptr || n->Opcode() == Op_SafePoint, "correct value for next_exception");
1292   if (len() == req()) {
1293     if (n != nullptr)  add_prec(n);
1294   } else {
1295     set_prec(req(), n);
1296   }
1297 }
1298 
1299 
1300 //----------------------------next_exception-----------------------------------
1301 SafePointNode* SafePointNode::next_exception() const {
1302   if (len() == req()) {
1303     return nullptr;
1304   } else {
1305     Node* n = in(req());
1306     assert(n == nullptr || n->Opcode() == Op_SafePoint, "no other uses of prec edges");
1307     return (SafePointNode*) n;
1308   }
1309 }
1310 
1311 
1312 //------------------------------Ideal------------------------------------------
1313 // Skip over any collapsed Regions
1314 Node *SafePointNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1315   assert(_jvms == nullptr || ((uintptr_t)_jvms->map() & 1) || _jvms->map() == this, "inconsistent JVMState");
1316   return remove_dead_region(phase, can_reshape) ? this : nullptr;
1317 }
1318 
1319 //------------------------------Identity---------------------------------------
1320 // Remove obviously duplicate safepoints
1321 Node* SafePointNode::Identity(PhaseGVN* phase) {
1322 
1323   // If you have back to back safepoints, remove one
1324   if (in(TypeFunc::Control)->is_SafePoint()) {
1325     Node* out_c = unique_ctrl_out_or_null();
1326     // This can be the safepoint of an outer strip mined loop if the inner loop's backedge was removed. Replacing the
1327     // outer loop's safepoint could confuse removal of the outer loop.
1328     if (out_c != nullptr && !out_c->is_OuterStripMinedLoopEnd()) {
1329       return in(TypeFunc::Control);
1330     }
1331   }
1332 
1333   // Transforming long counted loops requires a safepoint node. Do not
1334   // eliminate a safepoint until loop opts are over.
1335   if (in(0)->is_Proj() && !phase->C->major_progress()) {
1336     Node *n0 = in(0)->in(0);
1337     // Check if he is a call projection (except Leaf Call)
1338     if( n0->is_Catch() ) {
1339       n0 = n0->in(0)->in(0);
1340       assert( n0->is_Call(), "expect a call here" );
1341     }
1342     if( n0->is_Call() && n0->as_Call()->guaranteed_safepoint() ) {
1343       // Don't remove a safepoint belonging to an OuterStripMinedLoopEndNode.
1344       // If the loop dies, they will be removed together.
1345       if (has_out_with(Op_OuterStripMinedLoopEnd)) {
1346         return this;
1347       }
1348       // Useless Safepoint, so remove it
1349       return in(TypeFunc::Control);
1350     }
1351   }
1352 
1353   return this;
1354 }
1355 
1356 //------------------------------Value------------------------------------------
1357 const Type* SafePointNode::Value(PhaseGVN* phase) const {
1358   if (phase->type(in(0)) == Type::TOP) {
1359     return Type::TOP;
1360   }
1361   if (in(0) == this) {
1362     return Type::TOP; // Dead infinite loop
1363   }
1364   return Type::CONTROL;
1365 }
1366 
1367 #ifndef PRODUCT
1368 void SafePointNode::dump_spec(outputStream *st) const {
1369   st->print(" SafePoint ");
1370   _replaced_nodes.dump(st);
1371 }
1372 #endif
1373 
1374 const RegMask &SafePointNode::in_RegMask(uint idx) const {
1375   if( idx < TypeFunc::Parms ) return RegMask::Empty;
1376   // Values outside the domain represent debug info
1377   return *(Compile::current()->matcher()->idealreg2debugmask[in(idx)->ideal_reg()]);
1378 }
1379 const RegMask &SafePointNode::out_RegMask() const {
1380   return RegMask::Empty;
1381 }
1382 
1383 
1384 void SafePointNode::grow_stack(JVMState* jvms, uint grow_by) {
1385   assert((int)grow_by > 0, "sanity");
1386   int monoff = jvms->monoff();
1387   int scloff = jvms->scloff();
1388   int endoff = jvms->endoff();
1389   assert(endoff == (int)req(), "no other states or debug info after me");
1390   Node* top = Compile::current()->top();
1391   for (uint i = 0; i < grow_by; i++) {
1392     ins_req(monoff, top);
1393   }
1394   jvms->set_monoff(monoff + grow_by);
1395   jvms->set_scloff(scloff + grow_by);
1396   jvms->set_endoff(endoff + grow_by);
1397 }
1398 
1399 void SafePointNode::push_monitor(const FastLockNode *lock) {
1400   // Add a LockNode, which points to both the original BoxLockNode (the
1401   // stack space for the monitor) and the Object being locked.
1402   const int MonitorEdges = 2;
1403   assert(JVMState::logMonitorEdges == exact_log2(MonitorEdges), "correct MonitorEdges");
1404   assert(req() == jvms()->endoff(), "correct sizing");
1405   int nextmon = jvms()->scloff();
1406   if (GenerateSynchronizationCode) {
1407     ins_req(nextmon,   lock->box_node());
1408     ins_req(nextmon+1, lock->obj_node());
1409   } else {
1410     Node* top = Compile::current()->top();
1411     ins_req(nextmon, top);
1412     ins_req(nextmon, top);
1413   }
1414   jvms()->set_scloff(nextmon + MonitorEdges);
1415   jvms()->set_endoff(req());
1416 }
1417 
1418 void SafePointNode::pop_monitor() {
1419   // Delete last monitor from debug info
1420   debug_only(int num_before_pop = jvms()->nof_monitors());
1421   const int MonitorEdges = 2;
1422   assert(JVMState::logMonitorEdges == exact_log2(MonitorEdges), "correct MonitorEdges");
1423   int scloff = jvms()->scloff();
1424   int endoff = jvms()->endoff();
1425   int new_scloff = scloff - MonitorEdges;
1426   int new_endoff = endoff - MonitorEdges;
1427   jvms()->set_scloff(new_scloff);
1428   jvms()->set_endoff(new_endoff);
1429   while (scloff > new_scloff)  del_req_ordered(--scloff);
1430   assert(jvms()->nof_monitors() == num_before_pop-1, "");
1431 }
1432 
1433 Node *SafePointNode::peek_monitor_box() const {
1434   int mon = jvms()->nof_monitors() - 1;
1435   assert(mon >= 0, "must have a monitor");
1436   return monitor_box(jvms(), mon);
1437 }
1438 
1439 Node *SafePointNode::peek_monitor_obj() const {
1440   int mon = jvms()->nof_monitors() - 1;
1441   assert(mon >= 0, "must have a monitor");
1442   return monitor_obj(jvms(), mon);
1443 }
1444 
1445 Node* SafePointNode::peek_operand(uint off) const {
1446   assert(jvms()->sp() > 0, "must have an operand");
1447   assert(off < jvms()->sp(), "off is out-of-range");
1448   return stack(jvms(), jvms()->sp() - off - 1);
1449 }
1450 
1451 // Do we Match on this edge index or not?  Match no edges
1452 uint SafePointNode::match_edge(uint idx) const {
1453   return (TypeFunc::Parms == idx);
1454 }
1455 
1456 void SafePointNode::disconnect_from_root(PhaseIterGVN *igvn) {
1457   assert(Opcode() == Op_SafePoint, "only value for safepoint in loops");
1458   int nb = igvn->C->root()->find_prec_edge(this);
1459   if (nb != -1) {
1460     igvn->delete_precedence_of(igvn->C->root(), nb);
1461   }
1462 }
1463 
1464 //==============  SafePointScalarObjectNode  ==============
1465 
1466 SafePointScalarObjectNode::SafePointScalarObjectNode(const TypeOopPtr* tp, Node* alloc, uint first_index, uint depth, uint n_fields) :
1467   TypeNode(tp, 1), // 1 control input -- seems required.  Get from root.
1468   _first_index(first_index),
1469   _depth(depth),
1470   _n_fields(n_fields),
1471   _alloc(alloc)
1472 {
1473 #ifdef ASSERT
1474   if (!alloc->is_Allocate() && !(alloc->Opcode() == Op_VectorBox)) {
1475     alloc->dump();
1476     assert(false, "unexpected call node");
1477   }
1478 #endif
1479   init_class_id(Class_SafePointScalarObject);
1480 }
1481 
1482 // Do not allow value-numbering for SafePointScalarObject node.
1483 uint SafePointScalarObjectNode::hash() const { return NO_HASH; }
1484 bool SafePointScalarObjectNode::cmp( const Node &n ) const {
1485   return (&n == this); // Always fail except on self
1486 }
1487 
1488 uint SafePointScalarObjectNode::ideal_reg() const {
1489   return 0; // No matching to machine instruction
1490 }
1491 
1492 const RegMask &SafePointScalarObjectNode::in_RegMask(uint idx) const {
1493   return *(Compile::current()->matcher()->idealreg2debugmask[in(idx)->ideal_reg()]);
1494 }
1495 
1496 const RegMask &SafePointScalarObjectNode::out_RegMask() const {
1497   return RegMask::Empty;
1498 }
1499 
1500 uint SafePointScalarObjectNode::match_edge(uint idx) const {
1501   return 0;
1502 }
1503 
1504 SafePointScalarObjectNode*
1505 SafePointScalarObjectNode::clone(Dict* sosn_map, bool& new_node) const {
1506   void* cached = (*sosn_map)[(void*)this];
1507   if (cached != nullptr) {
1508     new_node = false;
1509     return (SafePointScalarObjectNode*)cached;
1510   }
1511   new_node = true;
1512   SafePointScalarObjectNode* res = (SafePointScalarObjectNode*)Node::clone();
1513   sosn_map->Insert((void*)this, (void*)res);
1514   return res;
1515 }
1516 
1517 
1518 #ifndef PRODUCT
1519 void SafePointScalarObjectNode::dump_spec(outputStream *st) const {
1520   st->print(" # fields@[%d..%d]", first_index(), first_index() + n_fields() - 1);
1521 }
1522 #endif
1523 
1524 //==============  SafePointScalarMergeNode  ==============
1525 
1526 SafePointScalarMergeNode::SafePointScalarMergeNode(const TypeOopPtr* tp, int merge_pointer_idx) :
1527   TypeNode(tp, 1), // 1 control input -- seems required.  Get from root.
1528   _merge_pointer_idx(merge_pointer_idx)
1529 {
1530   init_class_id(Class_SafePointScalarMerge);
1531 }
1532 
1533 // Do not allow value-numbering for SafePointScalarMerge node.
1534 uint SafePointScalarMergeNode::hash() const { return NO_HASH; }
1535 bool SafePointScalarMergeNode::cmp( const Node &n ) const {
1536   return (&n == this); // Always fail except on self
1537 }
1538 
1539 uint SafePointScalarMergeNode::ideal_reg() const {
1540   return 0; // No matching to machine instruction
1541 }
1542 
1543 const RegMask &SafePointScalarMergeNode::in_RegMask(uint idx) const {
1544   return *(Compile::current()->matcher()->idealreg2debugmask[in(idx)->ideal_reg()]);
1545 }
1546 
1547 const RegMask &SafePointScalarMergeNode::out_RegMask() const {
1548   return RegMask::Empty;
1549 }
1550 
1551 uint SafePointScalarMergeNode::match_edge(uint idx) const {
1552   return 0;
1553 }
1554 
1555 SafePointScalarMergeNode*
1556 SafePointScalarMergeNode::clone(Dict* sosn_map, bool& new_node) const {
1557   void* cached = (*sosn_map)[(void*)this];
1558   if (cached != nullptr) {
1559     new_node = false;
1560     return (SafePointScalarMergeNode*)cached;
1561   }
1562   new_node = true;
1563   SafePointScalarMergeNode* res = (SafePointScalarMergeNode*)Node::clone();
1564   sosn_map->Insert((void*)this, (void*)res);
1565   return res;
1566 }
1567 
1568 #ifndef PRODUCT
1569 void SafePointScalarMergeNode::dump_spec(outputStream *st) const {
1570   st->print(" # merge_pointer_idx=%d, scalarized_objects=%d", _merge_pointer_idx, req()-1);
1571 }
1572 #endif
1573 
1574 //=============================================================================
1575 uint AllocateNode::size_of() const { return sizeof(*this); }
1576 
1577 AllocateNode::AllocateNode(Compile* C, const TypeFunc *atype,
1578                            Node *ctrl, Node *mem, Node *abio,
1579                            Node *size, Node *klass_node, Node *initial_test)
1580   : CallNode(atype, nullptr, TypeRawPtr::BOTTOM)
1581 {
1582   init_class_id(Class_Allocate);
1583   init_flags(Flag_is_macro);
1584   _is_scalar_replaceable = false;
1585   _is_non_escaping = false;
1586   _is_allocation_MemBar_redundant = false;
1587   Node *topnode = C->top();
1588 
1589   init_req( TypeFunc::Control  , ctrl );
1590   init_req( TypeFunc::I_O      , abio );
1591   init_req( TypeFunc::Memory   , mem );
1592   init_req( TypeFunc::ReturnAdr, topnode );
1593   init_req( TypeFunc::FramePtr , topnode );
1594   init_req( AllocSize          , size);
1595   init_req( KlassNode          , klass_node);
1596   init_req( InitialTest        , initial_test);
1597   init_req( ALength            , topnode);
1598   init_req( ValidLengthTest    , topnode);
1599   C->add_macro_node(this);
1600 }
1601 
1602 void AllocateNode::compute_MemBar_redundancy(ciMethod* initializer)
1603 {
1604   assert(initializer != nullptr &&
1605          initializer->is_initializer() &&
1606          !initializer->is_static(),
1607              "unexpected initializer method");
1608   BCEscapeAnalyzer* analyzer = initializer->get_bcea();
1609   if (analyzer == nullptr) {
1610     return;
1611   }
1612 
1613   // Allocation node is first parameter in its initializer
1614   if (analyzer->is_arg_stack(0) || analyzer->is_arg_local(0)) {
1615     _is_allocation_MemBar_redundant = true;
1616   }
1617 }
1618 Node *AllocateNode::make_ideal_mark(PhaseGVN *phase, Node* obj, Node* control, Node* mem) {
1619   Node* mark_node = nullptr;
1620   // For now only enable fast locking for non-array types
1621   mark_node = phase->MakeConX(markWord::prototype().value());
1622   return mark_node;
1623 }
1624 
1625 // Retrieve the length from the AllocateArrayNode. Narrow the type with a
1626 // CastII, if appropriate.  If we are not allowed to create new nodes, and
1627 // a CastII is appropriate, return null.
1628 Node *AllocateArrayNode::make_ideal_length(const TypeOopPtr* oop_type, PhaseValues* phase, bool allow_new_nodes) {
1629   Node *length = in(AllocateNode::ALength);
1630   assert(length != nullptr, "length is not null");
1631 
1632   const TypeInt* length_type = phase->find_int_type(length);
1633   const TypeAryPtr* ary_type = oop_type->isa_aryptr();
1634 
1635   if (ary_type != nullptr && length_type != nullptr) {
1636     const TypeInt* narrow_length_type = ary_type->narrow_size_type(length_type);
1637     if (narrow_length_type != length_type) {
1638       // Assert one of:
1639       //   - the narrow_length is 0
1640       //   - the narrow_length is not wider than length
1641       assert(narrow_length_type == TypeInt::ZERO ||
1642              (length_type->is_con() && narrow_length_type->is_con() &&
1643               (narrow_length_type->_hi <= length_type->_lo)) ||
1644              (narrow_length_type->_hi <= length_type->_hi &&
1645               narrow_length_type->_lo >= length_type->_lo),
1646              "narrow type must be narrower than length type");
1647 
1648       // Return null if new nodes are not allowed
1649       if (!allow_new_nodes) {
1650         return nullptr;
1651       }
1652       // Create a cast which is control dependent on the initialization to
1653       // propagate the fact that the array length must be positive.
1654       InitializeNode* init = initialization();
1655       if (init != nullptr) {
1656         length = new CastIINode(init->proj_out_or_null(TypeFunc::Control), length, narrow_length_type);
1657       }
1658     }
1659   }
1660 
1661   return length;
1662 }
1663 
1664 //=============================================================================
1665 uint LockNode::size_of() const { return sizeof(*this); }
1666 
1667 // Redundant lock elimination
1668 //
1669 // There are various patterns of locking where we release and
1670 // immediately reacquire a lock in a piece of code where no operations
1671 // occur in between that would be observable.  In those cases we can
1672 // skip releasing and reacquiring the lock without violating any
1673 // fairness requirements.  Doing this around a loop could cause a lock
1674 // to be held for a very long time so we concentrate on non-looping
1675 // control flow.  We also require that the operations are fully
1676 // redundant meaning that we don't introduce new lock operations on
1677 // some paths so to be able to eliminate it on others ala PRE.  This
1678 // would probably require some more extensive graph manipulation to
1679 // guarantee that the memory edges were all handled correctly.
1680 //
1681 // Assuming p is a simple predicate which can't trap in any way and s
1682 // is a synchronized method consider this code:
1683 //
1684 //   s();
1685 //   if (p)
1686 //     s();
1687 //   else
1688 //     s();
1689 //   s();
1690 //
1691 // 1. The unlocks of the first call to s can be eliminated if the
1692 // locks inside the then and else branches are eliminated.
1693 //
1694 // 2. The unlocks of the then and else branches can be eliminated if
1695 // the lock of the final call to s is eliminated.
1696 //
1697 // Either of these cases subsumes the simple case of sequential control flow
1698 //
1699 // Additionally we can eliminate versions without the else case:
1700 //
1701 //   s();
1702 //   if (p)
1703 //     s();
1704 //   s();
1705 //
1706 // 3. In this case we eliminate the unlock of the first s, the lock
1707 // and unlock in the then case and the lock in the final s.
1708 //
1709 // Note also that in all these cases the then/else pieces don't have
1710 // to be trivial as long as they begin and end with synchronization
1711 // operations.
1712 //
1713 //   s();
1714 //   if (p)
1715 //     s();
1716 //     f();
1717 //     s();
1718 //   s();
1719 //
1720 // The code will work properly for this case, leaving in the unlock
1721 // before the call to f and the relock after it.
1722 //
1723 // A potentially interesting case which isn't handled here is when the
1724 // locking is partially redundant.
1725 //
1726 //   s();
1727 //   if (p)
1728 //     s();
1729 //
1730 // This could be eliminated putting unlocking on the else case and
1731 // eliminating the first unlock and the lock in the then side.
1732 // Alternatively the unlock could be moved out of the then side so it
1733 // was after the merge and the first unlock and second lock
1734 // eliminated.  This might require less manipulation of the memory
1735 // state to get correct.
1736 //
1737 // Additionally we might allow work between a unlock and lock before
1738 // giving up eliminating the locks.  The current code disallows any
1739 // conditional control flow between these operations.  A formulation
1740 // similar to partial redundancy elimination computing the
1741 // availability of unlocking and the anticipatability of locking at a
1742 // program point would allow detection of fully redundant locking with
1743 // some amount of work in between.  I'm not sure how often I really
1744 // think that would occur though.  Most of the cases I've seen
1745 // indicate it's likely non-trivial work would occur in between.
1746 // There may be other more complicated constructs where we could
1747 // eliminate locking but I haven't seen any others appear as hot or
1748 // interesting.
1749 //
1750 // Locking and unlocking have a canonical form in ideal that looks
1751 // roughly like this:
1752 //
1753 //              <obj>
1754 //                | \\------+
1755 //                |  \       \
1756 //                | BoxLock   \
1757 //                |  |   |     \
1758 //                |  |    \     \
1759 //                |  |   FastLock
1760 //                |  |   /
1761 //                |  |  /
1762 //                |  |  |
1763 //
1764 //               Lock
1765 //                |
1766 //            Proj #0
1767 //                |
1768 //            MembarAcquire
1769 //                |
1770 //            Proj #0
1771 //
1772 //            MembarRelease
1773 //                |
1774 //            Proj #0
1775 //                |
1776 //              Unlock
1777 //                |
1778 //            Proj #0
1779 //
1780 //
1781 // This code proceeds by processing Lock nodes during PhaseIterGVN
1782 // and searching back through its control for the proper code
1783 // patterns.  Once it finds a set of lock and unlock operations to
1784 // eliminate they are marked as eliminatable which causes the
1785 // expansion of the Lock and Unlock macro nodes to make the operation a NOP
1786 //
1787 //=============================================================================
1788 
1789 //
1790 // Utility function to skip over uninteresting control nodes.  Nodes skipped are:
1791 //   - copy regions.  (These may not have been optimized away yet.)
1792 //   - eliminated locking nodes
1793 //
1794 static Node *next_control(Node *ctrl) {
1795   if (ctrl == nullptr)
1796     return nullptr;
1797   while (1) {
1798     if (ctrl->is_Region()) {
1799       RegionNode *r = ctrl->as_Region();
1800       Node *n = r->is_copy();
1801       if (n == nullptr)
1802         break;  // hit a region, return it
1803       else
1804         ctrl = n;
1805     } else if (ctrl->is_Proj()) {
1806       Node *in0 = ctrl->in(0);
1807       if (in0->is_AbstractLock() && in0->as_AbstractLock()->is_eliminated()) {
1808         ctrl = in0->in(0);
1809       } else {
1810         break;
1811       }
1812     } else {
1813       break; // found an interesting control
1814     }
1815   }
1816   return ctrl;
1817 }
1818 //
1819 // Given a control, see if it's the control projection of an Unlock which
1820 // operating on the same object as lock.
1821 //
1822 bool AbstractLockNode::find_matching_unlock(const Node* ctrl, LockNode* lock,
1823                                             GrowableArray<AbstractLockNode*> &lock_ops) {
1824   ProjNode *ctrl_proj = (ctrl->is_Proj()) ? ctrl->as_Proj() : nullptr;
1825   if (ctrl_proj != nullptr && ctrl_proj->_con == TypeFunc::Control) {
1826     Node *n = ctrl_proj->in(0);
1827     if (n != nullptr && n->is_Unlock()) {
1828       UnlockNode *unlock = n->as_Unlock();
1829       BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
1830       Node* lock_obj = bs->step_over_gc_barrier(lock->obj_node());
1831       Node* unlock_obj = bs->step_over_gc_barrier(unlock->obj_node());
1832       if (lock_obj->eqv_uncast(unlock_obj) &&
1833           BoxLockNode::same_slot(lock->box_node(), unlock->box_node()) &&
1834           !unlock->is_eliminated()) {
1835         lock_ops.append(unlock);
1836         return true;
1837       }
1838     }
1839   }
1840   return false;
1841 }
1842 
1843 //
1844 // Find the lock matching an unlock.  Returns null if a safepoint
1845 // or complicated control is encountered first.
1846 LockNode *AbstractLockNode::find_matching_lock(UnlockNode* unlock) {
1847   LockNode *lock_result = nullptr;
1848   // find the matching lock, or an intervening safepoint
1849   Node *ctrl = next_control(unlock->in(0));
1850   while (1) {
1851     assert(ctrl != nullptr, "invalid control graph");
1852     assert(!ctrl->is_Start(), "missing lock for unlock");
1853     if (ctrl->is_top()) break;  // dead control path
1854     if (ctrl->is_Proj()) ctrl = ctrl->in(0);
1855     if (ctrl->is_SafePoint()) {
1856         break;  // found a safepoint (may be the lock we are searching for)
1857     } else if (ctrl->is_Region()) {
1858       // Check for a simple diamond pattern.  Punt on anything more complicated
1859       if (ctrl->req() == 3 && ctrl->in(1) != nullptr && ctrl->in(2) != nullptr) {
1860         Node *in1 = next_control(ctrl->in(1));
1861         Node *in2 = next_control(ctrl->in(2));
1862         if (((in1->is_IfTrue() && in2->is_IfFalse()) ||
1863              (in2->is_IfTrue() && in1->is_IfFalse())) && (in1->in(0) == in2->in(0))) {
1864           ctrl = next_control(in1->in(0)->in(0));
1865         } else {
1866           break;
1867         }
1868       } else {
1869         break;
1870       }
1871     } else {
1872       ctrl = next_control(ctrl->in(0));  // keep searching
1873     }
1874   }
1875   if (ctrl->is_Lock()) {
1876     LockNode *lock = ctrl->as_Lock();
1877     BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
1878     Node* lock_obj = bs->step_over_gc_barrier(lock->obj_node());
1879     Node* unlock_obj = bs->step_over_gc_barrier(unlock->obj_node());
1880     if (lock_obj->eqv_uncast(unlock_obj) &&
1881         BoxLockNode::same_slot(lock->box_node(), unlock->box_node())) {
1882       lock_result = lock;
1883     }
1884   }
1885   return lock_result;
1886 }
1887 
1888 // This code corresponds to case 3 above.
1889 
1890 bool AbstractLockNode::find_lock_and_unlock_through_if(Node* node, LockNode* lock,
1891                                                        GrowableArray<AbstractLockNode*> &lock_ops) {
1892   Node* if_node = node->in(0);
1893   bool  if_true = node->is_IfTrue();
1894 
1895   if (if_node->is_If() && if_node->outcnt() == 2 && (if_true || node->is_IfFalse())) {
1896     Node *lock_ctrl = next_control(if_node->in(0));
1897     if (find_matching_unlock(lock_ctrl, lock, lock_ops)) {
1898       Node* lock1_node = nullptr;
1899       ProjNode* proj = if_node->as_If()->proj_out(!if_true);
1900       if (if_true) {
1901         if (proj->is_IfFalse() && proj->outcnt() == 1) {
1902           lock1_node = proj->unique_out();
1903         }
1904       } else {
1905         if (proj->is_IfTrue() && proj->outcnt() == 1) {
1906           lock1_node = proj->unique_out();
1907         }
1908       }
1909       if (lock1_node != nullptr && lock1_node->is_Lock()) {
1910         LockNode *lock1 = lock1_node->as_Lock();
1911         BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
1912         Node* lock_obj = bs->step_over_gc_barrier(lock->obj_node());
1913         Node* lock1_obj = bs->step_over_gc_barrier(lock1->obj_node());
1914         if (lock_obj->eqv_uncast(lock1_obj) &&
1915             BoxLockNode::same_slot(lock->box_node(), lock1->box_node()) &&
1916             !lock1->is_eliminated()) {
1917           lock_ops.append(lock1);
1918           return true;
1919         }
1920       }
1921     }
1922   }
1923 
1924   lock_ops.trunc_to(0);
1925   return false;
1926 }
1927 
1928 bool AbstractLockNode::find_unlocks_for_region(const RegionNode* region, LockNode* lock,
1929                                GrowableArray<AbstractLockNode*> &lock_ops) {
1930   // check each control merging at this point for a matching unlock.
1931   // in(0) should be self edge so skip it.
1932   for (int i = 1; i < (int)region->req(); i++) {
1933     Node *in_node = next_control(region->in(i));
1934     if (in_node != nullptr) {
1935       if (find_matching_unlock(in_node, lock, lock_ops)) {
1936         // found a match so keep on checking.
1937         continue;
1938       } else if (find_lock_and_unlock_through_if(in_node, lock, lock_ops)) {
1939         continue;
1940       }
1941 
1942       // If we fall through to here then it was some kind of node we
1943       // don't understand or there wasn't a matching unlock, so give
1944       // up trying to merge locks.
1945       lock_ops.trunc_to(0);
1946       return false;
1947     }
1948   }
1949   return true;
1950 
1951 }
1952 
1953 const char* AbstractLockNode::_kind_names[] = {"Regular", "NonEscObj", "Coarsened", "Nested"};
1954 
1955 const char * AbstractLockNode::kind_as_string() const {
1956   return _kind_names[_kind];
1957 }
1958 
1959 #ifndef PRODUCT
1960 //
1961 // Create a counter which counts the number of times this lock is acquired
1962 //
1963 void AbstractLockNode::create_lock_counter(JVMState* state) {
1964   _counter = OptoRuntime::new_named_counter(state, NamedCounter::LockCounter);
1965 }
1966 
1967 void AbstractLockNode::set_eliminated_lock_counter() {
1968   if (_counter) {
1969     // Update the counter to indicate that this lock was eliminated.
1970     // The counter update code will stay around even though the
1971     // optimizer will eliminate the lock operation itself.
1972     _counter->set_tag(NamedCounter::EliminatedLockCounter);
1973   }
1974 }
1975 
1976 void AbstractLockNode::dump_spec(outputStream* st) const {
1977   st->print("%s ", _kind_names[_kind]);
1978   CallNode::dump_spec(st);
1979 }
1980 
1981 void AbstractLockNode::dump_compact_spec(outputStream* st) const {
1982   st->print("%s", _kind_names[_kind]);
1983 }
1984 #endif
1985 
1986 //=============================================================================
1987 Node *LockNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1988 
1989   // perform any generic optimizations first (returns 'this' or null)
1990   Node *result = SafePointNode::Ideal(phase, can_reshape);
1991   if (result != nullptr)  return result;
1992   // Don't bother trying to transform a dead node
1993   if (in(0) && in(0)->is_top())  return nullptr;
1994 
1995   // Now see if we can optimize away this lock.  We don't actually
1996   // remove the locking here, we simply set the _eliminate flag which
1997   // prevents macro expansion from expanding the lock.  Since we don't
1998   // modify the graph, the value returned from this function is the
1999   // one computed above.
2000   if (can_reshape && EliminateLocks && !is_non_esc_obj()) {
2001     //
2002     // If we are locking an non-escaped object, the lock/unlock is unnecessary
2003     //
2004     ConnectionGraph *cgr = phase->C->congraph();
2005     if (cgr != nullptr && cgr->can_eliminate_lock(this)) {
2006       assert(!is_eliminated() || is_coarsened(), "sanity");
2007       // The lock could be marked eliminated by lock coarsening
2008       // code during first IGVN before EA. Replace coarsened flag
2009       // to eliminate all associated locks/unlocks.
2010 #ifdef ASSERT
2011       this->log_lock_optimization(phase->C,"eliminate_lock_set_non_esc1");
2012 #endif
2013       this->set_non_esc_obj();
2014       return result;
2015     }
2016 
2017     if (!phase->C->do_locks_coarsening()) {
2018       return result; // Compiling without locks coarsening
2019     }
2020     //
2021     // Try lock coarsening
2022     //
2023     PhaseIterGVN* iter = phase->is_IterGVN();
2024     if (iter != nullptr && !is_eliminated()) {
2025 
2026       GrowableArray<AbstractLockNode*>   lock_ops;
2027 
2028       Node *ctrl = next_control(in(0));
2029 
2030       // now search back for a matching Unlock
2031       if (find_matching_unlock(ctrl, this, lock_ops)) {
2032         // found an unlock directly preceding this lock.  This is the
2033         // case of single unlock directly control dependent on a
2034         // single lock which is the trivial version of case 1 or 2.
2035       } else if (ctrl->is_Region() ) {
2036         if (find_unlocks_for_region(ctrl->as_Region(), this, lock_ops)) {
2037         // found lock preceded by multiple unlocks along all paths
2038         // joining at this point which is case 3 in description above.
2039         }
2040       } else {
2041         // see if this lock comes from either half of an if and the
2042         // predecessors merges unlocks and the other half of the if
2043         // performs a lock.
2044         if (find_lock_and_unlock_through_if(ctrl, this, lock_ops)) {
2045           // found unlock splitting to an if with locks on both branches.
2046         }
2047       }
2048 
2049       if (lock_ops.length() > 0) {
2050         // add ourselves to the list of locks to be eliminated.
2051         lock_ops.append(this);
2052 
2053   #ifndef PRODUCT
2054         if (PrintEliminateLocks) {
2055           int locks = 0;
2056           int unlocks = 0;
2057           if (Verbose) {
2058             tty->print_cr("=== Locks coarsening ===");
2059           }
2060           for (int i = 0; i < lock_ops.length(); i++) {
2061             AbstractLockNode* lock = lock_ops.at(i);
2062             if (lock->Opcode() == Op_Lock)
2063               locks++;
2064             else
2065               unlocks++;
2066             if (Verbose) {
2067               tty->print(" %d: ", i);
2068               lock->dump();
2069             }
2070           }
2071           tty->print_cr("=== Coarsened %d unlocks and %d locks", unlocks, locks);
2072         }
2073   #endif
2074 
2075         // for each of the identified locks, mark them
2076         // as eliminatable
2077         for (int i = 0; i < lock_ops.length(); i++) {
2078           AbstractLockNode* lock = lock_ops.at(i);
2079 
2080           // Mark it eliminated by coarsening and update any counters
2081 #ifdef ASSERT
2082           lock->log_lock_optimization(phase->C, "eliminate_lock_set_coarsened");
2083 #endif
2084           lock->set_coarsened();
2085         }
2086         // Record this coarsened group.
2087         phase->C->add_coarsened_locks(lock_ops);
2088       } else if (ctrl->is_Region() &&
2089                  iter->_worklist.member(ctrl)) {
2090         // We weren't able to find any opportunities but the region this
2091         // lock is control dependent on hasn't been processed yet so put
2092         // this lock back on the worklist so we can check again once any
2093         // region simplification has occurred.
2094         iter->_worklist.push(this);
2095       }
2096     }
2097   }
2098 
2099   return result;
2100 }
2101 
2102 //=============================================================================
2103 bool LockNode::is_nested_lock_region() {
2104   return is_nested_lock_region(nullptr);
2105 }
2106 
2107 // p is used for access to compilation log; no logging if null
2108 bool LockNode::is_nested_lock_region(Compile * c) {
2109   BoxLockNode* box = box_node()->as_BoxLock();
2110   int stk_slot = box->stack_slot();
2111   if (stk_slot <= 0) {
2112 #ifdef ASSERT
2113     this->log_lock_optimization(c, "eliminate_lock_INLR_1");
2114 #endif
2115     return false; // External lock or it is not Box (Phi node).
2116   }
2117 
2118   // Ignore complex cases: merged locks or multiple locks.
2119   Node* obj = obj_node();
2120   LockNode* unique_lock = nullptr;
2121   Node* bad_lock = nullptr;
2122   if (!box->is_simple_lock_region(&unique_lock, obj, &bad_lock)) {
2123 #ifdef ASSERT
2124     this->log_lock_optimization(c, "eliminate_lock_INLR_2a", bad_lock);
2125 #endif
2126     return false;
2127   }
2128   if (unique_lock != this) {
2129 #ifdef ASSERT
2130     this->log_lock_optimization(c, "eliminate_lock_INLR_2b", (unique_lock != nullptr ? unique_lock : bad_lock));
2131     if (PrintEliminateLocks && Verbose) {
2132       tty->print_cr("=============== unique_lock != this ============");
2133       tty->print(" this: ");
2134       this->dump();
2135       tty->print(" box: ");
2136       box->dump();
2137       tty->print(" obj: ");
2138       obj->dump();
2139       if (unique_lock != nullptr) {
2140         tty->print(" unique_lock: ");
2141         unique_lock->dump();
2142       }
2143       if (bad_lock != nullptr) {
2144         tty->print(" bad_lock: ");
2145         bad_lock->dump();
2146       }
2147       tty->print_cr("===============");
2148     }
2149 #endif
2150     return false;
2151   }
2152 
2153   BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
2154   obj = bs->step_over_gc_barrier(obj);
2155   // Look for external lock for the same object.
2156   SafePointNode* sfn = this->as_SafePoint();
2157   JVMState* youngest_jvms = sfn->jvms();
2158   int max_depth = youngest_jvms->depth();
2159   for (int depth = 1; depth <= max_depth; depth++) {
2160     JVMState* jvms = youngest_jvms->of_depth(depth);
2161     int num_mon  = jvms->nof_monitors();
2162     // Loop over monitors
2163     for (int idx = 0; idx < num_mon; idx++) {
2164       Node* obj_node = sfn->monitor_obj(jvms, idx);
2165       obj_node = bs->step_over_gc_barrier(obj_node);
2166       BoxLockNode* box_node = sfn->monitor_box(jvms, idx)->as_BoxLock();
2167       if ((box_node->stack_slot() < stk_slot) && obj_node->eqv_uncast(obj)) {
2168         box->set_nested();
2169         return true;
2170       }
2171     }
2172   }
2173 #ifdef ASSERT
2174   this->log_lock_optimization(c, "eliminate_lock_INLR_3");
2175 #endif
2176   return false;
2177 }
2178 
2179 //=============================================================================
2180 uint UnlockNode::size_of() const { return sizeof(*this); }
2181 
2182 //=============================================================================
2183 Node *UnlockNode::Ideal(PhaseGVN *phase, bool can_reshape) {
2184 
2185   // perform any generic optimizations first (returns 'this' or null)
2186   Node *result = SafePointNode::Ideal(phase, can_reshape);
2187   if (result != nullptr)  return result;
2188   // Don't bother trying to transform a dead node
2189   if (in(0) && in(0)->is_top())  return nullptr;
2190 
2191   // Now see if we can optimize away this unlock.  We don't actually
2192   // remove the unlocking here, we simply set the _eliminate flag which
2193   // prevents macro expansion from expanding the unlock.  Since we don't
2194   // modify the graph, the value returned from this function is the
2195   // one computed above.
2196   // Escape state is defined after Parse phase.
2197   if (can_reshape && EliminateLocks && !is_non_esc_obj()) {
2198     //
2199     // If we are unlocking an non-escaped object, the lock/unlock is unnecessary.
2200     //
2201     ConnectionGraph *cgr = phase->C->congraph();
2202     if (cgr != nullptr && cgr->can_eliminate_lock(this)) {
2203       assert(!is_eliminated() || is_coarsened(), "sanity");
2204       // The lock could be marked eliminated by lock coarsening
2205       // code during first IGVN before EA. Replace coarsened flag
2206       // to eliminate all associated locks/unlocks.
2207 #ifdef ASSERT
2208       this->log_lock_optimization(phase->C, "eliminate_lock_set_non_esc2");
2209 #endif
2210       this->set_non_esc_obj();
2211     }
2212   }
2213   return result;
2214 }
2215 
2216 void AbstractLockNode::log_lock_optimization(Compile *C, const char * tag, Node* bad_lock)  const {
2217   if (C == nullptr) {
2218     return;
2219   }
2220   CompileLog* log = C->log();
2221   if (log != nullptr) {
2222     Node* box = box_node();
2223     Node* obj = obj_node();
2224     int box_id = box != nullptr ? box->_idx : -1;
2225     int obj_id = obj != nullptr ? obj->_idx : -1;
2226 
2227     log->begin_head("%s compile_id='%d' lock_id='%d' class='%s' kind='%s' box_id='%d' obj_id='%d' bad_id='%d'",
2228           tag, C->compile_id(), this->_idx,
2229           is_Unlock() ? "unlock" : is_Lock() ? "lock" : "?",
2230           kind_as_string(), box_id, obj_id, (bad_lock != nullptr ? bad_lock->_idx : -1));
2231     log->stamp();
2232     log->end_head();
2233     JVMState* p = is_Unlock() ? (as_Unlock()->dbg_jvms()) : jvms();
2234     while (p != nullptr) {
2235       log->elem("jvms bci='%d' method='%d'", p->bci(), log->identify(p->method()));
2236       p = p->caller();
2237     }
2238     log->tail(tag);
2239   }
2240 }
2241 
2242 bool CallNode::may_modify_arraycopy_helper(const TypeOopPtr* dest_t, const TypeOopPtr* t_oop, PhaseValues* phase) {
2243   if (dest_t->is_known_instance() && t_oop->is_known_instance()) {
2244     return dest_t->instance_id() == t_oop->instance_id();
2245   }
2246 
2247   if (dest_t->isa_instptr() && !dest_t->is_instptr()->instance_klass()->equals(phase->C->env()->Object_klass())) {
2248     // clone
2249     if (t_oop->isa_aryptr()) {
2250       return false;
2251     }
2252     if (!t_oop->isa_instptr()) {
2253       return true;
2254     }
2255     if (dest_t->maybe_java_subtype_of(t_oop) || t_oop->maybe_java_subtype_of(dest_t)) {
2256       return true;
2257     }
2258     // unrelated
2259     return false;
2260   }
2261 
2262   if (dest_t->isa_aryptr()) {
2263     // arraycopy or array clone
2264     if (t_oop->isa_instptr()) {
2265       return false;
2266     }
2267     if (!t_oop->isa_aryptr()) {
2268       return true;
2269     }
2270 
2271     const Type* elem = dest_t->is_aryptr()->elem();
2272     if (elem == Type::BOTTOM) {
2273       // An array but we don't know what elements are
2274       return true;
2275     }
2276 
2277     dest_t = dest_t->add_offset(Type::OffsetBot)->is_oopptr();
2278     uint dest_alias = phase->C->get_alias_index(dest_t);
2279     uint t_oop_alias = phase->C->get_alias_index(t_oop);
2280 
2281     return dest_alias == t_oop_alias;
2282   }
2283 
2284   return true;
2285 }