Move main llvm-prof functionality into a ModulePass.

- To support using the ProfileInfo analysis results instead of accessing the
   ProfileInfoLoader directly.

 - Based on (part of) a patch by Andreas Neustifter.

 - No functionality change.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@75625 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Daniel Dunbar 2009-07-14 07:41:11 +00:00
parent 858cb8a5e0
commit 314fa8e40a

View File

@ -16,7 +16,9 @@
#include "llvm/InstrTypes.h"
#include "llvm/LLVMContext.h"
#include "llvm/Module.h"
#include "llvm/PassManager.h"
#include "llvm/Assembly/AsmAnnotationWriter.h"
#include "llvm/Analysis/ProfileInfo.h"
#include "llvm/Analysis/ProfileInfoLoader.h"
#include "llvm/Bitcode/ReaderWriter.h"
#include "llvm/Support/CommandLine.h"
@ -113,34 +115,30 @@ namespace {
};
}
namespace {
/// ProfileInfoPrinterPass - Helper pass to dump the profile information for
/// a module.
//
// FIXME: This should move elsewhere.
class ProfileInfoPrinterPass : public ModulePass {
ProfileInfoLoader &PIL;
public:
static char ID; // Class identification, replacement for typeinfo.
explicit ProfileInfoPrinterPass(ProfileInfoLoader &_PIL)
: ModulePass(&ID), PIL(_PIL) {}
int main(int argc, char **argv) {
// Print a stack trace if we signal out.
sys::PrintStackTraceOnErrorSignal();
PrettyStackTraceProgram X(argc, argv);
LLVMContext Context;
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
try {
cl::ParseCommandLineOptions(argc, argv, "llvm profile dump decoder\n");
// Read in the bitcode file...
std::string ErrorMessage;
Module *M = 0;
if (MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(BitcodeFile,
&ErrorMessage)) {
M = ParseBitcodeFile(Buffer, Context, &ErrorMessage);
delete Buffer;
}
if (M == 0) {
std::cerr << argv[0] << ": " << BitcodeFile << ": "
<< ErrorMessage << "\n";
return 1;
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
AU.setPreservesAll();
AU.addRequired<ProfileInfo>();
}
// Read the profiling information
ProfileInfoLoader PI(argv[0], ProfileDataFile, *M);
bool runOnModule(Module &M);
};
}
char ProfileInfoPrinterPass::ID = 0;
bool ProfileInfoPrinterPass::runOnModule(Module &M) {
std::map<const Function *, unsigned> FuncFreqs;
std::map<const BasicBlock*, unsigned> BlockFreqs;
std::map<ProfileInfoLoader::Edge, unsigned> EdgeFreqs;
@ -150,7 +148,7 @@ int main(int argc, char **argv) {
// Emit the most frequent function table...
std::vector<std::pair<Function*, unsigned> > FunctionCounts;
PI.getFunctionCounts(FunctionCounts);
PIL.getFunctionCounts(FunctionCounts);
FuncFreqs.insert(FunctionCounts.begin(), FunctionCounts.end());
// Sort by the frequency, backwards.
@ -163,13 +161,13 @@ int main(int argc, char **argv) {
std::cout << "===" << std::string(73, '-') << "===\n"
<< "LLVM profiling output for execution";
if (PI.getNumExecutions() != 1) std::cout << "s";
if (PIL.getNumExecutions() != 1) std::cout << "s";
std::cout << ":\n";
for (unsigned i = 0, e = PI.getNumExecutions(); i != e; ++i) {
for (unsigned i = 0, e = PIL.getNumExecutions(); i != e; ++i) {
std::cout << " ";
if (e != 1) std::cout << i+1 << ". ";
std::cout << PI.getExecution(i) << "\n";
std::cout << PIL.getExecution(i) << "\n";
}
std::cout << "\n===" << std::string(73, '-') << "===\n";
@ -194,9 +192,9 @@ int main(int argc, char **argv) {
// If we have block count information, print out the LLVM module with
// frequency annotations.
if (PI.hasAccurateBlockCounts()) {
if (PIL.hasAccurateBlockCounts()) {
std::vector<std::pair<BasicBlock*, unsigned> > Counts;
PI.getBlockCounts(Counts);
PIL.getBlockCounts(Counts);
TotalExecutions = 0;
for (unsigned i = 0, e = Counts.size(); i != e; ++i)
@ -229,9 +227,9 @@ int main(int argc, char **argv) {
BlockFreqs.insert(Counts.begin(), Counts.end());
}
if (PI.hasAccurateEdgeCounts()) {
if (PIL.hasAccurateEdgeCounts()) {
std::vector<std::pair<ProfileInfoLoader::Edge, unsigned> > Counts;
PI.getEdgeCounts(Counts);
PIL.getEdgeCounts(Counts);
EdgeFreqs.insert(Counts.begin(), Counts.end());
}
@ -242,7 +240,7 @@ int main(int argc, char **argv) {
ProfileAnnotator PA(FuncFreqs, BlockFreqs, EdgeFreqs);
if (FunctionsToPrint.empty() || PrintAllCode)
M->print(std::cout, &PA);
M.print(std::cout, &PA);
else
// Print just a subset of the functions.
for (std::set<Function*>::iterator I = FunctionsToPrint.begin(),
@ -250,11 +248,51 @@ int main(int argc, char **argv) {
(*I)->print(std::cout, &PA);
}
return false;
}
int main(int argc, char **argv) {
// Print a stack trace if we signal out.
sys::PrintStackTraceOnErrorSignal();
PrettyStackTraceProgram X(argc, argv);
LLVMContext Context;
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
try {
cl::ParseCommandLineOptions(argc, argv, "llvm profile dump decoder\n");
// Read in the bitcode file...
std::string ErrorMessage;
Module *M = 0;
if (MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(BitcodeFile,
&ErrorMessage)) {
M = ParseBitcodeFile(Buffer, Context, &ErrorMessage);
delete Buffer;
}
if (M == 0) {
std::cerr << argv[0] << ": " << BitcodeFile << ": "
<< ErrorMessage << "\n";
return 1;
}
// Read the profiling information. This is redundant since we load it again
// using the standard profile info provider pass, but for now this gives us
// access to additional information not exposed via the ProfileInfo
// interface.
ProfileInfoLoader PIL(argv[0], ProfileDataFile, *M);
// Run the printer pass.
PassManager PassMgr;
PassMgr.add(createProfileLoaderPass(ProfileDataFile));
PassMgr.add(new ProfileInfoPrinterPass(PIL));
PassMgr.run(*M);
return 0;
} catch (const std::string& msg) {
std::cerr << argv[0] << ": " << msg << "\n";
} catch (...) {
std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
}
return 1;
}