Removed silly test code
[oota-llvm.git] / tools / opt / opt.cpp
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 //
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"
28 #include "llvm/Opt/AllOpts.h"
29
30 struct {
31   const string ArgName, Name;
32   bool (*OptPtr)(Module *C);
33 } OptTable[] = {
34   { "-dce",      "Dead Code Elimination", DoDeadCodeElimination },
35   { "-constprop","Constant Propogation",  DoConstantPropogation }, 
36   { "-inline"   ,"Method Inlining",       DoMethodInlining      },
37   { "-strip"    ,"Strip Symbols",         DoSymbolStripping     },
38   { "-mstrip"   ,"Strip Module Symbols",  DoFullSymbolStripping },
39 };
40
41 int main(int argc, char **argv) {
42   ToolCommandLine Opts(argc, argv, false);
43   bool Quiet = false;
44
45   for (int i = 1; i < argc; i++) {
46     if (string(argv[i]) == string("--help")) {
47       cerr << argv[0] << " usage:\n"
48            << "  " << argv[0] << " --help  - Print this usage information\n";
49       for (unsigned j = 0; j < sizeof(OptTable)/sizeof(OptTable[0]); ++j) {
50         cerr << "\t" << OptTable[j].ArgName << "\t - Enable " 
51              << OptTable[j].Name << endl;
52       }
53       return 1;
54     } else if (string(argv[i]) == string("-q")) {
55       Quiet = true; argv[i] = 0;
56     }
57   }
58   
59   ostream *Out = &cout;  // Default to printing to stdout...
60
61   Module *C = ParseBytecodeFile(Opts.getInputFilename());
62   if (C == 0) {
63     cerr << "bytecode didn't read correctly.\n";
64     return 1;
65   }
66
67
68   for (int i = 1; i < argc; i++) {
69     if (argv[i] == 0) continue;
70     unsigned j;
71     for (j = 0; j < sizeof(OptTable)/sizeof(OptTable[0]); j++) {
72       if (string(argv[i]) == OptTable[j].ArgName) {
73         if (OptTable[j].OptPtr(C) && !Quiet)
74           cerr << OptTable[j].Name << " pass made modifications!\n";
75         break;
76       }
77     }
78
79     if (j == sizeof(OptTable)/sizeof(OptTable[0])) 
80       cerr << "'" << argv[i] << "' argument unrecognized: ignored\n";
81   }
82
83   if (Opts.getOutputFilename() != "-") {
84     Out = new ofstream(Opts.getOutputFilename().c_str(), 
85                        (Opts.getForce() ? 0 : ios::noreplace)|ios::out);
86     if (!Out->good()) {
87       cerr << "Error opening " << Opts.getOutputFilename() 
88            << "!\n";
89       delete C;
90       return 1;
91     }
92   }
93
94   // Okay, we're done now... write out result...
95   WriteBytecodeToFile(C, *Out);
96   delete C;
97
98   if (Out != &cout) delete Out;
99   return 0;
100 }