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 "llvm/Support/CommandLine.h"
22 #include "llvm/System/Signals.h"
32 BytecodeFile(cl::Positional, cl::desc("<program bytecode file>"),
36 ProfileDataFile(cl::Positional, cl::desc("<llvmprof.out file>"),
37 cl::Optional, cl::init("llvmprof.out"));
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));
45 PrintAllCode("print-all-code",
46 cl::desc("Print annotated code for the entire program"));
49 // PairSecondSort - A sorting predicate to sort by the second element of a pair.
51 struct PairSecondSortReverse
52 : 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 {
56 return LHS.second > RHS.second;
61 class ProfileAnnotator : public AssemblyAnnotationWriter {
62 std::map<const Function *, unsigned> &FuncFreqs;
63 std::map<const BasicBlock*, unsigned> &BlockFreqs;
64 std::map<ProfileInfoLoader::Edge, unsigned> &EdgeFreqs;
66 ProfileAnnotator(std::map<const Function *, unsigned> &FF,
67 std::map<const BasicBlock*, unsigned> &BF,
68 std::map<ProfileInfoLoader::Edge, unsigned> &EF)
69 : FuncFreqs(FF), BlockFreqs(BF), EdgeFreqs(EF) {}
71 virtual void emitFunctionAnnot(const Function *F, std::ostream &OS) {
72 OS << ";;; %" << F->getName() << " called " << FuncFreqs[F]
75 virtual void emitBasicBlockStartAnnot(const BasicBlock *BB,
77 if (BlockFreqs.empty()) return;
78 if (unsigned Count = BlockFreqs[BB])
79 OS << "\t;;; Basic block executed " << Count << " times.\n";
81 OS << "\t;;; Never executed!\n";
84 virtual void emitBasicBlockEndAnnot(const BasicBlock *BB, std::ostream &OS){
85 if (EdgeFreqs.empty()) return;
87 // Figure out how many times each successor executed.
88 std::vector<std::pair<const BasicBlock*, unsigned> > SuccCounts;
89 const TerminatorInst *TI = BB->getTerminator();
91 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)
95 SuccCounts.push_back(std::make_pair(TI->getSuccessor(I->first.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() << "]";
109 int main(int argc, char **argv) {
110 cl::ParseCommandLineOptions(argc, argv, " llvm profile dump decoder\n");
111 sys::PrintStackTraceOnErrorSignal();
113 // Read in the bytecode file...
114 std::string ErrorMessage;
115 Module *M = ParseBytecodeFile(BytecodeFile, &ErrorMessage);
117 std::cerr << argv[0] << ": " << BytecodeFile << ": " << ErrorMessage
122 // Read the profiling information
123 ProfileInfoLoader PI(argv[0], ProfileDataFile, *M);
125 std::map<const Function *, unsigned> FuncFreqs;
126 std::map<const BasicBlock*, unsigned> BlockFreqs;
127 std::map<ProfileInfoLoader::Edge, unsigned> EdgeFreqs;
129 // Output a report. Eventually, there will be multiple reports selectable on
130 // the command line, for now, just keep things simple.
132 // Emit the most frequent function table...
133 std::vector<std::pair<Function*, unsigned> > FunctionCounts;
134 PI.getFunctionCounts(FunctionCounts);
135 FuncFreqs.insert(FunctionCounts.begin(), FunctionCounts.end());
137 // Sort by the frequency, backwards.
138 std::sort(FunctionCounts.begin(), FunctionCounts.end(),
139 PairSecondSortReverse<Function*>());
141 unsigned long long TotalExecutions = 0;
142 for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i)
143 TotalExecutions += FunctionCounts[i].second;
145 std::cout << "===" << std::string(73, '-') << "===\n"
146 << "LLVM profiling output for execution";
147 if (PI.getNumExecutions() != 1) std::cout << "s";
150 for (unsigned i = 0, e = PI.getNumExecutions(); i != e; ++i) {
152 if (e != 1) std::cout << i+1 << ". ";
153 std::cout << PI.getExecution(i) << "\n";
156 std::cout << "\n===" << std::string(73, '-') << "===\n";
157 std::cout << "Function execution frequencies:\n\n";
159 // Print out the function frequencies...
160 printf(" ## Frequency\n");
161 for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i) {
162 if (FunctionCounts[i].second == 0) {
163 printf("\n NOTE: %d function%s never executed!\n",
164 e-i, e-i-1 ? "s were" : " was");
168 printf("%3d. %5u/%llu %s\n", i+1, FunctionCounts[i].second, TotalExecutions,
169 FunctionCounts[i].first->getName().c_str());
172 std::set<Function*> FunctionsToPrint;
174 // If we have block count information, print out the LLVM module with
175 // frequency annotations.
176 if (PI.hasAccurateBlockCounts()) {
177 std::vector<std::pair<BasicBlock*, unsigned> > Counts;
178 PI.getBlockCounts(Counts);
181 for (unsigned i = 0, e = Counts.size(); i != e; ++i)
182 TotalExecutions += Counts[i].second;
184 // Sort by the frequency, backwards.
185 std::sort(Counts.begin(), Counts.end(),
186 PairSecondSortReverse<BasicBlock*>());
188 std::cout << "\n===" << std::string(73, '-') << "===\n";
189 std::cout << "Top 20 most frequently executed basic blocks:\n\n";
191 // Print out the function frequencies...
192 printf(" ## %%%% \tFrequency\n");
193 unsigned BlocksToPrint = Counts.size();
194 if (BlocksToPrint > 20) BlocksToPrint = 20;
195 for (unsigned i = 0; i != BlocksToPrint; ++i) {
196 if (Counts[i].second == 0) break;
197 Function *F = Counts[i].first->getParent();
198 printf("%3d. %5.2f%% %5u/%llu\t%s() - %s\n", i+1,
199 Counts[i].second/(double)TotalExecutions*100,
200 Counts[i].second, TotalExecutions,
201 F->getName().c_str(), Counts[i].first->getName().c_str());
202 FunctionsToPrint.insert(F);
205 BlockFreqs.insert(Counts.begin(), Counts.end());
208 if (PI.hasAccurateEdgeCounts()) {
209 std::vector<std::pair<ProfileInfoLoader::Edge, unsigned> > Counts;
210 PI.getEdgeCounts(Counts);
211 EdgeFreqs.insert(Counts.begin(), Counts.end());
214 if (PrintAnnotatedLLVM || PrintAllCode) {
215 std::cout << "\n===" << std::string(73, '-') << "===\n";
216 std::cout << "Annotated LLVM code for the module:\n\n";
218 ProfileAnnotator PA(FuncFreqs, BlockFreqs, EdgeFreqs);
220 if (FunctionsToPrint.empty() || PrintAllCode)
221 M->print(std::cout, &PA);
223 // Print just a subset of the functions...
224 for (std::set<Function*>::iterator I = FunctionsToPrint.begin(),
225 E = FunctionsToPrint.end(); I != E; ++I)
226 (*I)->print(std::cout, &PA);