Skip to content

Commit 4656f88

Browse files
committed
Propagate the FNegOp FMF flag instead of I.
1 parent 0bf11f0 commit 4656f88

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
@@ -2741,22 +2741,6 @@ Instruction *InstCombinerImpl::visitSub(BinaryOperator &I) {
27412741
return TryToNarrowDeduceFlags();
27422742
}
27432743

2744-
static FastMathFlags getCorrectFMFForFNeg(FastMathFlags F1, FastMathFlags F2) {
2745-
2746-
FastMathFlags NF1 = F1 & F2;
2747-
FastMathFlags NF2 = F1 | F2;
2748-
if (NF2.noNaNs())
2749-
return F2;
2750-
2751-
if (NF1.any() || (F1.none() && F2.none()))
2752-
return NF1;
2753-
2754-
if (F1.all() || F2.all())
2755-
return F2;
2756-
2757-
return NF1;
2758-
}
2759-
27602744
/// This eliminates floating-point negation in either 'fneg(X)' or
27612745
/// 'fsub(-0.0, X)' form by combining into a constant operand.
27622746
static Instruction *foldFNegIntoConstant(Instruction &I, const DataLayout &DL) {
@@ -2773,15 +2757,8 @@ static Instruction *foldFNegIntoConstant(Instruction &I, const DataLayout &DL) {
27732757
// Fold negation into constant operand.
27742758
// -(X * C) --> X * (-C)
27752759
if (match(FNegOp, m_FMul(m_Value(X), m_Constant(C))))
2776-
if (Constant *NegC = ConstantFoldUnaryOpOperand(Instruction::FNeg, C, DL)) {
2777-
FastMathFlags FMF = cast<FPMathOperator>(I.getOperand(0))->getFastMathFlags();
2778-
if (I.getFastMathFlags().noInfs()) {
2779-
return BinaryOperator::CreateFMulFMF(
2780-
X, NegC,
2781-
getCorrectFMFForFNeg(I.getFastMathFlags(), FMF));
2782-
}
2783-
return BinaryOperator::CreateFMulFMF(X, NegC, &I);
2784-
}
2760+
if (Constant *NegC = ConstantFoldUnaryOpOperand(Instruction::FNeg, C, DL))
2761+
return BinaryOperator::CreateFMulFMF(X, NegC, FNegOp);
27852762
// -(X / C) --> X / (-C)
27862763
if (match(FNegOp, m_FDiv(m_Value(X), m_Constant(C))))
27872764
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)