blob: 81498752186d071572ea50cf4caf41056dbf82c5 [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"
Chris Lattner5e717642003-10-30 23:42:09 +000017#include "llvm/Module.h"
18#include "llvm/Assembly/AsmAnnotationWriter.h"
Chris Lattner89cf3932004-02-11 05:56:07 +000019#include "llvm/Analysis/ProfileInfoLoader.h"
Chris Lattner592488a2007-05-06 04:43:00 +000020#include "llvm/Bitcode/ReaderWriter.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000021#include "llvm/Support/CommandLine.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000022#include "llvm/Support/ManagedStatic.h"
Chris Lattner592488a2007-05-06 04:43:00 +000023#include "llvm/Support/MemoryBuffer.h"
Chris Lattner944fac72008-08-23 22:23:09 +000024#include "llvm/Support/raw_ostream.h"
Chris Lattnerbed85ff2004-05-27 05:41:36 +000025#include "llvm/System/Signals.h"
Jeff Cohenca5183d2007-03-05 00:00:42 +000026#include <algorithm>
Reid Spencer86f42bd2004-07-04 12:20:55 +000027#include <iostream>
Reid Spencer78b0e6a2005-12-29 21:13:45 +000028#include <iomanip>
Chris Lattner33f1ca72003-10-28 21:25:23 +000029#include <map>
Chris Lattner5e717642003-10-30 23:42:09 +000030#include <set>
Chris Lattner6f82d072003-10-28 19:16:35 +000031
Brian Gaeked0fde302003-11-11 22:41:34 +000032using namespace llvm;
33
Chris Lattner6f82d072003-10-28 19:16:35 +000034namespace {
Reid Spencer1adc3de2005-12-30 09:07:29 +000035 cl::opt<std::string>
Gabor Greifa99be512007-07-05 17:07:56 +000036 BitcodeFile(cl::Positional, cl::desc("<program bitcode file>"),
37 cl::Required);
Chris Lattner6f82d072003-10-28 19:16:35 +000038
Reid Spencer1adc3de2005-12-30 09:07:29 +000039 cl::opt<std::string>
Chris Lattner6f82d072003-10-28 19:16:35 +000040 ProfileDataFile(cl::Positional, cl::desc("<llvmprof.out file>"),
41 cl::Optional, cl::init("llvmprof.out"));
Chris Lattner5e717642003-10-30 23:42:09 +000042
43 cl::opt<bool>
44 PrintAnnotatedLLVM("annotated-llvm",
45 cl::desc("Print LLVM code with frequency annotations"));
46 cl::alias PrintAnnotated2("A", cl::desc("Alias for --annotated-llvm"),
47 cl::aliasopt(PrintAnnotatedLLVM));
Chris Lattnercde1cf32003-11-06 20:29:25 +000048 cl::opt<bool>
49 PrintAllCode("print-all-code",
50 cl::desc("Print annotated code for the entire program"));
Chris Lattner6f82d072003-10-28 19:16:35 +000051}
52
Chris Lattner7a78d812003-10-28 21:08:18 +000053// PairSecondSort - A sorting predicate to sort by the second element of a pair.
54template<class T>
Chris Lattner18884a82003-10-29 21:41:17 +000055struct PairSecondSortReverse
Reid Spencer1adc3de2005-12-30 09:07:29 +000056 : public std::binary_function<std::pair<T, unsigned>,
57 std::pair<T, unsigned>, bool> {
58 bool operator()(const std::pair<T, unsigned> &LHS,
59 const std::pair<T, unsigned> &RHS) const {
Chris Lattner18884a82003-10-29 21:41:17 +000060 return LHS.second > RHS.second;
Chris Lattner7a78d812003-10-28 21:08:18 +000061 }
62};
63
Chris Lattner5e717642003-10-30 23:42:09 +000064namespace {
65 class ProfileAnnotator : public AssemblyAnnotationWriter {
Reid Spencer1adc3de2005-12-30 09:07:29 +000066 std::map<const Function *, unsigned> &FuncFreqs;
67 std::map<const BasicBlock*, unsigned> &BlockFreqs;
68 std::map<ProfileInfoLoader::Edge, unsigned> &EdgeFreqs;
Chris Lattner5e717642003-10-30 23:42:09 +000069 public:
Reid Spencer1adc3de2005-12-30 09:07:29 +000070 ProfileAnnotator(std::map<const Function *, unsigned> &FF,
71 std::map<const BasicBlock*, unsigned> &BF,
72 std::map<ProfileInfoLoader::Edge, unsigned> &EF)
Chris Lattner8c2730e2004-03-08 20:04:32 +000073 : FuncFreqs(FF), BlockFreqs(BF), EdgeFreqs(EF) {}
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) {
Chris Lattner5e717642003-10-30 23:42:09 +000076 OS << ";;; %" << F->getName() << " called " << FuncFreqs[F]
77 << " 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) {
Chris Lattner36737302003-10-30 23:44:28 +000081 if (BlockFreqs.empty()) return;
Chris Lattner5e717642003-10-30 23:42:09 +000082 if (unsigned Count = BlockFreqs[BB])
Chris Lattner8c2730e2004-03-08 20:04:32 +000083 OS << "\t;;; Basic block executed " << Count << " 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 if (EdgeFreqs.empty()) return;
90
91 // Figure out how many times each successor executed.
Reid Spencer1adc3de2005-12-30 09:07:29 +000092 std::vector<std::pair<const BasicBlock*, unsigned> > SuccCounts;
Chris Lattner8c2730e2004-03-08 20:04:32 +000093 const TerminatorInst *TI = BB->getTerminator();
Misha Brukman3da94ae2005-04-22 00:00:37 +000094
Reid Spencer1adc3de2005-12-30 09:07:29 +000095 std::map<ProfileInfoLoader::Edge, unsigned>::iterator I =
96 EdgeFreqs.lower_bound(std::make_pair(const_cast<BasicBlock*>(BB), 0U));
Chris Lattner8c2730e2004-03-08 20:04:32 +000097 for (; I != EdgeFreqs.end() && I->first.first == BB; ++I)
98 if (I->second)
Reid Spencer1adc3de2005-12-30 09:07:29 +000099 SuccCounts.push_back(std::make_pair(TI->getSuccessor(I->first.second),
Chris Lattner8c2730e2004-03-08 20:04:32 +0000100 I->second));
101 if (!SuccCounts.empty()) {
102 OS << "\t;;; Out-edge counts:";
103 for (unsigned i = 0, e = SuccCounts.size(); i != e; ++i)
104 OS << " [" << SuccCounts[i].second << " -> "
105 << SuccCounts[i].first->getName() << "]";
106 OS << "\n";
107 }
Chris Lattner5e717642003-10-30 23:42:09 +0000108 }
109 };
110}
111
Chris Lattner7a78d812003-10-28 21:08:18 +0000112
Chris Lattner6f82d072003-10-28 19:16:35 +0000113int main(int argc, char **argv) {
Chris Lattnerc30598b2006-12-06 01:18:01 +0000114 llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000115 try {
Dan Gohman82a13c92007-10-08 15:45:12 +0000116 cl::ParseCommandLineOptions(argc, argv, "llvm profile dump decoder\n");
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000117 sys::PrintStackTraceOnErrorSignal();
Chris Lattner6f82d072003-10-28 19:16:35 +0000118
Gabor Greifa99be512007-07-05 17:07:56 +0000119 // Read in the bitcode file...
Reid Spencer1adc3de2005-12-30 09:07:29 +0000120 std::string ErrorMessage;
Reid Spencer295b1ce2007-05-07 18:50:07 +0000121 Module *M = 0;
Gabor Greifa99be512007-07-05 17:07:56 +0000122 if (MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(BitcodeFile,
Chris Lattner065344d2007-05-06 23:45:49 +0000123 &ErrorMessage)) {
Chris Lattner44dadff2007-05-06 09:29:57 +0000124 M = ParseBitcodeFile(Buffer, &ErrorMessage);
Chris Lattner065344d2007-05-06 23:45:49 +0000125 delete Buffer;
126 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000127 if (M == 0) {
Gabor Greifa99be512007-07-05 17:07:56 +0000128 std::cerr << argv[0] << ": " << BitcodeFile << ": "
Reid Spencer1adc3de2005-12-30 09:07:29 +0000129 << ErrorMessage << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000130 return 1;
Chris Lattner4963dcf2003-10-28 22:30:37 +0000131 }
132
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000133 // Read the profiling information
134 ProfileInfoLoader PI(argv[0], ProfileDataFile, *M);
Chris Lattner33f1ca72003-10-28 21:25:23 +0000135
Reid Spencer1adc3de2005-12-30 09:07:29 +0000136 std::map<const Function *, unsigned> FuncFreqs;
137 std::map<const BasicBlock*, unsigned> BlockFreqs;
138 std::map<ProfileInfoLoader::Edge, unsigned> EdgeFreqs;
Chris Lattner33f1ca72003-10-28 21:25:23 +0000139
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000140 // Output a report. Eventually, there will be multiple reports selectable on
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000141 // the command line, for now, just keep things simple.
Chris Lattner18884a82003-10-29 21:41:17 +0000142
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000143 // Emit the most frequent function table...
Reid Spencer1adc3de2005-12-30 09:07:29 +0000144 std::vector<std::pair<Function*, unsigned> > FunctionCounts;
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000145 PI.getFunctionCounts(FunctionCounts);
146 FuncFreqs.insert(FunctionCounts.begin(), FunctionCounts.end());
Chris Lattner18884a82003-10-29 21:41:17 +0000147
148 // Sort by the frequency, backwards.
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000149 sort(FunctionCounts.begin(), FunctionCounts.end(),
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000150 PairSecondSortReverse<Function*>());
151
Reid Spencer19b7e0e2006-05-24 19:21:13 +0000152 uint64_t TotalExecutions = 0;
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000153 for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i)
154 TotalExecutions += FunctionCounts[i].second;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000155
Reid Spencer1adc3de2005-12-30 09:07:29 +0000156 std::cout << "===" << std::string(73, '-') << "===\n"
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000157 << "LLVM profiling output for execution";
Reid Spencer1adc3de2005-12-30 09:07:29 +0000158 if (PI.getNumExecutions() != 1) std::cout << "s";
159 std::cout << ":\n";
Misha Brukman3da94ae2005-04-22 00:00:37 +0000160
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000161 for (unsigned i = 0, e = PI.getNumExecutions(); i != e; ++i) {
Reid Spencer1adc3de2005-12-30 09:07:29 +0000162 std::cout << " ";
163 if (e != 1) std::cout << i+1 << ". ";
164 std::cout << PI.getExecution(i) << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000165 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000166
Reid Spencer1adc3de2005-12-30 09:07:29 +0000167 std::cout << "\n===" << std::string(73, '-') << "===\n";
168 std::cout << "Function execution frequencies:\n\n";
Chris Lattner18884a82003-10-29 21:41:17 +0000169
170 // Print out the function frequencies...
Reid Spencer1adc3de2005-12-30 09:07:29 +0000171 std::cout << " ## Frequency\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000172 for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i) {
173 if (FunctionCounts[i].second == 0) {
Reid Spencer1adc3de2005-12-30 09:07:29 +0000174 std::cout << "\n NOTE: " << e-i << " function" <<
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000175 (e-i-1 ? "s were" : " was") << " never executed!\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000176 break;
177 }
178
Reid Spencer1adc3de2005-12-30 09:07:29 +0000179 std::cout << std::setw(3) << i+1 << ". "
180 << std::setw(5) << FunctionCounts[i].second << "/"
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000181 << TotalExecutions << " "
182 << FunctionCounts[i].first->getName().c_str() << "\n";
Chris Lattner5e717642003-10-30 23:42:09 +0000183 }
Chris Lattner18884a82003-10-29 21:41:17 +0000184
Reid Spencer1adc3de2005-12-30 09:07:29 +0000185 std::set<Function*> FunctionsToPrint;
Chris Lattner8c2730e2004-03-08 20:04:32 +0000186
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000187 // If we have block count information, print out the LLVM module with
188 // frequency annotations.
189 if (PI.hasAccurateBlockCounts()) {
Reid Spencer1adc3de2005-12-30 09:07:29 +0000190 std::vector<std::pair<BasicBlock*, unsigned> > Counts;
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000191 PI.getBlockCounts(Counts);
192
193 TotalExecutions = 0;
194 for (unsigned i = 0, e = Counts.size(); i != e; ++i)
195 TotalExecutions += Counts[i].second;
196
197 // Sort by the frequency, backwards.
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000198 sort(Counts.begin(), Counts.end(),
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000199 PairSecondSortReverse<BasicBlock*>());
Misha Brukman3da94ae2005-04-22 00:00:37 +0000200
Reid Spencer1adc3de2005-12-30 09:07:29 +0000201 std::cout << "\n===" << std::string(73, '-') << "===\n";
202 std::cout << "Top 20 most frequently executed basic blocks:\n\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000203
204 // Print out the function frequencies...
Reid Spencer1adc3de2005-12-30 09:07:29 +0000205 std::cout <<" ## %% \tFrequency\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000206 unsigned BlocksToPrint = Counts.size();
207 if (BlocksToPrint > 20) BlocksToPrint = 20;
208 for (unsigned i = 0; i != BlocksToPrint; ++i) {
209 if (Counts[i].second == 0) break;
210 Function *F = Counts[i].first->getParent();
Reid Spencer1adc3de2005-12-30 09:07:29 +0000211 std::cout << std::setw(3) << i+1 << ". "
212 << std::setw(5) << std::setprecision(2)
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000213 << Counts[i].second/(double)TotalExecutions*100 << "% "
Reid Spencer1adc3de2005-12-30 09:07:29 +0000214 << std::setw(5) << Counts[i].second << "/"
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000215 << TotalExecutions << "\t"
216 << F->getName().c_str() << "() - "
217 << Counts[i].first->getName().c_str() << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000218 FunctionsToPrint.insert(F);
219 }
220
221 BlockFreqs.insert(Counts.begin(), Counts.end());
222 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000223
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000224 if (PI.hasAccurateEdgeCounts()) {
Reid Spencer1adc3de2005-12-30 09:07:29 +0000225 std::vector<std::pair<ProfileInfoLoader::Edge, unsigned> > Counts;
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000226 PI.getEdgeCounts(Counts);
227 EdgeFreqs.insert(Counts.begin(), Counts.end());
228 }
Chris Lattner5e717642003-10-30 23:42:09 +0000229
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000230 if (PrintAnnotatedLLVM || PrintAllCode) {
Reid Spencer1adc3de2005-12-30 09:07:29 +0000231 std::cout << "\n===" << std::string(73, '-') << "===\n";
232 std::cout << "Annotated LLVM code for the module:\n\n";
Misha Brukman3da94ae2005-04-22 00:00:37 +0000233
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000234 ProfileAnnotator PA(FuncFreqs, BlockFreqs, EdgeFreqs);
235
236 if (FunctionsToPrint.empty() || PrintAllCode)
Reid Spencer1adc3de2005-12-30 09:07:29 +0000237 M->print(std::cout, &PA);
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000238 else
Chris Lattner944fac72008-08-23 22:23:09 +0000239 // Print just a subset of the functions.
Reid Spencer1adc3de2005-12-30 09:07:29 +0000240 for (std::set<Function*>::iterator I = FunctionsToPrint.begin(),
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000241 E = FunctionsToPrint.end(); I != E; ++I)
Reid Spencer1adc3de2005-12-30 09:07:29 +0000242 (*I)->print(std::cout, &PA);
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000243 }
244
245 return 0;
Reid Spencer1adc3de2005-12-30 09:07:29 +0000246 } catch (const std::string& msg) {
247 std::cerr << argv[0] << ": " << msg << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000248 } catch (...) {
Reid Spencer1adc3de2005-12-30 09:07:29 +0000249 std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
Chris Lattner33f1ca72003-10-28 21:25:23 +0000250 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000251 return 1;
Chris Lattner6f82d072003-10-28 19:16:35 +0000252}