1 //===----------------------------------------------------------------------===//
4 // Optimizations may be specified an arbitrary number of times on the command
5 // line, they are run in the order specified.
7 //===----------------------------------------------------------------------===//
11 #include "llvm/Module.h"
12 #include "llvm/Bytecode/Reader.h"
13 #include "llvm/Bytecode/Writer.h"
14 #include "llvm/Support/CommandLine.h"
15 #include "llvm/Optimizations/AllOpts.h"
16 #include "llvm/Transforms/Instrumentation/TraceValues.h"
17 #include "llvm/Transforms/PrintModulePass.h"
22 // Basic optimizations
23 dce, constprop, inlining, strip, mstrip,
25 // Miscellaneous Transformations
28 // More powerful optimizations
29 indvars, sccp, adce, raise,
36 { dce , new opt::DeadCodeElimination() },
37 { constprop, new opt::ConstantPropogation() },
38 { inlining , new opt::MethodInlining() },
39 { strip , new opt::SymbolStripping() },
40 { mstrip , new opt::FullSymbolStripping() },
41 { indvars , new opt::InductionVariableCannonicalize() },
42 { sccp , new opt::SCCPPass() },
43 { adce , new opt::AgressiveDCE() },
44 { raise , new opt::RaiseRepresentation() },
45 { trace , new InsertTraceCode(true, true) },
46 { tracem , new InsertTraceCode(false, true) },
47 { print , new PrintModulePass("Current Method: \n",&cerr) },
50 cl::String InputFilename ("", "Load <arg> file to optimize", cl::NoFlags, "-");
51 cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "");
52 cl::Flag Force ("f", "Overwrite output files", cl::NoFlags, false);
53 cl::Flag Quiet ("q", "Don't print modifying pass names", 0, false);
54 cl::Alias QuietA ("quiet", "Alias for -q", cl::NoFlags, Quiet);
55 cl::EnumList<enum Opts> OptimizationList(cl::NoFlags,
56 clEnumVal(dce , "Dead Code Elimination"),
57 clEnumVal(constprop, "Simple Constant Propogation"),
58 clEnumValN(inlining , "inline", "Method Integration"),
59 clEnumVal(strip , "Strip Symbols"),
60 clEnumVal(mstrip , "Strip Module Symbols"),
61 clEnumVal(indvars , "Simplify Induction Variables"),
62 clEnumVal(sccp , "Sparse Conditional Constant Propogation"),
63 clEnumVal(adce , "Agressive DCE"),
64 clEnumVal(raise , "Raise to Higher Level"),
65 clEnumVal(trace , "Insert BB & Method trace code"),
66 clEnumVal(tracem , "Insert Method trace code only"),
67 clEnumVal(print , "Print working method to stderr"),
71 int main(int argc, char **argv) {
72 cl::ParseCommandLineOptions(argc, argv,
73 " llvm .bc -> .bc modular optimizer\n");
75 Module *C = ParseBytecodeFile(InputFilename);
77 cerr << "bytecode didn't read correctly.\n";
81 for (unsigned i = 0; i < OptimizationList.size(); ++i) {
82 enum Opts Opt = OptimizationList[i];
85 for (j = 0; j < sizeof(OptTable)/sizeof(OptTable[0]); ++j) {
86 if (Opt == OptTable[j].OptID) {
87 if (OptTable[j].ThePass->run(C) && !Quiet)
88 cerr << OptimizationList.getArgName(Opt)
89 << " pass made modifications!\n";
94 if (j == sizeof(OptTable)/sizeof(OptTable[0]))
95 cerr << "Optimization tables inconsistent!!\n";
98 ostream *Out = &cout; // Default to printing to stdout...
99 if (OutputFilename != "") {
100 Out = new ofstream(OutputFilename.c_str(),
101 (Force ? 0 : ios::noreplace)|ios::out);
103 cerr << "Error opening " << OutputFilename << "!\n";
109 // Okay, we're done now... write out result...
110 WriteBytecodeToFile(C, *Out);
113 if (Out != &cout) delete Out;