LLVM  4.0.0
DwarfAccelTable.cpp
Go to the documentation of this file.
1 //=-- llvm/CodeGen/DwarfAccelTable.cpp - Dwarf Accelerator Tables -*- C++ -*-=//
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 //
10 // This file contains support for writing dwarf accelerator tables.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "DwarfAccelTable.h"
15 #include "DwarfCompileUnit.h"
16 #include "DwarfDebug.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/Twine.h"
20 #include "llvm/CodeGen/DIE.h"
21 #include "llvm/MC/MCExpr.h"
22 #include "llvm/MC/MCStreamer.h"
23 #include "llvm/MC/MCSymbol.h"
24 #include "llvm/Support/Debug.h"
25 
26 using namespace llvm;
27 
28 // The length of the header data is always going to be 4 + 4 + 4*NumAtoms.
29 DwarfAccelTable::DwarfAccelTable(ArrayRef<DwarfAccelTable::Atom> atomList)
30  : Header(8 + (atomList.size() * 4)), HeaderData(atomList),
31  Entries(Allocator) {}
32 
34  char Flags) {
35  assert(Data.empty() && "Already finalized!");
36  // If the string is in the list already then add this die to the list
37  // otherwise add a new one.
38  DataArray &DIEs = Entries[Name.getString()];
39  assert(!DIEs.Name || DIEs.Name == Name);
40  DIEs.Name = Name;
41  DIEs.Values.push_back(new (Allocator) HashDataContents(die, Flags));
42 }
43 
44 void DwarfAccelTable::ComputeBucketCount() {
45  // First get the number of unique hashes.
46  std::vector<uint32_t> uniques(Data.size());
47  for (size_t i = 0, e = Data.size(); i < e; ++i)
48  uniques[i] = Data[i]->HashValue;
49  array_pod_sort(uniques.begin(), uniques.end());
50  std::vector<uint32_t>::iterator p =
51  std::unique(uniques.begin(), uniques.end());
52  uint32_t num = std::distance(uniques.begin(), p);
53 
54  // Then compute the bucket size, minimum of 1 bucket.
55  if (num > 1024)
56  Header.bucket_count = num / 4;
57  else if (num > 16)
58  Header.bucket_count = num / 2;
59  else
60  Header.bucket_count = num > 0 ? num : 1;
61 
62  Header.hashes_count = num;
63 }
64 
65 // compareDIEs - comparison predicate that sorts DIEs by their offset.
68  return A->Die->getOffset() < B->Die->getOffset();
69 }
70 
72  // Create the individual hash data outputs.
73  Data.reserve(Entries.size());
74  for (StringMap<DataArray>::iterator EI = Entries.begin(), EE = Entries.end();
75  EI != EE; ++EI) {
76 
77  // Unique the entries.
78  std::stable_sort(EI->second.Values.begin(), EI->second.Values.end(), compareDIEs);
79  EI->second.Values.erase(
80  std::unique(EI->second.Values.begin(), EI->second.Values.end()),
81  EI->second.Values.end());
82 
83  HashData *Entry = new (Allocator) HashData(EI->getKey(), EI->second);
84  Data.push_back(Entry);
85  }
86 
87  // Figure out how many buckets we need, then compute the bucket
88  // contents and the final ordering. We'll emit the hashes and offsets
89  // by doing a walk during the emission phase. We add temporary
90  // symbols to the data so that we can reference them during the offset
91  // later, we'll emit them when we emit the data.
92  ComputeBucketCount();
93 
94  // Compute bucket contents and final ordering.
95  Buckets.resize(Header.bucket_count);
96  for (size_t i = 0, e = Data.size(); i < e; ++i) {
97  uint32_t bucket = Data[i]->HashValue % Header.bucket_count;
98  Buckets[bucket].push_back(Data[i]);
99  Data[i]->Sym = Asm->createTempSymbol(Prefix);
100  }
101 
102  // Sort the contents of the buckets by hash value so that hash
103  // collisions end up together. Stable sort makes testing easier and
104  // doesn't cost much more.
105  for (size_t i = 0; i < Buckets.size(); ++i)
106  std::stable_sort(Buckets[i].begin(), Buckets[i].end(),
107  [] (HashData *LHS, HashData *RHS) {
108  return LHS->HashValue < RHS->HashValue;
109  });
110 }
111 
112 // Emits the header for the table via the AsmPrinter.
113 void DwarfAccelTable::EmitHeader(AsmPrinter *Asm) {
114  Asm->OutStreamer->AddComment("Header Magic");
115  Asm->EmitInt32(Header.magic);
116  Asm->OutStreamer->AddComment("Header Version");
117  Asm->EmitInt16(Header.version);
118  Asm->OutStreamer->AddComment("Header Hash Function");
119  Asm->EmitInt16(Header.hash_function);
120  Asm->OutStreamer->AddComment("Header Bucket Count");
121  Asm->EmitInt32(Header.bucket_count);
122  Asm->OutStreamer->AddComment("Header Hash Count");
123  Asm->EmitInt32(Header.hashes_count);
124  Asm->OutStreamer->AddComment("Header Data Length");
125  Asm->EmitInt32(Header.header_data_len);
126  Asm->OutStreamer->AddComment("HeaderData Die Offset Base");
127  Asm->EmitInt32(HeaderData.die_offset_base);
128  Asm->OutStreamer->AddComment("HeaderData Atom Count");
129  Asm->EmitInt32(HeaderData.Atoms.size());
130  for (size_t i = 0; i < HeaderData.Atoms.size(); i++) {
131  Atom A = HeaderData.Atoms[i];
132  Asm->OutStreamer->AddComment(dwarf::AtomTypeString(A.type));
133  Asm->EmitInt16(A.type);
134  Asm->OutStreamer->AddComment(dwarf::FormEncodingString(A.form));
135  Asm->EmitInt16(A.form);
136  }
137 }
138 
139 // Walk through and emit the buckets for the table. Each index is
140 // an offset into the list of hashes.
141 void DwarfAccelTable::EmitBuckets(AsmPrinter *Asm) {
142  unsigned index = 0;
143  for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
144  Asm->OutStreamer->AddComment("Bucket " + Twine(i));
145  if (Buckets[i].size() != 0)
146  Asm->EmitInt32(index);
147  else
148  Asm->EmitInt32(UINT32_MAX);
149  // Buckets point in the list of hashes, not to the data. Do not
150  // increment the index multiple times in case of hash collisions.
151  uint64_t PrevHash = UINT64_MAX;
152  for (auto *HD : Buckets[i]) {
153  uint32_t HashValue = HD->HashValue;
154  if (PrevHash != HashValue)
155  ++index;
156  PrevHash = HashValue;
157  }
158  }
159 }
160 
161 // Walk through the buckets and emit the individual hashes for each
162 // bucket.
163 void DwarfAccelTable::EmitHashes(AsmPrinter *Asm) {
164  uint64_t PrevHash = UINT64_MAX;
165  for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
166  for (HashList::const_iterator HI = Buckets[i].begin(),
167  HE = Buckets[i].end();
168  HI != HE; ++HI) {
169  uint32_t HashValue = (*HI)->HashValue;
170  if (PrevHash == HashValue)
171  continue;
172  Asm->OutStreamer->AddComment("Hash in Bucket " + Twine(i));
173  Asm->EmitInt32(HashValue);
174  PrevHash = HashValue;
175  }
176  }
177 }
178 
179 // Walk through the buckets and emit the individual offsets for each
180 // element in each bucket. This is done via a symbol subtraction from the
181 // beginning of the section. The non-section symbol will be output later
182 // when we emit the actual data.
183 void DwarfAccelTable::emitOffsets(AsmPrinter *Asm, const MCSymbol *SecBegin) {
184  uint64_t PrevHash = UINT64_MAX;
185  for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
186  for (HashList::const_iterator HI = Buckets[i].begin(),
187  HE = Buckets[i].end();
188  HI != HE; ++HI) {
189  uint32_t HashValue = (*HI)->HashValue;
190  if (PrevHash == HashValue)
191  continue;
192  PrevHash = HashValue;
193  Asm->OutStreamer->AddComment("Offset in Bucket " + Twine(i));
194  MCContext &Context = Asm->OutStreamer->getContext();
195  const MCExpr *Sub = MCBinaryExpr::createSub(
196  MCSymbolRefExpr::create((*HI)->Sym, Context),
197  MCSymbolRefExpr::create(SecBegin, Context), Context);
198  Asm->OutStreamer->EmitValue(Sub, sizeof(uint32_t));
199  }
200  }
201 }
202 
203 // Walk through the buckets and emit the full data for each element in
204 // the bucket. For the string case emit the dies and the various offsets.
205 // Terminate each HashData bucket with 0.
206 void DwarfAccelTable::EmitData(AsmPrinter *Asm, DwarfDebug *D) {
207  for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
208  uint64_t PrevHash = UINT64_MAX;
209  for (HashList::const_iterator HI = Buckets[i].begin(),
210  HE = Buckets[i].end();
211  HI != HE; ++HI) {
212  // Terminate the previous entry if there is no hash collision
213  // with the current one.
214  if (PrevHash != UINT64_MAX && PrevHash != (*HI)->HashValue)
215  Asm->EmitInt32(0);
216  // Remember to emit the label for our offset.
217  Asm->OutStreamer->EmitLabel((*HI)->Sym);
218  Asm->OutStreamer->AddComment((*HI)->Str);
219  Asm->emitDwarfStringOffset((*HI)->Data.Name);
220  Asm->OutStreamer->AddComment("Num DIEs");
221  Asm->EmitInt32((*HI)->Data.Values.size());
222  for (HashDataContents *HD : (*HI)->Data.Values) {
223  // Emit the DIE offset
224  Asm->EmitInt32(HD->Die->getDebugSectionOffset());
225  // If we have multiple Atoms emit that info too.
226  // FIXME: A bit of a hack, we either emit only one atom or all info.
227  if (HeaderData.Atoms.size() > 1) {
228  Asm->EmitInt16(HD->Die->getTag());
229  Asm->EmitInt8(HD->Flags);
230  }
231  }
232  PrevHash = (*HI)->HashValue;
233  }
234  // Emit the final end marker for the bucket.
235  if (!Buckets[i].empty())
236  Asm->EmitInt32(0);
237  }
238 }
239 
240 // Emit the entire data structure to the output file.
241 void DwarfAccelTable::emit(AsmPrinter *Asm, const MCSymbol *SecBegin,
242  DwarfDebug *D) {
243  // Emit the header.
244  EmitHeader(Asm);
245 
246  // Emit the buckets.
247  EmitBuckets(Asm);
248 
249  // Emit the hashes.
250  EmitHashes(Asm);
251 
252  // Emit the offsets.
253  emitOffsets(Asm, SecBegin);
254 
255  // Emit the hash data.
256  EmitData(Asm, D);
257 }
258 
259 #ifndef NDEBUG
261 
262  Header.print(O);
263  HeaderData.print(O);
264 
265  O << "Entries: \n";
266  for (StringMap<DataArray>::const_iterator EI = Entries.begin(),
267  EE = Entries.end();
268  EI != EE; ++EI) {
269  O << "Name: " << EI->getKeyData() << "\n";
270  for (HashDataContents *HD : EI->second.Values)
271  HD->print(O);
272  }
273 
274  O << "Buckets and Hashes: \n";
275  for (size_t i = 0, e = Buckets.size(); i < e; ++i)
276  for (HashList::const_iterator HI = Buckets[i].begin(),
277  HE = Buckets[i].end();
278  HI != HE; ++HI)
279  (*HI)->print(O);
280 
281  O << "Data: \n";
282  for (std::vector<HashData *>::const_iterator DI = Data.begin(),
283  DE = Data.end();
284  DI != DE; ++DI)
285  (*DI)->print(O);
286 }
287 #endif
LLVMContext & Context
std::unique_ptr< MCStreamer > OutStreamer
This is the MCStreamer object for the file we are generating.
Definition: AsmPrinter.h:84
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:298
size_t i
StringRef AtomTypeString(unsigned Atom)
Definition: Dwarf.cpp:307
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:39
void EmitInt8(int Value) const
Emit a byte directive and value.
void print(raw_ostream &O) const
Collects and handles dwarf debug information.
Definition: DwarfDebug.h:196
void emit(AsmPrinter *, const MCSymbol *, DwarfDebug *)
const_iterator begin(StringRef path)
Get begin iterator over path.
Definition: Path.cpp:233
void EmitInt32(int Value) const
Emit a long directive and value.
struct fuzzer::@269 Flags
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
StringRef FormEncodingString(unsigned Encoding)
Definition: Dwarf.cpp:58
String pool entry reference.
static GCRegistry::Add< StatepointGC > D("statepoint-example","an example strategy for statepoint")
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:34
Context object for machine code objects.
Definition: MCContext.h:51
static const MCBinaryExpr * createSub(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:497
void FinalizeTable(AsmPrinter *, StringRef)
void EmitInt16(int Value) const
Emit a short directive and value.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
void AddName(DwarfStringPoolEntryRef Name, const DIE *Die, char Flags=0)
static GCRegistry::Add< OcamlGC > B("ocaml","ocaml 3.10-compatible GC")
void array_pod_sort(IteratorTy Start, IteratorTy End)
array_pod_sort - This sorts an array with the specified start and end extent.
Definition: STLExtras.h:689
A structured debug information entry.
Definition: DIE.h:655
unsigned size() const
Definition: StringMap.h:114
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:67
Greedy Register Allocator
static bool compareDIEs(const DwarfAccelTable::HashDataContents *A, const DwarfAccelTable::HashDataContents *B)
unsigned getOffset() const
Get the compile/type unit relative offset of this DIE.
Definition: DIE.h:694
void print(raw_ostream &O)
MCSymbol * createTempSymbol(const Twine &Name) const
iterator begin()
Definition: StringMap.h:302
void emitDwarfStringOffset(DwarfStringPoolEntryRef S) const
Emit the 4-byte offset of a string from the start of its section.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47
static GCRegistry::Add< ErlangGC > A("erlang","erlang-compatible garbage collector")
iterator end()
Definition: StringMap.h:305