730
731 //------------------------------Idealize---------------------------------------
732 Node *CmpINode::Ideal( PhaseGVN *phase, bool can_reshape ) {
733 if (phase->type(in(2))->higher_equal(TypeInt::ZERO)) {
734 switch (in(1)->Opcode()) {
735 case Op_CmpL3: // Collapse a CmpL3/CmpI into a CmpL
736 return new CmpLNode(in(1)->in(1),in(1)->in(2));
737 case Op_CmpF3: // Collapse a CmpF3/CmpI into a CmpF
738 return new CmpFNode(in(1)->in(1),in(1)->in(2));
739 case Op_CmpD3: // Collapse a CmpD3/CmpI into a CmpD
740 return new CmpDNode(in(1)->in(1),in(1)->in(2));
741 //case Op_SubI:
742 // If (x - y) cannot overflow, then ((x - y) <?> 0)
743 // can be turned into (x <?> y).
744 // This is handled (with more general cases) by Ideal_sub_algebra.
745 }
746 }
747 return NULL; // No change
748 }
749
750 Node *CmpLNode::Ideal( PhaseGVN *phase, bool can_reshape ) {
751 const TypeLong *t2 = phase->type(in(2))->isa_long();
752 if (Opcode() == Op_CmpL && in(1)->Opcode() == Op_ConvI2L && t2 && t2->is_con()) {
753 const jlong con = t2->get_con();
754 if (con >= min_jint && con <= max_jint) {
755 return new CmpINode(in(1)->in(1), phase->intcon((jint)con));
756 }
757 }
758 return NULL;
759 }
760
761 //=============================================================================
762 // Simplify a CmpL (compare 2 longs ) node, based on local information.
763 // If both inputs are constants, compare them.
764 const Type *CmpLNode::sub( const Type *t1, const Type *t2 ) const {
765 const TypeLong *r0 = t1->is_long(); // Handy access
766 const TypeLong *r1 = t2->is_long();
767
768 if( r0->_hi < r1->_lo ) // Range is always low?
769 return TypeInt::CC_LT;
770 else if( r0->_lo > r1->_hi ) // Range is always high?
771 return TypeInt::CC_GT;
772
773 else if( r0->is_con() && r1->is_con() ) { // comparing constants?
774 assert(r0->get_con() == r1->get_con(), "must be equal");
775 return TypeInt::CC_EQ; // Equal results.
776 } else if( r0->_hi == r1->_lo ) // Range is never high?
777 return TypeInt::CC_LE;
778 else if( r0->_lo == r1->_hi ) // Range is never low?
779 return TypeInt::CC_GE;
780 return TypeInt::CC; // else use worst case results
899 klass1->is_loaded() && !klass1->is_interface() &&
900 (!klass0->is_obj_array_klass() ||
901 !klass0->as_obj_array_klass()->base_element_klass()->is_interface()) &&
902 (!klass1->is_obj_array_klass() ||
903 !klass1->as_obj_array_klass()->base_element_klass()->is_interface())) {
904 bool unrelated_classes = false;
905 // See if neither subclasses the other, or if the class on top
906 // is precise. In either of these cases, the compare is known
907 // to fail if at least one of the pointers is provably not null.
908 if (klass0->equals(klass1)) { // if types are unequal but klasses are equal
909 // Do nothing; we know nothing for imprecise types
910 } else if (klass0->is_subtype_of(klass1)) {
911 // If klass1's type is PRECISE, then classes are unrelated.
912 unrelated_classes = xklass1;
913 } else if (klass1->is_subtype_of(klass0)) {
914 // If klass0's type is PRECISE, then classes are unrelated.
915 unrelated_classes = xklass0;
916 } else { // Neither subtypes the other
917 unrelated_classes = true;
918 }
919 if (unrelated_classes) {
920 // The oops classes are known to be unrelated. If the joined PTRs of
921 // two oops is not Null and not Bottom, then we are sure that one
922 // of the two oops is non-null, and the comparison will always fail.
923 TypePtr::PTR jp = r0->join_ptr(r1->_ptr);
924 if (jp != TypePtr::Null && jp != TypePtr::BotPTR) {
925 return TypeInt::CC_GT;
926 }
927 }
928 }
929 }
930
931 // Known constants can be compared exactly
932 // Null can be distinguished from any NotNull pointers
933 // Unknown inputs makes an unknown result
934 if( r0->singleton() ) {
935 intptr_t bits0 = r0->get_con();
936 if( r1->singleton() )
937 return bits0 == r1->get_con() ? TypeInt::CC_EQ : TypeInt::CC_GT;
938 return ( r1->_ptr == TypePtr::NotNull && bits0==0 ) ? TypeInt::CC_GT : TypeInt::CC;
984 if (!mirror_type) return NULL;
985
986 // x.getClass() == int.class can never be true (for all primitive types)
987 // Return a ConP(NULL) node for this case.
988 if (mirror_type->is_classless()) {
989 return phase->makecon(TypePtr::NULL_PTR);
990 }
991
992 // return the ConP(Foo.klass)
993 assert(mirror_type->is_klass(), "mirror_type should represent a Klass*");
994 return phase->makecon(TypeKlassPtr::make(mirror_type->as_klass()));
995 }
996
997 //------------------------------Ideal------------------------------------------
998 // Normalize comparisons between Java mirror loads to compare the klass instead.
999 //
1000 // Also check for the case of comparing an unknown klass loaded from the primary
1001 // super-type array vs a known klass with no subtypes. This amounts to
1002 // checking to see an unknown klass subtypes a known klass with no subtypes;
1003 // this only happens on an exact match. We can shorten this test by 1 load.
1004 Node *CmpPNode::Ideal( PhaseGVN *phase, bool can_reshape ) {
1005 // Normalize comparisons between Java mirrors into comparisons of the low-
1006 // level klass, where a dependent load could be shortened.
1007 //
1008 // The new pattern has a nice effect of matching the same pattern used in the
1009 // fast path of instanceof/checkcast/Class.isInstance(), which allows
1010 // redundant exact type check be optimized away by GVN.
1011 // For example, in
1012 // if (x.getClass() == Foo.class) {
1013 // Foo foo = (Foo) x;
1014 // // ... use a ...
1015 // }
1016 // a CmpPNode could be shared between if_acmpne and checkcast
1017 {
1018 Node* k1 = isa_java_mirror_load(phase, in(1));
1019 Node* k2 = isa_java_mirror_load(phase, in(2));
1020 Node* conk2 = isa_const_java_mirror(phase, in(2));
1021
1022 if (k1 && (k2 || conk2)) {
1023 Node* lhs = k1;
1024 Node* rhs = (k2 != NULL) ? k2 : conk2;
|
730
731 //------------------------------Idealize---------------------------------------
732 Node *CmpINode::Ideal( PhaseGVN *phase, bool can_reshape ) {
733 if (phase->type(in(2))->higher_equal(TypeInt::ZERO)) {
734 switch (in(1)->Opcode()) {
735 case Op_CmpL3: // Collapse a CmpL3/CmpI into a CmpL
736 return new CmpLNode(in(1)->in(1),in(1)->in(2));
737 case Op_CmpF3: // Collapse a CmpF3/CmpI into a CmpF
738 return new CmpFNode(in(1)->in(1),in(1)->in(2));
739 case Op_CmpD3: // Collapse a CmpD3/CmpI into a CmpD
740 return new CmpDNode(in(1)->in(1),in(1)->in(2));
741 //case Op_SubI:
742 // If (x - y) cannot overflow, then ((x - y) <?> 0)
743 // can be turned into (x <?> y).
744 // This is handled (with more general cases) by Ideal_sub_algebra.
745 }
746 }
747 return NULL; // No change
748 }
749
750 //------------------------------Ideal------------------------------------------
751 Node* CmpLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
752 Node* a = NULL;
753 Node* b = NULL;
754 if (is_double_null_check(phase, a, b) && (phase->type(a)->is_zero_type() || phase->type(b)->is_zero_type())) {
755 // Degraded to a simple null check, use old acmp
756 return new CmpPNode(a, b);
757 }
758 const TypeLong *t2 = phase->type(in(2))->isa_long();
759 if (Opcode() == Op_CmpL && in(1)->Opcode() == Op_ConvI2L && t2 && t2->is_con()) {
760 const jlong con = t2->get_con();
761 if (con >= min_jint && con <= max_jint) {
762 return new CmpINode(in(1)->in(1), phase->intcon((jint)con));
763 }
764 }
765 return NULL;
766 }
767
768 // Match double null check emitted by Compile::optimize_acmp()
769 bool CmpLNode::is_double_null_check(PhaseGVN* phase, Node*& a, Node*& b) const {
770 if (in(1)->Opcode() == Op_OrL &&
771 in(1)->in(1)->Opcode() == Op_CastP2X &&
772 in(1)->in(2)->Opcode() == Op_CastP2X &&
773 in(2)->bottom_type()->is_zero_type()) {
774 assert(EnableValhalla, "unexpected double null check");
775 a = in(1)->in(1)->in(1);
776 b = in(1)->in(2)->in(1);
777 return true;
778 }
779 return false;
780 }
781
782 //------------------------------Value------------------------------------------
783 const Type* CmpLNode::Value(PhaseGVN* phase) const {
784 Node* a = NULL;
785 Node* b = NULL;
786 if (is_double_null_check(phase, a, b) && (!phase->type(a)->maybe_null() || !phase->type(b)->maybe_null())) {
787 // One operand is never NULL, emit constant false
788 return TypeInt::CC_GT;
789 }
790 return SubNode::Value(phase);
791 }
792
793 //=============================================================================
794 // Simplify a CmpL (compare 2 longs ) node, based on local information.
795 // If both inputs are constants, compare them.
796 const Type *CmpLNode::sub( const Type *t1, const Type *t2 ) const {
797 const TypeLong *r0 = t1->is_long(); // Handy access
798 const TypeLong *r1 = t2->is_long();
799
800 if( r0->_hi < r1->_lo ) // Range is always low?
801 return TypeInt::CC_LT;
802 else if( r0->_lo > r1->_hi ) // Range is always high?
803 return TypeInt::CC_GT;
804
805 else if( r0->is_con() && r1->is_con() ) { // comparing constants?
806 assert(r0->get_con() == r1->get_con(), "must be equal");
807 return TypeInt::CC_EQ; // Equal results.
808 } else if( r0->_hi == r1->_lo ) // Range is never high?
809 return TypeInt::CC_LE;
810 else if( r0->_lo == r1->_hi ) // Range is never low?
811 return TypeInt::CC_GE;
812 return TypeInt::CC; // else use worst case results
931 klass1->is_loaded() && !klass1->is_interface() &&
932 (!klass0->is_obj_array_klass() ||
933 !klass0->as_obj_array_klass()->base_element_klass()->is_interface()) &&
934 (!klass1->is_obj_array_klass() ||
935 !klass1->as_obj_array_klass()->base_element_klass()->is_interface())) {
936 bool unrelated_classes = false;
937 // See if neither subclasses the other, or if the class on top
938 // is precise. In either of these cases, the compare is known
939 // to fail if at least one of the pointers is provably not null.
940 if (klass0->equals(klass1)) { // if types are unequal but klasses are equal
941 // Do nothing; we know nothing for imprecise types
942 } else if (klass0->is_subtype_of(klass1)) {
943 // If klass1's type is PRECISE, then classes are unrelated.
944 unrelated_classes = xklass1;
945 } else if (klass1->is_subtype_of(klass0)) {
946 // If klass0's type is PRECISE, then classes are unrelated.
947 unrelated_classes = xklass0;
948 } else { // Neither subtypes the other
949 unrelated_classes = true;
950 }
951 if (!unrelated_classes) {
952 // Handle inline type arrays
953 if ((r0->flatten_array() && (!r1->can_be_inline_type() || (klass1->is_inlinetype() && !klass1->flatten_array()))) ||
954 (r1->flatten_array() && (!r0->can_be_inline_type() || (klass0->is_inlinetype() && !klass0->flatten_array())))) {
955 // One type is flattened in arrays but the other type is not. Must be unrelated.
956 unrelated_classes = true;
957 }
958 }
959 if (unrelated_classes) {
960 // The oops classes are known to be unrelated. If the joined PTRs of
961 // two oops is not Null and not Bottom, then we are sure that one
962 // of the two oops is non-null, and the comparison will always fail.
963 TypePtr::PTR jp = r0->join_ptr(r1->_ptr);
964 if (jp != TypePtr::Null && jp != TypePtr::BotPTR) {
965 return TypeInt::CC_GT;
966 }
967 }
968 }
969 }
970
971 // Known constants can be compared exactly
972 // Null can be distinguished from any NotNull pointers
973 // Unknown inputs makes an unknown result
974 if( r0->singleton() ) {
975 intptr_t bits0 = r0->get_con();
976 if( r1->singleton() )
977 return bits0 == r1->get_con() ? TypeInt::CC_EQ : TypeInt::CC_GT;
978 return ( r1->_ptr == TypePtr::NotNull && bits0==0 ) ? TypeInt::CC_GT : TypeInt::CC;
1024 if (!mirror_type) return NULL;
1025
1026 // x.getClass() == int.class can never be true (for all primitive types)
1027 // Return a ConP(NULL) node for this case.
1028 if (mirror_type->is_classless()) {
1029 return phase->makecon(TypePtr::NULL_PTR);
1030 }
1031
1032 // return the ConP(Foo.klass)
1033 assert(mirror_type->is_klass(), "mirror_type should represent a Klass*");
1034 return phase->makecon(TypeKlassPtr::make(mirror_type->as_klass()));
1035 }
1036
1037 //------------------------------Ideal------------------------------------------
1038 // Normalize comparisons between Java mirror loads to compare the klass instead.
1039 //
1040 // Also check for the case of comparing an unknown klass loaded from the primary
1041 // super-type array vs a known klass with no subtypes. This amounts to
1042 // checking to see an unknown klass subtypes a known klass with no subtypes;
1043 // this only happens on an exact match. We can shorten this test by 1 load.
1044 Node* CmpPNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1045 // Normalize comparisons between Java mirrors into comparisons of the low-
1046 // level klass, where a dependent load could be shortened.
1047 //
1048 // The new pattern has a nice effect of matching the same pattern used in the
1049 // fast path of instanceof/checkcast/Class.isInstance(), which allows
1050 // redundant exact type check be optimized away by GVN.
1051 // For example, in
1052 // if (x.getClass() == Foo.class) {
1053 // Foo foo = (Foo) x;
1054 // // ... use a ...
1055 // }
1056 // a CmpPNode could be shared between if_acmpne and checkcast
1057 {
1058 Node* k1 = isa_java_mirror_load(phase, in(1));
1059 Node* k2 = isa_java_mirror_load(phase, in(2));
1060 Node* conk2 = isa_const_java_mirror(phase, in(2));
1061
1062 if (k1 && (k2 || conk2)) {
1063 Node* lhs = k1;
1064 Node* rhs = (k2 != NULL) ? k2 : conk2;
|