[llvm] [VPlan] Expand AddRecs in VPSCEVExpander (PR #209921)

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Sun Aug 9 07:45:29 PDT 2026


================
@@ -996,6 +987,57 @@ VPValue *VPSCEVExpander::tryToExpand(const SCEV *S) {
                                              ResultTy, DL);
     return Result;
   }
+  case scAddRecExpr: {
+    // AddRecs never appear in the vector loop; the AR's loop would correspond
+    // to an outer loop outside the vector loop, and its header would be modeled
+    // as a VPIRBasicBlock.
+    auto *AR = cast<SCEVAddRecExpr>(S);
+    VPlan &Plan = Builder.getPlan();
+
+    // If a canonical IV to re-use is present, it would be in the Plan's entry.
+    // We cannot create a phi in the Plan's entry, which would be required in
+    // the absence of a canonical IV to re-use or if AR is non-affine, because
+    // its predecessors are not modeled: fall back to the IR expander.
+    if (!AR->isAffine())
+      return vputils::getOrCreateVPValueForSCEVExpr(Plan, AR);
+    auto FoundCanIV =
+        find_if(Plan.getEntry()->phis(), [&](const VPRecipeBase &R) {
+          if (!SE.isSCEVable(cast<VPIRPhi>(R).getIRPhi().getType()))
+            return false;
+          const SCEV *Candidate = SE.getSCEV(&cast<VPIRPhi>(R).getIRPhi());
+          return match(Candidate,
+                       m_scev_AffineAddRec(m_scev_Zero(), m_scev_One(),
+                                           m_SpecificLoop(AR->getLoop()))) &&
+                 Candidate->getType() == AR->getType();
+        });
+    if (FoundCanIV == Plan.getEntry()->phis().end())
+      return vputils::getOrCreateVPValueForSCEVExpr(Plan, AR);
+
+    VPValue *CanonicalIV =
+        Plan.getOrAddLiveIn(&cast<VPIRPhi>(FoundCanIV)->getIRPhi());
+    VPValue *Start;
+    Start = tryToExpand(AR->getStart());
+    if (!Start)
+      return nullptr;
+    VPValue *Step = tryToExpand(AR->getStepRecurrence(SE));
+    if (!Step)
+      return nullptr;
+
+    GEPNoWrapFlags GEPFlags;
+    VPIRFlags::WrapFlagsTy NWFlags;
+    if (AR->hasNoUnsignedWrap()) {
+      GEPFlags = GEPNoWrapFlags::noUnsignedSignedWrap();
----------------
fhahn wrote:

I don't think `nuw` directly implies `noUnsignedSignedWrap` in general. This would at least need a proof, but the pointer path in general looks untested?

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


More information about the llvm-commits mailing list