diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp b/llvm/lib/Target/X86/X86ISelLowering.cpp --- a/llvm/lib/Target/X86/X86ISelLowering.cpp +++ b/llvm/lib/Target/X86/X86ISelLowering.cpp @@ -52150,6 +52150,13 @@ return DAG.getNode(X86ISD::ADC, SDLoc(N), VTs, LHS, RHS, Flags); } + // Fold ADC(ADD(X,Y),0,Carry) -> ADC(X,Y,Carry) + // iff the flag result is dead. + if (LHS.getOpcode() == ISD::ADD && isNullConstant(RHS) && + !N->hasAnyUseOfValue(1)) + return DAG.getNode(X86ISD::ADC, SDLoc(N), N->getVTList(), LHS.getOperand(0), + LHS.getOperand(1), CarryIn); + return SDValue(); } diff --git a/llvm/test/CodeGen/X86/addcarry.ll b/llvm/test/CodeGen/X86/addcarry.ll --- a/llvm/test/CodeGen/X86/addcarry.ll +++ b/llvm/test/CodeGen/X86/addcarry.ll @@ -1366,11 +1366,9 @@ define i32 @addcarry_ult(i32 %a, i32 %b, i32 %x, i32 %y) { ; CHECK-LABEL: addcarry_ult: ; CHECK: # %bb.0: -; CHECK-NEXT: # kill: def $esi killed $esi def $rsi -; CHECK-NEXT: # kill: def $edi killed $edi def $rdi -; CHECK-NEXT: leal (%rdi,%rsi), %eax +; CHECK-NEXT: movl %edi, %eax ; CHECK-NEXT: cmpl %ecx, %edx -; CHECK-NEXT: adcl $0, %eax +; CHECK-NEXT: adcl %esi, %eax ; CHECK-NEXT: retq %s = add i32 %a, %b %k = icmp ult i32 %x, %y @@ -1382,11 +1380,9 @@ define i32 @addcarry_ugt(i32 %a, i32 %b, i32 %x, i32 %y) { ; CHECK-LABEL: addcarry_ugt: ; CHECK: # %bb.0: -; CHECK-NEXT: # kill: def $esi killed $esi def $rsi -; CHECK-NEXT: # kill: def $edi killed $edi def $rdi -; CHECK-NEXT: leal (%rdi,%rsi), %eax +; CHECK-NEXT: movl %edi, %eax ; CHECK-NEXT: cmpl %edx, %ecx -; CHECK-NEXT: adcl $0, %eax +; CHECK-NEXT: adcl %esi, %eax ; CHECK-NEXT: retq %s = add i32 %a, %b %k = icmp ugt i32 %x, %y