[llvm-commits] [llvm] r78811 - in /llvm/trunk: lib/Transforms/Scalar/InstructionCombining.cpp test/Transforms/InstCombine/exact-sdiv.ll
Dan Gohman
gohman at apple.com
Wed Aug 12 09:33:10 PDT 2009
Author: djg
Date: Wed Aug 12 11:33:09 2009
New Revision: 78811
URL: http://llvm.org/viewvc/llvm-project?rev=78811&view=rev
Log:
Optimize (x/C)*C to x if the division is exact.
Modified:
llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp
llvm/trunk/test/Transforms/InstCombine/exact-sdiv.ll
Modified: llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp?rev=78811&r1=78810&r2=78811&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp Wed Aug 12 11:33:09 2009
@@ -2701,6 +2701,15 @@
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 @@
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()) {
Modified: llvm/trunk/test/Transforms/InstCombine/exact-sdiv.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/exact-sdiv.ll?rev=78811&r1=78810&r2=78811&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/exact-sdiv.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/exact-sdiv.ll Wed Aug 12 11:33:09 2009
@@ -13,3 +13,40 @@
%y = sdiv exact i32 %x, 8
ret i32 %y
}
+
+; CHECK: i32 @a0
+; CHECK: %y = srem i32 %x, 3
+; CHECK: %z = sub i32 %x, %y
+; CHECK: ret i32 %z
+define i32 @a0(i32 %x) {
+ %y = sdiv i32 %x, 3
+ %z = mul i32 %y, 3
+ ret i32 %z
+}
+
+; CHECK: i32 @b0
+; CHECK: ret i32 %x
+define i32 @b0(i32 %x) {
+ %y = sdiv exact i32 %x, 3
+ %z = mul i32 %y, 3
+ ret i32 %z
+}
+
+; CHECK: i32 @a1
+; CHECK: %y = srem i32 %x, 3
+; CHECK: %z = sub i32 %y, %x
+; CHECK: ret i32 %z
+define i32 @a1(i32 %x) {
+ %y = sdiv i32 %x, 3
+ %z = mul i32 %y, -3
+ ret i32 %z
+}
+
+; CHECK: i32 @b1
+; CHECK: %z = sub i32 0, %x
+; CHECK: ret i32 %z
+define i32 @b1(i32 %x) {
+ %y = sdiv exact i32 %x, 3
+ %z = mul i32 %y, -3
+ ret i32 %z
+}
More information about the llvm-commits
mailing list