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