summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp25
-rw-r--r--llvm/test/Transforms/InstCombine/fdiv.ll6
2 files changed, 19 insertions, 12 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
index ec62beb4fe9..7c674bb83b2 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
@@ -1316,21 +1316,30 @@ static Instruction *foldFDivConstantDivisor(BinaryOperator &I) {
Instruction::FMul, I.getOperand(0), RecipC, &I);
}
-/// Try to reassociate C / X expressions where X includes another constant.
+/// Remove negation and try to reassociate constant math.
static Instruction *foldFDivConstantDividend(BinaryOperator &I) {
- Constant *C1;
- if (!I.hasAllowReassoc() || !I.hasAllowReciprocal() ||
- !match(I.getOperand(0), m_Constant(C1)))
+ Constant *C;
+ if (!match(I.getOperand(0), m_Constant(C)))
return nullptr;
+ // C / -X --> -C / X
Value *X;
+ if (match(I.getOperand(1), m_FNeg(m_Value(X)))) {
+ return BinaryOperator::CreateWithCopiedFlags(
+ Instruction::FDiv, ConstantExpr::getFNeg(C), X, &I);
+ }
+
+ if (!I.hasAllowReassoc() || !I.hasAllowReciprocal())
+ return nullptr;
+
+ // Try to reassociate C / X expressions where X includes another constant.
Constant *C2, *NewC = nullptr;
if (match(I.getOperand(1), m_FMul(m_Value(X), m_Constant(C2)))) {
- // C1 / (X * C2) --> (C1 / C2) / X
- NewC = ConstantExpr::getFDiv(C1, C2);
+ // C / (X * C2) --> (C / C2) / X
+ NewC = ConstantExpr::getFDiv(C, C2);
} else if (match(I.getOperand(1), m_FDiv(m_Value(X), m_Constant(C2)))) {
- // C1 / (X / C2) --> (C1 * C2) / X
- NewC = ConstantExpr::getFMul(C1, C2);
+ // C / (X / C2) --> (C * C2) / X
+ NewC = ConstantExpr::getFMul(C, C2);
}
// Disallow denormal constants because we don't know what would happen
// on all targets.
diff --git a/llvm/test/Transforms/InstCombine/fdiv.ll b/llvm/test/Transforms/InstCombine/fdiv.ll
index a70907eb85a..8009a89f10d 100644
--- a/llvm/test/Transforms/InstCombine/fdiv.ll
+++ b/llvm/test/Transforms/InstCombine/fdiv.ll
@@ -225,8 +225,7 @@ define float @fneg_dividend_constant_divisor(float %x) {
define float @fneg_divisor_constant_dividend(float %x) {
; CHECK-LABEL: @fneg_divisor_constant_dividend(
-; CHECK-NEXT: [[NEG:%.*]] = fsub float -0.000000e+00, [[X:%.*]]
-; CHECK-NEXT: [[DIV:%.*]] = fdiv nnan float -3.000000e+00, [[NEG]]
+; CHECK-NEXT: [[DIV:%.*]] = fdiv nnan float 3.000000e+00, [[X:%.*]]
; CHECK-NEXT: ret float [[DIV]]
;
%neg = fsub float -0.0, %x
@@ -246,8 +245,7 @@ define <2 x float> @fneg_dividend_constant_divisor_vec(<2 x float> %x) {
define <2 x float> @fneg_divisor_constant_dividend_vec(<2 x float> %x) {
; CHECK-LABEL: @fneg_divisor_constant_dividend_vec(
-; CHECK-NEXT: [[NEG:%.*]] = fsub <2 x float> <float -0.000000e+00, float -0.000000e+00>, [[X:%.*]]
-; CHECK-NEXT: [[DIV:%.*]] = fdiv afn <2 x float> <float -3.000000e+00, float 5.000000e+00>, [[NEG]]
+; CHECK-NEXT: [[DIV:%.*]] = fdiv afn <2 x float> <float 3.000000e+00, float -5.000000e+00>, [[X:%.*]]
; CHECK-NEXT: ret <2 x float> [[DIV]]
;
%neg = fsub <2 x float> <float -0.0, float -0.0>, %x
OpenPOWER on IntegriCloud