2003-10-20 19:57:13 +02:00
|
|
|
//===- analyze.cpp - The LLVM analyze utility -----------------------------===//
|
2005-04-22 02:00:37 +02:00
|
|
|
//
|
2003-10-20 19:47:21 +02: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.
|
2005-04-22 02:00:37 +02:00
|
|
|
//
|
2003-10-20 19:47:21 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-07-03 17:30:38 +02:00
|
|
|
//
|
|
|
|
// This utility is designed to print out the results of running various analysis
|
2005-04-22 02:00:37 +02:00
|
|
|
// passes on a program. This is useful for understanding a program, or for
|
2001-07-03 17:30:38 +02:00
|
|
|
// debugging an analysis pass.
|
|
|
|
//
|
|
|
|
// analyze --help - Output information about command line switches
|
|
|
|
// analyze --quiet - Do not print analysis name before output
|
|
|
|
//
|
2002-01-31 01:46:09 +01:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-07-03 17:30:38 +02:00
|
|
|
|
|
|
|
#include "llvm/Module.h"
|
2002-01-31 01:46:09 +01:00
|
|
|
#include "llvm/PassManager.h"
|
2001-07-03 17:30:38 +02:00
|
|
|
#include "llvm/Bytecode/Reader.h"
|
|
|
|
#include "llvm/Assembly/Parser.h"
|
2002-08-31 00:54:37 +02:00
|
|
|
#include "llvm/Analysis/Verifier.h"
|
2005-10-24 03:00:13 +02:00
|
|
|
#include "llvm/Analysis/LinkAllAnalyses.h"
|
2003-04-24 21:13:02 +02:00
|
|
|
#include "llvm/Target/TargetData.h"
|
2002-07-27 03:08:50 +02:00
|
|
|
#include "llvm/Support/PassNameParser.h"
|
2004-05-27 07:41:36 +02:00
|
|
|
#include "llvm/System/Signals.h"
|
2004-09-02 00:55:40 +02:00
|
|
|
#include "llvm/Support/PluginLoader.h"
|
|
|
|
#include "llvm/Support/Timer.h"
|
2001-09-28 02:07:36 +02:00
|
|
|
#include <algorithm>
|
2002-01-31 01:46:09 +01:00
|
|
|
|
2003-11-11 23:41:34 +01:00
|
|
|
using namespace llvm;
|
2002-07-27 03:08:50 +02:00
|
|
|
|
2006-03-03 03:12:04 +01: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));
|
|
|
|
|
|
|
|
cl::opt<bool> NoVerify("disable-verify", cl::Hidden,
|
|
|
|
cl::desc("Do not verify input module"));
|
|
|
|
|
|
|
|
// 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");
|
|
|
|
}
|
|
|
|
|
2004-09-20 06:48:05 +02:00
|
|
|
struct ModulePassPrinter : public ModulePass {
|
2002-07-30 01:02:25 +02:00
|
|
|
const PassInfo *PassToPrint;
|
|
|
|
ModulePassPrinter(const PassInfo *PI) : PassToPrint(PI) {}
|
2002-01-31 01:46:09 +01:00
|
|
|
|
2004-09-20 06:48:05 +02:00
|
|
|
virtual bool runOnModule(Module &M) {
|
2006-03-03 03:12:04 +01:00
|
|
|
if (!Quiet) {
|
|
|
|
std::cout << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
|
|
|
|
getAnalysisID<Pass>(PassToPrint).print(std::cout, &M);
|
|
|
|
}
|
2005-04-22 02:00:37 +02:00
|
|
|
|
2002-07-27 03:08:50 +02:00
|
|
|
// Get and print pass...
|
|
|
|
return false;
|
|
|
|
}
|
2005-04-22 02:00:37 +02:00
|
|
|
|
2002-11-06 07:16:08 +01:00
|
|
|
virtual const char *getPassName() const { return "'Pass' Printer"; }
|
2002-07-30 01:02:25 +02:00
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
2002-08-08 21:01:30 +02:00
|
|
|
AU.addRequiredID(PassToPrint);
|
2002-07-30 01:02:25 +02:00
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
2002-07-27 03:08:50 +02:00
|
|
|
};
|
2002-01-31 01:46:09 +01:00
|
|
|
|
2002-07-27 03:08:50 +02:00
|
|
|
struct FunctionPassPrinter : public FunctionPass {
|
|
|
|
const PassInfo *PassToPrint;
|
|
|
|
FunctionPassPrinter(const PassInfo *PI) : PassToPrint(PI) {}
|
2002-04-29 16:57:45 +02:00
|
|
|
|
2002-06-25 18:13:21 +02:00
|
|
|
virtual bool runOnFunction(Function &F) {
|
2006-03-03 03:12:04 +01:00
|
|
|
if (!Quiet) {
|
|
|
|
std::cout << "Printing analysis '" << PassToPrint->getPassName()
|
|
|
|
<< "' for function '" << F.getName() << "':\n";
|
|
|
|
}
|
2002-07-27 03:08:50 +02:00
|
|
|
// Get and print pass...
|
2006-03-03 03:12:04 +01:00
|
|
|
getAnalysisID<Pass>(PassToPrint).print(std::cout, F.getParent());
|
2002-01-31 01:46:09 +01:00
|
|
|
return false;
|
|
|
|
}
|
2002-07-27 03:08:50 +02:00
|
|
|
|
2002-11-06 07:16:08 +01:00
|
|
|
virtual const char *getPassName() const { return "FunctionPass Printer"; }
|
|
|
|
|
2002-07-27 03:08:50 +02:00
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
2002-08-08 21:01:30 +02:00
|
|
|
AU.addRequiredID(PassToPrint);
|
2002-07-27 03:08:50 +02:00
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
2002-01-31 01:46:09 +01:00
|
|
|
};
|
|
|
|
|
2002-07-27 03:08:50 +02:00
|
|
|
struct BasicBlockPassPrinter : public BasicBlockPass {
|
|
|
|
const PassInfo *PassToPrint;
|
|
|
|
BasicBlockPassPrinter(const PassInfo *PI) : PassToPrint(PI) {}
|
2002-01-31 01:46:09 +01:00
|
|
|
|
2002-07-27 03:08:50 +02:00
|
|
|
virtual bool runOnBasicBlock(BasicBlock &BB) {
|
2006-03-03 03:12:04 +01:00
|
|
|
if (!Quiet) {
|
|
|
|
std::cout << "Printing Analysis info for BasicBlock '" << BB.getName()
|
|
|
|
<< "': Pass " << PassToPrint->getPassName() << ":\n";
|
|
|
|
}
|
2001-07-06 18:59:10 +02:00
|
|
|
|
2002-07-27 03:08:50 +02:00
|
|
|
// Get and print pass...
|
2006-03-03 03:12:04 +01:00
|
|
|
getAnalysisID<Pass>(PassToPrint).print(std::cout, BB.getParent()->getParent());
|
2002-07-27 03:08:50 +02:00
|
|
|
return false;
|
|
|
|
}
|
2001-09-28 02:07:36 +02:00
|
|
|
|
2002-11-06 07:16:08 +01:00
|
|
|
virtual const char *getPassName() const { return "BasicBlockPass Printer"; }
|
|
|
|
|
2002-07-27 03:08:50 +02:00
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
2002-08-08 21:01:30 +02:00
|
|
|
AU.addRequiredID(PassToPrint);
|
2002-07-27 03:08:50 +02:00
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
};
|
2002-01-31 01:46:09 +01:00
|
|
|
|
|
|
|
|
2001-11-09 06:27:34 +01:00
|
|
|
|
2001-07-03 17:30:38 +02:00
|
|
|
int main(int argc, char **argv) {
|
2002-02-01 06:09:35 +01:00
|
|
|
try {
|
2004-12-30 06:36:08 +01:00
|
|
|
cl::ParseCommandLineOptions(argc, argv, " llvm analysis printer tool\n");
|
|
|
|
sys::PrintStackTraceOnErrorSignal();
|
|
|
|
|
|
|
|
Module *CurMod = 0;
|
|
|
|
try {
|
2002-12-03 20:42:26 +01:00
|
|
|
#if 0
|
2004-12-30 06:36:08 +01:00
|
|
|
TimeRegion RegionTimer(BytecodeLoadTimer);
|
2002-12-03 20:42:26 +01:00
|
|
|
#endif
|
2004-12-30 06:36:08 +01:00
|
|
|
CurMod = ParseBytecodeFile(InputFilename);
|
|
|
|
if (!CurMod && !(CurMod = ParseAssemblyFile(InputFilename))){
|
|
|
|
std::cerr << argv[0] << ": input file didn't read correctly.\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} catch (const ParseException &E) {
|
|
|
|
std::cerr << argv[0] << ": " << E.getMessage() << "\n";
|
2002-02-01 06:09:35 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2001-07-03 17:30:38 +02:00
|
|
|
|
2004-12-30 06:36:08 +01:00
|
|
|
// Create a PassManager to hold and optimize the collection of passes we are
|
|
|
|
// about to build...
|
|
|
|
//
|
|
|
|
PassManager Passes;
|
|
|
|
|
|
|
|
// Add an appropriate TargetData instance for this module...
|
|
|
|
Passes.add(new TargetData("analyze", CurMod));
|
|
|
|
|
|
|
|
// Make sure the input LLVM is well formed.
|
|
|
|
if (!NoVerify)
|
|
|
|
Passes.add(createVerifierPass());
|
|
|
|
|
|
|
|
// Create a new optimization pass for each one specified on the command line
|
|
|
|
for (unsigned i = 0; i < AnalysesList.size(); ++i) {
|
|
|
|
const PassInfo *Analysis = AnalysesList[i];
|
2005-04-22 02:00:37 +02:00
|
|
|
|
2004-12-30 06:36:08 +01:00
|
|
|
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
|
|
|
|
Passes.add(new ModulePassPrinter(Analysis));
|
|
|
|
|
|
|
|
} else
|
|
|
|
std::cerr << argv[0] << ": cannot create pass: "
|
|
|
|
<< Analysis->getPassName() << "\n";
|
|
|
|
}
|
2002-01-31 01:46:09 +01:00
|
|
|
|
2004-12-30 06:36:08 +01:00
|
|
|
Passes.run(*CurMod);
|
2003-04-24 21:13:02 +02:00
|
|
|
|
2004-12-30 06:36:08 +01:00
|
|
|
delete CurMod;
|
|
|
|
return 0;
|
2002-08-31 00:54:37 +02:00
|
|
|
|
2004-12-30 06:36:08 +01:00
|
|
|
} catch (const std::string& msg) {
|
|
|
|
std::cerr << argv[0] << ": " << msg << "\n";
|
|
|
|
} catch (...) {
|
|
|
|
std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
|
2002-07-27 03:08:50 +02:00
|
|
|
}
|
2004-12-30 06:36:08 +01:00
|
|
|
return 1;
|
2001-07-03 17:30:38 +02:00
|
|
|
}
|