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"
28 BytecodeFile(cl::Positional, cl::desc("<program bytecode file>"),
32 ProfileDataFile(cl::Positional, cl::desc("<llvmprof.out file>"),
33 cl::Optional, cl::init("llvmprof.out"));
36 PrintAnnotatedLLVM("annotated-llvm",
37 cl::desc("Print LLVM code with frequency annotations"));
38 cl::alias PrintAnnotated2("A", cl::desc("Alias for --annotated-llvm"),
39 cl::aliasopt(PrintAnnotatedLLVM));
41 PrintAllCode("print-all-code",
42 cl::desc("Print annotated code for the entire program"));
45 // PairSecondSort - A sorting predicate to sort by the second element of a pair.
47 struct PairSecondSortReverse
48 : public std::binary_function<std::pair<T, unsigned>,
49 std::pair<T, unsigned>, bool> {
50 bool operator()(const std::pair<T, unsigned> &LHS,
51 const std::pair<T, unsigned> &RHS) const {
52 return LHS.second > RHS.second;
57 class ProfileAnnotator : public AssemblyAnnotationWriter {
58 std::map<const Function *, unsigned> &FuncFreqs;
59 std::map<const BasicBlock*, unsigned> &BlockFreqs;
61 ProfileAnnotator(std::map<const Function *, unsigned> &FF,
62 std::map<const BasicBlock*, unsigned> &BF)
63 : FuncFreqs(FF), BlockFreqs(BF) {}
65 virtual void emitFunctionAnnot(const Function *F, std::ostream &OS) {
66 OS << ";;; %" << F->getName() << " called " << FuncFreqs[F]
69 virtual void emitBasicBlockAnnot(const BasicBlock *BB, std::ostream &OS) {
70 if (BlockFreqs.empty()) return;
71 if (unsigned Count = BlockFreqs[BB])
72 OS << ";;; Executed " << Count << " times.\n";
74 OS << ";;; Never executed!\n";
80 int main(int argc, char **argv) {
81 cl::ParseCommandLineOptions(argc, argv, " llvm profile dump decoder\n");
83 // Read in the bytecode file...
84 std::string ErrorMessage;
85 Module *M = ParseBytecodeFile(BytecodeFile, &ErrorMessage);
87 std::cerr << argv[0] << ": " << BytecodeFile << ": " << ErrorMessage
92 // Read the profiling information
93 ProfileInfo PI(argv[0], ProfileDataFile, *M);
95 std::map<const Function *, unsigned> FuncFreqs;
96 std::map<const BasicBlock*, unsigned> BlockFreqs;
98 // Output a report. Eventually, there will be multiple reports selectable on
99 // the command line, for now, just keep things simple.
101 // Emit the most frequent function table...
102 std::vector<std::pair<Function*, unsigned> > FunctionCounts;
103 PI.getFunctionCounts(FunctionCounts);
104 FuncFreqs.insert(FunctionCounts.begin(), FunctionCounts.end());
106 // Sort by the frequency, backwards.
107 std::sort(FunctionCounts.begin(), FunctionCounts.end(),
108 PairSecondSortReverse<Function*>());
110 unsigned long long TotalExecutions = 0;
111 for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i)
112 TotalExecutions += FunctionCounts[i].second;
114 std::cout << "===" << std::string(73, '-') << "===\n"
115 << "LLVM profiling output for execution";
116 if (PI.getNumExecutions() != 1) std::cout << "s";
119 for (unsigned i = 0, e = PI.getNumExecutions(); i != e; ++i) {
121 if (e != 1) std::cout << i+1 << ". ";
122 std::cout << PI.getExecution(i) << "\n";
125 std::cout << "\n===" << std::string(73, '-') << "===\n";
126 std::cout << "Function execution frequencies:\n\n";
128 // Print out the function frequencies...
129 printf(" ## Frequency\n");
130 for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i) {
131 if (FunctionCounts[i].second == 0) {
132 printf("\n NOTE: %d function%s never executed!\n",
133 e-i, e-i-1 ? "s were" : " was");
137 printf("%3d. %5u/%llu %s\n", i+1, FunctionCounts[i].second, TotalExecutions,
138 FunctionCounts[i].first->getName().c_str());
141 std::set<Function*> FunctionsToPrint;
143 // If we have block count information, print out the LLVM module with
144 // frequency annotations.
145 if (PI.hasAccurateBlockCounts()) {
146 std::vector<std::pair<BasicBlock*, unsigned> > Counts;
147 PI.getBlockCounts(Counts);
150 for (unsigned i = 0, e = Counts.size(); i != e; ++i)
151 TotalExecutions += Counts[i].second;
153 // Sort by the frequency, backwards.
154 std::sort(Counts.begin(), Counts.end(),
155 PairSecondSortReverse<BasicBlock*>());
157 std::cout << "\n===" << std::string(73, '-') << "===\n";
158 std::cout << "Top 20 most frequently executed basic blocks:\n\n";
160 // Print out the function frequencies...
161 printf(" ## %%%% \tFrequency\n");
162 unsigned BlocksToPrint = Counts.size();
163 if (BlocksToPrint > 20) BlocksToPrint = 20;
164 for (unsigned i = 0; i != BlocksToPrint; ++i) {
165 if (Counts[i].second == 0) break;
166 Function *F = Counts[i].first->getParent();
167 printf("%3d. %5.2f%% %5u/%llu\t%s() - %s\n", i+1,
168 Counts[i].second/(double)TotalExecutions*100,
169 Counts[i].second, TotalExecutions,
170 F->getName().c_str(), Counts[i].first->getName().c_str());
171 FunctionsToPrint.insert(F);
174 BlockFreqs.insert(Counts.begin(), Counts.end());
177 if (PrintAnnotatedLLVM || PrintAllCode) {
178 std::cout << "\n===" << std::string(73, '-') << "===\n";
179 std::cout << "Annotated LLVM code for the module:\n\n";
181 ProfileAnnotator PA(FuncFreqs, BlockFreqs);
183 if (FunctionsToPrint.empty() || PrintAllCode)
184 M->print(std::cout, &PA);
186 // Print just a subset of the functions...
187 for (std::set<Function*>::iterator I = FunctionsToPrint.begin(),
188 E = FunctionsToPrint.end(); I != E; ++I)
189 (*I)->print(std::cout, &PA);