diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp --- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp @@ -8297,6 +8297,13 @@ if (SDValue RXOR = reassociateOps(ISD::XOR, DL, N0, N1, N->getFlags())) return RXOR; + // look for AddLike folds: + // XOR(N0,MIN_SIGNED_VALUE) == ADD(N0,MIN_SIGNED_VALUE) + if (auto *N1C = dyn_cast(N1)) + if (N1C->isMinSignedValue()) + if (SDValue Combined = visitADDLike(N)) + return Combined; + // fold !(x cc y) -> (x !cc y) unsigned N0Opcode = N0.getOpcode(); SDValue LHS, RHS, CC; diff --git a/llvm/test/CodeGen/X86/xor-lea.ll b/llvm/test/CodeGen/X86/xor-lea.ll --- a/llvm/test/CodeGen/X86/xor-lea.ll +++ b/llvm/test/CodeGen/X86/xor-lea.ll @@ -143,17 +143,15 @@ define i16 @xor_sub_sminval_i16(i16 %x) { ; X86-LABEL: xor_sub_sminval_i16: ; X86: # %bb.0: -; X86-NEXT: movl {{[0-9]+}}(%esp), %eax -; X86-NEXT: addl $-2, %eax -; X86-NEXT: xorl $32768, %eax # imm = 0x8000 +; X86-NEXT: movl $32766, %eax # imm = 0x7FFE +; X86-NEXT: addl {{[0-9]+}}(%esp), %eax ; X86-NEXT: # kill: def $ax killed $ax killed $eax ; X86-NEXT: retl ; ; X64-LABEL: xor_sub_sminval_i16: ; X64: # %bb.0: ; X64-NEXT: # kill: def $edi killed $edi def $rdi -; X64-NEXT: leal -2(%rdi), %eax -; X64-NEXT: xorl $32768, %eax # imm = 0x8000 +; X64-NEXT: leal 32766(%rdi), %eax ; X64-NEXT: # kill: def $ax killed $ax killed $eax ; X64-NEXT: retq %s = sub i16 %x, 2 @@ -164,16 +162,14 @@ define i32 @xor_add_sminval_i32(i32 %x) { ; X86-LABEL: xor_add_sminval_i32: ; X86: # %bb.0: -; X86-NEXT: movl $512, %eax # imm = 0x200 +; X86-NEXT: movl $-2147483136, %eax # imm = 0x80000200 ; X86-NEXT: addl {{[0-9]+}}(%esp), %eax -; X86-NEXT: xorl $-2147483648, %eax # imm = 0x80000000 ; X86-NEXT: retl ; ; X64-LABEL: xor_add_sminval_i32: ; X64: # %bb.0: ; X64-NEXT: # kill: def $edi killed $edi def $rdi -; X64-NEXT: leal 512(%rdi), %eax -; X64-NEXT: xorl $-2147483648, %eax # imm = 0x80000000 +; X64-NEXT: leal -2147483136(%rdi), %eax ; X64-NEXT: retq %s = add i32 %x, 512 %r = xor i32 %s, 2147483648