[llvm] r367146 - [InstCombine] canonicalize negated operand of fdiv
Sanjay Patel via llvm-commits
llvm-commits at lists.llvm.org
Fri Jul 26 12:56:59 PDT 2019
Author: spatel
Date: Fri Jul 26 12:56:59 2019
New Revision: 367146
URL: http://llvm.org/viewvc/llvm-project?rev=367146&view=rev
Log:
[InstCombine] canonicalize negated operand of fdiv
This is a transform that we use with fmul, so use
it for fdiv too for consistency.
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=367146&r1=367145&r2=367146&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp Fri Jul 26 12:56:59 2019
@@ -1234,6 +1234,16 @@ Instruction *InstCombiner::visitFDiv(Bin
return &I;
}
+ // Sink negation: -X / Y --> -(X / Y)
+ // But don't transform constant expressions because there's an inverse fold.
+ if (match(Op0, m_OneUse(m_FNeg(m_Value(X)))) && !isa<ConstantExpr>(Op0))
+ return BinaryOperator::CreateFNegFMF(Builder.CreateFDivFMF(X, Op1, &I), &I);
+
+ // Sink negation: Y / -X --> -(Y / X)
+ // But don't transform constant expressions because there's an inverse fold.
+ if (match(Op1, m_OneUse(m_FNeg(m_Value(X)))) && !isa<ConstantExpr>(Op1))
+ return BinaryOperator::CreateFNegFMF(Builder.CreateFDivFMF(Op0, X, &I), &I);
+
// X / (X * Y) --> 1.0 / Y
// Reassociate to (X / X -> 1.0) is legal when NaNs are not allowed.
// We can ignore the possibility that X is infinity because INF/INF is NaN.
Modified: llvm/trunk/test/Transforms/InstCombine/fdiv.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/fdiv.ll?rev=367146&r1=367145&r2=367146&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/fdiv.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/fdiv.ll Fri Jul 26 12:56:59 2019
@@ -501,8 +501,8 @@ define <2 x float> @div_constant_dividen
define double @fdiv_fneg1(double %x, double %y) {
; CHECK-LABEL: @fdiv_fneg1(
-; CHECK-NEXT: [[NEG:%.*]] = fsub double -0.000000e+00, [[X:%.*]]
-; CHECK-NEXT: [[DIV:%.*]] = fdiv double [[NEG]], [[Y:%.*]]
+; CHECK-NEXT: [[TMP1:%.*]] = fdiv double [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT: [[DIV:%.*]] = fsub double -0.000000e+00, [[TMP1]]
; CHECK-NEXT: ret double [[DIV]]
;
%neg = fsub double -0.0, %x
@@ -512,8 +512,8 @@ define double @fdiv_fneg1(double %x, dou
define <2 x float> @fdiv_fneg2(<2 x float> %x, <2 x float> %y) {
; CHECK-LABEL: @fdiv_fneg2(
-; CHECK-NEXT: [[NEG:%.*]] = fsub <2 x float> <float -0.000000e+00, float -0.000000e+00>, [[X:%.*]]
-; CHECK-NEXT: [[DIV:%.*]] = fdiv <2 x float> [[Y:%.*]], [[NEG]]
+; CHECK-NEXT: [[TMP1:%.*]] = fdiv <2 x float> [[Y:%.*]], [[X:%.*]]
+; CHECK-NEXT: [[DIV:%.*]] = fsub <2 x float> <float -0.000000e+00, float -0.000000e+00>, [[TMP1]]
; CHECK-NEXT: ret <2 x float> [[DIV]]
;
%neg = fsub <2 x float> <float -0.0, float -0.0>, %x
@@ -533,4 +533,3 @@ define float @fdiv_fneg1_extra_use(float
%div = fdiv float %neg, %y
ret float %div
}
-
More information about the llvm-commits
mailing list