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
|
|
|
|
//
|
|
|
|
//===------------------------------------------------------------------------===
|
|
|
|
|
|
|
|
#include <iostream>
|
2001-07-21 03:16:29 +08:00
|
|
|
#include "llvm/Instruction.h"
|
2001-07-03 23:30:38 +08:00
|
|
|
#include "llvm/Module.h"
|
2001-07-21 03:16:29 +08:00
|
|
|
#include "llvm/Method.h"
|
2001-07-03 23:30:38 +08:00
|
|
|
#include "llvm/Bytecode/Reader.h"
|
|
|
|
#include "llvm/Assembly/Parser.h"
|
2001-07-24 01:46:59 +08:00
|
|
|
#include "llvm/Support/CommandLine.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-07-03 23:30:38 +08:00
|
|
|
|
2001-07-07 00:59:10 +08:00
|
|
|
static void PrintMethod(Method *M) {
|
|
|
|
cout << M;
|
2001-07-03 23:30:38 +08:00
|
|
|
}
|
|
|
|
|
2001-07-07 00:59:10 +08:00
|
|
|
static void PrintIntervalPartition(Method *M) {
|
|
|
|
cout << cfg::IntervalPartition(M);
|
|
|
|
}
|
|
|
|
|
2001-07-21 03:16:29 +08:00
|
|
|
static void PrintClassifiedExprs(Method *M) {
|
|
|
|
cout << "Classified expressions for: " << M->getName() << endl;
|
|
|
|
Method::inst_iterator I = M->inst_begin(), E = M->inst_end();
|
|
|
|
for (; I != E; ++I) {
|
|
|
|
cout << *I;
|
|
|
|
|
|
|
|
if ((*I)->getType() == Type::VoidTy) continue;
|
2001-07-22 03:08:44 +08:00
|
|
|
analysis::ExprType R = analysis::ClassifyExpression(*I);
|
2001-07-21 03:16:29 +08:00
|
|
|
if (R.Var == *I) continue; // Doesn't tell us anything
|
|
|
|
|
|
|
|
cout << "\t\tExpr =";
|
2001-07-22 03:08:44 +08:00
|
|
|
switch (R.ExprTy) {
|
|
|
|
case analysis::ExprType::ScaledLinear:
|
2001-07-26 06:48:37 +08:00
|
|
|
WriteAsOperand(cout << "(", (Value*)R.Scale) << " ) *";
|
2001-07-21 03:16:29 +08:00
|
|
|
// fall through
|
2001-07-22 03:08:44 +08:00
|
|
|
case analysis::ExprType::Linear:
|
2001-07-26 06:48:37 +08:00
|
|
|
WriteAsOperand(cout << "(", R.Var) << " )";
|
2001-07-21 03:16:29 +08:00
|
|
|
if (R.Offset == 0) break;
|
|
|
|
else cout << " +";
|
|
|
|
// fall through
|
2001-07-22 03:08:44 +08:00
|
|
|
case analysis::ExprType::Constant:
|
2001-07-21 03:16:29 +08:00
|
|
|
if (R.Offset) WriteAsOperand(cout, (Value*)R.Offset); else cout << " 0";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
cout << endl << endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-09-14 09:42:42 +08:00
|
|
|
static void PrintInstForest(Method *M) {
|
|
|
|
cout << analysis::InstForest<char>(M);
|
|
|
|
}
|
2001-07-07 00:59:10 +08:00
|
|
|
|
|
|
|
static void PrintDominatorSets(Method *M) {
|
2001-07-03 23:30:38 +08:00
|
|
|
cout << cfg::DominatorSet(M);
|
|
|
|
}
|
2001-07-07 00:59:10 +08:00
|
|
|
static void PrintImmediateDominators(Method *M) {
|
2001-07-03 23:30:38 +08:00
|
|
|
cout << cfg::ImmediateDominators(M);
|
|
|
|
}
|
2001-07-07 00:59:10 +08:00
|
|
|
static void PrintDominatorTree(Method *M) {
|
2001-07-03 23:30:38 +08:00
|
|
|
cout << cfg::DominatorTree(M);
|
|
|
|
}
|
2001-07-07 00:59:10 +08:00
|
|
|
static void PrintDominanceFrontier(Method *M) {
|
2001-07-03 23:30:38 +08:00
|
|
|
cout << cfg::DominanceFrontier(M);
|
|
|
|
}
|
|
|
|
|
2001-07-07 00:59:10 +08:00
|
|
|
static void PrintPostDominatorSets(Method *M) {
|
|
|
|
cout << cfg::DominatorSet(M, true);
|
|
|
|
}
|
|
|
|
static void PrintImmediatePostDoms(Method *M) {
|
|
|
|
cout << cfg::ImmediateDominators(cfg::DominatorSet(M, true));
|
|
|
|
}
|
|
|
|
static void PrintPostDomTree(Method *M) {
|
|
|
|
cout << cfg::DominatorTree(cfg::DominatorSet(M, true));
|
|
|
|
}
|
|
|
|
static void PrintPostDomFrontier(Method *M) {
|
|
|
|
cout << cfg::DominanceFrontier(cfg::DominatorSet(M, true));
|
|
|
|
}
|
|
|
|
|
2001-07-23 10:35:57 +08:00
|
|
|
enum Ans {
|
2001-09-14 09:42:42 +08:00
|
|
|
print, intervals, exprclassify, instforest,
|
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,
|
|
|
|
clEnumVal(print , "Print each Method"),
|
|
|
|
clEnumVal(intervals , "Print Interval Partitions"),
|
|
|
|
clEnumVal(exprclassify , "Classify Expressions"),
|
2001-09-14 09:42:42 +08:00
|
|
|
clEnumVal(instforest , "Print Instruction Forest"),
|
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);
|
|
|
|
|
2001-07-03 23:30:38 +08:00
|
|
|
struct {
|
2001-07-23 10:35:57 +08:00
|
|
|
enum Ans AnID;
|
2001-07-07 00:59:10 +08:00
|
|
|
void (*AnPtr)(Method *M);
|
2001-07-03 23:30:38 +08:00
|
|
|
} AnTable[] = {
|
2001-07-23 10:35:57 +08:00
|
|
|
{ print , PrintMethod },
|
|
|
|
{ intervals , PrintIntervalPartition },
|
|
|
|
{ exprclassify , PrintClassifiedExprs },
|
2001-09-14 09:42:42 +08:00
|
|
|
{ instforest , PrintInstForest },
|
2001-07-23 10:35:57 +08:00
|
|
|
|
|
|
|
{ domset , PrintDominatorSets },
|
|
|
|
{ idom , PrintImmediateDominators },
|
|
|
|
{ domtree , PrintDominatorTree },
|
|
|
|
{ domfrontier , PrintDominanceFrontier },
|
|
|
|
|
|
|
|
{ postdomset , PrintPostDominatorSets },
|
|
|
|
{ postidom , PrintImmediatePostDoms },
|
|
|
|
{ postdomtree , PrintPostDomTree },
|
|
|
|
{ postdomfrontier, PrintPostDomFrontier },
|
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");
|
|
|
|
|
2001-07-24 03:27:24 +08:00
|
|
|
Module *C = ParseBytecodeFile(InputFilename);
|
|
|
|
if (!C && !(C = ParseAssemblyFile(InputFilename))) {
|
2001-07-07 00:59:10 +08:00
|
|
|
cerr << "Input file didn't read correctly.\n";
|
|
|
|
return 1;
|
2001-07-03 23:30:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Loop over all of the methods in the module...
|
2001-07-07 00:59:10 +08:00
|
|
|
for (Module::iterator I = C->begin(), E = C->end(); I != E; ++I) {
|
|
|
|
Method *M = *I;
|
2001-07-21 03:16:29 +08:00
|
|
|
if (M->isExternal()) continue;
|
2001-07-03 23:30:38 +08:00
|
|
|
|
2001-07-23 10:35:57 +08:00
|
|
|
for (unsigned i = 0; i < AnalysesList.size(); ++i) {
|
|
|
|
enum Ans AnalysisPass = AnalysesList[i];
|
|
|
|
|
|
|
|
// Loop over all of the analyses to be run...
|
2001-07-03 23:30:38 +08:00
|
|
|
unsigned j;
|
2001-07-23 10:35:57 +08:00
|
|
|
for (j = 0; j < sizeof(AnTable)/sizeof(AnTable[0]); ++j) {
|
|
|
|
if (AnalysisPass == AnTable[j].AnID) {
|
2001-07-03 23:30:38 +08:00
|
|
|
if (!Quiet)
|
2001-07-23 10:35:57 +08:00
|
|
|
cerr << "Running: " << AnalysesList.getArgDescription(AnalysisPass)
|
|
|
|
<< " analysis on '" << ((Value*)M)->getName() << "'!\n";
|
2001-07-03 23:30:38 +08:00
|
|
|
AnTable[j].AnPtr(M);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (j == sizeof(AnTable)/sizeof(AnTable[0]))
|
2001-07-23 10:35:57 +08:00
|
|
|
cerr << "Analysis tables inconsistent!\n";
|
2001-07-03 23:30:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
delete C;
|
|
|
|
return 0;
|
|
|
|
}
|