Skip to content

[InstCombine] Fold (trunc nuw A to i1) == (trunc nuw B to i1) to A == B #133368

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
Mar 28, 2025

Conversation

veera-sivarajan
Copy link
Contributor

Fixes #133344

Proof: https://alive2.llvm.org/ce/z/X3Uh23

InstCombine couldn't optimize i1 because canonicalizeICmpBool() was transforming the comparison into bitwise operations before foldICmpTruncWithTruncOrExt() was called.

This PR solves the ordering issue by placing foldICmpTruncWithTruncOrExt() before canonicalizeICmpBool().

I believe this will not cause any regressions since all tests are passing.

@llvmbot
Copy link
Member

llvmbot commented Mar 28, 2025

@llvm/pr-subscribers-llvm-transforms

Author: Veera (veera-sivarajan)

Changes

Fixes #133344

Proof: https://alive2.llvm.org/ce/z/X3Uh23

InstCombine couldn't optimize i1 because canonicalizeICmpBool() was transforming the comparison into bitwise operations before foldICmpTruncWithTruncOrExt() was called.

This PR solves the ordering issue by placing foldICmpTruncWithTruncOrExt() before canonicalizeICmpBool().

I believe this will not cause any regressions since all tests are passing.


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

2 Files Affected:

  • (modified) llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp (+3-3)
  • (modified) llvm/test/Transforms/InstCombine/icmp-of-trunc-ext.ll (+11)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index 76020d2b1dbf4..e75b4026d5424 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -7451,6 +7451,9 @@ Instruction *InstCombinerImpl::visitICmpInst(ICmpInst &I) {
     }
   }
 
+  if (Instruction *Res = foldICmpTruncWithTruncOrExt(I, Q))
+    return Res;
+
   if (Op0->getType()->isIntOrIntVectorTy(1))
     if (Instruction *Res = canonicalizeICmpBool(I, Builder))
       return Res;
@@ -7473,9 +7476,6 @@ Instruction *InstCombinerImpl::visitICmpInst(ICmpInst &I) {
   if (Instruction *Res = foldICmpUsingKnownBits(I))
     return Res;
 
-  if (Instruction *Res = foldICmpTruncWithTruncOrExt(I, Q))
-    return Res;
-
   // Test if the ICmpInst instruction is used exclusively by a select as
   // part of a minimum or maximum operation. If so, refrain from doing
   // any other folding. This helps out other analyses which understand
diff --git a/llvm/test/Transforms/InstCombine/icmp-of-trunc-ext.ll b/llvm/test/Transforms/InstCombine/icmp-of-trunc-ext.ll
index 2c2de5dbf09f6..aeb70a636682f 100644
--- a/llvm/test/Transforms/InstCombine/icmp-of-trunc-ext.ll
+++ b/llvm/test/Transforms/InstCombine/icmp-of-trunc-ext.ll
@@ -409,6 +409,17 @@ define i1 @trunc_equality_either(i16 %x, i16 %y) {
   ret i1 %c
 }
 
+define i1 @trunc_equality_bool(i8 %a, i8 %b) {
+; CHECK-LABEL: @trunc_equality_bool(
+; CHECK-NEXT:    [[EQ:%.*]] = icmp eq i8 [[A:%.*]], [[B:%.*]]
+; CHECK-NEXT:    ret i1 [[EQ]]
+;
+  %at = trunc nuw i8 %a to i1
+  %bt = trunc nuw i8 %b to i1
+  %eq = icmp eq i1 %at, %bt
+  ret i1 %eq
+}
+
 define i1 @trunc_unsigned_nuw_zext(i32 %x, i8 %y) {
 ; CHECK-LABEL: @trunc_unsigned_nuw_zext(
 ; CHECK-NEXT:    [[TMP1:%.*]] = zext i8 [[Y:%.*]] to i32

Copy link
Member

@dtcxzyw dtcxzyw left a comment

Choose a reason for hiding this comment

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

LG.

@veera-sivarajan veera-sivarajan merged commit 4cdcf3b into llvm:main Mar 28, 2025
14 checks passed
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.

(trunc nuw A) == (trunc nuw B) should not take multiple xors [InstCombine]
3 participants