Chris Lattner | f3dc2c9 | 2001-07-03 15:30:38 +0000 | [diff] [blame^] | 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 PrintIntervalPartition(const Method *M) { |
| 24 | cout << cfg::IntervalPartition((Method*)M); |
| 25 | } |
| 26 | |
| 27 | static void PrintDominatorSets(const Method *M) { |
| 28 | cout << cfg::DominatorSet(M); |
| 29 | } |
| 30 | static void PrintImmediateDominators(const Method *M) { |
| 31 | cout << cfg::ImmediateDominators(M); |
| 32 | } |
| 33 | static void PrintDominatorTree(const Method *M) { |
| 34 | cout << cfg::DominatorTree(M); |
| 35 | } |
| 36 | static void PrintDominanceFrontier(const Method *M) { |
| 37 | cout << cfg::DominanceFrontier(M); |
| 38 | } |
| 39 | |
| 40 | struct { |
| 41 | const string ArgName, Name; |
| 42 | void (*AnPtr)(const Method *M); |
| 43 | } AnTable[] = { |
| 44 | { "-intervals" , "Interval Partition" , PrintIntervalPartition }, |
| 45 | { "-domset" , "Dominator Sets" , PrintDominatorSets }, |
| 46 | { "-immdom" , "Immediate Dominators", PrintImmediateDominators }, |
| 47 | { "-domtree" , "Dominator Tree" , PrintDominatorTree }, |
| 48 | { "-domfrontier" , "Dominance Frontier" , PrintDominanceFrontier }, |
| 49 | }; |
| 50 | |
| 51 | int main(int argc, char **argv) { |
| 52 | ToolCommandLine Options(argc, argv, false); |
| 53 | bool Quiet = false; |
| 54 | |
| 55 | for (int i = 1; i < argc; i++) { |
| 56 | if (string(argv[i]) == string("--help")) { |
| 57 | cerr << argv[0] << " usage:\n" |
| 58 | << " " << argv[0] << " --help\t - Print this usage information\n" |
| 59 | << "\t --quiet\t - Do not print optimization name before output\n"; |
| 60 | for (unsigned j = 0; j < sizeof(AnTable)/sizeof(AnTable[0]); ++j) { |
| 61 | cerr << "\t " << AnTable[j].ArgName << "\t - Print " |
| 62 | << AnTable[j].Name << endl; |
| 63 | } |
| 64 | return 1; |
| 65 | } else if (string(argv[i]) == string("-q") || |
| 66 | string(argv[i]) == string("--quiet")) { |
| 67 | Quiet = true; argv[i] = 0; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | const Module *C = ParseBytecodeFile(Options.getInputFilename()); |
| 72 | if (C == 0) { |
| 73 | C = ParseAssemblyFile(Options); |
| 74 | if (C == 0) { |
| 75 | cerr << "Input file didn't read correctly.\n"; |
| 76 | return 1; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | // Loop over all of the methods in the module... |
| 81 | for (Module::const_iterator I = C->begin(), E = C->end(); I != E; ++I) { |
| 82 | const Method *M = *I; |
| 83 | |
| 84 | // Loop over all of the optimizations to be run... |
| 85 | for (int i = 1; i < argc; i++) { |
| 86 | if (argv[i] == 0) continue; |
| 87 | unsigned j; |
| 88 | for (j = 0; j < sizeof(AnTable)/sizeof(AnTable[0]); j++) { |
| 89 | if (string(argv[i]) == AnTable[j].ArgName) { |
| 90 | if (!Quiet) |
| 91 | cerr << "Running: " << AnTable[j].Name << " analysis!\n"; |
| 92 | AnTable[j].AnPtr(M); |
| 93 | break; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | if (j == sizeof(AnTable)/sizeof(AnTable[0])) |
| 98 | cerr << "'" << argv[i] << "' argument unrecognized: ignored\n"; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | delete C; |
| 103 | return 0; |
| 104 | } |