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