blob: a0cf140c4de53e38700bceea128585f2759709d5 [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.
19// TODO: Add a -h command line arg that prints all available optimizations
20// TODO: Add a -q command line arg that quiets "XXX pass made modifications"
21//
22//===------------------------------------------------------------------------===
23
24#include <iostream.h>
25#include <fstream.h>
26#include "llvm/Module.h"
27#include "llvm/Bytecode/Reader.h"
28#include "llvm/Bytecode/Writer.h"
29#include "llvm/Tools/CommandLine.h"
30#include "llvm/Opt/AllOpts.h"
31
32struct {
33 const string ArgName, Name;
34 bool (*OptPtr)(Module *C);
35} OptTable[] = {
36 { "-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};
42
43int main(int argc, char **argv) {
44 ToolCommandLine Opts(argc, argv, false);
45 bool Quiet = false;
46
47 for (int i = 1; i < argc; i++) {
48 if (string(argv[i]) == string("--help")) {
49 cerr << argv[0] << " usage:\n"
50 << " " << argv[0] << " --help - Print this usage information\n";
51 return 1;
52 } else if (string(argv[i]) == string("-q")) {
53 Quiet = true; argv[i] = 0;
54 }
55 }
56
57 ostream *Out = &cout; // Default to printing to stdout...
58
59 Module *C = ParseBytecodeFile(Opts.getInputFilename());
60 if (C == 0) {
61 cerr << "bytecode didn't read correctly.\n";
62 return 1;
63 }
64
65
66 for (int i = 1; i < argc; i++) {
67 if (argv[i] == 0) continue;
68 unsigned j;
69 for (j = 0; j < sizeof(OptTable)/sizeof(OptTable[0]); j++) {
70 if (string(argv[i]) == OptTable[j].ArgName) {
71 if (OptTable[j].OptPtr(C) && !Quiet)
72 cerr << OptTable[j].Name << " pass made modifications!\n";
73 break;
74 }
75 }
76
77 if (j == sizeof(OptTable)/sizeof(OptTable[0]))
78 cerr << "'" << argv[i] << "' argument unrecognized: ignored\n";
79 }
80
81 if (Opts.getOutputFilename() != "-") {
82 Out = new ofstream(Opts.getOutputFilename().c_str(),
83 (Opts.getForce() ? 0 : ios::noreplace)|ios::out);
84 if (!Out->good()) {
85 cerr << "Error opening " << Opts.getOutputFilename()
86 << "!\n";
87 delete C;
88 return 1;
89 }
90 }
91
92 // Okay, we're done now... write out result...
93 WriteBytecodeToFile(C, *Out);
94 delete C;
95
96 if (Out != &cout) delete Out;
97 return 0;
98}