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