blob: 4ad3e1867328f242c10bc27eeb62f169ac3c87e6 [file] [log] [blame]
Eric Christopher6e472042011-11-07 09:18:42 +00001//=-- 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
Craig Topper6e80c282012-03-26 06:58:25 +000014#include "DwarfAccelTable.h"
Frederic Riss3a6b3542014-11-12 23:48:14 +000015#include "DwarfCompileUnit.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "DwarfDebug.h"
Benjamin Kramer3e6719c2012-03-26 14:17:26 +000017#include "llvm/ADT/STLExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/ADT/Twine.h"
Eric Christopher6e472042011-11-07 09:18:42 +000019#include "llvm/CodeGen/AsmPrinter.h"
Frederic Risse541e0b2015-01-05 21:29:41 +000020#include "llvm/CodeGen/DIE.h"
Eric Christopher6e472042011-11-07 09:18:42 +000021#include "llvm/MC/MCExpr.h"
22#include "llvm/MC/MCStreamer.h"
23#include "llvm/MC/MCSymbol.h"
24#include "llvm/Support/Debug.h"
Eric Christopher6e472042011-11-07 09:18:42 +000025
26using namespace llvm;
27
Eric Christopher21bde872012-01-06 04:35:23 +000028// The length of the header data is always going to be 4 + 4 + 4*NumAtoms.
Eric Christopherb4e2cc42013-09-05 16:46:43 +000029DwarfAccelTable::DwarfAccelTable(ArrayRef<DwarfAccelTable::Atom> atomList)
30 : Header(8 + (atomList.size() * 4)), HeaderData(atomList),
31 Entries(Allocator) {}
Eric Christopher21bde872012-01-06 04:35:23 +000032
Duncan P. N. Exon Smithf4599942015-05-24 16:44:32 +000033void DwarfAccelTable::AddName(DwarfStringPoolEntryRef Name, const DIE *die,
David Blaikie772ab8a2014-04-25 22:21:35 +000034 char Flags) {
Benjamin Kramer330970d2012-04-13 20:06:17 +000035 assert(Data.empty() && "Already finalized!");
Eric Christopher6e472042011-11-07 09:18:42 +000036 // If the string is in the list already then add this die to the list
37 // otherwise add a new one.
Duncan P. N. Exon Smithf4599942015-05-24 16:44:32 +000038 DataArray &DIEs = Entries[Name.getString()];
39 assert(!DIEs.Name || DIEs.Name == Name);
40 DIEs.Name = Name;
David Blaikie772ab8a2014-04-25 22:21:35 +000041 DIEs.Values.push_back(new (Allocator) HashDataContents(die, Flags));
Eric Christopher6e472042011-11-07 09:18:42 +000042}
43
Eugene Zelenkoffec81c2015-11-04 22:32:32 +000044void DwarfAccelTable::ComputeBucketCount() {
Eric Christopher6e472042011-11-07 09:18:42 +000045 // First get the number of unique hashes.
Benjamin Kramer3e6719c2012-03-26 14:17:26 +000046 std::vector<uint32_t> uniques(Data.size());
Eric Christopher54ce295d2011-11-08 18:38:40 +000047 for (size_t i = 0, e = Data.size(); i < e; ++i)
Eric Christopher6e472042011-11-07 09:18:42 +000048 uniques[i] = Data[i]->HashValue;
Benjamin Kramer3e6719c2012-03-26 14:17:26 +000049 array_pod_sort(uniques.begin(), uniques.end());
Eric Christopher6e472042011-11-07 09:18:42 +000050 std::vector<uint32_t>::iterator p =
Eric Christopherb4e2cc42013-09-05 16:46:43 +000051 std::unique(uniques.begin(), uniques.end());
Eric Christopher6e472042011-11-07 09:18:42 +000052 uint32_t num = std::distance(uniques.begin(), p);
53
54 // Then compute the bucket size, minimum of 1 bucket.
Eric Christopherb4e2cc42013-09-05 16:46:43 +000055 if (num > 1024)
56 Header.bucket_count = num / 4;
Frederic Riss2ad51882015-03-09 21:09:50 +000057 else if (num > 16)
Eric Christopherb4e2cc42013-09-05 16:46:43 +000058 Header.bucket_count = num / 2;
59 else
60 Header.bucket_count = num > 0 ? num : 1;
Eric Christopher6e472042011-11-07 09:18:42 +000061
62 Header.hashes_count = num;
63}
64
Benjamin Kramer330970d2012-04-13 20:06:17 +000065// compareDIEs - comparison predicate that sorts DIEs by their offset.
66static bool compareDIEs(const DwarfAccelTable::HashDataContents *A,
67 const DwarfAccelTable::HashDataContents *B) {
68 return A->Die->getOffset() < B->Die->getOffset();
Eric Christopher0abbd0e2011-11-15 23:37:17 +000069}
70
Benjamin Kramer63e39eb2013-05-11 18:24:28 +000071void DwarfAccelTable::FinalizeTable(AsmPrinter *Asm, StringRef Prefix) {
Eric Christopher6e472042011-11-07 09:18:42 +000072 // Create the individual hash data outputs.
Benjamin Kramer49a11322015-02-28 20:15:00 +000073 Data.reserve(Entries.size());
Eric Christopherb4e2cc42013-09-05 16:46:43 +000074 for (StringMap<DataArray>::iterator EI = Entries.begin(), EE = Entries.end();
75 EI != EE; ++EI) {
Eric Christopherd9843b32011-11-10 19:25:34 +000076
77 // Unique the entries.
David Blaikie772ab8a2014-04-25 22:21:35 +000078 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());
Eric Christopherd9843b32011-11-10 19:25:34 +000082
Benjamin Kramer330970d2012-04-13 20:06:17 +000083 HashData *Entry = new (Allocator) HashData(EI->getKey(), EI->second);
Eric Christopher6e472042011-11-07 09:18:42 +000084 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);
Eric Christopher54ce295d2011-11-08 18:38:40 +000096 for (size_t i = 0, e = Data.size(); i < e; ++i) {
Eric Christopher6e472042011-11-07 09:18:42 +000097 uint32_t bucket = Data[i]->HashValue % Header.bucket_count;
98 Buckets[bucket].push_back(Data[i]);
Rafael Espindola9ab09232015-03-17 20:07:06 +000099 Data[i]->Sym = Asm->createTempSymbol(Prefix);
Eric Christopher6e472042011-11-07 09:18:42 +0000100 }
Frederic Riss0e9a50f2015-03-10 00:46:31 +0000101
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 });
Eric Christopher6e472042011-11-07 09:18:42 +0000110}
111
112// Emits the header for the table via the AsmPrinter.
113void DwarfAccelTable::EmitHeader(AsmPrinter *Asm) {
Lang Hames9ff69c82015-04-24 19:11:51 +0000114 Asm->OutStreamer->AddComment("Header Magic");
Eric Christopher6e472042011-11-07 09:18:42 +0000115 Asm->EmitInt32(Header.magic);
Lang Hames9ff69c82015-04-24 19:11:51 +0000116 Asm->OutStreamer->AddComment("Header Version");
Eric Christopher6e472042011-11-07 09:18:42 +0000117 Asm->EmitInt16(Header.version);
Lang Hames9ff69c82015-04-24 19:11:51 +0000118 Asm->OutStreamer->AddComment("Header Hash Function");
Eric Christopher6e472042011-11-07 09:18:42 +0000119 Asm->EmitInt16(Header.hash_function);
Lang Hames9ff69c82015-04-24 19:11:51 +0000120 Asm->OutStreamer->AddComment("Header Bucket Count");
Eric Christopher6e472042011-11-07 09:18:42 +0000121 Asm->EmitInt32(Header.bucket_count);
Lang Hames9ff69c82015-04-24 19:11:51 +0000122 Asm->OutStreamer->AddComment("Header Hash Count");
Eric Christopher6e472042011-11-07 09:18:42 +0000123 Asm->EmitInt32(Header.hashes_count);
Lang Hames9ff69c82015-04-24 19:11:51 +0000124 Asm->OutStreamer->AddComment("Header Data Length");
Eric Christopher6e472042011-11-07 09:18:42 +0000125 Asm->EmitInt32(Header.header_data_len);
Lang Hames9ff69c82015-04-24 19:11:51 +0000126 Asm->OutStreamer->AddComment("HeaderData Die Offset Base");
Eric Christopher6e472042011-11-07 09:18:42 +0000127 Asm->EmitInt32(HeaderData.die_offset_base);
Lang Hames9ff69c82015-04-24 19:11:51 +0000128 Asm->OutStreamer->AddComment("HeaderData Atom Count");
Eric Christopher6e472042011-11-07 09:18:42 +0000129 Asm->EmitInt32(HeaderData.Atoms.size());
130 for (size_t i = 0; i < HeaderData.Atoms.size(); i++) {
131 Atom A = HeaderData.Atoms[i];
Lang Hames9ff69c82015-04-24 19:11:51 +0000132 Asm->OutStreamer->AddComment(dwarf::AtomTypeString(A.type));
Eric Christopher6e472042011-11-07 09:18:42 +0000133 Asm->EmitInt16(A.type);
Lang Hames9ff69c82015-04-24 19:11:51 +0000134 Asm->OutStreamer->AddComment(dwarf::FormEncodingString(A.form));
Eric Christopher6e472042011-11-07 09:18:42 +0000135 Asm->EmitInt16(A.form);
136 }
137}
138
Eric Christopher28611362012-10-08 23:53:45 +0000139// Walk through and emit the buckets for the table. Each index is
140// an offset into the list of hashes.
Eric Christopher6e472042011-11-07 09:18:42 +0000141void DwarfAccelTable::EmitBuckets(AsmPrinter *Asm) {
142 unsigned index = 0;
Eric Christopher54ce295d2011-11-08 18:38:40 +0000143 for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
Lang Hames9ff69c82015-04-24 19:11:51 +0000144 Asm->OutStreamer->AddComment("Bucket " + Twine(i));
Eric Christopher6e472042011-11-07 09:18:42 +0000145 if (Buckets[i].size() != 0)
146 Asm->EmitInt32(index);
147 else
148 Asm->EmitInt32(UINT32_MAX);
Frederic Riss0e9a50f2015-03-10 00:46:31 +0000149 // 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 }
Eric Christopher6e472042011-11-07 09:18:42 +0000158 }
159}
160
161// Walk through the buckets and emit the individual hashes for each
162// bucket.
163void DwarfAccelTable::EmitHashes(AsmPrinter *Asm) {
Frederic Riss0e9a50f2015-03-10 00:46:31 +0000164 uint64_t PrevHash = UINT64_MAX;
Eric Christopher54ce295d2011-11-08 18:38:40 +0000165 for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
Eric Christopher6e472042011-11-07 09:18:42 +0000166 for (HashList::const_iterator HI = Buckets[i].begin(),
Eric Christopherb4e2cc42013-09-05 16:46:43 +0000167 HE = Buckets[i].end();
168 HI != HE; ++HI) {
Frederic Riss0e9a50f2015-03-10 00:46:31 +0000169 uint32_t HashValue = (*HI)->HashValue;
170 if (PrevHash == HashValue)
171 continue;
Lang Hames9ff69c82015-04-24 19:11:51 +0000172 Asm->OutStreamer->AddComment("Hash in Bucket " + Twine(i));
Frederic Riss0e9a50f2015-03-10 00:46:31 +0000173 Asm->EmitInt32(HashValue);
174 PrevHash = HashValue;
Eric Christopher48fef592012-12-20 21:58:40 +0000175 }
Eric Christopher6e472042011-11-07 09:18:42 +0000176 }
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.
Rafael Espindola063d7252015-03-10 16:58:10 +0000183void DwarfAccelTable::emitOffsets(AsmPrinter *Asm, const MCSymbol *SecBegin) {
Frederic Riss0e9a50f2015-03-10 00:46:31 +0000184 uint64_t PrevHash = UINT64_MAX;
Eric Christopher54ce295d2011-11-08 18:38:40 +0000185 for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
Eric Christopher6e472042011-11-07 09:18:42 +0000186 for (HashList::const_iterator HI = Buckets[i].begin(),
Eric Christopherb4e2cc42013-09-05 16:46:43 +0000187 HE = Buckets[i].end();
188 HI != HE; ++HI) {
Frederic Riss0e9a50f2015-03-10 00:46:31 +0000189 uint32_t HashValue = (*HI)->HashValue;
190 if (PrevHash == HashValue)
191 continue;
192 PrevHash = HashValue;
Lang Hames9ff69c82015-04-24 19:11:51 +0000193 Asm->OutStreamer->AddComment("Offset in Bucket " + Twine(i));
194 MCContext &Context = Asm->OutStreamer->getContext();
Jim Grosbach13760bd2015-05-30 01:25:56 +0000195 const MCExpr *Sub = MCBinaryExpr::createSub(
196 MCSymbolRefExpr::create((*HI)->Sym, Context),
197 MCSymbolRefExpr::create(SecBegin, Context), Context);
Lang Hames9ff69c82015-04-24 19:11:51 +0000198 Asm->OutStreamer->EmitValue(Sub, sizeof(uint32_t));
Eric Christopher6e472042011-11-07 09:18:42 +0000199 }
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.
Rafael Espindola063d7252015-03-10 16:58:10 +0000206void DwarfAccelTable::EmitData(AsmPrinter *Asm, DwarfDebug *D) {
Eric Christopher54ce295d2011-11-08 18:38:40 +0000207 for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
Frederic Riss44a219f2015-03-10 03:47:55 +0000208 uint64_t PrevHash = UINT64_MAX;
Eric Christopher6e472042011-11-07 09:18:42 +0000209 for (HashList::const_iterator HI = Buckets[i].begin(),
Eric Christopherb4e2cc42013-09-05 16:46:43 +0000210 HE = Buckets[i].end();
211 HI != HE; ++HI) {
Frederic Riss0e9a50f2015-03-10 00:46:31 +0000212 // 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);
Eric Christopher6e472042011-11-07 09:18:42 +0000216 // Remember to emit the label for our offset.
Lang Hames9ff69c82015-04-24 19:11:51 +0000217 Asm->OutStreamer->EmitLabel((*HI)->Sym);
218 Asm->OutStreamer->AddComment((*HI)->Str);
Duncan P. N. Exon Smith1e0d94e2015-05-24 16:48:54 +0000219 Asm->emitDwarfStringOffset((*HI)->Data.Name);
Lang Hames9ff69c82015-04-24 19:11:51 +0000220 Asm->OutStreamer->AddComment("Num DIEs");
David Blaikie772ab8a2014-04-25 22:21:35 +0000221 Asm->EmitInt32((*HI)->Data.Values.size());
222 for (HashDataContents *HD : (*HI)->Data.Values) {
Eric Christopher21bde872012-01-06 04:35:23 +0000223 // Emit the DIE offset
Frederic Riss3a6b3542014-11-12 23:48:14 +0000224 DwarfCompileUnit *CU = D->lookupUnit(HD->Die->getUnit());
225 assert(CU && "Accelerated DIE should belong to a CU.");
226 Asm->EmitInt32(HD->Die->getOffset() + CU->getDebugInfoOffset());
Eric Christopher21bde872012-01-06 04:35:23 +0000227 // If we have multiple Atoms emit that info too.
228 // FIXME: A bit of a hack, we either emit only one atom or all info.
229 if (HeaderData.Atoms.size() > 1) {
David Blaikie772ab8a2014-04-25 22:21:35 +0000230 Asm->EmitInt16(HD->Die->getTag());
231 Asm->EmitInt8(HD->Flags);
Eric Christopher21bde872012-01-06 04:35:23 +0000232 }
Eric Christopher6e472042011-11-07 09:18:42 +0000233 }
Eric Christopher6e472042011-11-07 09:18:42 +0000234 PrevHash = (*HI)->HashValue;
235 }
Frederic Riss0e9a50f2015-03-10 00:46:31 +0000236 // Emit the final end marker for the bucket.
Frederic Riss44a219f2015-03-10 03:47:55 +0000237 if (!Buckets[i].empty())
238 Asm->EmitInt32(0);
Eric Christopher6e472042011-11-07 09:18:42 +0000239 }
240}
241
242// Emit the entire data structure to the output file.
Rafael Espindola063d7252015-03-10 16:58:10 +0000243void DwarfAccelTable::emit(AsmPrinter *Asm, const MCSymbol *SecBegin,
244 DwarfDebug *D) {
Eric Christopher6e472042011-11-07 09:18:42 +0000245 // Emit the header.
246 EmitHeader(Asm);
247
248 // Emit the buckets.
249 EmitBuckets(Asm);
250
251 // Emit the hashes.
252 EmitHashes(Asm);
253
254 // Emit the offsets.
Rafael Espindola063d7252015-03-10 16:58:10 +0000255 emitOffsets(Asm, SecBegin);
Eric Christopher6e472042011-11-07 09:18:42 +0000256
257 // Emit the hash data.
Rafael Espindola063d7252015-03-10 16:58:10 +0000258 EmitData(Asm, D);
Eric Christopher6e472042011-11-07 09:18:42 +0000259}
260
261#ifndef NDEBUG
262void DwarfAccelTable::print(raw_ostream &O) {
263
264 Header.print(O);
265 HeaderData.print(O);
266
267 O << "Entries: \n";
Eric Christopherb4e2cc42013-09-05 16:46:43 +0000268 for (StringMap<DataArray>::const_iterator EI = Entries.begin(),
269 EE = Entries.end();
270 EI != EE; ++EI) {
Eric Christopher5a28a6e2012-01-06 23:03:27 +0000271 O << "Name: " << EI->getKeyData() << "\n";
David Blaikie772ab8a2014-04-25 22:21:35 +0000272 for (HashDataContents *HD : EI->second.Values)
273 HD->print(O);
Eric Christopher6e472042011-11-07 09:18:42 +0000274 }
275
276 O << "Buckets and Hashes: \n";
Eric Christopher54ce295d2011-11-08 18:38:40 +0000277 for (size_t i = 0, e = Buckets.size(); i < e; ++i)
Eric Christopher6e472042011-11-07 09:18:42 +0000278 for (HashList::const_iterator HI = Buckets[i].begin(),
Eric Christopherb4e2cc42013-09-05 16:46:43 +0000279 HE = Buckets[i].end();
280 HI != HE; ++HI)
Eric Christopher6e472042011-11-07 09:18:42 +0000281 (*HI)->print(O);
282
283 O << "Data: \n";
Eric Christopherb4e2cc42013-09-05 16:46:43 +0000284 for (std::vector<HashData *>::const_iterator DI = Data.begin(),
285 DE = Data.end();
286 DI != DE; ++DI)
287 (*DI)->print(O);
Eric Christopher6e472042011-11-07 09:18:42 +0000288}
289#endif