X-Git-Url: http://plrg.eecs.uci.edu/git/?p=oota-llvm.git;a=blobdiff_plain;f=tools%2Fllvm-prof%2Fllvm-prof.cpp;h=6c340b89c65c2bc49f921ccffee9a2496d3abe9b;hp=0c6c4d7bb7826a4f9e657248252a192f342b15d5;hb=6ffce6fa9295699078f81fabb9ca32f6f22bdf09;hpb=9ccaf53ada99c63737547c0235baeb8454b04e80 diff --git a/tools/llvm-prof/llvm-prof.cpp b/tools/llvm-prof/llvm-prof.cpp index 0c6c4d7bb78..6c340b89c65 100644 --- a/tools/llvm-prof/llvm-prof.cpp +++ b/tools/llvm-prof/llvm-prof.cpp @@ -13,22 +13,24 @@ // //===----------------------------------------------------------------------===// -#include "llvm/InstrTypes.h" -#include "llvm/LLVMContext.h" -#include "llvm/Module.h" -#include "llvm/PassManager.h" -#include "llvm/Assembly/AsmAnnotationWriter.h" +#include "llvm/IR/LLVMContext.h" +#include "llvm/Analysis/Passes.h" #include "llvm/Analysis/ProfileInfo.h" #include "llvm/Analysis/ProfileInfoLoader.h" -#include "llvm/Analysis/Passes.h" +#include "llvm/Assembly/AssemblyAnnotationWriter.h" #include "llvm/Bitcode/ReaderWriter.h" +#include "llvm/IR/InstrTypes.h" +#include "llvm/IR/Module.h" +#include "llvm/PassManager.h" #include "llvm/Support/CommandLine.h" +#include "llvm/Support/Format.h" +#include "llvm/Support/FormattedStream.h" #include "llvm/Support/ManagedStatic.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/PrettyStackTrace.h" +#include "llvm/Support/Signals.h" #include "llvm/Support/raw_ostream.h" -#include "llvm/Support/Format.h" -#include "llvm/System/Signals.h" +#include "llvm/Support/system_error.h" #include #include #include @@ -75,9 +77,10 @@ namespace { class ProfileAnnotator : public AssemblyAnnotationWriter { ProfileInfo &PI; public: - ProfileAnnotator(ProfileInfo& pi) : PI(pi) {} + ProfileAnnotator(ProfileInfo &pi) : PI(pi) {} - virtual void emitFunctionAnnot(const Function *F, raw_ostream &OS) { + 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 @@ -85,7 +88,7 @@ namespace { } } virtual void emitBasicBlockStartAnnot(const BasicBlock *BB, - raw_ostream &OS) { + formatted_raw_ostream &OS) { double w = PI.getExecutionCount(BB); if (w != ProfileInfo::MissingValue) { if (w != 0) { @@ -96,7 +99,8 @@ namespace { } } - 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; @@ -127,7 +131,7 @@ namespace { ProfileInfoLoader &PIL; public: static char ID; // Class identification, replacement for typeinfo. - explicit ProfileInfoPrinterPass(ProfileInfoLoader &_PIL) + explicit ProfileInfoPrinterPass(ProfileInfoLoader &_PIL) : ModulePass(ID), PIL(_PIL) {} virtual void getAnalysisUsage(AnalysisUsage &AU) const { @@ -143,9 +147,6 @@ char ProfileInfoPrinterPass::ID = 0; bool ProfileInfoPrinterPass::runOnModule(Module &M) { ProfileInfo &PI = getAnalysis(); - std::map FuncFreqs; - std::map BlockFreqs; - std::map EdgeFreqs; // Output a report. Eventually, there will be multiple reports selectable on // the command line, for now, just keep things simple. @@ -157,7 +158,7 @@ bool ProfileInfoPrinterPass::runOnModule(Module &M) { 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(); + for (Function::iterator BB = FI->begin(), BBE = FI->end(); BB != BBE; ++BB) { double w = ignoreMissing(PI.getExecutionCount(BB)); Counts.push_back(std::make_pair(BB, w)); @@ -190,15 +191,15 @@ bool ProfileInfoPrinterPass::runOnModule(Module &M) { outs() << " ## Frequency\n"; for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i) { if (FunctionCounts[i].second == 0) { - outs() << "\n NOTE: " << e-i << " function" + outs() << "\n NOTE: " << e-i << " function" << (e-i-1 ? "s were" : " was") << " never executed!\n"; break; } outs() << format("%3d", i+1) << ". " - << format("%5.2g", FunctionCounts[i].second) << "/" - << format("%g", TotalExecutions) << " " - << FunctionCounts[i].first->getNameStr() << "\n"; + << format("%5.2g", FunctionCounts[i].second) << "/" + << format("%g", TotalExecutions) << " " + << FunctionCounts[i].first->getName() << "\n"; } std::set FunctionsToPrint; @@ -206,14 +207,14 @@ bool ProfileInfoPrinterPass::runOnModule(Module &M) { TotalExecutions = 0; for (unsigned i = 0, e = Counts.size(); i != e; ++i) TotalExecutions += Counts[i].second; - + // Sort by the frequency, backwards. sort(Counts.begin(), Counts.end(), PairSecondSortReverse()); - + outs() << "\n===" << std::string(73, '-') << "===\n"; outs() << "Top 20 most frequently executed basic blocks:\n\n"; - + // Print out the function frequencies... outs() <<" ## %% \tFrequency\n"; unsigned BlocksToPrint = Counts.size(); @@ -221,19 +222,19 @@ bool ProfileInfoPrinterPass::runOnModule(Module &M) { for (unsigned i = 0; i != BlocksToPrint; ++i) { if (Counts[i].second == 0) break; Function *F = Counts[i].first->getParent(); - 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"; + outs() << format("%3d", i+1) << ". " + << format("%5g", Counts[i].second/(double)TotalExecutions*100)<<"% " + << format("%5.0f", Counts[i].second) << "/" + << format("%g", TotalExecutions) << "\t" + << F->getName() << "() - " + << Counts[i].first->getName() << "\n"; FunctionsToPrint.insert(F); } if (PrintAnnotatedLLVM || PrintAllCode) { outs() << "\n===" << std::string(73, '-') << "===\n"; outs() << "Annotated LLVM code for the module:\n\n"; - + ProfileAnnotator PA(PI); if (FunctionsToPrint.empty() || PrintAllCode) @@ -255,17 +256,18 @@ int main(int argc, char **argv) { LLVMContext &Context = getGlobalContext(); llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. - + 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 (MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(BitcodeFile, - &ErrorMessage)) { - M = ParseBitcodeFile(Buffer, Context, &ErrorMessage); - delete Buffer; - } + 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"; @@ -276,7 +278,7 @@ int main(int argc, char **argv) { // 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); + ProfileInfoLoader PIL(argv[0], ProfileDataFile); // Run the printer pass. PassManager PassMgr;