From: Craig Topper Date: Fri, 31 Mar 2017 18:48:14 +0000 (+0000) Subject: [APInt] Rewrite getLoBits in a way that will do one less memory allocation in the... X-Git-Tag: llvmorg-5.0.0-rc1~8737 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=e7e3560288e8d05b065b71b9b2b56deb27d45929;p=platform%2Fupstream%2Fllvm.git [APInt] Rewrite getLoBits in a way that will do one less memory allocation in the multiword case. Rewrite getHiBits to use the class method version of lshr instead of the one in APIntOps. NFCI llvm-svn: 299243 --- diff --git a/llvm/lib/Support/APInt.cpp b/llvm/lib/Support/APInt.cpp index 37ad30981d13..8ac881e4c7fe 100644 --- a/llvm/lib/Support/APInt.cpp +++ b/llvm/lib/Support/APInt.cpp @@ -724,13 +724,14 @@ bool APInt::isSplat(unsigned SplatSizeInBits) const { /// This function returns the high "numBits" bits of this APInt. APInt APInt::getHiBits(unsigned numBits) const { - return APIntOps::lshr(*this, BitWidth - numBits); + return this->lshr(BitWidth - numBits); } /// This function returns the low "numBits" bits of this APInt. APInt APInt::getLoBits(unsigned numBits) const { - return APIntOps::lshr(APIntOps::shl(*this, BitWidth - numBits), - BitWidth - numBits); + APInt Result(getLowBitsSet(BitWidth, numBits)); + Result &= *this; + return Result; } unsigned APInt::countLeadingZerosSlowCase() const { diff --git a/llvm/unittests/ADT/APIntTest.cpp b/llvm/unittests/ADT/APIntTest.cpp index f05cbb9d9152..51c6347cbe80 100644 --- a/llvm/unittests/ADT/APIntTest.cpp +++ b/llvm/unittests/ADT/APIntTest.cpp @@ -1958,3 +1958,21 @@ TEST(APIntTest, setAllBits) { EXPECT_EQ(128u, i128.countTrailingOnes()); EXPECT_EQ(128u, i128.countPopulation()); } + +TEST(APIntTest, getLoBits) { + APInt i32(32, 0xfa); + i32.setHighBits(1); + EXPECT_EQ(0xa, i32.getLoBits(4)); + APInt i128(128, 0xfa); + i128.setHighBits(1); + EXPECT_EQ(0xa, i128.getLoBits(4)); +} + +TEST(APIntTest, getHiBits) { + APInt i32(32, 0xfa); + i32.setHighBits(2); + EXPECT_EQ(0xc, i32.getHiBits(4)); + APInt i128(128, 0xfa); + i128.setHighBits(2); + EXPECT_EQ(0xc, i128.getHiBits(4)); +}