1 //===- llvm-prof.cpp - Read in and process llvmprof.out data files --------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
16 #include "ProfileInfo.h"
17 #include "llvm/Function.h"
18 #include "llvm/Bytecode/Reader.h"
19 #include "Support/CommandLine.h"
25 BytecodeFile(cl::Positional, cl::desc("<program bytecode file>"),
29 ProfileDataFile(cl::Positional, cl::desc("<llvmprof.out file>"),
30 cl::Optional, cl::init("llvmprof.out"));
33 // PairSecondSort - A sorting predicate to sort by the second element of a pair.
36 : public std::binary_function<std::pair<T, unsigned>,
37 std::pair<T, unsigned>, bool> {
38 bool operator()(const std::pair<T, unsigned> &LHS,
39 const std::pair<T, unsigned> &RHS) const {
40 return LHS.second < RHS.second;
45 int main(int argc, char **argv) {
46 cl::ParseCommandLineOptions(argc, argv, " llvm profile dump decoder\n");
48 // Read in the bytecode file...
49 std::string ErrorMessage;
50 Module *M = ParseBytecodeFile(BytecodeFile, &ErrorMessage);
52 std::cerr << argv[0] << ": " << BytecodeFile << ": " << ErrorMessage
57 // Read the profiling information
58 ProfileInfo PI(argv[0], ProfileDataFile, *M);
60 // Output a report. Eventually, there will be multiple reports selectable on
61 // the command line, for now, just keep things simple.
63 // Emit the most frequent function table...
64 std::vector<std::pair<Function*, unsigned> > FunctionCounts;
65 PI.getFunctionCounts(FunctionCounts);
67 // Sort by the frequency, backwards.
68 std::sort(FunctionCounts.begin(), FunctionCounts.end(),
69 std::not2(PairSecondSort<Function*>()));
71 unsigned TotalExecutions = 0;
72 for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i)
73 TotalExecutions += FunctionCounts[i].second;
75 // Print out the function frequencies...
76 printf(" ## Frequency\n");
77 for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i)
78 printf("%3d. %5d/%d %s\n", i, FunctionCounts[i].second, TotalExecutions,
79 FunctionCounts[i].first->getName().c_str());