From d4d09fd73d2cb9e503828ead17401afb446e3ed1 Mon Sep 17 00:00:00 2001 From: Craig Topper Date: Thu, 4 May 2017 04:33:27 +0000 Subject: [PATCH] [SelectionDAG] Improve known bits support for CTPOP. This is based on the same concept from ValueTracking's version of computeKnownBits. llvm-svn: 302110 --- llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp | 5 ++++- llvm/test/CodeGen/X86/ctpop-combine.ll | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp index 33b9b3337652..efcb6158ed30 100644 --- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp @@ -2376,7 +2376,10 @@ void SelectionDAG::computeKnownBits(SDValue Op, KnownBits &Known, break; } case ISD::CTPOP: { - Known.Zero.setBitsFrom(Log2_32(BitWidth)+1); + computeKnownBits(Op.getOperand(0), Known2, DemandedElts, Depth + 1); + // If we know some of the bits are zero, they can't be one. + unsigned PossibleOnes = BitWidth - Known2.Zero.countPopulation(); + Known.Zero.setBitsFrom(Log2_32(PossibleOnes) + 1); break; } case ISD::LOAD: { diff --git a/llvm/test/CodeGen/X86/ctpop-combine.ll b/llvm/test/CodeGen/X86/ctpop-combine.ll index b7031a817e82..bbfc2ead04c6 100644 --- a/llvm/test/CodeGen/X86/ctpop-combine.ll +++ b/llvm/test/CodeGen/X86/ctpop-combine.ll @@ -1,6 +1,7 @@ ; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py ; RUN: llc < %s -mtriple=x86_64-unknown -mcpu=corei7 | FileCheck %s +declare i8 @llvm.ctpop.i8(i8) nounwind readnone declare i64 @llvm.ctpop.i64(i64) nounwind readnone define i32 @test1(i64 %x) nounwind readnone { @@ -48,3 +49,16 @@ define i32 @test3(i64 %x) nounwind readnone { %conv = zext i1 %cmp to i32 ret i32 %conv } + +define i8 @test4(i8 %x) nounwind readnone { +; CHECK-LABEL: test4: +; CHECK: # BB#0: +; CHECK-NEXT: andl $127, %edi +; CHECK-NEXT: popcntw %di, %ax +; CHECK-NEXT: # kill: %AL %AL %AX +; CHECK-NEXT: retq + %x2 = and i8 %x, 127 + %count = tail call i8 @llvm.ctpop.i8(i8 %x2) + %and = and i8 %count, 7 + ret i8 %and +}