5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
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 "compiler/compileLog.hpp"
26 #include "gc/shared/barrierSet.hpp"
27 #include "gc/shared/c2/barrierSetC2.hpp"
28 #include "memory/allocation.inline.hpp"
29 #include "opto/addnode.hpp"
30 #include "opto/callnode.hpp"
31 #include "opto/cfgnode.hpp"
32 #include "opto/loopnode.hpp"
33 #include "opto/matcher.hpp"
34 #include "opto/movenode.hpp"
35 #include "opto/mulnode.hpp"
36 #include "opto/opaquenode.hpp"
37 #include "opto/opcodes.hpp"
38 #include "opto/phaseX.hpp"
39 #include "opto/subnode.hpp"
40 #include "runtime/sharedRuntime.hpp"
41 #include "utilities/reverse_bits.hpp"
42
43 // Portions of code courtesy of Clifford Click
44
45 // Optimization - Graph Style
46
47 #include "math.h"
48
49 //=============================================================================
50 //------------------------------Identity---------------------------------------
51 // If right input is a constant 0, return the left input.
52 Node* SubNode::Identity(PhaseGVN* phase) {
53 assert(in(1) != this, "Must already have called Value");
54 assert(in(2) != this, "Must already have called Value");
55
56 const Type* zero = add_id();
57
58 // Remove double negation if it is not a floating point number since negation
59 // is not the same as subtraction for floating point numbers
872 switch (in(1)->Opcode()) {
873 case Op_CmpU3: // Collapse a CmpU3/CmpI into a CmpU
874 return new CmpUNode(in(1)->in(1),in(1)->in(2));
875 case Op_CmpL3: // Collapse a CmpL3/CmpI into a CmpL
876 return new CmpLNode(in(1)->in(1),in(1)->in(2));
877 case Op_CmpUL3: // Collapse a CmpUL3/CmpI into a CmpUL
878 return new CmpULNode(in(1)->in(1),in(1)->in(2));
879 case Op_CmpF3: // Collapse a CmpF3/CmpI into a CmpF
880 return new CmpFNode(in(1)->in(1),in(1)->in(2));
881 case Op_CmpD3: // Collapse a CmpD3/CmpI into a CmpD
882 return new CmpDNode(in(1)->in(1),in(1)->in(2));
883 //case Op_SubI:
884 // If (x - y) cannot overflow, then ((x - y) <?> 0)
885 // can be turned into (x <?> y).
886 // This is handled (with more general cases) by Ideal_sub_algebra.
887 }
888 }
889 return nullptr; // No change
890 }
891
892 Node *CmpLNode::Ideal( PhaseGVN *phase, bool can_reshape ) {
893 const TypeLong *t2 = phase->type(in(2))->isa_long();
894 if (Opcode() == Op_CmpL && in(1)->Opcode() == Op_ConvI2L && t2 && t2->is_con()) {
895 const jlong con = t2->get_con();
896 if (con >= min_jint && con <= max_jint) {
897 return new CmpINode(in(1)->in(1), phase->intcon((jint)con));
898 }
899 }
900 return nullptr;
901 }
902
903 //=============================================================================
904 // Simplify a CmpL (compare 2 longs ) node, based on local information.
905 // If both inputs are constants, compare them.
906 const Type *CmpLNode::sub( const Type *t1, const Type *t2 ) const {
907 const TypeLong *r0 = t1->is_long(); // Handy access
908 const TypeLong *r1 = t2->is_long();
909
910 if( r0->_hi < r1->_lo ) // Range is always low?
911 return TypeInt::CC_LT;
912 else if( r0->_lo > r1->_hi ) // Range is always high?
980 const TypeOopPtr* p1 = r1->isa_oopptr();
981 const TypeKlassPtr* k0 = r0->isa_klassptr();
982 const TypeKlassPtr* k1 = r1->isa_klassptr();
983 if ((p0 && p1) || (k0 && k1)) {
984 bool xklass0 = p0 ? p0->klass_is_exact() : k0->klass_is_exact();
985 bool xklass1 = p1 ? p1->klass_is_exact() : k1->klass_is_exact();
986 bool unrelated_classes = false;
987
988 if ((p0 && p0->is_same_java_type_as(p1)) ||
989 (k0 && k0->is_same_java_type_as(k1))) {
990 } else if ((p0 && !p1->maybe_java_subtype_of(p0) && !p0->maybe_java_subtype_of(p1)) ||
991 (k0 && !k1->maybe_java_subtype_of(k0) && !k0->maybe_java_subtype_of(k1))) {
992 unrelated_classes = true;
993 } else if ((p0 && !p1->maybe_java_subtype_of(p0)) ||
994 (k0 && !k1->maybe_java_subtype_of(k0))) {
995 unrelated_classes = xklass1;
996 } else if ((p0 && !p0->maybe_java_subtype_of(p1)) ||
997 (k0 && !k0->maybe_java_subtype_of(k1))) {
998 unrelated_classes = xklass0;
999 }
1000
1001 if (unrelated_classes) {
1002 // The oops classes are known to be unrelated. If the joined PTRs of
1003 // two oops is not Null and not Bottom, then we are sure that one
1004 // of the two oops is non-null, and the comparison will always fail.
1005 TypePtr::PTR jp = r0->join_ptr(r1->_ptr);
1006 if (jp != TypePtr::Null && jp != TypePtr::BotPTR) {
1007 return TypeInt::CC_GT;
1008 }
1009 }
1010 }
1011
1012 // Known constants can be compared exactly
1013 // Null can be distinguished from any NotNull pointers
1014 // Unknown inputs makes an unknown result
1015 if( r0->singleton() ) {
1016 intptr_t bits0 = r0->get_con();
1017 if( r1->singleton() )
1018 return bits0 == r1->get_con() ? TypeInt::CC_EQ : TypeInt::CC_GT;
1019 return ( r1->_ptr == TypePtr::NotNull && bits0==0 ) ? TypeInt::CC_GT : TypeInt::CC;
1020 } else if( r1->singleton() ) {
1021 intptr_t bits1 = r1->get_con();
1022 return ( r0->_ptr == TypePtr::NotNull && bits1==0 ) ? TypeInt::CC_GT : TypeInt::CC;
1023 } else
1024 return TypeInt::CC;
1025 }
1026
1027 static inline Node* isa_java_mirror_load(PhaseGVN* phase, Node* n) {
1028 // Return the klass node for (indirect load from OopHandle)
1029 // LoadBarrier?(LoadP(LoadP(AddP(foo:Klass, #java_mirror))))
1030 // or null if not matching.
1031 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
1032 n = bs->step_over_gc_barrier(n);
1033
1034 if (n->Opcode() != Op_LoadP) return nullptr;
1035
1036 const TypeInstPtr* tp = phase->type(n)->isa_instptr();
1037 if (!tp || tp->instance_klass() != phase->C->env()->Class_klass()) return nullptr;
1038
1039 Node* adr = n->in(MemNode::Address);
1040 // First load from OopHandle: ((OopHandle)mirror)->resolve(); may need barrier.
1041 if (adr->Opcode() != Op_LoadP || !phase->type(adr)->isa_rawptr()) return nullptr;
1042 adr = adr->in(MemNode::Address);
1043
1044 intptr_t off = 0;
1045 Node* k = AddPNode::Ideal_base_and_offset(adr, phase, off);
1046 if (k == nullptr) return nullptr;
1047 const TypeKlassPtr* tkp = phase->type(k)->isa_klassptr();
1048 if (!tkp || off != in_bytes(Klass::java_mirror_offset())) return nullptr;
1049
1050 // We've found the klass node of a Java mirror load.
1051 return k;
1052 }
1053
1054 static inline Node* isa_const_java_mirror(PhaseGVN* phase, Node* n) {
1055 // for ConP(Foo.class) return ConP(Foo.klass)
1056 // otherwise return null
1057 if (!n->is_Con()) return nullptr;
1058
1059 const TypeInstPtr* tp = phase->type(n)->isa_instptr();
1060 if (!tp) return nullptr;
1061
1062 ciType* mirror_type = tp->java_mirror_type();
1063 // TypeInstPtr::java_mirror_type() returns non-null for compile-
1064 // time Class constants only.
1065 if (!mirror_type) return nullptr;
1066
1067 // x.getClass() == int.class can never be true (for all primitive types)
1068 // Return a ConP(null) node for this case.
1069 if (mirror_type->is_classless()) {
1070 return phase->makecon(TypePtr::NULL_PTR);
1071 }
1072
1073 // return the ConP(Foo.klass)
1074 assert(mirror_type->is_klass(), "mirror_type should represent a Klass*");
1075 return phase->makecon(TypeKlassPtr::make(mirror_type->as_klass(), Type::trust_interfaces));
1076 }
1077
1078 //------------------------------Ideal------------------------------------------
1079 // Normalize comparisons between Java mirror loads to compare the klass instead.
1080 //
1081 // Also check for the case of comparing an unknown klass loaded from the primary
1082 // super-type array vs a known klass with no subtypes. This amounts to
1083 // checking to see an unknown klass subtypes a known klass with no subtypes;
1084 // this only happens on an exact match. We can shorten this test by 1 load.
1085 Node *CmpPNode::Ideal( PhaseGVN *phase, bool can_reshape ) {
1086 // Normalize comparisons between Java mirrors into comparisons of the low-
1087 // level klass, where a dependent load could be shortened.
1088 //
1089 // The new pattern has a nice effect of matching the same pattern used in the
1090 // fast path of instanceof/checkcast/Class.isInstance(), which allows
1091 // redundant exact type check be optimized away by GVN.
1092 // For example, in
1093 // if (x.getClass() == Foo.class) {
1094 // Foo foo = (Foo) x;
1095 // // ... use a ...
1096 // }
1097 // a CmpPNode could be shared between if_acmpne and checkcast
1098 {
1099 Node* k1 = isa_java_mirror_load(phase, in(1));
1100 Node* k2 = isa_java_mirror_load(phase, in(2));
1101 Node* conk2 = isa_const_java_mirror(phase, in(2));
1102
1103 if (k1 && (k2 || conk2)) {
1104 Node* lhs = k1;
1105 Node* rhs = (k2 != nullptr) ? k2 : conk2;
1106 set_req_X(1, lhs, phase);
1107 set_req_X(2, rhs, phase);
1108 return this;
1109 }
1110 }
1111
1112 // Constant pointer on right?
1113 const TypeKlassPtr* t2 = phase->type(in(2))->isa_klassptr();
1114 if (t2 == nullptr || !t2->klass_is_exact())
1115 return nullptr;
1116 // Get the constant klass we are comparing to.
1117 ciKlass* superklass = t2->exact_klass();
1118
1119 // Now check for LoadKlass on left.
1120 Node* ldk1 = in(1);
1121 if (ldk1->is_DecodeNKlass()) {
1160 //
1161 // We could be more liberal here, and allow the optimization on interfaces
1162 // which have a single implementor. This would require us to increase the
1163 // expressiveness of the add_dependency() mechanism.
1164 // %%% Do this after we fix TypeOopPtr: Deps are expressive enough now.
1165
1166 // Object arrays must have their base element have no subtypes
1167 while (superklass->is_obj_array_klass()) {
1168 ciType* elem = superklass->as_obj_array_klass()->element_type();
1169 superklass = elem->as_klass();
1170 }
1171 if (superklass->is_instance_klass()) {
1172 ciInstanceKlass* ik = superklass->as_instance_klass();
1173 if (ik->has_subklass() || ik->is_interface()) return nullptr;
1174 // Add a dependency if there is a chance that a subclass will be added later.
1175 if (!ik->is_final()) {
1176 phase->C->dependencies()->assert_leaf_type(ik);
1177 }
1178 }
1179
1180 // Bypass the dependent load, and compare directly
1181 this->set_req_X(1, ldk2, phase);
1182
1183 return this;
1184 }
1185
1186 const Type* CmpPNode::Value(PhaseGVN* phase) const {
1187 const Type* res = CmpNode::Value(phase);
1188 if (res == TypeInt::CC) {
1189 const TypeOopPtr* p0 = phase->type(in(1))->isa_oopptr();
1190 const TypeOopPtr* p1 = phase->type(in(2))->isa_oopptr();
1191 if (p0 != nullptr && p1 != nullptr) {
1192 Node* in1 = in(1)->uncast();
1193 Node* in2 = in(2)->uncast();
1194 AllocateNode* alloc1 = AllocateNode::Ideal_allocation(in1);
1195 AllocateNode* alloc2 = AllocateNode::Ideal_allocation(in2);
1196 if (MemNode::detect_ptr_independence(in1, alloc1, in2, alloc2, phase)) {
1197 return TypeInt::CC_GT; // different pointers
1198 }
1199 }
1298 if( t2_value_as_double == (double)t2_value_as_float ) {
1299 // Test value can be represented as a float
1300 // Eliminate the conversion to double and create new comparison
1301 Node *new_in1 = in(idx_f2d)->in(1);
1302 Node *new_in2 = phase->makecon( TypeF::make(t2_value_as_float) );
1303 if( idx_f2d != 1 ) { // Must flip args to match original order
1304 Node *tmp = new_in1;
1305 new_in1 = new_in2;
1306 new_in2 = tmp;
1307 }
1308 CmpFNode *new_cmp = (Opcode() == Op_CmpD3)
1309 ? new CmpF3Node( new_in1, new_in2 )
1310 : new CmpFNode ( new_in1, new_in2 ) ;
1311 return new_cmp; // Changed to CmpFNode
1312 }
1313 // Testing value required the precision of a double
1314 }
1315 return nullptr; // No change
1316 }
1317
1318
1319 //=============================================================================
1320 //------------------------------cc2logical-------------------------------------
1321 // Convert a condition code type to a logical type
1322 const Type *BoolTest::cc2logical( const Type *CC ) const {
1323 if( CC == Type::TOP ) return Type::TOP;
1324 if( CC->base() != Type::Int ) return TypeInt::BOOL; // Bottom or worse
1325 const TypeInt *ti = CC->is_int();
1326 if( ti->is_con() ) { // Only 1 kind of condition codes set?
1327 // Match low order 2 bits
1328 int tmp = ((ti->get_con()&3) == (_test&3)) ? 1 : 0;
1329 if( _test & 4 ) tmp = 1-tmp; // Optionally complement result
1330 return TypeInt::make(tmp); // Boolean result
1331 }
1332
1333 if( CC == TypeInt::CC_GE ) {
1334 if( _test == ge ) return TypeInt::ONE;
1335 if( _test == lt ) return TypeInt::ZERO;
1336 }
1337 if( CC == TypeInt::CC_LE ) {
1479 }
1480 return nullptr;
1481 }
1482
1483 static bool is_counted_loop_cmp(Node *cmp) {
1484 Node *n = cmp->in(1)->in(1);
1485 return n != nullptr &&
1486 n->is_Phi() &&
1487 n->in(0) != nullptr &&
1488 n->in(0)->is_CountedLoop() &&
1489 n->in(0)->as_CountedLoop()->phi() == n;
1490 }
1491
1492 //------------------------------Ideal------------------------------------------
1493 Node *BoolNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1494 // Change "bool tst (cmp con x)" into "bool ~tst (cmp x con)".
1495 // This moves the constant to the right. Helps value-numbering.
1496 Node *cmp = in(1);
1497 if( !cmp->is_Sub() ) return nullptr;
1498 int cop = cmp->Opcode();
1499 if( cop == Op_FastLock || cop == Op_FastUnlock ||
1500 cmp->is_SubTypeCheck() || cop == Op_VectorTest ) {
1501 return nullptr;
1502 }
1503 Node *cmp1 = cmp->in(1);
1504 Node *cmp2 = cmp->in(2);
1505 if( !cmp1 ) return nullptr;
1506
1507 if (_test._test == BoolTest::overflow || _test._test == BoolTest::no_overflow) {
1508 return nullptr;
1509 }
1510
1511 const int cmp1_op = cmp1->Opcode();
1512 const int cmp2_op = cmp2->Opcode();
1513
1514 // Constant on left?
1515 Node *con = cmp1;
1516 // Move constants to the right of compare's to canonicalize.
1517 // Do not muck with Opaque1 nodes, as this indicates a loop
1518 // guard that cannot change shape.
1519 if (con->is_Con() && !cmp2->is_Con() && cmp2_op != Op_OpaqueZeroTripGuard &&
1520 // Because of NaN's, CmpD and CmpF are not commutative
|
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
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/ciObjArrayKlass.hpp"
26 #include "compiler/compileLog.hpp"
27 #include "gc/shared/barrierSet.hpp"
28 #include "gc/shared/c2/barrierSetC2.hpp"
29 #include "memory/allocation.inline.hpp"
30 #include "opto/addnode.hpp"
31 #include "opto/callnode.hpp"
32 #include "opto/castnode.hpp"
33 #include "opto/cfgnode.hpp"
34 #include "opto/convertnode.hpp"
35 #include "opto/inlinetypenode.hpp"
36 #include "opto/loopnode.hpp"
37 #include "opto/matcher.hpp"
38 #include "opto/movenode.hpp"
39 #include "opto/mulnode.hpp"
40 #include "opto/opaquenode.hpp"
41 #include "opto/opcodes.hpp"
42 #include "opto/phaseX.hpp"
43 #include "opto/subnode.hpp"
44 #include "runtime/arguments.hpp"
45 #include "runtime/sharedRuntime.hpp"
46 #include "utilities/reverse_bits.hpp"
47
48 // Portions of code courtesy of Clifford Click
49
50 // Optimization - Graph Style
51
52 #include "math.h"
53
54 //=============================================================================
55 //------------------------------Identity---------------------------------------
56 // If right input is a constant 0, return the left input.
57 Node* SubNode::Identity(PhaseGVN* phase) {
58 assert(in(1) != this, "Must already have called Value");
59 assert(in(2) != this, "Must already have called Value");
60
61 const Type* zero = add_id();
62
63 // Remove double negation if it is not a floating point number since negation
64 // is not the same as subtraction for floating point numbers
877 switch (in(1)->Opcode()) {
878 case Op_CmpU3: // Collapse a CmpU3/CmpI into a CmpU
879 return new CmpUNode(in(1)->in(1),in(1)->in(2));
880 case Op_CmpL3: // Collapse a CmpL3/CmpI into a CmpL
881 return new CmpLNode(in(1)->in(1),in(1)->in(2));
882 case Op_CmpUL3: // Collapse a CmpUL3/CmpI into a CmpUL
883 return new CmpULNode(in(1)->in(1),in(1)->in(2));
884 case Op_CmpF3: // Collapse a CmpF3/CmpI into a CmpF
885 return new CmpFNode(in(1)->in(1),in(1)->in(2));
886 case Op_CmpD3: // Collapse a CmpD3/CmpI into a CmpD
887 return new CmpDNode(in(1)->in(1),in(1)->in(2));
888 //case Op_SubI:
889 // If (x - y) cannot overflow, then ((x - y) <?> 0)
890 // can be turned into (x <?> y).
891 // This is handled (with more general cases) by Ideal_sub_algebra.
892 }
893 }
894 return nullptr; // No change
895 }
896
897 //------------------------------Ideal------------------------------------------
898 Node* CmpLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
899 // Optimize expressions like
900 // CmpL(OrL(CastP2X(..), CastP2X(..)), 0L)
901 // that are used by acmp to implement a "both operands are null" check.
902 // See also the corresponding code in CmpPNode::Ideal.
903 if (can_reshape && in(1)->Opcode() == Op_OrL &&
904 in(2)->bottom_type()->is_zero_type()) {
905 for (int i = 1; i <= 2; ++i) {
906 Node* orIn = in(1)->in(i);
907 if (orIn->Opcode() == Op_CastP2X) {
908 Node* castIn = orIn->in(1);
909 if (castIn->is_InlineType()) {
910 // Replace the CastP2X by the null marker
911 InlineTypeNode* vt = castIn->as_InlineType();
912 Node* nm = phase->transform(new ConvI2LNode(vt->get_null_marker()));
913 phase->is_IterGVN()->replace_input_of(in(1), i, nm);
914 return this;
915 } else if (!phase->type(castIn)->maybe_null()) {
916 // Never null. Replace the CastP2X by constant 1L.
917 phase->is_IterGVN()->replace_input_of(in(1), i, phase->longcon(1));
918 return this;
919 }
920 }
921 }
922 }
923 const TypeLong *t2 = phase->type(in(2))->isa_long();
924 if (Opcode() == Op_CmpL && in(1)->Opcode() == Op_ConvI2L && t2 && t2->is_con()) {
925 const jlong con = t2->get_con();
926 if (con >= min_jint && con <= max_jint) {
927 return new CmpINode(in(1)->in(1), phase->intcon((jint)con));
928 }
929 }
930 return nullptr;
931 }
932
933 //=============================================================================
934 // Simplify a CmpL (compare 2 longs ) node, based on local information.
935 // If both inputs are constants, compare them.
936 const Type *CmpLNode::sub( const Type *t1, const Type *t2 ) const {
937 const TypeLong *r0 = t1->is_long(); // Handy access
938 const TypeLong *r1 = t2->is_long();
939
940 if( r0->_hi < r1->_lo ) // Range is always low?
941 return TypeInt::CC_LT;
942 else if( r0->_lo > r1->_hi ) // Range is always high?
1010 const TypeOopPtr* p1 = r1->isa_oopptr();
1011 const TypeKlassPtr* k0 = r0->isa_klassptr();
1012 const TypeKlassPtr* k1 = r1->isa_klassptr();
1013 if ((p0 && p1) || (k0 && k1)) {
1014 bool xklass0 = p0 ? p0->klass_is_exact() : k0->klass_is_exact();
1015 bool xklass1 = p1 ? p1->klass_is_exact() : k1->klass_is_exact();
1016 bool unrelated_classes = false;
1017
1018 if ((p0 && p0->is_same_java_type_as(p1)) ||
1019 (k0 && k0->is_same_java_type_as(k1))) {
1020 } else if ((p0 && !p1->maybe_java_subtype_of(p0) && !p0->maybe_java_subtype_of(p1)) ||
1021 (k0 && !k1->maybe_java_subtype_of(k0) && !k0->maybe_java_subtype_of(k1))) {
1022 unrelated_classes = true;
1023 } else if ((p0 && !p1->maybe_java_subtype_of(p0)) ||
1024 (k0 && !k1->maybe_java_subtype_of(k0))) {
1025 unrelated_classes = xklass1;
1026 } else if ((p0 && !p0->maybe_java_subtype_of(p1)) ||
1027 (k0 && !k0->maybe_java_subtype_of(k1))) {
1028 unrelated_classes = xklass0;
1029 }
1030 if (!unrelated_classes) {
1031 // Handle inline type arrays
1032 if ((r0->is_flat_in_array() && r1->is_not_flat_in_array()) ||
1033 (r1->is_flat_in_array() && r0->is_not_flat_in_array())) {
1034 // One type is in flat arrays but the other type is not. Must be unrelated.
1035 unrelated_classes = true;
1036 } else if ((r0->is_not_flat() && r1->is_flat()) ||
1037 (r1->is_not_flat() && r0->is_flat())) {
1038 // One type is a non-flat array and the other type is a flat array. Must be unrelated.
1039 unrelated_classes = true;
1040 } else if ((r0->is_not_null_free() && r1->is_null_free()) ||
1041 (r1->is_not_null_free() && r0->is_null_free())) {
1042 // One type is a nullable array and the other type is a null-free array. Must be unrelated.
1043 unrelated_classes = true;
1044 }
1045 }
1046 if (unrelated_classes) {
1047 // The oops classes are known to be unrelated. If the joined PTRs of
1048 // two oops is not Null and not Bottom, then we are sure that one
1049 // of the two oops is non-null, and the comparison will always fail.
1050 TypePtr::PTR jp = r0->join_ptr(r1->_ptr);
1051 if (jp != TypePtr::Null && jp != TypePtr::BotPTR) {
1052 return TypeInt::CC_GT;
1053 }
1054 }
1055 }
1056
1057 // Known constants can be compared exactly
1058 // Null can be distinguished from any NotNull pointers
1059 // Unknown inputs makes an unknown result
1060 if( r0->singleton() ) {
1061 intptr_t bits0 = r0->get_con();
1062 if( r1->singleton() )
1063 return bits0 == r1->get_con() ? TypeInt::CC_EQ : TypeInt::CC_GT;
1064 return ( r1->_ptr == TypePtr::NotNull && bits0==0 ) ? TypeInt::CC_GT : TypeInt::CC;
1065 } else if( r1->singleton() ) {
1066 intptr_t bits1 = r1->get_con();
1067 return ( r0->_ptr == TypePtr::NotNull && bits1==0 ) ? TypeInt::CC_GT : TypeInt::CC;
1068 } else
1069 return TypeInt::CC;
1070 }
1071
1072 static inline Node* isa_java_mirror_load(PhaseGVN* phase, Node* n, bool& might_be_an_array) {
1073 // Return the klass node for (indirect load from OopHandle)
1074 // LoadBarrier?(LoadP(LoadP(AddP(foo:Klass, #java_mirror))))
1075 // or null if not matching.
1076 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
1077 n = bs->step_over_gc_barrier(n);
1078
1079 if (n->Opcode() != Op_LoadP) return nullptr;
1080
1081 const TypeInstPtr* tp = phase->type(n)->isa_instptr();
1082 if (!tp || tp->instance_klass() != phase->C->env()->Class_klass()) return nullptr;
1083
1084 Node* adr = n->in(MemNode::Address);
1085 // First load from OopHandle: ((OopHandle)mirror)->resolve(); may need barrier.
1086 if (adr->Opcode() != Op_LoadP || !phase->type(adr)->isa_rawptr()) return nullptr;
1087 adr = adr->in(MemNode::Address);
1088
1089 intptr_t off = 0;
1090 Node* k = AddPNode::Ideal_base_and_offset(adr, phase, off);
1091 if (k == nullptr) return nullptr;
1092 const TypeKlassPtr* tkp = phase->type(k)->isa_klassptr();
1093 if (!tkp || off != in_bytes(Klass::java_mirror_offset())) return nullptr;
1094 might_be_an_array |= tkp->isa_aryklassptr() || tkp->is_instklassptr()->might_be_an_array();
1095
1096 // We've found the klass node of a Java mirror load.
1097 return k;
1098 }
1099
1100 static inline Node* isa_const_java_mirror(PhaseGVN* phase, Node* n, bool& might_be_an_array) {
1101 // for ConP(Foo.class) return ConP(Foo.klass)
1102 // otherwise return null
1103 if (!n->is_Con()) return nullptr;
1104
1105 const TypeInstPtr* tp = phase->type(n)->isa_instptr();
1106 if (!tp) return nullptr;
1107
1108 ciType* mirror_type = tp->java_mirror_type();
1109 // TypeInstPtr::java_mirror_type() returns non-null for compile-
1110 // time Class constants only.
1111 if (!mirror_type) return nullptr;
1112
1113 // x.getClass() == int.class can never be true (for all primitive types)
1114 // Return a ConP(null) node for this case.
1115 if (mirror_type->is_classless()) {
1116 return phase->makecon(TypePtr::NULL_PTR);
1117 }
1118
1119 // return the ConP(Foo.klass)
1120 ciKlass* mirror_klass = mirror_type->as_klass();
1121
1122 if (mirror_klass->is_array_klass() && !mirror_klass->is_type_array_klass()) {
1123 if (!mirror_klass->can_be_inline_array_klass()) {
1124 // Special case for non-value arrays: They only have one (default) refined class, use it
1125 ciArrayKlass* refined_mirror_klass = ciObjArrayKlass::make(mirror_klass->as_array_klass()->element_klass(), true);
1126 return phase->makecon(TypeAryKlassPtr::make(refined_mirror_klass, Type::trust_interfaces));
1127 }
1128 might_be_an_array |= true;
1129 }
1130
1131 return phase->makecon(TypeKlassPtr::make(mirror_klass, Type::trust_interfaces));
1132 }
1133
1134 //------------------------------Ideal------------------------------------------
1135 // Normalize comparisons between Java mirror loads to compare the klass instead.
1136 //
1137 // Also check for the case of comparing an unknown klass loaded from the primary
1138 // super-type array vs a known klass with no subtypes. This amounts to
1139 // checking to see an unknown klass subtypes a known klass with no subtypes;
1140 // this only happens on an exact match. We can shorten this test by 1 load.
1141 Node* CmpPNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1142 Node* uncast_in1 = in(1)->uncast();
1143 Node* uncast_in2 = in(2)->uncast();
1144 if (uncast_in1->is_InlineType() && phase->type(uncast_in2)->is_zero_type()) {
1145 // Null checking a scalarized but nullable inline type. Check the null marker
1146 // input instead of the oop input to avoid keeping buffer allocations alive.
1147 return new CmpINode(uncast_in1->as_InlineType()->get_null_marker(), phase->intcon(0));
1148 }
1149 if (uncast_in1->is_InlineType() || uncast_in2->is_InlineType()) {
1150 // In C2 IR, CmpP on value objects is a pointer comparison, not a value comparison.
1151 // For non-null operands it cannot reliably be true, since their buffer oops are not
1152 // guaranteed to be identical. Therefore, the comparison can only be true when both
1153 // operands are null. Convert expressions like this to a "both operands are null" check:
1154 // CmpL(OrL(CastP2X(..), CastP2X(..)), 0L)
1155 // CmpLNode::Ideal might optimize this further to avoid keeping buffer allocations alive.
1156 Node* input[2];
1157 for (int i = 1; i <= 2; ++i) {
1158 Node* uncast_in = in(i)->uncast();
1159 if (uncast_in->is_InlineType()) {
1160 input[i-1] = phase->transform(new ConvI2LNode(uncast_in->as_InlineType()->get_null_marker()));
1161 } else {
1162 input[i-1] = phase->transform(new CastP2XNode(nullptr, uncast_in));
1163 }
1164 }
1165 Node* orL = phase->transform(new OrXNode(input[0], input[1]));
1166 return new CmpXNode(orL, phase->MakeConX(0));
1167 }
1168
1169 // Normalize comparisons between Java mirrors into comparisons of the low-
1170 // level klass, where a dependent load could be shortened.
1171 //
1172 // The new pattern has a nice effect of matching the same pattern used in the
1173 // fast path of instanceof/checkcast/Class.isInstance(), which allows
1174 // redundant exact type check be optimized away by GVN.
1175 // For example, in
1176 // if (x.getClass() == Foo.class) {
1177 // Foo foo = (Foo) x;
1178 // // ... use a ...
1179 // }
1180 // a CmpPNode could be shared between if_acmpne and checkcast
1181 {
1182 bool might_be_an_array1 = false;
1183 bool might_be_an_array2 = false;
1184 Node* k1 = isa_java_mirror_load(phase, in(1), might_be_an_array1);
1185 Node* k2 = isa_java_mirror_load(phase, in(2), might_be_an_array2);
1186 Node* conk2 = isa_const_java_mirror(phase, in(2), might_be_an_array2);
1187 if (might_be_an_array1 && might_be_an_array2) {
1188 // Don't optimize if both sides might be an array because arrays with
1189 // the same Java mirror can have different refined array klasses.
1190 k1 = k2 = nullptr;
1191 }
1192
1193 if (k1 && (k2 || conk2)) {
1194 Node* lhs = k1;
1195 Node* rhs = (k2 != nullptr) ? k2 : conk2;
1196 set_req_X(1, lhs, phase);
1197 set_req_X(2, rhs, phase);
1198 return this;
1199 }
1200 }
1201
1202 // Constant pointer on right?
1203 const TypeKlassPtr* t2 = phase->type(in(2))->isa_klassptr();
1204 if (t2 == nullptr || !t2->klass_is_exact())
1205 return nullptr;
1206 // Get the constant klass we are comparing to.
1207 ciKlass* superklass = t2->exact_klass();
1208
1209 // Now check for LoadKlass on left.
1210 Node* ldk1 = in(1);
1211 if (ldk1->is_DecodeNKlass()) {
1250 //
1251 // We could be more liberal here, and allow the optimization on interfaces
1252 // which have a single implementor. This would require us to increase the
1253 // expressiveness of the add_dependency() mechanism.
1254 // %%% Do this after we fix TypeOopPtr: Deps are expressive enough now.
1255
1256 // Object arrays must have their base element have no subtypes
1257 while (superklass->is_obj_array_klass()) {
1258 ciType* elem = superklass->as_obj_array_klass()->element_type();
1259 superklass = elem->as_klass();
1260 }
1261 if (superklass->is_instance_klass()) {
1262 ciInstanceKlass* ik = superklass->as_instance_klass();
1263 if (ik->has_subklass() || ik->is_interface()) return nullptr;
1264 // Add a dependency if there is a chance that a subclass will be added later.
1265 if (!ik->is_final()) {
1266 phase->C->dependencies()->assert_leaf_type(ik);
1267 }
1268 }
1269
1270 // Do not fold the subtype check to an array klass pointer comparison for
1271 // value class arrays because they can have multiple refined array klasses.
1272 superklass = t2->exact_klass();
1273 assert(!superklass->is_flat_array_klass(), "Unexpected flat array klass");
1274 if (superklass->is_obj_array_klass()) {
1275 if (superklass->as_array_klass()->element_klass()->is_inlinetype() && !superklass->as_array_klass()->is_refined()) {
1276 return nullptr;
1277 } else {
1278 // Special case for non-value arrays: They only have one (default) refined class, use it
1279 set_req_X(2, phase->makecon(t2->is_aryklassptr()->cast_to_refined_array_klass_ptr()), phase);
1280 }
1281 }
1282
1283 // Bypass the dependent load, and compare directly
1284 this->set_req_X(1, ldk2, phase);
1285
1286 return this;
1287 }
1288
1289 const Type* CmpPNode::Value(PhaseGVN* phase) const {
1290 const Type* res = CmpNode::Value(phase);
1291 if (res == TypeInt::CC) {
1292 const TypeOopPtr* p0 = phase->type(in(1))->isa_oopptr();
1293 const TypeOopPtr* p1 = phase->type(in(2))->isa_oopptr();
1294 if (p0 != nullptr && p1 != nullptr) {
1295 Node* in1 = in(1)->uncast();
1296 Node* in2 = in(2)->uncast();
1297 AllocateNode* alloc1 = AllocateNode::Ideal_allocation(in1);
1298 AllocateNode* alloc2 = AllocateNode::Ideal_allocation(in2);
1299 if (MemNode::detect_ptr_independence(in1, alloc1, in2, alloc2, phase)) {
1300 return TypeInt::CC_GT; // different pointers
1301 }
1302 }
1401 if( t2_value_as_double == (double)t2_value_as_float ) {
1402 // Test value can be represented as a float
1403 // Eliminate the conversion to double and create new comparison
1404 Node *new_in1 = in(idx_f2d)->in(1);
1405 Node *new_in2 = phase->makecon( TypeF::make(t2_value_as_float) );
1406 if( idx_f2d != 1 ) { // Must flip args to match original order
1407 Node *tmp = new_in1;
1408 new_in1 = new_in2;
1409 new_in2 = tmp;
1410 }
1411 CmpFNode *new_cmp = (Opcode() == Op_CmpD3)
1412 ? new CmpF3Node( new_in1, new_in2 )
1413 : new CmpFNode ( new_in1, new_in2 ) ;
1414 return new_cmp; // Changed to CmpFNode
1415 }
1416 // Testing value required the precision of a double
1417 }
1418 return nullptr; // No change
1419 }
1420
1421 //=============================================================================
1422 //------------------------------Value------------------------------------------
1423 const Type* FlatArrayCheckNode::Value(PhaseGVN* phase) const {
1424 bool all_not_flat = true;
1425 for (uint i = ArrayOrKlass; i < req(); ++i) {
1426 const Type* t = phase->type(in(i));
1427 if (t == Type::TOP) {
1428 return Type::TOP;
1429 }
1430 if (t->is_ptr()->is_flat()) {
1431 // One of the input arrays is flat, check always passes
1432 return TypeInt::CC_EQ;
1433 } else if (!t->is_ptr()->is_not_flat()) {
1434 // One of the input arrays might be flat
1435 all_not_flat = false;
1436 }
1437 }
1438 if (all_not_flat) {
1439 // None of the input arrays can be flat, check always fails
1440 return TypeInt::CC_GT;
1441 }
1442 return TypeInt::CC;
1443 }
1444
1445 //------------------------------Ideal------------------------------------------
1446 Node* FlatArrayCheckNode::Ideal(PhaseGVN* phase, bool can_reshape) {
1447 bool changed = false;
1448 // Remove inputs that are known to be non-flat
1449 for (uint i = ArrayOrKlass; i < req(); ++i) {
1450 const Type* t = phase->type(in(i));
1451 if (t->isa_ptr() && t->is_ptr()->is_not_flat()) {
1452 del_req(i--);
1453 changed = true;
1454 }
1455 }
1456 return changed ? this : nullptr;
1457 }
1458
1459 //=============================================================================
1460 //------------------------------cc2logical-------------------------------------
1461 // Convert a condition code type to a logical type
1462 const Type *BoolTest::cc2logical( const Type *CC ) const {
1463 if( CC == Type::TOP ) return Type::TOP;
1464 if( CC->base() != Type::Int ) return TypeInt::BOOL; // Bottom or worse
1465 const TypeInt *ti = CC->is_int();
1466 if( ti->is_con() ) { // Only 1 kind of condition codes set?
1467 // Match low order 2 bits
1468 int tmp = ((ti->get_con()&3) == (_test&3)) ? 1 : 0;
1469 if( _test & 4 ) tmp = 1-tmp; // Optionally complement result
1470 return TypeInt::make(tmp); // Boolean result
1471 }
1472
1473 if( CC == TypeInt::CC_GE ) {
1474 if( _test == ge ) return TypeInt::ONE;
1475 if( _test == lt ) return TypeInt::ZERO;
1476 }
1477 if( CC == TypeInt::CC_LE ) {
1619 }
1620 return nullptr;
1621 }
1622
1623 static bool is_counted_loop_cmp(Node *cmp) {
1624 Node *n = cmp->in(1)->in(1);
1625 return n != nullptr &&
1626 n->is_Phi() &&
1627 n->in(0) != nullptr &&
1628 n->in(0)->is_CountedLoop() &&
1629 n->in(0)->as_CountedLoop()->phi() == n;
1630 }
1631
1632 //------------------------------Ideal------------------------------------------
1633 Node *BoolNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1634 // Change "bool tst (cmp con x)" into "bool ~tst (cmp x con)".
1635 // This moves the constant to the right. Helps value-numbering.
1636 Node *cmp = in(1);
1637 if( !cmp->is_Sub() ) return nullptr;
1638 int cop = cmp->Opcode();
1639 if (cop == Op_FastLock || cop == Op_FastUnlock || cop == Op_FlatArrayCheck ||
1640 cmp->is_SubTypeCheck() || cop == Op_VectorTest) {
1641 return nullptr;
1642 }
1643 Node *cmp1 = cmp->in(1);
1644 Node *cmp2 = cmp->in(2);
1645 if( !cmp1 ) return nullptr;
1646
1647 if (_test._test == BoolTest::overflow || _test._test == BoolTest::no_overflow) {
1648 return nullptr;
1649 }
1650
1651 const int cmp1_op = cmp1->Opcode();
1652 const int cmp2_op = cmp2->Opcode();
1653
1654 // Constant on left?
1655 Node *con = cmp1;
1656 // Move constants to the right of compare's to canonicalize.
1657 // Do not muck with Opaque1 nodes, as this indicates a loop
1658 // guard that cannot change shape.
1659 if (con->is_Con() && !cmp2->is_Con() && cmp2_op != Op_OpaqueZeroTripGuard &&
1660 // Because of NaN's, CmpD and CmpF are not commutative
|