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/Analysis/Verifier.h"
15 #include "llvm/Transforms/UnifyMethodExitNodes.h"
16 #include "llvm/Transforms/ConstantMerge.h"
17 #include "llvm/Transforms/CleanupGCCOutput.h"
18 #include "llvm/Transforms/LevelChange.h"
19 #include "llvm/Transforms/MethodInlining.h"
20 #include "llvm/Transforms/SymbolStripping.h"
21 #include "llvm/Transforms/ChangeAllocations.h"
22 #include "llvm/Transforms/IPO/SimpleStructMutation.h"
23 #include "llvm/Transforms/IPO/GlobalDCE.h"
24 #include "llvm/Transforms/Scalar/DCE.h"
25 #include "llvm/Transforms/Scalar/ConstantProp.h"
26 #include "llvm/Transforms/Scalar/IndVarSimplify.h"
27 #include "llvm/Transforms/Scalar/InstructionCombining.h"
28 #include "llvm/Transforms/Scalar/PromoteMemoryToRegister.h"
29 #include "llvm/Transforms/Instrumentation/TraceValues.h"
30 #include "llvm/Transforms/Instrumentation/ProfilePaths.h"
31 #include "Support/CommandLine.h"
35 // Opts enum - All of the transformations we can do...
37 // Basic optimizations
38 dce, constprop, inlining, constmerge, strip, mstrip, mergereturn,
40 // Miscellaneous Transformations
41 raiseallocs, cleangcc,
43 // Printing and verifying...
46 // More powerful optimizations
47 indvars, instcombine, sccp, adce, raise, mem2reg,
52 // Interprocedural optimizations...
53 globaldce, swapstructs, sortstructs,
57 // New template functions - Provide functions that return passes of specified
58 // types, with specified arguments...
60 template<class PassClass>
62 return new PassClass();
65 template<class PassClass, typename ArgTy1, ArgTy1 Arg1>
67 return new PassClass(Arg1);
70 template<class PassClass, typename ArgTy1, ArgTy1 Arg1,
71 typename ArgTy2, ArgTy1 Arg2>
73 return new PassClass(Arg1, Arg2);
76 static Pass *NewPrintMethodPass() {
77 return new PrintMethodPass("Current Method: \n", &cerr);
80 // OptTable - Correlate enum Opts to Pass constructors...
86 { dce , New<DeadCodeElimination> },
87 { constprop , New<ConstantPropogation> },
88 { inlining , New<MethodInlining> },
89 { constmerge , New<ConstantMerge> },
90 { strip , New<SymbolStripping> },
91 { mstrip , New<FullSymbolStripping> },
92 { mergereturn, New<UnifyMethodExitNodes> },
94 { indvars , New<InductionVariableSimplify> },
95 { instcombine, New<InstructionCombining> },
96 { sccp , New<SCCPPass> },
97 { adce , New<AgressiveDCE> },
98 { raise , New<RaisePointerReferences> },
99 { mem2reg , newPromoteMemoryToRegister },
101 { trace , New<InsertTraceCode, bool, true, bool, true> },
102 { tracem , New<InsertTraceCode, bool, false, bool, true> },
103 { paths , New<ProfilePaths> },
104 { print , NewPrintMethodPass },
105 { verify , createVerifierPass },
106 { raiseallocs, New<RaiseAllocations> },
107 { cleangcc , New<CleanupGCCOutput> },
108 { globaldce , New<GlobalDCE> },
109 { swapstructs, New<SimpleStructMutation, SimpleStructMutation::Transform,
110 SimpleStructMutation::SwapElements>},
111 { sortstructs, New<SimpleStructMutation, SimpleStructMutation::Transform,
112 SimpleStructMutation::SortElements>},
115 // Command line option handling code...
117 cl::String InputFilename ("", "Load <arg> file to optimize", cl::NoFlags, "-");
118 cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "");
119 cl::Flag Force ("f", "Overwrite output files", cl::NoFlags, false);
120 cl::Flag PrintEachXForm("p", "Print module after each transformation");
121 cl::Flag Quiet ("q", "Don't print modifying pass names", 0, false);
122 cl::Alias QuietA ("quiet", "Alias for -q", cl::NoFlags, Quiet);
123 cl::EnumList<enum Opts> OptimizationList(cl::NoFlags,
124 clEnumVal(dce , "Dead Code Elimination"),
125 clEnumVal(constprop , "Simple constant propogation"),
126 clEnumValN(inlining , "inline", "Method integration"),
127 clEnumVal(constmerge , "Merge identical global constants"),
128 clEnumVal(strip , "Strip symbols"),
129 clEnumVal(mstrip , "Strip module symbols"),
130 clEnumVal(mergereturn, "Unify method exit nodes"),
132 clEnumVal(indvars , "Simplify Induction Variables"),
133 clEnumVal(instcombine, "Combine redundant instructions"),
134 clEnumVal(sccp , "Sparse Conditional Constant Propogation"),
135 clEnumVal(adce , "Agressive DCE"),
136 clEnumVal(mem2reg , "Promote alloca locations to registers"),
138 clEnumVal(globaldce , "Remove unreachable globals"),
139 clEnumVal(swapstructs, "Swap structure types around"),
140 clEnumVal(sortstructs, "Sort structure elements"),
142 clEnumVal(raiseallocs, "Raise allocations from calls to instructions"),
143 clEnumVal(cleangcc , "Cleanup GCC Output"),
144 clEnumVal(raise , "Raise to Higher Level"),
145 clEnumVal(trace , "Insert BB & Method trace code"),
146 clEnumVal(tracem , "Insert Method trace code only"),
147 clEnumVal(paths , "Insert path profiling instrumentation"),
148 clEnumVal(print , "Print working method to stderr"),
149 clEnumVal(verify , "Verify module is well formed"),
154 int main(int argc, char **argv) {
155 cl::ParseCommandLineOptions(argc, argv,
156 " llvm .bc -> .bc modular optimizer\n");
158 // Load the input module...
159 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
161 cerr << "bytecode didn't read correctly.\n";
165 // Figure out what stream we are supposed to write to...
166 std::ostream *Out = &std::cout; // Default to printing to stdout...
167 if (OutputFilename != "") {
168 if (!Force && std::ifstream(OutputFilename.c_str())) {
169 // If force is not specified, make sure not to overwrite a file!
170 cerr << "Error opening '" << OutputFilename << "': File exists!\n"
171 << "Use -f command line argument to force output\n";
174 Out = new std::ofstream(OutputFilename.c_str());
177 cerr << "Error opening " << OutputFilename << "!\n";
182 // Create a PassManager to hold and optimize the collection of passes we are
187 // Create a new optimization pass for each one specified on the command line
188 for (unsigned i = 0; i < OptimizationList.size(); ++i) {
189 enum Opts Opt = OptimizationList[i];
190 for (unsigned j = 0; j < sizeof(OptTable)/sizeof(OptTable[0]); ++j)
191 if (Opt == OptTable[j].OptID) {
192 Passes.add(OptTable[j].PassCtor());
197 Passes.add(new PrintModulePass(&std::cerr));
200 // Check that the module is well formed on completion of optimization
201 Passes.add(createVerifierPass());
203 // Write bytecode out to disk or cout as the last step...
204 Passes.add(new WriteBytecodePass(Out, Out != &std::cout));
206 // Now that we have all of the passes ready, run them.
207 if (Passes.run(M.get()) && !Quiet)
208 cerr << "Program modified.\n";