From: Chris Lattner Date: Mon, 8 Mar 2004 20:04:32 +0000 (+0000) Subject: Annotate functions with edge counts as well, if they are available. X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=8c2730e347092e2c62ddb8442252e694bc498aa0;p=oota-llvm.git Annotate functions with edge counts as well, if they are available. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@12233 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/tools/llvm-prof/llvm-prof.cpp b/tools/llvm-prof/llvm-prof.cpp index 2a7789dcd53..3288a4d811d 100644 --- a/tools/llvm-prof/llvm-prof.cpp +++ b/tools/llvm-prof/llvm-prof.cpp @@ -13,6 +13,7 @@ // //===----------------------------------------------------------------------===// +#include "llvm/InstrTypes.h" #include "llvm/Module.h" #include "llvm/Assembly/AsmAnnotationWriter.h" #include "llvm/Analysis/ProfileInfoLoader.h" @@ -59,21 +60,46 @@ namespace { class ProfileAnnotator : public AssemblyAnnotationWriter { std::map &FuncFreqs; std::map &BlockFreqs; + std::map &EdgeFreqs; public: ProfileAnnotator(std::map &FF, - std::map &BF) - : FuncFreqs(FF), BlockFreqs(BF) {} + std::map &BF, + std::map &EF) + : FuncFreqs(FF), BlockFreqs(BF), EdgeFreqs(EF) {} virtual void emitFunctionAnnot(const Function *F, std::ostream &OS) { OS << ";;; %" << F->getName() << " called " << FuncFreqs[F] << " times.\n;;;\n"; } - virtual void emitBasicBlockAnnot(const BasicBlock *BB, std::ostream &OS) { + virtual void emitBasicBlockStartAnnot(const BasicBlock *BB, + std::ostream &OS) { if (BlockFreqs.empty()) return; if (unsigned Count = BlockFreqs[BB]) - OS << ";;; Executed " << Count << " times.\n"; + OS << "\t;;; Basic block executed " << Count << " times.\n"; else - OS << ";;; Never executed!\n"; + OS << "\t;;; Never executed!\n"; + } + + virtual void emitBasicBlockEndAnnot(const BasicBlock *BB, std::ostream &OS){ + if (EdgeFreqs.empty()) return; + + // Figure out how many times each successor executed. + std::vector > SuccCounts; + const TerminatorInst *TI = BB->getTerminator(); + + std::map::iterator I = + EdgeFreqs.lower_bound(std::make_pair(const_cast(BB), 0U)); + for (; I != EdgeFreqs.end() && I->first.first == BB; ++I) + if (I->second) + SuccCounts.push_back(std::make_pair(TI->getSuccessor(I->first.second), + I->second)); + if (!SuccCounts.empty()) { + OS << "\t;;; Out-edge counts:"; + for (unsigned i = 0, e = SuccCounts.size(); i != e; ++i) + OS << " [" << SuccCounts[i].second << " -> " + << SuccCounts[i].first->getName() << "]"; + OS << "\n"; + } } }; } @@ -97,6 +123,7 @@ int main(int argc, char **argv) { 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. @@ -177,11 +204,17 @@ int main(int argc, char **argv) { BlockFreqs.insert(Counts.begin(), Counts.end()); } + if (PI.hasAccurateEdgeCounts()) { + std::vector > Counts; + PI.getEdgeCounts(Counts); + EdgeFreqs.insert(Counts.begin(), Counts.end()); + } + if (PrintAnnotatedLLVM || PrintAllCode) { std::cout << "\n===" << std::string(73, '-') << "===\n"; std::cout << "Annotated LLVM code for the module:\n\n"; - ProfileAnnotator PA(FuncFreqs, BlockFreqs); + ProfileAnnotator PA(FuncFreqs, BlockFreqs, EdgeFreqs); if (FunctionsToPrint.empty() || PrintAllCode) M->print(std::cout, &PA);