Optimizations and the -pedantic option

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi all,

I've been experimenting with tail call optimization in C and I wrote
the following recursive function:

int recur_q(int *p)
{
    const int j = 1;
    if (*p <= 0) return 0;
    --*p;
    return *p + j + recur_q(p);
}

The code above was compiled with -O2.
I noticed that with the -pedantic option, the compiler was able to
transform the recursive call in a loop, but the same optimization
didn't happen without -pedantic. Look at the bottom of the post for
the full assembly listings.

I also tried with different combinations of -ansi and -std=..., but
the only option that seems to affect the generated code is -pedantic.

What is the reason for this behaviour?

   TIA
   Dario Saccavino

----- Full log -----

$ gcc --version

gcc (GCC) 4.1.2 (Ubuntu 4.1.2-0ubuntu4)

$ uname -m

i686

$ gcc -O2 -S file.c -o file1.s && cat file1.s

        .file   "file.c"
        .text
        .p2align 4,,15
.globl recur_q
        .type   recur_q, @function
recur_q:
        pushl   %ebp
        xorl    %eax, %eax
        movl    %esp, %ebp
        pushl   %ebx
        subl    $4, %esp
        movl    8(%ebp), %edx
        movl    (%edx), %ebx
        testl   %ebx, %ebx
        jle     .L4
        leal    -1(%ebx), %eax
        movl    %eax, (%edx)
        movl    %edx, (%esp)
        call    recur_q
        addl    %ebx, %eax
.L4:
        addl    $4, %esp
        popl    %ebx
        popl    %ebp
        ret
        .size   recur_q, .-recur_q
        .ident  "GCC: (GNU) 4.1.2 (Ubuntu 4.1.2-0ubuntu4)"
        .section        .note.GNU-stack,"",@progbits

$ gcc -O2 -pedantic -S file.c -o file2.s && cat file2.s

        .file   "file.c"
        .text
        .p2align 4,,15
.globl recur_q
        .type   recur_q, @function
recur_q:
        pushl   %ebp
        xorl    %eax, %eax
        movl    %esp, %ebp
        movl    8(%ebp), %ecx
        movl    (%ecx), %edx
        testl   %edx, %edx
        jle     .L5
        .p2align 4,,7
.L6:
        subl    $1, %edx
        testl   %edx, %edx
        leal    1(%eax,%edx), %eax
        jne     .L6
        movl    $0, (%ecx)
.L5:
        popl    %ebp
        ret
        .size   recur_q, .-recur_q
        .ident  "GCC: (GNU) 4.1.2 (Ubuntu 4.1.2-0ubuntu4)"
        .section        .note.GNU-stack,"",@progbits

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux