<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/55207>55207</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Volatile is discarded in the frontend during a cast to a reference to a volatile type
</td>
</tr>
<tr>
<th>Labels</th>
<td>
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
jeremy-rifkin
</td>
</tr>
</table>
<pre>
Loads and stores of references to volatile variables aren't reflected in the generated IR as being as volatile. None of `const_cast<volatile int&>(x)`, `static_cast<volatile int&>(x)`, or `(volatile int&)(x)` are properly reflected. If I'm understanding correctly, `f0` here should be generating six add instructions and the InstCombine should not be allowed
```cpp
void f0() {
const_cast<volatile int&>(x) = const_cast<volatile int&>(x) + 1;
const_cast<volatile int&>(x) = const_cast<volatile int&>(x) + 1;
static_cast<volatile int&>(x) = static_cast<volatile int&>(x) + 1;
static_cast<volatile int&>(x) = static_cast<volatile int&>(x) + 1;
(volatile int&)x = (volatile int&)(x) + 1;
(volatile int&)x = (volatile int&)(x) + 1;
}
```
```x86asm
f0(): # @f0()
add dword ptr [rip + x], 6
ret
```
Volatile is respected if it's in the context of a pointer or a reference variable is created.
https://godbolt.org/z/hE6xxP8To
GCC and MSVC do not fold the increments in `f0`: https://godbolt.org/z/hEPe66cjT.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzFVF1vmzAU_TXm5aqImQDpAw9r0k2Rtqnaqr5OBpvgzrGRbVKyX79rUkhWVV1etlmE2NfH9-NwfSrDD-Unw7gDpjk4b6xwYBqwohFW6BpX3sDeKOalErBnVrJKoZXhLqGFD0glai84SA2-FbAVWlgWDJuvwBxUQuptmExeYvhitAhRSJ7URjv_vWbOk3Q1x5HaE5qT9JbQ5UDoNQIJXQW884ioLz1gLIyz5UsgImZgqAU6azph1eFUTwybBjZY4w56zYXFyJqHUmpjLSLU4TmlJglOWuQLXGt6xbHkiYWAd3IAxgM_ztu-9hJLHukObG3QuDK7Sur5tDY-eGBKmSfBSbImyfvnd54cn7rrjpa9kRyaUCHWAqS4OZoBx2XMAknXF0PpDbwj6T-OceEXH4NcjP1PUV7txGF0-laT_jVPpFi_6KxX221Y5sztjsap2Ug65vHmIDQFskjmI3P2YYQrEQZ_MpZD5_GqZjdWdmOKA8nW4Xrlv5-xwr-R78NctUOk655lqQGJJBRuEijsRC8GHwSIQWeQImGDUrCT6s1CF1zVVgQ5i89Dtd53Dikg9AM-W8Mro3xs7BZXP_HX3ubDcLe8N-eHPq5W473__O1hBdyMF70x6igEUmOcndB-zHOSlcDyn2PdiTyvH-_jiJcpv06vWeSlV6I8J4RLVzPLTzrd2MADpsN7Oyo0hIYOcn9OxLic28kfOhH1VpUvcpK-7au4NjtcKLWf_q5QVR_xK-BSOtcLh5Mso0kRtSXPsybjtMjqnCYZbeo84wvOm7paJMsFKyLFKqFciU2BvRDJkiYUgck73M1SGldJUTUJemGMpnlFsc_EjkkVh8CBnciWYw5Vv3W4qaTz7rTJnJNbLcTkn_W-NbZ8xLJ3hysrmx9SR2PS5ZjxL8LZKWo">