2004-02-11 06:54:25 +01:00
|
|
|
//===- ProfileInfoLoad.cpp - Load profile information from disk -----------===//
|
2005-04-21 23:13:18 +02:00
|
|
|
//
|
2004-02-11 06:54:25 +01:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 21:36:04 +01:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 23:13:18 +02:00
|
|
|
//
|
2004-02-11 06:54:25 +01:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The ProfileInfoLoader class is used to load and represent profiling
|
|
|
|
// information read in from the dump file.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Analysis/ProfileInfoLoader.h"
|
2004-05-04 18:53:07 +02:00
|
|
|
#include "llvm/Analysis/ProfileInfoTypes.h"
|
2013-01-02 12:36:10 +01:00
|
|
|
#include "llvm/IR/InstrTypes.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2009-08-23 09:33:14 +02:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2004-02-11 06:54:25 +01:00
|
|
|
#include <cstdio>
|
2009-01-05 18:59:02 +01:00
|
|
|
#include <cstdlib>
|
2004-02-11 06:54:25 +01:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
// ByteSwap - Byteswap 'Var' if 'Really' is true.
|
|
|
|
//
|
|
|
|
static inline unsigned ByteSwap(unsigned Var, bool Really) {
|
|
|
|
if (!Really) return Var;
|
2009-09-06 14:56:52 +02:00
|
|
|
return ((Var & (255U<< 0U)) << 24U) |
|
|
|
|
((Var & (255U<< 8U)) << 8U) |
|
|
|
|
((Var & (255U<<16U)) >> 8U) |
|
|
|
|
((Var & (255U<<24U)) >> 24U);
|
2004-02-11 06:54:25 +01:00
|
|
|
}
|
|
|
|
|
2009-09-03 11:11:10 +02:00
|
|
|
static unsigned AddCounts(unsigned A, unsigned B) {
|
2009-09-03 10:41:05 +02:00
|
|
|
// If either value is undefined, use the other.
|
2009-09-16 13:35:50 +02:00
|
|
|
if (A == ProfileInfoLoader::Uncounted) return B;
|
|
|
|
if (B == ProfileInfoLoader::Uncounted) return A;
|
2009-09-03 10:41:05 +02:00
|
|
|
return A + B;
|
|
|
|
}
|
|
|
|
|
2004-02-11 06:54:25 +01:00
|
|
|
static void ReadProfilingBlock(const char *ToolName, FILE *F,
|
|
|
|
bool ShouldByteSwap,
|
|
|
|
std::vector<unsigned> &Data) {
|
|
|
|
// Read the number of entries...
|
|
|
|
unsigned NumEntries;
|
|
|
|
if (fread(&NumEntries, sizeof(unsigned), 1, F) != 1) {
|
2009-08-23 09:33:14 +02:00
|
|
|
errs() << ToolName << ": data packet truncated!\n";
|
2004-02-11 06:54:25 +01:00
|
|
|
perror(0);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
NumEntries = ByteSwap(NumEntries, ShouldByteSwap);
|
|
|
|
|
|
|
|
// Read the counts...
|
|
|
|
std::vector<unsigned> TempSpace(NumEntries);
|
|
|
|
|
|
|
|
// Read in the block of data...
|
|
|
|
if (fread(&TempSpace[0], sizeof(unsigned)*NumEntries, 1, F) != 1) {
|
2009-08-23 09:33:14 +02:00
|
|
|
errs() << ToolName << ": data packet truncated!\n";
|
2004-02-11 06:54:25 +01:00
|
|
|
perror(0);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2009-09-01 21:08:51 +02:00
|
|
|
// Make sure we have enough space... The space is initialised to -1 to
|
|
|
|
// facitiltate the loading of missing values for OptimalEdgeProfiling.
|
2004-02-11 06:54:25 +01:00
|
|
|
if (Data.size() < NumEntries)
|
2009-09-16 13:35:50 +02:00
|
|
|
Data.resize(NumEntries, ProfileInfoLoader::Uncounted);
|
2005-04-21 23:13:18 +02:00
|
|
|
|
2004-02-11 06:54:25 +01:00
|
|
|
// Accumulate the data we just read into the data.
|
|
|
|
if (!ShouldByteSwap) {
|
2009-09-01 21:08:51 +02:00
|
|
|
for (unsigned i = 0; i != NumEntries; ++i) {
|
2009-09-03 10:41:05 +02:00
|
|
|
Data[i] = AddCounts(TempSpace[i], Data[i]);
|
2009-09-01 21:08:51 +02:00
|
|
|
}
|
2004-02-11 06:54:25 +01:00
|
|
|
} else {
|
2009-09-01 21:08:51 +02:00
|
|
|
for (unsigned i = 0; i != NumEntries; ++i) {
|
2009-09-03 10:41:05 +02:00
|
|
|
Data[i] = AddCounts(ByteSwap(TempSpace[i], true), Data[i]);
|
2009-09-01 21:08:51 +02:00
|
|
|
}
|
2004-02-11 06:54:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-16 13:35:50 +02:00
|
|
|
const unsigned ProfileInfoLoader::Uncounted = ~0U;
|
|
|
|
|
2004-02-11 06:54:25 +01:00
|
|
|
// ProfileInfoLoader ctor - Read the specified profiling data file, exiting the
|
|
|
|
// program if the file is invalid or broken.
|
|
|
|
//
|
|
|
|
ProfileInfoLoader::ProfileInfoLoader(const char *ToolName,
|
2012-06-06 21:47:08 +02:00
|
|
|
const std::string &Filename)
|
2012-07-21 00:05:57 +02:00
|
|
|
: Filename(Filename) {
|
2009-08-25 14:53:27 +02:00
|
|
|
FILE *F = fopen(Filename.c_str(), "rb");
|
2004-02-11 06:54:25 +01:00
|
|
|
if (F == 0) {
|
2009-08-23 09:33:14 +02:00
|
|
|
errs() << ToolName << ": Error opening '" << Filename << "': ";
|
2004-02-11 06:54:25 +01:00
|
|
|
perror(0);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keep reading packets until we run out of them.
|
|
|
|
unsigned PacketType;
|
|
|
|
while (fread(&PacketType, sizeof(unsigned), 1, F) == 1) {
|
|
|
|
// If the low eight bits of the packet are zero, we must be dealing with an
|
|
|
|
// endianness mismatch. Byteswap all words read from the profiling
|
|
|
|
// information.
|
|
|
|
bool ShouldByteSwap = (char)PacketType == 0;
|
|
|
|
PacketType = ByteSwap(PacketType, ShouldByteSwap);
|
|
|
|
|
|
|
|
switch (PacketType) {
|
|
|
|
case ArgumentInfo: {
|
|
|
|
unsigned ArgLength;
|
|
|
|
if (fread(&ArgLength, sizeof(unsigned), 1, F) != 1) {
|
2009-08-23 09:33:14 +02:00
|
|
|
errs() << ToolName << ": arguments packet truncated!\n";
|
2004-02-11 06:54:25 +01:00
|
|
|
perror(0);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
ArgLength = ByteSwap(ArgLength, ShouldByteSwap);
|
|
|
|
|
|
|
|
// Read in the arguments...
|
|
|
|
std::vector<char> Chars(ArgLength+4);
|
|
|
|
|
|
|
|
if (ArgLength)
|
|
|
|
if (fread(&Chars[0], (ArgLength+3) & ~3, 1, F) != 1) {
|
2009-08-23 09:33:14 +02:00
|
|
|
errs() << ToolName << ": arguments packet truncated!\n";
|
2004-02-11 06:54:25 +01:00
|
|
|
perror(0);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
CommandLines.push_back(std::string(&Chars[0], &Chars[ArgLength]));
|
|
|
|
break;
|
|
|
|
}
|
2005-04-21 23:13:18 +02:00
|
|
|
|
2004-02-11 06:54:25 +01:00
|
|
|
case FunctionInfo:
|
|
|
|
ReadProfilingBlock(ToolName, F, ShouldByteSwap, FunctionCounts);
|
|
|
|
break;
|
2005-04-21 23:13:18 +02:00
|
|
|
|
2004-02-11 06:54:25 +01:00
|
|
|
case BlockInfo:
|
|
|
|
ReadProfilingBlock(ToolName, F, ShouldByteSwap, BlockCounts);
|
|
|
|
break;
|
|
|
|
|
2004-03-08 19:20:18 +01:00
|
|
|
case EdgeInfo:
|
|
|
|
ReadProfilingBlock(ToolName, F, ShouldByteSwap, EdgeCounts);
|
|
|
|
break;
|
|
|
|
|
2009-09-01 21:08:51 +02:00
|
|
|
case OptEdgeInfo:
|
|
|
|
ReadProfilingBlock(ToolName, F, ShouldByteSwap, OptimalEdgeCounts);
|
|
|
|
break;
|
|
|
|
|
2004-05-04 19:11:14 +02:00
|
|
|
case BBTraceInfo:
|
|
|
|
ReadProfilingBlock(ToolName, F, ShouldByteSwap, BBTrace);
|
|
|
|
break;
|
|
|
|
|
2004-02-11 06:54:25 +01:00
|
|
|
default:
|
2009-08-23 09:33:14 +02:00
|
|
|
errs() << ToolName << ": Unknown packet type #" << PacketType << "!\n";
|
2004-02-11 06:54:25 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
2005-04-21 23:13:18 +02:00
|
|
|
|
2004-02-11 06:54:25 +01:00
|
|
|
fclose(F);
|
|
|
|
}
|
|
|
|
|