ProfileDataLoader.cpp [plain text]
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/OwningPtr.h"
#include "llvm/Module.h"
#include "llvm/InstrTypes.h"
#include "llvm/Analysis/ProfileDataLoader.h"
#include "llvm/Analysis/ProfileDataTypes.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/system_error.h"
#include <cstdio>
#include <cstdlib>
using namespace llvm;
raw_ostream &llvm::operator<<(raw_ostream &O, std::pair<const BasicBlock *,
const BasicBlock *> E) {
O << "(";
if (E.first)
O << E.first->getName();
else
O << "0";
O << ",";
if (E.second)
O << E.second->getName();
else
O << "0";
return O << ")";
}
static unsigned AddCounts(unsigned A, unsigned B) {
if (A == ProfileDataLoader::Uncounted) return B;
if (B == ProfileDataLoader::Uncounted) return A;
uint64_t tmp = (uint64_t)A + (uint64_t)B;
if (tmp > (uint64_t)ProfileDataLoader::MaxCount)
tmp = ProfileDataLoader::MaxCount;
return (unsigned)tmp;
}
template <typename T>
static void ReadProfilingData(const char *ToolName, FILE *F,
T *Data, size_t NumEntries) {
if (fread(Data, sizeof(T), NumEntries, F) != NumEntries)
report_fatal_error(Twine(ToolName) + ": Profiling data truncated");
}
static unsigned ReadProfilingNumEntries(const char *ToolName, FILE *F,
bool ShouldByteSwap) {
unsigned Entry;
ReadProfilingData<unsigned>(ToolName, F, &Entry, 1);
return ShouldByteSwap ? ByteSwap_32(Entry) : Entry;
}
static void ReadProfilingBlock(const char *ToolName, FILE *F,
bool ShouldByteSwap,
SmallVector<unsigned, 32> &Data) {
unsigned NumEntries = ReadProfilingNumEntries(ToolName, F, ShouldByteSwap);
SmallVector<unsigned, 8> TempSpace(NumEntries);
ReadProfilingData<unsigned>(ToolName, F, TempSpace.data(), NumEntries);
if (Data.size() < NumEntries)
Data.resize(NumEntries, ProfileDataLoader::Uncounted);
for (unsigned i = 0; i < NumEntries; ++i) {
unsigned Entry = ShouldByteSwap ? ByteSwap_32(TempSpace[i]) : TempSpace[i];
Data[i] = AddCounts(Entry, Data[i]);
}
}
static void ReadProfilingArgBlock(const char *ToolName, FILE *F,
bool ShouldByteSwap,
SmallVector<std::string, 1> &CommandLines) {
unsigned ArgLength = ReadProfilingNumEntries(ToolName, F, ShouldByteSwap);
SmallVector<char, 8> Args(ArgLength+4);
if (ArgLength)
ReadProfilingData<char>(ToolName, F, Args.data(), (ArgLength+3) & ~3);
CommandLines.push_back(std::string(&Args[0], &Args[ArgLength]));
}
const unsigned ProfileDataLoader::Uncounted = ~0U;
const unsigned ProfileDataLoader::MaxCount = ~0U - 1U;
ProfileDataLoader::ProfileDataLoader(const char *ToolName,
const std::string &Filename)
: Filename(Filename) {
FILE *F = fopen(Filename.c_str(), "rb");
if (F == 0)
report_fatal_error(Twine(ToolName) + ": Error opening '" +
Filename + "': ");
unsigned PacketType;
while (fread(&PacketType, sizeof(unsigned), 1, F) == 1) {
bool ShouldByteSwap = (char)PacketType == 0;
PacketType = ShouldByteSwap ? ByteSwap_32(PacketType) : PacketType;
switch (PacketType) {
case ArgumentInfo:
ReadProfilingArgBlock(ToolName, F, ShouldByteSwap, CommandLines);
break;
case EdgeInfo:
ReadProfilingBlock(ToolName, F, ShouldByteSwap, EdgeCounts);
break;
default:
report_fatal_error(std::string(ToolName)
+ ": Unknown profiling packet type");
break;
}
}
fclose(F);
}