[llvm] [VPlan] Sink recipes from the vector loop region in licm. (PR #168031)

Luke Lau via llvm-commits llvm-commits at lists.llvm.org
Sun Feb 1 20:23:57 PST 2026


================
@@ -2496,14 +2496,63 @@ void VPlanTransforms::cse(VPlan &Plan) {
 
 /// Move loop-invariant recipes out of the vector loop region in \p Plan.
 static void licm(VPlan &Plan) {
-  VPBasicBlock *Preheader = Plan.getVectorPreheader();
+  VPRegionBlock *LoopRegion = Plan.getVectorLoopRegion();
+
+  // Collect loop regions in preorder.
+  SmallVector<VPRegionBlock *> WorklistForSink{LoopRegion};
+  for (VPRegionBlock *R : VPBlockUtils::blocksOnly<VPRegionBlock>(
+           vp_depth_first_deep(LoopRegion->getEntry()))) {
+    if (!R->isReplicator())
+      WorklistForSink.push_back(R);
+  }
+
+  VPDominatorTree VPDT(Plan);
+  // Sink recipes with no users inside the vector loop region into a dedicated
+  // exit block.
+  while (!WorklistForSink.empty()) {
+    VPRegionBlock *CurLoop = WorklistForSink.pop_back_val();
+    auto *SingleExit =
+        cast_or_null<VPBasicBlock>(CurLoop->getSingleSuccessor());
+    // Check whether there is a unique dedicated exit block.
+    // TODO: Should check all predecessors of the exit block.
+    if (!SingleExit || SingleExit->getSinglePredecessor() != CurLoop)
+      continue;
+
+    for (VPBasicBlock *VPBB : VPBlockUtils::blocksOnly<VPBasicBlock>(
+             vp_post_order_shallow(CurLoop->getEntry()))) {
+      // Skip the basic block that is not dominates the exit block.
+      if (!VPDT.properlyDominates(VPBB, SingleExit))
+        continue;
+
+      for (VPRecipeBase &R : make_early_inc_range(reverse(*VPBB))) {
+        if (isDeadRecipe(R)) {
+          R.eraseFromParent();
+          continue;
+        }
+        if (cannotHoistOrSinkRecipe(R) || R.mayHaveSideEffects())
----------------
lukel97 wrote:

We should land this PR first before refactoring cannotHoistOrSinkRecipe, since we'll want to make sure we continue to hoist assumes

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


More information about the llvm-commits mailing list