2002-01-31 00:46:09 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-04-28 16:21:53 +00:00
|
|
|
// The LLVM analyze utility
|
2001-07-03 15:30:38 +00:00
|
|
|
//
|
|
|
|
// This utility is designed to print out the results of running various analysis
|
|
|
|
// passes on a program. This is useful for understanding a program, or for
|
|
|
|
// debugging an analysis pass.
|
|
|
|
//
|
|
|
|
// analyze --help - Output information about command line switches
|
|
|
|
// analyze --quiet - Do not print analysis name before output
|
|
|
|
//
|
2002-01-31 00:46:09 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-07-03 15:30:38 +00:00
|
|
|
|
|
|
|
#include "llvm/Module.h"
|
2002-01-31 00:46:09 +00:00
|
|
|
#include "llvm/PassManager.h"
|
2001-07-03 15:30:38 +00:00
|
|
|
#include "llvm/Bytecode/Reader.h"
|
|
|
|
#include "llvm/Assembly/Parser.h"
|
2002-08-30 22:54:37 +00:00
|
|
|
#include "llvm/Analysis/Verifier.h"
|
2003-04-24 19:13:02 +00:00
|
|
|
#include "llvm/Target/TargetData.h"
|
2002-07-27 01:08:50 +00:00
|
|
|
#include "llvm/Support/PassNameParser.h"
|
2002-11-10 06:55:02 +00:00
|
|
|
#include "Support/Timer.h"
|
2001-09-28 00:07:36 +00:00
|
|
|
#include <algorithm>
|
2002-01-31 00:46:09 +00:00
|
|
|
|
2002-07-27 01:08:50 +00:00
|
|
|
|
|
|
|
struct ModulePassPrinter : public Pass {
|
2002-07-29 23:02:25 +00:00
|
|
|
const PassInfo *PassToPrint;
|
|
|
|
ModulePassPrinter(const PassInfo *PI) : PassToPrint(PI) {}
|
2002-01-31 00:46:09 +00:00
|
|
|
|
2002-07-27 01:08:50 +00:00
|
|
|
virtual bool run(Module &M) {
|
2003-02-24 20:07:54 +00:00
|
|
|
std::cout << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
|
2002-08-08 19:01:30 +00:00
|
|
|
getAnalysisID<Pass>(PassToPrint).print(std::cout, &M);
|
2002-07-27 01:08:50 +00:00
|
|
|
|
|
|
|
// Get and print pass...
|
|
|
|
return false;
|
|
|
|
}
|
2002-11-06 06:16:08 +00:00
|
|
|
|
|
|
|
virtual const char *getPassName() const { return "'Pass' Printer"; }
|
2002-07-29 23:02:25 +00:00
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
2002-08-08 19:01:30 +00:00
|
|
|
AU.addRequiredID(PassToPrint);
|
2002-07-29 23:02:25 +00:00
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
2002-07-27 01:08:50 +00:00
|
|
|
};
|
2002-01-31 00:46:09 +00:00
|
|
|
|
2002-07-27 01:08:50 +00:00
|
|
|
struct FunctionPassPrinter : public FunctionPass {
|
|
|
|
const PassInfo *PassToPrint;
|
|
|
|
FunctionPassPrinter(const PassInfo *PI) : PassToPrint(PI) {}
|
2002-04-29 14:57:45 +00:00
|
|
|
|
2002-06-25 16:13:21 +00:00
|
|
|
virtual bool runOnFunction(Function &F) {
|
2003-02-24 20:07:54 +00:00
|
|
|
std::cout << "Printing analysis '" << PassToPrint->getPassName()
|
|
|
|
<< "' for function '" << F.getName() << "':\n";
|
2002-08-08 19:01:30 +00:00
|
|
|
getAnalysisID<Pass>(PassToPrint).print(std::cout, F.getParent());
|
2002-01-31 00:46:09 +00:00
|
|
|
|
2002-07-27 01:08:50 +00:00
|
|
|
// Get and print pass...
|
2002-01-31 00:46:09 +00:00
|
|
|
return false;
|
|
|
|
}
|
2002-07-27 01:08:50 +00:00
|
|
|
|
2002-11-06 06:16:08 +00:00
|
|
|
virtual const char *getPassName() const { return "FunctionPass Printer"; }
|
|
|
|
|
2002-07-27 01:08:50 +00:00
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
2002-08-08 19:01:30 +00:00
|
|
|
AU.addRequiredID(PassToPrint);
|
2002-07-27 01:08:50 +00:00
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
2002-01-31 00:46:09 +00:00
|
|
|
};
|
|
|
|
|
2002-07-27 01:08:50 +00:00
|
|
|
struct BasicBlockPassPrinter : public BasicBlockPass {
|
|
|
|
const PassInfo *PassToPrint;
|
|
|
|
BasicBlockPassPrinter(const PassInfo *PI) : PassToPrint(PI) {}
|
2002-01-31 00:46:09 +00:00
|
|
|
|
2002-07-27 01:08:50 +00:00
|
|
|
virtual bool runOnBasicBlock(BasicBlock &BB) {
|
|
|
|
std::cout << "Printing Analysis info for BasicBlock '" << BB.getName()
|
|
|
|
<< "': Pass " << PassToPrint->getPassName() << ":\n";
|
2002-08-08 19:01:30 +00:00
|
|
|
getAnalysisID<Pass>(PassToPrint).print(std::cout, BB.getParent()->getParent());
|
2001-07-06 16:59:10 +00:00
|
|
|
|
2002-07-27 01:08:50 +00:00
|
|
|
// Get and print pass...
|
|
|
|
return false;
|
|
|
|
}
|
2001-09-28 00:07:36 +00:00
|
|
|
|
2002-11-06 06:16:08 +00:00
|
|
|
virtual const char *getPassName() const { return "BasicBlockPass Printer"; }
|
|
|
|
|
2002-07-27 01:08:50 +00:00
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
2002-08-08 19:01:30 +00:00
|
|
|
AU.addRequiredID(PassToPrint);
|
2002-07-27 01:08:50 +00:00
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
};
|
2002-01-31 00:46:09 +00:00
|
|
|
|
|
|
|
|
2001-11-09 05:27:34 +00:00
|
|
|
|
2001-07-23 02:35:57 +00:00
|
|
|
|
2002-07-25 16:31:01 +00:00
|
|
|
static cl::opt<std::string>
|
2002-07-22 02:10:07 +00:00
|
|
|
InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"),
|
|
|
|
cl::value_desc("filename"));
|
|
|
|
|
|
|
|
static cl::opt<bool> Quiet("q", cl::desc("Don't print analysis pass names"));
|
|
|
|
static cl::alias QuietA("quiet", cl::desc("Alias for -q"),
|
|
|
|
cl::aliasopt(Quiet));
|
|
|
|
|
2002-07-27 01:08:50 +00:00
|
|
|
// The AnalysesList is automatically populated with registered Passes by the
|
|
|
|
// PassNameParser.
|
|
|
|
//
|
|
|
|
static cl::list<const PassInfo*, bool,
|
|
|
|
FilteredPassNameParser<PassInfo::Analysis> >
|
|
|
|
AnalysesList(cl::desc("Analyses available:"));
|
|
|
|
|
2001-09-28 00:07:36 +00:00
|
|
|
|
2002-11-10 06:55:02 +00:00
|
|
|
static Timer BytecodeLoadTimer("Bytecode Loader");
|
|
|
|
|
2001-07-03 15:30:38 +00:00
|
|
|
int main(int argc, char **argv) {
|
2001-07-23 02:35:57 +00:00
|
|
|
cl::ParseCommandLineOptions(argc, argv, " llvm analysis printer tool\n");
|
|
|
|
|
2002-03-26 22:43:12 +00:00
|
|
|
Module *CurMod = 0;
|
2002-02-01 05:09:35 +00:00
|
|
|
try {
|
2002-12-03 19:42:26 +00:00
|
|
|
#if 0
|
2002-11-10 06:55:02 +00:00
|
|
|
TimeRegion RegionTimer(BytecodeLoadTimer);
|
2002-12-03 19:42:26 +00:00
|
|
|
#endif
|
2002-03-26 22:43:12 +00:00
|
|
|
CurMod = ParseBytecodeFile(InputFilename);
|
|
|
|
if (!CurMod && !(CurMod = ParseAssemblyFile(InputFilename))){
|
2002-07-30 21:43:22 +00:00
|
|
|
std::cerr << argv[0] << ": input file didn't read correctly.\n";
|
2002-02-01 05:09:35 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} catch (const ParseException &E) {
|
2002-07-30 21:43:22 +00:00
|
|
|
std::cerr << argv[0] << ": " << E.getMessage() << "\n";
|
2001-07-06 16:59:10 +00:00
|
|
|
return 1;
|
2001-07-03 15:30:38 +00:00
|
|
|
}
|
|
|
|
|
2002-01-31 00:46:09 +00:00
|
|
|
// Create a PassManager to hold and optimize the collection of passes we are
|
|
|
|
// about to build...
|
|
|
|
//
|
2002-07-27 01:08:50 +00:00
|
|
|
PassManager Passes;
|
2002-01-31 00:46:09 +00:00
|
|
|
|
2003-04-24 19:13:02 +00:00
|
|
|
// Add an appropriate TargetData instance for this module...
|
|
|
|
Passes.add(new TargetData("analyze", CurMod));
|
|
|
|
|
2002-08-30 22:54:37 +00:00
|
|
|
// Make sure the input LLVM is well formed.
|
|
|
|
Passes.add(createVerifierPass());
|
|
|
|
|
2002-07-27 01:08:50 +00:00
|
|
|
// Create a new optimization pass for each one specified on the command line
|
2001-09-28 00:07:36 +00:00
|
|
|
for (unsigned i = 0; i < AnalysesList.size(); ++i) {
|
2002-07-27 01:08:50 +00:00
|
|
|
const PassInfo *Analysis = AnalysesList[i];
|
|
|
|
|
|
|
|
if (Analysis->getNormalCtor()) {
|
|
|
|
Pass *P = Analysis->getNormalCtor()();
|
|
|
|
Passes.add(P);
|
|
|
|
|
|
|
|
if (BasicBlockPass *BBP = dynamic_cast<BasicBlockPass*>(P))
|
|
|
|
Passes.add(new BasicBlockPassPrinter(Analysis));
|
|
|
|
else if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P))
|
|
|
|
Passes.add(new FunctionPassPrinter(Analysis));
|
|
|
|
else
|
2002-07-29 23:02:25 +00:00
|
|
|
Passes.add(new ModulePassPrinter(Analysis));
|
2002-07-27 01:08:50 +00:00
|
|
|
|
|
|
|
} else
|
2002-07-31 19:32:14 +00:00
|
|
|
std::cerr << argv[0] << ": cannot create pass: "
|
|
|
|
<< Analysis->getPassName() << "\n";
|
2002-07-27 01:08:50 +00:00
|
|
|
}
|
2001-09-28 00:07:36 +00:00
|
|
|
|
2002-07-27 01:08:50 +00:00
|
|
|
Passes.run(*CurMod);
|
2001-07-03 15:30:38 +00:00
|
|
|
|
2002-03-26 22:43:12 +00:00
|
|
|
delete CurMod;
|
2001-07-03 15:30:38 +00:00
|
|
|
return 0;
|
|
|
|
}
|