[all-commits] [llvm/llvm-project] d6957d: [X86] fuse constant addition after sbb (#184541)

Takashi Idobe via All-commits all-commits at lists.llvm.org
Wed Mar 25 15:48:59 PDT 2026


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: d6957d2140c96dd5d821c3dd69712be0edd38cee
      https://github.com/llvm/llvm-project/commit/d6957d2140c96dd5d821c3dd69712be0edd38cee
  Author: Takashi Idobe <idobetakashi at gmail.com>
  Date:   2026-03-25 (Wed, 25 Mar 2026)

  Changed paths:
    M llvm/lib/Target/X86/X86ISelLowering.cpp
    M llvm/test/CodeGen/X86/apx/long-instruction-fixup.ll
    M llvm/test/CodeGen/X86/apx/sbb.ll
    A llvm/test/CodeGen/X86/sbb-add-constant.ll
    M llvm/test/CodeGen/X86/select_const.ll

  Log Message:
  -----------
  [X86] fuse constant addition after sbb (#184541)

Resolves: https://github.com/llvm/llvm-project/issues/171676
Related: https://github.com/llvm/llvm-project/pull/185117 (AArch64 side)

The issue points out that `Fold ADD(ADC(Y,0,W),X) -> ADC(X,Y,W)` is
optimized and that SBB can be optimized similarly:
`Fold ADD(SBB(Y,0,W),C) -> SBB(Y,-C,W)`. 

With the changes from this branch, a new clang will compile the example
code:

```c
#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;
}
```

And it's optimized for the sub case as well, instead of emitting a leaq
on x86, it folds it in:

```asm
f:
	movq	%rdi, %rax
	addq	%rsi, %rax
	adcq	$10, %rax
	retq
g:
	movq	%rdi, %rax
	subq	%rsi, %rax
	sbbq	$-10, %rax
	retq
```



To unsubscribe from these emails, change your notification settings at https://github.com/llvm/llvm-project/settings/notifications


More information about the All-commits mailing list