Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 1 | //===- llvm/CodeGen/DwarfAccelTable.cpp - Dwarf Accelerator Tables --------===// |
Eric Christopher | 6e47204 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 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" |
Benjamin Kramer | 3e6719c | 2012-03-26 14:17:26 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/STLExtras.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/StringMap.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/Twine.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 18 | #include "llvm/BinaryFormat/Dwarf.h" |
Eric Christopher | 6e47204 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/AsmPrinter.h" |
Frederic Riss | e541e0b | 2015-01-05 21:29:41 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/DIE.h" |
Eric Christopher | 6e47204 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCExpr.h" |
| 22 | #include "llvm/MC/MCStreamer.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 23 | #include "llvm/Support/raw_ostream.h" |
| 24 | #include <algorithm> |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 25 | #include <cstddef> |
| 26 | #include <cstdint> |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 27 | #include <limits> |
| 28 | #include <vector> |
Eric Christopher | 6e47204 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 29 | |
| 30 | using namespace llvm; |
| 31 | |
Jonas Devlieghere | e699dfa | 2018-01-29 14:52:34 +0000 | [diff] [blame^] | 32 | void AppleAccelTableHeader::emit(AsmPrinter *Asm) { |
| 33 | // Emit Header. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 34 | Asm->OutStreamer->AddComment("Header Magic"); |
Jonas Devlieghere | e699dfa | 2018-01-29 14:52:34 +0000 | [diff] [blame^] | 35 | Asm->EmitInt32(Header.Magic); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 36 | Asm->OutStreamer->AddComment("Header Version"); |
Jonas Devlieghere | e699dfa | 2018-01-29 14:52:34 +0000 | [diff] [blame^] | 37 | Asm->EmitInt16(Header.Version); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 38 | Asm->OutStreamer->AddComment("Header Hash Function"); |
Jonas Devlieghere | e699dfa | 2018-01-29 14:52:34 +0000 | [diff] [blame^] | 39 | Asm->EmitInt16(Header.HashFunction); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 40 | Asm->OutStreamer->AddComment("Header Bucket Count"); |
Jonas Devlieghere | e699dfa | 2018-01-29 14:52:34 +0000 | [diff] [blame^] | 41 | Asm->EmitInt32(Header.BucketCount); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 42 | Asm->OutStreamer->AddComment("Header Hash Count"); |
Jonas Devlieghere | e699dfa | 2018-01-29 14:52:34 +0000 | [diff] [blame^] | 43 | Asm->EmitInt32(Header.HashCount); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 44 | Asm->OutStreamer->AddComment("Header Data Length"); |
Jonas Devlieghere | e699dfa | 2018-01-29 14:52:34 +0000 | [diff] [blame^] | 45 | Asm->EmitInt32(Header.HeaderDataLength); |
| 46 | |
| 47 | // Emit Header Data |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 48 | Asm->OutStreamer->AddComment("HeaderData Die Offset Base"); |
Jonas Devlieghere | e699dfa | 2018-01-29 14:52:34 +0000 | [diff] [blame^] | 49 | Asm->EmitInt32(HeaderData.DieOffsetBase); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 50 | Asm->OutStreamer->AddComment("HeaderData Atom Count"); |
Eric Christopher | 6e47204 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 51 | Asm->EmitInt32(HeaderData.Atoms.size()); |
Jonas Devlieghere | e699dfa | 2018-01-29 14:52:34 +0000 | [diff] [blame^] | 52 | |
Eric Christopher | 6e47204 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 53 | for (size_t i = 0; i < HeaderData.Atoms.size(); i++) { |
| 54 | Atom A = HeaderData.Atoms[i]; |
Jonas Devlieghere | e699dfa | 2018-01-29 14:52:34 +0000 | [diff] [blame^] | 55 | Asm->OutStreamer->AddComment(dwarf::AtomTypeString(A.Type)); |
| 56 | Asm->EmitInt16(A.Type); |
| 57 | Asm->OutStreamer->AddComment(dwarf::FormEncodingString(A.Form)); |
| 58 | Asm->EmitInt16(A.Form); |
Eric Christopher | 6e47204 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | |
Jonas Devlieghere | e699dfa | 2018-01-29 14:52:34 +0000 | [diff] [blame^] | 62 | void AppleAccelTableHeader::setBucketAndHashCount(uint32_t HashCount) { |
| 63 | if (HashCount > 1024) |
| 64 | Header.BucketCount = HashCount / 4; |
| 65 | else if (HashCount > 16) |
| 66 | Header.BucketCount = HashCount / 2; |
| 67 | else |
| 68 | Header.BucketCount = HashCount > 0 ? HashCount : 1; |
| 69 | |
| 70 | Header.HashCount = HashCount; |
| 71 | } |
| 72 | |
| 73 | constexpr AppleAccelTableHeader::Atom AppleAccelTableTypeData::Atoms[]; |
| 74 | constexpr AppleAccelTableHeader::Atom AppleAccelTableOffsetData::Atoms[]; |
| 75 | |
| 76 | void AppleAccelTableBase::emitHeader(AsmPrinter *Asm) { Header.emit(Asm); } |
| 77 | |
| 78 | void AppleAccelTableBase::emitBuckets(AsmPrinter *Asm) { |
Eric Christopher | 6e47204 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 79 | unsigned index = 0; |
Eric Christopher | 54ce295d | 2011-11-08 18:38:40 +0000 | [diff] [blame] | 80 | for (size_t i = 0, e = Buckets.size(); i < e; ++i) { |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 81 | Asm->OutStreamer->AddComment("Bucket " + Twine(i)); |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 82 | if (!Buckets[i].empty()) |
Eric Christopher | 6e47204 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 83 | Asm->EmitInt32(index); |
| 84 | else |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 85 | Asm->EmitInt32(std::numeric_limits<uint32_t>::max()); |
Jonas Devlieghere | e699dfa | 2018-01-29 14:52:34 +0000 | [diff] [blame^] | 86 | // Buckets point in the list of hashes, not to the data. Do not increment |
| 87 | // the index multiple times in case of hash collisions. |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 88 | uint64_t PrevHash = std::numeric_limits<uint64_t>::max(); |
Frederic Riss | 0e9a50f | 2015-03-10 00:46:31 +0000 | [diff] [blame] | 89 | for (auto *HD : Buckets[i]) { |
| 90 | uint32_t HashValue = HD->HashValue; |
| 91 | if (PrevHash != HashValue) |
| 92 | ++index; |
| 93 | PrevHash = HashValue; |
| 94 | } |
Eric Christopher | 6e47204 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
Jonas Devlieghere | e699dfa | 2018-01-29 14:52:34 +0000 | [diff] [blame^] | 98 | void AppleAccelTableBase::emitHashes(AsmPrinter *Asm) { |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 99 | uint64_t PrevHash = std::numeric_limits<uint64_t>::max(); |
Jonas Devlieghere | e699dfa | 2018-01-29 14:52:34 +0000 | [diff] [blame^] | 100 | unsigned BucketIdx = 0; |
| 101 | for (auto &Bucket : Buckets) { |
| 102 | for (auto &Hash : Bucket) { |
| 103 | uint32_t HashValue = Hash->HashValue; |
Frederic Riss | 0e9a50f | 2015-03-10 00:46:31 +0000 | [diff] [blame] | 104 | if (PrevHash == HashValue) |
| 105 | continue; |
Jonas Devlieghere | e699dfa | 2018-01-29 14:52:34 +0000 | [diff] [blame^] | 106 | Asm->OutStreamer->AddComment("Hash in Bucket " + Twine(BucketIdx)); |
Frederic Riss | 0e9a50f | 2015-03-10 00:46:31 +0000 | [diff] [blame] | 107 | Asm->EmitInt32(HashValue); |
| 108 | PrevHash = HashValue; |
Eric Christopher | 48fef59 | 2012-12-20 21:58:40 +0000 | [diff] [blame] | 109 | } |
Jonas Devlieghere | e699dfa | 2018-01-29 14:52:34 +0000 | [diff] [blame^] | 110 | BucketIdx++; |
Eric Christopher | 6e47204 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | |
Jonas Devlieghere | e699dfa | 2018-01-29 14:52:34 +0000 | [diff] [blame^] | 114 | void AppleAccelTableBase::emitOffsets(AsmPrinter *Asm, |
| 115 | const MCSymbol *SecBegin) { |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 116 | uint64_t PrevHash = std::numeric_limits<uint64_t>::max(); |
Eric Christopher | 54ce295d | 2011-11-08 18:38:40 +0000 | [diff] [blame] | 117 | for (size_t i = 0, e = Buckets.size(); i < e; ++i) { |
Jonas Devlieghere | e699dfa | 2018-01-29 14:52:34 +0000 | [diff] [blame^] | 118 | for (auto HI = Buckets[i].begin(), HE = Buckets[i].end(); HI != HE; ++HI) { |
Frederic Riss | 0e9a50f | 2015-03-10 00:46:31 +0000 | [diff] [blame] | 119 | uint32_t HashValue = (*HI)->HashValue; |
| 120 | if (PrevHash == HashValue) |
| 121 | continue; |
| 122 | PrevHash = HashValue; |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 123 | Asm->OutStreamer->AddComment("Offset in Bucket " + Twine(i)); |
| 124 | MCContext &Context = Asm->OutStreamer->getContext(); |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 125 | const MCExpr *Sub = MCBinaryExpr::createSub( |
| 126 | MCSymbolRefExpr::create((*HI)->Sym, Context), |
| 127 | MCSymbolRefExpr::create(SecBegin, Context), Context); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 128 | Asm->OutStreamer->EmitValue(Sub, sizeof(uint32_t)); |
Eric Christopher | 6e47204 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
Jonas Devlieghere | e699dfa | 2018-01-29 14:52:34 +0000 | [diff] [blame^] | 133 | void AppleAccelTableBase::emitData(AsmPrinter *Asm) { |
Eric Christopher | 54ce295d | 2011-11-08 18:38:40 +0000 | [diff] [blame] | 134 | for (size_t i = 0, e = Buckets.size(); i < e; ++i) { |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 135 | uint64_t PrevHash = std::numeric_limits<uint64_t>::max(); |
Jonas Devlieghere | e699dfa | 2018-01-29 14:52:34 +0000 | [diff] [blame^] | 136 | for (auto &Hash : Buckets[i]) { |
| 137 | // Terminate the previous entry if there is no hash collision with the |
| 138 | // current one. |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 139 | if (PrevHash != std::numeric_limits<uint64_t>::max() && |
Jonas Devlieghere | e699dfa | 2018-01-29 14:52:34 +0000 | [diff] [blame^] | 140 | PrevHash != Hash->HashValue) |
Frederic Riss | 0e9a50f | 2015-03-10 00:46:31 +0000 | [diff] [blame] | 141 | Asm->EmitInt32(0); |
Eric Christopher | 6e47204 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 142 | // Remember to emit the label for our offset. |
Jonas Devlieghere | e699dfa | 2018-01-29 14:52:34 +0000 | [diff] [blame^] | 143 | Asm->OutStreamer->EmitLabel(Hash->Sym); |
| 144 | Asm->OutStreamer->AddComment(Hash->Str); |
| 145 | Asm->emitDwarfStringOffset(Hash->Data.Name); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 146 | Asm->OutStreamer->AddComment("Num DIEs"); |
Jonas Devlieghere | e699dfa | 2018-01-29 14:52:34 +0000 | [diff] [blame^] | 147 | Asm->EmitInt32(Hash->Data.Values.size()); |
| 148 | for (const auto *V : Hash->Data.Values) { |
| 149 | V->emit(Asm); |
Eric Christopher | 6e47204 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 150 | } |
Jonas Devlieghere | e699dfa | 2018-01-29 14:52:34 +0000 | [diff] [blame^] | 151 | PrevHash = Hash->HashValue; |
Eric Christopher | 6e47204 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 152 | } |
Frederic Riss | 0e9a50f | 2015-03-10 00:46:31 +0000 | [diff] [blame] | 153 | // Emit the final end marker for the bucket. |
Frederic Riss | 44a219f | 2015-03-10 03:47:55 +0000 | [diff] [blame] | 154 | if (!Buckets[i].empty()) |
| 155 | Asm->EmitInt32(0); |
Eric Christopher | 6e47204 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
Jonas Devlieghere | e699dfa | 2018-01-29 14:52:34 +0000 | [diff] [blame^] | 159 | void AppleAccelTableBase::computeBucketCount() { |
| 160 | // First get the number of unique hashes. |
| 161 | std::vector<uint32_t> uniques(Data.size()); |
| 162 | for (size_t i = 0, e = Data.size(); i < e; ++i) |
| 163 | uniques[i] = Data[i]->HashValue; |
| 164 | array_pod_sort(uniques.begin(), uniques.end()); |
| 165 | std::vector<uint32_t>::iterator p = |
| 166 | std::unique(uniques.begin(), uniques.end()); |
Eric Christopher | 6e47204 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 167 | |
Jonas Devlieghere | e699dfa | 2018-01-29 14:52:34 +0000 | [diff] [blame^] | 168 | // Compute the hashes count and use it to set that together with the bucket |
| 169 | // count in the header. |
| 170 | Header.setBucketAndHashCount(std::distance(uniques.begin(), p)); |
Eric Christopher | 6e47204 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Jonas Devlieghere | e699dfa | 2018-01-29 14:52:34 +0000 | [diff] [blame^] | 173 | void AppleAccelTableBase::finalizeTable(AsmPrinter *Asm, StringRef Prefix) { |
| 174 | // Create the individual hash data outputs. |
| 175 | Data.reserve(Entries.size()); |
| 176 | for (auto &E : Entries) { |
| 177 | // Unique the entries. |
| 178 | std::stable_sort(E.second.Values.begin(), E.second.Values.end(), |
| 179 | [](const AppleAccelTableData *A, |
| 180 | const AppleAccelTableData *B) { return *A < *B; }); |
| 181 | E.second.Values.erase( |
| 182 | std::unique(E.second.Values.begin(), E.second.Values.end()), |
| 183 | E.second.Values.end()); |
Eric Christopher | 6e47204 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 184 | |
Jonas Devlieghere | e699dfa | 2018-01-29 14:52:34 +0000 | [diff] [blame^] | 185 | HashData *Entry = new (Allocator) HashData(E.first(), E.second); |
| 186 | Data.push_back(Entry); |
Eric Christopher | 6e47204 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Jonas Devlieghere | e699dfa | 2018-01-29 14:52:34 +0000 | [diff] [blame^] | 189 | // Figure out how many buckets we need, then compute the bucket contents and |
| 190 | // the final ordering. We'll emit the hashes and offsets by doing a walk |
| 191 | // during the emission phase. We add temporary symbols to the data so that we |
| 192 | // can reference them during the offset later, we'll emit them when we emit |
| 193 | // the data. |
| 194 | computeBucketCount(); |
Eric Christopher | 6e47204 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 195 | |
Jonas Devlieghere | e699dfa | 2018-01-29 14:52:34 +0000 | [diff] [blame^] | 196 | // Compute bucket contents and final ordering. |
| 197 | Buckets.resize(Header.getBucketCount()); |
| 198 | for (auto &D : Data) { |
| 199 | uint32_t bucket = D->HashValue % Header.getBucketCount(); |
| 200 | Buckets[bucket].push_back(D); |
| 201 | D->Sym = Asm->createTempSymbol(Prefix); |
| 202 | } |
| 203 | |
| 204 | // Sort the contents of the buckets by hash value so that hash collisions end |
| 205 | // up together. Stable sort makes testing easier and doesn't cost much more. |
| 206 | for (auto &Bucket : Buckets) |
| 207 | std::stable_sort(Bucket.begin(), Bucket.end(), |
| 208 | [](HashData *LHS, HashData *RHS) { |
| 209 | return LHS->HashValue < RHS->HashValue; |
| 210 | }); |
Eric Christopher | 6e47204 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 211 | } |
Jonas Devlieghere | e699dfa | 2018-01-29 14:52:34 +0000 | [diff] [blame^] | 212 | |
| 213 | void AppleAccelTableOffsetData::emit(AsmPrinter *Asm) const { |
| 214 | Asm->EmitInt32(Die->getDebugSectionOffset()); |
| 215 | } |
| 216 | |
| 217 | void AppleAccelTableTypeData::emit(AsmPrinter *Asm) const { |
| 218 | Asm->EmitInt32(Die->getDebugSectionOffset()); |
| 219 | Asm->EmitInt16(Die->getTag()); |
| 220 | Asm->EmitInt8(0); |
| 221 | } |