X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=tools%2Fllvm-prof%2Fllvm-prof.cpp;h=9d0b46833befa606f2ad581f61e849bc3bdfceb4;hb=adf01b3f18442ae8db6b8948e70d82d9df415119;hp=64fe45150064aa795649c74c3b7d8c0a40c8e101;hpb=ee16638bfc9c17068c4b3c2dc130277785a11e20;p=oota-llvm.git diff --git a/tools/llvm-prof/llvm-prof.cpp b/tools/llvm-prof/llvm-prof.cpp index 64fe4515006..9d0b46833be 100644 --- a/tools/llvm-prof/llvm-prof.cpp +++ b/tools/llvm-prof/llvm-prof.cpp @@ -17,19 +17,21 @@ #include "llvm/LLVMContext.h" #include "llvm/Module.h" #include "llvm/PassManager.h" -#include "llvm/Assembly/AsmAnnotationWriter.h" +#include "llvm/Assembly/AssemblyAnnotationWriter.h" #include "llvm/Analysis/ProfileInfo.h" #include "llvm/Analysis/ProfileInfoLoader.h" #include "llvm/Analysis/Passes.h" #include "llvm/Bitcode/ReaderWriter.h" #include "llvm/Support/CommandLine.h" +#include "llvm/Support/FormattedStream.h" #include "llvm/Support/ManagedStatic.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/PrettyStackTrace.h" #include "llvm/Support/raw_ostream.h" -#include "llvm/System/Signals.h" +#include "llvm/Support/Format.h" +#include "llvm/Support/Signals.h" +#include "llvm/Support/system_error.h" #include -#include #include #include #include @@ -58,42 +60,56 @@ namespace { // PairSecondSort - A sorting predicate to sort by the second element of a pair. template struct PairSecondSortReverse - : public std::binary_function, - std::pair, bool> { - bool operator()(const std::pair &LHS, - const std::pair &RHS) const { + : public std::binary_function, + std::pair, bool> { + bool operator()(const std::pair &LHS, + const std::pair &RHS) const { return LHS.second > RHS.second; } }; +static double ignoreMissing(double w) { + if (w == ProfileInfo::MissingValue) return 0; + return w; +} + namespace { class ProfileAnnotator : public AssemblyAnnotationWriter { ProfileInfo &PI; public: - ProfileAnnotator(ProfileInfo& pi) : PI(pi) {} - - virtual void emitFunctionAnnot(const Function *F, raw_ostream &OS) { - OS << ";;; %" << F->getName() << " called " << PI.getExecutionCount(F) - << " times.\n;;;\n"; + ProfileAnnotator(ProfileInfo &pi) : PI(pi) {} + + virtual void emitFunctionAnnot(const Function *F, + formatted_raw_ostream &OS) { + double w = PI.getExecutionCount(F); + if (w != ProfileInfo::MissingValue) { + OS << ";;; %" << F->getName() << " called "<<(unsigned)w + <<" times.\n;;;\n"; + } } virtual void emitBasicBlockStartAnnot(const BasicBlock *BB, - raw_ostream &OS) { - unsigned w = PI.getExecutionCount(BB); - if (w != 0) - OS << "\t;;; Basic block executed " << w << " times.\n"; - else - OS << "\t;;; Never executed!\n"; + formatted_raw_ostream &OS) { + double w = PI.getExecutionCount(BB); + if (w != ProfileInfo::MissingValue) { + if (w != 0) { + OS << "\t;;; Basic block executed " << (unsigned)w << " times.\n"; + } else { + OS << "\t;;; Never executed!\n"; + } + } } - virtual void emitBasicBlockEndAnnot(const BasicBlock *BB, raw_ostream &OS) { + virtual void emitBasicBlockEndAnnot(const BasicBlock *BB, + formatted_raw_ostream &OS) { // Figure out how many times each successor executed. - std::vector > SuccCounts; + std::vector > SuccCounts; const TerminatorInst *TI = BB->getTerminator(); for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) { BasicBlock* Succ = TI->getSuccessor(s); - SuccCounts.push_back(std::make_pair(std::make_pair(BB,Succ), - PI.getEdgeWeight(BB,Succ))); + double w = ignoreMissing(PI.getEdgeWeight(std::make_pair(BB, Succ))); + if (w != 0) + SuccCounts.push_back(std::make_pair(std::make_pair(BB, Succ), w)); } if (!SuccCounts.empty()) { OS << "\t;;; Out-edge counts:"; @@ -116,7 +132,7 @@ namespace { public: static char ID; // Class identification, replacement for typeinfo. explicit ProfileInfoPrinterPass(ProfileInfoLoader &_PIL) - : ModulePass(&ID), PIL(_PIL) {} + : ModulePass(ID), PIL(_PIL) {} virtual void getAnalysisUsage(AnalysisUsage &AU) const { AU.setPreservesAll(); @@ -139,15 +155,16 @@ bool ProfileInfoPrinterPass::runOnModule(Module &M) { // the command line, for now, just keep things simple. // Emit the most frequent function table... - std::vector > FunctionCounts; - std::vector > Counts; + std::vector > FunctionCounts; + std::vector > Counts; for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI) { - unsigned w = PI.getExecutionCount(FI); - if (w != (unsigned) -1) - FunctionCounts.push_back(std::make_pair(FI,PI.getExecutionCount(FI))); + if (FI->isDeclaration()) continue; + double w = ignoreMissing(PI.getExecutionCount(FI)); + FunctionCounts.push_back(std::make_pair(FI, w)); for (Function::iterator BB = FI->begin(), BBE = FI->end(); BB != BBE; ++BB) { - Counts.push_back(std::make_pair(BB,PI.getExecutionCount(BB))); + double w = ignoreMissing(PI.getExecutionCount(BB)); + Counts.push_back(std::make_pair(BB, w)); } } @@ -155,36 +172,36 @@ bool ProfileInfoPrinterPass::runOnModule(Module &M) { sort(FunctionCounts.begin(), FunctionCounts.end(), PairSecondSortReverse()); - uint64_t TotalExecutions = 0; + double TotalExecutions = 0; for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i) TotalExecutions += FunctionCounts[i].second; - std::cout << "===" << std::string(73, '-') << "===\n" - << "LLVM profiling output for execution"; - if (PIL.getNumExecutions() != 1) std::cout << "s"; - std::cout << ":\n"; + outs() << "===" << std::string(73, '-') << "===\n" + << "LLVM profiling output for execution"; + if (PIL.getNumExecutions() != 1) outs() << "s"; + outs() << ":\n"; for (unsigned i = 0, e = PIL.getNumExecutions(); i != e; ++i) { - std::cout << " "; - if (e != 1) std::cout << i+1 << ". "; - std::cout << PIL.getExecution(i) << "\n"; + outs() << " "; + if (e != 1) outs() << i+1 << ". "; + outs() << PIL.getExecution(i) << "\n"; } - std::cout << "\n===" << std::string(73, '-') << "===\n"; - std::cout << "Function execution frequencies:\n\n"; + outs() << "\n===" << std::string(73, '-') << "===\n"; + outs() << "Function execution frequencies:\n\n"; // Print out the function frequencies... - std::cout << " ## Frequency\n"; + outs() << " ## Frequency\n"; for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i) { if (FunctionCounts[i].second == 0) { - std::cout << "\n NOTE: " << e-i << " function" << - (e-i-1 ? "s were" : " was") << " never executed!\n"; + outs() << "\n NOTE: " << e-i << " function" + << (e-i-1 ? "s were" : " was") << " never executed!\n"; break; } - std::cout << std::setw(3) << i+1 << ". " - << std::setw(5) << FunctionCounts[i].second << "/" - << TotalExecutions << " " + outs() << format("%3d", i+1) << ". " + << format("%5.2g", FunctionCounts[i].second) << "/" + << format("%g", TotalExecutions) << " " << FunctionCounts[i].first->getNameStr() << "\n"; } @@ -198,39 +215,38 @@ bool ProfileInfoPrinterPass::runOnModule(Module &M) { sort(Counts.begin(), Counts.end(), PairSecondSortReverse()); - std::cout << "\n===" << std::string(73, '-') << "===\n"; - std::cout << "Top 20 most frequently executed basic blocks:\n\n"; + outs() << "\n===" << std::string(73, '-') << "===\n"; + outs() << "Top 20 most frequently executed basic blocks:\n\n"; // Print out the function frequencies... - std::cout <<" ## %% \tFrequency\n"; + outs() <<" ## %% \tFrequency\n"; unsigned BlocksToPrint = Counts.size(); if (BlocksToPrint > 20) BlocksToPrint = 20; for (unsigned i = 0; i != BlocksToPrint; ++i) { if (Counts[i].second == 0) break; Function *F = Counts[i].first->getParent(); - std::cout << std::setw(3) << i+1 << ". " - << std::setw(5) << std::setprecision(2) - << Counts[i].second/(double)TotalExecutions*100 << "% " - << std::setw(5) << Counts[i].second << "/" - << TotalExecutions << "\t" - << F->getNameStr() << "() - " - << Counts[i].first->getNameStr() << "\n"; + outs() << format("%3d", i+1) << ". " + << format("%5g", Counts[i].second/(double)TotalExecutions*100) << "% " + << format("%5.0f", Counts[i].second) << "/" + << format("%g", TotalExecutions) << "\t" + << F->getNameStr() << "() - " + << Counts[i].first->getNameStr() << "\n"; FunctionsToPrint.insert(F); } if (PrintAnnotatedLLVM || PrintAllCode) { - std::cout << "\n===" << std::string(73, '-') << "===\n"; - std::cout << "Annotated LLVM code for the module:\n\n"; + outs() << "\n===" << std::string(73, '-') << "===\n"; + outs() << "Annotated LLVM code for the module:\n\n"; ProfileAnnotator PA(PI); if (FunctionsToPrint.empty() || PrintAllCode) - M.print(std::cout, &PA); + M.print(outs(), &PA); else // Print just a subset of the functions. for (std::set::iterator I = FunctionsToPrint.begin(), E = FunctionsToPrint.end(); I != E; ++I) - (*I)->print(std::cout, &PA); + (*I)->print(outs(), &PA); } return false; @@ -243,41 +259,35 @@ int main(int argc, char **argv) { LLVMContext &Context = getGlobalContext(); llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. - try { - cl::ParseCommandLineOptions(argc, argv, "llvm profile dump decoder\n"); - - // Read in the bitcode file... - std::string ErrorMessage; - Module *M = 0; - if (MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(BitcodeFile, - &ErrorMessage)) { - M = ParseBitcodeFile(Buffer, Context, &ErrorMessage); - delete Buffer; - } - if (M == 0) { - errs() << argv[0] << ": " << BitcodeFile << ": " - << ErrorMessage << "\n"; - return 1; - } - - // Read the profiling information. This is redundant since we load it again - // using the standard profile info provider pass, but for now this gives us - // access to additional information not exposed via the ProfileInfo - // interface. - ProfileInfoLoader PIL(argv[0], ProfileDataFile, *M); - - // Run the printer pass. - PassManager PassMgr; - PassMgr.add(createProfileLoaderPass(ProfileDataFile)); - PassMgr.add(new ProfileInfoPrinterPass(PIL)); - PassMgr.run(*M); - - return 0; - } catch (const std::string& msg) { - errs() << argv[0] << ": " << msg << "\n"; - } catch (...) { - errs() << argv[0] << ": Unexpected unknown exception occurred.\n"; - } - return 1; + cl::ParseCommandLineOptions(argc, argv, "llvm profile dump decoder\n"); + + // Read in the bitcode file... + std::string ErrorMessage; + OwningPtr Buffer; + error_code ec; + Module *M = 0; + if (!(ec = MemoryBuffer::getFileOrSTDIN(BitcodeFile, Buffer))) { + M = ParseBitcodeFile(Buffer.get(), Context, &ErrorMessage); + } else + ErrorMessage = ec.message(); + if (M == 0) { + errs() << argv[0] << ": " << BitcodeFile << ": " + << ErrorMessage << "\n"; + return 1; + } + + // Read the profiling information. This is redundant since we load it again + // using the standard profile info provider pass, but for now this gives us + // access to additional information not exposed via the ProfileInfo + // interface. + ProfileInfoLoader PIL(argv[0], ProfileDataFile, *M); + + // Run the printer pass. + PassManager PassMgr; + PassMgr.add(createProfileLoaderPass(ProfileDataFile)); + PassMgr.add(new ProfileInfoPrinterPass(PIL)); + PassMgr.run(*M); + + return 0; }