* dead code remover under gcc -O and higher(Is there a flag to do this)
@ 2017-12-04 0:56 chengjian (D)
2017-12-04 2:26 ` carl hansen
2017-12-04 8:21 ` Jakub Jelinek
0 siblings, 2 replies; 4+ messages in thread
From: chengjian (D) @ 2017-12-04 0:56 UTC (permalink / raw)
To: gcc, Li Bin, Xiexiuqi (Xie XiuQi)
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.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: dead code remover under gcc -O and higher(Is there a flag to do this)
2017-12-04 0:56 dead code remover under gcc -O and higher(Is there a flag to do this) chengjian (D)
@ 2017-12-04 2:26 ` carl hansen
2017-12-04 2:51 ` chengjian (D)
2017-12-04 8:21 ` Jakub Jelinek
1 sibling, 1 reply; 4+ messages in thread
From: carl hansen @ 2017-12-04 2:26 UTC (permalink / raw)
To: chengjian (D); +Cc: GCC Development, Li Bin, Xiexiuqi (Xie XiuQi)
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.
>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: dead code remover under gcc -O and higher(Is there a flag to do this)
2017-12-04 2:26 ` carl hansen
@ 2017-12-04 2:51 ` chengjian (D)
0 siblings, 0 replies; 4+ messages in thread
From: chengjian (D) @ 2017-12-04 2:51 UTC (permalink / raw)
To: carl hansen; +Cc: GCC Development, Li Bin, Xiexiuqi (Xie XiuQi)
On 2017/12/4 10:25, carl hansen wrote:
> line 12 , you say "void A( );"
>
> say instead:
> void A(){};
>
> That solved it for me, using gcc7.2
>
>
Yeah, I miss the implementation, but I did this on purpose
I'm curious is there any options to eliminate this.
like gcc -O2 do.
thanks.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: dead code remover under gcc -O and higher(Is there a flag to do this)
2017-12-04 0:56 dead code remover under gcc -O and higher(Is there a flag to do this) chengjian (D)
2017-12-04 2:26 ` carl hansen
@ 2017-12-04 8:21 ` Jakub Jelinek
1 sibling, 0 replies; 4+ messages in thread
From: Jakub Jelinek @ 2017-12-04 8:21 UTC (permalink / raw)
To: chengjian (D); +Cc: gcc, Li Bin, Xiexiuqi (Xie XiuQi)
On Mon, Dec 04, 2017 at 08:56:05AM +0800, chengjian (D) wrote:
> 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.
-ftree-dce, -fdce (but actually many other passes do that, if some condition
is folded to a constant, any cfg cleanup will remove the dead code).
> 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?
No. The thing is, -O0 vs. -Og vs. -O1+/-Os are using different sets of
passes, and various passes have gates flag_foo_bar_baz && optimize or
similar, so even if you enable -ffoo-bar-baz, at -O0 it will not do
anything. In your testcase, you need either inlining (which is at -O0 done
only for __attribute__((always_inline)) functions), or some IPA analysis on
the return value range (I think that is still unimplemented), but even if it
would be, it certainly wouldn't be done at -O0.
So, make it always_inline and it might happen to "work" at -O0, otherwise,
no, don't try that at -O0.
Jakub
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2017-12-04 8:21 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-12-04 0:56 dead code remover under gcc -O and higher(Is there a flag to do this) chengjian (D)
2017-12-04 2:26 ` carl hansen
2017-12-04 2:51 ` chengjian (D)
2017-12-04 8:21 ` Jakub Jelinek
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).