Skip to content

Commit 0e81698

Browse files
committed
ASHR
1 parent 56e4718 commit 0e81698

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1707,6 +1707,22 @@ Instruction *InstCombinerImpl::visitAShr(BinaryOperator &I) {
17071707
if (match(Op0, m_OneUse(m_NSWSub(m_Value(X), m_Value(Y)))))
17081708
return new SExtInst(Builder.CreateICmpSLT(X, Y), Ty);
17091709
}
1710+
1711+
const APInt *MulC;
1712+
if (match(Op0, m_OneUse(m_NSWMul(m_Value(X), m_APInt(MulC)))) &&
1713+
(BitWidth > 2 && (*MulC - 1).isPowerOf2() &&
1714+
MulC->logBase2() == ShAmt &&
1715+
(ShAmt < BitWidth - 1 ||
1716+
I.isExact()))) /* Minus 1 for the sign bit */ {
1717+
1718+
// ashr (mul nsw (X, 2^N + 1)), N -> add nsw (X, ashr(X, N))
1719+
auto *NewAdd = BinaryOperator::CreateNSWAdd(
1720+
X,
1721+
Builder.CreateAShr(X, ConstantInt::get(Ty, ShAmt), "", I.isExact()));
1722+
NewAdd->setHasNoUnsignedWrap(
1723+
cast<OverflowingBinaryOperator>(Op0)->hasNoUnsignedWrap());
1724+
return NewAdd;
1725+
}
17101726
}
17111727

17121728
const SimplifyQuery Q = SQ.getWithInstruction(&I);

0 commit comments

Comments
 (0)