[llvm-commits] [llvm] r93317 - in /llvm/trunk: include/llvm/ADT/Twine.h lib/Support/Twine.cpp lib/VMCore/Mangler.cpp lib/VMCore/Metadata.cpp lib/VMCore/Value.cpp
Benjamin Kramer
benny.kra at googlemail.com
Wed Jan 13 04:45:24 PST 2010
Author: d0k
Date: Wed Jan 13 06:45:23 2010
New Revision: 93317
URL: http://llvm.org/viewvc/llvm-project?rev=93317&view=rev
Log:
Introduce Twine::toStringRef, a variant of toVector which avoids the copy if the
twine can be represented as a single StringRef. Use the new methode to simplify
some twine users.
Modified:
llvm/trunk/include/llvm/ADT/Twine.h
llvm/trunk/lib/Support/Twine.cpp
llvm/trunk/lib/VMCore/Mangler.cpp
llvm/trunk/lib/VMCore/Metadata.cpp
llvm/trunk/lib/VMCore/Value.cpp
Modified: llvm/trunk/include/llvm/ADT/Twine.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/Twine.h?rev=93317&r1=93316&r2=93317&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/Twine.h (original)
+++ llvm/trunk/include/llvm/ADT/Twine.h Wed Jan 13 06:45:23 2010
@@ -375,8 +375,12 @@
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;
Modified: llvm/trunk/lib/Support/Twine.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Twine.cpp?rev=93317&r1=93316&r2=93317&view=diff
==============================================================================
--- llvm/trunk/lib/Support/Twine.cpp (original)
+++ llvm/trunk/lib/Support/Twine.cpp Wed Jan 13 06:45:23 2010
@@ -15,8 +15,7 @@
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 @@
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) {
Modified: llvm/trunk/lib/VMCore/Mangler.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/Mangler.cpp?rev=93317&r1=93316&r2=93317&view=diff
==============================================================================
--- llvm/trunk/lib/VMCore/Mangler.cpp (original)
+++ llvm/trunk/lib/VMCore/Mangler.cpp Wed Jan 13 06:45:23 2010
@@ -41,8 +41,7 @@
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 @@
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.
Modified: llvm/trunk/lib/VMCore/Metadata.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/Metadata.cpp?rev=93317&r1=93316&r2=93317&view=diff
==============================================================================
--- llvm/trunk/lib/VMCore/Metadata.cpp (original)
+++ llvm/trunk/lib/VMCore/Metadata.cpp Wed Jan 13 06:45:23 2010
@@ -327,12 +327,8 @@
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;
Modified: llvm/trunk/lib/VMCore/Value.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/Value.cpp?rev=93317&r1=93316&r2=93317&view=diff
==============================================================================
--- llvm/trunk/lib/VMCore/Value.cpp (original)
+++ llvm/trunk/lib/VMCore/Value.cpp Wed Jan 13 06:45:23 2010
@@ -170,13 +170,10 @@
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 @@
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 @@
// 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 @@
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);
}
More information about the llvm-commits
mailing list