Remove templates from CostTableLookup functions. All instantiations had the same...
[oota-llvm.git] / lib / MC / StringTableBuilder.cpp
index 9de9363611e67ee3510f427d3bd6492ae375a12f..80e552287b3dd76396e5c17d71b45c465a670aa1 100644 (file)
@@ -8,35 +8,71 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/MC/StringTableBuilder.h"
-#include "llvm/ADT/SmallVector.h"
+#include "llvm/ADT/STLExtras.h"
 #include "llvm/Support/COFF.h"
 #include "llvm/Support/Endian.h"
 
+#include <vector>
+
 using namespace llvm;
 
-static bool compareBySuffix(StringRef a, StringRef b) {
-  size_t sizeA = a.size();
-  size_t sizeB = b.size();
-  size_t len = std::min(sizeA, sizeB);
-  for (size_t i = 0; i < len; ++i) {
-    char ca = a[sizeA - i - 1];
-    char cb = b[sizeB - i - 1];
-    if (ca != cb)
-      return ca > cb;
+StringTableBuilder::StringTableBuilder(Kind K) : K(K) {}
+
+typedef std::pair<StringRef, size_t> StringPair;
+
+// Returns the character at Pos from end of a string.
+static int charTailAt(StringPair *P, size_t Pos) {
+  StringRef S = P->first;
+  if (Pos >= S.size())
+    return -1;
+  return (unsigned char)S[S.size() - Pos - 1];
+}
+
+// Three-way radix quicksort. This is much faster than std::sort with strcmp
+// because it does not compare characters that we already know the same.
+static void multikey_qsort(StringPair **Begin, StringPair **End, int Pos) {
+tailcall:
+  if (End - Begin <= 1)
+    return;
+
+  // Partition items. Items in [Begin, P) are greater than the pivot,
+  // [P, Q) are the same as the pivot, and [Q, End) are less than the pivot.
+  int Pivot = charTailAt(*Begin, Pos);
+  StringPair **P = Begin;
+  StringPair **Q = End;
+  for (StringPair **R = Begin + 1; R < Q;) {
+    int C = charTailAt(*R, Pos);
+    if (C > Pivot)
+      std::swap(*P++, *R++);
+    else if (C < Pivot)
+      std::swap(*--Q, *R);
+    else
+      R++;
+  }
+
+  multikey_qsort(Begin, P, Pos);
+  multikey_qsort(Q, End, Pos);
+  if (Pivot != -1) {
+    // qsort(P, Q, Pos + 1), but with tail call optimization.
+    Begin = P;
+    End = Q;
+    ++Pos;
+    goto tailcall;
   }
-  return sizeA > sizeB;
 }
 
-void StringTableBuilder::finalize(Kind kind) {
-  SmallVector<StringRef, 8> Strings;
+void StringTableBuilder::finalize() {
+  std::vector<std::pair<StringRef, size_t> *> Strings;
   Strings.reserve(StringIndexMap.size());
+  for (std::pair<StringRef, size_t> &P : StringIndexMap)
+    Strings.push_back(&P);
 
-  for (auto i = StringIndexMap.begin(), e = StringIndexMap.end(); i != e; ++i)
-    Strings.push_back(i->getKey());
-
-  std::sort(Strings.begin(), Strings.end(), compareBySuffix);
+  if (!Strings.empty())
+    multikey_qsort(&Strings[0], &Strings[0] + Strings.size(), 0);
 
-  switch (kind) {
+  switch (K) {
+  case RAW:
+    break;
   case ELF:
   case MachO:
     // Start the table with a NUL byte.
@@ -49,22 +85,25 @@ void StringTableBuilder::finalize(Kind kind) {
   }
 
   StringRef Previous;
-  for (StringRef s : Strings) {
-    if (kind == WinCOFF)
-      assert(s.size() > COFF::NameSize && "Short string in COFF string table!");
+  for (std::pair<StringRef, size_t> *P : Strings) {
+    StringRef S = P->first;
+    if (K == WinCOFF)
+      assert(S.size() > COFF::NameSize && "Short string in COFF string table!");
 
-    if (Previous.endswith(s)) {
-      StringIndexMap[s] = StringTable.size() - 1 - s.size();
+    if (Previous.endswith(S)) {
+      P->second = StringTable.size() - S.size() - (K != RAW);
       continue;
     }
 
-    StringIndexMap[s] = StringTable.size();
-    StringTable += s;
-    StringTable += '\x00';
-    Previous = s;
+    P->second = StringTable.size();
+    StringTable += S;
+    if (K != RAW)
+      StringTable += '\x00';
+    Previous = S;
   }
 
-  switch (kind) {
+  switch (K) {
+  case RAW:
   case ELF:
     break;
   case MachO:
@@ -75,14 +114,31 @@ void StringTableBuilder::finalize(Kind kind) {
   case WinCOFF:
     // Write the table size in the first word.
     assert(StringTable.size() <= std::numeric_limits<uint32_t>::max());
-    uint32_t size = static_cast<uint32_t>(StringTable.size());
+    uint32_t Size = static_cast<uint32_t>(StringTable.size());
     support::endian::write<uint32_t, support::little, support::unaligned>(
-        StringTable.data(), size);
+        StringTable.data(), Size);
     break;
   }
+
+  Size = StringTable.size();
 }
 
 void StringTableBuilder::clear() {
   StringTable.clear();
   StringIndexMap.clear();
 }
+
+size_t StringTableBuilder::getOffset(StringRef S) const {
+  assert(isFinalized());
+  auto I = StringIndexMap.find(S);
+  assert(I != StringIndexMap.end() && "String is not in table!");
+  return I->second;
+}
+
+size_t StringTableBuilder::add(StringRef S) {
+  assert(!isFinalized());
+  auto P = StringIndexMap.insert(std::make_pair(S, Size));
+  if (P.second)
+    Size += S.size() + (K != RAW);
+  return P.first->second;
+}