This is the mail archive of the gcc-bugs@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]

bad code generated with optimization on (inlining problem?)



hi -

I just ran into another problem with the gcc optimizer producing incorrect
code.  This is with the cvs version of gcc (2.96 20000222 (experimental)),
on a i686-pc-linux-gnu platform.

The following input shows the problem when compiled with the
c++ compiler with -O2 (this is distilled from code in libstdc++ v3).
[Nb, in this version of the compiler, there was another, trivial,
problem in init_output_buffer that caused an ICE.  But that's already
been fixed in cvs.]

- x.cc ---------------------------------------------------
extern "C" int printf (...);
void bar ()
{
  printf ("asd\n");
}


struct Iter
{
  int _M_node;
  void advance () { _M_node = 0; }
};


inline int __copy(Iter first)
{
  first.advance();
  return first._M_node;
}


void foo (Iter x)
{
  if (__copy (x)) bar ();
}


int main () 
{
  Iter first = {123};
  foo (first);


  return 0;
}
----------------------------------------------------------

I expect this program to produce no output.
And that is indeed the case if it is compiled with optimizaton off.
But with optimization on, it does produce output:

$ g++ -o x x.cc
$ ./x
$ g++ -O2 -o x x.cc
$ ./x
asd
$

Here's the generated assembly code for foo().
Note that the test instruction is testing the wrong quantity:


foo__FG4Iter:
.LFB2:
	pushl	%ebp
.LCFI4:
	movl	%esp, %ebp
.LCFI5:
	subl	$8, %esp
.LCFI6:
	movl	8(%ebp), %eax
	movl	$0, -4(%ebp)
	testl	%eax, %eax
	je	.L13
	call	bar__Fv
.L13:
	movl	%ebp, %esp
	popl	%ebp
	ret
.LFE2:
.Lfe2:


Looking at the RTL dumps, i see that the problem is already present
in the first one, 00.rtl.  Thus, my guess is that the culprit is probably
the function inlining processing.  I have not, however, verified that.

thanks.
sss

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