src/share/vm/opto/escape.cpp

Tue, 15 Apr 2008 10:49:32 -0700

author
kvn
date
Tue, 15 Apr 2008 10:49:32 -0700
changeset 520
f3b3fe64f59f
parent 512
36cd3cc4d27b
child 537
f96100ac3d12
permissions
-rw-r--r--

6692301: Side effect in NumberFormat tests with -server -Xcomp
Summary: Optimization in CmpPNode::sub() removed the valid compare instruction because of false positive answer from detect_dominating_control().
Reviewed-by: jrose, sgoldman

     1 /*
     2  * Copyright 2005-2006 Sun Microsystems, Inc.  All Rights Reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  *
    19  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
    21  * have any questions.
    22  *
    23  */
    25 #include "incls/_precompiled.incl"
    26 #include "incls/_escape.cpp.incl"
    28 uint PointsToNode::edge_target(uint e) const {
    29   assert(_edges != NULL && e < (uint)_edges->length(), "valid edge index");
    30   return (_edges->at(e) >> EdgeShift);
    31 }
    33 PointsToNode::EdgeType PointsToNode::edge_type(uint e) const {
    34   assert(_edges != NULL && e < (uint)_edges->length(), "valid edge index");
    35   return (EdgeType) (_edges->at(e) & EdgeMask);
    36 }
    38 void PointsToNode::add_edge(uint targIdx, PointsToNode::EdgeType et) {
    39   uint v = (targIdx << EdgeShift) + ((uint) et);
    40   if (_edges == NULL) {
    41      Arena *a = Compile::current()->comp_arena();
    42     _edges = new(a) GrowableArray<uint>(a, INITIAL_EDGE_COUNT, 0, 0);
    43   }
    44   _edges->append_if_missing(v);
    45 }
    47 void PointsToNode::remove_edge(uint targIdx, PointsToNode::EdgeType et) {
    48   uint v = (targIdx << EdgeShift) + ((uint) et);
    50   _edges->remove(v);
    51 }
    53 #ifndef PRODUCT
    54 static const char *node_type_names[] = {
    55   "UnknownType",
    56   "JavaObject",
    57   "LocalVar",
    58   "Field"
    59 };
    61 static const char *esc_names[] = {
    62   "UnknownEscape",
    63   "NoEscape",
    64   "ArgEscape",
    65   "GlobalEscape"
    66 };
    68 static const char *edge_type_suffix[] = {
    69  "?", // UnknownEdge
    70  "P", // PointsToEdge
    71  "D", // DeferredEdge
    72  "F"  // FieldEdge
    73 };
    75 void PointsToNode::dump() const {
    76   NodeType nt = node_type();
    77   EscapeState es = escape_state();
    78   tty->print("%s %s %s [[", node_type_names[(int) nt], esc_names[(int) es], _scalar_replaceable ? "" : "NSR");
    79   for (uint i = 0; i < edge_count(); i++) {
    80     tty->print(" %d%s", edge_target(i), edge_type_suffix[(int) edge_type(i)]);
    81   }
    82   tty->print("]]  ");
    83   if (_node == NULL)
    84     tty->print_cr("<null>");
    85   else
    86     _node->dump();
    87 }
    88 #endif
    90 ConnectionGraph::ConnectionGraph(Compile * C) : _processed(C->comp_arena()), _node_map(C->comp_arena()) {
    91   _collecting = true;
    92   this->_compile = C;
    93   const PointsToNode &dummy = PointsToNode();
    94   int sz = C->unique();
    95   _nodes = new(C->comp_arena()) GrowableArray<PointsToNode>(C->comp_arena(), sz, sz, dummy);
    96   _phantom_object = C->top()->_idx;
    97   PointsToNode *phn = ptnode_adr(_phantom_object);
    98   phn->_node = C->top();
    99   phn->set_node_type(PointsToNode::JavaObject);
   100   phn->set_escape_state(PointsToNode::GlobalEscape);
   101 }
   103 void ConnectionGraph::add_pointsto_edge(uint from_i, uint to_i) {
   104   PointsToNode *f = ptnode_adr(from_i);
   105   PointsToNode *t = ptnode_adr(to_i);
   107   assert(f->node_type() != PointsToNode::UnknownType && t->node_type() != PointsToNode::UnknownType, "node types must be set");
   108   assert(f->node_type() == PointsToNode::LocalVar || f->node_type() == PointsToNode::Field, "invalid source of PointsTo edge");
   109   assert(t->node_type() == PointsToNode::JavaObject, "invalid destination of PointsTo edge");
   110   f->add_edge(to_i, PointsToNode::PointsToEdge);
   111 }
   113 void ConnectionGraph::add_deferred_edge(uint from_i, uint to_i) {
   114   PointsToNode *f = ptnode_adr(from_i);
   115   PointsToNode *t = ptnode_adr(to_i);
   117   assert(f->node_type() != PointsToNode::UnknownType && t->node_type() != PointsToNode::UnknownType, "node types must be set");
   118   assert(f->node_type() == PointsToNode::LocalVar || f->node_type() == PointsToNode::Field, "invalid source of Deferred edge");
   119   assert(t->node_type() == PointsToNode::LocalVar || t->node_type() == PointsToNode::Field, "invalid destination of Deferred edge");
   120   // don't add a self-referential edge, this can occur during removal of
   121   // deferred edges
   122   if (from_i != to_i)
   123     f->add_edge(to_i, PointsToNode::DeferredEdge);
   124 }
   126 int ConnectionGraph::address_offset(Node* adr, PhaseTransform *phase) {
   127   const Type *adr_type = phase->type(adr);
   128   if (adr->is_AddP() && adr_type->isa_oopptr() == NULL &&
   129       adr->in(AddPNode::Address)->is_Proj() &&
   130       adr->in(AddPNode::Address)->in(0)->is_Allocate()) {
   131     // We are computing a raw address for a store captured by an Initialize
   132     // compute an appropriate address type. AddP cases #3 and #5 (see below).
   133     int offs = (int)phase->find_intptr_t_con(adr->in(AddPNode::Offset), Type::OffsetBot);
   134     assert(offs != Type::OffsetBot ||
   135            adr->in(AddPNode::Address)->in(0)->is_AllocateArray(),
   136            "offset must be a constant or it is initialization of array");
   137     return offs;
   138   }
   139   const TypePtr *t_ptr = adr_type->isa_ptr();
   140   assert(t_ptr != NULL, "must be a pointer type");
   141   return t_ptr->offset();
   142 }
   144 void ConnectionGraph::add_field_edge(uint from_i, uint to_i, int offset) {
   145   PointsToNode *f = ptnode_adr(from_i);
   146   PointsToNode *t = ptnode_adr(to_i);
   148   assert(f->node_type() != PointsToNode::UnknownType && t->node_type() != PointsToNode::UnknownType, "node types must be set");
   149   assert(f->node_type() == PointsToNode::JavaObject, "invalid destination of Field edge");
   150   assert(t->node_type() == PointsToNode::Field, "invalid destination of Field edge");
   151   assert (t->offset() == -1 || t->offset() == offset, "conflicting field offsets");
   152   t->set_offset(offset);
   154   f->add_edge(to_i, PointsToNode::FieldEdge);
   155 }
   157 void ConnectionGraph::set_escape_state(uint ni, PointsToNode::EscapeState es) {
   158   PointsToNode *npt = ptnode_adr(ni);
   159   PointsToNode::EscapeState old_es = npt->escape_state();
   160   if (es > old_es)
   161     npt->set_escape_state(es);
   162 }
   164 void ConnectionGraph::add_node(Node *n, PointsToNode::NodeType nt,
   165                                PointsToNode::EscapeState es, bool done) {
   166   PointsToNode* ptadr = ptnode_adr(n->_idx);
   167   ptadr->_node = n;
   168   ptadr->set_node_type(nt);
   170   // inline set_escape_state(idx, es);
   171   PointsToNode::EscapeState old_es = ptadr->escape_state();
   172   if (es > old_es)
   173     ptadr->set_escape_state(es);
   175   if (done)
   176     _processed.set(n->_idx);
   177 }
   179 PointsToNode::EscapeState ConnectionGraph::escape_state(Node *n, PhaseTransform *phase) {
   180   uint idx = n->_idx;
   181   PointsToNode::EscapeState es;
   183   // If we are still collecting or there were no non-escaping allocations
   184   // we don't know the answer yet
   185   if (_collecting || !_has_allocations)
   186     return PointsToNode::UnknownEscape;
   188   // if the node was created after the escape computation, return
   189   // UnknownEscape
   190   if (idx >= (uint)_nodes->length())
   191     return PointsToNode::UnknownEscape;
   193   es = _nodes->at_grow(idx).escape_state();
   195   // if we have already computed a value, return it
   196   if (es != PointsToNode::UnknownEscape)
   197     return es;
   199   // compute max escape state of anything this node could point to
   200   VectorSet ptset(Thread::current()->resource_area());
   201   PointsTo(ptset, n, phase);
   202   for(VectorSetI i(&ptset); i.test() && es != PointsToNode::GlobalEscape; ++i) {
   203     uint pt = i.elem;
   204     PointsToNode::EscapeState pes = _nodes->adr_at(pt)->escape_state();
   205     if (pes > es)
   206       es = pes;
   207   }
   208   // cache the computed escape state
   209   assert(es != PointsToNode::UnknownEscape, "should have computed an escape state");
   210   _nodes->adr_at(idx)->set_escape_state(es);
   211   return es;
   212 }
   214 void ConnectionGraph::PointsTo(VectorSet &ptset, Node * n, PhaseTransform *phase) {
   215   VectorSet visited(Thread::current()->resource_area());
   216   GrowableArray<uint>  worklist;
   218   n = n->uncast();
   219   PointsToNode  npt = _nodes->at_grow(n->_idx);
   221   // If we have a JavaObject, return just that object
   222   if (npt.node_type() == PointsToNode::JavaObject) {
   223     ptset.set(n->_idx);
   224     return;
   225   }
   226   assert(npt._node != NULL, "unregistered node");
   228   worklist.push(n->_idx);
   229   while(worklist.length() > 0) {
   230     int ni = worklist.pop();
   231     PointsToNode pn = _nodes->at_grow(ni);
   232     if (!visited.test_set(ni)) {
   233       // ensure that all inputs of a Phi have been processed
   234       assert(!_collecting || !pn._node->is_Phi() || _processed.test(ni),"");
   236       int edges_processed = 0;
   237       for (uint e = 0; e < pn.edge_count(); e++) {
   238         uint etgt = pn.edge_target(e);
   239         PointsToNode::EdgeType et = pn.edge_type(e);
   240         if (et == PointsToNode::PointsToEdge) {
   241           ptset.set(etgt);
   242           edges_processed++;
   243         } else if (et == PointsToNode::DeferredEdge) {
   244           worklist.push(etgt);
   245           edges_processed++;
   246         } else {
   247           assert(false,"neither PointsToEdge or DeferredEdge");
   248         }
   249       }
   250       if (edges_processed == 0) {
   251         // no deferred or pointsto edges found.  Assume the value was set
   252         // outside this method.  Add the phantom object to the pointsto set.
   253         ptset.set(_phantom_object);
   254       }
   255     }
   256   }
   257 }
   259 void ConnectionGraph::remove_deferred(uint ni) {
   260   VectorSet visited(Thread::current()->resource_area());
   262   uint i = 0;
   263   PointsToNode *ptn = ptnode_adr(ni);
   265   while(i < ptn->edge_count()) {
   266     uint t = ptn->edge_target(i);
   267     PointsToNode *ptt = ptnode_adr(t);
   268     if (ptn->edge_type(i) != PointsToNode::DeferredEdge) {
   269       i++;
   270     } else {
   271       ptn->remove_edge(t, PointsToNode::DeferredEdge);
   272       if(!visited.test_set(t)) {
   273         for (uint j = 0; j < ptt->edge_count(); j++) {
   274           uint n1 = ptt->edge_target(j);
   275           PointsToNode *pt1 = ptnode_adr(n1);
   276           switch(ptt->edge_type(j)) {
   277             case PointsToNode::PointsToEdge:
   278               add_pointsto_edge(ni, n1);
   279               if(n1 == _phantom_object) {
   280                 // Special case - field set outside (globally escaping).
   281                 ptn->set_escape_state(PointsToNode::GlobalEscape);
   282               }
   283               break;
   284             case PointsToNode::DeferredEdge:
   285               add_deferred_edge(ni, n1);
   286               break;
   287             case PointsToNode::FieldEdge:
   288               assert(false, "invalid connection graph");
   289               break;
   290           }
   291         }
   292       }
   293     }
   294   }
   295 }
   298 //  Add an edge to node given by "to_i" from any field of adr_i whose offset
   299 //  matches "offset"  A deferred edge is added if to_i is a LocalVar, and
   300 //  a pointsto edge is added if it is a JavaObject
   302 void ConnectionGraph::add_edge_from_fields(uint adr_i, uint to_i, int offs) {
   303   PointsToNode an = _nodes->at_grow(adr_i);
   304   PointsToNode to = _nodes->at_grow(to_i);
   305   bool deferred = (to.node_type() == PointsToNode::LocalVar);
   307   for (uint fe = 0; fe < an.edge_count(); fe++) {
   308     assert(an.edge_type(fe) == PointsToNode::FieldEdge, "expecting a field edge");
   309     int fi = an.edge_target(fe);
   310     PointsToNode pf = _nodes->at_grow(fi);
   311     int po = pf.offset();
   312     if (po == offs || po == Type::OffsetBot || offs == Type::OffsetBot) {
   313       if (deferred)
   314         add_deferred_edge(fi, to_i);
   315       else
   316         add_pointsto_edge(fi, to_i);
   317     }
   318   }
   319 }
   321 // Add a deferred  edge from node given by "from_i" to any field of adr_i
   322 // whose offset matches "offset".
   323 void ConnectionGraph::add_deferred_edge_to_fields(uint from_i, uint adr_i, int offs) {
   324   PointsToNode an = _nodes->at_grow(adr_i);
   325   for (uint fe = 0; fe < an.edge_count(); fe++) {
   326     assert(an.edge_type(fe) == PointsToNode::FieldEdge, "expecting a field edge");
   327     int fi = an.edge_target(fe);
   328     PointsToNode pf = _nodes->at_grow(fi);
   329     int po = pf.offset();
   330     if (pf.edge_count() == 0) {
   331       // we have not seen any stores to this field, assume it was set outside this method
   332       add_pointsto_edge(fi, _phantom_object);
   333     }
   334     if (po == offs || po == Type::OffsetBot || offs == Type::OffsetBot) {
   335       add_deferred_edge(from_i, fi);
   336     }
   337   }
   338 }
   340 // Helper functions
   342 static Node* get_addp_base(Node *addp) {
   343   assert(addp->is_AddP(), "must be AddP");
   344   //
   345   // AddP cases for Base and Address inputs:
   346   // case #1. Direct object's field reference:
   347   //     Allocate
   348   //       |
   349   //     Proj #5 ( oop result )
   350   //       |
   351   //     CheckCastPP (cast to instance type)
   352   //      | |
   353   //     AddP  ( base == address )
   354   //
   355   // case #2. Indirect object's field reference:
   356   //      Phi
   357   //       |
   358   //     CastPP (cast to instance type)
   359   //      | |
   360   //     AddP  ( base == address )
   361   //
   362   // case #3. Raw object's field reference for Initialize node:
   363   //      Allocate
   364   //        |
   365   //      Proj #5 ( oop result )
   366   //  top   |
   367   //     \  |
   368   //     AddP  ( base == top )
   369   //
   370   // case #4. Array's element reference:
   371   //   {CheckCastPP | CastPP}
   372   //     |  | |
   373   //     |  AddP ( array's element offset )
   374   //     |  |
   375   //     AddP ( array's offset )
   376   //
   377   // case #5. Raw object's field reference for arraycopy stub call:
   378   //          The inline_native_clone() case when the arraycopy stub is called
   379   //          after the allocation before Initialize and CheckCastPP nodes.
   380   //      Allocate
   381   //        |
   382   //      Proj #5 ( oop result )
   383   //       | |
   384   //       AddP  ( base == address )
   385   //
   386   // case #6. Constant Pool, ThreadLocal, CastX2P or
   387   //          Raw object's field reference:
   388   //      {ConP, ThreadLocal, CastX2P, raw Load}
   389   //  top   |
   390   //     \  |
   391   //     AddP  ( base == top )
   392   //
   393   // case #7. Klass's field reference.
   394   //      LoadKlass
   395   //       | |
   396   //       AddP  ( base == address )
   397   //
   398   Node *base = addp->in(AddPNode::Base)->uncast();
   399   if (base->is_top()) { // The AddP case #3 and #6.
   400     base = addp->in(AddPNode::Address)->uncast();
   401     assert(base->Opcode() == Op_ConP || base->Opcode() == Op_ThreadLocal ||
   402            base->Opcode() == Op_CastX2P ||
   403            (base->is_Mem() && base->bottom_type() == TypeRawPtr::NOTNULL) ||
   404            (base->is_Proj() && base->in(0)->is_Allocate()), "sanity");
   405   }
   406   return base;
   407 }
   409 static Node* find_second_addp(Node* addp, Node* n) {
   410   assert(addp->is_AddP() && addp->outcnt() > 0, "Don't process dead nodes");
   412   Node* addp2 = addp->raw_out(0);
   413   if (addp->outcnt() == 1 && addp2->is_AddP() &&
   414       addp2->in(AddPNode::Base) == n &&
   415       addp2->in(AddPNode::Address) == addp) {
   417     assert(addp->in(AddPNode::Base) == n, "expecting the same base");
   418     //
   419     // Find array's offset to push it on worklist first and
   420     // as result process an array's element offset first (pushed second)
   421     // to avoid CastPP for the array's offset.
   422     // Otherwise the inserted CastPP (LocalVar) will point to what
   423     // the AddP (Field) points to. Which would be wrong since
   424     // the algorithm expects the CastPP has the same point as
   425     // as AddP's base CheckCastPP (LocalVar).
   426     //
   427     //    ArrayAllocation
   428     //     |
   429     //    CheckCastPP
   430     //     |
   431     //    memProj (from ArrayAllocation CheckCastPP)
   432     //     |  ||
   433     //     |  ||   Int (element index)
   434     //     |  ||    |   ConI (log(element size))
   435     //     |  ||    |   /
   436     //     |  ||   LShift
   437     //     |  ||  /
   438     //     |  AddP (array's element offset)
   439     //     |  |
   440     //     |  | ConI (array's offset: #12(32-bits) or #24(64-bits))
   441     //     | / /
   442     //     AddP (array's offset)
   443     //      |
   444     //     Load/Store (memory operation on array's element)
   445     //
   446     return addp2;
   447   }
   448   return NULL;
   449 }
   451 //
   452 // Adjust the type and inputs of an AddP which computes the
   453 // address of a field of an instance
   454 //
   455 void ConnectionGraph::split_AddP(Node *addp, Node *base,  PhaseGVN  *igvn) {
   456   const TypeOopPtr *base_t = igvn->type(base)->isa_oopptr();
   457   assert(base_t != NULL && base_t->is_instance(), "expecting instance oopptr");
   458   const TypeOopPtr *t = igvn->type(addp)->isa_oopptr();
   459   if (t == NULL) {
   460     // We are computing a raw address for a store captured by an Initialize
   461     // compute an appropriate address type.
   462     assert(igvn->type(addp) == TypeRawPtr::NOTNULL, "must be raw pointer");
   463     assert(addp->in(AddPNode::Address)->is_Proj(), "base of raw address must be result projection from allocation");
   464     int offs = (int)igvn->find_intptr_t_con(addp->in(AddPNode::Offset), Type::OffsetBot);
   465     assert(offs != Type::OffsetBot, "offset must be a constant");
   466     t = base_t->add_offset(offs)->is_oopptr();
   467   }
   468   uint inst_id =  base_t->instance_id();
   469   assert(!t->is_instance() || t->instance_id() == inst_id,
   470                              "old type must be non-instance or match new type");
   471   const TypeOopPtr *tinst = base_t->add_offset(t->offset())->is_oopptr();
   472   // Do NOT remove the next call: ensure an new alias index is allocated
   473   // for the instance type
   474   int alias_idx = _compile->get_alias_index(tinst);
   475   igvn->set_type(addp, tinst);
   476   // record the allocation in the node map
   477   set_map(addp->_idx, get_map(base->_idx));
   478   // if the Address input is not the appropriate instance type
   479   // (due to intervening casts,) insert a cast
   480   Node *adr = addp->in(AddPNode::Address);
   481   const TypeOopPtr  *atype = igvn->type(adr)->isa_oopptr();
   482   if (atype != NULL && atype->instance_id() != inst_id) {
   483     assert(!atype->is_instance(), "no conflicting instances");
   484     const TypeOopPtr *new_atype = base_t->add_offset(atype->offset())->isa_oopptr();
   485     Node *acast = new (_compile, 2) CastPPNode(adr, new_atype);
   486     acast->set_req(0, adr->in(0));
   487     igvn->set_type(acast, new_atype);
   488     record_for_optimizer(acast);
   489     Node *bcast = acast;
   490     Node *abase = addp->in(AddPNode::Base);
   491     if (abase != adr) {
   492       bcast = new (_compile, 2) CastPPNode(abase, base_t);
   493       bcast->set_req(0, abase->in(0));
   494       igvn->set_type(bcast, base_t);
   495       record_for_optimizer(bcast);
   496     }
   497     igvn->hash_delete(addp);
   498     addp->set_req(AddPNode::Base, bcast);
   499     addp->set_req(AddPNode::Address, acast);
   500     igvn->hash_insert(addp);
   501   }
   502   // Put on IGVN worklist since at least addp's type was changed above.
   503   record_for_optimizer(addp);
   504 }
   506 //
   507 // Create a new version of orig_phi if necessary. Returns either the newly
   508 // created phi or an existing phi.  Sets create_new to indicate wheter  a new
   509 // phi was created.  Cache the last newly created phi in the node map.
   510 //
   511 PhiNode *ConnectionGraph::create_split_phi(PhiNode *orig_phi, int alias_idx, GrowableArray<PhiNode *>  &orig_phi_worklist, PhaseGVN  *igvn, bool &new_created) {
   512   Compile *C = _compile;
   513   new_created = false;
   514   int phi_alias_idx = C->get_alias_index(orig_phi->adr_type());
   515   // nothing to do if orig_phi is bottom memory or matches alias_idx
   516   if (phi_alias_idx == alias_idx) {
   517     return orig_phi;
   518   }
   519   // have we already created a Phi for this alias index?
   520   PhiNode *result = get_map_phi(orig_phi->_idx);
   521   if (result != NULL && C->get_alias_index(result->adr_type()) == alias_idx) {
   522     return result;
   523   }
   524   if ((int)C->unique() + 2*NodeLimitFudgeFactor > MaxNodeLimit) {
   525     if (C->do_escape_analysis() == true && !C->failing()) {
   526       // Retry compilation without escape analysis.
   527       // If this is the first failure, the sentinel string will "stick"
   528       // to the Compile object, and the C2Compiler will see it and retry.
   529       C->record_failure(C2Compiler::retry_no_escape_analysis());
   530     }
   531     return NULL;
   532   }
   533   orig_phi_worklist.append_if_missing(orig_phi);
   534   const TypePtr *atype = C->get_adr_type(alias_idx);
   535   result = PhiNode::make(orig_phi->in(0), NULL, Type::MEMORY, atype);
   536   set_map_phi(orig_phi->_idx, result);
   537   igvn->set_type(result, result->bottom_type());
   538   record_for_optimizer(result);
   539   new_created = true;
   540   return result;
   541 }
   543 //
   544 // Return a new version  of Memory Phi "orig_phi" with the inputs having the
   545 // specified alias index.
   546 //
   547 PhiNode *ConnectionGraph::split_memory_phi(PhiNode *orig_phi, int alias_idx, GrowableArray<PhiNode *>  &orig_phi_worklist, PhaseGVN  *igvn) {
   549   assert(alias_idx != Compile::AliasIdxBot, "can't split out bottom memory");
   550   Compile *C = _compile;
   551   bool new_phi_created;
   552   PhiNode *result = create_split_phi(orig_phi, alias_idx, orig_phi_worklist, igvn, new_phi_created);
   553   if (!new_phi_created) {
   554     return result;
   555   }
   557   GrowableArray<PhiNode *>  phi_list;
   558   GrowableArray<uint>  cur_input;
   560   PhiNode *phi = orig_phi;
   561   uint idx = 1;
   562   bool finished = false;
   563   while(!finished) {
   564     while (idx < phi->req()) {
   565       Node *mem = find_inst_mem(phi->in(idx), alias_idx, orig_phi_worklist, igvn);
   566       if (mem != NULL && mem->is_Phi()) {
   567         PhiNode *newphi = create_split_phi(mem->as_Phi(), alias_idx, orig_phi_worklist, igvn, new_phi_created);
   568         if (new_phi_created) {
   569           // found an phi for which we created a new split, push current one on worklist and begin
   570           // processing new one
   571           phi_list.push(phi);
   572           cur_input.push(idx);
   573           phi = mem->as_Phi();
   574           result = newphi;
   575           idx = 1;
   576           continue;
   577         } else {
   578           mem = newphi;
   579         }
   580       }
   581       if (C->failing()) {
   582         return NULL;
   583       }
   584       result->set_req(idx++, mem);
   585     }
   586 #ifdef ASSERT
   587     // verify that the new Phi has an input for each input of the original
   588     assert( phi->req() == result->req(), "must have same number of inputs.");
   589     assert( result->in(0) != NULL && result->in(0) == phi->in(0), "regions must match");
   590 #endif
   591     // Check if all new phi's inputs have specified alias index.
   592     // Otherwise use old phi.
   593     for (uint i = 1; i < phi->req(); i++) {
   594       Node* in = result->in(i);
   595       assert((phi->in(i) == NULL) == (in == NULL), "inputs must correspond.");
   596     }
   597     // we have finished processing a Phi, see if there are any more to do
   598     finished = (phi_list.length() == 0 );
   599     if (!finished) {
   600       phi = phi_list.pop();
   601       idx = cur_input.pop();
   602       PhiNode *prev_result = get_map_phi(phi->_idx);
   603       prev_result->set_req(idx++, result);
   604       result = prev_result;
   605     }
   606   }
   607   return result;
   608 }
   611 //
   612 // The next methods are derived from methods in MemNode.
   613 //
   614 static Node *step_through_mergemem(MergeMemNode *mmem, int alias_idx, const TypeOopPtr *tinst) {
   615   Node *mem = mmem;
   616   // TypeInstPtr::NOTNULL+any is an OOP with unknown offset - generally
   617   // means an array I have not precisely typed yet.  Do not do any
   618   // alias stuff with it any time soon.
   619   if( tinst->base() != Type::AnyPtr &&
   620       !(tinst->klass()->is_java_lang_Object() &&
   621         tinst->offset() == Type::OffsetBot) ) {
   622     mem = mmem->memory_at(alias_idx);
   623     // Update input if it is progress over what we have now
   624   }
   625   return mem;
   626 }
   628 //
   629 // Search memory chain of "mem" to find a MemNode whose address
   630 // is the specified alias index.
   631 //
   632 Node* ConnectionGraph::find_inst_mem(Node *orig_mem, int alias_idx, GrowableArray<PhiNode *>  &orig_phis, PhaseGVN *phase) {
   633   if (orig_mem == NULL)
   634     return orig_mem;
   635   Compile* C = phase->C;
   636   const TypeOopPtr *tinst = C->get_adr_type(alias_idx)->isa_oopptr();
   637   bool is_instance = (tinst != NULL) && tinst->is_instance();
   638   Node *prev = NULL;
   639   Node *result = orig_mem;
   640   while (prev != result) {
   641     prev = result;
   642     if (result->is_Mem()) {
   643       MemNode *mem = result->as_Mem();
   644       const Type *at = phase->type(mem->in(MemNode::Address));
   645       if (at != Type::TOP) {
   646         assert (at->isa_ptr() != NULL, "pointer type required.");
   647         int idx = C->get_alias_index(at->is_ptr());
   648         if (idx == alias_idx)
   649           break;
   650       }
   651       result = mem->in(MemNode::Memory);
   652     }
   653     if (!is_instance)
   654       continue;  // don't search further for non-instance types
   655     // skip over a call which does not affect this memory slice
   656     if (result->is_Proj() && result->as_Proj()->_con == TypeFunc::Memory) {
   657       Node *proj_in = result->in(0);
   658       if (proj_in->is_Call()) {
   659         CallNode *call = proj_in->as_Call();
   660         if (!call->may_modify(tinst, phase)) {
   661           result = call->in(TypeFunc::Memory);
   662         }
   663       } else if (proj_in->is_Initialize()) {
   664         AllocateNode* alloc = proj_in->as_Initialize()->allocation();
   665         // Stop if this is the initialization for the object instance which
   666         // which contains this memory slice, otherwise skip over it.
   667         if (alloc == NULL || alloc->_idx != tinst->instance_id()) {
   668           result = proj_in->in(TypeFunc::Memory);
   669         }
   670       } else if (proj_in->is_MemBar()) {
   671         result = proj_in->in(TypeFunc::Memory);
   672       }
   673     } else if (result->is_MergeMem()) {
   674       MergeMemNode *mmem = result->as_MergeMem();
   675       result = step_through_mergemem(mmem, alias_idx, tinst);
   676       if (result == mmem->base_memory()) {
   677         // Didn't find instance memory, search through general slice recursively.
   678         result = mmem->memory_at(C->get_general_index(alias_idx));
   679         result = find_inst_mem(result, alias_idx, orig_phis, phase);
   680         if (C->failing()) {
   681           return NULL;
   682         }
   683         mmem->set_memory_at(alias_idx, result);
   684       }
   685     } else if (result->is_Phi() &&
   686                C->get_alias_index(result->as_Phi()->adr_type()) != alias_idx) {
   687       Node *un = result->as_Phi()->unique_input(phase);
   688       if (un != NULL) {
   689         result = un;
   690       } else {
   691         break;
   692       }
   693     }
   694   }
   695   if (is_instance && result->is_Phi()) {
   696     PhiNode *mphi = result->as_Phi();
   697     assert(mphi->bottom_type() == Type::MEMORY, "memory phi required");
   698     const TypePtr *t = mphi->adr_type();
   699     if (C->get_alias_index(t) != alias_idx) {
   700       result = split_memory_phi(mphi, alias_idx, orig_phis, phase);
   701     }
   702   }
   703   // the result is either MemNode, PhiNode, InitializeNode.
   704   return result;
   705 }
   708 //
   709 //  Convert the types of unescaped object to instance types where possible,
   710 //  propagate the new type information through the graph, and update memory
   711 //  edges and MergeMem inputs to reflect the new type.
   712 //
   713 //  We start with allocations (and calls which may be allocations)  on alloc_worklist.
   714 //  The processing is done in 4 phases:
   715 //
   716 //  Phase 1:  Process possible allocations from alloc_worklist.  Create instance
   717 //            types for the CheckCastPP for allocations where possible.
   718 //            Propagate the the new types through users as follows:
   719 //               casts and Phi:  push users on alloc_worklist
   720 //               AddP:  cast Base and Address inputs to the instance type
   721 //                      push any AddP users on alloc_worklist and push any memnode
   722 //                      users onto memnode_worklist.
   723 //  Phase 2:  Process MemNode's from memnode_worklist. compute new address type and
   724 //            search the Memory chain for a store with the appropriate type
   725 //            address type.  If a Phi is found, create a new version with
   726 //            the approriate memory slices from each of the Phi inputs.
   727 //            For stores, process the users as follows:
   728 //               MemNode:  push on memnode_worklist
   729 //               MergeMem: push on mergemem_worklist
   730 //  Phase 3:  Process MergeMem nodes from mergemem_worklist.  Walk each memory slice
   731 //            moving the first node encountered of each  instance type to the
   732 //            the input corresponding to its alias index.
   733 //            appropriate memory slice.
   734 //  Phase 4:  Update the inputs of non-instance memory Phis and the Memory input of memnodes.
   735 //
   736 // In the following example, the CheckCastPP nodes are the cast of allocation
   737 // results and the allocation of node 29 is unescaped and eligible to be an
   738 // instance type.
   739 //
   740 // We start with:
   741 //
   742 //     7 Parm #memory
   743 //    10  ConI  "12"
   744 //    19  CheckCastPP   "Foo"
   745 //    20  AddP  _ 19 19 10  Foo+12  alias_index=4
   746 //    29  CheckCastPP   "Foo"
   747 //    30  AddP  _ 29 29 10  Foo+12  alias_index=4
   748 //
   749 //    40  StoreP  25   7  20   ... alias_index=4
   750 //    50  StoreP  35  40  30   ... alias_index=4
   751 //    60  StoreP  45  50  20   ... alias_index=4
   752 //    70  LoadP    _  60  30   ... alias_index=4
   753 //    80  Phi     75  50  60   Memory alias_index=4
   754 //    90  LoadP    _  80  30   ... alias_index=4
   755 //   100  LoadP    _  80  20   ... alias_index=4
   756 //
   757 //
   758 // Phase 1 creates an instance type for node 29 assigning it an instance id of 24
   759 // and creating a new alias index for node 30.  This gives:
   760 //
   761 //     7 Parm #memory
   762 //    10  ConI  "12"
   763 //    19  CheckCastPP   "Foo"
   764 //    20  AddP  _ 19 19 10  Foo+12  alias_index=4
   765 //    29  CheckCastPP   "Foo"  iid=24
   766 //    30  AddP  _ 29 29 10  Foo+12  alias_index=6  iid=24
   767 //
   768 //    40  StoreP  25   7  20   ... alias_index=4
   769 //    50  StoreP  35  40  30   ... alias_index=6
   770 //    60  StoreP  45  50  20   ... alias_index=4
   771 //    70  LoadP    _  60  30   ... alias_index=6
   772 //    80  Phi     75  50  60   Memory alias_index=4
   773 //    90  LoadP    _  80  30   ... alias_index=6
   774 //   100  LoadP    _  80  20   ... alias_index=4
   775 //
   776 // In phase 2, new memory inputs are computed for the loads and stores,
   777 // And a new version of the phi is created.  In phase 4, the inputs to
   778 // node 80 are updated and then the memory nodes are updated with the
   779 // values computed in phase 2.  This results in:
   780 //
   781 //     7 Parm #memory
   782 //    10  ConI  "12"
   783 //    19  CheckCastPP   "Foo"
   784 //    20  AddP  _ 19 19 10  Foo+12  alias_index=4
   785 //    29  CheckCastPP   "Foo"  iid=24
   786 //    30  AddP  _ 29 29 10  Foo+12  alias_index=6  iid=24
   787 //
   788 //    40  StoreP  25  7   20   ... alias_index=4
   789 //    50  StoreP  35  7   30   ... alias_index=6
   790 //    60  StoreP  45  40  20   ... alias_index=4
   791 //    70  LoadP    _  50  30   ... alias_index=6
   792 //    80  Phi     75  40  60   Memory alias_index=4
   793 //   120  Phi     75  50  50   Memory alias_index=6
   794 //    90  LoadP    _ 120  30   ... alias_index=6
   795 //   100  LoadP    _  80  20   ... alias_index=4
   796 //
   797 void ConnectionGraph::split_unique_types(GrowableArray<Node *>  &alloc_worklist) {
   798   GrowableArray<Node *>  memnode_worklist;
   799   GrowableArray<Node *>  mergemem_worklist;
   800   GrowableArray<PhiNode *>  orig_phis;
   801   PhaseGVN  *igvn = _compile->initial_gvn();
   802   uint new_index_start = (uint) _compile->num_alias_types();
   803   VectorSet visited(Thread::current()->resource_area());
   804   VectorSet ptset(Thread::current()->resource_area());
   807   //  Phase 1:  Process possible allocations from alloc_worklist.
   808   //  Create instance types for the CheckCastPP for allocations where possible.
   809   while (alloc_worklist.length() != 0) {
   810     Node *n = alloc_worklist.pop();
   811     uint ni = n->_idx;
   812     const TypeOopPtr* tinst = NULL;
   813     if (n->is_Call()) {
   814       CallNode *alloc = n->as_Call();
   815       // copy escape information to call node
   816       PointsToNode* ptn = _nodes->adr_at(alloc->_idx);
   817       PointsToNode::EscapeState es = escape_state(alloc, igvn);
   818       // We have an allocation or call which returns a Java object,
   819       // see if it is unescaped.
   820       if (es != PointsToNode::NoEscape || !ptn->_scalar_replaceable)
   821         continue;
   822       if (alloc->is_Allocate()) {
   823         // Set the scalar_replaceable flag before the next check.
   824         alloc->as_Allocate()->_is_scalar_replaceable = true;
   825       }
   826       // find CheckCastPP of call return value
   827       n = alloc->result_cast();
   828       if (n == NULL ||          // No uses accept Initialize or
   829           !n->is_CheckCastPP()) // not unique CheckCastPP.
   830         continue;
   831       // The inline code for Object.clone() casts the allocation result to
   832       // java.lang.Object and then to the the actual type of the allocated
   833       // object. Detect this case and use the second cast.
   834       if (alloc->is_Allocate() && n->as_Type()->type() == TypeInstPtr::NOTNULL
   835           && igvn->type(alloc->in(AllocateNode::KlassNode)) != TypeKlassPtr::OBJECT) {
   836         Node *cast2 = NULL;
   837         for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
   838           Node *use = n->fast_out(i);
   839           if (use->is_CheckCastPP()) {
   840             cast2 = use;
   841             break;
   842           }
   843         }
   844         if (cast2 != NULL) {
   845           n = cast2;
   846         } else {
   847           continue;
   848         }
   849       }
   850       set_escape_state(n->_idx, es);
   851       // in order for an object to be stackallocatable, it must be:
   852       //   - a direct allocation (not a call returning an object)
   853       //   - non-escaping
   854       //   - eligible to be a unique type
   855       //   - not determined to be ineligible by escape analysis
   856       set_map(alloc->_idx, n);
   857       set_map(n->_idx, alloc);
   858       const TypeOopPtr *t = igvn->type(n)->isa_oopptr();
   859       if (t == NULL)
   860         continue;  // not a TypeInstPtr
   861       tinst = t->cast_to_instance(ni);
   862       igvn->hash_delete(n);
   863       igvn->set_type(n,  tinst);
   864       n->raise_bottom_type(tinst);
   865       igvn->hash_insert(n);
   866       record_for_optimizer(n);
   867       if (alloc->is_Allocate() && ptn->_scalar_replaceable &&
   868           (t->isa_instptr() || t->isa_aryptr())) {
   869         // An allocation may have an Initialize which has raw stores. Scan
   870         // the users of the raw allocation result and push AddP users
   871         // on alloc_worklist.
   872         Node *raw_result = alloc->proj_out(TypeFunc::Parms);
   873         assert (raw_result != NULL, "must have an allocation result");
   874         for (DUIterator_Fast imax, i = raw_result->fast_outs(imax); i < imax; i++) {
   875           Node *use = raw_result->fast_out(i);
   876           if (use->is_AddP() && use->outcnt() > 0) { // Don't process dead nodes
   877             Node* addp2 = find_second_addp(use, raw_result);
   878             if (addp2 != NULL) {
   879               assert(alloc->is_AllocateArray(),"array allocation was expected");
   880               alloc_worklist.append_if_missing(addp2);
   881             }
   882             alloc_worklist.append_if_missing(use);
   883           } else if (use->is_Initialize()) {
   884             memnode_worklist.append_if_missing(use);
   885           }
   886         }
   887       }
   888     } else if (n->is_AddP()) {
   889       ptset.Clear();
   890       PointsTo(ptset, get_addp_base(n), igvn);
   891       assert(ptset.Size() == 1, "AddP address is unique");
   892       uint elem = ptset.getelem(); // Allocation node's index
   893       if (elem == _phantom_object)
   894         continue; // Assume the value was set outside this method.
   895       Node *base = get_map(elem);  // CheckCastPP node
   896       split_AddP(n, base, igvn);
   897       tinst = igvn->type(base)->isa_oopptr();
   898     } else if (n->is_Phi() ||
   899                n->is_CheckCastPP() ||
   900                (n->is_ConstraintCast() && n->Opcode() == Op_CastPP)) {
   901       if (visited.test_set(n->_idx)) {
   902         assert(n->is_Phi(), "loops only through Phi's");
   903         continue;  // already processed
   904       }
   905       ptset.Clear();
   906       PointsTo(ptset, n, igvn);
   907       if (ptset.Size() == 1) {
   908         uint elem = ptset.getelem(); // Allocation node's index
   909         if (elem == _phantom_object)
   910           continue; // Assume the value was set outside this method.
   911         Node *val = get_map(elem);   // CheckCastPP node
   912         TypeNode *tn = n->as_Type();
   913         tinst = igvn->type(val)->isa_oopptr();
   914         assert(tinst != NULL && tinst->is_instance() &&
   915                tinst->instance_id() == elem , "instance type expected.");
   916         const TypeOopPtr *tn_t = igvn->type(tn)->isa_oopptr();
   918         if (tn_t != NULL &&
   919  tinst->cast_to_instance(TypeOopPtr::UNKNOWN_INSTANCE)->higher_equal(tn_t)) {
   920           igvn->hash_delete(tn);
   921           igvn->set_type(tn, tinst);
   922           tn->set_type(tinst);
   923           igvn->hash_insert(tn);
   924           record_for_optimizer(n);
   925         }
   926       }
   927     } else {
   928       continue;
   929     }
   930     // push users on appropriate worklist
   931     for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
   932       Node *use = n->fast_out(i);
   933       if(use->is_Mem() && use->in(MemNode::Address) == n) {
   934         memnode_worklist.append_if_missing(use);
   935       } else if (use->is_Initialize()) {
   936         memnode_worklist.append_if_missing(use);
   937       } else if (use->is_MergeMem()) {
   938         mergemem_worklist.append_if_missing(use);
   939       } else if (use->is_Call() && tinst != NULL) {
   940         // Look for MergeMem nodes for calls which reference unique allocation
   941         // (through CheckCastPP nodes) even for debug info.
   942         Node* m = use->in(TypeFunc::Memory);
   943         uint iid = tinst->instance_id();
   944         while (m->is_Proj() && m->in(0)->is_Call() &&
   945                m->in(0) != use && !m->in(0)->_idx != iid) {
   946           m = m->in(0)->in(TypeFunc::Memory);
   947         }
   948         if (m->is_MergeMem()) {
   949           mergemem_worklist.append_if_missing(m);
   950         }
   951       } else if (use->is_AddP() && use->outcnt() > 0) { // No dead nodes
   952         Node* addp2 = find_second_addp(use, n);
   953         if (addp2 != NULL) {
   954           alloc_worklist.append_if_missing(addp2);
   955         }
   956         alloc_worklist.append_if_missing(use);
   957       } else if (use->is_Phi() ||
   958                  use->is_CheckCastPP() ||
   959                  (use->is_ConstraintCast() && use->Opcode() == Op_CastPP)) {
   960         alloc_worklist.append_if_missing(use);
   961       }
   962     }
   964   }
   965   // New alias types were created in split_AddP().
   966   uint new_index_end = (uint) _compile->num_alias_types();
   968   //  Phase 2:  Process MemNode's from memnode_worklist. compute new address type and
   969   //            compute new values for Memory inputs  (the Memory inputs are not
   970   //            actually updated until phase 4.)
   971   if (memnode_worklist.length() == 0)
   972     return;  // nothing to do
   974   while (memnode_worklist.length() != 0) {
   975     Node *n = memnode_worklist.pop();
   976     if (visited.test_set(n->_idx))
   977       continue;
   978     if (n->is_Phi()) {
   979       assert(n->as_Phi()->adr_type() != TypePtr::BOTTOM, "narrow memory slice required");
   980       // we don't need to do anything, but the users must be pushed if we haven't processed
   981       // this Phi before
   982     } else if (n->is_Initialize()) {
   983       // we don't need to do anything, but the users of the memory projection must be pushed
   984       n = n->as_Initialize()->proj_out(TypeFunc::Memory);
   985       if (n == NULL)
   986         continue;
   987     } else {
   988       assert(n->is_Mem(), "memory node required.");
   989       Node *addr = n->in(MemNode::Address);
   990       assert(addr->is_AddP(), "AddP required");
   991       const Type *addr_t = igvn->type(addr);
   992       if (addr_t == Type::TOP)
   993         continue;
   994       assert (addr_t->isa_ptr() != NULL, "pointer type required.");
   995       int alias_idx = _compile->get_alias_index(addr_t->is_ptr());
   996       assert ((uint)alias_idx < new_index_end, "wrong alias index");
   997       Node *mem = find_inst_mem(n->in(MemNode::Memory), alias_idx, orig_phis, igvn);
   998       if (_compile->failing()) {
   999         return;
  1001       if (mem != n->in(MemNode::Memory)) {
  1002         set_map(n->_idx, mem);
  1003         _nodes->adr_at(n->_idx)->_node = n;
  1005       if (n->is_Load()) {
  1006         continue;  // don't push users
  1007       } else if (n->is_LoadStore()) {
  1008         // get the memory projection
  1009         for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
  1010           Node *use = n->fast_out(i);
  1011           if (use->Opcode() == Op_SCMemProj) {
  1012             n = use;
  1013             break;
  1016         assert(n->Opcode() == Op_SCMemProj, "memory projection required");
  1019     // push user on appropriate worklist
  1020     for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
  1021       Node *use = n->fast_out(i);
  1022       if (use->is_Phi()) {
  1023         memnode_worklist.append_if_missing(use);
  1024       } else if(use->is_Mem() && use->in(MemNode::Memory) == n) {
  1025         memnode_worklist.append_if_missing(use);
  1026       } else if (use->is_Initialize()) {
  1027         memnode_worklist.append_if_missing(use);
  1028       } else if (use->is_MergeMem()) {
  1029         mergemem_worklist.append_if_missing(use);
  1034   //  Phase 3:  Process MergeMem nodes from mergemem_worklist.
  1035   //            Walk each memory moving the first node encountered of each
  1036   //            instance type to the the input corresponding to its alias index.
  1037   while (mergemem_worklist.length() != 0) {
  1038     Node *n = mergemem_worklist.pop();
  1039     assert(n->is_MergeMem(), "MergeMem node required.");
  1040     if (visited.test_set(n->_idx))
  1041       continue;
  1042     MergeMemNode *nmm = n->as_MergeMem();
  1043     // Note: we don't want to use MergeMemStream here because we only want to
  1044     //  scan inputs which exist at the start, not ones we add during processing.
  1045     uint nslices = nmm->req();
  1046     igvn->hash_delete(nmm);
  1047     for (uint i = Compile::AliasIdxRaw+1; i < nslices; i++) {
  1048       Node* mem = nmm->in(i);
  1049       Node* cur = NULL;
  1050       if (mem == NULL || mem->is_top())
  1051         continue;
  1052       while (mem->is_Mem()) {
  1053         const Type *at = igvn->type(mem->in(MemNode::Address));
  1054         if (at != Type::TOP) {
  1055           assert (at->isa_ptr() != NULL, "pointer type required.");
  1056           uint idx = (uint)_compile->get_alias_index(at->is_ptr());
  1057           if (idx == i) {
  1058             if (cur == NULL)
  1059               cur = mem;
  1060           } else {
  1061             if (idx >= nmm->req() || nmm->is_empty_memory(nmm->in(idx))) {
  1062               nmm->set_memory_at(idx, mem);
  1066         mem = mem->in(MemNode::Memory);
  1068       nmm->set_memory_at(i, (cur != NULL) ? cur : mem);
  1069       // Find any instance of the current type if we haven't encountered
  1070       // a value of the instance along the chain.
  1071       for (uint ni = new_index_start; ni < new_index_end; ni++) {
  1072         if((uint)_compile->get_general_index(ni) == i) {
  1073           Node *m = (ni >= nmm->req()) ? nmm->empty_memory() : nmm->in(ni);
  1074           if (nmm->is_empty_memory(m)) {
  1075             Node* result = find_inst_mem(mem, ni, orig_phis, igvn);
  1076             if (_compile->failing()) {
  1077               return;
  1079             nmm->set_memory_at(ni, result);
  1084     // Find the rest of instances values
  1085     for (uint ni = new_index_start; ni < new_index_end; ni++) {
  1086       const TypeOopPtr *tinst = igvn->C->get_adr_type(ni)->isa_oopptr();
  1087       Node* result = step_through_mergemem(nmm, ni, tinst);
  1088       if (result == nmm->base_memory()) {
  1089         // Didn't find instance memory, search through general slice recursively.
  1090         result = nmm->memory_at(igvn->C->get_general_index(ni));
  1091         result = find_inst_mem(result, ni, orig_phis, igvn);
  1092         if (_compile->failing()) {
  1093           return;
  1095         nmm->set_memory_at(ni, result);
  1098     igvn->hash_insert(nmm);
  1099     record_for_optimizer(nmm);
  1101     // Propagate new memory slices to following MergeMem nodes.
  1102     for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
  1103       Node *use = n->fast_out(i);
  1104       if (use->is_Call()) {
  1105         CallNode* in = use->as_Call();
  1106         if (in->proj_out(TypeFunc::Memory) != NULL) {
  1107           Node* m = in->proj_out(TypeFunc::Memory);
  1108           for (DUIterator_Fast jmax, j = m->fast_outs(jmax); j < jmax; j++) {
  1109             Node* mm = m->fast_out(j);
  1110             if (mm->is_MergeMem()) {
  1111               mergemem_worklist.append_if_missing(mm);
  1115         if (use->is_Allocate()) {
  1116           use = use->as_Allocate()->initialization();
  1117           if (use == NULL) {
  1118             continue;
  1122       if (use->is_Initialize()) {
  1123         InitializeNode* in = use->as_Initialize();
  1124         if (in->proj_out(TypeFunc::Memory) != NULL) {
  1125           Node* m = in->proj_out(TypeFunc::Memory);
  1126           for (DUIterator_Fast jmax, j = m->fast_outs(jmax); j < jmax; j++) {
  1127             Node* mm = m->fast_out(j);
  1128             if (mm->is_MergeMem()) {
  1129               mergemem_worklist.append_if_missing(mm);
  1137   //  Phase 4:  Update the inputs of non-instance memory Phis and
  1138   //            the Memory input of memnodes
  1139   // First update the inputs of any non-instance Phi's from
  1140   // which we split out an instance Phi.  Note we don't have
  1141   // to recursively process Phi's encounted on the input memory
  1142   // chains as is done in split_memory_phi() since they  will
  1143   // also be processed here.
  1144   while (orig_phis.length() != 0) {
  1145     PhiNode *phi = orig_phis.pop();
  1146     int alias_idx = _compile->get_alias_index(phi->adr_type());
  1147     igvn->hash_delete(phi);
  1148     for (uint i = 1; i < phi->req(); i++) {
  1149       Node *mem = phi->in(i);
  1150       Node *new_mem = find_inst_mem(mem, alias_idx, orig_phis, igvn);
  1151       if (_compile->failing()) {
  1152         return;
  1154       if (mem != new_mem) {
  1155         phi->set_req(i, new_mem);
  1158     igvn->hash_insert(phi);
  1159     record_for_optimizer(phi);
  1162   // Update the memory inputs of MemNodes with the value we computed
  1163   // in Phase 2.
  1164   for (int i = 0; i < _nodes->length(); i++) {
  1165     Node *nmem = get_map(i);
  1166     if (nmem != NULL) {
  1167       Node *n = _nodes->adr_at(i)->_node;
  1168       if (n != NULL && n->is_Mem()) {
  1169         igvn->hash_delete(n);
  1170         n->set_req(MemNode::Memory, nmem);
  1171         igvn->hash_insert(n);
  1172         record_for_optimizer(n);
  1178 void ConnectionGraph::compute_escape() {
  1180   // 1. Populate Connection Graph with Ideal nodes.
  1182   Unique_Node_List worklist_init;
  1183   worklist_init.map(_compile->unique(), NULL);  // preallocate space
  1185   // Initialize worklist
  1186   if (_compile->root() != NULL) {
  1187     worklist_init.push(_compile->root());
  1190   GrowableArray<int> cg_worklist;
  1191   PhaseGVN* igvn = _compile->initial_gvn();
  1192   bool has_allocations = false;
  1194   // Push all useful nodes onto CG list and set their type.
  1195   for( uint next = 0; next < worklist_init.size(); ++next ) {
  1196     Node* n = worklist_init.at(next);
  1197     record_for_escape_analysis(n, igvn);
  1198     if (n->is_Call() &&
  1199         _nodes->adr_at(n->_idx)->node_type() == PointsToNode::JavaObject) {
  1200       has_allocations = true;
  1202     if(n->is_AddP())
  1203       cg_worklist.append(n->_idx);
  1204     for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
  1205       Node* m = n->fast_out(i);   // Get user
  1206       worklist_init.push(m);
  1210   if (has_allocations) {
  1211     _has_allocations = true;
  1212   } else {
  1213     _has_allocations = false;
  1214     _collecting = false;
  1215     return; // Nothing to do.
  1218   // 2. First pass to create simple CG edges (doesn't require to walk CG).
  1219   for( uint next = 0; next < _delayed_worklist.size(); ++next ) {
  1220     Node* n = _delayed_worklist.at(next);
  1221     build_connection_graph(n, igvn);
  1224   // 3. Pass to create fields edges (Allocate -F-> AddP).
  1225   for( int next = 0; next < cg_worklist.length(); ++next ) {
  1226     int ni = cg_worklist.at(next);
  1227     build_connection_graph(_nodes->adr_at(ni)->_node, igvn);
  1230   cg_worklist.clear();
  1231   cg_worklist.append(_phantom_object);
  1233   // 4. Build Connection Graph which need
  1234   //    to walk the connection graph.
  1235   for (uint ni = 0; ni < (uint)_nodes->length(); ni++) {
  1236     PointsToNode* ptn = _nodes->adr_at(ni);
  1237     Node *n = ptn->_node;
  1238     if (n != NULL) { // Call, AddP, LoadP, StoreP
  1239       build_connection_graph(n, igvn);
  1240       if (ptn->node_type() != PointsToNode::UnknownType)
  1241         cg_worklist.append(n->_idx); // Collect CG nodes
  1245   VectorSet ptset(Thread::current()->resource_area());
  1246   GrowableArray<Node*>  alloc_worklist;
  1247   GrowableArray<int>  worklist;
  1249   // remove deferred edges from the graph and collect
  1250   // information we will need for type splitting
  1251   for( int next = 0; next < cg_worklist.length(); ++next ) {
  1252     int ni = cg_worklist.at(next);
  1253     PointsToNode* ptn = _nodes->adr_at(ni);
  1254     PointsToNode::NodeType nt = ptn->node_type();
  1255     Node *n = ptn->_node;
  1256     if (nt == PointsToNode::LocalVar || nt == PointsToNode::Field) {
  1257       remove_deferred(ni);
  1258       if (n->is_AddP()) {
  1259         // If this AddP computes an address which may point to more that one
  1260         // object, nothing the address points to can be scalar replaceable.
  1261         Node *base = get_addp_base(n);
  1262         ptset.Clear();
  1263         PointsTo(ptset, base, igvn);
  1264         if (ptset.Size() > 1) {
  1265           for( VectorSetI j(&ptset); j.test(); ++j ) {
  1266             uint pt = j.elem;
  1267             ptnode_adr(pt)->_scalar_replaceable = false;
  1271     } else if (nt == PointsToNode::JavaObject && n->is_Call()) {
  1272       // Push call on alloc_worlist (alocations are calls)
  1273       // for processing by split_unique_types().
  1274       alloc_worklist.append(n);
  1278   // push all GlobalEscape nodes on the worklist
  1279   for( int next = 0; next < cg_worklist.length(); ++next ) {
  1280     int nk = cg_worklist.at(next);
  1281     if (_nodes->adr_at(nk)->escape_state() == PointsToNode::GlobalEscape)
  1282       worklist.append(nk);
  1284   // mark all node reachable from GlobalEscape nodes
  1285   while(worklist.length() > 0) {
  1286     PointsToNode n = _nodes->at(worklist.pop());
  1287     for (uint ei = 0; ei < n.edge_count(); ei++) {
  1288       uint npi = n.edge_target(ei);
  1289       PointsToNode *np = ptnode_adr(npi);
  1290       if (np->escape_state() < PointsToNode::GlobalEscape) {
  1291         np->set_escape_state(PointsToNode::GlobalEscape);
  1292         worklist.append_if_missing(npi);
  1297   // push all ArgEscape nodes on the worklist
  1298   for( int next = 0; next < cg_worklist.length(); ++next ) {
  1299     int nk = cg_worklist.at(next);
  1300     if (_nodes->adr_at(nk)->escape_state() == PointsToNode::ArgEscape)
  1301       worklist.push(nk);
  1303   // mark all node reachable from ArgEscape nodes
  1304   while(worklist.length() > 0) {
  1305     PointsToNode n = _nodes->at(worklist.pop());
  1306     for (uint ei = 0; ei < n.edge_count(); ei++) {
  1307       uint npi = n.edge_target(ei);
  1308       PointsToNode *np = ptnode_adr(npi);
  1309       if (np->escape_state() < PointsToNode::ArgEscape) {
  1310         np->set_escape_state(PointsToNode::ArgEscape);
  1311         worklist.append_if_missing(npi);
  1316   // push all NoEscape nodes on the worklist
  1317   for( int next = 0; next < cg_worklist.length(); ++next ) {
  1318     int nk = cg_worklist.at(next);
  1319     if (_nodes->adr_at(nk)->escape_state() == PointsToNode::NoEscape)
  1320       worklist.push(nk);
  1322   // mark all node reachable from NoEscape nodes
  1323   while(worklist.length() > 0) {
  1324     PointsToNode n = _nodes->at(worklist.pop());
  1325     for (uint ei = 0; ei < n.edge_count(); ei++) {
  1326       uint npi = n.edge_target(ei);
  1327       PointsToNode *np = ptnode_adr(npi);
  1328       if (np->escape_state() < PointsToNode::NoEscape) {
  1329         np->set_escape_state(PointsToNode::NoEscape);
  1330         worklist.append_if_missing(npi);
  1335   _collecting = false;
  1337   has_allocations = false; // Are there scalar replaceable allocations?
  1339   for( int next = 0; next < alloc_worklist.length(); ++next ) {
  1340     Node* n = alloc_worklist.at(next);
  1341     uint ni = n->_idx;
  1342     PointsToNode* ptn = _nodes->adr_at(ni);
  1343     PointsToNode::EscapeState es = ptn->escape_state();
  1344     if (ptn->escape_state() == PointsToNode::NoEscape &&
  1345         ptn->_scalar_replaceable) {
  1346       has_allocations = true;
  1347       break;
  1350   if (!has_allocations) {
  1351     return; // Nothing to do.
  1354   if(_compile->AliasLevel() >= 3 && EliminateAllocations) {
  1355     // Now use the escape information to create unique types for
  1356     // unescaped objects
  1357     split_unique_types(alloc_worklist);
  1358     if (_compile->failing())  return;
  1360     // Clean up after split unique types.
  1361     ResourceMark rm;
  1362     PhaseRemoveUseless pru(_compile->initial_gvn(), _compile->for_igvn());
  1364 #ifdef ASSERT
  1365   } else if (PrintEscapeAnalysis || PrintEliminateAllocations) {
  1366     tty->print("=== No allocations eliminated for ");
  1367     C()->method()->print_short_name();
  1368     if(!EliminateAllocations) {
  1369       tty->print(" since EliminateAllocations is off ===");
  1370     } else if(_compile->AliasLevel() < 3) {
  1371       tty->print(" since AliasLevel < 3 ===");
  1373     tty->cr();
  1374 #endif
  1378 void ConnectionGraph::process_call_arguments(CallNode *call, PhaseTransform *phase) {
  1380     switch (call->Opcode()) {
  1381 #ifdef ASSERT
  1382     case Op_Allocate:
  1383     case Op_AllocateArray:
  1384     case Op_Lock:
  1385     case Op_Unlock:
  1386       assert(false, "should be done already");
  1387       break;
  1388 #endif
  1389     case Op_CallLeafNoFP:
  1391       // Stub calls, objects do not escape but they are not scale replaceable.
  1392       // Adjust escape state for outgoing arguments.
  1393       const TypeTuple * d = call->tf()->domain();
  1394       VectorSet ptset(Thread::current()->resource_area());
  1395       for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
  1396         const Type* at = d->field_at(i);
  1397         Node *arg = call->in(i)->uncast();
  1398         const Type *aat = phase->type(arg);
  1399         if (!arg->is_top() && at->isa_ptr() && aat->isa_ptr()) {
  1400           assert(aat == Type::TOP || aat == TypePtr::NULL_PTR ||
  1401                  aat->isa_ptr() != NULL, "expecting an Ptr");
  1402           set_escape_state(arg->_idx, PointsToNode::ArgEscape);
  1403           if (arg->is_AddP()) {
  1404             //
  1405             // The inline_native_clone() case when the arraycopy stub is called
  1406             // after the allocation before Initialize and CheckCastPP nodes.
  1407             //
  1408             // Set AddP's base (Allocate) as not scalar replaceable since
  1409             // pointer to the base (with offset) is passed as argument.
  1410             //
  1411             arg = get_addp_base(arg);
  1413           ptset.Clear();
  1414           PointsTo(ptset, arg, phase);
  1415           for( VectorSetI j(&ptset); j.test(); ++j ) {
  1416             uint pt = j.elem;
  1417             set_escape_state(pt, PointsToNode::ArgEscape);
  1421       break;
  1424     case Op_CallStaticJava:
  1425     // For a static call, we know exactly what method is being called.
  1426     // Use bytecode estimator to record the call's escape affects
  1428       ciMethod *meth = call->as_CallJava()->method();
  1429       BCEscapeAnalyzer *call_analyzer = (meth !=NULL) ? meth->get_bcea() : NULL;
  1430       // fall-through if not a Java method or no analyzer information
  1431       if (call_analyzer != NULL) {
  1432         const TypeTuple * d = call->tf()->domain();
  1433         VectorSet ptset(Thread::current()->resource_area());
  1434         bool copy_dependencies = false;
  1435         for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
  1436           const Type* at = d->field_at(i);
  1437           int k = i - TypeFunc::Parms;
  1439           if (at->isa_oopptr() != NULL) {
  1440             Node *arg = call->in(i)->uncast();
  1442             bool global_escapes = false;
  1443             bool fields_escapes = false;
  1444             if (!call_analyzer->is_arg_stack(k)) {
  1445               // The argument global escapes, mark everything it could point to
  1446               set_escape_state(arg->_idx, PointsToNode::GlobalEscape);
  1447               global_escapes = true;
  1448             } else {
  1449               if (!call_analyzer->is_arg_local(k)) {
  1450                 // The argument itself doesn't escape, but any fields might
  1451                 fields_escapes = true;
  1453               set_escape_state(arg->_idx, PointsToNode::ArgEscape);
  1454               copy_dependencies = true;
  1457             ptset.Clear();
  1458             PointsTo(ptset, arg, phase);
  1459             for( VectorSetI j(&ptset); j.test(); ++j ) {
  1460               uint pt = j.elem;
  1461               if (global_escapes) {
  1462                 //The argument global escapes, mark everything it could point to
  1463                 set_escape_state(pt, PointsToNode::GlobalEscape);
  1464               } else {
  1465                 if (fields_escapes) {
  1466                   // The argument itself doesn't escape, but any fields might
  1467                   add_edge_from_fields(pt, _phantom_object, Type::OffsetBot);
  1469                 set_escape_state(pt, PointsToNode::ArgEscape);
  1474         if (copy_dependencies)
  1475           call_analyzer->copy_dependencies(C()->dependencies());
  1476         break;
  1480     default:
  1481     // Fall-through here if not a Java method or no analyzer information
  1482     // or some other type of call, assume the worst case: all arguments
  1483     // globally escape.
  1485       // adjust escape state for  outgoing arguments
  1486       const TypeTuple * d = call->tf()->domain();
  1487       VectorSet ptset(Thread::current()->resource_area());
  1488       for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
  1489         const Type* at = d->field_at(i);
  1490         if (at->isa_oopptr() != NULL) {
  1491           Node *arg = call->in(i)->uncast();
  1492           set_escape_state(arg->_idx, PointsToNode::GlobalEscape);
  1493           ptset.Clear();
  1494           PointsTo(ptset, arg, phase);
  1495           for( VectorSetI j(&ptset); j.test(); ++j ) {
  1496             uint pt = j.elem;
  1497             set_escape_state(pt, PointsToNode::GlobalEscape);
  1498             PointsToNode *ptadr = ptnode_adr(pt);
  1505 void ConnectionGraph::process_call_result(ProjNode *resproj, PhaseTransform *phase) {
  1506   PointsToNode *ptadr = ptnode_adr(resproj->_idx);
  1508   CallNode *call = resproj->in(0)->as_Call();
  1509   switch (call->Opcode()) {
  1510     case Op_Allocate:
  1512       Node *k = call->in(AllocateNode::KlassNode);
  1513       const TypeKlassPtr *kt;
  1514       if (k->Opcode() == Op_LoadKlass) {
  1515         kt = k->as_Load()->type()->isa_klassptr();
  1516       } else {
  1517         kt = k->as_Type()->type()->isa_klassptr();
  1519       assert(kt != NULL, "TypeKlassPtr  required.");
  1520       ciKlass* cik = kt->klass();
  1521       ciInstanceKlass* ciik = cik->as_instance_klass();
  1523       PointsToNode *ptadr = ptnode_adr(call->_idx);
  1524       PointsToNode::EscapeState es;
  1525       uint edge_to;
  1526       if (cik->is_subclass_of(_compile->env()->Thread_klass()) || ciik->has_finalizer()) {
  1527         es = PointsToNode::GlobalEscape;
  1528         edge_to = _phantom_object; // Could not be worse
  1529       } else {
  1530         es = PointsToNode::NoEscape;
  1531         edge_to = call->_idx;
  1533       set_escape_state(call->_idx, es);
  1534       add_pointsto_edge(resproj->_idx, edge_to);
  1535       _processed.set(resproj->_idx);
  1536       break;
  1539     case Op_AllocateArray:
  1541       PointsToNode *ptadr = ptnode_adr(call->_idx);
  1542       int length = call->in(AllocateNode::ALength)->find_int_con(-1);
  1543       if (length < 0 || length > EliminateAllocationArraySizeLimit) {
  1544         // Not scalar replaceable if the length is not constant or too big.
  1545         ptadr->_scalar_replaceable = false;
  1547       set_escape_state(call->_idx, PointsToNode::NoEscape);
  1548       add_pointsto_edge(resproj->_idx, call->_idx);
  1549       _processed.set(resproj->_idx);
  1550       break;
  1553     case Op_CallStaticJava:
  1554     // For a static call, we know exactly what method is being called.
  1555     // Use bytecode estimator to record whether the call's return value escapes
  1557       bool done = true;
  1558       const TypeTuple *r = call->tf()->range();
  1559       const Type* ret_type = NULL;
  1561       if (r->cnt() > TypeFunc::Parms)
  1562         ret_type = r->field_at(TypeFunc::Parms);
  1564       // Note:  we use isa_ptr() instead of isa_oopptr()  here because the
  1565       //        _multianewarray functions return a TypeRawPtr.
  1566       if (ret_type == NULL || ret_type->isa_ptr() == NULL) {
  1567         _processed.set(resproj->_idx);
  1568         break;  // doesn't return a pointer type
  1570       ciMethod *meth = call->as_CallJava()->method();
  1571       const TypeTuple * d = call->tf()->domain();
  1572       if (meth == NULL) {
  1573         // not a Java method, assume global escape
  1574         set_escape_state(call->_idx, PointsToNode::GlobalEscape);
  1575         if (resproj != NULL)
  1576           add_pointsto_edge(resproj->_idx, _phantom_object);
  1577       } else {
  1578         BCEscapeAnalyzer *call_analyzer = meth->get_bcea();
  1579         VectorSet ptset(Thread::current()->resource_area());
  1580         bool copy_dependencies = false;
  1582         if (call_analyzer->is_return_allocated()) {
  1583           // Returns a newly allocated unescaped object, simply
  1584           // update dependency information.
  1585           // Mark it as NoEscape so that objects referenced by
  1586           // it's fields will be marked as NoEscape at least.
  1587           set_escape_state(call->_idx, PointsToNode::NoEscape);
  1588           if (resproj != NULL)
  1589             add_pointsto_edge(resproj->_idx, call->_idx);
  1590           copy_dependencies = true;
  1591         } else if (call_analyzer->is_return_local() && resproj != NULL) {
  1592           // determine whether any arguments are returned
  1593           set_escape_state(call->_idx, PointsToNode::NoEscape);
  1594           for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
  1595             const Type* at = d->field_at(i);
  1597             if (at->isa_oopptr() != NULL) {
  1598               Node *arg = call->in(i)->uncast();
  1600               if (call_analyzer->is_arg_returned(i - TypeFunc::Parms)) {
  1601                 PointsToNode *arg_esp = _nodes->adr_at(arg->_idx);
  1602                 if (arg_esp->node_type() == PointsToNode::UnknownType)
  1603                   done = false;
  1604                 else if (arg_esp->node_type() == PointsToNode::JavaObject)
  1605                   add_pointsto_edge(resproj->_idx, arg->_idx);
  1606                 else
  1607                   add_deferred_edge(resproj->_idx, arg->_idx);
  1608                 arg_esp->_hidden_alias = true;
  1612           copy_dependencies = true;
  1613         } else {
  1614           set_escape_state(call->_idx, PointsToNode::GlobalEscape);
  1615           if (resproj != NULL)
  1616             add_pointsto_edge(resproj->_idx, _phantom_object);
  1617           for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
  1618             const Type* at = d->field_at(i);
  1619             if (at->isa_oopptr() != NULL) {
  1620               Node *arg = call->in(i)->uncast();
  1621               PointsToNode *arg_esp = _nodes->adr_at(arg->_idx);
  1622               arg_esp->_hidden_alias = true;
  1626         if (copy_dependencies)
  1627           call_analyzer->copy_dependencies(C()->dependencies());
  1629       if (done)
  1630         _processed.set(resproj->_idx);
  1631       break;
  1634     default:
  1635     // Some other type of call, assume the worst case that the
  1636     // returned value, if any, globally escapes.
  1638       const TypeTuple *r = call->tf()->range();
  1639       if (r->cnt() > TypeFunc::Parms) {
  1640         const Type* ret_type = r->field_at(TypeFunc::Parms);
  1642         // Note:  we use isa_ptr() instead of isa_oopptr()  here because the
  1643         //        _multianewarray functions return a TypeRawPtr.
  1644         if (ret_type->isa_ptr() != NULL) {
  1645           PointsToNode *ptadr = ptnode_adr(call->_idx);
  1646           set_escape_state(call->_idx, PointsToNode::GlobalEscape);
  1647           if (resproj != NULL)
  1648             add_pointsto_edge(resproj->_idx, _phantom_object);
  1651       _processed.set(resproj->_idx);
  1656 // Populate Connection Graph with Ideal nodes and create simple
  1657 // connection graph edges (do not need to check the node_type of inputs
  1658 // or to call PointsTo() to walk the connection graph).
  1659 void ConnectionGraph::record_for_escape_analysis(Node *n, PhaseTransform *phase) {
  1660   if (_processed.test(n->_idx))
  1661     return; // No need to redefine node's state.
  1663   if (n->is_Call()) {
  1664     // Arguments to allocation and locking don't escape.
  1665     if (n->is_Allocate()) {
  1666       add_node(n, PointsToNode::JavaObject, PointsToNode::UnknownEscape, true);
  1667       record_for_optimizer(n);
  1668     } else if (n->is_Lock() || n->is_Unlock()) {
  1669       // Put Lock and Unlock nodes on IGVN worklist to process them during
  1670       // the first IGVN optimization when escape information is still available.
  1671       record_for_optimizer(n);
  1672       _processed.set(n->_idx);
  1673     } else {
  1674       // Have to process call's arguments first.
  1675       PointsToNode::NodeType nt = PointsToNode::UnknownType;
  1677       // Check if a call returns an object.
  1678       const TypeTuple *r = n->as_Call()->tf()->range();
  1679       if (r->cnt() > TypeFunc::Parms &&
  1680           n->as_Call()->proj_out(TypeFunc::Parms) != NULL) {
  1681         // Note:  use isa_ptr() instead of isa_oopptr() here because
  1682         //        the _multianewarray functions return a TypeRawPtr.
  1683         if (r->field_at(TypeFunc::Parms)->isa_ptr() != NULL) {
  1684           nt = PointsToNode::JavaObject;
  1687       add_node(n, nt, PointsToNode::UnknownEscape, false);
  1689     return;
  1692   // Using isa_ptr() instead of isa_oopptr() for LoadP and Phi because
  1693   // ThreadLocal has RawPrt type.
  1694   switch (n->Opcode()) {
  1695     case Op_AddP:
  1697       add_node(n, PointsToNode::Field, PointsToNode::UnknownEscape, false);
  1698       break;
  1700     case Op_CastX2P:
  1701     { // "Unsafe" memory access.
  1702       add_node(n, PointsToNode::JavaObject, PointsToNode::GlobalEscape, true);
  1703       break;
  1705     case Op_CastPP:
  1706     case Op_CheckCastPP:
  1708       add_node(n, PointsToNode::LocalVar, PointsToNode::UnknownEscape, false);
  1709       int ti = n->in(1)->_idx;
  1710       PointsToNode::NodeType nt = _nodes->adr_at(ti)->node_type();
  1711       if (nt == PointsToNode::UnknownType) {
  1712         _delayed_worklist.push(n); // Process it later.
  1713         break;
  1714       } else if (nt == PointsToNode::JavaObject) {
  1715         add_pointsto_edge(n->_idx, ti);
  1716       } else {
  1717         add_deferred_edge(n->_idx, ti);
  1719       _processed.set(n->_idx);
  1720       break;
  1722     case Op_ConP:
  1724       // assume all pointer constants globally escape except for null
  1725       PointsToNode::EscapeState es;
  1726       if (phase->type(n) == TypePtr::NULL_PTR)
  1727         es = PointsToNode::NoEscape;
  1728       else
  1729         es = PointsToNode::GlobalEscape;
  1731       add_node(n, PointsToNode::JavaObject, es, true);
  1732       break;
  1734     case Op_CreateEx:
  1736       // assume that all exception objects globally escape
  1737       add_node(n, PointsToNode::JavaObject, PointsToNode::GlobalEscape, true);
  1738       break;
  1740     case Op_LoadKlass:
  1742       add_node(n, PointsToNode::JavaObject, PointsToNode::GlobalEscape, true);
  1743       break;
  1745     case Op_LoadP:
  1747       const Type *t = phase->type(n);
  1748       if (t->isa_ptr() == NULL) {
  1749         _processed.set(n->_idx);
  1750         return;
  1752       add_node(n, PointsToNode::LocalVar, PointsToNode::UnknownEscape, false);
  1753       break;
  1755     case Op_Parm:
  1757       _processed.set(n->_idx); // No need to redefine it state.
  1758       uint con = n->as_Proj()->_con;
  1759       if (con < TypeFunc::Parms)
  1760         return;
  1761       const Type *t = n->in(0)->as_Start()->_domain->field_at(con);
  1762       if (t->isa_ptr() == NULL)
  1763         return;
  1764       // We have to assume all input parameters globally escape
  1765       // (Note: passing 'false' since _processed is already set).
  1766       add_node(n, PointsToNode::JavaObject, PointsToNode::GlobalEscape, false);
  1767       break;
  1769     case Op_Phi:
  1771       if (n->as_Phi()->type()->isa_ptr() == NULL) {
  1772         // nothing to do if not an oop
  1773         _processed.set(n->_idx);
  1774         return;
  1776       add_node(n, PointsToNode::LocalVar, PointsToNode::UnknownEscape, false);
  1777       uint i;
  1778       for (i = 1; i < n->req() ; i++) {
  1779         Node* in = n->in(i);
  1780         if (in == NULL)
  1781           continue;  // ignore NULL
  1782         in = in->uncast();
  1783         if (in->is_top() || in == n)
  1784           continue;  // ignore top or inputs which go back this node
  1785         int ti = in->_idx;
  1786         PointsToNode::NodeType nt = _nodes->adr_at(ti)->node_type();
  1787         if (nt == PointsToNode::UnknownType) {
  1788           break;
  1789         } else if (nt == PointsToNode::JavaObject) {
  1790           add_pointsto_edge(n->_idx, ti);
  1791         } else {
  1792           add_deferred_edge(n->_idx, ti);
  1795       if (i >= n->req())
  1796         _processed.set(n->_idx);
  1797       else
  1798         _delayed_worklist.push(n);
  1799       break;
  1801     case Op_Proj:
  1803       // we are only interested in the result projection from a call
  1804       if (n->as_Proj()->_con == TypeFunc::Parms && n->in(0)->is_Call() ) {
  1805         add_node(n, PointsToNode::LocalVar, PointsToNode::UnknownEscape, false);
  1806         process_call_result(n->as_Proj(), phase);
  1807         if (!_processed.test(n->_idx)) {
  1808           // The call's result may need to be processed later if the call
  1809           // returns it's argument and the argument is not processed yet.
  1810           _delayed_worklist.push(n);
  1812       } else {
  1813         _processed.set(n->_idx);
  1815       break;
  1817     case Op_Return:
  1819       if( n->req() > TypeFunc::Parms &&
  1820           phase->type(n->in(TypeFunc::Parms))->isa_oopptr() ) {
  1821         // Treat Return value as LocalVar with GlobalEscape escape state.
  1822         add_node(n, PointsToNode::LocalVar, PointsToNode::GlobalEscape, false);
  1823         int ti = n->in(TypeFunc::Parms)->_idx;
  1824         PointsToNode::NodeType nt = _nodes->adr_at(ti)->node_type();
  1825         if (nt == PointsToNode::UnknownType) {
  1826           _delayed_worklist.push(n); // Process it later.
  1827           break;
  1828         } else if (nt == PointsToNode::JavaObject) {
  1829           add_pointsto_edge(n->_idx, ti);
  1830         } else {
  1831           add_deferred_edge(n->_idx, ti);
  1834       _processed.set(n->_idx);
  1835       break;
  1837     case Op_StoreP:
  1839       const Type *adr_type = phase->type(n->in(MemNode::Address));
  1840       if (adr_type->isa_oopptr()) {
  1841         add_node(n, PointsToNode::UnknownType, PointsToNode::UnknownEscape, false);
  1842       } else {
  1843         Node* adr = n->in(MemNode::Address);
  1844         if (adr->is_AddP() && phase->type(adr) == TypeRawPtr::NOTNULL &&
  1845             adr->in(AddPNode::Address)->is_Proj() &&
  1846             adr->in(AddPNode::Address)->in(0)->is_Allocate()) {
  1847           add_node(n, PointsToNode::UnknownType, PointsToNode::UnknownEscape, false);
  1848           // We are computing a raw address for a store captured
  1849           // by an Initialize compute an appropriate address type.
  1850           int offs = (int)phase->find_intptr_t_con(adr->in(AddPNode::Offset), Type::OffsetBot);
  1851           assert(offs != Type::OffsetBot, "offset must be a constant");
  1852         } else {
  1853           _processed.set(n->_idx);
  1854           return;
  1857       break;
  1859     case Op_StorePConditional:
  1860     case Op_CompareAndSwapP:
  1862       const Type *adr_type = phase->type(n->in(MemNode::Address));
  1863       if (adr_type->isa_oopptr()) {
  1864         add_node(n, PointsToNode::UnknownType, PointsToNode::UnknownEscape, false);
  1865       } else {
  1866         _processed.set(n->_idx);
  1867         return;
  1869       break;
  1871     case Op_ThreadLocal:
  1873       add_node(n, PointsToNode::JavaObject, PointsToNode::ArgEscape, true);
  1874       break;
  1876     default:
  1878       // nothing to do
  1880   return;
  1883 void ConnectionGraph::build_connection_graph(Node *n, PhaseTransform *phase) {
  1884   // Don't set processed bit for AddP, LoadP, StoreP since
  1885   // they may need more then one pass to process.
  1886   if (_processed.test(n->_idx))
  1887     return; // No need to redefine node's state.
  1889   PointsToNode *ptadr = ptnode_adr(n->_idx);
  1891   if (n->is_Call()) {
  1892     CallNode *call = n->as_Call();
  1893     process_call_arguments(call, phase);
  1894     _processed.set(n->_idx);
  1895     return;
  1898   switch (n->Opcode()) {
  1899     case Op_AddP:
  1901       Node *base = get_addp_base(n);
  1902       // Create a field edge to this node from everything base could point to.
  1903       VectorSet ptset(Thread::current()->resource_area());
  1904       PointsTo(ptset, base, phase);
  1905       for( VectorSetI i(&ptset); i.test(); ++i ) {
  1906         uint pt = i.elem;
  1907         add_field_edge(pt, n->_idx, address_offset(n, phase));
  1909       break;
  1911     case Op_CastX2P:
  1913       assert(false, "Op_CastX2P");
  1914       break;
  1916     case Op_CastPP:
  1917     case Op_CheckCastPP:
  1919       int ti = n->in(1)->_idx;
  1920       if (_nodes->adr_at(ti)->node_type() == PointsToNode::JavaObject) {
  1921         add_pointsto_edge(n->_idx, ti);
  1922       } else {
  1923         add_deferred_edge(n->_idx, ti);
  1925       _processed.set(n->_idx);
  1926       break;
  1928     case Op_ConP:
  1930       assert(false, "Op_ConP");
  1931       break;
  1933     case Op_CreateEx:
  1935       assert(false, "Op_CreateEx");
  1936       break;
  1938     case Op_LoadKlass:
  1940       assert(false, "Op_LoadKlass");
  1941       break;
  1943     case Op_LoadP:
  1945       const Type *t = phase->type(n);
  1946 #ifdef ASSERT
  1947       if (t->isa_ptr() == NULL)
  1948         assert(false, "Op_LoadP");
  1949 #endif
  1951       Node* adr = n->in(MemNode::Address)->uncast();
  1952       const Type *adr_type = phase->type(adr);
  1953       Node* adr_base;
  1954       if (adr->is_AddP()) {
  1955         adr_base = get_addp_base(adr);
  1956       } else {
  1957         adr_base = adr;
  1960       // For everything "adr_base" could point to, create a deferred edge from
  1961       // this node to each field with the same offset.
  1962       VectorSet ptset(Thread::current()->resource_area());
  1963       PointsTo(ptset, adr_base, phase);
  1964       int offset = address_offset(adr, phase);
  1965       for( VectorSetI i(&ptset); i.test(); ++i ) {
  1966         uint pt = i.elem;
  1967         add_deferred_edge_to_fields(n->_idx, pt, offset);
  1969       break;
  1971     case Op_Parm:
  1973       assert(false, "Op_Parm");
  1974       break;
  1976     case Op_Phi:
  1978 #ifdef ASSERT
  1979       if (n->as_Phi()->type()->isa_ptr() == NULL)
  1980         assert(false, "Op_Phi");
  1981 #endif
  1982       for (uint i = 1; i < n->req() ; i++) {
  1983         Node* in = n->in(i);
  1984         if (in == NULL)
  1985           continue;  // ignore NULL
  1986         in = in->uncast();
  1987         if (in->is_top() || in == n)
  1988           continue;  // ignore top or inputs which go back this node
  1989         int ti = in->_idx;
  1990         if (_nodes->adr_at(in->_idx)->node_type() == PointsToNode::JavaObject) {
  1991           add_pointsto_edge(n->_idx, ti);
  1992         } else {
  1993           add_deferred_edge(n->_idx, ti);
  1996       _processed.set(n->_idx);
  1997       break;
  1999     case Op_Proj:
  2001       // we are only interested in the result projection from a call
  2002       if (n->as_Proj()->_con == TypeFunc::Parms && n->in(0)->is_Call() ) {
  2003         process_call_result(n->as_Proj(), phase);
  2004         assert(_processed.test(n->_idx), "all call results should be processed");
  2005       } else {
  2006         assert(false, "Op_Proj");
  2008       break;
  2010     case Op_Return:
  2012 #ifdef ASSERT
  2013       if( n->req() <= TypeFunc::Parms ||
  2014           !phase->type(n->in(TypeFunc::Parms))->isa_oopptr() ) {
  2015         assert(false, "Op_Return");
  2017 #endif
  2018       int ti = n->in(TypeFunc::Parms)->_idx;
  2019       if (_nodes->adr_at(ti)->node_type() == PointsToNode::JavaObject) {
  2020         add_pointsto_edge(n->_idx, ti);
  2021       } else {
  2022         add_deferred_edge(n->_idx, ti);
  2024       _processed.set(n->_idx);
  2025       break;
  2027     case Op_StoreP:
  2028     case Op_StorePConditional:
  2029     case Op_CompareAndSwapP:
  2031       Node *adr = n->in(MemNode::Address);
  2032       const Type *adr_type = phase->type(adr);
  2033 #ifdef ASSERT
  2034       if (!adr_type->isa_oopptr())
  2035         assert(phase->type(adr) == TypeRawPtr::NOTNULL, "Op_StoreP");
  2036 #endif
  2038       assert(adr->is_AddP(), "expecting an AddP");
  2039       Node *adr_base = get_addp_base(adr);
  2040       Node *val = n->in(MemNode::ValueIn)->uncast();
  2041       // For everything "adr_base" could point to, create a deferred edge
  2042       // to "val" from each field with the same offset.
  2043       VectorSet ptset(Thread::current()->resource_area());
  2044       PointsTo(ptset, adr_base, phase);
  2045       for( VectorSetI i(&ptset); i.test(); ++i ) {
  2046         uint pt = i.elem;
  2047         add_edge_from_fields(pt, val->_idx, address_offset(adr, phase));
  2049       break;
  2051     case Op_ThreadLocal:
  2053       assert(false, "Op_ThreadLocal");
  2054       break;
  2056     default:
  2058       // nothing to do
  2062 #ifndef PRODUCT
  2063 void ConnectionGraph::dump() {
  2064   PhaseGVN  *igvn = _compile->initial_gvn();
  2065   bool first = true;
  2067   uint size = (uint)_nodes->length();
  2068   for (uint ni = 0; ni < size; ni++) {
  2069     PointsToNode *ptn = _nodes->adr_at(ni);
  2070     PointsToNode::NodeType ptn_type = ptn->node_type();
  2072     if (ptn_type != PointsToNode::JavaObject || ptn->_node == NULL)
  2073       continue;
  2074     PointsToNode::EscapeState es = escape_state(ptn->_node, igvn);
  2075     if (ptn->_node->is_Allocate() && (es == PointsToNode::NoEscape || Verbose)) {
  2076       if (first) {
  2077         tty->cr();
  2078         tty->print("======== Connection graph for ");
  2079         C()->method()->print_short_name();
  2080         tty->cr();
  2081         first = false;
  2083       tty->print("%6d ", ni);
  2084       ptn->dump();
  2085       // Print all locals which reference this allocation
  2086       for (uint li = ni; li < size; li++) {
  2087         PointsToNode *ptn_loc = _nodes->adr_at(li);
  2088         PointsToNode::NodeType ptn_loc_type = ptn_loc->node_type();
  2089         if ( ptn_loc_type == PointsToNode::LocalVar && ptn_loc->_node != NULL &&
  2090              ptn_loc->edge_count() == 1 && ptn_loc->edge_target(0) == ni ) {
  2091           tty->print("%6d  LocalVar [[%d]]", li, ni);
  2092           _nodes->adr_at(li)->_node->dump();
  2095       if (Verbose) {
  2096         // Print all fields which reference this allocation
  2097         for (uint i = 0; i < ptn->edge_count(); i++) {
  2098           uint ei = ptn->edge_target(i);
  2099           tty->print("%6d  Field [[%d]]", ei, ni);
  2100           _nodes->adr_at(ei)->_node->dump();
  2103       tty->cr();
  2107 #endif

mercurial