Issue 76628
Summary Support `!range` metadata for function argument
Labels new issue
Assignees
Reporter AngelicosPhosphoros
    It can be useful for cases like Rusts [`NonZeroInt` types](https://doc.rust-lang.org/stable/std/num/struct.NonZeroUsize.html) or [subrange types](https://wiki.freepascal.org/subrange_types) like in Pascal,

For example, in Rust, when passing non-zero integer function arguments as references, compiler emits `!range` metadata on loads so optimizer can exploit this to remove unnecessary checks for zero (and generated branches).
However, when arguments are passed by value (which is optimal in sense of stack memory usage), frontend cannot emit range metadata for function arguments so LLVM optimizer fails to remove zero checks despite them being unnecessary.

E. g. this optimizes well
```rust
pub fn is_zero(a: &NonZeroU32)->bool {
   a.get() == 0
}
```
Output:
```llvm-ir
define noundef zeroext i1 @is_zero(i32 noundef %_a) unnamed_addr #0 !dbg !14 {
  ret i1 false, !dbg !15
}
```

and this optimizes badly
```rust
pub fn is_zero(a: NonZeroU32)->bool {
   a.get() == 0
}
```
Output:
```llvm-ir
define noundef zeroext i1 @is_zero(i32 noundef %a) unnamed_addr #0 !dbg !7 {
  %_0 = icmp eq i32 %a, 0, !dbg !12
  ret i1 %_0, !dbg !13
}
```

Frontend cannot solve this because even if it would put such the arguments into stack and then load them with metadata specified, first `SROA` or `mem2reg` would remove this operations so later optimizations wouldn't see that values have limited range.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to