[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:40 PDT 2024


================
@@ -1467,6 +1478,15 @@ Instruction *InstCombinerImpl::visitSExt(SExtInst &Sext) {
     if (ComputeNumSignBits(X, 0, &Sext) > XBitSize - SrcBitSize)
       return CastInst::CreateIntegerCast(X, DestTy, /* isSigned */ true);
 
+    // If trunc has nsw flag, then convert directly to final type.
+    auto *CSrc = static_cast<TruncInst *>(Src);
+    if (CSrc->hasNoSignedWrap()) {
+      CastInst *I = CastInst::CreateIntegerCast(X, DestTy, /* isSigned */ true);
----------------
dtcxzyw wrote:

```suggestion
      CastInst *I = CastInst::CreateIntegerCast(X, DestTy, /*isSigned=*/ true);
```


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


More information about the llvm-commits mailing list