diff options
author | Sanjay Patel <spatel@rotateright.com> | 2016-06-27 22:27:11 +0000 |
---|---|---|
committer | Sanjay Patel <spatel@rotateright.com> | 2016-06-27 22:27:11 +0000 |
commit | 59ed2ffca38f01b326459d366d1b131ec45eb483 (patch) | |
tree | 480c5bebdf739530d2b2e42e14e0aa19592092b7 /llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp | |
parent | a657f875a91b652adb3a373265db2745c1e7680d (diff) | |
download | llvm-59ed2ffca38f01b326459d366d1b131ec45eb483.zip llvm-59ed2ffca38f01b326459d366d1b131ec45eb483.tar.gz llvm-59ed2ffca38f01b326459d366d1b131ec45eb483.tar.bz2 |
[InstCombine] shrink type of sdiv if dividend is sexted and constant divisor is small enough (PR28153)
This should fix PR28153:
https://llvm.org/bugs/show_bug.cgi?id=28153
Differential Revision: http://reviews.llvm.org/D21769
llvm-svn: 273951
Diffstat (limited to 'llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp')
-rw-r--r-- | llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp index 4e5bf78..788097f 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp @@ -1150,6 +1150,23 @@ Instruction *InstCombiner::visitSDiv(BinaryOperator &I) { Value *ShAmt = ConstantInt::get(Op1->getType(), Op1C->exactLogBase2()); return BinaryOperator::CreateExactAShr(Op0, ShAmt, I.getName()); } + + // If the dividend is sign-extended and the constant divisor is small enough + // to fit in the source type, shrink the division to the narrower type: + // (sext X) sdiv C --> sext (X sdiv C) + Value *Op0Src; + if (match(Op0, m_OneUse(m_SExt(m_Value(Op0Src)))) && + Op0Src->getType()->getScalarSizeInBits() >= Op1C->getMinSignedBits()) { + + // In the general case, we need to make sure that the dividend is not the + // minimum signed value because dividing that by -1 is UB. But here, we + // know that the -1 divisor case is already handled above. + + Constant *NarrowDivisor = + ConstantExpr::getTrunc(cast<Constant>(Op1), Op0Src->getType()); + Value *NarrowOp = Builder->CreateSDiv(Op0Src, NarrowDivisor); + return new SExtInst(NarrowOp, Op0->getType()); + } } if (Constant *RHS = dyn_cast<Constant>(Op1)) { |