blob: 2a7789dcd5352e85c67c9a877c2504a2ba3cb3ec [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 Lattnerf73b4ca2004-02-19 20:32:12 +000021#include "Support/Signals.h"
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
Brian Gaeked0fde302003-11-11 22:41:34 +000026using namespace llvm;
27
Chris Lattner6f82d072003-10-28 19:16:35 +000028namespace {
29 cl::opt<std::string>
30 BytecodeFile(cl::Positional, cl::desc("<program bytecode file>"),
31 cl::Required);
32
33 cl::opt<std::string>
34 ProfileDataFile(cl::Positional, cl::desc("<llvmprof.out file>"),
35 cl::Optional, cl::init("llvmprof.out"));
Chris Lattner5e717642003-10-30 23:42:09 +000036
37 cl::opt<bool>
38 PrintAnnotatedLLVM("annotated-llvm",
39 cl::desc("Print LLVM code with frequency annotations"));
40 cl::alias PrintAnnotated2("A", cl::desc("Alias for --annotated-llvm"),
41 cl::aliasopt(PrintAnnotatedLLVM));
Chris Lattnercde1cf32003-11-06 20:29:25 +000042 cl::opt<bool>
43 PrintAllCode("print-all-code",
44 cl::desc("Print annotated code for the entire program"));
Chris Lattner6f82d072003-10-28 19:16:35 +000045}
46
Chris Lattner7a78d812003-10-28 21:08:18 +000047// PairSecondSort - A sorting predicate to sort by the second element of a pair.
48template<class T>
Chris Lattner18884a82003-10-29 21:41:17 +000049struct PairSecondSortReverse
Chris Lattner7a78d812003-10-28 21:08:18 +000050 : public std::binary_function<std::pair<T, unsigned>,
51 std::pair<T, unsigned>, bool> {
52 bool operator()(const std::pair<T, unsigned> &LHS,
53 const std::pair<T, unsigned> &RHS) const {
Chris Lattner18884a82003-10-29 21:41:17 +000054 return LHS.second > RHS.second;
Chris Lattner7a78d812003-10-28 21:08:18 +000055 }
56};
57
Chris Lattner5e717642003-10-30 23:42:09 +000058namespace {
59 class ProfileAnnotator : public AssemblyAnnotationWriter {
60 std::map<const Function *, unsigned> &FuncFreqs;
61 std::map<const BasicBlock*, unsigned> &BlockFreqs;
62 public:
63 ProfileAnnotator(std::map<const Function *, unsigned> &FF,
64 std::map<const BasicBlock*, unsigned> &BF)
65 : FuncFreqs(FF), BlockFreqs(BF) {}
66
67 virtual void emitFunctionAnnot(const Function *F, std::ostream &OS) {
68 OS << ";;; %" << F->getName() << " called " << FuncFreqs[F]
69 << " times.\n;;;\n";
70 }
71 virtual void emitBasicBlockAnnot(const BasicBlock *BB, std::ostream &OS) {
Chris Lattner36737302003-10-30 23:44:28 +000072 if (BlockFreqs.empty()) return;
Chris Lattner5e717642003-10-30 23:42:09 +000073 if (unsigned Count = BlockFreqs[BB])
74 OS << ";;; Executed " << Count << " times.\n";
75 else
76 OS << ";;; Never executed!\n";
77 }
78 };
79}
80
Chris Lattner7a78d812003-10-28 21:08:18 +000081
Chris Lattner6f82d072003-10-28 19:16:35 +000082int main(int argc, char **argv) {
83 cl::ParseCommandLineOptions(argc, argv, " llvm profile dump decoder\n");
Chris Lattnerf73b4ca2004-02-19 20:32:12 +000084 PrintStackTraceOnErrorSignal();
Chris Lattner6f82d072003-10-28 19:16:35 +000085
Chris Lattnere4367792003-10-28 20:13:07 +000086 // Read in the bytecode file...
87 std::string ErrorMessage;
Chris Lattner7a78d812003-10-28 21:08:18 +000088 Module *M = ParseBytecodeFile(BytecodeFile, &ErrorMessage);
89 if (M == 0) {
Chris Lattnere4367792003-10-28 20:13:07 +000090 std::cerr << argv[0] << ": " << BytecodeFile << ": " << ErrorMessage
91 << "\n";
92 return 1;
93 }
Chris Lattner6f82d072003-10-28 19:16:35 +000094
Chris Lattnere4367792003-10-28 20:13:07 +000095 // Read the profiling information
Chris Lattner89cf3932004-02-11 05:56:07 +000096 ProfileInfoLoader PI(argv[0], ProfileDataFile, *M);
Chris Lattner6f82d072003-10-28 19:16:35 +000097
Chris Lattner5e717642003-10-30 23:42:09 +000098 std::map<const Function *, unsigned> FuncFreqs;
99 std::map<const BasicBlock*, unsigned> BlockFreqs;
100
Chris Lattner7a78d812003-10-28 21:08:18 +0000101 // Output a report. Eventually, there will be multiple reports selectable on
102 // the command line, for now, just keep things simple.
103
104 // Emit the most frequent function table...
105 std::vector<std::pair<Function*, unsigned> > FunctionCounts;
106 PI.getFunctionCounts(FunctionCounts);
Chris Lattner5e717642003-10-30 23:42:09 +0000107 FuncFreqs.insert(FunctionCounts.begin(), FunctionCounts.end());
Chris Lattner7a78d812003-10-28 21:08:18 +0000108
109 // Sort by the frequency, backwards.
110 std::sort(FunctionCounts.begin(), FunctionCounts.end(),
Chris Lattner18884a82003-10-29 21:41:17 +0000111 PairSecondSortReverse<Function*>());
Chris Lattner7a78d812003-10-28 21:08:18 +0000112
Chris Lattner3baed412003-10-31 00:06:57 +0000113 unsigned long long TotalExecutions = 0;
Chris Lattner7a78d812003-10-28 21:08:18 +0000114 for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i)
115 TotalExecutions += FunctionCounts[i].second;
116
Chris Lattner4963dcf2003-10-28 22:30:37 +0000117 std::cout << "===" << std::string(73, '-') << "===\n"
Chris Lattner36882052003-10-28 22:53:49 +0000118 << "LLVM profiling output for execution";
119 if (PI.getNumExecutions() != 1) std::cout << "s";
120 std::cout << ":\n";
Chris Lattner4963dcf2003-10-28 22:30:37 +0000121
122 for (unsigned i = 0, e = PI.getNumExecutions(); i != e; ++i) {
123 std::cout << " ";
Chris Lattner18884a82003-10-29 21:41:17 +0000124 if (e != 1) std::cout << i+1 << ". ";
Chris Lattner4963dcf2003-10-28 22:30:37 +0000125 std::cout << PI.getExecution(i) << "\n";
126 }
127
128 std::cout << "\n===" << std::string(73, '-') << "===\n";
129 std::cout << "Function execution frequencies:\n\n";
130
Chris Lattner7a78d812003-10-28 21:08:18 +0000131 // Print out the function frequencies...
132 printf(" ## Frequency\n");
Chris Lattner4963dcf2003-10-28 22:30:37 +0000133 for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i) {
134 if (FunctionCounts[i].second == 0) {
135 printf("\n NOTE: %d function%s never executed!\n",
136 e-i, e-i-1 ? "s were" : " was");
137 break;
138 }
139
Chris Lattner3baed412003-10-31 00:06:57 +0000140 printf("%3d. %5u/%llu %s\n", i+1, FunctionCounts[i].second, TotalExecutions,
Chris Lattner7a78d812003-10-28 21:08:18 +0000141 FunctionCounts[i].first->getName().c_str());
Chris Lattner4963dcf2003-10-28 22:30:37 +0000142 }
Chris Lattner33f1ca72003-10-28 21:25:23 +0000143
Chris Lattner5e717642003-10-30 23:42:09 +0000144 std::set<Function*> FunctionsToPrint;
Chris Lattner33f1ca72003-10-28 21:25:23 +0000145
146 // If we have block count information, print out the LLVM module with
147 // frequency annotations.
148 if (PI.hasAccurateBlockCounts()) {
149 std::vector<std::pair<BasicBlock*, unsigned> > Counts;
150 PI.getBlockCounts(Counts);
Chris Lattner18884a82003-10-29 21:41:17 +0000151
152 TotalExecutions = 0;
153 for (unsigned i = 0, e = Counts.size(); i != e; ++i)
154 TotalExecutions += Counts[i].second;
155
156 // Sort by the frequency, backwards.
157 std::sort(Counts.begin(), Counts.end(),
158 PairSecondSortReverse<BasicBlock*>());
159
160 std::cout << "\n===" << std::string(73, '-') << "===\n";
161 std::cout << "Top 20 most frequently executed basic blocks:\n\n";
162
163 // Print out the function frequencies...
Chris Lattnere1ccadf2003-10-31 00:13:26 +0000164 printf(" ## %%%% \tFrequency\n");
Chris Lattner18884a82003-10-29 21:41:17 +0000165 unsigned BlocksToPrint = Counts.size();
166 if (BlocksToPrint > 20) BlocksToPrint = 20;
Chris Lattner5e717642003-10-30 23:42:09 +0000167 for (unsigned i = 0; i != BlocksToPrint; ++i) {
Chris Lattner9088de72003-10-31 00:34:05 +0000168 if (Counts[i].second == 0) break;
Chris Lattner5e717642003-10-30 23:42:09 +0000169 Function *F = Counts[i].first->getParent();
Chris Lattnere1ccadf2003-10-31 00:13:26 +0000170 printf("%3d. %5.2f%% %5u/%llu\t%s() - %s\n", i+1,
171 Counts[i].second/(double)TotalExecutions*100,
Chris Lattner3baed412003-10-31 00:06:57 +0000172 Counts[i].second, TotalExecutions,
Chris Lattner5e717642003-10-30 23:42:09 +0000173 F->getName().c_str(), Counts[i].first->getName().c_str());
174 FunctionsToPrint.insert(F);
175 }
Chris Lattner18884a82003-10-29 21:41:17 +0000176
Chris Lattner5e717642003-10-30 23:42:09 +0000177 BlockFreqs.insert(Counts.begin(), Counts.end());
178 }
179
Chris Lattnercde1cf32003-11-06 20:29:25 +0000180 if (PrintAnnotatedLLVM || PrintAllCode) {
Chris Lattner5e717642003-10-30 23:42:09 +0000181 std::cout << "\n===" << std::string(73, '-') << "===\n";
182 std::cout << "Annotated LLVM code for the module:\n\n";
Chris Lattner18884a82003-10-29 21:41:17 +0000183
Chris Lattner5e717642003-10-30 23:42:09 +0000184 ProfileAnnotator PA(FuncFreqs, BlockFreqs);
185
Chris Lattnercde1cf32003-11-06 20:29:25 +0000186 if (FunctionsToPrint.empty() || PrintAllCode)
Chris Lattner3b7f4162003-10-31 00:20:09 +0000187 M->print(std::cout, &PA);
188 else
189 // Print just a subset of the functions...
190 for (std::set<Function*>::iterator I = FunctionsToPrint.begin(),
191 E = FunctionsToPrint.end(); I != E; ++I)
192 (*I)->print(std::cout, &PA);
Chris Lattner33f1ca72003-10-28 21:25:23 +0000193 }
194
Chris Lattner6f82d072003-10-28 19:16:35 +0000195 return 0;
196}