Issue 243058
Summary [DAGCombiner] Signed 2-word compare `x1 <s y1 || (x1 == y1 && x0 <u y0)` is not lowered to a borrow chain
Labels missed-optimization, llvm:SelectionDAG
Assignees
Reporter chfast
    A signed compare of 2-word numbers `(x1, x0)` and `(y1, y0)`, as `std::pair`/`std::tuple` `operator<` gives it with a signed high word (https://godbolt.org/z/35qcn1G9f):

```llvm
define i1 @slt2(i64 %x0, i64 %x1, i64 %y0, i64 %y1) {
  %lo = icmp ult i64 %x0, %y0
  %hi = icmp slt i64 %x1, %y1
  %eq = icmp eq i64 %x1, %y1
  %t = and i1 %eq, %lo
  %r = or i1 %hi, %t
  ret i1 %r
}
```

x86-64:

```asm
slt2:
        cmpq    %rdx, %rdi
        setb    %dl
        cmpq    %rcx, %rsi
        setl    %cl
        sete    %al
        andb    %dl, %al
        orb     %cl, %al
        retq
```

AArch64:

```asm
slt2:
        cmp     x1, x3
        cset    w8, eq
        cmp     x0, x2
        csel    w8, wzr, w8, hs
        cmp     x1, x3
        csinc   w0, w8, wzr, ge
        ret
```

Expected, as llc already emits for `icmp slt i128`:

```asm
slt2:                                   # x86-64
        cmpq    %rdx, %rdi
        sbbq    %rcx, %rsi
        setl    %al
        retq
```

```asm
slt2:                                   // AArch64
        cmp     x0, x2
        sbcs    xzr, x1, x3
        cset    w0, lt
        ret
```

#193707 added `combineOrOfSetCCToUSUBOCarry()`, which turns the unsigned form `or(setcc ult A, B, and(setcc eq A, B, Carry))` into `USUBO_CARRY(A, B, Carry)`. It matches only `ult` in the top step. With `slt`, the same pattern could become `SETCCCARRY(A, B, Carry, SETLT)`, the node `IntegerExpandSetCCOperands()` builds for `icmp slt i128` where SETCCCARRY is legal or custom.

For more words only the top step is signed, so the lower steps are already rebuilt. A 4-word signed compare through `__builtin_subcll` (with the sign bits flipped, which InstCombine turns into this `slt` top step) is 13 instructions on x86-64 instead of `cmp`, 3 × `sbb`, `setl`.

Found while optimizing evmone's (Ethereum VM) SLT/SGT instructions on 256-bit words.

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

Reply via email to