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 "precompiled.hpp"
26 #include "gc/shared/barrierSet.hpp"
27 #include "gc/shared/c2/barrierSetC2.hpp"
28 #include "memory/allocation.inline.hpp"
29 #include "memory/resourceArea.hpp"
30 #include "opto/addnode.hpp"
31 #include "opto/callnode.hpp"
32 #include "opto/castnode.hpp"
33 #include "opto/connode.hpp"
34 #include "opto/castnode.hpp"
35 #include "opto/divnode.hpp"
36 #include "opto/loopnode.hpp"
37 #include "opto/matcher.hpp"
38 #include "opto/mulnode.hpp"
39 #include "opto/movenode.hpp"
40 #include "opto/opaquenode.hpp"
41 #include "opto/rootnode.hpp"
42 #include "opto/subnode.hpp"
43 #include "opto/subtypenode.hpp"
44 #include "opto/superword.hpp"
45 #include "opto/vectornode.hpp"
46 #include "utilities/macros.hpp"
47
48 //=============================================================================
49 //------------------------------split_thru_phi---------------------------------
50 // Split Node 'n' through merge point if there is enough win.
51 Node* PhaseIdealLoop::split_thru_phi(Node* n, Node* region, int policy) {
52 if ((n->Opcode() == Op_ConvI2L && n->bottom_type() != TypeLong::LONG) ||
53 (n->Opcode() == Op_ConvL2I && n->bottom_type() != TypeInt::INT)) {
54 // ConvI2L/ConvL2I may have type information on it which is unsafe to push up
55 // so disable this for now
56 return nullptr;
57 }
58
59 // Splitting range check CastIIs through a loop induction Phi can
60 // cause new Phis to be created that are left unrelated to the loop
61 // induction Phi and prevent optimizations (vectorization)
62 if (n->Opcode() == Op_CastII && region->is_CountedLoop() &&
63 n->in(1) == region->as_CountedLoop()->phi()) {
64 return nullptr;
65 }
66
67 if (cannot_split_division(n, region)) {
68 return nullptr;
69 }
70
71 int wins = 0;
72 assert(!n->is_CFG(), "");
73 assert(region->is_Region(), "");
74
75 const Type* type = n->bottom_type();
76 const TypeOopPtr* t_oop = _igvn.type(n)->isa_oopptr();
77 Node* phi;
78 if (t_oop != nullptr && t_oop->is_known_instance_field()) {
79 int iid = t_oop->instance_id();
80 int index = C->get_alias_index(t_oop);
81 int offset = t_oop->offset();
82 phi = new PhiNode(region, type, nullptr, iid, index, offset);
83 } else {
84 phi = PhiNode::make_blank(region, n);
85 }
86 uint old_unique = C->unique();
743 // CMOVE'd derived pointer? It's a CMOVE'd derived base. Thus
744 // CMOVE'ing a derived pointer requires we also CMOVE the base. If we
745 // have a Phi for the base here that we convert to a CMOVE all is well
746 // and good. But if the base is dead, we'll not make a CMOVE. Later
747 // the allocator will have to produce a base by creating a CMOVE of the
748 // relevant bases. This puts the allocator in the business of
749 // manufacturing expensive instructions, generally a bad plan.
750 // Just Say No to Conditionally-Moved Derived Pointers.
751 if (tp && tp->offset() != 0)
752 return nullptr;
753 cost++;
754 break;
755 }
756 default:
757 return nullptr; // In particular, can't do memory or I/O
758 }
759 // Add in cost any speculative ops
760 for (uint j = 1; j < region->req(); j++) {
761 Node *proj = region->in(j);
762 Node *inp = phi->in(j);
763 if (get_ctrl(inp) == proj) { // Found local op
764 cost++;
765 // Check for a chain of dependent ops; these will all become
766 // speculative in a CMOV.
767 for (uint k = 1; k < inp->req(); k++)
768 if (get_ctrl(inp->in(k)) == proj)
769 cost += ConditionalMoveLimit; // Too much speculative goo
770 }
771 }
772 // See if the Phi is used by a Cmp or Narrow oop Decode/Encode.
773 // This will likely Split-If, a higher-payoff operation.
774 for (DUIterator_Fast kmax, k = phi->fast_outs(kmax); k < kmax; k++) {
775 Node* use = phi->fast_out(k);
776 if (use->is_Cmp() || use->is_DecodeNarrowPtr() || use->is_EncodeNarrowPtr())
777 cost += ConditionalMoveLimit;
778 // Is there a use inside the loop?
779 // Note: check only basic types since CMoveP is pinned.
780 if (!used_inside_loop && is_java_primitive(bt)) {
781 IdealLoopTree* u_loop = get_loop(has_ctrl(use) ? get_ctrl(use) : use);
782 if (r_loop == u_loop || r_loop->is_member(u_loop)) {
1072 assert(get_loop(lca)->_nest < n_loop->_nest || get_loop(lca)->_head->as_Loop()->is_in_infinite_subgraph(), "must not be moved into inner loop");
1073
1074 // Move store out of the loop
1075 _igvn.replace_node(hook, n->in(MemNode::Memory));
1076 _igvn.replace_input_of(n, 0, lca);
1077 set_ctrl_and_loop(n, lca);
1078
1079 // Disconnect the phi now. An empty phi can confuse other
1080 // optimizations in this pass of loop opts..
1081 if (phi->in(LoopNode::LoopBackControl) == phi) {
1082 _igvn.replace_node(phi, phi->in(LoopNode::EntryControl));
1083 n_loop->_body.yank(phi);
1084 }
1085 }
1086 }
1087 }
1088 }
1089 }
1090 }
1091
1092 // Split some nodes that take a counted loop phi as input at a counted
1093 // loop can cause vectorization of some expressions to fail
1094 bool PhaseIdealLoop::split_thru_phi_could_prevent_vectorization(Node* n, Node* n_blk) {
1095 if (!n_blk->is_CountedLoop()) {
1096 return false;
1097 }
1098
1099 int opcode = n->Opcode();
1100
1101 if (opcode != Op_AndI &&
1102 opcode != Op_MulI &&
1103 opcode != Op_RotateRight &&
1104 opcode != Op_RShiftI) {
1105 return false;
1106 }
1107
1108 return n->in(1) == n_blk->as_BaseCountedLoop()->phi();
1109 }
1110
1111 //------------------------------split_if_with_blocks_pre-----------------------
1112 // Do the real work in a non-recursive function. Data nodes want to be
1113 // cloned in the pre-order so they can feed each other nicely.
1114 Node *PhaseIdealLoop::split_if_with_blocks_pre( Node *n ) {
1115 // Cloning these guys is unlikely to win
1116 int n_op = n->Opcode();
1117 if (n_op == Op_MergeMem) {
1118 return n;
1119 }
1120 if (n->is_Proj()) {
1121 return n;
1122 }
1123 // Do not clone-up CmpFXXX variations, as these are always
1124 // followed by a CmpI
1125 if (n->is_Cmp()) {
1126 return n;
1127 }
1128 // Attempt to use a conditional move instead of a phi/branch
1129 if (ConditionalMoveLimit > 0 && n_op == Op_Region) {
1130 Node *cmov = conditional_move( n );
1131 if (cmov) {
1132 return cmov;
1133 }
1134 }
1135 if (n->is_CFG() || n->is_LoadStore()) {
1136 return n;
1137 }
1138 if (n->is_Opaque1()) { // Opaque nodes cannot be mod'd
1139 if (!C->major_progress()) { // If chance of no more loop opts...
1140 _igvn._worklist.push(n); // maybe we'll remove them
1141 }
1142 return n;
1382
1383 return true;
1384 }
1385
1386 // Detect if the node is the inner strip-mined loop
1387 // Return: null if it's not the case, or the exit of outer strip-mined loop
1388 static Node* is_inner_of_stripmined_loop(const Node* out) {
1389 Node* out_le = nullptr;
1390
1391 if (out->is_CountedLoopEnd()) {
1392 const CountedLoopNode* loop = out->as_CountedLoopEnd()->loopnode();
1393
1394 if (loop != nullptr && loop->is_strip_mined()) {
1395 out_le = loop->in(LoopNode::EntryControl)->as_OuterStripMinedLoop()->outer_loop_exit();
1396 }
1397 }
1398
1399 return out_le;
1400 }
1401
1402 //------------------------------split_if_with_blocks_post----------------------
1403 // Do the real work in a non-recursive function. CFG hackery wants to be
1404 // in the post-order, so it can dirty the I-DOM info and not use the dirtied
1405 // info.
1406 void PhaseIdealLoop::split_if_with_blocks_post(Node *n) {
1407
1408 // Cloning Cmp through Phi's involves the split-if transform.
1409 // FastLock is not used by an If
1410 if (n->is_Cmp() && !n->is_FastLock()) {
1411 Node *n_ctrl = get_ctrl(n);
1412 // Determine if the Node has inputs from some local Phi.
1413 // Returns the block to clone thru.
1414 Node *n_blk = has_local_phi_input(n);
1415 if (n_blk != n_ctrl) {
1416 return;
1417 }
1418
1419 if (!can_split_if(n_ctrl)) {
1420 return;
1421 }
1422
1423 if (n->outcnt() != 1) {
1424 return; // Multiple bool's from 1 compare?
1425 }
1426 Node *bol = n->unique_out();
1427 assert(bol->is_Bool(), "expect a bool here");
1535 // 1. Move from RangeCheck "a" to RangeCheck "b": don't need to pin. If we ever remove b, then we pin
1536 // all its array accesses at that point.
1537 // 2. We move from RangeCheck "a" to regular if "b": need to pin. If we ever remove b, then its array
1538 // accesses would start to float, since we don't pin at that point.
1539 // 3. If we move from regular if: don't pin. All array accesses are already assumed to be pinned.
1540 bool pin_array_access_nodes = n->Opcode() == Op_RangeCheck &&
1541 prevdom->in(0)->Opcode() != Op_RangeCheck;
1542 dominated_by(prevdom->as_IfProj(), n->as_If(), false, pin_array_access_nodes);
1543 DEBUG_ONLY( if (VerifyLoopOptimizations) { verify(); } );
1544 return;
1545 }
1546 prevdom = dom;
1547 dom = idom(prevdom);
1548 }
1549 }
1550 }
1551
1552 try_sink_out_of_loop(n);
1553
1554 try_move_store_after_loop(n);
1555 }
1556
1557 // Transform:
1558 //
1559 // if (some_condition) {
1560 // // body 1
1561 // } else {
1562 // // body 2
1563 // }
1564 // if (some_condition) {
1565 // // body 3
1566 // } else {
1567 // // body 4
1568 // }
1569 //
1570 // into:
1571 //
1572 //
1573 // if (some_condition) {
1574 // // body 1
2017 uint i;
2018 for (i = 1; i < phi->req(); i++) {
2019 Node* b = phi->in(i);
2020 if (b->is_Phi()) {
2021 _igvn.replace_input_of(phi, i, clone_iff(b->as_Phi()));
2022 } else {
2023 assert(b->is_Bool() || b->is_Opaque4() || b->is_OpaqueInitializedAssertionPredicate(),
2024 "bool, non-null check with Opaque4 node or Initialized Assertion Predicate with its Opaque node");
2025 }
2026 }
2027 Node* n = phi->in(1);
2028 Node* sample_opaque = nullptr;
2029 Node *sample_bool = nullptr;
2030 if (n->is_Opaque4() || n->is_OpaqueInitializedAssertionPredicate()) {
2031 sample_opaque = n;
2032 sample_bool = n->in(1);
2033 assert(sample_bool->is_Bool(), "wrong type");
2034 } else {
2035 sample_bool = n;
2036 }
2037 Node *sample_cmp = sample_bool->in(1);
2038
2039 // Make Phis to merge the Cmp's inputs.
2040 PhiNode *phi1 = new PhiNode(phi->in(0), Type::TOP);
2041 PhiNode *phi2 = new PhiNode(phi->in(0), Type::TOP);
2042 for (i = 1; i < phi->req(); i++) {
2043 Node *n1 = sample_opaque == nullptr ? phi->in(i)->in(1)->in(1) : phi->in(i)->in(1)->in(1)->in(1);
2044 Node *n2 = sample_opaque == nullptr ? phi->in(i)->in(1)->in(2) : phi->in(i)->in(1)->in(1)->in(2);
2045 phi1->set_req(i, n1);
2046 phi2->set_req(i, n2);
2047 phi1->set_type(phi1->type()->meet_speculative(n1->bottom_type()));
2048 phi2->set_type(phi2->type()->meet_speculative(n2->bottom_type()));
2049 }
2050 // See if these Phis have been made before.
2051 // Register with optimizer
2052 Node *hit1 = _igvn.hash_find_insert(phi1);
2053 if (hit1) { // Hit, toss just made Phi
2054 _igvn.remove_dead_node(phi1); // Remove new phi
2055 assert(hit1->is_Phi(), "" );
2056 phi1 = (PhiNode*)hit1; // Use existing phi
2057 } else { // Miss
2058 _igvn.register_new_node_with_optimizer(phi1);
2059 }
2060 Node *hit2 = _igvn.hash_find_insert(phi2);
|
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 "precompiled.hpp"
26 #include "gc/shared/barrierSet.hpp"
27 #include "gc/shared/c2/barrierSetC2.hpp"
28 #include "memory/allocation.inline.hpp"
29 #include "memory/resourceArea.hpp"
30 #include "opto/addnode.hpp"
31 #include "opto/callnode.hpp"
32 #include "opto/castnode.hpp"
33 #include "opto/connode.hpp"
34 #include "opto/castnode.hpp"
35 #include "opto/divnode.hpp"
36 #include "opto/inlinetypenode.hpp"
37 #include "opto/loopnode.hpp"
38 #include "opto/matcher.hpp"
39 #include "opto/mulnode.hpp"
40 #include "opto/movenode.hpp"
41 #include "opto/opaquenode.hpp"
42 #include "opto/rootnode.hpp"
43 #include "opto/subnode.hpp"
44 #include "opto/subtypenode.hpp"
45 #include "opto/superword.hpp"
46 #include "opto/vectornode.hpp"
47 #include "utilities/macros.hpp"
48
49 //=============================================================================
50 //------------------------------split_thru_phi---------------------------------
51 // Split Node 'n' through merge point if there is enough win.
52 Node* PhaseIdealLoop::split_thru_phi(Node* n, Node* region, int policy) {
53 if ((n->Opcode() == Op_ConvI2L && n->bottom_type() != TypeLong::LONG) ||
54 (n->Opcode() == Op_ConvL2I && n->bottom_type() != TypeInt::INT)) {
55 // ConvI2L/ConvL2I may have type information on it which is unsafe to push up
56 // so disable this for now
57 return nullptr;
58 }
59
60 // Splitting range check CastIIs through a loop induction Phi can
61 // cause new Phis to be created that are left unrelated to the loop
62 // induction Phi and prevent optimizations (vectorization)
63 if (n->Opcode() == Op_CastII && region->is_CountedLoop() &&
64 n->in(1) == region->as_CountedLoop()->phi()) {
65 return nullptr;
66 }
67
68 // Inline types should not be split through Phis because they cannot be merged
69 // through Phi nodes but each value input needs to be merged individually.
70 if (n->is_InlineType()) {
71 return nullptr;
72 }
73
74 if (cannot_split_division(n, region)) {
75 return nullptr;
76 }
77
78 int wins = 0;
79 assert(!n->is_CFG(), "");
80 assert(region->is_Region(), "");
81
82 const Type* type = n->bottom_type();
83 const TypeOopPtr* t_oop = _igvn.type(n)->isa_oopptr();
84 Node* phi;
85 if (t_oop != nullptr && t_oop->is_known_instance_field()) {
86 int iid = t_oop->instance_id();
87 int index = C->get_alias_index(t_oop);
88 int offset = t_oop->offset();
89 phi = new PhiNode(region, type, nullptr, iid, index, offset);
90 } else {
91 phi = PhiNode::make_blank(region, n);
92 }
93 uint old_unique = C->unique();
750 // CMOVE'd derived pointer? It's a CMOVE'd derived base. Thus
751 // CMOVE'ing a derived pointer requires we also CMOVE the base. If we
752 // have a Phi for the base here that we convert to a CMOVE all is well
753 // and good. But if the base is dead, we'll not make a CMOVE. Later
754 // the allocator will have to produce a base by creating a CMOVE of the
755 // relevant bases. This puts the allocator in the business of
756 // manufacturing expensive instructions, generally a bad plan.
757 // Just Say No to Conditionally-Moved Derived Pointers.
758 if (tp && tp->offset() != 0)
759 return nullptr;
760 cost++;
761 break;
762 }
763 default:
764 return nullptr; // In particular, can't do memory or I/O
765 }
766 // Add in cost any speculative ops
767 for (uint j = 1; j < region->req(); j++) {
768 Node *proj = region->in(j);
769 Node *inp = phi->in(j);
770 if (inp->isa_InlineType()) {
771 // TODO 8302217 This prevents PhiNode::push_inline_types_through
772 return nullptr;
773 }
774 if (get_ctrl(inp) == proj) { // Found local op
775 cost++;
776 // Check for a chain of dependent ops; these will all become
777 // speculative in a CMOV.
778 for (uint k = 1; k < inp->req(); k++)
779 if (get_ctrl(inp->in(k)) == proj)
780 cost += ConditionalMoveLimit; // Too much speculative goo
781 }
782 }
783 // See if the Phi is used by a Cmp or Narrow oop Decode/Encode.
784 // This will likely Split-If, a higher-payoff operation.
785 for (DUIterator_Fast kmax, k = phi->fast_outs(kmax); k < kmax; k++) {
786 Node* use = phi->fast_out(k);
787 if (use->is_Cmp() || use->is_DecodeNarrowPtr() || use->is_EncodeNarrowPtr())
788 cost += ConditionalMoveLimit;
789 // Is there a use inside the loop?
790 // Note: check only basic types since CMoveP is pinned.
791 if (!used_inside_loop && is_java_primitive(bt)) {
792 IdealLoopTree* u_loop = get_loop(has_ctrl(use) ? get_ctrl(use) : use);
793 if (r_loop == u_loop || r_loop->is_member(u_loop)) {
1083 assert(get_loop(lca)->_nest < n_loop->_nest || get_loop(lca)->_head->as_Loop()->is_in_infinite_subgraph(), "must not be moved into inner loop");
1084
1085 // Move store out of the loop
1086 _igvn.replace_node(hook, n->in(MemNode::Memory));
1087 _igvn.replace_input_of(n, 0, lca);
1088 set_ctrl_and_loop(n, lca);
1089
1090 // Disconnect the phi now. An empty phi can confuse other
1091 // optimizations in this pass of loop opts..
1092 if (phi->in(LoopNode::LoopBackControl) == phi) {
1093 _igvn.replace_node(phi, phi->in(LoopNode::EntryControl));
1094 n_loop->_body.yank(phi);
1095 }
1096 }
1097 }
1098 }
1099 }
1100 }
1101 }
1102
1103 // We can't use immutable memory for the flat array check because we are loading the mark word which is
1104 // mutable. Although the bits we are interested in are immutable (we check for markWord::unlocked_value),
1105 // we need to use raw memory to not break anti dependency analysis. Below code will attempt to still move
1106 // flat array checks out of loops, mainly to enable loop unswitching.
1107 void PhaseIdealLoop::move_flat_array_check_out_of_loop(Node* n) {
1108 // Skip checks for more than one array
1109 if (n->req() > 3) {
1110 return;
1111 }
1112 Node* mem = n->in(FlatArrayCheckNode::Memory);
1113 Node* array = n->in(FlatArrayCheckNode::ArrayOrKlass)->uncast();
1114 IdealLoopTree* check_loop = get_loop(get_ctrl(n));
1115 IdealLoopTree* ary_loop = get_loop(get_ctrl(array));
1116
1117 // Check if array is loop invariant
1118 if (!check_loop->is_member(ary_loop)) {
1119 // Walk up memory graph from the check until we leave the loop
1120 VectorSet wq;
1121 wq.set(mem->_idx);
1122 while (check_loop->is_member(get_loop(ctrl_or_self(mem)))) {
1123 if (mem->is_Phi()) {
1124 mem = mem->in(1);
1125 } else if (mem->is_MergeMem()) {
1126 mem = mem->as_MergeMem()->memory_at(Compile::AliasIdxRaw);
1127 } else if (mem->is_Proj()) {
1128 mem = mem->in(0);
1129 } else if (mem->is_MemBar() || mem->is_SafePoint()) {
1130 mem = mem->in(TypeFunc::Memory);
1131 } else if (mem->is_Store() || mem->is_LoadStore() || mem->is_ClearArray()) {
1132 mem = mem->in(MemNode::Memory);
1133 } else {
1134 #ifdef ASSERT
1135 mem->dump();
1136 #endif
1137 ShouldNotReachHere();
1138 }
1139 if (wq.test_set(mem->_idx)) {
1140 return;
1141 }
1142 }
1143 // Replace memory input and re-compute ctrl to move the check out of the loop
1144 _igvn.replace_input_of(n, 1, mem);
1145 set_ctrl_and_loop(n, get_early_ctrl(n));
1146 Node* bol = n->unique_out();
1147 set_ctrl_and_loop(bol, get_early_ctrl(bol));
1148 }
1149 }
1150
1151 // Split some nodes that take a counted loop phi as input at a counted
1152 // loop can cause vectorization of some expressions to fail
1153 bool PhaseIdealLoop::split_thru_phi_could_prevent_vectorization(Node* n, Node* n_blk) {
1154 if (!n_blk->is_CountedLoop()) {
1155 return false;
1156 }
1157
1158 int opcode = n->Opcode();
1159
1160 if (opcode != Op_AndI &&
1161 opcode != Op_MulI &&
1162 opcode != Op_RotateRight &&
1163 opcode != Op_RShiftI) {
1164 return false;
1165 }
1166
1167 return n->in(1) == n_blk->as_BaseCountedLoop()->phi();
1168 }
1169
1170 //------------------------------split_if_with_blocks_pre-----------------------
1171 // Do the real work in a non-recursive function. Data nodes want to be
1172 // cloned in the pre-order so they can feed each other nicely.
1173 Node *PhaseIdealLoop::split_if_with_blocks_pre( Node *n ) {
1174 // Cloning these guys is unlikely to win
1175 int n_op = n->Opcode();
1176 if (n_op == Op_MergeMem) {
1177 return n;
1178 }
1179 if (n->is_Proj()) {
1180 return n;
1181 }
1182
1183 if (n->isa_FlatArrayCheck()) {
1184 move_flat_array_check_out_of_loop(n);
1185 return n;
1186 }
1187
1188 // Do not clone-up CmpFXXX variations, as these are always
1189 // followed by a CmpI
1190 if (n->is_Cmp()) {
1191 return n;
1192 }
1193 // Attempt to use a conditional move instead of a phi/branch
1194 if (ConditionalMoveLimit > 0 && n_op == Op_Region) {
1195 Node *cmov = conditional_move( n );
1196 if (cmov) {
1197 return cmov;
1198 }
1199 }
1200 if (n->is_CFG() || n->is_LoadStore()) {
1201 return n;
1202 }
1203 if (n->is_Opaque1()) { // Opaque nodes cannot be mod'd
1204 if (!C->major_progress()) { // If chance of no more loop opts...
1205 _igvn._worklist.push(n); // maybe we'll remove them
1206 }
1207 return n;
1447
1448 return true;
1449 }
1450
1451 // Detect if the node is the inner strip-mined loop
1452 // Return: null if it's not the case, or the exit of outer strip-mined loop
1453 static Node* is_inner_of_stripmined_loop(const Node* out) {
1454 Node* out_le = nullptr;
1455
1456 if (out->is_CountedLoopEnd()) {
1457 const CountedLoopNode* loop = out->as_CountedLoopEnd()->loopnode();
1458
1459 if (loop != nullptr && loop->is_strip_mined()) {
1460 out_le = loop->in(LoopNode::EntryControl)->as_OuterStripMinedLoop()->outer_loop_exit();
1461 }
1462 }
1463
1464 return out_le;
1465 }
1466
1467 bool PhaseIdealLoop::flat_array_element_type_check(Node *n) {
1468 // If the CmpP is a subtype check for a value that has just been
1469 // loaded from an array, the subtype check guarantees the value
1470 // can't be stored in a flat array and the load of the value
1471 // happens with a flat array check then: push the type check
1472 // through the phi of the flat array check. This needs special
1473 // logic because the subtype check's input is not a phi but a
1474 // LoadKlass that must first be cloned through the phi.
1475 if (n->Opcode() != Op_CmpP) {
1476 return false;
1477 }
1478
1479 Node* klassptr = n->in(1);
1480 Node* klasscon = n->in(2);
1481
1482 if (klassptr->is_DecodeNarrowPtr()) {
1483 klassptr = klassptr->in(1);
1484 }
1485
1486 if (klassptr->Opcode() != Op_LoadKlass && klassptr->Opcode() != Op_LoadNKlass) {
1487 return false;
1488 }
1489
1490 if (!klasscon->is_Con()) {
1491 return false;
1492 }
1493
1494 Node* addr = klassptr->in(MemNode::Address);
1495
1496 if (!addr->is_AddP()) {
1497 return false;
1498 }
1499
1500 intptr_t offset;
1501 Node* obj = AddPNode::Ideal_base_and_offset(addr, &_igvn, offset);
1502
1503 if (obj == nullptr) {
1504 return false;
1505 }
1506
1507 assert(obj != nullptr && addr->in(AddPNode::Base) == addr->in(AddPNode::Address), "malformed AddP?");
1508 if (obj->Opcode() == Op_CastPP) {
1509 obj = obj->in(1);
1510 }
1511
1512 if (!obj->is_Phi()) {
1513 return false;
1514 }
1515
1516 Node* region = obj->in(0);
1517
1518 Node* phi = PhiNode::make_blank(region, n->in(1));
1519 for (uint i = 1; i < region->req(); i++) {
1520 Node* in = obj->in(i);
1521 Node* ctrl = region->in(i);
1522 if (addr->in(AddPNode::Base) != obj) {
1523 Node* cast = addr->in(AddPNode::Base);
1524 assert(cast->Opcode() == Op_CastPP && cast->in(0) != nullptr, "inconsistent subgraph");
1525 Node* cast_clone = cast->clone();
1526 cast_clone->set_req(0, ctrl);
1527 cast_clone->set_req(1, in);
1528 register_new_node(cast_clone, ctrl);
1529 const Type* tcast = cast_clone->Value(&_igvn);
1530 _igvn.set_type(cast_clone, tcast);
1531 cast_clone->as_Type()->set_type(tcast);
1532 in = cast_clone;
1533 }
1534 Node* addr_clone = addr->clone();
1535 addr_clone->set_req(AddPNode::Base, in);
1536 addr_clone->set_req(AddPNode::Address, in);
1537 register_new_node(addr_clone, ctrl);
1538 _igvn.set_type(addr_clone, addr_clone->Value(&_igvn));
1539 Node* klassptr_clone = klassptr->clone();
1540 klassptr_clone->set_req(2, addr_clone);
1541 register_new_node(klassptr_clone, ctrl);
1542 _igvn.set_type(klassptr_clone, klassptr_clone->Value(&_igvn));
1543 if (klassptr != n->in(1)) {
1544 Node* decode = n->in(1);
1545 assert(decode->is_DecodeNarrowPtr(), "inconsistent subgraph");
1546 Node* decode_clone = decode->clone();
1547 decode_clone->set_req(1, klassptr_clone);
1548 register_new_node(decode_clone, ctrl);
1549 _igvn.set_type(decode_clone, decode_clone->Value(&_igvn));
1550 klassptr_clone = decode_clone;
1551 }
1552 phi->set_req(i, klassptr_clone);
1553 }
1554 register_new_node(phi, region);
1555 Node* orig = n->in(1);
1556 _igvn.replace_input_of(n, 1, phi);
1557 split_if_with_blocks_post(n);
1558 if (n->outcnt() != 0) {
1559 _igvn.replace_input_of(n, 1, orig);
1560 _igvn.remove_dead_node(phi);
1561 }
1562 return true;
1563 }
1564
1565 //------------------------------split_if_with_blocks_post----------------------
1566 // Do the real work in a non-recursive function. CFG hackery wants to be
1567 // in the post-order, so it can dirty the I-DOM info and not use the dirtied
1568 // info.
1569 void PhaseIdealLoop::split_if_with_blocks_post(Node *n) {
1570
1571 if (flat_array_element_type_check(n)) {
1572 return;
1573 }
1574
1575 // Cloning Cmp through Phi's involves the split-if transform.
1576 // FastLock is not used by an If
1577 if (n->is_Cmp() && !n->is_FastLock()) {
1578 Node *n_ctrl = get_ctrl(n);
1579 // Determine if the Node has inputs from some local Phi.
1580 // Returns the block to clone thru.
1581 Node *n_blk = has_local_phi_input(n);
1582 if (n_blk != n_ctrl) {
1583 return;
1584 }
1585
1586 if (!can_split_if(n_ctrl)) {
1587 return;
1588 }
1589
1590 if (n->outcnt() != 1) {
1591 return; // Multiple bool's from 1 compare?
1592 }
1593 Node *bol = n->unique_out();
1594 assert(bol->is_Bool(), "expect a bool here");
1702 // 1. Move from RangeCheck "a" to RangeCheck "b": don't need to pin. If we ever remove b, then we pin
1703 // all its array accesses at that point.
1704 // 2. We move from RangeCheck "a" to regular if "b": need to pin. If we ever remove b, then its array
1705 // accesses would start to float, since we don't pin at that point.
1706 // 3. If we move from regular if: don't pin. All array accesses are already assumed to be pinned.
1707 bool pin_array_access_nodes = n->Opcode() == Op_RangeCheck &&
1708 prevdom->in(0)->Opcode() != Op_RangeCheck;
1709 dominated_by(prevdom->as_IfProj(), n->as_If(), false, pin_array_access_nodes);
1710 DEBUG_ONLY( if (VerifyLoopOptimizations) { verify(); } );
1711 return;
1712 }
1713 prevdom = dom;
1714 dom = idom(prevdom);
1715 }
1716 }
1717 }
1718
1719 try_sink_out_of_loop(n);
1720
1721 try_move_store_after_loop(n);
1722
1723 // Remove multiple allocations of the same inline type
1724 if (n->is_InlineType()) {
1725 n->as_InlineType()->remove_redundant_allocations(this);
1726 }
1727 }
1728
1729 // Transform:
1730 //
1731 // if (some_condition) {
1732 // // body 1
1733 // } else {
1734 // // body 2
1735 // }
1736 // if (some_condition) {
1737 // // body 3
1738 // } else {
1739 // // body 4
1740 // }
1741 //
1742 // into:
1743 //
1744 //
1745 // if (some_condition) {
1746 // // body 1
2189 uint i;
2190 for (i = 1; i < phi->req(); i++) {
2191 Node* b = phi->in(i);
2192 if (b->is_Phi()) {
2193 _igvn.replace_input_of(phi, i, clone_iff(b->as_Phi()));
2194 } else {
2195 assert(b->is_Bool() || b->is_Opaque4() || b->is_OpaqueInitializedAssertionPredicate(),
2196 "bool, non-null check with Opaque4 node or Initialized Assertion Predicate with its Opaque node");
2197 }
2198 }
2199 Node* n = phi->in(1);
2200 Node* sample_opaque = nullptr;
2201 Node *sample_bool = nullptr;
2202 if (n->is_Opaque4() || n->is_OpaqueInitializedAssertionPredicate()) {
2203 sample_opaque = n;
2204 sample_bool = n->in(1);
2205 assert(sample_bool->is_Bool(), "wrong type");
2206 } else {
2207 sample_bool = n;
2208 }
2209 Node* sample_cmp = sample_bool->in(1);
2210 const Type* t = Type::TOP;
2211 const TypePtr* at = nullptr;
2212 if (sample_cmp->is_FlatArrayCheck()) {
2213 // Left input of a FlatArrayCheckNode is memory, set the (adr) type of the phi accordingly
2214 assert(sample_cmp->in(1)->bottom_type() == Type::MEMORY, "unexpected input type");
2215 t = Type::MEMORY;
2216 at = TypeRawPtr::BOTTOM;
2217 }
2218
2219 // Make Phis to merge the Cmp's inputs.
2220 PhiNode *phi1 = new PhiNode(phi->in(0), t, at);
2221 PhiNode *phi2 = new PhiNode(phi->in(0), Type::TOP);
2222 for (i = 1; i < phi->req(); i++) {
2223 Node *n1 = sample_opaque == nullptr ? phi->in(i)->in(1)->in(1) : phi->in(i)->in(1)->in(1)->in(1);
2224 Node *n2 = sample_opaque == nullptr ? phi->in(i)->in(1)->in(2) : phi->in(i)->in(1)->in(1)->in(2);
2225 phi1->set_req(i, n1);
2226 phi2->set_req(i, n2);
2227 phi1->set_type(phi1->type()->meet_speculative(n1->bottom_type()));
2228 phi2->set_type(phi2->type()->meet_speculative(n2->bottom_type()));
2229 }
2230 // See if these Phis have been made before.
2231 // Register with optimizer
2232 Node *hit1 = _igvn.hash_find_insert(phi1);
2233 if (hit1) { // Hit, toss just made Phi
2234 _igvn.remove_dead_node(phi1); // Remove new phi
2235 assert(hit1->is_Phi(), "" );
2236 phi1 = (PhiNode*)hit1; // Use existing phi
2237 } else { // Miss
2238 _igvn.register_new_node_with_optimizer(phi1);
2239 }
2240 Node *hit2 = _igvn.hash_find_insert(phi2);
|