-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[InstCombine] Simplify commutative compares of symmetric pairs #80134
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
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) ChangesFixes #78038. Full diff: https://github.com/llvm/llvm-project/pull/80134.diff 3 Files Affected:
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index eab2c8bd8efde..d295853798b80 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -7055,6 +7055,14 @@ Instruction *InstCombinerImpl::visitICmpInst(ICmpInst &I) {
foldICmpCommutative(I.getSwappedPredicate(), Op1, Op0, I))
return Res;
+ if (I.isCommutative()) {
+ if (auto Pair = matchSymmetricPair(I.getOperand(0), I.getOperand(1))) {
+ replaceOperand(I, 0, Pair->first);
+ replaceOperand(I, 1, Pair->second);
+ return &I;
+ }
+ }
+
// In case of a comparison with two select instructions having the same
// condition, check whether one of the resulting branches can be simplified.
// If so, just compare the other branch and select the appropriate result.
@@ -7668,6 +7676,14 @@ Instruction *InstCombinerImpl::visitFCmpInst(FCmpInst &I) {
}
}
+ if (I.isCommutative()) {
+ if (auto Pair = matchSymmetricPair(I.getOperand(0), I.getOperand(1))) {
+ replaceOperand(I, 0, Pair->first);
+ replaceOperand(I, 1, Pair->second);
+ return &I;
+ }
+ }
+
// If we're just checking for a NaN (ORD/UNO) and have a non-NaN operand,
// then canonicalize the operand to 0.0.
if (Pred == CmpInst::FCMP_ORD || Pred == CmpInst::FCMP_UNO) {
diff --git a/llvm/test/Transforms/InstCombine/fcmp-select.ll b/llvm/test/Transforms/InstCombine/fcmp-select.ll
index b4ab8ef725d71..f37c586845b12 100644
--- a/llvm/test/Transforms/InstCombine/fcmp-select.ll
+++ b/llvm/test/Transforms/InstCombine/fcmp-select.ll
@@ -114,3 +114,37 @@ define double @one_swapped(double %x) {
ret double %cond
}
+define i1 @fcmp_oeq_select(i1 %cond, float %a, float %b) {
+; CHECK-LABEL: @fcmp_oeq_select(
+; CHECK-NEXT: [[RES:%.*]] = fcmp oeq float [[A:%.*]], [[B:%.*]]
+; CHECK-NEXT: ret i1 [[RES]]
+;
+ %lhs = select i1 %cond, float %a, float %b
+ %rhs = select i1 %cond, float %b, float %a
+ %res = fcmp oeq float %lhs, %rhs
+ ret i1 %res
+}
+
+define i1 @fcmp_uno_select(i1 %cond, float %a, float %b) {
+; CHECK-LABEL: @fcmp_uno_select(
+; CHECK-NEXT: [[RES:%.*]] = fcmp uno float [[A:%.*]], [[B:%.*]]
+; CHECK-NEXT: ret i1 [[RES]]
+;
+ %lhs = select i1 %cond, float %a, float %b
+ %rhs = select i1 %cond, float %b, float %a
+ %res = fcmp uno float %lhs, %rhs
+ ret i1 %res
+}
+
+define i1 @fcmp_ogt_select(i1 %cond, float %a, float %b) {
+; CHECK-LABEL: @fcmp_ogt_select(
+; CHECK-NEXT: [[LHS:%.*]] = select i1 [[COND:%.*]], float [[A:%.*]], float [[B:%.*]]
+; CHECK-NEXT: [[RHS:%.*]] = select i1 [[COND]], float [[B]], float [[A]]
+; CHECK-NEXT: [[RES:%.*]] = fcmp ogt float [[LHS]], [[RHS]]
+; CHECK-NEXT: ret i1 [[RES]]
+;
+ %lhs = select i1 %cond, float %a, float %b
+ %rhs = select i1 %cond, float %b, float %a
+ %res = fcmp ogt float %lhs, %rhs
+ ret i1 %res
+}
diff --git a/llvm/test/Transforms/InstCombine/icmp-select.ll b/llvm/test/Transforms/InstCombine/icmp-select.ll
index 0d723c9df32e2..59d2a1b165c0f 100644
--- a/llvm/test/Transforms/InstCombine/icmp-select.ll
+++ b/llvm/test/Transforms/InstCombine/icmp-select.ll
@@ -604,3 +604,27 @@ define i1 @select_constants_and_icmp_ne_fval(i1 %x, i1 %y) {
%cmp = icmp ne i8 %and, 3
ret i1 %cmp
}
+
+define i1 @icmp_eq_select(i1 %cond, i32 %a, i32 %b) {
+; CHECK-LABEL: @icmp_eq_select(
+; CHECK-NEXT: [[RES:%.*]] = icmp eq i32 [[A:%.*]], [[B:%.*]]
+; CHECK-NEXT: ret i1 [[RES]]
+;
+ %lhs = select i1 %cond, i32 %a, i32 %b
+ %rhs = select i1 %cond, i32 %b, i32 %a
+ %res = icmp eq i32 %lhs, %rhs
+ ret i1 %res
+}
+
+define i1 @icmp_slt_select(i1 %cond, i32 %a, i32 %b) {
+; CHECK-LABEL: @icmp_slt_select(
+; CHECK-NEXT: [[LHS:%.*]] = select i1 [[COND:%.*]], i32 [[A:%.*]], i32 [[B:%.*]]
+; CHECK-NEXT: [[RHS:%.*]] = select i1 [[COND]], i32 [[B]], i32 [[A]]
+; CHECK-NEXT: [[RES:%.*]] = icmp slt i32 [[LHS]], [[RHS]]
+; CHECK-NEXT: ret i1 [[RES]]
+;
+ %lhs = select i1 %cond, i32 %a, i32 %b
+ %rhs = select i1 %cond, i32 %b, i32 %a
+ %res = icmp slt i32 %lhs, %rhs
+ ret i1 %res
+}
|
nikic
approved these changes
Jan 31, 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
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 #78038.