Issue 234666
Summary [X86] Branch on the borrow of a 2-word subtraction is split into two branches
Labels backend:X86, missed-optimization
Assignees
Reporter chfast
    Branching on the final borrow of a 2-word subtraction gives two compares and two branches instead of `sub; sbb; jb`. Returning the same borrow as a value is fine.

The case comes from multi-precision integer code (intx).

https://godbolt.org/z/G8PnebMWj

```c
typedef unsigned long long u64;
void g(void);

// 2-word subtraction, branch on the final borrow.
void sub_branch(u64 *x, u64 y0, u64 y1) {
    u64 c0, c1;
    x[0] = __builtin_subcll(x[0], y0, 0, &c0);
    x[1] = __builtin_subcll(x[1], y1, c0, &c1);
    if (c1)
        g();
}

// The same borrow returned as a value.
int sub_value(u64 *x, u64 y0, u64 y1) {
    u64 c0, c1;
    x[0] = __builtin_subcll(x[0], y0, 0, &c0);
    x[1] = __builtin_subcll(x[1], y1, c0, &c1);
    return (int)c1;
}
```

`clang -O2`:

```asm
sub_branch:
        movq    8(%rdi), %rcx
        movq %rcx, %r8
        subq    %rdx, %r8
        subq    %rsi, (%rdi)
 sbbq    $0, %r8
        setb    %al
        movq    %r8, 8(%rdi)
 cmpq    %rdx, %rcx
        jb      g@PLT
        testb   %al, %al
 jne     g@PLT
        retq

sub_value:
        subq    %rsi, (%rdi)
 sbbq    %rdx, 8(%rdi)
        setb    %al
        movzbl  %al, %eax
 retq
```

The IR still has a single `br` on `or i1 %borrow1, %borrow2`; it is split into two branches in codegen. With `-mllvm -jump-is-expensive` the result is `sub; sbb; jb`.

Reproduces with LLVM 23.0.0 (090308199b30) and trunk.

Related: #59846 (the same split on AArch64 loses `cmp; ccmp`).

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to