2003-10-20 19:57:13 +02:00
|
|
|
//===- analyze.cpp - The LLVM analyze utility -----------------------------===//
|
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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2001-07-03 17:30:38 +02: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 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"
|
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"
|
2002-11-10 07:55:02 +01:00
|
|
|
#include "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
|
|
|
|
|
|
|
struct ModulePassPrinter : public Pass {
|
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
|
|
|
|
2002-07-27 03:08:50 +02:00
|
|
|
virtual bool run(Module &M) {
|
2003-02-24 21:07:54 +01:00
|
|
|
std::cout << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
|
2002-08-08 21:01:30 +02:00
|
|
|
getAnalysisID<Pass>(PassToPrint).print(std::cout, &M);
|
2002-07-27 03:08:50 +02:00
|
|
|
|
|
|
|
// Get and print pass...
|
|
|
|
return false;
|
|
|
|
}
|
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) {
|
2003-02-24 21:07:54 +01:00
|
|
|
std::cout << "Printing analysis '" << PassToPrint->getPassName()
|
|
|
|
<< "' for function '" << F.getName() << "':\n";
|
2002-08-08 21:01:30 +02:00
|
|
|
getAnalysisID<Pass>(PassToPrint).print(std::cout, F.getParent());
|
2002-01-31 01:46:09 +01:00
|
|
|
|
2002-07-27 03:08:50 +02:00
|
|
|
// Get and print pass...
|
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) {
|
|
|
|
std::cout << "Printing Analysis info for BasicBlock '" << BB.getName()
|
|
|
|
<< "': Pass " << PassToPrint->getPassName() << ":\n";
|
2002-08-08 21:01:30 +02:00
|
|
|
getAnalysisID<Pass>(PassToPrint).print(std::cout, BB.getParent()->getParent());
|
2001-07-06 18:59:10 +02:00
|
|
|
|
2002-07-27 03:08:50 +02:00
|
|
|
// Get and print pass...
|
|
|
|
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
|
|
|
|
2003-05-13 00:08:58 +02: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-13 00:12:44 +02:00
|
|
|
cl::opt<bool> NoVerify("disable-verify", cl::Hidden,
|
|
|
|
cl::desc("Do not verify input module"));
|
|
|
|
|
2003-05-13 00:08:58 +02: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 07:55:02 +01:00
|
|
|
|
2001-07-03 17:30:38 +02:00
|
|
|
int main(int argc, char **argv) {
|
2001-07-23 04:35:57 +02:00
|
|
|
cl::ParseCommandLineOptions(argc, argv, " llvm analysis printer tool\n");
|
2004-02-19 21:33:00 +01:00
|
|
|
PrintStackTraceOnErrorSignal();
|
2001-07-23 04:35:57 +02:00
|
|
|
|
2002-03-26 23:43:12 +01:00
|
|
|
Module *CurMod = 0;
|
2002-02-01 06:09:35 +01:00
|
|
|
try {
|
2002-12-03 20:42:26 +01:00
|
|
|
#if 0
|
2002-11-10 07:55:02 +01:00
|
|
|
TimeRegion RegionTimer(BytecodeLoadTimer);
|
2002-12-03 20:42:26 +01:00
|
|
|
#endif
|
2002-03-26 23:43:12 +01:00
|
|
|
CurMod = ParseBytecodeFile(InputFilename);
|
|
|
|
if (!CurMod && !(CurMod = ParseAssemblyFile(InputFilename))){
|
2002-07-30 23:43:22 +02:00
|
|
|
std::cerr << argv[0] << ": input file didn't read correctly.\n";
|
2002-02-01 06:09:35 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} catch (const ParseException &E) {
|
2002-07-30 23:43:22 +02:00
|
|
|
std::cerr << argv[0] << ": " << E.getMessage() << "\n";
|
2001-07-06 18:59:10 +02:00
|
|
|
return 1;
|
2001-07-03 17:30:38 +02:00
|
|
|
}
|
|
|
|
|
2002-01-31 01:46:09 +01:00
|
|
|
// Create a PassManager to hold and optimize the collection of passes we are
|
|
|
|
// about to build...
|
|
|
|
//
|
2002-07-27 03:08:50 +02:00
|
|
|
PassManager Passes;
|
2002-01-31 01:46:09 +01:00
|
|
|
|
2003-04-24 21:13:02 +02:00
|
|
|
// Add an appropriate TargetData instance for this module...
|
|
|
|
Passes.add(new TargetData("analyze", CurMod));
|
|
|
|
|
2002-08-31 00:54:37 +02:00
|
|
|
// Make sure the input LLVM is well formed.
|
2003-05-13 00:12:44 +02:00
|
|
|
if (!NoVerify)
|
|
|
|
Passes.add(createVerifierPass());
|
2002-08-31 00:54:37 +02:00
|
|
|
|
2002-07-27 03:08:50 +02:00
|
|
|
// Create a new optimization pass for each one specified on the command line
|
2001-09-28 02:07:36 +02:00
|
|
|
for (unsigned i = 0; i < AnalysesList.size(); ++i) {
|
2002-07-27 03:08:50 +02: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-30 01:02:25 +02:00
|
|
|
Passes.add(new ModulePassPrinter(Analysis));
|
2002-07-27 03:08:50 +02:00
|
|
|
|
|
|
|
} else
|
2002-07-31 21:32:14 +02:00
|
|
|
std::cerr << argv[0] << ": cannot create pass: "
|
|
|
|
<< Analysis->getPassName() << "\n";
|
2002-07-27 03:08:50 +02:00
|
|
|
}
|
2001-09-28 02:07:36 +02:00
|
|
|
|
2002-07-27 03:08:50 +02:00
|
|
|
Passes.run(*CurMod);
|
2001-07-03 17:30:38 +02:00
|
|
|
|
2002-03-26 23:43:12 +01:00
|
|
|
delete CurMod;
|
2001-07-03 17:30:38 +02:00
|
|
|
return 0;
|
|
|
|
}
|