Index: lib/CodeGen/SelectionDAG/DAGCombiner.cpp =================================================================== --- lib/CodeGen/SelectionDAG/DAGCombiner.cpp +++ lib/CodeGen/SelectionDAG/DAGCombiner.cpp @@ -2185,10 +2185,30 @@ DAG.haveNoCommonBitsSet(N0, N1)) return DAG.getNode(ISD::OR, DL, VT, N0, N1); - // fold (add (xor a, -1), 1) -> (sub 0, a) - if (isBitwiseNot(N0) && isOneOrOneSplat(N1)) - return DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT), - N0.getOperand(0)); + if (isOneOrOneSplat(N1)) { + // fold (add (xor a, -1), 1) -> (sub 0, a) + if (isBitwiseNot(N0)) + return DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT), + N0.getOperand(0)); + + // fold (add (add (xor a, -1), b), 1) -> (sub b, a) + if (N0.getOpcode() == ISD::ADD || + N0.getOpcode() == ISD::UADDO || + N0.getOpcode() == ISD::SADDO) { + SDValue A, Xor; + + if (isBitwiseNot(N0.getOperand(0))) { + A = N0.getOperand(1); + Xor = N0.getOperand(0); + } else if (isBitwiseNot(N0.getOperand(1))) { + A = N0.getOperand(0); + Xor = N0.getOperand(1); + } + + if (Xor) + return DAG.getNode(ISD::SUB, DL, VT, A, Xor.getOperand(0)); + } + } if (SDValue Combined = visitADDLike(N0, N1, N)) return Combined; Index: test/CodeGen/X86/add.ll =================================================================== --- test/CodeGen/X86/add.ll +++ test/CodeGen/X86/add.ll @@ -482,25 +482,19 @@ ; X32-LABEL: add_to_sub: ; X32: # %bb.0: ; X32-NEXT: movl {{[0-9]+}}(%esp), %eax -; X32-NEXT: movl {{[0-9]+}}(%esp), %ecx -; X32-NEXT: notl %ecx -; X32-NEXT: leal 1(%ecx,%eax), %eax +; X32-NEXT: subl {{[0-9]+}}(%esp), %eax ; X32-NEXT: retl ; ; X64-LINUX-LABEL: add_to_sub: ; X64-LINUX: # %bb.0: -; X64-LINUX-NEXT: # kill: def $esi killed $esi def $rsi -; X64-LINUX-NEXT: # kill: def $edi killed $edi def $rdi -; X64-LINUX-NEXT: notl %edi -; X64-LINUX-NEXT: leal 1(%rdi,%rsi), %eax +; X64-LINUX-NEXT: movl %esi, %eax +; X64-LINUX-NEXT: subl %edi, %eax ; X64-LINUX-NEXT: retq ; ; X64-WIN32-LABEL: add_to_sub: ; X64-WIN32: # %bb.0: -; X64-WIN32-NEXT: # kill: def $edx killed $edx def $rdx -; X64-WIN32-NEXT: # kill: def $ecx killed $ecx def $rcx -; X64-WIN32-NEXT: notl %ecx -; X64-WIN32-NEXT: leal 1(%rcx,%rdx), %eax +; X64-WIN32-NEXT: movl %edx, %eax +; X64-WIN32-NEXT: subl %ecx, %eax ; X64-WIN32-NEXT: retq %nota = xor i32 %a, -1 %add = add i32 %nota, %b