blob: 80eb36bd63d80df72def9bf2ea806bbde588e796 [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 Lattnere4367792003-10-28 20:13:07 +000016#include "ProfileInfo.h"
Chris Lattner5e717642003-10-30 23:42:09 +000017#include "llvm/Module.h"
18#include "llvm/Assembly/AsmAnnotationWriter.h"
Chris Lattner6f82d072003-10-28 19:16:35 +000019#include "llvm/Bytecode/Reader.h"
20#include "Support/CommandLine.h"
Chris Lattnere4367792003-10-28 20:13:07 +000021#include <iostream>
Chris Lattner7a78d812003-10-28 21:08:18 +000022#include <cstdio>
Chris Lattner33f1ca72003-10-28 21:25:23 +000023#include <map>
Chris Lattner5e717642003-10-30 23:42:09 +000024#include <set>
Chris Lattner6f82d072003-10-28 19:16:35 +000025
26namespace {
27 cl::opt<std::string>
28 BytecodeFile(cl::Positional, cl::desc("<program bytecode file>"),
29 cl::Required);
30
31 cl::opt<std::string>
32 ProfileDataFile(cl::Positional, cl::desc("<llvmprof.out file>"),
33 cl::Optional, cl::init("llvmprof.out"));
Chris Lattner5e717642003-10-30 23:42:09 +000034
35 cl::opt<bool>
36 PrintAnnotatedLLVM("annotated-llvm",
37 cl::desc("Print LLVM code with frequency annotations"));
38 cl::alias PrintAnnotated2("A", cl::desc("Alias for --annotated-llvm"),
39 cl::aliasopt(PrintAnnotatedLLVM));
Chris Lattner6f82d072003-10-28 19:16:35 +000040}
41
Chris Lattner7a78d812003-10-28 21:08:18 +000042// PairSecondSort - A sorting predicate to sort by the second element of a pair.
43template<class T>
Chris Lattner18884a82003-10-29 21:41:17 +000044struct PairSecondSortReverse
Chris Lattner7a78d812003-10-28 21:08:18 +000045 : public std::binary_function<std::pair<T, unsigned>,
46 std::pair<T, unsigned>, bool> {
47 bool operator()(const std::pair<T, unsigned> &LHS,
48 const std::pair<T, unsigned> &RHS) const {
Chris Lattner18884a82003-10-29 21:41:17 +000049 return LHS.second > RHS.second;
Chris Lattner7a78d812003-10-28 21:08:18 +000050 }
51};
52
Chris Lattner5e717642003-10-30 23:42:09 +000053namespace {
54 class ProfileAnnotator : public AssemblyAnnotationWriter {
55 std::map<const Function *, unsigned> &FuncFreqs;
56 std::map<const BasicBlock*, unsigned> &BlockFreqs;
57 public:
58 ProfileAnnotator(std::map<const Function *, unsigned> &FF,
59 std::map<const BasicBlock*, unsigned> &BF)
60 : FuncFreqs(FF), BlockFreqs(BF) {}
61
62 virtual void emitFunctionAnnot(const Function *F, std::ostream &OS) {
63 OS << ";;; %" << F->getName() << " called " << FuncFreqs[F]
64 << " times.\n;;;\n";
65 }
66 virtual void emitBasicBlockAnnot(const BasicBlock *BB, std::ostream &OS) {
Chris Lattner36737302003-10-30 23:44:28 +000067 if (BlockFreqs.empty()) return;
Chris Lattner5e717642003-10-30 23:42:09 +000068 if (unsigned Count = BlockFreqs[BB])
69 OS << ";;; Executed " << Count << " times.\n";
70 else
71 OS << ";;; Never executed!\n";
72 }
73 };
74}
75
Chris Lattner7a78d812003-10-28 21:08:18 +000076
Chris Lattner6f82d072003-10-28 19:16:35 +000077int main(int argc, char **argv) {
78 cl::ParseCommandLineOptions(argc, argv, " llvm profile dump decoder\n");
Chris Lattner6f82d072003-10-28 19:16:35 +000079
Chris Lattnere4367792003-10-28 20:13:07 +000080 // Read in the bytecode file...
81 std::string ErrorMessage;
Chris Lattner7a78d812003-10-28 21:08:18 +000082 Module *M = ParseBytecodeFile(BytecodeFile, &ErrorMessage);
83 if (M == 0) {
Chris Lattnere4367792003-10-28 20:13:07 +000084 std::cerr << argv[0] << ": " << BytecodeFile << ": " << ErrorMessage
85 << "\n";
86 return 1;
87 }
Chris Lattner6f82d072003-10-28 19:16:35 +000088
Chris Lattnere4367792003-10-28 20:13:07 +000089 // Read the profiling information
Chris Lattner7a78d812003-10-28 21:08:18 +000090 ProfileInfo PI(argv[0], ProfileDataFile, *M);
Chris Lattner6f82d072003-10-28 19:16:35 +000091
Chris Lattner5e717642003-10-30 23:42:09 +000092 std::map<const Function *, unsigned> FuncFreqs;
93 std::map<const BasicBlock*, unsigned> BlockFreqs;
94
Chris Lattner7a78d812003-10-28 21:08:18 +000095 // Output a report. Eventually, there will be multiple reports selectable on
96 // the command line, for now, just keep things simple.
97
98 // Emit the most frequent function table...
99 std::vector<std::pair<Function*, unsigned> > FunctionCounts;
100 PI.getFunctionCounts(FunctionCounts);
Chris Lattner5e717642003-10-30 23:42:09 +0000101 FuncFreqs.insert(FunctionCounts.begin(), FunctionCounts.end());
Chris Lattner7a78d812003-10-28 21:08:18 +0000102
103 // Sort by the frequency, backwards.
104 std::sort(FunctionCounts.begin(), FunctionCounts.end(),
Chris Lattner18884a82003-10-29 21:41:17 +0000105 PairSecondSortReverse<Function*>());
Chris Lattner7a78d812003-10-28 21:08:18 +0000106
Chris Lattner3baed412003-10-31 00:06:57 +0000107 unsigned long long TotalExecutions = 0;
Chris Lattner7a78d812003-10-28 21:08:18 +0000108 for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i)
109 TotalExecutions += FunctionCounts[i].second;
110
Chris Lattner4963dcf2003-10-28 22:30:37 +0000111 std::cout << "===" << std::string(73, '-') << "===\n"
Chris Lattner36882052003-10-28 22:53:49 +0000112 << "LLVM profiling output for execution";
113 if (PI.getNumExecutions() != 1) std::cout << "s";
114 std::cout << ":\n";
Chris Lattner4963dcf2003-10-28 22:30:37 +0000115
116 for (unsigned i = 0, e = PI.getNumExecutions(); i != e; ++i) {
117 std::cout << " ";
Chris Lattner18884a82003-10-29 21:41:17 +0000118 if (e != 1) std::cout << i+1 << ". ";
Chris Lattner4963dcf2003-10-28 22:30:37 +0000119 std::cout << PI.getExecution(i) << "\n";
120 }
121
122 std::cout << "\n===" << std::string(73, '-') << "===\n";
123 std::cout << "Function execution frequencies:\n\n";
124
Chris Lattner7a78d812003-10-28 21:08:18 +0000125 // Print out the function frequencies...
126 printf(" ## Frequency\n");
Chris Lattner4963dcf2003-10-28 22:30:37 +0000127 for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i) {
128 if (FunctionCounts[i].second == 0) {
129 printf("\n NOTE: %d function%s never executed!\n",
130 e-i, e-i-1 ? "s were" : " was");
131 break;
132 }
133
Chris Lattner3baed412003-10-31 00:06:57 +0000134 printf("%3d. %5u/%llu %s\n", i+1, FunctionCounts[i].second, TotalExecutions,
Chris Lattner7a78d812003-10-28 21:08:18 +0000135 FunctionCounts[i].first->getName().c_str());
Chris Lattner4963dcf2003-10-28 22:30:37 +0000136 }
Chris Lattner33f1ca72003-10-28 21:25:23 +0000137
Chris Lattner5e717642003-10-30 23:42:09 +0000138 std::set<Function*> FunctionsToPrint;
Chris Lattner33f1ca72003-10-28 21:25:23 +0000139
140 // If we have block count information, print out the LLVM module with
141 // frequency annotations.
142 if (PI.hasAccurateBlockCounts()) {
143 std::vector<std::pair<BasicBlock*, unsigned> > Counts;
144 PI.getBlockCounts(Counts);
Chris Lattner18884a82003-10-29 21:41:17 +0000145
146 TotalExecutions = 0;
147 for (unsigned i = 0, e = Counts.size(); i != e; ++i)
148 TotalExecutions += Counts[i].second;
149
150 // Sort by the frequency, backwards.
151 std::sort(Counts.begin(), Counts.end(),
152 PairSecondSortReverse<BasicBlock*>());
153
154 std::cout << "\n===" << std::string(73, '-') << "===\n";
155 std::cout << "Top 20 most frequently executed basic blocks:\n\n";
156
157 // Print out the function frequencies...
Chris Lattnere1ccadf2003-10-31 00:13:26 +0000158 printf(" ## %%%% \tFrequency\n");
Chris Lattner18884a82003-10-29 21:41:17 +0000159 unsigned BlocksToPrint = Counts.size();
160 if (BlocksToPrint > 20) BlocksToPrint = 20;
Chris Lattner5e717642003-10-30 23:42:09 +0000161 for (unsigned i = 0; i != BlocksToPrint; ++i) {
162 Function *F = Counts[i].first->getParent();
Chris Lattnere1ccadf2003-10-31 00:13:26 +0000163 printf("%3d. %5.2f%% %5u/%llu\t%s() - %s\n", i+1,
164 Counts[i].second/(double)TotalExecutions*100,
Chris Lattner3baed412003-10-31 00:06:57 +0000165 Counts[i].second, TotalExecutions,
Chris Lattner5e717642003-10-30 23:42:09 +0000166 F->getName().c_str(), Counts[i].first->getName().c_str());
167 FunctionsToPrint.insert(F);
168 }
Chris Lattner18884a82003-10-29 21:41:17 +0000169
Chris Lattner5e717642003-10-30 23:42:09 +0000170 BlockFreqs.insert(Counts.begin(), Counts.end());
171 }
172
173 if (PrintAnnotatedLLVM) {
174 std::cout << "\n===" << std::string(73, '-') << "===\n";
175 std::cout << "Annotated LLVM code for the module:\n\n";
Chris Lattner18884a82003-10-29 21:41:17 +0000176
Chris Lattner5e717642003-10-30 23:42:09 +0000177 ProfileAnnotator PA(FuncFreqs, BlockFreqs);
178
Chris Lattner3b7f4162003-10-31 00:20:09 +0000179 if (FunctionsToPrint.empty())
180 M->print(std::cout, &PA);
181 else
182 // Print just a subset of the functions...
183 for (std::set<Function*>::iterator I = FunctionsToPrint.begin(),
184 E = FunctionsToPrint.end(); I != E; ++I)
185 (*I)->print(std::cout, &PA);
Chris Lattner33f1ca72003-10-28 21:25:23 +0000186 }
187
Chris Lattner6f82d072003-10-28 19:16:35 +0000188 return 0;
189}