blob: 973a78ff8d3f2f95d09f28f564d21d32c98ed68f [file] [log] [blame]
Chris Lattner00950542001-06-06 20:29:01 +00001//===------------------------------------------------------------------------===
2// LLVM 'OPT' UTILITY
3//
4// This utility may be invoked in the following manner:
5// opt --help - Output information about command line switches
6// opt [options] -dce - Run a dead code elimination pass on input
7// bytecodes
8// opt [options] -constprop - Run a constant propogation pass on input
9// bytecodes
10// opt [options] -inline - Run a method inlining pass on input bytecodes
11// opt [options] -strip - Strip symbol tables out of methods
12// opt [options] -mstrip - Strip module & method symbol tables
13//
14// Optimizations may be specified an arbitrary number of times on the command
15// line, they are run in the order specified.
16//
17// TODO: Add a -all option to keep applying all optimizations until the program
18// stops permuting.
Chris Lattner00950542001-06-06 20:29:01 +000019//
20//===------------------------------------------------------------------------===
21
22#include <iostream.h>
23#include <fstream.h>
24#include "llvm/Module.h"
25#include "llvm/Bytecode/Reader.h"
26#include "llvm/Bytecode/Writer.h"
Chris Lattner57dbb3a2001-07-23 17:46:59 +000027#include "llvm/Support/CommandLine.h"
Chris Lattner95781b62001-06-30 06:38:31 +000028#include "llvm/Optimizations/AllOpts.h"
29
30using namespace opt;
Chris Lattner00950542001-06-06 20:29:01 +000031
Chris Lattner8f367bd2001-07-23 02:35:57 +000032enum Opts {
33 // Basic optimizations
34 dce, constprop, inlining, strip, mstrip,
35
36 // More powerful optimizations
Chris Lattner6dcf92a2001-09-07 16:59:35 +000037 indvars, sccp, adce, raise,
Chris Lattner00950542001-06-06 20:29:01 +000038};
39
Chris Lattner8f367bd2001-07-23 02:35:57 +000040struct {
41 enum Opts OptID;
42 bool (*OptPtr)(Module *C);
43} OptTable[] = {
44 { dce , DoDeadCodeElimination },
45 { constprop, DoConstantPropogation },
46 { inlining , DoMethodInlining },
47 { strip , DoSymbolStripping },
48 { mstrip , DoFullSymbolStripping },
49 { indvars , DoInductionVariableCannonicalize },
50 { sccp , DoSCCP },
Chris Lattner8f367bd2001-07-23 02:35:57 +000051 { adce , DoADCE },
52 { raise , DoRaiseRepresentation },
53};
54
Chris Lattnera8e1fd32001-07-23 20:22:30 +000055cl::String InputFilename ("", "Load <arg> file to optimize", cl::NoFlags, "-");
56cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "");
57cl::Flag Force ("f", "Overwrite output files", cl::NoFlags, false);
Chris Lattner8f367bd2001-07-23 02:35:57 +000058cl::Flag Quiet ("q", "Don't print modifying pass names", 0, false);
Chris Lattnera8e1fd32001-07-23 20:22:30 +000059cl::Alias QuietA ("quiet", "Alias for -q", cl::NoFlags, Quiet);
Chris Lattner8f367bd2001-07-23 02:35:57 +000060cl::EnumList<enum Opts> OptimizationList(cl::NoFlags,
61 clEnumVal(dce , "Dead Code Elimination"),
62 clEnumVal(constprop, "Simple Constant Propogation"),
Chris Lattnerafb0cbb2001-07-23 23:02:51 +000063 clEnumValN(inlining , "inline", "Method Integration"),
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"),
69 clEnumVal(raise , "Raise to Higher Level"),
700);
71
72
Chris Lattner00950542001-06-06 20:29:01 +000073int main(int argc, char **argv) {
Chris Lattner8f367bd2001-07-23 02:35:57 +000074 cl::ParseCommandLineOptions(argc, argv,
75 " llvm .bc -> .bc modular optimizer\n");
76
Chris Lattner1e78f362001-07-23 19:27:24 +000077 Module *C = ParseBytecodeFile(InputFilename);
Chris Lattner00950542001-06-06 20:29:01 +000078 if (C == 0) {
79 cerr << "bytecode didn't read correctly.\n";
80 return 1;
81 }
82
Chris Lattner8f367bd2001-07-23 02:35:57 +000083 for (unsigned i = 0; i < OptimizationList.size(); ++i) {
84 enum Opts Opt = OptimizationList[i];
Chris Lattner00950542001-06-06 20:29:01 +000085
Chris Lattner00950542001-06-06 20:29:01 +000086 unsigned j;
Chris Lattner8f367bd2001-07-23 02:35:57 +000087 for (j = 0; j < sizeof(OptTable)/sizeof(OptTable[0]); ++j) {
88 if (Opt == OptTable[j].OptID) {
Chris Lattner00950542001-06-06 20:29:01 +000089 if (OptTable[j].OptPtr(C) && !Quiet)
Chris Lattner8f367bd2001-07-23 02:35:57 +000090 cerr << OptimizationList.getArgName(Opt)
91 << " pass made modifications!\n";
Chris Lattner00950542001-06-06 20:29:01 +000092 break;
93 }
94 }
95
96 if (j == sizeof(OptTable)/sizeof(OptTable[0]))
Chris Lattner8f367bd2001-07-23 02:35:57 +000097 cerr << "Optimization tables inconsistent!!\n";
Chris Lattner00950542001-06-06 20:29:01 +000098 }
99
Chris Lattner8f367bd2001-07-23 02:35:57 +0000100 ostream *Out = &cout; // Default to printing to stdout...
Chris Lattner1e78f362001-07-23 19:27:24 +0000101 if (OutputFilename != "") {
102 Out = new ofstream(OutputFilename.c_str(),
103 (Force ? 0 : ios::noreplace)|ios::out);
Chris Lattner00950542001-06-06 20:29:01 +0000104 if (!Out->good()) {
Chris Lattner1e78f362001-07-23 19:27:24 +0000105 cerr << "Error opening " << OutputFilename << "!\n";
Chris Lattner00950542001-06-06 20:29:01 +0000106 delete C;
107 return 1;
108 }
109 }
110
111 // Okay, we're done now... write out result...
112 WriteBytecodeToFile(C, *Out);
113 delete C;
114
115 if (Out != &cout) delete Out;
116 return 0;
117}