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/arraycopynode.hpp"
33 #include "opto/c2compiler.hpp"
34 #include "opto/callnode.hpp"
35 #include "opto/castnode.hpp"
36 #include "opto/cfgnode.hpp"
37 #include "opto/compile.hpp"
38 #include "opto/escape.hpp"
39 #include "opto/locknode.hpp"
40 #include "opto/macro.hpp"
41 #include "opto/movenode.hpp"
42 #include "opto/narrowptrnode.hpp"
43 #include "opto/phaseX.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),
58 _compile(C),
150 GrowableArray<SafePointNode*> sfn_worklist;
151 GrowableArray<MergeMemNode*> mergemem_worklist;
152 DEBUG_ONLY( GrowableArray<Node*> addp_worklist; )
153
154 { Compile::TracePhase tp(Phase::_t_connectionGraph);
155
156 // 1. Populate Connection Graph (CG) with PointsTo nodes.
157 ideal_nodes.map(C->live_nodes(), nullptr); // preallocate space
158 // Initialize worklist
159 if (C->root() != nullptr) {
160 ideal_nodes.push(C->root());
161 }
162 // Processed ideal nodes are unique on ideal_nodes list
163 // but several ideal nodes are mapped to the phantom_obj.
164 // To avoid duplicated entries on the following worklists
165 // add the phantom_obj only once to them.
166 ptnodes_worklist.append(phantom_obj);
167 java_objects_worklist.append(phantom_obj);
168 for( uint next = 0; next < ideal_nodes.size(); ++next ) {
169 Node* n = ideal_nodes.at(next);
170 // Create PointsTo nodes and add them to Connection Graph. Called
171 // only once per ideal node since ideal_nodes is Unique_Node list.
172 add_node_to_connection_graph(n, &delayed_worklist);
173 PointsToNode* ptn = ptnode_adr(n->_idx);
174 if (ptn != nullptr && ptn != phantom_obj) {
175 ptnodes_worklist.append(ptn);
176 if (ptn->is_JavaObject()) {
177 java_objects_worklist.append(ptn->as_JavaObject());
178 if ((n->is_Allocate() || n->is_CallStaticJava()) &&
179 (ptn->escape_state() < PointsToNode::GlobalEscape)) {
180 // Only allocations and java static calls results are interesting.
181 non_escaped_allocs_worklist.append(ptn->as_JavaObject());
182 }
183 } else if (ptn->is_Field() && ptn->as_Field()->is_oop()) {
184 oop_fields_worklist.append(ptn->as_Field());
185 }
186 }
187 // Collect some interesting nodes for further use.
188 switch (n->Opcode()) {
189 case Op_MergeMem:
407 // scalar replaceable objects.
408 split_unique_types(alloc_worklist, arraycopy_worklist, mergemem_worklist, reducible_merges);
409 if (C->failing()) {
410 NOT_PRODUCT(escape_state_statistics(java_objects_worklist);)
411 return false;
412 }
413
414 #ifdef ASSERT
415 } else if (Verbose && (PrintEscapeAnalysis || PrintEliminateAllocations)) {
416 tty->print("=== No allocations eliminated for ");
417 C->method()->print_short_name();
418 if (!EliminateAllocations) {
419 tty->print(" since EliminateAllocations is off ===");
420 } else if(!has_scalar_replaceable_candidates) {
421 tty->print(" since there are no scalar replaceable candidates ===");
422 }
423 tty->cr();
424 #endif
425 }
426
427 _compile->print_method(PHASE_EA_AFTER_SPLIT_UNIQUE_TYPES, 4);
428
429 // 6. Reduce allocation merges used as debug information. This is done after
430 // split_unique_types because the methods used to create SafePointScalarObject
431 // need to traverse the memory graph to find values for object fields. We also
432 // set to null the scalarized inputs of reducible Phis so that the Allocate
433 // that they point can be later scalar replaced.
434 bool delay = _igvn->delay_transform();
435 _igvn->set_delay_transform(true);
436 for (uint i = 0; i < reducible_merges.size(); i++) {
437 Node* n = reducible_merges.at(i);
438 if (n->outcnt() > 0) {
439 if (!reduce_phi_on_safepoints(n->as_Phi())) {
440 NOT_PRODUCT(escape_state_statistics(java_objects_worklist);)
441 C->record_failure(C2Compiler::retry_no_reduce_allocation_merges());
442 return false;
443 }
444
445 // Now we set the scalar replaceable inputs of ophi to null, which is
446 // the last piece that would prevent it from being scalar replaceable.
447 reset_scalar_replaceable_entries(n->as_Phi());
448 }
449 }
1308 // (2) A selector, used to decide if we need to rematerialize an object
1309 // or use the pointer to a NSR object.
1310 // See more details of these fields in the declaration of SafePointScalarMergeNode.
1311 // It is safe to include them into debug info straight away since create_scalarized_object_description()
1312 // will include all newly added inputs into debug info anyway.
1313 sfpt->add_req(nsr_merge_pointer);
1314 sfpt->add_req(selector);
1315 sfpt->jvms()->set_endoff(sfpt->req());
1316
1317 for (uint i = 1; i < ophi->req(); i++) {
1318 Node* base = ophi->in(i);
1319 JavaObjectNode* ptn = unique_java_object(base);
1320
1321 // If the base is not scalar replaceable we don't need to register information about
1322 // it at this time.
1323 if (ptn == nullptr || !ptn->scalar_replaceable()) {
1324 continue;
1325 }
1326
1327 AllocateNode* alloc = ptn->ideal_node()->as_Allocate();
1328 SafePointScalarObjectNode* sobj = mexp.create_scalarized_object_description(alloc, sfpt);
1329 if (sobj == nullptr) {
1330 sfpt->restore_non_debug_edges(non_debug_edges_worklist);
1331 return false; // non-recoverable failure; recompile
1332 }
1333
1334 // Now make a pass over the debug information replacing any references
1335 // to the allocated object with "sobj"
1336 Node* ccpp = alloc->result_cast();
1337 sfpt->replace_edges_in_range(ccpp, sobj, debug_start, jvms->debug_end(), _igvn);
1338 non_debug_edges_worklist.remove_edge_if_present(ccpp); // drop scalarized input from non-debug info
1339
1340 // Register the scalarized object as a candidate for reallocation
1341 smerge->add_req(sobj);
1342 }
1343
1344 // Replaces debug information references to "original_sfpt_parent" in "sfpt" with references to "smerge"
1345 sfpt->replace_edges_in_range(original_sfpt_parent, smerge, debug_start, jvms->debug_end(), _igvn);
1346 non_debug_edges_worklist.remove_edge_if_present(original_sfpt_parent); // drop scalarized input from non-debug info
1347
1348 // The call to 'replace_edges_in_range' above might have removed the
1349 // reference to ophi that we need at _merge_pointer_idx. The line below make
1350 // sure the reference is maintained.
1351 sfpt->set_req(smerge->merge_pointer_idx(jvms), nsr_merge_pointer);
1352
1353 sfpt->restore_non_debug_edges(non_debug_edges_worklist);
1354
1355 _igvn->_worklist.push(sfpt);
1356 }
1357
1358 return true;
1359 }
1360
1361 void ConnectionGraph::reduce_phi(PhiNode* ophi, GrowableArray<Node*> &alloc_worklist) {
1528 return false;
1529 }
1530
1531 // Returns true if at least one of the arguments to the call is an object
1532 // that does not escape globally.
1533 bool ConnectionGraph::has_arg_escape(CallJavaNode* call) {
1534 if (call->method() != nullptr) {
1535 uint max_idx = TypeFunc::Parms + call->method()->arg_size();
1536 for (uint idx = TypeFunc::Parms; idx < max_idx; idx++) {
1537 Node* p = call->in(idx);
1538 if (not_global_escape(p)) {
1539 return true;
1540 }
1541 }
1542 } else {
1543 const char* name = call->as_CallStaticJava()->_name;
1544 assert(name != nullptr, "no name");
1545 // no arg escapes through uncommon traps
1546 if (strcmp(name, "uncommon_trap") != 0) {
1547 // process_call_arguments() assumes that all arguments escape globally
1548 const TypeTuple* d = call->tf()->domain();
1549 for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
1550 const Type* at = d->field_at(i);
1551 if (at->isa_oopptr() != nullptr) {
1552 return true;
1553 }
1554 }
1555 }
1556 }
1557 return false;
1558 }
1559
1560
1561
1562 // Utility function for nodes that load an object
1563 void ConnectionGraph::add_objload_to_connection_graph(Node *n, Unique_Node_List *delayed_worklist) {
1564 // Using isa_ptr() instead of isa_oopptr() for LoadP and Phi because
1565 // ThreadLocal has RawPtr type.
1566 const Type* t = _igvn->type(n);
1567 if (t->make_ptr() != nullptr) {
1568 Node* adr = n->in(MemNode::Address);
1569 #ifdef ASSERT
1570 if (!adr->is_AddP()) {
1571 assert(_igvn->type(adr)->isa_rawptr(), "sanity");
1572 } else {
1573 assert((ptnode_adr(adr->_idx) == nullptr ||
1574 ptnode_adr(adr->_idx)->as_Field()->is_oop()), "sanity");
1575 }
1576 #endif
1577 add_local_var_and_edge(n, PointsToNode::NoEscape,
1578 adr, delayed_worklist);
1579 }
1580 }
1581
1582 // Populate Connection Graph with PointsTo nodes and create simple
1583 // connection graph edges.
1584 void ConnectionGraph::add_node_to_connection_graph(Node *n, Unique_Node_List *delayed_worklist) {
1585 assert(!_verify, "this method should not be called for verification");
1586 PhaseGVN* igvn = _igvn;
1587 uint n_idx = n->_idx;
1588 PointsToNode* n_ptn = ptnode_adr(n_idx);
1589 if (n_ptn != nullptr) {
1590 return; // No need to redefine PointsTo node during first iteration.
1591 }
1592 int opcode = n->Opcode();
1593 bool gc_handled = BarrierSet::barrier_set()->barrier_set_c2()->escape_add_to_con_graph(this, igvn, delayed_worklist, n, opcode);
1594 if (gc_handled) {
1595 return; // Ignore node if already handled by GC.
1596 }
1597
1598 if (n->is_Call()) {
1599 // Arguments to allocation and locking don't escape.
1600 if (n->is_AbstractLock()) {
1601 // Put Lock and Unlock nodes on IGVN worklist to process them during
1602 // first IGVN optimization when escape information is still available.
1603 record_for_optimizer(n);
1604 } else if (n->is_Allocate()) {
1605 add_call_node(n->as_Call());
1606 record_for_optimizer(n);
1607 } else {
1608 if (n->is_CallStaticJava()) {
1609 const char* name = n->as_CallStaticJava()->_name;
1610 if (name != nullptr && strcmp(name, "uncommon_trap") == 0) {
1611 return; // Skip uncommon traps
1612 }
1613 }
1614 // Don't mark as processed since call's arguments have to be processed.
1615 delayed_worklist->push(n);
1616 // Check if a call returns an object.
1617 if ((n->as_Call()->returns_pointer() &&
1618 n->as_Call()->proj_out_or_null(TypeFunc::Parms) != nullptr) ||
1619 (n->is_CallStaticJava() &&
1620 n->as_CallStaticJava()->is_boxing_method())) {
1621 add_call_node(n->as_Call());
1622 }
1623 }
1624 return;
1625 }
1626 // Put this check here to process call arguments since some call nodes
1627 // point to phantom_obj.
1628 if (n_ptn == phantom_obj || n_ptn == null_obj) {
1629 return; // Skip predefined nodes.
1630 }
1631 switch (opcode) {
1632 case Op_AddP: {
1633 Node* base = get_addp_base(n);
1634 PointsToNode* ptn_base = ptnode_adr(base->_idx);
1635 // Field nodes are created for all field types. They are used in
1636 // adjust_scalar_replaceable_state() and split_unique_types().
1637 // Note, non-oop fields will have only base edges in Connection
1638 // Graph because such fields are not used for oop loads and stores.
1639 int offset = address_offset(n, igvn);
1640 add_field(n, PointsToNode::NoEscape, offset);
1641 if (ptn_base == nullptr) {
1642 delayed_worklist->push(n); // Process it later.
1643 } else {
1644 n_ptn = ptnode_adr(n_idx);
1645 add_base(n_ptn->as_Field(), ptn_base);
1646 }
1647 break;
1648 }
1649 case Op_CastX2P: {
1650 map_ideal_node(n, phantom_obj);
1651 break;
1652 }
1653 case Op_CastPP:
1654 case Op_CheckCastPP:
1655 case Op_EncodeP:
1656 case Op_DecodeN:
1657 case Op_EncodePKlass:
1658 case Op_DecodeNKlass: {
1659 add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(1), delayed_worklist);
1660 break;
1661 }
1662 case Op_CMoveP: {
1663 add_local_var(n, PointsToNode::NoEscape);
1664 // Do not add edges during first iteration because some could be
1665 // not defined yet.
1666 delayed_worklist->push(n);
1667 break;
1668 }
1669 case Op_ConP:
1670 case Op_ConN:
1671 case Op_ConNKlass: {
1672 // assume all oop constants globally escape except for null
1702 break;
1703 }
1704 case Op_PartialSubtypeCheck: {
1705 // Produces Null or notNull and is used in only in CmpP so
1706 // phantom_obj could be used.
1707 map_ideal_node(n, phantom_obj); // Result is unknown
1708 break;
1709 }
1710 case Op_Phi: {
1711 // Using isa_ptr() instead of isa_oopptr() for LoadP and Phi because
1712 // ThreadLocal has RawPtr type.
1713 const Type* t = n->as_Phi()->type();
1714 if (t->make_ptr() != nullptr) {
1715 add_local_var(n, PointsToNode::NoEscape);
1716 // Do not add edges during first iteration because some could be
1717 // not defined yet.
1718 delayed_worklist->push(n);
1719 }
1720 break;
1721 }
1722 case Op_Proj: {
1723 // we are only interested in the oop result projection from a call
1724 if (n->as_Proj()->_con == TypeFunc::Parms && n->in(0)->is_Call() &&
1725 n->in(0)->as_Call()->returns_pointer()) {
1726 add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(0), delayed_worklist);
1727 }
1728 break;
1729 }
1730 case Op_Rethrow: // Exception object escapes
1731 case Op_Return: {
1732 if (n->req() > TypeFunc::Parms &&
1733 igvn->type(n->in(TypeFunc::Parms))->isa_oopptr()) {
1734 // Treat Return value as LocalVar with GlobalEscape escape state.
1735 add_local_var_and_edge(n, PointsToNode::GlobalEscape, n->in(TypeFunc::Parms), delayed_worklist);
1736 }
1737 break;
1738 }
1739 case Op_CompareAndExchangeP:
1740 case Op_CompareAndExchangeN:
1741 case Op_GetAndSetP:
1742 case Op_GetAndSetN: {
1743 add_objload_to_connection_graph(n, delayed_worklist);
1744 // fall-through
1745 }
1746 case Op_StoreP:
1747 case Op_StoreN:
1791 break;
1792 }
1793 default:
1794 ; // Do nothing for nodes not related to EA.
1795 }
1796 return;
1797 }
1798
1799 // Add final simple edges to graph.
1800 void ConnectionGraph::add_final_edges(Node *n) {
1801 PointsToNode* n_ptn = ptnode_adr(n->_idx);
1802 #ifdef ASSERT
1803 if (_verify && n_ptn->is_JavaObject())
1804 return; // This method does not change graph for JavaObject.
1805 #endif
1806
1807 if (n->is_Call()) {
1808 process_call_arguments(n->as_Call());
1809 return;
1810 }
1811 assert(n->is_Store() || n->is_LoadStore() ||
1812 ((n_ptn != nullptr) && (n_ptn->ideal_node() != nullptr)),
1813 "node should be registered already");
1814 int opcode = n->Opcode();
1815 bool gc_handled = BarrierSet::barrier_set()->barrier_set_c2()->escape_add_final_edges(this, _igvn, n, opcode);
1816 if (gc_handled) {
1817 return; // Ignore node if already handled by GC.
1818 }
1819 switch (opcode) {
1820 case Op_AddP: {
1821 Node* base = get_addp_base(n);
1822 PointsToNode* ptn_base = ptnode_adr(base->_idx);
1823 assert(ptn_base != nullptr, "field's base should be registered");
1824 add_base(n_ptn->as_Field(), ptn_base);
1825 break;
1826 }
1827 case Op_CastPP:
1828 case Op_CheckCastPP:
1829 case Op_EncodeP:
1830 case Op_DecodeN:
1831 case Op_EncodePKlass:
1832 case Op_DecodeNKlass: {
1833 add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(1), nullptr);
1834 break;
1835 }
1836 case Op_CMoveP: {
1837 for (uint i = CMoveNode::IfFalse; i < n->req(); i++) {
1838 Node* in = n->in(i);
1839 if (in == nullptr) {
1840 continue; // ignore null
1841 }
1842 Node* uncast_in = in->uncast();
1843 if (uncast_in->is_top() || uncast_in == n) {
1844 continue; // ignore top or inputs which go back this node
1845 }
1846 PointsToNode* ptn = ptnode_adr(in->_idx);
1859 }
1860 case Op_Phi: {
1861 // Using isa_ptr() instead of isa_oopptr() for LoadP and Phi because
1862 // ThreadLocal has RawPtr type.
1863 assert(n->as_Phi()->type()->make_ptr() != nullptr, "Unexpected node type");
1864 for (uint i = 1; i < n->req(); i++) {
1865 Node* in = n->in(i);
1866 if (in == nullptr) {
1867 continue; // ignore null
1868 }
1869 Node* uncast_in = in->uncast();
1870 if (uncast_in->is_top() || uncast_in == n) {
1871 continue; // ignore top or inputs which go back this node
1872 }
1873 PointsToNode* ptn = ptnode_adr(in->_idx);
1874 assert(ptn != nullptr, "node should be registered");
1875 add_edge(n_ptn, ptn);
1876 }
1877 break;
1878 }
1879 case Op_Proj: {
1880 // we are only interested in the oop result projection from a call
1881 assert(n->as_Proj()->_con == TypeFunc::Parms && n->in(0)->is_Call() &&
1882 n->in(0)->as_Call()->returns_pointer(), "Unexpected node type");
1883 add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(0), nullptr);
1884 break;
1885 }
1886 case Op_Rethrow: // Exception object escapes
1887 case Op_Return: {
1888 assert(n->req() > TypeFunc::Parms && _igvn->type(n->in(TypeFunc::Parms))->isa_oopptr(),
1889 "Unexpected node type");
1890 // Treat Return value as LocalVar with GlobalEscape escape state.
1891 add_local_var_and_edge(n, PointsToNode::GlobalEscape, n->in(TypeFunc::Parms), nullptr);
1892 break;
1893 }
1894 case Op_CompareAndExchangeP:
1895 case Op_CompareAndExchangeN:
1896 case Op_GetAndSetP:
1897 case Op_GetAndSetN:{
1898 assert(_igvn->type(n)->make_ptr() != nullptr, "Unexpected node type");
1899 add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(MemNode::Address), nullptr);
1900 // fall-through
1901 }
1902 case Op_CompareAndSwapP:
1903 case Op_CompareAndSwapN:
2037 Node* val = n->in(MemNode::ValueIn);
2038 PointsToNode* ptn = ptnode_adr(val->_idx);
2039 assert(ptn != nullptr, "node should be registered");
2040 set_escape_state(ptn, PointsToNode::GlobalEscape NOT_PRODUCT(COMMA "stored at raw address"));
2041 // Add edge to object for unsafe access with offset.
2042 PointsToNode* adr_ptn = ptnode_adr(adr->_idx);
2043 assert(adr_ptn != nullptr, "node should be registered");
2044 if (adr_ptn->is_Field()) {
2045 assert(adr_ptn->as_Field()->is_oop(), "should be oop field");
2046 add_edge(adr_ptn, ptn);
2047 }
2048 return true;
2049 }
2050 #ifdef ASSERT
2051 n->dump(1);
2052 assert(false, "not unsafe");
2053 #endif
2054 return false;
2055 }
2056
2057 void ConnectionGraph::add_call_node(CallNode* call) {
2058 assert(call->returns_pointer(), "only for call which returns pointer");
2059 uint call_idx = call->_idx;
2060 if (call->is_Allocate()) {
2061 Node* k = call->in(AllocateNode::KlassNode);
2062 const TypeKlassPtr* kt = k->bottom_type()->isa_klassptr();
2063 assert(kt != nullptr, "TypeKlassPtr required.");
2064 PointsToNode::EscapeState es = PointsToNode::NoEscape;
2065 bool scalar_replaceable = true;
2066 NOT_PRODUCT(const char* nsr_reason = "");
2067 if (call->is_AllocateArray()) {
2068 if (!kt->isa_aryklassptr()) { // StressReflectiveCode
2069 es = PointsToNode::GlobalEscape;
2070 } else {
2071 int length = call->in(AllocateNode::ALength)->find_int_con(-1);
2072 if (length < 0) {
2073 // Not scalar replaceable if the length is not constant.
2074 scalar_replaceable = false;
2075 NOT_PRODUCT(nsr_reason = "has a non-constant length");
2076 } else if (length > EliminateAllocationArraySizeLimit) {
2077 // Not scalar replaceable if the length is too big.
2078 scalar_replaceable = false;
2113 // - mapped to GlobalEscape JavaObject node if oop is returned;
2114 //
2115 // - all oop arguments are escaping globally;
2116 //
2117 // 2. CallStaticJavaNode (execute bytecode analysis if possible):
2118 //
2119 // - the same as CallDynamicJavaNode if can't do bytecode analysis;
2120 //
2121 // - mapped to GlobalEscape JavaObject node if unknown oop is returned;
2122 // - mapped to NoEscape JavaObject node if non-escaping object allocated
2123 // during call is returned;
2124 // - mapped to ArgEscape LocalVar node pointed to object arguments
2125 // which are returned and does not escape during call;
2126 //
2127 // - oop arguments escaping status is defined by bytecode analysis;
2128 //
2129 // For a static call, we know exactly what method is being called.
2130 // Use bytecode estimator to record whether the call's return value escapes.
2131 ciMethod* meth = call->as_CallJava()->method();
2132 if (meth == nullptr) {
2133 assert(call->as_CallStaticJava()->is_call_to_multianewarray_stub(), "TODO: add failed case check");
2134 // Returns a newly allocated non-escaped object.
2135 add_java_object(call, PointsToNode::NoEscape);
2136 set_not_scalar_replaceable(ptnode_adr(call_idx) NOT_PRODUCT(COMMA "is result of multinewarray"));
2137 } else if (meth->is_boxing_method()) {
2138 // Returns boxing object
2139 PointsToNode::EscapeState es;
2140 vmIntrinsics::ID intr = meth->intrinsic_id();
2141 if (intr == vmIntrinsics::_floatValue || intr == vmIntrinsics::_doubleValue) {
2142 // It does not escape if object is always allocated.
2143 es = PointsToNode::NoEscape;
2144 } else {
2145 // It escapes globally if object could be loaded from cache.
2146 es = PointsToNode::GlobalEscape;
2147 }
2148 add_java_object(call, es);
2149 if (es == PointsToNode::GlobalEscape) {
2150 set_not_scalar_replaceable(ptnode_adr(call->_idx) NOT_PRODUCT(COMMA "object can be loaded from boxing cache"));
2151 }
2152 } else {
2153 BCEscapeAnalyzer* call_analyzer = meth->get_bcea();
2154 call_analyzer->copy_dependencies(_compile->dependencies());
2155 if (call_analyzer->is_return_allocated()) {
2156 // Returns a newly allocated non-escaped object, simply
2157 // update dependency information.
2158 // Mark it as NoEscape so that objects referenced by
2159 // it's fields will be marked as NoEscape at least.
2160 add_java_object(call, PointsToNode::NoEscape);
2161 set_not_scalar_replaceable(ptnode_adr(call_idx) NOT_PRODUCT(COMMA "is result of call"));
2162 } else {
2163 // Determine whether any arguments are returned.
2164 const TypeTuple* d = call->tf()->domain();
2165 bool ret_arg = false;
2166 for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
2167 if (d->field_at(i)->isa_ptr() != nullptr &&
2168 call_analyzer->is_arg_returned(i - TypeFunc::Parms)) {
2169 ret_arg = true;
2170 break;
2171 }
2172 }
2173 if (ret_arg) {
2174 add_local_var(call, PointsToNode::ArgEscape);
2175 } else {
2176 // Returns unknown object.
2177 map_ideal_node(call, phantom_obj);
2178 }
2179 }
2180 }
2181 } else {
2182 // An other type of call, assume the worst case:
2183 // returned value is unknown and globally escapes.
2184 assert(call->Opcode() == Op_CallDynamicJava, "add failed case check");
2185 map_ideal_node(call, phantom_obj);
2186 }
2187 }
2188
2189 void ConnectionGraph::process_call_arguments(CallNode *call) {
2190 bool is_arraycopy = false;
2191 switch (call->Opcode()) {
2192 #ifdef ASSERT
2193 case Op_Allocate:
2194 case Op_AllocateArray:
2195 case Op_Lock:
2196 case Op_Unlock:
2197 assert(false, "should be done already");
2198 break;
2199 #endif
2200 case Op_ArrayCopy:
2201 case Op_CallLeafNoFP:
2202 // Most array copies are ArrayCopy nodes at this point but there
2203 // are still a few direct calls to the copy subroutines (See
2204 // PhaseStringOpts::copy_string())
2205 is_arraycopy = (call->Opcode() == Op_ArrayCopy) ||
2206 call->as_CallLeaf()->is_call_to_arraycopystub();
2207 // fall through
2208 case Op_CallLeafVector:
2209 case Op_CallLeaf: {
2210 // Stub calls, objects do not escape but they are not scale replaceable.
2211 // Adjust escape state for outgoing arguments.
2212 const TypeTuple * d = call->tf()->domain();
2213 bool src_has_oops = false;
2214 for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
2215 const Type* at = d->field_at(i);
2216 Node *arg = call->in(i);
2217 if (arg == nullptr) {
2218 continue;
2219 }
2220 const Type *aat = _igvn->type(arg);
2221 if (arg->is_top() || !at->isa_ptr() || !aat->isa_ptr()) {
2222 continue;
2223 }
2224 if (arg->is_AddP()) {
2225 //
2226 // The inline_native_clone() case when the arraycopy stub is called
2227 // after the allocation before Initialize and CheckCastPP nodes.
2228 // Or normal arraycopy for object arrays case.
2229 //
2230 // Set AddP's base (Allocate) as not scalar replaceable since
2231 // pointer to the base (with offset) is passed as argument.
2232 //
2233 arg = get_addp_base(arg);
2234 }
2235 PointsToNode* arg_ptn = ptnode_adr(arg->_idx);
2236 assert(arg_ptn != nullptr, "should be registered");
2237 PointsToNode::EscapeState arg_esc = arg_ptn->escape_state();
2238 if (is_arraycopy || arg_esc < PointsToNode::ArgEscape) {
2239 assert(aat == Type::TOP || aat == TypePtr::NULL_PTR ||
2240 aat->isa_ptr() != nullptr, "expecting an Ptr");
2241 bool arg_has_oops = aat->isa_oopptr() &&
2242 (aat->isa_instptr() ||
2243 (aat->isa_aryptr() && (aat->isa_aryptr()->elem() == Type::BOTTOM || aat->isa_aryptr()->elem()->make_oopptr() != nullptr)));
2244 if (i == TypeFunc::Parms) {
2245 src_has_oops = arg_has_oops;
2246 }
2247 //
2248 // src or dst could be j.l.Object when other is basic type array:
2249 //
2250 // arraycopy(char[],0,Object*,0,size);
2251 // arraycopy(Object*,0,char[],0,size);
2252 //
2253 // Don't add edges in such cases.
2254 //
2255 bool arg_is_arraycopy_dest = src_has_oops && is_arraycopy &&
2256 arg_has_oops && (i > TypeFunc::Parms);
2257 #ifdef ASSERT
2258 if (!(is_arraycopy ||
2259 BarrierSet::barrier_set()->barrier_set_c2()->is_gc_barrier_node(call) ||
2260 (call->as_CallLeaf()->_name != nullptr &&
2261 (strcmp(call->as_CallLeaf()->_name, "updateBytesCRC32") == 0 ||
2262 strcmp(call->as_CallLeaf()->_name, "updateBytesCRC32C") == 0 ||
2263 strcmp(call->as_CallLeaf()->_name, "updateBytesAdler32") == 0 ||
2290 strcmp(call->as_CallLeaf()->_name, "dilithiumDecomposePoly") == 0 ||
2291 strcmp(call->as_CallLeaf()->_name, "encodeBlock") == 0 ||
2292 strcmp(call->as_CallLeaf()->_name, "decodeBlock") == 0 ||
2293 strcmp(call->as_CallLeaf()->_name, "md5_implCompress") == 0 ||
2294 strcmp(call->as_CallLeaf()->_name, "md5_implCompressMB") == 0 ||
2295 strcmp(call->as_CallLeaf()->_name, "sha1_implCompress") == 0 ||
2296 strcmp(call->as_CallLeaf()->_name, "sha1_implCompressMB") == 0 ||
2297 strcmp(call->as_CallLeaf()->_name, "sha256_implCompress") == 0 ||
2298 strcmp(call->as_CallLeaf()->_name, "sha256_implCompressMB") == 0 ||
2299 strcmp(call->as_CallLeaf()->_name, "sha512_implCompress") == 0 ||
2300 strcmp(call->as_CallLeaf()->_name, "sha512_implCompressMB") == 0 ||
2301 strcmp(call->as_CallLeaf()->_name, "sha3_implCompress") == 0 ||
2302 strcmp(call->as_CallLeaf()->_name, "double_keccak") == 0 ||
2303 strcmp(call->as_CallLeaf()->_name, "quad_keccak") == 0 ||
2304 strcmp(call->as_CallLeaf()->_name, "sha3_implCompressMB") == 0 ||
2305 strcmp(call->as_CallLeaf()->_name, "multiplyToLen") == 0 ||
2306 strcmp(call->as_CallLeaf()->_name, "squareToLen") == 0 ||
2307 strcmp(call->as_CallLeaf()->_name, "mulAdd") == 0 ||
2308 strcmp(call->as_CallLeaf()->_name, "montgomery_multiply") == 0 ||
2309 strcmp(call->as_CallLeaf()->_name, "montgomery_square") == 0 ||
2310 strcmp(call->as_CallLeaf()->_name, "bigIntegerRightShiftWorker") == 0 ||
2311 strcmp(call->as_CallLeaf()->_name, "bigIntegerLeftShiftWorker") == 0 ||
2312 strcmp(call->as_CallLeaf()->_name, "vectorizedMismatch") == 0 ||
2313 strcmp(call->as_CallLeaf()->_name, "stringIndexOf") == 0 ||
2314 strcmp(call->as_CallLeaf()->_name, "arraysort_stub") == 0 ||
2315 strcmp(call->as_CallLeaf()->_name, "array_partition_stub") == 0 ||
2316 strcmp(call->as_CallLeaf()->_name, "get_class_id_intrinsic") == 0 ||
2317 strcmp(call->as_CallLeaf()->_name, "unsafe_setmemory") == 0)
2318 ))) {
2319 call->dump();
2320 fatal("EA unexpected CallLeaf %s", call->as_CallLeaf()->_name);
2321 }
2322 #endif
2323 // Always process arraycopy's destination object since
2324 // we need to add all possible edges to references in
2325 // source object.
2326 if (arg_esc >= PointsToNode::ArgEscape &&
2327 !arg_is_arraycopy_dest) {
2328 continue;
2329 }
2352 }
2353 }
2354 }
2355 break;
2356 }
2357 case Op_CallStaticJava: {
2358 // For a static call, we know exactly what method is being called.
2359 // Use bytecode estimator to record the call's escape affects
2360 #ifdef ASSERT
2361 const char* name = call->as_CallStaticJava()->_name;
2362 assert((name == nullptr || strcmp(name, "uncommon_trap") != 0), "normal calls only");
2363 #endif
2364 ciMethod* meth = call->as_CallJava()->method();
2365 if ((meth != nullptr) && meth->is_boxing_method()) {
2366 break; // Boxing methods do not modify any oops.
2367 }
2368 BCEscapeAnalyzer* call_analyzer = (meth !=nullptr) ? meth->get_bcea() : nullptr;
2369 // fall-through if not a Java method or no analyzer information
2370 if (call_analyzer != nullptr) {
2371 PointsToNode* call_ptn = ptnode_adr(call->_idx);
2372 const TypeTuple* d = call->tf()->domain();
2373 for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
2374 const Type* at = d->field_at(i);
2375 int k = i - TypeFunc::Parms;
2376 Node* arg = call->in(i);
2377 PointsToNode* arg_ptn = ptnode_adr(arg->_idx);
2378 if (at->isa_ptr() != nullptr &&
2379 call_analyzer->is_arg_returned(k)) {
2380 // The call returns arguments.
2381 if (call_ptn != nullptr) { // Is call's result used?
2382 assert(call_ptn->is_LocalVar(), "node should be registered");
2383 assert(arg_ptn != nullptr, "node should be registered");
2384 add_edge(call_ptn, arg_ptn);
2385 }
2386 }
2387 if (at->isa_oopptr() != nullptr &&
2388 arg_ptn->escape_state() < PointsToNode::GlobalEscape) {
2389 if (!call_analyzer->is_arg_stack(k)) {
2390 // The argument global escapes
2391 set_escape_state(arg_ptn, PointsToNode::GlobalEscape NOT_PRODUCT(COMMA trace_arg_escape_message(call)));
2392 } else {
2393 set_escape_state(arg_ptn, PointsToNode::ArgEscape NOT_PRODUCT(COMMA trace_arg_escape_message(call)));
2394 if (!call_analyzer->is_arg_local(k)) {
2395 // The argument itself doesn't escape, but any fields might
2396 set_fields_escape_state(arg_ptn, PointsToNode::GlobalEscape NOT_PRODUCT(COMMA trace_arg_escape_message(call)));
2397 }
2398 }
2399 }
2400 }
2401 if (call_ptn != nullptr && call_ptn->is_LocalVar()) {
2402 // The call returns arguments.
2403 assert(call_ptn->edge_count() > 0, "sanity");
2404 if (!call_analyzer->is_return_local()) {
2405 // Returns also unknown object.
2406 add_edge(call_ptn, phantom_obj);
2407 }
2408 }
2409 break;
2410 }
2411 }
2412 default: {
2413 // Fall-through here if not a Java method or no analyzer information
2414 // or some other type of call, assume the worst case: all arguments
2415 // globally escape.
2416 const TypeTuple* d = call->tf()->domain();
2417 for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
2418 const Type* at = d->field_at(i);
2419 if (at->isa_oopptr() != nullptr) {
2420 Node* arg = call->in(i);
2421 if (arg->is_AddP()) {
2422 arg = get_addp_base(arg);
2423 }
2424 assert(ptnode_adr(arg->_idx) != nullptr, "should be defined already");
2425 set_escape_state(ptnode_adr(arg->_idx), PointsToNode::GlobalEscape NOT_PRODUCT(COMMA trace_arg_escape_message(call)));
2426 }
2427 }
2428 }
2429 }
2430 }
2431
2432
2433 // Finish Graph construction.
2434 bool ConnectionGraph::complete_connection_graph(
2435 GrowableArray<PointsToNode*>& ptnodes_worklist,
2436 GrowableArray<JavaObjectNode*>& non_escaped_allocs_worklist,
2814 PointsToNode* base = i.get();
2815 if (base->is_JavaObject()) {
2816 // Skip Allocate's fields which will be processed later.
2817 if (base->ideal_node()->is_Allocate()) {
2818 return 0;
2819 }
2820 assert(base == null_obj, "only null ptr base expected here");
2821 }
2822 }
2823 if (add_edge(field, phantom_obj)) {
2824 // New edge was added
2825 new_edges++;
2826 add_field_uses_to_worklist(field);
2827 }
2828 return new_edges;
2829 }
2830
2831 // Find fields initializing values for allocations.
2832 int ConnectionGraph::find_init_values_phantom(JavaObjectNode* pta) {
2833 assert(pta->escape_state() == PointsToNode::NoEscape, "Not escaped Allocate nodes only");
2834 Node* alloc = pta->ideal_node();
2835
2836 // Do nothing for Allocate nodes since its fields values are
2837 // "known" unless they are initialized by arraycopy/clone.
2838 if (alloc->is_Allocate() && !pta->arraycopy_dst()) {
2839 return 0;
2840 }
2841 assert(pta->arraycopy_dst() || alloc->as_CallStaticJava(), "sanity");
2842 #ifdef ASSERT
2843 if (!pta->arraycopy_dst() && alloc->as_CallStaticJava()->method() == nullptr) {
2844 assert(alloc->as_CallStaticJava()->is_call_to_multianewarray_stub(), "sanity");
2845 }
2846 #endif
2847 // Non-escaped allocation returned from Java or runtime call have unknown values in fields.
2848 int new_edges = 0;
2849 for (EdgeIterator i(pta); i.has_next(); i.next()) {
2850 PointsToNode* field = i.get();
2851 if (field->is_Field() && field->as_Field()->is_oop()) {
2852 if (add_edge(field, phantom_obj)) {
2853 // New edge was added
2854 new_edges++;
2855 add_field_uses_to_worklist(field->as_Field());
2856 }
2857 }
2858 }
2859 return new_edges;
2860 }
2861
2862 // Find fields initializing values for allocations.
2863 int ConnectionGraph::find_init_values_null(JavaObjectNode* pta, PhaseValues* phase) {
2864 assert(pta->escape_state() == PointsToNode::NoEscape, "Not escaped Allocate nodes only");
2865 Node* alloc = pta->ideal_node();
2866 // Do nothing for Call nodes since its fields values are unknown.
2867 if (!alloc->is_Allocate()) {
2868 return 0;
2869 }
2870 InitializeNode* ini = alloc->as_Allocate()->initialization();
2871 bool visited_bottom_offset = false;
2872 GrowableArray<int> offsets_worklist;
2873 int new_edges = 0;
2874
2875 // Check if an oop field's initializing value is recorded and add
2876 // a corresponding null if field's value if it is not recorded.
2877 // Connection Graph does not record a default initialization by null
2878 // captured by Initialize node.
2879 //
2880 for (EdgeIterator i(pta); i.has_next(); i.next()) {
2881 PointsToNode* field = i.get(); // Field (AddP)
2882 if (!field->is_Field() || !field->as_Field()->is_oop()) {
2883 continue; // Not oop field
2884 }
2885 int offset = field->as_Field()->offset();
2886 if (offset == Type::OffsetBot) {
2887 if (!visited_bottom_offset) {
2933 } else {
2934 if (!val->is_LocalVar() || (val->edge_count() == 0)) {
2935 tty->print_cr("----------init store has invalid value -----");
2936 store->dump();
2937 val->dump();
2938 assert(val->is_LocalVar() && (val->edge_count() > 0), "should be processed already");
2939 }
2940 for (EdgeIterator j(val); j.has_next(); j.next()) {
2941 PointsToNode* obj = j.get();
2942 if (obj->is_JavaObject()) {
2943 if (!field->points_to(obj->as_JavaObject())) {
2944 missed_obj = obj;
2945 break;
2946 }
2947 }
2948 }
2949 }
2950 if (missed_obj != nullptr) {
2951 tty->print_cr("----------field---------------------------------");
2952 field->dump();
2953 tty->print_cr("----------missed referernce to object-----------");
2954 missed_obj->dump();
2955 tty->print_cr("----------object referernced by init store -----");
2956 store->dump();
2957 val->dump();
2958 assert(!field->points_to(missed_obj->as_JavaObject()), "missed JavaObject reference");
2959 }
2960 }
2961 #endif
2962 } else {
2963 // There could be initializing stores which follow allocation.
2964 // For example, a volatile field store is not collected
2965 // by Initialize node.
2966 //
2967 // Need to check for dependent loads to separate such stores from
2968 // stores which follow loads. For now, add initial value null so
2969 // that compare pointers optimization works correctly.
2970 }
2971 }
2972 if (value == nullptr) {
2973 // A field's initializing value was not recorded. Add null.
2974 if (add_edge(field, null_obj)) {
2975 // New edge was added
3300 assert(field->edge_count() > 0, "sanity");
3301 }
3302 }
3303 }
3304 }
3305 #endif
3306
3307 // Optimize ideal graph.
3308 void ConnectionGraph::optimize_ideal_graph(GrowableArray<Node*>& ptr_cmp_worklist,
3309 GrowableArray<MemBarStoreStoreNode*>& storestore_worklist) {
3310 Compile* C = _compile;
3311 PhaseIterGVN* igvn = _igvn;
3312 if (EliminateLocks) {
3313 // Mark locks before changing ideal graph.
3314 int cnt = C->macro_count();
3315 for (int i = 0; i < cnt; i++) {
3316 Node *n = C->macro_node(i);
3317 if (n->is_AbstractLock()) { // Lock and Unlock nodes
3318 AbstractLockNode* alock = n->as_AbstractLock();
3319 if (!alock->is_non_esc_obj()) {
3320 if (can_eliminate_lock(alock)) {
3321 assert(!alock->is_eliminated() || alock->is_coarsened(), "sanity");
3322 // The lock could be marked eliminated by lock coarsening
3323 // code during first IGVN before EA. Replace coarsened flag
3324 // to eliminate all associated locks/unlocks.
3325 #ifdef ASSERT
3326 alock->log_lock_optimization(C, "eliminate_lock_set_non_esc3");
3327 #endif
3328 alock->set_non_esc_obj();
3329 }
3330 }
3331 }
3332 }
3333 }
3334
3335 if (OptimizePtrCompare) {
3336 for (int i = 0; i < ptr_cmp_worklist.length(); i++) {
3337 Node *n = ptr_cmp_worklist.at(i);
3338 assert(n->Opcode() == Op_CmpN || n->Opcode() == Op_CmpP, "must be");
3339 const TypeInt* tcmp = optimize_ptr_compare(n->in(1), n->in(2));
3340 if (tcmp->singleton()) {
3342 #ifndef PRODUCT
3343 if (PrintOptimizePtrCompare) {
3344 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"));
3345 if (Verbose) {
3346 n->dump(1);
3347 }
3348 }
3349 #endif
3350 igvn->replace_node(n, cmp);
3351 }
3352 }
3353 }
3354
3355 // For MemBarStoreStore nodes added in library_call.cpp, check
3356 // escape status of associated AllocateNode and optimize out
3357 // MemBarStoreStore node if the allocated object never escapes.
3358 for (int i = 0; i < storestore_worklist.length(); i++) {
3359 Node* storestore = storestore_worklist.at(i);
3360 Node* alloc = storestore->in(MemBarNode::Precedent)->in(0);
3361 if (alloc->is_Allocate() && not_global_escape(alloc)) {
3362 MemBarNode* mb = MemBarNode::make(C, Op_MemBarCPUOrder, Compile::AliasIdxBot);
3363 mb->init_req(TypeFunc::Memory, storestore->in(TypeFunc::Memory));
3364 mb->init_req(TypeFunc::Control, storestore->in(TypeFunc::Control));
3365 igvn->register_new_node_with_optimizer(mb);
3366 igvn->replace_node(storestore, mb);
3367 }
3368 }
3369 }
3370
3371 // Optimize objects compare.
3372 const TypeInt* ConnectionGraph::optimize_ptr_compare(Node* left, Node* right) {
3373 const TypeInt* UNKNOWN = TypeInt::CC; // [-1, 0,1]
3374 if (!OptimizePtrCompare) {
3375 return UNKNOWN;
3376 }
3377 const TypeInt* EQ = TypeInt::CC_EQ; // [0] == ZERO
3378 const TypeInt* NE = TypeInt::CC_GT; // [1] == ONE
3379
3380 PointsToNode* ptn1 = ptnode_adr(left->_idx);
3381 PointsToNode* ptn2 = ptnode_adr(right->_idx);
3382 JavaObjectNode* jobj1 = unique_java_object(left);
3383 JavaObjectNode* jobj2 = unique_java_object(right);
3384
3385 // The use of this method during allocation merge reduction may cause 'left'
3386 // or 'right' be something (e.g., a Phi) that isn't in the connection graph or
3387 // that doesn't reference an unique java object.
3388 if (ptn1 == nullptr || ptn2 == nullptr ||
3389 jobj1 == nullptr || jobj2 == nullptr) {
3390 return UNKNOWN;
3510 assert(!src->is_Field() && !dst->is_Field(), "only for JavaObject and LocalVar");
3511 assert((src != null_obj) && (dst != null_obj), "not for ConP null");
3512 PointsToNode* ptadr = _nodes.at(n->_idx);
3513 if (ptadr != nullptr) {
3514 assert(ptadr->is_Arraycopy() && ptadr->ideal_node() == n, "sanity");
3515 return;
3516 }
3517 Compile* C = _compile;
3518 ptadr = new (C->comp_arena()) ArraycopyNode(this, n, es);
3519 map_ideal_node(n, ptadr);
3520 // Add edge from arraycopy node to source object.
3521 (void)add_edge(ptadr, src);
3522 src->set_arraycopy_src();
3523 // Add edge from destination object to arraycopy node.
3524 (void)add_edge(dst, ptadr);
3525 dst->set_arraycopy_dst();
3526 }
3527
3528 bool ConnectionGraph::is_oop_field(Node* n, int offset, bool* unsafe) {
3529 const Type* adr_type = n->as_AddP()->bottom_type();
3530 BasicType bt = T_INT;
3531 if (offset == Type::OffsetBot) {
3532 // Check only oop fields.
3533 if (!adr_type->isa_aryptr() ||
3534 adr_type->isa_aryptr()->elem() == Type::BOTTOM ||
3535 adr_type->isa_aryptr()->elem()->make_oopptr() != nullptr) {
3536 // OffsetBot is used to reference array's element. Ignore first AddP.
3537 if (find_second_addp(n, n->in(AddPNode::Base)) == nullptr) {
3538 bt = T_OBJECT;
3539 }
3540 }
3541 } else if (offset != oopDesc::klass_offset_in_bytes()) {
3542 if (adr_type->isa_instptr()) {
3543 ciField* field = _compile->alias_type(adr_type->isa_instptr())->field();
3544 if (field != nullptr) {
3545 bt = field->layout_type();
3546 } else {
3547 // Check for unsafe oop field access
3548 if (has_oop_node_outs(n)) {
3549 bt = T_OBJECT;
3550 (*unsafe) = true;
3551 }
3552 }
3553 } else if (adr_type->isa_aryptr()) {
3554 if (offset == arrayOopDesc::length_offset_in_bytes()) {
3555 // Ignore array length load.
3556 } else if (find_second_addp(n, n->in(AddPNode::Base)) != nullptr) {
3557 // Ignore first AddP.
3558 } else {
3559 const Type* elemtype = adr_type->isa_aryptr()->elem();
3560 bt = elemtype->array_element_basic_type();
3561 }
3562 } else if (adr_type->isa_rawptr() || adr_type->isa_klassptr()) {
3563 // Allocation initialization, ThreadLocal field access, unsafe access
3564 if (has_oop_node_outs(n)) {
3565 bt = T_OBJECT;
3566 }
3567 }
3568 }
3569 // Note: T_NARROWOOP is not classed as a real reference type
3570 bool res = (is_reference_type(bt) || bt == T_NARROWOOP);
3571 assert(!has_oop_node_outs(n) || res, "sanity: AddP has oop outs, needs to be treated as oop field");
3572 return res;
3573 }
3574
3575 bool ConnectionGraph::has_oop_node_outs(Node* n) {
3576 return n->has_out_with(Op_StoreP, Op_LoadP, Op_StoreN, Op_LoadN) ||
3577 n->has_out_with(Op_GetAndSetP, Op_GetAndSetN, Op_CompareAndExchangeP, Op_CompareAndExchangeN) ||
3578 n->has_out_with(Op_CompareAndSwapP, Op_CompareAndSwapN, Op_WeakCompareAndSwapP, Op_WeakCompareAndSwapN) ||
3579 BarrierSet::barrier_set()->barrier_set_c2()->escape_has_out_with_unsafe_object(n);
3580 }
3743 return true;
3744 }
3745 }
3746 }
3747 }
3748 }
3749 return false;
3750 }
3751
3752 int ConnectionGraph::address_offset(Node* adr, PhaseValues* phase) {
3753 const Type *adr_type = phase->type(adr);
3754 if (adr->is_AddP() && adr_type->isa_oopptr() == nullptr && is_captured_store_address(adr)) {
3755 // We are computing a raw address for a store captured by an Initialize
3756 // compute an appropriate address type. AddP cases #3 and #5 (see below).
3757 int offs = (int)phase->find_intptr_t_con(adr->in(AddPNode::Offset), Type::OffsetBot);
3758 assert(offs != Type::OffsetBot ||
3759 adr->in(AddPNode::Address)->in(0)->is_AllocateArray(),
3760 "offset must be a constant or it is initialization of array");
3761 return offs;
3762 }
3763 const TypePtr *t_ptr = adr_type->isa_ptr();
3764 assert(t_ptr != nullptr, "must be a pointer type");
3765 return t_ptr->offset();
3766 }
3767
3768 Node* ConnectionGraph::get_addp_base(Node *addp) {
3769 assert(addp->is_AddP(), "must be AddP");
3770 //
3771 // AddP cases for Base and Address inputs:
3772 // case #1. Direct object's field reference:
3773 // Allocate
3774 // |
3775 // Proj #5 ( oop result )
3776 // |
3777 // CheckCastPP (cast to instance type)
3778 // | |
3779 // AddP ( base == address )
3780 //
3781 // case #2. Indirect object's field reference:
3782 // Phi
3783 // |
3784 // CastPP (cast to instance type)
3785 // | |
3786 // AddP ( base == address )
3787 //
3788 // case #3. Raw object's field reference for Initialize node:
3789 // Allocate
3790 // |
3791 // Proj #5 ( oop result )
3792 // top |
3793 // \ |
3794 // AddP ( base == top )
3795 //
3796 // case #4. Array's element reference:
3797 // {CheckCastPP | CastPP}
3798 // | | |
3799 // | AddP ( array's element offset )
3800 // | |
3801 // AddP ( array's offset )
3802 //
3803 // case #5. Raw object's field reference for arraycopy stub call:
3804 // The inline_native_clone() case when the arraycopy stub is called
3805 // after the allocation before Initialize and CheckCastPP nodes.
3806 // Allocate
3807 // |
3808 // Proj #5 ( oop result )
3819 // case #7. Klass's field reference.
3820 // LoadKlass
3821 // | |
3822 // AddP ( base == address )
3823 //
3824 // case #8. narrow Klass's field reference.
3825 // LoadNKlass
3826 // |
3827 // DecodeN
3828 // | |
3829 // AddP ( base == address )
3830 //
3831 // case #9. Mixed unsafe access
3832 // {instance}
3833 // |
3834 // CheckCastPP (raw)
3835 // top |
3836 // \ |
3837 // AddP ( base == top )
3838 //
3839 Node *base = addp->in(AddPNode::Base);
3840 if (base->uncast()->is_top()) { // The AddP case #3 and #6 and #9.
3841 base = addp->in(AddPNode::Address);
3842 while (base->is_AddP()) {
3843 // Case #6 (unsafe access) may have several chained AddP nodes.
3844 assert(base->in(AddPNode::Base)->uncast()->is_top(), "expected unsafe access address only");
3845 base = base->in(AddPNode::Address);
3846 }
3847 if (base->Opcode() == Op_CheckCastPP &&
3848 base->bottom_type()->isa_rawptr() &&
3849 _igvn->type(base->in(1))->isa_oopptr()) {
3850 base = base->in(1); // Case #9
3851 } else {
3852 Node* uncast_base = base->uncast();
3853 int opcode = uncast_base->Opcode();
3854 assert(opcode == Op_ConP || opcode == Op_ThreadLocal ||
3855 opcode == Op_CastX2P || uncast_base->is_DecodeNarrowPtr() ||
3856 (_igvn->C->is_osr_compilation() && uncast_base->is_Parm() && uncast_base->as_Parm()->_con == TypeFunc::Parms)||
3857 (uncast_base->is_Mem() && (uncast_base->bottom_type()->isa_rawptr() != nullptr)) ||
3858 (uncast_base->is_Mem() && (uncast_base->bottom_type()->isa_klassptr() != nullptr)) ||
3859 is_captured_store_address(addp), "sanity");
3860 }
3861 }
3862 return base;
3863 }
3864
3865 Node* ConnectionGraph::find_second_addp(Node* addp, Node* n) {
3866 assert(addp->is_AddP() && addp->outcnt() > 0, "Don't process dead nodes");
3867 Node* addp2 = addp->raw_out(0);
3868 if (addp->outcnt() == 1 && addp2->is_AddP() &&
3869 addp2->in(AddPNode::Base) == n &&
3870 addp2->in(AddPNode::Address) == addp) {
3871 assert(addp->in(AddPNode::Base) == n, "expecting the same base");
3872 //
3873 // Find array's offset to push it on worklist first and
3874 // as result process an array's element offset first (pushed second)
3875 // to avoid CastPP for the array's offset.
3876 // Otherwise the inserted CastPP (LocalVar) will point to what
3877 // the AddP (Field) points to. Which would be wrong since
3878 // the algorithm expects the CastPP has the same point as
3879 // as AddP's base CheckCastPP (LocalVar).
3880 //
3881 // ArrayAllocation
3882 // |
3883 // CheckCastPP
3884 // |
3901 }
3902 return nullptr;
3903 }
3904
3905 //
3906 // Adjust the type and inputs of an AddP which computes the
3907 // address of a field of an instance
3908 //
3909 bool ConnectionGraph::split_AddP(Node *addp, Node *base) {
3910 PhaseGVN* igvn = _igvn;
3911 const TypeOopPtr *base_t = igvn->type(base)->isa_oopptr();
3912 assert(base_t != nullptr && base_t->is_known_instance(), "expecting instance oopptr");
3913 const TypeOopPtr *t = igvn->type(addp)->isa_oopptr();
3914 if (t == nullptr) {
3915 // We are computing a raw address for a store captured by an Initialize
3916 // compute an appropriate address type (cases #3 and #5).
3917 assert(igvn->type(addp) == TypeRawPtr::NOTNULL, "must be raw pointer");
3918 assert(addp->in(AddPNode::Address)->is_Proj(), "base of raw address must be result projection from allocation");
3919 intptr_t offs = (int)igvn->find_intptr_t_con(addp->in(AddPNode::Offset), Type::OffsetBot);
3920 assert(offs != Type::OffsetBot, "offset must be a constant");
3921 t = base_t->add_offset(offs)->is_oopptr();
3922 }
3923 int inst_id = base_t->instance_id();
3924 assert(!t->is_known_instance() || t->instance_id() == inst_id,
3925 "old type must be non-instance or match new type");
3926
3927 // The type 't' could be subclass of 'base_t'.
3928 // As result t->offset() could be large then base_t's size and it will
3929 // cause the failure in add_offset() with narrow oops since TypeOopPtr()
3930 // constructor verifies correctness of the offset.
3931 //
3932 // It could happened on subclass's branch (from the type profiling
3933 // inlining) which was not eliminated during parsing since the exactness
3934 // of the allocation type was not propagated to the subclass type check.
3935 //
3936 // Or the type 't' could be not related to 'base_t' at all.
3937 // It could happened when CHA type is different from MDO type on a dead path
3938 // (for example, from instanceof check) which is not collapsed during parsing.
3939 //
3940 // Do nothing for such AddP node and don't process its users since
3941 // this code branch will go away.
3942 //
3943 if (!t->is_known_instance() &&
3944 !base_t->maybe_java_subtype_of(t)) {
3945 return false; // bail out
3946 }
3947 const TypeOopPtr *tinst = base_t->add_offset(t->offset())->is_oopptr();
3948 // Do NOT remove the next line: ensure a new alias index is allocated
3949 // for the instance type. Note: C++ will not remove it since the call
3950 // has side effect.
3951 int alias_idx = _compile->get_alias_index(tinst);
3952 igvn->set_type(addp, tinst);
3953 // record the allocation in the node map
3954 set_map(addp, get_map(base->_idx));
3955 // Set addp's Base and Address to 'base'.
3956 Node *abase = addp->in(AddPNode::Base);
3957 Node *adr = addp->in(AddPNode::Address);
3958 if (adr->is_Proj() && adr->in(0)->is_Allocate() &&
3959 adr->in(0)->_idx == (uint)inst_id) {
3960 // Skip AddP cases #3 and #5.
3961 } else {
3962 assert(!abase->is_top(), "sanity"); // AddP case #3
3963 if (abase != base) {
3964 igvn->hash_delete(addp);
3965 addp->set_req(AddPNode::Base, base);
3966 if (abase == adr) {
3967 addp->set_req(AddPNode::Address, base);
4175 "Following memory nodes should have new memory input or be on the same memory slice");
4176 } else if (use->is_Phi()) {
4177 // Phi nodes should be split and moved already.
4178 tp = use->as_Phi()->adr_type()->isa_ptr();
4179 assert(tp != nullptr, "ptr type");
4180 int idx = C->get_alias_index(tp);
4181 assert(idx == alias_idx, "Following Phi nodes should be on the same memory slice");
4182 } else {
4183 use->dump();
4184 assert(false, "should not be here");
4185 #endif
4186 }
4187 }
4188 }
4189
4190 //
4191 // Search memory chain of "mem" to find a MemNode whose address
4192 // is the specified alias index.
4193 //
4194 #define FIND_INST_MEM_RECURSION_DEPTH_LIMIT 1000
4195 Node* ConnectionGraph::find_inst_mem(Node* orig_mem, int alias_idx, Unique_Node_List& orig_phis, uint rec_depth) {
4196 if (rec_depth > FIND_INST_MEM_RECURSION_DEPTH_LIMIT) {
4197 _compile->record_failure(_invocation > 0 ? C2Compiler::retry_no_iterative_escape_analysis() : C2Compiler::retry_no_escape_analysis());
4198 return nullptr;
4199 }
4200 if (orig_mem == nullptr) {
4201 return orig_mem;
4202 }
4203 Compile* C = _compile;
4204 PhaseGVN* igvn = _igvn;
4205 const TypeOopPtr *toop = C->get_adr_type(alias_idx)->isa_oopptr();
4206 bool is_instance = (toop != nullptr) && toop->is_known_instance();
4207 Node *start_mem = C->start()->proj_out_or_null(TypeFunc::Memory);
4208 Node *prev = nullptr;
4209 Node *result = orig_mem;
4210 while (prev != result) {
4211 prev = result;
4212 if (result == start_mem) {
4213 break; // hit one of our sentinels
4214 }
4251 } else if (C->get_alias_index(result->adr_type()) != alias_idx) {
4252 assert(C->get_general_index(alias_idx) == C->get_alias_index(result->adr_type()), "should be projection for the same field/array element");
4253 result = get_map(result->_idx);
4254 assert(result != nullptr, "new projection should have been allocated");
4255 break;
4256 }
4257 } else if (proj_in->is_MemBar()) {
4258 // Check if there is an array copy for a clone
4259 // Step over GC barrier when ReduceInitialCardMarks is disabled
4260 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
4261 Node* control_proj_ac = bs->step_over_gc_barrier(proj_in->in(0));
4262
4263 if (control_proj_ac->is_Proj() && control_proj_ac->in(0)->is_ArrayCopy()) {
4264 // Stop if it is a clone
4265 ArrayCopyNode* ac = control_proj_ac->in(0)->as_ArrayCopy();
4266 if (ac->may_modify(toop, igvn)) {
4267 break;
4268 }
4269 }
4270 result = proj_in->in(TypeFunc::Memory);
4271 }
4272 } else if (result->is_MergeMem()) {
4273 MergeMemNode *mmem = result->as_MergeMem();
4274 result = step_through_mergemem(mmem, alias_idx, toop);
4275 if (result == mmem->base_memory()) {
4276 // Didn't find instance memory, search through general slice recursively.
4277 result = mmem->memory_at(C->get_general_index(alias_idx));
4278 result = find_inst_mem(result, alias_idx, orig_phis, rec_depth + 1);
4279 if (C->failing()) {
4280 return nullptr;
4281 }
4282 mmem->set_memory_at(alias_idx, result);
4283 }
4284 } else if (result->is_Phi() &&
4285 C->get_alias_index(result->as_Phi()->adr_type()) != alias_idx) {
4286 Node *un = result->as_Phi()->unique_input(igvn);
4287 if (un != nullptr) {
4288 orig_phis.push(result);
4289 result = un;
4290 } else {
4536 // - not determined to be ineligible by escape analysis
4537 set_map(alloc, n);
4538 set_map(n, alloc);
4539 const TypeOopPtr* tinst = t->cast_to_instance_id(ni);
4540 igvn->hash_delete(n);
4541 igvn->set_type(n, tinst);
4542 n->raise_bottom_type(tinst);
4543 igvn->hash_insert(n);
4544 record_for_optimizer(n);
4545 // Allocate an alias index for the header fields. Accesses to
4546 // the header emitted during macro expansion wouldn't have
4547 // correct memory state otherwise.
4548 _compile->get_alias_index(tinst->add_offset(oopDesc::mark_offset_in_bytes()));
4549 _compile->get_alias_index(tinst->add_offset(oopDesc::klass_offset_in_bytes()));
4550 if (alloc->is_Allocate() && (t->isa_instptr() || t->isa_aryptr())) {
4551 // Add a new NarrowMem projection for each existing NarrowMem projection with new adr type
4552 InitializeNode* init = alloc->as_Allocate()->initialization();
4553 assert(init != nullptr, "can't find Initialization node for this Allocate node");
4554 auto process_narrow_proj = [&](NarrowMemProjNode* proj) {
4555 const TypePtr* adr_type = proj->adr_type();
4556 const TypePtr* new_adr_type = tinst->add_offset(adr_type->offset());
4557 if (adr_type != new_adr_type && !init->already_has_narrow_mem_proj_with_adr_type(new_adr_type)) {
4558 // Do NOT remove the next line: ensure a new alias index is allocated for the instance type.
4559 uint alias_idx = _compile->get_alias_index(new_adr_type);
4560 assert(_compile->get_general_index(alias_idx) == _compile->get_alias_index(adr_type), "new adr type should be narrowed down from existing adr type");
4561 NarrowMemProjNode* new_proj = new NarrowMemProjNode(init, new_adr_type);
4562 igvn->set_type(new_proj, new_proj->bottom_type());
4563 record_for_optimizer(new_proj);
4564 set_map(proj, new_proj); // record it so ConnectionGraph::find_inst_mem() can find it
4565 }
4566 };
4567 init->for_each_narrow_mem_proj_with_new_uses(process_narrow_proj);
4568
4569 // First, put on the worklist all Field edges from Connection Graph
4570 // which is more accurate than putting immediate users from Ideal Graph.
4571 for (EdgeIterator e(ptn); e.has_next(); e.next()) {
4572 PointsToNode* tgt = e.get();
4573 if (tgt->is_Arraycopy()) {
4574 continue;
4575 }
4576 Node* use = tgt->ideal_node();
4654 ptnode_adr(n->_idx)->dump();
4655 assert(jobj != nullptr && jobj != phantom_obj, "escaped allocation");
4656 #endif
4657 _compile->record_failure(_invocation > 0 ? C2Compiler::retry_no_iterative_escape_analysis() : C2Compiler::retry_no_escape_analysis());
4658 return;
4659 } else {
4660 Node *val = get_map(jobj->idx()); // CheckCastPP node
4661 TypeNode *tn = n->as_Type();
4662 const TypeOopPtr* tinst = igvn->type(val)->isa_oopptr();
4663 assert(tinst != nullptr && tinst->is_known_instance() &&
4664 tinst->instance_id() == jobj->idx() , "instance type expected.");
4665
4666 const Type *tn_type = igvn->type(tn);
4667 const TypeOopPtr *tn_t;
4668 if (tn_type->isa_narrowoop()) {
4669 tn_t = tn_type->make_ptr()->isa_oopptr();
4670 } else {
4671 tn_t = tn_type->isa_oopptr();
4672 }
4673 if (tn_t != nullptr && tinst->maybe_java_subtype_of(tn_t)) {
4674 if (tn_type->isa_narrowoop()) {
4675 tn_type = tinst->make_narrowoop();
4676 } else {
4677 tn_type = tinst;
4678 }
4679 igvn->hash_delete(tn);
4680 igvn->set_type(tn, tn_type);
4681 tn->set_type(tn_type);
4682 igvn->hash_insert(tn);
4683 record_for_optimizer(n);
4684 } else {
4685 assert(tn_type == TypePtr::NULL_PTR ||
4686 (tn_t != nullptr && !tinst->maybe_java_subtype_of(tn_t)),
4687 "unexpected type");
4688 continue; // Skip dead path with different type
4689 }
4690 }
4691 } else {
4692 DEBUG_ONLY(n->dump();)
4693 assert(false, "EA: unexpected node");
4694 continue;
4695 }
4696 // push allocation's users on appropriate worklist
4697 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
4698 Node *use = n->fast_out(i);
4699 if(use->is_Mem() && use->in(MemNode::Address) == n) {
4700 // Load/store to instance's field
4701 memnode_worklist.push(use);
4702 } else if (use->is_MemBar()) {
4703 if (use->in(TypeFunc::Memory) == n) { // Ignore precedent edge
4704 memnode_worklist.push(use);
4705 }
4706 } else if (use->is_AddP() && use->outcnt() > 0) { // No dead nodes
4707 Node* addp2 = find_second_addp(use, n);
4708 if (addp2 != nullptr) {
4709 alloc_worklist.append_if_missing(addp2);
4710 }
4711 alloc_worklist.append_if_missing(use);
4712 } else if (use->is_Phi() ||
4713 use->is_CheckCastPP() ||
4714 use->is_EncodeNarrowPtr() ||
4715 use->is_DecodeNarrowPtr() ||
4716 (use->is_ConstraintCast() && use->Opcode() == Op_CastPP)) {
4717 alloc_worklist.append_if_missing(use);
4718 #ifdef ASSERT
4719 } else if (use->is_Mem()) {
4720 assert(use->in(MemNode::Address) != n, "EA: missing allocation reference path");
4721 } else if (use->is_MergeMem()) {
4722 assert(mergemem_worklist.contains(use->as_MergeMem()), "EA: missing MergeMem node in the worklist");
4723 } else if (use->is_SafePoint()) {
4724 // Look for MergeMem nodes for calls which reference unique allocation
4725 // (through CheckCastPP nodes) even for debug info.
4726 Node* m = use->in(TypeFunc::Memory);
4727 if (m->is_MergeMem()) {
4728 assert(mergemem_worklist.contains(m->as_MergeMem()), "EA: missing MergeMem node in the worklist");
4729 }
4730 } else if (use->Opcode() == Op_EncodeISOArray) {
4731 if (use->in(MemNode::Memory) == n || use->in(3) == n) {
4732 // EncodeISOArray overwrites destination array
4733 memnode_worklist.push(use);
4734 }
4735 } else {
4736 uint op = use->Opcode();
4737 if ((op == Op_StrCompressedCopy || op == Op_StrInflatedCopy) &&
4738 (use->in(MemNode::Memory) == n)) {
4739 // They overwrite memory edge corresponding to destination array,
4740 memnode_worklist.push(use);
4741 } else if (!(op == Op_CmpP || op == Op_Conv2B ||
4742 op == Op_CastP2X ||
4743 op == Op_FastLock || op == Op_AryEq ||
4744 op == Op_StrComp || op == Op_CountPositives ||
4745 op == Op_StrCompressedCopy || op == Op_StrInflatedCopy ||
4746 op == Op_StrEquals || op == Op_VectorizedHashCode ||
4747 op == Op_StrIndexOf || op == Op_StrIndexOfChar ||
4748 op == Op_SubTypeCheck ||
4749 op == Op_ReinterpretS2HF ||
4750 op == Op_ReachabilityFence ||
4751 BarrierSet::barrier_set()->barrier_set_c2()->is_gc_barrier_node(use))) {
4752 n->dump();
4753 use->dump();
4754 assert(false, "EA: missing allocation reference path");
4755 }
4756 #endif
4757 }
4758 }
4759
4760 }
4761
4762 #ifdef ASSERT
4763 if (VerifyReduceAllocationMerges) {
4764 for (uint i = 0; i < reducible_merges.size(); i++) {
4765 Node* phi = reducible_merges.at(i);
4766
4767 if (!reduced_merges.member(phi)) {
4768 phi->dump(2);
4844 n = n->as_MemBar()->proj_out_or_null(TypeFunc::Memory);
4845 if (n == nullptr) {
4846 continue;
4847 }
4848 }
4849 } else if (n->is_CallLeaf()) {
4850 // Runtime calls with narrow memory input (no MergeMem node)
4851 // get the memory projection
4852 n = n->as_Call()->proj_out_or_null(TypeFunc::Memory);
4853 if (n == nullptr) {
4854 continue;
4855 }
4856 } else if (n->Opcode() == Op_StrInflatedCopy) {
4857 // Check direct uses of StrInflatedCopy.
4858 // It is memory type Node - no special SCMemProj node.
4859 } else if (n->Opcode() == Op_StrCompressedCopy ||
4860 n->Opcode() == Op_EncodeISOArray) {
4861 // get the memory projection
4862 n = n->find_out_with(Op_SCMemProj);
4863 assert(n != nullptr && n->Opcode() == Op_SCMemProj, "memory projection required");
4864 } else if (n->is_Proj()) {
4865 assert(n->in(0)->is_Initialize(), "we only push memory projections for Initialize");
4866 } else {
4867 #ifdef ASSERT
4868 if (!n->is_Mem()) {
4869 n->dump();
4870 }
4871 assert(n->is_Mem(), "memory node required.");
4872 #endif
4873 Node *addr = n->in(MemNode::Address);
4874 const Type *addr_t = igvn->type(addr);
4875 if (addr_t == Type::TOP) {
4876 continue;
4877 }
4878 assert (addr_t->isa_ptr() != nullptr, "pointer type required.");
4879 int alias_idx = _compile->get_alias_index(addr_t->is_ptr());
4880 assert ((uint)alias_idx < new_index_end, "wrong alias index");
4881 Node *mem = find_inst_mem(n->in(MemNode::Memory), alias_idx, orig_phis);
4882 if (_compile->failing()) {
4883 return;
4895 assert(n != nullptr && n->Opcode() == Op_SCMemProj, "memory projection required");
4896 }
4897 }
4898 // push user on appropriate worklist
4899 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
4900 Node *use = n->fast_out(i);
4901 if (use->is_Phi() || use->is_ClearArray()) {
4902 memnode_worklist.push(use);
4903 } else if (use->is_Mem() && use->in(MemNode::Memory) == n) {
4904 memnode_worklist.push(use);
4905 } else if (use->is_MemBar() || use->is_CallLeaf()) {
4906 if (use->in(TypeFunc::Memory) == n) { // Ignore precedent edge
4907 memnode_worklist.push(use);
4908 }
4909 } else if (use->is_Proj()) {
4910 assert(n->is_Initialize(), "We only push projections of Initialize");
4911 if (use->as_Proj()->_con == TypeFunc::Memory) { // Ignore precedent edge
4912 memnode_worklist.push(use);
4913 }
4914 #ifdef ASSERT
4915 } else if(use->is_Mem()) {
4916 assert(use->in(MemNode::Memory) != n, "EA: missing memory path");
4917 } else if (use->is_MergeMem()) {
4918 assert(mergemem_worklist.contains(use->as_MergeMem()), "EA: missing MergeMem node in the worklist");
4919 } else if (use->Opcode() == Op_EncodeISOArray) {
4920 if (use->in(MemNode::Memory) == n || use->in(3) == n) {
4921 // EncodeISOArray overwrites destination array
4922 memnode_worklist.push(use);
4923 }
4924 } else {
4925 uint op = use->Opcode();
4926 if ((use->in(MemNode::Memory) == n) &&
4927 (op == Op_StrCompressedCopy || op == Op_StrInflatedCopy)) {
4928 // They overwrite memory edge corresponding to destination array,
4929 memnode_worklist.push(use);
4930 } else if (!(BarrierSet::barrier_set()->barrier_set_c2()->is_gc_barrier_node(use) ||
4931 op == Op_AryEq || op == Op_StrComp || op == Op_CountPositives ||
4932 op == Op_StrCompressedCopy || op == Op_StrInflatedCopy || op == Op_VectorizedHashCode ||
4933 op == Op_StrEquals || op == Op_StrIndexOf || op == Op_StrIndexOfChar)) {
4934 n->dump();
4935 use->dump();
4936 assert(false, "EA: missing memory path");
4937 }
4938 #endif
4939 }
4940 }
4941 }
4942
4943 // Phase 3: Process MergeMem nodes from mergemem_worklist.
4944 // Walk each memory slice moving the first node encountered of each
4945 // instance type to the input corresponding to its alias index.
4946 uint length = mergemem_worklist.length();
4947 for( uint next = 0; next < length; ++next ) {
4948 MergeMemNode* nmm = mergemem_worklist.at(next);
4949 assert(!visited.test_set(nmm->_idx), "should not be visited before");
4950 // Note: we don't want to use MergeMemStream here because we only want to
4951 // scan inputs which exist at the start, not ones we add during processing.
4952 // Note 2: MergeMem may already contains instance memory slices added
4953 // during find_inst_mem() call when memory nodes were processed above.
5016 _compile->record_failure(C2Compiler::retry_no_reduce_allocation_merges());
5017 } else if (_invocation > 0) {
5018 _compile->record_failure(C2Compiler::retry_no_iterative_escape_analysis());
5019 } else {
5020 _compile->record_failure(C2Compiler::retry_no_escape_analysis());
5021 }
5022 return;
5023 }
5024
5025 igvn->hash_insert(nmm);
5026 record_for_optimizer(nmm);
5027 }
5028
5029 _compile->print_method(PHASE_EA_AFTER_SPLIT_UNIQUE_TYPES_3, 5);
5030
5031 // Phase 4: Update the inputs of non-instance memory Phis and
5032 // the Memory input of memnodes
5033 // First update the inputs of any non-instance Phi's from
5034 // which we split out an instance Phi. Note we don't have
5035 // to recursively process Phi's encountered on the input memory
5036 // chains as is done in split_memory_phi() since they will
5037 // also be processed here.
5038 for (uint j = 0; j < orig_phis.size(); j++) {
5039 PhiNode* phi = orig_phis.at(j)->as_Phi();
5040 int alias_idx = _compile->get_alias_index(phi->adr_type());
5041 igvn->hash_delete(phi);
5042 for (uint i = 1; i < phi->req(); i++) {
5043 Node *mem = phi->in(i);
5044 Node *new_mem = find_inst_mem(mem, alias_idx, orig_phis);
5045 if (_compile->failing()) {
5046 return;
5047 }
5048 if (mem != new_mem) {
5049 phi->set_req(i, new_mem);
5050 }
5051 }
5052 igvn->hash_insert(phi);
5053 record_for_optimizer(phi);
5054 }
5055
5056 // 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/arraycopynode.hpp"
34 #include "opto/c2compiler.hpp"
35 #include "opto/callnode.hpp"
36 #include "opto/castnode.hpp"
37 #include "opto/cfgnode.hpp"
38 #include "opto/compile.hpp"
39 #include "opto/escape.hpp"
40 #include "opto/inlinetypenode.hpp"
41 #include "opto/locknode.hpp"
42 #include "opto/macro.hpp"
43 #include "opto/movenode.hpp"
44 #include "opto/narrowptrnode.hpp"
45 #include "opto/phaseX.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),
60 _compile(C),
152 GrowableArray<SafePointNode*> sfn_worklist;
153 GrowableArray<MergeMemNode*> mergemem_worklist;
154 DEBUG_ONLY( GrowableArray<Node*> addp_worklist; )
155
156 { Compile::TracePhase tp(Phase::_t_connectionGraph);
157
158 // 1. Populate Connection Graph (CG) with PointsTo nodes.
159 ideal_nodes.map(C->live_nodes(), nullptr); // preallocate space
160 // Initialize worklist
161 if (C->root() != nullptr) {
162 ideal_nodes.push(C->root());
163 }
164 // Processed ideal nodes are unique on ideal_nodes list
165 // but several ideal nodes are mapped to the phantom_obj.
166 // To avoid duplicated entries on the following worklists
167 // add the phantom_obj only once to them.
168 ptnodes_worklist.append(phantom_obj);
169 java_objects_worklist.append(phantom_obj);
170 for( uint next = 0; next < ideal_nodes.size(); ++next ) {
171 Node* n = ideal_nodes.at(next);
172 if ((n->Opcode() == Op_LoadX || n->Opcode() == Op_StoreX) &&
173 !n->in(MemNode::Address)->is_AddP() &&
174 _igvn->type(n->in(MemNode::Address))->isa_oopptr()) {
175 // Load/Store at mark work address is at offset 0 so has no AddP which confuses EA
176 Node* addp = AddPNode::make_with_base(n->in(MemNode::Address), n->in(MemNode::Address), _igvn->MakeConX(0));
177 _igvn->register_new_node_with_optimizer(addp);
178 _igvn->replace_input_of(n, MemNode::Address, addp);
179 ideal_nodes.push(addp);
180 _nodes.at_put_grow(addp->_idx, nullptr, nullptr);
181 }
182 // Create PointsTo nodes and add them to Connection Graph. Called
183 // only once per ideal node since ideal_nodes is Unique_Node list.
184 add_node_to_connection_graph(n, &delayed_worklist);
185 PointsToNode* ptn = ptnode_adr(n->_idx);
186 if (ptn != nullptr && ptn != phantom_obj) {
187 ptnodes_worklist.append(ptn);
188 if (ptn->is_JavaObject()) {
189 java_objects_worklist.append(ptn->as_JavaObject());
190 if ((n->is_Allocate() || n->is_CallStaticJava()) &&
191 (ptn->escape_state() < PointsToNode::GlobalEscape)) {
192 // Only allocations and java static calls results are interesting.
193 non_escaped_allocs_worklist.append(ptn->as_JavaObject());
194 }
195 } else if (ptn->is_Field() && ptn->as_Field()->is_oop()) {
196 oop_fields_worklist.append(ptn->as_Field());
197 }
198 }
199 // Collect some interesting nodes for further use.
200 switch (n->Opcode()) {
201 case Op_MergeMem:
419 // scalar replaceable objects.
420 split_unique_types(alloc_worklist, arraycopy_worklist, mergemem_worklist, reducible_merges);
421 if (C->failing()) {
422 NOT_PRODUCT(escape_state_statistics(java_objects_worklist);)
423 return false;
424 }
425
426 #ifdef ASSERT
427 } else if (Verbose && (PrintEscapeAnalysis || PrintEliminateAllocations)) {
428 tty->print("=== No allocations eliminated for ");
429 C->method()->print_short_name();
430 if (!EliminateAllocations) {
431 tty->print(" since EliminateAllocations is off ===");
432 } else if(!has_scalar_replaceable_candidates) {
433 tty->print(" since there are no scalar replaceable candidates ===");
434 }
435 tty->cr();
436 #endif
437 }
438
439 // 6. Expand flat accesses if the object does not escape. This adds nodes to
440 // the graph, so it has to be after split_unique_types. This expands atomic
441 // mismatched accesses (though encapsulated in LoadFlats and StoreFlats) into
442 // non-mismatched accesses, so it is better before reduce allocation merges.
443 if (has_non_escaping_obj) {
444 optimize_flat_accesses(sfn_worklist);
445 }
446
447 _compile->print_method(PHASE_EA_AFTER_SPLIT_UNIQUE_TYPES, 4);
448
449 // 7. Reduce allocation merges used as debug information. This is done after
450 // split_unique_types because the methods used to create SafePointScalarObject
451 // need to traverse the memory graph to find values for object fields. We also
452 // set to null the scalarized inputs of reducible Phis so that the Allocate
453 // that they point can be later scalar replaced.
454 bool delay = _igvn->delay_transform();
455 _igvn->set_delay_transform(true);
456 for (uint i = 0; i < reducible_merges.size(); i++) {
457 Node* n = reducible_merges.at(i);
458 if (n->outcnt() > 0) {
459 if (!reduce_phi_on_safepoints(n->as_Phi())) {
460 NOT_PRODUCT(escape_state_statistics(java_objects_worklist);)
461 C->record_failure(C2Compiler::retry_no_reduce_allocation_merges());
462 return false;
463 }
464
465 // Now we set the scalar replaceable inputs of ophi to null, which is
466 // the last piece that would prevent it from being scalar replaceable.
467 reset_scalar_replaceable_entries(n->as_Phi());
468 }
469 }
1328 // (2) A selector, used to decide if we need to rematerialize an object
1329 // or use the pointer to a NSR object.
1330 // See more details of these fields in the declaration of SafePointScalarMergeNode.
1331 // It is safe to include them into debug info straight away since create_scalarized_object_description()
1332 // will include all newly added inputs into debug info anyway.
1333 sfpt->add_req(nsr_merge_pointer);
1334 sfpt->add_req(selector);
1335 sfpt->jvms()->set_endoff(sfpt->req());
1336
1337 for (uint i = 1; i < ophi->req(); i++) {
1338 Node* base = ophi->in(i);
1339 JavaObjectNode* ptn = unique_java_object(base);
1340
1341 // If the base is not scalar replaceable we don't need to register information about
1342 // it at this time.
1343 if (ptn == nullptr || !ptn->scalar_replaceable()) {
1344 continue;
1345 }
1346
1347 AllocateNode* alloc = ptn->ideal_node()->as_Allocate();
1348 Unique_Node_List value_worklist;
1349 #ifdef ASSERT
1350 const Type* res_type = alloc->result_cast()->bottom_type();
1351 if (res_type->is_inlinetypeptr() && !Compile::current()->has_circular_inline_type()) {
1352 assert(!ophi->as_Phi()->can_push_inline_types_down(_igvn), "missed earlier scalarization opportunity");
1353 }
1354 #endif
1355 SafePointScalarObjectNode* sobj = mexp.create_scalarized_object_description(alloc, sfpt, &value_worklist);
1356 if (sobj == nullptr) {
1357 _compile->record_failure(C2Compiler::retry_no_reduce_allocation_merges());
1358 sfpt->restore_non_debug_edges(non_debug_edges_worklist);
1359 return false; // non-recoverable failure; recompile
1360 }
1361
1362 // Now make a pass over the debug information replacing any references
1363 // to the allocated object with "sobj"
1364 Node* ccpp = alloc->result_cast();
1365 sfpt->replace_edges_in_range(ccpp, sobj, debug_start, jvms->debug_end(), _igvn);
1366 non_debug_edges_worklist.remove_edge_if_present(ccpp); // drop scalarized input from non-debug info
1367
1368 // Register the scalarized object as a candidate for reallocation
1369 smerge->add_req(sobj);
1370
1371 // Scalarize inline types that were added to the safepoint.
1372 // Don't allow linking a constant oop (if available) for flat array elements
1373 // because Deoptimization::reassign_flat_array_elements needs field values.
1374 const bool allow_oop = !merge_t->is_flat();
1375 for (uint j = 0; j < value_worklist.size(); ++j) {
1376 InlineTypeNode* vt = value_worklist.at(j)->as_InlineType();
1377 vt->make_scalar_in_safepoints(_igvn, allow_oop);
1378 }
1379 }
1380
1381 // Replaces debug information references to "original_sfpt_parent" in "sfpt" with references to "smerge"
1382 sfpt->replace_edges_in_range(original_sfpt_parent, smerge, debug_start, jvms->debug_end(), _igvn);
1383 non_debug_edges_worklist.remove_edge_if_present(original_sfpt_parent); // drop scalarized input from non-debug info
1384
1385 // The call to 'replace_edges_in_range' above might have removed the
1386 // reference to ophi that we need at _merge_pointer_idx. The line below make
1387 // sure the reference is maintained.
1388 sfpt->set_req(smerge->merge_pointer_idx(jvms), nsr_merge_pointer);
1389
1390 sfpt->restore_non_debug_edges(non_debug_edges_worklist);
1391
1392 _igvn->_worklist.push(sfpt);
1393 }
1394
1395 return true;
1396 }
1397
1398 void ConnectionGraph::reduce_phi(PhiNode* ophi, GrowableArray<Node*> &alloc_worklist) {
1565 return false;
1566 }
1567
1568 // Returns true if at least one of the arguments to the call is an object
1569 // that does not escape globally.
1570 bool ConnectionGraph::has_arg_escape(CallJavaNode* call) {
1571 if (call->method() != nullptr) {
1572 uint max_idx = TypeFunc::Parms + call->method()->arg_size();
1573 for (uint idx = TypeFunc::Parms; idx < max_idx; idx++) {
1574 Node* p = call->in(idx);
1575 if (not_global_escape(p)) {
1576 return true;
1577 }
1578 }
1579 } else {
1580 const char* name = call->as_CallStaticJava()->_name;
1581 assert(name != nullptr, "no name");
1582 // no arg escapes through uncommon traps
1583 if (strcmp(name, "uncommon_trap") != 0) {
1584 // process_call_arguments() assumes that all arguments escape globally
1585 const TypeTuple* d = call->tf()->domain_sig();
1586 for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
1587 const Type* at = d->field_at(i);
1588 if (at->isa_oopptr() != nullptr) {
1589 return true;
1590 }
1591 }
1592 }
1593 }
1594 return false;
1595 }
1596
1597
1598
1599 // Utility function for nodes that load an object
1600 void ConnectionGraph::add_objload_to_connection_graph(Node *n, Unique_Node_List *delayed_worklist) {
1601 // Using isa_ptr() instead of isa_oopptr() for LoadP and Phi because
1602 // ThreadLocal has RawPtr type.
1603 const Type* t = _igvn->type(n);
1604 if (t->make_ptr() != nullptr) {
1605 Node* adr = n->in(MemNode::Address);
1606 #ifdef ASSERT
1607 if (!adr->is_AddP()) {
1608 assert(_igvn->type(adr)->isa_rawptr(), "sanity");
1609 } else {
1610 assert((ptnode_adr(adr->_idx) == nullptr ||
1611 ptnode_adr(adr->_idx)->as_Field()->is_oop()), "sanity");
1612 }
1613 #endif
1614 add_local_var_and_edge(n, PointsToNode::NoEscape,
1615 adr, delayed_worklist);
1616 }
1617 }
1618
1619 void ConnectionGraph::add_proj(Node* n, Unique_Node_List* delayed_worklist) {
1620 if (n->as_Proj()->_con == TypeFunc::Parms && n->in(0)->is_Call() && n->in(0)->as_Call()->returns_pointer()) {
1621 add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(0), delayed_worklist);
1622 } else if (n->in(0)->is_LoadFlat()) {
1623 // Treat LoadFlat outputs similar to a call return value
1624 add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(0), delayed_worklist);
1625 } else if (n->as_Proj()->_con >= TypeFunc::Parms && n->in(0)->is_Call() && n->bottom_type()->isa_ptr()) {
1626 CallNode* call = n->in(0)->as_Call();
1627 assert(call->tf()->returns_inline_type_as_fields(), "");
1628 if (n->as_Proj()->_con == TypeFunc::Parms || !returns_an_argument(call)) {
1629 // either:
1630 // - not an argument returned
1631 // - the returned buffer for a returned scalarized argument
1632 add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(0), delayed_worklist);
1633 } else {
1634 add_local_var(n, PointsToNode::NoEscape);
1635 }
1636 }
1637 }
1638
1639 // Populate Connection Graph with PointsTo nodes and create simple
1640 // connection graph edges.
1641 void ConnectionGraph::add_node_to_connection_graph(Node *n, Unique_Node_List *delayed_worklist) {
1642 assert(!_verify, "this method should not be called for verification");
1643 PhaseGVN* igvn = _igvn;
1644 uint n_idx = n->_idx;
1645 PointsToNode* n_ptn = ptnode_adr(n_idx);
1646 if (n_ptn != nullptr) {
1647 return; // No need to redefine PointsTo node during first iteration.
1648 }
1649 int opcode = n->Opcode();
1650 bool gc_handled = BarrierSet::barrier_set()->barrier_set_c2()->escape_add_to_con_graph(this, igvn, delayed_worklist, n, opcode);
1651 if (gc_handled) {
1652 return; // Ignore node if already handled by GC.
1653 }
1654
1655 if (n->is_Call()) {
1656 // Arguments to allocation and locking don't escape.
1657 if (n->is_AbstractLock()) {
1658 // Put Lock and Unlock nodes on IGVN worklist to process them during
1659 // first IGVN optimization when escape information is still available.
1660 record_for_optimizer(n);
1661 } else if (n->is_Allocate()) {
1662 add_call_node(n->as_Call());
1663 record_for_optimizer(n);
1664 } else {
1665 if (n->is_CallStaticJava()) {
1666 const char* name = n->as_CallStaticJava()->_name;
1667 if (name != nullptr && strcmp(name, "uncommon_trap") == 0) {
1668 return; // Skip uncommon traps
1669 }
1670 }
1671 // Don't mark as processed since call's arguments have to be processed.
1672 delayed_worklist->push(n);
1673 // Check if a call returns an object.
1674 if ((n->as_Call()->returns_pointer() &&
1675 n->as_Call()->proj_out_or_null(TypeFunc::Parms) != nullptr) ||
1676 (n->is_CallStaticJava() &&
1677 n->as_CallStaticJava()->is_boxing_method())) {
1678 add_call_node(n->as_Call());
1679 } else if (n->as_Call()->tf()->returns_inline_type_as_fields()) {
1680 bool returns_oop = false;
1681 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax && !returns_oop; i++) {
1682 ProjNode* pn = n->fast_out(i)->as_Proj();
1683 if (pn->_con >= TypeFunc::Parms && pn->bottom_type()->isa_ptr()) {
1684 returns_oop = true;
1685 }
1686 }
1687 if (returns_oop) {
1688 add_call_node(n->as_Call());
1689 }
1690 }
1691 }
1692 return;
1693 }
1694 // Put this check here to process call arguments since some call nodes
1695 // point to phantom_obj.
1696 if (n_ptn == phantom_obj || n_ptn == null_obj) {
1697 return; // Skip predefined nodes.
1698 }
1699 switch (opcode) {
1700 case Op_AddP: {
1701 Node* base = get_addp_base(n);
1702 PointsToNode* ptn_base = ptnode_adr(base->_idx);
1703 // Field nodes are created for all field types. They are used in
1704 // adjust_scalar_replaceable_state() and split_unique_types().
1705 // Note, non-oop fields will have only base edges in Connection
1706 // Graph because such fields are not used for oop loads and stores.
1707 int offset = address_offset(n, igvn);
1708 add_field(n, PointsToNode::NoEscape, offset);
1709 if (ptn_base == nullptr) {
1710 delayed_worklist->push(n); // Process it later.
1711 } else {
1712 n_ptn = ptnode_adr(n_idx);
1713 add_base(n_ptn->as_Field(), ptn_base);
1714 }
1715 break;
1716 }
1717 case Op_CastX2P:
1718 case Op_CastI2N: {
1719 map_ideal_node(n, phantom_obj);
1720 break;
1721 }
1722 case Op_InlineType:
1723 case Op_CastPP:
1724 case Op_CheckCastPP:
1725 case Op_EncodeP:
1726 case Op_DecodeN:
1727 case Op_EncodePKlass:
1728 case Op_DecodeNKlass: {
1729 add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(1), delayed_worklist);
1730 break;
1731 }
1732 case Op_CMoveP: {
1733 add_local_var(n, PointsToNode::NoEscape);
1734 // Do not add edges during first iteration because some could be
1735 // not defined yet.
1736 delayed_worklist->push(n);
1737 break;
1738 }
1739 case Op_ConP:
1740 case Op_ConN:
1741 case Op_ConNKlass: {
1742 // assume all oop constants globally escape except for null
1772 break;
1773 }
1774 case Op_PartialSubtypeCheck: {
1775 // Produces Null or notNull and is used in only in CmpP so
1776 // phantom_obj could be used.
1777 map_ideal_node(n, phantom_obj); // Result is unknown
1778 break;
1779 }
1780 case Op_Phi: {
1781 // Using isa_ptr() instead of isa_oopptr() for LoadP and Phi because
1782 // ThreadLocal has RawPtr type.
1783 const Type* t = n->as_Phi()->type();
1784 if (t->make_ptr() != nullptr) {
1785 add_local_var(n, PointsToNode::NoEscape);
1786 // Do not add edges during first iteration because some could be
1787 // not defined yet.
1788 delayed_worklist->push(n);
1789 }
1790 break;
1791 }
1792 case Op_LoadFlat:
1793 // Treat LoadFlat similar to an unknown call that receives nothing and produces its results
1794 map_ideal_node(n, phantom_obj);
1795 break;
1796 case Op_StoreFlat:
1797 // Treat StoreFlat similar to a call that escapes the stored flattened fields
1798 delayed_worklist->push(n);
1799 break;
1800 case Op_Proj: {
1801 // we are only interested in the oop result projection from a call
1802 add_proj(n, delayed_worklist);
1803 break;
1804 }
1805 case Op_Rethrow: // Exception object escapes
1806 case Op_Return: {
1807 if (n->req() > TypeFunc::Parms &&
1808 igvn->type(n->in(TypeFunc::Parms))->isa_oopptr()) {
1809 // Treat Return value as LocalVar with GlobalEscape escape state.
1810 add_local_var_and_edge(n, PointsToNode::GlobalEscape, n->in(TypeFunc::Parms), delayed_worklist);
1811 }
1812 break;
1813 }
1814 case Op_CompareAndExchangeP:
1815 case Op_CompareAndExchangeN:
1816 case Op_GetAndSetP:
1817 case Op_GetAndSetN: {
1818 add_objload_to_connection_graph(n, delayed_worklist);
1819 // fall-through
1820 }
1821 case Op_StoreP:
1822 case Op_StoreN:
1866 break;
1867 }
1868 default:
1869 ; // Do nothing for nodes not related to EA.
1870 }
1871 return;
1872 }
1873
1874 // Add final simple edges to graph.
1875 void ConnectionGraph::add_final_edges(Node *n) {
1876 PointsToNode* n_ptn = ptnode_adr(n->_idx);
1877 #ifdef ASSERT
1878 if (_verify && n_ptn->is_JavaObject())
1879 return; // This method does not change graph for JavaObject.
1880 #endif
1881
1882 if (n->is_Call()) {
1883 process_call_arguments(n->as_Call());
1884 return;
1885 }
1886 assert(n->is_Store() || n->is_LoadStore() || n->is_StoreFlat() ||
1887 ((n_ptn != nullptr) && (n_ptn->ideal_node() != nullptr)),
1888 "node should be registered already");
1889 int opcode = n->Opcode();
1890 bool gc_handled = BarrierSet::barrier_set()->barrier_set_c2()->escape_add_final_edges(this, _igvn, n, opcode);
1891 if (gc_handled) {
1892 return; // Ignore node if already handled by GC.
1893 }
1894 switch (opcode) {
1895 case Op_AddP: {
1896 Node* base = get_addp_base(n);
1897 PointsToNode* ptn_base = ptnode_adr(base->_idx);
1898 assert(ptn_base != nullptr, "field's base should be registered");
1899 add_base(n_ptn->as_Field(), ptn_base);
1900 break;
1901 }
1902 case Op_InlineType:
1903 case Op_CastPP:
1904 case Op_CheckCastPP:
1905 case Op_EncodeP:
1906 case Op_DecodeN:
1907 case Op_EncodePKlass:
1908 case Op_DecodeNKlass: {
1909 add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(1), nullptr);
1910 break;
1911 }
1912 case Op_CMoveP: {
1913 for (uint i = CMoveNode::IfFalse; i < n->req(); i++) {
1914 Node* in = n->in(i);
1915 if (in == nullptr) {
1916 continue; // ignore null
1917 }
1918 Node* uncast_in = in->uncast();
1919 if (uncast_in->is_top() || uncast_in == n) {
1920 continue; // ignore top or inputs which go back this node
1921 }
1922 PointsToNode* ptn = ptnode_adr(in->_idx);
1935 }
1936 case Op_Phi: {
1937 // Using isa_ptr() instead of isa_oopptr() for LoadP and Phi because
1938 // ThreadLocal has RawPtr type.
1939 assert(n->as_Phi()->type()->make_ptr() != nullptr, "Unexpected node type");
1940 for (uint i = 1; i < n->req(); i++) {
1941 Node* in = n->in(i);
1942 if (in == nullptr) {
1943 continue; // ignore null
1944 }
1945 Node* uncast_in = in->uncast();
1946 if (uncast_in->is_top() || uncast_in == n) {
1947 continue; // ignore top or inputs which go back this node
1948 }
1949 PointsToNode* ptn = ptnode_adr(in->_idx);
1950 assert(ptn != nullptr, "node should be registered");
1951 add_edge(n_ptn, ptn);
1952 }
1953 break;
1954 }
1955 case Op_StoreFlat: {
1956 // StoreFlat globally escapes its stored flattened fields
1957 InlineTypeNode* value = n->as_StoreFlat()->value();
1958 ciInlineKlass* vk = _igvn->type(value)->inline_klass();
1959 for (int i = 0; i < vk->nof_nonstatic_fields(); i++) {
1960 ciField* field = vk->nonstatic_field_at(i);
1961 if (field->type()->is_primitive_type()) {
1962 continue;
1963 }
1964
1965 Node* field_value = value->field_value_by_offset(field->offset_in_bytes(), true);
1966 PointsToNode* field_value_ptn = ptnode_adr(field_value->_idx);
1967 set_escape_state(field_value_ptn, PointsToNode::GlobalEscape NOT_PRODUCT(COMMA "store into a flat field"));
1968 }
1969 break;
1970 }
1971 case Op_Proj: {
1972 add_proj(n, nullptr);
1973 break;
1974 }
1975 case Op_Rethrow: // Exception object escapes
1976 case Op_Return: {
1977 assert(n->req() > TypeFunc::Parms && _igvn->type(n->in(TypeFunc::Parms))->isa_oopptr(),
1978 "Unexpected node type");
1979 // Treat Return value as LocalVar with GlobalEscape escape state.
1980 add_local_var_and_edge(n, PointsToNode::GlobalEscape, n->in(TypeFunc::Parms), nullptr);
1981 break;
1982 }
1983 case Op_CompareAndExchangeP:
1984 case Op_CompareAndExchangeN:
1985 case Op_GetAndSetP:
1986 case Op_GetAndSetN:{
1987 assert(_igvn->type(n)->make_ptr() != nullptr, "Unexpected node type");
1988 add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(MemNode::Address), nullptr);
1989 // fall-through
1990 }
1991 case Op_CompareAndSwapP:
1992 case Op_CompareAndSwapN:
2126 Node* val = n->in(MemNode::ValueIn);
2127 PointsToNode* ptn = ptnode_adr(val->_idx);
2128 assert(ptn != nullptr, "node should be registered");
2129 set_escape_state(ptn, PointsToNode::GlobalEscape NOT_PRODUCT(COMMA "stored at raw address"));
2130 // Add edge to object for unsafe access with offset.
2131 PointsToNode* adr_ptn = ptnode_adr(adr->_idx);
2132 assert(adr_ptn != nullptr, "node should be registered");
2133 if (adr_ptn->is_Field()) {
2134 assert(adr_ptn->as_Field()->is_oop(), "should be oop field");
2135 add_edge(adr_ptn, ptn);
2136 }
2137 return true;
2138 }
2139 #ifdef ASSERT
2140 n->dump(1);
2141 assert(false, "not unsafe");
2142 #endif
2143 return false;
2144 }
2145
2146 // Iterate over the domains for the scalarized and non scalarized calling conventions: Only move to the next element
2147 // in the non scalarized calling convention once all elements of the scalarized calling convention for that parameter
2148 // have been iterated over. So (ignoring hidden arguments such as the null marker) iterating over:
2149 // value class MyValue {
2150 // int f1;
2151 // float f2;
2152 // }
2153 // void m(Object o, MyValue v, int i)
2154 // produces the pairs:
2155 // (Object, Object), (Myvalue, int), (MyValue, float), (int, int)
2156 class DomainIterator : public StackObj {
2157 private:
2158 const TypeTuple* _domain;
2159 const TypeTuple* _domain_cc;
2160 const GrowableArray<SigEntry>* _sig_cc;
2161
2162 uint _i_domain;
2163 uint _i_domain_cc;
2164 int _i_sig_cc;
2165 uint _depth;
2166 uint _first_field_pos;
2167 const bool _is_static;
2168
2169 void next_helper() {
2170 if (_sig_cc == nullptr) {
2171 return;
2172 }
2173 BasicType prev_bt = _i_sig_cc > 0 ? _sig_cc->at(_i_sig_cc-1)._bt : T_ILLEGAL;
2174 BasicType prev_prev_bt = _i_sig_cc > 1 ? _sig_cc->at(_i_sig_cc-2)._bt : T_ILLEGAL;
2175 while (_i_sig_cc < _sig_cc->length()) {
2176 BasicType bt = _sig_cc->at(_i_sig_cc)._bt;
2177 assert(bt != T_VOID || _sig_cc->at(_i_sig_cc-1)._bt == prev_bt, "incorrect prev bt");
2178 if (bt == T_METADATA) {
2179 if (_depth == 0) {
2180 _first_field_pos = _i_domain_cc;
2181 }
2182 _depth++;
2183 } else if (bt == T_VOID && (prev_bt != T_LONG && prev_bt != T_DOUBLE)) {
2184 _depth--;
2185 if (_depth == 0) {
2186 _i_domain++;
2187 }
2188 } else if (bt == T_OBJECT && prev_bt == T_METADATA && (_is_static || _i_domain > 0) && _sig_cc->at(_i_sig_cc)._offset == 0) {
2189 assert(_sig_cc->at(_i_sig_cc)._vt_oop, "buffer expected right after T_METADATA");
2190 assert(_depth == 1, "only root value has buffer");
2191 _i_domain_cc++;
2192 _first_field_pos = _i_domain_cc;
2193 } else if (bt == T_BOOLEAN && prev_prev_bt == T_METADATA && (_is_static || _i_domain > 0) && _sig_cc->at(_i_sig_cc)._offset == -1) {
2194 assert(_sig_cc->at(_i_sig_cc)._null_marker, "null marker expected right after T_METADATA");
2195 assert(_depth == 1, "only root value null marker");
2196 _i_domain_cc++;
2197 _first_field_pos = _i_domain_cc;
2198 } else {
2199 return;
2200 }
2201 prev_prev_bt = prev_bt;
2202 prev_bt = bt;
2203 _i_sig_cc++;
2204 }
2205 }
2206
2207 public:
2208
2209 DomainIterator(CallJavaNode* call) :
2210 _domain(call->tf()->domain_sig()),
2211 _domain_cc(call->tf()->domain_cc()),
2212 _sig_cc(call->method()->get_sig_cc()),
2213 _i_domain(TypeFunc::Parms),
2214 _i_domain_cc(TypeFunc::Parms),
2215 _i_sig_cc(0),
2216 _depth(0),
2217 _first_field_pos(0),
2218 _is_static(call->method()->is_static()) {
2219 next_helper();
2220 }
2221
2222 bool has_next() const {
2223 assert(_sig_cc == nullptr || (_i_sig_cc < _sig_cc->length()) == (_i_domain < _domain->cnt()), "should reach end in sync");
2224 assert((_i_domain < _domain->cnt()) == (_i_domain_cc < _domain_cc->cnt()), "should reach end in sync");
2225 return _i_domain < _domain->cnt();
2226 }
2227
2228 void next() {
2229 assert(_depth != 0 || _domain->field_at(_i_domain) == _domain_cc->field_at(_i_domain_cc), "should produce same non scalarized elements");
2230 _i_sig_cc++;
2231 if (_depth == 0) {
2232 _i_domain++;
2233 }
2234 _i_domain_cc++;
2235 next_helper();
2236 }
2237
2238 uint i_domain() const {
2239 return _i_domain;
2240 }
2241
2242 uint i_domain_cc() const {
2243 return _i_domain_cc;
2244 }
2245
2246 const Type* current_domain() const {
2247 return _domain->field_at(_i_domain);
2248 }
2249
2250 const Type* current_domain_cc() const {
2251 return _domain_cc->field_at(_i_domain_cc);
2252 }
2253
2254 uint first_field_pos() const {
2255 assert(_first_field_pos >= TypeFunc::Parms, "not yet updated?");
2256 return _first_field_pos;
2257 }
2258 };
2259
2260 // Determine whether any arguments are returned.
2261 bool ConnectionGraph::returns_an_argument(CallNode* call) {
2262 ciMethod* meth = call->as_CallJava()->method();
2263 BCEscapeAnalyzer* call_analyzer = meth->get_bcea();
2264 if (call_analyzer == nullptr) {
2265 return false;
2266 }
2267
2268 const TypeTuple* d = call->tf()->domain_sig();
2269 bool ret_arg = false;
2270 for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
2271 if (d->field_at(i)->isa_ptr() != nullptr &&
2272 call_analyzer->is_arg_returned(i - TypeFunc::Parms)) {
2273 if (meth->is_scalarized_arg(i - TypeFunc::Parms) && !compatible_return(call->as_CallJava(), i)) {
2274 return false;
2275 }
2276 if (call->tf()->returns_inline_type_as_fields() != meth->is_scalarized_arg(i - TypeFunc::Parms)) {
2277 return false;
2278 }
2279 ret_arg = true;
2280 }
2281 }
2282 return ret_arg;
2283 }
2284
2285 void ConnectionGraph::add_call_node(CallNode* call) {
2286 assert(call->returns_pointer() || call->tf()->returns_inline_type_as_fields(), "only for call which returns pointer");
2287 uint call_idx = call->_idx;
2288 if (call->is_Allocate()) {
2289 Node* k = call->in(AllocateNode::KlassNode);
2290 const TypeKlassPtr* kt = k->bottom_type()->isa_klassptr();
2291 assert(kt != nullptr, "TypeKlassPtr required.");
2292 PointsToNode::EscapeState es = PointsToNode::NoEscape;
2293 bool scalar_replaceable = true;
2294 NOT_PRODUCT(const char* nsr_reason = "");
2295 if (call->is_AllocateArray()) {
2296 if (!kt->isa_aryklassptr()) { // StressReflectiveCode
2297 es = PointsToNode::GlobalEscape;
2298 } else {
2299 int length = call->in(AllocateNode::ALength)->find_int_con(-1);
2300 if (length < 0) {
2301 // Not scalar replaceable if the length is not constant.
2302 scalar_replaceable = false;
2303 NOT_PRODUCT(nsr_reason = "has a non-constant length");
2304 } else if (length > EliminateAllocationArraySizeLimit) {
2305 // Not scalar replaceable if the length is too big.
2306 scalar_replaceable = false;
2341 // - mapped to GlobalEscape JavaObject node if oop is returned;
2342 //
2343 // - all oop arguments are escaping globally;
2344 //
2345 // 2. CallStaticJavaNode (execute bytecode analysis if possible):
2346 //
2347 // - the same as CallDynamicJavaNode if can't do bytecode analysis;
2348 //
2349 // - mapped to GlobalEscape JavaObject node if unknown oop is returned;
2350 // - mapped to NoEscape JavaObject node if non-escaping object allocated
2351 // during call is returned;
2352 // - mapped to ArgEscape LocalVar node pointed to object arguments
2353 // which are returned and does not escape during call;
2354 //
2355 // - oop arguments escaping status is defined by bytecode analysis;
2356 //
2357 // For a static call, we know exactly what method is being called.
2358 // Use bytecode estimator to record whether the call's return value escapes.
2359 ciMethod* meth = call->as_CallJava()->method();
2360 if (meth == nullptr) {
2361 const char* name = call->as_CallStaticJava()->_name;
2362 assert(call->as_CallStaticJava()->is_call_to_multianewarray_stub() ||
2363 strncmp(name, "load_unknown_inline", 19) == 0 ||
2364 strncmp(name, "store_inline_type_fields_to_buf", 31) == 0, "TODO: add failed case check");
2365 // Returns a newly allocated non-escaped object.
2366 add_java_object(call, PointsToNode::NoEscape);
2367 set_not_scalar_replaceable(ptnode_adr(call_idx) NOT_PRODUCT(COMMA "is result of multinewarray"));
2368 } else if (meth->is_boxing_method()) {
2369 // Returns boxing object
2370 PointsToNode::EscapeState es;
2371 vmIntrinsics::ID intr = meth->intrinsic_id();
2372 if (intr == vmIntrinsics::_floatValue || intr == vmIntrinsics::_doubleValue) {
2373 // It does not escape if object is always allocated.
2374 es = PointsToNode::NoEscape;
2375 } else {
2376 // It escapes globally if object could be loaded from cache.
2377 es = PointsToNode::GlobalEscape;
2378 }
2379 add_java_object(call, es);
2380 if (es == PointsToNode::GlobalEscape) {
2381 set_not_scalar_replaceable(ptnode_adr(call->_idx) NOT_PRODUCT(COMMA "object can be loaded from boxing cache"));
2382 }
2383 } else {
2384 BCEscapeAnalyzer* call_analyzer = meth->get_bcea();
2385 call_analyzer->copy_dependencies(_compile->dependencies());
2386 if (call_analyzer->is_return_allocated()) {
2387 // Returns a newly allocated non-escaped object, simply
2388 // update dependency information.
2389 // Mark it as NoEscape so that objects referenced by
2390 // it's fields will be marked as NoEscape at least.
2391 add_java_object(call, PointsToNode::NoEscape);
2392 set_not_scalar_replaceable(ptnode_adr(call_idx) NOT_PRODUCT(COMMA "is result of call"));
2393 } else {
2394 // For non scalarized argument/return: add_proj() adds an edge between the return projection and the call,
2395 // process_call_arguments() adds an edge between the call and the argument
2396 // For scalarized argument/return: process_call_arguments() adds an edge between a call projection for a field
2397 // and the argument input to the call for that field. An edge is added between the projection for the returned
2398 // buffer and the call.
2399 if (returns_an_argument(call) && !call->tf()->returns_inline_type_as_fields()) {
2400 // returns non scalarized argument
2401 add_local_var(call, PointsToNode::ArgEscape);
2402 } else {
2403 // Returns unknown object or scalarized argument being returned
2404 map_ideal_node(call, phantom_obj);
2405 }
2406 }
2407 }
2408 } else {
2409 // An other type of call, assume the worst case:
2410 // returned value is unknown and globally escapes.
2411 assert(call->Opcode() == Op_CallDynamicJava, "add failed case check");
2412 map_ideal_node(call, phantom_obj);
2413 }
2414 }
2415
2416 // Check that the return type is compatible with the type of the argument being returned i.e. that there's no cast that
2417 // fails in the method
2418 bool ConnectionGraph::compatible_return(CallJavaNode* call, uint k) {
2419 return call->tf()->domain_sig()->field_at(k)->is_instptr()->instance_klass() == call->tf()->range_sig()->field_at(TypeFunc::Parms)->is_instptr()->instance_klass();
2420 }
2421
2422 void ConnectionGraph::process_call_arguments(CallNode *call) {
2423 bool is_arraycopy = false;
2424 switch (call->Opcode()) {
2425 #ifdef ASSERT
2426 case Op_Allocate:
2427 case Op_AllocateArray:
2428 case Op_Lock:
2429 case Op_Unlock:
2430 assert(false, "should be done already");
2431 break;
2432 #endif
2433 case Op_ArrayCopy:
2434 case Op_CallLeafNoFP:
2435 // Most array copies are ArrayCopy nodes at this point but there
2436 // are still a few direct calls to the copy subroutines (See
2437 // PhaseStringOpts::copy_string())
2438 is_arraycopy = (call->Opcode() == Op_ArrayCopy) ||
2439 call->as_CallLeaf()->is_call_to_arraycopystub();
2440 // fall through
2441 case Op_CallLeafVector:
2442 case Op_CallLeaf: {
2443 // Stub calls, objects do not escape but they are not scale replaceable.
2444 // Adjust escape state for outgoing arguments.
2445 const TypeTuple * d = call->tf()->domain_sig();
2446 bool src_has_oops = false;
2447 for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
2448 const Type* at = d->field_at(i);
2449 Node *arg = call->in(i);
2450 if (arg == nullptr) {
2451 continue;
2452 }
2453 const Type *aat = _igvn->type(arg);
2454 if (arg->is_top() || !at->isa_ptr() || !aat->isa_ptr()) {
2455 continue;
2456 }
2457 if (arg->is_AddP()) {
2458 //
2459 // The inline_native_clone() case when the arraycopy stub is called
2460 // after the allocation before Initialize and CheckCastPP nodes.
2461 // Or normal arraycopy for object arrays case.
2462 //
2463 // Set AddP's base (Allocate) as not scalar replaceable since
2464 // pointer to the base (with offset) is passed as argument.
2465 //
2466 arg = get_addp_base(arg);
2467 }
2468 PointsToNode* arg_ptn = ptnode_adr(arg->_idx);
2469 assert(arg_ptn != nullptr, "should be registered");
2470 PointsToNode::EscapeState arg_esc = arg_ptn->escape_state();
2471 if (is_arraycopy || arg_esc < PointsToNode::ArgEscape) {
2472 assert(aat == Type::TOP || aat == TypePtr::NULL_PTR ||
2473 aat->isa_ptr() != nullptr, "expecting an Ptr");
2474 bool arg_has_oops = aat->isa_oopptr() &&
2475 (aat->isa_instptr() ||
2476 (aat->isa_aryptr() && (aat->isa_aryptr()->elem() == Type::BOTTOM || aat->isa_aryptr()->elem()->make_oopptr() != nullptr)) ||
2477 (aat->isa_aryptr() && aat->isa_aryptr()->elem() != nullptr &&
2478 aat->isa_aryptr()->is_flat() &&
2479 aat->isa_aryptr()->elem()->inline_klass()->contains_oops()));
2480 if (i == TypeFunc::Parms) {
2481 src_has_oops = arg_has_oops;
2482 }
2483 //
2484 // src or dst could be j.l.Object when other is basic type array:
2485 //
2486 // arraycopy(char[],0,Object*,0,size);
2487 // arraycopy(Object*,0,char[],0,size);
2488 //
2489 // Don't add edges in such cases.
2490 //
2491 bool arg_is_arraycopy_dest = src_has_oops && is_arraycopy &&
2492 arg_has_oops && (i > TypeFunc::Parms);
2493 #ifdef ASSERT
2494 if (!(is_arraycopy ||
2495 BarrierSet::barrier_set()->barrier_set_c2()->is_gc_barrier_node(call) ||
2496 (call->as_CallLeaf()->_name != nullptr &&
2497 (strcmp(call->as_CallLeaf()->_name, "updateBytesCRC32") == 0 ||
2498 strcmp(call->as_CallLeaf()->_name, "updateBytesCRC32C") == 0 ||
2499 strcmp(call->as_CallLeaf()->_name, "updateBytesAdler32") == 0 ||
2526 strcmp(call->as_CallLeaf()->_name, "dilithiumDecomposePoly") == 0 ||
2527 strcmp(call->as_CallLeaf()->_name, "encodeBlock") == 0 ||
2528 strcmp(call->as_CallLeaf()->_name, "decodeBlock") == 0 ||
2529 strcmp(call->as_CallLeaf()->_name, "md5_implCompress") == 0 ||
2530 strcmp(call->as_CallLeaf()->_name, "md5_implCompressMB") == 0 ||
2531 strcmp(call->as_CallLeaf()->_name, "sha1_implCompress") == 0 ||
2532 strcmp(call->as_CallLeaf()->_name, "sha1_implCompressMB") == 0 ||
2533 strcmp(call->as_CallLeaf()->_name, "sha256_implCompress") == 0 ||
2534 strcmp(call->as_CallLeaf()->_name, "sha256_implCompressMB") == 0 ||
2535 strcmp(call->as_CallLeaf()->_name, "sha512_implCompress") == 0 ||
2536 strcmp(call->as_CallLeaf()->_name, "sha512_implCompressMB") == 0 ||
2537 strcmp(call->as_CallLeaf()->_name, "sha3_implCompress") == 0 ||
2538 strcmp(call->as_CallLeaf()->_name, "double_keccak") == 0 ||
2539 strcmp(call->as_CallLeaf()->_name, "quad_keccak") == 0 ||
2540 strcmp(call->as_CallLeaf()->_name, "sha3_implCompressMB") == 0 ||
2541 strcmp(call->as_CallLeaf()->_name, "multiplyToLen") == 0 ||
2542 strcmp(call->as_CallLeaf()->_name, "squareToLen") == 0 ||
2543 strcmp(call->as_CallLeaf()->_name, "mulAdd") == 0 ||
2544 strcmp(call->as_CallLeaf()->_name, "montgomery_multiply") == 0 ||
2545 strcmp(call->as_CallLeaf()->_name, "montgomery_square") == 0 ||
2546 strcmp(call->as_CallLeaf()->_name, "vectorizedMismatch") == 0 ||
2547 strcmp(call->as_CallLeaf()->_name, "load_unknown_inline") == 0 ||
2548 strcmp(call->as_CallLeaf()->_name, "store_unknown_inline") == 0 ||
2549 strcmp(call->as_CallLeaf()->_name, "store_inline_type_fields_to_buf") == 0 ||
2550 strcmp(call->as_CallLeaf()->_name, "bigIntegerRightShiftWorker") == 0 ||
2551 strcmp(call->as_CallLeaf()->_name, "bigIntegerLeftShiftWorker") == 0 ||
2552 strcmp(call->as_CallLeaf()->_name, "vectorizedMismatch") == 0 ||
2553 strcmp(call->as_CallLeaf()->_name, "stringIndexOf") == 0 ||
2554 strcmp(call->as_CallLeaf()->_name, "arraysort_stub") == 0 ||
2555 strcmp(call->as_CallLeaf()->_name, "array_partition_stub") == 0 ||
2556 strcmp(call->as_CallLeaf()->_name, "get_class_id_intrinsic") == 0 ||
2557 strcmp(call->as_CallLeaf()->_name, "unsafe_setmemory") == 0)
2558 ))) {
2559 call->dump();
2560 fatal("EA unexpected CallLeaf %s", call->as_CallLeaf()->_name);
2561 }
2562 #endif
2563 // Always process arraycopy's destination object since
2564 // we need to add all possible edges to references in
2565 // source object.
2566 if (arg_esc >= PointsToNode::ArgEscape &&
2567 !arg_is_arraycopy_dest) {
2568 continue;
2569 }
2592 }
2593 }
2594 }
2595 break;
2596 }
2597 case Op_CallStaticJava: {
2598 // For a static call, we know exactly what method is being called.
2599 // Use bytecode estimator to record the call's escape affects
2600 #ifdef ASSERT
2601 const char* name = call->as_CallStaticJava()->_name;
2602 assert((name == nullptr || strcmp(name, "uncommon_trap") != 0), "normal calls only");
2603 #endif
2604 ciMethod* meth = call->as_CallJava()->method();
2605 if ((meth != nullptr) && meth->is_boxing_method()) {
2606 break; // Boxing methods do not modify any oops.
2607 }
2608 BCEscapeAnalyzer* call_analyzer = (meth !=nullptr) ? meth->get_bcea() : nullptr;
2609 // fall-through if not a Java method or no analyzer information
2610 if (call_analyzer != nullptr) {
2611 PointsToNode* call_ptn = ptnode_adr(call->_idx);
2612 bool ret_arg = returns_an_argument(call);
2613 for (DomainIterator di(call->as_CallJava()); di.has_next(); di.next()) {
2614 int k = di.i_domain() - TypeFunc::Parms;
2615 const Type* at = di.current_domain_cc();
2616 Node* arg = call->in(di.i_domain_cc());
2617 PointsToNode* arg_ptn = ptnode_adr(arg->_idx);
2618 assert(!call_analyzer->is_arg_returned(k) || !meth->is_scalarized_arg(k) ||
2619 !compatible_return(call->as_CallJava(), di.i_domain()) ||
2620 call->proj_out_or_null(di.i_domain_cc() - di.first_field_pos() + TypeFunc::Parms + 1) == nullptr ||
2621 _igvn->type(call->proj_out_or_null(di.i_domain_cc() - di.first_field_pos() + TypeFunc::Parms + 1)) == at,
2622 "scalarized return and scalarized argument should match");
2623 if (at->isa_ptr() != nullptr && call_analyzer->is_arg_returned(k) && ret_arg) {
2624 // The call returns arguments.
2625 if (meth->is_scalarized_arg(k)) {
2626 ProjNode* res_proj = call->proj_out_or_null(di.i_domain_cc() - di.first_field_pos() + TypeFunc::Parms + 1);
2627 if (res_proj != nullptr) {
2628 assert(_igvn->type(res_proj)->isa_ptr(), "scalarized return and scalarized argument should match");
2629 if (res_proj->_con != TypeFunc::Parms) {
2630 // add an edge between the result projection for a field and the argument projection for the same argument field
2631 PointsToNode* proj_ptn = ptnode_adr(res_proj->_idx);
2632 add_edge(proj_ptn, arg_ptn);
2633 if (!call_analyzer->is_return_local()) {
2634 add_edge(proj_ptn, phantom_obj);
2635 }
2636 }
2637 }
2638 } else if (call_ptn != nullptr) { // Is call's result used?
2639 assert(call_ptn->is_LocalVar(), "node should be registered");
2640 assert(arg_ptn != nullptr, "node should be registered");
2641 add_edge(call_ptn, arg_ptn);
2642 }
2643 }
2644 if (at->isa_oopptr() != nullptr &&
2645 arg_ptn->escape_state() < PointsToNode::GlobalEscape) {
2646 if (!call_analyzer->is_arg_stack(k)) {
2647 // The argument global escapes
2648 set_escape_state(arg_ptn, PointsToNode::GlobalEscape NOT_PRODUCT(COMMA trace_arg_escape_message(call)));
2649 } else {
2650 set_escape_state(arg_ptn, PointsToNode::ArgEscape NOT_PRODUCT(COMMA trace_arg_escape_message(call)));
2651 if (!call_analyzer->is_arg_local(k)) {
2652 // The argument itself doesn't escape, but any fields might
2653 set_fields_escape_state(arg_ptn, PointsToNode::GlobalEscape NOT_PRODUCT(COMMA trace_arg_escape_message(call)));
2654 }
2655 }
2656 }
2657 }
2658 if (call_ptn != nullptr && call_ptn->is_LocalVar()) {
2659 // The call returns arguments.
2660 assert(call_ptn->edge_count() > 0, "sanity");
2661 if (!call_analyzer->is_return_local()) {
2662 // Returns also unknown object.
2663 add_edge(call_ptn, phantom_obj);
2664 }
2665 }
2666 break;
2667 }
2668 }
2669 default: {
2670 // Fall-through here if not a Java method or no analyzer information
2671 // or some other type of call, assume the worst case: all arguments
2672 // globally escape.
2673 const TypeTuple* d = call->tf()->domain_cc();
2674 for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
2675 const Type* at = d->field_at(i);
2676 if (at->isa_oopptr() != nullptr) {
2677 Node* arg = call->in(i);
2678 if (arg->is_AddP()) {
2679 arg = get_addp_base(arg);
2680 }
2681 assert(ptnode_adr(arg->_idx) != nullptr, "should be defined already");
2682 set_escape_state(ptnode_adr(arg->_idx), PointsToNode::GlobalEscape NOT_PRODUCT(COMMA trace_arg_escape_message(call)));
2683 }
2684 }
2685 }
2686 }
2687 }
2688
2689
2690 // Finish Graph construction.
2691 bool ConnectionGraph::complete_connection_graph(
2692 GrowableArray<PointsToNode*>& ptnodes_worklist,
2693 GrowableArray<JavaObjectNode*>& non_escaped_allocs_worklist,
3071 PointsToNode* base = i.get();
3072 if (base->is_JavaObject()) {
3073 // Skip Allocate's fields which will be processed later.
3074 if (base->ideal_node()->is_Allocate()) {
3075 return 0;
3076 }
3077 assert(base == null_obj, "only null ptr base expected here");
3078 }
3079 }
3080 if (add_edge(field, phantom_obj)) {
3081 // New edge was added
3082 new_edges++;
3083 add_field_uses_to_worklist(field);
3084 }
3085 return new_edges;
3086 }
3087
3088 // Find fields initializing values for allocations.
3089 int ConnectionGraph::find_init_values_phantom(JavaObjectNode* pta) {
3090 assert(pta->escape_state() == PointsToNode::NoEscape, "Not escaped Allocate nodes only");
3091 PointsToNode* init_val = phantom_obj;
3092 Node* alloc = pta->ideal_node();
3093
3094 // Do nothing for Allocate nodes since its fields values are
3095 // "known" unless they are initialized by arraycopy/clone.
3096 if (alloc->is_Allocate() && !pta->arraycopy_dst()) {
3097 if (alloc->as_Allocate()->in(AllocateNode::InitValue) != nullptr) {
3098 // Null-free inline type arrays are initialized with an init value instead of null
3099 init_val = ptnode_adr(alloc->as_Allocate()->in(AllocateNode::InitValue)->_idx);
3100 assert(init_val != nullptr, "init value should be registered");
3101 } else {
3102 return 0;
3103 }
3104 }
3105 // Non-escaped allocation returned from Java or runtime call has unknown values in fields.
3106 assert(pta->arraycopy_dst() || alloc->is_CallStaticJava() || init_val != phantom_obj, "sanity");
3107 #ifdef ASSERT
3108 if (alloc->is_CallStaticJava() && alloc->as_CallStaticJava()->method() == nullptr) {
3109 const char* name = alloc->as_CallStaticJava()->_name;
3110 assert(alloc->as_CallStaticJava()->is_call_to_multianewarray_stub() ||
3111 strncmp(name, "load_unknown_inline", 19) == 0 ||
3112 strncmp(name, "store_inline_type_fields_to_buf", 31) == 0, "sanity");
3113 }
3114 #endif
3115 // Non-escaped allocation returned from Java or runtime call have unknown values in fields.
3116 int new_edges = 0;
3117 for (EdgeIterator i(pta); i.has_next(); i.next()) {
3118 PointsToNode* field = i.get();
3119 if (field->is_Field() && field->as_Field()->is_oop()) {
3120 if (add_edge(field, init_val)) {
3121 // New edge was added
3122 new_edges++;
3123 add_field_uses_to_worklist(field->as_Field());
3124 }
3125 }
3126 }
3127 return new_edges;
3128 }
3129
3130 // Find fields initializing values for allocations.
3131 int ConnectionGraph::find_init_values_null(JavaObjectNode* pta, PhaseValues* phase) {
3132 assert(pta->escape_state() == PointsToNode::NoEscape, "Not escaped Allocate nodes only");
3133 Node* alloc = pta->ideal_node();
3134 // Do nothing for Call nodes since its fields values are unknown.
3135 if (!alloc->is_Allocate() || alloc->as_Allocate()->in(AllocateNode::InitValue) != nullptr) {
3136 return 0;
3137 }
3138 InitializeNode* ini = alloc->as_Allocate()->initialization();
3139 bool visited_bottom_offset = false;
3140 GrowableArray<int> offsets_worklist;
3141 int new_edges = 0;
3142
3143 // Check if an oop field's initializing value is recorded and add
3144 // a corresponding null if field's value if it is not recorded.
3145 // Connection Graph does not record a default initialization by null
3146 // captured by Initialize node.
3147 //
3148 for (EdgeIterator i(pta); i.has_next(); i.next()) {
3149 PointsToNode* field = i.get(); // Field (AddP)
3150 if (!field->is_Field() || !field->as_Field()->is_oop()) {
3151 continue; // Not oop field
3152 }
3153 int offset = field->as_Field()->offset();
3154 if (offset == Type::OffsetBot) {
3155 if (!visited_bottom_offset) {
3201 } else {
3202 if (!val->is_LocalVar() || (val->edge_count() == 0)) {
3203 tty->print_cr("----------init store has invalid value -----");
3204 store->dump();
3205 val->dump();
3206 assert(val->is_LocalVar() && (val->edge_count() > 0), "should be processed already");
3207 }
3208 for (EdgeIterator j(val); j.has_next(); j.next()) {
3209 PointsToNode* obj = j.get();
3210 if (obj->is_JavaObject()) {
3211 if (!field->points_to(obj->as_JavaObject())) {
3212 missed_obj = obj;
3213 break;
3214 }
3215 }
3216 }
3217 }
3218 if (missed_obj != nullptr) {
3219 tty->print_cr("----------field---------------------------------");
3220 field->dump();
3221 tty->print_cr("----------missed reference to object------------");
3222 missed_obj->dump();
3223 tty->print_cr("----------object referenced by init store-------");
3224 store->dump();
3225 val->dump();
3226 assert(!field->points_to(missed_obj->as_JavaObject()), "missed JavaObject reference");
3227 }
3228 }
3229 #endif
3230 } else {
3231 // There could be initializing stores which follow allocation.
3232 // For example, a volatile field store is not collected
3233 // by Initialize node.
3234 //
3235 // Need to check for dependent loads to separate such stores from
3236 // stores which follow loads. For now, add initial value null so
3237 // that compare pointers optimization works correctly.
3238 }
3239 }
3240 if (value == nullptr) {
3241 // A field's initializing value was not recorded. Add null.
3242 if (add_edge(field, null_obj)) {
3243 // New edge was added
3568 assert(field->edge_count() > 0, "sanity");
3569 }
3570 }
3571 }
3572 }
3573 #endif
3574
3575 // Optimize ideal graph.
3576 void ConnectionGraph::optimize_ideal_graph(GrowableArray<Node*>& ptr_cmp_worklist,
3577 GrowableArray<MemBarStoreStoreNode*>& storestore_worklist) {
3578 Compile* C = _compile;
3579 PhaseIterGVN* igvn = _igvn;
3580 if (EliminateLocks) {
3581 // Mark locks before changing ideal graph.
3582 int cnt = C->macro_count();
3583 for (int i = 0; i < cnt; i++) {
3584 Node *n = C->macro_node(i);
3585 if (n->is_AbstractLock()) { // Lock and Unlock nodes
3586 AbstractLockNode* alock = n->as_AbstractLock();
3587 if (!alock->is_non_esc_obj()) {
3588 const Type* obj_type = igvn->type(alock->obj_node());
3589 if (can_eliminate_lock(alock) && !obj_type->is_inlinetypeptr()) {
3590 assert(!alock->is_eliminated() || alock->is_coarsened(), "sanity");
3591 // The lock could be marked eliminated by lock coarsening
3592 // code during first IGVN before EA. Replace coarsened flag
3593 // to eliminate all associated locks/unlocks.
3594 #ifdef ASSERT
3595 alock->log_lock_optimization(C, "eliminate_lock_set_non_esc3");
3596 #endif
3597 alock->set_non_esc_obj();
3598 }
3599 }
3600 }
3601 }
3602 }
3603
3604 if (OptimizePtrCompare) {
3605 for (int i = 0; i < ptr_cmp_worklist.length(); i++) {
3606 Node *n = ptr_cmp_worklist.at(i);
3607 assert(n->Opcode() == Op_CmpN || n->Opcode() == Op_CmpP, "must be");
3608 const TypeInt* tcmp = optimize_ptr_compare(n->in(1), n->in(2));
3609 if (tcmp->singleton()) {
3611 #ifndef PRODUCT
3612 if (PrintOptimizePtrCompare) {
3613 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"));
3614 if (Verbose) {
3615 n->dump(1);
3616 }
3617 }
3618 #endif
3619 igvn->replace_node(n, cmp);
3620 }
3621 }
3622 }
3623
3624 // For MemBarStoreStore nodes added in library_call.cpp, check
3625 // escape status of associated AllocateNode and optimize out
3626 // MemBarStoreStore node if the allocated object never escapes.
3627 for (int i = 0; i < storestore_worklist.length(); i++) {
3628 Node* storestore = storestore_worklist.at(i);
3629 Node* alloc = storestore->in(MemBarNode::Precedent)->in(0);
3630 if (alloc->is_Allocate() && not_global_escape(alloc)) {
3631 if (alloc->in(AllocateNode::InlineType) != nullptr) {
3632 // Non-escaping inline type buffer allocations don't require a membar
3633 storestore->as_MemBar()->remove(_igvn);
3634 } else {
3635 MemBarNode* mb = MemBarNode::make(C, Op_MemBarCPUOrder, Compile::AliasIdxBot);
3636 mb->init_req(TypeFunc::Memory, storestore->in(TypeFunc::Memory));
3637 mb->init_req(TypeFunc::Control, storestore->in(TypeFunc::Control));
3638 igvn->register_new_node_with_optimizer(mb);
3639 igvn->replace_node(storestore, mb);
3640 }
3641 }
3642 }
3643 }
3644
3645 // Atomic flat accesses on non-escaping objects can be optimized to non-atomic accesses
3646 void ConnectionGraph::optimize_flat_accesses(GrowableArray<SafePointNode*>& sfn_worklist) {
3647 PhaseIterGVN& igvn = *_igvn;
3648 bool delay = igvn.delay_transform();
3649 igvn.set_delay_transform(true);
3650 igvn.C->for_each_flat_access([&](Node* n) {
3651 Node* base = n->is_LoadFlat() ? n->as_LoadFlat()->base() : n->as_StoreFlat()->base();
3652 if (!not_global_escape(base)) {
3653 return;
3654 }
3655
3656 bool expanded;
3657 if (n->is_LoadFlat()) {
3658 expanded = n->as_LoadFlat()->expand_non_atomic(igvn);
3659 } else {
3660 expanded = n->as_StoreFlat()->expand_non_atomic(igvn);
3661 }
3662 if (expanded) {
3663 sfn_worklist.remove(n->as_SafePoint());
3664 igvn.C->remove_flat_access(n);
3665 }
3666 });
3667 igvn.set_delay_transform(delay);
3668 }
3669
3670 // Optimize objects compare.
3671 const TypeInt* ConnectionGraph::optimize_ptr_compare(Node* left, Node* right) {
3672 const TypeInt* UNKNOWN = TypeInt::CC; // [-1, 0,1]
3673 if (!OptimizePtrCompare) {
3674 return UNKNOWN;
3675 }
3676 const TypeInt* EQ = TypeInt::CC_EQ; // [0] == ZERO
3677 const TypeInt* NE = TypeInt::CC_GT; // [1] == ONE
3678
3679 PointsToNode* ptn1 = ptnode_adr(left->_idx);
3680 PointsToNode* ptn2 = ptnode_adr(right->_idx);
3681 JavaObjectNode* jobj1 = unique_java_object(left);
3682 JavaObjectNode* jobj2 = unique_java_object(right);
3683
3684 // The use of this method during allocation merge reduction may cause 'left'
3685 // or 'right' be something (e.g., a Phi) that isn't in the connection graph or
3686 // that doesn't reference an unique java object.
3687 if (ptn1 == nullptr || ptn2 == nullptr ||
3688 jobj1 == nullptr || jobj2 == nullptr) {
3689 return UNKNOWN;
3809 assert(!src->is_Field() && !dst->is_Field(), "only for JavaObject and LocalVar");
3810 assert((src != null_obj) && (dst != null_obj), "not for ConP null");
3811 PointsToNode* ptadr = _nodes.at(n->_idx);
3812 if (ptadr != nullptr) {
3813 assert(ptadr->is_Arraycopy() && ptadr->ideal_node() == n, "sanity");
3814 return;
3815 }
3816 Compile* C = _compile;
3817 ptadr = new (C->comp_arena()) ArraycopyNode(this, n, es);
3818 map_ideal_node(n, ptadr);
3819 // Add edge from arraycopy node to source object.
3820 (void)add_edge(ptadr, src);
3821 src->set_arraycopy_src();
3822 // Add edge from destination object to arraycopy node.
3823 (void)add_edge(dst, ptadr);
3824 dst->set_arraycopy_dst();
3825 }
3826
3827 bool ConnectionGraph::is_oop_field(Node* n, int offset, bool* unsafe) {
3828 const Type* adr_type = n->as_AddP()->bottom_type();
3829 int field_offset = adr_type->isa_aryptr() ? adr_type->isa_aryptr()->field_offset().get() : Type::OffsetBot;
3830 BasicType bt = T_INT;
3831 if (offset == Type::OffsetBot && field_offset == Type::OffsetBot) {
3832 // Check only oop fields.
3833 if (!adr_type->isa_aryptr() ||
3834 adr_type->isa_aryptr()->elem() == Type::BOTTOM ||
3835 adr_type->isa_aryptr()->elem()->make_oopptr() != nullptr) {
3836 // OffsetBot is used to reference array's element. Ignore first AddP.
3837 if (find_second_addp(n, n->in(AddPNode::Base)) == nullptr) {
3838 bt = T_OBJECT;
3839 }
3840 }
3841 } else if (offset != oopDesc::klass_offset_in_bytes()) {
3842 if (adr_type->isa_instptr()) {
3843 ciField* field = _compile->alias_type(adr_type->is_ptr())->field();
3844 if (field != nullptr) {
3845 bt = field->layout_type();
3846 } else {
3847 // Check for unsafe oop field access
3848 if (has_oop_node_outs(n)) {
3849 bt = T_OBJECT;
3850 (*unsafe) = true;
3851 }
3852 }
3853 } else if (adr_type->isa_aryptr()) {
3854 if (offset == arrayOopDesc::length_offset_in_bytes()) {
3855 // Ignore array length load.
3856 } else if (find_second_addp(n, n->in(AddPNode::Base)) != nullptr) {
3857 // Ignore first AddP.
3858 } else {
3859 const Type* elemtype = adr_type->is_aryptr()->elem();
3860 if (adr_type->is_aryptr()->is_flat() && field_offset != Type::OffsetBot) {
3861 ciInlineKlass* vk = elemtype->inline_klass();
3862 field_offset += vk->payload_offset();
3863 ciField* field = vk->get_field_by_offset(field_offset, false);
3864 if (field != nullptr) {
3865 bt = field->layout_type();
3866 } else {
3867 assert(field_offset == vk->payload_offset() + vk->null_marker_offset_in_payload(), "no field or null marker of %s at offset %d", vk->name()->as_utf8(), field_offset);
3868 bt = T_BOOLEAN;
3869 }
3870 } else {
3871 bt = elemtype->array_element_basic_type();
3872 }
3873 }
3874 } else if (adr_type->isa_rawptr() || adr_type->isa_klassptr()) {
3875 // Allocation initialization, ThreadLocal field access, unsafe access
3876 if (has_oop_node_outs(n)) {
3877 bt = T_OBJECT;
3878 }
3879 }
3880 }
3881 // Note: T_NARROWOOP is not classed as a real reference type
3882 bool res = (is_reference_type(bt) || bt == T_NARROWOOP);
3883 assert(!has_oop_node_outs(n) || res, "sanity: AddP has oop outs, needs to be treated as oop field");
3884 return res;
3885 }
3886
3887 bool ConnectionGraph::has_oop_node_outs(Node* n) {
3888 return n->has_out_with(Op_StoreP, Op_LoadP, Op_StoreN, Op_LoadN) ||
3889 n->has_out_with(Op_GetAndSetP, Op_GetAndSetN, Op_CompareAndExchangeP, Op_CompareAndExchangeN) ||
3890 n->has_out_with(Op_CompareAndSwapP, Op_CompareAndSwapN, Op_WeakCompareAndSwapP, Op_WeakCompareAndSwapN) ||
3891 BarrierSet::barrier_set()->barrier_set_c2()->escape_has_out_with_unsafe_object(n);
3892 }
4055 return true;
4056 }
4057 }
4058 }
4059 }
4060 }
4061 return false;
4062 }
4063
4064 int ConnectionGraph::address_offset(Node* adr, PhaseValues* phase) {
4065 const Type *adr_type = phase->type(adr);
4066 if (adr->is_AddP() && adr_type->isa_oopptr() == nullptr && is_captured_store_address(adr)) {
4067 // We are computing a raw address for a store captured by an Initialize
4068 // compute an appropriate address type. AddP cases #3 and #5 (see below).
4069 int offs = (int)phase->find_intptr_t_con(adr->in(AddPNode::Offset), Type::OffsetBot);
4070 assert(offs != Type::OffsetBot ||
4071 adr->in(AddPNode::Address)->in(0)->is_AllocateArray(),
4072 "offset must be a constant or it is initialization of array");
4073 return offs;
4074 }
4075 return adr_type->is_ptr()->flat_offset();
4076 }
4077
4078 Node* ConnectionGraph::get_addp_base(Node *addp) {
4079 assert(addp->is_AddP(), "must be AddP");
4080 //
4081 // AddP cases for Base and Address inputs:
4082 // case #1. Direct object's field reference:
4083 // Allocate
4084 // |
4085 // Proj #5 ( oop result )
4086 // |
4087 // CheckCastPP (cast to instance type)
4088 // | |
4089 // AddP ( base == address )
4090 //
4091 // case #2. Indirect object's field reference:
4092 // Phi
4093 // |
4094 // CastPP (cast to instance type)
4095 // | |
4096 // AddP ( base == address )
4097 //
4098 // case #3. Raw object's field reference for Initialize node.
4099 // Could have an additional Phi merging multiple allocations.
4100 // Allocate
4101 // |
4102 // Proj #5 ( oop result )
4103 // top |
4104 // \ |
4105 // AddP ( base == top )
4106 //
4107 // case #4. Array's element reference:
4108 // {CheckCastPP | CastPP}
4109 // | | |
4110 // | AddP ( array's element offset )
4111 // | |
4112 // AddP ( array's offset )
4113 //
4114 // case #5. Raw object's field reference for arraycopy stub call:
4115 // The inline_native_clone() case when the arraycopy stub is called
4116 // after the allocation before Initialize and CheckCastPP nodes.
4117 // Allocate
4118 // |
4119 // Proj #5 ( oop result )
4130 // case #7. Klass's field reference.
4131 // LoadKlass
4132 // | |
4133 // AddP ( base == address )
4134 //
4135 // case #8. narrow Klass's field reference.
4136 // LoadNKlass
4137 // |
4138 // DecodeN
4139 // | |
4140 // AddP ( base == address )
4141 //
4142 // case #9. Mixed unsafe access
4143 // {instance}
4144 // |
4145 // CheckCastPP (raw)
4146 // top |
4147 // \ |
4148 // AddP ( base == top )
4149 //
4150 // case #10. Klass fetched with
4151 // LibraryCallKit::load_*_refined_array_klass()
4152 // which has en extra Phi.
4153 // LoadKlass LoadKlass
4154 // | |
4155 // CastPP CastPP
4156 // \ /
4157 // Phi
4158 // top |
4159 // \ |
4160 // AddP ( base == top )
4161 //
4162 Node *base = addp->in(AddPNode::Base);
4163 if (base->uncast()->is_top()) { // The AddP case #3, #6, #9, and #10.
4164 base = addp->in(AddPNode::Address);
4165 while (base->is_AddP()) {
4166 // Case #6 (unsafe access) may have several chained AddP nodes.
4167 assert(base->in(AddPNode::Base)->uncast()->is_top(), "expected unsafe access address only");
4168 base = base->in(AddPNode::Address);
4169 }
4170 if (base->Opcode() == Op_CheckCastPP &&
4171 base->bottom_type()->isa_rawptr() &&
4172 _igvn->type(base->in(1))->isa_oopptr()) {
4173 base = base->in(1); // Case #9
4174 } else {
4175 // Case #3, #6, and #10
4176 Node* uncast_base = base->uncast();
4177 int opcode = uncast_base->Opcode();
4178 assert(opcode == Op_ConP || opcode == Op_ThreadLocal ||
4179 opcode == Op_CastX2P || uncast_base->is_DecodeNarrowPtr() ||
4180 (_igvn->C->is_osr_compilation() && uncast_base->is_Parm() && uncast_base->as_Parm()->_con == TypeFunc::Parms)||
4181 (uncast_base->is_Mem() && (uncast_base->bottom_type()->isa_rawptr() != nullptr)) ||
4182 (uncast_base->is_Mem() && (uncast_base->bottom_type()->isa_klassptr() != nullptr)) ||
4183 is_captured_store_address(addp) ||
4184 is_load_array_klass_related(uncast_base), "sanity");
4185 }
4186 }
4187 return base;
4188 }
4189
4190 #ifdef ASSERT
4191 // Case #10
4192 bool ConnectionGraph::is_load_array_klass_related(const Node* uncast_base) {
4193 if (!uncast_base->is_Phi() || uncast_base->req() != 3) {
4194 return false;
4195 }
4196 Node* in1 = uncast_base->in(1);
4197 Node* in2 = uncast_base->in(2);
4198 return in1->uncast()->Opcode() == Op_LoadKlass &&
4199 in2->uncast()->Opcode() == Op_LoadKlass;
4200 }
4201 #endif
4202
4203 Node* ConnectionGraph::find_second_addp(Node* addp, Node* n) {
4204 assert(addp->is_AddP() && addp->outcnt() > 0, "Don't process dead nodes");
4205 Node* addp2 = addp->raw_out(0);
4206 if (addp->outcnt() == 1 && addp2->is_AddP() &&
4207 addp2->in(AddPNode::Base) == n &&
4208 addp2->in(AddPNode::Address) == addp) {
4209 assert(addp->in(AddPNode::Base) == n, "expecting the same base");
4210 //
4211 // Find array's offset to push it on worklist first and
4212 // as result process an array's element offset first (pushed second)
4213 // to avoid CastPP for the array's offset.
4214 // Otherwise the inserted CastPP (LocalVar) will point to what
4215 // the AddP (Field) points to. Which would be wrong since
4216 // the algorithm expects the CastPP has the same point as
4217 // as AddP's base CheckCastPP (LocalVar).
4218 //
4219 // ArrayAllocation
4220 // |
4221 // CheckCastPP
4222 // |
4239 }
4240 return nullptr;
4241 }
4242
4243 //
4244 // Adjust the type and inputs of an AddP which computes the
4245 // address of a field of an instance
4246 //
4247 bool ConnectionGraph::split_AddP(Node *addp, Node *base) {
4248 PhaseGVN* igvn = _igvn;
4249 const TypeOopPtr *base_t = igvn->type(base)->isa_oopptr();
4250 assert(base_t != nullptr && base_t->is_known_instance(), "expecting instance oopptr");
4251 const TypeOopPtr *t = igvn->type(addp)->isa_oopptr();
4252 if (t == nullptr) {
4253 // We are computing a raw address for a store captured by an Initialize
4254 // compute an appropriate address type (cases #3 and #5).
4255 assert(igvn->type(addp) == TypeRawPtr::NOTNULL, "must be raw pointer");
4256 assert(addp->in(AddPNode::Address)->is_Proj(), "base of raw address must be result projection from allocation");
4257 intptr_t offs = (int)igvn->find_intptr_t_con(addp->in(AddPNode::Offset), Type::OffsetBot);
4258 assert(offs != Type::OffsetBot, "offset must be a constant");
4259 if (base_t->isa_aryptr() != nullptr) {
4260 // In the case of a flat inline type array, each field has its
4261 // own slice so we need to extract the field being accessed from
4262 // the address computation
4263 t = base_t->isa_aryptr()->add_field_offset_and_offset(offs)->is_oopptr();
4264 } else {
4265 t = base_t->add_offset(offs)->is_oopptr();
4266 }
4267 }
4268 int inst_id = base_t->instance_id();
4269 assert(!t->is_known_instance() || t->instance_id() == inst_id,
4270 "old type must be non-instance or match new type");
4271
4272 // The type 't' could be subclass of 'base_t'.
4273 // As result t->offset() could be large then base_t's size and it will
4274 // cause the failure in add_offset() with narrow oops since TypeOopPtr()
4275 // constructor verifies correctness of the offset.
4276 //
4277 // It could happened on subclass's branch (from the type profiling
4278 // inlining) which was not eliminated during parsing since the exactness
4279 // of the allocation type was not propagated to the subclass type check.
4280 //
4281 // Or the type 't' could be not related to 'base_t' at all.
4282 // It could happen when CHA type is different from MDO type on a dead path
4283 // (for example, from instanceof check) which is not collapsed during parsing.
4284 //
4285 // Do nothing for such AddP node and don't process its users since
4286 // this code branch will go away.
4287 //
4288 if (!t->is_known_instance() &&
4289 !base_t->maybe_java_subtype_of(t)) {
4290 return false; // bail out
4291 }
4292 const TypePtr* tinst = base_t->add_offset(t->offset());
4293 if (tinst->isa_aryptr() && t->isa_aryptr()) {
4294 // In the case of a flat inline type array, each field has its
4295 // own slice so we need to keep track of the field being accessed.
4296 tinst = tinst->is_aryptr()->with_field_offset(t->is_aryptr()->field_offset().get());
4297 // Keep array properties (not flat/null-free)
4298 tinst = tinst->is_aryptr()->update_properties(t->is_aryptr());
4299 if (tinst == nullptr) {
4300 return false; // Skip dead path with inconsistent properties
4301 }
4302 }
4303
4304 // Do NOT remove the next line: ensure a new alias index is allocated
4305 // for the instance type. Note: C++ will not remove it since the call
4306 // has side effect.
4307 int alias_idx = _compile->get_alias_index(tinst);
4308 igvn->set_type(addp, tinst);
4309 // record the allocation in the node map
4310 set_map(addp, get_map(base->_idx));
4311 // Set addp's Base and Address to 'base'.
4312 Node *abase = addp->in(AddPNode::Base);
4313 Node *adr = addp->in(AddPNode::Address);
4314 if (adr->is_Proj() && adr->in(0)->is_Allocate() &&
4315 adr->in(0)->_idx == (uint)inst_id) {
4316 // Skip AddP cases #3 and #5.
4317 } else {
4318 assert(!abase->is_top(), "sanity"); // AddP case #3
4319 if (abase != base) {
4320 igvn->hash_delete(addp);
4321 addp->set_req(AddPNode::Base, base);
4322 if (abase == adr) {
4323 addp->set_req(AddPNode::Address, base);
4531 "Following memory nodes should have new memory input or be on the same memory slice");
4532 } else if (use->is_Phi()) {
4533 // Phi nodes should be split and moved already.
4534 tp = use->as_Phi()->adr_type()->isa_ptr();
4535 assert(tp != nullptr, "ptr type");
4536 int idx = C->get_alias_index(tp);
4537 assert(idx == alias_idx, "Following Phi nodes should be on the same memory slice");
4538 } else {
4539 use->dump();
4540 assert(false, "should not be here");
4541 #endif
4542 }
4543 }
4544 }
4545
4546 //
4547 // Search memory chain of "mem" to find a MemNode whose address
4548 // is the specified alias index.
4549 //
4550 #define FIND_INST_MEM_RECURSION_DEPTH_LIMIT 1000
4551
4552 // Does LoadFlat/StoreFlat flat_access alias with memory access with type toop?
4553 // toop is the type for some field of some known instance
4554 bool ConnectionGraph::flat_access_aliases_with(Node* flat_access, const TypeOopPtr* toop) {
4555 Node* base = flat_access->is_StoreFlat() ? flat_access->as_StoreFlat()->base() : flat_access->as_LoadFlat()->base();
4556 uint idx = base->_idx;
4557 if (idx >= nodes_size()) {
4558 return false;
4559 }
4560 PointsToNode* ptn = ptnode_adr(idx);
4561 if (ptn == nullptr) {
4562 return false;
4563 }
4564 PointsToNode::EscapeState es = ptn->escape_state();
4565 if (es >= PointsToNode::GlobalEscape) {
4566 return false;
4567 }
4568 if (ptn->is_JavaObject()) {
4569 Node* jobj_base = get_map(ptn->idx());
4570 if (jobj_base == nullptr || !_igvn->type(jobj_base)->is_oopptr()->same_instance_as(toop)) {
4571 assert(!_igvn->type(base)->is_oopptr()->same_instance_as(toop), "should not alias");
4572 return false;
4573 }
4574 return true;
4575 }
4576 assert(ptn->is_LocalVar(), "sanity");
4577 for (EdgeIterator i(ptn); i.has_next(); i.next()) {
4578 if (i.get()->is_JavaObject()) {
4579 Node* jobj_base = get_map(i.get()->idx());
4580 if (jobj_base != nullptr && _igvn->type(jobj_base)->is_oopptr()->same_instance_as(toop)) {
4581 return true;
4582 }
4583 }
4584 }
4585 assert(!_igvn->type(base)->is_oopptr()->same_instance_as(toop), "should not alias");
4586 return false;
4587 }
4588
4589 Node* ConnectionGraph::find_inst_mem(Node* orig_mem, int alias_idx, Unique_Node_List& orig_phis, uint rec_depth) {
4590 if (rec_depth > FIND_INST_MEM_RECURSION_DEPTH_LIMIT) {
4591 _compile->record_failure(_invocation > 0 ? C2Compiler::retry_no_iterative_escape_analysis() : C2Compiler::retry_no_escape_analysis());
4592 return nullptr;
4593 }
4594 if (orig_mem == nullptr) {
4595 return orig_mem;
4596 }
4597 Compile* C = _compile;
4598 PhaseGVN* igvn = _igvn;
4599 const TypeOopPtr *toop = C->get_adr_type(alias_idx)->isa_oopptr();
4600 bool is_instance = (toop != nullptr) && toop->is_known_instance();
4601 Node *start_mem = C->start()->proj_out_or_null(TypeFunc::Memory);
4602 Node *prev = nullptr;
4603 Node *result = orig_mem;
4604 while (prev != result) {
4605 prev = result;
4606 if (result == start_mem) {
4607 break; // hit one of our sentinels
4608 }
4645 } else if (C->get_alias_index(result->adr_type()) != alias_idx) {
4646 assert(C->get_general_index(alias_idx) == C->get_alias_index(result->adr_type()), "should be projection for the same field/array element");
4647 result = get_map(result->_idx);
4648 assert(result != nullptr, "new projection should have been allocated");
4649 break;
4650 }
4651 } else if (proj_in->is_MemBar()) {
4652 // Check if there is an array copy for a clone
4653 // Step over GC barrier when ReduceInitialCardMarks is disabled
4654 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
4655 Node* control_proj_ac = bs->step_over_gc_barrier(proj_in->in(0));
4656
4657 if (control_proj_ac->is_Proj() && control_proj_ac->in(0)->is_ArrayCopy()) {
4658 // Stop if it is a clone
4659 ArrayCopyNode* ac = control_proj_ac->in(0)->as_ArrayCopy();
4660 if (ac->may_modify(toop, igvn)) {
4661 break;
4662 }
4663 }
4664 result = proj_in->in(TypeFunc::Memory);
4665 } else if (proj_in->is_LoadFlat()) {
4666 // Either:
4667 // 1- this is a non mismatched LoadFlat for alias_idx's non escaping allocation: it will get removed by
4668 // ConnectionGraph::optimize_flat_accesses() and has no effect on the memory state
4669 // 2- or it is a LoadFlat for some object unrelated to alias_idx
4670 // 3- this is mismatched LoadFlat for alias_idx's non escaping allocation: it won't get removed by
4671 // ConnectionGraph::optimize_flat_accesses()
4672 // In cases 1- and 2-, it's safe to assume this LoadFlat doesn't modify the memory for alias_idx
4673 // If the LoadFlat is mismatched, it's not removed so don't step over it if it's a flat access to the alias_idx
4674 // known instance
4675 if (!proj_in->as_LoadFlat()->is_mismatched() || !flat_access_aliases_with(proj_in, toop)) {
4676 result = proj_in->in(TypeFunc::Memory);
4677 }
4678 } else if (proj_in->is_StoreFlat()) {
4679 // Either:
4680 // - this is a StoreFlat for alias_idx's non escaping allocation that does modify the memory state for alias_idx
4681 // - or it is a StoreFlat for some object unrelated to alias_idx that can't modify the memory state for alias_idx
4682 if (!flat_access_aliases_with(proj_in, toop)) {
4683 result = proj_in->in(TypeFunc::Memory);
4684 }
4685 }
4686 } else if (result->is_MergeMem()) {
4687 MergeMemNode *mmem = result->as_MergeMem();
4688 result = step_through_mergemem(mmem, alias_idx, toop);
4689 if (result == mmem->base_memory()) {
4690 // Didn't find instance memory, search through general slice recursively.
4691 result = mmem->memory_at(C->get_general_index(alias_idx));
4692 result = find_inst_mem(result, alias_idx, orig_phis, rec_depth + 1);
4693 if (C->failing()) {
4694 return nullptr;
4695 }
4696 mmem->set_memory_at(alias_idx, result);
4697 }
4698 } else if (result->is_Phi() &&
4699 C->get_alias_index(result->as_Phi()->adr_type()) != alias_idx) {
4700 Node *un = result->as_Phi()->unique_input(igvn);
4701 if (un != nullptr) {
4702 orig_phis.push(result);
4703 result = un;
4704 } else {
4950 // - not determined to be ineligible by escape analysis
4951 set_map(alloc, n);
4952 set_map(n, alloc);
4953 const TypeOopPtr* tinst = t->cast_to_instance_id(ni);
4954 igvn->hash_delete(n);
4955 igvn->set_type(n, tinst);
4956 n->raise_bottom_type(tinst);
4957 igvn->hash_insert(n);
4958 record_for_optimizer(n);
4959 // Allocate an alias index for the header fields. Accesses to
4960 // the header emitted during macro expansion wouldn't have
4961 // correct memory state otherwise.
4962 _compile->get_alias_index(tinst->add_offset(oopDesc::mark_offset_in_bytes()));
4963 _compile->get_alias_index(tinst->add_offset(oopDesc::klass_offset_in_bytes()));
4964 if (alloc->is_Allocate() && (t->isa_instptr() || t->isa_aryptr())) {
4965 // Add a new NarrowMem projection for each existing NarrowMem projection with new adr type
4966 InitializeNode* init = alloc->as_Allocate()->initialization();
4967 assert(init != nullptr, "can't find Initialization node for this Allocate node");
4968 auto process_narrow_proj = [&](NarrowMemProjNode* proj) {
4969 const TypePtr* adr_type = proj->adr_type();
4970 const TypePtr* new_adr_type = tinst->with_offset(adr_type->offset());
4971 if (adr_type->isa_aryptr()) {
4972 // In the case of a flat inline type array, each field has its own slice so we need a
4973 // NarrowMemProj for each field of the flat array elements
4974 new_adr_type = new_adr_type->is_aryptr()->with_field_offset(adr_type->is_aryptr()->field_offset().get());
4975 }
4976 if (adr_type != new_adr_type && !init->already_has_narrow_mem_proj_with_adr_type(new_adr_type)) {
4977 // Do NOT remove the next line: ensure a new alias index is allocated for the instance type.
4978 uint alias_idx = _compile->get_alias_index(new_adr_type);
4979 assert(_compile->get_general_index(alias_idx) == _compile->get_alias_index(adr_type), "new adr type should be narrowed down from existing adr type");
4980 NarrowMemProjNode* new_proj = new NarrowMemProjNode(init, new_adr_type);
4981 igvn->set_type(new_proj, new_proj->bottom_type());
4982 record_for_optimizer(new_proj);
4983 set_map(proj, new_proj); // record it so ConnectionGraph::find_inst_mem() can find it
4984 }
4985 };
4986 init->for_each_narrow_mem_proj_with_new_uses(process_narrow_proj);
4987
4988 // First, put on the worklist all Field edges from Connection Graph
4989 // which is more accurate than putting immediate users from Ideal Graph.
4990 for (EdgeIterator e(ptn); e.has_next(); e.next()) {
4991 PointsToNode* tgt = e.get();
4992 if (tgt->is_Arraycopy()) {
4993 continue;
4994 }
4995 Node* use = tgt->ideal_node();
5073 ptnode_adr(n->_idx)->dump();
5074 assert(jobj != nullptr && jobj != phantom_obj, "escaped allocation");
5075 #endif
5076 _compile->record_failure(_invocation > 0 ? C2Compiler::retry_no_iterative_escape_analysis() : C2Compiler::retry_no_escape_analysis());
5077 return;
5078 } else {
5079 Node *val = get_map(jobj->idx()); // CheckCastPP node
5080 TypeNode *tn = n->as_Type();
5081 const TypeOopPtr* tinst = igvn->type(val)->isa_oopptr();
5082 assert(tinst != nullptr && tinst->is_known_instance() &&
5083 tinst->instance_id() == jobj->idx() , "instance type expected.");
5084
5085 const Type *tn_type = igvn->type(tn);
5086 const TypeOopPtr *tn_t;
5087 if (tn_type->isa_narrowoop()) {
5088 tn_t = tn_type->make_ptr()->isa_oopptr();
5089 } else {
5090 tn_t = tn_type->isa_oopptr();
5091 }
5092 if (tn_t != nullptr && tinst->maybe_java_subtype_of(tn_t)) {
5093 if (tn_t->isa_aryptr()) {
5094 // Keep array properties (not flat/null-free)
5095 tinst = tinst->is_aryptr()->update_properties(tn_t->is_aryptr());
5096 if (tinst == nullptr) {
5097 continue; // Skip dead path with inconsistent properties
5098 }
5099 }
5100 if (tn_type->isa_narrowoop()) {
5101 tn_type = tinst->make_narrowoop();
5102 } else {
5103 tn_type = tinst;
5104 }
5105 igvn->hash_delete(tn);
5106 igvn->set_type(tn, tn_type);
5107 tn->set_type(tn_type);
5108 igvn->hash_insert(tn);
5109 record_for_optimizer(n);
5110 } else {
5111 assert(tn_type == TypePtr::NULL_PTR ||
5112 (tn_t != nullptr && !tinst->maybe_java_subtype_of(tn_t)),
5113 "unexpected type");
5114 continue; // Skip dead path with different type
5115 }
5116 }
5117 } else {
5118 DEBUG_ONLY(n->dump();)
5119 assert(false, "EA: unexpected node");
5120 continue;
5121 }
5122 // push allocation's users on appropriate worklist
5123 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
5124 Node *use = n->fast_out(i);
5125 if (use->is_Mem() && use->in(MemNode::Address) == n) {
5126 // Load/store to instance's field
5127 memnode_worklist.push(use);
5128 } else if (use->is_MemBar()) {
5129 if (use->in(TypeFunc::Memory) == n) { // Ignore precedent edge
5130 memnode_worklist.push(use);
5131 }
5132 } else if (use->is_AddP() && use->outcnt() > 0) { // No dead nodes
5133 Node* addp2 = find_second_addp(use, n);
5134 if (addp2 != nullptr) {
5135 alloc_worklist.append_if_missing(addp2);
5136 }
5137 alloc_worklist.append_if_missing(use);
5138 } else if (use->is_Phi() ||
5139 use->is_CheckCastPP() ||
5140 use->is_EncodeNarrowPtr() ||
5141 use->is_DecodeNarrowPtr() ||
5142 (use->is_ConstraintCast() && use->Opcode() == Op_CastPP)) {
5143 alloc_worklist.append_if_missing(use);
5144 #ifdef ASSERT
5145 } else if (use->is_Mem()) {
5146 assert(use->in(MemNode::Address) != n, "EA: missing allocation reference path");
5147 } else if (use->is_MergeMem()) {
5148 assert(mergemem_worklist.contains(use->as_MergeMem()), "EA: missing MergeMem node in the worklist");
5149 } else if (use->is_SafePoint()) {
5150 // Look for MergeMem nodes for calls which reference unique allocation
5151 // (through CheckCastPP nodes) even for debug info.
5152 Node* m = use->in(TypeFunc::Memory);
5153 if (m->is_MergeMem()) {
5154 assert(mergemem_worklist.contains(m->as_MergeMem()), "EA: missing MergeMem node in the worklist");
5155 }
5156 } else if (use->Opcode() == Op_EncodeISOArray) {
5157 if (use->in(MemNode::Memory) == n || use->in(3) == n) {
5158 // EncodeISOArray overwrites destination array
5159 memnode_worklist.push(use);
5160 }
5161 } else if (use->Opcode() == Op_Return) {
5162 // Allocation is referenced by field of returned inline type
5163 assert(_compile->tf()->returns_inline_type_as_fields(), "EA: unexpected reference by ReturnNode");
5164 } else {
5165 uint op = use->Opcode();
5166 if ((op == Op_StrCompressedCopy || op == Op_StrInflatedCopy) &&
5167 (use->in(MemNode::Memory) == n)) {
5168 // They overwrite memory edge corresponding to destination array,
5169 memnode_worklist.push(use);
5170 } else if (!(op == Op_CmpP || op == Op_Conv2B ||
5171 op == Op_CastP2X ||
5172 op == Op_FastLock || op == Op_AryEq ||
5173 op == Op_StrComp || op == Op_CountPositives ||
5174 op == Op_StrCompressedCopy || op == Op_StrInflatedCopy ||
5175 op == Op_StrEquals || op == Op_VectorizedHashCode ||
5176 op == Op_StrIndexOf || op == Op_StrIndexOfChar ||
5177 op == Op_SubTypeCheck || op == Op_InlineType || op == Op_FlatArrayCheck ||
5178 op == Op_ReinterpretS2HF ||
5179 op == Op_ReachabilityFence ||
5180 BarrierSet::barrier_set()->barrier_set_c2()->is_gc_barrier_node(use))) {
5181 n->dump();
5182 use->dump();
5183 assert(false, "EA: missing allocation reference path");
5184 }
5185 #endif
5186 }
5187 }
5188
5189 }
5190
5191 #ifdef ASSERT
5192 if (VerifyReduceAllocationMerges) {
5193 for (uint i = 0; i < reducible_merges.size(); i++) {
5194 Node* phi = reducible_merges.at(i);
5195
5196 if (!reduced_merges.member(phi)) {
5197 phi->dump(2);
5273 n = n->as_MemBar()->proj_out_or_null(TypeFunc::Memory);
5274 if (n == nullptr) {
5275 continue;
5276 }
5277 }
5278 } else if (n->is_CallLeaf()) {
5279 // Runtime calls with narrow memory input (no MergeMem node)
5280 // get the memory projection
5281 n = n->as_Call()->proj_out_or_null(TypeFunc::Memory);
5282 if (n == nullptr) {
5283 continue;
5284 }
5285 } else if (n->Opcode() == Op_StrInflatedCopy) {
5286 // Check direct uses of StrInflatedCopy.
5287 // It is memory type Node - no special SCMemProj node.
5288 } else if (n->Opcode() == Op_StrCompressedCopy ||
5289 n->Opcode() == Op_EncodeISOArray) {
5290 // get the memory projection
5291 n = n->find_out_with(Op_SCMemProj);
5292 assert(n != nullptr && n->Opcode() == Op_SCMemProj, "memory projection required");
5293 } else if (n->is_CallLeaf() && n->as_CallLeaf()->_name != nullptr &&
5294 strcmp(n->as_CallLeaf()->_name, "store_unknown_inline") == 0) {
5295 n = n->as_CallLeaf()->proj_out(TypeFunc::Memory);
5296 } else if (n->is_Proj()) {
5297 assert(n->in(0)->is_Initialize(), "we only push memory projections for Initialize");
5298 } else {
5299 #ifdef ASSERT
5300 if (!n->is_Mem()) {
5301 n->dump();
5302 }
5303 assert(n->is_Mem(), "memory node required.");
5304 #endif
5305 Node *addr = n->in(MemNode::Address);
5306 const Type *addr_t = igvn->type(addr);
5307 if (addr_t == Type::TOP) {
5308 continue;
5309 }
5310 assert (addr_t->isa_ptr() != nullptr, "pointer type required.");
5311 int alias_idx = _compile->get_alias_index(addr_t->is_ptr());
5312 assert ((uint)alias_idx < new_index_end, "wrong alias index");
5313 Node *mem = find_inst_mem(n->in(MemNode::Memory), alias_idx, orig_phis);
5314 if (_compile->failing()) {
5315 return;
5327 assert(n != nullptr && n->Opcode() == Op_SCMemProj, "memory projection required");
5328 }
5329 }
5330 // push user on appropriate worklist
5331 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
5332 Node *use = n->fast_out(i);
5333 if (use->is_Phi() || use->is_ClearArray()) {
5334 memnode_worklist.push(use);
5335 } else if (use->is_Mem() && use->in(MemNode::Memory) == n) {
5336 memnode_worklist.push(use);
5337 } else if (use->is_MemBar() || use->is_CallLeaf()) {
5338 if (use->in(TypeFunc::Memory) == n) { // Ignore precedent edge
5339 memnode_worklist.push(use);
5340 }
5341 } else if (use->is_Proj()) {
5342 assert(n->is_Initialize(), "We only push projections of Initialize");
5343 if (use->as_Proj()->_con == TypeFunc::Memory) { // Ignore precedent edge
5344 memnode_worklist.push(use);
5345 }
5346 #ifdef ASSERT
5347 } else if (use->is_Mem()) {
5348 assert(use->in(MemNode::Memory) != n, "EA: missing memory path");
5349 } else if (use->is_MergeMem()) {
5350 assert(mergemem_worklist.contains(use->as_MergeMem()), "EA: missing MergeMem node in the worklist");
5351 } else if (use->Opcode() == Op_EncodeISOArray) {
5352 if (use->in(MemNode::Memory) == n || use->in(3) == n) {
5353 // EncodeISOArray overwrites destination array
5354 memnode_worklist.push(use);
5355 }
5356 } else if (use->is_CallLeaf() && use->as_CallLeaf()->_name != nullptr &&
5357 strcmp(use->as_CallLeaf()->_name, "store_unknown_inline") == 0) {
5358 // store_unknown_inline overwrites destination array
5359 memnode_worklist.push(use);
5360 } else {
5361 uint op = use->Opcode();
5362 if ((use->in(MemNode::Memory) == n) &&
5363 (op == Op_StrCompressedCopy || op == Op_StrInflatedCopy)) {
5364 // They overwrite memory edge corresponding to destination array,
5365 memnode_worklist.push(use);
5366 } else if (!(BarrierSet::barrier_set()->barrier_set_c2()->is_gc_barrier_node(use) ||
5367 op == Op_AryEq || op == Op_StrComp || op == Op_CountPositives ||
5368 op == Op_StrCompressedCopy || op == Op_StrInflatedCopy || op == Op_VectorizedHashCode ||
5369 op == Op_StrEquals || op == Op_StrIndexOf || op == Op_StrIndexOfChar || op == Op_FlatArrayCheck)) {
5370 n->dump();
5371 use->dump();
5372 assert(false, "EA: missing memory path");
5373 }
5374 #endif
5375 }
5376 }
5377 }
5378
5379 // Phase 3: Process MergeMem nodes from mergemem_worklist.
5380 // Walk each memory slice moving the first node encountered of each
5381 // instance type to the input corresponding to its alias index.
5382 uint length = mergemem_worklist.length();
5383 for( uint next = 0; next < length; ++next ) {
5384 MergeMemNode* nmm = mergemem_worklist.at(next);
5385 assert(!visited.test_set(nmm->_idx), "should not be visited before");
5386 // Note: we don't want to use MergeMemStream here because we only want to
5387 // scan inputs which exist at the start, not ones we add during processing.
5388 // Note 2: MergeMem may already contains instance memory slices added
5389 // during find_inst_mem() call when memory nodes were processed above.
5452 _compile->record_failure(C2Compiler::retry_no_reduce_allocation_merges());
5453 } else if (_invocation > 0) {
5454 _compile->record_failure(C2Compiler::retry_no_iterative_escape_analysis());
5455 } else {
5456 _compile->record_failure(C2Compiler::retry_no_escape_analysis());
5457 }
5458 return;
5459 }
5460
5461 igvn->hash_insert(nmm);
5462 record_for_optimizer(nmm);
5463 }
5464
5465 _compile->print_method(PHASE_EA_AFTER_SPLIT_UNIQUE_TYPES_3, 5);
5466
5467 // Phase 4: Update the inputs of non-instance memory Phis and
5468 // the Memory input of memnodes
5469 // First update the inputs of any non-instance Phi's from
5470 // which we split out an instance Phi. Note we don't have
5471 // to recursively process Phi's encountered on the input memory
5472 // chains as is done in split_memory_phi() since they will
5473 // also be processed here.
5474 for (uint j = 0; j < orig_phis.size(); j++) {
5475 PhiNode* phi = orig_phis.at(j)->as_Phi();
5476 int alias_idx = _compile->get_alias_index(phi->adr_type());
5477 igvn->hash_delete(phi);
5478 for (uint i = 1; i < phi->req(); i++) {
5479 Node *mem = phi->in(i);
5480 Node *new_mem = find_inst_mem(mem, alias_idx, orig_phis);
5481 if (_compile->failing()) {
5482 return;
5483 }
5484 if (mem != new_mem) {
5485 phi->set_req(i, new_mem);
5486 }
5487 }
5488 igvn->hash_insert(phi);
5489 record_for_optimizer(phi);
5490 }
5491
5492 // Update the memory inputs of MemNodes with the value we computed
|