blob: 075466b198ea9685404775ca98400e6f53ff22e0 [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//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source 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 Lattner6f82d072003-10-28 19:16:35 +000020#include "llvm/Bytecode/Reader.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000021#include "llvm/Support/CommandLine.h"
Chris Lattnerbed85ff2004-05-27 05:41:36 +000022#include "llvm/System/Signals.h"
Reid Spencer86f42bd2004-07-04 12:20:55 +000023#include <iostream>
Reid Spencer78b0e6a2005-12-29 21:13:45 +000024#include <iomanip>
Chris Lattner33f1ca72003-10-28 21:25:23 +000025#include <map>
Chris Lattner5e717642003-10-30 23:42:09 +000026#include <set>
Chris Lattner6f82d072003-10-28 19:16:35 +000027
Brian Gaeked0fde302003-11-11 22:41:34 +000028using namespace llvm;
Reid Spencer78b0e6a2005-12-29 21:13:45 +000029using namespace std;
Brian Gaeked0fde302003-11-11 22:41:34 +000030
Chris Lattner6f82d072003-10-28 19:16:35 +000031namespace {
Reid Spencer78b0e6a2005-12-29 21:13:45 +000032 cl::opt<string>
Chris Lattner6f82d072003-10-28 19:16:35 +000033 BytecodeFile(cl::Positional, cl::desc("<program bytecode file>"),
34 cl::Required);
35
Reid Spencer78b0e6a2005-12-29 21:13:45 +000036 cl::opt<string>
Chris Lattner6f82d072003-10-28 19:16:35 +000037 ProfileDataFile(cl::Positional, cl::desc("<llvmprof.out file>"),
38 cl::Optional, cl::init("llvmprof.out"));
Chris Lattner5e717642003-10-30 23:42:09 +000039
40 cl::opt<bool>
41 PrintAnnotatedLLVM("annotated-llvm",
42 cl::desc("Print LLVM code with frequency annotations"));
43 cl::alias PrintAnnotated2("A", cl::desc("Alias for --annotated-llvm"),
44 cl::aliasopt(PrintAnnotatedLLVM));
Chris Lattnercde1cf32003-11-06 20:29:25 +000045 cl::opt<bool>
46 PrintAllCode("print-all-code",
47 cl::desc("Print annotated code for the entire program"));
Chris Lattner6f82d072003-10-28 19:16:35 +000048}
49
Chris Lattner7a78d812003-10-28 21:08:18 +000050// PairSecondSort - A sorting predicate to sort by the second element of a pair.
51template<class T>
Chris Lattner18884a82003-10-29 21:41:17 +000052struct PairSecondSortReverse
Reid Spencer78b0e6a2005-12-29 21:13:45 +000053 : public binary_function<pair<T, unsigned>,
54 pair<T, unsigned>, bool> {
55 bool operator()(const pair<T, unsigned> &LHS,
56 const pair<T, unsigned> &RHS) const {
Chris Lattner18884a82003-10-29 21:41:17 +000057 return LHS.second > RHS.second;
Chris Lattner7a78d812003-10-28 21:08:18 +000058 }
59};
60
Chris Lattner5e717642003-10-30 23:42:09 +000061namespace {
62 class ProfileAnnotator : public AssemblyAnnotationWriter {
Reid Spencer78b0e6a2005-12-29 21:13:45 +000063 map<const Function *, unsigned> &FuncFreqs;
64 map<const BasicBlock*, unsigned> &BlockFreqs;
65 map<ProfileInfoLoader::Edge, unsigned> &EdgeFreqs;
Chris Lattner5e717642003-10-30 23:42:09 +000066 public:
Reid Spencer78b0e6a2005-12-29 21:13:45 +000067 ProfileAnnotator(map<const Function *, unsigned> &FF,
68 map<const BasicBlock*, unsigned> &BF,
69 map<ProfileInfoLoader::Edge, unsigned> &EF)
Chris Lattner8c2730e2004-03-08 20:04:32 +000070 : FuncFreqs(FF), BlockFreqs(BF), EdgeFreqs(EF) {}
Chris Lattner5e717642003-10-30 23:42:09 +000071
Reid Spencer78b0e6a2005-12-29 21:13:45 +000072 virtual void emitFunctionAnnot(const Function *F, ostream &OS) {
Chris Lattner5e717642003-10-30 23:42:09 +000073 OS << ";;; %" << F->getName() << " called " << FuncFreqs[F]
74 << " times.\n;;;\n";
75 }
Chris Lattner8c2730e2004-03-08 20:04:32 +000076 virtual void emitBasicBlockStartAnnot(const BasicBlock *BB,
Reid Spencer78b0e6a2005-12-29 21:13:45 +000077 ostream &OS) {
Chris Lattner36737302003-10-30 23:44:28 +000078 if (BlockFreqs.empty()) return;
Chris Lattner5e717642003-10-30 23:42:09 +000079 if (unsigned Count = BlockFreqs[BB])
Chris Lattner8c2730e2004-03-08 20:04:32 +000080 OS << "\t;;; Basic block executed " << Count << " times.\n";
Chris Lattner5e717642003-10-30 23:42:09 +000081 else
Chris Lattner8c2730e2004-03-08 20:04:32 +000082 OS << "\t;;; Never executed!\n";
83 }
84
Reid Spencer78b0e6a2005-12-29 21:13:45 +000085 virtual void emitBasicBlockEndAnnot(const BasicBlock *BB, ostream &OS){
Chris Lattner8c2730e2004-03-08 20:04:32 +000086 if (EdgeFreqs.empty()) return;
87
88 // Figure out how many times each successor executed.
Reid Spencer78b0e6a2005-12-29 21:13:45 +000089 vector<pair<const BasicBlock*, unsigned> > SuccCounts;
Chris Lattner8c2730e2004-03-08 20:04:32 +000090 const TerminatorInst *TI = BB->getTerminator();
Misha Brukman3da94ae2005-04-22 00:00:37 +000091
Reid Spencer78b0e6a2005-12-29 21:13:45 +000092 map<ProfileInfoLoader::Edge, unsigned>::iterator I =
93 EdgeFreqs.lower_bound(make_pair(const_cast<BasicBlock*>(BB), 0U));
Chris Lattner8c2730e2004-03-08 20:04:32 +000094 for (; I != EdgeFreqs.end() && I->first.first == BB; ++I)
95 if (I->second)
Reid Spencer78b0e6a2005-12-29 21:13:45 +000096 SuccCounts.push_back(make_pair(TI->getSuccessor(I->first.second),
Chris Lattner8c2730e2004-03-08 20:04:32 +000097 I->second));
98 if (!SuccCounts.empty()) {
99 OS << "\t;;; Out-edge counts:";
100 for (unsigned i = 0, e = SuccCounts.size(); i != e; ++i)
101 OS << " [" << SuccCounts[i].second << " -> "
102 << SuccCounts[i].first->getName() << "]";
103 OS << "\n";
104 }
Chris Lattner5e717642003-10-30 23:42:09 +0000105 }
106 };
107}
108
Chris Lattner7a78d812003-10-28 21:08:18 +0000109
Chris Lattner6f82d072003-10-28 19:16:35 +0000110int main(int argc, char **argv) {
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000111 try {
112 cl::ParseCommandLineOptions(argc, argv, " llvm profile dump decoder\n");
113 sys::PrintStackTraceOnErrorSignal();
Chris Lattner6f82d072003-10-28 19:16:35 +0000114
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000115 // Read in the bytecode file...
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000116 string ErrorMessage;
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000117 Module *M = ParseBytecodeFile(BytecodeFile, &ErrorMessage);
118 if (M == 0) {
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000119 cerr << argv[0] << ": " << BytecodeFile << ": " << ErrorMessage << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000120 return 1;
Chris Lattner4963dcf2003-10-28 22:30:37 +0000121 }
122
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000123 // Read the profiling information
124 ProfileInfoLoader PI(argv[0], ProfileDataFile, *M);
Chris Lattner33f1ca72003-10-28 21:25:23 +0000125
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000126 map<const Function *, unsigned> FuncFreqs;
127 map<const BasicBlock*, unsigned> BlockFreqs;
128 map<ProfileInfoLoader::Edge, unsigned> EdgeFreqs;
Chris Lattner33f1ca72003-10-28 21:25:23 +0000129
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000130 // Output a report. Eventually, there will be multiple reports selectable on
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000131 // the command line, for now, just keep things simple.
Chris Lattner18884a82003-10-29 21:41:17 +0000132
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000133 // Emit the most frequent function table...
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000134 vector<pair<Function*, unsigned> > FunctionCounts;
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000135 PI.getFunctionCounts(FunctionCounts);
136 FuncFreqs.insert(FunctionCounts.begin(), FunctionCounts.end());
Chris Lattner18884a82003-10-29 21:41:17 +0000137
138 // Sort by the frequency, backwards.
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000139 sort(FunctionCounts.begin(), FunctionCounts.end(),
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000140 PairSecondSortReverse<Function*>());
141
142 unsigned long long TotalExecutions = 0;
143 for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i)
144 TotalExecutions += FunctionCounts[i].second;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000145
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000146 cout << "===" << string(73, '-') << "===\n"
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000147 << "LLVM profiling output for execution";
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000148 if (PI.getNumExecutions() != 1) cout << "s";
149 cout << ":\n";
Misha Brukman3da94ae2005-04-22 00:00:37 +0000150
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000151 for (unsigned i = 0, e = PI.getNumExecutions(); i != e; ++i) {
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000152 cout << " ";
153 if (e != 1) cout << i+1 << ". ";
154 cout << PI.getExecution(i) << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000155 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000156
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000157 cout << "\n===" << string(73, '-') << "===\n";
158 cout << "Function execution frequencies:\n\n";
Chris Lattner18884a82003-10-29 21:41:17 +0000159
160 // Print out the function frequencies...
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000161 cout << " ## Frequency\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000162 for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i) {
163 if (FunctionCounts[i].second == 0) {
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000164 cout << "\n NOTE: " << e-i << " function" <<
165 (e-i-1 ? "s were" : " was") << " never executed!\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000166 break;
167 }
168
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000169 cout << setw(3) << i+1 << ". "
170 << setw(5) << FunctionCounts[i].second << "/"
171 << TotalExecutions << " "
172 << FunctionCounts[i].first->getName().c_str() << "\n";
Chris Lattner5e717642003-10-30 23:42:09 +0000173 }
Chris Lattner18884a82003-10-29 21:41:17 +0000174
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000175 set<Function*> FunctionsToPrint;
Chris Lattner8c2730e2004-03-08 20:04:32 +0000176
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000177 // If we have block count information, print out the LLVM module with
178 // frequency annotations.
179 if (PI.hasAccurateBlockCounts()) {
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000180 vector<pair<BasicBlock*, unsigned> > Counts;
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000181 PI.getBlockCounts(Counts);
182
183 TotalExecutions = 0;
184 for (unsigned i = 0, e = Counts.size(); i != e; ++i)
185 TotalExecutions += Counts[i].second;
186
187 // Sort by the frequency, backwards.
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000188 sort(Counts.begin(), Counts.end(),
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000189 PairSecondSortReverse<BasicBlock*>());
Misha Brukman3da94ae2005-04-22 00:00:37 +0000190
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000191 cout << "\n===" << string(73, '-') << "===\n";
192 cout << "Top 20 most frequently executed basic blocks:\n\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000193
194 // Print out the function frequencies...
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000195 cout <<" ## %% \tFrequency\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000196 unsigned BlocksToPrint = Counts.size();
197 if (BlocksToPrint > 20) BlocksToPrint = 20;
198 for (unsigned i = 0; i != BlocksToPrint; ++i) {
199 if (Counts[i].second == 0) break;
200 Function *F = Counts[i].first->getParent();
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000201 cout << setw(3) << i+1 << ". "
202 << setw(5) << setprecision(2)
203 << Counts[i].second/(double)TotalExecutions*100 << "% "
204 << setw(5) << Counts[i].second << "/"
205 << TotalExecutions << "\t"
206 << F->getName().c_str() << "() - "
207 << Counts[i].first->getName().c_str() << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000208 FunctionsToPrint.insert(F);
209 }
210
211 BlockFreqs.insert(Counts.begin(), Counts.end());
212 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000213
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000214 if (PI.hasAccurateEdgeCounts()) {
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000215 vector<pair<ProfileInfoLoader::Edge, unsigned> > Counts;
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000216 PI.getEdgeCounts(Counts);
217 EdgeFreqs.insert(Counts.begin(), Counts.end());
218 }
Chris Lattner5e717642003-10-30 23:42:09 +0000219
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000220 if (PrintAnnotatedLLVM || PrintAllCode) {
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000221 cout << "\n===" << string(73, '-') << "===\n";
222 cout << "Annotated LLVM code for the module:\n\n";
Misha Brukman3da94ae2005-04-22 00:00:37 +0000223
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000224 ProfileAnnotator PA(FuncFreqs, BlockFreqs, EdgeFreqs);
225
226 if (FunctionsToPrint.empty() || PrintAllCode)
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000227 M->print(cout, &PA);
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000228 else
229 // Print just a subset of the functions...
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000230 for (set<Function*>::iterator I = FunctionsToPrint.begin(),
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000231 E = FunctionsToPrint.end(); I != E; ++I)
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000232 (*I)->print(cout, &PA);
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000233 }
234
235 return 0;
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000236 } catch (const string& msg) {
237 cerr << argv[0] << ": " << msg << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000238 } catch (...) {
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000239 cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
Chris Lattner33f1ca72003-10-28 21:25:23 +0000240 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000241 return 1;
Chris Lattner6f82d072003-10-28 19:16:35 +0000242}