blob: 36de3bfbf73246e69c0ae76e4d9dde295dbe005c [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 Lattner04c85dc2002-01-21 07:52:35 +000018#include "llvm/Transforms/IPO/SimpleStructMutation.h"
Chris Lattner63202322001-11-26 19:22:39 +000019#include "llvm/Transforms/IPO/GlobalDCE.h"
Chris Lattner59b6b8e2002-01-21 23:17:48 +000020#include "llvm/Transforms/Scalar/DCE.h"
21#include "llvm/Transforms/Scalar/ConstantProp.h"
22#include "llvm/Transforms/Scalar/InductionVars.h"
Chris Lattnerfe196cf2001-12-04 04:32:04 +000023#include "llvm/Transforms/Scalar/IndVarSimplify.h"
Chris Lattner528e8b52001-12-14 16:50:35 +000024#include "llvm/Transforms/Scalar/InstructionCombining.h"
Chris Lattner59b6b8e2002-01-21 23:17:48 +000025#include "llvm/Transforms/Instrumentation/TraceValues.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000026#include "Support/CommandLine.h"
Chris Lattner73e11d72001-10-18 06:13:08 +000027#include <fstream>
Chris Lattner63202322001-11-26 19:22:39 +000028#include <memory>
Chris Lattner95781b62001-06-30 06:38:31 +000029
Chris Lattner59b6b8e2002-01-21 23:17:48 +000030
31
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 Lattner63202322001-11-26 19:22:39 +000037 trace, tracem, print, 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 Lattner8f367bd2001-07-23 02:35:57 +000046struct {
47 enum Opts OptID;
Chris Lattner6db0f472001-10-18 01:31:43 +000048 Pass *ThePass;
Chris Lattner8f367bd2001-07-23 02:35:57 +000049} OptTable[] = {
Chris Lattner59b6b8e2002-01-21 23:17:48 +000050 { dce , new DeadCodeElimination() },
51 { constprop , new ConstantPropogation() },
52 { inlining , new MethodInlining() },
Chris Lattner528e8b52001-12-14 16:50:35 +000053 { constmerge , new ConstantMerge() },
Chris Lattner59b6b8e2002-01-21 23:17:48 +000054 { strip , new SymbolStripping() },
55 { mstrip , new FullSymbolStripping() },
Chris Lattner528e8b52001-12-14 16:50:35 +000056 { indvars , new InductionVariableSimplify() },
57 { instcombine, new InstructionCombining() },
Chris Lattner59b6b8e2002-01-21 23:17:48 +000058 { sccp , new SCCPPass() },
59 { adce , new AgressiveDCE() },
Chris Lattner528e8b52001-12-14 16:50:35 +000060 { raise , new RaisePointerReferences() },
61 { trace , new InsertTraceCode(true, true) },
62 { tracem , new InsertTraceCode(false, true) },
Chris Lattnerf4de63f2002-01-21 07:31:50 +000063 { print , new PrintMethodPass("Current Method: \n",&cerr) },
Chris Lattner528e8b52001-12-14 16:50:35 +000064 { cleangcc , new CleanupGCCOutput() },
Chris Lattnerf4de63f2002-01-21 07:31:50 +000065 { globaldce , new GlobalDCE() },
66 { swapstructs, new SimpleStructMutation(SimpleStructMutation::SwapElements) },
67 { sortstructs, new SimpleStructMutation(SimpleStructMutation::SortElements) },
Chris Lattner8f367bd2001-07-23 02:35:57 +000068};
69
Chris Lattnera8e1fd32001-07-23 20:22:30 +000070cl::String InputFilename ("", "Load <arg> file to optimize", cl::NoFlags, "-");
71cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "");
72cl::Flag Force ("f", "Overwrite output files", cl::NoFlags, false);
Chris Lattner8f367bd2001-07-23 02:35:57 +000073cl::Flag Quiet ("q", "Don't print modifying pass names", 0, false);
Chris Lattnera8e1fd32001-07-23 20:22:30 +000074cl::Alias QuietA ("quiet", "Alias for -q", cl::NoFlags, Quiet);
Chris Lattner8f367bd2001-07-23 02:35:57 +000075cl::EnumList<enum Opts> OptimizationList(cl::NoFlags,
Chris Lattner528e8b52001-12-14 16:50:35 +000076 clEnumVal(dce , "Dead Code Elimination"),
77 clEnumVal(constprop , "Simple Constant Propogation"),
78 clEnumValN(inlining , "inline", "Method Integration"),
79 clEnumVal(constmerge , "Merge identical global constants"),
80 clEnumVal(strip , "Strip Symbols"),
81 clEnumVal(mstrip , "Strip Module Symbols"),
82 clEnumVal(indvars , "Simplify Induction Variables"),
83 clEnumVal(instcombine, "Simplify Induction Variables"),
84 clEnumVal(sccp , "Sparse Conditional Constant Propogation"),
85 clEnumVal(adce , "Agressive DCE"),
Chris Lattner854acb92001-11-10 07:16:10 +000086
Chris Lattner528e8b52001-12-14 16:50:35 +000087 clEnumVal(globaldce , "Remove unreachable globals"),
Chris Lattner63202322001-11-26 19:22:39 +000088 clEnumVal(swapstructs, "Swap structure types around"),
Chris Lattnerf4de63f2002-01-21 07:31:50 +000089 clEnumVal(sortstructs, "Sort structure elements"),
Chris Lattner63202322001-11-26 19:22:39 +000090
Chris Lattner528e8b52001-12-14 16:50:35 +000091 clEnumVal(cleangcc , "Cleanup GCC Output"),
92 clEnumVal(raise , "Raise to Higher Level"),
93 clEnumVal(trace , "Insert BB & Method trace code"),
94 clEnumVal(tracem , "Insert Method trace code only"),
95 clEnumVal(print , "Print working method to stderr"),
Chris Lattner8f367bd2001-07-23 02:35:57 +0000960);
97
Chris Lattner63202322001-11-26 19:22:39 +000098static void RunOptimization(Module *M, enum Opts Opt) {
99 for (unsigned j = 0; j < sizeof(OptTable)/sizeof(OptTable[0]); ++j)
100 if (Opt == OptTable[j].OptID) {
101 if (OptTable[j].ThePass->run(M) && !Quiet)
102 cerr << OptimizationList.getArgName(Opt)
103 << " pass made modifications!\n";
104 return;
105 }
106
Chris Lattnerf4de63f2002-01-21 07:31:50 +0000107 cerr << "Optimization tables inconsistent!!\n";
Chris Lattner63202322001-11-26 19:22:39 +0000108}
Chris Lattner8f367bd2001-07-23 02:35:57 +0000109
Chris Lattner00950542001-06-06 20:29:01 +0000110int main(int argc, char **argv) {
Chris Lattner8f367bd2001-07-23 02:35:57 +0000111 cl::ParseCommandLineOptions(argc, argv,
112 " llvm .bc -> .bc modular optimizer\n");
Chris Lattner63202322001-11-26 19:22:39 +0000113 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
114 if (M.get() == 0) {
Chris Lattner00950542001-06-06 20:29:01 +0000115 cerr << "bytecode didn't read correctly.\n";
116 return 1;
117 }
118
Chris Lattnerf4de63f2002-01-21 07:31:50 +0000119 PassManager Passes;
120
Chris Lattner63202322001-11-26 19:22:39 +0000121 // Run all of the optimizations specified on the command line
122 for (unsigned i = 0; i < OptimizationList.size(); ++i)
123 RunOptimization(M.get(), OptimizationList[i]);
Chris Lattner00950542001-06-06 20:29:01 +0000124
Chris Lattner697954c2002-01-20 22:54:45 +0000125 std::ostream *Out = &std::cout; // Default to printing to stdout...
Chris Lattner1e78f362001-07-23 19:27:24 +0000126 if (OutputFilename != "") {
Chris Lattner697954c2002-01-20 22:54:45 +0000127 if (!Force && !std::ifstream(OutputFilename.c_str())) {
128 // If force is not specified, make sure not to overwrite a file!
129 cerr << "Error opening '" << OutputFilename << "': File exists!\n"
130 << "Use -f command line argument to force output\n";
131 return 1;
132 }
133 Out = new std::ofstream(OutputFilename.c_str());
134
Chris Lattner00950542001-06-06 20:29:01 +0000135 if (!Out->good()) {
Chris Lattner1e78f362001-07-23 19:27:24 +0000136 cerr << "Error opening " << OutputFilename << "!\n";
Chris Lattner00950542001-06-06 20:29:01 +0000137 return 1;
138 }
139 }
140
141 // Okay, we're done now... write out result...
Chris Lattner63202322001-11-26 19:22:39 +0000142 WriteBytecodeToFile(M.get(), *Out);
Chris Lattner00950542001-06-06 20:29:01 +0000143
144 if (Out != &cout) delete Out;
145 return 0;
146}