11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "ci/bcEscapeAnalyzer.hpp"
26 #include "compiler/compileLog.hpp"
27 #include "gc/shared/barrierSet.hpp"
28 #include "gc/shared/c2/barrierSetC2.hpp"
29 #include "libadt/vectset.hpp"
30 #include "memory/allocation.hpp"
31 #include "memory/resourceArea.hpp"
32 #include "opto/arraycopynode.hpp"
33 #include "opto/c2compiler.hpp"
34 #include "opto/callnode.hpp"
35 #include "opto/castnode.hpp"
36 #include "opto/cfgnode.hpp"
37 #include "opto/compile.hpp"
38 #include "opto/escape.hpp"
39 #include "opto/locknode.hpp"
40 #include "opto/macro.hpp"
41 #include "opto/movenode.hpp"
42 #include "opto/narrowptrnode.hpp"
43 #include "opto/phaseX.hpp"
44 #include "opto/rootnode.hpp"
45 #include "utilities/macros.hpp"
46
47 ConnectionGraph::ConnectionGraph(Compile * C, PhaseIterGVN *igvn, int invocation) :
48 // If ReduceAllocationMerges is enabled we might call split_through_phi during
49 // split_unique_types and that will create additional nodes that need to be
50 // pushed to the ConnectionGraph. The code below bumps the initial capacity of
51 // _nodes by 10% to account for these additional nodes. If capacity is exceeded
52 // the array will be reallocated.
53 _nodes(C->comp_arena(), C->do_reduce_allocation_merges() ? C->unique()*1.10 : C->unique(), C->unique(), nullptr),
54 _in_worklist(C->comp_arena()),
55 _next_pidx(0),
56 _collecting(true),
57 _verify(false),
58 _compile(C),
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:
396 split_unique_types(alloc_worklist, arraycopy_worklist, mergemem_worklist, reducible_merges);
397 if (C->failing()) {
398 NOT_PRODUCT(escape_state_statistics(java_objects_worklist);)
399 return false;
400 }
401 C->print_method(PHASE_AFTER_EA, 2);
402
403 #ifdef ASSERT
404 } else if (Verbose && (PrintEscapeAnalysis || PrintEliminateAllocations)) {
405 tty->print("=== No allocations eliminated for ");
406 C->method()->print_short_name();
407 if (!EliminateAllocations) {
408 tty->print(" since EliminateAllocations is off ===");
409 } else if(!has_scalar_replaceable_candidates) {
410 tty->print(" since there are no scalar replaceable candidates ===");
411 }
412 tty->cr();
413 #endif
414 }
415
416 // 6. Reduce allocation merges used as debug information. This is done after
417 // split_unique_types because the methods used to create SafePointScalarObject
418 // need to traverse the memory graph to find values for object fields. We also
419 // set to null the scalarized inputs of reducible Phis so that the Allocate
420 // that they point can be later scalar replaced.
421 bool delay = _igvn->delay_transform();
422 _igvn->set_delay_transform(true);
423 for (uint i = 0; i < reducible_merges.size(); i++) {
424 Node* n = reducible_merges.at(i);
425 if (n->outcnt() > 0) {
426 if (!reduce_phi_on_safepoints(n->as_Phi())) {
427 NOT_PRODUCT(escape_state_statistics(java_objects_worklist);)
428 C->record_failure(C2Compiler::retry_no_reduce_allocation_merges());
429 return false;
430 }
431
432 // Now we set the scalar replaceable inputs of ophi to null, which is
433 // the last piece that would prevent it from being scalar replaceable.
434 reset_scalar_replaceable_entries(n->as_Phi());
435 }
436 }
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
1444 return false;
1445 }
1446
1447 // Returns true if at least one of the arguments to the call is an object
1448 // that does not escape globally.
1449 bool ConnectionGraph::has_arg_escape(CallJavaNode* call) {
1450 if (call->method() != nullptr) {
1451 uint max_idx = TypeFunc::Parms + call->method()->arg_size();
1452 for (uint idx = TypeFunc::Parms; idx < max_idx; idx++) {
1453 Node* p = call->in(idx);
1454 if (not_global_escape(p)) {
1455 return true;
1456 }
1457 }
1458 } else {
1459 const char* name = call->as_CallStaticJava()->_name;
1460 assert(name != nullptr, "no name");
1461 // no arg escapes through uncommon traps
1462 if (strcmp(name, "uncommon_trap") != 0) {
1463 // process_call_arguments() assumes that all arguments escape globally
1464 const TypeTuple* d = call->tf()->domain();
1465 for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
1466 const Type* at = d->field_at(i);
1467 if (at->isa_oopptr() != nullptr) {
1468 return true;
1469 }
1470 }
1471 }
1472 }
1473 return false;
1474 }
1475
1476
1477
1478 // Utility function for nodes that load an object
1479 void ConnectionGraph::add_objload_to_connection_graph(Node *n, Unique_Node_List *delayed_worklist) {
1480 // Using isa_ptr() instead of isa_oopptr() for LoadP and Phi because
1481 // ThreadLocal has RawPtr type.
1482 const Type* t = _igvn->type(n);
1483 if (t->make_ptr() != nullptr) {
1484 Node* adr = n->in(MemNode::Address);
1518 // first IGVN optimization when escape information is still available.
1519 record_for_optimizer(n);
1520 } else if (n->is_Allocate()) {
1521 add_call_node(n->as_Call());
1522 record_for_optimizer(n);
1523 } else {
1524 if (n->is_CallStaticJava()) {
1525 const char* name = n->as_CallStaticJava()->_name;
1526 if (name != nullptr && strcmp(name, "uncommon_trap") == 0) {
1527 return; // Skip uncommon traps
1528 }
1529 }
1530 // Don't mark as processed since call's arguments have to be processed.
1531 delayed_worklist->push(n);
1532 // Check if a call returns an object.
1533 if ((n->as_Call()->returns_pointer() &&
1534 n->as_Call()->proj_out_or_null(TypeFunc::Parms) != nullptr) ||
1535 (n->is_CallStaticJava() &&
1536 n->as_CallStaticJava()->is_boxing_method())) {
1537 add_call_node(n->as_Call());
1538 }
1539 }
1540 return;
1541 }
1542 // Put this check here to process call arguments since some call nodes
1543 // point to phantom_obj.
1544 if (n_ptn == phantom_obj || n_ptn == null_obj) {
1545 return; // Skip predefined nodes.
1546 }
1547 switch (opcode) {
1548 case Op_AddP: {
1549 Node* base = get_addp_base(n);
1550 PointsToNode* ptn_base = ptnode_adr(base->_idx);
1551 // Field nodes are created for all field types. They are used in
1552 // adjust_scalar_replaceable_state() and split_unique_types().
1553 // Note, non-oop fields will have only base edges in Connection
1554 // Graph because such fields are not used for oop loads and stores.
1555 int offset = address_offset(n, igvn);
1556 add_field(n, PointsToNode::NoEscape, offset);
1557 if (ptn_base == nullptr) {
1558 delayed_worklist->push(n); // Process it later.
1559 } else {
1560 n_ptn = ptnode_adr(n_idx);
1561 add_base(n_ptn->as_Field(), ptn_base);
1562 }
1563 break;
1564 }
1565 case Op_CastX2P: {
1566 map_ideal_node(n, phantom_obj);
1567 break;
1568 }
1569 case Op_CastPP:
1570 case Op_CheckCastPP:
1571 case Op_EncodeP:
1572 case Op_DecodeN:
1573 case Op_EncodePKlass:
1574 case Op_DecodeNKlass: {
1575 add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(1), delayed_worklist);
1576 break;
1577 }
1578 case Op_CMoveP: {
1579 add_local_var(n, PointsToNode::NoEscape);
1580 // Do not add edges during first iteration because some could be
1581 // not defined yet.
1582 delayed_worklist->push(n);
1583 break;
1584 }
1585 case Op_ConP:
1586 case Op_ConN:
1587 case Op_ConNKlass: {
1588 // assume all oop constants globally escape except for null
1618 break;
1619 }
1620 case Op_PartialSubtypeCheck: {
1621 // Produces Null or notNull and is used in only in CmpP so
1622 // phantom_obj could be used.
1623 map_ideal_node(n, phantom_obj); // Result is unknown
1624 break;
1625 }
1626 case Op_Phi: {
1627 // Using isa_ptr() instead of isa_oopptr() for LoadP and Phi because
1628 // ThreadLocal has RawPtr type.
1629 const Type* t = n->as_Phi()->type();
1630 if (t->make_ptr() != nullptr) {
1631 add_local_var(n, PointsToNode::NoEscape);
1632 // Do not add edges during first iteration because some could be
1633 // not defined yet.
1634 delayed_worklist->push(n);
1635 }
1636 break;
1637 }
1638 case Op_Proj: {
1639 // we are only interested in the oop result projection from a call
1640 if (n->as_Proj()->_con == TypeFunc::Parms && n->in(0)->is_Call() &&
1641 n->in(0)->as_Call()->returns_pointer()) {
1642 add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(0), delayed_worklist);
1643 }
1644 break;
1645 }
1646 case Op_Rethrow: // Exception object escapes
1647 case Op_Return: {
1648 if (n->req() > TypeFunc::Parms &&
1649 igvn->type(n->in(TypeFunc::Parms))->isa_oopptr()) {
1650 // Treat Return value as LocalVar with GlobalEscape escape state.
1651 add_local_var_and_edge(n, PointsToNode::GlobalEscape, n->in(TypeFunc::Parms), delayed_worklist);
1652 }
1653 break;
1654 }
1655 case Op_CompareAndExchangeP:
1656 case Op_CompareAndExchangeN:
1657 case Op_GetAndSetP:
1658 case Op_GetAndSetN: {
1659 add_objload_to_connection_graph(n, delayed_worklist);
1660 // fall-through
1661 }
1707 break;
1708 }
1709 default:
1710 ; // Do nothing for nodes not related to EA.
1711 }
1712 return;
1713 }
1714
1715 // Add final simple edges to graph.
1716 void ConnectionGraph::add_final_edges(Node *n) {
1717 PointsToNode* n_ptn = ptnode_adr(n->_idx);
1718 #ifdef ASSERT
1719 if (_verify && n_ptn->is_JavaObject())
1720 return; // This method does not change graph for JavaObject.
1721 #endif
1722
1723 if (n->is_Call()) {
1724 process_call_arguments(n->as_Call());
1725 return;
1726 }
1727 assert(n->is_Store() || n->is_LoadStore() ||
1728 ((n_ptn != nullptr) && (n_ptn->ideal_node() != nullptr)),
1729 "node should be registered already");
1730 int opcode = n->Opcode();
1731 bool gc_handled = BarrierSet::barrier_set()->barrier_set_c2()->escape_add_final_edges(this, _igvn, n, opcode);
1732 if (gc_handled) {
1733 return; // Ignore node if already handled by GC.
1734 }
1735 switch (opcode) {
1736 case Op_AddP: {
1737 Node* base = get_addp_base(n);
1738 PointsToNode* ptn_base = ptnode_adr(base->_idx);
1739 assert(ptn_base != nullptr, "field's base should be registered");
1740 add_base(n_ptn->as_Field(), ptn_base);
1741 break;
1742 }
1743 case Op_CastPP:
1744 case Op_CheckCastPP:
1745 case Op_EncodeP:
1746 case Op_DecodeN:
1747 case Op_EncodePKlass:
1748 case Op_DecodeNKlass: {
1749 add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(1), nullptr);
1750 break;
1751 }
1752 case Op_CMoveP: {
1753 for (uint i = CMoveNode::IfFalse; i < n->req(); i++) {
1754 Node* in = n->in(i);
1755 if (in == nullptr) {
1756 continue; // ignore null
1757 }
1758 Node* uncast_in = in->uncast();
1759 if (uncast_in->is_top() || uncast_in == n) {
1760 continue; // ignore top or inputs which go back this node
1761 }
1762 PointsToNode* ptn = ptnode_adr(in->_idx);
1775 }
1776 case Op_Phi: {
1777 // Using isa_ptr() instead of isa_oopptr() for LoadP and Phi because
1778 // ThreadLocal has RawPtr type.
1779 assert(n->as_Phi()->type()->make_ptr() != nullptr, "Unexpected node type");
1780 for (uint i = 1; i < n->req(); i++) {
1781 Node* in = n->in(i);
1782 if (in == nullptr) {
1783 continue; // ignore null
1784 }
1785 Node* uncast_in = in->uncast();
1786 if (uncast_in->is_top() || uncast_in == n) {
1787 continue; // ignore top or inputs which go back this node
1788 }
1789 PointsToNode* ptn = ptnode_adr(in->_idx);
1790 assert(ptn != nullptr, "node should be registered");
1791 add_edge(n_ptn, ptn);
1792 }
1793 break;
1794 }
1795 case Op_Proj: {
1796 // we are only interested in the oop result projection from a call
1797 assert(n->as_Proj()->_con == TypeFunc::Parms && n->in(0)->is_Call() &&
1798 n->in(0)->as_Call()->returns_pointer(), "Unexpected node type");
1799 add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(0), nullptr);
1800 break;
1801 }
1802 case Op_Rethrow: // Exception object escapes
1803 case Op_Return: {
1804 assert(n->req() > TypeFunc::Parms && _igvn->type(n->in(TypeFunc::Parms))->isa_oopptr(),
1805 "Unexpected node type");
1806 // Treat Return value as LocalVar with GlobalEscape escape state.
1807 add_local_var_and_edge(n, PointsToNode::GlobalEscape, n->in(TypeFunc::Parms), nullptr);
1808 break;
1809 }
1810 case Op_CompareAndExchangeP:
1811 case Op_CompareAndExchangeN:
1812 case Op_GetAndSetP:
1813 case Op_GetAndSetN:{
1814 assert(_igvn->type(n)->make_ptr() != nullptr, "Unexpected node type");
1815 add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(MemNode::Address), nullptr);
1816 // fall-through
1817 }
1818 case Op_CompareAndSwapP:
1819 case Op_CompareAndSwapN:
1954 PointsToNode* ptn = ptnode_adr(val->_idx);
1955 assert(ptn != nullptr, "node should be registered");
1956 set_escape_state(ptn, PointsToNode::GlobalEscape NOT_PRODUCT(COMMA "stored at raw address"));
1957 // Add edge to object for unsafe access with offset.
1958 PointsToNode* adr_ptn = ptnode_adr(adr->_idx);
1959 assert(adr_ptn != nullptr, "node should be registered");
1960 if (adr_ptn->is_Field()) {
1961 assert(adr_ptn->as_Field()->is_oop(), "should be oop field");
1962 add_edge(adr_ptn, ptn);
1963 }
1964 return true;
1965 }
1966 #ifdef ASSERT
1967 n->dump(1);
1968 assert(false, "not unsafe");
1969 #endif
1970 return false;
1971 }
1972
1973 void ConnectionGraph::add_call_node(CallNode* call) {
1974 assert(call->returns_pointer(), "only for call which returns pointer");
1975 uint call_idx = call->_idx;
1976 if (call->is_Allocate()) {
1977 Node* k = call->in(AllocateNode::KlassNode);
1978 const TypeKlassPtr* kt = k->bottom_type()->isa_klassptr();
1979 assert(kt != nullptr, "TypeKlassPtr required.");
1980 PointsToNode::EscapeState es = PointsToNode::NoEscape;
1981 bool scalar_replaceable = true;
1982 NOT_PRODUCT(const char* nsr_reason = "");
1983 if (call->is_AllocateArray()) {
1984 if (!kt->isa_aryklassptr()) { // StressReflectiveCode
1985 es = PointsToNode::GlobalEscape;
1986 } else {
1987 int length = call->in(AllocateNode::ALength)->find_int_con(-1);
1988 if (length < 0) {
1989 // Not scalar replaceable if the length is not constant.
1990 scalar_replaceable = false;
1991 NOT_PRODUCT(nsr_reason = "has a non-constant length");
1992 } else if (length > EliminateAllocationArraySizeLimit) {
1993 // Not scalar replaceable if the length is too big.
1994 scalar_replaceable = false;
2030 //
2031 // - all oop arguments are escaping globally;
2032 //
2033 // 2. CallStaticJavaNode (execute bytecode analysis if possible):
2034 //
2035 // - the same as CallDynamicJavaNode if can't do bytecode analysis;
2036 //
2037 // - mapped to GlobalEscape JavaObject node if unknown oop is returned;
2038 // - mapped to NoEscape JavaObject node if non-escaping object allocated
2039 // during call is returned;
2040 // - mapped to ArgEscape LocalVar node pointed to object arguments
2041 // which are returned and does not escape during call;
2042 //
2043 // - oop arguments escaping status is defined by bytecode analysis;
2044 //
2045 // For a static call, we know exactly what method is being called.
2046 // Use bytecode estimator to record whether the call's return value escapes.
2047 ciMethod* meth = call->as_CallJava()->method();
2048 if (meth == nullptr) {
2049 const char* name = call->as_CallStaticJava()->_name;
2050 assert(strncmp(name, "C2 Runtime multianewarray", 25) == 0, "TODO: add failed case check");
2051 // Returns a newly allocated non-escaped object.
2052 add_java_object(call, PointsToNode::NoEscape);
2053 set_not_scalar_replaceable(ptnode_adr(call_idx) NOT_PRODUCT(COMMA "is result of multinewarray"));
2054 } else if (meth->is_boxing_method()) {
2055 // Returns boxing object
2056 PointsToNode::EscapeState es;
2057 vmIntrinsics::ID intr = meth->intrinsic_id();
2058 if (intr == vmIntrinsics::_floatValue || intr == vmIntrinsics::_doubleValue) {
2059 // It does not escape if object is always allocated.
2060 es = PointsToNode::NoEscape;
2061 } else {
2062 // It escapes globally if object could be loaded from cache.
2063 es = PointsToNode::GlobalEscape;
2064 }
2065 add_java_object(call, es);
2066 if (es == PointsToNode::GlobalEscape) {
2067 set_not_scalar_replaceable(ptnode_adr(call->_idx) NOT_PRODUCT(COMMA "object can be loaded from boxing cache"));
2068 }
2069 } else {
2070 BCEscapeAnalyzer* call_analyzer = meth->get_bcea();
2071 call_analyzer->copy_dependencies(_compile->dependencies());
2072 if (call_analyzer->is_return_allocated()) {
2073 // Returns a newly allocated non-escaped object, simply
2074 // update dependency information.
2075 // Mark it as NoEscape so that objects referenced by
2076 // it's fields will be marked as NoEscape at least.
2077 add_java_object(call, PointsToNode::NoEscape);
2078 set_not_scalar_replaceable(ptnode_adr(call_idx) NOT_PRODUCT(COMMA "is result of call"));
2079 } else {
2080 // Determine whether any arguments are returned.
2081 const TypeTuple* d = call->tf()->domain();
2082 bool ret_arg = false;
2083 for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
2084 if (d->field_at(i)->isa_ptr() != nullptr &&
2085 call_analyzer->is_arg_returned(i - TypeFunc::Parms)) {
2086 ret_arg = true;
2087 break;
2088 }
2089 }
2090 if (ret_arg) {
2091 add_local_var(call, PointsToNode::ArgEscape);
2092 } else {
2093 // Returns unknown object.
2094 map_ideal_node(call, phantom_obj);
2095 }
2096 }
2097 }
2098 } else {
2099 // An other type of call, assume the worst case:
2100 // returned value is unknown and globally escapes.
2101 assert(call->Opcode() == Op_CallDynamicJava, "add failed case check");
2109 #ifdef ASSERT
2110 case Op_Allocate:
2111 case Op_AllocateArray:
2112 case Op_Lock:
2113 case Op_Unlock:
2114 assert(false, "should be done already");
2115 break;
2116 #endif
2117 case Op_ArrayCopy:
2118 case Op_CallLeafNoFP:
2119 // Most array copies are ArrayCopy nodes at this point but there
2120 // are still a few direct calls to the copy subroutines (See
2121 // PhaseStringOpts::copy_string())
2122 is_arraycopy = (call->Opcode() == Op_ArrayCopy) ||
2123 call->as_CallLeaf()->is_call_to_arraycopystub();
2124 // fall through
2125 case Op_CallLeafVector:
2126 case Op_CallLeaf: {
2127 // Stub calls, objects do not escape but they are not scale replaceable.
2128 // Adjust escape state for outgoing arguments.
2129 const TypeTuple * d = call->tf()->domain();
2130 bool src_has_oops = false;
2131 for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
2132 const Type* at = d->field_at(i);
2133 Node *arg = call->in(i);
2134 if (arg == nullptr) {
2135 continue;
2136 }
2137 const Type *aat = _igvn->type(arg);
2138 if (arg->is_top() || !at->isa_ptr() || !aat->isa_ptr()) {
2139 continue;
2140 }
2141 if (arg->is_AddP()) {
2142 //
2143 // The inline_native_clone() case when the arraycopy stub is called
2144 // after the allocation before Initialize and CheckCastPP nodes.
2145 // Or normal arraycopy for object arrays case.
2146 //
2147 // Set AddP's base (Allocate) as not scalar replaceable since
2148 // pointer to the base (with offset) is passed as argument.
2149 //
2150 arg = get_addp_base(arg);
2151 }
2152 PointsToNode* arg_ptn = ptnode_adr(arg->_idx);
2153 assert(arg_ptn != nullptr, "should be registered");
2154 PointsToNode::EscapeState arg_esc = arg_ptn->escape_state();
2155 if (is_arraycopy || arg_esc < PointsToNode::ArgEscape) {
2156 assert(aat == Type::TOP || aat == TypePtr::NULL_PTR ||
2157 aat->isa_ptr() != nullptr, "expecting an Ptr");
2158 bool arg_has_oops = aat->isa_oopptr() &&
2159 (aat->isa_instptr() ||
2160 (aat->isa_aryptr() && (aat->isa_aryptr()->elem() == Type::BOTTOM || aat->isa_aryptr()->elem()->make_oopptr() != nullptr)));
2161 if (i == TypeFunc::Parms) {
2162 src_has_oops = arg_has_oops;
2163 }
2164 //
2165 // src or dst could be j.l.Object when other is basic type array:
2166 //
2167 // arraycopy(char[],0,Object*,0,size);
2168 // arraycopy(Object*,0,char[],0,size);
2169 //
2170 // Don't add edges in such cases.
2171 //
2172 bool arg_is_arraycopy_dest = src_has_oops && is_arraycopy &&
2173 arg_has_oops && (i > TypeFunc::Parms);
2174 #ifdef ASSERT
2175 if (!(is_arraycopy ||
2176 BarrierSet::barrier_set()->barrier_set_c2()->is_gc_barrier_node(call) ||
2177 (call->as_CallLeaf()->_name != nullptr &&
2178 (strcmp(call->as_CallLeaf()->_name, "updateBytesCRC32") == 0 ||
2179 strcmp(call->as_CallLeaf()->_name, "updateBytesCRC32C") == 0 ||
2180 strcmp(call->as_CallLeaf()->_name, "updateBytesAdler32") == 0 ||
2204 strcmp(call->as_CallLeaf()->_name, "dilithiumMontMulByConstant") == 0 ||
2205 strcmp(call->as_CallLeaf()->_name, "dilithiumDecomposePoly") == 0 ||
2206 strcmp(call->as_CallLeaf()->_name, "encodeBlock") == 0 ||
2207 strcmp(call->as_CallLeaf()->_name, "decodeBlock") == 0 ||
2208 strcmp(call->as_CallLeaf()->_name, "md5_implCompress") == 0 ||
2209 strcmp(call->as_CallLeaf()->_name, "md5_implCompressMB") == 0 ||
2210 strcmp(call->as_CallLeaf()->_name, "sha1_implCompress") == 0 ||
2211 strcmp(call->as_CallLeaf()->_name, "sha1_implCompressMB") == 0 ||
2212 strcmp(call->as_CallLeaf()->_name, "sha256_implCompress") == 0 ||
2213 strcmp(call->as_CallLeaf()->_name, "sha256_implCompressMB") == 0 ||
2214 strcmp(call->as_CallLeaf()->_name, "sha512_implCompress") == 0 ||
2215 strcmp(call->as_CallLeaf()->_name, "sha512_implCompressMB") == 0 ||
2216 strcmp(call->as_CallLeaf()->_name, "sha3_implCompress") == 0 ||
2217 strcmp(call->as_CallLeaf()->_name, "double_keccak") == 0 ||
2218 strcmp(call->as_CallLeaf()->_name, "sha3_implCompressMB") == 0 ||
2219 strcmp(call->as_CallLeaf()->_name, "multiplyToLen") == 0 ||
2220 strcmp(call->as_CallLeaf()->_name, "squareToLen") == 0 ||
2221 strcmp(call->as_CallLeaf()->_name, "mulAdd") == 0 ||
2222 strcmp(call->as_CallLeaf()->_name, "montgomery_multiply") == 0 ||
2223 strcmp(call->as_CallLeaf()->_name, "montgomery_square") == 0 ||
2224 strcmp(call->as_CallLeaf()->_name, "bigIntegerRightShiftWorker") == 0 ||
2225 strcmp(call->as_CallLeaf()->_name, "bigIntegerLeftShiftWorker") == 0 ||
2226 strcmp(call->as_CallLeaf()->_name, "vectorizedMismatch") == 0 ||
2227 strcmp(call->as_CallLeaf()->_name, "stringIndexOf") == 0 ||
2228 strcmp(call->as_CallLeaf()->_name, "arraysort_stub") == 0 ||
2229 strcmp(call->as_CallLeaf()->_name, "array_partition_stub") == 0 ||
2230 strcmp(call->as_CallLeaf()->_name, "get_class_id_intrinsic") == 0 ||
2231 strcmp(call->as_CallLeaf()->_name, "unsafe_setmemory") == 0)
2232 ))) {
2233 call->dump();
2234 fatal("EA unexpected CallLeaf %s", call->as_CallLeaf()->_name);
2235 }
2236 #endif
2237 // Always process arraycopy's destination object since
2238 // we need to add all possible edges to references in
2239 // source object.
2240 if (arg_esc >= PointsToNode::ArgEscape &&
2241 !arg_is_arraycopy_dest) {
2242 continue;
2243 }
2266 }
2267 }
2268 }
2269 break;
2270 }
2271 case Op_CallStaticJava: {
2272 // For a static call, we know exactly what method is being called.
2273 // Use bytecode estimator to record the call's escape affects
2274 #ifdef ASSERT
2275 const char* name = call->as_CallStaticJava()->_name;
2276 assert((name == nullptr || strcmp(name, "uncommon_trap") != 0), "normal calls only");
2277 #endif
2278 ciMethod* meth = call->as_CallJava()->method();
2279 if ((meth != nullptr) && meth->is_boxing_method()) {
2280 break; // Boxing methods do not modify any oops.
2281 }
2282 BCEscapeAnalyzer* call_analyzer = (meth !=nullptr) ? meth->get_bcea() : nullptr;
2283 // fall-through if not a Java method or no analyzer information
2284 if (call_analyzer != nullptr) {
2285 PointsToNode* call_ptn = ptnode_adr(call->_idx);
2286 const TypeTuple* d = call->tf()->domain();
2287 for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
2288 const Type* at = d->field_at(i);
2289 int k = i - TypeFunc::Parms;
2290 Node* arg = call->in(i);
2291 PointsToNode* arg_ptn = ptnode_adr(arg->_idx);
2292 if (at->isa_ptr() != nullptr &&
2293 call_analyzer->is_arg_returned(k)) {
2294 // The call returns arguments.
2295 if (call_ptn != nullptr) { // Is call's result used?
2296 assert(call_ptn->is_LocalVar(), "node should be registered");
2297 assert(arg_ptn != nullptr, "node should be registered");
2298 add_edge(call_ptn, arg_ptn);
2299 }
2300 }
2301 if (at->isa_oopptr() != nullptr &&
2302 arg_ptn->escape_state() < PointsToNode::GlobalEscape) {
2303 if (!call_analyzer->is_arg_stack(k)) {
2304 // The argument global escapes
2305 set_escape_state(arg_ptn, PointsToNode::GlobalEscape NOT_PRODUCT(COMMA trace_arg_escape_message(call)));
2306 } else {
2310 set_fields_escape_state(arg_ptn, PointsToNode::GlobalEscape NOT_PRODUCT(COMMA trace_arg_escape_message(call)));
2311 }
2312 }
2313 }
2314 }
2315 if (call_ptn != nullptr && call_ptn->is_LocalVar()) {
2316 // The call returns arguments.
2317 assert(call_ptn->edge_count() > 0, "sanity");
2318 if (!call_analyzer->is_return_local()) {
2319 // Returns also unknown object.
2320 add_edge(call_ptn, phantom_obj);
2321 }
2322 }
2323 break;
2324 }
2325 }
2326 default: {
2327 // Fall-through here if not a Java method or no analyzer information
2328 // or some other type of call, assume the worst case: all arguments
2329 // globally escape.
2330 const TypeTuple* d = call->tf()->domain();
2331 for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
2332 const Type* at = d->field_at(i);
2333 if (at->isa_oopptr() != nullptr) {
2334 Node* arg = call->in(i);
2335 if (arg->is_AddP()) {
2336 arg = get_addp_base(arg);
2337 }
2338 assert(ptnode_adr(arg->_idx) != nullptr, "should be defined already");
2339 set_escape_state(ptnode_adr(arg->_idx), PointsToNode::GlobalEscape NOT_PRODUCT(COMMA trace_arg_escape_message(call)));
2340 }
2341 }
2342 }
2343 }
2344 }
2345
2346
2347 // Finish Graph construction.
2348 bool ConnectionGraph::complete_connection_graph(
2349 GrowableArray<PointsToNode*>& ptnodes_worklist,
2350 GrowableArray<JavaObjectNode*>& non_escaped_allocs_worklist,
2723 PointsToNode* base = i.get();
2724 if (base->is_JavaObject()) {
2725 // Skip Allocate's fields which will be processed later.
2726 if (base->ideal_node()->is_Allocate()) {
2727 return 0;
2728 }
2729 assert(base == null_obj, "only null ptr base expected here");
2730 }
2731 }
2732 if (add_edge(field, phantom_obj)) {
2733 // New edge was added
2734 new_edges++;
2735 add_field_uses_to_worklist(field);
2736 }
2737 return new_edges;
2738 }
2739
2740 // Find fields initializing values for allocations.
2741 int ConnectionGraph::find_init_values_phantom(JavaObjectNode* pta) {
2742 assert(pta->escape_state() == PointsToNode::NoEscape, "Not escaped Allocate nodes only");
2743 Node* alloc = pta->ideal_node();
2744
2745 // Do nothing for Allocate nodes since its fields values are
2746 // "known" unless they are initialized by arraycopy/clone.
2747 if (alloc->is_Allocate() && !pta->arraycopy_dst()) {
2748 return 0;
2749 }
2750 assert(pta->arraycopy_dst() || alloc->as_CallStaticJava(), "sanity");
2751 #ifdef ASSERT
2752 if (!pta->arraycopy_dst() && alloc->as_CallStaticJava()->method() == nullptr) {
2753 const char* name = alloc->as_CallStaticJava()->_name;
2754 assert(strncmp(name, "C2 Runtime multianewarray", 25) == 0, "sanity");
2755 }
2756 #endif
2757 // Non-escaped allocation returned from Java or runtime call have unknown values in fields.
2758 int new_edges = 0;
2759 for (EdgeIterator i(pta); i.has_next(); i.next()) {
2760 PointsToNode* field = i.get();
2761 if (field->is_Field() && field->as_Field()->is_oop()) {
2762 if (add_edge(field, phantom_obj)) {
2763 // New edge was added
2764 new_edges++;
2765 add_field_uses_to_worklist(field->as_Field());
2766 }
2767 }
2768 }
2769 return new_edges;
2770 }
2771
2772 // Find fields initializing values for allocations.
2773 int ConnectionGraph::find_init_values_null(JavaObjectNode* pta, PhaseValues* phase) {
2774 assert(pta->escape_state() == PointsToNode::NoEscape, "Not escaped Allocate nodes only");
2775 Node* alloc = pta->ideal_node();
2776 // Do nothing for Call nodes since its fields values are unknown.
2777 if (!alloc->is_Allocate()) {
2778 return 0;
2779 }
2780 InitializeNode* ini = alloc->as_Allocate()->initialization();
2781 bool visited_bottom_offset = false;
2782 GrowableArray<int> offsets_worklist;
2783 int new_edges = 0;
2784
2785 // Check if an oop field's initializing value is recorded and add
2786 // a corresponding null if field's value if it is not recorded.
2787 // Connection Graph does not record a default initialization by null
2788 // captured by Initialize node.
2789 //
2790 for (EdgeIterator i(pta); i.has_next(); i.next()) {
2791 PointsToNode* field = i.get(); // Field (AddP)
2792 if (!field->is_Field() || !field->as_Field()->is_oop()) {
2793 continue; // Not oop field
2794 }
2795 int offset = field->as_Field()->offset();
2796 if (offset == Type::OffsetBot) {
2797 if (!visited_bottom_offset) {
2843 } else {
2844 if (!val->is_LocalVar() || (val->edge_count() == 0)) {
2845 tty->print_cr("----------init store has invalid value -----");
2846 store->dump();
2847 val->dump();
2848 assert(val->is_LocalVar() && (val->edge_count() > 0), "should be processed already");
2849 }
2850 for (EdgeIterator j(val); j.has_next(); j.next()) {
2851 PointsToNode* obj = j.get();
2852 if (obj->is_JavaObject()) {
2853 if (!field->points_to(obj->as_JavaObject())) {
2854 missed_obj = obj;
2855 break;
2856 }
2857 }
2858 }
2859 }
2860 if (missed_obj != nullptr) {
2861 tty->print_cr("----------field---------------------------------");
2862 field->dump();
2863 tty->print_cr("----------missed referernce to object-----------");
2864 missed_obj->dump();
2865 tty->print_cr("----------object referernced by init store -----");
2866 store->dump();
2867 val->dump();
2868 assert(!field->points_to(missed_obj->as_JavaObject()), "missed JavaObject reference");
2869 }
2870 }
2871 #endif
2872 } else {
2873 // There could be initializing stores which follow allocation.
2874 // For example, a volatile field store is not collected
2875 // by Initialize node.
2876 //
2877 // Need to check for dependent loads to separate such stores from
2878 // stores which follow loads. For now, add initial value null so
2879 // that compare pointers optimization works correctly.
2880 }
2881 }
2882 if (value == nullptr) {
2883 // A field's initializing value was not recorded. Add null.
2884 if (add_edge(field, null_obj)) {
2885 // New edge was added
3209 assert(field->edge_count() > 0, "sanity");
3210 }
3211 }
3212 }
3213 }
3214 #endif
3215
3216 // Optimize ideal graph.
3217 void ConnectionGraph::optimize_ideal_graph(GrowableArray<Node*>& ptr_cmp_worklist,
3218 GrowableArray<MemBarStoreStoreNode*>& storestore_worklist) {
3219 Compile* C = _compile;
3220 PhaseIterGVN* igvn = _igvn;
3221 if (EliminateLocks) {
3222 // Mark locks before changing ideal graph.
3223 int cnt = C->macro_count();
3224 for (int i = 0; i < cnt; i++) {
3225 Node *n = C->macro_node(i);
3226 if (n->is_AbstractLock()) { // Lock and Unlock nodes
3227 AbstractLockNode* alock = n->as_AbstractLock();
3228 if (!alock->is_non_esc_obj()) {
3229 if (can_eliminate_lock(alock)) {
3230 assert(!alock->is_eliminated() || alock->is_coarsened(), "sanity");
3231 // The lock could be marked eliminated by lock coarsening
3232 // code during first IGVN before EA. Replace coarsened flag
3233 // to eliminate all associated locks/unlocks.
3234 #ifdef ASSERT
3235 alock->log_lock_optimization(C, "eliminate_lock_set_non_esc3");
3236 #endif
3237 alock->set_non_esc_obj();
3238 }
3239 }
3240 }
3241 }
3242 }
3243
3244 if (OptimizePtrCompare) {
3245 for (int i = 0; i < ptr_cmp_worklist.length(); i++) {
3246 Node *n = ptr_cmp_worklist.at(i);
3247 assert(n->Opcode() == Op_CmpN || n->Opcode() == Op_CmpP, "must be");
3248 const TypeInt* tcmp = optimize_ptr_compare(n->in(1), n->in(2));
3249 if (tcmp->singleton()) {
3251 #ifndef PRODUCT
3252 if (PrintOptimizePtrCompare) {
3253 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"));
3254 if (Verbose) {
3255 n->dump(1);
3256 }
3257 }
3258 #endif
3259 igvn->replace_node(n, cmp);
3260 }
3261 }
3262 }
3263
3264 // For MemBarStoreStore nodes added in library_call.cpp, check
3265 // escape status of associated AllocateNode and optimize out
3266 // MemBarStoreStore node if the allocated object never escapes.
3267 for (int i = 0; i < storestore_worklist.length(); i++) {
3268 Node* storestore = storestore_worklist.at(i);
3269 Node* alloc = storestore->in(MemBarNode::Precedent)->in(0);
3270 if (alloc->is_Allocate() && not_global_escape(alloc)) {
3271 MemBarNode* mb = MemBarNode::make(C, Op_MemBarCPUOrder, Compile::AliasIdxBot);
3272 mb->init_req(TypeFunc::Memory, storestore->in(TypeFunc::Memory));
3273 mb->init_req(TypeFunc::Control, storestore->in(TypeFunc::Control));
3274 igvn->register_new_node_with_optimizer(mb);
3275 igvn->replace_node(storestore, mb);
3276 }
3277 }
3278 }
3279
3280 // Optimize objects compare.
3281 const TypeInt* ConnectionGraph::optimize_ptr_compare(Node* left, Node* right) {
3282 const TypeInt* UNKNOWN = TypeInt::CC; // [-1, 0,1]
3283 if (!OptimizePtrCompare) {
3284 return UNKNOWN;
3285 }
3286 const TypeInt* EQ = TypeInt::CC_EQ; // [0] == ZERO
3287 const TypeInt* NE = TypeInt::CC_GT; // [1] == ONE
3288
3289 PointsToNode* ptn1 = ptnode_adr(left->_idx);
3290 PointsToNode* ptn2 = ptnode_adr(right->_idx);
3291 JavaObjectNode* jobj1 = unique_java_object(left);
3292 JavaObjectNode* jobj2 = unique_java_object(right);
3293
3294 // The use of this method during allocation merge reduction may cause 'left'
3295 // or 'right' be something (e.g., a Phi) that isn't in the connection graph or
3296 // that doesn't reference an unique java object.
3297 if (ptn1 == nullptr || ptn2 == nullptr ||
3298 jobj1 == nullptr || jobj2 == nullptr) {
3299 return UNKNOWN;
3419 assert(!src->is_Field() && !dst->is_Field(), "only for JavaObject and LocalVar");
3420 assert((src != null_obj) && (dst != null_obj), "not for ConP null");
3421 PointsToNode* ptadr = _nodes.at(n->_idx);
3422 if (ptadr != nullptr) {
3423 assert(ptadr->is_Arraycopy() && ptadr->ideal_node() == n, "sanity");
3424 return;
3425 }
3426 Compile* C = _compile;
3427 ptadr = new (C->comp_arena()) ArraycopyNode(this, n, es);
3428 map_ideal_node(n, ptadr);
3429 // Add edge from arraycopy node to source object.
3430 (void)add_edge(ptadr, src);
3431 src->set_arraycopy_src();
3432 // Add edge from destination object to arraycopy node.
3433 (void)add_edge(dst, ptadr);
3434 dst->set_arraycopy_dst();
3435 }
3436
3437 bool ConnectionGraph::is_oop_field(Node* n, int offset, bool* unsafe) {
3438 const Type* adr_type = n->as_AddP()->bottom_type();
3439 BasicType bt = T_INT;
3440 if (offset == Type::OffsetBot) {
3441 // Check only oop fields.
3442 if (!adr_type->isa_aryptr() ||
3443 adr_type->isa_aryptr()->elem() == Type::BOTTOM ||
3444 adr_type->isa_aryptr()->elem()->make_oopptr() != nullptr) {
3445 // OffsetBot is used to reference array's element. Ignore first AddP.
3446 if (find_second_addp(n, n->in(AddPNode::Base)) == nullptr) {
3447 bt = T_OBJECT;
3448 }
3449 }
3450 } else if (offset != oopDesc::klass_offset_in_bytes()) {
3451 if (adr_type->isa_instptr()) {
3452 ciField* field = _compile->alias_type(adr_type->isa_instptr())->field();
3453 if (field != nullptr) {
3454 bt = field->layout_type();
3455 } else {
3456 // Check for unsafe oop field access
3457 if (n->has_out_with(Op_StoreP, Op_LoadP, Op_StoreN, Op_LoadN) ||
3458 n->has_out_with(Op_GetAndSetP, Op_GetAndSetN, Op_CompareAndExchangeP, Op_CompareAndExchangeN) ||
3459 n->has_out_with(Op_CompareAndSwapP, Op_CompareAndSwapN, Op_WeakCompareAndSwapP, Op_WeakCompareAndSwapN) ||
3460 BarrierSet::barrier_set()->barrier_set_c2()->escape_has_out_with_unsafe_object(n)) {
3461 bt = T_OBJECT;
3462 (*unsafe) = true;
3463 }
3464 }
3465 } else if (adr_type->isa_aryptr()) {
3466 if (offset == arrayOopDesc::length_offset_in_bytes()) {
3467 // Ignore array length load.
3468 } else if (find_second_addp(n, n->in(AddPNode::Base)) != nullptr) {
3469 // Ignore first AddP.
3470 } else {
3471 const Type* elemtype = adr_type->isa_aryptr()->elem();
3472 bt = elemtype->array_element_basic_type();
3473 }
3474 } else if (adr_type->isa_rawptr() || adr_type->isa_klassptr()) {
3475 // Allocation initialization, ThreadLocal field access, unsafe access
3476 if (n->has_out_with(Op_StoreP, Op_LoadP, Op_StoreN, Op_LoadN) ||
3477 n->has_out_with(Op_GetAndSetP, Op_GetAndSetN, Op_CompareAndExchangeP, Op_CompareAndExchangeN) ||
3478 n->has_out_with(Op_CompareAndSwapP, Op_CompareAndSwapN, Op_WeakCompareAndSwapP, Op_WeakCompareAndSwapN) ||
3479 BarrierSet::barrier_set()->barrier_set_c2()->escape_has_out_with_unsafe_object(n)) {
3480 bt = T_OBJECT;
3481 }
3482 }
3483 }
3484 // Note: T_NARROWOOP is not classed as a real reference type
3485 return (is_reference_type(bt) || bt == T_NARROWOOP);
3486 }
3487
3488 // Returns unique pointed java object or null.
3489 JavaObjectNode* ConnectionGraph::unique_java_object(Node *n) const {
3490 // If the node was created after the escape computation we can't answer.
3491 uint idx = n->_idx;
3492 if (idx >= nodes_size()) {
3649 return true;
3650 }
3651 }
3652 }
3653 }
3654 }
3655 return false;
3656 }
3657
3658 int ConnectionGraph::address_offset(Node* adr, PhaseValues* phase) {
3659 const Type *adr_type = phase->type(adr);
3660 if (adr->is_AddP() && adr_type->isa_oopptr() == nullptr && is_captured_store_address(adr)) {
3661 // We are computing a raw address for a store captured by an Initialize
3662 // compute an appropriate address type. AddP cases #3 and #5 (see below).
3663 int offs = (int)phase->find_intptr_t_con(adr->in(AddPNode::Offset), Type::OffsetBot);
3664 assert(offs != Type::OffsetBot ||
3665 adr->in(AddPNode::Address)->in(0)->is_AllocateArray(),
3666 "offset must be a constant or it is initialization of array");
3667 return offs;
3668 }
3669 const TypePtr *t_ptr = adr_type->isa_ptr();
3670 assert(t_ptr != nullptr, "must be a pointer type");
3671 return t_ptr->offset();
3672 }
3673
3674 Node* ConnectionGraph::get_addp_base(Node *addp) {
3675 assert(addp->is_AddP(), "must be AddP");
3676 //
3677 // AddP cases for Base and Address inputs:
3678 // case #1. Direct object's field reference:
3679 // Allocate
3680 // |
3681 // Proj #5 ( oop result )
3682 // |
3683 // CheckCastPP (cast to instance type)
3684 // | |
3685 // AddP ( base == address )
3686 //
3687 // case #2. Indirect object's field reference:
3688 // Phi
3689 // |
3690 // CastPP (cast to instance type)
3691 // | |
3805 }
3806 return nullptr;
3807 }
3808
3809 //
3810 // Adjust the type and inputs of an AddP which computes the
3811 // address of a field of an instance
3812 //
3813 bool ConnectionGraph::split_AddP(Node *addp, Node *base) {
3814 PhaseGVN* igvn = _igvn;
3815 const TypeOopPtr *base_t = igvn->type(base)->isa_oopptr();
3816 assert(base_t != nullptr && base_t->is_known_instance(), "expecting instance oopptr");
3817 const TypeOopPtr *t = igvn->type(addp)->isa_oopptr();
3818 if (t == nullptr) {
3819 // We are computing a raw address for a store captured by an Initialize
3820 // compute an appropriate address type (cases #3 and #5).
3821 assert(igvn->type(addp) == TypeRawPtr::NOTNULL, "must be raw pointer");
3822 assert(addp->in(AddPNode::Address)->is_Proj(), "base of raw address must be result projection from allocation");
3823 intptr_t offs = (int)igvn->find_intptr_t_con(addp->in(AddPNode::Offset), Type::OffsetBot);
3824 assert(offs != Type::OffsetBot, "offset must be a constant");
3825 t = base_t->add_offset(offs)->is_oopptr();
3826 }
3827 int inst_id = base_t->instance_id();
3828 assert(!t->is_known_instance() || t->instance_id() == inst_id,
3829 "old type must be non-instance or match new type");
3830
3831 // The type 't' could be subclass of 'base_t'.
3832 // As result t->offset() could be large then base_t's size and it will
3833 // cause the failure in add_offset() with narrow oops since TypeOopPtr()
3834 // constructor verifies correctness of the offset.
3835 //
3836 // It could happened on subclass's branch (from the type profiling
3837 // inlining) which was not eliminated during parsing since the exactness
3838 // of the allocation type was not propagated to the subclass type check.
3839 //
3840 // Or the type 't' could be not related to 'base_t' at all.
3841 // It could happened when CHA type is different from MDO type on a dead path
3842 // (for example, from instanceof check) which is not collapsed during parsing.
3843 //
3844 // Do nothing for such AddP node and don't process its users since
3845 // this code branch will go away.
3846 //
3847 if (!t->is_known_instance() &&
3848 !base_t->maybe_java_subtype_of(t)) {
3849 return false; // bail out
3850 }
3851 const TypeOopPtr *tinst = base_t->add_offset(t->offset())->is_oopptr();
3852 // Do NOT remove the next line: ensure a new alias index is allocated
3853 // for the instance type. Note: C++ will not remove it since the call
3854 // has side effect.
3855 int alias_idx = _compile->get_alias_index(tinst);
3856 igvn->set_type(addp, tinst);
3857 // record the allocation in the node map
3858 set_map(addp, get_map(base->_idx));
3859 // Set addp's Base and Address to 'base'.
3860 Node *abase = addp->in(AddPNode::Base);
3861 Node *adr = addp->in(AddPNode::Address);
3862 if (adr->is_Proj() && adr->in(0)->is_Allocate() &&
3863 adr->in(0)->_idx == (uint)inst_id) {
3864 // Skip AddP cases #3 and #5.
3865 } else {
3866 assert(!abase->is_top(), "sanity"); // AddP case #3
3867 if (abase != base) {
3868 igvn->hash_delete(addp);
3869 addp->set_req(AddPNode::Base, base);
3870 if (abase == adr) {
3871 addp->set_req(AddPNode::Address, base);
4537 ptnode_adr(n->_idx)->dump();
4538 assert(jobj != nullptr && jobj != phantom_obj, "escaped allocation");
4539 #endif
4540 _compile->record_failure(_invocation > 0 ? C2Compiler::retry_no_iterative_escape_analysis() : C2Compiler::retry_no_escape_analysis());
4541 return;
4542 } else {
4543 Node *val = get_map(jobj->idx()); // CheckCastPP node
4544 TypeNode *tn = n->as_Type();
4545 const TypeOopPtr* tinst = igvn->type(val)->isa_oopptr();
4546 assert(tinst != nullptr && tinst->is_known_instance() &&
4547 tinst->instance_id() == jobj->idx() , "instance type expected.");
4548
4549 const Type *tn_type = igvn->type(tn);
4550 const TypeOopPtr *tn_t;
4551 if (tn_type->isa_narrowoop()) {
4552 tn_t = tn_type->make_ptr()->isa_oopptr();
4553 } else {
4554 tn_t = tn_type->isa_oopptr();
4555 }
4556 if (tn_t != nullptr && tinst->maybe_java_subtype_of(tn_t)) {
4557 if (tn_type->isa_narrowoop()) {
4558 tn_type = tinst->make_narrowoop();
4559 } else {
4560 tn_type = tinst;
4561 }
4562 igvn->hash_delete(tn);
4563 igvn->set_type(tn, tn_type);
4564 tn->set_type(tn_type);
4565 igvn->hash_insert(tn);
4566 record_for_optimizer(n);
4567 } else {
4568 assert(tn_type == TypePtr::NULL_PTR ||
4569 (tn_t != nullptr && !tinst->maybe_java_subtype_of(tn_t)),
4570 "unexpected type");
4571 continue; // Skip dead path with different type
4572 }
4573 }
4574 } else {
4575 DEBUG_ONLY(n->dump();)
4576 assert(false, "EA: unexpected node");
4577 continue;
4578 }
4579 // push allocation's users on appropriate worklist
4580 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
4581 Node *use = n->fast_out(i);
4582 if(use->is_Mem() && use->in(MemNode::Address) == n) {
4583 // Load/store to instance's field
4584 memnode_worklist.append_if_missing(use);
4585 } else if (use->is_MemBar()) {
4586 if (use->in(TypeFunc::Memory) == n) { // Ignore precedent edge
4587 memnode_worklist.append_if_missing(use);
4588 }
4589 } else if (use->is_AddP() && use->outcnt() > 0) { // No dead nodes
4590 Node* addp2 = find_second_addp(use, n);
4591 if (addp2 != nullptr) {
4592 alloc_worklist.append_if_missing(addp2);
4593 }
4594 alloc_worklist.append_if_missing(use);
4595 } else if (use->is_Phi() ||
4596 use->is_CheckCastPP() ||
4597 use->is_EncodeNarrowPtr() ||
4598 use->is_DecodeNarrowPtr() ||
4599 (use->is_ConstraintCast() && use->Opcode() == Op_CastPP)) {
4600 alloc_worklist.append_if_missing(use);
4601 #ifdef ASSERT
4602 } else if (use->is_Mem()) {
4603 assert(use->in(MemNode::Address) != n, "EA: missing allocation reference path");
4604 } else if (use->is_MergeMem()) {
4605 assert(mergemem_worklist.contains(use->as_MergeMem()), "EA: missing MergeMem node in the worklist");
4606 } else if (use->is_SafePoint()) {
4607 // Look for MergeMem nodes for calls which reference unique allocation
4608 // (through CheckCastPP nodes) even for debug info.
4609 Node* m = use->in(TypeFunc::Memory);
4610 if (m->is_MergeMem()) {
4611 assert(mergemem_worklist.contains(m->as_MergeMem()), "EA: missing MergeMem node in the worklist");
4612 }
4613 } else if (use->Opcode() == Op_EncodeISOArray) {
4614 if (use->in(MemNode::Memory) == n || use->in(3) == n) {
4615 // EncodeISOArray overwrites destination array
4616 memnode_worklist.append_if_missing(use);
4617 }
4618 } else {
4619 uint op = use->Opcode();
4620 if ((op == Op_StrCompressedCopy || op == Op_StrInflatedCopy) &&
4621 (use->in(MemNode::Memory) == n)) {
4622 // They overwrite memory edge corresponding to destination array,
4623 memnode_worklist.append_if_missing(use);
4624 } else if (!(op == Op_CmpP || op == Op_Conv2B ||
4625 op == Op_CastP2X ||
4626 op == Op_FastLock || op == Op_AryEq ||
4627 op == Op_StrComp || op == Op_CountPositives ||
4628 op == Op_StrCompressedCopy || op == Op_StrInflatedCopy ||
4629 op == Op_StrEquals || op == Op_VectorizedHashCode ||
4630 op == Op_StrIndexOf || op == Op_StrIndexOfChar ||
4631 op == Op_SubTypeCheck ||
4632 op == Op_ReinterpretS2HF ||
4633 BarrierSet::barrier_set()->barrier_set_c2()->is_gc_barrier_node(use))) {
4634 n->dump();
4635 use->dump();
4636 assert(false, "EA: missing allocation reference path");
4637 }
4638 #endif
4639 }
4640 }
4641
4642 }
4643
4644 #ifdef ASSERT
4645 if (VerifyReduceAllocationMerges) {
4646 for (uint i = 0; i < reducible_merges.size(); i++) {
4647 Node* phi = reducible_merges.at(i);
4648
4649 if (!reduced_merges.member(phi)) {
4650 phi->dump(2);
4651 phi->dump(-2);
4715 // we don't need to do anything, but the users must be pushed
4716 n = n->as_MemBar()->proj_out_or_null(TypeFunc::Memory);
4717 if (n == nullptr) {
4718 continue;
4719 }
4720 } else if (n->is_CallLeaf()) {
4721 // Runtime calls with narrow memory input (no MergeMem node)
4722 // get the memory projection
4723 n = n->as_Call()->proj_out_or_null(TypeFunc::Memory);
4724 if (n == nullptr) {
4725 continue;
4726 }
4727 } else if (n->Opcode() == Op_StrInflatedCopy) {
4728 // Check direct uses of StrInflatedCopy.
4729 // It is memory type Node - no special SCMemProj node.
4730 } else if (n->Opcode() == Op_StrCompressedCopy ||
4731 n->Opcode() == Op_EncodeISOArray) {
4732 // get the memory projection
4733 n = n->find_out_with(Op_SCMemProj);
4734 assert(n != nullptr && n->Opcode() == Op_SCMemProj, "memory projection required");
4735 } else {
4736 #ifdef ASSERT
4737 if (!n->is_Mem()) {
4738 n->dump();
4739 }
4740 assert(n->is_Mem(), "memory node required.");
4741 #endif
4742 Node *addr = n->in(MemNode::Address);
4743 const Type *addr_t = igvn->type(addr);
4744 if (addr_t == Type::TOP) {
4745 continue;
4746 }
4747 assert (addr_t->isa_ptr() != nullptr, "pointer type required.");
4748 int alias_idx = _compile->get_alias_index(addr_t->is_ptr());
4749 assert ((uint)alias_idx < new_index_end, "wrong alias index");
4750 Node *mem = find_inst_mem(n->in(MemNode::Memory), alias_idx, orig_phis);
4751 if (_compile->failing()) {
4752 return;
4753 }
4754 if (mem != n->in(MemNode::Memory)) {
4759 if (n->is_Load()) {
4760 continue; // don't push users
4761 } else if (n->is_LoadStore()) {
4762 // get the memory projection
4763 n = n->find_out_with(Op_SCMemProj);
4764 assert(n != nullptr && n->Opcode() == Op_SCMemProj, "memory projection required");
4765 }
4766 }
4767 // push user on appropriate worklist
4768 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
4769 Node *use = n->fast_out(i);
4770 if (use->is_Phi() || use->is_ClearArray()) {
4771 memnode_worklist.append_if_missing(use);
4772 } else if (use->is_Mem() && use->in(MemNode::Memory) == n) {
4773 memnode_worklist.append_if_missing(use);
4774 } else if (use->is_MemBar() || use->is_CallLeaf()) {
4775 if (use->in(TypeFunc::Memory) == n) { // Ignore precedent edge
4776 memnode_worklist.append_if_missing(use);
4777 }
4778 #ifdef ASSERT
4779 } else if(use->is_Mem()) {
4780 assert(use->in(MemNode::Memory) != n, "EA: missing memory path");
4781 } else if (use->is_MergeMem()) {
4782 assert(mergemem_worklist.contains(use->as_MergeMem()), "EA: missing MergeMem node in the worklist");
4783 } else if (use->Opcode() == Op_EncodeISOArray) {
4784 if (use->in(MemNode::Memory) == n || use->in(3) == n) {
4785 // EncodeISOArray overwrites destination array
4786 memnode_worklist.append_if_missing(use);
4787 }
4788 } else {
4789 uint op = use->Opcode();
4790 if ((use->in(MemNode::Memory) == n) &&
4791 (op == Op_StrCompressedCopy || op == Op_StrInflatedCopy)) {
4792 // They overwrite memory edge corresponding to destination array,
4793 memnode_worklist.append_if_missing(use);
4794 } else if (!(BarrierSet::barrier_set()->barrier_set_c2()->is_gc_barrier_node(use) ||
4795 op == Op_AryEq || op == Op_StrComp || op == Op_CountPositives ||
4796 op == Op_StrCompressedCopy || op == Op_StrInflatedCopy || op == Op_VectorizedHashCode ||
4797 op == Op_StrEquals || op == Op_StrIndexOf || op == Op_StrIndexOfChar)) {
4798 n->dump();
4799 use->dump();
4800 assert(false, "EA: missing memory path");
4801 }
4802 #endif
4803 }
4804 }
4805 }
4806
4807 // Phase 3: Process MergeMem nodes from mergemem_worklist.
4808 // Walk each memory slice moving the first node encountered of each
4809 // instance type to the input corresponding to its alias index.
4810 uint length = mergemem_worklist.length();
4811 for( uint next = 0; next < length; ++next ) {
4812 MergeMemNode* nmm = mergemem_worklist.at(next);
4813 assert(!visited.test_set(nmm->_idx), "should not be visited before");
4814 // Note: we don't want to use MergeMemStream here because we only want to
4815 // scan inputs which exist at the start, not ones we add during processing.
4816 // Note 2: MergeMem may already contains instance memory slices added
4817 // during find_inst_mem() call when memory nodes were processed above.
4878 if (_compile->live_nodes() >= _compile->max_node_limit() * 0.75) {
4879 if (_compile->do_reduce_allocation_merges()) {
4880 _compile->record_failure(C2Compiler::retry_no_reduce_allocation_merges());
4881 } else if (_invocation > 0) {
4882 _compile->record_failure(C2Compiler::retry_no_iterative_escape_analysis());
4883 } else {
4884 _compile->record_failure(C2Compiler::retry_no_escape_analysis());
4885 }
4886 return;
4887 }
4888
4889 igvn->hash_insert(nmm);
4890 record_for_optimizer(nmm);
4891 }
4892
4893 // Phase 4: Update the inputs of non-instance memory Phis and
4894 // the Memory input of memnodes
4895 // First update the inputs of any non-instance Phi's from
4896 // which we split out an instance Phi. Note we don't have
4897 // to recursively process Phi's encountered on the input memory
4898 // chains as is done in split_memory_phi() since they will
4899 // also be processed here.
4900 for (int j = 0; j < orig_phis.length(); j++) {
4901 PhiNode *phi = orig_phis.at(j);
4902 int alias_idx = _compile->get_alias_index(phi->adr_type());
4903 igvn->hash_delete(phi);
4904 for (uint i = 1; i < phi->req(); i++) {
4905 Node *mem = phi->in(i);
4906 Node *new_mem = find_inst_mem(mem, alias_idx, orig_phis);
4907 if (_compile->failing()) {
4908 return;
4909 }
4910 if (mem != new_mem) {
4911 phi->set_req(i, new_mem);
4912 }
4913 }
4914 igvn->hash_insert(phi);
4915 record_for_optimizer(phi);
4916 }
4917
4918 // Update the memory inputs of MemNodes with the value we computed
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "ci/bcEscapeAnalyzer.hpp"
26 #include "compiler/compileLog.hpp"
27 #include "gc/shared/barrierSet.hpp"
28 #include "gc/shared/c2/barrierSetC2.hpp"
29 #include "libadt/vectset.hpp"
30 #include "memory/allocation.hpp"
31 #include "memory/metaspace.hpp"
32 #include "memory/resourceArea.hpp"
33 #include "opto/arraycopynode.hpp"
34 #include "opto/c2compiler.hpp"
35 #include "opto/callnode.hpp"
36 #include "opto/castnode.hpp"
37 #include "opto/cfgnode.hpp"
38 #include "opto/compile.hpp"
39 #include "opto/escape.hpp"
40 #include "opto/inlinetypenode.hpp"
41 #include "opto/locknode.hpp"
42 #include "opto/macro.hpp"
43 #include "opto/movenode.hpp"
44 #include "opto/narrowptrnode.hpp"
45 #include "opto/phaseX.hpp"
46 #include "opto/rootnode.hpp"
47 #include "utilities/macros.hpp"
48
49 ConnectionGraph::ConnectionGraph(Compile * C, PhaseIterGVN *igvn, int invocation) :
50 // If ReduceAllocationMerges is enabled we might call split_through_phi during
51 // split_unique_types and that will create additional nodes that need to be
52 // pushed to the ConnectionGraph. The code below bumps the initial capacity of
53 // _nodes by 10% to account for these additional nodes. If capacity is exceeded
54 // the array will be reallocated.
55 _nodes(C->comp_arena(), C->do_reduce_allocation_merges() ? C->unique()*1.10 : C->unique(), C->unique(), nullptr),
56 _in_worklist(C->comp_arena()),
57 _next_pidx(0),
58 _collecting(true),
59 _verify(false),
60 _compile(C),
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:
408 split_unique_types(alloc_worklist, arraycopy_worklist, mergemem_worklist, reducible_merges);
409 if (C->failing()) {
410 NOT_PRODUCT(escape_state_statistics(java_objects_worklist);)
411 return false;
412 }
413 C->print_method(PHASE_AFTER_EA, 2);
414
415 #ifdef ASSERT
416 } else if (Verbose && (PrintEscapeAnalysis || PrintEliminateAllocations)) {
417 tty->print("=== No allocations eliminated for ");
418 C->method()->print_short_name();
419 if (!EliminateAllocations) {
420 tty->print(" since EliminateAllocations is off ===");
421 } else if(!has_scalar_replaceable_candidates) {
422 tty->print(" since there are no scalar replaceable candidates ===");
423 }
424 tty->cr();
425 #endif
426 }
427
428 // 6. Expand flat accesses if the object does not escape. This adds nodes to
429 // the graph, so it has to be after split_unique_types. This expands atomic
430 // mismatched accesses (though encapsulated in LoadFlats and StoreFlats) into
431 // non-mismatched accesses, so it is better before reduce allocation merges.
432 if (has_non_escaping_obj) {
433 optimize_flat_accesses(sfn_worklist);
434 }
435
436 // 7. Reduce allocation merges used as debug information. This is done after
437 // split_unique_types because the methods used to create SafePointScalarObject
438 // need to traverse the memory graph to find values for object fields. We also
439 // set to null the scalarized inputs of reducible Phis so that the Allocate
440 // that they point can be later scalar replaced.
441 bool delay = _igvn->delay_transform();
442 _igvn->set_delay_transform(true);
443 for (uint i = 0; i < reducible_merges.size(); i++) {
444 Node* n = reducible_merges.at(i);
445 if (n->outcnt() > 0) {
446 if (!reduce_phi_on_safepoints(n->as_Phi())) {
447 NOT_PRODUCT(escape_state_statistics(java_objects_worklist);)
448 C->record_failure(C2Compiler::retry_no_reduce_allocation_merges());
449 return false;
450 }
451
452 // Now we set the scalar replaceable inputs of ophi to null, which is
453 // the last piece that would prevent it from being scalar replaceable.
454 reset_scalar_replaceable_entries(n->as_Phi());
455 }
456 }
1255
1256 // The next two inputs are:
1257 // (1) A copy of the original pointer to NSR objects.
1258 // (2) A selector, used to decide if we need to rematerialize an object
1259 // or use the pointer to a NSR object.
1260 // See more details of these fields in the declaration of SafePointScalarMergeNode
1261 sfpt->add_req(nsr_merge_pointer);
1262 sfpt->add_req(selector);
1263
1264 for (uint i = 1; i < ophi->req(); i++) {
1265 Node* base = ophi->in(i);
1266 JavaObjectNode* ptn = unique_java_object(base);
1267
1268 // If the base is not scalar replaceable we don't need to register information about
1269 // it at this time.
1270 if (ptn == nullptr || !ptn->scalar_replaceable()) {
1271 continue;
1272 }
1273
1274 AllocateNode* alloc = ptn->ideal_node()->as_Allocate();
1275 Unique_Node_List value_worklist;
1276 #ifdef ASSERT
1277 const Type* res_type = alloc->result_cast()->bottom_type();
1278 if (res_type->is_inlinetypeptr() && !Compile::current()->has_circular_inline_type()) {
1279 PhiNode* phi = ophi->as_Phi();
1280 assert(!ophi->as_Phi()->can_push_inline_types_down(_igvn), "missed earlier scalarization opportunity");
1281 }
1282 #endif
1283 SafePointScalarObjectNode* sobj = mexp.create_scalarized_object_description(alloc, sfpt, &value_worklist);
1284 if (sobj == nullptr) {
1285 _compile->record_failure(C2Compiler::retry_no_reduce_allocation_merges());
1286 return false;
1287 }
1288
1289 // Now make a pass over the debug information replacing any references
1290 // to the allocated object with "sobj"
1291 Node* ccpp = alloc->result_cast();
1292 sfpt->replace_edges_in_range(ccpp, sobj, debug_start, jvms->debug_end(), _igvn);
1293
1294 // Register the scalarized object as a candidate for reallocation
1295 smerge->add_req(sobj);
1296
1297 // Scalarize inline types that were added to the safepoint.
1298 // Don't allow linking a constant oop (if available) for flat array elements
1299 // because Deoptimization::reassign_flat_array_elements needs field values.
1300 const bool allow_oop = !merge_t->is_flat();
1301 for (uint j = 0; j < value_worklist.size(); ++j) {
1302 InlineTypeNode* vt = value_worklist.at(j)->as_InlineType();
1303 vt->make_scalar_in_safepoints(_igvn, allow_oop);
1304 }
1305 }
1306
1307 // Replaces debug information references to "original_sfpt_parent" in "sfpt" with references to "smerge"
1308 sfpt->replace_edges_in_range(original_sfpt_parent, smerge, debug_start, jvms->debug_end(), _igvn);
1309
1310 // The call to 'replace_edges_in_range' above might have removed the
1311 // reference to ophi that we need at _merge_pointer_idx. The line below make
1312 // sure the reference is maintained.
1313 sfpt->set_req(smerge->merge_pointer_idx(jvms), nsr_merge_pointer);
1314 _igvn->_worklist.push(sfpt);
1315 }
1316
1317 return true;
1318 }
1319
1320 void ConnectionGraph::reduce_phi(PhiNode* ophi, GrowableArray<Node *> &alloc_worklist, GrowableArray<Node *> &memnode_worklist) {
1321 bool delay = _igvn->delay_transform();
1322 _igvn->set_delay_transform(true);
1323 _igvn->hash_delete(ophi);
1324
1482 return false;
1483 }
1484
1485 // Returns true if at least one of the arguments to the call is an object
1486 // that does not escape globally.
1487 bool ConnectionGraph::has_arg_escape(CallJavaNode* call) {
1488 if (call->method() != nullptr) {
1489 uint max_idx = TypeFunc::Parms + call->method()->arg_size();
1490 for (uint idx = TypeFunc::Parms; idx < max_idx; idx++) {
1491 Node* p = call->in(idx);
1492 if (not_global_escape(p)) {
1493 return true;
1494 }
1495 }
1496 } else {
1497 const char* name = call->as_CallStaticJava()->_name;
1498 assert(name != nullptr, "no name");
1499 // no arg escapes through uncommon traps
1500 if (strcmp(name, "uncommon_trap") != 0) {
1501 // process_call_arguments() assumes that all arguments escape globally
1502 const TypeTuple* d = call->tf()->domain_sig();
1503 for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
1504 const Type* at = d->field_at(i);
1505 if (at->isa_oopptr() != nullptr) {
1506 return true;
1507 }
1508 }
1509 }
1510 }
1511 return false;
1512 }
1513
1514
1515
1516 // Utility function for nodes that load an object
1517 void ConnectionGraph::add_objload_to_connection_graph(Node *n, Unique_Node_List *delayed_worklist) {
1518 // Using isa_ptr() instead of isa_oopptr() for LoadP and Phi because
1519 // ThreadLocal has RawPtr type.
1520 const Type* t = _igvn->type(n);
1521 if (t->make_ptr() != nullptr) {
1522 Node* adr = n->in(MemNode::Address);
1556 // first IGVN optimization when escape information is still available.
1557 record_for_optimizer(n);
1558 } else if (n->is_Allocate()) {
1559 add_call_node(n->as_Call());
1560 record_for_optimizer(n);
1561 } else {
1562 if (n->is_CallStaticJava()) {
1563 const char* name = n->as_CallStaticJava()->_name;
1564 if (name != nullptr && strcmp(name, "uncommon_trap") == 0) {
1565 return; // Skip uncommon traps
1566 }
1567 }
1568 // Don't mark as processed since call's arguments have to be processed.
1569 delayed_worklist->push(n);
1570 // Check if a call returns an object.
1571 if ((n->as_Call()->returns_pointer() &&
1572 n->as_Call()->proj_out_or_null(TypeFunc::Parms) != nullptr) ||
1573 (n->is_CallStaticJava() &&
1574 n->as_CallStaticJava()->is_boxing_method())) {
1575 add_call_node(n->as_Call());
1576 } else if (n->as_Call()->tf()->returns_inline_type_as_fields()) {
1577 bool returns_oop = false;
1578 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax && !returns_oop; i++) {
1579 ProjNode* pn = n->fast_out(i)->as_Proj();
1580 if (pn->_con >= TypeFunc::Parms && pn->bottom_type()->isa_ptr()) {
1581 returns_oop = true;
1582 }
1583 }
1584 if (returns_oop) {
1585 add_call_node(n->as_Call());
1586 }
1587 }
1588 }
1589 return;
1590 }
1591 // Put this check here to process call arguments since some call nodes
1592 // point to phantom_obj.
1593 if (n_ptn == phantom_obj || n_ptn == null_obj) {
1594 return; // Skip predefined nodes.
1595 }
1596 switch (opcode) {
1597 case Op_AddP: {
1598 Node* base = get_addp_base(n);
1599 PointsToNode* ptn_base = ptnode_adr(base->_idx);
1600 // Field nodes are created for all field types. They are used in
1601 // adjust_scalar_replaceable_state() and split_unique_types().
1602 // Note, non-oop fields will have only base edges in Connection
1603 // Graph because such fields are not used for oop loads and stores.
1604 int offset = address_offset(n, igvn);
1605 add_field(n, PointsToNode::NoEscape, offset);
1606 if (ptn_base == nullptr) {
1607 delayed_worklist->push(n); // Process it later.
1608 } else {
1609 n_ptn = ptnode_adr(n_idx);
1610 add_base(n_ptn->as_Field(), ptn_base);
1611 }
1612 break;
1613 }
1614 case Op_CastX2P:
1615 case Op_CastI2N: {
1616 map_ideal_node(n, phantom_obj);
1617 break;
1618 }
1619 case Op_InlineType:
1620 case Op_CastPP:
1621 case Op_CheckCastPP:
1622 case Op_EncodeP:
1623 case Op_DecodeN:
1624 case Op_EncodePKlass:
1625 case Op_DecodeNKlass: {
1626 add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(1), delayed_worklist);
1627 break;
1628 }
1629 case Op_CMoveP: {
1630 add_local_var(n, PointsToNode::NoEscape);
1631 // Do not add edges during first iteration because some could be
1632 // not defined yet.
1633 delayed_worklist->push(n);
1634 break;
1635 }
1636 case Op_ConP:
1637 case Op_ConN:
1638 case Op_ConNKlass: {
1639 // assume all oop constants globally escape except for null
1669 break;
1670 }
1671 case Op_PartialSubtypeCheck: {
1672 // Produces Null or notNull and is used in only in CmpP so
1673 // phantom_obj could be used.
1674 map_ideal_node(n, phantom_obj); // Result is unknown
1675 break;
1676 }
1677 case Op_Phi: {
1678 // Using isa_ptr() instead of isa_oopptr() for LoadP and Phi because
1679 // ThreadLocal has RawPtr type.
1680 const Type* t = n->as_Phi()->type();
1681 if (t->make_ptr() != nullptr) {
1682 add_local_var(n, PointsToNode::NoEscape);
1683 // Do not add edges during first iteration because some could be
1684 // not defined yet.
1685 delayed_worklist->push(n);
1686 }
1687 break;
1688 }
1689 case Op_LoadFlat:
1690 // Treat LoadFlat similar to an unknown call that receives nothing and produces its results
1691 map_ideal_node(n, phantom_obj);
1692 break;
1693 case Op_StoreFlat:
1694 // Treat StoreFlat similar to a call that escapes the stored flattened fields
1695 delayed_worklist->push(n);
1696 break;
1697 case Op_Proj: {
1698 // we are only interested in the oop result projection from a call
1699 if (n->as_Proj()->_con >= TypeFunc::Parms && n->in(0)->is_Call() &&
1700 (n->in(0)->as_Call()->returns_pointer() || n->bottom_type()->isa_ptr())) {
1701 assert((n->as_Proj()->_con == TypeFunc::Parms && n->in(0)->as_Call()->returns_pointer()) ||
1702 n->in(0)->as_Call()->tf()->returns_inline_type_as_fields(), "what kind of oop return is it?");
1703 add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(0), delayed_worklist);
1704 } else if (n->as_Proj()->_con >= TypeFunc::Parms && n->in(0)->is_LoadFlat() && igvn->type(n)->isa_ptr()) {
1705 // Treat LoadFlat outputs similar to a call return value
1706 add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(0), delayed_worklist);
1707 }
1708 break;
1709 }
1710 case Op_Rethrow: // Exception object escapes
1711 case Op_Return: {
1712 if (n->req() > TypeFunc::Parms &&
1713 igvn->type(n->in(TypeFunc::Parms))->isa_oopptr()) {
1714 // Treat Return value as LocalVar with GlobalEscape escape state.
1715 add_local_var_and_edge(n, PointsToNode::GlobalEscape, n->in(TypeFunc::Parms), delayed_worklist);
1716 }
1717 break;
1718 }
1719 case Op_CompareAndExchangeP:
1720 case Op_CompareAndExchangeN:
1721 case Op_GetAndSetP:
1722 case Op_GetAndSetN: {
1723 add_objload_to_connection_graph(n, delayed_worklist);
1724 // fall-through
1725 }
1771 break;
1772 }
1773 default:
1774 ; // Do nothing for nodes not related to EA.
1775 }
1776 return;
1777 }
1778
1779 // Add final simple edges to graph.
1780 void ConnectionGraph::add_final_edges(Node *n) {
1781 PointsToNode* n_ptn = ptnode_adr(n->_idx);
1782 #ifdef ASSERT
1783 if (_verify && n_ptn->is_JavaObject())
1784 return; // This method does not change graph for JavaObject.
1785 #endif
1786
1787 if (n->is_Call()) {
1788 process_call_arguments(n->as_Call());
1789 return;
1790 }
1791 assert(n->is_Store() || n->is_LoadStore() || n->is_StoreFlat() ||
1792 ((n_ptn != nullptr) && (n_ptn->ideal_node() != nullptr)),
1793 "node should be registered already");
1794 int opcode = n->Opcode();
1795 bool gc_handled = BarrierSet::barrier_set()->barrier_set_c2()->escape_add_final_edges(this, _igvn, n, opcode);
1796 if (gc_handled) {
1797 return; // Ignore node if already handled by GC.
1798 }
1799 switch (opcode) {
1800 case Op_AddP: {
1801 Node* base = get_addp_base(n);
1802 PointsToNode* ptn_base = ptnode_adr(base->_idx);
1803 assert(ptn_base != nullptr, "field's base should be registered");
1804 add_base(n_ptn->as_Field(), ptn_base);
1805 break;
1806 }
1807 case Op_InlineType:
1808 case Op_CastPP:
1809 case Op_CheckCastPP:
1810 case Op_EncodeP:
1811 case Op_DecodeN:
1812 case Op_EncodePKlass:
1813 case Op_DecodeNKlass: {
1814 add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(1), nullptr);
1815 break;
1816 }
1817 case Op_CMoveP: {
1818 for (uint i = CMoveNode::IfFalse; i < n->req(); i++) {
1819 Node* in = n->in(i);
1820 if (in == nullptr) {
1821 continue; // ignore null
1822 }
1823 Node* uncast_in = in->uncast();
1824 if (uncast_in->is_top() || uncast_in == n) {
1825 continue; // ignore top or inputs which go back this node
1826 }
1827 PointsToNode* ptn = ptnode_adr(in->_idx);
1840 }
1841 case Op_Phi: {
1842 // Using isa_ptr() instead of isa_oopptr() for LoadP and Phi because
1843 // ThreadLocal has RawPtr type.
1844 assert(n->as_Phi()->type()->make_ptr() != nullptr, "Unexpected node type");
1845 for (uint i = 1; i < n->req(); i++) {
1846 Node* in = n->in(i);
1847 if (in == nullptr) {
1848 continue; // ignore null
1849 }
1850 Node* uncast_in = in->uncast();
1851 if (uncast_in->is_top() || uncast_in == n) {
1852 continue; // ignore top or inputs which go back this node
1853 }
1854 PointsToNode* ptn = ptnode_adr(in->_idx);
1855 assert(ptn != nullptr, "node should be registered");
1856 add_edge(n_ptn, ptn);
1857 }
1858 break;
1859 }
1860 case Op_StoreFlat: {
1861 // StoreFlat globally escapes its stored flattened fields
1862 InlineTypeNode* value = n->as_StoreFlat()->value();
1863 ciInlineKlass* vk = _igvn->type(value)->inline_klass();
1864 for (int i = 0; i < vk->nof_nonstatic_fields(); i++) {
1865 ciField* field = vk->nonstatic_field_at(i);
1866 if (field->type()->is_primitive_type()) {
1867 continue;
1868 }
1869
1870 Node* field_value = value->field_value_by_offset(field->offset_in_bytes(), true);
1871 PointsToNode* field_value_ptn = ptnode_adr(field_value->_idx);
1872 set_escape_state(field_value_ptn, PointsToNode::GlobalEscape NOT_PRODUCT(COMMA "store into a flat field"));
1873 }
1874 break;
1875 }
1876 case Op_Proj: {
1877 if (n->in(0)->is_Call()) {
1878 // we are only interested in the oop result projection from a call
1879 assert((n->as_Proj()->_con == TypeFunc::Parms && n->in(0)->as_Call()->returns_pointer()) ||
1880 n->in(0)->as_Call()->tf()->returns_inline_type_as_fields(), "what kind of oop return is it?");
1881 add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(0), nullptr);
1882 } else if (n->in(0)->is_LoadFlat()) {
1883 // Treat LoadFlat outputs similar to a call return value
1884 add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(0), nullptr);
1885 }
1886 break;
1887 }
1888 case Op_Rethrow: // Exception object escapes
1889 case Op_Return: {
1890 assert(n->req() > TypeFunc::Parms && _igvn->type(n->in(TypeFunc::Parms))->isa_oopptr(),
1891 "Unexpected node type");
1892 // Treat Return value as LocalVar with GlobalEscape escape state.
1893 add_local_var_and_edge(n, PointsToNode::GlobalEscape, n->in(TypeFunc::Parms), nullptr);
1894 break;
1895 }
1896 case Op_CompareAndExchangeP:
1897 case Op_CompareAndExchangeN:
1898 case Op_GetAndSetP:
1899 case Op_GetAndSetN:{
1900 assert(_igvn->type(n)->make_ptr() != nullptr, "Unexpected node type");
1901 add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(MemNode::Address), nullptr);
1902 // fall-through
1903 }
1904 case Op_CompareAndSwapP:
1905 case Op_CompareAndSwapN:
2040 PointsToNode* ptn = ptnode_adr(val->_idx);
2041 assert(ptn != nullptr, "node should be registered");
2042 set_escape_state(ptn, PointsToNode::GlobalEscape NOT_PRODUCT(COMMA "stored at raw address"));
2043 // Add edge to object for unsafe access with offset.
2044 PointsToNode* adr_ptn = ptnode_adr(adr->_idx);
2045 assert(adr_ptn != nullptr, "node should be registered");
2046 if (adr_ptn->is_Field()) {
2047 assert(adr_ptn->as_Field()->is_oop(), "should be oop field");
2048 add_edge(adr_ptn, ptn);
2049 }
2050 return true;
2051 }
2052 #ifdef ASSERT
2053 n->dump(1);
2054 assert(false, "not unsafe");
2055 #endif
2056 return false;
2057 }
2058
2059 void ConnectionGraph::add_call_node(CallNode* call) {
2060 assert(call->returns_pointer() || call->tf()->returns_inline_type_as_fields(), "only for call which returns pointer");
2061 uint call_idx = call->_idx;
2062 if (call->is_Allocate()) {
2063 Node* k = call->in(AllocateNode::KlassNode);
2064 const TypeKlassPtr* kt = k->bottom_type()->isa_klassptr();
2065 assert(kt != nullptr, "TypeKlassPtr required.");
2066 PointsToNode::EscapeState es = PointsToNode::NoEscape;
2067 bool scalar_replaceable = true;
2068 NOT_PRODUCT(const char* nsr_reason = "");
2069 if (call->is_AllocateArray()) {
2070 if (!kt->isa_aryklassptr()) { // StressReflectiveCode
2071 es = PointsToNode::GlobalEscape;
2072 } else {
2073 int length = call->in(AllocateNode::ALength)->find_int_con(-1);
2074 if (length < 0) {
2075 // Not scalar replaceable if the length is not constant.
2076 scalar_replaceable = false;
2077 NOT_PRODUCT(nsr_reason = "has a non-constant length");
2078 } else if (length > EliminateAllocationArraySizeLimit) {
2079 // Not scalar replaceable if the length is too big.
2080 scalar_replaceable = false;
2116 //
2117 // - all oop arguments are escaping globally;
2118 //
2119 // 2. CallStaticJavaNode (execute bytecode analysis if possible):
2120 //
2121 // - the same as CallDynamicJavaNode if can't do bytecode analysis;
2122 //
2123 // - mapped to GlobalEscape JavaObject node if unknown oop is returned;
2124 // - mapped to NoEscape JavaObject node if non-escaping object allocated
2125 // during call is returned;
2126 // - mapped to ArgEscape LocalVar node pointed to object arguments
2127 // which are returned and does not escape during call;
2128 //
2129 // - oop arguments escaping status is defined by bytecode analysis;
2130 //
2131 // For a static call, we know exactly what method is being called.
2132 // Use bytecode estimator to record whether the call's return value escapes.
2133 ciMethod* meth = call->as_CallJava()->method();
2134 if (meth == nullptr) {
2135 const char* name = call->as_CallStaticJava()->_name;
2136 assert(strncmp(name, "C2 Runtime multianewarray", 25) == 0 ||
2137 strncmp(name, "C2 Runtime load_unknown_inline", 30) == 0 ||
2138 strncmp(name, "store_inline_type_fields_to_buf", 31) == 0, "TODO: add failed case check");
2139 // Returns a newly allocated non-escaped object.
2140 add_java_object(call, PointsToNode::NoEscape);
2141 set_not_scalar_replaceable(ptnode_adr(call_idx) NOT_PRODUCT(COMMA "is result of multinewarray"));
2142 } else if (meth->is_boxing_method()) {
2143 // Returns boxing object
2144 PointsToNode::EscapeState es;
2145 vmIntrinsics::ID intr = meth->intrinsic_id();
2146 if (intr == vmIntrinsics::_floatValue || intr == vmIntrinsics::_doubleValue) {
2147 // It does not escape if object is always allocated.
2148 es = PointsToNode::NoEscape;
2149 } else {
2150 // It escapes globally if object could be loaded from cache.
2151 es = PointsToNode::GlobalEscape;
2152 }
2153 add_java_object(call, es);
2154 if (es == PointsToNode::GlobalEscape) {
2155 set_not_scalar_replaceable(ptnode_adr(call->_idx) NOT_PRODUCT(COMMA "object can be loaded from boxing cache"));
2156 }
2157 } else {
2158 BCEscapeAnalyzer* call_analyzer = meth->get_bcea();
2159 call_analyzer->copy_dependencies(_compile->dependencies());
2160 if (call_analyzer->is_return_allocated()) {
2161 // Returns a newly allocated non-escaped object, simply
2162 // update dependency information.
2163 // Mark it as NoEscape so that objects referenced by
2164 // it's fields will be marked as NoEscape at least.
2165 add_java_object(call, PointsToNode::NoEscape);
2166 set_not_scalar_replaceable(ptnode_adr(call_idx) NOT_PRODUCT(COMMA "is result of call"));
2167 } else {
2168 // Determine whether any arguments are returned.
2169 const TypeTuple* d = call->tf()->domain_cc();
2170 bool ret_arg = false;
2171 for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
2172 if (d->field_at(i)->isa_ptr() != nullptr &&
2173 call_analyzer->is_arg_returned(i - TypeFunc::Parms)) {
2174 ret_arg = true;
2175 break;
2176 }
2177 }
2178 if (ret_arg) {
2179 add_local_var(call, PointsToNode::ArgEscape);
2180 } else {
2181 // Returns unknown object.
2182 map_ideal_node(call, phantom_obj);
2183 }
2184 }
2185 }
2186 } else {
2187 // An other type of call, assume the worst case:
2188 // returned value is unknown and globally escapes.
2189 assert(call->Opcode() == Op_CallDynamicJava, "add failed case check");
2197 #ifdef ASSERT
2198 case Op_Allocate:
2199 case Op_AllocateArray:
2200 case Op_Lock:
2201 case Op_Unlock:
2202 assert(false, "should be done already");
2203 break;
2204 #endif
2205 case Op_ArrayCopy:
2206 case Op_CallLeafNoFP:
2207 // Most array copies are ArrayCopy nodes at this point but there
2208 // are still a few direct calls to the copy subroutines (See
2209 // PhaseStringOpts::copy_string())
2210 is_arraycopy = (call->Opcode() == Op_ArrayCopy) ||
2211 call->as_CallLeaf()->is_call_to_arraycopystub();
2212 // fall through
2213 case Op_CallLeafVector:
2214 case Op_CallLeaf: {
2215 // Stub calls, objects do not escape but they are not scale replaceable.
2216 // Adjust escape state for outgoing arguments.
2217 const TypeTuple * d = call->tf()->domain_sig();
2218 bool src_has_oops = false;
2219 for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
2220 const Type* at = d->field_at(i);
2221 Node *arg = call->in(i);
2222 if (arg == nullptr) {
2223 continue;
2224 }
2225 const Type *aat = _igvn->type(arg);
2226 if (arg->is_top() || !at->isa_ptr() || !aat->isa_ptr()) {
2227 continue;
2228 }
2229 if (arg->is_AddP()) {
2230 //
2231 // The inline_native_clone() case when the arraycopy stub is called
2232 // after the allocation before Initialize and CheckCastPP nodes.
2233 // Or normal arraycopy for object arrays case.
2234 //
2235 // Set AddP's base (Allocate) as not scalar replaceable since
2236 // pointer to the base (with offset) is passed as argument.
2237 //
2238 arg = get_addp_base(arg);
2239 }
2240 PointsToNode* arg_ptn = ptnode_adr(arg->_idx);
2241 assert(arg_ptn != nullptr, "should be registered");
2242 PointsToNode::EscapeState arg_esc = arg_ptn->escape_state();
2243 if (is_arraycopy || arg_esc < PointsToNode::ArgEscape) {
2244 assert(aat == Type::TOP || aat == TypePtr::NULL_PTR ||
2245 aat->isa_ptr() != nullptr, "expecting an Ptr");
2246 bool arg_has_oops = aat->isa_oopptr() &&
2247 (aat->isa_instptr() ||
2248 (aat->isa_aryptr() && (aat->isa_aryptr()->elem() == Type::BOTTOM || aat->isa_aryptr()->elem()->make_oopptr() != nullptr)) ||
2249 (aat->isa_aryptr() && aat->isa_aryptr()->elem() != nullptr &&
2250 aat->isa_aryptr()->is_flat() &&
2251 aat->isa_aryptr()->elem()->inline_klass()->contains_oops()));
2252 if (i == TypeFunc::Parms) {
2253 src_has_oops = arg_has_oops;
2254 }
2255 //
2256 // src or dst could be j.l.Object when other is basic type array:
2257 //
2258 // arraycopy(char[],0,Object*,0,size);
2259 // arraycopy(Object*,0,char[],0,size);
2260 //
2261 // Don't add edges in such cases.
2262 //
2263 bool arg_is_arraycopy_dest = src_has_oops && is_arraycopy &&
2264 arg_has_oops && (i > TypeFunc::Parms);
2265 #ifdef ASSERT
2266 if (!(is_arraycopy ||
2267 BarrierSet::barrier_set()->barrier_set_c2()->is_gc_barrier_node(call) ||
2268 (call->as_CallLeaf()->_name != nullptr &&
2269 (strcmp(call->as_CallLeaf()->_name, "updateBytesCRC32") == 0 ||
2270 strcmp(call->as_CallLeaf()->_name, "updateBytesCRC32C") == 0 ||
2271 strcmp(call->as_CallLeaf()->_name, "updateBytesAdler32") == 0 ||
2295 strcmp(call->as_CallLeaf()->_name, "dilithiumMontMulByConstant") == 0 ||
2296 strcmp(call->as_CallLeaf()->_name, "dilithiumDecomposePoly") == 0 ||
2297 strcmp(call->as_CallLeaf()->_name, "encodeBlock") == 0 ||
2298 strcmp(call->as_CallLeaf()->_name, "decodeBlock") == 0 ||
2299 strcmp(call->as_CallLeaf()->_name, "md5_implCompress") == 0 ||
2300 strcmp(call->as_CallLeaf()->_name, "md5_implCompressMB") == 0 ||
2301 strcmp(call->as_CallLeaf()->_name, "sha1_implCompress") == 0 ||
2302 strcmp(call->as_CallLeaf()->_name, "sha1_implCompressMB") == 0 ||
2303 strcmp(call->as_CallLeaf()->_name, "sha256_implCompress") == 0 ||
2304 strcmp(call->as_CallLeaf()->_name, "sha256_implCompressMB") == 0 ||
2305 strcmp(call->as_CallLeaf()->_name, "sha512_implCompress") == 0 ||
2306 strcmp(call->as_CallLeaf()->_name, "sha512_implCompressMB") == 0 ||
2307 strcmp(call->as_CallLeaf()->_name, "sha3_implCompress") == 0 ||
2308 strcmp(call->as_CallLeaf()->_name, "double_keccak") == 0 ||
2309 strcmp(call->as_CallLeaf()->_name, "sha3_implCompressMB") == 0 ||
2310 strcmp(call->as_CallLeaf()->_name, "multiplyToLen") == 0 ||
2311 strcmp(call->as_CallLeaf()->_name, "squareToLen") == 0 ||
2312 strcmp(call->as_CallLeaf()->_name, "mulAdd") == 0 ||
2313 strcmp(call->as_CallLeaf()->_name, "montgomery_multiply") == 0 ||
2314 strcmp(call->as_CallLeaf()->_name, "montgomery_square") == 0 ||
2315 strcmp(call->as_CallLeaf()->_name, "vectorizedMismatch") == 0 ||
2316 strcmp(call->as_CallLeaf()->_name, "load_unknown_inline") == 0 ||
2317 strcmp(call->as_CallLeaf()->_name, "store_unknown_inline") == 0 ||
2318 strcmp(call->as_CallLeaf()->_name, "store_inline_type_fields_to_buf") == 0 ||
2319 strcmp(call->as_CallLeaf()->_name, "bigIntegerRightShiftWorker") == 0 ||
2320 strcmp(call->as_CallLeaf()->_name, "bigIntegerLeftShiftWorker") == 0 ||
2321 strcmp(call->as_CallLeaf()->_name, "vectorizedMismatch") == 0 ||
2322 strcmp(call->as_CallLeaf()->_name, "stringIndexOf") == 0 ||
2323 strcmp(call->as_CallLeaf()->_name, "arraysort_stub") == 0 ||
2324 strcmp(call->as_CallLeaf()->_name, "array_partition_stub") == 0 ||
2325 strcmp(call->as_CallLeaf()->_name, "get_class_id_intrinsic") == 0 ||
2326 strcmp(call->as_CallLeaf()->_name, "unsafe_setmemory") == 0)
2327 ))) {
2328 call->dump();
2329 fatal("EA unexpected CallLeaf %s", call->as_CallLeaf()->_name);
2330 }
2331 #endif
2332 // Always process arraycopy's destination object since
2333 // we need to add all possible edges to references in
2334 // source object.
2335 if (arg_esc >= PointsToNode::ArgEscape &&
2336 !arg_is_arraycopy_dest) {
2337 continue;
2338 }
2361 }
2362 }
2363 }
2364 break;
2365 }
2366 case Op_CallStaticJava: {
2367 // For a static call, we know exactly what method is being called.
2368 // Use bytecode estimator to record the call's escape affects
2369 #ifdef ASSERT
2370 const char* name = call->as_CallStaticJava()->_name;
2371 assert((name == nullptr || strcmp(name, "uncommon_trap") != 0), "normal calls only");
2372 #endif
2373 ciMethod* meth = call->as_CallJava()->method();
2374 if ((meth != nullptr) && meth->is_boxing_method()) {
2375 break; // Boxing methods do not modify any oops.
2376 }
2377 BCEscapeAnalyzer* call_analyzer = (meth !=nullptr) ? meth->get_bcea() : nullptr;
2378 // fall-through if not a Java method or no analyzer information
2379 if (call_analyzer != nullptr) {
2380 PointsToNode* call_ptn = ptnode_adr(call->_idx);
2381 const TypeTuple* d = call->tf()->domain_cc();
2382 for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
2383 const Type* at = d->field_at(i);
2384 int k = i - TypeFunc::Parms;
2385 Node* arg = call->in(i);
2386 PointsToNode* arg_ptn = ptnode_adr(arg->_idx);
2387 if (at->isa_ptr() != nullptr &&
2388 call_analyzer->is_arg_returned(k)) {
2389 // The call returns arguments.
2390 if (call_ptn != nullptr) { // Is call's result used?
2391 assert(call_ptn->is_LocalVar(), "node should be registered");
2392 assert(arg_ptn != nullptr, "node should be registered");
2393 add_edge(call_ptn, arg_ptn);
2394 }
2395 }
2396 if (at->isa_oopptr() != nullptr &&
2397 arg_ptn->escape_state() < PointsToNode::GlobalEscape) {
2398 if (!call_analyzer->is_arg_stack(k)) {
2399 // The argument global escapes
2400 set_escape_state(arg_ptn, PointsToNode::GlobalEscape NOT_PRODUCT(COMMA trace_arg_escape_message(call)));
2401 } else {
2405 set_fields_escape_state(arg_ptn, PointsToNode::GlobalEscape NOT_PRODUCT(COMMA trace_arg_escape_message(call)));
2406 }
2407 }
2408 }
2409 }
2410 if (call_ptn != nullptr && call_ptn->is_LocalVar()) {
2411 // The call returns arguments.
2412 assert(call_ptn->edge_count() > 0, "sanity");
2413 if (!call_analyzer->is_return_local()) {
2414 // Returns also unknown object.
2415 add_edge(call_ptn, phantom_obj);
2416 }
2417 }
2418 break;
2419 }
2420 }
2421 default: {
2422 // Fall-through here if not a Java method or no analyzer information
2423 // or some other type of call, assume the worst case: all arguments
2424 // globally escape.
2425 const TypeTuple* d = call->tf()->domain_cc();
2426 for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
2427 const Type* at = d->field_at(i);
2428 if (at->isa_oopptr() != nullptr) {
2429 Node* arg = call->in(i);
2430 if (arg->is_AddP()) {
2431 arg = get_addp_base(arg);
2432 }
2433 assert(ptnode_adr(arg->_idx) != nullptr, "should be defined already");
2434 set_escape_state(ptnode_adr(arg->_idx), PointsToNode::GlobalEscape NOT_PRODUCT(COMMA trace_arg_escape_message(call)));
2435 }
2436 }
2437 }
2438 }
2439 }
2440
2441
2442 // Finish Graph construction.
2443 bool ConnectionGraph::complete_connection_graph(
2444 GrowableArray<PointsToNode*>& ptnodes_worklist,
2445 GrowableArray<JavaObjectNode*>& non_escaped_allocs_worklist,
2818 PointsToNode* base = i.get();
2819 if (base->is_JavaObject()) {
2820 // Skip Allocate's fields which will be processed later.
2821 if (base->ideal_node()->is_Allocate()) {
2822 return 0;
2823 }
2824 assert(base == null_obj, "only null ptr base expected here");
2825 }
2826 }
2827 if (add_edge(field, phantom_obj)) {
2828 // New edge was added
2829 new_edges++;
2830 add_field_uses_to_worklist(field);
2831 }
2832 return new_edges;
2833 }
2834
2835 // Find fields initializing values for allocations.
2836 int ConnectionGraph::find_init_values_phantom(JavaObjectNode* pta) {
2837 assert(pta->escape_state() == PointsToNode::NoEscape, "Not escaped Allocate nodes only");
2838 PointsToNode* init_val = phantom_obj;
2839 Node* alloc = pta->ideal_node();
2840
2841 // Do nothing for Allocate nodes since its fields values are
2842 // "known" unless they are initialized by arraycopy/clone.
2843 if (alloc->is_Allocate() && !pta->arraycopy_dst()) {
2844 if (alloc->as_Allocate()->in(AllocateNode::InitValue) != nullptr) {
2845 // Null-free inline type arrays are initialized with an init value instead of null
2846 init_val = ptnode_adr(alloc->as_Allocate()->in(AllocateNode::InitValue)->_idx);
2847 assert(init_val != nullptr, "init value should be registered");
2848 } else {
2849 return 0;
2850 }
2851 }
2852 // Non-escaped allocation returned from Java or runtime call has unknown values in fields.
2853 assert(pta->arraycopy_dst() || alloc->is_CallStaticJava() || init_val != phantom_obj, "sanity");
2854 #ifdef ASSERT
2855 if (alloc->is_CallStaticJava() && alloc->as_CallStaticJava()->method() == nullptr) {
2856 const char* name = alloc->as_CallStaticJava()->_name;
2857 assert(strncmp(name, "C2 Runtime multianewarray", 25) == 0 ||
2858 strncmp(name, "C2 Runtime load_unknown_inline", 30) == 0 ||
2859 strncmp(name, "store_inline_type_fields_to_buf", 31) == 0, "sanity");
2860 }
2861 #endif
2862 // Non-escaped allocation returned from Java or runtime call have unknown values in fields.
2863 int new_edges = 0;
2864 for (EdgeIterator i(pta); i.has_next(); i.next()) {
2865 PointsToNode* field = i.get();
2866 if (field->is_Field() && field->as_Field()->is_oop()) {
2867 if (add_edge(field, init_val)) {
2868 // New edge was added
2869 new_edges++;
2870 add_field_uses_to_worklist(field->as_Field());
2871 }
2872 }
2873 }
2874 return new_edges;
2875 }
2876
2877 // Find fields initializing values for allocations.
2878 int ConnectionGraph::find_init_values_null(JavaObjectNode* pta, PhaseValues* phase) {
2879 assert(pta->escape_state() == PointsToNode::NoEscape, "Not escaped Allocate nodes only");
2880 Node* alloc = pta->ideal_node();
2881 // Do nothing for Call nodes since its fields values are unknown.
2882 if (!alloc->is_Allocate() || alloc->as_Allocate()->in(AllocateNode::InitValue) != nullptr) {
2883 return 0;
2884 }
2885 InitializeNode* ini = alloc->as_Allocate()->initialization();
2886 bool visited_bottom_offset = false;
2887 GrowableArray<int> offsets_worklist;
2888 int new_edges = 0;
2889
2890 // Check if an oop field's initializing value is recorded and add
2891 // a corresponding null if field's value if it is not recorded.
2892 // Connection Graph does not record a default initialization by null
2893 // captured by Initialize node.
2894 //
2895 for (EdgeIterator i(pta); i.has_next(); i.next()) {
2896 PointsToNode* field = i.get(); // Field (AddP)
2897 if (!field->is_Field() || !field->as_Field()->is_oop()) {
2898 continue; // Not oop field
2899 }
2900 int offset = field->as_Field()->offset();
2901 if (offset == Type::OffsetBot) {
2902 if (!visited_bottom_offset) {
2948 } else {
2949 if (!val->is_LocalVar() || (val->edge_count() == 0)) {
2950 tty->print_cr("----------init store has invalid value -----");
2951 store->dump();
2952 val->dump();
2953 assert(val->is_LocalVar() && (val->edge_count() > 0), "should be processed already");
2954 }
2955 for (EdgeIterator j(val); j.has_next(); j.next()) {
2956 PointsToNode* obj = j.get();
2957 if (obj->is_JavaObject()) {
2958 if (!field->points_to(obj->as_JavaObject())) {
2959 missed_obj = obj;
2960 break;
2961 }
2962 }
2963 }
2964 }
2965 if (missed_obj != nullptr) {
2966 tty->print_cr("----------field---------------------------------");
2967 field->dump();
2968 tty->print_cr("----------missed reference to object------------");
2969 missed_obj->dump();
2970 tty->print_cr("----------object referenced by init store-------");
2971 store->dump();
2972 val->dump();
2973 assert(!field->points_to(missed_obj->as_JavaObject()), "missed JavaObject reference");
2974 }
2975 }
2976 #endif
2977 } else {
2978 // There could be initializing stores which follow allocation.
2979 // For example, a volatile field store is not collected
2980 // by Initialize node.
2981 //
2982 // Need to check for dependent loads to separate such stores from
2983 // stores which follow loads. For now, add initial value null so
2984 // that compare pointers optimization works correctly.
2985 }
2986 }
2987 if (value == nullptr) {
2988 // A field's initializing value was not recorded. Add null.
2989 if (add_edge(field, null_obj)) {
2990 // New edge was added
3314 assert(field->edge_count() > 0, "sanity");
3315 }
3316 }
3317 }
3318 }
3319 #endif
3320
3321 // Optimize ideal graph.
3322 void ConnectionGraph::optimize_ideal_graph(GrowableArray<Node*>& ptr_cmp_worklist,
3323 GrowableArray<MemBarStoreStoreNode*>& storestore_worklist) {
3324 Compile* C = _compile;
3325 PhaseIterGVN* igvn = _igvn;
3326 if (EliminateLocks) {
3327 // Mark locks before changing ideal graph.
3328 int cnt = C->macro_count();
3329 for (int i = 0; i < cnt; i++) {
3330 Node *n = C->macro_node(i);
3331 if (n->is_AbstractLock()) { // Lock and Unlock nodes
3332 AbstractLockNode* alock = n->as_AbstractLock();
3333 if (!alock->is_non_esc_obj()) {
3334 const Type* obj_type = igvn->type(alock->obj_node());
3335 if (can_eliminate_lock(alock) && !obj_type->is_inlinetypeptr()) {
3336 assert(!alock->is_eliminated() || alock->is_coarsened(), "sanity");
3337 // The lock could be marked eliminated by lock coarsening
3338 // code during first IGVN before EA. Replace coarsened flag
3339 // to eliminate all associated locks/unlocks.
3340 #ifdef ASSERT
3341 alock->log_lock_optimization(C, "eliminate_lock_set_non_esc3");
3342 #endif
3343 alock->set_non_esc_obj();
3344 }
3345 }
3346 }
3347 }
3348 }
3349
3350 if (OptimizePtrCompare) {
3351 for (int i = 0; i < ptr_cmp_worklist.length(); i++) {
3352 Node *n = ptr_cmp_worklist.at(i);
3353 assert(n->Opcode() == Op_CmpN || n->Opcode() == Op_CmpP, "must be");
3354 const TypeInt* tcmp = optimize_ptr_compare(n->in(1), n->in(2));
3355 if (tcmp->singleton()) {
3357 #ifndef PRODUCT
3358 if (PrintOptimizePtrCompare) {
3359 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"));
3360 if (Verbose) {
3361 n->dump(1);
3362 }
3363 }
3364 #endif
3365 igvn->replace_node(n, cmp);
3366 }
3367 }
3368 }
3369
3370 // For MemBarStoreStore nodes added in library_call.cpp, check
3371 // escape status of associated AllocateNode and optimize out
3372 // MemBarStoreStore node if the allocated object never escapes.
3373 for (int i = 0; i < storestore_worklist.length(); i++) {
3374 Node* storestore = storestore_worklist.at(i);
3375 Node* alloc = storestore->in(MemBarNode::Precedent)->in(0);
3376 if (alloc->is_Allocate() && not_global_escape(alloc)) {
3377 if (alloc->in(AllocateNode::InlineType) != nullptr) {
3378 // Non-escaping inline type buffer allocations don't require a membar
3379 storestore->as_MemBar()->remove(_igvn);
3380 } else {
3381 MemBarNode* mb = MemBarNode::make(C, Op_MemBarCPUOrder, Compile::AliasIdxBot);
3382 mb->init_req(TypeFunc::Memory, storestore->in(TypeFunc::Memory));
3383 mb->init_req(TypeFunc::Control, storestore->in(TypeFunc::Control));
3384 igvn->register_new_node_with_optimizer(mb);
3385 igvn->replace_node(storestore, mb);
3386 }
3387 }
3388 }
3389 }
3390
3391 // Atomic flat accesses on non-escaping objects can be optimized to non-atomic accesses
3392 void ConnectionGraph::optimize_flat_accesses(GrowableArray<SafePointNode*>& sfn_worklist) {
3393 PhaseIterGVN& igvn = *_igvn;
3394 bool delay = igvn.delay_transform();
3395 igvn.set_delay_transform(true);
3396 igvn.C->for_each_flat_access([&](Node* n) {
3397 Node* base = n->is_LoadFlat() ? n->as_LoadFlat()->base() : n->as_StoreFlat()->base();
3398 if (!not_global_escape(base)) {
3399 return;
3400 }
3401
3402 bool expanded;
3403 if (n->is_LoadFlat()) {
3404 expanded = n->as_LoadFlat()->expand_non_atomic(igvn);
3405 } else {
3406 expanded = n->as_StoreFlat()->expand_non_atomic(igvn);
3407 }
3408 if (expanded) {
3409 sfn_worklist.remove(n->as_SafePoint());
3410 igvn.C->remove_flat_access(n);
3411 }
3412 });
3413 igvn.set_delay_transform(delay);
3414 }
3415
3416 // Optimize objects compare.
3417 const TypeInt* ConnectionGraph::optimize_ptr_compare(Node* left, Node* right) {
3418 const TypeInt* UNKNOWN = TypeInt::CC; // [-1, 0,1]
3419 if (!OptimizePtrCompare) {
3420 return UNKNOWN;
3421 }
3422 const TypeInt* EQ = TypeInt::CC_EQ; // [0] == ZERO
3423 const TypeInt* NE = TypeInt::CC_GT; // [1] == ONE
3424
3425 PointsToNode* ptn1 = ptnode_adr(left->_idx);
3426 PointsToNode* ptn2 = ptnode_adr(right->_idx);
3427 JavaObjectNode* jobj1 = unique_java_object(left);
3428 JavaObjectNode* jobj2 = unique_java_object(right);
3429
3430 // The use of this method during allocation merge reduction may cause 'left'
3431 // or 'right' be something (e.g., a Phi) that isn't in the connection graph or
3432 // that doesn't reference an unique java object.
3433 if (ptn1 == nullptr || ptn2 == nullptr ||
3434 jobj1 == nullptr || jobj2 == nullptr) {
3435 return UNKNOWN;
3555 assert(!src->is_Field() && !dst->is_Field(), "only for JavaObject and LocalVar");
3556 assert((src != null_obj) && (dst != null_obj), "not for ConP null");
3557 PointsToNode* ptadr = _nodes.at(n->_idx);
3558 if (ptadr != nullptr) {
3559 assert(ptadr->is_Arraycopy() && ptadr->ideal_node() == n, "sanity");
3560 return;
3561 }
3562 Compile* C = _compile;
3563 ptadr = new (C->comp_arena()) ArraycopyNode(this, n, es);
3564 map_ideal_node(n, ptadr);
3565 // Add edge from arraycopy node to source object.
3566 (void)add_edge(ptadr, src);
3567 src->set_arraycopy_src();
3568 // Add edge from destination object to arraycopy node.
3569 (void)add_edge(dst, ptadr);
3570 dst->set_arraycopy_dst();
3571 }
3572
3573 bool ConnectionGraph::is_oop_field(Node* n, int offset, bool* unsafe) {
3574 const Type* adr_type = n->as_AddP()->bottom_type();
3575 int field_offset = adr_type->isa_aryptr() ? adr_type->isa_aryptr()->field_offset().get() : Type::OffsetBot;
3576 BasicType bt = T_INT;
3577 if (offset == Type::OffsetBot && field_offset == Type::OffsetBot) {
3578 // Check only oop fields.
3579 if (!adr_type->isa_aryptr() ||
3580 adr_type->isa_aryptr()->elem() == Type::BOTTOM ||
3581 adr_type->isa_aryptr()->elem()->make_oopptr() != nullptr) {
3582 // OffsetBot is used to reference array's element. Ignore first AddP.
3583 if (find_second_addp(n, n->in(AddPNode::Base)) == nullptr) {
3584 bt = T_OBJECT;
3585 }
3586 }
3587 } else if (offset != oopDesc::klass_offset_in_bytes()) {
3588 if (adr_type->isa_instptr()) {
3589 ciField* field = _compile->alias_type(adr_type->is_ptr())->field();
3590 if (field != nullptr) {
3591 bt = field->layout_type();
3592 } else {
3593 // Check for unsafe oop field access
3594 if (n->has_out_with(Op_StoreP, Op_LoadP, Op_StoreN, Op_LoadN) ||
3595 n->has_out_with(Op_GetAndSetP, Op_GetAndSetN, Op_CompareAndExchangeP, Op_CompareAndExchangeN) ||
3596 n->has_out_with(Op_CompareAndSwapP, Op_CompareAndSwapN, Op_WeakCompareAndSwapP, Op_WeakCompareAndSwapN) ||
3597 BarrierSet::barrier_set()->barrier_set_c2()->escape_has_out_with_unsafe_object(n)) {
3598 bt = T_OBJECT;
3599 (*unsafe) = true;
3600 }
3601 }
3602 } else if (adr_type->isa_aryptr()) {
3603 if (offset == arrayOopDesc::length_offset_in_bytes()) {
3604 // Ignore array length load.
3605 } else if (find_second_addp(n, n->in(AddPNode::Base)) != nullptr) {
3606 // Ignore first AddP.
3607 } else {
3608 const Type* elemtype = adr_type->is_aryptr()->elem();
3609 if (adr_type->is_aryptr()->is_flat() && field_offset != Type::OffsetBot) {
3610 ciInlineKlass* vk = elemtype->inline_klass();
3611 field_offset += vk->payload_offset();
3612 ciField* field = vk->get_field_by_offset(field_offset, false);
3613 if (field != nullptr) {
3614 bt = field->layout_type();
3615 } else {
3616 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);
3617 bt = T_BOOLEAN;
3618 }
3619 } else {
3620 bt = elemtype->array_element_basic_type();
3621 }
3622 }
3623 } else if (adr_type->isa_rawptr() || adr_type->isa_klassptr()) {
3624 // Allocation initialization, ThreadLocal field access, unsafe access
3625 if (n->has_out_with(Op_StoreP, Op_LoadP, Op_StoreN, Op_LoadN) ||
3626 n->has_out_with(Op_GetAndSetP, Op_GetAndSetN, Op_CompareAndExchangeP, Op_CompareAndExchangeN) ||
3627 n->has_out_with(Op_CompareAndSwapP, Op_CompareAndSwapN, Op_WeakCompareAndSwapP, Op_WeakCompareAndSwapN) ||
3628 BarrierSet::barrier_set()->barrier_set_c2()->escape_has_out_with_unsafe_object(n)) {
3629 bt = T_OBJECT;
3630 }
3631 }
3632 }
3633 // Note: T_NARROWOOP is not classed as a real reference type
3634 return (is_reference_type(bt) || bt == T_NARROWOOP);
3635 }
3636
3637 // Returns unique pointed java object or null.
3638 JavaObjectNode* ConnectionGraph::unique_java_object(Node *n) const {
3639 // If the node was created after the escape computation we can't answer.
3640 uint idx = n->_idx;
3641 if (idx >= nodes_size()) {
3798 return true;
3799 }
3800 }
3801 }
3802 }
3803 }
3804 return false;
3805 }
3806
3807 int ConnectionGraph::address_offset(Node* adr, PhaseValues* phase) {
3808 const Type *adr_type = phase->type(adr);
3809 if (adr->is_AddP() && adr_type->isa_oopptr() == nullptr && is_captured_store_address(adr)) {
3810 // We are computing a raw address for a store captured by an Initialize
3811 // compute an appropriate address type. AddP cases #3 and #5 (see below).
3812 int offs = (int)phase->find_intptr_t_con(adr->in(AddPNode::Offset), Type::OffsetBot);
3813 assert(offs != Type::OffsetBot ||
3814 adr->in(AddPNode::Address)->in(0)->is_AllocateArray(),
3815 "offset must be a constant or it is initialization of array");
3816 return offs;
3817 }
3818 return adr_type->is_ptr()->flat_offset();
3819 }
3820
3821 Node* ConnectionGraph::get_addp_base(Node *addp) {
3822 assert(addp->is_AddP(), "must be AddP");
3823 //
3824 // AddP cases for Base and Address inputs:
3825 // case #1. Direct object's field reference:
3826 // Allocate
3827 // |
3828 // Proj #5 ( oop result )
3829 // |
3830 // CheckCastPP (cast to instance type)
3831 // | |
3832 // AddP ( base == address )
3833 //
3834 // case #2. Indirect object's field reference:
3835 // Phi
3836 // |
3837 // CastPP (cast to instance type)
3838 // | |
3952 }
3953 return nullptr;
3954 }
3955
3956 //
3957 // Adjust the type and inputs of an AddP which computes the
3958 // address of a field of an instance
3959 //
3960 bool ConnectionGraph::split_AddP(Node *addp, Node *base) {
3961 PhaseGVN* igvn = _igvn;
3962 const TypeOopPtr *base_t = igvn->type(base)->isa_oopptr();
3963 assert(base_t != nullptr && base_t->is_known_instance(), "expecting instance oopptr");
3964 const TypeOopPtr *t = igvn->type(addp)->isa_oopptr();
3965 if (t == nullptr) {
3966 // We are computing a raw address for a store captured by an Initialize
3967 // compute an appropriate address type (cases #3 and #5).
3968 assert(igvn->type(addp) == TypeRawPtr::NOTNULL, "must be raw pointer");
3969 assert(addp->in(AddPNode::Address)->is_Proj(), "base of raw address must be result projection from allocation");
3970 intptr_t offs = (int)igvn->find_intptr_t_con(addp->in(AddPNode::Offset), Type::OffsetBot);
3971 assert(offs != Type::OffsetBot, "offset must be a constant");
3972 if (base_t->isa_aryptr() != nullptr) {
3973 // In the case of a flat inline type array, each field has its
3974 // own slice so we need to extract the field being accessed from
3975 // the address computation
3976 t = base_t->isa_aryptr()->add_field_offset_and_offset(offs)->is_oopptr();
3977 } else {
3978 t = base_t->add_offset(offs)->is_oopptr();
3979 }
3980 }
3981 int inst_id = base_t->instance_id();
3982 assert(!t->is_known_instance() || t->instance_id() == inst_id,
3983 "old type must be non-instance or match new type");
3984
3985 // The type 't' could be subclass of 'base_t'.
3986 // As result t->offset() could be large then base_t's size and it will
3987 // cause the failure in add_offset() with narrow oops since TypeOopPtr()
3988 // constructor verifies correctness of the offset.
3989 //
3990 // It could happened on subclass's branch (from the type profiling
3991 // inlining) which was not eliminated during parsing since the exactness
3992 // of the allocation type was not propagated to the subclass type check.
3993 //
3994 // Or the type 't' could be not related to 'base_t' at all.
3995 // It could happen when CHA type is different from MDO type on a dead path
3996 // (for example, from instanceof check) which is not collapsed during parsing.
3997 //
3998 // Do nothing for such AddP node and don't process its users since
3999 // this code branch will go away.
4000 //
4001 if (!t->is_known_instance() &&
4002 !base_t->maybe_java_subtype_of(t)) {
4003 return false; // bail out
4004 }
4005 const TypePtr* tinst = base_t->add_offset(t->offset());
4006 if (tinst->isa_aryptr() && t->isa_aryptr()) {
4007 // In the case of a flat inline type array, each field has its
4008 // own slice so we need to keep track of the field being accessed.
4009 tinst = tinst->is_aryptr()->with_field_offset(t->is_aryptr()->field_offset().get());
4010 // Keep array properties (not flat/null-free)
4011 tinst = tinst->is_aryptr()->update_properties(t->is_aryptr());
4012 if (tinst == nullptr) {
4013 return false; // Skip dead path with inconsistent properties
4014 }
4015 }
4016
4017 // Do NOT remove the next line: ensure a new alias index is allocated
4018 // for the instance type. Note: C++ will not remove it since the call
4019 // has side effect.
4020 int alias_idx = _compile->get_alias_index(tinst);
4021 igvn->set_type(addp, tinst);
4022 // record the allocation in the node map
4023 set_map(addp, get_map(base->_idx));
4024 // Set addp's Base and Address to 'base'.
4025 Node *abase = addp->in(AddPNode::Base);
4026 Node *adr = addp->in(AddPNode::Address);
4027 if (adr->is_Proj() && adr->in(0)->is_Allocate() &&
4028 adr->in(0)->_idx == (uint)inst_id) {
4029 // Skip AddP cases #3 and #5.
4030 } else {
4031 assert(!abase->is_top(), "sanity"); // AddP case #3
4032 if (abase != base) {
4033 igvn->hash_delete(addp);
4034 addp->set_req(AddPNode::Base, base);
4035 if (abase == adr) {
4036 addp->set_req(AddPNode::Address, base);
4702 ptnode_adr(n->_idx)->dump();
4703 assert(jobj != nullptr && jobj != phantom_obj, "escaped allocation");
4704 #endif
4705 _compile->record_failure(_invocation > 0 ? C2Compiler::retry_no_iterative_escape_analysis() : C2Compiler::retry_no_escape_analysis());
4706 return;
4707 } else {
4708 Node *val = get_map(jobj->idx()); // CheckCastPP node
4709 TypeNode *tn = n->as_Type();
4710 const TypeOopPtr* tinst = igvn->type(val)->isa_oopptr();
4711 assert(tinst != nullptr && tinst->is_known_instance() &&
4712 tinst->instance_id() == jobj->idx() , "instance type expected.");
4713
4714 const Type *tn_type = igvn->type(tn);
4715 const TypeOopPtr *tn_t;
4716 if (tn_type->isa_narrowoop()) {
4717 tn_t = tn_type->make_ptr()->isa_oopptr();
4718 } else {
4719 tn_t = tn_type->isa_oopptr();
4720 }
4721 if (tn_t != nullptr && tinst->maybe_java_subtype_of(tn_t)) {
4722 if (tn_t->isa_aryptr()) {
4723 // Keep array properties (not flat/null-free)
4724 tinst = tinst->is_aryptr()->update_properties(tn_t->is_aryptr());
4725 if (tinst == nullptr) {
4726 continue; // Skip dead path with inconsistent properties
4727 }
4728 }
4729 if (tn_type->isa_narrowoop()) {
4730 tn_type = tinst->make_narrowoop();
4731 } else {
4732 tn_type = tinst;
4733 }
4734 igvn->hash_delete(tn);
4735 igvn->set_type(tn, tn_type);
4736 tn->set_type(tn_type);
4737 igvn->hash_insert(tn);
4738 record_for_optimizer(n);
4739 } else {
4740 assert(tn_type == TypePtr::NULL_PTR ||
4741 (tn_t != nullptr && !tinst->maybe_java_subtype_of(tn_t)),
4742 "unexpected type");
4743 continue; // Skip dead path with different type
4744 }
4745 }
4746 } else {
4747 DEBUG_ONLY(n->dump();)
4748 assert(false, "EA: unexpected node");
4749 continue;
4750 }
4751 // push allocation's users on appropriate worklist
4752 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
4753 Node *use = n->fast_out(i);
4754 if (use->is_Mem() && use->in(MemNode::Address) == n) {
4755 // Load/store to instance's field
4756 memnode_worklist.append_if_missing(use);
4757 } else if (use->is_MemBar()) {
4758 if (use->in(TypeFunc::Memory) == n) { // Ignore precedent edge
4759 memnode_worklist.append_if_missing(use);
4760 }
4761 } else if (use->is_AddP() && use->outcnt() > 0) { // No dead nodes
4762 Node* addp2 = find_second_addp(use, n);
4763 if (addp2 != nullptr) {
4764 alloc_worklist.append_if_missing(addp2);
4765 }
4766 alloc_worklist.append_if_missing(use);
4767 } else if (use->is_Phi() ||
4768 use->is_CheckCastPP() ||
4769 use->is_EncodeNarrowPtr() ||
4770 use->is_DecodeNarrowPtr() ||
4771 (use->is_ConstraintCast() && use->Opcode() == Op_CastPP)) {
4772 alloc_worklist.append_if_missing(use);
4773 #ifdef ASSERT
4774 } else if (use->is_Mem()) {
4775 assert(use->in(MemNode::Address) != n, "EA: missing allocation reference path");
4776 } else if (use->is_MergeMem()) {
4777 assert(mergemem_worklist.contains(use->as_MergeMem()), "EA: missing MergeMem node in the worklist");
4778 } else if (use->is_SafePoint()) {
4779 // Look for MergeMem nodes for calls which reference unique allocation
4780 // (through CheckCastPP nodes) even for debug info.
4781 Node* m = use->in(TypeFunc::Memory);
4782 if (m->is_MergeMem()) {
4783 assert(mergemem_worklist.contains(m->as_MergeMem()), "EA: missing MergeMem node in the worklist");
4784 }
4785 } else if (use->Opcode() == Op_EncodeISOArray) {
4786 if (use->in(MemNode::Memory) == n || use->in(3) == n) {
4787 // EncodeISOArray overwrites destination array
4788 memnode_worklist.append_if_missing(use);
4789 }
4790 } else if (use->Opcode() == Op_Return) {
4791 // Allocation is referenced by field of returned inline type
4792 assert(_compile->tf()->returns_inline_type_as_fields(), "EA: unexpected reference by ReturnNode");
4793 } else {
4794 uint op = use->Opcode();
4795 if ((op == Op_StrCompressedCopy || op == Op_StrInflatedCopy) &&
4796 (use->in(MemNode::Memory) == n)) {
4797 // They overwrite memory edge corresponding to destination array,
4798 memnode_worklist.append_if_missing(use);
4799 } else if (!(op == Op_CmpP || op == Op_Conv2B ||
4800 op == Op_CastP2X ||
4801 op == Op_FastLock || op == Op_AryEq ||
4802 op == Op_StrComp || op == Op_CountPositives ||
4803 op == Op_StrCompressedCopy || op == Op_StrInflatedCopy ||
4804 op == Op_StrEquals || op == Op_VectorizedHashCode ||
4805 op == Op_StrIndexOf || op == Op_StrIndexOfChar ||
4806 op == Op_SubTypeCheck || op == Op_InlineType || op == Op_FlatArrayCheck ||
4807 op == Op_ReinterpretS2HF ||
4808 BarrierSet::barrier_set()->barrier_set_c2()->is_gc_barrier_node(use))) {
4809 n->dump();
4810 use->dump();
4811 assert(false, "EA: missing allocation reference path");
4812 }
4813 #endif
4814 }
4815 }
4816
4817 }
4818
4819 #ifdef ASSERT
4820 if (VerifyReduceAllocationMerges) {
4821 for (uint i = 0; i < reducible_merges.size(); i++) {
4822 Node* phi = reducible_merges.at(i);
4823
4824 if (!reduced_merges.member(phi)) {
4825 phi->dump(2);
4826 phi->dump(-2);
4890 // we don't need to do anything, but the users must be pushed
4891 n = n->as_MemBar()->proj_out_or_null(TypeFunc::Memory);
4892 if (n == nullptr) {
4893 continue;
4894 }
4895 } else if (n->is_CallLeaf()) {
4896 // Runtime calls with narrow memory input (no MergeMem node)
4897 // get the memory projection
4898 n = n->as_Call()->proj_out_or_null(TypeFunc::Memory);
4899 if (n == nullptr) {
4900 continue;
4901 }
4902 } else if (n->Opcode() == Op_StrInflatedCopy) {
4903 // Check direct uses of StrInflatedCopy.
4904 // It is memory type Node - no special SCMemProj node.
4905 } else if (n->Opcode() == Op_StrCompressedCopy ||
4906 n->Opcode() == Op_EncodeISOArray) {
4907 // get the memory projection
4908 n = n->find_out_with(Op_SCMemProj);
4909 assert(n != nullptr && n->Opcode() == Op_SCMemProj, "memory projection required");
4910 } else if (n->is_CallLeaf() && n->as_CallLeaf()->_name != nullptr &&
4911 strcmp(n->as_CallLeaf()->_name, "store_unknown_inline") == 0) {
4912 n = n->as_CallLeaf()->proj_out(TypeFunc::Memory);
4913 } else {
4914 #ifdef ASSERT
4915 if (!n->is_Mem()) {
4916 n->dump();
4917 }
4918 assert(n->is_Mem(), "memory node required.");
4919 #endif
4920 Node *addr = n->in(MemNode::Address);
4921 const Type *addr_t = igvn->type(addr);
4922 if (addr_t == Type::TOP) {
4923 continue;
4924 }
4925 assert (addr_t->isa_ptr() != nullptr, "pointer type required.");
4926 int alias_idx = _compile->get_alias_index(addr_t->is_ptr());
4927 assert ((uint)alias_idx < new_index_end, "wrong alias index");
4928 Node *mem = find_inst_mem(n->in(MemNode::Memory), alias_idx, orig_phis);
4929 if (_compile->failing()) {
4930 return;
4931 }
4932 if (mem != n->in(MemNode::Memory)) {
4937 if (n->is_Load()) {
4938 continue; // don't push users
4939 } else if (n->is_LoadStore()) {
4940 // get the memory projection
4941 n = n->find_out_with(Op_SCMemProj);
4942 assert(n != nullptr && n->Opcode() == Op_SCMemProj, "memory projection required");
4943 }
4944 }
4945 // push user on appropriate worklist
4946 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
4947 Node *use = n->fast_out(i);
4948 if (use->is_Phi() || use->is_ClearArray()) {
4949 memnode_worklist.append_if_missing(use);
4950 } else if (use->is_Mem() && use->in(MemNode::Memory) == n) {
4951 memnode_worklist.append_if_missing(use);
4952 } else if (use->is_MemBar() || use->is_CallLeaf()) {
4953 if (use->in(TypeFunc::Memory) == n) { // Ignore precedent edge
4954 memnode_worklist.append_if_missing(use);
4955 }
4956 #ifdef ASSERT
4957 } else if (use->is_Mem()) {
4958 assert(use->in(MemNode::Memory) != n, "EA: missing memory path");
4959 } else if (use->is_MergeMem()) {
4960 assert(mergemem_worklist.contains(use->as_MergeMem()), "EA: missing MergeMem node in the worklist");
4961 } else if (use->Opcode() == Op_EncodeISOArray) {
4962 if (use->in(MemNode::Memory) == n || use->in(3) == n) {
4963 // EncodeISOArray overwrites destination array
4964 memnode_worklist.append_if_missing(use);
4965 }
4966 } else if (use->is_CallLeaf() && use->as_CallLeaf()->_name != nullptr &&
4967 strcmp(use->as_CallLeaf()->_name, "store_unknown_inline") == 0) {
4968 // store_unknown_inline overwrites destination array
4969 memnode_worklist.append_if_missing(use);
4970 } else {
4971 uint op = use->Opcode();
4972 if ((use->in(MemNode::Memory) == n) &&
4973 (op == Op_StrCompressedCopy || op == Op_StrInflatedCopy)) {
4974 // They overwrite memory edge corresponding to destination array,
4975 memnode_worklist.append_if_missing(use);
4976 } else if (!(BarrierSet::barrier_set()->barrier_set_c2()->is_gc_barrier_node(use) ||
4977 op == Op_AryEq || op == Op_StrComp || op == Op_CountPositives ||
4978 op == Op_StrCompressedCopy || op == Op_StrInflatedCopy || op == Op_VectorizedHashCode ||
4979 op == Op_StrEquals || op == Op_StrIndexOf || op == Op_StrIndexOfChar || op == Op_FlatArrayCheck)) {
4980 n->dump();
4981 use->dump();
4982 assert(false, "EA: missing memory path");
4983 }
4984 #endif
4985 }
4986 }
4987 }
4988
4989 // Phase 3: Process MergeMem nodes from mergemem_worklist.
4990 // Walk each memory slice moving the first node encountered of each
4991 // instance type to the input corresponding to its alias index.
4992 uint length = mergemem_worklist.length();
4993 for( uint next = 0; next < length; ++next ) {
4994 MergeMemNode* nmm = mergemem_worklist.at(next);
4995 assert(!visited.test_set(nmm->_idx), "should not be visited before");
4996 // Note: we don't want to use MergeMemStream here because we only want to
4997 // scan inputs which exist at the start, not ones we add during processing.
4998 // Note 2: MergeMem may already contains instance memory slices added
4999 // during find_inst_mem() call when memory nodes were processed above.
5060 if (_compile->live_nodes() >= _compile->max_node_limit() * 0.75) {
5061 if (_compile->do_reduce_allocation_merges()) {
5062 _compile->record_failure(C2Compiler::retry_no_reduce_allocation_merges());
5063 } else if (_invocation > 0) {
5064 _compile->record_failure(C2Compiler::retry_no_iterative_escape_analysis());
5065 } else {
5066 _compile->record_failure(C2Compiler::retry_no_escape_analysis());
5067 }
5068 return;
5069 }
5070
5071 igvn->hash_insert(nmm);
5072 record_for_optimizer(nmm);
5073 }
5074
5075 // Phase 4: Update the inputs of non-instance memory Phis and
5076 // the Memory input of memnodes
5077 // First update the inputs of any non-instance Phi's from
5078 // which we split out an instance Phi. Note we don't have
5079 // to recursively process Phi's encountered on the input memory
5080 // chains as is done in split_memory_phi() since they will
5081 // also be processed here.
5082 for (int j = 0; j < orig_phis.length(); j++) {
5083 PhiNode *phi = orig_phis.at(j);
5084 int alias_idx = _compile->get_alias_index(phi->adr_type());
5085 igvn->hash_delete(phi);
5086 for (uint i = 1; i < phi->req(); i++) {
5087 Node *mem = phi->in(i);
5088 Node *new_mem = find_inst_mem(mem, alias_idx, orig_phis);
5089 if (_compile->failing()) {
5090 return;
5091 }
5092 if (mem != new_mem) {
5093 phi->set_req(i, new_mem);
5094 }
5095 }
5096 igvn->hash_insert(phi);
5097 record_for_optimizer(phi);
5098 }
5099
5100 // Update the memory inputs of MemNodes with the value we computed
|