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

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 19 13:46:52 PST 2018


Author: spatel
Date: Mon Feb 19 13:46:52 2018
New Revision: 325533

URL: http://llvm.org/viewvc/llvm-project?rev=325533&view=rev
Log:
[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.

Modified:
    llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
    llvm/trunk/test/Transforms/InstCombine/fdiv.ll

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp?rev=325533&r1=325532&r2=325533&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp Mon Feb 19 13:46:52 2018
@@ -1316,10 +1316,11 @@ static Instruction *foldFDivConstantDivi
   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;

Modified: llvm/trunk/test/Transforms/InstCombine/fdiv.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/fdiv.ll?rev=325533&r1=325532&r2=325533&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/fdiv.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/fdiv.ll Mon Feb 19 13:46:52 2018
@@ -237,11 +237,22 @@ define <2 x float> @div_factor_commute(<
 
 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_dividen
 
 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 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 fast <2 x float> <float 15.0e0, float -7.0e0>, %t1
+  %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_dividen
 
 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
 }
 




More information about the llvm-commits mailing list