[llvm] [InstCombine] Fold sext of an nsw binop on trunc nsw into a wide nsw … (PR #228530)

Alexey Bataev via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 2 13:00:55 PDT 2026


================
@@ -1931,6 +1977,11 @@ Instruction *InstCombinerImpl::visitSExt(SExtInst &Sext) {
   if (Instruction *I = commonCastTransforms(Sext))
     return I;
 
+  // Do this before EvaluateInDifferentType, which drops nsw and may emit
+  // shl/ashr.
+  if (Instruction *I = foldSExtOfNSWBinOpOfTrunc(Sext, DL))
----------------
alexey-bataev wrote:

```suggestion
  // shl/ashr. Do not widen the arithmetic past a legal type.
  if (isa<VectorType>(Sext.getType()) ||
      shouldChangeType(Sext.getSrcTy(), Sext.getType()))
    if (Instruction *I = foldSExtOfNSWBinOpOfTrunc(Sext, DL))
      return I;
```

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


More information about the llvm-commits mailing list