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" |
Craig Topper | f1d0f77 | 2012-03-26 06:58:25 +0000 | [diff] [blame] | 15 | #include "DIE.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" |
| 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 | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 24 | |
| 25 | using namespace llvm; |
| 26 | |
| 27 | const char *DwarfAccelTable::Atom::AtomTypeString(enum AtomType AT) { |
| 28 | switch (AT) { |
Eric Christopher | 5b9544b | 2013-09-05 16:46:43 +0000 | [diff] [blame^] | 29 | 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 Christopher | 72c1655 | 2012-12-20 21:58:40 +0000 | [diff] [blame] | 41 | } |
David Blaikie | 2dd674f | 2012-01-16 23:24:27 +0000 | [diff] [blame] | 42 | llvm_unreachable("invalid AtomType!"); |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Eric Christopher | c36145f | 2012-01-06 04:35:23 +0000 | [diff] [blame] | 45 | // 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^] | 46 | DwarfAccelTable::DwarfAccelTable(ArrayRef<DwarfAccelTable::Atom> atomList) |
| 47 | : Header(8 + (atomList.size() * 4)), HeaderData(atomList), |
| 48 | Entries(Allocator) {} |
Eric Christopher | c36145f | 2012-01-06 04:35:23 +0000 | [diff] [blame] | 49 | |
Eric Christopher | 5b9544b | 2013-09-05 16:46:43 +0000 | [diff] [blame^] | 50 | DwarfAccelTable::~DwarfAccelTable() {} |
Eric Christopher | e77546c | 2011-11-07 21:49:28 +0000 | [diff] [blame] | 51 | |
Eric Christopher | 5b9544b | 2013-09-05 16:46:43 +0000 | [diff] [blame^] | 52 | void DwarfAccelTable::AddName(StringRef Name, DIE *die, char Flags) { |
Benjamin Kramer | 36c38b8 | 2012-04-13 20:06:17 +0000 | [diff] [blame] | 53 | assert(Data.empty() && "Already finalized!"); |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 54 | // If the string is in the list already then add this die to the list |
| 55 | // otherwise add a new one. |
Eric Christopher | c36145f | 2012-01-06 04:35:23 +0000 | [diff] [blame] | 56 | DataArray &DIEs = Entries[Name]; |
Benjamin Kramer | 36c38b8 | 2012-04-13 20:06:17 +0000 | [diff] [blame] | 57 | DIEs.push_back(new (Allocator) HashDataContents(die, Flags)); |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | void DwarfAccelTable::ComputeBucketCount(void) { |
| 61 | // First get the number of unique hashes. |
Benjamin Kramer | be3f051 | 2012-03-26 14:17:26 +0000 | [diff] [blame] | 62 | std::vector<uint32_t> uniques(Data.size()); |
Eric Christopher | 30b4d8b | 2011-11-08 18:38:40 +0000 | [diff] [blame] | 63 | for (size_t i = 0, e = Data.size(); i < e; ++i) |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 64 | uniques[i] = Data[i]->HashValue; |
Benjamin Kramer | be3f051 | 2012-03-26 14:17:26 +0000 | [diff] [blame] | 65 | array_pod_sort(uniques.begin(), uniques.end()); |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 66 | std::vector<uint32_t>::iterator p = |
Eric Christopher | 5b9544b | 2013-09-05 16:46:43 +0000 | [diff] [blame^] | 67 | std::unique(uniques.begin(), uniques.end()); |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 68 | uint32_t num = std::distance(uniques.begin(), p); |
| 69 | |
| 70 | // Then compute the bucket size, minimum of 1 bucket. |
Eric Christopher | 5b9544b | 2013-09-05 16:46:43 +0000 | [diff] [blame^] | 71 | 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 Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 77 | |
| 78 | Header.hashes_count = num; |
| 79 | } |
| 80 | |
Benjamin Kramer | 36c38b8 | 2012-04-13 20:06:17 +0000 | [diff] [blame] | 81 | // compareDIEs - comparison predicate that sorts DIEs by their offset. |
| 82 | static bool compareDIEs(const DwarfAccelTable::HashDataContents *A, |
| 83 | const DwarfAccelTable::HashDataContents *B) { |
| 84 | return A->Die->getOffset() < B->Die->getOffset(); |
Eric Christopher | 8368f74 | 2011-11-15 23:37:17 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Benjamin Kramer | 03406c4 | 2013-05-11 18:24:28 +0000 | [diff] [blame] | 87 | void DwarfAccelTable::FinalizeTable(AsmPrinter *Asm, StringRef Prefix) { |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 88 | // Create the individual hash data outputs. |
Eric Christopher | 5b9544b | 2013-09-05 16:46:43 +0000 | [diff] [blame^] | 89 | for (StringMap<DataArray>::iterator EI = Entries.begin(), EE = Entries.end(); |
| 90 | EI != EE; ++EI) { |
Eric Christopher | 0ffe2b4 | 2011-11-10 19:25:34 +0000 | [diff] [blame] | 91 | |
| 92 | // Unique the entries. |
Benjamin Kramer | 36c38b8 | 2012-04-13 20:06:17 +0000 | [diff] [blame] | 93 | std::stable_sort(EI->second.begin(), EI->second.end(), compareDIEs); |
Eric Christopher | 547abbb | 2012-01-06 23:03:27 +0000 | [diff] [blame] | 94 | EI->second.erase(std::unique(EI->second.begin(), EI->second.end()), |
Eric Christopher | 5b9544b | 2013-09-05 16:46:43 +0000 | [diff] [blame^] | 95 | EI->second.end()); |
Eric Christopher | 0ffe2b4 | 2011-11-10 19:25:34 +0000 | [diff] [blame] | 96 | |
Benjamin Kramer | 36c38b8 | 2012-04-13 20:06:17 +0000 | [diff] [blame] | 97 | HashData *Entry = new (Allocator) HashData(EI->getKey(), EI->second); |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 98 | 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 Christopher | 30b4d8b | 2011-11-08 18:38:40 +0000 | [diff] [blame] | 110 | for (size_t i = 0, e = Data.size(); i < e; ++i) { |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 111 | 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. |
| 118 | void 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 Christopher | 2d65efe | 2012-10-08 23:53:45 +0000 | [diff] [blame] | 144 | // Walk through and emit the buckets for the table. Each index is |
| 145 | // an offset into the list of hashes. |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 146 | void DwarfAccelTable::EmitBuckets(AsmPrinter *Asm) { |
| 147 | unsigned index = 0; |
Eric Christopher | 30b4d8b | 2011-11-08 18:38:40 +0000 | [diff] [blame] | 148 | for (size_t i = 0, e = Buckets.size(); i < e; ++i) { |
Eric Christopher | c545322 | 2011-11-07 18:34:47 +0000 | [diff] [blame] | 149 | Asm->OutStreamer.AddComment("Bucket " + Twine(i)); |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 150 | 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. |
| 160 | void DwarfAccelTable::EmitHashes(AsmPrinter *Asm) { |
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("Hash in Bucket " + Twine(i)); |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 166 | Asm->EmitInt32((*HI)->HashValue); |
Eric Christopher | 72c1655 | 2012-12-20 21:58:40 +0000 | [diff] [blame] | 167 | } |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 168 | } |
| 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. |
| 175 | void DwarfAccelTable::EmitOffsets(AsmPrinter *Asm, MCSymbol *SecBegin) { |
Eric Christopher | 30b4d8b | 2011-11-08 18:38:40 +0000 | [diff] [blame] | 176 | for (size_t i = 0, e = Buckets.size(); i < e; ++i) { |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 177 | for (HashList::const_iterator HI = Buckets[i].begin(), |
Eric Christopher | 5b9544b | 2013-09-05 16:46:43 +0000 | [diff] [blame^] | 178 | HE = Buckets[i].end(); |
| 179 | HI != HE; ++HI) { |
Eric Christopher | c545322 | 2011-11-07 18:34:47 +0000 | [diff] [blame] | 180 | Asm->OutStreamer.AddComment("Offset in Bucket " + Twine(i)); |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 181 | MCContext &Context = Asm->OutStreamer.getContext(); |
Eric Christopher | 5b9544b | 2013-09-05 16:46:43 +0000 | [diff] [blame^] | 182 | const MCExpr *Sub = MCBinaryExpr::CreateSub( |
| 183 | MCSymbolRefExpr::Create((*HI)->Sym, Context), |
| 184 | MCSymbolRefExpr::Create(SecBegin, Context), Context); |
Eric Christopher | 1ced208 | 2013-01-09 03:52:05 +0000 | [diff] [blame] | 185 | Asm->OutStreamer.EmitValue(Sub, sizeof(uint32_t)); |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 186 | } |
| 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 Christopher | 2e5d870 | 2012-12-20 21:58:36 +0000 | [diff] [blame] | 193 | void DwarfAccelTable::EmitData(AsmPrinter *Asm, DwarfUnits *D) { |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 194 | uint64_t PrevHash = UINT64_MAX; |
Eric Christopher | 30b4d8b | 2011-11-08 18:38:40 +0000 | [diff] [blame] | 195 | for (size_t i = 0, e = Buckets.size(); i < e; ++i) { |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 196 | for (HashList::const_iterator HI = Buckets[i].begin(), |
Eric Christopher | 5b9544b | 2013-09-05 16:46:43 +0000 | [diff] [blame^] | 197 | HE = Buckets[i].end(); |
| 198 | HI != HE; ++HI) { |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 199 | // 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 Christopher | 2e5d870 | 2012-12-20 21:58:36 +0000 | [diff] [blame] | 203 | D->getStringPoolSym()); |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 204 | Asm->OutStreamer.AddComment("Num DIEs"); |
Eric Christopher | c36145f | 2012-01-06 04:35:23 +0000 | [diff] [blame] | 205 | Asm->EmitInt32((*HI)->Data.size()); |
Eric Christopher | 5b9544b | 2013-09-05 16:46:43 +0000 | [diff] [blame^] | 206 | for (ArrayRef<HashDataContents *>::const_iterator |
| 207 | DI = (*HI)->Data.begin(), |
| 208 | DE = (*HI)->Data.end(); |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 209 | DI != DE; ++DI) { |
Eric Christopher | c36145f | 2012-01-06 04:35:23 +0000 | [diff] [blame] | 210 | // 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 Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 218 | } |
| 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 Christopher | 5b9544b | 2013-09-05 16:46:43 +0000 | [diff] [blame^] | 228 | void DwarfAccelTable::Emit(AsmPrinter *Asm, MCSymbol *SecBegin, DwarfUnits *D) { |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 229 | // 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 |
| 246 | void DwarfAccelTable::print(raw_ostream &O) { |
| 247 | |
| 248 | Header.print(O); |
| 249 | HeaderData.print(O); |
| 250 | |
| 251 | O << "Entries: \n"; |
Eric Christopher | 5b9544b | 2013-09-05 16:46:43 +0000 | [diff] [blame^] | 252 | for (StringMap<DataArray>::const_iterator EI = Entries.begin(), |
| 253 | EE = Entries.end(); |
| 254 | EI != EE; ++EI) { |
Eric Christopher | 547abbb | 2012-01-06 23:03:27 +0000 | [diff] [blame] | 255 | O << "Name: " << EI->getKeyData() << "\n"; |
| 256 | for (DataArray::const_iterator DI = EI->second.begin(), |
Eric Christopher | 5b9544b | 2013-09-05 16:46:43 +0000 | [diff] [blame^] | 257 | DE = EI->second.end(); |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 258 | DI != DE; ++DI) |
| 259 | (*DI)->print(O); |
| 260 | } |
| 261 | |
| 262 | O << "Buckets and Hashes: \n"; |
Eric Christopher | 30b4d8b | 2011-11-08 18:38:40 +0000 | [diff] [blame] | 263 | for (size_t i = 0, e = Buckets.size(); i < e; ++i) |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 264 | for (HashList::const_iterator HI = Buckets[i].begin(), |
Eric Christopher | 5b9544b | 2013-09-05 16:46:43 +0000 | [diff] [blame^] | 265 | HE = Buckets[i].end(); |
| 266 | HI != HE; ++HI) |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 267 | (*HI)->print(O); |
| 268 | |
| 269 | O << "Data: \n"; |
Eric Christopher | 5b9544b | 2013-09-05 16:46:43 +0000 | [diff] [blame^] | 270 | for (std::vector<HashData *>::const_iterator DI = Data.begin(), |
| 271 | DE = Data.end(); |
| 272 | DI != DE; ++DI) |
| 273 | (*DI)->print(O); |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 274 | } |
| 275 | #endif |