[llvm] [SCEV] Fold zext(C+A)<nsw> -> (sext(C) + zext(A))<nsw> if possible. (PR #142599)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 3 06:58:51 PDT 2025


================
@@ -1793,6 +1793,18 @@ const SCEV *ScalarEvolution::getZeroExtendExprImpl(const SCEV *Op, Type *Ty,
       return getAddExpr(Ops, SCEV::FlagNUW, Depth + 1);
     }
 
+    const SCEVConstant *C;
+    const SCEV *A;
+    // zext (C + A)<nsw> -> (sext(C) + zext(A))<nsw> if zext (C + A)<nsw> >=s 0
+    // and A >=s V.
----------------
nikic wrote:

Though, wouldn't the more natural fold here be something like https://alive2.llvm.org/ce/z/RF9XaY? For the case where A >= 0 the sext would become a zext.

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


More information about the llvm-commits mailing list