-
Notifications
You must be signed in to change notification settings - Fork 14.9k
[llvm][InstSimplify]: Increase the MaxRecurse limit to support deeper fold (icmp eq a, c1) | (icmp ult f(a), c2) to icmp ult f(a), c2. #155650
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
3
commits into
llvm:main
Choose a base branch
from
YLChenZ:instsimplify-143957
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.
+119
−24
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
… fold (icmp eq a, c1) | (icmp ult f(a), c2) to icmp ult f(a), c2.
@llvm/pr-subscribers-backend-amdgpu @llvm/pr-subscribers-llvm-transforms Author: None (YLChenZ) ChangesClose #143957. Full diff: https://github.com/llvm/llvm-project/pull/155650.diff 2 Files Affected:
diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index 5907e21065331..1642ce07ad685 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -1933,13 +1933,16 @@ static Value *simplifyAndOrWithICmpEq(unsigned Opcode, Value *Op0, Value *Op1,
// In the final case (Res == Absorber with inverted predicate), it is safe to
// refine poison during simplification, but not undef. For simplicity always
// disable undef-based folds here.
+ // Allow one extra recursion level for this speculative replace+simplify;
+ // because some folds require > MaxRecurse replacements to appear.
+ unsigned LocalMaxRecurse = MaxRecurse ? MaxRecurse + 1 : 1;
if (Value *Res = simplifyWithOpReplaced(Op1, A, B, Q.getWithoutUndef(),
/* AllowRefinement */ true,
- /* DropFlags */ nullptr, MaxRecurse))
+ /* DropFlags */ nullptr, LocalMaxRecurse))
return Simplify(Res);
if (Value *Res = simplifyWithOpReplaced(Op1, B, A, Q.getWithoutUndef(),
/* AllowRefinement */ true,
- /* DropFlags */ nullptr, MaxRecurse))
+ /* DropFlags */ nullptr, LocalMaxRecurse))
return Simplify(Res);
return nullptr;
diff --git a/llvm/test/Transforms/InstSimplify/and-or-implied-cond.ll b/llvm/test/Transforms/InstSimplify/and-or-implied-cond.ll
index 99e1dd4528697..aeae8a9880929 100644
--- a/llvm/test/Transforms/InstSimplify/and-or-implied-cond.ll
+++ b/llvm/test/Transforms/InstSimplify/and-or-implied-cond.ll
@@ -347,3 +347,107 @@ define i1 @pr98753(i32 noundef %x, i32 %y) {
}
declare i1 @llvm.is.constant.i1(i1)
+
+
+define i1 @or_icmp_fold(i64 %arg0) {
+; CHECK-LABEL: @or_icmp_fold(
+; CHECK-NEXT: [[TMP1:%.*]] = lshr i64 [[ARG0:%.*]], 32
+; CHECK-NEXT: [[TMP2:%.*]] = trunc nuw i64 [[TMP1]] to i32
+; CHECK-NEXT: [[TMP3:%.*]] = xor i32 [[TMP2]], 55296
+; CHECK-NEXT: [[TMP4:%.*]] = add i32 [[TMP3]], -1114112
+; CHECK-NEXT: [[TMP5:%.*]] = icmp ult i32 [[TMP4]], -1112064
+; CHECK-NEXT: ret i1 [[TMP5]]
+;
+ %1 = lshr i64 %arg0, 32
+ %2 = trunc nuw i64 %1 to i32
+ %3 = xor i32 %2, 55296
+ %4 = add i32 %3, -1114112
+ %5 = icmp ult i32 %4, -1112064
+ %6 = icmp eq i64 %1, 1114112
+ %7 = or i1 %6, %5
+ ret i1 %7
+}
+
+
+define i1 @or_icmp_fold_negative(i64 %arg0) {
+; CHECK-LABEL: @or_icmp_fold_negative(
+; CHECK-NEXT: [[TMP1:%.*]] = lshr i64 [[ARG0:%.*]], 32
+; CHECK-NEXT: [[TMP2:%.*]] = trunc nuw i64 [[TMP1]] to i32
+; CHECK-NEXT: [[TMP3:%.*]] = xor i32 [[TMP2]], 55296
+; CHECK-NEXT: [[TMP4:%.*]] = add i32 [[TMP3]], -1114112
+; CHECK-NEXT: [[TMP5:%.*]] = icmp ult i32 [[TMP4]], 1000
+; CHECK-NEXT: [[TMP6:%.*]] = icmp eq i64 [[TMP1]], 1114112
+; CHECK-NEXT: [[TMP7:%.*]] = or i1 [[TMP6]], [[TMP5]]
+; CHECK-NEXT: ret i1 [[TMP7]]
+;
+ %1 = lshr i64 %arg0, 32
+ %2 = trunc nuw i64 %1 to i32
+ %3 = xor i32 %2, 55296
+ %4 = add i32 %3, -1114112
+ %5 = icmp ult i32 %4, 1000
+ %6 = icmp eq i64 %1, 1114112
+ %7 = or i1 %6, %5
+ ret i1 %7
+}
+
+declare void @use(i32)
+
+define i1 @or_icmp_fold_multi_use(i64 %arg0) {
+; CHECK-LABEL: @or_icmp_fold_multi_use(
+; CHECK-NEXT: [[TMP1:%.*]] = lshr i64 [[ARG0:%.*]], 32
+; CHECK-NEXT: [[TMP2:%.*]] = trunc nuw i64 [[TMP1]] to i32
+; CHECK-NEXT: [[TMP3:%.*]] = xor i32 [[TMP2]], 55296
+; CHECK-NEXT: [[TMP4:%.*]] = add i32 [[TMP3]], -1114112
+; CHECK-NEXT: call void @use(i32 [[TMP4]])
+; CHECK-NEXT: [[TMP5:%.*]] = icmp ult i32 [[TMP4]], -1112064
+; CHECK-NEXT: ret i1 [[TMP5]]
+;
+ %1 = lshr i64 %arg0, 32
+ %2 = trunc nuw i64 %1 to i32
+ %3 = xor i32 %2, 55296
+ %4 = add i32 %3, -1114112
+ call void @use(i32 %4)
+ %5 = icmp ult i32 %4, -1112064
+ %6 = icmp eq i64 %1, 1114112
+ %7 = or i1 %6, %5
+ ret i1 %7
+}
+
+define i1 @or_icmp_fold_commuted(i64 %arg0) {
+; CHECK-LABEL: @or_icmp_fold_commuted(
+; CHECK-NEXT: [[TMP1:%.*]] = lshr i64 [[ARG0:%.*]], 32
+; CHECK-NEXT: [[TMP2:%.*]] = trunc nuw i64 [[TMP1]] to i32
+; CHECK-NEXT: [[TMP3:%.*]] = xor i32 [[TMP2]], 55296
+; CHECK-NEXT: [[TMP4:%.*]] = add i32 [[TMP3]], -1114112
+; CHECK-NEXT: [[TMP5:%.*]] = icmp ult i32 [[TMP4]], -1112064
+; CHECK-NEXT: ret i1 [[TMP5]]
+;
+ %1 = lshr i64 %arg0, 32
+ %2 = trunc nuw i64 %1 to i32
+ %3 = xor i32 %2, 55296
+ %4 = add i32 %3, -1114112
+ %5 = icmp ult i32 %4, -1112064
+ %6 = icmp eq i64 %1, 1114112
+ %7 = or i1 %5, %6
+ ret i1 %7
+}
+
+
+define <2 x i1> @or_icmp_fold_vec(<2 x i64> %arg0) {
+; CHECK-LABEL: @or_icmp_fold_vec(
+; CHECK-NEXT: [[TMP1:%.*]] = lshr <2 x i64> [[ARG0:%.*]], splat (i64 32)
+; CHECK-NEXT: [[TMP2:%.*]] = trunc <2 x i64> [[TMP1]] to <2 x i32>
+; CHECK-NEXT: [[TMP3:%.*]] = xor <2 x i32> [[TMP2]], splat (i32 55296)
+; CHECK-NEXT: [[TMP4:%.*]] = add <2 x i32> [[TMP3]], splat (i32 -1114112)
+; CHECK-NEXT: [[TMP5:%.*]] = icmp ult <2 x i32> [[TMP4]], splat (i32 -1112064)
+; CHECK-NEXT: ret <2 x i1> [[TMP5]]
+;
+ %1 = lshr <2 x i64> %arg0, <i64 32, i64 32>
+ %2 = trunc <2 x i64> %1 to <2 x i32>
+ %3 = xor <2 x i32> %2, <i32 55296, i32 55296>
+ %4 = add <2 x i32> %3, <i32 -1114112, i32 -1114112>
+ %5 = icmp ult <2 x i32> %4, <i32 -1112064, i32 -1112064>
+ %6 = icmp eq <2 x i64> %1, <i64 1114112, i64 1114112>
+ %7 = or <2 x i1> %6, %5
+ ret <2 x i1> %7
+}
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
backend:AMDGPU
llvm:analysis
Includes value tracking, cost tables and constant folding
llvm:instcombine
Covers the InstCombine, InstSimplify and AggressiveInstCombine passes
llvm:transforms
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.
Close #143957.
alive2: https://alive2.llvm.org/ce/z/wc2PXT
godbolt: https://godbolt.org/z/o8nqYz577