CommandLine library cleanup. No longer use getValue/setValue, instead, just treat...
[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/Support/CommandLine.h"
28 #include "llvm/Optimizations/AllOpts.h"
29
30 using namespace opt;
31
32 enum Opts {
33   // Basic optimizations
34   dce, constprop, inlining, strip, mstrip,
35
36   // More powerful optimizations
37   indvars, sccp, cpm, adce, raise,
38 };
39
40 struct {
41   enum Opts OptID;
42   bool (*OptPtr)(Module *C);
43 } OptTable[] = {
44   { dce      , DoDeadCodeElimination },
45   { constprop, DoConstantPropogation }, 
46   { inlining , DoMethodInlining      },
47   { strip    , DoSymbolStripping     },
48   { mstrip   , DoFullSymbolStripping },
49   { indvars  , DoInductionVariableCannonicalize },
50   { sccp     , DoSCCP                },
51   { cpm      , DoConstantPoolMerging },
52   { adce     , DoADCE                },
53   { raise    , DoRaiseRepresentation },
54 };
55
56 cl::String InputFilename ("", "Load <arg> file to optimize", 0, "-");
57 cl::String OutputFilename("o", "Override output filename", 0, "");
58 cl::Flag   Force         ("f", "Overwrite output files", 0, false);
59 cl::Flag   Quiet         ("q", "Don't print modifying pass names", 0, false);
60 cl::EnumList<enum Opts> OptimizationList(cl::NoFlags,
61   clEnumVal(dce      , "Dead Code Elimination"),
62   clEnumVal(constprop, "Simple Constant Propogation"),
63  clEnumValN(inlining , "inline", "Method Inlining"),
64   clEnumVal(strip    , "Strip Symbols"),
65   clEnumVal(mstrip   , "Strip Module Symbols"),
66   clEnumVal(indvars  , "Simplify Induction Variables"),
67   clEnumVal(sccp     , "Sparse Conditional Constant Propogation"),
68   clEnumVal(cpm      , "Constant Pool Merging"),
69   clEnumVal(adce     , "Agressive DCE"),
70   clEnumVal(raise    , "Raise to Higher Level"),
71 0);
72
73
74 int main(int argc, char **argv) {
75   cl::ParseCommandLineOptions(argc, argv,
76                               " llvm .bc -> .bc modular optimizer\n");
77  
78   Module *C = ParseBytecodeFile(InputFilename);
79   if (C == 0) {
80     cerr << "bytecode didn't read correctly.\n";
81     return 1;
82   }
83
84   for (unsigned i = 0; i < OptimizationList.size(); ++i) {
85     enum Opts Opt = OptimizationList[i];
86
87     unsigned j;
88     for (j = 0; j < sizeof(OptTable)/sizeof(OptTable[0]); ++j) {
89       if (Opt == OptTable[j].OptID) {
90         if (OptTable[j].OptPtr(C) && !Quiet)
91           cerr << OptimizationList.getArgName(Opt)
92                << " pass made modifications!\n";
93         break;
94       }
95     }
96
97     if (j == sizeof(OptTable)/sizeof(OptTable[0])) 
98       cerr << "Optimization tables inconsistent!!\n";
99   }
100
101   ostream *Out = &cout;  // Default to printing to stdout...
102   if (OutputFilename != "") {
103     Out = new ofstream(OutputFilename.c_str(), 
104                        (Force ? 0 : ios::noreplace)|ios::out);
105     if (!Out->good()) {
106       cerr << "Error opening " << OutputFilename << "!\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 }