[llvm] [InstCombine] Fold `sext(trunc nsw)` and `zext(trunc nuw)` (PR #88609)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Sat Apr 13 06:37:41 PDT 2024


================
@@ -1188,9 +1188,20 @@ Instruction *InstCombinerImpl::visitZExt(ZExtInst &Zext) {
   if (auto *CSrc = dyn_cast<TruncInst>(Src)) {   // A->B->C cast
     // TODO: Subsume this into EvaluateInDifferentType.
 
+    Value *A = CSrc->getOperand(0);
+    // If TRUNC has nuw flag, then convert directly to final type.
+    if (CSrc->hasNoUnsignedWrap()) {
+      CastInst *I =
+          CastInst::CreateIntegerCast(A, DestTy, /* isSigned */ false);
----------------
dtcxzyw wrote:

```suggestion
          CastInst::CreateIntegerCast(A, DestTy, /*isSigned=*/ false);
```

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


More information about the llvm-commits mailing list