blob: 119dc1a1178c5d893b31c2d71fa4bc677512d901 [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 Lattnercc14d252009-03-06 05:34:10 +000024#include "llvm/Support/PrettyStackTrace.h"
Chris Lattner944fac72008-08-23 22:23:09 +000025#include "llvm/Support/raw_ostream.h"
Chris Lattnerbed85ff2004-05-27 05:41:36 +000026#include "llvm/System/Signals.h"
Jeff Cohenca5183d2007-03-05 00:00:42 +000027#include <algorithm>
Reid Spencer86f42bd2004-07-04 12:20:55 +000028#include <iostream>
Reid Spencer78b0e6a2005-12-29 21:13:45 +000029#include <iomanip>
Chris Lattner33f1ca72003-10-28 21:25:23 +000030#include <map>
Chris Lattner5e717642003-10-30 23:42:09 +000031#include <set>
Chris Lattner6f82d072003-10-28 19:16:35 +000032
Brian Gaeked0fde302003-11-11 22:41:34 +000033using namespace llvm;
34
Chris Lattner6f82d072003-10-28 19:16:35 +000035namespace {
Reid Spencer1adc3de2005-12-30 09:07:29 +000036 cl::opt<std::string>
Gabor Greifa99be512007-07-05 17:07:56 +000037 BitcodeFile(cl::Positional, cl::desc("<program bitcode file>"),
38 cl::Required);
Chris Lattner6f82d072003-10-28 19:16:35 +000039
Reid Spencer1adc3de2005-12-30 09:07:29 +000040 cl::opt<std::string>
Chris Lattner6f82d072003-10-28 19:16:35 +000041 ProfileDataFile(cl::Positional, cl::desc("<llvmprof.out file>"),
42 cl::Optional, cl::init("llvmprof.out"));
Chris Lattner5e717642003-10-30 23:42:09 +000043
44 cl::opt<bool>
45 PrintAnnotatedLLVM("annotated-llvm",
46 cl::desc("Print LLVM code with frequency annotations"));
47 cl::alias PrintAnnotated2("A", cl::desc("Alias for --annotated-llvm"),
48 cl::aliasopt(PrintAnnotatedLLVM));
Chris Lattnercde1cf32003-11-06 20:29:25 +000049 cl::opt<bool>
50 PrintAllCode("print-all-code",
51 cl::desc("Print annotated code for the entire program"));
Chris Lattner6f82d072003-10-28 19:16:35 +000052}
53
Chris Lattner7a78d812003-10-28 21:08:18 +000054// PairSecondSort - A sorting predicate to sort by the second element of a pair.
55template<class T>
Chris Lattner18884a82003-10-29 21:41:17 +000056struct PairSecondSortReverse
Reid Spencer1adc3de2005-12-30 09:07:29 +000057 : public std::binary_function<std::pair<T, unsigned>,
58 std::pair<T, unsigned>, bool> {
59 bool operator()(const std::pair<T, unsigned> &LHS,
60 const std::pair<T, unsigned> &RHS) const {
Chris Lattner18884a82003-10-29 21:41:17 +000061 return LHS.second > RHS.second;
Chris Lattner7a78d812003-10-28 21:08:18 +000062 }
63};
64
Chris Lattner5e717642003-10-30 23:42:09 +000065namespace {
66 class ProfileAnnotator : public AssemblyAnnotationWriter {
Reid Spencer1adc3de2005-12-30 09:07:29 +000067 std::map<const Function *, unsigned> &FuncFreqs;
68 std::map<const BasicBlock*, unsigned> &BlockFreqs;
69 std::map<ProfileInfoLoader::Edge, unsigned> &EdgeFreqs;
Chris Lattner5e717642003-10-30 23:42:09 +000070 public:
Reid Spencer1adc3de2005-12-30 09:07:29 +000071 ProfileAnnotator(std::map<const Function *, unsigned> &FF,
72 std::map<const BasicBlock*, unsigned> &BF,
73 std::map<ProfileInfoLoader::Edge, unsigned> &EF)
Chris Lattner8c2730e2004-03-08 20:04:32 +000074 : FuncFreqs(FF), BlockFreqs(BF), EdgeFreqs(EF) {}
Chris Lattner5e717642003-10-30 23:42:09 +000075
Chris Lattner944fac72008-08-23 22:23:09 +000076 virtual void emitFunctionAnnot(const Function *F, raw_ostream &OS) {
Chris Lattner5e717642003-10-30 23:42:09 +000077 OS << ";;; %" << F->getName() << " called " << FuncFreqs[F]
78 << " times.\n;;;\n";
79 }
Chris Lattner8c2730e2004-03-08 20:04:32 +000080 virtual void emitBasicBlockStartAnnot(const BasicBlock *BB,
Chris Lattner944fac72008-08-23 22:23:09 +000081 raw_ostream &OS) {
Chris Lattner36737302003-10-30 23:44:28 +000082 if (BlockFreqs.empty()) return;
Chris Lattner5e717642003-10-30 23:42:09 +000083 if (unsigned Count = BlockFreqs[BB])
Chris Lattner8c2730e2004-03-08 20:04:32 +000084 OS << "\t;;; Basic block executed " << Count << " times.\n";
Chris Lattner5e717642003-10-30 23:42:09 +000085 else
Chris Lattner8c2730e2004-03-08 20:04:32 +000086 OS << "\t;;; Never executed!\n";
87 }
88
Chris Lattner944fac72008-08-23 22:23:09 +000089 virtual void emitBasicBlockEndAnnot(const BasicBlock *BB, raw_ostream &OS) {
Chris Lattner8c2730e2004-03-08 20:04:32 +000090 if (EdgeFreqs.empty()) return;
91
92 // Figure out how many times each successor executed.
Reid Spencer1adc3de2005-12-30 09:07:29 +000093 std::vector<std::pair<const BasicBlock*, unsigned> > SuccCounts;
Chris Lattner8c2730e2004-03-08 20:04:32 +000094 const TerminatorInst *TI = BB->getTerminator();
Misha Brukman3da94ae2005-04-22 00:00:37 +000095
Reid Spencer1adc3de2005-12-30 09:07:29 +000096 std::map<ProfileInfoLoader::Edge, unsigned>::iterator I =
97 EdgeFreqs.lower_bound(std::make_pair(const_cast<BasicBlock*>(BB), 0U));
Chris Lattner8c2730e2004-03-08 20:04:32 +000098 for (; I != EdgeFreqs.end() && I->first.first == BB; ++I)
99 if (I->second)
Reid Spencer1adc3de2005-12-30 09:07:29 +0000100 SuccCounts.push_back(std::make_pair(TI->getSuccessor(I->first.second),
Chris Lattner8c2730e2004-03-08 20:04:32 +0000101 I->second));
102 if (!SuccCounts.empty()) {
103 OS << "\t;;; Out-edge counts:";
104 for (unsigned i = 0, e = SuccCounts.size(); i != e; ++i)
105 OS << " [" << SuccCounts[i].second << " -> "
106 << SuccCounts[i].first->getName() << "]";
107 OS << "\n";
108 }
Chris Lattner5e717642003-10-30 23:42:09 +0000109 }
110 };
111}
112
Chris Lattner7a78d812003-10-28 21:08:18 +0000113
Chris Lattner6f82d072003-10-28 19:16:35 +0000114int main(int argc, char **argv) {
Chris Lattnercc14d252009-03-06 05:34:10 +0000115 // Print a stack trace if we signal out.
116 sys::PrintStackTraceOnErrorSignal();
117 PrettyStackTraceProgram X(argc, argv);
118
119 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000120 try {
Dan Gohman82a13c92007-10-08 15:45:12 +0000121 cl::ParseCommandLineOptions(argc, argv, "llvm profile dump decoder\n");
Chris Lattner6f82d072003-10-28 19:16:35 +0000122
Gabor Greifa99be512007-07-05 17:07:56 +0000123 // Read in the bitcode file...
Reid Spencer1adc3de2005-12-30 09:07:29 +0000124 std::string ErrorMessage;
Reid Spencer295b1ce2007-05-07 18:50:07 +0000125 Module *M = 0;
Gabor Greifa99be512007-07-05 17:07:56 +0000126 if (MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(BitcodeFile,
Chris Lattner065344d2007-05-06 23:45:49 +0000127 &ErrorMessage)) {
Chris Lattner44dadff2007-05-06 09:29:57 +0000128 M = ParseBitcodeFile(Buffer, &ErrorMessage);
Chris Lattner065344d2007-05-06 23:45:49 +0000129 delete Buffer;
130 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000131 if (M == 0) {
Gabor Greifa99be512007-07-05 17:07:56 +0000132 std::cerr << argv[0] << ": " << BitcodeFile << ": "
Reid Spencer1adc3de2005-12-30 09:07:29 +0000133 << ErrorMessage << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000134 return 1;
Chris Lattner4963dcf2003-10-28 22:30:37 +0000135 }
136
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000137 // Read the profiling information
138 ProfileInfoLoader PI(argv[0], ProfileDataFile, *M);
Chris Lattner33f1ca72003-10-28 21:25:23 +0000139
Reid Spencer1adc3de2005-12-30 09:07:29 +0000140 std::map<const Function *, unsigned> FuncFreqs;
141 std::map<const BasicBlock*, unsigned> BlockFreqs;
142 std::map<ProfileInfoLoader::Edge, unsigned> EdgeFreqs;
Chris Lattner33f1ca72003-10-28 21:25:23 +0000143
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000144 // Output a report. Eventually, there will be multiple reports selectable on
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000145 // the command line, for now, just keep things simple.
Chris Lattner18884a82003-10-29 21:41:17 +0000146
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000147 // Emit the most frequent function table...
Reid Spencer1adc3de2005-12-30 09:07:29 +0000148 std::vector<std::pair<Function*, unsigned> > FunctionCounts;
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000149 PI.getFunctionCounts(FunctionCounts);
150 FuncFreqs.insert(FunctionCounts.begin(), FunctionCounts.end());
Chris Lattner18884a82003-10-29 21:41:17 +0000151
152 // Sort by the frequency, backwards.
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000153 sort(FunctionCounts.begin(), FunctionCounts.end(),
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000154 PairSecondSortReverse<Function*>());
155
Reid Spencer19b7e0e2006-05-24 19:21:13 +0000156 uint64_t TotalExecutions = 0;
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000157 for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i)
158 TotalExecutions += FunctionCounts[i].second;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000159
Reid Spencer1adc3de2005-12-30 09:07:29 +0000160 std::cout << "===" << std::string(73, '-') << "===\n"
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000161 << "LLVM profiling output for execution";
Reid Spencer1adc3de2005-12-30 09:07:29 +0000162 if (PI.getNumExecutions() != 1) std::cout << "s";
163 std::cout << ":\n";
Misha Brukman3da94ae2005-04-22 00:00:37 +0000164
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000165 for (unsigned i = 0, e = PI.getNumExecutions(); i != e; ++i) {
Reid Spencer1adc3de2005-12-30 09:07:29 +0000166 std::cout << " ";
167 if (e != 1) std::cout << i+1 << ". ";
168 std::cout << PI.getExecution(i) << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000169 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000170
Reid Spencer1adc3de2005-12-30 09:07:29 +0000171 std::cout << "\n===" << std::string(73, '-') << "===\n";
172 std::cout << "Function execution frequencies:\n\n";
Chris Lattner18884a82003-10-29 21:41:17 +0000173
174 // Print out the function frequencies...
Reid Spencer1adc3de2005-12-30 09:07:29 +0000175 std::cout << " ## Frequency\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000176 for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i) {
177 if (FunctionCounts[i].second == 0) {
Reid Spencer1adc3de2005-12-30 09:07:29 +0000178 std::cout << "\n NOTE: " << e-i << " function" <<
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000179 (e-i-1 ? "s were" : " was") << " never executed!\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000180 break;
181 }
182
Reid Spencer1adc3de2005-12-30 09:07:29 +0000183 std::cout << std::setw(3) << i+1 << ". "
184 << std::setw(5) << FunctionCounts[i].second << "/"
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000185 << TotalExecutions << " "
186 << FunctionCounts[i].first->getName().c_str() << "\n";
Chris Lattner5e717642003-10-30 23:42:09 +0000187 }
Chris Lattner18884a82003-10-29 21:41:17 +0000188
Reid Spencer1adc3de2005-12-30 09:07:29 +0000189 std::set<Function*> FunctionsToPrint;
Chris Lattner8c2730e2004-03-08 20:04:32 +0000190
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000191 // If we have block count information, print out the LLVM module with
192 // frequency annotations.
193 if (PI.hasAccurateBlockCounts()) {
Reid Spencer1adc3de2005-12-30 09:07:29 +0000194 std::vector<std::pair<BasicBlock*, unsigned> > Counts;
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000195 PI.getBlockCounts(Counts);
196
197 TotalExecutions = 0;
198 for (unsigned i = 0, e = Counts.size(); i != e; ++i)
199 TotalExecutions += Counts[i].second;
200
201 // Sort by the frequency, backwards.
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000202 sort(Counts.begin(), Counts.end(),
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000203 PairSecondSortReverse<BasicBlock*>());
Misha Brukman3da94ae2005-04-22 00:00:37 +0000204
Reid Spencer1adc3de2005-12-30 09:07:29 +0000205 std::cout << "\n===" << std::string(73, '-') << "===\n";
206 std::cout << "Top 20 most frequently executed basic blocks:\n\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000207
208 // Print out the function frequencies...
Reid Spencer1adc3de2005-12-30 09:07:29 +0000209 std::cout <<" ## %% \tFrequency\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000210 unsigned BlocksToPrint = Counts.size();
211 if (BlocksToPrint > 20) BlocksToPrint = 20;
212 for (unsigned i = 0; i != BlocksToPrint; ++i) {
213 if (Counts[i].second == 0) break;
214 Function *F = Counts[i].first->getParent();
Reid Spencer1adc3de2005-12-30 09:07:29 +0000215 std::cout << std::setw(3) << i+1 << ". "
216 << std::setw(5) << std::setprecision(2)
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000217 << Counts[i].second/(double)TotalExecutions*100 << "% "
Reid Spencer1adc3de2005-12-30 09:07:29 +0000218 << std::setw(5) << Counts[i].second << "/"
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000219 << TotalExecutions << "\t"
220 << F->getName().c_str() << "() - "
221 << Counts[i].first->getName().c_str() << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000222 FunctionsToPrint.insert(F);
223 }
224
225 BlockFreqs.insert(Counts.begin(), Counts.end());
226 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000227
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000228 if (PI.hasAccurateEdgeCounts()) {
Reid Spencer1adc3de2005-12-30 09:07:29 +0000229 std::vector<std::pair<ProfileInfoLoader::Edge, unsigned> > Counts;
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000230 PI.getEdgeCounts(Counts);
231 EdgeFreqs.insert(Counts.begin(), Counts.end());
232 }
Chris Lattner5e717642003-10-30 23:42:09 +0000233
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000234 if (PrintAnnotatedLLVM || PrintAllCode) {
Reid Spencer1adc3de2005-12-30 09:07:29 +0000235 std::cout << "\n===" << std::string(73, '-') << "===\n";
236 std::cout << "Annotated LLVM code for the module:\n\n";
Misha Brukman3da94ae2005-04-22 00:00:37 +0000237
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000238 ProfileAnnotator PA(FuncFreqs, BlockFreqs, EdgeFreqs);
239
240 if (FunctionsToPrint.empty() || PrintAllCode)
Reid Spencer1adc3de2005-12-30 09:07:29 +0000241 M->print(std::cout, &PA);
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000242 else
Chris Lattner944fac72008-08-23 22:23:09 +0000243 // Print just a subset of the functions.
Reid Spencer1adc3de2005-12-30 09:07:29 +0000244 for (std::set<Function*>::iterator I = FunctionsToPrint.begin(),
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000245 E = FunctionsToPrint.end(); I != E; ++I)
Reid Spencer1adc3de2005-12-30 09:07:29 +0000246 (*I)->print(std::cout, &PA);
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000247 }
248
249 return 0;
Reid Spencer1adc3de2005-12-30 09:07:29 +0000250 } catch (const std::string& msg) {
251 std::cerr << argv[0] << ": " << msg << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000252 } catch (...) {
Reid Spencer1adc3de2005-12-30 09:07:29 +0000253 std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
Chris Lattner33f1ca72003-10-28 21:25:23 +0000254 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000255 return 1;
Chris Lattner6f82d072003-10-28 19:16:35 +0000256}