summaryrefslogtreecommitdiff
path: root/lib/Transforms/InstCombine
diff options
context:
space:
mode:
authorNadav Rotem <nrotem@apple.com>2012-08-28 10:01:43 +0000
committerNadav Rotem <nrotem@apple.com>2012-08-28 10:01:43 +0000
commit9753f0b9b4ab33919c5010acb6a7b2dc1e875aff (patch)
tree1b51cc99811cc851fded60ed5ec0f232f06b2929 /lib/Transforms/InstCombine
parenteeba6e83175d7835f3648c30a44c2ca12116d40b (diff)
Teach InstCombine to canonicalize [SU]div+[AL]shl patterns.
For example: %1 = lshr i32 %x, 2 %2 = udiv i32 %1, 100 rdar://12182093 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@162743 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Transforms/InstCombine')
-rw-r--r--lib/Transforms/InstCombine/InstCombineMulDivRem.cpp20
1 files changed, 20 insertions, 0 deletions
diff --git a/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp b/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
index 35a0bbb7614..e104a0a9798 100644
--- a/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
+++ b/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
@@ -462,6 +462,16 @@ Instruction *InstCombiner::visitUDiv(BinaryOperator &I) {
}
}
+ // Udiv ((Lshl x, c1) , c2) -> x / (C1 * 1<<C2);
+ if (Constant *C = dyn_cast<Constant>(Op1)) {
+ Value *X = 0, *C1 = 0;
+ if (match(Op0, m_LShr(m_Value(X), m_Value(C1)))) {
+ uint64_t NC = cast<ConstantInt>(C)->getZExtValue() *
+ (1<< cast<ConstantInt>(C1)->getZExtValue());
+ return BinaryOperator::CreateUDiv(X, ConstantInt::get(I.getType(), NC));
+ }
+ }
+
// X udiv (C1 << N), where C1 is "1<<C2" --> X >> (N+C2)
{ const APInt *CI; Value *N;
if (match(Op1, m_Shl(m_Power2(CI), m_Value(N))) ||
@@ -533,6 +543,16 @@ Instruction *InstCombiner::visitSDiv(BinaryOperator &I) {
ConstantExpr::getNeg(RHS));
}
+ // Sdiv ((Ashl x, c1) , c2) -> x / (C1 * 1<<C2);
+ if (Constant *C = dyn_cast<Constant>(Op1)) {
+ Value *X = 0, *C1 = 0;
+ if (match(Op0, m_AShr(m_Value(X), m_Value(C1)))) {
+ uint64_t NC = cast<ConstantInt>(C)->getZExtValue() *
+ (1<< cast<ConstantInt>(C1)->getZExtValue());
+ return BinaryOperator::CreateSDiv(X, ConstantInt::get(I.getType(), NC));
+ }
+ }
+
// If the sign bits of both operands are zero (i.e. we can prove they are
// unsigned inputs), turn this into a udiv.
if (I.getType()->isIntegerTy()) {