blob: 8dd26b03c91c8e759256340d9099bd11088fe453 [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"
Chris Lattner592488a2007-05-06 04:43:00 +000023#include "llvm/Bitcode/ReaderWriter.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000024#include "llvm/Support/CommandLine.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000025#include "llvm/Support/ManagedStatic.h"
Chris Lattner592488a2007-05-06 04:43:00 +000026#include "llvm/Support/MemoryBuffer.h"
Chris Lattnercc14d252009-03-06 05:34:10 +000027#include "llvm/Support/PrettyStackTrace.h"
Chris Lattner944fac72008-08-23 22:23:09 +000028#include "llvm/Support/raw_ostream.h"
Chris Lattnerbed85ff2004-05-27 05:41:36 +000029#include "llvm/System/Signals.h"
Jeff Cohenca5183d2007-03-05 00:00:42 +000030#include <algorithm>
Reid Spencer86f42bd2004-07-04 12:20:55 +000031#include <iostream>
Reid Spencer78b0e6a2005-12-29 21:13:45 +000032#include <iomanip>
Chris Lattner33f1ca72003-10-28 21:25:23 +000033#include <map>
Chris Lattner5e717642003-10-30 23:42:09 +000034#include <set>
Chris Lattner6f82d072003-10-28 19:16:35 +000035
Brian Gaeked0fde302003-11-11 22:41:34 +000036using namespace llvm;
37
Chris Lattner6f82d072003-10-28 19:16:35 +000038namespace {
Reid Spencer1adc3de2005-12-30 09:07:29 +000039 cl::opt<std::string>
Gabor Greifa99be512007-07-05 17:07:56 +000040 BitcodeFile(cl::Positional, cl::desc("<program bitcode file>"),
41 cl::Required);
Chris Lattner6f82d072003-10-28 19:16:35 +000042
Reid Spencer1adc3de2005-12-30 09:07:29 +000043 cl::opt<std::string>
Chris Lattner6f82d072003-10-28 19:16:35 +000044 ProfileDataFile(cl::Positional, cl::desc("<llvmprof.out file>"),
45 cl::Optional, cl::init("llvmprof.out"));
Chris Lattner5e717642003-10-30 23:42:09 +000046
47 cl::opt<bool>
48 PrintAnnotatedLLVM("annotated-llvm",
49 cl::desc("Print LLVM code with frequency annotations"));
50 cl::alias PrintAnnotated2("A", cl::desc("Alias for --annotated-llvm"),
51 cl::aliasopt(PrintAnnotatedLLVM));
Chris Lattnercde1cf32003-11-06 20:29:25 +000052 cl::opt<bool>
53 PrintAllCode("print-all-code",
54 cl::desc("Print annotated code for the entire program"));
Chris Lattner6f82d072003-10-28 19:16:35 +000055}
56
Chris Lattner7a78d812003-10-28 21:08:18 +000057// PairSecondSort - A sorting predicate to sort by the second element of a pair.
58template<class T>
Chris Lattner18884a82003-10-29 21:41:17 +000059struct PairSecondSortReverse
Reid Spencer1adc3de2005-12-30 09:07:29 +000060 : public std::binary_function<std::pair<T, unsigned>,
61 std::pair<T, unsigned>, bool> {
62 bool operator()(const std::pair<T, unsigned> &LHS,
63 const std::pair<T, unsigned> &RHS) const {
Chris Lattner18884a82003-10-29 21:41:17 +000064 return LHS.second > RHS.second;
Chris Lattner7a78d812003-10-28 21:08:18 +000065 }
66};
67
Chris Lattner5e717642003-10-30 23:42:09 +000068namespace {
69 class ProfileAnnotator : public AssemblyAnnotationWriter {
Reid Spencer1adc3de2005-12-30 09:07:29 +000070 std::map<const Function *, unsigned> &FuncFreqs;
71 std::map<const BasicBlock*, unsigned> &BlockFreqs;
72 std::map<ProfileInfoLoader::Edge, unsigned> &EdgeFreqs;
Chris Lattner5e717642003-10-30 23:42:09 +000073 public:
Reid Spencer1adc3de2005-12-30 09:07:29 +000074 ProfileAnnotator(std::map<const Function *, unsigned> &FF,
75 std::map<const BasicBlock*, unsigned> &BF,
76 std::map<ProfileInfoLoader::Edge, unsigned> &EF)
Chris Lattner8c2730e2004-03-08 20:04:32 +000077 : FuncFreqs(FF), BlockFreqs(BF), EdgeFreqs(EF) {}
Chris Lattner5e717642003-10-30 23:42:09 +000078
Chris Lattner944fac72008-08-23 22:23:09 +000079 virtual void emitFunctionAnnot(const Function *F, raw_ostream &OS) {
Chris Lattner5e717642003-10-30 23:42:09 +000080 OS << ";;; %" << F->getName() << " called " << FuncFreqs[F]
81 << " times.\n;;;\n";
82 }
Chris Lattner8c2730e2004-03-08 20:04:32 +000083 virtual void emitBasicBlockStartAnnot(const BasicBlock *BB,
Chris Lattner944fac72008-08-23 22:23:09 +000084 raw_ostream &OS) {
Chris Lattner36737302003-10-30 23:44:28 +000085 if (BlockFreqs.empty()) return;
Dan Gohmanf530c922009-07-02 00:17:47 +000086 std::map<const BasicBlock *, unsigned>::const_iterator I =
87 BlockFreqs.find(BB);
88 if (I != BlockFreqs.end())
89 OS << "\t;;; Basic block executed " << I->second << " times.\n";
Chris Lattner5e717642003-10-30 23:42:09 +000090 else
Chris Lattner8c2730e2004-03-08 20:04:32 +000091 OS << "\t;;; Never executed!\n";
92 }
93
Chris Lattner944fac72008-08-23 22:23:09 +000094 virtual void emitBasicBlockEndAnnot(const BasicBlock *BB, raw_ostream &OS) {
Chris Lattner8c2730e2004-03-08 20:04:32 +000095 if (EdgeFreqs.empty()) return;
96
97 // Figure out how many times each successor executed.
Reid Spencer1adc3de2005-12-30 09:07:29 +000098 std::vector<std::pair<const BasicBlock*, unsigned> > SuccCounts;
Chris Lattner8c2730e2004-03-08 20:04:32 +000099 const TerminatorInst *TI = BB->getTerminator();
Misha Brukman3da94ae2005-04-22 00:00:37 +0000100
Reid Spencer1adc3de2005-12-30 09:07:29 +0000101 std::map<ProfileInfoLoader::Edge, unsigned>::iterator I =
102 EdgeFreqs.lower_bound(std::make_pair(const_cast<BasicBlock*>(BB), 0U));
Chris Lattner8c2730e2004-03-08 20:04:32 +0000103 for (; I != EdgeFreqs.end() && I->first.first == BB; ++I)
104 if (I->second)
Reid Spencer1adc3de2005-12-30 09:07:29 +0000105 SuccCounts.push_back(std::make_pair(TI->getSuccessor(I->first.second),
Chris Lattner8c2730e2004-03-08 20:04:32 +0000106 I->second));
107 if (!SuccCounts.empty()) {
108 OS << "\t;;; Out-edge counts:";
109 for (unsigned i = 0, e = SuccCounts.size(); i != e; ++i)
110 OS << " [" << SuccCounts[i].second << " -> "
111 << SuccCounts[i].first->getName() << "]";
112 OS << "\n";
113 }
Chris Lattner5e717642003-10-30 23:42:09 +0000114 }
115 };
116}
117
Daniel Dunbar314fa8e2009-07-14 07:41:11 +0000118namespace {
119 /// ProfileInfoPrinterPass - Helper pass to dump the profile information for
120 /// a module.
121 //
122 // FIXME: This should move elsewhere.
123 class ProfileInfoPrinterPass : public ModulePass {
124 ProfileInfoLoader &PIL;
125 public:
126 static char ID; // Class identification, replacement for typeinfo.
127 explicit ProfileInfoPrinterPass(ProfileInfoLoader &_PIL)
128 : ModulePass(&ID), PIL(_PIL) {}
129
130 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
131 AU.setPreservesAll();
132 AU.addRequired<ProfileInfo>();
133 }
134
135 bool runOnModule(Module &M);
136 };
137}
138
139char ProfileInfoPrinterPass::ID = 0;
140
141bool ProfileInfoPrinterPass::runOnModule(Module &M) {
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 PIL.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 (PIL.getNumExecutions() != 1) std::cout << "s";
165 std::cout << ":\n";
166
167 for (unsigned i = 0, e = PIL.getNumExecutions(); i != e; ++i) {
168 std::cout << " ";
169 if (e != 1) std::cout << i+1 << ". ";
170 std::cout << PIL.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 << " "
Daniel Dunbard5b385c2009-07-25 00:43:31 +0000188 << FunctionCounts[i].first->getNameStr() << "\n";
Daniel Dunbar314fa8e2009-07-14 07:41:11 +0000189 }
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 (PIL.hasAccurateBlockCounts()) {
196 std::vector<std::pair<BasicBlock*, unsigned> > Counts;
197 PIL.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"
Daniel Dunbard5b385c2009-07-25 00:43:31 +0000222 << F->getNameStr() << "() - "
223 << Counts[i].first->getNameStr() << "\n";
Daniel Dunbar314fa8e2009-07-14 07:41:11 +0000224 FunctionsToPrint.insert(F);
225 }
226
227 BlockFreqs.insert(Counts.begin(), Counts.end());
228 }
229
230 if (PIL.hasAccurateEdgeCounts()) {
231 std::vector<std::pair<ProfileInfoLoader::Edge, unsigned> > Counts;
232 PIL.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
245 // Print just a subset of the functions.
246 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 false;
252}
Chris Lattner7a78d812003-10-28 21:08:18 +0000253
Chris Lattner6f82d072003-10-28 19:16:35 +0000254int main(int argc, char **argv) {
Chris Lattnercc14d252009-03-06 05:34:10 +0000255 // Print a stack trace if we signal out.
256 sys::PrintStackTraceOnErrorSignal();
257 PrettyStackTraceProgram X(argc, argv);
Owen Anderson8b477ed2009-07-01 16:58:40 +0000258
Owen Anderson0d7c6952009-07-15 22:16:10 +0000259 LLVMContext &Context = getGlobalContext();
Chris Lattnercc14d252009-03-06 05:34:10 +0000260 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000261 try {
Dan Gohman82a13c92007-10-08 15:45:12 +0000262 cl::ParseCommandLineOptions(argc, argv, "llvm profile dump decoder\n");
Chris Lattner6f82d072003-10-28 19:16:35 +0000263
Gabor Greifa99be512007-07-05 17:07:56 +0000264 // Read in the bitcode file...
Reid Spencer1adc3de2005-12-30 09:07:29 +0000265 std::string ErrorMessage;
Reid Spencer295b1ce2007-05-07 18:50:07 +0000266 Module *M = 0;
Gabor Greifa99be512007-07-05 17:07:56 +0000267 if (MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(BitcodeFile,
Chris Lattner065344d2007-05-06 23:45:49 +0000268 &ErrorMessage)) {
Owen Anderson31895e72009-07-01 21:22:36 +0000269 M = ParseBitcodeFile(Buffer, Context, &ErrorMessage);
Chris Lattner065344d2007-05-06 23:45:49 +0000270 delete Buffer;
271 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000272 if (M == 0) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000273 errs() << argv[0] << ": " << BitcodeFile << ": "
Reid Spencer1adc3de2005-12-30 09:07:29 +0000274 << ErrorMessage << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000275 return 1;
Chris Lattner4963dcf2003-10-28 22:30:37 +0000276 }
277
Daniel Dunbar314fa8e2009-07-14 07:41:11 +0000278 // Read the profiling information. This is redundant since we load it again
279 // using the standard profile info provider pass, but for now this gives us
280 // access to additional information not exposed via the ProfileInfo
281 // interface.
282 ProfileInfoLoader PIL(argv[0], ProfileDataFile, *M);
Chris Lattner33f1ca72003-10-28 21:25:23 +0000283
Daniel Dunbar314fa8e2009-07-14 07:41:11 +0000284 // Run the printer pass.
285 PassManager PassMgr;
286 PassMgr.add(createProfileLoaderPass(ProfileDataFile));
287 PassMgr.add(new ProfileInfoPrinterPass(PIL));
288 PassMgr.run(*M);
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000289
290 return 0;
Reid Spencer1adc3de2005-12-30 09:07:29 +0000291 } catch (const std::string& msg) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000292 errs() << argv[0] << ": " << msg << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000293 } catch (...) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000294 errs() << argv[0] << ": Unexpected unknown exception occurred.\n";
Chris Lattner33f1ca72003-10-28 21:25:23 +0000295 }
Daniel Dunbar314fa8e2009-07-14 07:41:11 +0000296
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000297 return 1;
Chris Lattner6f82d072003-10-28 19:16:35 +0000298}