Cleanups due to feedback. No functionality change. Patch by Alistair.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@162979 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Bill Wendling 2012-08-31 05:18:31 +00:00
parent 5d60c67318
commit f91e400b21
3 changed files with 63 additions and 63 deletions

View File

@ -16,11 +16,11 @@
#ifndef LLVM_ANALYSIS_PROFILEDATALOADER_H #ifndef LLVM_ANALYSIS_PROFILEDATALOADER_H
#define LLVM_ANALYSIS_PROFILEDATALOADER_H #define LLVM_ANALYSIS_PROFILEDATALOADER_H
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/Debug.h" #include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h" #include "llvm/Support/ErrorHandling.h"
#include <vector>
#include <string> #include <string>
#include <map>
namespace llvm { namespace llvm {
@ -46,14 +46,14 @@ class ProfileDataT {
typedef std::pair<const BType*, const BType*> Edge; typedef std::pair<const BType*, const BType*> Edge;
private: private:
typedef std::map<Edge, unsigned> EdgeWeights; typedef DenseMap<Edge, unsigned> EdgeWeights;
/// \brief Count the number of times a transition between two blocks is /// \brief Count the number of times a transition between two blocks is
/// executed. /// executed.
/// ///
/// As a special case, we also hold an edge from the null BasicBlock to the /// As a special case, we also hold an edge from the null BasicBlock to the
/// entry block to indicate how many times the function was entered. /// entry block to indicate how many times the function was entered.
std::map<const FType*, EdgeWeights> EdgeInformation; DenseMap<const FType*, EdgeWeights> EdgeInformation;
public: public:
static char ID; // Class identification, replacement for typeinfo static char ID; // Class identification, replacement for typeinfo
@ -61,9 +61,9 @@ class ProfileDataT {
~ProfileDataT() {}; ~ProfileDataT() {};
/// getFunction() - Returns the Function for an Edge. /// getFunction() - Returns the Function for an Edge.
static const FType* getFunction(Edge e) { static const FType *getFunction(Edge e) {
// e.first may be NULL // e.first may be NULL
assert( ((!e.first) || (e.first->getParent() == e.second->getParent())) assert(((!e.first) || (e.first->getParent() == e.second->getParent()))
&& "A ProfileData::Edge can not be between two functions"); && "A ProfileData::Edge can not be between two functions");
assert(e.second && "A ProfileData::Edge must have a real sink"); assert(e.second && "A ProfileData::Edge must have a real sink");
return e.second->getParent(); return e.second->getParent();
@ -71,18 +71,18 @@ class ProfileDataT {
/// getEdge() - Creates an Edge between two BasicBlocks. /// getEdge() - Creates an Edge between two BasicBlocks.
static Edge getEdge(const BType *Src, const BType *Dest) { static Edge getEdge(const BType *Src, const BType *Dest) {
return std::make_pair(Src, Dest); return Edge(Src, Dest);
} }
/// getEdgeWeight - Return the number of times that a given edge was /// getEdgeWeight - Return the number of times that a given edge was
/// executed. /// executed.
unsigned getEdgeWeight(Edge e) const { unsigned getEdgeWeight(Edge e) const {
const FType *f = getFunction(e); const FType *f = getFunction(e);
assert( (EdgeInformation.find(f) != EdgeInformation.end()) assert((EdgeInformation.find(f) != EdgeInformation.end())
&& "No profiling information for function"); && "No profiling information for function");
EdgeWeights weights = EdgeInformation.find(f)->second; EdgeWeights weights = EdgeInformation.find(f)->second;
assert( (weights.find(e) != weights.end()) assert((weights.find(e) != weights.end())
&& "No profiling information for edge"); && "No profiling information for edge");
return weights.find(e)->second; return weights.find(e)->second;
} }
@ -106,11 +106,11 @@ private:
/// A vector of the command line arguments used when the target program was /// A vector of the command line arguments used when the target program was
/// run to generate profiling data. One entry per program run. /// run to generate profiling data. One entry per program run.
std::vector<std::string> CommandLines; SmallVector<std::string, 1> CommandLines;
/// The raw values for how many times each edge was traversed, values from /// The raw values for how many times each edge was traversed, values from
/// multiple program runs are accumulated. /// multiple program runs are accumulated.
std::vector<unsigned> EdgeCounts; SmallVector<unsigned, 32> EdgeCounts;
public: public:
/// ProfileDataLoader ctor - Read the specified profiling data file, exiting /// ProfileDataLoader ctor - Read the specified profiling data file, exiting
@ -130,13 +130,13 @@ public:
/// getExecution - Return the command line parameters used to generate the /// getExecution - Return the command line parameters used to generate the
/// i'th set of profiling data. /// i'th set of profiling data.
const std::string& getExecution(unsigned i) const { return CommandLines[i]; } const std::string &getExecution(unsigned i) const { return CommandLines[i]; }
const std::string& getFileName() const { return Filename; } const std::string &getFileName() const { return Filename; }
/// getRawEdgeCounts - Return the raw profiling data, this is just a list of /// getRawEdgeCounts - Return the raw profiling data, this is just a list of
/// numbers with no mappings to edges. /// numbers with no mappings to edges.
const std::vector<unsigned>& getRawEdgeCounts() const { return EdgeCounts; } const SmallVector<unsigned, 32> &getRawEdgeCounts() const { return EdgeCounts; }
}; };
/// createProfileMetadataLoaderPass - This function returns a Pass that loads /// createProfileMetadataLoaderPass - This function returns a Pass that loads

View File

@ -12,11 +12,14 @@
// //
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/OwningPtr.h"
#include "llvm/Module.h" #include "llvm/Module.h"
#include "llvm/InstrTypes.h" #include "llvm/InstrTypes.h"
#include "llvm/Analysis/ProfileDataLoader.h" #include "llvm/Analysis/ProfileDataLoader.h"
#include "llvm/Analysis/ProfileDataTypes.h" #include "llvm/Analysis/ProfileDataTypes.h"
#include "llvm/Support/raw_ostream.h" #include "llvm/Support/raw_ostream.h"
#include "llvm/Support/system_error.h"
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
using namespace llvm; using namespace llvm;
@ -34,7 +37,8 @@ raw_ostream& operator<<(raw_ostream &O, const BasicBlock *BB) {
return O << BB->getName(); return O << BB->getName();
} }
raw_ostream& operator<<(raw_ostream &O, std::pair<const BasicBlock *, const BasicBlock *> E) { raw_ostream& operator<<(raw_ostream &O, std::pair<const BasicBlock *,
const BasicBlock *> E) {
O << "("; O << "(";
if (E.first) if (E.first)
@ -54,10 +58,9 @@ raw_ostream& operator<<(raw_ostream &O, std::pair<const BasicBlock *, const Basi
} // namespace llvm } // namespace llvm
/// ByteSwap - Byteswap 'Var' if 'Really' is true. Required when the compiler /// ByteSwap - Byteswap 'Var'. Required when the compiler host and target have
/// host and target have different endianness. /// different endianness.
static inline unsigned ByteSwap(unsigned Var, bool Really) { static inline unsigned ByteSwap(unsigned Var) {
if (!Really) return Var;
return ((Var & (255U<< 0U)) << 24U) | return ((Var & (255U<< 0U)) << 24U) |
((Var & (255U<< 8U)) << 8U) | ((Var & (255U<< 8U)) << 8U) |
((Var & (255U<<16U)) >> 8U) | ((Var & (255U<<16U)) >> 8U) |
@ -75,21 +78,19 @@ static unsigned AddCounts(unsigned A, unsigned B) {
// Saturate to the maximum storable value. This could change taken/nottaken // Saturate to the maximum storable value. This could change taken/nottaken
// ratios, but is presumably better than wrapping and thus potentially // ratios, but is presumably better than wrapping and thus potentially
// inverting ratios. // inverting ratios.
unsigned long long tmp = (unsigned long long)A + (unsigned long long)B; uint64_t tmp = (uint64_t)A + (uint64_t)B;
if (tmp > (unsigned long long)ProfileDataLoader::MaxCount) if (tmp > (uint64_t)ProfileDataLoader::MaxCount)
tmp = ProfileDataLoader::MaxCount; tmp = ProfileDataLoader::MaxCount;
return (unsigned)tmp; return (unsigned)tmp;
} }
/// ReadProfilingData - Load 'NumEntries' items of type 'T' from file 'F' /// ReadProfilingData - Load 'NumEntries' items of type 'T' from file 'F'
template <typename T> template <typename T, unsigned N>
static void ReadProfilingData(const char *ToolName, FILE *F, static void ReadProfilingData(const char *ToolName, FILE *F,
std::vector<T> &Data, size_t NumEntries) { SmallVector<T, N> &Data, size_t NumEntries) {
// Read in the block of data... // Read in the block of data...
if (fread(&Data[0], sizeof(T), NumEntries, F) != NumEntries) { if (fread(&Data[0], sizeof(T), NumEntries, F) != NumEntries) {
errs() << ToolName << ": profiling data truncated!\n"; report_fatal_error(std::string(ToolName) + ": Profiling data truncated");
perror(0);
exit(1);
} }
} }
@ -97,46 +98,46 @@ static void ReadProfilingData(const char *ToolName, FILE *F,
/// packet. /// packet.
static unsigned ReadProfilingNumEntries(const char *ToolName, FILE *F, static unsigned ReadProfilingNumEntries(const char *ToolName, FILE *F,
bool ShouldByteSwap) { bool ShouldByteSwap) {
std::vector<unsigned> NumEntries(1); SmallVector<unsigned, 1> NumEntries(1);
ReadProfilingData<unsigned>(ToolName, F, NumEntries, 1); ReadProfilingData<unsigned, 1>(ToolName, F, NumEntries, 1);
return ByteSwap(NumEntries[0], ShouldByteSwap); return ShouldByteSwap ? ByteSwap(NumEntries[0]) : NumEntries[0];
} }
/// ReadProfilingBlock - Read the number of entries in the next profiling data /// ReadProfilingBlock - Read the number of entries in the next profiling data
/// packet and then accumulate the entries into 'Data'. /// packet and then accumulate the entries into 'Data'.
static void ReadProfilingBlock(const char *ToolName, FILE *F, static void ReadProfilingBlock(const char *ToolName, FILE *F,
bool ShouldByteSwap, bool ShouldByteSwap,
std::vector<unsigned> &Data) { SmallVector<unsigned, 32> &Data) {
// Read the number of entries... // Read the number of entries...
unsigned NumEntries = ReadProfilingNumEntries(ToolName, F, ShouldByteSwap); unsigned NumEntries = ReadProfilingNumEntries(ToolName, F, ShouldByteSwap);
// Read in the data. // Read in the data.
std::vector<unsigned> TempSpace(NumEntries); SmallVector<unsigned, 8> TempSpace(NumEntries);
ReadProfilingData<unsigned>(ToolName, F, TempSpace, (size_t)NumEntries); ReadProfilingData<unsigned, 8>(ToolName, F, TempSpace, (size_t)NumEntries);
// Make sure we have enough space ... // Make sure we have enough space ...
if (Data.size() < NumEntries) if (Data.size() < NumEntries)
Data.resize(NumEntries, ProfileDataLoader::Uncounted); Data.resize(NumEntries, ProfileDataLoader::Uncounted);
// Accumulate the data we just read into the existing data. // Accumulate the data we just read into the existing data.
for (unsigned i = 0; i < NumEntries; ++i) { for (unsigned i = 0; i < NumEntries; ++i)
Data[i] = AddCounts(ByteSwap(TempSpace[i], ShouldByteSwap), Data[i]); Data[i] = AddCounts(ShouldByteSwap ? ByteSwap(TempSpace[i]) : TempSpace[i],
} Data[i]);
} }
/// ReadProfilingArgBlock - Read the command line arguments that the progam was /// ReadProfilingArgBlock - Read the command line arguments that the progam was
/// run with when the current profiling data packet(s) were generated. /// run with when the current profiling data packet(s) were generated.
static void ReadProfilingArgBlock(const char *ToolName, FILE *F, static void ReadProfilingArgBlock(const char *ToolName, FILE *F,
bool ShouldByteSwap, bool ShouldByteSwap,
std::vector<std::string> &CommandLines) { SmallVector<std::string, 1> &CommandLines) {
// Read the number of bytes ... // Read the number of bytes ...
unsigned ArgLength = ReadProfilingNumEntries(ToolName, F, ShouldByteSwap); unsigned ArgLength = ReadProfilingNumEntries(ToolName, F, ShouldByteSwap);
// Read in the arguments (if there are any to read). Round up the length to // Read in the arguments (if there are any to read). Round up the length to
// the nearest 4-byte multiple. // the nearest 4-byte multiple.
std::vector<char> Args(ArgLength+4); SmallVector<char, 8> Args(ArgLength+4);
if (ArgLength) if (ArgLength)
ReadProfilingData<char>(ToolName, F, Args, (ArgLength+3) & ~3); ReadProfilingData<char, 8>(ToolName, F, Args, (ArgLength+3) & ~3);
// Store the arguments. // Store the arguments.
CommandLines.push_back(std::string(&Args[0], &Args[ArgLength])); CommandLines.push_back(std::string(&Args[0], &Args[ArgLength]));
@ -145,17 +146,15 @@ static void ReadProfilingArgBlock(const char *ToolName, FILE *F,
const unsigned ProfileDataLoader::Uncounted = ~0U; const unsigned ProfileDataLoader::Uncounted = ~0U;
const unsigned ProfileDataLoader::MaxCount = ~0U - 1U; const unsigned ProfileDataLoader::MaxCount = ~0U - 1U;
/// ProfileDataLoader ctor - Read the specified profiling data file, exiting /// ProfileDataLoader ctor - Read the specified profiling data file, reporting
/// the program if the file is invalid or broken. /// a fatal error if the file is invalid or broken.
ProfileDataLoader::ProfileDataLoader(const char *ToolName, ProfileDataLoader::ProfileDataLoader(const char *ToolName,
const std::string &Filename) const std::string &Filename)
: Filename(Filename) { : Filename(Filename) {
FILE *F = fopen(Filename.c_str(), "rb"); FILE *F = fopen(Filename.c_str(), "rb");
if (F == 0) { if (F == 0)
errs() << ToolName << ": Error opening '" << Filename << "': "; report_fatal_error(std::string(ToolName) + ": Error opening '" +
perror(0); Filename + "': ");
exit(1);
}
// Keep reading packets until we run out of them. // Keep reading packets until we run out of them.
unsigned PacketType; unsigned PacketType;
@ -165,7 +164,7 @@ ProfileDataLoader::ProfileDataLoader(const char *ToolName,
// information. This can happen when the compiler host and target have // information. This can happen when the compiler host and target have
// different endianness. // different endianness.
bool ShouldByteSwap = (char)PacketType == 0; bool ShouldByteSwap = (char)PacketType == 0;
PacketType = ByteSwap(PacketType, ShouldByteSwap); PacketType = ShouldByteSwap ? ByteSwap(PacketType) : PacketType;
switch (PacketType) { switch (PacketType) {
case ArgumentInfo: case ArgumentInfo:
@ -177,8 +176,9 @@ ProfileDataLoader::ProfileDataLoader(const char *ToolName,
break; break;
default: default:
errs() << ToolName << ": Unknown packet type #" << PacketType << "!\n"; report_fatal_error(std::string(ToolName)
exit(1); + ": Unknown profiling packet type");
break;
} }
} }

View File

@ -15,6 +15,7 @@
// //
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#define DEBUG_TYPE "profile-metadata-loader" #define DEBUG_TYPE "profile-metadata-loader"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/BasicBlock.h" #include "llvm/BasicBlock.h"
#include "llvm/InstrTypes.h" #include "llvm/InstrTypes.h"
#include "llvm/Module.h" #include "llvm/Module.h"
@ -30,7 +31,6 @@
#include "llvm/Support/raw_ostream.h" #include "llvm/Support/raw_ostream.h"
#include "llvm/Support/Format.h" #include "llvm/Support/Format.h"
#include "llvm/ADT/Statistic.h" #include "llvm/ADT/Statistic.h"
#include <vector>
using namespace llvm; using namespace llvm;
STATISTIC(NumEdgesRead, "The # of edges read."); STATISTIC(NumEdgesRead, "The # of edges read.");
@ -63,8 +63,8 @@ namespace {
} }
virtual void readEdge(unsigned, ProfileData&, ProfileData::Edge, virtual void readEdge(unsigned, ProfileData&, ProfileData::Edge,
std::vector<unsigned>&); ArrayRef<unsigned>);
virtual unsigned matchEdges(Module&, ProfileData&, std::vector<unsigned>&); virtual unsigned matchEdges(Module&, ProfileData&, ArrayRef<unsigned>);
virtual void setBranchWeightMetadata(Module&, ProfileData&); virtual void setBranchWeightMetadata(Module&, ProfileData&);
virtual bool runOnModule(Module &M); virtual bool runOnModule(Module &M);
@ -92,8 +92,9 @@ ModulePass *llvm::createProfileMetadataLoaderPass(const std::string &Filename) {
/// readEdge - Take the value from a profile counter and assign it to an edge. /// readEdge - Take the value from a profile counter and assign it to an edge.
void ProfileMetadataLoaderPass::readEdge(unsigned ReadCount, void ProfileMetadataLoaderPass::readEdge(unsigned ReadCount,
ProfileData &PB, ProfileData::Edge e, ProfileData &PB, ProfileData::Edge e,
std::vector<unsigned> &Counters) { ArrayRef<unsigned> Counters) {
if (ReadCount < Counters.size()) { if (ReadCount >= Counters.size()) return;
unsigned weight = Counters[ReadCount]; unsigned weight = Counters[ReadCount];
assert(weight != ProfileDataLoader::Uncounted); assert(weight != ProfileDataLoader::Uncounted);
PB.addEdgeWeight(e, weight); PB.addEdgeWeight(e, weight);
@ -101,12 +102,11 @@ void ProfileMetadataLoaderPass::readEdge(unsigned ReadCount,
DEBUG(dbgs() << "-- Read Edge Counter for " << e DEBUG(dbgs() << "-- Read Edge Counter for " << e
<< " (# "<< (ReadCount) << "): " << " (# "<< (ReadCount) << "): "
<< PB.getEdgeWeight(e) << "\n"); << PB.getEdgeWeight(e) << "\n");
}
} }
/// matchEdges - Link every profile counter with an edge. /// matchEdges - Link every profile counter with an edge.
unsigned ProfileMetadataLoaderPass::matchEdges(Module &M, ProfileData &PB, unsigned ProfileMetadataLoaderPass::matchEdges(Module &M, ProfileData &PB,
std::vector<unsigned> &Counters) { ArrayRef<unsigned> Counters) {
if (Counters.size() == 0) return 0; if (Counters.size() == 0) return 0;
unsigned ReadCount = 0; unsigned ReadCount = 0;
@ -147,7 +147,7 @@ void ProfileMetadataLoaderPass::setBranchWeightMetadata(Module &M,
// Load the weights of all edges leading from this terminator. // Load the weights of all edges leading from this terminator.
DEBUG(dbgs() << "-- Terminator with " << NumSuccessors DEBUG(dbgs() << "-- Terminator with " << NumSuccessors
<< " successors:\n"); << " successors:\n");
std::vector<uint32_t> Weights(NumSuccessors); SmallVector<uint32_t, 4> Weights(NumSuccessors);
for (unsigned s = 0 ; s < NumSuccessors ; ++s) { for (unsigned s = 0 ; s < NumSuccessors ; ++s) {
ProfileData::Edge edge = PB.getEdge(BB, TI->getSuccessor(s)); ProfileData::Edge edge = PB.getEdge(BB, TI->getSuccessor(s));
Weights[s] = (uint32_t)PB.getEdgeWeight(edge); Weights[s] = (uint32_t)PB.getEdgeWeight(edge);
@ -172,7 +172,7 @@ bool ProfileMetadataLoaderPass::runOnModule(Module &M) {
ProfileDataLoader PDL("profile-data-loader", Filename); ProfileDataLoader PDL("profile-data-loader", Filename);
ProfileData PB; ProfileData PB;
std::vector<unsigned> Counters = PDL.getRawEdgeCounts(); ArrayRef<unsigned> Counters = PDL.getRawEdgeCounts();
unsigned ReadCount = matchEdges(M, PB, Counters); unsigned ReadCount = matchEdges(M, PB, Counters);