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