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 //===------------------------------------------------------------------------===
13 #include "llvm/Instruction.h"
14 #include "llvm/Module.h"
15 #include "llvm/Method.h"
16 #include "llvm/iPHINode.h"
17 #include "llvm/Bytecode/Reader.h"
18 #include "llvm/Assembly/Parser.h"
19 #include "llvm/Analysis/Writer.h"
20 #include "llvm/Analysis/InstForest.h"
21 #include "llvm/Analysis/Dominators.h"
22 #include "llvm/Analysis/IntervalPartition.h"
23 #include "llvm/Analysis/Expressions.h"
24 #include "llvm/Analysis/InductionVariable.h"
25 #include "llvm/Analysis/CallGraph.h"
26 #include "llvm/Analysis/LoopInfo.h"
27 #include "llvm/Analysis/FindUnsafePointerTypes.h"
28 #include "llvm/Analysis/FindUsedTypes.h"
29 #include "Support/CommandLine.h"
36 static void PrintMethod(Method *M) {
40 static void PrintIntervalPartition(Method *M) {
41 cout << cfg::IntervalPartition(M);
44 static void PrintClassifiedExprs(Method *M) {
45 cout << "Classified expressions for: " << M->getName() << "\n";
46 Method::inst_iterator I = M->inst_begin(), E = M->inst_end();
50 if ((*I)->getType() == Type::VoidTy) continue;
51 analysis::ExprType R = analysis::ClassifyExpression(*I);
52 if (R.Var == *I) continue; // Doesn't tell us anything
56 case analysis::ExprType::ScaledLinear:
57 WriteAsOperand(cout << "(", (Value*)R.Scale) << " ) *";
59 case analysis::ExprType::Linear:
60 WriteAsOperand(cout << "(", R.Var) << " )";
61 if (R.Offset == 0) break;
64 case analysis::ExprType::Constant:
65 if (R.Offset) WriteAsOperand(cout, (Value*)R.Offset); else cout << " 0";
72 static void PrintInductionVariables(Method *M) {
74 for (Method::inst_iterator I = M->inst_begin(), E = M->inst_end();
76 if (PHINode *PN = dyn_cast<PHINode>(*I)) {
77 InductionVariable IV(PN, &LI);
78 if (IV.InductionType != InductionVariable::Unknown)
84 static void PrintInstForest(Method *M) {
85 cout << analysis::InstForest<char>(M);
87 static void PrintLoops(Method *M) {
88 cout << cfg::LoopInfo(M);
90 static void PrintCallGraph(Module *M) {
91 cout << cfg::CallGraph(M);
94 static void PrintUnsafePtrTypes(Module *M) {
95 FindUnsafePointerTypes FUPT;
97 FUPT.printResults(M, cout);
100 static void PrintUsedTypes(Module *M) {
103 FUT.printTypes(cout, M);
106 static void PrintDominatorSets(Method *M) {
107 cout << cfg::DominatorSet(M);
109 static void PrintImmediateDominators(Method *M) {
110 cout << cfg::ImmediateDominators(M);
112 static void PrintDominatorTree(Method *M) {
113 cout << cfg::DominatorTree(M);
115 static void PrintDominanceFrontier(Method *M) {
116 cout << cfg::DominanceFrontier(M);
119 static void PrintPostDominatorSets(Method *M) {
120 cout << cfg::DominatorSet(M, true);
122 static void PrintImmediatePostDoms(Method *M) {
123 cout << cfg::ImmediateDominators(cfg::DominatorSet(M, true));
125 static void PrintPostDomTree(Method *M) {
126 cout << cfg::DominatorTree(cfg::DominatorSet(M, true));
128 static void PrintPostDomFrontier(Method *M) {
129 cout << cfg::DominanceFrontier(cfg::DominatorSet(M, true));
134 PassDone, // Unique Marker
135 print, intervals, exprclassify, instforest, loops, indvars, callgraph,
136 printusedtypes, unsafepointertypes,
138 domset, idom, domtree, domfrontier,
139 postdomset, postidom, postdomtree, postdomfrontier,
142 cl::String InputFilename ("", "Load <arg> file to analyze", cl::NoFlags, "-");
143 cl::Flag Quiet ("q", "Don't print analysis pass names");
144 cl::Alias QuietA ("quiet", "Alias for -q", cl::NoFlags, Quiet);
145 cl::EnumList<enum Ans> AnalysesList(cl::NoFlags,
146 clEnumVal(print , "Print each Method"),
147 clEnumVal(intervals , "Print Interval Partitions"),
148 clEnumVal(exprclassify , "Classify Expressions"),
149 clEnumVal(instforest , "Print Instruction Forest"),
150 clEnumVal(loops , "Print Loops"),
151 clEnumVal(indvars , "Print Induction Variables"),
152 clEnumVal(callgraph , "Print Call Graph"),
153 clEnumVal(printusedtypes , "Print Types Used by Module"),
154 clEnumVal(unsafepointertypes, "Print Unsafe Pointer Types"),
156 clEnumVal(domset , "Print Dominator Sets"),
157 clEnumVal(idom , "Print Immediate Dominators"),
158 clEnumVal(domtree , "Print Dominator Tree"),
159 clEnumVal(domfrontier , "Print Dominance Frontier"),
161 clEnumVal(postdomset , "Print Postdominator Sets"),
162 clEnumVal(postidom , "Print Immediate Postdominators"),
163 clEnumVal(postdomtree , "Print Post Dominator Tree"),
164 clEnumVal(postdomfrontier, "Print Postdominance Frontier"),
169 void (*AnPtr)(Method *M);
171 { print , PrintMethod },
172 { intervals , PrintIntervalPartition },
173 { exprclassify , PrintClassifiedExprs },
174 { instforest , PrintInstForest },
175 { loops , PrintLoops },
176 { indvars , PrintInductionVariables },
178 { domset , PrintDominatorSets },
179 { idom , PrintImmediateDominators },
180 { domtree , PrintDominatorTree },
181 { domfrontier , PrintDominanceFrontier },
183 { postdomset , PrintPostDominatorSets },
184 { postidom , PrintImmediatePostDoms },
185 { postdomtree , PrintPostDomTree },
186 { postdomfrontier, PrintPostDomFrontier },
189 pair<enum Ans, void (*)(Module *)> ModAnTable[] = {
190 pair<enum Ans, void (*)(Module *)>(callgraph , PrintCallGraph),
191 pair<enum Ans, void (*)(Module *)>(printusedtypes , PrintUsedTypes),
192 pair<enum Ans, void (*)(Module *)>(unsafepointertypes, PrintUnsafePtrTypes),
197 int main(int argc, char **argv) {
198 cl::ParseCommandLineOptions(argc, argv, " llvm analysis printer tool\n");
200 Module *C = ParseBytecodeFile(InputFilename);
201 if (!C && !(C = ParseAssemblyFile(InputFilename))) {
202 cerr << "Input file didn't read correctly.\n";
206 // Loop over all of the analyses looking for module level analyses to run...
207 for (unsigned i = 0; i < AnalysesList.size(); ++i) {
208 enum Ans AnalysisPass = AnalysesList[i];
210 for (unsigned j = 0; j < sizeof(ModAnTable)/sizeof(ModAnTable[0]); ++j) {
211 if (ModAnTable[j].first == AnalysisPass) {
213 cerr << "Running: " << AnalysesList.getArgDescription(AnalysisPass)
214 << " analysis on module!\n";
215 ModAnTable[j].second(C);
216 AnalysesList[i] = PassDone; // Mark pass as complete so that we don't
217 break; // get an error later
222 // Loop over all of the methods in the module...
223 for (Module::iterator I = C->begin(), E = C->end(); I != E; ++I) {
225 if (M->isExternal()) continue;
227 for (unsigned i = 0; i < AnalysesList.size(); ++i) {
228 enum Ans AnalysisPass = AnalysesList[i];
229 if (AnalysisPass == PassDone) continue; // Don't rerun module analyses
231 // Loop over all of the analyses to be run...
233 for (j = 0; j < sizeof(MethAnTable)/sizeof(MethAnTable[0]); ++j) {
234 if (AnalysisPass == MethAnTable[j].AnID) {
236 cerr << "Running: " << AnalysesList.getArgDescription(AnalysisPass)
237 << " analysis on '" << ((Value*)M)->getName() << "'!\n";
238 MethAnTable[j].AnPtr(M);
242 if (j == sizeof(MethAnTable)/sizeof(MethAnTable[0]))
243 cerr << "Analysis tables inconsistent!\n";