Chris Lattner | 6f82d07 | 2003-10-28 19:16:35 +0000 | [diff] [blame] | 1 | //===- llvm-prof.cpp - Read in and process llvmprof.out data files --------===// |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | 6f82d07 | 2003-10-28 19:16:35 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 21c62da | 2007-12-29 20:44:31 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | 6f82d07 | 2003-10-28 19:16:35 +0000 | [diff] [blame] | 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 | 8c2730e | 2004-03-08 20:04:32 +0000 | [diff] [blame] | 16 | #include "llvm/InstrTypes.h" |
Owen Anderson | 8b477ed | 2009-07-01 16:58:40 +0000 | [diff] [blame] | 17 | #include "llvm/LLVMContext.h" |
Chris Lattner | 5e71764 | 2003-10-30 23:42:09 +0000 | [diff] [blame] | 18 | #include "llvm/Module.h" |
Daniel Dunbar | 314fa8e | 2009-07-14 07:41:11 +0000 | [diff] [blame] | 19 | #include "llvm/PassManager.h" |
Chris Lattner | 1dbb387 | 2010-09-02 23:09:42 +0000 | [diff] [blame] | 20 | #include "llvm/Assembly/AssemblyAnnotationWriter.h" |
Daniel Dunbar | 314fa8e | 2009-07-14 07:41:11 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/ProfileInfo.h" |
Chris Lattner | 89cf393 | 2004-02-11 05:56:07 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/ProfileInfoLoader.h" |
Daniel Dunbar | ee16638 | 2009-08-05 15:55:56 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/Passes.h" |
Chris Lattner | 592488a | 2007-05-06 04:43:00 +0000 | [diff] [blame] | 24 | #include "llvm/Bitcode/ReaderWriter.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 25 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | a11a6a0 | 2010-09-02 23:07:12 +0000 | [diff] [blame] | 26 | #include "llvm/Support/FormattedStream.h" |
Chris Lattner | c30598b | 2006-12-06 01:18:01 +0000 | [diff] [blame] | 27 | #include "llvm/Support/ManagedStatic.h" |
Chris Lattner | 592488a | 2007-05-06 04:43:00 +0000 | [diff] [blame] | 28 | #include "llvm/Support/MemoryBuffer.h" |
Chris Lattner | cc14d25 | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 29 | #include "llvm/Support/PrettyStackTrace.h" |
Chris Lattner | 944fac7 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 30 | #include "llvm/Support/raw_ostream.h" |
Andreas Neustifter | 30457f2 | 2009-08-26 09:05:21 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Format.h" |
Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 32 | #include "llvm/Support/Signals.h" |
Michael J. Spencer | 333fb04 | 2010-12-09 17:36:48 +0000 | [diff] [blame] | 33 | #include "llvm/Support/system_error.h" |
Jeff Cohen | ca5183d | 2007-03-05 00:00:42 +0000 | [diff] [blame] | 34 | #include <algorithm> |
Reid Spencer | 78b0e6a | 2005-12-29 21:13:45 +0000 | [diff] [blame] | 35 | #include <iomanip> |
Chris Lattner | 33f1ca7 | 2003-10-28 21:25:23 +0000 | [diff] [blame] | 36 | #include <map> |
Chris Lattner | 5e71764 | 2003-10-30 23:42:09 +0000 | [diff] [blame] | 37 | #include <set> |
Chris Lattner | 6f82d07 | 2003-10-28 19:16:35 +0000 | [diff] [blame] | 38 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 39 | using namespace llvm; |
| 40 | |
Chris Lattner | 6f82d07 | 2003-10-28 19:16:35 +0000 | [diff] [blame] | 41 | namespace { |
Reid Spencer | 1adc3de | 2005-12-30 09:07:29 +0000 | [diff] [blame] | 42 | cl::opt<std::string> |
Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 43 | BitcodeFile(cl::Positional, cl::desc("<program bitcode file>"), |
| 44 | cl::Required); |
Chris Lattner | 6f82d07 | 2003-10-28 19:16:35 +0000 | [diff] [blame] | 45 | |
Reid Spencer | 1adc3de | 2005-12-30 09:07:29 +0000 | [diff] [blame] | 46 | cl::opt<std::string> |
Chris Lattner | 6f82d07 | 2003-10-28 19:16:35 +0000 | [diff] [blame] | 47 | ProfileDataFile(cl::Positional, cl::desc("<llvmprof.out file>"), |
| 48 | cl::Optional, cl::init("llvmprof.out")); |
Chris Lattner | 5e71764 | 2003-10-30 23:42:09 +0000 | [diff] [blame] | 49 | |
| 50 | cl::opt<bool> |
| 51 | PrintAnnotatedLLVM("annotated-llvm", |
| 52 | cl::desc("Print LLVM code with frequency annotations")); |
| 53 | cl::alias PrintAnnotated2("A", cl::desc("Alias for --annotated-llvm"), |
| 54 | cl::aliasopt(PrintAnnotatedLLVM)); |
Chris Lattner | cde1cf3 | 2003-11-06 20:29:25 +0000 | [diff] [blame] | 55 | cl::opt<bool> |
| 56 | PrintAllCode("print-all-code", |
| 57 | cl::desc("Print annotated code for the entire program")); |
Chris Lattner | 6f82d07 | 2003-10-28 19:16:35 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Chris Lattner | 7a78d81 | 2003-10-28 21:08:18 +0000 | [diff] [blame] | 60 | // PairSecondSort - A sorting predicate to sort by the second element of a pair. |
| 61 | template<class T> |
Chris Lattner | 18884a8 | 2003-10-29 21:41:17 +0000 | [diff] [blame] | 62 | struct PairSecondSortReverse |
Daniel Dunbar | 3cdfb4a | 2009-08-13 01:55:43 +0000 | [diff] [blame] | 63 | : public std::binary_function<std::pair<T, double>, |
| 64 | std::pair<T, double>, bool> { |
| 65 | bool operator()(const std::pair<T, double> &LHS, |
| 66 | const std::pair<T, double> &RHS) const { |
Chris Lattner | 18884a8 | 2003-10-29 21:41:17 +0000 | [diff] [blame] | 67 | return LHS.second > RHS.second; |
Chris Lattner | 7a78d81 | 2003-10-28 21:08:18 +0000 | [diff] [blame] | 68 | } |
| 69 | }; |
| 70 | |
Daniel Dunbar | caaa493 | 2009-08-08 17:43:09 +0000 | [diff] [blame] | 71 | static double ignoreMissing(double w) { |
| 72 | if (w == ProfileInfo::MissingValue) return 0; |
| 73 | return w; |
| 74 | } |
| 75 | |
Chris Lattner | 5e71764 | 2003-10-30 23:42:09 +0000 | [diff] [blame] | 76 | namespace { |
| 77 | class ProfileAnnotator : public AssemblyAnnotationWriter { |
Daniel Dunbar | ee16638 | 2009-08-05 15:55:56 +0000 | [diff] [blame] | 78 | ProfileInfo &PI; |
Chris Lattner | 5e71764 | 2003-10-30 23:42:09 +0000 | [diff] [blame] | 79 | public: |
Chris Lattner | a11a6a0 | 2010-09-02 23:07:12 +0000 | [diff] [blame] | 80 | ProfileAnnotator(ProfileInfo &pi) : PI(pi) {} |
Chris Lattner | 5e71764 | 2003-10-30 23:42:09 +0000 | [diff] [blame] | 81 | |
Chris Lattner | a11a6a0 | 2010-09-02 23:07:12 +0000 | [diff] [blame] | 82 | virtual void emitFunctionAnnot(const Function *F, |
| 83 | formatted_raw_ostream &OS) { |
Daniel Dunbar | caaa493 | 2009-08-08 17:43:09 +0000 | [diff] [blame] | 84 | double w = PI.getExecutionCount(F); |
Daniel Dunbar | c43782c | 2009-08-08 18:59:03 +0000 | [diff] [blame] | 85 | if (w != ProfileInfo::MissingValue) { |
| 86 | OS << ";;; %" << F->getName() << " called "<<(unsigned)w |
| 87 | <<" times.\n;;;\n"; |
| 88 | } |
Chris Lattner | 5e71764 | 2003-10-30 23:42:09 +0000 | [diff] [blame] | 89 | } |
Chris Lattner | 8c2730e | 2004-03-08 20:04:32 +0000 | [diff] [blame] | 90 | virtual void emitBasicBlockStartAnnot(const BasicBlock *BB, |
Chris Lattner | a11a6a0 | 2010-09-02 23:07:12 +0000 | [diff] [blame] | 91 | formatted_raw_ostream &OS) { |
Daniel Dunbar | caaa493 | 2009-08-08 17:43:09 +0000 | [diff] [blame] | 92 | double w = PI.getExecutionCount(BB); |
Daniel Dunbar | c43782c | 2009-08-08 18:59:03 +0000 | [diff] [blame] | 93 | if (w != ProfileInfo::MissingValue) { |
| 94 | if (w != 0) { |
| 95 | OS << "\t;;; Basic block executed " << (unsigned)w << " times.\n"; |
| 96 | } else { |
Daniel Dunbar | caaa493 | 2009-08-08 17:43:09 +0000 | [diff] [blame] | 97 | OS << "\t;;; Never executed!\n"; |
Daniel Dunbar | c43782c | 2009-08-08 18:59:03 +0000 | [diff] [blame] | 98 | } |
| 99 | } |
Chris Lattner | 8c2730e | 2004-03-08 20:04:32 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Chris Lattner | a11a6a0 | 2010-09-02 23:07:12 +0000 | [diff] [blame] | 102 | virtual void emitBasicBlockEndAnnot(const BasicBlock *BB, |
| 103 | formatted_raw_ostream &OS) { |
Chris Lattner | 8c2730e | 2004-03-08 20:04:32 +0000 | [diff] [blame] | 104 | // Figure out how many times each successor executed. |
Daniel Dunbar | 3cdfb4a | 2009-08-13 01:55:43 +0000 | [diff] [blame] | 105 | std::vector<std::pair<ProfileInfo::Edge, double> > SuccCounts; |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 106 | |
Daniel Dunbar | ee16638 | 2009-08-05 15:55:56 +0000 | [diff] [blame] | 107 | const TerminatorInst *TI = BB->getTerminator(); |
| 108 | for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) { |
| 109 | BasicBlock* Succ = TI->getSuccessor(s); |
Daniel Dunbar | c43782c | 2009-08-08 18:59:03 +0000 | [diff] [blame] | 110 | double w = ignoreMissing(PI.getEdgeWeight(std::make_pair(BB, Succ))); |
| 111 | if (w != 0) |
| 112 | SuccCounts.push_back(std::make_pair(std::make_pair(BB, Succ), w)); |
Daniel Dunbar | ee16638 | 2009-08-05 15:55:56 +0000 | [diff] [blame] | 113 | } |
Chris Lattner | 8c2730e | 2004-03-08 20:04:32 +0000 | [diff] [blame] | 114 | if (!SuccCounts.empty()) { |
| 115 | OS << "\t;;; Out-edge counts:"; |
| 116 | for (unsigned i = 0, e = SuccCounts.size(); i != e; ++i) |
Daniel Dunbar | ee16638 | 2009-08-05 15:55:56 +0000 | [diff] [blame] | 117 | OS << " [" << (SuccCounts[i]).second << " -> " |
| 118 | << (SuccCounts[i]).first.second->getName() << "]"; |
Chris Lattner | 8c2730e | 2004-03-08 20:04:32 +0000 | [diff] [blame] | 119 | OS << "\n"; |
| 120 | } |
Chris Lattner | 5e71764 | 2003-10-30 23:42:09 +0000 | [diff] [blame] | 121 | } |
| 122 | }; |
| 123 | } |
| 124 | |
Daniel Dunbar | 314fa8e | 2009-07-14 07:41:11 +0000 | [diff] [blame] | 125 | namespace { |
| 126 | /// ProfileInfoPrinterPass - Helper pass to dump the profile information for |
| 127 | /// a module. |
| 128 | // |
| 129 | // FIXME: This should move elsewhere. |
| 130 | class ProfileInfoPrinterPass : public ModulePass { |
| 131 | ProfileInfoLoader &PIL; |
| 132 | public: |
| 133 | static char ID; // Class identification, replacement for typeinfo. |
| 134 | explicit ProfileInfoPrinterPass(ProfileInfoLoader &_PIL) |
Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 135 | : ModulePass(ID), PIL(_PIL) {} |
Daniel Dunbar | 314fa8e | 2009-07-14 07:41:11 +0000 | [diff] [blame] | 136 | |
| 137 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 138 | AU.setPreservesAll(); |
| 139 | AU.addRequired<ProfileInfo>(); |
| 140 | } |
| 141 | |
| 142 | bool runOnModule(Module &M); |
| 143 | }; |
| 144 | } |
| 145 | |
| 146 | char ProfileInfoPrinterPass::ID = 0; |
| 147 | |
| 148 | bool ProfileInfoPrinterPass::runOnModule(Module &M) { |
Daniel Dunbar | ee16638 | 2009-08-05 15:55:56 +0000 | [diff] [blame] | 149 | ProfileInfo &PI = getAnalysis<ProfileInfo>(); |
Daniel Dunbar | 314fa8e | 2009-07-14 07:41:11 +0000 | [diff] [blame] | 150 | std::map<const Function *, unsigned> FuncFreqs; |
| 151 | std::map<const BasicBlock*, unsigned> BlockFreqs; |
Daniel Dunbar | ee16638 | 2009-08-05 15:55:56 +0000 | [diff] [blame] | 152 | std::map<ProfileInfo::Edge, unsigned> EdgeFreqs; |
Daniel Dunbar | 314fa8e | 2009-07-14 07:41:11 +0000 | [diff] [blame] | 153 | |
| 154 | // Output a report. Eventually, there will be multiple reports selectable on |
| 155 | // the command line, for now, just keep things simple. |
| 156 | |
| 157 | // Emit the most frequent function table... |
Daniel Dunbar | 3cdfb4a | 2009-08-13 01:55:43 +0000 | [diff] [blame] | 158 | std::vector<std::pair<Function*, double> > FunctionCounts; |
| 159 | std::vector<std::pair<BasicBlock*, double> > Counts; |
Daniel Dunbar | ee16638 | 2009-08-05 15:55:56 +0000 | [diff] [blame] | 160 | for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI) { |
Daniel Dunbar | c9008c5 | 2009-08-05 21:51:16 +0000 | [diff] [blame] | 161 | if (FI->isDeclaration()) continue; |
Daniel Dunbar | caaa493 | 2009-08-08 17:43:09 +0000 | [diff] [blame] | 162 | double w = ignoreMissing(PI.getExecutionCount(FI)); |
Daniel Dunbar | c43782c | 2009-08-08 18:59:03 +0000 | [diff] [blame] | 163 | FunctionCounts.push_back(std::make_pair(FI, w)); |
Daniel Dunbar | ee16638 | 2009-08-05 15:55:56 +0000 | [diff] [blame] | 164 | for (Function::iterator BB = FI->begin(), BBE = FI->end(); |
| 165 | BB != BBE; ++BB) { |
Daniel Dunbar | caaa493 | 2009-08-08 17:43:09 +0000 | [diff] [blame] | 166 | double w = ignoreMissing(PI.getExecutionCount(BB)); |
Daniel Dunbar | c43782c | 2009-08-08 18:59:03 +0000 | [diff] [blame] | 167 | Counts.push_back(std::make_pair(BB, w)); |
Daniel Dunbar | ee16638 | 2009-08-05 15:55:56 +0000 | [diff] [blame] | 168 | } |
| 169 | } |
Daniel Dunbar | 314fa8e | 2009-07-14 07:41:11 +0000 | [diff] [blame] | 170 | |
| 171 | // Sort by the frequency, backwards. |
| 172 | sort(FunctionCounts.begin(), FunctionCounts.end(), |
| 173 | PairSecondSortReverse<Function*>()); |
| 174 | |
Daniel Dunbar | 3cdfb4a | 2009-08-13 01:55:43 +0000 | [diff] [blame] | 175 | double TotalExecutions = 0; |
Daniel Dunbar | 314fa8e | 2009-07-14 07:41:11 +0000 | [diff] [blame] | 176 | for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i) |
| 177 | TotalExecutions += FunctionCounts[i].second; |
| 178 | |
Andreas Neustifter | 30457f2 | 2009-08-26 09:05:21 +0000 | [diff] [blame] | 179 | outs() << "===" << std::string(73, '-') << "===\n" |
| 180 | << "LLVM profiling output for execution"; |
| 181 | if (PIL.getNumExecutions() != 1) outs() << "s"; |
| 182 | outs() << ":\n"; |
Daniel Dunbar | 314fa8e | 2009-07-14 07:41:11 +0000 | [diff] [blame] | 183 | |
| 184 | for (unsigned i = 0, e = PIL.getNumExecutions(); i != e; ++i) { |
Andreas Neustifter | 30457f2 | 2009-08-26 09:05:21 +0000 | [diff] [blame] | 185 | outs() << " "; |
| 186 | if (e != 1) outs() << i+1 << ". "; |
| 187 | outs() << PIL.getExecution(i) << "\n"; |
Daniel Dunbar | 314fa8e | 2009-07-14 07:41:11 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Andreas Neustifter | 30457f2 | 2009-08-26 09:05:21 +0000 | [diff] [blame] | 190 | outs() << "\n===" << std::string(73, '-') << "===\n"; |
| 191 | outs() << "Function execution frequencies:\n\n"; |
Daniel Dunbar | 314fa8e | 2009-07-14 07:41:11 +0000 | [diff] [blame] | 192 | |
| 193 | // Print out the function frequencies... |
Andreas Neustifter | 30457f2 | 2009-08-26 09:05:21 +0000 | [diff] [blame] | 194 | outs() << " ## Frequency\n"; |
Daniel Dunbar | 314fa8e | 2009-07-14 07:41:11 +0000 | [diff] [blame] | 195 | for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i) { |
| 196 | if (FunctionCounts[i].second == 0) { |
Andreas Neustifter | 30457f2 | 2009-08-26 09:05:21 +0000 | [diff] [blame] | 197 | outs() << "\n NOTE: " << e-i << " function" |
| 198 | << (e-i-1 ? "s were" : " was") << " never executed!\n"; |
Daniel Dunbar | 314fa8e | 2009-07-14 07:41:11 +0000 | [diff] [blame] | 199 | break; |
| 200 | } |
| 201 | |
Andreas Neustifter | 30457f2 | 2009-08-26 09:05:21 +0000 | [diff] [blame] | 202 | outs() << format("%3d", i+1) << ". " |
| 203 | << format("%5.2g", FunctionCounts[i].second) << "/" |
| 204 | << format("%g", TotalExecutions) << " " |
Daniel Dunbar | d5b385c | 2009-07-25 00:43:31 +0000 | [diff] [blame] | 205 | << FunctionCounts[i].first->getNameStr() << "\n"; |
Daniel Dunbar | 314fa8e | 2009-07-14 07:41:11 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | std::set<Function*> FunctionsToPrint; |
| 209 | |
Daniel Dunbar | ee16638 | 2009-08-05 15:55:56 +0000 | [diff] [blame] | 210 | TotalExecutions = 0; |
| 211 | for (unsigned i = 0, e = Counts.size(); i != e; ++i) |
| 212 | TotalExecutions += Counts[i].second; |
| 213 | |
| 214 | // Sort by the frequency, backwards. |
| 215 | sort(Counts.begin(), Counts.end(), |
| 216 | PairSecondSortReverse<BasicBlock*>()); |
| 217 | |
Andreas Neustifter | 30457f2 | 2009-08-26 09:05:21 +0000 | [diff] [blame] | 218 | outs() << "\n===" << std::string(73, '-') << "===\n"; |
| 219 | outs() << "Top 20 most frequently executed basic blocks:\n\n"; |
Daniel Dunbar | ee16638 | 2009-08-05 15:55:56 +0000 | [diff] [blame] | 220 | |
| 221 | // Print out the function frequencies... |
Andreas Neustifter | 30457f2 | 2009-08-26 09:05:21 +0000 | [diff] [blame] | 222 | outs() <<" ## %% \tFrequency\n"; |
Daniel Dunbar | ee16638 | 2009-08-05 15:55:56 +0000 | [diff] [blame] | 223 | unsigned BlocksToPrint = Counts.size(); |
| 224 | if (BlocksToPrint > 20) BlocksToPrint = 20; |
| 225 | for (unsigned i = 0; i != BlocksToPrint; ++i) { |
| 226 | if (Counts[i].second == 0) break; |
| 227 | Function *F = Counts[i].first->getParent(); |
Andreas Neustifter | 30457f2 | 2009-08-26 09:05:21 +0000 | [diff] [blame] | 228 | outs() << format("%3d", i+1) << ". " |
| 229 | << format("%5g", Counts[i].second/(double)TotalExecutions*100) << "% " |
| 230 | << format("%5.0f", Counts[i].second) << "/" |
| 231 | << format("%g", TotalExecutions) << "\t" |
| 232 | << F->getNameStr() << "() - " |
| 233 | << Counts[i].first->getNameStr() << "\n"; |
Daniel Dunbar | ee16638 | 2009-08-05 15:55:56 +0000 | [diff] [blame] | 234 | FunctionsToPrint.insert(F); |
Daniel Dunbar | 314fa8e | 2009-07-14 07:41:11 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | if (PrintAnnotatedLLVM || PrintAllCode) { |
Andreas Neustifter | 30457f2 | 2009-08-26 09:05:21 +0000 | [diff] [blame] | 238 | outs() << "\n===" << std::string(73, '-') << "===\n"; |
| 239 | outs() << "Annotated LLVM code for the module:\n\n"; |
Daniel Dunbar | ee16638 | 2009-08-05 15:55:56 +0000 | [diff] [blame] | 240 | |
| 241 | ProfileAnnotator PA(PI); |
Daniel Dunbar | 314fa8e | 2009-07-14 07:41:11 +0000 | [diff] [blame] | 242 | |
| 243 | if (FunctionsToPrint.empty() || PrintAllCode) |
Chris Lattner | 79c5d3f | 2009-08-23 04:52:46 +0000 | [diff] [blame] | 244 | M.print(outs(), &PA); |
Daniel Dunbar | 314fa8e | 2009-07-14 07:41:11 +0000 | [diff] [blame] | 245 | else |
| 246 | // Print just a subset of the functions. |
| 247 | for (std::set<Function*>::iterator I = FunctionsToPrint.begin(), |
| 248 | E = FunctionsToPrint.end(); I != E; ++I) |
Chris Lattner | bdff548 | 2009-08-23 04:37:46 +0000 | [diff] [blame] | 249 | (*I)->print(outs(), &PA); |
Daniel Dunbar | 314fa8e | 2009-07-14 07:41:11 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | return false; |
| 253 | } |
Chris Lattner | 7a78d81 | 2003-10-28 21:08:18 +0000 | [diff] [blame] | 254 | |
Chris Lattner | 6f82d07 | 2003-10-28 19:16:35 +0000 | [diff] [blame] | 255 | int main(int argc, char **argv) { |
Chris Lattner | cc14d25 | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 256 | // Print a stack trace if we signal out. |
| 257 | sys::PrintStackTraceOnErrorSignal(); |
| 258 | PrettyStackTraceProgram X(argc, argv); |
Owen Anderson | 8b477ed | 2009-07-01 16:58:40 +0000 | [diff] [blame] | 259 | |
Owen Anderson | 0d7c695 | 2009-07-15 22:16:10 +0000 | [diff] [blame] | 260 | LLVMContext &Context = getGlobalContext(); |
Chris Lattner | cc14d25 | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 261 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
Daniel Dunbar | 314fa8e | 2009-07-14 07:41:11 +0000 | [diff] [blame] | 262 | |
Chris Lattner | 42a8832 | 2009-10-22 00:50:24 +0000 | [diff] [blame] | 263 | cl::ParseCommandLineOptions(argc, argv, "llvm profile dump decoder\n"); |
| 264 | |
| 265 | // Read in the bitcode file... |
| 266 | std::string ErrorMessage; |
Michael J. Spencer | 3ff9563 | 2010-12-16 03:29:14 +0000 | [diff] [blame^] | 267 | OwningPtr<MemoryBuffer> Buffer; |
Michael J. Spencer | 333fb04 | 2010-12-09 17:36:48 +0000 | [diff] [blame] | 268 | error_code ec; |
Chris Lattner | 42a8832 | 2009-10-22 00:50:24 +0000 | [diff] [blame] | 269 | Module *M = 0; |
Michael J. Spencer | 3ff9563 | 2010-12-16 03:29:14 +0000 | [diff] [blame^] | 270 | if (!(ec = MemoryBuffer::getFileOrSTDIN(BitcodeFile, Buffer))) { |
| 271 | M = ParseBitcodeFile(Buffer.get(), Context, &ErrorMessage); |
Michael J. Spencer | 333fb04 | 2010-12-09 17:36:48 +0000 | [diff] [blame] | 272 | } else |
| 273 | ErrorMessage = ec.message(); |
Chris Lattner | 42a8832 | 2009-10-22 00:50:24 +0000 | [diff] [blame] | 274 | if (M == 0) { |
| 275 | errs() << argv[0] << ": " << BitcodeFile << ": " |
| 276 | << ErrorMessage << "\n"; |
| 277 | return 1; |
| 278 | } |
| 279 | |
| 280 | // Read the profiling information. This is redundant since we load it again |
| 281 | // using the standard profile info provider pass, but for now this gives us |
| 282 | // access to additional information not exposed via the ProfileInfo |
| 283 | // interface. |
| 284 | ProfileInfoLoader PIL(argv[0], ProfileDataFile, *M); |
| 285 | |
| 286 | // Run the printer pass. |
| 287 | PassManager PassMgr; |
| 288 | PassMgr.add(createProfileLoaderPass(ProfileDataFile)); |
| 289 | PassMgr.add(new ProfileInfoPrinterPass(PIL)); |
| 290 | PassMgr.run(*M); |
| 291 | |
| 292 | return 0; |
Chris Lattner | 6f82d07 | 2003-10-28 19:16:35 +0000 | [diff] [blame] | 293 | } |