blob: 3c9c3906ff37aa886b4720d0279d59ad72f765d9 [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 Lattner5e717642003-10-30 23:42:09 +000016#include "llvm/Module.h"
17#include "llvm/Assembly/AsmAnnotationWriter.h"
Chris Lattner89cf3932004-02-11 05:56:07 +000018#include "llvm/Analysis/ProfileInfoLoader.h"
Chris Lattner6f82d072003-10-28 19:16:35 +000019#include "llvm/Bytecode/Reader.h"
20#include "Support/CommandLine.h"
Chris Lattner7a78d812003-10-28 21:08:18 +000021#include <cstdio>
Chris Lattner33f1ca72003-10-28 21:25:23 +000022#include <map>
Chris Lattner5e717642003-10-30 23:42:09 +000023#include <set>
Chris Lattner6f82d072003-10-28 19:16:35 +000024
Brian Gaeked0fde302003-11-11 22:41:34 +000025using namespace llvm;
26
Chris Lattner6f82d072003-10-28 19:16:35 +000027namespace {
28 cl::opt<std::string>
29 BytecodeFile(cl::Positional, cl::desc("<program bytecode file>"),
30 cl::Required);
31
32 cl::opt<std::string>
33 ProfileDataFile(cl::Positional, cl::desc("<llvmprof.out file>"),
34 cl::Optional, cl::init("llvmprof.out"));
Chris Lattner5e717642003-10-30 23:42:09 +000035
36 cl::opt<bool>
37 PrintAnnotatedLLVM("annotated-llvm",
38 cl::desc("Print LLVM code with frequency annotations"));
39 cl::alias PrintAnnotated2("A", cl::desc("Alias for --annotated-llvm"),
40 cl::aliasopt(PrintAnnotatedLLVM));
Chris Lattnercde1cf32003-11-06 20:29:25 +000041 cl::opt<bool>
42 PrintAllCode("print-all-code",
43 cl::desc("Print annotated code for the entire program"));
Chris Lattner6f82d072003-10-28 19:16:35 +000044}
45
Chris Lattner7a78d812003-10-28 21:08:18 +000046// PairSecondSort - A sorting predicate to sort by the second element of a pair.
47template<class T>
Chris Lattner18884a82003-10-29 21:41:17 +000048struct PairSecondSortReverse
Chris Lattner7a78d812003-10-28 21:08:18 +000049 : public std::binary_function<std::pair<T, unsigned>,
50 std::pair<T, unsigned>, bool> {
51 bool operator()(const std::pair<T, unsigned> &LHS,
52 const std::pair<T, unsigned> &RHS) const {
Chris Lattner18884a82003-10-29 21:41:17 +000053 return LHS.second > RHS.second;
Chris Lattner7a78d812003-10-28 21:08:18 +000054 }
55};
56
Chris Lattner5e717642003-10-30 23:42:09 +000057namespace {
58 class ProfileAnnotator : public AssemblyAnnotationWriter {
59 std::map<const Function *, unsigned> &FuncFreqs;
60 std::map<const BasicBlock*, unsigned> &BlockFreqs;
61 public:
62 ProfileAnnotator(std::map<const Function *, unsigned> &FF,
63 std::map<const BasicBlock*, unsigned> &BF)
64 : FuncFreqs(FF), BlockFreqs(BF) {}
65
66 virtual void emitFunctionAnnot(const Function *F, std::ostream &OS) {
67 OS << ";;; %" << F->getName() << " called " << FuncFreqs[F]
68 << " times.\n;;;\n";
69 }
70 virtual void emitBasicBlockAnnot(const BasicBlock *BB, std::ostream &OS) {
Chris Lattner36737302003-10-30 23:44:28 +000071 if (BlockFreqs.empty()) return;
Chris Lattner5e717642003-10-30 23:42:09 +000072 if (unsigned Count = BlockFreqs[BB])
73 OS << ";;; Executed " << Count << " times.\n";
74 else
75 OS << ";;; Never executed!\n";
76 }
77 };
78}
79
Chris Lattner7a78d812003-10-28 21:08:18 +000080
Chris Lattner6f82d072003-10-28 19:16:35 +000081int main(int argc, char **argv) {
82 cl::ParseCommandLineOptions(argc, argv, " llvm profile dump decoder\n");
Chris Lattner6f82d072003-10-28 19:16:35 +000083
Chris Lattnere4367792003-10-28 20:13:07 +000084 // Read in the bytecode file...
85 std::string ErrorMessage;
Chris Lattner7a78d812003-10-28 21:08:18 +000086 Module *M = ParseBytecodeFile(BytecodeFile, &ErrorMessage);
87 if (M == 0) {
Chris Lattnere4367792003-10-28 20:13:07 +000088 std::cerr << argv[0] << ": " << BytecodeFile << ": " << ErrorMessage
89 << "\n";
90 return 1;
91 }
Chris Lattner6f82d072003-10-28 19:16:35 +000092
Chris Lattnere4367792003-10-28 20:13:07 +000093 // Read the profiling information
Chris Lattner89cf3932004-02-11 05:56:07 +000094 ProfileInfoLoader PI(argv[0], ProfileDataFile, *M);
Chris Lattner6f82d072003-10-28 19:16:35 +000095
Chris Lattner5e717642003-10-30 23:42:09 +000096 std::map<const Function *, unsigned> FuncFreqs;
97 std::map<const BasicBlock*, unsigned> BlockFreqs;
98
Chris Lattner7a78d812003-10-28 21:08:18 +000099 // Output a report. Eventually, there will be multiple reports selectable on
100 // the command line, for now, just keep things simple.
101
102 // Emit the most frequent function table...
103 std::vector<std::pair<Function*, unsigned> > FunctionCounts;
104 PI.getFunctionCounts(FunctionCounts);
Chris Lattner5e717642003-10-30 23:42:09 +0000105 FuncFreqs.insert(FunctionCounts.begin(), FunctionCounts.end());
Chris Lattner7a78d812003-10-28 21:08:18 +0000106
107 // Sort by the frequency, backwards.
108 std::sort(FunctionCounts.begin(), FunctionCounts.end(),
Chris Lattner18884a82003-10-29 21:41:17 +0000109 PairSecondSortReverse<Function*>());
Chris Lattner7a78d812003-10-28 21:08:18 +0000110
Chris Lattner3baed412003-10-31 00:06:57 +0000111 unsigned long long TotalExecutions = 0;
Chris Lattner7a78d812003-10-28 21:08:18 +0000112 for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i)
113 TotalExecutions += FunctionCounts[i].second;
114
Chris Lattner4963dcf2003-10-28 22:30:37 +0000115 std::cout << "===" << std::string(73, '-') << "===\n"
Chris Lattner36882052003-10-28 22:53:49 +0000116 << "LLVM profiling output for execution";
117 if (PI.getNumExecutions() != 1) std::cout << "s";
118 std::cout << ":\n";
Chris Lattner4963dcf2003-10-28 22:30:37 +0000119
120 for (unsigned i = 0, e = PI.getNumExecutions(); i != e; ++i) {
121 std::cout << " ";
Chris Lattner18884a82003-10-29 21:41:17 +0000122 if (e != 1) std::cout << i+1 << ". ";
Chris Lattner4963dcf2003-10-28 22:30:37 +0000123 std::cout << PI.getExecution(i) << "\n";
124 }
125
126 std::cout << "\n===" << std::string(73, '-') << "===\n";
127 std::cout << "Function execution frequencies:\n\n";
128
Chris Lattner7a78d812003-10-28 21:08:18 +0000129 // Print out the function frequencies...
130 printf(" ## Frequency\n");
Chris Lattner4963dcf2003-10-28 22:30:37 +0000131 for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i) {
132 if (FunctionCounts[i].second == 0) {
133 printf("\n NOTE: %d function%s never executed!\n",
134 e-i, e-i-1 ? "s were" : " was");
135 break;
136 }
137
Chris Lattner3baed412003-10-31 00:06:57 +0000138 printf("%3d. %5u/%llu %s\n", i+1, FunctionCounts[i].second, TotalExecutions,
Chris Lattner7a78d812003-10-28 21:08:18 +0000139 FunctionCounts[i].first->getName().c_str());
Chris Lattner4963dcf2003-10-28 22:30:37 +0000140 }
Chris Lattner33f1ca72003-10-28 21:25:23 +0000141
Chris Lattner5e717642003-10-30 23:42:09 +0000142 std::set<Function*> FunctionsToPrint;
Chris Lattner33f1ca72003-10-28 21:25:23 +0000143
144 // If we have block count information, print out the LLVM module with
145 // frequency annotations.
146 if (PI.hasAccurateBlockCounts()) {
147 std::vector<std::pair<BasicBlock*, unsigned> > Counts;
148 PI.getBlockCounts(Counts);
Chris Lattner18884a82003-10-29 21:41:17 +0000149
150 TotalExecutions = 0;
151 for (unsigned i = 0, e = Counts.size(); i != e; ++i)
152 TotalExecutions += Counts[i].second;
153
154 // Sort by the frequency, backwards.
155 std::sort(Counts.begin(), Counts.end(),
156 PairSecondSortReverse<BasicBlock*>());
157
158 std::cout << "\n===" << std::string(73, '-') << "===\n";
159 std::cout << "Top 20 most frequently executed basic blocks:\n\n";
160
161 // Print out the function frequencies...
Chris Lattnere1ccadf2003-10-31 00:13:26 +0000162 printf(" ## %%%% \tFrequency\n");
Chris Lattner18884a82003-10-29 21:41:17 +0000163 unsigned BlocksToPrint = Counts.size();
164 if (BlocksToPrint > 20) BlocksToPrint = 20;
Chris Lattner5e717642003-10-30 23:42:09 +0000165 for (unsigned i = 0; i != BlocksToPrint; ++i) {
Chris Lattner9088de72003-10-31 00:34:05 +0000166 if (Counts[i].second == 0) break;
Chris Lattner5e717642003-10-30 23:42:09 +0000167 Function *F = Counts[i].first->getParent();
Chris Lattnere1ccadf2003-10-31 00:13:26 +0000168 printf("%3d. %5.2f%% %5u/%llu\t%s() - %s\n", i+1,
169 Counts[i].second/(double)TotalExecutions*100,
Chris Lattner3baed412003-10-31 00:06:57 +0000170 Counts[i].second, TotalExecutions,
Chris Lattner5e717642003-10-30 23:42:09 +0000171 F->getName().c_str(), Counts[i].first->getName().c_str());
172 FunctionsToPrint.insert(F);
173 }
Chris Lattner18884a82003-10-29 21:41:17 +0000174
Chris Lattner5e717642003-10-30 23:42:09 +0000175 BlockFreqs.insert(Counts.begin(), Counts.end());
176 }
177
Chris Lattnercde1cf32003-11-06 20:29:25 +0000178 if (PrintAnnotatedLLVM || PrintAllCode) {
Chris Lattner5e717642003-10-30 23:42:09 +0000179 std::cout << "\n===" << std::string(73, '-') << "===\n";
180 std::cout << "Annotated LLVM code for the module:\n\n";
Chris Lattner18884a82003-10-29 21:41:17 +0000181
Chris Lattner5e717642003-10-30 23:42:09 +0000182 ProfileAnnotator PA(FuncFreqs, BlockFreqs);
183
Chris Lattnercde1cf32003-11-06 20:29:25 +0000184 if (FunctionsToPrint.empty() || PrintAllCode)
Chris Lattner3b7f4162003-10-31 00:20:09 +0000185 M->print(std::cout, &PA);
186 else
187 // Print just a subset of the functions...
188 for (std::set<Function*>::iterator I = FunctionsToPrint.begin(),
189 E = FunctionsToPrint.end(); I != E; ++I)
190 (*I)->print(std::cout, &PA);
Chris Lattner33f1ca72003-10-28 21:25:23 +0000191 }
192
Chris Lattner6f82d072003-10-28 19:16:35 +0000193 return 0;
194}