blob: 8a2208d92d3b5bab6969d2526e55bab6d74884a3 [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...
37 globaldce, swapstructs,
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 Lattneree6826b2001-11-26 18:18:53 +000044 { swapstructs, 0 },
Chris Lattner528e8b52001-12-14 16:50:35 +000045 { dce , new opt::DeadCodeElimination() },
46 { constprop , new opt::ConstantPropogation() },
47 { inlining , new opt::MethodInlining() },
48 { constmerge , new ConstantMerge() },
49 { strip , new opt::SymbolStripping() },
50 { mstrip , new opt::FullSymbolStripping() },
51 { indvars , new InductionVariableSimplify() },
52 { instcombine, new InstructionCombining() },
53 { sccp , new opt::SCCPPass() },
54 { adce , new opt::AgressiveDCE() },
55 { raise , new RaisePointerReferences() },
56 { trace , new InsertTraceCode(true, true) },
57 { tracem , new InsertTraceCode(false, true) },
58 { print , new PrintModulePass("Current Method: \n",&cerr) },
59 { cleangcc , new CleanupGCCOutput() },
Chris Lattner8f367bd2001-07-23 02:35:57 +000060};
61
Chris Lattnera8e1fd32001-07-23 20:22:30 +000062cl::String InputFilename ("", "Load <arg> file to optimize", cl::NoFlags, "-");
63cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "");
64cl::Flag Force ("f", "Overwrite output files", cl::NoFlags, false);
Chris Lattner8f367bd2001-07-23 02:35:57 +000065cl::Flag Quiet ("q", "Don't print modifying pass names", 0, false);
Chris Lattnera8e1fd32001-07-23 20:22:30 +000066cl::Alias QuietA ("quiet", "Alias for -q", cl::NoFlags, Quiet);
Chris Lattner8f367bd2001-07-23 02:35:57 +000067cl::EnumList<enum Opts> OptimizationList(cl::NoFlags,
Chris Lattner528e8b52001-12-14 16:50:35 +000068 clEnumVal(dce , "Dead Code Elimination"),
69 clEnumVal(constprop , "Simple Constant Propogation"),
70 clEnumValN(inlining , "inline", "Method Integration"),
71 clEnumVal(constmerge , "Merge identical global constants"),
72 clEnumVal(strip , "Strip Symbols"),
73 clEnumVal(mstrip , "Strip Module Symbols"),
74 clEnumVal(indvars , "Simplify Induction Variables"),
75 clEnumVal(instcombine, "Simplify Induction Variables"),
76 clEnumVal(sccp , "Sparse Conditional Constant Propogation"),
77 clEnumVal(adce , "Agressive DCE"),
Chris Lattner854acb92001-11-10 07:16:10 +000078
Chris Lattner528e8b52001-12-14 16:50:35 +000079 clEnumVal(globaldce , "Remove unreachable globals"),
Chris Lattner63202322001-11-26 19:22:39 +000080 clEnumVal(swapstructs, "Swap structure types around"),
81
Chris Lattner528e8b52001-12-14 16:50:35 +000082 clEnumVal(cleangcc , "Cleanup GCC Output"),
83 clEnumVal(raise , "Raise to Higher Level"),
84 clEnumVal(trace , "Insert BB & Method trace code"),
85 clEnumVal(tracem , "Insert Method trace code only"),
86 clEnumVal(print , "Print working method to stderr"),
Chris Lattner8f367bd2001-07-23 02:35:57 +0000870);
88
Chris Lattner63202322001-11-26 19:22:39 +000089static void RunOptimization(Module *M, enum Opts Opt) {
90 for (unsigned j = 0; j < sizeof(OptTable)/sizeof(OptTable[0]); ++j)
91 if (Opt == OptTable[j].OptID) {
92 if (OptTable[j].ThePass->run(M) && !Quiet)
93 cerr << OptimizationList.getArgName(Opt)
94 << " pass made modifications!\n";
95 return;
96 }
97
98 // Special cases that haven't been fit into a consistent framework yet...
99 switch (Opt) {
100 case globaldce: {
101 GlobalDCE GDCE; GDCE.run(M); return;
102 }
103 case swapstructs: {
104 PrebuiltStructMutation SM(M, PrebuiltStructMutation::SortElements);
105 SM.run(M); return;
106 }
107 default:
108 cerr << "Optimization tables inconsistent!!\n";
109 }
110}
Chris Lattner8f367bd2001-07-23 02:35:57 +0000111
Chris Lattner00950542001-06-06 20:29:01 +0000112int main(int argc, char **argv) {
Chris Lattner8f367bd2001-07-23 02:35:57 +0000113 cl::ParseCommandLineOptions(argc, argv,
114 " llvm .bc -> .bc modular optimizer\n");
Chris Lattner63202322001-11-26 19:22:39 +0000115 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
116 if (M.get() == 0) {
Chris Lattner00950542001-06-06 20:29:01 +0000117 cerr << "bytecode didn't read correctly.\n";
118 return 1;
119 }
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}