Skip to content

Commit 0d761fb

Browse files
committed
Update Flag Propagation Logic
1 parent 4656f88 commit 0d761fb

File tree

3 files changed

+13
-8
lines changed

3 files changed

+13
-8
lines changed

llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2757,8 +2757,12 @@ static Instruction *foldFNegIntoConstant(Instruction &I, const DataLayout &DL) {
27572757
// Fold negation into constant operand.
27582758
// -(X * C) --> X * (-C)
27592759
if (match(FNegOp, m_FMul(m_Value(X), m_Constant(C))))
2760-
if (Constant *NegC = ConstantFoldUnaryOpOperand(Instruction::FNeg, C, DL))
2761-
return BinaryOperator::CreateFMulFMF(X, NegC, FNegOp);
2760+
if (Constant *NegC = ConstantFoldUnaryOpOperand(Instruction::FNeg, C, DL)) {
2761+
FastMathFlags Flag = I.getFastMathFlags() | FNegOp->getFastMathFlags();
2762+
Flag.setNoInfs(I.getFastMathFlags().noInfs() &&
2763+
FNegOp->getFastMathFlags().noInfs());
2764+
return BinaryOperator::CreateFMulFMF(X, NegC, Flag);
2765+
}
27622766
// -(X / C) --> X / (-C)
27632767
if (match(FNegOp, m_FDiv(m_Value(X), m_Constant(C))))
27642768
if (Constant *NegC = ConstantFoldUnaryOpOperand(Instruction::FNeg, C, DL))

llvm/test/Transforms/InstCombine/fneg.ll

Lines changed: 6 additions & 5 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 float [[X:%.*]], -4.200000e+01
44+
; CHECK-NEXT: [[R:%.*]] = fmul reassoc nsz 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 float [[X:%.*]], -4.200000e+01
54+
; CHECK-NEXT: [[R:%.*]] = fmul reassoc nsz float [[X:%.*]], -4.200000e+01
5555
; CHECK-NEXT: ret float [[R]]
5656
;
5757
%m = fmul float %x, 42.0
@@ -1131,7 +1131,8 @@ define float @test_fsub_ninf_mul_with_anyzero(float %a) {
11311131

11321132
define float @test_fneg_nnan_mul_with_anyzero(float %a) {
11331133
; CHECK-LABEL: @test_fneg_nnan_mul_with_anyzero(
1134-
; CHECK-NEXT: [[F2:%.*]] = fmul ninf float [[A:%.*]], -0.000000e+00
1134+
; CHECK-NEXT: [[TMP1:%.*]] = fneg nnan float [[A:%.*]]
1135+
; CHECK-NEXT: [[F2:%.*]] = call nnan float @llvm.copysign.f32(float 0.000000e+00, float [[TMP1]])
11351136
; CHECK-NEXT: ret float [[F2]]
11361137
;
11371138
%f1 = fmul ninf float %a, 0.000000
@@ -1141,7 +1142,7 @@ define float @test_fneg_nnan_mul_with_anyzero(float %a) {
11411142

11421143
define float @test_fneg_nsz_mul_with_anyzero(float %a) {
11431144
; CHECK-LABEL: @test_fneg_nsz_mul_with_anyzero(
1144-
; CHECK-NEXT: [[F2:%.*]] = fmul ninf float [[A:%.*]], -0.000000e+00
1145+
; CHECK-NEXT: [[F2:%.*]] = fmul nsz float [[A:%.*]], -0.000000e+00
11451146
; CHECK-NEXT: ret float [[F2]]
11461147
;
11471148
%f1 = fmul ninf float %a, 0.000000
@@ -1192,7 +1193,7 @@ define <2 x float> @test_fneg_ninf_mul_nsz_with_vec_const(<2 x float> %a) {
11921193

11931194
define <2 x float> @test_fneg_nnan_ninf_mul_with_vec_const(<2 x float> %a) {
11941195
; 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: [[F2:%.*]] = fmul nnan <2 x float> [[A:%.*]], <float -0.000000e+00, float 0.000000e+00>
11961197
; CHECK-NEXT: ret <2 x float> [[F2]]
11971198
;
11981199
%f1 = fmul <2 x float> %a, <float 0.000000, float -0.000000>

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 float [[X:%.*]], -5.000000e+00
101+
; CHECK-NEXT: [[R:%.*]] = fmul reassoc nsz 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)