blob: dcd1c90cd35ec6a2c2cff5e30c01a013a2e2bd35 [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 Lattner57dbb3a2001-07-23 17:46:59 +000012#include "llvm/Support/CommandLine.h"
Chris Lattner95781b62001-06-30 06:38:31 +000013#include "llvm/Optimizations/AllOpts.h"
Chris Lattner0eafc312001-10-18 06:05:15 +000014#include "llvm/Transforms/Instrumentation/TraceValues.h"
Chris Lattnerffa6f9c2001-10-19 15:39:14 +000015#include "llvm/Assembly/PrintModulePass.h"
Chris Lattner9effd692001-10-18 20:06:45 +000016#include "llvm/Transforms/ConstantMerge.h"
Chris Lattnere166fe12001-10-31 04:29:44 +000017#include "llvm/Transforms/CleanupGCCOutput.h"
Chris Lattner068f4872001-11-01 02:41:09 +000018#include "llvm/Transforms/LevelChange.h"
Chris Lattner854acb92001-11-10 07:16:10 +000019#include "llvm/Transforms/SwapStructContents.h"
Chris Lattner73e11d72001-10-18 06:13:08 +000020#include <fstream>
Chris Lattner95781b62001-06-30 06:38:31 +000021
22using namespace opt;
Chris Lattner00950542001-06-06 20:29:01 +000023
Chris Lattner8f367bd2001-07-23 02:35:57 +000024enum Opts {
25 // Basic optimizations
Chris Lattner9effd692001-10-18 20:06:45 +000026 dce, constprop, inlining, mergecons, strip, mstrip,
Chris Lattner8f367bd2001-07-23 02:35:57 +000027
Chris Lattner0eafc312001-10-18 06:05:15 +000028 // Miscellaneous Transformations
Chris Lattner854acb92001-11-10 07:16:10 +000029 trace, tracem, print, cleangcc, swapstructs,
Chris Lattner0eafc312001-10-18 06:05:15 +000030
Chris Lattner8f367bd2001-07-23 02:35:57 +000031 // More powerful optimizations
Chris Lattner6dcf92a2001-09-07 16:59:35 +000032 indvars, sccp, adce, raise,
Chris Lattner00950542001-06-06 20:29:01 +000033};
34
Chris Lattner8f367bd2001-07-23 02:35:57 +000035struct {
36 enum Opts OptID;
Chris Lattner6db0f472001-10-18 01:31:43 +000037 Pass *ThePass;
Chris Lattner8f367bd2001-07-23 02:35:57 +000038} OptTable[] = {
Chris Lattner6db0f472001-10-18 01:31:43 +000039 { dce , new opt::DeadCodeElimination() },
40 { constprop, new opt::ConstantPropogation() },
41 { inlining , new opt::MethodInlining() },
Chris Lattner9effd692001-10-18 20:06:45 +000042 { mergecons, new ConstantMerge() },
Chris Lattner6db0f472001-10-18 01:31:43 +000043 { strip , new opt::SymbolStripping() },
44 { mstrip , new opt::FullSymbolStripping() },
45 { indvars , new opt::InductionVariableCannonicalize() },
46 { sccp , new opt::SCCPPass() },
47 { adce , new opt::AgressiveDCE() },
Chris Lattner068f4872001-11-01 02:41:09 +000048 { raise , new RaisePointerReferences() },
Chris Lattner0eafc312001-10-18 06:05:15 +000049 { trace , new InsertTraceCode(true, true) },
50 { tracem , new InsertTraceCode(false, true) },
51 { print , new PrintModulePass("Current Method: \n",&cerr) },
Chris Lattnere166fe12001-10-31 04:29:44 +000052 { cleangcc , new CleanupGCCOutput() },
Chris Lattner854acb92001-11-10 07:16:10 +000053 { swapstructs, new SwapStructContents() },
Chris Lattner8f367bd2001-07-23 02:35:57 +000054};
55
Chris Lattnera8e1fd32001-07-23 20:22:30 +000056cl::String InputFilename ("", "Load <arg> file to optimize", cl::NoFlags, "-");
57cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "");
58cl::Flag Force ("f", "Overwrite output files", cl::NoFlags, false);
Chris Lattner8f367bd2001-07-23 02:35:57 +000059cl::Flag Quiet ("q", "Don't print modifying pass names", 0, false);
Chris Lattnera8e1fd32001-07-23 20:22:30 +000060cl::Alias QuietA ("quiet", "Alias for -q", cl::NoFlags, Quiet);
Chris Lattner8f367bd2001-07-23 02:35:57 +000061cl::EnumList<enum Opts> OptimizationList(cl::NoFlags,
62 clEnumVal(dce , "Dead Code Elimination"),
63 clEnumVal(constprop, "Simple Constant Propogation"),
Chris Lattnerafb0cbb2001-07-23 23:02:51 +000064 clEnumValN(inlining , "inline", "Method Integration"),
Chris Lattner9effd692001-10-18 20:06:45 +000065 clEnumVal(mergecons, "Merge identical global constants"),
Chris Lattner8f367bd2001-07-23 02:35:57 +000066 clEnumVal(strip , "Strip Symbols"),
67 clEnumVal(mstrip , "Strip Module Symbols"),
68 clEnumVal(indvars , "Simplify Induction Variables"),
69 clEnumVal(sccp , "Sparse Conditional Constant Propogation"),
Chris Lattner8f367bd2001-07-23 02:35:57 +000070 clEnumVal(adce , "Agressive DCE"),
Chris Lattner854acb92001-11-10 07:16:10 +000071
Chris Lattnere166fe12001-10-31 04:29:44 +000072 clEnumVal(cleangcc , "Cleanup GCC Output"),
Chris Lattner8f367bd2001-07-23 02:35:57 +000073 clEnumVal(raise , "Raise to Higher Level"),
Chris Lattner0eafc312001-10-18 06:05:15 +000074 clEnumVal(trace , "Insert BB & Method trace code"),
75 clEnumVal(tracem , "Insert Method trace code only"),
Chris Lattner854acb92001-11-10 07:16:10 +000076 clEnumVal(swapstructs, "Swap structure types around"),
Chris Lattner0eafc312001-10-18 06:05:15 +000077 clEnumVal(print , "Print working method to stderr"),
Chris Lattner8f367bd2001-07-23 02:35:57 +0000780);
79
80
Chris Lattner00950542001-06-06 20:29:01 +000081int main(int argc, char **argv) {
Chris Lattner8f367bd2001-07-23 02:35:57 +000082 cl::ParseCommandLineOptions(argc, argv,
83 " llvm .bc -> .bc modular optimizer\n");
84
Chris Lattner1e78f362001-07-23 19:27:24 +000085 Module *C = ParseBytecodeFile(InputFilename);
Chris Lattner00950542001-06-06 20:29:01 +000086 if (C == 0) {
87 cerr << "bytecode didn't read correctly.\n";
88 return 1;
89 }
90
Chris Lattner8f367bd2001-07-23 02:35:57 +000091 for (unsigned i = 0; i < OptimizationList.size(); ++i) {
92 enum Opts Opt = OptimizationList[i];
Chris Lattner00950542001-06-06 20:29:01 +000093
Chris Lattner00950542001-06-06 20:29:01 +000094 unsigned j;
Chris Lattner8f367bd2001-07-23 02:35:57 +000095 for (j = 0; j < sizeof(OptTable)/sizeof(OptTable[0]); ++j) {
96 if (Opt == OptTable[j].OptID) {
Chris Lattner6db0f472001-10-18 01:31:43 +000097 if (OptTable[j].ThePass->run(C) && !Quiet)
Chris Lattner8f367bd2001-07-23 02:35:57 +000098 cerr << OptimizationList.getArgName(Opt)
99 << " pass made modifications!\n";
Chris Lattner00950542001-06-06 20:29:01 +0000100 break;
101 }
102 }
103
104 if (j == sizeof(OptTable)/sizeof(OptTable[0]))
Chris Lattner8f367bd2001-07-23 02:35:57 +0000105 cerr << "Optimization tables inconsistent!!\n";
Chris Lattner00950542001-06-06 20:29:01 +0000106 }
107
Chris Lattner8f367bd2001-07-23 02:35:57 +0000108 ostream *Out = &cout; // Default to printing to stdout...
Chris Lattner1e78f362001-07-23 19:27:24 +0000109 if (OutputFilename != "") {
110 Out = new ofstream(OutputFilename.c_str(),
111 (Force ? 0 : ios::noreplace)|ios::out);
Chris Lattner00950542001-06-06 20:29:01 +0000112 if (!Out->good()) {
Chris Lattner1e78f362001-07-23 19:27:24 +0000113 cerr << "Error opening " << OutputFilename << "!\n";
Chris Lattner00950542001-06-06 20:29:01 +0000114 delete C;
115 return 1;
116 }
117 }
118
119 // Okay, we're done now... write out result...
120 WriteBytecodeToFile(C, *Out);
121 delete C;
122
123 if (Out != &cout) delete Out;
124 return 0;
125}