public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/37096]  New: conditional evaluation incorrect with -O3
@ 2008-08-12 13:18 erik dot moller at cycos dot com
  2008-08-12 15:50 ` [Bug c++/37096] " rguenth at gcc dot gnu dot org
                   ` (10 more replies)
  0 siblings, 11 replies; 12+ messages in thread
From: erik dot moller at cycos dot com @ 2008-08-12 13:18 UTC (permalink / raw)
  To: gcc-bugs

// system: core duo, 2.4 GHz, 64bit debian sid, vanilla kernel 2.6.26.2
// I stumbled over this using gcc 4.3.1 (Debian 4.3.1-8)
// compiler switches: -O3 -mssse3
// there were no compiler warnings / errors.

const int32_t nCC_[16] = 
{
        3, 2, 2, 1, 1, 1, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0
};

int32_t Residual4x4( int16_t *pAR, int32_t &nCost )
{
        int32_t nRun = 0;

        for( int i=0 ; i<16 ; ++i )
                if ( const int v = pAR[i] ) {
                        nCost += ( abs(v) > 1 ) ? 16 : nCC_[nRun];
                        nRun = 0;
                } else
                        ++nRun;
}

// the above code calculates nCost uncorrectly (leading to a segfault later on)
// when I use the compiler switch -O2 or replace the conditional assignment by
an if / else
// construct, everything works fine.
// gcc 4.2 works fine too. (-with -O3 -msse2)


-- 
           Summary: conditional evaluation incorrect with -O3
           Product: gcc
           Version: 4.3.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: erik dot moller at cycos dot com


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


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

* [Bug c++/37096] conditional evaluation incorrect with -O3
  2008-08-12 13:18 [Bug c++/37096] New: conditional evaluation incorrect with -O3 erik dot moller at cycos dot com
@ 2008-08-12 15:50 ` rguenth at gcc dot gnu dot org
  2008-08-16 22:19 ` pinskia at gcc dot gnu dot org
                   ` (9 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2008-08-12 15:50 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from rguenth at gcc dot gnu dot org  2008-08-12 15:49 -------
Can you please provide a testcase that can be complied and run stating the
expected and the errorneous output you see?

Thanks.


-- 

rguenth at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |rguenth at gcc dot gnu dot
                   |                            |org


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


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

* [Bug c++/37096] conditional evaluation incorrect with -O3
  2008-08-12 13:18 [Bug c++/37096] New: conditional evaluation incorrect with -O3 erik dot moller at cycos dot com
  2008-08-12 15:50 ` [Bug c++/37096] " rguenth at gcc dot gnu dot org
@ 2008-08-16 22:19 ` pinskia at gcc dot gnu dot org
  2008-08-16 22:45 ` [Bug middle-end/37096] " pinskia at gcc dot gnu dot org
                   ` (8 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2008-08-16 22:19 UTC (permalink / raw)
  To: gcc-bugs



-- 

pinskia at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |WAITING


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


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

* [Bug middle-end/37096] conditional evaluation incorrect with -O3
  2008-08-12 13:18 [Bug c++/37096] New: conditional evaluation incorrect with -O3 erik dot moller at cycos dot com
  2008-08-12 15:50 ` [Bug c++/37096] " rguenth at gcc dot gnu dot org
  2008-08-16 22:19 ` pinskia at gcc dot gnu dot org
@ 2008-08-16 22:45 ` pinskia at gcc dot gnu dot org
  2008-08-18 16:21 ` erik dot moller at cycos dot com
                   ` (7 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2008-08-16 22:45 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from pinskia at gcc dot gnu dot org  2008-08-16 22:43 -------
Might be related to PR 37101.

Does -O2 -ftree-vectorize fail too?


-- 

pinskia at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |pinskia at gcc dot gnu dot
                   |                            |org
          Component|c++                         |middle-end


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


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

* [Bug middle-end/37096] conditional evaluation incorrect with -O3
  2008-08-12 13:18 [Bug c++/37096] New: conditional evaluation incorrect with -O3 erik dot moller at cycos dot com
                   ` (2 preceding siblings ...)
  2008-08-16 22:45 ` [Bug middle-end/37096] " pinskia at gcc dot gnu dot org
@ 2008-08-18 16:21 ` erik dot moller at cycos dot com
  2008-08-18 16:22 ` erik dot moller at cycos dot com
                   ` (6 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: erik dot moller at cycos dot com @ 2008-08-18 16:21 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from erik dot moller at cycos dot com  2008-08-18 16:20 -------
Created an attachment (id=16084)
 --> (http://gcc.gnu.org/bugzilla/attachment.cgi?id=16084&action=view)
main() for the testcase


-- 


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


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

* [Bug middle-end/37096] conditional evaluation incorrect with -O3
  2008-08-12 13:18 [Bug c++/37096] New: conditional evaluation incorrect with -O3 erik dot moller at cycos dot com
                   ` (3 preceding siblings ...)
  2008-08-18 16:21 ` erik dot moller at cycos dot com
@ 2008-08-18 16:22 ` erik dot moller at cycos dot com
  2008-08-18 16:23 ` erik dot moller at cycos dot com
                   ` (5 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: erik dot moller at cycos dot com @ 2008-08-18 16:22 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from erik dot moller at cycos dot com  2008-08-18 16:20 -------
Created an attachment (id=16085)
 --> (http://gcc.gnu.org/bugzilla/attachment.cgi?id=16085&action=view)
the test case

it's as minimal I could get it...


-- 


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


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

* [Bug middle-end/37096] conditional evaluation incorrect with -O3
  2008-08-12 13:18 [Bug c++/37096] New: conditional evaluation incorrect with -O3 erik dot moller at cycos dot com
                   ` (4 preceding siblings ...)
  2008-08-18 16:22 ` erik dot moller at cycos dot com
@ 2008-08-18 16:23 ` erik dot moller at cycos dot com
  2008-09-08 10:55 ` erik dot moller at cycos dot com
                   ` (4 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: erik dot moller at cycos dot com @ 2008-08-18 16:23 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #5 from erik dot moller at cycos dot com  2008-08-18 16:22 -------

the printf("shouldn't happen: %d\n", pAR[0]) happens when debug flags -O2 or
-O3 and -msse2 are set.
the printf("nz: %d\n", nZ); seems to do something awful...

void fun(short *pAR)
{
        const short tmp = Transform4x4(pAR);
        pAR[0] = 0;

        int nZ = 0;
        for( int i=0; i<4 ; ++i )
                if ( pAR[i] ) {
                        ++nZ;
                }

        printf("nz: %d\n", nZ);
        if ( pAR[0] )
                printf("shouldn't happen: %d\n", pAR[0]);
        pAR[0] = tmp;
}


-- 


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


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

* [Bug middle-end/37096] conditional evaluation incorrect with -O3
  2008-08-12 13:18 [Bug c++/37096] New: conditional evaluation incorrect with -O3 erik dot moller at cycos dot com
                   ` (5 preceding siblings ...)
  2008-08-18 16:23 ` erik dot moller at cycos dot com
@ 2008-09-08 10:55 ` erik dot moller at cycos dot com
  2008-09-11 17:59 ` [Bug target/37096] " ubizjak at gmail dot com
                   ` (3 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: erik dot moller at cycos dot com @ 2008-09-08 10:55 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #6 from erik dot moller at cycos dot com  2008-09-08 10:54 -------
bug is still in 4.3.2


-- 

erik dot moller at cycos dot com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Version|4.3.1                       |4.3.2


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


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

* [Bug target/37096] conditional evaluation incorrect with -O3
  2008-08-12 13:18 [Bug c++/37096] New: conditional evaluation incorrect with -O3 erik dot moller at cycos dot com
                   ` (6 preceding siblings ...)
  2008-09-08 10:55 ` erik dot moller at cycos dot com
@ 2008-09-11 17:59 ` ubizjak at gmail dot com
  2008-09-11 18:17 ` ubizjak at gmail dot com
                   ` (2 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: ubizjak at gmail dot com @ 2008-09-11 17:59 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #7 from ubizjak at gmail dot com  2008-09-11 17:57 -------
There is a runtime difference between -O1 and -O2:

g++ -O1 pr37096.cpp main.o
./a.out
nz: 3

g++ -O2 pr37096.cpp main.o
./a.out
nz: 3
98

Target: x86_64-unknown-linux-gnu
gcc version 4.4.0 20080911 (experimental) [trunk revision 140293] (GCC)

Probably target issue, I will look into it.


-- 

ubizjak at gmail dot com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         AssignedTo|unassigned at gcc dot gnu   |ubizjak at gmail dot com
                   |dot org                     |
             Status|WAITING                     |ASSIGNED
          Component|middle-end                  |target
     Ever Confirmed|0                           |1
   Last reconfirmed|0000-00-00 00:00:00         |2008-09-11 17:57:56
               date|                            |


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


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

* [Bug target/37096] conditional evaluation incorrect with -O3
  2008-08-12 13:18 [Bug c++/37096] New: conditional evaluation incorrect with -O3 erik dot moller at cycos dot com
                   ` (7 preceding siblings ...)
  2008-09-11 17:59 ` [Bug target/37096] " ubizjak at gmail dot com
@ 2008-09-11 18:17 ` ubizjak at gmail dot com
  2008-09-12 11:35 ` erik dot moller at cycos dot com
  2008-09-12 18:04 ` ubizjak at gmail dot com
  10 siblings, 0 replies; 12+ messages in thread
From: ubizjak at gmail dot com @ 2008-09-11 18:17 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #8 from ubizjak at gmail dot com  2008-09-11 18:16 -------
Hm, with -O2 -fno-strict-aliasing, it works fine.

Is there an aliasing issue involved?

short Transform4x4(short *pMatrix)
{
        __m128i r4, r5;

        __m128i r0 = _mm_loadl_epi64((__m128i *)(pMatrix + 0 * 16));
        __m128i r1 = _mm_loadl_epi64((__m128i *)(pMatrix + 1 * 16));
        __m128i r2 = _mm_loadl_epi64((__m128i *)(pMatrix + 2 * 16));
        __m128i r3 = _mm_loadl_epi64((__m128i *)(pMatrix + 3 * 16));

        ... stuff ...

        _mm_storel_epi64((__m128i *)(pMatrix + 0 * 16), r0);
        _mm_storel_epi64((__m128i *)(pMatrix + 1 * 16), r1);
        _mm_storel_epi64((__m128i *)(pMatrix + 2 * 16), r2);
        _mm_storel_epi64((__m128i *)(pMatrix + 3 * 16), r3);

}


-- 


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


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

* [Bug target/37096] conditional evaluation incorrect with -O3
  2008-08-12 13:18 [Bug c++/37096] New: conditional evaluation incorrect with -O3 erik dot moller at cycos dot com
                   ` (8 preceding siblings ...)
  2008-09-11 18:17 ` ubizjak at gmail dot com
@ 2008-09-12 11:35 ` erik dot moller at cycos dot com
  2008-09-12 18:04 ` ubizjak at gmail dot com
  10 siblings, 0 replies; 12+ messages in thread
From: erik dot moller at cycos dot com @ 2008-09-12 11:35 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #9 from erik dot moller at cycos dot com  2008-09-12 11:33 -------
true, -fno-strict-aliasing makes even -O3 work... I don't know about the
liasing, the example is very simple, can that happen when the SSE2 intrinsics
are involved?


-- 


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


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

* [Bug target/37096] conditional evaluation incorrect with -O3
  2008-08-12 13:18 [Bug c++/37096] New: conditional evaluation incorrect with -O3 erik dot moller at cycos dot com
                   ` (9 preceding siblings ...)
  2008-09-12 11:35 ` erik dot moller at cycos dot com
@ 2008-09-12 18:04 ` ubizjak at gmail dot com
  10 siblings, 0 replies; 12+ messages in thread
From: ubizjak at gmail dot com @ 2008-09-12 18:04 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #10 from ubizjak at gmail dot com  2008-09-12 18:03 -------
This is in fact undefined code. When Transform4x4() gets inlined in fun(), you
are accessing pAR[0] (aliased to *pMatrix) as "short" and as __m128i. Since
-fstrict-aliasing (the default) assumes that "short" can't alias __m128i, gcc
reorders stores and loads to the same address at will.

This is the diff between -fstrict-aliasing (t_.s) and -fno-strict-aliasing
(t.s):

--- t.s 2008-09-12 19:27:23.000000000 +0200
+++ t_.s        2008-09-12 19:27:04.000000000 +0200
@@ -68,6 +68,7 @@
        movq    8(%rsp), %rax
        movq    %xmm2, 32(%rdi)
        movq    %xmm5, 64(%rdi)
+       movw    $0, (%rdi)
        movq    %xmm0, 96(%rdi)
        movl    %eax, %esi
        movq    %rax, %rcx
@@ -77,10 +78,9 @@
        shrq    $48, %rdx
        testw   %si, %si
        movq    %rax, (%rdi)
-       movw    $0, (%rdi)
+       movl    $.LC0, %edi
        setne   %sil
        cmpw    $1, %cx
-       movl    $.LC0, %edi
        movzbl  %sil, %esi
        sbbl    $-1, %esi
        cmpw    $1, %dx

You can see that store of 0 to (%rdi) has been moved above store of %rax to the
same address. You should use unions to fix your code.


-- 

ubizjak at gmail dot com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|                            |INVALID


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


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

end of thread, other threads:[~2008-09-12 18:04 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-08-12 13:18 [Bug c++/37096] New: conditional evaluation incorrect with -O3 erik dot moller at cycos dot com
2008-08-12 15:50 ` [Bug c++/37096] " rguenth at gcc dot gnu dot org
2008-08-16 22:19 ` pinskia at gcc dot gnu dot org
2008-08-16 22:45 ` [Bug middle-end/37096] " pinskia at gcc dot gnu dot org
2008-08-18 16:21 ` erik dot moller at cycos dot com
2008-08-18 16:22 ` erik dot moller at cycos dot com
2008-08-18 16:23 ` erik dot moller at cycos dot com
2008-09-08 10:55 ` erik dot moller at cycos dot com
2008-09-11 17:59 ` [Bug target/37096] " ubizjak at gmail dot com
2008-09-11 18:17 ` ubizjak at gmail dot com
2008-09-12 11:35 ` erik dot moller at cycos dot com
2008-09-12 18:04 ` ubizjak at gmail dot com

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).