blob: d9b671336407e96e2f4f95f936711a3b0ac57738 [file] [log] [blame]
Chris Lattner6f82d072003-10-28 19:16:35 +00001//===- llvm-prof.cpp - Read in and process llvmprof.out data files --------===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
Chris Lattner6f82d072003-10-28 19:16:35 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner21c62da2007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman3da94ae2005-04-22 00:00:37 +00007//
Chris Lattner6f82d072003-10-28 19:16:35 +00008//===----------------------------------------------------------------------===//
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
Chris Lattner8c2730e2004-03-08 20:04:32 +000016#include "llvm/InstrTypes.h"
Owen Anderson8b477ed2009-07-01 16:58:40 +000017#include "llvm/LLVMContext.h"
Chris Lattner5e717642003-10-30 23:42:09 +000018#include "llvm/Module.h"
Daniel Dunbar314fa8e2009-07-14 07:41:11 +000019#include "llvm/PassManager.h"
Chris Lattner1dbb3872010-09-02 23:09:42 +000020#include "llvm/Assembly/AssemblyAnnotationWriter.h"
Daniel Dunbar314fa8e2009-07-14 07:41:11 +000021#include "llvm/Analysis/ProfileInfo.h"
Chris Lattner89cf3932004-02-11 05:56:07 +000022#include "llvm/Analysis/ProfileInfoLoader.h"
Daniel Dunbaree166382009-08-05 15:55:56 +000023#include "llvm/Analysis/Passes.h"
Chris Lattner592488a2007-05-06 04:43:00 +000024#include "llvm/Bitcode/ReaderWriter.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000025#include "llvm/Support/CommandLine.h"
Chris Lattnera11a6a02010-09-02 23:07:12 +000026#include "llvm/Support/FormattedStream.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000027#include "llvm/Support/ManagedStatic.h"
Chris Lattner592488a2007-05-06 04:43:00 +000028#include "llvm/Support/MemoryBuffer.h"
Chris Lattnercc14d252009-03-06 05:34:10 +000029#include "llvm/Support/PrettyStackTrace.h"
Chris Lattner944fac72008-08-23 22:23:09 +000030#include "llvm/Support/raw_ostream.h"
Andreas Neustifter30457f22009-08-26 09:05:21 +000031#include "llvm/Support/Format.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000032#include "llvm/Support/Signals.h"
Michael J. Spencer333fb042010-12-09 17:36:48 +000033#include "llvm/Support/system_error.h"
Jeff Cohenca5183d2007-03-05 00:00:42 +000034#include <algorithm>
Reid Spencer78b0e6a2005-12-29 21:13:45 +000035#include <iomanip>
Chris Lattner33f1ca72003-10-28 21:25:23 +000036#include <map>
Chris Lattner5e717642003-10-30 23:42:09 +000037#include <set>
Chris Lattner6f82d072003-10-28 19:16:35 +000038
Brian Gaeked0fde302003-11-11 22:41:34 +000039using namespace llvm;
40
Chris Lattner6f82d072003-10-28 19:16:35 +000041namespace {
Reid Spencer1adc3de2005-12-30 09:07:29 +000042 cl::opt<std::string>
Gabor Greifa99be512007-07-05 17:07:56 +000043 BitcodeFile(cl::Positional, cl::desc("<program bitcode file>"),
44 cl::Required);
Chris Lattner6f82d072003-10-28 19:16:35 +000045
Reid Spencer1adc3de2005-12-30 09:07:29 +000046 cl::opt<std::string>
Chris Lattner6f82d072003-10-28 19:16:35 +000047 ProfileDataFile(cl::Positional, cl::desc("<llvmprof.out file>"),
48 cl::Optional, cl::init("llvmprof.out"));
Chris Lattner5e717642003-10-30 23:42:09 +000049
50 cl::opt<bool>
51 PrintAnnotatedLLVM("annotated-llvm",
52 cl::desc("Print LLVM code with frequency annotations"));
53 cl::alias PrintAnnotated2("A", cl::desc("Alias for --annotated-llvm"),
54 cl::aliasopt(PrintAnnotatedLLVM));
Chris Lattnercde1cf32003-11-06 20:29:25 +000055 cl::opt<bool>
56 PrintAllCode("print-all-code",
57 cl::desc("Print annotated code for the entire program"));
Chris Lattner6f82d072003-10-28 19:16:35 +000058}
59
Chris Lattner7a78d812003-10-28 21:08:18 +000060// PairSecondSort - A sorting predicate to sort by the second element of a pair.
61template<class T>
Chris Lattner18884a82003-10-29 21:41:17 +000062struct PairSecondSortReverse
Daniel Dunbar3cdfb4a2009-08-13 01:55:43 +000063 : public std::binary_function<std::pair<T, double>,
64 std::pair<T, double>, bool> {
65 bool operator()(const std::pair<T, double> &LHS,
66 const std::pair<T, double> &RHS) const {
Chris Lattner18884a82003-10-29 21:41:17 +000067 return LHS.second > RHS.second;
Chris Lattner7a78d812003-10-28 21:08:18 +000068 }
69};
70
Daniel Dunbarcaaa4932009-08-08 17:43:09 +000071static double ignoreMissing(double w) {
72 if (w == ProfileInfo::MissingValue) return 0;
73 return w;
74}
75
Chris Lattner5e717642003-10-30 23:42:09 +000076namespace {
77 class ProfileAnnotator : public AssemblyAnnotationWriter {
Daniel Dunbaree166382009-08-05 15:55:56 +000078 ProfileInfo &PI;
Chris Lattner5e717642003-10-30 23:42:09 +000079 public:
Chris Lattnera11a6a02010-09-02 23:07:12 +000080 ProfileAnnotator(ProfileInfo &pi) : PI(pi) {}
Chris Lattner5e717642003-10-30 23:42:09 +000081
Chris Lattnera11a6a02010-09-02 23:07:12 +000082 virtual void emitFunctionAnnot(const Function *F,
83 formatted_raw_ostream &OS) {
Daniel Dunbarcaaa4932009-08-08 17:43:09 +000084 double w = PI.getExecutionCount(F);
Daniel Dunbarc43782c2009-08-08 18:59:03 +000085 if (w != ProfileInfo::MissingValue) {
86 OS << ";;; %" << F->getName() << " called "<<(unsigned)w
87 <<" times.\n;;;\n";
88 }
Chris Lattner5e717642003-10-30 23:42:09 +000089 }
Chris Lattner8c2730e2004-03-08 20:04:32 +000090 virtual void emitBasicBlockStartAnnot(const BasicBlock *BB,
Chris Lattnera11a6a02010-09-02 23:07:12 +000091 formatted_raw_ostream &OS) {
Daniel Dunbarcaaa4932009-08-08 17:43:09 +000092 double w = PI.getExecutionCount(BB);
Daniel Dunbarc43782c2009-08-08 18:59:03 +000093 if (w != ProfileInfo::MissingValue) {
94 if (w != 0) {
95 OS << "\t;;; Basic block executed " << (unsigned)w << " times.\n";
96 } else {
Daniel Dunbarcaaa4932009-08-08 17:43:09 +000097 OS << "\t;;; Never executed!\n";
Daniel Dunbarc43782c2009-08-08 18:59:03 +000098 }
99 }
Chris Lattner8c2730e2004-03-08 20:04:32 +0000100 }
101
Chris Lattnera11a6a02010-09-02 23:07:12 +0000102 virtual void emitBasicBlockEndAnnot(const BasicBlock *BB,
103 formatted_raw_ostream &OS) {
Chris Lattner8c2730e2004-03-08 20:04:32 +0000104 // Figure out how many times each successor executed.
Daniel Dunbar3cdfb4a2009-08-13 01:55:43 +0000105 std::vector<std::pair<ProfileInfo::Edge, double> > SuccCounts;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000106
Daniel Dunbaree166382009-08-05 15:55:56 +0000107 const TerminatorInst *TI = BB->getTerminator();
108 for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) {
109 BasicBlock* Succ = TI->getSuccessor(s);
Daniel Dunbarc43782c2009-08-08 18:59:03 +0000110 double w = ignoreMissing(PI.getEdgeWeight(std::make_pair(BB, Succ)));
111 if (w != 0)
112 SuccCounts.push_back(std::make_pair(std::make_pair(BB, Succ), w));
Daniel Dunbaree166382009-08-05 15:55:56 +0000113 }
Chris Lattner8c2730e2004-03-08 20:04:32 +0000114 if (!SuccCounts.empty()) {
115 OS << "\t;;; Out-edge counts:";
116 for (unsigned i = 0, e = SuccCounts.size(); i != e; ++i)
Daniel Dunbaree166382009-08-05 15:55:56 +0000117 OS << " [" << (SuccCounts[i]).second << " -> "
118 << (SuccCounts[i]).first.second->getName() << "]";
Chris Lattner8c2730e2004-03-08 20:04:32 +0000119 OS << "\n";
120 }
Chris Lattner5e717642003-10-30 23:42:09 +0000121 }
122 };
123}
124
Daniel Dunbar314fa8e2009-07-14 07:41:11 +0000125namespace {
126 /// ProfileInfoPrinterPass - Helper pass to dump the profile information for
127 /// a module.
128 //
129 // FIXME: This should move elsewhere.
130 class ProfileInfoPrinterPass : public ModulePass {
131 ProfileInfoLoader &PIL;
132 public:
133 static char ID; // Class identification, replacement for typeinfo.
134 explicit ProfileInfoPrinterPass(ProfileInfoLoader &_PIL)
Owen Anderson90c579d2010-08-06 18:33:48 +0000135 : ModulePass(ID), PIL(_PIL) {}
Daniel Dunbar314fa8e2009-07-14 07:41:11 +0000136
137 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
138 AU.setPreservesAll();
139 AU.addRequired<ProfileInfo>();
140 }
141
142 bool runOnModule(Module &M);
143 };
144}
145
146char ProfileInfoPrinterPass::ID = 0;
147
148bool ProfileInfoPrinterPass::runOnModule(Module &M) {
Daniel Dunbaree166382009-08-05 15:55:56 +0000149 ProfileInfo &PI = getAnalysis<ProfileInfo>();
Daniel Dunbar314fa8e2009-07-14 07:41:11 +0000150 std::map<const Function *, unsigned> FuncFreqs;
151 std::map<const BasicBlock*, unsigned> BlockFreqs;
Daniel Dunbaree166382009-08-05 15:55:56 +0000152 std::map<ProfileInfo::Edge, unsigned> EdgeFreqs;
Daniel Dunbar314fa8e2009-07-14 07:41:11 +0000153
154 // Output a report. Eventually, there will be multiple reports selectable on
155 // the command line, for now, just keep things simple.
156
157 // Emit the most frequent function table...
Daniel Dunbar3cdfb4a2009-08-13 01:55:43 +0000158 std::vector<std::pair<Function*, double> > FunctionCounts;
159 std::vector<std::pair<BasicBlock*, double> > Counts;
Daniel Dunbaree166382009-08-05 15:55:56 +0000160 for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI) {
Daniel Dunbarc9008c52009-08-05 21:51:16 +0000161 if (FI->isDeclaration()) continue;
Daniel Dunbarcaaa4932009-08-08 17:43:09 +0000162 double w = ignoreMissing(PI.getExecutionCount(FI));
Daniel Dunbarc43782c2009-08-08 18:59:03 +0000163 FunctionCounts.push_back(std::make_pair(FI, w));
Daniel Dunbaree166382009-08-05 15:55:56 +0000164 for (Function::iterator BB = FI->begin(), BBE = FI->end();
165 BB != BBE; ++BB) {
Daniel Dunbarcaaa4932009-08-08 17:43:09 +0000166 double w = ignoreMissing(PI.getExecutionCount(BB));
Daniel Dunbarc43782c2009-08-08 18:59:03 +0000167 Counts.push_back(std::make_pair(BB, w));
Daniel Dunbaree166382009-08-05 15:55:56 +0000168 }
169 }
Daniel Dunbar314fa8e2009-07-14 07:41:11 +0000170
171 // Sort by the frequency, backwards.
172 sort(FunctionCounts.begin(), FunctionCounts.end(),
173 PairSecondSortReverse<Function*>());
174
Daniel Dunbar3cdfb4a2009-08-13 01:55:43 +0000175 double TotalExecutions = 0;
Daniel Dunbar314fa8e2009-07-14 07:41:11 +0000176 for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i)
177 TotalExecutions += FunctionCounts[i].second;
178
Andreas Neustifter30457f22009-08-26 09:05:21 +0000179 outs() << "===" << std::string(73, '-') << "===\n"
180 << "LLVM profiling output for execution";
181 if (PIL.getNumExecutions() != 1) outs() << "s";
182 outs() << ":\n";
Daniel Dunbar314fa8e2009-07-14 07:41:11 +0000183
184 for (unsigned i = 0, e = PIL.getNumExecutions(); i != e; ++i) {
Andreas Neustifter30457f22009-08-26 09:05:21 +0000185 outs() << " ";
186 if (e != 1) outs() << i+1 << ". ";
187 outs() << PIL.getExecution(i) << "\n";
Daniel Dunbar314fa8e2009-07-14 07:41:11 +0000188 }
189
Andreas Neustifter30457f22009-08-26 09:05:21 +0000190 outs() << "\n===" << std::string(73, '-') << "===\n";
191 outs() << "Function execution frequencies:\n\n";
Daniel Dunbar314fa8e2009-07-14 07:41:11 +0000192
193 // Print out the function frequencies...
Andreas Neustifter30457f22009-08-26 09:05:21 +0000194 outs() << " ## Frequency\n";
Daniel Dunbar314fa8e2009-07-14 07:41:11 +0000195 for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i) {
196 if (FunctionCounts[i].second == 0) {
Andreas Neustifter30457f22009-08-26 09:05:21 +0000197 outs() << "\n NOTE: " << e-i << " function"
198 << (e-i-1 ? "s were" : " was") << " never executed!\n";
Daniel Dunbar314fa8e2009-07-14 07:41:11 +0000199 break;
200 }
201
Andreas Neustifter30457f22009-08-26 09:05:21 +0000202 outs() << format("%3d", i+1) << ". "
Benjamin Kramera7b0cb72011-11-15 16:27:03 +0000203 << format("%5.2g", FunctionCounts[i].second) << "/"
204 << format("%g", TotalExecutions) << " "
205 << FunctionCounts[i].first->getName() << "\n";
Daniel Dunbar314fa8e2009-07-14 07:41:11 +0000206 }
207
208 std::set<Function*> FunctionsToPrint;
209
Daniel Dunbaree166382009-08-05 15:55:56 +0000210 TotalExecutions = 0;
211 for (unsigned i = 0, e = Counts.size(); i != e; ++i)
212 TotalExecutions += Counts[i].second;
213
214 // Sort by the frequency, backwards.
215 sort(Counts.begin(), Counts.end(),
216 PairSecondSortReverse<BasicBlock*>());
217
Andreas Neustifter30457f22009-08-26 09:05:21 +0000218 outs() << "\n===" << std::string(73, '-') << "===\n";
219 outs() << "Top 20 most frequently executed basic blocks:\n\n";
Daniel Dunbaree166382009-08-05 15:55:56 +0000220
221 // Print out the function frequencies...
Andreas Neustifter30457f22009-08-26 09:05:21 +0000222 outs() <<" ## %% \tFrequency\n";
Daniel Dunbaree166382009-08-05 15:55:56 +0000223 unsigned BlocksToPrint = Counts.size();
224 if (BlocksToPrint > 20) BlocksToPrint = 20;
225 for (unsigned i = 0; i != BlocksToPrint; ++i) {
226 if (Counts[i].second == 0) break;
227 Function *F = Counts[i].first->getParent();
Benjamin Kramera7b0cb72011-11-15 16:27:03 +0000228 outs() << format("%3d", i+1) << ". "
229 << format("%5g", Counts[i].second/(double)TotalExecutions*100)<<"% "
230 << format("%5.0f", Counts[i].second) << "/"
231 << format("%g", TotalExecutions) << "\t"
232 << F->getName() << "() - "
233 << Counts[i].first->getName() << "\n";
Daniel Dunbaree166382009-08-05 15:55:56 +0000234 FunctionsToPrint.insert(F);
Daniel Dunbar314fa8e2009-07-14 07:41:11 +0000235 }
236
237 if (PrintAnnotatedLLVM || PrintAllCode) {
Andreas Neustifter30457f22009-08-26 09:05:21 +0000238 outs() << "\n===" << std::string(73, '-') << "===\n";
239 outs() << "Annotated LLVM code for the module:\n\n";
Daniel Dunbaree166382009-08-05 15:55:56 +0000240
241 ProfileAnnotator PA(PI);
Daniel Dunbar314fa8e2009-07-14 07:41:11 +0000242
243 if (FunctionsToPrint.empty() || PrintAllCode)
Chris Lattner79c5d3f2009-08-23 04:52:46 +0000244 M.print(outs(), &PA);
Daniel Dunbar314fa8e2009-07-14 07:41:11 +0000245 else
246 // Print just a subset of the functions.
247 for (std::set<Function*>::iterator I = FunctionsToPrint.begin(),
248 E = FunctionsToPrint.end(); I != E; ++I)
Chris Lattnerbdff5482009-08-23 04:37:46 +0000249 (*I)->print(outs(), &PA);
Daniel Dunbar314fa8e2009-07-14 07:41:11 +0000250 }
251
252 return false;
253}
Chris Lattner7a78d812003-10-28 21:08:18 +0000254
Chris Lattner6f82d072003-10-28 19:16:35 +0000255int main(int argc, char **argv) {
Chris Lattnercc14d252009-03-06 05:34:10 +0000256 // Print a stack trace if we signal out.
257 sys::PrintStackTraceOnErrorSignal();
258 PrettyStackTraceProgram X(argc, argv);
Owen Anderson8b477ed2009-07-01 16:58:40 +0000259
Owen Anderson0d7c6952009-07-15 22:16:10 +0000260 LLVMContext &Context = getGlobalContext();
Chris Lattnercc14d252009-03-06 05:34:10 +0000261 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Daniel Dunbar314fa8e2009-07-14 07:41:11 +0000262
Chris Lattner42a88322009-10-22 00:50:24 +0000263 cl::ParseCommandLineOptions(argc, argv, "llvm profile dump decoder\n");
264
265 // Read in the bitcode file...
266 std::string ErrorMessage;
Michael J. Spencer3ff95632010-12-16 03:29:14 +0000267 OwningPtr<MemoryBuffer> Buffer;
Michael J. Spencer333fb042010-12-09 17:36:48 +0000268 error_code ec;
Chris Lattner42a88322009-10-22 00:50:24 +0000269 Module *M = 0;
Michael J. Spencer3ff95632010-12-16 03:29:14 +0000270 if (!(ec = MemoryBuffer::getFileOrSTDIN(BitcodeFile, Buffer))) {
271 M = ParseBitcodeFile(Buffer.get(), Context, &ErrorMessage);
Michael J. Spencer333fb042010-12-09 17:36:48 +0000272 } else
273 ErrorMessage = ec.message();
Chris Lattner42a88322009-10-22 00:50:24 +0000274 if (M == 0) {
275 errs() << argv[0] << ": " << BitcodeFile << ": "
276 << ErrorMessage << "\n";
277 return 1;
278 }
279
280 // Read the profiling information. This is redundant since we load it again
281 // using the standard profile info provider pass, but for now this gives us
282 // access to additional information not exposed via the ProfileInfo
283 // interface.
284 ProfileInfoLoader PIL(argv[0], ProfileDataFile, *M);
285
286 // Run the printer pass.
287 PassManager PassMgr;
288 PassMgr.add(createProfileLoaderPass(ProfileDataFile));
289 PassMgr.add(new ProfileInfoPrinterPass(PIL));
290 PassMgr.run(*M);
291
292 return 0;
Chris Lattner6f82d072003-10-28 19:16:35 +0000293}