Skip to content

Commit

Permalink
Push fneg into RHS instead of LHS
Browse files Browse the repository at this point in the history
This is better because we canonicalize fneg to the right, so this
will cancel out two fnegs.
  • Loading branch information
nikic committed Aug 20, 2024
1 parent 6f90ac1 commit 4ba1ed2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 16 deletions.
4 changes: 3 additions & 1 deletion llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2719,8 +2719,10 @@ Instruction *InstCombinerImpl::hoistFNegAboveFMulFDiv(Value *FNegOp,
Instruction &FMFSource) {
Value *X, *Y;
if (match(FNegOp, m_FMul(m_Value(X), m_Value(Y)))) {
// Push into RHS which is more likely to simplify (const or another fneg).
// FIXME: It would be better to invert the transform.
return cast<Instruction>(Builder.CreateFMulFMF(
Builder.CreateFNegFMF(X, &FMFSource), Y, &FMFSource));
X, Builder.CreateFNegFMF(Y, &FMFSource), &FMFSource));
}

if (match(FNegOp, m_FDiv(m_Value(X), m_Value(Y)))) {
Expand Down
25 changes: 10 additions & 15 deletions llvm/test/Transforms/InstCombine/fast-basictest.ll
Original file line number Diff line number Diff line change
Expand Up @@ -549,9 +549,8 @@ define float @test16_reassoc(float %a, float %b, float %z) {

define float @test17(float %a, float %b, float %z) {
; CHECK-LABEL: @test17(
; CHECK-NEXT: [[C:%.*]] = fmul fast float [[Z:%.*]], -4.000000e+01
; CHECK-NEXT: [[TMP1:%.*]] = fneg fast float [[A:%.*]]
; CHECK-NEXT: [[F:%.*]] = fmul fast float [[C]], [[TMP1]]
; CHECK-NEXT: [[TMP1:%.*]] = fmul fast float [[Z:%.*]], 4.000000e+01
; CHECK-NEXT: [[F:%.*]] = fmul fast float [[A:%.*]], [[TMP1]]
; CHECK-NEXT: ret float [[F]]
;
%d = fmul fast float %z, 4.000000e+01
Expand All @@ -563,9 +562,8 @@ define float @test17(float %a, float %b, float %z) {

define float @test17_unary_fneg(float %a, float %b, float %z) {
; CHECK-LABEL: @test17_unary_fneg(
; CHECK-NEXT: [[C:%.*]] = fmul fast float [[Z:%.*]], -4.000000e+01
; CHECK-NEXT: [[TMP1:%.*]] = fneg fast float [[A:%.*]]
; CHECK-NEXT: [[F:%.*]] = fmul fast float [[C]], [[TMP1]]
; CHECK-NEXT: [[TMP1:%.*]] = fmul fast float [[Z:%.*]], 4.000000e+01
; CHECK-NEXT: [[F:%.*]] = fmul fast float [[A:%.*]], [[TMP1]]
; CHECK-NEXT: ret float [[F]]
;
%d = fmul fast float %z, 4.000000e+01
Expand All @@ -577,9 +575,8 @@ define float @test17_unary_fneg(float %a, float %b, float %z) {

define float @test17_reassoc_nsz(float %a, float %b, float %z) {
; CHECK-LABEL: @test17_reassoc_nsz(
; CHECK-NEXT: [[C:%.*]] = fmul reassoc nsz float [[Z:%.*]], -4.000000e+01
; CHECK-NEXT: [[TMP1:%.*]] = fneg reassoc nsz float [[A:%.*]]
; CHECK-NEXT: [[F:%.*]] = fmul reassoc nsz float [[C]], [[TMP1]]
; CHECK-NEXT: [[TMP1:%.*]] = fmul reassoc nsz float [[Z:%.*]], 4.000000e+01
; CHECK-NEXT: [[F:%.*]] = fmul reassoc nsz float [[A:%.*]], [[TMP1]]
; CHECK-NEXT: ret float [[F]]
;
%d = fmul reassoc nsz float %z, 4.000000e+01
Expand Down Expand Up @@ -609,9 +606,8 @@ define float @test17_reassoc(float %a, float %b, float %z) {

define float @test17_unary_fneg_no_FMF(float %a, float %b, float %z) {
; CHECK-LABEL: @test17_unary_fneg_no_FMF(
; CHECK-NEXT: [[C:%.*]] = fmul float [[Z:%.*]], -4.000000e+01
; CHECK-NEXT: [[TMP1:%.*]] = fneg float [[A:%.*]]
; CHECK-NEXT: [[F:%.*]] = fmul float [[C]], [[TMP1]]
; CHECK-NEXT: [[TMP1:%.*]] = fmul float [[Z:%.*]], 4.000000e+01
; CHECK-NEXT: [[F:%.*]] = fmul float [[A:%.*]], [[TMP1]]
; CHECK-NEXT: ret float [[F]]
;
%d = fmul float %z, 4.000000e+01
Expand All @@ -623,9 +619,8 @@ define float @test17_unary_fneg_no_FMF(float %a, float %b, float %z) {

define float @test17_reassoc_unary_fneg(float %a, float %b, float %z) {
; CHECK-LABEL: @test17_reassoc_unary_fneg(
; CHECK-NEXT: [[C:%.*]] = fmul reassoc float [[Z:%.*]], -4.000000e+01
; CHECK-NEXT: [[TMP1:%.*]] = fneg reassoc float [[A:%.*]]
; CHECK-NEXT: [[F:%.*]] = fmul reassoc float [[C]], [[TMP1]]
; CHECK-NEXT: [[TMP1:%.*]] = fmul reassoc float [[Z:%.*]], 4.000000e+01
; CHECK-NEXT: [[F:%.*]] = fmul reassoc float [[A:%.*]], [[TMP1]]
; CHECK-NEXT: ret float [[F]]
;
%d = fmul reassoc float %z, 4.000000e+01
Expand Down

0 comments on commit 4ba1ed2

Please sign in to comment.