<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/61849>61849</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Incorrect Optimization On Pointer Arithmetic
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
dkw72n
</td>
</tr>
</table>
<pre>
reproduce code
```cpp
class Ptr {
int* _r;
inline void m_set(int* p){
_r = p + 1;
}
inline int* m_get(){
return _r - 1;
}
public:
Ptr(int* p) { this->m_set(p); };
inline operator bool() {
return !!m_get();
}
};
int main()
{
Ptr p((int*)0);
while(p);
return 0;
}
```
when compile with `-O0` or `-O1`, the output returns 0 as expected.
when compile with `-O2` or higher, the output is an infinite loop;
this issue first appeared at clang 3.3, and affects all archs (those i can test with godbolt), godbolt link below:
https://godbolt.org/z/59j8GT1KG
gcc also share the same issue since gcc 9, FYI.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJx8lM1u4zgMgJ9GvhAJZNn58cGHZroZDPbQHvayp0KWGZszsiRI9GR3nn7hn7RptxggiEGZ_PhRMqRTos4h1mJ3ErvHTI_c-1i3P64H5bLGt__WEUP07WgQjG9RyEchH8ReLj8TwrJirE4JnjmCOJyWJSDHQj3ASxTFbQnIWXIIPz21MLwkZKGOa14QqnorBoCXCKJ4hABCnSC_g4jD42uLGbcShpduBn4EReQxuom3-ZwTxsaSEcXD7c0zx_di01jAPaWNKP64ic_GxWnm_G9EHzBq9hEa7-0iBZ9ZCZULld-rfyJ432H5J8cwaHJrzZp1uhtg8j6-TiFUJd_Brz1ZfBviVrhaybd2rw63Q1_Ca48OjB8CWYQrcQ9iLzdPUwL4uAT5lK2-APcIfuQw8spPIEEnwH8CGsZ2-1ukWpE9dT3GDzxKoB2Qu5AjRrDehw87NZ0aUEojwoViYtAhoI7YgmYwVrsOim0xYbVrQV8uaDiBthZ0NH0CoY7c-4RAYLQDxsSLXOfbxlue9k99uUVgyf2ABq2_vn5PPXNIU6TOQp3XxK2PnVDnX0Kdd9X349e_8j-_3mt3xoC2yUPqdcR55KQHXCdJ5AzClDM3P__9bZu1ddFWRaUzrPP9UeZVXpRl1tdVo0q5Q40Kda5UtbscD0ezq4w55M2-yTOqlVSFLAqZ78pKllt90KrUB32RZb5vmosoJQ6a7Nban8Mkns0W9T4_llVmdYM2zVeIUg6vi6JQarpRYj3VbJqxS6KUlhKnNwoTW6y_OeNjRMPwFJgG-qWZvIMnB8-eHGOEh0jcD8hksjHa-sN-EvdjszV-EOo8odfHJkT_HQ0LdZ6FklDnWfi_AAAA___qS3RY">