blob: ed927a98604db6281ebd8327b647f5705d7d9bb9 [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>>
Benjamin Kramer0da23a22016-05-29 10:31:00 +000022setupMemoryBuffer(const Twine &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>>
Benjamin Kramer0da23a22016-05-29 10:31:00 +000035InstrProfReader::create(const Twine &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
Rong Xu2c684cf2016-10-19 22:51:17 +000049 if (Buffer->getBufferSize() == 0)
50 return make_error<InstrProfError>(instrprof_error::empty_raw_profile);
51
Diego Novillofcd55602014-11-03 00:51:45 +000052 std::unique_ptr<InstrProfReader> Result;
Duncan P. N. Exon Smith09a67f42014-03-21 20:42:31 +000053 // Create the reader.
Justin Bognerb7aa2632014-04-18 21:48:40 +000054 if (IndexedInstrProfReader::hasFormat(*Buffer))
55 Result.reset(new IndexedInstrProfReader(std::move(Buffer)));
56 else if (RawInstrProfReader64::hasFormat(*Buffer))
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +000057 Result.reset(new RawInstrProfReader64(std::move(Buffer)));
58 else if (RawInstrProfReader32::hasFormat(*Buffer))
59 Result.reset(new RawInstrProfReader32(std::move(Buffer)));
Nathan Slingerland4f823662015-11-13 03:47:58 +000060 else if (TextInstrProfReader::hasFormat(*Buffer))
Nathan Slingerland911ced62015-11-12 18:39:26 +000061 Result.reset(new TextInstrProfReader(std::move(Buffer)));
Nathan Slingerland4f823662015-11-13 03:47:58 +000062 else
Vedant Kumar9152fd12016-05-19 03:54:45 +000063 return make_error<InstrProfError>(instrprof_error::unrecognized_format);
Duncan P. N. Exon Smith09a67f42014-03-21 20:42:31 +000064
Justin Bognerb7aa2632014-04-18 21:48:40 +000065 // Initialize the reader and return the result.
Vedant Kumar9152fd12016-05-19 03:54:45 +000066 if (Error E = initializeReader(*Result))
67 return std::move(E);
Diego Novillofcd55602014-11-03 00:51:45 +000068
69 return std::move(Result);
Justin Bognerb7aa2632014-04-18 21:48:40 +000070}
71
Vedant Kumar9152fd12016-05-19 03:54:45 +000072Expected<std::unique_ptr<IndexedInstrProfReader>>
Benjamin Kramer0da23a22016-05-29 10:31:00 +000073IndexedInstrProfReader::create(const Twine &Path) {
Justin Bognerb7aa2632014-04-18 21:48:40 +000074 // Set up the buffer to read.
Diego Novillofcd55602014-11-03 00:51:45 +000075 auto BufferOrError = setupMemoryBuffer(Path);
Vedant Kumar9152fd12016-05-19 03:54:45 +000076 if (Error E = BufferOrError.takeError())
77 return std::move(E);
Justin Bogner2b6c5372015-02-18 01:58:17 +000078 return IndexedInstrProfReader::create(std::move(BufferOrError.get()));
79}
Justin Bognerb7aa2632014-04-18 21:48:40 +000080
Justin Bogner2b6c5372015-02-18 01:58:17 +000081
Vedant Kumar9152fd12016-05-19 03:54:45 +000082Expected<std::unique_ptr<IndexedInstrProfReader>>
Justin Bogner2b6c5372015-02-18 01:58:17 +000083IndexedInstrProfReader::create(std::unique_ptr<MemoryBuffer> Buffer) {
84 // Sanity check the buffer.
85 if (Buffer->getBufferSize() > std::numeric_limits<unsigned>::max())
Vedant Kumar9152fd12016-05-19 03:54:45 +000086 return make_error<InstrProfError>(instrprof_error::too_large);
Justin Bognerab89ed72015-02-16 21:28:58 +000087
Justin Bognerb7aa2632014-04-18 21:48:40 +000088 // Create the reader.
89 if (!IndexedInstrProfReader::hasFormat(*Buffer))
Vedant Kumar9152fd12016-05-19 03:54:45 +000090 return make_error<InstrProfError>(instrprof_error::bad_magic);
Justin Bogner2b6c5372015-02-18 01:58:17 +000091 auto Result = llvm::make_unique<IndexedInstrProfReader>(std::move(Buffer));
Justin Bognerb7aa2632014-04-18 21:48:40 +000092
93 // Initialize the reader and return the result.
Vedant Kumar9152fd12016-05-19 03:54:45 +000094 if (Error E = initializeReader(*Result))
95 return std::move(E);
Justin Bognerab89ed72015-02-16 21:28:58 +000096
97 return std::move(Result);
Justin Bognerf8d79192014-03-21 17:24:48 +000098}
99
100void InstrProfIterator::Increment() {
Vedant Kumar9152fd12016-05-19 03:54:45 +0000101 if (auto E = Reader->readNextRecord(Record)) {
102 // Handle errors in the reader.
103 InstrProfError::take(std::move(E));
Justin Bognerf8d79192014-03-21 17:24:48 +0000104 *this = InstrProfIterator();
Vedant Kumar9152fd12016-05-19 03:54:45 +0000105 }
Justin Bognerf8d79192014-03-21 17:24:48 +0000106}
107
Nathan Slingerland4f823662015-11-13 03:47:58 +0000108bool TextInstrProfReader::hasFormat(const MemoryBuffer &Buffer) {
109 // Verify that this really looks like plain ASCII text by checking a
110 // 'reasonable' number of characters (up to profile magic size).
111 size_t count = std::min(Buffer.getBufferSize(), sizeof(uint64_t));
112 StringRef buffer = Buffer.getBufferStart();
Vedant Kumar2491dd12015-12-11 00:40:05 +0000113 return count == 0 ||
114 std::all_of(buffer.begin(), buffer.begin() + count,
115 [](char c) { return ::isprint(c) || ::isspace(c); });
Nathan Slingerland4f823662015-11-13 03:47:58 +0000116}
117
Rong Xu33c76c02016-02-10 17:18:30 +0000118// Read the profile variant flag from the header: ":FE" means this is a FE
119// generated profile. ":IR" means this is an IR level profile. Other strings
120// with a leading ':' will be reported an error format.
Vedant Kumar9152fd12016-05-19 03:54:45 +0000121Error TextInstrProfReader::readHeader() {
Xinliang David Lia716cc52015-12-20 06:22:13 +0000122 Symtab.reset(new InstrProfSymtab());
Rong Xu33c76c02016-02-10 17:18:30 +0000123 bool IsIRInstr = false;
124 if (!Line->startswith(":")) {
125 IsIRLevelProfile = false;
126 return success();
127 }
128 StringRef Str = (Line)->substr(1);
129 if (Str.equals_lower("ir"))
130 IsIRInstr = true;
131 else if (Str.equals_lower("fe"))
132 IsIRInstr = false;
133 else
Vedant Kumar9152fd12016-05-19 03:54:45 +0000134 return error(instrprof_error::bad_header);
Rong Xu33c76c02016-02-10 17:18:30 +0000135
136 ++Line;
137 IsIRLevelProfile = IsIRInstr;
Xinliang David Lia716cc52015-12-20 06:22:13 +0000138 return success();
139}
140
Vedant Kumar9152fd12016-05-19 03:54:45 +0000141Error
Xinliang David Lie3bf4fd32015-12-14 18:44:01 +0000142TextInstrProfReader::readValueProfileData(InstrProfRecord &Record) {
143
144#define CHECK_LINE_END(Line) \
145 if (Line.is_at_end()) \
146 return error(instrprof_error::truncated);
147#define READ_NUM(Str, Dst) \
148 if ((Str).getAsInteger(10, (Dst))) \
149 return error(instrprof_error::malformed);
150#define VP_READ_ADVANCE(Val) \
151 CHECK_LINE_END(Line); \
152 uint32_t Val; \
153 READ_NUM((*Line), (Val)); \
154 Line++;
155
156 if (Line.is_at_end())
157 return success();
Xinliang David Lia716cc52015-12-20 06:22:13 +0000158
Xinliang David Lie3bf4fd32015-12-14 18:44:01 +0000159 uint32_t NumValueKinds;
160 if (Line->getAsInteger(10, NumValueKinds)) {
161 // No value profile data
162 return success();
163 }
164 if (NumValueKinds == 0 || NumValueKinds > IPVK_Last + 1)
165 return error(instrprof_error::malformed);
166 Line++;
167
168 for (uint32_t VK = 0; VK < NumValueKinds; VK++) {
169 VP_READ_ADVANCE(ValueKind);
170 if (ValueKind > IPVK_Last)
171 return error(instrprof_error::malformed);
172 VP_READ_ADVANCE(NumValueSites);
173 if (!NumValueSites)
174 continue;
175
176 Record.reserveSites(VK, NumValueSites);
177 for (uint32_t S = 0; S < NumValueSites; S++) {
178 VP_READ_ADVANCE(NumValueData);
179
180 std::vector<InstrProfValueData> CurrentValues;
181 for (uint32_t V = 0; V < NumValueData; V++) {
182 CHECK_LINE_END(Line);
Rong Xu35723642016-05-06 23:20:58 +0000183 std::pair<StringRef, StringRef> VD = Line->rsplit(':');
Xinliang David Lie3bf4fd32015-12-14 18:44:01 +0000184 uint64_t TakenCount, Value;
Xinliang David Lia716cc52015-12-20 06:22:13 +0000185 if (VK == IPVK_IndirectCallTarget) {
186 Symtab->addFuncName(VD.first);
187 Value = IndexedInstrProf::ComputeHash(VD.first);
188 } else {
Xinliang David Lie3bf4fd32015-12-14 18:44:01 +0000189 READ_NUM(VD.first, Value);
190 }
Xinliang David Lia716cc52015-12-20 06:22:13 +0000191 READ_NUM(VD.second, TakenCount);
Xinliang David Lie3bf4fd32015-12-14 18:44:01 +0000192 CurrentValues.push_back({Value, TakenCount});
193 Line++;
194 }
195 Record.addValueData(VK, S, CurrentValues.data(), NumValueData, nullptr);
196 }
197 }
198 return success();
199
200#undef CHECK_LINE_END
201#undef READ_NUM
202#undef VP_READ_ADVANCE
203}
204
Vedant Kumar9152fd12016-05-19 03:54:45 +0000205Error TextInstrProfReader::readNextRecord(InstrProfRecord &Record) {
Justin Bognercf36a362014-07-29 22:29:23 +0000206 // Skip empty lines and comments.
207 while (!Line.is_at_end() && (Line->empty() || Line->startswith("#")))
Justin Bognerf8d79192014-03-21 17:24:48 +0000208 ++Line;
209 // If we hit EOF while looking for a name, we're done.
Xinliang David Lia716cc52015-12-20 06:22:13 +0000210 if (Line.is_at_end()) {
211 Symtab->finalizeSymtab();
Justin Bognerf8d79192014-03-21 17:24:48 +0000212 return error(instrprof_error::eof);
Xinliang David Lia716cc52015-12-20 06:22:13 +0000213 }
Justin Bognerf8d79192014-03-21 17:24:48 +0000214
215 // Read the function name.
216 Record.Name = *Line++;
Xinliang David Lia716cc52015-12-20 06:22:13 +0000217 Symtab->addFuncName(Record.Name);
Justin Bognerf8d79192014-03-21 17:24:48 +0000218
219 // Read the function hash.
220 if (Line.is_at_end())
221 return error(instrprof_error::truncated);
Justin Bognerf95ca072015-03-09 18:54:49 +0000222 if ((Line++)->getAsInteger(0, Record.Hash))
Justin Bognerf8d79192014-03-21 17:24:48 +0000223 return error(instrprof_error::malformed);
224
225 // Read the number of counters.
226 uint64_t NumCounters;
227 if (Line.is_at_end())
228 return error(instrprof_error::truncated);
229 if ((Line++)->getAsInteger(10, NumCounters))
230 return error(instrprof_error::malformed);
Justin Bognerb59d7c72014-04-25 02:45:33 +0000231 if (NumCounters == 0)
232 return error(instrprof_error::malformed);
Justin Bognerf8d79192014-03-21 17:24:48 +0000233
234 // Read each counter and fill our internal storage with the values.
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000235 Record.Counts.clear();
236 Record.Counts.reserve(NumCounters);
Justin Bognerf8d79192014-03-21 17:24:48 +0000237 for (uint64_t I = 0; I < NumCounters; ++I) {
238 if (Line.is_at_end())
239 return error(instrprof_error::truncated);
240 uint64_t Count;
241 if ((Line++)->getAsInteger(10, Count))
242 return error(instrprof_error::malformed);
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000243 Record.Counts.push_back(Count);
Justin Bognerf8d79192014-03-21 17:24:48 +0000244 }
Justin Bognerf8d79192014-03-21 17:24:48 +0000245
Xinliang David Lie3bf4fd32015-12-14 18:44:01 +0000246 // Check if value profile data exists and read it if so.
Vedant Kumar9152fd12016-05-19 03:54:45 +0000247 if (Error E = readValueProfileData(Record))
248 return E;
Xinliang David Lie3bf4fd32015-12-14 18:44:01 +0000249
Xinliang David Lia716cc52015-12-20 06:22:13 +0000250 // This is needed to avoid two pass parsing because llvm-profdata
251 // does dumping while reading.
252 Symtab->finalizeSymtab();
Justin Bognerf8d79192014-03-21 17:24:48 +0000253 return success();
254}
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000255
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000256template <class IntPtrT>
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000257bool RawInstrProfReader<IntPtrT>::hasFormat(const MemoryBuffer &DataBuffer) {
Duncan P. N. Exon Smithd7d83472014-03-24 00:47:18 +0000258 if (DataBuffer.getBufferSize() < sizeof(uint64_t))
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000259 return false;
Duncan P. N. Exon Smithd7d83472014-03-24 00:47:18 +0000260 uint64_t Magic =
261 *reinterpret_cast<const uint64_t *>(DataBuffer.getBufferStart());
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000262 return RawInstrProf::getMagic<IntPtrT>() == Magic ||
263 sys::getSwappedBytes(RawInstrProf::getMagic<IntPtrT>()) == Magic;
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000264}
265
266template <class IntPtrT>
Vedant Kumar9152fd12016-05-19 03:54:45 +0000267Error RawInstrProfReader<IntPtrT>::readHeader() {
Duncan P. N. Exon Smith09a67f42014-03-21 20:42:31 +0000268 if (!hasFormat(*DataBuffer))
269 return error(instrprof_error::bad_magic);
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000270 if (DataBuffer->getBufferSize() < sizeof(RawInstrProf::Header))
Duncan P. N. Exon Smith531bb482014-03-21 20:42:28 +0000271 return error(instrprof_error::bad_header);
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000272 auto *Header = reinterpret_cast<const RawInstrProf::Header *>(
273 DataBuffer->getBufferStart());
274 ShouldSwapBytes = Header->Magic != RawInstrProf::getMagic<IntPtrT>();
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000275 return readHeader(*Header);
276}
277
Justin Bognera119f322014-05-16 00:38:00 +0000278template <class IntPtrT>
Vedant Kumar9152fd12016-05-19 03:54:45 +0000279Error RawInstrProfReader<IntPtrT>::readNextHeader(const char *CurrentPos) {
Justin Bognera119f322014-05-16 00:38:00 +0000280 const char *End = DataBuffer->getBufferEnd();
281 // Skip zero padding between profiles.
282 while (CurrentPos != End && *CurrentPos == 0)
283 ++CurrentPos;
284 // If there's nothing left, we're done.
285 if (CurrentPos == End)
Vedant Kumar9152fd12016-05-19 03:54:45 +0000286 return make_error<InstrProfError>(instrprof_error::eof);
Justin Bognera119f322014-05-16 00:38:00 +0000287 // If there isn't enough space for another header, this is probably just
288 // garbage at the end of the file.
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000289 if (CurrentPos + sizeof(RawInstrProf::Header) > End)
Vedant Kumar9152fd12016-05-19 03:54:45 +0000290 return make_error<InstrProfError>(instrprof_error::malformed);
Justin Bogner54b11282014-09-12 21:22:55 +0000291 // The writer ensures each profile is padded to start at an aligned address.
292 if (reinterpret_cast<size_t>(CurrentPos) % alignOf<uint64_t>())
Vedant Kumar9152fd12016-05-19 03:54:45 +0000293 return make_error<InstrProfError>(instrprof_error::malformed);
Justin Bognera119f322014-05-16 00:38:00 +0000294 // The magic should have the same byte order as in the previous header.
295 uint64_t Magic = *reinterpret_cast<const uint64_t *>(CurrentPos);
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000296 if (Magic != swap(RawInstrProf::getMagic<IntPtrT>()))
Vedant Kumar9152fd12016-05-19 03:54:45 +0000297 return make_error<InstrProfError>(instrprof_error::bad_magic);
Justin Bognera119f322014-05-16 00:38:00 +0000298
299 // There's another profile to read, so we need to process the header.
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000300 auto *Header = reinterpret_cast<const RawInstrProf::Header *>(CurrentPos);
Justin Bognera119f322014-05-16 00:38:00 +0000301 return readHeader(*Header);
302}
303
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000304template <class IntPtrT>
Vedant Kumar9152fd12016-05-19 03:54:45 +0000305Error RawInstrProfReader<IntPtrT>::createSymtab(InstrProfSymtab &Symtab) {
306 if (Error E = Symtab.create(StringRef(NamesStart, NamesSize)))
307 return error(std::move(E));
Xinliang David Lia716cc52015-12-20 06:22:13 +0000308 for (const RawInstrProf::ProfileData<IntPtrT> *I = Data; I != DataEnd; ++I) {
Xinliang David Lia716cc52015-12-20 06:22:13 +0000309 const IntPtrT FPtr = swap(I->FunctionPointer);
310 if (!FPtr)
311 continue;
Xinliang David Lia82d6c02016-02-08 18:13:49 +0000312 Symtab.mapAddress(FPtr, I->NameRef);
Xinliang David Lia716cc52015-12-20 06:22:13 +0000313 }
314 Symtab.finalizeSymtab();
Vedant Kumare44482f2016-04-21 21:07:25 +0000315 return success();
Xinliang David Lia716cc52015-12-20 06:22:13 +0000316}
317
318template <class IntPtrT>
Vedant Kumar9152fd12016-05-19 03:54:45 +0000319Error RawInstrProfReader<IntPtrT>::readHeader(
320 const RawInstrProf::Header &Header) {
Rong Xu33c76c02016-02-10 17:18:30 +0000321 Version = swap(Header.Version);
322 if (GET_VERSION(Version) != RawInstrProf::Version)
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000323 return error(instrprof_error::unsupported_version);
324
325 CountersDelta = swap(Header.CountersDelta);
326 NamesDelta = swap(Header.NamesDelta);
327 auto DataSize = swap(Header.DataSize);
328 auto CountersSize = swap(Header.CountersSize);
Xinliang David Lia82d6c02016-02-08 18:13:49 +0000329 NamesSize = swap(Header.NamesSize);
Betul Buyukkurt6fac1742015-11-18 18:14:55 +0000330 ValueKindLast = swap(Header.ValueKindLast);
331
332 auto DataSizeInBytes = DataSize * sizeof(RawInstrProf::ProfileData<IntPtrT>);
333 auto PaddingSize = getNumPaddingBytes(NamesSize);
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000334
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000335 ptrdiff_t DataOffset = sizeof(RawInstrProf::Header);
Betul Buyukkurt6fac1742015-11-18 18:14:55 +0000336 ptrdiff_t CountersOffset = DataOffset + DataSizeInBytes;
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000337 ptrdiff_t NamesOffset = CountersOffset + sizeof(uint64_t) * CountersSize;
Betul Buyukkurt6fac1742015-11-18 18:14:55 +0000338 ptrdiff_t ValueDataOffset = NamesOffset + NamesSize + PaddingSize;
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000339
Justin Bognera119f322014-05-16 00:38:00 +0000340 auto *Start = reinterpret_cast<const char *>(&Header);
Xinliang David Li188a7c52016-05-05 19:41:18 +0000341 if (Start + ValueDataOffset > DataBuffer->getBufferEnd())
Duncan P. N. Exon Smith531bb482014-03-21 20:42:28 +0000342 return error(instrprof_error::bad_header);
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000343
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000344 Data = reinterpret_cast<const RawInstrProf::ProfileData<IntPtrT> *>(
345 Start + DataOffset);
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000346 DataEnd = Data + DataSize;
Duncan P. N. Exon Smithd7d83472014-03-24 00:47:18 +0000347 CountersStart = reinterpret_cast<const uint64_t *>(Start + CountersOffset);
348 NamesStart = Start + NamesOffset;
Xinliang David Lia716cc52015-12-20 06:22:13 +0000349 ValueDataStart = reinterpret_cast<const uint8_t *>(Start + ValueDataOffset);
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000350
Xinliang David Lia716cc52015-12-20 06:22:13 +0000351 std::unique_ptr<InstrProfSymtab> NewSymtab = make_unique<InstrProfSymtab>();
Vedant Kumar9152fd12016-05-19 03:54:45 +0000352 if (Error E = createSymtab(*NewSymtab.get()))
353 return E;
Vedant Kumare44482f2016-04-21 21:07:25 +0000354
Xinliang David Lia716cc52015-12-20 06:22:13 +0000355 Symtab = std::move(NewSymtab);
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000356 return success();
357}
358
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000359template <class IntPtrT>
Vedant Kumar9152fd12016-05-19 03:54:45 +0000360Error RawInstrProfReader<IntPtrT>::readName(InstrProfRecord &Record) {
Xinliang David Lia82d6c02016-02-08 18:13:49 +0000361 Record.Name = getName(Data->NameRef);
Xinliang David Licf4a1282015-10-28 19:34:04 +0000362 return success();
363}
364
365template <class IntPtrT>
Vedant Kumar9152fd12016-05-19 03:54:45 +0000366Error RawInstrProfReader<IntPtrT>::readFuncHash(InstrProfRecord &Record) {
Xinliang David Licf4a1282015-10-28 19:34:04 +0000367 Record.Hash = swap(Data->FuncHash);
368 return success();
369}
370
371template <class IntPtrT>
Vedant Kumar9152fd12016-05-19 03:54:45 +0000372Error RawInstrProfReader<IntPtrT>::readRawCounts(
Xinliang David Licf4a1282015-10-28 19:34:04 +0000373 InstrProfRecord &Record) {
Justin Bognerb59d7c72014-04-25 02:45:33 +0000374 uint32_t NumCounters = swap(Data->NumCounters);
Xinliang David Licf4a1282015-10-28 19:34:04 +0000375 IntPtrT CounterPtr = Data->CounterPtr;
Justin Bognerb59d7c72014-04-25 02:45:33 +0000376 if (NumCounters == 0)
377 return error(instrprof_error::malformed);
Xinliang David Licf4a1282015-10-28 19:34:04 +0000378
379 auto RawCounts = makeArrayRef(getCounter(CounterPtr), NumCounters);
380 auto *NamesStartAsCounter = reinterpret_cast<const uint64_t *>(NamesStart);
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000381
382 // Check bounds.
Xinliang David Licf4a1282015-10-28 19:34:04 +0000383 if (RawCounts.data() < CountersStart ||
Duncan P. N. Exon Smithd7d83472014-03-24 00:47:18 +0000384 RawCounts.data() + RawCounts.size() > NamesStartAsCounter)
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000385 return error(instrprof_error::malformed);
386
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000387 if (ShouldSwapBytes) {
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000388 Record.Counts.clear();
389 Record.Counts.reserve(RawCounts.size());
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000390 for (uint64_t Count : RawCounts)
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000391 Record.Counts.push_back(swap(Count));
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000392 } else
393 Record.Counts = RawCounts;
394
Xinliang David Licf4a1282015-10-28 19:34:04 +0000395 return success();
396}
397
398template <class IntPtrT>
Vedant Kumar9152fd12016-05-19 03:54:45 +0000399Error RawInstrProfReader<IntPtrT>::readValueProfilingData(
400 InstrProfRecord &Record) {
Betul Buyukkurt6fac1742015-11-18 18:14:55 +0000401
402 Record.clearValueData();
Xinliang David Lid922c262015-12-11 06:53:53 +0000403 CurValueDataSize = 0;
404 // Need to match the logic in value profile dumper code in compiler-rt:
405 uint32_t NumValueKinds = 0;
406 for (uint32_t I = 0; I < IPVK_Last + 1; I++)
407 NumValueKinds += (Data->NumValueSites[I] != 0);
408
409 if (!NumValueKinds)
Betul Buyukkurt6fac1742015-11-18 18:14:55 +0000410 return success();
411
Vedant Kumar9152fd12016-05-19 03:54:45 +0000412 Expected<std::unique_ptr<ValueProfData>> VDataPtrOrErr =
Xinliang David Li188a7c52016-05-05 19:41:18 +0000413 ValueProfData::getValueProfData(
414 ValueDataStart, (const unsigned char *)DataBuffer->getBufferEnd(),
415 getDataEndianness());
Betul Buyukkurt6fac1742015-11-18 18:14:55 +0000416
Vedant Kumar9152fd12016-05-19 03:54:45 +0000417 if (Error E = VDataPtrOrErr.takeError())
418 return E;
Betul Buyukkurt6fac1742015-11-18 18:14:55 +0000419
Adam Nemet2f36f052016-03-28 18:27:44 +0000420 // Note that besides deserialization, this also performs the conversion for
421 // indirect call targets. The function pointers from the raw profile are
422 // remapped into function name hashes.
Xinliang David Lia716cc52015-12-20 06:22:13 +0000423 VDataPtrOrErr.get()->deserializeTo(Record, &Symtab->getAddrHashMap());
Xinliang David Lid922c262015-12-11 06:53:53 +0000424 CurValueDataSize = VDataPtrOrErr.get()->getSize();
Betul Buyukkurt6fac1742015-11-18 18:14:55 +0000425 return success();
426}
427
428template <class IntPtrT>
Vedant Kumar9152fd12016-05-19 03:54:45 +0000429Error RawInstrProfReader<IntPtrT>::readNextRecord(InstrProfRecord &Record) {
Xinliang David Licf4a1282015-10-28 19:34:04 +0000430 if (atEnd())
Xinliang David Li188a7c52016-05-05 19:41:18 +0000431 // At this point, ValueDataStart field points to the next header.
Vedant Kumar9152fd12016-05-19 03:54:45 +0000432 if (Error E = readNextHeader(getNextHeaderPos()))
433 return E;
Xinliang David Licf4a1282015-10-28 19:34:04 +0000434
435 // Read name ad set it in Record.
Vedant Kumar9152fd12016-05-19 03:54:45 +0000436 if (Error E = readName(Record))
437 return E;
Xinliang David Licf4a1282015-10-28 19:34:04 +0000438
439 // Read FuncHash and set it in Record.
Vedant Kumar9152fd12016-05-19 03:54:45 +0000440 if (Error E = readFuncHash(Record))
441 return E;
Xinliang David Licf4a1282015-10-28 19:34:04 +0000442
443 // Read raw counts and set Record.
Vedant Kumar9152fd12016-05-19 03:54:45 +0000444 if (Error E = readRawCounts(Record))
445 return E;
Xinliang David Licf4a1282015-10-28 19:34:04 +0000446
Betul Buyukkurt6fac1742015-11-18 18:14:55 +0000447 // Read value data and set Record.
Vedant Kumar9152fd12016-05-19 03:54:45 +0000448 if (Error E = readValueProfilingData(Record))
449 return E;
Betul Buyukkurt6fac1742015-11-18 18:14:55 +0000450
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000451 // Iterate.
Xinliang David Licf4a1282015-10-28 19:34:04 +0000452 advanceData();
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000453 return success();
454}
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000455
456namespace llvm {
457template class RawInstrProfReader<uint32_t>;
458template class RawInstrProfReader<uint64_t>;
459}
Justin Bognerb7aa2632014-04-18 21:48:40 +0000460
Justin Bognerb5d368e2014-04-18 22:00:22 +0000461InstrProfLookupTrait::hash_value_type
462InstrProfLookupTrait::ComputeHash(StringRef K) {
463 return IndexedInstrProf::ComputeHash(HashType, K);
464}
465
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000466typedef InstrProfLookupTrait::data_type data_type;
467typedef InstrProfLookupTrait::offset_type offset_type;
468
Xinliang David Libe969c22015-11-28 05:06:00 +0000469bool InstrProfLookupTrait::readValueProfilingData(
Justin Bogner9e9a0572015-09-29 22:13:58 +0000470 const unsigned char *&D, const unsigned char *const End) {
Vedant Kumar9152fd12016-05-19 03:54:45 +0000471 Expected<std::unique_ptr<ValueProfData>> VDataPtrOrErr =
Xinliang David Li99556872015-11-17 23:00:40 +0000472 ValueProfData::getValueProfData(D, End, ValueProfDataEndianness);
Justin Bogner9e9a0572015-09-29 22:13:58 +0000473
Vedant Kumar9152fd12016-05-19 03:54:45 +0000474 if (VDataPtrOrErr.takeError())
Justin Bogner9e9a0572015-09-29 22:13:58 +0000475 return false;
Justin Bogner9e9a0572015-09-29 22:13:58 +0000476
Xinliang David Lia716cc52015-12-20 06:22:13 +0000477 VDataPtrOrErr.get()->deserializeTo(DataBuffer.back(), nullptr);
Xinliang David Liee415892015-11-10 00:24:45 +0000478 D += VDataPtrOrErr.get()->TotalSize;
Justin Bogner9e9a0572015-09-29 22:13:58 +0000479
Justin Bogner9e9a0572015-09-29 22:13:58 +0000480 return true;
481}
482
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000483data_type InstrProfLookupTrait::ReadData(StringRef K, const unsigned char *D,
484 offset_type N) {
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000485 // Check if the data is corrupt. If so, don't try to read it.
486 if (N % sizeof(uint64_t))
487 return data_type();
488
489 DataBuffer.clear();
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000490 std::vector<uint64_t> CounterBuffer;
Justin Bogner9e9a0572015-09-29 22:13:58 +0000491
492 using namespace support;
493 const unsigned char *End = D + N;
494 while (D < End) {
Xinliang David Lic7583872015-10-13 16:35:59 +0000495 // Read hash.
Justin Bogner9e9a0572015-09-29 22:13:58 +0000496 if (D + sizeof(uint64_t) >= End)
497 return data_type();
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000498 uint64_t Hash = endian::readNext<uint64_t, little, unaligned>(D);
499
Rong Xu33c76c02016-02-10 17:18:30 +0000500 // Initialize number of counters for GET_VERSION(FormatVersion) == 1.
Justin Bogner9e9a0572015-09-29 22:13:58 +0000501 uint64_t CountsSize = N / sizeof(uint64_t) - 1;
Xinliang David Lic7583872015-10-13 16:35:59 +0000502 // If format version is different then read the number of counters.
Rong Xu33c76c02016-02-10 17:18:30 +0000503 if (GET_VERSION(FormatVersion) != IndexedInstrProf::ProfVersion::Version1) {
Justin Bogner9e9a0572015-09-29 22:13:58 +0000504 if (D + sizeof(uint64_t) > End)
505 return data_type();
506 CountsSize = endian::readNext<uint64_t, little, unaligned>(D);
507 }
Xinliang David Lic7583872015-10-13 16:35:59 +0000508 // Read counter values.
Justin Bogner9e9a0572015-09-29 22:13:58 +0000509 if (D + CountsSize * sizeof(uint64_t) > End)
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000510 return data_type();
511
512 CounterBuffer.clear();
Justin Bogner9e9a0572015-09-29 22:13:58 +0000513 CounterBuffer.reserve(CountsSize);
514 for (uint64_t J = 0; J < CountsSize; ++J)
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000515 CounterBuffer.push_back(endian::readNext<uint64_t, little, unaligned>(D));
516
Xinliang David Li2004f002015-11-02 05:08:23 +0000517 DataBuffer.emplace_back(K, Hash, std::move(CounterBuffer));
Justin Bogner9e9a0572015-09-29 22:13:58 +0000518
Xinliang David Lic7583872015-10-13 16:35:59 +0000519 // Read value profiling data.
Rong Xu33c76c02016-02-10 17:18:30 +0000520 if (GET_VERSION(FormatVersion) > IndexedInstrProf::ProfVersion::Version2 &&
Xinliang David Lia6b2c4f2016-01-14 02:47:01 +0000521 !readValueProfilingData(D, End)) {
Justin Bogner9e9a0572015-09-29 22:13:58 +0000522 DataBuffer.clear();
523 return data_type();
524 }
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000525 }
526 return DataBuffer;
527}
528
Xinliang David Lia28306d2015-12-01 20:26:26 +0000529template <typename HashTableImpl>
Vedant Kumar9152fd12016-05-19 03:54:45 +0000530Error InstrProfReaderIndex<HashTableImpl>::getRecords(
Xinliang David Lia28306d2015-12-01 20:26:26 +0000531 StringRef FuncName, ArrayRef<InstrProfRecord> &Data) {
532 auto Iter = HashTable->find(FuncName);
533 if (Iter == HashTable->end())
Vedant Kumar9152fd12016-05-19 03:54:45 +0000534 return make_error<InstrProfError>(instrprof_error::unknown_function);
Xinliang David Li140f4c42015-10-28 04:20:31 +0000535
536 Data = (*Iter);
Xinliang David Li4c3ab812015-11-12 00:32:17 +0000537 if (Data.empty())
Vedant Kumar9152fd12016-05-19 03:54:45 +0000538 return make_error<InstrProfError>(instrprof_error::malformed);
Xinliang David Li140f4c42015-10-28 04:20:31 +0000539
Vedant Kumar9152fd12016-05-19 03:54:45 +0000540 return Error::success();
Xinliang David Li140f4c42015-10-28 04:20:31 +0000541}
542
Xinliang David Lia28306d2015-12-01 20:26:26 +0000543template <typename HashTableImpl>
Vedant Kumar9152fd12016-05-19 03:54:45 +0000544Error InstrProfReaderIndex<HashTableImpl>::getRecords(
Xinliang David Li140f4c42015-10-28 04:20:31 +0000545 ArrayRef<InstrProfRecord> &Data) {
Xinliang David Lia28306d2015-12-01 20:26:26 +0000546 if (atEnd())
Vedant Kumar9152fd12016-05-19 03:54:45 +0000547 return make_error<InstrProfError>(instrprof_error::eof);
Xinliang David Li140f4c42015-10-28 04:20:31 +0000548
549 Data = *RecordIterator;
550
Xinliang David Li2d4803e2015-12-10 23:48:05 +0000551 if (Data.empty())
Vedant Kumar9152fd12016-05-19 03:54:45 +0000552 return make_error<InstrProfError>(instrprof_error::malformed);
Xinliang David Li140f4c42015-10-28 04:20:31 +0000553
Vedant Kumar9152fd12016-05-19 03:54:45 +0000554 return Error::success();
Xinliang David Li140f4c42015-10-28 04:20:31 +0000555}
556
Xinliang David Lia28306d2015-12-01 20:26:26 +0000557template <typename HashTableImpl>
558InstrProfReaderIndex<HashTableImpl>::InstrProfReaderIndex(
559 const unsigned char *Buckets, const unsigned char *const Payload,
560 const unsigned char *const Base, IndexedInstrProf::HashT HashType,
561 uint64_t Version) {
Xinliang David Li140f4c42015-10-28 04:20:31 +0000562 FormatVersion = Version;
Xinliang David Lia28306d2015-12-01 20:26:26 +0000563 HashTable.reset(HashTableImpl::Create(
564 Buckets, Payload, Base,
565 typename HashTableImpl::InfoType(HashType, Version)));
Xinliang David Lia28306d2015-12-01 20:26:26 +0000566 RecordIterator = HashTable->data_begin();
Xinliang David Li140f4c42015-10-28 04:20:31 +0000567}
568
Justin Bognerb7aa2632014-04-18 21:48:40 +0000569bool IndexedInstrProfReader::hasFormat(const MemoryBuffer &DataBuffer) {
Xinliang David Li4c3ab812015-11-12 00:32:17 +0000570 if (DataBuffer.getBufferSize() < 8)
571 return false;
Justin Bognerb7aa2632014-04-18 21:48:40 +0000572 using namespace support;
573 uint64_t Magic =
574 endian::read<uint64_t, little, aligned>(DataBuffer.getBufferStart());
Xinliang David Lic7583872015-10-13 16:35:59 +0000575 // Verify that it's magical.
Justin Bognerb7aa2632014-04-18 21:48:40 +0000576 return Magic == IndexedInstrProf::Magic;
577}
578
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000579const unsigned char *
580IndexedInstrProfReader::readSummary(IndexedInstrProf::ProfVersion Version,
581 const unsigned char *Cur) {
Easwaran Ramane5a17e32016-05-19 21:07:12 +0000582 using namespace IndexedInstrProf;
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000583 using namespace support;
584 if (Version >= IndexedInstrProf::Version4) {
585 const IndexedInstrProf::Summary *SummaryInLE =
586 reinterpret_cast<const IndexedInstrProf::Summary *>(Cur);
587 uint64_t NFields =
588 endian::byte_swap<uint64_t, little>(SummaryInLE->NumSummaryFields);
589 uint64_t NEntries =
590 endian::byte_swap<uint64_t, little>(SummaryInLE->NumCutoffEntries);
591 uint32_t SummarySize =
592 IndexedInstrProf::Summary::getSize(NFields, NEntries);
593 std::unique_ptr<IndexedInstrProf::Summary> SummaryData =
594 IndexedInstrProf::allocSummary(SummarySize);
595
596 const uint64_t *Src = reinterpret_cast<const uint64_t *>(SummaryInLE);
597 uint64_t *Dst = reinterpret_cast<uint64_t *>(SummaryData.get());
598 for (unsigned I = 0; I < SummarySize / sizeof(uint64_t); I++)
599 Dst[I] = endian::byte_swap<uint64_t, little>(Src[I]);
600
Easwaran Ramane5a17e32016-05-19 21:07:12 +0000601 llvm::SummaryEntryVector DetailedSummary;
602 for (unsigned I = 0; I < SummaryData->NumCutoffEntries; I++) {
603 const IndexedInstrProf::Summary::Entry &Ent = SummaryData->getEntry(I);
604 DetailedSummary.emplace_back((uint32_t)Ent.Cutoff, Ent.MinBlockCount,
605 Ent.NumBlocks);
606 }
Easwaran Raman43095702016-02-17 18:18:47 +0000607 // initialize InstrProfSummary using the SummaryData from disk.
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000608 this->Summary = llvm::make_unique<ProfileSummary>(
609 ProfileSummary::PSK_Instr, DetailedSummary,
Easwaran Ramane5a17e32016-05-19 21:07:12 +0000610 SummaryData->get(Summary::TotalBlockCount),
611 SummaryData->get(Summary::MaxBlockCount),
612 SummaryData->get(Summary::MaxInternalBlockCount),
613 SummaryData->get(Summary::MaxFunctionCount),
614 SummaryData->get(Summary::TotalNumBlocks),
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000615 SummaryData->get(Summary::TotalNumFunctions));
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000616 return Cur + SummarySize;
617 } else {
618 // For older version of profile data, we need to compute on the fly:
619 using namespace IndexedInstrProf;
Easwaran Ramane5a17e32016-05-19 21:07:12 +0000620 InstrProfSummaryBuilder Builder(ProfileSummaryBuilder::DefaultCutoffs);
621 // FIXME: This only computes an empty summary. Need to call addRecord for
622 // all InstrProfRecords to get the correct summary.
Benjamin Kramer38de59e2016-05-20 09:18:37 +0000623 this->Summary = Builder.getSummary();
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000624 return Cur;
625 }
626}
627
Vedant Kumar9152fd12016-05-19 03:54:45 +0000628Error IndexedInstrProfReader::readHeader() {
Aaron Ballmana7c9ed52014-05-01 17:16:24 +0000629 const unsigned char *Start =
630 (const unsigned char *)DataBuffer->getBufferStart();
Justin Bognerb7aa2632014-04-18 21:48:40 +0000631 const unsigned char *Cur = Start;
Aaron Ballmana7c9ed52014-05-01 17:16:24 +0000632 if ((const unsigned char *)DataBuffer->getBufferEnd() - Cur < 24)
Justin Bognerb7aa2632014-04-18 21:48:40 +0000633 return error(instrprof_error::truncated);
634
635 using namespace support;
636
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000637 auto *Header = reinterpret_cast<const IndexedInstrProf::Header *>(Cur);
638 Cur += sizeof(IndexedInstrProf::Header);
639
Justin Bognerb7aa2632014-04-18 21:48:40 +0000640 // Check the magic number.
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000641 uint64_t Magic = endian::byte_swap<uint64_t, little>(Header->Magic);
Justin Bognerb7aa2632014-04-18 21:48:40 +0000642 if (Magic != IndexedInstrProf::Magic)
643 return error(instrprof_error::bad_magic);
644
645 // Read the version.
Xinliang David Li140f4c42015-10-28 04:20:31 +0000646 uint64_t FormatVersion = endian::byte_swap<uint64_t, little>(Header->Version);
Rong Xu33c76c02016-02-10 17:18:30 +0000647 if (GET_VERSION(FormatVersion) >
648 IndexedInstrProf::ProfVersion::CurrentVersion)
Justin Bognerb7aa2632014-04-18 21:48:40 +0000649 return error(instrprof_error::unsupported_version);
650
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000651 Cur = readSummary((IndexedInstrProf::ProfVersion)FormatVersion, Cur);
Justin Bognerb7aa2632014-04-18 21:48:40 +0000652
653 // Read the hash type and start offset.
654 IndexedInstrProf::HashT HashType = static_cast<IndexedInstrProf::HashT>(
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000655 endian::byte_swap<uint64_t, little>(Header->HashType));
Justin Bognerb7aa2632014-04-18 21:48:40 +0000656 if (HashType > IndexedInstrProf::HashT::Last)
657 return error(instrprof_error::unsupported_hash_type);
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000658
659 uint64_t HashOffset = endian::byte_swap<uint64_t, little>(Header->HashOffset);
Justin Bognerb7aa2632014-04-18 21:48:40 +0000660
661 // The rest of the file is an on disk hash table.
Xinliang David Lia28306d2015-12-01 20:26:26 +0000662 InstrProfReaderIndexBase *IndexPtr = nullptr;
663 IndexPtr = new InstrProfReaderIndex<OnDiskHashTableImplV3>(
664 Start + HashOffset, Cur, Start, HashType, FormatVersion);
665 Index.reset(IndexPtr);
Justin Bognerb7aa2632014-04-18 21:48:40 +0000666 return success();
667}
668
Xinliang David Lia716cc52015-12-20 06:22:13 +0000669InstrProfSymtab &IndexedInstrProfReader::getSymtab() {
670 if (Symtab.get())
671 return *Symtab.get();
672
673 std::unique_ptr<InstrProfSymtab> NewSymtab = make_unique<InstrProfSymtab>();
674 Index->populateSymtab(*NewSymtab.get());
675
676 Symtab = std::move(NewSymtab);
677 return *Symtab.get();
678}
679
Vedant Kumar9152fd12016-05-19 03:54:45 +0000680Expected<InstrProfRecord>
Xinliang David Li6aa216c2015-11-06 07:54:21 +0000681IndexedInstrProfReader::getInstrProfRecord(StringRef FuncName,
682 uint64_t FuncHash) {
Xinliang David Li140f4c42015-10-28 04:20:31 +0000683 ArrayRef<InstrProfRecord> Data;
Vedant Kumar9152fd12016-05-19 03:54:45 +0000684 Error Err = Index->getRecords(FuncName, Data);
685 if (Err)
686 return std::move(Err);
Justin Bogner821d7472014-08-01 22:50:07 +0000687 // Found it. Look for counters with the right hash.
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000688 for (unsigned I = 0, E = Data.size(); I < E; ++I) {
Justin Bogner821d7472014-08-01 22:50:07 +0000689 // Check for a match and fill the vector if there is one.
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000690 if (Data[I].Hash == FuncHash) {
Xinliang David Li2004f002015-11-02 05:08:23 +0000691 return std::move(Data[I]);
Justin Bogner821d7472014-08-01 22:50:07 +0000692 }
693 }
694 return error(instrprof_error::hash_mismatch);
Justin Bognerb7aa2632014-04-18 21:48:40 +0000695}
696
Vedant Kumar9152fd12016-05-19 03:54:45 +0000697Error IndexedInstrProfReader::getFunctionCounts(StringRef FuncName,
698 uint64_t FuncHash,
699 std::vector<uint64_t> &Counts) {
700 Expected<InstrProfRecord> Record = getInstrProfRecord(FuncName, FuncHash);
701 if (Error E = Record.takeError())
702 return error(std::move(E));
Xinliang David Li2004f002015-11-02 05:08:23 +0000703
704 Counts = Record.get().Counts;
705 return success();
706}
707
Vedant Kumar9152fd12016-05-19 03:54:45 +0000708Error IndexedInstrProfReader::readNextRecord(InstrProfRecord &Record) {
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000709 static unsigned RecordIndex = 0;
Xinliang David Li140f4c42015-10-28 04:20:31 +0000710
711 ArrayRef<InstrProfRecord> Data;
712
Vedant Kumar9152fd12016-05-19 03:54:45 +0000713 Error E = Index->getRecords(Data);
714 if (E)
715 return error(std::move(E));
Xinliang David Li140f4c42015-10-28 04:20:31 +0000716
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000717 Record = Data[RecordIndex++];
718 if (RecordIndex >= Data.size()) {
Xinliang David Lia28306d2015-12-01 20:26:26 +0000719 Index->advanceToNextKey();
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000720 RecordIndex = 0;
Justin Bogner821d7472014-08-01 22:50:07 +0000721 }
Justin Bognerb7aa2632014-04-18 21:48:40 +0000722 return success();
723}