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