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

Luke Lau via llvm-commits llvm-commits at lists.llvm.org
Sat Jan 10 00:37:35 PST 2026


================
@@ -2376,14 +2376,66 @@ 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.
+  SmallPtrSet<VPBasicBlock *, 16> Visited;
+  while (!WorklistForSink.empty()) {
+    VPRegionBlock *CurLoop = WorklistForSink.pop_back_val();
+    auto *SingleExit = cast<VPBasicBlock>(CurLoop->getSingleSuccessor());
+    // Check whether there is a unique dedicated exit block.
+    // TODO: Should check all predecessors of the exit block.
+    if (SingleExit->getSinglePredecessor() != CurLoop)
+      continue;
+
+    for (VPBasicBlock *VPBB : VPBlockUtils::blocksOnly<VPBasicBlock>(
+             vp_post_order_shallow(CurLoop->getEntry()))) {
+      // Skip the basic block in inner loops.
+      if (!Visited.insert(VPBB).second)
+        continue;
+      // 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;
+        }
----------------
lukel97 wrote:

Isn't licm run after removeDeadRecipes in VPlanTransforms::optimize? I wouldn't expect there to be any dead recipes to be around to begin with. Does removing this have any impact on the tests?

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


More information about the llvm-commits mailing list