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/Transforms/PrintModulePass.h"
16 #include "llvm/Transforms/ConstantMerge.h"
22 // Basic optimizations
23 dce, constprop, inlining, mergecons, 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 { mergecons, new ConstantMerge() },
40 { strip , new opt::SymbolStripping() },
41 { mstrip , new opt::FullSymbolStripping() },
42 { indvars , new opt::InductionVariableCannonicalize() },
43 { sccp , new opt::SCCPPass() },
44 { adce , new opt::AgressiveDCE() },
45 { raise , new opt::RaiseRepresentation() },
46 { trace , new InsertTraceCode(true, true) },
47 { tracem , new InsertTraceCode(false, true) },
48 { print , new PrintModulePass("Current Method: \n",&cerr) },
51 cl::String InputFilename ("", "Load <arg> file to optimize", cl::NoFlags, "-");
52 cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "");
53 cl::Flag Force ("f", "Overwrite output files", cl::NoFlags, false);
54 cl::Flag Quiet ("q", "Don't print modifying pass names", 0, false);
55 cl::Alias QuietA ("quiet", "Alias for -q", cl::NoFlags, Quiet);
56 cl::EnumList<enum Opts> OptimizationList(cl::NoFlags,
57 clEnumVal(dce , "Dead Code Elimination"),
58 clEnumVal(constprop, "Simple Constant Propogation"),
59 clEnumValN(inlining , "inline", "Method Integration"),
60 clEnumVal(mergecons, "Merge identical global constants"),
61 clEnumVal(strip , "Strip Symbols"),
62 clEnumVal(mstrip , "Strip Module Symbols"),
63 clEnumVal(indvars , "Simplify Induction Variables"),
64 clEnumVal(sccp , "Sparse Conditional Constant Propogation"),
65 clEnumVal(adce , "Agressive DCE"),
66 clEnumVal(raise , "Raise to Higher Level"),
67 clEnumVal(trace , "Insert BB & Method trace code"),
68 clEnumVal(tracem , "Insert Method trace code only"),
69 clEnumVal(print , "Print working method to stderr"),
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;