blob: 6cbf25111bf9d1eb71b899ddba509f2bd61d8569 [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"
10#include "llvm/Bytecode/Reader.h"
11#include "llvm/Bytecode/Writer.h"
Chris Lattnerffa6f9c2001-10-19 15:39:14 +000012#include "llvm/Assembly/PrintModulePass.h"
Chris Lattner9effd692001-10-18 20:06:45 +000013#include "llvm/Transforms/ConstantMerge.h"
Chris Lattnere166fe12001-10-31 04:29:44 +000014#include "llvm/Transforms/CleanupGCCOutput.h"
Chris Lattner068f4872001-11-01 02:41:09 +000015#include "llvm/Transforms/LevelChange.h"
Chris Lattner59b6b8e2002-01-21 23:17:48 +000016#include "llvm/Transforms/MethodInlining.h"
17#include "llvm/Transforms/SymbolStripping.h"
Chris Lattnerd7db8632002-01-22 01:04:08 +000018#include "llvm/Transforms/ChangeAllocations.h"
Chris Lattner04c85dc2002-01-21 07:52:35 +000019#include "llvm/Transforms/IPO/SimpleStructMutation.h"
Chris Lattner63202322001-11-26 19:22:39 +000020#include "llvm/Transforms/IPO/GlobalDCE.h"
Chris Lattner59b6b8e2002-01-21 23:17:48 +000021#include "llvm/Transforms/Scalar/DCE.h"
22#include "llvm/Transforms/Scalar/ConstantProp.h"
23#include "llvm/Transforms/Scalar/InductionVars.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 Lattner59b6b8e2002-01-21 23:17:48 +000031
32
Chris Lattner8f367bd2001-07-23 02:35:57 +000033enum Opts {
34 // Basic optimizations
Chris Lattner528e8b52001-12-14 16:50:35 +000035 dce, constprop, inlining, constmerge, strip, mstrip,
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 Lattner8f367bd2001-07-23 02:35:57 +000047struct {
48 enum Opts OptID;
Chris Lattner6db0f472001-10-18 01:31:43 +000049 Pass *ThePass;
Chris Lattner8f367bd2001-07-23 02:35:57 +000050} OptTable[] = {
Chris Lattner59b6b8e2002-01-21 23:17:48 +000051 { dce , new DeadCodeElimination() },
52 { constprop , new ConstantPropogation() },
53 { inlining , new MethodInlining() },
Chris Lattner528e8b52001-12-14 16:50:35 +000054 { constmerge , new ConstantMerge() },
Chris Lattner59b6b8e2002-01-21 23:17:48 +000055 { strip , new SymbolStripping() },
56 { mstrip , new FullSymbolStripping() },
Chris Lattner528e8b52001-12-14 16:50:35 +000057 { indvars , new InductionVariableSimplify() },
58 { instcombine, new InstructionCombining() },
Chris Lattner59b6b8e2002-01-21 23:17:48 +000059 { sccp , new SCCPPass() },
60 { adce , new AgressiveDCE() },
Chris Lattner528e8b52001-12-14 16:50:35 +000061 { raise , new RaisePointerReferences() },
62 { trace , new InsertTraceCode(true, true) },
63 { tracem , new InsertTraceCode(false, true) },
Chris Lattnerf4de63f2002-01-21 07:31:50 +000064 { print , new PrintMethodPass("Current Method: \n",&cerr) },
Chris Lattner5048c3b2002-01-22 00:13:51 +000065 { raiseallocs, new RaiseAllocations() },
Chris Lattner528e8b52001-12-14 16:50:35 +000066 { cleangcc , new CleanupGCCOutput() },
Chris Lattnerf4de63f2002-01-21 07:31:50 +000067 { globaldce , new GlobalDCE() },
68 { swapstructs, new SimpleStructMutation(SimpleStructMutation::SwapElements) },
69 { sortstructs, new SimpleStructMutation(SimpleStructMutation::SortElements) },
Chris Lattner8f367bd2001-07-23 02:35:57 +000070};
71
Chris Lattnera8e1fd32001-07-23 20:22:30 +000072cl::String InputFilename ("", "Load <arg> file to optimize", cl::NoFlags, "-");
73cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "");
74cl::Flag Force ("f", "Overwrite output files", cl::NoFlags, false);
Chris Lattner8f367bd2001-07-23 02:35:57 +000075cl::Flag Quiet ("q", "Don't print modifying pass names", 0, false);
Chris Lattnera8e1fd32001-07-23 20:22:30 +000076cl::Alias QuietA ("quiet", "Alias for -q", cl::NoFlags, Quiet);
Chris Lattner8f367bd2001-07-23 02:35:57 +000077cl::EnumList<enum Opts> OptimizationList(cl::NoFlags,
Chris Lattner528e8b52001-12-14 16:50:35 +000078 clEnumVal(dce , "Dead Code Elimination"),
79 clEnumVal(constprop , "Simple Constant Propogation"),
80 clEnumValN(inlining , "inline", "Method Integration"),
81 clEnumVal(constmerge , "Merge identical global constants"),
82 clEnumVal(strip , "Strip Symbols"),
83 clEnumVal(mstrip , "Strip Module Symbols"),
84 clEnumVal(indvars , "Simplify Induction Variables"),
85 clEnumVal(instcombine, "Simplify Induction Variables"),
86 clEnumVal(sccp , "Sparse Conditional Constant Propogation"),
87 clEnumVal(adce , "Agressive DCE"),
Chris Lattner854acb92001-11-10 07:16:10 +000088
Chris Lattner528e8b52001-12-14 16:50:35 +000089 clEnumVal(globaldce , "Remove unreachable globals"),
Chris Lattner63202322001-11-26 19:22:39 +000090 clEnumVal(swapstructs, "Swap structure types around"),
Chris Lattnerf4de63f2002-01-21 07:31:50 +000091 clEnumVal(sortstructs, "Sort structure elements"),
Chris Lattner63202322001-11-26 19:22:39 +000092
Chris Lattner5048c3b2002-01-22 00:13:51 +000093 clEnumVal(raiseallocs, "Raise allocations from calls to instructions"),
Chris Lattner528e8b52001-12-14 16:50:35 +000094 clEnumVal(cleangcc , "Cleanup GCC Output"),
95 clEnumVal(raise , "Raise to Higher Level"),
96 clEnumVal(trace , "Insert BB & Method trace code"),
97 clEnumVal(tracem , "Insert Method trace code only"),
98 clEnumVal(print , "Print working method to stderr"),
Chris Lattner8f367bd2001-07-23 02:35:57 +0000990);
100
Chris Lattner63202322001-11-26 19:22:39 +0000101static void RunOptimization(Module *M, enum Opts Opt) {
102 for (unsigned j = 0; j < sizeof(OptTable)/sizeof(OptTable[0]); ++j)
103 if (Opt == OptTable[j].OptID) {
104 if (OptTable[j].ThePass->run(M) && !Quiet)
105 cerr << OptimizationList.getArgName(Opt)
106 << " pass made modifications!\n";
107 return;
108 }
109
Chris Lattnerf4de63f2002-01-21 07:31:50 +0000110 cerr << "Optimization tables inconsistent!!\n";
Chris Lattner63202322001-11-26 19:22:39 +0000111}
Chris Lattner8f367bd2001-07-23 02:35:57 +0000112
Chris Lattner00950542001-06-06 20:29:01 +0000113int main(int argc, char **argv) {
Chris Lattner8f367bd2001-07-23 02:35:57 +0000114 cl::ParseCommandLineOptions(argc, argv,
115 " llvm .bc -> .bc modular optimizer\n");
Chris Lattner63202322001-11-26 19:22:39 +0000116 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
117 if (M.get() == 0) {
Chris Lattner00950542001-06-06 20:29:01 +0000118 cerr << "bytecode didn't read correctly.\n";
119 return 1;
120 }
121
Chris Lattnerf4de63f2002-01-21 07:31:50 +0000122 PassManager Passes;
123
Chris Lattner63202322001-11-26 19:22:39 +0000124 // Run all of the optimizations specified on the command line
125 for (unsigned i = 0; i < OptimizationList.size(); ++i)
126 RunOptimization(M.get(), OptimizationList[i]);
Chris Lattner00950542001-06-06 20:29:01 +0000127
Chris Lattner697954c2002-01-20 22:54:45 +0000128 std::ostream *Out = &std::cout; // Default to printing to stdout...
Chris Lattner1e78f362001-07-23 19:27:24 +0000129 if (OutputFilename != "") {
Chris Lattner697954c2002-01-20 22:54:45 +0000130 if (!Force && !std::ifstream(OutputFilename.c_str())) {
131 // If force is not specified, make sure not to overwrite a file!
132 cerr << "Error opening '" << OutputFilename << "': File exists!\n"
133 << "Use -f command line argument to force output\n";
134 return 1;
135 }
136 Out = new std::ofstream(OutputFilename.c_str());
137
Chris Lattner00950542001-06-06 20:29:01 +0000138 if (!Out->good()) {
Chris Lattner1e78f362001-07-23 19:27:24 +0000139 cerr << "Error opening " << OutputFilename << "!\n";
Chris Lattner00950542001-06-06 20:29:01 +0000140 return 1;
141 }
142 }
143
144 // Okay, we're done now... write out result...
Chris Lattner63202322001-11-26 19:22:39 +0000145 WriteBytecodeToFile(M.get(), *Out);
Chris Lattner00950542001-06-06 20:29:01 +0000146
147 if (Out != &cout) delete Out;
148 return 0;
149}