[llvm] [VPlan] Add initial loop-invariant code motion transform. (PR #107894)

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 20 01:57:36 PDT 2024


================
@@ -971,6 +971,40 @@ static void simplifyRecipe(VPRecipeBase &R, VPTypeAnalysis &TypeInfo) {
     return R.getVPSingleValue()->replaceAllUsesWith(A);
 }
 
+/// Move loop-invariant recipes out of the vector loop region in \p Plan.
+static void licm(VPlan &Plan) {
+  VPRegionBlock *LoopRegion = Plan.getVectorLoopRegion();
+  VPBasicBlock *Preheader =
+      cast<VPBasicBlock>(LoopRegion->getSinglePredecessor());
+
+  // Return true if-and-only-if we know how to (mechanically) both hoist a given
+  // recipe out of a loop region.  Does not address legality concerns such as
+  // aliasing or speculation safety.
+  auto CanHoistRecipe = [](VPRecipeBase &R) {
+    // Allocas cannot be hoisted.
+    auto *RepR = dyn_cast<VPReplicateRecipe>(&R);
+    return !RepR || RepR->getOpcode() != Instruction::Alloca;
+  };
+
+  // Hoist any loop invariant recipes from the vector loop region to the
+  // preheader.
+  for (VPBasicBlock *VPBB : VPBlockUtils::blocksOnly<VPBasicBlock>(
+           vp_depth_first_shallow(LoopRegion->getEntry()))) {
----------------
fhahn wrote:

Yes, I'll add a comment to clarify re shallow traversal.

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


More information about the llvm-commits mailing list