diff --git a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp --- a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp @@ -3446,7 +3446,7 @@ if (CTPOP.hasOneUse() && CTPOP.getOpcode() == ISD::CTPOP && (N0 == CTPOP || - N0.getValueSizeInBits() > Log2_32_Ceil(CTPOP.getValueSizeInBits()))) { + N0.getValueSizeInBits() > Log2_32(CTPOP.getValueSizeInBits()))) { EVT CTVT = CTPOP.getValueType(); SDValue CTOp = CTPOP.getOperand(0); diff --git a/llvm/test/CodeGen/X86/ctpop-combine.ll b/llvm/test/CodeGen/X86/ctpop-combine.ll --- a/llvm/test/CodeGen/X86/ctpop-combine.ll +++ b/llvm/test/CodeGen/X86/ctpop-combine.ll @@ -162,96 +162,39 @@ } define i1 @ctpop_trunc_non_power2(i255 %x) { -; POPCOUNT-LABEL: ctpop_trunc_non_power2: -; POPCOUNT: # %bb.0: -; POPCOUNT-NEXT: movabsq $9223372036854775807, %rax # imm = 0x7FFFFFFFFFFFFFFF -; POPCOUNT-NEXT: andq %rcx, %rax -; POPCOUNT-NEXT: popcntq %rsi, %rcx -; POPCOUNT-NEXT: popcntq %rdi, %rsi -; POPCOUNT-NEXT: addl %ecx, %esi -; POPCOUNT-NEXT: popcntq %rax, %rax -; POPCOUNT-NEXT: popcntq %rdx, %rcx -; POPCOUNT-NEXT: addl %eax, %ecx -; POPCOUNT-NEXT: addl %esi, %ecx -; POPCOUNT-NEXT: cmpb $1, %cl -; POPCOUNT-NEXT: sete %al -; POPCOUNT-NEXT: retq -; -; NO-POPCOUNT-LABEL: ctpop_trunc_non_power2: -; NO-POPCOUNT: # %bb.0: -; NO-POPCOUNT-NEXT: movabsq $9223372036854775807, %rax # imm = 0x7FFFFFFFFFFFFFFF -; NO-POPCOUNT-NEXT: andq %rcx, %rax -; NO-POPCOUNT-NEXT: movq %rsi, %rcx -; NO-POPCOUNT-NEXT: shrq %rcx -; NO-POPCOUNT-NEXT: movabsq $6148914691236517205, %r8 # imm = 0x5555555555555555 -; NO-POPCOUNT-NEXT: andq %r8, %rcx -; NO-POPCOUNT-NEXT: subq %rcx, %rsi -; NO-POPCOUNT-NEXT: movabsq $3689348814741910323, %r11 # imm = 0x3333333333333333 -; NO-POPCOUNT-NEXT: movq %rsi, %rcx -; NO-POPCOUNT-NEXT: andq %r11, %rcx -; NO-POPCOUNT-NEXT: shrq $2, %rsi -; NO-POPCOUNT-NEXT: andq %r11, %rsi -; NO-POPCOUNT-NEXT: addq %rcx, %rsi -; NO-POPCOUNT-NEXT: movq %rsi, %rcx -; NO-POPCOUNT-NEXT: shrq $4, %rcx -; NO-POPCOUNT-NEXT: addq %rsi, %rcx -; NO-POPCOUNT-NEXT: movabsq $1085102592571150095, %r9 # imm = 0xF0F0F0F0F0F0F0F -; NO-POPCOUNT-NEXT: andq %r9, %rcx -; NO-POPCOUNT-NEXT: movabsq $72340172838076673, %r10 # imm = 0x101010101010101 -; NO-POPCOUNT-NEXT: imulq %r10, %rcx -; NO-POPCOUNT-NEXT: shrq $56, %rcx -; NO-POPCOUNT-NEXT: movq %rdi, %rsi -; NO-POPCOUNT-NEXT: shrq %rsi -; NO-POPCOUNT-NEXT: andq %r8, %rsi -; NO-POPCOUNT-NEXT: subq %rsi, %rdi -; NO-POPCOUNT-NEXT: movq %rdi, %rsi -; NO-POPCOUNT-NEXT: andq %r11, %rsi -; NO-POPCOUNT-NEXT: shrq $2, %rdi -; NO-POPCOUNT-NEXT: andq %r11, %rdi -; NO-POPCOUNT-NEXT: addq %rsi, %rdi -; NO-POPCOUNT-NEXT: movq %rdi, %rsi -; NO-POPCOUNT-NEXT: shrq $4, %rsi -; NO-POPCOUNT-NEXT: addq %rdi, %rsi -; NO-POPCOUNT-NEXT: andq %r9, %rsi -; NO-POPCOUNT-NEXT: imulq %r10, %rsi -; NO-POPCOUNT-NEXT: shrq $56, %rsi -; NO-POPCOUNT-NEXT: addl %ecx, %esi -; NO-POPCOUNT-NEXT: movq %rax, %rcx -; NO-POPCOUNT-NEXT: shrq %rcx -; NO-POPCOUNT-NEXT: movabsq $1537228672809129301, %rdi # imm = 0x1555555555555555 -; NO-POPCOUNT-NEXT: andq %rcx, %rdi -; NO-POPCOUNT-NEXT: subq %rdi, %rax -; NO-POPCOUNT-NEXT: movq %rax, %rcx -; NO-POPCOUNT-NEXT: andq %r11, %rcx -; NO-POPCOUNT-NEXT: shrq $2, %rax -; NO-POPCOUNT-NEXT: andq %r11, %rax -; NO-POPCOUNT-NEXT: addq %rcx, %rax -; NO-POPCOUNT-NEXT: movq %rax, %rcx -; NO-POPCOUNT-NEXT: shrq $4, %rcx -; NO-POPCOUNT-NEXT: addq %rax, %rcx -; NO-POPCOUNT-NEXT: andq %r9, %rcx -; NO-POPCOUNT-NEXT: imulq %r10, %rcx -; NO-POPCOUNT-NEXT: shrq $56, %rcx -; NO-POPCOUNT-NEXT: movq %rdx, %rax -; NO-POPCOUNT-NEXT: shrq %rax -; NO-POPCOUNT-NEXT: andq %r8, %rax -; NO-POPCOUNT-NEXT: subq %rax, %rdx -; NO-POPCOUNT-NEXT: movq %rdx, %rax -; NO-POPCOUNT-NEXT: andq %r11, %rax -; NO-POPCOUNT-NEXT: shrq $2, %rdx -; NO-POPCOUNT-NEXT: andq %r11, %rdx -; NO-POPCOUNT-NEXT: addq %rax, %rdx -; NO-POPCOUNT-NEXT: movq %rdx, %rax -; NO-POPCOUNT-NEXT: shrq $4, %rax -; NO-POPCOUNT-NEXT: addq %rdx, %rax -; NO-POPCOUNT-NEXT: andq %r9, %rax -; NO-POPCOUNT-NEXT: imulq %r10, %rax -; NO-POPCOUNT-NEXT: shrq $56, %rax -; NO-POPCOUNT-NEXT: addl %ecx, %eax -; NO-POPCOUNT-NEXT: addl %esi, %eax -; NO-POPCOUNT-NEXT: cmpb $1, %al -; NO-POPCOUNT-NEXT: sete %al -; NO-POPCOUNT-NEXT: retq +; CHECK-LABEL: ctpop_trunc_non_power2: +; CHECK: # %bb.0: +; CHECK-NEXT: pushq %rbx +; CHECK-NEXT: .cfi_def_cfa_offset 16 +; CHECK-NEXT: .cfi_offset %rbx, -16 +; CHECK-NEXT: movabsq $9223372036854775807, %r8 # imm = 0x7FFFFFFFFFFFFFFF +; CHECK-NEXT: movq %rcx, %r9 +; CHECK-NEXT: andq %r8, %r9 +; CHECK-NEXT: movq %rdi, %r11 +; CHECK-NEXT: addq $-1, %r11 +; CHECK-NEXT: movq %rsi, %r10 +; CHECK-NEXT: adcq $-1, %r10 +; CHECK-NEXT: movq %rdx, %rax +; CHECK-NEXT: adcq $-1, %rax +; CHECK-NEXT: movq %rcx, %rbx +; CHECK-NEXT: adcq %r8, %rbx +; CHECK-NEXT: andq %rdi, %r11 +; CHECK-NEXT: andq %rdx, %rax +; CHECK-NEXT: orq %r11, %rax +; CHECK-NEXT: andq %rsi, %r10 +; CHECK-NEXT: andq %r8, %rbx +; CHECK-NEXT: andq %rcx, %rbx +; CHECK-NEXT: orq %r10, %rbx +; CHECK-NEXT: orq %rax, %rbx +; CHECK-NEXT: sete %cl +; CHECK-NEXT: orq %rdx, %rdi +; CHECK-NEXT: orq %rsi, %r9 +; CHECK-NEXT: orq %rdi, %r9 +; CHECK-NEXT: setne %al +; CHECK-NEXT: andb %cl, %al +; CHECK-NEXT: popq %rbx +; CHECK-NEXT: .cfi_def_cfa_offset 8 +; CHECK-NEXT: retq %a = call i255 @llvm.ctpop.i255(i255 %x) %b = trunc i255 %a to i8 ; largest value from ctpop is 255, fits in 8 bits. %c = icmp eq i8 %b, 1