llvm/lib/ProfileData/InstrProfReader.cpp
Justin Bogner c0f3b72555 ProfileData: Introduce the InstrProfReader interface and a text reader
This introduces the ProfileData library and updates llvm-profdata to
use this library for reading profiles. InstrProfReader is an abstract
base class that will be subclassed for both the raw instrprof data
from compiler-rt and the efficient instrprof format that will be used
for PGO.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@204482 91177308-0d34-0410-b5e6-96231b3b80d8
2014-03-21 17:24:48 +00:00

85 lines
2.6 KiB
C++

//=-- InstrProfReader.cpp - Instrumented profiling reader -------------------=//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file contains support for reading profiling data for clang's
// instrumentation based PGO and coverage.
//
//===----------------------------------------------------------------------===//
#include "llvm/ProfileData/InstrProfReader.h"
#include "llvm/ProfileData/InstrProf.h"
#include "llvm/Support/Endian.h"
#include <cassert>
using namespace llvm;
error_code InstrProfReader::create(std::string Path,
std::unique_ptr<InstrProfReader> &Result) {
std::unique_ptr<MemoryBuffer> Buffer;
if (error_code EC = MemoryBuffer::getFileOrSTDIN(Path, Buffer))
return EC;
// Sanity check the file.
if (Buffer->getBufferSize() > std::numeric_limits<unsigned>::max())
return instrprof_error::too_large;
// FIXME: This needs to determine which format the file is and construct the
// correct subclass.
Result.reset(new TextInstrProfReader(Buffer));
return instrprof_error::success;
}
void InstrProfIterator::Increment() {
if (Reader->readNextRecord(Record))
*this = InstrProfIterator();
}
error_code TextInstrProfReader::readNextRecord(InstrProfRecord &Record) {
// Skip empty lines.
while (!Line.is_at_end() && Line->empty())
++Line;
// If we hit EOF while looking for a name, we're done.
if (Line.is_at_end())
return error(instrprof_error::eof);
// Read the function name.
Record.Name = *Line++;
// Read the function hash.
if (Line.is_at_end())
return error(instrprof_error::truncated);
if ((Line++)->getAsInteger(10, Record.Hash))
return error(instrprof_error::malformed);
// Read the number of counters.
uint64_t NumCounters;
if (Line.is_at_end())
return error(instrprof_error::truncated);
if ((Line++)->getAsInteger(10, NumCounters))
return error(instrprof_error::malformed);
// Read each counter and fill our internal storage with the values.
Counts.clear();
Counts.reserve(NumCounters);
for (uint64_t I = 0; I < NumCounters; ++I) {
if (Line.is_at_end())
return error(instrprof_error::truncated);
uint64_t Count;
if ((Line++)->getAsInteger(10, Count))
return error(instrprof_error::malformed);
Counts.push_back(Count);
}
// Give the record a reference to our internal counter storage.
Record.Counts = Counts;
return success();
}