[llvm] [VPlan] Narrow truncates of an induction plus a constant offset (PR #226035)

Ramkumar Ramachandra via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 25 00:09:33 PDT 2026


================
@@ -929,7 +953,26 @@ getOptimizableIVOf(VPValue *VPV, PredicatedScalarEvolution &PSE) {
     }
     llvm_unreachable("should have been covered by switch above");
   };
-  return IsWideIVInc() ? WideIV : nullptr;
+  if (IsWideIVInc())
+    return {WideIV, WideIV->getStartValue()};
+
+  // Look through a constant offset from the induction. The offset need not be
+  // the induction step, as start + C + i * step stays affine for any constant
+  // C, so it can be folded into the start value.
+  const APInt *C;
+  APInt Offset;
+  if (match(VPV, m_c_Add(m_Specific(WideIV), m_APInt(C))))
+    Offset = *C;
+  else if (match(VPV, m_Sub(m_Specific(WideIV), m_APInt(C))))
+    Offset = -*C;
+  else
+    return {nullptr, nullptr};
----------------
artagnon wrote:

Isn't the logic as simple as:

```suggestion
  VPBuilder Builder(WideIV);
  VPValue *PostInc, *PostIncStart;
  if (match(VPV, m_c_Add(m_Specific(WideIV), m_VPValue(PostInc))))
    PostIncStart = Builder.createAdd(WideIV->getStart(), Offset);
  else if (match(VPV, m_Sub(m_Specific(WideIV), m_VPValue(PostInc))))
    PostIncStart = Builder.createSub(WideIV->getStart(), Offset);
  else
    return {WideIV, nullptr};
 return {WideIV, PostIncStart};
```

We're essentially turning a PreInc WideIV to handle increments, and returning an optional PostIncStart? I think you could also extend getOptimizableIVOf to take a `VPValue &*PostIncStart` argument to avoid the ugly pair returns?

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


More information about the llvm-commits mailing list