llvm / llvm/llvm-project

Missed simplification due to failing to infer that %2 == 6 is unreachable since the loop exits at %2 == 1

Open
#188,737 2 comments 0 reactions 0 assignees View on GitHub
llvm:optimizations missed-optimization
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

The following reduced IR is derived from https://github.com/torvalds/linux/blob/0138af2472dfdef0d56fc4697416eaa0ff2589bd/arch/sh/kernel/hw_breakpoint.c#L47

https://godbolt.org/z/bWo1E3nYa
alive2: https://alive2.llvm.org/ce/z/MFZQ3H

In the reduced IR, reaching `%10` requires `%2 == 6`, but the loop exits earlier at block `%7` when `%2 == 1`, so execution can never reach that value.

```llvm
define noundef i32 @arch_install_hw_breakpoint(ptr readnone %bp_per_reg) local_unnamed_addr {
br label %1

1: ; preds = %7, %0
%2 = phi i64 [ 0, %0 ], [ %8, %7 ]
%3 = getelementptr [4 x ptr], ptr %bp_per_reg, i64 0, i64 %2
%4 = icmp eq ptr %3, null
br i1 %4, label %5, label %7

5: ; preds = %1
%6 = icmp eq i64 %2, 6
br i1 %6, label %10, label %common.ret

7: ; preds = %1
%8 = add nuw nsw i64 %2, 1
%9 = icmp eq i64 %2, 1
br i1 %9, label %common.ret, label %1, !llvm.loop !0

common.ret: ; preds = %7, %10, %5
ret i32 0

10: ; preds = %5
tail call void asm sideeffect "mov $0, %db6", "r,~{dirflag},~{fpsr},~{flags}"(i64 0) #0
br label %common.ret
}

attributes #0 = { nounwind }

!0 = distinct !{!0, !1, !2}
!1 = !{!"llvm.loop.mustprogress"}
!2 = !{!"llvm.loop.unroll.disable"}
```

Expected:
```llvm
define noundef i32 @arch_install_hw_breakpoint(ptr readnone captures(none) %bp_per_reg) local_unnamed_addr #0 {
common.ret:
ret i32 0
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.