blob: c21b3d3451adba055b3be3b21bca28dacb095256 [file] [log] [blame]
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +00001//===- llvm/CodeGen/DwarfAccelTable.cpp - Dwarf Accelerator Tables --------===//
Eric Christopher6e472042011-11-07 09:18:42 +00002//
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"
Benjamin Kramer3e6719c2012-03-26 14:17:26 +000015#include "llvm/ADT/STLExtras.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000016#include "llvm/ADT/StringMap.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/ADT/Twine.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000018#include "llvm/BinaryFormat/Dwarf.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"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000023#include "llvm/Support/raw_ostream.h"
24#include <algorithm>
25#include <cassert>
26#include <cstddef>
27#include <cstdint>
28#include <iterator>
29#include <limits>
30#include <vector>
Eric Christopher6e472042011-11-07 09:18:42 +000031
32using namespace llvm;
33
Eric Christopher21bde872012-01-06 04:35:23 +000034// The length of the header data is always going to be 4 + 4 + 4*NumAtoms.
Eric Christopherb4e2cc42013-09-05 16:46:43 +000035DwarfAccelTable::DwarfAccelTable(ArrayRef<DwarfAccelTable::Atom> atomList)
36 : Header(8 + (atomList.size() * 4)), HeaderData(atomList),
37 Entries(Allocator) {}
Eric Christopher21bde872012-01-06 04:35:23 +000038
Duncan P. N. Exon Smithf4599942015-05-24 16:44:32 +000039void DwarfAccelTable::AddName(DwarfStringPoolEntryRef Name, const DIE *die,
David Blaikie772ab8a2014-04-25 22:21:35 +000040 char Flags) {
Benjamin Kramer330970d2012-04-13 20:06:17 +000041 assert(Data.empty() && "Already finalized!");
Eric Christopher6e472042011-11-07 09:18:42 +000042 // If the string is in the list already then add this die to the list
43 // otherwise add a new one.
Duncan P. N. Exon Smithf4599942015-05-24 16:44:32 +000044 DataArray &DIEs = Entries[Name.getString()];
45 assert(!DIEs.Name || DIEs.Name == Name);
46 DIEs.Name = Name;
David Blaikie772ab8a2014-04-25 22:21:35 +000047 DIEs.Values.push_back(new (Allocator) HashDataContents(die, Flags));
Eric Christopher6e472042011-11-07 09:18:42 +000048}
49
Eugene Zelenkoffec81c2015-11-04 22:32:32 +000050void DwarfAccelTable::ComputeBucketCount() {
Eric Christopher6e472042011-11-07 09:18:42 +000051 // First get the number of unique hashes.
Benjamin Kramer3e6719c2012-03-26 14:17:26 +000052 std::vector<uint32_t> uniques(Data.size());
Eric Christopher54ce295d2011-11-08 18:38:40 +000053 for (size_t i = 0, e = Data.size(); i < e; ++i)
Eric Christopher6e472042011-11-07 09:18:42 +000054 uniques[i] = Data[i]->HashValue;
Benjamin Kramer3e6719c2012-03-26 14:17:26 +000055 array_pod_sort(uniques.begin(), uniques.end());
Eric Christopher6e472042011-11-07 09:18:42 +000056 std::vector<uint32_t>::iterator p =
Eric Christopherb4e2cc42013-09-05 16:46:43 +000057 std::unique(uniques.begin(), uniques.end());
Eric Christopher6e472042011-11-07 09:18:42 +000058 uint32_t num = std::distance(uniques.begin(), p);
59
60 // Then compute the bucket size, minimum of 1 bucket.
Eric Christopherb4e2cc42013-09-05 16:46:43 +000061 if (num > 1024)
62 Header.bucket_count = num / 4;
Frederic Riss2ad51882015-03-09 21:09:50 +000063 else if (num > 16)
Eric Christopherb4e2cc42013-09-05 16:46:43 +000064 Header.bucket_count = num / 2;
65 else
66 Header.bucket_count = num > 0 ? num : 1;
Eric Christopher6e472042011-11-07 09:18:42 +000067
68 Header.hashes_count = num;
69}
70
Benjamin Kramer330970d2012-04-13 20:06:17 +000071// compareDIEs - comparison predicate that sorts DIEs by their offset.
72static bool compareDIEs(const DwarfAccelTable::HashDataContents *A,
73 const DwarfAccelTable::HashDataContents *B) {
74 return A->Die->getOffset() < B->Die->getOffset();
Eric Christopher0abbd0e2011-11-15 23:37:17 +000075}
76
Benjamin Kramer63e39eb2013-05-11 18:24:28 +000077void DwarfAccelTable::FinalizeTable(AsmPrinter *Asm, StringRef Prefix) {
Eric Christopher6e472042011-11-07 09:18:42 +000078 // Create the individual hash data outputs.
Benjamin Kramer49a11322015-02-28 20:15:00 +000079 Data.reserve(Entries.size());
Eric Christopherb4e2cc42013-09-05 16:46:43 +000080 for (StringMap<DataArray>::iterator EI = Entries.begin(), EE = Entries.end();
81 EI != EE; ++EI) {
Eric Christopherd9843b32011-11-10 19:25:34 +000082
83 // Unique the entries.
David Blaikie772ab8a2014-04-25 22:21:35 +000084 std::stable_sort(EI->second.Values.begin(), EI->second.Values.end(), compareDIEs);
85 EI->second.Values.erase(
86 std::unique(EI->second.Values.begin(), EI->second.Values.end()),
87 EI->second.Values.end());
Eric Christopherd9843b32011-11-10 19:25:34 +000088
Benjamin Kramer330970d2012-04-13 20:06:17 +000089 HashData *Entry = new (Allocator) HashData(EI->getKey(), EI->second);
Eric Christopher6e472042011-11-07 09:18:42 +000090 Data.push_back(Entry);
91 }
92
93 // Figure out how many buckets we need, then compute the bucket
94 // contents and the final ordering. We'll emit the hashes and offsets
95 // by doing a walk during the emission phase. We add temporary
96 // symbols to the data so that we can reference them during the offset
97 // later, we'll emit them when we emit the data.
98 ComputeBucketCount();
99
100 // Compute bucket contents and final ordering.
101 Buckets.resize(Header.bucket_count);
Eric Christopher54ce295d2011-11-08 18:38:40 +0000102 for (size_t i = 0, e = Data.size(); i < e; ++i) {
Eric Christopher6e472042011-11-07 09:18:42 +0000103 uint32_t bucket = Data[i]->HashValue % Header.bucket_count;
104 Buckets[bucket].push_back(Data[i]);
Rafael Espindola9ab09232015-03-17 20:07:06 +0000105 Data[i]->Sym = Asm->createTempSymbol(Prefix);
Eric Christopher6e472042011-11-07 09:18:42 +0000106 }
Frederic Riss0e9a50f2015-03-10 00:46:31 +0000107
108 // Sort the contents of the buckets by hash value so that hash
109 // collisions end up together. Stable sort makes testing easier and
110 // doesn't cost much more.
111 for (size_t i = 0; i < Buckets.size(); ++i)
112 std::stable_sort(Buckets[i].begin(), Buckets[i].end(),
113 [] (HashData *LHS, HashData *RHS) {
114 return LHS->HashValue < RHS->HashValue;
115 });
Eric Christopher6e472042011-11-07 09:18:42 +0000116}
117
118// Emits the header for the table via the AsmPrinter.
119void DwarfAccelTable::EmitHeader(AsmPrinter *Asm) {
Lang Hames9ff69c82015-04-24 19:11:51 +0000120 Asm->OutStreamer->AddComment("Header Magic");
Eric Christopher6e472042011-11-07 09:18:42 +0000121 Asm->EmitInt32(Header.magic);
Lang Hames9ff69c82015-04-24 19:11:51 +0000122 Asm->OutStreamer->AddComment("Header Version");
Eric Christopher6e472042011-11-07 09:18:42 +0000123 Asm->EmitInt16(Header.version);
Lang Hames9ff69c82015-04-24 19:11:51 +0000124 Asm->OutStreamer->AddComment("Header Hash Function");
Eric Christopher6e472042011-11-07 09:18:42 +0000125 Asm->EmitInt16(Header.hash_function);
Lang Hames9ff69c82015-04-24 19:11:51 +0000126 Asm->OutStreamer->AddComment("Header Bucket Count");
Eric Christopher6e472042011-11-07 09:18:42 +0000127 Asm->EmitInt32(Header.bucket_count);
Lang Hames9ff69c82015-04-24 19:11:51 +0000128 Asm->OutStreamer->AddComment("Header Hash Count");
Eric Christopher6e472042011-11-07 09:18:42 +0000129 Asm->EmitInt32(Header.hashes_count);
Lang Hames9ff69c82015-04-24 19:11:51 +0000130 Asm->OutStreamer->AddComment("Header Data Length");
Eric Christopher6e472042011-11-07 09:18:42 +0000131 Asm->EmitInt32(Header.header_data_len);
Lang Hames9ff69c82015-04-24 19:11:51 +0000132 Asm->OutStreamer->AddComment("HeaderData Die Offset Base");
Eric Christopher6e472042011-11-07 09:18:42 +0000133 Asm->EmitInt32(HeaderData.die_offset_base);
Lang Hames9ff69c82015-04-24 19:11:51 +0000134 Asm->OutStreamer->AddComment("HeaderData Atom Count");
Eric Christopher6e472042011-11-07 09:18:42 +0000135 Asm->EmitInt32(HeaderData.Atoms.size());
136 for (size_t i = 0; i < HeaderData.Atoms.size(); i++) {
137 Atom A = HeaderData.Atoms[i];
Lang Hames9ff69c82015-04-24 19:11:51 +0000138 Asm->OutStreamer->AddComment(dwarf::AtomTypeString(A.type));
Eric Christopher6e472042011-11-07 09:18:42 +0000139 Asm->EmitInt16(A.type);
Lang Hames9ff69c82015-04-24 19:11:51 +0000140 Asm->OutStreamer->AddComment(dwarf::FormEncodingString(A.form));
Eric Christopher6e472042011-11-07 09:18:42 +0000141 Asm->EmitInt16(A.form);
142 }
143}
144
Eric Christopher28611362012-10-08 23:53:45 +0000145// Walk through and emit the buckets for the table. Each index is
146// an offset into the list of hashes.
Eric Christopher6e472042011-11-07 09:18:42 +0000147void DwarfAccelTable::EmitBuckets(AsmPrinter *Asm) {
148 unsigned index = 0;
Eric Christopher54ce295d2011-11-08 18:38:40 +0000149 for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
Lang Hames9ff69c82015-04-24 19:11:51 +0000150 Asm->OutStreamer->AddComment("Bucket " + Twine(i));
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000151 if (!Buckets[i].empty())
Eric Christopher6e472042011-11-07 09:18:42 +0000152 Asm->EmitInt32(index);
153 else
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000154 Asm->EmitInt32(std::numeric_limits<uint32_t>::max());
Frederic Riss0e9a50f2015-03-10 00:46:31 +0000155 // Buckets point in the list of hashes, not to the data. Do not
156 // increment the index multiple times in case of hash collisions.
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000157 uint64_t PrevHash = std::numeric_limits<uint64_t>::max();
Frederic Riss0e9a50f2015-03-10 00:46:31 +0000158 for (auto *HD : Buckets[i]) {
159 uint32_t HashValue = HD->HashValue;
160 if (PrevHash != HashValue)
161 ++index;
162 PrevHash = HashValue;
163 }
Eric Christopher6e472042011-11-07 09:18:42 +0000164 }
165}
166
167// Walk through the buckets and emit the individual hashes for each
168// bucket.
169void DwarfAccelTable::EmitHashes(AsmPrinter *Asm) {
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000170 uint64_t PrevHash = std::numeric_limits<uint64_t>::max();
Eric Christopher54ce295d2011-11-08 18:38:40 +0000171 for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
Eric Christopher6e472042011-11-07 09:18:42 +0000172 for (HashList::const_iterator HI = Buckets[i].begin(),
Eric Christopherb4e2cc42013-09-05 16:46:43 +0000173 HE = Buckets[i].end();
174 HI != HE; ++HI) {
Frederic Riss0e9a50f2015-03-10 00:46:31 +0000175 uint32_t HashValue = (*HI)->HashValue;
176 if (PrevHash == HashValue)
177 continue;
Lang Hames9ff69c82015-04-24 19:11:51 +0000178 Asm->OutStreamer->AddComment("Hash in Bucket " + Twine(i));
Frederic Riss0e9a50f2015-03-10 00:46:31 +0000179 Asm->EmitInt32(HashValue);
180 PrevHash = HashValue;
Eric Christopher48fef592012-12-20 21:58:40 +0000181 }
Eric Christopher6e472042011-11-07 09:18:42 +0000182 }
183}
184
185// Walk through the buckets and emit the individual offsets for each
186// element in each bucket. This is done via a symbol subtraction from the
187// beginning of the section. The non-section symbol will be output later
188// when we emit the actual data.
Rafael Espindola063d7252015-03-10 16:58:10 +0000189void DwarfAccelTable::emitOffsets(AsmPrinter *Asm, const MCSymbol *SecBegin) {
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000190 uint64_t PrevHash = std::numeric_limits<uint64_t>::max();
Eric Christopher54ce295d2011-11-08 18:38:40 +0000191 for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
Eric Christopher6e472042011-11-07 09:18:42 +0000192 for (HashList::const_iterator HI = Buckets[i].begin(),
Eric Christopherb4e2cc42013-09-05 16:46:43 +0000193 HE = Buckets[i].end();
194 HI != HE; ++HI) {
Frederic Riss0e9a50f2015-03-10 00:46:31 +0000195 uint32_t HashValue = (*HI)->HashValue;
196 if (PrevHash == HashValue)
197 continue;
198 PrevHash = HashValue;
Lang Hames9ff69c82015-04-24 19:11:51 +0000199 Asm->OutStreamer->AddComment("Offset in Bucket " + Twine(i));
200 MCContext &Context = Asm->OutStreamer->getContext();
Jim Grosbach13760bd2015-05-30 01:25:56 +0000201 const MCExpr *Sub = MCBinaryExpr::createSub(
202 MCSymbolRefExpr::create((*HI)->Sym, Context),
203 MCSymbolRefExpr::create(SecBegin, Context), Context);
Lang Hames9ff69c82015-04-24 19:11:51 +0000204 Asm->OutStreamer->EmitValue(Sub, sizeof(uint32_t));
Eric Christopher6e472042011-11-07 09:18:42 +0000205 }
206 }
207}
208
209// Walk through the buckets and emit the full data for each element in
210// the bucket. For the string case emit the dies and the various offsets.
211// Terminate each HashData bucket with 0.
Rafael Espindola063d7252015-03-10 16:58:10 +0000212void DwarfAccelTable::EmitData(AsmPrinter *Asm, DwarfDebug *D) {
Eric Christopher54ce295d2011-11-08 18:38:40 +0000213 for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000214 uint64_t PrevHash = std::numeric_limits<uint64_t>::max();
Eric Christopher6e472042011-11-07 09:18:42 +0000215 for (HashList::const_iterator HI = Buckets[i].begin(),
Eric Christopherb4e2cc42013-09-05 16:46:43 +0000216 HE = Buckets[i].end();
217 HI != HE; ++HI) {
Frederic Riss0e9a50f2015-03-10 00:46:31 +0000218 // Terminate the previous entry if there is no hash collision
219 // with the current one.
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000220 if (PrevHash != std::numeric_limits<uint64_t>::max() &&
221 PrevHash != (*HI)->HashValue)
Frederic Riss0e9a50f2015-03-10 00:46:31 +0000222 Asm->EmitInt32(0);
Eric Christopher6e472042011-11-07 09:18:42 +0000223 // Remember to emit the label for our offset.
Lang Hames9ff69c82015-04-24 19:11:51 +0000224 Asm->OutStreamer->EmitLabel((*HI)->Sym);
225 Asm->OutStreamer->AddComment((*HI)->Str);
Duncan P. N. Exon Smith1e0d94e2015-05-24 16:48:54 +0000226 Asm->emitDwarfStringOffset((*HI)->Data.Name);
Lang Hames9ff69c82015-04-24 19:11:51 +0000227 Asm->OutStreamer->AddComment("Num DIEs");
David Blaikie772ab8a2014-04-25 22:21:35 +0000228 Asm->EmitInt32((*HI)->Data.Values.size());
229 for (HashDataContents *HD : (*HI)->Data.Values) {
Eric Christopher21bde872012-01-06 04:35:23 +0000230 // Emit the DIE offset
Greg Clayton35630c32016-12-01 18:56:29 +0000231 Asm->EmitInt32(HD->Die->getDebugSectionOffset());
Eric Christopher21bde872012-01-06 04:35:23 +0000232 // If we have multiple Atoms emit that info too.
233 // FIXME: A bit of a hack, we either emit only one atom or all info.
234 if (HeaderData.Atoms.size() > 1) {
David Blaikie772ab8a2014-04-25 22:21:35 +0000235 Asm->EmitInt16(HD->Die->getTag());
236 Asm->EmitInt8(HD->Flags);
Eric Christopher21bde872012-01-06 04:35:23 +0000237 }
Eric Christopher6e472042011-11-07 09:18:42 +0000238 }
Eric Christopher6e472042011-11-07 09:18:42 +0000239 PrevHash = (*HI)->HashValue;
240 }
Frederic Riss0e9a50f2015-03-10 00:46:31 +0000241 // Emit the final end marker for the bucket.
Frederic Riss44a219f2015-03-10 03:47:55 +0000242 if (!Buckets[i].empty())
243 Asm->EmitInt32(0);
Eric Christopher6e472042011-11-07 09:18:42 +0000244 }
245}
246
247// Emit the entire data structure to the output file.
Rafael Espindola063d7252015-03-10 16:58:10 +0000248void DwarfAccelTable::emit(AsmPrinter *Asm, const MCSymbol *SecBegin,
249 DwarfDebug *D) {
Eric Christopher6e472042011-11-07 09:18:42 +0000250 // Emit the header.
251 EmitHeader(Asm);
252
253 // Emit the buckets.
254 EmitBuckets(Asm);
255
256 // Emit the hashes.
257 EmitHashes(Asm);
258
259 // Emit the offsets.
Rafael Espindola063d7252015-03-10 16:58:10 +0000260 emitOffsets(Asm, SecBegin);
Eric Christopher6e472042011-11-07 09:18:42 +0000261
262 // Emit the hash data.
Rafael Espindola063d7252015-03-10 16:58:10 +0000263 EmitData(Asm, D);
Eric Christopher6e472042011-11-07 09:18:42 +0000264}
265
266#ifndef NDEBUG
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000267void DwarfAccelTable::print(raw_ostream &OS) {
268 Header.print(OS);
269 HeaderData.print(OS);
Eric Christopher6e472042011-11-07 09:18:42 +0000270
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000271 OS << "Entries: \n";
Eric Christopherb4e2cc42013-09-05 16:46:43 +0000272 for (StringMap<DataArray>::const_iterator EI = Entries.begin(),
273 EE = Entries.end();
274 EI != EE; ++EI) {
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000275 OS << "Name: " << EI->getKeyData() << "\n";
David Blaikie772ab8a2014-04-25 22:21:35 +0000276 for (HashDataContents *HD : EI->second.Values)
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000277 HD->print(OS);
Eric Christopher6e472042011-11-07 09:18:42 +0000278 }
279
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000280 OS << "Buckets and Hashes: \n";
Eric Christopher54ce295d2011-11-08 18:38:40 +0000281 for (size_t i = 0, e = Buckets.size(); i < e; ++i)
Eric Christopher6e472042011-11-07 09:18:42 +0000282 for (HashList::const_iterator HI = Buckets[i].begin(),
Eric Christopherb4e2cc42013-09-05 16:46:43 +0000283 HE = Buckets[i].end();
284 HI != HE; ++HI)
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000285 (*HI)->print(OS);
Eric Christopher6e472042011-11-07 09:18:42 +0000286
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000287 OS << "Data: \n";
Eric Christopherb4e2cc42013-09-05 16:46:43 +0000288 for (std::vector<HashData *>::const_iterator DI = Data.begin(),
289 DE = Data.end();
290 DI != DE; ++DI)
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000291 (*DI)->print(OS);
Eric Christopher6e472042011-11-07 09:18:42 +0000292}
293#endif