[llvm] [NPM] Make "LoopStrengthReduce" function pass (PR #186312)
Aiden Grossman via llvm-commits
llvm-commits at lists.llvm.org
Sat Apr 11 12:45:40 PDT 2026
https://github.com/boomanaiden154 commented:
I don't think this is the route that we want to go down.
While Loop Strength Reduce is only used in the backend currently, does not necessarily receive loops in LCSSA and does not currently preserve LCSSA form, that is probably what we should be doing. Loop Strength Reduce currently does a lot of `getSCEV`, for which LCSSA is important (or otherwise you need to be careful with getting scoped SCEV). The optimization quality also improves when using LCSSA:
```patch
diff --git a/llvm/test/Analysis/ScalarEvolution/pr62430.ll b/llvm/test/Analysis/ScalarEvolution/pr62430.ll
index a3e3b8ff5c11..a8e91b34b65d 100644
--- a/llvm/test/Analysis/ScalarEvolution/pr62430.ll
+++ b/llvm/test/Analysis/ScalarEvolution/pr62430.ll
@@ -13,14 +13,12 @@ define void @test() {
; CHECK-NEXT: [[LSR_IV:%.*]] = phi i32 [ [[LSR_IV_NEXT:%.*]], [[BB7]] ], [ 16, [[BB]] ]
; CHECK-NEXT: br label [[BB4:%.*]]
; CHECK: bb4:
-; CHECK-NEXT: [[LSR_IV3:%.*]] = phi i32 [ [[LSR_IV_NEXT4:%.*]], [[BB6:%.*]] ], [ [[LSR_IV1]], [[BB3]] ]
-; CHECK-NEXT: br i1 true, label [[BB7]], label [[BB6]]
+; CHECK-NEXT: br i1 true, label [[BB7]], label [[BB6:%.*]]
; CHECK: bb6:
-; CHECK-NEXT: [[LSR_IV_NEXT4]] = add i32 [[LSR_IV3]], 268435456
; CHECK-NEXT: br label [[BB4]]
; CHECK: bb7:
-; CHECK-NEXT: [[MUL9:%.*]] = mul i32 [[LSR_IV3]], [[LSR_IV3]]
-; CHECK-NEXT: [[MUL10:%.*]] = mul i32 [[MUL9]], [[LSR_IV3]]
+; CHECK-NEXT: [[MUL9:%.*]] = mul i32 [[LSR_IV1]], [[LSR_IV1]]
+; CHECK-NEXT: [[MUL10:%.*]] = mul i32 [[MUL9]], [[LSR_IV1]]
; CHECK-NEXT: call void @foo(i32 [[MUL10]])
; CHECK-NEXT: [[SEXT:%.*]] = sext i32 [[MUL10]] to i64
; CHECK-NEXT: [[LSR_IV_NEXT]] = add i32 [[LSR_IV]], 32
```
(from an in-tree test example where we currently use the LegacyPM, which means loop strength reduction gets a loop in non-LCSSA form).
Preservation of LCSSA I would guess is also important in some cases when optimizing loop nests, but I haven't thought too hard about that case.
Converting this to a function pass also probably means we want to just drop this late loop pipeline altogether and also convert `CanonicalizeFreezeInLoops` (which should be fine given it just hoists things to the preheader, but not sure if we end up with loops in loopsimplify form here without the PM guaranteeing it) along with `LoopTermFold`, which might need some work around using scoped SCEV.
Needing to construct a worklist to iterate over all the loops for each function along with reconstructing most loop analyses, which would be duplicated across the three passes, would also be a bit unfortunate.
It would be good if someone more familiar with loop optimizations could double check my thinking given I'm pretty new to this, but those are my thoughts on this.
https://github.com/llvm/llvm-project/pull/186312
More information about the llvm-commits
mailing list