LLVM  8.0.1
StringTableBuilder.cpp
Go to the documentation of this file.
1 //===- StringTableBuilder.cpp - String table building utility -------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
12 #include "llvm/ADT/SmallString.h"
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/BinaryFormat/COFF.h"
15 #include "llvm/Support/Endian.h"
18 #include <cassert>
19 #include <cstddef>
20 #include <cstdint>
21 #include <cstring>
22 #include <utility>
23 #include <vector>
24 
25 using namespace llvm;
26 
28 
29 void StringTableBuilder::initSize() {
30  // Account for leading bytes in table so that offsets returned from add are
31  // correct.
32  switch (K) {
33  case RAW:
34  case DWARF:
35  Size = 0;
36  break;
37  case MachO:
38  case ELF:
39  // Start the table with a NUL byte.
40  Size = 1;
41  break;
42  case WinCOFF:
43  // Make room to write the table size later.
44  Size = 4;
45  break;
46  }
47 }
48 
50  : K(K), Alignment(Alignment) {
51  initSize();
52 }
53 
55  assert(isFinalized());
57  Data.resize(getSize());
58  write((uint8_t *)Data.data());
59  OS << Data;
60 }
61 
62 using StringPair = std::pair<CachedHashStringRef, size_t>;
63 
64 void StringTableBuilder::write(uint8_t *Buf) const {
65  assert(isFinalized());
66  for (const StringPair &P : StringIndexMap) {
67  StringRef Data = P.first.val();
68  if (!Data.empty())
69  memcpy(Buf + P.second, Data.data(), Data.size());
70  }
71  if (K != WinCOFF)
72  return;
73  support::endian::write32le(Buf, Size);
74 }
75 
76 // Returns the character at Pos from end of a string.
77 static int charTailAt(StringPair *P, size_t Pos) {
78  StringRef S = P->first.val();
79  if (Pos >= S.size())
80  return -1;
81  return (unsigned char)S[S.size() - Pos - 1];
82 }
83 
84 // Three-way radix quicksort. This is much faster than std::sort with strcmp
85 // because it does not compare characters that we already know the same.
86 static void multikeySort(MutableArrayRef<StringPair *> Vec, int Pos) {
87 tailcall:
88  if (Vec.size() <= 1)
89  return;
90 
91  // Partition items so that items in [0, I) are greater than the pivot,
92  // [I, J) are the same as the pivot, and [J, Vec.size()) are less than
93  // the pivot.
94  int Pivot = charTailAt(Vec[0], Pos);
95  size_t I = 0;
96  size_t J = Vec.size();
97  for (size_t K = 1; K < J;) {
98  int C = charTailAt(Vec[K], Pos);
99  if (C > Pivot)
100  std::swap(Vec[I++], Vec[K++]);
101  else if (C < Pivot)
102  std::swap(Vec[--J], Vec[K]);
103  else
104  K++;
105  }
106 
107  multikeySort(Vec.slice(0, I), Pos);
108  multikeySort(Vec.slice(J), Pos);
109 
110  // multikeySort(Vec.slice(I, J - I), Pos + 1), but with
111  // tail call optimization.
112  if (Pivot != -1) {
113  Vec = Vec.slice(I, J - I);
114  ++Pos;
115  goto tailcall;
116  }
117 }
118 
120  assert(K != DWARF);
121  finalizeStringTable(/*Optimize=*/true);
122 }
123 
125  finalizeStringTable(/*Optimize=*/false);
126 }
127 
128 void StringTableBuilder::finalizeStringTable(bool Optimize) {
129  Finalized = true;
130 
131  if (Optimize) {
132  std::vector<StringPair *> Strings;
133  Strings.reserve(StringIndexMap.size());
134  for (StringPair &P : StringIndexMap)
135  Strings.push_back(&P);
136 
137  multikeySort(Strings, 0);
138  initSize();
139 
140  StringRef Previous;
141  for (StringPair *P : Strings) {
142  StringRef S = P->first.val();
143  if (Previous.endswith(S)) {
144  size_t Pos = Size - S.size() - (K != RAW);
145  if (!(Pos & (Alignment - 1))) {
146  P->second = Pos;
147  continue;
148  }
149  }
150 
151  Size = alignTo(Size, Alignment);
152  P->second = Size;
153 
154  Size += S.size();
155  if (K != RAW)
156  ++Size;
157  Previous = S;
158  }
159  }
160 
161  if (K == MachO)
162  Size = alignTo(Size, 4); // Pad to multiple of 4.
163 }
164 
166  Finalized = false;
167  StringIndexMap.clear();
168 }
169 
171  assert(isFinalized());
172  auto I = StringIndexMap.find(S);
173  assert(I != StringIndexMap.end() && "String is not in table!");
174  return I->second;
175 }
176 
178  if (K == WinCOFF)
179  assert(S.size() > COFF::NameSize && "Short string in COFF string table!");
180 
181  assert(!isFinalized());
182  auto P = StringIndexMap.insert(std::make_pair(S, 0));
183  if (P.second) {
184  size_t Start = alignTo(Size, Alignment);
185  P.first->second = Start;
186  Size = Start + S.size() + (K != RAW);
187  }
188  return P.first->second;
189 }
uint64_t CallInst * C
MutableArrayRef< T > slice(size_t N, size_t M) const
slice(n, m) - Chop off the first N elements of the array, and keep M elements in the array...
Definition: ArrayRef.h:351
A container which contains a StringRef plus a precomputed hash.
This class represents lattice values for constants.
Definition: AllocatorList.h:24
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
Definition: StringRef.h:138
void write32le(void *P, uint32_t V)
Definition: Endian.h:404
uint64_t alignTo(uint64_t Value, uint64_t Align, uint64_t Skew=0)
Returns the next integer (mod 2**64) that is greater than or equal to Value and is a multiple of Alig...
Definition: MathExtras.h:685
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:128
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool endswith(StringRef Suffix) const
Check if this string ends with the given Suffix.
Definition: StringRef.h:279
void write(raw_ostream &OS) const
size_t add(CachedHashStringRef S)
Add a string to the builder.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:133
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
static int charTailAt(StringPair *P, size_t Pos)
#define P(N)
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:291
void finalizeInOrder()
Finalize the string table without reording it.
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:149
void finalize()
Analyze the strings and build the final table.
size_t getOffset(CachedHashStringRef S) const
Get the offest of a string in the string table.
StringTableBuilder(Kind K, unsigned Alignment=1)
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:941
pointer data()
Return a pointer to the vector&#39;s buffer, even if empty().
Definition: SmallVector.h:149
#define I(x, y, z)
Definition: MD5.cpp:58
std::pair< CachedHashStringRef, size_t > StringPair
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static void multikeySort(MutableArrayRef< StringPair *> Vec, int Pos)
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
void resize(size_type N)
Definition: SmallVector.h:351