2004-02-11 05:54:25 +00:00
|
|
|
//===- ProfileInfoLoad.cpp - Load profile information from disk -----------===//
|
2005-04-21 21:13:18 +00:00
|
|
|
//
|
2004-02-11 05:54:25 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 21:13:18 +00:00
|
|
|
//
|
2004-02-11 05:54:25 +00: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 16:53:07 +00:00
|
|
|
#include "llvm/Analysis/ProfileInfoTypes.h"
|
2004-02-11 05:54:25 +00:00
|
|
|
#include "llvm/Module.h"
|
2004-03-08 18:20:18 +00:00
|
|
|
#include "llvm/InstrTypes.h"
|
2009-08-23 07:33:14 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2004-02-11 05:54:25 +00:00
|
|
|
#include <cstdio>
|
2009-01-05 17:59:02 +00:00
|
|
|
#include <cstdlib>
|
2004-03-08 20:03:52 +00:00
|
|
|
#include <map>
|
2004-02-11 05:54:25 +00: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 12:56:52 +00:00
|
|
|
return ((Var & (255U<< 0U)) << 24U) |
|
|
|
|
((Var & (255U<< 8U)) << 8U) |
|
|
|
|
((Var & (255U<<16U)) >> 8U) |
|
|
|
|
((Var & (255U<<24U)) >> 24U);
|
2004-02-11 05:54:25 +00:00
|
|
|
}
|
|
|
|
|
2009-09-03 09:11:10 +00:00
|
|
|
static unsigned AddCounts(unsigned A, unsigned B) {
|
2009-09-03 08:41:05 +00:00
|
|
|
// If either value is undefined, use the other.
|
2009-09-16 11:35:50 +00:00
|
|
|
if (A == ProfileInfoLoader::Uncounted) return B;
|
|
|
|
if (B == ProfileInfoLoader::Uncounted) return A;
|
2009-09-03 08:41:05 +00:00
|
|
|
return A + B;
|
|
|
|
}
|
|
|
|
|
2004-02-11 05:54:25 +00: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 07:33:14 +00:00
|
|
|
errs() << ToolName << ": data packet truncated!\n";
|
2004-02-11 05:54:25 +00: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 07:33:14 +00:00
|
|
|
errs() << ToolName << ": data packet truncated!\n";
|
2004-02-11 05:54:25 +00:00
|
|
|
perror(0);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2009-09-01 19:08:51 +00: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 05:54:25 +00:00
|
|
|
if (Data.size() < NumEntries)
|
2009-09-16 11:35:50 +00:00
|
|
|
Data.resize(NumEntries, ProfileInfoLoader::Uncounted);
|
2005-04-21 21:13:18 +00:00
|
|
|
|
2004-02-11 05:54:25 +00:00
|
|
|
// Accumulate the data we just read into the data.
|
|
|
|
if (!ShouldByteSwap) {
|
2009-09-01 19:08:51 +00:00
|
|
|
for (unsigned i = 0; i != NumEntries; ++i) {
|
2009-09-03 08:41:05 +00:00
|
|
|
Data[i] = AddCounts(TempSpace[i], Data[i]);
|
2009-09-01 19:08:51 +00:00
|
|
|
}
|
2004-02-11 05:54:25 +00:00
|
|
|
} else {
|
2009-09-01 19:08:51 +00:00
|
|
|
for (unsigned i = 0; i != NumEntries; ++i) {
|
2009-09-03 08:41:05 +00:00
|
|
|
Data[i] = AddCounts(ByteSwap(TempSpace[i], true), Data[i]);
|
2009-09-01 19:08:51 +00:00
|
|
|
}
|
2004-02-11 05:54:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-16 11:35:50 +00:00
|
|
|
const unsigned ProfileInfoLoader::Uncounted = ~0U;
|
|
|
|
|
2004-02-11 05:54:25 +00:00
|
|
|
// ProfileInfoLoader ctor - Read the specified profiling data file, exiting the
|
|
|
|
// program if the file is invalid or broken.
|
|
|
|
//
|
|
|
|
ProfileInfoLoader::ProfileInfoLoader(const char *ToolName,
|
|
|
|
const std::string &Filename,
|
2009-08-05 15:55:56 +00:00
|
|
|
Module &TheModule) :
|
|
|
|
Filename(Filename),
|
|
|
|
M(TheModule), Warned(false) {
|
2009-08-25 12:53:27 +00:00
|
|
|
FILE *F = fopen(Filename.c_str(), "rb");
|
2004-02-11 05:54:25 +00:00
|
|
|
if (F == 0) {
|
2009-08-23 07:33:14 +00:00
|
|
|
errs() << ToolName << ": Error opening '" << Filename << "': ";
|
2004-02-11 05:54:25 +00: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 07:33:14 +00:00
|
|
|
errs() << ToolName << ": arguments packet truncated!\n";
|
2004-02-11 05:54:25 +00: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 07:33:14 +00:00
|
|
|
errs() << ToolName << ": arguments packet truncated!\n";
|
2004-02-11 05:54:25 +00:00
|
|
|
perror(0);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
CommandLines.push_back(std::string(&Chars[0], &Chars[ArgLength]));
|
|
|
|
break;
|
|
|
|
}
|
2005-04-21 21:13:18 +00:00
|
|
|
|
2004-02-11 05:54:25 +00:00
|
|
|
case FunctionInfo:
|
|
|
|
ReadProfilingBlock(ToolName, F, ShouldByteSwap, FunctionCounts);
|
|
|
|
break;
|
2005-04-21 21:13:18 +00:00
|
|
|
|
2004-02-11 05:54:25 +00:00
|
|
|
case BlockInfo:
|
|
|
|
ReadProfilingBlock(ToolName, F, ShouldByteSwap, BlockCounts);
|
|
|
|
break;
|
|
|
|
|
2004-03-08 18:20:18 +00:00
|
|
|
case EdgeInfo:
|
|
|
|
ReadProfilingBlock(ToolName, F, ShouldByteSwap, EdgeCounts);
|
|
|
|
break;
|
|
|
|
|
2009-09-01 19:08:51 +00:00
|
|
|
case OptEdgeInfo:
|
|
|
|
ReadProfilingBlock(ToolName, F, ShouldByteSwap, OptimalEdgeCounts);
|
|
|
|
break;
|
|
|
|
|
2004-05-04 17:11:14 +00:00
|
|
|
case BBTraceInfo:
|
|
|
|
ReadProfilingBlock(ToolName, F, ShouldByteSwap, BBTrace);
|
|
|
|
break;
|
|
|
|
|
2004-02-11 05:54:25 +00:00
|
|
|
default:
|
2009-08-23 07:33:14 +00:00
|
|
|
errs() << ToolName << ": Unknown packet type #" << PacketType << "!\n";
|
2004-02-11 05:54:25 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
2005-04-21 21:13:18 +00:00
|
|
|
|
2004-02-11 05:54:25 +00:00
|
|
|
fclose(F);
|
|
|
|
}
|
|
|
|
|