blob: 5c620a5234688fadd4a98a036adadcfd907a445a [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 Lattner73e11d72001-10-18 06:13:08 +000019#include <fstream>
Chris Lattner95781b62001-06-30 06:38:31 +000020
21using namespace opt;
Chris Lattner00950542001-06-06 20:29:01 +000022
Chris Lattner8f367bd2001-07-23 02:35:57 +000023enum Opts {
24 // Basic optimizations
Chris Lattner9effd692001-10-18 20:06:45 +000025 dce, constprop, inlining, mergecons, strip, mstrip,
Chris Lattner8f367bd2001-07-23 02:35:57 +000026
Chris Lattner0eafc312001-10-18 06:05:15 +000027 // Miscellaneous Transformations
Chris Lattnere166fe12001-10-31 04:29:44 +000028 trace, tracem, print, cleangcc,
Chris Lattner0eafc312001-10-18 06:05:15 +000029
Chris Lattner8f367bd2001-07-23 02:35:57 +000030 // More powerful optimizations
Chris Lattner6dcf92a2001-09-07 16:59:35 +000031 indvars, sccp, adce, raise,
Chris Lattner00950542001-06-06 20:29:01 +000032};
33
Chris Lattner8f367bd2001-07-23 02:35:57 +000034struct {
35 enum Opts OptID;
Chris Lattner6db0f472001-10-18 01:31:43 +000036 Pass *ThePass;
Chris Lattner8f367bd2001-07-23 02:35:57 +000037} OptTable[] = {
Chris Lattner6db0f472001-10-18 01:31:43 +000038 { dce , new opt::DeadCodeElimination() },
39 { constprop, new opt::ConstantPropogation() },
40 { inlining , new opt::MethodInlining() },
Chris Lattner9effd692001-10-18 20:06:45 +000041 { mergecons, new ConstantMerge() },
Chris Lattner6db0f472001-10-18 01:31:43 +000042 { strip , new opt::SymbolStripping() },
43 { mstrip , new opt::FullSymbolStripping() },
44 { indvars , new opt::InductionVariableCannonicalize() },
45 { sccp , new opt::SCCPPass() },
46 { adce , new opt::AgressiveDCE() },
Chris Lattner068f4872001-11-01 02:41:09 +000047 { raise , new RaisePointerReferences() },
Chris Lattner0eafc312001-10-18 06:05:15 +000048 { trace , new InsertTraceCode(true, true) },
49 { tracem , new InsertTraceCode(false, true) },
50 { print , new PrintModulePass("Current Method: \n",&cerr) },
Chris Lattnere166fe12001-10-31 04:29:44 +000051 { cleangcc , new CleanupGCCOutput() },
Chris Lattner8f367bd2001-07-23 02:35:57 +000052};
53
Chris Lattnera8e1fd32001-07-23 20:22:30 +000054cl::String InputFilename ("", "Load <arg> file to optimize", cl::NoFlags, "-");
55cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "");
56cl::Flag Force ("f", "Overwrite output files", cl::NoFlags, false);
Chris Lattner8f367bd2001-07-23 02:35:57 +000057cl::Flag Quiet ("q", "Don't print modifying pass names", 0, false);
Chris Lattnera8e1fd32001-07-23 20:22:30 +000058cl::Alias QuietA ("quiet", "Alias for -q", cl::NoFlags, Quiet);
Chris Lattner8f367bd2001-07-23 02:35:57 +000059cl::EnumList<enum Opts> OptimizationList(cl::NoFlags,
60 clEnumVal(dce , "Dead Code Elimination"),
61 clEnumVal(constprop, "Simple Constant Propogation"),
Chris Lattnerafb0cbb2001-07-23 23:02:51 +000062 clEnumValN(inlining , "inline", "Method Integration"),
Chris Lattner9effd692001-10-18 20:06:45 +000063 clEnumVal(mergecons, "Merge identical global constants"),
Chris Lattner8f367bd2001-07-23 02:35:57 +000064 clEnumVal(strip , "Strip Symbols"),
65 clEnumVal(mstrip , "Strip Module Symbols"),
66 clEnumVal(indvars , "Simplify Induction Variables"),
67 clEnumVal(sccp , "Sparse Conditional Constant Propogation"),
Chris Lattner8f367bd2001-07-23 02:35:57 +000068 clEnumVal(adce , "Agressive DCE"),
Chris Lattnere166fe12001-10-31 04:29:44 +000069 clEnumVal(cleangcc , "Cleanup GCC Output"),
Chris Lattner8f367bd2001-07-23 02:35:57 +000070 clEnumVal(raise , "Raise to Higher Level"),
Chris Lattner0eafc312001-10-18 06:05:15 +000071 clEnumVal(trace , "Insert BB & Method trace code"),
72 clEnumVal(tracem , "Insert Method trace code only"),
73 clEnumVal(print , "Print working method to stderr"),
Chris Lattner8f367bd2001-07-23 02:35:57 +0000740);
75
76
Chris Lattner00950542001-06-06 20:29:01 +000077int main(int argc, char **argv) {
Chris Lattner8f367bd2001-07-23 02:35:57 +000078 cl::ParseCommandLineOptions(argc, argv,
79 " llvm .bc -> .bc modular optimizer\n");
80
Chris Lattner1e78f362001-07-23 19:27:24 +000081 Module *C = ParseBytecodeFile(InputFilename);
Chris Lattner00950542001-06-06 20:29:01 +000082 if (C == 0) {
83 cerr << "bytecode didn't read correctly.\n";
84 return 1;
85 }
86
Chris Lattner8f367bd2001-07-23 02:35:57 +000087 for (unsigned i = 0; i < OptimizationList.size(); ++i) {
88 enum Opts Opt = OptimizationList[i];
Chris Lattner00950542001-06-06 20:29:01 +000089
Chris Lattner00950542001-06-06 20:29:01 +000090 unsigned j;
Chris Lattner8f367bd2001-07-23 02:35:57 +000091 for (j = 0; j < sizeof(OptTable)/sizeof(OptTable[0]); ++j) {
92 if (Opt == OptTable[j].OptID) {
Chris Lattner6db0f472001-10-18 01:31:43 +000093 if (OptTable[j].ThePass->run(C) && !Quiet)
Chris Lattner8f367bd2001-07-23 02:35:57 +000094 cerr << OptimizationList.getArgName(Opt)
95 << " pass made modifications!\n";
Chris Lattner00950542001-06-06 20:29:01 +000096 break;
97 }
98 }
99
100 if (j == sizeof(OptTable)/sizeof(OptTable[0]))
Chris Lattner8f367bd2001-07-23 02:35:57 +0000101 cerr << "Optimization tables inconsistent!!\n";
Chris Lattner00950542001-06-06 20:29:01 +0000102 }
103
Chris Lattner8f367bd2001-07-23 02:35:57 +0000104 ostream *Out = &cout; // Default to printing to stdout...
Chris Lattner1e78f362001-07-23 19:27:24 +0000105 if (OutputFilename != "") {
106 Out = new ofstream(OutputFilename.c_str(),
107 (Force ? 0 : ios::noreplace)|ios::out);
Chris Lattner00950542001-06-06 20:29:01 +0000108 if (!Out->good()) {
Chris Lattner1e78f362001-07-23 19:27:24 +0000109 cerr << "Error opening " << OutputFilename << "!\n";
Chris Lattner00950542001-06-06 20:29:01 +0000110 delete C;
111 return 1;
112 }
113 }
114
115 // Okay, we're done now... write out result...
116 WriteBytecodeToFile(C, *Out);
117 delete C;
118
119 if (Out != &cout) delete Out;
120 return 0;
121}