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

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Sun Jun 15 20:10:43 PDT 2025


================
@@ -2807,6 +2807,44 @@ Instruction *InstCombinerImpl::visitSub(BinaryOperator &I) {
   if (Instruction *Res = foldBinOpOfSelectAndCastOfSelectCondition(I))
     return Res;
 
+  // (sub (sext (add nsw (X, Y)), sext (X))) --> (sext (Y))
+  if (match(Op1, m_SExtLike(m_Value(X))) &&
+      match(Op0, m_SExtLike(m_c_NSWAdd(m_Specific(X), m_Value(Y))))) {
+    Value *SExtY = Builder.CreateSExt(Y, I.getType());
+    return replaceInstUsesWith(I, SExtY);
+  }
+
+  // (sub[ nsw] (sext (add nsw (X, Y)), sext (add nsw (X, Z)))) -->
+  // --> (sub[ nsw] (sext (Y), sext(Z)))
----------------
dtcxzyw wrote:

```suggestion
  // --> (sub[ nsw] (sext (Y), sext (Z)))
```

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


More information about the llvm-commits mailing list