blob: c487da959ff416b1c6a0b4a8d6e48ce45288a8ce [file] [log] [blame]
Chris Lattner6f82d072003-10-28 19:16:35 +00001//===- 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
Chris Lattnere4367792003-10-28 20:13:07 +000016#include "ProfileInfo.h"
Chris Lattner7a78d812003-10-28 21:08:18 +000017#include "llvm/Function.h"
Chris Lattner6f82d072003-10-28 19:16:35 +000018#include "llvm/Bytecode/Reader.h"
19#include "Support/CommandLine.h"
Chris Lattnere4367792003-10-28 20:13:07 +000020#include <iostream>
Chris Lattner7a78d812003-10-28 21:08:18 +000021#include <cstdio>
Chris Lattner33f1ca72003-10-28 21:25:23 +000022#include <map>
Chris Lattner6f82d072003-10-28 19:16:35 +000023
24namespace {
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
Chris Lattner7a78d812003-10-28 21:08:18 +000034// PairSecondSort - A sorting predicate to sort by the second element of a pair.
35template<class T>
36struct 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
Chris Lattner6f82d072003-10-28 19:16:35 +000046int main(int argc, char **argv) {
47 cl::ParseCommandLineOptions(argc, argv, " llvm profile dump decoder\n");
Chris Lattner6f82d072003-10-28 19:16:35 +000048
Chris Lattnere4367792003-10-28 20:13:07 +000049 // Read in the bytecode file...
50 std::string ErrorMessage;
Chris Lattner7a78d812003-10-28 21:08:18 +000051 Module *M = ParseBytecodeFile(BytecodeFile, &ErrorMessage);
52 if (M == 0) {
Chris Lattnere4367792003-10-28 20:13:07 +000053 std::cerr << argv[0] << ": " << BytecodeFile << ": " << ErrorMessage
54 << "\n";
55 return 1;
56 }
Chris Lattner6f82d072003-10-28 19:16:35 +000057
Chris Lattnere4367792003-10-28 20:13:07 +000058 // Read the profiling information
Chris Lattner7a78d812003-10-28 21:08:18 +000059 ProfileInfo PI(argv[0], ProfileDataFile, *M);
Chris Lattner6f82d072003-10-28 19:16:35 +000060
Chris Lattner7a78d812003-10-28 21:08:18 +000061 // 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
Chris Lattner4963dcf2003-10-28 22:30:37 +000076 std::cout << "===" << std::string(73, '-') << "===\n"
77 << "LLVM profiling output for:\n";
78
79 for (unsigned i = 0, e = PI.getNumExecutions(); i != e; ++i) {
80 std::cout << " ";
81 if (e != 1) std::cout << i << ". ";
82 std::cout << PI.getExecution(i) << "\n";
83 }
84
85 std::cout << "\n===" << std::string(73, '-') << "===\n";
86 std::cout << "Function execution frequencies:\n\n";
87
Chris Lattner7a78d812003-10-28 21:08:18 +000088 // Print out the function frequencies...
89 printf(" ## Frequency\n");
Chris Lattner4963dcf2003-10-28 22:30:37 +000090 for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i) {
91 if (FunctionCounts[i].second == 0) {
92 printf("\n NOTE: %d function%s never executed!\n",
93 e-i, e-i-1 ? "s were" : " was");
94 break;
95 }
96
Chris Lattner7a78d812003-10-28 21:08:18 +000097 printf("%3d. %5d/%d %s\n", i, FunctionCounts[i].second, TotalExecutions,
98 FunctionCounts[i].first->getName().c_str());
Chris Lattner4963dcf2003-10-28 22:30:37 +000099 }
Chris Lattner33f1ca72003-10-28 21:25:23 +0000100
101
102 // If we have block count information, print out the LLVM module with
103 // frequency annotations.
104 if (PI.hasAccurateBlockCounts()) {
105 std::vector<std::pair<BasicBlock*, unsigned> > Counts;
106 PI.getBlockCounts(Counts);
107 std::map<BasicBlock*, unsigned> BlockFreqs(Counts.begin(), Counts.end());
108
109 }
110
Chris Lattner6f82d072003-10-28 19:16:35 +0000111 return 0;
112}