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