[llvm] [VPlan] Simplify live-ins early using SCEV. (PR #155304)

via llvm-commits llvm-commits at lists.llvm.org
Sat Sep 13 09:09:06 PDT 2025


================
@@ -531,12 +532,29 @@ static void addInitialSkeleton(VPlan &Plan, Type *InductionTy, DebugLoc IVDL,
   createExtractsForLiveOuts(Plan, MiddleVPBB);
 }
 
+/// Check \p Plan's live-in and replace them with constants, if they can be
+/// simplified via SCEV.
+static void simplifyLiveInsWithSCEV(VPlan &Plan, ScalarEvolution &SE) {
+  auto GetSimplifiedLiveInViaSCEV = [&](VPValue *VPV) -> VPValue * {
+    const SCEV *Expr = vputils::getSCEVExprForVPValue(VPV, SE);
+    if (auto *C = dyn_cast<SCEVConstant>(Expr))
+      return Plan.getOrAddLiveIn(C->getValue());
+    return nullptr;
+  };
+
+  for (VPValue *LiveIn : Plan.getLiveIns()) {
+    if (VPValue *SimplifiedLiveIn = GetSimplifiedLiveInViaSCEV(LiveIn))
+      LiveIn->replaceAllUsesWith(SimplifiedLiveIn);
+  }
+}
+
 std::unique_ptr<VPlan>
 VPlanTransforms::buildVPlan0(Loop *TheLoop, LoopInfo &LI, Type *InductionTy,
                              DebugLoc IVDL, PredicatedScalarEvolution &PSE) {
   PlainCFGBuilder Builder(TheLoop, &LI);
   std::unique_ptr<VPlan> VPlan0 = Builder.buildPlainCFG();
   addInitialSkeleton(*VPlan0, InductionTy, IVDL, PSE, TheLoop);
+  simplifyLiveInsWithSCEV(*VPlan0, *PSE.getSE());
----------------
ayalz wrote:

Better doing so here this way than as a vplan-to-vplan transform, because expected to apply only once, which can be at construction?

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


More information about the llvm-commits mailing list