0N/A/*
3226N/A * Copyright (c) 2005, 2012, Oracle and/or its affiliates. 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 *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A *
0N/A */
0N/A
1879N/A#include "precompiled.hpp"
1879N/A#include "ci/bcEscapeAnalyzer.hpp"
3619N/A#include "compiler/compileLog.hpp"
1879N/A#include "libadt/vectset.hpp"
1879N/A#include "memory/allocation.hpp"
1879N/A#include "opto/c2compiler.hpp"
1879N/A#include "opto/callnode.hpp"
1879N/A#include "opto/cfgnode.hpp"
1879N/A#include "opto/compile.hpp"
1879N/A#include "opto/escape.hpp"
1879N/A#include "opto/phaseX.hpp"
1879N/A#include "opto/rootnode.hpp"
0N/A
1554N/AConnectionGraph::ConnectionGraph(Compile * C, PhaseIterGVN *igvn) :
3619N/A _nodes(C->comp_arena(), C->unique(), C->unique(), NULL),
244N/A _collecting(true),
3619N/A _verify(false),
244N/A _compile(C),
1554N/A _igvn(igvn),
244N/A _node_map(C->comp_arena()) {
3619N/A // Add unknown java object.
3619N/A add_java_object(C->top(), PointsToNode::GlobalEscape);
3619N/A phantom_obj = ptnode_adr(C->top()->_idx)->as_JavaObject();
253N/A // Add ConP(#NULL) and ConN(#NULL) nodes.
253N/A Node* oop_null = igvn->zerocon(T_OBJECT);
3619N/A assert(oop_null->_idx < nodes_size(), "should be created already");
3619N/A add_java_object(oop_null, PointsToNode::NoEscape);
3619N/A null_obj = ptnode_adr(oop_null->_idx)->as_JavaObject();
253N/A if (UseCompressedOops) {
253N/A Node* noop_null = igvn->zerocon(T_NARROWOOP);
3619N/A assert(noop_null->_idx < nodes_size(), "should be created already");
3619N/A map_ideal_node(noop_null, null_obj);
253N/A }
2956N/A _pcmp_neq = NULL; // Should be initialized
2956N/A _pcmp_eq = NULL;
0N/A}
0N/A
3619N/Abool ConnectionGraph::has_candidates(Compile *C) {
3619N/A // EA brings benefits only when the code has allocations and/or locks which
3619N/A // are represented by ideal Macro nodes.
3619N/A int cnt = C->macro_count();
3619N/A for( int i=0; i < cnt; i++ ) {
3619N/A Node *n = C->macro_node(i);
3619N/A if ( n->is_Allocate() )
3619N/A return true;
3619N/A if( n->is_Lock() ) {
3619N/A Node* obj = n->as_Lock()->obj_node()->uncast();
3619N/A if( !(obj->is_Parm() || obj->is_Con()) )
3619N/A return true;
3619N/A }
3619N/A }
3619N/A return false;
3619N/A}
3619N/A
3619N/Avoid ConnectionGraph::do_analysis(Compile *C, PhaseIterGVN *igvn) {
3619N/A Compile::TracePhase t2("escapeAnalysis", &Phase::_t_escapeAnalysis, true);
3619N/A ResourceMark rm;
3619N/A
3619N/A // Add ConP#NULL and ConN#NULL nodes before ConnectionGraph construction
3619N/A // to create space for them in ConnectionGraph::_nodes[].
3619N/A Node* oop_null = igvn->zerocon(T_OBJECT);
3619N/A Node* noop_null = igvn->zerocon(T_NARROWOOP);
3619N/A ConnectionGraph* congraph = new(C->comp_arena()) ConnectionGraph(C, igvn);
3619N/A // Perform escape analysis
3619N/A if (congraph->compute_escape()) {
3619N/A // There are non escaping objects.
3619N/A C->set_congraph(congraph);
3619N/A }
3619N/A // Cleanup.
3619N/A if (oop_null->outcnt() == 0)
3619N/A igvn->hash_delete(oop_null);
3619N/A if (noop_null->outcnt() == 0)
3619N/A igvn->hash_delete(noop_null);
3619N/A}
3619N/A
3619N/Abool ConnectionGraph::compute_escape() {
3619N/A Compile* C = _compile;
3619N/A PhaseGVN* igvn = _igvn;
3619N/A
3619N/A // Worklists used by EA.
3619N/A Unique_Node_List delayed_worklist;
3619N/A GrowableArray<Node*> alloc_worklist;
3619N/A GrowableArray<Node*> ptr_cmp_worklist;
3619N/A GrowableArray<Node*> storestore_worklist;
3619N/A GrowableArray<PointsToNode*> ptnodes_worklist;
3619N/A GrowableArray<JavaObjectNode*> java_objects_worklist;
3619N/A GrowableArray<JavaObjectNode*> non_escaped_worklist;
3619N/A GrowableArray<FieldNode*> oop_fields_worklist;
3619N/A DEBUG_ONLY( GrowableArray<Node*> addp_worklist; )
3619N/A
3619N/A { Compile::TracePhase t3("connectionGraph", &Phase::_t_connectionGraph, true);
3619N/A
3619N/A // 1. Populate Connection Graph (CG) with PointsTo nodes.
3619N/A ideal_nodes.map(C->unique(), NULL); // preallocate space
3619N/A // Initialize worklist
3619N/A if (C->root() != NULL) {
3619N/A ideal_nodes.push(C->root());
3619N/A }
3619N/A for( uint next = 0; next < ideal_nodes.size(); ++next ) {
3619N/A Node* n = ideal_nodes.at(next);
3619N/A // Create PointsTo nodes and add them to Connection Graph. Called
3619N/A // only once per ideal node since ideal_nodes is Unique_Node list.
3619N/A add_node_to_connection_graph(n, &delayed_worklist);
3619N/A PointsToNode* ptn = ptnode_adr(n->_idx);
3619N/A if (ptn != NULL) {
3619N/A ptnodes_worklist.append(ptn);
3619N/A if (ptn->is_JavaObject()) {
3619N/A java_objects_worklist.append(ptn->as_JavaObject());
3619N/A if ((n->is_Allocate() || n->is_CallStaticJava()) &&
3619N/A (ptn->escape_state() < PointsToNode::GlobalEscape)) {
3619N/A // Only allocations and java static calls results are interesting.
3619N/A non_escaped_worklist.append(ptn->as_JavaObject());
3619N/A }
3619N/A } else if (ptn->is_Field() && ptn->as_Field()->is_oop()) {
3619N/A oop_fields_worklist.append(ptn->as_Field());
3619N/A }
3619N/A }
3619N/A if (n->is_MergeMem()) {
3619N/A // Collect all MergeMem nodes to add memory slices for
3619N/A // scalar replaceable objects in split_unique_types().
3619N/A _mergemem_worklist.append(n->as_MergeMem());
3619N/A } else if (OptimizePtrCompare && n->is_Cmp() &&
3619N/A (n->Opcode() == Op_CmpP || n->Opcode() == Op_CmpN)) {
3619N/A // Collect compare pointers nodes.
3619N/A ptr_cmp_worklist.append(n);
3619N/A } else if (n->is_MemBarStoreStore()) {
3619N/A // Collect all MemBarStoreStore nodes so that depending on the
3619N/A // escape status of the associated Allocate node some of them
3619N/A // may be eliminated.
3619N/A storestore_worklist.append(n);
3619N/A#ifdef ASSERT
3619N/A } else if(n->is_AddP()) {
3619N/A // Collect address nodes for graph verification.
3619N/A addp_worklist.append(n);
3619N/A#endif
3619N/A }
3619N/A for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
3619N/A Node* m = n->fast_out(i); // Get user
3619N/A ideal_nodes.push(m);
3619N/A }
3619N/A }
3619N/A if (non_escaped_worklist.length() == 0) {
3619N/A _collecting = false;
3619N/A return false; // Nothing to do.
3619N/A }
3619N/A // Add final simple edges to graph.
3619N/A while(delayed_worklist.size() > 0) {
3619N/A Node* n = delayed_worklist.pop();
3619N/A add_final_edges(n);
3619N/A }
3619N/A int ptnodes_length = ptnodes_worklist.length();
3619N/A
3619N/A#ifdef ASSERT
3619N/A if (VerifyConnectionGraph) {
3619N/A // Verify that no new simple edges could be created and all
3619N/A // local vars has edges.
3619N/A _verify = true;
3619N/A for (int next = 0; next < ptnodes_length; ++next) {
3619N/A PointsToNode* ptn = ptnodes_worklist.at(next);
3619N/A add_final_edges(ptn->ideal_node());
3619N/A if (ptn->is_LocalVar() && ptn->edge_count() == 0) {
3619N/A ptn->dump();
3619N/A assert(ptn->as_LocalVar()->edge_count() > 0, "sanity");
3619N/A }
3619N/A }
3619N/A _verify = false;
3619N/A }
3619N/A#endif
3619N/A
3619N/A // 2. Finish Graph construction by propagating references to all
3619N/A // java objects through graph.
3619N/A if (!complete_connection_graph(ptnodes_worklist, non_escaped_worklist,
3619N/A java_objects_worklist, oop_fields_worklist)) {
3619N/A // All objects escaped or hit time or iterations limits.
3619N/A _collecting = false;
3619N/A return false;
3619N/A }
3619N/A
3619N/A // 3. Adjust scalar_replaceable state of nonescaping objects and push
3619N/A // scalar replaceable allocations on alloc_worklist for processing
3619N/A // in split_unique_types().
3619N/A int non_escaped_length = non_escaped_worklist.length();
3619N/A for (int next = 0; next < non_escaped_length; next++) {
3619N/A JavaObjectNode* ptn = non_escaped_worklist.at(next);
3619N/A if (ptn->escape_state() == PointsToNode::NoEscape &&
3619N/A ptn->scalar_replaceable()) {
3619N/A adjust_scalar_replaceable_state(ptn);
3619N/A if (ptn->scalar_replaceable()) {
3619N/A alloc_worklist.append(ptn->ideal_node());
3619N/A }
3619N/A }
3619N/A }
3619N/A
3619N/A#ifdef ASSERT
3619N/A if (VerifyConnectionGraph) {
3619N/A // Verify that graph is complete - no new edges could be added or needed.
3619N/A verify_connection_graph(ptnodes_worklist, non_escaped_worklist,
3619N/A java_objects_worklist, addp_worklist);
3619N/A }
3619N/A assert(C->unique() == nodes_size(), "no new ideal nodes should be added during ConnectionGraph build");
3619N/A assert(null_obj->escape_state() == PointsToNode::NoEscape &&
3619N/A null_obj->edge_count() == 0 &&
3619N/A !null_obj->arraycopy_src() &&
3619N/A !null_obj->arraycopy_dst(), "sanity");
3619N/A#endif
3619N/A
3619N/A _collecting = false;
3619N/A
3619N/A } // TracePhase t3("connectionGraph")
3619N/A
3619N/A // 4. Optimize ideal graph based on EA information.
3619N/A bool has_non_escaping_obj = (non_escaped_worklist.length() > 0);
3619N/A if (has_non_escaping_obj) {
3619N/A optimize_ideal_graph(ptr_cmp_worklist, storestore_worklist);
3619N/A }
3619N/A
3619N/A#ifndef PRODUCT
3619N/A if (PrintEscapeAnalysis) {
3619N/A dump(ptnodes_worklist); // Dump ConnectionGraph
3619N/A }
3619N/A#endif
3619N/A
3619N/A bool has_scalar_replaceable_candidates = (alloc_worklist.length() > 0);
3619N/A#ifdef ASSERT
3619N/A if (VerifyConnectionGraph) {
3619N/A int alloc_length = alloc_worklist.length();
3619N/A for (int next = 0; next < alloc_length; ++next) {
3619N/A Node* n = alloc_worklist.at(next);
3619N/A PointsToNode* ptn = ptnode_adr(n->_idx);
3619N/A assert(ptn->escape_state() == PointsToNode::NoEscape && ptn->scalar_replaceable(), "sanity");
3619N/A }
3619N/A }
3619N/A#endif
3619N/A
3619N/A // 5. Separate memory graph for scalar replaceable allcations.
3619N/A if (has_scalar_replaceable_candidates &&
3619N/A C->AliasLevel() >= 3 && EliminateAllocations) {
3619N/A // Now use the escape information to create unique types for
3619N/A // scalar replaceable objects.
3619N/A split_unique_types(alloc_worklist);
3619N/A if (C->failing()) return false;
4141N/A C->print_method(PHASE_AFTER_EA, 2);
3619N/A
3619N/A#ifdef ASSERT
3619N/A } else if (Verbose && (PrintEscapeAnalysis || PrintEliminateAllocations)) {
3619N/A tty->print("=== No allocations eliminated for ");
3619N/A C->method()->print_short_name();
3619N/A if(!EliminateAllocations) {
3619N/A tty->print(" since EliminateAllocations is off ===");
3619N/A } else if(!has_scalar_replaceable_candidates) {
3619N/A tty->print(" since there are no scalar replaceable candidates ===");
3619N/A } else if(C->AliasLevel() < 3) {
3619N/A tty->print(" since AliasLevel < 3 ===");
3619N/A }
3619N/A tty->cr();
3619N/A#endif
3619N/A }
3619N/A return has_non_escaping_obj;
3619N/A}
3619N/A
4015N/A// Utility function for nodes that load an object
4015N/Avoid ConnectionGraph::add_objload_to_connection_graph(Node *n, Unique_Node_List *delayed_worklist) {
4015N/A // Using isa_ptr() instead of isa_oopptr() for LoadP and Phi because
4015N/A // ThreadLocal has RawPtr type.
4015N/A const Type* t = _igvn->type(n);
4015N/A if (t->make_ptr() != NULL) {
4015N/A Node* adr = n->in(MemNode::Address);
4015N/A#ifdef ASSERT
4015N/A if (!adr->is_AddP()) {
4015N/A assert(_igvn->type(adr)->isa_rawptr(), "sanity");
4015N/A } else {
4015N/A assert((ptnode_adr(adr->_idx) == NULL ||
4015N/A ptnode_adr(adr->_idx)->as_Field()->is_oop()), "sanity");
4015N/A }
4015N/A#endif
4015N/A add_local_var_and_edge(n, PointsToNode::NoEscape,
4015N/A adr, delayed_worklist);
4015N/A }
4015N/A}
4015N/A
3619N/A// Populate Connection Graph with PointsTo nodes and create simple
3619N/A// connection graph edges.
3619N/Avoid ConnectionGraph::add_node_to_connection_graph(Node *n, Unique_Node_List *delayed_worklist) {
3619N/A assert(!_verify, "this method sould not be called for verification");
3619N/A PhaseGVN* igvn = _igvn;
3619N/A uint n_idx = n->_idx;
3619N/A PointsToNode* n_ptn = ptnode_adr(n_idx);
3619N/A if (n_ptn != NULL)
3619N/A return; // No need to redefine PointsTo node during first iteration.
3619N/A
3619N/A if (n->is_Call()) {
3619N/A // Arguments to allocation and locking don't escape.
3619N/A if (n->is_AbstractLock()) {
3619N/A // Put Lock and Unlock nodes on IGVN worklist to process them during
3619N/A // first IGVN optimization when escape information is still available.
3619N/A record_for_optimizer(n);
3619N/A } else if (n->is_Allocate()) {
3619N/A add_call_node(n->as_Call());
3619N/A record_for_optimizer(n);
3619N/A } else {
3619N/A if (n->is_CallStaticJava()) {
3619N/A const char* name = n->as_CallStaticJava()->_name;
3619N/A if (name != NULL && strcmp(name, "uncommon_trap") == 0)
3619N/A return; // Skip uncommon traps
3619N/A }
3619N/A // Don't mark as processed since call's arguments have to be processed.
3619N/A delayed_worklist->push(n);
3619N/A // Check if a call returns an object.
3619N/A if (n->as_Call()->returns_pointer() &&
3619N/A n->as_Call()->proj_out(TypeFunc::Parms) != NULL) {
3619N/A add_call_node(n->as_Call());
3619N/A }
3619N/A }
3619N/A return;
3619N/A }
3619N/A // Put this check here to process call arguments since some call nodes
3619N/A // point to phantom_obj.
3619N/A if (n_ptn == phantom_obj || n_ptn == null_obj)
3619N/A return; // Skip predefined nodes.
3619N/A
3619N/A int opcode = n->Opcode();
3619N/A switch (opcode) {
3619N/A case Op_AddP: {
3619N/A Node* base = get_addp_base(n);
3619N/A PointsToNode* ptn_base = ptnode_adr(base->_idx);
3619N/A // Field nodes are created for all field types. They are used in
3619N/A // adjust_scalar_replaceable_state() and split_unique_types().
3619N/A // Note, non-oop fields will have only base edges in Connection
3619N/A // Graph because such fields are not used for oop loads and stores.
3619N/A int offset = address_offset(n, igvn);
3619N/A add_field(n, PointsToNode::NoEscape, offset);
3619N/A if (ptn_base == NULL) {
3619N/A delayed_worklist->push(n); // Process it later.
3619N/A } else {
3619N/A n_ptn = ptnode_adr(n_idx);
3619N/A add_base(n_ptn->as_Field(), ptn_base);
3619N/A }
3619N/A break;
3619N/A }
3619N/A case Op_CastX2P: {
3619N/A map_ideal_node(n, phantom_obj);
3619N/A break;
3619N/A }
3619N/A case Op_CastPP:
3619N/A case Op_CheckCastPP:
3619N/A case Op_EncodeP:
3619N/A case Op_DecodeN: {
3619N/A add_local_var_and_edge(n, PointsToNode::NoEscape,
3619N/A n->in(1), delayed_worklist);
3619N/A break;
3619N/A }
3619N/A case Op_CMoveP: {
3619N/A add_local_var(n, PointsToNode::NoEscape);
3619N/A // Do not add edges during first iteration because some could be
3619N/A // not defined yet.
3619N/A delayed_worklist->push(n);
3619N/A break;
3619N/A }
3619N/A case Op_ConP:
3619N/A case Op_ConN: {
3619N/A // assume all oop constants globally escape except for null
3619N/A PointsToNode::EscapeState es;
3619N/A if (igvn->type(n) == TypePtr::NULL_PTR ||
3619N/A igvn->type(n) == TypeNarrowOop::NULL_PTR) {
3619N/A es = PointsToNode::NoEscape;
3619N/A } else {
3619N/A es = PointsToNode::GlobalEscape;
3619N/A }
3619N/A add_java_object(n, es);
3619N/A break;
3619N/A }
3619N/A case Op_CreateEx: {
3619N/A // assume that all exception objects globally escape
3619N/A add_java_object(n, PointsToNode::GlobalEscape);
3619N/A break;
3619N/A }
3619N/A case Op_LoadKlass:
3619N/A case Op_LoadNKlass: {
3619N/A // Unknown class is loaded
3619N/A map_ideal_node(n, phantom_obj);
3619N/A break;
3619N/A }
3619N/A case Op_LoadP:
3619N/A case Op_LoadN:
3619N/A case Op_LoadPLocked: {
4015N/A add_objload_to_connection_graph(n, delayed_worklist);
3619N/A break;
3619N/A }
3619N/A case Op_Parm: {
3619N/A map_ideal_node(n, phantom_obj);
3619N/A break;
3619N/A }
3619N/A case Op_PartialSubtypeCheck: {
3619N/A // Produces Null or notNull and is used in only in CmpP so
3619N/A // phantom_obj could be used.
3619N/A map_ideal_node(n, phantom_obj); // Result is unknown
3619N/A break;
3619N/A }
3619N/A case Op_Phi: {
3619N/A // Using isa_ptr() instead of isa_oopptr() for LoadP and Phi because
4015N/A // ThreadLocal has RawPtr type.
3619N/A const Type* t = n->as_Phi()->type();
3619N/A if (t->make_ptr() != NULL) {
3619N/A add_local_var(n, PointsToNode::NoEscape);
3619N/A // Do not add edges during first iteration because some could be
3619N/A // not defined yet.
3619N/A delayed_worklist->push(n);
3619N/A }
3619N/A break;
3619N/A }
3619N/A case Op_Proj: {
3619N/A // we are only interested in the oop result projection from a call
3619N/A if (n->as_Proj()->_con == TypeFunc::Parms && n->in(0)->is_Call() &&
3619N/A n->in(0)->as_Call()->returns_pointer()) {
3619N/A add_local_var_and_edge(n, PointsToNode::NoEscape,
3619N/A n->in(0), delayed_worklist);
3619N/A }
3619N/A break;
3619N/A }
3619N/A case Op_Rethrow: // Exception object escapes
3619N/A case Op_Return: {
3619N/A if (n->req() > TypeFunc::Parms &&
3619N/A igvn->type(n->in(TypeFunc::Parms))->isa_oopptr()) {
3619N/A // Treat Return value as LocalVar with GlobalEscape escape state.
3619N/A add_local_var_and_edge(n, PointsToNode::GlobalEscape,
3619N/A n->in(TypeFunc::Parms), delayed_worklist);
3619N/A }
3619N/A break;
3619N/A }
4015N/A case Op_GetAndSetP:
4015N/A case Op_GetAndSetN: {
4015N/A add_objload_to_connection_graph(n, delayed_worklist);
4015N/A // fallthrough
4015N/A }
3619N/A case Op_StoreP:
3619N/A case Op_StoreN:
3619N/A case Op_StorePConditional:
3619N/A case Op_CompareAndSwapP:
3619N/A case Op_CompareAndSwapN: {
3619N/A Node* adr = n->in(MemNode::Address);
3619N/A const Type *adr_type = igvn->type(adr);
3619N/A adr_type = adr_type->make_ptr();
4505N/A if (adr_type == NULL) {
4505N/A break; // skip dead nodes
4505N/A }
3619N/A if (adr_type->isa_oopptr() ||
3619N/A (opcode == Op_StoreP || opcode == Op_StoreN) &&
3619N/A (adr_type == TypeRawPtr::NOTNULL &&
3619N/A adr->in(AddPNode::Address)->is_Proj() &&
3619N/A adr->in(AddPNode::Address)->in(0)->is_Allocate())) {
3619N/A delayed_worklist->push(n); // Process it later.
3619N/A#ifdef ASSERT
3619N/A assert(adr->is_AddP(), "expecting an AddP");
3619N/A if (adr_type == TypeRawPtr::NOTNULL) {
3619N/A // Verify a raw address for a store captured by Initialize node.
3619N/A int offs = (int)igvn->find_intptr_t_con(adr->in(AddPNode::Offset), Type::OffsetBot);
3619N/A assert(offs != Type::OffsetBot, "offset must be a constant");
3619N/A }
3625N/A#endif
3619N/A } else {
3619N/A // Ignore copy the displaced header to the BoxNode (OSR compilation).
3619N/A if (adr->is_BoxLock())
3619N/A break;
3625N/A // Stored value escapes in unsafe access.
3625N/A if ((opcode == Op_StoreP) && (adr_type == TypeRawPtr::BOTTOM)) {
3625N/A // Pointer stores in G1 barriers looks like unsafe access.
3625N/A // Ignore such stores to be able scalar replace non-escaping
3625N/A // allocations.
3625N/A if (UseG1GC && adr->is_AddP()) {
3625N/A Node* base = get_addp_base(adr);
3625N/A if (base->Opcode() == Op_LoadP &&
3625N/A base->in(MemNode::Address)->is_AddP()) {
3625N/A adr = base->in(MemNode::Address);
3625N/A Node* tls = get_addp_base(adr);
3625N/A if (tls->Opcode() == Op_ThreadLocal) {
3625N/A int offs = (int)igvn->find_intptr_t_con(adr->in(AddPNode::Offset), Type::OffsetBot);
3625N/A if (offs == in_bytes(JavaThread::satb_mark_queue_offset() +
3625N/A PtrQueue::byte_offset_of_buf())) {
3625N/A break; // G1 pre barier previous oop value store.
3625N/A }
3625N/A if (offs == in_bytes(JavaThread::dirty_card_queue_offset() +
3625N/A PtrQueue::byte_offset_of_buf())) {
3625N/A break; // G1 post barier card address store.
3625N/A }
3625N/A }
3625N/A }
3625N/A }
3625N/A delayed_worklist->push(n); // Process unsafe access later.
3625N/A break;
3619N/A }
3625N/A#ifdef ASSERT
3625N/A n->dump(1);
3625N/A assert(false, "not unsafe or G1 barrier raw StoreP");
3619N/A#endif
3619N/A }
3619N/A break;
3619N/A }
3619N/A case Op_AryEq:
3619N/A case Op_StrComp:
3619N/A case Op_StrEquals:
3619N/A case Op_StrIndexOf: {
3619N/A add_local_var(n, PointsToNode::ArgEscape);
3619N/A delayed_worklist->push(n); // Process it later.
3619N/A break;
3619N/A }
3619N/A case Op_ThreadLocal: {
3619N/A add_java_object(n, PointsToNode::ArgEscape);
3619N/A break;
3619N/A }
3619N/A default:
3619N/A ; // Do nothing for nodes not related to EA.
3619N/A }
3619N/A return;
3619N/A}
3619N/A
3619N/A#ifdef ASSERT
3619N/A#define ELSE_FAIL(name) \
3619N/A /* Should not be called for not pointer type. */ \
3619N/A n->dump(1); \
3619N/A assert(false, name); \
3619N/A break;
3619N/A#else
3619N/A#define ELSE_FAIL(name) \
3619N/A break;
3619N/A#endif
3619N/A
3619N/A// Add final simple edges to graph.
3619N/Avoid ConnectionGraph::add_final_edges(Node *n) {
3619N/A PointsToNode* n_ptn = ptnode_adr(n->_idx);
3619N/A#ifdef ASSERT
3619N/A if (_verify && n_ptn->is_JavaObject())
3619N/A return; // This method does not change graph for JavaObject.
3619N/A#endif
3619N/A
3619N/A if (n->is_Call()) {
3619N/A process_call_arguments(n->as_Call());
3619N/A return;
3619N/A }
3619N/A assert(n->is_Store() || n->is_LoadStore() ||
3619N/A (n_ptn != NULL) && (n_ptn->ideal_node() != NULL),
3619N/A "node should be registered already");
3619N/A int opcode = n->Opcode();
3619N/A switch (opcode) {
3619N/A case Op_AddP: {
3619N/A Node* base = get_addp_base(n);
3619N/A PointsToNode* ptn_base = ptnode_adr(base->_idx);
3619N/A assert(ptn_base != NULL, "field's base should be registered");
3619N/A add_base(n_ptn->as_Field(), ptn_base);
3619N/A break;
3619N/A }
3619N/A case Op_CastPP:
3619N/A case Op_CheckCastPP:
3619N/A case Op_EncodeP:
3619N/A case Op_DecodeN: {
3619N/A add_local_var_and_edge(n, PointsToNode::NoEscape,
3619N/A n->in(1), NULL);
3619N/A break;
3619N/A }
3619N/A case Op_CMoveP: {
3619N/A for (uint i = CMoveNode::IfFalse; i < n->req(); i++) {
3619N/A Node* in = n->in(i);
3619N/A if (in == NULL)
3619N/A continue; // ignore NULL
3619N/A Node* uncast_in = in->uncast();
3619N/A if (uncast_in->is_top() || uncast_in == n)
3619N/A continue; // ignore top or inputs which go back this node
3619N/A PointsToNode* ptn = ptnode_adr(in->_idx);
3619N/A assert(ptn != NULL, "node should be registered");
3619N/A add_edge(n_ptn, ptn);
3619N/A }
3619N/A break;
3619N/A }
3619N/A case Op_LoadP:
3619N/A case Op_LoadN:
3619N/A case Op_LoadPLocked: {
3619N/A // Using isa_ptr() instead of isa_oopptr() for LoadP and Phi because
4015N/A // ThreadLocal has RawPtr type.
3619N/A const Type* t = _igvn->type(n);
3619N/A if (t->make_ptr() != NULL) {
3619N/A Node* adr = n->in(MemNode::Address);
3619N/A add_local_var_and_edge(n, PointsToNode::NoEscape, adr, NULL);
3619N/A break;
3619N/A }
3619N/A ELSE_FAIL("Op_LoadP");
3619N/A }
3619N/A case Op_Phi: {
3619N/A // Using isa_ptr() instead of isa_oopptr() for LoadP and Phi because
4015N/A // ThreadLocal has RawPtr type.
3619N/A const Type* t = n->as_Phi()->type();
3619N/A if (t->make_ptr() != NULL) {
3619N/A for (uint i = 1; i < n->req(); i++) {
3619N/A Node* in = n->in(i);
3619N/A if (in == NULL)
3619N/A continue; // ignore NULL
3619N/A Node* uncast_in = in->uncast();
3619N/A if (uncast_in->is_top() || uncast_in == n)
3619N/A continue; // ignore top or inputs which go back this node
3619N/A PointsToNode* ptn = ptnode_adr(in->_idx);
3619N/A assert(ptn != NULL, "node should be registered");
3619N/A add_edge(n_ptn, ptn);
3619N/A }
3619N/A break;
3619N/A }
3619N/A ELSE_FAIL("Op_Phi");
3619N/A }
3619N/A case Op_Proj: {
3619N/A // we are only interested in the oop result projection from a call
3619N/A if (n->as_Proj()->_con == TypeFunc::Parms && n->in(0)->is_Call() &&
3619N/A n->in(0)->as_Call()->returns_pointer()) {
3619N/A add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(0), NULL);
3619N/A break;
3619N/A }
3619N/A ELSE_FAIL("Op_Proj");
3619N/A }
3619N/A case Op_Rethrow: // Exception object escapes
3619N/A case Op_Return: {
3619N/A if (n->req() > TypeFunc::Parms &&
3619N/A _igvn->type(n->in(TypeFunc::Parms))->isa_oopptr()) {
3619N/A // Treat Return value as LocalVar with GlobalEscape escape state.
3619N/A add_local_var_and_edge(n, PointsToNode::GlobalEscape,
3619N/A n->in(TypeFunc::Parms), NULL);
3619N/A break;
3619N/A }
3619N/A ELSE_FAIL("Op_Return");
3619N/A }
3619N/A case Op_StoreP:
3619N/A case Op_StoreN:
3619N/A case Op_StorePConditional:
3619N/A case Op_CompareAndSwapP:
4015N/A case Op_CompareAndSwapN:
4015N/A case Op_GetAndSetP:
4015N/A case Op_GetAndSetN: {
3619N/A Node* adr = n->in(MemNode::Address);
3619N/A const Type *adr_type = _igvn->type(adr);
3619N/A adr_type = adr_type->make_ptr();
4505N/A#ifdef ASSERT
4505N/A if (adr_type == NULL) {
4505N/A n->dump(1);
4505N/A assert(adr_type != NULL, "dead node should not be on list");
4505N/A break;
4505N/A }
4505N/A#endif
4505N/A if (opcode == Op_GetAndSetP || opcode == Op_GetAndSetN) {
4505N/A add_local_var_and_edge(n, PointsToNode::NoEscape, adr, NULL);
4505N/A }
3619N/A if (adr_type->isa_oopptr() ||
3619N/A (opcode == Op_StoreP || opcode == Op_StoreN) &&
3619N/A (adr_type == TypeRawPtr::NOTNULL &&
3619N/A adr->in(AddPNode::Address)->is_Proj() &&
3619N/A adr->in(AddPNode::Address)->in(0)->is_Allocate())) {
3619N/A // Point Address to Value
3619N/A PointsToNode* adr_ptn = ptnode_adr(adr->_idx);
3619N/A assert(adr_ptn != NULL &&
3619N/A adr_ptn->as_Field()->is_oop(), "node should be registered");
3619N/A Node *val = n->in(MemNode::ValueIn);
3619N/A PointsToNode* ptn = ptnode_adr(val->_idx);
3619N/A assert(ptn != NULL, "node should be registered");
3619N/A add_edge(adr_ptn, ptn);
3619N/A break;
3625N/A } else if ((opcode == Op_StoreP) && (adr_type == TypeRawPtr::BOTTOM)) {
3625N/A // Stored value escapes in unsafe access.
3625N/A Node *val = n->in(MemNode::ValueIn);
3625N/A PointsToNode* ptn = ptnode_adr(val->_idx);
3625N/A assert(ptn != NULL, "node should be registered");
3625N/A ptn->set_escape_state(PointsToNode::GlobalEscape);
3625N/A // Add edge to object for unsafe access with offset.
3625N/A PointsToNode* adr_ptn = ptnode_adr(adr->_idx);
3625N/A assert(adr_ptn != NULL, "node should be registered");
3625N/A if (adr_ptn->is_Field()) {
3625N/A assert(adr_ptn->as_Field()->is_oop(), "should be oop field");
3625N/A add_edge(adr_ptn, ptn);
3625N/A }
3625N/A break;
3619N/A }
3619N/A ELSE_FAIL("Op_StoreP");
3619N/A }
3619N/A case Op_AryEq:
3619N/A case Op_StrComp:
3619N/A case Op_StrEquals:
3619N/A case Op_StrIndexOf: {
3619N/A // char[] arrays passed to string intrinsic do not escape but
3619N/A // they are not scalar replaceable. Adjust escape state for them.
3619N/A // Start from in(2) edge since in(1) is memory edge.
3619N/A for (uint i = 2; i < n->req(); i++) {
3619N/A Node* adr = n->in(i);
3619N/A const Type* at = _igvn->type(adr);
3619N/A if (!adr->is_top() && at->isa_ptr()) {
3619N/A assert(at == Type::TOP || at == TypePtr::NULL_PTR ||
3619N/A at->isa_ptr() != NULL, "expecting a pointer");
3619N/A if (adr->is_AddP()) {
3619N/A adr = get_addp_base(adr);
3619N/A }
3619N/A PointsToNode* ptn = ptnode_adr(adr->_idx);
3619N/A assert(ptn != NULL, "node should be registered");
3619N/A add_edge(n_ptn, ptn);
3619N/A }
3619N/A }
3619N/A break;
3619N/A }
3619N/A default: {
3619N/A // This method should be called only for EA specific nodes which may
3619N/A // miss some edges when they were created.
3619N/A#ifdef ASSERT
3619N/A n->dump(1);
3619N/A#endif
3619N/A guarantee(false, "unknown node");
3619N/A }
3619N/A }
3619N/A return;
3619N/A}
3619N/A
3619N/Avoid ConnectionGraph::add_call_node(CallNode* call) {
3619N/A assert(call->returns_pointer(), "only for call which returns pointer");
3619N/A uint call_idx = call->_idx;
3619N/A if (call->is_Allocate()) {
3619N/A Node* k = call->in(AllocateNode::KlassNode);
3619N/A const TypeKlassPtr* kt = k->bottom_type()->isa_klassptr();
3619N/A assert(kt != NULL, "TypeKlassPtr required.");
3619N/A ciKlass* cik = kt->klass();
3619N/A PointsToNode::EscapeState es = PointsToNode::NoEscape;
3619N/A bool scalar_replaceable = true;
3619N/A if (call->is_AllocateArray()) {
3619N/A if (!cik->is_array_klass()) { // StressReflectiveCode
3619N/A es = PointsToNode::GlobalEscape;
3619N/A } else {
3619N/A int length = call->in(AllocateNode::ALength)->find_int_con(-1);
3619N/A if (length < 0 || length > EliminateAllocationArraySizeLimit) {
3619N/A // Not scalar replaceable if the length is not constant or too big.
3619N/A scalar_replaceable = false;
3619N/A }
3619N/A }
3619N/A } else { // Allocate instance
3619N/A if (cik->is_subclass_of(_compile->env()->Thread_klass()) ||
3619N/A !cik->is_instance_klass() || // StressReflectiveCode
3619N/A cik->as_instance_klass()->has_finalizer()) {
3619N/A es = PointsToNode::GlobalEscape;
3619N/A }
3619N/A }
3619N/A add_java_object(call, es);
3619N/A PointsToNode* ptn = ptnode_adr(call_idx);
3619N/A if (!scalar_replaceable && ptn->scalar_replaceable()) {
3619N/A ptn->set_scalar_replaceable(false);
3619N/A }
3619N/A } else if (call->is_CallStaticJava()) {
3619N/A // Call nodes could be different types:
3619N/A //
3619N/A // 1. CallDynamicJavaNode (what happened during call is unknown):
3619N/A //
3619N/A // - mapped to GlobalEscape JavaObject node if oop is returned;
3619N/A //
3619N/A // - all oop arguments are escaping globally;
3619N/A //
3619N/A // 2. CallStaticJavaNode (execute bytecode analysis if possible):
3619N/A //
3619N/A // - the same as CallDynamicJavaNode if can't do bytecode analysis;
3619N/A //
3619N/A // - mapped to GlobalEscape JavaObject node if unknown oop is returned;
3619N/A // - mapped to NoEscape JavaObject node if non-escaping object allocated
3619N/A // during call is returned;
3619N/A // - mapped to ArgEscape LocalVar node pointed to object arguments
3619N/A // which are returned and does not escape during call;
3619N/A //
3619N/A // - oop arguments escaping status is defined by bytecode analysis;
3619N/A //
3619N/A // For a static call, we know exactly what method is being called.
3619N/A // Use bytecode estimator to record whether the call's return value escapes.
3619N/A ciMethod* meth = call->as_CallJava()->method();
3619N/A if (meth == NULL) {
3619N/A const char* name = call->as_CallStaticJava()->_name;
3619N/A assert(strncmp(name, "_multianewarray", 15) == 0, "TODO: add failed case check");
3619N/A // Returns a newly allocated unescaped object.
3619N/A add_java_object(call, PointsToNode::NoEscape);
3619N/A ptnode_adr(call_idx)->set_scalar_replaceable(false);
2965N/A } else {
3619N/A BCEscapeAnalyzer* call_analyzer = meth->get_bcea();
3619N/A call_analyzer->copy_dependencies(_compile->dependencies());
3619N/A if (call_analyzer->is_return_allocated()) {
3619N/A // Returns a newly allocated unescaped object, simply
3619N/A // update dependency information.
3619N/A // Mark it as NoEscape so that objects referenced by
3619N/A // it's fields will be marked as NoEscape at least.
3619N/A add_java_object(call, PointsToNode::NoEscape);
3619N/A ptnode_adr(call_idx)->set_scalar_replaceable(false);
3619N/A } else {
3619N/A // Determine whether any arguments are returned.
3619N/A const TypeTuple* d = call->tf()->domain();
3619N/A bool ret_arg = false;
3619N/A for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
3619N/A if (d->field_at(i)->isa_ptr() != NULL &&
3619N/A call_analyzer->is_arg_returned(i - TypeFunc::Parms)) {
3619N/A ret_arg = true;
3619N/A break;
3619N/A }
3619N/A }
3619N/A if (ret_arg) {
3619N/A add_local_var(call, PointsToNode::ArgEscape);
3619N/A } else {
3619N/A // Returns unknown object.
3619N/A map_ideal_node(call, phantom_obj);
3619N/A }
3619N/A }
3619N/A }
3619N/A } else {
3619N/A // An other type of call, assume the worst case:
3619N/A // returned value is unknown and globally escapes.
3619N/A assert(call->Opcode() == Op_CallDynamicJava, "add failed case check");
3619N/A map_ideal_node(call, phantom_obj);
3619N/A }
3619N/A}
3619N/A
3619N/Avoid ConnectionGraph::process_call_arguments(CallNode *call) {
3619N/A bool is_arraycopy = false;
3619N/A switch (call->Opcode()) {
3619N/A#ifdef ASSERT
3619N/A case Op_Allocate:
3619N/A case Op_AllocateArray:
3619N/A case Op_Lock:
3619N/A case Op_Unlock:
3619N/A assert(false, "should be done already");
3619N/A break;
3619N/A#endif
3619N/A case Op_CallLeafNoFP:
3619N/A is_arraycopy = (call->as_CallLeaf()->_name != NULL &&
3619N/A strstr(call->as_CallLeaf()->_name, "arraycopy") != 0);
3619N/A // fall through
3619N/A case Op_CallLeaf: {
3619N/A // Stub calls, objects do not escape but they are not scale replaceable.
3619N/A // Adjust escape state for outgoing arguments.
3619N/A const TypeTuple * d = call->tf()->domain();
3619N/A bool src_has_oops = false;
3619N/A for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
3619N/A const Type* at = d->field_at(i);
3619N/A Node *arg = call->in(i);
3619N/A const Type *aat = _igvn->type(arg);
3619N/A if (arg->is_top() || !at->isa_ptr() || !aat->isa_ptr())
3619N/A continue;
3619N/A if (arg->is_AddP()) {
3619N/A //
3619N/A // The inline_native_clone() case when the arraycopy stub is called
3619N/A // after the allocation before Initialize and CheckCastPP nodes.
3619N/A // Or normal arraycopy for object arrays case.
3619N/A //
3619N/A // Set AddP's base (Allocate) as not scalar replaceable since
3619N/A // pointer to the base (with offset) is passed as argument.
3619N/A //
3619N/A arg = get_addp_base(arg);
3619N/A }
3619N/A PointsToNode* arg_ptn = ptnode_adr(arg->_idx);
3619N/A assert(arg_ptn != NULL, "should be registered");
3619N/A PointsToNode::EscapeState arg_esc = arg_ptn->escape_state();
3619N/A if (is_arraycopy || arg_esc < PointsToNode::ArgEscape) {
3619N/A assert(aat == Type::TOP || aat == TypePtr::NULL_PTR ||
3619N/A aat->isa_ptr() != NULL, "expecting an Ptr");
3619N/A bool arg_has_oops = aat->isa_oopptr() &&
3619N/A (aat->isa_oopptr()->klass() == NULL || aat->isa_instptr() ||
3619N/A (aat->isa_aryptr() && aat->isa_aryptr()->klass()->is_obj_array_klass()));
3619N/A if (i == TypeFunc::Parms) {
3619N/A src_has_oops = arg_has_oops;
3619N/A }
3619N/A //
3619N/A // src or dst could be j.l.Object when other is basic type array:
3619N/A //
3619N/A // arraycopy(char[],0,Object*,0,size);
3619N/A // arraycopy(Object*,0,char[],0,size);
3619N/A //
3619N/A // Don't add edges in such cases.
3619N/A //
3619N/A bool arg_is_arraycopy_dest = src_has_oops && is_arraycopy &&
3619N/A arg_has_oops && (i > TypeFunc::Parms);
3619N/A#ifdef ASSERT
3619N/A if (!(is_arraycopy ||
4056N/A (call->as_CallLeaf()->_name != NULL &&
4056N/A (strcmp(call->as_CallLeaf()->_name, "g1_wb_pre") == 0 ||
4056N/A strcmp(call->as_CallLeaf()->_name, "g1_wb_post") == 0 ||
4056N/A strcmp(call->as_CallLeaf()->_name, "aescrypt_encryptBlock") == 0 ||
4056N/A strcmp(call->as_CallLeaf()->_name, "aescrypt_decryptBlock") == 0 ||
4056N/A strcmp(call->as_CallLeaf()->_name, "cipherBlockChaining_encryptAESCrypt") == 0 ||
4056N/A strcmp(call->as_CallLeaf()->_name, "cipherBlockChaining_decryptAESCrypt") == 0)
4056N/A ))) {
3619N/A call->dump();
4056N/A fatal(err_msg_res("EA unexpected CallLeaf %s", call->as_CallLeaf()->_name));
3619N/A }
3619N/A#endif
3619N/A // Always process arraycopy's destination object since
3619N/A // we need to add all possible edges to references in
3619N/A // source object.
3619N/A if (arg_esc >= PointsToNode::ArgEscape &&
3619N/A !arg_is_arraycopy_dest) {
3619N/A continue;
3619N/A }
3619N/A set_escape_state(arg_ptn, PointsToNode::ArgEscape);
3619N/A if (arg_is_arraycopy_dest) {
3619N/A Node* src = call->in(TypeFunc::Parms);
3619N/A if (src->is_AddP()) {
3619N/A src = get_addp_base(src);
3619N/A }
3619N/A PointsToNode* src_ptn = ptnode_adr(src->_idx);
3619N/A assert(src_ptn != NULL, "should be registered");
3619N/A if (arg_ptn != src_ptn) {
3619N/A // Special arraycopy edge:
3619N/A // A destination object's field can't have the source object
3619N/A // as base since objects escape states are not related.
3619N/A // Only escape state of destination object's fields affects
3619N/A // escape state of fields in source object.
3619N/A add_arraycopy(call, PointsToNode::ArgEscape, src_ptn, arg_ptn);
3619N/A }
3619N/A }
3619N/A }
3619N/A }
3619N/A break;
3619N/A }
3619N/A case Op_CallStaticJava: {
3619N/A // For a static call, we know exactly what method is being called.
3619N/A // Use bytecode estimator to record the call's escape affects
3619N/A#ifdef ASSERT
3619N/A const char* name = call->as_CallStaticJava()->_name;
3619N/A assert((name == NULL || strcmp(name, "uncommon_trap") != 0), "normal calls only");
3619N/A#endif
3619N/A ciMethod* meth = call->as_CallJava()->method();
3619N/A BCEscapeAnalyzer* call_analyzer = (meth !=NULL) ? meth->get_bcea() : NULL;
3619N/A // fall-through if not a Java method or no analyzer information
3619N/A if (call_analyzer != NULL) {
3619N/A PointsToNode* call_ptn = ptnode_adr(call->_idx);
3619N/A const TypeTuple* d = call->tf()->domain();
3619N/A for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
3619N/A const Type* at = d->field_at(i);
3619N/A int k = i - TypeFunc::Parms;
3619N/A Node* arg = call->in(i);
3619N/A PointsToNode* arg_ptn = ptnode_adr(arg->_idx);
3619N/A if (at->isa_ptr() != NULL &&
3619N/A call_analyzer->is_arg_returned(k)) {
3619N/A // The call returns arguments.
3619N/A if (call_ptn != NULL) { // Is call's result used?
3619N/A assert(call_ptn->is_LocalVar(), "node should be registered");
3619N/A assert(arg_ptn != NULL, "node should be registered");
3619N/A add_edge(call_ptn, arg_ptn);
3619N/A }
3619N/A }
3619N/A if (at->isa_oopptr() != NULL &&
3619N/A arg_ptn->escape_state() < PointsToNode::GlobalEscape) {
3619N/A if (!call_analyzer->is_arg_stack(k)) {
3619N/A // The argument global escapes
3619N/A set_escape_state(arg_ptn, PointsToNode::GlobalEscape);
3619N/A } else {
3619N/A set_escape_state(arg_ptn, PointsToNode::ArgEscape);
3619N/A if (!call_analyzer->is_arg_local(k)) {
3619N/A // The argument itself doesn't escape, but any fields might
3619N/A set_fields_escape_state(arg_ptn, PointsToNode::GlobalEscape);
3619N/A }
3619N/A }
3619N/A }
3619N/A }
3619N/A if (call_ptn != NULL && call_ptn->is_LocalVar()) {
3619N/A // The call returns arguments.
3619N/A assert(call_ptn->edge_count() > 0, "sanity");
3619N/A if (!call_analyzer->is_return_local()) {
3619N/A // Returns also unknown object.
3619N/A add_edge(call_ptn, phantom_obj);
3619N/A }
3619N/A }
3619N/A break;
3619N/A }
3619N/A }
3619N/A default: {
3619N/A // Fall-through here if not a Java method or no analyzer information
3619N/A // or some other type of call, assume the worst case: all arguments
3619N/A // globally escape.
3619N/A const TypeTuple* d = call->tf()->domain();
3619N/A for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
3619N/A const Type* at = d->field_at(i);
3619N/A if (at->isa_oopptr() != NULL) {
3619N/A Node* arg = call->in(i);
3619N/A if (arg->is_AddP()) {
3619N/A arg = get_addp_base(arg);
3619N/A }
3619N/A assert(ptnode_adr(arg->_idx) != NULL, "should be defined already");
3619N/A set_escape_state(ptnode_adr(arg->_idx), PointsToNode::GlobalEscape);
3619N/A }
3619N/A }
2965N/A }
2965N/A }
3619N/A}
3619N/A
3619N/A
3619N/A// Finish Graph construction.
3619N/Abool ConnectionGraph::complete_connection_graph(
3619N/A GrowableArray<PointsToNode*>& ptnodes_worklist,
3619N/A GrowableArray<JavaObjectNode*>& non_escaped_worklist,
3619N/A GrowableArray<JavaObjectNode*>& java_objects_worklist,
3619N/A GrowableArray<FieldNode*>& oop_fields_worklist) {
3619N/A // Normally only 1-3 passes needed to build Connection Graph depending
3619N/A // on graph complexity. Observed 8 passes in jvm2008 compiler.compiler.
3619N/A // Set limit to 20 to catch situation when something did go wrong and
3619N/A // bailout Escape Analysis.
3619N/A // Also limit build time to 30 sec (60 in debug VM).
3619N/A#define CG_BUILD_ITER_LIMIT 20
3619N/A#ifdef ASSERT
3619N/A#define CG_BUILD_TIME_LIMIT 60.0
3619N/A#else
3619N/A#define CG_BUILD_TIME_LIMIT 30.0
3619N/A#endif
3619N/A
3619N/A // Propagate GlobalEscape and ArgEscape escape states and check that
3619N/A // we still have non-escaping objects. The method pushs on _worklist
3619N/A // Field nodes which reference phantom_object.
3619N/A if (!find_non_escaped_objects(ptnodes_worklist, non_escaped_worklist)) {
3619N/A return false; // Nothing to do.
3619N/A }
3619N/A // Now propagate references to all JavaObject nodes.
3619N/A int java_objects_length = java_objects_worklist.length();
3619N/A elapsedTimer time;
3619N/A int new_edges = 1;
3619N/A int iterations = 0;
3619N/A do {
3619N/A while ((new_edges > 0) &&
3619N/A (iterations++ < CG_BUILD_ITER_LIMIT) &&
3619N/A (time.seconds() < CG_BUILD_TIME_LIMIT)) {
3619N/A time.start();
3619N/A new_edges = 0;
3619N/A // Propagate references to phantom_object for nodes pushed on _worklist
3619N/A // by find_non_escaped_objects() and find_field_value().
3619N/A new_edges += add_java_object_edges(phantom_obj, false);
3619N/A for (int next = 0; next < java_objects_length; ++next) {
3619N/A JavaObjectNode* ptn = java_objects_worklist.at(next);
3619N/A new_edges += add_java_object_edges(ptn, true);
3619N/A }
3619N/A if (new_edges > 0) {
3619N/A // Update escape states on each iteration if graph was updated.
3619N/A if (!find_non_escaped_objects(ptnodes_worklist, non_escaped_worklist)) {
3619N/A return false; // Nothing to do.
3619N/A }
3619N/A }
3619N/A time.stop();
3619N/A }
3619N/A if ((iterations < CG_BUILD_ITER_LIMIT) &&
3619N/A (time.seconds() < CG_BUILD_TIME_LIMIT)) {
3619N/A time.start();
3619N/A // Find fields which have unknown value.
3619N/A int fields_length = oop_fields_worklist.length();
3619N/A for (int next = 0; next < fields_length; next++) {
3619N/A FieldNode* field = oop_fields_worklist.at(next);
3619N/A if (field->edge_count() == 0) {
3619N/A new_edges += find_field_value(field);
3619N/A // This code may added new edges to phantom_object.
3619N/A // Need an other cycle to propagate references to phantom_object.
3619N/A }
3619N/A }
3619N/A time.stop();
3619N/A } else {
3619N/A new_edges = 0; // Bailout
3619N/A }
3619N/A } while (new_edges > 0);
3619N/A
3619N/A // Bailout if passed limits.
3619N/A if ((iterations >= CG_BUILD_ITER_LIMIT) ||
3619N/A (time.seconds() >= CG_BUILD_TIME_LIMIT)) {
3619N/A Compile* C = _compile;
3619N/A if (C->log() != NULL) {
3619N/A C->log()->begin_elem("connectionGraph_bailout reason='reached ");
3619N/A C->log()->text("%s", (iterations >= CG_BUILD_ITER_LIMIT) ? "iterations" : "time");
3619N/A C->log()->end_elem(" limit'");
3619N/A }
4040N/A assert(ExitEscapeAnalysisOnTimeout, err_msg_res("infinite EA connection graph build (%f sec, %d iterations) with %d nodes and worklist size %d",
3619N/A time.seconds(), iterations, nodes_size(), ptnodes_worklist.length()));
3619N/A // Possible infinite build_connection_graph loop,
3619N/A // bailout (no changes to ideal graph were made).
3619N/A return false;
3619N/A }
3619N/A#ifdef ASSERT
3619N/A if (Verbose && PrintEscapeAnalysis) {
3619N/A tty->print_cr("EA: %d iterations to build connection graph with %d nodes and worklist size %d",
3619N/A iterations, nodes_size(), ptnodes_worklist.length());
3619N/A }
3619N/A#endif
3619N/A
3619N/A#undef CG_BUILD_ITER_LIMIT
3619N/A#undef CG_BUILD_TIME_LIMIT
3619N/A
3619N/A // Find fields initialized by NULL for non-escaping Allocations.
3619N/A int non_escaped_length = non_escaped_worklist.length();
3619N/A for (int next = 0; next < non_escaped_length; next++) {
3619N/A JavaObjectNode* ptn = non_escaped_worklist.at(next);
3619N/A PointsToNode::EscapeState es = ptn->escape_state();
3619N/A assert(es <= PointsToNode::ArgEscape, "sanity");
3619N/A if (es == PointsToNode::NoEscape) {
3619N/A if (find_init_values(ptn, null_obj, _igvn) > 0) {
3619N/A // Adding references to NULL object does not change escape states
3619N/A // since it does not escape. Also no fields are added to NULL object.
3619N/A add_java_object_edges(null_obj, false);
3619N/A }
3619N/A }
3619N/A Node* n = ptn->ideal_node();
3619N/A if (n->is_Allocate()) {
3619N/A // The object allocated by this Allocate node will never be
3619N/A // seen by an other thread. Mark it so that when it is
3619N/A // expanded no MemBarStoreStore is added.
3619N/A InitializeNode* ini = n->as_Allocate()->initialization();
3619N/A if (ini != NULL)
3619N/A ini->set_does_not_escape();
3619N/A }
3619N/A }
3619N/A return true; // Finished graph construction.
3619N/A}
3619N/A
3619N/A// Propagate GlobalEscape and ArgEscape escape states to all nodes
3619N/A// and check that we still have non-escaping java objects.
3619N/Abool ConnectionGraph::find_non_escaped_objects(GrowableArray<PointsToNode*>& ptnodes_worklist,
3619N/A GrowableArray<JavaObjectNode*>& non_escaped_worklist) {
3619N/A GrowableArray<PointsToNode*> escape_worklist;
3619N/A // First, put all nodes with GlobalEscape and ArgEscape states on worklist.
3619N/A int ptnodes_length = ptnodes_worklist.length();
3619N/A for (int next = 0; next < ptnodes_length; ++next) {
3619N/A PointsToNode* ptn = ptnodes_worklist.at(next);
3619N/A if (ptn->escape_state() >= PointsToNode::ArgEscape ||
3619N/A ptn->fields_escape_state() >= PointsToNode::ArgEscape) {
3619N/A escape_worklist.push(ptn);
3619N/A }
3619N/A }
3619N/A // Set escape states to referenced nodes (edges list).
3619N/A while (escape_worklist.length() > 0) {
3619N/A PointsToNode* ptn = escape_worklist.pop();
3619N/A PointsToNode::EscapeState es = ptn->escape_state();
3619N/A PointsToNode::EscapeState field_es = ptn->fields_escape_state();
3619N/A if (ptn->is_Field() && ptn->as_Field()->is_oop() &&
3619N/A es >= PointsToNode::ArgEscape) {
3619N/A // GlobalEscape or ArgEscape state of field means it has unknown value.
3619N/A if (add_edge(ptn, phantom_obj)) {
3619N/A // New edge was added
3619N/A add_field_uses_to_worklist(ptn->as_Field());
3619N/A }
3619N/A }
3619N/A for (EdgeIterator i(ptn); i.has_next(); i.next()) {
3619N/A PointsToNode* e = i.get();
3619N/A if (e->is_Arraycopy()) {
3619N/A assert(ptn->arraycopy_dst(), "sanity");
3619N/A // Propagate only fields escape state through arraycopy edge.
3619N/A if (e->fields_escape_state() < field_es) {
3619N/A set_fields_escape_state(e, field_es);
3619N/A escape_worklist.push(e);
3619N/A }
3619N/A } else if (es >= field_es) {
3619N/A // fields_escape_state is also set to 'es' if it is less than 'es'.
3619N/A if (e->escape_state() < es) {
3619N/A set_escape_state(e, es);
3619N/A escape_worklist.push(e);
3619N/A }
3619N/A } else {
3619N/A // Propagate field escape state.
3619N/A bool es_changed = false;
3619N/A if (e->fields_escape_state() < field_es) {
3619N/A set_fields_escape_state(e, field_es);
3619N/A es_changed = true;
3619N/A }
3619N/A if ((e->escape_state() < field_es) &&
3619N/A e->is_Field() && ptn->is_JavaObject() &&
3619N/A e->as_Field()->is_oop()) {
3619N/A // Change escape state of referenced fileds.
3619N/A set_escape_state(e, field_es);
3619N/A es_changed = true;;
3619N/A } else if (e->escape_state() < es) {
3619N/A set_escape_state(e, es);
3619N/A es_changed = true;;
3619N/A }
3619N/A if (es_changed) {
3619N/A escape_worklist.push(e);
3619N/A }
3619N/A }
3619N/A }
3619N/A }
3619N/A // Remove escaped objects from non_escaped list.
3619N/A for (int next = non_escaped_worklist.length()-1; next >= 0 ; --next) {
3619N/A JavaObjectNode* ptn = non_escaped_worklist.at(next);
3619N/A if (ptn->escape_state() >= PointsToNode::GlobalEscape) {
3619N/A non_escaped_worklist.delete_at(next);
3619N/A }
3619N/A if (ptn->escape_state() == PointsToNode::NoEscape) {
3619N/A // Find fields in non-escaped allocations which have unknown value.
3619N/A find_init_values(ptn, phantom_obj, NULL);
3619N/A }
3619N/A }
3619N/A return (non_escaped_worklist.length() > 0);
3619N/A}
3619N/A
3619N/A// Add all references to JavaObject node by walking over all uses.
3619N/Aint ConnectionGraph::add_java_object_edges(JavaObjectNode* jobj, bool populate_worklist) {
3619N/A int new_edges = 0;
3619N/A if (populate_worklist) {
3619N/A // Populate _worklist by uses of jobj's uses.
3619N/A for (UseIterator i(jobj); i.has_next(); i.next()) {
3619N/A PointsToNode* use = i.get();
3619N/A if (use->is_Arraycopy())
3619N/A continue;
3619N/A add_uses_to_worklist(use);
3619N/A if (use->is_Field() && use->as_Field()->is_oop()) {
3619N/A // Put on worklist all field's uses (loads) and
3619N/A // related field nodes (same base and offset).
3619N/A add_field_uses_to_worklist(use->as_Field());
3619N/A }
3619N/A }
3619N/A }
3619N/A while(_worklist.length() > 0) {
3619N/A PointsToNode* use = _worklist.pop();
3619N/A if (PointsToNode::is_base_use(use)) {
3619N/A // Add reference from jobj to field and from field to jobj (field's base).
3619N/A use = PointsToNode::get_use_node(use)->as_Field();
3619N/A if (add_base(use->as_Field(), jobj)) {
3619N/A new_edges++;
3619N/A }
3619N/A continue;
3619N/A }
3619N/A assert(!use->is_JavaObject(), "sanity");
3619N/A if (use->is_Arraycopy()) {
3619N/A if (jobj == null_obj) // NULL object does not have field edges
3619N/A continue;
3619N/A // Added edge from Arraycopy node to arraycopy's source java object
3619N/A if (add_edge(use, jobj)) {
3619N/A jobj->set_arraycopy_src();
3619N/A new_edges++;
3619N/A }
3619N/A // and stop here.
3619N/A continue;
3619N/A }
3619N/A if (!add_edge(use, jobj))
3619N/A continue; // No new edge added, there was such edge already.
3619N/A new_edges++;
3619N/A if (use->is_LocalVar()) {
3619N/A add_uses_to_worklist(use);
3619N/A if (use->arraycopy_dst()) {
3619N/A for (EdgeIterator i(use); i.has_next(); i.next()) {
3619N/A PointsToNode* e = i.get();
3619N/A if (e->is_Arraycopy()) {
3619N/A if (jobj == null_obj) // NULL object does not have field edges
3619N/A continue;
3619N/A // Add edge from arraycopy's destination java object to Arraycopy node.
3619N/A if (add_edge(jobj, e)) {
3619N/A new_edges++;
3619N/A jobj->set_arraycopy_dst();
3619N/A }
3619N/A }
3619N/A }
3619N/A }
3619N/A } else {
3619N/A // Added new edge to stored in field values.
3619N/A // Put on worklist all field's uses (loads) and
3619N/A // related field nodes (same base and offset).
3619N/A add_field_uses_to_worklist(use->as_Field());
3619N/A }
3619N/A }
3619N/A return new_edges;
3619N/A}
3619N/A
3619N/A// Put on worklist all related field nodes.
3619N/Avoid ConnectionGraph::add_field_uses_to_worklist(FieldNode* field) {
3619N/A assert(field->is_oop(), "sanity");
3619N/A int offset = field->offset();
3619N/A add_uses_to_worklist(field);
3619N/A // Loop over all bases of this field and push on worklist Field nodes
3619N/A // with the same offset and base (since they may reference the same field).
3619N/A for (BaseIterator i(field); i.has_next(); i.next()) {
3619N/A PointsToNode* base = i.get();
3619N/A add_fields_to_worklist(field, base);
3619N/A // Check if the base was source object of arraycopy and go over arraycopy's
3619N/A // destination objects since values stored to a field of source object are
3619N/A // accessable by uses (loads) of fields of destination objects.
3619N/A if (base->arraycopy_src()) {
3619N/A for (UseIterator j(base); j.has_next(); j.next()) {
3619N/A PointsToNode* arycp = j.get();
3619N/A if (arycp->is_Arraycopy()) {
3619N/A for (UseIterator k(arycp); k.has_next(); k.next()) {
3619N/A PointsToNode* abase = k.get();
3619N/A if (abase->arraycopy_dst() && abase != base) {
3619N/A // Look for the same arracopy reference.
3619N/A add_fields_to_worklist(field, abase);
3619N/A }
3619N/A }
3619N/A }
3619N/A }
3619N/A }
3619N/A }
3619N/A}
3619N/A
3619N/A// Put on worklist all related field nodes.
3619N/Avoid ConnectionGraph::add_fields_to_worklist(FieldNode* field, PointsToNode* base) {
3619N/A int offset = field->offset();
3619N/A if (base->is_LocalVar()) {
3619N/A for (UseIterator j(base); j.has_next(); j.next()) {
3619N/A PointsToNode* f = j.get();
3619N/A if (PointsToNode::is_base_use(f)) { // Field
3619N/A f = PointsToNode::get_use_node(f);
3619N/A if (f == field || !f->as_Field()->is_oop())
3619N/A continue;
3619N/A int offs = f->as_Field()->offset();
3619N/A if (offs == offset || offset == Type::OffsetBot || offs == Type::OffsetBot) {
3619N/A add_to_worklist(f);
3619N/A }
3619N/A }
3619N/A }
3619N/A } else {
3619N/A assert(base->is_JavaObject(), "sanity");
3619N/A if (// Skip phantom_object since it is only used to indicate that
3619N/A // this field's content globally escapes.
3619N/A (base != phantom_obj) &&
3619N/A // NULL object node does not have fields.
3619N/A (base != null_obj)) {
3619N/A for (EdgeIterator i(base); i.has_next(); i.next()) {
3619N/A PointsToNode* f = i.get();
3619N/A // Skip arraycopy edge since store to destination object field
3619N/A // does not update value in source object field.
3619N/A if (f->is_Arraycopy()) {
3619N/A assert(base->arraycopy_dst(), "sanity");
3619N/A continue;
3619N/A }
3619N/A if (f == field || !f->as_Field()->is_oop())
3619N/A continue;
3619N/A int offs = f->as_Field()->offset();
3619N/A if (offs == offset || offset == Type::OffsetBot || offs == Type::OffsetBot) {
3619N/A add_to_worklist(f);
3619N/A }
3619N/A }
3619N/A }
3619N/A }
3619N/A}
3619N/A
3619N/A// Find fields which have unknown value.
3619N/Aint ConnectionGraph::find_field_value(FieldNode* field) {
3619N/A // Escaped fields should have init value already.
3619N/A assert(field->escape_state() == PointsToNode::NoEscape, "sanity");
3619N/A int new_edges = 0;
3619N/A for (BaseIterator i(field); i.has_next(); i.next()) {
3619N/A PointsToNode* base = i.get();
3619N/A if (base->is_JavaObject()) {
3619N/A // Skip Allocate's fields which will be processed later.
3619N/A if (base->ideal_node()->is_Allocate())
3619N/A return 0;
3619N/A assert(base == null_obj, "only NULL ptr base expected here");
3619N/A }
3619N/A }
3619N/A if (add_edge(field, phantom_obj)) {
3619N/A // New edge was added
3619N/A new_edges++;
3619N/A add_field_uses_to_worklist(field);
3619N/A }
3619N/A return new_edges;
3619N/A}
3619N/A
3619N/A// Find fields initializing values for allocations.
3619N/Aint ConnectionGraph::find_init_values(JavaObjectNode* pta, PointsToNode* init_val, PhaseTransform* phase) {
3619N/A assert(pta->escape_state() == PointsToNode::NoEscape, "Not escaped Allocate nodes only");
3619N/A int new_edges = 0;
3619N/A Node* alloc = pta->ideal_node();
3619N/A if (init_val == phantom_obj) {
3619N/A // Do nothing for Allocate nodes since its fields values are "known".
3619N/A if (alloc->is_Allocate())
3619N/A return 0;
3619N/A assert(alloc->as_CallStaticJava(), "sanity");
3619N/A#ifdef ASSERT
3619N/A if (alloc->as_CallStaticJava()->method() == NULL) {
3619N/A const char* name = alloc->as_CallStaticJava()->_name;
3619N/A assert(strncmp(name, "_multianewarray", 15) == 0, "sanity");
3619N/A }
3619N/A#endif
3619N/A // Non-escaped allocation returned from Java or runtime call have
3619N/A // unknown values in fields.
3619N/A for (EdgeIterator i(pta); i.has_next(); i.next()) {
4049N/A PointsToNode* field = i.get();
4049N/A if (field->is_Field() && field->as_Field()->is_oop()) {
4049N/A if (add_edge(field, phantom_obj)) {
3619N/A // New edge was added
3619N/A new_edges++;
4049N/A add_field_uses_to_worklist(field->as_Field());
3619N/A }
3619N/A }
3619N/A }
3619N/A return new_edges;
3619N/A }
3619N/A assert(init_val == null_obj, "sanity");
3619N/A // Do nothing for Call nodes since its fields values are unknown.
3619N/A if (!alloc->is_Allocate())
3619N/A return 0;
3619N/A
3619N/A InitializeNode* ini = alloc->as_Allocate()->initialization();
3619N/A Compile* C = _compile;
3619N/A bool visited_bottom_offset = false;
3619N/A GrowableArray<int> offsets_worklist;
3619N/A
3619N/A // Check if an oop field's initializing value is recorded and add
3619N/A // a corresponding NULL if field's value if it is not recorded.
3619N/A // Connection Graph does not record a default initialization by NULL
3619N/A // captured by Initialize node.
3619N/A //
3619N/A for (EdgeIterator i(pta); i.has_next(); i.next()) {
4049N/A PointsToNode* field = i.get(); // Field (AddP)
4049N/A if (!field->is_Field() || !field->as_Field()->is_oop())
3619N/A continue; // Not oop field
4049N/A int offset = field->as_Field()->offset();
3619N/A if (offset == Type::OffsetBot) {
3619N/A if (!visited_bottom_offset) {
3619N/A // OffsetBot is used to reference array's element,
3619N/A // always add reference to NULL to all Field nodes since we don't
3619N/A // known which element is referenced.
4049N/A if (add_edge(field, null_obj)) {
3619N/A // New edge was added
3619N/A new_edges++;
4049N/A add_field_uses_to_worklist(field->as_Field());
3619N/A visited_bottom_offset = true;
3619N/A }
3619N/A }
3619N/A } else {
3619N/A // Check only oop fields.
4049N/A const Type* adr_type = field->ideal_node()->as_AddP()->bottom_type();
3619N/A if (adr_type->isa_rawptr()) {
3619N/A#ifdef ASSERT
3619N/A // Raw pointers are used for initializing stores so skip it
3619N/A // since it should be recorded already
4049N/A Node* base = get_addp_base(field->ideal_node());
3619N/A assert(adr_type->isa_rawptr() && base->is_Proj() &&
3619N/A (base->in(0) == alloc),"unexpected pointer type");
3619N/A#endif
3619N/A continue;
3619N/A }
3619N/A if (!offsets_worklist.contains(offset)) {
3619N/A offsets_worklist.append(offset);
3619N/A Node* value = NULL;
3619N/A if (ini != NULL) {
4049N/A // StoreP::memory_type() == T_ADDRESS
4049N/A BasicType ft = UseCompressedOops ? T_NARROWOOP : T_ADDRESS;
4049N/A Node* store = ini->find_captured_store(offset, type2aelembytes(ft, true), phase);
4049N/A // Make sure initializing store has the same type as this AddP.
4049N/A // This AddP may reference non existing field because it is on a
4049N/A // dead branch of bimorphic call which is not eliminated yet.
4049N/A if (store != NULL && store->is_Store() &&
4049N/A store->as_Store()->memory_type() == ft) {
3619N/A value = store->in(MemNode::ValueIn);
4049N/A#ifdef ASSERT
4049N/A if (VerifyConnectionGraph) {
4049N/A // Verify that AddP already points to all objects the value points to.
4049N/A PointsToNode* val = ptnode_adr(value->_idx);
4049N/A assert((val != NULL), "should be processed already");
4049N/A PointsToNode* missed_obj = NULL;
4049N/A if (val->is_JavaObject()) {
4049N/A if (!field->points_to(val->as_JavaObject())) {
4049N/A missed_obj = val;
4049N/A }
4049N/A } else {
4049N/A if (!val->is_LocalVar() || (val->edge_count() == 0)) {
4049N/A tty->print_cr("----------init store has invalid value -----");
4049N/A store->dump();
4049N/A val->dump();
4049N/A assert(val->is_LocalVar() && (val->edge_count() > 0), "should be processed already");
4049N/A }
4049N/A for (EdgeIterator j(val); j.has_next(); j.next()) {
4049N/A PointsToNode* obj = j.get();
4049N/A if (obj->is_JavaObject()) {
4049N/A if (!field->points_to(obj->as_JavaObject())) {
4049N/A missed_obj = obj;
4049N/A break;
4049N/A }
4049N/A }
4049N/A }
4049N/A }
4049N/A if (missed_obj != NULL) {
4049N/A tty->print_cr("----------field---------------------------------");
4049N/A field->dump();
4049N/A tty->print_cr("----------missed referernce to object-----------");
4049N/A missed_obj->dump();
4049N/A tty->print_cr("----------object referernced by init store -----");
4049N/A store->dump();
4049N/A val->dump();
4049N/A assert(!field->points_to(missed_obj->as_JavaObject()), "missed JavaObject reference");
4049N/A }
4049N/A }
4049N/A#endif
3619N/A } else {
3619N/A // There could be initializing stores which follow allocation.
3619N/A // For example, a volatile field store is not collected
3619N/A // by Initialize node.
3619N/A //
3619N/A // Need to check for dependent loads to separate such stores from
3619N/A // stores which follow loads. For now, add initial value NULL so
3619N/A // that compare pointers optimization works correctly.
3619N/A }
3619N/A }
3619N/A if (value == NULL) {
3619N/A // A field's initializing value was not recorded. Add NULL.
4049N/A if (add_edge(field, null_obj)) {
3619N/A // New edge was added
3619N/A new_edges++;
4049N/A add_field_uses_to_worklist(field->as_Field());
3619N/A }
3619N/A }
3619N/A }
3619N/A }
3619N/A }
3619N/A return new_edges;
0N/A}
0N/A
3619N/A// Adjust scalar_replaceable state after Connection Graph is built.
3619N/Avoid ConnectionGraph::adjust_scalar_replaceable_state(JavaObjectNode* jobj) {
3619N/A // Search for non-escaping objects which are not scalar replaceable
3619N/A // and mark them to propagate the state to referenced objects.
3619N/A
3619N/A // 1. An object is not scalar replaceable if the field into which it is
3619N/A // stored has unknown offset (stored into unknown element of an array).
3619N/A //
3619N/A for (UseIterator i(jobj); i.has_next(); i.next()) {
3619N/A PointsToNode* use = i.get();
3619N/A assert(!use->is_Arraycopy(), "sanity");
3619N/A if (use->is_Field()) {
3619N/A FieldNode* field = use->as_Field();
3619N/A assert(field->is_oop() && field->scalar_replaceable() &&
3619N/A field->fields_escape_state() == PointsToNode::NoEscape, "sanity");
3619N/A if (field->offset() == Type::OffsetBot) {
3619N/A jobj->set_scalar_replaceable(false);
3619N/A return;
3619N/A }
3619N/A }
3619N/A assert(use->is_Field() || use->is_LocalVar(), "sanity");
3619N/A // 2. An object is not scalar replaceable if it is merged with other objects.
3619N/A for (EdgeIterator j(use); j.has_next(); j.next()) {
3619N/A PointsToNode* ptn = j.get();
3619N/A if (ptn->is_JavaObject() && ptn != jobj) {
3619N/A // Mark all objects.
3619N/A jobj->set_scalar_replaceable(false);
3619N/A ptn->set_scalar_replaceable(false);
3619N/A }
3619N/A }
3619N/A if (!jobj->scalar_replaceable()) {
3619N/A return;
3619N/A }
3619N/A }
3619N/A
3619N/A for (EdgeIterator j(jobj); j.has_next(); j.next()) {
3619N/A // Non-escaping object node should point only to field nodes.
3619N/A FieldNode* field = j.get()->as_Field();
3619N/A int offset = field->as_Field()->offset();
3619N/A
3619N/A // 3. An object is not scalar replaceable if it has a field with unknown
3619N/A // offset (array's element is accessed in loop).
3619N/A if (offset == Type::OffsetBot) {
3619N/A jobj->set_scalar_replaceable(false);
3619N/A return;
3619N/A }
3619N/A // 4. Currently an object is not scalar replaceable if a LoadStore node
3619N/A // access its field since the field value is unknown after it.
3619N/A //
3619N/A Node* n = field->ideal_node();
3619N/A for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
3619N/A if (n->fast_out(i)->is_LoadStore()) {
3619N/A jobj->set_scalar_replaceable(false);
3619N/A return;
3619N/A }
3619N/A }
3619N/A
3619N/A // 5. Or the address may point to more then one object. This may produce
3619N/A // the false positive result (set not scalar replaceable)
3619N/A // since the flow-insensitive escape analysis can't separate
3619N/A // the case when stores overwrite the field's value from the case
3619N/A // when stores happened on different control branches.
3619N/A //
3619N/A // Note: it will disable scalar replacement in some cases:
3619N/A //
3619N/A // Point p[] = new Point[1];
3619N/A // p[0] = new Point(); // Will be not scalar replaced
3619N/A //
3619N/A // but it will save us from incorrect optimizations in next cases:
3619N/A //
3619N/A // Point p[] = new Point[1];
3619N/A // if ( x ) p[0] = new Point(); // Will be not scalar replaced
3619N/A //
3619N/A if (field->base_count() > 1) {
3619N/A for (BaseIterator i(field); i.has_next(); i.next()) {
3619N/A PointsToNode* base = i.get();
3619N/A // Don't take into account LocalVar nodes which
3619N/A // may point to only one object which should be also
3619N/A // this field's base by now.
3619N/A if (base->is_JavaObject() && base != jobj) {
3619N/A // Mark all bases.
3619N/A jobj->set_scalar_replaceable(false);
3619N/A base->set_scalar_replaceable(false);
3619N/A }
3619N/A }
3619N/A }
3619N/A }
3619N/A}
3619N/A
3619N/A#ifdef ASSERT
3619N/Avoid ConnectionGraph::verify_connection_graph(
3619N/A GrowableArray<PointsToNode*>& ptnodes_worklist,
3619N/A GrowableArray<JavaObjectNode*>& non_escaped_worklist,
3619N/A GrowableArray<JavaObjectNode*>& java_objects_worklist,
3619N/A GrowableArray<Node*>& addp_worklist) {
3619N/A // Verify that graph is complete - no new edges could be added.
3619N/A int java_objects_length = java_objects_worklist.length();
3619N/A int non_escaped_length = non_escaped_worklist.length();
3619N/A int new_edges = 0;
3619N/A for (int next = 0; next < java_objects_length; ++next) {
3619N/A JavaObjectNode* ptn = java_objects_worklist.at(next);
3619N/A new_edges += add_java_object_edges(ptn, true);
3619N/A }
3619N/A assert(new_edges == 0, "graph was not complete");
3619N/A // Verify that escape state is final.
3619N/A int length = non_escaped_worklist.length();
3619N/A find_non_escaped_objects(ptnodes_worklist, non_escaped_worklist);
3619N/A assert((non_escaped_length == non_escaped_worklist.length()) &&
3619N/A (non_escaped_length == length) &&
3619N/A (_worklist.length() == 0), "escape state was not final");
3619N/A
3619N/A // Verify fields information.
3619N/A int addp_length = addp_worklist.length();
3619N/A for (int next = 0; next < addp_length; ++next ) {
3619N/A Node* n = addp_worklist.at(next);
3619N/A FieldNode* field = ptnode_adr(n->_idx)->as_Field();
3619N/A if (field->is_oop()) {
3619N/A // Verify that field has all bases
3619N/A Node* base = get_addp_base(n);
3619N/A PointsToNode* ptn = ptnode_adr(base->_idx);
3619N/A if (ptn->is_JavaObject()) {
3619N/A assert(field->has_base(ptn->as_JavaObject()), "sanity");
3619N/A } else {
3619N/A assert(ptn->is_LocalVar(), "sanity");
3619N/A for (EdgeIterator i(ptn); i.has_next(); i.next()) {
3619N/A PointsToNode* e = i.get();
3619N/A if (e->is_JavaObject()) {
3619N/A assert(field->has_base(e->as_JavaObject()), "sanity");
3619N/A }
3619N/A }
3619N/A }
3619N/A // Verify that all fields have initializing values.
3619N/A if (field->edge_count() == 0) {
4049N/A tty->print_cr("----------field does not have references----------");
3619N/A field->dump();
4049N/A for (BaseIterator i(field); i.has_next(); i.next()) {
4049N/A PointsToNode* base = i.get();
4049N/A tty->print_cr("----------field has next base---------------------");
4049N/A base->dump();
4049N/A if (base->is_JavaObject() && (base != phantom_obj) && (base != null_obj)) {
4049N/A tty->print_cr("----------base has fields-------------------------");
4049N/A for (EdgeIterator j(base); j.has_next(); j.next()) {
4049N/A j.get()->dump();
4049N/A }
4049N/A tty->print_cr("----------base has references---------------------");
4049N/A for (UseIterator j(base); j.has_next(); j.next()) {
4049N/A j.get()->dump();
4049N/A }
4049N/A }
4049N/A }
4049N/A for (UseIterator i(field); i.has_next(); i.next()) {
4049N/A i.get()->dump();
4049N/A }
3619N/A assert(field->edge_count() > 0, "sanity");
3619N/A }
3619N/A }
3619N/A }
3619N/A}
3619N/A#endif
3619N/A
3619N/A// Optimize ideal graph.
3619N/Avoid ConnectionGraph::optimize_ideal_graph(GrowableArray<Node*>& ptr_cmp_worklist,
3619N/A GrowableArray<Node*>& storestore_worklist) {
3619N/A Compile* C = _compile;
3619N/A PhaseIterGVN* igvn = _igvn;
3619N/A if (EliminateLocks) {
3619N/A // Mark locks before changing ideal graph.
3619N/A int cnt = C->macro_count();
3619N/A for( int i=0; i < cnt; i++ ) {
3619N/A Node *n = C->macro_node(i);
3619N/A if (n->is_AbstractLock()) { // Lock and Unlock nodes
3619N/A AbstractLockNode* alock = n->as_AbstractLock();
3619N/A if (!alock->is_non_esc_obj()) {
3619N/A if (not_global_escape(alock->obj_node())) {
3619N/A assert(!alock->is_eliminated() || alock->is_coarsened(), "sanity");
3619N/A // The lock could be marked eliminated by lock coarsening
3619N/A // code during first IGVN before EA. Replace coarsened flag
3619N/A // to eliminate all associated locks/unlocks.
3619N/A alock->set_non_esc_obj();
3619N/A }
3619N/A }
3619N/A }
3619N/A }
3619N/A }
3619N/A
3619N/A if (OptimizePtrCompare) {
3619N/A // Add ConI(#CC_GT) and ConI(#CC_EQ).
3619N/A _pcmp_neq = igvn->makecon(TypeInt::CC_GT);
3619N/A _pcmp_eq = igvn->makecon(TypeInt::CC_EQ);
3619N/A // Optimize objects compare.
3619N/A while (ptr_cmp_worklist.length() != 0) {
3619N/A Node *n = ptr_cmp_worklist.pop();
3619N/A Node *res = optimize_ptr_compare(n);
3619N/A if (res != NULL) {
3619N/A#ifndef PRODUCT
3619N/A if (PrintOptimizePtrCompare) {
3619N/A tty->print_cr("++++ Replaced: %d %s(%d,%d) --> %s", n->_idx, (n->Opcode() == Op_CmpP ? "CmpP" : "CmpN"), n->in(1)->_idx, n->in(2)->_idx, (res == _pcmp_eq ? "EQ" : "NotEQ"));
3619N/A if (Verbose) {
3619N/A n->dump(1);
3619N/A }
3619N/A }
3619N/A#endif
3619N/A igvn->replace_node(n, res);
3619N/A }
3619N/A }
3619N/A // cleanup
3619N/A if (_pcmp_neq->outcnt() == 0)
3619N/A igvn->hash_delete(_pcmp_neq);
3619N/A if (_pcmp_eq->outcnt() == 0)
3619N/A igvn->hash_delete(_pcmp_eq);
3619N/A }
3619N/A
3619N/A // For MemBarStoreStore nodes added in library_call.cpp, check
3619N/A // escape status of associated AllocateNode and optimize out
3619N/A // MemBarStoreStore node if the allocated object never escapes.
3619N/A while (storestore_worklist.length() != 0) {
3619N/A Node *n = storestore_worklist.pop();
3619N/A MemBarStoreStoreNode *storestore = n ->as_MemBarStoreStore();
3619N/A Node *alloc = storestore->in(MemBarNode::Precedent)->in(0);
3619N/A assert (alloc->is_Allocate(), "storestore should point to AllocateNode");
3619N/A if (not_global_escape(alloc)) {
3619N/A MemBarNode* mb = MemBarNode::make(C, Op_MemBarCPUOrder, Compile::AliasIdxBot);
3619N/A mb->init_req(TypeFunc::Memory, storestore->in(TypeFunc::Memory));
3619N/A mb->init_req(TypeFunc::Control, storestore->in(TypeFunc::Control));
3619N/A igvn->register_new_node_with_optimizer(mb);
3619N/A igvn->replace_node(storestore, mb);
3619N/A }
3619N/A }
3619N/A}
3619N/A
3619N/A// Optimize objects compare.
3619N/ANode* ConnectionGraph::optimize_ptr_compare(Node* n) {
3619N/A assert(OptimizePtrCompare, "sanity");
3619N/A PointsToNode* ptn1 = ptnode_adr(n->in(1)->_idx);
3619N/A PointsToNode* ptn2 = ptnode_adr(n->in(2)->_idx);
3619N/A JavaObjectNode* jobj1 = unique_java_object(n->in(1));
3619N/A JavaObjectNode* jobj2 = unique_java_object(n->in(2));
3619N/A assert(ptn1->is_JavaObject() || ptn1->is_LocalVar(), "sanity");
3619N/A assert(ptn2->is_JavaObject() || ptn2->is_LocalVar(), "sanity");
0N/A
3619N/A // Check simple cases first.
3619N/A if (jobj1 != NULL) {
3619N/A if (jobj1->escape_state() == PointsToNode::NoEscape) {
3619N/A if (jobj1 == jobj2) {
3619N/A // Comparing the same not escaping object.
3619N/A return _pcmp_eq;
3619N/A }
3619N/A Node* obj = jobj1->ideal_node();
3619N/A // Comparing not escaping allocation.
3619N/A if ((obj->is_Allocate() || obj->is_CallStaticJava()) &&
3619N/A !ptn2->points_to(jobj1)) {
3619N/A return _pcmp_neq; // This includes nullness check.
3619N/A }
3619N/A }
3619N/A }
3619N/A if (jobj2 != NULL) {
3619N/A if (jobj2->escape_state() == PointsToNode::NoEscape) {
3619N/A Node* obj = jobj2->ideal_node();
3619N/A // Comparing not escaping allocation.
3619N/A if ((obj->is_Allocate() || obj->is_CallStaticJava()) &&
3619N/A !ptn1->points_to(jobj2)) {
3619N/A return _pcmp_neq; // This includes nullness check.
3619N/A }
3619N/A }
3619N/A }
3619N/A if (jobj1 != NULL && jobj1 != phantom_obj &&
3619N/A jobj2 != NULL && jobj2 != phantom_obj &&
3619N/A jobj1->ideal_node()->is_Con() &&
3619N/A jobj2->ideal_node()->is_Con()) {
3619N/A // Klass or String constants compare. Need to be careful with
3619N/A // compressed pointers - compare types of ConN and ConP instead of nodes.
4505N/A const Type* t1 = jobj1->ideal_node()->get_ptr_type();
4505N/A const Type* t2 = jobj2->ideal_node()->get_ptr_type();
3619N/A if (t1->make_ptr() == t2->make_ptr()) {
3619N/A return _pcmp_eq;
3619N/A } else {
3619N/A return _pcmp_neq;
3619N/A }
3619N/A }
3619N/A if (ptn1->meet(ptn2)) {
3619N/A return NULL; // Sets are not disjoint
3619N/A }
3619N/A
3619N/A // Sets are disjoint.
3619N/A bool set1_has_unknown_ptr = ptn1->points_to(phantom_obj);
3619N/A bool set2_has_unknown_ptr = ptn2->points_to(phantom_obj);
3619N/A bool set1_has_null_ptr = ptn1->points_to(null_obj);
3619N/A bool set2_has_null_ptr = ptn2->points_to(null_obj);
3619N/A if (set1_has_unknown_ptr && set2_has_null_ptr ||
3619N/A set2_has_unknown_ptr && set1_has_null_ptr) {
3619N/A // Check nullness of unknown object.
3619N/A return NULL;
3619N/A }
3619N/A
3619N/A // Disjointness by itself is not sufficient since
3619N/A // alias analysis is not complete for escaped objects.
3619N/A // Disjoint sets are definitely unrelated only when
3619N/A // at least one set has only not escaping allocations.
3619N/A if (!set1_has_unknown_ptr && !set1_has_null_ptr) {
3619N/A if (ptn1->non_escaping_allocation()) {
3619N/A return _pcmp_neq;
3619N/A }
3619N/A }
3619N/A if (!set2_has_unknown_ptr && !set2_has_null_ptr) {
3619N/A if (ptn2->non_escaping_allocation()) {
3619N/A return _pcmp_neq;
3619N/A }
3619N/A }
3619N/A return NULL;
3619N/A}
3619N/A
3619N/A// Connection Graph constuction functions.
3619N/A
3619N/Avoid ConnectionGraph::add_local_var(Node *n, PointsToNode::EscapeState es) {
3619N/A PointsToNode* ptadr = _nodes.at(n->_idx);
3619N/A if (ptadr != NULL) {
3619N/A assert(ptadr->is_LocalVar() && ptadr->ideal_node() == n, "sanity");
3619N/A return;
3619N/A }
3619N/A Compile* C = _compile;
3619N/A ptadr = new (C->comp_arena()) LocalVarNode(C, n, es);
3619N/A _nodes.at_put(n->_idx, ptadr);
3619N/A}
3619N/A
3619N/Avoid ConnectionGraph::add_java_object(Node *n, PointsToNode::EscapeState es) {
3619N/A PointsToNode* ptadr = _nodes.at(n->_idx);
3619N/A if (ptadr != NULL) {
3619N/A assert(ptadr->is_JavaObject() && ptadr->ideal_node() == n, "sanity");
3619N/A return;
3619N/A }
3619N/A Compile* C = _compile;
3619N/A ptadr = new (C->comp_arena()) JavaObjectNode(C, n, es);
3619N/A _nodes.at_put(n->_idx, ptadr);
3619N/A}
3619N/A
3619N/Avoid ConnectionGraph::add_field(Node *n, PointsToNode::EscapeState es, int offset) {
3619N/A PointsToNode* ptadr = _nodes.at(n->_idx);
3619N/A if (ptadr != NULL) {
3619N/A assert(ptadr->is_Field() && ptadr->ideal_node() == n, "sanity");
3619N/A return;
3619N/A }
3932N/A bool unsafe = false;
3932N/A bool is_oop = is_oop_field(n, offset, &unsafe);
3932N/A if (unsafe) {
3932N/A es = PointsToNode::GlobalEscape;
3932N/A }
3619N/A Compile* C = _compile;
3619N/A FieldNode* field = new (C->comp_arena()) FieldNode(C, n, es, offset, is_oop);
3619N/A _nodes.at_put(n->_idx, field);
3619N/A}
3619N/A
3619N/Avoid ConnectionGraph::add_arraycopy(Node *n, PointsToNode::EscapeState es,
3619N/A PointsToNode* src, PointsToNode* dst) {
3619N/A assert(!src->is_Field() && !dst->is_Field(), "only for JavaObject and LocalVar");
3619N/A assert((src != null_obj) && (dst != null_obj), "not for ConP NULL");
3619N/A PointsToNode* ptadr = _nodes.at(n->_idx);
3619N/A if (ptadr != NULL) {
3619N/A assert(ptadr->is_Arraycopy() && ptadr->ideal_node() == n, "sanity");
3619N/A return;
3619N/A }
3619N/A Compile* C = _compile;
3619N/A ptadr = new (C->comp_arena()) ArraycopyNode(C, n, es);
3619N/A _nodes.at_put(n->_idx, ptadr);
3619N/A // Add edge from arraycopy node to source object.
3619N/A (void)add_edge(ptadr, src);
3619N/A src->set_arraycopy_src();
3619N/A // Add edge from destination object to arraycopy node.
3619N/A (void)add_edge(dst, ptadr);
3619N/A dst->set_arraycopy_dst();
0N/A}
0N/A
3932N/Abool ConnectionGraph::is_oop_field(Node* n, int offset, bool* unsafe) {
3619N/A const Type* adr_type = n->as_AddP()->bottom_type();
3619N/A BasicType bt = T_INT;
3619N/A if (offset == Type::OffsetBot) {
3619N/A // Check only oop fields.
3619N/A if (!adr_type->isa_aryptr() ||
3619N/A (adr_type->isa_aryptr()->klass() == NULL) ||
3619N/A adr_type->isa_aryptr()->klass()->is_obj_array_klass()) {
3619N/A // OffsetBot is used to reference array's element. Ignore first AddP.
3619N/A if (find_second_addp(n, n->in(AddPNode::Base)) == NULL) {
3619N/A bt = T_OBJECT;
3619N/A }
3619N/A }
3619N/A } else if (offset != oopDesc::klass_offset_in_bytes()) {
3619N/A if (adr_type->isa_instptr()) {
3619N/A ciField* field = _compile->alias_type(adr_type->isa_instptr())->field();
3619N/A if (field != NULL) {
3619N/A bt = field->layout_type();
3619N/A } else {
3932N/A // Check for unsafe oop field access
3932N/A for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
3932N/A int opcode = n->fast_out(i)->Opcode();
3932N/A if (opcode == Op_StoreP || opcode == Op_LoadP ||
3932N/A opcode == Op_StoreN || opcode == Op_LoadN) {
3932N/A bt = T_OBJECT;
3932N/A (*unsafe) = true;
3932N/A break;
3932N/A }
3932N/A }
3619N/A }
3619N/A } else if (adr_type->isa_aryptr()) {
3619N/A if (offset == arrayOopDesc::length_offset_in_bytes()) {
3619N/A // Ignore array length load.
3619N/A } else if (find_second_addp(n, n->in(AddPNode::Base)) != NULL) {
3619N/A // Ignore first AddP.
3619N/A } else {
3619N/A const Type* elemtype = adr_type->isa_aryptr()->elem();
3619N/A bt = elemtype->array_element_basic_type();
3619N/A }
3619N/A } else if (adr_type->isa_rawptr() || adr_type->isa_klassptr()) {
3619N/A // Allocation initialization, ThreadLocal field access, unsafe access
3619N/A for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
3619N/A int opcode = n->fast_out(i)->Opcode();
3619N/A if (opcode == Op_StoreP || opcode == Op_LoadP ||
3619N/A opcode == Op_StoreN || opcode == Op_LoadN) {
3619N/A bt = T_OBJECT;
3932N/A break;
3619N/A }
3619N/A }
3619N/A }
3619N/A }
3619N/A return (bt == T_OBJECT || bt == T_NARROWOOP || bt == T_ARRAY);
3619N/A}
3619N/A
3619N/A// Returns unique pointed java object or NULL.
3619N/AJavaObjectNode* ConnectionGraph::unique_java_object(Node *n) {
3619N/A assert(!_collecting, "should not call when contructed graph");
3619N/A // If the node was created after the escape computation we can't answer.
3619N/A uint idx = n->_idx;
3619N/A if (idx >= nodes_size()) {
3619N/A return NULL;
3619N/A }
3619N/A PointsToNode* ptn = ptnode_adr(idx);
3619N/A if (ptn->is_JavaObject()) {
3619N/A return ptn->as_JavaObject();
3619N/A }
3619N/A assert(ptn->is_LocalVar(), "sanity");
3619N/A // Check all java objects it points to.
3619N/A JavaObjectNode* jobj = NULL;
3619N/A for (EdgeIterator i(ptn); i.has_next(); i.next()) {
3619N/A PointsToNode* e = i.get();
3619N/A if (e->is_JavaObject()) {
3619N/A if (jobj == NULL) {
3619N/A jobj = e->as_JavaObject();
3619N/A } else if (jobj != e) {
3619N/A return NULL;
3619N/A }
3619N/A }
3619N/A }
3619N/A return jobj;
3619N/A}
3619N/A
3619N/A// Return true if this node points only to non-escaping allocations.
3619N/Abool PointsToNode::non_escaping_allocation() {
3619N/A if (is_JavaObject()) {
3619N/A Node* n = ideal_node();
3619N/A if (n->is_Allocate() || n->is_CallStaticJava()) {
3619N/A return (escape_state() == PointsToNode::NoEscape);
3619N/A } else {
3619N/A return false;
3619N/A }
3619N/A }
3619N/A assert(is_LocalVar(), "sanity");
3619N/A // Check all java objects it points to.
3619N/A for (EdgeIterator i(this); i.has_next(); i.next()) {
3619N/A PointsToNode* e = i.get();
3619N/A if (e->is_JavaObject()) {
3619N/A Node* n = e->ideal_node();
3619N/A if ((e->escape_state() != PointsToNode::NoEscape) ||
3619N/A !(n->is_Allocate() || n->is_CallStaticJava())) {
3619N/A return false;
3619N/A }
3619N/A }
3619N/A }
3619N/A return true;
3619N/A}
3619N/A
3619N/A// Return true if we know the node does not escape globally.
3619N/Abool ConnectionGraph::not_global_escape(Node *n) {
3619N/A assert(!_collecting, "should not call during graph construction");
3619N/A // If the node was created after the escape computation we can't answer.
3619N/A uint idx = n->_idx;
3619N/A if (idx >= nodes_size()) {
3619N/A return false;
3619N/A }
3619N/A PointsToNode* ptn = ptnode_adr(idx);
3619N/A PointsToNode::EscapeState es = ptn->escape_state();
3619N/A // If we have already computed a value, return it.
3619N/A if (es >= PointsToNode::GlobalEscape)
3619N/A return false;
3619N/A if (ptn->is_JavaObject()) {
3619N/A return true; // (es < PointsToNode::GlobalEscape);
3619N/A }
3619N/A assert(ptn->is_LocalVar(), "sanity");
3619N/A // Check all java objects it points to.
3619N/A for (EdgeIterator i(ptn); i.has_next(); i.next()) {
3619N/A if (i.get()->escape_state() >= PointsToNode::GlobalEscape)
3619N/A return false;
3619N/A }
3619N/A return true;
3619N/A}
3619N/A
3619N/A
3619N/A// Helper functions
3619N/A
3619N/A// Return true if this node points to specified node or nodes it points to.
3619N/Abool PointsToNode::points_to(JavaObjectNode* ptn) const {
3619N/A if (is_JavaObject()) {
3619N/A return (this == ptn);
3619N/A }
4049N/A assert(is_LocalVar() || is_Field(), "sanity");
3619N/A for (EdgeIterator i(this); i.has_next(); i.next()) {
3619N/A if (i.get() == ptn)
3619N/A return true;
3619N/A }
3619N/A return false;
3619N/A}
3619N/A
3619N/A// Return true if one node points to an other.
3619N/Abool PointsToNode::meet(PointsToNode* ptn) {
3619N/A if (this == ptn) {
3619N/A return true;
3619N/A } else if (ptn->is_JavaObject()) {
3619N/A return this->points_to(ptn->as_JavaObject());
3619N/A } else if (this->is_JavaObject()) {
3619N/A return ptn->points_to(this->as_JavaObject());
3619N/A }
3619N/A assert(this->is_LocalVar() && ptn->is_LocalVar(), "sanity");
3619N/A int ptn_count = ptn->edge_count();
3619N/A for (EdgeIterator i(this); i.has_next(); i.next()) {
3619N/A PointsToNode* this_e = i.get();
3619N/A for (int j = 0; j < ptn_count; j++) {
3619N/A if (this_e == ptn->edge(j))
3619N/A return true;
3619N/A }
3619N/A }
3619N/A return false;
3619N/A}
3619N/A
3619N/A#ifdef ASSERT
3619N/A// Return true if bases point to this java object.
3619N/Abool FieldNode::has_base(JavaObjectNode* jobj) const {
3619N/A for (BaseIterator i(this); i.has_next(); i.next()) {
3619N/A if (i.get() == jobj)
3619N/A return true;
3619N/A }
3619N/A return false;
3619N/A}
3619N/A#endif
3619N/A
65N/Aint ConnectionGraph::address_offset(Node* adr, PhaseTransform *phase) {
65N/A const Type *adr_type = phase->type(adr);
65N/A if (adr->is_AddP() && adr_type->isa_oopptr() == NULL &&
65N/A adr->in(AddPNode::Address)->is_Proj() &&
65N/A adr->in(AddPNode::Address)->in(0)->is_Allocate()) {
65N/A // We are computing a raw address for a store captured by an Initialize
65N/A // compute an appropriate address type. AddP cases #3 and #5 (see below).
65N/A int offs = (int)phase->find_intptr_t_con(adr->in(AddPNode::Offset), Type::OffsetBot);
65N/A assert(offs != Type::OffsetBot ||
65N/A adr->in(AddPNode::Address)->in(0)->is_AllocateArray(),
65N/A "offset must be a constant or it is initialization of array");
65N/A return offs;
65N/A }
65N/A const TypePtr *t_ptr = adr_type->isa_ptr();
0N/A assert(t_ptr != NULL, "must be a pointer type");
0N/A return t_ptr->offset();
0N/A}
0N/A
3619N/ANode* ConnectionGraph::get_addp_base(Node *addp) {
65N/A assert(addp->is_AddP(), "must be AddP");
65N/A //
65N/A // AddP cases for Base and Address inputs:
65N/A // case #1. Direct object's field reference:
65N/A // Allocate
65N/A // |
65N/A // Proj #5 ( oop result )
65N/A // |
65N/A // CheckCastPP (cast to instance type)
65N/A // | |
65N/A // AddP ( base == address )
65N/A //
65N/A // case #2. Indirect object's field reference:
65N/A // Phi
65N/A // |
65N/A // CastPP (cast to instance type)
65N/A // | |
65N/A // AddP ( base == address )
65N/A //
65N/A // case #3. Raw object's field reference for Initialize node:
65N/A // Allocate
65N/A // |
65N/A // Proj #5 ( oop result )
65N/A // top |
65N/A // \ |
65N/A // AddP ( base == top )
65N/A //
65N/A // case #4. Array's element reference:
65N/A // {CheckCastPP | CastPP}
65N/A // | | |
65N/A // | AddP ( array's element offset )
65N/A // | |
65N/A // AddP ( array's offset )
65N/A //
65N/A // case #5. Raw object's field reference for arraycopy stub call:
65N/A // The inline_native_clone() case when the arraycopy stub is called
65N/A // after the allocation before Initialize and CheckCastPP nodes.
65N/A // Allocate
65N/A // |
65N/A // Proj #5 ( oop result )
65N/A // | |
65N/A // AddP ( base == address )
65N/A //
77N/A // case #6. Constant Pool, ThreadLocal, CastX2P or
77N/A // Raw object's field reference:
77N/A // {ConP, ThreadLocal, CastX2P, raw Load}
65N/A // top |
65N/A // \ |
65N/A // AddP ( base == top )
65N/A //
77N/A // case #7. Klass's field reference.
77N/A // LoadKlass
77N/A // | |
77N/A // AddP ( base == address )
77N/A //
164N/A // case #8. narrow Klass's field reference.
164N/A // LoadNKlass
164N/A // |
164N/A // DecodeN
164N/A // | |
164N/A // AddP ( base == address )
164N/A //
3619N/A Node *base = addp->in(AddPNode::Base);
3619N/A if (base->uncast()->is_top()) { // The AddP case #3 and #6.
3619N/A base = addp->in(AddPNode::Address);
957N/A while (base->is_AddP()) {
957N/A // Case #6 (unsafe access) may have several chained AddP nodes.
3619N/A assert(base->in(AddPNode::Base)->uncast()->is_top(), "expected unsafe access address only");
3619N/A base = base->in(AddPNode::Address);
957N/A }
3619N/A Node* uncast_base = base->uncast();
3619N/A int opcode = uncast_base->Opcode();
3619N/A assert(opcode == Op_ConP || opcode == Op_ThreadLocal ||
3619N/A opcode == Op_CastX2P || uncast_base->is_DecodeN() ||
4607N/A (uncast_base->is_Mem() && (uncast_base->bottom_type()->isa_rawptr() != NULL)) ||
3619N/A (uncast_base->is_Proj() && uncast_base->in(0)->is_Allocate()), "sanity");
0N/A }
65N/A return base;
65N/A}
65N/A
3619N/ANode* ConnectionGraph::find_second_addp(Node* addp, Node* n) {
65N/A assert(addp->is_AddP() && addp->outcnt() > 0, "Don't process dead nodes");
65N/A Node* addp2 = addp->raw_out(0);
65N/A if (addp->outcnt() == 1 && addp2->is_AddP() &&
65N/A addp2->in(AddPNode::Base) == n &&
65N/A addp2->in(AddPNode::Address) == addp) {
65N/A assert(addp->in(AddPNode::Base) == n, "expecting the same base");
65N/A //
65N/A // Find array's offset to push it on worklist first and
65N/A // as result process an array's element offset first (pushed second)
65N/A // to avoid CastPP for the array's offset.
65N/A // Otherwise the inserted CastPP (LocalVar) will point to what
65N/A // the AddP (Field) points to. Which would be wrong since
65N/A // the algorithm expects the CastPP has the same point as
65N/A // as AddP's base CheckCastPP (LocalVar).
65N/A //
65N/A // ArrayAllocation
65N/A // |
65N/A // CheckCastPP
65N/A // |
65N/A // memProj (from ArrayAllocation CheckCastPP)
65N/A // | ||
65N/A // | || Int (element index)
65N/A // | || | ConI (log(element size))
65N/A // | || | /
65N/A // | || LShift
65N/A // | || /
65N/A // | AddP (array's element offset)
65N/A // | |
65N/A // | | ConI (array's offset: #12(32-bits) or #24(64-bits))
65N/A // | / /
65N/A // AddP (array's offset)
65N/A // |
65N/A // Load/Store (memory operation on array's element)
65N/A //
65N/A return addp2;
65N/A }
65N/A return NULL;
0N/A}
0N/A
0N/A//
0N/A// Adjust the type and inputs of an AddP which computes the
0N/A// address of a field of an instance
0N/A//
3619N/Abool ConnectionGraph::split_AddP(Node *addp, Node *base) {
3619N/A PhaseGVN* igvn = _igvn;
65N/A const TypeOopPtr *base_t = igvn->type(base)->isa_oopptr();
223N/A assert(base_t != NULL && base_t->is_known_instance(), "expecting instance oopptr");
0N/A const TypeOopPtr *t = igvn->type(addp)->isa_oopptr();
65N/A if (t == NULL) {
65N/A // We are computing a raw address for a store captured by an Initialize
293N/A // compute an appropriate address type (cases #3 and #5).
65N/A assert(igvn->type(addp) == TypeRawPtr::NOTNULL, "must be raw pointer");
65N/A assert(addp->in(AddPNode::Address)->is_Proj(), "base of raw address must be result projection from allocation");
306N/A intptr_t offs = (int)igvn->find_intptr_t_con(addp->in(AddPNode::Offset), Type::OffsetBot);
65N/A assert(offs != Type::OffsetBot, "offset must be a constant");
65N/A t = base_t->add_offset(offs)->is_oopptr();
65N/A }
223N/A int inst_id = base_t->instance_id();
223N/A assert(!t->is_known_instance() || t->instance_id() == inst_id,
0N/A "old type must be non-instance or match new type");
293N/A
293N/A // The type 't' could be subclass of 'base_t'.
293N/A // As result t->offset() could be large then base_t's size and it will
293N/A // cause the failure in add_offset() with narrow oops since TypeOopPtr()
293N/A // constructor verifies correctness of the offset.
293N/A //
605N/A // It could happened on subclass's branch (from the type profiling
293N/A // inlining) which was not eliminated during parsing since the exactness
293N/A // of the allocation type was not propagated to the subclass type check.
293N/A //
988N/A // Or the type 't' could be not related to 'base_t' at all.
988N/A // It could happened when CHA type is different from MDO type on a dead path
988N/A // (for example, from instanceof check) which is not collapsed during parsing.
988N/A //
293N/A // Do nothing for such AddP node and don't process its users since
293N/A // this code branch will go away.
293N/A //
293N/A if (!t->is_known_instance() &&
988N/A !base_t->klass()->is_subtype_of(t->klass())) {
293N/A return false; // bail out
293N/A }
0N/A const TypeOopPtr *tinst = base_t->add_offset(t->offset())->is_oopptr();
1062N/A // Do NOT remove the next line: ensure a new alias index is allocated
1062N/A // for the instance type. Note: C++ will not remove it since the call
1062N/A // has side effect.
0N/A int alias_idx = _compile->get_alias_index(tinst);
0N/A igvn->set_type(addp, tinst);
0N/A // record the allocation in the node map
3619N/A set_map(addp, get_map(base->_idx));
253N/A // Set addp's Base and Address to 'base'.
253N/A Node *abase = addp->in(AddPNode::Base);
253N/A Node *adr = addp->in(AddPNode::Address);
253N/A if (adr->is_Proj() && adr->in(0)->is_Allocate() &&
253N/A adr->in(0)->_idx == (uint)inst_id) {
253N/A // Skip AddP cases #3 and #5.
253N/A } else {
253N/A assert(!abase->is_top(), "sanity"); // AddP case #3
253N/A if (abase != base) {
253N/A igvn->hash_delete(addp);
253N/A addp->set_req(AddPNode::Base, base);
253N/A if (abase == adr) {
253N/A addp->set_req(AddPNode::Address, base);
253N/A } else {
253N/A // AddP case #4 (adr is array's element offset AddP node)
253N/A#ifdef ASSERT
253N/A const TypeOopPtr *atype = igvn->type(adr)->isa_oopptr();
253N/A assert(adr->is_AddP() && atype != NULL &&
253N/A atype->instance_id() == inst_id, "array's element offset should be processed first");
253N/A#endif
253N/A }
253N/A igvn->hash_insert(addp);
0N/A }
0N/A }
65N/A // Put on IGVN worklist since at least addp's type was changed above.
65N/A record_for_optimizer(addp);
293N/A return true;
0N/A}
0N/A
0N/A//
0N/A// Create a new version of orig_phi if necessary. Returns either the newly
2306N/A// created phi or an existing phi. Sets create_new to indicate whether a new
0N/A// phi was created. Cache the last newly created phi in the node map.
0N/A//
3619N/APhiNode *ConnectionGraph::create_split_phi(PhiNode *orig_phi, int alias_idx, GrowableArray<PhiNode *> &orig_phi_worklist, bool &new_created) {
0N/A Compile *C = _compile;
3619N/A PhaseGVN* igvn = _igvn;
0N/A new_created = false;
0N/A int phi_alias_idx = C->get_alias_index(orig_phi->adr_type());
0N/A // nothing to do if orig_phi is bottom memory or matches alias_idx
65N/A if (phi_alias_idx == alias_idx) {
0N/A return orig_phi;
0N/A }
851N/A // Have we recently created a Phi for this alias index?
0N/A PhiNode *result = get_map_phi(orig_phi->_idx);
0N/A if (result != NULL && C->get_alias_index(result->adr_type()) == alias_idx) {
0N/A return result;
0N/A }
851N/A // Previous check may fail when the same wide memory Phi was split into Phis
851N/A // for different memory slices. Search all Phis for this region.
851N/A if (result != NULL) {
851N/A Node* region = orig_phi->in(0);
851N/A for (DUIterator_Fast imax, i = region->fast_outs(imax); i < imax; i++) {
851N/A Node* phi = region->fast_out(i);
851N/A if (phi->is_Phi() &&
851N/A C->get_alias_index(phi->as_Phi()->adr_type()) == alias_idx) {
851N/A assert(phi->_idx >= nodes_size(), "only new Phi per instance memory slice");
851N/A return phi->as_Phi();
851N/A }
851N/A }
851N/A }
4123N/A if ((int) (C->live_nodes() + 2*NodeLimitFudgeFactor) > MaxNodeLimit) {
38N/A if (C->do_escape_analysis() == true && !C->failing()) {
38N/A // Retry compilation without escape analysis.
38N/A // If this is the first failure, the sentinel string will "stick"
38N/A // to the Compile object, and the C2Compiler will see it and retry.
38N/A C->record_failure(C2Compiler::retry_no_escape_analysis());
38N/A }
38N/A return NULL;
38N/A }
0N/A orig_phi_worklist.append_if_missing(orig_phi);
65N/A const TypePtr *atype = C->get_adr_type(alias_idx);
0N/A result = PhiNode::make(orig_phi->in(0), NULL, Type::MEMORY, atype);
851N/A C->copy_node_notes_to(result, orig_phi);
0N/A igvn->set_type(result, result->bottom_type());
0N/A record_for_optimizer(result);
3619N/A set_map(orig_phi, result);
0N/A new_created = true;
0N/A return result;
0N/A}
0N/A
0N/A//
2306N/A// Return a new version of Memory Phi "orig_phi" with the inputs having the
0N/A// specified alias index.
0N/A//
3619N/APhiNode *ConnectionGraph::split_memory_phi(PhiNode *orig_phi, int alias_idx, GrowableArray<PhiNode *> &orig_phi_worklist) {
0N/A assert(alias_idx != Compile::AliasIdxBot, "can't split out bottom memory");
0N/A Compile *C = _compile;
3619N/A PhaseGVN* igvn = _igvn;
0N/A bool new_phi_created;
3619N/A PhiNode *result = create_split_phi(orig_phi, alias_idx, orig_phi_worklist, new_phi_created);
0N/A if (!new_phi_created) {
0N/A return result;
0N/A }
0N/A GrowableArray<PhiNode *> phi_list;
0N/A GrowableArray<uint> cur_input;
0N/A PhiNode *phi = orig_phi;
0N/A uint idx = 1;
0N/A bool finished = false;
0N/A while(!finished) {
0N/A while (idx < phi->req()) {
3619N/A Node *mem = find_inst_mem(phi->in(idx), alias_idx, orig_phi_worklist);
0N/A if (mem != NULL && mem->is_Phi()) {
3619N/A PhiNode *newphi = create_split_phi(mem->as_Phi(), alias_idx, orig_phi_worklist, new_phi_created);
0N/A if (new_phi_created) {
0N/A // found an phi for which we created a new split, push current one on worklist and begin
0N/A // processing new one
0N/A phi_list.push(phi);
0N/A cur_input.push(idx);
0N/A phi = mem->as_Phi();
65N/A result = newphi;
0N/A idx = 1;
0N/A continue;
0N/A } else {
65N/A mem = newphi;
0N/A }
0N/A }
38N/A if (C->failing()) {
38N/A return NULL;
38N/A }
0N/A result->set_req(idx++, mem);
0N/A }
0N/A#ifdef ASSERT
0N/A // verify that the new Phi has an input for each input of the original
0N/A assert( phi->req() == result->req(), "must have same number of inputs.");
0N/A assert( result->in(0) != NULL && result->in(0) == phi->in(0), "regions must match");
65N/A#endif
65N/A // Check if all new phi's inputs have specified alias index.
65N/A // Otherwise use old phi.
0N/A for (uint i = 1; i < phi->req(); i++) {
65N/A Node* in = result->in(i);
65N/A assert((phi->in(i) == NULL) == (in == NULL), "inputs must correspond.");
0N/A }
0N/A // we have finished processing a Phi, see if there are any more to do
0N/A finished = (phi_list.length() == 0 );
0N/A if (!finished) {
0N/A phi = phi_list.pop();
0N/A idx = cur_input.pop();
65N/A PhiNode *prev_result = get_map_phi(phi->_idx);
65N/A prev_result->set_req(idx++, result);
65N/A result = prev_result;
0N/A }
0N/A }
0N/A return result;
0N/A}
0N/A
65N/A//
65N/A// The next methods are derived from methods in MemNode.
65N/A//
3619N/ANode* ConnectionGraph::step_through_mergemem(MergeMemNode *mmem, int alias_idx, const TypeOopPtr *toop) {
65N/A Node *mem = mmem;
1735N/A // TypeOopPtr::NOTNULL+any is an OOP with unknown offset - generally
65N/A // means an array I have not precisely typed yet. Do not do any
65N/A // alias stuff with it any time soon.
3619N/A if (toop->base() != Type::AnyPtr &&
1735N/A !(toop->klass() != NULL &&
1735N/A toop->klass()->is_java_lang_Object() &&
3619N/A toop->offset() == Type::OffsetBot)) {
65N/A mem = mmem->memory_at(alias_idx);
65N/A // Update input if it is progress over what we have now
65N/A }
65N/A return mem;
65N/A}
65N/A
65N/A//
1101N/A// Move memory users to their memory slices.
1101N/A//
3619N/Avoid ConnectionGraph::move_inst_mem(Node* n, GrowableArray<PhiNode *> &orig_phis) {
1101N/A Compile* C = _compile;
3619N/A PhaseGVN* igvn = _igvn;
1101N/A const TypePtr* tp = igvn->type(n->in(MemNode::Address))->isa_ptr();
1101N/A assert(tp != NULL, "ptr type");
1101N/A int alias_idx = C->get_alias_index(tp);
1101N/A int general_idx = C->get_general_index(alias_idx);
1101N/A
1101N/A // Move users first
1101N/A for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
1101N/A Node* use = n->fast_out(i);
1101N/A if (use->is_MergeMem()) {
1101N/A MergeMemNode* mmem = use->as_MergeMem();
1101N/A assert(n == mmem->memory_at(alias_idx), "should be on instance memory slice");
1101N/A if (n != mmem->memory_at(general_idx) || alias_idx == general_idx) {
1101N/A continue; // Nothing to do
1101N/A }
1101N/A // Replace previous general reference to mem node.
1101N/A uint orig_uniq = C->unique();
3619N/A Node* m = find_inst_mem(n, general_idx, orig_phis);
1101N/A assert(orig_uniq == C->unique(), "no new nodes");
1101N/A mmem->set_memory_at(general_idx, m);
1101N/A --imax;
1101N/A --i;
1101N/A } else if (use->is_MemBar()) {
1101N/A assert(!use->is_Initialize(), "initializing stores should not be moved");
1101N/A if (use->req() > MemBarNode::Precedent &&
1101N/A use->in(MemBarNode::Precedent) == n) {
1101N/A // Don't move related membars.
1101N/A record_for_optimizer(use);
1101N/A continue;
1101N/A }
1101N/A tp = use->as_MemBar()->adr_type()->isa_ptr();
1101N/A if (tp != NULL && C->get_alias_index(tp) == alias_idx ||
1101N/A alias_idx == general_idx) {
1101N/A continue; // Nothing to do
1101N/A }
1101N/A // Move to general memory slice.
1101N/A uint orig_uniq = C->unique();
3619N/A Node* m = find_inst_mem(n, general_idx, orig_phis);
1101N/A assert(orig_uniq == C->unique(), "no new nodes");
1101N/A igvn->hash_delete(use);
1101N/A imax -= use->replace_edge(n, m);
1101N/A igvn->hash_insert(use);
1101N/A record_for_optimizer(use);
1101N/A --i;
1101N/A#ifdef ASSERT
1101N/A } else if (use->is_Mem()) {
1101N/A if (use->Opcode() == Op_StoreCM && use->in(MemNode::OopStore) == n) {
1101N/A // Don't move related cardmark.
1101N/A continue;
1101N/A }
1101N/A // Memory nodes should have new memory input.
1101N/A tp = igvn->type(use->in(MemNode::Address))->isa_ptr();
1101N/A assert(tp != NULL, "ptr type");
1101N/A int idx = C->get_alias_index(tp);
1101N/A assert(get_map(use->_idx) != NULL || idx == alias_idx,
1101N/A "Following memory nodes should have new memory input or be on the same memory slice");
1101N/A } else if (use->is_Phi()) {
1101N/A // Phi nodes should be split and moved already.
1101N/A tp = use->as_Phi()->adr_type()->isa_ptr();
1101N/A assert(tp != NULL, "ptr type");
1101N/A int idx = C->get_alias_index(tp);
1101N/A assert(idx == alias_idx, "Following Phi nodes should be on the same memory slice");
1101N/A } else {
1101N/A use->dump();
1101N/A assert(false, "should not be here");
1101N/A#endif
1101N/A }
1101N/A }
1101N/A}
1101N/A
1101N/A//
65N/A// Search memory chain of "mem" to find a MemNode whose address
65N/A// is the specified alias index.
65N/A//
3619N/ANode* ConnectionGraph::find_inst_mem(Node *orig_mem, int alias_idx, GrowableArray<PhiNode *> &orig_phis) {
65N/A if (orig_mem == NULL)
65N/A return orig_mem;
3619N/A Compile* C = _compile;
3619N/A PhaseGVN* igvn = _igvn;
1735N/A const TypeOopPtr *toop = C->get_adr_type(alias_idx)->isa_oopptr();
1735N/A bool is_instance = (toop != NULL) && toop->is_known_instance();
253N/A Node *start_mem = C->start()->proj_out(TypeFunc::Memory);
65N/A Node *prev = NULL;
65N/A Node *result = orig_mem;
65N/A while (prev != result) {
65N/A prev = result;
253N/A if (result == start_mem)
605N/A break; // hit one of our sentinels
65N/A if (result->is_Mem()) {
3619N/A const Type *at = igvn->type(result->in(MemNode::Address));
2306N/A if (at == Type::TOP)
2306N/A break; // Dead
2306N/A assert (at->isa_ptr() != NULL, "pointer type required.");
2306N/A int idx = C->get_alias_index(at->is_ptr());
2306N/A if (idx == alias_idx)
2306N/A break; // Found
2306N/A if (!is_instance && (at->isa_oopptr() == NULL ||
2306N/A !at->is_oopptr()->is_known_instance())) {
2306N/A break; // Do not skip store to general memory slice.
65N/A }
253N/A result = result->in(MemNode::Memory);
65N/A }
65N/A if (!is_instance)
65N/A continue; // don't search further for non-instance types
65N/A // skip over a call which does not affect this memory slice
65N/A if (result->is_Proj() && result->as_Proj()->_con == TypeFunc::Memory) {
65N/A Node *proj_in = result->in(0);
1735N/A if (proj_in->is_Allocate() && proj_in->_idx == (uint)toop->instance_id()) {
605N/A break; // hit one of our sentinels
253N/A } else if (proj_in->is_Call()) {
65N/A CallNode *call = proj_in->as_Call();
3619N/A if (!call->may_modify(toop, igvn)) {
65N/A result = call->in(TypeFunc::Memory);
65N/A }
65N/A } else if (proj_in->is_Initialize()) {
65N/A AllocateNode* alloc = proj_in->as_Initialize()->allocation();
65N/A // Stop if this is the initialization for the object instance which
65N/A // which contains this memory slice, otherwise skip over it.
1735N/A if (alloc == NULL || alloc->_idx != (uint)toop->instance_id()) {
65N/A result = proj_in->in(TypeFunc::Memory);
65N/A }
65N/A } else if (proj_in->is_MemBar()) {
65N/A result = proj_in->in(TypeFunc::Memory);
65N/A }
65N/A } else if (result->is_MergeMem()) {
65N/A MergeMemNode *mmem = result->as_MergeMem();
1735N/A result = step_through_mergemem(mmem, alias_idx, toop);
65N/A if (result == mmem->base_memory()) {
65N/A // Didn't find instance memory, search through general slice recursively.
65N/A result = mmem->memory_at(C->get_general_index(alias_idx));
3619N/A result = find_inst_mem(result, alias_idx, orig_phis);
65N/A if (C->failing()) {
65N/A return NULL;
65N/A }
65N/A mmem->set_memory_at(alias_idx, result);
65N/A }
65N/A } else if (result->is_Phi() &&
65N/A C->get_alias_index(result->as_Phi()->adr_type()) != alias_idx) {
3619N/A Node *un = result->as_Phi()->unique_input(igvn);
65N/A if (un != NULL) {
1101N/A orig_phis.append_if_missing(result->as_Phi());
65N/A result = un;
65N/A } else {
65N/A break;
65N/A }
1100N/A } else if (result->is_ClearArray()) {
3619N/A if (!ClearArrayNode::step_through(&result, (uint)toop->instance_id(), igvn)) {
1100N/A // Can not bypass initialization of the instance
1100N/A // we are looking for.
1100N/A break;
1100N/A }
1100N/A // Otherwise skip it (the call updated 'result' value).
584N/A } else if (result->Opcode() == Op_SCMemProj) {
584N/A assert(result->in(0)->is_LoadStore(), "sanity");
3619N/A const Type *at = igvn->type(result->in(0)->in(MemNode::Address));
584N/A if (at != Type::TOP) {
584N/A assert (at->isa_ptr() != NULL, "pointer type required.");
584N/A int idx = C->get_alias_index(at->is_ptr());
584N/A assert(idx != alias_idx, "Object is not scalar replaceable if a LoadStore node access its field");
584N/A break;
584N/A }
584N/A result = result->in(0)->in(MemNode::Memory);
65N/A }
65N/A }
247N/A if (result->is_Phi()) {
65N/A PhiNode *mphi = result->as_Phi();
65N/A assert(mphi->bottom_type() == Type::MEMORY, "memory phi required");
65N/A const TypePtr *t = mphi->adr_type();
2306N/A if (!is_instance) {
247N/A // Push all non-instance Phis on the orig_phis worklist to update inputs
247N/A // during Phase 4 if needed.
247N/A orig_phis.append_if_missing(mphi);
2306N/A } else if (C->get_alias_index(t) != alias_idx) {
2306N/A // Create a new Phi with the specified alias index type.
3619N/A result = split_memory_phi(mphi, alias_idx, orig_phis);
65N/A }
65N/A }
65N/A // the result is either MemNode, PhiNode, InitializeNode.
65N/A return result;
65N/A}
65N/A
0N/A//
0N/A// Convert the types of unescaped object to instance types where possible,
0N/A// propagate the new type information through the graph, and update memory
0N/A// edges and MergeMem inputs to reflect the new type.
0N/A//
0N/A// We start with allocations (and calls which may be allocations) on alloc_worklist.
0N/A// The processing is done in 4 phases:
0N/A//
0N/A// Phase 1: Process possible allocations from alloc_worklist. Create instance
0N/A// types for the CheckCastPP for allocations where possible.
0N/A// Propagate the the new types through users as follows:
0N/A// casts and Phi: push users on alloc_worklist
0N/A// AddP: cast Base and Address inputs to the instance type
0N/A// push any AddP users on alloc_worklist and push any memnode
0N/A// users onto memnode_worklist.
0N/A// Phase 2: Process MemNode's from memnode_worklist. compute new address type and
0N/A// search the Memory chain for a store with the appropriate type
0N/A// address type. If a Phi is found, create a new version with
605N/A// the appropriate memory slices from each of the Phi inputs.
0N/A// For stores, process the users as follows:
0N/A// MemNode: push on memnode_worklist
0N/A// MergeMem: push on mergemem_worklist
0N/A// Phase 3: Process MergeMem nodes from mergemem_worklist. Walk each memory slice
0N/A// moving the first node encountered of each instance type to the
0N/A// the input corresponding to its alias index.
0N/A// appropriate memory slice.
0N/A// Phase 4: Update the inputs of non-instance memory Phis and the Memory input of memnodes.
0N/A//
0N/A// In the following example, the CheckCastPP nodes are the cast of allocation
0N/A// results and the allocation of node 29 is unescaped and eligible to be an
0N/A// instance type.
0N/A//
0N/A// We start with:
0N/A//
0N/A// 7 Parm #memory
0N/A// 10 ConI "12"
0N/A// 19 CheckCastPP "Foo"
0N/A// 20 AddP _ 19 19 10 Foo+12 alias_index=4
0N/A// 29 CheckCastPP "Foo"
0N/A// 30 AddP _ 29 29 10 Foo+12 alias_index=4
0N/A//
0N/A// 40 StoreP 25 7 20 ... alias_index=4
0N/A// 50 StoreP 35 40 30 ... alias_index=4
0N/A// 60 StoreP 45 50 20 ... alias_index=4
0N/A// 70 LoadP _ 60 30 ... alias_index=4
0N/A// 80 Phi 75 50 60 Memory alias_index=4
0N/A// 90 LoadP _ 80 30 ... alias_index=4
0N/A// 100 LoadP _ 80 20 ... alias_index=4
0N/A//
0N/A//
0N/A// Phase 1 creates an instance type for node 29 assigning it an instance id of 24
0N/A// and creating a new alias index for node 30. This gives:
0N/A//
0N/A// 7 Parm #memory
0N/A// 10 ConI "12"
0N/A// 19 CheckCastPP "Foo"
0N/A// 20 AddP _ 19 19 10 Foo+12 alias_index=4
0N/A// 29 CheckCastPP "Foo" iid=24
0N/A// 30 AddP _ 29 29 10 Foo+12 alias_index=6 iid=24
0N/A//
0N/A// 40 StoreP 25 7 20 ... alias_index=4
0N/A// 50 StoreP 35 40 30 ... alias_index=6
0N/A// 60 StoreP 45 50 20 ... alias_index=4
0N/A// 70 LoadP _ 60 30 ... alias_index=6
0N/A// 80 Phi 75 50 60 Memory alias_index=4
0N/A// 90 LoadP _ 80 30 ... alias_index=6
0N/A// 100 LoadP _ 80 20 ... alias_index=4
0N/A//
0N/A// In phase 2, new memory inputs are computed for the loads and stores,
0N/A// And a new version of the phi is created. In phase 4, the inputs to
0N/A// node 80 are updated and then the memory nodes are updated with the
0N/A// values computed in phase 2. This results in:
0N/A//
0N/A// 7 Parm #memory
0N/A// 10 ConI "12"
0N/A// 19 CheckCastPP "Foo"
0N/A// 20 AddP _ 19 19 10 Foo+12 alias_index=4
0N/A// 29 CheckCastPP "Foo" iid=24
0N/A// 30 AddP _ 29 29 10 Foo+12 alias_index=6 iid=24
0N/A//
0N/A// 40 StoreP 25 7 20 ... alias_index=4
0N/A// 50 StoreP 35 7 30 ... alias_index=6
0N/A// 60 StoreP 45 40 20 ... alias_index=4
0N/A// 70 LoadP _ 50 30 ... alias_index=6
0N/A// 80 Phi 75 40 60 Memory alias_index=4
0N/A// 120 Phi 75 50 50 Memory alias_index=6
0N/A// 90 LoadP _ 120 30 ... alias_index=6
0N/A// 100 LoadP _ 80 20 ... alias_index=4
0N/A//
0N/Avoid ConnectionGraph::split_unique_types(GrowableArray<Node *> &alloc_worklist) {
0N/A GrowableArray<Node *> memnode_worklist;
0N/A GrowableArray<PhiNode *> orig_phis;
1841N/A PhaseIterGVN *igvn = _igvn;
0N/A uint new_index_start = (uint) _compile->num_alias_types();
1101N/A Arena* arena = Thread::current()->resource_area();
1101N/A VectorSet visited(arena);
3619N/A ideal_nodes.clear(); // Reset for use with set_map/get_map.
3619N/A uint unique_old = _compile->unique();
65N/A
65N/A // Phase 1: Process possible allocations from alloc_worklist.
65N/A // Create instance types for the CheckCastPP for allocations where possible.
244N/A //
244N/A // (Note: don't forget to change the order of the second AddP node on
244N/A // the alloc_worklist if the order of the worklist processing is changed,
244N/A // see the comment in find_second_addp().)
244N/A //
0N/A while (alloc_worklist.length() != 0) {
0N/A Node *n = alloc_worklist.pop();
0N/A uint ni = n->_idx;
0N/A if (n->is_Call()) {
0N/A CallNode *alloc = n->as_Call();
0N/A // copy escape information to call node
244N/A PointsToNode* ptn = ptnode_adr(alloc->_idx);
3619N/A PointsToNode::EscapeState es = ptn->escape_state();
65N/A // We have an allocation or call which returns a Java object,
65N/A // see if it is unescaped.
2896N/A if (es != PointsToNode::NoEscape || !ptn->scalar_replaceable())
0N/A continue;
784N/A // Find CheckCastPP for the allocate or for the return value of a call
784N/A n = alloc->result_cast();
784N/A if (n == NULL) { // No uses except Initialize node
784N/A if (alloc->is_Allocate()) {
784N/A // Set the scalar_replaceable flag for allocation
784N/A // so it could be eliminated if it has no uses.
784N/A alloc->as_Allocate()->_is_scalar_replaceable = true;
784N/A }
784N/A continue;
39N/A }
784N/A if (!n->is_CheckCastPP()) { // not unique CheckCastPP.
784N/A assert(!alloc->is_Allocate(), "allocation should have unique type");
65N/A continue;
784N/A }
784N/A
65N/A // The inline code for Object.clone() casts the allocation result to
247N/A // java.lang.Object and then to the actual type of the allocated
65N/A // object. Detect this case and use the second cast.
247N/A // Also detect j.l.reflect.Array.newInstance(jobject, jint) case when
247N/A // the allocation result is cast to java.lang.Object and then
247N/A // to the actual Array type.
65N/A if (alloc->is_Allocate() && n->as_Type()->type() == TypeInstPtr::NOTNULL
247N/A && (alloc->is_AllocateArray() ||
247N/A igvn->type(alloc->in(AllocateNode::KlassNode)) != TypeKlassPtr::OBJECT)) {
65N/A Node *cast2 = NULL;
65N/A for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
65N/A Node *use = n->fast_out(i);
65N/A if (use->is_CheckCastPP()) {
65N/A cast2 = use;
65N/A break;
65N/A }
65N/A }
65N/A if (cast2 != NULL) {
65N/A n = cast2;
65N/A } else {
784N/A // Non-scalar replaceable if the allocation type is unknown statically
784N/A // (reflection allocation), the object can't be restored during
784N/A // deoptimization without precise type.
65N/A continue;
65N/A }
65N/A }
784N/A if (alloc->is_Allocate()) {
784N/A // Set the scalar_replaceable flag for allocation
784N/A // so it could be eliminated.
784N/A alloc->as_Allocate()->_is_scalar_replaceable = true;
784N/A }
3619N/A set_escape_state(ptnode_adr(n->_idx), es); // CheckCastPP escape state
247N/A // in order for an object to be scalar-replaceable, it must be:
65N/A // - a direct allocation (not a call returning an object)
65N/A // - non-escaping
65N/A // - eligible to be a unique type
65N/A // - not determined to be ineligible by escape analysis
3619N/A set_map(alloc, n);
3619N/A set_map(n, alloc);
65N/A const TypeOopPtr *t = igvn->type(n)->isa_oopptr();
65N/A if (t == NULL)
2896N/A continue; // not a TypeOopPtr
3619N/A const TypeOopPtr* tinst = t->cast_to_exactness(true)->is_oopptr()->cast_to_instance_id(ni);
0N/A igvn->hash_delete(n);
0N/A igvn->set_type(n, tinst);
0N/A n->raise_bottom_type(tinst);
0N/A igvn->hash_insert(n);
65N/A record_for_optimizer(n);
2896N/A if (alloc->is_Allocate() && (t->isa_instptr() || t->isa_aryptr())) {
163N/A
163N/A // First, put on the worklist all Field edges from Connection Graph
163N/A // which is more accurate then putting immediate users from Ideal Graph.
3619N/A for (EdgeIterator e(ptn); e.has_next(); e.next()) {
3619N/A PointsToNode* tgt = e.get();
3619N/A Node* use = tgt->ideal_node();
3619N/A assert(tgt->is_Field() && use->is_AddP(),
163N/A "only AddP nodes are Field edges in CG");
163N/A if (use->outcnt() > 0) { // Don't process dead nodes
163N/A Node* addp2 = find_second_addp(use, use->in(AddPNode::Base));
163N/A if (addp2 != NULL) {
163N/A assert(alloc->is_AllocateArray(),"array allocation was expected");
163N/A alloc_worklist.append_if_missing(addp2);
163N/A }
163N/A alloc_worklist.append_if_missing(use);
163N/A }
163N/A }
163N/A
65N/A // An allocation may have an Initialize which has raw stores. Scan
65N/A // the users of the raw allocation result and push AddP users
65N/A // on alloc_worklist.
65N/A Node *raw_result = alloc->proj_out(TypeFunc::Parms);
65N/A assert (raw_result != NULL, "must have an allocation result");
65N/A for (DUIterator_Fast imax, i = raw_result->fast_outs(imax); i < imax; i++) {
65N/A Node *use = raw_result->fast_out(i);
65N/A if (use->is_AddP() && use->outcnt() > 0) { // Don't process dead nodes
65N/A Node* addp2 = find_second_addp(use, raw_result);
65N/A if (addp2 != NULL) {
65N/A assert(alloc->is_AllocateArray(),"array allocation was expected");
65N/A alloc_worklist.append_if_missing(addp2);
65N/A }
65N/A alloc_worklist.append_if_missing(use);
1100N/A } else if (use->is_MemBar()) {
65N/A memnode_worklist.append_if_missing(use);
65N/A }
65N/A }
65N/A }
0N/A } else if (n->is_AddP()) {
3619N/A JavaObjectNode* jobj = unique_java_object(get_addp_base(n));
3619N/A if (jobj == NULL || jobj == phantom_obj) {
3619N/A#ifdef ASSERT
3619N/A ptnode_adr(get_addp_base(n)->_idx)->dump();
3619N/A ptnode_adr(n->_idx)->dump();
3619N/A assert(jobj != NULL && jobj != phantom_obj, "escaped allocation");
3619N/A#endif
3619N/A _compile->record_failure(C2Compiler::retry_no_escape_analysis());
3619N/A return;
1100N/A }
3619N/A Node *base = get_map(jobj->idx()); // CheckCastPP node
3619N/A if (!split_AddP(n, base)) continue; // wrong type from dead path
65N/A } else if (n->is_Phi() ||
65N/A n->is_CheckCastPP() ||
168N/A n->is_EncodeP() ||
168N/A n->is_DecodeN() ||
65N/A (n->is_ConstraintCast() && n->Opcode() == Op_CastPP)) {
0N/A if (visited.test_set(n->_idx)) {
0N/A assert(n->is_Phi(), "loops only through Phi's");
0N/A continue; // already processed
0N/A }
3619N/A JavaObjectNode* jobj = unique_java_object(n);
3619N/A if (jobj == NULL || jobj == phantom_obj) {
3619N/A#ifdef ASSERT
3619N/A ptnode_adr(n->_idx)->dump();
3619N/A assert(jobj != NULL && jobj != phantom_obj, "escaped allocation");
3619N/A#endif
3619N/A _compile->record_failure(C2Compiler::retry_no_escape_analysis());
3619N/A return;
3619N/A } else {
3619N/A Node *val = get_map(jobj->idx()); // CheckCastPP node
0N/A TypeNode *tn = n->as_Type();
3619N/A const TypeOopPtr* tinst = igvn->type(val)->isa_oopptr();
223N/A assert(tinst != NULL && tinst->is_known_instance() &&
3619N/A tinst->instance_id() == jobj->idx() , "instance type expected.");
163N/A
163N/A const Type *tn_type = igvn->type(tn);
223N/A const TypeOopPtr *tn_t;
223N/A if (tn_type->isa_narrowoop()) {
223N/A tn_t = tn_type->make_ptr()->isa_oopptr();
223N/A } else {
223N/A tn_t = tn_type->isa_oopptr();
223N/A }
1100N/A if (tn_t != NULL && tinst->klass()->is_subtype_of(tn_t->klass())) {
163N/A if (tn_type->isa_narrowoop()) {
163N/A tn_type = tinst->make_narrowoop();
163N/A } else {
163N/A tn_type = tinst;
163N/A }
0N/A igvn->hash_delete(tn);
163N/A igvn->set_type(tn, tn_type);
163N/A tn->set_type(tn_type);
0N/A igvn->hash_insert(tn);
65N/A record_for_optimizer(n);
293N/A } else {
1100N/A assert(tn_type == TypePtr::NULL_PTR ||
1100N/A tn_t != NULL && !tinst->klass()->is_subtype_of(tn_t->klass()),
1100N/A "unexpected type");
1100N/A continue; // Skip dead path with different type
0N/A }
0N/A }
0N/A } else {
1100N/A debug_only(n->dump();)
1100N/A assert(false, "EA: unexpected node");
0N/A continue;
0N/A }
1100N/A // push allocation's users on appropriate worklist
0N/A for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
0N/A Node *use = n->fast_out(i);
0N/A if(use->is_Mem() && use->in(MemNode::Address) == n) {
1100N/A // Load/store to instance's field
65N/A memnode_worklist.append_if_missing(use);
1100N/A } else if (use->is_MemBar()) {
1100N/A memnode_worklist.append_if_missing(use);
65N/A } else if (use->is_AddP() && use->outcnt() > 0) { // No dead nodes
65N/A Node* addp2 = find_second_addp(use, n);
65N/A if (addp2 != NULL) {
65N/A alloc_worklist.append_if_missing(addp2);
65N/A }
65N/A alloc_worklist.append_if_missing(use);
65N/A } else if (use->is_Phi() ||
65N/A use->is_CheckCastPP() ||
168N/A use->is_EncodeP() ||
168N/A use->is_DecodeN() ||
65N/A (use->is_ConstraintCast() && use->Opcode() == Op_CastPP)) {
65N/A alloc_worklist.append_if_missing(use);
1100N/A#ifdef ASSERT
1100N/A } else if (use->is_Mem()) {
1100N/A assert(use->in(MemNode::Address) != n, "EA: missing allocation reference path");
1100N/A } else if (use->is_MergeMem()) {
1100N/A assert(_mergemem_worklist.contains(use->as_MergeMem()), "EA: missing MergeMem node in the worklist");
1100N/A } else if (use->is_SafePoint()) {
1100N/A // Look for MergeMem nodes for calls which reference unique allocation
1100N/A // (through CheckCastPP nodes) even for debug info.
1100N/A Node* m = use->in(TypeFunc::Memory);
1100N/A if (m->is_MergeMem()) {
1100N/A assert(_mergemem_worklist.contains(m->as_MergeMem()), "EA: missing MergeMem node in the worklist");
1100N/A }
1100N/A } else {
1100N/A uint op = use->Opcode();
1100N/A if (!(op == Op_CmpP || op == Op_Conv2B ||
1100N/A op == Op_CastP2X || op == Op_StoreCM ||
1100N/A op == Op_FastLock || op == Op_AryEq || op == Op_StrComp ||
1100N/A op == Op_StrEquals || op == Op_StrIndexOf)) {
1100N/A n->dump();
1100N/A use->dump();
1100N/A assert(false, "EA: missing allocation reference path");
1100N/A }
1100N/A#endif
0N/A }
0N/A }
0N/A
0N/A }
65N/A // New alias types were created in split_AddP().
0N/A uint new_index_end = (uint) _compile->num_alias_types();
3619N/A assert(unique_old == _compile->unique(), "there should be no new ideal nodes after Phase 1");
0N/A
0N/A // Phase 2: Process MemNode's from memnode_worklist. compute new address type and
0N/A // compute new values for Memory inputs (the Memory inputs are not
0N/A // actually updated until phase 4.)
0N/A if (memnode_worklist.length() == 0)
0N/A return; // nothing to do
0N/A while (memnode_worklist.length() != 0) {
0N/A Node *n = memnode_worklist.pop();
65N/A if (visited.test_set(n->_idx))
65N/A continue;
1100N/A if (n->is_Phi() || n->is_ClearArray()) {
1100N/A // we don't need to do anything, but the users must be pushed
1100N/A } else if (n->is_MemBar()) { // Initialize, MemBar nodes
1100N/A // we don't need to do anything, but the users must be pushed
1100N/A n = n->as_MemBar()->proj_out(TypeFunc::Memory);
65N/A if (n == NULL)
0N/A continue;
0N/A } else {
0N/A assert(n->is_Mem(), "memory node required.");
0N/A Node *addr = n->in(MemNode::Address);
0N/A const Type *addr_t = igvn->type(addr);
0N/A if (addr_t == Type::TOP)
0N/A continue;
0N/A assert (addr_t->isa_ptr() != NULL, "pointer type required.");
0N/A int alias_idx = _compile->get_alias_index(addr_t->is_ptr());
65N/A assert ((uint)alias_idx < new_index_end, "wrong alias index");
3619N/A Node *mem = find_inst_mem(n->in(MemNode::Memory), alias_idx, orig_phis);
38N/A if (_compile->failing()) {
38N/A return;
38N/A }
65N/A if (mem != n->in(MemNode::Memory)) {
1101N/A // We delay the memory edge update since we need old one in
1101N/A // MergeMem code below when instances memory slices are separated.
3619N/A set_map(n, mem);
65N/A }
0N/A if (n->is_Load()) {
0N/A continue; // don't push users
0N/A } else if (n->is_LoadStore()) {
0N/A // get the memory projection
0N/A for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
0N/A Node *use = n->fast_out(i);
0N/A if (use->Opcode() == Op_SCMemProj) {
0N/A n = use;
0N/A break;
0N/A }
0N/A }
0N/A assert(n->Opcode() == Op_SCMemProj, "memory projection required");
0N/A }
0N/A }
0N/A // push user on appropriate worklist
0N/A for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
0N/A Node *use = n->fast_out(i);
1100N/A if (use->is_Phi() || use->is_ClearArray()) {
65N/A memnode_worklist.append_if_missing(use);
0N/A } else if(use->is_Mem() && use->in(MemNode::Memory) == n) {
1100N/A if (use->Opcode() == Op_StoreCM) // Ignore cardmark stores
1100N/A continue;
65N/A memnode_worklist.append_if_missing(use);
1100N/A } else if (use->is_MemBar()) {
65N/A memnode_worklist.append_if_missing(use);
1100N/A#ifdef ASSERT
1100N/A } else if(use->is_Mem()) {
1100N/A assert(use->in(MemNode::Memory) != n, "EA: missing memory path");
0N/A } else if (use->is_MergeMem()) {
1100N/A assert(_mergemem_worklist.contains(use->as_MergeMem()), "EA: missing MergeMem node in the worklist");
1100N/A } else {
1100N/A uint op = use->Opcode();
1100N/A if (!(op == Op_StoreCM ||
1100N/A (op == Op_CallLeaf && use->as_CallLeaf()->_name != NULL &&
1100N/A strcmp(use->as_CallLeaf()->_name, "g1_wb_pre") == 0) ||
1100N/A op == Op_AryEq || op == Op_StrComp ||
1100N/A op == Op_StrEquals || op == Op_StrIndexOf)) {
1100N/A n->dump();
1100N/A use->dump();
1100N/A assert(false, "EA: missing memory path");
1100N/A }
1100N/A#endif
0N/A }
0N/A }
0N/A }
0N/A
65N/A // Phase 3: Process MergeMem nodes from mergemem_worklist.
1100N/A // Walk each memory slice moving the first node encountered of each
65N/A // instance type to the the input corresponding to its alias index.
1100N/A uint length = _mergemem_worklist.length();
1100N/A for( uint next = 0; next < length; ++next ) {
1100N/A MergeMemNode* nmm = _mergemem_worklist.at(next);
1100N/A assert(!visited.test_set(nmm->_idx), "should not be visited before");
0N/A // Note: we don't want to use MergeMemStream here because we only want to
1100N/A // scan inputs which exist at the start, not ones we add during processing.
1100N/A // Note 2: MergeMem may already contains instance memory slices added
1100N/A // during find_inst_mem() call when memory nodes were processed above.
1100N/A igvn->hash_delete(nmm);
0N/A uint nslices = nmm->req();
0N/A for (uint i = Compile::AliasIdxRaw+1; i < nslices; i++) {
65N/A Node* mem = nmm->in(i);
65N/A Node* cur = NULL;
0N/A if (mem == NULL || mem->is_top())
0N/A continue;
1101N/A // First, update mergemem by moving memory nodes to corresponding slices
1101N/A // if their type became more precise since this mergemem was created.
0N/A while (mem->is_Mem()) {
0N/A const Type *at = igvn->type(mem->in(MemNode::Address));
0N/A if (at != Type::TOP) {
0N/A assert (at->isa_ptr() != NULL, "pointer type required.");
0N/A uint idx = (uint)_compile->get_alias_index(at->is_ptr());
0N/A if (idx == i) {
0N/A if (cur == NULL)
0N/A cur = mem;
0N/A } else {
0N/A if (idx >= nmm->req() || nmm->is_empty_memory(nmm->in(idx))) {
0N/A nmm->set_memory_at(idx, mem);
0N/A }
0N/A }
0N/A }
0N/A mem = mem->in(MemNode::Memory);
0N/A }
0N/A nmm->set_memory_at(i, (cur != NULL) ? cur : mem);
65N/A // Find any instance of the current type if we haven't encountered
1101N/A // already a memory slice of the instance along the memory chain.
65N/A for (uint ni = new_index_start; ni < new_index_end; ni++) {
65N/A if((uint)_compile->get_general_index(ni) == i) {
65N/A Node *m = (ni >= nmm->req()) ? nmm->empty_memory() : nmm->in(ni);
65N/A if (nmm->is_empty_memory(m)) {
3619N/A Node* result = find_inst_mem(mem, ni, orig_phis);
65N/A if (_compile->failing()) {
65N/A return;
65N/A }
65N/A nmm->set_memory_at(ni, result);
65N/A }
65N/A }
65N/A }
65N/A }
65N/A // Find the rest of instances values
65N/A for (uint ni = new_index_start; ni < new_index_end; ni++) {
1101N/A const TypeOopPtr *tinst = _compile->get_adr_type(ni)->isa_oopptr();
65N/A Node* result = step_through_mergemem(nmm, ni, tinst);
65N/A if (result == nmm->base_memory()) {
65N/A // Didn't find instance memory, search through general slice recursively.
1101N/A result = nmm->memory_at(_compile->get_general_index(ni));
3619N/A result = find_inst_mem(result, ni, orig_phis);
65N/A if (_compile->failing()) {
65N/A return;
65N/A }
65N/A nmm->set_memory_at(ni, result);
65N/A }
65N/A }
65N/A igvn->hash_insert(nmm);
65N/A record_for_optimizer(nmm);
0N/A }
0N/A
65N/A // Phase 4: Update the inputs of non-instance memory Phis and
65N/A // the Memory input of memnodes
0N/A // First update the inputs of any non-instance Phi's from
0N/A // which we split out an instance Phi. Note we don't have
0N/A // to recursively process Phi's encounted on the input memory
0N/A // chains as is done in split_memory_phi() since they will
0N/A // also be processed here.
247N/A for (int j = 0; j < orig_phis.length(); j++) {
247N/A PhiNode *phi = orig_phis.at(j);
0N/A int alias_idx = _compile->get_alias_index(phi->adr_type());
0N/A igvn->hash_delete(phi);
0N/A for (uint i = 1; i < phi->req(); i++) {
0N/A Node *mem = phi->in(i);
3619N/A Node *new_mem = find_inst_mem(mem, alias_idx, orig_phis);
65N/A if (_compile->failing()) {
65N/A return;
65N/A }
0N/A if (mem != new_mem) {
0N/A phi->set_req(i, new_mem);
0N/A }
0N/A }
0N/A igvn->hash_insert(phi);
0N/A record_for_optimizer(phi);
0N/A }
0N/A
0N/A // Update the memory inputs of MemNodes with the value we computed
1101N/A // in Phase 2 and move stores memory users to corresponding memory slices.
2375N/A // Disable memory split verification code until the fix for 6984348.
2375N/A // Currently it produces false negative results since it does not cover all cases.
2375N/A#if 0 // ifdef ASSERT
2121N/A visited.Reset();
1101N/A Node_Stack old_mems(arena, _compile->unique() >> 2);
1101N/A#endif
3619N/A for (uint i = 0; i < ideal_nodes.size(); i++) {
3619N/A Node* n = ideal_nodes.at(i);
3619N/A Node* nmem = get_map(n->_idx);
3619N/A assert(nmem != NULL, "sanity");
3619N/A if (n->is_Mem()) {
2375N/A#if 0 // ifdef ASSERT
3619N/A Node* old_mem = n->in(MemNode::Memory);
3619N/A if (!visited.test_set(old_mem->_idx)) {
3619N/A old_mems.push(old_mem, old_mem->outcnt());
3619N/A }
1101N/A#endif
3619N/A assert(n->in(MemNode::Memory) != nmem, "sanity");
3619N/A if (!n->is_Load()) {
3619N/A // Move memory users of a store first.
3619N/A move_inst_mem(n, orig_phis);
0N/A }
3619N/A // Now update memory input
3619N/A igvn->hash_delete(n);
3619N/A n->set_req(MemNode::Memory, nmem);
3619N/A igvn->hash_insert(n);
3619N/A record_for_optimizer(n);
3619N/A } else {
3619N/A assert(n->is_Allocate() || n->is_CheckCastPP() ||
3619N/A n->is_AddP() || n->is_Phi(), "unknown node used for set_map()");
0N/A }
0N/A }
2375N/A#if 0 // ifdef ASSERT
1101N/A // Verify that memory was split correctly
1101N/A while (old_mems.is_nonempty()) {
1101N/A Node* old_mem = old_mems.node();
1101N/A uint old_cnt = old_mems.index();
1101N/A old_mems.pop();
2375N/A assert(old_cnt == old_mem->outcnt(), "old mem could be lost");
1101N/A }
1101N/A#endif
0N/A}
0N/A
3619N/A#ifndef PRODUCT
3619N/Astatic const char *node_type_names[] = {
3619N/A "UnknownType",
3619N/A "JavaObject",
3619N/A "LocalVar",
3619N/A "Field",
3619N/A "Arraycopy"
3619N/A};
3226N/A
3619N/Astatic const char *esc_names[] = {
3619N/A "UnknownEscape",
3619N/A "NoEscape",
3619N/A "ArgEscape",
3619N/A "GlobalEscape"
3619N/A};
0N/A
3619N/Avoid PointsToNode::dump(bool print_state) const {
3619N/A NodeType nt = node_type();
3619N/A tty->print("%s ", node_type_names[(int) nt]);
3619N/A if (print_state) {
3619N/A EscapeState es = escape_state();
3619N/A EscapeState fields_es = fields_escape_state();
3619N/A tty->print("%s(%s) ", esc_names[(int)es], esc_names[(int)fields_es]);
3619N/A if (nt == PointsToNode::JavaObject && !this->scalar_replaceable())
4049N/A tty->print("NSR ");
2956N/A }
3619N/A if (is_Field()) {
3619N/A FieldNode* f = (FieldNode*)this;
4049N/A if (f->is_oop())
4049N/A tty->print("oop ");
4049N/A if (f->offset() > 0)
4049N/A tty->print("+%d ", f->offset());
3619N/A tty->print("(");
3619N/A for (BaseIterator i(f); i.has_next(); i.next()) {
3619N/A PointsToNode* b = i.get();
3619N/A tty->print(" %d%s", b->idx(),(b->is_JavaObject() ? "P" : ""));
3043N/A }
3619N/A tty->print(" )");
0N/A }
3619N/A tty->print("[");
3619N/A for (EdgeIterator i(this); i.has_next(); i.next()) {
3619N/A PointsToNode* e = i.get();
3619N/A tty->print(" %d%s%s", e->idx(),(e->is_JavaObject() ? "P" : (e->is_Field() ? "F" : "")), e->is_Arraycopy() ? "cp" : "");
1100N/A }
3619N/A tty->print(" [");
3619N/A for (UseIterator i(this); i.has_next(); i.next()) {
3619N/A PointsToNode* u = i.get();
3619N/A bool is_base = false;
3619N/A if (PointsToNode::is_base_use(u)) {
3619N/A is_base = true;
3619N/A u = PointsToNode::get_use_node(u)->as_Field();
2896N/A }
3619N/A tty->print(" %d%s%s", u->idx(), is_base ? "b" : "", u->is_Arraycopy() ? "cp" : "");
2956N/A }
3619N/A tty->print(" ]] ");
3619N/A if (_node == NULL)
3619N/A tty->print_cr("<null>");
3619N/A else
3619N/A _node->dump();
2956N/A}
2956N/A
3619N/Avoid ConnectionGraph::dump(GrowableArray<PointsToNode*>& ptnodes_worklist) {
0N/A bool first = true;
3619N/A int ptnodes_length = ptnodes_worklist.length();
3619N/A for (int i = 0; i < ptnodes_length; i++) {
3619N/A PointsToNode *ptn = ptnodes_worklist.at(i);
3619N/A if (ptn == NULL || !ptn->is_JavaObject())
0N/A continue;
3619N/A PointsToNode::EscapeState es = ptn->escape_state();
3619N/A if (ptn->ideal_node()->is_Allocate() && (es == PointsToNode::NoEscape || Verbose)) {
65N/A if (first) {
65N/A tty->cr();
65N/A tty->print("======== Connection graph for ");
244N/A _compile->method()->print_short_name();
65N/A tty->cr();
65N/A first = false;
65N/A }
65N/A ptn->dump();
3619N/A // Print all locals and fields which reference this allocation
3619N/A for (UseIterator j(ptn); j.has_next(); j.next()) {
3619N/A PointsToNode* use = j.get();
3619N/A if (use->is_LocalVar()) {
3619N/A use->dump(Verbose);
3619N/A } else if (Verbose) {
3619N/A use->dump();
65N/A }
65N/A }
65N/A tty->cr();
0N/A }
0N/A }
0N/A}
0N/A#endif