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