Issue 171676
Summary constant addition not merged into subtract-with-borrow
Labels new issue
Assignees
Reporter purplesyringa
    [Godbolt](https://godbolt.org/z/rWY51x7GE)

```cpp
#include <stdint.h>

uint64_t f(uint64_t a, uint64_t b) {
    uint64_t x;
    x += __builtin_add_overflow(a, b, &x);
    return x + 10;
}

uint64_t g(uint64_t a, uint64_t b) {
    uint64_t x;
    x -= __builtin_sub_overflow(a, b, &x);
    return x + 10;
}
```

On x86-64, I expected the lowering of `f` to use `adc ..., 10` and the lowering of `g` to use `sbb ..., -10`. `f` works, `g` doesn't:

```asm
f(unsigned long, unsigned long):
        mov     rax, rdi
        add     rax, rsi
 adc     rax, 10
        ret

g(unsigned long, unsigned long):
        sub rdi, rsi
        sbb     rdi, 0
        lea     rax, [rdi + 10]
 ret
```

Same on aarch64. The addition of 10 could be integrated into `sbc`:

```asm
f(unsigned long, unsigned long):
        mov     w8, #10
 adds    x9, x0, x1
        adc     x0, x9, x8
 ret

g(unsigned long, unsigned long):
        subs    x8, x0, x1
 sbc     x8, x8, xzr
        add     x0, x8, #10
        ret
```
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to