[Bug c++/14703] New: Inadequate optimization of inline templated functions

eric-gcc at omnifarious dot org gcc-bugzilla@gcc.gnu.org
Wed Mar 24 02:25:00 GMT 2004


This code:

=====================
#include <iostream>

namespace {
template <unsigned long long L> class fib {
 public:
   static const unsigned long long value = fib<L - 1>::value + fib<L - 2>::value;
};

template <> class fib<0> {
 public:
   static const unsigned long long value = 1;
};

template <> class fib<1> {
 public:
   static const unsigned long long value = 1;
};

template<unsigned long long L> inline unsigned long long fibconst()
{
   return fibconst<L - 1>() + fibconst<L - 2>();
}

template <> inline unsigned long long fibconst<0>()
{
   return 1ull;
}

template <> inline unsigned long long fibconst<1>()
{
   return 1ull;
}

template <> inline unsigned long long fibconst<2>()
{
   return 2ull;
}

}

int main()
{
   ::std::cerr << "fib<90>::value == " << fib<90>::value << "\n";
   ::std::cerr << "fibconst<90>() == " << fibconst<90>() << "\n";
}
=====================

does not result in the call to fibconst being optimized out of existence.  In
fact, only calls to fibconst<11>() and lower are optimized out at -O3.  The
compiler should have plenty enough information to realize that they can all be
optimized out.  After all, in order to expand the template at all, it has to get
down to fibconst<0> or fibconst<1> or fibconst<2> and build things back up again.

This is against gcc 3.4 pre-release.  I certainly don't expect anything to be
done to gcc 3.4 about it, but I think it should still be taken care of someday.

-- 
           Summary: Inadequate optimization of inline templated functions
           Product: gcc
           Version: 3.4.0
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P2
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: eric-gcc at omnifarious dot org
                CC: gcc-bugs at gcc dot gnu dot org
 GCC build triplet: pentium4-redhat-linux
  GCC host triplet: pentium4-redhat-linux
GCC target triplet: pentium4-redhat-linux


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14703



More information about the Gcc-bugs mailing list