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: Optimization and template parms



>>>>> "Jeffrey" == Jeffrey A Law <law@cygnus.com> writes:

    Jeffrey>   In message
    Jeffrey> <Pine.GSO.3.92.980202142024.10958D-100000@johnson.cs.nyu.edu>you
    Jeffrey> w rite:
    >> Hi,
    >> 
    >> In the following method of a templated class:
    >> 
    >> template <bool c> Foo<c>::f1() { if (c) {cout << "true" <<
    >> endl;} else {cout << "false" << endl;} }
    >> 
    >> Would I be correct to assume that after instantiating the
    >> template, egcs wouldn't emit any code for the if/else test
    >> because the test always succeeds for Foo<true> and fails for
    >> Foo<false>?
    Jeffrey> That would be my expectation.  But I don't know enough
    Jeffrey> about C++ to know how it will look in RTL when the
    Jeffrey> optimizers get their first crack at removing the
    Jeffrey> unreachable code.

It's easy enough to do the test.  Here's code with -O2 -S on i686:

  f1__t3Foo1b0:
  .LFB2:
	  pushl %ebp
  .LCFI5:
	  movl %esp,%ebp
  .LCFI6:
	  pushl $.LC1
  .LCFI7:
	  pushl $cout
  .LCFI8:
	  call __ls__7ostreamPCc
	  addl $8,%esp
  .LCFI9:
	  pushl %eax
  .LCFI10:
	  call endl__FR7ostream
	  movl %ebp,%esp
	  popl %ebp
	  ret

So, the answer is yes.  The shorter answer is that, essentially, g++
generates RTL for template functions exactly as if all template
parameters had been plugged in.  So, the same optimizations get done
as if the template parameters were constants in non-template functions.

    Jeffrey> jeff

-- 
Mark Mitchell		mmitchell@usa.net
Stanford University	http://www.stanford.edu



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