diff --git a/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/lib/CodeGen/SelectionDAG/DAGCombiner.cpp index 549527c1a3f..7fe908c7302 100644 --- a/lib/CodeGen/SelectionDAG/DAGCombiner.cpp +++ b/lib/CodeGen/SelectionDAG/DAGCombiner.cpp @@ -1176,6 +1176,9 @@ SDValue DAGCombiner::visitSUB(SDNode *N) { if (N1C) return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, DAG.getConstant(-N1C->getAPIntValue(), VT)); + // Canonicalize (sub -1, x) -> ~x, i.e. (xor x, -1) + if (N0C && N0C->isAllOnesValue()) + return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N1, N0); // fold (A+B)-A -> B if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1) return N0.getOperand(1); diff --git a/test/CodeGen/X86/xor.ll b/test/CodeGen/X86/xor.ll index 7bd06bba4c3..9bfff8a06a8 100644 --- a/test/CodeGen/X86/xor.ll +++ b/test/CodeGen/X86/xor.ll @@ -131,3 +131,14 @@ bb12: ; X32: andl {{.*}}[[REG]] } +define i32 @test8(i32 %a) nounwind { +; rdar://7553032 +entry: + %t1 = sub i32 0, %a + %t2 = add i32 %t1, -1 + ret i32 %t2 +; X64: test8: +; X64: notl %eax +; X32: test8: +; X32: notl %eax +}