aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Transforms/Scalar
diff options
context:
space:
mode:
authorDan Gohman <gohman@apple.com>2009-08-12 16:33:09 +0000
committerDan Gohman <gohman@apple.com>2009-08-12 16:33:09 +0000
commitfa94b948d9a0aaaad98901815e56b676803ad259 (patch)
treefe5f5ff724d6bd1e9a39ed0c49b4d4148856e70d /lib/Transforms/Scalar
parent2df6e6467f679984e98612e695682fcc834bd95f (diff)
downloadexternal_llvm-fa94b948d9a0aaaad98901815e56b676803ad259.zip
external_llvm-fa94b948d9a0aaaad98901815e56b676803ad259.tar.gz
external_llvm-fa94b948d9a0aaaad98901815e56b676803ad259.tar.bz2
Optimize (x/C)*C to x if the division is exact.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@78811 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Transforms/Scalar')
-rw-r--r--lib/Transforms/Scalar/InstructionCombining.cpp11
1 files changed, 10 insertions, 1 deletions
diff --git a/lib/Transforms/Scalar/InstructionCombining.cpp b/lib/Transforms/Scalar/InstructionCombining.cpp
index 514e91b..669ef2d 100644
--- a/lib/Transforms/Scalar/InstructionCombining.cpp
+++ b/lib/Transforms/Scalar/InstructionCombining.cpp
@@ -2701,6 +2701,15 @@ Instruction *InstCombiner::visitMul(BinaryOperator &I) {
BO->getOpcode() == Instruction::SDiv)) {
Value *Op0BO = BO->getOperand(0), *Op1BO = BO->getOperand(1);
+ // If the division is exact, X % Y is zero.
+ if (SDivOperator *SDiv = dyn_cast<SDivOperator>(BO))
+ if (SDiv->isExact()) {
+ if (Op1BO == Op1)
+ return ReplaceInstUsesWith(I, Op0BO);
+ else
+ return BinaryOperator::CreateNeg(Op0BO);
+ }
+
Instruction *Rem;
if (BO->getOpcode() == Instruction::UDiv)
Rem = BinaryOperator::CreateURem(Op0BO, Op1BO);
@@ -3060,7 +3069,7 @@ Instruction *InstCombiner::visitSDiv(BinaryOperator &I) {
if (RHS->isAllOnesValue())
return BinaryOperator::CreateNeg(Op0);
- // sdiv X, C --> ashr X, log2(C)
+ // sdiv X, C --> ashr X, log2(C)
if (cast<SDivOperator>(&I)->isExact() &&
RHS->getValue().isNonNegative() &&
RHS->getValue().isPowerOf2()) {