2002-01-31 08:46:09 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-07-03 23:30:38 +08:00
|
|
|
// LLVM 'Analyze' UTILITY
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
2001-07-21 03:16:29 +08:00
|
|
|
#include "llvm/Instruction.h"
|
2001-07-03 23:30:38 +08:00
|
|
|
#include "llvm/Module.h"
|
2002-04-08 04:49:59 +08:00
|
|
|
#include "llvm/Function.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-21 06:54:45 +08:00
|
|
|
#include <iostream>
|
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) {}
|
|
|
|
|
|
|
|
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-01-31 08:46:09 +08:00
|
|
|
virtual void getAnalysisUsageInfo(Pass::AnalysisSet &Required,
|
|
|
|
Pass::AnalysisSet &Destroyed,
|
|
|
|
Pass::AnalysisSet &Provided) {
|
|
|
|
Required.push_back(ID);
|
|
|
|
}
|
|
|
|
};
|
2001-11-27 03:18:11 +08:00
|
|
|
|
2002-01-31 08:46:09 +08:00
|
|
|
template <class PassName>
|
|
|
|
class PassPrinter<MethodPass, PassName> : public MethodPass {
|
|
|
|
const string Message;
|
|
|
|
const AnalysisID ID;
|
|
|
|
public:
|
|
|
|
PassPrinter(const string &M, AnalysisID id) : Message(M), ID(id) {}
|
|
|
|
|
2002-04-08 04:49:59 +08:00
|
|
|
virtual bool runOnMethod(Function *F) {
|
|
|
|
std::cout << Message << " on method '" << F->getName() << "'\n";
|
|
|
|
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-01-31 08:46:09 +08:00
|
|
|
virtual void getAnalysisUsageInfo(Pass::AnalysisSet &Required,
|
|
|
|
Pass::AnalysisSet &Destroyed,
|
|
|
|
Pass::AnalysisSet &Provided) {
|
|
|
|
Required.push_back(ID);
|
|
|
|
}
|
|
|
|
};
|
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-08 04:49:59 +08:00
|
|
|
Pass *NewPrintFunction(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-01-31 08:46:09 +08:00
|
|
|
Pass *NewPrintModule(const string &Message) {
|
|
|
|
return new PrintModulePass(&std::cout);
|
2001-07-07 00:59:10 +08:00
|
|
|
}
|
2002-01-31 08:46:09 +08:00
|
|
|
|
|
|
|
struct InstForest : public MethodPass {
|
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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct IndVars : public MethodPass {
|
2002-04-08 04:49:59 +08:00
|
|
|
void doit(Function *F) {
|
2002-01-31 08:46:09 +08:00
|
|
|
cfg::LoopInfo &LI = getAnalysis<cfg::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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void getAnalysisUsageInfo(Pass::AnalysisSet &Req,
|
|
|
|
Pass::AnalysisSet &, Pass::AnalysisSet &) {
|
|
|
|
Req.push_back(cfg::LoopInfo::ID);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Exprs : public MethodPass {
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template<class TraitClass>
|
|
|
|
class PrinterPass : public TraitClass {
|
|
|
|
const string Message;
|
|
|
|
public:
|
|
|
|
PrinterPass(const string &M) : Message(M) {}
|
|
|
|
|
2002-04-08 04:49:59 +08:00
|
|
|
virtual bool runOnMethod(Function *F) {
|
|
|
|
std::cout << Message << " on method '" << 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-08 04:49:59 +08:00
|
|
|
{ print , NewPrintFunction },
|
2002-01-31 08:46:09 +08:00
|
|
|
{ intervals , New<MethodPass, cfg::IntervalPartition> },
|
|
|
|
{ loops , New<MethodPass, cfg::LoopInfo> },
|
|
|
|
{ instforest , Create<PrinterPass<InstForest> > },
|
|
|
|
{ indvars , Create<PrinterPass<IndVars> > },
|
|
|
|
{ exprs , Create<PrinterPass<Exprs> > },
|
|
|
|
|
|
|
|
// IP Analyses...
|
|
|
|
{ printmodule , NewPrintModule },
|
|
|
|
{ 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
|
|
|
|
{ domset , New<MethodPass, cfg::DominatorSet> },
|
|
|
|
{ idom , New<MethodPass, cfg::ImmediateDominators> },
|
|
|
|
{ domtree , New<MethodPass, cfg::DominatorTree> },
|
|
|
|
{ domfrontier , New<MethodPass, cfg::DominanceFrontier> },
|
|
|
|
|
|
|
|
{ postdomset , New<MethodPass, cfg::DominatorSet, cfg::DominatorSet::PostDomID> },
|
|
|
|
{ postidom , New<MethodPass, cfg::ImmediateDominators, cfg::ImmediateDominators::PostDomID> },
|
|
|
|
{ postdomtree , New<MethodPass, cfg::DominatorTree, cfg::DominatorTree::PostDomID> },
|
|
|
|
{ postdomfrontier , New<MethodPass, cfg::DominanceFrontier, cfg::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;
|
|
|
|
}
|