Skip to content

[InstCombine] Avoid to create bitreverse.i1 for or of trunc to i1 #142258

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
May 31, 2025

Conversation

andjo403
Copy link
Contributor

Avoid to create bitreverse.i1 as it hinder optimizations due to there is less optimizations for bitreverse and also is a bit strange to create a i1 bitreverse

this change avoids this transform a couple of times in llvm-opt-benchmark.

@llvmbot
Copy link
Member

llvmbot commented May 31, 2025

@llvm/pr-subscribers-llvm-transforms

Author: Andreas Jonson (andjo403)

Changes

Avoid to create bitreverse.i1 as it hinder optimizations due to there is less optimizations for bitreverse and also is a bit strange to create a i1 bitreverse

this change avoids this transform a couple of times in llvm-opt-benchmark.


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

2 Files Affected:

  • (modified) llvm/lib/Transforms/Utils/Local.cpp (+2-1)
  • (modified) llvm/test/Transforms/InstCombine/or.ll (+12)
diff --git a/llvm/lib/Transforms/Utils/Local.cpp b/llvm/lib/Transforms/Utils/Local.cpp
index fe1391a501b43..a0618726ac0ac 100644
--- a/llvm/lib/Transforms/Utils/Local.cpp
+++ b/llvm/lib/Transforms/Utils/Local.cpp
@@ -4117,7 +4117,8 @@ bool llvm::recognizeBSwapOrBitReverseIdiom(
   if (!MatchBSwaps && !MatchBitReversals)
     return false;
   Type *ITy = I->getType();
-  if (!ITy->isIntOrIntVectorTy() || ITy->getScalarSizeInBits() > 128)
+  if (!ITy->isIntOrIntVectorTy() || ITy->getScalarSizeInBits() == 1 ||
+      ITy->getScalarSizeInBits() > 128)
     return false;  // Can't do integer/elements > 128 bits.
 
   // Try to find all the pieces corresponding to the bswap.
diff --git a/llvm/test/Transforms/InstCombine/or.ll b/llvm/test/Transforms/InstCombine/or.ll
index 95f89e4ce11cd..a5cc933278982 100644
--- a/llvm/test/Transforms/InstCombine/or.ll
+++ b/llvm/test/Transforms/InstCombine/or.ll
@@ -2035,3 +2035,15 @@ define i32 @or_xor_and_commuted3(i32 %x, i32 %y, i32 %z) {
   %or1 = or i32 %xor, %yy
   ret i32 %or1
 }
+
+define i1 @or_truncs(i8 %x) {
+; CHECK-LABEL: @or_truncs(
+; CHECK-NEXT:    [[TMP1:%.*]] = and i8 [[X:%.*]], 1
+; CHECK-NEXT:    [[OR1:%.*]] = icmp ne i8 [[TMP1]], 0
+; CHECK-NEXT:    ret i1 [[OR1]]
+;
+  %trunc1 = trunc i8 %x to i1
+  %trunc2 = trunc i8 %x to i1
+  %or1 = or i1 %trunc1, %trunc2
+  ret i1 %or1
+}

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.

Nice catch!

@andjo403 andjo403 merged commit 7c080e2 into llvm:main May 31, 2025
14 checks passed
@andjo403 andjo403 deleted the avoidi1BitReverse branch May 31, 2025 17: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.

3 participants