<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/64796>64796</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
llvm.objectsize cannot compute a trivial bound
</td>
</tr>
<tr>
<th>Labels</th>
<td>
llvm
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
bevin-hansson
</td>
</tr>
</table>
<pre>
The following example results in a return value of 0, but it should be possible to figure out that there's at minimum 10 bytes left in the object:
```
int cond;
long long bos() {
char arr[20];
void *p = cond ? &arr[0] : &arr[10];
return __builtin_object_size(p, 2);
}
```
See Godbolt: https://godbolt.org/z/vhfWv69Tv
Looking at ObjectSizeOffsetVisitor shows two things that seem odd.
The first is that it will give up on visiting instructions that have already been visited:
```
if (Instruction *I = dyn_cast<Instruction>(V)) {
// If we have already seen this instruction, bail out. Cycles can happen in
// unreachable code after constant propagation.
if (!SeenInsts.insert(I).second)
return unknown();
return visit(*I);
}
```
So it won't end up visiting the `alloca` again, and just considers it to have unknown size on the second visit.
Even without this, the analysis still fails. If `MustSucceed` is false, it resorts to finding an exact size instead of a reasonable conservative estimate:
```
// Unless we have to fold this to something, try to be as accurate as
// possible.
if (MustSucceed)
EvalOptions.EvalMode =
MaxVal ? ObjectSizeOpts::Mode::Max : ObjectSizeOpts::Mode::Min;
else
EvalOptions.EvalMode = ObjectSizeOpts::Mode::ExactSizeFromOffset;
```
But the result then is that it cannot find an exact size (because it doesn't know if it's 10 or 20), so it produces the most conservative estimate 0 instead.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyEVk1v2zgQ_TX0ZVBDoeIPHXzIlxcBtugh3e4xGIkjiy1NChxKqfvrF0MpiZNu0CCQKXM4HL735tHIbA-eaKdW12p1u8AhdSHuahqt_9ShZw5-UQdz2n3tCNrgXHiy_gD0E4-9I4jEg0sM1gNCpDREDyO6gSC0UCh9A_WQwCbgLgzOQE3QB2Z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