[llvm] [IndVars] Support decrementing counters in LFTR (PR #225619)

Pengcheng Wang via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 23 20:54:43 PDT 2026


================
@@ -119,13 +119,16 @@ define void @zext_preinc(ptr %A, i32 %start) {
 ; CHECK-NEXT:    br i1 [[NONPOS]], label [[EXIT:%.*]], label [[FOR_BODY_PREHEADER:%.*]]
 ; CHECK:       for.body.preheader:
 ; CHECK-NEXT:    [[TMP0:%.*]] = zext i32 [[START]] to i64
+; CHECK-NEXT:    [[UMIN:%.*]] = call i32 @llvm.umin.i32(i32 [[START]], i32 6)
----------------
wangpc-pp wrote:

Before:

```asm
addi    s1, a1, 1
li      s2, 6
.LBB3_2:
call    use_ptr
addi    s1, s1, -1
addi    s0, s0, -4
bltu    s2, s1, .LBB3_2
```

After (w/o Zbb):

```asm
li      a3, 6
mv      s1, a2
bltu    a2, a3, .LBB3_3
li      s1, 6
.LBB3_3:
not     a1, a1
add     s1, a1, s1
.LBB3_4:
call    use_ptr
addiw   s1, s1, 1
addi    s0, s0, -4
bnez    s1, .LBB3_4
```

After (w/ Zbb):

```asm
li      a3, 6
minu    s1, a2, a3
not     a1, a1
add     s1, s1, a1
.LBB3_2:
call    use_ptr
addiw   s1, s1, 1
addi    s0, s0, -4
bnez    s1, .LBB3_2
```

So this PR removes the loop-invariant `6` from a live register and eliminates its potential spill/reload, but it still adds extra efforts like `minu` to compute the new limit before the loop. I don't know if we should take it into consideration since it is in the preheader, and this behavior is not introduced by this PR (it is from the original LFTR code).


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


More information about the llvm-commits mailing list