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