Skip to content

Commit cad89bf

Browse files
committed
Add helper function for flag rewriting
1 parent e37cdc8 commit cad89bf

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

2744+
static FastMathFlags combineFastMathFlagsForFNeg(FastMathFlags FMF,
2745+
FastMathFlags OpFMF) {
2746+
FastMathFlags Flag = FMF | OpFMF;
2747+
Flag.setNoInfs(FMF.noInfs() && OpFMF.noInfs());
2748+
Flag.setAllowReassoc(FMF.allowReassoc() && OpFMF.allowReassoc());
2749+
Flag.setApproxFunc(FMF.approxFunc() && OpFMF.approxFunc());
2750+
Flag.setAllowReciprocal(FMF.allowReciprocal() && OpFMF.allowReciprocal());
2751+
Flag.setAllowContract(FMF.allowContract() && OpFMF.allowContract());
2752+
return Flag;
2753+
}
2754+
27442755
/// This eliminates floating-point negation in either 'fneg(X)' or
27452756
/// 'fsub(-0.0, X)' form by combining into a constant operand.
27462757
static Instruction *foldFNegIntoConstant(Instruction &I, const DataLayout &DL) {
@@ -2758,11 +2769,10 @@ static Instruction *foldFNegIntoConstant(Instruction &I, const DataLayout &DL) {
27582769
// -(X * C) --> X * (-C)
27592770
if (match(FNegOp, m_FMul(m_Value(X), m_Constant(C))))
27602771
if (Constant *NegC = ConstantFoldUnaryOpOperand(Instruction::FNeg, C, DL)) {
2761-
FastMathFlags FMF = I.getFastMathFlags();
2762-
FastMathFlags OpFMF = FNegOp->getFastMathFlags();
2763-
FastMathFlags Flag = FMF | OpFMF;
2764-
Flag.setNoInfs(FMF.noInfs() && OpFMF.noInfs());
2765-
return BinaryOperator::CreateFMulFMF(X, NegC, Flag);
2772+
return BinaryOperator::CreateFMulFMF(
2773+
X, NegC,
2774+
combineFastMathFlagsForFNeg(I.getFastMathFlags(),
2775+
FNegOp->getFastMathFlags()));
27662776
}
27672777
// -(X / C) --> X / (-C)
27682778
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
@@ -1171,8 +1171,8 @@ define float @test_fneg_ninf_mul_nsz_with_const(float %a) {
11711171
ret float %f2
11721172
}
11731173

1174-
define <2 x float> @test_fneg_ninf_mul_nnan_with_vec_const(<2 x float> %a) {
1175-
; CHECK-LABEL: @test_fneg_ninf_mul_nnan_with_vec_const(
1174+
define <2 x float> @test_fneg_mul_combine_nnan_ninf_with_vec_const(<2 x float> %a) {
1175+
; CHECK-LABEL: @test_fneg_mul_combine_nnan_ninf_with_vec_const(
11761176
; CHECK-NEXT: [[F2:%.*]] = fmul nnan <2 x float> [[A:%.*]], <float -0.000000e+00, float 0.000000e+00>
11771177
; CHECK-NEXT: ret <2 x float> [[F2]]
11781178
;
@@ -1181,8 +1181,8 @@ define <2 x float> @test_fneg_ninf_mul_nnan_with_vec_const(<2 x float> %a) {
11811181
ret <2 x float> %f2
11821182
}
11831183

1184-
define <2 x float> @test_fneg_ninf_mul_nsz_with_vec_const(<2 x float> %a) {
1185-
; CHECK-LABEL: @test_fneg_ninf_mul_nsz_with_vec_const(
1184+
define <2 x float> @test_fneg_mul_combine_nsz_ninf_with_vec_const(<2 x float> %a) {
1185+
; CHECK-LABEL: @test_fneg_mul_combine_nsz_ninf_with_vec_const(
11861186
; CHECK-NEXT: [[F2:%.*]] = fmul nsz <2 x float> [[A:%.*]], <float -0.000000e+00, float 0.000000e+00>
11871187
; CHECK-NEXT: ret <2 x float> [[F2]]
11881188
;
@@ -1191,8 +1191,8 @@ define <2 x float> @test_fneg_ninf_mul_nsz_with_vec_const(<2 x float> %a) {
11911191
ret <2 x float> %f2
11921192
}
11931193

1194-
define <2 x float> @test_fneg_nnan_ninf_mul_with_vec_const(<2 x float> %a) {
1195-
; CHECK-LABEL: @test_fneg_nnan_ninf_mul_with_vec_const(
1194+
define <2 x float> @test_fneg_ninf_nnan_mul_with_vec_const(<2 x float> %a) {
1195+
; CHECK-LABEL: @test_fneg_ninf_nnan_mul_with_vec_const(
11961196
; CHECK-NEXT: [[F2:%.*]] = fmul nnan <2 x float> [[A:%.*]], <float -0.000000e+00, float 0.000000e+00>
11971197
; CHECK-NEXT: ret <2 x float> [[F2]]
11981198
;
@@ -1201,8 +1201,8 @@ define <2 x float> @test_fneg_nnan_ninf_mul_with_vec_const(<2 x float> %a) {
12011201
ret <2 x float> %f2
12021202
}
12031203

1204-
define <2 x float> @test_fneg_nnan_ninf_mul_ninf_with_vec_const(<2 x float> %a) {
1205-
; CHECK-LABEL: @test_fneg_nnan_ninf_mul_ninf_with_vec_const(
1204+
define <2 x float> @test_fneg_mul_combine_nnan_ninf_with_vec_const2(<2 x float> %a) {
1205+
; CHECK-LABEL: @test_fneg_mul_combine_nnan_ninf_with_vec_const2(
12061206
; CHECK-NEXT: [[F2:%.*]] = fmul nnan ninf <2 x float> [[A:%.*]], <float -0.000000e+00, float 0.000000e+00>
12071207
; CHECK-NEXT: ret <2 x float> [[F2]]
12081208
;
@@ -1211,4 +1211,64 @@ define <2 x float> @test_fneg_nnan_ninf_mul_ninf_with_vec_const(<2 x float> %a)
12111211
ret <2 x float> %f2
12121212
}
12131213

1214+
define <2 x float> @test_fneg_mul_combine_reassoc_ninf_with_vec_const1(<2 x float> %a) {
1215+
; CHECK-LABEL: @test_fneg_mul_combine_reassoc_ninf_with_vec_const1(
1216+
; CHECK-NEXT: [[F2:%.*]] = fmul <2 x float> [[A:%.*]], <float -0.000000e+00, float 0.000000e+00>
1217+
; CHECK-NEXT: ret <2 x float> [[F2]]
1218+
;
1219+
%f1 = fmul reassoc <2 x float> %a, <float 0.000000, float -0.000000>
1220+
%f2 = fneg ninf <2 x float> %f1
1221+
ret <2 x float> %f2
1222+
}
1223+
1224+
define <2 x float> @test_fneg_mul_combine_reassoc_ninf_with_vec_const2(<2 x float> %a) {
1225+
; CHECK-LABEL: @test_fneg_mul_combine_reassoc_ninf_with_vec_const2(
1226+
; CHECK-NEXT: [[F2:%.*]] = fmul ninf <2 x float> [[A:%.*]], <float -0.000000e+00, float 0.000000e+00>
1227+
; CHECK-NEXT: ret <2 x float> [[F2]]
1228+
;
1229+
%f1 = fmul ninf <2 x float> %a, <float 0.000000, float -0.000000>
1230+
%f2 = fneg reassoc ninf <2 x float> %f1
1231+
ret <2 x float> %f2
1232+
}
1233+
1234+
define <2 x float> @test_fneg_mul_combine_reassoc_ninf_with_vec_const3(<2 x float> %a) {
1235+
; CHECK-LABEL: @test_fneg_mul_combine_reassoc_ninf_with_vec_const3(
1236+
; CHECK-NEXT: [[F2:%.*]] = fmul reassoc <2 x float> [[A:%.*]], <float -0.000000e+00, float 0.000000e+00>
1237+
; CHECK-NEXT: ret <2 x float> [[F2]]
1238+
;
1239+
%f1 = fmul reassoc <2 x float> %a, <float 0.000000, float -0.000000>
1240+
%f2 = fneg reassoc ninf <2 x float> %f1
1241+
ret <2 x float> %f2
1242+
}
1243+
1244+
define <2 x float> @test_fneg_mul_combine_contract_ninf_with_vec_const1(<2 x float> %a) {
1245+
; CHECK-LABEL: @test_fneg_mul_combine_contract_ninf_with_vec_const1(
1246+
; CHECK-NEXT: [[F2:%.*]] = fmul <2 x float> [[A:%.*]], <float -0.000000e+00, float 0.000000e+00>
1247+
; CHECK-NEXT: ret <2 x float> [[F2]]
1248+
;
1249+
%f1 = fmul contract <2 x float> %a, <float 0.000000, float -0.000000>
1250+
%f2 = fneg ninf <2 x float> %f1
1251+
ret <2 x float> %f2
1252+
}
1253+
1254+
define <2 x float> @test_fneg_mul_combine_contract_ninf_with_vec_const2(<2 x float> %a) {
1255+
; CHECK-LABEL: @test_fneg_mul_combine_contract_ninf_with_vec_const2(
1256+
; CHECK-NEXT: [[F2:%.*]] = fmul ninf <2 x float> [[A:%.*]], <float -0.000000e+00, float 0.000000e+00>
1257+
; CHECK-NEXT: ret <2 x float> [[F2]]
1258+
;
1259+
%f1 = fmul ninf <2 x float> %a, <float 0.000000, float -0.000000>
1260+
%f2 = fneg contract ninf <2 x float> %f1
1261+
ret <2 x float> %f2
1262+
}
1263+
1264+
define <2 x float> @test_fneg_mul_combine_contract_ninf_with_vec_const3(<2 x float> %a) {
1265+
; CHECK-LABEL: @test_fneg_mul_combine_contract_ninf_with_vec_const3(
1266+
; CHECK-NEXT: [[F2:%.*]] = fmul contract <2 x float> [[A:%.*]], <float -0.000000e+00, float 0.000000e+00>
1267+
; CHECK-NEXT: ret <2 x float> [[F2]]
1268+
;
1269+
%f1 = fmul contract <2 x float> %a, <float 0.000000, float -0.000000>
1270+
%f2 = fneg contract ninf <2 x float> %f1
1271+
ret <2 x float> %f2
1272+
}
1273+
12141274
!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)