Skip to content

[DAGCombiner] Require a 0/1 carry-in when rebuilding a borrow chain - #223396

Merged
chfast merged 1 commit into
llvm:mainfrom
chfast:fix/222839-usubo-carry-boolean
Sep 14, 2026
Merged

chfast merged 1 commit into
llvm:mainfrom
chfast:fix/222839-usubo-carry-boolean

Conversation

@chfast

@chfast chfast commented Sep 14, 2026

Copy link
Copy Markdown
Member

combineOrOfSetCCToUSUBOCarry (aae871b) rewrites

carry_out = (A <u B) | ((A == B) & carry_in)

to USUBO_CARRY(A, B, carry_in), accepting any value as carry_in.

USUBO_CARRY's third operand is specified as "1 if and only if there is an incoming carry/borrow", so 0 and 1 are its only meaningful values. Unlike SADDO_CARRY/SSUBO_CARRY it does not fall back to getBooleanContents for wider types. The matched and is bitwise against a 0/1 setcc, so only bit 0 of carry_in reaches carry_out and nothing else constrains it; a carry_in with any other bit set is outside the operand's contract and miscompiles.

Require carry_in to be known 0/1. Check known bits rather than look for an and carry_in, 1: only bit 0 is demanded inside the and, so such a mask is usually folded away before this combine runs. An i1 carry_in, which is what a real borrow chain produces, passes trivially.

Fixes #222839.

Assisted-by: Claude Code

combineOrOfSetCCToUSUBOCarry (aae871b) rewrites

  carry_out = (A <u B) | ((A == B) & carry_in)

to USUBO_CARRY(A, B, carry_in), accepting any value as carry_in.

USUBO_CARRY's third operand is specified as "1 if and only if there is an
incoming carry/borrow", so 0 and 1 are its only meaningful values. Unlike
SADDO_CARRY/SSUBO_CARRY it does not fall back to getBooleanContents for
wider types. The matched `and` is bitwise against a 0/1 setcc, so only bit 0
of carry_in reaches carry_out and nothing else constrains it; a carry_in
with any other bit set is outside the operand's contract and miscompiles.

Require carry_in to be known 0/1. Check known bits rather than look for an
`and carry_in, 1`: only bit 0 is demanded inside the `and`, so such a mask
is usually folded away before this combine runs. An i1 carry_in, which is
what a real borrow chain produces, passes trivially.

Fixes llvm#222839.

Assisted-by: Claude Code
@llvmorg-github-actions

llvmorg-github-actions Bot commented Sep 14, 2026 •

Copy link
Copy Markdown

@llvm/pr-subscribers-backend-x86
@llvm/pr-subscribers-llvm-selectiondag

@llvm/pr-subscribers-backend-aarch64

Author: Paweł Bylica (chfast)

Changes

combineOrOfSetCCToUSUBOCarry (aae871b) rewrites

carry_out = (A <u B) | ((A == B) & carry_in)

to USUBO_CARRY(A, B, carry_in), accepting any value as carry_in.

USUBO_CARRY's third operand is specified as "1 if and only if there is an incoming carry/borrow", so 0 and 1 are its only meaningful values. Unlike SADDO_CARRY/SSUBO_CARRY it does not fall back to getBooleanContents for wider types. The matched and is bitwise against a 0/1 setcc, so only bit 0 of carry_in reaches carry_out and nothing else constrains it; a carry_in with any other bit set is outside the operand's contract and miscompiles.

Require carry_in to be known 0/1. Check known bits rather than look for an and carry_in, 1: only bit 0 is demanded inside the and, so such a mask is usually folded away before this combine runs. An i1 carry_in, which is what a real borrow chain produces, passes trivially.

Fixes #222839.

Assisted-by: Claude Code


Full diff: https://github.com/llvm/llvm-project/pull/223396.diff

3 Files Affected:

  • (modified) llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp (+7)
  • (modified) llvm/test/CodeGen/AArch64/cgp-usubo.ll (+19)
  • (modified) llvm/test/CodeGen/X86/subcarry.ll (+90)
diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index 5567fcaf7fcb3..97927b82dae93 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -3970,6 +3970,13 @@ static SDValue combineOrOfSetCCToUSUBOCarry(SDNode *N, SelectionDAG &DAG,
                                                     *DAG.getContext(), IntVT)))
     return SDValue();
 
+  // USUBO_CARRY's carry-in must be 0 or 1, which the matched pattern does not
+  // guarantee.
+  if (!DAG.MaskedValueIsZero(
+          CarryIn,
+          APInt::getBitsSetFrom(CarryIn.getScalarValueSizeInBits(), 1)))
+    return SDValue();
+
   SDLoc DL(N);
   SDVTList VTs = DAG.getVTList(IntVT, N->getValueType(0));
   return DAG.getNode(ISD::USUBO_CARRY, DL, VTs, A, B, CarryIn).getValue(1);
diff --git a/llvm/test/CodeGen/AArch64/cgp-usubo.ll b/llvm/test/CodeGen/AArch64/cgp-usubo.ll
index 71a873014a61f..28e50d0abbceb 100644
--- a/llvm/test/CodeGen/AArch64/cgp-usubo.ll
+++ b/llvm/test/CodeGen/AArch64/cgp-usubo.ll
@@ -320,3 +320,22 @@ end:
   ret i1 %ov
 }
 
+
+; Negative test: %c is not known to be 0/1 and cannot be a borrow-in.
+; See https://github.com/llvm/llvm-project/issues/222839.
+define i8 @no_subcarry_carry_in_not_bool(i64 %a, i64 %b, i8 %c) nounwind {
+; CHECK-LABEL: no_subcarry_carry_in_not_bool:
+; CHECK:       // %bb.0:
+; CHECK-NEXT:    cmp x0, x1
+; CHECK-NEXT:    cset w8, eq
+; CHECK-NEXT:    and w8, w8, w2
+; CHECK-NEXT:    csinc w0, w8, wzr, hs
+; CHECK-NEXT:    ret
+  %ult = icmp ult i64 %a, %b
+  %ultz = zext i1 %ult to i8
+  %eq = icmp eq i64 %a, %b
+  %eqz = zext i1 %eq to i8
+  %and = and i8 %eqz, %c
+  %or = or i8 %ultz, %and
+  ret i8 %or
+}
diff --git a/llvm/test/CodeGen/X86/subcarry.ll b/llvm/test/CodeGen/X86/subcarry.ll
index ca6a623a2b8f3..af1f9b6d9936a 100644
--- a/llvm/test/CodeGen/X86/subcarry.ll
+++ b/llvm/test/CodeGen/X86/subcarry.ll
@@ -1606,3 +1606,93 @@ define <4 x i1> @no_subcarry_vector(<4 x i32> %x0, <4 x i32> %x1, <4 x i32> %y0,
   ret <4 x i1> %br
 }
 
+; Negative test: %c is not known to be 0/1 and cannot be a borrow-in.
+; See https://github.com/llvm/llvm-project/issues/222839.
+define i8 @no_subcarry_carry_in_not_bool(i64 %a, i64 %b, i8 %c) nounwind {
+; X64-LABEL: no_subcarry_carry_in_not_bool:
+; X64:       # %bb.0:
+; X64-NEXT:    cmpq %rsi, %rdi
+; X64-NEXT:    setb %cl
+; X64-NEXT:    sete %al
+; X64-NEXT:    andb %dl, %al
+; X64-NEXT:    orb %cl, %al
+; X64-NEXT:    retq
+;
+; X86-LABEL: no_subcarry_carry_in_not_bool:
+; X86:       # %bb.0:
+; X86-NEXT:    pushl %ebx
+; X86-NEXT:    pushl %edi
+; X86-NEXT:    pushl %esi
+; X86-NEXT:    movl 24(%esp), %eax
+; X86-NEXT:    movl 28(%esp), %ecx
+; X86-NEXT:    movl 16(%esp), %edx
+; X86-NEXT:    movl 20(%esp), %esi
+; X86-NEXT:    cmpl %eax, %edx
+; X86-NEXT:    movl %esi, %edi
+; X86-NEXT:    sbbl %ecx, %edi
+; X86-NEXT:    setb %bl
+; X86-NEXT:    xorl %ecx, %esi
+; X86-NEXT:    xorl %eax, %edx
+; X86-NEXT:    orl %esi, %edx
+; X86-NEXT:    sete %al
+; X86-NEXT:    andb 32(%esp), %al
+; X86-NEXT:    orb %bl, %al
+; X86-NEXT:    popl %esi
+; X86-NEXT:    popl %edi
+; X86-NEXT:    popl %ebx
+; X86-NEXT:    retl
+  %ult = icmp ult i64 %a, %b
+  %ultz = zext i1 %ult to i8
+  %eq = icmp eq i64 %a, %b
+  %eqz = zext i1 %eq to i8
+  %and = and i8 %eqz, %c
+  %or = or i8 %ultz, %and
+  ret i8 %or
+}
+
+; %c is known to be 0/1 here, so the borrow chain is still rebuilt. Deliberately
+; not an i1 carry: that would pass the check trivially and would not catch a
+; regression to a structural test for the carry-in.
+define i8 @subcarry_carry_in_known_bool(i64 %a, i64 %b, i8 %c) nounwind {
+; X64-LABEL: subcarry_carry_in_known_bool:
+; X64:       # %bb.0:
+; X64-NEXT:    shrb $7, %dl
+; X64-NEXT:    addb $-1, %dl
+; X64-NEXT:    sbbq %rsi, %rdi
+; X64-NEXT:    setb %al
+; X64-NEXT:    retq
+;
+; X86-LABEL: subcarry_carry_in_known_bool:
+; X86:       # %bb.0:
+; X86-NEXT:    pushl %ebx
+; X86-NEXT:    pushl %edi
+; X86-NEXT:    pushl %esi
+; X86-NEXT:    movl 24(%esp), %eax
+; X86-NEXT:    movl 28(%esp), %ecx
+; X86-NEXT:    movl 16(%esp), %edx
+; X86-NEXT:    movl 20(%esp), %esi
+; X86-NEXT:    movzbl 32(%esp), %ebx
+; X86-NEXT:    shrb $7, %bl
+; X86-NEXT:    cmpl %eax, %edx
+; X86-NEXT:    movl %esi, %edi
+; X86-NEXT:    sbbl %ecx, %edi
+; X86-NEXT:    setb %bh
+; X86-NEXT:    xorl %ecx, %esi
+; X86-NEXT:    xorl %eax, %edx
+; X86-NEXT:    orl %esi, %edx
+; X86-NEXT:    sete %al
+; X86-NEXT:    andb %bl, %al
+; X86-NEXT:    orb %bh, %al
+; X86-NEXT:    popl %esi
+; X86-NEXT:    popl %edi
+; X86-NEXT:    popl %ebx
+; X86-NEXT:    retl
+  %cin = lshr i8 %c, 7
+  %ult = icmp ult i64 %a, %b
+  %ultz = zext i1 %ult to i8
+  %eq = icmp eq i64 %a, %b
+  %eqz = zext i1 %eq to i8
+  %and = and i8 %eqz, %cin
+  %or = or i8 %ultz, %and
+  ret i8 %or
+}

@RKSimon RKSimon left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@chfast
chfast merged commit 4eba5bc into llvm:main Sep 14, 2026
16 checks passed
@topperc

topperc commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

Is it really true that UADDO_CARRY/USUBO_CARRY don't conform to getBooleanContents? Type legalization uses PromoteIntRes_Overflow which uses PromoteTargetBoolean and getBoolExtOrTrunc.

anilkund pushed a commit to anilkund/llvm-project that referenced this pull request Sep 16, 2026
…lvm#223396)

combineOrOfSetCCToUSUBOCarry (aae871b) rewrites

  carry_out = (A <u B) | ((A == B) & carry_in)

to USUBO_CARRY(A, B, carry_in), accepting any value as carry_in.

USUBO_CARRY's third operand is specified as "1 if and only if there is
an incoming carry/borrow", so 0 and 1 are its only meaningful values.
Unlike SADDO_CARRY/SSUBO_CARRY it does not fall back to
getBooleanContents for wider types. The matched `and` is bitwise against
a 0/1 setcc, so only bit 0 of carry_in reaches carry_out and nothing
else constrains it; a carry_in with any other bit set is outside the
operand's contract and miscompiles.

Require carry_in to be known 0/1. Check known bits rather than look for
an `and carry_in, 1`: only bit 0 is demanded inside the `and`, so such a
mask is usually folded away before this combine runs. An i1 carry_in,
which is what a real borrow chain produces, passes trivially.

Fixes llvm#222839.

Assisted-by: Claude Code
@chfast

chfast commented Sep 16, 2026

Copy link
Copy Markdown
Member Author

Is it really true that UADDO_CARRY/USUBO_CARRY don't conform to getBooleanContents? Type legalization uses PromoteIntRes_Overflow which uses PromoteTargetBoolean and getBoolExtOrTrunc.

You are right here, I got confused by doc "the third is a boolean value that is 1 if and only if there is an incoming carry/borrow".

The fix works somehow: it is correct for ZeroOrOneBooleanContent, it is too restrictive for UndefinedBooleanContent and wrong for ZeroOrNegativeOneBooleanContent, but unreachable for the last case.

The fix focus on matching side: is value "CarryIn" known to be valid target boolean. Should I add proper isKnownTargetBoolean helper? I was not able to find any.

Alternative, is to mask CarryIn. I tried this path and it was causing issues as it sometimes adds new instruction. But I can revisit this path if you think this is worth it.

@topperc

topperc commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

Is it really true that UADDO_CARRY/USUBO_CARRY don't conform to getBooleanContents? Type legalization uses PromoteIntRes_Overflow which uses PromoteTargetBoolean and getBoolExtOrTrunc.

You are right here, I got confused by doc "the third is a boolean value that is 1 if and only if there is an incoming carry/borrow".

The fix works somehow: it is correct for ZeroOrOneBooleanContent, it is too restrictive for UndefinedBooleanContent and wrong for ZeroOrNegativeOneBooleanContent, but unreachable for the last case.

The fix focus on matching side: is value "CarryIn" known to be valid target boolean. Should I add proper isKnownTargetBoolean helper? I was not able to find any.

Alternative, is to mask CarryIn. I tried this path and it was causing issues as it sometimes adds new instruction. But I can revisit this path if you think this is worth it.

In the near term, for the backport can we just disable the combine for anything but ZeroOrOneBooleanContent? I doubt we have test coverage.

topperc added a commit to topperc/llvm-project that referenced this pull request Sep 18, 2026
…ents. NFC

Use 'true' instead of '1'.

Follow up from post commit discussion in llvm#223396.
topperc added a commit that referenced this pull request Sep 20, 2026
…anContent. (#224759)

We need to ensure the carry in matches boolean contents. We are checking
MaskedValueIsZero which is only valid for ZeroOrOneBooleanContent.

We could check ComputeNumSignBits for ZeroOrNegativeOneBooleanContent,
but no in tree target supports USUBO_CARRY and
ZeroOrNegativeOneBooleanContent.

This was an oversight in #223396 due to incorrect documentation for
USUBO_CARRY. I will fix that in a separate patch.
topperc added a commit that referenced this pull request Sep 20, 2026
…ents. NFC (#224764)

Use 'true' instead of '1'.

Follow up from post commit discussion in #223396.
tru pushed a commit that referenced this pull request Sep 23, 2026
…223396)

combineOrOfSetCCToUSUBOCarry (aae871b) rewrites

  carry_out = (A <u B) | ((A == B) & carry_in)

to USUBO_CARRY(A, B, carry_in), accepting any value as carry_in.

USUBO_CARRY's third operand is specified as "1 if and only if there is
an incoming carry/borrow", so 0 and 1 are its only meaningful values.
Unlike SADDO_CARRY/SSUBO_CARRY it does not fall back to
getBooleanContents for wider types. The matched `and` is bitwise against
a 0/1 setcc, so only bit 0 of carry_in reaches carry_out and nothing
else constrains it; a carry_in with any other bit set is outside the
operand's contract and miscompiles.

Require carry_in to be known 0/1. Check known bits rather than look for
an `and carry_in, 1`: only bit 0 is demanded inside the `and`, so such a
mask is usually folded away before this combine runs. An i1 carry_in,
which is what a real borrow chain produces, passes trivially.

Fixes #222839.

Assisted-by: Claude Code
(cherry picked from commit 4eba5bc)
tru pushed a commit that referenced this pull request Sep 23, 2026
…anContent. (#224759)

We need to ensure the carry in matches boolean contents. We are checking
MaskedValueIsZero which is only valid for ZeroOrOneBooleanContent.

We could check ComputeNumSignBits for ZeroOrNegativeOneBooleanContent,
but no in tree target supports USUBO_CARRY and
ZeroOrNegativeOneBooleanContent.

This was an oversight in #223396 due to incorrect documentation for
USUBO_CARRY. I will fix that in a separate patch.

(cherry picked from commit 51d2905)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[SelectionDAG] Wrong code at -O1

3 participants