[llvm] [InstCombine] Fold adds + shifts with nsw and nuw flags (PR #88193)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Wed May 1 07:08:46 PDT 2024


================
@@ -1259,6 +1259,18 @@ Instruction *InstCombinerImpl::visitLShr(BinaryOperator &I) {
       match(Op1, m_SpecificIntAllowPoison(BitWidth - 1)))
     return new ZExtInst(Builder.CreateIsNotNeg(X, "isnotneg"), Ty);
 
+  // If both the add and the shift are nuw, then:
+  // ((X << Z) + Y) nuw >>u Z --> X + (Y >>u Z) nuw
+  Value *Y;
+  if (match(Op0, m_OneUse(m_c_NUWAdd(m_NUWShl(m_Value(X), m_Specific(Op1)),
+                                     m_Value(Y))))) {
+    Value *NewLshr = Builder.CreateLShr(Y, Op1, "", I.isExact());
+    auto *newAdd = BinaryOperator::CreateNUWAdd(NewLshr, X);
----------------
dtcxzyw wrote:

```suggestion
    auto *NewAdd = BinaryOperator::CreateNUWAdd(NewLshr, X);
```

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


More information about the llvm-commits mailing list