2002-01-31 01:46:09 +01:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-04-28 18:21:53 +02:00
|
|
|
// The LLVM analyze utility
|
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"
|
2001-12-03 18:27:42 +01:00
|
|
|
#include "llvm/iPHINode.h"
|
2002-04-09 00:04:24 +02:00
|
|
|
#include "llvm/Type.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-01-31 01:46:09 +01:00
|
|
|
#include "llvm/Assembly/PrintModulePass.h"
|
2002-04-09 00:04:24 +02:00
|
|
|
#include "llvm/Assembly/Writer.h"
|
2001-07-03 17:30:38 +02:00
|
|
|
#include "llvm/Analysis/Writer.h"
|
2001-09-14 03:42:42 +02:00
|
|
|
#include "llvm/Analysis/InstForest.h"
|
2001-07-03 17:30:38 +02:00
|
|
|
#include "llvm/Analysis/Dominators.h"
|
|
|
|
#include "llvm/Analysis/IntervalPartition.h"
|
2001-07-20 21:16:29 +02:00
|
|
|
#include "llvm/Analysis/Expressions.h"
|
2001-11-26 20:18:11 +01:00
|
|
|
#include "llvm/Analysis/InductionVariable.h"
|
2001-09-28 02:07:36 +02:00
|
|
|
#include "llvm/Analysis/CallGraph.h"
|
2001-11-26 20:18:11 +01:00
|
|
|
#include "llvm/Analysis/LoopInfo.h"
|
2002-03-26 23:43:12 +01:00
|
|
|
#include "llvm/Analysis/DataStructure.h"
|
2001-11-07 22:16:29 +01:00
|
|
|
#include "llvm/Analysis/FindUnsafePointerTypes.h"
|
2001-11-09 06:27:34 +01:00
|
|
|
#include "llvm/Analysis/FindUsedTypes.h"
|
2002-02-12 22:07:25 +01:00
|
|
|
#include "llvm/Support/InstIterator.h"
|
2001-11-27 01:03:19 +01:00
|
|
|
#include "Support/CommandLine.h"
|
2001-09-28 02:07:36 +02:00
|
|
|
#include <algorithm>
|
2002-01-31 01:46:09 +01:00
|
|
|
|
|
|
|
using std::ostream;
|
|
|
|
using std::string;
|
|
|
|
|
2002-03-26 23:43:12 +01:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// printPass - Specify how to print out a pass. For most passes, the standard
|
|
|
|
// way of using operator<< works great, so we use it directly...
|
|
|
|
//
|
|
|
|
template<class PassType>
|
|
|
|
static void printPass(PassType &P, ostream &O, Module *M) {
|
|
|
|
O << P;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class PassType>
|
2002-04-07 22:49:59 +02:00
|
|
|
static void printPass(PassType &P, ostream &O, Function *F) {
|
2002-03-26 23:43:12 +01:00
|
|
|
O << P;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Other classes require more information to print out information, so we
|
|
|
|
// specialize the template here for them...
|
|
|
|
//
|
|
|
|
template<>
|
|
|
|
static void printPass(DataStructure &P, ostream &O, Module *M) {
|
|
|
|
P.print(O, M);
|
|
|
|
}
|
2001-07-03 17:30:38 +02:00
|
|
|
|
2002-03-26 23:43:12 +01:00
|
|
|
template<>
|
|
|
|
static void printPass(FindUsedTypes &FUT, ostream &O, Module *M) {
|
|
|
|
FUT.printTypes(O, M);
|
2001-07-03 17:30:38 +02:00
|
|
|
}
|
|
|
|
|
2002-03-26 23:43:12 +01:00
|
|
|
template<>
|
|
|
|
static void printPass(FindUnsafePointerTypes &FUPT, ostream &O,
|
|
|
|
Module *M) {
|
|
|
|
FUPT.printResults(M, O);
|
2001-07-06 18:59:10 +02:00
|
|
|
}
|
|
|
|
|
2002-01-31 01:46:09 +01:00
|
|
|
|
|
|
|
|
|
|
|
template <class PassType, class PassName>
|
|
|
|
class PassPrinter; // Do not implement
|
|
|
|
|
|
|
|
template <class PassName>
|
|
|
|
class PassPrinter<Pass, PassName> : public Pass {
|
|
|
|
const string Message;
|
|
|
|
const AnalysisID ID;
|
|
|
|
public:
|
|
|
|
PassPrinter(const string &M, AnalysisID id) : Message(M), ID(id) {}
|
2002-04-29 16:57:45 +02:00
|
|
|
|
|
|
|
const char *getPassName() const { return "IP Pass Printer"; }
|
2002-01-31 01:46:09 +01:00
|
|
|
|
|
|
|
virtual bool run(Module *M) {
|
2002-03-26 23:43:12 +01:00
|
|
|
std::cout << Message << "\n";
|
|
|
|
printPass(getAnalysis<PassName>(ID), std::cout, M);
|
2002-01-31 01:46:09 +01:00
|
|
|
return false;
|
2001-07-20 21:16:29 +02:00
|
|
|
}
|
|
|
|
|
2002-04-27 08:56:12 +02:00
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.addRequired(ID);
|
2002-01-31 01:46:09 +01:00
|
|
|
}
|
|
|
|
};
|
2001-11-26 20:18:11 +01:00
|
|
|
|
2002-01-31 01:46:09 +01:00
|
|
|
template <class PassName>
|
2002-04-27 08:56:12 +02:00
|
|
|
class PassPrinter<FunctionPass, PassName> : public FunctionPass {
|
2002-01-31 01:46:09 +01:00
|
|
|
const string Message;
|
|
|
|
const AnalysisID ID;
|
|
|
|
public:
|
|
|
|
PassPrinter(const string &M, AnalysisID id) : Message(M), ID(id) {}
|
2002-04-29 16:57:45 +02:00
|
|
|
|
|
|
|
const char *getPassName() const { return "Function Pass Printer"; }
|
2002-01-31 01:46:09 +01:00
|
|
|
|
2002-04-27 08:56:12 +02:00
|
|
|
virtual bool runOnFunction(Function *F) {
|
|
|
|
std::cout << Message << " on function '" << F->getName() << "'\n";
|
2002-04-07 22:49:59 +02:00
|
|
|
printPass(getAnalysis<PassName>(ID), std::cout, F);
|
2002-01-31 01:46:09 +01:00
|
|
|
return false;
|
|
|
|
}
|
2001-11-26 20:18:11 +01:00
|
|
|
|
2002-04-27 08:56:12 +02:00
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.addRequired(ID);
|
2002-04-28 23:27:06 +02:00
|
|
|
AU.setPreservesAll();
|
2002-01-31 01:46:09 +01:00
|
|
|
}
|
|
|
|
};
|
2001-07-06 18:59:10 +02:00
|
|
|
|
2001-11-07 22:16:29 +01:00
|
|
|
|
2001-11-09 06:27:34 +01:00
|
|
|
|
2002-01-31 01:46:09 +01:00
|
|
|
template <class PassType, class PassName, AnalysisID &ID>
|
|
|
|
Pass *New(const string &Message) {
|
|
|
|
return new PassPrinter<PassType, PassName>(Message, ID);
|
2001-07-03 17:30:38 +02:00
|
|
|
}
|
2002-01-31 01:46:09 +01:00
|
|
|
template <class PassType, class PassName>
|
|
|
|
Pass *New(const string &Message) {
|
|
|
|
return new PassPrinter<PassType, PassName>(Message, PassName::ID);
|
2001-07-03 17:30:38 +02:00
|
|
|
}
|
|
|
|
|
2002-01-31 01:46:09 +01:00
|
|
|
|
2002-04-28 23:45:36 +02:00
|
|
|
Pass *createPrintFunctionPass(const string &Message) {
|
2002-04-09 00:04:24 +02:00
|
|
|
return new PrintFunctionPass(Message, &std::cout);
|
2001-07-06 18:59:10 +02:00
|
|
|
}
|
2002-04-28 23:45:36 +02:00
|
|
|
Pass *createPrintModulePass(const string &Message) {
|
2002-01-31 01:46:09 +01:00
|
|
|
return new PrintModulePass(&std::cout);
|
2001-07-06 18:59:10 +02:00
|
|
|
}
|
2002-01-31 01:46:09 +01:00
|
|
|
|
2002-04-29 20:13:31 +02:00
|
|
|
struct InstForestHelper : public FunctionPass {
|
2002-04-29 16:57:45 +02:00
|
|
|
const char *getPassName() const { return "InstForest Printer"; }
|
|
|
|
|
2002-04-07 22:49:59 +02:00
|
|
|
void doit(Function *F) {
|
2002-04-29 20:13:31 +02:00
|
|
|
std::cout << InstForest<char>(F);
|
2002-01-31 01:46:09 +01:00
|
|
|
}
|
2002-04-28 23:27:06 +02:00
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
2002-01-31 01:46:09 +01:00
|
|
|
};
|
|
|
|
|
2002-04-27 08:56:12 +02:00
|
|
|
struct IndVars : public FunctionPass {
|
2002-04-29 16:57:45 +02:00
|
|
|
const char *getPassName() const { return "IndVars Printer"; }
|
|
|
|
|
2002-04-07 22:49:59 +02:00
|
|
|
void doit(Function *F) {
|
2002-04-28 18:21:53 +02:00
|
|
|
LoopInfo &LI = getAnalysis<LoopInfo>();
|
2002-04-07 22:49:59 +02:00
|
|
|
for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
|
2002-01-31 01:46:09 +01:00
|
|
|
if (PHINode *PN = dyn_cast<PHINode>(*I)) {
|
|
|
|
InductionVariable IV(PN, &LI);
|
|
|
|
if (IV.InductionType != InductionVariable::Unknown)
|
2002-03-26 23:43:12 +01:00
|
|
|
std::cout << IV;
|
2002-01-31 01:46:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-04-27 08:56:12 +02:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const {
|
2002-04-28 18:21:53 +02:00
|
|
|
AU.addRequired(LoopInfo::ID);
|
2002-04-28 23:27:06 +02:00
|
|
|
AU.setPreservesAll();
|
2002-01-31 01:46:09 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2002-04-27 08:56:12 +02:00
|
|
|
struct Exprs : public FunctionPass {
|
2002-04-29 16:57:45 +02:00
|
|
|
const char *getPassName() const { return "Expression Printer"; }
|
|
|
|
|
2002-04-07 22:49:59 +02:00
|
|
|
static void doit(Function *F) {
|
|
|
|
std::cout << "Classified expressions for: " << F->getName() << "\n";
|
|
|
|
for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
|
2002-03-26 23:43:12 +01:00
|
|
|
std::cout << *I;
|
2002-01-31 01:46:09 +01:00
|
|
|
|
|
|
|
if ((*I)->getType() == Type::VoidTy) continue;
|
|
|
|
analysis::ExprType R = analysis::ClassifyExpression(*I);
|
|
|
|
if (R.Var == *I) continue; // Doesn't tell us anything
|
|
|
|
|
2002-03-26 23:43:12 +01:00
|
|
|
std::cout << "\t\tExpr =";
|
2002-01-31 01:46:09 +01:00
|
|
|
switch (R.ExprTy) {
|
|
|
|
case analysis::ExprType::ScaledLinear:
|
2002-03-26 23:43:12 +01:00
|
|
|
WriteAsOperand(std::cout << "(", (Value*)R.Scale) << " ) *";
|
2002-01-31 01:46:09 +01:00
|
|
|
// fall through
|
|
|
|
case analysis::ExprType::Linear:
|
2002-03-26 23:43:12 +01:00
|
|
|
WriteAsOperand(std::cout << "(", R.Var) << " )";
|
2002-01-31 01:46:09 +01:00
|
|
|
if (R.Offset == 0) break;
|
2002-03-26 23:43:12 +01:00
|
|
|
else std::cout << " +";
|
2002-01-31 01:46:09 +01:00
|
|
|
// fall through
|
|
|
|
case analysis::ExprType::Constant:
|
2002-03-26 23:43:12 +01:00
|
|
|
if (R.Offset) WriteAsOperand(std::cout, (Value*)R.Offset);
|
|
|
|
else std::cout << " 0";
|
2002-01-31 01:46:09 +01:00
|
|
|
break;
|
|
|
|
}
|
2002-03-26 23:43:12 +01:00
|
|
|
std::cout << "\n\n";
|
2002-01-31 01:46:09 +01:00
|
|
|
}
|
|
|
|
}
|
2002-04-28 23:27:06 +02:00
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
2002-01-31 01:46:09 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template<class TraitClass>
|
|
|
|
class PrinterPass : public TraitClass {
|
|
|
|
const string Message;
|
|
|
|
public:
|
|
|
|
PrinterPass(const string &M) : Message(M) {}
|
2002-04-29 16:57:45 +02:00
|
|
|
|
2002-04-27 08:56:12 +02:00
|
|
|
virtual bool runOnFunction(Function *F) {
|
|
|
|
std::cout << Message << " on function '" << F->getName() << "'\n";
|
2002-01-31 01:46:09 +01:00
|
|
|
|
2002-04-07 22:49:59 +02:00
|
|
|
TraitClass::doit(F);
|
2002-01-31 01:46:09 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template<class PassClass>
|
|
|
|
Pass *Create(const string &Message) {
|
|
|
|
return new PassClass(Message);
|
2001-07-06 18:59:10 +02:00
|
|
|
}
|
|
|
|
|
2001-09-28 02:07:36 +02:00
|
|
|
|
2002-01-31 01:46:09 +01:00
|
|
|
|
2001-07-23 04:35:57 +02:00
|
|
|
enum Ans {
|
2002-01-31 01:46:09 +01:00
|
|
|
// global analyses
|
|
|
|
print, intervals, exprs, instforest, loops, indvars,
|
|
|
|
|
|
|
|
// ip analyses
|
2002-03-26 23:43:12 +01:00
|
|
|
printmodule, callgraph, datastructure, printusedtypes, unsafepointertypes,
|
2001-11-09 06:27:34 +01:00
|
|
|
|
2001-07-23 04:35:57 +02:00
|
|
|
domset, idom, domtree, domfrontier,
|
|
|
|
postdomset, postidom, postdomtree, postdomfrontier,
|
|
|
|
};
|
|
|
|
|
2001-07-23 21:27:24 +02:00
|
|
|
cl::String InputFilename ("", "Load <arg> file to analyze", cl::NoFlags, "-");
|
2001-07-23 22:22:30 +02:00
|
|
|
cl::Flag Quiet ("q", "Don't print analysis pass names");
|
|
|
|
cl::Alias QuietA ("quiet", "Alias for -q", cl::NoFlags, Quiet);
|
2001-07-23 04:35:57 +02:00
|
|
|
cl::EnumList<enum Ans> AnalysesList(cl::NoFlags,
|
2002-04-07 22:49:59 +02:00
|
|
|
clEnumVal(print , "Print each function"),
|
2001-07-23 04:35:57 +02:00
|
|
|
clEnumVal(intervals , "Print Interval Partitions"),
|
2002-01-31 01:46:09 +01:00
|
|
|
clEnumVal(exprs , "Classify Expressions"),
|
2001-09-14 03:42:42 +02:00
|
|
|
clEnumVal(instforest , "Print Instruction Forest"),
|
2002-03-26 23:43:12 +01:00
|
|
|
clEnumVal(loops , "Print natural loops"),
|
2001-11-26 20:18:11 +01:00
|
|
|
clEnumVal(indvars , "Print Induction Variables"),
|
2002-01-31 01:46:09 +01:00
|
|
|
|
|
|
|
clEnumVal(printmodule , "Print entire module"),
|
2001-09-28 02:07:36 +02:00
|
|
|
clEnumVal(callgraph , "Print Call Graph"),
|
2002-03-26 23:43:12 +01:00
|
|
|
clEnumVal(datastructure , "Print data structure information"),
|
|
|
|
clEnumVal(printusedtypes , "Print types used by module"),
|
|
|
|
clEnumVal(unsafepointertypes, "Print unsafe pointer types"),
|
2001-07-23 04:35:57 +02:00
|
|
|
|
|
|
|
clEnumVal(domset , "Print Dominator Sets"),
|
|
|
|
clEnumVal(idom , "Print Immediate Dominators"),
|
|
|
|
clEnumVal(domtree , "Print Dominator Tree"),
|
|
|
|
clEnumVal(domfrontier , "Print Dominance Frontier"),
|
|
|
|
|
|
|
|
clEnumVal(postdomset , "Print Postdominator Sets"),
|
|
|
|
clEnumVal(postidom , "Print Immediate Postdominators"),
|
|
|
|
clEnumVal(postdomtree , "Print Post Dominator Tree"),
|
|
|
|
clEnumVal(postdomfrontier, "Print Postdominance Frontier"),
|
|
|
|
0);
|
|
|
|
|
2002-01-31 01:46:09 +01:00
|
|
|
|
2001-07-03 17:30:38 +02:00
|
|
|
struct {
|
2001-07-23 04:35:57 +02:00
|
|
|
enum Ans AnID;
|
2002-01-31 01:46:09 +01:00
|
|
|
Pass *(*PassConstructor)(const string &Message);
|
|
|
|
} AnTable[] = {
|
|
|
|
// Global analyses
|
2002-04-28 23:45:36 +02:00
|
|
|
{ print , createPrintFunctionPass },
|
2002-04-28 18:21:53 +02:00
|
|
|
{ intervals , New<FunctionPass, IntervalPartition> },
|
|
|
|
{ loops , New<FunctionPass, LoopInfo> },
|
2002-04-29 20:13:31 +02:00
|
|
|
{ instforest , Create<PrinterPass<InstForestHelper> > },
|
2002-01-31 01:46:09 +01:00
|
|
|
{ indvars , Create<PrinterPass<IndVars> > },
|
|
|
|
{ exprs , Create<PrinterPass<Exprs> > },
|
|
|
|
|
|
|
|
// IP Analyses...
|
2002-04-28 23:45:36 +02:00
|
|
|
{ printmodule , createPrintModulePass },
|
2002-01-31 01:46:09 +01:00
|
|
|
{ printusedtypes , New<Pass, FindUsedTypes> },
|
2002-03-06 18:40:37 +01:00
|
|
|
{ callgraph , New<Pass, CallGraph> },
|
2002-03-26 23:43:12 +01:00
|
|
|
{ datastructure , New<Pass, DataStructure> },
|
2002-01-31 01:46:09 +01:00
|
|
|
{ unsafepointertypes, New<Pass, FindUnsafePointerTypes> },
|
|
|
|
|
|
|
|
// Dominator analyses
|
2002-04-28 18:21:53 +02:00
|
|
|
{ domset , New<FunctionPass, DominatorSet> },
|
|
|
|
{ idom , New<FunctionPass, ImmediateDominators> },
|
|
|
|
{ domtree , New<FunctionPass, DominatorTree> },
|
|
|
|
{ domfrontier , New<FunctionPass, DominanceFrontier> },
|
|
|
|
|
|
|
|
{ postdomset , New<FunctionPass, DominatorSet, DominatorSet::PostDomID> },
|
|
|
|
{ postidom , New<FunctionPass, ImmediateDominators, ImmediateDominators::PostDomID> },
|
|
|
|
{ postdomtree , New<FunctionPass, DominatorTree, DominatorTree::PostDomID> },
|
|
|
|
{ postdomfrontier , New<FunctionPass, DominanceFrontier, DominanceFrontier::PostDomID> },
|
2001-09-28 02:07:36 +02: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");
|
|
|
|
|
2002-03-26 23:43:12 +01:00
|
|
|
Module *CurMod = 0;
|
2002-02-01 06:09:35 +01:00
|
|
|
try {
|
2002-03-26 23:43:12 +01:00
|
|
|
CurMod = ParseBytecodeFile(InputFilename);
|
|
|
|
if (!CurMod && !(CurMod = ParseAssemblyFile(InputFilename))){
|
2002-02-01 06:09:35 +01:00
|
|
|
std::cerr << "Input file didn't read correctly.\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} catch (const ParseException &E) {
|
2002-02-25 00:25:24 +01:00
|
|
|
std::cerr << 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...
|
|
|
|
//
|
|
|
|
PassManager Analyses;
|
|
|
|
|
|
|
|
// Loop over all of the analyses looking for analyses to run...
|
2001-09-28 02:07:36 +02:00
|
|
|
for (unsigned i = 0; i < AnalysesList.size(); ++i) {
|
|
|
|
enum Ans AnalysisPass = AnalysesList[i];
|
|
|
|
|
2002-01-31 01:46:09 +01:00
|
|
|
for (unsigned j = 0; j < sizeof(AnTable)/sizeof(AnTable[0]); ++j) {
|
|
|
|
if (AnTable[j].AnID == AnalysisPass) {
|
|
|
|
string Message;
|
2001-09-28 02:07:36 +02:00
|
|
|
if (!Quiet)
|
2002-01-31 01:46:09 +01:00
|
|
|
Message = "\nRunning: '" +
|
|
|
|
string(AnalysesList.getArgDescription(AnalysisPass)) + "' analysis";
|
|
|
|
Analyses.add(AnTable[j].PassConstructor(Message));
|
2001-09-28 02:07:36 +02:00
|
|
|
break; // get an error later
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-03-26 23:43:12 +01:00
|
|
|
Analyses.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;
|
|
|
|
}
|