[InstCombine] allow fdiv with constant dividend folds with less than full -ffast-math

It's possible that we could allow this either 'arcp' or 'reassoc' alone, but this
should be conservatively better than what we have right now. GCC allows this with
only -freciprocal-math.

The last test is changed to show a case that is expected to fold, but we need D43398.

llvm-svn: 325533
This commit is contained in:
Sanjay Patel 2018-02-19 21:46:52 +00:00
parent 49a9d1a4e6
commit 1d14779aed
2 changed files with 32 additions and 8 deletions

View File

@ -1316,10 +1316,11 @@ static Instruction *foldFDivConstantDivisor(BinaryOperator &FDiv) {
return BinaryOperator::CreateFMul(FDiv.getOperand(0), RecipCFP);
}
/// Try to strength-reduce C / X expressions where X includes another constant.
/// Try to reassociate C / X expressions where X includes another constant.
static Instruction *foldFDivConstantDividend(BinaryOperator &I) {
Constant *C1;
if (!I.isFast() || !match(I.getOperand(0), m_Constant(C1)))
if (!I.hasAllowReassoc() || !I.hasAllowReciprocal() ||
!match(I.getOperand(0), m_Constant(C1)))
return nullptr;
Value *X;

View File

@ -237,11 +237,22 @@ define <2 x float> @div_factor_commute(<2 x float> %x, <2 x float> %y) {
define <2 x float> @div_constant_dividend1(<2 x float> %x) {
; CHECK-LABEL: @div_constant_dividend1(
; CHECK-NEXT: [[T2:%.*]] = fdiv fast <2 x float> <float 5.000000e+00, float 1.000000e+00>, [[X:%.*]]
; CHECK-NEXT: [[T2:%.*]] = fdiv reassoc arcp <2 x float> <float 5.000000e+00, float 1.000000e+00>, [[X:%.*]]
; CHECK-NEXT: ret <2 x float> [[T2]]
;
%t1 = fmul <2 x float> %x, <float 3.0e0, float 7.0e0>
%t2 = fdiv fast <2 x float> <float 15.0e0, float 7.0e0>, %t1
%t2 = fdiv arcp reassoc <2 x float> <float 15.0e0, float 7.0e0>, %t1
ret <2 x float> %t2
}
define <2 x float> @div_constant_dividend1_arcp_only(<2 x float> %x) {
; CHECK-LABEL: @div_constant_dividend1_arcp_only(
; CHECK-NEXT: [[T1:%.*]] = fmul <2 x float> [[X:%.*]], <float 3.000000e+00, float 7.000000e+00>
; CHECK-NEXT: [[T2:%.*]] = fdiv arcp <2 x float> <float 1.500000e+01, float 7.000000e+00>, [[T1]]
; CHECK-NEXT: ret <2 x float> [[T2]]
;
%t1 = fmul <2 x float> %x, <float 3.0e0, float 7.0e0>
%t2 = fdiv arcp <2 x float> <float 15.0e0, float 7.0e0>, %t1
ret <2 x float> %t2
}
@ -249,11 +260,22 @@ define <2 x float> @div_constant_dividend1(<2 x float> %x) {
define <2 x float> @div_constant_dividend2(<2 x float> %x) {
; CHECK-LABEL: @div_constant_dividend2(
; CHECK-NEXT: [[T2:%.*]] = fdiv fast <2 x float> <float 4.500000e+01, float 4.900000e+01>, [[X:%.*]]
; CHECK-NEXT: [[T2:%.*]] = fdiv reassoc arcp <2 x float> <float 4.500000e+01, float 4.900000e+01>, [[X:%.*]]
; CHECK-NEXT: ret <2 x float> [[T2]]
;
%t1 = fdiv <2 x float> %x, <float 3.0e0, float -7.0e0>
%t2 = fdiv fast <2 x float> <float 15.0e0, float -7.0e0>, %t1
%t2 = fdiv arcp reassoc <2 x float> <float 15.0e0, float -7.0e0>, %t1
ret <2 x float> %t2
}
define <2 x float> @div_constant_dividend2_reassoc_only(<2 x float> %x) {
; CHECK-LABEL: @div_constant_dividend2_reassoc_only(
; CHECK-NEXT: [[T1:%.*]] = fdiv <2 x float> [[X:%.*]], <float 3.000000e+00, float -7.000000e+00>
; CHECK-NEXT: [[T2:%.*]] = fdiv reassoc <2 x float> <float 1.500000e+01, float -7.000000e+00>, [[T1]]
; CHECK-NEXT: ret <2 x float> [[T2]]
;
%t1 = fdiv <2 x float> %x, <float 3.0e0, float -7.0e0>
%t2 = fdiv reassoc <2 x float> <float 15.0e0, float -7.0e0>, %t1
ret <2 x float> %t2
}
@ -262,11 +284,12 @@ define <2 x float> @div_constant_dividend2(<2 x float> %x) {
define <2 x float> @div_constant_dividend3(<2 x float> %x) {
; CHECK-LABEL: @div_constant_dividend3(
; CHECK-NEXT: [[T2:%.*]] = fmul fast <2 x float> [[X:%.*]], <float 5.000000e+00, float -1.000000e+00>
; CHECK-NEXT: [[T1:%.*]] = fdiv <2 x float> <float 3.000000e+00, float 7.000000e+00>, [[X:%.*]]
; CHECK-NEXT: [[T2:%.*]] = fdiv reassoc arcp <2 x float> <float 1.500000e+01, float -7.000000e+00>, [[T1]]
; CHECK-NEXT: ret <2 x float> [[T2]]
;
%t1 = fdiv <2 x float> <float 3.0e0, float 7.0e0>, %x
%t2 = fdiv fast <2 x float> <float 15.0e0, float -7.0e0>, %t1
%t2 = fdiv arcp reassoc <2 x float> <float 15.0e0, float -7.0e0>, %t1
ret <2 x float> %t2
}