[llvm] [InstCombine] Optimize `zext(trunc nuw x)` (PR #225206)

Andreas Jonson via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 21 23:20:26 PDT 2026


================
@@ -239,6 +239,23 @@ Instruction *InstCombinerImpl::commonCastTransforms(CastInst &CI) {
         replaceAllDbgUsesWith(*CSrc, *Res, CI, DT);
       return Res;
     }
+
+    // A no-wrap trunc followed by the corresponding extension preserves the
+    // value, so cast directly to the final type.
+    bool IsZExt = isa<ZExtInst>(CI);
+    bool IsSExt = isa<SExtInst>(CI);
+    if (auto *Trunc = dyn_cast<TruncInst>(CSrc);
+        Trunc && ((IsZExt && Trunc->hasNoUnsignedWrap()) ||
+                  (IsSExt && Trunc->hasNoSignedWrap()))) {
+      auto *Res = CastInst::CreateIntegerCast(Trunc->getOperand(0), Ty, IsSExt);
----------------
andjo403 wrote:

why not call EvaluateInDifferentType where setting the flags already is handled?

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


More information about the llvm-commits mailing list