Martin Ettl | 1 Jun 2009 13:31
Picon
Picon
Gravatar

gcc-performance::number of local variables

Hello,

i try to improve the performance of a cpp-program. I am insterested to understand how gcc optimizes the code
(switch on -02 or -O3 flag).

Suppose i have this simple function:

#Version 1
double vFoo( const double &a, const double &b, double &e )
{
  double s = a + b;
  double h = s - a;
         e =(s-(a-h))+(h-b);
  return s;
}

This function ca be rewritten to:

#Version 2
double vFoo( const double &a, const double &b, double &e )
{
  double s = a + b;
         e = s - a;
         e =(s-(a-e))+(e-b);
  return s;
}

A version 2 there is one local variable allocation less than at version 1.Is this step automatically done by
gcc, or can i improve the performance of code by doing this myself?

(Continue reading)

John (Eljay) Love-Jensen | 1 Jun 2009 14:03
Picon
Favicon

Re: gcc-performance::number of local variables

Hi Martin,

Since you did not specify, I presume your platform is Motorola 68060 running
Amiga OS 3.9 on Amiga 3000 with Phase5 CyberStorm, and you are using GCC
4.4.0.

Ultimately, you should profile your routines, and you should look at the
assembly for your platform, and you should have test code which exercises
your routines for a variety of inputs and expected outputs (both the return
value and the out parameter).

For the two const&, since they are scalar types you probably should
pass-by-value or pass-by-const-value.

I would write your routine this way, and then profile, run the test cases,
and (if performance critical, as your inquiry suggests) check the assembly.

double vFoo(
  double const a,
  double const b,
  double& e)
{
  double const s = a + b;
  double const h = s - a;
  e = (s - (a - h)) + (h - b);
  return s;
}

Note that since GCC uses the really, really cool SSA optimization, you
really do not gain much benefit from making the code obscure in order to
(Continue reading)

Clemens Eisserer | 1 Jun 2009 14:13
Picon

Re: gcc-performance::number of local variables

Hi,

> A version 2 there is one local variable allocation less than at version 1.Is this step automatically done
by gcc, or can i
> improve the performance of code by doing this myself?
Such simple optimizations are done automatically by gcc, no need at
all to change your code.

- Clemens

John Fine | 1 Jun 2009 15:39
Picon

Re: gcc-performance::number of local variables

For most simple optimizations, the compiler is good enough that you 
shouldn't try to do the work for it.

The big difference between your understanding of your code and the 
compiler's understanding is usually in aliasing.

In version 2, you probably know that e does not alias a or b.  But the 
compiler may not.  So the compiler may generate less efficient code.

In version 1, the compiler knows h does not alias a or b, so it can 
generate the best code.

Trying to avoid allocation of a local variable is almost always silly.  
In version 1, the compiler will generally figure out that h is a 
temporary value that can live entirely in a register and doesn't really 
need a local variable.

"const" for local variable (your version 3) should be used to help catch 
programming errors.  It is almost always useless as an optimization 
hint, so don't try to use it as an optimization hint.

Passing doubles by value vs. const& is a difficult question (when you're 
trying to push performance to the limit).  In an inline function it 
usually doesn't matter and if it matters const& is usually better.  But 
in a non inline function, it usually matters and is very hard to predict 
which will be better.  Aliasing issues may be a significant reason that 
value may be better than const&.  I think the aliasing issues can be 
managed with attributes, but I'm not certain.

If you cared about performance on a routine like this, making it inline 
(Continue reading)

Amker.Cheng | 2 Jun 2009 08:16
Picon

about multiple --with-sysroot

Hi All:
      In buildroot, sometime it will configure gcc with two
--with-sysroot options, I did not find any words about this case, does
this alright or it has any effects?
Thanks!
--

-- 
Best Regards.

Alex Luya | 2 Jun 2009 08:16
Favicon

Link error ....redefinition of......

I download source code for book <</Data Structures and Algorithm 
Analysis in C++ (Second Edition), /by Mark Allen Weiss>> 
from:http://users.cs.fiu.edu/~weiss/dsaa_c++/code/,try to compiler 
it,but got many errors,most of them say:
...... previously declared here
.......: redefinition of .....

I think template causes these errors,but how to fix it.
-----------------------------------------------------------------------
My configuration:
Ubuntu 9.04
GCC version 4.3.3 (Ubuntu 4.3.3-5ubuntu4)
Eclipse 3.4
CDT:.5.0.2
-------------------------------------------------------------------------

Files  and error message are following:

StackAr.h
-----
        #ifndef STACKAR_H
        #define STACKAR_H

        #include "../vector.h"
        #include "../dsexceptions.h"

        template <class Object>
        class Stack
        {
          public:
(Continue reading)

QiangHuang | 2 Jun 2009 09:10
Picon

code instrumentation in AST

     Hi, Everyone~

     I need to do some research in the area of software testing with
the help of GCC.Especially I intend to instrument some assert code in
the AST of GCC.

     Take the following code as an example:

     int main()
     {
         char str[10], *buf;
         scanf("%s", buf);
         strcpy(str, buf);
         return 0;
     }

     after instrumentation finished, it looks like this:

     int main()
     {
         char str[10], *buf;
         scanf("%s", buf);
         assert(strlen(buf)<10);       // Instrumentatiom to make sure
that the length of the later one is less than the former one.
         strcpy(str, buf);
         return 0;
     }

     I already had a good understand on the structures of the gcc
AST.Instrumentation could be done by manually inserting the AST nodes
(Continue reading)

Kalle Olavi Niemitalo | 2 Jun 2009 09:36
Picon
Picon
Favicon

Re: Link error ....redefinition of......

Alex Luya <alex_luya <at> yeah.net> writes:

> I download source code for book <</Data Structures and Algorithm
> Analysis in C++ (Second Edition), /by Mark Allen Weiss>>
> from:http://users.cs.fiu.edu/~weiss/dsaa_c++/code/,try to compiler
> it,but got many errors,most of them say:
> ...... previously declared here
> .......: redefinition of .....

These errors occur also with g++ -c, which does not run the
linker; therefore they are compile errors, rather than link
errors.

First, download the ANSI C++ versions of the source files
from <http://users.cs.fiu.edu/~weiss/dsaa_c++/code/ansi/>.
The original files are for GCC 2.8.1 and have problems
with GCC 4.3.3.

Then, do not compile StackAr.cpp separately.  That won't work.
StackAr.cpp begins with #include "StackAr.h", and there is an
#include "StackAr.cpp" near the end of StackAr.h, so you get a
loop (although not an infinite loop):

StackAr.cpp: #include "StackAr.h"
  StackAr.h: #ifndef STACKAR_H  /* not yet defined */
  StackAr.h: #define STACKAR_H
  StackAr.h: template <class Object> class Stack { ... };
  StackAr.h: #include "StackAr.cpp"
    StackAr.cpp: #include "StackAr.h"
      StackAr.h: #ifndef STACKAR_H /* now it's already defined */
(Continue reading)

Vijay Holimath | 2 Jun 2009 15:02
Picon
Favicon

Enquiry

Dear Sir,

         I am using gcc compiler for v850e cpu. When I use the 
arrtribute: __attribute__ ((interrupt_handler)) or __attribute__ 
((interrupt); for interrupt function,  say for example

void swnmi() __attribute__ ((interrupt_handler));

void swnmi()
{
...
..
}

main()
{
..
..
}

I am getting following error messages when I compile:

main.o (.text+0xea): In function 'swnmi': undefined reference to '__ep'

main.o (.text+0xee): In function 'swnmi': undefined reference to '__ep'

collect2: Id returned 1 exit status

I will be grateful to you if you could help me to get rid of these error 
messages.  Probably I have to link some libraries?
(Continue reading)

Ian Lance Taylor | 2 Jun 2009 15:26
Picon
Favicon
Gravatar

Re: Enquiry

Vijay Holimath <vijay <at> nii.ac.jp> writes:

>         I am using gcc compiler for v850e cpu. When I use the
> arrtribute: __attribute__ ((interrupt_handler)) or __attribute__
> ((interrupt); for interrupt function,  say for example

...

> I am getting following error messages when I compile:
>
> main.o (.text+0xea): In function 'swnmi': undefined reference to '__ep'
>
> main.o (.text+0xee): In function 'swnmi': undefined reference to '__ep'

It looks like the compiler expects the linker to define __ep.  Make sure
you have an up to date version of the GNU binutils.

Ian


Gmane