[llvm] r358404 - [InstCombine] canonicalize fdiv after fmul if reassociation is allowed
Sanjay Patel via llvm-commits
llvm-commits at lists.llvm.org
Mon Apr 15 06:23:38 PDT 2019
Author: spatel
Date: Mon Apr 15 06:23:38 2019
New Revision: 358404
URL: http://llvm.org/viewvc/llvm-project?rev=358404&view=rev
Log:
[InstCombine] canonicalize fdiv after fmul if reassociation is allowed
(X / Y) * Z --> (X * Z) / Y
This can allow other optimizations/reassociations as shown in the test diffs.
Modified:
llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
llvm/trunk/test/Transforms/InstCombine/fast-math.ll
llvm/trunk/test/Transforms/InstCombine/fmul-pow.ll
llvm/trunk/test/Transforms/InstCombine/fmul.ll
Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp?rev=358404&r1=358403&r2=358404&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp Mon Apr 15 06:23:38 2019
@@ -430,6 +430,14 @@ Instruction *InstCombiner::visitFMul(Bin
}
}
+ Value *Z;
+ if (match(&I, m_c_FMul(m_OneUse(m_FDiv(m_Value(X), m_Value(Y))),
+ m_Value(Z)))) {
+ // Sink division: (X / Y) * Z --> (X * Z) / Y
+ Value *NewFMul = Builder.CreateFMulFMF(X, Z, &I);
+ return BinaryOperator::CreateFDivFMF(NewFMul, Y, &I);
+ }
+
// sqrt(X) * sqrt(Y) -> sqrt(X * Y)
// nnan disallows the possibility of returning a number if both operands are
// negative (in that case, we should return NaN).
Modified: llvm/trunk/test/Transforms/InstCombine/fast-math.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/fast-math.ll?rev=358404&r1=358403&r2=358404&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/fast-math.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/fast-math.ll Mon Apr 15 06:23:38 2019
@@ -599,8 +599,8 @@ define <2 x float> @fdiv2_vec(<2 x float
;
define float @fdiv3(float %x) {
; CHECK-LABEL: @fdiv3(
-; CHECK-NEXT: [[DIV:%.*]] = fdiv float [[X:%.*]], 0x47EFFFFFE0000000
-; CHECK-NEXT: [[DIV1:%.*]] = fmul fast float [[DIV]], 0x3FDBD37A80000000
+; CHECK-NEXT: [[TMP1:%.*]] = fmul fast float [[X:%.*]], 0x3FDBD37A80000000
+; CHECK-NEXT: [[DIV1:%.*]] = fdiv fast float [[TMP1]], 0x47EFFFFFE0000000
; CHECK-NEXT: ret float [[DIV1]]
;
%div = fdiv float %x, 0x47EFFFFFE0000000
Modified: llvm/trunk/test/Transforms/InstCombine/fmul-pow.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/fmul-pow.ll?rev=358404&r1=358403&r2=358404&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/fmul-pow.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/fmul-pow.ll Mon Apr 15 06:23:38 2019
@@ -27,9 +27,8 @@ define double @pow_ab_a_reassoc(double %
define double @pow_ab_a_reassoc_commute(double %a, double %b) {
; CHECK-LABEL: @pow_ab_a_reassoc_commute(
-; CHECK-NEXT: [[TMP1:%.*]] = fdiv double 1.000000e+00, [[A:%.*]]
-; CHECK-NEXT: [[TMP2:%.*]] = call double @llvm.pow.f64(double [[A]], double [[B:%.*]])
-; CHECK-NEXT: [[MUL:%.*]] = fmul reassoc double [[TMP1]], [[TMP2]]
+; CHECK-NEXT: [[TMP1:%.*]] = call double @llvm.pow.f64(double [[A:%.*]], double [[B:%.*]])
+; CHECK-NEXT: [[MUL:%.*]] = fdiv reassoc double [[TMP1]], [[A]]
; CHECK-NEXT: ret double [[MUL]]
;
%1 = fdiv double 1.0, %a
Modified: llvm/trunk/test/Transforms/InstCombine/fmul.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/fmul.ll?rev=358404&r1=358403&r2=358404&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/fmul.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/fmul.ll Mon Apr 15 06:23:38 2019
@@ -383,10 +383,10 @@ define float @log2half(float %x, float %
define float @log2half_commute(float %x1, float %y) {
; CHECK-LABEL: @log2half_commute(
-; CHECK-NEXT: [[X:%.*]] = fdiv float [[X1:%.*]], 7.000000e+00
; CHECK-NEXT: [[LOG2:%.*]] = call fast float @llvm.log2.f32(float [[Y:%.*]])
-; CHECK-NEXT: [[TMP1:%.*]] = fmul fast float [[LOG2]], [[X]]
-; CHECK-NEXT: [[MUL:%.*]] = fsub fast float [[TMP1]], [[X]]
+; CHECK-NEXT: [[TMP1:%.*]] = fmul fast float [[LOG2]], [[X1:%.*]]
+; CHECK-NEXT: [[TMP2:%.*]] = fsub fast float [[TMP1]], [[X1]]
+; CHECK-NEXT: [[MUL:%.*]] = fmul fast float [[TMP2]], 0x3FC24924A0000000
; CHECK-NEXT: ret float [[MUL]]
;
%x = fdiv float %x1, 7.0 ; thwart complexity-based canonicalization
@@ -475,13 +475,13 @@ define float @fmul_constant_reassociatio
ret float %r
}
-; Rule "X/C1 * C2 => X * (C2/C1) is not applicable if C2/C1 is abnormal
+; Canonicalization "X/C1 * C2 => X * (C2/C1)" still applies if C2/C1 is denormal
+; (otherwise, we should not have allowed the reassociation in the previous test).
; 0x3810000000000000 == FLT_MIN
define float @fdiv_constant_denominator_fmul_denorm(float %x) {
; CHECK-LABEL: @fdiv_constant_denominator_fmul_denorm(
-; CHECK-NEXT: [[T1:%.*]] = fdiv float [[X:%.*]], 2.000000e+03
-; CHECK-NEXT: [[T3:%.*]] = fmul fast float [[T1]], 0x3810000000000000
+; CHECK-NEXT: [[T3:%.*]] = fmul fast float [[X:%.*]], 0x3760620000000000
; CHECK-NEXT: ret float [[T3]]
;
%t1 = fdiv float %x, 2.0e+3
@@ -707,9 +707,9 @@ define double @fmul_fdiv_factor_squared(
define double @fmul_fdivs_factor_common_denominator(double %x, double %y, double %z) {
; CHECK-LABEL: @fmul_fdivs_factor_common_denominator(
-; CHECK-NEXT: [[DIV1:%.*]] = fdiv double [[X:%.*]], [[Z:%.*]]
-; CHECK-NEXT: [[DIV2:%.*]] = fdiv double [[Y:%.*]], [[Z]]
-; CHECK-NEXT: [[MUL:%.*]] = fmul fast double [[DIV1]], [[DIV2]]
+; CHECK-NEXT: [[TMP1:%.*]] = fmul fast double [[Y:%.*]], [[X:%.*]]
+; CHECK-NEXT: [[TMP2:%.*]] = fmul fast double [[Z:%.*]], [[Z]]
+; CHECK-NEXT: [[MUL:%.*]] = fdiv fast double [[TMP1]], [[TMP2]]
; CHECK-NEXT: ret double [[MUL]]
;
%div1 = fdiv double %x, %z
@@ -720,9 +720,9 @@ define double @fmul_fdivs_factor_common_
define double @fmul_fdivs_factor(double %x, double %y, double %z, double %w) {
; CHECK-LABEL: @fmul_fdivs_factor(
-; CHECK-NEXT: [[DIV1:%.*]] = fdiv double [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT: [[DIV2:%.*]] = fdiv double [[Z:%.*]], [[W:%.*]]
-; CHECK-NEXT: [[MUL:%.*]] = fmul reassoc double [[DIV1]], [[DIV2]]
+; CHECK-NEXT: [[TMP1:%.*]] = fmul reassoc double [[Z:%.*]], [[X:%.*]]
+; CHECK-NEXT: [[TMP2:%.*]] = fdiv reassoc double [[TMP1]], [[W:%.*]]
+; CHECK-NEXT: [[MUL:%.*]] = fdiv reassoc double [[TMP2]], [[Y:%.*]]
; CHECK-NEXT: ret double [[MUL]]
;
%div1 = fdiv double %x, %y
@@ -733,8 +733,8 @@ define double @fmul_fdivs_factor(double
define double @fmul_fdiv_factor(double %x, double %y, double %z) {
; CHECK-LABEL: @fmul_fdiv_factor(
-; CHECK-NEXT: [[DIV:%.*]] = fdiv double [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT: [[MUL:%.*]] = fmul reassoc double [[DIV]], [[Z:%.*]]
+; CHECK-NEXT: [[TMP1:%.*]] = fmul reassoc double [[X:%.*]], [[Z:%.*]]
+; CHECK-NEXT: [[MUL:%.*]] = fdiv reassoc double [[TMP1]], [[Y:%.*]]
; CHECK-NEXT: ret double [[MUL]]
;
%div = fdiv double %x, %y
@@ -744,8 +744,8 @@ define double @fmul_fdiv_factor(double %
define double @fmul_fdiv_factor_constant1(double %x, double %y) {
; CHECK-LABEL: @fmul_fdiv_factor_constant1(
-; CHECK-NEXT: [[DIV:%.*]] = fdiv double [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT: [[MUL:%.*]] = fmul reassoc double [[DIV]], 4.200000e+01
+; CHECK-NEXT: [[TMP1:%.*]] = fmul reassoc double [[X:%.*]], 4.200000e+01
+; CHECK-NEXT: [[MUL:%.*]] = fdiv reassoc double [[TMP1]], [[Y:%.*]]
; CHECK-NEXT: ret double [[MUL]]
;
%div = fdiv double %x, %y
@@ -755,8 +755,8 @@ define double @fmul_fdiv_factor_constant
define <2 x float> @fmul_fdiv_factor_constant2(<2 x float> %x, <2 x float> %y) {
; CHECK-LABEL: @fmul_fdiv_factor_constant2(
-; CHECK-NEXT: [[DIV:%.*]] = fdiv <2 x float> [[X:%.*]], <float 4.200000e+01, float 1.200000e+01>
-; CHECK-NEXT: [[MUL:%.*]] = fmul reassoc <2 x float> [[DIV]], [[Y:%.*]]
+; CHECK-NEXT: [[TMP1:%.*]] = fmul reassoc <2 x float> [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT: [[MUL:%.*]] = fdiv reassoc <2 x float> [[TMP1]], <float 4.200000e+01, float 1.200000e+01>
; CHECK-NEXT: ret <2 x float> [[MUL]]
;
%div = fdiv <2 x float> %x, <float 42.0, float 12.0>
More information about the llvm-commits
mailing list