[llvm] [VPlan] Sink vector loop region recipes in licm (PR #172343)

Luke Lau via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 7 03:17:46 PST 2026


================

----------------
lukel97 wrote:

I took a look at this, I think it's because your patch runs the sinking before the hoisting, but this patch does the sinking after. So e.g. this not recipe which was previously hoisted in the preheader is now sunk into the middle block:

```diff
--- scratch/lukepatch.dbg.txt	2026-01-07 18:43:52
+++ scratch/melpatch.dbg.txt	2026-01-07 18:45:00
@@ -5598,37 +5598,37 @@
 
 ir-bb<entry>:
 Successor(s): scalar.ph, vector.ph
 
 vector.ph:
-  EMIT vp<%3> = not ir<%c>
 Successor(s): vector loop
 
 <x1> vector loop: {
   vector.body:
-    EMIT vp<%4> = CANONICAL-INDUCTION ir<0>, vp<%index.next>
-    EXPLICIT-VECTOR-LENGTH-BASED-IV-PHI vp<%5> = phi ir<0>, vp<%index.evl.next>
-    ir<%narrow.iv> = WIDEN-INDUCTION ir<-12>, ir<1>, vp<%6>
+    EMIT vp<%3> = CANONICAL-INDUCTION ir<0>, vp<%index.next>
+    EXPLICIT-VECTOR-LENGTH-BASED-IV-PHI vp<%4> = phi ir<0>, vp<%index.evl.next>
+    ir<%narrow.iv> = WIDEN-INDUCTION ir<-12>, ir<1>, vp<%5>
     EMIT-SCALAR vp<%avl> = phi [ ir<12>, vector.ph ], [ vp<%avl.next>, vector.body ]
-    EMIT-SCALAR vp<%6> = EXPLICIT-VECTOR-LENGTH vp<%avl>
-    EMIT vp<%index.evl.next> = add nuw vp<%6>, vp<%5>
-    EMIT vp<%avl.next> = sub nuw vp<%avl>, vp<%6>
-    EMIT vp<%index.next> = add nuw vp<%4>, vp<%0>
+    EMIT-SCALAR vp<%5> = EXPLICIT-VECTOR-LENGTH vp<%avl>
+    EMIT vp<%index.evl.next> = add nuw vp<%5>, vp<%4>
+    EMIT vp<%avl.next> = sub nuw vp<%avl>, vp<%5>
+    EMIT vp<%index.next> = add nuw vp<%3>, vp<%0>
     EMIT branch-on-count vp<%index.next>, vp<%1>
   No successors
 }
 Successor(s): middle.block
 
 middle.block:
-  WIDEN-INTRINSIC vp<%8> = call llvm.vp.merge(vp<%3>, ir<%x>, ir<1>, vp<%6>)
+  EMIT vp<%7> = not ir<%c>
+  WIDEN-INTRINSIC vp<%8> = call llvm.vp.merge(vp<%7>, ir<%x>, ir<1>, vp<%5>)
   WIDEN ir<%ud> = udiv ir<%narrow.iv>, vp<%8>
   WIDEN-CAST ir<%ud.ext> = zext ir<%ud> to i16
-  WIDEN-INTRINSIC vp<%9> = call llvm.vp.merge(vp<%3>, ir<%y>, ir<1>, vp<%6>)
+  WIDEN-INTRINSIC vp<%9> = call llvm.vp.merge(vp<%7>, ir<%y>, ir<1>, vp<%5>)
   WIDEN ir<%sd> = sdiv ir<%ud.ext>, vp<%9>
   WIDEN-CAST ir<%sd.ext> = sext ir<%sd> to i32
   BLEND ir<%merge> = ir<%sd.ext> ir<0>/ir<%c>
-  EMIT-SCALAR vp<%10> = zext vp<%6> to i64
+  EMIT-SCALAR vp<%10> = zext vp<%5> to i64
   EMIT vp<%11> = sub vp<%10>, ir<1>
   EMIT vp<%12> = extract-lane vp<%11>, ir<%merge>
 Successor(s): ir-bb<exit>
```

So I presume this now gets costed in `isOutsideLoopWorkProfitable`.  AFAIK the vector preheader isn't costed?

FWIW we should probably be sinking this recipe where possible to minimize register pressure, but maybe best to look at doing this as a follow up in another PR so we can add your planContainsAdditionalSimplifications check?

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


More information about the llvm-commits mailing list