2003-10-28 19:16:35 +00:00
|
|
|
//===- llvm-prof.cpp - Read in and process llvmprof.out data files --------===//
|
2005-04-22 00:00:37 +00:00
|
|
|
//
|
2003-10-28 19:16:35 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:44:31 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 00:00:37 +00:00
|
|
|
//
|
2003-10-28 19:16:35 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This tools is meant for use with the various LLVM profiling instrumentation
|
|
|
|
// passes. It reads in the data file produced by executing an instrumented
|
|
|
|
// program, and outputs a nice report.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-03-08 20:04:32 +00:00
|
|
|
#include "llvm/InstrTypes.h"
|
2009-07-01 16:58:40 +00:00
|
|
|
#include "llvm/LLVMContext.h"
|
2003-10-30 23:42:09 +00:00
|
|
|
#include "llvm/Module.h"
|
2009-07-14 07:41:11 +00:00
|
|
|
#include "llvm/PassManager.h"
|
2003-10-30 23:42:09 +00:00
|
|
|
#include "llvm/Assembly/AsmAnnotationWriter.h"
|
2009-07-14 07:41:11 +00:00
|
|
|
#include "llvm/Analysis/ProfileInfo.h"
|
2004-02-11 05:56:07 +00:00
|
|
|
#include "llvm/Analysis/ProfileInfoLoader.h"
|
2007-05-06 04:43:00 +00:00
|
|
|
#include "llvm/Bitcode/ReaderWriter.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2006-12-06 01:18:01 +00:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2007-05-06 04:43:00 +00:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2009-03-06 05:34:10 +00:00
|
|
|
#include "llvm/Support/PrettyStackTrace.h"
|
2008-08-23 22:23:09 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2004-05-27 05:41:36 +00:00
|
|
|
#include "llvm/System/Signals.h"
|
2007-03-05 00:00:42 +00:00
|
|
|
#include <algorithm>
|
2004-07-04 12:20:55 +00:00
|
|
|
#include <iostream>
|
2005-12-29 21:13:45 +00:00
|
|
|
#include <iomanip>
|
2003-10-28 21:25:23 +00:00
|
|
|
#include <map>
|
2003-10-30 23:42:09 +00:00
|
|
|
#include <set>
|
2003-10-28 19:16:35 +00:00
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2003-10-28 19:16:35 +00:00
|
|
|
namespace {
|
2005-12-30 09:07:29 +00:00
|
|
|
cl::opt<std::string>
|
2007-07-05 17:07:56 +00:00
|
|
|
BitcodeFile(cl::Positional, cl::desc("<program bitcode file>"),
|
|
|
|
cl::Required);
|
2003-10-28 19:16:35 +00:00
|
|
|
|
2005-12-30 09:07:29 +00:00
|
|
|
cl::opt<std::string>
|
2003-10-28 19:16:35 +00:00
|
|
|
ProfileDataFile(cl::Positional, cl::desc("<llvmprof.out file>"),
|
|
|
|
cl::Optional, cl::init("llvmprof.out"));
|
2003-10-30 23:42:09 +00:00
|
|
|
|
|
|
|
cl::opt<bool>
|
|
|
|
PrintAnnotatedLLVM("annotated-llvm",
|
|
|
|
cl::desc("Print LLVM code with frequency annotations"));
|
|
|
|
cl::alias PrintAnnotated2("A", cl::desc("Alias for --annotated-llvm"),
|
|
|
|
cl::aliasopt(PrintAnnotatedLLVM));
|
2003-11-06 20:29:25 +00:00
|
|
|
cl::opt<bool>
|
|
|
|
PrintAllCode("print-all-code",
|
|
|
|
cl::desc("Print annotated code for the entire program"));
|
2003-10-28 19:16:35 +00:00
|
|
|
}
|
|
|
|
|
2003-10-28 21:08:18 +00:00
|
|
|
// PairSecondSort - A sorting predicate to sort by the second element of a pair.
|
|
|
|
template<class T>
|
2003-10-29 21:41:17 +00:00
|
|
|
struct PairSecondSortReverse
|
2005-12-30 09:07:29 +00:00
|
|
|
: public std::binary_function<std::pair<T, unsigned>,
|
|
|
|
std::pair<T, unsigned>, bool> {
|
|
|
|
bool operator()(const std::pair<T, unsigned> &LHS,
|
|
|
|
const std::pair<T, unsigned> &RHS) const {
|
2003-10-29 21:41:17 +00:00
|
|
|
return LHS.second > RHS.second;
|
2003-10-28 21:08:18 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2003-10-30 23:42:09 +00:00
|
|
|
namespace {
|
|
|
|
class ProfileAnnotator : public AssemblyAnnotationWriter {
|
2005-12-30 09:07:29 +00:00
|
|
|
std::map<const Function *, unsigned> &FuncFreqs;
|
|
|
|
std::map<const BasicBlock*, unsigned> &BlockFreqs;
|
|
|
|
std::map<ProfileInfoLoader::Edge, unsigned> &EdgeFreqs;
|
2003-10-30 23:42:09 +00:00
|
|
|
public:
|
2005-12-30 09:07:29 +00:00
|
|
|
ProfileAnnotator(std::map<const Function *, unsigned> &FF,
|
|
|
|
std::map<const BasicBlock*, unsigned> &BF,
|
|
|
|
std::map<ProfileInfoLoader::Edge, unsigned> &EF)
|
2004-03-08 20:04:32 +00:00
|
|
|
: FuncFreqs(FF), BlockFreqs(BF), EdgeFreqs(EF) {}
|
2003-10-30 23:42:09 +00:00
|
|
|
|
2008-08-23 22:23:09 +00:00
|
|
|
virtual void emitFunctionAnnot(const Function *F, raw_ostream &OS) {
|
2003-10-30 23:42:09 +00:00
|
|
|
OS << ";;; %" << F->getName() << " called " << FuncFreqs[F]
|
|
|
|
<< " times.\n;;;\n";
|
|
|
|
}
|
2004-03-08 20:04:32 +00:00
|
|
|
virtual void emitBasicBlockStartAnnot(const BasicBlock *BB,
|
2008-08-23 22:23:09 +00:00
|
|
|
raw_ostream &OS) {
|
2003-10-30 23:44:28 +00:00
|
|
|
if (BlockFreqs.empty()) return;
|
2009-07-02 00:17:47 +00:00
|
|
|
std::map<const BasicBlock *, unsigned>::const_iterator I =
|
|
|
|
BlockFreqs.find(BB);
|
|
|
|
if (I != BlockFreqs.end())
|
|
|
|
OS << "\t;;; Basic block executed " << I->second << " times.\n";
|
2003-10-30 23:42:09 +00:00
|
|
|
else
|
2004-03-08 20:04:32 +00:00
|
|
|
OS << "\t;;; Never executed!\n";
|
|
|
|
}
|
|
|
|
|
2008-08-23 22:23:09 +00:00
|
|
|
virtual void emitBasicBlockEndAnnot(const BasicBlock *BB, raw_ostream &OS) {
|
2004-03-08 20:04:32 +00:00
|
|
|
if (EdgeFreqs.empty()) return;
|
|
|
|
|
|
|
|
// Figure out how many times each successor executed.
|
2005-12-30 09:07:29 +00:00
|
|
|
std::vector<std::pair<const BasicBlock*, unsigned> > SuccCounts;
|
2004-03-08 20:04:32 +00:00
|
|
|
const TerminatorInst *TI = BB->getTerminator();
|
2005-04-22 00:00:37 +00:00
|
|
|
|
2005-12-30 09:07:29 +00:00
|
|
|
std::map<ProfileInfoLoader::Edge, unsigned>::iterator I =
|
|
|
|
EdgeFreqs.lower_bound(std::make_pair(const_cast<BasicBlock*>(BB), 0U));
|
2004-03-08 20:04:32 +00:00
|
|
|
for (; I != EdgeFreqs.end() && I->first.first == BB; ++I)
|
|
|
|
if (I->second)
|
2005-12-30 09:07:29 +00:00
|
|
|
SuccCounts.push_back(std::make_pair(TI->getSuccessor(I->first.second),
|
2004-03-08 20:04:32 +00:00
|
|
|
I->second));
|
|
|
|
if (!SuccCounts.empty()) {
|
|
|
|
OS << "\t;;; Out-edge counts:";
|
|
|
|
for (unsigned i = 0, e = SuccCounts.size(); i != e; ++i)
|
|
|
|
OS << " [" << SuccCounts[i].second << " -> "
|
|
|
|
<< SuccCounts[i].first->getName() << "]";
|
|
|
|
OS << "\n";
|
|
|
|
}
|
2003-10-30 23:42:09 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2009-07-14 07:41:11 +00:00
|
|
|
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) {}
|
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
AU.addRequired<ProfileInfo>();
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
// Output a report. Eventually, there will be multiple reports selectable on
|
|
|
|
// the command line, for now, just keep things simple.
|
|
|
|
|
|
|
|
// Emit the most frequent function table...
|
|
|
|
std::vector<std::pair<Function*, unsigned> > FunctionCounts;
|
|
|
|
PIL.getFunctionCounts(FunctionCounts);
|
|
|
|
FuncFreqs.insert(FunctionCounts.begin(), FunctionCounts.end());
|
|
|
|
|
|
|
|
// Sort by the frequency, backwards.
|
|
|
|
sort(FunctionCounts.begin(), FunctionCounts.end(),
|
|
|
|
PairSecondSortReverse<Function*>());
|
|
|
|
|
|
|
|
uint64_t TotalExecutions = 0;
|
|
|
|
for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i)
|
|
|
|
TotalExecutions += FunctionCounts[i].second;
|
|
|
|
|
|
|
|
std::cout << "===" << std::string(73, '-') << "===\n"
|
|
|
|
<< "LLVM profiling output for execution";
|
|
|
|
if (PIL.getNumExecutions() != 1) std::cout << "s";
|
|
|
|
std::cout << ":\n";
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = PIL.getNumExecutions(); i != e; ++i) {
|
|
|
|
std::cout << " ";
|
|
|
|
if (e != 1) std::cout << i+1 << ". ";
|
|
|
|
std::cout << PIL.getExecution(i) << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cout << "\n===" << std::string(73, '-') << "===\n";
|
|
|
|
std::cout << "Function execution frequencies:\n\n";
|
|
|
|
|
|
|
|
// Print out the function frequencies...
|
|
|
|
std::cout << " ## Frequency\n";
|
|
|
|
for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i) {
|
|
|
|
if (FunctionCounts[i].second == 0) {
|
|
|
|
std::cout << "\n NOTE: " << e-i << " function" <<
|
|
|
|
(e-i-1 ? "s were" : " was") << " never executed!\n";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cout << std::setw(3) << i+1 << ". "
|
|
|
|
<< std::setw(5) << FunctionCounts[i].second << "/"
|
|
|
|
<< TotalExecutions << " "
|
2009-07-25 00:43:31 +00:00
|
|
|
<< FunctionCounts[i].first->getNameStr() << "\n";
|
2009-07-14 07:41:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::set<Function*> FunctionsToPrint;
|
|
|
|
|
|
|
|
// If we have block count information, print out the LLVM module with
|
|
|
|
// frequency annotations.
|
|
|
|
if (PIL.hasAccurateBlockCounts()) {
|
|
|
|
std::vector<std::pair<BasicBlock*, unsigned> > Counts;
|
|
|
|
PIL.getBlockCounts(Counts);
|
|
|
|
|
|
|
|
TotalExecutions = 0;
|
|
|
|
for (unsigned i = 0, e = Counts.size(); i != e; ++i)
|
|
|
|
TotalExecutions += Counts[i].second;
|
|
|
|
|
|
|
|
// Sort by the frequency, backwards.
|
|
|
|
sort(Counts.begin(), Counts.end(),
|
|
|
|
PairSecondSortReverse<BasicBlock*>());
|
|
|
|
|
|
|
|
std::cout << "\n===" << std::string(73, '-') << "===\n";
|
|
|
|
std::cout << "Top 20 most frequently executed basic blocks:\n\n";
|
|
|
|
|
|
|
|
// Print out the function frequencies...
|
|
|
|
std::cout <<" ## %% \tFrequency\n";
|
|
|
|
unsigned BlocksToPrint = Counts.size();
|
|
|
|
if (BlocksToPrint > 20) BlocksToPrint = 20;
|
|
|
|
for (unsigned i = 0; i != BlocksToPrint; ++i) {
|
|
|
|
if (Counts[i].second == 0) break;
|
|
|
|
Function *F = Counts[i].first->getParent();
|
|
|
|
std::cout << std::setw(3) << i+1 << ". "
|
|
|
|
<< std::setw(5) << std::setprecision(2)
|
|
|
|
<< Counts[i].second/(double)TotalExecutions*100 << "% "
|
|
|
|
<< std::setw(5) << Counts[i].second << "/"
|
|
|
|
<< TotalExecutions << "\t"
|
2009-07-25 00:43:31 +00:00
|
|
|
<< F->getNameStr() << "() - "
|
|
|
|
<< Counts[i].first->getNameStr() << "\n";
|
2009-07-14 07:41:11 +00:00
|
|
|
FunctionsToPrint.insert(F);
|
|
|
|
}
|
|
|
|
|
|
|
|
BlockFreqs.insert(Counts.begin(), Counts.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PIL.hasAccurateEdgeCounts()) {
|
|
|
|
std::vector<std::pair<ProfileInfoLoader::Edge, unsigned> > Counts;
|
|
|
|
PIL.getEdgeCounts(Counts);
|
|
|
|
EdgeFreqs.insert(Counts.begin(), Counts.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PrintAnnotatedLLVM || PrintAllCode) {
|
|
|
|
std::cout << "\n===" << std::string(73, '-') << "===\n";
|
|
|
|
std::cout << "Annotated LLVM code for the module:\n\n";
|
|
|
|
|
|
|
|
ProfileAnnotator PA(FuncFreqs, BlockFreqs, EdgeFreqs);
|
|
|
|
|
|
|
|
if (FunctionsToPrint.empty() || PrintAllCode)
|
|
|
|
M.print(std::cout, &PA);
|
|
|
|
else
|
|
|
|
// Print just a subset of the functions.
|
|
|
|
for (std::set<Function*>::iterator I = FunctionsToPrint.begin(),
|
|
|
|
E = FunctionsToPrint.end(); I != E; ++I)
|
|
|
|
(*I)->print(std::cout, &PA);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2003-10-28 21:08:18 +00:00
|
|
|
|
2003-10-28 19:16:35 +00:00
|
|
|
int main(int argc, char **argv) {
|
2009-03-06 05:34:10 +00:00
|
|
|
// Print a stack trace if we signal out.
|
|
|
|
sys::PrintStackTraceOnErrorSignal();
|
|
|
|
PrettyStackTraceProgram X(argc, argv);
|
2009-07-01 16:58:40 +00:00
|
|
|
|
2009-07-15 22:16:10 +00:00
|
|
|
LLVMContext &Context = getGlobalContext();
|
2009-03-06 05:34:10 +00:00
|
|
|
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
|
2004-12-30 05:36:08 +00:00
|
|
|
try {
|
2007-10-08 15:45:12 +00:00
|
|
|
cl::ParseCommandLineOptions(argc, argv, "llvm profile dump decoder\n");
|
2004-12-30 05:36:08 +00:00
|
|
|
|
2007-07-05 17:07:56 +00:00
|
|
|
// Read in the bitcode file...
|
2005-12-30 09:07:29 +00:00
|
|
|
std::string ErrorMessage;
|
2007-05-07 18:50:07 +00:00
|
|
|
Module *M = 0;
|
2007-07-05 17:07:56 +00:00
|
|
|
if (MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(BitcodeFile,
|
2007-05-06 23:45:49 +00:00
|
|
|
&ErrorMessage)) {
|
2009-07-01 21:22:36 +00:00
|
|
|
M = ParseBitcodeFile(Buffer, Context, &ErrorMessage);
|
2007-05-06 23:45:49 +00:00
|
|
|
delete Buffer;
|
|
|
|
}
|
2004-12-30 05:36:08 +00:00
|
|
|
if (M == 0) {
|
2009-07-15 16:35:29 +00:00
|
|
|
errs() << argv[0] << ": " << BitcodeFile << ": "
|
2005-12-30 09:07:29 +00:00
|
|
|
<< ErrorMessage << "\n";
|
2004-12-30 05:36:08 +00:00
|
|
|
return 1;
|
2003-10-28 22:30:37 +00:00
|
|
|
}
|
|
|
|
|
2009-07-14 07:41:11 +00:00
|
|
|
// 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);
|
2003-10-29 21:41:17 +00:00
|
|
|
|
2009-07-14 07:41:11 +00:00
|
|
|
// Run the printer pass.
|
|
|
|
PassManager PassMgr;
|
|
|
|
PassMgr.add(createProfileLoaderPass(ProfileDataFile));
|
|
|
|
PassMgr.add(new ProfileInfoPrinterPass(PIL));
|
|
|
|
PassMgr.run(*M);
|
2004-12-30 05:36:08 +00:00
|
|
|
|
|
|
|
return 0;
|
2005-12-30 09:07:29 +00:00
|
|
|
} catch (const std::string& msg) {
|
2009-07-15 16:35:29 +00:00
|
|
|
errs() << argv[0] << ": " << msg << "\n";
|
2004-12-30 05:36:08 +00:00
|
|
|
} catch (...) {
|
2009-07-15 16:35:29 +00:00
|
|
|
errs() << argv[0] << ": Unexpected unknown exception occurred.\n";
|
2004-12-30 05:36:08 +00:00
|
|
|
}
|
2009-07-14 07:41:11 +00:00
|
|
|
|
2004-12-30 05:36:08 +00:00
|
|
|
return 1;
|
2003-10-28 19:16:35 +00:00
|
|
|
}
|