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

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


================
@@ -905,8 +905,17 @@ 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));
+    // Preserve the nsw flag iff the sum of Op01C and Op1C will not overflow
+    // so that there is a chance to make some other transformations.
+    // For some cases, sdiv can be converted to udiv when the newly created add
+    // carrying the nsw flag is one of its operands.
----------------
nikic wrote:

Remove this comment. You don't need to justify preserving nowrap flags.

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


More information about the llvm-commits mailing list