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