[llvm] [LSR] Deduplicate cast instructions across ICmpZero fixups (PR #201970)
Timur Golubovich via llvm-commits
llvm-commits at lists.llvm.org
Mon Jun 8 06:17:46 PDT 2026
timurgol007 wrote:
> > I've seen this happen in SPEC sealcrypto (the test added here is reduced from that case). Without this optimization, LSR can produce multiple equivalent casts, and later simplifyInstruction can't fold the icmp because its operands end up being different cast instructions.
>
> And the sealcrypto case would _not_ be covered by not emitting inttoptr at all?
Not in this case. The inttoptr casts are still needed because the IV is used both as an integer offset (for the GEPs) and as a pointer value in the comparisons. The reduced test case doesn't fully reflect that aspect of the original sealcrypto example, but that's why LSR ends up generating the casts here. This is the IR that is beeing generated now (without this patch):
```ll
define void @test_inttoptr_reuse(ptr %base1, ptr %base2, ptr %base3, i64 %n) {
entry:
%0 = shl i64 %n, 3
%1 = inttoptr i64 %0 to ptr
br label %loop
loop: ; preds = %loop.latch, %entry
%lsr.iv = phi i64 [ %lsr.iv.next, %loop.latch ], [ 0, %entry ]
%scevgep = getelementptr i8, ptr %base1, i64 %lsr.iv
%scevgep3 = getelementptr i8, ptr %base2, i64 %lsr.iv
%scevgep4 = getelementptr i8, ptr %base3, i64 %lsr.iv
%tmp1 = inttoptr i64 %lsr.iv to ptr
%cmp.exit = icmp eq ptr %1, %tmp1
br i1 %cmp.exit, label %check, label %body
check: ; preds = %loop
%tmp = inttoptr i64 %lsr.iv to ptr
%cmp1 = icmp ne ptr %1, %tmp
%tmp2 = inttoptr i64 %lsr.iv to ptr
%cmp3 = icmp ne ptr %1, %tmp2
%sel = select i1 %cmp1, i1 true, i1 %cmp3
br i1 %sel, label %body, label %exit
body: ; preds = %check, %loop
%v1 = load i64, ptr %scevgep, align 8
%v2 = load i64, ptr %scevgep3, align 8
%sum = add i64 %v1, %v2
store i64 %sum, ptr %scevgep4, align 8
br label %loop.latch
loop.latch: ; preds = %body
%lsr.iv.next = add i64 %lsr.iv, 8
br label %loop
exit: ; preds = %check
ret void
}
```
As you see icmp couldn't be folded in the future optimizations.
https://github.com/llvm/llvm-project/pull/201970
More information about the llvm-commits
mailing list