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 @@ -2978,34 +2978,48 @@ return nullptr; } -// (icmp eq X, 0) | (icmp ult Other, X) -> (icmp ule Other, X-1) -// (icmp ne X, 0) & (icmp uge Other, X) -> (icmp ugt Other, X-1) -static Value *foldAndOrOfICmpEqZeroAndICmp(ICmpInst *LHS, ICmpInst *RHS, - bool IsAnd, bool IsLogical, - IRBuilderBase &Builder) { +// (icmp eq X, C) | (icmp ult Other, (X - C)) -> (icmp ule Other, (X - (C + 1))) +// (icmp ne X, C) & (icmp uge Other, (X - C)) -> (icmp ugt Other, (X - (C + 1))) +static Value *foldAndOrOfICmpEqConstantAndICmp(ICmpInst *LHS, ICmpInst *RHS, + bool IsAnd, bool IsLogical, + IRBuilderBase &Builder) { + Value *LHS0 = LHS->getOperand(0); + Value *RHS0 = RHS->getOperand(0); + Value *RHS1 = RHS->getOperand(1); + ICmpInst::Predicate LPred = IsAnd ? LHS->getInversePredicate() : LHS->getPredicate(); ICmpInst::Predicate RPred = IsAnd ? RHS->getInversePredicate() : RHS->getPredicate(); - Value *LHS0 = LHS->getOperand(0); - if (LPred != ICmpInst::ICMP_EQ || !match(LHS->getOperand(1), m_Zero()) || + + const APInt *CInt; + if (LPred != ICmpInst::ICMP_EQ || !match(LHS->getOperand(1), m_APInt(CInt)) || !LHS0->getType()->isIntOrIntVectorTy() || !(LHS->hasOneUse() || RHS->hasOneUse())) return nullptr; + Constant *C = ConstantInt::get(LHS0->getType(), *CInt); + + auto MatchRHSOp = [LHS0, CInt](const Value *RHSOp) { + return match(RHSOp, m_Add(m_Specific(LHS0), m_SpecificInt(-*CInt))) || + (CInt->isZero() && RHSOp == LHS0); + }; + Value *Other; - if (RPred == ICmpInst::ICMP_ULT && RHS->getOperand(1) == LHS0) - Other = RHS->getOperand(0); - else if (RPred == ICmpInst::ICMP_UGT && RHS->getOperand(0) == LHS0) - Other = RHS->getOperand(1); + if (RPred == ICmpInst::ICMP_ULT && MatchRHSOp(RHS1)) + Other = RHS0; + else if (RPred == ICmpInst::ICMP_UGT && MatchRHSOp(RHS0)) + Other = RHS1; else return nullptr; if (IsLogical) Other = Builder.CreateFreeze(Other); + return Builder.CreateICmp( IsAnd ? ICmpInst::ICMP_ULT : ICmpInst::ICMP_UGE, - Builder.CreateAdd(LHS0, Constant::getAllOnesValue(LHS0->getType())), + Builder.CreateSub( + LHS0, Builder.CreateAdd(C, ConstantInt::get(LHS0->getType(), 1))), Other); } @@ -3052,12 +3066,12 @@ return V; if (Value *V = - foldAndOrOfICmpEqZeroAndICmp(LHS, RHS, IsAnd, IsLogical, Builder)) + foldAndOrOfICmpEqConstantAndICmp(LHS, RHS, IsAnd, IsLogical, Builder)) return V; // We can treat logical like bitwise here, because both operands are used on // the LHS, and as such poison from both will propagate. - if (Value *V = foldAndOrOfICmpEqZeroAndICmp(RHS, LHS, IsAnd, - /*IsLogical*/ false, Builder)) + if (Value *V = foldAndOrOfICmpEqConstantAndICmp(RHS, LHS, IsAnd, + /*IsLogical*/ false, Builder)) return V; if (Value *V = diff --git a/llvm/test/Transforms/InstCombine/and-or-icmp-const-icmp.ll b/llvm/test/Transforms/InstCombine/and-or-icmp-const-icmp.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Transforms/InstCombine/and-or-icmp-const-icmp.ll @@ -0,0 +1,551 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 3 +; RUN: opt < %s -passes=instcombine -S | FileCheck %s + +; Tests for foldAndOrOfICmpEqConstantAndICmp +; https://github.com/llvm/llvm-project/issues/63749 + +; ============================================================================== +; (icmp eq X, C) | (icmp ult Other, (X - C)) -> (icmp ule Other, (X - (C + 1))) +; ============================================================================== + +; ============================================================================== +; Basic tests +; ============================================================================== +define i1 @eq_basic(i8 %x, i8 %y) { +; CHECK-LABEL: define i1 @eq_basic +; CHECK-SAME: (i8 [[X:%.*]], i8 [[Y:%.*]]) { +; CHECK-NEXT: [[TMP1:%.*]] = add i8 [[X]], -1 +; CHECK-NEXT: [[OR:%.*]] = icmp uge i8 [[TMP1]], [[Y]] +; CHECK-NEXT: ret i1 [[OR]] +; + %c1 = icmp eq i8 %x, 0 + %c2 = icmp ugt i8 %x, %y + %or = or i1 %c1, %c2 + ret i1 %or +} + +define i1 @eq_basic_equal_5(i8 %x, i8 %y) { +; CHECK-LABEL: define i1 @eq_basic_equal_5 +; CHECK-SAME: (i8 [[X:%.*]], i8 [[Y:%.*]]) { +; CHECK-NEXT: [[TMP1:%.*]] = add i8 [[X]], -6 +; CHECK-NEXT: [[OR:%.*]] = icmp uge i8 [[TMP1]], [[Y]] +; CHECK-NEXT: ret i1 [[OR]] +; + %sub = sub i8 %x, 5 + %c1 = icmp eq i8 %x, 5 + %c2 = icmp ugt i8 %sub, %y + %or = or i1 %c1, %c2 + ret i1 %or +} + +define i1 @eq_basic_equal_minus_1(i8 %x, i8 %y) { +; CHECK-LABEL: define i1 @eq_basic_equal_minus_1 +; CHECK-SAME: (i8 [[X:%.*]], i8 [[Y:%.*]]) { +; CHECK-NEXT: [[OR:%.*]] = icmp uge i8 [[X]], [[Y]] +; CHECK-NEXT: ret i1 [[OR]] +; + %add = add i8 %x, 1 + %c1 = icmp eq i8 %x, -1 + %c2 = icmp ugt i8 %add, %y + %or = or i1 %c1, %c2 + ret i1 %or +} + +define i1 @eq_basic_equal_minus_7(i8 %x, i8 %y) { +; CHECK-LABEL: define i1 @eq_basic_equal_minus_7 +; CHECK-SAME: (i8 [[X:%.*]], i8 [[Y:%.*]]) { +; CHECK-NEXT: [[TMP1:%.*]] = add i8 [[X]], 6 +; CHECK-NEXT: [[OR:%.*]] = icmp uge i8 [[TMP1]], [[Y]] +; CHECK-NEXT: ret i1 [[OR]] +; + %add = add i8 %x, 7 + %c1 = icmp eq i8 %x, -7 + %c2 = icmp ugt i8 %add, %y + %or = or i1 %c1, %c2 + ret i1 %or +} + +define i1 @eq_basic_unequal1(i8 %x, i8 %y) { +; CHECK-LABEL: define i1 @eq_basic_unequal1 +; CHECK-SAME: (i8 [[X:%.*]], i8 [[Y:%.*]]) { +; CHECK-NEXT: [[SUB:%.*]] = add i8 [[X]], -5 +; CHECK-NEXT: [[C1:%.*]] = icmp eq i8 [[X]], 6 +; CHECK-NEXT: [[C2:%.*]] = icmp ugt i8 [[SUB]], [[Y]] +; CHECK-NEXT: [[OR:%.*]] = or i1 [[C1]], [[C2]] +; CHECK-NEXT: ret i1 [[OR]] +; + %sub = sub i8 %x, 5 + %c1 = icmp eq i8 %x, 6 + %c2 = icmp ugt i8 %sub, %y + %or = or i1 %c1, %c2 + ret i1 %or +} + +define i1 @eq_basic_unequal2(i8 %x, i8 %y) { +; CHECK-LABEL: define i1 @eq_basic_unequal2 +; CHECK-SAME: (i8 [[X:%.*]], i8 [[Y:%.*]]) { +; CHECK-NEXT: [[ADD:%.*]] = add i8 [[X]], 7 +; CHECK-NEXT: [[C1:%.*]] = icmp eq i8 [[X]], -4 +; CHECK-NEXT: [[C2:%.*]] = icmp ugt i8 [[ADD]], [[Y]] +; CHECK-NEXT: [[OR:%.*]] = or i1 [[C1]], [[C2]] +; CHECK-NEXT: ret i1 [[OR]] +; + %add = add i8 %x, 7 + %c1 = icmp eq i8 %x, -4 + %c2 = icmp ugt i8 %add, %y + %or = or i1 %c1, %c2 + ret i1 %or +} + +; ============================================================================== +; Tests with multiple uses +; ============================================================================== +define i1 @eq_multi_c1(i8 %x, i8 %y) { +; CHECK-LABEL: define i1 @eq_multi_c1 +; CHECK-SAME: (i8 [[X:%.*]], i8 [[Y:%.*]]) { +; CHECK-NEXT: [[C1:%.*]] = icmp eq i8 [[X]], 0 +; CHECK-NEXT: [[TMP1:%.*]] = add i8 [[X]], -1 +; CHECK-NEXT: [[OR:%.*]] = icmp uge i8 [[TMP1]], [[Y]] +; CHECK-NEXT: call void @use(i1 [[C1]]) +; CHECK-NEXT: ret i1 [[OR]] +; + %c1 = icmp eq i8 %x, 0 + %c2 = icmp ugt i8 %x, %y + %or = or i1 %c1, %c2 + call void @use(i1 %c1) + ret i1 %or +} + +define i1 @eq_multi_c2(i8 %x, i8 %y) { +; CHECK-LABEL: define i1 @eq_multi_c2 +; CHECK-SAME: (i8 [[X:%.*]], i8 [[Y:%.*]]) { +; CHECK-NEXT: [[C2:%.*]] = icmp ugt i8 [[X]], [[Y]] +; CHECK-NEXT: [[TMP1:%.*]] = add i8 [[X]], -1 +; CHECK-NEXT: [[OR:%.*]] = icmp uge i8 [[TMP1]], [[Y]] +; CHECK-NEXT: call void @use(i1 [[C2]]) +; CHECK-NEXT: ret i1 [[OR]] +; + %c1 = icmp eq i8 %x, 0 + %c2 = icmp ugt i8 %x, %y + %or = or i1 %c1, %c2 + call void @use(i1 %c2) + ret i1 %or +} + +; ============================================================================== +; Tests with vector types +; ============================================================================== +define <2 x i1> @eq_vector(<2 x i8> %x, <2 x i8> %y) { +; CHECK-LABEL: define <2 x i1> @eq_vector +; CHECK-SAME: (<2 x i8> [[X:%.*]], <2 x i8> [[Y:%.*]]) { +; CHECK-NEXT: [[TMP1:%.*]] = add <2 x i8> [[X]], +; CHECK-NEXT: [[OR:%.*]] = icmp uge <2 x i8> [[TMP1]], [[Y]] +; CHECK-NEXT: ret <2 x i1> [[OR]] +; + %c1 = icmp eq <2 x i8> %x, + %c2 = icmp ugt <2 x i8> %x, %y + %or = or <2 x i1> %c1, %c2 + ret <2 x i1> %or +} + +define <2 x i1> @eq_vector_equal_5(<2 x i8> %x, <2 x i8> %y) { +; CHECK-LABEL: define <2 x i1> @eq_vector_equal_5 +; CHECK-SAME: (<2 x i8> [[X:%.*]], <2 x i8> [[Y:%.*]]) { +; CHECK-NEXT: [[TMP1:%.*]] = add <2 x i8> [[X]], +; CHECK-NEXT: [[OR:%.*]] = icmp uge <2 x i8> [[TMP1]], [[Y]] +; CHECK-NEXT: ret <2 x i1> [[OR]] +; + %sub = sub <2 x i8> %x, + %c1 = icmp eq <2 x i8> %x, + %c2 = icmp ugt <2 x i8> %sub, %y + %or = or <2 x i1> %c1, %c2 + ret <2 x i1> %or +} + +define <2 x i1> @eq_vector_equal_minus_1(<2 x i8> %x, <2 x i8> %y) { +; CHECK-LABEL: define <2 x i1> @eq_vector_equal_minus_1 +; CHECK-SAME: (<2 x i8> [[X:%.*]], <2 x i8> [[Y:%.*]]) { +; CHECK-NEXT: [[OR:%.*]] = icmp uge <2 x i8> [[X]], [[Y]] +; CHECK-NEXT: ret <2 x i1> [[OR]] +; + %add = add <2 x i8> %x, + %c1 = icmp eq <2 x i8> %x, + %c2 = icmp ugt <2 x i8> %add, %y + %or = or <2 x i1> %c1, %c2 + ret <2 x i1> %or +} + +define <2 x i1> @eq_vector_equal_minus_7(<2 x i8> %x, <2 x i8> %y) { +; CHECK-LABEL: define <2 x i1> @eq_vector_equal_minus_7 +; CHECK-SAME: (<2 x i8> [[X:%.*]], <2 x i8> [[Y:%.*]]) { +; CHECK-NEXT: [[TMP1:%.*]] = add <2 x i8> [[X]], +; CHECK-NEXT: [[OR:%.*]] = icmp uge <2 x i8> [[TMP1]], [[Y]] +; CHECK-NEXT: ret <2 x i1> [[OR]] +; + %add = add <2 x i8> %x, + %c1 = icmp eq <2 x i8> %x, + %c2 = icmp ugt <2 x i8> %add, %y + %or = or <2 x i1> %c1, %c2 + ret <2 x i1> %or +} + +define <2 x i1> @eq_vector_unequal1(<2 x i8> %x, <2 x i8> %y) { +; CHECK-LABEL: define <2 x i1> @eq_vector_unequal1 +; CHECK-SAME: (<2 x i8> [[X:%.*]], <2 x i8> [[Y:%.*]]) { +; CHECK-NEXT: [[SUB:%.*]] = add <2 x i8> [[X]], +; CHECK-NEXT: [[C1:%.*]] = icmp eq <2 x i8> [[X]], +; CHECK-NEXT: [[C2:%.*]] = icmp ugt <2 x i8> [[SUB]], [[Y]] +; CHECK-NEXT: [[OR:%.*]] = or <2 x i1> [[C1]], [[C2]] +; CHECK-NEXT: ret <2 x i1> [[OR]] +; + %sub = sub <2 x i8> %x, + %c1 = icmp eq <2 x i8> %x, + %c2 = icmp ugt <2 x i8> %sub, %y + %or = or <2 x i1> %c1, %c2 + ret <2 x i1> %or +} + +define <2 x i1> @eq_vector_unequal2(<2 x i8> %x, <2 x i8> %y) { +; CHECK-LABEL: define <2 x i1> @eq_vector_unequal2 +; CHECK-SAME: (<2 x i8> [[X:%.*]], <2 x i8> [[Y:%.*]]) { +; CHECK-NEXT: [[ADD:%.*]] = add <2 x i8> [[X]], +; CHECK-NEXT: [[C1:%.*]] = icmp eq <2 x i8> [[X]], +; CHECK-NEXT: [[C2:%.*]] = icmp ugt <2 x i8> [[ADD]], [[Y]] +; CHECK-NEXT: [[OR:%.*]] = or <2 x i1> [[C1]], [[C2]] +; CHECK-NEXT: ret <2 x i1> [[OR]] +; + %add = add <2 x i8> %x, + %c1 = icmp eq <2 x i8> %x, + %c2 = icmp ugt <2 x i8> %add, %y + %or = or <2 x i1> %c1, %c2 + ret <2 x i1> %or +} + +; ============================================================================== +; Tests with values commuted +; ============================================================================== +define i1 @eq_commuted(i8 %x, i8 %y) { +; CHECK-LABEL: define i1 @eq_commuted +; CHECK-SAME: (i8 [[X:%.*]], i8 [[Y:%.*]]) { +; CHECK-NEXT: [[TMP1:%.*]] = add i8 [[X]], -1 +; CHECK-NEXT: [[OR:%.*]] = icmp uge i8 [[TMP1]], [[Y]] +; CHECK-NEXT: ret i1 [[OR]] +; + %c1 = icmp eq i8 %x, 0 + %c2 = icmp ult i8 %y, %x + %or = or i1 %c1, %c2 + ret i1 %or +} + +define i1 @eq_commuted_equal_minus_1(i8 %x, i8 %y) { +; CHECK-LABEL: define i1 @eq_commuted_equal_minus_1 +; CHECK-SAME: (i8 [[X:%.*]], i8 [[Y:%.*]]) { +; CHECK-NEXT: [[OR:%.*]] = icmp uge i8 [[X]], [[Y]] +; CHECK-NEXT: ret i1 [[OR]] +; + %add = add i8 %x, 1 + %c1 = icmp eq i8 %x, -1 + %c2 = icmp ult i8 %y, %add + %or = or i1 %c1, %c2 + ret i1 %or +} + +define <2 x i1> @eq_vetor_commuted(<2 x i8> %x, <2 x i8> %y) { +; CHECK-LABEL: define <2 x i1> @eq_vetor_commuted +; CHECK-SAME: (<2 x i8> [[X:%.*]], <2 x i8> [[Y:%.*]]) { +; CHECK-NEXT: [[TMP1:%.*]] = add <2 x i8> [[X]], +; CHECK-NEXT: [[OR:%.*]] = icmp uge <2 x i8> [[TMP1]], [[Y]] +; CHECK-NEXT: ret <2 x i1> [[OR]] +; + %c1 = icmp eq <2 x i8> %x, + %c2 = icmp ult <2 x i8> %y, %x + %or = or <2 x i1> %c1, %c2 + ret <2 x i1> %or +} + +define <2 x i1> @eq_vetor_commuted_equal_minus_1(<2 x i8> %x, <2 x i8> %y) { +; CHECK-LABEL: define <2 x i1> @eq_vetor_commuted_equal_minus_1 +; CHECK-SAME: (<2 x i8> [[X:%.*]], <2 x i8> [[Y:%.*]]) { +; CHECK-NEXT: [[OR:%.*]] = icmp uge <2 x i8> [[X]], [[Y]] +; CHECK-NEXT: ret <2 x i1> [[OR]] +; + %add = add <2 x i8> %x, + %c1 = icmp eq <2 x i8> %x, + %c2 = icmp ult <2 x i8> %y, %add + %or = or <2 x i1> %c1, %c2 + ret <2 x i1> %or +} + +; ============================================================================== +; (icmp ne X, C) & (icmp uge Other, (X - C)) -> (icmp ugt Other, (X - (C + 1))) +; ============================================================================== + +; ============================================================================== +; Basic tests +; ============================================================================== +define i1 @ne_basic(i8 %x, i8 %y) { +; CHECK-LABEL: define i1 @ne_basic +; CHECK-SAME: (i8 [[X:%.*]], i8 [[Y:%.*]]) { +; CHECK-NEXT: [[TMP1:%.*]] = add i8 [[X]], -1 +; CHECK-NEXT: [[AND:%.*]] = icmp ult i8 [[TMP1]], [[Y]] +; CHECK-NEXT: ret i1 [[AND]] +; + %c1 = icmp ne i8 %x, 0 + %c2 = icmp ule i8 %x, %y + %and = and i1 %c1, %c2 + ret i1 %and +} + +define i1 @ne_basic_equal_5(i8 %x, i8 %y) { +; CHECK-LABEL: define i1 @ne_basic_equal_5 +; CHECK-SAME: (i8 [[X:%.*]], i8 [[Y:%.*]]) { +; CHECK-NEXT: [[TMP1:%.*]] = add i8 [[X]], -6 +; CHECK-NEXT: [[AND:%.*]] = icmp ult i8 [[TMP1]], [[Y]] +; CHECK-NEXT: ret i1 [[AND]] +; + %sub = sub i8 %x, 5 + %c1 = icmp ne i8 %x, 5 + %c2 = icmp ule i8 %sub, %y + %and = and i1 %c1, %c2 + ret i1 %and +} + +define i1 @ne_basic_equal_minus_1(i8 %x, i8 %y) { +; CHECK-LABEL: define i1 @ne_basic_equal_minus_1 +; CHECK-SAME: (i8 [[X:%.*]], i8 [[Y:%.*]]) { +; CHECK-NEXT: [[AND:%.*]] = icmp ult i8 [[X]], [[Y]] +; CHECK-NEXT: ret i1 [[AND]] +; + %add = add i8 %x, 1 + %c1 = icmp ne i8 %x, -1 + %c2 = icmp ule i8 %add, %y + %and = and i1 %c1, %c2 + ret i1 %and +} + +define i1 @ne_basic_equal_minus_7(i8 %x, i8 %y) { +; CHECK-LABEL: define i1 @ne_basic_equal_minus_7 +; CHECK-SAME: (i8 [[X:%.*]], i8 [[Y:%.*]]) { +; CHECK-NEXT: [[TMP1:%.*]] = add i8 [[X]], 6 +; CHECK-NEXT: [[AND:%.*]] = icmp ult i8 [[TMP1]], [[Y]] +; CHECK-NEXT: ret i1 [[AND]] +; + %add = add i8 %x, 7 + %c1 = icmp ne i8 %x, -7 + %c2 = icmp ule i8 %add, %y + %and = and i1 %c1, %c2 + ret i1 %and +} + +define i1 @ne_basic_unequal1(i8 %x, i8 %y) { +; CHECK-LABEL: define i1 @ne_basic_unequal1 +; CHECK-SAME: (i8 [[X:%.*]], i8 [[Y:%.*]]) { +; CHECK-NEXT: [[SUB:%.*]] = add i8 [[X]], -5 +; CHECK-NEXT: [[C1:%.*]] = icmp ne i8 [[X]], 6 +; CHECK-NEXT: [[C2:%.*]] = icmp ule i8 [[SUB]], [[Y]] +; CHECK-NEXT: [[AND:%.*]] = and i1 [[C1]], [[C2]] +; CHECK-NEXT: ret i1 [[AND]] +; + %sub = sub i8 %x, 5 + %c1 = icmp ne i8 %x, 6 + %c2 = icmp ule i8 %sub, %y + %and = and i1 %c1, %c2 + ret i1 %and +} + +define i1 @ne_basic_unequal2(i8 %x, i8 %y) { +; CHECK-LABEL: define i1 @ne_basic_unequal2 +; CHECK-SAME: (i8 [[X:%.*]], i8 [[Y:%.*]]) { +; CHECK-NEXT: [[ADD:%.*]] = add i8 [[X]], 7 +; CHECK-NEXT: [[C1:%.*]] = icmp ne i8 [[X]], -4 +; CHECK-NEXT: [[C2:%.*]] = icmp ule i8 [[ADD]], [[Y]] +; CHECK-NEXT: [[AND:%.*]] = and i1 [[C1]], [[C2]] +; CHECK-NEXT: ret i1 [[AND]] +; + %add = add i8 %x, 7 + %c1 = icmp ne i8 %x, -4 + %c2 = icmp ule i8 %add, %y + %and = and i1 %c1, %c2 + ret i1 %and +} + +; ============================================================================== +; Tests with multiple uses +; ============================================================================== +define i1 @ne_multi_c1(i8 %x, i8 %y) { +; CHECK-LABEL: define i1 @ne_multi_c1 +; CHECK-SAME: (i8 [[X:%.*]], i8 [[Y:%.*]]) { +; CHECK-NEXT: [[C1:%.*]] = icmp ne i8 [[X]], 0 +; CHECK-NEXT: [[TMP1:%.*]] = add i8 [[X]], -1 +; CHECK-NEXT: [[AND:%.*]] = icmp ult i8 [[TMP1]], [[Y]] +; CHECK-NEXT: call void @use(i1 [[C1]]) +; CHECK-NEXT: ret i1 [[AND]] +; + %c1 = icmp ne i8 %x, 0 + %c2 = icmp ule i8 %x, %y + %and = and i1 %c1, %c2 + call void @use(i1 %c1) + ret i1 %and +} + +define i1 @ne_multi_c2(i8 %x, i8 %y) { +; CHECK-LABEL: define i1 @ne_multi_c2 +; CHECK-SAME: (i8 [[X:%.*]], i8 [[Y:%.*]]) { +; CHECK-NEXT: [[C2:%.*]] = icmp ule i8 [[X]], [[Y]] +; CHECK-NEXT: [[TMP1:%.*]] = add i8 [[X]], -1 +; CHECK-NEXT: [[AND:%.*]] = icmp ult i8 [[TMP1]], [[Y]] +; CHECK-NEXT: call void @use(i1 [[C2]]) +; CHECK-NEXT: ret i1 [[AND]] +; + %c1 = icmp ne i8 %x, 0 + %c2 = icmp ule i8 %x, %y + %and = and i1 %c1, %c2 + call void @use(i1 %c2) + ret i1 %and +} + +; ============================================================================== +; Tests with vector types +; ============================================================================== +define <2 x i1> @ne_vector(<2 x i8> %x, <2 x i8> %y) { +; CHECK-LABEL: define <2 x i1> @ne_vector +; CHECK-SAME: (<2 x i8> [[X:%.*]], <2 x i8> [[Y:%.*]]) { +; CHECK-NEXT: [[TMP1:%.*]] = add <2 x i8> [[X]], +; CHECK-NEXT: [[AND:%.*]] = icmp ult <2 x i8> [[TMP1]], [[Y]] +; CHECK-NEXT: ret <2 x i1> [[AND]] +; + %c1 = icmp ne <2 x i8> %x, + %c2 = icmp ule <2 x i8> %x, %y + %and = and <2 x i1> %c1, %c2 + ret <2 x i1> %and +} + +define <2 x i1> @ne_vector_equal_5(<2 x i8> %x, <2 x i8> %y) { +; CHECK-LABEL: define <2 x i1> @ne_vector_equal_5 +; CHECK-SAME: (<2 x i8> [[X:%.*]], <2 x i8> [[Y:%.*]]) { +; CHECK-NEXT: [[TMP1:%.*]] = add <2 x i8> [[X]], +; CHECK-NEXT: [[AND:%.*]] = icmp ult <2 x i8> [[TMP1]], [[Y]] +; CHECK-NEXT: ret <2 x i1> [[AND]] +; + %sub = sub <2 x i8> %x, + %c1 = icmp ne <2 x i8> %x, + %c2 = icmp ule <2 x i8> %sub, %y + %and = and <2 x i1> %c1, %c2 + ret <2 x i1> %and +} + +define <2 x i1> @ne_vector_equal_minus_1(<2 x i8> %x, <2 x i8> %y) { +; CHECK-LABEL: define <2 x i1> @ne_vector_equal_minus_1 +; CHECK-SAME: (<2 x i8> [[X:%.*]], <2 x i8> [[Y:%.*]]) { +; CHECK-NEXT: [[AND:%.*]] = icmp ult <2 x i8> [[X]], [[Y]] +; CHECK-NEXT: ret <2 x i1> [[AND]] +; + %add = add <2 x i8> %x, + %c1 = icmp ne <2 x i8> %x, + %c2 = icmp ule <2 x i8> %add, %y + %and = and <2 x i1> %c1, %c2 + ret <2 x i1> %and +} + +define <2 x i1> @ne_vector_equal_minus_7(<2 x i8> %x, <2 x i8> %y) { +; CHECK-LABEL: define <2 x i1> @ne_vector_equal_minus_7 +; CHECK-SAME: (<2 x i8> [[X:%.*]], <2 x i8> [[Y:%.*]]) { +; CHECK-NEXT: [[TMP1:%.*]] = add <2 x i8> [[X]], +; CHECK-NEXT: [[AND:%.*]] = icmp ult <2 x i8> [[TMP1]], [[Y]] +; CHECK-NEXT: ret <2 x i1> [[AND]] +; + %add = add <2 x i8> %x, + %c1 = icmp ne <2 x i8> %x, + %c2 = icmp ule <2 x i8> %add, %y + %and = and <2 x i1> %c1, %c2 + ret <2 x i1> %and +} + +define <2 x i1> @ne_vector_unequal1(<2 x i8> %x, <2 x i8> %y) { +; CHECK-LABEL: define <2 x i1> @ne_vector_unequal1 +; CHECK-SAME: (<2 x i8> [[X:%.*]], <2 x i8> [[Y:%.*]]) { +; CHECK-NEXT: [[SUB:%.*]] = add <2 x i8> [[X]], +; CHECK-NEXT: [[C1:%.*]] = icmp ne <2 x i8> [[X]], +; CHECK-NEXT: [[C2:%.*]] = icmp ule <2 x i8> [[SUB]], [[Y]] +; CHECK-NEXT: [[AND:%.*]] = and <2 x i1> [[C1]], [[C2]] +; CHECK-NEXT: ret <2 x i1> [[AND]] +; + %sub = sub <2 x i8> %x, + %c1 = icmp ne <2 x i8> %x, + %c2 = icmp ule <2 x i8> %sub, %y + %and = and <2 x i1> %c1, %c2 + ret <2 x i1> %and +} + +define <2 x i1> @ne_vector_unequal2(<2 x i8> %x, <2 x i8> %y) { +; CHECK-LABEL: define <2 x i1> @ne_vector_unequal2 +; CHECK-SAME: (<2 x i8> [[X:%.*]], <2 x i8> [[Y:%.*]]) { +; CHECK-NEXT: [[ADD:%.*]] = add <2 x i8> [[X]], +; CHECK-NEXT: [[C1:%.*]] = icmp ne <2 x i8> [[X]], +; CHECK-NEXT: [[C2:%.*]] = icmp ule <2 x i8> [[ADD]], [[Y]] +; CHECK-NEXT: [[AND:%.*]] = and <2 x i1> [[C1]], [[C2]] +; CHECK-NEXT: ret <2 x i1> [[AND]] +; + %add = add <2 x i8> %x, + %c1 = icmp ne <2 x i8> %x, + %c2 = icmp ule <2 x i8> %add, %y + %and = and <2 x i1> %c1, %c2 + ret <2 x i1> %and +} + +; ============================================================================== +; Tests with values commuted +; ============================================================================== +define i1 @ne_commuted(i8 %x, i8 %y) { +; CHECK-LABEL: define i1 @ne_commuted +; CHECK-SAME: (i8 [[X:%.*]], i8 [[Y:%.*]]) { +; CHECK-NEXT: [[TMP1:%.*]] = add i8 [[X]], -1 +; CHECK-NEXT: [[OR:%.*]] = icmp uge i8 [[TMP1]], [[Y]] +; CHECK-NEXT: ret i1 [[OR]] +; + %c1 = icmp ne i8 %x, 0 + %c2 = icmp uge i8 %y, %x + %and = and i1 %c1, %c2 + ret i1 %and +} + +define i1 @ne_commuted_equal_minus_1(i8 %x, i8 %y) { +; CHECK-LABEL: define i1 @ne_commuted_equal_minus_1 +; CHECK-SAME: (i8 [[X:%.*]], i8 [[Y:%.*]]) { +; CHECK-NEXT: [[OR:%.*]] = icmp uge i8 [[X]], [[Y]] +; CHECK-NEXT: ret i1 [[OR]] +; + %add = add i8 %x, 1 + %c1 = icmp ne i8 %x, -1 + %c2 = icmp uge i8 %y, %add + %and = and i1 %c1, %c2 + ret i1 %and +} + +define <2 x i1> @ne_vetor_commuted(<2 x i8> %x, <2 x i8> %y) { +; CHECK-LABEL: define <2 x i1> @ne_vetor_commuted +; CHECK-SAME: (<2 x i8> [[X:%.*]], <2 x i8> [[Y:%.*]]) { +; CHECK-NEXT: [[TMP1:%.*]] = add <2 x i8> [[X]], +; CHECK-NEXT: [[OR:%.*]] = icmp uge <2 x i8> [[TMP1]], [[Y]] +; CHECK-NEXT: ret <2 x i1> [[OR]] +; + %c1 = icmp ne <2 x i8> %x, + %c2 = icmp uge <2 x i8> %y, %x + %and = and <2 x i1> %c1, %c2 + ret <2 x i1> %and +} + +define <2 x i1> @ne_vetor_commuted_equal_minus_1(<2 x i8> %x, <2 x i8> %y) { +; CHECK-LABEL: define <2 x i1> @ne_vetor_commuted_equal_minus_1 +; CHECK-SAME: (<2 x i8> [[X:%.*]], <2 x i8> [[Y:%.*]]) { +; CHECK-NEXT: [[OR:%.*]] = icmp uge <2 x i8> [[X]], [[Y]] +; CHECK-NEXT: ret <2 x i1> [[OR]] +; + %add = add <2 x i8> %x, + %c1 = icmp ne <2 x i8> %x, + %c2 = icmp uge <2 x i8> %y, %add + %and = and <2 x i1> %c1, %c2 + ret <2 x i1> %and +} + +declare void @use(i1)