1 //===- llvm-prof.cpp - Read in and process llvmprof.out data files --------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
16 #include "llvm/InstrTypes.h"
17 #include "llvm/Module.h"
18 #include "llvm/Assembly/AsmAnnotationWriter.h"
19 #include "llvm/Analysis/ProfileInfoLoader.h"
20 #include "llvm/Bytecode/Reader.h"
21 #include "Support/CommandLine.h"
22 #include "llvm/System/Signals.h"
31 BytecodeFile(cl::Positional, cl::desc("<program bytecode file>"),
35 ProfileDataFile(cl::Positional, cl::desc("<llvmprof.out file>"),
36 cl::Optional, cl::init("llvmprof.out"));
39 PrintAnnotatedLLVM("annotated-llvm",
40 cl::desc("Print LLVM code with frequency annotations"));
41 cl::alias PrintAnnotated2("A", cl::desc("Alias for --annotated-llvm"),
42 cl::aliasopt(PrintAnnotatedLLVM));
44 PrintAllCode("print-all-code",
45 cl::desc("Print annotated code for the entire program"));
48 // PairSecondSort - A sorting predicate to sort by the second element of a pair.
50 struct PairSecondSortReverse
51 : public std::binary_function<std::pair<T, unsigned>,
52 std::pair<T, unsigned>, bool> {
53 bool operator()(const std::pair<T, unsigned> &LHS,
54 const std::pair<T, unsigned> &RHS) const {
55 return LHS.second > RHS.second;
60 class ProfileAnnotator : public AssemblyAnnotationWriter {
61 std::map<const Function *, unsigned> &FuncFreqs;
62 std::map<const BasicBlock*, unsigned> &BlockFreqs;
63 std::map<ProfileInfoLoader::Edge, unsigned> &EdgeFreqs;
65 ProfileAnnotator(std::map<const Function *, unsigned> &FF,
66 std::map<const BasicBlock*, unsigned> &BF,
67 std::map<ProfileInfoLoader::Edge, unsigned> &EF)
68 : FuncFreqs(FF), BlockFreqs(BF), EdgeFreqs(EF) {}
70 virtual void emitFunctionAnnot(const Function *F, std::ostream &OS) {
71 OS << ";;; %" << F->getName() << " called " << FuncFreqs[F]
74 virtual void emitBasicBlockStartAnnot(const BasicBlock *BB,
76 if (BlockFreqs.empty()) return;
77 if (unsigned Count = BlockFreqs[BB])
78 OS << "\t;;; Basic block executed " << Count << " times.\n";
80 OS << "\t;;; Never executed!\n";
83 virtual void emitBasicBlockEndAnnot(const BasicBlock *BB, std::ostream &OS){
84 if (EdgeFreqs.empty()) return;
86 // Figure out how many times each successor executed.
87 std::vector<std::pair<const BasicBlock*, unsigned> > SuccCounts;
88 const TerminatorInst *TI = BB->getTerminator();
90 std::map<ProfileInfoLoader::Edge, unsigned>::iterator I =
91 EdgeFreqs.lower_bound(std::make_pair(const_cast<BasicBlock*>(BB), 0U));
92 for (; I != EdgeFreqs.end() && I->first.first == BB; ++I)
94 SuccCounts.push_back(std::make_pair(TI->getSuccessor(I->first.second),
96 if (!SuccCounts.empty()) {
97 OS << "\t;;; Out-edge counts:";
98 for (unsigned i = 0, e = SuccCounts.size(); i != e; ++i)
99 OS << " [" << SuccCounts[i].second << " -> "
100 << SuccCounts[i].first->getName() << "]";
108 int main(int argc, char **argv) {
109 cl::ParseCommandLineOptions(argc, argv, " llvm profile dump decoder\n");
110 PrintStackTraceOnErrorSignal();
112 // Read in the bytecode file...
113 std::string ErrorMessage;
114 Module *M = ParseBytecodeFile(BytecodeFile, &ErrorMessage);
116 std::cerr << argv[0] << ": " << BytecodeFile << ": " << ErrorMessage
121 // Read the profiling information
122 ProfileInfoLoader PI(argv[0], ProfileDataFile, *M);
124 std::map<const Function *, unsigned> FuncFreqs;
125 std::map<const BasicBlock*, unsigned> BlockFreqs;
126 std::map<ProfileInfoLoader::Edge, unsigned> EdgeFreqs;
128 // Output a report. Eventually, there will be multiple reports selectable on
129 // the command line, for now, just keep things simple.
131 // Emit the most frequent function table...
132 std::vector<std::pair<Function*, unsigned> > FunctionCounts;
133 PI.getFunctionCounts(FunctionCounts);
134 FuncFreqs.insert(FunctionCounts.begin(), FunctionCounts.end());
136 // Sort by the frequency, backwards.
137 std::sort(FunctionCounts.begin(), FunctionCounts.end(),
138 PairSecondSortReverse<Function*>());
140 unsigned long long TotalExecutions = 0;
141 for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i)
142 TotalExecutions += FunctionCounts[i].second;
144 std::cout << "===" << std::string(73, '-') << "===\n"
145 << "LLVM profiling output for execution";
146 if (PI.getNumExecutions() != 1) std::cout << "s";
149 for (unsigned i = 0, e = PI.getNumExecutions(); i != e; ++i) {
151 if (e != 1) std::cout << i+1 << ". ";
152 std::cout << PI.getExecution(i) << "\n";
155 std::cout << "\n===" << std::string(73, '-') << "===\n";
156 std::cout << "Function execution frequencies:\n\n";
158 // Print out the function frequencies...
159 printf(" ## Frequency\n");
160 for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i) {
161 if (FunctionCounts[i].second == 0) {
162 printf("\n NOTE: %d function%s never executed!\n",
163 e-i, e-i-1 ? "s were" : " was");
167 printf("%3d. %5u/%llu %s\n", i+1, FunctionCounts[i].second, TotalExecutions,
168 FunctionCounts[i].first->getName().c_str());
171 std::set<Function*> FunctionsToPrint;
173 // If we have block count information, print out the LLVM module with
174 // frequency annotations.
175 if (PI.hasAccurateBlockCounts()) {
176 std::vector<std::pair<BasicBlock*, unsigned> > Counts;
177 PI.getBlockCounts(Counts);
180 for (unsigned i = 0, e = Counts.size(); i != e; ++i)
181 TotalExecutions += Counts[i].second;
183 // Sort by the frequency, backwards.
184 std::sort(Counts.begin(), Counts.end(),
185 PairSecondSortReverse<BasicBlock*>());
187 std::cout << "\n===" << std::string(73, '-') << "===\n";
188 std::cout << "Top 20 most frequently executed basic blocks:\n\n";
190 // Print out the function frequencies...
191 printf(" ## %%%% \tFrequency\n");
192 unsigned BlocksToPrint = Counts.size();
193 if (BlocksToPrint > 20) BlocksToPrint = 20;
194 for (unsigned i = 0; i != BlocksToPrint; ++i) {
195 if (Counts[i].second == 0) break;
196 Function *F = Counts[i].first->getParent();
197 printf("%3d. %5.2f%% %5u/%llu\t%s() - %s\n", i+1,
198 Counts[i].second/(double)TotalExecutions*100,
199 Counts[i].second, TotalExecutions,
200 F->getName().c_str(), Counts[i].first->getName().c_str());
201 FunctionsToPrint.insert(F);
204 BlockFreqs.insert(Counts.begin(), Counts.end());
207 if (PI.hasAccurateEdgeCounts()) {
208 std::vector<std::pair<ProfileInfoLoader::Edge, unsigned> > Counts;
209 PI.getEdgeCounts(Counts);
210 EdgeFreqs.insert(Counts.begin(), Counts.end());
213 if (PrintAnnotatedLLVM || PrintAllCode) {
214 std::cout << "\n===" << std::string(73, '-') << "===\n";
215 std::cout << "Annotated LLVM code for the module:\n\n";
217 ProfileAnnotator PA(FuncFreqs, BlockFreqs, EdgeFreqs);
219 if (FunctionsToPrint.empty() || PrintAllCode)
220 M->print(std::cout, &PA);
222 // Print just a subset of the functions...
223 for (std::set<Function*>::iterator I = FunctionsToPrint.begin(),
224 E = FunctionsToPrint.end(); I != E; ++I)
225 (*I)->print(std::cout, &PA);