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 "Support/CommandLine.h"
34 // Opts enum - All of the transformations we can do...
36 // Basic optimizations
37 dce, constprop, inlining, constmerge, strip, mstrip, mergereturn,
39 // Miscellaneous Transformations
40 trace, tracem, raiseallocs, cleangcc,
42 // Printing and verifying...
45 // More powerful optimizations
46 indvars, instcombine, sccp, adce, raise, mem2reg,
48 // Interprocedural optimizations...
49 globaldce, swapstructs, sortstructs,
53 // New template functions - Provide functions that return passes of specified
54 // types, with specified arguments...
56 template<class PassClass>
58 return new PassClass();
61 template<class PassClass, typename ArgTy1, ArgTy1 Arg1>
63 return new PassClass(Arg1);
66 template<class PassClass, typename ArgTy1, ArgTy1 Arg1,
67 typename ArgTy2, ArgTy1 Arg2>
69 return new PassClass(Arg1, Arg2);
72 static Pass *NewPrintMethodPass() {
73 return new PrintMethodPass("Current Method: \n", &cerr);
76 // OptTable - Correlate enum Opts to Pass constructors...
82 { dce , New<DeadCodeElimination> },
83 { constprop , New<ConstantPropogation> },
84 { inlining , New<MethodInlining> },
85 { constmerge , New<ConstantMerge> },
86 { strip , New<SymbolStripping> },
87 { mstrip , New<FullSymbolStripping> },
88 { mergereturn, New<UnifyMethodExitNodes> },
90 { indvars , New<InductionVariableSimplify> },
91 { instcombine, New<InstructionCombining> },
92 { sccp , New<SCCPPass> },
93 { adce , New<AgressiveDCE> },
94 { raise , New<RaisePointerReferences> },
95 { mem2reg , newPromoteMemoryToRegister },
97 { trace , New<InsertTraceCode, bool, true, bool, true> },
98 { tracem , New<InsertTraceCode, bool, false, bool, true> },
99 { print , NewPrintMethodPass },
100 { verify , createVerifierPass },
101 { raiseallocs, New<RaiseAllocations> },
102 { cleangcc , New<CleanupGCCOutput> },
103 { globaldce , New<GlobalDCE> },
104 { swapstructs, New<SimpleStructMutation, SimpleStructMutation::Transform,
105 SimpleStructMutation::SwapElements>},
106 { sortstructs, New<SimpleStructMutation, SimpleStructMutation::Transform,
107 SimpleStructMutation::SortElements>},
110 // Command line option handling code...
112 cl::String InputFilename ("", "Load <arg> file to optimize", cl::NoFlags, "-");
113 cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "");
114 cl::Flag Force ("f", "Overwrite output files", cl::NoFlags, false);
115 cl::Flag PrintEachXForm("p", "Print module after each transformation");
116 cl::Flag Quiet ("q", "Don't print modifying pass names", 0, false);
117 cl::Alias QuietA ("quiet", "Alias for -q", cl::NoFlags, Quiet);
118 cl::EnumList<enum Opts> OptimizationList(cl::NoFlags,
119 clEnumVal(dce , "Dead Code Elimination"),
120 clEnumVal(constprop , "Simple constant propogation"),
121 clEnumValN(inlining , "inline", "Method integration"),
122 clEnumVal(constmerge , "Merge identical global constants"),
123 clEnumVal(strip , "Strip symbols"),
124 clEnumVal(mstrip , "Strip module symbols"),
125 clEnumVal(mergereturn, "Unify method exit nodes"),
127 clEnumVal(indvars , "Simplify Induction Variables"),
128 clEnumVal(instcombine, "Combine redundant instructions"),
129 clEnumVal(sccp , "Sparse Conditional Constant Propogation"),
130 clEnumVal(adce , "Agressive DCE"),
131 clEnumVal(mem2reg , "Promote alloca locations to registers"),
133 clEnumVal(globaldce , "Remove unreachable globals"),
134 clEnumVal(swapstructs, "Swap structure types around"),
135 clEnumVal(sortstructs, "Sort structure elements"),
137 clEnumVal(raiseallocs, "Raise allocations from calls to instructions"),
138 clEnumVal(cleangcc , "Cleanup GCC Output"),
139 clEnumVal(raise , "Raise to Higher Level"),
140 clEnumVal(trace , "Insert BB & Method trace code"),
141 clEnumVal(tracem , "Insert Method trace code only"),
143 clEnumVal(print , "Print working method to stderr"),
144 clEnumVal(verify , "Verify module is well formed"),
149 int main(int argc, char **argv) {
150 cl::ParseCommandLineOptions(argc, argv,
151 " llvm .bc -> .bc modular optimizer\n");
153 // Load the input module...
154 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
156 cerr << "bytecode didn't read correctly.\n";
160 // Figure out what stream we are supposed to write to...
161 std::ostream *Out = &std::cout; // Default to printing to stdout...
162 if (OutputFilename != "") {
163 if (!Force && std::ifstream(OutputFilename.c_str())) {
164 // If force is not specified, make sure not to overwrite a file!
165 cerr << "Error opening '" << OutputFilename << "': File exists!\n"
166 << "Use -f command line argument to force output\n";
169 Out = new std::ofstream(OutputFilename.c_str());
172 cerr << "Error opening " << OutputFilename << "!\n";
177 // Create a PassManager to hold and optimize the collection of passes we are
182 // Create a new optimization pass for each one specified on the command line
183 for (unsigned i = 0; i < OptimizationList.size(); ++i) {
184 enum Opts Opt = OptimizationList[i];
185 for (unsigned j = 0; j < sizeof(OptTable)/sizeof(OptTable[0]); ++j)
186 if (Opt == OptTable[j].OptID) {
187 Passes.add(OptTable[j].PassCtor());
192 Passes.add(new PrintModulePass(&std::cerr));
195 // Check that the module is well formed on completion of optimization
196 Passes.add(createVerifierPass());
198 // Write bytecode out to disk or cout as the last step...
199 Passes.add(new WriteBytecodePass(Out, Out != &std::cout));
201 // Now that we have all of the passes ready, run them.
202 if (Passes.run(M.get()) && !Quiet)
203 cerr << "Program modified.\n";