1 /* 2 * Copyright (c) 1997, 2024, Oracle and/or its affiliates. All rights reserved. 3 * Copyright (c) 2024, Alibaba Group Holding Limited. All rights reserved. 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * This code is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License version 2 only, as 8 * published by the Free Software Foundation. 9 * 10 * This code is distributed in the hope that it will be useful, but WITHOUT 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13 * version 2 for more details (a copy is included in the LICENSE file that 14 * accompanied this code). 15 * 16 * You should have received a copy of the GNU General Public License version 17 * 2 along with this work; if not, write to the Free Software Foundation, 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 19 * 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 21 * or visit www.oracle.com if you need additional information or have any 22 * questions. 23 * 24 */ 25 26 #include "precompiled.hpp" 27 #include "ci/ciFlatArrayKlass.hpp" 28 #include "classfile/javaClasses.hpp" 29 #include "classfile/systemDictionary.hpp" 30 #include "compiler/compileLog.hpp" 31 #include "gc/shared/barrierSet.hpp" 32 #include "gc/shared/c2/barrierSetC2.hpp" 33 #include "gc/shared/tlab_globals.hpp" 34 #include "memory/allocation.inline.hpp" 35 #include "memory/resourceArea.hpp" 36 #include "oops/objArrayKlass.hpp" 37 #include "opto/addnode.hpp" 38 #include "opto/arraycopynode.hpp" 39 #include "opto/cfgnode.hpp" 40 #include "opto/regalloc.hpp" 41 #include "opto/compile.hpp" 42 #include "opto/connode.hpp" 43 #include "opto/convertnode.hpp" 44 #include "opto/inlinetypenode.hpp" 45 #include "opto/loopnode.hpp" 46 #include "opto/machnode.hpp" 47 #include "opto/matcher.hpp" 48 #include "opto/memnode.hpp" 49 #include "opto/mulnode.hpp" 50 #include "opto/narrowptrnode.hpp" 51 #include "opto/phaseX.hpp" 52 #include "opto/regmask.hpp" 53 #include "opto/rootnode.hpp" 54 #include "opto/vectornode.hpp" 55 #include "utilities/align.hpp" 56 #include "utilities/copy.hpp" 57 #include "utilities/macros.hpp" 58 #include "utilities/powerOfTwo.hpp" 59 #include "utilities/vmError.hpp" 60 61 // Portions of code courtesy of Clifford Click 62 63 // Optimization - Graph Style 64 65 static Node *step_through_mergemem(PhaseGVN *phase, MergeMemNode *mmem, const TypePtr *tp, const TypePtr *adr_check, outputStream *st); 66 67 //============================================================================= 68 uint MemNode::size_of() const { return sizeof(*this); } 69 70 const TypePtr *MemNode::adr_type() const { 71 Node* adr = in(Address); 72 if (adr == nullptr) return nullptr; // node is dead 73 const TypePtr* cross_check = nullptr; 74 DEBUG_ONLY(cross_check = _adr_type); 75 return calculate_adr_type(adr->bottom_type(), cross_check); 76 } 77 78 bool MemNode::check_if_adr_maybe_raw(Node* adr) { 79 if (adr != nullptr) { 80 if (adr->bottom_type()->base() == Type::RawPtr || adr->bottom_type()->base() == Type::AnyPtr) { 81 return true; 82 } 83 } 84 return false; 85 } 86 87 #ifndef PRODUCT 88 void MemNode::dump_spec(outputStream *st) const { 89 if (in(Address) == nullptr) return; // node is dead 90 #ifndef ASSERT 91 // fake the missing field 92 const TypePtr* _adr_type = nullptr; 93 if (in(Address) != nullptr) 94 _adr_type = in(Address)->bottom_type()->isa_ptr(); 95 #endif 96 dump_adr_type(this, _adr_type, st); 97 98 Compile* C = Compile::current(); 99 if (C->alias_type(_adr_type)->is_volatile()) { 100 st->print(" Volatile!"); 101 } 102 if (_unaligned_access) { 103 st->print(" unaligned"); 104 } 105 if (_mismatched_access) { 106 st->print(" mismatched"); 107 } 108 if (_unsafe_access) { 109 st->print(" unsafe"); 110 } 111 } 112 113 void MemNode::dump_adr_type(const Node* mem, const TypePtr* adr_type, outputStream *st) { 114 st->print(" @"); 115 if (adr_type == nullptr) { 116 st->print("null"); 117 } else { 118 adr_type->dump_on(st); 119 Compile* C = Compile::current(); 120 Compile::AliasType* atp = nullptr; 121 if (C->have_alias_type(adr_type)) atp = C->alias_type(adr_type); 122 if (atp == nullptr) 123 st->print(", idx=?\?;"); 124 else if (atp->index() == Compile::AliasIdxBot) 125 st->print(", idx=Bot;"); 126 else if (atp->index() == Compile::AliasIdxTop) 127 st->print(", idx=Top;"); 128 else if (atp->index() == Compile::AliasIdxRaw) 129 st->print(", idx=Raw;"); 130 else { 131 ciField* field = atp->field(); 132 if (field) { 133 st->print(", name="); 134 field->print_name_on(st); 135 } 136 st->print(", idx=%d;", atp->index()); 137 } 138 } 139 } 140 141 extern void print_alias_types(); 142 143 #endif 144 145 Node *MemNode::optimize_simple_memory_chain(Node *mchain, const TypeOopPtr *t_oop, Node *load, PhaseGVN *phase) { 146 assert((t_oop != nullptr), "sanity"); 147 bool is_instance = t_oop->is_known_instance_field(); 148 bool is_boxed_value_load = t_oop->is_ptr_to_boxed_value() && 149 (load != nullptr) && load->is_Load() && 150 (phase->is_IterGVN() != nullptr); 151 if (!(is_instance || is_boxed_value_load)) 152 return mchain; // don't try to optimize non-instance types 153 uint instance_id = t_oop->instance_id(); 154 Node *start_mem = phase->C->start()->proj_out_or_null(TypeFunc::Memory); 155 Node *prev = nullptr; 156 Node *result = mchain; 157 while (prev != result) { 158 prev = result; 159 if (result == start_mem) 160 break; // hit one of our sentinels 161 // skip over a call which does not affect this memory slice 162 if (result->is_Proj() && result->as_Proj()->_con == TypeFunc::Memory) { 163 Node *proj_in = result->in(0); 164 if (proj_in->is_Allocate() && proj_in->_idx == instance_id) { 165 break; // hit one of our sentinels 166 } else if (proj_in->is_Call()) { 167 // ArrayCopyNodes processed here as well 168 CallNode *call = proj_in->as_Call(); 169 if (!call->may_modify(t_oop, phase)) { // returns false for instances 170 result = call->in(TypeFunc::Memory); 171 } 172 } else if (proj_in->is_Initialize()) { 173 AllocateNode* alloc = proj_in->as_Initialize()->allocation(); 174 // Stop if this is the initialization for the object instance which 175 // contains this memory slice, otherwise skip over it. 176 if ((alloc == nullptr) || (alloc->_idx == instance_id)) { 177 break; 178 } 179 if (is_instance) { 180 result = proj_in->in(TypeFunc::Memory); 181 } else if (is_boxed_value_load) { 182 Node* klass = alloc->in(AllocateNode::KlassNode); 183 const TypeKlassPtr* tklass = phase->type(klass)->is_klassptr(); 184 if (tklass->klass_is_exact() && !tklass->exact_klass()->equals(t_oop->is_instptr()->exact_klass())) { 185 result = proj_in->in(TypeFunc::Memory); // not related allocation 186 } 187 } 188 } else if (proj_in->is_MemBar()) { 189 ArrayCopyNode* ac = nullptr; 190 if (ArrayCopyNode::may_modify(t_oop, proj_in->as_MemBar(), phase, ac)) { 191 break; 192 } 193 result = proj_in->in(TypeFunc::Memory); 194 } else if (proj_in->is_top()) { 195 break; // dead code 196 } else { 197 assert(false, "unexpected projection"); 198 } 199 } else if (result->is_ClearArray()) { 200 if (!is_instance || !ClearArrayNode::step_through(&result, instance_id, phase)) { 201 // Can not bypass initialization of the instance 202 // we are looking for. 203 break; 204 } 205 // Otherwise skip it (the call updated 'result' value). 206 } else if (result->is_MergeMem()) { 207 result = step_through_mergemem(phase, result->as_MergeMem(), t_oop, nullptr, tty); 208 } 209 } 210 return result; 211 } 212 213 Node *MemNode::optimize_memory_chain(Node *mchain, const TypePtr *t_adr, Node *load, PhaseGVN *phase) { 214 const TypeOopPtr* t_oop = t_adr->isa_oopptr(); 215 if (t_oop == nullptr) 216 return mchain; // don't try to optimize non-oop types 217 Node* result = optimize_simple_memory_chain(mchain, t_oop, load, phase); 218 bool is_instance = t_oop->is_known_instance_field(); 219 PhaseIterGVN *igvn = phase->is_IterGVN(); 220 if (is_instance && igvn != nullptr && result->is_Phi()) { 221 PhiNode *mphi = result->as_Phi(); 222 assert(mphi->bottom_type() == Type::MEMORY, "memory phi required"); 223 const TypePtr *t = mphi->adr_type(); 224 bool do_split = false; 225 // In the following cases, Load memory input can be further optimized based on 226 // its precise address type 227 if (t == TypePtr::BOTTOM || t == TypeRawPtr::BOTTOM ) { 228 do_split = true; 229 } else if (t->isa_oopptr() && !t->is_oopptr()->is_known_instance()) { 230 const TypeOopPtr* mem_t = 231 t->is_oopptr()->cast_to_exactness(true) 232 ->is_oopptr()->cast_to_ptr_type(t_oop->ptr()) 233 ->is_oopptr()->cast_to_instance_id(t_oop->instance_id()); 234 if (t_oop->isa_aryptr()) { 235 mem_t = mem_t->is_aryptr() 236 ->cast_to_stable(t_oop->is_aryptr()->is_stable()) 237 ->cast_to_size(t_oop->is_aryptr()->size()) 238 ->cast_to_not_flat(t_oop->is_aryptr()->is_not_flat()) 239 ->cast_to_not_null_free(t_oop->is_aryptr()->is_not_null_free()) 240 ->with_offset(t_oop->is_aryptr()->offset()) 241 ->is_aryptr(); 242 } 243 do_split = mem_t == t_oop; 244 } 245 if (do_split) { 246 // clone the Phi with our address type 247 result = mphi->split_out_instance(t_adr, igvn); 248 } else { 249 assert(phase->C->get_alias_index(t) == phase->C->get_alias_index(t_adr), "correct memory chain"); 250 } 251 } 252 return result; 253 } 254 255 static Node *step_through_mergemem(PhaseGVN *phase, MergeMemNode *mmem, const TypePtr *tp, const TypePtr *adr_check, outputStream *st) { 256 uint alias_idx = phase->C->get_alias_index(tp); 257 Node *mem = mmem; 258 #ifdef ASSERT 259 { 260 // Check that current type is consistent with the alias index used during graph construction 261 assert(alias_idx >= Compile::AliasIdxRaw, "must not be a bad alias_idx"); 262 bool consistent = adr_check == nullptr || adr_check->empty() || 263 phase->C->must_alias(adr_check, alias_idx ); 264 // Sometimes dead array references collapse to a[-1], a[-2], or a[-3] 265 if( !consistent && adr_check != nullptr && !adr_check->empty() && 266 tp->isa_aryptr() && tp->offset() == Type::OffsetBot && 267 adr_check->isa_aryptr() && adr_check->offset() != Type::OffsetBot && 268 ( adr_check->offset() == arrayOopDesc::length_offset_in_bytes() || 269 adr_check->offset() == oopDesc::klass_offset_in_bytes() || 270 adr_check->offset() == oopDesc::mark_offset_in_bytes() ) ) { 271 // don't assert if it is dead code. 272 consistent = true; 273 } 274 if( !consistent ) { 275 st->print("alias_idx==%d, adr_check==", alias_idx); 276 if( adr_check == nullptr ) { 277 st->print("null"); 278 } else { 279 adr_check->dump(); 280 } 281 st->cr(); 282 print_alias_types(); 283 assert(consistent, "adr_check must match alias idx"); 284 } 285 } 286 #endif 287 // TypeOopPtr::NOTNULL+any is an OOP with unknown offset - generally 288 // means an array I have not precisely typed yet. Do not do any 289 // alias stuff with it any time soon. 290 const TypeOopPtr *toop = tp->isa_oopptr(); 291 if (tp->base() != Type::AnyPtr && 292 !(toop && 293 toop->isa_instptr() && 294 toop->is_instptr()->instance_klass()->is_java_lang_Object() && 295 toop->offset() == Type::OffsetBot)) { 296 // compress paths and change unreachable cycles to TOP 297 // If not, we can update the input infinitely along a MergeMem cycle 298 // Equivalent code in PhiNode::Ideal 299 Node* m = phase->transform(mmem); 300 // If transformed to a MergeMem, get the desired slice 301 // Otherwise the returned node represents memory for every slice 302 mem = (m->is_MergeMem())? m->as_MergeMem()->memory_at(alias_idx) : m; 303 // Update input if it is progress over what we have now 304 } 305 return mem; 306 } 307 308 //--------------------------Ideal_common--------------------------------------- 309 // Look for degenerate control and memory inputs. Bypass MergeMem inputs. 310 // Unhook non-raw memories from complete (macro-expanded) initializations. 311 Node *MemNode::Ideal_common(PhaseGVN *phase, bool can_reshape) { 312 // If our control input is a dead region, kill all below the region 313 Node *ctl = in(MemNode::Control); 314 if (ctl && remove_dead_region(phase, can_reshape)) 315 return this; 316 ctl = in(MemNode::Control); 317 // Don't bother trying to transform a dead node 318 if (ctl && ctl->is_top()) return NodeSentinel; 319 320 PhaseIterGVN *igvn = phase->is_IterGVN(); 321 // Wait if control on the worklist. 322 if (ctl && can_reshape && igvn != nullptr) { 323 Node* bol = nullptr; 324 Node* cmp = nullptr; 325 if (ctl->in(0)->is_If()) { 326 assert(ctl->is_IfTrue() || ctl->is_IfFalse(), "sanity"); 327 bol = ctl->in(0)->in(1); 328 if (bol->is_Bool()) 329 cmp = ctl->in(0)->in(1)->in(1); 330 } 331 if (igvn->_worklist.member(ctl) || 332 (bol != nullptr && igvn->_worklist.member(bol)) || 333 (cmp != nullptr && igvn->_worklist.member(cmp)) ) { 334 // This control path may be dead. 335 // Delay this memory node transformation until the control is processed. 336 igvn->_worklist.push(this); 337 return NodeSentinel; // caller will return null 338 } 339 } 340 // Ignore if memory is dead, or self-loop 341 Node *mem = in(MemNode::Memory); 342 if (phase->type( mem ) == Type::TOP) return NodeSentinel; // caller will return null 343 assert(mem != this, "dead loop in MemNode::Ideal"); 344 345 if (can_reshape && igvn != nullptr && igvn->_worklist.member(mem)) { 346 // This memory slice may be dead. 347 // Delay this mem node transformation until the memory is processed. 348 igvn->_worklist.push(this); 349 return NodeSentinel; // caller will return null 350 } 351 352 Node *address = in(MemNode::Address); 353 const Type *t_adr = phase->type(address); 354 if (t_adr == Type::TOP) return NodeSentinel; // caller will return null 355 356 if (can_reshape && is_unsafe_access() && (t_adr == TypePtr::NULL_PTR)) { 357 // Unsafe off-heap access with zero address. Remove access and other control users 358 // to not confuse optimizations and add a HaltNode to fail if this is ever executed. 359 assert(ctl != nullptr, "unsafe accesses should be control dependent"); 360 for (DUIterator_Fast imax, i = ctl->fast_outs(imax); i < imax; i++) { 361 Node* u = ctl->fast_out(i); 362 if (u != ctl) { 363 igvn->rehash_node_delayed(u); 364 int nb = u->replace_edge(ctl, phase->C->top(), igvn); 365 --i, imax -= nb; 366 } 367 } 368 Node* frame = igvn->transform(new ParmNode(phase->C->start(), TypeFunc::FramePtr)); 369 Node* halt = igvn->transform(new HaltNode(ctl, frame, "unsafe off-heap access with zero address")); 370 phase->C->root()->add_req(halt); 371 return this; 372 } 373 374 if (can_reshape && igvn != nullptr && 375 (igvn->_worklist.member(address) || 376 (igvn->_worklist.size() > 0 && t_adr != adr_type())) ) { 377 // The address's base and type may change when the address is processed. 378 // Delay this mem node transformation until the address is processed. 379 igvn->_worklist.push(this); 380 return NodeSentinel; // caller will return null 381 } 382 383 // Do NOT remove or optimize the next lines: ensure a new alias index 384 // is allocated for an oop pointer type before Escape Analysis. 385 // Note: C++ will not remove it since the call has side effect. 386 if (t_adr->isa_oopptr()) { 387 int alias_idx = phase->C->get_alias_index(t_adr->is_ptr()); 388 } 389 390 Node* base = nullptr; 391 if (address->is_AddP()) { 392 base = address->in(AddPNode::Base); 393 } 394 if (base != nullptr && phase->type(base)->higher_equal(TypePtr::NULL_PTR) && 395 !t_adr->isa_rawptr()) { 396 // Note: raw address has TOP base and top->higher_equal(TypePtr::NULL_PTR) is true. 397 // Skip this node optimization if its address has TOP base. 398 return NodeSentinel; // caller will return null 399 } 400 401 // Avoid independent memory operations 402 Node* old_mem = mem; 403 404 // The code which unhooks non-raw memories from complete (macro-expanded) 405 // initializations was removed. After macro-expansion all stores caught 406 // by Initialize node became raw stores and there is no information 407 // which memory slices they modify. So it is unsafe to move any memory 408 // operation above these stores. Also in most cases hooked non-raw memories 409 // were already unhooked by using information from detect_ptr_independence() 410 // and find_previous_store(). 411 412 if (mem->is_MergeMem()) { 413 MergeMemNode* mmem = mem->as_MergeMem(); 414 const TypePtr *tp = t_adr->is_ptr(); 415 416 mem = step_through_mergemem(phase, mmem, tp, adr_type(), tty); 417 } 418 419 if (mem != old_mem) { 420 set_req_X(MemNode::Memory, mem, phase); 421 if (phase->type(mem) == Type::TOP) return NodeSentinel; 422 return this; 423 } 424 425 // let the subclass continue analyzing... 426 return nullptr; 427 } 428 429 // Helper function for proving some simple control dominations. 430 // Attempt to prove that all control inputs of 'dom' dominate 'sub'. 431 // Already assumes that 'dom' is available at 'sub', and that 'sub' 432 // is not a constant (dominated by the method's StartNode). 433 // Used by MemNode::find_previous_store to prove that the 434 // control input of a memory operation predates (dominates) 435 // an allocation it wants to look past. 436 // Returns 'DomResult::Dominate' if all control inputs of 'dom' 437 // dominate 'sub', 'DomResult::NotDominate' if not, 438 // and 'DomResult::EncounteredDeadCode' if we can't decide due to 439 // dead code, but at the end of IGVN, we know the definite result 440 // once the dead code is cleaned up. 441 Node::DomResult MemNode::maybe_all_controls_dominate(Node* dom, Node* sub) { 442 if (dom == nullptr || dom->is_top() || sub == nullptr || sub->is_top()) { 443 return DomResult::EncounteredDeadCode; // Conservative answer for dead code 444 } 445 446 // Check 'dom'. Skip Proj and CatchProj nodes. 447 dom = dom->find_exact_control(dom); 448 if (dom == nullptr || dom->is_top()) { 449 return DomResult::EncounteredDeadCode; // Conservative answer for dead code 450 } 451 452 if (dom == sub) { 453 // For the case when, for example, 'sub' is Initialize and the original 454 // 'dom' is Proj node of the 'sub'. 455 return DomResult::NotDominate; 456 } 457 458 if (dom->is_Con() || dom->is_Start() || dom->is_Root() || dom == sub) { 459 return DomResult::Dominate; 460 } 461 462 // 'dom' dominates 'sub' if its control edge and control edges 463 // of all its inputs dominate or equal to sub's control edge. 464 465 // Currently 'sub' is either Allocate, Initialize or Start nodes. 466 // Or Region for the check in LoadNode::Ideal(); 467 // 'sub' should have sub->in(0) != nullptr. 468 assert(sub->is_Allocate() || sub->is_Initialize() || sub->is_Start() || 469 sub->is_Region() || sub->is_Call(), "expecting only these nodes"); 470 471 // Get control edge of 'sub'. 472 Node* orig_sub = sub; 473 sub = sub->find_exact_control(sub->in(0)); 474 if (sub == nullptr || sub->is_top()) { 475 return DomResult::EncounteredDeadCode; // Conservative answer for dead code 476 } 477 478 assert(sub->is_CFG(), "expecting control"); 479 480 if (sub == dom) { 481 return DomResult::Dominate; 482 } 483 484 if (sub->is_Start() || sub->is_Root()) { 485 return DomResult::NotDominate; 486 } 487 488 { 489 // Check all control edges of 'dom'. 490 491 ResourceMark rm; 492 Node_List nlist; 493 Unique_Node_List dom_list; 494 495 dom_list.push(dom); 496 bool only_dominating_controls = false; 497 498 for (uint next = 0; next < dom_list.size(); next++) { 499 Node* n = dom_list.at(next); 500 if (n == orig_sub) { 501 return DomResult::NotDominate; // One of dom's inputs dominated by sub. 502 } 503 if (!n->is_CFG() && n->pinned()) { 504 // Check only own control edge for pinned non-control nodes. 505 n = n->find_exact_control(n->in(0)); 506 if (n == nullptr || n->is_top()) { 507 return DomResult::EncounteredDeadCode; // Conservative answer for dead code 508 } 509 assert(n->is_CFG(), "expecting control"); 510 dom_list.push(n); 511 } else if (n->is_Con() || n->is_Start() || n->is_Root()) { 512 only_dominating_controls = true; 513 } else if (n->is_CFG()) { 514 DomResult dom_result = n->dominates(sub, nlist); 515 if (dom_result == DomResult::Dominate) { 516 only_dominating_controls = true; 517 } else { 518 return dom_result; 519 } 520 } else { 521 // First, own control edge. 522 Node* m = n->find_exact_control(n->in(0)); 523 if (m != nullptr) { 524 if (m->is_top()) { 525 return DomResult::EncounteredDeadCode; // Conservative answer for dead code 526 } 527 dom_list.push(m); 528 } 529 // Now, the rest of edges. 530 uint cnt = n->req(); 531 for (uint i = 1; i < cnt; i++) { 532 m = n->find_exact_control(n->in(i)); 533 if (m == nullptr || m->is_top()) { 534 continue; 535 } 536 dom_list.push(m); 537 } 538 } 539 } 540 return only_dominating_controls ? DomResult::Dominate : DomResult::NotDominate; 541 } 542 } 543 544 //---------------------detect_ptr_independence--------------------------------- 545 // Used by MemNode::find_previous_store to prove that two base 546 // pointers are never equal. 547 // The pointers are accompanied by their associated allocations, 548 // if any, which have been previously discovered by the caller. 549 bool MemNode::detect_ptr_independence(Node* p1, AllocateNode* a1, 550 Node* p2, AllocateNode* a2, 551 PhaseTransform* phase) { 552 // Attempt to prove that these two pointers cannot be aliased. 553 // They may both manifestly be allocations, and they should differ. 554 // Or, if they are not both allocations, they can be distinct constants. 555 // Otherwise, one is an allocation and the other a pre-existing value. 556 if (a1 == nullptr && a2 == nullptr) { // neither an allocation 557 return (p1 != p2) && p1->is_Con() && p2->is_Con(); 558 } else if (a1 != nullptr && a2 != nullptr) { // both allocations 559 return (a1 != a2); 560 } else if (a1 != nullptr) { // one allocation a1 561 // (Note: p2->is_Con implies p2->in(0)->is_Root, which dominates.) 562 return all_controls_dominate(p2, a1); 563 } else { //(a2 != null) // one allocation a2 564 return all_controls_dominate(p1, a2); 565 } 566 return false; 567 } 568 569 570 // Find an arraycopy ac that produces the memory state represented by parameter mem. 571 // Return ac if 572 // (a) can_see_stored_value=true and ac must have set the value for this load or if 573 // (b) can_see_stored_value=false and ac could have set the value for this load or if 574 // (c) can_see_stored_value=false and ac cannot have set the value for this load. 575 // In case (c) change the parameter mem to the memory input of ac to skip it 576 // when searching stored value. 577 // Otherwise return null. 578 Node* LoadNode::find_previous_arraycopy(PhaseValues* phase, Node* ld_alloc, Node*& mem, bool can_see_stored_value) const { 579 ArrayCopyNode* ac = find_array_copy_clone(ld_alloc, mem); 580 if (ac != nullptr) { 581 Node* ld_addp = in(MemNode::Address); 582 Node* src = ac->in(ArrayCopyNode::Src); 583 const TypeAryPtr* ary_t = phase->type(src)->isa_aryptr(); 584 585 // This is a load from a cloned array. The corresponding arraycopy ac must 586 // have set the value for the load and we can return ac but only if the load 587 // is known to be within bounds. This is checked below. 588 if (ary_t != nullptr && ld_addp->is_AddP()) { 589 Node* ld_offs = ld_addp->in(AddPNode::Offset); 590 BasicType ary_elem = ary_t->elem()->array_element_basic_type(); 591 jlong header = arrayOopDesc::base_offset_in_bytes(ary_elem); 592 jlong elemsize = type2aelembytes(ary_elem); 593 594 const TypeX* ld_offs_t = phase->type(ld_offs)->isa_intptr_t(); 595 const TypeInt* sizetype = ary_t->size(); 596 597 if (ld_offs_t->_lo >= header && ld_offs_t->_hi < (sizetype->_lo * elemsize + header)) { 598 // The load is known to be within bounds. It receives its value from ac. 599 return ac; 600 } 601 // The load is known to be out-of-bounds. 602 } 603 // The load could be out-of-bounds. It must not be hoisted but must remain 604 // dependent on the runtime range check. This is achieved by returning null. 605 } else if (mem->is_Proj() && mem->in(0) != nullptr && mem->in(0)->is_ArrayCopy()) { 606 ArrayCopyNode* ac = mem->in(0)->as_ArrayCopy(); 607 608 if (ac->is_arraycopy_validated() || 609 ac->is_copyof_validated() || 610 ac->is_copyofrange_validated()) { 611 Node* ld_addp = in(MemNode::Address); 612 if (ld_addp->is_AddP()) { 613 Node* ld_base = ld_addp->in(AddPNode::Address); 614 Node* ld_offs = ld_addp->in(AddPNode::Offset); 615 616 Node* dest = ac->in(ArrayCopyNode::Dest); 617 618 if (dest == ld_base) { 619 const TypeX* ld_offs_t = phase->type(ld_offs)->isa_intptr_t(); 620 assert(!ld_offs_t->empty(), "dead reference should be checked already"); 621 // Take into account vector or unsafe access size 622 jlong ld_size_in_bytes = (jlong)memory_size(); 623 jlong offset_hi = ld_offs_t->_hi + ld_size_in_bytes - 1; 624 offset_hi = MIN2(offset_hi, (jlong)(TypeX::MAX->_hi)); // Take care for overflow in 32-bit VM 625 if (ac->modifies(ld_offs_t->_lo, (intptr_t)offset_hi, phase, can_see_stored_value)) { 626 return ac; 627 } 628 if (!can_see_stored_value) { 629 mem = ac->in(TypeFunc::Memory); 630 return ac; 631 } 632 } 633 } 634 } 635 } 636 return nullptr; 637 } 638 639 ArrayCopyNode* MemNode::find_array_copy_clone(Node* ld_alloc, Node* mem) const { 640 if (mem->is_Proj() && mem->in(0) != nullptr && (mem->in(0)->Opcode() == Op_MemBarStoreStore || 641 mem->in(0)->Opcode() == Op_MemBarCPUOrder)) { 642 if (ld_alloc != nullptr) { 643 // Check if there is an array copy for a clone 644 Node* mb = mem->in(0); 645 ArrayCopyNode* ac = nullptr; 646 if (mb->in(0) != nullptr && mb->in(0)->is_Proj() && 647 mb->in(0)->in(0) != nullptr && mb->in(0)->in(0)->is_ArrayCopy()) { 648 ac = mb->in(0)->in(0)->as_ArrayCopy(); 649 } else { 650 // Step over GC barrier when ReduceInitialCardMarks is disabled 651 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2(); 652 Node* control_proj_ac = bs->step_over_gc_barrier(mb->in(0)); 653 654 if (control_proj_ac->is_Proj() && control_proj_ac->in(0)->is_ArrayCopy()) { 655 ac = control_proj_ac->in(0)->as_ArrayCopy(); 656 } 657 } 658 659 if (ac != nullptr && ac->is_clonebasic()) { 660 AllocateNode* alloc = AllocateNode::Ideal_allocation(ac->in(ArrayCopyNode::Dest)); 661 if (alloc != nullptr && alloc == ld_alloc) { 662 return ac; 663 } 664 } 665 } 666 } 667 return nullptr; 668 } 669 670 // The logic for reordering loads and stores uses four steps: 671 // (a) Walk carefully past stores and initializations which we 672 // can prove are independent of this load. 673 // (b) Observe that the next memory state makes an exact match 674 // with self (load or store), and locate the relevant store. 675 // (c) Ensure that, if we were to wire self directly to the store, 676 // the optimizer would fold it up somehow. 677 // (d) Do the rewiring, and return, depending on some other part of 678 // the optimizer to fold up the load. 679 // This routine handles steps (a) and (b). Steps (c) and (d) are 680 // specific to loads and stores, so they are handled by the callers. 681 // (Currently, only LoadNode::Ideal has steps (c), (d). More later.) 682 // 683 Node* MemNode::find_previous_store(PhaseValues* phase) { 684 Node* ctrl = in(MemNode::Control); 685 Node* adr = in(MemNode::Address); 686 intptr_t offset = 0; 687 Node* base = AddPNode::Ideal_base_and_offset(adr, phase, offset); 688 AllocateNode* alloc = AllocateNode::Ideal_allocation(base); 689 690 if (offset == Type::OffsetBot) 691 return nullptr; // cannot unalias unless there are precise offsets 692 693 const bool adr_maybe_raw = check_if_adr_maybe_raw(adr); 694 const TypeOopPtr *addr_t = adr->bottom_type()->isa_oopptr(); 695 696 intptr_t size_in_bytes = memory_size(); 697 698 Node* mem = in(MemNode::Memory); // start searching here... 699 700 int cnt = 50; // Cycle limiter 701 for (;;) { // While we can dance past unrelated stores... 702 if (--cnt < 0) break; // Caught in cycle or a complicated dance? 703 704 Node* prev = mem; 705 if (mem->is_Store()) { 706 Node* st_adr = mem->in(MemNode::Address); 707 intptr_t st_offset = 0; 708 Node* st_base = AddPNode::Ideal_base_and_offset(st_adr, phase, st_offset); 709 if (st_base == nullptr) 710 break; // inscrutable pointer 711 712 // For raw accesses it's not enough to prove that constant offsets don't intersect. 713 // We need the bases to be the equal in order for the offset check to make sense. 714 if ((adr_maybe_raw || check_if_adr_maybe_raw(st_adr)) && st_base != base) { 715 break; 716 } 717 718 if (st_offset != offset && st_offset != Type::OffsetBot) { 719 const int MAX_STORE = MAX2(BytesPerLong, (int)MaxVectorSize); 720 assert(mem->as_Store()->memory_size() <= MAX_STORE, ""); 721 if (st_offset >= offset + size_in_bytes || 722 st_offset <= offset - MAX_STORE || 723 st_offset <= offset - mem->as_Store()->memory_size()) { 724 // Success: The offsets are provably independent. 725 // (You may ask, why not just test st_offset != offset and be done? 726 // The answer is that stores of different sizes can co-exist 727 // in the same sequence of RawMem effects. We sometimes initialize 728 // a whole 'tile' of array elements with a single jint or jlong.) 729 mem = mem->in(MemNode::Memory); 730 continue; // (a) advance through independent store memory 731 } 732 } 733 if (st_base != base && 734 detect_ptr_independence(base, alloc, 735 st_base, 736 AllocateNode::Ideal_allocation(st_base), 737 phase)) { 738 // Success: The bases are provably independent. 739 mem = mem->in(MemNode::Memory); 740 continue; // (a) advance through independent store memory 741 } 742 743 // (b) At this point, if the bases or offsets do not agree, we lose, 744 // since we have not managed to prove 'this' and 'mem' independent. 745 if (st_base == base && st_offset == offset) { 746 return mem; // let caller handle steps (c), (d) 747 } 748 749 } else if (mem->is_Proj() && mem->in(0)->is_Initialize()) { 750 InitializeNode* st_init = mem->in(0)->as_Initialize(); 751 AllocateNode* st_alloc = st_init->allocation(); 752 if (st_alloc == nullptr) { 753 break; // something degenerated 754 } 755 bool known_identical = false; 756 bool known_independent = false; 757 if (alloc == st_alloc) { 758 known_identical = true; 759 } else if (alloc != nullptr) { 760 known_independent = true; 761 } else if (all_controls_dominate(this, st_alloc)) { 762 known_independent = true; 763 } 764 765 if (known_independent) { 766 // The bases are provably independent: Either they are 767 // manifestly distinct allocations, or else the control 768 // of this load dominates the store's allocation. 769 int alias_idx = phase->C->get_alias_index(adr_type()); 770 if (alias_idx == Compile::AliasIdxRaw) { 771 mem = st_alloc->in(TypeFunc::Memory); 772 } else { 773 mem = st_init->memory(alias_idx); 774 } 775 continue; // (a) advance through independent store memory 776 } 777 778 // (b) at this point, if we are not looking at a store initializing 779 // the same allocation we are loading from, we lose. 780 if (known_identical) { 781 // From caller, can_see_stored_value will consult find_captured_store. 782 return mem; // let caller handle steps (c), (d) 783 } 784 785 } else if (find_previous_arraycopy(phase, alloc, mem, false) != nullptr) { 786 if (prev != mem) { 787 // Found an arraycopy but it doesn't affect that load 788 continue; 789 } 790 // Found an arraycopy that may affect that load 791 return mem; 792 } else if (addr_t != nullptr && addr_t->is_known_instance_field()) { 793 // Can't use optimize_simple_memory_chain() since it needs PhaseGVN. 794 if (mem->is_Proj() && mem->in(0)->is_Call()) { 795 // ArrayCopyNodes processed here as well. 796 CallNode *call = mem->in(0)->as_Call(); 797 if (!call->may_modify(addr_t, phase)) { 798 mem = call->in(TypeFunc::Memory); 799 continue; // (a) advance through independent call memory 800 } 801 } else if (mem->is_Proj() && mem->in(0)->is_MemBar()) { 802 ArrayCopyNode* ac = nullptr; 803 if (ArrayCopyNode::may_modify(addr_t, mem->in(0)->as_MemBar(), phase, ac)) { 804 break; 805 } 806 mem = mem->in(0)->in(TypeFunc::Memory); 807 continue; // (a) advance through independent MemBar memory 808 } else if (mem->is_ClearArray()) { 809 if (ClearArrayNode::step_through(&mem, (uint)addr_t->instance_id(), phase)) { 810 // (the call updated 'mem' value) 811 continue; // (a) advance through independent allocation memory 812 } else { 813 // Can not bypass initialization of the instance 814 // we are looking for. 815 return mem; 816 } 817 } else if (mem->is_MergeMem()) { 818 int alias_idx = phase->C->get_alias_index(adr_type()); 819 mem = mem->as_MergeMem()->memory_at(alias_idx); 820 continue; // (a) advance through independent MergeMem memory 821 } 822 } 823 824 // Unless there is an explicit 'continue', we must bail out here, 825 // because 'mem' is an inscrutable memory state (e.g., a call). 826 break; 827 } 828 829 return nullptr; // bail out 830 } 831 832 //----------------------calculate_adr_type------------------------------------- 833 // Helper function. Notices when the given type of address hits top or bottom. 834 // Also, asserts a cross-check of the type against the expected address type. 835 const TypePtr* MemNode::calculate_adr_type(const Type* t, const TypePtr* cross_check) { 836 if (t == Type::TOP) return nullptr; // does not touch memory any more? 837 #ifdef ASSERT 838 if (!VerifyAliases || VMError::is_error_reported() || Node::in_dump()) cross_check = nullptr; 839 #endif 840 const TypePtr* tp = t->isa_ptr(); 841 if (tp == nullptr) { 842 assert(cross_check == nullptr || cross_check == TypePtr::BOTTOM, "expected memory type must be wide"); 843 return TypePtr::BOTTOM; // touches lots of memory 844 } else { 845 #ifdef ASSERT 846 // %%%% [phh] We don't check the alias index if cross_check is 847 // TypeRawPtr::BOTTOM. Needs to be investigated. 848 if (cross_check != nullptr && 849 cross_check != TypePtr::BOTTOM && 850 cross_check != TypeRawPtr::BOTTOM) { 851 // Recheck the alias index, to see if it has changed (due to a bug). 852 Compile* C = Compile::current(); 853 assert(C->get_alias_index(cross_check) == C->get_alias_index(tp), 854 "must stay in the original alias category"); 855 // The type of the address must be contained in the adr_type, 856 // disregarding "null"-ness. 857 // (We make an exception for TypeRawPtr::BOTTOM, which is a bit bucket.) 858 const TypePtr* tp_notnull = tp->join(TypePtr::NOTNULL)->is_ptr(); 859 assert(cross_check->meet(tp_notnull) == cross_check->remove_speculative(), 860 "real address must not escape from expected memory type"); 861 } 862 #endif 863 return tp; 864 } 865 } 866 867 uint8_t MemNode::barrier_data(const Node* n) { 868 if (n->is_LoadStore()) { 869 return n->as_LoadStore()->barrier_data(); 870 } else if (n->is_Mem()) { 871 return n->as_Mem()->barrier_data(); 872 } 873 return 0; 874 } 875 876 //============================================================================= 877 // Should LoadNode::Ideal() attempt to remove control edges? 878 bool LoadNode::can_remove_control() const { 879 return !has_pinned_control_dependency(); 880 } 881 uint LoadNode::size_of() const { return sizeof(*this); } 882 bool LoadNode::cmp(const Node &n) const { 883 LoadNode& load = (LoadNode &)n; 884 return Type::equals(_type, load._type) && 885 _control_dependency == load._control_dependency && 886 _mo == load._mo; 887 } 888 const Type *LoadNode::bottom_type() const { return _type; } 889 uint LoadNode::ideal_reg() const { 890 return _type->ideal_reg(); 891 } 892 893 #ifndef PRODUCT 894 void LoadNode::dump_spec(outputStream *st) const { 895 MemNode::dump_spec(st); 896 if( !Verbose && !WizardMode ) { 897 // standard dump does this in Verbose and WizardMode 898 st->print(" #"); _type->dump_on(st); 899 } 900 if (!depends_only_on_test()) { 901 st->print(" (does not depend only on test, "); 902 if (control_dependency() == UnknownControl) { 903 st->print("unknown control"); 904 } else if (control_dependency() == Pinned) { 905 st->print("pinned"); 906 } else if (adr_type() == TypeRawPtr::BOTTOM) { 907 st->print("raw access"); 908 } else { 909 st->print("unknown reason"); 910 } 911 st->print(")"); 912 } 913 } 914 #endif 915 916 #ifdef ASSERT 917 //----------------------------is_immutable_value------------------------------- 918 // Helper function to allow a raw load without control edge for some cases 919 bool LoadNode::is_immutable_value(Node* adr) { 920 if (adr->is_AddP() && adr->in(AddPNode::Base)->is_top() && 921 adr->in(AddPNode::Address)->Opcode() == Op_ThreadLocal) { 922 923 jlong offset = adr->in(AddPNode::Offset)->find_intptr_t_con(-1); 924 int offsets[] = { 925 in_bytes(JavaThread::osthread_offset()), 926 in_bytes(JavaThread::threadObj_offset()), 927 in_bytes(JavaThread::vthread_offset()), 928 in_bytes(JavaThread::scopedValueCache_offset()), 929 }; 930 931 for (size_t i = 0; i < sizeof offsets / sizeof offsets[0]; i++) { 932 if (offset == offsets[i]) { 933 return true; 934 } 935 } 936 } 937 938 return false; 939 } 940 #endif 941 942 //----------------------------LoadNode::make----------------------------------- 943 // Polymorphic factory method: 944 Node* LoadNode::make(PhaseGVN& gvn, Node* ctl, Node* mem, Node* adr, const TypePtr* adr_type, const Type* rt, BasicType bt, MemOrd mo, 945 ControlDependency control_dependency, bool require_atomic_access, bool unaligned, bool mismatched, bool unsafe, uint8_t barrier_data) { 946 Compile* C = gvn.C; 947 948 // sanity check the alias category against the created node type 949 assert(!(adr_type->isa_oopptr() && 950 adr_type->offset() == oopDesc::klass_offset_in_bytes()), 951 "use LoadKlassNode instead"); 952 assert(!(adr_type->isa_aryptr() && 953 adr_type->offset() == arrayOopDesc::length_offset_in_bytes()), 954 "use LoadRangeNode instead"); 955 // Check control edge of raw loads 956 assert( ctl != nullptr || C->get_alias_index(adr_type) != Compile::AliasIdxRaw || 957 // oop will be recorded in oop map if load crosses safepoint 958 rt->isa_oopptr() || is_immutable_value(adr), 959 "raw memory operations should have control edge"); 960 LoadNode* load = nullptr; 961 switch (bt) { 962 case T_BOOLEAN: load = new LoadUBNode(ctl, mem, adr, adr_type, rt->is_int(), mo, control_dependency); break; 963 case T_BYTE: load = new LoadBNode (ctl, mem, adr, adr_type, rt->is_int(), mo, control_dependency); break; 964 case T_INT: load = new LoadINode (ctl, mem, adr, adr_type, rt->is_int(), mo, control_dependency); break; 965 case T_CHAR: load = new LoadUSNode(ctl, mem, adr, adr_type, rt->is_int(), mo, control_dependency); break; 966 case T_SHORT: load = new LoadSNode (ctl, mem, adr, adr_type, rt->is_int(), mo, control_dependency); break; 967 case T_LONG: load = new LoadLNode (ctl, mem, adr, adr_type, rt->is_long(), mo, control_dependency, require_atomic_access); break; 968 case T_FLOAT: load = new LoadFNode (ctl, mem, adr, adr_type, rt, mo, control_dependency); break; 969 case T_DOUBLE: load = new LoadDNode (ctl, mem, adr, adr_type, rt, mo, control_dependency, require_atomic_access); break; 970 case T_ADDRESS: load = new LoadPNode (ctl, mem, adr, adr_type, rt->is_ptr(), mo, control_dependency); break; 971 case T_OBJECT: 972 case T_NARROWOOP: 973 #ifdef _LP64 974 if (adr->bottom_type()->is_ptr_to_narrowoop()) { 975 load = new LoadNNode(ctl, mem, adr, adr_type, rt->make_narrowoop(), mo, control_dependency); 976 } else 977 #endif 978 { 979 assert(!adr->bottom_type()->is_ptr_to_narrowoop() && !adr->bottom_type()->is_ptr_to_narrowklass(), "should have got back a narrow oop"); 980 load = new LoadPNode(ctl, mem, adr, adr_type, rt->is_ptr(), mo, control_dependency); 981 } 982 break; 983 default: 984 ShouldNotReachHere(); 985 break; 986 } 987 assert(load != nullptr, "LoadNode should have been created"); 988 if (unaligned) { 989 load->set_unaligned_access(); 990 } 991 if (mismatched) { 992 load->set_mismatched_access(); 993 } 994 if (unsafe) { 995 load->set_unsafe_access(); 996 } 997 load->set_barrier_data(barrier_data); 998 if (load->Opcode() == Op_LoadN) { 999 Node* ld = gvn.transform(load); 1000 return new DecodeNNode(ld, ld->bottom_type()->make_ptr()); 1001 } 1002 1003 return load; 1004 } 1005 1006 //------------------------------hash------------------------------------------- 1007 uint LoadNode::hash() const { 1008 // unroll addition of interesting fields 1009 return (uintptr_t)in(Control) + (uintptr_t)in(Memory) + (uintptr_t)in(Address); 1010 } 1011 1012 static bool skip_through_membars(Compile::AliasType* atp, const TypeInstPtr* tp, bool eliminate_boxing) { 1013 if ((atp != nullptr) && (atp->index() >= Compile::AliasIdxRaw)) { 1014 bool non_volatile = (atp->field() != nullptr) && !atp->field()->is_volatile(); 1015 bool is_stable_ary = FoldStableValues && 1016 (tp != nullptr) && (tp->isa_aryptr() != nullptr) && 1017 tp->isa_aryptr()->is_stable(); 1018 1019 return (eliminate_boxing && non_volatile) || is_stable_ary || tp->is_inlinetypeptr(); 1020 } 1021 1022 return false; 1023 } 1024 1025 LoadNode* LoadNode::pin_array_access_node() const { 1026 const TypePtr* adr_type = this->adr_type(); 1027 if (adr_type != nullptr && adr_type->isa_aryptr()) { 1028 return clone_pinned(); 1029 } 1030 return nullptr; 1031 } 1032 1033 // Is the value loaded previously stored by an arraycopy? If so return 1034 // a load node that reads from the source array so we may be able to 1035 // optimize out the ArrayCopy node later. 1036 Node* LoadNode::can_see_arraycopy_value(Node* st, PhaseGVN* phase) const { 1037 Node* ld_adr = in(MemNode::Address); 1038 intptr_t ld_off = 0; 1039 AllocateNode* ld_alloc = AllocateNode::Ideal_allocation(ld_adr, phase, ld_off); 1040 Node* ac = find_previous_arraycopy(phase, ld_alloc, st, true); 1041 if (ac != nullptr) { 1042 assert(ac->is_ArrayCopy(), "what kind of node can this be?"); 1043 1044 Node* mem = ac->in(TypeFunc::Memory); 1045 Node* ctl = ac->in(0); 1046 Node* src = ac->in(ArrayCopyNode::Src); 1047 1048 if (!ac->as_ArrayCopy()->is_clonebasic() && !phase->type(src)->isa_aryptr()) { 1049 return nullptr; 1050 } 1051 1052 // load depends on the tests that validate the arraycopy 1053 LoadNode* ld = clone_pinned(); 1054 Node* addp = in(MemNode::Address)->clone(); 1055 if (ac->as_ArrayCopy()->is_clonebasic()) { 1056 assert(ld_alloc != nullptr, "need an alloc"); 1057 assert(addp->is_AddP(), "address must be addp"); 1058 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2(); 1059 assert(bs->step_over_gc_barrier(addp->in(AddPNode::Base)) == bs->step_over_gc_barrier(ac->in(ArrayCopyNode::Dest)), "strange pattern"); 1060 assert(bs->step_over_gc_barrier(addp->in(AddPNode::Address)) == bs->step_over_gc_barrier(ac->in(ArrayCopyNode::Dest)), "strange pattern"); 1061 addp->set_req(AddPNode::Base, src); 1062 addp->set_req(AddPNode::Address, src); 1063 } else { 1064 assert(ac->as_ArrayCopy()->is_arraycopy_validated() || 1065 ac->as_ArrayCopy()->is_copyof_validated() || 1066 ac->as_ArrayCopy()->is_copyofrange_validated(), "only supported cases"); 1067 assert(addp->in(AddPNode::Base) == addp->in(AddPNode::Address), "should be"); 1068 addp->set_req(AddPNode::Base, src); 1069 addp->set_req(AddPNode::Address, src); 1070 1071 const TypeAryPtr* ary_t = phase->type(in(MemNode::Address))->isa_aryptr(); 1072 BasicType ary_elem = ary_t->isa_aryptr()->elem()->array_element_basic_type(); 1073 if (is_reference_type(ary_elem, true)) ary_elem = T_OBJECT; 1074 1075 uint header = arrayOopDesc::base_offset_in_bytes(ary_elem); 1076 uint shift = ary_t->is_flat() ? ary_t->flat_log_elem_size() : exact_log2(type2aelembytes(ary_elem)); 1077 1078 Node* diff = phase->transform(new SubINode(ac->in(ArrayCopyNode::SrcPos), ac->in(ArrayCopyNode::DestPos))); 1079 #ifdef _LP64 1080 diff = phase->transform(new ConvI2LNode(diff)); 1081 #endif 1082 diff = phase->transform(new LShiftXNode(diff, phase->intcon(shift))); 1083 1084 Node* offset = phase->transform(new AddXNode(addp->in(AddPNode::Offset), diff)); 1085 addp->set_req(AddPNode::Offset, offset); 1086 } 1087 addp = phase->transform(addp); 1088 #ifdef ASSERT 1089 const TypePtr* adr_type = phase->type(addp)->is_ptr(); 1090 ld->_adr_type = adr_type; 1091 #endif 1092 ld->set_req(MemNode::Address, addp); 1093 ld->set_req(0, ctl); 1094 ld->set_req(MemNode::Memory, mem); 1095 return ld; 1096 } 1097 return nullptr; 1098 } 1099 1100 1101 //---------------------------can_see_stored_value------------------------------ 1102 // This routine exists to make sure this set of tests is done the same 1103 // everywhere. We need to make a coordinated change: first LoadNode::Ideal 1104 // will change the graph shape in a way which makes memory alive twice at the 1105 // same time (uses the Oracle model of aliasing), then some 1106 // LoadXNode::Identity will fold things back to the equivalence-class model 1107 // of aliasing. 1108 Node* MemNode::can_see_stored_value(Node* st, PhaseValues* phase) const { 1109 Node* ld_adr = in(MemNode::Address); 1110 intptr_t ld_off = 0; 1111 Node* ld_base = AddPNode::Ideal_base_and_offset(ld_adr, phase, ld_off); 1112 Node* ld_alloc = AllocateNode::Ideal_allocation(ld_base); 1113 const TypeInstPtr* tp = phase->type(ld_adr)->isa_instptr(); 1114 Compile::AliasType* atp = (tp != nullptr) ? phase->C->alias_type(tp) : nullptr; 1115 // This is more general than load from boxing objects. 1116 if (skip_through_membars(atp, tp, phase->C->eliminate_boxing())) { 1117 uint alias_idx = atp->index(); 1118 Node* result = nullptr; 1119 Node* current = st; 1120 // Skip through chains of MemBarNodes checking the MergeMems for 1121 // new states for the slice of this load. Stop once any other 1122 // kind of node is encountered. Loads from final memory can skip 1123 // through any kind of MemBar but normal loads shouldn't skip 1124 // through MemBarAcquire since the could allow them to move out of 1125 // a synchronized region. It is not safe to step over MemBarCPUOrder, 1126 // because alias info above them may be inaccurate (e.g., due to 1127 // mixed/mismatched unsafe accesses). 1128 bool is_final_mem = !atp->is_rewritable(); 1129 while (current->is_Proj()) { 1130 int opc = current->in(0)->Opcode(); 1131 if ((is_final_mem && (opc == Op_MemBarAcquire || 1132 opc == Op_MemBarAcquireLock || 1133 opc == Op_LoadFence)) || 1134 opc == Op_MemBarRelease || 1135 opc == Op_StoreFence || 1136 opc == Op_MemBarReleaseLock || 1137 opc == Op_MemBarStoreStore || 1138 opc == Op_StoreStoreFence) { 1139 Node* mem = current->in(0)->in(TypeFunc::Memory); 1140 if (mem->is_MergeMem()) { 1141 MergeMemNode* merge = mem->as_MergeMem(); 1142 Node* new_st = merge->memory_at(alias_idx); 1143 if (new_st == merge->base_memory()) { 1144 // Keep searching 1145 current = new_st; 1146 continue; 1147 } 1148 // Save the new memory state for the slice and fall through 1149 // to exit. 1150 result = new_st; 1151 } 1152 } 1153 break; 1154 } 1155 if (result != nullptr) { 1156 st = result; 1157 } 1158 } 1159 1160 // Loop around twice in the case Load -> Initialize -> Store. 1161 // (See PhaseIterGVN::add_users_to_worklist, which knows about this case.) 1162 for (int trip = 0; trip <= 1; trip++) { 1163 1164 if (st->is_Store()) { 1165 Node* st_adr = st->in(MemNode::Address); 1166 if (st_adr != ld_adr) { 1167 // Try harder before giving up. Unify base pointers with casts (e.g., raw/non-raw pointers). 1168 intptr_t st_off = 0; 1169 Node* st_base = AddPNode::Ideal_base_and_offset(st_adr, phase, st_off); 1170 if (ld_base == nullptr) return nullptr; 1171 if (st_base == nullptr) return nullptr; 1172 if (!ld_base->eqv_uncast(st_base, /*keep_deps=*/true)) return nullptr; 1173 if (ld_off != st_off) return nullptr; 1174 if (ld_off == Type::OffsetBot) return nullptr; 1175 // Same base, same offset. 1176 // Possible improvement for arrays: check index value instead of absolute offset. 1177 1178 // At this point we have proven something like this setup: 1179 // B = << base >> 1180 // L = LoadQ(AddP(Check/CastPP(B), #Off)) 1181 // S = StoreQ(AddP( B , #Off), V) 1182 // (Actually, we haven't yet proven the Q's are the same.) 1183 // In other words, we are loading from a casted version of 1184 // the same pointer-and-offset that we stored to. 1185 // Casted version may carry a dependency and it is respected. 1186 // Thus, we are able to replace L by V. 1187 } 1188 // Now prove that we have a LoadQ matched to a StoreQ, for some Q. 1189 if (store_Opcode() != st->Opcode()) { 1190 return nullptr; 1191 } 1192 // LoadVector/StoreVector needs additional check to ensure the types match. 1193 if (st->is_StoreVector()) { 1194 const TypeVect* in_vt = st->as_StoreVector()->vect_type(); 1195 const TypeVect* out_vt = is_Load() ? as_LoadVector()->vect_type() : as_StoreVector()->vect_type(); 1196 if (in_vt != out_vt) { 1197 return nullptr; 1198 } 1199 } 1200 return st->in(MemNode::ValueIn); 1201 } 1202 1203 // A load from a freshly-created object always returns zero. 1204 // (This can happen after LoadNode::Ideal resets the load's memory input 1205 // to find_captured_store, which returned InitializeNode::zero_memory.) 1206 if (st->is_Proj() && st->in(0)->is_Allocate() && 1207 (st->in(0) == ld_alloc) && 1208 (ld_off >= st->in(0)->as_Allocate()->minimum_header_size())) { 1209 // return a zero value for the load's basic type 1210 // (This is one of the few places where a generic PhaseTransform 1211 // can create new nodes. Think of it as lazily manifesting 1212 // virtually pre-existing constants.) 1213 Node* default_value = ld_alloc->in(AllocateNode::DefaultValue); 1214 if (default_value != nullptr) { 1215 return default_value; 1216 } 1217 assert(ld_alloc->in(AllocateNode::RawDefaultValue) == nullptr, "default value may not be null"); 1218 if (memory_type() != T_VOID) { 1219 if (ReduceBulkZeroing || find_array_copy_clone(ld_alloc, in(MemNode::Memory)) == nullptr) { 1220 // If ReduceBulkZeroing is disabled, we need to check if the allocation does not belong to an 1221 // ArrayCopyNode clone. If it does, then we cannot assume zero since the initialization is done 1222 // by the ArrayCopyNode. 1223 return phase->zerocon(memory_type()); 1224 } 1225 } else { 1226 // TODO: materialize all-zero vector constant 1227 assert(!isa_Load() || as_Load()->type()->isa_vect(), ""); 1228 } 1229 } 1230 1231 // A load from an initialization barrier can match a captured store. 1232 if (st->is_Proj() && st->in(0)->is_Initialize()) { 1233 InitializeNode* init = st->in(0)->as_Initialize(); 1234 AllocateNode* alloc = init->allocation(); 1235 if ((alloc != nullptr) && (alloc == ld_alloc)) { 1236 // examine a captured store value 1237 st = init->find_captured_store(ld_off, memory_size(), phase); 1238 if (st != nullptr) { 1239 continue; // take one more trip around 1240 } 1241 } 1242 } 1243 1244 // Load boxed value from result of valueOf() call is input parameter. 1245 if (this->is_Load() && ld_adr->is_AddP() && 1246 (tp != nullptr) && tp->is_ptr_to_boxed_value()) { 1247 intptr_t ignore = 0; 1248 Node* base = AddPNode::Ideal_base_and_offset(ld_adr, phase, ignore); 1249 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2(); 1250 base = bs->step_over_gc_barrier(base); 1251 if (base != nullptr && base->is_Proj() && 1252 base->as_Proj()->_con == TypeFunc::Parms && 1253 base->in(0)->is_CallStaticJava() && 1254 base->in(0)->as_CallStaticJava()->is_boxing_method()) { 1255 return base->in(0)->in(TypeFunc::Parms); 1256 } 1257 } 1258 1259 break; 1260 } 1261 1262 return nullptr; 1263 } 1264 1265 //----------------------is_instance_field_load_with_local_phi------------------ 1266 bool LoadNode::is_instance_field_load_with_local_phi(Node* ctrl) { 1267 if( in(Memory)->is_Phi() && in(Memory)->in(0) == ctrl && 1268 in(Address)->is_AddP() ) { 1269 const TypeOopPtr* t_oop = in(Address)->bottom_type()->isa_oopptr(); 1270 // Only instances and boxed values. 1271 if( t_oop != nullptr && 1272 (t_oop->is_ptr_to_boxed_value() || 1273 t_oop->is_known_instance_field()) && 1274 t_oop->offset() != Type::OffsetBot && 1275 t_oop->offset() != Type::OffsetTop) { 1276 return true; 1277 } 1278 } 1279 return false; 1280 } 1281 1282 //------------------------------Identity--------------------------------------- 1283 // Loads are identity if previous store is to same address 1284 Node* LoadNode::Identity(PhaseGVN* phase) { 1285 // Loading from an InlineType? The InlineType has the values of 1286 // all fields as input. Look for the field with matching offset. 1287 Node* addr = in(Address); 1288 intptr_t offset; 1289 Node* base = AddPNode::Ideal_base_and_offset(addr, phase, offset); 1290 if (base != nullptr && base->is_InlineType() && offset > oopDesc::klass_offset_in_bytes()) { 1291 Node* value = base->as_InlineType()->field_value_by_offset((int)offset, true); 1292 if (value != nullptr) { 1293 if (Opcode() == Op_LoadN) { 1294 // Encode oop value if we are loading a narrow oop 1295 assert(!phase->type(value)->isa_narrowoop(), "should already be decoded"); 1296 value = phase->transform(new EncodePNode(value, bottom_type())); 1297 } 1298 return value; 1299 } 1300 } 1301 1302 // If the previous store-maker is the right kind of Store, and the store is 1303 // to the same address, then we are equal to the value stored. 1304 Node* mem = in(Memory); 1305 Node* value = can_see_stored_value(mem, phase); 1306 if( value ) { 1307 // byte, short & char stores truncate naturally. 1308 // A load has to load the truncated value which requires 1309 // some sort of masking operation and that requires an 1310 // Ideal call instead of an Identity call. 1311 if (memory_size() < BytesPerInt) { 1312 // If the input to the store does not fit with the load's result type, 1313 // it must be truncated via an Ideal call. 1314 if (!phase->type(value)->higher_equal(phase->type(this))) 1315 return this; 1316 } 1317 // (This works even when value is a Con, but LoadNode::Value 1318 // usually runs first, producing the singleton type of the Con.) 1319 if (!has_pinned_control_dependency() || value->is_Con()) { 1320 return value; 1321 } else { 1322 return this; 1323 } 1324 } 1325 1326 if (has_pinned_control_dependency()) { 1327 return this; 1328 } 1329 // Search for an existing data phi which was generated before for the same 1330 // instance's field to avoid infinite generation of phis in a loop. 1331 Node *region = mem->in(0); 1332 if (is_instance_field_load_with_local_phi(region)) { 1333 const TypeOopPtr *addr_t = in(Address)->bottom_type()->isa_oopptr(); 1334 int this_index = phase->C->get_alias_index(addr_t); 1335 int this_offset = addr_t->offset(); 1336 int this_iid = addr_t->instance_id(); 1337 if (!addr_t->is_known_instance() && 1338 addr_t->is_ptr_to_boxed_value()) { 1339 // Use _idx of address base (could be Phi node) for boxed values. 1340 intptr_t ignore = 0; 1341 Node* base = AddPNode::Ideal_base_and_offset(in(Address), phase, ignore); 1342 if (base == nullptr) { 1343 return this; 1344 } 1345 this_iid = base->_idx; 1346 } 1347 const Type* this_type = bottom_type(); 1348 for (DUIterator_Fast imax, i = region->fast_outs(imax); i < imax; i++) { 1349 Node* phi = region->fast_out(i); 1350 if (phi->is_Phi() && phi != mem && 1351 phi->as_Phi()->is_same_inst_field(this_type, (int)mem->_idx, this_iid, this_index, this_offset)) { 1352 return phi; 1353 } 1354 } 1355 } 1356 1357 return this; 1358 } 1359 1360 // Construct an equivalent unsigned load. 1361 Node* LoadNode::convert_to_unsigned_load(PhaseGVN& gvn) { 1362 BasicType bt = T_ILLEGAL; 1363 const Type* rt = nullptr; 1364 switch (Opcode()) { 1365 case Op_LoadUB: return this; 1366 case Op_LoadUS: return this; 1367 case Op_LoadB: bt = T_BOOLEAN; rt = TypeInt::UBYTE; break; 1368 case Op_LoadS: bt = T_CHAR; rt = TypeInt::CHAR; break; 1369 default: 1370 assert(false, "no unsigned variant: %s", Name()); 1371 return nullptr; 1372 } 1373 return LoadNode::make(gvn, in(MemNode::Control), in(MemNode::Memory), in(MemNode::Address), 1374 raw_adr_type(), rt, bt, _mo, _control_dependency, 1375 false /*require_atomic_access*/, is_unaligned_access(), is_mismatched_access()); 1376 } 1377 1378 // Construct an equivalent signed load. 1379 Node* LoadNode::convert_to_signed_load(PhaseGVN& gvn) { 1380 BasicType bt = T_ILLEGAL; 1381 const Type* rt = nullptr; 1382 switch (Opcode()) { 1383 case Op_LoadUB: bt = T_BYTE; rt = TypeInt::BYTE; break; 1384 case Op_LoadUS: bt = T_SHORT; rt = TypeInt::SHORT; break; 1385 case Op_LoadB: // fall through 1386 case Op_LoadS: // fall through 1387 case Op_LoadI: // fall through 1388 case Op_LoadL: return this; 1389 default: 1390 assert(false, "no signed variant: %s", Name()); 1391 return nullptr; 1392 } 1393 return LoadNode::make(gvn, in(MemNode::Control), in(MemNode::Memory), in(MemNode::Address), 1394 raw_adr_type(), rt, bt, _mo, _control_dependency, 1395 false /*require_atomic_access*/, is_unaligned_access(), is_mismatched_access()); 1396 } 1397 1398 bool LoadNode::has_reinterpret_variant(const Type* rt) { 1399 BasicType bt = rt->basic_type(); 1400 switch (Opcode()) { 1401 case Op_LoadI: return (bt == T_FLOAT); 1402 case Op_LoadL: return (bt == T_DOUBLE); 1403 case Op_LoadF: return (bt == T_INT); 1404 case Op_LoadD: return (bt == T_LONG); 1405 1406 default: return false; 1407 } 1408 } 1409 1410 Node* LoadNode::convert_to_reinterpret_load(PhaseGVN& gvn, const Type* rt) { 1411 BasicType bt = rt->basic_type(); 1412 assert(has_reinterpret_variant(rt), "no reinterpret variant: %s %s", Name(), type2name(bt)); 1413 bool is_mismatched = is_mismatched_access(); 1414 const TypeRawPtr* raw_type = gvn.type(in(MemNode::Memory))->isa_rawptr(); 1415 if (raw_type == nullptr) { 1416 is_mismatched = true; // conservatively match all non-raw accesses as mismatched 1417 } 1418 const int op = Opcode(); 1419 bool require_atomic_access = (op == Op_LoadL && ((LoadLNode*)this)->require_atomic_access()) || 1420 (op == Op_LoadD && ((LoadDNode*)this)->require_atomic_access()); 1421 return LoadNode::make(gvn, in(MemNode::Control), in(MemNode::Memory), in(MemNode::Address), 1422 raw_adr_type(), rt, bt, _mo, _control_dependency, 1423 require_atomic_access, is_unaligned_access(), is_mismatched); 1424 } 1425 1426 bool StoreNode::has_reinterpret_variant(const Type* vt) { 1427 BasicType bt = vt->basic_type(); 1428 switch (Opcode()) { 1429 case Op_StoreI: return (bt == T_FLOAT); 1430 case Op_StoreL: return (bt == T_DOUBLE); 1431 case Op_StoreF: return (bt == T_INT); 1432 case Op_StoreD: return (bt == T_LONG); 1433 1434 default: return false; 1435 } 1436 } 1437 1438 Node* StoreNode::convert_to_reinterpret_store(PhaseGVN& gvn, Node* val, const Type* vt) { 1439 BasicType bt = vt->basic_type(); 1440 assert(has_reinterpret_variant(vt), "no reinterpret variant: %s %s", Name(), type2name(bt)); 1441 const int op = Opcode(); 1442 bool require_atomic_access = (op == Op_StoreL && ((StoreLNode*)this)->require_atomic_access()) || 1443 (op == Op_StoreD && ((StoreDNode*)this)->require_atomic_access()); 1444 StoreNode* st = StoreNode::make(gvn, in(MemNode::Control), in(MemNode::Memory), in(MemNode::Address), 1445 raw_adr_type(), val, bt, _mo, require_atomic_access); 1446 1447 bool is_mismatched = is_mismatched_access(); 1448 const TypeRawPtr* raw_type = gvn.type(in(MemNode::Memory))->isa_rawptr(); 1449 if (raw_type == nullptr) { 1450 is_mismatched = true; // conservatively match all non-raw accesses as mismatched 1451 } 1452 if (is_mismatched) { 1453 st->set_mismatched_access(); 1454 } 1455 return st; 1456 } 1457 1458 // We're loading from an object which has autobox behaviour. 1459 // If this object is result of a valueOf call we'll have a phi 1460 // merging a newly allocated object and a load from the cache. 1461 // We want to replace this load with the original incoming 1462 // argument to the valueOf call. 1463 Node* LoadNode::eliminate_autobox(PhaseIterGVN* igvn) { 1464 assert(igvn->C->eliminate_boxing(), "sanity"); 1465 intptr_t ignore = 0; 1466 Node* base = AddPNode::Ideal_base_and_offset(in(Address), igvn, ignore); 1467 if ((base == nullptr) || base->is_Phi()) { 1468 // Push the loads from the phi that comes from valueOf up 1469 // through it to allow elimination of the loads and the recovery 1470 // of the original value. It is done in split_through_phi(). 1471 return nullptr; 1472 } else if (base->is_Load() || 1473 (base->is_DecodeN() && base->in(1)->is_Load())) { 1474 // Eliminate the load of boxed value for integer types from the cache 1475 // array by deriving the value from the index into the array. 1476 // Capture the offset of the load and then reverse the computation. 1477 1478 // Get LoadN node which loads a boxing object from 'cache' array. 1479 if (base->is_DecodeN()) { 1480 base = base->in(1); 1481 } 1482 if (!base->in(Address)->is_AddP()) { 1483 return nullptr; // Complex address 1484 } 1485 AddPNode* address = base->in(Address)->as_AddP(); 1486 Node* cache_base = address->in(AddPNode::Base); 1487 if ((cache_base != nullptr) && cache_base->is_DecodeN()) { 1488 // Get ConP node which is static 'cache' field. 1489 cache_base = cache_base->in(1); 1490 } 1491 if ((cache_base != nullptr) && cache_base->is_Con()) { 1492 const TypeAryPtr* base_type = cache_base->bottom_type()->isa_aryptr(); 1493 if ((base_type != nullptr) && base_type->is_autobox_cache()) { 1494 Node* elements[4]; 1495 int shift = exact_log2(type2aelembytes(T_OBJECT)); 1496 int count = address->unpack_offsets(elements, ARRAY_SIZE(elements)); 1497 if (count > 0 && elements[0]->is_Con() && 1498 (count == 1 || 1499 (count == 2 && elements[1]->Opcode() == Op_LShiftX && 1500 elements[1]->in(2) == igvn->intcon(shift)))) { 1501 ciObjArray* array = base_type->const_oop()->as_obj_array(); 1502 // Fetch the box object cache[0] at the base of the array and get its value 1503 ciInstance* box = array->obj_at(0)->as_instance(); 1504 ciInstanceKlass* ik = box->klass()->as_instance_klass(); 1505 assert(ik->is_box_klass(), "sanity"); 1506 assert(ik->nof_nonstatic_fields() == 1, "change following code"); 1507 if (ik->nof_nonstatic_fields() == 1) { 1508 // This should be true nonstatic_field_at requires calling 1509 // nof_nonstatic_fields so check it anyway 1510 ciConstant c = box->field_value(ik->nonstatic_field_at(0)); 1511 BasicType bt = c.basic_type(); 1512 // Only integer types have boxing cache. 1513 assert(bt == T_BOOLEAN || bt == T_CHAR || 1514 bt == T_BYTE || bt == T_SHORT || 1515 bt == T_INT || bt == T_LONG, "wrong type = %s", type2name(bt)); 1516 jlong cache_low = (bt == T_LONG) ? c.as_long() : c.as_int(); 1517 if (cache_low != (int)cache_low) { 1518 return nullptr; // should not happen since cache is array indexed by value 1519 } 1520 jlong offset = arrayOopDesc::base_offset_in_bytes(T_OBJECT) - (cache_low << shift); 1521 if (offset != (int)offset) { 1522 return nullptr; // should not happen since cache is array indexed by value 1523 } 1524 // Add up all the offsets making of the address of the load 1525 Node* result = elements[0]; 1526 for (int i = 1; i < count; i++) { 1527 result = igvn->transform(new AddXNode(result, elements[i])); 1528 } 1529 // Remove the constant offset from the address and then 1530 result = igvn->transform(new AddXNode(result, igvn->MakeConX(-(int)offset))); 1531 // remove the scaling of the offset to recover the original index. 1532 if (result->Opcode() == Op_LShiftX && result->in(2) == igvn->intcon(shift)) { 1533 // Peel the shift off directly but wrap it in a dummy node 1534 // since Ideal can't return existing nodes 1535 igvn->_worklist.push(result); // remove dead node later 1536 result = new RShiftXNode(result->in(1), igvn->intcon(0)); 1537 } else if (result->is_Add() && result->in(2)->is_Con() && 1538 result->in(1)->Opcode() == Op_LShiftX && 1539 result->in(1)->in(2) == igvn->intcon(shift)) { 1540 // We can't do general optimization: ((X<<Z) + Y) >> Z ==> X + (Y>>Z) 1541 // but for boxing cache access we know that X<<Z will not overflow 1542 // (there is range check) so we do this optimizatrion by hand here. 1543 igvn->_worklist.push(result); // remove dead node later 1544 Node* add_con = new RShiftXNode(result->in(2), igvn->intcon(shift)); 1545 result = new AddXNode(result->in(1)->in(1), igvn->transform(add_con)); 1546 } else { 1547 result = new RShiftXNode(result, igvn->intcon(shift)); 1548 } 1549 #ifdef _LP64 1550 if (bt != T_LONG) { 1551 result = new ConvL2INode(igvn->transform(result)); 1552 } 1553 #else 1554 if (bt == T_LONG) { 1555 result = new ConvI2LNode(igvn->transform(result)); 1556 } 1557 #endif 1558 // Boxing/unboxing can be done from signed & unsigned loads (e.g. LoadUB -> ... -> LoadB pair). 1559 // Need to preserve unboxing load type if it is unsigned. 1560 switch(this->Opcode()) { 1561 case Op_LoadUB: 1562 result = new AndINode(igvn->transform(result), igvn->intcon(0xFF)); 1563 break; 1564 case Op_LoadUS: 1565 result = new AndINode(igvn->transform(result), igvn->intcon(0xFFFF)); 1566 break; 1567 } 1568 return result; 1569 } 1570 } 1571 } 1572 } 1573 } 1574 return nullptr; 1575 } 1576 1577 static bool stable_phi(PhiNode* phi, PhaseGVN *phase) { 1578 Node* region = phi->in(0); 1579 if (region == nullptr) { 1580 return false; // Wait stable graph 1581 } 1582 uint cnt = phi->req(); 1583 for (uint i = 1; i < cnt; i++) { 1584 Node* rc = region->in(i); 1585 if (rc == nullptr || phase->type(rc) == Type::TOP) 1586 return false; // Wait stable graph 1587 Node* in = phi->in(i); 1588 if (in == nullptr || phase->type(in) == Type::TOP) 1589 return false; // Wait stable graph 1590 } 1591 return true; 1592 } 1593 1594 //------------------------------split_through_phi------------------------------ 1595 // Check whether a call to 'split_through_phi' would split this load through the 1596 // Phi *base*. This method is essentially a copy of the validations performed 1597 // by 'split_through_phi'. The first use of this method was in EA code as part 1598 // of simplification of allocation merges. 1599 // Some differences from original method (split_through_phi): 1600 // - If base->is_CastPP(): base = base->in(1) 1601 bool LoadNode::can_split_through_phi_base(PhaseGVN* phase) { 1602 Node* mem = in(Memory); 1603 Node* address = in(Address); 1604 intptr_t ignore = 0; 1605 Node* base = AddPNode::Ideal_base_and_offset(address, phase, ignore); 1606 1607 if (base->is_CastPP()) { 1608 base = base->in(1); 1609 } 1610 1611 if (req() > 3 || base == nullptr || !base->is_Phi()) { 1612 return false; 1613 } 1614 1615 if (!mem->is_Phi()) { 1616 if (!MemNode::all_controls_dominate(mem, base->in(0))) { 1617 return false; 1618 } 1619 } else if (base->in(0) != mem->in(0)) { 1620 if (!MemNode::all_controls_dominate(mem, base->in(0))) { 1621 return false; 1622 } 1623 } 1624 1625 return true; 1626 } 1627 1628 //------------------------------split_through_phi------------------------------ 1629 // Split instance or boxed field load through Phi. 1630 Node* LoadNode::split_through_phi(PhaseGVN* phase, bool ignore_missing_instance_id) { 1631 if (req() > 3) { 1632 assert(is_LoadVector() && Opcode() != Op_LoadVector, "load has too many inputs"); 1633 // LoadVector subclasses such as LoadVectorMasked have extra inputs that the logic below doesn't take into account 1634 return nullptr; 1635 } 1636 Node* mem = in(Memory); 1637 Node* address = in(Address); 1638 const TypeOopPtr *t_oop = phase->type(address)->isa_oopptr(); 1639 1640 assert((t_oop != nullptr) && 1641 (ignore_missing_instance_id || 1642 t_oop->is_known_instance_field() || 1643 t_oop->is_ptr_to_boxed_value()), "invalid conditions"); 1644 1645 Compile* C = phase->C; 1646 intptr_t ignore = 0; 1647 Node* base = AddPNode::Ideal_base_and_offset(address, phase, ignore); 1648 bool base_is_phi = (base != nullptr) && base->is_Phi(); 1649 bool load_boxed_values = t_oop->is_ptr_to_boxed_value() && C->aggressive_unboxing() && 1650 (base != nullptr) && (base == address->in(AddPNode::Base)) && 1651 phase->type(base)->higher_equal(TypePtr::NOTNULL); 1652 1653 if (!((mem->is_Phi() || base_is_phi) && 1654 (ignore_missing_instance_id || load_boxed_values || t_oop->is_known_instance_field()))) { 1655 return nullptr; // Neither memory or base are Phi 1656 } 1657 1658 if (mem->is_Phi()) { 1659 if (!stable_phi(mem->as_Phi(), phase)) { 1660 return nullptr; // Wait stable graph 1661 } 1662 uint cnt = mem->req(); 1663 // Check for loop invariant memory. 1664 if (cnt == 3) { 1665 for (uint i = 1; i < cnt; i++) { 1666 Node* in = mem->in(i); 1667 Node* m = optimize_memory_chain(in, t_oop, this, phase); 1668 if (m == mem) { 1669 if (i == 1) { 1670 // if the first edge was a loop, check second edge too. 1671 // If both are replaceable - we are in an infinite loop 1672 Node *n = optimize_memory_chain(mem->in(2), t_oop, this, phase); 1673 if (n == mem) { 1674 break; 1675 } 1676 } 1677 set_req(Memory, mem->in(cnt - i)); 1678 return this; // made change 1679 } 1680 } 1681 } 1682 } 1683 if (base_is_phi) { 1684 if (!stable_phi(base->as_Phi(), phase)) { 1685 return nullptr; // Wait stable graph 1686 } 1687 uint cnt = base->req(); 1688 // Check for loop invariant memory. 1689 if (cnt == 3) { 1690 for (uint i = 1; i < cnt; i++) { 1691 if (base->in(i) == base) { 1692 return nullptr; // Wait stable graph 1693 } 1694 } 1695 } 1696 } 1697 1698 // Split through Phi (see original code in loopopts.cpp). 1699 assert(ignore_missing_instance_id || C->have_alias_type(t_oop), "instance should have alias type"); 1700 1701 // Do nothing here if Identity will find a value 1702 // (to avoid infinite chain of value phis generation). 1703 if (this != Identity(phase)) { 1704 return nullptr; 1705 } 1706 1707 // Select Region to split through. 1708 Node* region; 1709 DomResult dom_result = DomResult::Dominate; 1710 if (!base_is_phi) { 1711 assert(mem->is_Phi(), "sanity"); 1712 region = mem->in(0); 1713 // Skip if the region dominates some control edge of the address. 1714 // We will check `dom_result` later. 1715 dom_result = MemNode::maybe_all_controls_dominate(address, region); 1716 } else if (!mem->is_Phi()) { 1717 assert(base_is_phi, "sanity"); 1718 region = base->in(0); 1719 // Skip if the region dominates some control edge of the memory. 1720 // We will check `dom_result` later. 1721 dom_result = MemNode::maybe_all_controls_dominate(mem, region); 1722 } else if (base->in(0) != mem->in(0)) { 1723 assert(base_is_phi && mem->is_Phi(), "sanity"); 1724 dom_result = MemNode::maybe_all_controls_dominate(mem, base->in(0)); 1725 if (dom_result == DomResult::Dominate) { 1726 region = base->in(0); 1727 } else { 1728 dom_result = MemNode::maybe_all_controls_dominate(address, mem->in(0)); 1729 if (dom_result == DomResult::Dominate) { 1730 region = mem->in(0); 1731 } 1732 // Otherwise we encountered a complex graph. 1733 } 1734 } else { 1735 assert(base->in(0) == mem->in(0), "sanity"); 1736 region = mem->in(0); 1737 } 1738 1739 PhaseIterGVN* igvn = phase->is_IterGVN(); 1740 if (dom_result != DomResult::Dominate) { 1741 if (dom_result == DomResult::EncounteredDeadCode) { 1742 // There is some dead code which eventually will be removed in IGVN. 1743 // Once this is the case, we get an unambiguous dominance result. 1744 // Push the node to the worklist again until the dead code is removed. 1745 igvn->_worklist.push(this); 1746 } 1747 return nullptr; 1748 } 1749 1750 Node* phi = nullptr; 1751 const Type* this_type = this->bottom_type(); 1752 if (t_oop != nullptr && (t_oop->is_known_instance_field() || load_boxed_values)) { 1753 int this_index = C->get_alias_index(t_oop); 1754 int this_offset = t_oop->offset(); 1755 int this_iid = t_oop->is_known_instance_field() ? t_oop->instance_id() : base->_idx; 1756 phi = new PhiNode(region, this_type, nullptr, mem->_idx, this_iid, this_index, this_offset); 1757 } else if (ignore_missing_instance_id) { 1758 phi = new PhiNode(region, this_type, nullptr, mem->_idx); 1759 } else { 1760 return nullptr; 1761 } 1762 1763 for (uint i = 1; i < region->req(); i++) { 1764 Node* x; 1765 Node* the_clone = nullptr; 1766 Node* in = region->in(i); 1767 if (region->is_CountedLoop() && region->as_Loop()->is_strip_mined() && i == LoopNode::EntryControl && 1768 in != nullptr && in->is_OuterStripMinedLoop()) { 1769 // No node should go in the outer strip mined loop 1770 in = in->in(LoopNode::EntryControl); 1771 } 1772 if (in == nullptr || in == C->top()) { 1773 x = C->top(); // Dead path? Use a dead data op 1774 } else { 1775 x = this->clone(); // Else clone up the data op 1776 the_clone = x; // Remember for possible deletion. 1777 // Alter data node to use pre-phi inputs 1778 if (this->in(0) == region) { 1779 x->set_req(0, in); 1780 } else { 1781 x->set_req(0, nullptr); 1782 } 1783 if (mem->is_Phi() && (mem->in(0) == region)) { 1784 x->set_req(Memory, mem->in(i)); // Use pre-Phi input for the clone. 1785 } 1786 if (address->is_Phi() && address->in(0) == region) { 1787 x->set_req(Address, address->in(i)); // Use pre-Phi input for the clone 1788 } 1789 if (base_is_phi && (base->in(0) == region)) { 1790 Node* base_x = base->in(i); // Clone address for loads from boxed objects. 1791 Node* adr_x = phase->transform(new AddPNode(base_x,base_x,address->in(AddPNode::Offset))); 1792 x->set_req(Address, adr_x); 1793 } 1794 } 1795 // Check for a 'win' on some paths 1796 const Type *t = x->Value(igvn); 1797 1798 bool singleton = t->singleton(); 1799 1800 // See comments in PhaseIdealLoop::split_thru_phi(). 1801 if (singleton && t == Type::TOP) { 1802 singleton &= region->is_Loop() && (i != LoopNode::EntryControl); 1803 } 1804 1805 if (singleton) { 1806 x = igvn->makecon(t); 1807 } else { 1808 // We now call Identity to try to simplify the cloned node. 1809 // Note that some Identity methods call phase->type(this). 1810 // Make sure that the type array is big enough for 1811 // our new node, even though we may throw the node away. 1812 // (This tweaking with igvn only works because x is a new node.) 1813 igvn->set_type(x, t); 1814 // If x is a TypeNode, capture any more-precise type permanently into Node 1815 // otherwise it will be not updated during igvn->transform since 1816 // igvn->type(x) is set to x->Value() already. 1817 x->raise_bottom_type(t); 1818 Node* y = x->Identity(igvn); 1819 if (y != x) { 1820 x = y; 1821 } else { 1822 y = igvn->hash_find_insert(x); 1823 if (y) { 1824 x = y; 1825 } else { 1826 // Else x is a new node we are keeping 1827 // We do not need register_new_node_with_optimizer 1828 // because set_type has already been called. 1829 igvn->_worklist.push(x); 1830 } 1831 } 1832 } 1833 if (x != the_clone && the_clone != nullptr) { 1834 igvn->remove_dead_node(the_clone); 1835 } 1836 phi->set_req(i, x); 1837 } 1838 // Record Phi 1839 igvn->register_new_node_with_optimizer(phi); 1840 return phi; 1841 } 1842 1843 AllocateNode* LoadNode::is_new_object_mark_load() const { 1844 if (Opcode() == Op_LoadX) { 1845 Node* address = in(MemNode::Address); 1846 AllocateNode* alloc = AllocateNode::Ideal_allocation(address); 1847 Node* mem = in(MemNode::Memory); 1848 if (alloc != nullptr && mem->is_Proj() && 1849 mem->in(0) != nullptr && 1850 mem->in(0) == alloc->initialization() && 1851 alloc->initialization()->proj_out_or_null(0) != nullptr) { 1852 return alloc; 1853 } 1854 } 1855 return nullptr; 1856 } 1857 1858 1859 //------------------------------Ideal------------------------------------------ 1860 // If the load is from Field memory and the pointer is non-null, it might be possible to 1861 // zero out the control input. 1862 // If the offset is constant and the base is an object allocation, 1863 // try to hook me up to the exact initializing store. 1864 Node *LoadNode::Ideal(PhaseGVN *phase, bool can_reshape) { 1865 if (has_pinned_control_dependency()) { 1866 return nullptr; 1867 } 1868 Node* p = MemNode::Ideal_common(phase, can_reshape); 1869 if (p) return (p == NodeSentinel) ? nullptr : p; 1870 1871 Node* ctrl = in(MemNode::Control); 1872 Node* address = in(MemNode::Address); 1873 bool progress = false; 1874 1875 bool addr_mark = ((phase->type(address)->isa_oopptr() || phase->type(address)->isa_narrowoop()) && 1876 phase->type(address)->is_ptr()->offset() == oopDesc::mark_offset_in_bytes()); 1877 1878 // Skip up past a SafePoint control. Cannot do this for Stores because 1879 // pointer stores & cardmarks must stay on the same side of a SafePoint. 1880 if( ctrl != nullptr && ctrl->Opcode() == Op_SafePoint && 1881 phase->C->get_alias_index(phase->type(address)->is_ptr()) != Compile::AliasIdxRaw && 1882 !addr_mark && 1883 (depends_only_on_test() || has_unknown_control_dependency())) { 1884 ctrl = ctrl->in(0); 1885 set_req(MemNode::Control,ctrl); 1886 progress = true; 1887 } 1888 1889 intptr_t ignore = 0; 1890 Node* base = AddPNode::Ideal_base_and_offset(address, phase, ignore); 1891 if (base != nullptr 1892 && phase->C->get_alias_index(phase->type(address)->is_ptr()) != Compile::AliasIdxRaw) { 1893 // Check for useless control edge in some common special cases 1894 if (in(MemNode::Control) != nullptr 1895 && can_remove_control() 1896 && phase->type(base)->higher_equal(TypePtr::NOTNULL) 1897 && all_controls_dominate(base, phase->C->start())) { 1898 // A method-invariant, non-null address (constant or 'this' argument). 1899 set_req(MemNode::Control, nullptr); 1900 progress = true; 1901 } 1902 } 1903 1904 Node* mem = in(MemNode::Memory); 1905 const TypePtr *addr_t = phase->type(address)->isa_ptr(); 1906 1907 if (can_reshape && (addr_t != nullptr)) { 1908 // try to optimize our memory input 1909 Node* opt_mem = MemNode::optimize_memory_chain(mem, addr_t, this, phase); 1910 if (opt_mem != mem) { 1911 set_req_X(MemNode::Memory, opt_mem, phase); 1912 if (phase->type( opt_mem ) == Type::TOP) return nullptr; 1913 return this; 1914 } 1915 const TypeOopPtr *t_oop = addr_t->isa_oopptr(); 1916 if ((t_oop != nullptr) && 1917 (t_oop->is_known_instance_field() || 1918 t_oop->is_ptr_to_boxed_value())) { 1919 PhaseIterGVN *igvn = phase->is_IterGVN(); 1920 assert(igvn != nullptr, "must be PhaseIterGVN when can_reshape is true"); 1921 if (igvn->_worklist.member(opt_mem)) { 1922 // Delay this transformation until memory Phi is processed. 1923 igvn->_worklist.push(this); 1924 return nullptr; 1925 } 1926 // Split instance field load through Phi. 1927 Node* result = split_through_phi(phase); 1928 if (result != nullptr) return result; 1929 1930 if (t_oop->is_ptr_to_boxed_value()) { 1931 Node* result = eliminate_autobox(igvn); 1932 if (result != nullptr) return result; 1933 } 1934 } 1935 } 1936 1937 // Is there a dominating load that loads the same value? Leave 1938 // anything that is not a load of a field/array element (like 1939 // barriers etc.) alone 1940 if (in(0) != nullptr && !adr_type()->isa_rawptr() && can_reshape) { 1941 for (DUIterator_Fast imax, i = mem->fast_outs(imax); i < imax; i++) { 1942 Node *use = mem->fast_out(i); 1943 if (use != this && 1944 use->Opcode() == Opcode() && 1945 use->in(0) != nullptr && 1946 use->in(0) != in(0) && 1947 use->in(Address) == in(Address)) { 1948 Node* ctl = in(0); 1949 for (int i = 0; i < 10 && ctl != nullptr; i++) { 1950 ctl = IfNode::up_one_dom(ctl); 1951 if (ctl == use->in(0)) { 1952 set_req(0, use->in(0)); 1953 return this; 1954 } 1955 } 1956 } 1957 } 1958 } 1959 1960 // Check for prior store with a different base or offset; make Load 1961 // independent. Skip through any number of them. Bail out if the stores 1962 // are in an endless dead cycle and report no progress. This is a key 1963 // transform for Reflection. However, if after skipping through the Stores 1964 // we can't then fold up against a prior store do NOT do the transform as 1965 // this amounts to using the 'Oracle' model of aliasing. It leaves the same 1966 // array memory alive twice: once for the hoisted Load and again after the 1967 // bypassed Store. This situation only works if EVERYBODY who does 1968 // anti-dependence work knows how to bypass. I.e. we need all 1969 // anti-dependence checks to ask the same Oracle. Right now, that Oracle is 1970 // the alias index stuff. So instead, peek through Stores and IFF we can 1971 // fold up, do so. 1972 Node* prev_mem = find_previous_store(phase); 1973 if (prev_mem != nullptr) { 1974 Node* value = can_see_arraycopy_value(prev_mem, phase); 1975 if (value != nullptr) { 1976 return value; 1977 } 1978 } 1979 // Steps (a), (b): Walk past independent stores to find an exact match. 1980 if (prev_mem != nullptr && prev_mem != in(MemNode::Memory)) { 1981 // (c) See if we can fold up on the spot, but don't fold up here. 1982 // Fold-up might require truncation (for LoadB/LoadS/LoadUS) or 1983 // just return a prior value, which is done by Identity calls. 1984 if (can_see_stored_value(prev_mem, phase)) { 1985 // Make ready for step (d): 1986 set_req_X(MemNode::Memory, prev_mem, phase); 1987 return this; 1988 } 1989 } 1990 1991 return progress ? this : nullptr; 1992 } 1993 1994 // Helper to recognize certain Klass fields which are invariant across 1995 // some group of array types (e.g., int[] or all T[] where T < Object). 1996 const Type* 1997 LoadNode::load_array_final_field(const TypeKlassPtr *tkls, 1998 ciKlass* klass) const { 1999 if (tkls->offset() == in_bytes(Klass::modifier_flags_offset())) { 2000 // The field is Klass::_modifier_flags. Return its (constant) value. 2001 // (Folds up the 2nd indirection in aClassConstant.getModifiers().) 2002 assert(this->Opcode() == Op_LoadI, "must load an int from _modifier_flags"); 2003 return TypeInt::make(klass->modifier_flags()); 2004 } 2005 if (tkls->offset() == in_bytes(Klass::access_flags_offset())) { 2006 // The field is Klass::_access_flags. Return its (constant) value. 2007 // (Folds up the 2nd indirection in Reflection.getClassAccessFlags(aClassConstant).) 2008 assert(this->Opcode() == Op_LoadI, "must load an int from _access_flags"); 2009 return TypeInt::make(klass->access_flags()); 2010 } 2011 if (tkls->offset() == in_bytes(Klass::misc_flags_offset())) { 2012 // The field is Klass::_misc_flags. Return its (constant) value. 2013 // (Folds up the 2nd indirection in Reflection.getClassAccessFlags(aClassConstant).) 2014 assert(this->Opcode() == Op_LoadUB, "must load an unsigned byte from _misc_flags"); 2015 return TypeInt::make(klass->misc_flags()); 2016 } 2017 if (tkls->offset() == in_bytes(Klass::layout_helper_offset())) { 2018 // The field is Klass::_layout_helper. Return its constant value if known. 2019 assert(this->Opcode() == Op_LoadI, "must load an int from _layout_helper"); 2020 return TypeInt::make(klass->layout_helper()); 2021 } 2022 2023 // No match. 2024 return nullptr; 2025 } 2026 2027 //------------------------------Value----------------------------------------- 2028 const Type* LoadNode::Value(PhaseGVN* phase) const { 2029 // Either input is TOP ==> the result is TOP 2030 Node* mem = in(MemNode::Memory); 2031 const Type *t1 = phase->type(mem); 2032 if (t1 == Type::TOP) return Type::TOP; 2033 Node* adr = in(MemNode::Address); 2034 const TypePtr* tp = phase->type(adr)->isa_ptr(); 2035 if (tp == nullptr || tp->empty()) return Type::TOP; 2036 int off = tp->offset(); 2037 assert(off != Type::OffsetTop, "case covered by TypePtr::empty"); 2038 Compile* C = phase->C; 2039 2040 // Try to guess loaded type from pointer type 2041 if (tp->isa_aryptr()) { 2042 const TypeAryPtr* ary = tp->is_aryptr(); 2043 const Type* t = ary->elem(); 2044 2045 // Determine whether the reference is beyond the header or not, by comparing 2046 // the offset against the offset of the start of the array's data. 2047 // Different array types begin at slightly different offsets (12 vs. 16). 2048 // We choose T_BYTE as an example base type that is least restrictive 2049 // as to alignment, which will therefore produce the smallest 2050 // possible base offset. 2051 const int min_base_off = arrayOopDesc::base_offset_in_bytes(T_BYTE); 2052 const bool off_beyond_header = (off >= min_base_off); 2053 2054 // Try to constant-fold a stable array element. 2055 if (FoldStableValues && !is_mismatched_access() && ary->is_stable()) { 2056 // Make sure the reference is not into the header and the offset is constant 2057 ciObject* aobj = ary->const_oop(); 2058 if (aobj != nullptr && off_beyond_header && adr->is_AddP() && off != Type::OffsetBot) { 2059 int stable_dimension = (ary->stable_dimension() > 0 ? ary->stable_dimension() - 1 : 0); 2060 const Type* con_type = Type::make_constant_from_array_element(aobj->as_array(), off, 2061 stable_dimension, 2062 memory_type(), is_unsigned()); 2063 if (con_type != nullptr) { 2064 return con_type; 2065 } 2066 } 2067 } 2068 2069 // Don't do this for integer types. There is only potential profit if 2070 // the element type t is lower than _type; that is, for int types, if _type is 2071 // more restrictive than t. This only happens here if one is short and the other 2072 // char (both 16 bits), and in those cases we've made an intentional decision 2073 // to use one kind of load over the other. See AndINode::Ideal and 4965907. 2074 // Also, do not try to narrow the type for a LoadKlass, regardless of offset. 2075 // 2076 // Yes, it is possible to encounter an expression like (LoadKlass p1:(AddP x x 8)) 2077 // where the _gvn.type of the AddP is wider than 8. This occurs when an earlier 2078 // copy p0 of (AddP x x 8) has been proven equal to p1, and the p0 has been 2079 // subsumed by p1. If p1 is on the worklist but has not yet been re-transformed, 2080 // it is possible that p1 will have a type like Foo*[int+]:NotNull*+any. 2081 // In fact, that could have been the original type of p1, and p1 could have 2082 // had an original form like p1:(AddP x x (LShiftL quux 3)), where the 2083 // expression (LShiftL quux 3) independently optimized to the constant 8. 2084 if ((t->isa_int() == nullptr) && (t->isa_long() == nullptr) 2085 && (_type->isa_vect() == nullptr) 2086 && !ary->is_flat() 2087 && Opcode() != Op_LoadKlass && Opcode() != Op_LoadNKlass) { 2088 // t might actually be lower than _type, if _type is a unique 2089 // concrete subclass of abstract class t. 2090 if (off_beyond_header || off == Type::OffsetBot) { // is the offset beyond the header? 2091 const Type* jt = t->join_speculative(_type); 2092 // In any case, do not allow the join, per se, to empty out the type. 2093 if (jt->empty() && !t->empty()) { 2094 // This can happen if a interface-typed array narrows to a class type. 2095 jt = _type; 2096 } 2097 #ifdef ASSERT 2098 if (phase->C->eliminate_boxing() && adr->is_AddP()) { 2099 // The pointers in the autobox arrays are always non-null 2100 Node* base = adr->in(AddPNode::Base); 2101 if ((base != nullptr) && base->is_DecodeN()) { 2102 // Get LoadN node which loads IntegerCache.cache field 2103 base = base->in(1); 2104 } 2105 if ((base != nullptr) && base->is_Con()) { 2106 const TypeAryPtr* base_type = base->bottom_type()->isa_aryptr(); 2107 if ((base_type != nullptr) && base_type->is_autobox_cache()) { 2108 // It could be narrow oop 2109 assert(jt->make_ptr()->ptr() == TypePtr::NotNull,"sanity"); 2110 } 2111 } 2112 } 2113 #endif 2114 return jt; 2115 } 2116 } 2117 } else if (tp->base() == Type::InstPtr) { 2118 assert( off != Type::OffsetBot || 2119 // arrays can be cast to Objects 2120 !tp->isa_instptr() || 2121 tp->is_instptr()->instance_klass()->is_java_lang_Object() || 2122 // Default value load 2123 tp->is_instptr()->instance_klass() == ciEnv::current()->Class_klass() || 2124 // unsafe field access may not have a constant offset 2125 C->has_unsafe_access(), 2126 "Field accesses must be precise" ); 2127 // For oop loads, we expect the _type to be precise. 2128 2129 const TypeInstPtr* tinst = tp->is_instptr(); 2130 BasicType bt = memory_type(); 2131 2132 // Optimize loads from constant fields. 2133 ciObject* const_oop = tinst->const_oop(); 2134 if (!is_mismatched_access() && off != Type::OffsetBot && const_oop != nullptr && const_oop->is_instance()) { 2135 const Type* con_type = Type::make_constant_from_field(const_oop->as_instance(), off, is_unsigned(), bt); 2136 if (con_type != nullptr) { 2137 return con_type; 2138 } 2139 } 2140 } else if (tp->base() == Type::KlassPtr || tp->base() == Type::InstKlassPtr || tp->base() == Type::AryKlassPtr) { 2141 assert(off != Type::OffsetBot || 2142 !tp->isa_instklassptr() || 2143 // arrays can be cast to Objects 2144 tp->isa_instklassptr()->instance_klass()->is_java_lang_Object() || 2145 // also allow array-loading from the primary supertype 2146 // array during subtype checks 2147 Opcode() == Op_LoadKlass, 2148 "Field accesses must be precise"); 2149 // For klass/static loads, we expect the _type to be precise 2150 } else if (tp->base() == Type::RawPtr && adr->is_Load() && off == 0) { 2151 /* With mirrors being an indirect in the Klass* 2152 * the VM is now using two loads. LoadKlass(LoadP(LoadP(Klass, mirror_offset), zero_offset)) 2153 * The LoadP from the Klass has a RawPtr type (see LibraryCallKit::load_mirror_from_klass). 2154 * 2155 * So check the type and klass of the node before the LoadP. 2156 */ 2157 Node* adr2 = adr->in(MemNode::Address); 2158 const TypeKlassPtr* tkls = phase->type(adr2)->isa_klassptr(); 2159 if (tkls != nullptr && !StressReflectiveCode) { 2160 if (tkls->is_loaded() && tkls->klass_is_exact() && tkls->offset() == in_bytes(Klass::java_mirror_offset())) { 2161 ciKlass* klass = tkls->exact_klass(); 2162 assert(adr->Opcode() == Op_LoadP, "must load an oop from _java_mirror"); 2163 assert(Opcode() == Op_LoadP, "must load an oop from _java_mirror"); 2164 return TypeInstPtr::make(klass->java_mirror()); 2165 } 2166 } 2167 } 2168 2169 const TypeKlassPtr *tkls = tp->isa_klassptr(); 2170 if (tkls != nullptr) { 2171 if (tkls->is_loaded() && tkls->klass_is_exact()) { 2172 ciKlass* klass = tkls->exact_klass(); 2173 // We are loading a field from a Klass metaobject whose identity 2174 // is known at compile time (the type is "exact" or "precise"). 2175 // Check for fields we know are maintained as constants by the VM. 2176 if (tkls->offset() == in_bytes(Klass::super_check_offset_offset())) { 2177 // The field is Klass::_super_check_offset. Return its (constant) value. 2178 // (Folds up type checking code.) 2179 assert(Opcode() == Op_LoadI, "must load an int from _super_check_offset"); 2180 return TypeInt::make(klass->super_check_offset()); 2181 } 2182 // Compute index into primary_supers array 2183 juint depth = (tkls->offset() - in_bytes(Klass::primary_supers_offset())) / sizeof(Klass*); 2184 // Check for overflowing; use unsigned compare to handle the negative case. 2185 if( depth < ciKlass::primary_super_limit() ) { 2186 // The field is an element of Klass::_primary_supers. Return its (constant) value. 2187 // (Folds up type checking code.) 2188 assert(Opcode() == Op_LoadKlass, "must load a klass from _primary_supers"); 2189 ciKlass *ss = klass->super_of_depth(depth); 2190 return ss ? TypeKlassPtr::make(ss, Type::trust_interfaces) : TypePtr::NULL_PTR; 2191 } 2192 const Type* aift = load_array_final_field(tkls, klass); 2193 if (aift != nullptr) return aift; 2194 } 2195 2196 // We can still check if we are loading from the primary_supers array at a 2197 // shallow enough depth. Even though the klass is not exact, entries less 2198 // than or equal to its super depth are correct. 2199 if (tkls->is_loaded()) { 2200 ciKlass* klass = nullptr; 2201 if (tkls->isa_instklassptr()) { 2202 klass = tkls->is_instklassptr()->instance_klass(); 2203 } else { 2204 int dims; 2205 const Type* inner = tkls->is_aryklassptr()->base_element_type(dims); 2206 if (inner->isa_instklassptr()) { 2207 klass = inner->is_instklassptr()->instance_klass(); 2208 klass = ciObjArrayKlass::make(klass, dims); 2209 } 2210 } 2211 if (klass != nullptr) { 2212 // Compute index into primary_supers array 2213 juint depth = (tkls->offset() - in_bytes(Klass::primary_supers_offset())) / sizeof(Klass*); 2214 // Check for overflowing; use unsigned compare to handle the negative case. 2215 if (depth < ciKlass::primary_super_limit() && 2216 depth <= klass->super_depth()) { // allow self-depth checks to handle self-check case 2217 // The field is an element of Klass::_primary_supers. Return its (constant) value. 2218 // (Folds up type checking code.) 2219 assert(Opcode() == Op_LoadKlass, "must load a klass from _primary_supers"); 2220 ciKlass *ss = klass->super_of_depth(depth); 2221 return ss ? TypeKlassPtr::make(ss, Type::trust_interfaces) : TypePtr::NULL_PTR; 2222 } 2223 } 2224 } 2225 2226 // If the type is enough to determine that the thing is not an array, 2227 // we can give the layout_helper a positive interval type. 2228 // This will help short-circuit some reflective code. 2229 if (tkls->offset() == in_bytes(Klass::layout_helper_offset()) && 2230 tkls->isa_instklassptr() && // not directly typed as an array 2231 !tkls->is_instklassptr()->instance_klass()->is_java_lang_Object() // not the supertype of all T[] and specifically not Serializable & Cloneable 2232 ) { 2233 // Note: When interfaces are reliable, we can narrow the interface 2234 // test to (klass != Serializable && klass != Cloneable). 2235 assert(Opcode() == Op_LoadI, "must load an int from _layout_helper"); 2236 jint min_size = Klass::instance_layout_helper(oopDesc::header_size(), false); 2237 // The key property of this type is that it folds up tests 2238 // for array-ness, since it proves that the layout_helper is positive. 2239 // Thus, a generic value like the basic object layout helper works fine. 2240 return TypeInt::make(min_size, max_jint, Type::WidenMin); 2241 } 2242 } 2243 2244 // If we are loading from a freshly-allocated object, produce a zero, 2245 // if the load is provably beyond the header of the object. 2246 // (Also allow a variable load from a fresh array to produce zero.) 2247 const TypeOopPtr *tinst = tp->isa_oopptr(); 2248 bool is_instance = (tinst != nullptr) && tinst->is_known_instance_field(); 2249 bool is_boxed_value = (tinst != nullptr) && tinst->is_ptr_to_boxed_value(); 2250 if (ReduceFieldZeroing || is_instance || is_boxed_value) { 2251 Node* value = can_see_stored_value(mem,phase); 2252 if (value != nullptr && value->is_Con()) { 2253 assert(value->bottom_type()->higher_equal(_type),"sanity"); 2254 return value->bottom_type(); 2255 } 2256 } 2257 2258 bool is_vect = (_type->isa_vect() != nullptr); 2259 if (is_instance && !is_vect) { 2260 // If we have an instance type and our memory input is the 2261 // programs's initial memory state, there is no matching store, 2262 // so just return a zero of the appropriate type - 2263 // except if it is vectorized - then we have no zero constant. 2264 Node *mem = in(MemNode::Memory); 2265 if (mem->is_Parm() && mem->in(0)->is_Start()) { 2266 assert(mem->as_Parm()->_con == TypeFunc::Memory, "must be memory Parm"); 2267 return Type::get_zero_type(_type->basic_type()); 2268 } 2269 } 2270 Node* alloc = is_new_object_mark_load(); 2271 if (alloc != nullptr) { 2272 if (EnableValhalla) { 2273 // The mark word may contain property bits (inline, flat, null-free) 2274 Node* klass_node = alloc->in(AllocateNode::KlassNode); 2275 const TypeKlassPtr* tkls = phase->type(klass_node)->isa_klassptr(); 2276 if (tkls != nullptr && tkls->is_loaded() && tkls->klass_is_exact()) { 2277 return TypeX::make(tkls->exact_klass()->prototype_header().value()); 2278 } 2279 } else { 2280 return TypeX::make(markWord::prototype().value()); 2281 } 2282 } 2283 2284 return _type; 2285 } 2286 2287 //------------------------------match_edge------------------------------------- 2288 // Do we Match on this edge index or not? Match only the address. 2289 uint LoadNode::match_edge(uint idx) const { 2290 return idx == MemNode::Address; 2291 } 2292 2293 //--------------------------LoadBNode::Ideal-------------------------------------- 2294 // 2295 // If the previous store is to the same address as this load, 2296 // and the value stored was larger than a byte, replace this load 2297 // with the value stored truncated to a byte. If no truncation is 2298 // needed, the replacement is done in LoadNode::Identity(). 2299 // 2300 Node* LoadBNode::Ideal(PhaseGVN* phase, bool can_reshape) { 2301 Node* mem = in(MemNode::Memory); 2302 Node* value = can_see_stored_value(mem,phase); 2303 if (value != nullptr) { 2304 Node* narrow = Compile::narrow_value(T_BYTE, value, _type, phase, false); 2305 if (narrow != value) { 2306 return narrow; 2307 } 2308 } 2309 // Identity call will handle the case where truncation is not needed. 2310 return LoadNode::Ideal(phase, can_reshape); 2311 } 2312 2313 const Type* LoadBNode::Value(PhaseGVN* phase) const { 2314 Node* mem = in(MemNode::Memory); 2315 Node* value = can_see_stored_value(mem,phase); 2316 if (value != nullptr && value->is_Con() && 2317 !value->bottom_type()->higher_equal(_type)) { 2318 // If the input to the store does not fit with the load's result type, 2319 // it must be truncated. We can't delay until Ideal call since 2320 // a singleton Value is needed for split_thru_phi optimization. 2321 int con = value->get_int(); 2322 return TypeInt::make((con << 24) >> 24); 2323 } 2324 return LoadNode::Value(phase); 2325 } 2326 2327 //--------------------------LoadUBNode::Ideal------------------------------------- 2328 // 2329 // If the previous store is to the same address as this load, 2330 // and the value stored was larger than a byte, replace this load 2331 // with the value stored truncated to a byte. If no truncation is 2332 // needed, the replacement is done in LoadNode::Identity(). 2333 // 2334 Node* LoadUBNode::Ideal(PhaseGVN* phase, bool can_reshape) { 2335 Node* mem = in(MemNode::Memory); 2336 Node* value = can_see_stored_value(mem, phase); 2337 if (value != nullptr) { 2338 Node* narrow = Compile::narrow_value(T_BOOLEAN, value, _type, phase, false); 2339 if (narrow != value) { 2340 return narrow; 2341 } 2342 } 2343 // Identity call will handle the case where truncation is not needed. 2344 return LoadNode::Ideal(phase, can_reshape); 2345 } 2346 2347 const Type* LoadUBNode::Value(PhaseGVN* phase) const { 2348 Node* mem = in(MemNode::Memory); 2349 Node* value = can_see_stored_value(mem,phase); 2350 if (value != nullptr && value->is_Con() && 2351 !value->bottom_type()->higher_equal(_type)) { 2352 // If the input to the store does not fit with the load's result type, 2353 // it must be truncated. We can't delay until Ideal call since 2354 // a singleton Value is needed for split_thru_phi optimization. 2355 int con = value->get_int(); 2356 return TypeInt::make(con & 0xFF); 2357 } 2358 return LoadNode::Value(phase); 2359 } 2360 2361 //--------------------------LoadUSNode::Ideal------------------------------------- 2362 // 2363 // If the previous store is to the same address as this load, 2364 // and the value stored was larger than a char, replace this load 2365 // with the value stored truncated to a char. If no truncation is 2366 // needed, the replacement is done in LoadNode::Identity(). 2367 // 2368 Node* LoadUSNode::Ideal(PhaseGVN* phase, bool can_reshape) { 2369 Node* mem = in(MemNode::Memory); 2370 Node* value = can_see_stored_value(mem,phase); 2371 if (value != nullptr) { 2372 Node* narrow = Compile::narrow_value(T_CHAR, value, _type, phase, false); 2373 if (narrow != value) { 2374 return narrow; 2375 } 2376 } 2377 // Identity call will handle the case where truncation is not needed. 2378 return LoadNode::Ideal(phase, can_reshape); 2379 } 2380 2381 const Type* LoadUSNode::Value(PhaseGVN* phase) const { 2382 Node* mem = in(MemNode::Memory); 2383 Node* value = can_see_stored_value(mem,phase); 2384 if (value != nullptr && value->is_Con() && 2385 !value->bottom_type()->higher_equal(_type)) { 2386 // If the input to the store does not fit with the load's result type, 2387 // it must be truncated. We can't delay until Ideal call since 2388 // a singleton Value is needed for split_thru_phi optimization. 2389 int con = value->get_int(); 2390 return TypeInt::make(con & 0xFFFF); 2391 } 2392 return LoadNode::Value(phase); 2393 } 2394 2395 //--------------------------LoadSNode::Ideal-------------------------------------- 2396 // 2397 // If the previous store is to the same address as this load, 2398 // and the value stored was larger than a short, replace this load 2399 // with the value stored truncated to a short. If no truncation is 2400 // needed, the replacement is done in LoadNode::Identity(). 2401 // 2402 Node* LoadSNode::Ideal(PhaseGVN* phase, bool can_reshape) { 2403 Node* mem = in(MemNode::Memory); 2404 Node* value = can_see_stored_value(mem,phase); 2405 if (value != nullptr) { 2406 Node* narrow = Compile::narrow_value(T_SHORT, value, _type, phase, false); 2407 if (narrow != value) { 2408 return narrow; 2409 } 2410 } 2411 // Identity call will handle the case where truncation is not needed. 2412 return LoadNode::Ideal(phase, can_reshape); 2413 } 2414 2415 const Type* LoadSNode::Value(PhaseGVN* phase) const { 2416 Node* mem = in(MemNode::Memory); 2417 Node* value = can_see_stored_value(mem,phase); 2418 if (value != nullptr && value->is_Con() && 2419 !value->bottom_type()->higher_equal(_type)) { 2420 // If the input to the store does not fit with the load's result type, 2421 // it must be truncated. We can't delay until Ideal call since 2422 // a singleton Value is needed for split_thru_phi optimization. 2423 int con = value->get_int(); 2424 return TypeInt::make((con << 16) >> 16); 2425 } 2426 return LoadNode::Value(phase); 2427 } 2428 2429 //============================================================================= 2430 //----------------------------LoadKlassNode::make------------------------------ 2431 // Polymorphic factory method: 2432 Node* LoadKlassNode::make(PhaseGVN& gvn, Node* ctl, Node* mem, Node* adr, const TypePtr* at, 2433 const TypeKlassPtr* tk) { 2434 // sanity check the alias category against the created node type 2435 const TypePtr *adr_type = adr->bottom_type()->isa_ptr(); 2436 assert(adr_type != nullptr, "expecting TypeKlassPtr"); 2437 #ifdef _LP64 2438 if (adr_type->is_ptr_to_narrowklass()) { 2439 assert(UseCompressedClassPointers, "no compressed klasses"); 2440 Node* load_klass = gvn.transform(new LoadNKlassNode(ctl, mem, adr, at, tk->make_narrowklass(), MemNode::unordered)); 2441 return new DecodeNKlassNode(load_klass, load_klass->bottom_type()->make_ptr()); 2442 } 2443 #endif 2444 assert(!adr_type->is_ptr_to_narrowklass() && !adr_type->is_ptr_to_narrowoop(), "should have got back a narrow oop"); 2445 return new LoadKlassNode(ctl, mem, adr, at, tk, MemNode::unordered); 2446 } 2447 2448 //------------------------------Value------------------------------------------ 2449 const Type* LoadKlassNode::Value(PhaseGVN* phase) const { 2450 return klass_value_common(phase); 2451 } 2452 2453 // In most cases, LoadKlassNode does not have the control input set. If the control 2454 // input is set, it must not be removed (by LoadNode::Ideal()). 2455 bool LoadKlassNode::can_remove_control() const { 2456 return false; 2457 } 2458 2459 const Type* LoadNode::klass_value_common(PhaseGVN* phase) const { 2460 // Either input is TOP ==> the result is TOP 2461 const Type *t1 = phase->type( in(MemNode::Memory) ); 2462 if (t1 == Type::TOP) return Type::TOP; 2463 Node *adr = in(MemNode::Address); 2464 const Type *t2 = phase->type( adr ); 2465 if (t2 == Type::TOP) return Type::TOP; 2466 const TypePtr *tp = t2->is_ptr(); 2467 if (TypePtr::above_centerline(tp->ptr()) || 2468 tp->ptr() == TypePtr::Null) return Type::TOP; 2469 2470 // Return a more precise klass, if possible 2471 const TypeInstPtr *tinst = tp->isa_instptr(); 2472 if (tinst != nullptr) { 2473 ciInstanceKlass* ik = tinst->instance_klass(); 2474 int offset = tinst->offset(); 2475 if (ik == phase->C->env()->Class_klass() 2476 && (offset == java_lang_Class::klass_offset() || 2477 offset == java_lang_Class::array_klass_offset())) { 2478 // We are loading a special hidden field from a Class mirror object, 2479 // the field which points to the VM's Klass metaobject. 2480 bool is_null_free_array = false; 2481 ciType* t = tinst->java_mirror_type(&is_null_free_array); 2482 // java_mirror_type returns non-null for compile-time Class constants. 2483 if (t != nullptr) { 2484 // constant oop => constant klass 2485 if (offset == java_lang_Class::array_klass_offset()) { 2486 if (t->is_void()) { 2487 // We cannot create a void array. Since void is a primitive type return null 2488 // klass. Users of this result need to do a null check on the returned klass. 2489 return TypePtr::NULL_PTR; 2490 } 2491 const TypeKlassPtr* tklass = TypeKlassPtr::make(ciArrayKlass::make(t), Type::trust_interfaces); 2492 if (is_null_free_array) { 2493 tklass = tklass->is_aryklassptr()->cast_to_null_free(); 2494 } 2495 return tklass; 2496 } 2497 if (!t->is_klass()) { 2498 // a primitive Class (e.g., int.class) has null for a klass field 2499 return TypePtr::NULL_PTR; 2500 } 2501 // (Folds up the 1st indirection in aClassConstant.getModifiers().) 2502 const TypeKlassPtr* tklass = TypeKlassPtr::make(t->as_klass(), Type::trust_interfaces); 2503 if (is_null_free_array) { 2504 tklass = tklass->is_aryklassptr()->cast_to_null_free(); 2505 } 2506 return tklass; 2507 } 2508 // non-constant mirror, so we can't tell what's going on 2509 } 2510 if (!tinst->is_loaded()) 2511 return _type; // Bail out if not loaded 2512 if (offset == oopDesc::klass_offset_in_bytes()) { 2513 return tinst->as_klass_type(true); 2514 } 2515 } 2516 2517 // Check for loading klass from an array 2518 const TypeAryPtr* tary = tp->isa_aryptr(); 2519 if (tary != nullptr && 2520 tary->offset() == oopDesc::klass_offset_in_bytes()) { 2521 return tary->as_klass_type(true); 2522 } 2523 2524 // Check for loading klass from an array klass 2525 const TypeKlassPtr *tkls = tp->isa_klassptr(); 2526 if (tkls != nullptr && !StressReflectiveCode) { 2527 if (!tkls->is_loaded()) 2528 return _type; // Bail out if not loaded 2529 if (tkls->isa_aryklassptr() && tkls->is_aryklassptr()->elem()->isa_klassptr() && 2530 tkls->offset() == in_bytes(ObjArrayKlass::element_klass_offset())) { 2531 // // Always returning precise element type is incorrect, 2532 // // e.g., element type could be object and array may contain strings 2533 // return TypeKlassPtr::make(TypePtr::Constant, elem, 0); 2534 2535 // The array's TypeKlassPtr was declared 'precise' or 'not precise' 2536 // according to the element type's subclassing. 2537 return tkls->is_aryklassptr()->elem()->isa_klassptr()->cast_to_exactness(tkls->klass_is_exact()); 2538 } 2539 if (tkls->isa_instklassptr() != nullptr && tkls->klass_is_exact() && 2540 tkls->offset() == in_bytes(Klass::super_offset())) { 2541 ciKlass* sup = tkls->is_instklassptr()->instance_klass()->super(); 2542 // The field is Klass::_super. Return its (constant) value. 2543 // (Folds up the 2nd indirection in aClassConstant.getSuperClass().) 2544 return sup ? TypeKlassPtr::make(sup, Type::trust_interfaces) : TypePtr::NULL_PTR; 2545 } 2546 } 2547 2548 if (tkls != nullptr && !UseSecondarySupersCache 2549 && tkls->offset() == in_bytes(Klass::secondary_super_cache_offset())) { 2550 // Treat Klass::_secondary_super_cache as a constant when the cache is disabled. 2551 return TypePtr::NULL_PTR; 2552 } 2553 2554 // Bailout case 2555 return LoadNode::Value(phase); 2556 } 2557 2558 //------------------------------Identity--------------------------------------- 2559 // To clean up reflective code, simplify k.java_mirror.as_klass to plain k. 2560 // Also feed through the klass in Allocate(...klass...)._klass. 2561 Node* LoadKlassNode::Identity(PhaseGVN* phase) { 2562 return klass_identity_common(phase); 2563 } 2564 2565 Node* LoadNode::klass_identity_common(PhaseGVN* phase) { 2566 Node* x = LoadNode::Identity(phase); 2567 if (x != this) return x; 2568 2569 // Take apart the address into an oop and offset. 2570 // Return 'this' if we cannot. 2571 Node* adr = in(MemNode::Address); 2572 intptr_t offset = 0; 2573 Node* base = AddPNode::Ideal_base_and_offset(adr, phase, offset); 2574 if (base == nullptr) return this; 2575 const TypeOopPtr* toop = phase->type(adr)->isa_oopptr(); 2576 if (toop == nullptr) return this; 2577 2578 // Step over potential GC barrier for OopHandle resolve 2579 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2(); 2580 if (bs->is_gc_barrier_node(base)) { 2581 base = bs->step_over_gc_barrier(base); 2582 } 2583 2584 // We can fetch the klass directly through an AllocateNode. 2585 // This works even if the klass is not constant (clone or newArray). 2586 if (offset == oopDesc::klass_offset_in_bytes()) { 2587 Node* allocated_klass = AllocateNode::Ideal_klass(base, phase); 2588 if (allocated_klass != nullptr) { 2589 return allocated_klass; 2590 } 2591 } 2592 2593 // Simplify k.java_mirror.as_klass to plain k, where k is a Klass*. 2594 // See inline_native_Class_query for occurrences of these patterns. 2595 // Java Example: x.getClass().isAssignableFrom(y) 2596 // 2597 // This improves reflective code, often making the Class 2598 // mirror go completely dead. (Current exception: Class 2599 // mirrors may appear in debug info, but we could clean them out by 2600 // introducing a new debug info operator for Klass.java_mirror). 2601 2602 if (toop->isa_instptr() && toop->is_instptr()->instance_klass() == phase->C->env()->Class_klass() 2603 && offset == java_lang_Class::klass_offset()) { 2604 if (base->is_Load()) { 2605 Node* base2 = base->in(MemNode::Address); 2606 if (base2->is_Load()) { /* direct load of a load which is the OopHandle */ 2607 Node* adr2 = base2->in(MemNode::Address); 2608 const TypeKlassPtr* tkls = phase->type(adr2)->isa_klassptr(); 2609 if (tkls != nullptr && !tkls->empty() 2610 && (tkls->isa_instklassptr() || tkls->isa_aryklassptr()) 2611 && adr2->is_AddP() 2612 ) { 2613 int mirror_field = in_bytes(Klass::java_mirror_offset()); 2614 if (tkls->offset() == mirror_field) { 2615 return adr2->in(AddPNode::Base); 2616 } 2617 } 2618 } 2619 } 2620 } 2621 2622 return this; 2623 } 2624 2625 LoadNode* LoadNode::clone_pinned() const { 2626 LoadNode* ld = clone()->as_Load(); 2627 ld->_control_dependency = UnknownControl; 2628 return ld; 2629 } 2630 2631 2632 //------------------------------Value------------------------------------------ 2633 const Type* LoadNKlassNode::Value(PhaseGVN* phase) const { 2634 const Type *t = klass_value_common(phase); 2635 if (t == Type::TOP) 2636 return t; 2637 2638 return t->make_narrowklass(); 2639 } 2640 2641 //------------------------------Identity--------------------------------------- 2642 // To clean up reflective code, simplify k.java_mirror.as_klass to narrow k. 2643 // Also feed through the klass in Allocate(...klass...)._klass. 2644 Node* LoadNKlassNode::Identity(PhaseGVN* phase) { 2645 Node *x = klass_identity_common(phase); 2646 2647 const Type *t = phase->type( x ); 2648 if( t == Type::TOP ) return x; 2649 if( t->isa_narrowklass()) return x; 2650 assert (!t->isa_narrowoop(), "no narrow oop here"); 2651 2652 return phase->transform(new EncodePKlassNode(x, t->make_narrowklass())); 2653 } 2654 2655 //------------------------------Value----------------------------------------- 2656 const Type* LoadRangeNode::Value(PhaseGVN* phase) const { 2657 // Either input is TOP ==> the result is TOP 2658 const Type *t1 = phase->type( in(MemNode::Memory) ); 2659 if( t1 == Type::TOP ) return Type::TOP; 2660 Node *adr = in(MemNode::Address); 2661 const Type *t2 = phase->type( adr ); 2662 if( t2 == Type::TOP ) return Type::TOP; 2663 const TypePtr *tp = t2->is_ptr(); 2664 if (TypePtr::above_centerline(tp->ptr())) return Type::TOP; 2665 const TypeAryPtr *tap = tp->isa_aryptr(); 2666 if( !tap ) return _type; 2667 return tap->size(); 2668 } 2669 2670 //-------------------------------Ideal--------------------------------------- 2671 // Feed through the length in AllocateArray(...length...)._length. 2672 Node *LoadRangeNode::Ideal(PhaseGVN *phase, bool can_reshape) { 2673 Node* p = MemNode::Ideal_common(phase, can_reshape); 2674 if (p) return (p == NodeSentinel) ? nullptr : p; 2675 2676 // Take apart the address into an oop and offset. 2677 // Return 'this' if we cannot. 2678 Node* adr = in(MemNode::Address); 2679 intptr_t offset = 0; 2680 Node* base = AddPNode::Ideal_base_and_offset(adr, phase, offset); 2681 if (base == nullptr) return nullptr; 2682 const TypeAryPtr* tary = phase->type(adr)->isa_aryptr(); 2683 if (tary == nullptr) return nullptr; 2684 2685 // We can fetch the length directly through an AllocateArrayNode. 2686 // This works even if the length is not constant (clone or newArray). 2687 if (offset == arrayOopDesc::length_offset_in_bytes()) { 2688 AllocateArrayNode* alloc = AllocateArrayNode::Ideal_array_allocation(base); 2689 if (alloc != nullptr) { 2690 Node* allocated_length = alloc->Ideal_length(); 2691 Node* len = alloc->make_ideal_length(tary, phase); 2692 if (allocated_length != len) { 2693 // New CastII improves on this. 2694 return len; 2695 } 2696 } 2697 } 2698 2699 return nullptr; 2700 } 2701 2702 //------------------------------Identity--------------------------------------- 2703 // Feed through the length in AllocateArray(...length...)._length. 2704 Node* LoadRangeNode::Identity(PhaseGVN* phase) { 2705 Node* x = LoadINode::Identity(phase); 2706 if (x != this) return x; 2707 2708 // Take apart the address into an oop and offset. 2709 // Return 'this' if we cannot. 2710 Node* adr = in(MemNode::Address); 2711 intptr_t offset = 0; 2712 Node* base = AddPNode::Ideal_base_and_offset(adr, phase, offset); 2713 if (base == nullptr) return this; 2714 const TypeAryPtr* tary = phase->type(adr)->isa_aryptr(); 2715 if (tary == nullptr) return this; 2716 2717 // We can fetch the length directly through an AllocateArrayNode. 2718 // This works even if the length is not constant (clone or newArray). 2719 if (offset == arrayOopDesc::length_offset_in_bytes()) { 2720 AllocateArrayNode* alloc = AllocateArrayNode::Ideal_array_allocation(base); 2721 if (alloc != nullptr) { 2722 Node* allocated_length = alloc->Ideal_length(); 2723 // Do not allow make_ideal_length to allocate a CastII node. 2724 Node* len = alloc->make_ideal_length(tary, phase, false); 2725 if (allocated_length == len) { 2726 // Return allocated_length only if it would not be improved by a CastII. 2727 return allocated_length; 2728 } 2729 } 2730 } 2731 2732 return this; 2733 2734 } 2735 2736 //============================================================================= 2737 //---------------------------StoreNode::make----------------------------------- 2738 // Polymorphic factory method: 2739 StoreNode* StoreNode::make(PhaseGVN& gvn, Node* ctl, Node* mem, Node* adr, const TypePtr* adr_type, Node* val, BasicType bt, MemOrd mo, bool require_atomic_access) { 2740 assert((mo == unordered || mo == release), "unexpected"); 2741 Compile* C = gvn.C; 2742 assert(C->get_alias_index(adr_type) != Compile::AliasIdxRaw || 2743 ctl != nullptr, "raw memory operations should have control edge"); 2744 2745 switch (bt) { 2746 case T_BOOLEAN: val = gvn.transform(new AndINode(val, gvn.intcon(0x1))); // Fall through to T_BYTE case 2747 case T_BYTE: return new StoreBNode(ctl, mem, adr, adr_type, val, mo); 2748 case T_INT: return new StoreINode(ctl, mem, adr, adr_type, val, mo); 2749 case T_CHAR: 2750 case T_SHORT: return new StoreCNode(ctl, mem, adr, adr_type, val, mo); 2751 case T_LONG: return new StoreLNode(ctl, mem, adr, adr_type, val, mo, require_atomic_access); 2752 case T_FLOAT: return new StoreFNode(ctl, mem, adr, adr_type, val, mo); 2753 case T_DOUBLE: return new StoreDNode(ctl, mem, adr, adr_type, val, mo, require_atomic_access); 2754 case T_METADATA: 2755 case T_ADDRESS: 2756 case T_OBJECT: 2757 #ifdef _LP64 2758 if (adr->bottom_type()->is_ptr_to_narrowoop()) { 2759 val = gvn.transform(new EncodePNode(val, val->bottom_type()->make_narrowoop())); 2760 return new StoreNNode(ctl, mem, adr, adr_type, val, mo); 2761 } else if (adr->bottom_type()->is_ptr_to_narrowklass() || 2762 (UseCompressedClassPointers && val->bottom_type()->isa_klassptr() && 2763 adr->bottom_type()->isa_rawptr())) { 2764 val = gvn.transform(new EncodePKlassNode(val, val->bottom_type()->make_narrowklass())); 2765 return new StoreNKlassNode(ctl, mem, adr, adr_type, val, mo); 2766 } 2767 #endif 2768 { 2769 return new StorePNode(ctl, mem, adr, adr_type, val, mo); 2770 } 2771 default: 2772 ShouldNotReachHere(); 2773 return (StoreNode*)nullptr; 2774 } 2775 } 2776 2777 //--------------------------bottom_type---------------------------------------- 2778 const Type *StoreNode::bottom_type() const { 2779 return Type::MEMORY; 2780 } 2781 2782 //------------------------------hash------------------------------------------- 2783 uint StoreNode::hash() const { 2784 // unroll addition of interesting fields 2785 //return (uintptr_t)in(Control) + (uintptr_t)in(Memory) + (uintptr_t)in(Address) + (uintptr_t)in(ValueIn); 2786 2787 // Since they are not commoned, do not hash them: 2788 return NO_HASH; 2789 } 2790 2791 // Class to parse array pointers, and determine if they are adjacent. We parse the form: 2792 // 2793 // pointer = base 2794 // + constant_offset 2795 // + LShiftL( ConvI2L(int_offset + int_con), int_offset_shift) 2796 // + sum(other_offsets) 2797 // 2798 // 2799 // Note: we accumulate all constant offsets into constant_offset, even the int constant behind 2800 // the "LShiftL(ConvI2L(...))" pattern. We convert "ConvI2L(int_offset + int_con)" to 2801 // "ConvI2L(int_offset) + int_con", which is only safe if we can assume that either all 2802 // compared addresses have an overflow for "int_offset + int_con" or none. 2803 // For loads and stores on arrays, we know that if one overflows and the other not, then 2804 // the two addresses lay almost max_int indices apart, but the maximal array size is 2805 // only about half of that. Therefore, the RangeCheck on at least one of them must have 2806 // failed. 2807 // 2808 // constant_offset += LShiftL( ConvI2L(int_con), int_offset_shift) 2809 // 2810 // pointer = base 2811 // + constant_offset 2812 // + LShiftL( ConvI2L(int_offset), int_offset_shift) 2813 // + sum(other_offsets) 2814 // 2815 class ArrayPointer { 2816 private: 2817 const Node* _pointer; // The final pointer to the position in the array 2818 const Node* _base; // Base address of the array 2819 const jlong _constant_offset; // Sum of collected constant offsets 2820 const Node* _int_offset; // (optional) Offset behind LShiftL and ConvI2L 2821 const GrowableArray<Node*>* _other_offsets; // List of other AddP offsets 2822 const jint _int_offset_shift; // (optional) Shift value for int_offset 2823 const bool _is_valid; // The parsing succeeded 2824 2825 ArrayPointer(const bool is_valid, 2826 const Node* pointer, 2827 const Node* base, 2828 const jlong constant_offset, 2829 const Node* int_offset, 2830 const jint int_offset_shift, 2831 const GrowableArray<Node*>* other_offsets) : 2832 _pointer(pointer), 2833 _base(base), 2834 _constant_offset(constant_offset), 2835 _int_offset(int_offset), 2836 _other_offsets(other_offsets), 2837 _int_offset_shift(int_offset_shift), 2838 _is_valid(is_valid) 2839 { 2840 assert(_pointer != nullptr, "must always have pointer"); 2841 assert(is_valid == (_base != nullptr), "have base exactly if valid"); 2842 assert(is_valid == (_other_offsets != nullptr), "have other_offsets exactly if valid"); 2843 } 2844 2845 static ArrayPointer make_invalid(const Node* pointer) { 2846 return ArrayPointer(false, pointer, nullptr, 0, nullptr, 0, nullptr); 2847 } 2848 2849 static bool parse_int_offset(Node* offset, Node*& int_offset, jint& int_offset_shift) { 2850 // offset = LShiftL( ConvI2L(int_offset), int_offset_shift) 2851 if (offset->Opcode() == Op_LShiftL && 2852 offset->in(1)->Opcode() == Op_ConvI2L && 2853 offset->in(2)->Opcode() == Op_ConI) { 2854 int_offset = offset->in(1)->in(1); // LShiftL -> ConvI2L -> int_offset 2855 int_offset_shift = offset->in(2)->get_int(); // LShiftL -> int_offset_shift 2856 return true; 2857 } 2858 2859 // offset = ConvI2L(int_offset) = LShiftL( ConvI2L(int_offset), 0) 2860 if (offset->Opcode() == Op_ConvI2L) { 2861 int_offset = offset->in(1); 2862 int_offset_shift = 0; 2863 return true; 2864 } 2865 2866 // parse failed 2867 return false; 2868 } 2869 2870 public: 2871 // Parse the structure above the pointer 2872 static ArrayPointer make(PhaseGVN* phase, const Node* pointer) { 2873 assert(phase->type(pointer)->isa_aryptr() != nullptr, "must be array pointer"); 2874 if (!pointer->is_AddP()) { return ArrayPointer::make_invalid(pointer); } 2875 2876 const Node* base = pointer->in(AddPNode::Base); 2877 if (base == nullptr) { return ArrayPointer::make_invalid(pointer); } 2878 2879 const int search_depth = 5; 2880 Node* offsets[search_depth]; 2881 int count = pointer->as_AddP()->unpack_offsets(offsets, search_depth); 2882 2883 // We expect at least a constant each 2884 if (count <= 0) { return ArrayPointer::make_invalid(pointer); } 2885 2886 // We extract the form: 2887 // 2888 // pointer = base 2889 // + constant_offset 2890 // + LShiftL( ConvI2L(int_offset + int_con), int_offset_shift) 2891 // + sum(other_offsets) 2892 // 2893 jlong constant_offset = 0; 2894 Node* int_offset = nullptr; 2895 jint int_offset_shift = 0; 2896 GrowableArray<Node*>* other_offsets = new GrowableArray<Node*>(count); 2897 2898 for (int i = 0; i < count; i++) { 2899 Node* offset = offsets[i]; 2900 if (offset->Opcode() == Op_ConI) { 2901 // Constant int offset 2902 constant_offset += offset->get_int(); 2903 } else if (offset->Opcode() == Op_ConL) { 2904 // Constant long offset 2905 constant_offset += offset->get_long(); 2906 } else if(int_offset == nullptr && parse_int_offset(offset, int_offset, int_offset_shift)) { 2907 // LShiftL( ConvI2L(int_offset), int_offset_shift) 2908 int_offset = int_offset->uncast(); 2909 if (int_offset->Opcode() == Op_AddI && int_offset->in(2)->Opcode() == Op_ConI) { 2910 // LShiftL( ConvI2L(int_offset + int_con), int_offset_shift) 2911 constant_offset += ((jlong)int_offset->in(2)->get_int()) << int_offset_shift; 2912 int_offset = int_offset->in(1); 2913 } 2914 } else { 2915 // All others 2916 other_offsets->append(offset); 2917 } 2918 } 2919 2920 return ArrayPointer(true, pointer, base, constant_offset, int_offset, int_offset_shift, other_offsets); 2921 } 2922 2923 bool is_adjacent_to_and_before(const ArrayPointer& other, const jlong data_size) const { 2924 if (!_is_valid || !other._is_valid) { return false; } 2925 2926 // Offset adjacent? 2927 if (this->_constant_offset + data_size != other._constant_offset) { return false; } 2928 2929 // All other components identical? 2930 if (this->_base != other._base || 2931 this->_int_offset != other._int_offset || 2932 this->_int_offset_shift != other._int_offset_shift || 2933 this->_other_offsets->length() != other._other_offsets->length()) { 2934 return false; 2935 } 2936 2937 for (int i = 0; i < this->_other_offsets->length(); i++) { 2938 Node* o1 = this->_other_offsets->at(i); 2939 Node* o2 = other._other_offsets->at(i); 2940 if (o1 != o2) { return false; } 2941 } 2942 2943 return true; 2944 } 2945 2946 #ifndef PRODUCT 2947 void dump() { 2948 if (!_is_valid) { 2949 tty->print("ArrayPointer[%d %s, invalid]", _pointer->_idx, _pointer->Name()); 2950 return; 2951 } 2952 tty->print("ArrayPointer[%d %s, base[%d %s] + %lld", 2953 _pointer->_idx, _pointer->Name(), 2954 _base->_idx, _base->Name(), 2955 (long long)_constant_offset); 2956 if (_int_offset != nullptr) { 2957 tty->print(" + I2L[%d %s] << %d", 2958 _int_offset->_idx, _int_offset->Name(), _int_offset_shift); 2959 } 2960 for (int i = 0; i < _other_offsets->length(); i++) { 2961 Node* n = _other_offsets->at(i); 2962 tty->print(" + [%d %s]", n->_idx, n->Name()); 2963 } 2964 tty->print_cr("]"); 2965 } 2966 #endif 2967 }; 2968 2969 // Link together multiple stores (B/S/C/I) into a longer one. 2970 // 2971 // Example: _store = StoreB[i+3] 2972 // 2973 // RangeCheck[i+0] RangeCheck[i+0] 2974 // StoreB[i+0] 2975 // RangeCheck[i+3] RangeCheck[i+3] 2976 // StoreB[i+1] --> pass: fail: 2977 // StoreB[i+2] StoreI[i+0] StoreB[i+0] 2978 // StoreB[i+3] 2979 // 2980 // The 4 StoreB are merged into a single StoreI node. We have to be careful with RangeCheck[i+1]: before 2981 // the optimization, if this RangeCheck[i+1] fails, then we execute only StoreB[i+0], and then trap. After 2982 // the optimization, the new StoreI[i+0] is on the passing path of RangeCheck[i+3], and StoreB[i+0] on the 2983 // failing path. 2984 // 2985 // Note: For normal array stores, every store at first has a RangeCheck. But they can be removed with: 2986 // - RCE (RangeCheck Elimination): the RangeChecks in the loop are hoisted out and before the loop, 2987 // and possibly no RangeChecks remain between the stores. 2988 // - RangeCheck smearing: the earlier RangeChecks are adjusted such that they cover later RangeChecks, 2989 // and those later RangeChecks can be removed. Example: 2990 // 2991 // RangeCheck[i+0] RangeCheck[i+0] <- before first store 2992 // StoreB[i+0] StoreB[i+0] <- first store 2993 // RangeCheck[i+1] --> smeared --> RangeCheck[i+3] <- only RC between first and last store 2994 // StoreB[i+1] StoreB[i+1] <- second store 2995 // RangeCheck[i+2] --> removed 2996 // StoreB[i+2] StoreB[i+2] 2997 // RangeCheck[i+3] --> removed 2998 // StoreB[i+3] StoreB[i+3] <- last store 2999 // 3000 // Thus, it is a common pattern that between the first and last store in a chain 3001 // of adjacent stores there remains exactly one RangeCheck, located between the 3002 // first and the second store (e.g. RangeCheck[i+3]). 3003 // 3004 class MergePrimitiveArrayStores : public StackObj { 3005 private: 3006 PhaseGVN* _phase; 3007 StoreNode* _store; 3008 3009 public: 3010 MergePrimitiveArrayStores(PhaseGVN* phase, StoreNode* store) : _phase(phase), _store(store) {} 3011 3012 StoreNode* run(); 3013 3014 private: 3015 bool is_compatible_store(const StoreNode* other_store) const; 3016 bool is_adjacent_pair(const StoreNode* use_store, const StoreNode* def_store) const; 3017 bool is_adjacent_input_pair(const Node* n1, const Node* n2, const int memory_size) const; 3018 static bool is_con_RShift(const Node* n, Node const*& base_out, jint& shift_out); 3019 enum CFGStatus { SuccessNoRangeCheck, SuccessWithRangeCheck, Failure }; 3020 static CFGStatus cfg_status_for_pair(const StoreNode* use_store, const StoreNode* def_store); 3021 3022 class Status { 3023 private: 3024 StoreNode* _found_store; 3025 bool _found_range_check; 3026 3027 Status(StoreNode* found_store, bool found_range_check) 3028 : _found_store(found_store), _found_range_check(found_range_check) {} 3029 3030 public: 3031 StoreNode* found_store() const { return _found_store; } 3032 bool found_range_check() const { return _found_range_check; } 3033 static Status make_failure() { return Status(nullptr, false); } 3034 3035 static Status make(StoreNode* found_store, const CFGStatus cfg_status) { 3036 if (cfg_status == CFGStatus::Failure) { 3037 return Status::make_failure(); 3038 } 3039 return Status(found_store, cfg_status == CFGStatus::SuccessWithRangeCheck); 3040 } 3041 }; 3042 3043 Status find_adjacent_use_store(const StoreNode* def_store) const; 3044 Status find_adjacent_def_store(const StoreNode* use_store) const; 3045 Status find_use_store(const StoreNode* def_store) const; 3046 Status find_def_store(const StoreNode* use_store) const; 3047 Status find_use_store_unidirectional(const StoreNode* def_store) const; 3048 Status find_def_store_unidirectional(const StoreNode* use_store) const; 3049 3050 void collect_merge_list(Node_List& merge_list) const; 3051 Node* make_merged_input_value(const Node_List& merge_list); 3052 StoreNode* make_merged_store(const Node_List& merge_list, Node* merged_input_value); 3053 3054 DEBUG_ONLY( void trace(const Node_List& merge_list, const Node* merged_input_value, const StoreNode* merged_store) const; ) 3055 }; 3056 3057 StoreNode* MergePrimitiveArrayStores::run() { 3058 // Check for B/S/C/I 3059 int opc = _store->Opcode(); 3060 if (opc != Op_StoreB && opc != Op_StoreC && opc != Op_StoreI) { 3061 return nullptr; 3062 } 3063 3064 // Only merge stores on arrays, and the stores must have the same size as the elements. 3065 const TypePtr* ptr_t = _store->adr_type(); 3066 if (ptr_t == nullptr) { 3067 return nullptr; 3068 } 3069 const TypeAryPtr* aryptr_t = ptr_t->isa_aryptr(); 3070 if (aryptr_t == nullptr) { 3071 return nullptr; 3072 } 3073 BasicType bt = aryptr_t->elem()->array_element_basic_type(); 3074 if (!is_java_primitive(bt) || 3075 type2aelembytes(bt) != _store->memory_size()) { 3076 return nullptr; 3077 } 3078 if (_store->is_unsafe_access()) { 3079 return nullptr; 3080 } 3081 3082 // The _store must be the "last" store in a chain. If we find a use we could merge with 3083 // then that use or a store further down is the "last" store. 3084 Status status_use = find_adjacent_use_store(_store); 3085 if (status_use.found_store() != nullptr) { 3086 return nullptr; 3087 } 3088 3089 // Check if we can merge with at least one def, so that we have at least 2 stores to merge. 3090 Status status_def = find_adjacent_def_store(_store); 3091 if (status_def.found_store() == nullptr) { 3092 return nullptr; 3093 } 3094 3095 ResourceMark rm; 3096 Node_List merge_list; 3097 collect_merge_list(merge_list); 3098 3099 Node* merged_input_value = make_merged_input_value(merge_list); 3100 if (merged_input_value == nullptr) { return nullptr; } 3101 3102 StoreNode* merged_store = make_merged_store(merge_list, merged_input_value); 3103 3104 DEBUG_ONLY( if(TraceMergeStores) { trace(merge_list, merged_input_value, merged_store); } ) 3105 3106 return merged_store; 3107 } 3108 3109 // Check compatibility between _store and other_store. 3110 bool MergePrimitiveArrayStores::is_compatible_store(const StoreNode* other_store) const { 3111 int opc = _store->Opcode(); 3112 assert(opc == Op_StoreB || opc == Op_StoreC || opc == Op_StoreI, "precondition"); 3113 assert(_store->adr_type()->isa_aryptr() != nullptr, "must be array store"); 3114 assert(!_store->is_unsafe_access(), "no unsafe accesses"); 3115 3116 if (other_store == nullptr || 3117 _store->Opcode() != other_store->Opcode() || 3118 other_store->adr_type() == nullptr || 3119 other_store->adr_type()->isa_aryptr() == nullptr || 3120 other_store->is_unsafe_access()) { 3121 return false; 3122 } 3123 3124 // Check that the size of the stores, and the array elements are all the same. 3125 const TypeAryPtr* aryptr_t1 = _store->adr_type()->is_aryptr(); 3126 const TypeAryPtr* aryptr_t2 = other_store->adr_type()->is_aryptr(); 3127 BasicType aryptr_bt1 = aryptr_t1->elem()->array_element_basic_type(); 3128 BasicType aryptr_bt2 = aryptr_t2->elem()->array_element_basic_type(); 3129 if (!is_java_primitive(aryptr_bt1) || !is_java_primitive(aryptr_bt2)) { 3130 return false; 3131 } 3132 int size1 = type2aelembytes(aryptr_bt1); 3133 int size2 = type2aelembytes(aryptr_bt2); 3134 if (size1 != size2 || 3135 size1 != _store->memory_size() || 3136 _store->memory_size() != other_store->memory_size()) { 3137 return false; 3138 } 3139 return true; 3140 } 3141 3142 bool MergePrimitiveArrayStores::is_adjacent_pair(const StoreNode* use_store, const StoreNode* def_store) const { 3143 if (!is_adjacent_input_pair(def_store->in(MemNode::ValueIn), 3144 use_store->in(MemNode::ValueIn), 3145 def_store->memory_size())) { 3146 return false; 3147 } 3148 3149 ResourceMark rm; 3150 ArrayPointer array_pointer_use = ArrayPointer::make(_phase, use_store->in(MemNode::Address)); 3151 ArrayPointer array_pointer_def = ArrayPointer::make(_phase, def_store->in(MemNode::Address)); 3152 if (!array_pointer_def.is_adjacent_to_and_before(array_pointer_use, use_store->memory_size())) { 3153 return false; 3154 } 3155 3156 return true; 3157 } 3158 3159 bool MergePrimitiveArrayStores::is_adjacent_input_pair(const Node* n1, const Node* n2, const int memory_size) const { 3160 // Pattern: [n1 = ConI, n2 = ConI] 3161 if (n1->Opcode() == Op_ConI) { 3162 return n2->Opcode() == Op_ConI; 3163 } 3164 3165 // Pattern: [n1 = base >> shift, n2 = base >> (shift + memory_size)] 3166 #ifndef VM_LITTLE_ENDIAN 3167 // Pattern: [n1 = base >> (shift + memory_size), n2 = base >> shift] 3168 // Swapping n1 with n2 gives same pattern as on little endian platforms. 3169 swap(n1, n2); 3170 #endif // !VM_LITTLE_ENDIAN 3171 Node const* base_n2; 3172 jint shift_n2; 3173 if (!is_con_RShift(n2, base_n2, shift_n2)) { 3174 return false; 3175 } 3176 if (n1->Opcode() == Op_ConvL2I) { 3177 // look through 3178 n1 = n1->in(1); 3179 } 3180 Node const* base_n1; 3181 jint shift_n1; 3182 if (n1 == base_n2) { 3183 // n1 = base = base >> 0 3184 base_n1 = n1; 3185 shift_n1 = 0; 3186 } else if (!is_con_RShift(n1, base_n1, shift_n1)) { 3187 return false; 3188 } 3189 int bits_per_store = memory_size * 8; 3190 if (base_n1 != base_n2 || 3191 shift_n1 + bits_per_store != shift_n2 || 3192 shift_n1 % bits_per_store != 0) { 3193 return false; 3194 } 3195 3196 // both load from same value with correct shift 3197 return true; 3198 } 3199 3200 // Detect pattern: n = base_out >> shift_out 3201 bool MergePrimitiveArrayStores::is_con_RShift(const Node* n, Node const*& base_out, jint& shift_out) { 3202 assert(n != nullptr, "precondition"); 3203 3204 int opc = n->Opcode(); 3205 if (opc == Op_ConvL2I) { 3206 n = n->in(1); 3207 opc = n->Opcode(); 3208 } 3209 3210 if ((opc == Op_RShiftI || 3211 opc == Op_RShiftL || 3212 opc == Op_URShiftI || 3213 opc == Op_URShiftL) && 3214 n->in(2)->is_ConI()) { 3215 base_out = n->in(1); 3216 shift_out = n->in(2)->get_int(); 3217 // The shift must be positive: 3218 return shift_out >= 0; 3219 } 3220 return false; 3221 } 3222 3223 // Check if there is nothing between the two stores, except optionally a RangeCheck leading to an uncommon trap. 3224 MergePrimitiveArrayStores::CFGStatus MergePrimitiveArrayStores::cfg_status_for_pair(const StoreNode* use_store, const StoreNode* def_store) { 3225 assert(use_store->in(MemNode::Memory) == def_store, "use-def relationship"); 3226 3227 Node* ctrl_use = use_store->in(MemNode::Control); 3228 Node* ctrl_def = def_store->in(MemNode::Control); 3229 if (ctrl_use == nullptr || ctrl_def == nullptr) { 3230 return CFGStatus::Failure; 3231 } 3232 3233 if (ctrl_use == ctrl_def) { 3234 // Same ctrl -> no RangeCheck in between. 3235 // Check: use_store must be the only use of def_store. 3236 if (def_store->outcnt() > 1) { 3237 return CFGStatus::Failure; 3238 } 3239 return CFGStatus::SuccessNoRangeCheck; 3240 } 3241 3242 // Different ctrl -> could have RangeCheck in between. 3243 // Check: 1. def_store only has these uses: use_store and MergeMem for uncommon trap, and 3244 // 2. ctrl separated by RangeCheck. 3245 if (def_store->outcnt() != 2) { 3246 return CFGStatus::Failure; // Cannot have exactly these uses: use_store and MergeMem for uncommon trap. 3247 } 3248 int use_store_out_idx = def_store->raw_out(0) == use_store ? 0 : 1; 3249 Node* merge_mem = def_store->raw_out(1 - use_store_out_idx)->isa_MergeMem(); 3250 if (merge_mem == nullptr || 3251 merge_mem->outcnt() != 1) { 3252 return CFGStatus::Failure; // Does not have MergeMem for uncommon trap. 3253 } 3254 if (!ctrl_use->is_IfProj() || 3255 !ctrl_use->in(0)->is_RangeCheck() || 3256 ctrl_use->in(0)->outcnt() != 2) { 3257 return CFGStatus::Failure; // Not RangeCheck. 3258 } 3259 ProjNode* other_proj = ctrl_use->as_IfProj()->other_if_proj(); 3260 Node* trap = other_proj->is_uncommon_trap_proj(Deoptimization::Reason_range_check); 3261 if (trap != merge_mem->unique_out() || 3262 ctrl_use->in(0)->in(0) != ctrl_def) { 3263 return CFGStatus::Failure; // Not RangeCheck with merge_mem leading to uncommon trap. 3264 } 3265 3266 return CFGStatus::SuccessWithRangeCheck; 3267 } 3268 3269 MergePrimitiveArrayStores::Status MergePrimitiveArrayStores::find_adjacent_use_store(const StoreNode* def_store) const { 3270 Status status_use = find_use_store(def_store); 3271 StoreNode* use_store = status_use.found_store(); 3272 if (use_store != nullptr && !is_adjacent_pair(use_store, def_store)) { 3273 return Status::make_failure(); 3274 } 3275 return status_use; 3276 } 3277 3278 MergePrimitiveArrayStores::Status MergePrimitiveArrayStores::find_adjacent_def_store(const StoreNode* use_store) const { 3279 Status status_def = find_def_store(use_store); 3280 StoreNode* def_store = status_def.found_store(); 3281 if (def_store != nullptr && !is_adjacent_pair(use_store, def_store)) { 3282 return Status::make_failure(); 3283 } 3284 return status_def; 3285 } 3286 3287 MergePrimitiveArrayStores::Status MergePrimitiveArrayStores::find_use_store(const StoreNode* def_store) const { 3288 Status status_use = find_use_store_unidirectional(def_store); 3289 3290 #ifdef ASSERT 3291 StoreNode* use_store = status_use.found_store(); 3292 if (use_store != nullptr) { 3293 Status status_def = find_def_store_unidirectional(use_store); 3294 assert(status_def.found_store() == def_store && 3295 status_def.found_range_check() == status_use.found_range_check(), 3296 "find_use_store and find_def_store must be symmetric"); 3297 } 3298 #endif 3299 3300 return status_use; 3301 } 3302 3303 MergePrimitiveArrayStores::Status MergePrimitiveArrayStores::find_def_store(const StoreNode* use_store) const { 3304 Status status_def = find_def_store_unidirectional(use_store); 3305 3306 #ifdef ASSERT 3307 StoreNode* def_store = status_def.found_store(); 3308 if (def_store != nullptr) { 3309 Status status_use = find_use_store_unidirectional(def_store); 3310 assert(status_use.found_store() == use_store && 3311 status_use.found_range_check() == status_def.found_range_check(), 3312 "find_use_store and find_def_store must be symmetric"); 3313 } 3314 #endif 3315 3316 return status_def; 3317 } 3318 3319 MergePrimitiveArrayStores::Status MergePrimitiveArrayStores::find_use_store_unidirectional(const StoreNode* def_store) const { 3320 assert(is_compatible_store(def_store), "precondition: must be compatible with _store"); 3321 3322 for (DUIterator_Fast imax, i = def_store->fast_outs(imax); i < imax; i++) { 3323 StoreNode* use_store = def_store->fast_out(i)->isa_Store(); 3324 if (is_compatible_store(use_store)) { 3325 return Status::make(use_store, cfg_status_for_pair(use_store, def_store)); 3326 } 3327 } 3328 3329 return Status::make_failure(); 3330 } 3331 3332 MergePrimitiveArrayStores::Status MergePrimitiveArrayStores::find_def_store_unidirectional(const StoreNode* use_store) const { 3333 assert(is_compatible_store(use_store), "precondition: must be compatible with _store"); 3334 3335 StoreNode* def_store = use_store->in(MemNode::Memory)->isa_Store(); 3336 if (!is_compatible_store(def_store)) { 3337 return Status::make_failure(); 3338 } 3339 3340 return Status::make(def_store, cfg_status_for_pair(use_store, def_store)); 3341 } 3342 3343 void MergePrimitiveArrayStores::collect_merge_list(Node_List& merge_list) const { 3344 // The merged store can be at most 8 bytes. 3345 const uint merge_list_max_size = 8 / _store->memory_size(); 3346 assert(merge_list_max_size >= 2 && 3347 merge_list_max_size <= 8 && 3348 is_power_of_2(merge_list_max_size), 3349 "must be 2, 4 or 8"); 3350 3351 // Traverse up the chain of adjacent def stores. 3352 StoreNode* current = _store; 3353 merge_list.push(current); 3354 while (current != nullptr && merge_list.size() < merge_list_max_size) { 3355 Status status = find_adjacent_def_store(current); 3356 current = status.found_store(); 3357 if (current != nullptr) { 3358 merge_list.push(current); 3359 3360 // We can have at most one RangeCheck. 3361 if (status.found_range_check()) { 3362 break; 3363 } 3364 } 3365 } 3366 3367 // Truncate the merge_list to a power of 2. 3368 const uint pow2size = round_down_power_of_2(merge_list.size()); 3369 assert(pow2size >= 2, "must be merging at least 2 stores"); 3370 while (merge_list.size() > pow2size) { merge_list.pop(); } 3371 } 3372 3373 // Merge the input values of the smaller stores to a single larger input value. 3374 Node* MergePrimitiveArrayStores::make_merged_input_value(const Node_List& merge_list) { 3375 int new_memory_size = _store->memory_size() * merge_list.size(); 3376 Node* first = merge_list.at(merge_list.size()-1); 3377 Node* merged_input_value = nullptr; 3378 if (_store->in(MemNode::ValueIn)->Opcode() == Op_ConI) { 3379 // Pattern: [ConI, ConI, ...] -> new constant 3380 jlong con = 0; 3381 jlong bits_per_store = _store->memory_size() * 8; 3382 jlong mask = (((jlong)1) << bits_per_store) - 1; 3383 for (uint i = 0; i < merge_list.size(); i++) { 3384 jlong con_i = merge_list.at(i)->in(MemNode::ValueIn)->get_int(); 3385 #ifdef VM_LITTLE_ENDIAN 3386 con = con << bits_per_store; 3387 con = con | (mask & con_i); 3388 #else // VM_LITTLE_ENDIAN 3389 con_i = (mask & con_i) << (i * bits_per_store); 3390 con = con | con_i; 3391 #endif // VM_LITTLE_ENDIAN 3392 } 3393 merged_input_value = _phase->longcon(con); 3394 } else { 3395 // Pattern: [base >> 24, base >> 16, base >> 8, base] -> base 3396 // | | 3397 // _store first 3398 // 3399 Node* hi = _store->in(MemNode::ValueIn); 3400 Node* lo = first->in(MemNode::ValueIn); 3401 #ifndef VM_LITTLE_ENDIAN 3402 // `_store` and `first` are swapped in the diagram above 3403 swap(hi, lo); 3404 #endif // !VM_LITTLE_ENDIAN 3405 Node const* hi_base; 3406 jint hi_shift; 3407 merged_input_value = lo; 3408 bool is_true = is_con_RShift(hi, hi_base, hi_shift); 3409 assert(is_true, "must detect con RShift"); 3410 if (merged_input_value != hi_base && merged_input_value->Opcode() == Op_ConvL2I) { 3411 // look through 3412 merged_input_value = merged_input_value->in(1); 3413 } 3414 if (merged_input_value != hi_base) { 3415 // merged_input_value is not the base 3416 return nullptr; 3417 } 3418 } 3419 3420 if (_phase->type(merged_input_value)->isa_long() != nullptr && new_memory_size <= 4) { 3421 // Example: 3422 // 3423 // long base = ...; 3424 // a[0] = (byte)(base >> 0); 3425 // a[1] = (byte)(base >> 8); 3426 // 3427 merged_input_value = _phase->transform(new ConvL2INode(merged_input_value)); 3428 } 3429 3430 assert((_phase->type(merged_input_value)->isa_int() != nullptr && new_memory_size <= 4) || 3431 (_phase->type(merged_input_value)->isa_long() != nullptr && new_memory_size == 8), 3432 "merged_input_value is either int or long, and new_memory_size is small enough"); 3433 3434 return merged_input_value; 3435 } 3436 3437 // // 3438 // first_ctrl first_mem first_adr first_ctrl first_mem first_adr // 3439 // | | | | | | // 3440 // | | | | +---------------+ | // 3441 // | | | | | | | // 3442 // | | +---------+ | | +---------------+ // 3443 // | | | | | | | | // 3444 // +--------------+ | | v1 +------------------------------+ | | v1 // 3445 // | | | | | | | | | | | | // 3446 // RangeCheck first_store RangeCheck | | first_store // 3447 // | | | | | | | // 3448 // last_ctrl | +----> unc_trap last_ctrl | | +----> unc_trap // 3449 // | | ===> | | | // 3450 // +--------------+ | a2 v2 | | | // 3451 // | | | | | | | | // 3452 // | second_store | | | // 3453 // | | | | | [v1 v2 ... vn] // 3454 // ... ... | | | | // 3455 // | | | | | v // 3456 // +--------------+ | an vn +--------------+ | | merged_input_value // 3457 // | | | | | | | | // 3458 // last_store (= _store) merged_store // 3459 // // 3460 StoreNode* MergePrimitiveArrayStores::make_merged_store(const Node_List& merge_list, Node* merged_input_value) { 3461 Node* first_store = merge_list.at(merge_list.size()-1); 3462 Node* last_ctrl = _store->in(MemNode::Control); // after (optional) RangeCheck 3463 Node* first_mem = first_store->in(MemNode::Memory); 3464 Node* first_adr = first_store->in(MemNode::Address); 3465 3466 const TypePtr* new_adr_type = _store->adr_type(); 3467 3468 int new_memory_size = _store->memory_size() * merge_list.size(); 3469 BasicType bt = T_ILLEGAL; 3470 switch (new_memory_size) { 3471 case 2: bt = T_SHORT; break; 3472 case 4: bt = T_INT; break; 3473 case 8: bt = T_LONG; break; 3474 } 3475 3476 StoreNode* merged_store = StoreNode::make(*_phase, last_ctrl, first_mem, first_adr, 3477 new_adr_type, merged_input_value, bt, MemNode::unordered); 3478 3479 // Marking the store mismatched is sufficient to prevent reordering, since array stores 3480 // are all on the same slice. Hence, we need no barriers. 3481 merged_store->set_mismatched_access(); 3482 3483 // Constants above may now also be be packed -> put candidate on worklist 3484 _phase->is_IterGVN()->_worklist.push(first_mem); 3485 3486 return merged_store; 3487 } 3488 3489 #ifdef ASSERT 3490 void MergePrimitiveArrayStores::trace(const Node_List& merge_list, const Node* merged_input_value, const StoreNode* merged_store) const { 3491 stringStream ss; 3492 ss.print_cr("[TraceMergeStores]: Replace"); 3493 for (int i = (int)merge_list.size() - 1; i >= 0; i--) { 3494 merge_list.at(i)->dump("\n", false, &ss); 3495 } 3496 ss.print_cr("[TraceMergeStores]: with"); 3497 merged_input_value->dump("\n", false, &ss); 3498 merged_store->dump("\n", false, &ss); 3499 tty->print("%s", ss.as_string()); 3500 } 3501 #endif 3502 3503 //------------------------------Ideal------------------------------------------ 3504 // Change back-to-back Store(, p, x) -> Store(m, p, y) to Store(m, p, x). 3505 // When a store immediately follows a relevant allocation/initialization, 3506 // try to capture it into the initialization, or hoist it above. 3507 Node *StoreNode::Ideal(PhaseGVN *phase, bool can_reshape) { 3508 Node* p = MemNode::Ideal_common(phase, can_reshape); 3509 if (p) return (p == NodeSentinel) ? nullptr : p; 3510 3511 Node* mem = in(MemNode::Memory); 3512 Node* address = in(MemNode::Address); 3513 Node* value = in(MemNode::ValueIn); 3514 // Back-to-back stores to same address? Fold em up. Generally 3515 // unsafe if I have intervening uses... Also disallowed for StoreCM 3516 // since they must follow each StoreP operation. Redundant StoreCMs 3517 // are eliminated just before matching in final_graph_reshape. 3518 if (phase->C->get_adr_type(phase->C->get_alias_index(adr_type())) != TypeAryPtr::INLINES) { 3519 Node* st = mem; 3520 // If Store 'st' has more than one use, we cannot fold 'st' away. 3521 // For example, 'st' might be the final state at a conditional 3522 // return. Or, 'st' might be used by some node which is live at 3523 // the same time 'st' is live, which might be unschedulable. So, 3524 // require exactly ONE user until such time as we clone 'mem' for 3525 // each of 'mem's uses (thus making the exactly-1-user-rule hold 3526 // true). 3527 while (st->is_Store() && st->outcnt() == 1 && st->Opcode() != Op_StoreCM) { 3528 // Looking at a dead closed cycle of memory? 3529 assert(st != st->in(MemNode::Memory), "dead loop in StoreNode::Ideal"); 3530 assert(Opcode() == st->Opcode() || 3531 st->Opcode() == Op_StoreVector || 3532 Opcode() == Op_StoreVector || 3533 st->Opcode() == Op_StoreVectorScatter || 3534 Opcode() == Op_StoreVectorScatter || 3535 phase->C->get_alias_index(adr_type()) == Compile::AliasIdxRaw || 3536 (Opcode() == Op_StoreL && st->Opcode() == Op_StoreI) || // expanded ClearArrayNode 3537 (Opcode() == Op_StoreI && st->Opcode() == Op_StoreL) || // initialization by arraycopy 3538 (Opcode() == Op_StoreL && st->Opcode() == Op_StoreN) || 3539 (is_mismatched_access() || st->as_Store()->is_mismatched_access()), 3540 "no mismatched stores, except on raw memory: %s %s", NodeClassNames[Opcode()], NodeClassNames[st->Opcode()]); 3541 3542 if (st->in(MemNode::Address)->eqv_uncast(address) && 3543 st->as_Store()->memory_size() <= this->memory_size()) { 3544 Node* use = st->raw_out(0); 3545 if (phase->is_IterGVN()) { 3546 phase->is_IterGVN()->rehash_node_delayed(use); 3547 } 3548 // It's OK to do this in the parser, since DU info is always accurate, 3549 // and the parser always refers to nodes via SafePointNode maps. 3550 use->set_req_X(MemNode::Memory, st->in(MemNode::Memory), phase); 3551 return this; 3552 } 3553 st = st->in(MemNode::Memory); 3554 } 3555 } 3556 3557 3558 // Capture an unaliased, unconditional, simple store into an initializer. 3559 // Or, if it is independent of the allocation, hoist it above the allocation. 3560 if (ReduceFieldZeroing && /*can_reshape &&*/ 3561 mem->is_Proj() && mem->in(0)->is_Initialize()) { 3562 InitializeNode* init = mem->in(0)->as_Initialize(); 3563 intptr_t offset = init->can_capture_store(this, phase, can_reshape); 3564 if (offset > 0) { 3565 Node* moved = init->capture_store(this, offset, phase, can_reshape); 3566 // If the InitializeNode captured me, it made a raw copy of me, 3567 // and I need to disappear. 3568 if (moved != nullptr) { 3569 // %%% hack to ensure that Ideal returns a new node: 3570 mem = MergeMemNode::make(mem); 3571 return mem; // fold me away 3572 } 3573 } 3574 } 3575 3576 // Fold reinterpret cast into memory operation: 3577 // StoreX mem (MoveY2X v) => StoreY mem v 3578 if (value->is_Move()) { 3579 const Type* vt = value->in(1)->bottom_type(); 3580 if (has_reinterpret_variant(vt)) { 3581 if (phase->C->post_loop_opts_phase()) { 3582 return convert_to_reinterpret_store(*phase, value->in(1), vt); 3583 } else { 3584 phase->C->record_for_post_loop_opts_igvn(this); // attempt the transformation once loop opts are over 3585 } 3586 } 3587 } 3588 3589 if (MergeStores && UseUnalignedAccesses) { 3590 if (phase->C->post_loop_opts_phase()) { 3591 MergePrimitiveArrayStores merge(phase, this); 3592 Node* progress = merge.run(); 3593 if (progress != nullptr) { return progress; } 3594 } else { 3595 phase->C->record_for_post_loop_opts_igvn(this); 3596 } 3597 } 3598 3599 return nullptr; // No further progress 3600 } 3601 3602 //------------------------------Value----------------------------------------- 3603 const Type* StoreNode::Value(PhaseGVN* phase) const { 3604 // Either input is TOP ==> the result is TOP 3605 const Type *t1 = phase->type( in(MemNode::Memory) ); 3606 if( t1 == Type::TOP ) return Type::TOP; 3607 const Type *t2 = phase->type( in(MemNode::Address) ); 3608 if( t2 == Type::TOP ) return Type::TOP; 3609 const Type *t3 = phase->type( in(MemNode::ValueIn) ); 3610 if( t3 == Type::TOP ) return Type::TOP; 3611 return Type::MEMORY; 3612 } 3613 3614 //------------------------------Identity--------------------------------------- 3615 // Remove redundant stores: 3616 // Store(m, p, Load(m, p)) changes to m. 3617 // Store(, p, x) -> Store(m, p, x) changes to Store(m, p, x). 3618 Node* StoreNode::Identity(PhaseGVN* phase) { 3619 Node* mem = in(MemNode::Memory); 3620 Node* adr = in(MemNode::Address); 3621 Node* val = in(MemNode::ValueIn); 3622 3623 Node* result = this; 3624 3625 // Load then Store? Then the Store is useless 3626 if (val->is_Load() && 3627 val->in(MemNode::Address)->eqv_uncast(adr) && 3628 val->in(MemNode::Memory )->eqv_uncast(mem) && 3629 val->as_Load()->store_Opcode() == Opcode()) { 3630 // Ensure vector type is the same 3631 if (!is_StoreVector() || (mem->is_LoadVector() && as_StoreVector()->vect_type() == mem->as_LoadVector()->vect_type())) { 3632 result = mem; 3633 } 3634 } 3635 3636 // Two stores in a row of the same value? 3637 if (result == this && 3638 mem->is_Store() && 3639 mem->in(MemNode::Address)->eqv_uncast(adr) && 3640 mem->in(MemNode::ValueIn)->eqv_uncast(val) && 3641 mem->Opcode() == Opcode()) { 3642 if (!is_StoreVector()) { 3643 result = mem; 3644 } else { 3645 const StoreVectorNode* store_vector = as_StoreVector(); 3646 const StoreVectorNode* mem_vector = mem->as_StoreVector(); 3647 const Node* store_indices = store_vector->indices(); 3648 const Node* mem_indices = mem_vector->indices(); 3649 const Node* store_mask = store_vector->mask(); 3650 const Node* mem_mask = mem_vector->mask(); 3651 // Ensure types, indices, and masks match 3652 if (store_vector->vect_type() == mem_vector->vect_type() && 3653 ((store_indices == nullptr) == (mem_indices == nullptr) && 3654 (store_indices == nullptr || store_indices->eqv_uncast(mem_indices))) && 3655 ((store_mask == nullptr) == (mem_mask == nullptr) && 3656 (store_mask == nullptr || store_mask->eqv_uncast(mem_mask)))) { 3657 result = mem; 3658 } 3659 } 3660 } 3661 3662 // Store of zero anywhere into a freshly-allocated object? 3663 // Then the store is useless. 3664 // (It must already have been captured by the InitializeNode.) 3665 if (result == this && ReduceFieldZeroing) { 3666 // a newly allocated object is already all-zeroes everywhere 3667 if (mem->is_Proj() && mem->in(0)->is_Allocate() && 3668 (phase->type(val)->is_zero_type() || mem->in(0)->in(AllocateNode::DefaultValue) == val)) { 3669 result = mem; 3670 } 3671 3672 if (result == this && phase->type(val)->is_zero_type()) { 3673 // the store may also apply to zero-bits in an earlier object 3674 Node* prev_mem = find_previous_store(phase); 3675 // Steps (a), (b): Walk past independent stores to find an exact match. 3676 if (prev_mem != nullptr) { 3677 Node* prev_val = can_see_stored_value(prev_mem, phase); 3678 if (prev_val != nullptr && prev_val == val) { 3679 // prev_val and val might differ by a cast; it would be good 3680 // to keep the more informative of the two. 3681 result = mem; 3682 } 3683 } 3684 } 3685 } 3686 3687 PhaseIterGVN* igvn = phase->is_IterGVN(); 3688 if (result != this && igvn != nullptr) { 3689 MemBarNode* trailing = trailing_membar(); 3690 if (trailing != nullptr) { 3691 #ifdef ASSERT 3692 const TypeOopPtr* t_oop = phase->type(in(Address))->isa_oopptr(); 3693 assert(t_oop == nullptr || t_oop->is_known_instance_field(), "only for non escaping objects"); 3694 #endif 3695 trailing->remove(igvn); 3696 } 3697 } 3698 3699 return result; 3700 } 3701 3702 //------------------------------match_edge------------------------------------- 3703 // Do we Match on this edge index or not? Match only memory & value 3704 uint StoreNode::match_edge(uint idx) const { 3705 return idx == MemNode::Address || idx == MemNode::ValueIn; 3706 } 3707 3708 //------------------------------cmp-------------------------------------------- 3709 // Do not common stores up together. They generally have to be split 3710 // back up anyways, so do not bother. 3711 bool StoreNode::cmp( const Node &n ) const { 3712 return (&n == this); // Always fail except on self 3713 } 3714 3715 //------------------------------Ideal_masked_input----------------------------- 3716 // Check for a useless mask before a partial-word store 3717 // (StoreB ... (AndI valIn conIa) ) 3718 // If (conIa & mask == mask) this simplifies to 3719 // (StoreB ... (valIn) ) 3720 Node *StoreNode::Ideal_masked_input(PhaseGVN *phase, uint mask) { 3721 Node *val = in(MemNode::ValueIn); 3722 if( val->Opcode() == Op_AndI ) { 3723 const TypeInt *t = phase->type( val->in(2) )->isa_int(); 3724 if( t && t->is_con() && (t->get_con() & mask) == mask ) { 3725 set_req_X(MemNode::ValueIn, val->in(1), phase); 3726 return this; 3727 } 3728 } 3729 return nullptr; 3730 } 3731 3732 3733 //------------------------------Ideal_sign_extended_input---------------------- 3734 // Check for useless sign-extension before a partial-word store 3735 // (StoreB ... (RShiftI _ (LShiftI _ valIn conIL ) conIR) ) 3736 // If (conIL == conIR && conIR <= num_bits) this simplifies to 3737 // (StoreB ... (valIn) ) 3738 Node *StoreNode::Ideal_sign_extended_input(PhaseGVN *phase, int num_bits) { 3739 Node *val = in(MemNode::ValueIn); 3740 if( val->Opcode() == Op_RShiftI ) { 3741 const TypeInt *t = phase->type( val->in(2) )->isa_int(); 3742 if( t && t->is_con() && (t->get_con() <= num_bits) ) { 3743 Node *shl = val->in(1); 3744 if( shl->Opcode() == Op_LShiftI ) { 3745 const TypeInt *t2 = phase->type( shl->in(2) )->isa_int(); 3746 if( t2 && t2->is_con() && (t2->get_con() == t->get_con()) ) { 3747 set_req_X(MemNode::ValueIn, shl->in(1), phase); 3748 return this; 3749 } 3750 } 3751 } 3752 } 3753 return nullptr; 3754 } 3755 3756 //------------------------------value_never_loaded----------------------------------- 3757 // Determine whether there are any possible loads of the value stored. 3758 // For simplicity, we actually check if there are any loads from the 3759 // address stored to, not just for loads of the value stored by this node. 3760 // 3761 bool StoreNode::value_never_loaded(PhaseValues* phase) const { 3762 Node *adr = in(Address); 3763 const TypeOopPtr *adr_oop = phase->type(adr)->isa_oopptr(); 3764 if (adr_oop == nullptr) 3765 return false; 3766 if (!adr_oop->is_known_instance_field()) 3767 return false; // if not a distinct instance, there may be aliases of the address 3768 for (DUIterator_Fast imax, i = adr->fast_outs(imax); i < imax; i++) { 3769 Node *use = adr->fast_out(i); 3770 if (use->is_Load() || use->is_LoadStore()) { 3771 return false; 3772 } 3773 } 3774 return true; 3775 } 3776 3777 MemBarNode* StoreNode::trailing_membar() const { 3778 if (is_release()) { 3779 MemBarNode* trailing_mb = nullptr; 3780 for (DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++) { 3781 Node* u = fast_out(i); 3782 if (u->is_MemBar()) { 3783 if (u->as_MemBar()->trailing_store()) { 3784 assert(u->Opcode() == Op_MemBarVolatile, ""); 3785 assert(trailing_mb == nullptr, "only one"); 3786 trailing_mb = u->as_MemBar(); 3787 #ifdef ASSERT 3788 Node* leading = u->as_MemBar()->leading_membar(); 3789 assert(leading->Opcode() == Op_MemBarRelease, "incorrect membar"); 3790 assert(leading->as_MemBar()->leading_store(), "incorrect membar pair"); 3791 assert(leading->as_MemBar()->trailing_membar() == u, "incorrect membar pair"); 3792 #endif 3793 } else { 3794 assert(u->as_MemBar()->standalone(), ""); 3795 } 3796 } 3797 } 3798 return trailing_mb; 3799 } 3800 return nullptr; 3801 } 3802 3803 3804 //============================================================================= 3805 //------------------------------Ideal------------------------------------------ 3806 // If the store is from an AND mask that leaves the low bits untouched, then 3807 // we can skip the AND operation. If the store is from a sign-extension 3808 // (a left shift, then right shift) we can skip both. 3809 Node *StoreBNode::Ideal(PhaseGVN *phase, bool can_reshape){ 3810 Node *progress = StoreNode::Ideal_masked_input(phase, 0xFF); 3811 if( progress != nullptr ) return progress; 3812 3813 progress = StoreNode::Ideal_sign_extended_input(phase, 24); 3814 if( progress != nullptr ) return progress; 3815 3816 // Finally check the default case 3817 return StoreNode::Ideal(phase, can_reshape); 3818 } 3819 3820 //============================================================================= 3821 //------------------------------Ideal------------------------------------------ 3822 // If the store is from an AND mask that leaves the low bits untouched, then 3823 // we can skip the AND operation 3824 Node *StoreCNode::Ideal(PhaseGVN *phase, bool can_reshape){ 3825 Node *progress = StoreNode::Ideal_masked_input(phase, 0xFFFF); 3826 if( progress != nullptr ) return progress; 3827 3828 progress = StoreNode::Ideal_sign_extended_input(phase, 16); 3829 if( progress != nullptr ) return progress; 3830 3831 // Finally check the default case 3832 return StoreNode::Ideal(phase, can_reshape); 3833 } 3834 3835 //============================================================================= 3836 //------------------------------Identity--------------------------------------- 3837 Node* StoreCMNode::Identity(PhaseGVN* phase) { 3838 // No need to card mark when storing a null ptr 3839 Node* my_store = in(MemNode::OopStore); 3840 if (my_store->is_Store()) { 3841 const Type *t1 = phase->type( my_store->in(MemNode::ValueIn) ); 3842 if( t1 == TypePtr::NULL_PTR ) { 3843 return in(MemNode::Memory); 3844 } 3845 } 3846 return this; 3847 } 3848 3849 //============================================================================= 3850 //------------------------------Ideal--------------------------------------- 3851 Node *StoreCMNode::Ideal(PhaseGVN *phase, bool can_reshape){ 3852 Node* progress = StoreNode::Ideal(phase, can_reshape); 3853 if (progress != nullptr) return progress; 3854 3855 Node* my_store = in(MemNode::OopStore); 3856 if (my_store->is_MergeMem()) { 3857 if (oop_alias_idx() != phase->C->get_alias_index(TypeAryPtr::INLINES) || 3858 phase->C->flat_accesses_share_alias()) { 3859 // The alias that was recorded is no longer accurate enough. 3860 Node* mem = my_store->as_MergeMem()->memory_at(oop_alias_idx()); 3861 set_req_X(MemNode::OopStore, mem, phase); 3862 return this; 3863 } 3864 } 3865 3866 return nullptr; 3867 } 3868 3869 //------------------------------Value----------------------------------------- 3870 const Type* StoreCMNode::Value(PhaseGVN* phase) const { 3871 // Either input is TOP ==> the result is TOP (checked in StoreNode::Value). 3872 // If extra input is TOP ==> the result is TOP 3873 const Type* t = phase->type(in(MemNode::OopStore)); 3874 if (t == Type::TOP) { 3875 return Type::TOP; 3876 } 3877 return StoreNode::Value(phase); 3878 } 3879 3880 3881 //============================================================================= 3882 //----------------------------------SCMemProjNode------------------------------ 3883 const Type* SCMemProjNode::Value(PhaseGVN* phase) const 3884 { 3885 if (in(0) == nullptr || phase->type(in(0)) == Type::TOP) { 3886 return Type::TOP; 3887 } 3888 return bottom_type(); 3889 } 3890 3891 //============================================================================= 3892 //----------------------------------LoadStoreNode------------------------------ 3893 LoadStoreNode::LoadStoreNode( Node *c, Node *mem, Node *adr, Node *val, const TypePtr* at, const Type* rt, uint required ) 3894 : Node(required), 3895 _type(rt), 3896 _adr_type(at), 3897 _barrier_data(0) 3898 { 3899 init_req(MemNode::Control, c ); 3900 init_req(MemNode::Memory , mem); 3901 init_req(MemNode::Address, adr); 3902 init_req(MemNode::ValueIn, val); 3903 init_class_id(Class_LoadStore); 3904 } 3905 3906 //------------------------------Value----------------------------------------- 3907 const Type* LoadStoreNode::Value(PhaseGVN* phase) const { 3908 // Either input is TOP ==> the result is TOP 3909 if (!in(MemNode::Control) || phase->type(in(MemNode::Control)) == Type::TOP) { 3910 return Type::TOP; 3911 } 3912 const Type* t = phase->type(in(MemNode::Memory)); 3913 if (t == Type::TOP) { 3914 return Type::TOP; 3915 } 3916 t = phase->type(in(MemNode::Address)); 3917 if (t == Type::TOP) { 3918 return Type::TOP; 3919 } 3920 t = phase->type(in(MemNode::ValueIn)); 3921 if (t == Type::TOP) { 3922 return Type::TOP; 3923 } 3924 return bottom_type(); 3925 } 3926 3927 uint LoadStoreNode::ideal_reg() const { 3928 return _type->ideal_reg(); 3929 } 3930 3931 // This method conservatively checks if the result of a LoadStoreNode is 3932 // used, that is, if it returns true, then it is definitely the case that 3933 // the result of the node is not needed. 3934 // For example, GetAndAdd can be matched into a lock_add instead of a 3935 // lock_xadd if the result of LoadStoreNode::result_not_used() is true 3936 bool LoadStoreNode::result_not_used() const { 3937 for (DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++) { 3938 Node *x = fast_out(i); 3939 if (x->Opcode() == Op_SCMemProj) { 3940 continue; 3941 } 3942 if (x->bottom_type() == TypeTuple::MEMBAR && 3943 !x->is_Call() && 3944 x->Opcode() != Op_Blackhole) { 3945 continue; 3946 } 3947 return false; 3948 } 3949 return true; 3950 } 3951 3952 MemBarNode* LoadStoreNode::trailing_membar() const { 3953 MemBarNode* trailing = nullptr; 3954 for (DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++) { 3955 Node* u = fast_out(i); 3956 if (u->is_MemBar()) { 3957 if (u->as_MemBar()->trailing_load_store()) { 3958 assert(u->Opcode() == Op_MemBarAcquire, ""); 3959 assert(trailing == nullptr, "only one"); 3960 trailing = u->as_MemBar(); 3961 #ifdef ASSERT 3962 Node* leading = trailing->leading_membar(); 3963 assert(support_IRIW_for_not_multiple_copy_atomic_cpu || leading->Opcode() == Op_MemBarRelease, "incorrect membar"); 3964 assert(leading->as_MemBar()->leading_load_store(), "incorrect membar pair"); 3965 assert(leading->as_MemBar()->trailing_membar() == trailing, "incorrect membar pair"); 3966 #endif 3967 } else { 3968 assert(u->as_MemBar()->standalone(), "wrong barrier kind"); 3969 } 3970 } 3971 } 3972 3973 return trailing; 3974 } 3975 3976 uint LoadStoreNode::size_of() const { return sizeof(*this); } 3977 3978 //============================================================================= 3979 //----------------------------------LoadStoreConditionalNode-------------------- 3980 LoadStoreConditionalNode::LoadStoreConditionalNode( Node *c, Node *mem, Node *adr, Node *val, Node *ex ) : LoadStoreNode(c, mem, adr, val, nullptr, TypeInt::BOOL, 5) { 3981 init_req(ExpectedIn, ex ); 3982 } 3983 3984 const Type* LoadStoreConditionalNode::Value(PhaseGVN* phase) const { 3985 // Either input is TOP ==> the result is TOP 3986 const Type* t = phase->type(in(ExpectedIn)); 3987 if (t == Type::TOP) { 3988 return Type::TOP; 3989 } 3990 return LoadStoreNode::Value(phase); 3991 } 3992 3993 //============================================================================= 3994 //-------------------------------adr_type-------------------------------------- 3995 const TypePtr* ClearArrayNode::adr_type() const { 3996 Node *adr = in(3); 3997 if (adr == nullptr) return nullptr; // node is dead 3998 return MemNode::calculate_adr_type(adr->bottom_type()); 3999 } 4000 4001 //------------------------------match_edge------------------------------------- 4002 // Do we Match on this edge index or not? Do not match memory 4003 uint ClearArrayNode::match_edge(uint idx) const { 4004 return idx > 1; 4005 } 4006 4007 //------------------------------Identity--------------------------------------- 4008 // Clearing a zero length array does nothing 4009 Node* ClearArrayNode::Identity(PhaseGVN* phase) { 4010 return phase->type(in(2))->higher_equal(TypeX::ZERO) ? in(1) : this; 4011 } 4012 4013 //------------------------------Idealize--------------------------------------- 4014 // Clearing a short array is faster with stores 4015 Node *ClearArrayNode::Ideal(PhaseGVN *phase, bool can_reshape) { 4016 // Already know this is a large node, do not try to ideal it 4017 if (_is_large) return nullptr; 4018 4019 const int unit = BytesPerLong; 4020 const TypeX* t = phase->type(in(2))->isa_intptr_t(); 4021 if (!t) return nullptr; 4022 if (!t->is_con()) return nullptr; 4023 intptr_t raw_count = t->get_con(); 4024 intptr_t size = raw_count; 4025 if (!Matcher::init_array_count_is_in_bytes) size *= unit; 4026 // Clearing nothing uses the Identity call. 4027 // Negative clears are possible on dead ClearArrays 4028 // (see jck test stmt114.stmt11402.val). 4029 if (size <= 0 || size % unit != 0) return nullptr; 4030 intptr_t count = size / unit; 4031 // Length too long; communicate this to matchers and assemblers. 4032 // Assemblers are responsible to produce fast hardware clears for it. 4033 if (size > InitArrayShortSize) { 4034 return new ClearArrayNode(in(0), in(1), in(2), in(3), in(4), true); 4035 } else if (size > 2 && Matcher::match_rule_supported_vector(Op_ClearArray, 4, T_LONG)) { 4036 return nullptr; 4037 } 4038 if (!IdealizeClearArrayNode) return nullptr; 4039 Node *mem = in(1); 4040 if( phase->type(mem)==Type::TOP ) return nullptr; 4041 Node *adr = in(3); 4042 const Type* at = phase->type(adr); 4043 if( at==Type::TOP ) return nullptr; 4044 const TypePtr* atp = at->isa_ptr(); 4045 // adjust atp to be the correct array element address type 4046 if (atp == nullptr) atp = TypePtr::BOTTOM; 4047 else atp = atp->add_offset(Type::OffsetBot); 4048 // Get base for derived pointer purposes 4049 if( adr->Opcode() != Op_AddP ) Unimplemented(); 4050 Node *base = adr->in(1); 4051 4052 Node *val = in(4); 4053 Node *off = phase->MakeConX(BytesPerLong); 4054 mem = new StoreLNode(in(0), mem, adr, atp, val, MemNode::unordered, false); 4055 count--; 4056 while( count-- ) { 4057 mem = phase->transform(mem); 4058 adr = phase->transform(new AddPNode(base,adr,off)); 4059 mem = new StoreLNode(in(0), mem, adr, atp, val, MemNode::unordered, false); 4060 } 4061 return mem; 4062 } 4063 4064 //----------------------------step_through---------------------------------- 4065 // Return allocation input memory edge if it is different instance 4066 // or itself if it is the one we are looking for. 4067 bool ClearArrayNode::step_through(Node** np, uint instance_id, PhaseValues* phase) { 4068 Node* n = *np; 4069 assert(n->is_ClearArray(), "sanity"); 4070 intptr_t offset; 4071 AllocateNode* alloc = AllocateNode::Ideal_allocation(n->in(3), phase, offset); 4072 // This method is called only before Allocate nodes are expanded 4073 // during macro nodes expansion. Before that ClearArray nodes are 4074 // only generated in PhaseMacroExpand::generate_arraycopy() (before 4075 // Allocate nodes are expanded) which follows allocations. 4076 assert(alloc != nullptr, "should have allocation"); 4077 if (alloc->_idx == instance_id) { 4078 // Can not bypass initialization of the instance we are looking for. 4079 return false; 4080 } 4081 // Otherwise skip it. 4082 InitializeNode* init = alloc->initialization(); 4083 if (init != nullptr) 4084 *np = init->in(TypeFunc::Memory); 4085 else 4086 *np = alloc->in(TypeFunc::Memory); 4087 return true; 4088 } 4089 4090 //----------------------------clear_memory------------------------------------- 4091 // Generate code to initialize object storage to zero. 4092 Node* ClearArrayNode::clear_memory(Node* ctl, Node* mem, Node* dest, 4093 Node* val, 4094 Node* raw_val, 4095 intptr_t start_offset, 4096 Node* end_offset, 4097 PhaseGVN* phase) { 4098 intptr_t offset = start_offset; 4099 4100 int unit = BytesPerLong; 4101 if ((offset % unit) != 0) { 4102 Node* adr = new AddPNode(dest, dest, phase->MakeConX(offset)); 4103 adr = phase->transform(adr); 4104 const TypePtr* atp = TypeRawPtr::BOTTOM; 4105 if (val != nullptr) { 4106 assert(phase->type(val)->isa_narrowoop(), "should be narrow oop"); 4107 mem = new StoreNNode(ctl, mem, adr, atp, val, MemNode::unordered); 4108 } else { 4109 assert(raw_val == nullptr, "val may not be null"); 4110 mem = StoreNode::make(*phase, ctl, mem, adr, atp, phase->zerocon(T_INT), T_INT, MemNode::unordered); 4111 } 4112 mem = phase->transform(mem); 4113 offset += BytesPerInt; 4114 } 4115 assert((offset % unit) == 0, ""); 4116 4117 // Initialize the remaining stuff, if any, with a ClearArray. 4118 return clear_memory(ctl, mem, dest, raw_val, phase->MakeConX(offset), end_offset, phase); 4119 } 4120 4121 Node* ClearArrayNode::clear_memory(Node* ctl, Node* mem, Node* dest, 4122 Node* raw_val, 4123 Node* start_offset, 4124 Node* end_offset, 4125 PhaseGVN* phase) { 4126 if (start_offset == end_offset) { 4127 // nothing to do 4128 return mem; 4129 } 4130 4131 int unit = BytesPerLong; 4132 Node* zbase = start_offset; 4133 Node* zend = end_offset; 4134 4135 // Scale to the unit required by the CPU: 4136 if (!Matcher::init_array_count_is_in_bytes) { 4137 Node* shift = phase->intcon(exact_log2(unit)); 4138 zbase = phase->transform(new URShiftXNode(zbase, shift) ); 4139 zend = phase->transform(new URShiftXNode(zend, shift) ); 4140 } 4141 4142 // Bulk clear double-words 4143 Node* zsize = phase->transform(new SubXNode(zend, zbase) ); 4144 Node* adr = phase->transform(new AddPNode(dest, dest, start_offset) ); 4145 if (raw_val == nullptr) { 4146 raw_val = phase->MakeConX(0); 4147 } 4148 mem = new ClearArrayNode(ctl, mem, zsize, adr, raw_val, false); 4149 return phase->transform(mem); 4150 } 4151 4152 Node* ClearArrayNode::clear_memory(Node* ctl, Node* mem, Node* dest, 4153 Node* val, 4154 Node* raw_val, 4155 intptr_t start_offset, 4156 intptr_t end_offset, 4157 PhaseGVN* phase) { 4158 if (start_offset == end_offset) { 4159 // nothing to do 4160 return mem; 4161 } 4162 4163 assert((end_offset % BytesPerInt) == 0, "odd end offset"); 4164 intptr_t done_offset = end_offset; 4165 if ((done_offset % BytesPerLong) != 0) { 4166 done_offset -= BytesPerInt; 4167 } 4168 if (done_offset > start_offset) { 4169 mem = clear_memory(ctl, mem, dest, val, raw_val, 4170 start_offset, phase->MakeConX(done_offset), phase); 4171 } 4172 if (done_offset < end_offset) { // emit the final 32-bit store 4173 Node* adr = new AddPNode(dest, dest, phase->MakeConX(done_offset)); 4174 adr = phase->transform(adr); 4175 const TypePtr* atp = TypeRawPtr::BOTTOM; 4176 if (val != nullptr) { 4177 assert(phase->type(val)->isa_narrowoop(), "should be narrow oop"); 4178 mem = new StoreNNode(ctl, mem, adr, atp, val, MemNode::unordered); 4179 } else { 4180 assert(raw_val == nullptr, "val may not be null"); 4181 mem = StoreNode::make(*phase, ctl, mem, adr, atp, phase->zerocon(T_INT), T_INT, MemNode::unordered); 4182 } 4183 mem = phase->transform(mem); 4184 done_offset += BytesPerInt; 4185 } 4186 assert(done_offset == end_offset, ""); 4187 return mem; 4188 } 4189 4190 //============================================================================= 4191 MemBarNode::MemBarNode(Compile* C, int alias_idx, Node* precedent) 4192 : MultiNode(TypeFunc::Parms + (precedent == nullptr? 0: 1)), 4193 _adr_type(C->get_adr_type(alias_idx)), _kind(Standalone) 4194 #ifdef ASSERT 4195 , _pair_idx(0) 4196 #endif 4197 { 4198 init_class_id(Class_MemBar); 4199 Node* top = C->top(); 4200 init_req(TypeFunc::I_O,top); 4201 init_req(TypeFunc::FramePtr,top); 4202 init_req(TypeFunc::ReturnAdr,top); 4203 if (precedent != nullptr) 4204 init_req(TypeFunc::Parms, precedent); 4205 } 4206 4207 //------------------------------cmp-------------------------------------------- 4208 uint MemBarNode::hash() const { return NO_HASH; } 4209 bool MemBarNode::cmp( const Node &n ) const { 4210 return (&n == this); // Always fail except on self 4211 } 4212 4213 //------------------------------make------------------------------------------- 4214 MemBarNode* MemBarNode::make(Compile* C, int opcode, int atp, Node* pn) { 4215 switch (opcode) { 4216 case Op_MemBarAcquire: return new MemBarAcquireNode(C, atp, pn); 4217 case Op_LoadFence: return new LoadFenceNode(C, atp, pn); 4218 case Op_MemBarRelease: return new MemBarReleaseNode(C, atp, pn); 4219 case Op_StoreFence: return new StoreFenceNode(C, atp, pn); 4220 case Op_MemBarStoreStore: return new MemBarStoreStoreNode(C, atp, pn); 4221 case Op_StoreStoreFence: return new StoreStoreFenceNode(C, atp, pn); 4222 case Op_MemBarAcquireLock: return new MemBarAcquireLockNode(C, atp, pn); 4223 case Op_MemBarReleaseLock: return new MemBarReleaseLockNode(C, atp, pn); 4224 case Op_MemBarVolatile: return new MemBarVolatileNode(C, atp, pn); 4225 case Op_MemBarCPUOrder: return new MemBarCPUOrderNode(C, atp, pn); 4226 case Op_OnSpinWait: return new OnSpinWaitNode(C, atp, pn); 4227 case Op_Initialize: return new InitializeNode(C, atp, pn); 4228 default: ShouldNotReachHere(); return nullptr; 4229 } 4230 } 4231 4232 void MemBarNode::remove(PhaseIterGVN *igvn) { 4233 if (outcnt() != 2) { 4234 assert(Opcode() == Op_Initialize, "Only seen when there are no use of init memory"); 4235 assert(outcnt() == 1, "Only control then"); 4236 } 4237 if (trailing_store() || trailing_load_store()) { 4238 MemBarNode* leading = leading_membar(); 4239 if (leading != nullptr) { 4240 assert(leading->trailing_membar() == this, "inconsistent leading/trailing membars"); 4241 leading->remove(igvn); 4242 } 4243 } 4244 if (proj_out_or_null(TypeFunc::Memory) != nullptr) { 4245 igvn->replace_node(proj_out(TypeFunc::Memory), in(TypeFunc::Memory)); 4246 } 4247 if (proj_out_or_null(TypeFunc::Control) != nullptr) { 4248 igvn->replace_node(proj_out(TypeFunc::Control), in(TypeFunc::Control)); 4249 } 4250 } 4251 4252 //------------------------------Ideal------------------------------------------ 4253 // Return a node which is more "ideal" than the current node. Strip out 4254 // control copies 4255 Node *MemBarNode::Ideal(PhaseGVN *phase, bool can_reshape) { 4256 if (remove_dead_region(phase, can_reshape)) return this; 4257 // Don't bother trying to transform a dead node 4258 if (in(0) && in(0)->is_top()) { 4259 return nullptr; 4260 } 4261 4262 bool progress = false; 4263 // Eliminate volatile MemBars for scalar replaced objects. 4264 if (can_reshape && req() == (Precedent+1)) { 4265 bool eliminate = false; 4266 int opc = Opcode(); 4267 if ((opc == Op_MemBarAcquire || opc == Op_MemBarVolatile)) { 4268 // Volatile field loads and stores. 4269 Node* my_mem = in(MemBarNode::Precedent); 4270 // The MembarAquire may keep an unused LoadNode alive through the Precedent edge 4271 if ((my_mem != nullptr) && (opc == Op_MemBarAcquire) && (my_mem->outcnt() == 1)) { 4272 // if the Precedent is a decodeN and its input (a Load) is used at more than one place, 4273 // replace this Precedent (decodeN) with the Load instead. 4274 if ((my_mem->Opcode() == Op_DecodeN) && (my_mem->in(1)->outcnt() > 1)) { 4275 Node* load_node = my_mem->in(1); 4276 set_req(MemBarNode::Precedent, load_node); 4277 phase->is_IterGVN()->_worklist.push(my_mem); 4278 my_mem = load_node; 4279 } else { 4280 assert(my_mem->unique_out() == this, "sanity"); 4281 assert(!trailing_load_store(), "load store node can't be eliminated"); 4282 del_req(Precedent); 4283 phase->is_IterGVN()->_worklist.push(my_mem); // remove dead node later 4284 my_mem = nullptr; 4285 } 4286 progress = true; 4287 } 4288 if (my_mem != nullptr && my_mem->is_Mem()) { 4289 const TypeOopPtr* t_oop = my_mem->in(MemNode::Address)->bottom_type()->isa_oopptr(); 4290 // Check for scalar replaced object reference. 4291 if( t_oop != nullptr && t_oop->is_known_instance_field() && 4292 t_oop->offset() != Type::OffsetBot && 4293 t_oop->offset() != Type::OffsetTop) { 4294 eliminate = true; 4295 } 4296 } 4297 } else if (opc == Op_MemBarRelease || (UseStoreStoreForCtor && opc == Op_MemBarStoreStore)) { 4298 // Final field stores. 4299 Node* alloc = AllocateNode::Ideal_allocation(in(MemBarNode::Precedent)); 4300 if ((alloc != nullptr) && alloc->is_Allocate() && 4301 alloc->as_Allocate()->does_not_escape_thread()) { 4302 // The allocated object does not escape. 4303 eliminate = true; 4304 } 4305 } 4306 if (eliminate) { 4307 // Replace MemBar projections by its inputs. 4308 PhaseIterGVN* igvn = phase->is_IterGVN(); 4309 remove(igvn); 4310 // Must return either the original node (now dead) or a new node 4311 // (Do not return a top here, since that would break the uniqueness of top.) 4312 return new ConINode(TypeInt::ZERO); 4313 } 4314 } 4315 return progress ? this : nullptr; 4316 } 4317 4318 //------------------------------Value------------------------------------------ 4319 const Type* MemBarNode::Value(PhaseGVN* phase) const { 4320 if( !in(0) ) return Type::TOP; 4321 if( phase->type(in(0)) == Type::TOP ) 4322 return Type::TOP; 4323 return TypeTuple::MEMBAR; 4324 } 4325 4326 //------------------------------match------------------------------------------ 4327 // Construct projections for memory. 4328 Node *MemBarNode::match(const ProjNode *proj, const Matcher *m, const RegMask* mask) { 4329 switch (proj->_con) { 4330 case TypeFunc::Control: 4331 case TypeFunc::Memory: 4332 return new MachProjNode(this,proj->_con,RegMask::Empty,MachProjNode::unmatched_proj); 4333 } 4334 ShouldNotReachHere(); 4335 return nullptr; 4336 } 4337 4338 void MemBarNode::set_store_pair(MemBarNode* leading, MemBarNode* trailing) { 4339 trailing->_kind = TrailingStore; 4340 leading->_kind = LeadingStore; 4341 #ifdef ASSERT 4342 trailing->_pair_idx = leading->_idx; 4343 leading->_pair_idx = leading->_idx; 4344 #endif 4345 } 4346 4347 void MemBarNode::set_load_store_pair(MemBarNode* leading, MemBarNode* trailing) { 4348 trailing->_kind = TrailingLoadStore; 4349 leading->_kind = LeadingLoadStore; 4350 #ifdef ASSERT 4351 trailing->_pair_idx = leading->_idx; 4352 leading->_pair_idx = leading->_idx; 4353 #endif 4354 } 4355 4356 MemBarNode* MemBarNode::trailing_membar() const { 4357 ResourceMark rm; 4358 Node* trailing = (Node*)this; 4359 VectorSet seen; 4360 Node_Stack multis(0); 4361 do { 4362 Node* c = trailing; 4363 uint i = 0; 4364 do { 4365 trailing = nullptr; 4366 for (; i < c->outcnt(); i++) { 4367 Node* next = c->raw_out(i); 4368 if (next != c && next->is_CFG()) { 4369 if (c->is_MultiBranch()) { 4370 if (multis.node() == c) { 4371 multis.set_index(i+1); 4372 } else { 4373 multis.push(c, i+1); 4374 } 4375 } 4376 trailing = next; 4377 break; 4378 } 4379 } 4380 if (trailing != nullptr && !seen.test_set(trailing->_idx)) { 4381 break; 4382 } 4383 while (multis.size() > 0) { 4384 c = multis.node(); 4385 i = multis.index(); 4386 if (i < c->req()) { 4387 break; 4388 } 4389 multis.pop(); 4390 } 4391 } while (multis.size() > 0); 4392 } while (!trailing->is_MemBar() || !trailing->as_MemBar()->trailing()); 4393 4394 MemBarNode* mb = trailing->as_MemBar(); 4395 assert((mb->_kind == TrailingStore && _kind == LeadingStore) || 4396 (mb->_kind == TrailingLoadStore && _kind == LeadingLoadStore), "bad trailing membar"); 4397 assert(mb->_pair_idx == _pair_idx, "bad trailing membar"); 4398 return mb; 4399 } 4400 4401 MemBarNode* MemBarNode::leading_membar() const { 4402 ResourceMark rm; 4403 VectorSet seen; 4404 Node_Stack regions(0); 4405 Node* leading = in(0); 4406 while (leading != nullptr && (!leading->is_MemBar() || !leading->as_MemBar()->leading())) { 4407 while (leading == nullptr || leading->is_top() || seen.test_set(leading->_idx)) { 4408 leading = nullptr; 4409 while (regions.size() > 0 && leading == nullptr) { 4410 Node* r = regions.node(); 4411 uint i = regions.index(); 4412 if (i < r->req()) { 4413 leading = r->in(i); 4414 regions.set_index(i+1); 4415 } else { 4416 regions.pop(); 4417 } 4418 } 4419 if (leading == nullptr) { 4420 assert(regions.size() == 0, "all paths should have been tried"); 4421 return nullptr; 4422 } 4423 } 4424 if (leading->is_Region()) { 4425 regions.push(leading, 2); 4426 leading = leading->in(1); 4427 } else { 4428 leading = leading->in(0); 4429 } 4430 } 4431 #ifdef ASSERT 4432 Unique_Node_List wq; 4433 wq.push((Node*)this); 4434 uint found = 0; 4435 for (uint i = 0; i < wq.size(); i++) { 4436 Node* n = wq.at(i); 4437 if (n->is_Region()) { 4438 for (uint j = 1; j < n->req(); j++) { 4439 Node* in = n->in(j); 4440 if (in != nullptr && !in->is_top()) { 4441 wq.push(in); 4442 } 4443 } 4444 } else { 4445 if (n->is_MemBar() && n->as_MemBar()->leading()) { 4446 assert(n == leading, "consistency check failed"); 4447 found++; 4448 } else { 4449 Node* in = n->in(0); 4450 if (in != nullptr && !in->is_top()) { 4451 wq.push(in); 4452 } 4453 } 4454 } 4455 } 4456 assert(found == 1 || (found == 0 && leading == nullptr), "consistency check failed"); 4457 #endif 4458 if (leading == nullptr) { 4459 return nullptr; 4460 } 4461 MemBarNode* mb = leading->as_MemBar(); 4462 assert((mb->_kind == LeadingStore && _kind == TrailingStore) || 4463 (mb->_kind == LeadingLoadStore && _kind == TrailingLoadStore), "bad leading membar"); 4464 assert(mb->_pair_idx == _pair_idx, "bad leading membar"); 4465 return mb; 4466 } 4467 4468 4469 //===========================InitializeNode==================================== 4470 // SUMMARY: 4471 // This node acts as a memory barrier on raw memory, after some raw stores. 4472 // The 'cooked' oop value feeds from the Initialize, not the Allocation. 4473 // The Initialize can 'capture' suitably constrained stores as raw inits. 4474 // It can coalesce related raw stores into larger units (called 'tiles'). 4475 // It can avoid zeroing new storage for memory units which have raw inits. 4476 // At macro-expansion, it is marked 'complete', and does not optimize further. 4477 // 4478 // EXAMPLE: 4479 // The object 'new short[2]' occupies 16 bytes in a 32-bit machine. 4480 // ctl = incoming control; mem* = incoming memory 4481 // (Note: A star * on a memory edge denotes I/O and other standard edges.) 4482 // First allocate uninitialized memory and fill in the header: 4483 // alloc = (Allocate ctl mem* 16 #short[].klass ...) 4484 // ctl := alloc.Control; mem* := alloc.Memory* 4485 // rawmem = alloc.Memory; rawoop = alloc.RawAddress 4486 // Then initialize to zero the non-header parts of the raw memory block: 4487 // init = (Initialize alloc.Control alloc.Memory* alloc.RawAddress) 4488 // ctl := init.Control; mem.SLICE(#short[*]) := init.Memory 4489 // After the initialize node executes, the object is ready for service: 4490 // oop := (CheckCastPP init.Control alloc.RawAddress #short[]) 4491 // Suppose its body is immediately initialized as {1,2}: 4492 // store1 = (StoreC init.Control init.Memory (+ oop 12) 1) 4493 // store2 = (StoreC init.Control store1 (+ oop 14) 2) 4494 // mem.SLICE(#short[*]) := store2 4495 // 4496 // DETAILS: 4497 // An InitializeNode collects and isolates object initialization after 4498 // an AllocateNode and before the next possible safepoint. As a 4499 // memory barrier (MemBarNode), it keeps critical stores from drifting 4500 // down past any safepoint or any publication of the allocation. 4501 // Before this barrier, a newly-allocated object may have uninitialized bits. 4502 // After this barrier, it may be treated as a real oop, and GC is allowed. 4503 // 4504 // The semantics of the InitializeNode include an implicit zeroing of 4505 // the new object from object header to the end of the object. 4506 // (The object header and end are determined by the AllocateNode.) 4507 // 4508 // Certain stores may be added as direct inputs to the InitializeNode. 4509 // These stores must update raw memory, and they must be to addresses 4510 // derived from the raw address produced by AllocateNode, and with 4511 // a constant offset. They must be ordered by increasing offset. 4512 // The first one is at in(RawStores), the last at in(req()-1). 4513 // Unlike most memory operations, they are not linked in a chain, 4514 // but are displayed in parallel as users of the rawmem output of 4515 // the allocation. 4516 // 4517 // (See comments in InitializeNode::capture_store, which continue 4518 // the example given above.) 4519 // 4520 // When the associated Allocate is macro-expanded, the InitializeNode 4521 // may be rewritten to optimize collected stores. A ClearArrayNode 4522 // may also be created at that point to represent any required zeroing. 4523 // The InitializeNode is then marked 'complete', prohibiting further 4524 // capturing of nearby memory operations. 4525 // 4526 // During macro-expansion, all captured initializations which store 4527 // constant values of 32 bits or smaller are coalesced (if advantageous) 4528 // into larger 'tiles' 32 or 64 bits. This allows an object to be 4529 // initialized in fewer memory operations. Memory words which are 4530 // covered by neither tiles nor non-constant stores are pre-zeroed 4531 // by explicit stores of zero. (The code shape happens to do all 4532 // zeroing first, then all other stores, with both sequences occurring 4533 // in order of ascending offsets.) 4534 // 4535 // Alternatively, code may be inserted between an AllocateNode and its 4536 // InitializeNode, to perform arbitrary initialization of the new object. 4537 // E.g., the object copying intrinsics insert complex data transfers here. 4538 // The initialization must then be marked as 'complete' disable the 4539 // built-in zeroing semantics and the collection of initializing stores. 4540 // 4541 // While an InitializeNode is incomplete, reads from the memory state 4542 // produced by it are optimizable if they match the control edge and 4543 // new oop address associated with the allocation/initialization. 4544 // They return a stored value (if the offset matches) or else zero. 4545 // A write to the memory state, if it matches control and address, 4546 // and if it is to a constant offset, may be 'captured' by the 4547 // InitializeNode. It is cloned as a raw memory operation and rewired 4548 // inside the initialization, to the raw oop produced by the allocation. 4549 // Operations on addresses which are provably distinct (e.g., to 4550 // other AllocateNodes) are allowed to bypass the initialization. 4551 // 4552 // The effect of all this is to consolidate object initialization 4553 // (both arrays and non-arrays, both piecewise and bulk) into a 4554 // single location, where it can be optimized as a unit. 4555 // 4556 // Only stores with an offset less than TrackedInitializationLimit words 4557 // will be considered for capture by an InitializeNode. This puts a 4558 // reasonable limit on the complexity of optimized initializations. 4559 4560 //---------------------------InitializeNode------------------------------------ 4561 InitializeNode::InitializeNode(Compile* C, int adr_type, Node* rawoop) 4562 : MemBarNode(C, adr_type, rawoop), 4563 _is_complete(Incomplete), _does_not_escape(false) 4564 { 4565 init_class_id(Class_Initialize); 4566 4567 assert(adr_type == Compile::AliasIdxRaw, "only valid atp"); 4568 assert(in(RawAddress) == rawoop, "proper init"); 4569 // Note: allocation() can be null, for secondary initialization barriers 4570 } 4571 4572 // Since this node is not matched, it will be processed by the 4573 // register allocator. Declare that there are no constraints 4574 // on the allocation of the RawAddress edge. 4575 const RegMask &InitializeNode::in_RegMask(uint idx) const { 4576 // This edge should be set to top, by the set_complete. But be conservative. 4577 if (idx == InitializeNode::RawAddress) 4578 return *(Compile::current()->matcher()->idealreg2spillmask[in(idx)->ideal_reg()]); 4579 return RegMask::Empty; 4580 } 4581 4582 Node* InitializeNode::memory(uint alias_idx) { 4583 Node* mem = in(Memory); 4584 if (mem->is_MergeMem()) { 4585 return mem->as_MergeMem()->memory_at(alias_idx); 4586 } else { 4587 // incoming raw memory is not split 4588 return mem; 4589 } 4590 } 4591 4592 bool InitializeNode::is_non_zero() { 4593 if (is_complete()) return false; 4594 remove_extra_zeroes(); 4595 return (req() > RawStores); 4596 } 4597 4598 void InitializeNode::set_complete(PhaseGVN* phase) { 4599 assert(!is_complete(), "caller responsibility"); 4600 _is_complete = Complete; 4601 4602 // After this node is complete, it contains a bunch of 4603 // raw-memory initializations. There is no need for 4604 // it to have anything to do with non-raw memory effects. 4605 // Therefore, tell all non-raw users to re-optimize themselves, 4606 // after skipping the memory effects of this initialization. 4607 PhaseIterGVN* igvn = phase->is_IterGVN(); 4608 if (igvn) igvn->add_users_to_worklist(this); 4609 } 4610 4611 // convenience function 4612 // return false if the init contains any stores already 4613 bool AllocateNode::maybe_set_complete(PhaseGVN* phase) { 4614 InitializeNode* init = initialization(); 4615 if (init == nullptr || init->is_complete()) { 4616 return false; 4617 } 4618 init->remove_extra_zeroes(); 4619 // for now, if this allocation has already collected any inits, bail: 4620 if (init->is_non_zero()) return false; 4621 init->set_complete(phase); 4622 return true; 4623 } 4624 4625 void InitializeNode::remove_extra_zeroes() { 4626 if (req() == RawStores) return; 4627 Node* zmem = zero_memory(); 4628 uint fill = RawStores; 4629 for (uint i = fill; i < req(); i++) { 4630 Node* n = in(i); 4631 if (n->is_top() || n == zmem) continue; // skip 4632 if (fill < i) set_req(fill, n); // compact 4633 ++fill; 4634 } 4635 // delete any empty spaces created: 4636 while (fill < req()) { 4637 del_req(fill); 4638 } 4639 } 4640 4641 // Helper for remembering which stores go with which offsets. 4642 intptr_t InitializeNode::get_store_offset(Node* st, PhaseValues* phase) { 4643 if (!st->is_Store()) return -1; // can happen to dead code via subsume_node 4644 intptr_t offset = -1; 4645 Node* base = AddPNode::Ideal_base_and_offset(st->in(MemNode::Address), 4646 phase, offset); 4647 if (base == nullptr) return -1; // something is dead, 4648 if (offset < 0) return -1; // dead, dead 4649 return offset; 4650 } 4651 4652 // Helper for proving that an initialization expression is 4653 // "simple enough" to be folded into an object initialization. 4654 // Attempts to prove that a store's initial value 'n' can be captured 4655 // within the initialization without creating a vicious cycle, such as: 4656 // { Foo p = new Foo(); p.next = p; } 4657 // True for constants and parameters and small combinations thereof. 4658 bool InitializeNode::detect_init_independence(Node* value, PhaseGVN* phase) { 4659 ResourceMark rm; 4660 Unique_Node_List worklist; 4661 worklist.push(value); 4662 4663 uint complexity_limit = 20; 4664 for (uint j = 0; j < worklist.size(); j++) { 4665 if (j >= complexity_limit) { 4666 return false; // Bail out if processed too many nodes 4667 } 4668 4669 Node* n = worklist.at(j); 4670 if (n == nullptr) continue; // (can this really happen?) 4671 if (n->is_Proj()) n = n->in(0); 4672 if (n == this) return false; // found a cycle 4673 if (n->is_Con()) continue; 4674 if (n->is_Start()) continue; // params, etc., are OK 4675 if (n->is_Root()) continue; // even better 4676 4677 // There cannot be any dependency if 'n' is a CFG node that dominates the current allocation 4678 if (n->is_CFG() && phase->is_dominator(n, allocation())) { 4679 continue; 4680 } 4681 4682 Node* ctl = n->in(0); 4683 if (ctl != nullptr && !ctl->is_top()) { 4684 if (ctl->is_Proj()) ctl = ctl->in(0); 4685 if (ctl == this) return false; 4686 4687 // If we already know that the enclosing memory op is pinned right after 4688 // the init, then any control flow that the store has picked up 4689 // must have preceded the init, or else be equal to the init. 4690 // Even after loop optimizations (which might change control edges) 4691 // a store is never pinned *before* the availability of its inputs. 4692 if (!MemNode::all_controls_dominate(n, this)) { 4693 return false; // failed to prove a good control 4694 } 4695 } 4696 4697 // Check data edges for possible dependencies on 'this'. 4698 for (uint i = 1; i < n->req(); i++) { 4699 Node* m = n->in(i); 4700 if (m == nullptr || m == n || m->is_top()) continue; 4701 4702 // Only process data inputs once 4703 worklist.push(m); 4704 } 4705 } 4706 4707 return true; 4708 } 4709 4710 // Here are all the checks a Store must pass before it can be moved into 4711 // an initialization. Returns zero if a check fails. 4712 // On success, returns the (constant) offset to which the store applies, 4713 // within the initialized memory. 4714 intptr_t InitializeNode::can_capture_store(StoreNode* st, PhaseGVN* phase, bool can_reshape) { 4715 const int FAIL = 0; 4716 if (st->req() != MemNode::ValueIn + 1) 4717 return FAIL; // an inscrutable StoreNode (card mark?) 4718 Node* ctl = st->in(MemNode::Control); 4719 if (!(ctl != nullptr && ctl->is_Proj() && ctl->in(0) == this)) 4720 return FAIL; // must be unconditional after the initialization 4721 Node* mem = st->in(MemNode::Memory); 4722 if (!(mem->is_Proj() && mem->in(0) == this)) 4723 return FAIL; // must not be preceded by other stores 4724 Node* adr = st->in(MemNode::Address); 4725 intptr_t offset; 4726 AllocateNode* alloc = AllocateNode::Ideal_allocation(adr, phase, offset); 4727 if (alloc == nullptr) 4728 return FAIL; // inscrutable address 4729 if (alloc != allocation()) 4730 return FAIL; // wrong allocation! (store needs to float up) 4731 int size_in_bytes = st->memory_size(); 4732 if ((size_in_bytes != 0) && (offset % size_in_bytes) != 0) { 4733 return FAIL; // mismatched access 4734 } 4735 Node* val = st->in(MemNode::ValueIn); 4736 4737 if (!detect_init_independence(val, phase)) 4738 return FAIL; // stored value must be 'simple enough' 4739 4740 // The Store can be captured only if nothing after the allocation 4741 // and before the Store is using the memory location that the store 4742 // overwrites. 4743 bool failed = false; 4744 // If is_complete_with_arraycopy() is true the shape of the graph is 4745 // well defined and is safe so no need for extra checks. 4746 if (!is_complete_with_arraycopy()) { 4747 // We are going to look at each use of the memory state following 4748 // the allocation to make sure nothing reads the memory that the 4749 // Store writes. 4750 const TypePtr* t_adr = phase->type(adr)->isa_ptr(); 4751 int alias_idx = phase->C->get_alias_index(t_adr); 4752 ResourceMark rm; 4753 Unique_Node_List mems; 4754 mems.push(mem); 4755 Node* unique_merge = nullptr; 4756 for (uint next = 0; next < mems.size(); ++next) { 4757 Node *m = mems.at(next); 4758 for (DUIterator_Fast jmax, j = m->fast_outs(jmax); j < jmax; j++) { 4759 Node *n = m->fast_out(j); 4760 if (n->outcnt() == 0) { 4761 continue; 4762 } 4763 if (n == st) { 4764 continue; 4765 } else if (n->in(0) != nullptr && n->in(0) != ctl) { 4766 // If the control of this use is different from the control 4767 // of the Store which is right after the InitializeNode then 4768 // this node cannot be between the InitializeNode and the 4769 // Store. 4770 continue; 4771 } else if (n->is_MergeMem()) { 4772 if (n->as_MergeMem()->memory_at(alias_idx) == m) { 4773 // We can hit a MergeMemNode (that will likely go away 4774 // later) that is a direct use of the memory state 4775 // following the InitializeNode on the same slice as the 4776 // store node that we'd like to capture. We need to check 4777 // the uses of the MergeMemNode. 4778 mems.push(n); 4779 } 4780 } else if (n->is_Mem()) { 4781 Node* other_adr = n->in(MemNode::Address); 4782 if (other_adr == adr) { 4783 failed = true; 4784 break; 4785 } else { 4786 const TypePtr* other_t_adr = phase->type(other_adr)->isa_ptr(); 4787 if (other_t_adr != nullptr) { 4788 int other_alias_idx = phase->C->get_alias_index(other_t_adr); 4789 if (other_alias_idx == alias_idx) { 4790 // A load from the same memory slice as the store right 4791 // after the InitializeNode. We check the control of the 4792 // object/array that is loaded from. If it's the same as 4793 // the store control then we cannot capture the store. 4794 assert(!n->is_Store(), "2 stores to same slice on same control?"); 4795 Node* base = other_adr; 4796 if (base->is_Phi()) { 4797 // In rare case, base may be a PhiNode and it may read 4798 // the same memory slice between InitializeNode and store. 4799 failed = true; 4800 break; 4801 } 4802 assert(base->is_AddP(), "should be addp but is %s", base->Name()); 4803 base = base->in(AddPNode::Base); 4804 if (base != nullptr) { 4805 base = base->uncast(); 4806 if (base->is_Proj() && base->in(0) == alloc) { 4807 failed = true; 4808 break; 4809 } 4810 } 4811 } 4812 } 4813 } 4814 } else { 4815 failed = true; 4816 break; 4817 } 4818 } 4819 } 4820 } 4821 if (failed) { 4822 if (!can_reshape) { 4823 // We decided we couldn't capture the store during parsing. We 4824 // should try again during the next IGVN once the graph is 4825 // cleaner. 4826 phase->C->record_for_igvn(st); 4827 } 4828 return FAIL; 4829 } 4830 4831 return offset; // success 4832 } 4833 4834 // Find the captured store in(i) which corresponds to the range 4835 // [start..start+size) in the initialized object. 4836 // If there is one, return its index i. If there isn't, return the 4837 // negative of the index where it should be inserted. 4838 // Return 0 if the queried range overlaps an initialization boundary 4839 // or if dead code is encountered. 4840 // If size_in_bytes is zero, do not bother with overlap checks. 4841 int InitializeNode::captured_store_insertion_point(intptr_t start, 4842 int size_in_bytes, 4843 PhaseValues* phase) { 4844 const int FAIL = 0, MAX_STORE = MAX2(BytesPerLong, (int)MaxVectorSize); 4845 4846 if (is_complete()) 4847 return FAIL; // arraycopy got here first; punt 4848 4849 assert(allocation() != nullptr, "must be present"); 4850 4851 // no negatives, no header fields: 4852 if (start < (intptr_t) allocation()->minimum_header_size()) return FAIL; 4853 4854 // after a certain size, we bail out on tracking all the stores: 4855 intptr_t ti_limit = (TrackedInitializationLimit * HeapWordSize); 4856 if (start >= ti_limit) return FAIL; 4857 4858 for (uint i = InitializeNode::RawStores, limit = req(); ; ) { 4859 if (i >= limit) return -(int)i; // not found; here is where to put it 4860 4861 Node* st = in(i); 4862 intptr_t st_off = get_store_offset(st, phase); 4863 if (st_off < 0) { 4864 if (st != zero_memory()) { 4865 return FAIL; // bail out if there is dead garbage 4866 } 4867 } else if (st_off > start) { 4868 // ...we are done, since stores are ordered 4869 if (st_off < start + size_in_bytes) { 4870 return FAIL; // the next store overlaps 4871 } 4872 return -(int)i; // not found; here is where to put it 4873 } else if (st_off < start) { 4874 assert(st->as_Store()->memory_size() <= MAX_STORE, ""); 4875 if (size_in_bytes != 0 && 4876 start < st_off + MAX_STORE && 4877 start < st_off + st->as_Store()->memory_size()) { 4878 return FAIL; // the previous store overlaps 4879 } 4880 } else { 4881 if (size_in_bytes != 0 && 4882 st->as_Store()->memory_size() != size_in_bytes) { 4883 return FAIL; // mismatched store size 4884 } 4885 return i; 4886 } 4887 4888 ++i; 4889 } 4890 } 4891 4892 // Look for a captured store which initializes at the offset 'start' 4893 // with the given size. If there is no such store, and no other 4894 // initialization interferes, then return zero_memory (the memory 4895 // projection of the AllocateNode). 4896 Node* InitializeNode::find_captured_store(intptr_t start, int size_in_bytes, 4897 PhaseValues* phase) { 4898 assert(stores_are_sane(phase), ""); 4899 int i = captured_store_insertion_point(start, size_in_bytes, phase); 4900 if (i == 0) { 4901 return nullptr; // something is dead 4902 } else if (i < 0) { 4903 return zero_memory(); // just primordial zero bits here 4904 } else { 4905 Node* st = in(i); // here is the store at this position 4906 assert(get_store_offset(st->as_Store(), phase) == start, "sanity"); 4907 return st; 4908 } 4909 } 4910 4911 // Create, as a raw pointer, an address within my new object at 'offset'. 4912 Node* InitializeNode::make_raw_address(intptr_t offset, 4913 PhaseGVN* phase) { 4914 Node* addr = in(RawAddress); 4915 if (offset != 0) { 4916 Compile* C = phase->C; 4917 addr = phase->transform( new AddPNode(C->top(), addr, 4918 phase->MakeConX(offset)) ); 4919 } 4920 return addr; 4921 } 4922 4923 // Clone the given store, converting it into a raw store 4924 // initializing a field or element of my new object. 4925 // Caller is responsible for retiring the original store, 4926 // with subsume_node or the like. 4927 // 4928 // From the example above InitializeNode::InitializeNode, 4929 // here are the old stores to be captured: 4930 // store1 = (StoreC init.Control init.Memory (+ oop 12) 1) 4931 // store2 = (StoreC init.Control store1 (+ oop 14) 2) 4932 // 4933 // Here is the changed code; note the extra edges on init: 4934 // alloc = (Allocate ...) 4935 // rawoop = alloc.RawAddress 4936 // rawstore1 = (StoreC alloc.Control alloc.Memory (+ rawoop 12) 1) 4937 // rawstore2 = (StoreC alloc.Control alloc.Memory (+ rawoop 14) 2) 4938 // init = (Initialize alloc.Control alloc.Memory rawoop 4939 // rawstore1 rawstore2) 4940 // 4941 Node* InitializeNode::capture_store(StoreNode* st, intptr_t start, 4942 PhaseGVN* phase, bool can_reshape) { 4943 assert(stores_are_sane(phase), ""); 4944 4945 if (start < 0) return nullptr; 4946 assert(can_capture_store(st, phase, can_reshape) == start, "sanity"); 4947 4948 Compile* C = phase->C; 4949 int size_in_bytes = st->memory_size(); 4950 int i = captured_store_insertion_point(start, size_in_bytes, phase); 4951 if (i == 0) return nullptr; // bail out 4952 Node* prev_mem = nullptr; // raw memory for the captured store 4953 if (i > 0) { 4954 prev_mem = in(i); // there is a pre-existing store under this one 4955 set_req(i, C->top()); // temporarily disconnect it 4956 // See StoreNode::Ideal 'st->outcnt() == 1' for the reason to disconnect. 4957 } else { 4958 i = -i; // no pre-existing store 4959 prev_mem = zero_memory(); // a slice of the newly allocated object 4960 if (i > InitializeNode::RawStores && in(i-1) == prev_mem) 4961 set_req(--i, C->top()); // reuse this edge; it has been folded away 4962 else 4963 ins_req(i, C->top()); // build a new edge 4964 } 4965 Node* new_st = st->clone(); 4966 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2(); 4967 new_st->set_req(MemNode::Control, in(Control)); 4968 new_st->set_req(MemNode::Memory, prev_mem); 4969 new_st->set_req(MemNode::Address, make_raw_address(start, phase)); 4970 bs->eliminate_gc_barrier_data(new_st); 4971 new_st = phase->transform(new_st); 4972 4973 // At this point, new_st might have swallowed a pre-existing store 4974 // at the same offset, or perhaps new_st might have disappeared, 4975 // if it redundantly stored the same value (or zero to fresh memory). 4976 4977 // In any case, wire it in: 4978 PhaseIterGVN* igvn = phase->is_IterGVN(); 4979 if (igvn) { 4980 igvn->rehash_node_delayed(this); 4981 } 4982 set_req(i, new_st); 4983 4984 // The caller may now kill the old guy. 4985 DEBUG_ONLY(Node* check_st = find_captured_store(start, size_in_bytes, phase)); 4986 assert(check_st == new_st || check_st == nullptr, "must be findable"); 4987 assert(!is_complete(), ""); 4988 return new_st; 4989 } 4990 4991 static bool store_constant(jlong* tiles, int num_tiles, 4992 intptr_t st_off, int st_size, 4993 jlong con) { 4994 if ((st_off & (st_size-1)) != 0) 4995 return false; // strange store offset (assume size==2**N) 4996 address addr = (address)tiles + st_off; 4997 assert(st_off >= 0 && addr+st_size <= (address)&tiles[num_tiles], "oob"); 4998 switch (st_size) { 4999 case sizeof(jbyte): *(jbyte*) addr = (jbyte) con; break; 5000 case sizeof(jchar): *(jchar*) addr = (jchar) con; break; 5001 case sizeof(jint): *(jint*) addr = (jint) con; break; 5002 case sizeof(jlong): *(jlong*) addr = (jlong) con; break; 5003 default: return false; // strange store size (detect size!=2**N here) 5004 } 5005 return true; // return success to caller 5006 } 5007 5008 // Coalesce subword constants into int constants and possibly 5009 // into long constants. The goal, if the CPU permits, 5010 // is to initialize the object with a small number of 64-bit tiles. 5011 // Also, convert floating-point constants to bit patterns. 5012 // Non-constants are not relevant to this pass. 5013 // 5014 // In terms of the running example on InitializeNode::InitializeNode 5015 // and InitializeNode::capture_store, here is the transformation 5016 // of rawstore1 and rawstore2 into rawstore12: 5017 // alloc = (Allocate ...) 5018 // rawoop = alloc.RawAddress 5019 // tile12 = 0x00010002 5020 // rawstore12 = (StoreI alloc.Control alloc.Memory (+ rawoop 12) tile12) 5021 // init = (Initialize alloc.Control alloc.Memory rawoop rawstore12) 5022 // 5023 void 5024 InitializeNode::coalesce_subword_stores(intptr_t header_size, 5025 Node* size_in_bytes, 5026 PhaseGVN* phase) { 5027 Compile* C = phase->C; 5028 5029 assert(stores_are_sane(phase), ""); 5030 // Note: After this pass, they are not completely sane, 5031 // since there may be some overlaps. 5032 5033 int old_subword = 0, old_long = 0, new_int = 0, new_long = 0; 5034 5035 intptr_t ti_limit = (TrackedInitializationLimit * HeapWordSize); 5036 intptr_t size_limit = phase->find_intptr_t_con(size_in_bytes, ti_limit); 5037 size_limit = MIN2(size_limit, ti_limit); 5038 size_limit = align_up(size_limit, BytesPerLong); 5039 int num_tiles = size_limit / BytesPerLong; 5040 5041 // allocate space for the tile map: 5042 const int small_len = DEBUG_ONLY(true ? 3 :) 30; // keep stack frames small 5043 jlong tiles_buf[small_len]; 5044 Node* nodes_buf[small_len]; 5045 jlong inits_buf[small_len]; 5046 jlong* tiles = ((num_tiles <= small_len) ? &tiles_buf[0] 5047 : NEW_RESOURCE_ARRAY(jlong, num_tiles)); 5048 Node** nodes = ((num_tiles <= small_len) ? &nodes_buf[0] 5049 : NEW_RESOURCE_ARRAY(Node*, num_tiles)); 5050 jlong* inits = ((num_tiles <= small_len) ? &inits_buf[0] 5051 : NEW_RESOURCE_ARRAY(jlong, num_tiles)); 5052 // tiles: exact bitwise model of all primitive constants 5053 // nodes: last constant-storing node subsumed into the tiles model 5054 // inits: which bytes (in each tile) are touched by any initializations 5055 5056 //// Pass A: Fill in the tile model with any relevant stores. 5057 5058 Copy::zero_to_bytes(tiles, sizeof(tiles[0]) * num_tiles); 5059 Copy::zero_to_bytes(nodes, sizeof(nodes[0]) * num_tiles); 5060 Copy::zero_to_bytes(inits, sizeof(inits[0]) * num_tiles); 5061 Node* zmem = zero_memory(); // initially zero memory state 5062 for (uint i = InitializeNode::RawStores, limit = req(); i < limit; i++) { 5063 Node* st = in(i); 5064 intptr_t st_off = get_store_offset(st, phase); 5065 5066 // Figure out the store's offset and constant value: 5067 if (st_off < header_size) continue; //skip (ignore header) 5068 if (st->in(MemNode::Memory) != zmem) continue; //skip (odd store chain) 5069 int st_size = st->as_Store()->memory_size(); 5070 if (st_off + st_size > size_limit) break; 5071 5072 // Record which bytes are touched, whether by constant or not. 5073 if (!store_constant(inits, num_tiles, st_off, st_size, (jlong) -1)) 5074 continue; // skip (strange store size) 5075 5076 const Type* val = phase->type(st->in(MemNode::ValueIn)); 5077 if (!val->singleton()) continue; //skip (non-con store) 5078 BasicType type = val->basic_type(); 5079 5080 jlong con = 0; 5081 switch (type) { 5082 case T_INT: con = val->is_int()->get_con(); break; 5083 case T_LONG: con = val->is_long()->get_con(); break; 5084 case T_FLOAT: con = jint_cast(val->getf()); break; 5085 case T_DOUBLE: con = jlong_cast(val->getd()); break; 5086 default: continue; //skip (odd store type) 5087 } 5088 5089 if (type == T_LONG && Matcher::isSimpleConstant64(con) && 5090 st->Opcode() == Op_StoreL) { 5091 continue; // This StoreL is already optimal. 5092 } 5093 5094 // Store down the constant. 5095 store_constant(tiles, num_tiles, st_off, st_size, con); 5096 5097 intptr_t j = st_off >> LogBytesPerLong; 5098 5099 if (type == T_INT && st_size == BytesPerInt 5100 && (st_off & BytesPerInt) == BytesPerInt) { 5101 jlong lcon = tiles[j]; 5102 if (!Matcher::isSimpleConstant64(lcon) && 5103 st->Opcode() == Op_StoreI) { 5104 // This StoreI is already optimal by itself. 5105 jint* intcon = (jint*) &tiles[j]; 5106 intcon[1] = 0; // undo the store_constant() 5107 5108 // If the previous store is also optimal by itself, back up and 5109 // undo the action of the previous loop iteration... if we can. 5110 // But if we can't, just let the previous half take care of itself. 5111 st = nodes[j]; 5112 st_off -= BytesPerInt; 5113 con = intcon[0]; 5114 if (con != 0 && st != nullptr && st->Opcode() == Op_StoreI) { 5115 assert(st_off >= header_size, "still ignoring header"); 5116 assert(get_store_offset(st, phase) == st_off, "must be"); 5117 assert(in(i-1) == zmem, "must be"); 5118 DEBUG_ONLY(const Type* tcon = phase->type(st->in(MemNode::ValueIn))); 5119 assert(con == tcon->is_int()->get_con(), "must be"); 5120 // Undo the effects of the previous loop trip, which swallowed st: 5121 intcon[0] = 0; // undo store_constant() 5122 set_req(i-1, st); // undo set_req(i, zmem) 5123 nodes[j] = nullptr; // undo nodes[j] = st 5124 --old_subword; // undo ++old_subword 5125 } 5126 continue; // This StoreI is already optimal. 5127 } 5128 } 5129 5130 // This store is not needed. 5131 set_req(i, zmem); 5132 nodes[j] = st; // record for the moment 5133 if (st_size < BytesPerLong) // something has changed 5134 ++old_subword; // includes int/float, but who's counting... 5135 else ++old_long; 5136 } 5137 5138 if ((old_subword + old_long) == 0) 5139 return; // nothing more to do 5140 5141 //// Pass B: Convert any non-zero tiles into optimal constant stores. 5142 // Be sure to insert them before overlapping non-constant stores. 5143 // (E.g., byte[] x = { 1,2,y,4 } => x[int 0] = 0x01020004, x[2]=y.) 5144 for (int j = 0; j < num_tiles; j++) { 5145 jlong con = tiles[j]; 5146 jlong init = inits[j]; 5147 if (con == 0) continue; 5148 jint con0, con1; // split the constant, address-wise 5149 jint init0, init1; // split the init map, address-wise 5150 { union { jlong con; jint intcon[2]; } u; 5151 u.con = con; 5152 con0 = u.intcon[0]; 5153 con1 = u.intcon[1]; 5154 u.con = init; 5155 init0 = u.intcon[0]; 5156 init1 = u.intcon[1]; 5157 } 5158 5159 Node* old = nodes[j]; 5160 assert(old != nullptr, "need the prior store"); 5161 intptr_t offset = (j * BytesPerLong); 5162 5163 bool split = !Matcher::isSimpleConstant64(con); 5164 5165 if (offset < header_size) { 5166 assert(offset + BytesPerInt >= header_size, "second int counts"); 5167 assert(*(jint*)&tiles[j] == 0, "junk in header"); 5168 split = true; // only the second word counts 5169 // Example: int a[] = { 42 ... } 5170 } else if (con0 == 0 && init0 == -1) { 5171 split = true; // first word is covered by full inits 5172 // Example: int a[] = { ... foo(), 42 ... } 5173 } else if (con1 == 0 && init1 == -1) { 5174 split = true; // second word is covered by full inits 5175 // Example: int a[] = { ... 42, foo() ... } 5176 } 5177 5178 // Here's a case where init0 is neither 0 nor -1: 5179 // byte a[] = { ... 0,0,foo(),0, 0,0,0,42 ... } 5180 // Assuming big-endian memory, init0, init1 are 0x0000FF00, 0x000000FF. 5181 // In this case the tile is not split; it is (jlong)42. 5182 // The big tile is stored down, and then the foo() value is inserted. 5183 // (If there were foo(),foo() instead of foo(),0, init0 would be -1.) 5184 5185 Node* ctl = old->in(MemNode::Control); 5186 Node* adr = make_raw_address(offset, phase); 5187 const TypePtr* atp = TypeRawPtr::BOTTOM; 5188 5189 // One or two coalesced stores to plop down. 5190 Node* st[2]; 5191 intptr_t off[2]; 5192 int nst = 0; 5193 if (!split) { 5194 ++new_long; 5195 off[nst] = offset; 5196 st[nst++] = StoreNode::make(*phase, ctl, zmem, adr, atp, 5197 phase->longcon(con), T_LONG, MemNode::unordered); 5198 } else { 5199 // Omit either if it is a zero. 5200 if (con0 != 0) { 5201 ++new_int; 5202 off[nst] = offset; 5203 st[nst++] = StoreNode::make(*phase, ctl, zmem, adr, atp, 5204 phase->intcon(con0), T_INT, MemNode::unordered); 5205 } 5206 if (con1 != 0) { 5207 ++new_int; 5208 offset += BytesPerInt; 5209 adr = make_raw_address(offset, phase); 5210 off[nst] = offset; 5211 st[nst++] = StoreNode::make(*phase, ctl, zmem, adr, atp, 5212 phase->intcon(con1), T_INT, MemNode::unordered); 5213 } 5214 } 5215 5216 // Insert second store first, then the first before the second. 5217 // Insert each one just before any overlapping non-constant stores. 5218 while (nst > 0) { 5219 Node* st1 = st[--nst]; 5220 C->copy_node_notes_to(st1, old); 5221 st1 = phase->transform(st1); 5222 offset = off[nst]; 5223 assert(offset >= header_size, "do not smash header"); 5224 int ins_idx = captured_store_insertion_point(offset, /*size:*/0, phase); 5225 guarantee(ins_idx != 0, "must re-insert constant store"); 5226 if (ins_idx < 0) ins_idx = -ins_idx; // never overlap 5227 if (ins_idx > InitializeNode::RawStores && in(ins_idx-1) == zmem) 5228 set_req(--ins_idx, st1); 5229 else 5230 ins_req(ins_idx, st1); 5231 } 5232 } 5233 5234 if (PrintCompilation && WizardMode) 5235 tty->print_cr("Changed %d/%d subword/long constants into %d/%d int/long", 5236 old_subword, old_long, new_int, new_long); 5237 if (C->log() != nullptr) 5238 C->log()->elem("comment that='%d/%d subword/long to %d/%d int/long'", 5239 old_subword, old_long, new_int, new_long); 5240 5241 // Clean up any remaining occurrences of zmem: 5242 remove_extra_zeroes(); 5243 } 5244 5245 // Explore forward from in(start) to find the first fully initialized 5246 // word, and return its offset. Skip groups of subword stores which 5247 // together initialize full words. If in(start) is itself part of a 5248 // fully initialized word, return the offset of in(start). If there 5249 // are no following full-word stores, or if something is fishy, return 5250 // a negative value. 5251 intptr_t InitializeNode::find_next_fullword_store(uint start, PhaseGVN* phase) { 5252 int int_map = 0; 5253 intptr_t int_map_off = 0; 5254 const int FULL_MAP = right_n_bits(BytesPerInt); // the int_map we hope for 5255 5256 for (uint i = start, limit = req(); i < limit; i++) { 5257 Node* st = in(i); 5258 5259 intptr_t st_off = get_store_offset(st, phase); 5260 if (st_off < 0) break; // return conservative answer 5261 5262 int st_size = st->as_Store()->memory_size(); 5263 if (st_size >= BytesPerInt && (st_off % BytesPerInt) == 0) { 5264 return st_off; // we found a complete word init 5265 } 5266 5267 // update the map: 5268 5269 intptr_t this_int_off = align_down(st_off, BytesPerInt); 5270 if (this_int_off != int_map_off) { 5271 // reset the map: 5272 int_map = 0; 5273 int_map_off = this_int_off; 5274 } 5275 5276 int subword_off = st_off - this_int_off; 5277 int_map |= right_n_bits(st_size) << subword_off; 5278 if ((int_map & FULL_MAP) == FULL_MAP) { 5279 return this_int_off; // we found a complete word init 5280 } 5281 5282 // Did this store hit or cross the word boundary? 5283 intptr_t next_int_off = align_down(st_off + st_size, BytesPerInt); 5284 if (next_int_off == this_int_off + BytesPerInt) { 5285 // We passed the current int, without fully initializing it. 5286 int_map_off = next_int_off; 5287 int_map >>= BytesPerInt; 5288 } else if (next_int_off > this_int_off + BytesPerInt) { 5289 // We passed the current and next int. 5290 return this_int_off + BytesPerInt; 5291 } 5292 } 5293 5294 return -1; 5295 } 5296 5297 5298 // Called when the associated AllocateNode is expanded into CFG. 5299 // At this point, we may perform additional optimizations. 5300 // Linearize the stores by ascending offset, to make memory 5301 // activity as coherent as possible. 5302 Node* InitializeNode::complete_stores(Node* rawctl, Node* rawmem, Node* rawptr, 5303 intptr_t header_size, 5304 Node* size_in_bytes, 5305 PhaseIterGVN* phase) { 5306 assert(!is_complete(), "not already complete"); 5307 assert(stores_are_sane(phase), ""); 5308 assert(allocation() != nullptr, "must be present"); 5309 5310 remove_extra_zeroes(); 5311 5312 if (ReduceFieldZeroing || ReduceBulkZeroing) 5313 // reduce instruction count for common initialization patterns 5314 coalesce_subword_stores(header_size, size_in_bytes, phase); 5315 5316 Node* zmem = zero_memory(); // initially zero memory state 5317 Node* inits = zmem; // accumulating a linearized chain of inits 5318 #ifdef ASSERT 5319 intptr_t first_offset = allocation()->minimum_header_size(); 5320 intptr_t last_init_off = first_offset; // previous init offset 5321 intptr_t last_init_end = first_offset; // previous init offset+size 5322 intptr_t last_tile_end = first_offset; // previous tile offset+size 5323 #endif 5324 intptr_t zeroes_done = header_size; 5325 5326 bool do_zeroing = true; // we might give up if inits are very sparse 5327 int big_init_gaps = 0; // how many large gaps have we seen? 5328 5329 if (UseTLAB && ZeroTLAB) do_zeroing = false; 5330 if (!ReduceFieldZeroing && !ReduceBulkZeroing) do_zeroing = false; 5331 5332 for (uint i = InitializeNode::RawStores, limit = req(); i < limit; i++) { 5333 Node* st = in(i); 5334 intptr_t st_off = get_store_offset(st, phase); 5335 if (st_off < 0) 5336 break; // unknown junk in the inits 5337 if (st->in(MemNode::Memory) != zmem) 5338 break; // complicated store chains somehow in list 5339 5340 int st_size = st->as_Store()->memory_size(); 5341 intptr_t next_init_off = st_off + st_size; 5342 5343 if (do_zeroing && zeroes_done < next_init_off) { 5344 // See if this store needs a zero before it or under it. 5345 intptr_t zeroes_needed = st_off; 5346 5347 if (st_size < BytesPerInt) { 5348 // Look for subword stores which only partially initialize words. 5349 // If we find some, we must lay down some word-level zeroes first, 5350 // underneath the subword stores. 5351 // 5352 // Examples: 5353 // byte[] a = { p,q,r,s } => a[0]=p,a[1]=q,a[2]=r,a[3]=s 5354 // byte[] a = { x,y,0,0 } => a[0..3] = 0, a[0]=x,a[1]=y 5355 // byte[] a = { 0,0,z,0 } => a[0..3] = 0, a[2]=z 5356 // 5357 // Note: coalesce_subword_stores may have already done this, 5358 // if it was prompted by constant non-zero subword initializers. 5359 // But this case can still arise with non-constant stores. 5360 5361 intptr_t next_full_store = find_next_fullword_store(i, phase); 5362 5363 // In the examples above: 5364 // in(i) p q r s x y z 5365 // st_off 12 13 14 15 12 13 14 5366 // st_size 1 1 1 1 1 1 1 5367 // next_full_s. 12 16 16 16 16 16 16 5368 // z's_done 12 16 16 16 12 16 12 5369 // z's_needed 12 16 16 16 16 16 16 5370 // zsize 0 0 0 0 4 0 4 5371 if (next_full_store < 0) { 5372 // Conservative tack: Zero to end of current word. 5373 zeroes_needed = align_up(zeroes_needed, BytesPerInt); 5374 } else { 5375 // Zero to beginning of next fully initialized word. 5376 // Or, don't zero at all, if we are already in that word. 5377 assert(next_full_store >= zeroes_needed, "must go forward"); 5378 assert((next_full_store & (BytesPerInt-1)) == 0, "even boundary"); 5379 zeroes_needed = next_full_store; 5380 } 5381 } 5382 5383 if (zeroes_needed > zeroes_done) { 5384 intptr_t zsize = zeroes_needed - zeroes_done; 5385 // Do some incremental zeroing on rawmem, in parallel with inits. 5386 zeroes_done = align_down(zeroes_done, BytesPerInt); 5387 rawmem = ClearArrayNode::clear_memory(rawctl, rawmem, rawptr, 5388 allocation()->in(AllocateNode::DefaultValue), 5389 allocation()->in(AllocateNode::RawDefaultValue), 5390 zeroes_done, zeroes_needed, 5391 phase); 5392 zeroes_done = zeroes_needed; 5393 if (zsize > InitArrayShortSize && ++big_init_gaps > 2) 5394 do_zeroing = false; // leave the hole, next time 5395 } 5396 } 5397 5398 // Collect the store and move on: 5399 phase->replace_input_of(st, MemNode::Memory, inits); 5400 inits = st; // put it on the linearized chain 5401 set_req(i, zmem); // unhook from previous position 5402 5403 if (zeroes_done == st_off) 5404 zeroes_done = next_init_off; 5405 5406 assert(!do_zeroing || zeroes_done >= next_init_off, "don't miss any"); 5407 5408 #ifdef ASSERT 5409 // Various order invariants. Weaker than stores_are_sane because 5410 // a large constant tile can be filled in by smaller non-constant stores. 5411 assert(st_off >= last_init_off, "inits do not reverse"); 5412 last_init_off = st_off; 5413 const Type* val = nullptr; 5414 if (st_size >= BytesPerInt && 5415 (val = phase->type(st->in(MemNode::ValueIn)))->singleton() && 5416 (int)val->basic_type() < (int)T_OBJECT) { 5417 assert(st_off >= last_tile_end, "tiles do not overlap"); 5418 assert(st_off >= last_init_end, "tiles do not overwrite inits"); 5419 last_tile_end = MAX2(last_tile_end, next_init_off); 5420 } else { 5421 intptr_t st_tile_end = align_up(next_init_off, BytesPerLong); 5422 assert(st_tile_end >= last_tile_end, "inits stay with tiles"); 5423 assert(st_off >= last_init_end, "inits do not overlap"); 5424 last_init_end = next_init_off; // it's a non-tile 5425 } 5426 #endif //ASSERT 5427 } 5428 5429 remove_extra_zeroes(); // clear out all the zmems left over 5430 add_req(inits); 5431 5432 if (!(UseTLAB && ZeroTLAB)) { 5433 // If anything remains to be zeroed, zero it all now. 5434 zeroes_done = align_down(zeroes_done, BytesPerInt); 5435 // if it is the last unused 4 bytes of an instance, forget about it 5436 intptr_t size_limit = phase->find_intptr_t_con(size_in_bytes, max_jint); 5437 if (zeroes_done + BytesPerLong >= size_limit) { 5438 AllocateNode* alloc = allocation(); 5439 assert(alloc != nullptr, "must be present"); 5440 if (alloc != nullptr && alloc->Opcode() == Op_Allocate) { 5441 Node* klass_node = alloc->in(AllocateNode::KlassNode); 5442 ciKlass* k = phase->type(klass_node)->is_instklassptr()->instance_klass(); 5443 if (zeroes_done == k->layout_helper()) 5444 zeroes_done = size_limit; 5445 } 5446 } 5447 if (zeroes_done < size_limit) { 5448 rawmem = ClearArrayNode::clear_memory(rawctl, rawmem, rawptr, 5449 allocation()->in(AllocateNode::DefaultValue), 5450 allocation()->in(AllocateNode::RawDefaultValue), 5451 zeroes_done, size_in_bytes, phase); 5452 } 5453 } 5454 5455 set_complete(phase); 5456 return rawmem; 5457 } 5458 5459 5460 #ifdef ASSERT 5461 bool InitializeNode::stores_are_sane(PhaseValues* phase) { 5462 if (is_complete()) 5463 return true; // stores could be anything at this point 5464 assert(allocation() != nullptr, "must be present"); 5465 intptr_t last_off = allocation()->minimum_header_size(); 5466 for (uint i = InitializeNode::RawStores; i < req(); i++) { 5467 Node* st = in(i); 5468 intptr_t st_off = get_store_offset(st, phase); 5469 if (st_off < 0) continue; // ignore dead garbage 5470 if (last_off > st_off) { 5471 tty->print_cr("*** bad store offset at %d: " INTX_FORMAT " > " INTX_FORMAT, i, last_off, st_off); 5472 this->dump(2); 5473 assert(false, "ascending store offsets"); 5474 return false; 5475 } 5476 last_off = st_off + st->as_Store()->memory_size(); 5477 } 5478 return true; 5479 } 5480 #endif //ASSERT 5481 5482 5483 5484 5485 //============================MergeMemNode===================================== 5486 // 5487 // SEMANTICS OF MEMORY MERGES: A MergeMem is a memory state assembled from several 5488 // contributing store or call operations. Each contributor provides the memory 5489 // state for a particular "alias type" (see Compile::alias_type). For example, 5490 // if a MergeMem has an input X for alias category #6, then any memory reference 5491 // to alias category #6 may use X as its memory state input, as an exact equivalent 5492 // to using the MergeMem as a whole. 5493 // Load<6>( MergeMem(<6>: X, ...), p ) <==> Load<6>(X,p) 5494 // 5495 // (Here, the <N> notation gives the index of the relevant adr_type.) 5496 // 5497 // In one special case (and more cases in the future), alias categories overlap. 5498 // The special alias category "Bot" (Compile::AliasIdxBot) includes all memory 5499 // states. Therefore, if a MergeMem has only one contributing input W for Bot, 5500 // it is exactly equivalent to that state W: 5501 // MergeMem(<Bot>: W) <==> W 5502 // 5503 // Usually, the merge has more than one input. In that case, where inputs 5504 // overlap (i.e., one is Bot), the narrower alias type determines the memory 5505 // state for that type, and the wider alias type (Bot) fills in everywhere else: 5506 // Load<5>( MergeMem(<Bot>: W, <6>: X), p ) <==> Load<5>(W,p) 5507 // Load<6>( MergeMem(<Bot>: W, <6>: X), p ) <==> Load<6>(X,p) 5508 // 5509 // A merge can take a "wide" memory state as one of its narrow inputs. 5510 // This simply means that the merge observes out only the relevant parts of 5511 // the wide input. That is, wide memory states arriving at narrow merge inputs 5512 // are implicitly "filtered" or "sliced" as necessary. (This is rare.) 5513 // 5514 // These rules imply that MergeMem nodes may cascade (via their <Bot> links), 5515 // and that memory slices "leak through": 5516 // MergeMem(<Bot>: MergeMem(<Bot>: W, <7>: Y)) <==> MergeMem(<Bot>: W, <7>: Y) 5517 // 5518 // But, in such a cascade, repeated memory slices can "block the leak": 5519 // MergeMem(<Bot>: MergeMem(<Bot>: W, <7>: Y), <7>: Y') <==> MergeMem(<Bot>: W, <7>: Y') 5520 // 5521 // In the last example, Y is not part of the combined memory state of the 5522 // outermost MergeMem. The system must, of course, prevent unschedulable 5523 // memory states from arising, so you can be sure that the state Y is somehow 5524 // a precursor to state Y'. 5525 // 5526 // 5527 // REPRESENTATION OF MEMORY MERGES: The indexes used to address the Node::in array 5528 // of each MergeMemNode array are exactly the numerical alias indexes, including 5529 // but not limited to AliasIdxTop, AliasIdxBot, and AliasIdxRaw. The functions 5530 // Compile::alias_type (and kin) produce and manage these indexes. 5531 // 5532 // By convention, the value of in(AliasIdxTop) (i.e., in(1)) is always the top node. 5533 // (Note that this provides quick access to the top node inside MergeMem methods, 5534 // without the need to reach out via TLS to Compile::current.) 5535 // 5536 // As a consequence of what was just described, a MergeMem that represents a full 5537 // memory state has an edge in(AliasIdxBot) which is a "wide" memory state, 5538 // containing all alias categories. 5539 // 5540 // MergeMem nodes never (?) have control inputs, so in(0) is null. 5541 // 5542 // All other edges in(N) (including in(AliasIdxRaw), which is in(3)) are either 5543 // a memory state for the alias type <N>, or else the top node, meaning that 5544 // there is no particular input for that alias type. Note that the length of 5545 // a MergeMem is variable, and may be extended at any time to accommodate new 5546 // memory states at larger alias indexes. When merges grow, they are of course 5547 // filled with "top" in the unused in() positions. 5548 // 5549 // This use of top is named "empty_memory()", or "empty_mem" (no-memory) as a variable. 5550 // (Top was chosen because it works smoothly with passes like GCM.) 5551 // 5552 // For convenience, we hardwire the alias index for TypeRawPtr::BOTTOM. (It is 5553 // the type of random VM bits like TLS references.) Since it is always the 5554 // first non-Bot memory slice, some low-level loops use it to initialize an 5555 // index variable: for (i = AliasIdxRaw; i < req(); i++). 5556 // 5557 // 5558 // ACCESSORS: There is a special accessor MergeMemNode::base_memory which returns 5559 // the distinguished "wide" state. The accessor MergeMemNode::memory_at(N) returns 5560 // the memory state for alias type <N>, or (if there is no particular slice at <N>, 5561 // it returns the base memory. To prevent bugs, memory_at does not accept <Top> 5562 // or <Bot> indexes. The iterator MergeMemStream provides robust iteration over 5563 // MergeMem nodes or pairs of such nodes, ensuring that the non-top edges are visited. 5564 // 5565 // %%%% We may get rid of base_memory as a separate accessor at some point; it isn't 5566 // really that different from the other memory inputs. An abbreviation called 5567 // "bot_memory()" for "memory_at(AliasIdxBot)" would keep code tidy. 5568 // 5569 // 5570 // PARTIAL MEMORY STATES: During optimization, MergeMem nodes may arise that represent 5571 // partial memory states. When a Phi splits through a MergeMem, the copy of the Phi 5572 // that "emerges though" the base memory will be marked as excluding the alias types 5573 // of the other (narrow-memory) copies which "emerged through" the narrow edges: 5574 // 5575 // Phi<Bot>(U, MergeMem(<Bot>: W, <8>: Y)) 5576 // ==Ideal=> MergeMem(<Bot>: Phi<Bot-8>(U, W), Phi<8>(U, Y)) 5577 // 5578 // This strange "subtraction" effect is necessary to ensure IGVN convergence. 5579 // (It is currently unimplemented.) As you can see, the resulting merge is 5580 // actually a disjoint union of memory states, rather than an overlay. 5581 // 5582 5583 //------------------------------MergeMemNode----------------------------------- 5584 Node* MergeMemNode::make_empty_memory() { 5585 Node* empty_memory = (Node*) Compile::current()->top(); 5586 assert(empty_memory->is_top(), "correct sentinel identity"); 5587 return empty_memory; 5588 } 5589 5590 MergeMemNode::MergeMemNode(Node *new_base) : Node(1+Compile::AliasIdxRaw) { 5591 init_class_id(Class_MergeMem); 5592 // all inputs are nullified in Node::Node(int) 5593 // set_input(0, nullptr); // no control input 5594 5595 // Initialize the edges uniformly to top, for starters. 5596 Node* empty_mem = make_empty_memory(); 5597 for (uint i = Compile::AliasIdxTop; i < req(); i++) { 5598 init_req(i,empty_mem); 5599 } 5600 assert(empty_memory() == empty_mem, ""); 5601 5602 if( new_base != nullptr && new_base->is_MergeMem() ) { 5603 MergeMemNode* mdef = new_base->as_MergeMem(); 5604 assert(mdef->empty_memory() == empty_mem, "consistent sentinels"); 5605 for (MergeMemStream mms(this, mdef); mms.next_non_empty2(); ) { 5606 mms.set_memory(mms.memory2()); 5607 } 5608 assert(base_memory() == mdef->base_memory(), ""); 5609 } else { 5610 set_base_memory(new_base); 5611 } 5612 } 5613 5614 // Make a new, untransformed MergeMem with the same base as 'mem'. 5615 // If mem is itself a MergeMem, populate the result with the same edges. 5616 MergeMemNode* MergeMemNode::make(Node* mem) { 5617 return new MergeMemNode(mem); 5618 } 5619 5620 //------------------------------cmp-------------------------------------------- 5621 uint MergeMemNode::hash() const { return NO_HASH; } 5622 bool MergeMemNode::cmp( const Node &n ) const { 5623 return (&n == this); // Always fail except on self 5624 } 5625 5626 //------------------------------Identity--------------------------------------- 5627 Node* MergeMemNode::Identity(PhaseGVN* phase) { 5628 // Identity if this merge point does not record any interesting memory 5629 // disambiguations. 5630 Node* base_mem = base_memory(); 5631 Node* empty_mem = empty_memory(); 5632 if (base_mem != empty_mem) { // Memory path is not dead? 5633 for (uint i = Compile::AliasIdxRaw; i < req(); i++) { 5634 Node* mem = in(i); 5635 if (mem != empty_mem && mem != base_mem) { 5636 return this; // Many memory splits; no change 5637 } 5638 } 5639 } 5640 return base_mem; // No memory splits; ID on the one true input 5641 } 5642 5643 //------------------------------Ideal------------------------------------------ 5644 // This method is invoked recursively on chains of MergeMem nodes 5645 Node *MergeMemNode::Ideal(PhaseGVN *phase, bool can_reshape) { 5646 // Remove chain'd MergeMems 5647 // 5648 // This is delicate, because the each "in(i)" (i >= Raw) is interpreted 5649 // relative to the "in(Bot)". Since we are patching both at the same time, 5650 // we have to be careful to read each "in(i)" relative to the old "in(Bot)", 5651 // but rewrite each "in(i)" relative to the new "in(Bot)". 5652 Node *progress = nullptr; 5653 5654 5655 Node* old_base = base_memory(); 5656 Node* empty_mem = empty_memory(); 5657 if (old_base == empty_mem) 5658 return nullptr; // Dead memory path. 5659 5660 MergeMemNode* old_mbase; 5661 if (old_base != nullptr && old_base->is_MergeMem()) 5662 old_mbase = old_base->as_MergeMem(); 5663 else 5664 old_mbase = nullptr; 5665 Node* new_base = old_base; 5666 5667 // simplify stacked MergeMems in base memory 5668 if (old_mbase) new_base = old_mbase->base_memory(); 5669 5670 // the base memory might contribute new slices beyond my req() 5671 if (old_mbase) grow_to_match(old_mbase); 5672 5673 // Note: We do not call verify_sparse on entry, because inputs 5674 // can normalize to the base_memory via subsume_node or similar 5675 // mechanisms. This method repairs that damage. 5676 5677 assert(!old_mbase || old_mbase->is_empty_memory(empty_mem), "consistent sentinels"); 5678 5679 // Look at each slice. 5680 for (uint i = Compile::AliasIdxRaw; i < req(); i++) { 5681 Node* old_in = in(i); 5682 // calculate the old memory value 5683 Node* old_mem = old_in; 5684 if (old_mem == empty_mem) old_mem = old_base; 5685 assert(old_mem == memory_at(i), ""); 5686 5687 // maybe update (reslice) the old memory value 5688 5689 // simplify stacked MergeMems 5690 Node* new_mem = old_mem; 5691 MergeMemNode* old_mmem; 5692 if (old_mem != nullptr && old_mem->is_MergeMem()) 5693 old_mmem = old_mem->as_MergeMem(); 5694 else 5695 old_mmem = nullptr; 5696 if (old_mmem == this) { 5697 // This can happen if loops break up and safepoints disappear. 5698 // A merge of BotPtr (default) with a RawPtr memory derived from a 5699 // safepoint can be rewritten to a merge of the same BotPtr with 5700 // the BotPtr phi coming into the loop. If that phi disappears 5701 // also, we can end up with a self-loop of the mergemem. 5702 // In general, if loops degenerate and memory effects disappear, 5703 // a mergemem can be left looking at itself. This simply means 5704 // that the mergemem's default should be used, since there is 5705 // no longer any apparent effect on this slice. 5706 // Note: If a memory slice is a MergeMem cycle, it is unreachable 5707 // from start. Update the input to TOP. 5708 new_mem = (new_base == this || new_base == empty_mem)? empty_mem : new_base; 5709 } 5710 else if (old_mmem != nullptr) { 5711 new_mem = old_mmem->memory_at(i); 5712 } 5713 // else preceding memory was not a MergeMem 5714 5715 // maybe store down a new value 5716 Node* new_in = new_mem; 5717 if (new_in == new_base) new_in = empty_mem; 5718 5719 if (new_in != old_in) { 5720 // Warning: Do not combine this "if" with the previous "if" 5721 // A memory slice might have be be rewritten even if it is semantically 5722 // unchanged, if the base_memory value has changed. 5723 set_req_X(i, new_in, phase); 5724 progress = this; // Report progress 5725 } 5726 } 5727 5728 if (new_base != old_base) { 5729 set_req_X(Compile::AliasIdxBot, new_base, phase); 5730 // Don't use set_base_memory(new_base), because we need to update du. 5731 assert(base_memory() == new_base, ""); 5732 progress = this; 5733 } 5734 5735 if( base_memory() == this ) { 5736 // a self cycle indicates this memory path is dead 5737 set_req(Compile::AliasIdxBot, empty_mem); 5738 } 5739 5740 // Resolve external cycles by calling Ideal on a MergeMem base_memory 5741 // Recursion must occur after the self cycle check above 5742 if( base_memory()->is_MergeMem() ) { 5743 MergeMemNode *new_mbase = base_memory()->as_MergeMem(); 5744 Node *m = phase->transform(new_mbase); // Rollup any cycles 5745 if( m != nullptr && 5746 (m->is_top() || 5747 (m->is_MergeMem() && m->as_MergeMem()->base_memory() == empty_mem)) ) { 5748 // propagate rollup of dead cycle to self 5749 set_req(Compile::AliasIdxBot, empty_mem); 5750 } 5751 } 5752 5753 if( base_memory() == empty_mem ) { 5754 progress = this; 5755 // Cut inputs during Parse phase only. 5756 // During Optimize phase a dead MergeMem node will be subsumed by Top. 5757 if( !can_reshape ) { 5758 for (uint i = Compile::AliasIdxRaw; i < req(); i++) { 5759 if( in(i) != empty_mem ) { set_req(i, empty_mem); } 5760 } 5761 } 5762 } 5763 5764 if( !progress && base_memory()->is_Phi() && can_reshape ) { 5765 // Check if PhiNode::Ideal's "Split phis through memory merges" 5766 // transform should be attempted. Look for this->phi->this cycle. 5767 uint merge_width = req(); 5768 if (merge_width > Compile::AliasIdxRaw) { 5769 PhiNode* phi = base_memory()->as_Phi(); 5770 for( uint i = 1; i < phi->req(); ++i ) {// For all paths in 5771 if (phi->in(i) == this) { 5772 phase->is_IterGVN()->_worklist.push(phi); 5773 break; 5774 } 5775 } 5776 } 5777 } 5778 5779 assert(progress || verify_sparse(), "please, no dups of base"); 5780 return progress; 5781 } 5782 5783 //-------------------------set_base_memory------------------------------------- 5784 void MergeMemNode::set_base_memory(Node *new_base) { 5785 Node* empty_mem = empty_memory(); 5786 set_req(Compile::AliasIdxBot, new_base); 5787 assert(memory_at(req()) == new_base, "must set default memory"); 5788 // Clear out other occurrences of new_base: 5789 if (new_base != empty_mem) { 5790 for (uint i = Compile::AliasIdxRaw; i < req(); i++) { 5791 if (in(i) == new_base) set_req(i, empty_mem); 5792 } 5793 } 5794 } 5795 5796 //------------------------------out_RegMask------------------------------------ 5797 const RegMask &MergeMemNode::out_RegMask() const { 5798 return RegMask::Empty; 5799 } 5800 5801 //------------------------------dump_spec-------------------------------------- 5802 #ifndef PRODUCT 5803 void MergeMemNode::dump_spec(outputStream *st) const { 5804 st->print(" {"); 5805 Node* base_mem = base_memory(); 5806 for( uint i = Compile::AliasIdxRaw; i < req(); i++ ) { 5807 Node* mem = (in(i) != nullptr) ? memory_at(i) : base_mem; 5808 if (mem == base_mem) { st->print(" -"); continue; } 5809 st->print( " N%d:", mem->_idx ); 5810 Compile::current()->get_adr_type(i)->dump_on(st); 5811 } 5812 st->print(" }"); 5813 } 5814 #endif // !PRODUCT 5815 5816 5817 #ifdef ASSERT 5818 static bool might_be_same(Node* a, Node* b) { 5819 if (a == b) return true; 5820 if (!(a->is_Phi() || b->is_Phi())) return false; 5821 // phis shift around during optimization 5822 return true; // pretty stupid... 5823 } 5824 5825 // verify a narrow slice (either incoming or outgoing) 5826 static void verify_memory_slice(const MergeMemNode* m, int alias_idx, Node* n) { 5827 if (!VerifyAliases) return; // don't bother to verify unless requested 5828 if (VMError::is_error_reported()) return; // muzzle asserts when debugging an error 5829 if (Node::in_dump()) return; // muzzle asserts when printing 5830 assert(alias_idx >= Compile::AliasIdxRaw, "must not disturb base_memory or sentinel"); 5831 assert(n != nullptr, ""); 5832 // Elide intervening MergeMem's 5833 while (n->is_MergeMem()) { 5834 n = n->as_MergeMem()->memory_at(alias_idx); 5835 } 5836 Compile* C = Compile::current(); 5837 const TypePtr* n_adr_type = n->adr_type(); 5838 if (n == m->empty_memory()) { 5839 // Implicit copy of base_memory() 5840 } else if (n_adr_type != TypePtr::BOTTOM) { 5841 assert(n_adr_type != nullptr, "new memory must have a well-defined adr_type"); 5842 assert(C->must_alias(n_adr_type, alias_idx), "new memory must match selected slice"); 5843 } else { 5844 // A few places like make_runtime_call "know" that VM calls are narrow, 5845 // and can be used to update only the VM bits stored as TypeRawPtr::BOTTOM. 5846 bool expected_wide_mem = false; 5847 if (n == m->base_memory()) { 5848 expected_wide_mem = true; 5849 } else if (alias_idx == Compile::AliasIdxRaw || 5850 n == m->memory_at(Compile::AliasIdxRaw)) { 5851 expected_wide_mem = true; 5852 } else if (!C->alias_type(alias_idx)->is_rewritable()) { 5853 // memory can "leak through" calls on channels that 5854 // are write-once. Allow this also. 5855 expected_wide_mem = true; 5856 } 5857 assert(expected_wide_mem, "expected narrow slice replacement"); 5858 } 5859 } 5860 #else // !ASSERT 5861 #define verify_memory_slice(m,i,n) (void)(0) // PRODUCT version is no-op 5862 #endif 5863 5864 5865 //-----------------------------memory_at--------------------------------------- 5866 Node* MergeMemNode::memory_at(uint alias_idx) const { 5867 assert(alias_idx >= Compile::AliasIdxRaw || 5868 (alias_idx == Compile::AliasIdxBot && !Compile::current()->do_aliasing()), 5869 "must avoid base_memory and AliasIdxTop"); 5870 5871 // Otherwise, it is a narrow slice. 5872 Node* n = alias_idx < req() ? in(alias_idx) : empty_memory(); 5873 if (is_empty_memory(n)) { 5874 // the array is sparse; empty slots are the "top" node 5875 n = base_memory(); 5876 assert(Node::in_dump() 5877 || n == nullptr || n->bottom_type() == Type::TOP 5878 || n->adr_type() == nullptr // address is TOP 5879 || n->adr_type() == TypePtr::BOTTOM 5880 || n->adr_type() == TypeRawPtr::BOTTOM 5881 || !Compile::current()->do_aliasing(), 5882 "must be a wide memory"); 5883 // do_aliasing == false if we are organizing the memory states manually. 5884 // See verify_memory_slice for comments on TypeRawPtr::BOTTOM. 5885 } else { 5886 // make sure the stored slice is sane 5887 #ifdef ASSERT 5888 if (VMError::is_error_reported() || Node::in_dump()) { 5889 } else if (might_be_same(n, base_memory())) { 5890 // Give it a pass: It is a mostly harmless repetition of the base. 5891 // This can arise normally from node subsumption during optimization. 5892 } else { 5893 verify_memory_slice(this, alias_idx, n); 5894 } 5895 #endif 5896 } 5897 return n; 5898 } 5899 5900 //---------------------------set_memory_at------------------------------------- 5901 void MergeMemNode::set_memory_at(uint alias_idx, Node *n) { 5902 verify_memory_slice(this, alias_idx, n); 5903 Node* empty_mem = empty_memory(); 5904 if (n == base_memory()) n = empty_mem; // collapse default 5905 uint need_req = alias_idx+1; 5906 if (req() < need_req) { 5907 if (n == empty_mem) return; // already the default, so do not grow me 5908 // grow the sparse array 5909 do { 5910 add_req(empty_mem); 5911 } while (req() < need_req); 5912 } 5913 set_req( alias_idx, n ); 5914 } 5915 5916 5917 5918 //--------------------------iteration_setup------------------------------------ 5919 void MergeMemNode::iteration_setup(const MergeMemNode* other) { 5920 if (other != nullptr) { 5921 grow_to_match(other); 5922 // invariant: the finite support of mm2 is within mm->req() 5923 #ifdef ASSERT 5924 for (uint i = req(); i < other->req(); i++) { 5925 assert(other->is_empty_memory(other->in(i)), "slice left uncovered"); 5926 } 5927 #endif 5928 } 5929 // Replace spurious copies of base_memory by top. 5930 Node* base_mem = base_memory(); 5931 if (base_mem != nullptr && !base_mem->is_top()) { 5932 for (uint i = Compile::AliasIdxBot+1, imax = req(); i < imax; i++) { 5933 if (in(i) == base_mem) 5934 set_req(i, empty_memory()); 5935 } 5936 } 5937 } 5938 5939 //---------------------------grow_to_match------------------------------------- 5940 void MergeMemNode::grow_to_match(const MergeMemNode* other) { 5941 Node* empty_mem = empty_memory(); 5942 assert(other->is_empty_memory(empty_mem), "consistent sentinels"); 5943 // look for the finite support of the other memory 5944 for (uint i = other->req(); --i >= req(); ) { 5945 if (other->in(i) != empty_mem) { 5946 uint new_len = i+1; 5947 while (req() < new_len) add_req(empty_mem); 5948 break; 5949 } 5950 } 5951 } 5952 5953 //---------------------------verify_sparse------------------------------------- 5954 #ifndef PRODUCT 5955 bool MergeMemNode::verify_sparse() const { 5956 assert(is_empty_memory(make_empty_memory()), "sane sentinel"); 5957 Node* base_mem = base_memory(); 5958 // The following can happen in degenerate cases, since empty==top. 5959 if (is_empty_memory(base_mem)) return true; 5960 for (uint i = Compile::AliasIdxRaw; i < req(); i++) { 5961 assert(in(i) != nullptr, "sane slice"); 5962 if (in(i) == base_mem) return false; // should have been the sentinel value! 5963 } 5964 return true; 5965 } 5966 5967 bool MergeMemStream::match_memory(Node* mem, const MergeMemNode* mm, int idx) { 5968 Node* n; 5969 n = mm->in(idx); 5970 if (mem == n) return true; // might be empty_memory() 5971 n = (idx == Compile::AliasIdxBot)? mm->base_memory(): mm->memory_at(idx); 5972 if (mem == n) return true; 5973 return false; 5974 } 5975 #endif // !PRODUCT