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: unassig...@gcc.gnu.org ReportedBy: u...@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)