[llvm] [X86] Fix ADOX miscompile by restricting COND_O optimization when EFLAGS are used (PR #220117)

via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 31 17:56:14 PDT 2026


https://github.com/AZero13 updated https://github.com/llvm/llvm-project/pull/220117

>From fc59ca246aaab206771523ae0b93bd742fb42fc9 Mon Sep 17 00:00:00 2001
From: AZero13 <gfunni234 at gmail.com>
Date: Mon, 31 Aug 2026 17:08:05 -0400
Subject: [PATCH 1/2] Pre-commit zf flag error

---
 llvm/test/CodeGen/X86/adox.ll | 54 +++++++++++++++++++++++++++++++++++
 1 file changed, 54 insertions(+)

diff --git a/llvm/test/CodeGen/X86/adox.ll b/llvm/test/CodeGen/X86/adox.ll
index 10f5216b4a45a..1c40468440158 100644
--- a/llvm/test/CodeGen/X86/adox.ll
+++ b/llvm/test/CodeGen/X86/adox.ll
@@ -269,5 +269,59 @@ define i32 @adox_test_sub_i32(i32 %a, i32 %b, i8 %c, i8 %d) {
   ret i32 %res
 }
 
+define i32 @adox_test_zf(i32 %a, i32 %b, i32 %s) {
+; NDD-LABEL: adox_test_zf:
+; NDD:       # %bb.0:
+; NDD-NEXT:    xorl %eax, %eax
+; NDD-NEXT:    addl %edi, %esi
+; NDD-NEXT:    adoxl %eax, %edx
+; NDD-NEXT:    je .LBB8_1
+; NDD-NEXT:  # %bb.2: # %nonzero
+; NDD-NEXT:    movl $222, %eax
+; NDD-NEXT:    retq
+; NDD-NEXT:  .LBB8_1: # %iszero
+; NDD-NEXT:    movl $111, %eax
+; NDD-NEXT:    retq
+;
+; ADX-LABEL: adox_test_zf:
+; ADX:       # %bb.0:
+; ADX-NEXT:    xorl %eax, %eax
+; ADX-NEXT:    addl %esi, %edi
+; ADX-NEXT:    adoxl %eax, %edx
+; ADX-NEXT:    je .LBB8_1
+; ADX-NEXT:  # %bb.2: # %nonzero
+; ADX-NEXT:    movl $222, %eax
+; ADX-NEXT:    retq
+; ADX-NEXT:  .LBB8_1: # %iszero
+; ADX-NEXT:    movl $111, %eax
+; ADX-NEXT:    retq
+;
+; NOADX-LABEL: adox_test_zf:
+; NOADX:       # %bb.0:
+; NOADX-NEXT:    xorl %eax, %eax
+; NOADX-NEXT:    addl %esi, %edi
+; NOADX-NEXT:    seto %al
+; NOADX-NEXT:    addl %edx, %eax
+; NOADX-NEXT:    je .LBB8_1
+; NOADX-NEXT:  # %bb.2: # %nonzero
+; NOADX-NEXT:    movl $222, %eax
+; NOADX-NEXT:    retq
+; NOADX-NEXT:  .LBB8_1: # %iszero
+; NOADX-NEXT:    movl $111, %eax
+; NOADX-NEXT:    retq
+  %o = call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 %a, i32 %b)
+  %ov = extractvalue { i32, i1 } %o, 1
+  %ov.ext = zext i1 %ov to i32
+  %n = add i32 %s, %ov.ext
+  %z = icmp eq i32 %n, 0
+  br i1 %z, label %iszero, label %nonzero
+
+iszero:
+  ret i32 111
+
+nonzero:
+  ret i32 222
+}
+
 ;; NOTE: These prefixes are unused and the list is autogenerated. Do not add tests below this line:
 ; CHECK: {{.*}}

>From 4ac4cc5197923e18334aeaa8cdb0f2986d9768b7 Mon Sep 17 00:00:00 2001
From: AZero13 <gfunni234 at gmail.com>
Date: Mon, 31 Aug 2026 17:23:10 -0400
Subject: [PATCH 2/2] [X86] Fix ADOX miscompile by restricting COND_O
 optimization when EFLAGS are used

This patch fixes a miscompile where the X86 DAGCombiner aggressively folds an `ADD` node into an `ADOX` instruction even when the Zero Flag (ZF) produced by the `ADD` is used by a subsequent branch (e.g., `je`).
---
 llvm/lib/Target/X86/X86ISelLowering.cpp |  8 +++++---
 llvm/test/CodeGen/X86/adox.ll           | 11 +++++++++--
 2 files changed, 14 insertions(+), 5 deletions(-)

diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp b/llvm/lib/Target/X86/X86ISelLowering.cpp
index fbebb55cb8c51..71cba0b21af82 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.cpp
+++ b/llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -53514,7 +53514,8 @@ static SDValue combineOrCmpEqZeroToCtlzSrl(SDNode *N, SelectionDAG &DAG,
 static SDValue combineAddOrSubToADCOrSBB(bool IsSub, const SDLoc &DL, EVT VT,
                                          SDValue X, SDValue Y,
                                          SelectionDAG &DAG,
-                                         bool ZeroSecondOpOnly = false) {
+                                         bool ZeroSecondOpOnly = false,
+                                         bool FlagsUsed = false) {
   if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
     return SDValue();
 
@@ -53575,7 +53576,7 @@ static SDValue combineAddOrSubToADCOrSBB(bool IsSub, const SDLoc &DL, EVT VT,
                        DAG.getVTList(VT, MVT::i32), X,
                        DAG.getConstant(0, DL, VT), EFLAGS);
   }
-  if (!IsSub && CC == X86::COND_O &&
+  if (!IsSub && CC == X86::COND_O && !FlagsUsed &&
       (VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32 || VT == MVT::i64) &&
       DAG.getSubtarget<X86Subtarget>().hasADX()) {
     // X + (overflow_from_OF ? 1 : 0) --> adox X, 0
@@ -59729,7 +59730,8 @@ static SDValue combineX86AddSub(SDNode *N, SelectionDAG &DAG,
   // TODO: Can we drop the ZeroSecondOpOnly limit? This is to guarantee that the
   // EFLAGS result doesn't change.
   return combineAddOrSubToADCOrSBB(IsSub, DL, VT, LHS, RHS, DAG,
-                                   /*ZeroSecondOpOnly*/ true);
+                                   /*ZeroSecondOpOnly*/ true,
+                                   /*FlagsUsed*/ true);
 }
 
 static SDValue combineX86XOR(SDNode *N, SelectionDAG &DAG,
diff --git a/llvm/test/CodeGen/X86/adox.ll b/llvm/test/CodeGen/X86/adox.ll
index 1c40468440158..d8b833ced37fb 100644
--- a/llvm/test/CodeGen/X86/adox.ll
+++ b/llvm/test/CodeGen/X86/adox.ll
@@ -269,12 +269,18 @@ define i32 @adox_test_sub_i32(i32 %a, i32 %b, i8 %c, i8 %d) {
   ret i32 %res
 }
 
+; Negative test: ADOX modifies OF but does not modify ZF.
+; When the result of the addition is checked for equality with zero (using ZF),
+; we cannot optimize the addition into an ADOX instruction because the branch
+; would read an incorrect/stale ZF value. The compiler must fall back to a
+; standard ADD which sets ZF correctly.
 define i32 @adox_test_zf(i32 %a, i32 %b, i32 %s) {
 ; NDD-LABEL: adox_test_zf:
 ; NDD:       # %bb.0:
 ; NDD-NEXT:    xorl %eax, %eax
 ; NDD-NEXT:    addl %edi, %esi
-; NDD-NEXT:    adoxl %eax, %edx
+; NDD-NEXT:    seto %al
+; NDD-NEXT:    addl %edx, %eax
 ; NDD-NEXT:    je .LBB8_1
 ; NDD-NEXT:  # %bb.2: # %nonzero
 ; NDD-NEXT:    movl $222, %eax
@@ -287,7 +293,8 @@ define i32 @adox_test_zf(i32 %a, i32 %b, i32 %s) {
 ; ADX:       # %bb.0:
 ; ADX-NEXT:    xorl %eax, %eax
 ; ADX-NEXT:    addl %esi, %edi
-; ADX-NEXT:    adoxl %eax, %edx
+; ADX-NEXT:    seto %al
+; ADX-NEXT:    addl %edx, %eax
 ; ADX-NEXT:    je .LBB8_1
 ; ADX-NEXT:  # %bb.2: # %nonzero
 ; ADX-NEXT:    movl $222, %eax



More information about the llvm-commits mailing list