< prev index next >

src/hotspot/share/opto/escape.cpp

Print this page

  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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "ci/bcEscapeAnalyzer.hpp"
  26 #include "compiler/compileLog.hpp"
  27 #include "gc/shared/barrierSet.hpp"
  28 #include "gc/shared/c2/barrierSetC2.hpp"
  29 #include "libadt/vectset.hpp"
  30 #include "memory/allocation.hpp"

  31 #include "memory/resourceArea.hpp"
  32 #include "opto/c2compiler.hpp"
  33 #include "opto/arraycopynode.hpp"
  34 #include "opto/callnode.hpp"
  35 #include "opto/cfgnode.hpp"
  36 #include "opto/compile.hpp"
  37 #include "opto/escape.hpp"

  38 #include "opto/macro.hpp"
  39 #include "opto/locknode.hpp"
  40 #include "opto/phaseX.hpp"
  41 #include "opto/movenode.hpp"
  42 #include "opto/narrowptrnode.hpp"
  43 #include "opto/castnode.hpp"
  44 #include "opto/rootnode.hpp"
  45 #include "utilities/macros.hpp"
  46 
  47 ConnectionGraph::ConnectionGraph(Compile * C, PhaseIterGVN *igvn, int invocation) :
  48   // If ReduceAllocationMerges is enabled we might call split_through_phi during
  49   // split_unique_types and that will create additional nodes that need to be
  50   // pushed to the ConnectionGraph. The code below bumps the initial capacity of
  51   // _nodes by 10% to account for these additional nodes. If capacity is exceeded
  52   // the array will be reallocated.
  53   _nodes(C->comp_arena(), C->do_reduce_allocation_merges() ? C->unique()*1.10 : C->unique(), C->unique(), nullptr),
  54   _in_worklist(C->comp_arena()),
  55   _next_pidx(0),
  56   _collecting(true),
  57   _verify(false),

 146   GrowableArray<SafePointNode*>  sfn_worklist;
 147   GrowableArray<MergeMemNode*>   mergemem_worklist;
 148   DEBUG_ONLY( GrowableArray<Node*> addp_worklist; )
 149 
 150   { Compile::TracePhase tp(Phase::_t_connectionGraph);
 151 
 152   // 1. Populate Connection Graph (CG) with PointsTo nodes.
 153   ideal_nodes.map(C->live_nodes(), nullptr);  // preallocate space
 154   // Initialize worklist
 155   if (C->root() != nullptr) {
 156     ideal_nodes.push(C->root());
 157   }
 158   // Processed ideal nodes are unique on ideal_nodes list
 159   // but several ideal nodes are mapped to the phantom_obj.
 160   // To avoid duplicated entries on the following worklists
 161   // add the phantom_obj only once to them.
 162   ptnodes_worklist.append(phantom_obj);
 163   java_objects_worklist.append(phantom_obj);
 164   for( uint next = 0; next < ideal_nodes.size(); ++next ) {
 165     Node* n = ideal_nodes.at(next);










 166     // Create PointsTo nodes and add them to Connection Graph. Called
 167     // only once per ideal node since ideal_nodes is Unique_Node list.
 168     add_node_to_connection_graph(n, &delayed_worklist);
 169     PointsToNode* ptn = ptnode_adr(n->_idx);
 170     if (ptn != nullptr && ptn != phantom_obj) {
 171       ptnodes_worklist.append(ptn);
 172       if (ptn->is_JavaObject()) {
 173         java_objects_worklist.append(ptn->as_JavaObject());
 174         if ((n->is_Allocate() || n->is_CallStaticJava()) &&
 175             (ptn->escape_state() < PointsToNode::GlobalEscape)) {
 176           // Only allocations and java static calls results are interesting.
 177           non_escaped_allocs_worklist.append(ptn->as_JavaObject());
 178         }
 179       } else if (ptn->is_Field() && ptn->as_Field()->is_oop()) {
 180         oop_fields_worklist.append(ptn->as_Field());
 181       }
 182     }
 183     // Collect some interesting nodes for further use.
 184     switch (n->Opcode()) {
 185       case Op_MergeMem:

1235 
1236     // The next two inputs are:
1237     //  (1) A copy of the original pointer to NSR objects.
1238     //  (2) A selector, used to decide if we need to rematerialize an object
1239     //      or use the pointer to a NSR object.
1240     // See more details of these fields in the declaration of SafePointScalarMergeNode
1241     sfpt->add_req(nsr_merge_pointer);
1242     sfpt->add_req(selector);
1243 
1244     for (uint i = 1; i < ophi->req(); i++) {
1245       Node* base = ophi->in(i);
1246       JavaObjectNode* ptn = unique_java_object(base);
1247 
1248       // If the base is not scalar replaceable we don't need to register information about
1249       // it at this time.
1250       if (ptn == nullptr || !ptn->scalar_replaceable()) {
1251         continue;
1252       }
1253 
1254       AllocateNode* alloc = ptn->ideal_node()->as_Allocate();
1255       SafePointScalarObjectNode* sobj = mexp.create_scalarized_object_description(alloc, sfpt);








1256       if (sobj == nullptr) {

1257         return false;
1258       }
1259 
1260       // Now make a pass over the debug information replacing any references
1261       // to the allocated object with "sobj"
1262       Node* ccpp = alloc->result_cast();
1263       sfpt->replace_edges_in_range(ccpp, sobj, debug_start, jvms->debug_end(), _igvn);
1264 
1265       // Register the scalarized object as a candidate for reallocation
1266       smerge->add_req(sobj);









1267     }
1268 
1269     // Replaces debug information references to "original_sfpt_parent" in "sfpt" with references to "smerge"
1270     sfpt->replace_edges_in_range(original_sfpt_parent, smerge, debug_start, jvms->debug_end(), _igvn);
1271 
1272     // The call to 'replace_edges_in_range' above might have removed the
1273     // reference to ophi that we need at _merge_pointer_idx. The line below make
1274     // sure the reference is maintained.
1275     sfpt->set_req(smerge->merge_pointer_idx(jvms), nsr_merge_pointer);
1276     _igvn->_worklist.push(sfpt);
1277   }
1278 
1279   return true;
1280 }
1281 
1282 void ConnectionGraph::reduce_phi(PhiNode* ophi, GrowableArray<Node *>  &alloc_worklist, GrowableArray<Node *>  &memnode_worklist) {
1283   bool delay = _igvn->delay_transform();
1284   _igvn->set_delay_transform(true);
1285   _igvn->hash_delete(ophi);
1286 

1445   return false;
1446 }
1447 
1448 // Returns true if at least one of the arguments to the call is an object
1449 // that does not escape globally.
1450 bool ConnectionGraph::has_arg_escape(CallJavaNode* call) {
1451   if (call->method() != nullptr) {
1452     uint max_idx = TypeFunc::Parms + call->method()->arg_size();
1453     for (uint idx = TypeFunc::Parms; idx < max_idx; idx++) {
1454       Node* p = call->in(idx);
1455       if (not_global_escape(p)) {
1456         return true;
1457       }
1458     }
1459   } else {
1460     const char* name = call->as_CallStaticJava()->_name;
1461     assert(name != nullptr, "no name");
1462     // no arg escapes through uncommon traps
1463     if (strcmp(name, "uncommon_trap") != 0) {
1464       // process_call_arguments() assumes that all arguments escape globally
1465       const TypeTuple* d = call->tf()->domain();
1466       for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
1467         const Type* at = d->field_at(i);
1468         if (at->isa_oopptr() != nullptr) {
1469           return true;
1470         }
1471       }
1472     }
1473   }
1474   return false;
1475 }
1476 
1477 
1478 
1479 // Utility function for nodes that load an object
1480 void ConnectionGraph::add_objload_to_connection_graph(Node *n, Unique_Node_List *delayed_worklist) {
1481   // Using isa_ptr() instead of isa_oopptr() for LoadP and Phi because
1482   // ThreadLocal has RawPtr type.
1483   const Type* t = _igvn->type(n);
1484   if (t->make_ptr() != nullptr) {
1485     Node* adr = n->in(MemNode::Address);

1519       // first IGVN optimization when escape information is still available.
1520       record_for_optimizer(n);
1521     } else if (n->is_Allocate()) {
1522       add_call_node(n->as_Call());
1523       record_for_optimizer(n);
1524     } else {
1525       if (n->is_CallStaticJava()) {
1526         const char* name = n->as_CallStaticJava()->_name;
1527         if (name != nullptr && strcmp(name, "uncommon_trap") == 0) {
1528           return; // Skip uncommon traps
1529         }
1530       }
1531       // Don't mark as processed since call's arguments have to be processed.
1532       delayed_worklist->push(n);
1533       // Check if a call returns an object.
1534       if ((n->as_Call()->returns_pointer() &&
1535            n->as_Call()->proj_out_or_null(TypeFunc::Parms) != nullptr) ||
1536           (n->is_CallStaticJava() &&
1537            n->as_CallStaticJava()->is_boxing_method())) {
1538         add_call_node(n->as_Call());











1539       }
1540     }
1541     return;
1542   }
1543   // Put this check here to process call arguments since some call nodes
1544   // point to phantom_obj.
1545   if (n_ptn == phantom_obj || n_ptn == null_obj) {
1546     return; // Skip predefined nodes.
1547   }
1548   switch (opcode) {
1549     case Op_AddP: {
1550       Node* base = get_addp_base(n);
1551       PointsToNode* ptn_base = ptnode_adr(base->_idx);
1552       // Field nodes are created for all field types. They are used in
1553       // adjust_scalar_replaceable_state() and split_unique_types().
1554       // Note, non-oop fields will have only base edges in Connection
1555       // Graph because such fields are not used for oop loads and stores.
1556       int offset = address_offset(n, igvn);
1557       add_field(n, PointsToNode::NoEscape, offset);
1558       if (ptn_base == nullptr) {
1559         delayed_worklist->push(n); // Process it later.
1560       } else {
1561         n_ptn = ptnode_adr(n_idx);
1562         add_base(n_ptn->as_Field(), ptn_base);
1563       }
1564       break;
1565     }
1566     case Op_CastX2P: {

1567       map_ideal_node(n, phantom_obj);
1568       break;
1569     }

1570     case Op_CastPP:
1571     case Op_CheckCastPP:
1572     case Op_EncodeP:
1573     case Op_DecodeN:
1574     case Op_EncodePKlass:
1575     case Op_DecodeNKlass: {
1576       add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(1), delayed_worklist);
1577       break;
1578     }
1579     case Op_CMoveP: {
1580       add_local_var(n, PointsToNode::NoEscape);
1581       // Do not add edges during first iteration because some could be
1582       // not defined yet.
1583       delayed_worklist->push(n);
1584       break;
1585     }
1586     case Op_ConP:
1587     case Op_ConN:
1588     case Op_ConNKlass: {
1589       // assume all oop constants globally escape except for null

1621     case Op_PartialSubtypeCheck: {
1622       // Produces Null or notNull and is used in only in CmpP so
1623       // phantom_obj could be used.
1624       map_ideal_node(n, phantom_obj); // Result is unknown
1625       break;
1626     }
1627     case Op_Phi: {
1628       // Using isa_ptr() instead of isa_oopptr() for LoadP and Phi because
1629       // ThreadLocal has RawPtr type.
1630       const Type* t = n->as_Phi()->type();
1631       if (t->make_ptr() != nullptr) {
1632         add_local_var(n, PointsToNode::NoEscape);
1633         // Do not add edges during first iteration because some could be
1634         // not defined yet.
1635         delayed_worklist->push(n);
1636       }
1637       break;
1638     }
1639     case Op_Proj: {
1640       // we are only interested in the oop result projection from a call
1641       if (n->as_Proj()->_con == TypeFunc::Parms && n->in(0)->is_Call() &&
1642           n->in(0)->as_Call()->returns_pointer()) {


1643         add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(0), delayed_worklist);
1644       }
1645       break;
1646     }
1647     case Op_Rethrow: // Exception object escapes
1648     case Op_Return: {
1649       if (n->req() > TypeFunc::Parms &&
1650           igvn->type(n->in(TypeFunc::Parms))->isa_oopptr()) {
1651         // Treat Return value as LocalVar with GlobalEscape escape state.
1652         add_local_var_and_edge(n, PointsToNode::GlobalEscape, n->in(TypeFunc::Parms), delayed_worklist);
1653       }
1654       break;
1655     }
1656     case Op_CompareAndExchangeP:
1657     case Op_CompareAndExchangeN:
1658     case Op_GetAndSetP:
1659     case Op_GetAndSetN: {
1660       add_objload_to_connection_graph(n, delayed_worklist);
1661       // fall-through
1662     }

1724   if (n->is_Call()) {
1725     process_call_arguments(n->as_Call());
1726     return;
1727   }
1728   assert(n->is_Store() || n->is_LoadStore() ||
1729          ((n_ptn != nullptr) && (n_ptn->ideal_node() != nullptr)),
1730          "node should be registered already");
1731   int opcode = n->Opcode();
1732   bool gc_handled = BarrierSet::barrier_set()->barrier_set_c2()->escape_add_final_edges(this, _igvn, n, opcode);
1733   if (gc_handled) {
1734     return; // Ignore node if already handled by GC.
1735   }
1736   switch (opcode) {
1737     case Op_AddP: {
1738       Node* base = get_addp_base(n);
1739       PointsToNode* ptn_base = ptnode_adr(base->_idx);
1740       assert(ptn_base != nullptr, "field's base should be registered");
1741       add_base(n_ptn->as_Field(), ptn_base);
1742       break;
1743     }

1744     case Op_CastPP:
1745     case Op_CheckCastPP:
1746     case Op_EncodeP:
1747     case Op_DecodeN:
1748     case Op_EncodePKlass:
1749     case Op_DecodeNKlass: {
1750       add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(1), nullptr);
1751       break;
1752     }
1753     case Op_CMoveP: {
1754       for (uint i = CMoveNode::IfFalse; i < n->req(); i++) {
1755         Node* in = n->in(i);
1756         if (in == nullptr) {
1757           continue;  // ignore null
1758         }
1759         Node* uncast_in = in->uncast();
1760         if (uncast_in->is_top() || uncast_in == n) {
1761           continue;  // ignore top or inputs which go back this node
1762         }
1763         PointsToNode* ptn = ptnode_adr(in->_idx);

1778       // Using isa_ptr() instead of isa_oopptr() for LoadP and Phi because
1779       // ThreadLocal has RawPtr type.
1780       assert(n->as_Phi()->type()->make_ptr() != nullptr, "Unexpected node type");
1781       for (uint i = 1; i < n->req(); i++) {
1782         Node* in = n->in(i);
1783         if (in == nullptr) {
1784           continue;  // ignore null
1785         }
1786         Node* uncast_in = in->uncast();
1787         if (uncast_in->is_top() || uncast_in == n) {
1788           continue;  // ignore top or inputs which go back this node
1789         }
1790         PointsToNode* ptn = ptnode_adr(in->_idx);
1791         assert(ptn != nullptr, "node should be registered");
1792         add_edge(n_ptn, ptn);
1793       }
1794       break;
1795     }
1796     case Op_Proj: {
1797       // we are only interested in the oop result projection from a call
1798       assert(n->as_Proj()->_con == TypeFunc::Parms && n->in(0)->is_Call() &&
1799              n->in(0)->as_Call()->returns_pointer(), "Unexpected node type");
1800       add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(0), nullptr);
1801       break;
1802     }
1803     case Op_Rethrow: // Exception object escapes
1804     case Op_Return: {
1805       assert(n->req() > TypeFunc::Parms && _igvn->type(n->in(TypeFunc::Parms))->isa_oopptr(),
1806              "Unexpected node type");
1807       // Treat Return value as LocalVar with GlobalEscape escape state.
1808       add_local_var_and_edge(n, PointsToNode::GlobalEscape, n->in(TypeFunc::Parms), nullptr);
1809       break;
1810     }
1811     case Op_CompareAndExchangeP:
1812     case Op_CompareAndExchangeN:
1813     case Op_GetAndSetP:
1814     case Op_GetAndSetN:{
1815       assert(_igvn->type(n)->make_ptr() != nullptr, "Unexpected node type");
1816       add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(MemNode::Address), nullptr);
1817       // fall-through
1818     }
1819     case Op_CompareAndSwapP:

1955     PointsToNode* ptn = ptnode_adr(val->_idx);
1956     assert(ptn != nullptr, "node should be registered");
1957     set_escape_state(ptn, PointsToNode::GlobalEscape NOT_PRODUCT(COMMA "stored at raw address"));
1958     // Add edge to object for unsafe access with offset.
1959     PointsToNode* adr_ptn = ptnode_adr(adr->_idx);
1960     assert(adr_ptn != nullptr, "node should be registered");
1961     if (adr_ptn->is_Field()) {
1962       assert(adr_ptn->as_Field()->is_oop(), "should be oop field");
1963       add_edge(adr_ptn, ptn);
1964     }
1965     return true;
1966   }
1967 #ifdef ASSERT
1968   n->dump(1);
1969   assert(false, "not unsafe");
1970 #endif
1971   return false;
1972 }
1973 
1974 void ConnectionGraph::add_call_node(CallNode* call) {
1975   assert(call->returns_pointer(), "only for call which returns pointer");
1976   uint call_idx = call->_idx;
1977   if (call->is_Allocate()) {
1978     Node* k = call->in(AllocateNode::KlassNode);
1979     const TypeKlassPtr* kt = k->bottom_type()->isa_klassptr();
1980     assert(kt != nullptr, "TypeKlassPtr  required.");
1981     PointsToNode::EscapeState es = PointsToNode::NoEscape;
1982     bool scalar_replaceable = true;
1983     NOT_PRODUCT(const char* nsr_reason = "");
1984     if (call->is_AllocateArray()) {
1985       if (!kt->isa_aryklassptr()) { // StressReflectiveCode
1986         es = PointsToNode::GlobalEscape;
1987       } else {
1988         int length = call->in(AllocateNode::ALength)->find_int_con(-1);
1989         if (length < 0) {
1990           // Not scalar replaceable if the length is not constant.
1991           scalar_replaceable = false;
1992           NOT_PRODUCT(nsr_reason = "has a non-constant length");
1993         } else if (length > EliminateAllocationArraySizeLimit) {
1994           // Not scalar replaceable if the length is too big.
1995           scalar_replaceable = false;

2031     //
2032     //    - all oop arguments are escaping globally;
2033     //
2034     // 2. CallStaticJavaNode (execute bytecode analysis if possible):
2035     //
2036     //    - the same as CallDynamicJavaNode if can't do bytecode analysis;
2037     //
2038     //    - mapped to GlobalEscape JavaObject node if unknown oop is returned;
2039     //    - mapped to NoEscape JavaObject node if non-escaping object allocated
2040     //      during call is returned;
2041     //    - mapped to ArgEscape LocalVar node pointed to object arguments
2042     //      which are returned and does not escape during call;
2043     //
2044     //    - oop arguments escaping status is defined by bytecode analysis;
2045     //
2046     // For a static call, we know exactly what method is being called.
2047     // Use bytecode estimator to record whether the call's return value escapes.
2048     ciMethod* meth = call->as_CallJava()->method();
2049     if (meth == nullptr) {
2050       const char* name = call->as_CallStaticJava()->_name;
2051       assert(strncmp(name, "C2 Runtime multianewarray", 25) == 0, "TODO: add failed case check");

2052       // Returns a newly allocated non-escaped object.
2053       add_java_object(call, PointsToNode::NoEscape);
2054       set_not_scalar_replaceable(ptnode_adr(call_idx) NOT_PRODUCT(COMMA "is result of multinewarray"));
2055     } else if (meth->is_boxing_method()) {
2056       // Returns boxing object
2057       PointsToNode::EscapeState es;
2058       vmIntrinsics::ID intr = meth->intrinsic_id();
2059       if (intr == vmIntrinsics::_floatValue || intr == vmIntrinsics::_doubleValue) {
2060         // It does not escape if object is always allocated.
2061         es = PointsToNode::NoEscape;
2062       } else {
2063         // It escapes globally if object could be loaded from cache.
2064         es = PointsToNode::GlobalEscape;
2065       }
2066       add_java_object(call, es);
2067       if (es == PointsToNode::GlobalEscape) {
2068         set_not_scalar_replaceable(ptnode_adr(call->_idx) NOT_PRODUCT(COMMA "object can be loaded from boxing cache"));
2069       }
2070     } else {
2071       BCEscapeAnalyzer* call_analyzer = meth->get_bcea();
2072       call_analyzer->copy_dependencies(_compile->dependencies());
2073       if (call_analyzer->is_return_allocated()) {
2074         // Returns a newly allocated non-escaped object, simply
2075         // update dependency information.
2076         // Mark it as NoEscape so that objects referenced by
2077         // it's fields will be marked as NoEscape at least.
2078         add_java_object(call, PointsToNode::NoEscape);
2079         set_not_scalar_replaceable(ptnode_adr(call_idx) NOT_PRODUCT(COMMA "is result of call"));
2080       } else {
2081         // Determine whether any arguments are returned.
2082         const TypeTuple* d = call->tf()->domain();
2083         bool ret_arg = false;
2084         for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
2085           if (d->field_at(i)->isa_ptr() != nullptr &&
2086               call_analyzer->is_arg_returned(i - TypeFunc::Parms)) {
2087             ret_arg = true;
2088             break;
2089           }
2090         }
2091         if (ret_arg) {
2092           add_local_var(call, PointsToNode::ArgEscape);
2093         } else {
2094           // Returns unknown object.
2095           map_ideal_node(call, phantom_obj);
2096         }
2097       }
2098     }
2099   } else {
2100     // An other type of call, assume the worst case:
2101     // returned value is unknown and globally escapes.
2102     assert(call->Opcode() == Op_CallDynamicJava, "add failed case check");

2110 #ifdef ASSERT
2111     case Op_Allocate:
2112     case Op_AllocateArray:
2113     case Op_Lock:
2114     case Op_Unlock:
2115       assert(false, "should be done already");
2116       break;
2117 #endif
2118     case Op_ArrayCopy:
2119     case Op_CallLeafNoFP:
2120       // Most array copies are ArrayCopy nodes at this point but there
2121       // are still a few direct calls to the copy subroutines (See
2122       // PhaseStringOpts::copy_string())
2123       is_arraycopy = (call->Opcode() == Op_ArrayCopy) ||
2124         call->as_CallLeaf()->is_call_to_arraycopystub();
2125       // fall through
2126     case Op_CallLeafVector:
2127     case Op_CallLeaf: {
2128       // Stub calls, objects do not escape but they are not scale replaceable.
2129       // Adjust escape state for outgoing arguments.
2130       const TypeTuple * d = call->tf()->domain();
2131       bool src_has_oops = false;
2132       for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
2133         const Type* at = d->field_at(i);
2134         Node *arg = call->in(i);
2135         if (arg == nullptr) {
2136           continue;
2137         }
2138         const Type *aat = _igvn->type(arg);
2139         if (arg->is_top() || !at->isa_ptr() || !aat->isa_ptr()) {
2140           continue;
2141         }
2142         if (arg->is_AddP()) {
2143           //
2144           // The inline_native_clone() case when the arraycopy stub is called
2145           // after the allocation before Initialize and CheckCastPP nodes.
2146           // Or normal arraycopy for object arrays case.
2147           //
2148           // Set AddP's base (Allocate) as not scalar replaceable since
2149           // pointer to the base (with offset) is passed as argument.
2150           //
2151           arg = get_addp_base(arg);
2152         }
2153         PointsToNode* arg_ptn = ptnode_adr(arg->_idx);
2154         assert(arg_ptn != nullptr, "should be registered");
2155         PointsToNode::EscapeState arg_esc = arg_ptn->escape_state();
2156         if (is_arraycopy || arg_esc < PointsToNode::ArgEscape) {
2157           assert(aat == Type::TOP || aat == TypePtr::NULL_PTR ||
2158                  aat->isa_ptr() != nullptr, "expecting an Ptr");
2159           bool arg_has_oops = aat->isa_oopptr() &&
2160                               (aat->isa_instptr() ||
2161                                (aat->isa_aryptr() && (aat->isa_aryptr()->elem() == Type::BOTTOM || aat->isa_aryptr()->elem()->make_oopptr() != nullptr)));



2162           if (i == TypeFunc::Parms) {
2163             src_has_oops = arg_has_oops;
2164           }
2165           //
2166           // src or dst could be j.l.Object when other is basic type array:
2167           //
2168           //   arraycopy(char[],0,Object*,0,size);
2169           //   arraycopy(Object*,0,char[],0,size);
2170           //
2171           // Don't add edges in such cases.
2172           //
2173           bool arg_is_arraycopy_dest = src_has_oops && is_arraycopy &&
2174                                        arg_has_oops && (i > TypeFunc::Parms);
2175 #ifdef ASSERT
2176           if (!(is_arraycopy ||
2177                 BarrierSet::barrier_set()->barrier_set_c2()->is_gc_barrier_node(call) ||
2178                 (call->as_CallLeaf()->_name != nullptr &&
2179                  (strcmp(call->as_CallLeaf()->_name, "updateBytesCRC32") == 0 ||
2180                   strcmp(call->as_CallLeaf()->_name, "updateBytesCRC32C") == 0 ||
2181                   strcmp(call->as_CallLeaf()->_name, "updateBytesAdler32") == 0 ||

2205                   strcmp(call->as_CallLeaf()->_name, "dilithiumMontMulByConstant") == 0 ||
2206                   strcmp(call->as_CallLeaf()->_name, "dilithiumDecomposePoly") == 0 ||
2207                   strcmp(call->as_CallLeaf()->_name, "encodeBlock") == 0 ||
2208                   strcmp(call->as_CallLeaf()->_name, "decodeBlock") == 0 ||
2209                   strcmp(call->as_CallLeaf()->_name, "md5_implCompress") == 0 ||
2210                   strcmp(call->as_CallLeaf()->_name, "md5_implCompressMB") == 0 ||
2211                   strcmp(call->as_CallLeaf()->_name, "sha1_implCompress") == 0 ||
2212                   strcmp(call->as_CallLeaf()->_name, "sha1_implCompressMB") == 0 ||
2213                   strcmp(call->as_CallLeaf()->_name, "sha256_implCompress") == 0 ||
2214                   strcmp(call->as_CallLeaf()->_name, "sha256_implCompressMB") == 0 ||
2215                   strcmp(call->as_CallLeaf()->_name, "sha512_implCompress") == 0 ||
2216                   strcmp(call->as_CallLeaf()->_name, "sha512_implCompressMB") == 0 ||
2217                   strcmp(call->as_CallLeaf()->_name, "sha3_implCompress") == 0 ||
2218                   strcmp(call->as_CallLeaf()->_name, "double_keccak") == 0 ||
2219                   strcmp(call->as_CallLeaf()->_name, "sha3_implCompressMB") == 0 ||
2220                   strcmp(call->as_CallLeaf()->_name, "multiplyToLen") == 0 ||
2221                   strcmp(call->as_CallLeaf()->_name, "squareToLen") == 0 ||
2222                   strcmp(call->as_CallLeaf()->_name, "mulAdd") == 0 ||
2223                   strcmp(call->as_CallLeaf()->_name, "montgomery_multiply") == 0 ||
2224                   strcmp(call->as_CallLeaf()->_name, "montgomery_square") == 0 ||



2225                   strcmp(call->as_CallLeaf()->_name, "bigIntegerRightShiftWorker") == 0 ||
2226                   strcmp(call->as_CallLeaf()->_name, "bigIntegerLeftShiftWorker") == 0 ||
2227                   strcmp(call->as_CallLeaf()->_name, "vectorizedMismatch") == 0 ||
2228                   strcmp(call->as_CallLeaf()->_name, "stringIndexOf") == 0 ||
2229                   strcmp(call->as_CallLeaf()->_name, "arraysort_stub") == 0 ||
2230                   strcmp(call->as_CallLeaf()->_name, "array_partition_stub") == 0 ||
2231                   strcmp(call->as_CallLeaf()->_name, "get_class_id_intrinsic") == 0 ||
2232                   strcmp(call->as_CallLeaf()->_name, "unsafe_setmemory") == 0)
2233                  ))) {
2234             call->dump();
2235             fatal("EA unexpected CallLeaf %s", call->as_CallLeaf()->_name);
2236           }
2237 #endif
2238           // Always process arraycopy's destination object since
2239           // we need to add all possible edges to references in
2240           // source object.
2241           if (arg_esc >= PointsToNode::ArgEscape &&
2242               !arg_is_arraycopy_dest) {
2243             continue;
2244           }

2271           }
2272         }
2273       }
2274       break;
2275     }
2276     case Op_CallStaticJava: {
2277       // For a static call, we know exactly what method is being called.
2278       // Use bytecode estimator to record the call's escape affects
2279 #ifdef ASSERT
2280       const char* name = call->as_CallStaticJava()->_name;
2281       assert((name == nullptr || strcmp(name, "uncommon_trap") != 0), "normal calls only");
2282 #endif
2283       ciMethod* meth = call->as_CallJava()->method();
2284       if ((meth != nullptr) && meth->is_boxing_method()) {
2285         break; // Boxing methods do not modify any oops.
2286       }
2287       BCEscapeAnalyzer* call_analyzer = (meth !=nullptr) ? meth->get_bcea() : nullptr;
2288       // fall-through if not a Java method or no analyzer information
2289       if (call_analyzer != nullptr) {
2290         PointsToNode* call_ptn = ptnode_adr(call->_idx);
2291         const TypeTuple* d = call->tf()->domain();
2292         for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
2293           const Type* at = d->field_at(i);
2294           int k = i - TypeFunc::Parms;
2295           Node* arg = call->in(i);
2296           PointsToNode* arg_ptn = ptnode_adr(arg->_idx);
2297           if (at->isa_ptr() != nullptr &&
2298               call_analyzer->is_arg_returned(k)) {
2299             // The call returns arguments.
2300             if (call_ptn != nullptr) { // Is call's result used?
2301               assert(call_ptn->is_LocalVar(), "node should be registered");
2302               assert(arg_ptn != nullptr, "node should be registered");
2303               add_edge(call_ptn, arg_ptn);
2304             }
2305           }
2306           if (at->isa_oopptr() != nullptr &&
2307               arg_ptn->escape_state() < PointsToNode::GlobalEscape) {
2308             if (!call_analyzer->is_arg_stack(k)) {
2309               // The argument global escapes
2310               set_escape_state(arg_ptn, PointsToNode::GlobalEscape NOT_PRODUCT(COMMA trace_arg_escape_message(call)));
2311             } else {

2315                 set_fields_escape_state(arg_ptn, PointsToNode::GlobalEscape NOT_PRODUCT(COMMA trace_arg_escape_message(call)));
2316               }
2317             }
2318           }
2319         }
2320         if (call_ptn != nullptr && call_ptn->is_LocalVar()) {
2321           // The call returns arguments.
2322           assert(call_ptn->edge_count() > 0, "sanity");
2323           if (!call_analyzer->is_return_local()) {
2324             // Returns also unknown object.
2325             add_edge(call_ptn, phantom_obj);
2326           }
2327         }
2328         break;
2329       }
2330     }
2331     default: {
2332       // Fall-through here if not a Java method or no analyzer information
2333       // or some other type of call, assume the worst case: all arguments
2334       // globally escape.
2335       const TypeTuple* d = call->tf()->domain();
2336       for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
2337         const Type* at = d->field_at(i);
2338         if (at->isa_oopptr() != nullptr) {
2339           Node* arg = call->in(i);
2340           if (arg->is_AddP()) {
2341             arg = get_addp_base(arg);
2342           }
2343           assert(ptnode_adr(arg->_idx) != nullptr, "should be defined already");
2344           set_escape_state(ptnode_adr(arg->_idx), PointsToNode::GlobalEscape NOT_PRODUCT(COMMA trace_arg_escape_message(call)));
2345         }
2346       }
2347     }
2348   }
2349 }
2350 
2351 
2352 // Finish Graph construction.
2353 bool ConnectionGraph::complete_connection_graph(
2354                          GrowableArray<PointsToNode*>&   ptnodes_worklist,
2355                          GrowableArray<JavaObjectNode*>& non_escaped_allocs_worklist,

2728     PointsToNode* base = i.get();
2729     if (base->is_JavaObject()) {
2730       // Skip Allocate's fields which will be processed later.
2731       if (base->ideal_node()->is_Allocate()) {
2732         return 0;
2733       }
2734       assert(base == null_obj, "only null ptr base expected here");
2735     }
2736   }
2737   if (add_edge(field, phantom_obj)) {
2738     // New edge was added
2739     new_edges++;
2740     add_field_uses_to_worklist(field);
2741   }
2742   return new_edges;
2743 }
2744 
2745 // Find fields initializing values for allocations.
2746 int ConnectionGraph::find_init_values_phantom(JavaObjectNode* pta) {
2747   assert(pta->escape_state() == PointsToNode::NoEscape, "Not escaped Allocate nodes only");

2748   Node* alloc = pta->ideal_node();
2749 
2750   // Do nothing for Allocate nodes since its fields values are
2751   // "known" unless they are initialized by arraycopy/clone.
2752   if (alloc->is_Allocate() && !pta->arraycopy_dst()) {
2753     return 0;







2754   }
2755   assert(pta->arraycopy_dst() || alloc->as_CallStaticJava(), "sanity");

2756 #ifdef ASSERT
2757   if (!pta->arraycopy_dst() && alloc->as_CallStaticJava()->method() == nullptr) {
2758     const char* name = alloc->as_CallStaticJava()->_name;
2759     assert(strncmp(name, "C2 Runtime multianewarray", 25) == 0, "sanity");

2760   }
2761 #endif
2762   // Non-escaped allocation returned from Java or runtime call have unknown values in fields.
2763   int new_edges = 0;
2764   for (EdgeIterator i(pta); i.has_next(); i.next()) {
2765     PointsToNode* field = i.get();
2766     if (field->is_Field() && field->as_Field()->is_oop()) {
2767       if (add_edge(field, phantom_obj)) {
2768         // New edge was added
2769         new_edges++;
2770         add_field_uses_to_worklist(field->as_Field());
2771       }
2772     }
2773   }
2774   return new_edges;
2775 }
2776 
2777 // Find fields initializing values for allocations.
2778 int ConnectionGraph::find_init_values_null(JavaObjectNode* pta, PhaseValues* phase) {
2779   assert(pta->escape_state() == PointsToNode::NoEscape, "Not escaped Allocate nodes only");
2780   Node* alloc = pta->ideal_node();
2781   // Do nothing for Call nodes since its fields values are unknown.
2782   if (!alloc->is_Allocate()) {
2783     return 0;
2784   }
2785   InitializeNode* ini = alloc->as_Allocate()->initialization();
2786   bool visited_bottom_offset = false;
2787   GrowableArray<int> offsets_worklist;
2788   int new_edges = 0;
2789 
2790   // Check if an oop field's initializing value is recorded and add
2791   // a corresponding null if field's value if it is not recorded.
2792   // Connection Graph does not record a default initialization by null
2793   // captured by Initialize node.
2794   //
2795   for (EdgeIterator i(pta); i.has_next(); i.next()) {
2796     PointsToNode* field = i.get(); // Field (AddP)
2797     if (!field->is_Field() || !field->as_Field()->is_oop()) {
2798       continue; // Not oop field
2799     }
2800     int offset = field->as_Field()->offset();
2801     if (offset == Type::OffsetBot) {
2802       if (!visited_bottom_offset) {

2848               } else {
2849                 if (!val->is_LocalVar() || (val->edge_count() == 0)) {
2850                   tty->print_cr("----------init store has invalid value -----");
2851                   store->dump();
2852                   val->dump();
2853                   assert(val->is_LocalVar() && (val->edge_count() > 0), "should be processed already");
2854                 }
2855                 for (EdgeIterator j(val); j.has_next(); j.next()) {
2856                   PointsToNode* obj = j.get();
2857                   if (obj->is_JavaObject()) {
2858                     if (!field->points_to(obj->as_JavaObject())) {
2859                       missed_obj = obj;
2860                       break;
2861                     }
2862                   }
2863                 }
2864               }
2865               if (missed_obj != nullptr) {
2866                 tty->print_cr("----------field---------------------------------");
2867                 field->dump();
2868                 tty->print_cr("----------missed referernce to object-----------");
2869                 missed_obj->dump();
2870                 tty->print_cr("----------object referernced by init store -----");
2871                 store->dump();
2872                 val->dump();
2873                 assert(!field->points_to(missed_obj->as_JavaObject()), "missed JavaObject reference");
2874               }
2875             }
2876 #endif
2877           } else {
2878             // There could be initializing stores which follow allocation.
2879             // For example, a volatile field store is not collected
2880             // by Initialize node.
2881             //
2882             // Need to check for dependent loads to separate such stores from
2883             // stores which follow loads. For now, add initial value null so
2884             // that compare pointers optimization works correctly.
2885           }
2886         }
2887         if (value == nullptr) {
2888           // A field's initializing value was not recorded. Add null.
2889           if (add_edge(field, null_obj)) {
2890             // New edge was added

3206         assert(field->edge_count() > 0, "sanity");
3207       }
3208     }
3209   }
3210 }
3211 #endif
3212 
3213 // Optimize ideal graph.
3214 void ConnectionGraph::optimize_ideal_graph(GrowableArray<Node*>& ptr_cmp_worklist,
3215                                            GrowableArray<MemBarStoreStoreNode*>& storestore_worklist) {
3216   Compile* C = _compile;
3217   PhaseIterGVN* igvn = _igvn;
3218   if (EliminateLocks) {
3219     // Mark locks before changing ideal graph.
3220     int cnt = C->macro_count();
3221     for (int i = 0; i < cnt; i++) {
3222       Node *n = C->macro_node(i);
3223       if (n->is_AbstractLock()) { // Lock and Unlock nodes
3224         AbstractLockNode* alock = n->as_AbstractLock();
3225         if (!alock->is_non_esc_obj()) {
3226           if (can_eliminate_lock(alock)) {

3227             assert(!alock->is_eliminated() || alock->is_coarsened(), "sanity");
3228             // The lock could be marked eliminated by lock coarsening
3229             // code during first IGVN before EA. Replace coarsened flag
3230             // to eliminate all associated locks/unlocks.
3231 #ifdef ASSERT
3232             alock->log_lock_optimization(C, "eliminate_lock_set_non_esc3");
3233 #endif
3234             alock->set_non_esc_obj();
3235           }
3236         }
3237       }
3238     }
3239   }
3240 
3241   if (OptimizePtrCompare) {
3242     for (int i = 0; i < ptr_cmp_worklist.length(); i++) {
3243       Node *n = ptr_cmp_worklist.at(i);
3244       assert(n->Opcode() == Op_CmpN || n->Opcode() == Op_CmpP, "must be");
3245       const TypeInt* tcmp = optimize_ptr_compare(n->in(1), n->in(2));
3246       if (tcmp->singleton()) {

3248 #ifndef PRODUCT
3249         if (PrintOptimizePtrCompare) {
3250           tty->print_cr("++++ Replaced: %d %s(%d,%d) --> %s", n->_idx, (n->Opcode() == Op_CmpP ? "CmpP" : "CmpN"), n->in(1)->_idx, n->in(2)->_idx, (tcmp == TypeInt::CC_EQ ? "EQ" : "NotEQ"));
3251           if (Verbose) {
3252             n->dump(1);
3253           }
3254         }
3255 #endif
3256         igvn->replace_node(n, cmp);
3257       }
3258     }
3259   }
3260 
3261   // For MemBarStoreStore nodes added in library_call.cpp, check
3262   // escape status of associated AllocateNode and optimize out
3263   // MemBarStoreStore node if the allocated object never escapes.
3264   for (int i = 0; i < storestore_worklist.length(); i++) {
3265     Node* storestore = storestore_worklist.at(i);
3266     Node* alloc = storestore->in(MemBarNode::Precedent)->in(0);
3267     if (alloc->is_Allocate() && not_global_escape(alloc)) {
3268       MemBarNode* mb = MemBarNode::make(C, Op_MemBarCPUOrder, Compile::AliasIdxBot);
3269       mb->init_req(TypeFunc::Memory,  storestore->in(TypeFunc::Memory));
3270       mb->init_req(TypeFunc::Control, storestore->in(TypeFunc::Control));
3271       igvn->register_new_node_with_optimizer(mb);
3272       igvn->replace_node(storestore, mb);





3273     }
3274   }
3275 }
3276 
3277 // Optimize objects compare.
3278 const TypeInt* ConnectionGraph::optimize_ptr_compare(Node* left, Node* right) {
3279   assert(OptimizePtrCompare, "sanity");
3280   const TypeInt* EQ = TypeInt::CC_EQ; // [0] == ZERO
3281   const TypeInt* NE = TypeInt::CC_GT; // [1] == ONE
3282   const TypeInt* UNKNOWN = TypeInt::CC;    // [-1, 0,1]
3283 
3284   PointsToNode* ptn1 = ptnode_adr(left->_idx);
3285   PointsToNode* ptn2 = ptnode_adr(right->_idx);
3286   JavaObjectNode* jobj1 = unique_java_object(left);
3287   JavaObjectNode* jobj2 = unique_java_object(right);
3288 
3289   // The use of this method during allocation merge reduction may cause 'left'
3290   // or 'right' be something (e.g., a Phi) that isn't in the connection graph or
3291   // that doesn't reference an unique java object.
3292   if (ptn1 == nullptr || ptn2 == nullptr ||

3414   assert(!src->is_Field() && !dst->is_Field(), "only for JavaObject and LocalVar");
3415   assert((src != null_obj) && (dst != null_obj), "not for ConP null");
3416   PointsToNode* ptadr = _nodes.at(n->_idx);
3417   if (ptadr != nullptr) {
3418     assert(ptadr->is_Arraycopy() && ptadr->ideal_node() == n, "sanity");
3419     return;
3420   }
3421   Compile* C = _compile;
3422   ptadr = new (C->comp_arena()) ArraycopyNode(this, n, es);
3423   map_ideal_node(n, ptadr);
3424   // Add edge from arraycopy node to source object.
3425   (void)add_edge(ptadr, src);
3426   src->set_arraycopy_src();
3427   // Add edge from destination object to arraycopy node.
3428   (void)add_edge(dst, ptadr);
3429   dst->set_arraycopy_dst();
3430 }
3431 
3432 bool ConnectionGraph::is_oop_field(Node* n, int offset, bool* unsafe) {
3433   const Type* adr_type = n->as_AddP()->bottom_type();

3434   BasicType bt = T_INT;
3435   if (offset == Type::OffsetBot) {
3436     // Check only oop fields.
3437     if (!adr_type->isa_aryptr() ||
3438         adr_type->isa_aryptr()->elem() == Type::BOTTOM ||
3439         adr_type->isa_aryptr()->elem()->make_oopptr() != nullptr) {
3440       // OffsetBot is used to reference array's element. Ignore first AddP.
3441       if (find_second_addp(n, n->in(AddPNode::Base)) == nullptr) {
3442         bt = T_OBJECT;
3443       }
3444     }
3445   } else if (offset != oopDesc::klass_offset_in_bytes()) {
3446     if (adr_type->isa_instptr()) {
3447       ciField* field = _compile->alias_type(adr_type->isa_instptr())->field();
3448       if (field != nullptr) {
3449         bt = field->layout_type();
3450       } else {
3451         // Check for unsafe oop field access
3452         if (n->has_out_with(Op_StoreP, Op_LoadP, Op_StoreN, Op_LoadN) ||
3453             n->has_out_with(Op_GetAndSetP, Op_GetAndSetN, Op_CompareAndExchangeP, Op_CompareAndExchangeN) ||
3454             n->has_out_with(Op_CompareAndSwapP, Op_CompareAndSwapN, Op_WeakCompareAndSwapP, Op_WeakCompareAndSwapN) ||
3455             BarrierSet::barrier_set()->barrier_set_c2()->escape_has_out_with_unsafe_object(n)) {
3456           bt = T_OBJECT;
3457           (*unsafe) = true;
3458         }
3459       }
3460     } else if (adr_type->isa_aryptr()) {
3461       if (offset == arrayOopDesc::length_offset_in_bytes()) {
3462         // Ignore array length load.
3463       } else if (find_second_addp(n, n->in(AddPNode::Base)) != nullptr) {
3464         // Ignore first AddP.
3465       } else {
3466         const Type* elemtype = adr_type->isa_aryptr()->elem();
3467         bt = elemtype->array_element_basic_type();






3468       }
3469     } else if (adr_type->isa_rawptr() || adr_type->isa_klassptr()) {
3470       // Allocation initialization, ThreadLocal field access, unsafe access
3471       if (n->has_out_with(Op_StoreP, Op_LoadP, Op_StoreN, Op_LoadN) ||
3472           n->has_out_with(Op_GetAndSetP, Op_GetAndSetN, Op_CompareAndExchangeP, Op_CompareAndExchangeN) ||
3473           n->has_out_with(Op_CompareAndSwapP, Op_CompareAndSwapN, Op_WeakCompareAndSwapP, Op_WeakCompareAndSwapN) ||
3474           BarrierSet::barrier_set()->barrier_set_c2()->escape_has_out_with_unsafe_object(n)) {
3475         bt = T_OBJECT;
3476       }
3477     }
3478   }
3479   // Note: T_NARROWOOP is not classed as a real reference type
3480   return (is_reference_type(bt) || bt == T_NARROWOOP);
3481 }
3482 
3483 // Returns unique pointed java object or null.
3484 JavaObjectNode* ConnectionGraph::unique_java_object(Node *n) const {
3485   // If the node was created after the escape computation we can't answer.
3486   uint idx = n->_idx;
3487   if (idx >= nodes_size()) {

3644             return true;
3645           }
3646         }
3647       }
3648     }
3649   }
3650   return false;
3651 }
3652 
3653 int ConnectionGraph::address_offset(Node* adr, PhaseValues* phase) {
3654   const Type *adr_type = phase->type(adr);
3655   if (adr->is_AddP() && adr_type->isa_oopptr() == nullptr && is_captured_store_address(adr)) {
3656     // We are computing a raw address for a store captured by an Initialize
3657     // compute an appropriate address type. AddP cases #3 and #5 (see below).
3658     int offs = (int)phase->find_intptr_t_con(adr->in(AddPNode::Offset), Type::OffsetBot);
3659     assert(offs != Type::OffsetBot ||
3660            adr->in(AddPNode::Address)->in(0)->is_AllocateArray(),
3661            "offset must be a constant or it is initialization of array");
3662     return offs;
3663   }
3664   const TypePtr *t_ptr = adr_type->isa_ptr();
3665   assert(t_ptr != nullptr, "must be a pointer type");
3666   return t_ptr->offset();
3667 }
3668 
3669 Node* ConnectionGraph::get_addp_base(Node *addp) {
3670   assert(addp->is_AddP(), "must be AddP");
3671   //
3672   // AddP cases for Base and Address inputs:
3673   // case #1. Direct object's field reference:
3674   //     Allocate
3675   //       |
3676   //     Proj #5 ( oop result )
3677   //       |
3678   //     CheckCastPP (cast to instance type)
3679   //      | |
3680   //     AddP  ( base == address )
3681   //
3682   // case #2. Indirect object's field reference:
3683   //      Phi
3684   //       |
3685   //     CastPP (cast to instance type)
3686   //      | |

3800   }
3801   return nullptr;
3802 }
3803 
3804 //
3805 // Adjust the type and inputs of an AddP which computes the
3806 // address of a field of an instance
3807 //
3808 bool ConnectionGraph::split_AddP(Node *addp, Node *base) {
3809   PhaseGVN* igvn = _igvn;
3810   const TypeOopPtr *base_t = igvn->type(base)->isa_oopptr();
3811   assert(base_t != nullptr && base_t->is_known_instance(), "expecting instance oopptr");
3812   const TypeOopPtr *t = igvn->type(addp)->isa_oopptr();
3813   if (t == nullptr) {
3814     // We are computing a raw address for a store captured by an Initialize
3815     // compute an appropriate address type (cases #3 and #5).
3816     assert(igvn->type(addp) == TypeRawPtr::NOTNULL, "must be raw pointer");
3817     assert(addp->in(AddPNode::Address)->is_Proj(), "base of raw address must be result projection from allocation");
3818     intptr_t offs = (int)igvn->find_intptr_t_con(addp->in(AddPNode::Offset), Type::OffsetBot);
3819     assert(offs != Type::OffsetBot, "offset must be a constant");
3820     t = base_t->add_offset(offs)->is_oopptr();







3821   }
3822   int inst_id =  base_t->instance_id();
3823   assert(!t->is_known_instance() || t->instance_id() == inst_id,
3824                              "old type must be non-instance or match new type");
3825 
3826   // The type 't' could be subclass of 'base_t'.
3827   // As result t->offset() could be large then base_t's size and it will
3828   // cause the failure in add_offset() with narrow oops since TypeOopPtr()
3829   // constructor verifies correctness of the offset.
3830   //
3831   // It could happened on subclass's branch (from the type profiling
3832   // inlining) which was not eliminated during parsing since the exactness
3833   // of the allocation type was not propagated to the subclass type check.
3834   //
3835   // Or the type 't' could be not related to 'base_t' at all.
3836   // It could happened when CHA type is different from MDO type on a dead path
3837   // (for example, from instanceof check) which is not collapsed during parsing.
3838   //
3839   // Do nothing for such AddP node and don't process its users since
3840   // this code branch will go away.
3841   //
3842   if (!t->is_known_instance() &&
3843       !base_t->maybe_java_subtype_of(t)) {
3844      return false; // bail out
3845   }
3846   const TypeOopPtr *tinst = base_t->add_offset(t->offset())->is_oopptr();











3847   // Do NOT remove the next line: ensure a new alias index is allocated
3848   // for the instance type. Note: C++ will not remove it since the call
3849   // has side effect.
3850   int alias_idx = _compile->get_alias_index(tinst);
3851   igvn->set_type(addp, tinst);
3852   // record the allocation in the node map
3853   set_map(addp, get_map(base->_idx));
3854   // Set addp's Base and Address to 'base'.
3855   Node *abase = addp->in(AddPNode::Base);
3856   Node *adr   = addp->in(AddPNode::Address);
3857   if (adr->is_Proj() && adr->in(0)->is_Allocate() &&
3858       adr->in(0)->_idx == (uint)inst_id) {
3859     // Skip AddP cases #3 and #5.
3860   } else {
3861     assert(!abase->is_top(), "sanity"); // AddP case #3
3862     if (abase != base) {
3863       igvn->hash_delete(addp);
3864       addp->set_req(AddPNode::Base, base);
3865       if (abase == adr) {
3866         addp->set_req(AddPNode::Address, base);

4532         ptnode_adr(n->_idx)->dump();
4533         assert(jobj != nullptr && jobj != phantom_obj, "escaped allocation");
4534 #endif
4535         _compile->record_failure(_invocation > 0 ? C2Compiler::retry_no_iterative_escape_analysis() : C2Compiler::retry_no_escape_analysis());
4536         return;
4537       } else {
4538         Node *val = get_map(jobj->idx());   // CheckCastPP node
4539         TypeNode *tn = n->as_Type();
4540         const TypeOopPtr* tinst = igvn->type(val)->isa_oopptr();
4541         assert(tinst != nullptr && tinst->is_known_instance() &&
4542                tinst->instance_id() == jobj->idx() , "instance type expected.");
4543 
4544         const Type *tn_type = igvn->type(tn);
4545         const TypeOopPtr *tn_t;
4546         if (tn_type->isa_narrowoop()) {
4547           tn_t = tn_type->make_ptr()->isa_oopptr();
4548         } else {
4549           tn_t = tn_type->isa_oopptr();
4550         }
4551         if (tn_t != nullptr && tinst->maybe_java_subtype_of(tn_t)) {







4552           if (tn_type->isa_narrowoop()) {
4553             tn_type = tinst->make_narrowoop();
4554           } else {
4555             tn_type = tinst;
4556           }
4557           igvn->hash_delete(tn);
4558           igvn->set_type(tn, tn_type);
4559           tn->set_type(tn_type);
4560           igvn->hash_insert(tn);
4561           record_for_optimizer(n);
4562         } else {
4563           assert(tn_type == TypePtr::NULL_PTR ||
4564                  (tn_t != nullptr && !tinst->maybe_java_subtype_of(tn_t)),
4565                  "unexpected type");
4566           continue; // Skip dead path with different type
4567         }
4568       }
4569     } else {
4570       debug_only(n->dump();)
4571       assert(false, "EA: unexpected node");
4572       continue;
4573     }
4574     // push allocation's users on appropriate worklist
4575     for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
4576       Node *use = n->fast_out(i);
4577       if(use->is_Mem() && use->in(MemNode::Address) == n) {
4578         // Load/store to instance's field
4579         memnode_worklist.append_if_missing(use);
4580       } else if (use->is_MemBar()) {
4581         if (use->in(TypeFunc::Memory) == n) { // Ignore precedent edge
4582           memnode_worklist.append_if_missing(use);
4583         }
4584       } else if (use->is_AddP() && use->outcnt() > 0) { // No dead nodes
4585         Node* addp2 = find_second_addp(use, n);
4586         if (addp2 != nullptr) {
4587           alloc_worklist.append_if_missing(addp2);
4588         }
4589         alloc_worklist.append_if_missing(use);
4590       } else if (use->is_Phi() ||
4591                  use->is_CheckCastPP() ||
4592                  use->is_EncodeNarrowPtr() ||
4593                  use->is_DecodeNarrowPtr() ||
4594                  (use->is_ConstraintCast() && use->Opcode() == Op_CastPP)) {
4595         alloc_worklist.append_if_missing(use);
4596 #ifdef ASSERT
4597       } else if (use->is_Mem()) {
4598         assert(use->in(MemNode::Address) != n, "EA: missing allocation reference path");
4599       } else if (use->is_MergeMem()) {
4600         assert(mergemem_worklist.contains(use->as_MergeMem()), "EA: missing MergeMem node in the worklist");
4601       } else if (use->is_SafePoint()) {
4602         // Look for MergeMem nodes for calls which reference unique allocation
4603         // (through CheckCastPP nodes) even for debug info.
4604         Node* m = use->in(TypeFunc::Memory);
4605         if (m->is_MergeMem()) {
4606           assert(mergemem_worklist.contains(m->as_MergeMem()), "EA: missing MergeMem node in the worklist");
4607         }
4608       } else if (use->Opcode() == Op_EncodeISOArray) {
4609         if (use->in(MemNode::Memory) == n || use->in(3) == n) {
4610           // EncodeISOArray overwrites destination array
4611           memnode_worklist.append_if_missing(use);
4612         }



4613       } else {
4614         uint op = use->Opcode();
4615         if ((op == Op_StrCompressedCopy || op == Op_StrInflatedCopy) &&
4616             (use->in(MemNode::Memory) == n)) {
4617           // They overwrite memory edge corresponding to destination array,
4618           memnode_worklist.append_if_missing(use);
4619         } else if (!(op == Op_CmpP || op == Op_Conv2B ||
4620               op == Op_CastP2X ||
4621               op == Op_FastLock || op == Op_AryEq ||
4622               op == Op_StrComp || op == Op_CountPositives ||
4623               op == Op_StrCompressedCopy || op == Op_StrInflatedCopy ||
4624               op == Op_StrEquals || op == Op_VectorizedHashCode ||
4625               op == Op_StrIndexOf || op == Op_StrIndexOfChar ||
4626               op == Op_SubTypeCheck ||
4627               op == Op_ReinterpretS2HF ||
4628               BarrierSet::barrier_set()->barrier_set_c2()->is_gc_barrier_node(use))) {
4629           n->dump();
4630           use->dump();
4631           assert(false, "EA: missing allocation reference path");
4632         }
4633 #endif
4634       }
4635     }
4636 
4637   }
4638 
4639 #ifdef ASSERT
4640   if (VerifyReduceAllocationMerges) {
4641     for (uint i = 0; i < reducible_merges.size(); i++) {
4642       Node* phi = reducible_merges.at(i);
4643 
4644       if (!reduced_merges.member(phi)) {
4645         phi->dump(2);
4646         phi->dump(-2);

4710       // we don't need to do anything, but the users must be pushed
4711       n = n->as_MemBar()->proj_out_or_null(TypeFunc::Memory);
4712       if (n == nullptr) {
4713         continue;
4714       }
4715     } else if (n->is_CallLeaf()) {
4716       // Runtime calls with narrow memory input (no MergeMem node)
4717       // get the memory projection
4718       n = n->as_Call()->proj_out_or_null(TypeFunc::Memory);
4719       if (n == nullptr) {
4720         continue;
4721       }
4722     } else if (n->Opcode() == Op_StrInflatedCopy) {
4723       // Check direct uses of StrInflatedCopy.
4724       // It is memory type Node - no special SCMemProj node.
4725     } else if (n->Opcode() == Op_StrCompressedCopy ||
4726                n->Opcode() == Op_EncodeISOArray) {
4727       // get the memory projection
4728       n = n->find_out_with(Op_SCMemProj);
4729       assert(n != nullptr && n->Opcode() == Op_SCMemProj, "memory projection required");



4730     } else {
4731 #ifdef ASSERT
4732       if (!n->is_Mem()) {
4733         n->dump();
4734       }
4735       assert(n->is_Mem(), "memory node required.");
4736 #endif
4737       Node *addr = n->in(MemNode::Address);
4738       const Type *addr_t = igvn->type(addr);
4739       if (addr_t == Type::TOP) {
4740         continue;
4741       }
4742       assert (addr_t->isa_ptr() != nullptr, "pointer type required.");
4743       int alias_idx = _compile->get_alias_index(addr_t->is_ptr());
4744       assert ((uint)alias_idx < new_index_end, "wrong alias index");
4745       Node *mem = find_inst_mem(n->in(MemNode::Memory), alias_idx, orig_phis);
4746       if (_compile->failing()) {
4747         return;
4748       }
4749       if (mem != n->in(MemNode::Memory)) {

4754       if (n->is_Load()) {
4755         continue;  // don't push users
4756       } else if (n->is_LoadStore()) {
4757         // get the memory projection
4758         n = n->find_out_with(Op_SCMemProj);
4759         assert(n != nullptr && n->Opcode() == Op_SCMemProj, "memory projection required");
4760       }
4761     }
4762     // push user on appropriate worklist
4763     for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
4764       Node *use = n->fast_out(i);
4765       if (use->is_Phi() || use->is_ClearArray()) {
4766         memnode_worklist.append_if_missing(use);
4767       } else if (use->is_Mem() && use->in(MemNode::Memory) == n) {
4768         memnode_worklist.append_if_missing(use);
4769       } else if (use->is_MemBar() || use->is_CallLeaf()) {
4770         if (use->in(TypeFunc::Memory) == n) { // Ignore precedent edge
4771           memnode_worklist.append_if_missing(use);
4772         }
4773 #ifdef ASSERT
4774       } else if(use->is_Mem()) {
4775         assert(use->in(MemNode::Memory) != n, "EA: missing memory path");
4776       } else if (use->is_MergeMem()) {
4777         assert(mergemem_worklist.contains(use->as_MergeMem()), "EA: missing MergeMem node in the worklist");
4778       } else if (use->Opcode() == Op_EncodeISOArray) {
4779         if (use->in(MemNode::Memory) == n || use->in(3) == n) {
4780           // EncodeISOArray overwrites destination array
4781           memnode_worklist.append_if_missing(use);
4782         }




4783       } else {
4784         uint op = use->Opcode();
4785         if ((use->in(MemNode::Memory) == n) &&
4786             (op == Op_StrCompressedCopy || op == Op_StrInflatedCopy)) {
4787           // They overwrite memory edge corresponding to destination array,
4788           memnode_worklist.append_if_missing(use);
4789         } else if (!(BarrierSet::barrier_set()->barrier_set_c2()->is_gc_barrier_node(use) ||
4790               op == Op_AryEq || op == Op_StrComp || op == Op_CountPositives ||
4791               op == Op_StrCompressedCopy || op == Op_StrInflatedCopy || op == Op_VectorizedHashCode ||
4792               op == Op_StrEquals || op == Op_StrIndexOf || op == Op_StrIndexOfChar)) {
4793           n->dump();
4794           use->dump();
4795           assert(false, "EA: missing memory path");
4796         }
4797 #endif
4798       }
4799     }
4800   }
4801 
4802   //  Phase 3:  Process MergeMem nodes from mergemem_worklist.
4803   //            Walk each memory slice moving the first node encountered of each
4804   //            instance type to the input corresponding to its alias index.
4805   uint length = mergemem_worklist.length();
4806   for( uint next = 0; next < length; ++next ) {
4807     MergeMemNode* nmm = mergemem_worklist.at(next);
4808     assert(!visited.test_set(nmm->_idx), "should not be visited before");
4809     // Note: we don't want to use MergeMemStream here because we only want to
4810     // scan inputs which exist at the start, not ones we add during processing.
4811     // Note 2: MergeMem may already contains instance memory slices added
4812     // during find_inst_mem() call when memory nodes were processed above.

4873     if (_compile->live_nodes() >= _compile->max_node_limit() * 0.75) {
4874       if (_compile->do_reduce_allocation_merges()) {
4875         _compile->record_failure(C2Compiler::retry_no_reduce_allocation_merges());
4876       } else if (_invocation > 0) {
4877         _compile->record_failure(C2Compiler::retry_no_iterative_escape_analysis());
4878       } else {
4879         _compile->record_failure(C2Compiler::retry_no_escape_analysis());
4880       }
4881       return;
4882     }
4883 
4884     igvn->hash_insert(nmm);
4885     record_for_optimizer(nmm);
4886   }
4887 
4888   //  Phase 4:  Update the inputs of non-instance memory Phis and
4889   //            the Memory input of memnodes
4890   // First update the inputs of any non-instance Phi's from
4891   // which we split out an instance Phi.  Note we don't have
4892   // to recursively process Phi's encountered on the input memory
4893   // chains as is done in split_memory_phi() since they  will
4894   // also be processed here.
4895   for (int j = 0; j < orig_phis.length(); j++) {
4896     PhiNode *phi = orig_phis.at(j);
4897     int alias_idx = _compile->get_alias_index(phi->adr_type());
4898     igvn->hash_delete(phi);
4899     for (uint i = 1; i < phi->req(); i++) {
4900       Node *mem = phi->in(i);
4901       Node *new_mem = find_inst_mem(mem, alias_idx, orig_phis);
4902       if (_compile->failing()) {
4903         return;
4904       }
4905       if (mem != new_mem) {
4906         phi->set_req(i, new_mem);
4907       }
4908     }
4909     igvn->hash_insert(phi);
4910     record_for_optimizer(phi);
4911   }
4912 
4913   // Update the memory inputs of MemNodes with the value we computed

  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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "ci/bcEscapeAnalyzer.hpp"
  26 #include "compiler/compileLog.hpp"
  27 #include "gc/shared/barrierSet.hpp"
  28 #include "gc/shared/c2/barrierSetC2.hpp"
  29 #include "libadt/vectset.hpp"
  30 #include "memory/allocation.hpp"
  31 #include "memory/metaspace.hpp"
  32 #include "memory/resourceArea.hpp"
  33 #include "opto/c2compiler.hpp"
  34 #include "opto/arraycopynode.hpp"
  35 #include "opto/callnode.hpp"
  36 #include "opto/cfgnode.hpp"
  37 #include "opto/compile.hpp"
  38 #include "opto/escape.hpp"
  39 #include "opto/inlinetypenode.hpp"
  40 #include "opto/macro.hpp"
  41 #include "opto/locknode.hpp"
  42 #include "opto/phaseX.hpp"
  43 #include "opto/movenode.hpp"
  44 #include "opto/narrowptrnode.hpp"
  45 #include "opto/castnode.hpp"
  46 #include "opto/rootnode.hpp"
  47 #include "utilities/macros.hpp"
  48 
  49 ConnectionGraph::ConnectionGraph(Compile * C, PhaseIterGVN *igvn, int invocation) :
  50   // If ReduceAllocationMerges is enabled we might call split_through_phi during
  51   // split_unique_types and that will create additional nodes that need to be
  52   // pushed to the ConnectionGraph. The code below bumps the initial capacity of
  53   // _nodes by 10% to account for these additional nodes. If capacity is exceeded
  54   // the array will be reallocated.
  55   _nodes(C->comp_arena(), C->do_reduce_allocation_merges() ? C->unique()*1.10 : C->unique(), C->unique(), nullptr),
  56   _in_worklist(C->comp_arena()),
  57   _next_pidx(0),
  58   _collecting(true),
  59   _verify(false),

 148   GrowableArray<SafePointNode*>  sfn_worklist;
 149   GrowableArray<MergeMemNode*>   mergemem_worklist;
 150   DEBUG_ONLY( GrowableArray<Node*> addp_worklist; )
 151 
 152   { Compile::TracePhase tp(Phase::_t_connectionGraph);
 153 
 154   // 1. Populate Connection Graph (CG) with PointsTo nodes.
 155   ideal_nodes.map(C->live_nodes(), nullptr);  // preallocate space
 156   // Initialize worklist
 157   if (C->root() != nullptr) {
 158     ideal_nodes.push(C->root());
 159   }
 160   // Processed ideal nodes are unique on ideal_nodes list
 161   // but several ideal nodes are mapped to the phantom_obj.
 162   // To avoid duplicated entries on the following worklists
 163   // add the phantom_obj only once to them.
 164   ptnodes_worklist.append(phantom_obj);
 165   java_objects_worklist.append(phantom_obj);
 166   for( uint next = 0; next < ideal_nodes.size(); ++next ) {
 167     Node* n = ideal_nodes.at(next);
 168     if ((n->Opcode() == Op_LoadX || n->Opcode() == Op_StoreX) &&
 169         !n->in(MemNode::Address)->is_AddP() &&
 170         _igvn->type(n->in(MemNode::Address))->isa_oopptr()) {
 171       // Load/Store at mark work address is at offset 0 so has no AddP which confuses EA
 172       Node* addp = new AddPNode(n->in(MemNode::Address), n->in(MemNode::Address), _igvn->MakeConX(0));
 173       _igvn->register_new_node_with_optimizer(addp);
 174       _igvn->replace_input_of(n, MemNode::Address, addp);
 175       ideal_nodes.push(addp);
 176       _nodes.at_put_grow(addp->_idx, nullptr, nullptr);
 177     }
 178     // Create PointsTo nodes and add them to Connection Graph. Called
 179     // only once per ideal node since ideal_nodes is Unique_Node list.
 180     add_node_to_connection_graph(n, &delayed_worklist);
 181     PointsToNode* ptn = ptnode_adr(n->_idx);
 182     if (ptn != nullptr && ptn != phantom_obj) {
 183       ptnodes_worklist.append(ptn);
 184       if (ptn->is_JavaObject()) {
 185         java_objects_worklist.append(ptn->as_JavaObject());
 186         if ((n->is_Allocate() || n->is_CallStaticJava()) &&
 187             (ptn->escape_state() < PointsToNode::GlobalEscape)) {
 188           // Only allocations and java static calls results are interesting.
 189           non_escaped_allocs_worklist.append(ptn->as_JavaObject());
 190         }
 191       } else if (ptn->is_Field() && ptn->as_Field()->is_oop()) {
 192         oop_fields_worklist.append(ptn->as_Field());
 193       }
 194     }
 195     // Collect some interesting nodes for further use.
 196     switch (n->Opcode()) {
 197       case Op_MergeMem:

1247 
1248     // The next two inputs are:
1249     //  (1) A copy of the original pointer to NSR objects.
1250     //  (2) A selector, used to decide if we need to rematerialize an object
1251     //      or use the pointer to a NSR object.
1252     // See more details of these fields in the declaration of SafePointScalarMergeNode
1253     sfpt->add_req(nsr_merge_pointer);
1254     sfpt->add_req(selector);
1255 
1256     for (uint i = 1; i < ophi->req(); i++) {
1257       Node* base = ophi->in(i);
1258       JavaObjectNode* ptn = unique_java_object(base);
1259 
1260       // If the base is not scalar replaceable we don't need to register information about
1261       // it at this time.
1262       if (ptn == nullptr || !ptn->scalar_replaceable()) {
1263         continue;
1264       }
1265 
1266       AllocateNode* alloc = ptn->ideal_node()->as_Allocate();
1267       Unique_Node_List value_worklist;
1268 #ifdef ASSERT
1269       const Type* res_type = alloc->result_cast()->bottom_type();
1270       if (res_type->is_inlinetypeptr() && !Compile::current()->has_circular_inline_type()) {
1271         PhiNode* phi = ophi->as_Phi();
1272         assert(!ophi->as_Phi()->can_push_inline_types_down(_igvn), "missed earlier scalarization opportunity");
1273       }
1274 #endif
1275       SafePointScalarObjectNode* sobj = mexp.create_scalarized_object_description(alloc, sfpt, &value_worklist);
1276       if (sobj == nullptr) {
1277         _compile->record_failure(C2Compiler::retry_no_reduce_allocation_merges());
1278         return false;
1279       }
1280 
1281       // Now make a pass over the debug information replacing any references
1282       // to the allocated object with "sobj"
1283       Node* ccpp = alloc->result_cast();
1284       sfpt->replace_edges_in_range(ccpp, sobj, debug_start, jvms->debug_end(), _igvn);
1285 
1286       // Register the scalarized object as a candidate for reallocation
1287       smerge->add_req(sobj);
1288 
1289       // Scalarize inline types that were added to the safepoint.
1290       // Don't allow linking a constant oop (if available) for flat array elements
1291       // because Deoptimization::reassign_flat_array_elements needs field values.
1292       const bool allow_oop = !merge_t->is_flat();
1293       for (uint j = 0; j < value_worklist.size(); ++j) {
1294         InlineTypeNode* vt = value_worklist.at(j)->as_InlineType();
1295         vt->make_scalar_in_safepoints(_igvn, allow_oop);
1296       }
1297     }
1298 
1299     // Replaces debug information references to "original_sfpt_parent" in "sfpt" with references to "smerge"
1300     sfpt->replace_edges_in_range(original_sfpt_parent, smerge, debug_start, jvms->debug_end(), _igvn);
1301 
1302     // The call to 'replace_edges_in_range' above might have removed the
1303     // reference to ophi that we need at _merge_pointer_idx. The line below make
1304     // sure the reference is maintained.
1305     sfpt->set_req(smerge->merge_pointer_idx(jvms), nsr_merge_pointer);
1306     _igvn->_worklist.push(sfpt);
1307   }
1308 
1309   return true;
1310 }
1311 
1312 void ConnectionGraph::reduce_phi(PhiNode* ophi, GrowableArray<Node *>  &alloc_worklist, GrowableArray<Node *>  &memnode_worklist) {
1313   bool delay = _igvn->delay_transform();
1314   _igvn->set_delay_transform(true);
1315   _igvn->hash_delete(ophi);
1316 

1475   return false;
1476 }
1477 
1478 // Returns true if at least one of the arguments to the call is an object
1479 // that does not escape globally.
1480 bool ConnectionGraph::has_arg_escape(CallJavaNode* call) {
1481   if (call->method() != nullptr) {
1482     uint max_idx = TypeFunc::Parms + call->method()->arg_size();
1483     for (uint idx = TypeFunc::Parms; idx < max_idx; idx++) {
1484       Node* p = call->in(idx);
1485       if (not_global_escape(p)) {
1486         return true;
1487       }
1488     }
1489   } else {
1490     const char* name = call->as_CallStaticJava()->_name;
1491     assert(name != nullptr, "no name");
1492     // no arg escapes through uncommon traps
1493     if (strcmp(name, "uncommon_trap") != 0) {
1494       // process_call_arguments() assumes that all arguments escape globally
1495       const TypeTuple* d = call->tf()->domain_sig();
1496       for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
1497         const Type* at = d->field_at(i);
1498         if (at->isa_oopptr() != nullptr) {
1499           return true;
1500         }
1501       }
1502     }
1503   }
1504   return false;
1505 }
1506 
1507 
1508 
1509 // Utility function for nodes that load an object
1510 void ConnectionGraph::add_objload_to_connection_graph(Node *n, Unique_Node_List *delayed_worklist) {
1511   // Using isa_ptr() instead of isa_oopptr() for LoadP and Phi because
1512   // ThreadLocal has RawPtr type.
1513   const Type* t = _igvn->type(n);
1514   if (t->make_ptr() != nullptr) {
1515     Node* adr = n->in(MemNode::Address);

1549       // first IGVN optimization when escape information is still available.
1550       record_for_optimizer(n);
1551     } else if (n->is_Allocate()) {
1552       add_call_node(n->as_Call());
1553       record_for_optimizer(n);
1554     } else {
1555       if (n->is_CallStaticJava()) {
1556         const char* name = n->as_CallStaticJava()->_name;
1557         if (name != nullptr && strcmp(name, "uncommon_trap") == 0) {
1558           return; // Skip uncommon traps
1559         }
1560       }
1561       // Don't mark as processed since call's arguments have to be processed.
1562       delayed_worklist->push(n);
1563       // Check if a call returns an object.
1564       if ((n->as_Call()->returns_pointer() &&
1565            n->as_Call()->proj_out_or_null(TypeFunc::Parms) != nullptr) ||
1566           (n->is_CallStaticJava() &&
1567            n->as_CallStaticJava()->is_boxing_method())) {
1568         add_call_node(n->as_Call());
1569       } else if (n->as_Call()->tf()->returns_inline_type_as_fields()) {
1570         bool returns_oop = false;
1571         for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax && !returns_oop; i++) {
1572           ProjNode* pn = n->fast_out(i)->as_Proj();
1573           if (pn->_con >= TypeFunc::Parms && pn->bottom_type()->isa_ptr()) {
1574             returns_oop = true;
1575           }
1576         }
1577         if (returns_oop) {
1578           add_call_node(n->as_Call());
1579         }
1580       }
1581     }
1582     return;
1583   }
1584   // Put this check here to process call arguments since some call nodes
1585   // point to phantom_obj.
1586   if (n_ptn == phantom_obj || n_ptn == null_obj) {
1587     return; // Skip predefined nodes.
1588   }
1589   switch (opcode) {
1590     case Op_AddP: {
1591       Node* base = get_addp_base(n);
1592       PointsToNode* ptn_base = ptnode_adr(base->_idx);
1593       // Field nodes are created for all field types. They are used in
1594       // adjust_scalar_replaceable_state() and split_unique_types().
1595       // Note, non-oop fields will have only base edges in Connection
1596       // Graph because such fields are not used for oop loads and stores.
1597       int offset = address_offset(n, igvn);
1598       add_field(n, PointsToNode::NoEscape, offset);
1599       if (ptn_base == nullptr) {
1600         delayed_worklist->push(n); // Process it later.
1601       } else {
1602         n_ptn = ptnode_adr(n_idx);
1603         add_base(n_ptn->as_Field(), ptn_base);
1604       }
1605       break;
1606     }
1607     case Op_CastX2P:
1608     case Op_CastI2N: {
1609       map_ideal_node(n, phantom_obj);
1610       break;
1611     }
1612     case Op_InlineType:
1613     case Op_CastPP:
1614     case Op_CheckCastPP:
1615     case Op_EncodeP:
1616     case Op_DecodeN:
1617     case Op_EncodePKlass:
1618     case Op_DecodeNKlass: {
1619       add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(1), delayed_worklist);
1620       break;
1621     }
1622     case Op_CMoveP: {
1623       add_local_var(n, PointsToNode::NoEscape);
1624       // Do not add edges during first iteration because some could be
1625       // not defined yet.
1626       delayed_worklist->push(n);
1627       break;
1628     }
1629     case Op_ConP:
1630     case Op_ConN:
1631     case Op_ConNKlass: {
1632       // assume all oop constants globally escape except for null

1664     case Op_PartialSubtypeCheck: {
1665       // Produces Null or notNull and is used in only in CmpP so
1666       // phantom_obj could be used.
1667       map_ideal_node(n, phantom_obj); // Result is unknown
1668       break;
1669     }
1670     case Op_Phi: {
1671       // Using isa_ptr() instead of isa_oopptr() for LoadP and Phi because
1672       // ThreadLocal has RawPtr type.
1673       const Type* t = n->as_Phi()->type();
1674       if (t->make_ptr() != nullptr) {
1675         add_local_var(n, PointsToNode::NoEscape);
1676         // Do not add edges during first iteration because some could be
1677         // not defined yet.
1678         delayed_worklist->push(n);
1679       }
1680       break;
1681     }
1682     case Op_Proj: {
1683       // we are only interested in the oop result projection from a call
1684       if (n->as_Proj()->_con >= TypeFunc::Parms && n->in(0)->is_Call() &&
1685           (n->in(0)->as_Call()->returns_pointer() || n->bottom_type()->isa_ptr())) {
1686         assert((n->as_Proj()->_con == TypeFunc::Parms && n->in(0)->as_Call()->returns_pointer()) ||
1687                n->in(0)->as_Call()->tf()->returns_inline_type_as_fields(), "what kind of oop return is it?");
1688         add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(0), delayed_worklist);
1689       }
1690       break;
1691     }
1692     case Op_Rethrow: // Exception object escapes
1693     case Op_Return: {
1694       if (n->req() > TypeFunc::Parms &&
1695           igvn->type(n->in(TypeFunc::Parms))->isa_oopptr()) {
1696         // Treat Return value as LocalVar with GlobalEscape escape state.
1697         add_local_var_and_edge(n, PointsToNode::GlobalEscape, n->in(TypeFunc::Parms), delayed_worklist);
1698       }
1699       break;
1700     }
1701     case Op_CompareAndExchangeP:
1702     case Op_CompareAndExchangeN:
1703     case Op_GetAndSetP:
1704     case Op_GetAndSetN: {
1705       add_objload_to_connection_graph(n, delayed_worklist);
1706       // fall-through
1707     }

1769   if (n->is_Call()) {
1770     process_call_arguments(n->as_Call());
1771     return;
1772   }
1773   assert(n->is_Store() || n->is_LoadStore() ||
1774          ((n_ptn != nullptr) && (n_ptn->ideal_node() != nullptr)),
1775          "node should be registered already");
1776   int opcode = n->Opcode();
1777   bool gc_handled = BarrierSet::barrier_set()->barrier_set_c2()->escape_add_final_edges(this, _igvn, n, opcode);
1778   if (gc_handled) {
1779     return; // Ignore node if already handled by GC.
1780   }
1781   switch (opcode) {
1782     case Op_AddP: {
1783       Node* base = get_addp_base(n);
1784       PointsToNode* ptn_base = ptnode_adr(base->_idx);
1785       assert(ptn_base != nullptr, "field's base should be registered");
1786       add_base(n_ptn->as_Field(), ptn_base);
1787       break;
1788     }
1789     case Op_InlineType:
1790     case Op_CastPP:
1791     case Op_CheckCastPP:
1792     case Op_EncodeP:
1793     case Op_DecodeN:
1794     case Op_EncodePKlass:
1795     case Op_DecodeNKlass: {
1796       add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(1), nullptr);
1797       break;
1798     }
1799     case Op_CMoveP: {
1800       for (uint i = CMoveNode::IfFalse; i < n->req(); i++) {
1801         Node* in = n->in(i);
1802         if (in == nullptr) {
1803           continue;  // ignore null
1804         }
1805         Node* uncast_in = in->uncast();
1806         if (uncast_in->is_top() || uncast_in == n) {
1807           continue;  // ignore top or inputs which go back this node
1808         }
1809         PointsToNode* ptn = ptnode_adr(in->_idx);

1824       // Using isa_ptr() instead of isa_oopptr() for LoadP and Phi because
1825       // ThreadLocal has RawPtr type.
1826       assert(n->as_Phi()->type()->make_ptr() != nullptr, "Unexpected node type");
1827       for (uint i = 1; i < n->req(); i++) {
1828         Node* in = n->in(i);
1829         if (in == nullptr) {
1830           continue;  // ignore null
1831         }
1832         Node* uncast_in = in->uncast();
1833         if (uncast_in->is_top() || uncast_in == n) {
1834           continue;  // ignore top or inputs which go back this node
1835         }
1836         PointsToNode* ptn = ptnode_adr(in->_idx);
1837         assert(ptn != nullptr, "node should be registered");
1838         add_edge(n_ptn, ptn);
1839       }
1840       break;
1841     }
1842     case Op_Proj: {
1843       // we are only interested in the oop result projection from a call
1844       assert((n->as_Proj()->_con == TypeFunc::Parms && n->in(0)->as_Call()->returns_pointer()) ||
1845              n->in(0)->as_Call()->tf()->returns_inline_type_as_fields(), "what kind of oop return is it?");
1846       add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(0), nullptr);
1847       break;
1848     }
1849     case Op_Rethrow: // Exception object escapes
1850     case Op_Return: {
1851       assert(n->req() > TypeFunc::Parms && _igvn->type(n->in(TypeFunc::Parms))->isa_oopptr(),
1852              "Unexpected node type");
1853       // Treat Return value as LocalVar with GlobalEscape escape state.
1854       add_local_var_and_edge(n, PointsToNode::GlobalEscape, n->in(TypeFunc::Parms), nullptr);
1855       break;
1856     }
1857     case Op_CompareAndExchangeP:
1858     case Op_CompareAndExchangeN:
1859     case Op_GetAndSetP:
1860     case Op_GetAndSetN:{
1861       assert(_igvn->type(n)->make_ptr() != nullptr, "Unexpected node type");
1862       add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(MemNode::Address), nullptr);
1863       // fall-through
1864     }
1865     case Op_CompareAndSwapP:

2001     PointsToNode* ptn = ptnode_adr(val->_idx);
2002     assert(ptn != nullptr, "node should be registered");
2003     set_escape_state(ptn, PointsToNode::GlobalEscape NOT_PRODUCT(COMMA "stored at raw address"));
2004     // Add edge to object for unsafe access with offset.
2005     PointsToNode* adr_ptn = ptnode_adr(adr->_idx);
2006     assert(adr_ptn != nullptr, "node should be registered");
2007     if (adr_ptn->is_Field()) {
2008       assert(adr_ptn->as_Field()->is_oop(), "should be oop field");
2009       add_edge(adr_ptn, ptn);
2010     }
2011     return true;
2012   }
2013 #ifdef ASSERT
2014   n->dump(1);
2015   assert(false, "not unsafe");
2016 #endif
2017   return false;
2018 }
2019 
2020 void ConnectionGraph::add_call_node(CallNode* call) {
2021   assert(call->returns_pointer() || call->tf()->returns_inline_type_as_fields(), "only for call which returns pointer");
2022   uint call_idx = call->_idx;
2023   if (call->is_Allocate()) {
2024     Node* k = call->in(AllocateNode::KlassNode);
2025     const TypeKlassPtr* kt = k->bottom_type()->isa_klassptr();
2026     assert(kt != nullptr, "TypeKlassPtr  required.");
2027     PointsToNode::EscapeState es = PointsToNode::NoEscape;
2028     bool scalar_replaceable = true;
2029     NOT_PRODUCT(const char* nsr_reason = "");
2030     if (call->is_AllocateArray()) {
2031       if (!kt->isa_aryklassptr()) { // StressReflectiveCode
2032         es = PointsToNode::GlobalEscape;
2033       } else {
2034         int length = call->in(AllocateNode::ALength)->find_int_con(-1);
2035         if (length < 0) {
2036           // Not scalar replaceable if the length is not constant.
2037           scalar_replaceable = false;
2038           NOT_PRODUCT(nsr_reason = "has a non-constant length");
2039         } else if (length > EliminateAllocationArraySizeLimit) {
2040           // Not scalar replaceable if the length is too big.
2041           scalar_replaceable = false;

2077     //
2078     //    - all oop arguments are escaping globally;
2079     //
2080     // 2. CallStaticJavaNode (execute bytecode analysis if possible):
2081     //
2082     //    - the same as CallDynamicJavaNode if can't do bytecode analysis;
2083     //
2084     //    - mapped to GlobalEscape JavaObject node if unknown oop is returned;
2085     //    - mapped to NoEscape JavaObject node if non-escaping object allocated
2086     //      during call is returned;
2087     //    - mapped to ArgEscape LocalVar node pointed to object arguments
2088     //      which are returned and does not escape during call;
2089     //
2090     //    - oop arguments escaping status is defined by bytecode analysis;
2091     //
2092     // For a static call, we know exactly what method is being called.
2093     // Use bytecode estimator to record whether the call's return value escapes.
2094     ciMethod* meth = call->as_CallJava()->method();
2095     if (meth == nullptr) {
2096       const char* name = call->as_CallStaticJava()->_name;
2097       assert(strncmp(name, "C2 Runtime multianewarray", 25) == 0 ||
2098              strncmp(name, "C2 Runtime load_unknown_inline", 30) == 0, "TODO: add failed case check");
2099       // Returns a newly allocated non-escaped object.
2100       add_java_object(call, PointsToNode::NoEscape);
2101       set_not_scalar_replaceable(ptnode_adr(call_idx) NOT_PRODUCT(COMMA "is result of multinewarray"));
2102     } else if (meth->is_boxing_method()) {
2103       // Returns boxing object
2104       PointsToNode::EscapeState es;
2105       vmIntrinsics::ID intr = meth->intrinsic_id();
2106       if (intr == vmIntrinsics::_floatValue || intr == vmIntrinsics::_doubleValue) {
2107         // It does not escape if object is always allocated.
2108         es = PointsToNode::NoEscape;
2109       } else {
2110         // It escapes globally if object could be loaded from cache.
2111         es = PointsToNode::GlobalEscape;
2112       }
2113       add_java_object(call, es);
2114       if (es == PointsToNode::GlobalEscape) {
2115         set_not_scalar_replaceable(ptnode_adr(call->_idx) NOT_PRODUCT(COMMA "object can be loaded from boxing cache"));
2116       }
2117     } else {
2118       BCEscapeAnalyzer* call_analyzer = meth->get_bcea();
2119       call_analyzer->copy_dependencies(_compile->dependencies());
2120       if (call_analyzer->is_return_allocated()) {
2121         // Returns a newly allocated non-escaped object, simply
2122         // update dependency information.
2123         // Mark it as NoEscape so that objects referenced by
2124         // it's fields will be marked as NoEscape at least.
2125         add_java_object(call, PointsToNode::NoEscape);
2126         set_not_scalar_replaceable(ptnode_adr(call_idx) NOT_PRODUCT(COMMA "is result of call"));
2127       } else {
2128         // Determine whether any arguments are returned.
2129         const TypeTuple* d = call->tf()->domain_cc();
2130         bool ret_arg = false;
2131         for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
2132           if (d->field_at(i)->isa_ptr() != nullptr &&
2133               call_analyzer->is_arg_returned(i - TypeFunc::Parms)) {
2134             ret_arg = true;
2135             break;
2136           }
2137         }
2138         if (ret_arg) {
2139           add_local_var(call, PointsToNode::ArgEscape);
2140         } else {
2141           // Returns unknown object.
2142           map_ideal_node(call, phantom_obj);
2143         }
2144       }
2145     }
2146   } else {
2147     // An other type of call, assume the worst case:
2148     // returned value is unknown and globally escapes.
2149     assert(call->Opcode() == Op_CallDynamicJava, "add failed case check");

2157 #ifdef ASSERT
2158     case Op_Allocate:
2159     case Op_AllocateArray:
2160     case Op_Lock:
2161     case Op_Unlock:
2162       assert(false, "should be done already");
2163       break;
2164 #endif
2165     case Op_ArrayCopy:
2166     case Op_CallLeafNoFP:
2167       // Most array copies are ArrayCopy nodes at this point but there
2168       // are still a few direct calls to the copy subroutines (See
2169       // PhaseStringOpts::copy_string())
2170       is_arraycopy = (call->Opcode() == Op_ArrayCopy) ||
2171         call->as_CallLeaf()->is_call_to_arraycopystub();
2172       // fall through
2173     case Op_CallLeafVector:
2174     case Op_CallLeaf: {
2175       // Stub calls, objects do not escape but they are not scale replaceable.
2176       // Adjust escape state for outgoing arguments.
2177       const TypeTuple * d = call->tf()->domain_sig();
2178       bool src_has_oops = false;
2179       for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
2180         const Type* at = d->field_at(i);
2181         Node *arg = call->in(i);
2182         if (arg == nullptr) {
2183           continue;
2184         }
2185         const Type *aat = _igvn->type(arg);
2186         if (arg->is_top() || !at->isa_ptr() || !aat->isa_ptr()) {
2187           continue;
2188         }
2189         if (arg->is_AddP()) {
2190           //
2191           // The inline_native_clone() case when the arraycopy stub is called
2192           // after the allocation before Initialize and CheckCastPP nodes.
2193           // Or normal arraycopy for object arrays case.
2194           //
2195           // Set AddP's base (Allocate) as not scalar replaceable since
2196           // pointer to the base (with offset) is passed as argument.
2197           //
2198           arg = get_addp_base(arg);
2199         }
2200         PointsToNode* arg_ptn = ptnode_adr(arg->_idx);
2201         assert(arg_ptn != nullptr, "should be registered");
2202         PointsToNode::EscapeState arg_esc = arg_ptn->escape_state();
2203         if (is_arraycopy || arg_esc < PointsToNode::ArgEscape) {
2204           assert(aat == Type::TOP || aat == TypePtr::NULL_PTR ||
2205                  aat->isa_ptr() != nullptr, "expecting an Ptr");
2206           bool arg_has_oops = aat->isa_oopptr() &&
2207                               (aat->isa_instptr() ||
2208                                (aat->isa_aryptr() && (aat->isa_aryptr()->elem() == Type::BOTTOM || aat->isa_aryptr()->elem()->make_oopptr() != nullptr)) ||
2209                                (aat->isa_aryptr() && aat->isa_aryptr()->elem() != nullptr &&
2210                                                                aat->isa_aryptr()->is_flat() &&
2211                                                                aat->isa_aryptr()->elem()->inline_klass()->contains_oops()));
2212           if (i == TypeFunc::Parms) {
2213             src_has_oops = arg_has_oops;
2214           }
2215           //
2216           // src or dst could be j.l.Object when other is basic type array:
2217           //
2218           //   arraycopy(char[],0,Object*,0,size);
2219           //   arraycopy(Object*,0,char[],0,size);
2220           //
2221           // Don't add edges in such cases.
2222           //
2223           bool arg_is_arraycopy_dest = src_has_oops && is_arraycopy &&
2224                                        arg_has_oops && (i > TypeFunc::Parms);
2225 #ifdef ASSERT
2226           if (!(is_arraycopy ||
2227                 BarrierSet::barrier_set()->barrier_set_c2()->is_gc_barrier_node(call) ||
2228                 (call->as_CallLeaf()->_name != nullptr &&
2229                  (strcmp(call->as_CallLeaf()->_name, "updateBytesCRC32") == 0 ||
2230                   strcmp(call->as_CallLeaf()->_name, "updateBytesCRC32C") == 0 ||
2231                   strcmp(call->as_CallLeaf()->_name, "updateBytesAdler32") == 0 ||

2255                   strcmp(call->as_CallLeaf()->_name, "dilithiumMontMulByConstant") == 0 ||
2256                   strcmp(call->as_CallLeaf()->_name, "dilithiumDecomposePoly") == 0 ||
2257                   strcmp(call->as_CallLeaf()->_name, "encodeBlock") == 0 ||
2258                   strcmp(call->as_CallLeaf()->_name, "decodeBlock") == 0 ||
2259                   strcmp(call->as_CallLeaf()->_name, "md5_implCompress") == 0 ||
2260                   strcmp(call->as_CallLeaf()->_name, "md5_implCompressMB") == 0 ||
2261                   strcmp(call->as_CallLeaf()->_name, "sha1_implCompress") == 0 ||
2262                   strcmp(call->as_CallLeaf()->_name, "sha1_implCompressMB") == 0 ||
2263                   strcmp(call->as_CallLeaf()->_name, "sha256_implCompress") == 0 ||
2264                   strcmp(call->as_CallLeaf()->_name, "sha256_implCompressMB") == 0 ||
2265                   strcmp(call->as_CallLeaf()->_name, "sha512_implCompress") == 0 ||
2266                   strcmp(call->as_CallLeaf()->_name, "sha512_implCompressMB") == 0 ||
2267                   strcmp(call->as_CallLeaf()->_name, "sha3_implCompress") == 0 ||
2268                   strcmp(call->as_CallLeaf()->_name, "double_keccak") == 0 ||
2269                   strcmp(call->as_CallLeaf()->_name, "sha3_implCompressMB") == 0 ||
2270                   strcmp(call->as_CallLeaf()->_name, "multiplyToLen") == 0 ||
2271                   strcmp(call->as_CallLeaf()->_name, "squareToLen") == 0 ||
2272                   strcmp(call->as_CallLeaf()->_name, "mulAdd") == 0 ||
2273                   strcmp(call->as_CallLeaf()->_name, "montgomery_multiply") == 0 ||
2274                   strcmp(call->as_CallLeaf()->_name, "montgomery_square") == 0 ||
2275                   strcmp(call->as_CallLeaf()->_name, "vectorizedMismatch") == 0 ||
2276                   strcmp(call->as_CallLeaf()->_name, "load_unknown_inline") == 0 ||
2277                   strcmp(call->as_CallLeaf()->_name, "store_unknown_inline") == 0 ||
2278                   strcmp(call->as_CallLeaf()->_name, "bigIntegerRightShiftWorker") == 0 ||
2279                   strcmp(call->as_CallLeaf()->_name, "bigIntegerLeftShiftWorker") == 0 ||
2280                   strcmp(call->as_CallLeaf()->_name, "vectorizedMismatch") == 0 ||
2281                   strcmp(call->as_CallLeaf()->_name, "stringIndexOf") == 0 ||
2282                   strcmp(call->as_CallLeaf()->_name, "arraysort_stub") == 0 ||
2283                   strcmp(call->as_CallLeaf()->_name, "array_partition_stub") == 0 ||
2284                   strcmp(call->as_CallLeaf()->_name, "get_class_id_intrinsic") == 0 ||
2285                   strcmp(call->as_CallLeaf()->_name, "unsafe_setmemory") == 0)
2286                  ))) {
2287             call->dump();
2288             fatal("EA unexpected CallLeaf %s", call->as_CallLeaf()->_name);
2289           }
2290 #endif
2291           // Always process arraycopy's destination object since
2292           // we need to add all possible edges to references in
2293           // source object.
2294           if (arg_esc >= PointsToNode::ArgEscape &&
2295               !arg_is_arraycopy_dest) {
2296             continue;
2297           }

2324           }
2325         }
2326       }
2327       break;
2328     }
2329     case Op_CallStaticJava: {
2330       // For a static call, we know exactly what method is being called.
2331       // Use bytecode estimator to record the call's escape affects
2332 #ifdef ASSERT
2333       const char* name = call->as_CallStaticJava()->_name;
2334       assert((name == nullptr || strcmp(name, "uncommon_trap") != 0), "normal calls only");
2335 #endif
2336       ciMethod* meth = call->as_CallJava()->method();
2337       if ((meth != nullptr) && meth->is_boxing_method()) {
2338         break; // Boxing methods do not modify any oops.
2339       }
2340       BCEscapeAnalyzer* call_analyzer = (meth !=nullptr) ? meth->get_bcea() : nullptr;
2341       // fall-through if not a Java method or no analyzer information
2342       if (call_analyzer != nullptr) {
2343         PointsToNode* call_ptn = ptnode_adr(call->_idx);
2344         const TypeTuple* d = call->tf()->domain_cc();
2345         for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
2346           const Type* at = d->field_at(i);
2347           int k = i - TypeFunc::Parms;
2348           Node* arg = call->in(i);
2349           PointsToNode* arg_ptn = ptnode_adr(arg->_idx);
2350           if (at->isa_ptr() != nullptr &&
2351               call_analyzer->is_arg_returned(k)) {
2352             // The call returns arguments.
2353             if (call_ptn != nullptr) { // Is call's result used?
2354               assert(call_ptn->is_LocalVar(), "node should be registered");
2355               assert(arg_ptn != nullptr, "node should be registered");
2356               add_edge(call_ptn, arg_ptn);
2357             }
2358           }
2359           if (at->isa_oopptr() != nullptr &&
2360               arg_ptn->escape_state() < PointsToNode::GlobalEscape) {
2361             if (!call_analyzer->is_arg_stack(k)) {
2362               // The argument global escapes
2363               set_escape_state(arg_ptn, PointsToNode::GlobalEscape NOT_PRODUCT(COMMA trace_arg_escape_message(call)));
2364             } else {

2368                 set_fields_escape_state(arg_ptn, PointsToNode::GlobalEscape NOT_PRODUCT(COMMA trace_arg_escape_message(call)));
2369               }
2370             }
2371           }
2372         }
2373         if (call_ptn != nullptr && call_ptn->is_LocalVar()) {
2374           // The call returns arguments.
2375           assert(call_ptn->edge_count() > 0, "sanity");
2376           if (!call_analyzer->is_return_local()) {
2377             // Returns also unknown object.
2378             add_edge(call_ptn, phantom_obj);
2379           }
2380         }
2381         break;
2382       }
2383     }
2384     default: {
2385       // Fall-through here if not a Java method or no analyzer information
2386       // or some other type of call, assume the worst case: all arguments
2387       // globally escape.
2388       const TypeTuple* d = call->tf()->domain_cc();
2389       for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
2390         const Type* at = d->field_at(i);
2391         if (at->isa_oopptr() != nullptr) {
2392           Node* arg = call->in(i);
2393           if (arg->is_AddP()) {
2394             arg = get_addp_base(arg);
2395           }
2396           assert(ptnode_adr(arg->_idx) != nullptr, "should be defined already");
2397           set_escape_state(ptnode_adr(arg->_idx), PointsToNode::GlobalEscape NOT_PRODUCT(COMMA trace_arg_escape_message(call)));
2398         }
2399       }
2400     }
2401   }
2402 }
2403 
2404 
2405 // Finish Graph construction.
2406 bool ConnectionGraph::complete_connection_graph(
2407                          GrowableArray<PointsToNode*>&   ptnodes_worklist,
2408                          GrowableArray<JavaObjectNode*>& non_escaped_allocs_worklist,

2781     PointsToNode* base = i.get();
2782     if (base->is_JavaObject()) {
2783       // Skip Allocate's fields which will be processed later.
2784       if (base->ideal_node()->is_Allocate()) {
2785         return 0;
2786       }
2787       assert(base == null_obj, "only null ptr base expected here");
2788     }
2789   }
2790   if (add_edge(field, phantom_obj)) {
2791     // New edge was added
2792     new_edges++;
2793     add_field_uses_to_worklist(field);
2794   }
2795   return new_edges;
2796 }
2797 
2798 // Find fields initializing values for allocations.
2799 int ConnectionGraph::find_init_values_phantom(JavaObjectNode* pta) {
2800   assert(pta->escape_state() == PointsToNode::NoEscape, "Not escaped Allocate nodes only");
2801   PointsToNode* init_val = phantom_obj;
2802   Node* alloc = pta->ideal_node();
2803 
2804   // Do nothing for Allocate nodes since its fields values are
2805   // "known" unless they are initialized by arraycopy/clone.
2806   if (alloc->is_Allocate() && !pta->arraycopy_dst()) {
2807     if (alloc->as_Allocate()->in(AllocateNode::InitValue) != nullptr) {
2808       // Non-flat inline type arrays are initialized with
2809       // an init value instead of null. Handle them here.
2810       init_val = ptnode_adr(alloc->as_Allocate()->in(AllocateNode::InitValue)->_idx);
2811       assert(init_val != nullptr, "init value should be registered");
2812     } else {
2813       return 0;
2814     }
2815   }
2816   // Non-escaped allocation returned from Java or runtime call has unknown values in fields.
2817   assert(pta->arraycopy_dst() || alloc->is_CallStaticJava() || init_val != phantom_obj, "sanity");
2818 #ifdef ASSERT
2819   if (alloc->is_CallStaticJava() && alloc->as_CallStaticJava()->method() == nullptr) {
2820     const char* name = alloc->as_CallStaticJava()->_name;
2821     assert(strncmp(name, "C2 Runtime multianewarray", 25) == 0 ||
2822            strncmp(name, "C2 Runtime load_unknown_inline", 30) == 0, "sanity");
2823   }
2824 #endif
2825   // Non-escaped allocation returned from Java or runtime call have unknown values in fields.
2826   int new_edges = 0;
2827   for (EdgeIterator i(pta); i.has_next(); i.next()) {
2828     PointsToNode* field = i.get();
2829     if (field->is_Field() && field->as_Field()->is_oop()) {
2830       if (add_edge(field, init_val)) {
2831         // New edge was added
2832         new_edges++;
2833         add_field_uses_to_worklist(field->as_Field());
2834       }
2835     }
2836   }
2837   return new_edges;
2838 }
2839 
2840 // Find fields initializing values for allocations.
2841 int ConnectionGraph::find_init_values_null(JavaObjectNode* pta, PhaseValues* phase) {
2842   assert(pta->escape_state() == PointsToNode::NoEscape, "Not escaped Allocate nodes only");
2843   Node* alloc = pta->ideal_node();
2844   // Do nothing for Call nodes since its fields values are unknown.
2845   if (!alloc->is_Allocate() || alloc->as_Allocate()->in(AllocateNode::InitValue) != nullptr) {
2846     return 0;
2847   }
2848   InitializeNode* ini = alloc->as_Allocate()->initialization();
2849   bool visited_bottom_offset = false;
2850   GrowableArray<int> offsets_worklist;
2851   int new_edges = 0;
2852 
2853   // Check if an oop field's initializing value is recorded and add
2854   // a corresponding null if field's value if it is not recorded.
2855   // Connection Graph does not record a default initialization by null
2856   // captured by Initialize node.
2857   //
2858   for (EdgeIterator i(pta); i.has_next(); i.next()) {
2859     PointsToNode* field = i.get(); // Field (AddP)
2860     if (!field->is_Field() || !field->as_Field()->is_oop()) {
2861       continue; // Not oop field
2862     }
2863     int offset = field->as_Field()->offset();
2864     if (offset == Type::OffsetBot) {
2865       if (!visited_bottom_offset) {

2911               } else {
2912                 if (!val->is_LocalVar() || (val->edge_count() == 0)) {
2913                   tty->print_cr("----------init store has invalid value -----");
2914                   store->dump();
2915                   val->dump();
2916                   assert(val->is_LocalVar() && (val->edge_count() > 0), "should be processed already");
2917                 }
2918                 for (EdgeIterator j(val); j.has_next(); j.next()) {
2919                   PointsToNode* obj = j.get();
2920                   if (obj->is_JavaObject()) {
2921                     if (!field->points_to(obj->as_JavaObject())) {
2922                       missed_obj = obj;
2923                       break;
2924                     }
2925                   }
2926                 }
2927               }
2928               if (missed_obj != nullptr) {
2929                 tty->print_cr("----------field---------------------------------");
2930                 field->dump();
2931                 tty->print_cr("----------missed reference to object------------");
2932                 missed_obj->dump();
2933                 tty->print_cr("----------object referenced by init store-------");
2934                 store->dump();
2935                 val->dump();
2936                 assert(!field->points_to(missed_obj->as_JavaObject()), "missed JavaObject reference");
2937               }
2938             }
2939 #endif
2940           } else {
2941             // There could be initializing stores which follow allocation.
2942             // For example, a volatile field store is not collected
2943             // by Initialize node.
2944             //
2945             // Need to check for dependent loads to separate such stores from
2946             // stores which follow loads. For now, add initial value null so
2947             // that compare pointers optimization works correctly.
2948           }
2949         }
2950         if (value == nullptr) {
2951           // A field's initializing value was not recorded. Add null.
2952           if (add_edge(field, null_obj)) {
2953             // New edge was added

3269         assert(field->edge_count() > 0, "sanity");
3270       }
3271     }
3272   }
3273 }
3274 #endif
3275 
3276 // Optimize ideal graph.
3277 void ConnectionGraph::optimize_ideal_graph(GrowableArray<Node*>& ptr_cmp_worklist,
3278                                            GrowableArray<MemBarStoreStoreNode*>& storestore_worklist) {
3279   Compile* C = _compile;
3280   PhaseIterGVN* igvn = _igvn;
3281   if (EliminateLocks) {
3282     // Mark locks before changing ideal graph.
3283     int cnt = C->macro_count();
3284     for (int i = 0; i < cnt; i++) {
3285       Node *n = C->macro_node(i);
3286       if (n->is_AbstractLock()) { // Lock and Unlock nodes
3287         AbstractLockNode* alock = n->as_AbstractLock();
3288         if (!alock->is_non_esc_obj()) {
3289           const Type* obj_type = igvn->type(alock->obj_node());
3290           if (can_eliminate_lock(alock) && !obj_type->is_inlinetypeptr()) {
3291             assert(!alock->is_eliminated() || alock->is_coarsened(), "sanity");
3292             // The lock could be marked eliminated by lock coarsening
3293             // code during first IGVN before EA. Replace coarsened flag
3294             // to eliminate all associated locks/unlocks.
3295 #ifdef ASSERT
3296             alock->log_lock_optimization(C, "eliminate_lock_set_non_esc3");
3297 #endif
3298             alock->set_non_esc_obj();
3299           }
3300         }
3301       }
3302     }
3303   }
3304 
3305   if (OptimizePtrCompare) {
3306     for (int i = 0; i < ptr_cmp_worklist.length(); i++) {
3307       Node *n = ptr_cmp_worklist.at(i);
3308       assert(n->Opcode() == Op_CmpN || n->Opcode() == Op_CmpP, "must be");
3309       const TypeInt* tcmp = optimize_ptr_compare(n->in(1), n->in(2));
3310       if (tcmp->singleton()) {

3312 #ifndef PRODUCT
3313         if (PrintOptimizePtrCompare) {
3314           tty->print_cr("++++ Replaced: %d %s(%d,%d) --> %s", n->_idx, (n->Opcode() == Op_CmpP ? "CmpP" : "CmpN"), n->in(1)->_idx, n->in(2)->_idx, (tcmp == TypeInt::CC_EQ ? "EQ" : "NotEQ"));
3315           if (Verbose) {
3316             n->dump(1);
3317           }
3318         }
3319 #endif
3320         igvn->replace_node(n, cmp);
3321       }
3322     }
3323   }
3324 
3325   // For MemBarStoreStore nodes added in library_call.cpp, check
3326   // escape status of associated AllocateNode and optimize out
3327   // MemBarStoreStore node if the allocated object never escapes.
3328   for (int i = 0; i < storestore_worklist.length(); i++) {
3329     Node* storestore = storestore_worklist.at(i);
3330     Node* alloc = storestore->in(MemBarNode::Precedent)->in(0);
3331     if (alloc->is_Allocate() && not_global_escape(alloc)) {
3332       if (alloc->in(AllocateNode::InlineType) != nullptr) {
3333         // Non-escaping inline type buffer allocations don't require a membar
3334         storestore->as_MemBar()->remove(_igvn);
3335       } else {
3336         MemBarNode* mb = MemBarNode::make(C, Op_MemBarCPUOrder, Compile::AliasIdxBot);
3337         mb->init_req(TypeFunc::Memory,  storestore->in(TypeFunc::Memory));
3338         mb->init_req(TypeFunc::Control, storestore->in(TypeFunc::Control));
3339         igvn->register_new_node_with_optimizer(mb);
3340         igvn->replace_node(storestore, mb);
3341       }
3342     }
3343   }
3344 }
3345 
3346 // Optimize objects compare.
3347 const TypeInt* ConnectionGraph::optimize_ptr_compare(Node* left, Node* right) {
3348   assert(OptimizePtrCompare, "sanity");
3349   const TypeInt* EQ = TypeInt::CC_EQ; // [0] == ZERO
3350   const TypeInt* NE = TypeInt::CC_GT; // [1] == ONE
3351   const TypeInt* UNKNOWN = TypeInt::CC;    // [-1, 0,1]
3352 
3353   PointsToNode* ptn1 = ptnode_adr(left->_idx);
3354   PointsToNode* ptn2 = ptnode_adr(right->_idx);
3355   JavaObjectNode* jobj1 = unique_java_object(left);
3356   JavaObjectNode* jobj2 = unique_java_object(right);
3357 
3358   // The use of this method during allocation merge reduction may cause 'left'
3359   // or 'right' be something (e.g., a Phi) that isn't in the connection graph or
3360   // that doesn't reference an unique java object.
3361   if (ptn1 == nullptr || ptn2 == nullptr ||

3483   assert(!src->is_Field() && !dst->is_Field(), "only for JavaObject and LocalVar");
3484   assert((src != null_obj) && (dst != null_obj), "not for ConP null");
3485   PointsToNode* ptadr = _nodes.at(n->_idx);
3486   if (ptadr != nullptr) {
3487     assert(ptadr->is_Arraycopy() && ptadr->ideal_node() == n, "sanity");
3488     return;
3489   }
3490   Compile* C = _compile;
3491   ptadr = new (C->comp_arena()) ArraycopyNode(this, n, es);
3492   map_ideal_node(n, ptadr);
3493   // Add edge from arraycopy node to source object.
3494   (void)add_edge(ptadr, src);
3495   src->set_arraycopy_src();
3496   // Add edge from destination object to arraycopy node.
3497   (void)add_edge(dst, ptadr);
3498   dst->set_arraycopy_dst();
3499 }
3500 
3501 bool ConnectionGraph::is_oop_field(Node* n, int offset, bool* unsafe) {
3502   const Type* adr_type = n->as_AddP()->bottom_type();
3503   int field_offset = adr_type->isa_aryptr() ? adr_type->isa_aryptr()->field_offset().get() : Type::OffsetBot;
3504   BasicType bt = T_INT;
3505   if (offset == Type::OffsetBot && field_offset == Type::OffsetBot) {
3506     // Check only oop fields.
3507     if (!adr_type->isa_aryptr() ||
3508         adr_type->isa_aryptr()->elem() == Type::BOTTOM ||
3509         adr_type->isa_aryptr()->elem()->make_oopptr() != nullptr) {
3510       // OffsetBot is used to reference array's element. Ignore first AddP.
3511       if (find_second_addp(n, n->in(AddPNode::Base)) == nullptr) {
3512         bt = T_OBJECT;
3513       }
3514     }
3515   } else if (offset != oopDesc::klass_offset_in_bytes()) {
3516     if (adr_type->isa_instptr()) {
3517       ciField* field = _compile->alias_type(adr_type->is_ptr())->field();
3518       if (field != nullptr) {
3519         bt = field->layout_type();
3520       } else {
3521         // Check for unsafe oop field access
3522         if (n->has_out_with(Op_StoreP, Op_LoadP, Op_StoreN, Op_LoadN) ||
3523             n->has_out_with(Op_GetAndSetP, Op_GetAndSetN, Op_CompareAndExchangeP, Op_CompareAndExchangeN) ||
3524             n->has_out_with(Op_CompareAndSwapP, Op_CompareAndSwapN, Op_WeakCompareAndSwapP, Op_WeakCompareAndSwapN) ||
3525             BarrierSet::barrier_set()->barrier_set_c2()->escape_has_out_with_unsafe_object(n)) {
3526           bt = T_OBJECT;
3527           (*unsafe) = true;
3528         }
3529       }
3530     } else if (adr_type->isa_aryptr()) {
3531       if (offset == arrayOopDesc::length_offset_in_bytes()) {
3532         // Ignore array length load.
3533       } else if (find_second_addp(n, n->in(AddPNode::Base)) != nullptr) {
3534         // Ignore first AddP.
3535       } else {
3536         const Type* elemtype = adr_type->is_aryptr()->elem();
3537         if (adr_type->is_aryptr()->is_flat() && field_offset != Type::OffsetBot) {
3538           ciInlineKlass* vk = elemtype->inline_klass();
3539           field_offset += vk->payload_offset();
3540           bt = vk->get_field_by_offset(field_offset, false)->layout_type();
3541         } else {
3542           bt = elemtype->array_element_basic_type();
3543         }
3544       }
3545     } else if (adr_type->isa_rawptr() || adr_type->isa_klassptr()) {
3546       // Allocation initialization, ThreadLocal field access, unsafe access
3547       if (n->has_out_with(Op_StoreP, Op_LoadP, Op_StoreN, Op_LoadN) ||
3548           n->has_out_with(Op_GetAndSetP, Op_GetAndSetN, Op_CompareAndExchangeP, Op_CompareAndExchangeN) ||
3549           n->has_out_with(Op_CompareAndSwapP, Op_CompareAndSwapN, Op_WeakCompareAndSwapP, Op_WeakCompareAndSwapN) ||
3550           BarrierSet::barrier_set()->barrier_set_c2()->escape_has_out_with_unsafe_object(n)) {
3551         bt = T_OBJECT;
3552       }
3553     }
3554   }
3555   // Note: T_NARROWOOP is not classed as a real reference type
3556   return (is_reference_type(bt) || bt == T_NARROWOOP);
3557 }
3558 
3559 // Returns unique pointed java object or null.
3560 JavaObjectNode* ConnectionGraph::unique_java_object(Node *n) const {
3561   // If the node was created after the escape computation we can't answer.
3562   uint idx = n->_idx;
3563   if (idx >= nodes_size()) {

3720             return true;
3721           }
3722         }
3723       }
3724     }
3725   }
3726   return false;
3727 }
3728 
3729 int ConnectionGraph::address_offset(Node* adr, PhaseValues* phase) {
3730   const Type *adr_type = phase->type(adr);
3731   if (adr->is_AddP() && adr_type->isa_oopptr() == nullptr && is_captured_store_address(adr)) {
3732     // We are computing a raw address for a store captured by an Initialize
3733     // compute an appropriate address type. AddP cases #3 and #5 (see below).
3734     int offs = (int)phase->find_intptr_t_con(adr->in(AddPNode::Offset), Type::OffsetBot);
3735     assert(offs != Type::OffsetBot ||
3736            adr->in(AddPNode::Address)->in(0)->is_AllocateArray(),
3737            "offset must be a constant or it is initialization of array");
3738     return offs;
3739   }
3740   return adr_type->is_ptr()->flat_offset();


3741 }
3742 
3743 Node* ConnectionGraph::get_addp_base(Node *addp) {
3744   assert(addp->is_AddP(), "must be AddP");
3745   //
3746   // AddP cases for Base and Address inputs:
3747   // case #1. Direct object's field reference:
3748   //     Allocate
3749   //       |
3750   //     Proj #5 ( oop result )
3751   //       |
3752   //     CheckCastPP (cast to instance type)
3753   //      | |
3754   //     AddP  ( base == address )
3755   //
3756   // case #2. Indirect object's field reference:
3757   //      Phi
3758   //       |
3759   //     CastPP (cast to instance type)
3760   //      | |

3874   }
3875   return nullptr;
3876 }
3877 
3878 //
3879 // Adjust the type and inputs of an AddP which computes the
3880 // address of a field of an instance
3881 //
3882 bool ConnectionGraph::split_AddP(Node *addp, Node *base) {
3883   PhaseGVN* igvn = _igvn;
3884   const TypeOopPtr *base_t = igvn->type(base)->isa_oopptr();
3885   assert(base_t != nullptr && base_t->is_known_instance(), "expecting instance oopptr");
3886   const TypeOopPtr *t = igvn->type(addp)->isa_oopptr();
3887   if (t == nullptr) {
3888     // We are computing a raw address for a store captured by an Initialize
3889     // compute an appropriate address type (cases #3 and #5).
3890     assert(igvn->type(addp) == TypeRawPtr::NOTNULL, "must be raw pointer");
3891     assert(addp->in(AddPNode::Address)->is_Proj(), "base of raw address must be result projection from allocation");
3892     intptr_t offs = (int)igvn->find_intptr_t_con(addp->in(AddPNode::Offset), Type::OffsetBot);
3893     assert(offs != Type::OffsetBot, "offset must be a constant");
3894     if (base_t->isa_aryptr() != nullptr) {
3895       // In the case of a flat inline type array, each field has its
3896       // own slice so we need to extract the field being accessed from
3897       // the address computation
3898       t = base_t->isa_aryptr()->add_field_offset_and_offset(offs)->is_oopptr();
3899     } else {
3900       t = base_t->add_offset(offs)->is_oopptr();
3901     }
3902   }
3903   int inst_id = base_t->instance_id();
3904   assert(!t->is_known_instance() || t->instance_id() == inst_id,
3905                              "old type must be non-instance or match new type");
3906 
3907   // The type 't' could be subclass of 'base_t'.
3908   // As result t->offset() could be large then base_t's size and it will
3909   // cause the failure in add_offset() with narrow oops since TypeOopPtr()
3910   // constructor verifies correctness of the offset.
3911   //
3912   // It could happened on subclass's branch (from the type profiling
3913   // inlining) which was not eliminated during parsing since the exactness
3914   // of the allocation type was not propagated to the subclass type check.
3915   //
3916   // Or the type 't' could be not related to 'base_t' at all.
3917   // It could happen when CHA type is different from MDO type on a dead path
3918   // (for example, from instanceof check) which is not collapsed during parsing.
3919   //
3920   // Do nothing for such AddP node and don't process its users since
3921   // this code branch will go away.
3922   //
3923   if (!t->is_known_instance() &&
3924       !base_t->maybe_java_subtype_of(t)) {
3925      return false; // bail out
3926   }
3927   const TypePtr* tinst = base_t->add_offset(t->offset());
3928   if (tinst->isa_aryptr() && t->isa_aryptr()) {
3929     // In the case of a flat inline type array, each field has its
3930     // own slice so we need to keep track of the field being accessed.
3931     tinst = tinst->is_aryptr()->with_field_offset(t->is_aryptr()->field_offset().get());
3932     // Keep array properties (not flat/null-free)
3933     tinst = tinst->is_aryptr()->update_properties(t->is_aryptr());
3934     if (tinst == nullptr) {
3935       return false; // Skip dead path with inconsistent properties
3936     }
3937   }
3938 
3939   // Do NOT remove the next line: ensure a new alias index is allocated
3940   // for the instance type. Note: C++ will not remove it since the call
3941   // has side effect.
3942   int alias_idx = _compile->get_alias_index(tinst);
3943   igvn->set_type(addp, tinst);
3944   // record the allocation in the node map
3945   set_map(addp, get_map(base->_idx));
3946   // Set addp's Base and Address to 'base'.
3947   Node *abase = addp->in(AddPNode::Base);
3948   Node *adr   = addp->in(AddPNode::Address);
3949   if (adr->is_Proj() && adr->in(0)->is_Allocate() &&
3950       adr->in(0)->_idx == (uint)inst_id) {
3951     // Skip AddP cases #3 and #5.
3952   } else {
3953     assert(!abase->is_top(), "sanity"); // AddP case #3
3954     if (abase != base) {
3955       igvn->hash_delete(addp);
3956       addp->set_req(AddPNode::Base, base);
3957       if (abase == adr) {
3958         addp->set_req(AddPNode::Address, base);

4624         ptnode_adr(n->_idx)->dump();
4625         assert(jobj != nullptr && jobj != phantom_obj, "escaped allocation");
4626 #endif
4627         _compile->record_failure(_invocation > 0 ? C2Compiler::retry_no_iterative_escape_analysis() : C2Compiler::retry_no_escape_analysis());
4628         return;
4629       } else {
4630         Node *val = get_map(jobj->idx());   // CheckCastPP node
4631         TypeNode *tn = n->as_Type();
4632         const TypeOopPtr* tinst = igvn->type(val)->isa_oopptr();
4633         assert(tinst != nullptr && tinst->is_known_instance() &&
4634                tinst->instance_id() == jobj->idx() , "instance type expected.");
4635 
4636         const Type *tn_type = igvn->type(tn);
4637         const TypeOopPtr *tn_t;
4638         if (tn_type->isa_narrowoop()) {
4639           tn_t = tn_type->make_ptr()->isa_oopptr();
4640         } else {
4641           tn_t = tn_type->isa_oopptr();
4642         }
4643         if (tn_t != nullptr && tinst->maybe_java_subtype_of(tn_t)) {
4644           if (tn_t->isa_aryptr()) {
4645             // Keep array properties (not flat/null-free)
4646             tinst = tinst->is_aryptr()->update_properties(tn_t->is_aryptr());
4647             if (tinst == nullptr) {
4648               continue; // Skip dead path with inconsistent properties
4649             }
4650           }
4651           if (tn_type->isa_narrowoop()) {
4652             tn_type = tinst->make_narrowoop();
4653           } else {
4654             tn_type = tinst;
4655           }
4656           igvn->hash_delete(tn);
4657           igvn->set_type(tn, tn_type);
4658           tn->set_type(tn_type);
4659           igvn->hash_insert(tn);
4660           record_for_optimizer(n);
4661         } else {
4662           assert(tn_type == TypePtr::NULL_PTR ||
4663                  (tn_t != nullptr && !tinst->maybe_java_subtype_of(tn_t)),
4664                  "unexpected type");
4665           continue; // Skip dead path with different type
4666         }
4667       }
4668     } else {
4669       debug_only(n->dump();)
4670       assert(false, "EA: unexpected node");
4671       continue;
4672     }
4673     // push allocation's users on appropriate worklist
4674     for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
4675       Node *use = n->fast_out(i);
4676       if (use->is_Mem() && use->in(MemNode::Address) == n) {
4677         // Load/store to instance's field
4678         memnode_worklist.append_if_missing(use);
4679       } else if (use->is_MemBar()) {
4680         if (use->in(TypeFunc::Memory) == n) { // Ignore precedent edge
4681           memnode_worklist.append_if_missing(use);
4682         }
4683       } else if (use->is_AddP() && use->outcnt() > 0) { // No dead nodes
4684         Node* addp2 = find_second_addp(use, n);
4685         if (addp2 != nullptr) {
4686           alloc_worklist.append_if_missing(addp2);
4687         }
4688         alloc_worklist.append_if_missing(use);
4689       } else if (use->is_Phi() ||
4690                  use->is_CheckCastPP() ||
4691                  use->is_EncodeNarrowPtr() ||
4692                  use->is_DecodeNarrowPtr() ||
4693                  (use->is_ConstraintCast() && use->Opcode() == Op_CastPP)) {
4694         alloc_worklist.append_if_missing(use);
4695 #ifdef ASSERT
4696       } else if (use->is_Mem()) {
4697         assert(use->in(MemNode::Address) != n, "EA: missing allocation reference path");
4698       } else if (use->is_MergeMem()) {
4699         assert(mergemem_worklist.contains(use->as_MergeMem()), "EA: missing MergeMem node in the worklist");
4700       } else if (use->is_SafePoint()) {
4701         // Look for MergeMem nodes for calls which reference unique allocation
4702         // (through CheckCastPP nodes) even for debug info.
4703         Node* m = use->in(TypeFunc::Memory);
4704         if (m->is_MergeMem()) {
4705           assert(mergemem_worklist.contains(m->as_MergeMem()), "EA: missing MergeMem node in the worklist");
4706         }
4707       } else if (use->Opcode() == Op_EncodeISOArray) {
4708         if (use->in(MemNode::Memory) == n || use->in(3) == n) {
4709           // EncodeISOArray overwrites destination array
4710           memnode_worklist.append_if_missing(use);
4711         }
4712       } else if (use->Opcode() == Op_Return) {
4713         // Allocation is referenced by field of returned inline type
4714         assert(_compile->tf()->returns_inline_type_as_fields(), "EA: unexpected reference by ReturnNode");
4715       } else {
4716         uint op = use->Opcode();
4717         if ((op == Op_StrCompressedCopy || op == Op_StrInflatedCopy) &&
4718             (use->in(MemNode::Memory) == n)) {
4719           // They overwrite memory edge corresponding to destination array,
4720           memnode_worklist.append_if_missing(use);
4721         } else if (!(op == Op_CmpP || op == Op_Conv2B ||
4722               op == Op_CastP2X ||
4723               op == Op_FastLock || op == Op_AryEq ||
4724               op == Op_StrComp || op == Op_CountPositives ||
4725               op == Op_StrCompressedCopy || op == Op_StrInflatedCopy ||
4726               op == Op_StrEquals || op == Op_VectorizedHashCode ||
4727               op == Op_StrIndexOf || op == Op_StrIndexOfChar ||
4728               op == Op_SubTypeCheck || op == Op_InlineType || op == Op_FlatArrayCheck ||
4729               op == Op_ReinterpretS2HF ||
4730               BarrierSet::barrier_set()->barrier_set_c2()->is_gc_barrier_node(use))) {
4731           n->dump();
4732           use->dump();
4733           assert(false, "EA: missing allocation reference path");
4734         }
4735 #endif
4736       }
4737     }
4738 
4739   }
4740 
4741 #ifdef ASSERT
4742   if (VerifyReduceAllocationMerges) {
4743     for (uint i = 0; i < reducible_merges.size(); i++) {
4744       Node* phi = reducible_merges.at(i);
4745 
4746       if (!reduced_merges.member(phi)) {
4747         phi->dump(2);
4748         phi->dump(-2);

4812       // we don't need to do anything, but the users must be pushed
4813       n = n->as_MemBar()->proj_out_or_null(TypeFunc::Memory);
4814       if (n == nullptr) {
4815         continue;
4816       }
4817     } else if (n->is_CallLeaf()) {
4818       // Runtime calls with narrow memory input (no MergeMem node)
4819       // get the memory projection
4820       n = n->as_Call()->proj_out_or_null(TypeFunc::Memory);
4821       if (n == nullptr) {
4822         continue;
4823       }
4824     } else if (n->Opcode() == Op_StrInflatedCopy) {
4825       // Check direct uses of StrInflatedCopy.
4826       // It is memory type Node - no special SCMemProj node.
4827     } else if (n->Opcode() == Op_StrCompressedCopy ||
4828                n->Opcode() == Op_EncodeISOArray) {
4829       // get the memory projection
4830       n = n->find_out_with(Op_SCMemProj);
4831       assert(n != nullptr && n->Opcode() == Op_SCMemProj, "memory projection required");
4832     } else if (n->is_CallLeaf() && n->as_CallLeaf()->_name != nullptr &&
4833                strcmp(n->as_CallLeaf()->_name, "store_unknown_inline") == 0) {
4834       n = n->as_CallLeaf()->proj_out(TypeFunc::Memory);
4835     } else {
4836 #ifdef ASSERT
4837       if (!n->is_Mem()) {
4838         n->dump();
4839       }
4840       assert(n->is_Mem(), "memory node required.");
4841 #endif
4842       Node *addr = n->in(MemNode::Address);
4843       const Type *addr_t = igvn->type(addr);
4844       if (addr_t == Type::TOP) {
4845         continue;
4846       }
4847       assert (addr_t->isa_ptr() != nullptr, "pointer type required.");
4848       int alias_idx = _compile->get_alias_index(addr_t->is_ptr());
4849       assert ((uint)alias_idx < new_index_end, "wrong alias index");
4850       Node *mem = find_inst_mem(n->in(MemNode::Memory), alias_idx, orig_phis);
4851       if (_compile->failing()) {
4852         return;
4853       }
4854       if (mem != n->in(MemNode::Memory)) {

4859       if (n->is_Load()) {
4860         continue;  // don't push users
4861       } else if (n->is_LoadStore()) {
4862         // get the memory projection
4863         n = n->find_out_with(Op_SCMemProj);
4864         assert(n != nullptr && n->Opcode() == Op_SCMemProj, "memory projection required");
4865       }
4866     }
4867     // push user on appropriate worklist
4868     for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
4869       Node *use = n->fast_out(i);
4870       if (use->is_Phi() || use->is_ClearArray()) {
4871         memnode_worklist.append_if_missing(use);
4872       } else if (use->is_Mem() && use->in(MemNode::Memory) == n) {
4873         memnode_worklist.append_if_missing(use);
4874       } else if (use->is_MemBar() || use->is_CallLeaf()) {
4875         if (use->in(TypeFunc::Memory) == n) { // Ignore precedent edge
4876           memnode_worklist.append_if_missing(use);
4877         }
4878 #ifdef ASSERT
4879       } else if (use->is_Mem()) {
4880         assert(use->in(MemNode::Memory) != n, "EA: missing memory path");
4881       } else if (use->is_MergeMem()) {
4882         assert(mergemem_worklist.contains(use->as_MergeMem()), "EA: missing MergeMem node in the worklist");
4883       } else if (use->Opcode() == Op_EncodeISOArray) {
4884         if (use->in(MemNode::Memory) == n || use->in(3) == n) {
4885           // EncodeISOArray overwrites destination array
4886           memnode_worklist.append_if_missing(use);
4887         }
4888       } else if (use->is_CallLeaf() && use->as_CallLeaf()->_name != nullptr &&
4889                  strcmp(use->as_CallLeaf()->_name, "store_unknown_inline") == 0) {
4890         // store_unknown_inline overwrites destination array
4891         memnode_worklist.append_if_missing(use);
4892       } else {
4893         uint op = use->Opcode();
4894         if ((use->in(MemNode::Memory) == n) &&
4895             (op == Op_StrCompressedCopy || op == Op_StrInflatedCopy)) {
4896           // They overwrite memory edge corresponding to destination array,
4897           memnode_worklist.append_if_missing(use);
4898         } else if (!(BarrierSet::barrier_set()->barrier_set_c2()->is_gc_barrier_node(use) ||
4899               op == Op_AryEq || op == Op_StrComp || op == Op_CountPositives ||
4900               op == Op_StrCompressedCopy || op == Op_StrInflatedCopy || op == Op_VectorizedHashCode ||
4901               op == Op_StrEquals || op == Op_StrIndexOf || op == Op_StrIndexOfChar || op == Op_FlatArrayCheck)) {
4902           n->dump();
4903           use->dump();
4904           assert(false, "EA: missing memory path");
4905         }
4906 #endif
4907       }
4908     }
4909   }
4910 
4911   //  Phase 3:  Process MergeMem nodes from mergemem_worklist.
4912   //            Walk each memory slice moving the first node encountered of each
4913   //            instance type to the input corresponding to its alias index.
4914   uint length = mergemem_worklist.length();
4915   for( uint next = 0; next < length; ++next ) {
4916     MergeMemNode* nmm = mergemem_worklist.at(next);
4917     assert(!visited.test_set(nmm->_idx), "should not be visited before");
4918     // Note: we don't want to use MergeMemStream here because we only want to
4919     // scan inputs which exist at the start, not ones we add during processing.
4920     // Note 2: MergeMem may already contains instance memory slices added
4921     // during find_inst_mem() call when memory nodes were processed above.

4982     if (_compile->live_nodes() >= _compile->max_node_limit() * 0.75) {
4983       if (_compile->do_reduce_allocation_merges()) {
4984         _compile->record_failure(C2Compiler::retry_no_reduce_allocation_merges());
4985       } else if (_invocation > 0) {
4986         _compile->record_failure(C2Compiler::retry_no_iterative_escape_analysis());
4987       } else {
4988         _compile->record_failure(C2Compiler::retry_no_escape_analysis());
4989       }
4990       return;
4991     }
4992 
4993     igvn->hash_insert(nmm);
4994     record_for_optimizer(nmm);
4995   }
4996 
4997   //  Phase 4:  Update the inputs of non-instance memory Phis and
4998   //            the Memory input of memnodes
4999   // First update the inputs of any non-instance Phi's from
5000   // which we split out an instance Phi.  Note we don't have
5001   // to recursively process Phi's encountered on the input memory
5002   // chains as is done in split_memory_phi() since they will
5003   // also be processed here.
5004   for (int j = 0; j < orig_phis.length(); j++) {
5005     PhiNode *phi = orig_phis.at(j);
5006     int alias_idx = _compile->get_alias_index(phi->adr_type());
5007     igvn->hash_delete(phi);
5008     for (uint i = 1; i < phi->req(); i++) {
5009       Node *mem = phi->in(i);
5010       Node *new_mem = find_inst_mem(mem, alias_idx, orig_phis);
5011       if (_compile->failing()) {
5012         return;
5013       }
5014       if (mem != new_mem) {
5015         phi->set_req(i, new_mem);
5016       }
5017     }
5018     igvn->hash_insert(phi);
5019     record_for_optimizer(phi);
5020   }
5021 
5022   // Update the memory inputs of MemNodes with the value we computed
< prev index next >