Add better support for post dominator information.
[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 <iostream>
14 #include "llvm/Module.h"
15 #include "llvm/Bytecode/Reader.h"
16 #include "llvm/Assembly/Parser.h"
17 #include "llvm/Tools/CommandLine.h"
18 #include "llvm/Analysis/Writer.h"
19
20 #include "llvm/Analysis/Dominators.h"
21 #include "llvm/Analysis/IntervalPartition.h"
22
23 static void PrintMethod(Method *M) {
24   cout << M;
25 }
26
27 static void PrintIntervalPartition(Method *M) {
28   cout << cfg::IntervalPartition(M);
29 }
30
31
32 static void PrintDominatorSets(Method *M) {
33   cout << cfg::DominatorSet(M);
34 }
35 static void PrintImmediateDominators(Method *M) {
36   cout << cfg::ImmediateDominators(M);
37 }
38 static void PrintDominatorTree(Method *M) {
39   cout << cfg::DominatorTree(M);
40 }
41 static void PrintDominanceFrontier(Method *M) {
42   cout << cfg::DominanceFrontier(M);
43 }
44
45 static void PrintPostDominatorSets(Method *M) {
46   cout << cfg::DominatorSet(M, true);
47 }
48 static void PrintImmediatePostDoms(Method *M) {
49   cout << cfg::ImmediateDominators(cfg::DominatorSet(M, true));
50 }
51 static void PrintPostDomTree(Method *M) {
52   cout << cfg::DominatorTree(cfg::DominatorSet(M, true));
53 }
54 static void PrintPostDomFrontier(Method *M) {
55   cout << cfg::DominanceFrontier(cfg::DominatorSet(M, true));
56 }
57
58 struct {
59   const string ArgName, Name;
60   void (*AnPtr)(Method *M);
61 } AnTable[] = {
62   { "-print"          , "Print each Method"       , PrintMethod },
63   { "-intervals"      , "Interval Partition"      , PrintIntervalPartition },
64
65   { "-domset"         , "Dominator Sets"          , PrintDominatorSets },
66   { "-idom"           , "Immediate Dominators"    , PrintImmediateDominators },
67   { "-domtree"        , "Dominator Tree"          , PrintDominatorTree },
68   { "-domfrontier"    , "Dominance Frontier"      , PrintDominanceFrontier },
69
70   { "-postdomset"     , "Postdominator Sets"      , PrintPostDominatorSets },
71   { "-postidom"       , "Immediate Postdominators", PrintImmediatePostDoms },
72   { "-postdomtree"    , "Post Dominator Tree"     , PrintPostDomTree },
73   { "-postdomfrontier", "Postdominance Frontier"  , PrintPostDomFrontier },
74 };
75
76 int main(int argc, char **argv) {
77   ToolCommandLine Options(argc, argv, false);
78   bool Quiet = false;
79
80   for (int i = 1; i < argc; i++) {
81     if (string(argv[i]) == string("--help")) {
82       cerr << argv[0] << " usage:\n"
83            << "  " << argv[0] << " --help\t - Print this usage information\n"
84            << "\t  --quiet\t - Do not print analysis name before output\n";
85       for (unsigned j = 0; j < sizeof(AnTable)/sizeof(AnTable[0]); ++j) {
86         cerr << "\t   " << AnTable[j].ArgName << "\t - Print " 
87              << AnTable[j].Name << endl;
88       }
89       return 1;
90     } else if (string(argv[i]) == string("-q") ||
91                string(argv[i]) == string("--quiet")) {
92       Quiet = true; argv[i] = 0;
93     }
94   }
95   
96   Module *C = ParseBytecodeFile(Options.getInputFilename());
97   if (!C && !(C = ParseAssemblyFile(Options))) {
98     cerr << "Input file didn't read correctly.\n";
99     return 1;
100   }
101
102   // Loop over all of the methods in the module...
103   for (Module::iterator I = C->begin(), E = C->end(); I != E; ++I) {
104     Method *M = *I;
105
106     // Loop over all of the optimizations to be run...
107     for (int i = 1; i < argc; i++) {
108       if (argv[i] == 0) continue;
109       unsigned j;
110       for (j = 0; j < sizeof(AnTable)/sizeof(AnTable[0]); j++) {
111         if (string(argv[i]) == AnTable[j].ArgName) {
112           if (!Quiet)
113             cerr << "Running: " << AnTable[j].Name << " analysis on '"
114                  << ((Value*)M)->getName() << "'!\n";
115           AnTable[j].AnPtr(M);
116           break;
117         }
118       }
119       
120       if (j == sizeof(AnTable)/sizeof(AnTable[0])) 
121         cerr << "'" << argv[i] << "' argument unrecognized: ignored\n";
122     }
123   }
124
125   delete C;
126   return 0;
127 }