blob: 104d879447e106d663395571b364c44e302b8d64 [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"
19#include "llvm/Assembly/AsmAnnotationWriter.h"
Chris Lattner89cf3932004-02-11 05:56:07 +000020#include "llvm/Analysis/ProfileInfoLoader.h"
Chris Lattner592488a2007-05-06 04:43:00 +000021#include "llvm/Bitcode/ReaderWriter.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000022#include "llvm/Support/CommandLine.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000023#include "llvm/Support/ManagedStatic.h"
Chris Lattner592488a2007-05-06 04:43:00 +000024#include "llvm/Support/MemoryBuffer.h"
Chris Lattnercc14d252009-03-06 05:34:10 +000025#include "llvm/Support/PrettyStackTrace.h"
Chris Lattner944fac72008-08-23 22:23:09 +000026#include "llvm/Support/raw_ostream.h"
Chris Lattnerbed85ff2004-05-27 05:41:36 +000027#include "llvm/System/Signals.h"
Jeff Cohenca5183d2007-03-05 00:00:42 +000028#include <algorithm>
Reid Spencer86f42bd2004-07-04 12:20:55 +000029#include <iostream>
Reid Spencer78b0e6a2005-12-29 21:13:45 +000030#include <iomanip>
Chris Lattner33f1ca72003-10-28 21:25:23 +000031#include <map>
Chris Lattner5e717642003-10-30 23:42:09 +000032#include <set>
Chris Lattner6f82d072003-10-28 19:16:35 +000033
Brian Gaeked0fde302003-11-11 22:41:34 +000034using namespace llvm;
35
Chris Lattner6f82d072003-10-28 19:16:35 +000036namespace {
Reid Spencer1adc3de2005-12-30 09:07:29 +000037 cl::opt<std::string>
Gabor Greifa99be512007-07-05 17:07:56 +000038 BitcodeFile(cl::Positional, cl::desc("<program bitcode file>"),
39 cl::Required);
Chris Lattner6f82d072003-10-28 19:16:35 +000040
Reid Spencer1adc3de2005-12-30 09:07:29 +000041 cl::opt<std::string>
Chris Lattner6f82d072003-10-28 19:16:35 +000042 ProfileDataFile(cl::Positional, cl::desc("<llvmprof.out file>"),
43 cl::Optional, cl::init("llvmprof.out"));
Chris Lattner5e717642003-10-30 23:42:09 +000044
45 cl::opt<bool>
46 PrintAnnotatedLLVM("annotated-llvm",
47 cl::desc("Print LLVM code with frequency annotations"));
48 cl::alias PrintAnnotated2("A", cl::desc("Alias for --annotated-llvm"),
49 cl::aliasopt(PrintAnnotatedLLVM));
Chris Lattnercde1cf32003-11-06 20:29:25 +000050 cl::opt<bool>
51 PrintAllCode("print-all-code",
52 cl::desc("Print annotated code for the entire program"));
Chris Lattner6f82d072003-10-28 19:16:35 +000053}
54
Chris Lattner7a78d812003-10-28 21:08:18 +000055// PairSecondSort - A sorting predicate to sort by the second element of a pair.
56template<class T>
Chris Lattner18884a82003-10-29 21:41:17 +000057struct PairSecondSortReverse
Reid Spencer1adc3de2005-12-30 09:07:29 +000058 : public std::binary_function<std::pair<T, unsigned>,
59 std::pair<T, unsigned>, bool> {
60 bool operator()(const std::pair<T, unsigned> &LHS,
61 const std::pair<T, unsigned> &RHS) const {
Chris Lattner18884a82003-10-29 21:41:17 +000062 return LHS.second > RHS.second;
Chris Lattner7a78d812003-10-28 21:08:18 +000063 }
64};
65
Chris Lattner5e717642003-10-30 23:42:09 +000066namespace {
67 class ProfileAnnotator : public AssemblyAnnotationWriter {
Reid Spencer1adc3de2005-12-30 09:07:29 +000068 std::map<const Function *, unsigned> &FuncFreqs;
69 std::map<const BasicBlock*, unsigned> &BlockFreqs;
70 std::map<ProfileInfoLoader::Edge, unsigned> &EdgeFreqs;
Chris Lattner5e717642003-10-30 23:42:09 +000071 public:
Reid Spencer1adc3de2005-12-30 09:07:29 +000072 ProfileAnnotator(std::map<const Function *, unsigned> &FF,
73 std::map<const BasicBlock*, unsigned> &BF,
74 std::map<ProfileInfoLoader::Edge, unsigned> &EF)
Chris Lattner8c2730e2004-03-08 20:04:32 +000075 : FuncFreqs(FF), BlockFreqs(BF), EdgeFreqs(EF) {}
Chris Lattner5e717642003-10-30 23:42:09 +000076
Chris Lattner944fac72008-08-23 22:23:09 +000077 virtual void emitFunctionAnnot(const Function *F, raw_ostream &OS) {
Chris Lattner5e717642003-10-30 23:42:09 +000078 OS << ";;; %" << F->getName() << " called " << FuncFreqs[F]
79 << " times.\n;;;\n";
80 }
Chris Lattner8c2730e2004-03-08 20:04:32 +000081 virtual void emitBasicBlockStartAnnot(const BasicBlock *BB,
Chris Lattner944fac72008-08-23 22:23:09 +000082 raw_ostream &OS) {
Chris Lattner36737302003-10-30 23:44:28 +000083 if (BlockFreqs.empty()) return;
Chris Lattner5e717642003-10-30 23:42:09 +000084 if (unsigned Count = BlockFreqs[BB])
Chris Lattner8c2730e2004-03-08 20:04:32 +000085 OS << "\t;;; Basic block executed " << Count << " times.\n";
Chris Lattner5e717642003-10-30 23:42:09 +000086 else
Chris Lattner8c2730e2004-03-08 20:04:32 +000087 OS << "\t;;; Never executed!\n";
88 }
89
Chris Lattner944fac72008-08-23 22:23:09 +000090 virtual void emitBasicBlockEndAnnot(const BasicBlock *BB, raw_ostream &OS) {
Chris Lattner8c2730e2004-03-08 20:04:32 +000091 if (EdgeFreqs.empty()) return;
92
93 // Figure out how many times each successor executed.
Reid Spencer1adc3de2005-12-30 09:07:29 +000094 std::vector<std::pair<const BasicBlock*, unsigned> > SuccCounts;
Chris Lattner8c2730e2004-03-08 20:04:32 +000095 const TerminatorInst *TI = BB->getTerminator();
Misha Brukman3da94ae2005-04-22 00:00:37 +000096
Reid Spencer1adc3de2005-12-30 09:07:29 +000097 std::map<ProfileInfoLoader::Edge, unsigned>::iterator I =
98 EdgeFreqs.lower_bound(std::make_pair(const_cast<BasicBlock*>(BB), 0U));
Chris Lattner8c2730e2004-03-08 20:04:32 +000099 for (; I != EdgeFreqs.end() && I->first.first == BB; ++I)
100 if (I->second)
Reid Spencer1adc3de2005-12-30 09:07:29 +0000101 SuccCounts.push_back(std::make_pair(TI->getSuccessor(I->first.second),
Chris Lattner8c2730e2004-03-08 20:04:32 +0000102 I->second));
103 if (!SuccCounts.empty()) {
104 OS << "\t;;; Out-edge counts:";
105 for (unsigned i = 0, e = SuccCounts.size(); i != e; ++i)
106 OS << " [" << SuccCounts[i].second << " -> "
107 << SuccCounts[i].first->getName() << "]";
108 OS << "\n";
109 }
Chris Lattner5e717642003-10-30 23:42:09 +0000110 }
111 };
112}
113
Chris Lattner7a78d812003-10-28 21:08:18 +0000114
Chris Lattner6f82d072003-10-28 19:16:35 +0000115int main(int argc, char **argv) {
Chris Lattnercc14d252009-03-06 05:34:10 +0000116 // Print a stack trace if we signal out.
117 sys::PrintStackTraceOnErrorSignal();
118 PrettyStackTraceProgram X(argc, argv);
Owen Anderson8b477ed2009-07-01 16:58:40 +0000119
120 LLVMContext Context;
Chris Lattnercc14d252009-03-06 05:34:10 +0000121 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000122 try {
Dan Gohman82a13c92007-10-08 15:45:12 +0000123 cl::ParseCommandLineOptions(argc, argv, "llvm profile dump decoder\n");
Chris Lattner6f82d072003-10-28 19:16:35 +0000124
Gabor Greifa99be512007-07-05 17:07:56 +0000125 // Read in the bitcode file...
Reid Spencer1adc3de2005-12-30 09:07:29 +0000126 std::string ErrorMessage;
Reid Spencer295b1ce2007-05-07 18:50:07 +0000127 Module *M = 0;
Gabor Greifa99be512007-07-05 17:07:56 +0000128 if (MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(BitcodeFile,
Chris Lattner065344d2007-05-06 23:45:49 +0000129 &ErrorMessage)) {
Owen Anderson8b477ed2009-07-01 16:58:40 +0000130 M = ParseBitcodeFile(Buffer, &Context, &ErrorMessage);
Chris Lattner065344d2007-05-06 23:45:49 +0000131 delete Buffer;
132 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000133 if (M == 0) {
Gabor Greifa99be512007-07-05 17:07:56 +0000134 std::cerr << argv[0] << ": " << BitcodeFile << ": "
Reid Spencer1adc3de2005-12-30 09:07:29 +0000135 << ErrorMessage << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000136 return 1;
Chris Lattner4963dcf2003-10-28 22:30:37 +0000137 }
138
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000139 // Read the profiling information
140 ProfileInfoLoader PI(argv[0], ProfileDataFile, *M);
Chris Lattner33f1ca72003-10-28 21:25:23 +0000141
Reid Spencer1adc3de2005-12-30 09:07:29 +0000142 std::map<const Function *, unsigned> FuncFreqs;
143 std::map<const BasicBlock*, unsigned> BlockFreqs;
144 std::map<ProfileInfoLoader::Edge, unsigned> EdgeFreqs;
Chris Lattner33f1ca72003-10-28 21:25:23 +0000145
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000146 // Output a report. Eventually, there will be multiple reports selectable on
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000147 // the command line, for now, just keep things simple.
Chris Lattner18884a82003-10-29 21:41:17 +0000148
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000149 // Emit the most frequent function table...
Reid Spencer1adc3de2005-12-30 09:07:29 +0000150 std::vector<std::pair<Function*, unsigned> > FunctionCounts;
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000151 PI.getFunctionCounts(FunctionCounts);
152 FuncFreqs.insert(FunctionCounts.begin(), FunctionCounts.end());
Chris Lattner18884a82003-10-29 21:41:17 +0000153
154 // Sort by the frequency, backwards.
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000155 sort(FunctionCounts.begin(), FunctionCounts.end(),
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000156 PairSecondSortReverse<Function*>());
157
Reid Spencer19b7e0e2006-05-24 19:21:13 +0000158 uint64_t TotalExecutions = 0;
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000159 for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i)
160 TotalExecutions += FunctionCounts[i].second;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000161
Reid Spencer1adc3de2005-12-30 09:07:29 +0000162 std::cout << "===" << std::string(73, '-') << "===\n"
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000163 << "LLVM profiling output for execution";
Reid Spencer1adc3de2005-12-30 09:07:29 +0000164 if (PI.getNumExecutions() != 1) std::cout << "s";
165 std::cout << ":\n";
Misha Brukman3da94ae2005-04-22 00:00:37 +0000166
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000167 for (unsigned i = 0, e = PI.getNumExecutions(); i != e; ++i) {
Reid Spencer1adc3de2005-12-30 09:07:29 +0000168 std::cout << " ";
169 if (e != 1) std::cout << i+1 << ". ";
170 std::cout << PI.getExecution(i) << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000171 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000172
Reid Spencer1adc3de2005-12-30 09:07:29 +0000173 std::cout << "\n===" << std::string(73, '-') << "===\n";
174 std::cout << "Function execution frequencies:\n\n";
Chris Lattner18884a82003-10-29 21:41:17 +0000175
176 // Print out the function frequencies...
Reid Spencer1adc3de2005-12-30 09:07:29 +0000177 std::cout << " ## Frequency\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000178 for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i) {
179 if (FunctionCounts[i].second == 0) {
Reid Spencer1adc3de2005-12-30 09:07:29 +0000180 std::cout << "\n NOTE: " << e-i << " function" <<
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000181 (e-i-1 ? "s were" : " was") << " never executed!\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000182 break;
183 }
184
Reid Spencer1adc3de2005-12-30 09:07:29 +0000185 std::cout << std::setw(3) << i+1 << ". "
186 << std::setw(5) << FunctionCounts[i].second << "/"
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000187 << TotalExecutions << " "
188 << FunctionCounts[i].first->getName().c_str() << "\n";
Chris Lattner5e717642003-10-30 23:42:09 +0000189 }
Chris Lattner18884a82003-10-29 21:41:17 +0000190
Reid Spencer1adc3de2005-12-30 09:07:29 +0000191 std::set<Function*> FunctionsToPrint;
Chris Lattner8c2730e2004-03-08 20:04:32 +0000192
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000193 // If we have block count information, print out the LLVM module with
194 // frequency annotations.
195 if (PI.hasAccurateBlockCounts()) {
Reid Spencer1adc3de2005-12-30 09:07:29 +0000196 std::vector<std::pair<BasicBlock*, unsigned> > Counts;
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000197 PI.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.
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000204 sort(Counts.begin(), Counts.end(),
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000205 PairSecondSortReverse<BasicBlock*>());
Misha Brukman3da94ae2005-04-22 00:00:37 +0000206
Reid Spencer1adc3de2005-12-30 09:07:29 +0000207 std::cout << "\n===" << std::string(73, '-') << "===\n";
208 std::cout << "Top 20 most frequently executed basic blocks:\n\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000209
210 // Print out the function frequencies...
Reid Spencer1adc3de2005-12-30 09:07:29 +0000211 std::cout <<" ## %% \tFrequency\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000212 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();
Reid Spencer1adc3de2005-12-30 09:07:29 +0000217 std::cout << std::setw(3) << i+1 << ". "
218 << std::setw(5) << std::setprecision(2)
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000219 << Counts[i].second/(double)TotalExecutions*100 << "% "
Reid Spencer1adc3de2005-12-30 09:07:29 +0000220 << std::setw(5) << Counts[i].second << "/"
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000221 << TotalExecutions << "\t"
222 << F->getName().c_str() << "() - "
223 << Counts[i].first->getName().c_str() << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000224 FunctionsToPrint.insert(F);
225 }
226
227 BlockFreqs.insert(Counts.begin(), Counts.end());
228 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000229
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000230 if (PI.hasAccurateEdgeCounts()) {
Reid Spencer1adc3de2005-12-30 09:07:29 +0000231 std::vector<std::pair<ProfileInfoLoader::Edge, unsigned> > Counts;
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000232 PI.getEdgeCounts(Counts);
233 EdgeFreqs.insert(Counts.begin(), Counts.end());
234 }
Chris Lattner5e717642003-10-30 23:42:09 +0000235
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000236 if (PrintAnnotatedLLVM || PrintAllCode) {
Reid Spencer1adc3de2005-12-30 09:07:29 +0000237 std::cout << "\n===" << std::string(73, '-') << "===\n";
238 std::cout << "Annotated LLVM code for the module:\n\n";
Misha Brukman3da94ae2005-04-22 00:00:37 +0000239
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000240 ProfileAnnotator PA(FuncFreqs, BlockFreqs, EdgeFreqs);
241
242 if (FunctionsToPrint.empty() || PrintAllCode)
Reid Spencer1adc3de2005-12-30 09:07:29 +0000243 M->print(std::cout, &PA);
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000244 else
Chris Lattner944fac72008-08-23 22:23:09 +0000245 // Print just a subset of the functions.
Reid Spencer1adc3de2005-12-30 09:07:29 +0000246 for (std::set<Function*>::iterator I = FunctionsToPrint.begin(),
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000247 E = FunctionsToPrint.end(); I != E; ++I)
Reid Spencer1adc3de2005-12-30 09:07:29 +0000248 (*I)->print(std::cout, &PA);
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000249 }
250
251 return 0;
Reid Spencer1adc3de2005-12-30 09:07:29 +0000252 } catch (const std::string& msg) {
253 std::cerr << argv[0] << ": " << msg << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000254 } catch (...) {
Reid Spencer1adc3de2005-12-30 09:07:29 +0000255 std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
Chris Lattner33f1ca72003-10-28 21:25:23 +0000256 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000257 return 1;
Chris Lattner6f82d072003-10-28 19:16:35 +0000258}