diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp --- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -1847,6 +1847,21 @@ foldCmpLoadFromIndexedGlobal(LI, GEP, GV, Cmp, C2)) return Res; + // icmp slt (and -X, X), 0 --> icmp eq (X, MinSignedC) + // icmp sgt (and -X, X), -1 --> icmp ne (X, MinSignedC) + { + Value* X; + if (match(And, m_OneUse(m_c_And(m_Neg(m_Value(X)), m_Deferred(X))))) { + Constant *MinSignedC = ConstantInt::get( + X->getType(), + APInt::getSignedMinValue(X->getType()->getScalarSizeInBits())); + if (Pred == ICmpInst::ICMP_SLT && C.isZero()) + return new ICmpInst(ICmpInst::ICMP_EQ, X, MinSignedC); + if (Pred == ICmpInst::ICMP_SGT && C.isAllOnes()) + return new ICmpInst(ICmpInst::ICMP_NE, X, MinSignedC); + } + } + if (!Cmp.isEquality()) return nullptr; diff --git a/llvm/test/Transforms/InstCombine/fold-signbit-test-power2.ll b/llvm/test/Transforms/InstCombine/fold-signbit-test-power2.ll --- a/llvm/test/Transforms/InstCombine/fold-signbit-test-power2.ll +++ b/llvm/test/Transforms/InstCombine/fold-signbit-test-power2.ll @@ -4,9 +4,7 @@ ; icmp slt (and X, -X), 0 --> icmp eq (X, MinSignC) define i1 @pow2_or_zero1(i8 %x) { ; CHECK-LABEL: @pow2_or_zero1( -; CHECK-NEXT: [[NEG:%.*]] = sub i8 0, [[X:%.*]] -; CHECK-NEXT: [[POW2_OR_ZERO:%.*]] = and i8 [[NEG]], [[X]] -; CHECK-NEXT: [[CMP:%.*]] = icmp slt i8 [[POW2_OR_ZERO]], 0 +; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[X:%.*]], -128 ; CHECK-NEXT: ret i1 [[CMP]] ; %neg = sub i8 0, %x @@ -18,13 +16,11 @@ ; icmp slt (and -X, X), 0 --> icmp eq (X, MinSignC) define i1 @pow2_or_zero1_commute(i8 %A) { ; CHECK-LABEL: @pow2_or_zero1_commute( -; CHECK-NEXT: [[X:%.*]] = sdiv i8 42, [[A:%.*]] -; CHECK-NEXT: [[NEG:%.*]] = sub nsw i8 0, [[X]] -; CHECK-NEXT: [[POW2_OR_ZERO:%.*]] = and i8 [[X]], [[NEG]] -; CHECK-NEXT: [[CMP:%.*]] = icmp slt i8 [[POW2_OR_ZERO]], 0 +; CHECK-NEXT: [[X:%.*]] = mul i8 [[A:%.*]], 42 +; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[X]], -128 ; CHECK-NEXT: ret i1 [[CMP]] ; - %x = sdiv i8 42, %A ; thwart complexity-based canonicalization + %x = mul i8 42, %A ; thwart complexity-based canonicalization %neg = sub i8 0, %x %pow2_or_zero = and i8 %neg, %x %cmp = icmp slt i8 %pow2_or_zero, 0 @@ -33,9 +29,7 @@ define <2 x i1> @pow2_or_zero1_vec(<2 x i8> %x) { ; CHECK-LABEL: @pow2_or_zero1_vec( -; CHECK-NEXT: [[NEG:%.*]] = sub <2 x i8> zeroinitializer, [[X:%.*]] -; CHECK-NEXT: [[POW2_OR_ZERO:%.*]] = and <2 x i8> [[NEG]], [[X]] -; CHECK-NEXT: [[CMP:%.*]] = icmp slt <2 x i8> [[POW2_OR_ZERO]], zeroinitializer +; CHECK-NEXT: [[CMP:%.*]] = icmp eq <2 x i8> [[X:%.*]], ; CHECK-NEXT: ret <2 x i1> [[CMP]] ; %neg = sub <2 x i8> , %x @@ -48,9 +42,7 @@ define <2 x i1> @pow2_or_zero1_vec_commute(<2 x i8> %A) { ; CHECK-LABEL: @pow2_or_zero1_vec_commute( ; CHECK-NEXT: [[X:%.*]] = mul <2 x i8> [[A:%.*]], -; CHECK-NEXT: [[NEG:%.*]] = sub <2 x i8> zeroinitializer, [[X]] -; CHECK-NEXT: [[POW2_OR_ZERO:%.*]] = and <2 x i8> [[X]], [[NEG]] -; CHECK-NEXT: [[CMP:%.*]] = icmp slt <2 x i8> [[POW2_OR_ZERO]], zeroinitializer +; CHECK-NEXT: [[CMP:%.*]] = icmp eq <2 x i8> [[X]], ; CHECK-NEXT: ret <2 x i1> [[CMP]] ; %x = mul <2 x i8> , %A ; thwart complexity-based canonicalization @@ -63,9 +55,7 @@ ; icmp sgt (and X, -X), -1 --> icmp ne (X, MinSignC) define i1 @pow2_or_zero2(i8 %x) { ; CHECK-LABEL: @pow2_or_zero2( -; CHECK-NEXT: [[NEG:%.*]] = sub i8 0, [[X:%.*]] -; CHECK-NEXT: [[POW2_OR_ZERO:%.*]] = and i8 [[NEG]], [[X]] -; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i8 [[POW2_OR_ZERO]], -1 +; CHECK-NEXT: [[CMP:%.*]] = icmp ne i8 [[X:%.*]], -128 ; CHECK-NEXT: ret i1 [[CMP]] ; %neg = sub i8 0, %x @@ -78,9 +68,7 @@ define i1 @pow2_or_zero2_commute(i8 %A) { ; CHECK-LABEL: @pow2_or_zero2_commute( ; CHECK-NEXT: [[X:%.*]] = mul i8 [[A:%.*]], 42 -; CHECK-NEXT: [[NEG:%.*]] = sub i8 0, [[X]] -; CHECK-NEXT: [[POW2_OR_ZERO:%.*]] = and i8 [[X]], [[NEG]] -; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i8 [[POW2_OR_ZERO]], -1 +; CHECK-NEXT: [[CMP:%.*]] = icmp ne i8 [[X]], -128 ; CHECK-NEXT: ret i1 [[CMP]] ; %x = mul i8 42, %A ; thwart complexity-based canonicalization @@ -92,9 +80,7 @@ define <2 x i1> @pow2_or_zero2_vec(<2 x i8> %x) { ; CHECK-LABEL: @pow2_or_zero2_vec( -; CHECK-NEXT: [[NEG:%.*]] = sub <2 x i8> zeroinitializer, [[X:%.*]] -; CHECK-NEXT: [[POW2_OR_ZERO:%.*]] = and <2 x i8> [[NEG]], [[X]] -; CHECK-NEXT: [[CMP:%.*]] = icmp sgt <2 x i8> [[POW2_OR_ZERO]], +; CHECK-NEXT: [[CMP:%.*]] = icmp ne <2 x i8> [[X:%.*]], ; CHECK-NEXT: ret <2 x i1> [[CMP]] ; %neg = sub <2 x i8> , %x @@ -106,9 +92,7 @@ define <2 x i1> @pow2_or_zero2_vec_commute(<2 x i8> %A) { ; CHECK-LABEL: @pow2_or_zero2_vec_commute( ; CHECK-NEXT: [[X:%.*]] = mul <2 x i8> [[A:%.*]], -; CHECK-NEXT: [[NEG:%.*]] = sub <2 x i8> zeroinitializer, [[X]] -; CHECK-NEXT: [[POW2_OR_ZERO:%.*]] = and <2 x i8> [[X]], [[NEG]] -; CHECK-NEXT: [[CMP:%.*]] = icmp sgt <2 x i8> [[POW2_OR_ZERO]], +; CHECK-NEXT: [[CMP:%.*]] = icmp ne <2 x i8> [[X]], ; CHECK-NEXT: ret <2 x i1> [[CMP]] ; %x = mul <2 x i8> , %A ; thwart complexity-based canonicalization diff --git a/llvm/test/Transforms/InstCombine/minmax-of-xor-x.ll b/llvm/test/Transforms/InstCombine/minmax-of-xor-x.ll --- a/llvm/test/Transforms/InstCombine/minmax-of-xor-x.ll +++ b/llvm/test/Transforms/InstCombine/minmax-of-xor-x.ll @@ -134,9 +134,7 @@ define i8 @smax_xor_pow2_neg(i8 %x, i8 %y) { ; CHECK-LABEL: @smax_xor_pow2_neg( -; CHECK-NEXT: [[NY:%.*]] = sub i8 0, [[Y:%.*]] -; CHECK-NEXT: [[YP2:%.*]] = and i8 [[NY]], [[Y]] -; CHECK-NEXT: [[CMP:%.*]] = icmp slt i8 [[YP2]], 0 +; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[Y:%.*]], -128 ; CHECK-NEXT: br i1 [[CMP]], label [[NEG:%.*]], label [[POS:%.*]] ; CHECK: neg: ; CHECK-NEXT: [[R:%.*]] = and i8 [[X:%.*]], 127