Index: llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp =================================================================== --- llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp +++ llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -5863,8 +5863,7 @@ /// If one operand of an icmp is effectively a bool (value range of {0,1}), /// then try to reduce patterns based on that limit. -static Instruction *foldICmpUsingBoolRange(ICmpInst &I, - InstCombiner::BuilderTy &Builder) { +Instruction *InstCombinerImpl::foldICmpUsingBoolRange(ICmpInst &I) { Value *X, *Y; ICmpInst::Predicate Pred; @@ -5880,6 +5879,55 @@ Y->getType()->isIntOrIntVectorTy(1) && Pred == ICmpInst::ICMP_ULE) return BinaryOperator::CreateOr(Builder.CreateIsNull(X), Y); + const APInt *CmpC; + if (match(I.getOperand(0), + m_OneUse(m_c_Add(m_ZExt(m_Value(X)), m_SExt(m_Value(Y))))) && + match(I.getOperand(1), m_APInt(CmpC)) && + X->getType()->isIntOrIntVectorTy(1) && + Y->getType()->isIntOrIntVectorTy(1)) { + Pred = I.getPredicate(); + int64_t C = CmpC->getSExtValue(); + if ((C > 0 && Pred == ICmpInst::ICMP_SGT) || + (C < 0 && Pred == ICmpInst::ICMP_SLT)) + return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType())); + if ((C > 1 && Pred == ICmpInst::ICMP_SLT) || + (C < -1 && Pred == ICmpInst::ICMP_SGT)) + return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType())); + + // canonicalize predicate to eq/ne + if ((C == 0 && Pred == ICmpInst::ICMP_SLT) || + (C != 0 && C != -1 && Pred == ICmpInst::ICMP_UGT)) { + // x s< 0 in [-1, 1] --> x == -1 + // x u> 1(or any const !=0 !=-1) in [-1, 1] --> x == -1 + C = -1; + Pred = ICmpInst::ICMP_EQ; + } else if ((C == -1 && Pred == ICmpInst::ICMP_SGT) || + (C != 0 && C != 1 && Pred == ICmpInst::ICMP_ULT)) { + // x s> -1 in [-1, 1] --> x != -1 + // x u< -1 in [-1, 1] --> x != -1 + Pred = ICmpInst::ICMP_NE; + } else if (C == 0 && Pred == ICmpInst::ICMP_SGT) { + // x s> 0 in [-1, 1] --> x == 1 + C = 1; + Pred = ICmpInst::ICMP_EQ; + } else if (C == 1 && Pred == ICmpInst::ICMP_SLT) { + // x s< 1 in [-1, 1] --> x != 1 + Pred = ICmpInst::ICMP_NE; + } + + if (C == -1) { + if (Pred == ICmpInst::ICMP_EQ) + return BinaryOperator::CreateAnd(Builder.CreateNot(X), Y); + if (Pred == ICmpInst::ICMP_NE) + return BinaryOperator::CreateOr(X, Builder.CreateNot(Y)); + } else if (C == 1) { + if (Pred == ICmpInst::ICMP_EQ) + return BinaryOperator::CreateAnd(X, Builder.CreateNot(Y)); + if (Pred == ICmpInst::ICMP_NE) + return BinaryOperator::CreateOr(Builder.CreateNot(X), Y); + } + } + return nullptr; } @@ -6335,7 +6383,7 @@ if (Instruction *Res = foldICmpWithDominatingICmp(I)) return Res; - if (Instruction *Res = foldICmpUsingBoolRange(I, Builder)) + if (Instruction *Res = foldICmpUsingBoolRange(I)) return Res; if (Instruction *Res = foldICmpUsingKnownBits(I)) Index: llvm/lib/Transforms/InstCombine/InstCombineInternal.h =================================================================== --- llvm/lib/Transforms/InstCombine/InstCombineInternal.h +++ llvm/lib/Transforms/InstCombine/InstCombineInternal.h @@ -565,6 +565,7 @@ Instruction *foldICmpUsingKnownBits(ICmpInst &Cmp); Instruction *foldICmpWithDominatingICmp(ICmpInst &Cmp); Instruction *foldICmpWithConstant(ICmpInst &Cmp); + Instruction *foldICmpUsingBoolRange(ICmpInst &I); Instruction *foldICmpInstWithConstant(ICmpInst &Cmp); Instruction *foldICmpInstWithConstantNotInt(ICmpInst &Cmp); Instruction *foldICmpInstWithConstantAllowUndef(ICmpInst &Cmp, Index: llvm/test/Transforms/InstCombine/icmp-range.ll =================================================================== --- llvm/test/Transforms/InstCombine/icmp-range.ll +++ llvm/test/Transforms/InstCombine/icmp-range.ll @@ -629,12 +629,64 @@ ret i1 %r } +; (zext i1 a) + (sext i1 b)) s< -1 --> false + +define i1 @zext_sext_add_icmp_slt_minus1(i1 %a, i1 %b) { +; CHECK-LABEL: @zext_sext_add_icmp_slt_minus1( +; CHECK-NEXT: ret i1 false +; + %zext.a = zext i1 %a to i8 + %sext.b = sext i1 %b to i8 + %add = add i8 %zext.a, %sext.b + %r = icmp slt i8 %add, -1 + ret i1 %r +} + +; (zext i1 a) + (sext i1 b)) s> 1 --> false + +define i1 @zext_sext_add_icmp_sgt_1(i1 %a, i1 %b) { +; CHECK-LABEL: @zext_sext_add_icmp_sgt_1( +; CHECK-NEXT: ret i1 false +; + %zext.a = zext i1 %a to i8 + %sext.b = sext i1 %b to i8 + %add = add i8 %zext.a, %sext.b + %r = icmp sgt i8 %add, 1 + ret i1 %r +} + +; (zext i1 a) + (sext i1 b)) s> -2 --> true + +define i1 @zext_sext_add_icmp_sgt_minus2(i1 %a, i1 %b) { +; CHECK-LABEL: @zext_sext_add_icmp_sgt_minus2( +; CHECK-NEXT: ret i1 true +; + %zext.a = zext i1 %a to i8 + %sext.b = sext i1 %b to i8 + %add = add i8 %zext.a, %sext.b + %r = icmp sgt i8 %add, -2 + ret i1 %r +} + +; (zext i1 a) + (sext i1 b)) s< 2 --> true + +define i1 @zext_sext_add_icmp_slt_2(i1 %a, i1 %b) { +; CHECK-LABEL: @zext_sext_add_icmp_slt_2( +; CHECK-NEXT: ret i1 true +; + %zext.a = zext i1 %a to i8 + %sext.b = sext i1 %b to i8 + %add = add i8 %zext.a, %sext.b + %r = icmp slt i8 %add, 2 + ret i1 %r +} + +; (zext i1 a) + (sext i1 b)) == -1 --> ~a & b + define i1 @zext_sext_add_icmp_eq_minus1(i1 %a, i1 %b) { ; CHECK-LABEL: @zext_sext_add_icmp_eq_minus1( -; CHECK-NEXT: [[ZEXT_A:%.*]] = zext i1 [[A:%.*]] to i8 -; CHECK-NEXT: [[SEXT_B:%.*]] = sext i1 [[B:%.*]] to i8 -; CHECK-NEXT: [[ADD:%.*]] = add nsw i8 [[ZEXT_A]], [[SEXT_B]] -; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[ADD]], -1 +; CHECK-NEXT: [[TMP1:%.*]] = xor i1 [[A:%.*]], true +; CHECK-NEXT: [[R:%.*]] = and i1 [[TMP1]], [[B:%.*]] ; CHECK-NEXT: ret i1 [[R]] ; %zext.a = zext i1 %a to i8 @@ -644,12 +696,13 @@ ret i1 %r } + +; (zext i1 a) + (sext i1 b)) != -1 --> a | ~b + define i1 @zext_sext_add_icmp_ne_minus1(i1 %a, i1 %b) { ; CHECK-LABEL: @zext_sext_add_icmp_ne_minus1( -; CHECK-NEXT: [[ZEXT_A:%.*]] = zext i1 [[A:%.*]] to i8 -; CHECK-NEXT: [[SEXT_B:%.*]] = sext i1 [[B:%.*]] to i8 -; CHECK-NEXT: [[ADD:%.*]] = add nsw i8 [[ZEXT_A]], [[SEXT_B]] -; CHECK-NEXT: [[R:%.*]] = icmp ne i8 [[ADD]], -1 +; CHECK-NEXT: [[TMP1:%.*]] = xor i1 [[B:%.*]], true +; CHECK-NEXT: [[R:%.*]] = or i1 [[TMP1]], [[A:%.*]] ; CHECK-NEXT: ret i1 [[R]] ; %zext.a = zext i1 %a to i8 @@ -659,10 +712,12 @@ ret i1 %r } +; (zext i1 a) + (sext i1 b)) s> -1 --> a | ~b + define i1 @zext_sext_add_icmp_sgt_minus1(i1 %a, i1 %b) { ; CHECK-LABEL: @zext_sext_add_icmp_sgt_minus1( -; CHECK-NEXT: [[B_NOT:%.*]] = xor i1 [[B:%.*]], true -; CHECK-NEXT: [[R:%.*]] = or i1 [[B_NOT]], [[A:%.*]] +; CHECK-NEXT: [[TMP1:%.*]] = xor i1 [[B:%.*]], true +; CHECK-NEXT: [[R:%.*]] = or i1 [[TMP1]], [[A:%.*]] ; CHECK-NEXT: ret i1 [[R]] ; %zext.a = zext i1 %a to i8 @@ -672,12 +727,12 @@ ret i1 %r } +; (zext i1 a) + (sext i1 b)) u< -1 --> a | ~b + define i1 @zext_sext_add_icmp_ult_minus1(i1 %a, i1 %b) { ; CHECK-LABEL: @zext_sext_add_icmp_ult_minus1( -; CHECK-NEXT: [[ZEXT_A:%.*]] = zext i1 [[A:%.*]] to i8 -; CHECK-NEXT: [[SEXT_B:%.*]] = sext i1 [[B:%.*]] to i8 -; CHECK-NEXT: [[ADD:%.*]] = add nsw i8 [[ZEXT_A]], [[SEXT_B]] -; CHECK-NEXT: [[R:%.*]] = icmp ne i8 [[ADD]], -1 +; CHECK-NEXT: [[TMP1:%.*]] = xor i1 [[B:%.*]], true +; CHECK-NEXT: [[R:%.*]] = or i1 [[TMP1]], [[A:%.*]] ; CHECK-NEXT: ret i1 [[R]] ; %zext.a = zext i1 %a to i8 @@ -687,12 +742,12 @@ ret i1 %r } +; (zext i1 a) + (sext i1 b)) s> 0 --> a & ~b + define i1 @zext_sext_add_icmp_sgt_0(i1 %a, i1 %b) { ; CHECK-LABEL: @zext_sext_add_icmp_sgt_0( -; CHECK-NEXT: [[ZEXT_A:%.*]] = zext i1 [[A:%.*]] to i8 -; CHECK-NEXT: [[SEXT_B:%.*]] = sext i1 [[B:%.*]] to i8 -; CHECK-NEXT: [[ADD:%.*]] = add nsw i8 [[ZEXT_A]], [[SEXT_B]] -; CHECK-NEXT: [[R:%.*]] = icmp sgt i8 [[ADD]], 0 +; CHECK-NEXT: [[TMP1:%.*]] = xor i1 [[B:%.*]], true +; CHECK-NEXT: [[R:%.*]] = and i1 [[TMP1]], [[A:%.*]] ; CHECK-NEXT: ret i1 [[R]] ; %zext.a = zext i1 %a to i8 @@ -702,11 +757,13 @@ ret i1 %r } +; (zext i1 a) + (sext i1 b)) s< 0 --> ~a & b + define i1 @zext_sext_add_icmp_slt_0(i1 %a, i1 %b) { ; CHECK-LABEL: @zext_sext_add_icmp_slt_0( ; CHECK-NEXT: [[TMP1:%.*]] = xor i1 [[A:%.*]], true -; CHECK-NEXT: [[TMP2:%.*]] = and i1 [[TMP1]], [[B:%.*]] -; CHECK-NEXT: ret i1 [[TMP2]] +; CHECK-NEXT: [[R:%.*]] = and i1 [[TMP1]], [[B:%.*]] +; CHECK-NEXT: ret i1 [[R]] ; %zext.a = zext i1 %a to i8 %sext.b = sext i1 %b to i8 @@ -715,12 +772,12 @@ ret i1 %r } +; (zext i1 a) + (sext i1 b)) == 1 --> a & ~b + define i1 @zext_sext_add_icmp_eq_1(i1 %a, i1 %b) { ; CHECK-LABEL: @zext_sext_add_icmp_eq_1( -; CHECK-NEXT: [[ZEXT_A:%.*]] = zext i1 [[A:%.*]] to i8 -; CHECK-NEXT: [[SEXT_B:%.*]] = sext i1 [[B:%.*]] to i8 -; CHECK-NEXT: [[ADD:%.*]] = add nsw i8 [[ZEXT_A]], [[SEXT_B]] -; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[ADD]], 1 +; CHECK-NEXT: [[TMP1:%.*]] = xor i1 [[B:%.*]], true +; CHECK-NEXT: [[R:%.*]] = and i1 [[TMP1]], [[A:%.*]] ; CHECK-NEXT: ret i1 [[R]] ; %zext.a = zext i1 %a to i8 @@ -730,12 +787,12 @@ ret i1 %r } +; (zext i1 a) + (sext i1 b)) != 1 --> ~a | b + define i1 @zext_sext_add_icmp_ne_1(i1 %a, i1 %b) { ; CHECK-LABEL: @zext_sext_add_icmp_ne_1( -; CHECK-NEXT: [[ZEXT_A:%.*]] = zext i1 [[A:%.*]] to i8 -; CHECK-NEXT: [[SEXT_B:%.*]] = sext i1 [[B:%.*]] to i8 -; CHECK-NEXT: [[ADD:%.*]] = add nsw i8 [[ZEXT_A]], [[SEXT_B]] -; CHECK-NEXT: [[R:%.*]] = icmp ne i8 [[ADD]], 1 +; CHECK-NEXT: [[TMP1:%.*]] = xor i1 [[A:%.*]], true +; CHECK-NEXT: [[R:%.*]] = or i1 [[TMP1]], [[B:%.*]] ; CHECK-NEXT: ret i1 [[R]] ; %zext.a = zext i1 %a to i8 @@ -745,12 +802,12 @@ ret i1 %r } +; (zext i1 a) + (sext i1 b)) s< 1 --> ~a | b + define i1 @zext_sext_add_icmp_slt_1(i1 %a, i1 %b) { ; CHECK-LABEL: @zext_sext_add_icmp_slt_1( -; CHECK-NEXT: [[ZEXT_A:%.*]] = zext i1 [[A:%.*]] to i8 -; CHECK-NEXT: [[SEXT_B:%.*]] = sext i1 [[B:%.*]] to i8 -; CHECK-NEXT: [[ADD:%.*]] = add nsw i8 [[ZEXT_A]], [[SEXT_B]] -; CHECK-NEXT: [[R:%.*]] = icmp slt i8 [[ADD]], 1 +; CHECK-NEXT: [[TMP1:%.*]] = xor i1 [[A:%.*]], true +; CHECK-NEXT: [[R:%.*]] = or i1 [[TMP1]], [[B:%.*]] ; CHECK-NEXT: ret i1 [[R]] ; %zext.a = zext i1 %a to i8 @@ -760,11 +817,13 @@ ret i1 %r } +; (zext i1 a) + (sext i1 b)) u> 1 --> ~a & b + define i1 @zext_sext_add_icmp_ugt_1(i1 %a, i1 %b) { ; CHECK-LABEL: @zext_sext_add_icmp_ugt_1( ; CHECK-NEXT: [[TMP1:%.*]] = xor i1 [[A:%.*]], true -; CHECK-NEXT: [[TMP2:%.*]] = and i1 [[TMP1]], [[B:%.*]] -; CHECK-NEXT: ret i1 [[TMP2]] +; CHECK-NEXT: [[R:%.*]] = and i1 [[TMP1]], [[B:%.*]] +; CHECK-NEXT: ret i1 [[R]] ; %zext.a = zext i1 %a to i8 %sext.b = sext i1 %b to i8 @@ -775,10 +834,8 @@ define <2 x i1> @vector_zext_sext_add_icmp_slt_1(<2 x i1> %a, <2 x i1> %b) { ; CHECK-LABEL: @vector_zext_sext_add_icmp_slt_1( -; CHECK-NEXT: [[ZEXT_A:%.*]] = zext <2 x i1> [[A:%.*]] to <2 x i8> -; CHECK-NEXT: [[SEXT_B:%.*]] = sext <2 x i1> [[B:%.*]] to <2 x i8> -; CHECK-NEXT: [[ADD:%.*]] = add nsw <2 x i8> [[ZEXT_A]], [[SEXT_B]] -; CHECK-NEXT: [[R:%.*]] = icmp slt <2 x i8> [[ADD]], +; CHECK-NEXT: [[TMP1:%.*]] = xor <2 x i1> [[A:%.*]], +; CHECK-NEXT: [[R:%.*]] = or <2 x i1> [[TMP1]], [[B:%.*]] ; CHECK-NEXT: ret <2 x i1> [[R]] ; %zext.a = zext <2 x i1> %a to <2 x i8> @@ -803,6 +860,8 @@ ret <2 x i1> %r } +; Negative test, more than one use for icmp LHS + define i1 @zext_sext_add_icmp_slt_1_no_oneuse(i1 %a, i1 %b) { ; CHECK-LABEL: @zext_sext_add_icmp_slt_1_no_oneuse( ; CHECK-NEXT: [[ZEXT_A:%.*]] = zext i1 [[A:%.*]] to i8 @@ -820,6 +879,8 @@ ret i1 %r } +; Negative test, icmp RHS is not a constant + define i1 @zext_sext_add_icmp_slt_1_rhs_not_const(i1 %a, i1 %b, i8 %c) { ; CHECK-LABEL: @zext_sext_add_icmp_slt_1_rhs_not_const( ; CHECK-NEXT: [[ZEXT_A:%.*]] = zext i1 [[A:%.*]] to i8 @@ -835,6 +896,8 @@ ret i1 %r } +; Negative test, ext source is not i1 + define i1 @zext_sext_add_icmp_slt_1_type_not_i1(i2 %a, i1 %b) { ; CHECK-LABEL: @zext_sext_add_icmp_slt_1_type_not_i1( ; CHECK-NEXT: [[ZEXT_A:%.*]] = zext i2 [[A:%.*]] to i8