Skip to content

Commit 8f6a647

Browse files
committed
Add helper function for flag rewriting
1 parent 631f066 commit 8f6a647

File tree

3 files changed

+84
-14
lines changed

3 files changed

+84
-14
lines changed

llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2742,6 +2742,17 @@ Instruction *InstCombinerImpl::visitSub(BinaryOperator &I) {
27422742
return TryToNarrowDeduceFlags();
27432743
}
27442744

2745+
static FastMathFlags combineFastMathFlagsForFNeg(FastMathFlags FMF,
2746+
FastMathFlags OpFMF) {
2747+
FastMathFlags Flag = FMF | OpFMF;
2748+
Flag.setNoInfs(FMF.noInfs() && OpFMF.noInfs());
2749+
Flag.setAllowReassoc(FMF.allowReassoc() && OpFMF.allowReassoc());
2750+
Flag.setApproxFunc(FMF.approxFunc() && OpFMF.approxFunc());
2751+
Flag.setAllowReciprocal(FMF.allowReciprocal() && OpFMF.allowReciprocal());
2752+
Flag.setAllowContract(FMF.allowContract() && OpFMF.allowContract());
2753+
return Flag;
2754+
}
2755+
27452756
/// This eliminates floating-point negation in either 'fneg(X)' or
27462757
/// 'fsub(-0.0, X)' form by combining into a constant operand.
27472758
static Instruction *foldFNegIntoConstant(Instruction &I, const DataLayout &DL) {
@@ -2759,11 +2770,10 @@ static Instruction *foldFNegIntoConstant(Instruction &I, const DataLayout &DL) {
27592770
// -(X * C) --> X * (-C)
27602771
if (match(FNegOp, m_FMul(m_Value(X), m_Constant(C))))
27612772
if (Constant *NegC = ConstantFoldUnaryOpOperand(Instruction::FNeg, C, DL)) {
2762-
FastMathFlags FMF = I.getFastMathFlags();
2763-
FastMathFlags OpFMF = FNegOp->getFastMathFlags();
2764-
FastMathFlags Flag = FMF | OpFMF;
2765-
Flag.setNoInfs(FMF.noInfs() && OpFMF.noInfs());
2766-
return BinaryOperator::CreateFMulFMF(X, NegC, Flag);
2773+
return BinaryOperator::CreateFMulFMF(
2774+
X, NegC,
2775+
combineFastMathFlagsForFNeg(I.getFastMathFlags(),
2776+
FNegOp->getFastMathFlags()));
27672777
}
27682778
// -(X / C) --> X / (-C)
27692779
if (match(FNegOp, m_FDiv(m_Value(X), m_Constant(C))))

llvm/test/Transforms/InstCombine/fneg.ll

Lines changed: 68 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,8 +1203,8 @@ define float @test_fneg_ninf_mul_nsz_with_const(float %a) {
12031203
ret float %f2
12041204
}
12051205

1206-
define <2 x float> @test_fneg_ninf_mul_nnan_with_vec_const(<2 x float> %a) {
1207-
; CHECK-LABEL: @test_fneg_ninf_mul_nnan_with_vec_const(
1206+
define <2 x float> @test_fneg_mul_combine_nnan_ninf_with_vec_const(<2 x float> %a) {
1207+
; CHECK-LABEL: @test_fneg_mul_combine_nnan_ninf_with_vec_const(
12081208
; CHECK-NEXT: [[F2:%.*]] = fmul nnan <2 x float> [[A:%.*]], <float -0.000000e+00, float 0.000000e+00>
12091209
; CHECK-NEXT: ret <2 x float> [[F2]]
12101210
;
@@ -1213,8 +1213,8 @@ define <2 x float> @test_fneg_ninf_mul_nnan_with_vec_const(<2 x float> %a) {
12131213
ret <2 x float> %f2
12141214
}
12151215

1216-
define <2 x float> @test_fneg_ninf_mul_nsz_with_vec_const(<2 x float> %a) {
1217-
; CHECK-LABEL: @test_fneg_ninf_mul_nsz_with_vec_const(
1216+
define <2 x float> @test_fneg_mul_combine_nsz_ninf_with_vec_const(<2 x float> %a) {
1217+
; CHECK-LABEL: @test_fneg_mul_combine_nsz_ninf_with_vec_const(
12181218
; CHECK-NEXT: [[F2:%.*]] = fmul nsz <2 x float> [[A:%.*]], <float -0.000000e+00, float 0.000000e+00>
12191219
; CHECK-NEXT: ret <2 x float> [[F2]]
12201220
;
@@ -1223,8 +1223,8 @@ define <2 x float> @test_fneg_ninf_mul_nsz_with_vec_const(<2 x float> %a) {
12231223
ret <2 x float> %f2
12241224
}
12251225

1226-
define <2 x float> @test_fneg_nnan_ninf_mul_with_vec_const(<2 x float> %a) {
1227-
; CHECK-LABEL: @test_fneg_nnan_ninf_mul_with_vec_const(
1226+
define <2 x float> @test_fneg_ninf_nnan_mul_with_vec_const(<2 x float> %a) {
1227+
; CHECK-LABEL: @test_fneg_ninf_nnan_mul_with_vec_const(
12281228
; CHECK-NEXT: [[F2:%.*]] = fmul nnan <2 x float> [[A:%.*]], <float -0.000000e+00, float 0.000000e+00>
12291229
; CHECK-NEXT: ret <2 x float> [[F2]]
12301230
;
@@ -1233,8 +1233,8 @@ define <2 x float> @test_fneg_nnan_ninf_mul_with_vec_const(<2 x float> %a) {
12331233
ret <2 x float> %f2
12341234
}
12351235

1236-
define <2 x float> @test_fneg_nnan_ninf_mul_ninf_with_vec_const(<2 x float> %a) {
1237-
; CHECK-LABEL: @test_fneg_nnan_ninf_mul_ninf_with_vec_const(
1236+
define <2 x float> @test_fneg_mul_combine_nnan_ninf_with_vec_const2(<2 x float> %a) {
1237+
; CHECK-LABEL: @test_fneg_mul_combine_nnan_ninf_with_vec_const2(
12381238
; CHECK-NEXT: [[F2:%.*]] = fmul nnan ninf <2 x float> [[A:%.*]], <float -0.000000e+00, float 0.000000e+00>
12391239
; CHECK-NEXT: ret <2 x float> [[F2]]
12401240
;
@@ -1243,4 +1243,64 @@ define <2 x float> @test_fneg_nnan_ninf_mul_ninf_with_vec_const(<2 x float> %a)
12431243
ret <2 x float> %f2
12441244
}
12451245

1246+
define <2 x float> @test_fneg_mul_combine_reassoc_ninf_with_vec_const1(<2 x float> %a) {
1247+
; CHECK-LABEL: @test_fneg_mul_combine_reassoc_ninf_with_vec_const1(
1248+
; CHECK-NEXT: [[F2:%.*]] = fmul <2 x float> [[A:%.*]], <float -0.000000e+00, float 0.000000e+00>
1249+
; CHECK-NEXT: ret <2 x float> [[F2]]
1250+
;
1251+
%f1 = fmul reassoc <2 x float> %a, <float 0.000000, float -0.000000>
1252+
%f2 = fneg ninf <2 x float> %f1
1253+
ret <2 x float> %f2
1254+
}
1255+
1256+
define <2 x float> @test_fneg_mul_combine_reassoc_ninf_with_vec_const2(<2 x float> %a) {
1257+
; CHECK-LABEL: @test_fneg_mul_combine_reassoc_ninf_with_vec_const2(
1258+
; CHECK-NEXT: [[F2:%.*]] = fmul ninf <2 x float> [[A:%.*]], <float -0.000000e+00, float 0.000000e+00>
1259+
; CHECK-NEXT: ret <2 x float> [[F2]]
1260+
;
1261+
%f1 = fmul ninf <2 x float> %a, <float 0.000000, float -0.000000>
1262+
%f2 = fneg reassoc ninf <2 x float> %f1
1263+
ret <2 x float> %f2
1264+
}
1265+
1266+
define <2 x float> @test_fneg_mul_combine_reassoc_ninf_with_vec_const3(<2 x float> %a) {
1267+
; CHECK-LABEL: @test_fneg_mul_combine_reassoc_ninf_with_vec_const3(
1268+
; CHECK-NEXT: [[F2:%.*]] = fmul reassoc <2 x float> [[A:%.*]], <float -0.000000e+00, float 0.000000e+00>
1269+
; CHECK-NEXT: ret <2 x float> [[F2]]
1270+
;
1271+
%f1 = fmul reassoc <2 x float> %a, <float 0.000000, float -0.000000>
1272+
%f2 = fneg reassoc ninf <2 x float> %f1
1273+
ret <2 x float> %f2
1274+
}
1275+
1276+
define <2 x float> @test_fneg_mul_combine_contract_ninf_with_vec_const1(<2 x float> %a) {
1277+
; CHECK-LABEL: @test_fneg_mul_combine_contract_ninf_with_vec_const1(
1278+
; CHECK-NEXT: [[F2:%.*]] = fmul <2 x float> [[A:%.*]], <float -0.000000e+00, float 0.000000e+00>
1279+
; CHECK-NEXT: ret <2 x float> [[F2]]
1280+
;
1281+
%f1 = fmul contract <2 x float> %a, <float 0.000000, float -0.000000>
1282+
%f2 = fneg ninf <2 x float> %f1
1283+
ret <2 x float> %f2
1284+
}
1285+
1286+
define <2 x float> @test_fneg_mul_combine_contract_ninf_with_vec_const2(<2 x float> %a) {
1287+
; CHECK-LABEL: @test_fneg_mul_combine_contract_ninf_with_vec_const2(
1288+
; CHECK-NEXT: [[F2:%.*]] = fmul ninf <2 x float> [[A:%.*]], <float -0.000000e+00, float 0.000000e+00>
1289+
; CHECK-NEXT: ret <2 x float> [[F2]]
1290+
;
1291+
%f1 = fmul ninf <2 x float> %a, <float 0.000000, float -0.000000>
1292+
%f2 = fneg contract ninf <2 x float> %f1
1293+
ret <2 x float> %f2
1294+
}
1295+
1296+
define <2 x float> @test_fneg_mul_combine_contract_ninf_with_vec_const3(<2 x float> %a) {
1297+
; CHECK-LABEL: @test_fneg_mul_combine_contract_ninf_with_vec_const3(
1298+
; CHECK-NEXT: [[F2:%.*]] = fmul contract <2 x float> [[A:%.*]], <float -0.000000e+00, float 0.000000e+00>
1299+
; CHECK-NEXT: ret <2 x float> [[F2]]
1300+
;
1301+
%f1 = fmul contract <2 x float> %a, <float 0.000000, float -0.000000>
1302+
%f2 = fneg contract ninf <2 x float> %f1
1303+
ret <2 x float> %f2
1304+
}
1305+
12461306
!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 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)