[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
Mon Oct 5 16:53:01 PDT 2026


================
@@ -1931,6 +1997,13 @@ Instruction *InstCombinerImpl::visitSExt(SExtInst &Sext) {
   if (Instruction *I = commonCastTransforms(Sext))
     return I;
 
+  // Do this before EvaluateInDifferentType, which drops nsw and may emit
+  // 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, Builder))
----------------
alexey-bataev wrote:

```suggestion
  // shl/ashr. Do not widen the arithmetic past a legal type; check the scalar
  // types so that vectors follow the same rule as scalars.
  if (shouldChangeType(Sext.getSrcTy()->getScalarType(),
                       Sext.getType()->getScalarType()))
    if (Instruction *I = foldSExtOfNSWBinOpOfTrunc(Sext, Builder))
```

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


More information about the llvm-commits mailing list