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