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]
Other format: [Raw text]

ix86 optimizer vagaries?



I am confused: why GCC 3.3.2 generates code for


unsigned
f1(unsigned n) {
  return 2*(n + 1);
}

that is significantly better than the one generated for

unsigned
f2(unsigned n) {
  return 2*(++n);
}

which, however, in the attached program runs significantly
faster than the code obtained by using

unsigned
f3(unsigned n) {
  unsigned m = n;
  ++m;
  m *= 2;
  return m;
}

?

See by yourself by doing:

g++ -DF=f1 -o barf1 -O3 -fomit-frame-pointer borf.cc barf.cc
g++ -DF=f2 -o barf2 -O3 -fomit-frame-pointer borf.cc barf.cc
g++ -DF=f3 -o barf3 -O3 -fomit-frame-pointer borf.cc barf.cc
for k in barf[123]
do
  echo time $k
  time $k
done

borf.cc and barf.cc are under the signature.
All the best,

Roberto

P.S. My machine is equipped with an Athlon, but adding -march=athlon
     to the command line does not make a significant difference.

--
Prof. Roberto Bagnara
Computer Science Group
Department of Mathematics, University of Parma, Italy
http://www.cs.unipr.it/~bagnara/
mailto:bagnara@cs.unipr.it

// borf.cc

#include <iostream>

using namespace std;

// Cost: 1.00
unsigned
f1(unsigned n) {
  return 2*(n + 1);
}

// Cost: 1.16
unsigned
f2(unsigned n) {
  return 2*(++n);
}

// Cost: 1.46
unsigned
f3(unsigned n) {
  unsigned m = n;
  ++m;
  m *= 2;
  return m;
}

extern unsigned g(unsigned n);

int main() {
  unsigned a = 0;
  for (unsigned n = 1000000000; n > 0; --n)
    a += g(F(n));
  cout << a << endl;
}


// barf.cc


unsigned g(unsigned n) {
  return n;
}


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