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