< 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 ||

2198                   strcmp(call->as_CallLeaf()->_name, "dilithiumMontMulByConstant") == 0 ||
2199                   strcmp(call->as_CallLeaf()->_name, "dilithiumDecomposePoly") == 0 ||
2200                   strcmp(call->as_CallLeaf()->_name, "encodeBlock") == 0 ||
2201                   strcmp(call->as_CallLeaf()->_name, "decodeBlock") == 0 ||
2202                   strcmp(call->as_CallLeaf()->_name, "md5_implCompress") == 0 ||
2203                   strcmp(call->as_CallLeaf()->_name, "md5_implCompressMB") == 0 ||
2204                   strcmp(call->as_CallLeaf()->_name, "sha1_implCompress") == 0 ||
2205                   strcmp(call->as_CallLeaf()->_name, "sha1_implCompressMB") == 0 ||
2206                   strcmp(call->as_CallLeaf()->_name, "sha256_implCompress") == 0 ||
2207                   strcmp(call->as_CallLeaf()->_name, "sha256_implCompressMB") == 0 ||
2208                   strcmp(call->as_CallLeaf()->_name, "sha512_implCompress") == 0 ||
2209                   strcmp(call->as_CallLeaf()->_name, "sha512_implCompressMB") == 0 ||
2210                   strcmp(call->as_CallLeaf()->_name, "sha3_implCompress") == 0 ||
2211                   strcmp(call->as_CallLeaf()->_name, "double_keccak") == 0 ||
2212                   strcmp(call->as_CallLeaf()->_name, "sha3_implCompressMB") == 0 ||
2213                   strcmp(call->as_CallLeaf()->_name, "multiplyToLen") == 0 ||
2214                   strcmp(call->as_CallLeaf()->_name, "squareToLen") == 0 ||
2215                   strcmp(call->as_CallLeaf()->_name, "mulAdd") == 0 ||
2216                   strcmp(call->as_CallLeaf()->_name, "montgomery_multiply") == 0 ||
2217                   strcmp(call->as_CallLeaf()->_name, "montgomery_square") == 0 ||



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

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

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

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

2741   Node* alloc = pta->ideal_node();
2742 
2743   // Do nothing for Allocate nodes since its fields values are
2744   // "known" unless they are initialized by arraycopy/clone.
2745   if (alloc->is_Allocate() && !pta->arraycopy_dst()) {
2746     return 0;







2747   }
2748   assert(pta->arraycopy_dst() || alloc->as_CallStaticJava(), "sanity");

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

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

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

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

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

3241 #ifndef PRODUCT
3242         if (PrintOptimizePtrCompare) {
3243           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"));
3244           if (Verbose) {
3245             n->dump(1);
3246           }
3247         }
3248 #endif
3249         igvn->replace_node(n, cmp);
3250       }
3251     }
3252   }
3253 
3254   // For MemBarStoreStore nodes added in library_call.cpp, check
3255   // escape status of associated AllocateNode and optimize out
3256   // MemBarStoreStore node if the allocated object never escapes.
3257   for (int i = 0; i < storestore_worklist.length(); i++) {
3258     Node* storestore = storestore_worklist.at(i);
3259     Node* alloc = storestore->in(MemBarNode::Precedent)->in(0);
3260     if (alloc->is_Allocate() && not_global_escape(alloc)) {
3261       MemBarNode* mb = MemBarNode::make(C, Op_MemBarCPUOrder, Compile::AliasIdxBot);
3262       mb->init_req(TypeFunc::Memory,  storestore->in(TypeFunc::Memory));
3263       mb->init_req(TypeFunc::Control, storestore->in(TypeFunc::Control));
3264       igvn->register_new_node_with_optimizer(mb);
3265       igvn->replace_node(storestore, mb);





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

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

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






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

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

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







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











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

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







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



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

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



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

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




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

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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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