PATCH COMMITTED: Use conditional moves in groups of assignments

Richard Guenther richard.guenther@gmail.com
Sun Jan 15 19:16:00 GMT 2006


On 14 Jan 2006 18:57:05 -0800, Ian Lance Taylor <ian@airs.com> wrote:
> RTL if-conversion currently only operates on single instructions.
> That is, the then block and the optional else block must only contain
> a single instruction.  Extending them to support multiple instructions
> is fairly complex in the general case.  However, there is one case
> where it is easy to extend them: when the processor supports
> conditional moves, and both the then block and the else block are just
> a series of assignments.  In this case conditional moves can be used
> to handle all the assignments.  For assignments which set variables in
> only one of the blocks, we can use a conditional move which sets a
> register either to itself or to some other value.
>
> Consider this test case:
>
> extern int bar (int, int);
> int foo (int c, int d, int e)
> {
>   int a, b;
>
>   if (c)
>     {
>       a = 10;
>       b = d;
>     }
>   else
>     {
>       a = e;
>       b = 20;
>     }
>   return bar (a, b);
> }
>
> The compiler will currently generate a conditional branch.  With the
> patch I just committed, with -march=i686 -O2 on i686-pc-linux-gnu, the
> compiler will now generate
>
>         movl    $20, %edx
>         movl    %esp, %ebp
>         movl    $10, %eax
>         movl    8(%ebp), %ecx
>         testl   %ecx, %ecx
>         cmovne  12(%ebp), %edx
>         cmove   16(%ebp), %eax
>
> This avoids the conditional branch and will generally be more
> efficient.

Actually the P4 is horribly slow with conditional moves.  Btw, did you see
the patches posted and attached to PR22568 (don't know if that has the
most recent version) that address this problem in a maybe similar way?
It does handle slightly more cases it seems as it tries to move register
loads first.

Richard.



More information about the Gcc-patches mailing list