public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/56360] New: Loop invariant motion can introduce speculative store
@ 2013-02-17  4:09 ian at airs dot com
  2013-02-17  4:21 ` [Bug tree-optimization/56360] " ian at airs dot com
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: ian at airs dot com @ 2013-02-17  4:09 UTC (permalink / raw)
  To: gcc-bugs


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

             Bug #: 56360
           Summary: Loop invariant motion can introduce speculative store
    Classification: Unclassified
           Product: gcc
           Version: 4.8.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: tree-optimization
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: ian@airs.com


When I compile this C++ program with -O3 -std=gnu+11 on x86_64:

#include<mutex>
#include<thread>

extern int a;
std::mutex m;

void nop2(int*);

// this function is ONLY called with i=0.
void foo(int i)
{
  if (i) m.lock();
  nop2(&i); // Only here to prevent optimiser from creating one big if().
  for (int c = 0; c < 10000; ++c) {
    if (i) {
      ++a; // Thus this is never executed.
    }
  }
  nop2(&i); // Only here to prevent optimiser from creating one big if().
  if (i) m.unlock();
}

I see this in _Z3fooi:

        movl    a(%rip), %eax
        movl    12(%rsp), %ecx
        leaq    12(%rsp), %rdi
        leal    10000(%rax), %edx
        testl   %ecx, %ecx
        cmovne  %edx, %eax
        movl    %eax, a(%rip)

This unconditionally stores a value in a.  This is a speculative store, which
is invalid according to the C++ memory model.

This does not happen with -O2.

>From looking at the dumps, the bug appears to be in the loop invariant motion
pass.


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

end of thread, other threads:[~2021-07-07 11:35 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-02-17  4:09 [Bug tree-optimization/56360] New: Loop invariant motion can introduce speculative store ian at airs dot com
2013-02-17  4:21 ` [Bug tree-optimization/56360] " ian at airs dot com
2013-02-17  4:55 ` pinskia at gcc dot gnu.org
2013-02-17  4:58 ` pinskia at gcc dot gnu.org
2013-02-17  5:15 ` ian at airs dot com
2013-02-17  5:46 ` pinskia at gcc dot gnu.org
2013-02-17 15:33 ` ian at airs dot com
2013-02-18 10:49 ` [Bug tree-optimization/56360] Loop invariant motion by default " rguenth at gcc dot gnu.org
2021-07-07 11:35 ` rguenth at gcc dot gnu.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).