blob: a94ff76b33eca0af035bbfee6478a609bb616d90 [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 Lattner9effd692001-10-18 20:06:45 +000014#include "llvm/Transforms/ConstantMerge.h"
Chris Lattnere166fe12001-10-31 04:29:44 +000015#include "llvm/Transforms/CleanupGCCOutput.h"
Chris Lattner068f4872001-11-01 02:41:09 +000016#include "llvm/Transforms/LevelChange.h"
Chris Lattner59b6b8e2002-01-21 23:17:48 +000017#include "llvm/Transforms/MethodInlining.h"
18#include "llvm/Transforms/SymbolStripping.h"
Chris Lattnerd7db8632002-01-22 01:04:08 +000019#include "llvm/Transforms/ChangeAllocations.h"
Chris Lattner04c85dc2002-01-21 07:52:35 +000020#include "llvm/Transforms/IPO/SimpleStructMutation.h"
Chris Lattner63202322001-11-26 19:22:39 +000021#include "llvm/Transforms/IPO/GlobalDCE.h"
Chris Lattner59b6b8e2002-01-21 23:17:48 +000022#include "llvm/Transforms/Scalar/DCE.h"
23#include "llvm/Transforms/Scalar/ConstantProp.h"
Chris Lattnerfe196cf2001-12-04 04:32:04 +000024#include "llvm/Transforms/Scalar/IndVarSimplify.h"
Chris Lattner528e8b52001-12-14 16:50:35 +000025#include "llvm/Transforms/Scalar/InstructionCombining.h"
Chris Lattner59b6b8e2002-01-21 23:17:48 +000026#include "llvm/Transforms/Instrumentation/TraceValues.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000027#include "Support/CommandLine.h"
Chris Lattner73e11d72001-10-18 06:13:08 +000028#include <fstream>
Chris Lattner63202322001-11-26 19:22:39 +000029#include <memory>
Chris Lattner95781b62001-06-30 06:38:31 +000030
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000031// Opts enum - All of the transformations we can do...
Chris Lattner8f367bd2001-07-23 02:35:57 +000032enum Opts {
33 // Basic optimizations
Chris Lattner528e8b52001-12-14 16:50:35 +000034 dce, constprop, inlining, constmerge, strip, mstrip,
Chris Lattner8f367bd2001-07-23 02:35:57 +000035
Chris Lattner0eafc312001-10-18 06:05:15 +000036 // Miscellaneous Transformations
Chris Lattner5048c3b2002-01-22 00:13:51 +000037 trace, tracem, print, raiseallocs, cleangcc,
Chris Lattner0eafc312001-10-18 06:05:15 +000038
Chris Lattner8f367bd2001-07-23 02:35:57 +000039 // More powerful optimizations
Chris Lattner528e8b52001-12-14 16:50:35 +000040 indvars, instcombine, sccp, adce, raise,
Chris Lattner63202322001-11-26 19:22:39 +000041
42 // Interprocedural optimizations...
Chris Lattnerf4de63f2002-01-21 07:31:50 +000043 globaldce, swapstructs, sortstructs,
Chris Lattner00950542001-06-06 20:29:01 +000044};
45
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000046
47// New template functions - Provide functions that return passes of specified
48// types, with specified arguments...
49//
50template<class PassClass>
51Pass *New() {
52 return new PassClass();
53}
54
55template<class PassClass, typename ArgTy1, ArgTy1 Arg1>
56Pass *New() {
57 return new PassClass(Arg1);
58}
59
60template<class PassClass, typename ArgTy1, ArgTy1 Arg1,
61 typename ArgTy2, ArgTy1 Arg2>
62Pass *New() {
63 return new PassClass(Arg1, Arg2);
64}
65
66static Pass *NewPrintMethodPass() {
67 return new PrintMethodPass("Current Method: \n", &cerr);
68}
69
70// OptTable - Correlate enum Opts to Pass constructors...
71//
Chris Lattner8f367bd2001-07-23 02:35:57 +000072struct {
73 enum Opts OptID;
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000074 Pass * (*PassCtor)();
Chris Lattner8f367bd2001-07-23 02:35:57 +000075} OptTable[] = {
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000076 { dce , New<DeadCodeElimination> },
77 { constprop , New<ConstantPropogation> },
78 { inlining , New<MethodInlining> },
79 { constmerge , New<ConstantMerge> },
80 { strip , New<SymbolStripping> },
81 { mstrip , New<FullSymbolStripping> },
82 { indvars , New<InductionVariableSimplify> },
83 { instcombine, New<InstructionCombining> },
84 { sccp , New<SCCPPass> },
85 { adce , New<AgressiveDCE> },
86 { raise , New<RaisePointerReferences> },
87 { trace , New<InsertTraceCode, bool, true, bool, true> },
88 { tracem , New<InsertTraceCode, bool, false, bool, true> },
89 { print , NewPrintMethodPass },
90 { raiseallocs, New<RaiseAllocations> },
91 { cleangcc , New<CleanupGCCOutput> },
92 { globaldce , New<GlobalDCE> },
93 { swapstructs, New<SimpleStructMutation, SimpleStructMutation::Transform,
94 SimpleStructMutation::SwapElements>},
95 { sortstructs, New<SimpleStructMutation, SimpleStructMutation::Transform,
96 SimpleStructMutation::SortElements>},
Chris Lattner8f367bd2001-07-23 02:35:57 +000097};
98
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000099// Command line option handling code...
100//
Chris Lattnera8e1fd32001-07-23 20:22:30 +0000101cl::String InputFilename ("", "Load <arg> file to optimize", cl::NoFlags, "-");
102cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "");
103cl::Flag Force ("f", "Overwrite output files", cl::NoFlags, false);
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000104cl::Flag PrintEachXForm("p", "Print module after each transformation");
Chris Lattner8f367bd2001-07-23 02:35:57 +0000105cl::Flag Quiet ("q", "Don't print modifying pass names", 0, false);
Chris Lattnera8e1fd32001-07-23 20:22:30 +0000106cl::Alias QuietA ("quiet", "Alias for -q", cl::NoFlags, Quiet);
Chris Lattner8f367bd2001-07-23 02:35:57 +0000107cl::EnumList<enum Opts> OptimizationList(cl::NoFlags,
Chris Lattner528e8b52001-12-14 16:50:35 +0000108 clEnumVal(dce , "Dead Code 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(indvars , "Simplify Induction Variables"),
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000115 clEnumVal(instcombine, "Combine redundant instructions"),
Chris Lattner528e8b52001-12-14 16:50:35 +0000116 clEnumVal(sccp , "Sparse Conditional Constant Propogation"),
117 clEnumVal(adce , "Agressive DCE"),
Chris Lattner854acb92001-11-10 07:16:10 +0000118
Chris Lattner528e8b52001-12-14 16:50:35 +0000119 clEnumVal(globaldce , "Remove unreachable globals"),
Chris Lattner63202322001-11-26 19:22:39 +0000120 clEnumVal(swapstructs, "Swap structure types around"),
Chris Lattnerf4de63f2002-01-21 07:31:50 +0000121 clEnumVal(sortstructs, "Sort structure elements"),
Chris Lattner63202322001-11-26 19:22:39 +0000122
Chris Lattner5048c3b2002-01-22 00:13:51 +0000123 clEnumVal(raiseallocs, "Raise allocations from calls to instructions"),
Chris Lattner528e8b52001-12-14 16:50:35 +0000124 clEnumVal(cleangcc , "Cleanup GCC Output"),
125 clEnumVal(raise , "Raise to Higher Level"),
126 clEnumVal(trace , "Insert BB & Method trace code"),
127 clEnumVal(tracem , "Insert Method trace code only"),
128 clEnumVal(print , "Print working method to stderr"),
Chris Lattner8f367bd2001-07-23 02:35:57 +00001290);
130
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000131
Chris Lattner8f367bd2001-07-23 02:35:57 +0000132
Chris Lattner00950542001-06-06 20:29:01 +0000133int main(int argc, char **argv) {
Chris Lattner8f367bd2001-07-23 02:35:57 +0000134 cl::ParseCommandLineOptions(argc, argv,
135 " llvm .bc -> .bc modular optimizer\n");
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000136
137 // Load the input module...
Chris Lattner63202322001-11-26 19:22:39 +0000138 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
139 if (M.get() == 0) {
Chris Lattner00950542001-06-06 20:29:01 +0000140 cerr << "bytecode didn't read correctly.\n";
141 return 1;
142 }
143
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000144 // Figure out what stream we are supposed to write to...
Chris Lattner697954c2002-01-20 22:54:45 +0000145 std::ostream *Out = &std::cout; // Default to printing to stdout...
Chris Lattner1e78f362001-07-23 19:27:24 +0000146 if (OutputFilename != "") {
Chris Lattner888912d2002-01-22 21:07:24 +0000147 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner697954c2002-01-20 22:54:45 +0000148 // If force is not specified, make sure not to overwrite a file!
149 cerr << "Error opening '" << OutputFilename << "': File exists!\n"
150 << "Use -f command line argument to force output\n";
151 return 1;
152 }
153 Out = new std::ofstream(OutputFilename.c_str());
154
Chris Lattner00950542001-06-06 20:29:01 +0000155 if (!Out->good()) {
Chris Lattner1e78f362001-07-23 19:27:24 +0000156 cerr << "Error opening " << OutputFilename << "!\n";
Chris Lattner00950542001-06-06 20:29:01 +0000157 return 1;
158 }
159 }
160
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000161 // Create a PassManager to hold and optimize the collection of passes we are
162 // about to build...
163 //
164 PassManager Passes;
Chris Lattner00950542001-06-06 20:29:01 +0000165
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000166 // Create a new optimization pass for each one specified on the command line
167 for (unsigned i = 0; i < OptimizationList.size(); ++i) {
168 enum Opts Opt = OptimizationList[i];
169 for (unsigned j = 0; j < sizeof(OptTable)/sizeof(OptTable[0]); ++j)
170 if (Opt == OptTable[j].OptID) {
171 Passes.add(OptTable[j].PassCtor());
172 break;
173 }
174
175 if (PrintEachXForm)
176 Passes.add(new PrintModulePass(&std::cerr));
177 }
178
179 // Write bytecode out to disk or cout as the last step...
180 Passes.add(new WriteBytecodePass(Out, Out != &std::cout));
181
182 // Now that we have all of the passes ready, run them.
183 if (Passes.run(M.get()) && !Quiet)
184 cerr << "Program modified.\n";
185
Chris Lattner00950542001-06-06 20:29:01 +0000186 return 0;
187}