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