[llvm] r270959 - Don't allocate unnecessarily in APInt::operator[+-]. NFC.

Pete Cooper via llvm-commits llvm-commits at lists.llvm.org
Thu May 26 20:42:17 PDT 2016


Author: pete
Date: Thu May 26 22:42:17 2016
New Revision: 270959

URL: http://llvm.org/viewvc/llvm-project?rev=270959&view=rev
Log:
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.

Modified:
    llvm/trunk/include/llvm/ADT/APInt.h
    llvm/trunk/lib/Support/APInt.cpp

Modified: llvm/trunk/include/llvm/ADT/APInt.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/APInt.h?rev=270959&r1=270958&r2=270959&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/APInt.h (original)
+++ llvm/trunk/include/llvm/ADT/APInt.h Thu May 26 22:42:17 2016
@@ -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.
   ///

Modified: llvm/trunk/lib/Support/APInt.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/APInt.cpp?rev=270959&r1=270958&r2=270959&view=diff
==============================================================================
--- llvm/trunk/lib/Support/APInt.cpp (original)
+++ llvm/trunk/lib/Support/APInt.cpp Thu May 26 22:42:17 2016
@@ -480,6 +480,15 @@ APInt APInt::operator+(const APInt& RHS)
   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())
@@ -489,6 +498,15 @@ APInt APInt::operator-(const APInt& RHS)
   Result.clearUnusedBits();
   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);




More information about the llvm-commits mailing list