[llvm] [InstCombine] Preserve the nsw/nuw flags for (X | Op01C) + Op1C --> X + (Op01C + Op1C) (PR #94586)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 7 03:00:54 PDT 2024


================
@@ -905,8 +905,14 @@ Instruction *InstCombinerImpl::foldAddWithConstant(BinaryOperator &Add) {
 
   // (X | Op01C) + Op1C --> X + (Op01C + Op1C) iff the `or` is actually an `add`
   Constant *Op01C;
-  if (match(Op0, m_DisjointOr(m_Value(X), m_ImmConstant(Op01C))))
-    return BinaryOperator::CreateAdd(X, ConstantExpr::getAdd(Op01C, Op1C));
+  if (match(Op0, m_DisjointOr(m_Value(X), m_ImmConstant(Op01C)))) {
+    BinaryOperator *NewAdd =
+        BinaryOperator::CreateAdd(X, ConstantExpr::getAdd(Op01C, Op1C));
+    if (willNotOverflowSignedAdd(Op01C, Op1C, Add))
+      NewAdd->setHasNoSignedWrap(Add.hasNoSignedWrap());
----------------
nikic wrote:

```suggestion
    NewAdd->setHasNoSignedWrap(Add.hasNoSignedWrap() &&
                               willNotOverflowSignedAdd(Op01C, Op1C, Add));
```
Save the overflow check if not needed.

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


More information about the llvm-commits mailing list