blob: bcbb6c8455c9ff980560c447efb415d520aca1a7 [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
Eric Christopherc36145f2012-01-06 04:35:23 +000027// The length of the header data is always going to be 4 + 4 + 4*NumAtoms.
Eric Christopher5b9544b2013-09-05 16:46:43 +000028DwarfAccelTable::DwarfAccelTable(ArrayRef<DwarfAccelTable::Atom> atomList)
29 : Header(8 + (atomList.size() * 4)), HeaderData(atomList),
30 Entries(Allocator) {}
Eric Christopherc36145f2012-01-06 04:35:23 +000031
Eric Christopher5b9544b2013-09-05 16:46:43 +000032DwarfAccelTable::~DwarfAccelTable() {}
Eric Christophere77546c2011-11-07 21:49:28 +000033
Stephen Hines36b56882014-04-23 16:57:46 -070034void DwarfAccelTable::AddName(StringRef Name, const DIE *die, char Flags) {
Benjamin Kramer36c38b82012-04-13 20:06:17 +000035 assert(Data.empty() && "Already finalized!");
Eric Christopherbcbd3a42011-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.
Eric Christopherc36145f2012-01-06 04:35:23 +000038 DataArray &DIEs = Entries[Name];
Benjamin Kramer36c38b82012-04-13 20:06:17 +000039 DIEs.push_back(new (Allocator) HashDataContents(die, Flags));
Eric Christopherbcbd3a42011-11-07 09:18:42 +000040}
41
42void DwarfAccelTable::ComputeBucketCount(void) {
43 // First get the number of unique hashes.
Benjamin Kramerbe3f0512012-03-26 14:17:26 +000044 std::vector<uint32_t> uniques(Data.size());
Eric Christopher30b4d8b2011-11-08 18:38:40 +000045 for (size_t i = 0, e = Data.size(); i < e; ++i)
Eric Christopherbcbd3a42011-11-07 09:18:42 +000046 uniques[i] = Data[i]->HashValue;
Benjamin Kramerbe3f0512012-03-26 14:17:26 +000047 array_pod_sort(uniques.begin(), uniques.end());
Eric Christopherbcbd3a42011-11-07 09:18:42 +000048 std::vector<uint32_t>::iterator p =
Eric Christopher5b9544b2013-09-05 16:46:43 +000049 std::unique(uniques.begin(), uniques.end());
Eric Christopherbcbd3a42011-11-07 09:18:42 +000050 uint32_t num = std::distance(uniques.begin(), p);
51
52 // Then compute the bucket size, minimum of 1 bucket.
Eric Christopher5b9544b2013-09-05 16:46:43 +000053 if (num > 1024)
54 Header.bucket_count = num / 4;
55 if (num > 16)
56 Header.bucket_count = num / 2;
57 else
58 Header.bucket_count = num > 0 ? num : 1;
Eric Christopherbcbd3a42011-11-07 09:18:42 +000059
60 Header.hashes_count = num;
61}
62
Benjamin Kramer36c38b82012-04-13 20:06:17 +000063// compareDIEs - comparison predicate that sorts DIEs by their offset.
64static bool compareDIEs(const DwarfAccelTable::HashDataContents *A,
65 const DwarfAccelTable::HashDataContents *B) {
66 return A->Die->getOffset() < B->Die->getOffset();
Eric Christopher8368f742011-11-15 23:37:17 +000067}
68
Benjamin Kramer03406c42013-05-11 18:24:28 +000069void DwarfAccelTable::FinalizeTable(AsmPrinter *Asm, StringRef Prefix) {
Eric Christopherbcbd3a42011-11-07 09:18:42 +000070 // Create the individual hash data outputs.
Eric Christopher5b9544b2013-09-05 16:46:43 +000071 for (StringMap<DataArray>::iterator EI = Entries.begin(), EE = Entries.end();
72 EI != EE; ++EI) {
Eric Christopher0ffe2b42011-11-10 19:25:34 +000073
74 // Unique the entries.
Benjamin Kramer36c38b82012-04-13 20:06:17 +000075 std::stable_sort(EI->second.begin(), EI->second.end(), compareDIEs);
Eric Christopher547abbb2012-01-06 23:03:27 +000076 EI->second.erase(std::unique(EI->second.begin(), EI->second.end()),
Eric Christopher5b9544b2013-09-05 16:46:43 +000077 EI->second.end());
Eric Christopher0ffe2b42011-11-10 19:25:34 +000078
Benjamin Kramer36c38b82012-04-13 20:06:17 +000079 HashData *Entry = new (Allocator) HashData(EI->getKey(), EI->second);
Eric Christopherbcbd3a42011-11-07 09:18:42 +000080 Data.push_back(Entry);
81 }
82
83 // Figure out how many buckets we need, then compute the bucket
84 // contents and the final ordering. We'll emit the hashes and offsets
85 // by doing a walk during the emission phase. We add temporary
86 // symbols to the data so that we can reference them during the offset
87 // later, we'll emit them when we emit the data.
88 ComputeBucketCount();
89
90 // Compute bucket contents and final ordering.
91 Buckets.resize(Header.bucket_count);
Eric Christopher30b4d8b2011-11-08 18:38:40 +000092 for (size_t i = 0, e = Data.size(); i < e; ++i) {
Eric Christopherbcbd3a42011-11-07 09:18:42 +000093 uint32_t bucket = Data[i]->HashValue % Header.bucket_count;
94 Buckets[bucket].push_back(Data[i]);
95 Data[i]->Sym = Asm->GetTempSymbol(Prefix, i);
96 }
97}
98
99// Emits the header for the table via the AsmPrinter.
100void DwarfAccelTable::EmitHeader(AsmPrinter *Asm) {
101 Asm->OutStreamer.AddComment("Header Magic");
102 Asm->EmitInt32(Header.magic);
103 Asm->OutStreamer.AddComment("Header Version");
104 Asm->EmitInt16(Header.version);
105 Asm->OutStreamer.AddComment("Header Hash Function");
106 Asm->EmitInt16(Header.hash_function);
107 Asm->OutStreamer.AddComment("Header Bucket Count");
108 Asm->EmitInt32(Header.bucket_count);
109 Asm->OutStreamer.AddComment("Header Hash Count");
110 Asm->EmitInt32(Header.hashes_count);
111 Asm->OutStreamer.AddComment("Header Data Length");
112 Asm->EmitInt32(Header.header_data_len);
113 Asm->OutStreamer.AddComment("HeaderData Die Offset Base");
114 Asm->EmitInt32(HeaderData.die_offset_base);
115 Asm->OutStreamer.AddComment("HeaderData Atom Count");
116 Asm->EmitInt32(HeaderData.Atoms.size());
117 for (size_t i = 0; i < HeaderData.Atoms.size(); i++) {
118 Atom A = HeaderData.Atoms[i];
Eric Christopher577056f2013-09-05 18:20:16 +0000119 Asm->OutStreamer.AddComment(dwarf::AtomTypeString(A.type));
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000120 Asm->EmitInt16(A.type);
121 Asm->OutStreamer.AddComment(dwarf::FormEncodingString(A.form));
122 Asm->EmitInt16(A.form);
123 }
124}
125
Eric Christopher2d65efe2012-10-08 23:53:45 +0000126// Walk through and emit the buckets for the table. Each index is
127// an offset into the list of hashes.
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000128void DwarfAccelTable::EmitBuckets(AsmPrinter *Asm) {
129 unsigned index = 0;
Eric Christopher30b4d8b2011-11-08 18:38:40 +0000130 for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
Eric Christopherc5453222011-11-07 18:34:47 +0000131 Asm->OutStreamer.AddComment("Bucket " + Twine(i));
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000132 if (Buckets[i].size() != 0)
133 Asm->EmitInt32(index);
134 else
135 Asm->EmitInt32(UINT32_MAX);
136 index += Buckets[i].size();
137 }
138}
139
140// Walk through the buckets and emit the individual hashes for each
141// bucket.
142void DwarfAccelTable::EmitHashes(AsmPrinter *Asm) {
Eric Christopher30b4d8b2011-11-08 18:38:40 +0000143 for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000144 for (HashList::const_iterator HI = Buckets[i].begin(),
Eric Christopher5b9544b2013-09-05 16:46:43 +0000145 HE = Buckets[i].end();
146 HI != HE; ++HI) {
Eric Christopherc5453222011-11-07 18:34:47 +0000147 Asm->OutStreamer.AddComment("Hash in Bucket " + Twine(i));
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000148 Asm->EmitInt32((*HI)->HashValue);
Eric Christopher72c16552012-12-20 21:58:40 +0000149 }
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000150 }
151}
152
153// Walk through the buckets and emit the individual offsets for each
154// element in each bucket. This is done via a symbol subtraction from the
155// beginning of the section. The non-section symbol will be output later
156// when we emit the actual data.
157void DwarfAccelTable::EmitOffsets(AsmPrinter *Asm, MCSymbol *SecBegin) {
Eric Christopher30b4d8b2011-11-08 18:38:40 +0000158 for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000159 for (HashList::const_iterator HI = Buckets[i].begin(),
Eric Christopher5b9544b2013-09-05 16:46:43 +0000160 HE = Buckets[i].end();
161 HI != HE; ++HI) {
Eric Christopherc5453222011-11-07 18:34:47 +0000162 Asm->OutStreamer.AddComment("Offset in Bucket " + Twine(i));
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000163 MCContext &Context = Asm->OutStreamer.getContext();
Eric Christopher5b9544b2013-09-05 16:46:43 +0000164 const MCExpr *Sub = MCBinaryExpr::CreateSub(
165 MCSymbolRefExpr::Create((*HI)->Sym, Context),
166 MCSymbolRefExpr::Create(SecBegin, Context), Context);
Eric Christopher1ced2082013-01-09 03:52:05 +0000167 Asm->OutStreamer.EmitValue(Sub, sizeof(uint32_t));
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000168 }
169 }
170}
171
172// Walk through the buckets and emit the full data for each element in
173// the bucket. For the string case emit the dies and the various offsets.
174// Terminate each HashData bucket with 0.
Stephen Hines36b56882014-04-23 16:57:46 -0700175void DwarfAccelTable::EmitData(AsmPrinter *Asm, DwarfFile *D) {
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000176 uint64_t PrevHash = UINT64_MAX;
Eric Christopher30b4d8b2011-11-08 18:38:40 +0000177 for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000178 for (HashList::const_iterator HI = Buckets[i].begin(),
Eric Christopher5b9544b2013-09-05 16:46:43 +0000179 HE = Buckets[i].end();
180 HI != HE; ++HI) {
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000181 // Remember to emit the label for our offset.
182 Asm->OutStreamer.EmitLabel((*HI)->Sym);
183 Asm->OutStreamer.AddComment((*HI)->Str);
184 Asm->EmitSectionOffset(D->getStringPoolEntry((*HI)->Str),
Eric Christopher2e5d8702012-12-20 21:58:36 +0000185 D->getStringPoolSym());
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000186 Asm->OutStreamer.AddComment("Num DIEs");
Eric Christopherc36145f2012-01-06 04:35:23 +0000187 Asm->EmitInt32((*HI)->Data.size());
Eric Christopher5b9544b2013-09-05 16:46:43 +0000188 for (ArrayRef<HashDataContents *>::const_iterator
189 DI = (*HI)->Data.begin(),
190 DE = (*HI)->Data.end();
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000191 DI != DE; ++DI) {
Eric Christopherc36145f2012-01-06 04:35:23 +0000192 // Emit the DIE offset
193 Asm->EmitInt32((*DI)->Die->getOffset());
194 // If we have multiple Atoms emit that info too.
195 // FIXME: A bit of a hack, we either emit only one atom or all info.
196 if (HeaderData.Atoms.size() > 1) {
197 Asm->EmitInt16((*DI)->Die->getTag());
198 Asm->EmitInt8((*DI)->Flags);
199 }
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000200 }
201 // Emit a 0 to terminate the data unless we have a hash collision.
202 if (PrevHash != (*HI)->HashValue)
203 Asm->EmitInt32(0);
204 PrevHash = (*HI)->HashValue;
205 }
206 }
207}
208
209// Emit the entire data structure to the output file.
Stephen Hines36b56882014-04-23 16:57:46 -0700210void DwarfAccelTable::Emit(AsmPrinter *Asm, MCSymbol *SecBegin, DwarfFile *D) {
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000211 // Emit the header.
212 EmitHeader(Asm);
213
214 // Emit the buckets.
215 EmitBuckets(Asm);
216
217 // Emit the hashes.
218 EmitHashes(Asm);
219
220 // Emit the offsets.
221 EmitOffsets(Asm, SecBegin);
222
223 // Emit the hash data.
224 EmitData(Asm, D);
225}
226
227#ifndef NDEBUG
228void DwarfAccelTable::print(raw_ostream &O) {
229
230 Header.print(O);
231 HeaderData.print(O);
232
233 O << "Entries: \n";
Eric Christopher5b9544b2013-09-05 16:46:43 +0000234 for (StringMap<DataArray>::const_iterator EI = Entries.begin(),
235 EE = Entries.end();
236 EI != EE; ++EI) {
Eric Christopher547abbb2012-01-06 23:03:27 +0000237 O << "Name: " << EI->getKeyData() << "\n";
238 for (DataArray::const_iterator DI = EI->second.begin(),
Eric Christopher5b9544b2013-09-05 16:46:43 +0000239 DE = EI->second.end();
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000240 DI != DE; ++DI)
241 (*DI)->print(O);
242 }
243
244 O << "Buckets and Hashes: \n";
Eric Christopher30b4d8b2011-11-08 18:38:40 +0000245 for (size_t i = 0, e = Buckets.size(); i < e; ++i)
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000246 for (HashList::const_iterator HI = Buckets[i].begin(),
Eric Christopher5b9544b2013-09-05 16:46:43 +0000247 HE = Buckets[i].end();
248 HI != HE; ++HI)
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000249 (*HI)->print(O);
250
251 O << "Data: \n";
Eric Christopher5b9544b2013-09-05 16:46:43 +0000252 for (std::vector<HashData *>::const_iterator DI = Data.begin(),
253 DE = Data.end();
254 DI != DE; ++DI)
255 (*DI)->print(O);
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000256}
257#endif