'Tis quite silly to check for a cached version of the entire executable. That
[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 "ProfileInfo.h"
17 #include "llvm/Module.h"
18 #include "llvm/Assembly/AsmAnnotationWriter.h"
19 #include "llvm/Bytecode/Reader.h"
20 #include "Support/CommandLine.h"
21 #include <iostream>
22 #include <cstdio>
23 #include <map>
24 #include <set>
25
26 namespace {
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"));
34
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));
40   cl::opt<bool>
41   PrintAllCode("print-all-code",
42                cl::desc("Print annotated code for the entire program"));
43 }
44
45 // PairSecondSort - A sorting predicate to sort by the second element of a pair.
46 template<class T>
47 struct PairSecondSortReverse
48   : 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 {
52     return LHS.second > RHS.second;
53   }
54 };
55
56 namespace {
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) {
70       if (BlockFreqs.empty()) return;
71       if (unsigned Count = BlockFreqs[BB])
72         OS << ";;; Executed " << Count << " times.\n";
73       else
74         OS << ";;; Never executed!\n";
75     }
76   };
77 }
78
79
80 int main(int argc, char **argv) {
81   cl::ParseCommandLineOptions(argc, argv, " llvm profile dump decoder\n");
82
83   // Read in the bytecode file...
84   std::string ErrorMessage;
85   Module *M = ParseBytecodeFile(BytecodeFile, &ErrorMessage);
86   if (M == 0) {
87     std::cerr << argv[0] << ": " << BytecodeFile << ": " << ErrorMessage
88               << "\n";
89     return 1;
90   }
91
92   // Read the profiling information
93   ProfileInfo PI(argv[0], ProfileDataFile, *M);
94
95   std::map<const Function  *, unsigned> FuncFreqs;
96   std::map<const BasicBlock*, unsigned> BlockFreqs;
97
98   // 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);
104   FuncFreqs.insert(FunctionCounts.begin(), FunctionCounts.end());
105
106   // Sort by the frequency, backwards.
107   std::sort(FunctionCounts.begin(), FunctionCounts.end(),
108             PairSecondSortReverse<Function*>());
109
110   unsigned long long TotalExecutions = 0;
111   for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i)
112     TotalExecutions += FunctionCounts[i].second;
113   
114   std::cout << "===" << std::string(73, '-') << "===\n"
115             << "LLVM profiling output for execution";
116   if (PI.getNumExecutions() != 1) std::cout << "s";
117   std::cout << ":\n";
118   
119   for (unsigned i = 0, e = PI.getNumExecutions(); i != e; ++i) {
120     std::cout << "  ";
121     if (e != 1) std::cout << i+1 << ". ";
122     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
128   // Print out the function frequencies...
129   printf(" ##   Frequency\n");
130   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
137     printf("%3d. %5u/%llu %s\n", i+1, FunctionCounts[i].second, TotalExecutions,
138            FunctionCounts[i].first->getName().c_str());
139   }
140
141   std::set<Function*> FunctionsToPrint;
142
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);
148
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...
161     printf(" ##      %%%% \tFrequency\n");
162     unsigned BlocksToPrint = Counts.size();
163     if (BlocksToPrint > 20) BlocksToPrint = 20;
164     for (unsigned i = 0; i != BlocksToPrint; ++i) {
165       if (Counts[i].second == 0) break;
166       Function *F = Counts[i].first->getParent();
167       printf("%3d. %5.2f%% %5u/%llu\t%s() - %s\n", i+1,
168              Counts[i].second/(double)TotalExecutions*100,
169              Counts[i].second, TotalExecutions,
170              F->getName().c_str(), Counts[i].first->getName().c_str());
171       FunctionsToPrint.insert(F);
172     }
173
174     BlockFreqs.insert(Counts.begin(), Counts.end());
175   }
176   
177   if (PrintAnnotatedLLVM || PrintAllCode) {
178     std::cout << "\n===" << std::string(73, '-') << "===\n";
179     std::cout << "Annotated LLVM code for the module:\n\n";
180     
181     ProfileAnnotator PA(FuncFreqs, BlockFreqs);
182
183     if (FunctionsToPrint.empty() || PrintAllCode)
184       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);
190   }
191
192   return 0;
193 }