2003-10-28 20:16:35 +01:00
|
|
|
//===- llvm-prof.cpp - Read in and process llvmprof.out data files --------===//
|
2005-04-22 02:00:37 +02:00
|
|
|
//
|
2003-10-28 20:16:35 +01:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 21:44:31 +01:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 02:00:37 +02:00
|
|
|
//
|
2003-10-28 20:16:35 +01: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 21:04:32 +01:00
|
|
|
#include "llvm/InstrTypes.h"
|
2009-07-01 18:58:40 +02:00
|
|
|
#include "llvm/LLVMContext.h"
|
2003-10-31 00:42:09 +01:00
|
|
|
#include "llvm/Module.h"
|
2009-07-14 09:41:11 +02:00
|
|
|
#include "llvm/PassManager.h"
|
2010-09-03 01:09:42 +02:00
|
|
|
#include "llvm/Assembly/AssemblyAnnotationWriter.h"
|
2009-07-14 09:41:11 +02:00
|
|
|
#include "llvm/Analysis/ProfileInfo.h"
|
2004-02-11 06:56:07 +01:00
|
|
|
#include "llvm/Analysis/ProfileInfoLoader.h"
|
2009-08-05 17:55:56 +02:00
|
|
|
#include "llvm/Analysis/Passes.h"
|
2007-05-06 06:43:00 +02:00
|
|
|
#include "llvm/Bitcode/ReaderWriter.h"
|
2004-09-02 00:55:40 +02:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2010-09-03 01:07:12 +02:00
|
|
|
#include "llvm/Support/FormattedStream.h"
|
2006-12-06 02:18:01 +01:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2007-05-06 06:43:00 +02:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2009-03-06 06:34:10 +01:00
|
|
|
#include "llvm/Support/PrettyStackTrace.h"
|
2008-08-24 00:23:09 +02:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2009-08-26 11:05:21 +02:00
|
|
|
#include "llvm/Support/Format.h"
|
2010-11-29 19:16:10 +01:00
|
|
|
#include "llvm/Support/Signals.h"
|
2010-12-09 18:36:48 +01:00
|
|
|
#include "llvm/Support/system_error.h"
|
2007-03-05 01:00:42 +01:00
|
|
|
#include <algorithm>
|
2005-12-29 22:13:45 +01:00
|
|
|
#include <iomanip>
|
2003-10-28 22:25:23 +01:00
|
|
|
#include <map>
|
2003-10-31 00:42:09 +01:00
|
|
|
#include <set>
|
2003-10-28 20:16:35 +01:00
|
|
|
|
2003-11-11 23:41:34 +01:00
|
|
|
using namespace llvm;
|
|
|
|
|
2003-10-28 20:16:35 +01:00
|
|
|
namespace {
|
2005-12-30 10:07:29 +01:00
|
|
|
cl::opt<std::string>
|
2007-07-05 19:07:56 +02:00
|
|
|
BitcodeFile(cl::Positional, cl::desc("<program bitcode file>"),
|
|
|
|
cl::Required);
|
2003-10-28 20:16:35 +01:00
|
|
|
|
2005-12-30 10:07:29 +01:00
|
|
|
cl::opt<std::string>
|
2003-10-28 20:16:35 +01:00
|
|
|
ProfileDataFile(cl::Positional, cl::desc("<llvmprof.out file>"),
|
|
|
|
cl::Optional, cl::init("llvmprof.out"));
|
2003-10-31 00:42:09 +01: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 21:29:25 +01:00
|
|
|
cl::opt<bool>
|
|
|
|
PrintAllCode("print-all-code",
|
|
|
|
cl::desc("Print annotated code for the entire program"));
|
2003-10-28 20:16:35 +01:00
|
|
|
}
|
|
|
|
|
2003-10-28 22:08:18 +01:00
|
|
|
// PairSecondSort - A sorting predicate to sort by the second element of a pair.
|
|
|
|
template<class T>
|
2003-10-29 22:41:17 +01:00
|
|
|
struct PairSecondSortReverse
|
2009-08-13 03:55:43 +02:00
|
|
|
: public std::binary_function<std::pair<T, double>,
|
|
|
|
std::pair<T, double>, bool> {
|
|
|
|
bool operator()(const std::pair<T, double> &LHS,
|
|
|
|
const std::pair<T, double> &RHS) const {
|
2003-10-29 22:41:17 +01:00
|
|
|
return LHS.second > RHS.second;
|
2003-10-28 22:08:18 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-08-08 19:43:09 +02:00
|
|
|
static double ignoreMissing(double w) {
|
|
|
|
if (w == ProfileInfo::MissingValue) return 0;
|
|
|
|
return w;
|
|
|
|
}
|
|
|
|
|
2003-10-31 00:42:09 +01:00
|
|
|
namespace {
|
|
|
|
class ProfileAnnotator : public AssemblyAnnotationWriter {
|
2009-08-05 17:55:56 +02:00
|
|
|
ProfileInfo &PI;
|
2003-10-31 00:42:09 +01:00
|
|
|
public:
|
2010-09-03 01:07:12 +02:00
|
|
|
ProfileAnnotator(ProfileInfo &pi) : PI(pi) {}
|
2003-10-31 00:42:09 +01:00
|
|
|
|
2010-09-03 01:07:12 +02:00
|
|
|
virtual void emitFunctionAnnot(const Function *F,
|
|
|
|
formatted_raw_ostream &OS) {
|
2009-08-08 19:43:09 +02:00
|
|
|
double w = PI.getExecutionCount(F);
|
2009-08-08 20:59:03 +02:00
|
|
|
if (w != ProfileInfo::MissingValue) {
|
|
|
|
OS << ";;; %" << F->getName() << " called "<<(unsigned)w
|
|
|
|
<<" times.\n;;;\n";
|
|
|
|
}
|
2003-10-31 00:42:09 +01:00
|
|
|
}
|
2004-03-08 21:04:32 +01:00
|
|
|
virtual void emitBasicBlockStartAnnot(const BasicBlock *BB,
|
2010-09-03 01:07:12 +02:00
|
|
|
formatted_raw_ostream &OS) {
|
2009-08-08 19:43:09 +02:00
|
|
|
double w = PI.getExecutionCount(BB);
|
2009-08-08 20:59:03 +02:00
|
|
|
if (w != ProfileInfo::MissingValue) {
|
|
|
|
if (w != 0) {
|
|
|
|
OS << "\t;;; Basic block executed " << (unsigned)w << " times.\n";
|
|
|
|
} else {
|
2009-08-08 19:43:09 +02:00
|
|
|
OS << "\t;;; Never executed!\n";
|
2009-08-08 20:59:03 +02:00
|
|
|
}
|
|
|
|
}
|
2004-03-08 21:04:32 +01:00
|
|
|
}
|
|
|
|
|
2010-09-03 01:07:12 +02:00
|
|
|
virtual void emitBasicBlockEndAnnot(const BasicBlock *BB,
|
|
|
|
formatted_raw_ostream &OS) {
|
2004-03-08 21:04:32 +01:00
|
|
|
// Figure out how many times each successor executed.
|
2009-08-13 03:55:43 +02:00
|
|
|
std::vector<std::pair<ProfileInfo::Edge, double> > SuccCounts;
|
2005-04-22 02:00:37 +02:00
|
|
|
|
2009-08-05 17:55:56 +02:00
|
|
|
const TerminatorInst *TI = BB->getTerminator();
|
|
|
|
for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) {
|
|
|
|
BasicBlock* Succ = TI->getSuccessor(s);
|
2009-08-08 20:59:03 +02:00
|
|
|
double w = ignoreMissing(PI.getEdgeWeight(std::make_pair(BB, Succ)));
|
|
|
|
if (w != 0)
|
|
|
|
SuccCounts.push_back(std::make_pair(std::make_pair(BB, Succ), w));
|
2009-08-05 17:55:56 +02:00
|
|
|
}
|
2004-03-08 21:04:32 +01:00
|
|
|
if (!SuccCounts.empty()) {
|
|
|
|
OS << "\t;;; Out-edge counts:";
|
|
|
|
for (unsigned i = 0, e = SuccCounts.size(); i != e; ++i)
|
2009-08-05 17:55:56 +02:00
|
|
|
OS << " [" << (SuccCounts[i]).second << " -> "
|
|
|
|
<< (SuccCounts[i]).first.second->getName() << "]";
|
2004-03-08 21:04:32 +01:00
|
|
|
OS << "\n";
|
|
|
|
}
|
2003-10-31 00:42:09 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2009-07-14 09:41:11 +02: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)
|
2010-08-06 20:33:48 +02:00
|
|
|
: ModulePass(ID), PIL(_PIL) {}
|
2009-07-14 09:41:11 +02:00
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
AU.addRequired<ProfileInfo>();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool runOnModule(Module &M);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
char ProfileInfoPrinterPass::ID = 0;
|
|
|
|
|
|
|
|
bool ProfileInfoPrinterPass::runOnModule(Module &M) {
|
2009-08-05 17:55:56 +02:00
|
|
|
ProfileInfo &PI = getAnalysis<ProfileInfo>();
|
2009-07-14 09:41:11 +02:00
|
|
|
std::map<const Function *, unsigned> FuncFreqs;
|
|
|
|
std::map<const BasicBlock*, unsigned> BlockFreqs;
|
2009-08-05 17:55:56 +02:00
|
|
|
std::map<ProfileInfo::Edge, unsigned> EdgeFreqs;
|
2009-07-14 09:41:11 +02:00
|
|
|
|
|
|
|
// 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...
|
2009-08-13 03:55:43 +02:00
|
|
|
std::vector<std::pair<Function*, double> > FunctionCounts;
|
|
|
|
std::vector<std::pair<BasicBlock*, double> > Counts;
|
2009-08-05 17:55:56 +02:00
|
|
|
for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI) {
|
2009-08-05 23:51:16 +02:00
|
|
|
if (FI->isDeclaration()) continue;
|
2009-08-08 19:43:09 +02:00
|
|
|
double w = ignoreMissing(PI.getExecutionCount(FI));
|
2009-08-08 20:59:03 +02:00
|
|
|
FunctionCounts.push_back(std::make_pair(FI, w));
|
2009-08-05 17:55:56 +02:00
|
|
|
for (Function::iterator BB = FI->begin(), BBE = FI->end();
|
|
|
|
BB != BBE; ++BB) {
|
2009-08-08 19:43:09 +02:00
|
|
|
double w = ignoreMissing(PI.getExecutionCount(BB));
|
2009-08-08 20:59:03 +02:00
|
|
|
Counts.push_back(std::make_pair(BB, w));
|
2009-08-05 17:55:56 +02:00
|
|
|
}
|
|
|
|
}
|
2009-07-14 09:41:11 +02:00
|
|
|
|
|
|
|
// Sort by the frequency, backwards.
|
|
|
|
sort(FunctionCounts.begin(), FunctionCounts.end(),
|
|
|
|
PairSecondSortReverse<Function*>());
|
|
|
|
|
2009-08-13 03:55:43 +02:00
|
|
|
double TotalExecutions = 0;
|
2009-07-14 09:41:11 +02:00
|
|
|
for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i)
|
|
|
|
TotalExecutions += FunctionCounts[i].second;
|
|
|
|
|
2009-08-26 11:05:21 +02:00
|
|
|
outs() << "===" << std::string(73, '-') << "===\n"
|
|
|
|
<< "LLVM profiling output for execution";
|
|
|
|
if (PIL.getNumExecutions() != 1) outs() << "s";
|
|
|
|
outs() << ":\n";
|
2009-07-14 09:41:11 +02:00
|
|
|
|
|
|
|
for (unsigned i = 0, e = PIL.getNumExecutions(); i != e; ++i) {
|
2009-08-26 11:05:21 +02:00
|
|
|
outs() << " ";
|
|
|
|
if (e != 1) outs() << i+1 << ". ";
|
|
|
|
outs() << PIL.getExecution(i) << "\n";
|
2009-07-14 09:41:11 +02:00
|
|
|
}
|
|
|
|
|
2009-08-26 11:05:21 +02:00
|
|
|
outs() << "\n===" << std::string(73, '-') << "===\n";
|
|
|
|
outs() << "Function execution frequencies:\n\n";
|
2009-07-14 09:41:11 +02:00
|
|
|
|
|
|
|
// Print out the function frequencies...
|
2009-08-26 11:05:21 +02:00
|
|
|
outs() << " ## Frequency\n";
|
2009-07-14 09:41:11 +02:00
|
|
|
for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i) {
|
|
|
|
if (FunctionCounts[i].second == 0) {
|
2009-08-26 11:05:21 +02:00
|
|
|
outs() << "\n NOTE: " << e-i << " function"
|
|
|
|
<< (e-i-1 ? "s were" : " was") << " never executed!\n";
|
2009-07-14 09:41:11 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-08-26 11:05:21 +02:00
|
|
|
outs() << format("%3d", i+1) << ". "
|
2011-11-15 17:27:03 +01:00
|
|
|
<< format("%5.2g", FunctionCounts[i].second) << "/"
|
|
|
|
<< format("%g", TotalExecutions) << " "
|
|
|
|
<< FunctionCounts[i].first->getName() << "\n";
|
2009-07-14 09:41:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::set<Function*> FunctionsToPrint;
|
|
|
|
|
2009-08-05 17:55:56 +02:00
|
|
|
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*>());
|
|
|
|
|
2009-08-26 11:05:21 +02:00
|
|
|
outs() << "\n===" << std::string(73, '-') << "===\n";
|
|
|
|
outs() << "Top 20 most frequently executed basic blocks:\n\n";
|
2009-08-05 17:55:56 +02:00
|
|
|
|
|
|
|
// Print out the function frequencies...
|
2009-08-26 11:05:21 +02:00
|
|
|
outs() <<" ## %% \tFrequency\n";
|
2009-08-05 17:55:56 +02:00
|
|
|
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();
|
2011-11-15 17:27:03 +01:00
|
|
|
outs() << format("%3d", i+1) << ". "
|
|
|
|
<< format("%5g", Counts[i].second/(double)TotalExecutions*100)<<"% "
|
|
|
|
<< format("%5.0f", Counts[i].second) << "/"
|
|
|
|
<< format("%g", TotalExecutions) << "\t"
|
|
|
|
<< F->getName() << "() - "
|
|
|
|
<< Counts[i].first->getName() << "\n";
|
2009-08-05 17:55:56 +02:00
|
|
|
FunctionsToPrint.insert(F);
|
2009-07-14 09:41:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (PrintAnnotatedLLVM || PrintAllCode) {
|
2009-08-26 11:05:21 +02:00
|
|
|
outs() << "\n===" << std::string(73, '-') << "===\n";
|
|
|
|
outs() << "Annotated LLVM code for the module:\n\n";
|
2009-08-05 17:55:56 +02:00
|
|
|
|
|
|
|
ProfileAnnotator PA(PI);
|
2009-07-14 09:41:11 +02:00
|
|
|
|
|
|
|
if (FunctionsToPrint.empty() || PrintAllCode)
|
2009-08-23 06:52:46 +02:00
|
|
|
M.print(outs(), &PA);
|
2009-07-14 09:41:11 +02:00
|
|
|
else
|
|
|
|
// Print just a subset of the functions.
|
|
|
|
for (std::set<Function*>::iterator I = FunctionsToPrint.begin(),
|
|
|
|
E = FunctionsToPrint.end(); I != E; ++I)
|
2009-08-23 06:37:46 +02:00
|
|
|
(*I)->print(outs(), &PA);
|
2009-07-14 09:41:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2003-10-28 22:08:18 +01:00
|
|
|
|
2003-10-28 20:16:35 +01:00
|
|
|
int main(int argc, char **argv) {
|
2009-03-06 06:34:10 +01:00
|
|
|
// Print a stack trace if we signal out.
|
|
|
|
sys::PrintStackTraceOnErrorSignal();
|
|
|
|
PrettyStackTraceProgram X(argc, argv);
|
2009-07-01 18:58:40 +02:00
|
|
|
|
2009-07-16 00:16:10 +02:00
|
|
|
LLVMContext &Context = getGlobalContext();
|
2009-03-06 06:34:10 +01:00
|
|
|
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
|
2009-07-14 09:41:11 +02:00
|
|
|
|
2009-10-22 02:50:24 +02:00
|
|
|
cl::ParseCommandLineOptions(argc, argv, "llvm profile dump decoder\n");
|
|
|
|
|
|
|
|
// Read in the bitcode file...
|
|
|
|
std::string ErrorMessage;
|
2010-12-16 04:29:14 +01:00
|
|
|
OwningPtr<MemoryBuffer> Buffer;
|
2010-12-09 18:36:48 +01:00
|
|
|
error_code ec;
|
2009-10-22 02:50:24 +02:00
|
|
|
Module *M = 0;
|
2010-12-16 04:29:14 +01:00
|
|
|
if (!(ec = MemoryBuffer::getFileOrSTDIN(BitcodeFile, Buffer))) {
|
|
|
|
M = ParseBitcodeFile(Buffer.get(), Context, &ErrorMessage);
|
2010-12-09 18:36:48 +01:00
|
|
|
} else
|
|
|
|
ErrorMessage = ec.message();
|
2009-10-22 02:50:24 +02:00
|
|
|
if (M == 0) {
|
|
|
|
errs() << 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;
|
2003-10-28 20:16:35 +01:00
|
|
|
}
|