blob: f1d24f7527cbd9351dc4eeee5949f3c8d6008874 [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>
33#include <iostream>
34#include <iomanip>
35#include <map>
36#include <set>
37
38using namespace llvm;
39
40namespace {
41 cl::opt<std::string>
42 BitcodeFile(cl::Positional, cl::desc("<program bitcode file>"),
43 cl::Required);
44
45 cl::opt<std::string>
46 ProfileDataFile(cl::Positional, cl::desc("<llvmprof.out file>"),
47 cl::Optional, cl::init("llvmprof.out"));
48
49 cl::opt<bool>
50 PrintAnnotatedLLVM("annotated-llvm",
51 cl::desc("Print LLVM code with frequency annotations"));
52 cl::alias PrintAnnotated2("A", cl::desc("Alias for --annotated-llvm"),
53 cl::aliasopt(PrintAnnotatedLLVM));
54 cl::opt<bool>
55 PrintAllCode("print-all-code",
56 cl::desc("Print annotated code for the entire program"));
57}
58
59// PairSecondSort - A sorting predicate to sort by the second element of a pair.
60template<class T>
61struct PairSecondSortReverse
Daniel Dunbar670c7d72009-08-13 01:55:43 +000062 : public std::binary_function<std::pair<T, double>,
63 std::pair<T, double>, bool> {
64 bool operator()(const std::pair<T, double> &LHS,
65 const std::pair<T, double> &RHS) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000066 return LHS.second > RHS.second;
67 }
68};
69
Daniel Dunbar4ae20272009-08-08 17:43:09 +000070static double ignoreMissing(double w) {
71 if (w == ProfileInfo::MissingValue) return 0;
72 return w;
73}
74
Dan Gohmanf17a25c2007-07-18 16:29:46 +000075namespace {
76 class ProfileAnnotator : public AssemblyAnnotationWriter {
Daniel Dunbar13021f12009-08-05 15:55:56 +000077 ProfileInfo &PI;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000078 public:
Daniel Dunbar13021f12009-08-05 15:55:56 +000079 ProfileAnnotator(ProfileInfo& pi) : PI(pi) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000080
Chris Lattner1fefaac2008-08-23 22:23:09 +000081 virtual void emitFunctionAnnot(const Function *F, raw_ostream &OS) {
Daniel Dunbar4ae20272009-08-08 17:43:09 +000082 double w = PI.getExecutionCount(F);
Daniel Dunbar6d07ef12009-08-08 18:59:03 +000083 if (w != ProfileInfo::MissingValue) {
84 OS << ";;; %" << F->getName() << " called "<<(unsigned)w
85 <<" times.\n;;;\n";
86 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000087 }
88 virtual void emitBasicBlockStartAnnot(const BasicBlock *BB,
Chris Lattner1fefaac2008-08-23 22:23:09 +000089 raw_ostream &OS) {
Daniel Dunbar4ae20272009-08-08 17:43:09 +000090 double w = PI.getExecutionCount(BB);
Daniel Dunbar6d07ef12009-08-08 18:59:03 +000091 if (w != ProfileInfo::MissingValue) {
92 if (w != 0) {
93 OS << "\t;;; Basic block executed " << (unsigned)w << " times.\n";
94 } else {
Daniel Dunbar4ae20272009-08-08 17:43:09 +000095 OS << "\t;;; Never executed!\n";
Daniel Dunbar6d07ef12009-08-08 18:59:03 +000096 }
97 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000098 }
99
Chris Lattner1fefaac2008-08-23 22:23:09 +0000100 virtual void emitBasicBlockEndAnnot(const BasicBlock *BB, raw_ostream &OS) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000101 // Figure out how many times each successor executed.
Daniel Dunbar670c7d72009-08-13 01:55:43 +0000102 std::vector<std::pair<ProfileInfo::Edge, double> > SuccCounts;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000103
Daniel Dunbar13021f12009-08-05 15:55:56 +0000104 const TerminatorInst *TI = BB->getTerminator();
105 for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) {
106 BasicBlock* Succ = TI->getSuccessor(s);
Daniel Dunbar6d07ef12009-08-08 18:59:03 +0000107 double w = ignoreMissing(PI.getEdgeWeight(std::make_pair(BB, Succ)));
108 if (w != 0)
109 SuccCounts.push_back(std::make_pair(std::make_pair(BB, Succ), w));
Daniel Dunbar13021f12009-08-05 15:55:56 +0000110 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000111 if (!SuccCounts.empty()) {
112 OS << "\t;;; Out-edge counts:";
113 for (unsigned i = 0, e = SuccCounts.size(); i != e; ++i)
Daniel Dunbar13021f12009-08-05 15:55:56 +0000114 OS << " [" << (SuccCounts[i]).second << " -> "
115 << (SuccCounts[i]).first.second->getName() << "]";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000116 OS << "\n";
117 }
118 }
119 };
120}
121
Daniel Dunbar7a9aab02009-07-14 07:41:11 +0000122namespace {
123 /// ProfileInfoPrinterPass - Helper pass to dump the profile information for
124 /// a module.
125 //
126 // FIXME: This should move elsewhere.
127 class ProfileInfoPrinterPass : public ModulePass {
128 ProfileInfoLoader &PIL;
129 public:
130 static char ID; // Class identification, replacement for typeinfo.
131 explicit ProfileInfoPrinterPass(ProfileInfoLoader &_PIL)
132 : ModulePass(&ID), PIL(_PIL) {}
133
134 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
135 AU.setPreservesAll();
136 AU.addRequired<ProfileInfo>();
137 }
138
139 bool runOnModule(Module &M);
140 };
141}
142
143char ProfileInfoPrinterPass::ID = 0;
144
145bool ProfileInfoPrinterPass::runOnModule(Module &M) {
Daniel Dunbar13021f12009-08-05 15:55:56 +0000146 ProfileInfo &PI = getAnalysis<ProfileInfo>();
Daniel Dunbar7a9aab02009-07-14 07:41:11 +0000147 std::map<const Function *, unsigned> FuncFreqs;
148 std::map<const BasicBlock*, unsigned> BlockFreqs;
Daniel Dunbar13021f12009-08-05 15:55:56 +0000149 std::map<ProfileInfo::Edge, unsigned> EdgeFreqs;
Daniel Dunbar7a9aab02009-07-14 07:41:11 +0000150
151 // Output a report. Eventually, there will be multiple reports selectable on
152 // the command line, for now, just keep things simple.
153
154 // Emit the most frequent function table...
Daniel Dunbar670c7d72009-08-13 01:55:43 +0000155 std::vector<std::pair<Function*, double> > FunctionCounts;
156 std::vector<std::pair<BasicBlock*, double> > Counts;
Daniel Dunbar13021f12009-08-05 15:55:56 +0000157 for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI) {
Daniel Dunbar23505092009-08-05 21:51:16 +0000158 if (FI->isDeclaration()) continue;
Daniel Dunbar4ae20272009-08-08 17:43:09 +0000159 double w = ignoreMissing(PI.getExecutionCount(FI));
Daniel Dunbar6d07ef12009-08-08 18:59:03 +0000160 FunctionCounts.push_back(std::make_pair(FI, w));
Daniel Dunbar13021f12009-08-05 15:55:56 +0000161 for (Function::iterator BB = FI->begin(), BBE = FI->end();
162 BB != BBE; ++BB) {
Daniel Dunbar4ae20272009-08-08 17:43:09 +0000163 double w = ignoreMissing(PI.getExecutionCount(BB));
Daniel Dunbar6d07ef12009-08-08 18:59:03 +0000164 Counts.push_back(std::make_pair(BB, w));
Daniel Dunbar13021f12009-08-05 15:55:56 +0000165 }
166 }
Daniel Dunbar7a9aab02009-07-14 07:41:11 +0000167
168 // Sort by the frequency, backwards.
169 sort(FunctionCounts.begin(), FunctionCounts.end(),
170 PairSecondSortReverse<Function*>());
171
Daniel Dunbar670c7d72009-08-13 01:55:43 +0000172 double TotalExecutions = 0;
Daniel Dunbar7a9aab02009-07-14 07:41:11 +0000173 for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i)
174 TotalExecutions += FunctionCounts[i].second;
175
Andreas Neustifter3e64a6c2009-08-26 09:05:21 +0000176 outs() << "===" << std::string(73, '-') << "===\n"
177 << "LLVM profiling output for execution";
178 if (PIL.getNumExecutions() != 1) outs() << "s";
179 outs() << ":\n";
Daniel Dunbar7a9aab02009-07-14 07:41:11 +0000180
181 for (unsigned i = 0, e = PIL.getNumExecutions(); i != e; ++i) {
Andreas Neustifter3e64a6c2009-08-26 09:05:21 +0000182 outs() << " ";
183 if (e != 1) outs() << i+1 << ". ";
184 outs() << PIL.getExecution(i) << "\n";
Daniel Dunbar7a9aab02009-07-14 07:41:11 +0000185 }
186
Andreas Neustifter3e64a6c2009-08-26 09:05:21 +0000187 outs() << "\n===" << std::string(73, '-') << "===\n";
188 outs() << "Function execution frequencies:\n\n";
Daniel Dunbar7a9aab02009-07-14 07:41:11 +0000189
190 // Print out the function frequencies...
Andreas Neustifter3e64a6c2009-08-26 09:05:21 +0000191 outs() << " ## Frequency\n";
Daniel Dunbar7a9aab02009-07-14 07:41:11 +0000192 for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i) {
193 if (FunctionCounts[i].second == 0) {
Andreas Neustifter3e64a6c2009-08-26 09:05:21 +0000194 outs() << "\n NOTE: " << e-i << " function"
195 << (e-i-1 ? "s were" : " was") << " never executed!\n";
Daniel Dunbar7a9aab02009-07-14 07:41:11 +0000196 break;
197 }
198
Andreas Neustifter3e64a6c2009-08-26 09:05:21 +0000199 outs() << format("%3d", i+1) << ". "
200 << format("%5.2g", FunctionCounts[i].second) << "/"
201 << format("%g", TotalExecutions) << " "
Daniel Dunbar7c8e5732009-07-25 00:43:31 +0000202 << FunctionCounts[i].first->getNameStr() << "\n";
Daniel Dunbar7a9aab02009-07-14 07:41:11 +0000203 }
204
205 std::set<Function*> FunctionsToPrint;
206
Daniel Dunbar13021f12009-08-05 15:55:56 +0000207 TotalExecutions = 0;
208 for (unsigned i = 0, e = Counts.size(); i != e; ++i)
209 TotalExecutions += Counts[i].second;
210
211 // Sort by the frequency, backwards.
212 sort(Counts.begin(), Counts.end(),
213 PairSecondSortReverse<BasicBlock*>());
214
Andreas Neustifter3e64a6c2009-08-26 09:05:21 +0000215 outs() << "\n===" << std::string(73, '-') << "===\n";
216 outs() << "Top 20 most frequently executed basic blocks:\n\n";
Daniel Dunbar13021f12009-08-05 15:55:56 +0000217
218 // Print out the function frequencies...
Andreas Neustifter3e64a6c2009-08-26 09:05:21 +0000219 outs() <<" ## %% \tFrequency\n";
Daniel Dunbar13021f12009-08-05 15:55:56 +0000220 unsigned BlocksToPrint = Counts.size();
221 if (BlocksToPrint > 20) BlocksToPrint = 20;
222 for (unsigned i = 0; i != BlocksToPrint; ++i) {
223 if (Counts[i].second == 0) break;
224 Function *F = Counts[i].first->getParent();
Andreas Neustifter3e64a6c2009-08-26 09:05:21 +0000225 outs() << format("%3d", i+1) << ". "
226 << format("%5g", Counts[i].second/(double)TotalExecutions*100) << "% "
227 << format("%5.0f", Counts[i].second) << "/"
228 << format("%g", TotalExecutions) << "\t"
229 << F->getNameStr() << "() - "
230 << Counts[i].first->getNameStr() << "\n";
Daniel Dunbar13021f12009-08-05 15:55:56 +0000231 FunctionsToPrint.insert(F);
Daniel Dunbar7a9aab02009-07-14 07:41:11 +0000232 }
233
234 if (PrintAnnotatedLLVM || PrintAllCode) {
Andreas Neustifter3e64a6c2009-08-26 09:05:21 +0000235 outs() << "\n===" << std::string(73, '-') << "===\n";
236 outs() << "Annotated LLVM code for the module:\n\n";
Daniel Dunbar13021f12009-08-05 15:55:56 +0000237
238 ProfileAnnotator PA(PI);
Daniel Dunbar7a9aab02009-07-14 07:41:11 +0000239
240 if (FunctionsToPrint.empty() || PrintAllCode)
Chris Lattnerb0659d42009-08-23 04:52:46 +0000241 M.print(outs(), &PA);
Daniel Dunbar7a9aab02009-07-14 07:41:11 +0000242 else
243 // Print just a subset of the functions.
244 for (std::set<Function*>::iterator I = FunctionsToPrint.begin(),
245 E = FunctionsToPrint.end(); I != E; ++I)
Chris Lattner8a6411c2009-08-23 04:37:46 +0000246 (*I)->print(outs(), &PA);
Daniel Dunbar7a9aab02009-07-14 07:41:11 +0000247 }
248
249 return false;
250}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000251
252int main(int argc, char **argv) {
Chris Lattnere6012df2009-03-06 05:34:10 +0000253 // Print a stack trace if we signal out.
254 sys::PrintStackTraceOnErrorSignal();
255 PrettyStackTraceProgram X(argc, argv);
Owen Anderson25209b42009-07-01 16:58:40 +0000256
Owen Andersone84b8b32009-07-15 22:16:10 +0000257 LLVMContext &Context = getGlobalContext();
Chris Lattnere6012df2009-03-06 05:34:10 +0000258 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000259 try {
Dan Gohman6099df82007-10-08 15:45:12 +0000260 cl::ParseCommandLineOptions(argc, argv, "llvm profile dump decoder\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000261
262 // Read in the bitcode file...
263 std::string ErrorMessage;
264 Module *M = 0;
265 if (MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(BitcodeFile,
266 &ErrorMessage)) {
Owen Andersona148fdd2009-07-01 21:22:36 +0000267 M = ParseBitcodeFile(Buffer, Context, &ErrorMessage);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000268 delete Buffer;
269 }
270 if (M == 0) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000271 errs() << argv[0] << ": " << BitcodeFile << ": "
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000272 << ErrorMessage << "\n";
273 return 1;
274 }
275
Daniel Dunbar7a9aab02009-07-14 07:41:11 +0000276 // Read the profiling information. This is redundant since we load it again
277 // using the standard profile info provider pass, but for now this gives us
278 // access to additional information not exposed via the ProfileInfo
279 // interface.
280 ProfileInfoLoader PIL(argv[0], ProfileDataFile, *M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000281
Daniel Dunbar7a9aab02009-07-14 07:41:11 +0000282 // Run the printer pass.
283 PassManager PassMgr;
284 PassMgr.add(createProfileLoaderPass(ProfileDataFile));
285 PassMgr.add(new ProfileInfoPrinterPass(PIL));
286 PassMgr.run(*M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000287
288 return 0;
289 } catch (const std::string& msg) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000290 errs() << argv[0] << ": " << msg << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000291 } catch (...) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000292 errs() << argv[0] << ": Unexpected unknown exception occurred.\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000293 }
Daniel Dunbar7a9aab02009-07-14 07:41:11 +0000294
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000295 return 1;
296}