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
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and 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"
|
2003-10-30 23:42:09 +00:00
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/Assembly/AsmAnnotationWriter.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"
|
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
|
|
|
|
2005-12-30 09:07:29 +00:00
|
|
|
virtual void emitFunctionAnnot(const Function *F, std::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,
|
2005-12-30 09:07:29 +00:00
|
|
|
std::ostream &OS) {
|
2003-10-30 23:44:28 +00:00
|
|
|
if (BlockFreqs.empty()) return;
|
2003-10-30 23:42:09 +00:00
|
|
|
if (unsigned Count = BlockFreqs[BB])
|
2004-03-08 20:04:32 +00:00
|
|
|
OS << "\t;;; Basic block executed " << Count << " times.\n";
|
2003-10-30 23:42:09 +00:00
|
|
|
else
|
2004-03-08 20:04:32 +00:00
|
|
|
OS << "\t;;; Never executed!\n";
|
|
|
|
}
|
|
|
|
|
2005-12-30 09:07:29 +00:00
|
|
|
virtual void emitBasicBlockEndAnnot(const BasicBlock *BB, std::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
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2003-10-28 21:08:18 +00:00
|
|
|
|
2003-10-28 19:16:35 +00:00
|
|
|
int main(int argc, char **argv) {
|
2006-12-06 01:18:01 +00:00
|
|
|
llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
|
2004-12-30 05:36:08 +00:00
|
|
|
try {
|
|
|
|
cl::ParseCommandLineOptions(argc, argv, " llvm profile dump decoder\n");
|
|
|
|
sys::PrintStackTraceOnErrorSignal();
|
|
|
|
|
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)) {
|
2007-05-06 09:29:57 +00:00
|
|
|
M = ParseBitcodeFile(Buffer, &ErrorMessage);
|
2007-05-06 23:45:49 +00:00
|
|
|
delete Buffer;
|
|
|
|
}
|
2004-12-30 05:36:08 +00:00
|
|
|
if (M == 0) {
|
2007-07-05 17:07:56 +00:00
|
|
|
std::cerr << 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
|
|
|
}
|
|
|
|
|
2004-12-30 05:36:08 +00:00
|
|
|
// Read the profiling information
|
|
|
|
ProfileInfoLoader PI(argv[0], ProfileDataFile, *M);
|
2003-10-28 21:25:23 +00:00
|
|
|
|
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-28 21:25:23 +00:00
|
|
|
|
2005-12-29 21:13:45 +00:00
|
|
|
// Output a report. Eventually, there will be multiple reports selectable on
|
2004-12-30 05:36:08 +00:00
|
|
|
// the command line, for now, just keep things simple.
|
2003-10-29 21:41:17 +00:00
|
|
|
|
2004-12-30 05:36:08 +00:00
|
|
|
// Emit the most frequent function table...
|
2005-12-30 09:07:29 +00:00
|
|
|
std::vector<std::pair<Function*, unsigned> > FunctionCounts;
|
2004-12-30 05:36:08 +00:00
|
|
|
PI.getFunctionCounts(FunctionCounts);
|
|
|
|
FuncFreqs.insert(FunctionCounts.begin(), FunctionCounts.end());
|
2003-10-29 21:41:17 +00:00
|
|
|
|
|
|
|
// Sort by the frequency, backwards.
|
2005-12-29 21:13:45 +00:00
|
|
|
sort(FunctionCounts.begin(), FunctionCounts.end(),
|
2004-12-30 05:36:08 +00:00
|
|
|
PairSecondSortReverse<Function*>());
|
|
|
|
|
2006-05-24 19:21:13 +00:00
|
|
|
uint64_t TotalExecutions = 0;
|
2004-12-30 05:36:08 +00:00
|
|
|
for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i)
|
|
|
|
TotalExecutions += FunctionCounts[i].second;
|
2005-04-22 00:00:37 +00:00
|
|
|
|
2005-12-30 09:07:29 +00:00
|
|
|
std::cout << "===" << std::string(73, '-') << "===\n"
|
2004-12-30 05:36:08 +00:00
|
|
|
<< "LLVM profiling output for execution";
|
2005-12-30 09:07:29 +00:00
|
|
|
if (PI.getNumExecutions() != 1) std::cout << "s";
|
|
|
|
std::cout << ":\n";
|
2005-04-22 00:00:37 +00:00
|
|
|
|
2004-12-30 05:36:08 +00:00
|
|
|
for (unsigned i = 0, e = PI.getNumExecutions(); i != e; ++i) {
|
2005-12-30 09:07:29 +00:00
|
|
|
std::cout << " ";
|
|
|
|
if (e != 1) std::cout << i+1 << ". ";
|
|
|
|
std::cout << PI.getExecution(i) << "\n";
|
2004-12-30 05:36:08 +00:00
|
|
|
}
|
2005-04-22 00:00:37 +00:00
|
|
|
|
2005-12-30 09:07:29 +00:00
|
|
|
std::cout << "\n===" << std::string(73, '-') << "===\n";
|
|
|
|
std::cout << "Function execution frequencies:\n\n";
|
2003-10-29 21:41:17 +00:00
|
|
|
|
|
|
|
// Print out the function frequencies...
|
2005-12-30 09:07:29 +00:00
|
|
|
std::cout << " ## Frequency\n";
|
2004-12-30 05:36:08 +00:00
|
|
|
for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i) {
|
|
|
|
if (FunctionCounts[i].second == 0) {
|
2005-12-30 09:07:29 +00:00
|
|
|
std::cout << "\n NOTE: " << e-i << " function" <<
|
2005-12-29 21:13:45 +00:00
|
|
|
(e-i-1 ? "s were" : " was") << " never executed!\n";
|
2004-12-30 05:36:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2005-12-30 09:07:29 +00:00
|
|
|
std::cout << std::setw(3) << i+1 << ". "
|
|
|
|
<< std::setw(5) << FunctionCounts[i].second << "/"
|
2005-12-29 21:13:45 +00:00
|
|
|
<< TotalExecutions << " "
|
|
|
|
<< FunctionCounts[i].first->getName().c_str() << "\n";
|
2003-10-30 23:42:09 +00:00
|
|
|
}
|
2003-10-29 21:41:17 +00:00
|
|
|
|
2005-12-30 09:07:29 +00:00
|
|
|
std::set<Function*> FunctionsToPrint;
|
2004-03-08 20:04:32 +00:00
|
|
|
|
2004-12-30 05:36:08 +00:00
|
|
|
// If we have block count information, print out the LLVM module with
|
|
|
|
// frequency annotations.
|
|
|
|
if (PI.hasAccurateBlockCounts()) {
|
2005-12-30 09:07:29 +00:00
|
|
|
std::vector<std::pair<BasicBlock*, unsigned> > Counts;
|
2004-12-30 05:36:08 +00:00
|
|
|
PI.getBlockCounts(Counts);
|
|
|
|
|
|
|
|
TotalExecutions = 0;
|
|
|
|
for (unsigned i = 0, e = Counts.size(); i != e; ++i)
|
|
|
|
TotalExecutions += Counts[i].second;
|
|
|
|
|
|
|
|
// Sort by the frequency, backwards.
|
2005-12-29 21:13:45 +00:00
|
|
|
sort(Counts.begin(), Counts.end(),
|
2004-12-30 05:36:08 +00:00
|
|
|
PairSecondSortReverse<BasicBlock*>());
|
2005-04-22 00:00:37 +00:00
|
|
|
|
2005-12-30 09:07:29 +00:00
|
|
|
std::cout << "\n===" << std::string(73, '-') << "===\n";
|
|
|
|
std::cout << "Top 20 most frequently executed basic blocks:\n\n";
|
2004-12-30 05:36:08 +00:00
|
|
|
|
|
|
|
// Print out the function frequencies...
|
2005-12-30 09:07:29 +00:00
|
|
|
std::cout <<" ## %% \tFrequency\n";
|
2004-12-30 05:36:08 +00: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();
|
2005-12-30 09:07:29 +00:00
|
|
|
std::cout << std::setw(3) << i+1 << ". "
|
|
|
|
<< std::setw(5) << std::setprecision(2)
|
2005-12-29 21:13:45 +00:00
|
|
|
<< Counts[i].second/(double)TotalExecutions*100 << "% "
|
2005-12-30 09:07:29 +00:00
|
|
|
<< std::setw(5) << Counts[i].second << "/"
|
2005-12-29 21:13:45 +00:00
|
|
|
<< TotalExecutions << "\t"
|
|
|
|
<< F->getName().c_str() << "() - "
|
|
|
|
<< Counts[i].first->getName().c_str() << "\n";
|
2004-12-30 05:36:08 +00:00
|
|
|
FunctionsToPrint.insert(F);
|
|
|
|
}
|
|
|
|
|
|
|
|
BlockFreqs.insert(Counts.begin(), Counts.end());
|
|
|
|
}
|
2005-04-22 00:00:37 +00:00
|
|
|
|
2004-12-30 05:36:08 +00:00
|
|
|
if (PI.hasAccurateEdgeCounts()) {
|
2005-12-30 09:07:29 +00:00
|
|
|
std::vector<std::pair<ProfileInfoLoader::Edge, unsigned> > Counts;
|
2004-12-30 05:36:08 +00:00
|
|
|
PI.getEdgeCounts(Counts);
|
|
|
|
EdgeFreqs.insert(Counts.begin(), Counts.end());
|
|
|
|
}
|
2003-10-28 21:25:23 +00:00
|
|
|
|
2004-12-30 05:36:08 +00:00
|
|
|
if (PrintAnnotatedLLVM || PrintAllCode) {
|
2005-12-30 09:07:29 +00:00
|
|
|
std::cout << "\n===" << std::string(73, '-') << "===\n";
|
|
|
|
std::cout << "Annotated LLVM code for the module:\n\n";
|
2005-04-22 00:00:37 +00:00
|
|
|
|
2004-12-30 05:36:08 +00:00
|
|
|
ProfileAnnotator PA(FuncFreqs, BlockFreqs, EdgeFreqs);
|
|
|
|
|
|
|
|
if (FunctionsToPrint.empty() || PrintAllCode)
|
2005-12-30 09:07:29 +00:00
|
|
|
M->print(std::cout, &PA);
|
2004-12-30 05:36:08 +00:00
|
|
|
else
|
|
|
|
// Print just a subset of the functions...
|
2005-12-30 09:07:29 +00:00
|
|
|
for (std::set<Function*>::iterator I = FunctionsToPrint.begin(),
|
2004-12-30 05:36:08 +00:00
|
|
|
E = FunctionsToPrint.end(); I != E; ++I)
|
2005-12-30 09:07:29 +00:00
|
|
|
(*I)->print(std::cout, &PA);
|
2004-12-30 05:36:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2005-12-30 09:07:29 +00:00
|
|
|
} catch (const std::string& msg) {
|
|
|
|
std::cerr << argv[0] << ": " << msg << "\n";
|
2004-12-30 05:36:08 +00:00
|
|
|
} catch (...) {
|
2005-12-30 09:07:29 +00:00
|
|
|
std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
|
2004-12-30 05:36:08 +00:00
|
|
|
}
|
|
|
|
return 1;
|
2003-10-28 19:16:35 +00:00
|
|
|
}
|