escape.cpp revision 163
0N/A/*
0N/A * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
0N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
0N/A * CA 95054 USA or visit www.sun.com if you need additional information or
0N/A * have any questions.
0N/A *
0N/A */
0N/A
0N/A#include "incls/_precompiled.incl"
0N/A#include "incls/_escape.cpp.incl"
0N/A
0N/Auint PointsToNode::edge_target(uint e) const {
0N/A assert(_edges != NULL && e < (uint)_edges->length(), "valid edge index");
0N/A return (_edges->at(e) >> EdgeShift);
0N/A}
0N/A
0N/APointsToNode::EdgeType PointsToNode::edge_type(uint e) const {
0N/A assert(_edges != NULL && e < (uint)_edges->length(), "valid edge index");
0N/A return (EdgeType) (_edges->at(e) & EdgeMask);
0N/A}
0N/A
0N/Avoid PointsToNode::add_edge(uint targIdx, PointsToNode::EdgeType et) {
0N/A uint v = (targIdx << EdgeShift) + ((uint) et);
0N/A if (_edges == NULL) {
0N/A Arena *a = Compile::current()->comp_arena();
0N/A _edges = new(a) GrowableArray<uint>(a, INITIAL_EDGE_COUNT, 0, 0);
0N/A }
0N/A _edges->append_if_missing(v);
0N/A}
0N/A
0N/Avoid PointsToNode::remove_edge(uint targIdx, PointsToNode::EdgeType et) {
0N/A uint v = (targIdx << EdgeShift) + ((uint) et);
0N/A
0N/A _edges->remove(v);
0N/A}
0N/A
0N/A#ifndef PRODUCT
77N/Astatic const char *node_type_names[] = {
0N/A "UnknownType",
0N/A "JavaObject",
0N/A "LocalVar",
0N/A "Field"
0N/A};
0N/A
77N/Astatic const char *esc_names[] = {
0N/A "UnknownEscape",
65N/A "NoEscape",
65N/A "ArgEscape",
65N/A "GlobalEscape"
0N/A};
0N/A
77N/Astatic const char *edge_type_suffix[] = {
0N/A "?", // UnknownEdge
0N/A "P", // PointsToEdge
0N/A "D", // DeferredEdge
0N/A "F" // FieldEdge
0N/A};
0N/A
0N/Avoid PointsToNode::dump() const {
0N/A NodeType nt = node_type();
0N/A EscapeState es = escape_state();
65N/A tty->print("%s %s %s [[", node_type_names[(int) nt], esc_names[(int) es], _scalar_replaceable ? "" : "NSR");
0N/A for (uint i = 0; i < edge_count(); i++) {
0N/A tty->print(" %d%s", edge_target(i), edge_type_suffix[(int) edge_type(i)]);
0N/A }
0N/A tty->print("]] ");
0N/A if (_node == NULL)
0N/A tty->print_cr("<null>");
0N/A else
0N/A _node->dump();
0N/A}
0N/A#endif
0N/A
0N/AConnectionGraph::ConnectionGraph(Compile * C) : _processed(C->comp_arena()), _node_map(C->comp_arena()) {
0N/A _collecting = true;
0N/A this->_compile = C;
0N/A const PointsToNode &dummy = PointsToNode();
65N/A int sz = C->unique();
65N/A _nodes = new(C->comp_arena()) GrowableArray<PointsToNode>(C->comp_arena(), sz, sz, dummy);
0N/A _phantom_object = C->top()->_idx;
0N/A PointsToNode *phn = ptnode_adr(_phantom_object);
65N/A phn->_node = C->top();
0N/A phn->set_node_type(PointsToNode::JavaObject);
0N/A phn->set_escape_state(PointsToNode::GlobalEscape);
0N/A}
0N/A
0N/Avoid ConnectionGraph::add_pointsto_edge(uint from_i, uint to_i) {
0N/A PointsToNode *f = ptnode_adr(from_i);
0N/A PointsToNode *t = ptnode_adr(to_i);
0N/A
0N/A assert(f->node_type() != PointsToNode::UnknownType && t->node_type() != PointsToNode::UnknownType, "node types must be set");
0N/A assert(f->node_type() == PointsToNode::LocalVar || f->node_type() == PointsToNode::Field, "invalid source of PointsTo edge");
0N/A assert(t->node_type() == PointsToNode::JavaObject, "invalid destination of PointsTo edge");
0N/A f->add_edge(to_i, PointsToNode::PointsToEdge);
0N/A}
0N/A
0N/Avoid ConnectionGraph::add_deferred_edge(uint from_i, uint to_i) {
0N/A PointsToNode *f = ptnode_adr(from_i);
0N/A PointsToNode *t = ptnode_adr(to_i);
0N/A
0N/A assert(f->node_type() != PointsToNode::UnknownType && t->node_type() != PointsToNode::UnknownType, "node types must be set");
0N/A assert(f->node_type() == PointsToNode::LocalVar || f->node_type() == PointsToNode::Field, "invalid source of Deferred edge");
0N/A assert(t->node_type() == PointsToNode::LocalVar || t->node_type() == PointsToNode::Field, "invalid destination of Deferred edge");
0N/A // don't add a self-referential edge, this can occur during removal of
0N/A // deferred edges
0N/A if (from_i != to_i)
0N/A f->add_edge(to_i, PointsToNode::DeferredEdge);
0N/A}
0N/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
0N/Avoid ConnectionGraph::add_field_edge(uint from_i, uint to_i, int offset) {
0N/A PointsToNode *f = ptnode_adr(from_i);
0N/A PointsToNode *t = ptnode_adr(to_i);
0N/A
0N/A assert(f->node_type() != PointsToNode::UnknownType && t->node_type() != PointsToNode::UnknownType, "node types must be set");
0N/A assert(f->node_type() == PointsToNode::JavaObject, "invalid destination of Field edge");
0N/A assert(t->node_type() == PointsToNode::Field, "invalid destination of Field edge");
0N/A assert (t->offset() == -1 || t->offset() == offset, "conflicting field offsets");
0N/A t->set_offset(offset);
0N/A
0N/A f->add_edge(to_i, PointsToNode::FieldEdge);
0N/A}
0N/A
0N/Avoid ConnectionGraph::set_escape_state(uint ni, PointsToNode::EscapeState es) {
0N/A PointsToNode *npt = ptnode_adr(ni);
0N/A PointsToNode::EscapeState old_es = npt->escape_state();
0N/A if (es > old_es)
0N/A npt->set_escape_state(es);
0N/A}
0N/A
65N/Avoid ConnectionGraph::add_node(Node *n, PointsToNode::NodeType nt,
65N/A PointsToNode::EscapeState es, bool done) {
65N/A PointsToNode* ptadr = ptnode_adr(n->_idx);
65N/A ptadr->_node = n;
65N/A ptadr->set_node_type(nt);
65N/A
65N/A // inline set_escape_state(idx, es);
65N/A PointsToNode::EscapeState old_es = ptadr->escape_state();
65N/A if (es > old_es)
65N/A ptadr->set_escape_state(es);
65N/A
65N/A if (done)
65N/A _processed.set(n->_idx);
65N/A}
65N/A
0N/APointsToNode::EscapeState ConnectionGraph::escape_state(Node *n, PhaseTransform *phase) {
0N/A uint idx = n->_idx;
0N/A PointsToNode::EscapeState es;
0N/A
65N/A // If we are still collecting or there were no non-escaping allocations
65N/A // we don't know the answer yet
65N/A if (_collecting || !_has_allocations)
0N/A return PointsToNode::UnknownEscape;
0N/A
0N/A // if the node was created after the escape computation, return
0N/A // UnknownEscape
0N/A if (idx >= (uint)_nodes->length())
0N/A return PointsToNode::UnknownEscape;
0N/A
0N/A es = _nodes->at_grow(idx).escape_state();
0N/A
0N/A // if we have already computed a value, return it
0N/A if (es != PointsToNode::UnknownEscape)
0N/A return es;
0N/A
0N/A // compute max escape state of anything this node could point to
0N/A VectorSet ptset(Thread::current()->resource_area());
0N/A PointsTo(ptset, n, phase);
65N/A for(VectorSetI i(&ptset); i.test() && es != PointsToNode::GlobalEscape; ++i) {
0N/A uint pt = i.elem;
65N/A PointsToNode::EscapeState pes = _nodes->adr_at(pt)->escape_state();
0N/A if (pes > es)
0N/A es = pes;
0N/A }
0N/A // cache the computed escape state
0N/A assert(es != PointsToNode::UnknownEscape, "should have computed an escape state");
0N/A _nodes->adr_at(idx)->set_escape_state(es);
0N/A return es;
0N/A}
0N/A
0N/Avoid ConnectionGraph::PointsTo(VectorSet &ptset, Node * n, PhaseTransform *phase) {
0N/A VectorSet visited(Thread::current()->resource_area());
0N/A GrowableArray<uint> worklist;
0N/A
65N/A#ifdef ASSERT
0N/A Node *orig_n = n;
0N/A#endif
0N/A
0N/A n = n->uncast();
0N/A PointsToNode npt = _nodes->at_grow(n->_idx);
0N/A
0N/A // If we have a JavaObject, return just that object
65N/A if (npt.node_type() == PointsToNode::JavaObject) {
65N/A ptset.set(n->_idx);
0N/A return;
0N/A }
0N/A#ifdef ASSERT
0N/A if (npt._node == NULL) {
65N/A if (orig_n != n)
0N/A orig_n->dump();
65N/A n->dump();
0N/A assert(npt._node != NULL, "unregistered node");
0N/A }
0N/A#endif
65N/A worklist.push(n->_idx);
0N/A while(worklist.length() > 0) {
0N/A int ni = worklist.pop();
65N/A PointsToNode pn = _nodes->at_grow(ni);
0N/A if (!visited.test_set(ni)) {
0N/A // ensure that all inputs of a Phi have been processed
65N/A assert(!_collecting || !pn._node->is_Phi() || _processed.test(ni),"");
0N/A
65N/A int edges_processed = 0;
65N/A for (uint e = 0; e < pn.edge_count(); e++) {
0N/A uint etgt = pn.edge_target(e);
0N/A PointsToNode::EdgeType et = pn.edge_type(e);
0N/A if (et == PointsToNode::PointsToEdge) {
65N/A ptset.set(etgt);
65N/A edges_processed++;
0N/A } else if (et == PointsToNode::DeferredEdge) {
0N/A worklist.push(etgt);
0N/A edges_processed++;
0N/A } else {
0N/A assert(false,"neither PointsToEdge or DeferredEdge");
0N/A }
0N/A }
0N/A if (edges_processed == 0) {
0N/A // no deferred or pointsto edges found. Assume the value was set
0N/A // outside this method. Add the phantom object to the pointsto set.
0N/A ptset.set(_phantom_object);
0N/A }
0N/A }
65N/A }
65N/A}
0N/A
0N/Avoid ConnectionGraph::remove_deferred(uint ni, GrowableArray<uint>* deferred_edges, VectorSet* visited) {
0N/A // This method is most expensive during ConnectionGraph construction.
0N/A // Reuse vectorSet and an additional growable array for deferred edges.
65N/A deferred_edges->clear();
0N/A visited->Clear();
0N/A
0N/A uint i = 0;
0N/A PointsToNode *ptn = ptnode_adr(ni);
0N/A
65N/A // Mark current edges as visited and move deferred edges to separate array.
65N/A while (i < ptn->edge_count()) {
65N/A uint t = ptn->edge_target(i);
65N/A#ifdef ASSERT
65N/A assert(!visited->test_set(t), "expecting no duplications");
0N/A#else
0N/A visited->set(t);
0N/A#endif
0N/A if (ptn->edge_type(i) == PointsToNode::DeferredEdge) {
0N/A ptn->remove_edge(t, PointsToNode::DeferredEdge);
0N/A deferred_edges->append(t);
0N/A } else {
0N/A i++;
0N/A }
0N/A }
0N/A for (int next = 0; next < deferred_edges->length(); ++next) {
0N/A uint t = deferred_edges->at(next);
0N/A PointsToNode *ptt = ptnode_adr(t);
0N/A for (uint j = 0; j < ptt->edge_count(); j++) {
0N/A uint n1 = ptt->edge_target(j);
0N/A if (visited->test_set(n1))
0N/A continue;
0N/A switch(ptt->edge_type(j)) {
0N/A case PointsToNode::PointsToEdge:
0N/A add_pointsto_edge(ni, n1);
0N/A if(n1 == _phantom_object) {
0N/A // Special case - field set outside (globally escaping).
0N/A ptn->set_escape_state(PointsToNode::GlobalEscape);
0N/A }
0N/A break;
0N/A case PointsToNode::DeferredEdge:
0N/A deferred_edges->append(n1);
0N/A break;
0N/A case PointsToNode::FieldEdge:
0N/A assert(false, "invalid connection graph");
0N/A break;
0N/A }
0N/A }
0N/A }
0N/A}
0N/A
0N/A
0N/A// Add an edge to node given by "to_i" from any field of adr_i whose offset
65N/A// matches "offset" A deferred edge is added if to_i is a LocalVar, and
65N/A// a pointsto edge is added if it is a JavaObject
0N/A
0N/Avoid ConnectionGraph::add_edge_from_fields(uint adr_i, uint to_i, int offs) {
0N/A PointsToNode an = _nodes->at_grow(adr_i);
0N/A PointsToNode to = _nodes->at_grow(to_i);
0N/A bool deferred = (to.node_type() == PointsToNode::LocalVar);
0N/A
0N/A for (uint fe = 0; fe < an.edge_count(); fe++) {
0N/A assert(an.edge_type(fe) == PointsToNode::FieldEdge, "expecting a field edge");
0N/A int fi = an.edge_target(fe);
0N/A PointsToNode pf = _nodes->at_grow(fi);
0N/A int po = pf.offset();
0N/A if (po == offs || po == Type::OffsetBot || offs == Type::OffsetBot) {
0N/A if (deferred)
0N/A add_deferred_edge(fi, to_i);
0N/A else
0N/A add_pointsto_edge(fi, to_i);
0N/A }
65N/A }
65N/A}
65N/A
65N/A// Add a deferred edge from node given by "from_i" to any field of adr_i
65N/A// whose offset matches "offset".
65N/Avoid ConnectionGraph::add_deferred_edge_to_fields(uint from_i, uint adr_i, int offs) {
65N/A PointsToNode an = _nodes->at_grow(adr_i);
65N/A for (uint fe = 0; fe < an.edge_count(); fe++) {
65N/A assert(an.edge_type(fe) == PointsToNode::FieldEdge, "expecting a field edge");
65N/A int fi = an.edge_target(fe);
65N/A PointsToNode pf = _nodes->at_grow(fi);
65N/A int po = pf.offset();
65N/A if (pf.edge_count() == 0) {
65N/A // we have not seen any stores to this field, assume it was set outside this method
65N/A add_pointsto_edge(fi, _phantom_object);
65N/A }
65N/A if (po == offs || po == Type::OffsetBot || offs == Type::OffsetBot) {
65N/A add_deferred_edge(from_i, fi);
65N/A }
65N/A }
65N/A}
65N/A
65N/A// Helper functions
65N/A
65N/Astatic Node* 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
77N/A // |
77N/A // Proj #5 ( oop result )
77N/A // top |
65N/A // \ |
65N/A // AddP ( base == top )
65N/A //
65N/A // case #4. Array's element reference:
77N/A // {CheckCastPP | CastPP}
77N/A // | | |
77N/A // | AddP ( array's element offset )
77N/A // | |
77N/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.
77N/A // Allocate
77N/A // |
77N/A // Proj #5 ( oop result )
0N/A // | |
65N/A // AddP ( base == address )
65N/A //
65N/A // case #6. Constant Pool, ThreadLocal, CastX2P or
65N/A // Raw object's field reference:
65N/A // {ConP, ThreadLocal, CastX2P, raw Load}
65N/A // top |
65N/A // \ |
65N/A // AddP ( base == top )
65N/A //
65N/A // case #7. Klass's field reference.
65N/A // LoadKlass
65N/A // | |
65N/A // AddP ( base == address )
65N/A //
65N/A Node *base = addp->in(AddPNode::Base)->uncast();
65N/A if (base->is_top()) { // The AddP case #3 and #6.
65N/A base = addp->in(AddPNode::Address)->uncast();
65N/A assert(base->Opcode() == Op_ConP || base->Opcode() == Op_ThreadLocal ||
65N/A base->Opcode() == Op_CastX2P ||
65N/A (base->is_Mem() && base->bottom_type() == TypeRawPtr::NOTNULL) ||
65N/A (base->is_Proj() && base->in(0)->is_Allocate()), "sanity");
65N/A }
65N/A return base;
65N/A}
65N/A
65N/Astatic Node* find_second_addp(Node* addp, Node* n) {
65N/A assert(addp->is_AddP() && addp->outcnt() > 0, "Don't process dead nodes");
65N/A
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
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 //
0N/A // ArrayAllocation
0N/A // |
0N/A // CheckCastPP
0N/A // |
0N/A // memProj (from ArrayAllocation CheckCastPP)
0N/A // | ||
0N/A // | || Int (element index)
65N/A // | || | ConI (log(element size))
65N/A // | || | /
0N/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 //
0N/A return addp2;
0N/A }
0N/A return NULL;
0N/A}
65N/A
65N/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//
0N/Avoid ConnectionGraph::split_AddP(Node *addp, Node *base, PhaseGVN *igvn) {
65N/A const TypeOopPtr *base_t = igvn->type(base)->isa_oopptr();
65N/A assert(base_t != NULL && base_t->is_instance(), "expecting instance oopptr");
0N/A const TypeOopPtr *t = igvn->type(addp)->isa_oopptr();
0N/A if (t == NULL) {
65N/A // We are computing a raw address for a store captured by an Initialize
0N/A // compute an appropriate address type.
0N/A assert(igvn->type(addp) == TypeRawPtr::NOTNULL, "must be raw pointer");
0N/A assert(addp->in(AddPNode::Address)->is_Proj(), "base of raw address must be result projection from allocation");
0N/A int offs = (int)igvn->find_intptr_t_con(addp->in(AddPNode::Offset), Type::OffsetBot);
0N/A assert(offs != Type::OffsetBot, "offset must be a constant");
0N/A t = base_t->add_offset(offs)->is_oopptr();
0N/A }
0N/A uint inst_id = base_t->instance_id();
0N/A assert(!t->is_instance() || t->instance_id() == inst_id,
0N/A "old type must be non-instance or match new type");
0N/A const TypeOopPtr *tinst = base_t->add_offset(t->offset())->is_oopptr();
0N/A // Do NOT remove the next call: ensure an new alias index is allocated
0N/A // for the instance type
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
0N/A set_map(addp->_idx, get_map(base->_idx));
0N/A // if the Address input is not the appropriate instance type
0N/A // (due to intervening casts,) insert a cast
65N/A Node *adr = addp->in(AddPNode::Address);
65N/A const TypeOopPtr *atype = igvn->type(adr)->isa_oopptr();
0N/A if (atype != NULL && atype->instance_id() != inst_id) {
0N/A assert(!atype->is_instance(), "no conflicting instances");
0N/A const TypeOopPtr *new_atype = base_t->add_offset(atype->offset())->isa_oopptr();
0N/A Node *acast = new (_compile, 2) CastPPNode(adr, new_atype);
0N/A acast->set_req(0, adr->in(0));
0N/A igvn->set_type(acast, new_atype);
0N/A record_for_optimizer(acast);
0N/A Node *bcast = acast;
0N/A Node *abase = addp->in(AddPNode::Base);
0N/A if (abase != adr) {
0N/A bcast = new (_compile, 2) CastPPNode(abase, base_t);
0N/A bcast->set_req(0, abase->in(0));
65N/A igvn->set_type(bcast, base_t);
0N/A record_for_optimizer(bcast);
0N/A }
0N/A igvn->hash_delete(addp);
0N/A addp->set_req(AddPNode::Base, bcast);
0N/A addp->set_req(AddPNode::Address, acast);
0N/A igvn->hash_insert(addp);
0N/A }
38N/A // Put on IGVN worklist since at least addp's type was changed above.
38N/A record_for_optimizer(addp);
38N/A}
38N/A
38N/A//
38N/A// Create a new version of orig_phi if necessary. Returns either the newly
38N/A// created phi or an existing phi. Sets create_new to indicate wheter a new
38N/A// phi was created. Cache the last newly created phi in the node map.
38N/A//
0N/APhiNode *ConnectionGraph::create_split_phi(PhiNode *orig_phi, int alias_idx, GrowableArray<PhiNode *> &orig_phi_worklist, PhaseGVN *igvn, bool &new_created) {
65N/A Compile *C = _compile;
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
0N/A if (phi_alias_idx == alias_idx) {
0N/A return orig_phi;
0N/A }
0N/A // have we already 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 }
0N/A if ((int)C->unique() + 2*NodeLimitFudgeFactor > MaxNodeLimit) {
0N/A if (C->do_escape_analysis() == true && !C->failing()) {
0N/A // Retry compilation without escape analysis.
0N/A // If this is the first failure, the sentinel string will "stick"
0N/A // to the Compile object, and the C2Compiler will see it and retry.
0N/A C->record_failure(C2Compiler::retry_no_escape_analysis());
65N/A }
0N/A return NULL;
0N/A }
0N/A orig_phi_worklist.append_if_missing(orig_phi);
0N/A const TypePtr *atype = C->get_adr_type(alias_idx);
0N/A result = PhiNode::make(orig_phi->in(0), NULL, Type::MEMORY, atype);
0N/A set_map_phi(orig_phi->_idx, result);
0N/A igvn->set_type(result, result->bottom_type());
0N/A record_for_optimizer(result);
0N/A new_created = true;
0N/A return result;
0N/A}
0N/A
65N/A//
0N/A// Return a new version of Memory Phi "orig_phi" with the inputs having the
65N/A// specified alias index.
0N/A//
0N/APhiNode *ConnectionGraph::split_memory_phi(PhiNode *orig_phi, int alias_idx, GrowableArray<PhiNode *> &orig_phi_worklist, PhaseGVN *igvn) {
0N/A
0N/A assert(alias_idx != Compile::AliasIdxBot, "can't split out bottom memory");
0N/A Compile *C = _compile;
0N/A bool new_phi_created;
65N/A PhiNode *result = create_split_phi(orig_phi, alias_idx, orig_phi_worklist, igvn, new_phi_created);
0N/A if (!new_phi_created) {
0N/A return result;
0N/A }
65N/A
0N/A GrowableArray<PhiNode *> phi_list;
0N/A GrowableArray<uint> cur_input;
38N/A
38N/A PhiNode *phi = orig_phi;
38N/A uint idx = 1;
0N/A bool finished = false;
0N/A while(!finished) {
0N/A while (idx < phi->req()) {
0N/A Node *mem = find_inst_mem(phi->in(idx), alias_idx, orig_phi_worklist, igvn);
0N/A if (mem != NULL && mem->is_Phi()) {
0N/A PhiNode *newphi = create_split_phi(mem->as_Phi(), alias_idx, orig_phi_worklist, igvn, new_phi_created);
65N/A if (new_phi_created) {
65N/A // found an phi for which we created a new split, push current one on worklist and begin
65N/A // processing new one
0N/A phi_list.push(phi);
65N/A cur_input.push(idx);
65N/A phi = mem->as_Phi();
0N/A result = newphi;
0N/A idx = 1;
0N/A continue;
0N/A } else {
0N/A mem = newphi;
0N/A }
65N/A }
65N/A if (C->failing()) {
65N/A return NULL;
0N/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
65N/A assert( phi->req() == result->req(), "must have same number of inputs.");
65N/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.
65N/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.");
65N/A }
65N/A // we have finished processing a Phi, see if there are any more to do
65N/A finished = (phi_list.length() == 0 );
65N/A if (!finished) {
65N/A phi = phi_list.pop();
65N/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;
65N/A }
65N/A }
65N/A return result;
65N/A}
65N/A
65N/A
65N/A//
65N/A// The next methods are derived from methods in MemNode.
65N/A//
65N/Astatic Node *step_through_mergemem(MergeMemNode *mmem, int alias_idx, const TypeOopPtr *tinst) {
65N/A Node *mem = mmem;
65N/A // TypeInstPtr::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.
65N/A if( tinst->base() != Type::AnyPtr &&
65N/A !(tinst->klass()->is_java_lang_Object() &&
65N/A tinst->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//
65N/A// Search memory chain of "mem" to find a MemNode whose address
65N/A// is the specified alias index.
65N/A//
65N/ANode* ConnectionGraph::find_inst_mem(Node *orig_mem, int alias_idx, GrowableArray<PhiNode *> &orig_phis, PhaseGVN *phase) {
65N/A if (orig_mem == NULL)
65N/A return orig_mem;
65N/A Compile* C = phase->C;
65N/A const TypeOopPtr *tinst = C->get_adr_type(alias_idx)->isa_oopptr();
65N/A bool is_instance = (tinst != NULL) && tinst->is_instance();
65N/A Node *prev = NULL;
65N/A Node *result = orig_mem;
65N/A while (prev != result) {
65N/A prev = result;
65N/A if (result->is_Mem()) {
65N/A MemNode *mem = result->as_Mem();
65N/A const Type *at = phase->type(mem->in(MemNode::Address));
65N/A if (at != Type::TOP) {
65N/A assert (at->isa_ptr() != NULL, "pointer type required.");
65N/A int idx = C->get_alias_index(at->is_ptr());
65N/A if (idx == alias_idx)
65N/A break;
65N/A }
65N/A result = mem->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);
65N/A if (proj_in->is_Call()) {
65N/A CallNode *call = proj_in->as_Call();
65N/A if (!call->may_modify(tinst, phase)) {
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.
65N/A if (alloc == NULL || alloc->_idx != tinst->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();
65N/A result = step_through_mergemem(mmem, alias_idx, tinst);
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));
65N/A result = find_inst_mem(result, alias_idx, orig_phis, phase);
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() &&
0N/A C->get_alias_index(result->as_Phi()->adr_type()) != alias_idx) {
0N/A Node *un = result->as_Phi()->unique_input(phase);
0N/A if (un != NULL) {
0N/A result = un;
0N/A } else {
0N/A break;
0N/A }
0N/A }
0N/A }
0N/A if (is_instance && result->is_Phi()) {
0N/A PhiNode *mphi = result->as_Phi();
0N/A assert(mphi->bottom_type() == Type::MEMORY, "memory phi required");
0N/A const TypePtr *t = mphi->adr_type();
0N/A if (C->get_alias_index(t) != alias_idx) {
0N/A result = split_memory_phi(mphi, alias_idx, orig_phis, phase);
0N/A }
0N/A }
0N/A // the result is either MemNode, PhiNode, InitializeNode.
0N/A return result;
0N/A}
0N/A
0N/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
0N/A// the approriate 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"
65N/A// 20 AddP _ 19 19 10 Foo+12 alias_index=4
65N/A// 29 CheckCastPP "Foo" iid=24
65N/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
65N/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
65N/A// 90 LoadP _ 120 30 ... alias_index=6
0N/A// 100 LoadP _ 80 20 ... alias_index=4
65N/A//
65N/Avoid ConnectionGraph::split_unique_types(GrowableArray<Node *> &alloc_worklist) {
65N/A GrowableArray<Node *> memnode_worklist;
0N/A GrowableArray<Node *> mergemem_worklist;
39N/A GrowableArray<PhiNode *> orig_phis;
39N/A PhaseGVN *igvn = _compile->initial_gvn();
39N/A uint new_index_start = (uint) _compile->num_alias_types();
39N/A VectorSet visited(Thread::current()->resource_area());
65N/A VectorSet ptset(Thread::current()->resource_area());
65N/A
65N/A
65N/A // Phase 1: Process possible allocations from alloc_worklist.
65N/A // Create instance types for the CheckCastPP for allocations where possible.
65N/A while (alloc_worklist.length() != 0) {
65N/A Node *n = alloc_worklist.pop();
65N/A uint ni = n->_idx;
65N/A const TypeOopPtr* tinst = NULL;
65N/A if (n->is_Call()) {
65N/A CallNode *alloc = n->as_Call();
65N/A // copy escape information to call node
65N/A PointsToNode* ptn = _nodes->adr_at(alloc->_idx);
65N/A PointsToNode::EscapeState es = escape_state(alloc, igvn);
65N/A // We have an allocation or call which returns a Java object,
65N/A // see if it is unescaped.
65N/A if (es != PointsToNode::NoEscape || !ptn->_scalar_replaceable)
65N/A continue;
65N/A if (alloc->is_Allocate()) {
65N/A // Set the scalar_replaceable flag before the next check.
65N/A alloc->as_Allocate()->_is_scalar_replaceable = true;
65N/A }
65N/A // find CheckCastPP of call return value
65N/A n = alloc->result_cast();
65N/A if (n == NULL || // No uses accept Initialize or
65N/A !n->is_CheckCastPP()) // not unique CheckCastPP.
65N/A continue;
65N/A // The inline code for Object.clone() casts the allocation result to
65N/A // java.lang.Object and then to the the actual type of the allocated
65N/A // object. Detect this case and use the second cast.
0N/A if (alloc->is_Allocate() && n->as_Type()->type() == TypeInstPtr::NOTNULL
0N/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++) {
0N/A Node *use = n->fast_out(i);
65N/A if (use->is_CheckCastPP()) {
0N/A cast2 = use;
0N/A break;
0N/A }
0N/A }
65N/A if (cast2 != NULL) {
65N/A n = cast2;
65N/A } else {
65N/A continue;
65N/A }
65N/A }
65N/A set_escape_state(n->_idx, es);
65N/A // in order for an object to be stackallocatable, 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
65N/A set_map(alloc->_idx, n);
65N/A set_map(n->_idx, alloc);
65N/A const TypeOopPtr *t = igvn->type(n)->isa_oopptr();
65N/A if (t == NULL)
65N/A continue; // not a TypeInstPtr
65N/A tinst = t->cast_to_instance(ni);
65N/A igvn->hash_delete(n);
65N/A igvn->set_type(n, tinst);
65N/A n->raise_bottom_type(tinst);
65N/A igvn->hash_insert(n);
0N/A record_for_optimizer(n);
0N/A if (alloc->is_Allocate() && ptn->_scalar_replaceable &&
65N/A (t->isa_instptr() || t->isa_aryptr())) {
0N/A
65N/A // First, put on the worklist all Field edges from Connection Graph
65N/A // which is more accurate then putting immediate users from Ideal Graph.
65N/A for (uint e = 0; e < ptn->edge_count(); e++) {
65N/A Node *use = _nodes->adr_at(ptn->edge_target(e))->_node;
0N/A assert(ptn->edge_type(e) == PointsToNode::FieldEdge && use->is_AddP(),
65N/A "only AddP nodes are Field edges in CG");
65N/A if (use->outcnt() > 0) { // Don't process dead nodes
65N/A Node* addp2 = find_second_addp(use, use->in(AddPNode::Base));
65N/A if (addp2 != NULL) {
0N/A assert(alloc->is_AllocateArray(),"array allocation was expected");
0N/A alloc_worklist.append_if_missing(addp2);
0N/A }
0N/A alloc_worklist.append_if_missing(use);
0N/A }
0N/A }
0N/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);
0N/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);
0N/A if (addp2 != NULL) {
65N/A assert(alloc->is_AllocateArray(),"array allocation was expected");
65N/A alloc_worklist.append_if_missing(addp2);
0N/A }
65N/A alloc_worklist.append_if_missing(use);
65N/A } else if (use->is_Initialize()) {
0N/A memnode_worklist.append_if_missing(use);
65N/A }
0N/A }
0N/A }
0N/A } else if (n->is_AddP()) {
0N/A ptset.Clear();
0N/A PointsTo(ptset, get_addp_base(n), igvn);
0N/A assert(ptset.Size() == 1, "AddP address is unique");
0N/A uint elem = ptset.getelem(); // Allocation node's index
0N/A if (elem == _phantom_object)
0N/A continue; // Assume the value was set outside this method.
65N/A Node *base = get_map(elem); // CheckCastPP node
65N/A split_AddP(n, base, igvn);
65N/A tinst = igvn->type(base)->isa_oopptr();
65N/A } else if (n->is_Phi() ||
65N/A n->is_CheckCastPP() ||
65N/A n->Opcode() == Op_EncodeP ||
65N/A n->Opcode() == Op_DecodeN ||
65N/A (n->is_ConstraintCast() && n->Opcode() == Op_CastPP)) {
65N/A if (visited.test_set(n->_idx)) {
65N/A assert(n->is_Phi(), "loops only through Phi's");
65N/A continue; // already processed
65N/A }
65N/A ptset.Clear();
65N/A PointsTo(ptset, n, igvn);
65N/A if (ptset.Size() == 1) {
65N/A uint elem = ptset.getelem(); // Allocation node's index
65N/A if (elem == _phantom_object)
65N/A continue; // Assume the value was set outside this method.
65N/A Node *val = get_map(elem); // CheckCastPP node
65N/A TypeNode *tn = n->as_Type();
65N/A tinst = igvn->type(val)->isa_oopptr();
65N/A assert(tinst != NULL && tinst->is_instance() &&
65N/A tinst->instance_id() == elem , "instance type expected.");
65N/A
65N/A const TypeOopPtr *tn_t = NULL;
65N/A const Type *tn_type = igvn->type(tn);
65N/A if (tn_type->isa_narrowoop()) {
0N/A tn_t = tn_type->is_narrowoop()->make_oopptr()->isa_oopptr();
0N/A } else {
0N/A tn_t = tn_type->isa_oopptr();
0N/A }
65N/A
0N/A if (tn_t != NULL &&
0N/A tinst->cast_to_instance(TypeOopPtr::UNKNOWN_INSTANCE)->higher_equal(tn_t)) {
0N/A if (tn_type->isa_narrowoop()) {
0N/A tn_type = tinst->make_narrowoop();
0N/A } else {
0N/A tn_type = tinst;
0N/A }
0N/A igvn->hash_delete(tn);
0N/A igvn->set_type(tn, tn_type);
0N/A tn->set_type(tn_type);
65N/A igvn->hash_insert(tn);
65N/A record_for_optimizer(n);
0N/A }
0N/A }
0N/A } else {
0N/A continue;
65N/A }
65N/A // push users on appropriate worklist
65N/A for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
65N/A Node *use = n->fast_out(i);
0N/A if(use->is_Mem() && use->in(MemNode::Address) == n) {
0N/A memnode_worklist.append_if_missing(use);
0N/A } else if (use->is_Initialize()) {
0N/A memnode_worklist.append_if_missing(use);
65N/A } else if (use->is_MergeMem()) {
0N/A mergemem_worklist.append_if_missing(use);
0N/A } else if (use->is_Call() && tinst != NULL) {
0N/A // Look for MergeMem nodes for calls which reference unique allocation
0N/A // (through CheckCastPP nodes) even for debug info.
0N/A Node* m = use->in(TypeFunc::Memory);
65N/A uint iid = tinst->instance_id();
65N/A while (m->is_Proj() && m->in(0)->is_Call() &&
38N/A m->in(0) != use && !m->in(0)->_idx != iid) {
38N/A m = m->in(0)->in(TypeFunc::Memory);
38N/A }
65N/A if (m->is_MergeMem()) {
0N/A mergemem_worklist.append_if_missing(m);
65N/A }
65N/A } else if (use->is_AddP() && use->outcnt() > 0) { // No dead nodes
0N/A Node* addp2 = find_second_addp(use, n);
0N/A if (addp2 != NULL) {
0N/A alloc_worklist.append_if_missing(addp2);
0N/A }
0N/A alloc_worklist.append_if_missing(use);
0N/A } else if (use->is_Phi() ||
0N/A use->is_CheckCastPP() ||
0N/A use->Opcode() == Op_EncodeP ||
0N/A use->Opcode() == Op_DecodeN ||
0N/A (use->is_ConstraintCast() && use->Opcode() == Op_CastPP)) {
0N/A alloc_worklist.append_if_missing(use);
0N/A }
0N/A }
0N/A
0N/A }
0N/A // New alias types were created in split_AddP().
0N/A uint new_index_end = (uint) _compile->num_alias_types();
0N/A
65N/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
65N/A // actually updated until phase 4.)
65N/A if (memnode_worklist.length() == 0)
65N/A return; // nothing to do
0N/A
65N/A while (memnode_worklist.length() != 0) {
0N/A Node *n = memnode_worklist.pop();
0N/A if (visited.test_set(n->_idx))
0N/A continue;
0N/A if (n->is_Phi()) {
65N/A assert(n->as_Phi()->adr_type() != TypePtr::BOTTOM, "narrow memory slice required");
65N/A // we don't need to do anything, but the users must be pushed if we haven't processed
65N/A // this Phi before
0N/A } else if (n->is_Initialize()) {
0N/A // we don't need to do anything, but the users of the memory projection must be pushed
0N/A n = n->as_Initialize()->proj_out(TypeFunc::Memory);
65N/A if (n == NULL)
65N/A continue;
0N/A } else {
0N/A assert(n->is_Mem(), "memory node required.");
65N/A Node *addr = n->in(MemNode::Address);
0N/A assert(addr->is_AddP(), "AddP required");
0N/A const Type *addr_t = igvn->type(addr);
0N/A if (addr_t == Type::TOP)
65N/A continue;
65N/A assert (addr_t->isa_ptr() != NULL, "pointer type required.");
0N/A int alias_idx = _compile->get_alias_index(addr_t->is_ptr());
0N/A assert ((uint)alias_idx < new_index_end, "wrong alias index");
0N/A Node *mem = find_inst_mem(n->in(MemNode::Memory), alias_idx, orig_phis, igvn);
0N/A if (_compile->failing()) {
0N/A return;
0N/A }
0N/A if (mem != n->in(MemNode::Memory)) {
0N/A set_map(n->_idx, mem);
0N/A _nodes->adr_at(n->_idx)->_node = n;
0N/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;
65N/A }
65N/A }
65N/A assert(n->Opcode() == Op_SCMemProj, "memory projection required");
65N/A }
65N/A }
65N/A // push user on appropriate worklist
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_Phi()) {
65N/A memnode_worklist.append_if_missing(use);
65N/A } else if(use->is_Mem() && use->in(MemNode::Memory) == n) {
65N/A memnode_worklist.append_if_missing(use);
65N/A } else if (use->is_Initialize()) {
65N/A memnode_worklist.append_if_missing(use);
65N/A } else if (use->is_MergeMem()) {
65N/A mergemem_worklist.append_if_missing(use);
65N/A }
65N/A }
65N/A }
65N/A
65N/A // Phase 3: Process MergeMem nodes from mergemem_worklist.
65N/A // Walk each memory moving the first node encountered of each
65N/A // instance type to the the input corresponding to its alias index.
65N/A while (mergemem_worklist.length() != 0) {
65N/A Node *n = mergemem_worklist.pop();
65N/A assert(n->is_MergeMem(), "MergeMem node required.");
65N/A if (visited.test_set(n->_idx))
65N/A continue;
65N/A MergeMemNode *nmm = n->as_MergeMem();
65N/A // Note: we don't want to use MergeMemStream here because we only want to
65N/A // scan inputs which exist at the start, not ones we add during processing.
65N/A uint nslices = nmm->req();
65N/A igvn->hash_delete(nmm);
65N/A for (uint i = Compile::AliasIdxRaw+1; i < nslices; i++) {
65N/A Node* mem = nmm->in(i);
65N/A Node* cur = NULL;
65N/A if (mem == NULL || mem->is_top())
65N/A continue;
65N/A while (mem->is_Mem()) {
65N/A const Type *at = igvn->type(mem->in(MemNode::Address));
65N/A if (at != Type::TOP) {
65N/A assert (at->isa_ptr() != NULL, "pointer type required.");
65N/A uint idx = (uint)_compile->get_alias_index(at->is_ptr());
65N/A if (idx == i) {
65N/A if (cur == NULL)
65N/A cur = mem;
65N/A } else {
65N/A if (idx >= nmm->req() || nmm->is_empty_memory(nmm->in(idx))) {
65N/A nmm->set_memory_at(idx, mem);
65N/A }
65N/A }
65N/A }
65N/A mem = mem->in(MemNode::Memory);
65N/A }
65N/A nmm->set_memory_at(i, (cur != NULL) ? cur : mem);
65N/A // Find any instance of the current type if we haven't encountered
65N/A // a value of the instance along the 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)) {
0N/A Node* result = find_inst_mem(mem, ni, orig_phis, igvn);
0N/A if (_compile->failing()) {
0N/A return;
0N/A }
0N/A nmm->set_memory_at(ni, result);
0N/A }
0N/A }
65N/A }
65N/A }
0N/A // Find the rest of instances values
0N/A for (uint ni = new_index_start; ni < new_index_end; ni++) {
0N/A const TypeOopPtr *tinst = igvn->C->get_adr_type(ni)->isa_oopptr();
0N/A Node* result = step_through_mergemem(nmm, ni, tinst);
0N/A if (result == nmm->base_memory()) {
0N/A // Didn't find instance memory, search through general slice recursively.
0N/A result = nmm->memory_at(igvn->C->get_general_index(ni));
0N/A result = find_inst_mem(result, ni, orig_phis, igvn);
0N/A if (_compile->failing()) {
0N/A return;
0N/A }
65N/A nmm->set_memory_at(ni, result);
65N/A }
65N/A }
65N/A igvn->hash_insert(nmm);
0N/A record_for_optimizer(nmm);
0N/A
0N/A // Propagate new memory slices to following MergeMem nodes.
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_Call()) {
0N/A CallNode* in = use->as_Call();
0N/A if (in->proj_out(TypeFunc::Memory) != NULL) {
0N/A Node* m = in->proj_out(TypeFunc::Memory);
0N/A for (DUIterator_Fast jmax, j = m->fast_outs(jmax); j < jmax; j++) {
0N/A Node* mm = m->fast_out(j);
0N/A if (mm->is_MergeMem()) {
0N/A mergemem_worklist.append_if_missing(mm);
65N/A }
0N/A }
0N/A }
0N/A if (use->is_Allocate()) {
0N/A use = use->as_Allocate()->initialization();
0N/A if (use == NULL) {
0N/A continue;
0N/A }
0N/A }
0N/A }
0N/A if (use->is_Initialize()) {
0N/A InitializeNode* in = use->as_Initialize();
65N/A if (in->proj_out(TypeFunc::Memory) != NULL) {
65N/A Node* m = in->proj_out(TypeFunc::Memory);
65N/A for (DUIterator_Fast jmax, j = m->fast_outs(jmax); j < jmax; j++) {
65N/A Node* mm = m->fast_out(j);
65N/A if (mm->is_MergeMem()) {
65N/A mergemem_worklist.append_if_missing(mm);
65N/A }
65N/A }
65N/A }
65N/A }
65N/A }
65N/A }
65N/A
65N/A // Phase 4: Update the inputs of non-instance memory Phis and
65N/A // the Memory input of memnodes
65N/A // First update the inputs of any non-instance Phi's from
65N/A // which we split out an instance Phi. Note we don't have
65N/A // to recursively process Phi's encounted on the input memory
65N/A // chains as is done in split_memory_phi() since they will
65N/A // also be processed here.
65N/A while (orig_phis.length() != 0) {
65N/A PhiNode *phi = orig_phis.pop();
65N/A int alias_idx = _compile->get_alias_index(phi->adr_type());
65N/A igvn->hash_delete(phi);
65N/A for (uint i = 1; i < phi->req(); i++) {
65N/A Node *mem = phi->in(i);
65N/A Node *new_mem = find_inst_mem(mem, alias_idx, orig_phis, igvn);
65N/A if (_compile->failing()) {
65N/A return;
65N/A }
0N/A if (mem != new_mem) {
65N/A phi->set_req(i, new_mem);
65N/A }
65N/A }
65N/A igvn->hash_insert(phi);
65N/A record_for_optimizer(phi);
65N/A }
65N/A
65N/A // Update the memory inputs of MemNodes with the value we computed
65N/A // in Phase 2.
65N/A for (int i = 0; i < _nodes->length(); i++) {
65N/A Node *nmem = get_map(i);
65N/A if (nmem != NULL) {
65N/A Node *n = _nodes->adr_at(i)->_node;
0N/A if (n != NULL && n->is_Mem()) {
65N/A igvn->hash_delete(n);
65N/A n->set_req(MemNode::Memory, nmem);
65N/A igvn->hash_insert(n);
65N/A record_for_optimizer(n);
65N/A }
65N/A }
65N/A }
65N/A}
65N/A
65N/Avoid ConnectionGraph::compute_escape() {
65N/A
65N/A // 1. Populate Connection Graph (CG) with Ideal nodes.
65N/A
65N/A Unique_Node_List worklist_init;
65N/A worklist_init.map(_compile->unique(), NULL); // preallocate space
65N/A
65N/A // Initialize worklist
65N/A if (_compile->root() != NULL) {
65N/A worklist_init.push(_compile->root());
0N/A }
0N/A
0N/A GrowableArray<int> cg_worklist;
65N/A PhaseGVN* igvn = _compile->initial_gvn();
65N/A bool has_allocations = false;
0N/A
0N/A // Push all useful nodes onto CG list and set their type.
0N/A for( uint next = 0; next < worklist_init.size(); ++next ) {
65N/A Node* n = worklist_init.at(next);
65N/A record_for_escape_analysis(n, igvn);
65N/A if (n->is_Call() &&
0N/A _nodes->adr_at(n->_idx)->node_type() == PointsToNode::JavaObject) {
0N/A has_allocations = true;
0N/A }
0N/A if(n->is_AddP())
0N/A cg_worklist.append(n->_idx);
65N/A for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
65N/A Node* m = n->fast_out(i); // Get user
65N/A worklist_init.push(m);
0N/A }
0N/A }
0N/A
0N/A if (has_allocations) {
65N/A _has_allocations = true;
65N/A } else {
0N/A _has_allocations = false;
0N/A _collecting = false;
0N/A return; // Nothing to do.
65N/A }
65N/A
65N/A // 2. First pass to create simple CG edges (doesn't require to walk CG).
65N/A for( uint next = 0; next < _delayed_worklist.size(); ++next ) {
0N/A Node* n = _delayed_worklist.at(next);
0N/A build_connection_graph(n, igvn);
65N/A }
0N/A
65N/A // 3. Pass to create fields edges (Allocate -F-> AddP).
65N/A for( int next = 0; next < cg_worklist.length(); ++next ) {
65N/A int ni = cg_worklist.at(next);
65N/A build_connection_graph(_nodes->adr_at(ni)->_node, igvn);
0N/A }
0N/A
0N/A cg_worklist.clear();
0N/A cg_worklist.append(_phantom_object);
0N/A
0N/A // 4. Build Connection Graph which need
0N/A // to walk the connection graph.
65N/A for (uint ni = 0; ni < (uint)_nodes->length(); ni++) {
0N/A PointsToNode* ptn = _nodes->adr_at(ni);
0N/A Node *n = ptn->_node;
0N/A if (n != NULL) { // Call, AddP, LoadP, StoreP
0N/A build_connection_graph(n, igvn);
0N/A if (ptn->node_type() != PointsToNode::UnknownType)
0N/A cg_worklist.append(n->_idx); // Collect CG nodes
0N/A }
65N/A }
65N/A
65N/A VectorSet ptset(Thread::current()->resource_area());
0N/A GrowableArray<Node*> alloc_worklist;
0N/A GrowableArray<int> worklist;
0N/A GrowableArray<uint> deferred_edges;
0N/A VectorSet visited(Thread::current()->resource_area());
0N/A
0N/A // remove deferred edges from the graph and collect
0N/A // information we will need for type splitting
0N/A for( int next = 0; next < cg_worklist.length(); ++next ) {
65N/A int ni = cg_worklist.at(next);
0N/A PointsToNode* ptn = _nodes->adr_at(ni);
0N/A PointsToNode::NodeType nt = ptn->node_type();
0N/A Node *n = ptn->_node;
0N/A if (nt == PointsToNode::LocalVar || nt == PointsToNode::Field) {
0N/A remove_deferred(ni, &deferred_edges, &visited);
65N/A if (n->is_AddP()) {
65N/A // If this AddP computes an address which may point to more that one
65N/A // object or more then one field (array's element), nothing the address
65N/A // points to can be scalar replaceable.
65N/A Node *base = get_addp_base(n);
65N/A ptset.Clear();
65N/A PointsTo(ptset, base, igvn);
65N/A if (ptset.Size() > 1 ||
65N/A (ptset.Size() != 0 && ptn->offset() == Type::OffsetBot)) {
65N/A for( VectorSetI j(&ptset); j.test(); ++j ) {
65N/A uint pt = j.elem;
65N/A ptnode_adr(pt)->_scalar_replaceable = false;
65N/A }
65N/A }
65N/A }
65N/A } else if (nt == PointsToNode::JavaObject && n->is_Call()) {
65N/A // Push call on alloc_worlist (alocations are calls)
65N/A // for processing by split_unique_types().
65N/A alloc_worklist.append(n);
65N/A }
0N/A }
0N/A
65N/A // push all GlobalEscape nodes on the worklist
0N/A for( int next = 0; next < cg_worklist.length(); ++next ) {
65N/A int nk = cg_worklist.at(next);
65N/A if (_nodes->adr_at(nk)->escape_state() == PointsToNode::GlobalEscape)
65N/A worklist.append(nk);
65N/A }
65N/A // mark all node reachable from GlobalEscape nodes
65N/A while(worklist.length() > 0) {
65N/A PointsToNode n = _nodes->at(worklist.pop());
65N/A for (uint ei = 0; ei < n.edge_count(); ei++) {
65N/A uint npi = n.edge_target(ei);
65N/A PointsToNode *np = ptnode_adr(npi);
0N/A if (np->escape_state() < PointsToNode::GlobalEscape) {
65N/A np->set_escape_state(PointsToNode::GlobalEscape);
65N/A worklist.append_if_missing(npi);
65N/A }
0N/A }
65N/A }
65N/A
65N/A // push all ArgEscape nodes on the worklist
65N/A for( int next = 0; next < cg_worklist.length(); ++next ) {
65N/A int nk = cg_worklist.at(next);
0N/A if (_nodes->adr_at(nk)->escape_state() == PointsToNode::ArgEscape)
65N/A worklist.push(nk);
65N/A }
65N/A // mark all node reachable from ArgEscape nodes
0N/A while(worklist.length() > 0) {
65N/A PointsToNode n = _nodes->at(worklist.pop());
65N/A for (uint ei = 0; ei < n.edge_count(); ei++) {
65N/A uint npi = n.edge_target(ei);
65N/A PointsToNode *np = ptnode_adr(npi);
65N/A if (np->escape_state() < PointsToNode::ArgEscape) {
65N/A np->set_escape_state(PointsToNode::ArgEscape);
65N/A worklist.append_if_missing(npi);
65N/A }
0N/A }
65N/A }
65N/A
0N/A // push all NoEscape nodes on the worklist
0N/A for( int next = 0; next < cg_worklist.length(); ++next ) {
0N/A int nk = cg_worklist.at(next);
0N/A if (_nodes->adr_at(nk)->escape_state() == PointsToNode::NoEscape)
0N/A worklist.push(nk);
0N/A }
65N/A // mark all node reachable from NoEscape nodes
0N/A while(worklist.length() > 0) {
0N/A PointsToNode n = _nodes->at(worklist.pop());
0N/A for (uint ei = 0; ei < n.edge_count(); ei++) {
0N/A uint npi = n.edge_target(ei);
65N/A PointsToNode *np = ptnode_adr(npi);
0N/A if (np->escape_state() < PointsToNode::NoEscape) {
65N/A np->set_escape_state(PointsToNode::NoEscape);
65N/A worklist.append_if_missing(npi);
65N/A }
65N/A }
65N/A }
65N/A
65N/A _collecting = false;
65N/A
65N/A has_allocations = false; // Are there scalar replaceable allocations?
65N/A
65N/A for( int next = 0; next < alloc_worklist.length(); ++next ) {
65N/A Node* n = alloc_worklist.at(next);
65N/A uint ni = n->_idx;
65N/A PointsToNode* ptn = _nodes->adr_at(ni);
65N/A PointsToNode::EscapeState es = ptn->escape_state();
65N/A if (ptn->escape_state() == PointsToNode::NoEscape &&
65N/A ptn->_scalar_replaceable) {
65N/A has_allocations = true;
65N/A break;
65N/A }
65N/A }
65N/A if (!has_allocations) {
65N/A return; // Nothing to do.
65N/A }
65N/A
65N/A if(_compile->AliasLevel() >= 3 && EliminateAllocations) {
65N/A // Now use the escape information to create unique types for
65N/A // unescaped objects
65N/A split_unique_types(alloc_worklist);
65N/A if (_compile->failing()) return;
65N/A
65N/A // Clean up after split unique types.
65N/A ResourceMark rm;
65N/A PhaseRemoveUseless pru(_compile->initial_gvn(), _compile->for_igvn());
65N/A
0N/A#ifdef ASSERT
0N/A } else if (PrintEscapeAnalysis || PrintEliminateAllocations) {
0N/A tty->print("=== No allocations eliminated for ");
0N/A C()->method()->print_short_name();
0N/A if(!EliminateAllocations) {
0N/A tty->print(" since EliminateAllocations is off ===");
65N/A } else if(_compile->AliasLevel() < 3) {
65N/A tty->print(" since AliasLevel < 3 ===");
65N/A }
0N/A tty->cr();
0N/A#endif
65N/A }
0N/A}
0N/A
0N/Avoid ConnectionGraph::process_call_arguments(CallNode *call, PhaseTransform *phase) {
0N/A
0N/A switch (call->Opcode()) {
65N/A#ifdef ASSERT
0N/A case Op_Allocate:
65N/A case Op_AllocateArray:
65N/A case Op_Lock:
65N/A case Op_Unlock:
65N/A assert(false, "should be done already");
65N/A break;
65N/A#endif
65N/A case Op_CallLeafNoFP:
65N/A {
65N/A // Stub calls, objects do not escape but they are not scale replaceable.
65N/A // Adjust escape state for outgoing arguments.
0N/A const TypeTuple * d = call->tf()->domain();
65N/A VectorSet ptset(Thread::current()->resource_area());
65N/A for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
65N/A const Type* at = d->field_at(i);
65N/A Node *arg = call->in(i)->uncast();
65N/A const Type *aat = phase->type(arg);
65N/A if (!arg->is_top() && at->isa_ptr() && aat->isa_ptr()) {
65N/A assert(aat == Type::TOP || aat == TypePtr::NULL_PTR ||
65N/A aat->isa_ptr() != NULL, "expecting an Ptr");
65N/A set_escape_state(arg->_idx, PointsToNode::ArgEscape);
65N/A if (arg->is_AddP()) {
65N/A //
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 //
65N/A // Set AddP's base (Allocate) as not scalar replaceable since
65N/A // pointer to the base (with offset) is passed as argument.
65N/A //
0N/A arg = get_addp_base(arg);
0N/A }
0N/A ptset.Clear();
0N/A PointsTo(ptset, arg, phase);
65N/A for( VectorSetI j(&ptset); j.test(); ++j ) {
65N/A uint pt = j.elem;
0N/A set_escape_state(pt, PointsToNode::ArgEscape);
0N/A }
0N/A }
0N/A }
0N/A break;
65N/A }
65N/A
0N/A case Op_CallStaticJava:
0N/A // For a static call, we know exactly what method is being called.
0N/A // Use bytecode estimator to record the call's escape affects
0N/A {
0N/A ciMethod *meth = call->as_CallJava()->method();
0N/A BCEscapeAnalyzer *call_analyzer = (meth !=NULL) ? meth->get_bcea() : NULL;
0N/A // fall-through if not a Java method or no analyzer information
0N/A if (call_analyzer != NULL) {
65N/A const TypeTuple * d = call->tf()->domain();
65N/A VectorSet ptset(Thread::current()->resource_area());
0N/A bool copy_dependencies = false;
0N/A for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
0N/A const Type* at = d->field_at(i);
0N/A int k = i - TypeFunc::Parms;
0N/A
65N/A if (at->isa_oopptr() != NULL) {
0N/A Node *arg = call->in(i)->uncast();
0N/A
0N/A bool global_escapes = false;
0N/A bool fields_escapes = false;
0N/A if (!call_analyzer->is_arg_stack(k)) {
0N/A // The argument global escapes, mark everything it could point to
0N/A set_escape_state(arg->_idx, PointsToNode::GlobalEscape);
0N/A global_escapes = true;
0N/A } else {
65N/A if (!call_analyzer->is_arg_local(k)) {
0N/A // The argument itself doesn't escape, but any fields might
0N/A fields_escapes = true;
0N/A }
0N/A set_escape_state(arg->_idx, PointsToNode::ArgEscape);
0N/A copy_dependencies = true;
0N/A }
0N/A
0N/A ptset.Clear();
0N/A PointsTo(ptset, arg, phase);
0N/A for( VectorSetI j(&ptset); j.test(); ++j ) {
0N/A uint pt = j.elem;
0N/A if (global_escapes) {
0N/A //The argument global escapes, mark everything it could point to
0N/A set_escape_state(pt, PointsToNode::GlobalEscape);
0N/A } else {
65N/A if (fields_escapes) {
65N/A // The argument itself doesn't escape, but any fields might
0N/A add_edge_from_fields(pt, _phantom_object, Type::OffsetBot);
65N/A }
65N/A set_escape_state(pt, PointsToNode::ArgEscape);
0N/A }
65N/A }
65N/A }
0N/A }
65N/A if (copy_dependencies)
65N/A call_analyzer->copy_dependencies(C()->dependencies());
65N/A break;
0N/A }
0N/A }
0N/A
0N/A default:
0N/A // Fall-through here if not a Java method or no analyzer information
0N/A // or some other type of call, assume the worst case: all arguments
65N/A // globally escape.
65N/A {
65N/A // adjust escape state for outgoing arguments
65N/A const TypeTuple * d = call->tf()->domain();
65N/A VectorSet ptset(Thread::current()->resource_area());
0N/A for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
0N/A const Type* at = d->field_at(i);
65N/A if (at->isa_oopptr() != NULL) {
0N/A Node *arg = call->in(i)->uncast();
0N/A set_escape_state(arg->_idx, PointsToNode::GlobalEscape);
0N/A ptset.Clear();
0N/A PointsTo(ptset, arg, phase);
0N/A for( VectorSetI j(&ptset); j.test(); ++j ) {
0N/A uint pt = j.elem;
0N/A set_escape_state(pt, PointsToNode::GlobalEscape);
65N/A PointsToNode *ptadr = ptnode_adr(pt);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A }
0N/A}
0N/Avoid ConnectionGraph::process_call_result(ProjNode *resproj, PhaseTransform *phase) {
0N/A PointsToNode *ptadr = ptnode_adr(resproj->_idx);
65N/A
65N/A CallNode *call = resproj->in(0)->as_Call();
0N/A switch (call->Opcode()) {
65N/A case Op_Allocate:
0N/A {
65N/A Node *k = call->in(AllocateNode::KlassNode);
0N/A const TypeKlassPtr *kt;
0N/A if (k->Opcode() == Op_LoadKlass) {
0N/A kt = k->as_Load()->type()->isa_klassptr();
0N/A } else {
0N/A kt = k->as_Type()->type()->isa_klassptr();
0N/A }
65N/A assert(kt != NULL, "TypeKlassPtr required.");
0N/A ciKlass* cik = kt->klass();
65N/A ciInstanceKlass* ciik = cik->as_instance_klass();
0N/A
65N/A PointsToNode *ptadr = ptnode_adr(call->_idx);
65N/A PointsToNode::EscapeState es;
65N/A uint edge_to;
65N/A if (cik->is_subclass_of(_compile->env()->Thread_klass()) || ciik->has_finalizer()) {
65N/A es = PointsToNode::GlobalEscape;
65N/A edge_to = _phantom_object; // Could not be worse
65N/A } else {
65N/A es = PointsToNode::NoEscape;
65N/A edge_to = call->_idx;
65N/A }
0N/A set_escape_state(call->_idx, es);
0N/A add_pointsto_edge(resproj->_idx, edge_to);
0N/A _processed.set(resproj->_idx);
0N/A break;
0N/A }
0N/A
65N/A case Op_AllocateArray:
0N/A {
65N/A PointsToNode *ptadr = ptnode_adr(call->_idx);
0N/A int length = call->in(AllocateNode::ALength)->find_int_con(-1);
65N/A if (length < 0 || length > EliminateAllocationArraySizeLimit) {
65N/A // Not scalar replaceable if the length is not constant or too big.
65N/A ptadr->_scalar_replaceable = false;
0N/A }
0N/A set_escape_state(call->_idx, PointsToNode::NoEscape);
0N/A add_pointsto_edge(resproj->_idx, call->_idx);
0N/A _processed.set(resproj->_idx);
0N/A break;
0N/A }
0N/A
65N/A case Op_CallStaticJava:
0N/A // For a static call, we know exactly what method is being called.
0N/A // Use bytecode estimator to record whether the call's return value escapes
0N/A {
0N/A bool done = true;
65N/A const TypeTuple *r = call->tf()->range();
65N/A const Type* ret_type = NULL;
65N/A
65N/A if (r->cnt() > TypeFunc::Parms)
65N/A ret_type = r->field_at(TypeFunc::Parms);
65N/A
65N/A // Note: we use isa_ptr() instead of isa_oopptr() here because the
65N/A // _multianewarray functions return a TypeRawPtr.
0N/A if (ret_type == NULL || ret_type->isa_ptr() == NULL) {
65N/A _processed.set(resproj->_idx);
65N/A break; // doesn't return a pointer type
0N/A }
65N/A ciMethod *meth = call->as_CallJava()->method();
65N/A const TypeTuple * d = call->tf()->domain();
0N/A if (meth == NULL) {
0N/A // not a Java method, assume global escape
0N/A set_escape_state(call->_idx, PointsToNode::GlobalEscape);
0N/A if (resproj != NULL)
0N/A add_pointsto_edge(resproj->_idx, _phantom_object);
0N/A } else {
0N/A BCEscapeAnalyzer *call_analyzer = meth->get_bcea();
0N/A VectorSet ptset(Thread::current()->resource_area());
0N/A bool copy_dependencies = false;
0N/A
0N/A if (call_analyzer->is_return_allocated()) {
0N/A // Returns a newly allocated unescaped object, simply
0N/A // update dependency information.
0N/A // Mark it as NoEscape so that objects referenced by
0N/A // it's fields will be marked as NoEscape at least.
0N/A set_escape_state(call->_idx, PointsToNode::NoEscape);
0N/A if (resproj != NULL)
0N/A add_pointsto_edge(resproj->_idx, call->_idx);
0N/A copy_dependencies = true;
0N/A } else if (call_analyzer->is_return_local() && resproj != NULL) {
65N/A // determine whether any arguments are returned
0N/A set_escape_state(call->_idx, PointsToNode::NoEscape);
0N/A for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
0N/A const Type* at = d->field_at(i);
0N/A
65N/A if (at->isa_oopptr() != NULL) {
65N/A Node *arg = call->in(i)->uncast();
65N/A
65N/A if (call_analyzer->is_arg_returned(i - TypeFunc::Parms)) {
65N/A PointsToNode *arg_esp = _nodes->adr_at(arg->_idx);
65N/A if (arg_esp->node_type() == PointsToNode::UnknownType)
65N/A done = false;
65N/A else if (arg_esp->node_type() == PointsToNode::JavaObject)
65N/A add_pointsto_edge(resproj->_idx, arg->_idx);
65N/A else
65N/A add_deferred_edge(resproj->_idx, arg->_idx);
65N/A arg_esp->_hidden_alias = true;
65N/A }
65N/A }
65N/A }
65N/A copy_dependencies = true;
65N/A } else {
65N/A set_escape_state(call->_idx, PointsToNode::GlobalEscape);
65N/A if (resproj != NULL)
65N/A add_pointsto_edge(resproj->_idx, _phantom_object);
65N/A for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
65N/A const Type* at = d->field_at(i);
65N/A if (at->isa_oopptr() != NULL) {
65N/A Node *arg = call->in(i)->uncast();
65N/A PointsToNode *arg_esp = _nodes->adr_at(arg->_idx);
65N/A arg_esp->_hidden_alias = true;
65N/A }
65N/A }
65N/A }
65N/A if (copy_dependencies)
65N/A call_analyzer->copy_dependencies(C()->dependencies());
65N/A }
65N/A if (done)
65N/A _processed.set(resproj->_idx);
65N/A break;
65N/A }
65N/A
65N/A default:
65N/A // Some other type of call, assume the worst case that the
65N/A // returned value, if any, globally escapes.
65N/A {
65N/A const TypeTuple *r = call->tf()->range();
65N/A if (r->cnt() > TypeFunc::Parms) {
65N/A const Type* ret_type = r->field_at(TypeFunc::Parms);
65N/A
65N/A // Note: we use isa_ptr() instead of isa_oopptr() here because the
65N/A // _multianewarray functions return a TypeRawPtr.
65N/A if (ret_type->isa_ptr() != NULL) {
65N/A PointsToNode *ptadr = ptnode_adr(call->_idx);
65N/A set_escape_state(call->_idx, PointsToNode::GlobalEscape);
65N/A if (resproj != NULL)
65N/A add_pointsto_edge(resproj->_idx, _phantom_object);
65N/A }
65N/A }
65N/A _processed.set(resproj->_idx);
65N/A }
65N/A }
65N/A}
65N/A
65N/A// Populate Connection Graph with Ideal nodes and create simple
65N/A// connection graph edges (do not need to check the node_type of inputs
65N/A// or to call PointsTo() to walk the connection graph).
65N/Avoid ConnectionGraph::record_for_escape_analysis(Node *n, PhaseTransform *phase) {
65N/A if (_processed.test(n->_idx))
65N/A return; // No need to redefine node's state.
65N/A
65N/A if (n->is_Call()) {
65N/A // Arguments to allocation and locking don't escape.
65N/A if (n->is_Allocate()) {
65N/A add_node(n, PointsToNode::JavaObject, PointsToNode::UnknownEscape, true);
65N/A record_for_optimizer(n);
65N/A } else if (n->is_Lock() || n->is_Unlock()) {
65N/A // Put Lock and Unlock nodes on IGVN worklist to process them during
65N/A // the first IGVN optimization when escape information is still available.
0N/A record_for_optimizer(n);
65N/A _processed.set(n->_idx);
65N/A } else {
65N/A // Have to process call's arguments first.
65N/A PointsToNode::NodeType nt = PointsToNode::UnknownType;
65N/A
65N/A // Check if a call returns an object.
65N/A const TypeTuple *r = n->as_Call()->tf()->range();
65N/A if (r->cnt() > TypeFunc::Parms &&
65N/A n->as_Call()->proj_out(TypeFunc::Parms) != NULL) {
65N/A // Note: use isa_ptr() instead of isa_oopptr() here because
65N/A // the _multianewarray functions return a TypeRawPtr.
65N/A if (r->field_at(TypeFunc::Parms)->isa_ptr() != NULL) {
65N/A nt = PointsToNode::JavaObject;
65N/A }
65N/A }
65N/A add_node(n, nt, PointsToNode::UnknownEscape, false);
65N/A }
65N/A return;
65N/A }
65N/A
65N/A // Using isa_ptr() instead of isa_oopptr() for LoadP and Phi because
65N/A // ThreadLocal has RawPrt type.
65N/A switch (n->Opcode()) {
65N/A case Op_AddP:
65N/A {
65N/A add_node(n, PointsToNode::Field, PointsToNode::UnknownEscape, false);
65N/A break;
65N/A }
65N/A case Op_CastX2P:
65N/A { // "Unsafe" memory access.
65N/A add_node(n, PointsToNode::JavaObject, PointsToNode::GlobalEscape, true);
65N/A break;
65N/A }
65N/A case Op_CastPP:
65N/A case Op_CheckCastPP:
65N/A case Op_EncodeP:
65N/A case Op_DecodeN:
65N/A {
65N/A add_node(n, PointsToNode::LocalVar, PointsToNode::UnknownEscape, false);
65N/A int ti = n->in(1)->_idx;
65N/A PointsToNode::NodeType nt = _nodes->adr_at(ti)->node_type();
65N/A if (nt == PointsToNode::UnknownType) {
65N/A _delayed_worklist.push(n); // Process it later.
65N/A break;
65N/A } else if (nt == PointsToNode::JavaObject) {
65N/A add_pointsto_edge(n->_idx, ti);
65N/A } else {
65N/A add_deferred_edge(n->_idx, ti);
65N/A }
65N/A _processed.set(n->_idx);
65N/A break;
65N/A }
65N/A case Op_ConP:
65N/A {
65N/A // assume all pointer constants globally escape except for null
65N/A PointsToNode::EscapeState es;
65N/A if (phase->type(n) == TypePtr::NULL_PTR)
65N/A es = PointsToNode::NoEscape;
65N/A else
65N/A es = PointsToNode::GlobalEscape;
65N/A
65N/A add_node(n, PointsToNode::JavaObject, es, true);
65N/A break;
65N/A }
65N/A case Op_ConN:
65N/A {
65N/A // assume all narrow oop constants globally escape except for null
65N/A PointsToNode::EscapeState es;
65N/A if (phase->type(n) == TypeNarrowOop::NULL_PTR)
65N/A es = PointsToNode::NoEscape;
65N/A else
65N/A es = PointsToNode::GlobalEscape;
65N/A
65N/A add_node(n, PointsToNode::JavaObject, es, true);
65N/A break;
65N/A }
65N/A case Op_CreateEx:
65N/A {
65N/A // assume that all exception objects globally escape
65N/A add_node(n, PointsToNode::JavaObject, PointsToNode::GlobalEscape, true);
65N/A break;
65N/A }
65N/A case Op_LoadKlass:
65N/A {
65N/A add_node(n, PointsToNode::JavaObject, PointsToNode::GlobalEscape, true);
65N/A break;
65N/A }
65N/A case Op_LoadP:
65N/A case Op_LoadN:
65N/A {
65N/A const Type *t = phase->type(n);
65N/A if (!t->isa_narrowoop() && t->isa_ptr() == NULL) {
65N/A _processed.set(n->_idx);
65N/A return;
65N/A }
65N/A add_node(n, PointsToNode::LocalVar, PointsToNode::UnknownEscape, false);
65N/A break;
65N/A }
65N/A case Op_Parm:
65N/A {
65N/A _processed.set(n->_idx); // No need to redefine it state.
65N/A uint con = n->as_Proj()->_con;
65N/A if (con < TypeFunc::Parms)
65N/A return;
65N/A const Type *t = n->in(0)->as_Start()->_domain->field_at(con);
65N/A if (t->isa_ptr() == NULL)
65N/A return;
65N/A // We have to assume all input parameters globally escape
65N/A // (Note: passing 'false' since _processed is already set).
65N/A add_node(n, PointsToNode::JavaObject, PointsToNode::GlobalEscape, false);
65N/A break;
65N/A }
65N/A case Op_Phi:
65N/A {
65N/A if (n->as_Phi()->type()->isa_ptr() == NULL) {
65N/A // nothing to do if not an oop
65N/A _processed.set(n->_idx);
65N/A return;
65N/A }
65N/A add_node(n, PointsToNode::LocalVar, PointsToNode::UnknownEscape, false);
65N/A uint i;
65N/A for (i = 1; i < n->req() ; i++) {
65N/A Node* in = n->in(i);
65N/A if (in == NULL)
65N/A continue; // ignore NULL
65N/A in = in->uncast();
65N/A if (in->is_top() || in == n)
65N/A continue; // ignore top or inputs which go back this node
65N/A int ti = in->_idx;
65N/A PointsToNode::NodeType nt = _nodes->adr_at(ti)->node_type();
65N/A if (nt == PointsToNode::UnknownType) {
65N/A break;
65N/A } else if (nt == PointsToNode::JavaObject) {
65N/A add_pointsto_edge(n->_idx, ti);
65N/A } else {
65N/A add_deferred_edge(n->_idx, ti);
65N/A }
65N/A }
65N/A if (i >= n->req())
65N/A _processed.set(n->_idx);
65N/A else
65N/A _delayed_worklist.push(n);
65N/A break;
65N/A }
65N/A case Op_Proj:
65N/A {
65N/A // we are only interested in the result projection from a call
65N/A if (n->as_Proj()->_con == TypeFunc::Parms && n->in(0)->is_Call() ) {
65N/A add_node(n, PointsToNode::LocalVar, PointsToNode::UnknownEscape, false);
65N/A process_call_result(n->as_Proj(), phase);
65N/A if (!_processed.test(n->_idx)) {
65N/A // The call's result may need to be processed later if the call
65N/A // returns it's argument and the argument is not processed yet.
65N/A _delayed_worklist.push(n);
65N/A }
65N/A } else {
65N/A _processed.set(n->_idx);
65N/A }
0N/A break;
0N/A }
0N/A case Op_Return:
0N/A {
0N/A if( n->req() > TypeFunc::Parms &&
65N/A phase->type(n->in(TypeFunc::Parms))->isa_oopptr() ) {
0N/A // Treat Return value as LocalVar with GlobalEscape escape state.
0N/A add_node(n, PointsToNode::LocalVar, PointsToNode::GlobalEscape, false);
0N/A int ti = n->in(TypeFunc::Parms)->_idx;
65N/A PointsToNode::NodeType nt = _nodes->adr_at(ti)->node_type();
0N/A if (nt == PointsToNode::UnknownType) {
0N/A _delayed_worklist.push(n); // Process it later.
65N/A break;
65N/A } else if (nt == PointsToNode::JavaObject) {
0N/A add_pointsto_edge(n->_idx, ti);
0N/A } else {
0N/A add_deferred_edge(n->_idx, ti);
0N/A }
65N/A }
65N/A _processed.set(n->_idx);
65N/A break;
65N/A }
65N/A case Op_StoreP:
65N/A case Op_StoreN:
65N/A {
65N/A const Type *adr_type = phase->type(n->in(MemNode::Address));
65N/A if (adr_type->isa_narrowoop()) {
65N/A adr_type = adr_type->is_narrowoop()->make_oopptr();
65N/A }
65N/A if (adr_type->isa_oopptr()) {
65N/A add_node(n, PointsToNode::UnknownType, PointsToNode::UnknownEscape, false);
65N/A } else {
65N/A Node* adr = n->in(MemNode::Address);
65N/A if (adr->is_AddP() && phase->type(adr) == TypeRawPtr::NOTNULL &&
65N/A adr->in(AddPNode::Address)->is_Proj() &&
65N/A adr->in(AddPNode::Address)->in(0)->is_Allocate()) {
65N/A add_node(n, PointsToNode::UnknownType, PointsToNode::UnknownEscape, false);
65N/A // We are computing a raw address for a store captured
65N/A // by an Initialize compute an appropriate address type.
65N/A int offs = (int)phase->find_intptr_t_con(adr->in(AddPNode::Offset), Type::OffsetBot);
65N/A assert(offs != Type::OffsetBot, "offset must be a constant");
65N/A } else {
65N/A _processed.set(n->_idx);
65N/A return;
65N/A }
65N/A }
65N/A break;
65N/A }
65N/A case Op_StorePConditional:
65N/A case Op_CompareAndSwapP:
65N/A case Op_CompareAndSwapN:
65N/A {
65N/A const Type *adr_type = phase->type(n->in(MemNode::Address));
65N/A if (adr_type->isa_narrowoop()) {
65N/A adr_type = adr_type->is_narrowoop()->make_oopptr();
65N/A }
65N/A if (adr_type->isa_oopptr()) {
65N/A add_node(n, PointsToNode::UnknownType, PointsToNode::UnknownEscape, false);
65N/A } else {
65N/A _processed.set(n->_idx);
65N/A return;
65N/A }
65N/A break;
65N/A }
65N/A case Op_ThreadLocal:
65N/A {
65N/A add_node(n, PointsToNode::JavaObject, PointsToNode::ArgEscape, true);
65N/A break;
65N/A }
65N/A default:
65N/A ;
65N/A // nothing to do
65N/A }
65N/A return;
65N/A}
65N/A
65N/Avoid ConnectionGraph::build_connection_graph(Node *n, PhaseTransform *phase) {
65N/A // Don't set processed bit for AddP, LoadP, StoreP since
65N/A // they may need more then one pass to process.
0N/A if (_processed.test(n->_idx))
0N/A return; // No need to redefine node's state.
0N/A
0N/A PointsToNode *ptadr = ptnode_adr(n->_idx);
0N/A
65N/A if (n->is_Call()) {
0N/A CallNode *call = n->as_Call();
0N/A process_call_arguments(call, phase);
0N/A _processed.set(n->_idx);
0N/A return;
65N/A }
65N/A
65N/A switch (n->Opcode()) {
65N/A case Op_AddP:
65N/A {
65N/A Node *base = get_addp_base(n);
65N/A // Create a field edge to this node from everything base could point to.
65N/A VectorSet ptset(Thread::current()->resource_area());
65N/A PointsTo(ptset, base, phase);
65N/A for( VectorSetI i(&ptset); i.test(); ++i ) {
65N/A uint pt = i.elem;
65N/A add_field_edge(pt, n->_idx, address_offset(n, phase));
65N/A }
65N/A break;
65N/A }
65N/A case Op_CastX2P:
65N/A {
65N/A assert(false, "Op_CastX2P");
0N/A break;
0N/A }
0N/A case Op_CastPP:
65N/A case Op_CheckCastPP:
0N/A case Op_EncodeP:
65N/A case Op_DecodeN:
65N/A {
65N/A int ti = n->in(1)->_idx;
65N/A if (_nodes->adr_at(ti)->node_type() == PointsToNode::JavaObject) {
65N/A add_pointsto_edge(n->_idx, ti);
65N/A } else {
65N/A add_deferred_edge(n->_idx, ti);
0N/A }
0N/A _processed.set(n->_idx);
65N/A break;
0N/A }
65N/A case Op_ConP:
65N/A {
65N/A assert(false, "Op_ConP");
65N/A break;
0N/A }
65N/A case Op_ConN:
65N/A {
65N/A assert(false, "Op_ConN");
65N/A break;
65N/A }
65N/A case Op_CreateEx:
65N/A {
65N/A assert(false, "Op_CreateEx");
0N/A break;
0N/A }
0N/A case Op_LoadKlass:
0N/A {
0N/A assert(false, "Op_LoadKlass");
0N/A break;
0N/A }
0N/A case Op_LoadP:
65N/A case Op_LoadN:
0N/A {
65N/A const Type *t = phase->type(n);
65N/A#ifdef ASSERT
0N/A if (!t->isa_narrowoop() && t->isa_ptr() == NULL)
65N/A assert(false, "Op_LoadP");
65N/A#endif
65N/A
65N/A Node* adr = n->in(MemNode::Address)->uncast();
65N/A const Type *adr_type = phase->type(adr);
0N/A Node* adr_base;
0N/A if (adr->is_AddP()) {
0N/A adr_base = get_addp_base(adr);
0N/A } else {
65N/A adr_base = adr;
0N/A }
0N/A
0N/A // For everything "adr_base" could point to, create a deferred edge from
65N/A // this node to each field with the same offset.
0N/A VectorSet ptset(Thread::current()->resource_area());
65N/A PointsTo(ptset, adr_base, phase);
0N/A int offset = address_offset(adr, phase);
0N/A for( VectorSetI i(&ptset); i.test(); ++i ) {
0N/A uint pt = i.elem;
0N/A add_deferred_edge_to_fields(n->_idx, pt, offset);
0N/A }
0N/A break;
0N/A }
0N/A case Op_Parm:
0N/A {
0N/A assert(false, "Op_Parm");
0N/A break;
0N/A }
0N/A case Op_Phi:
65N/A {
65N/A#ifdef ASSERT
65N/A if (n->as_Phi()->type()->isa_ptr() == NULL)
65N/A assert(false, "Op_Phi");
65N/A#endif
65N/A for (uint i = 1; i < n->req() ; i++) {
0N/A Node* in = n->in(i);
65N/A if (in == NULL)
65N/A continue; // ignore NULL
65N/A in = in->uncast();
65N/A if (in->is_top() || in == n)
65N/A continue; // ignore top or inputs which go back this node
65N/A int ti = in->_idx;
65N/A if (_nodes->adr_at(in->_idx)->node_type() == PointsToNode::JavaObject) {
65N/A add_pointsto_edge(n->_idx, ti);
65N/A } else {
65N/A add_deferred_edge(n->_idx, ti);
65N/A }
65N/A }
65N/A _processed.set(n->_idx);
65N/A break;
65N/A }
65N/A case Op_Proj:
65N/A {
65N/A // we are only interested in the result projection from a call
65N/A if (n->as_Proj()->_con == TypeFunc::Parms && n->in(0)->is_Call() ) {
0N/A process_call_result(n->as_Proj(), phase);
0N/A assert(_processed.test(n->_idx), "all call results should be processed");
65N/A } else {
65N/A assert(false, "Op_Proj");
65N/A }
65N/A break;
65N/A }
65N/A case Op_Return:
65N/A {
65N/A#ifdef ASSERT
65N/A if( n->req() <= TypeFunc::Parms ||
0N/A !phase->type(n->in(TypeFunc::Parms))->isa_oopptr() ) {
0N/A assert(false, "Op_Return");
0N/A }
0N/A#endif
int ti = n->in(TypeFunc::Parms)->_idx;
if (_nodes->adr_at(ti)->node_type() == PointsToNode::JavaObject) {
add_pointsto_edge(n->_idx, ti);
} else {
add_deferred_edge(n->_idx, ti);
}
_processed.set(n->_idx);
break;
}
case Op_StoreP:
case Op_StoreN:
case Op_StorePConditional:
case Op_CompareAndSwapP:
case Op_CompareAndSwapN:
{
Node *adr = n->in(MemNode::Address);
const Type *adr_type = phase->type(adr);
if (adr_type->isa_narrowoop()) {
adr_type = adr_type->is_narrowoop()->make_oopptr();
}
#ifdef ASSERT
if (!adr_type->isa_oopptr())
assert(phase->type(adr) == TypeRawPtr::NOTNULL, "Op_StoreP");
#endif
assert(adr->is_AddP(), "expecting an AddP");
Node *adr_base = get_addp_base(adr);
Node *val = n->in(MemNode::ValueIn)->uncast();
// For everything "adr_base" could point to, create a deferred edge
// to "val" from each field with the same offset.
VectorSet ptset(Thread::current()->resource_area());
PointsTo(ptset, adr_base, phase);
for( VectorSetI i(&ptset); i.test(); ++i ) {
uint pt = i.elem;
add_edge_from_fields(pt, val->_idx, address_offset(adr, phase));
}
break;
}
case Op_ThreadLocal:
{
assert(false, "Op_ThreadLocal");
break;
}
default:
;
// nothing to do
}
}
#ifndef PRODUCT
void ConnectionGraph::dump() {
PhaseGVN *igvn = _compile->initial_gvn();
bool first = true;
uint size = (uint)_nodes->length();
for (uint ni = 0; ni < size; ni++) {
PointsToNode *ptn = _nodes->adr_at(ni);
PointsToNode::NodeType ptn_type = ptn->node_type();
if (ptn_type != PointsToNode::JavaObject || ptn->_node == NULL)
continue;
PointsToNode::EscapeState es = escape_state(ptn->_node, igvn);
if (ptn->_node->is_Allocate() && (es == PointsToNode::NoEscape || Verbose)) {
if (first) {
tty->cr();
tty->print("======== Connection graph for ");
C()->method()->print_short_name();
tty->cr();
first = false;
}
tty->print("%6d ", ni);
ptn->dump();
// Print all locals which reference this allocation
for (uint li = ni; li < size; li++) {
PointsToNode *ptn_loc = _nodes->adr_at(li);
PointsToNode::NodeType ptn_loc_type = ptn_loc->node_type();
if ( ptn_loc_type == PointsToNode::LocalVar && ptn_loc->_node != NULL &&
ptn_loc->edge_count() == 1 && ptn_loc->edge_target(0) == ni ) {
tty->print("%6d LocalVar [[%d]]", li, ni);
_nodes->adr_at(li)->_node->dump();
}
}
if (Verbose) {
// Print all fields which reference this allocation
for (uint i = 0; i < ptn->edge_count(); i++) {
uint ei = ptn->edge_target(i);
tty->print("%6d Field [[%d]]", ei, ni);
_nodes->adr_at(ei)->_node->dump();
}
}
tty->cr();
}
}
}
#endif