[llvm-commits] [llvm] r76304 - in /llvm/trunk: lib/Transforms/Scalar/InstructionCombining.cpp test/Transforms/InstCombine/sdiv-shift.ll
Eli Friedman
eli.friedman at gmail.com
Sat Jul 18 02:53:41 PDT 2009
Author: efriedma
Date: Sat Jul 18 04:53:21 2009
New Revision: 76304
URL: http://llvm.org/viewvc/llvm-project?rev=76304&view=rev
Log:
Add combine: X sdiv (1 << Y) -> X udiv (1 << Y) when X doesn't have the
sign bit set.
Added:
llvm/trunk/test/Transforms/InstCombine/sdiv-shift.ll
Modified:
llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp
Modified: llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp?rev=76304&r1=76303&r2=76304&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp Sat Jul 18 04:53:21 2009
@@ -3104,11 +3104,22 @@
// unsigned inputs), turn this into a udiv.
if (I.getType()->isInteger()) {
APInt Mask(APInt::getSignBit(I.getType()->getPrimitiveSizeInBits()));
- if (MaskedValueIsZero(Op1, Mask) && MaskedValueIsZero(Op0, Mask)) {
- // X sdiv Y -> X udiv Y, iff X and Y don't have sign bit set
- return BinaryOperator::CreateUDiv(Op0, Op1, I.getName());
+ if (MaskedValueIsZero(Op0, Mask)) {
+ if (MaskedValueIsZero(Op1, Mask)) {
+ // X sdiv Y -> X udiv Y, iff X and Y don't have sign bit set
+ return BinaryOperator::CreateUDiv(Op0, Op1, I.getName());
+ }
+ ConstantInt *ShiftedInt;
+ if (match(Op1, m_Shl(m_ConstantInt(ShiftedInt), m_Value()), *Context) &&
+ ShiftedInt->getValue().isPowerOf2()) {
+ // X sdiv (1 << Y) -> X udiv (1 << Y) ( -> X u>> Y)
+ // Safe because the only negative value (1 << Y) can take on is
+ // INT_MIN, and X sdiv INT_MIN == X udiv INT_MIN == 0 if X doesn't have
+ // the sign bit set.
+ return BinaryOperator::CreateUDiv(Op0, Op1, I.getName());
+ }
}
- }
+ }
return 0;
}
Added: llvm/trunk/test/Transforms/InstCombine/sdiv-shift.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/sdiv-shift.ll?rev=76304&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/sdiv-shift.ll (added)
+++ llvm/trunk/test/Transforms/InstCombine/sdiv-shift.ll Sat Jul 18 04:53:21 2009
@@ -0,0 +1,9 @@
+; RUN: llvm-as < %s | opt -instcombine | llvm-dis | not grep div
+
+define i32 @a(i16 zeroext %x, i32 %y) nounwind {
+entry:
+ %conv = zext i16 %x to i32
+ %s = shl i32 2, %y
+ %d = sdiv i32 %conv, %s
+ ret i32 %d
+}
More information about the llvm-commits
mailing list