aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #include "precompiled.hpp" aoqi@0: #include "classfile/systemDictionary.hpp" aoqi@0: #include "compiler/compileLog.hpp" aoqi@0: #include "memory/allocation.inline.hpp" aoqi@0: #include "oops/objArrayKlass.hpp" aoqi@0: #include "opto/addnode.hpp" aoqi@0: #include "opto/cfgnode.hpp" aoqi@0: #include "opto/compile.hpp" aoqi@0: #include "opto/connode.hpp" aoqi@0: #include "opto/loopnode.hpp" aoqi@0: #include "opto/machnode.hpp" aoqi@0: #include "opto/matcher.hpp" aoqi@0: #include "opto/memnode.hpp" aoqi@0: #include "opto/mulnode.hpp" aoqi@0: #include "opto/phaseX.hpp" aoqi@0: #include "opto/regmask.hpp" aoqi@0: aoqi@0: // Portions of code courtesy of Clifford Click aoqi@0: aoqi@0: // Optimization - Graph Style aoqi@0: aoqi@0: static Node *step_through_mergemem(PhaseGVN *phase, MergeMemNode *mmem, const TypePtr *tp, const TypePtr *adr_check, outputStream *st); aoqi@0: aoqi@0: //============================================================================= aoqi@0: uint MemNode::size_of() const { return sizeof(*this); } aoqi@0: aoqi@0: const TypePtr *MemNode::adr_type() const { aoqi@0: Node* adr = in(Address); aoqi@0: const TypePtr* cross_check = NULL; aoqi@0: DEBUG_ONLY(cross_check = _adr_type); aoqi@0: return calculate_adr_type(adr->bottom_type(), cross_check); aoqi@0: } aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: void MemNode::dump_spec(outputStream *st) const { aoqi@0: if (in(Address) == NULL) return; // node is dead aoqi@0: #ifndef ASSERT aoqi@0: // fake the missing field aoqi@0: const TypePtr* _adr_type = NULL; aoqi@0: if (in(Address) != NULL) aoqi@0: _adr_type = in(Address)->bottom_type()->isa_ptr(); aoqi@0: #endif aoqi@0: dump_adr_type(this, _adr_type, st); aoqi@0: aoqi@0: Compile* C = Compile::current(); aoqi@0: if( C->alias_type(_adr_type)->is_volatile() ) aoqi@0: st->print(" Volatile!"); aoqi@0: } aoqi@0: aoqi@0: void MemNode::dump_adr_type(const Node* mem, const TypePtr* adr_type, outputStream *st) { aoqi@0: st->print(" @"); aoqi@0: if (adr_type == NULL) { aoqi@0: st->print("NULL"); aoqi@0: } else { aoqi@0: adr_type->dump_on(st); aoqi@0: Compile* C = Compile::current(); aoqi@0: Compile::AliasType* atp = NULL; aoqi@0: if (C->have_alias_type(adr_type)) atp = C->alias_type(adr_type); aoqi@0: if (atp == NULL) aoqi@0: st->print(", idx=?\?;"); aoqi@0: else if (atp->index() == Compile::AliasIdxBot) aoqi@0: st->print(", idx=Bot;"); aoqi@0: else if (atp->index() == Compile::AliasIdxTop) aoqi@0: st->print(", idx=Top;"); aoqi@0: else if (atp->index() == Compile::AliasIdxRaw) aoqi@0: st->print(", idx=Raw;"); aoqi@0: else { aoqi@0: ciField* field = atp->field(); aoqi@0: if (field) { aoqi@0: st->print(", name="); aoqi@0: field->print_name_on(st); aoqi@0: } aoqi@0: st->print(", idx=%d;", atp->index()); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: extern void print_alias_types(); aoqi@0: aoqi@0: #endif aoqi@0: aoqi@0: Node *MemNode::optimize_simple_memory_chain(Node *mchain, const TypeOopPtr *t_oop, Node *load, PhaseGVN *phase) { aoqi@0: assert((t_oop != NULL), "sanity"); aoqi@0: bool is_instance = t_oop->is_known_instance_field(); aoqi@0: bool is_boxed_value_load = t_oop->is_ptr_to_boxed_value() && aoqi@0: (load != NULL) && load->is_Load() && aoqi@0: (phase->is_IterGVN() != NULL); aoqi@0: if (!(is_instance || is_boxed_value_load)) aoqi@0: return mchain; // don't try to optimize non-instance types aoqi@0: uint instance_id = t_oop->instance_id(); aoqi@0: Node *start_mem = phase->C->start()->proj_out(TypeFunc::Memory); aoqi@0: Node *prev = NULL; aoqi@0: Node *result = mchain; aoqi@0: while (prev != result) { aoqi@0: prev = result; aoqi@0: if (result == start_mem) aoqi@0: break; // hit one of our sentinels aoqi@0: // skip over a call which does not affect this memory slice aoqi@0: if (result->is_Proj() && result->as_Proj()->_con == TypeFunc::Memory) { aoqi@0: Node *proj_in = result->in(0); aoqi@0: if (proj_in->is_Allocate() && proj_in->_idx == instance_id) { aoqi@0: break; // hit one of our sentinels aoqi@0: } else if (proj_in->is_Call()) { aoqi@0: CallNode *call = proj_in->as_Call(); aoqi@0: if (!call->may_modify(t_oop, phase)) { // returns false for instances aoqi@0: result = call->in(TypeFunc::Memory); aoqi@0: } aoqi@0: } else if (proj_in->is_Initialize()) { aoqi@0: AllocateNode* alloc = proj_in->as_Initialize()->allocation(); aoqi@0: // Stop if this is the initialization for the object instance which aoqi@0: // which contains this memory slice, otherwise skip over it. aoqi@0: if ((alloc == NULL) || (alloc->_idx == instance_id)) { aoqi@0: break; aoqi@0: } aoqi@0: if (is_instance) { aoqi@0: result = proj_in->in(TypeFunc::Memory); aoqi@0: } else if (is_boxed_value_load) { aoqi@0: Node* klass = alloc->in(AllocateNode::KlassNode); aoqi@0: const TypeKlassPtr* tklass = phase->type(klass)->is_klassptr(); aoqi@0: if (tklass->klass_is_exact() && !tklass->klass()->equals(t_oop->klass())) { aoqi@0: result = proj_in->in(TypeFunc::Memory); // not related allocation aoqi@0: } aoqi@0: } aoqi@0: } else if (proj_in->is_MemBar()) { aoqi@0: result = proj_in->in(TypeFunc::Memory); aoqi@0: } else { aoqi@0: assert(false, "unexpected projection"); aoqi@0: } aoqi@0: } else if (result->is_ClearArray()) { aoqi@0: if (!is_instance || !ClearArrayNode::step_through(&result, instance_id, phase)) { aoqi@0: // Can not bypass initialization of the instance aoqi@0: // we are looking for. aoqi@0: break; aoqi@0: } aoqi@0: // Otherwise skip it (the call updated 'result' value). aoqi@0: } else if (result->is_MergeMem()) { aoqi@0: result = step_through_mergemem(phase, result->as_MergeMem(), t_oop, NULL, tty); aoqi@0: } aoqi@0: } aoqi@0: return result; aoqi@0: } aoqi@0: aoqi@0: Node *MemNode::optimize_memory_chain(Node *mchain, const TypePtr *t_adr, Node *load, PhaseGVN *phase) { aoqi@0: const TypeOopPtr* t_oop = t_adr->isa_oopptr(); aoqi@0: if (t_oop == NULL) aoqi@0: return mchain; // don't try to optimize non-oop types aoqi@0: Node* result = optimize_simple_memory_chain(mchain, t_oop, load, phase); aoqi@0: bool is_instance = t_oop->is_known_instance_field(); aoqi@0: PhaseIterGVN *igvn = phase->is_IterGVN(); aoqi@0: if (is_instance && igvn != NULL && result->is_Phi()) { aoqi@0: PhiNode *mphi = result->as_Phi(); aoqi@0: assert(mphi->bottom_type() == Type::MEMORY, "memory phi required"); aoqi@0: const TypePtr *t = mphi->adr_type(); aoqi@0: if (t == TypePtr::BOTTOM || t == TypeRawPtr::BOTTOM || aoqi@0: t->isa_oopptr() && !t->is_oopptr()->is_known_instance() && aoqi@0: t->is_oopptr()->cast_to_exactness(true) aoqi@0: ->is_oopptr()->cast_to_ptr_type(t_oop->ptr()) aoqi@0: ->is_oopptr()->cast_to_instance_id(t_oop->instance_id()) == t_oop) { aoqi@0: // clone the Phi with our address type aoqi@0: result = mphi->split_out_instance(t_adr, igvn); aoqi@0: } else { aoqi@0: assert(phase->C->get_alias_index(t) == phase->C->get_alias_index(t_adr), "correct memory chain"); aoqi@0: } aoqi@0: } aoqi@0: return result; aoqi@0: } aoqi@0: aoqi@0: static Node *step_through_mergemem(PhaseGVN *phase, MergeMemNode *mmem, const TypePtr *tp, const TypePtr *adr_check, outputStream *st) { aoqi@0: uint alias_idx = phase->C->get_alias_index(tp); aoqi@0: Node *mem = mmem; aoqi@0: #ifdef ASSERT aoqi@0: { aoqi@0: // Check that current type is consistent with the alias index used during graph construction aoqi@0: assert(alias_idx >= Compile::AliasIdxRaw, "must not be a bad alias_idx"); aoqi@0: bool consistent = adr_check == NULL || adr_check->empty() || aoqi@0: phase->C->must_alias(adr_check, alias_idx ); aoqi@0: // Sometimes dead array references collapse to a[-1], a[-2], or a[-3] aoqi@0: if( !consistent && adr_check != NULL && !adr_check->empty() && aoqi@0: tp->isa_aryptr() && tp->offset() == Type::OffsetBot && aoqi@0: adr_check->isa_aryptr() && adr_check->offset() != Type::OffsetBot && aoqi@0: ( adr_check->offset() == arrayOopDesc::length_offset_in_bytes() || aoqi@0: adr_check->offset() == oopDesc::klass_offset_in_bytes() || aoqi@0: adr_check->offset() == oopDesc::mark_offset_in_bytes() ) ) { aoqi@0: // don't assert if it is dead code. aoqi@0: consistent = true; aoqi@0: } aoqi@0: if( !consistent ) { aoqi@0: st->print("alias_idx==%d, adr_check==", alias_idx); aoqi@0: if( adr_check == NULL ) { aoqi@0: st->print("NULL"); aoqi@0: } else { aoqi@0: adr_check->dump(); aoqi@0: } aoqi@0: st->cr(); aoqi@0: print_alias_types(); aoqi@0: assert(consistent, "adr_check must match alias idx"); aoqi@0: } aoqi@0: } aoqi@0: #endif aoqi@0: // TypeOopPtr::NOTNULL+any is an OOP with unknown offset - generally aoqi@0: // means an array I have not precisely typed yet. Do not do any aoqi@0: // alias stuff with it any time soon. aoqi@0: const TypeOopPtr *toop = tp->isa_oopptr(); aoqi@0: if( tp->base() != Type::AnyPtr && aoqi@0: !(toop && aoqi@0: toop->klass() != NULL && aoqi@0: toop->klass()->is_java_lang_Object() && aoqi@0: toop->offset() == Type::OffsetBot) ) { aoqi@0: // compress paths and change unreachable cycles to TOP aoqi@0: // If not, we can update the input infinitely along a MergeMem cycle aoqi@0: // Equivalent code in PhiNode::Ideal aoqi@0: Node* m = phase->transform(mmem); aoqi@0: // If transformed to a MergeMem, get the desired slice aoqi@0: // Otherwise the returned node represents memory for every slice aoqi@0: mem = (m->is_MergeMem())? m->as_MergeMem()->memory_at(alias_idx) : m; aoqi@0: // Update input if it is progress over what we have now aoqi@0: } aoqi@0: return mem; aoqi@0: } aoqi@0: aoqi@0: //--------------------------Ideal_common--------------------------------------- aoqi@0: // Look for degenerate control and memory inputs. Bypass MergeMem inputs. aoqi@0: // Unhook non-raw memories from complete (macro-expanded) initializations. aoqi@0: Node *MemNode::Ideal_common(PhaseGVN *phase, bool can_reshape) { aoqi@0: // If our control input is a dead region, kill all below the region aoqi@0: Node *ctl = in(MemNode::Control); aoqi@0: if (ctl && remove_dead_region(phase, can_reshape)) aoqi@0: return this; aoqi@0: ctl = in(MemNode::Control); aoqi@0: // Don't bother trying to transform a dead node aoqi@0: if (ctl && ctl->is_top()) return NodeSentinel; aoqi@0: aoqi@0: PhaseIterGVN *igvn = phase->is_IterGVN(); aoqi@0: // Wait if control on the worklist. aoqi@0: if (ctl && can_reshape && igvn != NULL) { aoqi@0: Node* bol = NULL; aoqi@0: Node* cmp = NULL; aoqi@0: if (ctl->in(0)->is_If()) { aoqi@0: assert(ctl->is_IfTrue() || ctl->is_IfFalse(), "sanity"); aoqi@0: bol = ctl->in(0)->in(1); aoqi@0: if (bol->is_Bool()) aoqi@0: cmp = ctl->in(0)->in(1)->in(1); aoqi@0: } aoqi@0: if (igvn->_worklist.member(ctl) || aoqi@0: (bol != NULL && igvn->_worklist.member(bol)) || aoqi@0: (cmp != NULL && igvn->_worklist.member(cmp)) ) { aoqi@0: // This control path may be dead. aoqi@0: // Delay this memory node transformation until the control is processed. aoqi@0: phase->is_IterGVN()->_worklist.push(this); aoqi@0: return NodeSentinel; // caller will return NULL aoqi@0: } aoqi@0: } aoqi@0: // Ignore if memory is dead, or self-loop aoqi@0: Node *mem = in(MemNode::Memory); aoqi@0: if (phase->type( mem ) == Type::TOP) return NodeSentinel; // caller will return NULL aoqi@0: assert(mem != this, "dead loop in MemNode::Ideal"); aoqi@0: aoqi@0: if (can_reshape && igvn != NULL && igvn->_worklist.member(mem)) { aoqi@0: // This memory slice may be dead. aoqi@0: // Delay this mem node transformation until the memory is processed. aoqi@0: phase->is_IterGVN()->_worklist.push(this); aoqi@0: return NodeSentinel; // caller will return NULL aoqi@0: } aoqi@0: aoqi@0: Node *address = in(MemNode::Address); aoqi@0: const Type *t_adr = phase->type(address); aoqi@0: if (t_adr == Type::TOP) return NodeSentinel; // caller will return NULL aoqi@0: aoqi@0: if (can_reshape && igvn != NULL && aoqi@0: (igvn->_worklist.member(address) || aoqi@0: igvn->_worklist.size() > 0 && (t_adr != adr_type())) ) { aoqi@0: // The address's base and type may change when the address is processed. aoqi@0: // Delay this mem node transformation until the address is processed. aoqi@0: phase->is_IterGVN()->_worklist.push(this); aoqi@0: return NodeSentinel; // caller will return NULL aoqi@0: } aoqi@0: aoqi@0: // Do NOT remove or optimize the next lines: ensure a new alias index aoqi@0: // is allocated for an oop pointer type before Escape Analysis. aoqi@0: // Note: C++ will not remove it since the call has side effect. aoqi@0: if (t_adr->isa_oopptr()) { aoqi@0: int alias_idx = phase->C->get_alias_index(t_adr->is_ptr()); aoqi@0: } aoqi@0: aoqi@0: Node* base = NULL; aoqi@0: if (address->is_AddP()) { aoqi@0: base = address->in(AddPNode::Base); aoqi@0: } aoqi@0: if (base != NULL && phase->type(base)->higher_equal(TypePtr::NULL_PTR) && aoqi@0: !t_adr->isa_rawptr()) { aoqi@0: // Note: raw address has TOP base and top->higher_equal(TypePtr::NULL_PTR) is true. aoqi@0: // Skip this node optimization if its address has TOP base. aoqi@0: return NodeSentinel; // caller will return NULL aoqi@0: } aoqi@0: aoqi@0: // Avoid independent memory operations aoqi@0: Node* old_mem = mem; aoqi@0: aoqi@0: // The code which unhooks non-raw memories from complete (macro-expanded) aoqi@0: // initializations was removed. After macro-expansion all stores catched aoqi@0: // by Initialize node became raw stores and there is no information aoqi@0: // which memory slices they modify. So it is unsafe to move any memory aoqi@0: // operation above these stores. Also in most cases hooked non-raw memories aoqi@0: // were already unhooked by using information from detect_ptr_independence() aoqi@0: // and find_previous_store(). aoqi@0: aoqi@0: if (mem->is_MergeMem()) { aoqi@0: MergeMemNode* mmem = mem->as_MergeMem(); aoqi@0: const TypePtr *tp = t_adr->is_ptr(); aoqi@0: aoqi@0: mem = step_through_mergemem(phase, mmem, tp, adr_type(), tty); aoqi@0: } aoqi@0: aoqi@0: if (mem != old_mem) { aoqi@0: set_req(MemNode::Memory, mem); aoqi@0: if (can_reshape && old_mem->outcnt() == 0) { aoqi@0: igvn->_worklist.push(old_mem); aoqi@0: } aoqi@0: if (phase->type( mem ) == Type::TOP) return NodeSentinel; aoqi@0: return this; aoqi@0: } aoqi@0: aoqi@0: // let the subclass continue analyzing... aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: // Helper function for proving some simple control dominations. aoqi@0: // Attempt to prove that all control inputs of 'dom' dominate 'sub'. aoqi@0: // Already assumes that 'dom' is available at 'sub', and that 'sub' aoqi@0: // is not a constant (dominated by the method's StartNode). aoqi@0: // Used by MemNode::find_previous_store to prove that the aoqi@0: // control input of a memory operation predates (dominates) aoqi@0: // an allocation it wants to look past. aoqi@0: bool MemNode::all_controls_dominate(Node* dom, Node* sub) { aoqi@0: if (dom == NULL || dom->is_top() || sub == NULL || sub->is_top()) aoqi@0: return false; // Conservative answer for dead code aoqi@0: aoqi@0: // Check 'dom'. Skip Proj and CatchProj nodes. aoqi@0: dom = dom->find_exact_control(dom); aoqi@0: if (dom == NULL || dom->is_top()) aoqi@0: return false; // Conservative answer for dead code aoqi@0: aoqi@0: if (dom == sub) { aoqi@0: // For the case when, for example, 'sub' is Initialize and the original aoqi@0: // 'dom' is Proj node of the 'sub'. aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: if (dom->is_Con() || dom->is_Start() || dom->is_Root() || dom == sub) aoqi@0: return true; aoqi@0: aoqi@0: // 'dom' dominates 'sub' if its control edge and control edges aoqi@0: // of all its inputs dominate or equal to sub's control edge. aoqi@0: aoqi@0: // Currently 'sub' is either Allocate, Initialize or Start nodes. aoqi@0: // Or Region for the check in LoadNode::Ideal(); aoqi@0: // 'sub' should have sub->in(0) != NULL. aoqi@0: assert(sub->is_Allocate() || sub->is_Initialize() || sub->is_Start() || aoqi@0: sub->is_Region() || sub->is_Call(), "expecting only these nodes"); aoqi@0: aoqi@0: // Get control edge of 'sub'. aoqi@0: Node* orig_sub = sub; aoqi@0: sub = sub->find_exact_control(sub->in(0)); aoqi@0: if (sub == NULL || sub->is_top()) aoqi@0: return false; // Conservative answer for dead code aoqi@0: aoqi@0: assert(sub->is_CFG(), "expecting control"); aoqi@0: aoqi@0: if (sub == dom) aoqi@0: return true; aoqi@0: aoqi@0: if (sub->is_Start() || sub->is_Root()) aoqi@0: return false; aoqi@0: aoqi@0: { aoqi@0: // Check all control edges of 'dom'. aoqi@0: aoqi@0: ResourceMark rm; aoqi@0: Arena* arena = Thread::current()->resource_area(); aoqi@0: Node_List nlist(arena); aoqi@0: Unique_Node_List dom_list(arena); aoqi@0: aoqi@0: dom_list.push(dom); aoqi@0: bool only_dominating_controls = false; aoqi@0: aoqi@0: for (uint next = 0; next < dom_list.size(); next++) { aoqi@0: Node* n = dom_list.at(next); aoqi@0: if (n == orig_sub) aoqi@0: return false; // One of dom's inputs dominated by sub. aoqi@0: if (!n->is_CFG() && n->pinned()) { aoqi@0: // Check only own control edge for pinned non-control nodes. aoqi@0: n = n->find_exact_control(n->in(0)); aoqi@0: if (n == NULL || n->is_top()) aoqi@0: return false; // Conservative answer for dead code aoqi@0: assert(n->is_CFG(), "expecting control"); aoqi@0: dom_list.push(n); aoqi@0: } else if (n->is_Con() || n->is_Start() || n->is_Root()) { aoqi@0: only_dominating_controls = true; aoqi@0: } else if (n->is_CFG()) { aoqi@0: if (n->dominates(sub, nlist)) aoqi@0: only_dominating_controls = true; aoqi@0: else aoqi@0: return false; aoqi@0: } else { aoqi@0: // First, own control edge. aoqi@0: Node* m = n->find_exact_control(n->in(0)); aoqi@0: if (m != NULL) { aoqi@0: if (m->is_top()) aoqi@0: return false; // Conservative answer for dead code aoqi@0: dom_list.push(m); aoqi@0: } aoqi@0: // Now, the rest of edges. aoqi@0: uint cnt = n->req(); aoqi@0: for (uint i = 1; i < cnt; i++) { aoqi@0: m = n->find_exact_control(n->in(i)); aoqi@0: if (m == NULL || m->is_top()) aoqi@0: continue; aoqi@0: dom_list.push(m); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: return only_dominating_controls; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: //---------------------detect_ptr_independence--------------------------------- aoqi@0: // Used by MemNode::find_previous_store to prove that two base aoqi@0: // pointers are never equal. aoqi@0: // The pointers are accompanied by their associated allocations, aoqi@0: // if any, which have been previously discovered by the caller. aoqi@0: bool MemNode::detect_ptr_independence(Node* p1, AllocateNode* a1, aoqi@0: Node* p2, AllocateNode* a2, aoqi@0: PhaseTransform* phase) { aoqi@0: // Attempt to prove that these two pointers cannot be aliased. aoqi@0: // They may both manifestly be allocations, and they should differ. aoqi@0: // Or, if they are not both allocations, they can be distinct constants. aoqi@0: // Otherwise, one is an allocation and the other a pre-existing value. aoqi@0: if (a1 == NULL && a2 == NULL) { // neither an allocation aoqi@0: return (p1 != p2) && p1->is_Con() && p2->is_Con(); aoqi@0: } else if (a1 != NULL && a2 != NULL) { // both allocations aoqi@0: return (a1 != a2); aoqi@0: } else if (a1 != NULL) { // one allocation a1 aoqi@0: // (Note: p2->is_Con implies p2->in(0)->is_Root, which dominates.) aoqi@0: return all_controls_dominate(p2, a1); aoqi@0: } else { //(a2 != NULL) // one allocation a2 aoqi@0: return all_controls_dominate(p1, a2); aoqi@0: } aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // The logic for reordering loads and stores uses four steps: aoqi@0: // (a) Walk carefully past stores and initializations which we aoqi@0: // can prove are independent of this load. aoqi@0: // (b) Observe that the next memory state makes an exact match aoqi@0: // with self (load or store), and locate the relevant store. aoqi@0: // (c) Ensure that, if we were to wire self directly to the store, aoqi@0: // the optimizer would fold it up somehow. aoqi@0: // (d) Do the rewiring, and return, depending on some other part of aoqi@0: // the optimizer to fold up the load. aoqi@0: // This routine handles steps (a) and (b). Steps (c) and (d) are aoqi@0: // specific to loads and stores, so they are handled by the callers. aoqi@0: // (Currently, only LoadNode::Ideal has steps (c), (d). More later.) aoqi@0: // aoqi@0: Node* MemNode::find_previous_store(PhaseTransform* phase) { aoqi@0: Node* ctrl = in(MemNode::Control); aoqi@0: Node* adr = in(MemNode::Address); aoqi@0: intptr_t offset = 0; aoqi@0: Node* base = AddPNode::Ideal_base_and_offset(adr, phase, offset); aoqi@0: AllocateNode* alloc = AllocateNode::Ideal_allocation(base, phase); aoqi@0: aoqi@0: if (offset == Type::OffsetBot) aoqi@0: return NULL; // cannot unalias unless there are precise offsets aoqi@0: aoqi@0: const TypeOopPtr *addr_t = adr->bottom_type()->isa_oopptr(); aoqi@0: aoqi@0: intptr_t size_in_bytes = memory_size(); aoqi@0: aoqi@0: Node* mem = in(MemNode::Memory); // start searching here... aoqi@0: aoqi@0: int cnt = 50; // Cycle limiter aoqi@0: for (;;) { // While we can dance past unrelated stores... aoqi@0: if (--cnt < 0) break; // Caught in cycle or a complicated dance? aoqi@0: aoqi@0: if (mem->is_Store()) { aoqi@0: Node* st_adr = mem->in(MemNode::Address); aoqi@0: intptr_t st_offset = 0; aoqi@0: Node* st_base = AddPNode::Ideal_base_and_offset(st_adr, phase, st_offset); aoqi@0: if (st_base == NULL) aoqi@0: break; // inscrutable pointer aoqi@0: if (st_offset != offset && st_offset != Type::OffsetBot) { aoqi@0: const int MAX_STORE = BytesPerLong; aoqi@0: if (st_offset >= offset + size_in_bytes || aoqi@0: st_offset <= offset - MAX_STORE || aoqi@0: st_offset <= offset - mem->as_Store()->memory_size()) { aoqi@0: // Success: The offsets are provably independent. aoqi@0: // (You may ask, why not just test st_offset != offset and be done? aoqi@0: // The answer is that stores of different sizes can co-exist aoqi@0: // in the same sequence of RawMem effects. We sometimes initialize aoqi@0: // a whole 'tile' of array elements with a single jint or jlong.) aoqi@0: mem = mem->in(MemNode::Memory); aoqi@0: continue; // (a) advance through independent store memory aoqi@0: } aoqi@0: } aoqi@0: if (st_base != base && aoqi@0: detect_ptr_independence(base, alloc, aoqi@0: st_base, aoqi@0: AllocateNode::Ideal_allocation(st_base, phase), aoqi@0: phase)) { aoqi@0: // Success: The bases are provably independent. aoqi@0: mem = mem->in(MemNode::Memory); aoqi@0: continue; // (a) advance through independent store memory aoqi@0: } aoqi@0: aoqi@0: // (b) At this point, if the bases or offsets do not agree, we lose, aoqi@0: // since we have not managed to prove 'this' and 'mem' independent. aoqi@0: if (st_base == base && st_offset == offset) { aoqi@0: return mem; // let caller handle steps (c), (d) aoqi@0: } aoqi@0: aoqi@0: } else if (mem->is_Proj() && mem->in(0)->is_Initialize()) { aoqi@0: InitializeNode* st_init = mem->in(0)->as_Initialize(); aoqi@0: AllocateNode* st_alloc = st_init->allocation(); aoqi@0: if (st_alloc == NULL) aoqi@0: break; // something degenerated aoqi@0: bool known_identical = false; aoqi@0: bool known_independent = false; aoqi@0: if (alloc == st_alloc) aoqi@0: known_identical = true; aoqi@0: else if (alloc != NULL) aoqi@0: known_independent = true; aoqi@0: else if (all_controls_dominate(this, st_alloc)) aoqi@0: known_independent = true; aoqi@0: aoqi@0: if (known_independent) { aoqi@0: // The bases are provably independent: Either they are aoqi@0: // manifestly distinct allocations, or else the control aoqi@0: // of this load dominates the store's allocation. aoqi@0: int alias_idx = phase->C->get_alias_index(adr_type()); aoqi@0: if (alias_idx == Compile::AliasIdxRaw) { aoqi@0: mem = st_alloc->in(TypeFunc::Memory); aoqi@0: } else { aoqi@0: mem = st_init->memory(alias_idx); aoqi@0: } aoqi@0: continue; // (a) advance through independent store memory aoqi@0: } aoqi@0: aoqi@0: // (b) at this point, if we are not looking at a store initializing aoqi@0: // the same allocation we are loading from, we lose. aoqi@0: if (known_identical) { aoqi@0: // From caller, can_see_stored_value will consult find_captured_store. aoqi@0: return mem; // let caller handle steps (c), (d) aoqi@0: } aoqi@0: aoqi@0: } else if (addr_t != NULL && addr_t->is_known_instance_field()) { aoqi@0: // Can't use optimize_simple_memory_chain() since it needs PhaseGVN. aoqi@0: if (mem->is_Proj() && mem->in(0)->is_Call()) { aoqi@0: CallNode *call = mem->in(0)->as_Call(); aoqi@0: if (!call->may_modify(addr_t, phase)) { aoqi@0: mem = call->in(TypeFunc::Memory); aoqi@0: continue; // (a) advance through independent call memory aoqi@0: } aoqi@0: } else if (mem->is_Proj() && mem->in(0)->is_MemBar()) { aoqi@0: mem = mem->in(0)->in(TypeFunc::Memory); aoqi@0: continue; // (a) advance through independent MemBar memory aoqi@0: } else if (mem->is_ClearArray()) { aoqi@0: if (ClearArrayNode::step_through(&mem, (uint)addr_t->instance_id(), phase)) { aoqi@0: // (the call updated 'mem' value) aoqi@0: continue; // (a) advance through independent allocation memory aoqi@0: } else { aoqi@0: // Can not bypass initialization of the instance aoqi@0: // we are looking for. aoqi@0: return mem; aoqi@0: } aoqi@0: } else if (mem->is_MergeMem()) { aoqi@0: int alias_idx = phase->C->get_alias_index(adr_type()); aoqi@0: mem = mem->as_MergeMem()->memory_at(alias_idx); aoqi@0: continue; // (a) advance through independent MergeMem memory aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Unless there is an explicit 'continue', we must bail out here, aoqi@0: // because 'mem' is an inscrutable memory state (e.g., a call). aoqi@0: break; aoqi@0: } aoqi@0: aoqi@0: return NULL; // bail out aoqi@0: } aoqi@0: aoqi@0: //----------------------calculate_adr_type------------------------------------- aoqi@0: // Helper function. Notices when the given type of address hits top or bottom. aoqi@0: // Also, asserts a cross-check of the type against the expected address type. aoqi@0: const TypePtr* MemNode::calculate_adr_type(const Type* t, const TypePtr* cross_check) { aoqi@0: if (t == Type::TOP) return NULL; // does not touch memory any more? aoqi@0: #ifdef PRODUCT aoqi@0: cross_check = NULL; aoqi@0: #else aoqi@0: if (!VerifyAliases || is_error_reported() || Node::in_dump()) cross_check = NULL; aoqi@0: #endif aoqi@0: const TypePtr* tp = t->isa_ptr(); aoqi@0: if (tp == NULL) { aoqi@0: assert(cross_check == NULL || cross_check == TypePtr::BOTTOM, "expected memory type must be wide"); aoqi@0: return TypePtr::BOTTOM; // touches lots of memory aoqi@0: } else { aoqi@0: #ifdef ASSERT aoqi@0: // %%%% [phh] We don't check the alias index if cross_check is aoqi@0: // TypeRawPtr::BOTTOM. Needs to be investigated. aoqi@0: if (cross_check != NULL && aoqi@0: cross_check != TypePtr::BOTTOM && aoqi@0: cross_check != TypeRawPtr::BOTTOM) { aoqi@0: // Recheck the alias index, to see if it has changed (due to a bug). aoqi@0: Compile* C = Compile::current(); aoqi@0: assert(C->get_alias_index(cross_check) == C->get_alias_index(tp), aoqi@0: "must stay in the original alias category"); aoqi@0: // The type of the address must be contained in the adr_type, aoqi@0: // disregarding "null"-ness. aoqi@0: // (We make an exception for TypeRawPtr::BOTTOM, which is a bit bucket.) aoqi@0: const TypePtr* tp_notnull = tp->join(TypePtr::NOTNULL)->is_ptr(); aoqi@0: assert(cross_check->meet(tp_notnull) == cross_check->remove_speculative(), aoqi@0: "real address must not escape from expected memory type"); aoqi@0: } aoqi@0: #endif aoqi@0: return tp; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: //------------------------adr_phi_is_loop_invariant---------------------------- aoqi@0: // A helper function for Ideal_DU_postCCP to check if a Phi in a counted aoqi@0: // loop is loop invariant. Make a quick traversal of Phi and associated aoqi@0: // CastPP nodes, looking to see if they are a closed group within the loop. aoqi@0: bool MemNode::adr_phi_is_loop_invariant(Node* adr_phi, Node* cast) { aoqi@0: // The idea is that the phi-nest must boil down to only CastPP nodes aoqi@0: // with the same data. This implies that any path into the loop already aoqi@0: // includes such a CastPP, and so the original cast, whatever its input, aoqi@0: // must be covered by an equivalent cast, with an earlier control input. aoqi@0: ResourceMark rm; aoqi@0: aoqi@0: // The loop entry input of the phi should be the unique dominating aoqi@0: // node for every Phi/CastPP in the loop. aoqi@0: Unique_Node_List closure; aoqi@0: closure.push(adr_phi->in(LoopNode::EntryControl)); aoqi@0: aoqi@0: // Add the phi node and the cast to the worklist. aoqi@0: Unique_Node_List worklist; aoqi@0: worklist.push(adr_phi); aoqi@0: if( cast != NULL ){ aoqi@0: if( !cast->is_ConstraintCast() ) return false; aoqi@0: worklist.push(cast); aoqi@0: } aoqi@0: aoqi@0: // Begin recursive walk of phi nodes. aoqi@0: while( worklist.size() ){ aoqi@0: // Take a node off the worklist aoqi@0: Node *n = worklist.pop(); aoqi@0: if( !closure.member(n) ){ aoqi@0: // Add it to the closure. aoqi@0: closure.push(n); aoqi@0: // Make a sanity check to ensure we don't waste too much time here. aoqi@0: if( closure.size() > 20) return false; aoqi@0: // This node is OK if: aoqi@0: // - it is a cast of an identical value aoqi@0: // - or it is a phi node (then we add its inputs to the worklist) aoqi@0: // Otherwise, the node is not OK, and we presume the cast is not invariant aoqi@0: if( n->is_ConstraintCast() ){ aoqi@0: worklist.push(n->in(1)); aoqi@0: } else if( n->is_Phi() ) { aoqi@0: for( uint i = 1; i < n->req(); i++ ) { aoqi@0: worklist.push(n->in(i)); aoqi@0: } aoqi@0: } else { aoqi@0: return false; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Quit when the worklist is empty, and we've found no offending nodes. aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: //------------------------------Ideal_DU_postCCP------------------------------- aoqi@0: // Find any cast-away of null-ness and keep its control. Null cast-aways are aoqi@0: // going away in this pass and we need to make this memory op depend on the aoqi@0: // gating null check. aoqi@0: Node *MemNode::Ideal_DU_postCCP( PhaseCCP *ccp ) { aoqi@0: return Ideal_common_DU_postCCP(ccp, this, in(MemNode::Address)); aoqi@0: } aoqi@0: aoqi@0: // I tried to leave the CastPP's in. This makes the graph more accurate in aoqi@0: // some sense; we get to keep around the knowledge that an oop is not-null aoqi@0: // after some test. Alas, the CastPP's interfere with GVN (some values are aoqi@0: // the regular oop, some are the CastPP of the oop, all merge at Phi's which aoqi@0: // cannot collapse, etc). This cost us 10% on SpecJVM, even when I removed aoqi@0: // some of the more trivial cases in the optimizer. Removing more useless aoqi@0: // Phi's started allowing Loads to illegally float above null checks. I gave aoqi@0: // up on this approach. CNC 10/20/2000 aoqi@0: // This static method may be called not from MemNode (EncodePNode calls it). aoqi@0: // Only the control edge of the node 'n' might be updated. aoqi@0: Node *MemNode::Ideal_common_DU_postCCP( PhaseCCP *ccp, Node* n, Node* adr ) { aoqi@0: Node *skipped_cast = NULL; aoqi@0: // Need a null check? Regular static accesses do not because they are aoqi@0: // from constant addresses. Array ops are gated by the range check (which aoqi@0: // always includes a NULL check). Just check field ops. aoqi@0: if( n->in(MemNode::Control) == NULL ) { aoqi@0: // Scan upwards for the highest location we can place this memory op. aoqi@0: while( true ) { aoqi@0: switch( adr->Opcode() ) { aoqi@0: aoqi@0: case Op_AddP: // No change to NULL-ness, so peek thru AddP's aoqi@0: adr = adr->in(AddPNode::Base); aoqi@0: continue; aoqi@0: aoqi@0: case Op_DecodeN: // No change to NULL-ness, so peek thru aoqi@0: case Op_DecodeNKlass: aoqi@0: adr = adr->in(1); aoqi@0: continue; aoqi@0: aoqi@0: case Op_EncodeP: aoqi@0: case Op_EncodePKlass: aoqi@0: // EncodeP node's control edge could be set by this method aoqi@0: // when EncodeP node depends on CastPP node. aoqi@0: // aoqi@0: // Use its control edge for memory op because EncodeP may go away aoqi@0: // later when it is folded with following or preceding DecodeN node. aoqi@0: if (adr->in(0) == NULL) { aoqi@0: // Keep looking for cast nodes. aoqi@0: adr = adr->in(1); aoqi@0: continue; aoqi@0: } aoqi@0: ccp->hash_delete(n); aoqi@0: n->set_req(MemNode::Control, adr->in(0)); aoqi@0: ccp->hash_insert(n); aoqi@0: return n; aoqi@0: aoqi@0: case Op_CastPP: aoqi@0: // If the CastPP is useless, just peek on through it. aoqi@0: if( ccp->type(adr) == ccp->type(adr->in(1)) ) { aoqi@0: // Remember the cast that we've peeked though. If we peek aoqi@0: // through more than one, then we end up remembering the highest aoqi@0: // one, that is, if in a loop, the one closest to the top. aoqi@0: skipped_cast = adr; aoqi@0: adr = adr->in(1); aoqi@0: continue; aoqi@0: } aoqi@0: // CastPP is going away in this pass! We need this memory op to be aoqi@0: // control-dependent on the test that is guarding the CastPP. aoqi@0: ccp->hash_delete(n); aoqi@0: n->set_req(MemNode::Control, adr->in(0)); aoqi@0: ccp->hash_insert(n); aoqi@0: return n; aoqi@0: aoqi@0: case Op_Phi: aoqi@0: // Attempt to float above a Phi to some dominating point. aoqi@0: if (adr->in(0) != NULL && adr->in(0)->is_CountedLoop()) { aoqi@0: // If we've already peeked through a Cast (which could have set the aoqi@0: // control), we can't float above a Phi, because the skipped Cast aoqi@0: // may not be loop invariant. aoqi@0: if (adr_phi_is_loop_invariant(adr, skipped_cast)) { aoqi@0: adr = adr->in(1); aoqi@0: continue; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Intentional fallthrough! aoqi@0: aoqi@0: // No obvious dominating point. The mem op is pinned below the Phi aoqi@0: // by the Phi itself. If the Phi goes away (no true value is merged) aoqi@0: // then the mem op can float, but not indefinitely. It must be pinned aoqi@0: // behind the controls leading to the Phi. aoqi@0: case Op_CheckCastPP: aoqi@0: // These usually stick around to change address type, however a aoqi@0: // useless one can be elided and we still need to pick up a control edge aoqi@0: if (adr->in(0) == NULL) { aoqi@0: // This CheckCastPP node has NO control and is likely useless. But we aoqi@0: // need check further up the ancestor chain for a control input to keep aoqi@0: // the node in place. 4959717. aoqi@0: skipped_cast = adr; aoqi@0: adr = adr->in(1); aoqi@0: continue; aoqi@0: } aoqi@0: ccp->hash_delete(n); aoqi@0: n->set_req(MemNode::Control, adr->in(0)); aoqi@0: ccp->hash_insert(n); aoqi@0: return n; aoqi@0: aoqi@0: // List of "safe" opcodes; those that implicitly block the memory aoqi@0: // op below any null check. aoqi@0: case Op_CastX2P: // no null checks on native pointers aoqi@0: case Op_Parm: // 'this' pointer is not null aoqi@0: case Op_LoadP: // Loading from within a klass aoqi@0: case Op_LoadN: // Loading from within a klass aoqi@0: case Op_LoadKlass: // Loading from within a klass aoqi@0: case Op_LoadNKlass: // Loading from within a klass aoqi@0: case Op_ConP: // Loading from a klass aoqi@0: case Op_ConN: // Loading from a klass aoqi@0: case Op_ConNKlass: // Loading from a klass aoqi@0: case Op_CreateEx: // Sucking up the guts of an exception oop aoqi@0: case Op_Con: // Reading from TLS aoqi@0: case Op_CMoveP: // CMoveP is pinned aoqi@0: case Op_CMoveN: // CMoveN is pinned aoqi@0: break; // No progress aoqi@0: aoqi@0: case Op_Proj: // Direct call to an allocation routine aoqi@0: case Op_SCMemProj: // Memory state from store conditional ops aoqi@0: #ifdef ASSERT aoqi@0: { aoqi@0: assert(adr->as_Proj()->_con == TypeFunc::Parms, "must be return value"); aoqi@0: const Node* call = adr->in(0); aoqi@0: if (call->is_CallJava()) { aoqi@0: const CallJavaNode* call_java = call->as_CallJava(); aoqi@0: const TypeTuple *r = call_java->tf()->range(); aoqi@0: assert(r->cnt() > TypeFunc::Parms, "must return value"); aoqi@0: const Type* ret_type = r->field_at(TypeFunc::Parms); aoqi@0: assert(ret_type && ret_type->isa_ptr(), "must return pointer"); aoqi@0: // We further presume that this is one of aoqi@0: // new_instance_Java, new_array_Java, or aoqi@0: // the like, but do not assert for this. aoqi@0: } else if (call->is_Allocate()) { aoqi@0: // similar case to new_instance_Java, etc. aoqi@0: } else if (!call->is_CallLeaf()) { aoqi@0: // Projections from fetch_oop (OSR) are allowed as well. aoqi@0: ShouldNotReachHere(); aoqi@0: } aoqi@0: } aoqi@0: #endif aoqi@0: break; aoqi@0: default: aoqi@0: ShouldNotReachHere(); aoqi@0: } aoqi@0: break; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: return NULL; // No progress aoqi@0: } aoqi@0: aoqi@0: aoqi@0: //============================================================================= aoqi@0: uint LoadNode::size_of() const { return sizeof(*this); } aoqi@0: uint LoadNode::cmp( const Node &n ) const aoqi@0: { return !Type::cmp( _type, ((LoadNode&)n)._type ); } aoqi@0: const Type *LoadNode::bottom_type() const { return _type; } aoqi@0: uint LoadNode::ideal_reg() const { aoqi@0: return _type->ideal_reg(); aoqi@0: } aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: void LoadNode::dump_spec(outputStream *st) const { aoqi@0: MemNode::dump_spec(st); aoqi@0: if( !Verbose && !WizardMode ) { aoqi@0: // standard dump does this in Verbose and WizardMode aoqi@0: st->print(" #"); _type->dump_on(st); aoqi@0: } aoqi@0: } aoqi@0: #endif aoqi@0: aoqi@0: #ifdef ASSERT aoqi@0: //----------------------------is_immutable_value------------------------------- aoqi@0: // Helper function to allow a raw load without control edge for some cases aoqi@0: bool LoadNode::is_immutable_value(Node* adr) { aoqi@0: return (adr->is_AddP() && adr->in(AddPNode::Base)->is_top() && aoqi@0: adr->in(AddPNode::Address)->Opcode() == Op_ThreadLocal && aoqi@0: (adr->in(AddPNode::Offset)->find_intptr_t_con(-1) == aoqi@0: in_bytes(JavaThread::osthread_offset()))); aoqi@0: } aoqi@0: #endif aoqi@0: aoqi@0: //----------------------------LoadNode::make----------------------------------- aoqi@0: // Polymorphic factory method: aoqi@0: Node *LoadNode::make(PhaseGVN& gvn, Node *ctl, Node *mem, Node *adr, const TypePtr* adr_type, const Type *rt, BasicType bt, MemOrd mo) { aoqi@0: Compile* C = gvn.C; aoqi@0: aoqi@0: // sanity check the alias category against the created node type aoqi@0: assert(!(adr_type->isa_oopptr() && aoqi@0: adr_type->offset() == oopDesc::klass_offset_in_bytes()), aoqi@0: "use LoadKlassNode instead"); aoqi@0: assert(!(adr_type->isa_aryptr() && aoqi@0: adr_type->offset() == arrayOopDesc::length_offset_in_bytes()), aoqi@0: "use LoadRangeNode instead"); aoqi@0: // Check control edge of raw loads aoqi@0: assert( ctl != NULL || C->get_alias_index(adr_type) != Compile::AliasIdxRaw || aoqi@0: // oop will be recorded in oop map if load crosses safepoint aoqi@0: rt->isa_oopptr() || is_immutable_value(adr), aoqi@0: "raw memory operations should have control edge"); aoqi@0: switch (bt) { aoqi@0: case T_BOOLEAN: return new (C) LoadUBNode(ctl, mem, adr, adr_type, rt->is_int(), mo); aoqi@0: case T_BYTE: return new (C) LoadBNode (ctl, mem, adr, adr_type, rt->is_int(), mo); aoqi@0: case T_INT: return new (C) LoadINode (ctl, mem, adr, adr_type, rt->is_int(), mo); aoqi@0: case T_CHAR: return new (C) LoadUSNode(ctl, mem, adr, adr_type, rt->is_int(), mo); aoqi@0: case T_SHORT: return new (C) LoadSNode (ctl, mem, adr, adr_type, rt->is_int(), mo); aoqi@0: case T_LONG: return new (C) LoadLNode (ctl, mem, adr, adr_type, rt->is_long(), mo); aoqi@0: case T_FLOAT: return new (C) LoadFNode (ctl, mem, adr, adr_type, rt, mo); aoqi@0: case T_DOUBLE: return new (C) LoadDNode (ctl, mem, adr, adr_type, rt, mo); aoqi@0: case T_ADDRESS: return new (C) LoadPNode (ctl, mem, adr, adr_type, rt->is_ptr(), mo); aoqi@0: case T_OBJECT: aoqi@0: #ifdef _LP64 aoqi@0: if (adr->bottom_type()->is_ptr_to_narrowoop()) { aoqi@0: Node* load = gvn.transform(new (C) LoadNNode(ctl, mem, adr, adr_type, rt->make_narrowoop(), mo)); aoqi@0: return new (C) DecodeNNode(load, load->bottom_type()->make_ptr()); aoqi@0: } else aoqi@0: #endif aoqi@0: { aoqi@0: assert(!adr->bottom_type()->is_ptr_to_narrowoop() && !adr->bottom_type()->is_ptr_to_narrowklass(), "should have got back a narrow oop"); aoqi@0: return new (C) LoadPNode(ctl, mem, adr, adr_type, rt->is_oopptr(), mo); aoqi@0: } aoqi@0: } aoqi@0: ShouldNotReachHere(); aoqi@0: return (LoadNode*)NULL; aoqi@0: } aoqi@0: aoqi@0: LoadLNode* LoadLNode::make_atomic(Compile *C, Node* ctl, Node* mem, Node* adr, const TypePtr* adr_type, const Type* rt, MemOrd mo) { aoqi@0: bool require_atomic = true; aoqi@0: return new (C) LoadLNode(ctl, mem, adr, adr_type, rt->is_long(), mo, require_atomic); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: aoqi@0: aoqi@0: //------------------------------hash------------------------------------------- aoqi@0: uint LoadNode::hash() const { aoqi@0: // unroll addition of interesting fields aoqi@0: return (uintptr_t)in(Control) + (uintptr_t)in(Memory) + (uintptr_t)in(Address); aoqi@0: } aoqi@0: aoqi@0: static bool skip_through_membars(Compile::AliasType* atp, const TypeInstPtr* tp, bool eliminate_boxing) { aoqi@0: if ((atp != NULL) && (atp->index() >= Compile::AliasIdxRaw)) { aoqi@0: bool non_volatile = (atp->field() != NULL) && !atp->field()->is_volatile(); aoqi@0: bool is_stable_ary = FoldStableValues && aoqi@0: (tp != NULL) && (tp->isa_aryptr() != NULL) && aoqi@0: tp->isa_aryptr()->is_stable(); aoqi@0: aoqi@0: return (eliminate_boxing && non_volatile) || is_stable_ary; aoqi@0: } aoqi@0: aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: //---------------------------can_see_stored_value------------------------------ aoqi@0: // This routine exists to make sure this set of tests is done the same aoqi@0: // everywhere. We need to make a coordinated change: first LoadNode::Ideal aoqi@0: // will change the graph shape in a way which makes memory alive twice at the aoqi@0: // same time (uses the Oracle model of aliasing), then some aoqi@0: // LoadXNode::Identity will fold things back to the equivalence-class model aoqi@0: // of aliasing. aoqi@0: Node* MemNode::can_see_stored_value(Node* st, PhaseTransform* phase) const { aoqi@0: Node* ld_adr = in(MemNode::Address); aoqi@0: intptr_t ld_off = 0; aoqi@0: AllocateNode* ld_alloc = AllocateNode::Ideal_allocation(ld_adr, phase, ld_off); aoqi@0: const TypeInstPtr* tp = phase->type(ld_adr)->isa_instptr(); aoqi@0: Compile::AliasType* atp = (tp != NULL) ? phase->C->alias_type(tp) : NULL; aoqi@0: // This is more general than load from boxing objects. aoqi@0: if (skip_through_membars(atp, tp, phase->C->eliminate_boxing())) { aoqi@0: uint alias_idx = atp->index(); aoqi@0: bool final = !atp->is_rewritable(); aoqi@0: Node* result = NULL; aoqi@0: Node* current = st; aoqi@0: // Skip through chains of MemBarNodes checking the MergeMems for aoqi@0: // new states for the slice of this load. Stop once any other aoqi@0: // kind of node is encountered. Loads from final memory can skip aoqi@0: // through any kind of MemBar but normal loads shouldn't skip aoqi@0: // through MemBarAcquire since the could allow them to move out of aoqi@0: // a synchronized region. aoqi@0: while (current->is_Proj()) { aoqi@0: int opc = current->in(0)->Opcode(); aoqi@0: if ((final && (opc == Op_MemBarAcquire || aoqi@0: opc == Op_MemBarAcquireLock || aoqi@0: opc == Op_LoadFence)) || aoqi@0: opc == Op_MemBarRelease || aoqi@0: opc == Op_StoreFence || aoqi@0: opc == Op_MemBarReleaseLock || aoqi@0: opc == Op_MemBarCPUOrder) { aoqi@0: Node* mem = current->in(0)->in(TypeFunc::Memory); aoqi@0: if (mem->is_MergeMem()) { aoqi@0: MergeMemNode* merge = mem->as_MergeMem(); aoqi@0: Node* new_st = merge->memory_at(alias_idx); aoqi@0: if (new_st == merge->base_memory()) { aoqi@0: // Keep searching aoqi@0: current = new_st; aoqi@0: continue; aoqi@0: } aoqi@0: // Save the new memory state for the slice and fall through aoqi@0: // to exit. aoqi@0: result = new_st; aoqi@0: } aoqi@0: } aoqi@0: break; aoqi@0: } aoqi@0: if (result != NULL) { aoqi@0: st = result; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Loop around twice in the case Load -> Initialize -> Store. aoqi@0: // (See PhaseIterGVN::add_users_to_worklist, which knows about this case.) aoqi@0: for (int trip = 0; trip <= 1; trip++) { aoqi@0: aoqi@0: if (st->is_Store()) { aoqi@0: Node* st_adr = st->in(MemNode::Address); aoqi@0: if (!phase->eqv(st_adr, ld_adr)) { aoqi@0: // Try harder before giving up... Match raw and non-raw pointers. aoqi@0: intptr_t st_off = 0; aoqi@0: AllocateNode* alloc = AllocateNode::Ideal_allocation(st_adr, phase, st_off); aoqi@0: if (alloc == NULL) return NULL; aoqi@0: if (alloc != ld_alloc) return NULL; aoqi@0: if (ld_off != st_off) return NULL; aoqi@0: // At this point we have proven something like this setup: aoqi@0: // A = Allocate(...) aoqi@0: // L = LoadQ(, AddP(CastPP(, A.Parm),, #Off)) aoqi@0: // S = StoreQ(, AddP(, A.Parm , #Off), V) aoqi@0: // (Actually, we haven't yet proven the Q's are the same.) aoqi@0: // In other words, we are loading from a casted version of aoqi@0: // the same pointer-and-offset that we stored to. aoqi@0: // Thus, we are able to replace L by V. aoqi@0: } aoqi@0: // Now prove that we have a LoadQ matched to a StoreQ, for some Q. aoqi@0: if (store_Opcode() != st->Opcode()) aoqi@0: return NULL; aoqi@0: return st->in(MemNode::ValueIn); aoqi@0: } aoqi@0: aoqi@0: // A load from a freshly-created object always returns zero. aoqi@0: // (This can happen after LoadNode::Ideal resets the load's memory input aoqi@0: // to find_captured_store, which returned InitializeNode::zero_memory.) aoqi@0: if (st->is_Proj() && st->in(0)->is_Allocate() && aoqi@0: (st->in(0) == ld_alloc) && aoqi@0: (ld_off >= st->in(0)->as_Allocate()->minimum_header_size())) { aoqi@0: // return a zero value for the load's basic type aoqi@0: // (This is one of the few places where a generic PhaseTransform aoqi@0: // can create new nodes. Think of it as lazily manifesting aoqi@0: // virtually pre-existing constants.) aoqi@0: return phase->zerocon(memory_type()); aoqi@0: } aoqi@0: aoqi@0: // A load from an initialization barrier can match a captured store. aoqi@0: if (st->is_Proj() && st->in(0)->is_Initialize()) { aoqi@0: InitializeNode* init = st->in(0)->as_Initialize(); aoqi@0: AllocateNode* alloc = init->allocation(); aoqi@0: if ((alloc != NULL) && (alloc == ld_alloc)) { aoqi@0: // examine a captured store value aoqi@0: st = init->find_captured_store(ld_off, memory_size(), phase); aoqi@0: if (st != NULL) aoqi@0: continue; // take one more trip around aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Load boxed value from result of valueOf() call is input parameter. aoqi@0: if (this->is_Load() && ld_adr->is_AddP() && aoqi@0: (tp != NULL) && tp->is_ptr_to_boxed_value()) { aoqi@0: intptr_t ignore = 0; aoqi@0: Node* base = AddPNode::Ideal_base_and_offset(ld_adr, phase, ignore); aoqi@0: if (base != NULL && base->is_Proj() && aoqi@0: base->as_Proj()->_con == TypeFunc::Parms && aoqi@0: base->in(0)->is_CallStaticJava() && aoqi@0: base->in(0)->as_CallStaticJava()->is_boxing_method()) { aoqi@0: return base->in(0)->in(TypeFunc::Parms); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: break; aoqi@0: } aoqi@0: aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: //----------------------is_instance_field_load_with_local_phi------------------ aoqi@0: bool LoadNode::is_instance_field_load_with_local_phi(Node* ctrl) { aoqi@0: if( in(Memory)->is_Phi() && in(Memory)->in(0) == ctrl && aoqi@0: in(Address)->is_AddP() ) { aoqi@0: const TypeOopPtr* t_oop = in(Address)->bottom_type()->isa_oopptr(); aoqi@0: // Only instances and boxed values. aoqi@0: if( t_oop != NULL && aoqi@0: (t_oop->is_ptr_to_boxed_value() || aoqi@0: t_oop->is_known_instance_field()) && aoqi@0: t_oop->offset() != Type::OffsetBot && aoqi@0: t_oop->offset() != Type::OffsetTop) { aoqi@0: return true; aoqi@0: } aoqi@0: } aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: //------------------------------Identity--------------------------------------- aoqi@0: // Loads are identity if previous store is to same address aoqi@0: Node *LoadNode::Identity( PhaseTransform *phase ) { aoqi@0: // If the previous store-maker is the right kind of Store, and the store is aoqi@0: // to the same address, then we are equal to the value stored. aoqi@0: Node* mem = in(Memory); aoqi@0: Node* value = can_see_stored_value(mem, phase); aoqi@0: if( value ) { aoqi@0: // byte, short & char stores truncate naturally. aoqi@0: // A load has to load the truncated value which requires aoqi@0: // some sort of masking operation and that requires an aoqi@0: // Ideal call instead of an Identity call. aoqi@0: if (memory_size() < BytesPerInt) { aoqi@0: // If the input to the store does not fit with the load's result type, aoqi@0: // it must be truncated via an Ideal call. aoqi@0: if (!phase->type(value)->higher_equal(phase->type(this))) aoqi@0: return this; aoqi@0: } aoqi@0: // (This works even when value is a Con, but LoadNode::Value aoqi@0: // usually runs first, producing the singleton type of the Con.) aoqi@0: return value; aoqi@0: } aoqi@0: aoqi@0: // Search for an existing data phi which was generated before for the same aoqi@0: // instance's field to avoid infinite generation of phis in a loop. aoqi@0: Node *region = mem->in(0); aoqi@0: if (is_instance_field_load_with_local_phi(region)) { aoqi@0: const TypeOopPtr *addr_t = in(Address)->bottom_type()->isa_oopptr(); aoqi@0: int this_index = phase->C->get_alias_index(addr_t); aoqi@0: int this_offset = addr_t->offset(); aoqi@0: int this_iid = addr_t->instance_id(); aoqi@0: if (!addr_t->is_known_instance() && aoqi@0: addr_t->is_ptr_to_boxed_value()) { aoqi@0: // Use _idx of address base (could be Phi node) for boxed values. aoqi@0: intptr_t ignore = 0; aoqi@0: Node* base = AddPNode::Ideal_base_and_offset(in(Address), phase, ignore); aoqi@0: this_iid = base->_idx; aoqi@0: } aoqi@0: const Type* this_type = bottom_type(); aoqi@0: for (DUIterator_Fast imax, i = region->fast_outs(imax); i < imax; i++) { aoqi@0: Node* phi = region->fast_out(i); aoqi@0: if (phi->is_Phi() && phi != mem && aoqi@0: phi->as_Phi()->is_same_inst_field(this_type, this_iid, this_index, this_offset)) { aoqi@0: return phi; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: return this; aoqi@0: } aoqi@0: aoqi@0: // We're loading from an object which has autobox behaviour. aoqi@0: // If this object is result of a valueOf call we'll have a phi aoqi@0: // merging a newly allocated object and a load from the cache. aoqi@0: // We want to replace this load with the original incoming aoqi@0: // argument to the valueOf call. aoqi@0: Node* LoadNode::eliminate_autobox(PhaseGVN* phase) { aoqi@0: assert(phase->C->eliminate_boxing(), "sanity"); aoqi@0: intptr_t ignore = 0; aoqi@0: Node* base = AddPNode::Ideal_base_and_offset(in(Address), phase, ignore); aoqi@0: if ((base == NULL) || base->is_Phi()) { aoqi@0: // Push the loads from the phi that comes from valueOf up aoqi@0: // through it to allow elimination of the loads and the recovery aoqi@0: // of the original value. It is done in split_through_phi(). aoqi@0: return NULL; aoqi@0: } else if (base->is_Load() || aoqi@0: base->is_DecodeN() && base->in(1)->is_Load()) { aoqi@0: // Eliminate the load of boxed value for integer types from the cache aoqi@0: // array by deriving the value from the index into the array. aoqi@0: // Capture the offset of the load and then reverse the computation. aoqi@0: aoqi@0: // Get LoadN node which loads a boxing object from 'cache' array. aoqi@0: if (base->is_DecodeN()) { aoqi@0: base = base->in(1); aoqi@0: } aoqi@0: if (!base->in(Address)->is_AddP()) { aoqi@0: return NULL; // Complex address aoqi@0: } aoqi@0: AddPNode* address = base->in(Address)->as_AddP(); aoqi@0: Node* cache_base = address->in(AddPNode::Base); aoqi@0: if ((cache_base != NULL) && cache_base->is_DecodeN()) { aoqi@0: // Get ConP node which is static 'cache' field. aoqi@0: cache_base = cache_base->in(1); aoqi@0: } aoqi@0: if ((cache_base != NULL) && cache_base->is_Con()) { aoqi@0: const TypeAryPtr* base_type = cache_base->bottom_type()->isa_aryptr(); aoqi@0: if ((base_type != NULL) && base_type->is_autobox_cache()) { aoqi@0: Node* elements[4]; aoqi@0: int shift = exact_log2(type2aelembytes(T_OBJECT)); aoqi@0: int count = address->unpack_offsets(elements, ARRAY_SIZE(elements)); aoqi@0: if ((count > 0) && elements[0]->is_Con() && aoqi@0: ((count == 1) || aoqi@0: (count == 2) && elements[1]->Opcode() == Op_LShiftX && aoqi@0: elements[1]->in(2) == phase->intcon(shift))) { aoqi@0: ciObjArray* array = base_type->const_oop()->as_obj_array(); aoqi@0: // Fetch the box object cache[0] at the base of the array and get its value aoqi@0: ciInstance* box = array->obj_at(0)->as_instance(); aoqi@0: ciInstanceKlass* ik = box->klass()->as_instance_klass(); aoqi@0: assert(ik->is_box_klass(), "sanity"); aoqi@0: assert(ik->nof_nonstatic_fields() == 1, "change following code"); aoqi@0: if (ik->nof_nonstatic_fields() == 1) { aoqi@0: // This should be true nonstatic_field_at requires calling aoqi@0: // nof_nonstatic_fields so check it anyway aoqi@0: ciConstant c = box->field_value(ik->nonstatic_field_at(0)); aoqi@0: BasicType bt = c.basic_type(); aoqi@0: // Only integer types have boxing cache. aoqi@0: assert(bt == T_BOOLEAN || bt == T_CHAR || aoqi@0: bt == T_BYTE || bt == T_SHORT || aoqi@0: bt == T_INT || bt == T_LONG, err_msg_res("wrong type = %s", type2name(bt))); aoqi@0: jlong cache_low = (bt == T_LONG) ? c.as_long() : c.as_int(); aoqi@0: if (cache_low != (int)cache_low) { aoqi@0: return NULL; // should not happen since cache is array indexed by value aoqi@0: } aoqi@0: jlong offset = arrayOopDesc::base_offset_in_bytes(T_OBJECT) - (cache_low << shift); aoqi@0: if (offset != (int)offset) { aoqi@0: return NULL; // should not happen since cache is array indexed by value aoqi@0: } aoqi@0: // Add up all the offsets making of the address of the load aoqi@0: Node* result = elements[0]; aoqi@0: for (int i = 1; i < count; i++) { aoqi@0: result = phase->transform(new (phase->C) AddXNode(result, elements[i])); aoqi@0: } aoqi@0: // Remove the constant offset from the address and then aoqi@0: result = phase->transform(new (phase->C) AddXNode(result, phase->MakeConX(-(int)offset))); aoqi@0: // remove the scaling of the offset to recover the original index. aoqi@0: if (result->Opcode() == Op_LShiftX && result->in(2) == phase->intcon(shift)) { aoqi@0: // Peel the shift off directly but wrap it in a dummy node aoqi@0: // since Ideal can't return existing nodes aoqi@0: result = new (phase->C) RShiftXNode(result->in(1), phase->intcon(0)); aoqi@0: } else if (result->is_Add() && result->in(2)->is_Con() && aoqi@0: result->in(1)->Opcode() == Op_LShiftX && aoqi@0: result->in(1)->in(2) == phase->intcon(shift)) { aoqi@0: // We can't do general optimization: ((X<> Z ==> X + (Y>>Z) aoqi@0: // but for boxing cache access we know that X<C) RShiftXNode(result->in(2), phase->intcon(shift)); aoqi@0: result = new (phase->C) AddXNode(result->in(1)->in(1), phase->transform(add_con)); aoqi@0: } else { aoqi@0: result = new (phase->C) RShiftXNode(result, phase->intcon(shift)); aoqi@0: } aoqi@0: #ifdef _LP64 aoqi@0: if (bt != T_LONG) { aoqi@0: result = new (phase->C) ConvL2INode(phase->transform(result)); aoqi@0: } aoqi@0: #else aoqi@0: if (bt == T_LONG) { aoqi@0: result = new (phase->C) ConvI2LNode(phase->transform(result)); aoqi@0: } aoqi@0: #endif aoqi@0: return result; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: static bool stable_phi(PhiNode* phi, PhaseGVN *phase) { aoqi@0: Node* region = phi->in(0); aoqi@0: if (region == NULL) { aoqi@0: return false; // Wait stable graph aoqi@0: } aoqi@0: uint cnt = phi->req(); aoqi@0: for (uint i = 1; i < cnt; i++) { aoqi@0: Node* rc = region->in(i); aoqi@0: if (rc == NULL || phase->type(rc) == Type::TOP) aoqi@0: return false; // Wait stable graph aoqi@0: Node* in = phi->in(i); aoqi@0: if (in == NULL || phase->type(in) == Type::TOP) aoqi@0: return false; // Wait stable graph aoqi@0: } aoqi@0: return true; aoqi@0: } aoqi@0: //------------------------------split_through_phi------------------------------ aoqi@0: // Split instance or boxed field load through Phi. aoqi@0: Node *LoadNode::split_through_phi(PhaseGVN *phase) { aoqi@0: Node* mem = in(Memory); aoqi@0: Node* address = in(Address); aoqi@0: const TypeOopPtr *t_oop = phase->type(address)->isa_oopptr(); aoqi@0: aoqi@0: assert((t_oop != NULL) && aoqi@0: (t_oop->is_known_instance_field() || aoqi@0: t_oop->is_ptr_to_boxed_value()), "invalide conditions"); aoqi@0: aoqi@0: Compile* C = phase->C; aoqi@0: intptr_t ignore = 0; aoqi@0: Node* base = AddPNode::Ideal_base_and_offset(address, phase, ignore); aoqi@0: bool base_is_phi = (base != NULL) && base->is_Phi(); aoqi@0: bool load_boxed_values = t_oop->is_ptr_to_boxed_value() && C->aggressive_unboxing() && aoqi@0: (base != NULL) && (base == address->in(AddPNode::Base)) && aoqi@0: phase->type(base)->higher_equal(TypePtr::NOTNULL); aoqi@0: aoqi@0: if (!((mem->is_Phi() || base_is_phi) && aoqi@0: (load_boxed_values || t_oop->is_known_instance_field()))) { aoqi@0: return NULL; // memory is not Phi aoqi@0: } aoqi@0: aoqi@0: if (mem->is_Phi()) { aoqi@0: if (!stable_phi(mem->as_Phi(), phase)) { aoqi@0: return NULL; // Wait stable graph aoqi@0: } aoqi@0: uint cnt = mem->req(); aoqi@0: // Check for loop invariant memory. aoqi@0: if (cnt == 3) { aoqi@0: for (uint i = 1; i < cnt; i++) { aoqi@0: Node* in = mem->in(i); aoqi@0: Node* m = optimize_memory_chain(in, t_oop, this, phase); aoqi@0: if (m == mem) { aoqi@0: set_req(Memory, mem->in(cnt - i)); aoqi@0: return this; // made change aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: if (base_is_phi) { aoqi@0: if (!stable_phi(base->as_Phi(), phase)) { aoqi@0: return NULL; // Wait stable graph aoqi@0: } aoqi@0: uint cnt = base->req(); aoqi@0: // Check for loop invariant memory. aoqi@0: if (cnt == 3) { aoqi@0: for (uint i = 1; i < cnt; i++) { aoqi@0: if (base->in(i) == base) { aoqi@0: return NULL; // Wait stable graph aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: bool load_boxed_phi = load_boxed_values && base_is_phi && (base->in(0) == mem->in(0)); aoqi@0: aoqi@0: // Split through Phi (see original code in loopopts.cpp). aoqi@0: assert(C->have_alias_type(t_oop), "instance should have alias type"); aoqi@0: aoqi@0: // Do nothing here if Identity will find a value aoqi@0: // (to avoid infinite chain of value phis generation). aoqi@0: if (!phase->eqv(this, this->Identity(phase))) aoqi@0: return NULL; aoqi@0: aoqi@0: // Select Region to split through. aoqi@0: Node* region; aoqi@0: if (!base_is_phi) { aoqi@0: assert(mem->is_Phi(), "sanity"); aoqi@0: region = mem->in(0); aoqi@0: // Skip if the region dominates some control edge of the address. aoqi@0: if (!MemNode::all_controls_dominate(address, region)) aoqi@0: return NULL; aoqi@0: } else if (!mem->is_Phi()) { aoqi@0: assert(base_is_phi, "sanity"); aoqi@0: region = base->in(0); aoqi@0: // Skip if the region dominates some control edge of the memory. aoqi@0: if (!MemNode::all_controls_dominate(mem, region)) aoqi@0: return NULL; aoqi@0: } else if (base->in(0) != mem->in(0)) { aoqi@0: assert(base_is_phi && mem->is_Phi(), "sanity"); aoqi@0: if (MemNode::all_controls_dominate(mem, base->in(0))) { aoqi@0: region = base->in(0); aoqi@0: } else if (MemNode::all_controls_dominate(address, mem->in(0))) { aoqi@0: region = mem->in(0); aoqi@0: } else { aoqi@0: return NULL; // complex graph aoqi@0: } aoqi@0: } else { aoqi@0: assert(base->in(0) == mem->in(0), "sanity"); aoqi@0: region = mem->in(0); aoqi@0: } aoqi@0: aoqi@0: const Type* this_type = this->bottom_type(); aoqi@0: int this_index = C->get_alias_index(t_oop); aoqi@0: int this_offset = t_oop->offset(); aoqi@0: int this_iid = t_oop->instance_id(); aoqi@0: if (!t_oop->is_known_instance() && load_boxed_values) { aoqi@0: // Use _idx of address base for boxed values. aoqi@0: this_iid = base->_idx; aoqi@0: } aoqi@0: PhaseIterGVN* igvn = phase->is_IterGVN(); aoqi@0: Node* phi = new (C) PhiNode(region, this_type, NULL, this_iid, this_index, this_offset); aoqi@0: for (uint i = 1; i < region->req(); i++) { aoqi@0: Node* x; aoqi@0: Node* the_clone = NULL; aoqi@0: if (region->in(i) == C->top()) { aoqi@0: x = C->top(); // Dead path? Use a dead data op aoqi@0: } else { aoqi@0: x = this->clone(); // Else clone up the data op aoqi@0: the_clone = x; // Remember for possible deletion. aoqi@0: // Alter data node to use pre-phi inputs aoqi@0: if (this->in(0) == region) { aoqi@0: x->set_req(0, region->in(i)); aoqi@0: } else { aoqi@0: x->set_req(0, NULL); aoqi@0: } aoqi@0: if (mem->is_Phi() && (mem->in(0) == region)) { aoqi@0: x->set_req(Memory, mem->in(i)); // Use pre-Phi input for the clone. aoqi@0: } aoqi@0: if (address->is_Phi() && address->in(0) == region) { aoqi@0: x->set_req(Address, address->in(i)); // Use pre-Phi input for the clone aoqi@0: } aoqi@0: if (base_is_phi && (base->in(0) == region)) { aoqi@0: Node* base_x = base->in(i); // Clone address for loads from boxed objects. aoqi@0: Node* adr_x = phase->transform(new (C) AddPNode(base_x,base_x,address->in(AddPNode::Offset))); aoqi@0: x->set_req(Address, adr_x); aoqi@0: } aoqi@0: } aoqi@0: // Check for a 'win' on some paths aoqi@0: const Type *t = x->Value(igvn); aoqi@0: aoqi@0: bool singleton = t->singleton(); aoqi@0: aoqi@0: // See comments in PhaseIdealLoop::split_thru_phi(). aoqi@0: if (singleton && t == Type::TOP) { aoqi@0: singleton &= region->is_Loop() && (i != LoopNode::EntryControl); aoqi@0: } aoqi@0: aoqi@0: if (singleton) { aoqi@0: x = igvn->makecon(t); aoqi@0: } else { aoqi@0: // We now call Identity to try to simplify the cloned node. aoqi@0: // Note that some Identity methods call phase->type(this). aoqi@0: // Make sure that the type array is big enough for aoqi@0: // our new node, even though we may throw the node away. aoqi@0: // (This tweaking with igvn only works because x is a new node.) aoqi@0: igvn->set_type(x, t); aoqi@0: // If x is a TypeNode, capture any more-precise type permanently into Node aoqi@0: // otherwise it will be not updated during igvn->transform since aoqi@0: // igvn->type(x) is set to x->Value() already. aoqi@0: x->raise_bottom_type(t); aoqi@0: Node *y = x->Identity(igvn); aoqi@0: if (y != x) { aoqi@0: x = y; aoqi@0: } else { aoqi@0: y = igvn->hash_find_insert(x); aoqi@0: if (y) { aoqi@0: x = y; aoqi@0: } else { aoqi@0: // Else x is a new node we are keeping aoqi@0: // We do not need register_new_node_with_optimizer aoqi@0: // because set_type has already been called. aoqi@0: igvn->_worklist.push(x); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: if (x != the_clone && the_clone != NULL) { aoqi@0: igvn->remove_dead_node(the_clone); aoqi@0: } aoqi@0: phi->set_req(i, x); aoqi@0: } aoqi@0: // Record Phi aoqi@0: igvn->register_new_node_with_optimizer(phi); aoqi@0: return phi; aoqi@0: } aoqi@0: aoqi@0: //------------------------------Ideal------------------------------------------ aoqi@0: // If the load is from Field memory and the pointer is non-null, we can aoqi@0: // zero out the control input. aoqi@0: // If the offset is constant and the base is an object allocation, aoqi@0: // try to hook me up to the exact initializing store. aoqi@0: Node *LoadNode::Ideal(PhaseGVN *phase, bool can_reshape) { aoqi@0: Node* p = MemNode::Ideal_common(phase, can_reshape); aoqi@0: if (p) return (p == NodeSentinel) ? NULL : p; aoqi@0: aoqi@0: Node* ctrl = in(MemNode::Control); aoqi@0: Node* address = in(MemNode::Address); aoqi@0: aoqi@0: // Skip up past a SafePoint control. Cannot do this for Stores because aoqi@0: // pointer stores & cardmarks must stay on the same side of a SafePoint. aoqi@0: if( ctrl != NULL && ctrl->Opcode() == Op_SafePoint && aoqi@0: phase->C->get_alias_index(phase->type(address)->is_ptr()) != Compile::AliasIdxRaw ) { aoqi@0: ctrl = ctrl->in(0); aoqi@0: set_req(MemNode::Control,ctrl); aoqi@0: } aoqi@0: aoqi@0: intptr_t ignore = 0; aoqi@0: Node* base = AddPNode::Ideal_base_and_offset(address, phase, ignore); aoqi@0: if (base != NULL aoqi@0: && phase->C->get_alias_index(phase->type(address)->is_ptr()) != Compile::AliasIdxRaw) { aoqi@0: // Check for useless control edge in some common special cases aoqi@0: if (in(MemNode::Control) != NULL aoqi@0: && phase->type(base)->higher_equal(TypePtr::NOTNULL) aoqi@0: && all_controls_dominate(base, phase->C->start())) { aoqi@0: // A method-invariant, non-null address (constant or 'this' argument). aoqi@0: set_req(MemNode::Control, NULL); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: Node* mem = in(MemNode::Memory); aoqi@0: const TypePtr *addr_t = phase->type(address)->isa_ptr(); aoqi@0: aoqi@0: if (can_reshape && (addr_t != NULL)) { aoqi@0: // try to optimize our memory input aoqi@0: Node* opt_mem = MemNode::optimize_memory_chain(mem, addr_t, this, phase); aoqi@0: if (opt_mem != mem) { aoqi@0: set_req(MemNode::Memory, opt_mem); aoqi@0: if (phase->type( opt_mem ) == Type::TOP) return NULL; aoqi@0: return this; aoqi@0: } aoqi@0: const TypeOopPtr *t_oop = addr_t->isa_oopptr(); aoqi@0: if ((t_oop != NULL) && aoqi@0: (t_oop->is_known_instance_field() || aoqi@0: t_oop->is_ptr_to_boxed_value())) { aoqi@0: PhaseIterGVN *igvn = phase->is_IterGVN(); aoqi@0: if (igvn != NULL && igvn->_worklist.member(opt_mem)) { aoqi@0: // Delay this transformation until memory Phi is processed. aoqi@0: phase->is_IterGVN()->_worklist.push(this); aoqi@0: return NULL; aoqi@0: } aoqi@0: // Split instance field load through Phi. aoqi@0: Node* result = split_through_phi(phase); aoqi@0: if (result != NULL) return result; aoqi@0: aoqi@0: if (t_oop->is_ptr_to_boxed_value()) { aoqi@0: Node* result = eliminate_autobox(phase); aoqi@0: if (result != NULL) return result; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Check for prior store with a different base or offset; make Load aoqi@0: // independent. Skip through any number of them. Bail out if the stores aoqi@0: // are in an endless dead cycle and report no progress. This is a key aoqi@0: // transform for Reflection. However, if after skipping through the Stores aoqi@0: // we can't then fold up against a prior store do NOT do the transform as aoqi@0: // this amounts to using the 'Oracle' model of aliasing. It leaves the same aoqi@0: // array memory alive twice: once for the hoisted Load and again after the aoqi@0: // bypassed Store. This situation only works if EVERYBODY who does aoqi@0: // anti-dependence work knows how to bypass. I.e. we need all aoqi@0: // anti-dependence checks to ask the same Oracle. Right now, that Oracle is aoqi@0: // the alias index stuff. So instead, peek through Stores and IFF we can aoqi@0: // fold up, do so. aoqi@0: Node* prev_mem = find_previous_store(phase); aoqi@0: // Steps (a), (b): Walk past independent stores to find an exact match. aoqi@0: if (prev_mem != NULL && prev_mem != in(MemNode::Memory)) { aoqi@0: // (c) See if we can fold up on the spot, but don't fold up here. aoqi@0: // Fold-up might require truncation (for LoadB/LoadS/LoadUS) or aoqi@0: // just return a prior value, which is done by Identity calls. aoqi@0: if (can_see_stored_value(prev_mem, phase)) { aoqi@0: // Make ready for step (d): aoqi@0: set_req(MemNode::Memory, prev_mem); aoqi@0: return this; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: return NULL; // No further progress aoqi@0: } aoqi@0: aoqi@0: // Helper to recognize certain Klass fields which are invariant across aoqi@0: // some group of array types (e.g., int[] or all T[] where T < Object). aoqi@0: const Type* aoqi@0: LoadNode::load_array_final_field(const TypeKlassPtr *tkls, aoqi@0: ciKlass* klass) const { aoqi@0: if (tkls->offset() == in_bytes(Klass::modifier_flags_offset())) { aoqi@0: // The field is Klass::_modifier_flags. Return its (constant) value. aoqi@0: // (Folds up the 2nd indirection in aClassConstant.getModifiers().) aoqi@0: assert(this->Opcode() == Op_LoadI, "must load an int from _modifier_flags"); aoqi@0: return TypeInt::make(klass->modifier_flags()); aoqi@0: } aoqi@0: if (tkls->offset() == in_bytes(Klass::access_flags_offset())) { aoqi@0: // The field is Klass::_access_flags. Return its (constant) value. aoqi@0: // (Folds up the 2nd indirection in Reflection.getClassAccessFlags(aClassConstant).) aoqi@0: assert(this->Opcode() == Op_LoadI, "must load an int from _access_flags"); aoqi@0: return TypeInt::make(klass->access_flags()); aoqi@0: } aoqi@0: if (tkls->offset() == in_bytes(Klass::layout_helper_offset())) { aoqi@0: // The field is Klass::_layout_helper. Return its constant value if known. aoqi@0: assert(this->Opcode() == Op_LoadI, "must load an int from _layout_helper"); aoqi@0: return TypeInt::make(klass->layout_helper()); aoqi@0: } aoqi@0: aoqi@0: // No match. aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: // Try to constant-fold a stable array element. aoqi@0: static const Type* fold_stable_ary_elem(const TypeAryPtr* ary, int off, BasicType loadbt) { aoqi@0: assert(ary->const_oop(), "array should be constant"); aoqi@0: assert(ary->is_stable(), "array should be stable"); aoqi@0: aoqi@0: // Decode the results of GraphKit::array_element_address. aoqi@0: ciArray* aobj = ary->const_oop()->as_array(); aoqi@0: ciConstant con = aobj->element_value_by_offset(off); aoqi@0: aoqi@0: if (con.basic_type() != T_ILLEGAL && !con.is_null_or_zero()) { aoqi@0: const Type* con_type = Type::make_from_constant(con); aoqi@0: if (con_type != NULL) { aoqi@0: if (con_type->isa_aryptr()) { aoqi@0: // Join with the array element type, in case it is also stable. aoqi@0: int dim = ary->stable_dimension(); aoqi@0: con_type = con_type->is_aryptr()->cast_to_stable(true, dim-1); aoqi@0: } aoqi@0: if (loadbt == T_NARROWOOP && con_type->isa_oopptr()) { aoqi@0: con_type = con_type->make_narrowoop(); aoqi@0: } aoqi@0: #ifndef PRODUCT aoqi@0: if (TraceIterativeGVN) { aoqi@0: tty->print("FoldStableValues: array element [off=%d]: con_type=", off); aoqi@0: con_type->dump(); tty->cr(); aoqi@0: } aoqi@0: #endif //PRODUCT aoqi@0: return con_type; aoqi@0: } aoqi@0: } aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: //------------------------------Value----------------------------------------- aoqi@0: const Type *LoadNode::Value( PhaseTransform *phase ) const { aoqi@0: // Either input is TOP ==> the result is TOP aoqi@0: Node* mem = in(MemNode::Memory); aoqi@0: const Type *t1 = phase->type(mem); aoqi@0: if (t1 == Type::TOP) return Type::TOP; aoqi@0: Node* adr = in(MemNode::Address); aoqi@0: const TypePtr* tp = phase->type(adr)->isa_ptr(); aoqi@0: if (tp == NULL || tp->empty()) return Type::TOP; aoqi@0: int off = tp->offset(); aoqi@0: assert(off != Type::OffsetTop, "case covered by TypePtr::empty"); aoqi@0: Compile* C = phase->C; aoqi@0: aoqi@0: // Try to guess loaded type from pointer type aoqi@0: if (tp->isa_aryptr()) { aoqi@0: const TypeAryPtr* ary = tp->is_aryptr(); aoqi@0: const Type* t = ary->elem(); aoqi@0: aoqi@0: // Determine whether the reference is beyond the header or not, by comparing aoqi@0: // the offset against the offset of the start of the array's data. aoqi@0: // Different array types begin at slightly different offsets (12 vs. 16). aoqi@0: // We choose T_BYTE as an example base type that is least restrictive aoqi@0: // as to alignment, which will therefore produce the smallest aoqi@0: // possible base offset. aoqi@0: const int min_base_off = arrayOopDesc::base_offset_in_bytes(T_BYTE); aoqi@0: const bool off_beyond_header = ((uint)off >= (uint)min_base_off); aoqi@0: aoqi@0: // Try to constant-fold a stable array element. aoqi@0: if (FoldStableValues && ary->is_stable() && ary->const_oop() != NULL) { aoqi@0: // Make sure the reference is not into the header and the offset is constant aoqi@0: if (off_beyond_header && adr->is_AddP() && off != Type::OffsetBot) { aoqi@0: const Type* con_type = fold_stable_ary_elem(ary, off, memory_type()); aoqi@0: if (con_type != NULL) { aoqi@0: return con_type; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Don't do this for integer types. There is only potential profit if aoqi@0: // the element type t is lower than _type; that is, for int types, if _type is aoqi@0: // more restrictive than t. This only happens here if one is short and the other aoqi@0: // char (both 16 bits), and in those cases we've made an intentional decision aoqi@0: // to use one kind of load over the other. See AndINode::Ideal and 4965907. aoqi@0: // Also, do not try to narrow the type for a LoadKlass, regardless of offset. aoqi@0: // aoqi@0: // Yes, it is possible to encounter an expression like (LoadKlass p1:(AddP x x 8)) aoqi@0: // where the _gvn.type of the AddP is wider than 8. This occurs when an earlier aoqi@0: // copy p0 of (AddP x x 8) has been proven equal to p1, and the p0 has been aoqi@0: // subsumed by p1. If p1 is on the worklist but has not yet been re-transformed, aoqi@0: // it is possible that p1 will have a type like Foo*[int+]:NotNull*+any. aoqi@0: // In fact, that could have been the original type of p1, and p1 could have aoqi@0: // had an original form like p1:(AddP x x (LShiftL quux 3)), where the aoqi@0: // expression (LShiftL quux 3) independently optimized to the constant 8. aoqi@0: if ((t->isa_int() == NULL) && (t->isa_long() == NULL) aoqi@0: && (_type->isa_vect() == NULL) aoqi@0: && Opcode() != Op_LoadKlass && Opcode() != Op_LoadNKlass) { aoqi@0: // t might actually be lower than _type, if _type is a unique aoqi@0: // concrete subclass of abstract class t. aoqi@0: if (off_beyond_header) { // is the offset beyond the header? aoqi@0: const Type* jt = t->join_speculative(_type); aoqi@0: // In any case, do not allow the join, per se, to empty out the type. aoqi@0: if (jt->empty() && !t->empty()) { aoqi@0: // This can happen if a interface-typed array narrows to a class type. aoqi@0: jt = _type; aoqi@0: } aoqi@0: #ifdef ASSERT aoqi@0: if (phase->C->eliminate_boxing() && adr->is_AddP()) { aoqi@0: // The pointers in the autobox arrays are always non-null aoqi@0: Node* base = adr->in(AddPNode::Base); aoqi@0: if ((base != NULL) && base->is_DecodeN()) { aoqi@0: // Get LoadN node which loads IntegerCache.cache field aoqi@0: base = base->in(1); aoqi@0: } aoqi@0: if ((base != NULL) && base->is_Con()) { aoqi@0: const TypeAryPtr* base_type = base->bottom_type()->isa_aryptr(); aoqi@0: if ((base_type != NULL) && base_type->is_autobox_cache()) { aoqi@0: // It could be narrow oop aoqi@0: assert(jt->make_ptr()->ptr() == TypePtr::NotNull,"sanity"); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: #endif aoqi@0: return jt; aoqi@0: } aoqi@0: } aoqi@0: } else if (tp->base() == Type::InstPtr) { aoqi@0: ciEnv* env = C->env(); aoqi@0: const TypeInstPtr* tinst = tp->is_instptr(); aoqi@0: ciKlass* klass = tinst->klass(); aoqi@0: assert( off != Type::OffsetBot || aoqi@0: // arrays can be cast to Objects aoqi@0: tp->is_oopptr()->klass()->is_java_lang_Object() || aoqi@0: // unsafe field access may not have a constant offset aoqi@0: C->has_unsafe_access(), aoqi@0: "Field accesses must be precise" ); aoqi@0: // For oop loads, we expect the _type to be precise aoqi@0: if (klass == env->String_klass() && aoqi@0: adr->is_AddP() && off != Type::OffsetBot) { aoqi@0: // For constant Strings treat the final fields as compile time constants. aoqi@0: Node* base = adr->in(AddPNode::Base); aoqi@0: const TypeOopPtr* t = phase->type(base)->isa_oopptr(); aoqi@0: if (t != NULL && t->singleton()) { aoqi@0: ciField* field = env->String_klass()->get_field_by_offset(off, false); aoqi@0: if (field != NULL && field->is_final()) { aoqi@0: ciObject* string = t->const_oop(); aoqi@0: ciConstant constant = string->as_instance()->field_value(field); aoqi@0: if (constant.basic_type() == T_INT) { aoqi@0: return TypeInt::make(constant.as_int()); aoqi@0: } else if (constant.basic_type() == T_ARRAY) { aoqi@0: if (adr->bottom_type()->is_ptr_to_narrowoop()) { aoqi@0: return TypeNarrowOop::make_from_constant(constant.as_object(), true); aoqi@0: } else { aoqi@0: return TypeOopPtr::make_from_constant(constant.as_object(), true); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: // Optimizations for constant objects aoqi@0: ciObject* const_oop = tinst->const_oop(); aoqi@0: if (const_oop != NULL) { aoqi@0: // For constant Boxed value treat the target field as a compile time constant. aoqi@0: if (tinst->is_ptr_to_boxed_value()) { aoqi@0: return tinst->get_const_boxed_value(); aoqi@0: } else aoqi@0: // For constant CallSites treat the target field as a compile time constant. aoqi@0: if (const_oop->is_call_site()) { aoqi@0: ciCallSite* call_site = const_oop->as_call_site(); aoqi@0: ciField* field = call_site->klass()->as_instance_klass()->get_field_by_offset(off, /*is_static=*/ false); aoqi@0: if (field != NULL && field->is_call_site_target()) { aoqi@0: ciMethodHandle* target = call_site->get_target(); aoqi@0: if (target != NULL) { // just in case aoqi@0: ciConstant constant(T_OBJECT, target); aoqi@0: const Type* t; aoqi@0: if (adr->bottom_type()->is_ptr_to_narrowoop()) { aoqi@0: t = TypeNarrowOop::make_from_constant(constant.as_object(), true); aoqi@0: } else { aoqi@0: t = TypeOopPtr::make_from_constant(constant.as_object(), true); aoqi@0: } aoqi@0: // Add a dependence for invalidation of the optimization. aoqi@0: if (!call_site->is_constant_call_site()) { aoqi@0: C->dependencies()->assert_call_site_target_value(call_site, target); aoqi@0: } aoqi@0: return t; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } else if (tp->base() == Type::KlassPtr) { aoqi@0: assert( off != Type::OffsetBot || aoqi@0: // arrays can be cast to Objects aoqi@0: tp->is_klassptr()->klass()->is_java_lang_Object() || aoqi@0: // also allow array-loading from the primary supertype aoqi@0: // array during subtype checks aoqi@0: Opcode() == Op_LoadKlass, aoqi@0: "Field accesses must be precise" ); aoqi@0: // For klass/static loads, we expect the _type to be precise aoqi@0: } aoqi@0: aoqi@0: const TypeKlassPtr *tkls = tp->isa_klassptr(); aoqi@0: if (tkls != NULL && !StressReflectiveCode) { aoqi@0: ciKlass* klass = tkls->klass(); aoqi@0: if (klass->is_loaded() && tkls->klass_is_exact()) { aoqi@0: // We are loading a field from a Klass metaobject whose identity aoqi@0: // is known at compile time (the type is "exact" or "precise"). aoqi@0: // Check for fields we know are maintained as constants by the VM. aoqi@0: if (tkls->offset() == in_bytes(Klass::super_check_offset_offset())) { aoqi@0: // The field is Klass::_super_check_offset. Return its (constant) value. aoqi@0: // (Folds up type checking code.) aoqi@0: assert(Opcode() == Op_LoadI, "must load an int from _super_check_offset"); aoqi@0: return TypeInt::make(klass->super_check_offset()); aoqi@0: } aoqi@0: // Compute index into primary_supers array aoqi@0: juint depth = (tkls->offset() - in_bytes(Klass::primary_supers_offset())) / sizeof(Klass*); aoqi@0: // Check for overflowing; use unsigned compare to handle the negative case. aoqi@0: if( depth < ciKlass::primary_super_limit() ) { aoqi@0: // The field is an element of Klass::_primary_supers. Return its (constant) value. aoqi@0: // (Folds up type checking code.) aoqi@0: assert(Opcode() == Op_LoadKlass, "must load a klass from _primary_supers"); aoqi@0: ciKlass *ss = klass->super_of_depth(depth); aoqi@0: return ss ? TypeKlassPtr::make(ss) : TypePtr::NULL_PTR; aoqi@0: } aoqi@0: const Type* aift = load_array_final_field(tkls, klass); aoqi@0: if (aift != NULL) return aift; aoqi@0: if (tkls->offset() == in_bytes(ArrayKlass::component_mirror_offset()) aoqi@0: && klass->is_array_klass()) { aoqi@0: // The field is ArrayKlass::_component_mirror. Return its (constant) value. aoqi@0: // (Folds up aClassConstant.getComponentType, common in Arrays.copyOf.) aoqi@0: assert(Opcode() == Op_LoadP, "must load an oop from _component_mirror"); aoqi@0: return TypeInstPtr::make(klass->as_array_klass()->component_mirror()); aoqi@0: } aoqi@0: if (tkls->offset() == in_bytes(Klass::java_mirror_offset())) { aoqi@0: // The field is Klass::_java_mirror. Return its (constant) value. aoqi@0: // (Folds up the 2nd indirection in anObjConstant.getClass().) aoqi@0: assert(Opcode() == Op_LoadP, "must load an oop from _java_mirror"); aoqi@0: return TypeInstPtr::make(klass->java_mirror()); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // We can still check if we are loading from the primary_supers array at a aoqi@0: // shallow enough depth. Even though the klass is not exact, entries less aoqi@0: // than or equal to its super depth are correct. aoqi@0: if (klass->is_loaded() ) { aoqi@0: ciType *inner = klass; aoqi@0: while( inner->is_obj_array_klass() ) aoqi@0: inner = inner->as_obj_array_klass()->base_element_type(); aoqi@0: if( inner->is_instance_klass() && aoqi@0: !inner->as_instance_klass()->flags().is_interface() ) { aoqi@0: // Compute index into primary_supers array aoqi@0: juint depth = (tkls->offset() - in_bytes(Klass::primary_supers_offset())) / sizeof(Klass*); aoqi@0: // Check for overflowing; use unsigned compare to handle the negative case. aoqi@0: if( depth < ciKlass::primary_super_limit() && aoqi@0: depth <= klass->super_depth() ) { // allow self-depth checks to handle self-check case aoqi@0: // The field is an element of Klass::_primary_supers. Return its (constant) value. aoqi@0: // (Folds up type checking code.) aoqi@0: assert(Opcode() == Op_LoadKlass, "must load a klass from _primary_supers"); aoqi@0: ciKlass *ss = klass->super_of_depth(depth); aoqi@0: return ss ? TypeKlassPtr::make(ss) : TypePtr::NULL_PTR; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // If the type is enough to determine that the thing is not an array, aoqi@0: // we can give the layout_helper a positive interval type. aoqi@0: // This will help short-circuit some reflective code. aoqi@0: if (tkls->offset() == in_bytes(Klass::layout_helper_offset()) aoqi@0: && !klass->is_array_klass() // not directly typed as an array aoqi@0: && !klass->is_interface() // specifically not Serializable & Cloneable aoqi@0: && !klass->is_java_lang_Object() // not the supertype of all T[] aoqi@0: ) { aoqi@0: // Note: When interfaces are reliable, we can narrow the interface aoqi@0: // test to (klass != Serializable && klass != Cloneable). aoqi@0: assert(Opcode() == Op_LoadI, "must load an int from _layout_helper"); aoqi@0: jint min_size = Klass::instance_layout_helper(oopDesc::header_size(), false); aoqi@0: // The key property of this type is that it folds up tests aoqi@0: // for array-ness, since it proves that the layout_helper is positive. aoqi@0: // Thus, a generic value like the basic object layout helper works fine. aoqi@0: return TypeInt::make(min_size, max_jint, Type::WidenMin); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // If we are loading from a freshly-allocated object, produce a zero, aoqi@0: // if the load is provably beyond the header of the object. aoqi@0: // (Also allow a variable load from a fresh array to produce zero.) aoqi@0: const TypeOopPtr *tinst = tp->isa_oopptr(); aoqi@0: bool is_instance = (tinst != NULL) && tinst->is_known_instance_field(); aoqi@0: bool is_boxed_value = (tinst != NULL) && tinst->is_ptr_to_boxed_value(); aoqi@0: if (ReduceFieldZeroing || is_instance || is_boxed_value) { aoqi@0: Node* value = can_see_stored_value(mem,phase); aoqi@0: if (value != NULL && value->is_Con()) { aoqi@0: assert(value->bottom_type()->higher_equal(_type),"sanity"); aoqi@0: return value->bottom_type(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: if (is_instance) { aoqi@0: // If we have an instance type and our memory input is the aoqi@0: // programs's initial memory state, there is no matching store, aoqi@0: // so just return a zero of the appropriate type aoqi@0: Node *mem = in(MemNode::Memory); aoqi@0: if (mem->is_Parm() && mem->in(0)->is_Start()) { aoqi@0: assert(mem->as_Parm()->_con == TypeFunc::Memory, "must be memory Parm"); aoqi@0: return Type::get_zero_type(_type->basic_type()); aoqi@0: } aoqi@0: } aoqi@0: return _type; aoqi@0: } aoqi@0: aoqi@0: //------------------------------match_edge------------------------------------- aoqi@0: // Do we Match on this edge index or not? Match only the address. aoqi@0: uint LoadNode::match_edge(uint idx) const { aoqi@0: return idx == MemNode::Address; aoqi@0: } aoqi@0: aoqi@0: //--------------------------LoadBNode::Ideal-------------------------------------- aoqi@0: // aoqi@0: // If the previous store is to the same address as this load, aoqi@0: // and the value stored was larger than a byte, replace this load aoqi@0: // with the value stored truncated to a byte. If no truncation is aoqi@0: // needed, the replacement is done in LoadNode::Identity(). aoqi@0: // aoqi@0: Node *LoadBNode::Ideal(PhaseGVN *phase, bool can_reshape) { aoqi@0: Node* mem = in(MemNode::Memory); aoqi@0: Node* value = can_see_stored_value(mem,phase); aoqi@0: if( value && !phase->type(value)->higher_equal( _type ) ) { aoqi@0: Node *result = phase->transform( new (phase->C) LShiftINode(value, phase->intcon(24)) ); aoqi@0: return new (phase->C) RShiftINode(result, phase->intcon(24)); aoqi@0: } aoqi@0: // Identity call will handle the case where truncation is not needed. aoqi@0: return LoadNode::Ideal(phase, can_reshape); aoqi@0: } aoqi@0: aoqi@0: const Type* LoadBNode::Value(PhaseTransform *phase) const { aoqi@0: Node* mem = in(MemNode::Memory); aoqi@0: Node* value = can_see_stored_value(mem,phase); aoqi@0: if (value != NULL && value->is_Con() && aoqi@0: !value->bottom_type()->higher_equal(_type)) { aoqi@0: // If the input to the store does not fit with the load's result type, aoqi@0: // it must be truncated. We can't delay until Ideal call since aoqi@0: // a singleton Value is needed for split_thru_phi optimization. aoqi@0: int con = value->get_int(); aoqi@0: return TypeInt::make((con << 24) >> 24); aoqi@0: } aoqi@0: return LoadNode::Value(phase); aoqi@0: } aoqi@0: aoqi@0: //--------------------------LoadUBNode::Ideal------------------------------------- aoqi@0: // aoqi@0: // If the previous store is to the same address as this load, aoqi@0: // and the value stored was larger than a byte, replace this load aoqi@0: // with the value stored truncated to a byte. If no truncation is aoqi@0: // needed, the replacement is done in LoadNode::Identity(). aoqi@0: // aoqi@0: Node* LoadUBNode::Ideal(PhaseGVN* phase, bool can_reshape) { aoqi@0: Node* mem = in(MemNode::Memory); aoqi@0: Node* value = can_see_stored_value(mem, phase); aoqi@0: if (value && !phase->type(value)->higher_equal(_type)) aoqi@0: return new (phase->C) AndINode(value, phase->intcon(0xFF)); aoqi@0: // Identity call will handle the case where truncation is not needed. aoqi@0: return LoadNode::Ideal(phase, can_reshape); aoqi@0: } aoqi@0: aoqi@0: const Type* LoadUBNode::Value(PhaseTransform *phase) const { aoqi@0: Node* mem = in(MemNode::Memory); aoqi@0: Node* value = can_see_stored_value(mem,phase); aoqi@0: if (value != NULL && value->is_Con() && aoqi@0: !value->bottom_type()->higher_equal(_type)) { aoqi@0: // If the input to the store does not fit with the load's result type, aoqi@0: // it must be truncated. We can't delay until Ideal call since aoqi@0: // a singleton Value is needed for split_thru_phi optimization. aoqi@0: int con = value->get_int(); aoqi@0: return TypeInt::make(con & 0xFF); aoqi@0: } aoqi@0: return LoadNode::Value(phase); aoqi@0: } aoqi@0: aoqi@0: //--------------------------LoadUSNode::Ideal------------------------------------- aoqi@0: // aoqi@0: // If the previous store is to the same address as this load, aoqi@0: // and the value stored was larger than a char, replace this load aoqi@0: // with the value stored truncated to a char. If no truncation is aoqi@0: // needed, the replacement is done in LoadNode::Identity(). aoqi@0: // aoqi@0: Node *LoadUSNode::Ideal(PhaseGVN *phase, bool can_reshape) { aoqi@0: Node* mem = in(MemNode::Memory); aoqi@0: Node* value = can_see_stored_value(mem,phase); aoqi@0: if( value && !phase->type(value)->higher_equal( _type ) ) aoqi@0: return new (phase->C) AndINode(value,phase->intcon(0xFFFF)); aoqi@0: // Identity call will handle the case where truncation is not needed. aoqi@0: return LoadNode::Ideal(phase, can_reshape); aoqi@0: } aoqi@0: aoqi@0: const Type* LoadUSNode::Value(PhaseTransform *phase) const { aoqi@0: Node* mem = in(MemNode::Memory); aoqi@0: Node* value = can_see_stored_value(mem,phase); aoqi@0: if (value != NULL && value->is_Con() && aoqi@0: !value->bottom_type()->higher_equal(_type)) { aoqi@0: // If the input to the store does not fit with the load's result type, aoqi@0: // it must be truncated. We can't delay until Ideal call since aoqi@0: // a singleton Value is needed for split_thru_phi optimization. aoqi@0: int con = value->get_int(); aoqi@0: return TypeInt::make(con & 0xFFFF); aoqi@0: } aoqi@0: return LoadNode::Value(phase); aoqi@0: } aoqi@0: aoqi@0: //--------------------------LoadSNode::Ideal-------------------------------------- aoqi@0: // aoqi@0: // If the previous store is to the same address as this load, aoqi@0: // and the value stored was larger than a short, replace this load aoqi@0: // with the value stored truncated to a short. If no truncation is aoqi@0: // needed, the replacement is done in LoadNode::Identity(). aoqi@0: // aoqi@0: Node *LoadSNode::Ideal(PhaseGVN *phase, bool can_reshape) { aoqi@0: Node* mem = in(MemNode::Memory); aoqi@0: Node* value = can_see_stored_value(mem,phase); aoqi@0: if( value && !phase->type(value)->higher_equal( _type ) ) { aoqi@0: Node *result = phase->transform( new (phase->C) LShiftINode(value, phase->intcon(16)) ); aoqi@0: return new (phase->C) RShiftINode(result, phase->intcon(16)); aoqi@0: } aoqi@0: // Identity call will handle the case where truncation is not needed. aoqi@0: return LoadNode::Ideal(phase, can_reshape); aoqi@0: } aoqi@0: aoqi@0: const Type* LoadSNode::Value(PhaseTransform *phase) const { aoqi@0: Node* mem = in(MemNode::Memory); aoqi@0: Node* value = can_see_stored_value(mem,phase); aoqi@0: if (value != NULL && value->is_Con() && aoqi@0: !value->bottom_type()->higher_equal(_type)) { aoqi@0: // If the input to the store does not fit with the load's result type, aoqi@0: // it must be truncated. We can't delay until Ideal call since aoqi@0: // a singleton Value is needed for split_thru_phi optimization. aoqi@0: int con = value->get_int(); aoqi@0: return TypeInt::make((con << 16) >> 16); aoqi@0: } aoqi@0: return LoadNode::Value(phase); aoqi@0: } aoqi@0: aoqi@0: //============================================================================= aoqi@0: //----------------------------LoadKlassNode::make------------------------------ aoqi@0: // Polymorphic factory method: aoqi@0: Node *LoadKlassNode::make( PhaseGVN& gvn, Node *mem, Node *adr, const TypePtr* at, const TypeKlassPtr *tk ) { aoqi@0: Compile* C = gvn.C; aoqi@0: Node *ctl = NULL; aoqi@0: // sanity check the alias category against the created node type aoqi@0: const TypePtr *adr_type = adr->bottom_type()->isa_ptr(); aoqi@0: assert(adr_type != NULL, "expecting TypeKlassPtr"); aoqi@0: #ifdef _LP64 aoqi@0: if (adr_type->is_ptr_to_narrowklass()) { aoqi@0: assert(UseCompressedClassPointers, "no compressed klasses"); aoqi@0: Node* load_klass = gvn.transform(new (C) LoadNKlassNode(ctl, mem, adr, at, tk->make_narrowklass(), MemNode::unordered)); aoqi@0: return new (C) DecodeNKlassNode(load_klass, load_klass->bottom_type()->make_ptr()); aoqi@0: } aoqi@0: #endif aoqi@0: assert(!adr_type->is_ptr_to_narrowklass() && !adr_type->is_ptr_to_narrowoop(), "should have got back a narrow oop"); aoqi@0: return new (C) LoadKlassNode(ctl, mem, adr, at, tk, MemNode::unordered); aoqi@0: } aoqi@0: aoqi@0: //------------------------------Value------------------------------------------ aoqi@0: const Type *LoadKlassNode::Value( PhaseTransform *phase ) const { aoqi@0: return klass_value_common(phase); aoqi@0: } aoqi@0: aoqi@0: const Type *LoadNode::klass_value_common( PhaseTransform *phase ) const { aoqi@0: // Either input is TOP ==> the result is TOP aoqi@0: const Type *t1 = phase->type( in(MemNode::Memory) ); aoqi@0: if (t1 == Type::TOP) return Type::TOP; aoqi@0: Node *adr = in(MemNode::Address); aoqi@0: const Type *t2 = phase->type( adr ); aoqi@0: if (t2 == Type::TOP) return Type::TOP; aoqi@0: const TypePtr *tp = t2->is_ptr(); aoqi@0: if (TypePtr::above_centerline(tp->ptr()) || aoqi@0: tp->ptr() == TypePtr::Null) return Type::TOP; aoqi@0: aoqi@0: // Return a more precise klass, if possible aoqi@0: const TypeInstPtr *tinst = tp->isa_instptr(); aoqi@0: if (tinst != NULL) { aoqi@0: ciInstanceKlass* ik = tinst->klass()->as_instance_klass(); aoqi@0: int offset = tinst->offset(); aoqi@0: if (ik == phase->C->env()->Class_klass() aoqi@0: && (offset == java_lang_Class::klass_offset_in_bytes() || aoqi@0: offset == java_lang_Class::array_klass_offset_in_bytes())) { aoqi@0: // We are loading a special hidden field from a Class mirror object, aoqi@0: // the field which points to the VM's Klass metaobject. aoqi@0: ciType* t = tinst->java_mirror_type(); aoqi@0: // java_mirror_type returns non-null for compile-time Class constants. aoqi@0: if (t != NULL) { aoqi@0: // constant oop => constant klass aoqi@0: if (offset == java_lang_Class::array_klass_offset_in_bytes()) { aoqi@0: if (t->is_void()) { aoqi@0: // We cannot create a void array. Since void is a primitive type return null aoqi@0: // klass. Users of this result need to do a null check on the returned klass. aoqi@0: return TypePtr::NULL_PTR; aoqi@0: } aoqi@0: return TypeKlassPtr::make(ciArrayKlass::make(t)); aoqi@0: } aoqi@0: if (!t->is_klass()) { aoqi@0: // a primitive Class (e.g., int.class) has NULL for a klass field aoqi@0: return TypePtr::NULL_PTR; aoqi@0: } aoqi@0: // (Folds up the 1st indirection in aClassConstant.getModifiers().) aoqi@0: return TypeKlassPtr::make(t->as_klass()); aoqi@0: } aoqi@0: // non-constant mirror, so we can't tell what's going on aoqi@0: } aoqi@0: if( !ik->is_loaded() ) aoqi@0: return _type; // Bail out if not loaded aoqi@0: if (offset == oopDesc::klass_offset_in_bytes()) { aoqi@0: if (tinst->klass_is_exact()) { aoqi@0: return TypeKlassPtr::make(ik); aoqi@0: } aoqi@0: // See if we can become precise: no subklasses and no interface aoqi@0: // (Note: We need to support verified interfaces.) aoqi@0: if (!ik->is_interface() && !ik->has_subklass()) { aoqi@0: //assert(!UseExactTypes, "this code should be useless with exact types"); aoqi@0: // Add a dependence; if any subclass added we need to recompile aoqi@0: if (!ik->is_final()) { aoqi@0: // %%% should use stronger assert_unique_concrete_subtype instead aoqi@0: phase->C->dependencies()->assert_leaf_type(ik); aoqi@0: } aoqi@0: // Return precise klass aoqi@0: return TypeKlassPtr::make(ik); aoqi@0: } aoqi@0: aoqi@0: // Return root of possible klass aoqi@0: return TypeKlassPtr::make(TypePtr::NotNull, ik, 0/*offset*/); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Check for loading klass from an array aoqi@0: const TypeAryPtr *tary = tp->isa_aryptr(); aoqi@0: if( tary != NULL ) { aoqi@0: ciKlass *tary_klass = tary->klass(); aoqi@0: if (tary_klass != NULL // can be NULL when at BOTTOM or TOP aoqi@0: && tary->offset() == oopDesc::klass_offset_in_bytes()) { aoqi@0: if (tary->klass_is_exact()) { aoqi@0: return TypeKlassPtr::make(tary_klass); aoqi@0: } aoqi@0: ciArrayKlass *ak = tary->klass()->as_array_klass(); aoqi@0: // If the klass is an object array, we defer the question to the aoqi@0: // array component klass. aoqi@0: if( ak->is_obj_array_klass() ) { aoqi@0: assert( ak->is_loaded(), "" ); aoqi@0: ciKlass *base_k = ak->as_obj_array_klass()->base_element_klass(); aoqi@0: if( base_k->is_loaded() && base_k->is_instance_klass() ) { aoqi@0: ciInstanceKlass* ik = base_k->as_instance_klass(); aoqi@0: // See if we can become precise: no subklasses and no interface aoqi@0: if (!ik->is_interface() && !ik->has_subklass()) { aoqi@0: //assert(!UseExactTypes, "this code should be useless with exact types"); aoqi@0: // Add a dependence; if any subclass added we need to recompile aoqi@0: if (!ik->is_final()) { aoqi@0: phase->C->dependencies()->assert_leaf_type(ik); aoqi@0: } aoqi@0: // Return precise array klass aoqi@0: return TypeKlassPtr::make(ak); aoqi@0: } aoqi@0: } aoqi@0: return TypeKlassPtr::make(TypePtr::NotNull, ak, 0/*offset*/); aoqi@0: } else { // Found a type-array? aoqi@0: //assert(!UseExactTypes, "this code should be useless with exact types"); aoqi@0: assert( ak->is_type_array_klass(), "" ); aoqi@0: return TypeKlassPtr::make(ak); // These are always precise aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Check for loading klass from an array klass aoqi@0: const TypeKlassPtr *tkls = tp->isa_klassptr(); aoqi@0: if (tkls != NULL && !StressReflectiveCode) { aoqi@0: ciKlass* klass = tkls->klass(); aoqi@0: if( !klass->is_loaded() ) aoqi@0: return _type; // Bail out if not loaded aoqi@0: if( klass->is_obj_array_klass() && aoqi@0: tkls->offset() == in_bytes(ObjArrayKlass::element_klass_offset())) { aoqi@0: ciKlass* elem = klass->as_obj_array_klass()->element_klass(); aoqi@0: // // Always returning precise element type is incorrect, aoqi@0: // // e.g., element type could be object and array may contain strings aoqi@0: // return TypeKlassPtr::make(TypePtr::Constant, elem, 0); aoqi@0: aoqi@0: // The array's TypeKlassPtr was declared 'precise' or 'not precise' aoqi@0: // according to the element type's subclassing. aoqi@0: return TypeKlassPtr::make(tkls->ptr(), elem, 0/*offset*/); aoqi@0: } aoqi@0: if( klass->is_instance_klass() && tkls->klass_is_exact() && aoqi@0: tkls->offset() == in_bytes(Klass::super_offset())) { aoqi@0: ciKlass* sup = klass->as_instance_klass()->super(); aoqi@0: // The field is Klass::_super. Return its (constant) value. aoqi@0: // (Folds up the 2nd indirection in aClassConstant.getSuperClass().) aoqi@0: return sup ? TypeKlassPtr::make(sup) : TypePtr::NULL_PTR; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Bailout case aoqi@0: return LoadNode::Value(phase); aoqi@0: } aoqi@0: aoqi@0: //------------------------------Identity--------------------------------------- aoqi@0: // To clean up reflective code, simplify k.java_mirror.as_klass to plain k. aoqi@0: // Also feed through the klass in Allocate(...klass...)._klass. aoqi@0: Node* LoadKlassNode::Identity( PhaseTransform *phase ) { aoqi@0: return klass_identity_common(phase); aoqi@0: } aoqi@0: aoqi@0: Node* LoadNode::klass_identity_common(PhaseTransform *phase ) { aoqi@0: Node* x = LoadNode::Identity(phase); aoqi@0: if (x != this) return x; aoqi@0: aoqi@0: // Take apart the address into an oop and and offset. aoqi@0: // Return 'this' if we cannot. aoqi@0: Node* adr = in(MemNode::Address); aoqi@0: intptr_t offset = 0; aoqi@0: Node* base = AddPNode::Ideal_base_and_offset(adr, phase, offset); aoqi@0: if (base == NULL) return this; aoqi@0: const TypeOopPtr* toop = phase->type(adr)->isa_oopptr(); aoqi@0: if (toop == NULL) return this; aoqi@0: aoqi@0: // We can fetch the klass directly through an AllocateNode. aoqi@0: // This works even if the klass is not constant (clone or newArray). aoqi@0: if (offset == oopDesc::klass_offset_in_bytes()) { aoqi@0: Node* allocated_klass = AllocateNode::Ideal_klass(base, phase); aoqi@0: if (allocated_klass != NULL) { aoqi@0: return allocated_klass; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Simplify k.java_mirror.as_klass to plain k, where k is a Klass*. aoqi@0: // Simplify ak.component_mirror.array_klass to plain ak, ak an ArrayKlass. aoqi@0: // See inline_native_Class_query for occurrences of these patterns. aoqi@0: // Java Example: x.getClass().isAssignableFrom(y) aoqi@0: // Java Example: Array.newInstance(x.getClass().getComponentType(), n) aoqi@0: // aoqi@0: // This improves reflective code, often making the Class aoqi@0: // mirror go completely dead. (Current exception: Class aoqi@0: // mirrors may appear in debug info, but we could clean them out by aoqi@0: // introducing a new debug info operator for Klass*.java_mirror). aoqi@0: if (toop->isa_instptr() && toop->klass() == phase->C->env()->Class_klass() aoqi@0: && (offset == java_lang_Class::klass_offset_in_bytes() || aoqi@0: offset == java_lang_Class::array_klass_offset_in_bytes())) { aoqi@0: // We are loading a special hidden field from a Class mirror, aoqi@0: // the field which points to its Klass or ArrayKlass metaobject. aoqi@0: if (base->is_Load()) { aoqi@0: Node* adr2 = base->in(MemNode::Address); aoqi@0: const TypeKlassPtr* tkls = phase->type(adr2)->isa_klassptr(); aoqi@0: if (tkls != NULL && !tkls->empty() aoqi@0: && (tkls->klass()->is_instance_klass() || aoqi@0: tkls->klass()->is_array_klass()) aoqi@0: && adr2->is_AddP() aoqi@0: ) { aoqi@0: int mirror_field = in_bytes(Klass::java_mirror_offset()); aoqi@0: if (offset == java_lang_Class::array_klass_offset_in_bytes()) { aoqi@0: mirror_field = in_bytes(ArrayKlass::component_mirror_offset()); aoqi@0: } aoqi@0: if (tkls->offset() == mirror_field) { aoqi@0: return adr2->in(AddPNode::Base); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: return this; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: //------------------------------Value------------------------------------------ aoqi@0: const Type *LoadNKlassNode::Value( PhaseTransform *phase ) const { aoqi@0: const Type *t = klass_value_common(phase); aoqi@0: if (t == Type::TOP) aoqi@0: return t; aoqi@0: aoqi@0: return t->make_narrowklass(); aoqi@0: } aoqi@0: aoqi@0: //------------------------------Identity--------------------------------------- aoqi@0: // To clean up reflective code, simplify k.java_mirror.as_klass to narrow k. aoqi@0: // Also feed through the klass in Allocate(...klass...)._klass. aoqi@0: Node* LoadNKlassNode::Identity( PhaseTransform *phase ) { aoqi@0: Node *x = klass_identity_common(phase); aoqi@0: aoqi@0: const Type *t = phase->type( x ); aoqi@0: if( t == Type::TOP ) return x; aoqi@0: if( t->isa_narrowklass()) return x; aoqi@0: assert (!t->isa_narrowoop(), "no narrow oop here"); aoqi@0: aoqi@0: return phase->transform(new (phase->C) EncodePKlassNode(x, t->make_narrowklass())); aoqi@0: } aoqi@0: aoqi@0: //------------------------------Value----------------------------------------- aoqi@0: const Type *LoadRangeNode::Value( PhaseTransform *phase ) const { aoqi@0: // Either input is TOP ==> the result is TOP aoqi@0: const Type *t1 = phase->type( in(MemNode::Memory) ); aoqi@0: if( t1 == Type::TOP ) return Type::TOP; aoqi@0: Node *adr = in(MemNode::Address); aoqi@0: const Type *t2 = phase->type( adr ); aoqi@0: if( t2 == Type::TOP ) return Type::TOP; aoqi@0: const TypePtr *tp = t2->is_ptr(); aoqi@0: if (TypePtr::above_centerline(tp->ptr())) return Type::TOP; aoqi@0: const TypeAryPtr *tap = tp->isa_aryptr(); aoqi@0: if( !tap ) return _type; aoqi@0: return tap->size(); aoqi@0: } aoqi@0: aoqi@0: //-------------------------------Ideal--------------------------------------- aoqi@0: // Feed through the length in AllocateArray(...length...)._length. aoqi@0: Node *LoadRangeNode::Ideal(PhaseGVN *phase, bool can_reshape) { aoqi@0: Node* p = MemNode::Ideal_common(phase, can_reshape); aoqi@0: if (p) return (p == NodeSentinel) ? NULL : p; aoqi@0: aoqi@0: // Take apart the address into an oop and and offset. aoqi@0: // Return 'this' if we cannot. aoqi@0: Node* adr = in(MemNode::Address); aoqi@0: intptr_t offset = 0; aoqi@0: Node* base = AddPNode::Ideal_base_and_offset(adr, phase, offset); aoqi@0: if (base == NULL) return NULL; aoqi@0: const TypeAryPtr* tary = phase->type(adr)->isa_aryptr(); aoqi@0: if (tary == NULL) return NULL; aoqi@0: aoqi@0: // We can fetch the length directly through an AllocateArrayNode. aoqi@0: // This works even if the length is not constant (clone or newArray). aoqi@0: if (offset == arrayOopDesc::length_offset_in_bytes()) { aoqi@0: AllocateArrayNode* alloc = AllocateArrayNode::Ideal_array_allocation(base, phase); aoqi@0: if (alloc != NULL) { aoqi@0: Node* allocated_length = alloc->Ideal_length(); aoqi@0: Node* len = alloc->make_ideal_length(tary, phase); aoqi@0: if (allocated_length != len) { aoqi@0: // New CastII improves on this. aoqi@0: return len; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: //------------------------------Identity--------------------------------------- aoqi@0: // Feed through the length in AllocateArray(...length...)._length. aoqi@0: Node* LoadRangeNode::Identity( PhaseTransform *phase ) { aoqi@0: Node* x = LoadINode::Identity(phase); aoqi@0: if (x != this) return x; aoqi@0: aoqi@0: // Take apart the address into an oop and and offset. aoqi@0: // Return 'this' if we cannot. aoqi@0: Node* adr = in(MemNode::Address); aoqi@0: intptr_t offset = 0; aoqi@0: Node* base = AddPNode::Ideal_base_and_offset(adr, phase, offset); aoqi@0: if (base == NULL) return this; aoqi@0: const TypeAryPtr* tary = phase->type(adr)->isa_aryptr(); aoqi@0: if (tary == NULL) return this; aoqi@0: aoqi@0: // We can fetch the length directly through an AllocateArrayNode. aoqi@0: // This works even if the length is not constant (clone or newArray). aoqi@0: if (offset == arrayOopDesc::length_offset_in_bytes()) { aoqi@0: AllocateArrayNode* alloc = AllocateArrayNode::Ideal_array_allocation(base, phase); aoqi@0: if (alloc != NULL) { aoqi@0: Node* allocated_length = alloc->Ideal_length(); aoqi@0: // Do not allow make_ideal_length to allocate a CastII node. aoqi@0: Node* len = alloc->make_ideal_length(tary, phase, false); aoqi@0: if (allocated_length == len) { aoqi@0: // Return allocated_length only if it would not be improved by a CastII. aoqi@0: return allocated_length; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: return this; aoqi@0: aoqi@0: } aoqi@0: aoqi@0: //============================================================================= aoqi@0: //---------------------------StoreNode::make----------------------------------- aoqi@0: // Polymorphic factory method: aoqi@0: StoreNode* StoreNode::make(PhaseGVN& gvn, Node* ctl, Node* mem, Node* adr, const TypePtr* adr_type, Node* val, BasicType bt, MemOrd mo) { aoqi@0: assert((mo == unordered || mo == release), "unexpected"); aoqi@0: Compile* C = gvn.C; aoqi@0: assert(C->get_alias_index(adr_type) != Compile::AliasIdxRaw || aoqi@0: ctl != NULL, "raw memory operations should have control edge"); aoqi@0: aoqi@0: switch (bt) { aoqi@0: case T_BOOLEAN: aoqi@0: case T_BYTE: return new (C) StoreBNode(ctl, mem, adr, adr_type, val, mo); aoqi@0: case T_INT: return new (C) StoreINode(ctl, mem, adr, adr_type, val, mo); aoqi@0: case T_CHAR: aoqi@0: case T_SHORT: return new (C) StoreCNode(ctl, mem, adr, adr_type, val, mo); aoqi@0: case T_LONG: return new (C) StoreLNode(ctl, mem, adr, adr_type, val, mo); aoqi@0: case T_FLOAT: return new (C) StoreFNode(ctl, mem, adr, adr_type, val, mo); aoqi@0: case T_DOUBLE: return new (C) StoreDNode(ctl, mem, adr, adr_type, val, mo); aoqi@0: case T_METADATA: aoqi@0: case T_ADDRESS: aoqi@0: case T_OBJECT: aoqi@0: #ifdef _LP64 aoqi@0: if (adr->bottom_type()->is_ptr_to_narrowoop()) { aoqi@0: val = gvn.transform(new (C) EncodePNode(val, val->bottom_type()->make_narrowoop())); aoqi@0: return new (C) StoreNNode(ctl, mem, adr, adr_type, val, mo); aoqi@0: } else if (adr->bottom_type()->is_ptr_to_narrowklass() || aoqi@0: (UseCompressedClassPointers && val->bottom_type()->isa_klassptr() && aoqi@0: adr->bottom_type()->isa_rawptr())) { aoqi@0: val = gvn.transform(new (C) EncodePKlassNode(val, val->bottom_type()->make_narrowklass())); aoqi@0: return new (C) StoreNKlassNode(ctl, mem, adr, adr_type, val, mo); aoqi@0: } aoqi@0: #endif aoqi@0: { aoqi@0: return new (C) StorePNode(ctl, mem, adr, adr_type, val, mo); aoqi@0: } aoqi@0: } aoqi@0: ShouldNotReachHere(); aoqi@0: return (StoreNode*)NULL; aoqi@0: } aoqi@0: aoqi@0: StoreLNode* StoreLNode::make_atomic(Compile *C, Node* ctl, Node* mem, Node* adr, const TypePtr* adr_type, Node* val, MemOrd mo) { aoqi@0: bool require_atomic = true; aoqi@0: return new (C) StoreLNode(ctl, mem, adr, adr_type, val, mo, require_atomic); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: //--------------------------bottom_type---------------------------------------- aoqi@0: const Type *StoreNode::bottom_type() const { aoqi@0: return Type::MEMORY; aoqi@0: } aoqi@0: aoqi@0: //------------------------------hash------------------------------------------- aoqi@0: uint StoreNode::hash() const { aoqi@0: // unroll addition of interesting fields aoqi@0: //return (uintptr_t)in(Control) + (uintptr_t)in(Memory) + (uintptr_t)in(Address) + (uintptr_t)in(ValueIn); aoqi@0: aoqi@0: // Since they are not commoned, do not hash them: aoqi@0: return NO_HASH; aoqi@0: } aoqi@0: aoqi@0: //------------------------------Ideal------------------------------------------ aoqi@0: // Change back-to-back Store(, p, x) -> Store(m, p, y) to Store(m, p, x). aoqi@0: // When a store immediately follows a relevant allocation/initialization, aoqi@0: // try to capture it into the initialization, or hoist it above. aoqi@0: Node *StoreNode::Ideal(PhaseGVN *phase, bool can_reshape) { aoqi@0: Node* p = MemNode::Ideal_common(phase, can_reshape); aoqi@0: if (p) return (p == NodeSentinel) ? NULL : p; aoqi@0: aoqi@0: Node* mem = in(MemNode::Memory); aoqi@0: Node* address = in(MemNode::Address); aoqi@0: aoqi@0: // Back-to-back stores to same address? Fold em up. Generally aoqi@0: // unsafe if I have intervening uses... Also disallowed for StoreCM aoqi@0: // since they must follow each StoreP operation. Redundant StoreCMs aoqi@0: // are eliminated just before matching in final_graph_reshape. aoqi@0: if (mem->is_Store() && mem->in(MemNode::Address)->eqv_uncast(address) && aoqi@0: mem->Opcode() != Op_StoreCM) { aoqi@0: // Looking at a dead closed cycle of memory? aoqi@0: assert(mem != mem->in(MemNode::Memory), "dead loop in StoreNode::Ideal"); aoqi@0: aoqi@0: assert(Opcode() == mem->Opcode() || aoqi@0: phase->C->get_alias_index(adr_type()) == Compile::AliasIdxRaw, aoqi@0: "no mismatched stores, except on raw memory"); aoqi@0: aoqi@0: if (mem->outcnt() == 1 && // check for intervening uses aoqi@0: mem->as_Store()->memory_size() <= this->memory_size()) { aoqi@0: // If anybody other than 'this' uses 'mem', we cannot fold 'mem' away. aoqi@0: // For example, 'mem' might be the final state at a conditional return. aoqi@0: // Or, 'mem' might be used by some node which is live at the same time aoqi@0: // 'this' is live, which might be unschedulable. So, require exactly aoqi@0: // ONE user, the 'this' store, until such time as we clone 'mem' for aoqi@0: // each of 'mem's uses (thus making the exactly-1-user-rule hold true). aoqi@0: if (can_reshape) { // (%%% is this an anachronism?) aoqi@0: set_req_X(MemNode::Memory, mem->in(MemNode::Memory), aoqi@0: phase->is_IterGVN()); aoqi@0: } else { aoqi@0: // It's OK to do this in the parser, since DU info is always accurate, aoqi@0: // and the parser always refers to nodes via SafePointNode maps. aoqi@0: set_req(MemNode::Memory, mem->in(MemNode::Memory)); aoqi@0: } aoqi@0: return this; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Capture an unaliased, unconditional, simple store into an initializer. aoqi@0: // Or, if it is independent of the allocation, hoist it above the allocation. aoqi@0: if (ReduceFieldZeroing && /*can_reshape &&*/ aoqi@0: mem->is_Proj() && mem->in(0)->is_Initialize()) { aoqi@0: InitializeNode* init = mem->in(0)->as_Initialize(); aoqi@0: intptr_t offset = init->can_capture_store(this, phase, can_reshape); aoqi@0: if (offset > 0) { aoqi@0: Node* moved = init->capture_store(this, offset, phase, can_reshape); aoqi@0: // If the InitializeNode captured me, it made a raw copy of me, aoqi@0: // and I need to disappear. aoqi@0: if (moved != NULL) { aoqi@0: // %%% hack to ensure that Ideal returns a new node: aoqi@0: mem = MergeMemNode::make(phase->C, mem); aoqi@0: return mem; // fold me away aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: return NULL; // No further progress aoqi@0: } aoqi@0: aoqi@0: //------------------------------Value----------------------------------------- aoqi@0: const Type *StoreNode::Value( PhaseTransform *phase ) const { aoqi@0: // Either input is TOP ==> the result is TOP aoqi@0: const Type *t1 = phase->type( in(MemNode::Memory) ); aoqi@0: if( t1 == Type::TOP ) return Type::TOP; aoqi@0: const Type *t2 = phase->type( in(MemNode::Address) ); aoqi@0: if( t2 == Type::TOP ) return Type::TOP; aoqi@0: const Type *t3 = phase->type( in(MemNode::ValueIn) ); aoqi@0: if( t3 == Type::TOP ) return Type::TOP; aoqi@0: return Type::MEMORY; aoqi@0: } aoqi@0: aoqi@0: //------------------------------Identity--------------------------------------- aoqi@0: // Remove redundant stores: aoqi@0: // Store(m, p, Load(m, p)) changes to m. aoqi@0: // Store(, p, x) -> Store(m, p, x) changes to Store(m, p, x). aoqi@0: Node *StoreNode::Identity( PhaseTransform *phase ) { aoqi@0: Node* mem = in(MemNode::Memory); aoqi@0: Node* adr = in(MemNode::Address); aoqi@0: Node* val = in(MemNode::ValueIn); aoqi@0: aoqi@0: // Load then Store? Then the Store is useless aoqi@0: if (val->is_Load() && aoqi@0: val->in(MemNode::Address)->eqv_uncast(adr) && aoqi@0: val->in(MemNode::Memory )->eqv_uncast(mem) && aoqi@0: val->as_Load()->store_Opcode() == Opcode()) { aoqi@0: return mem; aoqi@0: } aoqi@0: aoqi@0: // Two stores in a row of the same value? aoqi@0: if (mem->is_Store() && aoqi@0: mem->in(MemNode::Address)->eqv_uncast(adr) && aoqi@0: mem->in(MemNode::ValueIn)->eqv_uncast(val) && aoqi@0: mem->Opcode() == Opcode()) { aoqi@0: return mem; aoqi@0: } aoqi@0: aoqi@0: // Store of zero anywhere into a freshly-allocated object? aoqi@0: // Then the store is useless. aoqi@0: // (It must already have been captured by the InitializeNode.) aoqi@0: if (ReduceFieldZeroing && phase->type(val)->is_zero_type()) { aoqi@0: // a newly allocated object is already all-zeroes everywhere aoqi@0: if (mem->is_Proj() && mem->in(0)->is_Allocate()) { aoqi@0: return mem; aoqi@0: } aoqi@0: aoqi@0: // the store may also apply to zero-bits in an earlier object aoqi@0: Node* prev_mem = find_previous_store(phase); aoqi@0: // Steps (a), (b): Walk past independent stores to find an exact match. aoqi@0: if (prev_mem != NULL) { aoqi@0: Node* prev_val = can_see_stored_value(prev_mem, phase); aoqi@0: if (prev_val != NULL && phase->eqv(prev_val, val)) { aoqi@0: // prev_val and val might differ by a cast; it would be good aoqi@0: // to keep the more informative of the two. aoqi@0: return mem; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: return this; aoqi@0: } aoqi@0: aoqi@0: //------------------------------match_edge------------------------------------- aoqi@0: // Do we Match on this edge index or not? Match only memory & value aoqi@0: uint StoreNode::match_edge(uint idx) const { aoqi@0: return idx == MemNode::Address || idx == MemNode::ValueIn; aoqi@0: } aoqi@0: aoqi@0: //------------------------------cmp-------------------------------------------- aoqi@0: // Do not common stores up together. They generally have to be split aoqi@0: // back up anyways, so do not bother. aoqi@0: uint StoreNode::cmp( const Node &n ) const { aoqi@0: return (&n == this); // Always fail except on self aoqi@0: } aoqi@0: aoqi@0: //------------------------------Ideal_masked_input----------------------------- aoqi@0: // Check for a useless mask before a partial-word store aoqi@0: // (StoreB ... (AndI valIn conIa) ) aoqi@0: // If (conIa & mask == mask) this simplifies to aoqi@0: // (StoreB ... (valIn) ) aoqi@0: Node *StoreNode::Ideal_masked_input(PhaseGVN *phase, uint mask) { aoqi@0: Node *val = in(MemNode::ValueIn); aoqi@0: if( val->Opcode() == Op_AndI ) { aoqi@0: const TypeInt *t = phase->type( val->in(2) )->isa_int(); aoqi@0: if( t && t->is_con() && (t->get_con() & mask) == mask ) { aoqi@0: set_req(MemNode::ValueIn, val->in(1)); aoqi@0: return this; aoqi@0: } aoqi@0: } aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: //------------------------------Ideal_sign_extended_input---------------------- aoqi@0: // Check for useless sign-extension before a partial-word store aoqi@0: // (StoreB ... (RShiftI _ (LShiftI _ valIn conIL ) conIR) ) aoqi@0: // If (conIL == conIR && conIR <= num_bits) this simplifies to aoqi@0: // (StoreB ... (valIn) ) aoqi@0: Node *StoreNode::Ideal_sign_extended_input(PhaseGVN *phase, int num_bits) { aoqi@0: Node *val = in(MemNode::ValueIn); aoqi@0: if( val->Opcode() == Op_RShiftI ) { aoqi@0: const TypeInt *t = phase->type( val->in(2) )->isa_int(); aoqi@0: if( t && t->is_con() && (t->get_con() <= num_bits) ) { aoqi@0: Node *shl = val->in(1); aoqi@0: if( shl->Opcode() == Op_LShiftI ) { aoqi@0: const TypeInt *t2 = phase->type( shl->in(2) )->isa_int(); aoqi@0: if( t2 && t2->is_con() && (t2->get_con() == t->get_con()) ) { aoqi@0: set_req(MemNode::ValueIn, shl->in(1)); aoqi@0: return this; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: //------------------------------value_never_loaded----------------------------------- aoqi@0: // Determine whether there are any possible loads of the value stored. aoqi@0: // For simplicity, we actually check if there are any loads from the aoqi@0: // address stored to, not just for loads of the value stored by this node. aoqi@0: // aoqi@0: bool StoreNode::value_never_loaded( PhaseTransform *phase) const { aoqi@0: Node *adr = in(Address); aoqi@0: const TypeOopPtr *adr_oop = phase->type(adr)->isa_oopptr(); aoqi@0: if (adr_oop == NULL) aoqi@0: return false; aoqi@0: if (!adr_oop->is_known_instance_field()) aoqi@0: return false; // if not a distinct instance, there may be aliases of the address aoqi@0: for (DUIterator_Fast imax, i = adr->fast_outs(imax); i < imax; i++) { aoqi@0: Node *use = adr->fast_out(i); aoqi@0: int opc = use->Opcode(); aoqi@0: if (use->is_Load() || use->is_LoadStore()) { aoqi@0: return false; aoqi@0: } aoqi@0: } aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: //============================================================================= aoqi@0: //------------------------------Ideal------------------------------------------ aoqi@0: // If the store is from an AND mask that leaves the low bits untouched, then aoqi@0: // we can skip the AND operation. If the store is from a sign-extension aoqi@0: // (a left shift, then right shift) we can skip both. aoqi@0: Node *StoreBNode::Ideal(PhaseGVN *phase, bool can_reshape){ aoqi@0: Node *progress = StoreNode::Ideal_masked_input(phase, 0xFF); aoqi@0: if( progress != NULL ) return progress; aoqi@0: aoqi@0: progress = StoreNode::Ideal_sign_extended_input(phase, 24); aoqi@0: if( progress != NULL ) return progress; aoqi@0: aoqi@0: // Finally check the default case aoqi@0: return StoreNode::Ideal(phase, can_reshape); aoqi@0: } aoqi@0: aoqi@0: //============================================================================= aoqi@0: //------------------------------Ideal------------------------------------------ aoqi@0: // If the store is from an AND mask that leaves the low bits untouched, then aoqi@0: // we can skip the AND operation aoqi@0: Node *StoreCNode::Ideal(PhaseGVN *phase, bool can_reshape){ aoqi@0: Node *progress = StoreNode::Ideal_masked_input(phase, 0xFFFF); aoqi@0: if( progress != NULL ) return progress; aoqi@0: aoqi@0: progress = StoreNode::Ideal_sign_extended_input(phase, 16); aoqi@0: if( progress != NULL ) return progress; aoqi@0: aoqi@0: // Finally check the default case aoqi@0: return StoreNode::Ideal(phase, can_reshape); aoqi@0: } aoqi@0: aoqi@0: //============================================================================= aoqi@0: //------------------------------Identity--------------------------------------- aoqi@0: Node *StoreCMNode::Identity( PhaseTransform *phase ) { aoqi@0: // No need to card mark when storing a null ptr aoqi@0: Node* my_store = in(MemNode::OopStore); aoqi@0: if (my_store->is_Store()) { aoqi@0: const Type *t1 = phase->type( my_store->in(MemNode::ValueIn) ); aoqi@0: if( t1 == TypePtr::NULL_PTR ) { aoqi@0: return in(MemNode::Memory); aoqi@0: } aoqi@0: } aoqi@0: return this; aoqi@0: } aoqi@0: aoqi@0: //============================================================================= aoqi@0: //------------------------------Ideal--------------------------------------- aoqi@0: Node *StoreCMNode::Ideal(PhaseGVN *phase, bool can_reshape){ aoqi@0: Node* progress = StoreNode::Ideal(phase, can_reshape); aoqi@0: if (progress != NULL) return progress; aoqi@0: aoqi@0: Node* my_store = in(MemNode::OopStore); aoqi@0: if (my_store->is_MergeMem()) { aoqi@0: Node* mem = my_store->as_MergeMem()->memory_at(oop_alias_idx()); aoqi@0: set_req(MemNode::OopStore, mem); aoqi@0: return this; aoqi@0: } aoqi@0: aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: //------------------------------Value----------------------------------------- aoqi@0: const Type *StoreCMNode::Value( PhaseTransform *phase ) const { aoqi@0: // Either input is TOP ==> the result is TOP aoqi@0: const Type *t = phase->type( in(MemNode::Memory) ); aoqi@0: if( t == Type::TOP ) return Type::TOP; aoqi@0: t = phase->type( in(MemNode::Address) ); aoqi@0: if( t == Type::TOP ) return Type::TOP; aoqi@0: t = phase->type( in(MemNode::ValueIn) ); aoqi@0: if( t == Type::TOP ) return Type::TOP; aoqi@0: // If extra input is TOP ==> the result is TOP aoqi@0: t = phase->type( in(MemNode::OopStore) ); aoqi@0: if( t == Type::TOP ) return Type::TOP; aoqi@0: aoqi@0: return StoreNode::Value( phase ); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: //============================================================================= aoqi@0: //----------------------------------SCMemProjNode------------------------------ aoqi@0: const Type * SCMemProjNode::Value( PhaseTransform *phase ) const aoqi@0: { aoqi@0: return bottom_type(); aoqi@0: } aoqi@0: aoqi@0: //============================================================================= aoqi@0: //----------------------------------LoadStoreNode------------------------------ aoqi@0: LoadStoreNode::LoadStoreNode( Node *c, Node *mem, Node *adr, Node *val, const TypePtr* at, const Type* rt, uint required ) aoqi@0: : Node(required), aoqi@0: _type(rt), aoqi@0: _adr_type(at) aoqi@0: { aoqi@0: init_req(MemNode::Control, c ); aoqi@0: init_req(MemNode::Memory , mem); aoqi@0: init_req(MemNode::Address, adr); aoqi@0: init_req(MemNode::ValueIn, val); aoqi@0: init_class_id(Class_LoadStore); aoqi@0: } aoqi@0: aoqi@0: uint LoadStoreNode::ideal_reg() const { aoqi@0: return _type->ideal_reg(); aoqi@0: } aoqi@0: aoqi@0: bool LoadStoreNode::result_not_used() const { aoqi@0: for( DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++ ) { aoqi@0: Node *x = fast_out(i); aoqi@0: if (x->Opcode() == Op_SCMemProj) continue; aoqi@0: return false; aoqi@0: } aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: uint LoadStoreNode::size_of() const { return sizeof(*this); } aoqi@0: aoqi@0: //============================================================================= aoqi@0: //----------------------------------LoadStoreConditionalNode-------------------- aoqi@0: LoadStoreConditionalNode::LoadStoreConditionalNode( Node *c, Node *mem, Node *adr, Node *val, Node *ex ) : LoadStoreNode(c, mem, adr, val, NULL, TypeInt::BOOL, 5) { aoqi@0: init_req(ExpectedIn, ex ); aoqi@0: } aoqi@0: aoqi@0: //============================================================================= aoqi@0: //-------------------------------adr_type-------------------------------------- aoqi@0: // Do we Match on this edge index or not? Do not match memory aoqi@0: const TypePtr* ClearArrayNode::adr_type() const { aoqi@0: Node *adr = in(3); aoqi@0: return MemNode::calculate_adr_type(adr->bottom_type()); aoqi@0: } aoqi@0: aoqi@0: //------------------------------match_edge------------------------------------- aoqi@0: // Do we Match on this edge index or not? Do not match memory aoqi@0: uint ClearArrayNode::match_edge(uint idx) const { aoqi@0: return idx > 1; aoqi@0: } aoqi@0: aoqi@0: //------------------------------Identity--------------------------------------- aoqi@0: // Clearing a zero length array does nothing aoqi@0: Node *ClearArrayNode::Identity( PhaseTransform *phase ) { aoqi@0: return phase->type(in(2))->higher_equal(TypeX::ZERO) ? in(1) : this; aoqi@0: } aoqi@0: aoqi@0: //------------------------------Idealize--------------------------------------- aoqi@0: // Clearing a short array is faster with stores aoqi@0: Node *ClearArrayNode::Ideal(PhaseGVN *phase, bool can_reshape){ aoqi@0: const int unit = BytesPerLong; aoqi@0: const TypeX* t = phase->type(in(2))->isa_intptr_t(); aoqi@0: if (!t) return NULL; aoqi@0: if (!t->is_con()) return NULL; aoqi@0: intptr_t raw_count = t->get_con(); aoqi@0: intptr_t size = raw_count; aoqi@0: if (!Matcher::init_array_count_is_in_bytes) size *= unit; aoqi@0: // Clearing nothing uses the Identity call. aoqi@0: // Negative clears are possible on dead ClearArrays aoqi@0: // (see jck test stmt114.stmt11402.val). aoqi@0: if (size <= 0 || size % unit != 0) return NULL; aoqi@0: intptr_t count = size / unit; aoqi@0: // Length too long; use fast hardware clear aoqi@0: if (size > Matcher::init_array_short_size) return NULL; aoqi@0: Node *mem = in(1); aoqi@0: if( phase->type(mem)==Type::TOP ) return NULL; aoqi@0: Node *adr = in(3); aoqi@0: const Type* at = phase->type(adr); aoqi@0: if( at==Type::TOP ) return NULL; aoqi@0: const TypePtr* atp = at->isa_ptr(); aoqi@0: // adjust atp to be the correct array element address type aoqi@0: if (atp == NULL) atp = TypePtr::BOTTOM; aoqi@0: else atp = atp->add_offset(Type::OffsetBot); aoqi@0: // Get base for derived pointer purposes aoqi@0: if( adr->Opcode() != Op_AddP ) Unimplemented(); aoqi@0: Node *base = adr->in(1); aoqi@0: aoqi@0: Node *zero = phase->makecon(TypeLong::ZERO); aoqi@0: Node *off = phase->MakeConX(BytesPerLong); aoqi@0: mem = new (phase->C) StoreLNode(in(0),mem,adr,atp,zero,MemNode::unordered,false); aoqi@0: count--; aoqi@0: while( count-- ) { aoqi@0: mem = phase->transform(mem); aoqi@0: adr = phase->transform(new (phase->C) AddPNode(base,adr,off)); aoqi@0: mem = new (phase->C) StoreLNode(in(0),mem,adr,atp,zero,MemNode::unordered,false); aoqi@0: } aoqi@0: return mem; aoqi@0: } aoqi@0: aoqi@0: //----------------------------step_through---------------------------------- aoqi@0: // Return allocation input memory edge if it is different instance aoqi@0: // or itself if it is the one we are looking for. aoqi@0: bool ClearArrayNode::step_through(Node** np, uint instance_id, PhaseTransform* phase) { aoqi@0: Node* n = *np; aoqi@0: assert(n->is_ClearArray(), "sanity"); aoqi@0: intptr_t offset; aoqi@0: AllocateNode* alloc = AllocateNode::Ideal_allocation(n->in(3), phase, offset); aoqi@0: // This method is called only before Allocate nodes are expanded during aoqi@0: // macro nodes expansion. Before that ClearArray nodes are only generated aoqi@0: // in LibraryCallKit::generate_arraycopy() which follows allocations. aoqi@0: assert(alloc != NULL, "should have allocation"); aoqi@0: if (alloc->_idx == instance_id) { aoqi@0: // Can not bypass initialization of the instance we are looking for. aoqi@0: return false; aoqi@0: } aoqi@0: // Otherwise skip it. aoqi@0: InitializeNode* init = alloc->initialization(); aoqi@0: if (init != NULL) aoqi@0: *np = init->in(TypeFunc::Memory); aoqi@0: else aoqi@0: *np = alloc->in(TypeFunc::Memory); aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: //----------------------------clear_memory------------------------------------- aoqi@0: // Generate code to initialize object storage to zero. aoqi@0: Node* ClearArrayNode::clear_memory(Node* ctl, Node* mem, Node* dest, aoqi@0: intptr_t start_offset, aoqi@0: Node* end_offset, aoqi@0: PhaseGVN* phase) { aoqi@0: Compile* C = phase->C; aoqi@0: intptr_t offset = start_offset; aoqi@0: aoqi@0: int unit = BytesPerLong; aoqi@0: if ((offset % unit) != 0) { aoqi@0: Node* adr = new (C) AddPNode(dest, dest, phase->MakeConX(offset)); aoqi@0: adr = phase->transform(adr); aoqi@0: const TypePtr* atp = TypeRawPtr::BOTTOM; aoqi@0: mem = StoreNode::make(*phase, ctl, mem, adr, atp, phase->zerocon(T_INT), T_INT, MemNode::unordered); aoqi@0: mem = phase->transform(mem); aoqi@0: offset += BytesPerInt; aoqi@0: } aoqi@0: assert((offset % unit) == 0, ""); aoqi@0: aoqi@0: // Initialize the remaining stuff, if any, with a ClearArray. aoqi@0: return clear_memory(ctl, mem, dest, phase->MakeConX(offset), end_offset, phase); aoqi@0: } aoqi@0: aoqi@0: Node* ClearArrayNode::clear_memory(Node* ctl, Node* mem, Node* dest, aoqi@0: Node* start_offset, aoqi@0: Node* end_offset, aoqi@0: PhaseGVN* phase) { aoqi@0: if (start_offset == end_offset) { aoqi@0: // nothing to do aoqi@0: return mem; aoqi@0: } aoqi@0: aoqi@0: Compile* C = phase->C; aoqi@0: int unit = BytesPerLong; aoqi@0: Node* zbase = start_offset; aoqi@0: Node* zend = end_offset; aoqi@0: aoqi@0: // Scale to the unit required by the CPU: aoqi@0: if (!Matcher::init_array_count_is_in_bytes) { aoqi@0: Node* shift = phase->intcon(exact_log2(unit)); aoqi@0: zbase = phase->transform( new(C) URShiftXNode(zbase, shift) ); aoqi@0: zend = phase->transform( new(C) URShiftXNode(zend, shift) ); aoqi@0: } aoqi@0: aoqi@0: // Bulk clear double-words aoqi@0: Node* zsize = phase->transform( new(C) SubXNode(zend, zbase) ); aoqi@0: Node* adr = phase->transform( new(C) AddPNode(dest, dest, start_offset) ); aoqi@0: mem = new (C) ClearArrayNode(ctl, mem, zsize, adr); aoqi@0: return phase->transform(mem); aoqi@0: } aoqi@0: aoqi@0: Node* ClearArrayNode::clear_memory(Node* ctl, Node* mem, Node* dest, aoqi@0: intptr_t start_offset, aoqi@0: intptr_t end_offset, aoqi@0: PhaseGVN* phase) { aoqi@0: if (start_offset == end_offset) { aoqi@0: // nothing to do aoqi@0: return mem; aoqi@0: } aoqi@0: aoqi@0: Compile* C = phase->C; aoqi@0: assert((end_offset % BytesPerInt) == 0, "odd end offset"); aoqi@0: intptr_t done_offset = end_offset; aoqi@0: if ((done_offset % BytesPerLong) != 0) { aoqi@0: done_offset -= BytesPerInt; aoqi@0: } aoqi@0: if (done_offset > start_offset) { aoqi@0: mem = clear_memory(ctl, mem, dest, aoqi@0: start_offset, phase->MakeConX(done_offset), phase); aoqi@0: } aoqi@0: if (done_offset < end_offset) { // emit the final 32-bit store aoqi@0: Node* adr = new (C) AddPNode(dest, dest, phase->MakeConX(done_offset)); aoqi@0: adr = phase->transform(adr); aoqi@0: const TypePtr* atp = TypeRawPtr::BOTTOM; aoqi@0: mem = StoreNode::make(*phase, ctl, mem, adr, atp, phase->zerocon(T_INT), T_INT, MemNode::unordered); aoqi@0: mem = phase->transform(mem); aoqi@0: done_offset += BytesPerInt; aoqi@0: } aoqi@0: assert(done_offset == end_offset, ""); aoqi@0: return mem; aoqi@0: } aoqi@0: aoqi@0: //============================================================================= aoqi@0: // Do not match memory edge. aoqi@0: uint StrIntrinsicNode::match_edge(uint idx) const { aoqi@0: return idx == 2 || idx == 3; aoqi@0: } aoqi@0: aoqi@0: //------------------------------Ideal------------------------------------------ aoqi@0: // Return a node which is more "ideal" than the current node. Strip out aoqi@0: // control copies aoqi@0: Node *StrIntrinsicNode::Ideal(PhaseGVN *phase, bool can_reshape) { aoqi@0: if (remove_dead_region(phase, can_reshape)) return this; aoqi@0: // Don't bother trying to transform a dead node aoqi@0: if (in(0) && in(0)->is_top()) return NULL; aoqi@0: aoqi@0: if (can_reshape) { aoqi@0: Node* mem = phase->transform(in(MemNode::Memory)); aoqi@0: // If transformed to a MergeMem, get the desired slice aoqi@0: uint alias_idx = phase->C->get_alias_index(adr_type()); aoqi@0: mem = mem->is_MergeMem() ? mem->as_MergeMem()->memory_at(alias_idx) : mem; aoqi@0: if (mem != in(MemNode::Memory)) { aoqi@0: set_req(MemNode::Memory, mem); aoqi@0: return this; aoqi@0: } aoqi@0: } aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: //------------------------------Value------------------------------------------ aoqi@0: const Type *StrIntrinsicNode::Value( PhaseTransform *phase ) const { aoqi@0: if (in(0) && phase->type(in(0)) == Type::TOP) return Type::TOP; aoqi@0: return bottom_type(); aoqi@0: } aoqi@0: aoqi@0: //============================================================================= aoqi@0: //------------------------------match_edge------------------------------------- aoqi@0: // Do not match memory edge aoqi@0: uint EncodeISOArrayNode::match_edge(uint idx) const { aoqi@0: return idx == 2 || idx == 3; // EncodeISOArray src (Binary dst len) aoqi@0: } aoqi@0: aoqi@0: //------------------------------Ideal------------------------------------------ aoqi@0: // Return a node which is more "ideal" than the current node. Strip out aoqi@0: // control copies aoqi@0: Node *EncodeISOArrayNode::Ideal(PhaseGVN *phase, bool can_reshape) { aoqi@0: return remove_dead_region(phase, can_reshape) ? this : NULL; aoqi@0: } aoqi@0: aoqi@0: //------------------------------Value------------------------------------------ aoqi@0: const Type *EncodeISOArrayNode::Value(PhaseTransform *phase) const { aoqi@0: if (in(0) && phase->type(in(0)) == Type::TOP) return Type::TOP; aoqi@0: return bottom_type(); aoqi@0: } aoqi@0: aoqi@0: //============================================================================= aoqi@0: MemBarNode::MemBarNode(Compile* C, int alias_idx, Node* precedent) aoqi@0: : MultiNode(TypeFunc::Parms + (precedent == NULL? 0: 1)), aoqi@0: _adr_type(C->get_adr_type(alias_idx)) aoqi@0: { aoqi@0: init_class_id(Class_MemBar); aoqi@0: Node* top = C->top(); aoqi@0: init_req(TypeFunc::I_O,top); aoqi@0: init_req(TypeFunc::FramePtr,top); aoqi@0: init_req(TypeFunc::ReturnAdr,top); aoqi@0: if (precedent != NULL) aoqi@0: init_req(TypeFunc::Parms, precedent); aoqi@0: } aoqi@0: aoqi@0: //------------------------------cmp-------------------------------------------- aoqi@0: uint MemBarNode::hash() const { return NO_HASH; } aoqi@0: uint MemBarNode::cmp( const Node &n ) const { aoqi@0: return (&n == this); // Always fail except on self aoqi@0: } aoqi@0: aoqi@0: //------------------------------make------------------------------------------- aoqi@0: MemBarNode* MemBarNode::make(Compile* C, int opcode, int atp, Node* pn) { aoqi@0: switch (opcode) { aoqi@0: case Op_MemBarAcquire: return new(C) MemBarAcquireNode(C, atp, pn); aoqi@0: case Op_LoadFence: return new(C) LoadFenceNode(C, atp, pn); aoqi@0: case Op_MemBarRelease: return new(C) MemBarReleaseNode(C, atp, pn); aoqi@0: case Op_StoreFence: return new(C) StoreFenceNode(C, atp, pn); aoqi@0: case Op_MemBarAcquireLock: return new(C) MemBarAcquireLockNode(C, atp, pn); aoqi@0: case Op_MemBarReleaseLock: return new(C) MemBarReleaseLockNode(C, atp, pn); aoqi@0: case Op_MemBarVolatile: return new(C) MemBarVolatileNode(C, atp, pn); aoqi@0: case Op_MemBarCPUOrder: return new(C) MemBarCPUOrderNode(C, atp, pn); aoqi@0: case Op_Initialize: return new(C) InitializeNode(C, atp, pn); aoqi@0: case Op_MemBarStoreStore: return new(C) MemBarStoreStoreNode(C, atp, pn); aoqi@0: default: ShouldNotReachHere(); return NULL; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: //------------------------------Ideal------------------------------------------ aoqi@0: // Return a node which is more "ideal" than the current node. Strip out aoqi@0: // control copies aoqi@0: Node *MemBarNode::Ideal(PhaseGVN *phase, bool can_reshape) { aoqi@0: if (remove_dead_region(phase, can_reshape)) return this; aoqi@0: // Don't bother trying to transform a dead node aoqi@0: if (in(0) && in(0)->is_top()) { aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: // Eliminate volatile MemBars for scalar replaced objects. aoqi@0: if (can_reshape && req() == (Precedent+1)) { aoqi@0: bool eliminate = false; aoqi@0: int opc = Opcode(); aoqi@0: if ((opc == Op_MemBarAcquire || opc == Op_MemBarVolatile)) { aoqi@0: // Volatile field loads and stores. aoqi@0: Node* my_mem = in(MemBarNode::Precedent); aoqi@0: // The MembarAquire may keep an unused LoadNode alive through the Precedent edge aoqi@0: if ((my_mem != NULL) && (opc == Op_MemBarAcquire) && (my_mem->outcnt() == 1)) { aoqi@0: // if the Precedent is a decodeN and its input (a Load) is used at more than one place, aoqi@0: // replace this Precedent (decodeN) with the Load instead. aoqi@0: if ((my_mem->Opcode() == Op_DecodeN) && (my_mem->in(1)->outcnt() > 1)) { aoqi@0: Node* load_node = my_mem->in(1); aoqi@0: set_req(MemBarNode::Precedent, load_node); aoqi@0: phase->is_IterGVN()->_worklist.push(my_mem); aoqi@0: my_mem = load_node; aoqi@0: } else { aoqi@0: assert(my_mem->unique_out() == this, "sanity"); aoqi@0: del_req(Precedent); aoqi@0: phase->is_IterGVN()->_worklist.push(my_mem); // remove dead node later aoqi@0: my_mem = NULL; aoqi@0: } aoqi@0: } aoqi@0: if (my_mem != NULL && my_mem->is_Mem()) { aoqi@0: const TypeOopPtr* t_oop = my_mem->in(MemNode::Address)->bottom_type()->isa_oopptr(); aoqi@0: // Check for scalar replaced object reference. aoqi@0: if( t_oop != NULL && t_oop->is_known_instance_field() && aoqi@0: t_oop->offset() != Type::OffsetBot && aoqi@0: t_oop->offset() != Type::OffsetTop) { aoqi@0: eliminate = true; aoqi@0: } aoqi@0: } aoqi@0: } else if (opc == Op_MemBarRelease) { aoqi@0: // Final field stores. aoqi@0: Node* alloc = AllocateNode::Ideal_allocation(in(MemBarNode::Precedent), phase); aoqi@0: if ((alloc != NULL) && alloc->is_Allocate() && aoqi@0: alloc->as_Allocate()->_is_non_escaping) { aoqi@0: // The allocated object does not escape. aoqi@0: eliminate = true; aoqi@0: } aoqi@0: } aoqi@0: if (eliminate) { aoqi@0: // Replace MemBar projections by its inputs. aoqi@0: PhaseIterGVN* igvn = phase->is_IterGVN(); aoqi@0: igvn->replace_node(proj_out(TypeFunc::Memory), in(TypeFunc::Memory)); aoqi@0: igvn->replace_node(proj_out(TypeFunc::Control), in(TypeFunc::Control)); aoqi@0: // Must return either the original node (now dead) or a new node aoqi@0: // (Do not return a top here, since that would break the uniqueness of top.) aoqi@0: return new (phase->C) ConINode(TypeInt::ZERO); aoqi@0: } aoqi@0: } aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: //------------------------------Value------------------------------------------ aoqi@0: const Type *MemBarNode::Value( PhaseTransform *phase ) const { aoqi@0: if( !in(0) ) return Type::TOP; aoqi@0: if( phase->type(in(0)) == Type::TOP ) aoqi@0: return Type::TOP; aoqi@0: return TypeTuple::MEMBAR; aoqi@0: } aoqi@0: aoqi@0: //------------------------------match------------------------------------------ aoqi@0: // Construct projections for memory. aoqi@0: Node *MemBarNode::match( const ProjNode *proj, const Matcher *m ) { aoqi@0: switch (proj->_con) { aoqi@0: case TypeFunc::Control: aoqi@0: case TypeFunc::Memory: aoqi@0: return new (m->C) MachProjNode(this,proj->_con,RegMask::Empty,MachProjNode::unmatched_proj); aoqi@0: } aoqi@0: ShouldNotReachHere(); aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: //===========================InitializeNode==================================== aoqi@0: // SUMMARY: aoqi@0: // This node acts as a memory barrier on raw memory, after some raw stores. aoqi@0: // The 'cooked' oop value feeds from the Initialize, not the Allocation. aoqi@0: // The Initialize can 'capture' suitably constrained stores as raw inits. aoqi@0: // It can coalesce related raw stores into larger units (called 'tiles'). aoqi@0: // It can avoid zeroing new storage for memory units which have raw inits. aoqi@0: // At macro-expansion, it is marked 'complete', and does not optimize further. aoqi@0: // aoqi@0: // EXAMPLE: aoqi@0: // The object 'new short[2]' occupies 16 bytes in a 32-bit machine. aoqi@0: // ctl = incoming control; mem* = incoming memory aoqi@0: // (Note: A star * on a memory edge denotes I/O and other standard edges.) aoqi@0: // First allocate uninitialized memory and fill in the header: aoqi@0: // alloc = (Allocate ctl mem* 16 #short[].klass ...) aoqi@0: // ctl := alloc.Control; mem* := alloc.Memory* aoqi@0: // rawmem = alloc.Memory; rawoop = alloc.RawAddress aoqi@0: // Then initialize to zero the non-header parts of the raw memory block: aoqi@0: // init = (Initialize alloc.Control alloc.Memory* alloc.RawAddress) aoqi@0: // ctl := init.Control; mem.SLICE(#short[*]) := init.Memory aoqi@0: // After the initialize node executes, the object is ready for service: aoqi@0: // oop := (CheckCastPP init.Control alloc.RawAddress #short[]) aoqi@0: // Suppose its body is immediately initialized as {1,2}: aoqi@0: // store1 = (StoreC init.Control init.Memory (+ oop 12) 1) aoqi@0: // store2 = (StoreC init.Control store1 (+ oop 14) 2) aoqi@0: // mem.SLICE(#short[*]) := store2 aoqi@0: // aoqi@0: // DETAILS: aoqi@0: // An InitializeNode collects and isolates object initialization after aoqi@0: // an AllocateNode and before the next possible safepoint. As a aoqi@0: // memory barrier (MemBarNode), it keeps critical stores from drifting aoqi@0: // down past any safepoint or any publication of the allocation. aoqi@0: // Before this barrier, a newly-allocated object may have uninitialized bits. aoqi@0: // After this barrier, it may be treated as a real oop, and GC is allowed. aoqi@0: // aoqi@0: // The semantics of the InitializeNode include an implicit zeroing of aoqi@0: // the new object from object header to the end of the object. aoqi@0: // (The object header and end are determined by the AllocateNode.) aoqi@0: // aoqi@0: // Certain stores may be added as direct inputs to the InitializeNode. aoqi@0: // These stores must update raw memory, and they must be to addresses aoqi@0: // derived from the raw address produced by AllocateNode, and with aoqi@0: // a constant offset. They must be ordered by increasing offset. aoqi@0: // The first one is at in(RawStores), the last at in(req()-1). aoqi@0: // Unlike most memory operations, they are not linked in a chain, aoqi@0: // but are displayed in parallel as users of the rawmem output of aoqi@0: // the allocation. aoqi@0: // aoqi@0: // (See comments in InitializeNode::capture_store, which continue aoqi@0: // the example given above.) aoqi@0: // aoqi@0: // When the associated Allocate is macro-expanded, the InitializeNode aoqi@0: // may be rewritten to optimize collected stores. A ClearArrayNode aoqi@0: // may also be created at that point to represent any required zeroing. aoqi@0: // The InitializeNode is then marked 'complete', prohibiting further aoqi@0: // capturing of nearby memory operations. aoqi@0: // aoqi@0: // During macro-expansion, all captured initializations which store aoqi@0: // constant values of 32 bits or smaller are coalesced (if advantageous) aoqi@0: // into larger 'tiles' 32 or 64 bits. This allows an object to be aoqi@0: // initialized in fewer memory operations. Memory words which are aoqi@0: // covered by neither tiles nor non-constant stores are pre-zeroed aoqi@0: // by explicit stores of zero. (The code shape happens to do all aoqi@0: // zeroing first, then all other stores, with both sequences occurring aoqi@0: // in order of ascending offsets.) aoqi@0: // aoqi@0: // Alternatively, code may be inserted between an AllocateNode and its aoqi@0: // InitializeNode, to perform arbitrary initialization of the new object. aoqi@0: // E.g., the object copying intrinsics insert complex data transfers here. aoqi@0: // The initialization must then be marked as 'complete' disable the aoqi@0: // built-in zeroing semantics and the collection of initializing stores. aoqi@0: // aoqi@0: // While an InitializeNode is incomplete, reads from the memory state aoqi@0: // produced by it are optimizable if they match the control edge and aoqi@0: // new oop address associated with the allocation/initialization. aoqi@0: // They return a stored value (if the offset matches) or else zero. aoqi@0: // A write to the memory state, if it matches control and address, aoqi@0: // and if it is to a constant offset, may be 'captured' by the aoqi@0: // InitializeNode. It is cloned as a raw memory operation and rewired aoqi@0: // inside the initialization, to the raw oop produced by the allocation. aoqi@0: // Operations on addresses which are provably distinct (e.g., to aoqi@0: // other AllocateNodes) are allowed to bypass the initialization. aoqi@0: // aoqi@0: // The effect of all this is to consolidate object initialization aoqi@0: // (both arrays and non-arrays, both piecewise and bulk) into a aoqi@0: // single location, where it can be optimized as a unit. aoqi@0: // aoqi@0: // Only stores with an offset less than TrackedInitializationLimit words aoqi@0: // will be considered for capture by an InitializeNode. This puts a aoqi@0: // reasonable limit on the complexity of optimized initializations. aoqi@0: aoqi@0: //---------------------------InitializeNode------------------------------------ aoqi@0: InitializeNode::InitializeNode(Compile* C, int adr_type, Node* rawoop) aoqi@0: : _is_complete(Incomplete), _does_not_escape(false), aoqi@0: MemBarNode(C, adr_type, rawoop) aoqi@0: { aoqi@0: init_class_id(Class_Initialize); aoqi@0: aoqi@0: assert(adr_type == Compile::AliasIdxRaw, "only valid atp"); aoqi@0: assert(in(RawAddress) == rawoop, "proper init"); aoqi@0: // Note: allocation() can be NULL, for secondary initialization barriers aoqi@0: } aoqi@0: aoqi@0: // Since this node is not matched, it will be processed by the aoqi@0: // register allocator. Declare that there are no constraints aoqi@0: // on the allocation of the RawAddress edge. aoqi@0: const RegMask &InitializeNode::in_RegMask(uint idx) const { aoqi@0: // This edge should be set to top, by the set_complete. But be conservative. aoqi@0: if (idx == InitializeNode::RawAddress) aoqi@0: return *(Compile::current()->matcher()->idealreg2spillmask[in(idx)->ideal_reg()]); aoqi@0: return RegMask::Empty; aoqi@0: } aoqi@0: aoqi@0: Node* InitializeNode::memory(uint alias_idx) { aoqi@0: Node* mem = in(Memory); aoqi@0: if (mem->is_MergeMem()) { aoqi@0: return mem->as_MergeMem()->memory_at(alias_idx); aoqi@0: } else { aoqi@0: // incoming raw memory is not split aoqi@0: return mem; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: bool InitializeNode::is_non_zero() { aoqi@0: if (is_complete()) return false; aoqi@0: remove_extra_zeroes(); aoqi@0: return (req() > RawStores); aoqi@0: } aoqi@0: aoqi@0: void InitializeNode::set_complete(PhaseGVN* phase) { aoqi@0: assert(!is_complete(), "caller responsibility"); aoqi@0: _is_complete = Complete; aoqi@0: aoqi@0: // After this node is complete, it contains a bunch of aoqi@0: // raw-memory initializations. There is no need for aoqi@0: // it to have anything to do with non-raw memory effects. aoqi@0: // Therefore, tell all non-raw users to re-optimize themselves, aoqi@0: // after skipping the memory effects of this initialization. aoqi@0: PhaseIterGVN* igvn = phase->is_IterGVN(); aoqi@0: if (igvn) igvn->add_users_to_worklist(this); aoqi@0: } aoqi@0: aoqi@0: // convenience function aoqi@0: // return false if the init contains any stores already aoqi@0: bool AllocateNode::maybe_set_complete(PhaseGVN* phase) { aoqi@0: InitializeNode* init = initialization(); aoqi@0: if (init == NULL || init->is_complete()) return false; aoqi@0: init->remove_extra_zeroes(); aoqi@0: // for now, if this allocation has already collected any inits, bail: aoqi@0: if (init->is_non_zero()) return false; aoqi@0: init->set_complete(phase); aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: void InitializeNode::remove_extra_zeroes() { aoqi@0: if (req() == RawStores) return; aoqi@0: Node* zmem = zero_memory(); aoqi@0: uint fill = RawStores; aoqi@0: for (uint i = fill; i < req(); i++) { aoqi@0: Node* n = in(i); aoqi@0: if (n->is_top() || n == zmem) continue; // skip aoqi@0: if (fill < i) set_req(fill, n); // compact aoqi@0: ++fill; aoqi@0: } aoqi@0: // delete any empty spaces created: aoqi@0: while (fill < req()) { aoqi@0: del_req(fill); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Helper for remembering which stores go with which offsets. aoqi@0: intptr_t InitializeNode::get_store_offset(Node* st, PhaseTransform* phase) { aoqi@0: if (!st->is_Store()) return -1; // can happen to dead code via subsume_node aoqi@0: intptr_t offset = -1; aoqi@0: Node* base = AddPNode::Ideal_base_and_offset(st->in(MemNode::Address), aoqi@0: phase, offset); aoqi@0: if (base == NULL) return -1; // something is dead, aoqi@0: if (offset < 0) return -1; // dead, dead aoqi@0: return offset; aoqi@0: } aoqi@0: aoqi@0: // Helper for proving that an initialization expression is aoqi@0: // "simple enough" to be folded into an object initialization. aoqi@0: // Attempts to prove that a store's initial value 'n' can be captured aoqi@0: // within the initialization without creating a vicious cycle, such as: aoqi@0: // { Foo p = new Foo(); p.next = p; } aoqi@0: // True for constants and parameters and small combinations thereof. aoqi@0: bool InitializeNode::detect_init_independence(Node* n, int& count) { aoqi@0: if (n == NULL) return true; // (can this really happen?) aoqi@0: if (n->is_Proj()) n = n->in(0); aoqi@0: if (n == this) return false; // found a cycle aoqi@0: if (n->is_Con()) return true; aoqi@0: if (n->is_Start()) return true; // params, etc., are OK aoqi@0: if (n->is_Root()) return true; // even better aoqi@0: aoqi@0: Node* ctl = n->in(0); aoqi@0: if (ctl != NULL && !ctl->is_top()) { aoqi@0: if (ctl->is_Proj()) ctl = ctl->in(0); aoqi@0: if (ctl == this) return false; aoqi@0: aoqi@0: // If we already know that the enclosing memory op is pinned right after aoqi@0: // the init, then any control flow that the store has picked up aoqi@0: // must have preceded the init, or else be equal to the init. aoqi@0: // Even after loop optimizations (which might change control edges) aoqi@0: // a store is never pinned *before* the availability of its inputs. aoqi@0: if (!MemNode::all_controls_dominate(n, this)) aoqi@0: return false; // failed to prove a good control aoqi@0: } aoqi@0: aoqi@0: // Check data edges for possible dependencies on 'this'. aoqi@0: if ((count += 1) > 20) return false; // complexity limit aoqi@0: for (uint i = 1; i < n->req(); i++) { aoqi@0: Node* m = n->in(i); aoqi@0: if (m == NULL || m == n || m->is_top()) continue; aoqi@0: uint first_i = n->find_edge(m); aoqi@0: if (i != first_i) continue; // process duplicate edge just once aoqi@0: if (!detect_init_independence(m, count)) { aoqi@0: return false; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: // Here are all the checks a Store must pass before it can be moved into aoqi@0: // an initialization. Returns zero if a check fails. aoqi@0: // On success, returns the (constant) offset to which the store applies, aoqi@0: // within the initialized memory. aoqi@0: intptr_t InitializeNode::can_capture_store(StoreNode* st, PhaseTransform* phase, bool can_reshape) { aoqi@0: const int FAIL = 0; aoqi@0: if (st->req() != MemNode::ValueIn + 1) aoqi@0: return FAIL; // an inscrutable StoreNode (card mark?) aoqi@0: Node* ctl = st->in(MemNode::Control); aoqi@0: if (!(ctl != NULL && ctl->is_Proj() && ctl->in(0) == this)) aoqi@0: return FAIL; // must be unconditional after the initialization aoqi@0: Node* mem = st->in(MemNode::Memory); aoqi@0: if (!(mem->is_Proj() && mem->in(0) == this)) aoqi@0: return FAIL; // must not be preceded by other stores aoqi@0: Node* adr = st->in(MemNode::Address); aoqi@0: intptr_t offset; aoqi@0: AllocateNode* alloc = AllocateNode::Ideal_allocation(adr, phase, offset); aoqi@0: if (alloc == NULL) aoqi@0: return FAIL; // inscrutable address aoqi@0: if (alloc != allocation()) aoqi@0: return FAIL; // wrong allocation! (store needs to float up) aoqi@0: Node* val = st->in(MemNode::ValueIn); aoqi@0: int complexity_count = 0; aoqi@0: if (!detect_init_independence(val, complexity_count)) aoqi@0: return FAIL; // stored value must be 'simple enough' aoqi@0: aoqi@0: // The Store can be captured only if nothing after the allocation aoqi@0: // and before the Store is using the memory location that the store aoqi@0: // overwrites. aoqi@0: bool failed = false; aoqi@0: // If is_complete_with_arraycopy() is true the shape of the graph is aoqi@0: // well defined and is safe so no need for extra checks. aoqi@0: if (!is_complete_with_arraycopy()) { aoqi@0: // We are going to look at each use of the memory state following aoqi@0: // the allocation to make sure nothing reads the memory that the aoqi@0: // Store writes. aoqi@0: const TypePtr* t_adr = phase->type(adr)->isa_ptr(); aoqi@0: int alias_idx = phase->C->get_alias_index(t_adr); aoqi@0: ResourceMark rm; aoqi@0: Unique_Node_List mems; aoqi@0: mems.push(mem); aoqi@0: Node* unique_merge = NULL; aoqi@0: for (uint next = 0; next < mems.size(); ++next) { aoqi@0: Node *m = mems.at(next); aoqi@0: for (DUIterator_Fast jmax, j = m->fast_outs(jmax); j < jmax; j++) { aoqi@0: Node *n = m->fast_out(j); aoqi@0: if (n->outcnt() == 0) { aoqi@0: continue; aoqi@0: } aoqi@0: if (n == st) { aoqi@0: continue; aoqi@0: } else if (n->in(0) != NULL && n->in(0) != ctl) { aoqi@0: // If the control of this use is different from the control aoqi@0: // of the Store which is right after the InitializeNode then aoqi@0: // this node cannot be between the InitializeNode and the aoqi@0: // Store. aoqi@0: continue; aoqi@0: } else if (n->is_MergeMem()) { aoqi@0: if (n->as_MergeMem()->memory_at(alias_idx) == m) { aoqi@0: // We can hit a MergeMemNode (that will likely go away aoqi@0: // later) that is a direct use of the memory state aoqi@0: // following the InitializeNode on the same slice as the aoqi@0: // store node that we'd like to capture. We need to check aoqi@0: // the uses of the MergeMemNode. aoqi@0: mems.push(n); aoqi@0: } aoqi@0: } else if (n->is_Mem()) { aoqi@0: Node* other_adr = n->in(MemNode::Address); aoqi@0: if (other_adr == adr) { aoqi@0: failed = true; aoqi@0: break; aoqi@0: } else { aoqi@0: const TypePtr* other_t_adr = phase->type(other_adr)->isa_ptr(); aoqi@0: if (other_t_adr != NULL) { aoqi@0: int other_alias_idx = phase->C->get_alias_index(other_t_adr); aoqi@0: if (other_alias_idx == alias_idx) { aoqi@0: // A load from the same memory slice as the store right aoqi@0: // after the InitializeNode. We check the control of the aoqi@0: // object/array that is loaded from. If it's the same as aoqi@0: // the store control then we cannot capture the store. aoqi@0: assert(!n->is_Store(), "2 stores to same slice on same control?"); aoqi@0: Node* base = other_adr; aoqi@0: assert(base->is_AddP(), err_msg_res("should be addp but is %s", base->Name())); aoqi@0: base = base->in(AddPNode::Base); aoqi@0: if (base != NULL) { aoqi@0: base = base->uncast(); aoqi@0: if (base->is_Proj() && base->in(0) == alloc) { aoqi@0: failed = true; aoqi@0: break; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } else { aoqi@0: failed = true; aoqi@0: break; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: if (failed) { aoqi@0: if (!can_reshape) { aoqi@0: // We decided we couldn't capture the store during parsing. We aoqi@0: // should try again during the next IGVN once the graph is aoqi@0: // cleaner. aoqi@0: phase->C->record_for_igvn(st); aoqi@0: } aoqi@0: return FAIL; aoqi@0: } aoqi@0: aoqi@0: return offset; // success aoqi@0: } aoqi@0: aoqi@0: // Find the captured store in(i) which corresponds to the range aoqi@0: // [start..start+size) in the initialized object. aoqi@0: // If there is one, return its index i. If there isn't, return the aoqi@0: // negative of the index where it should be inserted. aoqi@0: // Return 0 if the queried range overlaps an initialization boundary aoqi@0: // or if dead code is encountered. aoqi@0: // If size_in_bytes is zero, do not bother with overlap checks. aoqi@0: int InitializeNode::captured_store_insertion_point(intptr_t start, aoqi@0: int size_in_bytes, aoqi@0: PhaseTransform* phase) { aoqi@0: const int FAIL = 0, MAX_STORE = BytesPerLong; aoqi@0: aoqi@0: if (is_complete()) aoqi@0: return FAIL; // arraycopy got here first; punt aoqi@0: aoqi@0: assert(allocation() != NULL, "must be present"); aoqi@0: aoqi@0: // no negatives, no header fields: aoqi@0: if (start < (intptr_t) allocation()->minimum_header_size()) return FAIL; aoqi@0: aoqi@0: // after a certain size, we bail out on tracking all the stores: aoqi@0: intptr_t ti_limit = (TrackedInitializationLimit * HeapWordSize); aoqi@0: if (start >= ti_limit) return FAIL; aoqi@0: aoqi@0: for (uint i = InitializeNode::RawStores, limit = req(); ; ) { aoqi@0: if (i >= limit) return -(int)i; // not found; here is where to put it aoqi@0: aoqi@0: Node* st = in(i); aoqi@0: intptr_t st_off = get_store_offset(st, phase); aoqi@0: if (st_off < 0) { aoqi@0: if (st != zero_memory()) { aoqi@0: return FAIL; // bail out if there is dead garbage aoqi@0: } aoqi@0: } else if (st_off > start) { aoqi@0: // ...we are done, since stores are ordered aoqi@0: if (st_off < start + size_in_bytes) { aoqi@0: return FAIL; // the next store overlaps aoqi@0: } aoqi@0: return -(int)i; // not found; here is where to put it aoqi@0: } else if (st_off < start) { aoqi@0: if (size_in_bytes != 0 && aoqi@0: start < st_off + MAX_STORE && aoqi@0: start < st_off + st->as_Store()->memory_size()) { aoqi@0: return FAIL; // the previous store overlaps aoqi@0: } aoqi@0: } else { aoqi@0: if (size_in_bytes != 0 && aoqi@0: st->as_Store()->memory_size() != size_in_bytes) { aoqi@0: return FAIL; // mismatched store size aoqi@0: } aoqi@0: return i; aoqi@0: } aoqi@0: aoqi@0: ++i; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Look for a captured store which initializes at the offset 'start' aoqi@0: // with the given size. If there is no such store, and no other aoqi@0: // initialization interferes, then return zero_memory (the memory aoqi@0: // projection of the AllocateNode). aoqi@0: Node* InitializeNode::find_captured_store(intptr_t start, int size_in_bytes, aoqi@0: PhaseTransform* phase) { aoqi@0: assert(stores_are_sane(phase), ""); aoqi@0: int i = captured_store_insertion_point(start, size_in_bytes, phase); aoqi@0: if (i == 0) { aoqi@0: return NULL; // something is dead aoqi@0: } else if (i < 0) { aoqi@0: return zero_memory(); // just primordial zero bits here aoqi@0: } else { aoqi@0: Node* st = in(i); // here is the store at this position aoqi@0: assert(get_store_offset(st->as_Store(), phase) == start, "sanity"); aoqi@0: return st; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Create, as a raw pointer, an address within my new object at 'offset'. aoqi@0: Node* InitializeNode::make_raw_address(intptr_t offset, aoqi@0: PhaseTransform* phase) { aoqi@0: Node* addr = in(RawAddress); aoqi@0: if (offset != 0) { aoqi@0: Compile* C = phase->C; aoqi@0: addr = phase->transform( new (C) AddPNode(C->top(), addr, aoqi@0: phase->MakeConX(offset)) ); aoqi@0: } aoqi@0: return addr; aoqi@0: } aoqi@0: aoqi@0: // Clone the given store, converting it into a raw store aoqi@0: // initializing a field or element of my new object. aoqi@0: // Caller is responsible for retiring the original store, aoqi@0: // with subsume_node or the like. aoqi@0: // aoqi@0: // From the example above InitializeNode::InitializeNode, aoqi@0: // here are the old stores to be captured: aoqi@0: // store1 = (StoreC init.Control init.Memory (+ oop 12) 1) aoqi@0: // store2 = (StoreC init.Control store1 (+ oop 14) 2) aoqi@0: // aoqi@0: // Here is the changed code; note the extra edges on init: aoqi@0: // alloc = (Allocate ...) aoqi@0: // rawoop = alloc.RawAddress aoqi@0: // rawstore1 = (StoreC alloc.Control alloc.Memory (+ rawoop 12) 1) aoqi@0: // rawstore2 = (StoreC alloc.Control alloc.Memory (+ rawoop 14) 2) aoqi@0: // init = (Initialize alloc.Control alloc.Memory rawoop aoqi@0: // rawstore1 rawstore2) aoqi@0: // aoqi@0: Node* InitializeNode::capture_store(StoreNode* st, intptr_t start, aoqi@0: PhaseTransform* phase, bool can_reshape) { aoqi@0: assert(stores_are_sane(phase), ""); aoqi@0: aoqi@0: if (start < 0) return NULL; aoqi@0: assert(can_capture_store(st, phase, can_reshape) == start, "sanity"); aoqi@0: aoqi@0: Compile* C = phase->C; aoqi@0: int size_in_bytes = st->memory_size(); aoqi@0: int i = captured_store_insertion_point(start, size_in_bytes, phase); aoqi@0: if (i == 0) return NULL; // bail out aoqi@0: Node* prev_mem = NULL; // raw memory for the captured store aoqi@0: if (i > 0) { aoqi@0: prev_mem = in(i); // there is a pre-existing store under this one aoqi@0: set_req(i, C->top()); // temporarily disconnect it aoqi@0: // See StoreNode::Ideal 'st->outcnt() == 1' for the reason to disconnect. aoqi@0: } else { aoqi@0: i = -i; // no pre-existing store aoqi@0: prev_mem = zero_memory(); // a slice of the newly allocated object aoqi@0: if (i > InitializeNode::RawStores && in(i-1) == prev_mem) aoqi@0: set_req(--i, C->top()); // reuse this edge; it has been folded away aoqi@0: else aoqi@0: ins_req(i, C->top()); // build a new edge aoqi@0: } aoqi@0: Node* new_st = st->clone(); aoqi@0: new_st->set_req(MemNode::Control, in(Control)); aoqi@0: new_st->set_req(MemNode::Memory, prev_mem); aoqi@0: new_st->set_req(MemNode::Address, make_raw_address(start, phase)); aoqi@0: new_st = phase->transform(new_st); aoqi@0: aoqi@0: // At this point, new_st might have swallowed a pre-existing store aoqi@0: // at the same offset, or perhaps new_st might have disappeared, aoqi@0: // if it redundantly stored the same value (or zero to fresh memory). aoqi@0: aoqi@0: // In any case, wire it in: aoqi@0: set_req(i, new_st); aoqi@0: aoqi@0: // The caller may now kill the old guy. aoqi@0: DEBUG_ONLY(Node* check_st = find_captured_store(start, size_in_bytes, phase)); aoqi@0: assert(check_st == new_st || check_st == NULL, "must be findable"); aoqi@0: assert(!is_complete(), ""); aoqi@0: return new_st; aoqi@0: } aoqi@0: aoqi@0: static bool store_constant(jlong* tiles, int num_tiles, aoqi@0: intptr_t st_off, int st_size, aoqi@0: jlong con) { aoqi@0: if ((st_off & (st_size-1)) != 0) aoqi@0: return false; // strange store offset (assume size==2**N) aoqi@0: address addr = (address)tiles + st_off; aoqi@0: assert(st_off >= 0 && addr+st_size <= (address)&tiles[num_tiles], "oob"); aoqi@0: switch (st_size) { aoqi@0: case sizeof(jbyte): *(jbyte*) addr = (jbyte) con; break; aoqi@0: case sizeof(jchar): *(jchar*) addr = (jchar) con; break; aoqi@0: case sizeof(jint): *(jint*) addr = (jint) con; break; aoqi@0: case sizeof(jlong): *(jlong*) addr = (jlong) con; break; aoqi@0: default: return false; // strange store size (detect size!=2**N here) aoqi@0: } aoqi@0: return true; // return success to caller aoqi@0: } aoqi@0: aoqi@0: // Coalesce subword constants into int constants and possibly aoqi@0: // into long constants. The goal, if the CPU permits, aoqi@0: // is to initialize the object with a small number of 64-bit tiles. aoqi@0: // Also, convert floating-point constants to bit patterns. aoqi@0: // Non-constants are not relevant to this pass. aoqi@0: // aoqi@0: // In terms of the running example on InitializeNode::InitializeNode aoqi@0: // and InitializeNode::capture_store, here is the transformation aoqi@0: // of rawstore1 and rawstore2 into rawstore12: aoqi@0: // alloc = (Allocate ...) aoqi@0: // rawoop = alloc.RawAddress aoqi@0: // tile12 = 0x00010002 aoqi@0: // rawstore12 = (StoreI alloc.Control alloc.Memory (+ rawoop 12) tile12) aoqi@0: // init = (Initialize alloc.Control alloc.Memory rawoop rawstore12) aoqi@0: // aoqi@0: void aoqi@0: InitializeNode::coalesce_subword_stores(intptr_t header_size, aoqi@0: Node* size_in_bytes, aoqi@0: PhaseGVN* phase) { aoqi@0: Compile* C = phase->C; aoqi@0: aoqi@0: assert(stores_are_sane(phase), ""); aoqi@0: // Note: After this pass, they are not completely sane, aoqi@0: // since there may be some overlaps. aoqi@0: aoqi@0: int old_subword = 0, old_long = 0, new_int = 0, new_long = 0; aoqi@0: aoqi@0: intptr_t ti_limit = (TrackedInitializationLimit * HeapWordSize); aoqi@0: intptr_t size_limit = phase->find_intptr_t_con(size_in_bytes, ti_limit); aoqi@0: size_limit = MIN2(size_limit, ti_limit); aoqi@0: size_limit = align_size_up(size_limit, BytesPerLong); aoqi@0: int num_tiles = size_limit / BytesPerLong; aoqi@0: aoqi@0: // allocate space for the tile map: aoqi@0: const int small_len = DEBUG_ONLY(true ? 3 :) 30; // keep stack frames small aoqi@0: jlong tiles_buf[small_len]; aoqi@0: Node* nodes_buf[small_len]; aoqi@0: jlong inits_buf[small_len]; aoqi@0: jlong* tiles = ((num_tiles <= small_len) ? &tiles_buf[0] aoqi@0: : NEW_RESOURCE_ARRAY(jlong, num_tiles)); aoqi@0: Node** nodes = ((num_tiles <= small_len) ? &nodes_buf[0] aoqi@0: : NEW_RESOURCE_ARRAY(Node*, num_tiles)); aoqi@0: jlong* inits = ((num_tiles <= small_len) ? &inits_buf[0] aoqi@0: : NEW_RESOURCE_ARRAY(jlong, num_tiles)); aoqi@0: // tiles: exact bitwise model of all primitive constants aoqi@0: // nodes: last constant-storing node subsumed into the tiles model aoqi@0: // inits: which bytes (in each tile) are touched by any initializations aoqi@0: aoqi@0: //// Pass A: Fill in the tile model with any relevant stores. aoqi@0: aoqi@0: Copy::zero_to_bytes(tiles, sizeof(tiles[0]) * num_tiles); aoqi@0: Copy::zero_to_bytes(nodes, sizeof(nodes[0]) * num_tiles); aoqi@0: Copy::zero_to_bytes(inits, sizeof(inits[0]) * num_tiles); aoqi@0: Node* zmem = zero_memory(); // initially zero memory state aoqi@0: for (uint i = InitializeNode::RawStores, limit = req(); i < limit; i++) { aoqi@0: Node* st = in(i); aoqi@0: intptr_t st_off = get_store_offset(st, phase); aoqi@0: aoqi@0: // Figure out the store's offset and constant value: aoqi@0: if (st_off < header_size) continue; //skip (ignore header) aoqi@0: if (st->in(MemNode::Memory) != zmem) continue; //skip (odd store chain) aoqi@0: int st_size = st->as_Store()->memory_size(); aoqi@0: if (st_off + st_size > size_limit) break; aoqi@0: aoqi@0: // Record which bytes are touched, whether by constant or not. aoqi@0: if (!store_constant(inits, num_tiles, st_off, st_size, (jlong) -1)) aoqi@0: continue; // skip (strange store size) aoqi@0: aoqi@0: const Type* val = phase->type(st->in(MemNode::ValueIn)); aoqi@0: if (!val->singleton()) continue; //skip (non-con store) aoqi@0: BasicType type = val->basic_type(); aoqi@0: aoqi@0: jlong con = 0; aoqi@0: switch (type) { aoqi@0: case T_INT: con = val->is_int()->get_con(); break; aoqi@0: case T_LONG: con = val->is_long()->get_con(); break; aoqi@0: case T_FLOAT: con = jint_cast(val->getf()); break; aoqi@0: case T_DOUBLE: con = jlong_cast(val->getd()); break; aoqi@0: default: continue; //skip (odd store type) aoqi@0: } aoqi@0: aoqi@0: if (type == T_LONG && Matcher::isSimpleConstant64(con) && aoqi@0: st->Opcode() == Op_StoreL) { aoqi@0: continue; // This StoreL is already optimal. aoqi@0: } aoqi@0: aoqi@0: // Store down the constant. aoqi@0: store_constant(tiles, num_tiles, st_off, st_size, con); aoqi@0: aoqi@0: intptr_t j = st_off >> LogBytesPerLong; aoqi@0: aoqi@0: if (type == T_INT && st_size == BytesPerInt aoqi@0: && (st_off & BytesPerInt) == BytesPerInt) { aoqi@0: jlong lcon = tiles[j]; aoqi@0: if (!Matcher::isSimpleConstant64(lcon) && aoqi@0: st->Opcode() == Op_StoreI) { aoqi@0: // This StoreI is already optimal by itself. aoqi@0: jint* intcon = (jint*) &tiles[j]; aoqi@0: intcon[1] = 0; // undo the store_constant() aoqi@0: aoqi@0: // If the previous store is also optimal by itself, back up and aoqi@0: // undo the action of the previous loop iteration... if we can. aoqi@0: // But if we can't, just let the previous half take care of itself. aoqi@0: st = nodes[j]; aoqi@0: st_off -= BytesPerInt; aoqi@0: con = intcon[0]; aoqi@0: if (con != 0 && st != NULL && st->Opcode() == Op_StoreI) { aoqi@0: assert(st_off >= header_size, "still ignoring header"); aoqi@0: assert(get_store_offset(st, phase) == st_off, "must be"); aoqi@0: assert(in(i-1) == zmem, "must be"); aoqi@0: DEBUG_ONLY(const Type* tcon = phase->type(st->in(MemNode::ValueIn))); aoqi@0: assert(con == tcon->is_int()->get_con(), "must be"); aoqi@0: // Undo the effects of the previous loop trip, which swallowed st: aoqi@0: intcon[0] = 0; // undo store_constant() aoqi@0: set_req(i-1, st); // undo set_req(i, zmem) aoqi@0: nodes[j] = NULL; // undo nodes[j] = st aoqi@0: --old_subword; // undo ++old_subword aoqi@0: } aoqi@0: continue; // This StoreI is already optimal. aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // This store is not needed. aoqi@0: set_req(i, zmem); aoqi@0: nodes[j] = st; // record for the moment aoqi@0: if (st_size < BytesPerLong) // something has changed aoqi@0: ++old_subword; // includes int/float, but who's counting... aoqi@0: else ++old_long; aoqi@0: } aoqi@0: aoqi@0: if ((old_subword + old_long) == 0) aoqi@0: return; // nothing more to do aoqi@0: aoqi@0: //// Pass B: Convert any non-zero tiles into optimal constant stores. aoqi@0: // Be sure to insert them before overlapping non-constant stores. aoqi@0: // (E.g., byte[] x = { 1,2,y,4 } => x[int 0] = 0x01020004, x[2]=y.) aoqi@0: for (int j = 0; j < num_tiles; j++) { aoqi@0: jlong con = tiles[j]; aoqi@0: jlong init = inits[j]; aoqi@0: if (con == 0) continue; aoqi@0: jint con0, con1; // split the constant, address-wise aoqi@0: jint init0, init1; // split the init map, address-wise aoqi@0: { union { jlong con; jint intcon[2]; } u; aoqi@0: u.con = con; aoqi@0: con0 = u.intcon[0]; aoqi@0: con1 = u.intcon[1]; aoqi@0: u.con = init; aoqi@0: init0 = u.intcon[0]; aoqi@0: init1 = u.intcon[1]; aoqi@0: } aoqi@0: aoqi@0: Node* old = nodes[j]; aoqi@0: assert(old != NULL, "need the prior store"); aoqi@0: intptr_t offset = (j * BytesPerLong); aoqi@0: aoqi@0: bool split = !Matcher::isSimpleConstant64(con); aoqi@0: aoqi@0: if (offset < header_size) { aoqi@0: assert(offset + BytesPerInt >= header_size, "second int counts"); aoqi@0: assert(*(jint*)&tiles[j] == 0, "junk in header"); aoqi@0: split = true; // only the second word counts aoqi@0: // Example: int a[] = { 42 ... } aoqi@0: } else if (con0 == 0 && init0 == -1) { aoqi@0: split = true; // first word is covered by full inits aoqi@0: // Example: int a[] = { ... foo(), 42 ... } aoqi@0: } else if (con1 == 0 && init1 == -1) { aoqi@0: split = true; // second word is covered by full inits aoqi@0: // Example: int a[] = { ... 42, foo() ... } aoqi@0: } aoqi@0: aoqi@0: // Here's a case where init0 is neither 0 nor -1: aoqi@0: // byte a[] = { ... 0,0,foo(),0, 0,0,0,42 ... } aoqi@0: // Assuming big-endian memory, init0, init1 are 0x0000FF00, 0x000000FF. aoqi@0: // In this case the tile is not split; it is (jlong)42. aoqi@0: // The big tile is stored down, and then the foo() value is inserted. aoqi@0: // (If there were foo(),foo() instead of foo(),0, init0 would be -1.) aoqi@0: aoqi@0: Node* ctl = old->in(MemNode::Control); aoqi@0: Node* adr = make_raw_address(offset, phase); aoqi@0: const TypePtr* atp = TypeRawPtr::BOTTOM; aoqi@0: aoqi@0: // One or two coalesced stores to plop down. aoqi@0: Node* st[2]; aoqi@0: intptr_t off[2]; aoqi@0: int nst = 0; aoqi@0: if (!split) { aoqi@0: ++new_long; aoqi@0: off[nst] = offset; aoqi@0: st[nst++] = StoreNode::make(*phase, ctl, zmem, adr, atp, aoqi@0: phase->longcon(con), T_LONG, MemNode::unordered); aoqi@0: } else { aoqi@0: // Omit either if it is a zero. aoqi@0: if (con0 != 0) { aoqi@0: ++new_int; aoqi@0: off[nst] = offset; aoqi@0: st[nst++] = StoreNode::make(*phase, ctl, zmem, adr, atp, aoqi@0: phase->intcon(con0), T_INT, MemNode::unordered); aoqi@0: } aoqi@0: if (con1 != 0) { aoqi@0: ++new_int; aoqi@0: offset += BytesPerInt; aoqi@0: adr = make_raw_address(offset, phase); aoqi@0: off[nst] = offset; aoqi@0: st[nst++] = StoreNode::make(*phase, ctl, zmem, adr, atp, aoqi@0: phase->intcon(con1), T_INT, MemNode::unordered); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Insert second store first, then the first before the second. aoqi@0: // Insert each one just before any overlapping non-constant stores. aoqi@0: while (nst > 0) { aoqi@0: Node* st1 = st[--nst]; aoqi@0: C->copy_node_notes_to(st1, old); aoqi@0: st1 = phase->transform(st1); aoqi@0: offset = off[nst]; aoqi@0: assert(offset >= header_size, "do not smash header"); aoqi@0: int ins_idx = captured_store_insertion_point(offset, /*size:*/0, phase); aoqi@0: guarantee(ins_idx != 0, "must re-insert constant store"); aoqi@0: if (ins_idx < 0) ins_idx = -ins_idx; // never overlap aoqi@0: if (ins_idx > InitializeNode::RawStores && in(ins_idx-1) == zmem) aoqi@0: set_req(--ins_idx, st1); aoqi@0: else aoqi@0: ins_req(ins_idx, st1); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: if (PrintCompilation && WizardMode) aoqi@0: tty->print_cr("Changed %d/%d subword/long constants into %d/%d int/long", aoqi@0: old_subword, old_long, new_int, new_long); aoqi@0: if (C->log() != NULL) aoqi@0: C->log()->elem("comment that='%d/%d subword/long to %d/%d int/long'", aoqi@0: old_subword, old_long, new_int, new_long); aoqi@0: aoqi@0: // Clean up any remaining occurrences of zmem: aoqi@0: remove_extra_zeroes(); aoqi@0: } aoqi@0: aoqi@0: // Explore forward from in(start) to find the first fully initialized aoqi@0: // word, and return its offset. Skip groups of subword stores which aoqi@0: // together initialize full words. If in(start) is itself part of a aoqi@0: // fully initialized word, return the offset of in(start). If there aoqi@0: // are no following full-word stores, or if something is fishy, return aoqi@0: // a negative value. aoqi@0: intptr_t InitializeNode::find_next_fullword_store(uint start, PhaseGVN* phase) { aoqi@0: int int_map = 0; aoqi@0: intptr_t int_map_off = 0; aoqi@0: const int FULL_MAP = right_n_bits(BytesPerInt); // the int_map we hope for aoqi@0: aoqi@0: for (uint i = start, limit = req(); i < limit; i++) { aoqi@0: Node* st = in(i); aoqi@0: aoqi@0: intptr_t st_off = get_store_offset(st, phase); aoqi@0: if (st_off < 0) break; // return conservative answer aoqi@0: aoqi@0: int st_size = st->as_Store()->memory_size(); aoqi@0: if (st_size >= BytesPerInt && (st_off % BytesPerInt) == 0) { aoqi@0: return st_off; // we found a complete word init aoqi@0: } aoqi@0: aoqi@0: // update the map: aoqi@0: aoqi@0: intptr_t this_int_off = align_size_down(st_off, BytesPerInt); aoqi@0: if (this_int_off != int_map_off) { aoqi@0: // reset the map: aoqi@0: int_map = 0; aoqi@0: int_map_off = this_int_off; aoqi@0: } aoqi@0: aoqi@0: int subword_off = st_off - this_int_off; aoqi@0: int_map |= right_n_bits(st_size) << subword_off; aoqi@0: if ((int_map & FULL_MAP) == FULL_MAP) { aoqi@0: return this_int_off; // we found a complete word init aoqi@0: } aoqi@0: aoqi@0: // Did this store hit or cross the word boundary? aoqi@0: intptr_t next_int_off = align_size_down(st_off + st_size, BytesPerInt); aoqi@0: if (next_int_off == this_int_off + BytesPerInt) { aoqi@0: // We passed the current int, without fully initializing it. aoqi@0: int_map_off = next_int_off; aoqi@0: int_map >>= BytesPerInt; aoqi@0: } else if (next_int_off > this_int_off + BytesPerInt) { aoqi@0: // We passed the current and next int. aoqi@0: return this_int_off + BytesPerInt; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: return -1; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // Called when the associated AllocateNode is expanded into CFG. aoqi@0: // At this point, we may perform additional optimizations. aoqi@0: // Linearize the stores by ascending offset, to make memory aoqi@0: // activity as coherent as possible. aoqi@0: Node* InitializeNode::complete_stores(Node* rawctl, Node* rawmem, Node* rawptr, aoqi@0: intptr_t header_size, aoqi@0: Node* size_in_bytes, aoqi@0: PhaseGVN* phase) { aoqi@0: assert(!is_complete(), "not already complete"); aoqi@0: assert(stores_are_sane(phase), ""); aoqi@0: assert(allocation() != NULL, "must be present"); aoqi@0: aoqi@0: remove_extra_zeroes(); aoqi@0: aoqi@0: if (ReduceFieldZeroing || ReduceBulkZeroing) aoqi@0: // reduce instruction count for common initialization patterns aoqi@0: coalesce_subword_stores(header_size, size_in_bytes, phase); aoqi@0: aoqi@0: Node* zmem = zero_memory(); // initially zero memory state aoqi@0: Node* inits = zmem; // accumulating a linearized chain of inits aoqi@0: #ifdef ASSERT aoqi@0: intptr_t first_offset = allocation()->minimum_header_size(); aoqi@0: intptr_t last_init_off = first_offset; // previous init offset aoqi@0: intptr_t last_init_end = first_offset; // previous init offset+size aoqi@0: intptr_t last_tile_end = first_offset; // previous tile offset+size aoqi@0: #endif aoqi@0: intptr_t zeroes_done = header_size; aoqi@0: aoqi@0: bool do_zeroing = true; // we might give up if inits are very sparse aoqi@0: int big_init_gaps = 0; // how many large gaps have we seen? aoqi@0: aoqi@0: if (ZeroTLAB) do_zeroing = false; aoqi@0: if (!ReduceFieldZeroing && !ReduceBulkZeroing) do_zeroing = false; aoqi@0: aoqi@0: for (uint i = InitializeNode::RawStores, limit = req(); i < limit; i++) { aoqi@0: Node* st = in(i); aoqi@0: intptr_t st_off = get_store_offset(st, phase); aoqi@0: if (st_off < 0) aoqi@0: break; // unknown junk in the inits aoqi@0: if (st->in(MemNode::Memory) != zmem) aoqi@0: break; // complicated store chains somehow in list aoqi@0: aoqi@0: int st_size = st->as_Store()->memory_size(); aoqi@0: intptr_t next_init_off = st_off + st_size; aoqi@0: aoqi@0: if (do_zeroing && zeroes_done < next_init_off) { aoqi@0: // See if this store needs a zero before it or under it. aoqi@0: intptr_t zeroes_needed = st_off; aoqi@0: aoqi@0: if (st_size < BytesPerInt) { aoqi@0: // Look for subword stores which only partially initialize words. aoqi@0: // If we find some, we must lay down some word-level zeroes first, aoqi@0: // underneath the subword stores. aoqi@0: // aoqi@0: // Examples: aoqi@0: // byte[] a = { p,q,r,s } => a[0]=p,a[1]=q,a[2]=r,a[3]=s aoqi@0: // byte[] a = { x,y,0,0 } => a[0..3] = 0, a[0]=x,a[1]=y aoqi@0: // byte[] a = { 0,0,z,0 } => a[0..3] = 0, a[2]=z aoqi@0: // aoqi@0: // Note: coalesce_subword_stores may have already done this, aoqi@0: // if it was prompted by constant non-zero subword initializers. aoqi@0: // But this case can still arise with non-constant stores. aoqi@0: aoqi@0: intptr_t next_full_store = find_next_fullword_store(i, phase); aoqi@0: aoqi@0: // In the examples above: aoqi@0: // in(i) p q r s x y z aoqi@0: // st_off 12 13 14 15 12 13 14 aoqi@0: // st_size 1 1 1 1 1 1 1 aoqi@0: // next_full_s. 12 16 16 16 16 16 16 aoqi@0: // z's_done 12 16 16 16 12 16 12 aoqi@0: // z's_needed 12 16 16 16 16 16 16 aoqi@0: // zsize 0 0 0 0 4 0 4 aoqi@0: if (next_full_store < 0) { aoqi@0: // Conservative tack: Zero to end of current word. aoqi@0: zeroes_needed = align_size_up(zeroes_needed, BytesPerInt); aoqi@0: } else { aoqi@0: // Zero to beginning of next fully initialized word. aoqi@0: // Or, don't zero at all, if we are already in that word. aoqi@0: assert(next_full_store >= zeroes_needed, "must go forward"); aoqi@0: assert((next_full_store & (BytesPerInt-1)) == 0, "even boundary"); aoqi@0: zeroes_needed = next_full_store; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: if (zeroes_needed > zeroes_done) { aoqi@0: intptr_t zsize = zeroes_needed - zeroes_done; aoqi@0: // Do some incremental zeroing on rawmem, in parallel with inits. aoqi@0: zeroes_done = align_size_down(zeroes_done, BytesPerInt); aoqi@0: rawmem = ClearArrayNode::clear_memory(rawctl, rawmem, rawptr, aoqi@0: zeroes_done, zeroes_needed, aoqi@0: phase); aoqi@0: zeroes_done = zeroes_needed; aoqi@0: if (zsize > Matcher::init_array_short_size && ++big_init_gaps > 2) aoqi@0: do_zeroing = false; // leave the hole, next time aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Collect the store and move on: aoqi@0: st->set_req(MemNode::Memory, inits); aoqi@0: inits = st; // put it on the linearized chain aoqi@0: set_req(i, zmem); // unhook from previous position aoqi@0: aoqi@0: if (zeroes_done == st_off) aoqi@0: zeroes_done = next_init_off; aoqi@0: aoqi@0: assert(!do_zeroing || zeroes_done >= next_init_off, "don't miss any"); aoqi@0: aoqi@0: #ifdef ASSERT aoqi@0: // Various order invariants. Weaker than stores_are_sane because aoqi@0: // a large constant tile can be filled in by smaller non-constant stores. aoqi@0: assert(st_off >= last_init_off, "inits do not reverse"); aoqi@0: last_init_off = st_off; aoqi@0: const Type* val = NULL; aoqi@0: if (st_size >= BytesPerInt && aoqi@0: (val = phase->type(st->in(MemNode::ValueIn)))->singleton() && aoqi@0: (int)val->basic_type() < (int)T_OBJECT) { aoqi@0: assert(st_off >= last_tile_end, "tiles do not overlap"); aoqi@0: assert(st_off >= last_init_end, "tiles do not overwrite inits"); aoqi@0: last_tile_end = MAX2(last_tile_end, next_init_off); aoqi@0: } else { aoqi@0: intptr_t st_tile_end = align_size_up(next_init_off, BytesPerLong); aoqi@0: assert(st_tile_end >= last_tile_end, "inits stay with tiles"); aoqi@0: assert(st_off >= last_init_end, "inits do not overlap"); aoqi@0: last_init_end = next_init_off; // it's a non-tile aoqi@0: } aoqi@0: #endif //ASSERT aoqi@0: } aoqi@0: aoqi@0: remove_extra_zeroes(); // clear out all the zmems left over aoqi@0: add_req(inits); aoqi@0: aoqi@0: if (!ZeroTLAB) { aoqi@0: // If anything remains to be zeroed, zero it all now. aoqi@0: zeroes_done = align_size_down(zeroes_done, BytesPerInt); aoqi@0: // if it is the last unused 4 bytes of an instance, forget about it aoqi@0: intptr_t size_limit = phase->find_intptr_t_con(size_in_bytes, max_jint); aoqi@0: if (zeroes_done + BytesPerLong >= size_limit) { aoqi@0: assert(allocation() != NULL, ""); aoqi@0: if (allocation()->Opcode() == Op_Allocate) { aoqi@0: Node* klass_node = allocation()->in(AllocateNode::KlassNode); aoqi@0: ciKlass* k = phase->type(klass_node)->is_klassptr()->klass(); aoqi@0: if (zeroes_done == k->layout_helper()) aoqi@0: zeroes_done = size_limit; aoqi@0: } aoqi@0: } aoqi@0: if (zeroes_done < size_limit) { aoqi@0: rawmem = ClearArrayNode::clear_memory(rawctl, rawmem, rawptr, aoqi@0: zeroes_done, size_in_bytes, phase); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: set_complete(phase); aoqi@0: return rawmem; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: #ifdef ASSERT aoqi@0: bool InitializeNode::stores_are_sane(PhaseTransform* phase) { aoqi@0: if (is_complete()) aoqi@0: return true; // stores could be anything at this point aoqi@0: assert(allocation() != NULL, "must be present"); aoqi@0: intptr_t last_off = allocation()->minimum_header_size(); aoqi@0: for (uint i = InitializeNode::RawStores; i < req(); i++) { aoqi@0: Node* st = in(i); aoqi@0: intptr_t st_off = get_store_offset(st, phase); aoqi@0: if (st_off < 0) continue; // ignore dead garbage aoqi@0: if (last_off > st_off) { aoqi@0: tty->print_cr("*** bad store offset at %d: " INTX_FORMAT " > " INTX_FORMAT, i, last_off, st_off); aoqi@0: this->dump(2); aoqi@0: assert(false, "ascending store offsets"); aoqi@0: return false; aoqi@0: } aoqi@0: last_off = st_off + st->as_Store()->memory_size(); aoqi@0: } aoqi@0: return true; aoqi@0: } aoqi@0: #endif //ASSERT aoqi@0: aoqi@0: aoqi@0: aoqi@0: aoqi@0: //============================MergeMemNode===================================== aoqi@0: // aoqi@0: // SEMANTICS OF MEMORY MERGES: A MergeMem is a memory state assembled from several aoqi@0: // contributing store or call operations. Each contributor provides the memory aoqi@0: // state for a particular "alias type" (see Compile::alias_type). For example, aoqi@0: // if a MergeMem has an input X for alias category #6, then any memory reference aoqi@0: // to alias category #6 may use X as its memory state input, as an exact equivalent aoqi@0: // to using the MergeMem as a whole. aoqi@0: // Load<6>( MergeMem(<6>: X, ...), p ) <==> Load<6>(X,p) aoqi@0: // aoqi@0: // (Here, the notation gives the index of the relevant adr_type.) aoqi@0: // aoqi@0: // In one special case (and more cases in the future), alias categories overlap. aoqi@0: // The special alias category "Bot" (Compile::AliasIdxBot) includes all memory aoqi@0: // states. Therefore, if a MergeMem has only one contributing input W for Bot, aoqi@0: // it is exactly equivalent to that state W: aoqi@0: // MergeMem(: W) <==> W aoqi@0: // aoqi@0: // Usually, the merge has more than one input. In that case, where inputs aoqi@0: // overlap (i.e., one is Bot), the narrower alias type determines the memory aoqi@0: // state for that type, and the wider alias type (Bot) fills in everywhere else: aoqi@0: // Load<5>( MergeMem(: W, <6>: X), p ) <==> Load<5>(W,p) aoqi@0: // Load<6>( MergeMem(: W, <6>: X), p ) <==> Load<6>(X,p) aoqi@0: // aoqi@0: // A merge can take a "wide" memory state as one of its narrow inputs. aoqi@0: // This simply means that the merge observes out only the relevant parts of aoqi@0: // the wide input. That is, wide memory states arriving at narrow merge inputs aoqi@0: // are implicitly "filtered" or "sliced" as necessary. (This is rare.) aoqi@0: // aoqi@0: // These rules imply that MergeMem nodes may cascade (via their links), aoqi@0: // and that memory slices "leak through": aoqi@0: // MergeMem(: MergeMem(: W, <7>: Y)) <==> MergeMem(: W, <7>: Y) aoqi@0: // aoqi@0: // But, in such a cascade, repeated memory slices can "block the leak": aoqi@0: // MergeMem(: MergeMem(: W, <7>: Y), <7>: Y') <==> MergeMem(: W, <7>: Y') aoqi@0: // aoqi@0: // In the last example, Y is not part of the combined memory state of the aoqi@0: // outermost MergeMem. The system must, of course, prevent unschedulable aoqi@0: // memory states from arising, so you can be sure that the state Y is somehow aoqi@0: // a precursor to state Y'. aoqi@0: // aoqi@0: // aoqi@0: // REPRESENTATION OF MEMORY MERGES: The indexes used to address the Node::in array aoqi@0: // of each MergeMemNode array are exactly the numerical alias indexes, including aoqi@0: // but not limited to AliasIdxTop, AliasIdxBot, and AliasIdxRaw. The functions aoqi@0: // Compile::alias_type (and kin) produce and manage these indexes. aoqi@0: // aoqi@0: // By convention, the value of in(AliasIdxTop) (i.e., in(1)) is always the top node. aoqi@0: // (Note that this provides quick access to the top node inside MergeMem methods, aoqi@0: // without the need to reach out via TLS to Compile::current.) aoqi@0: // aoqi@0: // As a consequence of what was just described, a MergeMem that represents a full aoqi@0: // memory state has an edge in(AliasIdxBot) which is a "wide" memory state, aoqi@0: // containing all alias categories. aoqi@0: // aoqi@0: // MergeMem nodes never (?) have control inputs, so in(0) is NULL. aoqi@0: // aoqi@0: // All other edges in(N) (including in(AliasIdxRaw), which is in(3)) are either aoqi@0: // a memory state for the alias type , or else the top node, meaning that aoqi@0: // there is no particular input for that alias type. Note that the length of aoqi@0: // a MergeMem is variable, and may be extended at any time to accommodate new aoqi@0: // memory states at larger alias indexes. When merges grow, they are of course aoqi@0: // filled with "top" in the unused in() positions. aoqi@0: // aoqi@0: // This use of top is named "empty_memory()", or "empty_mem" (no-memory) as a variable. aoqi@0: // (Top was chosen because it works smoothly with passes like GCM.) aoqi@0: // aoqi@0: // For convenience, we hardwire the alias index for TypeRawPtr::BOTTOM. (It is aoqi@0: // the type of random VM bits like TLS references.) Since it is always the aoqi@0: // first non-Bot memory slice, some low-level loops use it to initialize an aoqi@0: // index variable: for (i = AliasIdxRaw; i < req(); i++). aoqi@0: // aoqi@0: // aoqi@0: // ACCESSORS: There is a special accessor MergeMemNode::base_memory which returns aoqi@0: // the distinguished "wide" state. The accessor MergeMemNode::memory_at(N) returns aoqi@0: // the memory state for alias type , or (if there is no particular slice at , aoqi@0: // it returns the base memory. To prevent bugs, memory_at does not accept aoqi@0: // or indexes. The iterator MergeMemStream provides robust iteration over aoqi@0: // MergeMem nodes or pairs of such nodes, ensuring that the non-top edges are visited. aoqi@0: // aoqi@0: // %%%% We may get rid of base_memory as a separate accessor at some point; it isn't aoqi@0: // really that different from the other memory inputs. An abbreviation called aoqi@0: // "bot_memory()" for "memory_at(AliasIdxBot)" would keep code tidy. aoqi@0: // aoqi@0: // aoqi@0: // PARTIAL MEMORY STATES: During optimization, MergeMem nodes may arise that represent aoqi@0: // partial memory states. When a Phi splits through a MergeMem, the copy of the Phi aoqi@0: // that "emerges though" the base memory will be marked as excluding the alias types aoqi@0: // of the other (narrow-memory) copies which "emerged through" the narrow edges: aoqi@0: // aoqi@0: // Phi(U, MergeMem(: W, <8>: Y)) aoqi@0: // ==Ideal=> MergeMem(: Phi(U, W), Phi<8>(U, Y)) aoqi@0: // aoqi@0: // This strange "subtraction" effect is necessary to ensure IGVN convergence. aoqi@0: // (It is currently unimplemented.) As you can see, the resulting merge is aoqi@0: // actually a disjoint union of memory states, rather than an overlay. aoqi@0: // aoqi@0: aoqi@0: //------------------------------MergeMemNode----------------------------------- aoqi@0: Node* MergeMemNode::make_empty_memory() { aoqi@0: Node* empty_memory = (Node*) Compile::current()->top(); aoqi@0: assert(empty_memory->is_top(), "correct sentinel identity"); aoqi@0: return empty_memory; aoqi@0: } aoqi@0: aoqi@0: MergeMemNode::MergeMemNode(Node *new_base) : Node(1+Compile::AliasIdxRaw) { aoqi@0: init_class_id(Class_MergeMem); aoqi@0: // all inputs are nullified in Node::Node(int) aoqi@0: // set_input(0, NULL); // no control input aoqi@0: aoqi@0: // Initialize the edges uniformly to top, for starters. aoqi@0: Node* empty_mem = make_empty_memory(); aoqi@0: for (uint i = Compile::AliasIdxTop; i < req(); i++) { aoqi@0: init_req(i,empty_mem); aoqi@0: } aoqi@0: assert(empty_memory() == empty_mem, ""); aoqi@0: aoqi@0: if( new_base != NULL && new_base->is_MergeMem() ) { aoqi@0: MergeMemNode* mdef = new_base->as_MergeMem(); aoqi@0: assert(mdef->empty_memory() == empty_mem, "consistent sentinels"); aoqi@0: for (MergeMemStream mms(this, mdef); mms.next_non_empty2(); ) { aoqi@0: mms.set_memory(mms.memory2()); aoqi@0: } aoqi@0: assert(base_memory() == mdef->base_memory(), ""); aoqi@0: } else { aoqi@0: set_base_memory(new_base); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Make a new, untransformed MergeMem with the same base as 'mem'. aoqi@0: // If mem is itself a MergeMem, populate the result with the same edges. aoqi@0: MergeMemNode* MergeMemNode::make(Compile* C, Node* mem) { aoqi@0: return new(C) MergeMemNode(mem); aoqi@0: } aoqi@0: aoqi@0: //------------------------------cmp-------------------------------------------- aoqi@0: uint MergeMemNode::hash() const { return NO_HASH; } aoqi@0: uint MergeMemNode::cmp( const Node &n ) const { aoqi@0: return (&n == this); // Always fail except on self aoqi@0: } aoqi@0: aoqi@0: //------------------------------Identity--------------------------------------- aoqi@0: Node* MergeMemNode::Identity(PhaseTransform *phase) { aoqi@0: // Identity if this merge point does not record any interesting memory aoqi@0: // disambiguations. aoqi@0: Node* base_mem = base_memory(); aoqi@0: Node* empty_mem = empty_memory(); aoqi@0: if (base_mem != empty_mem) { // Memory path is not dead? aoqi@0: for (uint i = Compile::AliasIdxRaw; i < req(); i++) { aoqi@0: Node* mem = in(i); aoqi@0: if (mem != empty_mem && mem != base_mem) { aoqi@0: return this; // Many memory splits; no change aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: return base_mem; // No memory splits; ID on the one true input aoqi@0: } aoqi@0: aoqi@0: //------------------------------Ideal------------------------------------------ aoqi@0: // This method is invoked recursively on chains of MergeMem nodes aoqi@0: Node *MergeMemNode::Ideal(PhaseGVN *phase, bool can_reshape) { aoqi@0: // Remove chain'd MergeMems aoqi@0: // aoqi@0: // This is delicate, because the each "in(i)" (i >= Raw) is interpreted aoqi@0: // relative to the "in(Bot)". Since we are patching both at the same time, aoqi@0: // we have to be careful to read each "in(i)" relative to the old "in(Bot)", aoqi@0: // but rewrite each "in(i)" relative to the new "in(Bot)". aoqi@0: Node *progress = NULL; aoqi@0: aoqi@0: aoqi@0: Node* old_base = base_memory(); aoqi@0: Node* empty_mem = empty_memory(); aoqi@0: if (old_base == empty_mem) aoqi@0: return NULL; // Dead memory path. aoqi@0: aoqi@0: MergeMemNode* old_mbase; aoqi@0: if (old_base != NULL && old_base->is_MergeMem()) aoqi@0: old_mbase = old_base->as_MergeMem(); aoqi@0: else aoqi@0: old_mbase = NULL; aoqi@0: Node* new_base = old_base; aoqi@0: aoqi@0: // simplify stacked MergeMems in base memory aoqi@0: if (old_mbase) new_base = old_mbase->base_memory(); aoqi@0: aoqi@0: // the base memory might contribute new slices beyond my req() aoqi@0: if (old_mbase) grow_to_match(old_mbase); aoqi@0: aoqi@0: // Look carefully at the base node if it is a phi. aoqi@0: PhiNode* phi_base; aoqi@0: if (new_base != NULL && new_base->is_Phi()) aoqi@0: phi_base = new_base->as_Phi(); aoqi@0: else aoqi@0: phi_base = NULL; aoqi@0: aoqi@0: Node* phi_reg = NULL; aoqi@0: uint phi_len = (uint)-1; aoqi@0: if (phi_base != NULL && !phi_base->is_copy()) { aoqi@0: // do not examine phi if degraded to a copy aoqi@0: phi_reg = phi_base->region(); aoqi@0: phi_len = phi_base->req(); aoqi@0: // see if the phi is unfinished aoqi@0: for (uint i = 1; i < phi_len; i++) { aoqi@0: if (phi_base->in(i) == NULL) { aoqi@0: // incomplete phi; do not look at it yet! aoqi@0: phi_reg = NULL; aoqi@0: phi_len = (uint)-1; aoqi@0: break; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Note: We do not call verify_sparse on entry, because inputs aoqi@0: // can normalize to the base_memory via subsume_node or similar aoqi@0: // mechanisms. This method repairs that damage. aoqi@0: aoqi@0: assert(!old_mbase || old_mbase->is_empty_memory(empty_mem), "consistent sentinels"); aoqi@0: aoqi@0: // Look at each slice. aoqi@0: for (uint i = Compile::AliasIdxRaw; i < req(); i++) { aoqi@0: Node* old_in = in(i); aoqi@0: // calculate the old memory value aoqi@0: Node* old_mem = old_in; aoqi@0: if (old_mem == empty_mem) old_mem = old_base; aoqi@0: assert(old_mem == memory_at(i), ""); aoqi@0: aoqi@0: // maybe update (reslice) the old memory value aoqi@0: aoqi@0: // simplify stacked MergeMems aoqi@0: Node* new_mem = old_mem; aoqi@0: MergeMemNode* old_mmem; aoqi@0: if (old_mem != NULL && old_mem->is_MergeMem()) aoqi@0: old_mmem = old_mem->as_MergeMem(); aoqi@0: else aoqi@0: old_mmem = NULL; aoqi@0: if (old_mmem == this) { aoqi@0: // This can happen if loops break up and safepoints disappear. aoqi@0: // A merge of BotPtr (default) with a RawPtr memory derived from a aoqi@0: // safepoint can be rewritten to a merge of the same BotPtr with aoqi@0: // the BotPtr phi coming into the loop. If that phi disappears aoqi@0: // also, we can end up with a self-loop of the mergemem. aoqi@0: // In general, if loops degenerate and memory effects disappear, aoqi@0: // a mergemem can be left looking at itself. This simply means aoqi@0: // that the mergemem's default should be used, since there is aoqi@0: // no longer any apparent effect on this slice. aoqi@0: // Note: If a memory slice is a MergeMem cycle, it is unreachable aoqi@0: // from start. Update the input to TOP. aoqi@0: new_mem = (new_base == this || new_base == empty_mem)? empty_mem : new_base; aoqi@0: } aoqi@0: else if (old_mmem != NULL) { aoqi@0: new_mem = old_mmem->memory_at(i); aoqi@0: } aoqi@0: // else preceding memory was not a MergeMem aoqi@0: aoqi@0: // replace equivalent phis (unfortunately, they do not GVN together) aoqi@0: if (new_mem != NULL && new_mem != new_base && aoqi@0: new_mem->req() == phi_len && new_mem->in(0) == phi_reg) { aoqi@0: if (new_mem->is_Phi()) { aoqi@0: PhiNode* phi_mem = new_mem->as_Phi(); aoqi@0: for (uint i = 1; i < phi_len; i++) { aoqi@0: if (phi_base->in(i) != phi_mem->in(i)) { aoqi@0: phi_mem = NULL; aoqi@0: break; aoqi@0: } aoqi@0: } aoqi@0: if (phi_mem != NULL) { aoqi@0: // equivalent phi nodes; revert to the def aoqi@0: new_mem = new_base; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // maybe store down a new value aoqi@0: Node* new_in = new_mem; aoqi@0: if (new_in == new_base) new_in = empty_mem; aoqi@0: aoqi@0: if (new_in != old_in) { aoqi@0: // Warning: Do not combine this "if" with the previous "if" aoqi@0: // A memory slice might have be be rewritten even if it is semantically aoqi@0: // unchanged, if the base_memory value has changed. aoqi@0: set_req(i, new_in); aoqi@0: progress = this; // Report progress aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: if (new_base != old_base) { aoqi@0: set_req(Compile::AliasIdxBot, new_base); aoqi@0: // Don't use set_base_memory(new_base), because we need to update du. aoqi@0: assert(base_memory() == new_base, ""); aoqi@0: progress = this; aoqi@0: } aoqi@0: aoqi@0: if( base_memory() == this ) { aoqi@0: // a self cycle indicates this memory path is dead aoqi@0: set_req(Compile::AliasIdxBot, empty_mem); aoqi@0: } aoqi@0: aoqi@0: // Resolve external cycles by calling Ideal on a MergeMem base_memory aoqi@0: // Recursion must occur after the self cycle check above aoqi@0: if( base_memory()->is_MergeMem() ) { aoqi@0: MergeMemNode *new_mbase = base_memory()->as_MergeMem(); aoqi@0: Node *m = phase->transform(new_mbase); // Rollup any cycles aoqi@0: if( m != NULL && (m->is_top() || aoqi@0: m->is_MergeMem() && m->as_MergeMem()->base_memory() == empty_mem) ) { aoqi@0: // propagate rollup of dead cycle to self aoqi@0: set_req(Compile::AliasIdxBot, empty_mem); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: if( base_memory() == empty_mem ) { aoqi@0: progress = this; aoqi@0: // Cut inputs during Parse phase only. aoqi@0: // During Optimize phase a dead MergeMem node will be subsumed by Top. aoqi@0: if( !can_reshape ) { aoqi@0: for (uint i = Compile::AliasIdxRaw; i < req(); i++) { aoqi@0: if( in(i) != empty_mem ) { set_req(i, empty_mem); } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: if( !progress && base_memory()->is_Phi() && can_reshape ) { aoqi@0: // Check if PhiNode::Ideal's "Split phis through memory merges" aoqi@0: // transform should be attempted. Look for this->phi->this cycle. aoqi@0: uint merge_width = req(); aoqi@0: if (merge_width > Compile::AliasIdxRaw) { aoqi@0: PhiNode* phi = base_memory()->as_Phi(); aoqi@0: for( uint i = 1; i < phi->req(); ++i ) {// For all paths in aoqi@0: if (phi->in(i) == this) { aoqi@0: phase->is_IterGVN()->_worklist.push(phi); aoqi@0: break; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: assert(progress || verify_sparse(), "please, no dups of base"); aoqi@0: return progress; aoqi@0: } aoqi@0: aoqi@0: //-------------------------set_base_memory------------------------------------- aoqi@0: void MergeMemNode::set_base_memory(Node *new_base) { aoqi@0: Node* empty_mem = empty_memory(); aoqi@0: set_req(Compile::AliasIdxBot, new_base); aoqi@0: assert(memory_at(req()) == new_base, "must set default memory"); aoqi@0: // Clear out other occurrences of new_base: aoqi@0: if (new_base != empty_mem) { aoqi@0: for (uint i = Compile::AliasIdxRaw; i < req(); i++) { aoqi@0: if (in(i) == new_base) set_req(i, empty_mem); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: //------------------------------out_RegMask------------------------------------ aoqi@0: const RegMask &MergeMemNode::out_RegMask() const { aoqi@0: return RegMask::Empty; aoqi@0: } aoqi@0: aoqi@0: //------------------------------dump_spec-------------------------------------- aoqi@0: #ifndef PRODUCT aoqi@0: void MergeMemNode::dump_spec(outputStream *st) const { aoqi@0: st->print(" {"); aoqi@0: Node* base_mem = base_memory(); aoqi@0: for( uint i = Compile::AliasIdxRaw; i < req(); i++ ) { aoqi@0: Node* mem = memory_at(i); aoqi@0: if (mem == base_mem) { st->print(" -"); continue; } aoqi@0: st->print( " N%d:", mem->_idx ); aoqi@0: Compile::current()->get_adr_type(i)->dump_on(st); aoqi@0: } aoqi@0: st->print(" }"); aoqi@0: } aoqi@0: #endif // !PRODUCT aoqi@0: aoqi@0: aoqi@0: #ifdef ASSERT aoqi@0: static bool might_be_same(Node* a, Node* b) { aoqi@0: if (a == b) return true; aoqi@0: if (!(a->is_Phi() || b->is_Phi())) return false; aoqi@0: // phis shift around during optimization aoqi@0: return true; // pretty stupid... aoqi@0: } aoqi@0: aoqi@0: // verify a narrow slice (either incoming or outgoing) aoqi@0: static void verify_memory_slice(const MergeMemNode* m, int alias_idx, Node* n) { aoqi@0: if (!VerifyAliases) return; // don't bother to verify unless requested aoqi@0: if (is_error_reported()) return; // muzzle asserts when debugging an error aoqi@0: if (Node::in_dump()) return; // muzzle asserts when printing aoqi@0: assert(alias_idx >= Compile::AliasIdxRaw, "must not disturb base_memory or sentinel"); aoqi@0: assert(n != NULL, ""); aoqi@0: // Elide intervening MergeMem's aoqi@0: while (n->is_MergeMem()) { aoqi@0: n = n->as_MergeMem()->memory_at(alias_idx); aoqi@0: } aoqi@0: Compile* C = Compile::current(); aoqi@0: const TypePtr* n_adr_type = n->adr_type(); aoqi@0: if (n == m->empty_memory()) { aoqi@0: // Implicit copy of base_memory() aoqi@0: } else if (n_adr_type != TypePtr::BOTTOM) { aoqi@0: assert(n_adr_type != NULL, "new memory must have a well-defined adr_type"); aoqi@0: assert(C->must_alias(n_adr_type, alias_idx), "new memory must match selected slice"); aoqi@0: } else { aoqi@0: // A few places like make_runtime_call "know" that VM calls are narrow, aoqi@0: // and can be used to update only the VM bits stored as TypeRawPtr::BOTTOM. aoqi@0: bool expected_wide_mem = false; aoqi@0: if (n == m->base_memory()) { aoqi@0: expected_wide_mem = true; aoqi@0: } else if (alias_idx == Compile::AliasIdxRaw || aoqi@0: n == m->memory_at(Compile::AliasIdxRaw)) { aoqi@0: expected_wide_mem = true; aoqi@0: } else if (!C->alias_type(alias_idx)->is_rewritable()) { aoqi@0: // memory can "leak through" calls on channels that aoqi@0: // are write-once. Allow this also. aoqi@0: expected_wide_mem = true; aoqi@0: } aoqi@0: assert(expected_wide_mem, "expected narrow slice replacement"); aoqi@0: } aoqi@0: } aoqi@0: #else // !ASSERT aoqi@0: #define verify_memory_slice(m,i,n) (void)(0) // PRODUCT version is no-op aoqi@0: #endif aoqi@0: aoqi@0: aoqi@0: //-----------------------------memory_at--------------------------------------- aoqi@0: Node* MergeMemNode::memory_at(uint alias_idx) const { aoqi@0: assert(alias_idx >= Compile::AliasIdxRaw || aoqi@0: alias_idx == Compile::AliasIdxBot && Compile::current()->AliasLevel() == 0, aoqi@0: "must avoid base_memory and AliasIdxTop"); aoqi@0: aoqi@0: // Otherwise, it is a narrow slice. aoqi@0: Node* n = alias_idx < req() ? in(alias_idx) : empty_memory(); aoqi@0: Compile *C = Compile::current(); aoqi@0: if (is_empty_memory(n)) { aoqi@0: // the array is sparse; empty slots are the "top" node aoqi@0: n = base_memory(); aoqi@0: assert(Node::in_dump() aoqi@0: || n == NULL || n->bottom_type() == Type::TOP aoqi@0: || n->adr_type() == NULL // address is TOP aoqi@0: || n->adr_type() == TypePtr::BOTTOM aoqi@0: || n->adr_type() == TypeRawPtr::BOTTOM aoqi@0: || Compile::current()->AliasLevel() == 0, aoqi@0: "must be a wide memory"); aoqi@0: // AliasLevel == 0 if we are organizing the memory states manually. aoqi@0: // See verify_memory_slice for comments on TypeRawPtr::BOTTOM. aoqi@0: } else { aoqi@0: // make sure the stored slice is sane aoqi@0: #ifdef ASSERT aoqi@0: if (is_error_reported() || Node::in_dump()) { aoqi@0: } else if (might_be_same(n, base_memory())) { aoqi@0: // Give it a pass: It is a mostly harmless repetition of the base. aoqi@0: // This can arise normally from node subsumption during optimization. aoqi@0: } else { aoqi@0: verify_memory_slice(this, alias_idx, n); aoqi@0: } aoqi@0: #endif aoqi@0: } aoqi@0: return n; aoqi@0: } aoqi@0: aoqi@0: //---------------------------set_memory_at------------------------------------- aoqi@0: void MergeMemNode::set_memory_at(uint alias_idx, Node *n) { aoqi@0: verify_memory_slice(this, alias_idx, n); aoqi@0: Node* empty_mem = empty_memory(); aoqi@0: if (n == base_memory()) n = empty_mem; // collapse default aoqi@0: uint need_req = alias_idx+1; aoqi@0: if (req() < need_req) { aoqi@0: if (n == empty_mem) return; // already the default, so do not grow me aoqi@0: // grow the sparse array aoqi@0: do { aoqi@0: add_req(empty_mem); aoqi@0: } while (req() < need_req); aoqi@0: } aoqi@0: set_req( alias_idx, n ); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: aoqi@0: //--------------------------iteration_setup------------------------------------ aoqi@0: void MergeMemNode::iteration_setup(const MergeMemNode* other) { aoqi@0: if (other != NULL) { aoqi@0: grow_to_match(other); aoqi@0: // invariant: the finite support of mm2 is within mm->req() aoqi@0: #ifdef ASSERT aoqi@0: for (uint i = req(); i < other->req(); i++) { aoqi@0: assert(other->is_empty_memory(other->in(i)), "slice left uncovered"); aoqi@0: } aoqi@0: #endif aoqi@0: } aoqi@0: // Replace spurious copies of base_memory by top. aoqi@0: Node* base_mem = base_memory(); aoqi@0: if (base_mem != NULL && !base_mem->is_top()) { aoqi@0: for (uint i = Compile::AliasIdxBot+1, imax = req(); i < imax; i++) { aoqi@0: if (in(i) == base_mem) aoqi@0: set_req(i, empty_memory()); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: //---------------------------grow_to_match------------------------------------- aoqi@0: void MergeMemNode::grow_to_match(const MergeMemNode* other) { aoqi@0: Node* empty_mem = empty_memory(); aoqi@0: assert(other->is_empty_memory(empty_mem), "consistent sentinels"); aoqi@0: // look for the finite support of the other memory aoqi@0: for (uint i = other->req(); --i >= req(); ) { aoqi@0: if (other->in(i) != empty_mem) { aoqi@0: uint new_len = i+1; aoqi@0: while (req() < new_len) add_req(empty_mem); aoqi@0: break; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: //---------------------------verify_sparse------------------------------------- aoqi@0: #ifndef PRODUCT aoqi@0: bool MergeMemNode::verify_sparse() const { aoqi@0: assert(is_empty_memory(make_empty_memory()), "sane sentinel"); aoqi@0: Node* base_mem = base_memory(); aoqi@0: // The following can happen in degenerate cases, since empty==top. aoqi@0: if (is_empty_memory(base_mem)) return true; aoqi@0: for (uint i = Compile::AliasIdxRaw; i < req(); i++) { aoqi@0: assert(in(i) != NULL, "sane slice"); aoqi@0: if (in(i) == base_mem) return false; // should have been the sentinel value! aoqi@0: } aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: bool MergeMemStream::match_memory(Node* mem, const MergeMemNode* mm, int idx) { aoqi@0: Node* n; aoqi@0: n = mm->in(idx); aoqi@0: if (mem == n) return true; // might be empty_memory() aoqi@0: n = (idx == Compile::AliasIdxBot)? mm->base_memory(): mm->memory_at(idx); aoqi@0: if (mem == n) return true; aoqi@0: while (n->is_Phi() && (n = n->as_Phi()->is_copy()) != NULL) { aoqi@0: if (mem == n) return true; aoqi@0: if (n == NULL) break; aoqi@0: } aoqi@0: return false; aoqi@0: } aoqi@0: #endif // !PRODUCT