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