2004-02-11 14:10:05 +08:00
|
|
|
//===- ProfileInfoLoaderPass.cpp - LLVM Pass to load profile info ---------===//
|
2005-04-22 05:13:18 +08:00
|
|
|
//
|
2004-02-11 14:10:05 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 05:13:18 +08:00
|
|
|
//
|
2004-02-11 14:10:05 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements a concrete implementation of profiling information that
|
|
|
|
// loads the information from a profile dump file.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-03-09 06:04:08 +08:00
|
|
|
#include "llvm/BasicBlock.h"
|
|
|
|
#include "llvm/InstrTypes.h"
|
2009-08-05 23:55:56 +08:00
|
|
|
#include "llvm/Module.h"
|
2004-02-11 14:10:05 +08:00
|
|
|
#include "llvm/Pass.h"
|
2005-01-09 06:01:16 +08:00
|
|
|
#include "llvm/Analysis/Passes.h"
|
2004-02-11 14:10:05 +08:00
|
|
|
#include "llvm/Analysis/ProfileInfo.h"
|
|
|
|
#include "llvm/Analysis/ProfileInfoLoader.h"
|
2004-09-02 06:55:40 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2007-02-06 07:42:17 +08:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2006-11-29 06:46:12 +08:00
|
|
|
#include "llvm/Support/Streams.h"
|
2004-02-11 14:10:05 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2008-05-13 08:00:25 +08: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-12 02:21:05 +08:00
|
|
|
|
2008-05-13 08:00:25 +08:00
|
|
|
namespace {
|
2007-02-06 07:42:17 +08:00
|
|
|
class VISIBILITY_HIDDEN LoaderPass : public ModulePass, public ProfileInfo {
|
2004-02-11 14:10:05 +08:00
|
|
|
std::string Filename;
|
|
|
|
public:
|
2007-05-03 09:11:54 +08:00
|
|
|
static char ID; // Class identification, replacement for typeinfo
|
2007-08-01 23:32:29 +08:00
|
|
|
explicit LoaderPass(const std::string &filename = "")
|
2008-09-05 01:05:41 +08:00
|
|
|
: ModulePass(&ID), Filename(filename) {
|
2004-02-12 02:21:05 +08:00
|
|
|
if (filename.empty()) Filename = ProfileInfoFilename;
|
|
|
|
}
|
2004-02-11 14:10:05 +08:00
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual const char *getPassName() const {
|
|
|
|
return "Profiling information loader";
|
|
|
|
}
|
|
|
|
|
|
|
|
/// run - Load the profile information from the specified file.
|
2004-09-20 12:48:05 +08:00
|
|
|
virtual bool runOnModule(Module &M);
|
2004-02-11 14:10:05 +08:00
|
|
|
};
|
2008-05-13 08:00:25 +08:00
|
|
|
} // End of anonymous namespace
|
2005-04-22 05:13:18 +08:00
|
|
|
|
2008-05-13 08:00:25 +08:00
|
|
|
char LoaderPass::ID = 0;
|
|
|
|
static RegisterPass<LoaderPass>
|
|
|
|
X("profile-loader", "Load profile information from llvmprof.out", false, true);
|
2004-02-11 14:10:05 +08:00
|
|
|
|
2008-05-13 08:00:25 +08:00
|
|
|
static RegisterAnalysisGroup<ProfileInfo> Y(X);
|
2004-02-11 14:10:05 +08:00
|
|
|
|
2005-01-10 11:56:27 +08:00
|
|
|
ModulePass *llvm::createProfileLoaderPass() { return new LoaderPass(); }
|
2004-02-11 14:10:05 +08: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);
|
|
|
|
}
|
|
|
|
|
2004-09-20 12:48:05 +08:00
|
|
|
bool LoaderPass::runOnModule(Module &M) {
|
2004-03-09 05:30:35 +08:00
|
|
|
ProfileInfoLoader PIL("profile-loader", Filename, M);
|
2004-03-09 06:04:08 +08:00
|
|
|
EdgeCounts.clear();
|
2005-04-22 05:13:18 +08:00
|
|
|
|
2009-08-05 23:55:56 +08:00
|
|
|
std::vector<unsigned> ECs = PIL.getRawEdgeCounts();
|
2009-08-06 05:51:16 +08:00
|
|
|
std::vector<unsigned> BCs = PIL.getRawBlockCounts();
|
|
|
|
std::vector<unsigned> FCs = PIL.getRawFunctionCounts();
|
2009-08-05 23:55:56 +08:00
|
|
|
// Instrument all of the edges...
|
2009-08-06 05:51:16 +08:00
|
|
|
unsigned ei = 0;
|
|
|
|
unsigned bi = 0;
|
|
|
|
unsigned fi = 0;
|
|
|
|
for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
|
|
|
|
if (F->isDeclaration()) continue;
|
|
|
|
if (fi<FCs.size()) FunctionCounts[F] = FCs[fi++];
|
2009-08-05 23:55:56 +08:00
|
|
|
for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
|
2009-08-06 05:51:16 +08:00
|
|
|
if (bi<BCs.size()) BlockCounts[BB] = BCs[bi++];
|
2009-08-05 23:55:56 +08:00
|
|
|
// Okay, we have to add a counter of each outgoing edge. If the
|
|
|
|
// outgoing edge is not critical don't split it, just insert the counter
|
|
|
|
// in the source or destination of the edge.
|
|
|
|
TerminatorInst *TI = BB->getTerminator();
|
|
|
|
for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) {
|
2009-08-06 05:51:16 +08:00
|
|
|
if (ei < ECs.size())
|
|
|
|
EdgeCounts[std::make_pair(BB, TI->getSuccessor(s))]+= ECs[ei++];
|
2004-03-09 06:04:08 +08:00
|
|
|
}
|
|
|
|
}
|
2009-08-06 05:51:16 +08:00
|
|
|
}
|
2009-08-05 23:55:56 +08:00
|
|
|
|
2009-08-06 05:51:16 +08:00
|
|
|
if (ei != ECs.size()) {
|
2009-08-05 23:55:56 +08:00
|
|
|
cerr << "WARNING: profile information is inconsistent with "
|
|
|
|
<< "the current program!\n";
|
2004-02-12 02:21:05 +08:00
|
|
|
}
|
2004-03-09 06:04:08 +08:00
|
|
|
|
2004-02-11 14:10:05 +08:00
|
|
|
return false;
|
|
|
|
}
|