[llvm] [InstCombine] Optimize sub(sext(add(x, y)), sext(add(x, z))). (PR #144174)

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 13 18:01:26 PDT 2025


================
@@ -2807,6 +2807,62 @@ Instruction *InstCombinerImpl::visitSub(BinaryOperator &I) {
   if (Instruction *Res = foldBinOpOfSelectAndCastOfSelectCondition(I))
     return Res;
 
+  // (sub[ nsw][ nuw] (sext (add nsw (X, Y)), sext (X))) --> (sext (Y))
+  {
+    Value *Add0;
+    if (match(Op0, m_SExt(m_Value(Add0))) &&
+        match(Add0, m_Add(m_Value(X), m_Value(Y))) &&
+        match(Op1, m_SExt(m_Specific(X)))) {
+      auto *OBO0 = cast<OverflowingBinaryOperator>(Add0);
+      if (OBO0->hasNoSignedWrap()) {
----------------
topperc wrote:

Can you use `m_NSWAdd` in the match call?

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


More information about the llvm-commits mailing list