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