[llvm] [InstCombine] Fold ((X << nuw Z) binop nuw Y) >>u Z --> X binop nuw (Y >>u Z) (PR #88193)
via llvm-commits
llvm-commits at lists.llvm.org
Tue May 7 08:48:40 PDT 2024
================
@@ -1259,6 +1259,53 @@ Instruction *InstCombinerImpl::visitLShr(BinaryOperator &I) {
match(Op1, m_SpecificIntAllowPoison(BitWidth - 1)))
return new ZExtInst(Builder.CreateIsNotNeg(X, "isnotneg"), Ty);
+ // ((X << nuw Z) sub nuw Y) >>u exact Z --> X sub nuw (Y >>u exact Z),
+ // ONLY if I is exact, and both the shift and sub are nuw
+ Value *Y;
+ if (I.isExact() &&
+ match(Op0, m_OneUse(m_NUWSub(m_NUWShl(m_Value(X), m_Specific(Op1)),
+ m_Value(Y))))) {
+ Value *NewLshr = Builder.CreateLShr(Y, Op1, "", /* isExact */ true);
+ auto *NewSub = BinaryOperator::CreateNUWSub(X, NewLshr);
+ NewSub->setHasNoSignedWrap(
+ cast<OverflowingBinaryOperator>(Op0)->hasNoSignedWrap());
+ return NewSub;
+ }
+
+ auto isSuitableBinOpcode = [](Instruction::BinaryOps BinOpcode) {
+ switch (BinOpcode) {
+ default:
+ return false;
+ case Instruction::Add:
+ case Instruction::Or:
+ case Instruction::Xor:
+ // And does not work here, and sub is handled separately.
+ return true;
+ }
+ };
+
+ // If both the binop and the shift are nuw, then:
+ // ((X << nuw Z) binop nuw Y) >>u Z --> X binop nuw (Y >>u Z)
+ if (match(Op0, m_OneUse(m_c_BinOp(m_NUWShl(m_Value(X), m_Specific(Op1)),
+ m_Value(Y))))) {
+ BinaryOperator *Op0OB = cast<BinaryOperator>(Op0);
+ if (isSuitableBinOpcode(Op0OB->getOpcode())) {
+ if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(Op0);
+ !OBO || OBO->hasNoUnsignedWrap()) {
+ Value *NewLshr = Builder.CreateLShr(Y, Op1, "", I.isExact());
+ auto *NewBinOp = BinaryOperator::Create(Op0OB->getOpcode(), NewLshr, X);
+ if (OBO) {
+ NewBinOp->setHasNoUnsignedWrap(true);
+ NewBinOp->setHasNoSignedWrap(OBO->hasNoSignedWrap());
+ } else if (auto *Disjoint = dyn_cast<PossiblyDisjointInst>(Op0);
+ Disjoint && Disjoint->isDisjoint()) {
+ cast<PossiblyDisjointInst>(NewBinOp)->setIsDisjoint(true);
+ }
+ return NewBinOp;
+ }
+ }
+ }
----------------
goldsteinn wrote:
Which proofs are for this case? Seems like all your proofs have the `exact` flag.
Also missing a proof for `disjoint` propagation.
https://github.com/llvm/llvm-project/pull/88193
More information about the llvm-commits
mailing list