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