diff --git a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp --- a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp +++ b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp @@ -10571,7 +10571,7 @@ return DAG.getNode(ISD::ADD, DL, VT, New1, DAG.getConstant(CB, DL, VT)); } -// Try to turn (add (xor (setcc X, Y), 1) -1) into (neg (setcc X, Y)). +// Try to turn (add (xor bool, 1) -1) into (neg bool). static SDValue combineAddOfBooleanXor(SDNode *N, SelectionDAG &DAG) { SDValue N0 = N->getOperand(0); SDValue N1 = N->getOperand(1); @@ -10582,9 +10582,13 @@ if (!isAllOnesConstant(N1)) return SDValue(); - // Look for an (xor (setcc X, Y), 1). - if (N0.getOpcode() != ISD::XOR || !isOneConstant(N0.getOperand(1)) || - N0.getOperand(0).getOpcode() != ISD::SETCC) + // Look for (xor X, 1). + if (N0.getOpcode() != ISD::XOR || !isOneConstant(N0.getOperand(1))) + return SDValue(); + + // First xor input should be 0 or 1. + APInt Mask = APInt::getBitsSetFrom(VT.getSizeInBits(), 1); + if (!DAG.MaskedValueIsZero(N0.getOperand(0), Mask)) return SDValue(); // Emit a negate of the setcc. diff --git a/llvm/test/CodeGen/RISCV/atomicrmw-uinc-udec-wrap.ll b/llvm/test/CodeGen/RISCV/atomicrmw-uinc-udec-wrap.ll --- a/llvm/test/CodeGen/RISCV/atomicrmw-uinc-udec-wrap.ll +++ b/llvm/test/CodeGen/RISCV/atomicrmw-uinc-udec-wrap.ll @@ -481,8 +481,7 @@ ; RV32I-NEXT: addi a1, a4, 1 ; RV32I-NEXT: seqz a2, a1 ; RV32I-NEXT: add a3, a5, a2 -; RV32I-NEXT: xori a0, a0, 1 -; RV32I-NEXT: addi a0, a0, -1 +; RV32I-NEXT: neg a0, a0 ; RV32I-NEXT: and a2, a0, a1 ; RV32I-NEXT: and a3, a0, a3 ; RV32I-NEXT: sw a4, 8(sp) @@ -537,8 +536,7 @@ ; RV32IA-NEXT: addi a1, a4, 1 ; RV32IA-NEXT: seqz a2, a1 ; RV32IA-NEXT: add a3, a5, a2 -; RV32IA-NEXT: xori a0, a0, 1 -; RV32IA-NEXT: addi a0, a0, -1 +; RV32IA-NEXT: neg a0, a0 ; RV32IA-NEXT: and a2, a0, a1 ; RV32IA-NEXT: and a3, a0, a3 ; RV32IA-NEXT: sw a4, 8(sp)