summaryrefslogtreecommitdiff
path: root/include/llvm/ADT/APInt.h
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2008-08-17 07:19:36 +0000
committerChris Lattner <sabre@nondot.org>2008-08-17 07:19:36 +0000
commitfad86b003a839cef40ec8ce8408322f4913368ca (patch)
tree4049a1355d17df35e86a6806d300f0c894bb3fd9 /include/llvm/ADT/APInt.h
parentb6c8a4098fd23c21d6cda33b09b99b5a0ac1e13f (diff)
downloadllvm-fad86b003a839cef40ec8ce8408322f4913368ca.tar.gz
llvm-fad86b003a839cef40ec8ce8408322f4913368ca.tar.bz2
llvm-fad86b003a839cef40ec8ce8408322f4913368ca.tar.xz
Rework the routines that convert AP[S]Int into a string. Now, instead of
returning an std::string by value, it fills in a SmallString/SmallVector passed in. This significantly reduces string thrashing in some cases. More specifically, this: - Adds an operator<< and a print method for APInt that allows you to directly send them to an ostream. - Reimplements APInt::toString to be much simpler and more efficient algorithmically in addition to not thrashing strings quite as much. This speeds up llvm-dis on kc++ by 7%, and may also slightly speed up the asmprinter. This also fixes a bug I introduced into the asmwriter in a previous patch w.r.t. alias printing. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@54873 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'include/llvm/ADT/APInt.h')
-rw-r--r--include/llvm/ADT/APInt.h37
1 files changed, 25 insertions, 12 deletions
diff --git a/include/llvm/ADT/APInt.h b/include/llvm/ADT/APInt.h
index 9e8119d7a3..c475cf3f71 100644
--- a/include/llvm/ADT/APInt.h
+++ b/include/llvm/ADT/APInt.h
@@ -17,6 +17,7 @@
#include "llvm/Support/DataTypes.h"
#include <cassert>
+#include <iosfwd>
#include <string>
namespace llvm {
@@ -24,6 +25,9 @@ namespace llvm {
class Deserializer;
class FoldingSetNodeID;
+ template<typename T>
+ class SmallVectorImpl;
+
/* An unsigned host type used as a single part of a multi-part
bignum. */
typedef uint64_t integerPart;
@@ -468,7 +472,7 @@ public:
/// Performs logical negation operation on this APInt.
/// @returns true if *this is zero, false otherwise.
/// @brief Logical negation operator.
- bool operator !() const;
+ bool operator!() const;
/// @}
/// @name Assignment Operators
@@ -972,25 +976,29 @@ public:
/// @name Conversion Functions
/// @{
- /// This is used internally to convert an APInt to a string.
- /// @brief Converts an APInt to a std::string
- std::string toString(uint8_t radix, bool wantSigned) const;
+ void print(std::ostream &OS, bool isSigned) const;
+
+ /// toString - Converts an APInt to a string and append it to Str. Str is
+ /// commonly a SmallString.
+ void toString(SmallVectorImpl<char> &Str, unsigned Radix, bool Signed) const;
/// Considers the APInt to be unsigned and converts it into a string in the
/// radix given. The radix can be 2, 8, 10 or 16.
- /// @returns a character interpretation of the APInt
- /// @brief Convert unsigned APInt to string representation.
- std::string toStringUnsigned(uint8_t radix = 10) const {
- return toString(radix, false);
+ void toStringUnsigned(SmallVectorImpl<char> &Str, unsigned Radix = 10) const {
+ return toString(Str, Radix, false);
}
/// Considers the APInt to be signed and converts it into a string in the
/// radix given. The radix can be 2, 8, 10 or 16.
- /// @returns a character interpretation of the APInt
- /// @brief Convert signed APInt to string representation.
- std::string toStringSigned(uint8_t radix = 10) const {
- return toString(radix, true);
+ void toStringSigned(SmallVectorImpl<char> &Str, unsigned Radix = 10) const {
+ return toString(Str, Radix, true);
}
+
+ /// toString - This returns the APInt as a std::string. Note that this is an
+ /// inefficient method. It is better to pass in a SmallVector/SmallString
+ /// to the methods above to avoid thrashing the heap for the string.
+ std::string toString(unsigned Radix, bool Signed) const;
+
/// @returns a byte-swapped representation of this APInt Value.
APInt byteSwap() const;
@@ -1237,6 +1245,11 @@ inline bool operator!=(uint64_t V1, const APInt& V2) {
return V2 != V1;
}
+inline std::ostream &operator<<(std::ostream &OS, const APInt &I) {
+ I.print(OS, true);
+ return OS;
+}
+
namespace APIntOps {
/// @brief Determine the smaller of two APInts considered to be signed.