<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/124540>124540</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[x86-64] The '-fdelete-null-pointer-checks' pass not removed redundant code
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
IshwaraK
</td>
</tr>
</table>
<pre>
The clang compiler is not optimizing away pointer check in the foo() function in this code. The gcc removes that part of code as dead code after '-fdelete-null-pointer-checks' pass. We can observe these behaviors in platforms like X86, ARM.
The clang assembly code in x86-64; https://godbolt.org/z/nncroxMx4
The gcc assembly code in x86-64; https://godbolt.org/z/YbqPYGon4
```
#include <stdio.h>
#include <string.h>
char dest[50];
void foo(char* dest, char* src) {
memcpy(dest, src, strlen(src));
if (!src) { // <------- This is redundant check as pointer is already de-referenced
printf("checking -fdelete-null-pointer-checks compiler optimization\n");
printf("This source code is redundant\n");
__builtin_abort ();
}
printf("After check for null pointer\n");
}
int main ()
{
char src[50] = "-fdelete-null-pointer-checks";
foo(dest, src);
return 0;
}
```
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJyMVMGO4zYM_Rr6QiRwZDt2Dj5kkqYoigWKYoF2TwNZomN1ZcmV5Mxkv76Q7Uwy22K6hgBDIPn4KD6Se6_OhqiG4gmKY8LH0FlX_-K7F-74r0lj5bX-3BEKzc0Zhe0Hpcmh8mhsQDsE1atvypyRv_ArDlaZQA5FR-IrKoOhI2ytBVYB22E7GhGUNbNFeRRW0hoj_lkIdNTbC3kMHQ84cBfQtpMLco-SuFwubUwBrFy1kjQFWplR69WSezXl9sBKHLj3a_yDUHCDtvHkLhQZecKGOn5R1vlIZdA8tNb1HrX6SvhntQV2wP3vn9aQ7iHd3-vn3lPf6OtMJMa-VtvVNofsCbELYfCQ7YGdgJ3OVjZWh7V1Z2Cnb8BOxghnXz-95niHjWW_B32H-X-QX5q_f_vyszX5jAjbdDnpHlimjNCjJITs4INUdt1B9tN_mJwy5zdbuhcddyjJByieihSKI2RPs-VilVzaGZ2A7Wc_dsDb3TsROw1lDEHsqRfDFVh185vsB_TBaTLAqtk_nmyOUC1OatnckXAuP5JdzR9-jupRHh3J0UhuwiI57t80qDxy7YjLK0paOWrJkREkY5bBKRPaKQ-bAqOCP5LTXfmL5HnUMRQHA4zd2T_iThS9HZ2gpbMPdP8VGb_n52ZUOijzzBvrwvwONwcoj5PfY4p9ex-21jqMxG_1f59hjod0r0zAnitzg4-mKcPU9_jqS9sRsiMCYx-PGXsrYBbGu0bfyDsKozOYvqPyINZE1pncZTueUL0ps3JX5cWuSrq6TcVGMLmt2rSt0kzkZbYtUsZLkTHR5EWiapayIt2wcpOxXVGtt1xWm3JHW1HsNukuhTylniu91vrSx9FJlPcj1RuWF3maaN6Q9tP6Y8zQC07WWFZxTFwdg1bNePaQp1r54O8wQQU97c1lXIvjtMh-cC9N63NeePJRxVZSMjpdfzf5KnRjsxa2B3aKDJbfanD2LxIB2Gni7YGdlsIuNfsnAAD__2lX06g">