New testcase for PR269
[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/Target/TargetData.h"
25 #include "llvm/Support/PassNameParser.h"
26 #include "Support/Signals.h"
27 #include "Support/Timer.h"
28 #include <algorithm>
29
30 using namespace llvm;
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   PrintStackTraceOnErrorSignal();
120
121   Module *CurMod = 0;
122   try {
123 #if 0
124     TimeRegion RegionTimer(BytecodeLoadTimer);
125 #endif
126     CurMod = ParseBytecodeFile(InputFilename);
127     if (!CurMod && !(CurMod = ParseAssemblyFile(InputFilename))){
128       std::cerr << argv[0] << ": input file didn't read correctly.\n";
129       return 1;
130     }
131   } catch (const ParseException &E) {
132     std::cerr << argv[0] << ": " << E.getMessage() << "\n";
133     return 1;
134   }
135
136   // Create a PassManager to hold and optimize the collection of passes we are
137   // about to build...
138   //
139   PassManager Passes;
140
141   // Add an appropriate TargetData instance for this module...
142   Passes.add(new TargetData("analyze", CurMod));
143
144   // Make sure the input LLVM is well formed.
145   if (!NoVerify)
146     Passes.add(createVerifierPass());
147
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];
151     
152     if (Analysis->getNormalCtor()) {
153       Pass *P = Analysis->getNormalCtor()();
154       Passes.add(P);
155
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));
160       else
161         Passes.add(new ModulePassPrinter(Analysis));
162
163     } else
164       std::cerr << argv[0] << ": cannot create pass: "
165                 << Analysis->getPassName() << "\n";
166   }
167
168   Passes.run(*CurMod);
169
170   delete CurMod;
171   return 0;
172 }