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 "llvm/Module.h"
17 #include "llvm/Assembly/AsmAnnotationWriter.h"
18 #include "llvm/Analysis/ProfileInfoLoader.h"
19 #include "llvm/Bytecode/Reader.h"
20 #include "Support/CommandLine.h"
29 BytecodeFile(cl::Positional, cl::desc("<program bytecode file>"),
33 ProfileDataFile(cl::Positional, cl::desc("<llvmprof.out file>"),
34 cl::Optional, cl::init("llvmprof.out"));
37 PrintAnnotatedLLVM("annotated-llvm",
38 cl::desc("Print LLVM code with frequency annotations"));
39 cl::alias PrintAnnotated2("A", cl::desc("Alias for --annotated-llvm"),
40 cl::aliasopt(PrintAnnotatedLLVM));
42 PrintAllCode("print-all-code",
43 cl::desc("Print annotated code for the entire program"));
46 // PairSecondSort - A sorting predicate to sort by the second element of a pair.
48 struct PairSecondSortReverse
49 : public std::binary_function<std::pair<T, unsigned>,
50 std::pair<T, unsigned>, bool> {
51 bool operator()(const std::pair<T, unsigned> &LHS,
52 const std::pair<T, unsigned> &RHS) const {
53 return LHS.second > RHS.second;
58 class ProfileAnnotator : public AssemblyAnnotationWriter {
59 std::map<const Function *, unsigned> &FuncFreqs;
60 std::map<const BasicBlock*, unsigned> &BlockFreqs;
62 ProfileAnnotator(std::map<const Function *, unsigned> &FF,
63 std::map<const BasicBlock*, unsigned> &BF)
64 : FuncFreqs(FF), BlockFreqs(BF) {}
66 virtual void emitFunctionAnnot(const Function *F, std::ostream &OS) {
67 OS << ";;; %" << F->getName() << " called " << FuncFreqs[F]
70 virtual void emitBasicBlockAnnot(const BasicBlock *BB, std::ostream &OS) {
71 if (BlockFreqs.empty()) return;
72 if (unsigned Count = BlockFreqs[BB])
73 OS << ";;; Executed " << Count << " times.\n";
75 OS << ";;; Never executed!\n";
81 int main(int argc, char **argv) {
82 cl::ParseCommandLineOptions(argc, argv, " llvm profile dump decoder\n");
84 // Read in the bytecode file...
85 std::string ErrorMessage;
86 Module *M = ParseBytecodeFile(BytecodeFile, &ErrorMessage);
88 std::cerr << argv[0] << ": " << BytecodeFile << ": " << ErrorMessage
93 // Read the profiling information
94 ProfileInfoLoader PI(argv[0], ProfileDataFile, *M);
96 std::map<const Function *, unsigned> FuncFreqs;
97 std::map<const BasicBlock*, unsigned> BlockFreqs;
99 // Output a report. Eventually, there will be multiple reports selectable on
100 // the command line, for now, just keep things simple.
102 // Emit the most frequent function table...
103 std::vector<std::pair<Function*, unsigned> > FunctionCounts;
104 PI.getFunctionCounts(FunctionCounts);
105 FuncFreqs.insert(FunctionCounts.begin(), FunctionCounts.end());
107 // Sort by the frequency, backwards.
108 std::sort(FunctionCounts.begin(), FunctionCounts.end(),
109 PairSecondSortReverse<Function*>());
111 unsigned long long TotalExecutions = 0;
112 for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i)
113 TotalExecutions += FunctionCounts[i].second;
115 std::cout << "===" << std::string(73, '-') << "===\n"
116 << "LLVM profiling output for execution";
117 if (PI.getNumExecutions() != 1) std::cout << "s";
120 for (unsigned i = 0, e = PI.getNumExecutions(); i != e; ++i) {
122 if (e != 1) std::cout << i+1 << ". ";
123 std::cout << PI.getExecution(i) << "\n";
126 std::cout << "\n===" << std::string(73, '-') << "===\n";
127 std::cout << "Function execution frequencies:\n\n";
129 // Print out the function frequencies...
130 printf(" ## Frequency\n");
131 for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i) {
132 if (FunctionCounts[i].second == 0) {
133 printf("\n NOTE: %d function%s never executed!\n",
134 e-i, e-i-1 ? "s were" : " was");
138 printf("%3d. %5u/%llu %s\n", i+1, FunctionCounts[i].second, TotalExecutions,
139 FunctionCounts[i].first->getName().c_str());
142 std::set<Function*> FunctionsToPrint;
144 // If we have block count information, print out the LLVM module with
145 // frequency annotations.
146 if (PI.hasAccurateBlockCounts()) {
147 std::vector<std::pair<BasicBlock*, unsigned> > Counts;
148 PI.getBlockCounts(Counts);
151 for (unsigned i = 0, e = Counts.size(); i != e; ++i)
152 TotalExecutions += Counts[i].second;
154 // Sort by the frequency, backwards.
155 std::sort(Counts.begin(), Counts.end(),
156 PairSecondSortReverse<BasicBlock*>());
158 std::cout << "\n===" << std::string(73, '-') << "===\n";
159 std::cout << "Top 20 most frequently executed basic blocks:\n\n";
161 // Print out the function frequencies...
162 printf(" ## %%%% \tFrequency\n");
163 unsigned BlocksToPrint = Counts.size();
164 if (BlocksToPrint > 20) BlocksToPrint = 20;
165 for (unsigned i = 0; i != BlocksToPrint; ++i) {
166 if (Counts[i].second == 0) break;
167 Function *F = Counts[i].first->getParent();
168 printf("%3d. %5.2f%% %5u/%llu\t%s() - %s\n", i+1,
169 Counts[i].second/(double)TotalExecutions*100,
170 Counts[i].second, TotalExecutions,
171 F->getName().c_str(), Counts[i].first->getName().c_str());
172 FunctionsToPrint.insert(F);
175 BlockFreqs.insert(Counts.begin(), Counts.end());
178 if (PrintAnnotatedLLVM || PrintAllCode) {
179 std::cout << "\n===" << std::string(73, '-') << "===\n";
180 std::cout << "Annotated LLVM code for the module:\n\n";
182 ProfileAnnotator PA(FuncFreqs, BlockFreqs);
184 if (FunctionsToPrint.empty() || PrintAllCode)
185 M->print(std::cout, &PA);
187 // Print just a subset of the functions...
188 for (std::set<Function*>::iterator I = FunctionsToPrint.begin(),
189 E = FunctionsToPrint.end(); I != E; ++I)
190 (*I)->print(std::cout, &PA);