Index: lib/CodeGen/SelectionDAG/TargetLowering.cpp =================================================================== --- lib/CodeGen/SelectionDAG/TargetLowering.cpp +++ lib/CodeGen/SelectionDAG/TargetLowering.cpp @@ -2689,7 +2689,16 @@ return DAG.getSetCC(dl, VT, And, DAG.getConstant(0, dl, CTVT), CC); } - // TODO: (ctpop x) == 1 -> x && (x & x-1) == 0 iff ctpop is illegal. + // (ctpop x) == 1 -> (x-1) u< (x & -x) iff ctpop is illegal. + if (Cond == ISD::SETEQ && C1 == 1 && + !isOperationLegalOrCustom(ISD::CTPOP, CTVT)) { + SDValue Sub = + DAG.getNode(ISD::SUB, dl, CTVT, CTOp, DAG.getConstant(1, dl, CTVT)); + SDValue Neg = + DAG.getNode(ISD::SUB, dl, CTVT, DAG.getConstant(0, dl, CTVT), CTOp); + SDValue And = DAG.getNode(ISD::AND, dl, CTVT, CTOp, Neg); + return DAG.getSetCC(dl, VT, Sub, And, ISD::SETULT); + } } // (zext x) == C --> x == (trunc C) Index: test/CodeGen/X86/ctpop-combine.ll =================================================================== --- test/CodeGen/X86/ctpop-combine.ll +++ test/CodeGen/X86/ctpop-combine.ll @@ -122,28 +122,13 @@ ; ; NO-POPCOUNT-LABEL: ctpop_eq_one: ; NO-POPCOUNT: # %bb.0: -; NO-POPCOUNT-NEXT: movq %rdi, %rax -; NO-POPCOUNT-NEXT: shrq %rax -; NO-POPCOUNT-NEXT: movabsq $6148914691236517205, %rcx # imm = 0x5555555555555555 -; NO-POPCOUNT-NEXT: andq %rax, %rcx -; NO-POPCOUNT-NEXT: subq %rcx, %rdi -; NO-POPCOUNT-NEXT: movabsq $3689348814741910323, %rax # imm = 0x3333333333333333 ; NO-POPCOUNT-NEXT: movq %rdi, %rcx -; NO-POPCOUNT-NEXT: andq %rax, %rcx -; NO-POPCOUNT-NEXT: shrq $2, %rdi -; NO-POPCOUNT-NEXT: andq %rax, %rdi -; NO-POPCOUNT-NEXT: addq %rcx, %rdi -; NO-POPCOUNT-NEXT: movq %rdi, %rax -; NO-POPCOUNT-NEXT: shrq $4, %rax -; NO-POPCOUNT-NEXT: addq %rdi, %rax -; NO-POPCOUNT-NEXT: movabsq $1085102592571150095, %rcx # imm = 0xF0F0F0F0F0F0F0F -; NO-POPCOUNT-NEXT: andq %rax, %rcx -; NO-POPCOUNT-NEXT: movabsq $72340172838076673, %rdx # imm = 0x101010101010101 -; NO-POPCOUNT-NEXT: imulq %rcx, %rdx -; NO-POPCOUNT-NEXT: shrq $56, %rdx +; NO-POPCOUNT-NEXT: negq %rcx +; NO-POPCOUNT-NEXT: andq %rdi, %rcx +; NO-POPCOUNT-NEXT: decq %rdi ; NO-POPCOUNT-NEXT: xorl %eax, %eax -; NO-POPCOUNT-NEXT: cmpq $1, %rdx -; NO-POPCOUNT-NEXT: sete %al +; NO-POPCOUNT-NEXT: cmpq %rdi, %rcx +; NO-POPCOUNT-NEXT: seta %al ; NO-POPCOUNT-NEXT: retq %count = tail call i64 @llvm.ctpop.i64(i64 %x) %cmp = icmp eq i64 %count, 1