-
Notifications
You must be signed in to change notification settings - Fork 10.8k
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] Implement fcmp (fadd x, 0.0), y
=> fcmp x, y
optimization
#88476
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write If you have received no comments on your PR for a week, you can request a review If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@dtcxzyw Please take a look whenever you have time :) Let me know if I should make any changes. Question: Should I also add tests for the case of |
@llvm/pr-subscribers-llvm-transforms Author: Vlad Mishel (vmishelcs) ChangesThis PR addresses issue #88168. I have implemented an optimization for the case of
and all other types of Proofs: Full diff: https://github.com/llvm/llvm-project/pull/88476.diff 2 Files Affected:
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index 7292bb62702aaa..67f3e0386402b1 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -8100,6 +8100,13 @@ Instruction *InstCombinerImpl::visitFCmpInst(FCmpInst &I) {
return new FCmpInst(I.getSwappedPredicate(), X, NegC, "", &I);
}
+ // fcmp (fadd X, 0.0), Y --> fcmp X, Y
+ if (match(Op0, m_FAdd(m_Value(X), m_APFloat(C))) && C->isZero()) {
+ if (match(Op1, m_Value(Y))) {
+ return new FCmpInst(Pred, X, Y, "", &I);
+ }
+ }
+
if (match(Op0, m_FPExt(m_Value(X)))) {
// fcmp (fpext X), (fpext Y) -> fcmp X, Y
if (match(Op1, m_FPExt(m_Value(Y))) && X->getType() == Y->getType())
diff --git a/llvm/test/Transforms/InstCombine/fcmp.ll b/llvm/test/Transforms/InstCombine/fcmp.ll
index f2701d16d0f3d1..003483c217771c 100644
--- a/llvm/test/Transforms/InstCombine/fcmp.ll
+++ b/llvm/test/Transforms/InstCombine/fcmp.ll
@@ -1284,3 +1284,145 @@ define <1 x i1> @bitcast_1vec_eq0(i32 %x) {
%cmp = fcmp oeq <1 x float> %f, zeroinitializer
ret <1 x i1> %cmp
}
+
+; Simplify fcmp (x + 0.0), y => fcmp x, y
+
+define i1 @fcmp_fadd_zero_ugt(float %x, float %y) {
+; CHECK-LABEL: @fcmp_fadd_zero_ugt(
+; CHECK-NEXT: [[CMP:%.*]] = fcmp ugt float [[ADD:%.*]], [[Y:%.*]]
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %add = fadd float %x, 0.000000e+00
+ %cmp = fcmp ugt float %add, %y
+ ret i1 %cmp
+}
+
+define i1 @fcmp_fadd_zero_uge(float %x, float %y) {
+; CHECK-LABEL: @fcmp_fadd_zero_uge(
+; CHECK-NEXT: [[CMP:%.*]] = fcmp uge float [[ADD:%.*]], [[Y:%.*]]
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %add = fadd float %x, 0.000000e+00
+ %cmp = fcmp uge float %add, %y
+ ret i1 %cmp
+}
+
+define i1 @fcmp_fadd_zero_ogt(float %x, float %y) {
+; CHECK-LABEL: @fcmp_fadd_zero_ogt(
+; CHECK-NEXT: [[CMP:%.*]] = fcmp ogt float [[ADD:%.*]], [[Y:%.*]]
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %add = fadd float %x, 0.000000e+00
+ %cmp = fcmp ogt float %add, %y
+ ret i1 %cmp
+}
+
+define i1 @fcmp_fadd_zero_oge(float %x, float %y) {
+; CHECK-LABEL: @fcmp_fadd_zero_oge(
+; CHECK-NEXT: [[CMP:%.*]] = fcmp oge float [[ADD:%.*]], [[Y:%.*]]
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %add = fadd float %x, 0.000000e+00
+ %cmp = fcmp oge float %add, %y
+ ret i1 %cmp
+}
+
+define i1 @fcmp_fadd_zero_ult(float %x, float %y) {
+; CHECK-LABEL: @fcmp_fadd_zero_ult(
+; CHECK-NEXT: [[CMP:%.*]] = fcmp ult float [[ADD:%.*]], [[Y:%.*]]
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %add = fadd float %x, 0.000000e+00
+ %cmp = fcmp ult float %add, %y
+ ret i1 %cmp
+}
+
+define i1 @fcmp_fadd_zero_ule(float %x, float %y) {
+; CHECK-LABEL: @fcmp_fadd_zero_ule(
+; CHECK-NEXT: [[CMP:%.*]] = fcmp ule float [[ADD:%.*]], [[Y:%.*]]
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %add = fadd float %x, 0.000000e+00
+ %cmp = fcmp ule float %add, %y
+ ret i1 %cmp
+}
+
+define i1 @fcmp_fadd_zero_olt(float %x, float %y) {
+; CHECK-LABEL: @fcmp_fadd_zero_olt(
+; CHECK-NEXT: [[CMP:%.*]] = fcmp olt float [[ADD:%.*]], [[Y:%.*]]
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %add = fadd float %x, 0.000000e+00
+ %cmp = fcmp olt float %add, %y
+ ret i1 %cmp
+}
+
+define i1 @fcmp_fadd_zero_ole(float %x, float %y) {
+; CHECK-LABEL: @fcmp_fadd_zero_ole(
+; CHECK-NEXT: [[CMP:%.*]] = fcmp ole float [[ADD:%.*]], [[Y:%.*]]
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %add = fadd float %x, 0.000000e+00
+ %cmp = fcmp ole float %add, %y
+ ret i1 %cmp
+}
+
+define i1 @fcmp_fadd_zero_oeq(float %x, float %y) {
+; CHECK-LABEL: @fcmp_fadd_zero_oeq(
+; CHECK-NEXT: [[CMP:%.*]] = fcmp oeq float [[ADD:%.*]], [[Y:%.*]]
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %add = fadd float %x, 0.000000e+00
+ %cmp = fcmp oeq float %add, %y
+ ret i1 %cmp
+}
+
+define i1 @fcmp_fadd_zero_one(float %x, float %y) {
+; CHECK-LABEL: @fcmp_fadd_zero_one(
+; CHECK-NEXT: [[CMP:%.*]] = fcmp one float [[ADD:%.*]], [[Y:%.*]]
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %add = fadd float %x, 0.000000e+00
+ %cmp = fcmp one float %add, %y
+ ret i1 %cmp
+}
+
+define i1 @fcmp_fadd_zero_ueq(float %x, float %y) {
+; CHECK-LABEL: @fcmp_fadd_zero_ueq(
+; CHECK-NEXT: [[CMP:%.*]] = fcmp ueq float [[ADD:%.*]], [[Y:%.*]]
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %add = fadd float %x, 0.000000e+00
+ %cmp = fcmp ueq float %add, %y
+ ret i1 %cmp
+}
+
+define i1 @fcmp_fadd_zero_une(float %x, float %y) {
+; CHECK-LABEL: @fcmp_fadd_zero_une(
+; CHECK-NEXT: [[CMP:%.*]] = fcmp une float [[ADD:%.*]], [[Y:%.*]]
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %add = fadd float %x, 0.000000e+00
+ %cmp = fcmp une float %add, %y
+ ret i1 %cmp
+}
+
+define i1 @fcmp_fadd_zero_ord(float %x, float %y) {
+; CHECK-LABEL: @fcmp_fadd_zero_ord(
+; CHECK-NEXT: [[CMP:%.*]] = fcmp ord float [[ADD:%.*]], [[Y:%.*]]
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %add = fadd float %x, 0.000000e+00
+ %cmp = fcmp ord float %add, %y
+ ret i1 %cmp
+}
+
+define i1 @fcmp_fadd_zero_uno(float %x, float %y) {
+; CHECK-LABEL: @fcmp_fadd_zero_uno(
+; CHECK-NEXT: [[CMP:%.*]] = fcmp uno float [[ADD:%.*]], [[Y:%.*]]
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %add = fadd float %x, 0.000000e+00
+ %cmp = fcmp uno float %add, %y
+ ret i1 %cmp
+}
|
Yeah, |
if (match(Op0, m_FAdd(m_Value(X), m_APFloat(C))) && C->isZero()) { | ||
if (match(Op1, m_Value(Y))) { | ||
return new FCmpInst(Pred, X, Y, "", &I); | ||
} | ||
} |
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.
if (match(Op0, m_FAdd(m_Value(X), m_APFloat(C))) && C->isZero()) { | |
if (match(Op1, m_Value(Y))) { | |
return new FCmpInst(Pred, X, Y, "", &I); | |
} | |
} | |
if (match(Op0, m_FAdd(m_Value(X), m_AnyZeroFP()))) | |
return new FCmpInst(Pred, X, Op1, "", &I); | |
if (match(Op1, m_FAdd(m_Value(X), m_AnyZeroFP()))) | |
return new FCmpInst(Pred, Op0, X, "", &I); |
- Add a test for
fcmp X, (fadd Y, 0.0)
- Add a vector test for
fcmp (fadd X, <0.0, -0.0>), Y
- Add a test for fast-math flag preservation
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.
- Add a test for
fcmp X, (fadd Y, 0.0)
- Add a vector test for
fcmp (fadd X, <0.0, -0.0>), Y
- Add a test for fast-math flag preservation
Should I write a separate test case for each type of fcmp
instruction (ugt
, ogt
, etc) for each of these? Or is one test case sufficient for each?
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.
Just one test each is fine.
ret <2 x i1> %cmp | ||
} | ||
|
||
define i1 @fcmp_fadd_zero_fast(float %x, float %y) { |
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.
Test cases where each instruction has flags, and the other doesn't. Also add a scalar case with negative 0
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.
Specifically you have to make sure the flags don't come from the fadd, they cannot be preserved
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.
Specifically you have to make sure the flags don't come from the fadd, they cannot be preserved
Added! The behaviour seems to be correct from what I can tell.
I have a quick question: How come for some of the tests, the CHECK
lines generate as
define i1 @fcmp_fadd_zero_ugt(float %x, float %y) {
; CHECK-LABEL: @fcmp_fadd_zero_ugt(
; CHECK-NEXT: [[CMP:%.*]] = fcmp ugt float [[ADD:%.*]], [[Y:%.*]]
; CHECK-NEXT: ret i1 [[CMP]]
;
%add = fadd float %x, 0.000000e+00
%cmp = fcmp ugt float %add, %y
ret i1 %cmp
}
and for the tests which I added later, the CHECK
lines are
define i1 @fcmp_fadd_neg_zero(float %x, float %y) {
; CHECK-LABEL: @fcmp_fadd_neg_zero(
; CHECK-NEXT: [[CMP:%.*]] = fcmp ugt float [[X:%.*]], [[Y:%.*]]
; CHECK-NEXT: ret i1 [[CMP]]
;
%add = fadd float %x, -0.000000e+00
%cmp = fcmp ugt float %add, %y
ret i1 %cmp
}
?
In particular, why is it that for the former test, the CHECK
line uses [[ADD:%.*]]
but the latter uses [[X:%.*]]
? When I ran the opt
binary, the result is correct in both cases, but I was wondering why the slight difference.
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.
I'm not sure exactly where update_test_checks pulls the name from. It looks like it tried the source in the first place?
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.
Sounds like a regression from preserving variable names in UTC, cc @nhaehnle.
✅ With the latest revision this PR passed the C/C++ code formatter. |
@dtcxzyw I see there are some issues with the formatting. Is there a script I can run and commit the changes with fixed formatting? |
|
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 with the formatting fixed.
Formatting should be fixed now. |
@vmishelcs Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested Please check whether problems have been caused by your change specifically, as How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
This PR addresses issue #88168. I have implemented an optimization for the case of
=>
and all other types of
fcmp
instructions (uge
,ogt
, etc). I have also added tests tollvm/test/Transforms/InstCombine/fcmp.ll
to check this behaviour.Proofs:
fadd x, 0.0
https://alive2.llvm.org/ce/z/7FzNnMfsub x, 0.0
https://alive2.llvm.org/ce/z/puUxLK