[llvm] [InstCombine] Fold ((X << nuw Z) binop nuw Y) >>u Z --> X binop nuw (Y >>u Z) (PR #88193)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Tue May 7 19:40:52 PDT 2024


================
@@ -1259,6 +1259,54 @@ 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),
+  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::And:
----------------
dtcxzyw wrote:

Oops, this line doesn't match my reviewed version. https://github.com/llvm/llvm-project/commit/5d61e1f6285846d4599cd77dc6a873aa13901aaf

cc @nikic @arsenm @tstellar Is there a way to revoke the approval after new changes are made to this PR?

@AtariDreams It is dangerous to force-push non-NFC changes after receiving an approval.


https://github.com/llvm/llvm-project/pull/88193


More information about the llvm-commits mailing list