1 //===- llvm-prof.cpp - Read in and process llvmprof.out data files --------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // 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 "llvm/InstrTypes.h"
17 #include "llvm/Module.h"
18 #include "llvm/Assembly/AsmAnnotationWriter.h"
19 #include "llvm/Analysis/ProfileInfoLoader.h"
20 #include "llvm/Bitcode/ReaderWriter.h"
21 #include "llvm/Support/CommandLine.h"
22 #include "llvm/Support/ManagedStatic.h"
23 #include "llvm/Support/MemoryBuffer.h"
24 #include "llvm/System/Signals.h"
35 BitcodeFile(cl::Positional
, cl::desc("<program bitcode file>"),
39 ProfileDataFile(cl::Positional
, cl::desc("<llvmprof.out file>"),
40 cl::Optional
, cl::init("llvmprof.out"));
43 PrintAnnotatedLLVM("annotated-llvm",
44 cl::desc("Print LLVM code with frequency annotations"));
45 cl::alias
PrintAnnotated2("A", cl::desc("Alias for --annotated-llvm"),
46 cl::aliasopt(PrintAnnotatedLLVM
));
48 PrintAllCode("print-all-code",
49 cl::desc("Print annotated code for the entire program"));
52 // PairSecondSort - A sorting predicate to sort by the second element of a pair.
54 struct PairSecondSortReverse
55 : public std::binary_function
<std::pair
<T
, unsigned>,
56 std::pair
<T
, unsigned>, bool> {
57 bool operator()(const std::pair
<T
, unsigned> &LHS
,
58 const std::pair
<T
, unsigned> &RHS
) const {
59 return LHS
.second
> RHS
.second
;
64 class ProfileAnnotator
: public AssemblyAnnotationWriter
{
65 std::map
<const Function
*, unsigned> &FuncFreqs
;
66 std::map
<const BasicBlock
*, unsigned> &BlockFreqs
;
67 std::map
<ProfileInfoLoader::Edge
, unsigned> &EdgeFreqs
;
69 ProfileAnnotator(std::map
<const Function
*, unsigned> &FF
,
70 std::map
<const BasicBlock
*, unsigned> &BF
,
71 std::map
<ProfileInfoLoader::Edge
, unsigned> &EF
)
72 : FuncFreqs(FF
), BlockFreqs(BF
), EdgeFreqs(EF
) {}
74 virtual void emitFunctionAnnot(const Function
*F
, std::ostream
&OS
) {
75 OS
<< ";;; %" << F
->getName() << " called " << FuncFreqs
[F
]
78 virtual void emitBasicBlockStartAnnot(const BasicBlock
*BB
,
80 if (BlockFreqs
.empty()) return;
81 if (unsigned Count
= BlockFreqs
[BB
])
82 OS
<< "\t;;; Basic block executed " << Count
<< " times.\n";
84 OS
<< "\t;;; Never executed!\n";
87 virtual void emitBasicBlockEndAnnot(const BasicBlock
*BB
, std::ostream
&OS
){
88 if (EdgeFreqs
.empty()) return;
90 // Figure out how many times each successor executed.
91 std::vector
<std::pair
<const BasicBlock
*, unsigned> > SuccCounts
;
92 const TerminatorInst
*TI
= BB
->getTerminator();
94 std::map
<ProfileInfoLoader::Edge
, unsigned>::iterator I
=
95 EdgeFreqs
.lower_bound(std::make_pair(const_cast<BasicBlock
*>(BB
), 0U));
96 for (; I
!= EdgeFreqs
.end() && I
->first
.first
== BB
; ++I
)
98 SuccCounts
.push_back(std::make_pair(TI
->getSuccessor(I
->first
.second
),
100 if (!SuccCounts
.empty()) {
101 OS
<< "\t;;; Out-edge counts:";
102 for (unsigned i
= 0, e
= SuccCounts
.size(); i
!= e
; ++i
)
103 OS
<< " [" << SuccCounts
[i
].second
<< " -> "
104 << SuccCounts
[i
].first
->getName() << "]";
112 int main(int argc
, char **argv
) {
113 llvm_shutdown_obj X
; // Call llvm_shutdown() on exit.
115 cl::ParseCommandLineOptions(argc
, argv
, "llvm profile dump decoder\n");
116 sys::PrintStackTraceOnErrorSignal();
118 // Read in the bitcode file...
119 std::string ErrorMessage
;
121 if (MemoryBuffer
*Buffer
= MemoryBuffer::getFileOrSTDIN(BitcodeFile
,
123 M
= ParseBitcodeFile(Buffer
, &ErrorMessage
);
127 std::cerr
<< argv
[0] << ": " << BitcodeFile
<< ": "
128 << ErrorMessage
<< "\n";
132 // Read the profiling information
133 ProfileInfoLoader
PI(argv
[0], ProfileDataFile
, *M
);
135 std::map
<const Function
*, unsigned> FuncFreqs
;
136 std::map
<const BasicBlock
*, unsigned> BlockFreqs
;
137 std::map
<ProfileInfoLoader::Edge
, unsigned> EdgeFreqs
;
139 // Output a report. Eventually, there will be multiple reports selectable on
140 // the command line, for now, just keep things simple.
142 // Emit the most frequent function table...
143 std::vector
<std::pair
<Function
*, unsigned> > FunctionCounts
;
144 PI
.getFunctionCounts(FunctionCounts
);
145 FuncFreqs
.insert(FunctionCounts
.begin(), FunctionCounts
.end());
147 // Sort by the frequency, backwards.
148 sort(FunctionCounts
.begin(), FunctionCounts
.end(),
149 PairSecondSortReverse
<Function
*>());
151 uint64_t TotalExecutions
= 0;
152 for (unsigned i
= 0, e
= FunctionCounts
.size(); i
!= e
; ++i
)
153 TotalExecutions
+= FunctionCounts
[i
].second
;
155 std::cout
<< "===" << std::string(73, '-') << "===\n"
156 << "LLVM profiling output for execution";
157 if (PI
.getNumExecutions() != 1) std::cout
<< "s";
160 for (unsigned i
= 0, e
= PI
.getNumExecutions(); i
!= e
; ++i
) {
162 if (e
!= 1) std::cout
<< i
+1 << ". ";
163 std::cout
<< PI
.getExecution(i
) << "\n";
166 std::cout
<< "\n===" << std::string(73, '-') << "===\n";
167 std::cout
<< "Function execution frequencies:\n\n";
169 // Print out the function frequencies...
170 std::cout
<< " ## Frequency\n";
171 for (unsigned i
= 0, e
= FunctionCounts
.size(); i
!= e
; ++i
) {
172 if (FunctionCounts
[i
].second
== 0) {
173 std::cout
<< "\n NOTE: " << e
-i
<< " function" <<
174 (e
-i
-1 ? "s were" : " was") << " never executed!\n";
178 std::cout
<< std::setw(3) << i
+1 << ". "
179 << std::setw(5) << FunctionCounts
[i
].second
<< "/"
180 << TotalExecutions
<< " "
181 << FunctionCounts
[i
].first
->getName().c_str() << "\n";
184 std::set
<Function
*> FunctionsToPrint
;
186 // If we have block count information, print out the LLVM module with
187 // frequency annotations.
188 if (PI
.hasAccurateBlockCounts()) {
189 std::vector
<std::pair
<BasicBlock
*, unsigned> > Counts
;
190 PI
.getBlockCounts(Counts
);
193 for (unsigned i
= 0, e
= Counts
.size(); i
!= e
; ++i
)
194 TotalExecutions
+= Counts
[i
].second
;
196 // Sort by the frequency, backwards.
197 sort(Counts
.begin(), Counts
.end(),
198 PairSecondSortReverse
<BasicBlock
*>());
200 std::cout
<< "\n===" << std::string(73, '-') << "===\n";
201 std::cout
<< "Top 20 most frequently executed basic blocks:\n\n";
203 // Print out the function frequencies...
204 std::cout
<<" ## %% \tFrequency\n";
205 unsigned BlocksToPrint
= Counts
.size();
206 if (BlocksToPrint
> 20) BlocksToPrint
= 20;
207 for (unsigned i
= 0; i
!= BlocksToPrint
; ++i
) {
208 if (Counts
[i
].second
== 0) break;
209 Function
*F
= Counts
[i
].first
->getParent();
210 std::cout
<< std::setw(3) << i
+1 << ". "
211 << std::setw(5) << std::setprecision(2)
212 << Counts
[i
].second
/(double)TotalExecutions
*100 << "% "
213 << std::setw(5) << Counts
[i
].second
<< "/"
214 << TotalExecutions
<< "\t"
215 << F
->getName().c_str() << "() - "
216 << Counts
[i
].first
->getName().c_str() << "\n";
217 FunctionsToPrint
.insert(F
);
220 BlockFreqs
.insert(Counts
.begin(), Counts
.end());
223 if (PI
.hasAccurateEdgeCounts()) {
224 std::vector
<std::pair
<ProfileInfoLoader::Edge
, unsigned> > Counts
;
225 PI
.getEdgeCounts(Counts
);
226 EdgeFreqs
.insert(Counts
.begin(), Counts
.end());
229 if (PrintAnnotatedLLVM
|| PrintAllCode
) {
230 std::cout
<< "\n===" << std::string(73, '-') << "===\n";
231 std::cout
<< "Annotated LLVM code for the module:\n\n";
233 ProfileAnnotator
PA(FuncFreqs
, BlockFreqs
, EdgeFreqs
);
235 if (FunctionsToPrint
.empty() || PrintAllCode
)
236 M
->print(std::cout
, &PA
);
238 // Print just a subset of the functions...
239 for (std::set
<Function
*>::iterator I
= FunctionsToPrint
.begin(),
240 E
= FunctionsToPrint
.end(); I
!= E
; ++I
)
241 (*I
)->print(std::cout
, &PA
);
245 } catch (const std::string
& msg
) {
246 std::cerr
<< argv
[0] << ": " << msg
<< "\n";
248 std::cerr
<< argv
[0] << ": Unexpected unknown exception occurred.\n";