blob: 1de644a4f88e2f2bb99d9fe07e6ea373dcc19ff7 [file] [log] [blame]
Eric Christopherbcbd3a42011-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 Topperf1d0f772012-03-26 06:58:25 +000014#include "DwarfAccelTable.h"
Craig Topperf1d0f772012-03-26 06:58:25 +000015#include "DIE.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000016#include "DwarfDebug.h"
Benjamin Kramerbe3f0512012-03-26 14:17:26 +000017#include "llvm/ADT/STLExtras.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000018#include "llvm/ADT/Twine.h"
Eric Christopherbcbd3a42011-11-07 09:18:42 +000019#include "llvm/CodeGen/AsmPrinter.h"
20#include "llvm/MC/MCExpr.h"
21#include "llvm/MC/MCStreamer.h"
22#include "llvm/MC/MCSymbol.h"
23#include "llvm/Support/Debug.h"
Eric Christopherbcbd3a42011-11-07 09:18:42 +000024
25using namespace llvm;
26
27const char *DwarfAccelTable::Atom::AtomTypeString(enum AtomType AT) {
28 switch (AT) {
Eric Christopher5b9544b2013-09-05 16:46:43 +000029 case eAtomTypeNULL:
30 return "eAtomTypeNULL";
31 case eAtomTypeDIEOffset:
32 return "eAtomTypeDIEOffset";
33 case eAtomTypeCUOffset:
34 return "eAtomTypeCUOffset";
35 case eAtomTypeTag:
36 return "eAtomTypeTag";
37 case eAtomTypeNameFlags:
38 return "eAtomTypeNameFlags";
39 case eAtomTypeTypeFlags:
40 return "eAtomTypeTypeFlags";
Eric Christopher72c16552012-12-20 21:58:40 +000041 }
David Blaikie2dd674f2012-01-16 23:24:27 +000042 llvm_unreachable("invalid AtomType!");
Eric Christopherbcbd3a42011-11-07 09:18:42 +000043}
44
Eric Christopherc36145f2012-01-06 04:35:23 +000045// The length of the header data is always going to be 4 + 4 + 4*NumAtoms.
Eric Christopher5b9544b2013-09-05 16:46:43 +000046DwarfAccelTable::DwarfAccelTable(ArrayRef<DwarfAccelTable::Atom> atomList)
47 : Header(8 + (atomList.size() * 4)), HeaderData(atomList),
48 Entries(Allocator) {}
Eric Christopherc36145f2012-01-06 04:35:23 +000049
Eric Christopher5b9544b2013-09-05 16:46:43 +000050DwarfAccelTable::~DwarfAccelTable() {}
Eric Christophere77546c2011-11-07 21:49:28 +000051
Eric Christopher5b9544b2013-09-05 16:46:43 +000052void DwarfAccelTable::AddName(StringRef Name, DIE *die, char Flags) {
Benjamin Kramer36c38b82012-04-13 20:06:17 +000053 assert(Data.empty() && "Already finalized!");
Eric Christopherbcbd3a42011-11-07 09:18:42 +000054 // If the string is in the list already then add this die to the list
55 // otherwise add a new one.
Eric Christopherc36145f2012-01-06 04:35:23 +000056 DataArray &DIEs = Entries[Name];
Benjamin Kramer36c38b82012-04-13 20:06:17 +000057 DIEs.push_back(new (Allocator) HashDataContents(die, Flags));
Eric Christopherbcbd3a42011-11-07 09:18:42 +000058}
59
60void DwarfAccelTable::ComputeBucketCount(void) {
61 // First get the number of unique hashes.
Benjamin Kramerbe3f0512012-03-26 14:17:26 +000062 std::vector<uint32_t> uniques(Data.size());
Eric Christopher30b4d8b2011-11-08 18:38:40 +000063 for (size_t i = 0, e = Data.size(); i < e; ++i)
Eric Christopherbcbd3a42011-11-07 09:18:42 +000064 uniques[i] = Data[i]->HashValue;
Benjamin Kramerbe3f0512012-03-26 14:17:26 +000065 array_pod_sort(uniques.begin(), uniques.end());
Eric Christopherbcbd3a42011-11-07 09:18:42 +000066 std::vector<uint32_t>::iterator p =
Eric Christopher5b9544b2013-09-05 16:46:43 +000067 std::unique(uniques.begin(), uniques.end());
Eric Christopherbcbd3a42011-11-07 09:18:42 +000068 uint32_t num = std::distance(uniques.begin(), p);
69
70 // Then compute the bucket size, minimum of 1 bucket.
Eric Christopher5b9544b2013-09-05 16:46:43 +000071 if (num > 1024)
72 Header.bucket_count = num / 4;
73 if (num > 16)
74 Header.bucket_count = num / 2;
75 else
76 Header.bucket_count = num > 0 ? num : 1;
Eric Christopherbcbd3a42011-11-07 09:18:42 +000077
78 Header.hashes_count = num;
79}
80
Benjamin Kramer36c38b82012-04-13 20:06:17 +000081// compareDIEs - comparison predicate that sorts DIEs by their offset.
82static bool compareDIEs(const DwarfAccelTable::HashDataContents *A,
83 const DwarfAccelTable::HashDataContents *B) {
84 return A->Die->getOffset() < B->Die->getOffset();
Eric Christopher8368f742011-11-15 23:37:17 +000085}
86
Benjamin Kramer03406c42013-05-11 18:24:28 +000087void DwarfAccelTable::FinalizeTable(AsmPrinter *Asm, StringRef Prefix) {
Eric Christopherbcbd3a42011-11-07 09:18:42 +000088 // Create the individual hash data outputs.
Eric Christopher5b9544b2013-09-05 16:46:43 +000089 for (StringMap<DataArray>::iterator EI = Entries.begin(), EE = Entries.end();
90 EI != EE; ++EI) {
Eric Christopher0ffe2b42011-11-10 19:25:34 +000091
92 // Unique the entries.
Benjamin Kramer36c38b82012-04-13 20:06:17 +000093 std::stable_sort(EI->second.begin(), EI->second.end(), compareDIEs);
Eric Christopher547abbb2012-01-06 23:03:27 +000094 EI->second.erase(std::unique(EI->second.begin(), EI->second.end()),
Eric Christopher5b9544b2013-09-05 16:46:43 +000095 EI->second.end());
Eric Christopher0ffe2b42011-11-10 19:25:34 +000096
Benjamin Kramer36c38b82012-04-13 20:06:17 +000097 HashData *Entry = new (Allocator) HashData(EI->getKey(), EI->second);
Eric Christopherbcbd3a42011-11-07 09:18:42 +000098 Data.push_back(Entry);
99 }
100
101 // Figure out how many buckets we need, then compute the bucket
102 // contents and the final ordering. We'll emit the hashes and offsets
103 // by doing a walk during the emission phase. We add temporary
104 // symbols to the data so that we can reference them during the offset
105 // later, we'll emit them when we emit the data.
106 ComputeBucketCount();
107
108 // Compute bucket contents and final ordering.
109 Buckets.resize(Header.bucket_count);
Eric Christopher30b4d8b2011-11-08 18:38:40 +0000110 for (size_t i = 0, e = Data.size(); i < e; ++i) {
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000111 uint32_t bucket = Data[i]->HashValue % Header.bucket_count;
112 Buckets[bucket].push_back(Data[i]);
113 Data[i]->Sym = Asm->GetTempSymbol(Prefix, i);
114 }
115}
116
117// Emits the header for the table via the AsmPrinter.
118void DwarfAccelTable::EmitHeader(AsmPrinter *Asm) {
119 Asm->OutStreamer.AddComment("Header Magic");
120 Asm->EmitInt32(Header.magic);
121 Asm->OutStreamer.AddComment("Header Version");
122 Asm->EmitInt16(Header.version);
123 Asm->OutStreamer.AddComment("Header Hash Function");
124 Asm->EmitInt16(Header.hash_function);
125 Asm->OutStreamer.AddComment("Header Bucket Count");
126 Asm->EmitInt32(Header.bucket_count);
127 Asm->OutStreamer.AddComment("Header Hash Count");
128 Asm->EmitInt32(Header.hashes_count);
129 Asm->OutStreamer.AddComment("Header Data Length");
130 Asm->EmitInt32(Header.header_data_len);
131 Asm->OutStreamer.AddComment("HeaderData Die Offset Base");
132 Asm->EmitInt32(HeaderData.die_offset_base);
133 Asm->OutStreamer.AddComment("HeaderData Atom Count");
134 Asm->EmitInt32(HeaderData.Atoms.size());
135 for (size_t i = 0; i < HeaderData.Atoms.size(); i++) {
136 Atom A = HeaderData.Atoms[i];
137 Asm->OutStreamer.AddComment(Atom::AtomTypeString(A.type));
138 Asm->EmitInt16(A.type);
139 Asm->OutStreamer.AddComment(dwarf::FormEncodingString(A.form));
140 Asm->EmitInt16(A.form);
141 }
142}
143
Eric Christopher2d65efe2012-10-08 23:53:45 +0000144// Walk through and emit the buckets for the table. Each index is
145// an offset into the list of hashes.
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000146void DwarfAccelTable::EmitBuckets(AsmPrinter *Asm) {
147 unsigned index = 0;
Eric Christopher30b4d8b2011-11-08 18:38:40 +0000148 for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
Eric Christopherc5453222011-11-07 18:34:47 +0000149 Asm->OutStreamer.AddComment("Bucket " + Twine(i));
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000150 if (Buckets[i].size() != 0)
151 Asm->EmitInt32(index);
152 else
153 Asm->EmitInt32(UINT32_MAX);
154 index += Buckets[i].size();
155 }
156}
157
158// Walk through the buckets and emit the individual hashes for each
159// bucket.
160void DwarfAccelTable::EmitHashes(AsmPrinter *Asm) {
Eric Christopher30b4d8b2011-11-08 18:38:40 +0000161 for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000162 for (HashList::const_iterator HI = Buckets[i].begin(),
Eric Christopher5b9544b2013-09-05 16:46:43 +0000163 HE = Buckets[i].end();
164 HI != HE; ++HI) {
Eric Christopherc5453222011-11-07 18:34:47 +0000165 Asm->OutStreamer.AddComment("Hash in Bucket " + Twine(i));
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000166 Asm->EmitInt32((*HI)->HashValue);
Eric Christopher72c16552012-12-20 21:58:40 +0000167 }
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000168 }
169}
170
171// Walk through the buckets and emit the individual offsets for each
172// element in each bucket. This is done via a symbol subtraction from the
173// beginning of the section. The non-section symbol will be output later
174// when we emit the actual data.
175void DwarfAccelTable::EmitOffsets(AsmPrinter *Asm, MCSymbol *SecBegin) {
Eric Christopher30b4d8b2011-11-08 18:38:40 +0000176 for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000177 for (HashList::const_iterator HI = Buckets[i].begin(),
Eric Christopher5b9544b2013-09-05 16:46:43 +0000178 HE = Buckets[i].end();
179 HI != HE; ++HI) {
Eric Christopherc5453222011-11-07 18:34:47 +0000180 Asm->OutStreamer.AddComment("Offset in Bucket " + Twine(i));
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000181 MCContext &Context = Asm->OutStreamer.getContext();
Eric Christopher5b9544b2013-09-05 16:46:43 +0000182 const MCExpr *Sub = MCBinaryExpr::CreateSub(
183 MCSymbolRefExpr::Create((*HI)->Sym, Context),
184 MCSymbolRefExpr::Create(SecBegin, Context), Context);
Eric Christopher1ced2082013-01-09 03:52:05 +0000185 Asm->OutStreamer.EmitValue(Sub, sizeof(uint32_t));
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000186 }
187 }
188}
189
190// Walk through the buckets and emit the full data for each element in
191// the bucket. For the string case emit the dies and the various offsets.
192// Terminate each HashData bucket with 0.
Eric Christopher2e5d8702012-12-20 21:58:36 +0000193void DwarfAccelTable::EmitData(AsmPrinter *Asm, DwarfUnits *D) {
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000194 uint64_t PrevHash = UINT64_MAX;
Eric Christopher30b4d8b2011-11-08 18:38:40 +0000195 for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000196 for (HashList::const_iterator HI = Buckets[i].begin(),
Eric Christopher5b9544b2013-09-05 16:46:43 +0000197 HE = Buckets[i].end();
198 HI != HE; ++HI) {
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000199 // Remember to emit the label for our offset.
200 Asm->OutStreamer.EmitLabel((*HI)->Sym);
201 Asm->OutStreamer.AddComment((*HI)->Str);
202 Asm->EmitSectionOffset(D->getStringPoolEntry((*HI)->Str),
Eric Christopher2e5d8702012-12-20 21:58:36 +0000203 D->getStringPoolSym());
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000204 Asm->OutStreamer.AddComment("Num DIEs");
Eric Christopherc36145f2012-01-06 04:35:23 +0000205 Asm->EmitInt32((*HI)->Data.size());
Eric Christopher5b9544b2013-09-05 16:46:43 +0000206 for (ArrayRef<HashDataContents *>::const_iterator
207 DI = (*HI)->Data.begin(),
208 DE = (*HI)->Data.end();
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000209 DI != DE; ++DI) {
Eric Christopherc36145f2012-01-06 04:35:23 +0000210 // Emit the DIE offset
211 Asm->EmitInt32((*DI)->Die->getOffset());
212 // If we have multiple Atoms emit that info too.
213 // FIXME: A bit of a hack, we either emit only one atom or all info.
214 if (HeaderData.Atoms.size() > 1) {
215 Asm->EmitInt16((*DI)->Die->getTag());
216 Asm->EmitInt8((*DI)->Flags);
217 }
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000218 }
219 // Emit a 0 to terminate the data unless we have a hash collision.
220 if (PrevHash != (*HI)->HashValue)
221 Asm->EmitInt32(0);
222 PrevHash = (*HI)->HashValue;
223 }
224 }
225}
226
227// Emit the entire data structure to the output file.
Eric Christopher5b9544b2013-09-05 16:46:43 +0000228void DwarfAccelTable::Emit(AsmPrinter *Asm, MCSymbol *SecBegin, DwarfUnits *D) {
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000229 // Emit the header.
230 EmitHeader(Asm);
231
232 // Emit the buckets.
233 EmitBuckets(Asm);
234
235 // Emit the hashes.
236 EmitHashes(Asm);
237
238 // Emit the offsets.
239 EmitOffsets(Asm, SecBegin);
240
241 // Emit the hash data.
242 EmitData(Asm, D);
243}
244
245#ifndef NDEBUG
246void DwarfAccelTable::print(raw_ostream &O) {
247
248 Header.print(O);
249 HeaderData.print(O);
250
251 O << "Entries: \n";
Eric Christopher5b9544b2013-09-05 16:46:43 +0000252 for (StringMap<DataArray>::const_iterator EI = Entries.begin(),
253 EE = Entries.end();
254 EI != EE; ++EI) {
Eric Christopher547abbb2012-01-06 23:03:27 +0000255 O << "Name: " << EI->getKeyData() << "\n";
256 for (DataArray::const_iterator DI = EI->second.begin(),
Eric Christopher5b9544b2013-09-05 16:46:43 +0000257 DE = EI->second.end();
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000258 DI != DE; ++DI)
259 (*DI)->print(O);
260 }
261
262 O << "Buckets and Hashes: \n";
Eric Christopher30b4d8b2011-11-08 18:38:40 +0000263 for (size_t i = 0, e = Buckets.size(); i < e; ++i)
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000264 for (HashList::const_iterator HI = Buckets[i].begin(),
Eric Christopher5b9544b2013-09-05 16:46:43 +0000265 HE = Buckets[i].end();
266 HI != HE; ++HI)
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000267 (*HI)->print(O);
268
269 O << "Data: \n";
Eric Christopher5b9544b2013-09-05 16:46:43 +0000270 for (std::vector<HashData *>::const_iterator DI = Data.begin(),
271 DE = Data.end();
272 DI != DE; ++DI)
273 (*DI)->print(O);
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000274}
275#endif