Introduce Twine::toStringRef, a variant of toVector which avoids the copy if the
authorBenjamin Kramer <benny.kra@googlemail.com>
Wed, 13 Jan 2010 12:45:23 +0000 (12:45 +0000)
committerBenjamin Kramer <benny.kra@googlemail.com>
Wed, 13 Jan 2010 12:45:23 +0000 (12:45 +0000)
twine can be represented as a single StringRef. Use the new methode to simplify
some twine users.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@93317 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/ADT/Twine.h
lib/Support/Twine.cpp
lib/VMCore/Mangler.cpp
lib/VMCore/Metadata.cpp
lib/VMCore/Value.cpp

index 29490b95b451bf40e514b769c74f763d931a7d07..97e9df445817d347a11275e3e724d1da9032ebc3 100644 (file)
@@ -375,8 +375,12 @@ namespace llvm {
       case StringRefKind:  return *(const StringRef*)LHS;
       }
     }
-    
-    
+
+    /// toStringRef - This returns the twine as a single StringRef if it can be
+    /// represented as such. Otherwise the twine is written into the given
+    /// SmallVector and a StringRef to the SmallVector's data is returned.
+    StringRef toStringRef(SmallVectorImpl<char> &Out) const;
+
     /// print - Write the concatenated string represented by this twine to the
     /// stream \arg OS.
     void print(raw_ostream &OS) const;
index 94464ffe4f0e96d93303d4d010f4e5afe6f4ac61..21504e964ea93404f8e2e32f4c5c834660d96d6b 100644 (file)
@@ -15,8 +15,7 @@ using namespace llvm;
 
 std::string Twine::str() const {
   SmallString<256> Vec;
-  toVector(Vec);
-  return std::string(Vec.begin(), Vec.end());
+  return toStringRef(Vec).str();
 }
 
 void Twine::toVector(SmallVectorImpl<char> &Out) const {
@@ -24,6 +23,13 @@ void Twine::toVector(SmallVectorImpl<char> &Out) const {
   print(OS);
 }
 
+StringRef Twine::toStringRef(SmallVectorImpl<char> &Out) const {
+  if (isSingleStringRef())
+    return getSingleStringRef();
+  toVector(Out);
+  return StringRef(Out.data(), Out.size());
+}
+
 void Twine::printOneChild(raw_ostream &OS, const void *Ptr, 
                           NodeKind Kind) const {
   switch (Kind) {
index 69a24a0bc2847bccb22ac1c80191b40bf56a273a..7d9f330f4c4e1e9174bb43a488cc6b10c2f813b5 100644 (file)
@@ -41,8 +41,7 @@ void Mangler::makeNameProper(SmallVectorImpl<char> &OutName,
                              const Twine &TheName,
                              ManglerPrefixTy PrefixTy) {
   SmallString<256> TmpData;
-  TheName.toVector(TmpData);
-  StringRef X = TmpData.str();
+  StringRef X = TheName.toStringRef(TmpData);
   assert(!X.empty() && "Cannot mangle empty strings");
   
   if (!UseQuotes) {
@@ -188,13 +187,7 @@ std::string Mangler::getMangledName(const GlobalValue *GV, const char *Suffix,
 void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
                                 const Twine &GVName, ManglerPrefixTy PrefixTy) {
   SmallString<256> TmpData;
-  StringRef Name;
-  if (GVName.isSingleStringRef())
-    Name = GVName.getSingleStringRef();
-  else {
-    GVName.toVector(TmpData);
-    Name = TmpData.str();
-  }
+  StringRef Name = GVName.toStringRef(TmpData);
   assert(!Name.empty() && "getNameWithPrefix requires non-empty name");
   
   // If the global name is not led with \1, add the appropriate prefixes.
index e6a962bbca0a4f5de3f3a48c5f0b5014c44ed615..bac89bfd7b6633e9e2dc1973206405e06b085f3f 100644 (file)
@@ -327,12 +327,8 @@ void NamedMDNode::setName(const Twine &NewName) {
   assert (!NewName.isTriviallyEmpty() && "Invalid named metadata name!");
 
   SmallString<256> NameData;
-  NewName.toVector(NameData);
+  StringRef NameRef = NewName.toStringRef(NameData);
 
-  const char *NameStr = NameData.data();
-  unsigned NameLen = NameData.size();
-
-  StringRef NameRef = StringRef(NameStr, NameLen);
   // Name isn't changing?
   if (getName() == NameRef)
     return;
index ecf2d4b7744bb62d82c9d340e116624b20900ff1..40679bfc290498fa71b4a1d546450a1fe84a3763 100644 (file)
@@ -170,13 +170,10 @@ void Value::setName(const Twine &NewName) {
     return;
 
   SmallString<256> NameData;
-  NewName.toVector(NameData);
-
-  const char *NameStr = NameData.data();
-  unsigned NameLen = NameData.size();
+  StringRef NameRef = NewName.toStringRef(NameData);
 
   // Name isn't changing?
-  if (getName() == StringRef(NameStr, NameLen))
+  if (getName() == NameRef)
     return;
 
   assert(!getType()->isVoidTy() && "Cannot assign a name to void values!");
@@ -187,7 +184,7 @@ void Value::setName(const Twine &NewName) {
     return;  // Cannot set a name on this value (e.g. constant).
 
   if (!ST) { // No symbol table to update?  Just do the change.
-    if (NameLen == 0) {
+    if (NameRef.empty()) {
       // Free the name for this value.
       Name->Destroy();
       Name = 0;
@@ -201,7 +198,7 @@ void Value::setName(const Twine &NewName) {
     // then reallocated.
 
     // Create the new name.
-    Name = ValueName::Create(NameStr, NameStr+NameLen);
+    Name = ValueName::Create(NameRef.begin(), NameRef.end());
     Name->setValue(this);
     return;
   }
@@ -214,12 +211,12 @@ void Value::setName(const Twine &NewName) {
     Name->Destroy();
     Name = 0;
 
-    if (NameLen == 0)
+    if (NameRef.empty())
       return;
   }
 
   // Name is changing to something new.
-  Name = ST->createValueName(StringRef(NameStr, NameLen), this);
+  Name = ST->createValueName(NameRef, this);
 }