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.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@270959 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Pete Cooper
2016-05-27 03:42:17 +00:00
parent 70172d9997
commit 1544fa2368
2 changed files with 20 additions and 2 deletions
+18
View File
@@ -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);
}