diff options
Diffstat (limited to 'llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp')
-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)) |