blob: d3971d30d1538398cca4c60e74f9b66cc11a072f [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 Lattner5e717642003-10-30 23:42:09 +000020#include "llvm/Assembly/AsmAnnotationWriter.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 Lattnerc30598b2006-12-06 01:18:01 +000026#include "llvm/Support/ManagedStatic.h"
Chris Lattner592488a2007-05-06 04:43:00 +000027#include "llvm/Support/MemoryBuffer.h"
Chris Lattnercc14d252009-03-06 05:34:10 +000028#include "llvm/Support/PrettyStackTrace.h"
Chris Lattner944fac72008-08-23 22:23:09 +000029#include "llvm/Support/raw_ostream.h"
Chris Lattnerbed85ff2004-05-27 05:41:36 +000030#include "llvm/System/Signals.h"
Jeff Cohenca5183d2007-03-05 00:00:42 +000031#include <algorithm>
Reid Spencer86f42bd2004-07-04 12:20:55 +000032#include <iostream>
Reid Spencer78b0e6a2005-12-29 21:13:45 +000033#include <iomanip>
Chris Lattner33f1ca72003-10-28 21:25:23 +000034#include <map>
Chris Lattner5e717642003-10-30 23:42:09 +000035#include <set>
Chris Lattner6f82d072003-10-28 19:16:35 +000036
Brian Gaeked0fde302003-11-11 22:41:34 +000037using namespace llvm;
38
Chris Lattner6f82d072003-10-28 19:16:35 +000039namespace {
Reid Spencer1adc3de2005-12-30 09:07:29 +000040 cl::opt<std::string>
Gabor Greifa99be512007-07-05 17:07:56 +000041 BitcodeFile(cl::Positional, cl::desc("<program bitcode file>"),
42 cl::Required);
Chris Lattner6f82d072003-10-28 19:16:35 +000043
Reid Spencer1adc3de2005-12-30 09:07:29 +000044 cl::opt<std::string>
Chris Lattner6f82d072003-10-28 19:16:35 +000045 ProfileDataFile(cl::Positional, cl::desc("<llvmprof.out file>"),
46 cl::Optional, cl::init("llvmprof.out"));
Chris Lattner5e717642003-10-30 23:42:09 +000047
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));
Chris Lattnercde1cf32003-11-06 20:29:25 +000053 cl::opt<bool>
54 PrintAllCode("print-all-code",
55 cl::desc("Print annotated code for the entire program"));
Chris Lattner6f82d072003-10-28 19:16:35 +000056}
57
Chris Lattner7a78d812003-10-28 21:08:18 +000058// PairSecondSort - A sorting predicate to sort by the second element of a pair.
59template<class T>
Chris Lattner18884a82003-10-29 21:41:17 +000060struct PairSecondSortReverse
Reid Spencer1adc3de2005-12-30 09:07:29 +000061 : public std::binary_function<std::pair<T, unsigned>,
62 std::pair<T, unsigned>, bool> {
63 bool operator()(const std::pair<T, unsigned> &LHS,
64 const std::pair<T, unsigned> &RHS) const {
Chris Lattner18884a82003-10-29 21:41:17 +000065 return LHS.second > RHS.second;
Chris Lattner7a78d812003-10-28 21:08:18 +000066 }
67};
68
Chris Lattner5e717642003-10-30 23:42:09 +000069namespace {
70 class ProfileAnnotator : public AssemblyAnnotationWriter {
Daniel Dunbaree166382009-08-05 15:55:56 +000071 ProfileInfo &PI;
Chris Lattner5e717642003-10-30 23:42:09 +000072 public:
Daniel Dunbaree166382009-08-05 15:55:56 +000073 ProfileAnnotator(ProfileInfo& pi) : PI(pi) {}
Chris Lattner5e717642003-10-30 23:42:09 +000074
Chris Lattner944fac72008-08-23 22:23:09 +000075 virtual void emitFunctionAnnot(const Function *F, raw_ostream &OS) {
Daniel Dunbaree166382009-08-05 15:55:56 +000076 OS << ";;; %" << F->getName() << " called " << PI.getExecutionCount(F)
Chris Lattner5e717642003-10-30 23:42:09 +000077 << " times.\n;;;\n";
78 }
Chris Lattner8c2730e2004-03-08 20:04:32 +000079 virtual void emitBasicBlockStartAnnot(const BasicBlock *BB,
Chris Lattner944fac72008-08-23 22:23:09 +000080 raw_ostream &OS) {
Daniel Dunbaree166382009-08-05 15:55:56 +000081 unsigned w = PI.getExecutionCount(BB);
82 if (w != 0)
83 OS << "\t;;; Basic block executed " << w << " times.\n";
Chris Lattner5e717642003-10-30 23:42:09 +000084 else
Chris Lattner8c2730e2004-03-08 20:04:32 +000085 OS << "\t;;; Never executed!\n";
86 }
87
Chris Lattner944fac72008-08-23 22:23:09 +000088 virtual void emitBasicBlockEndAnnot(const BasicBlock *BB, raw_ostream &OS) {
Chris Lattner8c2730e2004-03-08 20:04:32 +000089 // Figure out how many times each successor executed.
Daniel Dunbaree166382009-08-05 15:55:56 +000090 std::vector<std::pair<ProfileInfo::Edge, unsigned> > SuccCounts;
Misha Brukman3da94ae2005-04-22 00:00:37 +000091
Daniel Dunbaree166382009-08-05 15:55:56 +000092 const TerminatorInst *TI = BB->getTerminator();
93 for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) {
94 BasicBlock* Succ = TI->getSuccessor(s);
95 SuccCounts.push_back(std::make_pair(std::make_pair(BB,Succ),
96 PI.getEdgeWeight(BB,Succ)));
97 }
Chris Lattner8c2730e2004-03-08 20:04:32 +000098 if (!SuccCounts.empty()) {
99 OS << "\t;;; Out-edge counts:";
100 for (unsigned i = 0, e = SuccCounts.size(); i != e; ++i)
Daniel Dunbaree166382009-08-05 15:55:56 +0000101 OS << " [" << (SuccCounts[i]).second << " -> "
102 << (SuccCounts[i]).first.second->getName() << "]";
Chris Lattner8c2730e2004-03-08 20:04:32 +0000103 OS << "\n";
104 }
Chris Lattner5e717642003-10-30 23:42:09 +0000105 }
106 };
107}
108
Daniel Dunbar314fa8e2009-07-14 07:41:11 +0000109namespace {
110 /// ProfileInfoPrinterPass - Helper pass to dump the profile information for
111 /// a module.
112 //
113 // FIXME: This should move elsewhere.
114 class ProfileInfoPrinterPass : public ModulePass {
115 ProfileInfoLoader &PIL;
116 public:
117 static char ID; // Class identification, replacement for typeinfo.
118 explicit ProfileInfoPrinterPass(ProfileInfoLoader &_PIL)
119 : ModulePass(&ID), PIL(_PIL) {}
120
121 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
122 AU.setPreservesAll();
123 AU.addRequired<ProfileInfo>();
124 }
125
126 bool runOnModule(Module &M);
127 };
128}
129
130char ProfileInfoPrinterPass::ID = 0;
131
132bool ProfileInfoPrinterPass::runOnModule(Module &M) {
Daniel Dunbaree166382009-08-05 15:55:56 +0000133 ProfileInfo &PI = getAnalysis<ProfileInfo>();
Daniel Dunbar314fa8e2009-07-14 07:41:11 +0000134 std::map<const Function *, unsigned> FuncFreqs;
135 std::map<const BasicBlock*, unsigned> BlockFreqs;
Daniel Dunbaree166382009-08-05 15:55:56 +0000136 std::map<ProfileInfo::Edge, unsigned> EdgeFreqs;
Daniel Dunbar314fa8e2009-07-14 07:41:11 +0000137
138 // Output a report. Eventually, there will be multiple reports selectable on
139 // the command line, for now, just keep things simple.
140
141 // Emit the most frequent function table...
142 std::vector<std::pair<Function*, unsigned> > FunctionCounts;
Daniel Dunbaree166382009-08-05 15:55:56 +0000143 std::vector<std::pair<BasicBlock*, unsigned> > Counts;
144 for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI) {
Daniel Dunbarc9008c52009-08-05 21:51:16 +0000145 if (FI->isDeclaration()) continue;
146 FunctionCounts.push_back(std::make_pair(FI,PI.getExecutionCount(FI)));
Daniel Dunbaree166382009-08-05 15:55:56 +0000147 for (Function::iterator BB = FI->begin(), BBE = FI->end();
148 BB != BBE; ++BB) {
149 Counts.push_back(std::make_pair(BB,PI.getExecutionCount(BB)));
150 }
151 }
Daniel Dunbar314fa8e2009-07-14 07:41:11 +0000152
153 // Sort by the frequency, backwards.
154 sort(FunctionCounts.begin(), FunctionCounts.end(),
155 PairSecondSortReverse<Function*>());
156
157 uint64_t TotalExecutions = 0;
158 for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i)
159 TotalExecutions += FunctionCounts[i].second;
160
161 std::cout << "===" << std::string(73, '-') << "===\n"
162 << "LLVM profiling output for execution";
163 if (PIL.getNumExecutions() != 1) std::cout << "s";
164 std::cout << ":\n";
165
166 for (unsigned i = 0, e = PIL.getNumExecutions(); i != e; ++i) {
167 std::cout << " ";
168 if (e != 1) std::cout << i+1 << ". ";
169 std::cout << PIL.getExecution(i) << "\n";
170 }
171
172 std::cout << "\n===" << std::string(73, '-') << "===\n";
173 std::cout << "Function execution frequencies:\n\n";
174
175 // Print out the function frequencies...
176 std::cout << " ## Frequency\n";
177 for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i) {
178 if (FunctionCounts[i].second == 0) {
179 std::cout << "\n NOTE: " << e-i << " function" <<
180 (e-i-1 ? "s were" : " was") << " never executed!\n";
181 break;
182 }
183
184 std::cout << std::setw(3) << i+1 << ". "
185 << std::setw(5) << FunctionCounts[i].second << "/"
186 << TotalExecutions << " "
Daniel Dunbard5b385c2009-07-25 00:43:31 +0000187 << FunctionCounts[i].first->getNameStr() << "\n";
Daniel Dunbar314fa8e2009-07-14 07:41:11 +0000188 }
189
190 std::set<Function*> FunctionsToPrint;
191
Daniel Dunbaree166382009-08-05 15:55:56 +0000192 TotalExecutions = 0;
193 for (unsigned i = 0, e = Counts.size(); i != e; ++i)
194 TotalExecutions += Counts[i].second;
195
196 // Sort by the frequency, backwards.
197 sort(Counts.begin(), Counts.end(),
198 PairSecondSortReverse<BasicBlock*>());
199
200 std::cout << "\n===" << std::string(73, '-') << "===\n";
201 std::cout << "Top 20 most frequently executed basic blocks:\n\n";
202
203 // Print out the function frequencies...
204 std::cout <<" ## %% \tFrequency\n";
205 unsigned BlocksToPrint = Counts.size();
206 if (BlocksToPrint > 20) BlocksToPrint = 20;
207 for (unsigned i = 0; i != BlocksToPrint; ++i) {
208 if (Counts[i].second == 0) break;
209 Function *F = Counts[i].first->getParent();
210 std::cout << std::setw(3) << i+1 << ". "
Daniel Dunbarc9008c52009-08-05 21:51:16 +0000211 << std::setw(5) << std::setprecision(3)
Daniel Dunbaree166382009-08-05 15:55:56 +0000212 << Counts[i].second/(double)TotalExecutions*100 << "% "
213 << std::setw(5) << Counts[i].second << "/"
214 << TotalExecutions << "\t"
215 << F->getNameStr() << "() - "
216 << Counts[i].first->getNameStr() << "\n";
217 FunctionsToPrint.insert(F);
Daniel Dunbar314fa8e2009-07-14 07:41:11 +0000218 }
219
220 if (PrintAnnotatedLLVM || PrintAllCode) {
221 std::cout << "\n===" << std::string(73, '-') << "===\n";
222 std::cout << "Annotated LLVM code for the module:\n\n";
Daniel Dunbaree166382009-08-05 15:55:56 +0000223
224 ProfileAnnotator PA(PI);
Daniel Dunbar314fa8e2009-07-14 07:41:11 +0000225
226 if (FunctionsToPrint.empty() || PrintAllCode)
227 M.print(std::cout, &PA);
228 else
229 // Print just a subset of the functions.
230 for (std::set<Function*>::iterator I = FunctionsToPrint.begin(),
231 E = FunctionsToPrint.end(); I != E; ++I)
232 (*I)->print(std::cout, &PA);
233 }
234
235 return false;
236}
Chris Lattner7a78d812003-10-28 21:08:18 +0000237
Chris Lattner6f82d072003-10-28 19:16:35 +0000238int main(int argc, char **argv) {
Chris Lattnercc14d252009-03-06 05:34:10 +0000239 // Print a stack trace if we signal out.
240 sys::PrintStackTraceOnErrorSignal();
241 PrettyStackTraceProgram X(argc, argv);
Owen Anderson8b477ed2009-07-01 16:58:40 +0000242
Owen Anderson0d7c6952009-07-15 22:16:10 +0000243 LLVMContext &Context = getGlobalContext();
Chris Lattnercc14d252009-03-06 05:34:10 +0000244 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000245 try {
Dan Gohman82a13c92007-10-08 15:45:12 +0000246 cl::ParseCommandLineOptions(argc, argv, "llvm profile dump decoder\n");
Chris Lattner6f82d072003-10-28 19:16:35 +0000247
Gabor Greifa99be512007-07-05 17:07:56 +0000248 // Read in the bitcode file...
Reid Spencer1adc3de2005-12-30 09:07:29 +0000249 std::string ErrorMessage;
Reid Spencer295b1ce2007-05-07 18:50:07 +0000250 Module *M = 0;
Gabor Greifa99be512007-07-05 17:07:56 +0000251 if (MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(BitcodeFile,
Chris Lattner065344d2007-05-06 23:45:49 +0000252 &ErrorMessage)) {
Owen Anderson31895e72009-07-01 21:22:36 +0000253 M = ParseBitcodeFile(Buffer, Context, &ErrorMessage);
Chris Lattner065344d2007-05-06 23:45:49 +0000254 delete Buffer;
255 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000256 if (M == 0) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000257 errs() << argv[0] << ": " << BitcodeFile << ": "
Reid Spencer1adc3de2005-12-30 09:07:29 +0000258 << ErrorMessage << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000259 return 1;
Chris Lattner4963dcf2003-10-28 22:30:37 +0000260 }
261
Daniel Dunbar314fa8e2009-07-14 07:41:11 +0000262 // Read the profiling information. This is redundant since we load it again
263 // using the standard profile info provider pass, but for now this gives us
264 // access to additional information not exposed via the ProfileInfo
265 // interface.
266 ProfileInfoLoader PIL(argv[0], ProfileDataFile, *M);
Chris Lattner33f1ca72003-10-28 21:25:23 +0000267
Daniel Dunbar314fa8e2009-07-14 07:41:11 +0000268 // Run the printer pass.
269 PassManager PassMgr;
270 PassMgr.add(createProfileLoaderPass(ProfileDataFile));
271 PassMgr.add(new ProfileInfoPrinterPass(PIL));
272 PassMgr.run(*M);
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000273
274 return 0;
Reid Spencer1adc3de2005-12-30 09:07:29 +0000275 } catch (const std::string& msg) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000276 errs() << argv[0] << ": " << msg << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000277 } catch (...) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000278 errs() << argv[0] << ": Unexpected unknown exception occurred.\n";
Chris Lattner33f1ca72003-10-28 21:25:23 +0000279 }
Daniel Dunbar314fa8e2009-07-14 07:41:11 +0000280
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000281 return 1;
Chris Lattner6f82d072003-10-28 19:16:35 +0000282}