[Bug rtl-optimization/57967] New: Incorrect code generated on ARM with -fexpensive-optimizations
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57967 Bug ID: 57967 Summary: Incorrect code generated on ARM with -fexpensive-optimizations Product: gcc Version: 4.6.3 Status: UNCONFIRMED Severity: normal Priority: P3 Component: rtl-optimization Assignee: unassigned at gcc dot gnu.org Reporter: daniel.blaukopf at oracle dot com Created attachment 30543 --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=30543&action=edit Two functions, f1 and f2 should compile to almost the same result. The compiled code for f1 throws away the x0 and x1 parameters. In situations results of a computation that go into the top 16-bits of a 32-bit return value are optimized out. A small change to the source makes the problem go away. In the example provided, changing the order of function parameters provides the problem. - Reproduced on the ARM GCC 4.6.3 in Ubuntu 12.04 (both hard and soft float) by inspection of generated assembly - Reproduced on the Linaro GCC 4.7.2 unsupported 2012.09 toolchain from https://launchpad.net/linaro-toolchain-unsupported/, verified by inspection of generated assembly and by running a test case. + arm-linux-gnueabihf-gcc -v --save-temps -Wall -Wextra -shared -O -fexpensive-optimizations gcc-bug.c -o libgcc-bug.so Using built-in specs. COLLECT_GCC=arm-linux-gnueabihf-gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc/arm-linux-gnueabihf/4.6/lto-wrapper Target: arm-linux-gnueabihf Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.6.3-1ubuntu5' --with-bugurl=file:///usr/share/doc/gcc-4.6/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.6 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/arm-linux-gnueabihf/include/c++/4.6.3 --libdir=/usr/lib --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --enable-objc-gc --enable-multilib --disable-sjlj-exceptions --with-arch=armv7-a --with-float=hard --with-fpu=vfpv3-d16 --with-mode=thumb --disable-werror --enable-checking=release --build=i686-linux-gnu --host=i686-linux-gnu --target=arm-linux-gnueabihf --program-prefix=arm-linux-gnueabihf- --includedir=/usr/arm-linux-gnueabihf/include --with-headers=/usr/arm-linux-gnueabihf/include --with-libs=/usr/arm-linux-gnueabihf/lib Thread model: posix gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5) COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wall' '-Wextra' '-shared' '-O' '-fexpensive-optimizations' '-o' 'libgcc-bug.so' '-march=armv7-a' '-mfloat-abi=hard' '-mfpu=vfpv3-d16' '-mthumb' /usr/lib/gcc/arm-linux-gnueabihf/4.6/cc1 -E -quiet -v -imultilib . -imultiarch arm-linux-gnueabihf gcc-bug.c -march=armv7-a -mfloat-abi=hard -mfpu=vfpv3-d16 -mthumb -Wall -Wextra -fexpensive-optimizations -O -fpch-preprocess -fstack-protector -o gcc-bug.i ignoring duplicate directory "/usr/lib/gcc/arm-linux-gnueabihf/4.6/../../../../arm-linux-gnueabihf/include" ignoring nonexistent directory "/usr/include/arm-linux-gnueabihf" #include "..." search starts here: #include <...> search starts here: /usr/lib/gcc/arm-linux-gnueabihf/4.6/include /usr/lib/gcc/arm-linux-gnueabihf/4.6/include-fixed /usr/arm-linux-gnueabihf/include /usr/include End of search list. COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wall' '-Wextra' '-shared' '-O' '-fexpensive-optimizations' '-o' 'libgcc-bug.so' '-march=armv7-a' '-mfloat-abi=hard' '-mfpu=vfpv3-d16' '-mthumb' /usr/lib/gcc/arm-linux-gnueabihf/4.6/cc1 -fpreprocessed gcc-bug.i -quiet -dumpbase gcc-bug.c -march=armv7-a -mfloat-abi=hard -mfpu=vfpv3-d16 -mthumb -auxbase gcc-bug -O -Wall -Wextra -version -fexpensive-optimizations -fstack-protector -o gcc-bug.s GNU C (Ubuntu/Linaro 4.6.3-1ubuntu5) version 4.6.3 (arm-linux-gnueabihf) compiled by GNU C version 4.6.3, GMP version 5.0.2, MPFR version 3.1.0-p3, MPC version 0.9 GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072 GNU C (Ubuntu/Linaro 4.6.3-1ubuntu5) version 4.6.3 (arm-linux-gnueabihf) compiled by GNU C version 4.6.3, GMP version 5.0.2, MPFR version 3.1.0-p3, MPC version 0.9 GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072 Compiler executable checksum: cc9c163e1be35f3de7fa1357bc204e65 COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wall' '-Wextra' '-shared' '-O' '-fexpensive-optimizations' '-o' 'libgcc-bug.so' '-march=armv7-a' '-mf
[Bug rtl-optimization/57967] Incorrect code generated on ARM with -fexpensive-optimizations
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57967 --- Comment #1 from Daniel Blaukopf --- Created attachment 30544 --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=30544&action=edit Test case that can be run with libgcc-bug.so to show the failure
[Bug rtl-optimization/57967] Incorrect code generated on ARM with -fexpensive-optimizations
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57967 --- Comment #2 from Daniel Blaukopf --- This code: int f1(int x0, int y0, int z0, int x1, int y1, int z1) { int xx = ((x0 << 16) + (x1 - x0) * 0x1234 + 0x8000) >> 16; int yy = ((y0 << 16) + (y1 - y0) * 0x2345 + 0x8000) >> 16; int zz = ((z0 << 16) + (z1 - z0) * 0x3456 + 0x8000) >> 16; return (xx << 16) | (yy << 8) | zz; } Compiles (incorrectly) to: f1: @ args = 8, pretend = 0, frame = 0 @ frame_needed = 0, uses_anonymous_args = 0 @ link register save eliminated. ldrr0, [sp, #4] subsr0, r0, r2 movwr3, #13398 mulr0, r3, r0 addr2, r0, r2, lsl #16 ldrr3, [sp, #0] subsr3, r3, r1 movwr0, #9029 mulr3, r0, r3 addr1, r3, r1, lsl #16 addr1, r1, #32768 asrr1, r1, #16 lslr1, r1, #8 addr0, r2, #32768 orrr0, r1, r0, asr #16 bxlr This almost identical code: int f2(int x0, int x1, int y0, int y1, int z0, int z1) { int xx = ((x0 << 16) + (x1 - x0) * 0x1234 + 0x8000) >> 16; int yy = ((y0 << 16) + (y1 - y0) * 0x2345 + 0x8000) >> 16; int zz = ((z0 << 16) + (z1 - z0) * 0x3456 + 0x8000) >> 16; return (xx << 16) | (yy << 8) | zz; } Compiles (correctly) to: f2: @ args = 8, pretend = 0, frame = 0 @ frame_needed = 0, uses_anonymous_args = 0 @ link register save eliminated. push{r4, r5} ldrr4, [sp, #8] subsr3, r3, r2 movwr5, #9029 mulr3, r5, r3 addr2, r3, r2, lsl #16 addr2, r2, #32768 asrr2, r2, #16 subsr1, r1, r0 movwr3, #4660 mulr1, r3, r1 addr0, r1, r0, lsl #16 addr0, r0, #32768 asrr0, r0, #16 lslr0, r0, #16 orrr2, r0, r2, lsl #8 ldrr0, [sp, #12] subsr0, r0, r4 movwr3, #13398 mulr0, r3, r0 addr4, r0, r4, lsl #16 addr0, r4, #32768 orrr0, r2, r0, asr #16 pop{r4, r5} bxlr