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