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/LLVMContext.h"
18 #include "llvm/Module.h"
19 #include "llvm/PassManager.h"
20 #include "llvm/Assembly/AssemblyAnnotationWriter.h"
21 #include "llvm/Analysis/ProfileInfo.h"
22 #include "llvm/Analysis/ProfileInfoLoader.h"
23 #include "llvm/Analysis/Passes.h"
24 #include "llvm/Bitcode/ReaderWriter.h"
25 #include "llvm/Support/CommandLine.h"
26 #include "llvm/Support/FormattedStream.h"
27 #include "llvm/Support/ManagedStatic.h"
28 #include "llvm/Support/MemoryBuffer.h"
29 #include "llvm/Support/PrettyStackTrace.h"
30 #include "llvm/Support/raw_ostream.h"
31 #include "llvm/Support/Format.h"
32 #include "llvm/Support/Signals.h"
33 #include "llvm/Support/system_error.h"
43 BitcodeFile(cl::Positional
, cl::desc("<program bitcode file>"),
47 ProfileDataFile(cl::Positional
, cl::desc("<llvmprof.out file>"),
48 cl::Optional
, cl::init("llvmprof.out"));
51 PrintAnnotatedLLVM("annotated-llvm",
52 cl::desc("Print LLVM code with frequency annotations"));
53 cl::alias
PrintAnnotated2("A", cl::desc("Alias for --annotated-llvm"),
54 cl::aliasopt(PrintAnnotatedLLVM
));
56 PrintAllCode("print-all-code",
57 cl::desc("Print annotated code for the entire program"));
60 // PairSecondSort - A sorting predicate to sort by the second element of a pair.
62 struct PairSecondSortReverse
63 : public std::binary_function
<std::pair
<T
, double>,
64 std::pair
<T
, double>, bool> {
65 bool operator()(const std::pair
<T
, double> &LHS
,
66 const std::pair
<T
, double> &RHS
) const {
67 return LHS
.second
> RHS
.second
;
71 static double ignoreMissing(double w
) {
72 if (w
== ProfileInfo::MissingValue
) return 0;
77 class ProfileAnnotator
: public AssemblyAnnotationWriter
{
80 ProfileAnnotator(ProfileInfo
&pi
) : PI(pi
) {}
82 virtual void emitFunctionAnnot(const Function
*F
,
83 formatted_raw_ostream
&OS
) {
84 double w
= PI
.getExecutionCount(F
);
85 if (w
!= ProfileInfo::MissingValue
) {
86 OS
<< ";;; %" << F
->getName() << " called "<<(unsigned)w
90 virtual void emitBasicBlockStartAnnot(const BasicBlock
*BB
,
91 formatted_raw_ostream
&OS
) {
92 double w
= PI
.getExecutionCount(BB
);
93 if (w
!= ProfileInfo::MissingValue
) {
95 OS
<< "\t;;; Basic block executed " << (unsigned)w
<< " times.\n";
97 OS
<< "\t;;; Never executed!\n";
102 virtual void emitBasicBlockEndAnnot(const BasicBlock
*BB
,
103 formatted_raw_ostream
&OS
) {
104 // Figure out how many times each successor executed.
105 std::vector
<std::pair
<ProfileInfo::Edge
, double> > SuccCounts
;
107 const TerminatorInst
*TI
= BB
->getTerminator();
108 for (unsigned s
= 0, e
= TI
->getNumSuccessors(); s
!= e
; ++s
) {
109 BasicBlock
* Succ
= TI
->getSuccessor(s
);
110 double w
= ignoreMissing(PI
.getEdgeWeight(std::make_pair(BB
, Succ
)));
112 SuccCounts
.push_back(std::make_pair(std::make_pair(BB
, Succ
), w
));
114 if (!SuccCounts
.empty()) {
115 OS
<< "\t;;; Out-edge counts:";
116 for (unsigned i
= 0, e
= SuccCounts
.size(); i
!= e
; ++i
)
117 OS
<< " [" << (SuccCounts
[i
]).second
<< " -> "
118 << (SuccCounts
[i
]).first
.second
->getName() << "]";
126 /// ProfileInfoPrinterPass - Helper pass to dump the profile information for
129 // FIXME: This should move elsewhere.
130 class ProfileInfoPrinterPass
: public ModulePass
{
131 ProfileInfoLoader
&PIL
;
133 static char ID
; // Class identification, replacement for typeinfo.
134 explicit ProfileInfoPrinterPass(ProfileInfoLoader
&_PIL
)
135 : ModulePass(ID
), PIL(_PIL
) {}
137 virtual void getAnalysisUsage(AnalysisUsage
&AU
) const {
138 AU
.setPreservesAll();
139 AU
.addRequired
<ProfileInfo
>();
142 bool runOnModule(Module
&M
);
146 char ProfileInfoPrinterPass::ID
= 0;
148 bool ProfileInfoPrinterPass::runOnModule(Module
&M
) {
149 ProfileInfo
&PI
= getAnalysis
<ProfileInfo
>();
150 std::map
<const Function
*, unsigned> FuncFreqs
;
151 std::map
<const BasicBlock
*, unsigned> BlockFreqs
;
152 std::map
<ProfileInfo::Edge
, unsigned> EdgeFreqs
;
154 // Output a report. Eventually, there will be multiple reports selectable on
155 // the command line, for now, just keep things simple.
157 // Emit the most frequent function table...
158 std::vector
<std::pair
<Function
*, double> > FunctionCounts
;
159 std::vector
<std::pair
<BasicBlock
*, double> > Counts
;
160 for (Module::iterator FI
= M
.begin(), FE
= M
.end(); FI
!= FE
; ++FI
) {
161 if (FI
->isDeclaration()) continue;
162 double w
= ignoreMissing(PI
.getExecutionCount(FI
));
163 FunctionCounts
.push_back(std::make_pair(FI
, w
));
164 for (Function::iterator BB
= FI
->begin(), BBE
= FI
->end();
166 double w
= ignoreMissing(PI
.getExecutionCount(BB
));
167 Counts
.push_back(std::make_pair(BB
, w
));
171 // Sort by the frequency, backwards.
172 sort(FunctionCounts
.begin(), FunctionCounts
.end(),
173 PairSecondSortReverse
<Function
*>());
175 double TotalExecutions
= 0;
176 for (unsigned i
= 0, e
= FunctionCounts
.size(); i
!= e
; ++i
)
177 TotalExecutions
+= FunctionCounts
[i
].second
;
179 outs() << "===" << std::string(73, '-') << "===\n"
180 << "LLVM profiling output for execution";
181 if (PIL
.getNumExecutions() != 1) outs() << "s";
184 for (unsigned i
= 0, e
= PIL
.getNumExecutions(); i
!= e
; ++i
) {
186 if (e
!= 1) outs() << i
+1 << ". ";
187 outs() << PIL
.getExecution(i
) << "\n";
190 outs() << "\n===" << std::string(73, '-') << "===\n";
191 outs() << "Function execution frequencies:\n\n";
193 // Print out the function frequencies...
194 outs() << " ## Frequency\n";
195 for (unsigned i
= 0, e
= FunctionCounts
.size(); i
!= e
; ++i
) {
196 if (FunctionCounts
[i
].second
== 0) {
197 outs() << "\n NOTE: " << e
-i
<< " function"
198 << (e
-i
-1 ? "s were" : " was") << " never executed!\n";
202 outs() << format("%3d", i
+1) << ". "
203 << format("%5.2g", FunctionCounts
[i
].second
) << "/"
204 << format("%g", TotalExecutions
) << " "
205 << FunctionCounts
[i
].first
->getNameStr() << "\n";
208 std::set
<Function
*> FunctionsToPrint
;
211 for (unsigned i
= 0, e
= Counts
.size(); i
!= e
; ++i
)
212 TotalExecutions
+= Counts
[i
].second
;
214 // Sort by the frequency, backwards.
215 sort(Counts
.begin(), Counts
.end(),
216 PairSecondSortReverse
<BasicBlock
*>());
218 outs() << "\n===" << std::string(73, '-') << "===\n";
219 outs() << "Top 20 most frequently executed basic blocks:\n\n";
221 // Print out the function frequencies...
222 outs() <<" ## %% \tFrequency\n";
223 unsigned BlocksToPrint
= Counts
.size();
224 if (BlocksToPrint
> 20) BlocksToPrint
= 20;
225 for (unsigned i
= 0; i
!= BlocksToPrint
; ++i
) {
226 if (Counts
[i
].second
== 0) break;
227 Function
*F
= Counts
[i
].first
->getParent();
228 outs() << format("%3d", i
+1) << ". "
229 << format("%5g", Counts
[i
].second
/(double)TotalExecutions
*100) << "% "
230 << format("%5.0f", Counts
[i
].second
) << "/"
231 << format("%g", TotalExecutions
) << "\t"
232 << F
->getNameStr() << "() - "
233 << Counts
[i
].first
->getNameStr() << "\n";
234 FunctionsToPrint
.insert(F
);
237 if (PrintAnnotatedLLVM
|| PrintAllCode
) {
238 outs() << "\n===" << std::string(73, '-') << "===\n";
239 outs() << "Annotated LLVM code for the module:\n\n";
241 ProfileAnnotator
PA(PI
);
243 if (FunctionsToPrint
.empty() || PrintAllCode
)
244 M
.print(outs(), &PA
);
246 // Print just a subset of the functions.
247 for (std::set
<Function
*>::iterator I
= FunctionsToPrint
.begin(),
248 E
= FunctionsToPrint
.end(); I
!= E
; ++I
)
249 (*I
)->print(outs(), &PA
);
255 int main(int argc
, char **argv
) {
256 // Print a stack trace if we signal out.
257 sys::PrintStackTraceOnErrorSignal();
258 PrettyStackTraceProgram
X(argc
, argv
);
260 LLVMContext
&Context
= getGlobalContext();
261 llvm_shutdown_obj Y
; // Call llvm_shutdown() on exit.
263 cl::ParseCommandLineOptions(argc
, argv
, "llvm profile dump decoder\n");
265 // Read in the bitcode file...
266 std::string ErrorMessage
;
267 OwningPtr
<MemoryBuffer
> Buffer
;
270 if (!(ec
= MemoryBuffer::getFileOrSTDIN(BitcodeFile
, Buffer
))) {
271 M
= ParseBitcodeFile(Buffer
.get(), Context
, &ErrorMessage
);
273 ErrorMessage
= ec
.message();
275 errs() << argv
[0] << ": " << BitcodeFile
<< ": "
276 << ErrorMessage
<< "\n";
280 // Read the profiling information. This is redundant since we load it again
281 // using the standard profile info provider pass, but for now this gives us
282 // access to additional information not exposed via the ProfileInfo
284 ProfileInfoLoader
PIL(argv
[0], ProfileDataFile
, *M
);
286 // Run the printer pass.
288 PassMgr
.add(createProfileLoaderPass(ProfileDataFile
));
289 PassMgr
.add(new ProfileInfoPrinterPass(PIL
));