1 //===------------------------------------------------------------------------===
4 // This utility may be invoked in the following manner:
5 // opt --help - Output information about command line switches
6 // opt [options] -dce - Run a dead code elimination pass on input
8 // opt [options] -constprop - Run a constant propogation pass on input
10 // opt [options] -inline - Run a method inlining pass on input bytecodes
11 // opt [options] -strip - Strip symbol tables out of methods
12 // opt [options] -mstrip - Strip module & method symbol tables
14 // Optimizations may be specified an arbitrary number of times on the command
15 // line, they are run in the order specified.
17 // TODO: Add a -all option to keep applying all optimizations until the program
20 //===------------------------------------------------------------------------===
24 #include "llvm/Module.h"
25 #include "llvm/Bytecode/Reader.h"
26 #include "llvm/Bytecode/Writer.h"
27 #include "llvm/Support/CommandLine.h"
28 #include "llvm/Optimizations/AllOpts.h"
33 // Basic optimizations
34 dce, constprop, inlining, strip, mstrip,
36 // More powerful optimizations
37 indvars, sccp, adce, raise,
44 { dce , new opt::DeadCodeElimination() },
45 { constprop, new opt::ConstantPropogation() },
46 { inlining , new opt::MethodInlining() },
47 { strip , new opt::SymbolStripping() },
48 { mstrip , new opt::FullSymbolStripping() },
49 { indvars , new opt::InductionVariableCannonicalize() },
50 { sccp , new opt::SCCPPass() },
51 { adce , new opt::AgressiveDCE() },
52 { raise , new opt::RaiseRepresentation() },
55 cl::String InputFilename ("", "Load <arg> file to optimize", cl::NoFlags, "-");
56 cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "");
57 cl::Flag Force ("f", "Overwrite output files", cl::NoFlags, false);
58 cl::Flag Quiet ("q", "Don't print modifying pass names", 0, false);
59 cl::Alias QuietA ("quiet", "Alias for -q", cl::NoFlags, Quiet);
60 cl::EnumList<enum Opts> OptimizationList(cl::NoFlags,
61 clEnumVal(dce , "Dead Code Elimination"),
62 clEnumVal(constprop, "Simple Constant Propogation"),
63 clEnumValN(inlining , "inline", "Method Integration"),
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(raise , "Raise to Higher Level"),
73 int main(int argc, char **argv) {
74 cl::ParseCommandLineOptions(argc, argv,
75 " llvm .bc -> .bc modular optimizer\n");
77 Module *C = ParseBytecodeFile(InputFilename);
79 cerr << "bytecode didn't read correctly.\n";
83 for (unsigned i = 0; i < OptimizationList.size(); ++i) {
84 enum Opts Opt = OptimizationList[i];
87 for (j = 0; j < sizeof(OptTable)/sizeof(OptTable[0]); ++j) {
88 if (Opt == OptTable[j].OptID) {
89 if (OptTable[j].ThePass->run(C) && !Quiet)
90 cerr << OptimizationList.getArgName(Opt)
91 << " pass made modifications!\n";
96 if (j == sizeof(OptTable)/sizeof(OptTable[0]))
97 cerr << "Optimization tables inconsistent!!\n";
100 ostream *Out = &cout; // Default to printing to stdout...
101 if (OutputFilename != "") {
102 Out = new ofstream(OutputFilename.c_str(),
103 (Force ? 0 : ios::noreplace)|ios::out);
105 cerr << "Error opening " << OutputFilename << "!\n";
111 // Okay, we're done now... write out result...
112 WriteBytecodeToFile(C, *Out);
115 if (Out != &cout) delete Out;