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 | 5e71764 | 2003-10-30 23:42:09 +0000 | [diff] [blame] | 16 | #include "llvm/Module.h" |
| 17 | #include "llvm/Assembly/AsmAnnotationWriter.h" |
Chris Lattner | 89cf393 | 2004-02-11 05:56:07 +0000 | [diff] [blame^] | 18 | #include "llvm/Analysis/ProfileInfoLoader.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 | 7a78d81 | 2003-10-28 21:08:18 +0000 | [diff] [blame] | 21 | #include <cstdio> |
Chris Lattner | 33f1ca7 | 2003-10-28 21:25:23 +0000 | [diff] [blame] | 22 | #include <map> |
Chris Lattner | 5e71764 | 2003-10-30 23:42:09 +0000 | [diff] [blame] | 23 | #include <set> |
Chris Lattner | 6f82d07 | 2003-10-28 19:16:35 +0000 | [diff] [blame] | 24 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 25 | using namespace llvm; |
| 26 | |
Chris Lattner | 6f82d07 | 2003-10-28 19:16:35 +0000 | [diff] [blame] | 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")); |
Chris Lattner | 5e71764 | 2003-10-30 23:42:09 +0000 | [diff] [blame] | 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)); |
Chris Lattner | cde1cf3 | 2003-11-06 20:29:25 +0000 | [diff] [blame] | 41 | cl::opt<bool> |
| 42 | PrintAllCode("print-all-code", |
| 43 | cl::desc("Print annotated code for the entire program")); |
Chris Lattner | 6f82d07 | 2003-10-28 19:16:35 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Chris Lattner | 7a78d81 | 2003-10-28 21:08:18 +0000 | [diff] [blame] | 46 | // PairSecondSort - A sorting predicate to sort by the second element of a pair. |
| 47 | template<class T> |
Chris Lattner | 18884a8 | 2003-10-29 21:41:17 +0000 | [diff] [blame] | 48 | struct PairSecondSortReverse |
Chris Lattner | 7a78d81 | 2003-10-28 21:08:18 +0000 | [diff] [blame] | 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 { |
Chris Lattner | 18884a8 | 2003-10-29 21:41:17 +0000 | [diff] [blame] | 53 | return LHS.second > RHS.second; |
Chris Lattner | 7a78d81 | 2003-10-28 21:08:18 +0000 | [diff] [blame] | 54 | } |
| 55 | }; |
| 56 | |
Chris Lattner | 5e71764 | 2003-10-30 23:42:09 +0000 | [diff] [blame] | 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) { |
Chris Lattner | 3673730 | 2003-10-30 23:44:28 +0000 | [diff] [blame] | 71 | if (BlockFreqs.empty()) return; |
Chris Lattner | 5e71764 | 2003-10-30 23:42:09 +0000 | [diff] [blame] | 72 | if (unsigned Count = BlockFreqs[BB]) |
| 73 | OS << ";;; Executed " << Count << " times.\n"; |
| 74 | else |
| 75 | OS << ";;; Never executed!\n"; |
| 76 | } |
| 77 | }; |
| 78 | } |
| 79 | |
Chris Lattner | 7a78d81 | 2003-10-28 21:08:18 +0000 | [diff] [blame] | 80 | |
Chris Lattner | 6f82d07 | 2003-10-28 19:16:35 +0000 | [diff] [blame] | 81 | int main(int argc, char **argv) { |
| 82 | cl::ParseCommandLineOptions(argc, argv, " llvm profile dump decoder\n"); |
Chris Lattner | 6f82d07 | 2003-10-28 19:16:35 +0000 | [diff] [blame] | 83 | |
Chris Lattner | e436779 | 2003-10-28 20:13:07 +0000 | [diff] [blame] | 84 | // Read in the bytecode file... |
| 85 | std::string ErrorMessage; |
Chris Lattner | 7a78d81 | 2003-10-28 21:08:18 +0000 | [diff] [blame] | 86 | Module *M = ParseBytecodeFile(BytecodeFile, &ErrorMessage); |
| 87 | if (M == 0) { |
Chris Lattner | e436779 | 2003-10-28 20:13:07 +0000 | [diff] [blame] | 88 | std::cerr << argv[0] << ": " << BytecodeFile << ": " << ErrorMessage |
| 89 | << "\n"; |
| 90 | return 1; |
| 91 | } |
Chris Lattner | 6f82d07 | 2003-10-28 19:16:35 +0000 | [diff] [blame] | 92 | |
Chris Lattner | e436779 | 2003-10-28 20:13:07 +0000 | [diff] [blame] | 93 | // Read the profiling information |
Chris Lattner | 89cf393 | 2004-02-11 05:56:07 +0000 | [diff] [blame^] | 94 | ProfileInfoLoader PI(argv[0], ProfileDataFile, *M); |
Chris Lattner | 6f82d07 | 2003-10-28 19:16:35 +0000 | [diff] [blame] | 95 | |
Chris Lattner | 5e71764 | 2003-10-30 23:42:09 +0000 | [diff] [blame] | 96 | std::map<const Function *, unsigned> FuncFreqs; |
| 97 | std::map<const BasicBlock*, unsigned> BlockFreqs; |
| 98 | |
Chris Lattner | 7a78d81 | 2003-10-28 21:08:18 +0000 | [diff] [blame] | 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); |
Chris Lattner | 5e71764 | 2003-10-30 23:42:09 +0000 | [diff] [blame] | 105 | FuncFreqs.insert(FunctionCounts.begin(), FunctionCounts.end()); |
Chris Lattner | 7a78d81 | 2003-10-28 21:08:18 +0000 | [diff] [blame] | 106 | |
| 107 | // Sort by the frequency, backwards. |
| 108 | std::sort(FunctionCounts.begin(), FunctionCounts.end(), |
Chris Lattner | 18884a8 | 2003-10-29 21:41:17 +0000 | [diff] [blame] | 109 | PairSecondSortReverse<Function*>()); |
Chris Lattner | 7a78d81 | 2003-10-28 21:08:18 +0000 | [diff] [blame] | 110 | |
Chris Lattner | 3baed41 | 2003-10-31 00:06:57 +0000 | [diff] [blame] | 111 | unsigned long long TotalExecutions = 0; |
Chris Lattner | 7a78d81 | 2003-10-28 21:08:18 +0000 | [diff] [blame] | 112 | for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i) |
| 113 | TotalExecutions += FunctionCounts[i].second; |
| 114 | |
Chris Lattner | 4963dcf | 2003-10-28 22:30:37 +0000 | [diff] [blame] | 115 | std::cout << "===" << std::string(73, '-') << "===\n" |
Chris Lattner | 3688205 | 2003-10-28 22:53:49 +0000 | [diff] [blame] | 116 | << "LLVM profiling output for execution"; |
| 117 | if (PI.getNumExecutions() != 1) std::cout << "s"; |
| 118 | std::cout << ":\n"; |
Chris Lattner | 4963dcf | 2003-10-28 22:30:37 +0000 | [diff] [blame] | 119 | |
| 120 | for (unsigned i = 0, e = PI.getNumExecutions(); i != e; ++i) { |
| 121 | std::cout << " "; |
Chris Lattner | 18884a8 | 2003-10-29 21:41:17 +0000 | [diff] [blame] | 122 | if (e != 1) std::cout << i+1 << ". "; |
Chris Lattner | 4963dcf | 2003-10-28 22:30:37 +0000 | [diff] [blame] | 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 | |
Chris Lattner | 7a78d81 | 2003-10-28 21:08:18 +0000 | [diff] [blame] | 129 | // Print out the function frequencies... |
| 130 | printf(" ## Frequency\n"); |
Chris Lattner | 4963dcf | 2003-10-28 22:30:37 +0000 | [diff] [blame] | 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 | |
Chris Lattner | 3baed41 | 2003-10-31 00:06:57 +0000 | [diff] [blame] | 138 | printf("%3d. %5u/%llu %s\n", i+1, FunctionCounts[i].second, TotalExecutions, |
Chris Lattner | 7a78d81 | 2003-10-28 21:08:18 +0000 | [diff] [blame] | 139 | FunctionCounts[i].first->getName().c_str()); |
Chris Lattner | 4963dcf | 2003-10-28 22:30:37 +0000 | [diff] [blame] | 140 | } |
Chris Lattner | 33f1ca7 | 2003-10-28 21:25:23 +0000 | [diff] [blame] | 141 | |
Chris Lattner | 5e71764 | 2003-10-30 23:42:09 +0000 | [diff] [blame] | 142 | std::set<Function*> FunctionsToPrint; |
Chris Lattner | 33f1ca7 | 2003-10-28 21:25:23 +0000 | [diff] [blame] | 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); |
Chris Lattner | 18884a8 | 2003-10-29 21:41:17 +0000 | [diff] [blame] | 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... |
Chris Lattner | e1ccadf | 2003-10-31 00:13:26 +0000 | [diff] [blame] | 162 | printf(" ## %%%% \tFrequency\n"); |
Chris Lattner | 18884a8 | 2003-10-29 21:41:17 +0000 | [diff] [blame] | 163 | unsigned BlocksToPrint = Counts.size(); |
| 164 | if (BlocksToPrint > 20) BlocksToPrint = 20; |
Chris Lattner | 5e71764 | 2003-10-30 23:42:09 +0000 | [diff] [blame] | 165 | for (unsigned i = 0; i != BlocksToPrint; ++i) { |
Chris Lattner | 9088de7 | 2003-10-31 00:34:05 +0000 | [diff] [blame] | 166 | if (Counts[i].second == 0) break; |
Chris Lattner | 5e71764 | 2003-10-30 23:42:09 +0000 | [diff] [blame] | 167 | Function *F = Counts[i].first->getParent(); |
Chris Lattner | e1ccadf | 2003-10-31 00:13:26 +0000 | [diff] [blame] | 168 | printf("%3d. %5.2f%% %5u/%llu\t%s() - %s\n", i+1, |
| 169 | Counts[i].second/(double)TotalExecutions*100, |
Chris Lattner | 3baed41 | 2003-10-31 00:06:57 +0000 | [diff] [blame] | 170 | Counts[i].second, TotalExecutions, |
Chris Lattner | 5e71764 | 2003-10-30 23:42:09 +0000 | [diff] [blame] | 171 | F->getName().c_str(), Counts[i].first->getName().c_str()); |
| 172 | FunctionsToPrint.insert(F); |
| 173 | } |
Chris Lattner | 18884a8 | 2003-10-29 21:41:17 +0000 | [diff] [blame] | 174 | |
Chris Lattner | 5e71764 | 2003-10-30 23:42:09 +0000 | [diff] [blame] | 175 | BlockFreqs.insert(Counts.begin(), Counts.end()); |
| 176 | } |
| 177 | |
Chris Lattner | cde1cf3 | 2003-11-06 20:29:25 +0000 | [diff] [blame] | 178 | if (PrintAnnotatedLLVM || PrintAllCode) { |
Chris Lattner | 5e71764 | 2003-10-30 23:42:09 +0000 | [diff] [blame] | 179 | std::cout << "\n===" << std::string(73, '-') << "===\n"; |
| 180 | std::cout << "Annotated LLVM code for the module:\n\n"; |
Chris Lattner | 18884a8 | 2003-10-29 21:41:17 +0000 | [diff] [blame] | 181 | |
Chris Lattner | 5e71764 | 2003-10-30 23:42:09 +0000 | [diff] [blame] | 182 | ProfileAnnotator PA(FuncFreqs, BlockFreqs); |
| 183 | |
Chris Lattner | cde1cf3 | 2003-11-06 20:29:25 +0000 | [diff] [blame] | 184 | if (FunctionsToPrint.empty() || PrintAllCode) |
Chris Lattner | 3b7f416 | 2003-10-31 00:20:09 +0000 | [diff] [blame] | 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); |
Chris Lattner | 33f1ca7 | 2003-10-28 21:25:23 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Chris Lattner | 6f82d07 | 2003-10-28 19:16:35 +0000 | [diff] [blame] | 193 | return 0; |
| 194 | } |