2001-07-03 15:30:38 +00: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
|
|
|
|
//
|
|
|
|
//===------------------------------------------------------------------------===
|
|
|
|
|
2001-07-20 19:16:29 +00:00
|
|
|
#include "llvm/Instruction.h"
|
2001-07-03 15:30:38 +00:00
|
|
|
#include "llvm/Module.h"
|
2001-07-20 19:16:29 +00:00
|
|
|
#include "llvm/Method.h"
|
2001-07-03 15:30:38 +00:00
|
|
|
#include "llvm/Bytecode/Reader.h"
|
|
|
|
#include "llvm/Assembly/Parser.h"
|
|
|
|
#include "llvm/Analysis/Writer.h"
|
2001-09-14 01:42:42 +00:00
|
|
|
#include "llvm/Analysis/InstForest.h"
|
2001-07-03 15:30:38 +00:00
|
|
|
#include "llvm/Analysis/Dominators.h"
|
|
|
|
#include "llvm/Analysis/IntervalPartition.h"
|
2001-07-20 19:16:29 +00:00
|
|
|
#include "llvm/Analysis/Expressions.h"
|
2001-11-26 19:18:11 +00:00
|
|
|
#include "llvm/Analysis/InductionVariable.h"
|
2001-09-28 00:07:36 +00:00
|
|
|
#include "llvm/Analysis/CallGraph.h"
|
2001-11-26 19:18:11 +00:00
|
|
|
#include "llvm/Analysis/LoopInfo.h"
|
2001-11-07 21:16:29 +00:00
|
|
|
#include "llvm/Analysis/FindUnsafePointerTypes.h"
|
2001-11-09 05:27:34 +00:00
|
|
|
#include "llvm/Analysis/FindUsedTypes.h"
|
2001-11-27 00:03:19 +00:00
|
|
|
#include "Support/CommandLine.h"
|
2001-09-28 00:07:36 +00:00
|
|
|
#include <algorithm>
|
2001-07-03 15:30:38 +00:00
|
|
|
|
2001-07-06 16:59:10 +00:00
|
|
|
static void PrintMethod(Method *M) {
|
|
|
|
cout << M;
|
2001-07-03 15:30:38 +00:00
|
|
|
}
|
|
|
|
|
2001-07-06 16:59:10 +00:00
|
|
|
static void PrintIntervalPartition(Method *M) {
|
|
|
|
cout << cfg::IntervalPartition(M);
|
|
|
|
}
|
|
|
|
|
2001-07-20 19:16:29 +00: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-21 19:08:44 +00:00
|
|
|
analysis::ExprType R = analysis::ClassifyExpression(*I);
|
2001-07-20 19:16:29 +00:00
|
|
|
if (R.Var == *I) continue; // Doesn't tell us anything
|
|
|
|
|
|
|
|
cout << "\t\tExpr =";
|
2001-07-21 19:08:44 +00:00
|
|
|
switch (R.ExprTy) {
|
|
|
|
case analysis::ExprType::ScaledLinear:
|
2001-07-25 22:48:37 +00:00
|
|
|
WriteAsOperand(cout << "(", (Value*)R.Scale) << " ) *";
|
2001-07-20 19:16:29 +00:00
|
|
|
// fall through
|
2001-07-21 19:08:44 +00:00
|
|
|
case analysis::ExprType::Linear:
|
2001-07-25 22:48:37 +00:00
|
|
|
WriteAsOperand(cout << "(", R.Var) << " )";
|
2001-07-20 19:16:29 +00:00
|
|
|
if (R.Offset == 0) break;
|
|
|
|
else cout << " +";
|
|
|
|
// fall through
|
2001-07-21 19:08:44 +00:00
|
|
|
case analysis::ExprType::Constant:
|
2001-07-20 19:16:29 +00:00
|
|
|
if (R.Offset) WriteAsOperand(cout, (Value*)R.Offset); else cout << " 0";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
cout << endl << endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-11-26 19:18:11 +00:00
|
|
|
static void PrintInductionVariables(Method *M) {
|
|
|
|
cfg::LoopInfo LI(M);
|
|
|
|
for (Method::inst_iterator I = M->inst_begin(), E = M->inst_end();
|
|
|
|
I != E; ++I) {
|
|
|
|
InductionVariable IV(*I, &LI);
|
|
|
|
if (IV.InductionType != InductionVariable::Unknown)
|
|
|
|
cout << IV;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-09-14 01:42:42 +00:00
|
|
|
static void PrintInstForest(Method *M) {
|
|
|
|
cout << analysis::InstForest<char>(M);
|
|
|
|
}
|
2001-11-26 19:18:11 +00:00
|
|
|
static void PrintLoops(Method *M) {
|
|
|
|
cout << cfg::LoopInfo(M);
|
|
|
|
}
|
2001-09-28 00:07:36 +00:00
|
|
|
static void PrintCallGraph(Module *M) {
|
|
|
|
cout << cfg::CallGraph(M);
|
|
|
|
}
|
2001-07-06 16:59:10 +00:00
|
|
|
|
2001-11-07 21:16:29 +00:00
|
|
|
static void PrintUnsafePtrTypes(Module *M) {
|
|
|
|
FindUnsafePointerTypes FUPT;
|
|
|
|
FUPT.run(M);
|
|
|
|
FUPT.printResults(M, cout);
|
|
|
|
}
|
|
|
|
|
2001-11-09 05:27:34 +00:00
|
|
|
static void PrintUsedTypes(Module *M) {
|
|
|
|
FindUsedTypes FUT;
|
|
|
|
FUT.run(M);
|
|
|
|
FUT.printTypes(cout, M);
|
|
|
|
}
|
|
|
|
|
2001-07-06 16:59:10 +00:00
|
|
|
static void PrintDominatorSets(Method *M) {
|
2001-07-03 15:30:38 +00:00
|
|
|
cout << cfg::DominatorSet(M);
|
|
|
|
}
|
2001-07-06 16:59:10 +00:00
|
|
|
static void PrintImmediateDominators(Method *M) {
|
2001-07-03 15:30:38 +00:00
|
|
|
cout << cfg::ImmediateDominators(M);
|
|
|
|
}
|
2001-07-06 16:59:10 +00:00
|
|
|
static void PrintDominatorTree(Method *M) {
|
2001-07-03 15:30:38 +00:00
|
|
|
cout << cfg::DominatorTree(M);
|
|
|
|
}
|
2001-07-06 16:59:10 +00:00
|
|
|
static void PrintDominanceFrontier(Method *M) {
|
2001-07-03 15:30:38 +00:00
|
|
|
cout << cfg::DominanceFrontier(M);
|
|
|
|
}
|
|
|
|
|
2001-07-06 16:59:10 +00: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-09-28 00:07:36 +00:00
|
|
|
|
2001-07-23 02:35:57 +00:00
|
|
|
enum Ans {
|
2001-09-28 00:07:36 +00:00
|
|
|
PassDone, // Unique Marker
|
2001-11-26 19:18:11 +00:00
|
|
|
print, intervals, exprclassify, instforest, loops, indvars, callgraph,
|
2001-11-09 05:27:34 +00:00
|
|
|
printusedtypes, unsafepointertypes,
|
|
|
|
|
2001-07-23 02:35:57 +00:00
|
|
|
domset, idom, domtree, domfrontier,
|
|
|
|
postdomset, postidom, postdomtree, postdomfrontier,
|
|
|
|
};
|
|
|
|
|
2001-07-23 19:27:24 +00:00
|
|
|
cl::String InputFilename ("", "Load <arg> file to analyze", cl::NoFlags, "-");
|
2001-07-23 20:22:30 +00:00
|
|
|
cl::Flag Quiet ("q", "Don't print analysis pass names");
|
|
|
|
cl::Alias QuietA ("quiet", "Alias for -q", cl::NoFlags, Quiet);
|
2001-07-23 02:35:57 +00: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 01:42:42 +00:00
|
|
|
clEnumVal(instforest , "Print Instruction Forest"),
|
2001-11-26 19:18:11 +00:00
|
|
|
clEnumVal(loops , "Print Loops"),
|
|
|
|
clEnumVal(indvars , "Print Induction Variables"),
|
2001-09-28 00:07:36 +00:00
|
|
|
clEnumVal(callgraph , "Print Call Graph"),
|
2001-11-09 05:27:34 +00:00
|
|
|
clEnumVal(printusedtypes , "Print Types Used by Module"),
|
2001-11-07 21:16:29 +00:00
|
|
|
clEnumVal(unsafepointertypes, "Print Unsafe Pointer Types"),
|
2001-07-23 02:35:57 +00: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 15:30:38 +00:00
|
|
|
struct {
|
2001-07-23 02:35:57 +00:00
|
|
|
enum Ans AnID;
|
2001-07-06 16:59:10 +00:00
|
|
|
void (*AnPtr)(Method *M);
|
2001-09-28 00:07:36 +00:00
|
|
|
} MethAnTable[] = {
|
2001-07-23 02:35:57 +00:00
|
|
|
{ print , PrintMethod },
|
|
|
|
{ intervals , PrintIntervalPartition },
|
|
|
|
{ exprclassify , PrintClassifiedExprs },
|
2001-09-14 01:42:42 +00:00
|
|
|
{ instforest , PrintInstForest },
|
2001-11-26 19:18:11 +00:00
|
|
|
{ loops , PrintLoops },
|
|
|
|
{ indvars , PrintInductionVariables },
|
2001-07-23 02:35:57 +00:00
|
|
|
|
|
|
|
{ domset , PrintDominatorSets },
|
|
|
|
{ idom , PrintImmediateDominators },
|
|
|
|
{ domtree , PrintDominatorTree },
|
|
|
|
{ domfrontier , PrintDominanceFrontier },
|
|
|
|
|
|
|
|
{ postdomset , PrintPostDominatorSets },
|
|
|
|
{ postidom , PrintImmediatePostDoms },
|
|
|
|
{ postdomtree , PrintPostDomTree },
|
|
|
|
{ postdomfrontier, PrintPostDomFrontier },
|
2001-07-03 15:30:38 +00:00
|
|
|
};
|
|
|
|
|
2001-09-28 00:07:36 +00:00
|
|
|
pair<enum Ans, void (*)(Module *)> ModAnTable[] = {
|
2001-11-09 05:27:34 +00:00
|
|
|
pair<enum Ans, void (*)(Module *)>(callgraph , PrintCallGraph),
|
|
|
|
pair<enum Ans, void (*)(Module *)>(printusedtypes , PrintUsedTypes),
|
2001-11-07 21:16:29 +00:00
|
|
|
pair<enum Ans, void (*)(Module *)>(unsafepointertypes, PrintUnsafePtrTypes),
|
2001-09-28 00:07:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-07-03 15:30:38 +00:00
|
|
|
int main(int argc, char **argv) {
|
2001-07-23 02:35:57 +00:00
|
|
|
cl::ParseCommandLineOptions(argc, argv, " llvm analysis printer tool\n");
|
|
|
|
|
2001-07-23 19:27:24 +00:00
|
|
|
Module *C = ParseBytecodeFile(InputFilename);
|
|
|
|
if (!C && !(C = ParseAssemblyFile(InputFilename))) {
|
2001-07-06 16:59:10 +00:00
|
|
|
cerr << "Input file didn't read correctly.\n";
|
|
|
|
return 1;
|
2001-07-03 15:30:38 +00:00
|
|
|
}
|
|
|
|
|
2001-09-28 00:07:36 +00:00
|
|
|
// Loop over all of the analyses looking for module level analyses to run...
|
|
|
|
for (unsigned i = 0; i < AnalysesList.size(); ++i) {
|
|
|
|
enum Ans AnalysisPass = AnalysesList[i];
|
|
|
|
|
|
|
|
for (unsigned j = 0; j < sizeof(ModAnTable)/sizeof(ModAnTable[0]); ++j) {
|
|
|
|
if (ModAnTable[j].first == AnalysisPass) {
|
|
|
|
if (!Quiet)
|
|
|
|
cerr << "Running: " << AnalysesList.getArgDescription(AnalysisPass)
|
|
|
|
<< " analysis on module!\n";
|
|
|
|
ModAnTable[j].second(C);
|
|
|
|
AnalysesList[i] = PassDone; // Mark pass as complete so that we don't
|
|
|
|
break; // get an error later
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-07-03 15:30:38 +00:00
|
|
|
// Loop over all of the methods in the module...
|
2001-07-06 16:59:10 +00:00
|
|
|
for (Module::iterator I = C->begin(), E = C->end(); I != E; ++I) {
|
|
|
|
Method *M = *I;
|
2001-07-20 19:16:29 +00:00
|
|
|
if (M->isExternal()) continue;
|
2001-07-03 15:30:38 +00:00
|
|
|
|
2001-07-23 02:35:57 +00:00
|
|
|
for (unsigned i = 0; i < AnalysesList.size(); ++i) {
|
|
|
|
enum Ans AnalysisPass = AnalysesList[i];
|
2001-09-28 00:07:36 +00:00
|
|
|
if (AnalysisPass == PassDone) continue; // Don't rerun module analyses
|
2001-07-23 02:35:57 +00:00
|
|
|
|
|
|
|
// Loop over all of the analyses to be run...
|
2001-07-03 15:30:38 +00:00
|
|
|
unsigned j;
|
2001-09-28 00:07:36 +00:00
|
|
|
for (j = 0; j < sizeof(MethAnTable)/sizeof(MethAnTable[0]); ++j) {
|
|
|
|
if (AnalysisPass == MethAnTable[j].AnID) {
|
2001-07-03 15:30:38 +00:00
|
|
|
if (!Quiet)
|
2001-07-23 02:35:57 +00:00
|
|
|
cerr << "Running: " << AnalysesList.getArgDescription(AnalysisPass)
|
|
|
|
<< " analysis on '" << ((Value*)M)->getName() << "'!\n";
|
2001-09-28 00:07:36 +00:00
|
|
|
MethAnTable[j].AnPtr(M);
|
2001-07-03 15:30:38 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2001-09-28 00:07:36 +00:00
|
|
|
if (j == sizeof(MethAnTable)/sizeof(MethAnTable[0]))
|
2001-07-23 02:35:57 +00:00
|
|
|
cerr << "Analysis tables inconsistent!\n";
|
2001-07-03 15:30:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
delete C;
|
|
|
|
return 0;
|
|
|
|
}
|