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
|
|
|
|
//
|
|
|
|
//===------------------------------------------------------------------------===
|
|
|
|
|
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-12-04 01:27:42 +08:00
|
|
|
#include "llvm/iPHINode.h"
|
2001-07-03 23:30:38 +08:00
|
|
|
#include "llvm/Bytecode/Reader.h"
|
|
|
|
#include "llvm/Assembly/Parser.h"
|
|
|
|
#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"
|
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"
|
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>
|
|
|
|
using std::cout;
|
|
|
|
using std::cerr;
|
|
|
|
using std::pair;
|
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) {
|
2002-01-21 06:54:45 +08:00
|
|
|
cout << "Classified expressions for: " << M->getName() << "\n";
|
2001-07-21 03:16:29 +08:00
|
|
|
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;
|
|
|
|
}
|
2002-01-21 06:54:45 +08:00
|
|
|
cout << "\n\n";
|
2001-07-21 03:16:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-11-27 03:18:11 +08:00
|
|
|
static void PrintInductionVariables(Method *M) {
|
|
|
|
cfg::LoopInfo LI(M);
|
|
|
|
for (Method::inst_iterator I = M->inst_begin(), E = M->inst_end();
|
2001-12-04 01:27:42 +08:00
|
|
|
I != E; ++I)
|
|
|
|
if (PHINode *PN = dyn_cast<PHINode>(*I)) {
|
|
|
|
InductionVariable IV(PN, &LI);
|
|
|
|
if (IV.InductionType != InductionVariable::Unknown)
|
|
|
|
cout << IV;
|
|
|
|
}
|
2001-11-27 03:18:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-09-14 09:42:42 +08:00
|
|
|
static void PrintInstForest(Method *M) {
|
|
|
|
cout << analysis::InstForest<char>(M);
|
|
|
|
}
|
2001-11-27 03:18:11 +08:00
|
|
|
static void PrintLoops(Method *M) {
|
|
|
|
cout << cfg::LoopInfo(M);
|
|
|
|
}
|
2001-09-28 08:07:36 +08:00
|
|
|
static void PrintCallGraph(Module *M) {
|
|
|
|
cout << cfg::CallGraph(M);
|
|
|
|
}
|
2001-07-07 00:59:10 +08:00
|
|
|
|
2001-11-08 05:16:29 +08:00
|
|
|
static void PrintUnsafePtrTypes(Module *M) {
|
|
|
|
FindUnsafePointerTypes FUPT;
|
|
|
|
FUPT.run(M);
|
|
|
|
FUPT.printResults(M, cout);
|
|
|
|
}
|
|
|
|
|
2001-11-09 13:27:34 +08:00
|
|
|
static void PrintUsedTypes(Module *M) {
|
|
|
|
FindUsedTypes FUT;
|
|
|
|
FUT.run(M);
|
|
|
|
FUT.printTypes(cout, 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-09-28 08:07:36 +08:00
|
|
|
|
2001-07-23 10:35:57 +08:00
|
|
|
enum Ans {
|
2001-09-28 08:07:36 +08:00
|
|
|
PassDone, // Unique Marker
|
2001-11-27 03:18:11 +08:00
|
|
|
print, intervals, exprclassify, instforest, loops, indvars, callgraph,
|
2001-11-09 13:27:34 +08:00
|
|
|
printusedtypes, unsafepointertypes,
|
|
|
|
|
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-11-27 03:18:11 +08:00
|
|
|
clEnumVal(loops , "Print Loops"),
|
|
|
|
clEnumVal(indvars , "Print Induction Variables"),
|
2001-09-28 08:07:36 +08:00
|
|
|
clEnumVal(callgraph , "Print Call Graph"),
|
2001-11-09 13:27:34 +08:00
|
|
|
clEnumVal(printusedtypes , "Print Types Used by Module"),
|
2001-11-08 05:16:29 +08:00
|
|
|
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);
|
|
|
|
|
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-09-28 08:07:36 +08:00
|
|
|
} MethAnTable[] = {
|
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-11-27 03:18:11 +08:00
|
|
|
{ loops , PrintLoops },
|
|
|
|
{ indvars , PrintInductionVariables },
|
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
|
|
|
};
|
|
|
|
|
2001-09-28 08:07:36 +08:00
|
|
|
pair<enum Ans, void (*)(Module *)> ModAnTable[] = {
|
2001-11-09 13:27:34 +08:00
|
|
|
pair<enum Ans, void (*)(Module *)>(callgraph , PrintCallGraph),
|
|
|
|
pair<enum Ans, void (*)(Module *)>(printusedtypes , PrintUsedTypes),
|
2001-11-08 05:16:29 +08:00
|
|
|
pair<enum Ans, void (*)(Module *)>(unsafepointertypes, PrintUnsafePtrTypes),
|
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");
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2001-09-28 08:07:36 +08: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 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];
|
2001-09-28 08:07:36 +08:00
|
|
|
if (AnalysisPass == PassDone) continue; // Don't rerun module analyses
|
2001-07-23 10:35:57 +08:00
|
|
|
|
|
|
|
// Loop over all of the analyses to be run...
|
2001-07-03 23:30:38 +08:00
|
|
|
unsigned j;
|
2001-09-28 08:07:36 +08:00
|
|
|
for (j = 0; j < sizeof(MethAnTable)/sizeof(MethAnTable[0]); ++j) {
|
|
|
|
if (AnalysisPass == MethAnTable[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-09-28 08:07:36 +08:00
|
|
|
MethAnTable[j].AnPtr(M);
|
2001-07-03 23:30:38 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2001-09-28 08:07:36 +08:00
|
|
|
if (j == sizeof(MethAnTable)/sizeof(MethAnTable[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;
|
|
|
|
}
|