<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/76628>76628</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Support `!range` metadata for function argument
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
AngelicosPhosphoros
</td>
</tr>
</table>
<pre>
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.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzUVUuP2zYQ_jX0ZbACTfmlgw6bdYwGSJsgQXvoZUGJI4ldilQ5pB3n1xekvLuOkfRxLGBINDWPb755SSLdW8Sard-w9X4hYxicr-9tj0a3jj4OjqbBeUeLxqlz_S5AKy00CJGwiwY656GVhARGPyF8ihQIkrEN_8XZ39G7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