Use the standard header not the old one
[oota-llvm.git] / tools / opt / opt.cpp
1 //===----------------------------------------------------------------------===//
2 // LLVM 'OPT' UTILITY 
3 //
4 // Optimizations may be specified an arbitrary number of times on the command
5 // line, they are run in the order specified.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/Module.h"
10 #include "llvm/Bytecode/Reader.h"
11 #include "llvm/Bytecode/Writer.h"
12 #include "llvm/Support/CommandLine.h"
13 #include "llvm/Optimizations/AllOpts.h"
14 #include "llvm/Transforms/Instrumentation/TraceValues.h"
15 #include "llvm/Transforms/PrintModulePass.h"
16 #include <fstream>
17
18 using namespace opt;
19
20 enum Opts {
21   // Basic optimizations
22   dce, constprop, inlining, strip, mstrip,
23
24   // Miscellaneous Transformations
25   trace, tracem, print,
26
27   // More powerful optimizations
28   indvars, sccp, adce, raise,
29 };
30
31 struct {
32   enum Opts OptID;
33   Pass *ThePass;
34 } OptTable[] = {
35   { dce      , new opt::DeadCodeElimination() },
36   { constprop, new opt::ConstantPropogation() }, 
37   { inlining , new opt::MethodInlining() },
38   { strip    , new opt::SymbolStripping() },
39   { mstrip   , new opt::FullSymbolStripping() },
40   { indvars  , new opt::InductionVariableCannonicalize() },
41   { sccp     , new opt::SCCPPass() },
42   { adce     , new opt::AgressiveDCE() },
43   { raise    , new opt::RaiseRepresentation() },
44   { trace    , new InsertTraceCode(true, true) },
45   { tracem   , new InsertTraceCode(false, true) },
46   { print    , new PrintModulePass("Current Method: \n",&cerr) },
47 };
48
49 cl::String InputFilename ("", "Load <arg> file to optimize", cl::NoFlags, "-");
50 cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "");
51 cl::Flag   Force         ("f", "Overwrite output files", cl::NoFlags, false);
52 cl::Flag   Quiet         ("q", "Don't print modifying pass names", 0, false);
53 cl::Alias  QuietA        ("quiet", "Alias for -q", cl::NoFlags, Quiet);
54 cl::EnumList<enum Opts> OptimizationList(cl::NoFlags,
55   clEnumVal(dce      , "Dead Code Elimination"),
56   clEnumVal(constprop, "Simple Constant Propogation"),
57  clEnumValN(inlining , "inline", "Method Integration"),
58   clEnumVal(strip    , "Strip Symbols"),
59   clEnumVal(mstrip   , "Strip Module Symbols"),
60   clEnumVal(indvars  , "Simplify Induction Variables"),
61   clEnumVal(sccp     , "Sparse Conditional Constant Propogation"),
62   clEnumVal(adce     , "Agressive DCE"),
63   clEnumVal(raise    , "Raise to Higher Level"),
64   clEnumVal(trace    , "Insert BB & Method trace code"),
65   clEnumVal(tracem   , "Insert Method trace code only"),
66   clEnumVal(print    , "Print working method to stderr"),
67 0);
68
69
70 int main(int argc, char **argv) {
71   cl::ParseCommandLineOptions(argc, argv,
72                               " llvm .bc -> .bc modular optimizer\n");
73  
74   Module *C = ParseBytecodeFile(InputFilename);
75   if (C == 0) {
76     cerr << "bytecode didn't read correctly.\n";
77     return 1;
78   }
79
80   for (unsigned i = 0; i < OptimizationList.size(); ++i) {
81     enum Opts Opt = OptimizationList[i];
82
83     unsigned j;
84     for (j = 0; j < sizeof(OptTable)/sizeof(OptTable[0]); ++j) {
85       if (Opt == OptTable[j].OptID) {
86         if (OptTable[j].ThePass->run(C) && !Quiet)
87           cerr << OptimizationList.getArgName(Opt)
88                << " pass made modifications!\n";
89         break;
90       }
91     }
92
93     if (j == sizeof(OptTable)/sizeof(OptTable[0])) 
94       cerr << "Optimization tables inconsistent!!\n";
95   }
96
97   ostream *Out = &cout;  // Default to printing to stdout...
98   if (OutputFilename != "") {
99     Out = new ofstream(OutputFilename.c_str(), 
100                        (Force ? 0 : ios::noreplace)|ios::out);
101     if (!Out->good()) {
102       cerr << "Error opening " << OutputFilename << "!\n";
103       delete C;
104       return 1;
105     }
106   }
107
108   // Okay, we're done now... write out result...
109   WriteBytecodeToFile(C, *Out);
110   delete C;
111
112   if (Out != &cout) delete Out;
113   return 0;
114 }