GCC 4.7.2 - 4.9.0 for i686 and x86_64 miscompiles the following code. $ cat error.c int main (void) { volatile int k = 1; int t = 2 | ( ( k - 1) >> 31 ); if (t != 2) __builtin_abort(); return 0; } $ x86_64-unknown-linux-gnu-gcc-4.9.0 error.c -O1 $ ./a.out Aborted (core dumped) $ x86_64-unknown-linux-gnu-gcc-4.9.0 -v Using built-in specs. COLLECT_GCC=x86_64-unknown-linux-gnu-gcc-4.9.0 COLLECT_LTO_WRAPPER=/usr/local/x86_64-tools/gcc-4.9.0/libexec/gcc/x86_64-unknown-linux-gnu/4.9.0/lto-wrapper Target: x86_64-unknown-linux-gnu Configured with: /home/hassy/gcc/configure --prefix=/usr/local/x86_64-tools/gcc-4.9.0/ --with-gmp=/usr/local/gmp-5.1.1/ --with-mpfr=/usr/local/mpfr-3.1.2/ --with-mpc=/usr/local/mpc-1.0.1/ --disable-multilib --disable-nls --enable-languages=c Thread model: posix gcc version 4.9.0 20130704 (experimental) (GCC) The status of the other versions are as follows: (x86_64) GCC-4.7.2 Error GCC-4.8.2 (prerelease) OK GCC-4.9.0 (experimental) Error (i686) GCC-4.7.2 Error GCC-4.8.2 (prerelease) Error GCC-4.9.0 (experimental) Error
If I change it to return the computed value of t, I get wrong code for every single gcc version from 4.9 down to 3.2.3 on both x86_64 and sparc64. For int main (void) { volatile int k = 1; int t = 2 | ( ( k - 1) >> 31 ); return t; } gcc-4.9 generates main: movl $1, -4(%rsp) movl -4(%rsp), %eax leal -1(%rax), %eax sarl $31, %eax ret which completely loses all references to the "2 |" part.
|2 disappears during combine.
Created attachment 30463 [details] gcc49-pr57829.patch Sounds very old bug, svn blame shows r7224.
Author: jakub Date: Mon Jul 8 08:11:08 2013 New Revision: 200768 URL: http://gcc.gnu.org/viewcvs?rev=200768&root=gcc&view=rev Log: PR rtl-optimization/57829 * simplify-rtx.c (simplify_binary_operation_1) <case IOR>: Ensure that mask bits outside of mode are just sign-extension from mode to HWI. * gcc.c-torture/execute/pr57829.c: New test. Added: trunk/gcc/testsuite/gcc.c-torture/execute/pr57829.c Modified: trunk/gcc/ChangeLog trunk/gcc/simplify-rtx.c trunk/gcc/testsuite/ChangeLog Author: jakub Date: Mon Jul 8 08:15:01 2013 New Revision: 200770 URL: http://gcc.gnu.org/viewcvs?rev=200770&root=gcc&view=rev Log: PR rtl-optimization/57829 * simplify-rtx.c (simplify_binary_operation_1) <case IOR>: Ensure that mask bits outside of mode are just sign-extension from mode to HWI. * gcc.c-torture/execute/pr57829.c: New test. Added: branches/gcc-4_8-branch/gcc/testsuite/gcc.c-torture/execute/pr57829.c Modified: branches/gcc-4_8-branch/gcc/ChangeLog branches/gcc-4_8-branch/gcc/simplify-rtx.c branches/gcc-4_8-branch/gcc/testsuite/ChangeLog Author: jakub Date: Mon Jul 8 08:17:35 2013 New Revision: 200773 URL: http://gcc.gnu.org/viewcvs?rev=200773&root=gcc&view=rev Log: PR rtl-optimization/57829 * simplify-rtx.c (simplify_binary_operation_1) <case IOR>: Ensure that mask bits outside of mode are just sign-extension from mode to HWI. * gcc.c-torture/execute/pr57829.c: New test. Added: branches/gcc-4_7-branch/gcc/testsuite/gcc.c-torture/execute/pr57829.c Modified: branches/gcc-4_7-branch/gcc/ChangeLog branches/gcc-4_7-branch/gcc/simplify-rtx.c branches/gcc-4_7-branch/gcc/testsuite/ChangeLog
*** Bug 29589 has been marked as a duplicate of this bug. ***