public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug rtl-optimization/47698] New: CMOV accessing volatile memory with read side effect
@ 2011-02-11 15:40 us15 at os dot inf.tu-dresden.de
  2011-02-11 15:43 ` [Bug rtl-optimization/47698] " us15 at os dot inf.tu-dresden.de
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: us15 at os dot inf.tu-dresden.de @ 2011-02-11 15:40 UTC (permalink / raw)
  To: gcc-bugs

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

           Summary: CMOV accessing volatile memory with read side effect
           Product: gcc
           Version: 4.6.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: rtl-optimization
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: us15@os.inf.tu-dresden.de


In the attached testcase, the variable "mmio" is a memory-mapped device
register with read side effects. It must only be accessed if "cond" is true.
When compiling the testcase:
gcc -Os -c testcase.cc -o testcase.o

the following output is generated:

0000000000000000 <foo()>:
   0:   31 c0                   xor    %eax,%eax
   2:   48 83 3d 00 00 00 00    cmpq   $0x0,0x0(%rip)        # a <foo()+0xa>
   9:   00 
   a:   48 0f 45 05 00 00 00    cmovne 0x0(%rip),%rax        # 12 <foo()+0x12>
  11:   00 
  12:   c3                      retq   

Due to the use of CMOV, gcc is reading the memory location regardless of "cond"
being true, even though "mmio" is casted to volatile.

Reading "mmio" through a volatile pointer, as in
    return *static_cast<volatile unsigned long *>(&mmio);
does not get rid of the CMOV either.

What is the correct way to ensure mmio is read if and only if cond is true?

My gcc version is: gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-slackware-linux/4.6.0/lto-wrapper
Target: x86_64-slackware-linux
Configured with: ../gcc-svn/configure --prefix=/usr --libdir=/usr/lib64
--mandir=/usr/man --infodir=/usr/info --enable-shared --enable-bootstrap
--enable-languages=c,c++,lto --enable-threads=posix --enable-checking=release
--with-system-zlib --with-python-dir=/lib64/python2.6/site-packages
--disable-libunwind-exceptions --enable-__cxa_atexit --enable-libssp
--enable-lto --with-gnu-ld --verbose --disable-multilib
--target=x86_64-slackware-linux --build=x86_64-slackware-linux
--host=x86_64-slackware-linux
Thread model: posix
gcc version 4.6.0 20110211 (experimental) (GCC)


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

* [Bug rtl-optimization/47698] CMOV accessing volatile memory with read side effect
  2011-02-11 15:40 [Bug rtl-optimization/47698] New: CMOV accessing volatile memory with read side effect us15 at os dot inf.tu-dresden.de
@ 2011-02-11 15:43 ` us15 at os dot inf.tu-dresden.de
  2011-02-11 16:04 ` us15 at os dot inf.tu-dresden.de
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: us15 at os dot inf.tu-dresden.de @ 2011-02-11 15:43 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Udo Steinberg <us15 at os dot inf.tu-dresden.de> 2011-02-11 15:40:27 UTC ---
Created attachment 23308
  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=23308
Testcase


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

* [Bug rtl-optimization/47698] CMOV accessing volatile memory with read side effect
  2011-02-11 15:40 [Bug rtl-optimization/47698] New: CMOV accessing volatile memory with read side effect us15 at os dot inf.tu-dresden.de
  2011-02-11 15:43 ` [Bug rtl-optimization/47698] " us15 at os dot inf.tu-dresden.de
@ 2011-02-11 16:04 ` us15 at os dot inf.tu-dresden.de
  2011-02-11 16:36 ` rguenth at gcc dot gnu.org
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: us15 at os dot inf.tu-dresden.de @ 2011-02-11 16:04 UTC (permalink / raw)
  To: gcc-bugs

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

Udo Steinberg <us15 at os dot inf.tu-dresden.de> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
  Attachment #23308|0                           |1
        is obsolete|                            |

--- Comment #2 from Udo Steinberg <us15 at os dot inf.tu-dresden.de> 2011-02-11 15:42:30 UTC ---
Created attachment 23309
  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=23309
Testcase


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

* [Bug rtl-optimization/47698] CMOV accessing volatile memory with read side effect
  2011-02-11 15:40 [Bug rtl-optimization/47698] New: CMOV accessing volatile memory with read side effect us15 at os dot inf.tu-dresden.de
  2011-02-11 15:43 ` [Bug rtl-optimization/47698] " us15 at os dot inf.tu-dresden.de
  2011-02-11 16:04 ` us15 at os dot inf.tu-dresden.de
@ 2011-02-11 16:36 ` rguenth at gcc dot gnu.org
  2011-02-11 17:07 ` matz at gcc dot gnu.org
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: rguenth at gcc dot gnu.org @ 2011-02-11 16:36 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Guenther <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |wrong-code
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2011.02.11 16:34:48
     Ever Confirmed|0                           |1
      Known to fail|                            |4.3.4, 4.5.2, 4.6.0

--- Comment #3 from Richard Guenther <rguenth at gcc dot gnu.org> 2011-02-11 16:34:48 UTC ---
Confirmed.  C testcase:

extern volatile unsigned long mmio;
unsigned long foo(int cond)
{
  if (cond)
    return mmio;
  return 0;
}


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

* [Bug rtl-optimization/47698] CMOV accessing volatile memory with read side effect
  2011-02-11 15:40 [Bug rtl-optimization/47698] New: CMOV accessing volatile memory with read side effect us15 at os dot inf.tu-dresden.de
                   ` (2 preceding siblings ...)
  2011-02-11 16:36 ` rguenth at gcc dot gnu.org
@ 2011-02-11 17:07 ` matz at gcc dot gnu.org
  2011-02-11 17:57 ` hjl.tools at gmail dot com
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: matz at gcc dot gnu.org @ 2011-02-11 17:07 UTC (permalink / raw)
  To: gcc-bugs

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

Michael Matz <matz at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |matz at gcc dot gnu.org

--- Comment #4 from Michael Matz <matz at gcc dot gnu.org> 2011-02-11 17:05:57 UTC ---
noce_operand_ok doesn't disregard volatile MEMs:

  /* We special-case memories, so handle any of them with
     no address side effects.  */
  if (MEM_P (op))
    return ! side_effects_p (XEXP (op, 0));

But I don't see where we would handle volatile MEMs in any sensible
way.  We happily fiddle with such operands resulting in the problem.


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

* [Bug rtl-optimization/47698] CMOV accessing volatile memory with read side effect
  2011-02-11 15:40 [Bug rtl-optimization/47698] New: CMOV accessing volatile memory with read side effect us15 at os dot inf.tu-dresden.de
                   ` (3 preceding siblings ...)
  2011-02-11 17:07 ` matz at gcc dot gnu.org
@ 2011-02-11 17:57 ` hjl.tools at gmail dot com
  2011-11-07  8:42 ` kyukhin at gcc dot gnu.org
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: hjl.tools at gmail dot com @ 2011-02-11 17:57 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from H.J. Lu <hjl.tools at gmail dot com> 2011-02-11 17:48:43 UTC ---
(In reply to comment #4)
> noce_operand_ok doesn't disregard volatile MEMs:
> 
>   /* We special-case memories, so handle any of them with
>      no address side effects.  */
>   if (MEM_P (op))
>     return ! side_effects_p (XEXP (op, 0));
> 
> But I don't see where we would handle volatile MEMs in any sensible
> way.  We happily fiddle with such operands resulting in the problem.

cmov pattern should disallow operand with volatile MEM.


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

* [Bug rtl-optimization/47698] CMOV accessing volatile memory with read side effect
  2011-02-11 15:40 [Bug rtl-optimization/47698] New: CMOV accessing volatile memory with read side effect us15 at os dot inf.tu-dresden.de
                   ` (4 preceding siblings ...)
  2011-02-11 17:57 ` hjl.tools at gmail dot com
@ 2011-11-07  8:42 ` kyukhin at gcc dot gnu.org
  2011-11-08 13:46 ` ebotcazou at gcc dot gnu.org
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: kyukhin at gcc dot gnu.org @ 2011-11-07  8:42 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from Kirill Yukhin <kyukhin at gcc dot gnu.org> 2011-11-07 08:42:00 UTC ---
Author: kyukhin
Date: Mon Nov  7 08:41:55 2011
New Revision: 181075

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=181075
Log:
gcc/
    PR rtl-optimization/47698
    * ifconv.c (noce_operand_ok): prevent CMOV generation
    for volatile mem.

gcc/testsuite/
    PR rtl-optimization/47698
    * gcc.target/i386/47698.c: New test.


Added:
    trunk/gcc/testsuite/gcc.target/i386/47698.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/ifcvt.c
    trunk/gcc/testsuite/ChangeLog


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

* [Bug rtl-optimization/47698] CMOV accessing volatile memory with read side effect
  2011-02-11 15:40 [Bug rtl-optimization/47698] New: CMOV accessing volatile memory with read side effect us15 at os dot inf.tu-dresden.de
                   ` (5 preceding siblings ...)
  2011-11-07  8:42 ` kyukhin at gcc dot gnu.org
@ 2011-11-08 13:46 ` ebotcazou at gcc dot gnu.org
  2012-01-21 20:27 ` pinskia at gcc dot gnu.org
  2021-07-29 22:34 ` pinskia at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: ebotcazou at gcc dot gnu.org @ 2011-11-08 13:46 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from Eric Botcazou <ebotcazou at gcc dot gnu.org> 2011-11-08 13:44:22 UTC ---
Author: ebotcazou
Date: Tue Nov  8 13:44:11 2011
New Revision: 181161

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=181161
Log:
    PR rtl-optimization/47698
    * ifcvt.c (noce_operand_ok): Move around comment.

Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/ifcvt.c


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

* [Bug rtl-optimization/47698] CMOV accessing volatile memory with read side effect
  2011-02-11 15:40 [Bug rtl-optimization/47698] New: CMOV accessing volatile memory with read side effect us15 at os dot inf.tu-dresden.de
                   ` (6 preceding siblings ...)
  2011-11-08 13:46 ` ebotcazou at gcc dot gnu.org
@ 2012-01-21 20:27 ` pinskia at gcc dot gnu.org
  2021-07-29 22:34 ` pinskia at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2012-01-21 20:27 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

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

--- Comment #8 from Andrew Pinski <pinskia at gcc dot gnu.org> 2012-01-21 20:18:48 UTC ---
Fixed.


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

* [Bug rtl-optimization/47698] CMOV accessing volatile memory with read side effect
  2011-02-11 15:40 [Bug rtl-optimization/47698] New: CMOV accessing volatile memory with read side effect us15 at os dot inf.tu-dresden.de
                   ` (7 preceding siblings ...)
  2012-01-21 20:27 ` pinskia at gcc dot gnu.org
@ 2021-07-29 22:34 ` pinskia at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-07-29 22:34 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=47698

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |regehr at cs dot utah.edu

--- Comment #9 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
*** Bug 35764 has been marked as a duplicate of this bug. ***

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

end of thread, other threads:[~2021-07-29 22:34 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-02-11 15:40 [Bug rtl-optimization/47698] New: CMOV accessing volatile memory with read side effect us15 at os dot inf.tu-dresden.de
2011-02-11 15:43 ` [Bug rtl-optimization/47698] " us15 at os dot inf.tu-dresden.de
2011-02-11 16:04 ` us15 at os dot inf.tu-dresden.de
2011-02-11 16:36 ` rguenth at gcc dot gnu.org
2011-02-11 17:07 ` matz at gcc dot gnu.org
2011-02-11 17:57 ` hjl.tools at gmail dot com
2011-11-07  8:42 ` kyukhin at gcc dot gnu.org
2011-11-08 13:46 ` ebotcazou at gcc dot gnu.org
2012-01-21 20:27 ` pinskia at gcc dot gnu.org
2021-07-29 22:34 ` pinskia 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).