17f7e4866d851098a812409ca45753ec4e262222
[oota-llvm.git] / tools / analyze / analyze.cpp
1 //===----------------------------------------------------------------------===//
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 // The LLVM analyze utility
11 //
12 // This utility is designed to print out the results of running various analysis
13 // passes on a program.  This is useful for understanding a program, or for 
14 // debugging an analysis pass.
15 //
16 //  analyze --help           - Output information about command line switches
17 //  analyze --quiet          - Do not print analysis name before output
18 //
19 //===----------------------------------------------------------------------===//
20
21 #include "llvm/Module.h"
22 #include "llvm/PassManager.h"
23 #include "llvm/Bytecode/Reader.h"
24 #include "llvm/Assembly/Parser.h"
25 #include "llvm/Analysis/Verifier.h"
26 #include "llvm/Target/TargetData.h"
27 #include "llvm/Support/PassNameParser.h"
28 #include "Support/Timer.h"
29 #include <algorithm>
30
31
32 struct ModulePassPrinter : public Pass {
33   const PassInfo *PassToPrint;
34   ModulePassPrinter(const PassInfo *PI) : PassToPrint(PI) {}
35
36   virtual bool run(Module &M) {
37     std::cout << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
38     getAnalysisID<Pass>(PassToPrint).print(std::cout, &M);
39     
40     // Get and print pass...
41     return false;
42   }
43   
44   virtual const char *getPassName() const { return "'Pass' Printer"; }
45
46   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
47     AU.addRequiredID(PassToPrint);
48     AU.setPreservesAll();
49   }
50 };
51
52 struct FunctionPassPrinter : public FunctionPass {
53   const PassInfo *PassToPrint;
54   FunctionPassPrinter(const PassInfo *PI) : PassToPrint(PI) {}
55
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());
60
61     // Get and print pass...
62     return false;
63   }
64
65   virtual const char *getPassName() const { return "FunctionPass Printer"; }
66
67   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
68     AU.addRequiredID(PassToPrint);
69     AU.setPreservesAll();
70   }
71 };
72
73 struct BasicBlockPassPrinter : public BasicBlockPass {
74   const PassInfo *PassToPrint;
75   BasicBlockPassPrinter(const PassInfo *PI) : PassToPrint(PI) {}
76
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());
81
82     // Get and print pass...
83     return false;
84   }
85
86   virtual const char *getPassName() const { return "BasicBlockPass Printer"; }
87
88   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
89     AU.addRequiredID(PassToPrint);
90     AU.setPreservesAll();
91   }
92 };
93
94
95
96 namespace {
97   cl::opt<std::string>
98   InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"),
99                 cl::value_desc("filename"));
100
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));
104
105   cl::opt<bool> NoVerify("disable-verify", cl::Hidden,
106                          cl::desc("Do not verify input module"));
107
108   // The AnalysesList is automatically populated with registered Passes by the
109   // PassNameParser.
110   //
111   cl::list<const PassInfo*, bool, FilteredPassNameParser<PassInfo::Analysis> >
112   AnalysesList(cl::desc("Analyses available:"));
113
114   Timer BytecodeLoadTimer("Bytecode Loader");
115 }
116
117 int main(int argc, char **argv) {
118   cl::ParseCommandLineOptions(argc, argv, " llvm analysis printer tool\n");
119
120   Module *CurMod = 0;
121   try {
122 #if 0
123     TimeRegion RegionTimer(BytecodeLoadTimer);
124 #endif
125     CurMod = ParseBytecodeFile(InputFilename);
126     if (!CurMod && !(CurMod = ParseAssemblyFile(InputFilename))){
127       std::cerr << argv[0] << ": input file didn't read correctly.\n";
128       return 1;
129     }
130   } catch (const ParseException &E) {
131     std::cerr << argv[0] << ": " << E.getMessage() << "\n";
132     return 1;
133   }
134
135   // Create a PassManager to hold and optimize the collection of passes we are
136   // about to build...
137   //
138   PassManager Passes;
139
140   // Add an appropriate TargetData instance for this module...
141   Passes.add(new TargetData("analyze", CurMod));
142
143   // Make sure the input LLVM is well formed.
144   if (!NoVerify)
145     Passes.add(createVerifierPass());
146
147   // Create a new optimization pass for each one specified on the command line
148   for (unsigned i = 0; i < AnalysesList.size(); ++i) {
149     const PassInfo *Analysis = AnalysesList[i];
150     
151     if (Analysis->getNormalCtor()) {
152       Pass *P = Analysis->getNormalCtor()();
153       Passes.add(P);
154
155       if (BasicBlockPass *BBP = dynamic_cast<BasicBlockPass*>(P))
156         Passes.add(new BasicBlockPassPrinter(Analysis));
157       else if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P))
158         Passes.add(new FunctionPassPrinter(Analysis));
159       else
160         Passes.add(new ModulePassPrinter(Analysis));
161
162     } else
163       std::cerr << argv[0] << ": cannot create pass: "
164                 << Analysis->getPassName() << "\n";
165   }
166
167   Passes.run(*CurMod);
168
169   delete CurMod;
170   return 0;
171 }