blob: 88adeb43e6de35390d500cef99c5e8d58786a563 [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"
Daniel Dunbar7a9aab02009-07-14 07:41:11 +000019#include "llvm/PassManager.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000020#include "llvm/Assembly/AsmAnnotationWriter.h"
Daniel Dunbar7a9aab02009-07-14 07:41:11 +000021#include "llvm/Analysis/ProfileInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000022#include "llvm/Analysis/ProfileInfoLoader.h"
Daniel Dunbar13021f12009-08-05 15:55:56 +000023#include "llvm/Analysis/Passes.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000024#include "llvm/Bitcode/ReaderWriter.h"
25#include "llvm/Support/CommandLine.h"
26#include "llvm/Support/ManagedStatic.h"
27#include "llvm/Support/MemoryBuffer.h"
Chris Lattnere6012df2009-03-06 05:34:10 +000028#include "llvm/Support/PrettyStackTrace.h"
Chris Lattner1fefaac2008-08-23 22:23:09 +000029#include "llvm/Support/raw_ostream.h"
Andreas Neustifter3e64a6c2009-08-26 09:05:21 +000030#include "llvm/Support/Format.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000031#include "llvm/System/Signals.h"
32#include <algorithm>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033#include <iomanip>
34#include <map>
35#include <set>
36
37using namespace llvm;
38
39namespace {
40 cl::opt<std::string>
41 BitcodeFile(cl::Positional, cl::desc("<program bitcode file>"),
42 cl::Required);
43
44 cl::opt<std::string>
45 ProfileDataFile(cl::Positional, cl::desc("<llvmprof.out file>"),
46 cl::Optional, cl::init("llvmprof.out"));
47
48 cl::opt<bool>
49 PrintAnnotatedLLVM("annotated-llvm",
50 cl::desc("Print LLVM code with frequency annotations"));
51 cl::alias PrintAnnotated2("A", cl::desc("Alias for --annotated-llvm"),
52 cl::aliasopt(PrintAnnotatedLLVM));
53 cl::opt<bool>
54 PrintAllCode("print-all-code",
55 cl::desc("Print annotated code for the entire program"));
56}
57
58// PairSecondSort - A sorting predicate to sort by the second element of a pair.
59template<class T>
60struct PairSecondSortReverse
Daniel Dunbar670c7d72009-08-13 01:55:43 +000061 : public std::binary_function<std::pair<T, double>,
62 std::pair<T, double>, bool> {
63 bool operator()(const std::pair<T, double> &LHS,
64 const std::pair<T, double> &RHS) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000065 return LHS.second > RHS.second;
66 }
67};
68
Daniel Dunbar4ae20272009-08-08 17:43:09 +000069static double ignoreMissing(double w) {
70 if (w == ProfileInfo::MissingValue) return 0;
71 return w;
72}
73
Dan Gohmanf17a25c2007-07-18 16:29:46 +000074namespace {
75 class ProfileAnnotator : public AssemblyAnnotationWriter {
Daniel Dunbar13021f12009-08-05 15:55:56 +000076 ProfileInfo &PI;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000077 public:
Daniel Dunbar13021f12009-08-05 15:55:56 +000078 ProfileAnnotator(ProfileInfo& pi) : PI(pi) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000079
Chris Lattner1fefaac2008-08-23 22:23:09 +000080 virtual void emitFunctionAnnot(const Function *F, raw_ostream &OS) {
Daniel Dunbar4ae20272009-08-08 17:43:09 +000081 double w = PI.getExecutionCount(F);
Daniel Dunbar6d07ef12009-08-08 18:59:03 +000082 if (w != ProfileInfo::MissingValue) {
83 OS << ";;; %" << F->getName() << " called "<<(unsigned)w
84 <<" times.\n;;;\n";
85 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000086 }
87 virtual void emitBasicBlockStartAnnot(const BasicBlock *BB,
Chris Lattner1fefaac2008-08-23 22:23:09 +000088 raw_ostream &OS) {
Daniel Dunbar4ae20272009-08-08 17:43:09 +000089 double w = PI.getExecutionCount(BB);
Daniel Dunbar6d07ef12009-08-08 18:59:03 +000090 if (w != ProfileInfo::MissingValue) {
91 if (w != 0) {
92 OS << "\t;;; Basic block executed " << (unsigned)w << " times.\n";
93 } else {
Daniel Dunbar4ae20272009-08-08 17:43:09 +000094 OS << "\t;;; Never executed!\n";
Daniel Dunbar6d07ef12009-08-08 18:59:03 +000095 }
96 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000097 }
98
Chris Lattner1fefaac2008-08-23 22:23:09 +000099 virtual void emitBasicBlockEndAnnot(const BasicBlock *BB, raw_ostream &OS) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000100 // Figure out how many times each successor executed.
Daniel Dunbar670c7d72009-08-13 01:55:43 +0000101 std::vector<std::pair<ProfileInfo::Edge, double> > SuccCounts;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000102
Daniel Dunbar13021f12009-08-05 15:55:56 +0000103 const TerminatorInst *TI = BB->getTerminator();
104 for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) {
105 BasicBlock* Succ = TI->getSuccessor(s);
Daniel Dunbar6d07ef12009-08-08 18:59:03 +0000106 double w = ignoreMissing(PI.getEdgeWeight(std::make_pair(BB, Succ)));
107 if (w != 0)
108 SuccCounts.push_back(std::make_pair(std::make_pair(BB, Succ), w));
Daniel Dunbar13021f12009-08-05 15:55:56 +0000109 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000110 if (!SuccCounts.empty()) {
111 OS << "\t;;; Out-edge counts:";
112 for (unsigned i = 0, e = SuccCounts.size(); i != e; ++i)
Daniel Dunbar13021f12009-08-05 15:55:56 +0000113 OS << " [" << (SuccCounts[i]).second << " -> "
114 << (SuccCounts[i]).first.second->getName() << "]";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000115 OS << "\n";
116 }
117 }
118 };
119}
120
Daniel Dunbar7a9aab02009-07-14 07:41:11 +0000121namespace {
122 /// ProfileInfoPrinterPass - Helper pass to dump the profile information for
123 /// a module.
124 //
125 // FIXME: This should move elsewhere.
126 class ProfileInfoPrinterPass : public ModulePass {
127 ProfileInfoLoader &PIL;
128 public:
129 static char ID; // Class identification, replacement for typeinfo.
130 explicit ProfileInfoPrinterPass(ProfileInfoLoader &_PIL)
131 : ModulePass(&ID), PIL(_PIL) {}
132
133 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
134 AU.setPreservesAll();
135 AU.addRequired<ProfileInfo>();
136 }
137
138 bool runOnModule(Module &M);
139 };
140}
141
142char ProfileInfoPrinterPass::ID = 0;
143
144bool ProfileInfoPrinterPass::runOnModule(Module &M) {
Daniel Dunbar13021f12009-08-05 15:55:56 +0000145 ProfileInfo &PI = getAnalysis<ProfileInfo>();
Daniel Dunbar7a9aab02009-07-14 07:41:11 +0000146 std::map<const Function *, unsigned> FuncFreqs;
147 std::map<const BasicBlock*, unsigned> BlockFreqs;
Daniel Dunbar13021f12009-08-05 15:55:56 +0000148 std::map<ProfileInfo::Edge, unsigned> EdgeFreqs;
Daniel Dunbar7a9aab02009-07-14 07:41:11 +0000149
150 // Output a report. Eventually, there will be multiple reports selectable on
151 // the command line, for now, just keep things simple.
152
153 // Emit the most frequent function table...
Daniel Dunbar670c7d72009-08-13 01:55:43 +0000154 std::vector<std::pair<Function*, double> > FunctionCounts;
155 std::vector<std::pair<BasicBlock*, double> > Counts;
Daniel Dunbar13021f12009-08-05 15:55:56 +0000156 for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI) {
Daniel Dunbar23505092009-08-05 21:51:16 +0000157 if (FI->isDeclaration()) continue;
Daniel Dunbar4ae20272009-08-08 17:43:09 +0000158 double w = ignoreMissing(PI.getExecutionCount(FI));
Daniel Dunbar6d07ef12009-08-08 18:59:03 +0000159 FunctionCounts.push_back(std::make_pair(FI, w));
Daniel Dunbar13021f12009-08-05 15:55:56 +0000160 for (Function::iterator BB = FI->begin(), BBE = FI->end();
161 BB != BBE; ++BB) {
Daniel Dunbar4ae20272009-08-08 17:43:09 +0000162 double w = ignoreMissing(PI.getExecutionCount(BB));
Daniel Dunbar6d07ef12009-08-08 18:59:03 +0000163 Counts.push_back(std::make_pair(BB, w));
Daniel Dunbar13021f12009-08-05 15:55:56 +0000164 }
165 }
Daniel Dunbar7a9aab02009-07-14 07:41:11 +0000166
167 // Sort by the frequency, backwards.
168 sort(FunctionCounts.begin(), FunctionCounts.end(),
169 PairSecondSortReverse<Function*>());
170
Daniel Dunbar670c7d72009-08-13 01:55:43 +0000171 double TotalExecutions = 0;
Daniel Dunbar7a9aab02009-07-14 07:41:11 +0000172 for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i)
173 TotalExecutions += FunctionCounts[i].second;
174
Andreas Neustifter3e64a6c2009-08-26 09:05:21 +0000175 outs() << "===" << std::string(73, '-') << "===\n"
176 << "LLVM profiling output for execution";
177 if (PIL.getNumExecutions() != 1) outs() << "s";
178 outs() << ":\n";
Daniel Dunbar7a9aab02009-07-14 07:41:11 +0000179
180 for (unsigned i = 0, e = PIL.getNumExecutions(); i != e; ++i) {
Andreas Neustifter3e64a6c2009-08-26 09:05:21 +0000181 outs() << " ";
182 if (e != 1) outs() << i+1 << ". ";
183 outs() << PIL.getExecution(i) << "\n";
Daniel Dunbar7a9aab02009-07-14 07:41:11 +0000184 }
185
Andreas Neustifter3e64a6c2009-08-26 09:05:21 +0000186 outs() << "\n===" << std::string(73, '-') << "===\n";
187 outs() << "Function execution frequencies:\n\n";
Daniel Dunbar7a9aab02009-07-14 07:41:11 +0000188
189 // Print out the function frequencies...
Andreas Neustifter3e64a6c2009-08-26 09:05:21 +0000190 outs() << " ## Frequency\n";
Daniel Dunbar7a9aab02009-07-14 07:41:11 +0000191 for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i) {
192 if (FunctionCounts[i].second == 0) {
Andreas Neustifter3e64a6c2009-08-26 09:05:21 +0000193 outs() << "\n NOTE: " << e-i << " function"
194 << (e-i-1 ? "s were" : " was") << " never executed!\n";
Daniel Dunbar7a9aab02009-07-14 07:41:11 +0000195 break;
196 }
197
Andreas Neustifter3e64a6c2009-08-26 09:05:21 +0000198 outs() << format("%3d", i+1) << ". "
199 << format("%5.2g", FunctionCounts[i].second) << "/"
200 << format("%g", TotalExecutions) << " "
Daniel Dunbar7c8e5732009-07-25 00:43:31 +0000201 << FunctionCounts[i].first->getNameStr() << "\n";
Daniel Dunbar7a9aab02009-07-14 07:41:11 +0000202 }
203
204 std::set<Function*> FunctionsToPrint;
205
Daniel Dunbar13021f12009-08-05 15:55:56 +0000206 TotalExecutions = 0;
207 for (unsigned i = 0, e = Counts.size(); i != e; ++i)
208 TotalExecutions += Counts[i].second;
209
210 // Sort by the frequency, backwards.
211 sort(Counts.begin(), Counts.end(),
212 PairSecondSortReverse<BasicBlock*>());
213
Andreas Neustifter3e64a6c2009-08-26 09:05:21 +0000214 outs() << "\n===" << std::string(73, '-') << "===\n";
215 outs() << "Top 20 most frequently executed basic blocks:\n\n";
Daniel Dunbar13021f12009-08-05 15:55:56 +0000216
217 // Print out the function frequencies...
Andreas Neustifter3e64a6c2009-08-26 09:05:21 +0000218 outs() <<" ## %% \tFrequency\n";
Daniel Dunbar13021f12009-08-05 15:55:56 +0000219 unsigned BlocksToPrint = Counts.size();
220 if (BlocksToPrint > 20) BlocksToPrint = 20;
221 for (unsigned i = 0; i != BlocksToPrint; ++i) {
222 if (Counts[i].second == 0) break;
223 Function *F = Counts[i].first->getParent();
Andreas Neustifter3e64a6c2009-08-26 09:05:21 +0000224 outs() << format("%3d", i+1) << ". "
225 << format("%5g", Counts[i].second/(double)TotalExecutions*100) << "% "
226 << format("%5.0f", Counts[i].second) << "/"
227 << format("%g", TotalExecutions) << "\t"
228 << F->getNameStr() << "() - "
229 << Counts[i].first->getNameStr() << "\n";
Daniel Dunbar13021f12009-08-05 15:55:56 +0000230 FunctionsToPrint.insert(F);
Daniel Dunbar7a9aab02009-07-14 07:41:11 +0000231 }
232
233 if (PrintAnnotatedLLVM || PrintAllCode) {
Andreas Neustifter3e64a6c2009-08-26 09:05:21 +0000234 outs() << "\n===" << std::string(73, '-') << "===\n";
235 outs() << "Annotated LLVM code for the module:\n\n";
Daniel Dunbar13021f12009-08-05 15:55:56 +0000236
237 ProfileAnnotator PA(PI);
Daniel Dunbar7a9aab02009-07-14 07:41:11 +0000238
239 if (FunctionsToPrint.empty() || PrintAllCode)
Chris Lattnerb0659d42009-08-23 04:52:46 +0000240 M.print(outs(), &PA);
Daniel Dunbar7a9aab02009-07-14 07:41:11 +0000241 else
242 // Print just a subset of the functions.
243 for (std::set<Function*>::iterator I = FunctionsToPrint.begin(),
244 E = FunctionsToPrint.end(); I != E; ++I)
Chris Lattner8a6411c2009-08-23 04:37:46 +0000245 (*I)->print(outs(), &PA);
Daniel Dunbar7a9aab02009-07-14 07:41:11 +0000246 }
247
248 return false;
249}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000250
251int main(int argc, char **argv) {
Chris Lattnere6012df2009-03-06 05:34:10 +0000252 // Print a stack trace if we signal out.
253 sys::PrintStackTraceOnErrorSignal();
254 PrettyStackTraceProgram X(argc, argv);
Owen Anderson25209b42009-07-01 16:58:40 +0000255
Owen Andersone84b8b32009-07-15 22:16:10 +0000256 LLVMContext &Context = getGlobalContext();
Chris Lattnere6012df2009-03-06 05:34:10 +0000257 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Daniel Dunbar7a9aab02009-07-14 07:41:11 +0000258
Chris Lattnerc343f962009-10-22 00:50:24 +0000259 cl::ParseCommandLineOptions(argc, argv, "llvm profile dump decoder\n");
260
261 // Read in the bitcode file...
262 std::string ErrorMessage;
263 Module *M = 0;
264 if (MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(BitcodeFile,
265 &ErrorMessage)) {
266 M = ParseBitcodeFile(Buffer, Context, &ErrorMessage);
267 delete Buffer;
268 }
269 if (M == 0) {
270 errs() << argv[0] << ": " << BitcodeFile << ": "
271 << ErrorMessage << "\n";
272 return 1;
273 }
274
275 // Read the profiling information. This is redundant since we load it again
276 // using the standard profile info provider pass, but for now this gives us
277 // access to additional information not exposed via the ProfileInfo
278 // interface.
279 ProfileInfoLoader PIL(argv[0], ProfileDataFile, *M);
280
281 // Run the printer pass.
282 PassManager PassMgr;
283 PassMgr.add(createProfileLoaderPass(ProfileDataFile));
284 PassMgr.add(new ProfileInfoPrinterPass(PIL));
285 PassMgr.run(*M);
286
287 return 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000288}