blob: c85b0da1bc6cdb06618f2983934c2094547427d6 [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 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 Lattnerbed85ff2004-05-27 05:41:36 +000024#include "llvm/System/Signals.h"
Jeff Cohenca5183d2007-03-05 00:00:42 +000025#include <algorithm>
Reid Spencer86f42bd2004-07-04 12:20:55 +000026#include <iostream>
Reid Spencer78b0e6a2005-12-29 21:13:45 +000027#include <iomanip>
Chris Lattner33f1ca72003-10-28 21:25:23 +000028#include <map>
Chris Lattner5e717642003-10-30 23:42:09 +000029#include <set>
Chris Lattner6f82d072003-10-28 19:16:35 +000030
Brian Gaeked0fde302003-11-11 22:41:34 +000031using namespace llvm;
32
Chris Lattner6f82d072003-10-28 19:16:35 +000033namespace {
Reid Spencer1adc3de2005-12-30 09:07:29 +000034 cl::opt<std::string>
Chris Lattner6f82d072003-10-28 19:16:35 +000035 BytecodeFile(cl::Positional, cl::desc("<program bytecode file>"),
36 cl::Required);
37
Reid Spencer1adc3de2005-12-30 09:07:29 +000038 cl::opt<std::string>
Chris Lattner6f82d072003-10-28 19:16:35 +000039 ProfileDataFile(cl::Positional, cl::desc("<llvmprof.out file>"),
40 cl::Optional, cl::init("llvmprof.out"));
Chris Lattner5e717642003-10-30 23:42:09 +000041
42 cl::opt<bool>
43 PrintAnnotatedLLVM("annotated-llvm",
44 cl::desc("Print LLVM code with frequency annotations"));
45 cl::alias PrintAnnotated2("A", cl::desc("Alias for --annotated-llvm"),
46 cl::aliasopt(PrintAnnotatedLLVM));
Chris Lattnercde1cf32003-11-06 20:29:25 +000047 cl::opt<bool>
48 PrintAllCode("print-all-code",
49 cl::desc("Print annotated code for the entire program"));
Chris Lattner6f82d072003-10-28 19:16:35 +000050}
51
Chris Lattner7a78d812003-10-28 21:08:18 +000052// PairSecondSort - A sorting predicate to sort by the second element of a pair.
53template<class T>
Chris Lattner18884a82003-10-29 21:41:17 +000054struct PairSecondSortReverse
Reid Spencer1adc3de2005-12-30 09:07:29 +000055 : public std::binary_function<std::pair<T, unsigned>,
56 std::pair<T, unsigned>, bool> {
57 bool operator()(const std::pair<T, unsigned> &LHS,
58 const std::pair<T, unsigned> &RHS) const {
Chris Lattner18884a82003-10-29 21:41:17 +000059 return LHS.second > RHS.second;
Chris Lattner7a78d812003-10-28 21:08:18 +000060 }
61};
62
Chris Lattner5e717642003-10-30 23:42:09 +000063namespace {
64 class ProfileAnnotator : public AssemblyAnnotationWriter {
Reid Spencer1adc3de2005-12-30 09:07:29 +000065 std::map<const Function *, unsigned> &FuncFreqs;
66 std::map<const BasicBlock*, unsigned> &BlockFreqs;
67 std::map<ProfileInfoLoader::Edge, unsigned> &EdgeFreqs;
Chris Lattner5e717642003-10-30 23:42:09 +000068 public:
Reid Spencer1adc3de2005-12-30 09:07:29 +000069 ProfileAnnotator(std::map<const Function *, unsigned> &FF,
70 std::map<const BasicBlock*, unsigned> &BF,
71 std::map<ProfileInfoLoader::Edge, unsigned> &EF)
Chris Lattner8c2730e2004-03-08 20:04:32 +000072 : FuncFreqs(FF), BlockFreqs(BF), EdgeFreqs(EF) {}
Chris Lattner5e717642003-10-30 23:42:09 +000073
Reid Spencer1adc3de2005-12-30 09:07:29 +000074 virtual void emitFunctionAnnot(const Function *F, std::ostream &OS) {
Chris Lattner5e717642003-10-30 23:42:09 +000075 OS << ";;; %" << F->getName() << " called " << FuncFreqs[F]
76 << " times.\n;;;\n";
77 }
Chris Lattner8c2730e2004-03-08 20:04:32 +000078 virtual void emitBasicBlockStartAnnot(const BasicBlock *BB,
Reid Spencer1adc3de2005-12-30 09:07:29 +000079 std::ostream &OS) {
Chris Lattner36737302003-10-30 23:44:28 +000080 if (BlockFreqs.empty()) return;
Chris Lattner5e717642003-10-30 23:42:09 +000081 if (unsigned Count = BlockFreqs[BB])
Chris Lattner8c2730e2004-03-08 20:04:32 +000082 OS << "\t;;; Basic block executed " << Count << " times.\n";
Chris Lattner5e717642003-10-30 23:42:09 +000083 else
Chris Lattner8c2730e2004-03-08 20:04:32 +000084 OS << "\t;;; Never executed!\n";
85 }
86
Reid Spencer1adc3de2005-12-30 09:07:29 +000087 virtual void emitBasicBlockEndAnnot(const BasicBlock *BB, std::ostream &OS){
Chris Lattner8c2730e2004-03-08 20:04:32 +000088 if (EdgeFreqs.empty()) return;
89
90 // Figure out how many times each successor executed.
Reid Spencer1adc3de2005-12-30 09:07:29 +000091 std::vector<std::pair<const BasicBlock*, unsigned> > SuccCounts;
Chris Lattner8c2730e2004-03-08 20:04:32 +000092 const TerminatorInst *TI = BB->getTerminator();
Misha Brukman3da94ae2005-04-22 00:00:37 +000093
Reid Spencer1adc3de2005-12-30 09:07:29 +000094 std::map<ProfileInfoLoader::Edge, unsigned>::iterator I =
95 EdgeFreqs.lower_bound(std::make_pair(const_cast<BasicBlock*>(BB), 0U));
Chris Lattner8c2730e2004-03-08 20:04:32 +000096 for (; I != EdgeFreqs.end() && I->first.first == BB; ++I)
97 if (I->second)
Reid Spencer1adc3de2005-12-30 09:07:29 +000098 SuccCounts.push_back(std::make_pair(TI->getSuccessor(I->first.second),
Chris Lattner8c2730e2004-03-08 20:04:32 +000099 I->second));
100 if (!SuccCounts.empty()) {
101 OS << "\t;;; Out-edge counts:";
102 for (unsigned i = 0, e = SuccCounts.size(); i != e; ++i)
103 OS << " [" << SuccCounts[i].second << " -> "
104 << SuccCounts[i].first->getName() << "]";
105 OS << "\n";
106 }
Chris Lattner5e717642003-10-30 23:42:09 +0000107 }
108 };
109}
110
Chris Lattner7a78d812003-10-28 21:08:18 +0000111
Chris Lattner6f82d072003-10-28 19:16:35 +0000112int main(int argc, char **argv) {
Chris Lattnerc30598b2006-12-06 01:18:01 +0000113 llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000114 try {
115 cl::ParseCommandLineOptions(argc, argv, " llvm profile dump decoder\n");
116 sys::PrintStackTraceOnErrorSignal();
Chris Lattner6f82d072003-10-28 19:16:35 +0000117
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000118 // Read in the bytecode file...
Reid Spencer1adc3de2005-12-30 09:07:29 +0000119 std::string ErrorMessage;
Reid Spencer295b1ce2007-05-07 18:50:07 +0000120 Module *M = 0;
Chris Lattner065344d2007-05-06 23:45:49 +0000121 if (MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(BytecodeFile,
122 &ErrorMessage)) {
Chris Lattner44dadff2007-05-06 09:29:57 +0000123 M = ParseBitcodeFile(Buffer, &ErrorMessage);
Chris Lattner065344d2007-05-06 23:45:49 +0000124 delete Buffer;
125 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000126 if (M == 0) {
Reid Spencer1adc3de2005-12-30 09:07:29 +0000127 std::cerr << argv[0] << ": " << BytecodeFile << ": "
128 << ErrorMessage << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000129 return 1;
Chris Lattner4963dcf2003-10-28 22:30:37 +0000130 }
131
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000132 // Read the profiling information
133 ProfileInfoLoader PI(argv[0], ProfileDataFile, *M);
Chris Lattner33f1ca72003-10-28 21:25:23 +0000134
Reid Spencer1adc3de2005-12-30 09:07:29 +0000135 std::map<const Function *, unsigned> FuncFreqs;
136 std::map<const BasicBlock*, unsigned> BlockFreqs;
137 std::map<ProfileInfoLoader::Edge, unsigned> EdgeFreqs;
Chris Lattner33f1ca72003-10-28 21:25:23 +0000138
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000139 // Output a report. Eventually, there will be multiple reports selectable on
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000140 // the command line, for now, just keep things simple.
Chris Lattner18884a82003-10-29 21:41:17 +0000141
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000142 // Emit the most frequent function table...
Reid Spencer1adc3de2005-12-30 09:07:29 +0000143 std::vector<std::pair<Function*, unsigned> > FunctionCounts;
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000144 PI.getFunctionCounts(FunctionCounts);
145 FuncFreqs.insert(FunctionCounts.begin(), FunctionCounts.end());
Chris Lattner18884a82003-10-29 21:41:17 +0000146
147 // Sort by the frequency, backwards.
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000148 sort(FunctionCounts.begin(), FunctionCounts.end(),
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000149 PairSecondSortReverse<Function*>());
150
Reid Spencer19b7e0e2006-05-24 19:21:13 +0000151 uint64_t TotalExecutions = 0;
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000152 for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i)
153 TotalExecutions += FunctionCounts[i].second;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000154
Reid Spencer1adc3de2005-12-30 09:07:29 +0000155 std::cout << "===" << std::string(73, '-') << "===\n"
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000156 << "LLVM profiling output for execution";
Reid Spencer1adc3de2005-12-30 09:07:29 +0000157 if (PI.getNumExecutions() != 1) std::cout << "s";
158 std::cout << ":\n";
Misha Brukman3da94ae2005-04-22 00:00:37 +0000159
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000160 for (unsigned i = 0, e = PI.getNumExecutions(); i != e; ++i) {
Reid Spencer1adc3de2005-12-30 09:07:29 +0000161 std::cout << " ";
162 if (e != 1) std::cout << i+1 << ". ";
163 std::cout << PI.getExecution(i) << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000164 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000165
Reid Spencer1adc3de2005-12-30 09:07:29 +0000166 std::cout << "\n===" << std::string(73, '-') << "===\n";
167 std::cout << "Function execution frequencies:\n\n";
Chris Lattner18884a82003-10-29 21:41:17 +0000168
169 // Print out the function frequencies...
Reid Spencer1adc3de2005-12-30 09:07:29 +0000170 std::cout << " ## Frequency\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000171 for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i) {
172 if (FunctionCounts[i].second == 0) {
Reid Spencer1adc3de2005-12-30 09:07:29 +0000173 std::cout << "\n NOTE: " << e-i << " function" <<
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000174 (e-i-1 ? "s were" : " was") << " never executed!\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000175 break;
176 }
177
Reid Spencer1adc3de2005-12-30 09:07:29 +0000178 std::cout << std::setw(3) << i+1 << ". "
179 << std::setw(5) << FunctionCounts[i].second << "/"
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000180 << TotalExecutions << " "
181 << FunctionCounts[i].first->getName().c_str() << "\n";
Chris Lattner5e717642003-10-30 23:42:09 +0000182 }
Chris Lattner18884a82003-10-29 21:41:17 +0000183
Reid Spencer1adc3de2005-12-30 09:07:29 +0000184 std::set<Function*> FunctionsToPrint;
Chris Lattner8c2730e2004-03-08 20:04:32 +0000185
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000186 // If we have block count information, print out the LLVM module with
187 // frequency annotations.
188 if (PI.hasAccurateBlockCounts()) {
Reid Spencer1adc3de2005-12-30 09:07:29 +0000189 std::vector<std::pair<BasicBlock*, unsigned> > Counts;
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000190 PI.getBlockCounts(Counts);
191
192 TotalExecutions = 0;
193 for (unsigned i = 0, e = Counts.size(); i != e; ++i)
194 TotalExecutions += Counts[i].second;
195
196 // Sort by the frequency, backwards.
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000197 sort(Counts.begin(), Counts.end(),
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000198 PairSecondSortReverse<BasicBlock*>());
Misha Brukman3da94ae2005-04-22 00:00:37 +0000199
Reid Spencer1adc3de2005-12-30 09:07:29 +0000200 std::cout << "\n===" << std::string(73, '-') << "===\n";
201 std::cout << "Top 20 most frequently executed basic blocks:\n\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000202
203 // Print out the function frequencies...
Reid Spencer1adc3de2005-12-30 09:07:29 +0000204 std::cout <<" ## %% \tFrequency\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000205 unsigned BlocksToPrint = Counts.size();
206 if (BlocksToPrint > 20) BlocksToPrint = 20;
207 for (unsigned i = 0; i != BlocksToPrint; ++i) {
208 if (Counts[i].second == 0) break;
209 Function *F = Counts[i].first->getParent();
Reid Spencer1adc3de2005-12-30 09:07:29 +0000210 std::cout << std::setw(3) << i+1 << ". "
211 << std::setw(5) << std::setprecision(2)
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000212 << Counts[i].second/(double)TotalExecutions*100 << "% "
Reid Spencer1adc3de2005-12-30 09:07:29 +0000213 << std::setw(5) << Counts[i].second << "/"
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000214 << TotalExecutions << "\t"
215 << F->getName().c_str() << "() - "
216 << Counts[i].first->getName().c_str() << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000217 FunctionsToPrint.insert(F);
218 }
219
220 BlockFreqs.insert(Counts.begin(), Counts.end());
221 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000222
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000223 if (PI.hasAccurateEdgeCounts()) {
Reid Spencer1adc3de2005-12-30 09:07:29 +0000224 std::vector<std::pair<ProfileInfoLoader::Edge, unsigned> > Counts;
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000225 PI.getEdgeCounts(Counts);
226 EdgeFreqs.insert(Counts.begin(), Counts.end());
227 }
Chris Lattner5e717642003-10-30 23:42:09 +0000228
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000229 if (PrintAnnotatedLLVM || PrintAllCode) {
Reid Spencer1adc3de2005-12-30 09:07:29 +0000230 std::cout << "\n===" << std::string(73, '-') << "===\n";
231 std::cout << "Annotated LLVM code for the module:\n\n";
Misha Brukman3da94ae2005-04-22 00:00:37 +0000232
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000233 ProfileAnnotator PA(FuncFreqs, BlockFreqs, EdgeFreqs);
234
235 if (FunctionsToPrint.empty() || PrintAllCode)
Reid Spencer1adc3de2005-12-30 09:07:29 +0000236 M->print(std::cout, &PA);
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000237 else
238 // Print just a subset of the functions...
Reid Spencer1adc3de2005-12-30 09:07:29 +0000239 for (std::set<Function*>::iterator I = FunctionsToPrint.begin(),
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000240 E = FunctionsToPrint.end(); I != E; ++I)
Reid Spencer1adc3de2005-12-30 09:07:29 +0000241 (*I)->print(std::cout, &PA);
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000242 }
243
244 return 0;
Reid Spencer1adc3de2005-12-30 09:07:29 +0000245 } catch (const std::string& msg) {
246 std::cerr << argv[0] << ": " << msg << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000247 } catch (...) {
Reid Spencer1adc3de2005-12-30 09:07:29 +0000248 std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
Chris Lattner33f1ca72003-10-28 21:25:23 +0000249 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000250 return 1;
Chris Lattner6f82d072003-10-28 19:16:35 +0000251}