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/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"
20 #include "llvm/Analysis/Dominators.h"
21 #include "llvm/Analysis/IntervalPartition.h"
23 static void PrintMethod(Method *M) {
27 static void PrintIntervalPartition(Method *M) {
28 cout << cfg::IntervalPartition(M);
32 static void PrintDominatorSets(Method *M) {
33 cout << cfg::DominatorSet(M);
35 static void PrintImmediateDominators(Method *M) {
36 cout << cfg::ImmediateDominators(M);
38 static void PrintDominatorTree(Method *M) {
39 cout << cfg::DominatorTree(M);
41 static void PrintDominanceFrontier(Method *M) {
42 cout << cfg::DominanceFrontier(M);
45 static void PrintPostDominatorSets(Method *M) {
46 cout << cfg::DominatorSet(M, true);
48 static void PrintImmediatePostDoms(Method *M) {
49 cout << cfg::ImmediateDominators(cfg::DominatorSet(M, true));
51 static void PrintPostDomTree(Method *M) {
52 cout << cfg::DominatorTree(cfg::DominatorSet(M, true));
54 static void PrintPostDomFrontier(Method *M) {
55 cout << cfg::DominanceFrontier(cfg::DominatorSet(M, true));
59 const string ArgName, Name;
60 void (*AnPtr)(Method *M);
62 { "-print" , "Print each Method" , PrintMethod },
63 { "-intervals" , "Interval Partition" , PrintIntervalPartition },
65 { "-domset" , "Dominator Sets" , PrintDominatorSets },
66 { "-idom" , "Immediate Dominators" , PrintImmediateDominators },
67 { "-domtree" , "Dominator Tree" , PrintDominatorTree },
68 { "-domfrontier" , "Dominance Frontier" , PrintDominanceFrontier },
70 { "-postdomset" , "Postdominator Sets" , PrintPostDominatorSets },
71 { "-postidom" , "Immediate Postdominators", PrintImmediatePostDoms },
72 { "-postdomtree" , "Post Dominator Tree" , PrintPostDomTree },
73 { "-postdomfrontier", "Postdominance Frontier" , PrintPostDomFrontier },
76 int main(int argc, char **argv) {
77 ToolCommandLine Options(argc, argv, false);
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;
90 } else if (string(argv[i]) == string("-q") ||
91 string(argv[i]) == string("--quiet")) {
92 Quiet = true; argv[i] = 0;
96 Module *C = ParseBytecodeFile(Options.getInputFilename());
97 if (!C && !(C = ParseAssemblyFile(Options))) {
98 cerr << "Input file didn't read correctly.\n";
102 // Loop over all of the methods in the module...
103 for (Module::iterator I = C->begin(), E = C->end(); I != E; ++I) {
106 // Loop over all of the optimizations to be run...
107 for (int i = 1; i < argc; i++) {
108 if (argv[i] == 0) continue;
110 for (j = 0; j < sizeof(AnTable)/sizeof(AnTable[0]); j++) {
111 if (string(argv[i]) == AnTable[j].ArgName) {
113 cerr << "Running: " << AnTable[j].Name << " analysis on '"
114 << ((Value*)M)->getName() << "'!\n";
120 if (j == sizeof(AnTable)/sizeof(AnTable[0]))
121 cerr << "'" << argv[i] << "' argument unrecognized: ignored\n";