| Issue |
235399
|
| Summary |
[LoopStrengthReduce] Use-list order picks between two equal-cost solutions, and one of them costs an extra instruction (`d - 1` in a counted loop)
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
bucaoji
|
In a simple loop, LSR finds two solutions with exactly the same cost. Which one it picks depends only on the use-list order of the IV. One of them reuses the IV increment for free. The other needs an extra `add -1` in every iteration, which shows up as `sub w12, w11, #1` on AArch64 and `leal -1(%rdi)` on x86-64. Plain `clang -O2` on the C source below ends up with the second one. Split out of #235371.
### Minimal reproducer
The two runs differ only in one `uselistorder` directive for the IV `%12`, whose two users are `%8` (the `add`) and `%13` (the `urem`). Without the directive, LSR keeps `{2,+,1}`. With it, LSR switches to `{3,+,1}`:
```llvm
target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-n32:64-S128-Fn32"
target triple = "arm64-apple-macosx26.0.0"
@.str = private unnamed_addr constant [4 x i8] c"%d\0A\00", align 1
define noundef i32 @main() local_unnamed_addr #0 {
br label %1
1: ; preds = %0, %15
%2 = phi i32 [ 2, %0 ], [ %18, %15 ]
%3 = phi i32 [ 0, %0 ], [ %17, %15 ]
%4 = icmp samesign ult i32 %2, 4
br i1 %4, label %15, label %11
5: ; preds = %15
%6 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef %17)
ret i32 0
7: ; preds = %11
%8 = add nuw nsw i32 %12, 1
%9 = mul nuw nsw i32 %8, %8
%10 = icmp samesign ugt i32 %9, %2
br i1 %10, label %15, label %11, !llvm.loop !10
11: ; preds = %1, %7
%12 = phi i32 [ %8, %7 ], [ 2, %1 ]
%13 = urem i32 %2, %12
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %7
15: ; preds = %7, %11, %1
%16 = phi i32 [ 1, %1 ], [ 0, %11 ], [ 1, %7 ]
%17 = add nuw nsw i32 %16, %3
%18 = add nuw nsw i32 %2, 1
%19 = icmp eq i32 %18, 20000000
br i1 %19, label %5, label %1, !llvm.loop !12
uselistorder i32 %12, { 1, 0 }
}
declare i32 @printf(ptr, ...)
attributes #0 = { nounwind "target-cpu"="apple-m1" }
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}
!12 = distinct !{!12, !11}
```
```bash
opt -passes=loop-reduce -S repro.ll # IV becomes {3,+,1}, adds `add i32 %lsr.iv, -1` before the urem
llc -O2 repro.ll -o - # inner loop: sub; udiv; msub; cbz; mul; add; cmp; b.ls (8)
# without the uselistorder line: # inner loop: udiv; msub; cbz; add; mul; cmp; b.ls (7)
```
Checked with Homebrew LLVM 23.1.4 locally, and with `opt (assertions trunk)` and `llc (trunk)` on Compiler Explorer (run 2026-10-25).
### `-debug-_only_=loop-reduce` (assertions trunk)
Both orders end with the same cost:
```
The chosen solution requires 2 instructions 1 reg, with addrec cost 1, plus 1 base add, plus 1 setup cost:
```
Without the directive (fixup sites: `UserInst=%13` the `urem`, then `UserInst=%9` the `mul`):
```
LSR Use: Kind=Basic, Offsets={0}, all-fixups-unconditional
reg({2,+,1}<nuw><nsw><%11>)
LSR Use: Kind=Basic, Offsets={0}, all-fixups-unconditional
reg({2,+,1}<nuw><nsw><%11>) + imm(1)
```
With the directive, the fixup sites are visited in the opposite order (`UserInst=%9`, then `UserInst=%13`) and the chosen register is `{3,+,1}`:
```
LSR Use: Kind=Basic, Offsets={0}, all-fixups-unconditional
reg({3,+,1}<nuw><%11>)
LSR Use: Kind=Basic, Offsets={0}, all-fixups-unconditional
reg({3,+,1}<nuw><%11>) + imm(-1)
```
Both solutions are counted as "1 base add", but they don't cost the same in the generated code:
- With `{2,+,1}`, the `+ imm(1)` is the same value as the IV's own post-increment, so it's free.
- With `{3,+,1}`, the `+ imm(-1)` on the `urem` operand is a real instruction in every iteration.
It looks like the cost model doesn't see that one of the base adds coincides with the IV increment, so the tie is broken by whichever solution it reaches first. That in turn depends on use-list order.
### From C source
```c
#include <stdio.h>
int main(void) {
int c = 0;
for (int n = 2; n < 20000000; n++) {
int p = 1;
for (int d = 2; d * d <= n; d++)
if (n % d == 0) { p = 0; break; }
c += p;
}
printf("%d\n", c);
return 0;
}
```
`clang -O2` produces the order that triggers this. `-mllvm -disable-lsr` avoids it.
- **AArch64:** the default inner loop is `sub w12, w11, #1`; `udiv`; `msub`; `cbz`; `mul`; `add`; `cmp`; `b.ls`. With `-disable-lsr` it is `udiv`; `msub`; `cbz`; `add`; `mul`; `cmp`; `b.ls`.
- **x86-64** (`--target=x86_64-apple-macos`, codegen only): the default inner loop starts with `leal -1(%rdi), %r9d` before the `divl`, and `-disable-lsr` removes it (12 → 11 instructions).
- `-Oz` also avoids it, and so does `-O2 -mllvm -rotation-max-header-size=0` (Apple clang 21.0.0). In both cases the loop isn't rotated, and the inner loop becomes `mul`; `cmp`; `b.hi`; `udiv`; `msub`; `add`; `cbnz` (7 instructions, no `d - 1`). On M2 that runs at 2.77–2.78 cycles/iter, vs 2.887 for `-O2`. So `-Oz` is the fastest optimization level for this loop: `-O1`, `-O2`, `-O3` and `-Os` all generate the same 8-instruction loop.
- `-fwrapv` also avoids it. It changes the IR (`srem`, no `nuw`/`nsw`), so I haven't isolated why.
One detail that confused me at first: `clang -O2 -S -emit-llvm` followed by `llc -O2` does **not** reproduce this, because textual IR drops the use-list order. It does reproduce with `-Xclang -emit-llvm-uselists`.
### Runtime effect
On Apple M2 Pro, with Homebrew LLVM 23.1.4, `-mllvm -disable-lsr` makes the binary 2.1% faster: 2.861 → 2.800 cycles per inner iteration, 5 interleaved runs, sd ≤ 0.021. The measured gain depends on scheduling (#235371). With a better instruction order at the loop end, removing the extra instruction makes little or no difference on this chip. So I'd treat this mainly as a codegen-quality and determinism issue (one redundant instruction, chosen by use-list order) rather than a large speedup. I haven't measured on x86 hardware.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs