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