blob: 4a13cc260ad330713a96a0841d7bd0813b984b86 [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
David Blaikie772ab8a2014-04-25 22:21:35 +000033void DwarfAccelTable::AddName(StringRef Name, MCSymbol *StrSym, const DIE *die,
34 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.
Eric Christopher21bde872012-01-06 04:35:23 +000038 DataArray &DIEs = Entries[Name];
David Blaikie772ab8a2014-04-25 22:21:35 +000039 assert(!DIEs.StrSym || DIEs.StrSym == StrSym);
40 DIEs.StrSym = StrSym;
41 DIEs.Values.push_back(new (Allocator) HashDataContents(die, Flags));
Eric Christopher6e472042011-11-07 09:18:42 +000042}
43
44void DwarfAccelTable::ComputeBucketCount(void) {
45 // 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]);
99 Data[i]->Sym = Asm->GetTempSymbol(Prefix, i);
100 }
101}
102
103// Emits the header for the table via the AsmPrinter.
104void DwarfAccelTable::EmitHeader(AsmPrinter *Asm) {
105 Asm->OutStreamer.AddComment("Header Magic");
106 Asm->EmitInt32(Header.magic);
107 Asm->OutStreamer.AddComment("Header Version");
108 Asm->EmitInt16(Header.version);
109 Asm->OutStreamer.AddComment("Header Hash Function");
110 Asm->EmitInt16(Header.hash_function);
111 Asm->OutStreamer.AddComment("Header Bucket Count");
112 Asm->EmitInt32(Header.bucket_count);
113 Asm->OutStreamer.AddComment("Header Hash Count");
114 Asm->EmitInt32(Header.hashes_count);
115 Asm->OutStreamer.AddComment("Header Data Length");
116 Asm->EmitInt32(Header.header_data_len);
117 Asm->OutStreamer.AddComment("HeaderData Die Offset Base");
118 Asm->EmitInt32(HeaderData.die_offset_base);
119 Asm->OutStreamer.AddComment("HeaderData Atom Count");
120 Asm->EmitInt32(HeaderData.Atoms.size());
121 for (size_t i = 0; i < HeaderData.Atoms.size(); i++) {
122 Atom A = HeaderData.Atoms[i];
Eric Christophercf7289f2013-09-05 18:20:16 +0000123 Asm->OutStreamer.AddComment(dwarf::AtomTypeString(A.type));
Eric Christopher6e472042011-11-07 09:18:42 +0000124 Asm->EmitInt16(A.type);
125 Asm->OutStreamer.AddComment(dwarf::FormEncodingString(A.form));
126 Asm->EmitInt16(A.form);
127 }
128}
129
Eric Christopher28611362012-10-08 23:53:45 +0000130// Walk through and emit the buckets for the table. Each index is
131// an offset into the list of hashes.
Eric Christopher6e472042011-11-07 09:18:42 +0000132void DwarfAccelTable::EmitBuckets(AsmPrinter *Asm) {
133 unsigned index = 0;
Eric Christopher54ce295d2011-11-08 18:38:40 +0000134 for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
Eric Christopher2c5dab52011-11-07 18:34:47 +0000135 Asm->OutStreamer.AddComment("Bucket " + Twine(i));
Eric Christopher6e472042011-11-07 09:18:42 +0000136 if (Buckets[i].size() != 0)
137 Asm->EmitInt32(index);
138 else
139 Asm->EmitInt32(UINT32_MAX);
140 index += Buckets[i].size();
141 }
142}
143
144// Walk through the buckets and emit the individual hashes for each
145// bucket.
146void DwarfAccelTable::EmitHashes(AsmPrinter *Asm) {
Eric Christopher54ce295d2011-11-08 18:38:40 +0000147 for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
Eric Christopher6e472042011-11-07 09:18:42 +0000148 for (HashList::const_iterator HI = Buckets[i].begin(),
Eric Christopherb4e2cc42013-09-05 16:46:43 +0000149 HE = Buckets[i].end();
150 HI != HE; ++HI) {
Eric Christopher2c5dab52011-11-07 18:34:47 +0000151 Asm->OutStreamer.AddComment("Hash in Bucket " + Twine(i));
Eric Christopher6e472042011-11-07 09:18:42 +0000152 Asm->EmitInt32((*HI)->HashValue);
Eric Christopher48fef592012-12-20 21:58:40 +0000153 }
Eric Christopher6e472042011-11-07 09:18:42 +0000154 }
155}
156
157// Walk through the buckets and emit the individual offsets for each
158// element in each bucket. This is done via a symbol subtraction from the
159// beginning of the section. The non-section symbol will be output later
160// when we emit the actual data.
161void DwarfAccelTable::EmitOffsets(AsmPrinter *Asm, MCSymbol *SecBegin) {
Eric Christopher54ce295d2011-11-08 18:38:40 +0000162 for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
Eric Christopher6e472042011-11-07 09:18:42 +0000163 for (HashList::const_iterator HI = Buckets[i].begin(),
Eric Christopherb4e2cc42013-09-05 16:46:43 +0000164 HE = Buckets[i].end();
165 HI != HE; ++HI) {
Eric Christopher2c5dab52011-11-07 18:34:47 +0000166 Asm->OutStreamer.AddComment("Offset in Bucket " + Twine(i));
Eric Christopher6e472042011-11-07 09:18:42 +0000167 MCContext &Context = Asm->OutStreamer.getContext();
Eric Christopherb4e2cc42013-09-05 16:46:43 +0000168 const MCExpr *Sub = MCBinaryExpr::CreateSub(
169 MCSymbolRefExpr::Create((*HI)->Sym, Context),
170 MCSymbolRefExpr::Create(SecBegin, Context), Context);
Eric Christopherbf7bc492013-01-09 03:52:05 +0000171 Asm->OutStreamer.EmitValue(Sub, sizeof(uint32_t));
Eric Christopher6e472042011-11-07 09:18:42 +0000172 }
173 }
174}
175
176// Walk through the buckets and emit the full data for each element in
177// the bucket. For the string case emit the dies and the various offsets.
178// Terminate each HashData bucket with 0.
Frederic Riss3a6b3542014-11-12 23:48:14 +0000179void DwarfAccelTable::EmitData(AsmPrinter *Asm, DwarfDebug *D,
David Blaikie6741bb02014-09-11 21:12:48 +0000180 MCSymbol *StrSym) {
Eric Christopher6e472042011-11-07 09:18:42 +0000181 uint64_t PrevHash = UINT64_MAX;
Eric Christopher54ce295d2011-11-08 18:38:40 +0000182 for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
Eric Christopher6e472042011-11-07 09:18:42 +0000183 for (HashList::const_iterator HI = Buckets[i].begin(),
Eric Christopherb4e2cc42013-09-05 16:46:43 +0000184 HE = Buckets[i].end();
185 HI != HE; ++HI) {
Eric Christopher6e472042011-11-07 09:18:42 +0000186 // Remember to emit the label for our offset.
187 Asm->OutStreamer.EmitLabel((*HI)->Sym);
188 Asm->OutStreamer.AddComment((*HI)->Str);
David Blaikie6741bb02014-09-11 21:12:48 +0000189 Asm->EmitSectionOffset((*HI)->Data.StrSym, StrSym);
Eric Christopher6e472042011-11-07 09:18:42 +0000190 Asm->OutStreamer.AddComment("Num DIEs");
David Blaikie772ab8a2014-04-25 22:21:35 +0000191 Asm->EmitInt32((*HI)->Data.Values.size());
192 for (HashDataContents *HD : (*HI)->Data.Values) {
Eric Christopher21bde872012-01-06 04:35:23 +0000193 // Emit the DIE offset
Frederic Riss3a6b3542014-11-12 23:48:14 +0000194 DwarfCompileUnit *CU = D->lookupUnit(HD->Die->getUnit());
195 assert(CU && "Accelerated DIE should belong to a CU.");
196 Asm->EmitInt32(HD->Die->getOffset() + CU->getDebugInfoOffset());
Eric Christopher21bde872012-01-06 04:35:23 +0000197 // If we have multiple Atoms emit that info too.
198 // FIXME: A bit of a hack, we either emit only one atom or all info.
199 if (HeaderData.Atoms.size() > 1) {
David Blaikie772ab8a2014-04-25 22:21:35 +0000200 Asm->EmitInt16(HD->Die->getTag());
201 Asm->EmitInt8(HD->Flags);
Eric Christopher21bde872012-01-06 04:35:23 +0000202 }
Eric Christopher6e472042011-11-07 09:18:42 +0000203 }
204 // Emit a 0 to terminate the data unless we have a hash collision.
205 if (PrevHash != (*HI)->HashValue)
206 Asm->EmitInt32(0);
207 PrevHash = (*HI)->HashValue;
208 }
209 }
210}
211
212// Emit the entire data structure to the output file.
Frederic Riss3a6b3542014-11-12 23:48:14 +0000213void DwarfAccelTable::Emit(AsmPrinter *Asm, MCSymbol *SecBegin, DwarfDebug *D,
David Blaikie6741bb02014-09-11 21:12:48 +0000214 MCSymbol *StrSym) {
Eric Christopher6e472042011-11-07 09:18:42 +0000215 // Emit the header.
216 EmitHeader(Asm);
217
218 // Emit the buckets.
219 EmitBuckets(Asm);
220
221 // Emit the hashes.
222 EmitHashes(Asm);
223
224 // Emit the offsets.
225 EmitOffsets(Asm, SecBegin);
226
227 // Emit the hash data.
David Blaikie6741bb02014-09-11 21:12:48 +0000228 EmitData(Asm, D, StrSym);
Eric Christopher6e472042011-11-07 09:18:42 +0000229}
230
231#ifndef NDEBUG
232void DwarfAccelTable::print(raw_ostream &O) {
233
234 Header.print(O);
235 HeaderData.print(O);
236
237 O << "Entries: \n";
Eric Christopherb4e2cc42013-09-05 16:46:43 +0000238 for (StringMap<DataArray>::const_iterator EI = Entries.begin(),
239 EE = Entries.end();
240 EI != EE; ++EI) {
Eric Christopher5a28a6e2012-01-06 23:03:27 +0000241 O << "Name: " << EI->getKeyData() << "\n";
David Blaikie772ab8a2014-04-25 22:21:35 +0000242 for (HashDataContents *HD : EI->second.Values)
243 HD->print(O);
Eric Christopher6e472042011-11-07 09:18:42 +0000244 }
245
246 O << "Buckets and Hashes: \n";
Eric Christopher54ce295d2011-11-08 18:38:40 +0000247 for (size_t i = 0, e = Buckets.size(); i < e; ++i)
Eric Christopher6e472042011-11-07 09:18:42 +0000248 for (HashList::const_iterator HI = Buckets[i].begin(),
Eric Christopherb4e2cc42013-09-05 16:46:43 +0000249 HE = Buckets[i].end();
250 HI != HE; ++HI)
Eric Christopher6e472042011-11-07 09:18:42 +0000251 (*HI)->print(O);
252
253 O << "Data: \n";
Eric Christopherb4e2cc42013-09-05 16:46:43 +0000254 for (std::vector<HashData *>::const_iterator DI = Data.begin(),
255 DE = Data.end();
256 DI != DE; ++DI)
257 (*DI)->print(O);
Eric Christopher6e472042011-11-07 09:18:42 +0000258}
259#endif