From 18884a86ae337b6ce9636738c069a87004d43017 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Wed, 29 Oct 2003 21:41:17 +0000 Subject: [PATCH] Print the top 20 most frequently executed blocks. Fix sort predicate problem git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@9594 91177308-0d34-0410-b5e6-96231b3b80d8 --- tools/llvm-prof/llvm-prof.cpp | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/tools/llvm-prof/llvm-prof.cpp b/tools/llvm-prof/llvm-prof.cpp index ba09c898a4e..083e1ce1700 100644 --- a/tools/llvm-prof/llvm-prof.cpp +++ b/tools/llvm-prof/llvm-prof.cpp @@ -33,12 +33,12 @@ namespace { // PairSecondSort - A sorting predicate to sort by the second element of a pair. template -struct PairSecondSort +struct PairSecondSortReverse : public std::binary_function, std::pair, bool> { bool operator()(const std::pair &LHS, const std::pair &RHS) const { - return LHS.second < RHS.second; + return LHS.second > RHS.second; } }; @@ -67,7 +67,7 @@ int main(int argc, char **argv) { // Sort by the frequency, backwards. std::sort(FunctionCounts.begin(), FunctionCounts.end(), - std::not2(PairSecondSort())); + PairSecondSortReverse()); unsigned TotalExecutions = 0; for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i) @@ -80,7 +80,7 @@ int main(int argc, char **argv) { for (unsigned i = 0, e = PI.getNumExecutions(); i != e; ++i) { std::cout << " "; - if (e != 1) std::cout << i << ". "; + if (e != 1) std::cout << i+1 << ". "; std::cout << PI.getExecution(i) << "\n"; } @@ -96,7 +96,7 @@ int main(int argc, char **argv) { break; } - printf("%3d. %5d/%d %s\n", i, FunctionCounts[i].second, TotalExecutions, + printf("%3d. %5d/%d %s\n", i+1, FunctionCounts[i].second, TotalExecutions, FunctionCounts[i].first->getName().c_str()); } @@ -106,8 +106,29 @@ int main(int argc, char **argv) { if (PI.hasAccurateBlockCounts()) { std::vector > Counts; PI.getBlockCounts(Counts); + + TotalExecutions = 0; + for (unsigned i = 0, e = Counts.size(); i != e; ++i) + TotalExecutions += Counts[i].second; + + // Sort by the frequency, backwards. + std::sort(Counts.begin(), Counts.end(), + PairSecondSortReverse()); + + std::cout << "\n===" << std::string(73, '-') << "===\n"; + std::cout << "Top 20 most frequently executed basic blocks:\n\n"; + + // Print out the function frequencies... + printf(" ## Frequency\n"); + unsigned BlocksToPrint = Counts.size(); + if (BlocksToPrint > 20) BlocksToPrint = 20; + for (unsigned i = 0; i != BlocksToPrint; ++i) + printf("%3d. %5d/%d %s - %s\n", i+1, Counts[i].second, TotalExecutions, + Counts[i].first->getParent()->getName().c_str(), + Counts[i].first->getName().c_str()); + std::map BlockFreqs(Counts.begin(), Counts.end()); - + } return 0; -- 2.34.1