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