blob: 4492fd67b7196b1ddc88c0fb597488047cd938a0 [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 Lattnercde1cf32003-11-06 20:29:25 +000040 cl::opt<bool>
41 PrintAllCode("print-all-code",
42 cl::desc("Print annotated code for the entire program"));
Chris Lattner6f82d072003-10-28 19:16:35 +000043}
44
Chris Lattner7a78d812003-10-28 21:08:18 +000045// PairSecondSort - A sorting predicate to sort by the second element of a pair.
46template<class T>
Chris Lattner18884a82003-10-29 21:41:17 +000047struct PairSecondSortReverse
Chris Lattner7a78d812003-10-28 21:08:18 +000048 : public std::binary_function<std::pair<T, unsigned>,
49 std::pair<T, unsigned>, bool> {
50 bool operator()(const std::pair<T, unsigned> &LHS,
51 const std::pair<T, unsigned> &RHS) const {
Chris Lattner18884a82003-10-29 21:41:17 +000052 return LHS.second > RHS.second;
Chris Lattner7a78d812003-10-28 21:08:18 +000053 }
54};
55
Chris Lattner5e717642003-10-30 23:42:09 +000056namespace {
57 class ProfileAnnotator : public AssemblyAnnotationWriter {
58 std::map<const Function *, unsigned> &FuncFreqs;
59 std::map<const BasicBlock*, unsigned> &BlockFreqs;
60 public:
61 ProfileAnnotator(std::map<const Function *, unsigned> &FF,
62 std::map<const BasicBlock*, unsigned> &BF)
63 : FuncFreqs(FF), BlockFreqs(BF) {}
64
65 virtual void emitFunctionAnnot(const Function *F, std::ostream &OS) {
66 OS << ";;; %" << F->getName() << " called " << FuncFreqs[F]
67 << " times.\n;;;\n";
68 }
69 virtual void emitBasicBlockAnnot(const BasicBlock *BB, std::ostream &OS) {
Chris Lattner36737302003-10-30 23:44:28 +000070 if (BlockFreqs.empty()) return;
Chris Lattner5e717642003-10-30 23:42:09 +000071 if (unsigned Count = BlockFreqs[BB])
72 OS << ";;; Executed " << Count << " times.\n";
73 else
74 OS << ";;; Never executed!\n";
75 }
76 };
77}
78
Chris Lattner7a78d812003-10-28 21:08:18 +000079
Chris Lattner6f82d072003-10-28 19:16:35 +000080int main(int argc, char **argv) {
81 cl::ParseCommandLineOptions(argc, argv, " llvm profile dump decoder\n");
Chris Lattner6f82d072003-10-28 19:16:35 +000082
Chris Lattnere4367792003-10-28 20:13:07 +000083 // Read in the bytecode file...
84 std::string ErrorMessage;
Chris Lattner7a78d812003-10-28 21:08:18 +000085 Module *M = ParseBytecodeFile(BytecodeFile, &ErrorMessage);
86 if (M == 0) {
Chris Lattnere4367792003-10-28 20:13:07 +000087 std::cerr << argv[0] << ": " << BytecodeFile << ": " << ErrorMessage
88 << "\n";
89 return 1;
90 }
Chris Lattner6f82d072003-10-28 19:16:35 +000091
Chris Lattnere4367792003-10-28 20:13:07 +000092 // Read the profiling information
Chris Lattner7a78d812003-10-28 21:08:18 +000093 ProfileInfo PI(argv[0], ProfileDataFile, *M);
Chris Lattner6f82d072003-10-28 19:16:35 +000094
Chris Lattner5e717642003-10-30 23:42:09 +000095 std::map<const Function *, unsigned> FuncFreqs;
96 std::map<const BasicBlock*, unsigned> BlockFreqs;
97
Chris Lattner7a78d812003-10-28 21:08:18 +000098 // Output a report. Eventually, there will be multiple reports selectable on
99 // the command line, for now, just keep things simple.
100
101 // Emit the most frequent function table...
102 std::vector<std::pair<Function*, unsigned> > FunctionCounts;
103 PI.getFunctionCounts(FunctionCounts);
Chris Lattner5e717642003-10-30 23:42:09 +0000104 FuncFreqs.insert(FunctionCounts.begin(), FunctionCounts.end());
Chris Lattner7a78d812003-10-28 21:08:18 +0000105
106 // Sort by the frequency, backwards.
107 std::sort(FunctionCounts.begin(), FunctionCounts.end(),
Chris Lattner18884a82003-10-29 21:41:17 +0000108 PairSecondSortReverse<Function*>());
Chris Lattner7a78d812003-10-28 21:08:18 +0000109
Chris Lattner3baed412003-10-31 00:06:57 +0000110 unsigned long long TotalExecutions = 0;
Chris Lattner7a78d812003-10-28 21:08:18 +0000111 for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i)
112 TotalExecutions += FunctionCounts[i].second;
113
Chris Lattner4963dcf2003-10-28 22:30:37 +0000114 std::cout << "===" << std::string(73, '-') << "===\n"
Chris Lattner36882052003-10-28 22:53:49 +0000115 << "LLVM profiling output for execution";
116 if (PI.getNumExecutions() != 1) std::cout << "s";
117 std::cout << ":\n";
Chris Lattner4963dcf2003-10-28 22:30:37 +0000118
119 for (unsigned i = 0, e = PI.getNumExecutions(); i != e; ++i) {
120 std::cout << " ";
Chris Lattner18884a82003-10-29 21:41:17 +0000121 if (e != 1) std::cout << i+1 << ". ";
Chris Lattner4963dcf2003-10-28 22:30:37 +0000122 std::cout << PI.getExecution(i) << "\n";
123 }
124
125 std::cout << "\n===" << std::string(73, '-') << "===\n";
126 std::cout << "Function execution frequencies:\n\n";
127
Chris Lattner7a78d812003-10-28 21:08:18 +0000128 // Print out the function frequencies...
129 printf(" ## Frequency\n");
Chris Lattner4963dcf2003-10-28 22:30:37 +0000130 for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i) {
131 if (FunctionCounts[i].second == 0) {
132 printf("\n NOTE: %d function%s never executed!\n",
133 e-i, e-i-1 ? "s were" : " was");
134 break;
135 }
136
Chris Lattner3baed412003-10-31 00:06:57 +0000137 printf("%3d. %5u/%llu %s\n", i+1, FunctionCounts[i].second, TotalExecutions,
Chris Lattner7a78d812003-10-28 21:08:18 +0000138 FunctionCounts[i].first->getName().c_str());
Chris Lattner4963dcf2003-10-28 22:30:37 +0000139 }
Chris Lattner33f1ca72003-10-28 21:25:23 +0000140
Chris Lattner5e717642003-10-30 23:42:09 +0000141 std::set<Function*> FunctionsToPrint;
Chris Lattner33f1ca72003-10-28 21:25:23 +0000142
143 // If we have block count information, print out the LLVM module with
144 // frequency annotations.
145 if (PI.hasAccurateBlockCounts()) {
146 std::vector<std::pair<BasicBlock*, unsigned> > Counts;
147 PI.getBlockCounts(Counts);
Chris Lattner18884a82003-10-29 21:41:17 +0000148
149 TotalExecutions = 0;
150 for (unsigned i = 0, e = Counts.size(); i != e; ++i)
151 TotalExecutions += Counts[i].second;
152
153 // Sort by the frequency, backwards.
154 std::sort(Counts.begin(), Counts.end(),
155 PairSecondSortReverse<BasicBlock*>());
156
157 std::cout << "\n===" << std::string(73, '-') << "===\n";
158 std::cout << "Top 20 most frequently executed basic blocks:\n\n";
159
160 // Print out the function frequencies...
Chris Lattnere1ccadf2003-10-31 00:13:26 +0000161 printf(" ## %%%% \tFrequency\n");
Chris Lattner18884a82003-10-29 21:41:17 +0000162 unsigned BlocksToPrint = Counts.size();
163 if (BlocksToPrint > 20) BlocksToPrint = 20;
Chris Lattner5e717642003-10-30 23:42:09 +0000164 for (unsigned i = 0; i != BlocksToPrint; ++i) {
Chris Lattner9088de72003-10-31 00:34:05 +0000165 if (Counts[i].second == 0) break;
Chris Lattner5e717642003-10-30 23:42:09 +0000166 Function *F = Counts[i].first->getParent();
Chris Lattnere1ccadf2003-10-31 00:13:26 +0000167 printf("%3d. %5.2f%% %5u/%llu\t%s() - %s\n", i+1,
168 Counts[i].second/(double)TotalExecutions*100,
Chris Lattner3baed412003-10-31 00:06:57 +0000169 Counts[i].second, TotalExecutions,
Chris Lattner5e717642003-10-30 23:42:09 +0000170 F->getName().c_str(), Counts[i].first->getName().c_str());
171 FunctionsToPrint.insert(F);
172 }
Chris Lattner18884a82003-10-29 21:41:17 +0000173
Chris Lattner5e717642003-10-30 23:42:09 +0000174 BlockFreqs.insert(Counts.begin(), Counts.end());
175 }
176
Chris Lattnercde1cf32003-11-06 20:29:25 +0000177 if (PrintAnnotatedLLVM || PrintAllCode) {
Chris Lattner5e717642003-10-30 23:42:09 +0000178 std::cout << "\n===" << std::string(73, '-') << "===\n";
179 std::cout << "Annotated LLVM code for the module:\n\n";
Chris Lattner18884a82003-10-29 21:41:17 +0000180
Chris Lattner5e717642003-10-30 23:42:09 +0000181 ProfileAnnotator PA(FuncFreqs, BlockFreqs);
182
Chris Lattnercde1cf32003-11-06 20:29:25 +0000183 if (FunctionsToPrint.empty() || PrintAllCode)
Chris Lattner3b7f4162003-10-31 00:20:09 +0000184 M->print(std::cout, &PA);
185 else
186 // Print just a subset of the functions...
187 for (std::set<Function*>::iterator I = FunctionsToPrint.begin(),
188 E = FunctionsToPrint.end(); I != E; ++I)
189 (*I)->print(std::cout, &PA);
Chris Lattner33f1ca72003-10-28 21:25:23 +0000190 }
191
Chris Lattner6f82d072003-10-28 19:16:35 +0000192 return 0;
193}