1 //===- llvm-prof.cpp - Read in and process llvmprof.out data files --------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This tools is meant for use with the various LLVM profiling instrumentation
11 // passes. It reads in the data file produced by executing an instrumented
12 // program, and outputs a nice report.
14 //===----------------------------------------------------------------------===//
16 #include "ProfileInfo.h"
17 #include "llvm/Module.h"
18 #include "llvm/Assembly/AsmAnnotationWriter.h"
19 #include "llvm/Bytecode/Reader.h"
20 #include "Support/CommandLine.h"
30 BytecodeFile(cl::Positional, cl::desc("<program bytecode file>"),
34 ProfileDataFile(cl::Positional, cl::desc("<llvmprof.out file>"),
35 cl::Optional, cl::init("llvmprof.out"));
38 PrintAnnotatedLLVM("annotated-llvm",
39 cl::desc("Print LLVM code with frequency annotations"));
40 cl::alias PrintAnnotated2("A", cl::desc("Alias for --annotated-llvm"),
41 cl::aliasopt(PrintAnnotatedLLVM));
43 PrintAllCode("print-all-code",
44 cl::desc("Print annotated code for the entire program"));
47 // PairSecondSort - A sorting predicate to sort by the second element of a pair.
49 struct PairSecondSortReverse
50 : public std::binary_function<std::pair<T, unsigned>,
51 std::pair<T, unsigned>, bool> {
52 bool operator()(const std::pair<T, unsigned> &LHS,
53 const std::pair<T, unsigned> &RHS) const {
54 return LHS.second > RHS.second;
59 class ProfileAnnotator : public AssemblyAnnotationWriter {
60 std::map<const Function *, unsigned> &FuncFreqs;
61 std::map<const BasicBlock*, unsigned> &BlockFreqs;
63 ProfileAnnotator(std::map<const Function *, unsigned> &FF,
64 std::map<const BasicBlock*, unsigned> &BF)
65 : FuncFreqs(FF), BlockFreqs(BF) {}
67 virtual void emitFunctionAnnot(const Function *F, std::ostream &OS) {
68 OS << ";;; %" << F->getName() << " called " << FuncFreqs[F]
71 virtual void emitBasicBlockAnnot(const BasicBlock *BB, std::ostream &OS) {
72 if (BlockFreqs.empty()) return;
73 if (unsigned Count = BlockFreqs[BB])
74 OS << ";;; Executed " << Count << " times.\n";
76 OS << ";;; Never executed!\n";
82 int main(int argc, char **argv) {
83 cl::ParseCommandLineOptions(argc, argv, " llvm profile dump decoder\n");
85 // Read in the bytecode file...
86 std::string ErrorMessage;
87 Module *M = ParseBytecodeFile(BytecodeFile, &ErrorMessage);
89 std::cerr << argv[0] << ": " << BytecodeFile << ": " << ErrorMessage
94 // Read the profiling information
95 ProfileInfo PI(argv[0], ProfileDataFile, *M);
97 std::map<const Function *, unsigned> FuncFreqs;
98 std::map<const BasicBlock*, unsigned> BlockFreqs;
100 // Output a report. Eventually, there will be multiple reports selectable on
101 // the command line, for now, just keep things simple.
103 // Emit the most frequent function table...
104 std::vector<std::pair<Function*, unsigned> > FunctionCounts;
105 PI.getFunctionCounts(FunctionCounts);
106 FuncFreqs.insert(FunctionCounts.begin(), FunctionCounts.end());
108 // Sort by the frequency, backwards.
109 std::sort(FunctionCounts.begin(), FunctionCounts.end(),
110 PairSecondSortReverse<Function*>());
112 unsigned long long TotalExecutions = 0;
113 for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i)
114 TotalExecutions += FunctionCounts[i].second;
116 std::cout << "===" << std::string(73, '-') << "===\n"
117 << "LLVM profiling output for execution";
118 if (PI.getNumExecutions() != 1) std::cout << "s";
121 for (unsigned i = 0, e = PI.getNumExecutions(); i != e; ++i) {
123 if (e != 1) std::cout << i+1 << ". ";
124 std::cout << PI.getExecution(i) << "\n";
127 std::cout << "\n===" << std::string(73, '-') << "===\n";
128 std::cout << "Function execution frequencies:\n\n";
130 // Print out the function frequencies...
131 printf(" ## Frequency\n");
132 for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i) {
133 if (FunctionCounts[i].second == 0) {
134 printf("\n NOTE: %d function%s never executed!\n",
135 e-i, e-i-1 ? "s were" : " was");
139 printf("%3d. %5u/%llu %s\n", i+1, FunctionCounts[i].second, TotalExecutions,
140 FunctionCounts[i].first->getName().c_str());
143 std::set<Function*> FunctionsToPrint;
145 // If we have block count information, print out the LLVM module with
146 // frequency annotations.
147 if (PI.hasAccurateBlockCounts()) {
148 std::vector<std::pair<BasicBlock*, unsigned> > Counts;
149 PI.getBlockCounts(Counts);
152 for (unsigned i = 0, e = Counts.size(); i != e; ++i)
153 TotalExecutions += Counts[i].second;
155 // Sort by the frequency, backwards.
156 std::sort(Counts.begin(), Counts.end(),
157 PairSecondSortReverse<BasicBlock*>());
159 std::cout << "\n===" << std::string(73, '-') << "===\n";
160 std::cout << "Top 20 most frequently executed basic blocks:\n\n";
162 // Print out the function frequencies...
163 printf(" ## %%%% \tFrequency\n");
164 unsigned BlocksToPrint = Counts.size();
165 if (BlocksToPrint > 20) BlocksToPrint = 20;
166 for (unsigned i = 0; i != BlocksToPrint; ++i) {
167 if (Counts[i].second == 0) break;
168 Function *F = Counts[i].first->getParent();
169 printf("%3d. %5.2f%% %5u/%llu\t%s() - %s\n", i+1,
170 Counts[i].second/(double)TotalExecutions*100,
171 Counts[i].second, TotalExecutions,
172 F->getName().c_str(), Counts[i].first->getName().c_str());
173 FunctionsToPrint.insert(F);
176 BlockFreqs.insert(Counts.begin(), Counts.end());
179 if (PrintAnnotatedLLVM || PrintAllCode) {
180 std::cout << "\n===" << std::string(73, '-') << "===\n";
181 std::cout << "Annotated LLVM code for the module:\n\n";
183 ProfileAnnotator PA(FuncFreqs, BlockFreqs);
185 if (FunctionsToPrint.empty() || PrintAllCode)
186 M->print(std::cout, &PA);
188 // Print just a subset of the functions...
189 for (std::set<Function*>::iterator I = FunctionsToPrint.begin(),
190 E = FunctionsToPrint.end(); I != E; ++I)
191 (*I)->print(std::cout, &PA);