From d96514bcfe8197a8467218fa5e4b5dbaeb759eff Mon Sep 17 00:00:00 2001 From: Sanjay Patel Date: Mon, 27 Jun 2016 17:25:57 +0000 Subject: [PATCH] [InstCombine] use m_APInt for div --> ashr fold The APInt matcher works with splat vectors, so we get this fold for vectors too. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@273897 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../InstCombine/InstCombineMulDivRem.cpp | 14 ++++++-------- test/Transforms/InstCombine/exact.ll | 9 +++++++++ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp b/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp index 5fb337db165..d7bd80261ab 100644 --- a/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp +++ b/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp @@ -1143,14 +1143,12 @@ Instruction *InstCombiner::visitSDiv(BinaryOperator &I) { if (match(Op1, m_AllOnes())) return BinaryOperator::CreateNeg(Op0); - if (ConstantInt *RHS = dyn_cast(Op1)) { - // sdiv X, C --> ashr exact X, log2(C) - if (I.isExact() && RHS->getValue().isNonNegative() && - RHS->getValue().isPowerOf2()) { - Value *ShAmt = llvm::ConstantInt::get(RHS->getType(), - RHS->getValue().exactLogBase2()); - return BinaryOperator::CreateExactAShr(Op0, ShAmt, I.getName()); - } + // sdiv exact X, C --> ashr exact X, log2(C) + const APInt *Op1C; + if (match(Op1, m_APInt(Op1C)) && I.isExact() && Op1C->isNonNegative() && + Op1C->isPowerOf2()) { + Value *ShAmt = ConstantInt::get(Op1->getType(), Op1C->exactLogBase2()); + return BinaryOperator::CreateExactAShr(Op0, ShAmt, I.getName()); } if (Constant *RHS = dyn_cast(Op1)) { diff --git a/test/Transforms/InstCombine/exact.ll b/test/Transforms/InstCombine/exact.ll index 73b1e044a15..9edcd2491ff 100644 --- a/test/Transforms/InstCombine/exact.ll +++ b/test/Transforms/InstCombine/exact.ll @@ -19,6 +19,15 @@ define i32 @sdiv2(i32 %x) { ret i32 %y } +define <2 x i32> @sdiv2_vec(<2 x i32> %x) { +; CHECK-LABEL: @sdiv2_vec( +; CHECK-NEXT: [[Y:%.*]] = ashr exact <2 x i32> %x, +; CHECK-NEXT: ret <2 x i32> [[Y]] +; + %y = sdiv exact <2 x i32> %x, + ret <2 x i32> %y +} + define i32 @sdiv3(i32 %x) { ; CHECK-LABEL: @sdiv3( ; CHECK-NEXT: [[Y:%.*]] = srem i32 %x, 3