blob: ab2668daaebffe2baa9723bf921f950beed92f81 [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 Lattner0be41012002-02-01 04:54:11 +000014#include "llvm/Transforms/UnifyMethodExitNodes.h"
Chris Lattner9effd692001-10-18 20:06:45 +000015#include "llvm/Transforms/ConstantMerge.h"
Chris Lattnere166fe12001-10-31 04:29:44 +000016#include "llvm/Transforms/CleanupGCCOutput.h"
Chris Lattner068f4872001-11-01 02:41:09 +000017#include "llvm/Transforms/LevelChange.h"
Chris Lattner59b6b8e2002-01-21 23:17:48 +000018#include "llvm/Transforms/MethodInlining.h"
19#include "llvm/Transforms/SymbolStripping.h"
Chris Lattnerd7db8632002-01-22 01:04:08 +000020#include "llvm/Transforms/ChangeAllocations.h"
Chris Lattner04c85dc2002-01-21 07:52:35 +000021#include "llvm/Transforms/IPO/SimpleStructMutation.h"
Chris Lattner63202322001-11-26 19:22:39 +000022#include "llvm/Transforms/IPO/GlobalDCE.h"
Chris Lattner59b6b8e2002-01-21 23:17:48 +000023#include "llvm/Transforms/Scalar/DCE.h"
24#include "llvm/Transforms/Scalar/ConstantProp.h"
Chris Lattnerfe196cf2001-12-04 04:32:04 +000025#include "llvm/Transforms/Scalar/IndVarSimplify.h"
Chris Lattner528e8b52001-12-14 16:50:35 +000026#include "llvm/Transforms/Scalar/InstructionCombining.h"
Chris Lattner59b6b8e2002-01-21 23:17:48 +000027#include "llvm/Transforms/Instrumentation/TraceValues.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000028#include "Support/CommandLine.h"
Chris Lattner73e11d72001-10-18 06:13:08 +000029#include <fstream>
Chris Lattner63202322001-11-26 19:22:39 +000030#include <memory>
Chris Lattner95781b62001-06-30 06:38:31 +000031
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000032// Opts enum - All of the transformations we can do...
Chris Lattner8f367bd2001-07-23 02:35:57 +000033enum Opts {
34 // Basic optimizations
Chris Lattner0be41012002-02-01 04:54:11 +000035 dce, constprop, inlining, constmerge, strip, mstrip, mergereturn,
Chris Lattner8f367bd2001-07-23 02:35:57 +000036
Chris Lattner0eafc312001-10-18 06:05:15 +000037 // Miscellaneous Transformations
Chris Lattner5048c3b2002-01-22 00:13:51 +000038 trace, tracem, print, raiseallocs, cleangcc,
Chris Lattner0eafc312001-10-18 06:05:15 +000039
Chris Lattner8f367bd2001-07-23 02:35:57 +000040 // More powerful optimizations
Chris Lattner528e8b52001-12-14 16:50:35 +000041 indvars, instcombine, sccp, adce, raise,
Chris Lattner63202322001-11-26 19:22:39 +000042
43 // Interprocedural optimizations...
Chris Lattnerf4de63f2002-01-21 07:31:50 +000044 globaldce, swapstructs, sortstructs,
Chris Lattner00950542001-06-06 20:29:01 +000045};
46
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000047
48// New template functions - Provide functions that return passes of specified
49// types, with specified arguments...
50//
51template<class PassClass>
52Pass *New() {
53 return new PassClass();
54}
55
56template<class PassClass, typename ArgTy1, ArgTy1 Arg1>
57Pass *New() {
58 return new PassClass(Arg1);
59}
60
61template<class PassClass, typename ArgTy1, ArgTy1 Arg1,
62 typename ArgTy2, ArgTy1 Arg2>
63Pass *New() {
64 return new PassClass(Arg1, Arg2);
65}
66
67static Pass *NewPrintMethodPass() {
68 return new PrintMethodPass("Current Method: \n", &cerr);
69}
70
71// OptTable - Correlate enum Opts to Pass constructors...
72//
Chris Lattner8f367bd2001-07-23 02:35:57 +000073struct {
74 enum Opts OptID;
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000075 Pass * (*PassCtor)();
Chris Lattner8f367bd2001-07-23 02:35:57 +000076} OptTable[] = {
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000077 { dce , New<DeadCodeElimination> },
78 { constprop , New<ConstantPropogation> },
79 { inlining , New<MethodInlining> },
80 { constmerge , New<ConstantMerge> },
81 { strip , New<SymbolStripping> },
82 { mstrip , New<FullSymbolStripping> },
Chris Lattner0be41012002-02-01 04:54:11 +000083 { mergereturn, New<UnifyMethodExitNodes> },
84
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000085 { indvars , New<InductionVariableSimplify> },
86 { instcombine, New<InstructionCombining> },
87 { sccp , New<SCCPPass> },
88 { adce , New<AgressiveDCE> },
89 { raise , New<RaisePointerReferences> },
90 { trace , New<InsertTraceCode, bool, true, bool, true> },
91 { tracem , New<InsertTraceCode, bool, false, bool, true> },
92 { print , NewPrintMethodPass },
93 { raiseallocs, New<RaiseAllocations> },
94 { cleangcc , New<CleanupGCCOutput> },
95 { globaldce , New<GlobalDCE> },
96 { swapstructs, New<SimpleStructMutation, SimpleStructMutation::Transform,
97 SimpleStructMutation::SwapElements>},
98 { sortstructs, New<SimpleStructMutation, SimpleStructMutation::Transform,
99 SimpleStructMutation::SortElements>},
Chris Lattner8f367bd2001-07-23 02:35:57 +0000100};
101
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000102// Command line option handling code...
103//
Chris Lattnera8e1fd32001-07-23 20:22:30 +0000104cl::String InputFilename ("", "Load <arg> file to optimize", cl::NoFlags, "-");
105cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "");
106cl::Flag Force ("f", "Overwrite output files", cl::NoFlags, false);
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000107cl::Flag PrintEachXForm("p", "Print module after each transformation");
Chris Lattner8f367bd2001-07-23 02:35:57 +0000108cl::Flag Quiet ("q", "Don't print modifying pass names", 0, false);
Chris Lattnera8e1fd32001-07-23 20:22:30 +0000109cl::Alias QuietA ("quiet", "Alias for -q", cl::NoFlags, Quiet);
Chris Lattner8f367bd2001-07-23 02:35:57 +0000110cl::EnumList<enum Opts> OptimizationList(cl::NoFlags,
Chris Lattner528e8b52001-12-14 16:50:35 +0000111 clEnumVal(dce , "Dead Code Elimination"),
Chris Lattner0be41012002-02-01 04:54:11 +0000112 clEnumVal(constprop , "Simple constant propogation"),
113 clEnumValN(inlining , "inline", "Method integration"),
Chris Lattner528e8b52001-12-14 16:50:35 +0000114 clEnumVal(constmerge , "Merge identical global constants"),
Chris Lattner0be41012002-02-01 04:54:11 +0000115 clEnumVal(strip , "Strip symbols"),
116 clEnumVal(mstrip , "Strip module symbols"),
117 clEnumVal(mergereturn, "Unify method exit nodes"),
118
Chris Lattner528e8b52001-12-14 16:50:35 +0000119 clEnumVal(indvars , "Simplify Induction Variables"),
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000120 clEnumVal(instcombine, "Combine redundant instructions"),
Chris Lattner528e8b52001-12-14 16:50:35 +0000121 clEnumVal(sccp , "Sparse Conditional Constant Propogation"),
122 clEnumVal(adce , "Agressive DCE"),
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 Lattner63202322001-11-26 19:22:39 +0000127
Chris Lattner5048c3b2002-01-22 00:13:51 +0000128 clEnumVal(raiseallocs, "Raise allocations from calls to instructions"),
Chris Lattner528e8b52001-12-14 16:50:35 +0000129 clEnumVal(cleangcc , "Cleanup GCC Output"),
130 clEnumVal(raise , "Raise to Higher Level"),
131 clEnumVal(trace , "Insert BB & Method trace code"),
132 clEnumVal(tracem , "Insert Method trace code only"),
133 clEnumVal(print , "Print working method to stderr"),
Chris Lattner8f367bd2001-07-23 02:35:57 +00001340);
135
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000136
Chris Lattner8f367bd2001-07-23 02:35:57 +0000137
Chris Lattner00950542001-06-06 20:29:01 +0000138int main(int argc, char **argv) {
Chris Lattner8f367bd2001-07-23 02:35:57 +0000139 cl::ParseCommandLineOptions(argc, argv,
140 " llvm .bc -> .bc modular optimizer\n");
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000141
142 // Load the input module...
Chris Lattner63202322001-11-26 19:22:39 +0000143 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
144 if (M.get() == 0) {
Chris Lattner00950542001-06-06 20:29:01 +0000145 cerr << "bytecode didn't read correctly.\n";
146 return 1;
147 }
148
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000149 // Figure out what stream we are supposed to write to...
Chris Lattner697954c2002-01-20 22:54:45 +0000150 std::ostream *Out = &std::cout; // Default to printing to stdout...
Chris Lattner1e78f362001-07-23 19:27:24 +0000151 if (OutputFilename != "") {
Chris Lattner888912d2002-01-22 21:07:24 +0000152 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner697954c2002-01-20 22:54:45 +0000153 // If force is not specified, make sure not to overwrite a file!
154 cerr << "Error opening '" << OutputFilename << "': File exists!\n"
155 << "Use -f command line argument to force output\n";
156 return 1;
157 }
158 Out = new std::ofstream(OutputFilename.c_str());
159
Chris Lattner00950542001-06-06 20:29:01 +0000160 if (!Out->good()) {
Chris Lattner1e78f362001-07-23 19:27:24 +0000161 cerr << "Error opening " << OutputFilename << "!\n";
Chris Lattner00950542001-06-06 20:29:01 +0000162 return 1;
163 }
164 }
165
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000166 // Create a PassManager to hold and optimize the collection of passes we are
167 // about to build...
168 //
169 PassManager Passes;
Chris Lattner00950542001-06-06 20:29:01 +0000170
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000171 // Create a new optimization pass for each one specified on the command line
172 for (unsigned i = 0; i < OptimizationList.size(); ++i) {
173 enum Opts Opt = OptimizationList[i];
174 for (unsigned j = 0; j < sizeof(OptTable)/sizeof(OptTable[0]); ++j)
175 if (Opt == OptTable[j].OptID) {
176 Passes.add(OptTable[j].PassCtor());
177 break;
178 }
179
180 if (PrintEachXForm)
181 Passes.add(new PrintModulePass(&std::cerr));
182 }
183
184 // Write bytecode out to disk or cout as the last step...
185 Passes.add(new WriteBytecodePass(Out, Out != &std::cout));
186
187 // Now that we have all of the passes ready, run them.
188 if (Passes.run(M.get()) && !Quiet)
189 cerr << "Program modified.\n";
190
Chris Lattner00950542001-06-06 20:29:01 +0000191 return 0;
192}