[x86 PATCH] Improve ix86_expand_int_movcc to allow condition (mask) sharing.

Roger Sayle roger@nextmovesoftware.com
Mon Jan 2 10:50:38 GMT 2023


This patch modifies the way that ix86_expand_int_movcc generates RTL,
to allow the condition mask to be shared/reused between multiple
conditional move sequences.  Such redundancy is common when RTL
if-conversion transforms non-trivial basic blocks.

As a motivating example, consider the new test case:

int a, b, c, d;
int foo(int x)
{
    if (x == 0) {
        a = 3;
        b = 1;
        c = 4;
        d = 1;
    } else {
        a = 5;
        b = 9;
        c = 2;
        d = 7;
    }
    return x;
}

This is currently compiled, with -O2, to:

foo:    cmpl    $1, %edi
        movl    %edi, %eax
        sbbl    %edi, %edi
        andl    $-2, %edi
        addl    $5, %edi
        cmpl    $1, %eax
        sbbl    %esi, %esi
        movl    %edi, a(%rip)
        andl    $-8, %esi
        addl    $9, %esi
        cmpl    $1, %eax
        sbbl    %ecx, %ecx
        movl    %esi, b(%rip)
        andl    $2, %ecx
        addl    $2, %ecx
        cmpl    $1, %eax
        sbbl    %edx, %edx
        movl    %ecx, c(%rip)
        andl    $-6, %edx
        addl    $7, %edx
        movl    %edx, d(%rip)
        ret

Notice that the if-then-else blocks have been if-converted into four
conditional move sequences/assignments, each consisting of cmpl, sbbl,
andl and addl.  However, as the conditions are the same, the cmpl and
sbbl instructions used to generate the mask could be shared by CSE.

This patch enables that, so we now generate:

foo:    cmpl    $1, %edi
        movl    %edi, %eax
        sbbl    %edx, %edx
        movl    %edx, %edi
        movl    %edx, %esi
        movl    %edx, %ecx
        andl    $-6, %edx
        andl    $-2, %edi
        andl    $-8, %esi
        andl    $2, %ecx
        addl    $7, %edx
        addl    $5, %edi
        addl    $9, %esi
        addl    $2, %ecx
        movl    %edx, d(%rip)
        movl    %edi, a(%rip)
        movl    %esi, b(%rip)
        movl    %ecx, c(%rip)
        ret

Notice, the code now contains only a single cmpl and a single sbbl,
with their result being shared (via movl).

This patch has been tested on x86_64-pc-linux-gnu with make bootstrap
and make -k check, both with and without --target_board=unix{-m32},
with no new failures.  Ok for mainline?


2023-01-02  Roger Sayle  <roger@nextmovesoftware.com>

gcc/ChangeLog
	* config/i386/i386-expand.cc (ix86_expand_int_movcc): Rewrite
	RTL expansion to allow condition (mask) to be shared/reused,
	by avoiding overwriting pseudos and adding REG_EQUAL notes.

gcc/testsuite/ChangeLog
	* gcc.target/i386/cmov10.c: New test case.


Thanks in advance,
Roger
--

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: patchif.txt
URL: <https://gcc.gnu.org/pipermail/gcc-patches/attachments/20230102/464ff4f6/attachment-0001.txt>


More information about the Gcc-patches mailing list