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

Renato Golin via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 9 19:59:03 PDT 2024


================
@@ -1586,3 +1587,22 @@ 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)) {
+      if (R.mayHaveSideEffects() || R.mayReadFromMemory() || R.isPhi() ||
+          any_of(R.operands(), [](VPValue *Op) {
----------------
rengolin wrote:

Technically an op can still be hoisted if it reads from memory with no side effects, as long as the pointer is constant throughout the loop, which the `nay_of` takes care. It may increase register pressure inside the loop, but only if the liveness of the value is short, and the cost model should take care of the comparison.

What cases are you thinking here?

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


More information about the llvm-commits mailing list