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