2003-10-20 17:57:13 +00:00
|
|
|
//===- analyze.cpp - The LLVM analyze utility -----------------------------===//
|
2003-10-20 17:47:21 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
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"
|
2004-05-27 05:41:36 +00:00
|
|
|
#include "llvm/System/Signals.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/Support/PluginLoader.h"
|
|
|
|
#include "llvm/Support/Timer.h"
|
2001-09-28 00:07:36 +00:00
|
|
|
#include <algorithm>
|
2002-01-31 00:46:09 +00:00
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
using namespace llvm;
|
2002-07-27 01:08:50 +00:00
|
|
|
|
2004-09-20 04:48:05 +00:00
|
|
|
struct ModulePassPrinter : public ModulePass {
|
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
|
|
|
|
2004-09-20 04:48:05 +00:00
|
|
|
virtual bool runOnModule(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
|
|
|
|
2003-05-12 22:08:58 +00:00
|
|
|
namespace {
|
|
|
|
cl::opt<std::string>
|
|
|
|
InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"),
|
|
|
|
cl::value_desc("filename"));
|
|
|
|
|
|
|
|
cl::opt<bool> Quiet("q", cl::desc("Don't print analysis pass names"));
|
|
|
|
cl::alias QuietA("quiet", cl::desc("Alias for -q"),
|
|
|
|
cl::aliasopt(Quiet));
|
|
|
|
|
2003-05-12 22:12:44 +00:00
|
|
|
cl::opt<bool> NoVerify("disable-verify", cl::Hidden,
|
|
|
|
cl::desc("Do not verify input module"));
|
|
|
|
|
2003-05-12 22:08:58 +00:00
|
|
|
// The AnalysesList is automatically populated with registered Passes by the
|
|
|
|
// PassNameParser.
|
|
|
|
//
|
|
|
|
cl::list<const PassInfo*, bool, FilteredPassNameParser<PassInfo::Analysis> >
|
|
|
|
AnalysesList(cl::desc("Analyses available:"));
|
|
|
|
|
|
|
|
Timer BytecodeLoadTimer("Bytecode Loader");
|
|
|
|
}
|
2002-11-10 06:55:02 +00:00
|
|
|
|
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");
|
2004-08-29 19:28:55 +00:00
|
|
|
sys::PrintStackTraceOnErrorSignal();
|
2001-07-23 02:35:57 +00:00
|
|
|
|
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.
|
2003-05-12 22:12:44 +00:00
|
|
|
if (!NoVerify)
|
|
|
|
Passes.add(createVerifierPass());
|
2002-08-30 22:54:37 +00:00
|
|
|
|
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;
|
|
|
|
}
|