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