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