1 //===- analyze.cpp - The LLVM analyze utility -----------------------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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/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"
36 InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"),
37 cl::value_desc("filename"));
39 cl::opt<bool> Quiet("q", cl::desc("Don't print analysis pass names"));
40 cl::alias QuietA("quiet", cl::desc("Alias for -q"),
43 cl::opt<bool> NoVerify("disable-verify", cl::Hidden,
44 cl::desc("Do not verify input module"));
46 // The AnalysesList is automatically populated with registered Passes by the
49 cl::list<const PassInfo*, bool, FilteredPassNameParser<PassInfo::Analysis> >
50 AnalysesList(cl::desc("Analyses available:"));
52 Timer BytecodeLoadTimer("Bytecode Loader");
55 struct ModulePassPrinter : public ModulePass {
56 const PassInfo *PassToPrint;
57 ModulePassPrinter(const PassInfo *PI) : PassToPrint(PI) {}
59 virtual bool runOnModule(Module &M) {
61 std::cout << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
62 getAnalysisID<Pass>(PassToPrint).print(std::cout, &M);
65 // Get and print pass...
69 virtual const char *getPassName() const { return "'Pass' Printer"; }
71 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
72 AU.addRequiredID(PassToPrint);
77 struct FunctionPassPrinter : public FunctionPass {
78 const PassInfo *PassToPrint;
79 FunctionPassPrinter(const PassInfo *PI) : PassToPrint(PI) {}
81 virtual bool runOnFunction(Function &F) {
83 std::cout << "Printing analysis '" << PassToPrint->getPassName()
84 << "' for function '" << F.getName() << "':\n";
86 // Get and print pass...
87 getAnalysisID<Pass>(PassToPrint).print(std::cout, F.getParent());
91 virtual const char *getPassName() const { return "FunctionPass Printer"; }
93 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
94 AU.addRequiredID(PassToPrint);
99 struct BasicBlockPassPrinter : public BasicBlockPass {
100 const PassInfo *PassToPrint;
101 BasicBlockPassPrinter(const PassInfo *PI) : PassToPrint(PI) {}
103 virtual bool runOnBasicBlock(BasicBlock &BB) {
105 std::cout << "Printing Analysis info for BasicBlock '" << BB.getName()
106 << "': Pass " << PassToPrint->getPassName() << ":\n";
109 // Get and print pass...
110 getAnalysisID<Pass>(PassToPrint).print(std::cout, BB.getParent()->getParent());
114 virtual const char *getPassName() const { return "BasicBlockPass Printer"; }
116 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
117 AU.addRequiredID(PassToPrint);
118 AU.setPreservesAll();
124 int main(int argc, char **argv) {
126 cl::ParseCommandLineOptions(argc, argv, " llvm analysis printer tool\n");
127 sys::PrintStackTraceOnErrorSignal();
132 TimeRegion RegionTimer(BytecodeLoadTimer);
134 CurMod = ParseBytecodeFile(InputFilename);
135 if (!CurMod && !(CurMod = ParseAssemblyFile(InputFilename))){
136 std::cerr << argv[0] << ": input file didn't read correctly.\n";
139 } catch (const ParseException &E) {
140 std::cerr << argv[0] << ": " << E.getMessage() << "\n";
144 // Create a PassManager to hold and optimize the collection of passes we are
149 // Add an appropriate TargetData instance for this module...
150 Passes.add(new TargetData("analyze", CurMod));
152 // Make sure the input LLVM is well formed.
154 Passes.add(createVerifierPass());
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];
160 if (Analysis->getNormalCtor()) {
161 Pass *P = Analysis->getNormalCtor()();
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));
169 Passes.add(new ModulePassPrinter(Analysis));
172 std::cerr << argv[0] << ": cannot create pass: "
173 << Analysis->getPassName() << "\n";
181 } catch (const std::string& msg) {
182 std::cerr << argv[0] << ": " << msg << "\n";
184 std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";