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"
23 // Basic optimizations
24 dce, constprop, inlining, mergecons, strip, mstrip,
26 // Miscellaneous Transformations
27 trace, tracem, print, cleangcc,
29 // More powerful optimizations
30 indvars, sccp, adce, raise,
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() },
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"),
76 int main(int argc, char **argv) {
77 cl::ParseCommandLineOptions(argc, argv,
78 " llvm .bc -> .bc modular optimizer\n");
80 Module *C = ParseBytecodeFile(InputFilename);
82 cerr << "bytecode didn't read correctly.\n";
86 for (unsigned i = 0; i < OptimizationList.size(); ++i) {
87 enum Opts Opt = OptimizationList[i];
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";
99 if (j == sizeof(OptTable)/sizeof(OptTable[0]))
100 cerr << "Optimization tables inconsistent!!\n";
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);
108 cerr << "Error opening " << OutputFilename << "!\n";
114 // Okay, we're done now... write out result...
115 WriteBytecodeToFile(C, *Out);
118 if (Out != &cout) delete Out;