blob: f039a8a002ef24b8bb48ca0add81bb3c237354bc [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"
27#include "llvm/Tools/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
32struct {
33 const string ArgName, Name;
34 bool (*OptPtr)(Module *C);
35} OptTable[] = {
Chris Lattner241b0642001-06-27 23:37:58 +000036 { "-dce" , "Dead Code Elimination", DoDeadCodeElimination },
37 { "-constprop" , "Constant Propogation", DoConstantPropogation },
38 { "-inline" , "Method Inlining", DoMethodInlining },
39 { "-strip" , "Strip Symbols", DoSymbolStripping },
40 { "-mstrip" , "Strip Module Symbols", DoFullSymbolStripping },
41 { "-indvars" , "Simplify Induction Vars",DoInductionVariableCannonicalize },
Chris Lattner95781b62001-06-30 06:38:31 +000042 { "-sccp" , "Sparse Conditional Constant Prop", DoSCCP },
Chris Lattner241b0642001-06-27 23:37:58 +000043 { "-cpm" , "Constant Pool Merging", DoConstantPoolMerging },
Chris Lattner95781b62001-06-30 06:38:31 +000044 { "-adce" , "Agressive DCE", DoADCE },
Chris Lattner00950542001-06-06 20:29:01 +000045};
46
47int main(int argc, char **argv) {
48 ToolCommandLine Opts(argc, argv, false);
49 bool Quiet = false;
50
51 for (int i = 1; i < argc; i++) {
52 if (string(argv[i]) == string("--help")) {
53 cerr << argv[0] << " usage:\n"
54 << " " << argv[0] << " --help - Print this usage information\n";
Chris Lattner526f97e2001-06-13 19:55:50 +000055 for (unsigned j = 0; j < sizeof(OptTable)/sizeof(OptTable[0]); ++j) {
56 cerr << "\t" << OptTable[j].ArgName << "\t - Enable "
57 << OptTable[j].Name << endl;
58 }
Chris Lattner00950542001-06-06 20:29:01 +000059 return 1;
60 } else if (string(argv[i]) == string("-q")) {
61 Quiet = true; argv[i] = 0;
62 }
63 }
64
65 ostream *Out = &cout; // Default to printing to stdout...
66
67 Module *C = ParseBytecodeFile(Opts.getInputFilename());
68 if (C == 0) {
69 cerr << "bytecode didn't read correctly.\n";
70 return 1;
71 }
72
73
74 for (int i = 1; i < argc; i++) {
75 if (argv[i] == 0) continue;
76 unsigned j;
77 for (j = 0; j < sizeof(OptTable)/sizeof(OptTable[0]); j++) {
78 if (string(argv[i]) == OptTable[j].ArgName) {
79 if (OptTable[j].OptPtr(C) && !Quiet)
80 cerr << OptTable[j].Name << " pass made modifications!\n";
81 break;
82 }
83 }
84
85 if (j == sizeof(OptTable)/sizeof(OptTable[0]))
86 cerr << "'" << argv[i] << "' argument unrecognized: ignored\n";
87 }
88
89 if (Opts.getOutputFilename() != "-") {
90 Out = new ofstream(Opts.getOutputFilename().c_str(),
91 (Opts.getForce() ? 0 : ios::noreplace)|ios::out);
92 if (!Out->good()) {
93 cerr << "Error opening " << Opts.getOutputFilename()
94 << "!\n";
95 delete C;
96 return 1;
97 }
98 }
99
100 // Okay, we're done now... write out result...
101 WriteBytecodeToFile(C, *Out);
102 delete C;
103
104 if (Out != &cout) delete Out;
105 return 0;
106}