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/Scalar/DecomposeMultiDimRefs.h"
30 #include "llvm/Transforms/Instrumentation/TraceValues.h"
31 #include "llvm/Transforms/Instrumentation/ProfilePaths.h"
32 #include "Support/CommandLine.h"
36 // Opts enum - All of the transformations we can do...
38 // Basic optimizations
39 dce, die, constprop, inlining, constmerge, strip, mstrip, mergereturn,
41 // Miscellaneous Transformations
42 raiseallocs, cleangcc, lowerrefs,
44 // Printing and verifying...
47 // More powerful optimizations
48 indvars, instcombine, sccp, adce, raise, mem2reg,
53 // Interprocedural optimizations...
54 globaldce, swapstructs, sortstructs,
57 static Pass *createPrintMethodPass() {
58 return new PrintMethodPass("Current Method: \n", &cerr);
61 // OptTable - Correlate enum Opts to Pass constructors...
67 { dce , createDeadCodeEliminationPass },
68 { die , createDeadInstEliminationPass },
69 { constprop , createConstantPropogationPass },
70 { inlining , createMethodInliningPass },
71 { constmerge , createConstantMergePass },
72 { strip , createSymbolStrippingPass },
73 { mstrip , createFullSymbolStrippingPass },
74 { mergereturn, createUnifyMethodExitNodesPass },
76 { indvars , createIndVarSimplifyPass },
77 { instcombine, createInstructionCombiningPass },
78 { sccp , createSCCPPass },
79 { adce , createAgressiveDCEPass },
80 { raise , createRaisePointerReferencesPass },
81 { mem2reg , createPromoteMemoryToRegister },
83 { trace , createTraceValuesPassForBasicBlocks },
84 { tracem , createTraceValuesPassForMethod },
85 { paths , createProfilePathsPass },
87 { print , createPrintMethodPass },
88 { verify , createVerifierPass },
90 { raiseallocs, createRaiseAllocationsPass },
91 { cleangcc , createCleanupGCCOutputPass },
92 { globaldce , createGlobalDCEPass },
93 { swapstructs, createSwapElementsPass },
94 { sortstructs, createSortElementsPass },
95 { lowerrefs, createDecomposeMultiDimRefsPass }
98 // Command line option handling code...
100 cl::String InputFilename ("", "Load <arg> file to optimize", cl::NoFlags, "-");
101 cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "");
102 cl::Flag Force ("f", "Overwrite output files", cl::NoFlags, false);
103 cl::Flag PrintEachXForm("p", "Print module after each transformation");
104 cl::Flag Quiet ("q", "Don't print modifying pass names", 0, false);
105 cl::Alias QuietA ("quiet", "Alias for -q", cl::NoFlags, Quiet);
106 cl::EnumList<enum Opts> OptimizationList(cl::NoFlags,
107 clEnumVal(dce , "Dead Code Elimination"),
108 clEnumVal(die , "Dead Instruction 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(mergereturn, "Unify method exit nodes"),
116 clEnumVal(indvars , "Simplify Induction Variables"),
117 clEnumVal(instcombine, "Combine redundant instructions"),
118 clEnumVal(sccp , "Sparse Conditional Constant Propogation"),
119 clEnumVal(adce , "Agressive DCE"),
120 clEnumVal(mem2reg , "Promote alloca locations to registers"),
122 clEnumVal(globaldce , "Remove unreachable globals"),
123 clEnumVal(swapstructs, "Swap structure types around"),
124 clEnumVal(sortstructs, "Sort structure elements"),
126 clEnumVal(raiseallocs, "Raise allocations from calls to instructions"),
127 clEnumVal(cleangcc , "Cleanup GCC Output"),
128 clEnumVal(raise , "Raise to Higher Level"),
129 clEnumVal(trace , "Insert BB & Method trace code"),
130 clEnumVal(tracem , "Insert Method trace code only"),
131 clEnumVal(paths , "Insert path profiling instrumentation"),
132 clEnumVal(print , "Print working method to stderr"),
133 clEnumVal(verify , "Verify module is well formed"),
134 clEnumVal(lowerrefs , "Decompose multi-dimensional structure/array refs to use one index per instruction"),
139 int main(int argc, char **argv) {
140 cl::ParseCommandLineOptions(argc, argv,
141 " llvm .bc -> .bc modular optimizer\n");
143 // Load the input module...
144 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
146 cerr << "bytecode didn't read correctly.\n";
150 // Figure out what stream we are supposed to write to...
151 std::ostream *Out = &std::cout; // Default to printing to stdout...
152 if (OutputFilename != "") {
153 if (!Force && std::ifstream(OutputFilename.c_str())) {
154 // If force is not specified, make sure not to overwrite a file!
155 cerr << "Error opening '" << OutputFilename << "': File exists!\n"
156 << "Use -f command line argument to force output\n";
159 Out = new std::ofstream(OutputFilename.c_str());
162 cerr << "Error opening " << OutputFilename << "!\n";
167 // Create a PassManager to hold and optimize the collection of passes we are
172 // Create a new optimization pass for each one specified on the command line
173 for (unsigned i = 0; i < OptimizationList.size(); ++i) {
174 enum Opts Opt = OptimizationList[i];
175 for (unsigned j = 0; j < sizeof(OptTable)/sizeof(OptTable[0]); ++j)
176 if (Opt == OptTable[j].OptID) {
177 Passes.add(OptTable[j].PassCtor());
182 Passes.add(new PrintModulePass(&std::cerr));
185 // Check that the module is well formed on completion of optimization
186 Passes.add(createVerifierPass());
188 // Write bytecode out to disk or cout as the last step...
189 Passes.add(new WriteBytecodePass(Out, Out != &std::cout));
191 // Now that we have all of the passes ready, run them.
192 if (Passes.run(M.get()) && !Quiet)
193 cerr << "Program modified.\n";