This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
dead code remover under gcc -O and higher(Is there a flag to do this)
- From: "chengjian (D)" <cj dot chengjian at huawei dot com>
- To: <gcc at gcc dot gnu dot org>, Li Bin <huawei dot libin at huawei dot com>, "Xiexiuqi (Xie XiuQi)" <xiexiuqi at huawei dot com>
- Date: Mon, 4 Dec 2017 08:56:05 +0800
- Subject: dead code remover under gcc -O and higher(Is there a flag to do this)
- Authentication-results: sourceware.org; auth=none
I have written a simple code like this
```c
#include <stdlib.h>
#include <stdio.h>
//#define CONFIG_TARGET_X86_64
#ifdef CONFIG_TARGET_X86_64
static void A( )
{
printf("A\n");
}
#else
void A( );
#endif
static void B( )
{
printf("B\n");
}
static int xx( )
{
#ifdef CONFIG_TARGET_X86_64
return 1;
#else
return 0;
#endif
}
int main(void)
{
if (xx( )) /* define CONFIG_TARGET_X86_64 */
A( );
else
B( );
}
```
If we don't define the CONFIG_TARGET_X86_64, xx( ) will always return
FALSE, so functiopn A which is only declared, but not implemented will
never be called(dead code).
compile it with gcc -O0
```cpp
/tmp/cctSpgGk.o: In function `main':
1.c:(.text+0x34): undefined reference to `A'
collect2: error: ld returned 1 exit status
```
But it can be compiled by -O1 or higher.
use GCC V6.1.0.
I can declare A as weak:
```cpp
void A (void) __attribute__ ((weak));
```
Then the linker will ignore the undefined symbol reference, but a call
to this function will lead to a crash
So my question is :
It seems that one of the optimization options in the -O1 option
eliminates the dead code, I have seen the optimize doccument about GCC
https://gcc.gnu.org/onlinedocs/gcc-6.4.0/gcc/Optimize-Options.html
but I can't find it.
So if I just want to compile this code under the -O0 option, Is it
possible ? Are there some optimization flags help me to do this?
Thanks.