blob: 5cf65b445c71fcca25ffb569d908ac3f972119a4 [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 Lattnerbed85ff2004-05-27 05:41:36 +000022#include "llvm/System/Signals.h"
Chris Lattner7a78d812003-10-28 21:08:18 +000023#include <cstdio>
Reid Spencer86f42bd2004-07-04 12:20:55 +000024#include <iostream>
Chris Lattner33f1ca72003-10-28 21:25:23 +000025#include <map>
Chris Lattner5e717642003-10-30 23:42:09 +000026#include <set>
Chris Lattner6f82d072003-10-28 19:16:35 +000027
Brian Gaeked0fde302003-11-11 22:41:34 +000028using namespace llvm;
29
Chris Lattner6f82d072003-10-28 19:16:35 +000030namespace {
Misha Brukman3da94ae2005-04-22 00:00:37 +000031 cl::opt<std::string>
Chris Lattner6f82d072003-10-28 19:16:35 +000032 BytecodeFile(cl::Positional, cl::desc("<program bytecode file>"),
33 cl::Required);
34
Misha Brukman3da94ae2005-04-22 00:00:37 +000035 cl::opt<std::string>
Chris Lattner6f82d072003-10-28 19:16:35 +000036 ProfileDataFile(cl::Positional, cl::desc("<llvmprof.out file>"),
37 cl::Optional, cl::init("llvmprof.out"));
Chris Lattner5e717642003-10-30 23:42:09 +000038
39 cl::opt<bool>
40 PrintAnnotatedLLVM("annotated-llvm",
41 cl::desc("Print LLVM code with frequency annotations"));
42 cl::alias PrintAnnotated2("A", cl::desc("Alias for --annotated-llvm"),
43 cl::aliasopt(PrintAnnotatedLLVM));
Chris Lattnercde1cf32003-11-06 20:29:25 +000044 cl::opt<bool>
45 PrintAllCode("print-all-code",
46 cl::desc("Print annotated code for the entire program"));
Chris Lattner6f82d072003-10-28 19:16:35 +000047}
48
Chris Lattner7a78d812003-10-28 21:08:18 +000049// PairSecondSort - A sorting predicate to sort by the second element of a pair.
50template<class T>
Chris Lattner18884a82003-10-29 21:41:17 +000051struct PairSecondSortReverse
Chris Lattner7a78d812003-10-28 21:08:18 +000052 : public std::binary_function<std::pair<T, unsigned>,
53 std::pair<T, unsigned>, bool> {
54 bool operator()(const std::pair<T, unsigned> &LHS,
55 const std::pair<T, unsigned> &RHS) const {
Chris Lattner18884a82003-10-29 21:41:17 +000056 return LHS.second > RHS.second;
Chris Lattner7a78d812003-10-28 21:08:18 +000057 }
58};
59
Chris Lattner5e717642003-10-30 23:42:09 +000060namespace {
61 class ProfileAnnotator : public AssemblyAnnotationWriter {
62 std::map<const Function *, unsigned> &FuncFreqs;
63 std::map<const BasicBlock*, unsigned> &BlockFreqs;
Chris Lattner8c2730e2004-03-08 20:04:32 +000064 std::map<ProfileInfoLoader::Edge, unsigned> &EdgeFreqs;
Chris Lattner5e717642003-10-30 23:42:09 +000065 public:
66 ProfileAnnotator(std::map<const Function *, unsigned> &FF,
Chris Lattner8c2730e2004-03-08 20:04:32 +000067 std::map<const BasicBlock*, unsigned> &BF,
68 std::map<ProfileInfoLoader::Edge, unsigned> &EF)
69 : FuncFreqs(FF), BlockFreqs(BF), EdgeFreqs(EF) {}
Chris Lattner5e717642003-10-30 23:42:09 +000070
71 virtual void emitFunctionAnnot(const Function *F, std::ostream &OS) {
72 OS << ";;; %" << F->getName() << " called " << FuncFreqs[F]
73 << " times.\n;;;\n";
74 }
Chris Lattner8c2730e2004-03-08 20:04:32 +000075 virtual void emitBasicBlockStartAnnot(const BasicBlock *BB,
76 std::ostream &OS) {
Chris Lattner36737302003-10-30 23:44:28 +000077 if (BlockFreqs.empty()) return;
Chris Lattner5e717642003-10-30 23:42:09 +000078 if (unsigned Count = BlockFreqs[BB])
Chris Lattner8c2730e2004-03-08 20:04:32 +000079 OS << "\t;;; Basic block executed " << Count << " times.\n";
Chris Lattner5e717642003-10-30 23:42:09 +000080 else
Chris Lattner8c2730e2004-03-08 20:04:32 +000081 OS << "\t;;; Never executed!\n";
82 }
83
84 virtual void emitBasicBlockEndAnnot(const BasicBlock *BB, std::ostream &OS){
85 if (EdgeFreqs.empty()) return;
86
87 // Figure out how many times each successor executed.
88 std::vector<std::pair<const BasicBlock*, unsigned> > SuccCounts;
89 const TerminatorInst *TI = BB->getTerminator();
Misha Brukman3da94ae2005-04-22 00:00:37 +000090
Chris Lattner8c2730e2004-03-08 20:04:32 +000091 std::map<ProfileInfoLoader::Edge, unsigned>::iterator I =
92 EdgeFreqs.lower_bound(std::make_pair(const_cast<BasicBlock*>(BB), 0U));
93 for (; I != EdgeFreqs.end() && I->first.first == BB; ++I)
94 if (I->second)
95 SuccCounts.push_back(std::make_pair(TI->getSuccessor(I->first.second),
96 I->second));
97 if (!SuccCounts.empty()) {
98 OS << "\t;;; Out-edge counts:";
99 for (unsigned i = 0, e = SuccCounts.size(); i != e; ++i)
100 OS << " [" << SuccCounts[i].second << " -> "
101 << SuccCounts[i].first->getName() << "]";
102 OS << "\n";
103 }
Chris Lattner5e717642003-10-30 23:42:09 +0000104 }
105 };
106}
107
Chris Lattner7a78d812003-10-28 21:08:18 +0000108
Chris Lattner6f82d072003-10-28 19:16:35 +0000109int main(int argc, char **argv) {
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000110 try {
111 cl::ParseCommandLineOptions(argc, argv, " llvm profile dump decoder\n");
112 sys::PrintStackTraceOnErrorSignal();
Chris Lattner6f82d072003-10-28 19:16:35 +0000113
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000114 // Read in the bytecode file...
115 std::string ErrorMessage;
116 Module *M = ParseBytecodeFile(BytecodeFile, &ErrorMessage);
117 if (M == 0) {
118 std::cerr << argv[0] << ": " << BytecodeFile << ": " << ErrorMessage
119 << "\n";
120 return 1;
Chris Lattner4963dcf2003-10-28 22:30:37 +0000121 }
122
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000123 // Read the profiling information
124 ProfileInfoLoader PI(argv[0], ProfileDataFile, *M);
Chris Lattner33f1ca72003-10-28 21:25:23 +0000125
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000126 std::map<const Function *, unsigned> FuncFreqs;
127 std::map<const BasicBlock*, unsigned> BlockFreqs;
128 std::map<ProfileInfoLoader::Edge, unsigned> EdgeFreqs;
Chris Lattner33f1ca72003-10-28 21:25:23 +0000129
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000130 // Output a report. Eventually, there will be multiple reports selectable on
131 // the command line, for now, just keep things simple.
Chris Lattner18884a82003-10-29 21:41:17 +0000132
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000133 // Emit the most frequent function table...
134 std::vector<std::pair<Function*, unsigned> > FunctionCounts;
135 PI.getFunctionCounts(FunctionCounts);
136 FuncFreqs.insert(FunctionCounts.begin(), FunctionCounts.end());
Chris Lattner18884a82003-10-29 21:41:17 +0000137
138 // Sort by the frequency, backwards.
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000139 std::sort(FunctionCounts.begin(), FunctionCounts.end(),
140 PairSecondSortReverse<Function*>());
141
142 unsigned long long TotalExecutions = 0;
143 for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i)
144 TotalExecutions += FunctionCounts[i].second;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000145
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000146 std::cout << "===" << std::string(73, '-') << "===\n"
147 << "LLVM profiling output for execution";
148 if (PI.getNumExecutions() != 1) std::cout << "s";
149 std::cout << ":\n";
Misha Brukman3da94ae2005-04-22 00:00:37 +0000150
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000151 for (unsigned i = 0, e = PI.getNumExecutions(); i != e; ++i) {
152 std::cout << " ";
153 if (e != 1) std::cout << i+1 << ". ";
154 std::cout << PI.getExecution(i) << "\n";
155 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000156
Chris Lattner18884a82003-10-29 21:41:17 +0000157 std::cout << "\n===" << std::string(73, '-') << "===\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000158 std::cout << "Function execution frequencies:\n\n";
Chris Lattner18884a82003-10-29 21:41:17 +0000159
160 // Print out the function frequencies...
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000161 printf(" ## Frequency\n");
162 for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i) {
163 if (FunctionCounts[i].second == 0) {
164 printf("\n NOTE: %d function%s never executed!\n",
165 e-i, e-i-1 ? "s were" : " was");
166 break;
167 }
168
169 printf("%3d. %5u/%llu %s\n", i+1, FunctionCounts[i].second, TotalExecutions,
170 FunctionCounts[i].first->getName().c_str());
Chris Lattner5e717642003-10-30 23:42:09 +0000171 }
Chris Lattner18884a82003-10-29 21:41:17 +0000172
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000173 std::set<Function*> FunctionsToPrint;
Chris Lattner8c2730e2004-03-08 20:04:32 +0000174
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000175 // If we have block count information, print out the LLVM module with
176 // frequency annotations.
177 if (PI.hasAccurateBlockCounts()) {
178 std::vector<std::pair<BasicBlock*, unsigned> > Counts;
179 PI.getBlockCounts(Counts);
180
181 TotalExecutions = 0;
182 for (unsigned i = 0, e = Counts.size(); i != e; ++i)
183 TotalExecutions += Counts[i].second;
184
185 // Sort by the frequency, backwards.
186 std::sort(Counts.begin(), Counts.end(),
187 PairSecondSortReverse<BasicBlock*>());
Misha Brukman3da94ae2005-04-22 00:00:37 +0000188
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000189 std::cout << "\n===" << std::string(73, '-') << "===\n";
190 std::cout << "Top 20 most frequently executed basic blocks:\n\n";
191
192 // Print out the function frequencies...
193 printf(" ## %%%% \tFrequency\n");
194 unsigned BlocksToPrint = Counts.size();
195 if (BlocksToPrint > 20) BlocksToPrint = 20;
196 for (unsigned i = 0; i != BlocksToPrint; ++i) {
197 if (Counts[i].second == 0) break;
198 Function *F = Counts[i].first->getParent();
199 printf("%3d. %5.2f%% %5u/%llu\t%s() - %s\n", i+1,
200 Counts[i].second/(double)TotalExecutions*100,
201 Counts[i].second, TotalExecutions,
202 F->getName().c_str(), Counts[i].first->getName().c_str());
203 FunctionsToPrint.insert(F);
204 }
205
206 BlockFreqs.insert(Counts.begin(), Counts.end());
207 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000208
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000209 if (PI.hasAccurateEdgeCounts()) {
210 std::vector<std::pair<ProfileInfoLoader::Edge, unsigned> > Counts;
211 PI.getEdgeCounts(Counts);
212 EdgeFreqs.insert(Counts.begin(), Counts.end());
213 }
Chris Lattner5e717642003-10-30 23:42:09 +0000214
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000215 if (PrintAnnotatedLLVM || PrintAllCode) {
216 std::cout << "\n===" << std::string(73, '-') << "===\n";
217 std::cout << "Annotated LLVM code for the module:\n\n";
Misha Brukman3da94ae2005-04-22 00:00:37 +0000218
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000219 ProfileAnnotator PA(FuncFreqs, BlockFreqs, EdgeFreqs);
220
221 if (FunctionsToPrint.empty() || PrintAllCode)
222 M->print(std::cout, &PA);
223 else
224 // Print just a subset of the functions...
225 for (std::set<Function*>::iterator I = FunctionsToPrint.begin(),
226 E = FunctionsToPrint.end(); I != E; ++I)
227 (*I)->print(std::cout, &PA);
228 }
229
230 return 0;
231 } catch (const std::string& msg) {
232 std::cerr << argv[0] << ": " << msg << "\n";
233 } catch (...) {
234 std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
Chris Lattner33f1ca72003-10-28 21:25:23 +0000235 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000236 return 1;
Chris Lattner6f82d072003-10-28 19:16:35 +0000237}