Bug 36082 - -finline-functions miscompiles tail recursion
Summary: -finline-functions miscompiles tail recursion
Status: RESOLVED DUPLICATE of bug 21920
Alias: None
Product: gcc
Classification: Unclassified
Component: regression (show other bugs)
Version: 4.1.2
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
Depends on:
Reported: 2008-04-29 21:12 UTC by Jan "Yenya" Kasprzak
Modified: 2008-04-29 21:39 UTC (History)
24 users (show)

See Also:
Host: x86_64-redhat-linux
Target: x86_64-redhat-linux
Build: x86_64-redhat-linux
Known to work:
Known to fail:
Last reconfirmed:


Note You need to log in before you can comment on or make changes to this bug.
Description Jan "Yenya" Kasprzak 2008-04-29 21:12:48 UTC
In my program I use recursive quick-sort, which is apparently miscompiled by my gcc (Fedora 8, x86_64), when I use -O3 or -O2 -finline-functions. With these options, the data get out of the qsort routine only partially sorted. With -O2 it is OK, and when I add a debugging printf() at the end of the qsort routine (thus disabling the tail-call recursion), it also works (even with -O3 or -O2 -finline-functions).

The test program is at http://www.fi.muni.cz/~kas/data/sorttest.c - try to compile it and run on 64-bit x86_64 system (it is non-portable, but it is a design decision) with -O2 and with -O3. Then uncomment the debugging code
near the end of the file, and try it again.
Comment 1 Andrew Pinski 2008-04-29 21:24:28 UTC
It is more than unportable, it contains undefined code as you violate C/C++ aliasing rules:

	d = *(unsigned long *)e1;
	*(unsigned long *)e1 = *(unsigned long *)e2;
	*(unsigned long *)e2 = d;

You are accessing a bucket_entry_t as unsigned long.

*** This bug has been marked as a duplicate of 21920 ***
Comment 2 Jan "Yenya" Kasprzak 2008-04-29 21:39:12 UTC
Interesting. When rewritten to use union, it works even with -O3. Thanks!