1 /* 2 * Copyright (c) 2005, 2018, 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 "libadt/vectset.hpp" 29 #include "opto/addnode.hpp" 30 #include "opto/arraycopynode.hpp" 31 #include "opto/callnode.hpp" 32 #include "opto/castnode.hpp" 33 #include "opto/cfgnode.hpp" 34 #include "opto/compile.hpp" 35 #include "opto/convertnode.hpp" 36 #include "opto/graphKit.hpp" 37 #include "opto/locknode.hpp" 38 #include "opto/loopnode.hpp" 39 #include "opto/macro.hpp" 40 #include "opto/memnode.hpp" 41 #include "opto/narrowptrnode.hpp" 42 #include "opto/node.hpp" 43 #include "opto/opaquenode.hpp" 44 #include "opto/phaseX.hpp" 45 #include "opto/rootnode.hpp" 46 #include "opto/runtime.hpp" 47 #include "opto/subnode.hpp" 48 #include "opto/type.hpp" 49 #include "opto/valuetypenode.hpp" 50 #include "runtime/sharedRuntime.hpp" 51 #if INCLUDE_G1GC 52 #include "gc/g1/g1ThreadLocalData.hpp" 53 #endif // INCLUDE_G1GC 54 55 56 // 57 // Replace any references to "oldref" in inputs to "use" with "newref". 58 // Returns the number of replacements made. 59 // 60 int PhaseMacroExpand::replace_input(Node *use, Node *oldref, Node *newref) { 61 int nreplacements = 0; 62 uint req = use->req(); 63 for (uint j = 0; j < use->len(); j++) { 64 Node *uin = use->in(j); 65 if (uin == oldref) { 66 if (j < req) 67 use->set_req(j, newref); 68 else 69 use->set_prec(j, newref); 70 nreplacements++; 71 } else if (j >= req && uin == NULL) { 72 break; 73 } 74 } 75 return nreplacements; 76 } 77 78 void PhaseMacroExpand::copy_call_debug_info(CallNode *oldcall, CallNode * newcall) { 79 // Copy debug information and adjust JVMState information 80 uint old_dbg_start = oldcall->tf()->domain_sig()->cnt(); 81 uint new_dbg_start = newcall->tf()->domain_sig()->cnt(); 82 int jvms_adj = new_dbg_start - old_dbg_start; 83 assert (new_dbg_start == newcall->req(), "argument count mismatch"); 84 85 // SafePointScalarObject node could be referenced several times in debug info. 86 // Use Dict to record cloned nodes. 87 Dict* sosn_map = new Dict(cmpkey,hashkey); 88 for (uint i = old_dbg_start; i < oldcall->req(); i++) { 89 Node* old_in = oldcall->in(i); 90 // Clone old SafePointScalarObjectNodes, adjusting their field contents. 91 if (old_in != NULL && old_in->is_SafePointScalarObject()) { 92 SafePointScalarObjectNode* old_sosn = old_in->as_SafePointScalarObject(); 93 uint old_unique = C->unique(); 94 Node* new_in = old_sosn->clone(sosn_map); 95 if (old_unique != C->unique()) { // New node? 96 new_in->set_req(0, C->root()); // reset control edge 97 new_in = transform_later(new_in); // Register new node. 98 } 99 old_in = new_in; 100 } 101 newcall->add_req(old_in); 102 } 103 104 // JVMS may be shared so clone it before we modify it 105 newcall->set_jvms(oldcall->jvms() != NULL ? oldcall->jvms()->clone_deep(C) : NULL); 106 for (JVMState *jvms = newcall->jvms(); jvms != NULL; jvms = jvms->caller()) { 107 jvms->set_map(newcall); 108 jvms->set_locoff(jvms->locoff()+jvms_adj); 109 jvms->set_stkoff(jvms->stkoff()+jvms_adj); 110 jvms->set_monoff(jvms->monoff()+jvms_adj); 111 jvms->set_scloff(jvms->scloff()+jvms_adj); 112 jvms->set_endoff(jvms->endoff()+jvms_adj); 113 } 114 } 115 116 Node* PhaseMacroExpand::opt_bits_test(Node* ctrl, Node* region, int edge, Node* word, int mask, int bits, bool return_fast_path) { 117 Node* cmp; 118 if (mask != 0) { 119 Node* and_node = transform_later(new AndXNode(word, MakeConX(mask))); 120 cmp = transform_later(new CmpXNode(and_node, MakeConX(bits))); 121 } else { 122 cmp = word; 123 } 124 Node* bol = transform_later(new BoolNode(cmp, BoolTest::ne)); 125 IfNode* iff = new IfNode( ctrl, bol, PROB_MIN, COUNT_UNKNOWN ); 126 transform_later(iff); 127 128 // Fast path taken. 129 Node *fast_taken = transform_later(new IfFalseNode(iff)); 130 131 // Fast path not-taken, i.e. slow path 132 Node *slow_taken = transform_later(new IfTrueNode(iff)); 133 134 if (return_fast_path) { 135 region->init_req(edge, slow_taken); // Capture slow-control 136 return fast_taken; 137 } else { 138 region->init_req(edge, fast_taken); // Capture fast-control 139 return slow_taken; 140 } 141 } 142 143 //--------------------copy_predefined_input_for_runtime_call-------------------- 144 void PhaseMacroExpand::copy_predefined_input_for_runtime_call(Node * ctrl, CallNode* oldcall, CallNode* call) { 145 // Set fixed predefined input arguments 146 call->init_req( TypeFunc::Control, ctrl ); 147 call->init_req( TypeFunc::I_O , oldcall->in( TypeFunc::I_O) ); 148 call->init_req( TypeFunc::Memory , oldcall->in( TypeFunc::Memory ) ); // ????? 149 call->init_req( TypeFunc::ReturnAdr, oldcall->in( TypeFunc::ReturnAdr ) ); 150 call->init_req( TypeFunc::FramePtr, oldcall->in( TypeFunc::FramePtr ) ); 151 } 152 153 //------------------------------make_slow_call--------------------------------- 154 CallNode* PhaseMacroExpand::make_slow_call(CallNode *oldcall, const TypeFunc* slow_call_type, 155 address slow_call, const char* leaf_name, Node* slow_path, 156 Node* parm0, Node* parm1, Node* parm2) { 157 158 // Slow-path call 159 CallNode *call = leaf_name 160 ? (CallNode*)new CallLeafNode ( slow_call_type, slow_call, leaf_name, TypeRawPtr::BOTTOM ) 161 : (CallNode*)new CallStaticJavaNode( slow_call_type, slow_call, OptoRuntime::stub_name(slow_call), oldcall->jvms()->bci(), TypeRawPtr::BOTTOM ); 162 163 // Slow path call has no side-effects, uses few values 164 copy_predefined_input_for_runtime_call(slow_path, oldcall, call ); 165 if (parm0 != NULL) call->init_req(TypeFunc::Parms+0, parm0); 166 if (parm1 != NULL) call->init_req(TypeFunc::Parms+1, parm1); 167 if (parm2 != NULL) call->init_req(TypeFunc::Parms+2, parm2); 168 copy_call_debug_info(oldcall, call); 169 call->set_cnt(PROB_UNLIKELY_MAG(4)); // Same effect as RC_UNCOMMON. 170 _igvn.replace_node(oldcall, call); 171 transform_later(call); 172 173 return call; 174 } 175 176 void PhaseMacroExpand::extract_call_projections(CallNode *call) { 177 _fallthroughproj = NULL; 178 _fallthroughcatchproj = NULL; 179 _ioproj_fallthrough = NULL; 180 _ioproj_catchall = NULL; 181 _catchallcatchproj = NULL; 182 _memproj_fallthrough = NULL; 183 _memproj_catchall = NULL; 184 _resproj = NULL; 185 for (DUIterator_Fast imax, i = call->fast_outs(imax); i < imax; i++) { 186 ProjNode *pn = call->fast_out(i)->as_Proj(); 187 switch (pn->_con) { 188 case TypeFunc::Control: 189 { 190 // For Control (fallthrough) and I_O (catch_all_index) we have CatchProj -> Catch -> Proj 191 _fallthroughproj = pn; 192 DUIterator_Fast jmax, j = pn->fast_outs(jmax); 193 const Node *cn = pn->fast_out(j); 194 if (cn->is_Catch()) { 195 ProjNode *cpn = NULL; 196 for (DUIterator_Fast kmax, k = cn->fast_outs(kmax); k < kmax; k++) { 197 cpn = cn->fast_out(k)->as_Proj(); 198 assert(cpn->is_CatchProj(), "must be a CatchProjNode"); 199 if (cpn->_con == CatchProjNode::fall_through_index) 200 _fallthroughcatchproj = cpn; 201 else { 202 assert(cpn->_con == CatchProjNode::catch_all_index, "must be correct index."); 203 _catchallcatchproj = cpn; 204 } 205 } 206 } 207 break; 208 } 209 case TypeFunc::I_O: 210 if (pn->_is_io_use) 211 _ioproj_catchall = pn; 212 else 213 _ioproj_fallthrough = pn; 214 break; 215 case TypeFunc::Memory: 216 if (pn->_is_io_use) 217 _memproj_catchall = pn; 218 else 219 _memproj_fallthrough = pn; 220 break; 221 case TypeFunc::Parms: 222 _resproj = pn; 223 break; 224 default: 225 assert(false, "unexpected projection from allocation node."); 226 } 227 } 228 229 } 230 231 // Eliminate a card mark sequence. p2x is a ConvP2XNode 232 void PhaseMacroExpand::eliminate_card_mark(Node* p2x) { 233 assert(p2x->Opcode() == Op_CastP2X, "ConvP2XNode required"); 234 if (!UseG1GC) { 235 // vanilla/CMS post barrier 236 Node *shift = p2x->unique_out(); 237 Node *addp = shift->unique_out(); 238 for (DUIterator_Last jmin, j = addp->last_outs(jmin); j >= jmin; --j) { 239 Node *mem = addp->last_out(j); 240 if (UseCondCardMark && mem->is_Load()) { 241 assert(mem->Opcode() == Op_LoadB, "unexpected code shape"); 242 // The load is checking if the card has been written so 243 // replace it with zero to fold the test. 244 _igvn.replace_node(mem, intcon(0)); 245 continue; 246 } 247 assert(mem->is_Store(), "store required"); 248 _igvn.replace_node(mem, mem->in(MemNode::Memory)); 249 } 250 } 251 #if INCLUDE_G1GC 252 else { 253 // G1 pre/post barriers 254 assert(p2x->outcnt() <= 2, "expects 1 or 2 users: Xor and URShift nodes"); 255 // It could be only one user, URShift node, in Object.clone() intrinsic 256 // but the new allocation is passed to arraycopy stub and it could not 257 // be scalar replaced. So we don't check the case. 258 259 // An other case of only one user (Xor) is when the value check for NULL 260 // in G1 post barrier is folded after CCP so the code which used URShift 261 // is removed. 262 263 // Take Region node before eliminating post barrier since it also 264 // eliminates CastP2X node when it has only one user. 265 Node* this_region = p2x->in(0); 266 assert(this_region != NULL, ""); 267 268 // Remove G1 post barrier. 269 270 // Search for CastP2X->Xor->URShift->Cmp path which 271 // checks if the store done to a different from the value's region. 272 // And replace Cmp with #0 (false) to collapse G1 post barrier. 273 Node* xorx = p2x->find_out_with(Op_XorX); 274 if (xorx != NULL) { 275 Node* shift = xorx->unique_out(); 276 Node* cmpx = shift->unique_out(); 277 assert(cmpx->is_Cmp() && cmpx->unique_out()->is_Bool() && 278 cmpx->unique_out()->as_Bool()->_test._test == BoolTest::ne, 279 "missing region check in G1 post barrier"); 280 _igvn.replace_node(cmpx, makecon(TypeInt::CC_EQ)); 281 282 // Remove G1 pre barrier. 283 284 // Search "if (marking != 0)" check and set it to "false". 285 // There is no G1 pre barrier if previous stored value is NULL 286 // (for example, after initialization). 287 if (this_region->is_Region() && this_region->req() == 3) { 288 int ind = 1; 289 if (!this_region->in(ind)->is_IfFalse()) { 290 ind = 2; 291 } 292 if (this_region->in(ind)->is_IfFalse() && 293 this_region->in(ind)->in(0)->Opcode() == Op_If) { 294 Node* bol = this_region->in(ind)->in(0)->in(1); 295 assert(bol->is_Bool(), ""); 296 cmpx = bol->in(1); 297 if (bol->as_Bool()->_test._test == BoolTest::ne && 298 cmpx->is_Cmp() && cmpx->in(2) == intcon(0) && 299 cmpx->in(1)->is_Load()) { 300 Node* adr = cmpx->in(1)->as_Load()->in(MemNode::Address); 301 const int marking_offset = in_bytes(G1ThreadLocalData::satb_mark_queue_active_offset()); 302 if (adr->is_AddP() && adr->in(AddPNode::Base) == top() && 303 adr->in(AddPNode::Address)->Opcode() == Op_ThreadLocal && 304 adr->in(AddPNode::Offset) == MakeConX(marking_offset)) { 305 _igvn.replace_node(cmpx, makecon(TypeInt::CC_EQ)); 306 } 307 } 308 } 309 } 310 } else { 311 assert(!GraphKit::use_ReduceInitialCardMarks(), "can only happen with card marking"); 312 // This is a G1 post barrier emitted by the Object.clone() intrinsic. 313 // Search for the CastP2X->URShiftX->AddP->LoadB->Cmp path which checks if the card 314 // is marked as young_gen and replace the Cmp with 0 (false) to collapse the barrier. 315 Node* shift = p2x->find_out_with(Op_URShiftX); 316 assert(shift != NULL, "missing G1 post barrier"); 317 Node* addp = shift->unique_out(); 318 Node* load = addp->find_out_with(Op_LoadB); 319 assert(load != NULL, "missing G1 post barrier"); 320 Node* cmpx = load->unique_out(); 321 assert(cmpx->is_Cmp() && cmpx->unique_out()->is_Bool() && 322 cmpx->unique_out()->as_Bool()->_test._test == BoolTest::ne, 323 "missing card value check in G1 post barrier"); 324 _igvn.replace_node(cmpx, makecon(TypeInt::CC_EQ)); 325 // There is no G1 pre barrier in this case 326 } 327 // Now CastP2X can be removed since it is used only on dead path 328 // which currently still alive until igvn optimize it. 329 assert(p2x->outcnt() == 0 || p2x->unique_out()->Opcode() == Op_URShiftX, ""); 330 _igvn.replace_node(p2x, top()); 331 } 332 #endif // INCLUDE_G1GC 333 } 334 335 // Search for a memory operation for the specified memory slice. 336 static Node *scan_mem_chain(Node *mem, int alias_idx, int offset, Node *start_mem, Node *alloc, PhaseGVN *phase) { 337 Node *orig_mem = mem; 338 Node *alloc_mem = alloc->in(TypeFunc::Memory); 339 const TypeOopPtr *tinst = phase->C->get_adr_type(alias_idx)->isa_oopptr(); 340 while (true) { 341 if (mem == alloc_mem || mem == start_mem ) { 342 return mem; // hit one of our sentinels 343 } else if (mem->is_MergeMem()) { 344 mem = mem->as_MergeMem()->memory_at(alias_idx); 345 } else if (mem->is_Proj() && mem->as_Proj()->_con == TypeFunc::Memory) { 346 Node *in = mem->in(0); 347 // we can safely skip over safepoints, calls, locks and membars because we 348 // already know that the object is safe to eliminate. 349 if (in->is_Initialize() && in->as_Initialize()->allocation() == alloc) { 350 return in; 351 } else if (in->is_Call()) { 352 CallNode *call = in->as_Call(); 353 if (call->may_modify(tinst, phase)) { 354 assert(call->is_ArrayCopy(), "ArrayCopy is the only call node that doesn't make allocation escape"); 355 if (call->as_ArrayCopy()->modifies(offset, offset, phase, false)) { 356 return in; 357 } 358 } 359 mem = in->in(TypeFunc::Memory); 360 } else if (in->is_MemBar()) { 361 ArrayCopyNode* ac = NULL; 362 if (ArrayCopyNode::may_modify(tinst, in->as_MemBar(), phase, ac)) { 363 assert(ac != NULL && ac->is_clonebasic(), "Only basic clone is a non escaping clone"); 364 return ac; 365 } 366 mem = in->in(TypeFunc::Memory); 367 } else { 368 assert(false, "unexpected projection"); 369 } 370 } else if (mem->is_Store()) { 371 const TypePtr* atype = mem->as_Store()->adr_type(); 372 int adr_idx = phase->C->get_alias_index(atype); 373 if (adr_idx == alias_idx) { 374 assert(atype->isa_oopptr(), "address type must be oopptr"); 375 int adr_offset = atype->flattened_offset(); 376 uint adr_iid = atype->is_oopptr()->instance_id(); 377 // Array elements references have the same alias_idx 378 // but different offset and different instance_id. 379 if (adr_offset == offset && adr_iid == alloc->_idx) 380 return mem; 381 } else { 382 assert(adr_idx == Compile::AliasIdxRaw, "address must match or be raw"); 383 } 384 mem = mem->in(MemNode::Memory); 385 } else if (mem->is_ClearArray()) { 386 if (!ClearArrayNode::step_through(&mem, alloc->_idx, phase)) { 387 // Can not bypass initialization of the instance 388 // we are looking. 389 debug_only(intptr_t offset;) 390 assert(alloc == AllocateNode::Ideal_allocation(mem->in(3), phase, offset), "sanity"); 391 InitializeNode* init = alloc->as_Allocate()->initialization(); 392 // We are looking for stored value, return Initialize node 393 // or memory edge from Allocate node. 394 if (init != NULL) 395 return init; 396 else 397 return alloc->in(TypeFunc::Memory); // It will produce zero value (see callers). 398 } 399 // Otherwise skip it (the call updated 'mem' value). 400 } else if (mem->Opcode() == Op_SCMemProj) { 401 mem = mem->in(0); 402 Node* adr = NULL; 403 if (mem->is_LoadStore()) { 404 adr = mem->in(MemNode::Address); 405 } else { 406 assert(mem->Opcode() == Op_EncodeISOArray || 407 mem->Opcode() == Op_StrCompressedCopy, "sanity"); 408 adr = mem->in(3); // Destination array 409 } 410 const TypePtr* atype = adr->bottom_type()->is_ptr(); 411 int adr_idx = phase->C->get_alias_index(atype); 412 if (adr_idx == alias_idx) { 413 DEBUG_ONLY(mem->dump();) 414 assert(false, "Object is not scalar replaceable if a LoadStore node accesses its field"); 415 return NULL; 416 } 417 mem = mem->in(MemNode::Memory); 418 } else if (mem->Opcode() == Op_StrInflatedCopy) { 419 Node* adr = mem->in(3); // Destination array 420 const TypePtr* atype = adr->bottom_type()->is_ptr(); 421 int adr_idx = phase->C->get_alias_index(atype); 422 if (adr_idx == alias_idx) { 423 DEBUG_ONLY(mem->dump();) 424 assert(false, "Object is not scalar replaceable if a StrInflatedCopy node accesses its field"); 425 return NULL; 426 } 427 mem = mem->in(MemNode::Memory); 428 } else { 429 return mem; 430 } 431 assert(mem != orig_mem, "dead memory loop"); 432 } 433 } 434 435 // Generate loads from source of the arraycopy for fields of 436 // destination needed at a deoptimization point 437 Node* PhaseMacroExpand::make_arraycopy_load(ArrayCopyNode* ac, intptr_t offset, Node* ctl, Node* mem, BasicType ft, const Type *ftype, AllocateNode *alloc) { 438 BasicType bt = ft; 439 const Type *type = ftype; 440 if (ft == T_NARROWOOP) { 441 bt = T_OBJECT; 442 type = ftype->make_oopptr(); 443 } 444 Node* res = NULL; 445 if (ac->is_clonebasic()) { 446 Node* base = ac->in(ArrayCopyNode::Src)->in(AddPNode::Base); 447 Node* adr = _igvn.transform(new AddPNode(base, base, MakeConX(offset))); 448 const TypePtr* adr_type = _igvn.type(base)->is_ptr()->add_offset(offset); 449 res = LoadNode::make(_igvn, ctl, mem, adr, adr_type, type, bt, MemNode::unordered, LoadNode::Pinned); 450 } else { 451 if (ac->modifies(offset, offset, &_igvn, true)) { 452 assert(ac->in(ArrayCopyNode::Dest) == alloc->result_cast(), "arraycopy destination should be allocation's result"); 453 uint shift = exact_log2(type2aelembytes(bt)); 454 Node* diff = _igvn.transform(new SubINode(ac->in(ArrayCopyNode::SrcPos), ac->in(ArrayCopyNode::DestPos))); 455 #ifdef _LP64 456 diff = _igvn.transform(new ConvI2LNode(diff)); 457 #endif 458 diff = _igvn.transform(new LShiftXNode(diff, intcon(shift))); 459 460 Node* off = _igvn.transform(new AddXNode(MakeConX(offset), diff)); 461 Node* base = ac->in(ArrayCopyNode::Src); 462 Node* adr = _igvn.transform(new AddPNode(base, base, off)); 463 const TypePtr* adr_type = _igvn.type(base)->is_ptr()->add_offset(offset); 464 res = LoadNode::make(_igvn, ctl, mem, adr, adr_type, type, bt, MemNode::unordered, LoadNode::Pinned); 465 } 466 } 467 if (res != NULL) { 468 res = _igvn.transform(res); 469 if (ftype->isa_narrowoop()) { 470 // PhaseMacroExpand::scalar_replacement adds DecodeN nodes 471 res = _igvn.transform(new EncodePNode(res, ftype)); 472 } 473 return res; 474 } 475 return NULL; 476 } 477 478 // 479 // Given a Memory Phi, compute a value Phi containing the values from stores 480 // on the input paths. 481 // Note: this function is recursive, its depth is limited by the "level" argument 482 // Returns the computed Phi, or NULL if it cannot compute it. 483 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) { 484 assert(mem->is_Phi(), "sanity"); 485 int alias_idx = C->get_alias_index(adr_t); 486 int offset = adr_t->flattened_offset(); 487 int instance_id = adr_t->instance_id(); 488 489 // Check if an appropriate value phi already exists. 490 Node* region = mem->in(0); 491 for (DUIterator_Fast kmax, k = region->fast_outs(kmax); k < kmax; k++) { 492 Node* phi = region->fast_out(k); 493 if (phi->is_Phi() && phi != mem && 494 phi->as_Phi()->is_same_inst_field(phi_type, (int)mem->_idx, instance_id, alias_idx, offset)) { 495 return phi; 496 } 497 } 498 // Check if an appropriate new value phi already exists. 499 Node* new_phi = value_phis->find(mem->_idx); 500 if (new_phi != NULL) 501 return new_phi; 502 503 if (level <= 0) { 504 return NULL; // Give up: phi tree too deep 505 } 506 Node *start_mem = C->start()->proj_out_or_null(TypeFunc::Memory); 507 Node *alloc_mem = alloc->in(TypeFunc::Memory); 508 509 uint length = mem->req(); 510 GrowableArray <Node *> values(length, length, NULL, false); 511 512 // create a new Phi for the value 513 PhiNode *phi = new PhiNode(mem->in(0), phi_type, NULL, mem->_idx, instance_id, alias_idx, offset); 514 transform_later(phi); 515 value_phis->push(phi, mem->_idx); 516 517 for (uint j = 1; j < length; j++) { 518 Node *in = mem->in(j); 519 if (in == NULL || in->is_top()) { 520 values.at_put(j, in); 521 } else { 522 Node *val = scan_mem_chain(in, alias_idx, offset, start_mem, alloc, &_igvn); 523 if (val == start_mem || val == alloc_mem) { 524 // hit a sentinel, return appropriate 0 value 525 values.at_put(j, _igvn.zerocon(ft)); 526 continue; 527 } 528 if (val->is_Initialize()) { 529 val = val->as_Initialize()->find_captured_store(offset, type2aelembytes(ft), &_igvn); 530 } 531 if (val == NULL) { 532 return NULL; // can't find a value on this path 533 } 534 if (val == mem) { 535 values.at_put(j, mem); 536 } else if (val->is_Store()) { 537 values.at_put(j, val->in(MemNode::ValueIn)); 538 } else if(val->is_Proj() && val->in(0) == alloc) { 539 values.at_put(j, _igvn.zerocon(ft)); 540 } else if (val->is_Phi()) { 541 val = value_from_mem_phi(val, ft, phi_type, adr_t, alloc, value_phis, level-1); 542 if (val == NULL) { 543 return NULL; 544 } 545 values.at_put(j, val); 546 } else if (val->Opcode() == Op_SCMemProj) { 547 assert(val->in(0)->is_LoadStore() || 548 val->in(0)->Opcode() == Op_EncodeISOArray || 549 val->in(0)->Opcode() == Op_StrCompressedCopy, "sanity"); 550 assert(false, "Object is not scalar replaceable if a LoadStore node accesses its field"); 551 return NULL; 552 } else if (val->is_ArrayCopy()) { 553 Node* res = make_arraycopy_load(val->as_ArrayCopy(), offset, val->in(0), val->in(TypeFunc::Memory), ft, phi_type, alloc); 554 if (res == NULL) { 555 return NULL; 556 } 557 values.at_put(j, res); 558 } else { 559 #ifdef ASSERT 560 val->dump(); 561 assert(false, "unknown node on this path"); 562 #endif 563 return NULL; // unknown node on this path 564 } 565 } 566 } 567 // Set Phi's inputs 568 for (uint j = 1; j < length; j++) { 569 if (values.at(j) == mem) { 570 phi->init_req(j, phi); 571 } else { 572 phi->init_req(j, values.at(j)); 573 } 574 } 575 return phi; 576 } 577 578 // Search the last value stored into the object's field. 579 Node *PhaseMacroExpand::value_from_mem(Node *sfpt_mem, Node *sfpt_ctl, BasicType ft, const Type *ftype, const TypeOopPtr *adr_t, AllocateNode *alloc) { 580 assert(adr_t->is_known_instance_field(), "instance required"); 581 int instance_id = adr_t->instance_id(); 582 assert((uint)instance_id == alloc->_idx, "wrong allocation"); 583 584 int alias_idx = C->get_alias_index(adr_t); 585 int offset = adr_t->flattened_offset(); 586 Node *start_mem = C->start()->proj_out_or_null(TypeFunc::Memory); 587 Node *alloc_ctrl = alloc->in(TypeFunc::Control); 588 Node *alloc_mem = alloc->in(TypeFunc::Memory); 589 Arena *a = Thread::current()->resource_area(); 590 VectorSet visited(a); 591 592 bool done = sfpt_mem == alloc_mem; 593 Node *mem = sfpt_mem; 594 while (!done) { 595 if (visited.test_set(mem->_idx)) { 596 return NULL; // found a loop, give up 597 } 598 mem = scan_mem_chain(mem, alias_idx, offset, start_mem, alloc, &_igvn); 599 if (mem == start_mem || mem == alloc_mem) { 600 done = true; // hit a sentinel, return appropriate 0 value 601 } else if (mem->is_Initialize()) { 602 mem = mem->as_Initialize()->find_captured_store(offset, type2aelembytes(ft), &_igvn); 603 if (mem == NULL) { 604 done = true; // Something went wrong. 605 } else if (mem->is_Store()) { 606 const TypePtr* atype = mem->as_Store()->adr_type(); 607 assert(C->get_alias_index(atype) == Compile::AliasIdxRaw, "store is correct memory slice"); 608 done = true; 609 } 610 } else if (mem->is_Store()) { 611 const TypeOopPtr* atype = mem->as_Store()->adr_type()->isa_oopptr(); 612 assert(atype != NULL, "address type must be oopptr"); 613 assert(C->get_alias_index(atype) == alias_idx && 614 atype->is_known_instance_field() && atype->flattened_offset() == offset && 615 atype->instance_id() == instance_id, "store is correct memory slice"); 616 done = true; 617 } else if (mem->is_Phi()) { 618 // try to find a phi's unique input 619 Node *unique_input = NULL; 620 Node *top = C->top(); 621 for (uint i = 1; i < mem->req(); i++) { 622 Node *n = scan_mem_chain(mem->in(i), alias_idx, offset, start_mem, alloc, &_igvn); 623 if (n == NULL || n == top || n == mem) { 624 continue; 625 } else if (unique_input == NULL) { 626 unique_input = n; 627 } else if (unique_input != n) { 628 unique_input = top; 629 break; 630 } 631 } 632 if (unique_input != NULL && unique_input != top) { 633 mem = unique_input; 634 } else { 635 done = true; 636 } 637 } else if (mem->is_ArrayCopy()) { 638 done = true; 639 } else { 640 assert(false, "unexpected node"); 641 } 642 } 643 if (mem != NULL) { 644 if (mem == start_mem || mem == alloc_mem) { 645 // hit a sentinel, return appropriate 0 value 646 return _igvn.zerocon(ft); 647 } else if (mem->is_Store()) { 648 return mem->in(MemNode::ValueIn); 649 } else if (mem->is_Phi()) { 650 // attempt to produce a Phi reflecting the values on the input paths of the Phi 651 Node_Stack value_phis(a, 8); 652 Node * phi = value_from_mem_phi(mem, ft, ftype, adr_t, alloc, &value_phis, ValueSearchLimit); 653 if (phi != NULL) { 654 return phi; 655 } else { 656 // Kill all new Phis 657 while(value_phis.is_nonempty()) { 658 Node* n = value_phis.node(); 659 _igvn.replace_node(n, C->top()); 660 value_phis.pop(); 661 } 662 } 663 } else if (mem->is_ArrayCopy()) { 664 Node* ctl = mem->in(0); 665 Node* m = mem->in(TypeFunc::Memory); 666 if (sfpt_ctl->is_Proj() && sfpt_ctl->as_Proj()->is_uncommon_trap_proj(Deoptimization::Reason_none)) { 667 // pin the loads in the uncommon trap path 668 ctl = sfpt_ctl; 669 m = sfpt_mem; 670 } 671 return make_arraycopy_load(mem->as_ArrayCopy(), offset, ctl, m, ft, ftype, alloc); 672 } 673 } 674 // Something went wrong. 675 return NULL; 676 } 677 678 // Search the last value stored into the value type's fields. 679 Node* PhaseMacroExpand::value_type_from_mem(Node* mem, Node* ctl, ciValueKlass* vk, const TypeAryPtr* adr_type, int offset, AllocateNode* alloc) { 680 // Subtract the offset of the first field to account for the missing oop header 681 offset -= vk->first_field_offset(); 682 // Create a new ValueTypeNode and retrieve the field values from memory 683 ValueTypeNode* vt = ValueTypeNode::make_uninitialized(_igvn, vk)->as_ValueType(); 684 for (int i = 0; i < vk->nof_declared_nonstatic_fields(); ++i) { 685 ciType* field_type = vt->field_type(i); 686 int field_offset = offset + vt->field_offset(i); 687 // Each value type field has its own memory slice 688 adr_type = adr_type->with_field_offset(field_offset); 689 Node* value = NULL; 690 if (vt->field_is_flattened(i)) { 691 value = value_type_from_mem(mem, ctl, field_type->as_value_klass(), adr_type, field_offset, alloc); 692 } else { 693 const Type* ft = Type::get_const_type(field_type); 694 BasicType bt = field_type->basic_type(); 695 if (UseCompressedOops && !is_java_primitive(bt)) { 696 ft = ft->make_narrowoop(); 697 bt = T_NARROWOOP; 698 } 699 value = value_from_mem(mem, ctl, bt, ft, adr_type, alloc); 700 if (value != NULL && ft->isa_narrowoop()) { 701 assert(UseCompressedOops, "unexpected narrow oop"); 702 value = transform_later(new DecodeNNode(value, value->get_ptr_type())); 703 } 704 } 705 if (value != NULL) { 706 vt->set_field_value(i, value); 707 } else { 708 // We might have reached the TrackedInitializationLimit 709 return NULL; 710 } 711 } 712 return vt; 713 } 714 715 // Check the possibility of scalar replacement. 716 bool PhaseMacroExpand::can_eliminate_allocation(AllocateNode *alloc, GrowableArray <SafePointNode *>& safepoints) { 717 // Scan the uses of the allocation to check for anything that would 718 // prevent us from eliminating it. 719 NOT_PRODUCT( const char* fail_eliminate = NULL; ) 720 DEBUG_ONLY( Node* disq_node = NULL; ) 721 bool can_eliminate = true; 722 723 Node* res = alloc->result_cast(); 724 const TypeOopPtr* res_type = NULL; 725 if (res == NULL) { 726 // All users were eliminated. 727 } else if (!res->is_CheckCastPP()) { 728 NOT_PRODUCT(fail_eliminate = "Allocation does not have unique CheckCastPP";) 729 can_eliminate = false; 730 } else { 731 res_type = _igvn.type(res)->isa_oopptr(); 732 if (res_type == NULL) { 733 NOT_PRODUCT(fail_eliminate = "Neither instance or array allocation";) 734 can_eliminate = false; 735 } else if (res_type->isa_aryptr()) { 736 int length = alloc->in(AllocateNode::ALength)->find_int_con(-1); 737 if (length < 0) { 738 NOT_PRODUCT(fail_eliminate = "Array's size is not constant";) 739 can_eliminate = false; 740 } 741 } 742 } 743 744 if (can_eliminate && res != NULL) { 745 for (DUIterator_Fast jmax, j = res->fast_outs(jmax); 746 j < jmax && can_eliminate; j++) { 747 Node* use = res->fast_out(j); 748 749 if (use->is_AddP()) { 750 const TypePtr* addp_type = _igvn.type(use)->is_ptr(); 751 int offset = addp_type->offset(); 752 753 if (offset == Type::OffsetTop || offset == Type::OffsetBot) { 754 NOT_PRODUCT(fail_eliminate = "Undefined field referrence";) 755 can_eliminate = false; 756 break; 757 } 758 for (DUIterator_Fast kmax, k = use->fast_outs(kmax); 759 k < kmax && can_eliminate; k++) { 760 Node* n = use->fast_out(k); 761 if (!n->is_Store() && n->Opcode() != Op_CastP2X && 762 !(n->is_ArrayCopy() && 763 n->as_ArrayCopy()->is_clonebasic() && 764 n->in(ArrayCopyNode::Dest) == use)) { 765 DEBUG_ONLY(disq_node = n;) 766 if (n->is_Load() || n->is_LoadStore()) { 767 NOT_PRODUCT(fail_eliminate = "Field load";) 768 } else { 769 NOT_PRODUCT(fail_eliminate = "Not store field reference";) 770 } 771 can_eliminate = false; 772 } 773 } 774 } else if (use->is_ArrayCopy() && 775 (use->as_ArrayCopy()->is_arraycopy_validated() || 776 use->as_ArrayCopy()->is_copyof_validated() || 777 use->as_ArrayCopy()->is_copyofrange_validated()) && 778 use->in(ArrayCopyNode::Dest) == res) { 779 // ok to eliminate 780 } else if (use->is_SafePoint()) { 781 SafePointNode* sfpt = use->as_SafePoint(); 782 if (sfpt->is_Call() && sfpt->as_Call()->has_non_debug_use(res)) { 783 // Object is passed as argument. 784 DEBUG_ONLY(disq_node = use;) 785 NOT_PRODUCT(fail_eliminate = "Object is passed as argument";) 786 can_eliminate = false; 787 } 788 Node* sfptMem = sfpt->memory(); 789 if (sfptMem == NULL || sfptMem->is_top()) { 790 DEBUG_ONLY(disq_node = use;) 791 NOT_PRODUCT(fail_eliminate = "NULL or TOP memory";) 792 can_eliminate = false; 793 } else { 794 safepoints.append_if_missing(sfpt); 795 } 796 } else if (use->Opcode() != Op_CastP2X) { // CastP2X is used by card mark 797 if (use->is_Phi()) { 798 if (use->outcnt() == 1 && use->unique_out()->Opcode() == Op_Return) { 799 NOT_PRODUCT(fail_eliminate = "Object is return value";) 800 } else { 801 NOT_PRODUCT(fail_eliminate = "Object is referenced by Phi";) 802 } 803 DEBUG_ONLY(disq_node = use;) 804 } else { 805 if (use->Opcode() == Op_Return) { 806 NOT_PRODUCT(fail_eliminate = "Object is return value";) 807 } else { 808 NOT_PRODUCT(fail_eliminate = "Object is referenced by node";) 809 } 810 DEBUG_ONLY(disq_node = use;) 811 } 812 can_eliminate = false; 813 } 814 } 815 } 816 817 #ifndef PRODUCT 818 if (PrintEliminateAllocations) { 819 if (can_eliminate) { 820 tty->print("Scalar "); 821 if (res == NULL) 822 alloc->dump(); 823 else 824 res->dump(); 825 } else if (alloc->_is_scalar_replaceable) { 826 tty->print("NotScalar (%s)", fail_eliminate); 827 if (res == NULL) 828 alloc->dump(); 829 else 830 res->dump(); 831 #ifdef ASSERT 832 if (disq_node != NULL) { 833 tty->print(" >>>> "); 834 disq_node->dump(); 835 } 836 #endif /*ASSERT*/ 837 } 838 } 839 #endif 840 return can_eliminate; 841 } 842 843 // Do scalar replacement. 844 bool PhaseMacroExpand::scalar_replacement(AllocateNode *alloc, GrowableArray <SafePointNode *>& safepoints) { 845 GrowableArray <SafePointNode *> safepoints_done; 846 847 ciKlass* klass = NULL; 848 ciInstanceKlass* iklass = NULL; 849 int nfields = 0; 850 int array_base = 0; 851 int element_size = 0; 852 BasicType basic_elem_type = T_ILLEGAL; 853 ciType* elem_type = NULL; 854 855 Node* res = alloc->result_cast(); 856 assert(res == NULL || res->is_CheckCastPP(), "unexpected AllocateNode result"); 857 const TypeOopPtr* res_type = NULL; 858 if (res != NULL) { // Could be NULL when there are no users 859 res_type = _igvn.type(res)->isa_oopptr(); 860 } 861 862 if (res != NULL) { 863 klass = res_type->klass(); 864 // Value types are only allocated on demand 865 if (res_type->isa_instptr() || res_type->isa_valuetypeptr()) { 866 // find the fields of the class which will be needed for safepoint debug information 867 assert(klass->is_instance_klass(), "must be an instance klass."); 868 iklass = klass->as_instance_klass(); 869 nfields = iklass->nof_nonstatic_fields(); 870 } else { 871 // find the array's elements which will be needed for safepoint debug information 872 nfields = alloc->in(AllocateNode::ALength)->find_int_con(-1); 873 assert(klass->is_array_klass() && nfields >= 0, "must be an array klass."); 874 elem_type = klass->as_array_klass()->element_type(); 875 basic_elem_type = elem_type->basic_type(); 876 array_base = arrayOopDesc::base_offset_in_bytes(basic_elem_type); 877 element_size = type2aelembytes(basic_elem_type); 878 if (klass->is_value_array_klass()) { 879 // Flattened value type array 880 element_size = klass->as_value_array_klass()->element_byte_size(); 881 } 882 } 883 } 884 // 885 // Process the safepoint uses 886 // 887 Unique_Node_List value_worklist; 888 while (safepoints.length() > 0) { 889 SafePointNode* sfpt = safepoints.pop(); 890 Node* mem = sfpt->memory(); 891 Node* ctl = sfpt->control(); 892 assert(sfpt->jvms() != NULL, "missed JVMS"); 893 // Fields of scalar objs are referenced only at the end 894 // of regular debuginfo at the last (youngest) JVMS. 895 // Record relative start index. 896 uint first_ind = (sfpt->req() - sfpt->jvms()->scloff()); 897 SafePointScalarObjectNode* sobj = new SafePointScalarObjectNode(res_type, 898 #ifdef ASSERT 899 alloc, 900 #endif 901 first_ind, nfields); 902 sobj->init_req(0, C->root()); 903 transform_later(sobj); 904 905 // Scan object's fields adding an input to the safepoint for each field. 906 for (int j = 0; j < nfields; j++) { 907 intptr_t offset; 908 ciField* field = NULL; 909 if (iklass != NULL) { 910 field = iklass->nonstatic_field_at(j); 911 offset = field->offset(); 912 elem_type = field->type(); 913 basic_elem_type = field->layout_type(); 914 assert(!field->is_flattened(), "flattened value type fields should not have safepoint uses"); 915 } else { 916 offset = array_base + j * (intptr_t)element_size; 917 } 918 919 const Type *field_type; 920 // The next code is taken from Parse::do_get_xxx(). 921 if (basic_elem_type == T_OBJECT || basic_elem_type == T_ARRAY) { 922 if (!elem_type->is_loaded()) { 923 field_type = TypeInstPtr::BOTTOM; 924 } else if (field != NULL && field->is_static_constant()) { 925 // This can happen if the constant oop is non-perm. 926 ciObject* con = field->constant_value().as_object(); 927 // Do not "join" in the previous type; it doesn't add value, 928 // and may yield a vacuous result if the field is of interface type. 929 field_type = TypeOopPtr::make_from_constant(con)->isa_oopptr(); 930 assert(field_type != NULL, "field singleton type must be consistent"); 931 } else { 932 field_type = TypeOopPtr::make_from_klass(elem_type->as_klass()); 933 } 934 if (UseCompressedOops) { 935 field_type = field_type->make_narrowoop(); 936 basic_elem_type = T_NARROWOOP; 937 } 938 } else { 939 field_type = Type::get_const_basic_type(basic_elem_type); 940 } 941 942 Node* field_val = NULL; 943 const TypeOopPtr* field_addr_type = res_type->add_offset(offset)->isa_oopptr(); 944 if (klass->is_value_array_klass()) { 945 ciValueKlass* vk = elem_type->as_value_klass(); 946 assert(vk->flatten_array(), "must be flattened"); 947 field_val = value_type_from_mem(mem, ctl, vk, field_addr_type->isa_aryptr(), 0, alloc); 948 } else { 949 field_val = value_from_mem(mem, ctl, basic_elem_type, field_type, field_addr_type, alloc); 950 } 951 if (field_val == NULL) { 952 // We weren't able to find a value for this field, 953 // give up on eliminating this allocation. 954 955 // Remove any extra entries we added to the safepoint. 956 uint last = sfpt->req() - 1; 957 for (int k = 0; k < j; k++) { 958 sfpt->del_req(last--); 959 } 960 _igvn._worklist.push(sfpt); 961 // rollback processed safepoints 962 while (safepoints_done.length() > 0) { 963 SafePointNode* sfpt_done = safepoints_done.pop(); 964 // remove any extra entries we added to the safepoint 965 last = sfpt_done->req() - 1; 966 for (int k = 0; k < nfields; k++) { 967 sfpt_done->del_req(last--); 968 } 969 JVMState *jvms = sfpt_done->jvms(); 970 jvms->set_endoff(sfpt_done->req()); 971 // Now make a pass over the debug information replacing any references 972 // to SafePointScalarObjectNode with the allocated object. 973 int start = jvms->debug_start(); 974 int end = jvms->debug_end(); 975 for (int i = start; i < end; i++) { 976 if (sfpt_done->in(i)->is_SafePointScalarObject()) { 977 SafePointScalarObjectNode* scobj = sfpt_done->in(i)->as_SafePointScalarObject(); 978 if (scobj->first_index(jvms) == sfpt_done->req() && 979 scobj->n_fields() == (uint)nfields) { 980 assert(scobj->alloc() == alloc, "sanity"); 981 sfpt_done->set_req(i, res); 982 } 983 } 984 } 985 _igvn._worklist.push(sfpt_done); 986 } 987 #ifndef PRODUCT 988 if (PrintEliminateAllocations) { 989 if (field != NULL) { 990 tty->print("=== At SafePoint node %d can't find value of Field: ", 991 sfpt->_idx); 992 field->print(); 993 int field_idx = C->get_alias_index(field_addr_type); 994 tty->print(" (alias_idx=%d)", field_idx); 995 } else { // Array's element 996 tty->print("=== At SafePoint node %d can't find value of array element [%d]", 997 sfpt->_idx, j); 998 } 999 tty->print(", which prevents elimination of: "); 1000 if (res == NULL) 1001 alloc->dump(); 1002 else 1003 res->dump(); 1004 } 1005 #endif 1006 return false; 1007 } 1008 if (UseCompressedOops && field_type->isa_narrowoop()) { 1009 // Enable "DecodeN(EncodeP(Allocate)) --> Allocate" transformation 1010 // to be able scalar replace the allocation. 1011 if (field_val->is_EncodeP()) { 1012 field_val = field_val->in(1); 1013 } else { 1014 field_val = transform_later(new DecodeNNode(field_val, field_val->get_ptr_type())); 1015 } 1016 } else if (field_val->is_ValueType()) { 1017 // Keep track of value types to scalarize them later 1018 value_worklist.push(field_val); 1019 } 1020 sfpt->add_req(field_val); 1021 } 1022 JVMState *jvms = sfpt->jvms(); 1023 jvms->set_endoff(sfpt->req()); 1024 // Now make a pass over the debug information replacing any references 1025 // to the allocated object with "sobj" 1026 int start = jvms->debug_start(); 1027 int end = jvms->debug_end(); 1028 sfpt->replace_edges_in_range(res, sobj, start, end); 1029 _igvn._worklist.push(sfpt); 1030 safepoints_done.append_if_missing(sfpt); // keep it for rollback 1031 } 1032 // Scalarize value types that were added to the safepoint 1033 for (uint i = 0; i < value_worklist.size(); ++i) { 1034 Node* vt = value_worklist.at(i); 1035 vt->as_ValueType()->make_scalar_in_safepoints(C->root(), &_igvn); 1036 } 1037 return true; 1038 } 1039 1040 static void disconnect_projections(MultiNode* n, PhaseIterGVN& igvn) { 1041 Node* ctl_proj = n->proj_out_or_null(TypeFunc::Control); 1042 Node* mem_proj = n->proj_out_or_null(TypeFunc::Memory); 1043 if (ctl_proj != NULL) { 1044 igvn.replace_node(ctl_proj, n->in(0)); 1045 } 1046 if (mem_proj != NULL) { 1047 igvn.replace_node(mem_proj, n->in(TypeFunc::Memory)); 1048 } 1049 } 1050 1051 // Process users of eliminated allocation. 1052 void PhaseMacroExpand::process_users_of_allocation(CallNode *alloc) { 1053 Node* res = alloc->result_cast(); 1054 if (res != NULL) { 1055 for (DUIterator_Last jmin, j = res->last_outs(jmin); j >= jmin; ) { 1056 Node *use = res->last_out(j); 1057 uint oc1 = res->outcnt(); 1058 1059 if (use->is_AddP()) { 1060 for (DUIterator_Last kmin, k = use->last_outs(kmin); k >= kmin; ) { 1061 Node *n = use->last_out(k); 1062 uint oc2 = use->outcnt(); 1063 if (n->is_Store()) { 1064 #ifdef ASSERT 1065 // Verify that there is no dependent MemBarVolatile nodes, 1066 // they should be removed during IGVN, see MemBarNode::Ideal(). 1067 for (DUIterator_Fast pmax, p = n->fast_outs(pmax); 1068 p < pmax; p++) { 1069 Node* mb = n->fast_out(p); 1070 assert(mb->is_Initialize() || !mb->is_MemBar() || 1071 mb->req() <= MemBarNode::Precedent || 1072 mb->in(MemBarNode::Precedent) != n, 1073 "MemBarVolatile should be eliminated for non-escaping object"); 1074 } 1075 #endif 1076 _igvn.replace_node(n, n->in(MemNode::Memory)); 1077 } else if (n->is_ArrayCopy()) { 1078 // Disconnect ArrayCopy node 1079 ArrayCopyNode* ac = n->as_ArrayCopy(); 1080 assert(ac->is_clonebasic(), "unexpected array copy kind"); 1081 Node* membar_after = ac->proj_out(TypeFunc::Control)->unique_ctrl_out(); 1082 disconnect_projections(ac, _igvn); 1083 assert(alloc->in(0)->is_Proj() && alloc->in(0)->in(0)->Opcode() == Op_MemBarCPUOrder, "mem barrier expected before allocation"); 1084 Node* membar_before = alloc->in(0)->in(0); 1085 disconnect_projections(membar_before->as_MemBar(), _igvn); 1086 if (membar_after->is_MemBar()) { 1087 disconnect_projections(membar_after->as_MemBar(), _igvn); 1088 } 1089 } else { 1090 eliminate_card_mark(n); 1091 } 1092 k -= (oc2 - use->outcnt()); 1093 } 1094 } else if (use->is_ArrayCopy()) { 1095 // Disconnect ArrayCopy node 1096 ArrayCopyNode* ac = use->as_ArrayCopy(); 1097 assert(ac->is_arraycopy_validated() || 1098 ac->is_copyof_validated() || 1099 ac->is_copyofrange_validated(), "unsupported"); 1100 CallProjections* callprojs = ac->extract_projections(true); 1101 1102 _igvn.replace_node(callprojs->fallthrough_ioproj, ac->in(TypeFunc::I_O)); 1103 _igvn.replace_node(callprojs->fallthrough_memproj, ac->in(TypeFunc::Memory)); 1104 _igvn.replace_node(callprojs->fallthrough_catchproj, ac->in(TypeFunc::Control)); 1105 1106 // Set control to top. IGVN will remove the remaining projections 1107 ac->set_req(0, top()); 1108 ac->replace_edge(res, top()); 1109 1110 // Disconnect src right away: it can help find new 1111 // opportunities for allocation elimination 1112 Node* src = ac->in(ArrayCopyNode::Src); 1113 ac->replace_edge(src, top()); 1114 // src can be top at this point if src and dest of the 1115 // arraycopy were the same 1116 if (src->outcnt() == 0 && !src->is_top()) { 1117 _igvn.remove_dead_node(src); 1118 } 1119 1120 _igvn._worklist.push(ac); 1121 } else { 1122 eliminate_card_mark(use); 1123 } 1124 j -= (oc1 - res->outcnt()); 1125 } 1126 assert(res->outcnt() == 0, "all uses of allocated objects must be deleted"); 1127 _igvn.remove_dead_node(res); 1128 } 1129 1130 // 1131 // Process other users of allocation's projections 1132 // 1133 if (_resproj != NULL && _resproj->outcnt() != 0) { 1134 // First disconnect stores captured by Initialize node. 1135 // If Initialize node is eliminated first in the following code, 1136 // it will kill such stores and DUIterator_Last will assert. 1137 for (DUIterator_Fast jmax, j = _resproj->fast_outs(jmax); j < jmax; j++) { 1138 Node *use = _resproj->fast_out(j); 1139 if (use->is_AddP()) { 1140 // raw memory addresses used only by the initialization 1141 _igvn.replace_node(use, C->top()); 1142 --j; --jmax; 1143 } 1144 } 1145 for (DUIterator_Last jmin, j = _resproj->last_outs(jmin); j >= jmin; ) { 1146 Node *use = _resproj->last_out(j); 1147 uint oc1 = _resproj->outcnt(); 1148 if (use->is_Initialize()) { 1149 // Eliminate Initialize node. 1150 InitializeNode *init = use->as_Initialize(); 1151 assert(init->outcnt() <= 2, "only a control and memory projection expected"); 1152 Node *ctrl_proj = init->proj_out_or_null(TypeFunc::Control); 1153 if (ctrl_proj != NULL) { 1154 assert(init->in(TypeFunc::Control) == _fallthroughcatchproj, "allocation control projection"); 1155 _igvn.replace_node(ctrl_proj, _fallthroughcatchproj); 1156 } 1157 Node *mem_proj = init->proj_out_or_null(TypeFunc::Memory); 1158 if (mem_proj != NULL) { 1159 Node *mem = init->in(TypeFunc::Memory); 1160 #ifdef ASSERT 1161 if (mem->is_MergeMem()) { 1162 assert(mem->in(TypeFunc::Memory) == _memproj_fallthrough, "allocation memory projection"); 1163 } else { 1164 assert(mem == _memproj_fallthrough, "allocation memory projection"); 1165 } 1166 #endif 1167 _igvn.replace_node(mem_proj, mem); 1168 } 1169 } else { 1170 assert(false, "only Initialize or AddP expected"); 1171 } 1172 j -= (oc1 - _resproj->outcnt()); 1173 } 1174 } 1175 if (_fallthroughcatchproj != NULL) { 1176 _igvn.replace_node(_fallthroughcatchproj, alloc->in(TypeFunc::Control)); 1177 } 1178 if (_memproj_fallthrough != NULL) { 1179 _igvn.replace_node(_memproj_fallthrough, alloc->in(TypeFunc::Memory)); 1180 } 1181 if (_memproj_catchall != NULL) { 1182 _igvn.replace_node(_memproj_catchall, C->top()); 1183 } 1184 if (_ioproj_fallthrough != NULL) { 1185 _igvn.replace_node(_ioproj_fallthrough, alloc->in(TypeFunc::I_O)); 1186 } 1187 if (_ioproj_catchall != NULL) { 1188 _igvn.replace_node(_ioproj_catchall, C->top()); 1189 } 1190 if (_catchallcatchproj != NULL) { 1191 _igvn.replace_node(_catchallcatchproj, C->top()); 1192 } 1193 } 1194 1195 bool PhaseMacroExpand::eliminate_allocate_node(AllocateNode *alloc) { 1196 // Don't do scalar replacement if the frame can be popped by JVMTI: 1197 // if reallocation fails during deoptimization we'll pop all 1198 // interpreter frames for this compiled frame and that won't play 1199 // nice with JVMTI popframe. 1200 if (!EliminateAllocations || JvmtiExport::can_pop_frame() || !alloc->_is_non_escaping) { 1201 return false; 1202 } 1203 Node* klass = alloc->in(AllocateNode::KlassNode); 1204 const TypeKlassPtr* tklass = _igvn.type(klass)->is_klassptr(); 1205 Node* res = alloc->result_cast(); 1206 // Eliminate boxing allocations which are not used 1207 // regardless scalar replacable status. 1208 bool boxing_alloc = C->eliminate_boxing() && 1209 tklass->klass()->is_instance_klass() && 1210 tklass->klass()->as_instance_klass()->is_box_klass(); 1211 if (!alloc->_is_scalar_replaceable && (!boxing_alloc || (res != NULL))) { 1212 return false; 1213 } 1214 1215 extract_call_projections(alloc); 1216 1217 GrowableArray <SafePointNode *> safepoints; 1218 if (!can_eliminate_allocation(alloc, safepoints)) { 1219 return false; 1220 } 1221 1222 if (!alloc->_is_scalar_replaceable) { 1223 assert(res == NULL, "sanity"); 1224 // We can only eliminate allocation if all debug info references 1225 // are already replaced with SafePointScalarObject because 1226 // we can't search for a fields value without instance_id. 1227 if (safepoints.length() > 0) { 1228 return false; 1229 } 1230 } 1231 1232 if (!scalar_replacement(alloc, safepoints)) { 1233 return false; 1234 } 1235 1236 CompileLog* log = C->log(); 1237 if (log != NULL) { 1238 log->head("eliminate_allocation type='%d'", 1239 log->identify(tklass->klass())); 1240 JVMState* p = alloc->jvms(); 1241 while (p != NULL) { 1242 log->elem("jvms bci='%d' method='%d'", p->bci(), log->identify(p->method())); 1243 p = p->caller(); 1244 } 1245 log->tail("eliminate_allocation"); 1246 } 1247 1248 process_users_of_allocation(alloc); 1249 1250 #ifndef PRODUCT 1251 if (PrintEliminateAllocations) { 1252 if (alloc->is_AllocateArray()) 1253 tty->print_cr("++++ Eliminated: %d AllocateArray", alloc->_idx); 1254 else 1255 tty->print_cr("++++ Eliminated: %d Allocate", alloc->_idx); 1256 } 1257 #endif 1258 1259 return true; 1260 } 1261 1262 bool PhaseMacroExpand::eliminate_boxing_node(CallStaticJavaNode *boxing) { 1263 // EA should remove all uses of non-escaping boxing node. 1264 if (!C->eliminate_boxing() || boxing->proj_out_or_null(TypeFunc::Parms) != NULL) { 1265 return false; 1266 } 1267 1268 assert(boxing->result_cast() == NULL, "unexpected boxing node result"); 1269 1270 extract_call_projections(boxing); 1271 1272 const TypeTuple* r = boxing->tf()->range_sig(); 1273 assert(r->cnt() > TypeFunc::Parms, "sanity"); 1274 const TypeInstPtr* t = r->field_at(TypeFunc::Parms)->isa_instptr(); 1275 assert(t != NULL, "sanity"); 1276 1277 CompileLog* log = C->log(); 1278 if (log != NULL) { 1279 log->head("eliminate_boxing type='%d'", 1280 log->identify(t->klass())); 1281 JVMState* p = boxing->jvms(); 1282 while (p != NULL) { 1283 log->elem("jvms bci='%d' method='%d'", p->bci(), log->identify(p->method())); 1284 p = p->caller(); 1285 } 1286 log->tail("eliminate_boxing"); 1287 } 1288 1289 process_users_of_allocation(boxing); 1290 1291 #ifndef PRODUCT 1292 if (PrintEliminateAllocations) { 1293 tty->print("++++ Eliminated: %d ", boxing->_idx); 1294 boxing->method()->print_short_name(tty); 1295 tty->cr(); 1296 } 1297 #endif 1298 1299 return true; 1300 } 1301 1302 //---------------------------set_eden_pointers------------------------- 1303 void PhaseMacroExpand::set_eden_pointers(Node* &eden_top_adr, Node* &eden_end_adr) { 1304 if (UseTLAB) { // Private allocation: load from TLS 1305 Node* thread = transform_later(new ThreadLocalNode()); 1306 int tlab_top_offset = in_bytes(JavaThread::tlab_top_offset()); 1307 int tlab_end_offset = in_bytes(JavaThread::tlab_end_offset()); 1308 eden_top_adr = basic_plus_adr(top()/*not oop*/, thread, tlab_top_offset); 1309 eden_end_adr = basic_plus_adr(top()/*not oop*/, thread, tlab_end_offset); 1310 } else { // Shared allocation: load from globals 1311 CollectedHeap* ch = Universe::heap(); 1312 address top_adr = (address)ch->top_addr(); 1313 address end_adr = (address)ch->end_addr(); 1314 eden_top_adr = makecon(TypeRawPtr::make(top_adr)); 1315 eden_end_adr = basic_plus_adr(eden_top_adr, end_adr - top_adr); 1316 } 1317 } 1318 1319 1320 Node* PhaseMacroExpand::make_load(Node* ctl, Node* mem, Node* base, int offset, const Type* value_type, BasicType bt) { 1321 Node* adr = basic_plus_adr(base, offset); 1322 const TypePtr* adr_type = adr->bottom_type()->is_ptr(); 1323 Node* value = LoadNode::make(_igvn, ctl, mem, adr, adr_type, value_type, bt, MemNode::unordered); 1324 transform_later(value); 1325 return value; 1326 } 1327 1328 1329 Node* PhaseMacroExpand::make_store(Node* ctl, Node* mem, Node* base, int offset, Node* value, BasicType bt) { 1330 Node* adr = basic_plus_adr(base, offset); 1331 mem = StoreNode::make(_igvn, ctl, mem, adr, NULL, value, bt, MemNode::unordered); 1332 transform_later(mem); 1333 return mem; 1334 } 1335 1336 //============================================================================= 1337 // 1338 // A L L O C A T I O N 1339 // 1340 // Allocation attempts to be fast in the case of frequent small objects. 1341 // It breaks down like this: 1342 // 1343 // 1) Size in doublewords is computed. This is a constant for objects and 1344 // variable for most arrays. Doubleword units are used to avoid size 1345 // overflow of huge doubleword arrays. We need doublewords in the end for 1346 // rounding. 1347 // 1348 // 2) Size is checked for being 'too large'. Too-large allocations will go 1349 // the slow path into the VM. The slow path can throw any required 1350 // exceptions, and does all the special checks for very large arrays. The 1351 // size test can constant-fold away for objects. For objects with 1352 // finalizers it constant-folds the otherway: you always go slow with 1353 // finalizers. 1354 // 1355 // 3) If NOT using TLABs, this is the contended loop-back point. 1356 // Load-Locked the heap top. If using TLABs normal-load the heap top. 1357 // 1358 // 4) Check that heap top + size*8 < max. If we fail go the slow ` route. 1359 // NOTE: "top+size*8" cannot wrap the 4Gig line! Here's why: for largish 1360 // "size*8" we always enter the VM, where "largish" is a constant picked small 1361 // enough that there's always space between the eden max and 4Gig (old space is 1362 // there so it's quite large) and large enough that the cost of entering the VM 1363 // is dwarfed by the cost to initialize the space. 1364 // 1365 // 5) If NOT using TLABs, Store-Conditional the adjusted heap top back 1366 // down. If contended, repeat at step 3. If using TLABs normal-store 1367 // adjusted heap top back down; there is no contention. 1368 // 1369 // 6) If !ZeroTLAB then Bulk-clear the object/array. Fill in klass & mark 1370 // fields. 1371 // 1372 // 7) Merge with the slow-path; cast the raw memory pointer to the correct 1373 // oop flavor. 1374 // 1375 //============================================================================= 1376 // FastAllocateSizeLimit value is in DOUBLEWORDS. 1377 // Allocations bigger than this always go the slow route. 1378 // This value must be small enough that allocation attempts that need to 1379 // trigger exceptions go the slow route. Also, it must be small enough so 1380 // that heap_top + size_in_bytes does not wrap around the 4Gig limit. 1381 //=============================================================================j// 1382 // %%% Here is an old comment from parseHelper.cpp; is it outdated? 1383 // The allocator will coalesce int->oop copies away. See comment in 1384 // coalesce.cpp about how this works. It depends critically on the exact 1385 // code shape produced here, so if you are changing this code shape 1386 // make sure the GC info for the heap-top is correct in and around the 1387 // slow-path call. 1388 // 1389 1390 void PhaseMacroExpand::expand_allocate_common( 1391 AllocateNode* alloc, // allocation node to be expanded 1392 Node* length, // array length for an array allocation 1393 const TypeFunc* slow_call_type, // Type of slow call 1394 address slow_call_address // Address of slow call 1395 ) 1396 { 1397 1398 Node* ctrl = alloc->in(TypeFunc::Control); 1399 Node* mem = alloc->in(TypeFunc::Memory); 1400 Node* i_o = alloc->in(TypeFunc::I_O); 1401 Node* size_in_bytes = alloc->in(AllocateNode::AllocSize); 1402 Node* klass_node = alloc->in(AllocateNode::KlassNode); 1403 Node* initial_slow_test = alloc->in(AllocateNode::InitialTest); 1404 1405 assert(ctrl != NULL, "must have control"); 1406 // We need a Region and corresponding Phi's to merge the slow-path and fast-path results. 1407 // they will not be used if "always_slow" is set 1408 enum { slow_result_path = 1, fast_result_path = 2 }; 1409 Node *result_region = NULL; 1410 Node *result_phi_rawmem = NULL; 1411 Node *result_phi_rawoop = NULL; 1412 Node *result_phi_i_o = NULL; 1413 1414 // The initial slow comparison is a size check, the comparison 1415 // we want to do is a BoolTest::gt 1416 bool always_slow = false; 1417 int tv = _igvn.find_int_con(initial_slow_test, -1); 1418 if (tv >= 0) { 1419 always_slow = (tv == 1); 1420 initial_slow_test = NULL; 1421 } else { 1422 initial_slow_test = BoolNode::make_predicate(initial_slow_test, &_igvn); 1423 } 1424 1425 if (C->env()->dtrace_alloc_probes() || 1426 (!UseTLAB && !Universe::heap()->supports_inline_contig_alloc())) { 1427 // Force slow-path allocation 1428 always_slow = true; 1429 initial_slow_test = NULL; 1430 } 1431 1432 1433 enum { too_big_or_final_path = 1, need_gc_path = 2 }; 1434 Node *slow_region = NULL; 1435 Node *toobig_false = ctrl; 1436 1437 assert (initial_slow_test == NULL || !always_slow, "arguments must be consistent"); 1438 // generate the initial test if necessary 1439 if (initial_slow_test != NULL ) { 1440 slow_region = new RegionNode(3); 1441 1442 // Now make the initial failure test. Usually a too-big test but 1443 // might be a TRUE for finalizers or a fancy class check for 1444 // newInstance0. 1445 IfNode *toobig_iff = new IfNode(ctrl, initial_slow_test, PROB_MIN, COUNT_UNKNOWN); 1446 transform_later(toobig_iff); 1447 // Plug the failing-too-big test into the slow-path region 1448 Node *toobig_true = new IfTrueNode( toobig_iff ); 1449 transform_later(toobig_true); 1450 slow_region ->init_req( too_big_or_final_path, toobig_true ); 1451 toobig_false = new IfFalseNode( toobig_iff ); 1452 transform_later(toobig_false); 1453 } else { // No initial test, just fall into next case 1454 toobig_false = ctrl; 1455 debug_only(slow_region = NodeSentinel); 1456 } 1457 1458 Node *slow_mem = mem; // save the current memory state for slow path 1459 // generate the fast allocation code unless we know that the initial test will always go slow 1460 if (!always_slow) { 1461 // Fast path modifies only raw memory. 1462 if (mem->is_MergeMem()) { 1463 mem = mem->as_MergeMem()->memory_at(Compile::AliasIdxRaw); 1464 } 1465 1466 Node* eden_top_adr; 1467 Node* eden_end_adr; 1468 1469 set_eden_pointers(eden_top_adr, eden_end_adr); 1470 1471 // Load Eden::end. Loop invariant and hoisted. 1472 // 1473 // Note: We set the control input on "eden_end" and "old_eden_top" when using 1474 // a TLAB to work around a bug where these values were being moved across 1475 // a safepoint. These are not oops, so they cannot be include in the oop 1476 // map, but they can be changed by a GC. The proper way to fix this would 1477 // be to set the raw memory state when generating a SafepointNode. However 1478 // this will require extensive changes to the loop optimization in order to 1479 // prevent a degradation of the optimization. 1480 // See comment in memnode.hpp, around line 227 in class LoadPNode. 1481 Node *eden_end = make_load(ctrl, mem, eden_end_adr, 0, TypeRawPtr::BOTTOM, T_ADDRESS); 1482 1483 // allocate the Region and Phi nodes for the result 1484 result_region = new RegionNode(3); 1485 result_phi_rawmem = new PhiNode(result_region, Type::MEMORY, TypeRawPtr::BOTTOM); 1486 result_phi_rawoop = new PhiNode(result_region, TypeRawPtr::BOTTOM); 1487 result_phi_i_o = new PhiNode(result_region, Type::ABIO); // I/O is used for Prefetch 1488 1489 // We need a Region for the loop-back contended case. 1490 enum { fall_in_path = 1, contended_loopback_path = 2 }; 1491 Node *contended_region; 1492 Node *contended_phi_rawmem; 1493 if (UseTLAB) { 1494 contended_region = toobig_false; 1495 contended_phi_rawmem = mem; 1496 } else { 1497 contended_region = new RegionNode(3); 1498 contended_phi_rawmem = new PhiNode(contended_region, Type::MEMORY, TypeRawPtr::BOTTOM); 1499 // Now handle the passing-too-big test. We fall into the contended 1500 // loop-back merge point. 1501 contended_region ->init_req(fall_in_path, toobig_false); 1502 contended_phi_rawmem->init_req(fall_in_path, mem); 1503 transform_later(contended_region); 1504 transform_later(contended_phi_rawmem); 1505 } 1506 1507 // Load(-locked) the heap top. 1508 // See note above concerning the control input when using a TLAB 1509 Node *old_eden_top = UseTLAB 1510 ? new LoadPNode (ctrl, contended_phi_rawmem, eden_top_adr, TypeRawPtr::BOTTOM, TypeRawPtr::BOTTOM, MemNode::unordered) 1511 : new LoadPLockedNode(contended_region, contended_phi_rawmem, eden_top_adr, MemNode::acquire); 1512 1513 transform_later(old_eden_top); 1514 // Add to heap top to get a new heap top 1515 Node *new_eden_top = new AddPNode(top(), old_eden_top, size_in_bytes); 1516 transform_later(new_eden_top); 1517 // Check for needing a GC; compare against heap end 1518 Node *needgc_cmp = new CmpPNode(new_eden_top, eden_end); 1519 transform_later(needgc_cmp); 1520 Node *needgc_bol = new BoolNode(needgc_cmp, BoolTest::ge); 1521 transform_later(needgc_bol); 1522 IfNode *needgc_iff = new IfNode(contended_region, needgc_bol, PROB_UNLIKELY_MAG(4), COUNT_UNKNOWN); 1523 transform_later(needgc_iff); 1524 1525 // Plug the failing-heap-space-need-gc test into the slow-path region 1526 Node *needgc_true = new IfTrueNode(needgc_iff); 1527 transform_later(needgc_true); 1528 if (initial_slow_test) { 1529 slow_region->init_req(need_gc_path, needgc_true); 1530 // This completes all paths into the slow merge point 1531 transform_later(slow_region); 1532 } else { // No initial slow path needed! 1533 // Just fall from the need-GC path straight into the VM call. 1534 slow_region = needgc_true; 1535 } 1536 // No need for a GC. Setup for the Store-Conditional 1537 Node *needgc_false = new IfFalseNode(needgc_iff); 1538 transform_later(needgc_false); 1539 1540 // Grab regular I/O before optional prefetch may change it. 1541 // Slow-path does no I/O so just set it to the original I/O. 1542 result_phi_i_o->init_req(slow_result_path, i_o); 1543 1544 i_o = prefetch_allocation(i_o, needgc_false, contended_phi_rawmem, 1545 old_eden_top, new_eden_top, length); 1546 1547 // Name successful fast-path variables 1548 Node* fast_oop = old_eden_top; 1549 Node* fast_oop_ctrl; 1550 Node* fast_oop_rawmem; 1551 1552 // Store (-conditional) the modified eden top back down. 1553 // StorePConditional produces flags for a test PLUS a modified raw 1554 // memory state. 1555 if (UseTLAB) { 1556 Node* store_eden_top = 1557 new StorePNode(needgc_false, contended_phi_rawmem, eden_top_adr, 1558 TypeRawPtr::BOTTOM, new_eden_top, MemNode::unordered); 1559 transform_later(store_eden_top); 1560 fast_oop_ctrl = needgc_false; // No contention, so this is the fast path 1561 fast_oop_rawmem = store_eden_top; 1562 } else { 1563 Node* store_eden_top = 1564 new StorePConditionalNode(needgc_false, contended_phi_rawmem, eden_top_adr, 1565 new_eden_top, fast_oop/*old_eden_top*/); 1566 transform_later(store_eden_top); 1567 Node *contention_check = new BoolNode(store_eden_top, BoolTest::ne); 1568 transform_later(contention_check); 1569 store_eden_top = new SCMemProjNode(store_eden_top); 1570 transform_later(store_eden_top); 1571 1572 // If not using TLABs, check to see if there was contention. 1573 IfNode *contention_iff = new IfNode (needgc_false, contention_check, PROB_MIN, COUNT_UNKNOWN); 1574 transform_later(contention_iff); 1575 Node *contention_true = new IfTrueNode(contention_iff); 1576 transform_later(contention_true); 1577 // If contention, loopback and try again. 1578 contended_region->init_req(contended_loopback_path, contention_true); 1579 contended_phi_rawmem->init_req(contended_loopback_path, store_eden_top); 1580 1581 // Fast-path succeeded with no contention! 1582 Node *contention_false = new IfFalseNode(contention_iff); 1583 transform_later(contention_false); 1584 fast_oop_ctrl = contention_false; 1585 1586 // Bump total allocated bytes for this thread 1587 Node* thread = new ThreadLocalNode(); 1588 transform_later(thread); 1589 Node* alloc_bytes_adr = basic_plus_adr(top()/*not oop*/, thread, 1590 in_bytes(JavaThread::allocated_bytes_offset())); 1591 Node* alloc_bytes = make_load(fast_oop_ctrl, store_eden_top, alloc_bytes_adr, 1592 0, TypeLong::LONG, T_LONG); 1593 #ifdef _LP64 1594 Node* alloc_size = size_in_bytes; 1595 #else 1596 Node* alloc_size = new ConvI2LNode(size_in_bytes); 1597 transform_later(alloc_size); 1598 #endif 1599 Node* new_alloc_bytes = new AddLNode(alloc_bytes, alloc_size); 1600 transform_later(new_alloc_bytes); 1601 fast_oop_rawmem = make_store(fast_oop_ctrl, store_eden_top, alloc_bytes_adr, 1602 0, new_alloc_bytes, T_LONG); 1603 } 1604 1605 InitializeNode* init = alloc->initialization(); 1606 fast_oop_rawmem = initialize_object(alloc, 1607 fast_oop_ctrl, fast_oop_rawmem, fast_oop, 1608 klass_node, length, size_in_bytes); 1609 1610 // If initialization is performed by an array copy, any required 1611 // MemBarStoreStore was already added. If the object does not 1612 // escape no need for a MemBarStoreStore. If the object does not 1613 // escape in its initializer and memory barrier (MemBarStoreStore or 1614 // stronger) is already added at exit of initializer, also no need 1615 // for a MemBarStoreStore. Otherwise we need a MemBarStoreStore 1616 // so that stores that initialize this object can't be reordered 1617 // with a subsequent store that makes this object accessible by 1618 // other threads. 1619 // Other threads include java threads and JVM internal threads 1620 // (for example concurrent GC threads). Current concurrent GC 1621 // implementation: CMS and G1 will not scan newly created object, 1622 // so it's safe to skip storestore barrier when allocation does 1623 // not escape. 1624 if (!alloc->does_not_escape_thread() && 1625 !alloc->is_allocation_MemBar_redundant() && 1626 (init == NULL || !init->is_complete_with_arraycopy())) { 1627 if (init == NULL || init->req() < InitializeNode::RawStores) { 1628 // No InitializeNode or no stores captured by zeroing 1629 // elimination. Simply add the MemBarStoreStore after object 1630 // initialization. 1631 MemBarNode* mb = MemBarNode::make(C, Op_MemBarStoreStore, Compile::AliasIdxBot); 1632 transform_later(mb); 1633 1634 mb->init_req(TypeFunc::Memory, fast_oop_rawmem); 1635 mb->init_req(TypeFunc::Control, fast_oop_ctrl); 1636 fast_oop_ctrl = new ProjNode(mb,TypeFunc::Control); 1637 transform_later(fast_oop_ctrl); 1638 fast_oop_rawmem = new ProjNode(mb,TypeFunc::Memory); 1639 transform_later(fast_oop_rawmem); 1640 } else { 1641 // Add the MemBarStoreStore after the InitializeNode so that 1642 // all stores performing the initialization that were moved 1643 // before the InitializeNode happen before the storestore 1644 // barrier. 1645 1646 Node* init_ctrl = init->proj_out_or_null(TypeFunc::Control); 1647 Node* init_mem = init->proj_out_or_null(TypeFunc::Memory); 1648 1649 MemBarNode* mb = MemBarNode::make(C, Op_MemBarStoreStore, Compile::AliasIdxBot); 1650 transform_later(mb); 1651 1652 Node* ctrl = new ProjNode(init,TypeFunc::Control); 1653 transform_later(ctrl); 1654 Node* mem = new ProjNode(init,TypeFunc::Memory); 1655 transform_later(mem); 1656 1657 // The MemBarStoreStore depends on control and memory coming 1658 // from the InitializeNode 1659 mb->init_req(TypeFunc::Memory, mem); 1660 mb->init_req(TypeFunc::Control, ctrl); 1661 1662 ctrl = new ProjNode(mb,TypeFunc::Control); 1663 transform_later(ctrl); 1664 mem = new ProjNode(mb,TypeFunc::Memory); 1665 transform_later(mem); 1666 1667 // All nodes that depended on the InitializeNode for control 1668 // and memory must now depend on the MemBarNode that itself 1669 // depends on the InitializeNode 1670 if (init_ctrl != NULL) { 1671 _igvn.replace_node(init_ctrl, ctrl); 1672 } 1673 if (init_mem != NULL) { 1674 _igvn.replace_node(init_mem, mem); 1675 } 1676 } 1677 } 1678 1679 if (C->env()->dtrace_extended_probes()) { 1680 // Slow-path call 1681 int size = TypeFunc::Parms + 2; 1682 CallLeafNode *call = new CallLeafNode(OptoRuntime::dtrace_object_alloc_Type(), 1683 CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_object_alloc_base), 1684 "dtrace_object_alloc", 1685 TypeRawPtr::BOTTOM); 1686 1687 // Get base of thread-local storage area 1688 Node* thread = new ThreadLocalNode(); 1689 transform_later(thread); 1690 1691 call->init_req(TypeFunc::Parms+0, thread); 1692 call->init_req(TypeFunc::Parms+1, fast_oop); 1693 call->init_req(TypeFunc::Control, fast_oop_ctrl); 1694 call->init_req(TypeFunc::I_O , top()); // does no i/o 1695 call->init_req(TypeFunc::Memory , fast_oop_rawmem); 1696 call->init_req(TypeFunc::ReturnAdr, alloc->in(TypeFunc::ReturnAdr)); 1697 call->init_req(TypeFunc::FramePtr, alloc->in(TypeFunc::FramePtr)); 1698 transform_later(call); 1699 fast_oop_ctrl = new ProjNode(call,TypeFunc::Control); 1700 transform_later(fast_oop_ctrl); 1701 fast_oop_rawmem = new ProjNode(call,TypeFunc::Memory); 1702 transform_later(fast_oop_rawmem); 1703 } 1704 1705 // Plug in the successful fast-path into the result merge point 1706 result_region ->init_req(fast_result_path, fast_oop_ctrl); 1707 result_phi_rawoop->init_req(fast_result_path, fast_oop); 1708 result_phi_i_o ->init_req(fast_result_path, i_o); 1709 result_phi_rawmem->init_req(fast_result_path, fast_oop_rawmem); 1710 } else { 1711 slow_region = ctrl; 1712 result_phi_i_o = i_o; // Rename it to use in the following code. 1713 } 1714 1715 // Generate slow-path call 1716 CallNode *call = new CallStaticJavaNode(slow_call_type, slow_call_address, 1717 OptoRuntime::stub_name(slow_call_address), 1718 alloc->jvms()->bci(), 1719 TypePtr::BOTTOM); 1720 call->init_req( TypeFunc::Control, slow_region ); 1721 call->init_req( TypeFunc::I_O , top() ) ; // does no i/o 1722 call->init_req( TypeFunc::Memory , slow_mem ); // may gc ptrs 1723 call->init_req( TypeFunc::ReturnAdr, alloc->in(TypeFunc::ReturnAdr) ); 1724 call->init_req( TypeFunc::FramePtr, alloc->in(TypeFunc::FramePtr) ); 1725 1726 call->init_req(TypeFunc::Parms+0, klass_node); 1727 if (length != NULL) { 1728 call->init_req(TypeFunc::Parms+1, length); 1729 } 1730 1731 // Copy debug information and adjust JVMState information, then replace 1732 // allocate node with the call 1733 copy_call_debug_info((CallNode *) alloc, call); 1734 if (!always_slow) { 1735 call->set_cnt(PROB_UNLIKELY_MAG(4)); // Same effect as RC_UNCOMMON. 1736 } else { 1737 // Hook i_o projection to avoid its elimination during allocation 1738 // replacement (when only a slow call is generated). 1739 call->set_req(TypeFunc::I_O, result_phi_i_o); 1740 } 1741 _igvn.replace_node(alloc, call); 1742 transform_later(call); 1743 1744 // Identify the output projections from the allocate node and 1745 // adjust any references to them. 1746 // The control and io projections look like: 1747 // 1748 // v---Proj(ctrl) <-----+ v---CatchProj(ctrl) 1749 // Allocate Catch 1750 // ^---Proj(io) <-------+ ^---CatchProj(io) 1751 // 1752 // We are interested in the CatchProj nodes. 1753 // 1754 extract_call_projections(call); 1755 1756 // An allocate node has separate memory projections for the uses on 1757 // the control and i_o paths. Replace the control memory projection with 1758 // result_phi_rawmem (unless we are only generating a slow call when 1759 // both memory projections are combined) 1760 if (!always_slow && _memproj_fallthrough != NULL) { 1761 for (DUIterator_Fast imax, i = _memproj_fallthrough->fast_outs(imax); i < imax; i++) { 1762 Node *use = _memproj_fallthrough->fast_out(i); 1763 _igvn.rehash_node_delayed(use); 1764 imax -= replace_input(use, _memproj_fallthrough, result_phi_rawmem); 1765 // back up iterator 1766 --i; 1767 } 1768 } 1769 // Now change uses of _memproj_catchall to use _memproj_fallthrough and delete 1770 // _memproj_catchall so we end up with a call that has only 1 memory projection. 1771 if (_memproj_catchall != NULL ) { 1772 if (_memproj_fallthrough == NULL) { 1773 _memproj_fallthrough = new ProjNode(call, TypeFunc::Memory); 1774 transform_later(_memproj_fallthrough); 1775 } 1776 for (DUIterator_Fast imax, i = _memproj_catchall->fast_outs(imax); i < imax; i++) { 1777 Node *use = _memproj_catchall->fast_out(i); 1778 _igvn.rehash_node_delayed(use); 1779 imax -= replace_input(use, _memproj_catchall, _memproj_fallthrough); 1780 // back up iterator 1781 --i; 1782 } 1783 assert(_memproj_catchall->outcnt() == 0, "all uses must be deleted"); 1784 _igvn.remove_dead_node(_memproj_catchall); 1785 } 1786 1787 // An allocate node has separate i_o projections for the uses on the control 1788 // and i_o paths. Always replace the control i_o projection with result i_o 1789 // otherwise incoming i_o become dead when only a slow call is generated 1790 // (it is different from memory projections where both projections are 1791 // combined in such case). 1792 if (_ioproj_fallthrough != NULL) { 1793 for (DUIterator_Fast imax, i = _ioproj_fallthrough->fast_outs(imax); i < imax; i++) { 1794 Node *use = _ioproj_fallthrough->fast_out(i); 1795 _igvn.rehash_node_delayed(use); 1796 imax -= replace_input(use, _ioproj_fallthrough, result_phi_i_o); 1797 // back up iterator 1798 --i; 1799 } 1800 } 1801 // Now change uses of _ioproj_catchall to use _ioproj_fallthrough and delete 1802 // _ioproj_catchall so we end up with a call that has only 1 i_o projection. 1803 if (_ioproj_catchall != NULL ) { 1804 if (_ioproj_fallthrough == NULL) { 1805 _ioproj_fallthrough = new ProjNode(call, TypeFunc::I_O); 1806 transform_later(_ioproj_fallthrough); 1807 } 1808 for (DUIterator_Fast imax, i = _ioproj_catchall->fast_outs(imax); i < imax; i++) { 1809 Node *use = _ioproj_catchall->fast_out(i); 1810 _igvn.rehash_node_delayed(use); 1811 imax -= replace_input(use, _ioproj_catchall, _ioproj_fallthrough); 1812 // back up iterator 1813 --i; 1814 } 1815 assert(_ioproj_catchall->outcnt() == 0, "all uses must be deleted"); 1816 _igvn.remove_dead_node(_ioproj_catchall); 1817 } 1818 1819 // if we generated only a slow call, we are done 1820 if (always_slow) { 1821 // Now we can unhook i_o. 1822 if (result_phi_i_o->outcnt() > 1) { 1823 call->set_req(TypeFunc::I_O, top()); 1824 } else { 1825 assert(result_phi_i_o->unique_ctrl_out() == call, ""); 1826 // Case of new array with negative size known during compilation. 1827 // AllocateArrayNode::Ideal() optimization disconnect unreachable 1828 // following code since call to runtime will throw exception. 1829 // As result there will be no users of i_o after the call. 1830 // Leave i_o attached to this call to avoid problems in preceding graph. 1831 } 1832 return; 1833 } 1834 1835 1836 if (_fallthroughcatchproj != NULL) { 1837 ctrl = _fallthroughcatchproj->clone(); 1838 transform_later(ctrl); 1839 _igvn.replace_node(_fallthroughcatchproj, result_region); 1840 } else { 1841 ctrl = top(); 1842 } 1843 Node *slow_result; 1844 if (_resproj == NULL) { 1845 // no uses of the allocation result 1846 slow_result = top(); 1847 } else { 1848 slow_result = _resproj->clone(); 1849 transform_later(slow_result); 1850 _igvn.replace_node(_resproj, result_phi_rawoop); 1851 } 1852 1853 // Plug slow-path into result merge point 1854 result_region ->init_req( slow_result_path, ctrl ); 1855 result_phi_rawoop->init_req( slow_result_path, slow_result); 1856 result_phi_rawmem->init_req( slow_result_path, _memproj_fallthrough ); 1857 transform_later(result_region); 1858 transform_later(result_phi_rawoop); 1859 transform_later(result_phi_rawmem); 1860 transform_later(result_phi_i_o); 1861 // This completes all paths into the result merge point 1862 } 1863 1864 1865 // Helper for PhaseMacroExpand::expand_allocate_common. 1866 // Initializes the newly-allocated storage. 1867 Node* 1868 PhaseMacroExpand::initialize_object(AllocateNode* alloc, 1869 Node* control, Node* rawmem, Node* object, 1870 Node* klass_node, Node* length, 1871 Node* size_in_bytes) { 1872 InitializeNode* init = alloc->initialization(); 1873 // Store the klass & mark bits 1874 Node* mark_node = NULL; 1875 // For now only enable fast locking for non-array types 1876 if (UseBiasedLocking && (length == NULL)) { 1877 mark_node = make_load(control, rawmem, klass_node, in_bytes(Klass::prototype_header_offset()), TypeRawPtr::BOTTOM, T_ADDRESS); 1878 } else { 1879 mark_node = makecon(TypeRawPtr::make((address)markOopDesc::prototype())); 1880 } 1881 rawmem = make_store(control, rawmem, object, oopDesc::mark_offset_in_bytes(), mark_node, T_ADDRESS); 1882 1883 rawmem = make_store(control, rawmem, object, oopDesc::klass_offset_in_bytes(), klass_node, T_METADATA); 1884 int header_size = alloc->minimum_header_size(); // conservatively small 1885 1886 // Array length 1887 if (length != NULL) { // Arrays need length field 1888 rawmem = make_store(control, rawmem, object, arrayOopDesc::length_offset_in_bytes(), length, T_INT); 1889 // conservatively small header size: 1890 header_size = arrayOopDesc::base_offset_in_bytes(T_BYTE); 1891 ciKlass* k = _igvn.type(klass_node)->is_klassptr()->klass(); 1892 if (k->is_array_klass()) // we know the exact header size in most cases: 1893 header_size = Klass::layout_helper_header_size(k->layout_helper()); 1894 } 1895 1896 // Clear the object body, if necessary. 1897 if (init == NULL) { 1898 // The init has somehow disappeared; be cautious and clear everything. 1899 // 1900 // This can happen if a node is allocated but an uncommon trap occurs 1901 // immediately. In this case, the Initialize gets associated with the 1902 // trap, and may be placed in a different (outer) loop, if the Allocate 1903 // is in a loop. If (this is rare) the inner loop gets unrolled, then 1904 // there can be two Allocates to one Initialize. The answer in all these 1905 // edge cases is safety first. It is always safe to clear immediately 1906 // within an Allocate, and then (maybe or maybe not) clear some more later. 1907 if (!(UseTLAB && ZeroTLAB)) { 1908 rawmem = ClearArrayNode::clear_memory(control, rawmem, object, 1909 header_size, size_in_bytes, 1910 &_igvn); 1911 } 1912 } else { 1913 if (!init->is_complete()) { 1914 // Try to win by zeroing only what the init does not store. 1915 // We can also try to do some peephole optimizations, 1916 // such as combining some adjacent subword stores. 1917 rawmem = init->complete_stores(control, rawmem, object, 1918 header_size, size_in_bytes, &_igvn); 1919 } 1920 // We have no more use for this link, since the AllocateNode goes away: 1921 init->set_req(InitializeNode::RawAddress, top()); 1922 // (If we keep the link, it just confuses the register allocator, 1923 // who thinks he sees a real use of the address by the membar.) 1924 } 1925 1926 return rawmem; 1927 } 1928 1929 // Generate prefetch instructions for next allocations. 1930 Node* PhaseMacroExpand::prefetch_allocation(Node* i_o, Node*& needgc_false, 1931 Node*& contended_phi_rawmem, 1932 Node* old_eden_top, Node* new_eden_top, 1933 Node* length) { 1934 enum { fall_in_path = 1, pf_path = 2 }; 1935 if( UseTLAB && AllocatePrefetchStyle == 2 ) { 1936 // Generate prefetch allocation with watermark check. 1937 // As an allocation hits the watermark, we will prefetch starting 1938 // at a "distance" away from watermark. 1939 1940 Node *pf_region = new RegionNode(3); 1941 Node *pf_phi_rawmem = new PhiNode( pf_region, Type::MEMORY, 1942 TypeRawPtr::BOTTOM ); 1943 // I/O is used for Prefetch 1944 Node *pf_phi_abio = new PhiNode( pf_region, Type::ABIO ); 1945 1946 Node *thread = new ThreadLocalNode(); 1947 transform_later(thread); 1948 1949 Node *eden_pf_adr = new AddPNode( top()/*not oop*/, thread, 1950 _igvn.MakeConX(in_bytes(JavaThread::tlab_pf_top_offset())) ); 1951 transform_later(eden_pf_adr); 1952 1953 Node *old_pf_wm = new LoadPNode(needgc_false, 1954 contended_phi_rawmem, eden_pf_adr, 1955 TypeRawPtr::BOTTOM, TypeRawPtr::BOTTOM, 1956 MemNode::unordered); 1957 transform_later(old_pf_wm); 1958 1959 // check against new_eden_top 1960 Node *need_pf_cmp = new CmpPNode( new_eden_top, old_pf_wm ); 1961 transform_later(need_pf_cmp); 1962 Node *need_pf_bol = new BoolNode( need_pf_cmp, BoolTest::ge ); 1963 transform_later(need_pf_bol); 1964 IfNode *need_pf_iff = new IfNode( needgc_false, need_pf_bol, 1965 PROB_UNLIKELY_MAG(4), COUNT_UNKNOWN ); 1966 transform_later(need_pf_iff); 1967 1968 // true node, add prefetchdistance 1969 Node *need_pf_true = new IfTrueNode( need_pf_iff ); 1970 transform_later(need_pf_true); 1971 1972 Node *need_pf_false = new IfFalseNode( need_pf_iff ); 1973 transform_later(need_pf_false); 1974 1975 Node *new_pf_wmt = new AddPNode( top(), old_pf_wm, 1976 _igvn.MakeConX(AllocatePrefetchDistance) ); 1977 transform_later(new_pf_wmt ); 1978 new_pf_wmt->set_req(0, need_pf_true); 1979 1980 Node *store_new_wmt = new StorePNode(need_pf_true, 1981 contended_phi_rawmem, eden_pf_adr, 1982 TypeRawPtr::BOTTOM, new_pf_wmt, 1983 MemNode::unordered); 1984 transform_later(store_new_wmt); 1985 1986 // adding prefetches 1987 pf_phi_abio->init_req( fall_in_path, i_o ); 1988 1989 Node *prefetch_adr; 1990 Node *prefetch; 1991 uint lines = (length != NULL) ? AllocatePrefetchLines : AllocateInstancePrefetchLines; 1992 uint step_size = AllocatePrefetchStepSize; 1993 uint distance = 0; 1994 1995 for ( uint i = 0; i < lines; i++ ) { 1996 prefetch_adr = new AddPNode( old_pf_wm, new_pf_wmt, 1997 _igvn.MakeConX(distance) ); 1998 transform_later(prefetch_adr); 1999 prefetch = new PrefetchAllocationNode( i_o, prefetch_adr ); 2000 transform_later(prefetch); 2001 distance += step_size; 2002 i_o = prefetch; 2003 } 2004 pf_phi_abio->set_req( pf_path, i_o ); 2005 2006 pf_region->init_req( fall_in_path, need_pf_false ); 2007 pf_region->init_req( pf_path, need_pf_true ); 2008 2009 pf_phi_rawmem->init_req( fall_in_path, contended_phi_rawmem ); 2010 pf_phi_rawmem->init_req( pf_path, store_new_wmt ); 2011 2012 transform_later(pf_region); 2013 transform_later(pf_phi_rawmem); 2014 transform_later(pf_phi_abio); 2015 2016 needgc_false = pf_region; 2017 contended_phi_rawmem = pf_phi_rawmem; 2018 i_o = pf_phi_abio; 2019 } else if( UseTLAB && AllocatePrefetchStyle == 3 ) { 2020 // Insert a prefetch instruction for each allocation. 2021 // This code is used to generate 1 prefetch instruction per cache line. 2022 2023 // Generate several prefetch instructions. 2024 uint lines = (length != NULL) ? AllocatePrefetchLines : AllocateInstancePrefetchLines; 2025 uint step_size = AllocatePrefetchStepSize; 2026 uint distance = AllocatePrefetchDistance; 2027 2028 // Next cache address. 2029 Node *cache_adr = new AddPNode(old_eden_top, old_eden_top, 2030 _igvn.MakeConX(step_size + distance)); 2031 transform_later(cache_adr); 2032 cache_adr = new CastP2XNode(needgc_false, cache_adr); 2033 transform_later(cache_adr); 2034 // Address is aligned to execute prefetch to the beginning of cache line size 2035 // (it is important when BIS instruction is used on SPARC as prefetch). 2036 Node* mask = _igvn.MakeConX(~(intptr_t)(step_size-1)); 2037 cache_adr = new AndXNode(cache_adr, mask); 2038 transform_later(cache_adr); 2039 cache_adr = new CastX2PNode(cache_adr); 2040 transform_later(cache_adr); 2041 2042 // Prefetch 2043 Node *prefetch = new PrefetchAllocationNode( contended_phi_rawmem, cache_adr ); 2044 prefetch->set_req(0, needgc_false); 2045 transform_later(prefetch); 2046 contended_phi_rawmem = prefetch; 2047 Node *prefetch_adr; 2048 distance = step_size; 2049 for ( uint i = 1; i < lines; i++ ) { 2050 prefetch_adr = new AddPNode( cache_adr, cache_adr, 2051 _igvn.MakeConX(distance) ); 2052 transform_later(prefetch_adr); 2053 prefetch = new PrefetchAllocationNode( contended_phi_rawmem, prefetch_adr ); 2054 transform_later(prefetch); 2055 distance += step_size; 2056 contended_phi_rawmem = prefetch; 2057 } 2058 } else if( AllocatePrefetchStyle > 0 ) { 2059 // Insert a prefetch for each allocation only on the fast-path 2060 Node *prefetch_adr; 2061 Node *prefetch; 2062 // Generate several prefetch instructions. 2063 uint lines = (length != NULL) ? AllocatePrefetchLines : AllocateInstancePrefetchLines; 2064 uint step_size = AllocatePrefetchStepSize; 2065 uint distance = AllocatePrefetchDistance; 2066 for ( uint i = 0; i < lines; i++ ) { 2067 prefetch_adr = new AddPNode( old_eden_top, new_eden_top, 2068 _igvn.MakeConX(distance) ); 2069 transform_later(prefetch_adr); 2070 prefetch = new PrefetchAllocationNode( i_o, prefetch_adr ); 2071 // Do not let it float too high, since if eden_top == eden_end, 2072 // both might be null. 2073 if( i == 0 ) { // Set control for first prefetch, next follows it 2074 prefetch->init_req(0, needgc_false); 2075 } 2076 transform_later(prefetch); 2077 distance += step_size; 2078 i_o = prefetch; 2079 } 2080 } 2081 return i_o; 2082 } 2083 2084 2085 void PhaseMacroExpand::expand_allocate(AllocateNode *alloc) { 2086 expand_allocate_common(alloc, NULL, 2087 OptoRuntime::new_instance_Type(), 2088 OptoRuntime::new_instance_Java()); 2089 } 2090 2091 void PhaseMacroExpand::expand_allocate_array(AllocateArrayNode *alloc) { 2092 Node* length = alloc->in(AllocateNode::ALength); 2093 InitializeNode* init = alloc->initialization(); 2094 Node* klass_node = alloc->in(AllocateNode::KlassNode); 2095 ciKlass* k = _igvn.type(klass_node)->is_klassptr()->klass(); 2096 address slow_call_address; // Address of slow call 2097 if (init != NULL && init->is_complete_with_arraycopy() && 2098 k->is_type_array_klass()) { 2099 // Don't zero type array during slow allocation in VM since 2100 // it will be initialized later by arraycopy in compiled code. 2101 slow_call_address = OptoRuntime::new_array_nozero_Java(); 2102 } else { 2103 slow_call_address = OptoRuntime::new_array_Java(); 2104 } 2105 expand_allocate_common(alloc, length, 2106 OptoRuntime::new_array_Type(), 2107 slow_call_address); 2108 } 2109 2110 //-------------------mark_eliminated_box---------------------------------- 2111 // 2112 // During EA obj may point to several objects but after few ideal graph 2113 // transformations (CCP) it may point to only one non escaping object 2114 // (but still using phi), corresponding locks and unlocks will be marked 2115 // for elimination. Later obj could be replaced with a new node (new phi) 2116 // and which does not have escape information. And later after some graph 2117 // reshape other locks and unlocks (which were not marked for elimination 2118 // before) are connected to this new obj (phi) but they still will not be 2119 // marked for elimination since new obj has no escape information. 2120 // Mark all associated (same box and obj) lock and unlock nodes for 2121 // elimination if some of them marked already. 2122 void PhaseMacroExpand::mark_eliminated_box(Node* oldbox, Node* obj) { 2123 if (oldbox->as_BoxLock()->is_eliminated()) 2124 return; // This BoxLock node was processed already. 2125 2126 // New implementation (EliminateNestedLocks) has separate BoxLock 2127 // node for each locked region so mark all associated locks/unlocks as 2128 // eliminated even if different objects are referenced in one locked region 2129 // (for example, OSR compilation of nested loop inside locked scope). 2130 if (EliminateNestedLocks || 2131 oldbox->as_BoxLock()->is_simple_lock_region(NULL, obj)) { 2132 // Box is used only in one lock region. Mark this box as eliminated. 2133 _igvn.hash_delete(oldbox); 2134 oldbox->as_BoxLock()->set_eliminated(); // This changes box's hash value 2135 _igvn.hash_insert(oldbox); 2136 2137 for (uint i = 0; i < oldbox->outcnt(); i++) { 2138 Node* u = oldbox->raw_out(i); 2139 if (u->is_AbstractLock() && !u->as_AbstractLock()->is_non_esc_obj()) { 2140 AbstractLockNode* alock = u->as_AbstractLock(); 2141 // Check lock's box since box could be referenced by Lock's debug info. 2142 if (alock->box_node() == oldbox) { 2143 // Mark eliminated all related locks and unlocks. 2144 #ifdef ASSERT 2145 alock->log_lock_optimization(C, "eliminate_lock_set_non_esc4"); 2146 #endif 2147 alock->set_non_esc_obj(); 2148 } 2149 } 2150 } 2151 return; 2152 } 2153 2154 // Create new "eliminated" BoxLock node and use it in monitor debug info 2155 // instead of oldbox for the same object. 2156 BoxLockNode* newbox = oldbox->clone()->as_BoxLock(); 2157 2158 // Note: BoxLock node is marked eliminated only here and it is used 2159 // to indicate that all associated lock and unlock nodes are marked 2160 // for elimination. 2161 newbox->set_eliminated(); 2162 transform_later(newbox); 2163 2164 // Replace old box node with new box for all users of the same object. 2165 for (uint i = 0; i < oldbox->outcnt();) { 2166 bool next_edge = true; 2167 2168 Node* u = oldbox->raw_out(i); 2169 if (u->is_AbstractLock()) { 2170 AbstractLockNode* alock = u->as_AbstractLock(); 2171 if (alock->box_node() == oldbox && alock->obj_node()->eqv_uncast(obj)) { 2172 // Replace Box and mark eliminated all related locks and unlocks. 2173 #ifdef ASSERT 2174 alock->log_lock_optimization(C, "eliminate_lock_set_non_esc5"); 2175 #endif 2176 alock->set_non_esc_obj(); 2177 _igvn.rehash_node_delayed(alock); 2178 alock->set_box_node(newbox); 2179 next_edge = false; 2180 } 2181 } 2182 if (u->is_FastLock() && u->as_FastLock()->obj_node()->eqv_uncast(obj)) { 2183 FastLockNode* flock = u->as_FastLock(); 2184 assert(flock->box_node() == oldbox, "sanity"); 2185 _igvn.rehash_node_delayed(flock); 2186 flock->set_box_node(newbox); 2187 next_edge = false; 2188 } 2189 2190 // Replace old box in monitor debug info. 2191 if (u->is_SafePoint() && u->as_SafePoint()->jvms()) { 2192 SafePointNode* sfn = u->as_SafePoint(); 2193 JVMState* youngest_jvms = sfn->jvms(); 2194 int max_depth = youngest_jvms->depth(); 2195 for (int depth = 1; depth <= max_depth; depth++) { 2196 JVMState* jvms = youngest_jvms->of_depth(depth); 2197 int num_mon = jvms->nof_monitors(); 2198 // Loop over monitors 2199 for (int idx = 0; idx < num_mon; idx++) { 2200 Node* obj_node = sfn->monitor_obj(jvms, idx); 2201 Node* box_node = sfn->monitor_box(jvms, idx); 2202 if (box_node == oldbox && obj_node->eqv_uncast(obj)) { 2203 int j = jvms->monitor_box_offset(idx); 2204 _igvn.replace_input_of(u, j, newbox); 2205 next_edge = false; 2206 } 2207 } 2208 } 2209 } 2210 if (next_edge) i++; 2211 } 2212 } 2213 2214 //-----------------------mark_eliminated_locking_nodes----------------------- 2215 void PhaseMacroExpand::mark_eliminated_locking_nodes(AbstractLockNode *alock) { 2216 if (EliminateNestedLocks) { 2217 if (alock->is_nested()) { 2218 assert(alock->box_node()->as_BoxLock()->is_eliminated(), "sanity"); 2219 return; 2220 } else if (!alock->is_non_esc_obj()) { // Not eliminated or coarsened 2221 // Only Lock node has JVMState needed here. 2222 // Not that preceding claim is documented anywhere else. 2223 if (alock->jvms() != NULL) { 2224 if (alock->as_Lock()->is_nested_lock_region()) { 2225 // Mark eliminated related nested locks and unlocks. 2226 Node* obj = alock->obj_node(); 2227 BoxLockNode* box_node = alock->box_node()->as_BoxLock(); 2228 assert(!box_node->is_eliminated(), "should not be marked yet"); 2229 // Note: BoxLock node is marked eliminated only here 2230 // and it is used to indicate that all associated lock 2231 // and unlock nodes are marked for elimination. 2232 box_node->set_eliminated(); // Box's hash is always NO_HASH here 2233 for (uint i = 0; i < box_node->outcnt(); i++) { 2234 Node* u = box_node->raw_out(i); 2235 if (u->is_AbstractLock()) { 2236 alock = u->as_AbstractLock(); 2237 if (alock->box_node() == box_node) { 2238 // Verify that this Box is referenced only by related locks. 2239 assert(alock->obj_node()->eqv_uncast(obj), ""); 2240 // Mark all related locks and unlocks. 2241 #ifdef ASSERT 2242 alock->log_lock_optimization(C, "eliminate_lock_set_nested"); 2243 #endif 2244 alock->set_nested(); 2245 } 2246 } 2247 } 2248 } else { 2249 #ifdef ASSERT 2250 alock->log_lock_optimization(C, "eliminate_lock_NOT_nested_lock_region"); 2251 if (C->log() != NULL) 2252 alock->as_Lock()->is_nested_lock_region(C); // rerun for debugging output 2253 #endif 2254 } 2255 } 2256 return; 2257 } 2258 // Process locks for non escaping object 2259 assert(alock->is_non_esc_obj(), ""); 2260 } // EliminateNestedLocks 2261 2262 if (alock->is_non_esc_obj()) { // Lock is used for non escaping object 2263 // Look for all locks of this object and mark them and 2264 // corresponding BoxLock nodes as eliminated. 2265 Node* obj = alock->obj_node(); 2266 for (uint j = 0; j < obj->outcnt(); j++) { 2267 Node* o = obj->raw_out(j); 2268 if (o->is_AbstractLock() && 2269 o->as_AbstractLock()->obj_node()->eqv_uncast(obj)) { 2270 alock = o->as_AbstractLock(); 2271 Node* box = alock->box_node(); 2272 // Replace old box node with new eliminated box for all users 2273 // of the same object and mark related locks as eliminated. 2274 mark_eliminated_box(box, obj); 2275 } 2276 } 2277 } 2278 } 2279 2280 // we have determined that this lock/unlock can be eliminated, we simply 2281 // eliminate the node without expanding it. 2282 // 2283 // Note: The membar's associated with the lock/unlock are currently not 2284 // eliminated. This should be investigated as a future enhancement. 2285 // 2286 bool PhaseMacroExpand::eliminate_locking_node(AbstractLockNode *alock) { 2287 2288 if (!alock->is_eliminated()) { 2289 return false; 2290 } 2291 #ifdef ASSERT 2292 if (!alock->is_coarsened()) { 2293 // Check that new "eliminated" BoxLock node is created. 2294 BoxLockNode* oldbox = alock->box_node()->as_BoxLock(); 2295 assert(oldbox->is_eliminated(), "should be done already"); 2296 } 2297 #endif 2298 2299 alock->log_lock_optimization(C, "eliminate_lock"); 2300 2301 #ifndef PRODUCT 2302 if (PrintEliminateLocks) { 2303 if (alock->is_Lock()) { 2304 tty->print_cr("++++ Eliminated: %d Lock", alock->_idx); 2305 } else { 2306 tty->print_cr("++++ Eliminated: %d Unlock", alock->_idx); 2307 } 2308 } 2309 #endif 2310 2311 Node* mem = alock->in(TypeFunc::Memory); 2312 Node* ctrl = alock->in(TypeFunc::Control); 2313 2314 extract_call_projections(alock); 2315 // There are 2 projections from the lock. The lock node will 2316 // be deleted when its last use is subsumed below. 2317 assert(alock->outcnt() == 2 && 2318 _fallthroughproj != NULL && 2319 _memproj_fallthrough != NULL, 2320 "Unexpected projections from Lock/Unlock"); 2321 2322 Node* fallthroughproj = _fallthroughproj; 2323 Node* memproj_fallthrough = _memproj_fallthrough; 2324 2325 // The memory projection from a lock/unlock is RawMem 2326 // The input to a Lock is merged memory, so extract its RawMem input 2327 // (unless the MergeMem has been optimized away.) 2328 if (alock->is_Lock()) { 2329 // Seach for MemBarAcquireLock node and delete it also. 2330 MemBarNode* membar = fallthroughproj->unique_ctrl_out()->as_MemBar(); 2331 assert(membar != NULL && membar->Opcode() == Op_MemBarAcquireLock, ""); 2332 Node* ctrlproj = membar->proj_out(TypeFunc::Control); 2333 Node* memproj = membar->proj_out(TypeFunc::Memory); 2334 _igvn.replace_node(ctrlproj, fallthroughproj); 2335 _igvn.replace_node(memproj, memproj_fallthrough); 2336 2337 // Delete FastLock node also if this Lock node is unique user 2338 // (a loop peeling may clone a Lock node). 2339 Node* flock = alock->as_Lock()->fastlock_node(); 2340 if (flock->outcnt() == 1) { 2341 assert(flock->unique_out() == alock, "sanity"); 2342 _igvn.replace_node(flock, top()); 2343 } 2344 } 2345 2346 // Seach for MemBarReleaseLock node and delete it also. 2347 if (alock->is_Unlock() && ctrl != NULL && ctrl->is_Proj() && 2348 ctrl->in(0)->is_MemBar()) { 2349 MemBarNode* membar = ctrl->in(0)->as_MemBar(); 2350 assert(membar->Opcode() == Op_MemBarReleaseLock && 2351 mem->is_Proj() && membar == mem->in(0), ""); 2352 _igvn.replace_node(fallthroughproj, ctrl); 2353 _igvn.replace_node(memproj_fallthrough, mem); 2354 fallthroughproj = ctrl; 2355 memproj_fallthrough = mem; 2356 ctrl = membar->in(TypeFunc::Control); 2357 mem = membar->in(TypeFunc::Memory); 2358 } 2359 2360 _igvn.replace_node(fallthroughproj, ctrl); 2361 _igvn.replace_node(memproj_fallthrough, mem); 2362 return true; 2363 } 2364 2365 2366 //------------------------------expand_lock_node---------------------- 2367 void PhaseMacroExpand::expand_lock_node(LockNode *lock) { 2368 2369 Node* ctrl = lock->in(TypeFunc::Control); 2370 Node* mem = lock->in(TypeFunc::Memory); 2371 Node* obj = lock->obj_node(); 2372 Node* box = lock->box_node(); 2373 Node* flock = lock->fastlock_node(); 2374 2375 assert(!box->as_BoxLock()->is_eliminated(), "sanity"); 2376 2377 // Make the merge point 2378 Node *region; 2379 Node *mem_phi; 2380 Node *slow_path; 2381 2382 if (UseOptoBiasInlining) { 2383 /* 2384 * See the full description in MacroAssembler::biased_locking_enter(). 2385 * 2386 * if( (mark_word & biased_lock_mask) == biased_lock_pattern ) { 2387 * // The object is biased. 2388 * proto_node = klass->prototype_header; 2389 * o_node = thread | proto_node; 2390 * x_node = o_node ^ mark_word; 2391 * if( (x_node & ~age_mask) == 0 ) { // Biased to the current thread ? 2392 * // Done. 2393 * } else { 2394 * if( (x_node & biased_lock_mask) != 0 ) { 2395 * // The klass's prototype header is no longer biased. 2396 * cas(&mark_word, mark_word, proto_node) 2397 * goto cas_lock; 2398 * } else { 2399 * // The klass's prototype header is still biased. 2400 * if( (x_node & epoch_mask) != 0 ) { // Expired epoch? 2401 * old = mark_word; 2402 * new = o_node; 2403 * } else { 2404 * // Different thread or anonymous biased. 2405 * old = mark_word & (epoch_mask | age_mask | biased_lock_mask); 2406 * new = thread | old; 2407 * } 2408 * // Try to rebias. 2409 * if( cas(&mark_word, old, new) == 0 ) { 2410 * // Done. 2411 * } else { 2412 * goto slow_path; // Failed. 2413 * } 2414 * } 2415 * } 2416 * } else { 2417 * // The object is not biased. 2418 * cas_lock: 2419 * if( FastLock(obj) == 0 ) { 2420 * // Done. 2421 * } else { 2422 * slow_path: 2423 * OptoRuntime::complete_monitor_locking_Java(obj); 2424 * } 2425 * } 2426 */ 2427 2428 region = new RegionNode(5); 2429 // create a Phi for the memory state 2430 mem_phi = new PhiNode( region, Type::MEMORY, TypeRawPtr::BOTTOM); 2431 2432 Node* fast_lock_region = new RegionNode(3); 2433 Node* fast_lock_mem_phi = new PhiNode( fast_lock_region, Type::MEMORY, TypeRawPtr::BOTTOM); 2434 2435 // First, check mark word for the biased lock pattern. 2436 Node* mark_node = make_load(ctrl, mem, obj, oopDesc::mark_offset_in_bytes(), TypeX_X, TypeX_X->basic_type()); 2437 2438 // Get fast path - mark word has the biased lock pattern. 2439 ctrl = opt_bits_test(ctrl, fast_lock_region, 1, mark_node, 2440 markOopDesc::biased_lock_mask_in_place, 2441 markOopDesc::biased_lock_pattern, true); 2442 // fast_lock_region->in(1) is set to slow path. 2443 fast_lock_mem_phi->init_req(1, mem); 2444 2445 // Now check that the lock is biased to the current thread and has 2446 // the same epoch and bias as Klass::_prototype_header. 2447 2448 // Special-case a fresh allocation to avoid building nodes: 2449 Node* klass_node = AllocateNode::Ideal_klass(obj, &_igvn); 2450 if (klass_node == NULL) { 2451 Node* k_adr = basic_plus_adr(obj, oopDesc::klass_offset_in_bytes()); 2452 klass_node = transform_later(LoadKlassNode::make(_igvn, NULL, mem, k_adr, _igvn.type(k_adr)->is_ptr())); 2453 #ifdef _LP64 2454 if (UseCompressedClassPointers && klass_node->is_DecodeNKlass()) { 2455 assert(klass_node->in(1)->Opcode() == Op_LoadNKlass, "sanity"); 2456 klass_node->in(1)->init_req(0, ctrl); 2457 } else 2458 #endif 2459 klass_node->init_req(0, ctrl); 2460 } 2461 Node *proto_node = make_load(ctrl, mem, klass_node, in_bytes(Klass::prototype_header_offset()), TypeX_X, TypeX_X->basic_type()); 2462 2463 Node* thread = transform_later(new ThreadLocalNode()); 2464 Node* cast_thread = transform_later(new CastP2XNode(ctrl, thread)); 2465 Node* o_node = transform_later(new OrXNode(cast_thread, proto_node)); 2466 Node* x_node = transform_later(new XorXNode(o_node, mark_node)); 2467 2468 // Get slow path - mark word does NOT match the value. 2469 Node* not_biased_ctrl = opt_bits_test(ctrl, region, 3, x_node, 2470 (~markOopDesc::age_mask_in_place), 0); 2471 // region->in(3) is set to fast path - the object is biased to the current thread. 2472 mem_phi->init_req(3, mem); 2473 2474 2475 // Mark word does NOT match the value (thread | Klass::_prototype_header). 2476 2477 2478 // First, check biased pattern. 2479 // Get fast path - _prototype_header has the same biased lock pattern. 2480 ctrl = opt_bits_test(not_biased_ctrl, fast_lock_region, 2, x_node, 2481 markOopDesc::biased_lock_mask_in_place, 0, true); 2482 2483 not_biased_ctrl = fast_lock_region->in(2); // Slow path 2484 // fast_lock_region->in(2) - the prototype header is no longer biased 2485 // and we have to revoke the bias on this object. 2486 // We are going to try to reset the mark of this object to the prototype 2487 // value and fall through to the CAS-based locking scheme. 2488 Node* adr = basic_plus_adr(obj, oopDesc::mark_offset_in_bytes()); 2489 Node* cas = new StoreXConditionalNode(not_biased_ctrl, mem, adr, 2490 proto_node, mark_node); 2491 transform_later(cas); 2492 Node* proj = transform_later(new SCMemProjNode(cas)); 2493 fast_lock_mem_phi->init_req(2, proj); 2494 2495 2496 // Second, check epoch bits. 2497 Node* rebiased_region = new RegionNode(3); 2498 Node* old_phi = new PhiNode( rebiased_region, TypeX_X); 2499 Node* new_phi = new PhiNode( rebiased_region, TypeX_X); 2500 2501 // Get slow path - mark word does NOT match epoch bits. 2502 Node* epoch_ctrl = opt_bits_test(ctrl, rebiased_region, 1, x_node, 2503 markOopDesc::epoch_mask_in_place, 0); 2504 // The epoch of the current bias is not valid, attempt to rebias the object 2505 // toward the current thread. 2506 rebiased_region->init_req(2, epoch_ctrl); 2507 old_phi->init_req(2, mark_node); 2508 new_phi->init_req(2, o_node); 2509 2510 // rebiased_region->in(1) is set to fast path. 2511 // The epoch of the current bias is still valid but we know 2512 // nothing about the owner; it might be set or it might be clear. 2513 Node* cmask = MakeConX(markOopDesc::biased_lock_mask_in_place | 2514 markOopDesc::age_mask_in_place | 2515 markOopDesc::epoch_mask_in_place); 2516 Node* old = transform_later(new AndXNode(mark_node, cmask)); 2517 cast_thread = transform_later(new CastP2XNode(ctrl, thread)); 2518 Node* new_mark = transform_later(new OrXNode(cast_thread, old)); 2519 old_phi->init_req(1, old); 2520 new_phi->init_req(1, new_mark); 2521 2522 transform_later(rebiased_region); 2523 transform_later(old_phi); 2524 transform_later(new_phi); 2525 2526 // Try to acquire the bias of the object using an atomic operation. 2527 // If this fails we will go in to the runtime to revoke the object's bias. 2528 cas = new StoreXConditionalNode(rebiased_region, mem, adr, new_phi, old_phi); 2529 transform_later(cas); 2530 proj = transform_later(new SCMemProjNode(cas)); 2531 2532 // Get slow path - Failed to CAS. 2533 not_biased_ctrl = opt_bits_test(rebiased_region, region, 4, cas, 0, 0); 2534 mem_phi->init_req(4, proj); 2535 // region->in(4) is set to fast path - the object is rebiased to the current thread. 2536 2537 // Failed to CAS. 2538 slow_path = new RegionNode(3); 2539 Node *slow_mem = new PhiNode( slow_path, Type::MEMORY, TypeRawPtr::BOTTOM); 2540 2541 slow_path->init_req(1, not_biased_ctrl); // Capture slow-control 2542 slow_mem->init_req(1, proj); 2543 2544 // Call CAS-based locking scheme (FastLock node). 2545 2546 transform_later(fast_lock_region); 2547 transform_later(fast_lock_mem_phi); 2548 2549 // Get slow path - FastLock failed to lock the object. 2550 ctrl = opt_bits_test(fast_lock_region, region, 2, flock, 0, 0); 2551 mem_phi->init_req(2, fast_lock_mem_phi); 2552 // region->in(2) is set to fast path - the object is locked to the current thread. 2553 2554 slow_path->init_req(2, ctrl); // Capture slow-control 2555 slow_mem->init_req(2, fast_lock_mem_phi); 2556 2557 transform_later(slow_path); 2558 transform_later(slow_mem); 2559 // Reset lock's memory edge. 2560 lock->set_req(TypeFunc::Memory, slow_mem); 2561 2562 } else { 2563 region = new RegionNode(3); 2564 // create a Phi for the memory state 2565 mem_phi = new PhiNode( region, Type::MEMORY, TypeRawPtr::BOTTOM); 2566 2567 // Optimize test; set region slot 2 2568 slow_path = opt_bits_test(ctrl, region, 2, flock, 0, 0); 2569 mem_phi->init_req(2, mem); 2570 } 2571 2572 // Make slow path call 2573 CallNode *call = make_slow_call((CallNode *) lock, OptoRuntime::complete_monitor_enter_Type(), 2574 OptoRuntime::complete_monitor_locking_Java(), NULL, slow_path, 2575 obj, box, NULL); 2576 2577 extract_call_projections(call); 2578 2579 // Slow path can only throw asynchronous exceptions, which are always 2580 // de-opted. So the compiler thinks the slow-call can never throw an 2581 // exception. If it DOES throw an exception we would need the debug 2582 // info removed first (since if it throws there is no monitor). 2583 assert ( _ioproj_fallthrough == NULL && _ioproj_catchall == NULL && 2584 _memproj_catchall == NULL && _catchallcatchproj == NULL, "Unexpected projection from Lock"); 2585 2586 // Capture slow path 2587 // disconnect fall-through projection from call and create a new one 2588 // hook up users of fall-through projection to region 2589 Node *slow_ctrl = _fallthroughproj->clone(); 2590 transform_later(slow_ctrl); 2591 _igvn.hash_delete(_fallthroughproj); 2592 _fallthroughproj->disconnect_inputs(NULL, C); 2593 region->init_req(1, slow_ctrl); 2594 // region inputs are now complete 2595 transform_later(region); 2596 _igvn.replace_node(_fallthroughproj, region); 2597 2598 Node *memproj = transform_later(new ProjNode(call, TypeFunc::Memory)); 2599 mem_phi->init_req(1, memproj ); 2600 transform_later(mem_phi); 2601 _igvn.replace_node(_memproj_fallthrough, mem_phi); 2602 } 2603 2604 //------------------------------expand_unlock_node---------------------- 2605 void PhaseMacroExpand::expand_unlock_node(UnlockNode *unlock) { 2606 2607 Node* ctrl = unlock->in(TypeFunc::Control); 2608 Node* mem = unlock->in(TypeFunc::Memory); 2609 Node* obj = unlock->obj_node(); 2610 Node* box = unlock->box_node(); 2611 2612 assert(!box->as_BoxLock()->is_eliminated(), "sanity"); 2613 2614 // No need for a null check on unlock 2615 2616 // Make the merge point 2617 Node *region; 2618 Node *mem_phi; 2619 2620 if (UseOptoBiasInlining) { 2621 // Check for biased locking unlock case, which is a no-op. 2622 // See the full description in MacroAssembler::biased_locking_exit(). 2623 region = new RegionNode(4); 2624 // create a Phi for the memory state 2625 mem_phi = new PhiNode( region, Type::MEMORY, TypeRawPtr::BOTTOM); 2626 mem_phi->init_req(3, mem); 2627 2628 Node* mark_node = make_load(ctrl, mem, obj, oopDesc::mark_offset_in_bytes(), TypeX_X, TypeX_X->basic_type()); 2629 ctrl = opt_bits_test(ctrl, region, 3, mark_node, 2630 markOopDesc::biased_lock_mask_in_place, 2631 markOopDesc::biased_lock_pattern); 2632 } else { 2633 region = new RegionNode(3); 2634 // create a Phi for the memory state 2635 mem_phi = new PhiNode( region, Type::MEMORY, TypeRawPtr::BOTTOM); 2636 } 2637 2638 FastUnlockNode *funlock = new FastUnlockNode( ctrl, obj, box ); 2639 funlock = transform_later( funlock )->as_FastUnlock(); 2640 // Optimize test; set region slot 2 2641 Node *slow_path = opt_bits_test(ctrl, region, 2, funlock, 0, 0); 2642 Node *thread = transform_later(new ThreadLocalNode()); 2643 2644 CallNode *call = make_slow_call((CallNode *) unlock, OptoRuntime::complete_monitor_exit_Type(), 2645 CAST_FROM_FN_PTR(address, SharedRuntime::complete_monitor_unlocking_C), 2646 "complete_monitor_unlocking_C", slow_path, obj, box, thread); 2647 2648 extract_call_projections(call); 2649 2650 assert ( _ioproj_fallthrough == NULL && _ioproj_catchall == NULL && 2651 _memproj_catchall == NULL && _catchallcatchproj == NULL, "Unexpected projection from Lock"); 2652 2653 // No exceptions for unlocking 2654 // Capture slow path 2655 // disconnect fall-through projection from call and create a new one 2656 // hook up users of fall-through projection to region 2657 Node *slow_ctrl = _fallthroughproj->clone(); 2658 transform_later(slow_ctrl); 2659 _igvn.hash_delete(_fallthroughproj); 2660 _fallthroughproj->disconnect_inputs(NULL, C); 2661 region->init_req(1, slow_ctrl); 2662 // region inputs are now complete 2663 transform_later(region); 2664 _igvn.replace_node(_fallthroughproj, region); 2665 2666 Node *memproj = transform_later(new ProjNode(call, TypeFunc::Memory) ); 2667 mem_phi->init_req(1, memproj ); 2668 mem_phi->init_req(2, mem); 2669 transform_later(mem_phi); 2670 _igvn.replace_node(_memproj_fallthrough, mem_phi); 2671 } 2672 2673 // A value type is returned from the call but we don't know its 2674 // type. Either we get a buffered value (and nothing needs to be done) 2675 // or one of the values being returned is the klass of the value type 2676 // and we need to allocate a value type instance of that type and 2677 // initialize it with other values being returned. In that case, we 2678 // first try a fast path allocation and initialize the value with the 2679 // value klass's pack handler or we fall back to a runtime call. 2680 void PhaseMacroExpand::expand_mh_intrinsic_return(CallStaticJavaNode* call) { 2681 Node* ret = call->proj_out(TypeFunc::Parms); 2682 if (ret == NULL) { 2683 return; 2684 } 2685 assert(ret->bottom_type()->is_valuetypeptr()->is__Value(), "unexpected return type from MH intrinsic"); 2686 const TypeFunc* tf = call->_tf; 2687 const TypeTuple* domain = OptoRuntime::store_value_type_fields_Type()->domain_cc(); 2688 const TypeFunc* new_tf = TypeFunc::make(tf->domain_sig(), tf->domain_cc(), tf->range_sig(), domain); 2689 call->_tf = new_tf; 2690 // Make sure the change of type is applied before projections are 2691 // processed by igvn 2692 _igvn.set_type(call, call->Value(&_igvn)); 2693 _igvn.set_type(ret, ret->Value(&_igvn)); 2694 2695 // Before any new projection is added: 2696 CallProjections* projs = call->extract_projections(true, true); 2697 2698 Node* ctl = new Node(1); 2699 Node* mem = new Node(1); 2700 Node* io = new Node(1); 2701 Node* ex_ctl = new Node(1); 2702 Node* ex_mem = new Node(1); 2703 Node* ex_io = new Node(1); 2704 Node* res = new Node(1); 2705 2706 Node* cast = transform_later(new CastP2XNode(ctl, res)); 2707 Node* mask = MakeConX(0x1); 2708 Node* masked = transform_later(new AndXNode(cast, mask)); 2709 Node* cmp = transform_later(new CmpXNode(masked, mask)); 2710 Node* bol = transform_later(new BoolNode(cmp, BoolTest::eq)); 2711 IfNode* allocation_iff = new IfNode(ctl, bol, PROB_MAX, COUNT_UNKNOWN); 2712 transform_later(allocation_iff); 2713 Node* allocation_ctl = transform_later(new IfTrueNode(allocation_iff)); 2714 Node* no_allocation_ctl = transform_later(new IfFalseNode(allocation_iff)); 2715 2716 Node* no_allocation_res = transform_later(new CheckCastPPNode(no_allocation_ctl, res, TypeValueTypePtr::NOTNULL)); 2717 2718 Node* mask2 = MakeConX(-2); 2719 Node* masked2 = transform_later(new AndXNode(cast, mask2)); 2720 Node* rawklassptr = transform_later(new CastX2PNode(masked2)); 2721 Node* klass_node = transform_later(new CheckCastPPNode(allocation_ctl, rawklassptr, TypeKlassPtr::OBJECT_OR_NULL)); 2722 2723 Node* slowpath_bol = NULL; 2724 Node* top_adr = NULL; 2725 Node* old_top = NULL; 2726 Node* new_top = NULL; 2727 if (UseTLAB) { 2728 Node* end_adr = NULL; 2729 set_eden_pointers(top_adr, end_adr); 2730 Node* end = make_load(ctl, mem, end_adr, 0, TypeRawPtr::BOTTOM, T_ADDRESS); 2731 old_top = new LoadPNode(ctl, mem, top_adr, TypeRawPtr::BOTTOM, TypeRawPtr::BOTTOM, MemNode::unordered); 2732 transform_later(old_top); 2733 Node* layout_val = make_load(NULL, mem, klass_node, in_bytes(Klass::layout_helper_offset()), TypeInt::INT, T_INT); 2734 Node* size_in_bytes = ConvI2X(layout_val); 2735 new_top = new AddPNode(top(), old_top, size_in_bytes); 2736 transform_later(new_top); 2737 Node* slowpath_cmp = new CmpPNode(new_top, end); 2738 transform_later(slowpath_cmp); 2739 slowpath_bol = new BoolNode(slowpath_cmp, BoolTest::ge); 2740 transform_later(slowpath_bol); 2741 } else { 2742 slowpath_bol = intcon(1); 2743 top_adr = top(); 2744 old_top = top(); 2745 new_top = top(); 2746 } 2747 IfNode* slowpath_iff = new IfNode(allocation_ctl, slowpath_bol, PROB_UNLIKELY_MAG(4), COUNT_UNKNOWN); 2748 transform_later(slowpath_iff); 2749 2750 Node* slowpath_true = new IfTrueNode(slowpath_iff); 2751 transform_later(slowpath_true); 2752 2753 CallStaticJavaNode* slow_call = new CallStaticJavaNode(OptoRuntime::store_value_type_fields_Type(), 2754 StubRoutines::store_value_type_fields_to_buf(), 2755 "store_value_type_fields", 2756 call->jvms()->bci(), 2757 TypePtr::BOTTOM); 2758 slow_call->init_req(TypeFunc::Control, slowpath_true); 2759 slow_call->init_req(TypeFunc::Memory, mem); 2760 slow_call->init_req(TypeFunc::I_O, io); 2761 slow_call->init_req(TypeFunc::FramePtr, call->in(TypeFunc::FramePtr)); 2762 slow_call->init_req(TypeFunc::ReturnAdr, call->in(TypeFunc::ReturnAdr)); 2763 slow_call->init_req(TypeFunc::Parms, res); 2764 2765 Node* slow_ctl = transform_later(new ProjNode(slow_call, TypeFunc::Control)); 2766 Node* slow_mem = transform_later(new ProjNode(slow_call, TypeFunc::Memory)); 2767 Node* slow_io = transform_later(new ProjNode(slow_call, TypeFunc::I_O)); 2768 Node* slow_res = transform_later(new ProjNode(slow_call, TypeFunc::Parms)); 2769 Node* slow_catc = transform_later(new CatchNode(slow_ctl, slow_io, 2)); 2770 Node* slow_norm = transform_later(new CatchProjNode(slow_catc, CatchProjNode::fall_through_index, CatchProjNode::no_handler_bci)); 2771 Node* slow_excp = transform_later(new CatchProjNode(slow_catc, CatchProjNode::catch_all_index, CatchProjNode::no_handler_bci)); 2772 2773 Node* ex_r = new RegionNode(3); 2774 Node* ex_mem_phi = new PhiNode(ex_r, Type::MEMORY, TypePtr::BOTTOM); 2775 Node* ex_io_phi = new PhiNode(ex_r, Type::ABIO); 2776 ex_r->init_req(1, slow_excp); 2777 ex_mem_phi->init_req(1, slow_mem); 2778 ex_io_phi->init_req(1, slow_io); 2779 ex_r->init_req(2, ex_ctl); 2780 ex_mem_phi->init_req(2, ex_mem); 2781 ex_io_phi->init_req(2, ex_io); 2782 2783 transform_later(ex_r); 2784 transform_later(ex_mem_phi); 2785 transform_later(ex_io_phi); 2786 2787 Node* slowpath_false = new IfFalseNode(slowpath_iff); 2788 transform_later(slowpath_false); 2789 Node* rawmem = new StorePNode(slowpath_false, mem, top_adr, TypeRawPtr::BOTTOM, new_top, MemNode::unordered); 2790 transform_later(rawmem); 2791 Node* mark_node = NULL; 2792 // For now only enable fast locking for non-array types 2793 if (UseBiasedLocking) { 2794 mark_node = make_load(slowpath_false, rawmem, klass_node, in_bytes(Klass::prototype_header_offset()), TypeRawPtr::BOTTOM, T_ADDRESS); 2795 } else { 2796 mark_node = makecon(TypeRawPtr::make((address)markOopDesc::prototype())); 2797 } 2798 rawmem = make_store(slowpath_false, rawmem, old_top, oopDesc::mark_offset_in_bytes(), mark_node, T_ADDRESS); 2799 rawmem = make_store(slowpath_false, rawmem, old_top, oopDesc::klass_offset_in_bytes(), klass_node, T_METADATA); 2800 if (UseCompressedClassPointers) { 2801 rawmem = make_store(slowpath_false, rawmem, old_top, oopDesc::klass_gap_offset_in_bytes(), intcon(0), T_INT); 2802 } 2803 Node* pack_handler = make_load(slowpath_false, rawmem, klass_node, in_bytes(ValueKlass::pack_handler_offset()), TypeRawPtr::BOTTOM, T_ADDRESS); 2804 2805 CallLeafNoFPNode* handler_call = new CallLeafNoFPNode(OptoRuntime::pack_value_type_Type(), 2806 NULL, 2807 "pack handler", 2808 TypeRawPtr::BOTTOM); 2809 handler_call->init_req(TypeFunc::Control, slowpath_false); 2810 handler_call->init_req(TypeFunc::Memory, rawmem); 2811 handler_call->init_req(TypeFunc::I_O, top()); 2812 handler_call->init_req(TypeFunc::FramePtr, call->in(TypeFunc::FramePtr)); 2813 handler_call->init_req(TypeFunc::ReturnAdr, top()); 2814 handler_call->init_req(TypeFunc::Parms, pack_handler); 2815 handler_call->init_req(TypeFunc::Parms+1, old_top); 2816 2817 // We don't know how many values are returned. This assumes the 2818 // worst case, that all available registers are used. 2819 for (uint i = TypeFunc::Parms+1; i < domain->cnt(); i++) { 2820 if (domain->field_at(i) == Type::HALF) { 2821 slow_call->init_req(i, top()); 2822 handler_call->init_req(i+1, top()); 2823 continue; 2824 } 2825 Node* proj = transform_later(new ProjNode(call, i)); 2826 slow_call->init_req(i, proj); 2827 handler_call->init_req(i+1, proj); 2828 } 2829 2830 // We can safepoint at that new call 2831 copy_call_debug_info(call, slow_call); 2832 transform_later(slow_call); 2833 transform_later(handler_call); 2834 2835 Node* handler_ctl = transform_later(new ProjNode(handler_call, TypeFunc::Control)); 2836 rawmem = transform_later(new ProjNode(handler_call, TypeFunc::Memory)); 2837 Node* slowpath_false_res = transform_later(new ProjNode(handler_call, TypeFunc::Parms)); 2838 2839 MergeMemNode* slowpath_false_mem = MergeMemNode::make(mem); 2840 slowpath_false_mem->set_memory_at(Compile::AliasIdxRaw, rawmem); 2841 transform_later(slowpath_false_mem); 2842 2843 Node* r = new RegionNode(4); 2844 Node* mem_phi = new PhiNode(r, Type::MEMORY, TypePtr::BOTTOM); 2845 Node* io_phi = new PhiNode(r, Type::ABIO); 2846 Node* res_phi = new PhiNode(r, ret->bottom_type()); 2847 2848 r->init_req(1, no_allocation_ctl); 2849 mem_phi->init_req(1, mem); 2850 io_phi->init_req(1, io); 2851 res_phi->init_req(1, no_allocation_res); 2852 r->init_req(2, slow_norm); 2853 mem_phi->init_req(2, slow_mem); 2854 io_phi->init_req(2, slow_io); 2855 res_phi->init_req(2, slow_res); 2856 r->init_req(3, handler_ctl); 2857 mem_phi->init_req(3, slowpath_false_mem); 2858 io_phi->init_req(3, io); 2859 res_phi->init_req(3, slowpath_false_res); 2860 2861 transform_later(r); 2862 transform_later(mem_phi); 2863 transform_later(io_phi); 2864 transform_later(res_phi); 2865 2866 assert(projs->nb_resproj == 1, "unexpected number of results"); 2867 _igvn.replace_in_uses(projs->fallthrough_catchproj, r); 2868 _igvn.replace_in_uses(projs->fallthrough_memproj, mem_phi); 2869 _igvn.replace_in_uses(projs->fallthrough_ioproj, io_phi); 2870 _igvn.replace_in_uses(projs->resproj[0], res_phi); 2871 _igvn.replace_in_uses(projs->catchall_catchproj, ex_r); 2872 _igvn.replace_in_uses(projs->catchall_memproj, ex_mem_phi); 2873 _igvn.replace_in_uses(projs->catchall_ioproj, ex_io_phi); 2874 2875 _igvn.replace_node(ctl, projs->fallthrough_catchproj); 2876 _igvn.replace_node(mem, projs->fallthrough_memproj); 2877 _igvn.replace_node(io, projs->fallthrough_ioproj); 2878 _igvn.replace_node(res, projs->resproj[0]); 2879 _igvn.replace_node(ex_ctl, projs->catchall_catchproj); 2880 _igvn.replace_node(ex_mem, projs->catchall_memproj); 2881 _igvn.replace_node(ex_io, projs->catchall_ioproj); 2882 } 2883 2884 //---------------------------eliminate_macro_nodes---------------------- 2885 // Eliminate scalar replaced allocations and associated locks. 2886 void PhaseMacroExpand::eliminate_macro_nodes() { 2887 if (C->macro_count() == 0) 2888 return; 2889 2890 // First, attempt to eliminate locks 2891 int cnt = C->macro_count(); 2892 for (int i=0; i < cnt; i++) { 2893 Node *n = C->macro_node(i); 2894 if (n->is_AbstractLock()) { // Lock and Unlock nodes 2895 // Before elimination mark all associated (same box and obj) 2896 // lock and unlock nodes. 2897 mark_eliminated_locking_nodes(n->as_AbstractLock()); 2898 } 2899 } 2900 bool progress = true; 2901 while (progress) { 2902 progress = false; 2903 for (int i = C->macro_count(); i > 0; i--) { 2904 Node * n = C->macro_node(i-1); 2905 bool success = false; 2906 debug_only(int old_macro_count = C->macro_count();); 2907 if (n->is_AbstractLock()) { 2908 success = eliminate_locking_node(n->as_AbstractLock()); 2909 } 2910 assert(success == (C->macro_count() < old_macro_count), "elimination reduces macro count"); 2911 progress = progress || success; 2912 } 2913 } 2914 // Next, attempt to eliminate allocations 2915 _has_locks = false; 2916 progress = true; 2917 while (progress) { 2918 progress = false; 2919 for (int i = C->macro_count(); i > 0; i--) { 2920 Node * n = C->macro_node(i-1); 2921 bool success = false; 2922 debug_only(int old_macro_count = C->macro_count();); 2923 switch (n->class_id()) { 2924 case Node::Class_Allocate: 2925 case Node::Class_AllocateArray: 2926 success = eliminate_allocate_node(n->as_Allocate()); 2927 break; 2928 case Node::Class_CallStaticJava: { 2929 CallStaticJavaNode* call = n->as_CallStaticJava(); 2930 if (!call->method()->is_method_handle_intrinsic()) { 2931 success = eliminate_boxing_node(n->as_CallStaticJava()); 2932 } 2933 break; 2934 } 2935 case Node::Class_Lock: 2936 case Node::Class_Unlock: 2937 assert(!n->as_AbstractLock()->is_eliminated(), "sanity"); 2938 _has_locks = true; 2939 break; 2940 case Node::Class_ArrayCopy: 2941 break; 2942 case Node::Class_OuterStripMinedLoop: 2943 break; 2944 default: 2945 assert(n->Opcode() == Op_LoopLimit || 2946 n->Opcode() == Op_Opaque1 || 2947 n->Opcode() == Op_Opaque2 || 2948 n->Opcode() == Op_Opaque3, "unknown node type in macro list"); 2949 } 2950 assert(success == (C->macro_count() < old_macro_count), "elimination reduces macro count"); 2951 progress = progress || success; 2952 } 2953 } 2954 } 2955 2956 //------------------------------expand_macro_nodes---------------------- 2957 // Returns true if a failure occurred. 2958 bool PhaseMacroExpand::expand_macro_nodes() { 2959 // Last attempt to eliminate macro nodes. 2960 eliminate_macro_nodes(); 2961 2962 // Make sure expansion will not cause node limit to be exceeded. 2963 // Worst case is a macro node gets expanded into about 200 nodes. 2964 // Allow 50% more for optimization. 2965 if (C->check_node_count(C->macro_count() * 300, "out of nodes before macro expansion" ) ) 2966 return true; 2967 2968 // Eliminate Opaque and LoopLimit nodes. Do it after all loop optimizations. 2969 bool progress = true; 2970 while (progress) { 2971 progress = false; 2972 for (int i = C->macro_count(); i > 0; i--) { 2973 Node * n = C->macro_node(i-1); 2974 bool success = false; 2975 debug_only(int old_macro_count = C->macro_count();); 2976 if (n->Opcode() == Op_LoopLimit) { 2977 // Remove it from macro list and put on IGVN worklist to optimize. 2978 C->remove_macro_node(n); 2979 _igvn._worklist.push(n); 2980 success = true; 2981 } else if (n->Opcode() == Op_CallStaticJava) { 2982 CallStaticJavaNode* call = n->as_CallStaticJava(); 2983 if (!call->method()->is_method_handle_intrinsic()) { 2984 // Remove it from macro list and put on IGVN worklist to optimize. 2985 C->remove_macro_node(n); 2986 _igvn._worklist.push(n); 2987 success = true; 2988 } 2989 } else if (n->Opcode() == Op_Opaque1 || n->Opcode() == Op_Opaque2) { 2990 _igvn.replace_node(n, n->in(1)); 2991 success = true; 2992 #if INCLUDE_RTM_OPT 2993 } else if ((n->Opcode() == Op_Opaque3) && ((Opaque3Node*)n)->rtm_opt()) { 2994 assert(C->profile_rtm(), "should be used only in rtm deoptimization code"); 2995 assert((n->outcnt() == 1) && n->unique_out()->is_Cmp(), ""); 2996 Node* cmp = n->unique_out(); 2997 #ifdef ASSERT 2998 // Validate graph. 2999 assert((cmp->outcnt() == 1) && cmp->unique_out()->is_Bool(), ""); 3000 BoolNode* bol = cmp->unique_out()->as_Bool(); 3001 assert((bol->outcnt() == 1) && bol->unique_out()->is_If() && 3002 (bol->_test._test == BoolTest::ne), ""); 3003 IfNode* ifn = bol->unique_out()->as_If(); 3004 assert((ifn->outcnt() == 2) && 3005 ifn->proj_out(1)->is_uncommon_trap_proj(Deoptimization::Reason_rtm_state_change) != NULL, ""); 3006 #endif 3007 Node* repl = n->in(1); 3008 if (!_has_locks) { 3009 // Remove RTM state check if there are no locks in the code. 3010 // Replace input to compare the same value. 3011 repl = (cmp->in(1) == n) ? cmp->in(2) : cmp->in(1); 3012 } 3013 _igvn.replace_node(n, repl); 3014 success = true; 3015 #endif 3016 } else if (n->Opcode() == Op_OuterStripMinedLoop) { 3017 n->as_OuterStripMinedLoop()->adjust_strip_mined_loop(&_igvn); 3018 C->remove_macro_node(n); 3019 success = true; 3020 } 3021 assert(success == (C->macro_count() < old_macro_count), "elimination reduces macro count"); 3022 progress = progress || success; 3023 } 3024 } 3025 3026 // expand arraycopy "macro" nodes first 3027 // For ReduceBulkZeroing, we must first process all arraycopy nodes 3028 // before the allocate nodes are expanded. 3029 int macro_idx = C->macro_count() - 1; 3030 while (macro_idx >= 0) { 3031 Node * n = C->macro_node(macro_idx); 3032 assert(n->is_macro(), "only macro nodes expected here"); 3033 if (_igvn.type(n) == Type::TOP || n->in(0)->is_top() ) { 3034 // node is unreachable, so don't try to expand it 3035 C->remove_macro_node(n); 3036 } else if (n->is_ArrayCopy()){ 3037 int macro_count = C->macro_count(); 3038 expand_arraycopy_node(n->as_ArrayCopy()); 3039 assert(C->macro_count() < macro_count, "must have deleted a node from macro list"); 3040 } 3041 if (C->failing()) return true; 3042 macro_idx --; 3043 } 3044 3045 // expand "macro" nodes 3046 // nodes are removed from the macro list as they are processed 3047 while (C->macro_count() > 0) { 3048 int macro_count = C->macro_count(); 3049 Node * n = C->macro_node(macro_count-1); 3050 assert(n->is_macro(), "only macro nodes expected here"); 3051 if (_igvn.type(n) == Type::TOP || n->in(0)->is_top() ) { 3052 // node is unreachable, so don't try to expand it 3053 C->remove_macro_node(n); 3054 continue; 3055 } 3056 switch (n->class_id()) { 3057 case Node::Class_Allocate: 3058 expand_allocate(n->as_Allocate()); 3059 break; 3060 case Node::Class_AllocateArray: 3061 expand_allocate_array(n->as_AllocateArray()); 3062 break; 3063 case Node::Class_Lock: 3064 expand_lock_node(n->as_Lock()); 3065 break; 3066 case Node::Class_Unlock: 3067 expand_unlock_node(n->as_Unlock()); 3068 break; 3069 case Node::Class_CallStaticJava: 3070 expand_mh_intrinsic_return(n->as_CallStaticJava()); 3071 C->remove_macro_node(n); 3072 break; 3073 default: 3074 assert(false, "unknown node type in macro list"); 3075 } 3076 assert(C->macro_count() < macro_count, "must have deleted a node from macro list"); 3077 if (C->failing()) return true; 3078 } 3079 3080 _igvn.set_delay_transform(false); 3081 _igvn.optimize(); 3082 if (C->failing()) return true; 3083 return false; 3084 }