+ advanceData();
+ return success();
+}
+
+namespace llvm {
+template class RawInstrProfReader<uint32_t>;
+template class RawInstrProfReader<uint64_t>;
+}
+
+InstrProfLookupTrait::hash_value_type
+InstrProfLookupTrait::ComputeHash(StringRef K) {
+ return IndexedInstrProf::ComputeHash(HashType, K);
+}
+
+typedef InstrProfLookupTrait::data_type data_type;
+typedef InstrProfLookupTrait::offset_type offset_type;
+
+bool InstrProfLookupTrait::readValueProfilingData(
+ const unsigned char *&D, const unsigned char *const End) {
+ ErrorOr<std::unique_ptr<ValueProfData>> VDataPtrOrErr =
+ ValueProfData::getValueProfData(D, End, ValueProfDataEndianness);
+
+ if (VDataPtrOrErr.getError())
+ return false;
+
+ VDataPtrOrErr.get()->deserializeTo(DataBuffer.back(), nullptr);
+ D += VDataPtrOrErr.get()->TotalSize;
+
+ return true;
+}
+
+data_type InstrProfLookupTrait::ReadData(StringRef K, const unsigned char *D,
+ offset_type N) {
+ // Check if the data is corrupt. If so, don't try to read it.
+ if (N % sizeof(uint64_t))
+ return data_type();
+
+ DataBuffer.clear();
+ std::vector<uint64_t> CounterBuffer;
+
+ using namespace support;
+ const unsigned char *End = D + N;
+ while (D < End) {
+ // Read hash.
+ if (D + sizeof(uint64_t) >= End)
+ return data_type();
+ uint64_t Hash = endian::readNext<uint64_t, little, unaligned>(D);
+
+ // Initialize number of counters for FormatVersion == 1.
+ uint64_t CountsSize = N / sizeof(uint64_t) - 1;
+ // If format version is different then read the number of counters.
+ if (FormatVersion != 1) {
+ if (D + sizeof(uint64_t) > End)
+ return data_type();
+ CountsSize = endian::readNext<uint64_t, little, unaligned>(D);
+ }
+ // Read counter values.
+ if (D + CountsSize * sizeof(uint64_t) > End)
+ return data_type();
+
+ CounterBuffer.clear();
+ CounterBuffer.reserve(CountsSize);
+ for (uint64_t J = 0; J < CountsSize; ++J)
+ CounterBuffer.push_back(endian::readNext<uint64_t, little, unaligned>(D));
+
+ DataBuffer.emplace_back(K, Hash, std::move(CounterBuffer));
+
+ // Read value profiling data.
+ if (FormatVersion > 2 && !readValueProfilingData(D, End)) {
+ DataBuffer.clear();
+ return data_type();
+ }
+ }
+ return DataBuffer;
+}
+
+template <typename HashTableImpl>
+std::error_code InstrProfReaderIndex<HashTableImpl>::getRecords(
+ StringRef FuncName, ArrayRef<InstrProfRecord> &Data) {
+ auto Iter = HashTable->find(FuncName);
+ if (Iter == HashTable->end())
+ return instrprof_error::unknown_function;
+
+ Data = (*Iter);
+ if (Data.empty())
+ return instrprof_error::malformed;
+
+ return instrprof_error::success;
+}
+
+template <typename HashTableImpl>
+std::error_code InstrProfReaderIndex<HashTableImpl>::getRecords(
+ ArrayRef<InstrProfRecord> &Data) {
+ if (atEnd())
+ return instrprof_error::eof;
+
+ Data = *RecordIterator;
+
+ if (Data.empty())
+ return instrprof_error::malformed;
+
+ return instrprof_error::success;
+}
+
+template <typename HashTableImpl>
+InstrProfReaderIndex<HashTableImpl>::InstrProfReaderIndex(
+ const unsigned char *Buckets, const unsigned char *const Payload,
+ const unsigned char *const Base, IndexedInstrProf::HashT HashType,
+ uint64_t Version) {
+ FormatVersion = Version;
+ HashTable.reset(HashTableImpl::Create(
+ Buckets, Payload, Base,
+ typename HashTableImpl::InfoType(HashType, Version)));
+ RecordIterator = HashTable->data_begin();
+}
+
+bool IndexedInstrProfReader::hasFormat(const MemoryBuffer &DataBuffer) {
+ if (DataBuffer.getBufferSize() < 8)
+ return false;
+ using namespace support;
+ uint64_t Magic =
+ endian::read<uint64_t, little, aligned>(DataBuffer.getBufferStart());
+ // Verify that it's magical.
+ return Magic == IndexedInstrProf::Magic;
+}
+
+std::error_code IndexedInstrProfReader::readHeader() {
+ const unsigned char *Start =
+ (const unsigned char *)DataBuffer->getBufferStart();
+ const unsigned char *Cur = Start;
+ if ((const unsigned char *)DataBuffer->getBufferEnd() - Cur < 24)
+ return error(instrprof_error::truncated);
+
+ using namespace support;
+
+ auto *Header = reinterpret_cast<const IndexedInstrProf::Header *>(Cur);
+ Cur += sizeof(IndexedInstrProf::Header);
+
+ // Check the magic number.
+ uint64_t Magic = endian::byte_swap<uint64_t, little>(Header->Magic);
+ if (Magic != IndexedInstrProf::Magic)
+ return error(instrprof_error::bad_magic);
+
+ // Read the version.
+ uint64_t FormatVersion = endian::byte_swap<uint64_t, little>(Header->Version);
+ if (FormatVersion > IndexedInstrProf::Version)
+ return error(instrprof_error::unsupported_version);
+
+ // Read the maximal function count.
+ MaxFunctionCount =
+ endian::byte_swap<uint64_t, little>(Header->MaxFunctionCount);
+
+ // Read the hash type and start offset.
+ IndexedInstrProf::HashT HashType = static_cast<IndexedInstrProf::HashT>(
+ endian::byte_swap<uint64_t, little>(Header->HashType));
+ if (HashType > IndexedInstrProf::HashT::Last)
+ return error(instrprof_error::unsupported_hash_type);
+
+ uint64_t HashOffset = endian::byte_swap<uint64_t, little>(Header->HashOffset);
+
+ // The rest of the file is an on disk hash table.
+ InstrProfReaderIndexBase *IndexPtr = nullptr;
+ IndexPtr = new InstrProfReaderIndex<OnDiskHashTableImplV3>(
+ Start + HashOffset, Cur, Start, HashType, FormatVersion);
+ Index.reset(IndexPtr);
+ return success();
+}
+
+InstrProfSymtab &IndexedInstrProfReader::getSymtab() {
+ if (Symtab.get())
+ return *Symtab.get();
+
+ std::unique_ptr<InstrProfSymtab> NewSymtab = make_unique<InstrProfSymtab>();
+ Index->populateSymtab(*NewSymtab.get());
+
+ Symtab = std::move(NewSymtab);
+ return *Symtab.get();
+}
+
+ErrorOr<InstrProfRecord>
+IndexedInstrProfReader::getInstrProfRecord(StringRef FuncName,
+ uint64_t FuncHash) {
+ ArrayRef<InstrProfRecord> Data;
+ std::error_code EC = Index->getRecords(FuncName, Data);
+ if (EC != instrprof_error::success)
+ return EC;
+ // Found it. Look for counters with the right hash.
+ for (unsigned I = 0, E = Data.size(); I < E; ++I) {
+ // Check for a match and fill the vector if there is one.
+ if (Data[I].Hash == FuncHash) {
+ return std::move(Data[I]);
+ }
+ }
+ return error(instrprof_error::hash_mismatch);
+}
+
+std::error_code
+IndexedInstrProfReader::getFunctionCounts(StringRef FuncName, uint64_t FuncHash,
+ std::vector<uint64_t> &Counts) {
+ ErrorOr<InstrProfRecord> Record = getInstrProfRecord(FuncName, FuncHash);
+ if (std::error_code EC = Record.getError())
+ return EC;
+
+ Counts = Record.get().Counts;
+ return success();
+}
+
+std::error_code IndexedInstrProfReader::readNextRecord(
+ InstrProfRecord &Record) {
+ static unsigned RecordIndex = 0;
+
+ ArrayRef<InstrProfRecord> Data;
+
+ std::error_code EC = Index->getRecords(Data);
+ if (EC != instrprof_error::success)
+ return error(EC);
+
+ Record = Data[RecordIndex++];
+ if (RecordIndex >= Data.size()) {
+ Index->advanceToNextKey();
+ RecordIndex = 0;
+ }