Make analyze use the Pass repository to populate it's command line argument list

llvm-svn: 3114
This commit is contained in:
Chris Lattner 2002-07-27 01:08:50 +00:00
parent c8b7092e54
commit 5768f01926
1 changed files with 73 additions and 154 deletions

View File

@ -18,60 +18,16 @@
#include "llvm/Assembly/Parser.h" #include "llvm/Assembly/Parser.h"
#include "llvm/Assembly/PrintModulePass.h" #include "llvm/Assembly/PrintModulePass.h"
#include "llvm/Assembly/Writer.h" #include "llvm/Assembly/Writer.h"
#include "llvm/Analysis/Writer.h"
#include "llvm/Analysis/InstForest.h" #include "llvm/Analysis/InstForest.h"
#include "llvm/Analysis/Dominators.h"
#include "llvm/Analysis/IntervalPartition.h"
#include "llvm/Analysis/Expressions.h" #include "llvm/Analysis/Expressions.h"
#include "llvm/Analysis/InductionVariable.h" #include "llvm/Analysis/InductionVariable.h"
#include "llvm/Analysis/CallGraph.h"
#include "llvm/Analysis/LoopInfo.h"
#include "llvm/Analysis/DataStructure.h"
#include "llvm/Analysis/FindUnsafePointerTypes.h"
#include "llvm/Analysis/FindUsedTypes.h"
#include "llvm/Support/InstIterator.h" #include "llvm/Support/InstIterator.h"
#include "Support/CommandLine.h" #include "llvm/Support/PassNameParser.h"
#include <algorithm> #include <algorithm>
using std::ostream; using std::ostream;
//===----------------------------------------------------------------------===// #if 0
// 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>
static void printPass(PassType &P, ostream &O, Function &F) {
O << P;
}
// Other classes require more information to print out information, so we
// specialize the template here for them...
//
template<>
static void printPass(LocalDataStructures &P, ostream &O, Module &M) {
P.print(O, &M);
}
template<>
static void printPass(BUDataStructures &P, ostream &O, Module &M) {
P.print(O, &M);
}
template<>
static void printPass(FindUsedTypes &FUT, ostream &O, Module &M) {
FUT.printTypes(O, &M);
}
template<>
static void printPass(FindUnsafePointerTypes &FUPT, ostream &O, Module &M) {
FUPT.printResults(&M, O);
}
template <class PassType, class PassName> template <class PassType, class PassName>
class PassPrinter; // Do not implement class PassPrinter; // Do not implement
@ -115,20 +71,6 @@ public:
}; };
template <class PassType, class PassName>
Pass *New() {
return new PassPrinter<PassType, PassName>(PassName::ID);
}
Pass *createPrintFunctionPass() {
return new PrintFunctionPass("", &std::cout);
}
Pass *createPrintModulePass() {
return new PrintModulePass(&std::cout);
}
struct InstForestHelper : public FunctionPass { struct InstForestHelper : public FunctionPass {
const char *getPassName() const { return "InstForest Printer"; } const char *getPassName() const { return "InstForest Printer"; }
@ -150,7 +92,7 @@ struct IndVars : public FunctionPass {
if (PHINode *PN = dyn_cast<PHINode>(*I)) { if (PHINode *PN = dyn_cast<PHINode>(*I)) {
InductionVariable IV(PN, &LI); InductionVariable IV(PN, &LI);
if (IV.InductionType != InductionVariable::Unknown) if (IV.InductionType != InductionVariable::Unknown)
std::cout << IV; IV.print(std::cout);
} }
} }
@ -194,40 +136,63 @@ struct Exprs : public FunctionPass {
AU.setPreservesAll(); AU.setPreservesAll();
} }
}; };
#endif
struct ModulePassPrinter : public Pass {
Pass *PassToPrint;
ModulePassPrinter(Pass *PI) : PassToPrint(PI) {}
template<class TraitClass> virtual bool run(Module &M) {
struct PrinterPass : public TraitClass { std::cout << "Printing Analysis info for Pass "
PrinterPass() {} << PassToPrint->getPassName() << ":\n";
PassToPrint->print(std::cout, &M);
virtual bool runOnFunction(Function &F) {
std::cout << "Running on function '" << F.getName() << "'\n"; // Get and print pass...
TraitClass::doit(F);
return false; return false;
} }
}; };
struct FunctionPassPrinter : public FunctionPass {
const PassInfo *PassToPrint;
FunctionPassPrinter(const PassInfo *PI) : PassToPrint(PI) {}
template<class PassClass> virtual bool runOnFunction(Function &F) {
Pass *Create() { std::cout << "Printing Analysis info for function '" << F.getName()
return new PassClass(); << "': Pass " << PassToPrint->getPassName() << ":\n";
} getAnalysis<Pass>(PassToPrint).print(std::cout, F.getParent());
// Get and print pass...
return false;
}
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
enum Ans { AU.addRequired(PassToPrint);
// global analyses AU.setPreservesAll();
print, intervals, exprs, instforest, loops, indvars, }
// ip analyses
printmodule, callgraph, datastructure, budatastructure,
printusedtypes, unsafepointertypes,
domset, idom, domtree, domfrontier,
postdomset, postidom, postdomtree, postdomfrontier,
}; };
struct BasicBlockPassPrinter : public BasicBlockPass {
const PassInfo *PassToPrint;
BasicBlockPassPrinter(const PassInfo *PI) : PassToPrint(PI) {}
virtual bool runOnBasicBlock(BasicBlock &BB) {
std::cout << "Printing Analysis info for BasicBlock '" << BB.getName()
<< "': Pass " << PassToPrint->getPassName() << ":\n";
getAnalysis<Pass>(PassToPrint).print(std::cout, BB.getParent()->getParent());
// Get and print pass...
return false;
}
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
AU.addRequired(PassToPrint);
AU.setPreservesAll();
}
};
static cl::opt<std::string> static cl::opt<std::string>
InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"), InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"),
cl::value_desc("filename")); cl::value_desc("filename"));
@ -236,66 +201,13 @@ static cl::opt<bool> Quiet("q", cl::desc("Don't print analysis pass names"));
static cl::alias QuietA("quiet", cl::desc("Alias for -q"), static cl::alias QuietA("quiet", cl::desc("Alias for -q"),
cl::aliasopt(Quiet)); cl::aliasopt(Quiet));
static cl::list<enum Ans> // The AnalysesList is automatically populated with registered Passes by the
AnalysesList(cl::desc("Analyses available:"), // PassNameParser.
cl::values( //
clEnumVal(print , "Print each function"), static cl::list<const PassInfo*, bool,
clEnumVal(intervals , "Print Interval Partitions"), FilteredPassNameParser<PassInfo::Analysis> >
clEnumVal(exprs , "Classify Expressions"), AnalysesList(cl::desc("Analyses available:"));
clEnumVal(instforest , "Print Instruction Forest"),
clEnumVal(loops , "Print natural loops"),
clEnumVal(indvars , "Print Induction Variables"),
clEnumVal(printmodule , "Print entire module"),
clEnumVal(callgraph , "Print Call Graph"),
clEnumVal(datastructure , "Print data structure information"),
clEnumVal(budatastructure, "Print bottom-up data structure information"),
clEnumVal(printusedtypes , "Print types used by module"),
clEnumVal(unsafepointertypes, "Print unsafe pointer types"),
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));
struct {
enum Ans AnID;
Pass *(*PassConstructor)();
} AnTable[] = {
// Global analyses
{ print , createPrintFunctionPass },
{ intervals , New<FunctionPass, IntervalPartition> },
{ loops , New<FunctionPass, LoopInfo> },
{ instforest , Create<PrinterPass<InstForestHelper> > },
{ indvars , Create<PrinterPass<IndVars> > },
{ exprs , Create<PrinterPass<Exprs> > },
// IP Analyses...
{ printmodule , createPrintModulePass },
{ printusedtypes , New<Pass, FindUsedTypes> },
{ callgraph , New<Pass, CallGraph> },
{ datastructure , New<Pass, LocalDataStructures> },
{ budatastructure , New<Pass, BUDataStructures> },
{ unsafepointertypes, New<Pass, FindUnsafePointerTypes> },
// Dominator analyses
{ domset , New<FunctionPass, DominatorSet> },
{ idom , New<FunctionPass, ImmediateDominators> },
{ domtree , New<FunctionPass, DominatorTree> },
{ domfrontier , New<FunctionPass, DominanceFrontier> },
{ postdomset , New<FunctionPass, PostDominatorSet> },
{ postidom , New<FunctionPass, ImmediatePostDominators> },
{ postdomtree , New<FunctionPass, PostDominatorTree> },
{ postdomfrontier , New<FunctionPass, PostDominanceFrontier> },
};
int main(int argc, char **argv) { int main(int argc, char **argv) {
cl::ParseCommandLineOptions(argc, argv, " llvm analysis printer tool\n"); cl::ParseCommandLineOptions(argc, argv, " llvm analysis printer tool\n");
@ -315,21 +227,28 @@ int main(int argc, char **argv) {
// Create a PassManager to hold and optimize the collection of passes we are // Create a PassManager to hold and optimize the collection of passes we are
// about to build... // about to build...
// //
PassManager Analyses; PassManager Passes;
// Loop over all of the analyses looking for analyses to run... // Create a new optimization pass for each one specified on the command line
for (unsigned i = 0; i < AnalysesList.size(); ++i) { for (unsigned i = 0; i < AnalysesList.size(); ++i) {
enum Ans AnalysisPass = AnalysesList[i]; const PassInfo *Analysis = AnalysesList[i];
if (Analysis->getNormalCtor()) {
Pass *P = Analysis->getNormalCtor()();
Passes.add(P);
for (unsigned j = 0; j < sizeof(AnTable)/sizeof(AnTable[0]); ++j) { if (BasicBlockPass *BBP = dynamic_cast<BasicBlockPass*>(P))
if (AnTable[j].AnID == AnalysisPass) { Passes.add(new BasicBlockPassPrinter(Analysis));
Analyses.add(AnTable[j].PassConstructor()); else if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P))
break; // get an error later Passes.add(new FunctionPassPrinter(Analysis));
} else
} Passes.add(new ModulePassPrinter(P));
}
Analyses.run(*CurMod); } else
cerr << "Cannot create pass: " << Analysis->getPassName() << "\n";
}
Passes.run(*CurMod);
delete CurMod; delete CurMod;
return 0; return 0;