Skip to content

Commit bf2263b

Browse files
committed
Propagate the FNegOp FMF flag instead of I.
1 parent b039a91 commit bf2263b

File tree

3 files changed

+22
-37
lines changed

3 files changed

+22
-37
lines changed

llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2666,22 +2666,6 @@ Instruction *InstCombinerImpl::visitSub(BinaryOperator &I) {
26662666
return TryToNarrowDeduceFlags();
26672667
}
26682668

2669-
static FastMathFlags getCorrectFMFForFNeg(FastMathFlags F1, FastMathFlags F2) {
2670-
2671-
FastMathFlags NF1 = F1 & F2;
2672-
FastMathFlags NF2 = F1 | F2;
2673-
if (NF2.noNaNs())
2674-
return F2;
2675-
2676-
if (NF1.any() || (F1.none() && F2.none()))
2677-
return NF1;
2678-
2679-
if (F1.all() || F2.all())
2680-
return F2;
2681-
2682-
return NF1;
2683-
}
2684-
26852669
/// This eliminates floating-point negation in either 'fneg(X)' or
26862670
/// 'fsub(-0.0, X)' form by combining into a constant operand.
26872671
static Instruction *foldFNegIntoConstant(Instruction &I, const DataLayout &DL) {
@@ -2698,15 +2682,8 @@ static Instruction *foldFNegIntoConstant(Instruction &I, const DataLayout &DL) {
26982682
// Fold negation into constant operand.
26992683
// -(X * C) --> X * (-C)
27002684
if (match(FNegOp, m_FMul(m_Value(X), m_Constant(C))))
2701-
if (Constant *NegC = ConstantFoldUnaryOpOperand(Instruction::FNeg, C, DL)) {
2702-
FastMathFlags FMF = cast<FPMathOperator>(I.getOperand(0))->getFastMathFlags();
2703-
if (I.getFastMathFlags().noInfs()) {
2704-
return BinaryOperator::CreateFMulFMF(
2705-
X, NegC,
2706-
getCorrectFMFForFNeg(I.getFastMathFlags(), FMF));
2707-
}
2708-
return BinaryOperator::CreateFMulFMF(X, NegC, &I);
2709-
}
2685+
if (Constant *NegC = ConstantFoldUnaryOpOperand(Instruction::FNeg, C, DL))
2686+
return BinaryOperator::CreateFMulFMF(X, NegC, FNegOp);
27102687
// -(X / C) --> X / (-C)
27112688
if (match(FNegOp, m_FDiv(m_Value(X), m_Constant(C))))
27122689
if (Constant *NegC = ConstantFoldUnaryOpOperand(Instruction::FNeg, C, DL))

llvm/test/Transforms/InstCombine/fneg.ll

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ define float @fmul_fneg(float %x) {
4141

4242
define float @fmul_fsub_fmf(float %x) {
4343
; CHECK-LABEL: @fmul_fsub_fmf(
44-
; CHECK-NEXT: [[R:%.*]] = fmul reassoc nsz float [[X:%.*]], -4.200000e+01
44+
; CHECK-NEXT: [[R:%.*]] = fmul float [[X:%.*]], -4.200000e+01
4545
; CHECK-NEXT: ret float [[R]]
4646
;
4747
%m = fmul float %x, 42.0
@@ -51,7 +51,7 @@ define float @fmul_fsub_fmf(float %x) {
5151

5252
define float @fmul_fneg_fmf(float %x) {
5353
; CHECK-LABEL: @fmul_fneg_fmf(
54-
; CHECK-NEXT: [[R:%.*]] = fmul reassoc nsz float [[X:%.*]], -4.200000e+01
54+
; CHECK-NEXT: [[R:%.*]] = fmul float [[X:%.*]], -4.200000e+01
5555
; CHECK-NEXT: ret float [[R]]
5656
;
5757
%m = fmul float %x, 42.0
@@ -1121,31 +1121,30 @@ define float @test_fneg_ninf_mul_with_anyzero(float %a) {
11211121

11221122
define float @test_fsub_ninf_mul_with_anyzero(float %a) {
11231123
; CHECK-LABEL: @test_fsub_ninf_mul_with_anyzero(
1124-
; CHECK-NEXT: [[F2:%.*]] = fmul float [[A:%.*]], -0.000000e+00
1124+
; CHECK-NEXT: [[F2:%.*]] = fmul nsz float [[A:%.*]], -0.000000e+00
11251125
; CHECK-NEXT: ret float [[F2]]
11261126
;
1127-
%f1 = fmul float %a, 0.000000
1127+
%f1 = fmul nsz float %a, 0.000000
11281128
%f2 = fsub ninf float -0.000000, %f1
11291129
ret float %f2
11301130
}
11311131

11321132
define float @test_fneg_nnan_mul_with_anyzero(float %a) {
11331133
; CHECK-LABEL: @test_fneg_nnan_mul_with_anyzero(
1134-
; CHECK-NEXT: [[TMP1:%.*]] = fneg nnan float [[A:%.*]]
1135-
; CHECK-NEXT: [[F2:%.*]] = call nnan float @llvm.copysign.f32(float 0.000000e+00, float [[TMP1]])
1134+
; CHECK-NEXT: [[F2:%.*]] = fmul ninf float [[A:%.*]], -0.000000e+00
11361135
; CHECK-NEXT: ret float [[F2]]
11371136
;
1138-
%f1 = fmul float %a, 0.000000
1137+
%f1 = fmul ninf float %a, 0.000000
11391138
%f2 = fneg nnan float %f1
11401139
ret float %f2
11411140
}
11421141

11431142
define float @test_fneg_nsz_mul_with_anyzero(float %a) {
11441143
; CHECK-LABEL: @test_fneg_nsz_mul_with_anyzero(
1145-
; CHECK-NEXT: [[F2:%.*]] = fmul nsz float [[A:%.*]], -0.000000e+00
1144+
; CHECK-NEXT: [[F2:%.*]] = fmul ninf float [[A:%.*]], -0.000000e+00
11461145
; CHECK-NEXT: ret float [[F2]]
11471146
;
1148-
%f1 = fmul float %a, 0.000000
1147+
%f1 = fmul ninf float %a, 0.000000
11491148
%f2 = fneg nsz float %f1
11501149
ret float %f2
11511150
}
@@ -1163,7 +1162,7 @@ define float @test_fneg_ninf_mul_nnan_with_const(float %a) {
11631162

11641163
define float @test_fneg_ninf_mul_nsz_with_const(float %a) {
11651164
; CHECK-LABEL: @test_fneg_ninf_mul_nsz_with_const(
1166-
; CHECK-NEXT: [[F2:%.*]] = fmul float [[A:%.*]], -0.000000e+00
1165+
; CHECK-NEXT: [[F2:%.*]] = fmul nsz float [[A:%.*]], -0.000000e+00
11671166
; CHECK-NEXT: ret float [[F2]]
11681167
;
11691168
%f1 = fmul nsz float %a, 0.000000
@@ -1183,13 +1182,22 @@ define <2 x float> @test_fneg_ninf_mul_nnan_with_vec_const(<2 x float> %a) {
11831182

11841183
define <2 x float> @test_fneg_ninf_mul_nsz_with_vec_const(<2 x float> %a) {
11851184
; CHECK-LABEL: @test_fneg_ninf_mul_nsz_with_vec_const(
1186-
; CHECK-NEXT: [[F2:%.*]] = fmul <2 x float> [[A:%.*]], <float -0.000000e+00, float 0.000000e+00>
1185+
; CHECK-NEXT: [[F2:%.*]] = fmul nsz <2 x float> [[A:%.*]], <float -0.000000e+00, float 0.000000e+00>
11871186
; CHECK-NEXT: ret <2 x float> [[F2]]
11881187
;
11891188
%f1 = fmul nsz <2 x float> %a, <float 0.000000, float -0.000000>
11901189
%f2 = fneg ninf <2 x float> %f1
11911190
ret <2 x float> %f2
11921191
}
11931192

1193+
define <2 x float> @test_fneg_nnan_ninf_mul_with_vec_const(<2 x float> %a) {
1194+
; CHECK-LABEL: @test_fneg_nnan_ninf_mul_with_vec_const(
1195+
; CHECK-NEXT: [[F2:%.*]] = fmul <2 x float> [[A:%.*]], <float -0.000000e+00, float 0.000000e+00>
1196+
; CHECK-NEXT: ret <2 x float> [[F2]]
1197+
;
1198+
%f1 = fmul <2 x float> %a, <float 0.000000, float -0.000000>
1199+
%f2 = fneg nnan ninf <2 x float> %f1
1200+
ret <2 x float> %f2
1201+
}
11941202

11951203
!0 = !{}

llvm/test/Transforms/InstCombine/fsub.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ define float @sub_sub_nsz(float %x, float %y, float %z) {
9898

9999
define float @sub_add_neg_x(float %x, float %y) {
100100
; CHECK-LABEL: @sub_add_neg_x(
101-
; CHECK-NEXT: [[R:%.*]] = fmul reassoc nsz float [[X:%.*]], -5.000000e+00
101+
; CHECK-NEXT: [[R:%.*]] = fmul float [[X:%.*]], -5.000000e+00
102102
; CHECK-NEXT: ret float [[R]]
103103
;
104104
%mul = fmul float %x, 5.000000e+00

0 commit comments

Comments
 (0)