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/PassManager.h"
11 #include "llvm/Bytecode/Reader.h"
12 #include "llvm/Bytecode/WriteBytecodePass.h"
13 #include "llvm/Assembly/PrintModulePass.h"
14 #include "llvm/Transforms/ConstantMerge.h"
15 #include "llvm/Transforms/CleanupGCCOutput.h"
16 #include "llvm/Transforms/LevelChange.h"
17 #include "llvm/Transforms/MethodInlining.h"
18 #include "llvm/Transforms/SymbolStripping.h"
19 #include "llvm/Transforms/ChangeAllocations.h"
20 #include "llvm/Transforms/IPO/SimpleStructMutation.h"
21 #include "llvm/Transforms/IPO/GlobalDCE.h"
22 #include "llvm/Transforms/Scalar/DCE.h"
23 #include "llvm/Transforms/Scalar/ConstantProp.h"
24 #include "llvm/Transforms/Scalar/IndVarSimplify.h"
25 #include "llvm/Transforms/Scalar/InstructionCombining.h"
26 #include "llvm/Transforms/Instrumentation/TraceValues.h"
27 #include "Support/CommandLine.h"
31 // Opts enum - All of the transformations we can do...
33 // Basic optimizations
34 dce, constprop, inlining, constmerge, strip, mstrip,
36 // Miscellaneous Transformations
37 trace, tracem, print, raiseallocs, cleangcc,
39 // More powerful optimizations
40 indvars, instcombine, sccp, adce, raise,
42 // Interprocedural optimizations...
43 globaldce, swapstructs, sortstructs,
47 // New template functions - Provide functions that return passes of specified
48 // types, with specified arguments...
50 template<class PassClass>
52 return new PassClass();
55 template<class PassClass, typename ArgTy1, ArgTy1 Arg1>
57 return new PassClass(Arg1);
60 template<class PassClass, typename ArgTy1, ArgTy1 Arg1,
61 typename ArgTy2, ArgTy1 Arg2>
63 return new PassClass(Arg1, Arg2);
66 static Pass *NewPrintMethodPass() {
67 return new PrintMethodPass("Current Method: \n", &cerr);
70 // OptTable - Correlate enum Opts to Pass constructors...
76 { dce , New<DeadCodeElimination> },
77 { constprop , New<ConstantPropogation> },
78 { inlining , New<MethodInlining> },
79 { constmerge , New<ConstantMerge> },
80 { strip , New<SymbolStripping> },
81 { mstrip , New<FullSymbolStripping> },
82 { indvars , New<InductionVariableSimplify> },
83 { instcombine, New<InstructionCombining> },
84 { sccp , New<SCCPPass> },
85 { adce , New<AgressiveDCE> },
86 { raise , New<RaisePointerReferences> },
87 { trace , New<InsertTraceCode, bool, true, bool, true> },
88 { tracem , New<InsertTraceCode, bool, false, bool, true> },
89 { print , NewPrintMethodPass },
90 { raiseallocs, New<RaiseAllocations> },
91 { cleangcc , New<CleanupGCCOutput> },
92 { globaldce , New<GlobalDCE> },
93 { swapstructs, New<SimpleStructMutation, SimpleStructMutation::Transform,
94 SimpleStructMutation::SwapElements>},
95 { sortstructs, New<SimpleStructMutation, SimpleStructMutation::Transform,
96 SimpleStructMutation::SortElements>},
99 // Command line option handling code...
101 cl::String InputFilename ("", "Load <arg> file to optimize", cl::NoFlags, "-");
102 cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "");
103 cl::Flag Force ("f", "Overwrite output files", cl::NoFlags, false);
104 cl::Flag PrintEachXForm("p", "Print module after each transformation");
105 cl::Flag Quiet ("q", "Don't print modifying pass names", 0, false);
106 cl::Alias QuietA ("quiet", "Alias for -q", cl::NoFlags, Quiet);
107 cl::EnumList<enum Opts> OptimizationList(cl::NoFlags,
108 clEnumVal(dce , "Dead Code Elimination"),
109 clEnumVal(constprop , "Simple Constant Propogation"),
110 clEnumValN(inlining , "inline", "Method Integration"),
111 clEnumVal(constmerge , "Merge identical global constants"),
112 clEnumVal(strip , "Strip Symbols"),
113 clEnumVal(mstrip , "Strip Module Symbols"),
114 clEnumVal(indvars , "Simplify Induction Variables"),
115 clEnumVal(instcombine, "Combine redundant instructions"),
116 clEnumVal(sccp , "Sparse Conditional Constant Propogation"),
117 clEnumVal(adce , "Agressive DCE"),
119 clEnumVal(globaldce , "Remove unreachable globals"),
120 clEnumVal(swapstructs, "Swap structure types around"),
121 clEnumVal(sortstructs, "Sort structure elements"),
123 clEnumVal(raiseallocs, "Raise allocations from calls to instructions"),
124 clEnumVal(cleangcc , "Cleanup GCC Output"),
125 clEnumVal(raise , "Raise to Higher Level"),
126 clEnumVal(trace , "Insert BB & Method trace code"),
127 clEnumVal(tracem , "Insert Method trace code only"),
128 clEnumVal(print , "Print working method to stderr"),
133 int main(int argc, char **argv) {
134 cl::ParseCommandLineOptions(argc, argv,
135 " llvm .bc -> .bc modular optimizer\n");
137 // Load the input module...
138 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
140 cerr << "bytecode didn't read correctly.\n";
144 // Figure out what stream we are supposed to write to...
145 std::ostream *Out = &std::cout; // Default to printing to stdout...
146 if (OutputFilename != "") {
147 if (!Force && std::ifstream(OutputFilename.c_str())) {
148 // If force is not specified, make sure not to overwrite a file!
149 cerr << "Error opening '" << OutputFilename << "': File exists!\n"
150 << "Use -f command line argument to force output\n";
153 Out = new std::ofstream(OutputFilename.c_str());
156 cerr << "Error opening " << OutputFilename << "!\n";
161 // Create a PassManager to hold and optimize the collection of passes we are
166 // Create a new optimization pass for each one specified on the command line
167 for (unsigned i = 0; i < OptimizationList.size(); ++i) {
168 enum Opts Opt = OptimizationList[i];
169 for (unsigned j = 0; j < sizeof(OptTable)/sizeof(OptTable[0]); ++j)
170 if (Opt == OptTable[j].OptID) {
171 Passes.add(OptTable[j].PassCtor());
176 Passes.add(new PrintModulePass(&std::cerr));
179 // Write bytecode out to disk or cout as the last step...
180 Passes.add(new WriteBytecodePass(Out, Out != &std::cout));
182 // Now that we have all of the passes ready, run them.
183 if (Passes.run(M.get()) && !Quiet)
184 cerr << "Program modified.\n";