<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/58600>58600</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Clang misdiagnoses UB (emits unreachable IR) when incrementing unspecified value
</td>
</tr>
<tr>
<th>Labels</th>
<td>
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
bcsb1001
</td>
</tr>
</table>
<pre>
Clang, with -O1 or higher, seems to think the following C code invokes undefined behaviour. The IR output given by `clang -std=c17 -O1 -emit-llvm -S` has `main` as just `unreachable`.
```c
#include <stdio.h>
int main(void) {
unsigned char x;
(void) &x;
while (++x) putchar('a');
putchar('\n');
}
```
My understanding is that this is _not_ UB under C17 because the address of `x` is taken and `unsigned char` can't have trap representations, so the value of `x` is unspecified until the first increment. Interestingly, if you replace `++x` with `x+=1`, Clang compiles the code correctly, even though these are equivalent according to the standard. The same bug happens with `x++`, `--x` or `x--` instead of `++x`.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJxdU8mO2zAM_Rr5QtjwUieZgw-dpAV6KAp0OReyRNuaUSRXS2by96Xk2QPFCUX6kXx8HK28DkfNzczaIzyosED5owHrYFHzgi7desSzh2AhLMrc0xNhslrbB2VmOIKwEkGZi71HD9FInJRBCSMu_KJsdBX8pje-_QQbwxoDzOqCBsYrsF0tUmYofZCsO4lmn5OXeFah1PpyhvIXBcHCfQo-c2WSSdZd9CFdReOQi4WPGsmqWH1i9Wf6tx3xZLedMkJHKpN1R8qlbLWw7suTNz-VCZDx28PFKsnaG2D7280H9InGqzm1RckcPLLuje_tO-3uve9hURpTBGtv6TymICIhoeTLPacvXb576V0A64_mQwzbnz50upnfr5l_5wM3Mg1H0dgWHtLgfDL-Ghv-wp_bLQyORPiIgkePeapcSofeg50St4-J64TA72lehLgR_oaHFCB4qi7QjC4E4vgKDldCQRN4UNb4rCCb8S9cR_yAToArCjUpwowmKL3pS1ETJCrh8ExIFXwzAQk1UFf6miDVBFcbUzLNBSbEZ4YJN-s4J6G77tRk7xGyzkmv55WG4nOiLF5hnUMRNmBM6gyLjfOSIoga7hDwX1RUPtUCXFB8pjdsbWW6uZOb0D0_I4xxJkbWFY1_XwydrRb6KctcLO1a8pZlZsT4gFw-kfTaUlXg0Ox2u31Tt11XyKGTN90NL4IKGrcFhrPyUvHZWE_N0ZBJP2mTEscvW0J7mCT4sFCTL_SmXt7OIc-piE4PSwirZx2t0Fc6M3USx4oIJCPt59NPuTp7RwSSqbyPSDP_2h92dV0sw35EzkU9Hm4OQn6a9qPcN1JOhwMi9SJ5ofmI2g-sv2X9qVBDW7ctNdk3fd31XYW8rcU4YTfWfd1PE_tUIy2qrlLiyrq5cEOugSj35NTKB__q5D7rFZ_xeaTRumEUfmzquilyvUMu9j83v5TQ">