Index: llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp =================================================================== --- llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp +++ llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -5830,6 +5830,42 @@ Y->getType()->isIntOrIntVectorTy(1) && Pred == ICmpInst::ICMP_ULE) return BinaryOperator::CreateOr(Builder.CreateIsNull(X), Y); + const APInt *CmpC; + // For the pattern: (zext i1 X) + (sext i1 Y)), C + // when C == -1 means, X == false ,Y == true + // C == 0 means , X == Y + // C == 1 means, X == true, Y == false + // So based on these info we can simplify the icmp + 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) && CmpC->abs().ule(1)) { + Pred = I.getPredicate(); + switch (CmpC->getSExtValue()) { + case -1: + if (Pred == ICmpInst::ICMP_EQ) + return BinaryOperator::CreateAnd(Builder.CreateNot(X), Y); + if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SGT) + return BinaryOperator::CreateOr(X, Builder.CreateNot(Y)); + break; + case 0: + if (Pred == ICmpInst::ICMP_SGT) + return BinaryOperator::CreateAnd(X, Builder.CreateNot(Y)); + if (Pred == ICmpInst::ICMP_SLT) + return BinaryOperator::CreateAnd(Builder.CreateNot(X), Y); + break; + case 1: + if (Pred == ICmpInst::ICMP_EQ) + return BinaryOperator::CreateAnd(X, Builder.CreateNot(Y)); + if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SLT) + return BinaryOperator::CreateOr(Builder.CreateNot(X), Y); + break; + default: + llvm_unreachable("CmpC should be also one of [-1, 0, 1]"); + } + } + return nullptr; } Index: llvm/test/Transforms/InstCombine/icmp-range.ll =================================================================== --- llvm/test/Transforms/InstCombine/icmp-range.ll +++ llvm/test/Transforms/InstCombine/icmp-range.ll @@ -629,6 +629,185 @@ ret i1 %r } +define i1 @zext_sext_add_icmp_eq_minus1(i1 %a, i1 %b) { +; CHECK-LABEL: @zext_sext_add_icmp_eq_minus1( +; 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 + %zext.b = sext i1 %b to i8 + %add = add i8 %zext.a, %zext.b + %r = icmp eq i8 %add, -1 + ret i1 %r +} + +define i1 @zext_sext_add_icmp_ne_minus1(i1 %a, i1 %b) { +; CHECK-LABEL: @zext_sext_add_icmp_ne_minus1( +; 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 + %zext.b = sext i1 %b to i8 + %add = add i8 %zext.a, %zext.b + %r = icmp ne i8 %add, -1 + ret i1 %r +} + +define i1 @zext_sext_add_icmp_sgt_minus1(i1 %a, i1 %b) { +; CHECK-LABEL: @zext_sext_add_icmp_sgt_minus1( +; 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 + %zext.b = sext i1 %b to i8 + %add = add i8 %zext.a, %zext.b + %r = icmp sgt i8 %add, -1 + ret i1 %r +} + +define i1 @zext_sext_add_icmp_sgt_0(i1 %a, i1 %b) { +; CHECK-LABEL: @zext_sext_add_icmp_sgt_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 + %zext.b = sext i1 %b to i8 + %add = add i8 %zext.a, %zext.b + %r = icmp sgt i8 %add, 0 + ret i1 %r +} + +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: [[R:%.*]] = and i1 [[TMP1]], [[B:%.*]] +; CHECK-NEXT: ret i1 [[R]] +; + %zext.a = zext i1 %a to i8 + %zext.b = sext i1 %b to i8 + %add = add i8 %zext.a, %zext.b + %r = icmp slt i8 %add, 0 + ret i1 %r +} + +define i1 @zext_sext_add_icmp_eq_1(i1 %a, i1 %b) { +; CHECK-LABEL: @zext_sext_add_icmp_eq_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 + %zext.b = sext i1 %b to i8 + %add = add i8 %zext.a, %zext.b + %r = icmp eq i8 %add, 1 + ret i1 %r +} + +define i1 @zext_sext_add_icmp_ne_1(i1 %a, i1 %b) { +; CHECK-LABEL: @zext_sext_add_icmp_ne_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 + %zext.b = sext i1 %b to i8 + %add = add i8 %zext.a, %zext.b + %r = icmp ne i8 %add, 1 + ret i1 %r +} + +define i1 @zext_sext_add_icmp_slt_1(i1 %a, i1 %b) { +; CHECK-LABEL: @zext_sext_add_icmp_slt_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 + %zext.b = sext i1 %b to i8 + %add = add i8 %zext.a, %zext.b + %r = icmp slt i8 %add, 1 + ret i1 %r +} + +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: [[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> + %zext.b = sext <2 x i1> %b to <2 x i8> + %add = add <2 x i8> %zext.a, %zext.b + %r = icmp slt <2 x i8> %add, + ret <2 x i1> %r +} + +define <2 x i1> @vector_zext_sext_add_icmp_slt_1_poison(<2 x i1> %a, <2 x i1> %b) { +; CHECK-LABEL: @vector_zext_sext_add_icmp_slt_1_poison( +; CHECK-NEXT: [[ZEXT_A:%.*]] = zext <2 x i1> [[A:%.*]] to <2 x i8> +; CHECK-NEXT: [[ZEXT_B:%.*]] = sext <2 x i1> [[B:%.*]] to <2 x i8> +; CHECK-NEXT: [[ADD:%.*]] = add nsw <2 x i8> [[ZEXT_A]], [[ZEXT_B]] +; CHECK-NEXT: [[R:%.*]] = icmp slt <2 x i8> [[ADD]], +; CHECK-NEXT: ret <2 x i1> [[R]] +; + %zext.a = zext <2 x i1> %a to <2 x i8> + %zext.b = sext <2 x i1> %b to <2 x i8> + %add = add <2 x i8> %zext.a, %zext.b + %r = icmp slt <2 x i8> %add, + ret <2 x i1> %r +} + +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 +; CHECK-NEXT: [[ZEXT_B:%.*]] = sext i1 [[B:%.*]] to i8 +; CHECK-NEXT: [[ADD:%.*]] = add nsw i8 [[ZEXT_A]], [[ZEXT_B]] +; CHECK-NEXT: call void @use(i8 [[ADD]]) +; CHECK-NEXT: [[R:%.*]] = icmp slt i8 [[ADD]], 1 +; CHECK-NEXT: ret i1 [[R]] +; + %zext.a = zext i1 %a to i8 + %zext.b = sext i1 %b to i8 + %add = add i8 %zext.a, %zext.b + call void @use(i8 %add) + %r = icmp slt i8 %add, 1 + ret i1 %r +} + +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 +; CHECK-NEXT: [[ZEXT_B:%.*]] = sext i1 [[B:%.*]] to i8 +; CHECK-NEXT: [[ADD:%.*]] = add nsw i8 [[ZEXT_A]], [[ZEXT_B]] +; CHECK-NEXT: [[R:%.*]] = icmp slt i8 [[ADD]], [[C:%.*]] +; CHECK-NEXT: ret i1 [[R]] +; + %zext.a = zext i1 %a to i8 + %zext.b = sext i1 %b to i8 + %add = add i8 %zext.a, %zext.b + %r = icmp slt i8 %add, %c + ret i1 %r +} + +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 +; CHECK-NEXT: [[ZEXT_B:%.*]] = sext i1 [[B:%.*]] to i8 +; CHECK-NEXT: [[ADD:%.*]] = add nsw i8 [[ZEXT_A]], [[ZEXT_B]] +; CHECK-NEXT: [[R:%.*]] = icmp slt i8 [[ADD]], 1 +; CHECK-NEXT: ret i1 [[R]] +; + %zext.a = zext i2 %a to i8 + %zext.b = sext i1 %b to i8 + %add = add i8 %zext.a, %zext.b + %r = icmp slt i8 %add, 1 + ret i1 %r +} + !0 = !{i32 1, i32 6} !1 = !{i32 0, i32 6} !2 = !{i8 0, i8 1}