This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: A Method



Right... try:

#include <stdio.h>
int main()
{
  int a=1, b=2, c=3, d=4;
  int x = c+a+d;
  int y = b+a;
  int z = a+c+d+b;

  printf("%u %u %u",x,y,z);
}

Run gcc -S on that, it will indeed do lots of pointless addition.

run gcc -S -O1, and you get something like:

main:
        pushl %ebp
        movl %esp,%ebp
        pushl $10
        pushl $3
        pushl $8
        pushl $.LC0
        call printf

(x86 Linux). Even the most basic optimiser knows that x,y and z are
compile-time constants in the context of the printf statement.

In a case like:

int foo(int a, int b, int c, int d)
{
  int x = c+a+d;
  int y = b+a;
  int z = a+c+d+b;
}

...where foo isn't inlined, the existing CSE (common subexpression
elimination) procedure will notice the common terms and produces only
four additions, as one might hope:

foo:
        pushl %ebp
        movl %esp,%ebp
        pushl %edi
        pushl %esi
        pushl %ebx
        movl 8(%ebp),%eax
        movl 12(%ebp),%edi
        movl 16(%ebp),%ecx
        movl 20(%ebp),%ebx
        leal (%eax,%ecx),%edx
        addl %ebx,%edx
        leal (%eax,%edi),%esi
        addl %ecx,%eax
        addl %ebx,%eax
        addl %edi,%eax


What is this proposed method capable of doing that is not already done?

Regards,

        -Ben-

[ Ben Jones | bjj97@doc.ic.ac.uk | UG/ISE4 | Imperial College, London ]



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]