[llvm] [InstCombine] Evaluate zext nneg as sext where possible (PR #212230)
Valeriy Savchenko via llvm-commits
llvm-commits at lists.llvm.org
Fri Aug 7 06:55:16 PDT 2026
================
@@ -1602,11 +1606,21 @@ Instruction *InstCombinerImpl::visitZExt(ZExtInst &Zext) {
if (SrcTy->isIntOrIntVectorTy(1) && Zext.hasNonNeg())
return replaceInstUsesWith(Zext, Constant::getNullValue(Zext.getType()));
+ // 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, /*NoSignedWrap=*/true);
----------------
SavchenkoValeriy wrote:
If it's not `nneg`, we don't want to even check if it can be evaluated `SExtd`. That will change semantics and incorrectly broaden the cases when the new logic applies.
https://github.com/llvm/llvm-project/pull/212230
More information about the llvm-commits
mailing list