blob: 6b1d514bcd6e07bf557e1926a39cbd24454f757c [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 Lattnerc30598b2006-12-06 01:18:01 +000022#include "llvm/Support/ManagedStatic.h"
Chris Lattnerbed85ff2004-05-27 05:41:36 +000023#include "llvm/System/Signals.h"
Jeff Cohenca5183d2007-03-05 00:00:42 +000024#include <algorithm>
Reid Spencer86f42bd2004-07-04 12:20:55 +000025#include <iostream>
Reid Spencer78b0e6a2005-12-29 21:13:45 +000026#include <iomanip>
Chris Lattner33f1ca72003-10-28 21:25:23 +000027#include <map>
Chris Lattner5e717642003-10-30 23:42:09 +000028#include <set>
Chris Lattner6f82d072003-10-28 19:16:35 +000029
Brian Gaeked0fde302003-11-11 22:41:34 +000030using namespace llvm;
31
Chris Lattner6f82d072003-10-28 19:16:35 +000032namespace {
Reid Spencer1adc3de2005-12-30 09:07:29 +000033 cl::opt<std::string>
Chris Lattner6f82d072003-10-28 19:16:35 +000034 BytecodeFile(cl::Positional, cl::desc("<program bytecode file>"),
35 cl::Required);
36
Reid Spencer1adc3de2005-12-30 09:07:29 +000037 cl::opt<std::string>
Chris Lattner6f82d072003-10-28 19:16:35 +000038 ProfileDataFile(cl::Positional, cl::desc("<llvmprof.out file>"),
39 cl::Optional, cl::init("llvmprof.out"));
Chris Lattner5e717642003-10-30 23:42:09 +000040
41 cl::opt<bool>
42 PrintAnnotatedLLVM("annotated-llvm",
43 cl::desc("Print LLVM code with frequency annotations"));
44 cl::alias PrintAnnotated2("A", cl::desc("Alias for --annotated-llvm"),
45 cl::aliasopt(PrintAnnotatedLLVM));
Chris Lattnercde1cf32003-11-06 20:29:25 +000046 cl::opt<bool>
47 PrintAllCode("print-all-code",
48 cl::desc("Print annotated code for the entire program"));
Chris Lattner6f82d072003-10-28 19:16:35 +000049}
50
Chris Lattner7a78d812003-10-28 21:08:18 +000051// PairSecondSort - A sorting predicate to sort by the second element of a pair.
52template<class T>
Chris Lattner18884a82003-10-29 21:41:17 +000053struct PairSecondSortReverse
Reid Spencer1adc3de2005-12-30 09:07:29 +000054 : public std::binary_function<std::pair<T, unsigned>,
55 std::pair<T, unsigned>, bool> {
56 bool operator()(const std::pair<T, unsigned> &LHS,
57 const std::pair<T, unsigned> &RHS) const {
Chris Lattner18884a82003-10-29 21:41:17 +000058 return LHS.second > RHS.second;
Chris Lattner7a78d812003-10-28 21:08:18 +000059 }
60};
61
Chris Lattner5e717642003-10-30 23:42:09 +000062namespace {
63 class ProfileAnnotator : public AssemblyAnnotationWriter {
Reid Spencer1adc3de2005-12-30 09:07:29 +000064 std::map<const Function *, unsigned> &FuncFreqs;
65 std::map<const BasicBlock*, unsigned> &BlockFreqs;
66 std::map<ProfileInfoLoader::Edge, unsigned> &EdgeFreqs;
Chris Lattner5e717642003-10-30 23:42:09 +000067 public:
Reid Spencer1adc3de2005-12-30 09:07:29 +000068 ProfileAnnotator(std::map<const Function *, unsigned> &FF,
69 std::map<const BasicBlock*, unsigned> &BF,
70 std::map<ProfileInfoLoader::Edge, unsigned> &EF)
Chris Lattner8c2730e2004-03-08 20:04:32 +000071 : FuncFreqs(FF), BlockFreqs(BF), EdgeFreqs(EF) {}
Chris Lattner5e717642003-10-30 23:42:09 +000072
Reid Spencer1adc3de2005-12-30 09:07:29 +000073 virtual void emitFunctionAnnot(const Function *F, std::ostream &OS) {
Chris Lattner5e717642003-10-30 23:42:09 +000074 OS << ";;; %" << F->getName() << " called " << FuncFreqs[F]
75 << " times.\n;;;\n";
76 }
Chris Lattner8c2730e2004-03-08 20:04:32 +000077 virtual void emitBasicBlockStartAnnot(const BasicBlock *BB,
Reid Spencer1adc3de2005-12-30 09:07:29 +000078 std::ostream &OS) {
Chris Lattner36737302003-10-30 23:44:28 +000079 if (BlockFreqs.empty()) return;
Chris Lattner5e717642003-10-30 23:42:09 +000080 if (unsigned Count = BlockFreqs[BB])
Chris Lattner8c2730e2004-03-08 20:04:32 +000081 OS << "\t;;; Basic block executed " << Count << " times.\n";
Chris Lattner5e717642003-10-30 23:42:09 +000082 else
Chris Lattner8c2730e2004-03-08 20:04:32 +000083 OS << "\t;;; Never executed!\n";
84 }
85
Reid Spencer1adc3de2005-12-30 09:07:29 +000086 virtual void emitBasicBlockEndAnnot(const BasicBlock *BB, std::ostream &OS){
Chris Lattner8c2730e2004-03-08 20:04:32 +000087 if (EdgeFreqs.empty()) return;
88
89 // Figure out how many times each successor executed.
Reid Spencer1adc3de2005-12-30 09:07:29 +000090 std::vector<std::pair<const BasicBlock*, unsigned> > SuccCounts;
Chris Lattner8c2730e2004-03-08 20:04:32 +000091 const TerminatorInst *TI = BB->getTerminator();
Misha Brukman3da94ae2005-04-22 00:00:37 +000092
Reid Spencer1adc3de2005-12-30 09:07:29 +000093 std::map<ProfileInfoLoader::Edge, unsigned>::iterator I =
94 EdgeFreqs.lower_bound(std::make_pair(const_cast<BasicBlock*>(BB), 0U));
Chris Lattner8c2730e2004-03-08 20:04:32 +000095 for (; I != EdgeFreqs.end() && I->first.first == BB; ++I)
96 if (I->second)
Reid Spencer1adc3de2005-12-30 09:07:29 +000097 SuccCounts.push_back(std::make_pair(TI->getSuccessor(I->first.second),
Chris Lattner8c2730e2004-03-08 20:04:32 +000098 I->second));
99 if (!SuccCounts.empty()) {
100 OS << "\t;;; Out-edge counts:";
101 for (unsigned i = 0, e = SuccCounts.size(); i != e; ++i)
102 OS << " [" << SuccCounts[i].second << " -> "
103 << SuccCounts[i].first->getName() << "]";
104 OS << "\n";
105 }
Chris Lattner5e717642003-10-30 23:42:09 +0000106 }
107 };
108}
109
Chris Lattner7a78d812003-10-28 21:08:18 +0000110
Chris Lattner6f82d072003-10-28 19:16:35 +0000111int main(int argc, char **argv) {
Chris Lattnerc30598b2006-12-06 01:18:01 +0000112 llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000113 try {
114 cl::ParseCommandLineOptions(argc, argv, " llvm profile dump decoder\n");
115 sys::PrintStackTraceOnErrorSignal();
Chris Lattner6f82d072003-10-28 19:16:35 +0000116
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000117 // Read in the bytecode file...
Reid Spencer1adc3de2005-12-30 09:07:29 +0000118 std::string ErrorMessage;
Chris Lattnerf2e292c2007-02-07 21:41:02 +0000119 Module *M = ParseBytecodeFile(BytecodeFile,
120 Compressor::decompressToNewBuffer,
121 &ErrorMessage);
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000122 if (M == 0) {
Reid Spencer1adc3de2005-12-30 09:07:29 +0000123 std::cerr << argv[0] << ": " << BytecodeFile << ": "
124 << ErrorMessage << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000125 return 1;
Chris Lattner4963dcf2003-10-28 22:30:37 +0000126 }
127
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000128 // Read the profiling information
129 ProfileInfoLoader PI(argv[0], ProfileDataFile, *M);
Chris Lattner33f1ca72003-10-28 21:25:23 +0000130
Reid Spencer1adc3de2005-12-30 09:07:29 +0000131 std::map<const Function *, unsigned> FuncFreqs;
132 std::map<const BasicBlock*, unsigned> BlockFreqs;
133 std::map<ProfileInfoLoader::Edge, unsigned> EdgeFreqs;
Chris Lattner33f1ca72003-10-28 21:25:23 +0000134
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000135 // Output a report. Eventually, there will be multiple reports selectable on
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000136 // the command line, for now, just keep things simple.
Chris Lattner18884a82003-10-29 21:41:17 +0000137
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000138 // Emit the most frequent function table...
Reid Spencer1adc3de2005-12-30 09:07:29 +0000139 std::vector<std::pair<Function*, unsigned> > FunctionCounts;
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000140 PI.getFunctionCounts(FunctionCounts);
141 FuncFreqs.insert(FunctionCounts.begin(), FunctionCounts.end());
Chris Lattner18884a82003-10-29 21:41:17 +0000142
143 // Sort by the frequency, backwards.
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000144 sort(FunctionCounts.begin(), FunctionCounts.end(),
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000145 PairSecondSortReverse<Function*>());
146
Reid Spencer19b7e0e2006-05-24 19:21:13 +0000147 uint64_t TotalExecutions = 0;
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000148 for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i)
149 TotalExecutions += FunctionCounts[i].second;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000150
Reid Spencer1adc3de2005-12-30 09:07:29 +0000151 std::cout << "===" << std::string(73, '-') << "===\n"
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000152 << "LLVM profiling output for execution";
Reid Spencer1adc3de2005-12-30 09:07:29 +0000153 if (PI.getNumExecutions() != 1) std::cout << "s";
154 std::cout << ":\n";
Misha Brukman3da94ae2005-04-22 00:00:37 +0000155
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000156 for (unsigned i = 0, e = PI.getNumExecutions(); i != e; ++i) {
Reid Spencer1adc3de2005-12-30 09:07:29 +0000157 std::cout << " ";
158 if (e != 1) std::cout << i+1 << ". ";
159 std::cout << PI.getExecution(i) << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000160 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000161
Reid Spencer1adc3de2005-12-30 09:07:29 +0000162 std::cout << "\n===" << std::string(73, '-') << "===\n";
163 std::cout << "Function execution frequencies:\n\n";
Chris Lattner18884a82003-10-29 21:41:17 +0000164
165 // Print out the function frequencies...
Reid Spencer1adc3de2005-12-30 09:07:29 +0000166 std::cout << " ## Frequency\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000167 for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i) {
168 if (FunctionCounts[i].second == 0) {
Reid Spencer1adc3de2005-12-30 09:07:29 +0000169 std::cout << "\n NOTE: " << e-i << " function" <<
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000170 (e-i-1 ? "s were" : " was") << " never executed!\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000171 break;
172 }
173
Reid Spencer1adc3de2005-12-30 09:07:29 +0000174 std::cout << std::setw(3) << i+1 << ". "
175 << std::setw(5) << FunctionCounts[i].second << "/"
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000176 << TotalExecutions << " "
177 << FunctionCounts[i].first->getName().c_str() << "\n";
Chris Lattner5e717642003-10-30 23:42:09 +0000178 }
Chris Lattner18884a82003-10-29 21:41:17 +0000179
Reid Spencer1adc3de2005-12-30 09:07:29 +0000180 std::set<Function*> FunctionsToPrint;
Chris Lattner8c2730e2004-03-08 20:04:32 +0000181
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000182 // If we have block count information, print out the LLVM module with
183 // frequency annotations.
184 if (PI.hasAccurateBlockCounts()) {
Reid Spencer1adc3de2005-12-30 09:07:29 +0000185 std::vector<std::pair<BasicBlock*, unsigned> > Counts;
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000186 PI.getBlockCounts(Counts);
187
188 TotalExecutions = 0;
189 for (unsigned i = 0, e = Counts.size(); i != e; ++i)
190 TotalExecutions += Counts[i].second;
191
192 // Sort by the frequency, backwards.
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000193 sort(Counts.begin(), Counts.end(),
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000194 PairSecondSortReverse<BasicBlock*>());
Misha Brukman3da94ae2005-04-22 00:00:37 +0000195
Reid Spencer1adc3de2005-12-30 09:07:29 +0000196 std::cout << "\n===" << std::string(73, '-') << "===\n";
197 std::cout << "Top 20 most frequently executed basic blocks:\n\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000198
199 // Print out the function frequencies...
Reid Spencer1adc3de2005-12-30 09:07:29 +0000200 std::cout <<" ## %% \tFrequency\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000201 unsigned BlocksToPrint = Counts.size();
202 if (BlocksToPrint > 20) BlocksToPrint = 20;
203 for (unsigned i = 0; i != BlocksToPrint; ++i) {
204 if (Counts[i].second == 0) break;
205 Function *F = Counts[i].first->getParent();
Reid Spencer1adc3de2005-12-30 09:07:29 +0000206 std::cout << std::setw(3) << i+1 << ". "
207 << std::setw(5) << std::setprecision(2)
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000208 << Counts[i].second/(double)TotalExecutions*100 << "% "
Reid Spencer1adc3de2005-12-30 09:07:29 +0000209 << std::setw(5) << Counts[i].second << "/"
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000210 << TotalExecutions << "\t"
211 << F->getName().c_str() << "() - "
212 << Counts[i].first->getName().c_str() << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000213 FunctionsToPrint.insert(F);
214 }
215
216 BlockFreqs.insert(Counts.begin(), Counts.end());
217 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000218
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000219 if (PI.hasAccurateEdgeCounts()) {
Reid Spencer1adc3de2005-12-30 09:07:29 +0000220 std::vector<std::pair<ProfileInfoLoader::Edge, unsigned> > Counts;
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000221 PI.getEdgeCounts(Counts);
222 EdgeFreqs.insert(Counts.begin(), Counts.end());
223 }
Chris Lattner5e717642003-10-30 23:42:09 +0000224
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000225 if (PrintAnnotatedLLVM || PrintAllCode) {
Reid Spencer1adc3de2005-12-30 09:07:29 +0000226 std::cout << "\n===" << std::string(73, '-') << "===\n";
227 std::cout << "Annotated LLVM code for the module:\n\n";
Misha Brukman3da94ae2005-04-22 00:00:37 +0000228
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000229 ProfileAnnotator PA(FuncFreqs, BlockFreqs, EdgeFreqs);
230
231 if (FunctionsToPrint.empty() || PrintAllCode)
Reid Spencer1adc3de2005-12-30 09:07:29 +0000232 M->print(std::cout, &PA);
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000233 else
234 // Print just a subset of the functions...
Reid Spencer1adc3de2005-12-30 09:07:29 +0000235 for (std::set<Function*>::iterator I = FunctionsToPrint.begin(),
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000236 E = FunctionsToPrint.end(); I != E; ++I)
Reid Spencer1adc3de2005-12-30 09:07:29 +0000237 (*I)->print(std::cout, &PA);
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000238 }
239
240 return 0;
Reid Spencer1adc3de2005-12-30 09:07:29 +0000241 } catch (const std::string& msg) {
242 std::cerr << argv[0] << ": " << msg << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000243 } catch (...) {
Reid Spencer1adc3de2005-12-30 09:07:29 +0000244 std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
Chris Lattner33f1ca72003-10-28 21:25:23 +0000245 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000246 return 1;
Chris Lattner6f82d072003-10-28 19:16:35 +0000247}