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. |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 19 | // |
| 20 | //===------------------------------------------------------------------------=== |
| 21 | |
| 22 | #include <iostream.h> |
| 23 | #include <fstream.h> |
| 24 | #include "llvm/Module.h" |
| 25 | #include "llvm/Bytecode/Reader.h" |
| 26 | #include "llvm/Bytecode/Writer.h" |
| 27 | #include "llvm/Tools/CommandLine.h" |
Chris Lattner | 95781b6 | 2001-06-30 06:38:31 +0000 | [diff] [blame] | 28 | #include "llvm/Optimizations/AllOpts.h" |
| 29 | |
| 30 | using namespace opt; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 31 | |
| 32 | struct { |
| 33 | const string ArgName, Name; |
| 34 | bool (*OptPtr)(Module *C); |
| 35 | } OptTable[] = { |
Chris Lattner | 241b064 | 2001-06-27 23:37:58 +0000 | [diff] [blame] | 36 | { "-dce" , "Dead Code Elimination", DoDeadCodeElimination }, |
| 37 | { "-constprop" , "Constant Propogation", DoConstantPropogation }, |
| 38 | { "-inline" , "Method Inlining", DoMethodInlining }, |
| 39 | { "-strip" , "Strip Symbols", DoSymbolStripping }, |
| 40 | { "-mstrip" , "Strip Module Symbols", DoFullSymbolStripping }, |
| 41 | { "-indvars" , "Simplify Induction Vars",DoInductionVariableCannonicalize }, |
Chris Lattner | 95781b6 | 2001-06-30 06:38:31 +0000 | [diff] [blame] | 42 | { "-sccp" , "Sparse Conditional Constant Prop", DoSCCP }, |
Chris Lattner | 241b064 | 2001-06-27 23:37:58 +0000 | [diff] [blame] | 43 | { "-cpm" , "Constant Pool Merging", DoConstantPoolMerging }, |
Chris Lattner | 95781b6 | 2001-06-30 06:38:31 +0000 | [diff] [blame] | 44 | { "-adce" , "Agressive DCE", DoADCE }, |
Chris Lattner | 955f899 | 2001-07-20 19:16:47 +0000 | [diff] [blame] | 45 | { "-raise" , "Raise to Higher Level", DoRaiseRepresentation }, |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | int main(int argc, char **argv) { |
| 49 | ToolCommandLine Opts(argc, argv, false); |
| 50 | bool Quiet = false; |
| 51 | |
| 52 | for (int i = 1; i < argc; i++) { |
| 53 | if (string(argv[i]) == string("--help")) { |
| 54 | cerr << argv[0] << " usage:\n" |
| 55 | << " " << argv[0] << " --help - Print this usage information\n"; |
Chris Lattner | 526f97e | 2001-06-13 19:55:50 +0000 | [diff] [blame] | 56 | for (unsigned j = 0; j < sizeof(OptTable)/sizeof(OptTable[0]); ++j) { |
| 57 | cerr << "\t" << OptTable[j].ArgName << "\t - Enable " |
| 58 | << OptTable[j].Name << endl; |
| 59 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 60 | return 1; |
| 61 | } else if (string(argv[i]) == string("-q")) { |
| 62 | Quiet = true; argv[i] = 0; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | ostream *Out = &cout; // Default to printing to stdout... |
| 67 | |
| 68 | Module *C = ParseBytecodeFile(Opts.getInputFilename()); |
| 69 | if (C == 0) { |
| 70 | cerr << "bytecode didn't read correctly.\n"; |
| 71 | return 1; |
| 72 | } |
| 73 | |
| 74 | |
| 75 | for (int i = 1; i < argc; i++) { |
| 76 | if (argv[i] == 0) continue; |
| 77 | unsigned j; |
| 78 | for (j = 0; j < sizeof(OptTable)/sizeof(OptTable[0]); j++) { |
| 79 | if (string(argv[i]) == OptTable[j].ArgName) { |
| 80 | if (OptTable[j].OptPtr(C) && !Quiet) |
| 81 | cerr << OptTable[j].Name << " pass made modifications!\n"; |
| 82 | break; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | if (j == sizeof(OptTable)/sizeof(OptTable[0])) |
| 87 | cerr << "'" << argv[i] << "' argument unrecognized: ignored\n"; |
| 88 | } |
| 89 | |
| 90 | if (Opts.getOutputFilename() != "-") { |
| 91 | Out = new ofstream(Opts.getOutputFilename().c_str(), |
| 92 | (Opts.getForce() ? 0 : ios::noreplace)|ios::out); |
| 93 | if (!Out->good()) { |
| 94 | cerr << "Error opening " << Opts.getOutputFilename() |
| 95 | << "!\n"; |
| 96 | delete C; |
| 97 | return 1; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | // Okay, we're done now... write out result... |
| 102 | WriteBytecodeToFile(C, *Out); |
| 103 | delete C; |
| 104 | |
| 105 | if (Out != &cout) delete Out; |
| 106 | return 0; |
| 107 | } |