<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/149821>149821</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
LLVM doesn't keep nullability information around when inlining
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
philnik777
</td>
</tr>
</table>
<pre>
Consider
```
struct S {
int* i;
};
[[gnu::returns_nonnull]] int* get(S& s) { return s.i; }
auto test1(S& s) {
auto val = get(s);
if (!val)
__builtin_trap();
return *val;
}
auto deref(int* val) {
if (!val)
__builtin_trap();
return *val;
}
auto test2(S& s) {
auto val = get(s);
return deref(val);
}
```
`test1` gets optimized better than `test2`, even though the exact same information should be available to the compiler. It looks like LLVM doesn't keep the information that `s.i` can never be null when inlining `get`. This can be relevant for classes like `string`, where `data()` can never return a null pointer, but annotating the function doesn't help to remove redundant checks, since the annotation is lost as soon as the function is inlined.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJycVE-L6zYQ_zTyZXhBlu04OfiQfSFQeD290usi2xNLjSIZaeTt9tOXcbzd3ba08CDgSJrfv9EgnZKdPGInmifRnAudyYTYzcY6b29t2xZ9GF-7r8EnO2IU8iT2cvvJU6KYB4LvINonIU8A1pNQJ7Ci4rVoz9sfeVr5nyafRXUS1Ski5ejTsw_eZ-dEcxbN-Q0-IQl1-C7UHpJQR2aHBwDSjrmBmVdanSkAYaLy74jVz3q8aAeiOm-0fP5wBWCvINRBqHLRjrd5D-D5uc_WkfXPFPW8FrwhNhdCnRjxV8h3KyNGvAp12JI8eN_b80-9_xD7fzUOrn4k-Mb85naz81Hhwy0_lo8m7yWzJQgz2bv9A0fokQgjkNEetjLFOPUVcEEPZEKeDJBBwN_1QJD0HcH6a4h3TTZ4SCZkx0SgF22d7h0ChzMIQ7jP1mHcwU8ELoRbAmdvCN--_fozjAGTF6oluCHOa_1HWjKa2BGPzF7CoD14XDCyEM8cvBj0YL2z3vqJK7lPe7mDX4xNa32PENHhoj3BNUQYnE4JNw9MTdH6aUv7YjCuu6MmvV3kJ92t6fqhPgfrCSMj-0ygvQ-kiZ1wkGv2w5riPaRBN3NfIt7DwsbG7Ed2NhgcbomJkvUDrvg3uuDBJnAhEegEKQTP308KNj2agOOuGLtqPFZHXWBXtk1VyrZqDoXpRqzGQen6WGPVy2NVXut9NVRK6vpYqlYXtlNSNbJVZVmXTXPY9dWxbrDfl7JRQ99IUUu8a-t2zi33XYhTYVPK2JX18aDKwukeXVpfIaU8vsB6KpTiRyl2DPrS5ymJWjqbKL3TkCWH3b8NBLdZ99ZZev00GDqG7MfP11_k6DpDNCd-n9RFqMtkyeR-N4S7UBeW2z5f5hh-w4GEuqwmk1CXLcXSqT8DAAD__x4qqL4">