On Mon, 2006-04-17 at 14:48 +0200, Marcin 'Qrczak' Kowalczyk wrote:
> There is a missing opportunity for optimization.
> 
> int f(void);
> void test(int x) {
>    if (x & 1 ? x == 0 : x > 0) f();
> }
> 
> This is gcc-4.1.0-0.20051206 with some patches by PLD Linux
> Distribution. gcc -S -O2 -fomit-frame-pointer generates the
> following code:
> 
> test:
>         movl    4(%esp), %eax
>         testb   $1, %al
>         je      .L2
>         testl   %eax, %eax
>         sete    %al
>         testb   %al, %al
>         jne     .L9
> .L7:
>         rep ; ret
>         .p2align 4,,7
> .L2:
>         testl   %eax, %eax
>         setg    %al
>         testb   %al, %al
>         je      .L7
> .L9:
>         jmp     f
> 
> It makes little sense to materialize the boolean result of the
> condition in %al, and then to test %al for 0. I would expect
> direct conditional jumps to branches of the outer if.
See bug 15911 in the bugzilla database.  The proposed solution
for that bug will result in something like this for your testcase

test:
        movl    4(%esp), %eax
        testb   $1, %al
        jne     .L5
        testl   %eax, %eax
        jg      .L7
.L5:
        rep ; ret
        .p2align 4,,7
.L7:
        jmp     f

The patch referenced in the bugzilla database is not yet complete --
it's still a work in progress.

jeff

Reply via email to