Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 1 | //=-- 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 Topper | f1d0f77 | 2012-03-26 06:58:25 +0000 | [diff] [blame] | 14 | #include "DwarfAccelTable.h" |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame] | 15 | #include "DwarfCompileUnit.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 16 | #include "DwarfDebug.h" |
Benjamin Kramer | be3f051 | 2012-03-26 14:17:26 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/Twine.h" |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/AsmPrinter.h" |
Stephen Hines | ebe69fe | 2015-03-23 12:10:34 -0700 | [diff] [blame^] | 20 | #include "llvm/CodeGen/DIE.h" |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCExpr.h" |
| 22 | #include "llvm/MC/MCStreamer.h" |
| 23 | #include "llvm/MC/MCSymbol.h" |
| 24 | #include "llvm/Support/Debug.h" |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 25 | |
| 26 | using namespace llvm; |
| 27 | |
Eric Christopher | c36145f | 2012-01-06 04:35:23 +0000 | [diff] [blame] | 28 | // The length of the header data is always going to be 4 + 4 + 4*NumAtoms. |
Eric Christopher | 5b9544b | 2013-09-05 16:46:43 +0000 | [diff] [blame] | 29 | DwarfAccelTable::DwarfAccelTable(ArrayRef<DwarfAccelTable::Atom> atomList) |
| 30 | : Header(8 + (atomList.size() * 4)), HeaderData(atomList), |
| 31 | Entries(Allocator) {} |
Eric Christopher | c36145f | 2012-01-06 04:35:23 +0000 | [diff] [blame] | 32 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 33 | void DwarfAccelTable::AddName(StringRef Name, MCSymbol *StrSym, const DIE *die, |
| 34 | char Flags) { |
Benjamin Kramer | 36c38b8 | 2012-04-13 20:06:17 +0000 | [diff] [blame] | 35 | assert(Data.empty() && "Already finalized!"); |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 36 | // If the string is in the list already then add this die to the list |
| 37 | // otherwise add a new one. |
Eric Christopher | c36145f | 2012-01-06 04:35:23 +0000 | [diff] [blame] | 38 | DataArray &DIEs = Entries[Name]; |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 39 | assert(!DIEs.StrSym || DIEs.StrSym == StrSym); |
| 40 | DIEs.StrSym = StrSym; |
| 41 | DIEs.Values.push_back(new (Allocator) HashDataContents(die, Flags)); |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | void DwarfAccelTable::ComputeBucketCount(void) { |
| 45 | // First get the number of unique hashes. |
Benjamin Kramer | be3f051 | 2012-03-26 14:17:26 +0000 | [diff] [blame] | 46 | std::vector<uint32_t> uniques(Data.size()); |
Eric Christopher | 30b4d8b | 2011-11-08 18:38:40 +0000 | [diff] [blame] | 47 | for (size_t i = 0, e = Data.size(); i < e; ++i) |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 48 | uniques[i] = Data[i]->HashValue; |
Benjamin Kramer | be3f051 | 2012-03-26 14:17:26 +0000 | [diff] [blame] | 49 | array_pod_sort(uniques.begin(), uniques.end()); |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 50 | std::vector<uint32_t>::iterator p = |
Eric Christopher | 5b9544b | 2013-09-05 16:46:43 +0000 | [diff] [blame] | 51 | std::unique(uniques.begin(), uniques.end()); |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 52 | uint32_t num = std::distance(uniques.begin(), p); |
| 53 | |
| 54 | // Then compute the bucket size, minimum of 1 bucket. |
Eric Christopher | 5b9544b | 2013-09-05 16:46:43 +0000 | [diff] [blame] | 55 | if (num > 1024) |
| 56 | Header.bucket_count = num / 4; |
| 57 | if (num > 16) |
| 58 | Header.bucket_count = num / 2; |
| 59 | else |
| 60 | Header.bucket_count = num > 0 ? num : 1; |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 61 | |
| 62 | Header.hashes_count = num; |
| 63 | } |
| 64 | |
Benjamin Kramer | 36c38b8 | 2012-04-13 20:06:17 +0000 | [diff] [blame] | 65 | // compareDIEs - comparison predicate that sorts DIEs by their offset. |
| 66 | static bool compareDIEs(const DwarfAccelTable::HashDataContents *A, |
| 67 | const DwarfAccelTable::HashDataContents *B) { |
| 68 | return A->Die->getOffset() < B->Die->getOffset(); |
Eric Christopher | 8368f74 | 2011-11-15 23:37:17 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Benjamin Kramer | 03406c4 | 2013-05-11 18:24:28 +0000 | [diff] [blame] | 71 | void DwarfAccelTable::FinalizeTable(AsmPrinter *Asm, StringRef Prefix) { |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 72 | // Create the individual hash data outputs. |
Eric Christopher | 5b9544b | 2013-09-05 16:46:43 +0000 | [diff] [blame] | 73 | for (StringMap<DataArray>::iterator EI = Entries.begin(), EE = Entries.end(); |
| 74 | EI != EE; ++EI) { |
Eric Christopher | 0ffe2b4 | 2011-11-10 19:25:34 +0000 | [diff] [blame] | 75 | |
| 76 | // Unique the entries. |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 77 | std::stable_sort(EI->second.Values.begin(), EI->second.Values.end(), compareDIEs); |
| 78 | EI->second.Values.erase( |
| 79 | std::unique(EI->second.Values.begin(), EI->second.Values.end()), |
| 80 | EI->second.Values.end()); |
Eric Christopher | 0ffe2b4 | 2011-11-10 19:25:34 +0000 | [diff] [blame] | 81 | |
Benjamin Kramer | 36c38b8 | 2012-04-13 20:06:17 +0000 | [diff] [blame] | 82 | HashData *Entry = new (Allocator) HashData(EI->getKey(), EI->second); |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 83 | Data.push_back(Entry); |
| 84 | } |
| 85 | |
| 86 | // Figure out how many buckets we need, then compute the bucket |
| 87 | // contents and the final ordering. We'll emit the hashes and offsets |
| 88 | // by doing a walk during the emission phase. We add temporary |
| 89 | // symbols to the data so that we can reference them during the offset |
| 90 | // later, we'll emit them when we emit the data. |
| 91 | ComputeBucketCount(); |
| 92 | |
| 93 | // Compute bucket contents and final ordering. |
| 94 | Buckets.resize(Header.bucket_count); |
Eric Christopher | 30b4d8b | 2011-11-08 18:38:40 +0000 | [diff] [blame] | 95 | for (size_t i = 0, e = Data.size(); i < e; ++i) { |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 96 | uint32_t bucket = Data[i]->HashValue % Header.bucket_count; |
| 97 | Buckets[bucket].push_back(Data[i]); |
| 98 | Data[i]->Sym = Asm->GetTempSymbol(Prefix, i); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | // Emits the header for the table via the AsmPrinter. |
| 103 | void DwarfAccelTable::EmitHeader(AsmPrinter *Asm) { |
| 104 | Asm->OutStreamer.AddComment("Header Magic"); |
| 105 | Asm->EmitInt32(Header.magic); |
| 106 | Asm->OutStreamer.AddComment("Header Version"); |
| 107 | Asm->EmitInt16(Header.version); |
| 108 | Asm->OutStreamer.AddComment("Header Hash Function"); |
| 109 | Asm->EmitInt16(Header.hash_function); |
| 110 | Asm->OutStreamer.AddComment("Header Bucket Count"); |
| 111 | Asm->EmitInt32(Header.bucket_count); |
| 112 | Asm->OutStreamer.AddComment("Header Hash Count"); |
| 113 | Asm->EmitInt32(Header.hashes_count); |
| 114 | Asm->OutStreamer.AddComment("Header Data Length"); |
| 115 | Asm->EmitInt32(Header.header_data_len); |
| 116 | Asm->OutStreamer.AddComment("HeaderData Die Offset Base"); |
| 117 | Asm->EmitInt32(HeaderData.die_offset_base); |
| 118 | Asm->OutStreamer.AddComment("HeaderData Atom Count"); |
| 119 | Asm->EmitInt32(HeaderData.Atoms.size()); |
| 120 | for (size_t i = 0; i < HeaderData.Atoms.size(); i++) { |
| 121 | Atom A = HeaderData.Atoms[i]; |
Eric Christopher | 577056f | 2013-09-05 18:20:16 +0000 | [diff] [blame] | 122 | Asm->OutStreamer.AddComment(dwarf::AtomTypeString(A.type)); |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 123 | Asm->EmitInt16(A.type); |
| 124 | Asm->OutStreamer.AddComment(dwarf::FormEncodingString(A.form)); |
| 125 | Asm->EmitInt16(A.form); |
| 126 | } |
| 127 | } |
| 128 | |
Eric Christopher | 2d65efe | 2012-10-08 23:53:45 +0000 | [diff] [blame] | 129 | // Walk through and emit the buckets for the table. Each index is |
| 130 | // an offset into the list of hashes. |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 131 | void DwarfAccelTable::EmitBuckets(AsmPrinter *Asm) { |
| 132 | unsigned index = 0; |
Eric Christopher | 30b4d8b | 2011-11-08 18:38:40 +0000 | [diff] [blame] | 133 | for (size_t i = 0, e = Buckets.size(); i < e; ++i) { |
Eric Christopher | c545322 | 2011-11-07 18:34:47 +0000 | [diff] [blame] | 134 | Asm->OutStreamer.AddComment("Bucket " + Twine(i)); |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 135 | if (Buckets[i].size() != 0) |
| 136 | Asm->EmitInt32(index); |
| 137 | else |
| 138 | Asm->EmitInt32(UINT32_MAX); |
| 139 | index += Buckets[i].size(); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | // Walk through the buckets and emit the individual hashes for each |
| 144 | // bucket. |
| 145 | void DwarfAccelTable::EmitHashes(AsmPrinter *Asm) { |
Eric Christopher | 30b4d8b | 2011-11-08 18:38:40 +0000 | [diff] [blame] | 146 | for (size_t i = 0, e = Buckets.size(); i < e; ++i) { |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 147 | for (HashList::const_iterator HI = Buckets[i].begin(), |
Eric Christopher | 5b9544b | 2013-09-05 16:46:43 +0000 | [diff] [blame] | 148 | HE = Buckets[i].end(); |
| 149 | HI != HE; ++HI) { |
Eric Christopher | c545322 | 2011-11-07 18:34:47 +0000 | [diff] [blame] | 150 | Asm->OutStreamer.AddComment("Hash in Bucket " + Twine(i)); |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 151 | Asm->EmitInt32((*HI)->HashValue); |
Eric Christopher | 72c1655 | 2012-12-20 21:58:40 +0000 | [diff] [blame] | 152 | } |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | |
| 156 | // Walk through the buckets and emit the individual offsets for each |
| 157 | // element in each bucket. This is done via a symbol subtraction from the |
| 158 | // beginning of the section. The non-section symbol will be output later |
| 159 | // when we emit the actual data. |
| 160 | void DwarfAccelTable::EmitOffsets(AsmPrinter *Asm, MCSymbol *SecBegin) { |
Eric Christopher | 30b4d8b | 2011-11-08 18:38:40 +0000 | [diff] [blame] | 161 | for (size_t i = 0, e = Buckets.size(); i < e; ++i) { |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 162 | for (HashList::const_iterator HI = Buckets[i].begin(), |
Eric Christopher | 5b9544b | 2013-09-05 16:46:43 +0000 | [diff] [blame] | 163 | HE = Buckets[i].end(); |
| 164 | HI != HE; ++HI) { |
Eric Christopher | c545322 | 2011-11-07 18:34:47 +0000 | [diff] [blame] | 165 | Asm->OutStreamer.AddComment("Offset in Bucket " + Twine(i)); |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 166 | MCContext &Context = Asm->OutStreamer.getContext(); |
Eric Christopher | 5b9544b | 2013-09-05 16:46:43 +0000 | [diff] [blame] | 167 | const MCExpr *Sub = MCBinaryExpr::CreateSub( |
| 168 | MCSymbolRefExpr::Create((*HI)->Sym, Context), |
| 169 | MCSymbolRefExpr::Create(SecBegin, Context), Context); |
Eric Christopher | 1ced208 | 2013-01-09 03:52:05 +0000 | [diff] [blame] | 170 | Asm->OutStreamer.EmitValue(Sub, sizeof(uint32_t)); |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | // Walk through the buckets and emit the full data for each element in |
| 176 | // the bucket. For the string case emit the dies and the various offsets. |
| 177 | // Terminate each HashData bucket with 0. |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame] | 178 | void DwarfAccelTable::EmitData(AsmPrinter *Asm, DwarfDebug *D, |
| 179 | MCSymbol *StrSym) { |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 180 | uint64_t PrevHash = UINT64_MAX; |
Eric Christopher | 30b4d8b | 2011-11-08 18:38:40 +0000 | [diff] [blame] | 181 | for (size_t i = 0, e = Buckets.size(); i < e; ++i) { |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 182 | for (HashList::const_iterator HI = Buckets[i].begin(), |
Eric Christopher | 5b9544b | 2013-09-05 16:46:43 +0000 | [diff] [blame] | 183 | HE = Buckets[i].end(); |
| 184 | HI != HE; ++HI) { |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 185 | // Remember to emit the label for our offset. |
| 186 | Asm->OutStreamer.EmitLabel((*HI)->Sym); |
| 187 | Asm->OutStreamer.AddComment((*HI)->Str); |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame] | 188 | Asm->EmitSectionOffset((*HI)->Data.StrSym, StrSym); |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 189 | Asm->OutStreamer.AddComment("Num DIEs"); |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 190 | Asm->EmitInt32((*HI)->Data.Values.size()); |
| 191 | for (HashDataContents *HD : (*HI)->Data.Values) { |
Eric Christopher | c36145f | 2012-01-06 04:35:23 +0000 | [diff] [blame] | 192 | // Emit the DIE offset |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame] | 193 | DwarfCompileUnit *CU = D->lookupUnit(HD->Die->getUnit()); |
| 194 | assert(CU && "Accelerated DIE should belong to a CU."); |
| 195 | Asm->EmitInt32(HD->Die->getOffset() + CU->getDebugInfoOffset()); |
Eric Christopher | c36145f | 2012-01-06 04:35:23 +0000 | [diff] [blame] | 196 | // If we have multiple Atoms emit that info too. |
| 197 | // FIXME: A bit of a hack, we either emit only one atom or all info. |
| 198 | if (HeaderData.Atoms.size() > 1) { |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 199 | Asm->EmitInt16(HD->Die->getTag()); |
| 200 | Asm->EmitInt8(HD->Flags); |
Eric Christopher | c36145f | 2012-01-06 04:35:23 +0000 | [diff] [blame] | 201 | } |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 202 | } |
| 203 | // Emit a 0 to terminate the data unless we have a hash collision. |
| 204 | if (PrevHash != (*HI)->HashValue) |
| 205 | Asm->EmitInt32(0); |
| 206 | PrevHash = (*HI)->HashValue; |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | // Emit the entire data structure to the output file. |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame] | 212 | void DwarfAccelTable::Emit(AsmPrinter *Asm, MCSymbol *SecBegin, DwarfDebug *D, |
| 213 | MCSymbol *StrSym) { |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 214 | // Emit the header. |
| 215 | EmitHeader(Asm); |
| 216 | |
| 217 | // Emit the buckets. |
| 218 | EmitBuckets(Asm); |
| 219 | |
| 220 | // Emit the hashes. |
| 221 | EmitHashes(Asm); |
| 222 | |
| 223 | // Emit the offsets. |
| 224 | EmitOffsets(Asm, SecBegin); |
| 225 | |
| 226 | // Emit the hash data. |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame] | 227 | EmitData(Asm, D, StrSym); |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | #ifndef NDEBUG |
| 231 | void DwarfAccelTable::print(raw_ostream &O) { |
| 232 | |
| 233 | Header.print(O); |
| 234 | HeaderData.print(O); |
| 235 | |
| 236 | O << "Entries: \n"; |
Eric Christopher | 5b9544b | 2013-09-05 16:46:43 +0000 | [diff] [blame] | 237 | for (StringMap<DataArray>::const_iterator EI = Entries.begin(), |
| 238 | EE = Entries.end(); |
| 239 | EI != EE; ++EI) { |
Eric Christopher | 547abbb | 2012-01-06 23:03:27 +0000 | [diff] [blame] | 240 | O << "Name: " << EI->getKeyData() << "\n"; |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 241 | for (HashDataContents *HD : EI->second.Values) |
| 242 | HD->print(O); |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | O << "Buckets and Hashes: \n"; |
Eric Christopher | 30b4d8b | 2011-11-08 18:38:40 +0000 | [diff] [blame] | 246 | for (size_t i = 0, e = Buckets.size(); i < e; ++i) |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 247 | for (HashList::const_iterator HI = Buckets[i].begin(), |
Eric Christopher | 5b9544b | 2013-09-05 16:46:43 +0000 | [diff] [blame] | 248 | HE = Buckets[i].end(); |
| 249 | HI != HE; ++HI) |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 250 | (*HI)->print(O); |
| 251 | |
| 252 | O << "Data: \n"; |
Eric Christopher | 5b9544b | 2013-09-05 16:46:43 +0000 | [diff] [blame] | 253 | for (std::vector<HashData *>::const_iterator DI = Data.begin(), |
| 254 | DE = Data.end(); |
| 255 | DI != DE; ++DI) |
| 256 | (*DI)->print(O); |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 257 | } |
| 258 | #endif |