diff options
author | Pete Cooper <peter_cooper@apple.com> | 2016-05-27 03:42:17 +0000 |
---|---|---|
committer | Pete Cooper <peter_cooper@apple.com> | 2016-05-27 03:42:17 +0000 |
commit | 18e9102a852193adda32906270fc5fe0bfe73fcb (patch) | |
tree | 4875f01fdc8c340a07a955c1dff330cf49e060c7 | |
parent | 8c11fce7077a484530c7dfda2ddbe82417ae375c (diff) | |
download | bcm5719-llvm-18e9102a852193adda32906270fc5fe0bfe73fcb.tar.gz bcm5719-llvm-18e9102a852193adda32906270fc5fe0bfe73fcb.zip |
Don't allocate unnecessarily in APInt::operator[+-]. NFC.
APInt::operator+(uint64_t) just forwarded to operator+(const APInt&).
Constructing the APInt for the RHS takes an allocation which isn't
required. Also, for APInt's in the slow path, operator+ would
call add() internally which iterates over both arrays of values. Instead
we can use add_1 and sub_1 which only iterate while there is something to do.
Using the memory for 'opt -O2 verify-uselistorder.lto.opt.bc -o opt.bc'
(see r236629 for details), this reduces the number of allocations from
23.9M to 22.7M.
llvm-svn: 270959
-rw-r--r-- | llvm/include/llvm/ADT/APInt.h | 4 | ||||
-rw-r--r-- | llvm/lib/Support/APInt.cpp | 18 |
2 files changed, 20 insertions, 2 deletions
diff --git a/llvm/include/llvm/ADT/APInt.h b/llvm/include/llvm/ADT/APInt.h index 2cba538fc26..a02ecaa762a 100644 --- a/llvm/include/llvm/ADT/APInt.h +++ b/llvm/include/llvm/ADT/APInt.h @@ -835,13 +835,13 @@ public: /// /// Adds RHS to this APInt and returns the result. APInt operator+(const APInt &RHS) const; - APInt operator+(uint64_t RHS) const { return (*this) + APInt(BitWidth, RHS); } + APInt operator+(uint64_t RHS) const; /// \brief Subtraction operator. /// /// Subtracts RHS from this APInt and returns the result. APInt operator-(const APInt &RHS) const; - APInt operator-(uint64_t RHS) const { return (*this) - APInt(BitWidth, RHS); } + APInt operator-(uint64_t RHS) const; /// \brief Left logical shift operator. /// diff --git a/llvm/lib/Support/APInt.cpp b/llvm/lib/Support/APInt.cpp index c26d4829275..8aec54730ca 100644 --- a/llvm/lib/Support/APInt.cpp +++ b/llvm/lib/Support/APInt.cpp @@ -480,6 +480,15 @@ APInt APInt::operator+(const APInt& RHS) const { return Result; } +APInt APInt::operator+(uint64_t RHS) const { + if (isSingleWord()) + return APInt(BitWidth, VAL + RHS); + APInt Result(*this); + add_1(Result.pVal, Result.pVal, getNumWords(), RHS); + Result.clearUnusedBits(); + return Result; +} + APInt APInt::operator-(const APInt& RHS) const { assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); if (isSingleWord()) @@ -490,6 +499,15 @@ APInt APInt::operator-(const APInt& RHS) const { return Result; } +APInt APInt::operator-(uint64_t RHS) const { + if (isSingleWord()) + return APInt(BitWidth, VAL - RHS); + APInt Result(*this); + sub_1(Result.pVal, getNumWords(), RHS); + Result.clearUnusedBits(); + return Result; +} + bool APInt::EqualSlowCase(const APInt& RHS) const { return std::equal(pVal, pVal + getNumWords(), RHS.pVal); } |