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