-
Notifications
You must be signed in to change notification settings - Fork 13.3k
[InstCombine] Drop nsw in negation of select #112893
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
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: Yingwei Zheng (dtcxzyw) ChangesCloses #112666. Full diff: https://github.com/llvm/llvm-project/pull/112893.diff 2 Files Affected:
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp b/llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp
index 92293ef401465b..1d7ab84f19bd55 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp
@@ -334,6 +334,17 @@ std::array<Value *, 2> Negator::getSortedOperandsOfBinOp(Instruction *I) {
NewSelect->swapValues();
// Don't swap prof metadata, we didn't change the branch behavior.
NewSelect->setName(I->getName() + ".neg");
+ // Poison-generating flags should be dropped
+ Value *TV = NewSelect->getTrueValue();
+ Value *FV = NewSelect->getFalseValue();
+ if (match(TV, m_Neg(m_Specific(FV))))
+ cast<Instruction>(TV)->dropPoisonGeneratingFlags();
+ else if (match(FV, m_Neg(m_Specific(TV))))
+ cast<Instruction>(FV)->dropPoisonGeneratingFlags();
+ else {
+ cast<Instruction>(TV)->dropPoisonGeneratingFlags();
+ cast<Instruction>(FV)->dropPoisonGeneratingFlags();
+ }
Builder.Insert(NewSelect);
return NewSelect;
}
diff --git a/llvm/test/Transforms/InstCombine/sub-of-negatible.ll b/llvm/test/Transforms/InstCombine/sub-of-negatible.ll
index b19eae4d8f9a41..533db3144630ea 100644
--- a/llvm/test/Transforms/InstCombine/sub-of-negatible.ll
+++ b/llvm/test/Transforms/InstCombine/sub-of-negatible.ll
@@ -1374,6 +1374,48 @@ define i8 @negate_select_of_op_vs_negated_op(i8 %x, i8 %y, i1 %c) {
%t2 = sub i8 %y, %t1
ret i8 %t2
}
+
+define i8 @negate_select_of_op_vs_negated_op_nsw(i8 %x, i8 %y, i1 %c) {
+; CHECK-LABEL: @negate_select_of_op_vs_negated_op_nsw(
+; CHECK-NEXT: [[T0:%.*]] = sub i8 0, [[X:%.*]]
+; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[C:%.*]], i8 [[X]], i8 [[T0]]
+; CHECK-NEXT: [[T2:%.*]] = add i8 [[TMP1]], [[Y:%.*]]
+; CHECK-NEXT: ret i8 [[T2]]
+;
+ %t0 = sub nsw i8 0, %x
+ %t1 = select i1 %c, i8 %t0, i8 %x
+ %t2 = sub i8 %y, %t1
+ ret i8 %t2
+}
+
+define i8 @negate_select_of_op_vs_negated_op_nsw_commuted(i8 %x, i8 %y, i1 %c) {
+; CHECK-LABEL: @negate_select_of_op_vs_negated_op_nsw_commuted(
+; CHECK-NEXT: [[T0:%.*]] = sub i8 0, [[X:%.*]]
+; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[C:%.*]], i8 [[T0]], i8 [[X]]
+; CHECK-NEXT: [[T2:%.*]] = add i8 [[TMP1]], [[Y:%.*]]
+; CHECK-NEXT: ret i8 [[T2]]
+;
+ %t0 = sub nsw i8 0, %x
+ %t1 = select i1 %c, i8 %x, i8 %t0
+ %t2 = sub i8 %y, %t1
+ ret i8 %t2
+}
+
+define i8 @negate_select_of_op_vs_negated_op_nsw_xyyx(i8 %x, i8 %y, i8 %z, i1 %c) {
+; CHECK-LABEL: @negate_select_of_op_vs_negated_op_nsw_xyyx(
+; CHECK-NEXT: [[SUB1:%.*]] = sub i8 [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT: [[SUB2:%.*]] = sub i8 [[Y]], [[X]]
+; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[C:%.*]], i8 [[SUB2]], i8 [[SUB1]]
+; CHECK-NEXT: [[T2:%.*]] = add i8 [[TMP1]], [[Z:%.*]]
+; CHECK-NEXT: ret i8 [[T2]]
+;
+ %sub1 = sub nsw i8 %x, %y
+ %sub2 = sub nsw i8 %y, %x
+ %t1 = select i1 %c, i8 %sub1, i8 %sub2
+ %t2 = sub i8 %z, %t1
+ ret i8 %t2
+}
+
define i8 @dont_negate_ordinary_select(i8 %x, i8 %y, i8 %z, i1 %c) {
; CHECK-LABEL: @dont_negate_ordinary_select(
; CHECK-NEXT: [[T0:%.*]] = select i1 [[C:%.*]], i8 [[X:%.*]], i8 [[Y:%.*]]
|
nikic
approved these changes
Nov 7, 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
This was referenced Nov 8, 2024
AZero13
pushed a commit
to AZero13/llvm-project
that referenced
this pull request
Nov 14, 2024
Closes llvm#112666 and llvm#114181. (cherry picked from commit ff07df6)
Groverkss
pushed a commit
to iree-org/llvm-project
that referenced
this pull request
Nov 15, 2024
tru
pushed a commit
to AZero13/llvm-project
that referenced
this pull request
Nov 18, 2024
Closes llvm#112666 and llvm#114181. (cherry picked from commit ff07df6)
nikic
pushed a commit
to rust-lang/llvm-project
that referenced
this pull request
Nov 20, 2024
Closes llvm#112666 and llvm#114181. (cherry picked from commit ff07df6)
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 #112666 and #114181.