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

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 17 05:00:41 PDT 2024


================
@@ -1587,3 +1588,24 @@ void VPlanTransforms::createInterleaveGroups(
       }
   }
 }
+
+void VPlanTransforms::licm(VPlan &Plan) {
+  VPRegionBlock *LoopRegion = Plan.getVectorLoopRegion();
+  VPBasicBlock *Preheader =
+      cast<VPBasicBlock>(LoopRegion->getSinglePredecessor());
+  // 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()))) {
+    for (VPRecipeBase &R : make_early_inc_range(*VPBB)) {
+      // TODO: Relax checks in the future, e.g. we could also hoist reads, if
+      // their memory location is not modified in the vector loop.
+      if (R.mayHaveSideEffects() || R.mayReadFromMemory() || R.isPhi() ||
+          any_of(R.operands(), [](VPValue *Op) {
----------------
fhahn wrote:

Is that to break up the conditions into separate `ifs`? My thinking  for checking all legality conditions together is to check them all in the same way (any condition that prevents hoisting) vs some checks checking if anything prevents hoisting and some checking the inverse (can this be hoisted).

I left the code as is for now, but be happy to split things up if there's a strong preference. 

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


More information about the llvm-commits mailing list