1 /* 2 * Copyright (c) 2005, 2022, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "compiler/compileLog.hpp" 27 #include "gc/shared/collectedHeap.inline.hpp" 28 #include "gc/shared/tlab_globals.hpp" 29 #include "libadt/vectset.hpp" 30 #include "memory/universe.hpp" 31 #include "opto/addnode.hpp" 32 #include "opto/arraycopynode.hpp" 33 #include "opto/callnode.hpp" 34 #include "opto/castnode.hpp" 35 #include "opto/cfgnode.hpp" 36 #include "opto/compile.hpp" 37 #include "opto/convertnode.hpp" 38 #include "opto/graphKit.hpp" 39 #include "opto/intrinsicnode.hpp" 40 #include "opto/locknode.hpp" 41 #include "opto/loopnode.hpp" 42 #include "opto/macro.hpp" 43 #include "opto/memnode.hpp" 44 #include "opto/narrowptrnode.hpp" 45 #include "opto/node.hpp" 46 #include "opto/opaquenode.hpp" 47 #include "opto/phaseX.hpp" 48 #include "opto/rootnode.hpp" 49 #include "opto/runtime.hpp" 50 #include "opto/subnode.hpp" 51 #include "opto/subtypenode.hpp" 52 #include "opto/type.hpp" 53 #include "prims/jvmtiExport.hpp" 54 #include "runtime/continuation.hpp" 55 #include "runtime/sharedRuntime.hpp" 56 #include "utilities/macros.hpp" 57 #include "utilities/powerOfTwo.hpp" 58 #if INCLUDE_G1GC 59 #include "gc/g1/g1ThreadLocalData.hpp" 60 #endif // INCLUDE_G1GC 61 62 63 // 64 // Replace any references to "oldref" in inputs to "use" with "newref". 65 // Returns the number of replacements made. 66 // 67 int PhaseMacroExpand::replace_input(Node *use, Node *oldref, Node *newref) { 68 int nreplacements = 0; 69 uint req = use->req(); 70 for (uint j = 0; j < use->len(); j++) { 71 Node *uin = use->in(j); 72 if (uin == oldref) { 73 if (j < req) 74 use->set_req(j, newref); 75 else 76 use->set_prec(j, newref); 77 nreplacements++; 78 } else if (j >= req && uin == NULL) { 79 break; 80 } 81 } 82 return nreplacements; 83 } 84 85 void PhaseMacroExpand::migrate_outs(Node *old, Node *target) { 86 assert(old != NULL, "sanity"); 87 for (DUIterator_Fast imax, i = old->fast_outs(imax); i < imax; i++) { 88 Node* use = old->fast_out(i); 89 _igvn.rehash_node_delayed(use); 90 imax -= replace_input(use, old, target); 91 // back up iterator 92 --i; 93 } 94 assert(old->outcnt() == 0, "all uses must be deleted"); 95 } 96 97 Node* PhaseMacroExpand::opt_bits_test(Node* ctrl, Node* region, int edge, Node* word, int mask, int bits, bool return_fast_path) { 98 Node* cmp; 99 if (mask != 0) { 100 Node* and_node = transform_later(new AndXNode(word, MakeConX(mask))); 101 cmp = transform_later(new CmpXNode(and_node, MakeConX(bits))); 102 } else { 103 cmp = word; 104 } 105 Node* bol = transform_later(new BoolNode(cmp, BoolTest::ne)); 106 IfNode* iff = new IfNode( ctrl, bol, PROB_MIN, COUNT_UNKNOWN ); 107 transform_later(iff); 108 109 // Fast path taken. 110 Node *fast_taken = transform_later(new IfFalseNode(iff)); 111 112 // Fast path not-taken, i.e. slow path 113 Node *slow_taken = transform_later(new IfTrueNode(iff)); 114 115 if (return_fast_path) { 116 region->init_req(edge, slow_taken); // Capture slow-control 117 return fast_taken; 118 } else { 119 region->init_req(edge, fast_taken); // Capture fast-control 120 return slow_taken; 121 } 122 } 123 124 //--------------------copy_predefined_input_for_runtime_call-------------------- 125 void PhaseMacroExpand::copy_predefined_input_for_runtime_call(Node * ctrl, CallNode* oldcall, CallNode* call) { 126 // Set fixed predefined input arguments 127 call->init_req( TypeFunc::Control, ctrl ); 128 call->init_req( TypeFunc::I_O , oldcall->in( TypeFunc::I_O) ); 129 call->init_req( TypeFunc::Memory , oldcall->in( TypeFunc::Memory ) ); // ????? 130 call->init_req( TypeFunc::ReturnAdr, oldcall->in( TypeFunc::ReturnAdr ) ); 131 call->init_req( TypeFunc::FramePtr, oldcall->in( TypeFunc::FramePtr ) ); 132 } 133 134 //------------------------------make_slow_call--------------------------------- 135 CallNode* PhaseMacroExpand::make_slow_call(CallNode *oldcall, const TypeFunc* slow_call_type, 136 address slow_call, const char* leaf_name, Node* slow_path, 137 Node* parm0, Node* parm1, Node* parm2) { 138 139 // Slow-path call 140 CallNode *call = leaf_name 141 ? (CallNode*)new CallLeafNode ( slow_call_type, slow_call, leaf_name, TypeRawPtr::BOTTOM ) 142 : (CallNode*)new CallStaticJavaNode( slow_call_type, slow_call, OptoRuntime::stub_name(slow_call), TypeRawPtr::BOTTOM ); 143 144 // Slow path call has no side-effects, uses few values 145 copy_predefined_input_for_runtime_call(slow_path, oldcall, call ); 146 if (parm0 != NULL) call->init_req(TypeFunc::Parms+0, parm0); 147 if (parm1 != NULL) call->init_req(TypeFunc::Parms+1, parm1); 148 if (parm2 != NULL) call->init_req(TypeFunc::Parms+2, parm2); 149 call->copy_call_debug_info(&_igvn, oldcall); 150 call->set_cnt(PROB_UNLIKELY_MAG(4)); // Same effect as RC_UNCOMMON. 151 _igvn.replace_node(oldcall, call); 152 transform_later(call); 153 154 return call; 155 } 156 157 void PhaseMacroExpand::eliminate_gc_barrier(Node* p2x) { 158 BarrierSetC2 *bs = BarrierSet::barrier_set()->barrier_set_c2(); 159 bs->eliminate_gc_barrier(this, p2x); 160 #ifndef PRODUCT 161 if (PrintOptoStatistics) { 162 Atomic::inc(&PhaseMacroExpand::_GC_barriers_removed_counter); 163 } 164 #endif 165 } 166 167 // Search for a memory operation for the specified memory slice. 168 static Node *scan_mem_chain(Node *mem, int alias_idx, int offset, Node *start_mem, Node *alloc, PhaseGVN *phase) { 169 Node *orig_mem = mem; 170 Node *alloc_mem = alloc->in(TypeFunc::Memory); 171 const TypeOopPtr *tinst = phase->C->get_adr_type(alias_idx)->isa_oopptr(); 172 while (true) { 173 if (mem == alloc_mem || mem == start_mem ) { 174 return mem; // hit one of our sentinels 175 } else if (mem->is_MergeMem()) { 176 mem = mem->as_MergeMem()->memory_at(alias_idx); 177 } else if (mem->is_Proj() && mem->as_Proj()->_con == TypeFunc::Memory) { 178 Node *in = mem->in(0); 179 // we can safely skip over safepoints, calls, locks and membars because we 180 // already know that the object is safe to eliminate. 181 if (in->is_Initialize() && in->as_Initialize()->allocation() == alloc) { 182 return in; 183 } else if (in->is_Call()) { 184 CallNode *call = in->as_Call(); 185 if (call->may_modify(tinst, phase)) { 186 assert(call->is_ArrayCopy(), "ArrayCopy is the only call node that doesn't make allocation escape"); 187 if (call->as_ArrayCopy()->modifies(offset, offset, phase, false)) { 188 return in; 189 } 190 } 191 mem = in->in(TypeFunc::Memory); 192 } else if (in->is_MemBar()) { 193 ArrayCopyNode* ac = NULL; 194 if (ArrayCopyNode::may_modify(tinst, in->as_MemBar(), phase, ac)) { 195 if (ac != NULL) { 196 assert(ac->is_clonebasic(), "Only basic clone is a non escaping clone"); 197 return ac; 198 } 199 } 200 mem = in->in(TypeFunc::Memory); 201 } else { 202 #ifdef ASSERT 203 in->dump(); 204 mem->dump(); 205 assert(false, "unexpected projection"); 206 #endif 207 } 208 } else if (mem->is_Store()) { 209 const TypePtr* atype = mem->as_Store()->adr_type(); 210 int adr_idx = phase->C->get_alias_index(atype); 211 if (adr_idx == alias_idx) { 212 assert(atype->isa_oopptr(), "address type must be oopptr"); 213 int adr_offset = atype->offset(); 214 uint adr_iid = atype->is_oopptr()->instance_id(); 215 // Array elements references have the same alias_idx 216 // but different offset and different instance_id. 217 if (adr_offset == offset && adr_iid == alloc->_idx) { 218 return mem; 219 } 220 } else { 221 assert(adr_idx == Compile::AliasIdxRaw, "address must match or be raw"); 222 } 223 mem = mem->in(MemNode::Memory); 224 } else if (mem->is_ClearArray()) { 225 if (!ClearArrayNode::step_through(&mem, alloc->_idx, phase)) { 226 // Can not bypass initialization of the instance 227 // we are looking. 228 debug_only(intptr_t offset;) 229 assert(alloc == AllocateNode::Ideal_allocation(mem->in(3), phase, offset), "sanity"); 230 InitializeNode* init = alloc->as_Allocate()->initialization(); 231 // We are looking for stored value, return Initialize node 232 // or memory edge from Allocate node. 233 if (init != NULL) { 234 return init; 235 } else { 236 return alloc->in(TypeFunc::Memory); // It will produce zero value (see callers). 237 } 238 } 239 // Otherwise skip it (the call updated 'mem' value). 240 } else if (mem->Opcode() == Op_SCMemProj) { 241 mem = mem->in(0); 242 Node* adr = NULL; 243 if (mem->is_LoadStore()) { 244 adr = mem->in(MemNode::Address); 245 } else { 246 assert(mem->Opcode() == Op_EncodeISOArray || 247 mem->Opcode() == Op_StrCompressedCopy, "sanity"); 248 adr = mem->in(3); // Destination array 249 } 250 const TypePtr* atype = adr->bottom_type()->is_ptr(); 251 int adr_idx = phase->C->get_alias_index(atype); 252 if (adr_idx == alias_idx) { 253 DEBUG_ONLY(mem->dump();) 254 assert(false, "Object is not scalar replaceable if a LoadStore node accesses its field"); 255 return NULL; 256 } 257 mem = mem->in(MemNode::Memory); 258 } else if (mem->Opcode() == Op_StrInflatedCopy) { 259 Node* adr = mem->in(3); // Destination array 260 const TypePtr* atype = adr->bottom_type()->is_ptr(); 261 int adr_idx = phase->C->get_alias_index(atype); 262 if (adr_idx == alias_idx) { 263 DEBUG_ONLY(mem->dump();) 264 assert(false, "Object is not scalar replaceable if a StrInflatedCopy node accesses its field"); 265 return NULL; 266 } 267 mem = mem->in(MemNode::Memory); 268 } else { 269 return mem; 270 } 271 assert(mem != orig_mem, "dead memory loop"); 272 } 273 } 274 275 // Generate loads from source of the arraycopy for fields of 276 // destination needed at a deoptimization point 277 Node* PhaseMacroExpand::make_arraycopy_load(ArrayCopyNode* ac, intptr_t offset, Node* ctl, Node* mem, BasicType ft, const Type *ftype, AllocateNode *alloc) { 278 BasicType bt = ft; 279 const Type *type = ftype; 280 if (ft == T_NARROWOOP) { 281 bt = T_OBJECT; 282 type = ftype->make_oopptr(); 283 } 284 Node* res = NULL; 285 if (ac->is_clonebasic()) { 286 assert(ac->in(ArrayCopyNode::Src) != ac->in(ArrayCopyNode::Dest), "clone source equals destination"); 287 Node* base = ac->in(ArrayCopyNode::Src); 288 Node* adr = _igvn.transform(new AddPNode(base, base, MakeConX(offset))); 289 const TypePtr* adr_type = _igvn.type(base)->is_ptr()->add_offset(offset); 290 MergeMemNode* mergemen = _igvn.transform(MergeMemNode::make(mem))->as_MergeMem(); 291 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2(); 292 res = ArrayCopyNode::load(bs, &_igvn, ctl, mergemen, adr, adr_type, type, bt); 293 } else { 294 if (ac->modifies(offset, offset, &_igvn, true)) { 295 assert(ac->in(ArrayCopyNode::Dest) == alloc->result_cast(), "arraycopy destination should be allocation's result"); 296 uint shift = exact_log2(type2aelembytes(bt)); 297 Node* src_pos = ac->in(ArrayCopyNode::SrcPos); 298 Node* dest_pos = ac->in(ArrayCopyNode::DestPos); 299 const TypeInt* src_pos_t = _igvn.type(src_pos)->is_int(); 300 const TypeInt* dest_pos_t = _igvn.type(dest_pos)->is_int(); 301 302 Node* adr = NULL; 303 const TypePtr* adr_type = NULL; 304 if (src_pos_t->is_con() && dest_pos_t->is_con()) { 305 intptr_t off = ((src_pos_t->get_con() - dest_pos_t->get_con()) << shift) + offset; 306 Node* base = ac->in(ArrayCopyNode::Src); 307 adr = _igvn.transform(new AddPNode(base, base, MakeConX(off))); 308 adr_type = _igvn.type(base)->is_ptr()->add_offset(off); 309 if (ac->in(ArrayCopyNode::Src) == ac->in(ArrayCopyNode::Dest)) { 310 // Don't emit a new load from src if src == dst but try to get the value from memory instead 311 return value_from_mem(ac->in(TypeFunc::Memory), ctl, ft, ftype, adr_type->isa_oopptr(), alloc); 312 } 313 } else { 314 Node* diff = _igvn.transform(new SubINode(ac->in(ArrayCopyNode::SrcPos), ac->in(ArrayCopyNode::DestPos))); 315 #ifdef _LP64 316 diff = _igvn.transform(new ConvI2LNode(diff)); 317 #endif 318 diff = _igvn.transform(new LShiftXNode(diff, intcon(shift))); 319 320 Node* off = _igvn.transform(new AddXNode(MakeConX(offset), diff)); 321 Node* base = ac->in(ArrayCopyNode::Src); 322 adr = _igvn.transform(new AddPNode(base, base, off)); 323 adr_type = _igvn.type(base)->is_ptr()->add_offset(Type::OffsetBot); 324 if (ac->in(ArrayCopyNode::Src) == ac->in(ArrayCopyNode::Dest)) { 325 // Non constant offset in the array: we can't statically 326 // determine the value 327 return NULL; 328 } 329 } 330 MergeMemNode* mergemen = _igvn.transform(MergeMemNode::make(mem))->as_MergeMem(); 331 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2(); 332 res = ArrayCopyNode::load(bs, &_igvn, ctl, mergemen, adr, adr_type, type, bt); 333 } 334 } 335 if (res != NULL) { 336 if (ftype->isa_narrowoop()) { 337 // PhaseMacroExpand::scalar_replacement adds DecodeN nodes 338 res = _igvn.transform(new EncodePNode(res, ftype)); 339 } 340 return res; 341 } 342 return NULL; 343 } 344 345 // 346 // Given a Memory Phi, compute a value Phi containing the values from stores 347 // on the input paths. 348 // Note: this function is recursive, its depth is limited by the "level" argument 349 // Returns the computed Phi, or NULL if it cannot compute it. 350 Node *PhaseMacroExpand::value_from_mem_phi(Node *mem, BasicType ft, const Type *phi_type, const TypeOopPtr *adr_t, AllocateNode *alloc, Node_Stack *value_phis, int level) { 351 assert(mem->is_Phi(), "sanity"); 352 int alias_idx = C->get_alias_index(adr_t); 353 int offset = adr_t->offset(); 354 int instance_id = adr_t->instance_id(); 355 356 // Check if an appropriate value phi already exists. 357 Node* region = mem->in(0); 358 for (DUIterator_Fast kmax, k = region->fast_outs(kmax); k < kmax; k++) { 359 Node* phi = region->fast_out(k); 360 if (phi->is_Phi() && phi != mem && 361 phi->as_Phi()->is_same_inst_field(phi_type, (int)mem->_idx, instance_id, alias_idx, offset)) { 362 return phi; 363 } 364 } 365 // Check if an appropriate new value phi already exists. 366 Node* new_phi = value_phis->find(mem->_idx); 367 if (new_phi != NULL) 368 return new_phi; 369 370 if (level <= 0) { 371 return NULL; // Give up: phi tree too deep 372 } 373 Node *start_mem = C->start()->proj_out_or_null(TypeFunc::Memory); 374 Node *alloc_mem = alloc->in(TypeFunc::Memory); 375 376 uint length = mem->req(); 377 GrowableArray <Node *> values(length, length, NULL); 378 379 // create a new Phi for the value 380 PhiNode *phi = new PhiNode(mem->in(0), phi_type, NULL, mem->_idx, instance_id, alias_idx, offset); 381 transform_later(phi); 382 value_phis->push(phi, mem->_idx); 383 384 for (uint j = 1; j < length; j++) { 385 Node *in = mem->in(j); 386 if (in == NULL || in->is_top()) { 387 values.at_put(j, in); 388 } else { 389 Node *val = scan_mem_chain(in, alias_idx, offset, start_mem, alloc, &_igvn); 390 if (val == start_mem || val == alloc_mem) { 391 // hit a sentinel, return appropriate 0 value 392 values.at_put(j, _igvn.zerocon(ft)); 393 continue; 394 } 395 if (val->is_Initialize()) { 396 val = val->as_Initialize()->find_captured_store(offset, type2aelembytes(ft), &_igvn); 397 } 398 if (val == NULL) { 399 return NULL; // can't find a value on this path 400 } 401 if (val == mem) { 402 values.at_put(j, mem); 403 } else if (val->is_Store()) { 404 Node* n = val->in(MemNode::ValueIn); 405 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2(); 406 n = bs->step_over_gc_barrier(n); 407 if (is_subword_type(ft)) { 408 n = Compile::narrow_value(ft, n, phi_type, &_igvn, true); 409 } 410 values.at_put(j, n); 411 } else if(val->is_Proj() && val->in(0) == alloc) { 412 values.at_put(j, _igvn.zerocon(ft)); 413 } else if (val->is_Phi()) { 414 val = value_from_mem_phi(val, ft, phi_type, adr_t, alloc, value_phis, level-1); 415 if (val == NULL) { 416 return NULL; 417 } 418 values.at_put(j, val); 419 } else if (val->Opcode() == Op_SCMemProj) { 420 assert(val->in(0)->is_LoadStore() || 421 val->in(0)->Opcode() == Op_EncodeISOArray || 422 val->in(0)->Opcode() == Op_StrCompressedCopy, "sanity"); 423 assert(false, "Object is not scalar replaceable if a LoadStore node accesses its field"); 424 return NULL; 425 } else if (val->is_ArrayCopy()) { 426 Node* res = make_arraycopy_load(val->as_ArrayCopy(), offset, val->in(0), val->in(TypeFunc::Memory), ft, phi_type, alloc); 427 if (res == NULL) { 428 return NULL; 429 } 430 values.at_put(j, res); 431 } else { 432 DEBUG_ONLY( val->dump(); ) 433 assert(false, "unknown node on this path"); 434 return NULL; // unknown node on this path 435 } 436 } 437 } 438 // Set Phi's inputs 439 for (uint j = 1; j < length; j++) { 440 if (values.at(j) == mem) { 441 phi->init_req(j, phi); 442 } else { 443 phi->init_req(j, values.at(j)); 444 } 445 } 446 return phi; 447 } 448 449 // Search the last value stored into the object's field. 450 Node *PhaseMacroExpand::value_from_mem(Node *sfpt_mem, Node *sfpt_ctl, BasicType ft, const Type *ftype, const TypeOopPtr *adr_t, AllocateNode *alloc) { 451 assert(adr_t->is_known_instance_field(), "instance required"); 452 int instance_id = adr_t->instance_id(); 453 assert((uint)instance_id == alloc->_idx, "wrong allocation"); 454 455 int alias_idx = C->get_alias_index(adr_t); 456 int offset = adr_t->offset(); 457 Node *start_mem = C->start()->proj_out_or_null(TypeFunc::Memory); 458 Node *alloc_ctrl = alloc->in(TypeFunc::Control); 459 Node *alloc_mem = alloc->in(TypeFunc::Memory); 460 VectorSet visited; 461 462 bool done = sfpt_mem == alloc_mem; 463 Node *mem = sfpt_mem; 464 while (!done) { 465 if (visited.test_set(mem->_idx)) { 466 return NULL; // found a loop, give up 467 } 468 mem = scan_mem_chain(mem, alias_idx, offset, start_mem, alloc, &_igvn); 469 if (mem == start_mem || mem == alloc_mem) { 470 done = true; // hit a sentinel, return appropriate 0 value 471 } else if (mem->is_Initialize()) { 472 mem = mem->as_Initialize()->find_captured_store(offset, type2aelembytes(ft), &_igvn); 473 if (mem == NULL) { 474 done = true; // Something go wrong. 475 } else if (mem->is_Store()) { 476 const TypePtr* atype = mem->as_Store()->adr_type(); 477 assert(C->get_alias_index(atype) == Compile::AliasIdxRaw, "store is correct memory slice"); 478 done = true; 479 } 480 } else if (mem->is_Store()) { 481 const TypeOopPtr* atype = mem->as_Store()->adr_type()->isa_oopptr(); 482 assert(atype != NULL, "address type must be oopptr"); 483 assert(C->get_alias_index(atype) == alias_idx && 484 atype->is_known_instance_field() && atype->offset() == offset && 485 atype->instance_id() == instance_id, "store is correct memory slice"); 486 done = true; 487 } else if (mem->is_Phi()) { 488 // try to find a phi's unique input 489 Node *unique_input = NULL; 490 Node *top = C->top(); 491 for (uint i = 1; i < mem->req(); i++) { 492 Node *n = scan_mem_chain(mem->in(i), alias_idx, offset, start_mem, alloc, &_igvn); 493 if (n == NULL || n == top || n == mem) { 494 continue; 495 } else if (unique_input == NULL) { 496 unique_input = n; 497 } else if (unique_input != n) { 498 unique_input = top; 499 break; 500 } 501 } 502 if (unique_input != NULL && unique_input != top) { 503 mem = unique_input; 504 } else { 505 done = true; 506 } 507 } else if (mem->is_ArrayCopy()) { 508 done = true; 509 } else { 510 DEBUG_ONLY( mem->dump(); ) 511 assert(false, "unexpected node"); 512 } 513 } 514 if (mem != NULL) { 515 if (mem == start_mem || mem == alloc_mem) { 516 // hit a sentinel, return appropriate 0 value 517 return _igvn.zerocon(ft); 518 } else if (mem->is_Store()) { 519 Node* n = mem->in(MemNode::ValueIn); 520 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2(); 521 n = bs->step_over_gc_barrier(n); 522 return n; 523 } else if (mem->is_Phi()) { 524 // attempt to produce a Phi reflecting the values on the input paths of the Phi 525 Node_Stack value_phis(8); 526 Node* phi = value_from_mem_phi(mem, ft, ftype, adr_t, alloc, &value_phis, ValueSearchLimit); 527 if (phi != NULL) { 528 return phi; 529 } else { 530 // Kill all new Phis 531 while(value_phis.is_nonempty()) { 532 Node* n = value_phis.node(); 533 _igvn.replace_node(n, C->top()); 534 value_phis.pop(); 535 } 536 } 537 } else if (mem->is_ArrayCopy()) { 538 Node* ctl = mem->in(0); 539 Node* m = mem->in(TypeFunc::Memory); 540 if (sfpt_ctl->is_Proj() && sfpt_ctl->as_Proj()->is_uncommon_trap_proj(Deoptimization::Reason_none)) { 541 // pin the loads in the uncommon trap path 542 ctl = sfpt_ctl; 543 m = sfpt_mem; 544 } 545 return make_arraycopy_load(mem->as_ArrayCopy(), offset, ctl, m, ft, ftype, alloc); 546 } 547 } 548 // Something go wrong. 549 return NULL; 550 } 551 552 // Check the possibility of scalar replacement. 553 bool PhaseMacroExpand::can_eliminate_allocation(AllocateNode *alloc, GrowableArray <SafePointNode *>& safepoints) { 554 // Scan the uses of the allocation to check for anything that would 555 // prevent us from eliminating it. 556 NOT_PRODUCT( const char* fail_eliminate = NULL; ) 557 DEBUG_ONLY( Node* disq_node = NULL; ) 558 bool can_eliminate = true; 559 560 Node* res = alloc->result_cast(); 561 const TypeOopPtr* res_type = NULL; 562 if (res == NULL) { 563 // All users were eliminated. 564 } else if (!res->is_CheckCastPP()) { 565 NOT_PRODUCT(fail_eliminate = "Allocation does not have unique CheckCastPP";) 566 can_eliminate = false; 567 } else { 568 res_type = _igvn.type(res)->isa_oopptr(); 569 if (res_type == NULL) { 570 NOT_PRODUCT(fail_eliminate = "Neither instance or array allocation";) 571 can_eliminate = false; 572 } else if (res_type->isa_aryptr()) { 573 int length = alloc->in(AllocateNode::ALength)->find_int_con(-1); 574 if (length < 0) { 575 NOT_PRODUCT(fail_eliminate = "Array's size is not constant";) 576 can_eliminate = false; 577 } 578 } 579 } 580 581 if (can_eliminate && res != NULL) { 582 BarrierSetC2 *bs = BarrierSet::barrier_set()->barrier_set_c2(); 583 for (DUIterator_Fast jmax, j = res->fast_outs(jmax); 584 j < jmax && can_eliminate; j++) { 585 Node* use = res->fast_out(j); 586 587 if (use->is_AddP()) { 588 const TypePtr* addp_type = _igvn.type(use)->is_ptr(); 589 int offset = addp_type->offset(); 590 591 if (offset == Type::OffsetTop || offset == Type::OffsetBot) { 592 NOT_PRODUCT(fail_eliminate = "Undefined field reference";) 593 can_eliminate = false; 594 break; 595 } 596 for (DUIterator_Fast kmax, k = use->fast_outs(kmax); 597 k < kmax && can_eliminate; k++) { 598 Node* n = use->fast_out(k); 599 if (!n->is_Store() && n->Opcode() != Op_CastP2X && !bs->is_gc_pre_barrier_node(n)) { 600 DEBUG_ONLY(disq_node = n;) 601 if (n->is_Load() || n->is_LoadStore()) { 602 NOT_PRODUCT(fail_eliminate = "Field load";) 603 } else { 604 NOT_PRODUCT(fail_eliminate = "Not store field reference";) 605 } 606 can_eliminate = false; 607 } 608 } 609 } else if (use->is_ArrayCopy() && 610 (use->as_ArrayCopy()->is_clonebasic() || 611 use->as_ArrayCopy()->is_arraycopy_validated() || 612 use->as_ArrayCopy()->is_copyof_validated() || 613 use->as_ArrayCopy()->is_copyofrange_validated()) && 614 use->in(ArrayCopyNode::Dest) == res) { 615 // ok to eliminate 616 } else if (use->is_SafePoint()) { 617 SafePointNode* sfpt = use->as_SafePoint(); 618 if (sfpt->is_Call() && sfpt->as_Call()->has_non_debug_use(res)) { 619 // Object is passed as argument. 620 DEBUG_ONLY(disq_node = use;) 621 NOT_PRODUCT(fail_eliminate = "Object is passed as argument";) 622 can_eliminate = false; 623 } 624 Node* sfptMem = sfpt->memory(); 625 if (sfptMem == NULL || sfptMem->is_top()) { 626 DEBUG_ONLY(disq_node = use;) 627 NOT_PRODUCT(fail_eliminate = "NULL or TOP memory";) 628 can_eliminate = false; 629 } else { 630 safepoints.append_if_missing(sfpt); 631 } 632 } else if (use->Opcode() != Op_CastP2X) { // CastP2X is used by card mark 633 if (use->is_Phi()) { 634 if (use->outcnt() == 1 && use->unique_out()->Opcode() == Op_Return) { 635 NOT_PRODUCT(fail_eliminate = "Object is return value";) 636 } else { 637 NOT_PRODUCT(fail_eliminate = "Object is referenced by Phi";) 638 } 639 DEBUG_ONLY(disq_node = use;) 640 } else { 641 if (use->Opcode() == Op_Return) { 642 NOT_PRODUCT(fail_eliminate = "Object is return value";) 643 }else { 644 NOT_PRODUCT(fail_eliminate = "Object is referenced by node";) 645 } 646 DEBUG_ONLY(disq_node = use;) 647 } 648 can_eliminate = false; 649 } 650 } 651 } 652 653 #ifndef PRODUCT 654 if (PrintEliminateAllocations) { 655 if (can_eliminate) { 656 tty->print("Scalar "); 657 if (res == NULL) 658 alloc->dump(); 659 else 660 res->dump(); 661 } else if (alloc->_is_scalar_replaceable) { 662 tty->print("NotScalar (%s)", fail_eliminate); 663 if (res == NULL) 664 alloc->dump(); 665 else 666 res->dump(); 667 #ifdef ASSERT 668 if (disq_node != NULL) { 669 tty->print(" >>>> "); 670 disq_node->dump(); 671 } 672 #endif /*ASSERT*/ 673 } 674 } 675 #endif 676 return can_eliminate; 677 } 678 679 // Do scalar replacement. 680 bool PhaseMacroExpand::scalar_replacement(AllocateNode *alloc, GrowableArray <SafePointNode *>& safepoints) { 681 GrowableArray <SafePointNode *> safepoints_done; 682 683 ciInstanceKlass* iklass = NULL; 684 int nfields = 0; 685 int array_base = 0; 686 int element_size = 0; 687 BasicType basic_elem_type = T_ILLEGAL; 688 const Type* field_type = NULL; 689 690 Node* res = alloc->result_cast(); 691 assert(res == NULL || res->is_CheckCastPP(), "unexpected AllocateNode result"); 692 const TypeOopPtr* res_type = NULL; 693 if (res != NULL) { // Could be NULL when there are no users 694 res_type = _igvn.type(res)->isa_oopptr(); 695 } 696 697 if (res != NULL) { 698 if (res_type->isa_instptr()) { 699 // find the fields of the class which will be needed for safepoint debug information 700 iklass = res_type->is_instptr()->instance_klass(); 701 nfields = iklass->nof_nonstatic_fields(); 702 } else { 703 // find the array's elements which will be needed for safepoint debug information 704 nfields = alloc->in(AllocateNode::ALength)->find_int_con(-1); 705 assert(nfields >= 0, "must be an array klass."); 706 basic_elem_type = res_type->is_aryptr()->elem()->array_element_basic_type(); 707 array_base = arrayOopDesc::base_offset_in_bytes(basic_elem_type); 708 element_size = type2aelembytes(basic_elem_type); 709 field_type = res_type->is_aryptr()->elem(); 710 } 711 } 712 // 713 // Process the safepoint uses 714 // 715 while (safepoints.length() > 0) { 716 SafePointNode* sfpt = safepoints.pop(); 717 Node* mem = sfpt->memory(); 718 Node* ctl = sfpt->control(); 719 assert(sfpt->jvms() != NULL, "missed JVMS"); 720 // Fields of scalar objs are referenced only at the end 721 // of regular debuginfo at the last (youngest) JVMS. 722 // Record relative start index. 723 uint first_ind = (sfpt->req() - sfpt->jvms()->scloff()); 724 SafePointScalarObjectNode* sobj = new SafePointScalarObjectNode(res_type, 725 #ifdef ASSERT 726 alloc, 727 #endif 728 first_ind, nfields); 729 sobj->init_req(0, C->root()); 730 transform_later(sobj); 731 732 // Scan object's fields adding an input to the safepoint for each field. 733 for (int j = 0; j < nfields; j++) { 734 intptr_t offset; 735 ciField* field = NULL; 736 if (iklass != NULL) { 737 field = iklass->nonstatic_field_at(j); 738 offset = field->offset(); 739 ciType* elem_type = field->type(); 740 basic_elem_type = field->layout_type(); 741 742 // The next code is taken from Parse::do_get_xxx(). 743 if (is_reference_type(basic_elem_type)) { 744 if (!elem_type->is_loaded()) { 745 field_type = TypeInstPtr::BOTTOM; 746 } else if (field != NULL && field->is_static_constant()) { 747 ciObject* con = field->constant_value().as_object(); 748 // Do not "join" in the previous type; it doesn't add value, 749 // and may yield a vacuous result if the field is of interface type. 750 field_type = TypeOopPtr::make_from_constant(con)->isa_oopptr(); 751 assert(field_type != NULL, "field singleton type must be consistent"); 752 } else { 753 field_type = TypeOopPtr::make_from_klass(elem_type->as_klass()); 754 } 755 if (UseCompressedOops) { 756 field_type = field_type->make_narrowoop(); 757 basic_elem_type = T_NARROWOOP; 758 } 759 } else { 760 field_type = Type::get_const_basic_type(basic_elem_type); 761 } 762 } else { 763 offset = array_base + j * (intptr_t)element_size; 764 } 765 766 const TypeOopPtr *field_addr_type = res_type->add_offset(offset)->isa_oopptr(); 767 768 Node *field_val = value_from_mem(mem, ctl, basic_elem_type, field_type, field_addr_type, alloc); 769 if (field_val == NULL) { 770 // We weren't able to find a value for this field, 771 // give up on eliminating this allocation. 772 773 // Remove any extra entries we added to the safepoint. 774 uint last = sfpt->req() - 1; 775 for (int k = 0; k < j; k++) { 776 sfpt->del_req(last--); 777 } 778 _igvn._worklist.push(sfpt); 779 // rollback processed safepoints 780 while (safepoints_done.length() > 0) { 781 SafePointNode* sfpt_done = safepoints_done.pop(); 782 // remove any extra entries we added to the safepoint 783 last = sfpt_done->req() - 1; 784 for (int k = 0; k < nfields; k++) { 785 sfpt_done->del_req(last--); 786 } 787 JVMState *jvms = sfpt_done->jvms(); 788 jvms->set_endoff(sfpt_done->req()); 789 // Now make a pass over the debug information replacing any references 790 // to SafePointScalarObjectNode with the allocated object. 791 int start = jvms->debug_start(); 792 int end = jvms->debug_end(); 793 for (int i = start; i < end; i++) { 794 if (sfpt_done->in(i)->is_SafePointScalarObject()) { 795 SafePointScalarObjectNode* scobj = sfpt_done->in(i)->as_SafePointScalarObject(); 796 if (scobj->first_index(jvms) == sfpt_done->req() && 797 scobj->n_fields() == (uint)nfields) { 798 assert(scobj->alloc() == alloc, "sanity"); 799 sfpt_done->set_req(i, res); 800 } 801 } 802 } 803 _igvn._worklist.push(sfpt_done); 804 } 805 #ifndef PRODUCT 806 if (PrintEliminateAllocations) { 807 if (field != NULL) { 808 tty->print("=== At SafePoint node %d can't find value of Field: ", 809 sfpt->_idx); 810 field->print(); 811 int field_idx = C->get_alias_index(field_addr_type); 812 tty->print(" (alias_idx=%d)", field_idx); 813 } else { // Array's element 814 tty->print("=== At SafePoint node %d can't find value of array element [%d]", 815 sfpt->_idx, j); 816 } 817 tty->print(", which prevents elimination of: "); 818 if (res == NULL) 819 alloc->dump(); 820 else 821 res->dump(); 822 } 823 #endif 824 return false; 825 } 826 if (UseCompressedOops && field_type->isa_narrowoop()) { 827 // Enable "DecodeN(EncodeP(Allocate)) --> Allocate" transformation 828 // to be able scalar replace the allocation. 829 if (field_val->is_EncodeP()) { 830 field_val = field_val->in(1); 831 } else { 832 field_val = transform_later(new DecodeNNode(field_val, field_val->get_ptr_type())); 833 } 834 } 835 sfpt->add_req(field_val); 836 } 837 JVMState *jvms = sfpt->jvms(); 838 jvms->set_endoff(sfpt->req()); 839 // Now make a pass over the debug information replacing any references 840 // to the allocated object with "sobj" 841 int start = jvms->debug_start(); 842 int end = jvms->debug_end(); 843 sfpt->replace_edges_in_range(res, sobj, start, end, &_igvn); 844 _igvn._worklist.push(sfpt); 845 safepoints_done.append_if_missing(sfpt); // keep it for rollback 846 } 847 return true; 848 } 849 850 static void disconnect_projections(MultiNode* n, PhaseIterGVN& igvn) { 851 Node* ctl_proj = n->proj_out_or_null(TypeFunc::Control); 852 Node* mem_proj = n->proj_out_or_null(TypeFunc::Memory); 853 if (ctl_proj != NULL) { 854 igvn.replace_node(ctl_proj, n->in(0)); 855 } 856 if (mem_proj != NULL) { 857 igvn.replace_node(mem_proj, n->in(TypeFunc::Memory)); 858 } 859 } 860 861 // Process users of eliminated allocation. 862 void PhaseMacroExpand::process_users_of_allocation(CallNode *alloc) { 863 Node* res = alloc->result_cast(); 864 if (res != NULL) { 865 for (DUIterator_Last jmin, j = res->last_outs(jmin); j >= jmin; ) { 866 Node *use = res->last_out(j); 867 uint oc1 = res->outcnt(); 868 869 if (use->is_AddP()) { 870 for (DUIterator_Last kmin, k = use->last_outs(kmin); k >= kmin; ) { 871 Node *n = use->last_out(k); 872 uint oc2 = use->outcnt(); 873 if (n->is_Store()) { 874 #ifdef ASSERT 875 // Verify that there is no dependent MemBarVolatile nodes, 876 // they should be removed during IGVN, see MemBarNode::Ideal(). 877 for (DUIterator_Fast pmax, p = n->fast_outs(pmax); 878 p < pmax; p++) { 879 Node* mb = n->fast_out(p); 880 assert(mb->is_Initialize() || !mb->is_MemBar() || 881 mb->req() <= MemBarNode::Precedent || 882 mb->in(MemBarNode::Precedent) != n, 883 "MemBarVolatile should be eliminated for non-escaping object"); 884 } 885 #endif 886 _igvn.replace_node(n, n->in(MemNode::Memory)); 887 } else { 888 eliminate_gc_barrier(n); 889 } 890 k -= (oc2 - use->outcnt()); 891 } 892 _igvn.remove_dead_node(use); 893 } else if (use->is_ArrayCopy()) { 894 // Disconnect ArrayCopy node 895 ArrayCopyNode* ac = use->as_ArrayCopy(); 896 if (ac->is_clonebasic()) { 897 Node* membar_after = ac->proj_out(TypeFunc::Control)->unique_ctrl_out(); 898 disconnect_projections(ac, _igvn); 899 assert(alloc->in(TypeFunc::Memory)->is_Proj() && alloc->in(TypeFunc::Memory)->in(0)->Opcode() == Op_MemBarCPUOrder, "mem barrier expected before allocation"); 900 Node* membar_before = alloc->in(TypeFunc::Memory)->in(0); 901 disconnect_projections(membar_before->as_MemBar(), _igvn); 902 if (membar_after->is_MemBar()) { 903 disconnect_projections(membar_after->as_MemBar(), _igvn); 904 } 905 } else { 906 assert(ac->is_arraycopy_validated() || 907 ac->is_copyof_validated() || 908 ac->is_copyofrange_validated(), "unsupported"); 909 CallProjections callprojs; 910 ac->extract_projections(&callprojs, true); 911 912 _igvn.replace_node(callprojs.fallthrough_ioproj, ac->in(TypeFunc::I_O)); 913 _igvn.replace_node(callprojs.fallthrough_memproj, ac->in(TypeFunc::Memory)); 914 _igvn.replace_node(callprojs.fallthrough_catchproj, ac->in(TypeFunc::Control)); 915 916 // Set control to top. IGVN will remove the remaining projections 917 ac->set_req(0, top()); 918 ac->replace_edge(res, top(), &_igvn); 919 920 // Disconnect src right away: it can help find new 921 // opportunities for allocation elimination 922 Node* src = ac->in(ArrayCopyNode::Src); 923 ac->replace_edge(src, top(), &_igvn); 924 // src can be top at this point if src and dest of the 925 // arraycopy were the same 926 if (src->outcnt() == 0 && !src->is_top()) { 927 _igvn.remove_dead_node(src); 928 } 929 } 930 _igvn._worklist.push(ac); 931 } else { 932 eliminate_gc_barrier(use); 933 } 934 j -= (oc1 - res->outcnt()); 935 } 936 assert(res->outcnt() == 0, "all uses of allocated objects must be deleted"); 937 _igvn.remove_dead_node(res); 938 } 939 940 // 941 // Process other users of allocation's projections 942 // 943 if (_callprojs.resproj != NULL && _callprojs.resproj->outcnt() != 0) { 944 // First disconnect stores captured by Initialize node. 945 // If Initialize node is eliminated first in the following code, 946 // it will kill such stores and DUIterator_Last will assert. 947 for (DUIterator_Fast jmax, j = _callprojs.resproj->fast_outs(jmax); j < jmax; j++) { 948 Node* use = _callprojs.resproj->fast_out(j); 949 if (use->is_AddP()) { 950 // raw memory addresses used only by the initialization 951 _igvn.replace_node(use, C->top()); 952 --j; --jmax; 953 } 954 } 955 for (DUIterator_Last jmin, j = _callprojs.resproj->last_outs(jmin); j >= jmin; ) { 956 Node* use = _callprojs.resproj->last_out(j); 957 uint oc1 = _callprojs.resproj->outcnt(); 958 if (use->is_Initialize()) { 959 // Eliminate Initialize node. 960 InitializeNode *init = use->as_Initialize(); 961 assert(init->outcnt() <= 2, "only a control and memory projection expected"); 962 Node *ctrl_proj = init->proj_out_or_null(TypeFunc::Control); 963 if (ctrl_proj != NULL) { 964 _igvn.replace_node(ctrl_proj, init->in(TypeFunc::Control)); 965 #ifdef ASSERT 966 // If the InitializeNode has no memory out, it will die, and tmp will become NULL 967 Node* tmp = init->in(TypeFunc::Control); 968 assert(tmp == NULL || tmp == _callprojs.fallthrough_catchproj, "allocation control projection"); 969 #endif 970 } 971 Node *mem_proj = init->proj_out_or_null(TypeFunc::Memory); 972 if (mem_proj != NULL) { 973 Node *mem = init->in(TypeFunc::Memory); 974 #ifdef ASSERT 975 if (mem->is_MergeMem()) { 976 assert(mem->in(TypeFunc::Memory) == _callprojs.fallthrough_memproj, "allocation memory projection"); 977 } else { 978 assert(mem == _callprojs.fallthrough_memproj, "allocation memory projection"); 979 } 980 #endif 981 _igvn.replace_node(mem_proj, mem); 982 } 983 } else { 984 assert(false, "only Initialize or AddP expected"); 985 } 986 j -= (oc1 - _callprojs.resproj->outcnt()); 987 } 988 } 989 if (_callprojs.fallthrough_catchproj != NULL) { 990 _igvn.replace_node(_callprojs.fallthrough_catchproj, alloc->in(TypeFunc::Control)); 991 } 992 if (_callprojs.fallthrough_memproj != NULL) { 993 _igvn.replace_node(_callprojs.fallthrough_memproj, alloc->in(TypeFunc::Memory)); 994 } 995 if (_callprojs.catchall_memproj != NULL) { 996 _igvn.replace_node(_callprojs.catchall_memproj, C->top()); 997 } 998 if (_callprojs.fallthrough_ioproj != NULL) { 999 _igvn.replace_node(_callprojs.fallthrough_ioproj, alloc->in(TypeFunc::I_O)); 1000 } 1001 if (_callprojs.catchall_ioproj != NULL) { 1002 _igvn.replace_node(_callprojs.catchall_ioproj, C->top()); 1003 } 1004 if (_callprojs.catchall_catchproj != NULL) { 1005 _igvn.replace_node(_callprojs.catchall_catchproj, C->top()); 1006 } 1007 } 1008 1009 bool PhaseMacroExpand::eliminate_allocate_node(AllocateNode *alloc) { 1010 // If reallocation fails during deoptimization we'll pop all 1011 // interpreter frames for this compiled frame and that won't play 1012 // nice with JVMTI popframe. 1013 // We avoid this issue by eager reallocation when the popframe request 1014 // is received. 1015 if (!EliminateAllocations || !alloc->_is_non_escaping) { 1016 return false; 1017 } 1018 Node* klass = alloc->in(AllocateNode::KlassNode); 1019 const TypeKlassPtr* tklass = _igvn.type(klass)->is_klassptr(); 1020 Node* res = alloc->result_cast(); 1021 // Eliminate boxing allocations which are not used 1022 // regardless scalar replaceable status. 1023 bool boxing_alloc = C->eliminate_boxing() && 1024 tklass->isa_instklassptr() && 1025 tklass->is_instklassptr()->instance_klass()->is_box_klass(); 1026 if (!alloc->_is_scalar_replaceable && (!boxing_alloc || (res != NULL))) { 1027 return false; 1028 } 1029 1030 alloc->extract_projections(&_callprojs, false /*separate_io_proj*/, false /*do_asserts*/); 1031 1032 GrowableArray <SafePointNode *> safepoints; 1033 if (!can_eliminate_allocation(alloc, safepoints)) { 1034 return false; 1035 } 1036 1037 if (!alloc->_is_scalar_replaceable) { 1038 assert(res == NULL, "sanity"); 1039 // We can only eliminate allocation if all debug info references 1040 // are already replaced with SafePointScalarObject because 1041 // we can't search for a fields value without instance_id. 1042 if (safepoints.length() > 0) { 1043 return false; 1044 } 1045 } 1046 1047 if (!scalar_replacement(alloc, safepoints)) { 1048 return false; 1049 } 1050 1051 CompileLog* log = C->log(); 1052 if (log != NULL) { 1053 log->head("eliminate_allocation type='%d'", 1054 log->identify(tklass->exact_klass())); 1055 JVMState* p = alloc->jvms(); 1056 while (p != NULL) { 1057 log->elem("jvms bci='%d' method='%d'", p->bci(), log->identify(p->method())); 1058 p = p->caller(); 1059 } 1060 log->tail("eliminate_allocation"); 1061 } 1062 1063 process_users_of_allocation(alloc); 1064 1065 #ifndef PRODUCT 1066 if (PrintEliminateAllocations) { 1067 if (alloc->is_AllocateArray()) 1068 tty->print_cr("++++ Eliminated: %d AllocateArray", alloc->_idx); 1069 else 1070 tty->print_cr("++++ Eliminated: %d Allocate", alloc->_idx); 1071 } 1072 #endif 1073 1074 return true; 1075 } 1076 1077 bool PhaseMacroExpand::eliminate_boxing_node(CallStaticJavaNode *boxing) { 1078 // EA should remove all uses of non-escaping boxing node. 1079 if (!C->eliminate_boxing() || boxing->proj_out_or_null(TypeFunc::Parms) != NULL) { 1080 return false; 1081 } 1082 1083 assert(boxing->result_cast() == NULL, "unexpected boxing node result"); 1084 1085 boxing->extract_projections(&_callprojs, false /*separate_io_proj*/, false /*do_asserts*/); 1086 1087 const TypeTuple* r = boxing->tf()->range(); 1088 assert(r->cnt() > TypeFunc::Parms, "sanity"); 1089 const TypeInstPtr* t = r->field_at(TypeFunc::Parms)->isa_instptr(); 1090 assert(t != NULL, "sanity"); 1091 1092 CompileLog* log = C->log(); 1093 if (log != NULL) { 1094 log->head("eliminate_boxing type='%d'", 1095 log->identify(t->instance_klass())); 1096 JVMState* p = boxing->jvms(); 1097 while (p != NULL) { 1098 log->elem("jvms bci='%d' method='%d'", p->bci(), log->identify(p->method())); 1099 p = p->caller(); 1100 } 1101 log->tail("eliminate_boxing"); 1102 } 1103 1104 process_users_of_allocation(boxing); 1105 1106 #ifndef PRODUCT 1107 if (PrintEliminateAllocations) { 1108 tty->print("++++ Eliminated: %d ", boxing->_idx); 1109 boxing->method()->print_short_name(tty); 1110 tty->cr(); 1111 } 1112 #endif 1113 1114 return true; 1115 } 1116 1117 1118 Node* PhaseMacroExpand::make_load(Node* ctl, Node* mem, Node* base, int offset, const Type* value_type, BasicType bt) { 1119 Node* adr = basic_plus_adr(base, offset); 1120 const TypePtr* adr_type = adr->bottom_type()->is_ptr(); 1121 Node* value = LoadNode::make(_igvn, ctl, mem, adr, adr_type, value_type, bt, MemNode::unordered); 1122 transform_later(value); 1123 return value; 1124 } 1125 1126 1127 Node* PhaseMacroExpand::make_store(Node* ctl, Node* mem, Node* base, int offset, Node* value, BasicType bt) { 1128 Node* adr = basic_plus_adr(base, offset); 1129 mem = StoreNode::make(_igvn, ctl, mem, adr, NULL, value, bt, MemNode::unordered); 1130 transform_later(mem); 1131 return mem; 1132 } 1133 1134 //============================================================================= 1135 // 1136 // A L L O C A T I O N 1137 // 1138 // Allocation attempts to be fast in the case of frequent small objects. 1139 // It breaks down like this: 1140 // 1141 // 1) Size in doublewords is computed. This is a constant for objects and 1142 // variable for most arrays. Doubleword units are used to avoid size 1143 // overflow of huge doubleword arrays. We need doublewords in the end for 1144 // rounding. 1145 // 1146 // 2) Size is checked for being 'too large'. Too-large allocations will go 1147 // the slow path into the VM. The slow path can throw any required 1148 // exceptions, and does all the special checks for very large arrays. The 1149 // size test can constant-fold away for objects. For objects with 1150 // finalizers it constant-folds the otherway: you always go slow with 1151 // finalizers. 1152 // 1153 // 3) If NOT using TLABs, this is the contended loop-back point. 1154 // Load-Locked the heap top. If using TLABs normal-load the heap top. 1155 // 1156 // 4) Check that heap top + size*8 < max. If we fail go the slow ` route. 1157 // NOTE: "top+size*8" cannot wrap the 4Gig line! Here's why: for largish 1158 // "size*8" we always enter the VM, where "largish" is a constant picked small 1159 // enough that there's always space between the eden max and 4Gig (old space is 1160 // there so it's quite large) and large enough that the cost of entering the VM 1161 // is dwarfed by the cost to initialize the space. 1162 // 1163 // 5) If NOT using TLABs, Store-Conditional the adjusted heap top back 1164 // down. If contended, repeat at step 3. If using TLABs normal-store 1165 // adjusted heap top back down; there is no contention. 1166 // 1167 // 6) If !ZeroTLAB then Bulk-clear the object/array. Fill in klass & mark 1168 // fields. 1169 // 1170 // 7) Merge with the slow-path; cast the raw memory pointer to the correct 1171 // oop flavor. 1172 // 1173 //============================================================================= 1174 // FastAllocateSizeLimit value is in DOUBLEWORDS. 1175 // Allocations bigger than this always go the slow route. 1176 // This value must be small enough that allocation attempts that need to 1177 // trigger exceptions go the slow route. Also, it must be small enough so 1178 // that heap_top + size_in_bytes does not wrap around the 4Gig limit. 1179 //=============================================================================j// 1180 // %%% Here is an old comment from parseHelper.cpp; is it outdated? 1181 // The allocator will coalesce int->oop copies away. See comment in 1182 // coalesce.cpp about how this works. It depends critically on the exact 1183 // code shape produced here, so if you are changing this code shape 1184 // make sure the GC info for the heap-top is correct in and around the 1185 // slow-path call. 1186 // 1187 1188 void PhaseMacroExpand::expand_allocate_common( 1189 AllocateNode* alloc, // allocation node to be expanded 1190 Node* length, // array length for an array allocation 1191 const TypeFunc* slow_call_type, // Type of slow call 1192 address slow_call_address, // Address of slow call 1193 Node* valid_length_test // whether length is valid or not 1194 ) 1195 { 1196 Node* ctrl = alloc->in(TypeFunc::Control); 1197 Node* mem = alloc->in(TypeFunc::Memory); 1198 Node* i_o = alloc->in(TypeFunc::I_O); 1199 Node* size_in_bytes = alloc->in(AllocateNode::AllocSize); 1200 Node* klass_node = alloc->in(AllocateNode::KlassNode); 1201 Node* initial_slow_test = alloc->in(AllocateNode::InitialTest); 1202 assert(ctrl != NULL, "must have control"); 1203 1204 // We need a Region and corresponding Phi's to merge the slow-path and fast-path results. 1205 // they will not be used if "always_slow" is set 1206 enum { slow_result_path = 1, fast_result_path = 2 }; 1207 Node *result_region = NULL; 1208 Node *result_phi_rawmem = NULL; 1209 Node *result_phi_rawoop = NULL; 1210 Node *result_phi_i_o = NULL; 1211 1212 // The initial slow comparison is a size check, the comparison 1213 // we want to do is a BoolTest::gt 1214 bool expand_fast_path = true; 1215 int tv = _igvn.find_int_con(initial_slow_test, -1); 1216 if (tv >= 0) { 1217 // InitialTest has constant result 1218 // 0 - can fit in TLAB 1219 // 1 - always too big or negative 1220 assert(tv <= 1, "0 or 1 if a constant"); 1221 expand_fast_path = (tv == 0); 1222 initial_slow_test = NULL; 1223 } else { 1224 initial_slow_test = BoolNode::make_predicate(initial_slow_test, &_igvn); 1225 } 1226 1227 if (!UseTLAB) { 1228 // Force slow-path allocation 1229 expand_fast_path = false; 1230 initial_slow_test = NULL; 1231 } 1232 1233 bool allocation_has_use = (alloc->result_cast() != NULL); 1234 if (!allocation_has_use) { 1235 InitializeNode* init = alloc->initialization(); 1236 if (init != NULL) { 1237 init->remove(&_igvn); 1238 } 1239 if (expand_fast_path && (initial_slow_test == NULL)) { 1240 // Remove allocation node and return. 1241 // Size is a non-negative constant -> no initial check needed -> directly to fast path. 1242 // Also, no usages -> empty fast path -> no fall out to slow path -> nothing left. 1243 #ifndef PRODUCT 1244 if (PrintEliminateAllocations) { 1245 tty->print("NotUsed "); 1246 Node* res = alloc->proj_out_or_null(TypeFunc::Parms); 1247 if (res != NULL) { 1248 res->dump(); 1249 } else { 1250 alloc->dump(); 1251 } 1252 } 1253 #endif 1254 yank_alloc_node(alloc); 1255 return; 1256 } 1257 } 1258 1259 enum { too_big_or_final_path = 1, need_gc_path = 2 }; 1260 Node *slow_region = NULL; 1261 Node *toobig_false = ctrl; 1262 1263 // generate the initial test if necessary 1264 if (initial_slow_test != NULL ) { 1265 assert (expand_fast_path, "Only need test if there is a fast path"); 1266 slow_region = new RegionNode(3); 1267 1268 // Now make the initial failure test. Usually a too-big test but 1269 // might be a TRUE for finalizers or a fancy class check for 1270 // newInstance0. 1271 IfNode *toobig_iff = new IfNode(ctrl, initial_slow_test, PROB_MIN, COUNT_UNKNOWN); 1272 transform_later(toobig_iff); 1273 // Plug the failing-too-big test into the slow-path region 1274 Node *toobig_true = new IfTrueNode( toobig_iff ); 1275 transform_later(toobig_true); 1276 slow_region ->init_req( too_big_or_final_path, toobig_true ); 1277 toobig_false = new IfFalseNode( toobig_iff ); 1278 transform_later(toobig_false); 1279 } else { 1280 // No initial test, just fall into next case 1281 assert(allocation_has_use || !expand_fast_path, "Should already have been handled"); 1282 toobig_false = ctrl; 1283 debug_only(slow_region = NodeSentinel); 1284 } 1285 1286 // If we are here there are several possibilities 1287 // - expand_fast_path is false - then only a slow path is expanded. That's it. 1288 // no_initial_check means a constant allocation. 1289 // - If check always evaluates to false -> expand_fast_path is false (see above) 1290 // - If check always evaluates to true -> directly into fast path (but may bailout to slowpath) 1291 // if !allocation_has_use the fast path is empty 1292 // if !allocation_has_use && no_initial_check 1293 // - Then there are no fastpath that can fall out to slowpath -> no allocation code at all. 1294 // removed by yank_alloc_node above. 1295 1296 Node *slow_mem = mem; // save the current memory state for slow path 1297 // generate the fast allocation code unless we know that the initial test will always go slow 1298 if (expand_fast_path) { 1299 // Fast path modifies only raw memory. 1300 if (mem->is_MergeMem()) { 1301 mem = mem->as_MergeMem()->memory_at(Compile::AliasIdxRaw); 1302 } 1303 1304 // allocate the Region and Phi nodes for the result 1305 result_region = new RegionNode(3); 1306 result_phi_rawmem = new PhiNode(result_region, Type::MEMORY, TypeRawPtr::BOTTOM); 1307 result_phi_i_o = new PhiNode(result_region, Type::ABIO); // I/O is used for Prefetch 1308 1309 // Grab regular I/O before optional prefetch may change it. 1310 // Slow-path does no I/O so just set it to the original I/O. 1311 result_phi_i_o->init_req(slow_result_path, i_o); 1312 1313 // Name successful fast-path variables 1314 Node* fast_oop_ctrl; 1315 Node* fast_oop_rawmem; 1316 if (allocation_has_use) { 1317 Node* needgc_ctrl = NULL; 1318 result_phi_rawoop = new PhiNode(result_region, TypeRawPtr::BOTTOM); 1319 1320 intx prefetch_lines = length != NULL ? AllocatePrefetchLines : AllocateInstancePrefetchLines; 1321 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2(); 1322 Node* fast_oop = bs->obj_allocate(this, mem, toobig_false, size_in_bytes, i_o, needgc_ctrl, 1323 fast_oop_ctrl, fast_oop_rawmem, 1324 prefetch_lines); 1325 1326 if (initial_slow_test != NULL) { 1327 // This completes all paths into the slow merge point 1328 slow_region->init_req(need_gc_path, needgc_ctrl); 1329 transform_later(slow_region); 1330 } else { 1331 // No initial slow path needed! 1332 // Just fall from the need-GC path straight into the VM call. 1333 slow_region = needgc_ctrl; 1334 } 1335 1336 InitializeNode* init = alloc->initialization(); 1337 fast_oop_rawmem = initialize_object(alloc, 1338 fast_oop_ctrl, fast_oop_rawmem, fast_oop, 1339 klass_node, length, size_in_bytes); 1340 expand_initialize_membar(alloc, init, fast_oop_ctrl, fast_oop_rawmem); 1341 expand_dtrace_alloc_probe(alloc, fast_oop, fast_oop_ctrl, fast_oop_rawmem); 1342 1343 result_phi_rawoop->init_req(fast_result_path, fast_oop); 1344 } else { 1345 assert (initial_slow_test != NULL, "sanity"); 1346 fast_oop_ctrl = toobig_false; 1347 fast_oop_rawmem = mem; 1348 transform_later(slow_region); 1349 } 1350 1351 // Plug in the successful fast-path into the result merge point 1352 result_region ->init_req(fast_result_path, fast_oop_ctrl); 1353 result_phi_i_o ->init_req(fast_result_path, i_o); 1354 result_phi_rawmem->init_req(fast_result_path, fast_oop_rawmem); 1355 } else { 1356 slow_region = ctrl; 1357 result_phi_i_o = i_o; // Rename it to use in the following code. 1358 } 1359 1360 // Generate slow-path call 1361 CallNode *call = new CallStaticJavaNode(slow_call_type, slow_call_address, 1362 OptoRuntime::stub_name(slow_call_address), 1363 TypePtr::BOTTOM); 1364 call->init_req(TypeFunc::Control, slow_region); 1365 call->init_req(TypeFunc::I_O, top()); // does no i/o 1366 call->init_req(TypeFunc::Memory, slow_mem); // may gc ptrs 1367 call->init_req(TypeFunc::ReturnAdr, alloc->in(TypeFunc::ReturnAdr)); 1368 call->init_req(TypeFunc::FramePtr, alloc->in(TypeFunc::FramePtr)); 1369 1370 call->init_req(TypeFunc::Parms+0, klass_node); 1371 if (length != NULL) { 1372 call->init_req(TypeFunc::Parms+1, length); 1373 } 1374 1375 // Copy debug information and adjust JVMState information, then replace 1376 // allocate node with the call 1377 call->copy_call_debug_info(&_igvn, alloc); 1378 // For array allocations, copy the valid length check to the call node so Compile::final_graph_reshaping() can verify 1379 // that the call has the expected number of CatchProj nodes (in case the allocation always fails and the fallthrough 1380 // path dies). 1381 if (valid_length_test != NULL) { 1382 call->add_req(valid_length_test); 1383 } 1384 if (expand_fast_path) { 1385 call->set_cnt(PROB_UNLIKELY_MAG(4)); // Same effect as RC_UNCOMMON. 1386 } else { 1387 // Hook i_o projection to avoid its elimination during allocation 1388 // replacement (when only a slow call is generated). 1389 call->set_req(TypeFunc::I_O, result_phi_i_o); 1390 } 1391 _igvn.replace_node(alloc, call); 1392 transform_later(call); 1393 1394 // Identify the output projections from the allocate node and 1395 // adjust any references to them. 1396 // The control and io projections look like: 1397 // 1398 // v---Proj(ctrl) <-----+ v---CatchProj(ctrl) 1399 // Allocate Catch 1400 // ^---Proj(io) <-------+ ^---CatchProj(io) 1401 // 1402 // We are interested in the CatchProj nodes. 1403 // 1404 call->extract_projections(&_callprojs, false /*separate_io_proj*/, false /*do_asserts*/); 1405 1406 // An allocate node has separate memory projections for the uses on 1407 // the control and i_o paths. Replace the control memory projection with 1408 // result_phi_rawmem (unless we are only generating a slow call when 1409 // both memory projections are combined) 1410 if (expand_fast_path && _callprojs.fallthrough_memproj != NULL) { 1411 migrate_outs(_callprojs.fallthrough_memproj, result_phi_rawmem); 1412 } 1413 // Now change uses of catchall_memproj to use fallthrough_memproj and delete 1414 // catchall_memproj so we end up with a call that has only 1 memory projection. 1415 if (_callprojs.catchall_memproj != NULL ) { 1416 if (_callprojs.fallthrough_memproj == NULL) { 1417 _callprojs.fallthrough_memproj = new ProjNode(call, TypeFunc::Memory); 1418 transform_later(_callprojs.fallthrough_memproj); 1419 } 1420 migrate_outs(_callprojs.catchall_memproj, _callprojs.fallthrough_memproj); 1421 _igvn.remove_dead_node(_callprojs.catchall_memproj); 1422 } 1423 1424 // An allocate node has separate i_o projections for the uses on the control 1425 // and i_o paths. Always replace the control i_o projection with result i_o 1426 // otherwise incoming i_o become dead when only a slow call is generated 1427 // (it is different from memory projections where both projections are 1428 // combined in such case). 1429 if (_callprojs.fallthrough_ioproj != NULL) { 1430 migrate_outs(_callprojs.fallthrough_ioproj, result_phi_i_o); 1431 } 1432 // Now change uses of catchall_ioproj to use fallthrough_ioproj and delete 1433 // catchall_ioproj so we end up with a call that has only 1 i_o projection. 1434 if (_callprojs.catchall_ioproj != NULL ) { 1435 if (_callprojs.fallthrough_ioproj == NULL) { 1436 _callprojs.fallthrough_ioproj = new ProjNode(call, TypeFunc::I_O); 1437 transform_later(_callprojs.fallthrough_ioproj); 1438 } 1439 migrate_outs(_callprojs.catchall_ioproj, _callprojs.fallthrough_ioproj); 1440 _igvn.remove_dead_node(_callprojs.catchall_ioproj); 1441 } 1442 1443 // if we generated only a slow call, we are done 1444 if (!expand_fast_path) { 1445 // Now we can unhook i_o. 1446 if (result_phi_i_o->outcnt() > 1) { 1447 call->set_req(TypeFunc::I_O, top()); 1448 } else { 1449 assert(result_phi_i_o->unique_ctrl_out() == call, "sanity"); 1450 // Case of new array with negative size known during compilation. 1451 // AllocateArrayNode::Ideal() optimization disconnect unreachable 1452 // following code since call to runtime will throw exception. 1453 // As result there will be no users of i_o after the call. 1454 // Leave i_o attached to this call to avoid problems in preceding graph. 1455 } 1456 return; 1457 } 1458 1459 if (_callprojs.fallthrough_catchproj != NULL) { 1460 ctrl = _callprojs.fallthrough_catchproj->clone(); 1461 transform_later(ctrl); 1462 _igvn.replace_node(_callprojs.fallthrough_catchproj, result_region); 1463 } else { 1464 ctrl = top(); 1465 } 1466 Node *slow_result; 1467 if (_callprojs.resproj == NULL) { 1468 // no uses of the allocation result 1469 slow_result = top(); 1470 } else { 1471 slow_result = _callprojs.resproj->clone(); 1472 transform_later(slow_result); 1473 _igvn.replace_node(_callprojs.resproj, result_phi_rawoop); 1474 } 1475 1476 // Plug slow-path into result merge point 1477 result_region->init_req( slow_result_path, ctrl); 1478 transform_later(result_region); 1479 if (allocation_has_use) { 1480 result_phi_rawoop->init_req(slow_result_path, slow_result); 1481 transform_later(result_phi_rawoop); 1482 } 1483 result_phi_rawmem->init_req(slow_result_path, _callprojs.fallthrough_memproj); 1484 transform_later(result_phi_rawmem); 1485 transform_later(result_phi_i_o); 1486 // This completes all paths into the result merge point 1487 } 1488 1489 // Remove alloc node that has no uses. 1490 void PhaseMacroExpand::yank_alloc_node(AllocateNode* alloc) { 1491 Node* ctrl = alloc->in(TypeFunc::Control); 1492 Node* mem = alloc->in(TypeFunc::Memory); 1493 Node* i_o = alloc->in(TypeFunc::I_O); 1494 1495 alloc->extract_projections(&_callprojs, false /*separate_io_proj*/, false /*do_asserts*/); 1496 if (_callprojs.resproj != NULL) { 1497 for (DUIterator_Fast imax, i = _callprojs.resproj->fast_outs(imax); i < imax; i++) { 1498 Node* use = _callprojs.resproj->fast_out(i); 1499 use->isa_MemBar()->remove(&_igvn); 1500 --imax; 1501 --i; // back up iterator 1502 } 1503 assert(_callprojs.resproj->outcnt() == 0, "all uses must be deleted"); 1504 _igvn.remove_dead_node(_callprojs.resproj); 1505 } 1506 if (_callprojs.fallthrough_catchproj != NULL) { 1507 migrate_outs(_callprojs.fallthrough_catchproj, ctrl); 1508 _igvn.remove_dead_node(_callprojs.fallthrough_catchproj); 1509 } 1510 if (_callprojs.catchall_catchproj != NULL) { 1511 _igvn.rehash_node_delayed(_callprojs.catchall_catchproj); 1512 _callprojs.catchall_catchproj->set_req(0, top()); 1513 } 1514 if (_callprojs.fallthrough_proj != NULL) { 1515 Node* catchnode = _callprojs.fallthrough_proj->unique_ctrl_out(); 1516 _igvn.remove_dead_node(catchnode); 1517 _igvn.remove_dead_node(_callprojs.fallthrough_proj); 1518 } 1519 if (_callprojs.fallthrough_memproj != NULL) { 1520 migrate_outs(_callprojs.fallthrough_memproj, mem); 1521 _igvn.remove_dead_node(_callprojs.fallthrough_memproj); 1522 } 1523 if (_callprojs.fallthrough_ioproj != NULL) { 1524 migrate_outs(_callprojs.fallthrough_ioproj, i_o); 1525 _igvn.remove_dead_node(_callprojs.fallthrough_ioproj); 1526 } 1527 if (_callprojs.catchall_memproj != NULL) { 1528 _igvn.rehash_node_delayed(_callprojs.catchall_memproj); 1529 _callprojs.catchall_memproj->set_req(0, top()); 1530 } 1531 if (_callprojs.catchall_ioproj != NULL) { 1532 _igvn.rehash_node_delayed(_callprojs.catchall_ioproj); 1533 _callprojs.catchall_ioproj->set_req(0, top()); 1534 } 1535 #ifndef PRODUCT 1536 if (PrintEliminateAllocations) { 1537 if (alloc->is_AllocateArray()) { 1538 tty->print_cr("++++ Eliminated: %d AllocateArray", alloc->_idx); 1539 } else { 1540 tty->print_cr("++++ Eliminated: %d Allocate", alloc->_idx); 1541 } 1542 } 1543 #endif 1544 _igvn.remove_dead_node(alloc); 1545 } 1546 1547 void PhaseMacroExpand::expand_initialize_membar(AllocateNode* alloc, InitializeNode* init, 1548 Node*& fast_oop_ctrl, Node*& fast_oop_rawmem) { 1549 // If initialization is performed by an array copy, any required 1550 // MemBarStoreStore was already added. If the object does not 1551 // escape no need for a MemBarStoreStore. If the object does not 1552 // escape in its initializer and memory barrier (MemBarStoreStore or 1553 // stronger) is already added at exit of initializer, also no need 1554 // for a MemBarStoreStore. Otherwise we need a MemBarStoreStore 1555 // so that stores that initialize this object can't be reordered 1556 // with a subsequent store that makes this object accessible by 1557 // other threads. 1558 // Other threads include java threads and JVM internal threads 1559 // (for example concurrent GC threads). Current concurrent GC 1560 // implementation: G1 will not scan newly created object, 1561 // so it's safe to skip storestore barrier when allocation does 1562 // not escape. 1563 if (!alloc->does_not_escape_thread() && 1564 !alloc->is_allocation_MemBar_redundant() && 1565 (init == NULL || !init->is_complete_with_arraycopy())) { 1566 if (init == NULL || init->req() < InitializeNode::RawStores) { 1567 // No InitializeNode or no stores captured by zeroing 1568 // elimination. Simply add the MemBarStoreStore after object 1569 // initialization. 1570 MemBarNode* mb = MemBarNode::make(C, Op_MemBarStoreStore, Compile::AliasIdxBot); 1571 transform_later(mb); 1572 1573 mb->init_req(TypeFunc::Memory, fast_oop_rawmem); 1574 mb->init_req(TypeFunc::Control, fast_oop_ctrl); 1575 fast_oop_ctrl = new ProjNode(mb, TypeFunc::Control); 1576 transform_later(fast_oop_ctrl); 1577 fast_oop_rawmem = new ProjNode(mb, TypeFunc::Memory); 1578 transform_later(fast_oop_rawmem); 1579 } else { 1580 // Add the MemBarStoreStore after the InitializeNode so that 1581 // all stores performing the initialization that were moved 1582 // before the InitializeNode happen before the storestore 1583 // barrier. 1584 1585 Node* init_ctrl = init->proj_out_or_null(TypeFunc::Control); 1586 Node* init_mem = init->proj_out_or_null(TypeFunc::Memory); 1587 1588 MemBarNode* mb = MemBarNode::make(C, Op_MemBarStoreStore, Compile::AliasIdxBot); 1589 transform_later(mb); 1590 1591 Node* ctrl = new ProjNode(init, TypeFunc::Control); 1592 transform_later(ctrl); 1593 Node* mem = new ProjNode(init, TypeFunc::Memory); 1594 transform_later(mem); 1595 1596 // The MemBarStoreStore depends on control and memory coming 1597 // from the InitializeNode 1598 mb->init_req(TypeFunc::Memory, mem); 1599 mb->init_req(TypeFunc::Control, ctrl); 1600 1601 ctrl = new ProjNode(mb, TypeFunc::Control); 1602 transform_later(ctrl); 1603 mem = new ProjNode(mb, TypeFunc::Memory); 1604 transform_later(mem); 1605 1606 // All nodes that depended on the InitializeNode for control 1607 // and memory must now depend on the MemBarNode that itself 1608 // depends on the InitializeNode 1609 if (init_ctrl != NULL) { 1610 _igvn.replace_node(init_ctrl, ctrl); 1611 } 1612 if (init_mem != NULL) { 1613 _igvn.replace_node(init_mem, mem); 1614 } 1615 } 1616 } 1617 } 1618 1619 void PhaseMacroExpand::expand_dtrace_alloc_probe(AllocateNode* alloc, Node* oop, 1620 Node*& ctrl, Node*& rawmem) { 1621 if (C->env()->dtrace_alloc_probes()) { 1622 // Slow-path call 1623 int size = TypeFunc::Parms + 2; 1624 CallLeafNode *call = new CallLeafNode(OptoRuntime::dtrace_object_alloc_Type(), 1625 CAST_FROM_FN_PTR(address, 1626 static_cast<int (*)(JavaThread*, oopDesc*)>(SharedRuntime::dtrace_object_alloc)), 1627 "dtrace_object_alloc", 1628 TypeRawPtr::BOTTOM); 1629 1630 // Get base of thread-local storage area 1631 Node* thread = new ThreadLocalNode(); 1632 transform_later(thread); 1633 1634 call->init_req(TypeFunc::Parms + 0, thread); 1635 call->init_req(TypeFunc::Parms + 1, oop); 1636 call->init_req(TypeFunc::Control, ctrl); 1637 call->init_req(TypeFunc::I_O , top()); // does no i/o 1638 call->init_req(TypeFunc::Memory , rawmem); 1639 call->init_req(TypeFunc::ReturnAdr, alloc->in(TypeFunc::ReturnAdr)); 1640 call->init_req(TypeFunc::FramePtr, alloc->in(TypeFunc::FramePtr)); 1641 transform_later(call); 1642 ctrl = new ProjNode(call, TypeFunc::Control); 1643 transform_later(ctrl); 1644 rawmem = new ProjNode(call, TypeFunc::Memory); 1645 transform_later(rawmem); 1646 } 1647 } 1648 1649 // Helper for PhaseMacroExpand::expand_allocate_common. 1650 // Initializes the newly-allocated storage. 1651 Node* 1652 PhaseMacroExpand::initialize_object(AllocateNode* alloc, 1653 Node* control, Node* rawmem, Node* object, 1654 Node* klass_node, Node* length, 1655 Node* size_in_bytes) { 1656 InitializeNode* init = alloc->initialization(); 1657 // Store the klass & mark bits 1658 Node* mark_node = alloc->make_ideal_mark(&_igvn, object, control, rawmem); 1659 if (!mark_node->is_Con()) { 1660 transform_later(mark_node); 1661 } 1662 rawmem = make_store(control, rawmem, object, oopDesc::mark_offset_in_bytes(), mark_node, TypeX_X->basic_type()); 1663 1664 if (!UseCompactObjectHeaders) { 1665 rawmem = make_store(control, rawmem, object, oopDesc::klass_offset_in_bytes(), klass_node, T_METADATA); 1666 } 1667 1668 int header_size = alloc->minimum_header_size(); // conservatively small 1669 1670 // Array length 1671 if (length != NULL) { // Arrays need length field 1672 rawmem = make_store(control, rawmem, object, arrayOopDesc::length_offset_in_bytes(), length, T_INT); 1673 // conservatively small header size: 1674 header_size = arrayOopDesc::base_offset_in_bytes(T_BYTE); 1675 if (_igvn.type(klass_node)->isa_aryklassptr()) { // we know the exact header size in most cases: 1676 BasicType elem = _igvn.type(klass_node)->is_klassptr()->as_instance_type()->isa_aryptr()->elem()->array_element_basic_type(); 1677 if (is_reference_type(elem, true)) { 1678 elem = T_OBJECT; 1679 } 1680 header_size = Klass::layout_helper_header_size(Klass::array_layout_helper(elem)); 1681 } 1682 } 1683 1684 // Clear the object body, if necessary. 1685 if (init == NULL) { 1686 // The init has somehow disappeared; be cautious and clear everything. 1687 // 1688 // This can happen if a node is allocated but an uncommon trap occurs 1689 // immediately. In this case, the Initialize gets associated with the 1690 // trap, and may be placed in a different (outer) loop, if the Allocate 1691 // is in a loop. If (this is rare) the inner loop gets unrolled, then 1692 // there can be two Allocates to one Initialize. The answer in all these 1693 // edge cases is safety first. It is always safe to clear immediately 1694 // within an Allocate, and then (maybe or maybe not) clear some more later. 1695 if (!(UseTLAB && ZeroTLAB)) { 1696 rawmem = ClearArrayNode::clear_memory(control, rawmem, object, 1697 header_size, size_in_bytes, 1698 &_igvn); 1699 } 1700 } else { 1701 if (!init->is_complete()) { 1702 // Try to win by zeroing only what the init does not store. 1703 // We can also try to do some peephole optimizations, 1704 // such as combining some adjacent subword stores. 1705 rawmem = init->complete_stores(control, rawmem, object, 1706 header_size, size_in_bytes, &_igvn); 1707 } 1708 // We have no more use for this link, since the AllocateNode goes away: 1709 init->set_req(InitializeNode::RawAddress, top()); 1710 // (If we keep the link, it just confuses the register allocator, 1711 // who thinks he sees a real use of the address by the membar.) 1712 } 1713 1714 return rawmem; 1715 } 1716 1717 // Generate prefetch instructions for next allocations. 1718 Node* PhaseMacroExpand::prefetch_allocation(Node* i_o, Node*& needgc_false, 1719 Node*& contended_phi_rawmem, 1720 Node* old_eden_top, Node* new_eden_top, 1721 intx lines) { 1722 enum { fall_in_path = 1, pf_path = 2 }; 1723 if( UseTLAB && AllocatePrefetchStyle == 2 ) { 1724 // Generate prefetch allocation with watermark check. 1725 // As an allocation hits the watermark, we will prefetch starting 1726 // at a "distance" away from watermark. 1727 1728 Node *pf_region = new RegionNode(3); 1729 Node *pf_phi_rawmem = new PhiNode( pf_region, Type::MEMORY, 1730 TypeRawPtr::BOTTOM ); 1731 // I/O is used for Prefetch 1732 Node *pf_phi_abio = new PhiNode( pf_region, Type::ABIO ); 1733 1734 Node *thread = new ThreadLocalNode(); 1735 transform_later(thread); 1736 1737 Node *eden_pf_adr = new AddPNode( top()/*not oop*/, thread, 1738 _igvn.MakeConX(in_bytes(JavaThread::tlab_pf_top_offset())) ); 1739 transform_later(eden_pf_adr); 1740 1741 Node *old_pf_wm = new LoadPNode(needgc_false, 1742 contended_phi_rawmem, eden_pf_adr, 1743 TypeRawPtr::BOTTOM, TypeRawPtr::BOTTOM, 1744 MemNode::unordered); 1745 transform_later(old_pf_wm); 1746 1747 // check against new_eden_top 1748 Node *need_pf_cmp = new CmpPNode( new_eden_top, old_pf_wm ); 1749 transform_later(need_pf_cmp); 1750 Node *need_pf_bol = new BoolNode( need_pf_cmp, BoolTest::ge ); 1751 transform_later(need_pf_bol); 1752 IfNode *need_pf_iff = new IfNode( needgc_false, need_pf_bol, 1753 PROB_UNLIKELY_MAG(4), COUNT_UNKNOWN ); 1754 transform_later(need_pf_iff); 1755 1756 // true node, add prefetchdistance 1757 Node *need_pf_true = new IfTrueNode( need_pf_iff ); 1758 transform_later(need_pf_true); 1759 1760 Node *need_pf_false = new IfFalseNode( need_pf_iff ); 1761 transform_later(need_pf_false); 1762 1763 Node *new_pf_wmt = new AddPNode( top(), old_pf_wm, 1764 _igvn.MakeConX(AllocatePrefetchDistance) ); 1765 transform_later(new_pf_wmt ); 1766 new_pf_wmt->set_req(0, need_pf_true); 1767 1768 Node *store_new_wmt = new StorePNode(need_pf_true, 1769 contended_phi_rawmem, eden_pf_adr, 1770 TypeRawPtr::BOTTOM, new_pf_wmt, 1771 MemNode::unordered); 1772 transform_later(store_new_wmt); 1773 1774 // adding prefetches 1775 pf_phi_abio->init_req( fall_in_path, i_o ); 1776 1777 Node *prefetch_adr; 1778 Node *prefetch; 1779 uint step_size = AllocatePrefetchStepSize; 1780 uint distance = 0; 1781 1782 for ( intx i = 0; i < lines; i++ ) { 1783 prefetch_adr = new AddPNode( old_pf_wm, new_pf_wmt, 1784 _igvn.MakeConX(distance) ); 1785 transform_later(prefetch_adr); 1786 prefetch = new PrefetchAllocationNode( i_o, prefetch_adr ); 1787 transform_later(prefetch); 1788 distance += step_size; 1789 i_o = prefetch; 1790 } 1791 pf_phi_abio->set_req( pf_path, i_o ); 1792 1793 pf_region->init_req( fall_in_path, need_pf_false ); 1794 pf_region->init_req( pf_path, need_pf_true ); 1795 1796 pf_phi_rawmem->init_req( fall_in_path, contended_phi_rawmem ); 1797 pf_phi_rawmem->init_req( pf_path, store_new_wmt ); 1798 1799 transform_later(pf_region); 1800 transform_later(pf_phi_rawmem); 1801 transform_later(pf_phi_abio); 1802 1803 needgc_false = pf_region; 1804 contended_phi_rawmem = pf_phi_rawmem; 1805 i_o = pf_phi_abio; 1806 } else if( UseTLAB && AllocatePrefetchStyle == 3 ) { 1807 // Insert a prefetch instruction for each allocation. 1808 // This code is used to generate 1 prefetch instruction per cache line. 1809 1810 // Generate several prefetch instructions. 1811 uint step_size = AllocatePrefetchStepSize; 1812 uint distance = AllocatePrefetchDistance; 1813 1814 // Next cache address. 1815 Node *cache_adr = new AddPNode(old_eden_top, old_eden_top, 1816 _igvn.MakeConX(step_size + distance)); 1817 transform_later(cache_adr); 1818 cache_adr = new CastP2XNode(needgc_false, cache_adr); 1819 transform_later(cache_adr); 1820 // Address is aligned to execute prefetch to the beginning of cache line size 1821 // (it is important when BIS instruction is used on SPARC as prefetch). 1822 Node* mask = _igvn.MakeConX(~(intptr_t)(step_size-1)); 1823 cache_adr = new AndXNode(cache_adr, mask); 1824 transform_later(cache_adr); 1825 cache_adr = new CastX2PNode(cache_adr); 1826 transform_later(cache_adr); 1827 1828 // Prefetch 1829 Node *prefetch = new PrefetchAllocationNode( contended_phi_rawmem, cache_adr ); 1830 prefetch->set_req(0, needgc_false); 1831 transform_later(prefetch); 1832 contended_phi_rawmem = prefetch; 1833 Node *prefetch_adr; 1834 distance = step_size; 1835 for ( intx i = 1; i < lines; i++ ) { 1836 prefetch_adr = new AddPNode( cache_adr, cache_adr, 1837 _igvn.MakeConX(distance) ); 1838 transform_later(prefetch_adr); 1839 prefetch = new PrefetchAllocationNode( contended_phi_rawmem, prefetch_adr ); 1840 transform_later(prefetch); 1841 distance += step_size; 1842 contended_phi_rawmem = prefetch; 1843 } 1844 } else if( AllocatePrefetchStyle > 0 ) { 1845 // Insert a prefetch for each allocation only on the fast-path 1846 Node *prefetch_adr; 1847 Node *prefetch; 1848 // Generate several prefetch instructions. 1849 uint step_size = AllocatePrefetchStepSize; 1850 uint distance = AllocatePrefetchDistance; 1851 for ( intx i = 0; i < lines; i++ ) { 1852 prefetch_adr = new AddPNode( old_eden_top, new_eden_top, 1853 _igvn.MakeConX(distance) ); 1854 transform_later(prefetch_adr); 1855 prefetch = new PrefetchAllocationNode( i_o, prefetch_adr ); 1856 // Do not let it float too high, since if eden_top == eden_end, 1857 // both might be null. 1858 if( i == 0 ) { // Set control for first prefetch, next follows it 1859 prefetch->init_req(0, needgc_false); 1860 } 1861 transform_later(prefetch); 1862 distance += step_size; 1863 i_o = prefetch; 1864 } 1865 } 1866 return i_o; 1867 } 1868 1869 1870 void PhaseMacroExpand::expand_allocate(AllocateNode *alloc) { 1871 expand_allocate_common(alloc, NULL, 1872 OptoRuntime::new_instance_Type(), 1873 OptoRuntime::new_instance_Java(), NULL); 1874 } 1875 1876 void PhaseMacroExpand::expand_allocate_array(AllocateArrayNode *alloc) { 1877 Node* length = alloc->in(AllocateNode::ALength); 1878 Node* valid_length_test = alloc->in(AllocateNode::ValidLengthTest); 1879 InitializeNode* init = alloc->initialization(); 1880 Node* klass_node = alloc->in(AllocateNode::KlassNode); 1881 const TypeAryKlassPtr* ary_klass_t = _igvn.type(klass_node)->isa_aryklassptr(); 1882 address slow_call_address; // Address of slow call 1883 if (init != NULL && init->is_complete_with_arraycopy() && 1884 ary_klass_t && ary_klass_t->elem()->isa_klassptr() == NULL) { 1885 // Don't zero type array during slow allocation in VM since 1886 // it will be initialized later by arraycopy in compiled code. 1887 slow_call_address = OptoRuntime::new_array_nozero_Java(); 1888 } else { 1889 slow_call_address = OptoRuntime::new_array_Java(); 1890 } 1891 expand_allocate_common(alloc, length, 1892 OptoRuntime::new_array_Type(), 1893 slow_call_address, valid_length_test); 1894 } 1895 1896 //-------------------mark_eliminated_box---------------------------------- 1897 // 1898 // During EA obj may point to several objects but after few ideal graph 1899 // transformations (CCP) it may point to only one non escaping object 1900 // (but still using phi), corresponding locks and unlocks will be marked 1901 // for elimination. Later obj could be replaced with a new node (new phi) 1902 // and which does not have escape information. And later after some graph 1903 // reshape other locks and unlocks (which were not marked for elimination 1904 // before) are connected to this new obj (phi) but they still will not be 1905 // marked for elimination since new obj has no escape information. 1906 // Mark all associated (same box and obj) lock and unlock nodes for 1907 // elimination if some of them marked already. 1908 void PhaseMacroExpand::mark_eliminated_box(Node* oldbox, Node* obj) { 1909 if (oldbox->as_BoxLock()->is_eliminated()) { 1910 return; // This BoxLock node was processed already. 1911 } 1912 // New implementation (EliminateNestedLocks) has separate BoxLock 1913 // node for each locked region so mark all associated locks/unlocks as 1914 // eliminated even if different objects are referenced in one locked region 1915 // (for example, OSR compilation of nested loop inside locked scope). 1916 if (EliminateNestedLocks || 1917 oldbox->as_BoxLock()->is_simple_lock_region(NULL, obj, NULL)) { 1918 // Box is used only in one lock region. Mark this box as eliminated. 1919 _igvn.hash_delete(oldbox); 1920 oldbox->as_BoxLock()->set_eliminated(); // This changes box's hash value 1921 _igvn.hash_insert(oldbox); 1922 1923 for (uint i = 0; i < oldbox->outcnt(); i++) { 1924 Node* u = oldbox->raw_out(i); 1925 if (u->is_AbstractLock() && !u->as_AbstractLock()->is_non_esc_obj()) { 1926 AbstractLockNode* alock = u->as_AbstractLock(); 1927 // Check lock's box since box could be referenced by Lock's debug info. 1928 if (alock->box_node() == oldbox) { 1929 // Mark eliminated all related locks and unlocks. 1930 #ifdef ASSERT 1931 alock->log_lock_optimization(C, "eliminate_lock_set_non_esc4"); 1932 #endif 1933 alock->set_non_esc_obj(); 1934 } 1935 } 1936 } 1937 return; 1938 } 1939 1940 // Create new "eliminated" BoxLock node and use it in monitor debug info 1941 // instead of oldbox for the same object. 1942 BoxLockNode* newbox = oldbox->clone()->as_BoxLock(); 1943 1944 // Note: BoxLock node is marked eliminated only here and it is used 1945 // to indicate that all associated lock and unlock nodes are marked 1946 // for elimination. 1947 newbox->set_eliminated(); 1948 transform_later(newbox); 1949 1950 // Replace old box node with new box for all users of the same object. 1951 for (uint i = 0; i < oldbox->outcnt();) { 1952 bool next_edge = true; 1953 1954 Node* u = oldbox->raw_out(i); 1955 if (u->is_AbstractLock()) { 1956 AbstractLockNode* alock = u->as_AbstractLock(); 1957 if (alock->box_node() == oldbox && alock->obj_node()->eqv_uncast(obj)) { 1958 // Replace Box and mark eliminated all related locks and unlocks. 1959 #ifdef ASSERT 1960 alock->log_lock_optimization(C, "eliminate_lock_set_non_esc5"); 1961 #endif 1962 alock->set_non_esc_obj(); 1963 _igvn.rehash_node_delayed(alock); 1964 alock->set_box_node(newbox); 1965 next_edge = false; 1966 } 1967 } 1968 if (u->is_FastLock() && u->as_FastLock()->obj_node()->eqv_uncast(obj)) { 1969 FastLockNode* flock = u->as_FastLock(); 1970 assert(flock->box_node() == oldbox, "sanity"); 1971 _igvn.rehash_node_delayed(flock); 1972 flock->set_box_node(newbox); 1973 next_edge = false; 1974 } 1975 1976 // Replace old box in monitor debug info. 1977 if (u->is_SafePoint() && u->as_SafePoint()->jvms()) { 1978 SafePointNode* sfn = u->as_SafePoint(); 1979 JVMState* youngest_jvms = sfn->jvms(); 1980 int max_depth = youngest_jvms->depth(); 1981 for (int depth = 1; depth <= max_depth; depth++) { 1982 JVMState* jvms = youngest_jvms->of_depth(depth); 1983 int num_mon = jvms->nof_monitors(); 1984 // Loop over monitors 1985 for (int idx = 0; idx < num_mon; idx++) { 1986 Node* obj_node = sfn->monitor_obj(jvms, idx); 1987 Node* box_node = sfn->monitor_box(jvms, idx); 1988 if (box_node == oldbox && obj_node->eqv_uncast(obj)) { 1989 int j = jvms->monitor_box_offset(idx); 1990 _igvn.replace_input_of(u, j, newbox); 1991 next_edge = false; 1992 } 1993 } 1994 } 1995 } 1996 if (next_edge) i++; 1997 } 1998 } 1999 2000 //-----------------------mark_eliminated_locking_nodes----------------------- 2001 void PhaseMacroExpand::mark_eliminated_locking_nodes(AbstractLockNode *alock) { 2002 if (EliminateNestedLocks) { 2003 if (alock->is_nested()) { 2004 assert(alock->box_node()->as_BoxLock()->is_eliminated(), "sanity"); 2005 return; 2006 } else if (!alock->is_non_esc_obj()) { // Not eliminated or coarsened 2007 // Only Lock node has JVMState needed here. 2008 // Not that preceding claim is documented anywhere else. 2009 if (alock->jvms() != NULL) { 2010 if (alock->as_Lock()->is_nested_lock_region()) { 2011 // Mark eliminated related nested locks and unlocks. 2012 Node* obj = alock->obj_node(); 2013 BoxLockNode* box_node = alock->box_node()->as_BoxLock(); 2014 assert(!box_node->is_eliminated(), "should not be marked yet"); 2015 // Note: BoxLock node is marked eliminated only here 2016 // and it is used to indicate that all associated lock 2017 // and unlock nodes are marked for elimination. 2018 box_node->set_eliminated(); // Box's hash is always NO_HASH here 2019 for (uint i = 0; i < box_node->outcnt(); i++) { 2020 Node* u = box_node->raw_out(i); 2021 if (u->is_AbstractLock()) { 2022 alock = u->as_AbstractLock(); 2023 if (alock->box_node() == box_node) { 2024 // Verify that this Box is referenced only by related locks. 2025 assert(alock->obj_node()->eqv_uncast(obj), ""); 2026 // Mark all related locks and unlocks. 2027 #ifdef ASSERT 2028 alock->log_lock_optimization(C, "eliminate_lock_set_nested"); 2029 #endif 2030 alock->set_nested(); 2031 } 2032 } 2033 } 2034 } else { 2035 #ifdef ASSERT 2036 alock->log_lock_optimization(C, "eliminate_lock_NOT_nested_lock_region"); 2037 if (C->log() != NULL) 2038 alock->as_Lock()->is_nested_lock_region(C); // rerun for debugging output 2039 #endif 2040 } 2041 } 2042 return; 2043 } 2044 // Process locks for non escaping object 2045 assert(alock->is_non_esc_obj(), ""); 2046 } // EliminateNestedLocks 2047 2048 if (alock->is_non_esc_obj()) { // Lock is used for non escaping object 2049 // Look for all locks of this object and mark them and 2050 // corresponding BoxLock nodes as eliminated. 2051 Node* obj = alock->obj_node(); 2052 for (uint j = 0; j < obj->outcnt(); j++) { 2053 Node* o = obj->raw_out(j); 2054 if (o->is_AbstractLock() && 2055 o->as_AbstractLock()->obj_node()->eqv_uncast(obj)) { 2056 alock = o->as_AbstractLock(); 2057 Node* box = alock->box_node(); 2058 // Replace old box node with new eliminated box for all users 2059 // of the same object and mark related locks as eliminated. 2060 mark_eliminated_box(box, obj); 2061 } 2062 } 2063 } 2064 } 2065 2066 // we have determined that this lock/unlock can be eliminated, we simply 2067 // eliminate the node without expanding it. 2068 // 2069 // Note: The membar's associated with the lock/unlock are currently not 2070 // eliminated. This should be investigated as a future enhancement. 2071 // 2072 bool PhaseMacroExpand::eliminate_locking_node(AbstractLockNode *alock) { 2073 2074 if (!alock->is_eliminated()) { 2075 return false; 2076 } 2077 #ifdef ASSERT 2078 if (!alock->is_coarsened()) { 2079 // Check that new "eliminated" BoxLock node is created. 2080 BoxLockNode* oldbox = alock->box_node()->as_BoxLock(); 2081 assert(oldbox->is_eliminated(), "should be done already"); 2082 } 2083 #endif 2084 2085 alock->log_lock_optimization(C, "eliminate_lock"); 2086 2087 #ifndef PRODUCT 2088 if (PrintEliminateLocks) { 2089 tty->print_cr("++++ Eliminated: %d %s '%s'", alock->_idx, (alock->is_Lock() ? "Lock" : "Unlock"), alock->kind_as_string()); 2090 } 2091 #endif 2092 2093 Node* mem = alock->in(TypeFunc::Memory); 2094 Node* ctrl = alock->in(TypeFunc::Control); 2095 guarantee(ctrl != NULL, "missing control projection, cannot replace_node() with NULL"); 2096 2097 alock->extract_projections(&_callprojs, false /*separate_io_proj*/, false /*do_asserts*/); 2098 // There are 2 projections from the lock. The lock node will 2099 // be deleted when its last use is subsumed below. 2100 assert(alock->outcnt() == 2 && 2101 _callprojs.fallthrough_proj != NULL && 2102 _callprojs.fallthrough_memproj != NULL, 2103 "Unexpected projections from Lock/Unlock"); 2104 2105 Node* fallthroughproj = _callprojs.fallthrough_proj; 2106 Node* memproj_fallthrough = _callprojs.fallthrough_memproj; 2107 2108 // The memory projection from a lock/unlock is RawMem 2109 // The input to a Lock is merged memory, so extract its RawMem input 2110 // (unless the MergeMem has been optimized away.) 2111 if (alock->is_Lock()) { 2112 // Search for MemBarAcquireLock node and delete it also. 2113 MemBarNode* membar = fallthroughproj->unique_ctrl_out()->as_MemBar(); 2114 assert(membar != NULL && membar->Opcode() == Op_MemBarAcquireLock, ""); 2115 Node* ctrlproj = membar->proj_out(TypeFunc::Control); 2116 Node* memproj = membar->proj_out(TypeFunc::Memory); 2117 _igvn.replace_node(ctrlproj, fallthroughproj); 2118 _igvn.replace_node(memproj, memproj_fallthrough); 2119 2120 // Delete FastLock node also if this Lock node is unique user 2121 // (a loop peeling may clone a Lock node). 2122 Node* flock = alock->as_Lock()->fastlock_node(); 2123 if (flock->outcnt() == 1) { 2124 assert(flock->unique_out() == alock, "sanity"); 2125 _igvn.replace_node(flock, top()); 2126 } 2127 } 2128 2129 // Search for MemBarReleaseLock node and delete it also. 2130 if (alock->is_Unlock() && ctrl->is_Proj() && ctrl->in(0)->is_MemBar()) { 2131 MemBarNode* membar = ctrl->in(0)->as_MemBar(); 2132 assert(membar->Opcode() == Op_MemBarReleaseLock && 2133 mem->is_Proj() && membar == mem->in(0), ""); 2134 _igvn.replace_node(fallthroughproj, ctrl); 2135 _igvn.replace_node(memproj_fallthrough, mem); 2136 fallthroughproj = ctrl; 2137 memproj_fallthrough = mem; 2138 ctrl = membar->in(TypeFunc::Control); 2139 mem = membar->in(TypeFunc::Memory); 2140 } 2141 2142 _igvn.replace_node(fallthroughproj, ctrl); 2143 _igvn.replace_node(memproj_fallthrough, mem); 2144 return true; 2145 } 2146 2147 2148 //------------------------------expand_lock_node---------------------- 2149 void PhaseMacroExpand::expand_lock_node(LockNode *lock) { 2150 2151 Node* ctrl = lock->in(TypeFunc::Control); 2152 Node* mem = lock->in(TypeFunc::Memory); 2153 Node* obj = lock->obj_node(); 2154 Node* box = lock->box_node(); 2155 Node* flock = lock->fastlock_node(); 2156 2157 assert(!box->as_BoxLock()->is_eliminated(), "sanity"); 2158 2159 // Make the merge point 2160 Node *region; 2161 Node *mem_phi; 2162 Node *slow_path; 2163 2164 region = new RegionNode(3); 2165 // create a Phi for the memory state 2166 mem_phi = new PhiNode( region, Type::MEMORY, TypeRawPtr::BOTTOM); 2167 2168 // Optimize test; set region slot 2 2169 slow_path = opt_bits_test(ctrl, region, 2, flock, 0, 0); 2170 mem_phi->init_req(2, mem); 2171 2172 // Make slow path call 2173 CallNode *call = make_slow_call((CallNode *) lock, OptoRuntime::complete_monitor_enter_Type(), 2174 OptoRuntime::complete_monitor_locking_Java(), NULL, slow_path, 2175 obj, box, NULL); 2176 2177 call->extract_projections(&_callprojs, false /*separate_io_proj*/, false /*do_asserts*/); 2178 2179 // Slow path can only throw asynchronous exceptions, which are always 2180 // de-opted. So the compiler thinks the slow-call can never throw an 2181 // exception. If it DOES throw an exception we would need the debug 2182 // info removed first (since if it throws there is no monitor). 2183 assert(_callprojs.fallthrough_ioproj == NULL && _callprojs.catchall_ioproj == NULL && 2184 _callprojs.catchall_memproj == NULL && _callprojs.catchall_catchproj == NULL, "Unexpected projection from Lock"); 2185 2186 // Capture slow path 2187 // disconnect fall-through projection from call and create a new one 2188 // hook up users of fall-through projection to region 2189 Node *slow_ctrl = _callprojs.fallthrough_proj->clone(); 2190 transform_later(slow_ctrl); 2191 _igvn.hash_delete(_callprojs.fallthrough_proj); 2192 _callprojs.fallthrough_proj->disconnect_inputs(C); 2193 region->init_req(1, slow_ctrl); 2194 // region inputs are now complete 2195 transform_later(region); 2196 _igvn.replace_node(_callprojs.fallthrough_proj, region); 2197 2198 Node *memproj = transform_later(new ProjNode(call, TypeFunc::Memory)); 2199 2200 mem_phi->init_req(1, memproj); 2201 2202 transform_later(mem_phi); 2203 2204 _igvn.replace_node(_callprojs.fallthrough_memproj, mem_phi); 2205 } 2206 2207 //------------------------------expand_unlock_node---------------------- 2208 void PhaseMacroExpand::expand_unlock_node(UnlockNode *unlock) { 2209 2210 Node* ctrl = unlock->in(TypeFunc::Control); 2211 Node* mem = unlock->in(TypeFunc::Memory); 2212 Node* obj = unlock->obj_node(); 2213 Node* box = unlock->box_node(); 2214 2215 assert(!box->as_BoxLock()->is_eliminated(), "sanity"); 2216 2217 // No need for a null check on unlock 2218 2219 // Make the merge point 2220 Node *region; 2221 Node *mem_phi; 2222 2223 region = new RegionNode(3); 2224 // create a Phi for the memory state 2225 mem_phi = new PhiNode( region, Type::MEMORY, TypeRawPtr::BOTTOM); 2226 2227 FastUnlockNode *funlock = new FastUnlockNode( ctrl, obj, box ); 2228 funlock = transform_later( funlock )->as_FastUnlock(); 2229 // Optimize test; set region slot 2 2230 Node *slow_path = opt_bits_test(ctrl, region, 2, funlock, 0, 0); 2231 Node *thread = transform_later(new ThreadLocalNode()); 2232 2233 CallNode *call = make_slow_call((CallNode *) unlock, OptoRuntime::complete_monitor_exit_Type(), 2234 CAST_FROM_FN_PTR(address, SharedRuntime::complete_monitor_unlocking_C), 2235 "complete_monitor_unlocking_C", slow_path, obj, box, thread); 2236 2237 call->extract_projections(&_callprojs, false /*separate_io_proj*/, false /*do_asserts*/); 2238 assert(_callprojs.fallthrough_ioproj == NULL && _callprojs.catchall_ioproj == NULL && 2239 _callprojs.catchall_memproj == NULL && _callprojs.catchall_catchproj == NULL, "Unexpected projection from Lock"); 2240 2241 // No exceptions for unlocking 2242 // Capture slow path 2243 // disconnect fall-through projection from call and create a new one 2244 // hook up users of fall-through projection to region 2245 Node *slow_ctrl = _callprojs.fallthrough_proj->clone(); 2246 transform_later(slow_ctrl); 2247 _igvn.hash_delete(_callprojs.fallthrough_proj); 2248 _callprojs.fallthrough_proj->disconnect_inputs(C); 2249 region->init_req(1, slow_ctrl); 2250 // region inputs are now complete 2251 transform_later(region); 2252 _igvn.replace_node(_callprojs.fallthrough_proj, region); 2253 2254 Node *memproj = transform_later(new ProjNode(call, TypeFunc::Memory) ); 2255 mem_phi->init_req(1, memproj ); 2256 mem_phi->init_req(2, mem); 2257 transform_later(mem_phi); 2258 2259 _igvn.replace_node(_callprojs.fallthrough_memproj, mem_phi); 2260 } 2261 2262 void PhaseMacroExpand::expand_subtypecheck_node(SubTypeCheckNode *check) { 2263 assert(check->in(SubTypeCheckNode::Control) == NULL, "should be pinned"); 2264 Node* bol = check->unique_out(); 2265 Node* obj_or_subklass = check->in(SubTypeCheckNode::ObjOrSubKlass); 2266 Node* superklass = check->in(SubTypeCheckNode::SuperKlass); 2267 assert(bol->is_Bool() && bol->as_Bool()->_test._test == BoolTest::ne, "unexpected bool node"); 2268 2269 for (DUIterator_Last imin, i = bol->last_outs(imin); i >= imin; --i) { 2270 Node* iff = bol->last_out(i); 2271 assert(iff->is_If(), "where's the if?"); 2272 2273 if (iff->in(0)->is_top()) { 2274 _igvn.replace_input_of(iff, 1, C->top()); 2275 continue; 2276 } 2277 2278 Node* iftrue = iff->as_If()->proj_out(1); 2279 Node* iffalse = iff->as_If()->proj_out(0); 2280 Node* ctrl = iff->in(0); 2281 2282 Node* subklass = NULL; 2283 if (_igvn.type(obj_or_subklass)->isa_klassptr()) { 2284 subklass = obj_or_subklass; 2285 } else { 2286 Node* k_adr = basic_plus_adr(obj_or_subklass, oopDesc::klass_offset_in_bytes()); 2287 subklass = _igvn.transform(LoadKlassNode::make(_igvn, NULL, C->immutable_memory(), k_adr, TypeInstPtr::KLASS)); 2288 } 2289 2290 Node* not_subtype_ctrl = Phase::gen_subtype_check(subklass, superklass, &ctrl, NULL, _igvn); 2291 2292 _igvn.replace_input_of(iff, 0, C->top()); 2293 _igvn.replace_node(iftrue, not_subtype_ctrl); 2294 _igvn.replace_node(iffalse, ctrl); 2295 } 2296 _igvn.replace_node(check, C->top()); 2297 } 2298 2299 //---------------------------eliminate_macro_nodes---------------------- 2300 // Eliminate scalar replaced allocations and associated locks. 2301 void PhaseMacroExpand::eliminate_macro_nodes() { 2302 if (C->macro_count() == 0) 2303 return; 2304 NOT_PRODUCT(int membar_before = count_MemBar(C);) 2305 2306 // Before elimination may re-mark (change to Nested or NonEscObj) 2307 // all associated (same box and obj) lock and unlock nodes. 2308 int cnt = C->macro_count(); 2309 for (int i=0; i < cnt; i++) { 2310 Node *n = C->macro_node(i); 2311 if (n->is_AbstractLock()) { // Lock and Unlock nodes 2312 mark_eliminated_locking_nodes(n->as_AbstractLock()); 2313 } 2314 } 2315 // Re-marking may break consistency of Coarsened locks. 2316 if (!C->coarsened_locks_consistent()) { 2317 return; // recompile without Coarsened locks if broken 2318 } 2319 2320 // First, attempt to eliminate locks 2321 bool progress = true; 2322 while (progress) { 2323 progress = false; 2324 for (int i = C->macro_count(); i > 0; i = MIN2(i - 1, C->macro_count())) { // more than 1 element can be eliminated at once 2325 Node* n = C->macro_node(i - 1); 2326 bool success = false; 2327 DEBUG_ONLY(int old_macro_count = C->macro_count();) 2328 if (n->is_AbstractLock()) { 2329 success = eliminate_locking_node(n->as_AbstractLock()); 2330 #ifndef PRODUCT 2331 if (success && PrintOptoStatistics) { 2332 Atomic::inc(&PhaseMacroExpand::_monitor_objects_removed_counter); 2333 } 2334 #endif 2335 } 2336 assert(success == (C->macro_count() < old_macro_count), "elimination reduces macro count"); 2337 progress = progress || success; 2338 } 2339 } 2340 // Next, attempt to eliminate allocations 2341 _has_locks = false; 2342 progress = true; 2343 while (progress) { 2344 progress = false; 2345 for (int i = C->macro_count(); i > 0; i = MIN2(i - 1, C->macro_count())) { // more than 1 element can be eliminated at once 2346 Node* n = C->macro_node(i - 1); 2347 bool success = false; 2348 DEBUG_ONLY(int old_macro_count = C->macro_count();) 2349 switch (n->class_id()) { 2350 case Node::Class_Allocate: 2351 case Node::Class_AllocateArray: 2352 success = eliminate_allocate_node(n->as_Allocate()); 2353 #ifndef PRODUCT 2354 if (success && PrintOptoStatistics) { 2355 Atomic::inc(&PhaseMacroExpand::_objs_scalar_replaced_counter); 2356 } 2357 #endif 2358 break; 2359 case Node::Class_CallStaticJava: 2360 success = eliminate_boxing_node(n->as_CallStaticJava()); 2361 break; 2362 case Node::Class_Lock: 2363 case Node::Class_Unlock: 2364 assert(!n->as_AbstractLock()->is_eliminated(), "sanity"); 2365 _has_locks = true; 2366 break; 2367 case Node::Class_ArrayCopy: 2368 break; 2369 case Node::Class_OuterStripMinedLoop: 2370 break; 2371 case Node::Class_SubTypeCheck: 2372 break; 2373 case Node::Class_Opaque1: 2374 break; 2375 default: 2376 assert(n->Opcode() == Op_LoopLimit || 2377 n->Opcode() == Op_Opaque3 || 2378 n->Opcode() == Op_Opaque4 || 2379 BarrierSet::barrier_set()->barrier_set_c2()->is_gc_barrier_node(n), 2380 "unknown node type in macro list"); 2381 } 2382 assert(success == (C->macro_count() < old_macro_count), "elimination reduces macro count"); 2383 progress = progress || success; 2384 } 2385 } 2386 #ifndef PRODUCT 2387 if (PrintOptoStatistics) { 2388 int membar_after = count_MemBar(C); 2389 Atomic::add(&PhaseMacroExpand::_memory_barriers_removed_counter, membar_before - membar_after); 2390 } 2391 #endif 2392 } 2393 2394 //------------------------------expand_macro_nodes---------------------- 2395 // Returns true if a failure occurred. 2396 bool PhaseMacroExpand::expand_macro_nodes() { 2397 // Last attempt to eliminate macro nodes. 2398 eliminate_macro_nodes(); 2399 if (C->failing()) return true; 2400 2401 // Eliminate Opaque and LoopLimit nodes. Do it after all loop optimizations. 2402 bool progress = true; 2403 while (progress) { 2404 progress = false; 2405 for (int i = C->macro_count(); i > 0; i--) { 2406 Node* n = C->macro_node(i-1); 2407 bool success = false; 2408 DEBUG_ONLY(int old_macro_count = C->macro_count();) 2409 if (n->Opcode() == Op_LoopLimit) { 2410 // Remove it from macro list and put on IGVN worklist to optimize. 2411 C->remove_macro_node(n); 2412 _igvn._worklist.push(n); 2413 success = true; 2414 } else if (n->Opcode() == Op_CallStaticJava) { 2415 // Remove it from macro list and put on IGVN worklist to optimize. 2416 C->remove_macro_node(n); 2417 _igvn._worklist.push(n); 2418 success = true; 2419 } else if (n->is_Opaque1()) { 2420 _igvn.replace_node(n, n->in(1)); 2421 success = true; 2422 #if INCLUDE_RTM_OPT 2423 } else if ((n->Opcode() == Op_Opaque3) && ((Opaque3Node*)n)->rtm_opt()) { 2424 assert(C->profile_rtm(), "should be used only in rtm deoptimization code"); 2425 assert((n->outcnt() == 1) && n->unique_out()->is_Cmp(), ""); 2426 Node* cmp = n->unique_out(); 2427 #ifdef ASSERT 2428 // Validate graph. 2429 assert((cmp->outcnt() == 1) && cmp->unique_out()->is_Bool(), ""); 2430 BoolNode* bol = cmp->unique_out()->as_Bool(); 2431 assert((bol->outcnt() == 1) && bol->unique_out()->is_If() && 2432 (bol->_test._test == BoolTest::ne), ""); 2433 IfNode* ifn = bol->unique_out()->as_If(); 2434 assert((ifn->outcnt() == 2) && 2435 ifn->proj_out(1)->is_uncommon_trap_proj(Deoptimization::Reason_rtm_state_change) != NULL, ""); 2436 #endif 2437 Node* repl = n->in(1); 2438 if (!_has_locks) { 2439 // Remove RTM state check if there are no locks in the code. 2440 // Replace input to compare the same value. 2441 repl = (cmp->in(1) == n) ? cmp->in(2) : cmp->in(1); 2442 } 2443 _igvn.replace_node(n, repl); 2444 success = true; 2445 #endif 2446 } else if (n->Opcode() == Op_Opaque4) { 2447 // With Opaque4 nodes, the expectation is that the test of input 1 2448 // is always equal to the constant value of input 2. So we can 2449 // remove the Opaque4 and replace it by input 2. In debug builds, 2450 // leave the non constant test in instead to sanity check that it 2451 // never fails (if it does, that subgraph was constructed so, at 2452 // runtime, a Halt node is executed). 2453 #ifdef ASSERT 2454 _igvn.replace_node(n, n->in(1)); 2455 #else 2456 _igvn.replace_node(n, n->in(2)); 2457 #endif 2458 success = true; 2459 } else if (n->Opcode() == Op_OuterStripMinedLoop) { 2460 n->as_OuterStripMinedLoop()->adjust_strip_mined_loop(&_igvn); 2461 C->remove_macro_node(n); 2462 success = true; 2463 } 2464 assert(!success || (C->macro_count() == (old_macro_count - 1)), "elimination must have deleted one node from macro list"); 2465 progress = progress || success; 2466 } 2467 } 2468 2469 // Clean up the graph so we're less likely to hit the maximum node 2470 // limit 2471 _igvn.set_delay_transform(false); 2472 _igvn.optimize(); 2473 if (C->failing()) return true; 2474 _igvn.set_delay_transform(true); 2475 2476 2477 // Because we run IGVN after each expansion, some macro nodes may go 2478 // dead and be removed from the list as we iterate over it. Move 2479 // Allocate nodes (processed in a second pass) at the beginning of 2480 // the list and then iterate from the last element of the list until 2481 // an Allocate node is seen. This is robust to random deletion in 2482 // the list due to nodes going dead. 2483 C->sort_macro_nodes(); 2484 2485 // expand arraycopy "macro" nodes first 2486 // For ReduceBulkZeroing, we must first process all arraycopy nodes 2487 // before the allocate nodes are expanded. 2488 while (C->macro_count() > 0) { 2489 int macro_count = C->macro_count(); 2490 Node * n = C->macro_node(macro_count-1); 2491 assert(n->is_macro(), "only macro nodes expected here"); 2492 if (_igvn.type(n) == Type::TOP || (n->in(0) != NULL && n->in(0)->is_top())) { 2493 // node is unreachable, so don't try to expand it 2494 C->remove_macro_node(n); 2495 continue; 2496 } 2497 if (n->is_Allocate()) { 2498 break; 2499 } 2500 // Make sure expansion will not cause node limit to be exceeded. 2501 // Worst case is a macro node gets expanded into about 200 nodes. 2502 // Allow 50% more for optimization. 2503 if (C->check_node_count(300, "out of nodes before macro expansion")) { 2504 return true; 2505 } 2506 2507 DEBUG_ONLY(int old_macro_count = C->macro_count();) 2508 switch (n->class_id()) { 2509 case Node::Class_Lock: 2510 expand_lock_node(n->as_Lock()); 2511 break; 2512 case Node::Class_Unlock: 2513 expand_unlock_node(n->as_Unlock()); 2514 break; 2515 case Node::Class_ArrayCopy: 2516 expand_arraycopy_node(n->as_ArrayCopy()); 2517 break; 2518 case Node::Class_SubTypeCheck: 2519 expand_subtypecheck_node(n->as_SubTypeCheck()); 2520 break; 2521 default: 2522 assert(false, "unknown node type in macro list"); 2523 } 2524 assert(C->macro_count() == (old_macro_count - 1), "expansion must have deleted one node from macro list"); 2525 if (C->failing()) return true; 2526 2527 // Clean up the graph so we're less likely to hit the maximum node 2528 // limit 2529 _igvn.set_delay_transform(false); 2530 _igvn.optimize(); 2531 if (C->failing()) return true; 2532 _igvn.set_delay_transform(true); 2533 } 2534 2535 // All nodes except Allocate nodes are expanded now. There could be 2536 // new optimization opportunities (such as folding newly created 2537 // load from a just allocated object). Run IGVN. 2538 2539 // expand "macro" nodes 2540 // nodes are removed from the macro list as they are processed 2541 while (C->macro_count() > 0) { 2542 int macro_count = C->macro_count(); 2543 Node * n = C->macro_node(macro_count-1); 2544 assert(n->is_macro(), "only macro nodes expected here"); 2545 if (_igvn.type(n) == Type::TOP || (n->in(0) != NULL && n->in(0)->is_top())) { 2546 // node is unreachable, so don't try to expand it 2547 C->remove_macro_node(n); 2548 continue; 2549 } 2550 // Make sure expansion will not cause node limit to be exceeded. 2551 // Worst case is a macro node gets expanded into about 200 nodes. 2552 // Allow 50% more for optimization. 2553 if (C->check_node_count(300, "out of nodes before macro expansion")) { 2554 return true; 2555 } 2556 switch (n->class_id()) { 2557 case Node::Class_Allocate: 2558 expand_allocate(n->as_Allocate()); 2559 break; 2560 case Node::Class_AllocateArray: 2561 expand_allocate_array(n->as_AllocateArray()); 2562 break; 2563 default: 2564 assert(false, "unknown node type in macro list"); 2565 } 2566 assert(C->macro_count() < macro_count, "must have deleted a node from macro list"); 2567 if (C->failing()) return true; 2568 2569 // Clean up the graph so we're less likely to hit the maximum node 2570 // limit 2571 _igvn.set_delay_transform(false); 2572 _igvn.optimize(); 2573 if (C->failing()) return true; 2574 _igvn.set_delay_transform(true); 2575 } 2576 2577 _igvn.set_delay_transform(false); 2578 return false; 2579 } 2580 2581 #ifndef PRODUCT 2582 int PhaseMacroExpand::_objs_scalar_replaced_counter = 0; 2583 int PhaseMacroExpand::_monitor_objects_removed_counter = 0; 2584 int PhaseMacroExpand::_GC_barriers_removed_counter = 0; 2585 int PhaseMacroExpand::_memory_barriers_removed_counter = 0; 2586 2587 void PhaseMacroExpand::print_statistics() { 2588 tty->print("Objects scalar replaced = %d, ", Atomic::load(&_objs_scalar_replaced_counter)); 2589 tty->print("Monitor objects removed = %d, ", Atomic::load(&_monitor_objects_removed_counter)); 2590 tty->print("GC barriers removed = %d, ", Atomic::load(&_GC_barriers_removed_counter)); 2591 tty->print_cr("Memory barriers removed = %d", Atomic::load(&_memory_barriers_removed_counter)); 2592 } 2593 2594 int PhaseMacroExpand::count_MemBar(Compile *C) { 2595 if (!PrintOptoStatistics) { 2596 return 0; 2597 } 2598 Unique_Node_List ideal_nodes; 2599 int total = 0; 2600 ideal_nodes.map(C->live_nodes(), NULL); 2601 ideal_nodes.push(C->root()); 2602 for (uint next = 0; next < ideal_nodes.size(); ++next) { 2603 Node* n = ideal_nodes.at(next); 2604 if (n->is_MemBar()) { 2605 total++; 2606 } 2607 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) { 2608 Node* m = n->fast_out(i); 2609 ideal_nodes.push(m); 2610 } 2611 } 2612 return total; 2613 } 2614 #endif