blob: b0767e6fdf3066b85fa36f8d99a7b925825ef3ad [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"
Reid Spencer86f42bd2004-07-04 12:20:55 +000024#include <iostream>
Reid Spencer78b0e6a2005-12-29 21:13:45 +000025#include <iomanip>
Chris Lattner33f1ca72003-10-28 21:25:23 +000026#include <map>
Chris Lattner5e717642003-10-30 23:42:09 +000027#include <set>
Chris Lattner6f82d072003-10-28 19:16:35 +000028
Brian Gaeked0fde302003-11-11 22:41:34 +000029using namespace llvm;
30
Chris Lattner6f82d072003-10-28 19:16:35 +000031namespace {
Reid Spencer1adc3de2005-12-30 09:07:29 +000032 cl::opt<std::string>
Chris Lattner6f82d072003-10-28 19:16:35 +000033 BytecodeFile(cl::Positional, cl::desc("<program bytecode file>"),
34 cl::Required);
35
Reid Spencer1adc3de2005-12-30 09:07:29 +000036 cl::opt<std::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 Spencer1adc3de2005-12-30 09:07:29 +000053 : public std::binary_function<std::pair<T, unsigned>,
54 std::pair<T, unsigned>, bool> {
55 bool operator()(const std::pair<T, unsigned> &LHS,
56 const std::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 Spencer1adc3de2005-12-30 09:07:29 +000063 std::map<const Function *, unsigned> &FuncFreqs;
64 std::map<const BasicBlock*, unsigned> &BlockFreqs;
65 std::map<ProfileInfoLoader::Edge, unsigned> &EdgeFreqs;
Chris Lattner5e717642003-10-30 23:42:09 +000066 public:
Reid Spencer1adc3de2005-12-30 09:07:29 +000067 ProfileAnnotator(std::map<const Function *, unsigned> &FF,
68 std::map<const BasicBlock*, unsigned> &BF,
69 std::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 Spencer1adc3de2005-12-30 09:07:29 +000072 virtual void emitFunctionAnnot(const Function *F, std::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 Spencer1adc3de2005-12-30 09:07:29 +000077 std::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 Spencer1adc3de2005-12-30 09:07:29 +000085 virtual void emitBasicBlockEndAnnot(const BasicBlock *BB, std::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 Spencer1adc3de2005-12-30 09:07:29 +000089 std::vector<std::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 Spencer1adc3de2005-12-30 09:07:29 +000092 std::map<ProfileInfoLoader::Edge, unsigned>::iterator I =
93 EdgeFreqs.lower_bound(std::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 Spencer1adc3de2005-12-30 09:07:29 +000096 SuccCounts.push_back(std::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) {
Chris Lattnerc30598b2006-12-06 01:18:01 +0000111 llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000112 try {
113 cl::ParseCommandLineOptions(argc, argv, " llvm profile dump decoder\n");
114 sys::PrintStackTraceOnErrorSignal();
Chris Lattner6f82d072003-10-28 19:16:35 +0000115
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000116 // Read in the bytecode file...
Reid Spencer1adc3de2005-12-30 09:07:29 +0000117 std::string ErrorMessage;
Chris Lattnerf2e292c2007-02-07 21:41:02 +0000118 Module *M = ParseBytecodeFile(BytecodeFile,
119 Compressor::decompressToNewBuffer,
120 &ErrorMessage);
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000121 if (M == 0) {
Reid Spencer1adc3de2005-12-30 09:07:29 +0000122 std::cerr << argv[0] << ": " << BytecodeFile << ": "
123 << ErrorMessage << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000124 return 1;
Chris Lattner4963dcf2003-10-28 22:30:37 +0000125 }
126
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000127 // Read the profiling information
128 ProfileInfoLoader PI(argv[0], ProfileDataFile, *M);
Chris Lattner33f1ca72003-10-28 21:25:23 +0000129
Reid Spencer1adc3de2005-12-30 09:07:29 +0000130 std::map<const Function *, unsigned> FuncFreqs;
131 std::map<const BasicBlock*, unsigned> BlockFreqs;
132 std::map<ProfileInfoLoader::Edge, unsigned> EdgeFreqs;
Chris Lattner33f1ca72003-10-28 21:25:23 +0000133
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000134 // Output a report. Eventually, there will be multiple reports selectable on
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000135 // the command line, for now, just keep things simple.
Chris Lattner18884a82003-10-29 21:41:17 +0000136
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000137 // Emit the most frequent function table...
Reid Spencer1adc3de2005-12-30 09:07:29 +0000138 std::vector<std::pair<Function*, unsigned> > FunctionCounts;
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000139 PI.getFunctionCounts(FunctionCounts);
140 FuncFreqs.insert(FunctionCounts.begin(), FunctionCounts.end());
Chris Lattner18884a82003-10-29 21:41:17 +0000141
142 // Sort by the frequency, backwards.
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000143 sort(FunctionCounts.begin(), FunctionCounts.end(),
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000144 PairSecondSortReverse<Function*>());
145
Reid Spencer19b7e0e2006-05-24 19:21:13 +0000146 uint64_t TotalExecutions = 0;
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000147 for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i)
148 TotalExecutions += FunctionCounts[i].second;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000149
Reid Spencer1adc3de2005-12-30 09:07:29 +0000150 std::cout << "===" << std::string(73, '-') << "===\n"
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000151 << "LLVM profiling output for execution";
Reid Spencer1adc3de2005-12-30 09:07:29 +0000152 if (PI.getNumExecutions() != 1) std::cout << "s";
153 std::cout << ":\n";
Misha Brukman3da94ae2005-04-22 00:00:37 +0000154
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000155 for (unsigned i = 0, e = PI.getNumExecutions(); i != e; ++i) {
Reid Spencer1adc3de2005-12-30 09:07:29 +0000156 std::cout << " ";
157 if (e != 1) std::cout << i+1 << ". ";
158 std::cout << PI.getExecution(i) << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000159 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000160
Reid Spencer1adc3de2005-12-30 09:07:29 +0000161 std::cout << "\n===" << std::string(73, '-') << "===\n";
162 std::cout << "Function execution frequencies:\n\n";
Chris Lattner18884a82003-10-29 21:41:17 +0000163
164 // Print out the function frequencies...
Reid Spencer1adc3de2005-12-30 09:07:29 +0000165 std::cout << " ## Frequency\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000166 for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i) {
167 if (FunctionCounts[i].second == 0) {
Reid Spencer1adc3de2005-12-30 09:07:29 +0000168 std::cout << "\n NOTE: " << e-i << " function" <<
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000169 (e-i-1 ? "s were" : " was") << " never executed!\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000170 break;
171 }
172
Reid Spencer1adc3de2005-12-30 09:07:29 +0000173 std::cout << std::setw(3) << i+1 << ". "
174 << std::setw(5) << FunctionCounts[i].second << "/"
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000175 << TotalExecutions << " "
176 << FunctionCounts[i].first->getName().c_str() << "\n";
Chris Lattner5e717642003-10-30 23:42:09 +0000177 }
Chris Lattner18884a82003-10-29 21:41:17 +0000178
Reid Spencer1adc3de2005-12-30 09:07:29 +0000179 std::set<Function*> FunctionsToPrint;
Chris Lattner8c2730e2004-03-08 20:04:32 +0000180
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000181 // If we have block count information, print out the LLVM module with
182 // frequency annotations.
183 if (PI.hasAccurateBlockCounts()) {
Reid Spencer1adc3de2005-12-30 09:07:29 +0000184 std::vector<std::pair<BasicBlock*, unsigned> > Counts;
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000185 PI.getBlockCounts(Counts);
186
187 TotalExecutions = 0;
188 for (unsigned i = 0, e = Counts.size(); i != e; ++i)
189 TotalExecutions += Counts[i].second;
190
191 // Sort by the frequency, backwards.
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000192 sort(Counts.begin(), Counts.end(),
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000193 PairSecondSortReverse<BasicBlock*>());
Misha Brukman3da94ae2005-04-22 00:00:37 +0000194
Reid Spencer1adc3de2005-12-30 09:07:29 +0000195 std::cout << "\n===" << std::string(73, '-') << "===\n";
196 std::cout << "Top 20 most frequently executed basic blocks:\n\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000197
198 // Print out the function frequencies...
Reid Spencer1adc3de2005-12-30 09:07:29 +0000199 std::cout <<" ## %% \tFrequency\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000200 unsigned BlocksToPrint = Counts.size();
201 if (BlocksToPrint > 20) BlocksToPrint = 20;
202 for (unsigned i = 0; i != BlocksToPrint; ++i) {
203 if (Counts[i].second == 0) break;
204 Function *F = Counts[i].first->getParent();
Reid Spencer1adc3de2005-12-30 09:07:29 +0000205 std::cout << std::setw(3) << i+1 << ". "
206 << std::setw(5) << std::setprecision(2)
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000207 << Counts[i].second/(double)TotalExecutions*100 << "% "
Reid Spencer1adc3de2005-12-30 09:07:29 +0000208 << std::setw(5) << Counts[i].second << "/"
Reid Spencer78b0e6a2005-12-29 21:13:45 +0000209 << TotalExecutions << "\t"
210 << F->getName().c_str() << "() - "
211 << Counts[i].first->getName().c_str() << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000212 FunctionsToPrint.insert(F);
213 }
214
215 BlockFreqs.insert(Counts.begin(), Counts.end());
216 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000217
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000218 if (PI.hasAccurateEdgeCounts()) {
Reid Spencer1adc3de2005-12-30 09:07:29 +0000219 std::vector<std::pair<ProfileInfoLoader::Edge, unsigned> > Counts;
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000220 PI.getEdgeCounts(Counts);
221 EdgeFreqs.insert(Counts.begin(), Counts.end());
222 }
Chris Lattner5e717642003-10-30 23:42:09 +0000223
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000224 if (PrintAnnotatedLLVM || PrintAllCode) {
Reid Spencer1adc3de2005-12-30 09:07:29 +0000225 std::cout << "\n===" << std::string(73, '-') << "===\n";
226 std::cout << "Annotated LLVM code for the module:\n\n";
Misha Brukman3da94ae2005-04-22 00:00:37 +0000227
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000228 ProfileAnnotator PA(FuncFreqs, BlockFreqs, EdgeFreqs);
229
230 if (FunctionsToPrint.empty() || PrintAllCode)
Reid Spencer1adc3de2005-12-30 09:07:29 +0000231 M->print(std::cout, &PA);
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000232 else
233 // Print just a subset of the functions...
Reid Spencer1adc3de2005-12-30 09:07:29 +0000234 for (std::set<Function*>::iterator I = FunctionsToPrint.begin(),
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000235 E = FunctionsToPrint.end(); I != E; ++I)
Reid Spencer1adc3de2005-12-30 09:07:29 +0000236 (*I)->print(std::cout, &PA);
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000237 }
238
239 return 0;
Reid Spencer1adc3de2005-12-30 09:07:29 +0000240 } catch (const std::string& msg) {
241 std::cerr << argv[0] << ": " << msg << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000242 } catch (...) {
Reid Spencer1adc3de2005-12-30 09:07:29 +0000243 std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
Chris Lattner33f1ca72003-10-28 21:25:23 +0000244 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000245 return 1;
Chris Lattner6f82d072003-10-28 19:16:35 +0000246}