<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/55704>55704</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Merge e02f4976acbf086904bf3903348603cc0f04e6ad into 14.x
</td>
</tr>
<tr>
<th>Labels</th>
<td>
llvm:codegen,
release:backport
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
tstellar
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
hvdijk
</td>
</tr>
</table>
<pre>
clang 14.0.x releases can miscompile Doxygen causing it to segfault. A reduced test for this is
```c
struct S {
char *a;
unsigned b, c;
};
void foo(struct S *);
void bar(unsigned n)
{
struct S s[5];
for (unsigned i = 0; i < 5; i++) {
s[i].a = 0;
s[i].b = 0;
s[i].c = 0;
}
for (unsigned i = 0; i < 5; i++) {
if (n > s[i].c) {
unsigned c = n * 2;
void *a = __builtin_realloc(s[i].a, c);
if (!a)
break;
s[i].a = a;
s[i].c = c;
}
s[i].b = n;
}
foo(s);
}
```
When compiling with clang -O2, the assembly shows that the allocation is optimised away, `foo` is always called with `s[i].a` to a null pointer and `s[i].c` to zero.
The bug is in LoopIdiomRecognizePass which optimises the first loop to a memset with wrong aliasing info, which then causes `n > s[i].c` to be mis-optimised to `false` in the second loop. This is fixed on main by e02f4976acbf086904bf3903348603cc0f04e6ad which applies cleanly to 14.0.x and fixes the bug there too.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJytVU2PnDgQ_TX0xRrkMV_NgUNPeiOtlGhX2Uh7HBXGgDNuG9lmejq_PmXTYZiPRHvYFmrA9Vz16pWraE13abgCPZDbPKXpE7FCCXDCEQ6anKTj5jRJJcjRPF0GoXF5dhLh0hNviBNDD7PyKTngzm7moiNeOE96Y4kfpSPSJfSY0MP1v6TLxZd35-3MPfmHJNXdskLwx0ewJGEHSLLN6qydHDQGaBP2gfDVllTH5-f4_2hkhwxMwvbPAdghYfUKjJAWLEJWvzoAri43cVcXLinuiqQ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