2004-02-11 06:10:05 +00:00
|
|
|
//===- ProfileInfoLoaderPass.cpp - LLVM Pass to load profile info ---------===//
|
2005-04-21 21:13:18 +00:00
|
|
|
//
|
2004-02-11 06:10:05 +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 06:10:05 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements a concrete implementation of profiling information that
|
|
|
|
// loads the information from a profile dump file.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2009-09-01 19:08:51 +00:00
|
|
|
#define DEBUG_TYPE "profile-loader"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/Analysis/Passes.h"
|
|
|
|
#include "llvm/ADT/SmallSet.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#include "llvm/Analysis/ProfileInfo.h"
|
|
|
|
#include "llvm/Analysis/ProfileInfoLoader.h"
|
2004-03-08 22:04:08 +00:00
|
|
|
#include "llvm/BasicBlock.h"
|
|
|
|
#include "llvm/InstrTypes.h"
|
2009-08-05 15:55:56 +00:00
|
|
|
#include "llvm/Module.h"
|
2004-02-11 06:10:05 +00:00
|
|
|
#include "llvm/Pass.h"
|
2009-09-01 19:08:51 +00:00
|
|
|
#include "llvm/Support/CFG.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2009-09-01 19:08:51 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/Format.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2009-09-01 19:08:51 +00:00
|
|
|
#include <set>
|
2004-02-11 06:10:05 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2009-09-01 19:08:51 +00:00
|
|
|
STATISTIC(NumEdgesRead, "The # of edges read.");
|
|
|
|
|
2008-05-13 00:00:25 +00:00
|
|
|
static cl::opt<std::string>
|
|
|
|
ProfileInfoFilename("profile-info-file", cl::init("llvmprof.out"),
|
|
|
|
cl::value_desc("filename"),
|
|
|
|
cl::desc("Profile file loaded by -profile-loader"));
|
2004-02-11 18:21:05 +00:00
|
|
|
|
2008-05-13 00:00:25 +00:00
|
|
|
namespace {
|
2009-10-25 06:33:48 +00:00
|
|
|
class LoaderPass : public ModulePass, public ProfileInfo {
|
2004-02-11 06:10:05 +00:00
|
|
|
std::string Filename;
|
2009-09-01 19:08:51 +00:00
|
|
|
std::set<Edge> SpanningTree;
|
|
|
|
std::set<const BasicBlock*> BBisUnvisited;
|
2009-09-10 07:12:35 +00:00
|
|
|
unsigned ReadCount;
|
2004-02-11 06:10:05 +00:00
|
|
|
public:
|
2007-05-03 01:11:54 +00:00
|
|
|
static char ID; // Class identification, replacement for typeinfo
|
2007-08-01 15:32:29 +00:00
|
|
|
explicit LoaderPass(const std::string &filename = "")
|
2010-08-06 18:33:48 +00:00
|
|
|
: ModulePass(ID), Filename(filename) {
|
2010-10-19 17:21:58 +00:00
|
|
|
initializeLoaderPassPass(*PassRegistry::getPassRegistry());
|
2004-02-11 18:21:05 +00:00
|
|
|
if (filename.empty()) Filename = ProfileInfoFilename;
|
|
|
|
}
|
2004-02-11 06:10:05 +00:00
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual const char *getPassName() const {
|
|
|
|
return "Profiling information loader";
|
|
|
|
}
|
|
|
|
|
2009-09-01 19:08:51 +00:00
|
|
|
// recurseBasicBlock() - Calculates the edge weights for as much basic
|
|
|
|
// blocks as possbile.
|
|
|
|
virtual void recurseBasicBlock(const BasicBlock *BB);
|
2009-11-02 02:55:39 +00:00
|
|
|
virtual void readEdgeOrRemember(Edge, Edge&, unsigned &, double &);
|
2009-09-10 07:12:35 +00:00
|
|
|
virtual void readEdge(ProfileInfo::Edge, std::vector<unsigned>&);
|
2009-09-01 19:08:51 +00:00
|
|
|
|
2010-01-20 20:09:02 +00:00
|
|
|
/// getAdjustedAnalysisPointer - This method is used when a pass implements
|
|
|
|
/// an analysis interface through multiple inheritance. If needed, it
|
|
|
|
/// should override this to adjust the this pointer as needed for the
|
|
|
|
/// specified pass info.
|
2010-08-06 18:33:48 +00:00
|
|
|
virtual void *getAdjustedAnalysisPointer(AnalysisID PI) {
|
|
|
|
if (PI == &ProfileInfo::ID)
|
2010-01-20 20:09:02 +00:00
|
|
|
return (ProfileInfo*)this;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2004-02-11 06:10:05 +00:00
|
|
|
/// run - Load the profile information from the specified file.
|
2004-09-20 04:48:05 +00:00
|
|
|
virtual bool runOnModule(Module &M);
|
2004-02-11 06:10:05 +00:00
|
|
|
};
|
2008-05-13 00:00:25 +00:00
|
|
|
} // End of anonymous namespace
|
2005-04-21 21:13:18 +00:00
|
|
|
|
2008-05-13 00:00:25 +00:00
|
|
|
char LoaderPass::ID = 0;
|
2010-08-23 17:52:01 +00:00
|
|
|
INITIALIZE_AG_PASS(LoaderPass, ProfileInfo, "profile-loader",
|
2010-10-07 22:25:06 +00:00
|
|
|
"Load profile information from llvmprof.out", false, true, false)
|
2004-02-11 06:10:05 +00:00
|
|
|
|
2010-08-06 18:33:48 +00:00
|
|
|
char &llvm::ProfileLoaderPassID = LoaderPass::ID;
|
2009-12-03 11:00:37 +00:00
|
|
|
|
2005-01-10 03:56:27 +00:00
|
|
|
ModulePass *llvm::createProfileLoaderPass() { return new LoaderPass(); }
|
2004-02-11 06:10:05 +00:00
|
|
|
|
|
|
|
/// createProfileLoaderPass - This function returns a Pass that loads the
|
|
|
|
/// profiling information for the module from the specified filename, making it
|
|
|
|
/// available to the optimizers.
|
|
|
|
Pass *llvm::createProfileLoaderPass(const std::string &Filename) {
|
|
|
|
return new LoaderPass(Filename);
|
|
|
|
}
|
|
|
|
|
2009-09-01 19:08:51 +00:00
|
|
|
void LoaderPass::readEdgeOrRemember(Edge edge, Edge &tocalc,
|
2009-11-02 02:55:39 +00:00
|
|
|
unsigned &uncalc, double &count) {
|
2009-09-01 19:08:51 +00:00
|
|
|
double w;
|
|
|
|
if ((w = getEdgeWeight(edge)) == MissingValue) {
|
|
|
|
tocalc = edge;
|
|
|
|
uncalc++;
|
|
|
|
} else {
|
|
|
|
count+=w;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// recurseBasicBlock - Visits all neighbours of a block and then tries to
|
|
|
|
// calculate the missing edge values.
|
|
|
|
void LoaderPass::recurseBasicBlock(const BasicBlock *BB) {
|
|
|
|
|
|
|
|
// break recursion if already visited
|
|
|
|
if (BBisUnvisited.find(BB) == BBisUnvisited.end()) return;
|
|
|
|
BBisUnvisited.erase(BB);
|
|
|
|
if (!BB) return;
|
|
|
|
|
|
|
|
for (succ_const_iterator bbi = succ_begin(BB), bbe = succ_end(BB);
|
|
|
|
bbi != bbe; ++bbi) {
|
|
|
|
recurseBasicBlock(*bbi);
|
|
|
|
}
|
2010-03-25 23:25:28 +00:00
|
|
|
for (const_pred_iterator bbi = pred_begin(BB), bbe = pred_end(BB);
|
2009-09-01 19:08:51 +00:00
|
|
|
bbi != bbe; ++bbi) {
|
|
|
|
recurseBasicBlock(*bbi);
|
|
|
|
}
|
|
|
|
|
2009-12-03 11:00:37 +00:00
|
|
|
Edge tocalc;
|
|
|
|
if (CalculateMissingEdge(BB, tocalc)) {
|
|
|
|
SpanningTree.erase(tocalc);
|
2009-09-01 19:08:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-10 07:12:35 +00:00
|
|
|
void LoaderPass::readEdge(ProfileInfo::Edge e,
|
|
|
|
std::vector<unsigned> &ECs) {
|
|
|
|
if (ReadCount < ECs.size()) {
|
|
|
|
double weight = ECs[ReadCount++];
|
2009-09-16 11:35:50 +00:00
|
|
|
if (weight != ProfileInfoLoader::Uncounted) {
|
|
|
|
// Here the data realm changes from the unsigned of the file to the
|
|
|
|
// double of the ProfileInfo. This conversion is save because we know
|
|
|
|
// that everything thats representable in unsinged is also representable
|
|
|
|
// in double.
|
|
|
|
EdgeInformation[getFunction(e)][e] += (double)weight;
|
|
|
|
|
2009-12-23 21:58:29 +00:00
|
|
|
DEBUG(dbgs() << "--Read Edge Counter for " << e
|
2009-09-10 07:12:35 +00:00
|
|
|
<< " (# "<< (ReadCount-1) << "): "
|
|
|
|
<< (unsigned)getEdgeWeight(e) << "\n");
|
|
|
|
} else {
|
|
|
|
// This happens only if reading optimal profiling information, not when
|
|
|
|
// reading regular profiling information.
|
|
|
|
SpanningTree.insert(e);
|
|
|
|
}
|
2009-09-01 19:08:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-09-20 04:48:05 +00:00
|
|
|
bool LoaderPass::runOnModule(Module &M) {
|
2012-06-06 19:47:08 +00:00
|
|
|
ProfileInfoLoader PIL("profile-loader", Filename);
|
2005-04-21 21:13:18 +00:00
|
|
|
|
2009-08-08 17:43:09 +00:00
|
|
|
EdgeInformation.clear();
|
2009-09-10 07:12:35 +00:00
|
|
|
std::vector<unsigned> Counters = PIL.getRawEdgeCounts();
|
|
|
|
if (Counters.size() > 0) {
|
|
|
|
ReadCount = 0;
|
2009-08-08 17:43:09 +00:00
|
|
|
for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
|
|
|
|
if (F->isDeclaration()) continue;
|
2011-11-15 16:27:03 +00:00
|
|
|
DEBUG(dbgs() << "Working on " << F->getName() << "\n");
|
2009-09-10 07:12:35 +00:00
|
|
|
readEdge(getEdge(0,&F->getEntryBlock()), Counters);
|
2009-08-08 17:43:09 +00:00
|
|
|
for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
|
|
|
|
TerminatorInst *TI = BB->getTerminator();
|
|
|
|
for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) {
|
2009-09-10 07:12:35 +00:00
|
|
|
readEdge(getEdge(BB,TI->getSuccessor(s)), Counters);
|
2009-08-08 17:43:09 +00:00
|
|
|
}
|
2004-03-08 22:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-09-10 07:12:35 +00:00
|
|
|
if (ReadCount != Counters.size()) {
|
2009-12-23 23:00:50 +00:00
|
|
|
errs() << "WARNING: profile information is inconsistent with "
|
2009-08-23 07:33:14 +00:00
|
|
|
<< "the current program!\n";
|
2009-08-08 17:43:09 +00:00
|
|
|
}
|
2009-09-10 07:12:35 +00:00
|
|
|
NumEdgesRead = ReadCount;
|
2009-09-01 19:08:51 +00:00
|
|
|
}
|
|
|
|
|
2009-09-10 07:12:35 +00:00
|
|
|
Counters = PIL.getRawOptimalEdgeCounts();
|
|
|
|
if (Counters.size() > 0) {
|
|
|
|
ReadCount = 0;
|
2009-09-01 19:08:51 +00:00
|
|
|
for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
|
|
|
|
if (F->isDeclaration()) continue;
|
2011-11-15 16:27:03 +00:00
|
|
|
DEBUG(dbgs() << "Working on " << F->getName() << "\n");
|
2009-09-10 07:12:35 +00:00
|
|
|
readEdge(getEdge(0,&F->getEntryBlock()), Counters);
|
2009-09-01 19:08:51 +00:00
|
|
|
for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
|
|
|
|
TerminatorInst *TI = BB->getTerminator();
|
|
|
|
if (TI->getNumSuccessors() == 0) {
|
2009-09-10 07:12:35 +00:00
|
|
|
readEdge(getEdge(BB,0), Counters);
|
2009-09-01 19:08:51 +00:00
|
|
|
}
|
|
|
|
for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) {
|
2009-09-10 07:12:35 +00:00
|
|
|
readEdge(getEdge(BB,TI->getSuccessor(s)), Counters);
|
2009-09-01 19:08:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
while (SpanningTree.size() > 0) {
|
2009-12-03 11:00:37 +00:00
|
|
|
|
2009-09-01 19:08:51 +00:00
|
|
|
unsigned size = SpanningTree.size();
|
2009-12-03 11:00:37 +00:00
|
|
|
|
2009-09-01 19:08:51 +00:00
|
|
|
BBisUnvisited.clear();
|
|
|
|
for (std::set<Edge>::iterator ei = SpanningTree.begin(),
|
|
|
|
ee = SpanningTree.end(); ei != ee; ++ei) {
|
|
|
|
BBisUnvisited.insert(ei->first);
|
|
|
|
BBisUnvisited.insert(ei->second);
|
|
|
|
}
|
|
|
|
while (BBisUnvisited.size() > 0) {
|
|
|
|
recurseBasicBlock(*BBisUnvisited.begin());
|
|
|
|
}
|
2009-12-03 11:00:37 +00:00
|
|
|
|
2009-09-01 19:08:51 +00:00
|
|
|
if (SpanningTree.size() == size) {
|
2009-12-23 21:58:29 +00:00
|
|
|
DEBUG(dbgs()<<"{");
|
2009-09-01 19:08:51 +00:00
|
|
|
for (std::set<Edge>::iterator ei = SpanningTree.begin(),
|
|
|
|
ee = SpanningTree.end(); ei != ee; ++ei) {
|
2009-12-23 21:58:29 +00:00
|
|
|
DEBUG(dbgs()<< *ei <<",");
|
2009-09-01 19:08:51 +00:00
|
|
|
}
|
|
|
|
assert(0 && "No edge calculated!");
|
|
|
|
}
|
2009-12-03 11:00:37 +00:00
|
|
|
|
2009-09-01 19:08:51 +00:00
|
|
|
}
|
|
|
|
}
|
2009-09-10 07:12:35 +00:00
|
|
|
if (ReadCount != Counters.size()) {
|
2009-12-23 23:00:50 +00:00
|
|
|
errs() << "WARNING: profile information is inconsistent with "
|
2009-09-01 19:08:51 +00:00
|
|
|
<< "the current program!\n";
|
|
|
|
}
|
2009-09-10 07:12:35 +00:00
|
|
|
NumEdgesRead = ReadCount;
|
2009-08-08 17:43:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BlockInformation.clear();
|
2009-09-10 07:12:35 +00:00
|
|
|
Counters = PIL.getRawBlockCounts();
|
|
|
|
if (Counters.size() > 0) {
|
|
|
|
ReadCount = 0;
|
2009-08-08 17:43:09 +00:00
|
|
|
for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
|
|
|
|
if (F->isDeclaration()) continue;
|
|
|
|
for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
|
2009-09-10 07:12:35 +00:00
|
|
|
if (ReadCount < Counters.size())
|
2009-09-16 11:35:50 +00:00
|
|
|
// Here the data realm changes from the unsigned of the file to the
|
|
|
|
// double of the ProfileInfo. This conversion is save because we know
|
|
|
|
// that everything thats representable in unsinged is also
|
|
|
|
// representable in double.
|
|
|
|
BlockInformation[F][BB] = (double)Counters[ReadCount++];
|
2009-08-08 17:43:09 +00:00
|
|
|
}
|
2009-09-10 07:12:35 +00:00
|
|
|
if (ReadCount != Counters.size()) {
|
2009-12-23 23:00:50 +00:00
|
|
|
errs() << "WARNING: profile information is inconsistent with "
|
2009-08-23 07:33:14 +00:00
|
|
|
<< "the current program!\n";
|
2009-08-08 17:43:09 +00:00
|
|
|
}
|
2009-08-05 21:51:16 +00:00
|
|
|
}
|
2009-08-08 17:43:09 +00:00
|
|
|
|
|
|
|
FunctionInformation.clear();
|
2009-09-10 07:12:35 +00:00
|
|
|
Counters = PIL.getRawFunctionCounts();
|
|
|
|
if (Counters.size() > 0) {
|
|
|
|
ReadCount = 0;
|
2009-08-08 17:43:09 +00:00
|
|
|
for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
|
|
|
|
if (F->isDeclaration()) continue;
|
2009-09-10 07:12:35 +00:00
|
|
|
if (ReadCount < Counters.size())
|
2009-09-16 11:35:50 +00:00
|
|
|
// Here the data realm changes from the unsigned of the file to the
|
|
|
|
// double of the ProfileInfo. This conversion is save because we know
|
|
|
|
// that everything thats representable in unsinged is also
|
|
|
|
// representable in double.
|
|
|
|
FunctionInformation[F] = (double)Counters[ReadCount++];
|
2009-08-08 17:43:09 +00:00
|
|
|
}
|
2009-09-10 07:12:35 +00:00
|
|
|
if (ReadCount != Counters.size()) {
|
2009-12-23 23:00:50 +00:00
|
|
|
errs() << "WARNING: profile information is inconsistent with "
|
2009-08-23 07:33:14 +00:00
|
|
|
<< "the current program!\n";
|
2009-08-08 17:43:09 +00:00
|
|
|
}
|
2004-02-11 18:21:05 +00:00
|
|
|
}
|
2004-03-08 22:04:08 +00:00
|
|
|
|
2004-02-11 06:10:05 +00:00
|
|
|
return false;
|
|
|
|
}
|