blob: 5a4227e9bb352ccfdcae00bb85feb9a74cbda3f6 [file] [log] [blame]
Chris Lattner6f82d072003-10-28 19:16:35 +00001//===- llvm-prof.cpp - Read in and process llvmprof.out data files --------===//
2//
3// 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.
7//
8//===----------------------------------------------------------------------===//
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"
21#include "Support/CommandLine.h"
Chris Lattnerbed85ff2004-05-27 05:41:36 +000022#include "llvm/System/Signals.h"
Chris Lattner7a78d812003-10-28 21:08:18 +000023#include <cstdio>
Chris Lattner33f1ca72003-10-28 21:25:23 +000024#include <map>
Chris Lattner5e717642003-10-30 23:42:09 +000025#include <set>
Chris Lattner6f82d072003-10-28 19:16:35 +000026
Brian Gaeked0fde302003-11-11 22:41:34 +000027using namespace llvm;
28
Chris Lattner6f82d072003-10-28 19:16:35 +000029namespace {
30 cl::opt<std::string>
31 BytecodeFile(cl::Positional, cl::desc("<program bytecode file>"),
32 cl::Required);
33
34 cl::opt<std::string>
35 ProfileDataFile(cl::Positional, cl::desc("<llvmprof.out file>"),
36 cl::Optional, cl::init("llvmprof.out"));
Chris Lattner5e717642003-10-30 23:42:09 +000037
38 cl::opt<bool>
39 PrintAnnotatedLLVM("annotated-llvm",
40 cl::desc("Print LLVM code with frequency annotations"));
41 cl::alias PrintAnnotated2("A", cl::desc("Alias for --annotated-llvm"),
42 cl::aliasopt(PrintAnnotatedLLVM));
Chris Lattnercde1cf32003-11-06 20:29:25 +000043 cl::opt<bool>
44 PrintAllCode("print-all-code",
45 cl::desc("Print annotated code for the entire program"));
Chris Lattner6f82d072003-10-28 19:16:35 +000046}
47
Chris Lattner7a78d812003-10-28 21:08:18 +000048// PairSecondSort - A sorting predicate to sort by the second element of a pair.
49template<class T>
Chris Lattner18884a82003-10-29 21:41:17 +000050struct PairSecondSortReverse
Chris Lattner7a78d812003-10-28 21:08:18 +000051 : public std::binary_function<std::pair<T, unsigned>,
52 std::pair<T, unsigned>, bool> {
53 bool operator()(const std::pair<T, unsigned> &LHS,
54 const std::pair<T, unsigned> &RHS) const {
Chris Lattner18884a82003-10-29 21:41:17 +000055 return LHS.second > RHS.second;
Chris Lattner7a78d812003-10-28 21:08:18 +000056 }
57};
58
Chris Lattner5e717642003-10-30 23:42:09 +000059namespace {
60 class ProfileAnnotator : public AssemblyAnnotationWriter {
61 std::map<const Function *, unsigned> &FuncFreqs;
62 std::map<const BasicBlock*, unsigned> &BlockFreqs;
Chris Lattner8c2730e2004-03-08 20:04:32 +000063 std::map<ProfileInfoLoader::Edge, unsigned> &EdgeFreqs;
Chris Lattner5e717642003-10-30 23:42:09 +000064 public:
65 ProfileAnnotator(std::map<const Function *, unsigned> &FF,
Chris Lattner8c2730e2004-03-08 20:04:32 +000066 std::map<const BasicBlock*, unsigned> &BF,
67 std::map<ProfileInfoLoader::Edge, unsigned> &EF)
68 : FuncFreqs(FF), BlockFreqs(BF), EdgeFreqs(EF) {}
Chris Lattner5e717642003-10-30 23:42:09 +000069
70 virtual void emitFunctionAnnot(const Function *F, std::ostream &OS) {
71 OS << ";;; %" << F->getName() << " called " << FuncFreqs[F]
72 << " times.\n;;;\n";
73 }
Chris Lattner8c2730e2004-03-08 20:04:32 +000074 virtual void emitBasicBlockStartAnnot(const BasicBlock *BB,
75 std::ostream &OS) {
Chris Lattner36737302003-10-30 23:44:28 +000076 if (BlockFreqs.empty()) return;
Chris Lattner5e717642003-10-30 23:42:09 +000077 if (unsigned Count = BlockFreqs[BB])
Chris Lattner8c2730e2004-03-08 20:04:32 +000078 OS << "\t;;; Basic block executed " << Count << " times.\n";
Chris Lattner5e717642003-10-30 23:42:09 +000079 else
Chris Lattner8c2730e2004-03-08 20:04:32 +000080 OS << "\t;;; Never executed!\n";
81 }
82
83 virtual void emitBasicBlockEndAnnot(const BasicBlock *BB, std::ostream &OS){
84 if (EdgeFreqs.empty()) return;
85
86 // Figure out how many times each successor executed.
87 std::vector<std::pair<const BasicBlock*, unsigned> > SuccCounts;
88 const TerminatorInst *TI = BB->getTerminator();
89
90 std::map<ProfileInfoLoader::Edge, unsigned>::iterator I =
91 EdgeFreqs.lower_bound(std::make_pair(const_cast<BasicBlock*>(BB), 0U));
92 for (; I != EdgeFreqs.end() && I->first.first == BB; ++I)
93 if (I->second)
94 SuccCounts.push_back(std::make_pair(TI->getSuccessor(I->first.second),
95 I->second));
96 if (!SuccCounts.empty()) {
97 OS << "\t;;; Out-edge counts:";
98 for (unsigned i = 0, e = SuccCounts.size(); i != e; ++i)
99 OS << " [" << SuccCounts[i].second << " -> "
100 << SuccCounts[i].first->getName() << "]";
101 OS << "\n";
102 }
Chris Lattner5e717642003-10-30 23:42:09 +0000103 }
104 };
105}
106
Chris Lattner7a78d812003-10-28 21:08:18 +0000107
Chris Lattner6f82d072003-10-28 19:16:35 +0000108int main(int argc, char **argv) {
109 cl::ParseCommandLineOptions(argc, argv, " llvm profile dump decoder\n");
Chris Lattnerf73b4ca2004-02-19 20:32:12 +0000110 PrintStackTraceOnErrorSignal();
Chris Lattner6f82d072003-10-28 19:16:35 +0000111
Chris Lattnere4367792003-10-28 20:13:07 +0000112 // Read in the bytecode file...
113 std::string ErrorMessage;
Chris Lattner7a78d812003-10-28 21:08:18 +0000114 Module *M = ParseBytecodeFile(BytecodeFile, &ErrorMessage);
115 if (M == 0) {
Chris Lattnere4367792003-10-28 20:13:07 +0000116 std::cerr << argv[0] << ": " << BytecodeFile << ": " << ErrorMessage
117 << "\n";
118 return 1;
119 }
Chris Lattner6f82d072003-10-28 19:16:35 +0000120
Chris Lattnere4367792003-10-28 20:13:07 +0000121 // Read the profiling information
Chris Lattner89cf3932004-02-11 05:56:07 +0000122 ProfileInfoLoader PI(argv[0], ProfileDataFile, *M);
Chris Lattner6f82d072003-10-28 19:16:35 +0000123
Chris Lattner5e717642003-10-30 23:42:09 +0000124 std::map<const Function *, unsigned> FuncFreqs;
125 std::map<const BasicBlock*, unsigned> BlockFreqs;
Chris Lattner8c2730e2004-03-08 20:04:32 +0000126 std::map<ProfileInfoLoader::Edge, unsigned> EdgeFreqs;
Chris Lattner5e717642003-10-30 23:42:09 +0000127
Chris Lattner7a78d812003-10-28 21:08:18 +0000128 // Output a report. Eventually, there will be multiple reports selectable on
129 // the command line, for now, just keep things simple.
130
131 // Emit the most frequent function table...
132 std::vector<std::pair<Function*, unsigned> > FunctionCounts;
133 PI.getFunctionCounts(FunctionCounts);
Chris Lattner5e717642003-10-30 23:42:09 +0000134 FuncFreqs.insert(FunctionCounts.begin(), FunctionCounts.end());
Chris Lattner7a78d812003-10-28 21:08:18 +0000135
136 // Sort by the frequency, backwards.
137 std::sort(FunctionCounts.begin(), FunctionCounts.end(),
Chris Lattner18884a82003-10-29 21:41:17 +0000138 PairSecondSortReverse<Function*>());
Chris Lattner7a78d812003-10-28 21:08:18 +0000139
Chris Lattner3baed412003-10-31 00:06:57 +0000140 unsigned long long TotalExecutions = 0;
Chris Lattner7a78d812003-10-28 21:08:18 +0000141 for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i)
142 TotalExecutions += FunctionCounts[i].second;
143
Chris Lattner4963dcf2003-10-28 22:30:37 +0000144 std::cout << "===" << std::string(73, '-') << "===\n"
Chris Lattner36882052003-10-28 22:53:49 +0000145 << "LLVM profiling output for execution";
146 if (PI.getNumExecutions() != 1) std::cout << "s";
147 std::cout << ":\n";
Chris Lattner4963dcf2003-10-28 22:30:37 +0000148
149 for (unsigned i = 0, e = PI.getNumExecutions(); i != e; ++i) {
150 std::cout << " ";
Chris Lattner18884a82003-10-29 21:41:17 +0000151 if (e != 1) std::cout << i+1 << ". ";
Chris Lattner4963dcf2003-10-28 22:30:37 +0000152 std::cout << PI.getExecution(i) << "\n";
153 }
154
155 std::cout << "\n===" << std::string(73, '-') << "===\n";
156 std::cout << "Function execution frequencies:\n\n";
157
Chris Lattner7a78d812003-10-28 21:08:18 +0000158 // Print out the function frequencies...
159 printf(" ## Frequency\n");
Chris Lattner4963dcf2003-10-28 22:30:37 +0000160 for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i) {
161 if (FunctionCounts[i].second == 0) {
162 printf("\n NOTE: %d function%s never executed!\n",
163 e-i, e-i-1 ? "s were" : " was");
164 break;
165 }
166
Chris Lattner3baed412003-10-31 00:06:57 +0000167 printf("%3d. %5u/%llu %s\n", i+1, FunctionCounts[i].second, TotalExecutions,
Chris Lattner7a78d812003-10-28 21:08:18 +0000168 FunctionCounts[i].first->getName().c_str());
Chris Lattner4963dcf2003-10-28 22:30:37 +0000169 }
Chris Lattner33f1ca72003-10-28 21:25:23 +0000170
Chris Lattner5e717642003-10-30 23:42:09 +0000171 std::set<Function*> FunctionsToPrint;
Chris Lattner33f1ca72003-10-28 21:25:23 +0000172
173 // If we have block count information, print out the LLVM module with
174 // frequency annotations.
175 if (PI.hasAccurateBlockCounts()) {
176 std::vector<std::pair<BasicBlock*, unsigned> > Counts;
177 PI.getBlockCounts(Counts);
Chris Lattner18884a82003-10-29 21:41:17 +0000178
179 TotalExecutions = 0;
180 for (unsigned i = 0, e = Counts.size(); i != e; ++i)
181 TotalExecutions += Counts[i].second;
182
183 // Sort by the frequency, backwards.
184 std::sort(Counts.begin(), Counts.end(),
185 PairSecondSortReverse<BasicBlock*>());
186
187 std::cout << "\n===" << std::string(73, '-') << "===\n";
188 std::cout << "Top 20 most frequently executed basic blocks:\n\n";
189
190 // Print out the function frequencies...
Chris Lattnere1ccadf2003-10-31 00:13:26 +0000191 printf(" ## %%%% \tFrequency\n");
Chris Lattner18884a82003-10-29 21:41:17 +0000192 unsigned BlocksToPrint = Counts.size();
193 if (BlocksToPrint > 20) BlocksToPrint = 20;
Chris Lattner5e717642003-10-30 23:42:09 +0000194 for (unsigned i = 0; i != BlocksToPrint; ++i) {
Chris Lattner9088de72003-10-31 00:34:05 +0000195 if (Counts[i].second == 0) break;
Chris Lattner5e717642003-10-30 23:42:09 +0000196 Function *F = Counts[i].first->getParent();
Chris Lattnere1ccadf2003-10-31 00:13:26 +0000197 printf("%3d. %5.2f%% %5u/%llu\t%s() - %s\n", i+1,
198 Counts[i].second/(double)TotalExecutions*100,
Chris Lattner3baed412003-10-31 00:06:57 +0000199 Counts[i].second, TotalExecutions,
Chris Lattner5e717642003-10-30 23:42:09 +0000200 F->getName().c_str(), Counts[i].first->getName().c_str());
201 FunctionsToPrint.insert(F);
202 }
Chris Lattner18884a82003-10-29 21:41:17 +0000203
Chris Lattner5e717642003-10-30 23:42:09 +0000204 BlockFreqs.insert(Counts.begin(), Counts.end());
205 }
206
Chris Lattner8c2730e2004-03-08 20:04:32 +0000207 if (PI.hasAccurateEdgeCounts()) {
208 std::vector<std::pair<ProfileInfoLoader::Edge, unsigned> > Counts;
209 PI.getEdgeCounts(Counts);
210 EdgeFreqs.insert(Counts.begin(), Counts.end());
211 }
212
Chris Lattnercde1cf32003-11-06 20:29:25 +0000213 if (PrintAnnotatedLLVM || PrintAllCode) {
Chris Lattner5e717642003-10-30 23:42:09 +0000214 std::cout << "\n===" << std::string(73, '-') << "===\n";
215 std::cout << "Annotated LLVM code for the module:\n\n";
Chris Lattner18884a82003-10-29 21:41:17 +0000216
Chris Lattner8c2730e2004-03-08 20:04:32 +0000217 ProfileAnnotator PA(FuncFreqs, BlockFreqs, EdgeFreqs);
Chris Lattner5e717642003-10-30 23:42:09 +0000218
Chris Lattnercde1cf32003-11-06 20:29:25 +0000219 if (FunctionsToPrint.empty() || PrintAllCode)
Chris Lattner3b7f4162003-10-31 00:20:09 +0000220 M->print(std::cout, &PA);
221 else
222 // Print just a subset of the functions...
223 for (std::set<Function*>::iterator I = FunctionsToPrint.begin(),
224 E = FunctionsToPrint.end(); I != E; ++I)
225 (*I)->print(std::cout, &PA);
Chris Lattner33f1ca72003-10-28 21:25:23 +0000226 }
227
Chris Lattner6f82d072003-10-28 19:16:35 +0000228 return 0;
229}