Skip to content

[ConstantFold] Constant fold icmp of boolean scalable vectors #142528

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 3, 2025

Conversation

dtcxzyw
Copy link
Member

@dtcxzyw dtcxzyw commented Jun 3, 2025

Closes #142447.

@llvmbot
Copy link
Member

llvmbot commented Jun 3, 2025

@llvm/pr-subscribers-llvm-transforms

@llvm/pr-subscribers-llvm-ir

Author: Yingwei Zheng (dtcxzyw)

Changes

Closes #142447.


Full diff: https://github.com/llvm/llvm-project/pull/142528.diff

2 Files Affected:

  • (modified) llvm/lib/IR/ConstantFold.cpp (+2-2)
  • (modified) llvm/test/Transforms/InstSimplify/compare.ll (+20)
diff --git a/llvm/lib/IR/ConstantFold.cpp b/llvm/lib/IR/ConstantFold.cpp
index 7e5fda229b858..b9db402fe9562 100644
--- a/llvm/lib/IR/ConstantFold.cpp
+++ b/llvm/lib/IR/ConstantFold.cpp
@@ -1149,10 +1149,10 @@ Constant *llvm::ConstantFoldCompareInstruction(CmpInst::Predicate Predicate,
   }
 
   // If the comparison is a comparison between two i1's, simplify it.
-  if (C1->getType()->isIntegerTy(1)) {
+  if (C1->getType()->isIntOrIntVectorTy(1)) {
     switch (Predicate) {
     case ICmpInst::ICMP_EQ:
-      if (isa<ConstantInt>(C2))
+      if (isa<ConstantExpr>(C1))
         return ConstantExpr::getXor(C1, ConstantExpr::getNot(C2));
       return ConstantExpr::getXor(ConstantExpr::getNot(C1), C2);
     case ICmpInst::ICMP_NE:
diff --git a/llvm/test/Transforms/InstSimplify/compare.ll b/llvm/test/Transforms/InstSimplify/compare.ll
index 4192a59dc7134..8b4ce469d4490 100644
--- a/llvm/test/Transforms/InstSimplify/compare.ll
+++ b/llvm/test/Transforms/InstSimplify/compare.ll
@@ -3465,6 +3465,26 @@ define i1 @icmp_eq_false_by_trunc(i8 %x) {
   ret i1 %cmp
 }
 
+define <vscale x 8 x i1> @icmp_ne_i1_vec_constant_expr() {
+; CHECK-LABEL: @icmp_ne_i1_vec_constant_expr(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    ret <vscale x 8 x i1> insertelement (<vscale x 8 x i1> poison, i1 true, i64 0)
+;
+entry:
+  %1 = icmp ne <vscale x 8 x i1> insertelement (<vscale x 8 x i1> poison, i1 true, i64 0), zeroinitializer
+  ret <vscale x 8 x i1> %1
+}
+
+define <vscale x 8 x i1> @icmp_eq_i1_vec_constant_expr_commuted() {
+; CHECK-LABEL: @icmp_eq_i1_vec_constant_expr_commuted(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    ret <vscale x 8 x i1> xor (<vscale x 8 x i1> insertelement (<vscale x 8 x i1> poison, i1 true, i64 0), <vscale x 8 x i1> splat (i1 true))
+;
+entry:
+  %1 = icmp eq <vscale x 8 x i1> zeroinitializer, insertelement (<vscale x 8 x i1> poison, i1 true, i64 0)
+  ret <vscale x 8 x i1> %1
+}
+
 declare i64 @llvm.vscale.i64()
 
 ; TODO: Add coverage for global aliases, link once, etc..

Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@dtcxzyw dtcxzyw merged commit d9a5f7b into llvm:main Jun 3, 2025
11 checks passed
@dtcxzyw dtcxzyw deleted the fix-142447 branch June 3, 2025 11:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[InstCombine] ICmp i1 X, C not simplified as expected. with opt "-passes=lto<O3>"
4 participants