1 //===- analyze.cpp - The LLVM analyze utility -----------------------------===//
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 utility is designed to print out the results of running various analysis
11 // passes on a program. This is useful for understanding a program, or for
12 // debugging an analysis pass.
14 // analyze --help - Output information about command line switches
15 // analyze --quiet - Do not print analysis name before output
17 //===----------------------------------------------------------------------===//
19 #include "llvm/Module.h"
20 #include "llvm/PassManager.h"
21 #include "llvm/Bytecode/Reader.h"
22 #include "llvm/Assembly/Parser.h"
23 #include "llvm/Analysis/Verifier.h"
24 #include "llvm/Target/TargetData.h"
25 #include "llvm/Support/PassNameParser.h"
26 #include "Support/Signals.h"
27 #include "Support/Timer.h"
32 struct ModulePassPrinter
: public Pass
{
33 const PassInfo
*PassToPrint
;
34 ModulePassPrinter(const PassInfo
*PI
) : PassToPrint(PI
) {}
36 virtual bool run(Module
&M
) {
37 std::cout
<< "Printing analysis '" << PassToPrint
->getPassName() << "':\n";
38 getAnalysisID
<Pass
>(PassToPrint
).print(std::cout
, &M
);
40 // Get and print pass...
44 virtual const char *getPassName() const { return "'Pass' Printer"; }
46 virtual void getAnalysisUsage(AnalysisUsage
&AU
) const {
47 AU
.addRequiredID(PassToPrint
);
52 struct FunctionPassPrinter
: public FunctionPass
{
53 const PassInfo
*PassToPrint
;
54 FunctionPassPrinter(const PassInfo
*PI
) : PassToPrint(PI
) {}
56 virtual bool runOnFunction(Function
&F
) {
57 std::cout
<< "Printing analysis '" << PassToPrint
->getPassName()
58 << "' for function '" << F
.getName() << "':\n";
59 getAnalysisID
<Pass
>(PassToPrint
).print(std::cout
, F
.getParent());
61 // Get and print pass...
65 virtual const char *getPassName() const { return "FunctionPass Printer"; }
67 virtual void getAnalysisUsage(AnalysisUsage
&AU
) const {
68 AU
.addRequiredID(PassToPrint
);
73 struct BasicBlockPassPrinter
: public BasicBlockPass
{
74 const PassInfo
*PassToPrint
;
75 BasicBlockPassPrinter(const PassInfo
*PI
) : PassToPrint(PI
) {}
77 virtual bool runOnBasicBlock(BasicBlock
&BB
) {
78 std::cout
<< "Printing Analysis info for BasicBlock '" << BB
.getName()
79 << "': Pass " << PassToPrint
->getPassName() << ":\n";
80 getAnalysisID
<Pass
>(PassToPrint
).print(std::cout
, BB
.getParent()->getParent());
82 // Get and print pass...
86 virtual const char *getPassName() const { return "BasicBlockPass Printer"; }
88 virtual void getAnalysisUsage(AnalysisUsage
&AU
) const {
89 AU
.addRequiredID(PassToPrint
);
98 InputFilename(cl::Positional
, cl::desc("<input file>"), cl::init("-"),
99 cl::value_desc("filename"));
101 cl::opt
<bool> Quiet("q", cl::desc("Don't print analysis pass names"));
102 cl::alias
QuietA("quiet", cl::desc("Alias for -q"),
103 cl::aliasopt(Quiet
));
105 cl::opt
<bool> NoVerify("disable-verify", cl::Hidden
,
106 cl::desc("Do not verify input module"));
108 // The AnalysesList is automatically populated with registered Passes by the
111 cl::list
<const PassInfo
*, bool, FilteredPassNameParser
<PassInfo::Analysis
> >
112 AnalysesList(cl::desc("Analyses available:"));
114 Timer
BytecodeLoadTimer("Bytecode Loader");
117 int main(int argc
, char **argv
) {
118 cl::ParseCommandLineOptions(argc
, argv
, " llvm analysis printer tool\n");
119 PrintStackTraceOnErrorSignal();
124 TimeRegion
RegionTimer(BytecodeLoadTimer
);
126 CurMod
= ParseBytecodeFile(InputFilename
);
127 if (!CurMod
&& !(CurMod
= ParseAssemblyFile(InputFilename
))){
128 std::cerr
<< argv
[0] << ": input file didn't read correctly.\n";
131 } catch (const ParseException
&E
) {
132 std::cerr
<< argv
[0] << ": " << E
.getMessage() << "\n";
136 // Create a PassManager to hold and optimize the collection of passes we are
141 // Add an appropriate TargetData instance for this module...
142 Passes
.add(new TargetData("analyze", CurMod
));
144 // Make sure the input LLVM is well formed.
146 Passes
.add(createVerifierPass());
148 // Create a new optimization pass for each one specified on the command line
149 for (unsigned i
= 0; i
< AnalysesList
.size(); ++i
) {
150 const PassInfo
*Analysis
= AnalysesList
[i
];
152 if (Analysis
->getNormalCtor()) {
153 Pass
*P
= Analysis
->getNormalCtor()();
156 if (BasicBlockPass
*BBP
= dynamic_cast<BasicBlockPass
*>(P
))
157 Passes
.add(new BasicBlockPassPrinter(Analysis
));
158 else if (FunctionPass
*FP
= dynamic_cast<FunctionPass
*>(P
))
159 Passes
.add(new FunctionPassPrinter(Analysis
));
161 Passes
.add(new ModulePassPrinter(Analysis
));
164 std::cerr
<< argv
[0] << ": cannot create pass: "
165 << Analysis
->getPassName() << "\n";