1 /* 2 * Copyright 1997-2008 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 20 * CA 95054 USA or visit www.sun.com if you need additional information or 21 * have any questions. 22 * 23 */ 24 25 // Portions of code courtesy of Clifford Click 26 27 // Optimization - Graph Style 28 29 #include "incls/_precompiled.incl" 30 #include "incls/_callnode.cpp.incl" 31 32 //============================================================================= 33 uint StartNode::size_of() const { return sizeof(*this); } 34 uint StartNode::cmp( const Node &n ) const 35 { return _domain == ((StartNode&)n)._domain; } 36 const Type *StartNode::bottom_type() const { return _domain; } 37 const Type *StartNode::Value(PhaseTransform *phase) const { return _domain; } 38 #ifndef PRODUCT 39 void StartNode::dump_spec(outputStream *st) const { st->print(" #"); _domain->dump_on(st);} 40 #endif 41 42 //------------------------------Ideal------------------------------------------ 43 Node *StartNode::Ideal(PhaseGVN *phase, bool can_reshape){ 44 return remove_dead_region(phase, can_reshape) ? this : NULL; 45 } 46 47 //------------------------------calling_convention----------------------------- 48 void StartNode::calling_convention( BasicType* sig_bt, VMRegPair *parm_regs, uint argcnt ) const { 49 Matcher::calling_convention( sig_bt, parm_regs, argcnt, false ); 50 } 51 52 //------------------------------Registers-------------------------------------- 53 const RegMask &StartNode::in_RegMask(uint) const { 54 return RegMask::Empty; 55 } 56 57 //------------------------------match------------------------------------------ 58 // Construct projections for incoming parameters, and their RegMask info 59 Node *StartNode::match( const ProjNode *proj, const Matcher *match ) { 60 switch (proj->_con) { 61 case TypeFunc::Control: 62 case TypeFunc::I_O: 63 case TypeFunc::Memory: 64 return new (match->C, 1) MachProjNode(this,proj->_con,RegMask::Empty,MachProjNode::unmatched_proj); 65 case TypeFunc::FramePtr: 66 return new (match->C, 1) MachProjNode(this,proj->_con,Matcher::c_frame_ptr_mask, Op_RegP); 67 case TypeFunc::ReturnAdr: 68 return new (match->C, 1) MachProjNode(this,proj->_con,match->_return_addr_mask,Op_RegP); 69 case TypeFunc::Parms: 70 default: { 71 uint parm_num = proj->_con - TypeFunc::Parms; 72 const Type *t = _domain->field_at(proj->_con); 73 if (t->base() == Type::Half) // 2nd half of Longs and Doubles 74 return new (match->C, 1) ConNode(Type::TOP); 75 uint ideal_reg = Matcher::base2reg[t->base()]; 76 RegMask &rm = match->_calling_convention_mask[parm_num]; 77 return new (match->C, 1) MachProjNode(this,proj->_con,rm,ideal_reg); 78 } 79 } 80 return NULL; 81 } 82 83 //------------------------------StartOSRNode---------------------------------- 84 // The method start node for an on stack replacement adapter 85 86 //------------------------------osr_domain----------------------------- 87 const TypeTuple *StartOSRNode::osr_domain() { 88 const Type **fields = TypeTuple::fields(2); 89 fields[TypeFunc::Parms+0] = TypeRawPtr::BOTTOM; // address of osr buffer 90 91 return TypeTuple::make(TypeFunc::Parms+1, fields); 92 } 93 94 //============================================================================= 95 const char * const ParmNode::names[TypeFunc::Parms+1] = { 96 "Control", "I_O", "Memory", "FramePtr", "ReturnAdr", "Parms" 97 }; 98 99 #ifndef PRODUCT 100 void ParmNode::dump_spec(outputStream *st) const { 101 if( _con < TypeFunc::Parms ) { 102 st->print(names[_con]); 103 } else { 104 st->print("Parm%d: ",_con-TypeFunc::Parms); 105 // Verbose and WizardMode dump bottom_type for all nodes 106 if( !Verbose && !WizardMode ) bottom_type()->dump_on(st); 107 } 108 } 109 #endif 110 111 uint ParmNode::ideal_reg() const { 112 switch( _con ) { 113 case TypeFunc::Control : // fall through 114 case TypeFunc::I_O : // fall through 115 case TypeFunc::Memory : return 0; 116 case TypeFunc::FramePtr : // fall through 117 case TypeFunc::ReturnAdr: return Op_RegP; 118 default : assert( _con > TypeFunc::Parms, "" ); 119 // fall through 120 case TypeFunc::Parms : { 121 // Type of argument being passed 122 const Type *t = in(0)->as_Start()->_domain->field_at(_con); 123 return Matcher::base2reg[t->base()]; 124 } 125 } 126 ShouldNotReachHere(); 127 return 0; 128 } 129 130 //============================================================================= 131 ReturnNode::ReturnNode(uint edges, Node *cntrl, Node *i_o, Node *memory, Node *frameptr, Node *retadr ) : Node(edges) { 132 init_req(TypeFunc::Control,cntrl); 133 init_req(TypeFunc::I_O,i_o); 134 init_req(TypeFunc::Memory,memory); 135 init_req(TypeFunc::FramePtr,frameptr); 136 init_req(TypeFunc::ReturnAdr,retadr); 137 } 138 139 Node *ReturnNode::Ideal(PhaseGVN *phase, bool can_reshape){ 140 return remove_dead_region(phase, can_reshape) ? this : NULL; 141 } 142 143 const Type *ReturnNode::Value( PhaseTransform *phase ) const { 144 return ( phase->type(in(TypeFunc::Control)) == Type::TOP) 145 ? Type::TOP 146 : Type::BOTTOM; 147 } 148 149 // Do we Match on this edge index or not? No edges on return nodes 150 uint ReturnNode::match_edge(uint idx) const { 151 return 0; 152 } 153 154 155 #ifndef PRODUCT 156 void ReturnNode::dump_req() const { 157 // Dump the required inputs, enclosed in '(' and ')' 158 uint i; // Exit value of loop 159 for( i=0; i<req(); i++ ) { // For all required inputs 160 if( i == TypeFunc::Parms ) tty->print("returns"); 161 if( in(i) ) tty->print("%c%d ", Compile::current()->node_arena()->contains(in(i)) ? ' ' : 'o', in(i)->_idx); 162 else tty->print("_ "); 163 } 164 } 165 #endif 166 167 //============================================================================= 168 RethrowNode::RethrowNode( 169 Node* cntrl, 170 Node* i_o, 171 Node* memory, 172 Node* frameptr, 173 Node* ret_adr, 174 Node* exception 175 ) : Node(TypeFunc::Parms + 1) { 176 init_req(TypeFunc::Control , cntrl ); 177 init_req(TypeFunc::I_O , i_o ); 178 init_req(TypeFunc::Memory , memory ); 179 init_req(TypeFunc::FramePtr , frameptr ); 180 init_req(TypeFunc::ReturnAdr, ret_adr); 181 init_req(TypeFunc::Parms , exception); 182 } 183 184 Node *RethrowNode::Ideal(PhaseGVN *phase, bool can_reshape){ 185 return remove_dead_region(phase, can_reshape) ? this : NULL; 186 } 187 188 const Type *RethrowNode::Value( PhaseTransform *phase ) const { 189 return (phase->type(in(TypeFunc::Control)) == Type::TOP) 190 ? Type::TOP 191 : Type::BOTTOM; 192 } 193 194 uint RethrowNode::match_edge(uint idx) const { 195 return 0; 196 } 197 198 #ifndef PRODUCT 199 void RethrowNode::dump_req() const { 200 // Dump the required inputs, enclosed in '(' and ')' 201 uint i; // Exit value of loop 202 for( i=0; i<req(); i++ ) { // For all required inputs 203 if( i == TypeFunc::Parms ) tty->print("exception"); 204 if( in(i) ) tty->print("%c%d ", Compile::current()->node_arena()->contains(in(i)) ? ' ' : 'o', in(i)->_idx); 205 else tty->print("_ "); 206 } 207 } 208 #endif 209 210 //============================================================================= 211 // Do we Match on this edge index or not? Match only target address & method 212 uint TailCallNode::match_edge(uint idx) const { 213 return TypeFunc::Parms <= idx && idx <= TypeFunc::Parms+1; 214 } 215 216 //============================================================================= 217 // Do we Match on this edge index or not? Match only target address & oop 218 uint TailJumpNode::match_edge(uint idx) const { 219 return TypeFunc::Parms <= idx && idx <= TypeFunc::Parms+1; 220 } 221 222 //============================================================================= 223 JVMState::JVMState(ciMethod* method, JVMState* caller) { 224 assert(method != NULL, "must be valid call site"); 225 _method = method; 226 debug_only(_bci = -99); // random garbage value 227 debug_only(_map = (SafePointNode*)-1); 228 _caller = caller; 229 _depth = 1 + (caller == NULL ? 0 : caller->depth()); 230 _locoff = TypeFunc::Parms; 231 _stkoff = _locoff + _method->max_locals(); 232 _monoff = _stkoff + _method->max_stack(); 233 _scloff = _monoff; 234 _endoff = _monoff; 235 _sp = 0; 236 } 237 JVMState::JVMState(int stack_size) { 238 _method = NULL; 239 _bci = InvocationEntryBci; 240 debug_only(_map = (SafePointNode*)-1); 241 _caller = NULL; 242 _depth = 1; 243 _locoff = TypeFunc::Parms; 244 _stkoff = _locoff; 245 _monoff = _stkoff + stack_size; 246 _scloff = _monoff; 247 _endoff = _monoff; 248 _sp = 0; 249 } 250 251 //--------------------------------of_depth------------------------------------- 252 JVMState* JVMState::of_depth(int d) const { 253 const JVMState* jvmp = this; 254 assert(0 < d && (uint)d <= depth(), "oob"); 255 for (int skip = depth() - d; skip > 0; skip--) { 256 jvmp = jvmp->caller(); 257 } 258 assert(jvmp->depth() == (uint)d, "found the right one"); 259 return (JVMState*)jvmp; 260 } 261 262 //-----------------------------same_calls_as----------------------------------- 263 bool JVMState::same_calls_as(const JVMState* that) const { 264 if (this == that) return true; 265 if (this->depth() != that->depth()) return false; 266 const JVMState* p = this; 267 const JVMState* q = that; 268 for (;;) { 269 if (p->_method != q->_method) return false; 270 if (p->_method == NULL) return true; // bci is irrelevant 271 if (p->_bci != q->_bci) return false; 272 p = p->caller(); 273 q = q->caller(); 274 if (p == q) return true; 275 assert(p != NULL && q != NULL, "depth check ensures we don't run off end"); 276 } 277 } 278 279 //------------------------------debug_start------------------------------------ 280 uint JVMState::debug_start() const { 281 debug_only(JVMState* jvmroot = of_depth(1)); 282 assert(jvmroot->locoff() <= this->locoff(), "youngest JVMState must be last"); 283 return of_depth(1)->locoff(); 284 } 285 286 //-------------------------------debug_end------------------------------------- 287 uint JVMState::debug_end() const { 288 debug_only(JVMState* jvmroot = of_depth(1)); 289 assert(jvmroot->endoff() <= this->endoff(), "youngest JVMState must be last"); 290 return endoff(); 291 } 292 293 //------------------------------debug_depth------------------------------------ 294 uint JVMState::debug_depth() const { 295 uint total = 0; 296 for (const JVMState* jvmp = this; jvmp != NULL; jvmp = jvmp->caller()) { 297 total += jvmp->debug_size(); 298 } 299 return total; 300 } 301 302 #ifndef PRODUCT 303 304 //------------------------------format_helper---------------------------------- 305 // Given an allocation (a Chaitin object) and a Node decide if the Node carries 306 // any defined value or not. If it does, print out the register or constant. 307 static void format_helper( PhaseRegAlloc *regalloc, outputStream* st, Node *n, const char *msg, uint i, GrowableArray<SafePointScalarObjectNode*> *scobjs ) { 308 if (n == NULL) { st->print(" NULL"); return; } 309 if (n->is_SafePointScalarObject()) { 310 // Scalar replacement. 311 SafePointScalarObjectNode* spobj = n->as_SafePointScalarObject(); 312 scobjs->append_if_missing(spobj); 313 int sco_n = scobjs->find(spobj); 314 assert(sco_n >= 0, ""); 315 st->print(" %s%d]=#ScObj" INT32_FORMAT, msg, i, sco_n); 316 return; 317 } 318 if( OptoReg::is_valid(regalloc->get_reg_first(n))) { // Check for undefined 319 char buf[50]; 320 regalloc->dump_register(n,buf); 321 st->print(" %s%d]=%s",msg,i,buf); 322 } else { // No register, but might be constant 323 const Type *t = n->bottom_type(); 324 switch (t->base()) { 325 case Type::Int: 326 st->print(" %s%d]=#"INT32_FORMAT,msg,i,t->is_int()->get_con()); 327 break; 328 case Type::AnyPtr: 329 assert( t == TypePtr::NULL_PTR, "" ); 330 st->print(" %s%d]=#NULL",msg,i); 331 break; 332 case Type::AryPtr: 333 case Type::KlassPtr: 334 case Type::InstPtr: 335 st->print(" %s%d]=#Ptr" INTPTR_FORMAT,msg,i,t->isa_oopptr()->const_oop()); 336 break; 337 case Type::NarrowOop: 338 st->print(" %s%d]=#Ptr" INTPTR_FORMAT,msg,i,t->make_ptr()->isa_oopptr()->const_oop()); 339 break; 340 case Type::RawPtr: 341 st->print(" %s%d]=#Raw" INTPTR_FORMAT,msg,i,t->is_rawptr()); 342 break; 343 case Type::DoubleCon: 344 st->print(" %s%d]=#%fD",msg,i,t->is_double_constant()->_d); 345 break; 346 case Type::FloatCon: 347 st->print(" %s%d]=#%fF",msg,i,t->is_float_constant()->_f); 348 break; 349 case Type::Long: 350 st->print(" %s%d]=#"INT64_FORMAT,msg,i,t->is_long()->get_con()); 351 break; 352 case Type::Half: 353 case Type::Top: 354 st->print(" %s%d]=_",msg,i); 355 break; 356 default: ShouldNotReachHere(); 357 } 358 } 359 } 360 361 //------------------------------format----------------------------------------- 362 void JVMState::format(PhaseRegAlloc *regalloc, const Node *n, outputStream* st) const { 363 st->print(" #"); 364 if( _method ) { 365 _method->print_short_name(st); 366 st->print(" @ bci:%d ",_bci); 367 } else { 368 st->print_cr(" runtime stub "); 369 return; 370 } 371 if (n->is_MachSafePoint()) { 372 GrowableArray<SafePointScalarObjectNode*> scobjs; 373 MachSafePointNode *mcall = n->as_MachSafePoint(); 374 uint i; 375 // Print locals 376 for( i = 0; i < (uint)loc_size(); i++ ) 377 format_helper( regalloc, st, mcall->local(this, i), "L[", i, &scobjs ); 378 // Print stack 379 for (i = 0; i < (uint)stk_size(); i++) { 380 if ((uint)(_stkoff + i) >= mcall->len()) 381 st->print(" oob "); 382 else 383 format_helper( regalloc, st, mcall->stack(this, i), "STK[", i, &scobjs ); 384 } 385 for (i = 0; (int)i < nof_monitors(); i++) { 386 Node *box = mcall->monitor_box(this, i); 387 Node *obj = mcall->monitor_obj(this, i); 388 if ( OptoReg::is_valid(regalloc->get_reg_first(box)) ) { 389 while( !box->is_BoxLock() ) box = box->in(1); 390 format_helper( regalloc, st, box, "MON-BOX[", i, &scobjs ); 391 } else { 392 OptoReg::Name box_reg = BoxLockNode::stack_slot(box); 393 st->print(" MON-BOX%d=%s+%d", 394 i, 395 OptoReg::regname(OptoReg::c_frame_pointer), 396 regalloc->reg2offset(box_reg)); 397 } 398 const char* obj_msg = "MON-OBJ["; 399 if (EliminateLocks) { 400 while( !box->is_BoxLock() ) box = box->in(1); 401 if (box->as_BoxLock()->is_eliminated()) 402 obj_msg = "MON-OBJ(LOCK ELIMINATED)["; 403 } 404 format_helper( regalloc, st, obj, obj_msg, i, &scobjs ); 405 } 406 407 for (i = 0; i < (uint)scobjs.length(); i++) { 408 // Scalar replaced objects. 409 st->print_cr(""); 410 st->print(" # ScObj" INT32_FORMAT " ", i); 411 SafePointScalarObjectNode* spobj = scobjs.at(i); 412 ciKlass* cik = spobj->bottom_type()->is_oopptr()->klass(); 413 assert(cik->is_instance_klass() || 414 cik->is_array_klass(), "Not supported allocation."); 415 ciInstanceKlass *iklass = NULL; 416 if (cik->is_instance_klass()) { 417 cik->print_name_on(st); 418 iklass = cik->as_instance_klass(); 419 } else if (cik->is_type_array_klass()) { 420 cik->as_array_klass()->base_element_type()->print_name_on(st); 421 st->print("[%d]=", spobj->n_fields()); 422 } else if (cik->is_obj_array_klass()) { 423 ciType* cie = cik->as_array_klass()->base_element_type(); 424 int ndim = 1; 425 while (cie->is_obj_array_klass()) { 426 ndim += 1; 427 cie = cie->as_array_klass()->base_element_type(); 428 } 429 cie->print_name_on(st); 430 while (ndim-- > 0) { 431 st->print("[]"); 432 } 433 st->print("[%d]=", spobj->n_fields()); 434 } 435 st->print("{"); 436 uint nf = spobj->n_fields(); 437 if (nf > 0) { 438 uint first_ind = spobj->first_index(); 439 Node* fld_node = mcall->in(first_ind); 440 ciField* cifield; 441 if (iklass != NULL) { 442 st->print(" ["); 443 cifield = iklass->nonstatic_field_at(0); 444 cifield->print_name_on(st); 445 format_helper( regalloc, st, fld_node, ":", 0, &scobjs ); 446 } else { 447 format_helper( regalloc, st, fld_node, "[", 0, &scobjs ); 448 } 449 for (uint j = 1; j < nf; j++) { 450 fld_node = mcall->in(first_ind+j); 451 if (iklass != NULL) { 452 st->print(", ["); 453 cifield = iklass->nonstatic_field_at(j); 454 cifield->print_name_on(st); 455 format_helper( regalloc, st, fld_node, ":", j, &scobjs ); 456 } else { 457 format_helper( regalloc, st, fld_node, ", [", j, &scobjs ); 458 } 459 } 460 } 461 st->print(" }"); 462 } 463 } 464 st->print_cr(""); 465 if (caller() != NULL) caller()->format(regalloc, n, st); 466 } 467 468 469 void JVMState::dump_spec(outputStream *st) const { 470 if (_method != NULL) { 471 bool printed = false; 472 if (!Verbose) { 473 // The JVMS dumps make really, really long lines. 474 // Take out the most boring parts, which are the package prefixes. 475 char buf[500]; 476 stringStream namest(buf, sizeof(buf)); 477 _method->print_short_name(&namest); 478 if (namest.count() < sizeof(buf)) { 479 const char* name = namest.base(); 480 if (name[0] == ' ') ++name; 481 const char* endcn = strchr(name, ':'); // end of class name 482 if (endcn == NULL) endcn = strchr(name, '('); 483 if (endcn == NULL) endcn = name + strlen(name); 484 while (endcn > name && endcn[-1] != '.' && endcn[-1] != '/') 485 --endcn; 486 st->print(" %s", endcn); 487 printed = true; 488 } 489 } 490 if (!printed) 491 _method->print_short_name(st); 492 st->print(" @ bci:%d",_bci); 493 } else { 494 st->print(" runtime stub"); 495 } 496 if (caller() != NULL) caller()->dump_spec(st); 497 } 498 499 500 void JVMState::dump_on(outputStream* st) const { 501 if (_map && !((uintptr_t)_map & 1)) { 502 if (_map->len() > _map->req()) { // _map->has_exceptions() 503 Node* ex = _map->in(_map->req()); // _map->next_exception() 504 // skip the first one; it's already being printed 505 while (ex != NULL && ex->len() > ex->req()) { 506 ex = ex->in(ex->req()); // ex->next_exception() 507 ex->dump(1); 508 } 509 } 510 _map->dump(2); 511 } 512 st->print("JVMS depth=%d loc=%d stk=%d mon=%d scalar=%d end=%d mondepth=%d sp=%d bci=%d method=", 513 depth(), locoff(), stkoff(), monoff(), scloff(), endoff(), monitor_depth(), sp(), bci()); 514 if (_method == NULL) { 515 st->print_cr("(none)"); 516 } else { 517 _method->print_name(st); 518 st->cr(); 519 if (bci() >= 0 && bci() < _method->code_size()) { 520 st->print(" bc: "); 521 _method->print_codes_on(bci(), bci()+1, st); 522 } 523 } 524 if (caller() != NULL) { 525 caller()->dump_on(st); 526 } 527 } 528 529 // Extra way to dump a jvms from the debugger, 530 // to avoid a bug with C++ member function calls. 531 void dump_jvms(JVMState* jvms) { 532 jvms->dump(); 533 } 534 #endif 535 536 //--------------------------clone_shallow-------------------------------------- 537 JVMState* JVMState::clone_shallow(Compile* C) const { 538 JVMState* n = has_method() ? new (C) JVMState(_method, _caller) : new (C) JVMState(0); 539 n->set_bci(_bci); 540 n->set_locoff(_locoff); 541 n->set_stkoff(_stkoff); 542 n->set_monoff(_monoff); 543 n->set_scloff(_scloff); 544 n->set_endoff(_endoff); 545 n->set_sp(_sp); 546 n->set_map(_map); 547 return n; 548 } 549 550 //---------------------------clone_deep---------------------------------------- 551 JVMState* JVMState::clone_deep(Compile* C) const { 552 JVMState* n = clone_shallow(C); 553 for (JVMState* p = n; p->_caller != NULL; p = p->_caller) { 554 p->_caller = p->_caller->clone_shallow(C); 555 } 556 assert(n->depth() == depth(), "sanity"); 557 assert(n->debug_depth() == debug_depth(), "sanity"); 558 return n; 559 } 560 561 //============================================================================= 562 uint CallNode::cmp( const Node &n ) const 563 { return _tf == ((CallNode&)n)._tf && _jvms == ((CallNode&)n)._jvms; } 564 #ifndef PRODUCT 565 void CallNode::dump_req() const { 566 // Dump the required inputs, enclosed in '(' and ')' 567 uint i; // Exit value of loop 568 for( i=0; i<req(); i++ ) { // For all required inputs 569 if( i == TypeFunc::Parms ) tty->print("("); 570 if( in(i) ) tty->print("%c%d ", Compile::current()->node_arena()->contains(in(i)) ? ' ' : 'o', in(i)->_idx); 571 else tty->print("_ "); 572 } 573 tty->print(")"); 574 } 575 576 void CallNode::dump_spec(outputStream *st) const { 577 st->print(" "); 578 tf()->dump_on(st); 579 if (_cnt != COUNT_UNKNOWN) st->print(" C=%f",_cnt); 580 if (jvms() != NULL) jvms()->dump_spec(st); 581 } 582 #endif 583 584 const Type *CallNode::bottom_type() const { return tf()->range(); } 585 const Type *CallNode::Value(PhaseTransform *phase) const { 586 if (phase->type(in(0)) == Type::TOP) return Type::TOP; 587 return tf()->range(); 588 } 589 590 //------------------------------calling_convention----------------------------- 591 void CallNode::calling_convention( BasicType* sig_bt, VMRegPair *parm_regs, uint argcnt ) const { 592 // Use the standard compiler calling convention 593 Matcher::calling_convention( sig_bt, parm_regs, argcnt, true ); 594 } 595 596 597 //------------------------------match------------------------------------------ 598 // Construct projections for control, I/O, memory-fields, ..., and 599 // return result(s) along with their RegMask info 600 Node *CallNode::match( const ProjNode *proj, const Matcher *match ) { 601 switch (proj->_con) { 602 case TypeFunc::Control: 603 case TypeFunc::I_O: 604 case TypeFunc::Memory: 605 return new (match->C, 1) MachProjNode(this,proj->_con,RegMask::Empty,MachProjNode::unmatched_proj); 606 607 case TypeFunc::Parms+1: // For LONG & DOUBLE returns 608 assert(tf()->_range->field_at(TypeFunc::Parms+1) == Type::HALF, ""); 609 // 2nd half of doubles and longs 610 return new (match->C, 1) MachProjNode(this,proj->_con, RegMask::Empty, (uint)OptoReg::Bad); 611 612 case TypeFunc::Parms: { // Normal returns 613 uint ideal_reg = Matcher::base2reg[tf()->range()->field_at(TypeFunc::Parms)->base()]; 614 OptoRegPair regs = is_CallRuntime() 615 ? match->c_return_value(ideal_reg,true) // Calls into C runtime 616 : match-> return_value(ideal_reg,true); // Calls into compiled Java code 617 RegMask rm = RegMask(regs.first()); 618 if( OptoReg::is_valid(regs.second()) ) 619 rm.Insert( regs.second() ); 620 return new (match->C, 1) MachProjNode(this,proj->_con,rm,ideal_reg); 621 } 622 623 case TypeFunc::ReturnAdr: 624 case TypeFunc::FramePtr: 625 default: 626 ShouldNotReachHere(); 627 } 628 return NULL; 629 } 630 631 // Do we Match on this edge index or not? Match no edges 632 uint CallNode::match_edge(uint idx) const { 633 return 0; 634 } 635 636 // 637 // Determine whether the call could modify the field of the specified 638 // instance at the specified offset. 639 // 640 bool CallNode::may_modify(const TypePtr *addr_t, PhaseTransform *phase) { 641 const TypeOopPtr *adrInst_t = addr_t->isa_oopptr(); 642 643 // If not an OopPtr or not an instance type, assume the worst. 644 // Note: currently this method is called only for instance types. 645 if (adrInst_t == NULL || !adrInst_t->is_known_instance()) { 646 return true; 647 } 648 // The instance_id is set only for scalar-replaceable allocations which 649 // are not passed as arguments according to Escape Analysis. 650 return false; 651 } 652 653 // Does this call have a direct reference to n other than debug information? 654 bool CallNode::has_non_debug_use(Node *n) { 655 const TypeTuple * d = tf()->domain(); 656 for (uint i = TypeFunc::Parms; i < d->cnt(); i++) { 657 Node *arg = in(i); 658 if (arg == n) { 659 return true; 660 } 661 } 662 return false; 663 } 664 665 // Returns the unique CheckCastPP of a call 666 // or 'this' if there are several CheckCastPP 667 // or returns NULL if there is no one. 668 Node *CallNode::result_cast() { 669 Node *cast = NULL; 670 671 Node *p = proj_out(TypeFunc::Parms); 672 if (p == NULL) 673 return NULL; 674 675 for (DUIterator_Fast imax, i = p->fast_outs(imax); i < imax; i++) { 676 Node *use = p->fast_out(i); 677 if (use->is_CheckCastPP()) { 678 if (cast != NULL) { 679 return this; // more than 1 CheckCastPP 680 } 681 cast = use; 682 } 683 } 684 return cast; 685 } 686 687 688 //============================================================================= 689 uint CallJavaNode::size_of() const { return sizeof(*this); } 690 uint CallJavaNode::cmp( const Node &n ) const { 691 CallJavaNode &call = (CallJavaNode&)n; 692 return CallNode::cmp(call) && _method == call._method; 693 } 694 #ifndef PRODUCT 695 void CallJavaNode::dump_spec(outputStream *st) const { 696 if( _method ) _method->print_short_name(st); 697 CallNode::dump_spec(st); 698 } 699 #endif 700 701 //============================================================================= 702 uint CallStaticJavaNode::size_of() const { return sizeof(*this); } 703 uint CallStaticJavaNode::cmp( const Node &n ) const { 704 CallStaticJavaNode &call = (CallStaticJavaNode&)n; 705 return CallJavaNode::cmp(call); 706 } 707 708 //----------------------------uncommon_trap_request---------------------------- 709 // If this is an uncommon trap, return the request code, else zero. 710 int CallStaticJavaNode::uncommon_trap_request() const { 711 if (_name != NULL && !strcmp(_name, "uncommon_trap")) { 712 return extract_uncommon_trap_request(this); 713 } 714 return 0; 715 } 716 int CallStaticJavaNode::extract_uncommon_trap_request(const Node* call) { 717 #ifndef PRODUCT 718 if (!(call->req() > TypeFunc::Parms && 719 call->in(TypeFunc::Parms) != NULL && 720 call->in(TypeFunc::Parms)->is_Con())) { 721 assert(_in_dump_cnt != 0, "OK if dumping"); 722 tty->print("[bad uncommon trap]"); 723 return 0; 724 } 725 #endif 726 return call->in(TypeFunc::Parms)->bottom_type()->is_int()->get_con(); 727 } 728 729 #ifndef PRODUCT 730 void CallStaticJavaNode::dump_spec(outputStream *st) const { 731 st->print("# Static "); 732 if (_name != NULL) { 733 st->print("%s", _name); 734 int trap_req = uncommon_trap_request(); 735 if (trap_req != 0) { 736 char buf[100]; 737 st->print("(%s)", 738 Deoptimization::format_trap_request(buf, sizeof(buf), 739 trap_req)); 740 } 741 st->print(" "); 742 } 743 CallJavaNode::dump_spec(st); 744 } 745 #endif 746 747 //============================================================================= 748 uint CallDynamicJavaNode::size_of() const { return sizeof(*this); } 749 uint CallDynamicJavaNode::cmp( const Node &n ) const { 750 CallDynamicJavaNode &call = (CallDynamicJavaNode&)n; 751 return CallJavaNode::cmp(call); 752 } 753 #ifndef PRODUCT 754 void CallDynamicJavaNode::dump_spec(outputStream *st) const { 755 st->print("# Dynamic "); 756 CallJavaNode::dump_spec(st); 757 } 758 #endif 759 760 //============================================================================= 761 uint CallRuntimeNode::size_of() const { return sizeof(*this); } 762 uint CallRuntimeNode::cmp( const Node &n ) const { 763 CallRuntimeNode &call = (CallRuntimeNode&)n; 764 return CallNode::cmp(call) && !strcmp(_name,call._name); 765 } 766 #ifndef PRODUCT 767 void CallRuntimeNode::dump_spec(outputStream *st) const { 768 st->print("# "); 769 st->print(_name); 770 CallNode::dump_spec(st); 771 } 772 #endif 773 774 //------------------------------calling_convention----------------------------- 775 void CallRuntimeNode::calling_convention( BasicType* sig_bt, VMRegPair *parm_regs, uint argcnt ) const { 776 Matcher::c_calling_convention( sig_bt, parm_regs, argcnt ); 777 } 778 779 //============================================================================= 780 //------------------------------calling_convention----------------------------- 781 782 783 //============================================================================= 784 #ifndef PRODUCT 785 void CallLeafNode::dump_spec(outputStream *st) const { 786 st->print("# "); 787 st->print(_name); 788 CallNode::dump_spec(st); 789 } 790 #endif 791 792 //============================================================================= 793 794 void SafePointNode::set_local(JVMState* jvms, uint idx, Node *c) { 795 assert(verify_jvms(jvms), "jvms must match"); 796 int loc = jvms->locoff() + idx; 797 if (in(loc)->is_top() && idx > 0 && !c->is_top() ) { 798 // If current local idx is top then local idx - 1 could 799 // be a long/double that needs to be killed since top could 800 // represent the 2nd half ofthe long/double. 801 uint ideal = in(loc -1)->ideal_reg(); 802 if (ideal == Op_RegD || ideal == Op_RegL) { 803 // set other (low index) half to top 804 set_req(loc - 1, in(loc)); 805 } 806 } 807 set_req(loc, c); 808 } 809 810 uint SafePointNode::size_of() const { return sizeof(*this); } 811 uint SafePointNode::cmp( const Node &n ) const { 812 return (&n == this); // Always fail except on self 813 } 814 815 //-------------------------set_next_exception---------------------------------- 816 void SafePointNode::set_next_exception(SafePointNode* n) { 817 assert(n == NULL || n->Opcode() == Op_SafePoint, "correct value for next_exception"); 818 if (len() == req()) { 819 if (n != NULL) add_prec(n); 820 } else { 821 set_prec(req(), n); 822 } 823 } 824 825 826 //----------------------------next_exception----------------------------------- 827 SafePointNode* SafePointNode::next_exception() const { 828 if (len() == req()) { 829 return NULL; 830 } else { 831 Node* n = in(req()); 832 assert(n == NULL || n->Opcode() == Op_SafePoint, "no other uses of prec edges"); 833 return (SafePointNode*) n; 834 } 835 } 836 837 838 //------------------------------Ideal------------------------------------------ 839 // Skip over any collapsed Regions 840 Node *SafePointNode::Ideal(PhaseGVN *phase, bool can_reshape) { 841 return remove_dead_region(phase, can_reshape) ? this : NULL; 842 } 843 844 //------------------------------Identity--------------------------------------- 845 // Remove obviously duplicate safepoints 846 Node *SafePointNode::Identity( PhaseTransform *phase ) { 847 848 // If you have back to back safepoints, remove one 849 if( in(TypeFunc::Control)->is_SafePoint() ) 850 return in(TypeFunc::Control); 851 852 if( in(0)->is_Proj() ) { 853 Node *n0 = in(0)->in(0); 854 // Check if he is a call projection (except Leaf Call) 855 if( n0->is_Catch() ) { 856 n0 = n0->in(0)->in(0); 857 assert( n0->is_Call(), "expect a call here" ); 858 } 859 if( n0->is_Call() && n0->as_Call()->guaranteed_safepoint() ) { 860 // Useless Safepoint, so remove it 861 return in(TypeFunc::Control); 862 } 863 } 864 865 return this; 866 } 867 868 //------------------------------Value------------------------------------------ 869 const Type *SafePointNode::Value( PhaseTransform *phase ) const { 870 if( phase->type(in(0)) == Type::TOP ) return Type::TOP; 871 if( phase->eqv( in(0), this ) ) return Type::TOP; // Dead infinite loop 872 return Type::CONTROL; 873 } 874 875 #ifndef PRODUCT 876 void SafePointNode::dump_spec(outputStream *st) const { 877 st->print(" SafePoint "); 878 } 879 #endif 880 881 const RegMask &SafePointNode::in_RegMask(uint idx) const { 882 if( idx < TypeFunc::Parms ) return RegMask::Empty; 883 // Values outside the domain represent debug info 884 return *(Compile::current()->matcher()->idealreg2debugmask[in(idx)->ideal_reg()]); 885 } 886 const RegMask &SafePointNode::out_RegMask() const { 887 return RegMask::Empty; 888 } 889 890 891 void SafePointNode::grow_stack(JVMState* jvms, uint grow_by) { 892 assert((int)grow_by > 0, "sanity"); 893 int monoff = jvms->monoff(); 894 int scloff = jvms->scloff(); 895 int endoff = jvms->endoff(); 896 assert(endoff == (int)req(), "no other states or debug info after me"); 897 Node* top = Compile::current()->top(); 898 for (uint i = 0; i < grow_by; i++) { 899 ins_req(monoff, top); 900 } 901 jvms->set_monoff(monoff + grow_by); 902 jvms->set_scloff(scloff + grow_by); 903 jvms->set_endoff(endoff + grow_by); 904 } 905 906 void SafePointNode::push_monitor(const FastLockNode *lock) { 907 // Add a LockNode, which points to both the original BoxLockNode (the 908 // stack space for the monitor) and the Object being locked. 909 const int MonitorEdges = 2; 910 assert(JVMState::logMonitorEdges == exact_log2(MonitorEdges), "correct MonitorEdges"); 911 assert(req() == jvms()->endoff(), "correct sizing"); 912 int nextmon = jvms()->scloff(); 913 if (GenerateSynchronizationCode) { 914 add_req(lock->box_node()); 915 add_req(lock->obj_node()); 916 } else { 917 Node* top = Compile::current()->top(); 918 add_req(top); 919 add_req(top); 920 } 921 jvms()->set_scloff(nextmon+MonitorEdges); 922 jvms()->set_endoff(req()); 923 } 924 925 void SafePointNode::pop_monitor() { 926 // Delete last monitor from debug info 927 debug_only(int num_before_pop = jvms()->nof_monitors()); 928 const int MonitorEdges = (1<<JVMState::logMonitorEdges); 929 int scloff = jvms()->scloff(); 930 int endoff = jvms()->endoff(); 931 int new_scloff = scloff - MonitorEdges; 932 int new_endoff = endoff - MonitorEdges; 933 jvms()->set_scloff(new_scloff); 934 jvms()->set_endoff(new_endoff); 935 while (scloff > new_scloff) del_req(--scloff); 936 assert(jvms()->nof_monitors() == num_before_pop-1, ""); 937 } 938 939 Node *SafePointNode::peek_monitor_box() const { 940 int mon = jvms()->nof_monitors() - 1; 941 assert(mon >= 0, "most have a monitor"); 942 return monitor_box(jvms(), mon); 943 } 944 945 Node *SafePointNode::peek_monitor_obj() const { 946 int mon = jvms()->nof_monitors() - 1; 947 assert(mon >= 0, "most have a monitor"); 948 return monitor_obj(jvms(), mon); 949 } 950 951 // Do we Match on this edge index or not? Match no edges 952 uint SafePointNode::match_edge(uint idx) const { 953 if( !needs_polling_address_input() ) 954 return 0; 955 956 return (TypeFunc::Parms == idx); 957 } 958 959 //============== SafePointScalarObjectNode ============== 960 961 SafePointScalarObjectNode::SafePointScalarObjectNode(const TypeOopPtr* tp, 962 #ifdef ASSERT 963 AllocateNode* alloc, 964 #endif 965 uint first_index, 966 uint n_fields) : 967 TypeNode(tp, 1), // 1 control input -- seems required. Get from root. 968 #ifdef ASSERT 969 _alloc(alloc), 970 #endif 971 _first_index(first_index), 972 _n_fields(n_fields) 973 { 974 init_class_id(Class_SafePointScalarObject); 975 } 976 977 bool SafePointScalarObjectNode::pinned() const { return true; } 978 bool SafePointScalarObjectNode::depends_only_on_test() const { return false; } 979 980 uint SafePointScalarObjectNode::ideal_reg() const { 981 return 0; // No matching to machine instruction 982 } 983 984 const RegMask &SafePointScalarObjectNode::in_RegMask(uint idx) const { 985 return *(Compile::current()->matcher()->idealreg2debugmask[in(idx)->ideal_reg()]); 986 } 987 988 const RegMask &SafePointScalarObjectNode::out_RegMask() const { 989 return RegMask::Empty; 990 } 991 992 uint SafePointScalarObjectNode::match_edge(uint idx) const { 993 return 0; 994 } 995 996 SafePointScalarObjectNode* 997 SafePointScalarObjectNode::clone(int jvms_adj, Dict* sosn_map) const { 998 void* cached = (*sosn_map)[(void*)this]; 999 if (cached != NULL) { 1000 return (SafePointScalarObjectNode*)cached; 1001 } 1002 Compile* C = Compile::current(); 1003 SafePointScalarObjectNode* res = (SafePointScalarObjectNode*)Node::clone(); 1004 res->_first_index += jvms_adj; 1005 sosn_map->Insert((void*)this, (void*)res); 1006 return res; 1007 } 1008 1009 1010 #ifndef PRODUCT 1011 void SafePointScalarObjectNode::dump_spec(outputStream *st) const { 1012 st->print(" # fields@[%d..%d]", first_index(), 1013 first_index() + n_fields() - 1); 1014 } 1015 1016 #endif 1017 1018 //============================================================================= 1019 uint AllocateNode::size_of() const { return sizeof(*this); } 1020 1021 AllocateNode::AllocateNode(Compile* C, const TypeFunc *atype, 1022 Node *ctrl, Node *mem, Node *abio, 1023 Node *size, Node *klass_node, Node *initial_test) 1024 : CallNode(atype, NULL, TypeRawPtr::BOTTOM) 1025 { 1026 init_class_id(Class_Allocate); 1027 init_flags(Flag_is_macro); 1028 _is_scalar_replaceable = false; 1029 Node *topnode = C->top(); 1030 1031 init_req( TypeFunc::Control , ctrl ); 1032 init_req( TypeFunc::I_O , abio ); 1033 init_req( TypeFunc::Memory , mem ); 1034 init_req( TypeFunc::ReturnAdr, topnode ); 1035 init_req( TypeFunc::FramePtr , topnode ); 1036 init_req( AllocSize , size); 1037 init_req( KlassNode , klass_node); 1038 init_req( InitialTest , initial_test); 1039 init_req( ALength , topnode); 1040 C->add_macro_node(this); 1041 } 1042 1043 //============================================================================= 1044 uint AllocateArrayNode::size_of() const { return sizeof(*this); } 1045 1046 Node* AllocateArrayNode::Ideal(PhaseGVN *phase, bool can_reshape) { 1047 if (remove_dead_region(phase, can_reshape)) return this; 1048 1049 const Type* type = phase->type(Ideal_length()); 1050 if (type->isa_int() && type->is_int()->_hi < 0) { 1051 if (can_reshape) { 1052 PhaseIterGVN *igvn = phase->is_IterGVN(); 1053 // Unreachable fall through path (negative array length), 1054 // the allocation can only throw so disconnect it. 1055 Node* proj = proj_out(TypeFunc::Control); 1056 Node* catchproj = NULL; 1057 if (proj != NULL) { 1058 for (DUIterator_Fast imax, i = proj->fast_outs(imax); i < imax; i++) { 1059 Node *cn = proj->fast_out(i); 1060 if (cn->is_Catch()) { 1061 catchproj = cn->as_Multi()->proj_out(CatchProjNode::fall_through_index); 1062 break; 1063 } 1064 } 1065 } 1066 if (catchproj != NULL && catchproj->outcnt() > 0 && 1067 (catchproj->outcnt() > 1 || 1068 catchproj->unique_out()->Opcode() != Op_Halt)) { 1069 assert(catchproj->is_CatchProj(), "must be a CatchProjNode"); 1070 Node* nproj = catchproj->clone(); 1071 igvn->register_new_node_with_optimizer(nproj); 1072 1073 Node *frame = new (phase->C, 1) ParmNode( phase->C->start(), TypeFunc::FramePtr ); 1074 frame = phase->transform(frame); 1075 // Halt & Catch Fire 1076 Node *halt = new (phase->C, TypeFunc::Parms) HaltNode( nproj, frame ); 1077 phase->C->root()->add_req(halt); 1078 phase->transform(halt); 1079 1080 igvn->replace_node(catchproj, phase->C->top()); 1081 return this; 1082 } 1083 } else { 1084 // Can't correct it during regular GVN so register for IGVN 1085 phase->C->record_for_igvn(this); 1086 } 1087 } 1088 return NULL; 1089 } 1090 1091 // Retrieve the length from the AllocateArrayNode. Narrow the type with a 1092 // CastII, if appropriate. If we are not allowed to create new nodes, and 1093 // a CastII is appropriate, return NULL. 1094 Node *AllocateArrayNode::make_ideal_length(const TypeOopPtr* oop_type, PhaseTransform *phase, bool allow_new_nodes) { 1095 Node *length = in(AllocateNode::ALength); 1096 assert(length != NULL, "length is not null"); 1097 1098 const TypeInt* length_type = phase->find_int_type(length); 1099 const TypeAryPtr* ary_type = oop_type->isa_aryptr(); 1100 1101 if (ary_type != NULL && length_type != NULL) { 1102 const TypeInt* narrow_length_type = ary_type->narrow_size_type(length_type); 1103 if (narrow_length_type != length_type) { 1104 // Assert one of: 1105 // - the narrow_length is 0 1106 // - the narrow_length is not wider than length 1107 assert(narrow_length_type == TypeInt::ZERO || 1108 (narrow_length_type->_hi <= length_type->_hi && 1109 narrow_length_type->_lo >= length_type->_lo), 1110 "narrow type must be narrower than length type"); 1111 1112 // Return NULL if new nodes are not allowed 1113 if (!allow_new_nodes) return NULL; 1114 // Create a cast which is control dependent on the initialization to 1115 // propagate the fact that the array length must be positive. 1116 length = new (phase->C, 2) CastIINode(length, narrow_length_type); 1117 length->set_req(0, initialization()->proj_out(0)); 1118 } 1119 } 1120 1121 return length; 1122 } 1123 1124 //============================================================================= 1125 uint LockNode::size_of() const { return sizeof(*this); } 1126 1127 // Redundant lock elimination 1128 // 1129 // There are various patterns of locking where we release and 1130 // immediately reacquire a lock in a piece of code where no operations 1131 // occur in between that would be observable. In those cases we can 1132 // skip releasing and reacquiring the lock without violating any 1133 // fairness requirements. Doing this around a loop could cause a lock 1134 // to be held for a very long time so we concentrate on non-looping 1135 // control flow. We also require that the operations are fully 1136 // redundant meaning that we don't introduce new lock operations on 1137 // some paths so to be able to eliminate it on others ala PRE. This 1138 // would probably require some more extensive graph manipulation to 1139 // guarantee that the memory edges were all handled correctly. 1140 // 1141 // Assuming p is a simple predicate which can't trap in any way and s 1142 // is a synchronized method consider this code: 1143 // 1144 // s(); 1145 // if (p) 1146 // s(); 1147 // else 1148 // s(); 1149 // s(); 1150 // 1151 // 1. The unlocks of the first call to s can be eliminated if the 1152 // locks inside the then and else branches are eliminated. 1153 // 1154 // 2. The unlocks of the then and else branches can be eliminated if 1155 // the lock of the final call to s is eliminated. 1156 // 1157 // Either of these cases subsumes the simple case of sequential control flow 1158 // 1159 // Addtionally we can eliminate versions without the else case: 1160 // 1161 // s(); 1162 // if (p) 1163 // s(); 1164 // s(); 1165 // 1166 // 3. In this case we eliminate the unlock of the first s, the lock 1167 // and unlock in the then case and the lock in the final s. 1168 // 1169 // Note also that in all these cases the then/else pieces don't have 1170 // to be trivial as long as they begin and end with synchronization 1171 // operations. 1172 // 1173 // s(); 1174 // if (p) 1175 // s(); 1176 // f(); 1177 // s(); 1178 // s(); 1179 // 1180 // The code will work properly for this case, leaving in the unlock 1181 // before the call to f and the relock after it. 1182 // 1183 // A potentially interesting case which isn't handled here is when the 1184 // locking is partially redundant. 1185 // 1186 // s(); 1187 // if (p) 1188 // s(); 1189 // 1190 // This could be eliminated putting unlocking on the else case and 1191 // eliminating the first unlock and the lock in the then side. 1192 // Alternatively the unlock could be moved out of the then side so it 1193 // was after the merge and the first unlock and second lock 1194 // eliminated. This might require less manipulation of the memory 1195 // state to get correct. 1196 // 1197 // Additionally we might allow work between a unlock and lock before 1198 // giving up eliminating the locks. The current code disallows any 1199 // conditional control flow between these operations. A formulation 1200 // similar to partial redundancy elimination computing the 1201 // availability of unlocking and the anticipatability of locking at a 1202 // program point would allow detection of fully redundant locking with 1203 // some amount of work in between. I'm not sure how often I really 1204 // think that would occur though. Most of the cases I've seen 1205 // indicate it's likely non-trivial work would occur in between. 1206 // There may be other more complicated constructs where we could 1207 // eliminate locking but I haven't seen any others appear as hot or 1208 // interesting. 1209 // 1210 // Locking and unlocking have a canonical form in ideal that looks 1211 // roughly like this: 1212 // 1213 // <obj> 1214 // | \\------+ 1215 // | \ \ 1216 // | BoxLock \ 1217 // | | | \ 1218 // | | \ \ 1219 // | | FastLock 1220 // | | / 1221 // | | / 1222 // | | | 1223 // 1224 // Lock 1225 // | 1226 // Proj #0 1227 // | 1228 // MembarAcquire 1229 // | 1230 // Proj #0 1231 // 1232 // MembarRelease 1233 // | 1234 // Proj #0 1235 // | 1236 // Unlock 1237 // | 1238 // Proj #0 1239 // 1240 // 1241 // This code proceeds by processing Lock nodes during PhaseIterGVN 1242 // and searching back through its control for the proper code 1243 // patterns. Once it finds a set of lock and unlock operations to 1244 // eliminate they are marked as eliminatable which causes the 1245 // expansion of the Lock and Unlock macro nodes to make the operation a NOP 1246 // 1247 //============================================================================= 1248 1249 // 1250 // Utility function to skip over uninteresting control nodes. Nodes skipped are: 1251 // - copy regions. (These may not have been optimized away yet.) 1252 // - eliminated locking nodes 1253 // 1254 static Node *next_control(Node *ctrl) { 1255 if (ctrl == NULL) 1256 return NULL; 1257 while (1) { 1258 if (ctrl->is_Region()) { 1259 RegionNode *r = ctrl->as_Region(); 1260 Node *n = r->is_copy(); 1261 if (n == NULL) 1262 break; // hit a region, return it 1263 else 1264 ctrl = n; 1265 } else if (ctrl->is_Proj()) { 1266 Node *in0 = ctrl->in(0); 1267 if (in0->is_AbstractLock() && in0->as_AbstractLock()->is_eliminated()) { 1268 ctrl = in0->in(0); 1269 } else { 1270 break; 1271 } 1272 } else { 1273 break; // found an interesting control 1274 } 1275 } 1276 return ctrl; 1277 } 1278 // 1279 // Given a control, see if it's the control projection of an Unlock which 1280 // operating on the same object as lock. 1281 // 1282 bool AbstractLockNode::find_matching_unlock(const Node* ctrl, LockNode* lock, 1283 GrowableArray<AbstractLockNode*> &lock_ops) { 1284 ProjNode *ctrl_proj = (ctrl->is_Proj()) ? ctrl->as_Proj() : NULL; 1285 if (ctrl_proj != NULL && ctrl_proj->_con == TypeFunc::Control) { 1286 Node *n = ctrl_proj->in(0); 1287 if (n != NULL && n->is_Unlock()) { 1288 UnlockNode *unlock = n->as_Unlock(); 1289 if ((lock->obj_node() == unlock->obj_node()) && 1290 (lock->box_node() == unlock->box_node()) && !unlock->is_eliminated()) { 1291 lock_ops.append(unlock); 1292 return true; 1293 } 1294 } 1295 } 1296 return false; 1297 } 1298 1299 // 1300 // Find the lock matching an unlock. Returns null if a safepoint 1301 // or complicated control is encountered first. 1302 LockNode *AbstractLockNode::find_matching_lock(UnlockNode* unlock) { 1303 LockNode *lock_result = NULL; 1304 // find the matching lock, or an intervening safepoint 1305 Node *ctrl = next_control(unlock->in(0)); 1306 while (1) { 1307 assert(ctrl != NULL, "invalid control graph"); 1308 assert(!ctrl->is_Start(), "missing lock for unlock"); 1309 if (ctrl->is_top()) break; // dead control path 1310 if (ctrl->is_Proj()) ctrl = ctrl->in(0); 1311 if (ctrl->is_SafePoint()) { 1312 break; // found a safepoint (may be the lock we are searching for) 1313 } else if (ctrl->is_Region()) { 1314 // Check for a simple diamond pattern. Punt on anything more complicated 1315 if (ctrl->req() == 3 && ctrl->in(1) != NULL && ctrl->in(2) != NULL) { 1316 Node *in1 = next_control(ctrl->in(1)); 1317 Node *in2 = next_control(ctrl->in(2)); 1318 if (((in1->is_IfTrue() && in2->is_IfFalse()) || 1319 (in2->is_IfTrue() && in1->is_IfFalse())) && (in1->in(0) == in2->in(0))) { 1320 ctrl = next_control(in1->in(0)->in(0)); 1321 } else { 1322 break; 1323 } 1324 } else { 1325 break; 1326 } 1327 } else { 1328 ctrl = next_control(ctrl->in(0)); // keep searching 1329 } 1330 } 1331 if (ctrl->is_Lock()) { 1332 LockNode *lock = ctrl->as_Lock(); 1333 if ((lock->obj_node() == unlock->obj_node()) && 1334 (lock->box_node() == unlock->box_node())) { 1335 lock_result = lock; 1336 } 1337 } 1338 return lock_result; 1339 } 1340 1341 // This code corresponds to case 3 above. 1342 1343 bool AbstractLockNode::find_lock_and_unlock_through_if(Node* node, LockNode* lock, 1344 GrowableArray<AbstractLockNode*> &lock_ops) { 1345 Node* if_node = node->in(0); 1346 bool if_true = node->is_IfTrue(); 1347 1348 if (if_node->is_If() && if_node->outcnt() == 2 && (if_true || node->is_IfFalse())) { 1349 Node *lock_ctrl = next_control(if_node->in(0)); 1350 if (find_matching_unlock(lock_ctrl, lock, lock_ops)) { 1351 Node* lock1_node = NULL; 1352 ProjNode* proj = if_node->as_If()->proj_out(!if_true); 1353 if (if_true) { 1354 if (proj->is_IfFalse() && proj->outcnt() == 1) { 1355 lock1_node = proj->unique_out(); 1356 } 1357 } else { 1358 if (proj->is_IfTrue() && proj->outcnt() == 1) { 1359 lock1_node = proj->unique_out(); 1360 } 1361 } 1362 if (lock1_node != NULL && lock1_node->is_Lock()) { 1363 LockNode *lock1 = lock1_node->as_Lock(); 1364 if ((lock->obj_node() == lock1->obj_node()) && 1365 (lock->box_node() == lock1->box_node()) && !lock1->is_eliminated()) { 1366 lock_ops.append(lock1); 1367 return true; 1368 } 1369 } 1370 } 1371 } 1372 1373 lock_ops.trunc_to(0); 1374 return false; 1375 } 1376 1377 bool AbstractLockNode::find_unlocks_for_region(const RegionNode* region, LockNode* lock, 1378 GrowableArray<AbstractLockNode*> &lock_ops) { 1379 // check each control merging at this point for a matching unlock. 1380 // in(0) should be self edge so skip it. 1381 for (int i = 1; i < (int)region->req(); i++) { 1382 Node *in_node = next_control(region->in(i)); 1383 if (in_node != NULL) { 1384 if (find_matching_unlock(in_node, lock, lock_ops)) { 1385 // found a match so keep on checking. 1386 continue; 1387 } else if (find_lock_and_unlock_through_if(in_node, lock, lock_ops)) { 1388 continue; 1389 } 1390 1391 // If we fall through to here then it was some kind of node we 1392 // don't understand or there wasn't a matching unlock, so give 1393 // up trying to merge locks. 1394 lock_ops.trunc_to(0); 1395 return false; 1396 } 1397 } 1398 return true; 1399 1400 } 1401 1402 #ifndef PRODUCT 1403 // 1404 // Create a counter which counts the number of times this lock is acquired 1405 // 1406 void AbstractLockNode::create_lock_counter(JVMState* state) { 1407 _counter = OptoRuntime::new_named_counter(state, NamedCounter::LockCounter); 1408 } 1409 #endif 1410 1411 void AbstractLockNode::set_eliminated() { 1412 _eliminate = true; 1413 #ifndef PRODUCT 1414 if (_counter) { 1415 // Update the counter to indicate that this lock was eliminated. 1416 // The counter update code will stay around even though the 1417 // optimizer will eliminate the lock operation itself. 1418 _counter->set_tag(NamedCounter::EliminatedLockCounter); 1419 } 1420 #endif 1421 } 1422 1423 //============================================================================= 1424 Node *LockNode::Ideal(PhaseGVN *phase, bool can_reshape) { 1425 1426 // perform any generic optimizations first (returns 'this' or NULL) 1427 Node *result = SafePointNode::Ideal(phase, can_reshape); 1428 1429 // Now see if we can optimize away this lock. We don't actually 1430 // remove the locking here, we simply set the _eliminate flag which 1431 // prevents macro expansion from expanding the lock. Since we don't 1432 // modify the graph, the value returned from this function is the 1433 // one computed above. 1434 if (result == NULL && can_reshape && EliminateLocks && !is_eliminated()) { 1435 // 1436 // If we are locking an unescaped object, the lock/unlock is unnecessary 1437 // 1438 ConnectionGraph *cgr = phase->C->congraph(); 1439 PointsToNode::EscapeState es = PointsToNode::GlobalEscape; 1440 if (cgr != NULL) 1441 es = cgr->escape_state(obj_node(), phase); 1442 if (es != PointsToNode::UnknownEscape && es != PointsToNode::GlobalEscape) { 1443 // Mark it eliminated to update any counters 1444 this->set_eliminated(); 1445 return result; 1446 } 1447 1448 // 1449 // Try lock coarsening 1450 // 1451 PhaseIterGVN* iter = phase->is_IterGVN(); 1452 if (iter != NULL) { 1453 1454 GrowableArray<AbstractLockNode*> lock_ops; 1455 1456 Node *ctrl = next_control(in(0)); 1457 1458 // now search back for a matching Unlock 1459 if (find_matching_unlock(ctrl, this, lock_ops)) { 1460 // found an unlock directly preceding this lock. This is the 1461 // case of single unlock directly control dependent on a 1462 // single lock which is the trivial version of case 1 or 2. 1463 } else if (ctrl->is_Region() ) { 1464 if (find_unlocks_for_region(ctrl->as_Region(), this, lock_ops)) { 1465 // found lock preceded by multiple unlocks along all paths 1466 // joining at this point which is case 3 in description above. 1467 } 1468 } else { 1469 // see if this lock comes from either half of an if and the 1470 // predecessors merges unlocks and the other half of the if 1471 // performs a lock. 1472 if (find_lock_and_unlock_through_if(ctrl, this, lock_ops)) { 1473 // found unlock splitting to an if with locks on both branches. 1474 } 1475 } 1476 1477 if (lock_ops.length() > 0) { 1478 // add ourselves to the list of locks to be eliminated. 1479 lock_ops.append(this); 1480 1481 #ifndef PRODUCT 1482 if (PrintEliminateLocks) { 1483 int locks = 0; 1484 int unlocks = 0; 1485 for (int i = 0; i < lock_ops.length(); i++) { 1486 AbstractLockNode* lock = lock_ops.at(i); 1487 if (lock->Opcode() == Op_Lock) 1488 locks++; 1489 else 1490 unlocks++; 1491 if (Verbose) { 1492 lock->dump(1); 1493 } 1494 } 1495 tty->print_cr("***Eliminated %d unlocks and %d locks", unlocks, locks); 1496 } 1497 #endif 1498 1499 // for each of the identified locks, mark them 1500 // as eliminatable 1501 for (int i = 0; i < lock_ops.length(); i++) { 1502 AbstractLockNode* lock = lock_ops.at(i); 1503 1504 // Mark it eliminated to update any counters 1505 lock->set_eliminated(); 1506 lock->set_coarsened(); 1507 } 1508 } else if (result != NULL && ctrl->is_Region() && 1509 iter->_worklist.member(ctrl)) { 1510 // We weren't able to find any opportunities but the region this 1511 // lock is control dependent on hasn't been processed yet so put 1512 // this lock back on the worklist so we can check again once any 1513 // region simplification has occurred. 1514 iter->_worklist.push(this); 1515 } 1516 } 1517 } 1518 1519 return result; 1520 } 1521 1522 //============================================================================= 1523 uint UnlockNode::size_of() const { return sizeof(*this); } 1524 1525 //============================================================================= 1526 Node *UnlockNode::Ideal(PhaseGVN *phase, bool can_reshape) { 1527 1528 // perform any generic optimizations first (returns 'this' or NULL) 1529 Node * result = SafePointNode::Ideal(phase, can_reshape); 1530 1531 // Now see if we can optimize away this unlock. We don't actually 1532 // remove the unlocking here, we simply set the _eliminate flag which 1533 // prevents macro expansion from expanding the unlock. Since we don't 1534 // modify the graph, the value returned from this function is the 1535 // one computed above. 1536 // Escape state is defined after Parse phase. 1537 if (result == NULL && can_reshape && EliminateLocks && !is_eliminated()) { 1538 // 1539 // If we are unlocking an unescaped object, the lock/unlock is unnecessary. 1540 // 1541 ConnectionGraph *cgr = phase->C->congraph(); 1542 PointsToNode::EscapeState es = PointsToNode::GlobalEscape; 1543 if (cgr != NULL) 1544 es = cgr->escape_state(obj_node(), phase); 1545 if (es != PointsToNode::UnknownEscape && es != PointsToNode::GlobalEscape) { 1546 // Mark it eliminated to update any counters 1547 this->set_eliminated(); 1548 } 1549 } 1550 return result; 1551 }