[llvm] Match the inverse of m_AddOverflow (PR #147215)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 7 00:12:28 PDT 2025


================
@@ -7832,6 +7832,25 @@ Instruction *InstCombinerImpl::visitICmpInst(ICmpInst &I) {
       }
     }
 
+    if (match(&I, m_UAddWithOverflowInv(m_Value(X), m_Value(Y),
+                                        m_Instruction(AddI))) &&
+        isa<IntegerType>(X->getType())) {
+      Value *Result;
+      Constant *Overflow;
+      // m_UAddWithOverflowInv can match patterns that do not include an
+      // explicit "add" instruction, so check the opcode of the matched op.
+      if (AddI->getOpcode() == Instruction::Add &&
+          OptimizeOverflowCheck(Instruction::Add, /*Signed*/ false, X, Y, *AddI,
+                                Result, Overflow)) {
+        Overflow = Overflow->isNullValue()
+                       ? ConstantInt::getTrue(Overflow->getType())
+                       : ConstantInt::getFalse(Overflow->getType());
+        replaceInstUsesWith(*AddI, Result);
+        eraseInstFromFunction(*AddI);
+        return replaceInstUsesWith(I, Overflow);
+      }
+    }
----------------
nikic wrote:

This PR needs to be split into smaller parts. You can introduce the matcher and use it in *one* place with adequate test coverage. For example, you are adding code to InstCombine here, but there is not a single changed InstCombine test.

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


More information about the llvm-commits mailing list