public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/36149]  New: -O2 optimization generates wrong code
@ 2008-05-06  7:14 dino at concisoft dot com
  2008-05-06  7:19 ` [Bug c++/36149] " dino at concisoft dot com
                   ` (13 more replies)
  0 siblings, 14 replies; 15+ messages in thread
From: dino at concisoft dot com @ 2008-05-06  7:14 UTC (permalink / raw)
  To: gcc-bugs

GCC version:
gcc (GCC) 4.1.2 20070925 (Red Hat 4.1.2-27)

It is really hard to reproduce and goes away even with minimal code changes.

Happens in a unit test. 

Host is x86_64: "model name      : Intel(R) Core(TM)2 CPU          6300  @
1.86GHz" from /proc/cpuinfo.

The following snippets might be useful to someone perhaps:

C code:

    void pRemove(CLASS *d)
    {
      LC(d)->m_back->m_fwd = LC(d)->m_fwd;
      LC(d)->m_fwd->m_back = LC(d)->m_back;
    }

    void prepend(CLASS *a)
    {
        pRemove(a);
        LC(a)->m_fwd = head.m_fwd;
        head.m_fwd->m_back = LC(a);
        LC(a)->m_back = &head;
        head.m_fwd = LC(a);
    }

MapList::add():
            // ...
            m_list.remove(removed);
            if (fTop) {
                  m_list.prepend(item);
            } else {
            // ...


Disassembly w/ comments:

 8053c7f:       80 7d 8f 00             cmpb   $0x0,0xffffff8f(%ebp)        ###
if (fTop)
 8053c83:       89 02                   mov    %eax,(%edx)
 8053c85:       8b 07                   mov    (%edi),%eax
 8053c87:       89 3f                   mov    %edi,(%edi)
 8053c89:       89 50 04                mov    %edx,0x4(%eax)
 8053c8c:       89 7f 04                mov    %edi,0x4(%edi)

The above few lines perform the remove(removed) call and the if test out of
order, which is OK. After that, variables look like this:
14: item.m_back = (Vapi::DoubleLink *) 0x81d4400
13: item.m_fwd = (Vapi::DoubleLink *) 0x81d4400
12: item = (DownloadQueue::DQ_Item *) 0x81d4400
11: this->m_list.head.m_back = (Vapi::DoubleLink *) 0xffc26b3c
10: this->m_list.head.m_fwd = (Vapi::DoubleLink *) 0xffc26b3c
9: &this->m_list.head = (Vapi::DoubleLink *) 0xffc26b3c
8: removed.m_back = (Vapi::DoubleLink *) 0x81d43b0
7: removed.m_fwd = (Vapi::DoubleLink *) 0x81d43b0
6: removed = (DownloadQueue::DQ_Item *) 0x81d43b0

The m_list.head is empty, item is on no list, and removed is on no list.

Then the code for prepend(item) is supposed to execute:

 8053c8f:       0f 84 bf 01 00 00       je     8053e54
<DownloadQueue::DQ_MapList::add(DownloadQueue::DQ_Item*, bool)+0x354>
 8053c95:       8b 55 0c                mov    0xc(%ebp),%edx               ###
edx = item
 8053c98:       8b 42 04                mov    0x4(%edx),%eax               ###
eax = item->m_back (in pRemove)
 8053c9b:       89 d3                   mov    %edx,%ebx                    ###
ebx = item
 8053c9d:       8b 12                   mov    (%edx),%edx                  ###
edx = item->m_fwd
 8053c9f:       89 10                   mov    %edx,(%eax)                  ###
item->m_back->m_fwd = edx (item->m_fwd)
 8053ca1:       8b 13                   mov    (%ebx),%edx                  ###
edx = item->m_fwd
 8053ca3:       89 0b                   mov    %ecx,(%ebx)                  ###
item->m_fwd = %ecx (removed?!)
 8053ca5:       89 42 04                mov    %eax,0x4(%edx)               ###
item->m_fwd->m_back = item->m_back
 8053ca8:       8b 45 9c                mov    0xffffff9c(%ebp),%eax        ###
eax = head
 8053cab:       8b 55 98                mov    0xffffff98(%ebp),%edx        ###
??
 8053cae:       89 59 04                mov    %ebx,0x4(%ecx)               ###
removed->m_back = item (?!)
 8053cb1:       89 43 04                mov    %eax,0x4(%ebx)               ###
item->m_back = head
 8053cb4:       89 5a 04                mov    %ebx,0x4(%edx)               ###
?? head->m_back = item
 8053cb7:       8b 5f 20                mov    0x20(%edi),%ebx

But that doesn't look all that great. The compiler somehow tries to interleave
the pRemove and the rest of prepend, but the result is pretty bad:

14: item.m_back = (Vapi::DoubleLink *) 0xffc26b3c
13: item.m_fwd = (Vapi::DoubleLink *) 0x81d43b0
12: item = (DownloadQueue::DQ_Item *) 0x81d4400
11: this->m_list.head.m_back = (Vapi::DoubleLink *) 0xffc26b3c
10: this->m_list.head.m_fwd = (Vapi::DoubleLink *) 0x81d4400
9: &this->m_list.head = (Vapi::DoubleLink *) 0xffc26b3c
8: removed.m_back = (Vapi::DoubleLink *) 0x81d4400
7: removed.m_fwd = (Vapi::DoubleLink *) 0x81d43b0
6: removed = (DownloadQueue::DQ_Item *) 0x81d43b0

Item and removed point to each other, with one of their pointers, m_list.head
points to item but not with both pointers, item points to head with one of its
pointers...

May not be able to provide full source that reproduces the problem (couldn't
generate a short piece of code that reproduces the problem.) But, could perhaps
provide additional info.


-- 
           Summary: -O2 optimization generates wrong code
           Product: gcc
           Version: 4.1.2
            Status: UNCONFIRMED
          Severity: major
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: dino at concisoft dot com


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


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

* [Bug c++/36149] -O2 optimization generates wrong code
  2008-05-06  7:14 [Bug c++/36149] New: -O2 optimization generates wrong code dino at concisoft dot com
@ 2008-05-06  7:19 ` dino at concisoft dot com
  2008-05-06  8:24 ` pinskia at gcc dot gnu dot org
                   ` (12 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: dino at concisoft dot com @ 2008-05-06  7:19 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from dino at concisoft dot com  2008-05-06 07:19 -------
The same code works fine with -O1 and -O3.


-- 


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


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

* [Bug c++/36149] -O2 optimization generates wrong code
  2008-05-06  7:14 [Bug c++/36149] New: -O2 optimization generates wrong code dino at concisoft dot com
  2008-05-06  7:19 ` [Bug c++/36149] " dino at concisoft dot com
@ 2008-05-06  8:24 ` pinskia at gcc dot gnu dot org
  2008-05-06 14:19 ` dino at concisoft dot com
                   ` (11 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2008-05-06  8:24 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from pinskia at gcc dot gnu dot org  2008-05-06 08:23 -------
How is LC defined?


-- 


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


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

* [Bug c++/36149] -O2 optimization generates wrong code
  2008-05-06  7:14 [Bug c++/36149] New: -O2 optimization generates wrong code dino at concisoft dot com
  2008-05-06  7:19 ` [Bug c++/36149] " dino at concisoft dot com
  2008-05-06  8:24 ` pinskia at gcc dot gnu dot org
@ 2008-05-06 14:19 ` dino at concisoft dot com
  2008-05-06 15:23 ` pinskia at gcc dot gnu dot org
                   ` (10 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: dino at concisoft dot com @ 2008-05-06 14:19 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from dino at concisoft dot com  2008-05-06 14:18 -------
    template<class Y> static LINK* LC(Y* X) {
        return static_cast<LINK*>(X);
    }
    template<class Y> static const LINK* LCC(Y* X) {
        return static_cast<const LINK*>(X);
    }


-- 


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


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

* [Bug c++/36149] -O2 optimization generates wrong code
  2008-05-06  7:14 [Bug c++/36149] New: -O2 optimization generates wrong code dino at concisoft dot com
                   ` (2 preceding siblings ...)
  2008-05-06 14:19 ` dino at concisoft dot com
@ 2008-05-06 15:23 ` pinskia at gcc dot gnu dot org
  2008-05-06 15:43 ` dino at concisoft dot com
                   ` (9 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2008-05-06 15:23 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from pinskia at gcc dot gnu dot org  2008-05-06 15:23 -------
This looks like you are violating C/C++ aliasing rules from that definition.


-- 


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


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

* [Bug c++/36149] -O2 optimization generates wrong code
  2008-05-06  7:14 [Bug c++/36149] New: -O2 optimization generates wrong code dino at concisoft dot com
                   ` (3 preceding siblings ...)
  2008-05-06 15:23 ` pinskia at gcc dot gnu dot org
@ 2008-05-06 15:43 ` dino at concisoft dot com
  2008-05-06 15:46 ` pinskia at gcc dot gnu dot org
                   ` (8 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: dino at concisoft dot com @ 2008-05-06 15:43 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #5 from dino at concisoft dot com  2008-05-06 15:42 -------
Are we missing a const, or would you care to give a bit more specific comment?

I see that:

    template<class Y> static const LINK* LCC(Y* X) {
        return static_cast<const LINK*>(X);
    }

could be missing a const in the declaration of Y* X, should probably be const
Y* X. Is that what you're referring to?


-- 


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


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

* [Bug c++/36149] -O2 optimization generates wrong code
  2008-05-06  7:14 [Bug c++/36149] New: -O2 optimization generates wrong code dino at concisoft dot com
                   ` (4 preceding siblings ...)
  2008-05-06 15:43 ` dino at concisoft dot com
@ 2008-05-06 15:46 ` pinskia at gcc dot gnu dot org
  2008-05-06 16:55 ` dino at concisoft dot com
                   ` (7 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2008-05-06 15:46 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #6 from pinskia at gcc dot gnu dot org  2008-05-06 15:45 -------
As I mentioned it looks like you are violating aliasing rules.  Basically you
are accessing one type as another.  But without a full testcase I cann't be
sure.  Also Does -O2 -fno-strict-aliasing work?


-- 


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


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

* [Bug c++/36149] -O2 optimization generates wrong code
  2008-05-06  7:14 [Bug c++/36149] New: -O2 optimization generates wrong code dino at concisoft dot com
                   ` (5 preceding siblings ...)
  2008-05-06 15:46 ` pinskia at gcc dot gnu dot org
@ 2008-05-06 16:55 ` dino at concisoft dot com
  2008-05-07  4:18 ` bangerth at dealii dot org
                   ` (6 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: dino at concisoft dot com @ 2008-05-06 16:55 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #7 from dino at concisoft dot com  2008-05-06 16:54 -------
-O2 -fno-strict-aliasing does work.

I think the LC and LLC functions always cast from a derived class to a base
class. That shouldn't cause aliasing problems, but I need to learn more about
that.

Thanks.


-- 


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


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

* [Bug c++/36149] -O2 optimization generates wrong code
  2008-05-06  7:14 [Bug c++/36149] New: -O2 optimization generates wrong code dino at concisoft dot com
                   ` (6 preceding siblings ...)
  2008-05-06 16:55 ` dino at concisoft dot com
@ 2008-05-07  4:18 ` bangerth at dealii dot org
  2008-05-07  6:15 ` dino at concisoft dot com
                   ` (5 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: bangerth at dealii dot org @ 2008-05-07  4:18 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #8 from bangerth at dealii dot org  2008-05-07 04:17 -------
The point is: without the complete source code nothing definitive can
be said whether it's the compiler's or the programmer's fault. Your chances
that someone will take the time to try to understand what's going on
increase exponentially if you manage to produce a small testcase, whereas
they are pretty low if all you can show is several thousand lines of code.

W.


-- 

bangerth at dealii dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bangerth at dealii dot org


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


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

* [Bug c++/36149] -O2 optimization generates wrong code
  2008-05-06  7:14 [Bug c++/36149] New: -O2 optimization generates wrong code dino at concisoft dot com
                   ` (7 preceding siblings ...)
  2008-05-07  4:18 ` bangerth at dealii dot org
@ 2008-05-07  6:15 ` dino at concisoft dot com
  2008-05-07 19:30 ` rguenth at gcc dot gnu dot org
                   ` (4 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: dino at concisoft dot com @ 2008-05-07  6:15 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #9 from dino at concisoft dot com  2008-05-07 06:15 -------
Understood. Just haven't been able to reproduce on a small piece of code :-(

It seems GNU C++ compiler doesn't give strict-aliasing warnings.


-- 


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


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

* [Bug c++/36149] -O2 optimization generates wrong code
  2008-05-06  7:14 [Bug c++/36149] New: -O2 optimization generates wrong code dino at concisoft dot com
                   ` (8 preceding siblings ...)
  2008-05-07  6:15 ` dino at concisoft dot com
@ 2008-05-07 19:30 ` rguenth at gcc dot gnu dot org
  2008-05-11  7:25 ` pinskia at gcc dot gnu dot org
                   ` (3 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2008-05-07 19:30 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #10 from rguenth at gcc dot gnu dot org  2008-05-07 19:29 -------
Note that gcc 4.1 is known to have some wrong-code bugs regarding aliasing.


-- 


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


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

* [Bug c++/36149] -O2 optimization generates wrong code
  2008-05-06  7:14 [Bug c++/36149] New: -O2 optimization generates wrong code dino at concisoft dot com
                   ` (9 preceding siblings ...)
  2008-05-07 19:30 ` rguenth at gcc dot gnu dot org
@ 2008-05-11  7:25 ` pinskia at gcc dot gnu dot org
  2008-06-06 21:13 ` [Bug middle-end/36149] " dino at concisoft dot com
                   ` (2 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2008-05-11  7:25 UTC (permalink / raw)
  To: gcc-bugs



-- 

pinskia at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|major                       |normal
             Status|UNCONFIRMED                 |WAITING
           Keywords|                            |alias, wrong-code


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


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

* [Bug middle-end/36149] -O2 optimization generates wrong code
  2008-05-06  7:14 [Bug c++/36149] New: -O2 optimization generates wrong code dino at concisoft dot com
                   ` (10 preceding siblings ...)
  2008-05-11  7:25 ` pinskia at gcc dot gnu dot org
@ 2008-06-06 21:13 ` dino at concisoft dot com
  2008-06-06 21:33 ` rguenth at gcc dot gnu dot org
  2008-12-28  6:08 ` pinskia at gcc dot gnu dot org
  13 siblings, 0 replies; 15+ messages in thread
From: dino at concisoft dot com @ 2008-06-06 21:13 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #11 from dino at concisoft dot com  2008-06-06 21:13 -------
Created an attachment (id=15727)
 --> (http://gcc.gnu.org/bugzilla/attachment.cgi?id=15727&action=view)
A relatively simple self contained piece of code that reproduces the problem.

Please see the comments in the Makefile for info on how to reproduce the
problem.


-- 


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


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

* [Bug middle-end/36149] -O2 optimization generates wrong code
  2008-05-06  7:14 [Bug c++/36149] New: -O2 optimization generates wrong code dino at concisoft dot com
                   ` (11 preceding siblings ...)
  2008-06-06 21:13 ` [Bug middle-end/36149] " dino at concisoft dot com
@ 2008-06-06 21:33 ` rguenth at gcc dot gnu dot org
  2008-12-28  6:08 ` pinskia at gcc dot gnu dot org
  13 siblings, 0 replies; 15+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2008-06-06 21:33 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #12 from rguenth at gcc dot gnu dot org  2008-06-06 21:32 -------
Looks more like one of the known alias related miscompilations of the 4.1
branch.


-- 

rguenth at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |rguenth at gcc dot gnu dot
                   |                            |org
             Status|WAITING                     |NEW
     Ever Confirmed|0                           |1
      Known to fail|                            |4.0.4 4.1.3
      Known to work|                            |4.2.4 4.3.0
   Last reconfirmed|0000-00-00 00:00:00         |2008-06-06 21:32:26
               date|                            |


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


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

* [Bug middle-end/36149] -O2 optimization generates wrong code
  2008-05-06  7:14 [Bug c++/36149] New: -O2 optimization generates wrong code dino at concisoft dot com
                   ` (12 preceding siblings ...)
  2008-06-06 21:33 ` rguenth at gcc dot gnu dot org
@ 2008-12-28  6:08 ` pinskia at gcc dot gnu dot org
  13 siblings, 0 replies; 15+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2008-12-28  6:08 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #13 from pinskia at gcc dot gnu dot org  2008-12-28 06:06 -------
Since 4.1.x is closed, closing as fixed for 4.2.x.


-- 

pinskia at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED
   Target Milestone|---                         |4.2.4


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


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

end of thread, other threads:[~2008-12-28  6:08 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-05-06  7:14 [Bug c++/36149] New: -O2 optimization generates wrong code dino at concisoft dot com
2008-05-06  7:19 ` [Bug c++/36149] " dino at concisoft dot com
2008-05-06  8:24 ` pinskia at gcc dot gnu dot org
2008-05-06 14:19 ` dino at concisoft dot com
2008-05-06 15:23 ` pinskia at gcc dot gnu dot org
2008-05-06 15:43 ` dino at concisoft dot com
2008-05-06 15:46 ` pinskia at gcc dot gnu dot org
2008-05-06 16:55 ` dino at concisoft dot com
2008-05-07  4:18 ` bangerth at dealii dot org
2008-05-07  6:15 ` dino at concisoft dot com
2008-05-07 19:30 ` rguenth at gcc dot gnu dot org
2008-05-11  7:25 ` pinskia at gcc dot gnu dot org
2008-06-06 21:13 ` [Bug middle-end/36149] " dino at concisoft dot com
2008-06-06 21:33 ` rguenth at gcc dot gnu dot org
2008-12-28  6:08 ` pinskia 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).