1 //===------------------------------------------------------------------------===
2 // LLVM 'Analyze' UTILITY
4 // This utility is designed to print out the results of running various analysis
5 // passes on a program. This is useful for understanding a program, or for
6 // debugging an analysis pass.
8 // analyze --help - Output information about command line switches
9 // analyze --quiet - Do not print analysis name before output
11 //===------------------------------------------------------------------------===
14 #include "llvm/Instruction.h"
15 #include "llvm/Module.h"
16 #include "llvm/Method.h"
17 #include "llvm/Bytecode/Reader.h"
18 #include "llvm/Assembly/Parser.h"
19 #include "llvm/Tools/CommandLine.h"
20 #include "llvm/Analysis/Writer.h"
22 #include "llvm/Analysis/Dominators.h"
23 #include "llvm/Analysis/IntervalPartition.h"
24 #include "llvm/Analysis/Expressions.h"
26 static void PrintMethod(Method *M) {
30 static void PrintIntervalPartition(Method *M) {
31 cout << cfg::IntervalPartition(M);
34 static void PrintClassifiedExprs(Method *M) {
35 cout << "Classified expressions for: " << M->getName() << endl;
36 Method::inst_iterator I = M->inst_begin(), E = M->inst_end();
40 if ((*I)->getType() == Type::VoidTy) continue;
41 analysis::ExprType R = analysis::ClassifyExpression(*I);
42 if (R.Var == *I) continue; // Doesn't tell us anything
46 case analysis::ExprType::ScaledLinear:
47 WriteAsOperand(cout, (Value*)R.Scale) << " *";
49 case analysis::ExprType::Linear:
50 WriteAsOperand(cout, R.Var);
51 if (R.Offset == 0) break;
54 case analysis::ExprType::Constant:
55 if (R.Offset) WriteAsOperand(cout, (Value*)R.Offset); else cout << " 0";
63 static void PrintDominatorSets(Method *M) {
64 cout << cfg::DominatorSet(M);
66 static void PrintImmediateDominators(Method *M) {
67 cout << cfg::ImmediateDominators(M);
69 static void PrintDominatorTree(Method *M) {
70 cout << cfg::DominatorTree(M);
72 static void PrintDominanceFrontier(Method *M) {
73 cout << cfg::DominanceFrontier(M);
76 static void PrintPostDominatorSets(Method *M) {
77 cout << cfg::DominatorSet(M, true);
79 static void PrintImmediatePostDoms(Method *M) {
80 cout << cfg::ImmediateDominators(cfg::DominatorSet(M, true));
82 static void PrintPostDomTree(Method *M) {
83 cout << cfg::DominatorTree(cfg::DominatorSet(M, true));
85 static void PrintPostDomFrontier(Method *M) {
86 cout << cfg::DominanceFrontier(cfg::DominatorSet(M, true));
90 const string ArgName, Name;
91 void (*AnPtr)(Method *M);
93 { "-print" , "Print each Method" , PrintMethod },
94 { "-intervals" , "Interval Partition" , PrintIntervalPartition },
95 { "-exprclassify" , "Classify Expressions" , PrintClassifiedExprs },
97 { "-domset" , "Dominator Sets" , PrintDominatorSets },
98 { "-idom" , "Immediate Dominators" , PrintImmediateDominators },
99 { "-domtree" , "Dominator Tree" , PrintDominatorTree },
100 { "-domfrontier" , "Dominance Frontier" , PrintDominanceFrontier },
102 { "-postdomset" , "Postdominator Sets" , PrintPostDominatorSets },
103 { "-postidom" , "Immediate Postdominators", PrintImmediatePostDoms },
104 { "-postdomtree" , "Post Dominator Tree" , PrintPostDomTree },
105 { "-postdomfrontier", "Postdominance Frontier" , PrintPostDomFrontier },
108 int main(int argc, char **argv) {
109 ToolCommandLine Options(argc, argv, false);
112 for (int i = 1; i < argc; i++) {
113 if (string(argv[i]) == string("--help")) {
114 cerr << argv[0] << " usage:\n"
115 << " " << argv[0] << " --help\t - Print this usage information\n"
116 << "\t --quiet\t - Do not print analysis name before output\n";
117 for (unsigned j = 0; j < sizeof(AnTable)/sizeof(AnTable[0]); ++j) {
118 cerr << "\t " << AnTable[j].ArgName << "\t - Print "
119 << AnTable[j].Name << endl;
122 } else if (string(argv[i]) == string("-q") ||
123 string(argv[i]) == string("--quiet")) {
124 Quiet = true; argv[i] = 0;
128 Module *C = ParseBytecodeFile(Options.getInputFilename());
129 if (!C && !(C = ParseAssemblyFile(Options))) {
130 cerr << "Input file didn't read correctly.\n";
134 // Loop over all of the methods in the module...
135 for (Module::iterator I = C->begin(), E = C->end(); I != E; ++I) {
137 if (M->isExternal()) continue;
139 // Loop over all of the analyses to be run...
140 for (int i = 1; i < argc; i++) {
141 if (argv[i] == 0) continue;
143 for (j = 0; j < sizeof(AnTable)/sizeof(AnTable[0]); j++) {
144 if (string(argv[i]) == AnTable[j].ArgName) {
146 cerr << "Running: " << AnTable[j].Name << " analysis on '"
147 << ((Value*)M)->getName() << "'!\n";
153 if (j == sizeof(AnTable)/sizeof(AnTable[0]))
154 cerr << "'" << argv[i] << "' argument unrecognized: ignored\n";