diff options
author | Sanjay Patel <spatel@rotateright.com> | 2018-02-20 23:51:16 +0000 |
---|---|---|
committer | Sanjay Patel <spatel@rotateright.com> | 2018-02-20 23:51:16 +0000 |
commit | d8dd0151fc12fbbb3f15f71174452c81001a902c (patch) | |
tree | 2d3c6877ff9f208307efa09b672016b7111747b9 /llvm/lib/Transforms | |
parent | cd8758233ebacf36b373481a1dc1e025ee487a39 (diff) | |
download | bcm5719-llvm-d8dd0151fc12fbbb3f15f71174452c81001a902c.tar.gz bcm5719-llvm-d8dd0151fc12fbbb3f15f71174452c81001a902c.zip |
[InstCombine] -X / C --> X / -C for FP
We already do this in DAGCombiner, but it should
also be good to eliminate the fsub use in IR.
llvm-svn: 325648
Diffstat (limited to 'llvm/lib/Transforms')
-rw-r--r-- | llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp | 17 |
1 files changed, 12 insertions, 5 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp index cb42d24cd34..ec62beb4fe9 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp @@ -1285,12 +1285,18 @@ Instruction *InstCombiner::visitSDiv(BinaryOperator &I) { return nullptr; } -/// Try to convert X/C into X * (1/C). +/// Remove negation and try to convert division into multiplication. static Instruction *foldFDivConstantDivisor(BinaryOperator &I) { Constant *C; if (!match(I.getOperand(1), m_Constant(C))) return nullptr; + // -X / C --> X / -C + Value *X; + if (match(I.getOperand(0), m_FNeg(m_Value(X)))) + return BinaryOperator::CreateWithCopiedFlags(Instruction::FDiv, X, + ConstantExpr::getFNeg(C), &I); + // If the constant divisor has an exact inverse, this is always safe. If not, // then we can still create a reciprocal if fast-math-flags allow it and the // constant is a regular number (not zero, infinite, or denormal). @@ -1305,6 +1311,7 @@ static Instruction *foldFDivConstantDivisor(BinaryOperator &I) { if (!RecipC->isNormalFP()) return nullptr; + // X / C --> X * (1 / C) return BinaryOperator::CreateWithCopiedFlags( Instruction::FMul, I.getOperand(0), RecipC, &I); } @@ -1345,11 +1352,11 @@ Instruction *InstCombiner::visitFDiv(BinaryOperator &I) { SQ.getWithInstruction(&I))) return replaceInstUsesWith(I, V); - if (Instruction *FMul = foldFDivConstantDivisor(I)) - return FMul; + if (Instruction *R = foldFDivConstantDivisor(I)) + return R; - if (Instruction *NewFDiv = foldFDivConstantDividend(I)) - return NewFDiv; + if (Instruction *R = foldFDivConstantDividend(I)) + return R; if (isa<Constant>(Op0)) if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |