With the option -foptimize-sibling-calls tail recursion is performed. Adding -g destroys this optimization. In my opinion, adding compiler option -g should not change the behaviour of the compiled program.
Environment: System: SunOS rungner.nada.kth.se 5.9 Generic_117171-07 sun4u sparc SUNW,Sun-Fire-280R Architecture: sun4 host: sparc-sun-solaris2.9 build: sparc-sun-solaris2.9 target: sparc-sun-solaris2.9 configured with: /afs/.nada.kth.se/pkg/gcc/src/3.4.2/gcc-3.4.2/configure --prefix=/pkg/gcc/3.4.2/os How-To-Repeat: Compile the following program: /* * In this function, it seems that tail recursion is performed both with * * gcc -foptimize-sibling-calls -S tail.c * * and with * * gcc -g -foptimize-sibling-calls -S tail.c * * using gcc 3.4.2 on a SPARC. */ void f(int n) { if(n > 0) f(--n); } /* * In this function, it seems that tail recursion is performed with * * gcc -foptimize-sibling-calls -S tail.c * * but not with * * gcc -g -foptimize-sibling-calls -S tail.c * * using gcc 3.4.2 on a SPARC. */ void g(int n) { if(n > 0) f(--n); return; } int main(int argc, char **argv) { f(10); g(10); } (Preprocessor output is included below.) For the function f(), the assembler output ends with call f, 0 restore .LL1: nop ret restore .LLFE2: .size f, .-f both with and without compiler option -g. For the function g(), however, the assembler output ends with call f, 0 restore .LL3: nop ret restore .size g, .-g if "-g" is *not* used, and with call f, 0 nop .LL3: nop ret restore .LLFE3: .size g, .-g if "-g" *is* used. And now the preprocessor output: # 1 "tail.c" # 1 "<built-in>" # 1 "<command line>" # 1 "tail.c" # 27 "tail.c" void f(int n) { if(n > 0) f(--n); } # 44 "tail.c" void g(int n) { if(n > 0) f(--n); return; } int main(int argc, char **argv) { f(10); g(10); } ------- Additional Comments From enge at nada dot kth dot se 2005-01-21 10:12 ------- Fix: I do not know of a fix. -- Summary: Adding -g disables -foptimize-sibling-calls optimization in some cases Product: gcc Version: 3.4.2 Status: UNCONFIRMED Severity: normal Priority: P3 Component: rtl-optimization AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: enge at nada dot kth dot se CC: gcc-bugs at gcc dot gnu dot org GCC build triplet: sparc-sun-solaris2.9 GCC host triplet: sparc-sun-solaris2.9 GCC target triplet: sparc-sun-solaris2.9 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=19560