* Add hooks to globaldce
[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 "llvm/Transforms/SwapStructContents.h"
20 #include "llvm/Transforms/IPO/GlobalDCE.h"
21 #include <fstream>
22 #include <memory>
23
24 using namespace opt;
25
26 enum Opts {
27   // Basic optimizations
28   dce, constprop, inlining, mergecons, strip, mstrip,
29
30   // Miscellaneous Transformations
31   trace, tracem, print, cleangcc,
32
33   // More powerful optimizations
34   indvars, sccp, adce, raise,
35
36   // Interprocedural optimizations...
37   globaldce, swapstructs,
38 };
39
40 struct {
41   enum Opts OptID;
42   Pass *ThePass;
43 } OptTable[] = {
44   { swapstructs, 0 },
45   { dce      , new opt::DeadCodeElimination() },
46   { constprop, new opt::ConstantPropogation() }, 
47   { inlining , new opt::MethodInlining() },
48   { mergecons, new ConstantMerge() },
49   { strip    , new opt::SymbolStripping() },
50   { mstrip   , new opt::FullSymbolStripping() },
51   { indvars  , new opt::InductionVariableCannonicalize() },
52   { sccp     , new opt::SCCPPass() },
53   { adce     , new opt::AgressiveDCE() },
54   { raise    , new RaisePointerReferences() },
55   { trace    , new InsertTraceCode(true, true) },
56   { tracem   , new InsertTraceCode(false, true) },
57   { print    , new PrintModulePass("Current Method: \n",&cerr) },
58   { cleangcc , new CleanupGCCOutput() },
59 };
60
61 cl::String InputFilename ("", "Load <arg> file to optimize", cl::NoFlags, "-");
62 cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "");
63 cl::Flag   Force         ("f", "Overwrite output files", cl::NoFlags, false);
64 cl::Flag   Quiet         ("q", "Don't print modifying pass names", 0, false);
65 cl::Alias  QuietA        ("quiet", "Alias for -q", cl::NoFlags, Quiet);
66 cl::EnumList<enum Opts> OptimizationList(cl::NoFlags,
67   clEnumVal(dce      , "Dead Code Elimination"),
68   clEnumVal(constprop, "Simple Constant Propogation"),
69  clEnumValN(inlining , "inline", "Method Integration"),
70   clEnumVal(mergecons, "Merge identical global constants"),
71   clEnumVal(strip    , "Strip Symbols"),
72   clEnumVal(mstrip   , "Strip Module Symbols"),
73   clEnumVal(indvars  , "Simplify Induction Variables"),
74   clEnumVal(sccp     , "Sparse Conditional Constant Propogation"),
75   clEnumVal(adce     , "Agressive DCE"),
76
77   clEnumVal(globaldce, "Remove unreachable globals"),
78   clEnumVal(swapstructs, "Swap structure types around"),
79
80   clEnumVal(cleangcc , "Cleanup GCC Output"),
81   clEnumVal(raise    , "Raise to Higher Level"),
82   clEnumVal(trace    , "Insert BB & Method trace code"),
83   clEnumVal(tracem   , "Insert Method trace code only"),
84   clEnumVal(print    , "Print working method to stderr"),
85 0);
86
87 static void RunOptimization(Module *M, enum Opts Opt) {
88   for (unsigned j = 0; j < sizeof(OptTable)/sizeof(OptTable[0]); ++j)
89     if (Opt == OptTable[j].OptID) {
90       if (OptTable[j].ThePass->run(M) && !Quiet)
91         cerr << OptimizationList.getArgName(Opt)
92              << " pass made modifications!\n";
93       return;
94     }
95   
96   // Special cases that haven't been fit into a consistent framework yet...
97   switch (Opt) {
98   case globaldce: {
99     GlobalDCE GDCE; GDCE.run(M); return;
100   }
101   case swapstructs: {
102     PrebuiltStructMutation SM(M, PrebuiltStructMutation::SortElements);
103     SM.run(M); return;
104   }
105   default:
106     cerr << "Optimization tables inconsistent!!\n";
107   }
108 }
109
110 int main(int argc, char **argv) {
111   cl::ParseCommandLineOptions(argc, argv,
112                               " llvm .bc -> .bc modular optimizer\n");
113   std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
114   if (M.get() == 0) {
115     cerr << "bytecode didn't read correctly.\n";
116     return 1;
117   }
118
119   // Run all of the optimizations specified on the command line
120   for (unsigned i = 0; i < OptimizationList.size(); ++i)
121     RunOptimization(M.get(), OptimizationList[i]);
122
123   ostream *Out = &cout;  // Default to printing to stdout...
124   if (OutputFilename != "") {
125     Out = new ofstream(OutputFilename.c_str(), 
126                        (Force ? 0 : ios::noreplace)|ios::out);
127     if (!Out->good()) {
128       cerr << "Error opening " << OutputFilename << "!\n";
129       return 1;
130     }
131   }
132
133   // Okay, we're done now... write out result...
134   WriteBytecodeToFile(M.get(), *Out);
135
136   if (Out != &cout) delete Out;
137   return 0;
138 }