[llvm] [InstCombine] Evaluate zext nneg as sext where possible (PR #212230)

Valeriy Savchenko via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 5 04:49:48 PDT 2026


================
@@ -1615,7 +1619,18 @@ Instruction *InstCombinerImpl::visitZExt(ZExtInst &Zext) {
         dbgs() << "ICE: EvaluateInDifferentType converting expression type"
                   " to avoid zero extend: "
                << Zext << '\n');
-    Value *Res = EvaluateInDifferentType(Src, DestTy, false);
+
+    // zext nneg means Src is non-negative and we can treat this as an sext, and
+    // if no signed wrapping occurs we know that Src evaluated as a signed
+    // DestTy will also be non-negative. Evaluating as a signed type means that
+    // any constant operands will be sign-extended instead of zero-extended,
+    // meaning they remain in the range of a signed SrcTy so we won't need to
+    // clear any high bits.
+    bool EvaluateAsSigned =
+        Zext.hasNonNeg() &&
+        TypeEvaluationHelper::canEvaluateSExtd(Src, DestTy, true);
----------------
SavchenkoValeriy wrote:

I'm not feeling particularly strong about it, but I believe that at the high level we will never call `canEvaluateSExtd` with a runtime-defined value for `NoSignedWrap`, so all callees will be essentially with `true` or `false`. This makes me believe that we should have `canEvaluateSExtdNoWrap` function to make this intent obvious.

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


More information about the llvm-commits mailing list