cf93ea9d8ed4d51414d74602bed3c0466226a9b0
[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/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"
21
22 #include "llvm/Analysis/Dominators.h"
23 #include "llvm/Analysis/IntervalPartition.h"
24 #include "llvm/Analysis/Expressions.h"
25
26 static void PrintMethod(Method *M) {
27   cout << M;
28 }
29
30 static void PrintIntervalPartition(Method *M) {
31   cout << cfg::IntervalPartition(M);
32 }
33
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();
37   for (; I != E; ++I) {
38     cout << *I;
39
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
43
44     cout << "\t\tExpr =";
45     switch (R.ExprTy) {
46     case analysis::ExprType::ScaledLinear:
47       WriteAsOperand(cout, (Value*)R.Scale) << " *";
48       // fall through
49     case analysis::ExprType::Linear:
50       WriteAsOperand(cout, R.Var);
51       if (R.Offset == 0) break;
52       else cout << " +";
53       // fall through
54     case analysis::ExprType::Constant:
55       if (R.Offset) WriteAsOperand(cout, (Value*)R.Offset); else cout << " 0";
56       break;
57     }
58     cout << endl << endl;
59   }
60 }
61
62
63 static void PrintDominatorSets(Method *M) {
64   cout << cfg::DominatorSet(M);
65 }
66 static void PrintImmediateDominators(Method *M) {
67   cout << cfg::ImmediateDominators(M);
68 }
69 static void PrintDominatorTree(Method *M) {
70   cout << cfg::DominatorTree(M);
71 }
72 static void PrintDominanceFrontier(Method *M) {
73   cout << cfg::DominanceFrontier(M);
74 }
75
76 static void PrintPostDominatorSets(Method *M) {
77   cout << cfg::DominatorSet(M, true);
78 }
79 static void PrintImmediatePostDoms(Method *M) {
80   cout << cfg::ImmediateDominators(cfg::DominatorSet(M, true));
81 }
82 static void PrintPostDomTree(Method *M) {
83   cout << cfg::DominatorTree(cfg::DominatorSet(M, true));
84 }
85 static void PrintPostDomFrontier(Method *M) {
86   cout << cfg::DominanceFrontier(cfg::DominatorSet(M, true));
87 }
88
89 struct {
90   const string ArgName, Name;
91   void (*AnPtr)(Method *M);
92 } AnTable[] = {
93   { "-print"          , "Print each Method"       , PrintMethod },
94   { "-intervals"      , "Interval Partition"      , PrintIntervalPartition },
95   { "-exprclassify"   , "Classify Expressions"    , PrintClassifiedExprs },
96
97   { "-domset"         , "Dominator Sets"          , PrintDominatorSets },
98   { "-idom"           , "Immediate Dominators"    , PrintImmediateDominators },
99   { "-domtree"        , "Dominator Tree"          , PrintDominatorTree },
100   { "-domfrontier"    , "Dominance Frontier"      , PrintDominanceFrontier },
101
102   { "-postdomset"     , "Postdominator Sets"      , PrintPostDominatorSets },
103   { "-postidom"       , "Immediate Postdominators", PrintImmediatePostDoms },
104   { "-postdomtree"    , "Post Dominator Tree"     , PrintPostDomTree },
105   { "-postdomfrontier", "Postdominance Frontier"  , PrintPostDomFrontier },
106 };
107
108 int main(int argc, char **argv) {
109   ToolCommandLine Options(argc, argv, false);
110   bool Quiet = false;
111
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;
120       }
121       return 1;
122     } else if (string(argv[i]) == string("-q") ||
123                string(argv[i]) == string("--quiet")) {
124       Quiet = true; argv[i] = 0;
125     }
126   }
127   
128   Module *C = ParseBytecodeFile(Options.getInputFilename());
129   if (!C && !(C = ParseAssemblyFile(Options))) {
130     cerr << "Input file didn't read correctly.\n";
131     return 1;
132   }
133
134   // Loop over all of the methods in the module...
135   for (Module::iterator I = C->begin(), E = C->end(); I != E; ++I) {
136     Method *M = *I;
137     if (M->isExternal()) continue;
138
139     // Loop over all of the analyses to be run...
140     for (int i = 1; i < argc; i++) {
141       if (argv[i] == 0) continue;
142       unsigned j;
143       for (j = 0; j < sizeof(AnTable)/sizeof(AnTable[0]); j++) {
144         if (string(argv[i]) == AnTable[j].ArgName) {
145           if (!Quiet)
146             cerr << "Running: " << AnTable[j].Name << " analysis on '"
147                  << ((Value*)M)->getName() << "'!\n";
148           AnTable[j].AnPtr(M);
149           break;
150         }
151       }
152       
153       if (j == sizeof(AnTable)/sizeof(AnTable[0])) 
154         cerr << "'" << argv[i] << "' argument unrecognized: ignored\n";
155     }
156   }
157
158   delete C;
159   return 0;
160 }