-
Notifications
You must be signed in to change notification settings - Fork 12.1k
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
[InstCombine] Fix unexpected overwriting in foldSelectWithSRem
#89539
Merged
Conversation
This file contains 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: Yingwei Zheng (dtcxzyw) ChangesFixes #89516 Full diff: https://github.com/llvm/llvm-project/pull/89539.diff 2 Files Affected:
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
index 0262af28068be7..73600206a55c14 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
@@ -2720,7 +2720,7 @@ static Instruction *foldSelectWithSRem(SelectInst &SI, InstCombinerImpl &IC,
// %cnd = icmp slt i32 %rem, 0
// %add = add i32 %rem, %n
// %sel = select i1 %cnd, i32 %add, i32 %rem
- if (match(TrueVal, m_Add(m_Value(RemRes), m_Value(Remainder))) &&
+ if (match(TrueVal, m_Add(m_Specific(RemRes), m_Value(Remainder))) &&
match(RemRes, m_SRem(m_Value(Op), m_Specific(Remainder))) &&
IC.isKnownToBeAPowerOfTwo(Remainder, /*OrZero*/ true) &&
FalseVal == RemRes)
diff --git a/llvm/test/Transforms/InstCombine/select-divrem.ll b/llvm/test/Transforms/InstCombine/select-divrem.ll
index f007c53359ca5a..e0c460c37451db 100644
--- a/llvm/test/Transforms/InstCombine/select-divrem.ll
+++ b/llvm/test/Transforms/InstCombine/select-divrem.ll
@@ -343,3 +343,20 @@ define i32 @rem_euclid_pow2_false_arm_folded(i32 %n) {
%res = select i1 %nonneg, i32 %rem, i32 1
ret i32 %res
}
+
+define i8 @pr89516(i8 %n, i8 %x) {
+; CHECK-LABEL: @pr89516(
+; CHECK-NEXT: [[COND:%.*]] = icmp slt i8 [[X:%.*]], 0
+; CHECK-NEXT: [[POW2:%.*]] = shl nuw i8 1, [[N:%.*]]
+; CHECK-NEXT: [[SREM:%.*]] = srem i8 1, [[POW2]]
+; CHECK-NEXT: [[ADD:%.*]] = select i1 [[COND]], i8 [[POW2]], i8 0
+; CHECK-NEXT: [[RES:%.*]] = add nuw i8 [[SREM]], [[ADD]]
+; CHECK-NEXT: ret i8 [[RES]]
+;
+ %cond = icmp slt i8 %x, 0
+ %pow2 = shl nuw i8 1, %n
+ %srem = srem i8 1, %pow2
+ %add = add nuw i8 %srem, %pow2
+ %res = select i1 %cond, i8 %add, i8 %srem
+ ret i8 %res
+}
|
antoniofrighetto
approved these changes
Apr 21, 2024
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks for fixing it!
nikic
approved these changes
Apr 21, 2024
/cherry-pick 6309440 |
llvmbot
pushed a commit
to llvmbot/llvm-project
that referenced
this pull request
Apr 21, 2024
…vm#89539) Fixes llvm#89516 (cherry picked from commit 6309440)
/pull-request #89546 |
This was referenced Apr 22, 2024
tstellar
pushed a commit
to llvmbot/llvm-project
that referenced
this pull request
Apr 23, 2024
…vm#89539) Fixes llvm#89516 (cherry picked from commit 6309440)
Closed
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.
Fixes #89516