blob: c4870d833db8baac71df15b5d091446f8cadc495 [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 Lattner483e14e2002-04-27 07:27:19 +000015#include "llvm/Transforms/UnifyFunctionExitNodes.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 Lattner483e14e2002-04-27 07:27:19 +000019#include "llvm/Transforms/FunctionInlining.h"
Chris Lattner59b6b8e2002-01-21 23:17:48 +000020#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 Lattner328207c2002-03-28 18:08:07 +000024#include "llvm/Transforms/IPO/PoolAllocate.h"
Chris Lattner59b6b8e2002-01-21 23:17:48 +000025#include "llvm/Transforms/Scalar/DCE.h"
26#include "llvm/Transforms/Scalar/ConstantProp.h"
Chris Lattnere5fb83e2002-04-28 00:48:17 +000027#include "llvm/Transforms/Scalar/GCSE.h"
Chris Lattnerfe196cf2001-12-04 04:32:04 +000028#include "llvm/Transforms/Scalar/IndVarSimplify.h"
Chris Lattner528e8b52001-12-14 16:50:35 +000029#include "llvm/Transforms/Scalar/InstructionCombining.h"
Chris Lattnereeeaf522002-02-12 17:17:33 +000030#include "llvm/Transforms/Scalar/PromoteMemoryToRegister.h"
Vikram S. Adved32e70a2002-03-24 03:19:54 +000031#include "llvm/Transforms/Scalar/DecomposeMultiDimRefs.h"
Chris Lattner59b6b8e2002-01-21 23:17:48 +000032#include "llvm/Transforms/Instrumentation/TraceValues.h"
Anand Shukladc9a1f52002-02-26 19:57:59 +000033#include "llvm/Transforms/Instrumentation/ProfilePaths.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000034#include "Support/CommandLine.h"
Chris Lattner76d12292002-04-18 19:55:25 +000035#include "Support/Signals.h"
Chris Lattner73e11d72001-10-18 06:13:08 +000036#include <fstream>
Chris Lattner63202322001-11-26 19:22:39 +000037#include <memory>
Chris Lattner95781b62001-06-30 06:38:31 +000038
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000039// Opts enum - All of the transformations we can do...
Chris Lattner8f367bd2001-07-23 02:35:57 +000040enum Opts {
41 // Basic optimizations
Chris Lattnere5fb83e2002-04-28 00:48:17 +000042 dce, die, constprop, gcse, inlining, constmerge, strip, mstrip, mergereturn,
Chris Lattner8f367bd2001-07-23 02:35:57 +000043
Chris Lattner0eafc312001-10-18 06:05:15 +000044 // Miscellaneous Transformations
Chris Lattner9d6e7eb2002-04-12 18:21:13 +000045 raiseallocs, funcresolve, cleangcc, lowerrefs,
Chris Lattner22d26d72002-02-20 17:56:53 +000046
47 // Printing and verifying...
Chris Lattner30af3682002-04-13 18:32:47 +000048 print, printm, verify,
Chris Lattner0eafc312001-10-18 06:05:15 +000049
Chris Lattner8f367bd2001-07-23 02:35:57 +000050 // More powerful optimizations
Chris Lattnereeeaf522002-02-12 17:17:33 +000051 indvars, instcombine, sccp, adce, raise, mem2reg,
Chris Lattner63202322001-11-26 19:22:39 +000052
Anand Shukladc9a1f52002-02-26 19:57:59 +000053 // Instrumentation
54 trace, tracem, paths,
55
Chris Lattner63202322001-11-26 19:22:39 +000056 // Interprocedural optimizations...
Chris Lattner328207c2002-03-28 18:08:07 +000057 globaldce, swapstructs, sortstructs, poolalloc,
Chris Lattner00950542001-06-06 20:29:01 +000058};
59
Chris Lattner30af3682002-04-13 18:32:47 +000060static Pass *createPrintFunctionPass() {
61 return new PrintFunctionPass("Current Function: \n", &cerr);
62}
63
64static Pass *createPrintModulePass() {
65 return new PrintModulePass(&cerr);
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000066}
67
68// OptTable - Correlate enum Opts to Pass constructors...
69//
Chris Lattner8f367bd2001-07-23 02:35:57 +000070struct {
71 enum Opts OptID;
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000072 Pass * (*PassCtor)();
Chris Lattner8f367bd2001-07-23 02:35:57 +000073} OptTable[] = {
Chris Lattnerf57b8452002-04-27 06:56:12 +000074 { dce , createDeadCodeEliminationPass },
75 { die , createDeadInstEliminationPass },
76 { constprop , createConstantPropogationPass },
Chris Lattnere5fb83e2002-04-28 00:48:17 +000077 { gcse , createGCSEPass },
Chris Lattnerf57b8452002-04-27 06:56:12 +000078 { inlining , createFunctionInliningPass },
79 { constmerge , createConstantMergePass },
80 { strip , createSymbolStrippingPass },
81 { mstrip , createFullSymbolStrippingPass },
Chris Lattner483e14e2002-04-27 07:27:19 +000082 { mergereturn, createUnifyFunctionExitNodesPass },
Chris Lattner0be41012002-02-01 04:54:11 +000083
Chris Lattnerf57b8452002-04-27 06:56:12 +000084 { indvars , createIndVarSimplifyPass },
85 { instcombine, createInstructionCombiningPass },
86 { sccp , createSCCPPass },
87 { adce , createAgressiveDCEPass },
Chris Lattnerbd0ef772002-02-26 21:46:54 +000088 { raise , createRaisePointerReferencesPass },
Chris Lattnerf57b8452002-04-27 06:56:12 +000089 { mem2reg , createPromoteMemoryToRegister },
90 { lowerrefs, createDecomposeMultiDimRefsPass },
Chris Lattnereeeaf522002-02-12 17:17:33 +000091
Chris Lattnerbd0ef772002-02-26 21:46:54 +000092 { trace , createTraceValuesPassForBasicBlocks },
Chris Lattnerf57b8452002-04-27 06:56:12 +000093 { tracem , createTraceValuesPassForFunction },
94 { paths , createProfilePathsPass },
Chris Lattnereded4912002-02-26 20:04:59 +000095
Chris Lattner30af3682002-04-13 18:32:47 +000096 { print , createPrintFunctionPass },
Chris Lattnerf57b8452002-04-27 06:56:12 +000097 { printm , createPrintModulePass },
98 { verify , createVerifierPass },
Chris Lattnereded4912002-02-26 20:04:59 +000099
Chris Lattnerf57b8452002-04-27 06:56:12 +0000100 { raiseallocs, createRaiseAllocationsPass },
101 { cleangcc , createCleanupGCCOutputPass },
Chris Lattner9d6e7eb2002-04-12 18:21:13 +0000102 { funcresolve, createFunctionResolvingPass },
Chris Lattnerf57b8452002-04-27 06:56:12 +0000103 { globaldce , createGlobalDCEPass },
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000104 { swapstructs, createSwapElementsPass },
105 { sortstructs, createSortElementsPass },
Chris Lattner328207c2002-03-28 18:08:07 +0000106 { poolalloc , createPoolAllocatePass },
Chris Lattner8f367bd2001-07-23 02:35:57 +0000107};
108
Chris Lattner9d6e7eb2002-04-12 18:21:13 +0000109
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000110// Command line option handling code...
111//
Chris Lattnera8e1fd32001-07-23 20:22:30 +0000112cl::String InputFilename ("", "Load <arg> file to optimize", cl::NoFlags, "-");
113cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "");
114cl::Flag Force ("f", "Overwrite output files", cl::NoFlags, false);
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000115cl::Flag PrintEachXForm("p", "Print module after each transformation");
Chris Lattner8f367bd2001-07-23 02:35:57 +0000116cl::Flag Quiet ("q", "Don't print modifying pass names", 0, false);
Chris Lattnera8e1fd32001-07-23 20:22:30 +0000117cl::Alias QuietA ("quiet", "Alias for -q", cl::NoFlags, Quiet);
Chris Lattner8f367bd2001-07-23 02:35:57 +0000118cl::EnumList<enum Opts> OptimizationList(cl::NoFlags,
Chris Lattner528e8b52001-12-14 16:50:35 +0000119 clEnumVal(dce , "Dead Code Elimination"),
Chris Lattner2b72c362002-03-14 22:36:15 +0000120 clEnumVal(die , "Dead Instruction Elimination"),
Chris Lattner0be41012002-02-01 04:54:11 +0000121 clEnumVal(constprop , "Simple constant propogation"),
Chris Lattnere5fb83e2002-04-28 00:48:17 +0000122 clEnumVal(gcse , "Global Common Subexpression Elimination"),
Chris Lattner30af3682002-04-13 18:32:47 +0000123 clEnumValN(inlining , "inline", "Function integration"),
Chris Lattner528e8b52001-12-14 16:50:35 +0000124 clEnumVal(constmerge , "Merge identical global constants"),
Chris Lattner0be41012002-02-01 04:54:11 +0000125 clEnumVal(strip , "Strip symbols"),
126 clEnumVal(mstrip , "Strip module symbols"),
Chris Lattner30af3682002-04-13 18:32:47 +0000127 clEnumVal(mergereturn, "Unify function exit nodes"),
Chris Lattner0be41012002-02-01 04:54:11 +0000128
Chris Lattner528e8b52001-12-14 16:50:35 +0000129 clEnumVal(indvars , "Simplify Induction Variables"),
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000130 clEnumVal(instcombine, "Combine redundant instructions"),
Chris Lattner528e8b52001-12-14 16:50:35 +0000131 clEnumVal(sccp , "Sparse Conditional Constant Propogation"),
132 clEnumVal(adce , "Agressive DCE"),
Chris Lattnereeeaf522002-02-12 17:17:33 +0000133 clEnumVal(mem2reg , "Promote alloca locations to registers"),
Chris Lattner854acb92001-11-10 07:16:10 +0000134
Chris Lattner528e8b52001-12-14 16:50:35 +0000135 clEnumVal(globaldce , "Remove unreachable globals"),
Chris Lattner63202322001-11-26 19:22:39 +0000136 clEnumVal(swapstructs, "Swap structure types around"),
Chris Lattnerf4de63f2002-01-21 07:31:50 +0000137 clEnumVal(sortstructs, "Sort structure elements"),
Chris Lattner328207c2002-03-28 18:08:07 +0000138 clEnumVal(poolalloc , "Pool allocate disjoint datastructures"),
Chris Lattner63202322001-11-26 19:22:39 +0000139
Chris Lattner5048c3b2002-01-22 00:13:51 +0000140 clEnumVal(raiseallocs, "Raise allocations from calls to instructions"),
Chris Lattner528e8b52001-12-14 16:50:35 +0000141 clEnumVal(cleangcc , "Cleanup GCC Output"),
Chris Lattner9d6e7eb2002-04-12 18:21:13 +0000142 clEnumVal(funcresolve, "Resolve calls to foo(...) to foo(<concrete types>)"),
Chris Lattner528e8b52001-12-14 16:50:35 +0000143 clEnumVal(raise , "Raise to Higher Level"),
Chris Lattner30af3682002-04-13 18:32:47 +0000144 clEnumVal(trace , "Insert BB and Function trace code"),
145 clEnumVal(tracem , "Insert Function trace code only"),
Anand Shukladc9a1f52002-02-26 19:57:59 +0000146 clEnumVal(paths , "Insert path profiling instrumentation"),
Chris Lattner30af3682002-04-13 18:32:47 +0000147 clEnumVal(print , "Print working function to stderr"),
148 clEnumVal(printm , "Print working module to stderr"),
Chris Lattner22d26d72002-02-20 17:56:53 +0000149 clEnumVal(verify , "Verify module is well formed"),
Vikram S. Adved32e70a2002-03-24 03:19:54 +0000150 clEnumVal(lowerrefs , "Decompose multi-dimensional structure/array refs to use one index per instruction"),
Chris Lattner8f367bd2001-07-23 02:35:57 +00001510);
152
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000153
Chris Lattner8f367bd2001-07-23 02:35:57 +0000154
Chris Lattner00950542001-06-06 20:29:01 +0000155int main(int argc, char **argv) {
Chris Lattner8f367bd2001-07-23 02:35:57 +0000156 cl::ParseCommandLineOptions(argc, argv,
157 " llvm .bc -> .bc modular optimizer\n");
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000158
159 // Load the input module...
Chris Lattner63202322001-11-26 19:22:39 +0000160 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
161 if (M.get() == 0) {
Chris Lattner00950542001-06-06 20:29:01 +0000162 cerr << "bytecode didn't read correctly.\n";
163 return 1;
164 }
165
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000166 // Figure out what stream we are supposed to write to...
Chris Lattner697954c2002-01-20 22:54:45 +0000167 std::ostream *Out = &std::cout; // Default to printing to stdout...
Chris Lattner1e78f362001-07-23 19:27:24 +0000168 if (OutputFilename != "") {
Chris Lattner888912d2002-01-22 21:07:24 +0000169 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner697954c2002-01-20 22:54:45 +0000170 // If force is not specified, make sure not to overwrite a file!
171 cerr << "Error opening '" << OutputFilename << "': File exists!\n"
172 << "Use -f command line argument to force output\n";
173 return 1;
174 }
175 Out = new std::ofstream(OutputFilename.c_str());
176
Chris Lattner00950542001-06-06 20:29:01 +0000177 if (!Out->good()) {
Chris Lattner1e78f362001-07-23 19:27:24 +0000178 cerr << "Error opening " << OutputFilename << "!\n";
Chris Lattner00950542001-06-06 20:29:01 +0000179 return 1;
180 }
Chris Lattner76d12292002-04-18 19:55:25 +0000181
182 // Make sure that the Output file gets unlink'd from the disk if we get a
183 // SIGINT
184 RemoveFileOnSignal(OutputFilename);
Chris Lattner00950542001-06-06 20:29:01 +0000185 }
186
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000187 // Create a PassManager to hold and optimize the collection of passes we are
188 // about to build...
189 //
190 PassManager Passes;
Chris Lattner00950542001-06-06 20:29:01 +0000191
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000192 // Create a new optimization pass for each one specified on the command line
193 for (unsigned i = 0; i < OptimizationList.size(); ++i) {
194 enum Opts Opt = OptimizationList[i];
195 for (unsigned j = 0; j < sizeof(OptTable)/sizeof(OptTable[0]); ++j)
196 if (Opt == OptTable[j].OptID) {
197 Passes.add(OptTable[j].PassCtor());
198 break;
199 }
200
201 if (PrintEachXForm)
202 Passes.add(new PrintModulePass(&std::cerr));
203 }
204
Chris Lattner22d26d72002-02-20 17:56:53 +0000205 // Check that the module is well formed on completion of optimization
206 Passes.add(createVerifierPass());
207
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000208 // Write bytecode out to disk or cout as the last step...
209 Passes.add(new WriteBytecodePass(Out, Out != &std::cout));
210
211 // Now that we have all of the passes ready, run them.
212 if (Passes.run(M.get()) && !Quiet)
213 cerr << "Program modified.\n";
214
Chris Lattner00950542001-06-06 20:29:01 +0000215 return 0;
216}