Precompute block frequencies, pow() isn't free.
[oota-llvm.git] / lib / Support / Twine.cpp
index 8b8c0f0db80ce272413a0b455f7fc750c9a4cdd6..75cea2961a9dc68d8faccbbd4a6e99681c69283f 100644 (file)
@@ -8,50 +8,70 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/ADT/Twine.h"
+#include "llvm/ADT/SmallString.h"
+#include "llvm/Support/Debug.h"
 #include "llvm/Support/raw_ostream.h"
 using namespace llvm;
 
 std::string Twine::str() const {
-  std::string Res;
-  raw_string_ostream OS(Res);
-  print(OS);
-  return Res;
+  SmallString<256> Vec;
+  return toStringRef(Vec).str();
 }
 
 void Twine::toVector(SmallVectorImpl<char> &Out) const {
-  // FIXME: This is very inefficient, since we are creating a large raw_ostream
-  // buffer -- hitting malloc, which we were supposed to avoid -- all when we
-  // have this pretty little small vector available.
-  //
-  // The best way to fix this is to make raw_svector_ostream do the right thing
-  // and be efficient, by augmenting the base raw_ostream with the ability to
-  // have the buffer managed by a concrete implementation.
   raw_svector_ostream OS(Out);
   print(OS);
 }
 
-void Twine::printOneChild(raw_ostream &OS, const void *Ptr, 
+StringRef Twine::toStringRef(SmallVectorImpl<char> &Out) const {
+  if (isSingleStringRef())
+    return getSingleStringRef();
+  toVector(Out);
+  return StringRef(Out.data(), Out.size());
+}
+
+StringRef Twine::toNullTerminatedStringRef(SmallVectorImpl<char> &Out) const {
+  if (isUnary()) {
+    switch (getLHSKind()) {
+    case CStringKind:
+      // Already null terminated, yay!
+      return StringRef(static_cast<const char*>(LHS));
+    case StdStringKind: {
+        const std::string *str = static_cast<const std::string*>(LHS);
+        return StringRef(str->c_str(), str->size());
+      }
+    default:
+      break;
+    }
+  }
+  toVector(Out);
+  Out.push_back(0);
+  Out.pop_back();
+  return StringRef(Out.data(), Out.size());
+}
+
+void Twine::printOneChild(raw_ostream &OS, const void *Ptr,
                           NodeKind Kind) const {
   switch (Kind) {
   case Twine::NullKind: break;
   case Twine::EmptyKind: break;
   case Twine::TwineKind:
-    static_cast<const Twine*>(Ptr)->print(OS); 
+    static_cast<const Twine*>(Ptr)->print(OS);
     break;
-  case Twine::CStringKind: 
-    OS << static_cast<const char*>(Ptr); 
+  case Twine::CStringKind:
+    OS << static_cast<const char*>(Ptr);
     break;
   case Twine::StdStringKind:
-    OS << *static_cast<const std::string*>(Ptr); 
+    OS << *static_cast<const std::string*>(Ptr);
     break;
   case Twine::StringRefKind:
-    OS << *static_cast<const StringRef*>(Ptr); 
+    OS << *static_cast<const StringRef*>(Ptr);
     break;
   case Twine::DecUIKind:
-    OS << *static_cast<const unsigned int*>(Ptr);
+    OS << (unsigned)(uintptr_t)Ptr;
     break;
   case Twine::DecIKind:
-    OS << *static_cast<const int*>(Ptr);
+    OS << (int)(intptr_t)Ptr;
     break;
   case Twine::DecULKind:
     OS << *static_cast<const unsigned long*>(Ptr);
@@ -71,7 +91,7 @@ void Twine::printOneChild(raw_ostream &OS, const void *Ptr,
   }
 }
 
-void Twine::printOneChildRepr(raw_ostream &OS, const void *Ptr, 
+void Twine::printOneChildRepr(raw_ostream &OS, const void *Ptr,
                               NodeKind Kind) const {
   switch (Kind) {
   case Twine::NullKind:
@@ -95,10 +115,10 @@ void Twine::printOneChildRepr(raw_ostream &OS, const void *Ptr,
        << static_cast<const StringRef*>(Ptr) << "\"";
     break;
   case Twine::DecUIKind:
-    OS << "decUI:\"" << *static_cast<const unsigned int*>(Ptr) << "\"";
+    OS << "decUI:\"" << (unsigned)(uintptr_t)Ptr << "\"";
     break;
   case Twine::DecIKind:
-    OS << "decI:\"" << *static_cast<const int*>(Ptr) << "\"";
+    OS << "decI:\"" << (int)(intptr_t)Ptr << "\"";
     break;
   case Twine::DecULKind:
     OS << "decUL:\"" << *static_cast<const unsigned long*>(Ptr) << "\"";
@@ -132,9 +152,9 @@ void Twine::printRepr(raw_ostream &OS) const {
 }
 
 void Twine::dump() const {
-  print(llvm::errs());
+  print(llvm::dbgs());
 }
 
 void Twine::dumpRepr() const {
-  printRepr(llvm::errs());
+  printRepr(llvm::dbgs());
 }