diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp --- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp @@ -1924,6 +1924,20 @@ A->getType()->isIntOrIntVectorTy(1)) return SelectInst::Create(A, Op0, Constant::getNullValue(I.getType())); + // and(ashr(subNSW(X, V), ScalarSizeInBits -1), V) --> V s> X ? V : 0. + { + Value *X, *V; + const APInt *ShAmt; + Type *Ty = I.getType(); + if (match(&I, m_c_And(m_OneUse(m_AShr(m_NSWSub(m_Value(X), m_Value(V)), + m_APInt(ShAmt))), + m_Deferred(V))) && + *ShAmt == Ty->getScalarSizeInBits() - 1) { + Value *NewICmpInst = Builder.CreateICmpSGT(V, X); + return SelectInst::Create(NewICmpInst, V, ConstantInt::getNullValue(Ty)); + } + } + return nullptr; } diff --git a/llvm/test/Transforms/InstCombine/sub-ashr-and-to-icmp-select.ll b/llvm/test/Transforms/InstCombine/sub-ashr-and-to-icmp-select.ll --- a/llvm/test/Transforms/InstCombine/sub-ashr-and-to-icmp-select.ll +++ b/llvm/test/Transforms/InstCombine/sub-ashr-and-to-icmp-select.ll @@ -12,9 +12,8 @@ define i8 @sub_ashr_and_i8(i8 %v, i8 %x) { ; CHECK-LABEL: @sub_ashr_and_i8( -; CHECK-NEXT: [[SUB:%.*]] = sub nsw i8 [[X:%.*]], [[V:%.*]] -; CHECK-NEXT: [[SHR:%.*]] = ashr i8 [[SUB]], 7 -; CHECK-NEXT: [[AND:%.*]] = and i8 [[SHR]], [[V]] +; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i8 [[V:%.*]], [[X:%.*]] +; CHECK-NEXT: [[AND:%.*]] = select i1 [[TMP1]], i8 [[V]], i8 0 ; CHECK-NEXT: ret i8 [[AND]] ; %sub = sub nsw i8 %x, %v @@ -25,9 +24,8 @@ define i16 @sub_ashr_and_i16(i16 %v, i16 %x) { ; CHECK-LABEL: @sub_ashr_and_i16( -; CHECK-NEXT: [[SUB:%.*]] = sub nsw i16 [[X:%.*]], [[V:%.*]] -; CHECK-NEXT: [[SHR:%.*]] = ashr i16 [[SUB]], 15 -; CHECK-NEXT: [[AND:%.*]] = and i16 [[SHR]], [[V]] +; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i16 [[V:%.*]], [[X:%.*]] +; CHECK-NEXT: [[AND:%.*]] = select i1 [[TMP1]], i16 [[V]], i16 0 ; CHECK-NEXT: ret i16 [[AND]] ; @@ -39,9 +37,8 @@ define i32 @sub_ashr_and_i32(i32 %v, i32 %x) { ; CHECK-LABEL: @sub_ashr_and_i32( -; CHECK-NEXT: [[SUB:%.*]] = sub nsw i32 [[X:%.*]], [[V:%.*]] -; CHECK-NEXT: [[SHR:%.*]] = ashr i32 [[SUB]], 31 -; CHECK-NEXT: [[AND:%.*]] = and i32 [[SHR]], [[V]] +; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i32 [[V:%.*]], [[X:%.*]] +; CHECK-NEXT: [[AND:%.*]] = select i1 [[TMP1]], i32 [[V]], i32 0 ; CHECK-NEXT: ret i32 [[AND]] ; %sub = sub nsw i32 %x, %v @@ -52,9 +49,8 @@ define i64 @sub_ashr_and_i64(i64 %v, i64 %x) { ; CHECK-LABEL: @sub_ashr_and_i64( -; CHECK-NEXT: [[SUB:%.*]] = sub nsw i64 [[X:%.*]], [[V:%.*]] -; CHECK-NEXT: [[SHR:%.*]] = ashr i64 [[SUB]], 63 -; CHECK-NEXT: [[AND:%.*]] = and i64 [[SHR]], [[V]] +; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i64 [[V:%.*]], [[X:%.*]] +; CHECK-NEXT: [[AND:%.*]] = select i1 [[TMP1]], i64 [[V]], i64 0 ; CHECK-NEXT: ret i64 [[AND]] ; %sub = sub nsw i64 %x, %v @@ -67,9 +63,8 @@ define i32 @sub_ashr_and_i32_nuw_nsw(i32 %v, i32 %x) { ; CHECK-LABEL: @sub_ashr_and_i32_nuw_nsw( -; CHECK-NEXT: [[SUB:%.*]] = sub nuw nsw i32 [[X:%.*]], [[V:%.*]] -; CHECK-NEXT: [[SHR:%.*]] = ashr i32 [[SUB]], 31 -; CHECK-NEXT: [[AND:%.*]] = and i32 [[SHR]], [[V]] +; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i32 [[V:%.*]], [[X:%.*]] +; CHECK-NEXT: [[AND:%.*]] = select i1 [[TMP1]], i32 [[V]], i32 0 ; CHECK-NEXT: ret i32 [[AND]] ; %sub = sub nuw nsw i32 %x, %v @@ -82,9 +77,8 @@ define i32 @sub_ashr_and_i32_commute(i32 %v, i32 %x) { ; CHECK-LABEL: @sub_ashr_and_i32_commute( -; CHECK-NEXT: [[SUB:%.*]] = sub nsw i32 [[X:%.*]], [[V:%.*]] -; CHECK-NEXT: [[SHR:%.*]] = ashr i32 [[SUB]], 31 -; CHECK-NEXT: [[AND:%.*]] = and i32 [[SHR]], [[V]] +; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i32 [[V:%.*]], [[X:%.*]] +; CHECK-NEXT: [[AND:%.*]] = select i1 [[TMP1]], i32 [[V]], i32 0 ; CHECK-NEXT: ret i32 [[AND]] ; %sub = sub nsw i32 %x, %v @@ -97,9 +91,8 @@ define <4 x i32> @sub_ashr_and_i32_vec(<4 x i32> %v, <4 x i32> %x) { ; CHECK-LABEL: @sub_ashr_and_i32_vec( -; CHECK-NEXT: [[SUB:%.*]] = sub nsw <4 x i32> [[X:%.*]], [[V:%.*]] -; CHECK-NEXT: [[SHR:%.*]] = ashr <4 x i32> [[SUB]], <i32 31, i32 31, i32 31, i32 31> -; CHECK-NEXT: [[AND:%.*]] = and <4 x i32> [[SHR]], [[V]] +; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt <4 x i32> [[V:%.*]], [[X:%.*]] +; CHECK-NEXT: [[AND:%.*]] = select <4 x i1> [[TMP1]], <4 x i32> [[V]], <4 x i32> zeroinitializer ; CHECK-NEXT: ret <4 x i32> [[AND]] ; %sub = sub nsw <4 x i32> %x, %v @@ -110,9 +103,8 @@ define <4 x i32> @sub_ashr_and_i32_vec_nuw_nsw(<4 x i32> %v, <4 x i32> %x) { ; CHECK-LABEL: @sub_ashr_and_i32_vec_nuw_nsw( -; CHECK-NEXT: [[SUB:%.*]] = sub nuw nsw <4 x i32> [[X:%.*]], [[V:%.*]] -; CHECK-NEXT: [[SHR:%.*]] = ashr <4 x i32> [[SUB]], <i32 31, i32 31, i32 31, i32 31> -; CHECK-NEXT: [[AND:%.*]] = and <4 x i32> [[SHR]], [[V]] +; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt <4 x i32> [[V:%.*]], [[X:%.*]] +; CHECK-NEXT: [[AND:%.*]] = select <4 x i1> [[TMP1]], <4 x i32> [[V]], <4 x i32> zeroinitializer ; CHECK-NEXT: ret <4 x i32> [[AND]] ; %sub = sub nuw nsw <4 x i32> %x, %v @@ -123,9 +115,8 @@ define <4 x i32> @sub_ashr_and_i32_vec_commute(<4 x i32> %v, <4 x i32> %x) { ; CHECK-LABEL: @sub_ashr_and_i32_vec_commute( -; CHECK-NEXT: [[SUB:%.*]] = sub nsw <4 x i32> [[X:%.*]], [[V:%.*]] -; CHECK-NEXT: [[SHR:%.*]] = ashr <4 x i32> [[SUB]], <i32 31, i32 31, i32 31, i32 31> -; CHECK-NEXT: [[AND:%.*]] = and <4 x i32> [[SHR]], [[V]] +; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt <4 x i32> [[V:%.*]], [[X:%.*]] +; CHECK-NEXT: [[AND:%.*]] = select <4 x i1> [[TMP1]], <4 x i32> [[V]], <4 x i32> zeroinitializer ; CHECK-NEXT: ret <4 x i32> [[AND]] ; %sub = sub nsw <4 x i32> %x, %v @@ -140,8 +131,8 @@ ; CHECK-LABEL: @sub_ashr_and_i32_extra_use_sub( ; CHECK-NEXT: [[SUB:%.*]] = sub nsw i32 [[X:%.*]], [[V:%.*]] ; CHECK-NEXT: store i32 [[SUB]], i32* [[P:%.*]], align 4 -; CHECK-NEXT: [[SHR:%.*]] = ashr i32 [[SUB]], 31 -; CHECK-NEXT: [[AND:%.*]] = and i32 [[SHR]], [[V]] +; CHECK-NEXT: [[TMP1:%.*]] = icmp slt i32 [[X]], [[V]] +; CHECK-NEXT: [[AND:%.*]] = select i1 [[TMP1]], i32 [[V]], i32 0 ; CHECK-NEXT: ret i32 [[AND]] ; %sub = sub nsw i32 %x, %v @@ -153,9 +144,8 @@ define i32 @sub_ashr_and_i32_extra_use_and(i32 %v, i32 %x, i32* %p) { ; CHECK-LABEL: @sub_ashr_and_i32_extra_use_and( -; CHECK-NEXT: [[SUB:%.*]] = sub nsw i32 [[X:%.*]], [[V:%.*]] -; CHECK-NEXT: [[SHR:%.*]] = ashr i32 [[SUB]], 31 -; CHECK-NEXT: [[AND:%.*]] = and i32 [[SHR]], [[V]] +; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i32 [[V:%.*]], [[X:%.*]] +; CHECK-NEXT: [[AND:%.*]] = select i1 [[TMP1]], i32 [[V]], i32 0 ; CHECK-NEXT: store i32 [[AND]], i32* [[P:%.*]], align 4 ; CHECK-NEXT: ret i32 [[AND]] ;