llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[X86] Duplicate cmp when sbb and cmov use the same compare, depending on IR order

Open chfast opened this issue 2 months ago • 2 comments

When an icmp feeds both a zext+add (lowered to sbb) and a select (lowered to cmov), the cmp is emitted twice if the add comes first in the IR. sbb clobbers EFLAGS, so the cmp is repeated for the cmov. With the select first, cmov is scheduled before sbb and one cmp is enough.

https://godbolt.org/z/rcr84hrhf

define { i64, i64 } @select_first(i64 %r, i64 %d, i64 %q) {
  %c = icmp uge i64 %r, %d
  %s = select i1 %c, i64 %d, i64 0
  %r1 = sub i64 %r, %s
  %z = zext i1 %c to i64
  %q1 = add i64 %q, %z
  %a = insertvalue { i64, i64 } poison, i64 %r1, 0
  %b = insertvalue { i64, i64 } %a, i64 %q1, 1
  ret { i64, i64 } %b
}

define { i64, i64 } @add_first(i64 %r, i64 %d, i64 %q) {
  %c = icmp uge i64 %r, %d
  %z = zext i1 %c to i64
  %q1 = add i64 %q, %z
  %s = select i1 %c, i64 %d, i64 0
  %r1 = sub i64 %r, %s
  %a = insertvalue { i64, i64 } poison, i64 %r1, 0
  %b = insertvalue { i64, i64 } %a, i64 %q1, 1
  ret { i64, i64 } %b
}

llc -O2:

select_first:
        movq    %rdi, %rax
        xorl    %ecx, %ecx
        cmpq    %rsi, %rdi
        cmovaeq %rsi, %rcx
        sbbq    $-1, %rdx
        subq    %rcx, %rax
        retq

add_first:
        movq    %rdi, %rax
        cmpq    %rsi, %rdi
        sbbq    $-1, %rdx
        xorl    %ecx, %ecx
        cmpq    %rsi, %rdi
        cmovaeq %rsi, %rcx
        subq    %rcx, %rax
        retq

Clang produces the add_first order from:

unsigned long cond_sub(unsigned long r, unsigned long d, unsigned long q, unsigned long* out)
{
    if (r >= d)
    {
        ++q;
        r -= d;
    }
    *out = r;
    return q;
}

Reproduces with LLVM 23.0.0 (090308199b30) and current trunk.

chfast avatar Oct 01 '26 09:10 chfast

@llvm/issue-subscribers-backend-x86

Author: Paweł Bylica (chfast)

When an `icmp` feeds both a `zext`+`add` (lowered to `sbb`) and a `select` (lowered to `cmov`), the `cmp` is emitted twice if the `add` comes first in the IR. `sbb` clobbers EFLAGS, so the `cmp` is repeated for the `cmov`. With the `select` first, `cmov` is scheduled before `sbb` and one `cmp` is enough.

https://godbolt.org/z/rcr84hrhf

define { i64, i64 } @<!-- -->select_first(i64 %r, i64 %d, i64 %q) {
  %c = icmp uge i64 %r, %d
  %s = select i1 %c, i64 %d, i64 0
  %r1 = sub i64 %r, %s
  %z = zext i1 %c to i64
  %q1 = add i64 %q, %z
  %a = insertvalue { i64, i64 } poison, i64 %r1, 0
  %b = insertvalue { i64, i64 } %a, i64 %q1, 1
  ret { i64, i64 } %b
}

define { i64, i64 } @<!-- -->add_first(i64 %r, i64 %d, i64 %q) {
  %c = icmp uge i64 %r, %d
  %z = zext i1 %c to i64
  %q1 = add i64 %q, %z
  %s = select i1 %c, i64 %d, i64 0
  %r1 = sub i64 %r, %s
  %a = insertvalue { i64, i64 } poison, i64 %r1, 0
  %b = insertvalue { i64, i64 } %a, i64 %q1, 1
  ret { i64, i64 } %b
}

llc -O2:

select_first:
        movq    %rdi, %rax
        xorl    %ecx, %ecx
        cmpq    %rsi, %rdi
        cmovaeq %rsi, %rcx
        sbbq    $-1, %rdx
        subq    %rcx, %rax
        retq

add_first:
        movq    %rdi, %rax
        cmpq    %rsi, %rdi
        sbbq    $-1, %rdx
        xorl    %ecx, %ecx
        cmpq    %rsi, %rdi
        cmovaeq %rsi, %rcx
        subq    %rcx, %rax
        retq

Clang produces the add_first order from:

unsigned long cond_sub(unsigned long r, unsigned long d, unsigned long q, unsigned long* out)
{
    if (r &gt;= d)
    {
        ++q;
        r -= d;
    }
    *out = r;
    return q;
}

Reproduces with LLVM 23.0.0 (090308199b30) and current trunk.

Hi I'd like to take this up

222rohan avatar Oct 01 '26 11:10 222rohan