Don't crash if there are no passes in the PassesToRun list
[oota-llvm.git] / tools / llvm-prof / llvm-prof.cpp
1 //===- 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
16 #include "llvm/Module.h"
17 #include "llvm/Assembly/AsmAnnotationWriter.h"
18 #include "llvm/Analysis/ProfileInfoLoader.h"
19 #include "llvm/Bytecode/Reader.h"
20 #include "Support/CommandLine.h"
21 #include <cstdio>
22 #include <map>
23 #include <set>
24
25 using namespace llvm;
26
27 namespace {
28   cl::opt<std::string> 
29   BytecodeFile(cl::Positional, cl::desc("<program bytecode file>"),
30                cl::Required);
31
32   cl::opt<std::string> 
33   ProfileDataFile(cl::Positional, cl::desc("<llvmprof.out file>"),
34                   cl::Optional, cl::init("llvmprof.out"));
35
36   cl::opt<bool>
37   PrintAnnotatedLLVM("annotated-llvm",
38                      cl::desc("Print LLVM code with frequency annotations"));
39   cl::alias PrintAnnotated2("A", cl::desc("Alias for --annotated-llvm"),
40                             cl::aliasopt(PrintAnnotatedLLVM));
41   cl::opt<bool>
42   PrintAllCode("print-all-code",
43                cl::desc("Print annotated code for the entire program"));
44 }
45
46 // PairSecondSort - A sorting predicate to sort by the second element of a pair.
47 template<class T>
48 struct PairSecondSortReverse
49   : public std::binary_function<std::pair<T, unsigned>,
50                                 std::pair<T, unsigned>, bool> {
51   bool operator()(const std::pair<T, unsigned> &LHS,
52                   const std::pair<T, unsigned> &RHS) const {
53     return LHS.second > RHS.second;
54   }
55 };
56
57 namespace {
58   class ProfileAnnotator : public AssemblyAnnotationWriter {
59     std::map<const Function  *, unsigned> &FuncFreqs;
60     std::map<const BasicBlock*, unsigned> &BlockFreqs;
61   public:
62     ProfileAnnotator(std::map<const Function  *, unsigned> &FF,
63                      std::map<const BasicBlock*, unsigned> &BF)
64       : FuncFreqs(FF), BlockFreqs(BF) {}
65
66     virtual void emitFunctionAnnot(const Function *F, std::ostream &OS) {
67       OS << ";;; %" << F->getName() << " called " << FuncFreqs[F]
68          << " times.\n;;;\n";
69     }
70     virtual void emitBasicBlockAnnot(const BasicBlock *BB, std::ostream &OS) {
71       if (BlockFreqs.empty()) return;
72       if (unsigned Count = BlockFreqs[BB])
73         OS << ";;; Executed " << Count << " times.\n";
74       else
75         OS << ";;; Never executed!\n";
76     }
77   };
78 }
79
80
81 int main(int argc, char **argv) {
82   cl::ParseCommandLineOptions(argc, argv, " llvm profile dump decoder\n");
83
84   // Read in the bytecode file...
85   std::string ErrorMessage;
86   Module *M = ParseBytecodeFile(BytecodeFile, &ErrorMessage);
87   if (M == 0) {
88     std::cerr << argv[0] << ": " << BytecodeFile << ": " << ErrorMessage
89               << "\n";
90     return 1;
91   }
92
93   // Read the profiling information
94   ProfileInfoLoader PI(argv[0], ProfileDataFile, *M);
95
96   std::map<const Function  *, unsigned> FuncFreqs;
97   std::map<const BasicBlock*, unsigned> BlockFreqs;
98
99   // Output a report.  Eventually, there will be multiple reports selectable on
100   // the command line, for now, just keep things simple.
101
102   // Emit the most frequent function table...
103   std::vector<std::pair<Function*, unsigned> > FunctionCounts;
104   PI.getFunctionCounts(FunctionCounts);
105   FuncFreqs.insert(FunctionCounts.begin(), FunctionCounts.end());
106
107   // Sort by the frequency, backwards.
108   std::sort(FunctionCounts.begin(), FunctionCounts.end(),
109             PairSecondSortReverse<Function*>());
110
111   unsigned long long TotalExecutions = 0;
112   for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i)
113     TotalExecutions += FunctionCounts[i].second;
114   
115   std::cout << "===" << std::string(73, '-') << "===\n"
116             << "LLVM profiling output for execution";
117   if (PI.getNumExecutions() != 1) std::cout << "s";
118   std::cout << ":\n";
119   
120   for (unsigned i = 0, e = PI.getNumExecutions(); i != e; ++i) {
121     std::cout << "  ";
122     if (e != 1) std::cout << i+1 << ". ";
123     std::cout << PI.getExecution(i) << "\n";
124   }
125   
126   std::cout << "\n===" << std::string(73, '-') << "===\n";
127   std::cout << "Function execution frequencies:\n\n";
128
129   // Print out the function frequencies...
130   printf(" ##   Frequency\n");
131   for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i) {
132     if (FunctionCounts[i].second == 0) {
133       printf("\n  NOTE: %d function%s never executed!\n",
134              e-i, e-i-1 ? "s were" : " was");
135       break;
136     }
137
138     printf("%3d. %5u/%llu %s\n", i+1, FunctionCounts[i].second, TotalExecutions,
139            FunctionCounts[i].first->getName().c_str());
140   }
141
142   std::set<Function*> FunctionsToPrint;
143
144   // If we have block count information, print out the LLVM module with
145   // frequency annotations.
146   if (PI.hasAccurateBlockCounts()) {
147     std::vector<std::pair<BasicBlock*, unsigned> > Counts;
148     PI.getBlockCounts(Counts);
149
150     TotalExecutions = 0;
151     for (unsigned i = 0, e = Counts.size(); i != e; ++i)
152       TotalExecutions += Counts[i].second;
153
154     // Sort by the frequency, backwards.
155     std::sort(Counts.begin(), Counts.end(),
156               PairSecondSortReverse<BasicBlock*>());
157     
158     std::cout << "\n===" << std::string(73, '-') << "===\n";
159     std::cout << "Top 20 most frequently executed basic blocks:\n\n";
160
161     // Print out the function frequencies...
162     printf(" ##      %%%% \tFrequency\n");
163     unsigned BlocksToPrint = Counts.size();
164     if (BlocksToPrint > 20) BlocksToPrint = 20;
165     for (unsigned i = 0; i != BlocksToPrint; ++i) {
166       if (Counts[i].second == 0) break;
167       Function *F = Counts[i].first->getParent();
168       printf("%3d. %5.2f%% %5u/%llu\t%s() - %s\n", i+1,
169              Counts[i].second/(double)TotalExecutions*100,
170              Counts[i].second, TotalExecutions,
171              F->getName().c_str(), Counts[i].first->getName().c_str());
172       FunctionsToPrint.insert(F);
173     }
174
175     BlockFreqs.insert(Counts.begin(), Counts.end());
176   }
177   
178   if (PrintAnnotatedLLVM || PrintAllCode) {
179     std::cout << "\n===" << std::string(73, '-') << "===\n";
180     std::cout << "Annotated LLVM code for the module:\n\n";
181     
182     ProfileAnnotator PA(FuncFreqs, BlockFreqs);
183
184     if (FunctionsToPrint.empty() || PrintAllCode)
185       M->print(std::cout, &PA);
186     else
187       // Print just a subset of the functions...
188       for (std::set<Function*>::iterator I = FunctionsToPrint.begin(),
189              E = FunctionsToPrint.end(); I != E; ++I)
190         (*I)->print(std::cout, &PA);
191   }
192
193   return 0;
194 }