<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/54878>54878</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
LICM miscompile with TBAA
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
efriedma-quic
</td>
</tr>
</table>
<pre>
Consider the following program:
```
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void f(int* x, float *y, int n) {
int z = 0;
for (int i = 0; i < n; ++i) {
x[i] = i;
*y = 1.0;
}
}
void (*ff)(int*,float*,int) = f;
int main() {
void* x = malloc(4);
ff((int*)x, (float*)x, 1);
printf("%f\n", ((float*)x)[0]);
}
```
With gcc, or clang -O0, this prints "1.000000". With clang -O2, this prints "0.000000".
clang is pretty clearly wrong here according to the C effective type rules: in `*y = 1.0;`, the value 1.0 is "stored into an object having no declared type" (C99 6.5p6), and the value is loaded through an lvalue with the same type. Granted, the rules have been under debate, but I don't think anyone has disputed this particular consequence of the rules...
The C++ rules are a bit more complicated to follow, but I think they're supposed to imply the same thing as the C rules? Anyway, even if you suppose that the previous testcase is somehow undefined in C++, the following should be well-defined, and has the same issue:
```
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <new>
void f(void* x, void*y, int n) {
int z = 0;
for (int i = 0; i < n; ++i) {
new ((char*)x + i * sizeof(int)) int(i);
new (y) float(1.0);
}
}
void (*ff)(void*,void*,int) = f;
int main() {
void* x = malloc(4);
ff(x, x, 1);
printf("%f\n", ((float*)x)[0]);
}
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy9Vk2P2zYQ_TXyZbCCLFu2dNDBKzdFgBa9BOiZEkcWW1p0SMqu8us7Q8lfm2zRHJKFoCU5X2_eUG-3NnIsK9M7JdGC7xBao7W5qP4AJ2sOVhyj1S5K9lFyfW-S-Zm26Ur1jR4kQrSqnJfKxF20-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