From 9e513acd3145036bd06b5e0f5bcc83a3e5c08854 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Fri, 23 Nov 2007 22:42:31 +0000 Subject: [PATCH] Fix PR1816, by correcting the broken definition of APInt::countTrailingZeros. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44296 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/ADT/APInt.h | 7 +++---- lib/Support/APInt.cpp | 2 +- test/Transforms/ConstProp/2007-11-23-cttz.ll | 8 ++++++++ 3 files changed, 12 insertions(+), 5 deletions(-) create mode 100644 test/Transforms/ConstProp/2007-11-23-cttz.ll diff --git a/include/llvm/ADT/APInt.h b/include/llvm/ADT/APInt.h index dc5d34f3b1f..7e06d3d1aec 100644 --- a/include/llvm/ADT/APInt.h +++ b/include/llvm/ADT/APInt.h @@ -896,10 +896,9 @@ public: /// countLeadingZeros - This function is an APInt version of the /// countLeadingZeros_{32,64} functions in MathExtras.h. It counts the number /// of zeros from the most significant bit to the first one bit. - /// @returns getNumWords() * APINT_BITS_PER_WORD if the value is zero. + /// @returns BitWidth if the value is zero. /// @returns the number of zeros from the most significant bit to the first /// one bits. - /// @brief Count the number of leading one bits. uint32_t countLeadingZeros() const; /// countLeadingOnes - This function counts the number of contiguous 1 bits @@ -911,8 +910,8 @@ public: /// countTrailingZeros - This function is an APInt version of the /// countTrailingZoers_{32,64} functions in MathExtras.h. It counts - /// the number of zeros from the least significant bit to the first one bit. - /// @returns getNumWords() * APINT_BITS_PER_WORD if the value is zero. + /// the number of zeros from the least significant bit to the first set bit. + /// @returns BitWidth if the value is zero. /// @returns the number of zeros from the least significant bit to the first /// one bit. /// @brief Count the number of trailing zero bits. diff --git a/lib/Support/APInt.cpp b/lib/Support/APInt.cpp index 7af0101ece8..3c83e8a749d 100644 --- a/lib/Support/APInt.cpp +++ b/lib/Support/APInt.cpp @@ -745,7 +745,7 @@ uint32_t APInt::countLeadingZeros() const { uint32_t remainder = BitWidth % APINT_BITS_PER_WORD; if (remainder) Count -= APINT_BITS_PER_WORD - remainder; - return Count; + return std::min(Count, BitWidth); } static uint32_t countLeadingOnes_64(uint64_t V, uint32_t skip) { diff --git a/test/Transforms/ConstProp/2007-11-23-cttz.ll b/test/Transforms/ConstProp/2007-11-23-cttz.ll new file mode 100644 index 00000000000..995ce2febf8 --- /dev/null +++ b/test/Transforms/ConstProp/2007-11-23-cttz.ll @@ -0,0 +1,8 @@ +; RUN: llvm-as < %s | opt -constprop | llvm-dis | grep {ret i13 13} +; PR1816 +declare i13 @llvm.cttz.i13(i13) + +define i13 @test() { + %X = call i13 @llvm.cttz.i13(i13 0) + ret i13 %X +}