Add support to print a call graph, and also add support for module level interprocedu...
[oota-llvm.git] / tools / analyze / analyze.cpp
1 //===------------------------------------------------------------------------===
2 // LLVM 'Analyze' UTILITY 
3 //
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.
7 //
8 //  analyze --help           - Output information about command line switches
9 //  analyze --quiet          - Do not print analysis name before output
10 //
11 //===------------------------------------------------------------------------===
12
13 #include "llvm/Instruction.h"
14 #include "llvm/Module.h"
15 #include "llvm/Method.h"
16 #include "llvm/Bytecode/Reader.h"
17 #include "llvm/Assembly/Parser.h"
18 #include "llvm/Support/CommandLine.h"
19 #include "llvm/Analysis/Writer.h"
20
21 #include "llvm/Analysis/InstForest.h"
22 #include "llvm/Analysis/Dominators.h"
23 #include "llvm/Analysis/IntervalPartition.h"
24 #include "llvm/Analysis/Expressions.h"
25 #include "llvm/Analysis/CallGraph.h"
26 #include <algorithm>
27
28 static void PrintMethod(Method *M) {
29   cout << M;
30 }
31
32 static void PrintIntervalPartition(Method *M) {
33   cout << cfg::IntervalPartition(M);
34 }
35
36 static void PrintClassifiedExprs(Method *M) {
37   cout << "Classified expressions for: " << M->getName() << endl;
38   Method::inst_iterator I = M->inst_begin(), E = M->inst_end();
39   for (; I != E; ++I) {
40     cout << *I;
41
42     if ((*I)->getType() == Type::VoidTy) continue;
43     analysis::ExprType R = analysis::ClassifyExpression(*I);
44     if (R.Var == *I) continue;  // Doesn't tell us anything
45
46     cout << "\t\tExpr =";
47     switch (R.ExprTy) {
48     case analysis::ExprType::ScaledLinear:
49       WriteAsOperand(cout << "(", (Value*)R.Scale) << " ) *";
50       // fall through
51     case analysis::ExprType::Linear:
52       WriteAsOperand(cout << "(", R.Var) << " )";
53       if (R.Offset == 0) break;
54       else cout << " +";
55       // fall through
56     case analysis::ExprType::Constant:
57       if (R.Offset) WriteAsOperand(cout, (Value*)R.Offset); else cout << " 0";
58       break;
59     }
60     cout << endl << endl;
61   }
62 }
63
64 static void PrintInstForest(Method *M) {
65   cout << analysis::InstForest<char>(M);
66 }
67 static void PrintCallGraph(Module *M) {
68   cout << cfg::CallGraph(M);
69 }
70
71 static void PrintDominatorSets(Method *M) {
72   cout << cfg::DominatorSet(M);
73 }
74 static void PrintImmediateDominators(Method *M) {
75   cout << cfg::ImmediateDominators(M);
76 }
77 static void PrintDominatorTree(Method *M) {
78   cout << cfg::DominatorTree(M);
79 }
80 static void PrintDominanceFrontier(Method *M) {
81   cout << cfg::DominanceFrontier(M);
82 }
83
84 static void PrintPostDominatorSets(Method *M) {
85   cout << cfg::DominatorSet(M, true);
86 }
87 static void PrintImmediatePostDoms(Method *M) {
88   cout << cfg::ImmediateDominators(cfg::DominatorSet(M, true));
89 }
90 static void PrintPostDomTree(Method *M) {
91   cout << cfg::DominatorTree(cfg::DominatorSet(M, true));
92 }
93 static void PrintPostDomFrontier(Method *M) {
94   cout << cfg::DominanceFrontier(cfg::DominatorSet(M, true));
95 }
96
97
98 enum Ans {
99   PassDone,   // Unique Marker
100   print, intervals, exprclassify, instforest, callgraph,
101   domset, idom, domtree, domfrontier,
102   postdomset, postidom, postdomtree, postdomfrontier,
103 };
104
105 cl::String InputFilename ("", "Load <arg> file to analyze", cl::NoFlags, "-");
106 cl::Flag   Quiet         ("q", "Don't print analysis pass names");
107 cl::Alias  QuietA        ("quiet", "Alias for -q", cl::NoFlags, Quiet);
108 cl::EnumList<enum Ans> AnalysesList(cl::NoFlags,
109   clEnumVal(print          , "Print each Method"),
110   clEnumVal(intervals      , "Print Interval Partitions"),
111   clEnumVal(exprclassify   , "Classify Expressions"),
112   clEnumVal(instforest     , "Print Instruction Forest"),
113   clEnumVal(callgraph      , "Print Call Graph"),
114
115   clEnumVal(domset         , "Print Dominator Sets"),
116   clEnumVal(idom           , "Print Immediate Dominators"),
117   clEnumVal(domtree        , "Print Dominator Tree"),
118   clEnumVal(domfrontier    , "Print Dominance Frontier"),
119
120   clEnumVal(postdomset     , "Print Postdominator Sets"),
121   clEnumVal(postidom       , "Print Immediate Postdominators"),
122   clEnumVal(postdomtree    , "Print Post Dominator Tree"),
123   clEnumVal(postdomfrontier, "Print Postdominance Frontier"),
124 0);
125
126 struct {
127   enum Ans AnID;
128   void (*AnPtr)(Method *M);
129 } MethAnTable[] = {
130   { print          , PrintMethod              },
131   { intervals      , PrintIntervalPartition   },
132   { exprclassify   , PrintClassifiedExprs     },
133   { instforest     , PrintInstForest          },
134
135   { domset         , PrintDominatorSets       },
136   { idom           , PrintImmediateDominators },
137   { domtree        , PrintDominatorTree       },
138   { domfrontier    , PrintDominanceFrontier   },
139
140   { postdomset     , PrintPostDominatorSets   },
141   { postidom       , PrintImmediatePostDoms   },
142   { postdomtree    , PrintPostDomTree         },
143   { postdomfrontier, PrintPostDomFrontier     },
144 };
145
146 pair<enum Ans, void (*)(Module *)> ModAnTable[] = {
147   pair<enum Ans, void (*)(Module *)>(callgraph      , PrintCallGraph),
148 };
149
150
151
152 int main(int argc, char **argv) {
153   cl::ParseCommandLineOptions(argc, argv, " llvm analysis printer tool\n");
154
155   Module *C = ParseBytecodeFile(InputFilename);
156   if (!C && !(C = ParseAssemblyFile(InputFilename))) {
157     cerr << "Input file didn't read correctly.\n";
158     return 1;
159   }
160
161   // Loop over all of the analyses looking for module level analyses to run...
162   for (unsigned i = 0; i < AnalysesList.size(); ++i) {
163     enum Ans AnalysisPass = AnalysesList[i];
164
165     for (unsigned j = 0; j < sizeof(ModAnTable)/sizeof(ModAnTable[0]); ++j) {
166       if (ModAnTable[j].first == AnalysisPass) {
167         if (!Quiet)
168           cerr << "Running: " << AnalysesList.getArgDescription(AnalysisPass) 
169                << " analysis on module!\n";
170         ModAnTable[j].second(C);
171         AnalysesList[i] = PassDone;  // Mark pass as complete so that we don't
172         break;                       // get an error later
173       }
174     }
175   }  
176
177   // Loop over all of the methods in the module...
178   for (Module::iterator I = C->begin(), E = C->end(); I != E; ++I) {
179     Method *M = *I;
180     if (M->isExternal()) continue;
181
182     for (unsigned i = 0; i < AnalysesList.size(); ++i) {
183       enum Ans AnalysisPass = AnalysesList[i];
184       if (AnalysisPass == PassDone) continue;  // Don't rerun module analyses
185
186       // Loop over all of the analyses to be run...
187       unsigned j;
188       for (j = 0; j < sizeof(MethAnTable)/sizeof(MethAnTable[0]); ++j) {
189         if (AnalysisPass == MethAnTable[j].AnID) {
190           if (!Quiet)
191             cerr << "Running: " << AnalysesList.getArgDescription(AnalysisPass) 
192                  << " analysis on '" << ((Value*)M)->getName() << "'!\n";
193           MethAnTable[j].AnPtr(M);
194           break;
195         }
196       }
197       if (j == sizeof(MethAnTable)/sizeof(MethAnTable[0])) 
198         cerr << "Analysis tables inconsistent!\n";
199     }
200   }
201
202   delete C;
203   return 0;
204 }