blob: ce60d9453e40174e8bd3c97d06ad3c4703efe1ea [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 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 Lattnerfe196cf2001-12-04 04:32:04 +000027#include "llvm/Transforms/Scalar/IndVarSimplify.h"
Chris Lattner528e8b52001-12-14 16:50:35 +000028#include "llvm/Transforms/Scalar/InstructionCombining.h"
Chris Lattnereeeaf522002-02-12 17:17:33 +000029#include "llvm/Transforms/Scalar/PromoteMemoryToRegister.h"
Vikram S. Adved32e70a2002-03-24 03:19:54 +000030#include "llvm/Transforms/Scalar/DecomposeMultiDimRefs.h"
Chris Lattner59b6b8e2002-01-21 23:17:48 +000031#include "llvm/Transforms/Instrumentation/TraceValues.h"
Anand Shukladc9a1f52002-02-26 19:57:59 +000032#include "llvm/Transforms/Instrumentation/ProfilePaths.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000033#include "Support/CommandLine.h"
Chris Lattner73e11d72001-10-18 06:13:08 +000034#include <fstream>
Chris Lattner63202322001-11-26 19:22:39 +000035#include <memory>
Chris Lattner95781b62001-06-30 06:38:31 +000036
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000037// Opts enum - All of the transformations we can do...
Chris Lattner8f367bd2001-07-23 02:35:57 +000038enum Opts {
39 // Basic optimizations
Chris Lattner2b72c362002-03-14 22:36:15 +000040 dce, die, constprop, inlining, constmerge, strip, mstrip, mergereturn,
Chris Lattner8f367bd2001-07-23 02:35:57 +000041
Chris Lattner0eafc312001-10-18 06:05:15 +000042 // Miscellaneous Transformations
Vikram S. Adved32e70a2002-03-24 03:19:54 +000043 raiseallocs, cleangcc, lowerrefs,
Chris Lattner22d26d72002-02-20 17:56:53 +000044
45 // Printing and verifying...
46 print, verify,
Chris Lattner0eafc312001-10-18 06:05:15 +000047
Chris Lattner8f367bd2001-07-23 02:35:57 +000048 // More powerful optimizations
Chris Lattnereeeaf522002-02-12 17:17:33 +000049 indvars, instcombine, sccp, adce, raise, mem2reg,
Chris Lattner63202322001-11-26 19:22:39 +000050
Anand Shukladc9a1f52002-02-26 19:57:59 +000051 // Instrumentation
52 trace, tracem, paths,
53
Chris Lattner63202322001-11-26 19:22:39 +000054 // Interprocedural optimizations...
Chris Lattner328207c2002-03-28 18:08:07 +000055 globaldce, swapstructs, sortstructs, poolalloc,
Chris Lattner00950542001-06-06 20:29:01 +000056};
57
Chris Lattnerbd0ef772002-02-26 21:46:54 +000058static Pass *createPrintMethodPass() {
Chris Lattner02d6ef82002-04-08 22:05:01 +000059 return new PrintFunctionPass("Current Method: \n", &cerr);
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000060}
61
62// OptTable - Correlate enum Opts to Pass constructors...
63//
Chris Lattner8f367bd2001-07-23 02:35:57 +000064struct {
65 enum Opts OptID;
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000066 Pass * (*PassCtor)();
Chris Lattner8f367bd2001-07-23 02:35:57 +000067} OptTable[] = {
Chris Lattnerbd0ef772002-02-26 21:46:54 +000068 { dce , createDeadCodeEliminationPass },
Chris Lattner2b72c362002-03-14 22:36:15 +000069 { die , createDeadInstEliminationPass },
Chris Lattnerbd0ef772002-02-26 21:46:54 +000070 { constprop , createConstantPropogationPass },
71 { inlining , createMethodInliningPass },
72 { constmerge , createConstantMergePass },
73 { strip , createSymbolStrippingPass },
74 { mstrip , createFullSymbolStrippingPass },
75 { mergereturn, createUnifyMethodExitNodesPass },
Chris Lattner0be41012002-02-01 04:54:11 +000076
Chris Lattnerbd0ef772002-02-26 21:46:54 +000077 { indvars , createIndVarSimplifyPass },
78 { instcombine, createInstructionCombiningPass },
79 { sccp , createSCCPPass },
80 { adce , createAgressiveDCEPass },
81 { raise , createRaisePointerReferencesPass },
Chris Lattnerfe594542002-03-28 17:56:28 +000082 { mem2reg , createPromoteMemoryToRegister },
Chris Lattner328207c2002-03-28 18:08:07 +000083 { lowerrefs, createDecomposeMultiDimRefsPass },
Chris Lattnereeeaf522002-02-12 17:17:33 +000084
Chris Lattnerbd0ef772002-02-26 21:46:54 +000085 { trace , createTraceValuesPassForBasicBlocks },
86 { tracem , createTraceValuesPassForMethod },
Chris Lattnereded4912002-02-26 20:04:59 +000087 { paths , createProfilePathsPass },
88
Chris Lattnerbd0ef772002-02-26 21:46:54 +000089 { print , createPrintMethodPass },
Chris Lattner22d26d72002-02-20 17:56:53 +000090 { verify , createVerifierPass },
Chris Lattnereded4912002-02-26 20:04:59 +000091
Chris Lattnerbd0ef772002-02-26 21:46:54 +000092 { raiseallocs, createRaiseAllocationsPass },
93 { cleangcc , createCleanupGCCOutputPass },
94 { globaldce , createGlobalDCEPass },
95 { swapstructs, createSwapElementsPass },
96 { sortstructs, createSortElementsPass },
Chris Lattner328207c2002-03-28 18:08:07 +000097 { poolalloc , createPoolAllocatePass },
Chris Lattner8f367bd2001-07-23 02:35:57 +000098};
99
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000100// Command line option handling code...
101//
Chris Lattnera8e1fd32001-07-23 20:22:30 +0000102cl::String InputFilename ("", "Load <arg> file to optimize", cl::NoFlags, "-");
103cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "");
104cl::Flag Force ("f", "Overwrite output files", cl::NoFlags, false);
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000105cl::Flag PrintEachXForm("p", "Print module after each transformation");
Chris Lattner8f367bd2001-07-23 02:35:57 +0000106cl::Flag Quiet ("q", "Don't print modifying pass names", 0, false);
Chris Lattnera8e1fd32001-07-23 20:22:30 +0000107cl::Alias QuietA ("quiet", "Alias for -q", cl::NoFlags, Quiet);
Chris Lattner8f367bd2001-07-23 02:35:57 +0000108cl::EnumList<enum Opts> OptimizationList(cl::NoFlags,
Chris Lattner528e8b52001-12-14 16:50:35 +0000109 clEnumVal(dce , "Dead Code Elimination"),
Chris Lattner2b72c362002-03-14 22:36:15 +0000110 clEnumVal(die , "Dead Instruction Elimination"),
Chris Lattner0be41012002-02-01 04:54:11 +0000111 clEnumVal(constprop , "Simple constant propogation"),
112 clEnumValN(inlining , "inline", "Method integration"),
Chris Lattner528e8b52001-12-14 16:50:35 +0000113 clEnumVal(constmerge , "Merge identical global constants"),
Chris Lattner0be41012002-02-01 04:54:11 +0000114 clEnumVal(strip , "Strip symbols"),
115 clEnumVal(mstrip , "Strip module symbols"),
116 clEnumVal(mergereturn, "Unify method exit nodes"),
117
Chris Lattner528e8b52001-12-14 16:50:35 +0000118 clEnumVal(indvars , "Simplify Induction Variables"),
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000119 clEnumVal(instcombine, "Combine redundant instructions"),
Chris Lattner528e8b52001-12-14 16:50:35 +0000120 clEnumVal(sccp , "Sparse Conditional Constant Propogation"),
121 clEnumVal(adce , "Agressive DCE"),
Chris Lattnereeeaf522002-02-12 17:17:33 +0000122 clEnumVal(mem2reg , "Promote alloca locations to registers"),
Chris Lattner854acb92001-11-10 07:16:10 +0000123
Chris Lattner528e8b52001-12-14 16:50:35 +0000124 clEnumVal(globaldce , "Remove unreachable globals"),
Chris Lattner63202322001-11-26 19:22:39 +0000125 clEnumVal(swapstructs, "Swap structure types around"),
Chris Lattnerf4de63f2002-01-21 07:31:50 +0000126 clEnumVal(sortstructs, "Sort structure elements"),
Chris Lattner328207c2002-03-28 18:08:07 +0000127 clEnumVal(poolalloc , "Pool allocate disjoint datastructures"),
Chris Lattner63202322001-11-26 19:22:39 +0000128
Chris Lattner5048c3b2002-01-22 00:13:51 +0000129 clEnumVal(raiseallocs, "Raise allocations from calls to instructions"),
Chris Lattner528e8b52001-12-14 16:50:35 +0000130 clEnumVal(cleangcc , "Cleanup GCC Output"),
131 clEnumVal(raise , "Raise to Higher Level"),
132 clEnumVal(trace , "Insert BB & Method trace code"),
133 clEnumVal(tracem , "Insert Method trace code only"),
Anand Shukladc9a1f52002-02-26 19:57:59 +0000134 clEnumVal(paths , "Insert path profiling instrumentation"),
Chris Lattner528e8b52001-12-14 16:50:35 +0000135 clEnumVal(print , "Print working method to stderr"),
Chris Lattner22d26d72002-02-20 17:56:53 +0000136 clEnumVal(verify , "Verify module is well formed"),
Vikram S. Adved32e70a2002-03-24 03:19:54 +0000137 clEnumVal(lowerrefs , "Decompose multi-dimensional structure/array refs to use one index per instruction"),
Chris Lattner8f367bd2001-07-23 02:35:57 +00001380);
139
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000140
Chris Lattner8f367bd2001-07-23 02:35:57 +0000141
Chris Lattner00950542001-06-06 20:29:01 +0000142int main(int argc, char **argv) {
Chris Lattner8f367bd2001-07-23 02:35:57 +0000143 cl::ParseCommandLineOptions(argc, argv,
144 " llvm .bc -> .bc modular optimizer\n");
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000145
146 // Load the input module...
Chris Lattner63202322001-11-26 19:22:39 +0000147 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
148 if (M.get() == 0) {
Chris Lattner00950542001-06-06 20:29:01 +0000149 cerr << "bytecode didn't read correctly.\n";
150 return 1;
151 }
152
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000153 // Figure out what stream we are supposed to write to...
Chris Lattner697954c2002-01-20 22:54:45 +0000154 std::ostream *Out = &std::cout; // Default to printing to stdout...
Chris Lattner1e78f362001-07-23 19:27:24 +0000155 if (OutputFilename != "") {
Chris Lattner888912d2002-01-22 21:07:24 +0000156 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner697954c2002-01-20 22:54:45 +0000157 // If force is not specified, make sure not to overwrite a file!
158 cerr << "Error opening '" << OutputFilename << "': File exists!\n"
159 << "Use -f command line argument to force output\n";
160 return 1;
161 }
162 Out = new std::ofstream(OutputFilename.c_str());
163
Chris Lattner00950542001-06-06 20:29:01 +0000164 if (!Out->good()) {
Chris Lattner1e78f362001-07-23 19:27:24 +0000165 cerr << "Error opening " << OutputFilename << "!\n";
Chris Lattner00950542001-06-06 20:29:01 +0000166 return 1;
167 }
168 }
169
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000170 // Create a PassManager to hold and optimize the collection of passes we are
171 // about to build...
172 //
173 PassManager Passes;
Chris Lattner00950542001-06-06 20:29:01 +0000174
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000175 // Create a new optimization pass for each one specified on the command line
176 for (unsigned i = 0; i < OptimizationList.size(); ++i) {
177 enum Opts Opt = OptimizationList[i];
178 for (unsigned j = 0; j < sizeof(OptTable)/sizeof(OptTable[0]); ++j)
179 if (Opt == OptTable[j].OptID) {
180 Passes.add(OptTable[j].PassCtor());
181 break;
182 }
183
184 if (PrintEachXForm)
185 Passes.add(new PrintModulePass(&std::cerr));
186 }
187
Chris Lattner22d26d72002-02-20 17:56:53 +0000188 // Check that the module is well formed on completion of optimization
189 Passes.add(createVerifierPass());
190
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000191 // Write bytecode out to disk or cout as the last step...
192 Passes.add(new WriteBytecodePass(Out, Out != &std::cout));
193
194 // Now that we have all of the passes ready, run them.
195 if (Passes.run(M.get()) && !Quiet)
196 cerr << "Program modified.\n";
197
Chris Lattner00950542001-06-06 20:29:01 +0000198 return 0;
199}