This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: constant folding with constant function pointers in GCC 4.1.x
- From: Brian Dessent <brian at dessent dot net>
- To: "Frankel, Bob" <bios-bob at ti dot com>
- Cc: gcc-help at gcc dot gnu dot org
- Date: Thu, 05 Apr 2007 18:40:55 -0700
- Subject: Re: constant folding with constant function pointers in GCC 4.1.x
- References: <5E59B6BEADA68F4298DABA81E2849E57813F09@dlee13.ent.ti.com>
- Reply-to: gcc-help at gcc dot gnu dot org
"Frankel, Bob" wrote:
> volatile int OUT;
>
> void foo()
> {
> OUT = 100;
> }
>
> void (*const FOOPTR)() = foo;
>
> int main()
> {
> FOOPTR(); /* remains an indirect call to foo */
> return 0;
> }
>
> i would have expected the indirect call to fold away into an assignment
> of 100 to OUT, as in the other examples. i have seen this optimization
> occur on other compilers supporting whole-program optimization, and was
> wondering if there is something else i need to add to either my source
> file and/or my makefile (where i am already passing -O3 and
> -fwhole-program)
With 4.3 (not sure it matters) and a little modification to get rid of
volatile I was able to get it to optimize away the call through the
function pointer, resulting in just a regular function call, which I
would have thought would have been inlined under other circumstances,
but no matter what I tried it wouldn't inline it:
int foo(int i)
{
return i + 42;
}
int (*FOOPTR)(int) = foo;
int main(int argc, char **argv)
{
return FOOPTR(argc);
}
With -O3 -fwhole-program -fomit-frame-pointer, I get:
.text
.p2align 4,,15
.def _foo; .scl 3; .type 32; .endef
_foo:
movl 4(%esp), %eax # i, tmp60
addl $42, %eax #, tmp60
ret
.def ___main; .scl 2; .type 32; .endef
.p2align 4,,15
.globl _main
.def _main; .scl 2; .type 32; .endef
_main:
leal 4(%esp), %ecx #,
andl $-16, %esp #,
pushl -4(%ecx) #
subl $12, %esp #,
movl %ecx, 4(%esp) #,
movl %ebx, 8(%esp) #,
movl (%ecx), %ebx # argc, argc
call ___main #
movl %ebx, (%esp) # argc,
call _foo #
movl 4(%esp), %ecx #,
movl 8(%esp), %ebx #,
addl $12, %esp #,
leal -4(%ecx), %esp #,
ret
It doesn't seem to matter whether FOOPTR is const or static. I also got
an ICE with -O2 -fwhole-program -fipa-pta:
out4.c: In function 'main':
out4.c:11: error: stmt (0x7fe086a0) marked modified after optimization
pass:
FOOPTR.0_1 = FOOPTR;
out4.c:11: internal compiler error: verify_ssa failed
Please submit a full bug report,
with preprocessed source if appropriate.
See <URL:http://gcc.gnu.org/bugs.html> for instructions.
This is with mainline from about two months ago, I need to rebuild it to
see if it's still happens.
Brian