[llvm] 1c12dcc - [InstCombine] Extend `sext`/`zext` boolean additions to vectors
Antonio Frighetto via llvm-commits
llvm-commits at lists.llvm.org
Thu Oct 12 07:40:28 PDT 2023
Author: Antonio Frighetto
Date: 2023-10-12T14:38:54Z
New Revision: 1c12dcc910325cc592929e08ae1abd1764d1c3fb
URL: https://github.com/llvm/llvm-project/commit/1c12dcc910325cc592929e08ae1abd1764d1c3fb
DIFF: https://github.com/llvm/llvm-project/commit/1c12dcc910325cc592929e08ae1abd1764d1c3fb.diff
LOG: [InstCombine] Extend `sext`/`zext` boolean additions to vectors
Reported-by: shao-hua-li
Fixes: https://github.com/llvm/llvm-project/issues/68745.
Added:
Modified:
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
llvm/test/Transforms/InstCombine/icmp-add.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index 95b506f0e35faff..aa18c7e73ad5f34 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -2899,9 +2899,17 @@ Instruction *InstCombinerImpl::foldICmpSubConstant(ICmpInst &Cmp,
static Value *createLogicFromTable(const std::bitset<4> &Table, Value *Op0,
Value *Op1, IRBuilderBase &Builder,
bool HasOneUse) {
+ auto FoldConstant = [&](bool Val) {
+ Constant *Res = Val ? Builder.getTrue() : Builder.getFalse();
+ if (Op0->getType()->isVectorTy())
+ Res = ConstantVector::getSplat(
+ cast<VectorType>(Op0->getType())->getElementCount(), Res);
+ return Res;
+ };
+
switch (Table.to_ulong()) {
case 0: // 0 0 0 0
- return Builder.getFalse();
+ return FoldConstant(false);
case 1: // 0 0 0 1
return HasOneUse ? Builder.CreateNot(Builder.CreateOr(Op0, Op1)) : nullptr;
case 2: // 0 0 1 0
@@ -2931,7 +2939,7 @@ static Value *createLogicFromTable(const std::bitset<4> &Table, Value *Op0,
case 14: // 1 1 1 0
return Builder.CreateOr(Op0, Op1);
case 15: // 1 1 1 1
- return Builder.getTrue();
+ return FoldConstant(true);
default:
llvm_unreachable("Invalid Operation");
}
diff --git a/llvm/test/Transforms/InstCombine/icmp-add.ll b/llvm/test/Transforms/InstCombine/icmp-add.ll
index 832db40fda5f676..b99ed20d7d431c5 100644
--- a/llvm/test/Transforms/InstCombine/icmp-add.ll
+++ b/llvm/test/Transforms/InstCombine/icmp-add.ll
@@ -515,6 +515,19 @@ bb:
ret i1 %t
}
+define <2 x i1> @cvt_icmp_2_sext_plus_zext_ne_vec(<2 x i1> %arg, <2 x i1> %arg1) {
+; CHECK-LABEL: @cvt_icmp_2_sext_plus_zext_ne_vec(
+; CHECK-NEXT: bb:
+; CHECK-NEXT: ret <2 x i1> <i1 true, i1 true>
+;
+bb:
+ %i = sext <2 x i1> %arg to <2 x i32>
+ %i2 = zext <2 x i1> %arg1 to <2 x i32>
+ %i3 = add nsw <2 x i32> %i, %i2
+ %i4 = icmp ne <2 x i32> %i3, <i32 2, i32 2>
+ ret <2 x i1> %i4
+}
+
; test if zext i1 X + sext i1 Y converted to sext i1 X + zext i1 Y
; and then processed
@@ -531,6 +544,19 @@ bb:
ret i1 %t
}
+define <2 x i1> @cvt_icmp_neg_2_zext_plus_sext_eq_vec(<2 x i1> %arg, <2 x i1> %arg1) {
+; CHECK-LABEL: @cvt_icmp_neg_2_zext_plus_sext_eq_vec(
+; CHECK-NEXT: bb:
+; CHECK-NEXT: ret <2 x i1> zeroinitializer
+;
+bb:
+ %i = zext <2 x i1> %arg to <2 x i32>
+ %i2 = sext <2 x i1> %arg1 to <2 x i32>
+ %i3 = add nsw <2 x i32> %i2, %i
+ %i4 = icmp eq <2 x i32> %i3, <i32 2, i32 2>
+ ret <2 x i1> %i4
+}
+
define i1 @cvt_icmp_neg_1_zext_plus_sext_eq(i1 %arg, i1 %arg1) {
; CHECK-LABEL: @cvt_icmp_neg_1_zext_plus_sext_eq(
; CHECK-NEXT: bb:
More information about the llvm-commits
mailing list