11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "ci/bcEscapeAnalyzer.hpp"
26 #include "compiler/compileLog.hpp"
27 #include "gc/shared/barrierSet.hpp"
28 #include "gc/shared/c2/barrierSetC2.hpp"
29 #include "libadt/vectset.hpp"
30 #include "memory/allocation.hpp"
31 #include "memory/resourceArea.hpp"
32 #include "opto/c2compiler.hpp"
33 #include "opto/arraycopynode.hpp"
34 #include "opto/callnode.hpp"
35 #include "opto/cfgnode.hpp"
36 #include "opto/compile.hpp"
37 #include "opto/escape.hpp"
38 #include "opto/macro.hpp"
39 #include "opto/locknode.hpp"
40 #include "opto/phaseX.hpp"
41 #include "opto/movenode.hpp"
42 #include "opto/narrowptrnode.hpp"
43 #include "opto/castnode.hpp"
44 #include "opto/rootnode.hpp"
45 #include "utilities/macros.hpp"
46
47 ConnectionGraph::ConnectionGraph(Compile * C, PhaseIterGVN *igvn, int invocation) :
48 // If ReduceAllocationMerges is enabled we might call split_through_phi during
49 // split_unique_types and that will create additional nodes that need to be
50 // pushed to the ConnectionGraph. The code below bumps the initial capacity of
51 // _nodes by 10% to account for these additional nodes. If capacity is exceeded
52 // the array will be reallocated.
53 _nodes(C->comp_arena(), C->do_reduce_allocation_merges() ? C->unique()*1.10 : C->unique(), C->unique(), nullptr),
54 _in_worklist(C->comp_arena()),
55 _next_pidx(0),
56 _collecting(true),
57 _verify(false),
146 GrowableArray<SafePointNode*> sfn_worklist;
147 GrowableArray<MergeMemNode*> mergemem_worklist;
148 DEBUG_ONLY( GrowableArray<Node*> addp_worklist; )
149
150 { Compile::TracePhase tp(Phase::_t_connectionGraph);
151
152 // 1. Populate Connection Graph (CG) with PointsTo nodes.
153 ideal_nodes.map(C->live_nodes(), nullptr); // preallocate space
154 // Initialize worklist
155 if (C->root() != nullptr) {
156 ideal_nodes.push(C->root());
157 }
158 // Processed ideal nodes are unique on ideal_nodes list
159 // but several ideal nodes are mapped to the phantom_obj.
160 // To avoid duplicated entries on the following worklists
161 // add the phantom_obj only once to them.
162 ptnodes_worklist.append(phantom_obj);
163 java_objects_worklist.append(phantom_obj);
164 for( uint next = 0; next < ideal_nodes.size(); ++next ) {
165 Node* n = ideal_nodes.at(next);
166 // Create PointsTo nodes and add them to Connection Graph. Called
167 // only once per ideal node since ideal_nodes is Unique_Node list.
168 add_node_to_connection_graph(n, &delayed_worklist);
169 PointsToNode* ptn = ptnode_adr(n->_idx);
170 if (ptn != nullptr && ptn != phantom_obj) {
171 ptnodes_worklist.append(ptn);
172 if (ptn->is_JavaObject()) {
173 java_objects_worklist.append(ptn->as_JavaObject());
174 if ((n->is_Allocate() || n->is_CallStaticJava()) &&
175 (ptn->escape_state() < PointsToNode::GlobalEscape)) {
176 // Only allocations and java static calls results are interesting.
177 non_escaped_allocs_worklist.append(ptn->as_JavaObject());
178 }
179 } else if (ptn->is_Field() && ptn->as_Field()->is_oop()) {
180 oop_fields_worklist.append(ptn->as_Field());
181 }
182 }
183 // Collect some interesting nodes for further use.
184 switch (n->Opcode()) {
185 case Op_MergeMem:
1235
1236 // The next two inputs are:
1237 // (1) A copy of the original pointer to NSR objects.
1238 // (2) A selector, used to decide if we need to rematerialize an object
1239 // or use the pointer to a NSR object.
1240 // See more details of these fields in the declaration of SafePointScalarMergeNode
1241 sfpt->add_req(nsr_merge_pointer);
1242 sfpt->add_req(selector);
1243
1244 for (uint i = 1; i < ophi->req(); i++) {
1245 Node* base = ophi->in(i);
1246 JavaObjectNode* ptn = unique_java_object(base);
1247
1248 // If the base is not scalar replaceable we don't need to register information about
1249 // it at this time.
1250 if (ptn == nullptr || !ptn->scalar_replaceable()) {
1251 continue;
1252 }
1253
1254 AllocateNode* alloc = ptn->ideal_node()->as_Allocate();
1255 SafePointScalarObjectNode* sobj = mexp.create_scalarized_object_description(alloc, sfpt);
1256 if (sobj == nullptr) {
1257 return false;
1258 }
1259
1260 // Now make a pass over the debug information replacing any references
1261 // to the allocated object with "sobj"
1262 Node* ccpp = alloc->result_cast();
1263 sfpt->replace_edges_in_range(ccpp, sobj, debug_start, jvms->debug_end(), _igvn);
1264
1265 // Register the scalarized object as a candidate for reallocation
1266 smerge->add_req(sobj);
1267 }
1268
1269 // Replaces debug information references to "original_sfpt_parent" in "sfpt" with references to "smerge"
1270 sfpt->replace_edges_in_range(original_sfpt_parent, smerge, debug_start, jvms->debug_end(), _igvn);
1271
1272 // The call to 'replace_edges_in_range' above might have removed the
1273 // reference to ophi that we need at _merge_pointer_idx. The line below make
1274 // sure the reference is maintained.
1275 sfpt->set_req(smerge->merge_pointer_idx(jvms), nsr_merge_pointer);
1276 _igvn->_worklist.push(sfpt);
1277 }
1278
1279 return true;
1280 }
1281
1282 void ConnectionGraph::reduce_phi(PhiNode* ophi, GrowableArray<Node *> &alloc_worklist, GrowableArray<Node *> &memnode_worklist) {
1283 bool delay = _igvn->delay_transform();
1284 _igvn->set_delay_transform(true);
1285 _igvn->hash_delete(ophi);
1286
1445 return false;
1446 }
1447
1448 // Returns true if at least one of the arguments to the call is an object
1449 // that does not escape globally.
1450 bool ConnectionGraph::has_arg_escape(CallJavaNode* call) {
1451 if (call->method() != nullptr) {
1452 uint max_idx = TypeFunc::Parms + call->method()->arg_size();
1453 for (uint idx = TypeFunc::Parms; idx < max_idx; idx++) {
1454 Node* p = call->in(idx);
1455 if (not_global_escape(p)) {
1456 return true;
1457 }
1458 }
1459 } else {
1460 const char* name = call->as_CallStaticJava()->_name;
1461 assert(name != nullptr, "no name");
1462 // no arg escapes through uncommon traps
1463 if (strcmp(name, "uncommon_trap") != 0) {
1464 // process_call_arguments() assumes that all arguments escape globally
1465 const TypeTuple* d = call->tf()->domain();
1466 for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
1467 const Type* at = d->field_at(i);
1468 if (at->isa_oopptr() != nullptr) {
1469 return true;
1470 }
1471 }
1472 }
1473 }
1474 return false;
1475 }
1476
1477
1478
1479 // Utility function for nodes that load an object
1480 void ConnectionGraph::add_objload_to_connection_graph(Node *n, Unique_Node_List *delayed_worklist) {
1481 // Using isa_ptr() instead of isa_oopptr() for LoadP and Phi because
1482 // ThreadLocal has RawPtr type.
1483 const Type* t = _igvn->type(n);
1484 if (t->make_ptr() != nullptr) {
1485 Node* adr = n->in(MemNode::Address);
1519 // first IGVN optimization when escape information is still available.
1520 record_for_optimizer(n);
1521 } else if (n->is_Allocate()) {
1522 add_call_node(n->as_Call());
1523 record_for_optimizer(n);
1524 } else {
1525 if (n->is_CallStaticJava()) {
1526 const char* name = n->as_CallStaticJava()->_name;
1527 if (name != nullptr && strcmp(name, "uncommon_trap") == 0) {
1528 return; // Skip uncommon traps
1529 }
1530 }
1531 // Don't mark as processed since call's arguments have to be processed.
1532 delayed_worklist->push(n);
1533 // Check if a call returns an object.
1534 if ((n->as_Call()->returns_pointer() &&
1535 n->as_Call()->proj_out_or_null(TypeFunc::Parms) != nullptr) ||
1536 (n->is_CallStaticJava() &&
1537 n->as_CallStaticJava()->is_boxing_method())) {
1538 add_call_node(n->as_Call());
1539 }
1540 }
1541 return;
1542 }
1543 // Put this check here to process call arguments since some call nodes
1544 // point to phantom_obj.
1545 if (n_ptn == phantom_obj || n_ptn == null_obj) {
1546 return; // Skip predefined nodes.
1547 }
1548 switch (opcode) {
1549 case Op_AddP: {
1550 Node* base = get_addp_base(n);
1551 PointsToNode* ptn_base = ptnode_adr(base->_idx);
1552 // Field nodes are created for all field types. They are used in
1553 // adjust_scalar_replaceable_state() and split_unique_types().
1554 // Note, non-oop fields will have only base edges in Connection
1555 // Graph because such fields are not used for oop loads and stores.
1556 int offset = address_offset(n, igvn);
1557 add_field(n, PointsToNode::NoEscape, offset);
1558 if (ptn_base == nullptr) {
1559 delayed_worklist->push(n); // Process it later.
1560 } else {
1561 n_ptn = ptnode_adr(n_idx);
1562 add_base(n_ptn->as_Field(), ptn_base);
1563 }
1564 break;
1565 }
1566 case Op_CastX2P: {
1567 map_ideal_node(n, phantom_obj);
1568 break;
1569 }
1570 case Op_CastPP:
1571 case Op_CheckCastPP:
1572 case Op_EncodeP:
1573 case Op_DecodeN:
1574 case Op_EncodePKlass:
1575 case Op_DecodeNKlass: {
1576 add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(1), delayed_worklist);
1577 break;
1578 }
1579 case Op_CMoveP: {
1580 add_local_var(n, PointsToNode::NoEscape);
1581 // Do not add edges during first iteration because some could be
1582 // not defined yet.
1583 delayed_worklist->push(n);
1584 break;
1585 }
1586 case Op_ConP:
1587 case Op_ConN:
1588 case Op_ConNKlass: {
1589 // assume all oop constants globally escape except for null
1621 case Op_PartialSubtypeCheck: {
1622 // Produces Null or notNull and is used in only in CmpP so
1623 // phantom_obj could be used.
1624 map_ideal_node(n, phantom_obj); // Result is unknown
1625 break;
1626 }
1627 case Op_Phi: {
1628 // Using isa_ptr() instead of isa_oopptr() for LoadP and Phi because
1629 // ThreadLocal has RawPtr type.
1630 const Type* t = n->as_Phi()->type();
1631 if (t->make_ptr() != nullptr) {
1632 add_local_var(n, PointsToNode::NoEscape);
1633 // Do not add edges during first iteration because some could be
1634 // not defined yet.
1635 delayed_worklist->push(n);
1636 }
1637 break;
1638 }
1639 case Op_Proj: {
1640 // we are only interested in the oop result projection from a call
1641 if (n->as_Proj()->_con == TypeFunc::Parms && n->in(0)->is_Call() &&
1642 n->in(0)->as_Call()->returns_pointer()) {
1643 add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(0), delayed_worklist);
1644 }
1645 break;
1646 }
1647 case Op_Rethrow: // Exception object escapes
1648 case Op_Return: {
1649 if (n->req() > TypeFunc::Parms &&
1650 igvn->type(n->in(TypeFunc::Parms))->isa_oopptr()) {
1651 // Treat Return value as LocalVar with GlobalEscape escape state.
1652 add_local_var_and_edge(n, PointsToNode::GlobalEscape, n->in(TypeFunc::Parms), delayed_worklist);
1653 }
1654 break;
1655 }
1656 case Op_CompareAndExchangeP:
1657 case Op_CompareAndExchangeN:
1658 case Op_GetAndSetP:
1659 case Op_GetAndSetN: {
1660 add_objload_to_connection_graph(n, delayed_worklist);
1661 // fall-through
1662 }
1724 if (n->is_Call()) {
1725 process_call_arguments(n->as_Call());
1726 return;
1727 }
1728 assert(n->is_Store() || n->is_LoadStore() ||
1729 ((n_ptn != nullptr) && (n_ptn->ideal_node() != nullptr)),
1730 "node should be registered already");
1731 int opcode = n->Opcode();
1732 bool gc_handled = BarrierSet::barrier_set()->barrier_set_c2()->escape_add_final_edges(this, _igvn, n, opcode);
1733 if (gc_handled) {
1734 return; // Ignore node if already handled by GC.
1735 }
1736 switch (opcode) {
1737 case Op_AddP: {
1738 Node* base = get_addp_base(n);
1739 PointsToNode* ptn_base = ptnode_adr(base->_idx);
1740 assert(ptn_base != nullptr, "field's base should be registered");
1741 add_base(n_ptn->as_Field(), ptn_base);
1742 break;
1743 }
1744 case Op_CastPP:
1745 case Op_CheckCastPP:
1746 case Op_EncodeP:
1747 case Op_DecodeN:
1748 case Op_EncodePKlass:
1749 case Op_DecodeNKlass: {
1750 add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(1), nullptr);
1751 break;
1752 }
1753 case Op_CMoveP: {
1754 for (uint i = CMoveNode::IfFalse; i < n->req(); i++) {
1755 Node* in = n->in(i);
1756 if (in == nullptr) {
1757 continue; // ignore null
1758 }
1759 Node* uncast_in = in->uncast();
1760 if (uncast_in->is_top() || uncast_in == n) {
1761 continue; // ignore top or inputs which go back this node
1762 }
1763 PointsToNode* ptn = ptnode_adr(in->_idx);
1778 // Using isa_ptr() instead of isa_oopptr() for LoadP and Phi because
1779 // ThreadLocal has RawPtr type.
1780 assert(n->as_Phi()->type()->make_ptr() != nullptr, "Unexpected node type");
1781 for (uint i = 1; i < n->req(); i++) {
1782 Node* in = n->in(i);
1783 if (in == nullptr) {
1784 continue; // ignore null
1785 }
1786 Node* uncast_in = in->uncast();
1787 if (uncast_in->is_top() || uncast_in == n) {
1788 continue; // ignore top or inputs which go back this node
1789 }
1790 PointsToNode* ptn = ptnode_adr(in->_idx);
1791 assert(ptn != nullptr, "node should be registered");
1792 add_edge(n_ptn, ptn);
1793 }
1794 break;
1795 }
1796 case Op_Proj: {
1797 // we are only interested in the oop result projection from a call
1798 assert(n->as_Proj()->_con == TypeFunc::Parms && n->in(0)->is_Call() &&
1799 n->in(0)->as_Call()->returns_pointer(), "Unexpected node type");
1800 add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(0), nullptr);
1801 break;
1802 }
1803 case Op_Rethrow: // Exception object escapes
1804 case Op_Return: {
1805 assert(n->req() > TypeFunc::Parms && _igvn->type(n->in(TypeFunc::Parms))->isa_oopptr(),
1806 "Unexpected node type");
1807 // Treat Return value as LocalVar with GlobalEscape escape state.
1808 add_local_var_and_edge(n, PointsToNode::GlobalEscape, n->in(TypeFunc::Parms), nullptr);
1809 break;
1810 }
1811 case Op_CompareAndExchangeP:
1812 case Op_CompareAndExchangeN:
1813 case Op_GetAndSetP:
1814 case Op_GetAndSetN:{
1815 assert(_igvn->type(n)->make_ptr() != nullptr, "Unexpected node type");
1816 add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(MemNode::Address), nullptr);
1817 // fall-through
1818 }
1819 case Op_CompareAndSwapP:
1955 PointsToNode* ptn = ptnode_adr(val->_idx);
1956 assert(ptn != nullptr, "node should be registered");
1957 set_escape_state(ptn, PointsToNode::GlobalEscape NOT_PRODUCT(COMMA "stored at raw address"));
1958 // Add edge to object for unsafe access with offset.
1959 PointsToNode* adr_ptn = ptnode_adr(adr->_idx);
1960 assert(adr_ptn != nullptr, "node should be registered");
1961 if (adr_ptn->is_Field()) {
1962 assert(adr_ptn->as_Field()->is_oop(), "should be oop field");
1963 add_edge(adr_ptn, ptn);
1964 }
1965 return true;
1966 }
1967 #ifdef ASSERT
1968 n->dump(1);
1969 assert(false, "not unsafe");
1970 #endif
1971 return false;
1972 }
1973
1974 void ConnectionGraph::add_call_node(CallNode* call) {
1975 assert(call->returns_pointer(), "only for call which returns pointer");
1976 uint call_idx = call->_idx;
1977 if (call->is_Allocate()) {
1978 Node* k = call->in(AllocateNode::KlassNode);
1979 const TypeKlassPtr* kt = k->bottom_type()->isa_klassptr();
1980 assert(kt != nullptr, "TypeKlassPtr required.");
1981 PointsToNode::EscapeState es = PointsToNode::NoEscape;
1982 bool scalar_replaceable = true;
1983 NOT_PRODUCT(const char* nsr_reason = "");
1984 if (call->is_AllocateArray()) {
1985 if (!kt->isa_aryklassptr()) { // StressReflectiveCode
1986 es = PointsToNode::GlobalEscape;
1987 } else {
1988 int length = call->in(AllocateNode::ALength)->find_int_con(-1);
1989 if (length < 0) {
1990 // Not scalar replaceable if the length is not constant.
1991 scalar_replaceable = false;
1992 NOT_PRODUCT(nsr_reason = "has a non-constant length");
1993 } else if (length > EliminateAllocationArraySizeLimit) {
1994 // Not scalar replaceable if the length is too big.
1995 scalar_replaceable = false;
2031 //
2032 // - all oop arguments are escaping globally;
2033 //
2034 // 2. CallStaticJavaNode (execute bytecode analysis if possible):
2035 //
2036 // - the same as CallDynamicJavaNode if can't do bytecode analysis;
2037 //
2038 // - mapped to GlobalEscape JavaObject node if unknown oop is returned;
2039 // - mapped to NoEscape JavaObject node if non-escaping object allocated
2040 // during call is returned;
2041 // - mapped to ArgEscape LocalVar node pointed to object arguments
2042 // which are returned and does not escape during call;
2043 //
2044 // - oop arguments escaping status is defined by bytecode analysis;
2045 //
2046 // For a static call, we know exactly what method is being called.
2047 // Use bytecode estimator to record whether the call's return value escapes.
2048 ciMethod* meth = call->as_CallJava()->method();
2049 if (meth == nullptr) {
2050 const char* name = call->as_CallStaticJava()->_name;
2051 assert(strncmp(name, "C2 Runtime multianewarray", 25) == 0, "TODO: add failed case check");
2052 // Returns a newly allocated non-escaped object.
2053 add_java_object(call, PointsToNode::NoEscape);
2054 set_not_scalar_replaceable(ptnode_adr(call_idx) NOT_PRODUCT(COMMA "is result of multinewarray"));
2055 } else if (meth->is_boxing_method()) {
2056 // Returns boxing object
2057 PointsToNode::EscapeState es;
2058 vmIntrinsics::ID intr = meth->intrinsic_id();
2059 if (intr == vmIntrinsics::_floatValue || intr == vmIntrinsics::_doubleValue) {
2060 // It does not escape if object is always allocated.
2061 es = PointsToNode::NoEscape;
2062 } else {
2063 // It escapes globally if object could be loaded from cache.
2064 es = PointsToNode::GlobalEscape;
2065 }
2066 add_java_object(call, es);
2067 if (es == PointsToNode::GlobalEscape) {
2068 set_not_scalar_replaceable(ptnode_adr(call->_idx) NOT_PRODUCT(COMMA "object can be loaded from boxing cache"));
2069 }
2070 } else {
2071 BCEscapeAnalyzer* call_analyzer = meth->get_bcea();
2072 call_analyzer->copy_dependencies(_compile->dependencies());
2073 if (call_analyzer->is_return_allocated()) {
2074 // Returns a newly allocated non-escaped object, simply
2075 // update dependency information.
2076 // Mark it as NoEscape so that objects referenced by
2077 // it's fields will be marked as NoEscape at least.
2078 add_java_object(call, PointsToNode::NoEscape);
2079 set_not_scalar_replaceable(ptnode_adr(call_idx) NOT_PRODUCT(COMMA "is result of call"));
2080 } else {
2081 // Determine whether any arguments are returned.
2082 const TypeTuple* d = call->tf()->domain();
2083 bool ret_arg = false;
2084 for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
2085 if (d->field_at(i)->isa_ptr() != nullptr &&
2086 call_analyzer->is_arg_returned(i - TypeFunc::Parms)) {
2087 ret_arg = true;
2088 break;
2089 }
2090 }
2091 if (ret_arg) {
2092 add_local_var(call, PointsToNode::ArgEscape);
2093 } else {
2094 // Returns unknown object.
2095 map_ideal_node(call, phantom_obj);
2096 }
2097 }
2098 }
2099 } else {
2100 // An other type of call, assume the worst case:
2101 // returned value is unknown and globally escapes.
2102 assert(call->Opcode() == Op_CallDynamicJava, "add failed case check");
2110 #ifdef ASSERT
2111 case Op_Allocate:
2112 case Op_AllocateArray:
2113 case Op_Lock:
2114 case Op_Unlock:
2115 assert(false, "should be done already");
2116 break;
2117 #endif
2118 case Op_ArrayCopy:
2119 case Op_CallLeafNoFP:
2120 // Most array copies are ArrayCopy nodes at this point but there
2121 // are still a few direct calls to the copy subroutines (See
2122 // PhaseStringOpts::copy_string())
2123 is_arraycopy = (call->Opcode() == Op_ArrayCopy) ||
2124 call->as_CallLeaf()->is_call_to_arraycopystub();
2125 // fall through
2126 case Op_CallLeafVector:
2127 case Op_CallLeaf: {
2128 // Stub calls, objects do not escape but they are not scale replaceable.
2129 // Adjust escape state for outgoing arguments.
2130 const TypeTuple * d = call->tf()->domain();
2131 bool src_has_oops = false;
2132 for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
2133 const Type* at = d->field_at(i);
2134 Node *arg = call->in(i);
2135 if (arg == nullptr) {
2136 continue;
2137 }
2138 const Type *aat = _igvn->type(arg);
2139 if (arg->is_top() || !at->isa_ptr() || !aat->isa_ptr()) {
2140 continue;
2141 }
2142 if (arg->is_AddP()) {
2143 //
2144 // The inline_native_clone() case when the arraycopy stub is called
2145 // after the allocation before Initialize and CheckCastPP nodes.
2146 // Or normal arraycopy for object arrays case.
2147 //
2148 // Set AddP's base (Allocate) as not scalar replaceable since
2149 // pointer to the base (with offset) is passed as argument.
2150 //
2151 arg = get_addp_base(arg);
2152 }
2153 PointsToNode* arg_ptn = ptnode_adr(arg->_idx);
2154 assert(arg_ptn != nullptr, "should be registered");
2155 PointsToNode::EscapeState arg_esc = arg_ptn->escape_state();
2156 if (is_arraycopy || arg_esc < PointsToNode::ArgEscape) {
2157 assert(aat == Type::TOP || aat == TypePtr::NULL_PTR ||
2158 aat->isa_ptr() != nullptr, "expecting an Ptr");
2159 bool arg_has_oops = aat->isa_oopptr() &&
2160 (aat->isa_instptr() ||
2161 (aat->isa_aryptr() && (aat->isa_aryptr()->elem() == Type::BOTTOM || aat->isa_aryptr()->elem()->make_oopptr() != nullptr)));
2162 if (i == TypeFunc::Parms) {
2163 src_has_oops = arg_has_oops;
2164 }
2165 //
2166 // src or dst could be j.l.Object when other is basic type array:
2167 //
2168 // arraycopy(char[],0,Object*,0,size);
2169 // arraycopy(Object*,0,char[],0,size);
2170 //
2171 // Don't add edges in such cases.
2172 //
2173 bool arg_is_arraycopy_dest = src_has_oops && is_arraycopy &&
2174 arg_has_oops && (i > TypeFunc::Parms);
2175 #ifdef ASSERT
2176 if (!(is_arraycopy ||
2177 BarrierSet::barrier_set()->barrier_set_c2()->is_gc_barrier_node(call) ||
2178 (call->as_CallLeaf()->_name != nullptr &&
2179 (strcmp(call->as_CallLeaf()->_name, "updateBytesCRC32") == 0 ||
2180 strcmp(call->as_CallLeaf()->_name, "updateBytesCRC32C") == 0 ||
2181 strcmp(call->as_CallLeaf()->_name, "updateBytesAdler32") == 0 ||
2205 strcmp(call->as_CallLeaf()->_name, "dilithiumMontMulByConstant") == 0 ||
2206 strcmp(call->as_CallLeaf()->_name, "dilithiumDecomposePoly") == 0 ||
2207 strcmp(call->as_CallLeaf()->_name, "encodeBlock") == 0 ||
2208 strcmp(call->as_CallLeaf()->_name, "decodeBlock") == 0 ||
2209 strcmp(call->as_CallLeaf()->_name, "md5_implCompress") == 0 ||
2210 strcmp(call->as_CallLeaf()->_name, "md5_implCompressMB") == 0 ||
2211 strcmp(call->as_CallLeaf()->_name, "sha1_implCompress") == 0 ||
2212 strcmp(call->as_CallLeaf()->_name, "sha1_implCompressMB") == 0 ||
2213 strcmp(call->as_CallLeaf()->_name, "sha256_implCompress") == 0 ||
2214 strcmp(call->as_CallLeaf()->_name, "sha256_implCompressMB") == 0 ||
2215 strcmp(call->as_CallLeaf()->_name, "sha512_implCompress") == 0 ||
2216 strcmp(call->as_CallLeaf()->_name, "sha512_implCompressMB") == 0 ||
2217 strcmp(call->as_CallLeaf()->_name, "sha3_implCompress") == 0 ||
2218 strcmp(call->as_CallLeaf()->_name, "double_keccak") == 0 ||
2219 strcmp(call->as_CallLeaf()->_name, "sha3_implCompressMB") == 0 ||
2220 strcmp(call->as_CallLeaf()->_name, "multiplyToLen") == 0 ||
2221 strcmp(call->as_CallLeaf()->_name, "squareToLen") == 0 ||
2222 strcmp(call->as_CallLeaf()->_name, "mulAdd") == 0 ||
2223 strcmp(call->as_CallLeaf()->_name, "montgomery_multiply") == 0 ||
2224 strcmp(call->as_CallLeaf()->_name, "montgomery_square") == 0 ||
2225 strcmp(call->as_CallLeaf()->_name, "bigIntegerRightShiftWorker") == 0 ||
2226 strcmp(call->as_CallLeaf()->_name, "bigIntegerLeftShiftWorker") == 0 ||
2227 strcmp(call->as_CallLeaf()->_name, "vectorizedMismatch") == 0 ||
2228 strcmp(call->as_CallLeaf()->_name, "stringIndexOf") == 0 ||
2229 strcmp(call->as_CallLeaf()->_name, "arraysort_stub") == 0 ||
2230 strcmp(call->as_CallLeaf()->_name, "array_partition_stub") == 0 ||
2231 strcmp(call->as_CallLeaf()->_name, "get_class_id_intrinsic") == 0 ||
2232 strcmp(call->as_CallLeaf()->_name, "unsafe_setmemory") == 0)
2233 ))) {
2234 call->dump();
2235 fatal("EA unexpected CallLeaf %s", call->as_CallLeaf()->_name);
2236 }
2237 #endif
2238 // Always process arraycopy's destination object since
2239 // we need to add all possible edges to references in
2240 // source object.
2241 if (arg_esc >= PointsToNode::ArgEscape &&
2242 !arg_is_arraycopy_dest) {
2243 continue;
2244 }
2267 }
2268 }
2269 }
2270 break;
2271 }
2272 case Op_CallStaticJava: {
2273 // For a static call, we know exactly what method is being called.
2274 // Use bytecode estimator to record the call's escape affects
2275 #ifdef ASSERT
2276 const char* name = call->as_CallStaticJava()->_name;
2277 assert((name == nullptr || strcmp(name, "uncommon_trap") != 0), "normal calls only");
2278 #endif
2279 ciMethod* meth = call->as_CallJava()->method();
2280 if ((meth != nullptr) && meth->is_boxing_method()) {
2281 break; // Boxing methods do not modify any oops.
2282 }
2283 BCEscapeAnalyzer* call_analyzer = (meth !=nullptr) ? meth->get_bcea() : nullptr;
2284 // fall-through if not a Java method or no analyzer information
2285 if (call_analyzer != nullptr) {
2286 PointsToNode* call_ptn = ptnode_adr(call->_idx);
2287 const TypeTuple* d = call->tf()->domain();
2288 for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
2289 const Type* at = d->field_at(i);
2290 int k = i - TypeFunc::Parms;
2291 Node* arg = call->in(i);
2292 PointsToNode* arg_ptn = ptnode_adr(arg->_idx);
2293 if (at->isa_ptr() != nullptr &&
2294 call_analyzer->is_arg_returned(k)) {
2295 // The call returns arguments.
2296 if (call_ptn != nullptr) { // Is call's result used?
2297 assert(call_ptn->is_LocalVar(), "node should be registered");
2298 assert(arg_ptn != nullptr, "node should be registered");
2299 add_edge(call_ptn, arg_ptn);
2300 }
2301 }
2302 if (at->isa_oopptr() != nullptr &&
2303 arg_ptn->escape_state() < PointsToNode::GlobalEscape) {
2304 if (!call_analyzer->is_arg_stack(k)) {
2305 // The argument global escapes
2306 set_escape_state(arg_ptn, PointsToNode::GlobalEscape NOT_PRODUCT(COMMA trace_arg_escape_message(call)));
2307 } else {
2311 set_fields_escape_state(arg_ptn, PointsToNode::GlobalEscape NOT_PRODUCT(COMMA trace_arg_escape_message(call)));
2312 }
2313 }
2314 }
2315 }
2316 if (call_ptn != nullptr && call_ptn->is_LocalVar()) {
2317 // The call returns arguments.
2318 assert(call_ptn->edge_count() > 0, "sanity");
2319 if (!call_analyzer->is_return_local()) {
2320 // Returns also unknown object.
2321 add_edge(call_ptn, phantom_obj);
2322 }
2323 }
2324 break;
2325 }
2326 }
2327 default: {
2328 // Fall-through here if not a Java method or no analyzer information
2329 // or some other type of call, assume the worst case: all arguments
2330 // globally escape.
2331 const TypeTuple* d = call->tf()->domain();
2332 for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
2333 const Type* at = d->field_at(i);
2334 if (at->isa_oopptr() != nullptr) {
2335 Node* arg = call->in(i);
2336 if (arg->is_AddP()) {
2337 arg = get_addp_base(arg);
2338 }
2339 assert(ptnode_adr(arg->_idx) != nullptr, "should be defined already");
2340 set_escape_state(ptnode_adr(arg->_idx), PointsToNode::GlobalEscape NOT_PRODUCT(COMMA trace_arg_escape_message(call)));
2341 }
2342 }
2343 }
2344 }
2345 }
2346
2347
2348 // Finish Graph construction.
2349 bool ConnectionGraph::complete_connection_graph(
2350 GrowableArray<PointsToNode*>& ptnodes_worklist,
2351 GrowableArray<JavaObjectNode*>& non_escaped_allocs_worklist,
2724 PointsToNode* base = i.get();
2725 if (base->is_JavaObject()) {
2726 // Skip Allocate's fields which will be processed later.
2727 if (base->ideal_node()->is_Allocate()) {
2728 return 0;
2729 }
2730 assert(base == null_obj, "only null ptr base expected here");
2731 }
2732 }
2733 if (add_edge(field, phantom_obj)) {
2734 // New edge was added
2735 new_edges++;
2736 add_field_uses_to_worklist(field);
2737 }
2738 return new_edges;
2739 }
2740
2741 // Find fields initializing values for allocations.
2742 int ConnectionGraph::find_init_values_phantom(JavaObjectNode* pta) {
2743 assert(pta->escape_state() == PointsToNode::NoEscape, "Not escaped Allocate nodes only");
2744 Node* alloc = pta->ideal_node();
2745
2746 // Do nothing for Allocate nodes since its fields values are
2747 // "known" unless they are initialized by arraycopy/clone.
2748 if (alloc->is_Allocate() && !pta->arraycopy_dst()) {
2749 return 0;
2750 }
2751 assert(pta->arraycopy_dst() || alloc->as_CallStaticJava(), "sanity");
2752 #ifdef ASSERT
2753 if (!pta->arraycopy_dst() && alloc->as_CallStaticJava()->method() == nullptr) {
2754 const char* name = alloc->as_CallStaticJava()->_name;
2755 assert(strncmp(name, "C2 Runtime multianewarray", 25) == 0, "sanity");
2756 }
2757 #endif
2758 // Non-escaped allocation returned from Java or runtime call have unknown values in fields.
2759 int new_edges = 0;
2760 for (EdgeIterator i(pta); i.has_next(); i.next()) {
2761 PointsToNode* field = i.get();
2762 if (field->is_Field() && field->as_Field()->is_oop()) {
2763 if (add_edge(field, phantom_obj)) {
2764 // New edge was added
2765 new_edges++;
2766 add_field_uses_to_worklist(field->as_Field());
2767 }
2768 }
2769 }
2770 return new_edges;
2771 }
2772
2773 // Find fields initializing values for allocations.
2774 int ConnectionGraph::find_init_values_null(JavaObjectNode* pta, PhaseValues* phase) {
2775 assert(pta->escape_state() == PointsToNode::NoEscape, "Not escaped Allocate nodes only");
2776 Node* alloc = pta->ideal_node();
2777 // Do nothing for Call nodes since its fields values are unknown.
2778 if (!alloc->is_Allocate()) {
2779 return 0;
2780 }
2781 InitializeNode* ini = alloc->as_Allocate()->initialization();
2782 bool visited_bottom_offset = false;
2783 GrowableArray<int> offsets_worklist;
2784 int new_edges = 0;
2785
2786 // Check if an oop field's initializing value is recorded and add
2787 // a corresponding null if field's value if it is not recorded.
2788 // Connection Graph does not record a default initialization by null
2789 // captured by Initialize node.
2790 //
2791 for (EdgeIterator i(pta); i.has_next(); i.next()) {
2792 PointsToNode* field = i.get(); // Field (AddP)
2793 if (!field->is_Field() || !field->as_Field()->is_oop()) {
2794 continue; // Not oop field
2795 }
2796 int offset = field->as_Field()->offset();
2797 if (offset == Type::OffsetBot) {
2798 if (!visited_bottom_offset) {
2844 } else {
2845 if (!val->is_LocalVar() || (val->edge_count() == 0)) {
2846 tty->print_cr("----------init store has invalid value -----");
2847 store->dump();
2848 val->dump();
2849 assert(val->is_LocalVar() && (val->edge_count() > 0), "should be processed already");
2850 }
2851 for (EdgeIterator j(val); j.has_next(); j.next()) {
2852 PointsToNode* obj = j.get();
2853 if (obj->is_JavaObject()) {
2854 if (!field->points_to(obj->as_JavaObject())) {
2855 missed_obj = obj;
2856 break;
2857 }
2858 }
2859 }
2860 }
2861 if (missed_obj != nullptr) {
2862 tty->print_cr("----------field---------------------------------");
2863 field->dump();
2864 tty->print_cr("----------missed referernce to object-----------");
2865 missed_obj->dump();
2866 tty->print_cr("----------object referernced by init store -----");
2867 store->dump();
2868 val->dump();
2869 assert(!field->points_to(missed_obj->as_JavaObject()), "missed JavaObject reference");
2870 }
2871 }
2872 #endif
2873 } else {
2874 // There could be initializing stores which follow allocation.
2875 // For example, a volatile field store is not collected
2876 // by Initialize node.
2877 //
2878 // Need to check for dependent loads to separate such stores from
2879 // stores which follow loads. For now, add initial value null so
2880 // that compare pointers optimization works correctly.
2881 }
2882 }
2883 if (value == nullptr) {
2884 // A field's initializing value was not recorded. Add null.
2885 if (add_edge(field, null_obj)) {
2886 // New edge was added
3202 assert(field->edge_count() > 0, "sanity");
3203 }
3204 }
3205 }
3206 }
3207 #endif
3208
3209 // Optimize ideal graph.
3210 void ConnectionGraph::optimize_ideal_graph(GrowableArray<Node*>& ptr_cmp_worklist,
3211 GrowableArray<MemBarStoreStoreNode*>& storestore_worklist) {
3212 Compile* C = _compile;
3213 PhaseIterGVN* igvn = _igvn;
3214 if (EliminateLocks) {
3215 // Mark locks before changing ideal graph.
3216 int cnt = C->macro_count();
3217 for (int i = 0; i < cnt; i++) {
3218 Node *n = C->macro_node(i);
3219 if (n->is_AbstractLock()) { // Lock and Unlock nodes
3220 AbstractLockNode* alock = n->as_AbstractLock();
3221 if (!alock->is_non_esc_obj()) {
3222 if (can_eliminate_lock(alock)) {
3223 assert(!alock->is_eliminated() || alock->is_coarsened(), "sanity");
3224 // The lock could be marked eliminated by lock coarsening
3225 // code during first IGVN before EA. Replace coarsened flag
3226 // to eliminate all associated locks/unlocks.
3227 #ifdef ASSERT
3228 alock->log_lock_optimization(C, "eliminate_lock_set_non_esc3");
3229 #endif
3230 alock->set_non_esc_obj();
3231 }
3232 }
3233 }
3234 }
3235 }
3236
3237 if (OptimizePtrCompare) {
3238 for (int i = 0; i < ptr_cmp_worklist.length(); i++) {
3239 Node *n = ptr_cmp_worklist.at(i);
3240 assert(n->Opcode() == Op_CmpN || n->Opcode() == Op_CmpP, "must be");
3241 const TypeInt* tcmp = optimize_ptr_compare(n->in(1), n->in(2));
3242 if (tcmp->singleton()) {
3244 #ifndef PRODUCT
3245 if (PrintOptimizePtrCompare) {
3246 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"));
3247 if (Verbose) {
3248 n->dump(1);
3249 }
3250 }
3251 #endif
3252 igvn->replace_node(n, cmp);
3253 }
3254 }
3255 }
3256
3257 // For MemBarStoreStore nodes added in library_call.cpp, check
3258 // escape status of associated AllocateNode and optimize out
3259 // MemBarStoreStore node if the allocated object never escapes.
3260 for (int i = 0; i < storestore_worklist.length(); i++) {
3261 Node* storestore = storestore_worklist.at(i);
3262 Node* alloc = storestore->in(MemBarNode::Precedent)->in(0);
3263 if (alloc->is_Allocate() && not_global_escape(alloc)) {
3264 MemBarNode* mb = MemBarNode::make(C, Op_MemBarCPUOrder, Compile::AliasIdxBot);
3265 mb->init_req(TypeFunc::Memory, storestore->in(TypeFunc::Memory));
3266 mb->init_req(TypeFunc::Control, storestore->in(TypeFunc::Control));
3267 igvn->register_new_node_with_optimizer(mb);
3268 igvn->replace_node(storestore, mb);
3269 }
3270 }
3271 }
3272
3273 // Optimize objects compare.
3274 const TypeInt* ConnectionGraph::optimize_ptr_compare(Node* left, Node* right) {
3275 assert(OptimizePtrCompare, "sanity");
3276 const TypeInt* EQ = TypeInt::CC_EQ; // [0] == ZERO
3277 const TypeInt* NE = TypeInt::CC_GT; // [1] == ONE
3278 const TypeInt* UNKNOWN = TypeInt::CC; // [-1, 0,1]
3279
3280 PointsToNode* ptn1 = ptnode_adr(left->_idx);
3281 PointsToNode* ptn2 = ptnode_adr(right->_idx);
3282 JavaObjectNode* jobj1 = unique_java_object(left);
3283 JavaObjectNode* jobj2 = unique_java_object(right);
3284
3285 // The use of this method during allocation merge reduction may cause 'left'
3286 // or 'right' be something (e.g., a Phi) that isn't in the connection graph or
3287 // that doesn't reference an unique java object.
3288 if (ptn1 == nullptr || ptn2 == nullptr ||
3410 assert(!src->is_Field() && !dst->is_Field(), "only for JavaObject and LocalVar");
3411 assert((src != null_obj) && (dst != null_obj), "not for ConP null");
3412 PointsToNode* ptadr = _nodes.at(n->_idx);
3413 if (ptadr != nullptr) {
3414 assert(ptadr->is_Arraycopy() && ptadr->ideal_node() == n, "sanity");
3415 return;
3416 }
3417 Compile* C = _compile;
3418 ptadr = new (C->comp_arena()) ArraycopyNode(this, n, es);
3419 map_ideal_node(n, ptadr);
3420 // Add edge from arraycopy node to source object.
3421 (void)add_edge(ptadr, src);
3422 src->set_arraycopy_src();
3423 // Add edge from destination object to arraycopy node.
3424 (void)add_edge(dst, ptadr);
3425 dst->set_arraycopy_dst();
3426 }
3427
3428 bool ConnectionGraph::is_oop_field(Node* n, int offset, bool* unsafe) {
3429 const Type* adr_type = n->as_AddP()->bottom_type();
3430 BasicType bt = T_INT;
3431 if (offset == Type::OffsetBot) {
3432 // Check only oop fields.
3433 if (!adr_type->isa_aryptr() ||
3434 adr_type->isa_aryptr()->elem() == Type::BOTTOM ||
3435 adr_type->isa_aryptr()->elem()->make_oopptr() != nullptr) {
3436 // OffsetBot is used to reference array's element. Ignore first AddP.
3437 if (find_second_addp(n, n->in(AddPNode::Base)) == nullptr) {
3438 bt = T_OBJECT;
3439 }
3440 }
3441 } else if (offset != oopDesc::klass_offset_in_bytes()) {
3442 if (adr_type->isa_instptr()) {
3443 ciField* field = _compile->alias_type(adr_type->isa_instptr())->field();
3444 if (field != nullptr) {
3445 bt = field->layout_type();
3446 } else {
3447 // Check for unsafe oop field access
3448 if (n->has_out_with(Op_StoreP, Op_LoadP, Op_StoreN, Op_LoadN) ||
3449 n->has_out_with(Op_GetAndSetP, Op_GetAndSetN, Op_CompareAndExchangeP, Op_CompareAndExchangeN) ||
3450 n->has_out_with(Op_CompareAndSwapP, Op_CompareAndSwapN, Op_WeakCompareAndSwapP, Op_WeakCompareAndSwapN) ||
3451 BarrierSet::barrier_set()->barrier_set_c2()->escape_has_out_with_unsafe_object(n)) {
3452 bt = T_OBJECT;
3453 (*unsafe) = true;
3454 }
3455 }
3456 } else if (adr_type->isa_aryptr()) {
3457 if (offset == arrayOopDesc::length_offset_in_bytes()) {
3458 // Ignore array length load.
3459 } else if (find_second_addp(n, n->in(AddPNode::Base)) != nullptr) {
3460 // Ignore first AddP.
3461 } else {
3462 const Type* elemtype = adr_type->isa_aryptr()->elem();
3463 bt = elemtype->array_element_basic_type();
3464 }
3465 } else if (adr_type->isa_rawptr() || adr_type->isa_klassptr()) {
3466 // Allocation initialization, ThreadLocal field access, unsafe access
3467 if (n->has_out_with(Op_StoreP, Op_LoadP, Op_StoreN, Op_LoadN) ||
3468 n->has_out_with(Op_GetAndSetP, Op_GetAndSetN, Op_CompareAndExchangeP, Op_CompareAndExchangeN) ||
3469 n->has_out_with(Op_CompareAndSwapP, Op_CompareAndSwapN, Op_WeakCompareAndSwapP, Op_WeakCompareAndSwapN) ||
3470 BarrierSet::barrier_set()->barrier_set_c2()->escape_has_out_with_unsafe_object(n)) {
3471 bt = T_OBJECT;
3472 }
3473 }
3474 }
3475 // Note: T_NARROWOOP is not classed as a real reference type
3476 return (is_reference_type(bt) || bt == T_NARROWOOP);
3477 }
3478
3479 // Returns unique pointed java object or null.
3480 JavaObjectNode* ConnectionGraph::unique_java_object(Node *n) const {
3481 // If the node was created after the escape computation we can't answer.
3482 uint idx = n->_idx;
3483 if (idx >= nodes_size()) {
3640 return true;
3641 }
3642 }
3643 }
3644 }
3645 }
3646 return false;
3647 }
3648
3649 int ConnectionGraph::address_offset(Node* adr, PhaseValues* phase) {
3650 const Type *adr_type = phase->type(adr);
3651 if (adr->is_AddP() && adr_type->isa_oopptr() == nullptr && is_captured_store_address(adr)) {
3652 // We are computing a raw address for a store captured by an Initialize
3653 // compute an appropriate address type. AddP cases #3 and #5 (see below).
3654 int offs = (int)phase->find_intptr_t_con(adr->in(AddPNode::Offset), Type::OffsetBot);
3655 assert(offs != Type::OffsetBot ||
3656 adr->in(AddPNode::Address)->in(0)->is_AllocateArray(),
3657 "offset must be a constant or it is initialization of array");
3658 return offs;
3659 }
3660 const TypePtr *t_ptr = adr_type->isa_ptr();
3661 assert(t_ptr != nullptr, "must be a pointer type");
3662 return t_ptr->offset();
3663 }
3664
3665 Node* ConnectionGraph::get_addp_base(Node *addp) {
3666 assert(addp->is_AddP(), "must be AddP");
3667 //
3668 // AddP cases for Base and Address inputs:
3669 // case #1. Direct object's field reference:
3670 // Allocate
3671 // |
3672 // Proj #5 ( oop result )
3673 // |
3674 // CheckCastPP (cast to instance type)
3675 // | |
3676 // AddP ( base == address )
3677 //
3678 // case #2. Indirect object's field reference:
3679 // Phi
3680 // |
3681 // CastPP (cast to instance type)
3682 // | |
3796 }
3797 return nullptr;
3798 }
3799
3800 //
3801 // Adjust the type and inputs of an AddP which computes the
3802 // address of a field of an instance
3803 //
3804 bool ConnectionGraph::split_AddP(Node *addp, Node *base) {
3805 PhaseGVN* igvn = _igvn;
3806 const TypeOopPtr *base_t = igvn->type(base)->isa_oopptr();
3807 assert(base_t != nullptr && base_t->is_known_instance(), "expecting instance oopptr");
3808 const TypeOopPtr *t = igvn->type(addp)->isa_oopptr();
3809 if (t == nullptr) {
3810 // We are computing a raw address for a store captured by an Initialize
3811 // compute an appropriate address type (cases #3 and #5).
3812 assert(igvn->type(addp) == TypeRawPtr::NOTNULL, "must be raw pointer");
3813 assert(addp->in(AddPNode::Address)->is_Proj(), "base of raw address must be result projection from allocation");
3814 intptr_t offs = (int)igvn->find_intptr_t_con(addp->in(AddPNode::Offset), Type::OffsetBot);
3815 assert(offs != Type::OffsetBot, "offset must be a constant");
3816 t = base_t->add_offset(offs)->is_oopptr();
3817 }
3818 int inst_id = base_t->instance_id();
3819 assert(!t->is_known_instance() || t->instance_id() == inst_id,
3820 "old type must be non-instance or match new type");
3821
3822 // The type 't' could be subclass of 'base_t'.
3823 // As result t->offset() could be large then base_t's size and it will
3824 // cause the failure in add_offset() with narrow oops since TypeOopPtr()
3825 // constructor verifies correctness of the offset.
3826 //
3827 // It could happened on subclass's branch (from the type profiling
3828 // inlining) which was not eliminated during parsing since the exactness
3829 // of the allocation type was not propagated to the subclass type check.
3830 //
3831 // Or the type 't' could be not related to 'base_t' at all.
3832 // It could happened when CHA type is different from MDO type on a dead path
3833 // (for example, from instanceof check) which is not collapsed during parsing.
3834 //
3835 // Do nothing for such AddP node and don't process its users since
3836 // this code branch will go away.
3837 //
3838 if (!t->is_known_instance() &&
3839 !base_t->maybe_java_subtype_of(t)) {
3840 return false; // bail out
3841 }
3842 const TypeOopPtr *tinst = base_t->add_offset(t->offset())->is_oopptr();
3843 // Do NOT remove the next line: ensure a new alias index is allocated
3844 // for the instance type. Note: C++ will not remove it since the call
3845 // has side effect.
3846 int alias_idx = _compile->get_alias_index(tinst);
3847 igvn->set_type(addp, tinst);
3848 // record the allocation in the node map
3849 set_map(addp, get_map(base->_idx));
3850 // Set addp's Base and Address to 'base'.
3851 Node *abase = addp->in(AddPNode::Base);
3852 Node *adr = addp->in(AddPNode::Address);
3853 if (adr->is_Proj() && adr->in(0)->is_Allocate() &&
3854 adr->in(0)->_idx == (uint)inst_id) {
3855 // Skip AddP cases #3 and #5.
3856 } else {
3857 assert(!abase->is_top(), "sanity"); // AddP case #3
3858 if (abase != base) {
3859 igvn->hash_delete(addp);
3860 addp->set_req(AddPNode::Base, base);
3861 if (abase == adr) {
3862 addp->set_req(AddPNode::Address, base);
4528 ptnode_adr(n->_idx)->dump();
4529 assert(jobj != nullptr && jobj != phantom_obj, "escaped allocation");
4530 #endif
4531 _compile->record_failure(_invocation > 0 ? C2Compiler::retry_no_iterative_escape_analysis() : C2Compiler::retry_no_escape_analysis());
4532 return;
4533 } else {
4534 Node *val = get_map(jobj->idx()); // CheckCastPP node
4535 TypeNode *tn = n->as_Type();
4536 const TypeOopPtr* tinst = igvn->type(val)->isa_oopptr();
4537 assert(tinst != nullptr && tinst->is_known_instance() &&
4538 tinst->instance_id() == jobj->idx() , "instance type expected.");
4539
4540 const Type *tn_type = igvn->type(tn);
4541 const TypeOopPtr *tn_t;
4542 if (tn_type->isa_narrowoop()) {
4543 tn_t = tn_type->make_ptr()->isa_oopptr();
4544 } else {
4545 tn_t = tn_type->isa_oopptr();
4546 }
4547 if (tn_t != nullptr && tinst->maybe_java_subtype_of(tn_t)) {
4548 if (tn_type->isa_narrowoop()) {
4549 tn_type = tinst->make_narrowoop();
4550 } else {
4551 tn_type = tinst;
4552 }
4553 igvn->hash_delete(tn);
4554 igvn->set_type(tn, tn_type);
4555 tn->set_type(tn_type);
4556 igvn->hash_insert(tn);
4557 record_for_optimizer(n);
4558 } else {
4559 assert(tn_type == TypePtr::NULL_PTR ||
4560 (tn_t != nullptr && !tinst->maybe_java_subtype_of(tn_t)),
4561 "unexpected type");
4562 continue; // Skip dead path with different type
4563 }
4564 }
4565 } else {
4566 DEBUG_ONLY(n->dump();)
4567 assert(false, "EA: unexpected node");
4568 continue;
4569 }
4570 // push allocation's users on appropriate worklist
4571 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
4572 Node *use = n->fast_out(i);
4573 if(use->is_Mem() && use->in(MemNode::Address) == n) {
4574 // Load/store to instance's field
4575 memnode_worklist.append_if_missing(use);
4576 } else if (use->is_MemBar()) {
4577 if (use->in(TypeFunc::Memory) == n) { // Ignore precedent edge
4578 memnode_worklist.append_if_missing(use);
4579 }
4580 } else if (use->is_AddP() && use->outcnt() > 0) { // No dead nodes
4581 Node* addp2 = find_second_addp(use, n);
4582 if (addp2 != nullptr) {
4583 alloc_worklist.append_if_missing(addp2);
4584 }
4585 alloc_worklist.append_if_missing(use);
4586 } else if (use->is_Phi() ||
4587 use->is_CheckCastPP() ||
4588 use->is_EncodeNarrowPtr() ||
4589 use->is_DecodeNarrowPtr() ||
4590 (use->is_ConstraintCast() && use->Opcode() == Op_CastPP)) {
4591 alloc_worklist.append_if_missing(use);
4592 #ifdef ASSERT
4593 } else if (use->is_Mem()) {
4594 assert(use->in(MemNode::Address) != n, "EA: missing allocation reference path");
4595 } else if (use->is_MergeMem()) {
4596 assert(mergemem_worklist.contains(use->as_MergeMem()), "EA: missing MergeMem node in the worklist");
4597 } else if (use->is_SafePoint()) {
4598 // Look for MergeMem nodes for calls which reference unique allocation
4599 // (through CheckCastPP nodes) even for debug info.
4600 Node* m = use->in(TypeFunc::Memory);
4601 if (m->is_MergeMem()) {
4602 assert(mergemem_worklist.contains(m->as_MergeMem()), "EA: missing MergeMem node in the worklist");
4603 }
4604 } else if (use->Opcode() == Op_EncodeISOArray) {
4605 if (use->in(MemNode::Memory) == n || use->in(3) == n) {
4606 // EncodeISOArray overwrites destination array
4607 memnode_worklist.append_if_missing(use);
4608 }
4609 } else {
4610 uint op = use->Opcode();
4611 if ((op == Op_StrCompressedCopy || op == Op_StrInflatedCopy) &&
4612 (use->in(MemNode::Memory) == n)) {
4613 // They overwrite memory edge corresponding to destination array,
4614 memnode_worklist.append_if_missing(use);
4615 } else if (!(op == Op_CmpP || op == Op_Conv2B ||
4616 op == Op_CastP2X ||
4617 op == Op_FastLock || op == Op_AryEq ||
4618 op == Op_StrComp || op == Op_CountPositives ||
4619 op == Op_StrCompressedCopy || op == Op_StrInflatedCopy ||
4620 op == Op_StrEquals || op == Op_VectorizedHashCode ||
4621 op == Op_StrIndexOf || op == Op_StrIndexOfChar ||
4622 op == Op_SubTypeCheck ||
4623 op == Op_ReinterpretS2HF ||
4624 BarrierSet::barrier_set()->barrier_set_c2()->is_gc_barrier_node(use))) {
4625 n->dump();
4626 use->dump();
4627 assert(false, "EA: missing allocation reference path");
4628 }
4629 #endif
4630 }
4631 }
4632
4633 }
4634
4635 #ifdef ASSERT
4636 if (VerifyReduceAllocationMerges) {
4637 for (uint i = 0; i < reducible_merges.size(); i++) {
4638 Node* phi = reducible_merges.at(i);
4639
4640 if (!reduced_merges.member(phi)) {
4641 phi->dump(2);
4642 phi->dump(-2);
4706 // we don't need to do anything, but the users must be pushed
4707 n = n->as_MemBar()->proj_out_or_null(TypeFunc::Memory);
4708 if (n == nullptr) {
4709 continue;
4710 }
4711 } else if (n->is_CallLeaf()) {
4712 // Runtime calls with narrow memory input (no MergeMem node)
4713 // get the memory projection
4714 n = n->as_Call()->proj_out_or_null(TypeFunc::Memory);
4715 if (n == nullptr) {
4716 continue;
4717 }
4718 } else if (n->Opcode() == Op_StrInflatedCopy) {
4719 // Check direct uses of StrInflatedCopy.
4720 // It is memory type Node - no special SCMemProj node.
4721 } else if (n->Opcode() == Op_StrCompressedCopy ||
4722 n->Opcode() == Op_EncodeISOArray) {
4723 // get the memory projection
4724 n = n->find_out_with(Op_SCMemProj);
4725 assert(n != nullptr && n->Opcode() == Op_SCMemProj, "memory projection required");
4726 } else {
4727 #ifdef ASSERT
4728 if (!n->is_Mem()) {
4729 n->dump();
4730 }
4731 assert(n->is_Mem(), "memory node required.");
4732 #endif
4733 Node *addr = n->in(MemNode::Address);
4734 const Type *addr_t = igvn->type(addr);
4735 if (addr_t == Type::TOP) {
4736 continue;
4737 }
4738 assert (addr_t->isa_ptr() != nullptr, "pointer type required.");
4739 int alias_idx = _compile->get_alias_index(addr_t->is_ptr());
4740 assert ((uint)alias_idx < new_index_end, "wrong alias index");
4741 Node *mem = find_inst_mem(n->in(MemNode::Memory), alias_idx, orig_phis);
4742 if (_compile->failing()) {
4743 return;
4744 }
4745 if (mem != n->in(MemNode::Memory)) {
4750 if (n->is_Load()) {
4751 continue; // don't push users
4752 } else if (n->is_LoadStore()) {
4753 // get the memory projection
4754 n = n->find_out_with(Op_SCMemProj);
4755 assert(n != nullptr && n->Opcode() == Op_SCMemProj, "memory projection required");
4756 }
4757 }
4758 // push user on appropriate worklist
4759 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
4760 Node *use = n->fast_out(i);
4761 if (use->is_Phi() || use->is_ClearArray()) {
4762 memnode_worklist.append_if_missing(use);
4763 } else if (use->is_Mem() && use->in(MemNode::Memory) == n) {
4764 memnode_worklist.append_if_missing(use);
4765 } else if (use->is_MemBar() || use->is_CallLeaf()) {
4766 if (use->in(TypeFunc::Memory) == n) { // Ignore precedent edge
4767 memnode_worklist.append_if_missing(use);
4768 }
4769 #ifdef ASSERT
4770 } else if(use->is_Mem()) {
4771 assert(use->in(MemNode::Memory) != n, "EA: missing memory path");
4772 } else if (use->is_MergeMem()) {
4773 assert(mergemem_worklist.contains(use->as_MergeMem()), "EA: missing MergeMem node in the worklist");
4774 } else if (use->Opcode() == Op_EncodeISOArray) {
4775 if (use->in(MemNode::Memory) == n || use->in(3) == n) {
4776 // EncodeISOArray overwrites destination array
4777 memnode_worklist.append_if_missing(use);
4778 }
4779 } else {
4780 uint op = use->Opcode();
4781 if ((use->in(MemNode::Memory) == n) &&
4782 (op == Op_StrCompressedCopy || op == Op_StrInflatedCopy)) {
4783 // They overwrite memory edge corresponding to destination array,
4784 memnode_worklist.append_if_missing(use);
4785 } else if (!(BarrierSet::barrier_set()->barrier_set_c2()->is_gc_barrier_node(use) ||
4786 op == Op_AryEq || op == Op_StrComp || op == Op_CountPositives ||
4787 op == Op_StrCompressedCopy || op == Op_StrInflatedCopy || op == Op_VectorizedHashCode ||
4788 op == Op_StrEquals || op == Op_StrIndexOf || op == Op_StrIndexOfChar)) {
4789 n->dump();
4790 use->dump();
4791 assert(false, "EA: missing memory path");
4792 }
4793 #endif
4794 }
4795 }
4796 }
4797
4798 // Phase 3: Process MergeMem nodes from mergemem_worklist.
4799 // Walk each memory slice moving the first node encountered of each
4800 // instance type to the input corresponding to its alias index.
4801 uint length = mergemem_worklist.length();
4802 for( uint next = 0; next < length; ++next ) {
4803 MergeMemNode* nmm = mergemem_worklist.at(next);
4804 assert(!visited.test_set(nmm->_idx), "should not be visited before");
4805 // Note: we don't want to use MergeMemStream here because we only want to
4806 // scan inputs which exist at the start, not ones we add during processing.
4807 // Note 2: MergeMem may already contains instance memory slices added
4808 // during find_inst_mem() call when memory nodes were processed above.
4869 if (_compile->live_nodes() >= _compile->max_node_limit() * 0.75) {
4870 if (_compile->do_reduce_allocation_merges()) {
4871 _compile->record_failure(C2Compiler::retry_no_reduce_allocation_merges());
4872 } else if (_invocation > 0) {
4873 _compile->record_failure(C2Compiler::retry_no_iterative_escape_analysis());
4874 } else {
4875 _compile->record_failure(C2Compiler::retry_no_escape_analysis());
4876 }
4877 return;
4878 }
4879
4880 igvn->hash_insert(nmm);
4881 record_for_optimizer(nmm);
4882 }
4883
4884 // Phase 4: Update the inputs of non-instance memory Phis and
4885 // the Memory input of memnodes
4886 // First update the inputs of any non-instance Phi's from
4887 // which we split out an instance Phi. Note we don't have
4888 // to recursively process Phi's encountered on the input memory
4889 // chains as is done in split_memory_phi() since they will
4890 // also be processed here.
4891 for (int j = 0; j < orig_phis.length(); j++) {
4892 PhiNode *phi = orig_phis.at(j);
4893 int alias_idx = _compile->get_alias_index(phi->adr_type());
4894 igvn->hash_delete(phi);
4895 for (uint i = 1; i < phi->req(); i++) {
4896 Node *mem = phi->in(i);
4897 Node *new_mem = find_inst_mem(mem, alias_idx, orig_phis);
4898 if (_compile->failing()) {
4899 return;
4900 }
4901 if (mem != new_mem) {
4902 phi->set_req(i, new_mem);
4903 }
4904 }
4905 igvn->hash_insert(phi);
4906 record_for_optimizer(phi);
4907 }
4908
4909 // Update the memory inputs of MemNodes with the value we computed
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "ci/bcEscapeAnalyzer.hpp"
26 #include "compiler/compileLog.hpp"
27 #include "gc/shared/barrierSet.hpp"
28 #include "gc/shared/c2/barrierSetC2.hpp"
29 #include "libadt/vectset.hpp"
30 #include "memory/allocation.hpp"
31 #include "memory/metaspace.hpp"
32 #include "memory/resourceArea.hpp"
33 #include "opto/c2compiler.hpp"
34 #include "opto/arraycopynode.hpp"
35 #include "opto/callnode.hpp"
36 #include "opto/cfgnode.hpp"
37 #include "opto/compile.hpp"
38 #include "opto/escape.hpp"
39 #include "opto/inlinetypenode.hpp"
40 #include "opto/macro.hpp"
41 #include "opto/locknode.hpp"
42 #include "opto/phaseX.hpp"
43 #include "opto/movenode.hpp"
44 #include "opto/narrowptrnode.hpp"
45 #include "opto/castnode.hpp"
46 #include "opto/rootnode.hpp"
47 #include "utilities/macros.hpp"
48
49 ConnectionGraph::ConnectionGraph(Compile * C, PhaseIterGVN *igvn, int invocation) :
50 // If ReduceAllocationMerges is enabled we might call split_through_phi during
51 // split_unique_types and that will create additional nodes that need to be
52 // pushed to the ConnectionGraph. The code below bumps the initial capacity of
53 // _nodes by 10% to account for these additional nodes. If capacity is exceeded
54 // the array will be reallocated.
55 _nodes(C->comp_arena(), C->do_reduce_allocation_merges() ? C->unique()*1.10 : C->unique(), C->unique(), nullptr),
56 _in_worklist(C->comp_arena()),
57 _next_pidx(0),
58 _collecting(true),
59 _verify(false),
148 GrowableArray<SafePointNode*> sfn_worklist;
149 GrowableArray<MergeMemNode*> mergemem_worklist;
150 DEBUG_ONLY( GrowableArray<Node*> addp_worklist; )
151
152 { Compile::TracePhase tp(Phase::_t_connectionGraph);
153
154 // 1. Populate Connection Graph (CG) with PointsTo nodes.
155 ideal_nodes.map(C->live_nodes(), nullptr); // preallocate space
156 // Initialize worklist
157 if (C->root() != nullptr) {
158 ideal_nodes.push(C->root());
159 }
160 // Processed ideal nodes are unique on ideal_nodes list
161 // but several ideal nodes are mapped to the phantom_obj.
162 // To avoid duplicated entries on the following worklists
163 // add the phantom_obj only once to them.
164 ptnodes_worklist.append(phantom_obj);
165 java_objects_worklist.append(phantom_obj);
166 for( uint next = 0; next < ideal_nodes.size(); ++next ) {
167 Node* n = ideal_nodes.at(next);
168 if ((n->Opcode() == Op_LoadX || n->Opcode() == Op_StoreX) &&
169 !n->in(MemNode::Address)->is_AddP() &&
170 _igvn->type(n->in(MemNode::Address))->isa_oopptr()) {
171 // Load/Store at mark work address is at offset 0 so has no AddP which confuses EA
172 Node* addp = new AddPNode(n->in(MemNode::Address), n->in(MemNode::Address), _igvn->MakeConX(0));
173 _igvn->register_new_node_with_optimizer(addp);
174 _igvn->replace_input_of(n, MemNode::Address, addp);
175 ideal_nodes.push(addp);
176 _nodes.at_put_grow(addp->_idx, nullptr, nullptr);
177 }
178 // Create PointsTo nodes and add them to Connection Graph. Called
179 // only once per ideal node since ideal_nodes is Unique_Node list.
180 add_node_to_connection_graph(n, &delayed_worklist);
181 PointsToNode* ptn = ptnode_adr(n->_idx);
182 if (ptn != nullptr && ptn != phantom_obj) {
183 ptnodes_worklist.append(ptn);
184 if (ptn->is_JavaObject()) {
185 java_objects_worklist.append(ptn->as_JavaObject());
186 if ((n->is_Allocate() || n->is_CallStaticJava()) &&
187 (ptn->escape_state() < PointsToNode::GlobalEscape)) {
188 // Only allocations and java static calls results are interesting.
189 non_escaped_allocs_worklist.append(ptn->as_JavaObject());
190 }
191 } else if (ptn->is_Field() && ptn->as_Field()->is_oop()) {
192 oop_fields_worklist.append(ptn->as_Field());
193 }
194 }
195 // Collect some interesting nodes for further use.
196 switch (n->Opcode()) {
197 case Op_MergeMem:
1247
1248 // The next two inputs are:
1249 // (1) A copy of the original pointer to NSR objects.
1250 // (2) A selector, used to decide if we need to rematerialize an object
1251 // or use the pointer to a NSR object.
1252 // See more details of these fields in the declaration of SafePointScalarMergeNode
1253 sfpt->add_req(nsr_merge_pointer);
1254 sfpt->add_req(selector);
1255
1256 for (uint i = 1; i < ophi->req(); i++) {
1257 Node* base = ophi->in(i);
1258 JavaObjectNode* ptn = unique_java_object(base);
1259
1260 // If the base is not scalar replaceable we don't need to register information about
1261 // it at this time.
1262 if (ptn == nullptr || !ptn->scalar_replaceable()) {
1263 continue;
1264 }
1265
1266 AllocateNode* alloc = ptn->ideal_node()->as_Allocate();
1267 Unique_Node_List value_worklist;
1268 #ifdef ASSERT
1269 const Type* res_type = alloc->result_cast()->bottom_type();
1270 if (res_type->is_inlinetypeptr() && !Compile::current()->has_circular_inline_type()) {
1271 PhiNode* phi = ophi->as_Phi();
1272 assert(!ophi->as_Phi()->can_push_inline_types_down(_igvn), "missed earlier scalarization opportunity");
1273 }
1274 #endif
1275 SafePointScalarObjectNode* sobj = mexp.create_scalarized_object_description(alloc, sfpt, &value_worklist);
1276 if (sobj == nullptr) {
1277 _compile->record_failure(C2Compiler::retry_no_reduce_allocation_merges());
1278 return false;
1279 }
1280
1281 // Now make a pass over the debug information replacing any references
1282 // to the allocated object with "sobj"
1283 Node* ccpp = alloc->result_cast();
1284 sfpt->replace_edges_in_range(ccpp, sobj, debug_start, jvms->debug_end(), _igvn);
1285
1286 // Register the scalarized object as a candidate for reallocation
1287 smerge->add_req(sobj);
1288
1289 // Scalarize inline types that were added to the safepoint.
1290 // Don't allow linking a constant oop (if available) for flat array elements
1291 // because Deoptimization::reassign_flat_array_elements needs field values.
1292 const bool allow_oop = !merge_t->is_flat();
1293 for (uint j = 0; j < value_worklist.size(); ++j) {
1294 InlineTypeNode* vt = value_worklist.at(j)->as_InlineType();
1295 vt->make_scalar_in_safepoints(_igvn, allow_oop);
1296 }
1297 }
1298
1299 // Replaces debug information references to "original_sfpt_parent" in "sfpt" with references to "smerge"
1300 sfpt->replace_edges_in_range(original_sfpt_parent, smerge, debug_start, jvms->debug_end(), _igvn);
1301
1302 // The call to 'replace_edges_in_range' above might have removed the
1303 // reference to ophi that we need at _merge_pointer_idx. The line below make
1304 // sure the reference is maintained.
1305 sfpt->set_req(smerge->merge_pointer_idx(jvms), nsr_merge_pointer);
1306 _igvn->_worklist.push(sfpt);
1307 }
1308
1309 return true;
1310 }
1311
1312 void ConnectionGraph::reduce_phi(PhiNode* ophi, GrowableArray<Node *> &alloc_worklist, GrowableArray<Node *> &memnode_worklist) {
1313 bool delay = _igvn->delay_transform();
1314 _igvn->set_delay_transform(true);
1315 _igvn->hash_delete(ophi);
1316
1475 return false;
1476 }
1477
1478 // Returns true if at least one of the arguments to the call is an object
1479 // that does not escape globally.
1480 bool ConnectionGraph::has_arg_escape(CallJavaNode* call) {
1481 if (call->method() != nullptr) {
1482 uint max_idx = TypeFunc::Parms + call->method()->arg_size();
1483 for (uint idx = TypeFunc::Parms; idx < max_idx; idx++) {
1484 Node* p = call->in(idx);
1485 if (not_global_escape(p)) {
1486 return true;
1487 }
1488 }
1489 } else {
1490 const char* name = call->as_CallStaticJava()->_name;
1491 assert(name != nullptr, "no name");
1492 // no arg escapes through uncommon traps
1493 if (strcmp(name, "uncommon_trap") != 0) {
1494 // process_call_arguments() assumes that all arguments escape globally
1495 const TypeTuple* d = call->tf()->domain_sig();
1496 for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
1497 const Type* at = d->field_at(i);
1498 if (at->isa_oopptr() != nullptr) {
1499 return true;
1500 }
1501 }
1502 }
1503 }
1504 return false;
1505 }
1506
1507
1508
1509 // Utility function for nodes that load an object
1510 void ConnectionGraph::add_objload_to_connection_graph(Node *n, Unique_Node_List *delayed_worklist) {
1511 // Using isa_ptr() instead of isa_oopptr() for LoadP and Phi because
1512 // ThreadLocal has RawPtr type.
1513 const Type* t = _igvn->type(n);
1514 if (t->make_ptr() != nullptr) {
1515 Node* adr = n->in(MemNode::Address);
1549 // first IGVN optimization when escape information is still available.
1550 record_for_optimizer(n);
1551 } else if (n->is_Allocate()) {
1552 add_call_node(n->as_Call());
1553 record_for_optimizer(n);
1554 } else {
1555 if (n->is_CallStaticJava()) {
1556 const char* name = n->as_CallStaticJava()->_name;
1557 if (name != nullptr && strcmp(name, "uncommon_trap") == 0) {
1558 return; // Skip uncommon traps
1559 }
1560 }
1561 // Don't mark as processed since call's arguments have to be processed.
1562 delayed_worklist->push(n);
1563 // Check if a call returns an object.
1564 if ((n->as_Call()->returns_pointer() &&
1565 n->as_Call()->proj_out_or_null(TypeFunc::Parms) != nullptr) ||
1566 (n->is_CallStaticJava() &&
1567 n->as_CallStaticJava()->is_boxing_method())) {
1568 add_call_node(n->as_Call());
1569 } else if (n->as_Call()->tf()->returns_inline_type_as_fields()) {
1570 bool returns_oop = false;
1571 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax && !returns_oop; i++) {
1572 ProjNode* pn = n->fast_out(i)->as_Proj();
1573 if (pn->_con >= TypeFunc::Parms && pn->bottom_type()->isa_ptr()) {
1574 returns_oop = true;
1575 }
1576 }
1577 if (returns_oop) {
1578 add_call_node(n->as_Call());
1579 }
1580 }
1581 }
1582 return;
1583 }
1584 // Put this check here to process call arguments since some call nodes
1585 // point to phantom_obj.
1586 if (n_ptn == phantom_obj || n_ptn == null_obj) {
1587 return; // Skip predefined nodes.
1588 }
1589 switch (opcode) {
1590 case Op_AddP: {
1591 Node* base = get_addp_base(n);
1592 PointsToNode* ptn_base = ptnode_adr(base->_idx);
1593 // Field nodes are created for all field types. They are used in
1594 // adjust_scalar_replaceable_state() and split_unique_types().
1595 // Note, non-oop fields will have only base edges in Connection
1596 // Graph because such fields are not used for oop loads and stores.
1597 int offset = address_offset(n, igvn);
1598 add_field(n, PointsToNode::NoEscape, offset);
1599 if (ptn_base == nullptr) {
1600 delayed_worklist->push(n); // Process it later.
1601 } else {
1602 n_ptn = ptnode_adr(n_idx);
1603 add_base(n_ptn->as_Field(), ptn_base);
1604 }
1605 break;
1606 }
1607 case Op_CastX2P:
1608 case Op_CastI2N: {
1609 map_ideal_node(n, phantom_obj);
1610 break;
1611 }
1612 case Op_InlineType:
1613 case Op_CastPP:
1614 case Op_CheckCastPP:
1615 case Op_EncodeP:
1616 case Op_DecodeN:
1617 case Op_EncodePKlass:
1618 case Op_DecodeNKlass: {
1619 add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(1), delayed_worklist);
1620 break;
1621 }
1622 case Op_CMoveP: {
1623 add_local_var(n, PointsToNode::NoEscape);
1624 // Do not add edges during first iteration because some could be
1625 // not defined yet.
1626 delayed_worklist->push(n);
1627 break;
1628 }
1629 case Op_ConP:
1630 case Op_ConN:
1631 case Op_ConNKlass: {
1632 // assume all oop constants globally escape except for null
1664 case Op_PartialSubtypeCheck: {
1665 // Produces Null or notNull and is used in only in CmpP so
1666 // phantom_obj could be used.
1667 map_ideal_node(n, phantom_obj); // Result is unknown
1668 break;
1669 }
1670 case Op_Phi: {
1671 // Using isa_ptr() instead of isa_oopptr() for LoadP and Phi because
1672 // ThreadLocal has RawPtr type.
1673 const Type* t = n->as_Phi()->type();
1674 if (t->make_ptr() != nullptr) {
1675 add_local_var(n, PointsToNode::NoEscape);
1676 // Do not add edges during first iteration because some could be
1677 // not defined yet.
1678 delayed_worklist->push(n);
1679 }
1680 break;
1681 }
1682 case Op_Proj: {
1683 // we are only interested in the oop result projection from a call
1684 if (n->as_Proj()->_con >= TypeFunc::Parms && n->in(0)->is_Call() &&
1685 (n->in(0)->as_Call()->returns_pointer() || n->bottom_type()->isa_ptr())) {
1686 assert((n->as_Proj()->_con == TypeFunc::Parms && n->in(0)->as_Call()->returns_pointer()) ||
1687 n->in(0)->as_Call()->tf()->returns_inline_type_as_fields(), "what kind of oop return is it?");
1688 add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(0), delayed_worklist);
1689 }
1690 break;
1691 }
1692 case Op_Rethrow: // Exception object escapes
1693 case Op_Return: {
1694 if (n->req() > TypeFunc::Parms &&
1695 igvn->type(n->in(TypeFunc::Parms))->isa_oopptr()) {
1696 // Treat Return value as LocalVar with GlobalEscape escape state.
1697 add_local_var_and_edge(n, PointsToNode::GlobalEscape, n->in(TypeFunc::Parms), delayed_worklist);
1698 }
1699 break;
1700 }
1701 case Op_CompareAndExchangeP:
1702 case Op_CompareAndExchangeN:
1703 case Op_GetAndSetP:
1704 case Op_GetAndSetN: {
1705 add_objload_to_connection_graph(n, delayed_worklist);
1706 // fall-through
1707 }
1769 if (n->is_Call()) {
1770 process_call_arguments(n->as_Call());
1771 return;
1772 }
1773 assert(n->is_Store() || n->is_LoadStore() ||
1774 ((n_ptn != nullptr) && (n_ptn->ideal_node() != nullptr)),
1775 "node should be registered already");
1776 int opcode = n->Opcode();
1777 bool gc_handled = BarrierSet::barrier_set()->barrier_set_c2()->escape_add_final_edges(this, _igvn, n, opcode);
1778 if (gc_handled) {
1779 return; // Ignore node if already handled by GC.
1780 }
1781 switch (opcode) {
1782 case Op_AddP: {
1783 Node* base = get_addp_base(n);
1784 PointsToNode* ptn_base = ptnode_adr(base->_idx);
1785 assert(ptn_base != nullptr, "field's base should be registered");
1786 add_base(n_ptn->as_Field(), ptn_base);
1787 break;
1788 }
1789 case Op_InlineType:
1790 case Op_CastPP:
1791 case Op_CheckCastPP:
1792 case Op_EncodeP:
1793 case Op_DecodeN:
1794 case Op_EncodePKlass:
1795 case Op_DecodeNKlass: {
1796 add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(1), nullptr);
1797 break;
1798 }
1799 case Op_CMoveP: {
1800 for (uint i = CMoveNode::IfFalse; i < n->req(); i++) {
1801 Node* in = n->in(i);
1802 if (in == nullptr) {
1803 continue; // ignore null
1804 }
1805 Node* uncast_in = in->uncast();
1806 if (uncast_in->is_top() || uncast_in == n) {
1807 continue; // ignore top or inputs which go back this node
1808 }
1809 PointsToNode* ptn = ptnode_adr(in->_idx);
1824 // Using isa_ptr() instead of isa_oopptr() for LoadP and Phi because
1825 // ThreadLocal has RawPtr type.
1826 assert(n->as_Phi()->type()->make_ptr() != nullptr, "Unexpected node type");
1827 for (uint i = 1; i < n->req(); i++) {
1828 Node* in = n->in(i);
1829 if (in == nullptr) {
1830 continue; // ignore null
1831 }
1832 Node* uncast_in = in->uncast();
1833 if (uncast_in->is_top() || uncast_in == n) {
1834 continue; // ignore top or inputs which go back this node
1835 }
1836 PointsToNode* ptn = ptnode_adr(in->_idx);
1837 assert(ptn != nullptr, "node should be registered");
1838 add_edge(n_ptn, ptn);
1839 }
1840 break;
1841 }
1842 case Op_Proj: {
1843 // we are only interested in the oop result projection from a call
1844 assert((n->as_Proj()->_con == TypeFunc::Parms && n->in(0)->as_Call()->returns_pointer()) ||
1845 n->in(0)->as_Call()->tf()->returns_inline_type_as_fields(), "what kind of oop return is it?");
1846 add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(0), nullptr);
1847 break;
1848 }
1849 case Op_Rethrow: // Exception object escapes
1850 case Op_Return: {
1851 assert(n->req() > TypeFunc::Parms && _igvn->type(n->in(TypeFunc::Parms))->isa_oopptr(),
1852 "Unexpected node type");
1853 // Treat Return value as LocalVar with GlobalEscape escape state.
1854 add_local_var_and_edge(n, PointsToNode::GlobalEscape, n->in(TypeFunc::Parms), nullptr);
1855 break;
1856 }
1857 case Op_CompareAndExchangeP:
1858 case Op_CompareAndExchangeN:
1859 case Op_GetAndSetP:
1860 case Op_GetAndSetN:{
1861 assert(_igvn->type(n)->make_ptr() != nullptr, "Unexpected node type");
1862 add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(MemNode::Address), nullptr);
1863 // fall-through
1864 }
1865 case Op_CompareAndSwapP:
2001 PointsToNode* ptn = ptnode_adr(val->_idx);
2002 assert(ptn != nullptr, "node should be registered");
2003 set_escape_state(ptn, PointsToNode::GlobalEscape NOT_PRODUCT(COMMA "stored at raw address"));
2004 // Add edge to object for unsafe access with offset.
2005 PointsToNode* adr_ptn = ptnode_adr(adr->_idx);
2006 assert(adr_ptn != nullptr, "node should be registered");
2007 if (adr_ptn->is_Field()) {
2008 assert(adr_ptn->as_Field()->is_oop(), "should be oop field");
2009 add_edge(adr_ptn, ptn);
2010 }
2011 return true;
2012 }
2013 #ifdef ASSERT
2014 n->dump(1);
2015 assert(false, "not unsafe");
2016 #endif
2017 return false;
2018 }
2019
2020 void ConnectionGraph::add_call_node(CallNode* call) {
2021 assert(call->returns_pointer() || call->tf()->returns_inline_type_as_fields(), "only for call which returns pointer");
2022 uint call_idx = call->_idx;
2023 if (call->is_Allocate()) {
2024 Node* k = call->in(AllocateNode::KlassNode);
2025 const TypeKlassPtr* kt = k->bottom_type()->isa_klassptr();
2026 assert(kt != nullptr, "TypeKlassPtr required.");
2027 PointsToNode::EscapeState es = PointsToNode::NoEscape;
2028 bool scalar_replaceable = true;
2029 NOT_PRODUCT(const char* nsr_reason = "");
2030 if (call->is_AllocateArray()) {
2031 if (!kt->isa_aryklassptr()) { // StressReflectiveCode
2032 es = PointsToNode::GlobalEscape;
2033 } else {
2034 int length = call->in(AllocateNode::ALength)->find_int_con(-1);
2035 if (length < 0) {
2036 // Not scalar replaceable if the length is not constant.
2037 scalar_replaceable = false;
2038 NOT_PRODUCT(nsr_reason = "has a non-constant length");
2039 } else if (length > EliminateAllocationArraySizeLimit) {
2040 // Not scalar replaceable if the length is too big.
2041 scalar_replaceable = false;
2077 //
2078 // - all oop arguments are escaping globally;
2079 //
2080 // 2. CallStaticJavaNode (execute bytecode analysis if possible):
2081 //
2082 // - the same as CallDynamicJavaNode if can't do bytecode analysis;
2083 //
2084 // - mapped to GlobalEscape JavaObject node if unknown oop is returned;
2085 // - mapped to NoEscape JavaObject node if non-escaping object allocated
2086 // during call is returned;
2087 // - mapped to ArgEscape LocalVar node pointed to object arguments
2088 // which are returned and does not escape during call;
2089 //
2090 // - oop arguments escaping status is defined by bytecode analysis;
2091 //
2092 // For a static call, we know exactly what method is being called.
2093 // Use bytecode estimator to record whether the call's return value escapes.
2094 ciMethod* meth = call->as_CallJava()->method();
2095 if (meth == nullptr) {
2096 const char* name = call->as_CallStaticJava()->_name;
2097 assert(strncmp(name, "C2 Runtime multianewarray", 25) == 0 ||
2098 strncmp(name, "C2 Runtime load_unknown_inline", 30) == 0, "TODO: add failed case check");
2099 // Returns a newly allocated non-escaped object.
2100 add_java_object(call, PointsToNode::NoEscape);
2101 set_not_scalar_replaceable(ptnode_adr(call_idx) NOT_PRODUCT(COMMA "is result of multinewarray"));
2102 } else if (meth->is_boxing_method()) {
2103 // Returns boxing object
2104 PointsToNode::EscapeState es;
2105 vmIntrinsics::ID intr = meth->intrinsic_id();
2106 if (intr == vmIntrinsics::_floatValue || intr == vmIntrinsics::_doubleValue) {
2107 // It does not escape if object is always allocated.
2108 es = PointsToNode::NoEscape;
2109 } else {
2110 // It escapes globally if object could be loaded from cache.
2111 es = PointsToNode::GlobalEscape;
2112 }
2113 add_java_object(call, es);
2114 if (es == PointsToNode::GlobalEscape) {
2115 set_not_scalar_replaceable(ptnode_adr(call->_idx) NOT_PRODUCT(COMMA "object can be loaded from boxing cache"));
2116 }
2117 } else {
2118 BCEscapeAnalyzer* call_analyzer = meth->get_bcea();
2119 call_analyzer->copy_dependencies(_compile->dependencies());
2120 if (call_analyzer->is_return_allocated()) {
2121 // Returns a newly allocated non-escaped object, simply
2122 // update dependency information.
2123 // Mark it as NoEscape so that objects referenced by
2124 // it's fields will be marked as NoEscape at least.
2125 add_java_object(call, PointsToNode::NoEscape);
2126 set_not_scalar_replaceable(ptnode_adr(call_idx) NOT_PRODUCT(COMMA "is result of call"));
2127 } else {
2128 // Determine whether any arguments are returned.
2129 const TypeTuple* d = call->tf()->domain_cc();
2130 bool ret_arg = false;
2131 for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
2132 if (d->field_at(i)->isa_ptr() != nullptr &&
2133 call_analyzer->is_arg_returned(i - TypeFunc::Parms)) {
2134 ret_arg = true;
2135 break;
2136 }
2137 }
2138 if (ret_arg) {
2139 add_local_var(call, PointsToNode::ArgEscape);
2140 } else {
2141 // Returns unknown object.
2142 map_ideal_node(call, phantom_obj);
2143 }
2144 }
2145 }
2146 } else {
2147 // An other type of call, assume the worst case:
2148 // returned value is unknown and globally escapes.
2149 assert(call->Opcode() == Op_CallDynamicJava, "add failed case check");
2157 #ifdef ASSERT
2158 case Op_Allocate:
2159 case Op_AllocateArray:
2160 case Op_Lock:
2161 case Op_Unlock:
2162 assert(false, "should be done already");
2163 break;
2164 #endif
2165 case Op_ArrayCopy:
2166 case Op_CallLeafNoFP:
2167 // Most array copies are ArrayCopy nodes at this point but there
2168 // are still a few direct calls to the copy subroutines (See
2169 // PhaseStringOpts::copy_string())
2170 is_arraycopy = (call->Opcode() == Op_ArrayCopy) ||
2171 call->as_CallLeaf()->is_call_to_arraycopystub();
2172 // fall through
2173 case Op_CallLeafVector:
2174 case Op_CallLeaf: {
2175 // Stub calls, objects do not escape but they are not scale replaceable.
2176 // Adjust escape state for outgoing arguments.
2177 const TypeTuple * d = call->tf()->domain_sig();
2178 bool src_has_oops = false;
2179 for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
2180 const Type* at = d->field_at(i);
2181 Node *arg = call->in(i);
2182 if (arg == nullptr) {
2183 continue;
2184 }
2185 const Type *aat = _igvn->type(arg);
2186 if (arg->is_top() || !at->isa_ptr() || !aat->isa_ptr()) {
2187 continue;
2188 }
2189 if (arg->is_AddP()) {
2190 //
2191 // The inline_native_clone() case when the arraycopy stub is called
2192 // after the allocation before Initialize and CheckCastPP nodes.
2193 // Or normal arraycopy for object arrays case.
2194 //
2195 // Set AddP's base (Allocate) as not scalar replaceable since
2196 // pointer to the base (with offset) is passed as argument.
2197 //
2198 arg = get_addp_base(arg);
2199 }
2200 PointsToNode* arg_ptn = ptnode_adr(arg->_idx);
2201 assert(arg_ptn != nullptr, "should be registered");
2202 PointsToNode::EscapeState arg_esc = arg_ptn->escape_state();
2203 if (is_arraycopy || arg_esc < PointsToNode::ArgEscape) {
2204 assert(aat == Type::TOP || aat == TypePtr::NULL_PTR ||
2205 aat->isa_ptr() != nullptr, "expecting an Ptr");
2206 bool arg_has_oops = aat->isa_oopptr() &&
2207 (aat->isa_instptr() ||
2208 (aat->isa_aryptr() && (aat->isa_aryptr()->elem() == Type::BOTTOM || aat->isa_aryptr()->elem()->make_oopptr() != nullptr)) ||
2209 (aat->isa_aryptr() && aat->isa_aryptr()->elem() != nullptr &&
2210 aat->isa_aryptr()->is_flat() &&
2211 aat->isa_aryptr()->elem()->inline_klass()->contains_oops()));
2212 if (i == TypeFunc::Parms) {
2213 src_has_oops = arg_has_oops;
2214 }
2215 //
2216 // src or dst could be j.l.Object when other is basic type array:
2217 //
2218 // arraycopy(char[],0,Object*,0,size);
2219 // arraycopy(Object*,0,char[],0,size);
2220 //
2221 // Don't add edges in such cases.
2222 //
2223 bool arg_is_arraycopy_dest = src_has_oops && is_arraycopy &&
2224 arg_has_oops && (i > TypeFunc::Parms);
2225 #ifdef ASSERT
2226 if (!(is_arraycopy ||
2227 BarrierSet::barrier_set()->barrier_set_c2()->is_gc_barrier_node(call) ||
2228 (call->as_CallLeaf()->_name != nullptr &&
2229 (strcmp(call->as_CallLeaf()->_name, "updateBytesCRC32") == 0 ||
2230 strcmp(call->as_CallLeaf()->_name, "updateBytesCRC32C") == 0 ||
2231 strcmp(call->as_CallLeaf()->_name, "updateBytesAdler32") == 0 ||
2255 strcmp(call->as_CallLeaf()->_name, "dilithiumMontMulByConstant") == 0 ||
2256 strcmp(call->as_CallLeaf()->_name, "dilithiumDecomposePoly") == 0 ||
2257 strcmp(call->as_CallLeaf()->_name, "encodeBlock") == 0 ||
2258 strcmp(call->as_CallLeaf()->_name, "decodeBlock") == 0 ||
2259 strcmp(call->as_CallLeaf()->_name, "md5_implCompress") == 0 ||
2260 strcmp(call->as_CallLeaf()->_name, "md5_implCompressMB") == 0 ||
2261 strcmp(call->as_CallLeaf()->_name, "sha1_implCompress") == 0 ||
2262 strcmp(call->as_CallLeaf()->_name, "sha1_implCompressMB") == 0 ||
2263 strcmp(call->as_CallLeaf()->_name, "sha256_implCompress") == 0 ||
2264 strcmp(call->as_CallLeaf()->_name, "sha256_implCompressMB") == 0 ||
2265 strcmp(call->as_CallLeaf()->_name, "sha512_implCompress") == 0 ||
2266 strcmp(call->as_CallLeaf()->_name, "sha512_implCompressMB") == 0 ||
2267 strcmp(call->as_CallLeaf()->_name, "sha3_implCompress") == 0 ||
2268 strcmp(call->as_CallLeaf()->_name, "double_keccak") == 0 ||
2269 strcmp(call->as_CallLeaf()->_name, "sha3_implCompressMB") == 0 ||
2270 strcmp(call->as_CallLeaf()->_name, "multiplyToLen") == 0 ||
2271 strcmp(call->as_CallLeaf()->_name, "squareToLen") == 0 ||
2272 strcmp(call->as_CallLeaf()->_name, "mulAdd") == 0 ||
2273 strcmp(call->as_CallLeaf()->_name, "montgomery_multiply") == 0 ||
2274 strcmp(call->as_CallLeaf()->_name, "montgomery_square") == 0 ||
2275 strcmp(call->as_CallLeaf()->_name, "vectorizedMismatch") == 0 ||
2276 strcmp(call->as_CallLeaf()->_name, "load_unknown_inline") == 0 ||
2277 strcmp(call->as_CallLeaf()->_name, "store_unknown_inline") == 0 ||
2278 strcmp(call->as_CallLeaf()->_name, "bigIntegerRightShiftWorker") == 0 ||
2279 strcmp(call->as_CallLeaf()->_name, "bigIntegerLeftShiftWorker") == 0 ||
2280 strcmp(call->as_CallLeaf()->_name, "vectorizedMismatch") == 0 ||
2281 strcmp(call->as_CallLeaf()->_name, "stringIndexOf") == 0 ||
2282 strcmp(call->as_CallLeaf()->_name, "arraysort_stub") == 0 ||
2283 strcmp(call->as_CallLeaf()->_name, "array_partition_stub") == 0 ||
2284 strcmp(call->as_CallLeaf()->_name, "get_class_id_intrinsic") == 0 ||
2285 strcmp(call->as_CallLeaf()->_name, "unsafe_setmemory") == 0)
2286 ))) {
2287 call->dump();
2288 fatal("EA unexpected CallLeaf %s", call->as_CallLeaf()->_name);
2289 }
2290 #endif
2291 // Always process arraycopy's destination object since
2292 // we need to add all possible edges to references in
2293 // source object.
2294 if (arg_esc >= PointsToNode::ArgEscape &&
2295 !arg_is_arraycopy_dest) {
2296 continue;
2297 }
2320 }
2321 }
2322 }
2323 break;
2324 }
2325 case Op_CallStaticJava: {
2326 // For a static call, we know exactly what method is being called.
2327 // Use bytecode estimator to record the call's escape affects
2328 #ifdef ASSERT
2329 const char* name = call->as_CallStaticJava()->_name;
2330 assert((name == nullptr || strcmp(name, "uncommon_trap") != 0), "normal calls only");
2331 #endif
2332 ciMethod* meth = call->as_CallJava()->method();
2333 if ((meth != nullptr) && meth->is_boxing_method()) {
2334 break; // Boxing methods do not modify any oops.
2335 }
2336 BCEscapeAnalyzer* call_analyzer = (meth !=nullptr) ? meth->get_bcea() : nullptr;
2337 // fall-through if not a Java method or no analyzer information
2338 if (call_analyzer != nullptr) {
2339 PointsToNode* call_ptn = ptnode_adr(call->_idx);
2340 const TypeTuple* d = call->tf()->domain_cc();
2341 for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
2342 const Type* at = d->field_at(i);
2343 int k = i - TypeFunc::Parms;
2344 Node* arg = call->in(i);
2345 PointsToNode* arg_ptn = ptnode_adr(arg->_idx);
2346 if (at->isa_ptr() != nullptr &&
2347 call_analyzer->is_arg_returned(k)) {
2348 // The call returns arguments.
2349 if (call_ptn != nullptr) { // Is call's result used?
2350 assert(call_ptn->is_LocalVar(), "node should be registered");
2351 assert(arg_ptn != nullptr, "node should be registered");
2352 add_edge(call_ptn, arg_ptn);
2353 }
2354 }
2355 if (at->isa_oopptr() != nullptr &&
2356 arg_ptn->escape_state() < PointsToNode::GlobalEscape) {
2357 if (!call_analyzer->is_arg_stack(k)) {
2358 // The argument global escapes
2359 set_escape_state(arg_ptn, PointsToNode::GlobalEscape NOT_PRODUCT(COMMA trace_arg_escape_message(call)));
2360 } else {
2364 set_fields_escape_state(arg_ptn, PointsToNode::GlobalEscape NOT_PRODUCT(COMMA trace_arg_escape_message(call)));
2365 }
2366 }
2367 }
2368 }
2369 if (call_ptn != nullptr && call_ptn->is_LocalVar()) {
2370 // The call returns arguments.
2371 assert(call_ptn->edge_count() > 0, "sanity");
2372 if (!call_analyzer->is_return_local()) {
2373 // Returns also unknown object.
2374 add_edge(call_ptn, phantom_obj);
2375 }
2376 }
2377 break;
2378 }
2379 }
2380 default: {
2381 // Fall-through here if not a Java method or no analyzer information
2382 // or some other type of call, assume the worst case: all arguments
2383 // globally escape.
2384 const TypeTuple* d = call->tf()->domain_cc();
2385 for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
2386 const Type* at = d->field_at(i);
2387 if (at->isa_oopptr() != nullptr) {
2388 Node* arg = call->in(i);
2389 if (arg->is_AddP()) {
2390 arg = get_addp_base(arg);
2391 }
2392 assert(ptnode_adr(arg->_idx) != nullptr, "should be defined already");
2393 set_escape_state(ptnode_adr(arg->_idx), PointsToNode::GlobalEscape NOT_PRODUCT(COMMA trace_arg_escape_message(call)));
2394 }
2395 }
2396 }
2397 }
2398 }
2399
2400
2401 // Finish Graph construction.
2402 bool ConnectionGraph::complete_connection_graph(
2403 GrowableArray<PointsToNode*>& ptnodes_worklist,
2404 GrowableArray<JavaObjectNode*>& non_escaped_allocs_worklist,
2777 PointsToNode* base = i.get();
2778 if (base->is_JavaObject()) {
2779 // Skip Allocate's fields which will be processed later.
2780 if (base->ideal_node()->is_Allocate()) {
2781 return 0;
2782 }
2783 assert(base == null_obj, "only null ptr base expected here");
2784 }
2785 }
2786 if (add_edge(field, phantom_obj)) {
2787 // New edge was added
2788 new_edges++;
2789 add_field_uses_to_worklist(field);
2790 }
2791 return new_edges;
2792 }
2793
2794 // Find fields initializing values for allocations.
2795 int ConnectionGraph::find_init_values_phantom(JavaObjectNode* pta) {
2796 assert(pta->escape_state() == PointsToNode::NoEscape, "Not escaped Allocate nodes only");
2797 PointsToNode* init_val = phantom_obj;
2798 Node* alloc = pta->ideal_node();
2799
2800 // Do nothing for Allocate nodes since its fields values are
2801 // "known" unless they are initialized by arraycopy/clone.
2802 if (alloc->is_Allocate() && !pta->arraycopy_dst()) {
2803 if (alloc->as_Allocate()->in(AllocateNode::InitValue) != nullptr) {
2804 // Null-free inline type arrays are initialized with an init value instead of null
2805 init_val = ptnode_adr(alloc->as_Allocate()->in(AllocateNode::InitValue)->_idx);
2806 assert(init_val != nullptr, "init value should be registered");
2807 } else {
2808 return 0;
2809 }
2810 }
2811 // Non-escaped allocation returned from Java or runtime call has unknown values in fields.
2812 assert(pta->arraycopy_dst() || alloc->is_CallStaticJava() || init_val != phantom_obj, "sanity");
2813 #ifdef ASSERT
2814 if (alloc->is_CallStaticJava() && alloc->as_CallStaticJava()->method() == nullptr) {
2815 const char* name = alloc->as_CallStaticJava()->_name;
2816 assert(strncmp(name, "C2 Runtime multianewarray", 25) == 0 ||
2817 strncmp(name, "C2 Runtime load_unknown_inline", 30) == 0, "sanity");
2818 }
2819 #endif
2820 // Non-escaped allocation returned from Java or runtime call have unknown values in fields.
2821 int new_edges = 0;
2822 for (EdgeIterator i(pta); i.has_next(); i.next()) {
2823 PointsToNode* field = i.get();
2824 if (field->is_Field() && field->as_Field()->is_oop()) {
2825 if (add_edge(field, init_val)) {
2826 // New edge was added
2827 new_edges++;
2828 add_field_uses_to_worklist(field->as_Field());
2829 }
2830 }
2831 }
2832 return new_edges;
2833 }
2834
2835 // Find fields initializing values for allocations.
2836 int ConnectionGraph::find_init_values_null(JavaObjectNode* pta, PhaseValues* phase) {
2837 assert(pta->escape_state() == PointsToNode::NoEscape, "Not escaped Allocate nodes only");
2838 Node* alloc = pta->ideal_node();
2839 // Do nothing for Call nodes since its fields values are unknown.
2840 if (!alloc->is_Allocate() || alloc->as_Allocate()->in(AllocateNode::InitValue) != nullptr) {
2841 return 0;
2842 }
2843 InitializeNode* ini = alloc->as_Allocate()->initialization();
2844 bool visited_bottom_offset = false;
2845 GrowableArray<int> offsets_worklist;
2846 int new_edges = 0;
2847
2848 // Check if an oop field's initializing value is recorded and add
2849 // a corresponding null if field's value if it is not recorded.
2850 // Connection Graph does not record a default initialization by null
2851 // captured by Initialize node.
2852 //
2853 for (EdgeIterator i(pta); i.has_next(); i.next()) {
2854 PointsToNode* field = i.get(); // Field (AddP)
2855 if (!field->is_Field() || !field->as_Field()->is_oop()) {
2856 continue; // Not oop field
2857 }
2858 int offset = field->as_Field()->offset();
2859 if (offset == Type::OffsetBot) {
2860 if (!visited_bottom_offset) {
2906 } else {
2907 if (!val->is_LocalVar() || (val->edge_count() == 0)) {
2908 tty->print_cr("----------init store has invalid value -----");
2909 store->dump();
2910 val->dump();
2911 assert(val->is_LocalVar() && (val->edge_count() > 0), "should be processed already");
2912 }
2913 for (EdgeIterator j(val); j.has_next(); j.next()) {
2914 PointsToNode* obj = j.get();
2915 if (obj->is_JavaObject()) {
2916 if (!field->points_to(obj->as_JavaObject())) {
2917 missed_obj = obj;
2918 break;
2919 }
2920 }
2921 }
2922 }
2923 if (missed_obj != nullptr) {
2924 tty->print_cr("----------field---------------------------------");
2925 field->dump();
2926 tty->print_cr("----------missed reference to object------------");
2927 missed_obj->dump();
2928 tty->print_cr("----------object referenced by init store-------");
2929 store->dump();
2930 val->dump();
2931 assert(!field->points_to(missed_obj->as_JavaObject()), "missed JavaObject reference");
2932 }
2933 }
2934 #endif
2935 } else {
2936 // There could be initializing stores which follow allocation.
2937 // For example, a volatile field store is not collected
2938 // by Initialize node.
2939 //
2940 // Need to check for dependent loads to separate such stores from
2941 // stores which follow loads. For now, add initial value null so
2942 // that compare pointers optimization works correctly.
2943 }
2944 }
2945 if (value == nullptr) {
2946 // A field's initializing value was not recorded. Add null.
2947 if (add_edge(field, null_obj)) {
2948 // New edge was added
3264 assert(field->edge_count() > 0, "sanity");
3265 }
3266 }
3267 }
3268 }
3269 #endif
3270
3271 // Optimize ideal graph.
3272 void ConnectionGraph::optimize_ideal_graph(GrowableArray<Node*>& ptr_cmp_worklist,
3273 GrowableArray<MemBarStoreStoreNode*>& storestore_worklist) {
3274 Compile* C = _compile;
3275 PhaseIterGVN* igvn = _igvn;
3276 if (EliminateLocks) {
3277 // Mark locks before changing ideal graph.
3278 int cnt = C->macro_count();
3279 for (int i = 0; i < cnt; i++) {
3280 Node *n = C->macro_node(i);
3281 if (n->is_AbstractLock()) { // Lock and Unlock nodes
3282 AbstractLockNode* alock = n->as_AbstractLock();
3283 if (!alock->is_non_esc_obj()) {
3284 const Type* obj_type = igvn->type(alock->obj_node());
3285 if (can_eliminate_lock(alock) && !obj_type->is_inlinetypeptr()) {
3286 assert(!alock->is_eliminated() || alock->is_coarsened(), "sanity");
3287 // The lock could be marked eliminated by lock coarsening
3288 // code during first IGVN before EA. Replace coarsened flag
3289 // to eliminate all associated locks/unlocks.
3290 #ifdef ASSERT
3291 alock->log_lock_optimization(C, "eliminate_lock_set_non_esc3");
3292 #endif
3293 alock->set_non_esc_obj();
3294 }
3295 }
3296 }
3297 }
3298 }
3299
3300 if (OptimizePtrCompare) {
3301 for (int i = 0; i < ptr_cmp_worklist.length(); i++) {
3302 Node *n = ptr_cmp_worklist.at(i);
3303 assert(n->Opcode() == Op_CmpN || n->Opcode() == Op_CmpP, "must be");
3304 const TypeInt* tcmp = optimize_ptr_compare(n->in(1), n->in(2));
3305 if (tcmp->singleton()) {
3307 #ifndef PRODUCT
3308 if (PrintOptimizePtrCompare) {
3309 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"));
3310 if (Verbose) {
3311 n->dump(1);
3312 }
3313 }
3314 #endif
3315 igvn->replace_node(n, cmp);
3316 }
3317 }
3318 }
3319
3320 // For MemBarStoreStore nodes added in library_call.cpp, check
3321 // escape status of associated AllocateNode and optimize out
3322 // MemBarStoreStore node if the allocated object never escapes.
3323 for (int i = 0; i < storestore_worklist.length(); i++) {
3324 Node* storestore = storestore_worklist.at(i);
3325 Node* alloc = storestore->in(MemBarNode::Precedent)->in(0);
3326 if (alloc->is_Allocate() && not_global_escape(alloc)) {
3327 if (alloc->in(AllocateNode::InlineType) != nullptr) {
3328 // Non-escaping inline type buffer allocations don't require a membar
3329 storestore->as_MemBar()->remove(_igvn);
3330 } else {
3331 MemBarNode* mb = MemBarNode::make(C, Op_MemBarCPUOrder, Compile::AliasIdxBot);
3332 mb->init_req(TypeFunc::Memory, storestore->in(TypeFunc::Memory));
3333 mb->init_req(TypeFunc::Control, storestore->in(TypeFunc::Control));
3334 igvn->register_new_node_with_optimizer(mb);
3335 igvn->replace_node(storestore, mb);
3336 }
3337 }
3338 }
3339 }
3340
3341 // Optimize objects compare.
3342 const TypeInt* ConnectionGraph::optimize_ptr_compare(Node* left, Node* right) {
3343 assert(OptimizePtrCompare, "sanity");
3344 const TypeInt* EQ = TypeInt::CC_EQ; // [0] == ZERO
3345 const TypeInt* NE = TypeInt::CC_GT; // [1] == ONE
3346 const TypeInt* UNKNOWN = TypeInt::CC; // [-1, 0,1]
3347
3348 PointsToNode* ptn1 = ptnode_adr(left->_idx);
3349 PointsToNode* ptn2 = ptnode_adr(right->_idx);
3350 JavaObjectNode* jobj1 = unique_java_object(left);
3351 JavaObjectNode* jobj2 = unique_java_object(right);
3352
3353 // The use of this method during allocation merge reduction may cause 'left'
3354 // or 'right' be something (e.g., a Phi) that isn't in the connection graph or
3355 // that doesn't reference an unique java object.
3356 if (ptn1 == nullptr || ptn2 == nullptr ||
3478 assert(!src->is_Field() && !dst->is_Field(), "only for JavaObject and LocalVar");
3479 assert((src != null_obj) && (dst != null_obj), "not for ConP null");
3480 PointsToNode* ptadr = _nodes.at(n->_idx);
3481 if (ptadr != nullptr) {
3482 assert(ptadr->is_Arraycopy() && ptadr->ideal_node() == n, "sanity");
3483 return;
3484 }
3485 Compile* C = _compile;
3486 ptadr = new (C->comp_arena()) ArraycopyNode(this, n, es);
3487 map_ideal_node(n, ptadr);
3488 // Add edge from arraycopy node to source object.
3489 (void)add_edge(ptadr, src);
3490 src->set_arraycopy_src();
3491 // Add edge from destination object to arraycopy node.
3492 (void)add_edge(dst, ptadr);
3493 dst->set_arraycopy_dst();
3494 }
3495
3496 bool ConnectionGraph::is_oop_field(Node* n, int offset, bool* unsafe) {
3497 const Type* adr_type = n->as_AddP()->bottom_type();
3498 int field_offset = adr_type->isa_aryptr() ? adr_type->isa_aryptr()->field_offset().get() : Type::OffsetBot;
3499 BasicType bt = T_INT;
3500 if (offset == Type::OffsetBot && field_offset == Type::OffsetBot) {
3501 // Check only oop fields.
3502 if (!adr_type->isa_aryptr() ||
3503 adr_type->isa_aryptr()->elem() == Type::BOTTOM ||
3504 adr_type->isa_aryptr()->elem()->make_oopptr() != nullptr) {
3505 // OffsetBot is used to reference array's element. Ignore first AddP.
3506 if (find_second_addp(n, n->in(AddPNode::Base)) == nullptr) {
3507 bt = T_OBJECT;
3508 }
3509 }
3510 } else if (offset != oopDesc::klass_offset_in_bytes()) {
3511 if (adr_type->isa_instptr()) {
3512 ciField* field = _compile->alias_type(adr_type->is_ptr())->field();
3513 if (field != nullptr) {
3514 bt = field->layout_type();
3515 } else {
3516 // Check for unsafe oop field access
3517 if (n->has_out_with(Op_StoreP, Op_LoadP, Op_StoreN, Op_LoadN) ||
3518 n->has_out_with(Op_GetAndSetP, Op_GetAndSetN, Op_CompareAndExchangeP, Op_CompareAndExchangeN) ||
3519 n->has_out_with(Op_CompareAndSwapP, Op_CompareAndSwapN, Op_WeakCompareAndSwapP, Op_WeakCompareAndSwapN) ||
3520 BarrierSet::barrier_set()->barrier_set_c2()->escape_has_out_with_unsafe_object(n)) {
3521 bt = T_OBJECT;
3522 (*unsafe) = true;
3523 }
3524 }
3525 } else if (adr_type->isa_aryptr()) {
3526 if (offset == arrayOopDesc::length_offset_in_bytes()) {
3527 // Ignore array length load.
3528 } else if (find_second_addp(n, n->in(AddPNode::Base)) != nullptr) {
3529 // Ignore first AddP.
3530 } else {
3531 const Type* elemtype = adr_type->is_aryptr()->elem();
3532 if (adr_type->is_aryptr()->is_flat() && field_offset != Type::OffsetBot) {
3533 ciInlineKlass* vk = elemtype->inline_klass();
3534 field_offset += vk->payload_offset();
3535 ciField* field = vk->get_field_by_offset(field_offset, false);
3536 if (field != nullptr) {
3537 bt = field->layout_type();
3538 } else {
3539 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);
3540 bt = T_BOOLEAN;
3541 }
3542 } else {
3543 bt = elemtype->array_element_basic_type();
3544 }
3545 }
3546 } else if (adr_type->isa_rawptr() || adr_type->isa_klassptr()) {
3547 // Allocation initialization, ThreadLocal field access, unsafe access
3548 if (n->has_out_with(Op_StoreP, Op_LoadP, Op_StoreN, Op_LoadN) ||
3549 n->has_out_with(Op_GetAndSetP, Op_GetAndSetN, Op_CompareAndExchangeP, Op_CompareAndExchangeN) ||
3550 n->has_out_with(Op_CompareAndSwapP, Op_CompareAndSwapN, Op_WeakCompareAndSwapP, Op_WeakCompareAndSwapN) ||
3551 BarrierSet::barrier_set()->barrier_set_c2()->escape_has_out_with_unsafe_object(n)) {
3552 bt = T_OBJECT;
3553 }
3554 }
3555 }
3556 // Note: T_NARROWOOP is not classed as a real reference type
3557 return (is_reference_type(bt) || bt == T_NARROWOOP);
3558 }
3559
3560 // Returns unique pointed java object or null.
3561 JavaObjectNode* ConnectionGraph::unique_java_object(Node *n) const {
3562 // If the node was created after the escape computation we can't answer.
3563 uint idx = n->_idx;
3564 if (idx >= nodes_size()) {
3721 return true;
3722 }
3723 }
3724 }
3725 }
3726 }
3727 return false;
3728 }
3729
3730 int ConnectionGraph::address_offset(Node* adr, PhaseValues* phase) {
3731 const Type *adr_type = phase->type(adr);
3732 if (adr->is_AddP() && adr_type->isa_oopptr() == nullptr && is_captured_store_address(adr)) {
3733 // We are computing a raw address for a store captured by an Initialize
3734 // compute an appropriate address type. AddP cases #3 and #5 (see below).
3735 int offs = (int)phase->find_intptr_t_con(adr->in(AddPNode::Offset), Type::OffsetBot);
3736 assert(offs != Type::OffsetBot ||
3737 adr->in(AddPNode::Address)->in(0)->is_AllocateArray(),
3738 "offset must be a constant or it is initialization of array");
3739 return offs;
3740 }
3741 return adr_type->is_ptr()->flat_offset();
3742 }
3743
3744 Node* ConnectionGraph::get_addp_base(Node *addp) {
3745 assert(addp->is_AddP(), "must be AddP");
3746 //
3747 // AddP cases for Base and Address inputs:
3748 // case #1. Direct object's field reference:
3749 // Allocate
3750 // |
3751 // Proj #5 ( oop result )
3752 // |
3753 // CheckCastPP (cast to instance type)
3754 // | |
3755 // AddP ( base == address )
3756 //
3757 // case #2. Indirect object's field reference:
3758 // Phi
3759 // |
3760 // CastPP (cast to instance type)
3761 // | |
3875 }
3876 return nullptr;
3877 }
3878
3879 //
3880 // Adjust the type and inputs of an AddP which computes the
3881 // address of a field of an instance
3882 //
3883 bool ConnectionGraph::split_AddP(Node *addp, Node *base) {
3884 PhaseGVN* igvn = _igvn;
3885 const TypeOopPtr *base_t = igvn->type(base)->isa_oopptr();
3886 assert(base_t != nullptr && base_t->is_known_instance(), "expecting instance oopptr");
3887 const TypeOopPtr *t = igvn->type(addp)->isa_oopptr();
3888 if (t == nullptr) {
3889 // We are computing a raw address for a store captured by an Initialize
3890 // compute an appropriate address type (cases #3 and #5).
3891 assert(igvn->type(addp) == TypeRawPtr::NOTNULL, "must be raw pointer");
3892 assert(addp->in(AddPNode::Address)->is_Proj(), "base of raw address must be result projection from allocation");
3893 intptr_t offs = (int)igvn->find_intptr_t_con(addp->in(AddPNode::Offset), Type::OffsetBot);
3894 assert(offs != Type::OffsetBot, "offset must be a constant");
3895 if (base_t->isa_aryptr() != nullptr) {
3896 // In the case of a flat inline type array, each field has its
3897 // own slice so we need to extract the field being accessed from
3898 // the address computation
3899 t = base_t->isa_aryptr()->add_field_offset_and_offset(offs)->is_oopptr();
3900 } else {
3901 t = base_t->add_offset(offs)->is_oopptr();
3902 }
3903 }
3904 int inst_id = base_t->instance_id();
3905 assert(!t->is_known_instance() || t->instance_id() == inst_id,
3906 "old type must be non-instance or match new type");
3907
3908 // The type 't' could be subclass of 'base_t'.
3909 // As result t->offset() could be large then base_t's size and it will
3910 // cause the failure in add_offset() with narrow oops since TypeOopPtr()
3911 // constructor verifies correctness of the offset.
3912 //
3913 // It could happened on subclass's branch (from the type profiling
3914 // inlining) which was not eliminated during parsing since the exactness
3915 // of the allocation type was not propagated to the subclass type check.
3916 //
3917 // Or the type 't' could be not related to 'base_t' at all.
3918 // It could happen when CHA type is different from MDO type on a dead path
3919 // (for example, from instanceof check) which is not collapsed during parsing.
3920 //
3921 // Do nothing for such AddP node and don't process its users since
3922 // this code branch will go away.
3923 //
3924 if (!t->is_known_instance() &&
3925 !base_t->maybe_java_subtype_of(t)) {
3926 return false; // bail out
3927 }
3928 const TypePtr* tinst = base_t->add_offset(t->offset());
3929 if (tinst->isa_aryptr() && t->isa_aryptr()) {
3930 // In the case of a flat inline type array, each field has its
3931 // own slice so we need to keep track of the field being accessed.
3932 tinst = tinst->is_aryptr()->with_field_offset(t->is_aryptr()->field_offset().get());
3933 // Keep array properties (not flat/null-free)
3934 tinst = tinst->is_aryptr()->update_properties(t->is_aryptr());
3935 if (tinst == nullptr) {
3936 return false; // Skip dead path with inconsistent properties
3937 }
3938 }
3939
3940 // Do NOT remove the next line: ensure a new alias index is allocated
3941 // for the instance type. Note: C++ will not remove it since the call
3942 // has side effect.
3943 int alias_idx = _compile->get_alias_index(tinst);
3944 igvn->set_type(addp, tinst);
3945 // record the allocation in the node map
3946 set_map(addp, get_map(base->_idx));
3947 // Set addp's Base and Address to 'base'.
3948 Node *abase = addp->in(AddPNode::Base);
3949 Node *adr = addp->in(AddPNode::Address);
3950 if (adr->is_Proj() && adr->in(0)->is_Allocate() &&
3951 adr->in(0)->_idx == (uint)inst_id) {
3952 // Skip AddP cases #3 and #5.
3953 } else {
3954 assert(!abase->is_top(), "sanity"); // AddP case #3
3955 if (abase != base) {
3956 igvn->hash_delete(addp);
3957 addp->set_req(AddPNode::Base, base);
3958 if (abase == adr) {
3959 addp->set_req(AddPNode::Address, base);
4625 ptnode_adr(n->_idx)->dump();
4626 assert(jobj != nullptr && jobj != phantom_obj, "escaped allocation");
4627 #endif
4628 _compile->record_failure(_invocation > 0 ? C2Compiler::retry_no_iterative_escape_analysis() : C2Compiler::retry_no_escape_analysis());
4629 return;
4630 } else {
4631 Node *val = get_map(jobj->idx()); // CheckCastPP node
4632 TypeNode *tn = n->as_Type();
4633 const TypeOopPtr* tinst = igvn->type(val)->isa_oopptr();
4634 assert(tinst != nullptr && tinst->is_known_instance() &&
4635 tinst->instance_id() == jobj->idx() , "instance type expected.");
4636
4637 const Type *tn_type = igvn->type(tn);
4638 const TypeOopPtr *tn_t;
4639 if (tn_type->isa_narrowoop()) {
4640 tn_t = tn_type->make_ptr()->isa_oopptr();
4641 } else {
4642 tn_t = tn_type->isa_oopptr();
4643 }
4644 if (tn_t != nullptr && tinst->maybe_java_subtype_of(tn_t)) {
4645 if (tn_t->isa_aryptr()) {
4646 // Keep array properties (not flat/null-free)
4647 tinst = tinst->is_aryptr()->update_properties(tn_t->is_aryptr());
4648 if (tinst == nullptr) {
4649 continue; // Skip dead path with inconsistent properties
4650 }
4651 }
4652 if (tn_type->isa_narrowoop()) {
4653 tn_type = tinst->make_narrowoop();
4654 } else {
4655 tn_type = tinst;
4656 }
4657 igvn->hash_delete(tn);
4658 igvn->set_type(tn, tn_type);
4659 tn->set_type(tn_type);
4660 igvn->hash_insert(tn);
4661 record_for_optimizer(n);
4662 } else {
4663 assert(tn_type == TypePtr::NULL_PTR ||
4664 (tn_t != nullptr && !tinst->maybe_java_subtype_of(tn_t)),
4665 "unexpected type");
4666 continue; // Skip dead path with different type
4667 }
4668 }
4669 } else {
4670 DEBUG_ONLY(n->dump();)
4671 assert(false, "EA: unexpected node");
4672 continue;
4673 }
4674 // push allocation's users on appropriate worklist
4675 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
4676 Node *use = n->fast_out(i);
4677 if (use->is_Mem() && use->in(MemNode::Address) == n) {
4678 // Load/store to instance's field
4679 memnode_worklist.append_if_missing(use);
4680 } else if (use->is_MemBar()) {
4681 if (use->in(TypeFunc::Memory) == n) { // Ignore precedent edge
4682 memnode_worklist.append_if_missing(use);
4683 }
4684 } else if (use->is_AddP() && use->outcnt() > 0) { // No dead nodes
4685 Node* addp2 = find_second_addp(use, n);
4686 if (addp2 != nullptr) {
4687 alloc_worklist.append_if_missing(addp2);
4688 }
4689 alloc_worklist.append_if_missing(use);
4690 } else if (use->is_Phi() ||
4691 use->is_CheckCastPP() ||
4692 use->is_EncodeNarrowPtr() ||
4693 use->is_DecodeNarrowPtr() ||
4694 (use->is_ConstraintCast() && use->Opcode() == Op_CastPP)) {
4695 alloc_worklist.append_if_missing(use);
4696 #ifdef ASSERT
4697 } else if (use->is_Mem()) {
4698 assert(use->in(MemNode::Address) != n, "EA: missing allocation reference path");
4699 } else if (use->is_MergeMem()) {
4700 assert(mergemem_worklist.contains(use->as_MergeMem()), "EA: missing MergeMem node in the worklist");
4701 } else if (use->is_SafePoint()) {
4702 // Look for MergeMem nodes for calls which reference unique allocation
4703 // (through CheckCastPP nodes) even for debug info.
4704 Node* m = use->in(TypeFunc::Memory);
4705 if (m->is_MergeMem()) {
4706 assert(mergemem_worklist.contains(m->as_MergeMem()), "EA: missing MergeMem node in the worklist");
4707 }
4708 } else if (use->Opcode() == Op_EncodeISOArray) {
4709 if (use->in(MemNode::Memory) == n || use->in(3) == n) {
4710 // EncodeISOArray overwrites destination array
4711 memnode_worklist.append_if_missing(use);
4712 }
4713 } else if (use->Opcode() == Op_Return) {
4714 // Allocation is referenced by field of returned inline type
4715 assert(_compile->tf()->returns_inline_type_as_fields(), "EA: unexpected reference by ReturnNode");
4716 } else {
4717 uint op = use->Opcode();
4718 if ((op == Op_StrCompressedCopy || op == Op_StrInflatedCopy) &&
4719 (use->in(MemNode::Memory) == n)) {
4720 // They overwrite memory edge corresponding to destination array,
4721 memnode_worklist.append_if_missing(use);
4722 } else if (!(op == Op_CmpP || op == Op_Conv2B ||
4723 op == Op_CastP2X ||
4724 op == Op_FastLock || op == Op_AryEq ||
4725 op == Op_StrComp || op == Op_CountPositives ||
4726 op == Op_StrCompressedCopy || op == Op_StrInflatedCopy ||
4727 op == Op_StrEquals || op == Op_VectorizedHashCode ||
4728 op == Op_StrIndexOf || op == Op_StrIndexOfChar ||
4729 op == Op_SubTypeCheck || op == Op_InlineType || op == Op_FlatArrayCheck ||
4730 op == Op_ReinterpretS2HF ||
4731 BarrierSet::barrier_set()->barrier_set_c2()->is_gc_barrier_node(use))) {
4732 n->dump();
4733 use->dump();
4734 assert(false, "EA: missing allocation reference path");
4735 }
4736 #endif
4737 }
4738 }
4739
4740 }
4741
4742 #ifdef ASSERT
4743 if (VerifyReduceAllocationMerges) {
4744 for (uint i = 0; i < reducible_merges.size(); i++) {
4745 Node* phi = reducible_merges.at(i);
4746
4747 if (!reduced_merges.member(phi)) {
4748 phi->dump(2);
4749 phi->dump(-2);
4813 // we don't need to do anything, but the users must be pushed
4814 n = n->as_MemBar()->proj_out_or_null(TypeFunc::Memory);
4815 if (n == nullptr) {
4816 continue;
4817 }
4818 } else if (n->is_CallLeaf()) {
4819 // Runtime calls with narrow memory input (no MergeMem node)
4820 // get the memory projection
4821 n = n->as_Call()->proj_out_or_null(TypeFunc::Memory);
4822 if (n == nullptr) {
4823 continue;
4824 }
4825 } else if (n->Opcode() == Op_StrInflatedCopy) {
4826 // Check direct uses of StrInflatedCopy.
4827 // It is memory type Node - no special SCMemProj node.
4828 } else if (n->Opcode() == Op_StrCompressedCopy ||
4829 n->Opcode() == Op_EncodeISOArray) {
4830 // get the memory projection
4831 n = n->find_out_with(Op_SCMemProj);
4832 assert(n != nullptr && n->Opcode() == Op_SCMemProj, "memory projection required");
4833 } else if (n->is_CallLeaf() && n->as_CallLeaf()->_name != nullptr &&
4834 strcmp(n->as_CallLeaf()->_name, "store_unknown_inline") == 0) {
4835 n = n->as_CallLeaf()->proj_out(TypeFunc::Memory);
4836 } else {
4837 #ifdef ASSERT
4838 if (!n->is_Mem()) {
4839 n->dump();
4840 }
4841 assert(n->is_Mem(), "memory node required.");
4842 #endif
4843 Node *addr = n->in(MemNode::Address);
4844 const Type *addr_t = igvn->type(addr);
4845 if (addr_t == Type::TOP) {
4846 continue;
4847 }
4848 assert (addr_t->isa_ptr() != nullptr, "pointer type required.");
4849 int alias_idx = _compile->get_alias_index(addr_t->is_ptr());
4850 assert ((uint)alias_idx < new_index_end, "wrong alias index");
4851 Node *mem = find_inst_mem(n->in(MemNode::Memory), alias_idx, orig_phis);
4852 if (_compile->failing()) {
4853 return;
4854 }
4855 if (mem != n->in(MemNode::Memory)) {
4860 if (n->is_Load()) {
4861 continue; // don't push users
4862 } else if (n->is_LoadStore()) {
4863 // get the memory projection
4864 n = n->find_out_with(Op_SCMemProj);
4865 assert(n != nullptr && n->Opcode() == Op_SCMemProj, "memory projection required");
4866 }
4867 }
4868 // push user on appropriate worklist
4869 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
4870 Node *use = n->fast_out(i);
4871 if (use->is_Phi() || use->is_ClearArray()) {
4872 memnode_worklist.append_if_missing(use);
4873 } else if (use->is_Mem() && use->in(MemNode::Memory) == n) {
4874 memnode_worklist.append_if_missing(use);
4875 } else if (use->is_MemBar() || use->is_CallLeaf()) {
4876 if (use->in(TypeFunc::Memory) == n) { // Ignore precedent edge
4877 memnode_worklist.append_if_missing(use);
4878 }
4879 #ifdef ASSERT
4880 } else if (use->is_Mem()) {
4881 assert(use->in(MemNode::Memory) != n, "EA: missing memory path");
4882 } else if (use->is_MergeMem()) {
4883 assert(mergemem_worklist.contains(use->as_MergeMem()), "EA: missing MergeMem node in the worklist");
4884 } else if (use->Opcode() == Op_EncodeISOArray) {
4885 if (use->in(MemNode::Memory) == n || use->in(3) == n) {
4886 // EncodeISOArray overwrites destination array
4887 memnode_worklist.append_if_missing(use);
4888 }
4889 } else if (use->is_CallLeaf() && use->as_CallLeaf()->_name != nullptr &&
4890 strcmp(use->as_CallLeaf()->_name, "store_unknown_inline") == 0) {
4891 // store_unknown_inline overwrites destination array
4892 memnode_worklist.append_if_missing(use);
4893 } else {
4894 uint op = use->Opcode();
4895 if ((use->in(MemNode::Memory) == n) &&
4896 (op == Op_StrCompressedCopy || op == Op_StrInflatedCopy)) {
4897 // They overwrite memory edge corresponding to destination array,
4898 memnode_worklist.append_if_missing(use);
4899 } else if (!(BarrierSet::barrier_set()->barrier_set_c2()->is_gc_barrier_node(use) ||
4900 op == Op_AryEq || op == Op_StrComp || op == Op_CountPositives ||
4901 op == Op_StrCompressedCopy || op == Op_StrInflatedCopy || op == Op_VectorizedHashCode ||
4902 op == Op_StrEquals || op == Op_StrIndexOf || op == Op_StrIndexOfChar || op == Op_FlatArrayCheck)) {
4903 n->dump();
4904 use->dump();
4905 assert(false, "EA: missing memory path");
4906 }
4907 #endif
4908 }
4909 }
4910 }
4911
4912 // Phase 3: Process MergeMem nodes from mergemem_worklist.
4913 // Walk each memory slice moving the first node encountered of each
4914 // instance type to the input corresponding to its alias index.
4915 uint length = mergemem_worklist.length();
4916 for( uint next = 0; next < length; ++next ) {
4917 MergeMemNode* nmm = mergemem_worklist.at(next);
4918 assert(!visited.test_set(nmm->_idx), "should not be visited before");
4919 // Note: we don't want to use MergeMemStream here because we only want to
4920 // scan inputs which exist at the start, not ones we add during processing.
4921 // Note 2: MergeMem may already contains instance memory slices added
4922 // during find_inst_mem() call when memory nodes were processed above.
4983 if (_compile->live_nodes() >= _compile->max_node_limit() * 0.75) {
4984 if (_compile->do_reduce_allocation_merges()) {
4985 _compile->record_failure(C2Compiler::retry_no_reduce_allocation_merges());
4986 } else if (_invocation > 0) {
4987 _compile->record_failure(C2Compiler::retry_no_iterative_escape_analysis());
4988 } else {
4989 _compile->record_failure(C2Compiler::retry_no_escape_analysis());
4990 }
4991 return;
4992 }
4993
4994 igvn->hash_insert(nmm);
4995 record_for_optimizer(nmm);
4996 }
4997
4998 // Phase 4: Update the inputs of non-instance memory Phis and
4999 // the Memory input of memnodes
5000 // First update the inputs of any non-instance Phi's from
5001 // which we split out an instance Phi. Note we don't have
5002 // to recursively process Phi's encountered on the input memory
5003 // chains as is done in split_memory_phi() since they will
5004 // also be processed here.
5005 for (int j = 0; j < orig_phis.length(); j++) {
5006 PhiNode *phi = orig_phis.at(j);
5007 int alias_idx = _compile->get_alias_index(phi->adr_type());
5008 igvn->hash_delete(phi);
5009 for (uint i = 1; i < phi->req(); i++) {
5010 Node *mem = phi->in(i);
5011 Node *new_mem = find_inst_mem(mem, alias_idx, orig_phis);
5012 if (_compile->failing()) {
5013 return;
5014 }
5015 if (mem != new_mem) {
5016 phi->set_req(i, new_mem);
5017 }
5018 }
5019 igvn->hash_insert(phi);
5020 record_for_optimizer(phi);
5021 }
5022
5023 // Update the memory inputs of MemNodes with the value we computed
|