Add support for reading block frequencies. Fix bug in attribution of counts
[oota-llvm.git] / tools / llvm-prof / llvm-prof.cpp
1 //===- llvm-prof.cpp - Read in and process llvmprof.out data files --------===//
2 // 
3 //                      The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
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
16 #include "ProfileInfo.h"
17 #include "llvm/Function.h"
18 #include "llvm/Bytecode/Reader.h"
19 #include "Support/CommandLine.h"
20 #include <iostream>
21 #include <cstdio>
22 #include <map>
23
24 namespace {
25   cl::opt<std::string> 
26   BytecodeFile(cl::Positional, cl::desc("<program bytecode file>"),
27                cl::Required);
28
29   cl::opt<std::string> 
30   ProfileDataFile(cl::Positional, cl::desc("<llvmprof.out file>"),
31                   cl::Optional, cl::init("llvmprof.out"));
32 }
33
34 // PairSecondSort - A sorting predicate to sort by the second element of a pair.
35 template<class T>
36 struct PairSecondSort
37   : public std::binary_function<std::pair<T, unsigned>,
38                                 std::pair<T, unsigned>, bool> {
39   bool operator()(const std::pair<T, unsigned> &LHS,
40                   const std::pair<T, unsigned> &RHS) const {
41     return LHS.second < RHS.second;
42   }
43 };
44
45
46 int main(int argc, char **argv) {
47   cl::ParseCommandLineOptions(argc, argv, " llvm profile dump decoder\n");
48
49   // Read in the bytecode file...
50   std::string ErrorMessage;
51   Module *M = ParseBytecodeFile(BytecodeFile, &ErrorMessage);
52   if (M == 0) {
53     std::cerr << argv[0] << ": " << BytecodeFile << ": " << ErrorMessage
54               << "\n";
55     return 1;
56   }
57
58   // Read the profiling information
59   ProfileInfo PI(argv[0], ProfileDataFile, *M);
60
61   // Output a report.  Eventually, there will be multiple reports selectable on
62   // the command line, for now, just keep things simple.
63
64   // Emit the most frequent function table...
65   std::vector<std::pair<Function*, unsigned> > FunctionCounts;
66   PI.getFunctionCounts(FunctionCounts);
67
68   // Sort by the frequency, backwards.
69   std::sort(FunctionCounts.begin(), FunctionCounts.end(),
70             std::not2(PairSecondSort<Function*>()));
71
72   unsigned TotalExecutions = 0;
73   for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i)
74     TotalExecutions += FunctionCounts[i].second;
75   
76   // Print out the function frequencies...
77   printf(" ##   Frequency\n");
78   for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i)
79     printf("%3d. %5d/%d %s\n", i, FunctionCounts[i].second, TotalExecutions,
80            FunctionCounts[i].first->getName().c_str());
81
82
83   // If we have block count information, print out the LLVM module with
84   // frequency annotations.
85   if (PI.hasAccurateBlockCounts()) {
86     std::vector<std::pair<BasicBlock*, unsigned> > Counts;
87     PI.getBlockCounts(Counts);
88     std::map<BasicBlock*, unsigned> BlockFreqs(Counts.begin(), Counts.end());
89                    
90   }
91
92   return 0;
93 }