annotate src/share/vm/opto/callnode.cpp @ 0:a61af66fc99e

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