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

[Bug c++/13566] New: bad code with -O3 -fno-strict-aliasing


I've run into a case where bad code is being generated on ia86 when
i use -O2 -fno-strict-aliasing.

The test case is below; i apologize that i haven't been able to reduce
it much further.  I expect that this program will print `2' when
executed, and it does in fact do so when compiled without
optimization, or without -fno-strict-aliasing:

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

However, when -O2 is combined with -fno-strict-aliasing, it prints `1':

$ g++ -O2 -fno-strict-aliasing -o x x.cc
$ ./x
1
$


Here is the generated code for the add_bool_list() function, when
compiled with -O2 -fno-strict-aliasing:


-------------------------------------
.globl _Z13add_bool_listv
	.type	_Z13add_bool_listv, @function
_Z13add_bool_listv:
.LFB26:
.L6:
.L18:
.L23:
.L26:
	pushl	%ebp
.LCFI0:
	xorl	%eax, %eax
	movl	%esp, %ebp
.LCFI1:
	subl	$200, %esp
.LCFI2:
	movl	%eax, -188(%ebp)
	xorl	%eax, %eax
	xorl	%edx, %edx
	movl	%eax, -168(%ebp)
	xorl	%eax, %eax
	xorl	%ecx, %ecx
	movl	%eax, -164(%ebp)
	xorl	%eax, %eax
	movl	%eax, -152(%ebp)
	xorl	%eax, %eax
	movl	%edx, -132(%ebp)
	xorl	%edx, %edx
	testl	%edx, %edx
	movl	%eax, -148(%ebp)
	leal	-192(%ebp), %eax
	movl	$0, -72(%ebp)
	movl	$0, -68(%ebp)
	movl	$0, -56(%ebp)
	movl	$0, -52(%ebp)
	movl	$0, -40(%ebp)
	movl	$0, -36(%ebp)
	movl	$0, -24(%ebp)
	movl	$0, -20(%ebp)
	movl	%ecx, -192(%ebp)
	movl	%eax, -136(%ebp)
	movl	%eax, -104(%ebp)
	movl	$0, -100(%ebp)
	jle	.L36
.L45:
	orl	$1, (%eax)   ; This can't be right!
.L37:
	incl	-100(%ebp)
	incl	%edx
	cmpl	$1, %edx
	jle	.L43
	movl	-192(%ebp), %eax
	leave
	ret
	.p2align 4,,7
.L43:
	testl	%edx, %edx
	movl	-104(%ebp), %eax
	jg	.L45
.L36:
	andl	$-2, (%eax)
	jmp	.L37
-------------------------------------

[Besides the codegen problem being reported here, it looks like there is
useless code which the compiler is not removing.]

Environment:
System: Linux karma 2.6.0 #9 Sun Dec 28 19:12:01 EST 2003 i686 i686 i386 GNU/Linux
Architecture: i686

	<machine, os, target, libraries (multiple lines)>
host: i686-pc-linux-gnu
build: i686-pc-linux-gnu
target: i686-pc-linux-gnu
configured with: ../gcc/configure --prefix=/usr/local/gcc --enable-threads=posix --enable-long-long --enable-languages=c,c++,f77

How-To-Repeat:

Compile the below with -O2 -fno-strict-aliasing.

----------------------------------------------------------

extern "C" int printf (const char*, ...);
extern "C" void abort();


struct xbasic_string
{
  ~xbasic_string() {}
};


class foo{};
struct _Bit_iterator_base : public foo
{
  unsigned long * _M_p;
  unsigned int _M_offset;
};
struct Bit_iterator : public _Bit_iterator_base { };


inline
Bit_iterator
copy (Bit_iterator result)
{
  for (int n = 0; n < 2; ++n) 
  {
    if (n>0)
      *result._M_p |= (1<<result._M_offset);
    else
      *result._M_p &= ~(1<<result._M_offset);
    result._M_offset++;
  }
  return result;
}


inline
Bit_iterator
copya (Bit_iterator result=Bit_iterator())
{
  return result;
}


struct BoolList
{
  Bit_iterator _M_start;

  BoolList() { }

  BoolList(const BoolList&) {
    unsigned long bb = 0;
    this->_M_start._M_p = &bb;
    copya();
  }
};


static
int foofoo(int,
           Bit_iterator first = Bit_iterator(),
           Bit_iterator last = Bit_iterator(),
           Bit_iterator a = Bit_iterator(),
           Bit_iterator b = Bit_iterator(),
           Bit_iterator c = Bit_iterator(),
           Bit_iterator d = Bit_iterator()
           )
{
  unsigned long bb = 0;
  Bit_iterator i;
  i._M_p = &bb;
  i._M_offset = 0;
  copy(i);
  return bb;
}


struct bvalue_type {
  xbasic_string first;
  BoolList second;
  bvalue_type(const BoolList& b=BoolList()) : second(b) {}
};


struct biterator
{
  biterator() {}
  biterator(const biterator& __it)  {}
};

inline bool xxx (biterator = biterator()) { return true; }


inline
biterator
insertfoo(biterator position=biterator(),
          const xbasic_string& s = xbasic_string())
{ return biterator(); }

inline
biterator
insertbar(biterator position=biterator(),
          const bvalue_type& __x=bvalue_type())
{  return biterator (); }

int add_bool_list()
{
  {
    biterator i;
    i = insertfoo();
    if (xxx ())
      insertbar();
  }
  return foofoo(1);
}

int main( )
{
  int b = add_bool_list();
  printf ("%d\n", b);
}

----------------------------------------------------------
------- Additional Comments From snyder at fnal dot gov  2004-01-05 03:25 -------
Fix:
	<how to correct or work around the problem, if known (multiple lines)>

-- 
           Summary: bad code with -O3 -fno-strict-aliasing
           Product: gcc
           Version: 3.4
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: snyder at fnal dot gov
                CC: gcc-bugs at gcc dot gnu dot org
 GCC build triplet: i686-pc-linux-gnu
  GCC host triplet: i686-pc-linux-gnu
GCC target triplet: i686-pc-linux-gnu


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


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