[PATCH] ADT: Correct APInt::getActiveWords for zero values

Chris Lattner clattner at apple.com
Thu Feb 7 10:22:29 PST 2013


Makes sense, LGTM.

-Chris

On Feb 7, 2013, at 10:16 AM, Meador Inge <meadori at codesourcery.com> wrote:

> PR15138 was opened because of a segfault in the Bitcode writer.
> The actual issue ended up being a bug in APInt where calls to
> APInt::getActiveWords returns a bogus value when the APInt value
> is 0.  This patch fixes the problem by ensuring that getActiveWords
> returns 1 for 0 valued APInts.
> 
> OK?
> 
> ---
> include/llvm/ADT/APInt.h    | 3 ++-
> unittests/ADT/APIntTest.cpp | 8 ++++++++
> 2 files changed, 10 insertions(+), 1 deletion(-)
> 
> diff --git a/include/llvm/ADT/APInt.h b/include/llvm/ADT/APInt.h
> index e67e032..2e623a1 100644
> --- a/include/llvm/ADT/APInt.h
> +++ b/include/llvm/ADT/APInt.h
> @@ -1191,7 +1191,8 @@ public:
>   /// APInt. This is used in conjunction with getActiveData to extract the raw
>   /// value of the APInt.
>   unsigned getActiveWords() const {
> -    return whichWord(getActiveBits()-1) + 1;
> +    unsigned numActiveBits = getActiveBits();
> +    return numActiveBits ? whichWord(numActiveBits - 1) + 1 : 1;
>   }
> 
>   /// Computes the minimum bit width for this APInt while considering it to be
> diff --git a/unittests/ADT/APIntTest.cpp b/unittests/ADT/APIntTest.cpp
> index 2a4c5b4..2510bc7 100644
> --- a/unittests/ADT/APIntTest.cpp
> +++ b/unittests/ADT/APIntTest.cpp
> @@ -56,6 +56,14 @@ TEST(APIntTest, i33_Count) {
> #endif
> 
> TEST(APIntTest, i65_Count) {
> +  APInt i65(65, 0, true);
> +  EXPECT_EQ(65u, i65.countLeadingZeros());
> +  EXPECT_EQ(0u, i65.countLeadingOnes());
> +  EXPECT_EQ(0u, i65.getActiveBits());
> +  EXPECT_EQ(1u, i65.getActiveWords());
> +  EXPECT_EQ(65u, i65.countTrailingZeros());
> +  EXPECT_EQ(0u, i65.countPopulation());
> +
>   APInt i65minus(65, 0, true);
>   i65minus.setBit(64);
>   EXPECT_EQ(0u, i65minus.countLeadingZeros());
> -- 
> 1.8.1
> 
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits



More information about the llvm-commits mailing list