Bug 54068 - Compiler passes wrong argument to function when using optimize attribute with -O3
Summary: Compiler passes wrong argument to function when using optimize attribute with...
Status: RESOLVED DUPLICATE of bug 60062
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 4.6.3
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-07-22 23:39 UTC by Guy Voncken
Modified: 2021-08-08 22:22 UTC (History)
2 users (show)

See Also:
Host:
Target: i?86-*-*
Build:
Known to work:
Known to fail: 4.4.7, 4.5.4, 4.6.3, 4.7.1, 4.8.0
Last reconfirmed: 2012-07-23 00:00:00


Attachments
Compile this ii file and run it; it shows the described problem (3.31 KB, application/octet-stream)
2012-07-22 23:39 UTC, Guy Voncken
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Guy Voncken 2012-07-22 23:39:41 UTC
Created attachment 27855 [details]
Compile this ii file and run it; it shows the described problem

In the attached example, gcc passes the wrong parameter to a function. The problem only was observed on a Ubuntu 12.04 i386 platform. It did not occur  not on Ubuntu 12.04 amd64.

The problem can be avoided by a) omitting the inline statement or b) by commenting out the optimisation attribute in the source code.

Here's the output of    gcc -v -save-temps -o it it.cpp

Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/i686-linux-gnu/4.6/lto-wrapper
Target: i686-linux-gnu
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/include/c++/4.6 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --enable-objc-gc --enable-targets=all --disable-werror --with-arch-32=i686 --with-tune=generic --enable-checking=release --build=i686-linux-gnu --host=i686-linux-gnu --target=i686-linux-gnu
Thread model: posix
gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5) 
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-o' 'it' '-mtune=generic' '-march=i686'
 /usr/lib/gcc/i686-linux-gnu/4.6/cc1plus -E -quiet -v -imultilib . -imultiarch i386-linux-gnu -D_GNU_SOURCE it.cpp -mtune=generic -march=i686 -fpch-preprocess -fstack-protector -o it.ii
ignoring nonexistent directory "/usr/local/include/i386-linux-gnu"
ignoring nonexistent directory "/usr/lib/gcc/i686-linux-gnu/4.6/../../../../i686-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/include/c++/4.6
 /usr/include/c++/4.6/i686-linux-gnu/.
 /usr/include/c++/4.6/backward
 /usr/lib/gcc/i686-linux-gnu/4.6/include
 /usr/local/include
 /usr/lib/gcc/i686-linux-gnu/4.6/include-fixed
 /usr/include/i386-linux-gnu
 /usr/include
End of search list.
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-o' 'it' '-mtune=generic' '-march=i686'
 /usr/lib/gcc/i686-linux-gnu/4.6/cc1plus -fpreprocessed it.ii -quiet -dumpbase it.cpp -mtune=generic -march=i686 -auxbase it -version -fstack-protector -o it.s
GNU C++ (Ubuntu/Linaro 4.6.3-1ubuntu5) version 4.6.3 (i686-linux-gnu)
	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=89 --param ggc-min-heapsize=111959
GNU C++ (Ubuntu/Linaro 4.6.3-1ubuntu5) version 4.6.3 (i686-linux-gnu)
	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=89 --param ggc-min-heapsize=111959
Compiler executable checksum: 2ed62271b86e2b75137544459bab1a81
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-o' 'it' '-mtune=generic' '-march=i686'
 as --32 -o it.o it.s
COMPILER_PATH=/usr/lib/gcc/i686-linux-gnu/4.6/:/usr/lib/gcc/i686-linux-gnu/4.6/:/usr/lib/gcc/i686-linux-gnu/:/usr/lib/gcc/i686-linux-gnu/4.6/:/usr/lib/gcc/i686-linux-gnu/
LIBRARY_PATH=/usr/lib/gcc/i686-linux-gnu/4.6/:/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/:/usr/lib/gcc/i686-linux-gnu/4.6/../../../../lib/:/lib/i386-linux-gnu/:/lib/../lib/:/usr/lib/i386-linux-gnu/:/usr/lib/../lib/:/usr/lib/gcc/i686-linux-gnu/4.6/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-o' 'it' '-mtune=generic' '-march=i686'
 /usr/lib/gcc/i686-linux-gnu/4.6/collect2 --sysroot=/ --build-id --no-add-needed --as-needed --eh-frame-hdr -m elf_i386 --hash-style=gnu -dynamic-linker /lib/ld-linux.so.2 -z relro -o it /usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crt1.o /usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crti.o /usr/lib/gcc/i686-linux-gnu/4.6/crtbegin.o -L/usr/lib/gcc/i686-linux-gnu/4.6 -L/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu -L/usr/lib/gcc/i686-linux-gnu/4.6/../../../../lib -L/lib/i386-linux-gnu -L/lib/../lib -L/usr/lib/i386-linux-gnu -L/usr/lib/../lib -L/usr/lib/gcc/i686-linux-gnu/4.6/../../.. it.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/i686-linux-gnu/4.6/crtend.o /usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crtn.o
Comment 1 Richard Biener 2012-07-23 08:25:58 UTC
Confirmed.  Note that I would not expect

void myfn (int a, int b) __attribute__((optimize("-O3")));

to work reliably.  Attribute optimize is supposed to be used for debugging
only.  C testcase, compile with -O0 -m32:

extern void abort (void);
void myfn (int a, int b) __attribute__((optimize("-O3")));
static inline void showme (int b)
{
   if (b != 12)
     abort ();
}
void myfn (int a, int b)
{
   showme (b);
}
int main ()
{
   myfn (5, 12);
   return 0;
}

I suppose this is the known issue with tail-call optimization which
may get upset if you have parts compiled with -O0 and parts with -O3.
Comment 2 Steven Bosscher 2012-07-23 10:17:07 UTC
(In reply to comment #1)
> I suppose this is the known issue with tail-call optimization which
> may get upset if you have parts compiled with -O0 and parts with -O3.

If this is a known issue, then I'd expect there is already a PR for this...?
Comment 3 Andrew Pinski 2021-08-08 22:22:20 UTC
251901a027bf (Jakub Jelinek            2014-02-06 11:54:20 +0100  6619)       /* Caller and callee must agree on the calling convention, so
251901a027bf (Jakub Jelinek            2014-02-06 11:54:20 +0100  6620)          checking here just optimize means that with
251901a027bf (Jakub Jelinek            2014-02-06 11:54:20 +0100  6621)          __attribute__((optimize (...))) caller could use regparm convention
251901a027bf (Jakub Jelinek            2014-02-06 11:54:20 +0100  6622)          and callee not, or vice versa.  Instead look at whether the callee
251901a027bf (Jakub Jelinek            2014-02-06 11:54:20 +0100  6623)          is optimized or not.  */

So dup of bug 60062.

*** This bug has been marked as a duplicate of bug 60062 ***