This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: dead code remover under gcc -O and higher(Is there a flag to do this)
- From: carl hansen <carlhansen1234 at gmail dot com>
- To: "chengjian (D)" <cj dot chengjian at huawei dot com>
- Cc: GCC Development <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: Sun, 3 Dec 2017 18:25:37 -0800
- Subject: Re: dead code remover under gcc -O and higher(Is there a flag to do this)
- Authentication-results: sourceware.org; auth=none
- References: <abf9d156-a3db-58b8-26d2-d4c4159b1481@huawei.com>
line 12 , you say "void A( );"
say instead:
void A(){};
That solved it for me, using gcc7.2
On Sun, Dec 3, 2017 at 4:56 PM, chengjian (D) <cj.chengjian@huawei.com> wrote:
> 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.
>