blob: 7f9acf03e6b62c0d6a3ef38fe02fd65ddab826d1 [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 Lattner95781b62001-06-30 06:38:31 +000012#include "llvm/Optimizations/AllOpts.h"
Chris Lattner0eafc312001-10-18 06:05:15 +000013#include "llvm/Transforms/Instrumentation/TraceValues.h"
Chris Lattnerffa6f9c2001-10-19 15:39:14 +000014#include "llvm/Assembly/PrintModulePass.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 Lattner854acb92001-11-10 07:16:10 +000018#include "llvm/Transforms/SwapStructContents.h"
Chris Lattner63202322001-11-26 19:22:39 +000019#include "llvm/Transforms/IPO/GlobalDCE.h"
Chris Lattnerfe196cf2001-12-04 04:32:04 +000020#include "llvm/Transforms/Scalar/IndVarSimplify.h"
Chris Lattner528e8b52001-12-14 16:50:35 +000021#include "llvm/Transforms/Scalar/InstructionCombining.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000022#include "Support/CommandLine.h"
Chris Lattner73e11d72001-10-18 06:13:08 +000023#include <fstream>
Chris Lattner63202322001-11-26 19:22:39 +000024#include <memory>
Chris Lattner95781b62001-06-30 06:38:31 +000025
Chris Lattner8f367bd2001-07-23 02:35:57 +000026enum Opts {
27 // Basic optimizations
Chris Lattner528e8b52001-12-14 16:50:35 +000028 dce, constprop, inlining, constmerge, strip, mstrip,
Chris Lattner8f367bd2001-07-23 02:35:57 +000029
Chris Lattner0eafc312001-10-18 06:05:15 +000030 // Miscellaneous Transformations
Chris Lattner63202322001-11-26 19:22:39 +000031 trace, tracem, print, cleangcc,
Chris Lattner0eafc312001-10-18 06:05:15 +000032
Chris Lattner8f367bd2001-07-23 02:35:57 +000033 // More powerful optimizations
Chris Lattner528e8b52001-12-14 16:50:35 +000034 indvars, instcombine, sccp, adce, raise,
Chris Lattner63202322001-11-26 19:22:39 +000035
36 // Interprocedural optimizations...
Chris Lattnerf4de63f2002-01-21 07:31:50 +000037 globaldce, swapstructs, sortstructs,
Chris Lattner00950542001-06-06 20:29:01 +000038};
39
Chris Lattner8f367bd2001-07-23 02:35:57 +000040struct {
41 enum Opts OptID;
Chris Lattner6db0f472001-10-18 01:31:43 +000042 Pass *ThePass;
Chris Lattner8f367bd2001-07-23 02:35:57 +000043} OptTable[] = {
Chris Lattner528e8b52001-12-14 16:50:35 +000044 { dce , new opt::DeadCodeElimination() },
45 { constprop , new opt::ConstantPropogation() },
46 { inlining , new opt::MethodInlining() },
47 { constmerge , new ConstantMerge() },
48 { strip , new opt::SymbolStripping() },
49 { mstrip , new opt::FullSymbolStripping() },
50 { indvars , new InductionVariableSimplify() },
51 { instcombine, new InstructionCombining() },
52 { sccp , new opt::SCCPPass() },
53 { adce , new opt::AgressiveDCE() },
54 { raise , new RaisePointerReferences() },
55 { trace , new InsertTraceCode(true, true) },
56 { tracem , new InsertTraceCode(false, true) },
Chris Lattnerf4de63f2002-01-21 07:31:50 +000057 { print , new PrintMethodPass("Current Method: \n",&cerr) },
Chris Lattner528e8b52001-12-14 16:50:35 +000058 { cleangcc , new CleanupGCCOutput() },
Chris Lattnerf4de63f2002-01-21 07:31:50 +000059 { globaldce , new GlobalDCE() },
60 { swapstructs, new SimpleStructMutation(SimpleStructMutation::SwapElements) },
61 { sortstructs, new SimpleStructMutation(SimpleStructMutation::SortElements) },
Chris Lattner8f367bd2001-07-23 02:35:57 +000062};
63
Chris Lattnera8e1fd32001-07-23 20:22:30 +000064cl::String InputFilename ("", "Load <arg> file to optimize", cl::NoFlags, "-");
65cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "");
66cl::Flag Force ("f", "Overwrite output files", cl::NoFlags, false);
Chris Lattner8f367bd2001-07-23 02:35:57 +000067cl::Flag Quiet ("q", "Don't print modifying pass names", 0, false);
Chris Lattnera8e1fd32001-07-23 20:22:30 +000068cl::Alias QuietA ("quiet", "Alias for -q", cl::NoFlags, Quiet);
Chris Lattner8f367bd2001-07-23 02:35:57 +000069cl::EnumList<enum Opts> OptimizationList(cl::NoFlags,
Chris Lattner528e8b52001-12-14 16:50:35 +000070 clEnumVal(dce , "Dead Code Elimination"),
71 clEnumVal(constprop , "Simple Constant Propogation"),
72 clEnumValN(inlining , "inline", "Method Integration"),
73 clEnumVal(constmerge , "Merge identical global constants"),
74 clEnumVal(strip , "Strip Symbols"),
75 clEnumVal(mstrip , "Strip Module Symbols"),
76 clEnumVal(indvars , "Simplify Induction Variables"),
77 clEnumVal(instcombine, "Simplify Induction Variables"),
78 clEnumVal(sccp , "Sparse Conditional Constant Propogation"),
79 clEnumVal(adce , "Agressive DCE"),
Chris Lattner854acb92001-11-10 07:16:10 +000080
Chris Lattner528e8b52001-12-14 16:50:35 +000081 clEnumVal(globaldce , "Remove unreachable globals"),
Chris Lattner63202322001-11-26 19:22:39 +000082 clEnumVal(swapstructs, "Swap structure types around"),
Chris Lattnerf4de63f2002-01-21 07:31:50 +000083 clEnumVal(sortstructs, "Sort structure elements"),
Chris Lattner63202322001-11-26 19:22:39 +000084
Chris Lattner528e8b52001-12-14 16:50:35 +000085 clEnumVal(cleangcc , "Cleanup GCC Output"),
86 clEnumVal(raise , "Raise to Higher Level"),
87 clEnumVal(trace , "Insert BB & Method trace code"),
88 clEnumVal(tracem , "Insert Method trace code only"),
89 clEnumVal(print , "Print working method to stderr"),
Chris Lattner8f367bd2001-07-23 02:35:57 +0000900);
91
Chris Lattner63202322001-11-26 19:22:39 +000092static void RunOptimization(Module *M, enum Opts Opt) {
93 for (unsigned j = 0; j < sizeof(OptTable)/sizeof(OptTable[0]); ++j)
94 if (Opt == OptTable[j].OptID) {
95 if (OptTable[j].ThePass->run(M) && !Quiet)
96 cerr << OptimizationList.getArgName(Opt)
97 << " pass made modifications!\n";
98 return;
99 }
100
Chris Lattnerf4de63f2002-01-21 07:31:50 +0000101 cerr << "Optimization tables inconsistent!!\n";
Chris Lattner63202322001-11-26 19:22:39 +0000102}
Chris Lattner8f367bd2001-07-23 02:35:57 +0000103
Chris Lattner00950542001-06-06 20:29:01 +0000104int main(int argc, char **argv) {
Chris Lattner8f367bd2001-07-23 02:35:57 +0000105 cl::ParseCommandLineOptions(argc, argv,
106 " llvm .bc -> .bc modular optimizer\n");
Chris Lattner63202322001-11-26 19:22:39 +0000107 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
108 if (M.get() == 0) {
Chris Lattner00950542001-06-06 20:29:01 +0000109 cerr << "bytecode didn't read correctly.\n";
110 return 1;
111 }
112
Chris Lattnerf4de63f2002-01-21 07:31:50 +0000113 PassManager Passes;
114
Chris Lattner63202322001-11-26 19:22:39 +0000115 // Run all of the optimizations specified on the command line
116 for (unsigned i = 0; i < OptimizationList.size(); ++i)
117 RunOptimization(M.get(), OptimizationList[i]);
Chris Lattner00950542001-06-06 20:29:01 +0000118
Chris Lattner697954c2002-01-20 22:54:45 +0000119 std::ostream *Out = &std::cout; // Default to printing to stdout...
Chris Lattner1e78f362001-07-23 19:27:24 +0000120 if (OutputFilename != "") {
Chris Lattner697954c2002-01-20 22:54:45 +0000121 if (!Force && !std::ifstream(OutputFilename.c_str())) {
122 // If force is not specified, make sure not to overwrite a file!
123 cerr << "Error opening '" << OutputFilename << "': File exists!\n"
124 << "Use -f command line argument to force output\n";
125 return 1;
126 }
127 Out = new std::ofstream(OutputFilename.c_str());
128
Chris Lattner00950542001-06-06 20:29:01 +0000129 if (!Out->good()) {
Chris Lattner1e78f362001-07-23 19:27:24 +0000130 cerr << "Error opening " << OutputFilename << "!\n";
Chris Lattner00950542001-06-06 20:29:01 +0000131 return 1;
132 }
133 }
134
135 // Okay, we're done now... write out result...
Chris Lattner63202322001-11-26 19:22:39 +0000136 WriteBytecodeToFile(M.get(), *Out);
Chris Lattner00950542001-06-06 20:29:01 +0000137
138 if (Out != &cout) delete Out;
139 return 0;
140}