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"
19 #include "llvm/Transforms/SwapStructContents.h"
20 #include "llvm/Transforms/IPO/GlobalDCE.h"
27 // Basic optimizations
28 dce, constprop, inlining, mergecons, strip, mstrip,
30 // Miscellaneous Transformations
31 trace, tracem, print, cleangcc,
33 // More powerful optimizations
34 indvars, sccp, adce, raise,
36 // Interprocedural optimizations...
37 globaldce, swapstructs,
45 { dce , new opt::DeadCodeElimination() },
46 { constprop, new opt::ConstantPropogation() },
47 { inlining , new opt::MethodInlining() },
48 { mergecons, new ConstantMerge() },
49 { strip , new opt::SymbolStripping() },
50 { mstrip , new opt::FullSymbolStripping() },
51 { indvars , new opt::InductionVariableCannonicalize() },
52 { sccp , new opt::SCCPPass() },
53 { adce , new opt::AgressiveDCE() },
54 { raise , new RaisePointerReferences() },
55 { trace , new InsertTraceCode(true, true) },
56 { tracem , new InsertTraceCode(false, true) },
57 { print , new PrintModulePass("Current Method: \n",&cerr) },
58 { cleangcc , new CleanupGCCOutput() },
61 cl::String InputFilename ("", "Load <arg> file to optimize", cl::NoFlags, "-");
62 cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "");
63 cl::Flag Force ("f", "Overwrite output files", cl::NoFlags, false);
64 cl::Flag Quiet ("q", "Don't print modifying pass names", 0, false);
65 cl::Alias QuietA ("quiet", "Alias for -q", cl::NoFlags, Quiet);
66 cl::EnumList<enum Opts> OptimizationList(cl::NoFlags,
67 clEnumVal(dce , "Dead Code Elimination"),
68 clEnumVal(constprop, "Simple Constant Propogation"),
69 clEnumValN(inlining , "inline", "Method Integration"),
70 clEnumVal(mergecons, "Merge identical global constants"),
71 clEnumVal(strip , "Strip Symbols"),
72 clEnumVal(mstrip , "Strip Module Symbols"),
73 clEnumVal(indvars , "Simplify Induction Variables"),
74 clEnumVal(sccp , "Sparse Conditional Constant Propogation"),
75 clEnumVal(adce , "Agressive DCE"),
77 clEnumVal(globaldce, "Remove unreachable globals"),
78 clEnumVal(swapstructs, "Swap structure types around"),
80 clEnumVal(cleangcc , "Cleanup GCC Output"),
81 clEnumVal(raise , "Raise to Higher Level"),
82 clEnumVal(trace , "Insert BB & Method trace code"),
83 clEnumVal(tracem , "Insert Method trace code only"),
84 clEnumVal(print , "Print working method to stderr"),
87 static void RunOptimization(Module *M, enum Opts Opt) {
88 for (unsigned j = 0; j < sizeof(OptTable)/sizeof(OptTable[0]); ++j)
89 if (Opt == OptTable[j].OptID) {
90 if (OptTable[j].ThePass->run(M) && !Quiet)
91 cerr << OptimizationList.getArgName(Opt)
92 << " pass made modifications!\n";
96 // Special cases that haven't been fit into a consistent framework yet...
99 GlobalDCE GDCE; GDCE.run(M); return;
102 PrebuiltStructMutation SM(M, PrebuiltStructMutation::SortElements);
106 cerr << "Optimization tables inconsistent!!\n";
110 int main(int argc, char **argv) {
111 cl::ParseCommandLineOptions(argc, argv,
112 " llvm .bc -> .bc modular optimizer\n");
113 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
115 cerr << "bytecode didn't read correctly.\n";
119 // Run all of the optimizations specified on the command line
120 for (unsigned i = 0; i < OptimizationList.size(); ++i)
121 RunOptimization(M.get(), OptimizationList[i]);
123 ostream *Out = &cout; // Default to printing to stdout...
124 if (OutputFilename != "") {
125 Out = new ofstream(OutputFilename.c_str(),
126 (Force ? 0 : ios::noreplace)|ios::out);
128 cerr << "Error opening " << OutputFilename << "!\n";
133 // Okay, we're done now... write out result...
134 WriteBytecodeToFile(M.get(), *Out);
136 if (Out != &cout) delete Out;