Changed clone to be const.
[llvm-complete.git] / tools / analyze / analyze.cpp
blob9d99d604f4f939d635a28dbeb8d1aca072cb3aab
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.
14 // analyze --help - Output information about command line switches
15 // analyze --quiet - Do not print analysis name before output
17 //===----------------------------------------------------------------------===//
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>
30 using namespace llvm;
32 struct ModulePassPrinter : public Pass {
33 const PassInfo *PassToPrint;
34 ModulePassPrinter(const PassInfo *PI) : PassToPrint(PI) {}
36 virtual bool run(Module &M) {
37 std::cout << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
38 getAnalysisID<Pass>(PassToPrint).print(std::cout, &M);
40 // Get and print pass...
41 return false;
44 virtual const char *getPassName() const { return "'Pass' Printer"; }
46 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
47 AU.addRequiredID(PassToPrint);
48 AU.setPreservesAll();
52 struct FunctionPassPrinter : public FunctionPass {
53 const PassInfo *PassToPrint;
54 FunctionPassPrinter(const PassInfo *PI) : PassToPrint(PI) {}
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());
61 // Get and print pass...
62 return false;
65 virtual const char *getPassName() const { return "FunctionPass Printer"; }
67 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
68 AU.addRequiredID(PassToPrint);
69 AU.setPreservesAll();
73 struct BasicBlockPassPrinter : public BasicBlockPass {
74 const PassInfo *PassToPrint;
75 BasicBlockPassPrinter(const PassInfo *PI) : PassToPrint(PI) {}
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());
82 // Get and print pass...
83 return false;
86 virtual const char *getPassName() const { return "BasicBlockPass Printer"; }
88 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
89 AU.addRequiredID(PassToPrint);
90 AU.setPreservesAll();
96 namespace {
97 cl::opt<std::string>
98 InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"),
99 cl::value_desc("filename"));
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));
105 cl::opt<bool> NoVerify("disable-verify", cl::Hidden,
106 cl::desc("Do not verify input module"));
108 // The AnalysesList is automatically populated with registered Passes by the
109 // PassNameParser.
111 cl::list<const PassInfo*, bool, FilteredPassNameParser<PassInfo::Analysis> >
112 AnalysesList(cl::desc("Analyses available:"));
114 Timer BytecodeLoadTimer("Bytecode Loader");
117 int main(int argc, char **argv) {
118 cl::ParseCommandLineOptions(argc, argv, " llvm analysis printer tool\n");
119 PrintStackTraceOnErrorSignal();
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;
131 } catch (const ParseException &E) {
132 std::cerr << argv[0] << ": " << E.getMessage() << "\n";
133 return 1;
136 // Create a PassManager to hold and optimize the collection of passes we are
137 // about to build...
139 PassManager Passes;
141 // Add an appropriate TargetData instance for this module...
142 Passes.add(new TargetData("analyze", CurMod));
144 // Make sure the input LLVM is well formed.
145 if (!NoVerify)
146 Passes.add(createVerifierPass());
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];
152 if (Analysis->getNormalCtor()) {
153 Pass *P = Analysis->getNormalCtor()();
154 Passes.add(P);
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));
163 } else
164 std::cerr << argv[0] << ": cannot create pass: "
165 << Analysis->getPassName() << "\n";
168 Passes.run(*CurMod);
170 delete CurMod;
171 return 0;