1 //===----------------------------------------------------------------------===//
4 // Optimizations may be specified an arbitrary number of times on the command
5 // line, they are run in the order specified.
7 //===----------------------------------------------------------------------===//
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"
24 // Basic optimizations
25 dce, constprop, inlining, mergecons, strip, mstrip,
27 // Miscellaneous Transformations
28 trace, tracem, print, cleangcc,
30 // More powerful optimizations
31 indvars, sccp, adce, raise,
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() },
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"),
77 int main(int argc, char **argv) {
78 cl::ParseCommandLineOptions(argc, argv,
79 " llvm .bc -> .bc modular optimizer\n");
81 Module *C = ParseBytecodeFile(InputFilename);
83 cerr << "bytecode didn't read correctly.\n";
87 for (unsigned i = 0; i < OptimizationList.size(); ++i) {
88 enum Opts Opt = OptimizationList[i];
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";
100 if (j == sizeof(OptTable)/sizeof(OptTable[0]))
101 cerr << "Optimization tables inconsistent!!\n";
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);
109 cerr << "Error opening " << OutputFilename << "!\n";
115 // Okay, we're done now... write out result...
116 WriteBytecodeToFile(C, *Out);
119 if (Out != &cout) delete Out;