public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/34459]  New: incorrect code when compiled with optimization (-O1)
@ 2007-12-14  6:29 noblige at gmail dot com
  2007-12-14 13:32 ` [Bug c++/34459] [4.1/4.3 Regression] " rguenth at gcc dot gnu dot org
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: noblige at gmail dot com @ 2007-12-14  6:29 UTC (permalink / raw)
  To: gcc-bugs

The G++ produces invalid code for snippet bellow when compiled with -O1.

Using built-in specs.
Target: i486-linux
Configured with: ../src/configure -v --enable-languages=c,c++ --prefix=/usr
--enable-shared --with-system-zlib --libexecdir=/usr/lib
--without-included-gettext --enable-threads=posix --enable-nls
--program-suffix=-4.1 --enable-__cxa_atexit --enable-clocale=gnu
--enable-libstdcxx-debug --enable-checking=release i486-linux
Thread model: posix
gcc version 4.1.2 20060613 (prerelease) (Debian 4.1.1-5)

Program output:
a=content: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
f=content: 14 90 90 1 30 0 74 9 10 0 80 9 10 0 1 0 0 0 c0 2d e 90 f0 2 30

Expected behavior would be to have both variables initialized to zeroes - this
is OK in non-optimized build (-O0).

Here's the source snippet:

#include <iostream>
#include <sstream>

using std::string;

struct ip_addr {
    string to_string() const {
        std::stringstream os;
        os << "content: ";
        for (size_t i = 0; i < sizeof(addr_); ++i) {
            os << std::hex
                << (short(addr_[i]) & 0xff) << " ";
        }
        return os.str();
    }        
    ip_addr() {
        memset(&addr_, 0, sizeof(addr_));
    }
private:
    char addr_[25];
};

struct foo {
    foo(ip_addr const & a) : remote_(a) {
        rebind(remote_);
    }
    void rebind(ip_addr const & to) {
        remote_ = to;
    }
    ip_addr remote_;
};
int main()
{
    ip_addr a;
    std::cout << "a=" << a.to_string() << std::endl;
    foo f(a);
    std::cout << "f=" << f.remote_.to_string() << std::endl;
}


-- 
           Summary: incorrect code when compiled with optimization (-O1)
           Product: gcc
           Version: 4.1.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: noblige at gmail dot com
 GCC build triplet: gcc version 4.1.2 20060613 (prerelease) (Debian 4.1.1-5)


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


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug c++/34459] [4.1/4.3 Regression] incorrect code when compiled with optimization (-O1)
  2007-12-14  6:29 [Bug c++/34459] New: incorrect code when compiled with optimization (-O1) noblige at gmail dot com
@ 2007-12-14 13:32 ` rguenth at gcc dot gnu dot org
  2007-12-14 13:42 ` rguenth at gcc dot gnu dot org
                   ` (8 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2007-12-14 13:32 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from rguenth at gcc dot gnu dot org  2007-12-14 13:32 -------
Confirmed.  Reduced testcase:

extern "C" void abort(void);
extern "C" void *memset(void *s, int c, __SIZE_TYPE__ n);

struct ip_addr {
    void __attribute__((noinline)) verify() const
    {
        for (int i = 0; i < sizeof(addr_); ++i)
            if (addr_[i] != 0)
                abort ();
    }
    ip_addr() {
        memset(&addr_, 0, sizeof(addr_));
    }
    char addr_[25];
};

struct foo {
    foo(ip_addr const & a) : remote_(a) { remote_ = remote_; }
    ip_addr remote_;
};

int main()
{
    ip_addr a;
    a.verify();
    foo f(a);
    f.remote_.verify();
}


On trunk after inlining we end up with main() like the following:

<bb 2>:
  # a_6 = VDEF <a_4(D)>
  # f_7 = VDEF <f_5(D)>
  memset (&a.addr_, 0, 25);
  # a_8 = VDEF <a_6>
  # f_9 = VDEF <f_7>
  verify (&a);
  # VUSE <a_8>
  # f_10 = VDEF <f_9>
  f.remote_ = a;
  # f_11 = VDEF <f_10>
  f.remote_ = f.remote_;
  # a_12 = VDEF <a_8>
  # f_13 = VDEF <f_11>
  verify (&f.remote_);
  return 0;

and DSE chooses to remove the first store to f.remote.  Whoops.


-- 

rguenth at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
     Ever Confirmed|0                           |1
           Keywords|                            |wrong-code
      Known to fail|                            |4.1.3 4.3.0
      Known to work|                            |4.2.2
   Last reconfirmed|0000-00-00 00:00:00         |2007-12-14 13:32:37
               date|                            |
            Summary|incorrect code when compiled|[4.1/4.3 Regression]
                   |with optimization (-O1)     |incorrect code when compiled
                   |                            |with optimization (-O1)
   Target Milestone|---                         |4.1.3


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


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug c++/34459] [4.1/4.3 Regression] incorrect code when compiled with optimization (-O1)
  2007-12-14  6:29 [Bug c++/34459] New: incorrect code when compiled with optimization (-O1) noblige at gmail dot com
  2007-12-14 13:32 ` [Bug c++/34459] [4.1/4.3 Regression] " rguenth at gcc dot gnu dot org
@ 2007-12-14 13:42 ` rguenth at gcc dot gnu dot org
  2007-12-17 15:08 ` jakub at gcc dot gnu dot org
                   ` (7 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2007-12-14 13:42 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from rguenth at gcc dot gnu dot org  2007-12-14 13:42 -------
As even

  aggregate = *pointer

is valid gimple, DSE needs to punt on whole-aggregate DSE completely.


-- 


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


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug c++/34459] [4.1/4.3 Regression] incorrect code when compiled with optimization (-O1)
  2007-12-14  6:29 [Bug c++/34459] New: incorrect code when compiled with optimization (-O1) noblige at gmail dot com
  2007-12-14 13:32 ` [Bug c++/34459] [4.1/4.3 Regression] " rguenth at gcc dot gnu dot org
  2007-12-14 13:42 ` rguenth at gcc dot gnu dot org
@ 2007-12-17 15:08 ` jakub at gcc dot gnu dot org
  2007-12-19 15:32 ` jakub at gcc dot gnu dot org
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: jakub at gcc dot gnu dot org @ 2007-12-17 15:08 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from jakub at gcc dot gnu dot org  2007-12-17 15:08 -------
C testcase:

extern void abort (void);
extern void *memset (void *s, int c, __SIZE_TYPE__ n);

struct S
{
  char s[25];
};

void __attribute__((noinline))
foo (struct S *x)
{
  int i;
  for (i = 0; i < sizeof (x->s); ++i)
    if (x->s[i] != 0)
      abort ();
}

int
main ()
{
  struct S a;
  memset (&a.s, '\0', sizeof (a.s));
  foo (&a);
  struct S b = a;
  b = b;
  foo (&b);
  return 0;
}

Weird that nothing removed the noop move earlier, shouldn't that be a job at
least for DCE?


-- 


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


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug c++/34459] [4.1/4.3 Regression] incorrect code when compiled with optimization (-O1)
  2007-12-14  6:29 [Bug c++/34459] New: incorrect code when compiled with optimization (-O1) noblige at gmail dot com
                   ` (2 preceding siblings ...)
  2007-12-17 15:08 ` jakub at gcc dot gnu dot org
@ 2007-12-19 15:32 ` jakub at gcc dot gnu dot org
  2007-12-19 17:04 ` jakub at gcc dot gnu dot org
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: jakub at gcc dot gnu dot org @ 2007-12-19 15:32 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from jakub at gcc dot gnu dot org  2007-12-19 15:31 -------
extern void abort (void);
extern void *memset (void *s, int c, __SIZE_TYPE__ n);

struct S
{
  char s[25];
};

struct S *p;

void __attribute__((noinline))
foo (struct S *x, int set)
{
  int i;
  for (i = 0; i < sizeof (x->s); ++i)
    if (x->s[i] != 0)
      abort ();
    else if (set)
      x->s[i] = set;
  p = x;
}

int
main ()
{
  struct S a;
  memset (&a.s, '\0', sizeof (a.s));
  foo (&a, 0);
  struct S b = a;
  foo (&b, 1);
  b = a;
  b = *p;
  foo (&b, 0);
  return 0;
}

fails on the trunk too at -O2 (succeeds with -O2 -fno-tree-dse).  Here we have:
  # VUSE <aD.1560_18> { aD.1560 }
  # bD.1561_21 = VDEF <bD.1561_19> { bD.1561 }
  bD.1561 = aD.1560;
  # VUSE <pD.1547_17> { pD.1547 }
  p.2D.1562_1 = pD.1547;
  # VUSE <aD.1560_18, SMT.28D.1593_20> { aD.1560 bD.1561 SMT.28D.1593 }
  # bD.1561_22 = VDEF <bD.1561_21> { bD.1561 }
  bD.1561 = *p.2D.1562_1;
at dce4.


-- 


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


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug c++/34459] [4.1/4.3 Regression] incorrect code when compiled with optimization (-O1)
  2007-12-14  6:29 [Bug c++/34459] New: incorrect code when compiled with optimization (-O1) noblige at gmail dot com
                   ` (3 preceding siblings ...)
  2007-12-19 15:32 ` jakub at gcc dot gnu dot org
@ 2007-12-19 17:04 ` jakub at gcc dot gnu dot org
  2007-12-19 20:50 ` jakub at gcc dot gnu dot org
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: jakub at gcc dot gnu dot org @ 2007-12-19 17:04 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #5 from jakub at gcc dot gnu dot org  2007-12-19 17:03 -------
Created an attachment (id=14797)
 --> (http://gcc.gnu.org/bugzilla/attachment.cgi?id=14797&action=view)
gcc43-pr34459.patch

Untested patch.  The gimplify.c part is there just as an optimization, isn't
strictly necessary to fix the problem.


-- 


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


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug c++/34459] [4.1/4.3 Regression] incorrect code when compiled with optimization (-O1)
  2007-12-14  6:29 [Bug c++/34459] New: incorrect code when compiled with optimization (-O1) noblige at gmail dot com
                   ` (4 preceding siblings ...)
  2007-12-19 17:04 ` jakub at gcc dot gnu dot org
@ 2007-12-19 20:50 ` jakub at gcc dot gnu dot org
  2007-12-20  9:32 ` jakub at gcc dot gnu dot org
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: jakub at gcc dot gnu dot org @ 2007-12-19 20:50 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #6 from jakub at gcc dot gnu dot org  2007-12-19 20:49 -------
I have bootstrapped/regtested this patch on x86_64-linux with:
               {                                                
                 record_voperand_set (dse_gd->stores, &bd->stores, ann->uid);
                 return;
               }
replaced with gcc_unreachable () to see where it hits.  It bootstrapped fine,
the
only regression was the newly added tests (which works with the original patch,
obviously with that gcc_unreachable () ICEs) and gcc.dg/uninit-E.c reports
the warning on line 8 rather than expected 7 (because the i = i; statement
is optimized out by the gimplify.c hunk).  So perhaps the gimplify.c hunk in
that patch isn't a good idea and we should eliminate the nop moves later on
(either during DCE, or some other pass, suggestions?  But that can be left
for a separate patch, keeping just tree-ssa-dse.c (dse_optimize_stmt) hunk
together with the new 20071219-1.c test as the fix for this PR.


-- 


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


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug c++/34459] [4.1/4.3 Regression] incorrect code when compiled with optimization (-O1)
  2007-12-14  6:29 [Bug c++/34459] New: incorrect code when compiled with optimization (-O1) noblige at gmail dot com
                   ` (5 preceding siblings ...)
  2007-12-19 20:50 ` jakub at gcc dot gnu dot org
@ 2007-12-20  9:32 ` jakub at gcc dot gnu dot org
  2007-12-20 14:41 ` jakub at gcc dot gnu dot org
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: jakub at gcc dot gnu dot org @ 2007-12-20  9:32 UTC (permalink / raw)
  To: gcc-bugs



-- 

jakub at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         AssignedTo|unassigned at gcc dot gnu   |jakub at gcc dot gnu dot org
                   |dot org                     |
                URL|                            |http://gcc.gnu.org/ml/gcc-
                   |                            |patches/2007-
                   |                            |12/msg00971.html
             Status|NEW                         |ASSIGNED
   Last reconfirmed|2007-12-14 13:32:37         |2007-12-20 09:31:51
               date|                            |


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


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug c++/34459] [4.1/4.3 Regression] incorrect code when compiled with optimization (-O1)
  2007-12-14  6:29 [Bug c++/34459] New: incorrect code when compiled with optimization (-O1) noblige at gmail dot com
                   ` (6 preceding siblings ...)
  2007-12-20  9:32 ` jakub at gcc dot gnu dot org
@ 2007-12-20 14:41 ` jakub at gcc dot gnu dot org
  2007-12-20 14:43 ` [Bug c++/34459] [4.1 " jakub at gcc dot gnu dot org
  2008-07-04 16:17 ` jsm28 at gcc dot gnu dot org
  9 siblings, 0 replies; 11+ messages in thread
From: jakub at gcc dot gnu dot org @ 2007-12-20 14:41 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #7 from jakub at gcc dot gnu dot org  2007-12-20 14:40 -------
Subject: Bug 34459

Author: jakub
Date: Thu Dec 20 14:40:33 2007
New Revision: 131101

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=131101
Log:
        PR c++/34459
        * tree-ssa-dse.c (dse_optimize_stmt): Don't eliminate store if
        USE_STMT not only stores into the same object as STMT, but might
        read it too.

        * gcc.c-torture/execute/20071219-1.c: New test.

Added:
    trunk/gcc/testsuite/gcc.c-torture/execute/20071219-1.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/tree-ssa-dse.c


-- 


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


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug c++/34459] [4.1 Regression] incorrect code when compiled with optimization (-O1)
  2007-12-14  6:29 [Bug c++/34459] New: incorrect code when compiled with optimization (-O1) noblige at gmail dot com
                   ` (7 preceding siblings ...)
  2007-12-20 14:41 ` jakub at gcc dot gnu dot org
@ 2007-12-20 14:43 ` jakub at gcc dot gnu dot org
  2008-07-04 16:17 ` jsm28 at gcc dot gnu dot org
  9 siblings, 0 replies; 11+ messages in thread
From: jakub at gcc dot gnu dot org @ 2007-12-20 14:43 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #8 from jakub at gcc dot gnu dot org  2007-12-20 14:43 -------
Fixed on the trunk.


-- 

jakub at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
      Known to fail|4.1.3 4.3.0                 |4.1.3
      Known to work|4.2.2                       |4.2.2 4.3.0
            Summary|[4.1/4.3 Regression]        |[4.1 Regression] incorrect
                   |incorrect code when compiled|code when compiled with
                   |with optimization (-O1)     |optimization (-O1)


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


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug c++/34459] [4.1 Regression] incorrect code when compiled with optimization (-O1)
  2007-12-14  6:29 [Bug c++/34459] New: incorrect code when compiled with optimization (-O1) noblige at gmail dot com
                   ` (8 preceding siblings ...)
  2007-12-20 14:43 ` [Bug c++/34459] [4.1 " jakub at gcc dot gnu dot org
@ 2008-07-04 16:17 ` jsm28 at gcc dot gnu dot org
  9 siblings, 0 replies; 11+ messages in thread
From: jsm28 at gcc dot gnu dot org @ 2008-07-04 16:17 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #9 from jsm28 at gcc dot gnu dot org  2008-07-04 16:17 -------
Closing 4.1 branch.


-- 

jsm28 at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|                            |FIXED
   Target Milestone|4.1.3                       |4.2.2


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


^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2008-07-04 16:17 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-12-14  6:29 [Bug c++/34459] New: incorrect code when compiled with optimization (-O1) noblige at gmail dot com
2007-12-14 13:32 ` [Bug c++/34459] [4.1/4.3 Regression] " rguenth at gcc dot gnu dot org
2007-12-14 13:42 ` rguenth at gcc dot gnu dot org
2007-12-17 15:08 ` jakub at gcc dot gnu dot org
2007-12-19 15:32 ` jakub at gcc dot gnu dot org
2007-12-19 17:04 ` jakub at gcc dot gnu dot org
2007-12-19 20:50 ` jakub at gcc dot gnu dot org
2007-12-20  9:32 ` jakub at gcc dot gnu dot org
2007-12-20 14:41 ` jakub at gcc dot gnu dot org
2007-12-20 14:43 ` [Bug c++/34459] [4.1 " jakub at gcc dot gnu dot org
2008-07-04 16:17 ` jsm28 at gcc dot gnu dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).