blob: cab87e7305827b883c3759b68257a6f0d6eb63d9 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- llvm-prof.cpp - Read in and process llvmprof.out data files --------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5f5a5732007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
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.
13//
14//===----------------------------------------------------------------------===//
15
16#include "llvm/InstrTypes.h"
Owen Anderson25209b42009-07-01 16:58:40 +000017#include "llvm/LLVMContext.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000018#include "llvm/Module.h"
19#include "llvm/Assembly/AsmAnnotationWriter.h"
20#include "llvm/Analysis/ProfileInfoLoader.h"
21#include "llvm/Bitcode/ReaderWriter.h"
22#include "llvm/Support/CommandLine.h"
23#include "llvm/Support/ManagedStatic.h"
24#include "llvm/Support/MemoryBuffer.h"
Chris Lattnere6012df2009-03-06 05:34:10 +000025#include "llvm/Support/PrettyStackTrace.h"
Chris Lattner1fefaac2008-08-23 22:23:09 +000026#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000027#include "llvm/System/Signals.h"
28#include <algorithm>
29#include <iostream>
30#include <iomanip>
31#include <map>
32#include <set>
33
34using namespace llvm;
35
36namespace {
37 cl::opt<std::string>
38 BitcodeFile(cl::Positional, cl::desc("<program bitcode file>"),
39 cl::Required);
40
41 cl::opt<std::string>
42 ProfileDataFile(cl::Positional, cl::desc("<llvmprof.out file>"),
43 cl::Optional, cl::init("llvmprof.out"));
44
45 cl::opt<bool>
46 PrintAnnotatedLLVM("annotated-llvm",
47 cl::desc("Print LLVM code with frequency annotations"));
48 cl::alias PrintAnnotated2("A", cl::desc("Alias for --annotated-llvm"),
49 cl::aliasopt(PrintAnnotatedLLVM));
50 cl::opt<bool>
51 PrintAllCode("print-all-code",
52 cl::desc("Print annotated code for the entire program"));
53}
54
55// PairSecondSort - A sorting predicate to sort by the second element of a pair.
56template<class T>
57struct PairSecondSortReverse
58 : public std::binary_function<std::pair<T, unsigned>,
59 std::pair<T, unsigned>, bool> {
60 bool operator()(const std::pair<T, unsigned> &LHS,
61 const std::pair<T, unsigned> &RHS) const {
62 return LHS.second > RHS.second;
63 }
64};
65
66namespace {
67 class ProfileAnnotator : public AssemblyAnnotationWriter {
68 std::map<const Function *, unsigned> &FuncFreqs;
69 std::map<const BasicBlock*, unsigned> &BlockFreqs;
70 std::map<ProfileInfoLoader::Edge, unsigned> &EdgeFreqs;
71 public:
72 ProfileAnnotator(std::map<const Function *, unsigned> &FF,
73 std::map<const BasicBlock*, unsigned> &BF,
74 std::map<ProfileInfoLoader::Edge, unsigned> &EF)
75 : FuncFreqs(FF), BlockFreqs(BF), EdgeFreqs(EF) {}
76
Chris Lattner1fefaac2008-08-23 22:23:09 +000077 virtual void emitFunctionAnnot(const Function *F, raw_ostream &OS) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000078 OS << ";;; %" << F->getName() << " called " << FuncFreqs[F]
79 << " times.\n;;;\n";
80 }
81 virtual void emitBasicBlockStartAnnot(const BasicBlock *BB,
Chris Lattner1fefaac2008-08-23 22:23:09 +000082 raw_ostream &OS) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000083 if (BlockFreqs.empty()) return;
84 if (unsigned Count = BlockFreqs[BB])
85 OS << "\t;;; Basic block executed " << Count << " times.\n";
86 else
87 OS << "\t;;; Never executed!\n";
88 }
89
Chris Lattner1fefaac2008-08-23 22:23:09 +000090 virtual void emitBasicBlockEndAnnot(const BasicBlock *BB, raw_ostream &OS) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000091 if (EdgeFreqs.empty()) return;
92
93 // Figure out how many times each successor executed.
94 std::vector<std::pair<const BasicBlock*, unsigned> > SuccCounts;
95 const TerminatorInst *TI = BB->getTerminator();
96
97 std::map<ProfileInfoLoader::Edge, unsigned>::iterator I =
98 EdgeFreqs.lower_bound(std::make_pair(const_cast<BasicBlock*>(BB), 0U));
99 for (; I != EdgeFreqs.end() && I->first.first == BB; ++I)
100 if (I->second)
101 SuccCounts.push_back(std::make_pair(TI->getSuccessor(I->first.second),
102 I->second));
103 if (!SuccCounts.empty()) {
104 OS << "\t;;; Out-edge counts:";
105 for (unsigned i = 0, e = SuccCounts.size(); i != e; ++i)
106 OS << " [" << SuccCounts[i].second << " -> "
107 << SuccCounts[i].first->getName() << "]";
108 OS << "\n";
109 }
110 }
111 };
112}
113
114
115int main(int argc, char **argv) {
Chris Lattnere6012df2009-03-06 05:34:10 +0000116 // Print a stack trace if we signal out.
117 sys::PrintStackTraceOnErrorSignal();
118 PrettyStackTraceProgram X(argc, argv);
Owen Anderson25209b42009-07-01 16:58:40 +0000119
120 LLVMContext Context;
Chris Lattnere6012df2009-03-06 05:34:10 +0000121 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000122 try {
Dan Gohman6099df82007-10-08 15:45:12 +0000123 cl::ParseCommandLineOptions(argc, argv, "llvm profile dump decoder\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000124
125 // Read in the bitcode file...
126 std::string ErrorMessage;
127 Module *M = 0;
128 if (MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(BitcodeFile,
129 &ErrorMessage)) {
Owen Andersona148fdd2009-07-01 21:22:36 +0000130 M = ParseBitcodeFile(Buffer, Context, &ErrorMessage);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000131 delete Buffer;
132 }
133 if (M == 0) {
134 std::cerr << argv[0] << ": " << BitcodeFile << ": "
135 << ErrorMessage << "\n";
136 return 1;
137 }
138
139 // Read the profiling information
140 ProfileInfoLoader PI(argv[0], ProfileDataFile, *M);
141
142 std::map<const Function *, unsigned> FuncFreqs;
143 std::map<const BasicBlock*, unsigned> BlockFreqs;
144 std::map<ProfileInfoLoader::Edge, unsigned> EdgeFreqs;
145
146 // Output a report. Eventually, there will be multiple reports selectable on
147 // the command line, for now, just keep things simple.
148
149 // Emit the most frequent function table...
150 std::vector<std::pair<Function*, unsigned> > FunctionCounts;
151 PI.getFunctionCounts(FunctionCounts);
152 FuncFreqs.insert(FunctionCounts.begin(), FunctionCounts.end());
153
154 // Sort by the frequency, backwards.
155 sort(FunctionCounts.begin(), FunctionCounts.end(),
156 PairSecondSortReverse<Function*>());
157
158 uint64_t TotalExecutions = 0;
159 for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i)
160 TotalExecutions += FunctionCounts[i].second;
161
162 std::cout << "===" << std::string(73, '-') << "===\n"
163 << "LLVM profiling output for execution";
164 if (PI.getNumExecutions() != 1) std::cout << "s";
165 std::cout << ":\n";
166
167 for (unsigned i = 0, e = PI.getNumExecutions(); i != e; ++i) {
168 std::cout << " ";
169 if (e != 1) std::cout << i+1 << ". ";
170 std::cout << PI.getExecution(i) << "\n";
171 }
172
173 std::cout << "\n===" << std::string(73, '-') << "===\n";
174 std::cout << "Function execution frequencies:\n\n";
175
176 // Print out the function frequencies...
177 std::cout << " ## Frequency\n";
178 for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i) {
179 if (FunctionCounts[i].second == 0) {
180 std::cout << "\n NOTE: " << e-i << " function" <<
181 (e-i-1 ? "s were" : " was") << " never executed!\n";
182 break;
183 }
184
185 std::cout << std::setw(3) << i+1 << ". "
186 << std::setw(5) << FunctionCounts[i].second << "/"
187 << TotalExecutions << " "
188 << FunctionCounts[i].first->getName().c_str() << "\n";
189 }
190
191 std::set<Function*> FunctionsToPrint;
192
193 // If we have block count information, print out the LLVM module with
194 // frequency annotations.
195 if (PI.hasAccurateBlockCounts()) {
196 std::vector<std::pair<BasicBlock*, unsigned> > Counts;
197 PI.getBlockCounts(Counts);
198
199 TotalExecutions = 0;
200 for (unsigned i = 0, e = Counts.size(); i != e; ++i)
201 TotalExecutions += Counts[i].second;
202
203 // Sort by the frequency, backwards.
204 sort(Counts.begin(), Counts.end(),
205 PairSecondSortReverse<BasicBlock*>());
206
207 std::cout << "\n===" << std::string(73, '-') << "===\n";
208 std::cout << "Top 20 most frequently executed basic blocks:\n\n";
209
210 // Print out the function frequencies...
211 std::cout <<" ## %% \tFrequency\n";
212 unsigned BlocksToPrint = Counts.size();
213 if (BlocksToPrint > 20) BlocksToPrint = 20;
214 for (unsigned i = 0; i != BlocksToPrint; ++i) {
215 if (Counts[i].second == 0) break;
216 Function *F = Counts[i].first->getParent();
217 std::cout << std::setw(3) << i+1 << ". "
218 << std::setw(5) << std::setprecision(2)
219 << Counts[i].second/(double)TotalExecutions*100 << "% "
220 << std::setw(5) << Counts[i].second << "/"
221 << TotalExecutions << "\t"
222 << F->getName().c_str() << "() - "
223 << Counts[i].first->getName().c_str() << "\n";
224 FunctionsToPrint.insert(F);
225 }
226
227 BlockFreqs.insert(Counts.begin(), Counts.end());
228 }
229
230 if (PI.hasAccurateEdgeCounts()) {
231 std::vector<std::pair<ProfileInfoLoader::Edge, unsigned> > Counts;
232 PI.getEdgeCounts(Counts);
233 EdgeFreqs.insert(Counts.begin(), Counts.end());
234 }
235
236 if (PrintAnnotatedLLVM || PrintAllCode) {
237 std::cout << "\n===" << std::string(73, '-') << "===\n";
238 std::cout << "Annotated LLVM code for the module:\n\n";
239
240 ProfileAnnotator PA(FuncFreqs, BlockFreqs, EdgeFreqs);
241
242 if (FunctionsToPrint.empty() || PrintAllCode)
243 M->print(std::cout, &PA);
244 else
Chris Lattner1fefaac2008-08-23 22:23:09 +0000245 // Print just a subset of the functions.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000246 for (std::set<Function*>::iterator I = FunctionsToPrint.begin(),
247 E = FunctionsToPrint.end(); I != E; ++I)
248 (*I)->print(std::cout, &PA);
249 }
250
251 return 0;
252 } catch (const std::string& msg) {
253 std::cerr << argv[0] << ": " << msg << "\n";
254 } catch (...) {
255 std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
256 }
257 return 1;
258}