Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit 752da31

Browse files
committed
[InstCombine] fold fneg into constant operand of fmul/fdiv
This accounts for the missing IR fold noted in D50195. We don't need any fast-math to enable the negation transform. FP negation can always be folded into an fmul/fdiv constant to eliminate the fneg. I've limited this to one-use to ensure that we are eliminating an instruction rather than replacing fneg by a potentially expensive fdiv or fmul. Differential Revision: https://reviews.llvm.org/D50417 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@339248 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent cee6409 commit 752da31

File tree

3 files changed

+29
-25
lines changed

3 files changed

+29
-25
lines changed

lib/Transforms/InstCombine/InstCombineAddSub.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1847,13 +1847,27 @@ Instruction *InstCombiner::visitFSub(BinaryOperator &I) {
18471847
if (I.hasNoSignedZeros() && match(Op0, m_PosZeroFP()))
18481848
return BinaryOperator::CreateFNegFMF(Op1, &I);
18491849

1850+
Value *X, *Y;
1851+
Constant *C;
1852+
1853+
// Fold negation into constant operand. This is limited with one-use because
1854+
// fneg is assumed better for analysis and cheaper in codegen than fmul/fdiv.
1855+
// -(X * C) --> X * (-C)
1856+
if (match(&I, m_FNeg(m_OneUse(m_FMul(m_Value(X), m_Constant(C))))))
1857+
return BinaryOperator::CreateFMulFMF(X, ConstantExpr::getFNeg(C), &I);
1858+
// -(X / C) --> X / (-C)
1859+
if (match(&I, m_FNeg(m_OneUse(m_FDiv(m_Value(X), m_Constant(C))))))
1860+
return BinaryOperator::CreateFDivFMF(X, ConstantExpr::getFNeg(C), &I);
1861+
// -(C / X) --> (-C) / X
1862+
if (match(&I, m_FNeg(m_OneUse(m_FDiv(m_Constant(C), m_Value(X))))))
1863+
return BinaryOperator::CreateFDivFMF(ConstantExpr::getFNeg(C), X, &I);
1864+
18501865
// If Op0 is not -0.0 or we can ignore -0.0: Z - (X - Y) --> Z + (Y - X)
18511866
// Canonicalize to fadd to make analysis easier.
18521867
// This can also help codegen because fadd is commutative.
18531868
// Note that if this fsub was really an fneg, the fadd with -0.0 will get
18541869
// killed later. We still limit that particular transform with 'hasOneUse'
18551870
// because an fneg is assumed better/cheaper than a generic fsub.
1856-
Value *X, *Y;
18571871
if (I.hasNoSignedZeros() || CannotBeNegativeZero(Op0, SQ.TLI)) {
18581872
if (match(Op1, m_OneUse(m_FSub(m_Value(X), m_Value(Y))))) {
18591873
Value *NewSub = Builder.CreateFSubFMF(Y, X, &I);
@@ -1869,7 +1883,6 @@ Instruction *InstCombiner::visitFSub(BinaryOperator &I) {
18691883
// X - C --> X + (-C)
18701884
// But don't transform constant expressions because there's an inverse fold
18711885
// for X + (-Y) --> X - Y.
1872-
Constant *C;
18731886
if (match(Op1, m_Constant(C)) && !isa<ConstantExpr>(Op1))
18741887
return BinaryOperator::CreateFAddFMF(Op0, ConstantExpr::getFNeg(C), &I);
18751888

test/Transforms/InstCombine/fneg.ll

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ declare void @use(float)
77

88
define float @fmul_fneg(float %x) {
99
; CHECK-LABEL: @fmul_fneg(
10-
; CHECK-NEXT: [[M:%.*]] = fmul float [[X:%.*]], 4.200000e+01
11-
; CHECK-NEXT: [[R:%.*]] = fsub float -0.000000e+00, [[M]]
10+
; CHECK-NEXT: [[R:%.*]] = fmul float [[X:%.*]], -4.200000e+01
1211
; CHECK-NEXT: ret float [[R]]
1312
;
1413
%m = fmul float %x, 42.0
@@ -20,8 +19,7 @@ define float @fmul_fneg(float %x) {
2019

2120
define float @fmul_fneg_fmf(float %x) {
2221
; CHECK-LABEL: @fmul_fneg_fmf(
23-
; CHECK-NEXT: [[M:%.*]] = fmul float [[X:%.*]], 4.200000e+01
24-
; CHECK-NEXT: [[R:%.*]] = fsub reassoc nsz float -0.000000e+00, [[M]]
22+
; CHECK-NEXT: [[R:%.*]] = fmul reassoc nsz float [[X:%.*]], -4.200000e+01
2523
; CHECK-NEXT: ret float [[R]]
2624
;
2725
%m = fmul float %x, 42.0
@@ -48,8 +46,7 @@ define float @fmul_fneg_extra_use(float %x) {
4846

4947
define <4 x double> @fmul_fneg_vec(<4 x double> %x) {
5048
; CHECK-LABEL: @fmul_fneg_vec(
51-
; CHECK-NEXT: [[M:%.*]] = fmul <4 x double> [[X:%.*]], <double 4.200000e+01, double 0xFF80000000000000, double 0x7FF0000000000000, double undef>
52-
; CHECK-NEXT: [[R:%.*]] = fsub <4 x double> <double -0.000000e+00, double -0.000000e+00, double -0.000000e+00, double -0.000000e+00>, [[M]]
49+
; CHECK-NEXT: [[R:%.*]] = fmul <4 x double> [[X:%.*]], <double -4.200000e+01, double 0x7F80000000000000, double 0xFFF0000000000000, double 0x7FF8000000000000>
5350
; CHECK-NEXT: ret <4 x double> [[R]]
5451
;
5552
%m = fmul <4 x double> %x, <double 42.0, double 0x7FF80000000000000, double 0x7FF0000000000000, double undef>
@@ -61,8 +58,7 @@ define <4 x double> @fmul_fneg_vec(<4 x double> %x) {
6158

6259
define float @fdiv_op1_constant_fneg(float %x) {
6360
; CHECK-LABEL: @fdiv_op1_constant_fneg(
64-
; CHECK-NEXT: [[D:%.*]] = fdiv float [[X:%.*]], -4.200000e+01
65-
; CHECK-NEXT: [[R:%.*]] = fsub float -0.000000e+00, [[D]]
61+
; CHECK-NEXT: [[R:%.*]] = fdiv float [[X:%.*]], 4.200000e+01
6662
; CHECK-NEXT: ret float [[R]]
6763
;
6864
%d = fdiv float %x, -42.0
@@ -74,8 +70,7 @@ define float @fdiv_op1_constant_fneg(float %x) {
7470

7571
define float @fdiv_op1_constant_fneg_fmf(float %x) {
7672
; CHECK-LABEL: @fdiv_op1_constant_fneg_fmf(
77-
; CHECK-NEXT: [[D:%.*]] = fdiv float [[X:%.*]], -4.200000e+01
78-
; CHECK-NEXT: [[R:%.*]] = fsub nnan float -0.000000e+00, [[D]]
73+
; CHECK-NEXT: [[R:%.*]] = fdiv nnan float [[X:%.*]], 4.200000e+01
7974
; CHECK-NEXT: ret float [[R]]
8075
;
8176
%d = fdiv float %x, -42.0
@@ -102,8 +97,7 @@ define float @fdiv_op1_constant_fneg_extra_use(float %x) {
10297

10398
define <4 x double> @fdiv_op1_constant_fneg_vec(<4 x double> %x) {
10499
; CHECK-LABEL: @fdiv_op1_constant_fneg_vec(
105-
; CHECK-NEXT: [[D:%.*]] = fdiv <4 x double> [[X:%.*]], <double -4.200000e+01, double 0xFFF800000ABCD000, double 0xFFF0000000000000, double undef>
106-
; CHECK-NEXT: [[R:%.*]] = fsub <4 x double> <double -0.000000e+00, double -0.000000e+00, double -0.000000e+00, double -0.000000e+00>, [[D]]
100+
; CHECK-NEXT: [[R:%.*]] = fdiv <4 x double> [[X:%.*]], <double 4.200000e+01, double 0x7FF800000ABCD000, double 0x7FF0000000000000, double 0x7FF8000000000000>
107101
; CHECK-NEXT: ret <4 x double> [[R]]
108102
;
109103
%d = fdiv <4 x double> %x, <double -42.0, double 0xFFF800000ABCD000, double 0xFFF0000000000000, double undef>
@@ -115,8 +109,7 @@ define <4 x double> @fdiv_op1_constant_fneg_vec(<4 x double> %x) {
115109

116110
define float @fdiv_op0_constant_fneg(float %x) {
117111
; CHECK-LABEL: @fdiv_op0_constant_fneg(
118-
; CHECK-NEXT: [[D:%.*]] = fdiv float 4.200000e+01, [[X:%.*]]
119-
; CHECK-NEXT: [[R:%.*]] = fsub float -0.000000e+00, [[D]]
112+
; CHECK-NEXT: [[R:%.*]] = fdiv float -4.200000e+01, [[X:%.*]]
120113
; CHECK-NEXT: ret float [[R]]
121114
;
122115
%d = fdiv float 42.0, %x
@@ -128,8 +121,7 @@ define float @fdiv_op0_constant_fneg(float %x) {
128121

129122
define float @fdiv_op0_constant_fneg_fmf(float %x) {
130123
; CHECK-LABEL: @fdiv_op0_constant_fneg_fmf(
131-
; CHECK-NEXT: [[D:%.*]] = fdiv float 4.200000e+01, [[X:%.*]]
132-
; CHECK-NEXT: [[R:%.*]] = fsub fast float -0.000000e+00, [[D]]
124+
; CHECK-NEXT: [[R:%.*]] = fdiv fast float -4.200000e+01, [[X:%.*]]
133125
; CHECK-NEXT: ret float [[R]]
134126
;
135127
%d = fdiv float 42.0, %x
@@ -156,8 +148,7 @@ define float @fdiv_op0_constant_fneg_extra_use(float %x) {
156148

157149
define <4 x double> @fdiv_op0_constant_fneg_vec(<4 x double> %x) {
158150
; CHECK-LABEL: @fdiv_op0_constant_fneg_vec(
159-
; CHECK-NEXT: [[D:%.*]] = fdiv <4 x double> <double -4.200000e+01, double 0xFF80000000000000, double 0xFFF0000000000000, double undef>, [[X:%.*]]
160-
; CHECK-NEXT: [[R:%.*]] = fsub <4 x double> <double -0.000000e+00, double -0.000000e+00, double -0.000000e+00, double -0.000000e+00>, [[D]]
151+
; CHECK-NEXT: [[R:%.*]] = fdiv <4 x double> <double 4.200000e+01, double 0x7F80000000000000, double 0x7FF0000000000000, double 0x7FF8000000000000>, [[X:%.*]]
161152
; CHECK-NEXT: ret <4 x double> [[R]]
162153
;
163154
%d = fdiv <4 x double> <double -42.0, double 0x7FF80000000000000, double 0xFFF0000000000000, double undef>, %x

test/Transforms/InstCombine/fsub.ll

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,15 @@ define float @sub_sub_nsz(float %x, float %y, float %z) {
6060
ret float %t2
6161
}
6262

63-
; With nsz and reassoc: Y - (X + Y) --> -X
63+
; With nsz and reassoc: Y - ((X * 5) + Y) --> X * -5
64+
6465
define float @sub_add_neg_x(float %x, float %y) {
6566
; CHECK-LABEL: @sub_add_neg_x(
66-
; CHECK-NEXT: [[TMP1:%.*]] = fmul float [[X:%.*]], 5.000000e+00
67-
; CHECK-NEXT: [[T2:%.*]] = fsub reassoc nsz float -0.000000e+00, [[TMP1]]
68-
; CHECK-NEXT: ret float [[T2]]
67+
; CHECK-NEXT: [[TMP1:%.*]] = fmul reassoc nsz float [[X:%.*]], -5.000000e+00
68+
; CHECK-NEXT: ret float [[TMP1]]
6969
;
7070
%mul = fmul float %x, 5.000000e+00
71-
%add = fadd float %mul, %y
71+
%add = fadd float %mul, %y
7272
%r = fsub nsz reassoc float %y, %add
7373
ret float %r
7474
}

0 commit comments

Comments
 (0)