blob: 8ffdc5b809185d825210bf3a95ddd90dd32aec4b [file] [log] [blame]
Justin Bognerf8d79192014-03-21 17:24:48 +00001//=-- InstrProfReader.cpp - Instrumented profiling reader -------------------=//
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 reading profiling data for clang's
11// instrumentation based PGO and coverage.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/ProfileData/InstrProfReader.h"
Benjamin Kramer0a446fd2015-03-01 21:28:53 +000016#include "llvm/ADT/STLExtras.h"
Justin Bognerf8d79192014-03-21 17:24:48 +000017#include <cassert>
18
19using namespace llvm;
20
Vedant Kumar9152fd12016-05-19 03:54:45 +000021static Expected<std::unique_ptr<MemoryBuffer>>
Diego Novillofcd55602014-11-03 00:51:45 +000022setupMemoryBuffer(std::string Path) {
Rafael Espindolaadf21f22014-07-06 17:43:13 +000023 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
24 MemoryBuffer::getFileOrSTDIN(Path);
25 if (std::error_code EC = BufferOrErr.getError())
Vedant Kumar9152fd12016-05-19 03:54:45 +000026 return errorCodeToError(EC);
Justin Bogner2b6c5372015-02-18 01:58:17 +000027 return std::move(BufferOrErr.get());
Justin Bognerb7aa2632014-04-18 21:48:40 +000028}
29
Vedant Kumar9152fd12016-05-19 03:54:45 +000030static Error initializeReader(InstrProfReader &Reader) {
Justin Bognerb7aa2632014-04-18 21:48:40 +000031 return Reader.readHeader();
32}
33
Vedant Kumar9152fd12016-05-19 03:54:45 +000034Expected<std::unique_ptr<InstrProfReader>>
Diego Novillofcd55602014-11-03 00:51:45 +000035InstrProfReader::create(std::string Path) {
Justin Bognerb7aa2632014-04-18 21:48:40 +000036 // Set up the buffer to read.
Diego Novillofcd55602014-11-03 00:51:45 +000037 auto BufferOrError = setupMemoryBuffer(Path);
Vedant Kumar9152fd12016-05-19 03:54:45 +000038 if (Error E = BufferOrError.takeError())
39 return std::move(E);
Justin Bogner2b6c5372015-02-18 01:58:17 +000040 return InstrProfReader::create(std::move(BufferOrError.get()));
41}
Justin Bognerf8d79192014-03-21 17:24:48 +000042
Vedant Kumar9152fd12016-05-19 03:54:45 +000043Expected<std::unique_ptr<InstrProfReader>>
Justin Bogner2b6c5372015-02-18 01:58:17 +000044InstrProfReader::create(std::unique_ptr<MemoryBuffer> Buffer) {
45 // Sanity check the buffer.
46 if (Buffer->getBufferSize() > std::numeric_limits<unsigned>::max())
Vedant Kumar9152fd12016-05-19 03:54:45 +000047 return make_error<InstrProfError>(instrprof_error::too_large);
Justin Bogner2b6c5372015-02-18 01:58:17 +000048
Diego Novillofcd55602014-11-03 00:51:45 +000049 std::unique_ptr<InstrProfReader> Result;
Duncan P. N. Exon Smith09a67f42014-03-21 20:42:31 +000050 // Create the reader.
Justin Bognerb7aa2632014-04-18 21:48:40 +000051 if (IndexedInstrProfReader::hasFormat(*Buffer))
52 Result.reset(new IndexedInstrProfReader(std::move(Buffer)));
53 else if (RawInstrProfReader64::hasFormat(*Buffer))
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +000054 Result.reset(new RawInstrProfReader64(std::move(Buffer)));
55 else if (RawInstrProfReader32::hasFormat(*Buffer))
56 Result.reset(new RawInstrProfReader32(std::move(Buffer)));
Nathan Slingerland4f823662015-11-13 03:47:58 +000057 else if (TextInstrProfReader::hasFormat(*Buffer))
Nathan Slingerland911ced62015-11-12 18:39:26 +000058 Result.reset(new TextInstrProfReader(std::move(Buffer)));
Nathan Slingerland4f823662015-11-13 03:47:58 +000059 else
Vedant Kumar9152fd12016-05-19 03:54:45 +000060 return make_error<InstrProfError>(instrprof_error::unrecognized_format);
Duncan P. N. Exon Smith09a67f42014-03-21 20:42:31 +000061
Justin Bognerb7aa2632014-04-18 21:48:40 +000062 // Initialize the reader and return the result.
Vedant Kumar9152fd12016-05-19 03:54:45 +000063 if (Error E = initializeReader(*Result))
64 return std::move(E);
Diego Novillofcd55602014-11-03 00:51:45 +000065
66 return std::move(Result);
Justin Bognerb7aa2632014-04-18 21:48:40 +000067}
68
Vedant Kumar9152fd12016-05-19 03:54:45 +000069Expected<std::unique_ptr<IndexedInstrProfReader>>
Justin Bognerab89ed72015-02-16 21:28:58 +000070IndexedInstrProfReader::create(std::string Path) {
Justin Bognerb7aa2632014-04-18 21:48:40 +000071 // Set up the buffer to read.
Diego Novillofcd55602014-11-03 00:51:45 +000072 auto BufferOrError = setupMemoryBuffer(Path);
Vedant Kumar9152fd12016-05-19 03:54:45 +000073 if (Error E = BufferOrError.takeError())
74 return std::move(E);
Justin Bogner2b6c5372015-02-18 01:58:17 +000075 return IndexedInstrProfReader::create(std::move(BufferOrError.get()));
76}
Justin Bognerb7aa2632014-04-18 21:48:40 +000077
Justin Bogner2b6c5372015-02-18 01:58:17 +000078
Vedant Kumar9152fd12016-05-19 03:54:45 +000079Expected<std::unique_ptr<IndexedInstrProfReader>>
Justin Bogner2b6c5372015-02-18 01:58:17 +000080IndexedInstrProfReader::create(std::unique_ptr<MemoryBuffer> Buffer) {
81 // Sanity check the buffer.
82 if (Buffer->getBufferSize() > std::numeric_limits<unsigned>::max())
Vedant Kumar9152fd12016-05-19 03:54:45 +000083 return make_error<InstrProfError>(instrprof_error::too_large);
Justin Bognerab89ed72015-02-16 21:28:58 +000084
Justin Bognerb7aa2632014-04-18 21:48:40 +000085 // Create the reader.
86 if (!IndexedInstrProfReader::hasFormat(*Buffer))
Vedant Kumar9152fd12016-05-19 03:54:45 +000087 return make_error<InstrProfError>(instrprof_error::bad_magic);
Justin Bogner2b6c5372015-02-18 01:58:17 +000088 auto Result = llvm::make_unique<IndexedInstrProfReader>(std::move(Buffer));
Justin Bognerb7aa2632014-04-18 21:48:40 +000089
90 // Initialize the reader and return the result.
Vedant Kumar9152fd12016-05-19 03:54:45 +000091 if (Error E = initializeReader(*Result))
92 return std::move(E);
Justin Bognerab89ed72015-02-16 21:28:58 +000093
94 return std::move(Result);
Justin Bognerf8d79192014-03-21 17:24:48 +000095}
96
97void InstrProfIterator::Increment() {
Vedant Kumar9152fd12016-05-19 03:54:45 +000098 if (auto E = Reader->readNextRecord(Record)) {
99 // Handle errors in the reader.
100 InstrProfError::take(std::move(E));
Justin Bognerf8d79192014-03-21 17:24:48 +0000101 *this = InstrProfIterator();
Vedant Kumar9152fd12016-05-19 03:54:45 +0000102 }
Justin Bognerf8d79192014-03-21 17:24:48 +0000103}
104
Nathan Slingerland4f823662015-11-13 03:47:58 +0000105bool TextInstrProfReader::hasFormat(const MemoryBuffer &Buffer) {
106 // Verify that this really looks like plain ASCII text by checking a
107 // 'reasonable' number of characters (up to profile magic size).
108 size_t count = std::min(Buffer.getBufferSize(), sizeof(uint64_t));
109 StringRef buffer = Buffer.getBufferStart();
Vedant Kumar2491dd12015-12-11 00:40:05 +0000110 return count == 0 ||
111 std::all_of(buffer.begin(), buffer.begin() + count,
112 [](char c) { return ::isprint(c) || ::isspace(c); });
Nathan Slingerland4f823662015-11-13 03:47:58 +0000113}
114
Rong Xu33c76c02016-02-10 17:18:30 +0000115// Read the profile variant flag from the header: ":FE" means this is a FE
116// generated profile. ":IR" means this is an IR level profile. Other strings
117// with a leading ':' will be reported an error format.
Vedant Kumar9152fd12016-05-19 03:54:45 +0000118Error TextInstrProfReader::readHeader() {
Xinliang David Lia716cc52015-12-20 06:22:13 +0000119 Symtab.reset(new InstrProfSymtab());
Rong Xu33c76c02016-02-10 17:18:30 +0000120 bool IsIRInstr = false;
121 if (!Line->startswith(":")) {
122 IsIRLevelProfile = false;
123 return success();
124 }
125 StringRef Str = (Line)->substr(1);
126 if (Str.equals_lower("ir"))
127 IsIRInstr = true;
128 else if (Str.equals_lower("fe"))
129 IsIRInstr = false;
130 else
Vedant Kumar9152fd12016-05-19 03:54:45 +0000131 return error(instrprof_error::bad_header);
Rong Xu33c76c02016-02-10 17:18:30 +0000132
133 ++Line;
134 IsIRLevelProfile = IsIRInstr;
Xinliang David Lia716cc52015-12-20 06:22:13 +0000135 return success();
136}
137
Vedant Kumar9152fd12016-05-19 03:54:45 +0000138Error
Xinliang David Lie3bf4fd32015-12-14 18:44:01 +0000139TextInstrProfReader::readValueProfileData(InstrProfRecord &Record) {
140
141#define CHECK_LINE_END(Line) \
142 if (Line.is_at_end()) \
143 return error(instrprof_error::truncated);
144#define READ_NUM(Str, Dst) \
145 if ((Str).getAsInteger(10, (Dst))) \
146 return error(instrprof_error::malformed);
147#define VP_READ_ADVANCE(Val) \
148 CHECK_LINE_END(Line); \
149 uint32_t Val; \
150 READ_NUM((*Line), (Val)); \
151 Line++;
152
153 if (Line.is_at_end())
154 return success();
Xinliang David Lia716cc52015-12-20 06:22:13 +0000155
Xinliang David Lie3bf4fd32015-12-14 18:44:01 +0000156 uint32_t NumValueKinds;
157 if (Line->getAsInteger(10, NumValueKinds)) {
158 // No value profile data
159 return success();
160 }
161 if (NumValueKinds == 0 || NumValueKinds > IPVK_Last + 1)
162 return error(instrprof_error::malformed);
163 Line++;
164
165 for (uint32_t VK = 0; VK < NumValueKinds; VK++) {
166 VP_READ_ADVANCE(ValueKind);
167 if (ValueKind > IPVK_Last)
168 return error(instrprof_error::malformed);
169 VP_READ_ADVANCE(NumValueSites);
170 if (!NumValueSites)
171 continue;
172
173 Record.reserveSites(VK, NumValueSites);
174 for (uint32_t S = 0; S < NumValueSites; S++) {
175 VP_READ_ADVANCE(NumValueData);
176
177 std::vector<InstrProfValueData> CurrentValues;
178 for (uint32_t V = 0; V < NumValueData; V++) {
179 CHECK_LINE_END(Line);
Rong Xu35723642016-05-06 23:20:58 +0000180 std::pair<StringRef, StringRef> VD = Line->rsplit(':');
Xinliang David Lie3bf4fd32015-12-14 18:44:01 +0000181 uint64_t TakenCount, Value;
Xinliang David Lia716cc52015-12-20 06:22:13 +0000182 if (VK == IPVK_IndirectCallTarget) {
183 Symtab->addFuncName(VD.first);
184 Value = IndexedInstrProf::ComputeHash(VD.first);
185 } else {
Xinliang David Lie3bf4fd32015-12-14 18:44:01 +0000186 READ_NUM(VD.first, Value);
187 }
Xinliang David Lia716cc52015-12-20 06:22:13 +0000188 READ_NUM(VD.second, TakenCount);
Xinliang David Lie3bf4fd32015-12-14 18:44:01 +0000189 CurrentValues.push_back({Value, TakenCount});
190 Line++;
191 }
192 Record.addValueData(VK, S, CurrentValues.data(), NumValueData, nullptr);
193 }
194 }
195 return success();
196
197#undef CHECK_LINE_END
198#undef READ_NUM
199#undef VP_READ_ADVANCE
200}
201
Vedant Kumar9152fd12016-05-19 03:54:45 +0000202Error TextInstrProfReader::readNextRecord(InstrProfRecord &Record) {
Justin Bognercf36a362014-07-29 22:29:23 +0000203 // Skip empty lines and comments.
204 while (!Line.is_at_end() && (Line->empty() || Line->startswith("#")))
Justin Bognerf8d79192014-03-21 17:24:48 +0000205 ++Line;
206 // If we hit EOF while looking for a name, we're done.
Xinliang David Lia716cc52015-12-20 06:22:13 +0000207 if (Line.is_at_end()) {
208 Symtab->finalizeSymtab();
Justin Bognerf8d79192014-03-21 17:24:48 +0000209 return error(instrprof_error::eof);
Xinliang David Lia716cc52015-12-20 06:22:13 +0000210 }
Justin Bognerf8d79192014-03-21 17:24:48 +0000211
212 // Read the function name.
213 Record.Name = *Line++;
Xinliang David Lia716cc52015-12-20 06:22:13 +0000214 Symtab->addFuncName(Record.Name);
Justin Bognerf8d79192014-03-21 17:24:48 +0000215
216 // Read the function hash.
217 if (Line.is_at_end())
218 return error(instrprof_error::truncated);
Justin Bognerf95ca072015-03-09 18:54:49 +0000219 if ((Line++)->getAsInteger(0, Record.Hash))
Justin Bognerf8d79192014-03-21 17:24:48 +0000220 return error(instrprof_error::malformed);
221
222 // Read the number of counters.
223 uint64_t NumCounters;
224 if (Line.is_at_end())
225 return error(instrprof_error::truncated);
226 if ((Line++)->getAsInteger(10, NumCounters))
227 return error(instrprof_error::malformed);
Justin Bognerb59d7c72014-04-25 02:45:33 +0000228 if (NumCounters == 0)
229 return error(instrprof_error::malformed);
Justin Bognerf8d79192014-03-21 17:24:48 +0000230
231 // Read each counter and fill our internal storage with the values.
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000232 Record.Counts.clear();
233 Record.Counts.reserve(NumCounters);
Justin Bognerf8d79192014-03-21 17:24:48 +0000234 for (uint64_t I = 0; I < NumCounters; ++I) {
235 if (Line.is_at_end())
236 return error(instrprof_error::truncated);
237 uint64_t Count;
238 if ((Line++)->getAsInteger(10, Count))
239 return error(instrprof_error::malformed);
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000240 Record.Counts.push_back(Count);
Justin Bognerf8d79192014-03-21 17:24:48 +0000241 }
Justin Bognerf8d79192014-03-21 17:24:48 +0000242
Xinliang David Lie3bf4fd32015-12-14 18:44:01 +0000243 // Check if value profile data exists and read it if so.
Vedant Kumar9152fd12016-05-19 03:54:45 +0000244 if (Error E = readValueProfileData(Record))
245 return E;
Xinliang David Lie3bf4fd32015-12-14 18:44:01 +0000246
Xinliang David Lia716cc52015-12-20 06:22:13 +0000247 // This is needed to avoid two pass parsing because llvm-profdata
248 // does dumping while reading.
249 Symtab->finalizeSymtab();
Justin Bognerf8d79192014-03-21 17:24:48 +0000250 return success();
251}
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000252
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000253template <class IntPtrT>
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000254bool RawInstrProfReader<IntPtrT>::hasFormat(const MemoryBuffer &DataBuffer) {
Duncan P. N. Exon Smithd7d83472014-03-24 00:47:18 +0000255 if (DataBuffer.getBufferSize() < sizeof(uint64_t))
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000256 return false;
Duncan P. N. Exon Smithd7d83472014-03-24 00:47:18 +0000257 uint64_t Magic =
258 *reinterpret_cast<const uint64_t *>(DataBuffer.getBufferStart());
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000259 return RawInstrProf::getMagic<IntPtrT>() == Magic ||
260 sys::getSwappedBytes(RawInstrProf::getMagic<IntPtrT>()) == Magic;
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000261}
262
263template <class IntPtrT>
Vedant Kumar9152fd12016-05-19 03:54:45 +0000264Error RawInstrProfReader<IntPtrT>::readHeader() {
Duncan P. N. Exon Smith09a67f42014-03-21 20:42:31 +0000265 if (!hasFormat(*DataBuffer))
266 return error(instrprof_error::bad_magic);
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000267 if (DataBuffer->getBufferSize() < sizeof(RawInstrProf::Header))
Duncan P. N. Exon Smith531bb482014-03-21 20:42:28 +0000268 return error(instrprof_error::bad_header);
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000269 auto *Header = reinterpret_cast<const RawInstrProf::Header *>(
270 DataBuffer->getBufferStart());
271 ShouldSwapBytes = Header->Magic != RawInstrProf::getMagic<IntPtrT>();
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000272 return readHeader(*Header);
273}
274
Justin Bognera119f322014-05-16 00:38:00 +0000275template <class IntPtrT>
Vedant Kumar9152fd12016-05-19 03:54:45 +0000276Error RawInstrProfReader<IntPtrT>::readNextHeader(const char *CurrentPos) {
Justin Bognera119f322014-05-16 00:38:00 +0000277 const char *End = DataBuffer->getBufferEnd();
278 // Skip zero padding between profiles.
279 while (CurrentPos != End && *CurrentPos == 0)
280 ++CurrentPos;
281 // If there's nothing left, we're done.
282 if (CurrentPos == End)
Vedant Kumar9152fd12016-05-19 03:54:45 +0000283 return make_error<InstrProfError>(instrprof_error::eof);
Justin Bognera119f322014-05-16 00:38:00 +0000284 // If there isn't enough space for another header, this is probably just
285 // garbage at the end of the file.
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000286 if (CurrentPos + sizeof(RawInstrProf::Header) > End)
Vedant Kumar9152fd12016-05-19 03:54:45 +0000287 return make_error<InstrProfError>(instrprof_error::malformed);
Justin Bogner54b11282014-09-12 21:22:55 +0000288 // The writer ensures each profile is padded to start at an aligned address.
289 if (reinterpret_cast<size_t>(CurrentPos) % alignOf<uint64_t>())
Vedant Kumar9152fd12016-05-19 03:54:45 +0000290 return make_error<InstrProfError>(instrprof_error::malformed);
Justin Bognera119f322014-05-16 00:38:00 +0000291 // The magic should have the same byte order as in the previous header.
292 uint64_t Magic = *reinterpret_cast<const uint64_t *>(CurrentPos);
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000293 if (Magic != swap(RawInstrProf::getMagic<IntPtrT>()))
Vedant Kumar9152fd12016-05-19 03:54:45 +0000294 return make_error<InstrProfError>(instrprof_error::bad_magic);
Justin Bognera119f322014-05-16 00:38:00 +0000295
296 // There's another profile to read, so we need to process the header.
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000297 auto *Header = reinterpret_cast<const RawInstrProf::Header *>(CurrentPos);
Justin Bognera119f322014-05-16 00:38:00 +0000298 return readHeader(*Header);
299}
300
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000301template <class IntPtrT>
Vedant Kumar9152fd12016-05-19 03:54:45 +0000302Error RawInstrProfReader<IntPtrT>::createSymtab(InstrProfSymtab &Symtab) {
303 if (Error E = Symtab.create(StringRef(NamesStart, NamesSize)))
304 return error(std::move(E));
Xinliang David Lia716cc52015-12-20 06:22:13 +0000305 for (const RawInstrProf::ProfileData<IntPtrT> *I = Data; I != DataEnd; ++I) {
Xinliang David Lia716cc52015-12-20 06:22:13 +0000306 const IntPtrT FPtr = swap(I->FunctionPointer);
307 if (!FPtr)
308 continue;
Xinliang David Lia82d6c02016-02-08 18:13:49 +0000309 Symtab.mapAddress(FPtr, I->NameRef);
Xinliang David Lia716cc52015-12-20 06:22:13 +0000310 }
311 Symtab.finalizeSymtab();
Vedant Kumare44482f2016-04-21 21:07:25 +0000312 return success();
Xinliang David Lia716cc52015-12-20 06:22:13 +0000313}
314
315template <class IntPtrT>
Vedant Kumar9152fd12016-05-19 03:54:45 +0000316Error RawInstrProfReader<IntPtrT>::readHeader(
317 const RawInstrProf::Header &Header) {
Rong Xu33c76c02016-02-10 17:18:30 +0000318 Version = swap(Header.Version);
319 if (GET_VERSION(Version) != RawInstrProf::Version)
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000320 return error(instrprof_error::unsupported_version);
321
322 CountersDelta = swap(Header.CountersDelta);
323 NamesDelta = swap(Header.NamesDelta);
324 auto DataSize = swap(Header.DataSize);
325 auto CountersSize = swap(Header.CountersSize);
Xinliang David Lia82d6c02016-02-08 18:13:49 +0000326 NamesSize = swap(Header.NamesSize);
Betul Buyukkurt6fac1742015-11-18 18:14:55 +0000327 ValueKindLast = swap(Header.ValueKindLast);
328
329 auto DataSizeInBytes = DataSize * sizeof(RawInstrProf::ProfileData<IntPtrT>);
330 auto PaddingSize = getNumPaddingBytes(NamesSize);
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000331
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000332 ptrdiff_t DataOffset = sizeof(RawInstrProf::Header);
Betul Buyukkurt6fac1742015-11-18 18:14:55 +0000333 ptrdiff_t CountersOffset = DataOffset + DataSizeInBytes;
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000334 ptrdiff_t NamesOffset = CountersOffset + sizeof(uint64_t) * CountersSize;
Betul Buyukkurt6fac1742015-11-18 18:14:55 +0000335 ptrdiff_t ValueDataOffset = NamesOffset + NamesSize + PaddingSize;
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000336
Justin Bognera119f322014-05-16 00:38:00 +0000337 auto *Start = reinterpret_cast<const char *>(&Header);
Xinliang David Li188a7c52016-05-05 19:41:18 +0000338 if (Start + ValueDataOffset > DataBuffer->getBufferEnd())
Duncan P. N. Exon Smith531bb482014-03-21 20:42:28 +0000339 return error(instrprof_error::bad_header);
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000340
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000341 Data = reinterpret_cast<const RawInstrProf::ProfileData<IntPtrT> *>(
342 Start + DataOffset);
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000343 DataEnd = Data + DataSize;
Duncan P. N. Exon Smithd7d83472014-03-24 00:47:18 +0000344 CountersStart = reinterpret_cast<const uint64_t *>(Start + CountersOffset);
345 NamesStart = Start + NamesOffset;
Xinliang David Lia716cc52015-12-20 06:22:13 +0000346 ValueDataStart = reinterpret_cast<const uint8_t *>(Start + ValueDataOffset);
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000347
Xinliang David Lia716cc52015-12-20 06:22:13 +0000348 std::unique_ptr<InstrProfSymtab> NewSymtab = make_unique<InstrProfSymtab>();
Vedant Kumar9152fd12016-05-19 03:54:45 +0000349 if (Error E = createSymtab(*NewSymtab.get()))
350 return E;
Vedant Kumare44482f2016-04-21 21:07:25 +0000351
Xinliang David Lia716cc52015-12-20 06:22:13 +0000352 Symtab = std::move(NewSymtab);
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000353 return success();
354}
355
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000356template <class IntPtrT>
Vedant Kumar9152fd12016-05-19 03:54:45 +0000357Error RawInstrProfReader<IntPtrT>::readName(InstrProfRecord &Record) {
Xinliang David Lia82d6c02016-02-08 18:13:49 +0000358 Record.Name = getName(Data->NameRef);
Xinliang David Licf4a1282015-10-28 19:34:04 +0000359 return success();
360}
361
362template <class IntPtrT>
Vedant Kumar9152fd12016-05-19 03:54:45 +0000363Error RawInstrProfReader<IntPtrT>::readFuncHash(InstrProfRecord &Record) {
Xinliang David Licf4a1282015-10-28 19:34:04 +0000364 Record.Hash = swap(Data->FuncHash);
365 return success();
366}
367
368template <class IntPtrT>
Vedant Kumar9152fd12016-05-19 03:54:45 +0000369Error RawInstrProfReader<IntPtrT>::readRawCounts(
Xinliang David Licf4a1282015-10-28 19:34:04 +0000370 InstrProfRecord &Record) {
Justin Bognerb59d7c72014-04-25 02:45:33 +0000371 uint32_t NumCounters = swap(Data->NumCounters);
Xinliang David Licf4a1282015-10-28 19:34:04 +0000372 IntPtrT CounterPtr = Data->CounterPtr;
Justin Bognerb59d7c72014-04-25 02:45:33 +0000373 if (NumCounters == 0)
374 return error(instrprof_error::malformed);
Xinliang David Licf4a1282015-10-28 19:34:04 +0000375
376 auto RawCounts = makeArrayRef(getCounter(CounterPtr), NumCounters);
377 auto *NamesStartAsCounter = reinterpret_cast<const uint64_t *>(NamesStart);
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000378
379 // Check bounds.
Xinliang David Licf4a1282015-10-28 19:34:04 +0000380 if (RawCounts.data() < CountersStart ||
Duncan P. N. Exon Smithd7d83472014-03-24 00:47:18 +0000381 RawCounts.data() + RawCounts.size() > NamesStartAsCounter)
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000382 return error(instrprof_error::malformed);
383
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000384 if (ShouldSwapBytes) {
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000385 Record.Counts.clear();
386 Record.Counts.reserve(RawCounts.size());
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000387 for (uint64_t Count : RawCounts)
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000388 Record.Counts.push_back(swap(Count));
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000389 } else
390 Record.Counts = RawCounts;
391
Xinliang David Licf4a1282015-10-28 19:34:04 +0000392 return success();
393}
394
395template <class IntPtrT>
Vedant Kumar9152fd12016-05-19 03:54:45 +0000396Error RawInstrProfReader<IntPtrT>::readValueProfilingData(
397 InstrProfRecord &Record) {
Betul Buyukkurt6fac1742015-11-18 18:14:55 +0000398
399 Record.clearValueData();
Xinliang David Lid922c262015-12-11 06:53:53 +0000400 CurValueDataSize = 0;
401 // Need to match the logic in value profile dumper code in compiler-rt:
402 uint32_t NumValueKinds = 0;
403 for (uint32_t I = 0; I < IPVK_Last + 1; I++)
404 NumValueKinds += (Data->NumValueSites[I] != 0);
405
406 if (!NumValueKinds)
Betul Buyukkurt6fac1742015-11-18 18:14:55 +0000407 return success();
408
Vedant Kumar9152fd12016-05-19 03:54:45 +0000409 Expected<std::unique_ptr<ValueProfData>> VDataPtrOrErr =
Xinliang David Li188a7c52016-05-05 19:41:18 +0000410 ValueProfData::getValueProfData(
411 ValueDataStart, (const unsigned char *)DataBuffer->getBufferEnd(),
412 getDataEndianness());
Betul Buyukkurt6fac1742015-11-18 18:14:55 +0000413
Vedant Kumar9152fd12016-05-19 03:54:45 +0000414 if (Error E = VDataPtrOrErr.takeError())
415 return E;
Betul Buyukkurt6fac1742015-11-18 18:14:55 +0000416
Adam Nemet2f36f052016-03-28 18:27:44 +0000417 // Note that besides deserialization, this also performs the conversion for
418 // indirect call targets. The function pointers from the raw profile are
419 // remapped into function name hashes.
Xinliang David Lia716cc52015-12-20 06:22:13 +0000420 VDataPtrOrErr.get()->deserializeTo(Record, &Symtab->getAddrHashMap());
Xinliang David Lid922c262015-12-11 06:53:53 +0000421 CurValueDataSize = VDataPtrOrErr.get()->getSize();
Betul Buyukkurt6fac1742015-11-18 18:14:55 +0000422 return success();
423}
424
425template <class IntPtrT>
Vedant Kumar9152fd12016-05-19 03:54:45 +0000426Error RawInstrProfReader<IntPtrT>::readNextRecord(InstrProfRecord &Record) {
Xinliang David Licf4a1282015-10-28 19:34:04 +0000427 if (atEnd())
Xinliang David Li188a7c52016-05-05 19:41:18 +0000428 // At this point, ValueDataStart field points to the next header.
Vedant Kumar9152fd12016-05-19 03:54:45 +0000429 if (Error E = readNextHeader(getNextHeaderPos()))
430 return E;
Xinliang David Licf4a1282015-10-28 19:34:04 +0000431
432 // Read name ad set it in Record.
Vedant Kumar9152fd12016-05-19 03:54:45 +0000433 if (Error E = readName(Record))
434 return E;
Xinliang David Licf4a1282015-10-28 19:34:04 +0000435
436 // Read FuncHash and set it in Record.
Vedant Kumar9152fd12016-05-19 03:54:45 +0000437 if (Error E = readFuncHash(Record))
438 return E;
Xinliang David Licf4a1282015-10-28 19:34:04 +0000439
440 // Read raw counts and set Record.
Vedant Kumar9152fd12016-05-19 03:54:45 +0000441 if (Error E = readRawCounts(Record))
442 return E;
Xinliang David Licf4a1282015-10-28 19:34:04 +0000443
Betul Buyukkurt6fac1742015-11-18 18:14:55 +0000444 // Read value data and set Record.
Vedant Kumar9152fd12016-05-19 03:54:45 +0000445 if (Error E = readValueProfilingData(Record))
446 return E;
Betul Buyukkurt6fac1742015-11-18 18:14:55 +0000447
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000448 // Iterate.
Xinliang David Licf4a1282015-10-28 19:34:04 +0000449 advanceData();
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000450 return success();
451}
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000452
453namespace llvm {
454template class RawInstrProfReader<uint32_t>;
455template class RawInstrProfReader<uint64_t>;
456}
Justin Bognerb7aa2632014-04-18 21:48:40 +0000457
Justin Bognerb5d368e2014-04-18 22:00:22 +0000458InstrProfLookupTrait::hash_value_type
459InstrProfLookupTrait::ComputeHash(StringRef K) {
460 return IndexedInstrProf::ComputeHash(HashType, K);
461}
462
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000463typedef InstrProfLookupTrait::data_type data_type;
464typedef InstrProfLookupTrait::offset_type offset_type;
465
Xinliang David Libe969c22015-11-28 05:06:00 +0000466bool InstrProfLookupTrait::readValueProfilingData(
Justin Bogner9e9a0572015-09-29 22:13:58 +0000467 const unsigned char *&D, const unsigned char *const End) {
Vedant Kumar9152fd12016-05-19 03:54:45 +0000468 Expected<std::unique_ptr<ValueProfData>> VDataPtrOrErr =
Xinliang David Li99556872015-11-17 23:00:40 +0000469 ValueProfData::getValueProfData(D, End, ValueProfDataEndianness);
Justin Bogner9e9a0572015-09-29 22:13:58 +0000470
Vedant Kumar9152fd12016-05-19 03:54:45 +0000471 if (VDataPtrOrErr.takeError())
Justin Bogner9e9a0572015-09-29 22:13:58 +0000472 return false;
Justin Bogner9e9a0572015-09-29 22:13:58 +0000473
Xinliang David Lia716cc52015-12-20 06:22:13 +0000474 VDataPtrOrErr.get()->deserializeTo(DataBuffer.back(), nullptr);
Xinliang David Liee415892015-11-10 00:24:45 +0000475 D += VDataPtrOrErr.get()->TotalSize;
Justin Bogner9e9a0572015-09-29 22:13:58 +0000476
Justin Bogner9e9a0572015-09-29 22:13:58 +0000477 return true;
478}
479
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000480data_type InstrProfLookupTrait::ReadData(StringRef K, const unsigned char *D,
481 offset_type N) {
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000482 // Check if the data is corrupt. If so, don't try to read it.
483 if (N % sizeof(uint64_t))
484 return data_type();
485
486 DataBuffer.clear();
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000487 std::vector<uint64_t> CounterBuffer;
Justin Bogner9e9a0572015-09-29 22:13:58 +0000488
489 using namespace support;
490 const unsigned char *End = D + N;
491 while (D < End) {
Xinliang David Lic7583872015-10-13 16:35:59 +0000492 // Read hash.
Justin Bogner9e9a0572015-09-29 22:13:58 +0000493 if (D + sizeof(uint64_t) >= End)
494 return data_type();
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000495 uint64_t Hash = endian::readNext<uint64_t, little, unaligned>(D);
496
Rong Xu33c76c02016-02-10 17:18:30 +0000497 // Initialize number of counters for GET_VERSION(FormatVersion) == 1.
Justin Bogner9e9a0572015-09-29 22:13:58 +0000498 uint64_t CountsSize = N / sizeof(uint64_t) - 1;
Xinliang David Lic7583872015-10-13 16:35:59 +0000499 // If format version is different then read the number of counters.
Rong Xu33c76c02016-02-10 17:18:30 +0000500 if (GET_VERSION(FormatVersion) != IndexedInstrProf::ProfVersion::Version1) {
Justin Bogner9e9a0572015-09-29 22:13:58 +0000501 if (D + sizeof(uint64_t) > End)
502 return data_type();
503 CountsSize = endian::readNext<uint64_t, little, unaligned>(D);
504 }
Xinliang David Lic7583872015-10-13 16:35:59 +0000505 // Read counter values.
Justin Bogner9e9a0572015-09-29 22:13:58 +0000506 if (D + CountsSize * sizeof(uint64_t) > End)
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000507 return data_type();
508
509 CounterBuffer.clear();
Justin Bogner9e9a0572015-09-29 22:13:58 +0000510 CounterBuffer.reserve(CountsSize);
511 for (uint64_t J = 0; J < CountsSize; ++J)
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000512 CounterBuffer.push_back(endian::readNext<uint64_t, little, unaligned>(D));
513
Xinliang David Li2004f002015-11-02 05:08:23 +0000514 DataBuffer.emplace_back(K, Hash, std::move(CounterBuffer));
Justin Bogner9e9a0572015-09-29 22:13:58 +0000515
Xinliang David Lic7583872015-10-13 16:35:59 +0000516 // Read value profiling data.
Rong Xu33c76c02016-02-10 17:18:30 +0000517 if (GET_VERSION(FormatVersion) > IndexedInstrProf::ProfVersion::Version2 &&
Xinliang David Lia6b2c4f2016-01-14 02:47:01 +0000518 !readValueProfilingData(D, End)) {
Justin Bogner9e9a0572015-09-29 22:13:58 +0000519 DataBuffer.clear();
520 return data_type();
521 }
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000522 }
523 return DataBuffer;
524}
525
Xinliang David Lia28306d2015-12-01 20:26:26 +0000526template <typename HashTableImpl>
Vedant Kumar9152fd12016-05-19 03:54:45 +0000527Error InstrProfReaderIndex<HashTableImpl>::getRecords(
Xinliang David Lia28306d2015-12-01 20:26:26 +0000528 StringRef FuncName, ArrayRef<InstrProfRecord> &Data) {
529 auto Iter = HashTable->find(FuncName);
530 if (Iter == HashTable->end())
Vedant Kumar9152fd12016-05-19 03:54:45 +0000531 return make_error<InstrProfError>(instrprof_error::unknown_function);
Xinliang David Li140f4c42015-10-28 04:20:31 +0000532
533 Data = (*Iter);
Xinliang David Li4c3ab812015-11-12 00:32:17 +0000534 if (Data.empty())
Vedant Kumar9152fd12016-05-19 03:54:45 +0000535 return make_error<InstrProfError>(instrprof_error::malformed);
Xinliang David Li140f4c42015-10-28 04:20:31 +0000536
Vedant Kumar9152fd12016-05-19 03:54:45 +0000537 return Error::success();
Xinliang David Li140f4c42015-10-28 04:20:31 +0000538}
539
Xinliang David Lia28306d2015-12-01 20:26:26 +0000540template <typename HashTableImpl>
Vedant Kumar9152fd12016-05-19 03:54:45 +0000541Error InstrProfReaderIndex<HashTableImpl>::getRecords(
Xinliang David Li140f4c42015-10-28 04:20:31 +0000542 ArrayRef<InstrProfRecord> &Data) {
Xinliang David Lia28306d2015-12-01 20:26:26 +0000543 if (atEnd())
Vedant Kumar9152fd12016-05-19 03:54:45 +0000544 return make_error<InstrProfError>(instrprof_error::eof);
Xinliang David Li140f4c42015-10-28 04:20:31 +0000545
546 Data = *RecordIterator;
547
Xinliang David Li2d4803e2015-12-10 23:48:05 +0000548 if (Data.empty())
Vedant Kumar9152fd12016-05-19 03:54:45 +0000549 return make_error<InstrProfError>(instrprof_error::malformed);
Xinliang David Li140f4c42015-10-28 04:20:31 +0000550
Vedant Kumar9152fd12016-05-19 03:54:45 +0000551 return Error::success();
Xinliang David Li140f4c42015-10-28 04:20:31 +0000552}
553
Xinliang David Lia28306d2015-12-01 20:26:26 +0000554template <typename HashTableImpl>
555InstrProfReaderIndex<HashTableImpl>::InstrProfReaderIndex(
556 const unsigned char *Buckets, const unsigned char *const Payload,
557 const unsigned char *const Base, IndexedInstrProf::HashT HashType,
558 uint64_t Version) {
Xinliang David Li140f4c42015-10-28 04:20:31 +0000559 FormatVersion = Version;
Xinliang David Lia28306d2015-12-01 20:26:26 +0000560 HashTable.reset(HashTableImpl::Create(
561 Buckets, Payload, Base,
562 typename HashTableImpl::InfoType(HashType, Version)));
Xinliang David Lia28306d2015-12-01 20:26:26 +0000563 RecordIterator = HashTable->data_begin();
Xinliang David Li140f4c42015-10-28 04:20:31 +0000564}
565
Justin Bognerb7aa2632014-04-18 21:48:40 +0000566bool IndexedInstrProfReader::hasFormat(const MemoryBuffer &DataBuffer) {
Xinliang David Li4c3ab812015-11-12 00:32:17 +0000567 if (DataBuffer.getBufferSize() < 8)
568 return false;
Justin Bognerb7aa2632014-04-18 21:48:40 +0000569 using namespace support;
570 uint64_t Magic =
571 endian::read<uint64_t, little, aligned>(DataBuffer.getBufferStart());
Xinliang David Lic7583872015-10-13 16:35:59 +0000572 // Verify that it's magical.
Justin Bognerb7aa2632014-04-18 21:48:40 +0000573 return Magic == IndexedInstrProf::Magic;
574}
575
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000576const unsigned char *
577IndexedInstrProfReader::readSummary(IndexedInstrProf::ProfVersion Version,
578 const unsigned char *Cur) {
Easwaran Ramane5a17e32016-05-19 21:07:12 +0000579 using namespace IndexedInstrProf;
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000580 using namespace support;
581 if (Version >= IndexedInstrProf::Version4) {
582 const IndexedInstrProf::Summary *SummaryInLE =
583 reinterpret_cast<const IndexedInstrProf::Summary *>(Cur);
584 uint64_t NFields =
585 endian::byte_swap<uint64_t, little>(SummaryInLE->NumSummaryFields);
586 uint64_t NEntries =
587 endian::byte_swap<uint64_t, little>(SummaryInLE->NumCutoffEntries);
588 uint32_t SummarySize =
589 IndexedInstrProf::Summary::getSize(NFields, NEntries);
590 std::unique_ptr<IndexedInstrProf::Summary> SummaryData =
591 IndexedInstrProf::allocSummary(SummarySize);
592
593 const uint64_t *Src = reinterpret_cast<const uint64_t *>(SummaryInLE);
594 uint64_t *Dst = reinterpret_cast<uint64_t *>(SummaryData.get());
595 for (unsigned I = 0; I < SummarySize / sizeof(uint64_t); I++)
596 Dst[I] = endian::byte_swap<uint64_t, little>(Src[I]);
597
Easwaran Ramane5a17e32016-05-19 21:07:12 +0000598 llvm::SummaryEntryVector DetailedSummary;
599 for (unsigned I = 0; I < SummaryData->NumCutoffEntries; I++) {
600 const IndexedInstrProf::Summary::Entry &Ent = SummaryData->getEntry(I);
601 DetailedSummary.emplace_back((uint32_t)Ent.Cutoff, Ent.MinBlockCount,
602 Ent.NumBlocks);
603 }
Easwaran Raman43095702016-02-17 18:18:47 +0000604 // initialize InstrProfSummary using the SummaryData from disk.
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000605 this->Summary = llvm::make_unique<ProfileSummary>(
606 ProfileSummary::PSK_Instr, DetailedSummary,
Easwaran Ramane5a17e32016-05-19 21:07:12 +0000607 SummaryData->get(Summary::TotalBlockCount),
608 SummaryData->get(Summary::MaxBlockCount),
609 SummaryData->get(Summary::MaxInternalBlockCount),
610 SummaryData->get(Summary::MaxFunctionCount),
611 SummaryData->get(Summary::TotalNumBlocks),
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000612 SummaryData->get(Summary::TotalNumFunctions));
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000613 return Cur + SummarySize;
614 } else {
615 // For older version of profile data, we need to compute on the fly:
616 using namespace IndexedInstrProf;
Easwaran Ramane5a17e32016-05-19 21:07:12 +0000617 InstrProfSummaryBuilder Builder(ProfileSummaryBuilder::DefaultCutoffs);
618 // FIXME: This only computes an empty summary. Need to call addRecord for
619 // all InstrProfRecords to get the correct summary.
620 this->Summary.reset(Builder.getSummary());
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000621 return Cur;
622 }
623}
624
Vedant Kumar9152fd12016-05-19 03:54:45 +0000625Error IndexedInstrProfReader::readHeader() {
Aaron Ballmana7c9ed52014-05-01 17:16:24 +0000626 const unsigned char *Start =
627 (const unsigned char *)DataBuffer->getBufferStart();
Justin Bognerb7aa2632014-04-18 21:48:40 +0000628 const unsigned char *Cur = Start;
Aaron Ballmana7c9ed52014-05-01 17:16:24 +0000629 if ((const unsigned char *)DataBuffer->getBufferEnd() - Cur < 24)
Justin Bognerb7aa2632014-04-18 21:48:40 +0000630 return error(instrprof_error::truncated);
631
632 using namespace support;
633
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000634 auto *Header = reinterpret_cast<const IndexedInstrProf::Header *>(Cur);
635 Cur += sizeof(IndexedInstrProf::Header);
636
Justin Bognerb7aa2632014-04-18 21:48:40 +0000637 // Check the magic number.
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000638 uint64_t Magic = endian::byte_swap<uint64_t, little>(Header->Magic);
Justin Bognerb7aa2632014-04-18 21:48:40 +0000639 if (Magic != IndexedInstrProf::Magic)
640 return error(instrprof_error::bad_magic);
641
642 // Read the version.
Xinliang David Li140f4c42015-10-28 04:20:31 +0000643 uint64_t FormatVersion = endian::byte_swap<uint64_t, little>(Header->Version);
Rong Xu33c76c02016-02-10 17:18:30 +0000644 if (GET_VERSION(FormatVersion) >
645 IndexedInstrProf::ProfVersion::CurrentVersion)
Justin Bognerb7aa2632014-04-18 21:48:40 +0000646 return error(instrprof_error::unsupported_version);
647
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000648 Cur = readSummary((IndexedInstrProf::ProfVersion)FormatVersion, Cur);
Justin Bognerb7aa2632014-04-18 21:48:40 +0000649
650 // Read the hash type and start offset.
651 IndexedInstrProf::HashT HashType = static_cast<IndexedInstrProf::HashT>(
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000652 endian::byte_swap<uint64_t, little>(Header->HashType));
Justin Bognerb7aa2632014-04-18 21:48:40 +0000653 if (HashType > IndexedInstrProf::HashT::Last)
654 return error(instrprof_error::unsupported_hash_type);
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000655
656 uint64_t HashOffset = endian::byte_swap<uint64_t, little>(Header->HashOffset);
Justin Bognerb7aa2632014-04-18 21:48:40 +0000657
658 // The rest of the file is an on disk hash table.
Xinliang David Lia28306d2015-12-01 20:26:26 +0000659 InstrProfReaderIndexBase *IndexPtr = nullptr;
660 IndexPtr = new InstrProfReaderIndex<OnDiskHashTableImplV3>(
661 Start + HashOffset, Cur, Start, HashType, FormatVersion);
662 Index.reset(IndexPtr);
Justin Bognerb7aa2632014-04-18 21:48:40 +0000663 return success();
664}
665
Xinliang David Lia716cc52015-12-20 06:22:13 +0000666InstrProfSymtab &IndexedInstrProfReader::getSymtab() {
667 if (Symtab.get())
668 return *Symtab.get();
669
670 std::unique_ptr<InstrProfSymtab> NewSymtab = make_unique<InstrProfSymtab>();
671 Index->populateSymtab(*NewSymtab.get());
672
673 Symtab = std::move(NewSymtab);
674 return *Symtab.get();
675}
676
Vedant Kumar9152fd12016-05-19 03:54:45 +0000677Expected<InstrProfRecord>
Xinliang David Li6aa216c2015-11-06 07:54:21 +0000678IndexedInstrProfReader::getInstrProfRecord(StringRef FuncName,
679 uint64_t FuncHash) {
Xinliang David Li140f4c42015-10-28 04:20:31 +0000680 ArrayRef<InstrProfRecord> Data;
Vedant Kumar9152fd12016-05-19 03:54:45 +0000681 Error Err = Index->getRecords(FuncName, Data);
682 if (Err)
683 return std::move(Err);
Justin Bogner821d7472014-08-01 22:50:07 +0000684 // Found it. Look for counters with the right hash.
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000685 for (unsigned I = 0, E = Data.size(); I < E; ++I) {
Justin Bogner821d7472014-08-01 22:50:07 +0000686 // Check for a match and fill the vector if there is one.
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000687 if (Data[I].Hash == FuncHash) {
Xinliang David Li2004f002015-11-02 05:08:23 +0000688 return std::move(Data[I]);
Justin Bogner821d7472014-08-01 22:50:07 +0000689 }
690 }
691 return error(instrprof_error::hash_mismatch);
Justin Bognerb7aa2632014-04-18 21:48:40 +0000692}
693
Vedant Kumar9152fd12016-05-19 03:54:45 +0000694Error IndexedInstrProfReader::getFunctionCounts(StringRef FuncName,
695 uint64_t FuncHash,
696 std::vector<uint64_t> &Counts) {
697 Expected<InstrProfRecord> Record = getInstrProfRecord(FuncName, FuncHash);
698 if (Error E = Record.takeError())
699 return error(std::move(E));
Xinliang David Li2004f002015-11-02 05:08:23 +0000700
701 Counts = Record.get().Counts;
702 return success();
703}
704
Vedant Kumar9152fd12016-05-19 03:54:45 +0000705Error IndexedInstrProfReader::readNextRecord(InstrProfRecord &Record) {
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000706 static unsigned RecordIndex = 0;
Xinliang David Li140f4c42015-10-28 04:20:31 +0000707
708 ArrayRef<InstrProfRecord> Data;
709
Vedant Kumar9152fd12016-05-19 03:54:45 +0000710 Error E = Index->getRecords(Data);
711 if (E)
712 return error(std::move(E));
Xinliang David Li140f4c42015-10-28 04:20:31 +0000713
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000714 Record = Data[RecordIndex++];
715 if (RecordIndex >= Data.size()) {
Xinliang David Lia28306d2015-12-01 20:26:26 +0000716 Index->advanceToNextKey();
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000717 RecordIndex = 0;
Justin Bogner821d7472014-08-01 22:50:07 +0000718 }
Justin Bognerb7aa2632014-04-18 21:48:40 +0000719 return success();
720}