Bug 21690 - gcc misses trivial optimizations
Summary: gcc misses trivial optimizations
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 4.0.0
: P2 enhancement
Target Milestone: 4.3.0
Assignee: Not yet assigned to anyone
URL:
Keywords: missed-optimization
Depends on: 15459
Blocks:
  Show dependency treegraph
 
Reported: 2005-05-21 00:23 UTC by Benoît Dejean
Modified: 2008-09-09 01:49 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2005-12-24 20:18:32


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Benoît Dejean 2005-05-21 00:23:47 UTC
hi, i'm using Debian SID experimental gcc-4.0

gcc-4.0 -v
Using built-in specs.
Target: powerpc-linux
Configured with: ../src/configure -v
--enable-languages=c,c++,java,f95,objc,ada,treelang --prefix=/usr
--enable-shared --with-system-zlib --libexecdir=/usr/lib --enable-nls
--without-included-gettext --enable-threads=posix --program-suffix=-4.0
--enable-__cxa_atexit --enable-libstdcxx-allocator=mt --enable-clocale=gnu
--enable-libstdcxx-debug --enable-java-gc=boehm --enable-java-awt=gtk
--enable-mpfr --disable-multilib --disable-werror --enable-checking=release
powerpc-linux
Thread model: posix
gcc version 4.0.1 20050508 (prerelease) (Debian 4.0.0-6)



i'm compiling the following simple code :

BEGIN_CODE

void
foo(void*);

static inline char*
bar(char * const p)
{
	foo(p);
	return p;
}

unsigned long
baz(char * const p)
{
	return (unsigned long)(bar(p+1) - bar(p));
}

END_CODE

it's obvious that baz(...) == 1. But gcc outputs the following (gcc -O3 -S -Wall
foo.c)

BEGIN_CODE

baz:
        stwu 1,-32(1)
        mflr 0
        stw 28,16(1)
        addi 28,3,1
        stw 29,20(1)
        mr 29,3
        mr 3,28
        stw 0,36(1)
        bl foo
        mr 3,29
        bl foo
        lwz 0,36(1)
        subf 3,29,28
        lwz 28,16(1)
        lwz 29,20(1)
        mtlr 0
        addi 1,1,32
        blr

END_CODE

if i remove the call foo(p) in function bar, gcc outputs

BEGIN_CODE

baz:
        li 3,1
        blr
        
END_CODE


I would have expect gcc to output the same trivial code in both cases.

Now,

addi 28,3,1
...
mr 29,3
mr 3,28
...
mr 3,29
...
subf 3,29,28

is a complex way to get 1.

Why gcc is not able to optimize this kind of code ?
gcc-2.95, gcc-3.3 and gcc-4.0 output the same result.

Thank you.
Comment 1 Andrew Pinski 2005-05-21 00:32:08 UTC
This bolls down to combine not working across function calls.  The correct way to fix this would to have 
a tree combiner.
Comment 2 Andrew Pinski 2008-09-09 01:49:26 UTC
This was fixed for 4.3.0.