blob: 80fad86009394012d1fc61aa1f424c03995c2155 [file] [log] [blame]
Chris Lattner0eafc312001-10-18 06:05:15 +00001//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00002// LLVM 'OPT' UTILITY
3//
Chris Lattner00950542001-06-06 20:29:01 +00004// Optimizations may be specified an arbitrary number of times on the command
5// line, they are run in the order specified.
6//
Chris Lattner0eafc312001-10-18 06:05:15 +00007//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00008
Chris Lattner00950542001-06-06 20:29:01 +00009#include "llvm/Module.h"
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000010#include "llvm/PassManager.h"
Chris Lattner00950542001-06-06 20:29:01 +000011#include "llvm/Bytecode/Reader.h"
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000012#include "llvm/Bytecode/WriteBytecodePass.h"
Chris Lattnerffa6f9c2001-10-19 15:39:14 +000013#include "llvm/Assembly/PrintModulePass.h"
Chris Lattner22d26d72002-02-20 17:56:53 +000014#include "llvm/Analysis/Verifier.h"
Chris Lattner0be41012002-02-01 04:54:11 +000015#include "llvm/Transforms/UnifyMethodExitNodes.h"
Chris Lattner9effd692001-10-18 20:06:45 +000016#include "llvm/Transforms/ConstantMerge.h"
Chris Lattnere166fe12001-10-31 04:29:44 +000017#include "llvm/Transforms/CleanupGCCOutput.h"
Chris Lattner068f4872001-11-01 02:41:09 +000018#include "llvm/Transforms/LevelChange.h"
Chris Lattner59b6b8e2002-01-21 23:17:48 +000019#include "llvm/Transforms/MethodInlining.h"
20#include "llvm/Transforms/SymbolStripping.h"
Chris Lattnerd7db8632002-01-22 01:04:08 +000021#include "llvm/Transforms/ChangeAllocations.h"
Chris Lattner04c85dc2002-01-21 07:52:35 +000022#include "llvm/Transforms/IPO/SimpleStructMutation.h"
Chris Lattner63202322001-11-26 19:22:39 +000023#include "llvm/Transforms/IPO/GlobalDCE.h"
Chris Lattner59b6b8e2002-01-21 23:17:48 +000024#include "llvm/Transforms/Scalar/DCE.h"
25#include "llvm/Transforms/Scalar/ConstantProp.h"
Chris Lattnerfe196cf2001-12-04 04:32:04 +000026#include "llvm/Transforms/Scalar/IndVarSimplify.h"
Chris Lattner528e8b52001-12-14 16:50:35 +000027#include "llvm/Transforms/Scalar/InstructionCombining.h"
Chris Lattnereeeaf522002-02-12 17:17:33 +000028#include "llvm/Transforms/Scalar/PromoteMemoryToRegister.h"
Vikram S. Adved32e70a2002-03-24 03:19:54 +000029#include "llvm/Transforms/Scalar/DecomposeMultiDimRefs.h"
Chris Lattner59b6b8e2002-01-21 23:17:48 +000030#include "llvm/Transforms/Instrumentation/TraceValues.h"
Anand Shukladc9a1f52002-02-26 19:57:59 +000031#include "llvm/Transforms/Instrumentation/ProfilePaths.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000032#include "Support/CommandLine.h"
Chris Lattner73e11d72001-10-18 06:13:08 +000033#include <fstream>
Chris Lattner63202322001-11-26 19:22:39 +000034#include <memory>
Chris Lattner95781b62001-06-30 06:38:31 +000035
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000036// Opts enum - All of the transformations we can do...
Chris Lattner8f367bd2001-07-23 02:35:57 +000037enum Opts {
38 // Basic optimizations
Chris Lattner2b72c362002-03-14 22:36:15 +000039 dce, die, constprop, inlining, constmerge, strip, mstrip, mergereturn,
Chris Lattner8f367bd2001-07-23 02:35:57 +000040
Chris Lattner0eafc312001-10-18 06:05:15 +000041 // Miscellaneous Transformations
Vikram S. Adved32e70a2002-03-24 03:19:54 +000042 raiseallocs, cleangcc, lowerrefs,
Chris Lattner22d26d72002-02-20 17:56:53 +000043
44 // Printing and verifying...
45 print, verify,
Chris Lattner0eafc312001-10-18 06:05:15 +000046
Chris Lattner8f367bd2001-07-23 02:35:57 +000047 // More powerful optimizations
Chris Lattnereeeaf522002-02-12 17:17:33 +000048 indvars, instcombine, sccp, adce, raise, mem2reg,
Chris Lattner63202322001-11-26 19:22:39 +000049
Anand Shukladc9a1f52002-02-26 19:57:59 +000050 // Instrumentation
51 trace, tracem, paths,
52
Chris Lattner63202322001-11-26 19:22:39 +000053 // Interprocedural optimizations...
Chris Lattnerf4de63f2002-01-21 07:31:50 +000054 globaldce, swapstructs, sortstructs,
Chris Lattner00950542001-06-06 20:29:01 +000055};
56
Chris Lattnerbd0ef772002-02-26 21:46:54 +000057static Pass *createPrintMethodPass() {
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000058 return new PrintMethodPass("Current Method: \n", &cerr);
59}
60
61// OptTable - Correlate enum Opts to Pass constructors...
62//
Chris Lattner8f367bd2001-07-23 02:35:57 +000063struct {
64 enum Opts OptID;
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000065 Pass * (*PassCtor)();
Chris Lattner8f367bd2001-07-23 02:35:57 +000066} OptTable[] = {
Chris Lattnerbd0ef772002-02-26 21:46:54 +000067 { dce , createDeadCodeEliminationPass },
Chris Lattner2b72c362002-03-14 22:36:15 +000068 { die , createDeadInstEliminationPass },
Chris Lattnerbd0ef772002-02-26 21:46:54 +000069 { constprop , createConstantPropogationPass },
70 { inlining , createMethodInliningPass },
71 { constmerge , createConstantMergePass },
72 { strip , createSymbolStrippingPass },
73 { mstrip , createFullSymbolStrippingPass },
74 { mergereturn, createUnifyMethodExitNodesPass },
Chris Lattner0be41012002-02-01 04:54:11 +000075
Chris Lattnerbd0ef772002-02-26 21:46:54 +000076 { indvars , createIndVarSimplifyPass },
77 { instcombine, createInstructionCombiningPass },
78 { sccp , createSCCPPass },
79 { adce , createAgressiveDCEPass },
80 { raise , createRaisePointerReferencesPass },
Chris Lattnerfe594542002-03-28 17:56:28 +000081 { mem2reg , createPromoteMemoryToRegister },
Chris Lattnereeeaf522002-02-12 17:17:33 +000082
Chris Lattnerbd0ef772002-02-26 21:46:54 +000083 { trace , createTraceValuesPassForBasicBlocks },
84 { tracem , createTraceValuesPassForMethod },
Chris Lattnereded4912002-02-26 20:04:59 +000085 { paths , createProfilePathsPass },
86
Chris Lattnerbd0ef772002-02-26 21:46:54 +000087 { print , createPrintMethodPass },
Chris Lattner22d26d72002-02-20 17:56:53 +000088 { verify , createVerifierPass },
Chris Lattnereded4912002-02-26 20:04:59 +000089
Chris Lattnerbd0ef772002-02-26 21:46:54 +000090 { raiseallocs, createRaiseAllocationsPass },
91 { cleangcc , createCleanupGCCOutputPass },
92 { globaldce , createGlobalDCEPass },
93 { swapstructs, createSwapElementsPass },
94 { sortstructs, createSortElementsPass },
Vikram S. Adved32e70a2002-03-24 03:19:54 +000095 { lowerrefs, createDecomposeMultiDimRefsPass }
Chris Lattner8f367bd2001-07-23 02:35:57 +000096};
97
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000098// Command line option handling code...
99//
Chris Lattnera8e1fd32001-07-23 20:22:30 +0000100cl::String InputFilename ("", "Load <arg> file to optimize", cl::NoFlags, "-");
101cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "");
102cl::Flag Force ("f", "Overwrite output files", cl::NoFlags, false);
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000103cl::Flag PrintEachXForm("p", "Print module after each transformation");
Chris Lattner8f367bd2001-07-23 02:35:57 +0000104cl::Flag Quiet ("q", "Don't print modifying pass names", 0, false);
Chris Lattnera8e1fd32001-07-23 20:22:30 +0000105cl::Alias QuietA ("quiet", "Alias for -q", cl::NoFlags, Quiet);
Chris Lattner8f367bd2001-07-23 02:35:57 +0000106cl::EnumList<enum Opts> OptimizationList(cl::NoFlags,
Chris Lattner528e8b52001-12-14 16:50:35 +0000107 clEnumVal(dce , "Dead Code Elimination"),
Chris Lattner2b72c362002-03-14 22:36:15 +0000108 clEnumVal(die , "Dead Instruction Elimination"),
Chris Lattner0be41012002-02-01 04:54:11 +0000109 clEnumVal(constprop , "Simple constant propogation"),
110 clEnumValN(inlining , "inline", "Method integration"),
Chris Lattner528e8b52001-12-14 16:50:35 +0000111 clEnumVal(constmerge , "Merge identical global constants"),
Chris Lattner0be41012002-02-01 04:54:11 +0000112 clEnumVal(strip , "Strip symbols"),
113 clEnumVal(mstrip , "Strip module symbols"),
114 clEnumVal(mergereturn, "Unify method exit nodes"),
115
Chris Lattner528e8b52001-12-14 16:50:35 +0000116 clEnumVal(indvars , "Simplify Induction Variables"),
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000117 clEnumVal(instcombine, "Combine redundant instructions"),
Chris Lattner528e8b52001-12-14 16:50:35 +0000118 clEnumVal(sccp , "Sparse Conditional Constant Propogation"),
119 clEnumVal(adce , "Agressive DCE"),
Chris Lattnereeeaf522002-02-12 17:17:33 +0000120 clEnumVal(mem2reg , "Promote alloca locations to registers"),
Chris Lattner854acb92001-11-10 07:16:10 +0000121
Chris Lattner528e8b52001-12-14 16:50:35 +0000122 clEnumVal(globaldce , "Remove unreachable globals"),
Chris Lattner63202322001-11-26 19:22:39 +0000123 clEnumVal(swapstructs, "Swap structure types around"),
Chris Lattnerf4de63f2002-01-21 07:31:50 +0000124 clEnumVal(sortstructs, "Sort structure elements"),
Chris Lattner63202322001-11-26 19:22:39 +0000125
Chris Lattner5048c3b2002-01-22 00:13:51 +0000126 clEnumVal(raiseallocs, "Raise allocations from calls to instructions"),
Chris Lattner528e8b52001-12-14 16:50:35 +0000127 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"),
Anand Shukladc9a1f52002-02-26 19:57:59 +0000131 clEnumVal(paths , "Insert path profiling instrumentation"),
Chris Lattner528e8b52001-12-14 16:50:35 +0000132 clEnumVal(print , "Print working method to stderr"),
Chris Lattner22d26d72002-02-20 17:56:53 +0000133 clEnumVal(verify , "Verify module is well formed"),
Vikram S. Adved32e70a2002-03-24 03:19:54 +0000134 clEnumVal(lowerrefs , "Decompose multi-dimensional structure/array refs to use one index per instruction"),
Chris Lattner8f367bd2001-07-23 02:35:57 +00001350);
136
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000137
Chris Lattner8f367bd2001-07-23 02:35:57 +0000138
Chris Lattner00950542001-06-06 20:29:01 +0000139int main(int argc, char **argv) {
Chris Lattner8f367bd2001-07-23 02:35:57 +0000140 cl::ParseCommandLineOptions(argc, argv,
141 " llvm .bc -> .bc modular optimizer\n");
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000142
143 // Load the input module...
Chris Lattner63202322001-11-26 19:22:39 +0000144 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
145 if (M.get() == 0) {
Chris Lattner00950542001-06-06 20:29:01 +0000146 cerr << "bytecode didn't read correctly.\n";
147 return 1;
148 }
149
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000150 // Figure out what stream we are supposed to write to...
Chris Lattner697954c2002-01-20 22:54:45 +0000151 std::ostream *Out = &std::cout; // Default to printing to stdout...
Chris Lattner1e78f362001-07-23 19:27:24 +0000152 if (OutputFilename != "") {
Chris Lattner888912d2002-01-22 21:07:24 +0000153 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner697954c2002-01-20 22:54:45 +0000154 // 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";
157 return 1;
158 }
159 Out = new std::ofstream(OutputFilename.c_str());
160
Chris Lattner00950542001-06-06 20:29:01 +0000161 if (!Out->good()) {
Chris Lattner1e78f362001-07-23 19:27:24 +0000162 cerr << "Error opening " << OutputFilename << "!\n";
Chris Lattner00950542001-06-06 20:29:01 +0000163 return 1;
164 }
165 }
166
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000167 // Create a PassManager to hold and optimize the collection of passes we are
168 // about to build...
169 //
170 PassManager Passes;
Chris Lattner00950542001-06-06 20:29:01 +0000171
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000172 // 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());
178 break;
179 }
180
181 if (PrintEachXForm)
182 Passes.add(new PrintModulePass(&std::cerr));
183 }
184
Chris Lattner22d26d72002-02-20 17:56:53 +0000185 // Check that the module is well formed on completion of optimization
186 Passes.add(createVerifierPass());
187
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000188 // Write bytecode out to disk or cout as the last step...
189 Passes.add(new WriteBytecodePass(Out, Out != &std::cout));
190
191 // Now that we have all of the passes ready, run them.
192 if (Passes.run(M.get()) && !Quiet)
193 cerr << "Program modified.\n";
194
Chris Lattner00950542001-06-06 20:29:01 +0000195 return 0;
196}