Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1 | //===------------------------------------------------------------------------=== |
| 2 | // LLVM 'OPT' UTILITY |
| 3 | // |
| 4 | // This utility may be invoked in the following manner: |
| 5 | // opt --help - Output information about command line switches |
| 6 | // opt [options] -dce - Run a dead code elimination pass on input |
| 7 | // bytecodes |
| 8 | // opt [options] -constprop - Run a constant propogation pass on input |
| 9 | // bytecodes |
| 10 | // opt [options] -inline - Run a method inlining pass on input bytecodes |
| 11 | // opt [options] -strip - Strip symbol tables out of methods |
| 12 | // opt [options] -mstrip - Strip module & method symbol tables |
| 13 | // |
| 14 | // Optimizations may be specified an arbitrary number of times on the command |
| 15 | // line, they are run in the order specified. |
| 16 | // |
| 17 | // TODO: Add a -all option to keep applying all optimizations until the program |
| 18 | // stops permuting. |
| 19 | // TODO: Add a -h command line arg that prints all available optimizations |
| 20 | // TODO: Add a -q command line arg that quiets "XXX pass made modifications" |
| 21 | // |
| 22 | //===------------------------------------------------------------------------=== |
| 23 | |
| 24 | #include <iostream.h> |
| 25 | #include <fstream.h> |
| 26 | #include "llvm/Module.h" |
| 27 | #include "llvm/Bytecode/Reader.h" |
| 28 | #include "llvm/Bytecode/Writer.h" |
| 29 | #include "llvm/Tools/CommandLine.h" |
| 30 | #include "llvm/Opt/AllOpts.h" |
| 31 | |
Chris Lattner | 84608e4 | 2001-06-08 00:35:25 +0000 | [diff] [blame^] | 32 | #if 1 // Testcase, TODO: REMOVE |
| 33 | #include "llvm/CFG.h" |
| 34 | #include "llvm/Assembly/Writer.h" |
| 35 | #include "llvm/Method.h" |
| 36 | static bool DoPrintM(Method *M) { |
| 37 | df_iterator I = df_begin(M->getBasicBlocks().front(), false); |
| 38 | df_iterator E = df_end(M->getBasicBlocks().front()); |
| 39 | unsigned i = 0; |
| 40 | for (; I != E; ++I, ++i) { |
| 41 | cerr << "Basic Block Visited #" << i << *I; |
| 42 | } |
| 43 | return false; |
| 44 | } |
| 45 | |
| 46 | static bool DoPrint(Module *C) { |
| 47 | return ApplyOptToAllMethods(C, DoPrintM); |
| 48 | } |
| 49 | #endif |
| 50 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 51 | struct { |
| 52 | const string ArgName, Name; |
| 53 | bool (*OptPtr)(Module *C); |
| 54 | } OptTable[] = { |
| 55 | { "-dce", "Dead Code Elimination", DoDeadCodeElimination }, |
| 56 | { "-constprop","Constant Propogation", DoConstantPropogation }, |
| 57 | { "-inline" ,"Method Inlining", DoMethodInlining }, |
| 58 | { "-strip" ,"Strip Symbols", DoSymbolStripping }, |
| 59 | { "-mstrip" ,"Strip Module Symbols", DoFullSymbolStripping }, |
Chris Lattner | 84608e4 | 2001-06-08 00:35:25 +0000 | [diff] [blame^] | 60 | { "-print" ,"Test printing stuff", DoPrint }, |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | int main(int argc, char **argv) { |
| 64 | ToolCommandLine Opts(argc, argv, false); |
| 65 | bool Quiet = false; |
| 66 | |
| 67 | for (int i = 1; i < argc; i++) { |
| 68 | if (string(argv[i]) == string("--help")) { |
| 69 | cerr << argv[0] << " usage:\n" |
| 70 | << " " << argv[0] << " --help - Print this usage information\n"; |
| 71 | return 1; |
| 72 | } else if (string(argv[i]) == string("-q")) { |
| 73 | Quiet = true; argv[i] = 0; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | ostream *Out = &cout; // Default to printing to stdout... |
| 78 | |
| 79 | Module *C = ParseBytecodeFile(Opts.getInputFilename()); |
| 80 | if (C == 0) { |
| 81 | cerr << "bytecode didn't read correctly.\n"; |
| 82 | return 1; |
| 83 | } |
| 84 | |
| 85 | |
| 86 | for (int i = 1; i < argc; i++) { |
| 87 | if (argv[i] == 0) continue; |
| 88 | unsigned j; |
| 89 | for (j = 0; j < sizeof(OptTable)/sizeof(OptTable[0]); j++) { |
| 90 | if (string(argv[i]) == OptTable[j].ArgName) { |
| 91 | if (OptTable[j].OptPtr(C) && !Quiet) |
| 92 | cerr << OptTable[j].Name << " pass made modifications!\n"; |
| 93 | break; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | if (j == sizeof(OptTable)/sizeof(OptTable[0])) |
| 98 | cerr << "'" << argv[i] << "' argument unrecognized: ignored\n"; |
| 99 | } |
| 100 | |
| 101 | if (Opts.getOutputFilename() != "-") { |
| 102 | Out = new ofstream(Opts.getOutputFilename().c_str(), |
| 103 | (Opts.getForce() ? 0 : ios::noreplace)|ios::out); |
| 104 | if (!Out->good()) { |
| 105 | cerr << "Error opening " << Opts.getOutputFilename() |
| 106 | << "!\n"; |
| 107 | delete C; |
| 108 | return 1; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | // Okay, we're done now... write out result... |
| 113 | WriteBytecodeToFile(C, *Out); |
| 114 | delete C; |
| 115 | |
| 116 | if (Out != &cout) delete Out; |
| 117 | return 0; |
| 118 | } |