-
Notifications
You must be signed in to change notification settings - Fork 13.4k
[InstCombine] fold u <= (u <= (unsigned)b) into (u <= (unsigned)b). #136654
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
Open
YLChenZ
wants to merge
2
commits into
llvm:main
Choose a base branch
from
YLChenZ:InstCombine
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@llvm/pr-subscribers-llvm-transforms Author: None (YLChenZ) ChangesCloses #132508. Full diff: https://github.com/llvm/llvm-project/pull/136654.diff 2 Files Affected:
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index 55afe1258159a..23e9153ffa51b 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -7435,6 +7435,31 @@ Instruction *InstCombinerImpl::visitICmpInst(ICmpInst &I) {
if (Value *V = simplifyICmpInst(I.getCmpPredicate(), Op0, Op1, Q))
return replaceInstUsesWith(I, V);
+ // Try to fold pattern: u <= (u <= b), b is boolean-like.
+ // This fold handles cases where Clang produces code like:
+ // %cmp1 = icmp ule i32 %u, zext i1 %b to i32
+ // %zext = zext i1 %cmp1 to i32
+ // %cmp2 = icmp ule i32 %u, %zext
+ // fold the comparison into: u <= b
+ if (I.getPredicate() == ICmpInst::ICMP_ULE) {
+ if (auto *Z = dyn_cast<ZExtInst>(Op1)) {
+ if (Z->getSrcTy()->isIntegerTy(1)) {
+ if (auto *Inner = dyn_cast<ICmpInst>(Z->getOperand(0))) {
+ if (Inner->getPredicate() == ICmpInst::ICMP_ULE &&
+ Inner->getOperand(0) == Op0 && Inner->hasOneUse()) {
+ Value *Bnd = Inner->getOperand(1);
+ bool IsBoolExt = isa<ZExtInst>(Bnd) &&
+ cast<ZExtInst>(Bnd)->getSrcTy()->isIntegerTy(1);
+ if (match(Bnd, m_ZeroInt()) || match(Bnd, m_One()) || IsBoolExt) {
+ Value *NewCmp = Builder.CreateICmpULE(Op0, Bnd, I.getName());
+ return replaceInstUsesWith(I, NewCmp);
+ }
+ }
+ }
+ }
+ }
+ }
+
// Comparing -val or val with non-zero is the same as just comparing val
// ie, abs(val) != 0 -> val != 0
if (I.getPredicate() == ICmpInst::ICMP_NE && match(Op1, m_Zero())) {
diff --git a/llvm/test/Transforms/InstCombine/icmp.ll b/llvm/test/Transforms/InstCombine/icmp.ll
index 6e1486660b24d..81357300b8063 100644
--- a/llvm/test/Transforms/InstCombine/icmp.ll
+++ b/llvm/test/Transforms/InstCombine/icmp.ll
@@ -5400,3 +5400,38 @@ define i1 @icmp_samesign_logical_or(i32 %In) {
%V = select i1 %c1, i1 true, i1 %c2
ret i1 %V
}
+
+;===----------------------------------------------------------------------===;
+; Test folding of nested `icmp ule` when inner RHS is boolean-like
+;===----------------------------------------------------------------------===;
+
+define i32 @fold_nested_ule_bool(i32 %u, i1 %b) {
+; CHECK-LABEL: @fold_nested_ule_bool(
+; CHECK-NEXT: [[CONV:%.*]] = zext i1 [[B:%.*]] to i32
+; CHECK-NEXT: [[CMP:%.*]] = icmp ule i32 [[U:%.*]], [[CONV]]
+; CHECK-NEXT: [[CONV1:%.*]] = zext i1 [[CMP]] to i32
+; CHECK-NEXT: ret i32 [[CONV1]]
+;
+ %conv = zext i1 %b to i32
+ %cmp = icmp ule i32 %u, %conv
+ %conv1 = zext i1 %cmp to i32
+ %cmp2 = icmp ule i32 %u, %conv1
+ %conv3 = zext i1 %cmp2 to i32
+ ret i32 %conv3
+}
+
+define i32 @no_fold_nested_ule(i32 %u, i32 %b) {
+; CHECK-LABEL: @no_fold_nested_ule(
+; CHECK-NEXT: [[CMP1:%.*]] = icmp ule i32 [[U:%.*]], [[B:%.*]]
+; CHECK-NEXT: [[Z1:%.*]] = zext i1 [[CMP1]] to i32
+; CHECK-NEXT: [[CMP2:%.*]] = icmp ule i32 [[U]], [[Z1]]
+; CHECK-NEXT: [[CONV:%.*]] = zext i1 [[CMP2]] to i32
+; CHECK-NEXT: ret i32 [[CONV]]
+;
+ %cmp1 = icmp ule i32 %u, %b
+ %z1 = zext i1 %cmp1 to i32
+ %cmp2 = icmp ule i32 %u, %z1
+ %conv = zext i1 %cmp2 to i32
+ ret i32 %conv
+}
+
|
This was referenced Apr 22, 2025
I cannot provide evidence for the real-world usefulness of this patch. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Closes #132508.
Proof: https://alive2.llvm.org/ce/z/AjmXuN.