aboutsummaryrefslogtreecommitdiffstats
path: root/tools/opt
diff options
context:
space:
mode:
Diffstat (limited to 'tools/opt')
-rw-r--r--tools/opt/Makefile10
-rw-r--r--tools/opt/opt.cpp98
-rwxr-xr-xtools/opt/test.sh4
-rwxr-xr-xtools/opt/testinline.sh3
-rwxr-xr-xtools/opt/teststrip.sh3
5 files changed, 118 insertions, 0 deletions
diff --git a/tools/opt/Makefile b/tools/opt/Makefile
new file mode 100644
index 0000000..71bdcb8
--- /dev/null
+++ b/tools/opt/Makefile
@@ -0,0 +1,10 @@
+LEVEL = ../..
+include $(LEVEL)/Makefile.common
+
+all:: opt
+clean ::
+ rm -f opt
+
+opt : $(ObjectsG)
+ $(LinkG) -o $@ $(ObjectsG) -lvmcore -lanalysis -lbcreader -lbcwriter \
+ -lopt -lasmwriter
diff --git a/tools/opt/opt.cpp b/tools/opt/opt.cpp
new file mode 100644
index 0000000..a0cf140
--- /dev/null
+++ b/tools/opt/opt.cpp
@@ -0,0 +1,98 @@
+//===------------------------------------------------------------------------===
+// LLVM 'OPT' UTILITY
+//
+// This utility may be invoked in the following manner:
+// opt --help - Output information about command line switches
+// opt [options] -dce - Run a dead code elimination pass on input
+// bytecodes
+// opt [options] -constprop - Run a constant propogation pass on input
+// bytecodes
+// opt [options] -inline - Run a method inlining pass on input bytecodes
+// opt [options] -strip - Strip symbol tables out of methods
+// opt [options] -mstrip - Strip module & method symbol tables
+//
+// Optimizations may be specified an arbitrary number of times on the command
+// line, they are run in the order specified.
+//
+// TODO: Add a -all option to keep applying all optimizations until the program
+// stops permuting.
+// TODO: Add a -h command line arg that prints all available optimizations
+// TODO: Add a -q command line arg that quiets "XXX pass made modifications"
+//
+//===------------------------------------------------------------------------===
+
+#include <iostream.h>
+#include <fstream.h>
+#include "llvm/Module.h"
+#include "llvm/Bytecode/Reader.h"
+#include "llvm/Bytecode/Writer.h"
+#include "llvm/Tools/CommandLine.h"
+#include "llvm/Opt/AllOpts.h"
+
+struct {
+ const string ArgName, Name;
+ bool (*OptPtr)(Module *C);
+} OptTable[] = {
+ { "-dce", "Dead Code Elimination", DoDeadCodeElimination },
+ { "-constprop","Constant Propogation", DoConstantPropogation },
+ { "-inline" ,"Method Inlining", DoMethodInlining },
+ { "-strip" ,"Strip Symbols", DoSymbolStripping },
+ { "-mstrip" ,"Strip Module Symbols", DoFullSymbolStripping },
+};
+
+int main(int argc, char **argv) {
+ ToolCommandLine Opts(argc, argv, false);
+ bool Quiet = false;
+
+ for (int i = 1; i < argc; i++) {
+ if (string(argv[i]) == string("--help")) {
+ cerr << argv[0] << " usage:\n"
+ << " " << argv[0] << " --help - Print this usage information\n";
+ return 1;
+ } else if (string(argv[i]) == string("-q")) {
+ Quiet = true; argv[i] = 0;
+ }
+ }
+
+ ostream *Out = &cout; // Default to printing to stdout...
+
+ Module *C = ParseBytecodeFile(Opts.getInputFilename());
+ if (C == 0) {
+ cerr << "bytecode didn't read correctly.\n";
+ return 1;
+ }
+
+
+ for (int i = 1; i < argc; i++) {
+ if (argv[i] == 0) continue;
+ unsigned j;
+ for (j = 0; j < sizeof(OptTable)/sizeof(OptTable[0]); j++) {
+ if (string(argv[i]) == OptTable[j].ArgName) {
+ if (OptTable[j].OptPtr(C) && !Quiet)
+ cerr << OptTable[j].Name << " pass made modifications!\n";
+ break;
+ }
+ }
+
+ if (j == sizeof(OptTable)/sizeof(OptTable[0]))
+ cerr << "'" << argv[i] << "' argument unrecognized: ignored\n";
+ }
+
+ if (Opts.getOutputFilename() != "-") {
+ Out = new ofstream(Opts.getOutputFilename().c_str(),
+ (Opts.getForce() ? 0 : ios::noreplace)|ios::out);
+ if (!Out->good()) {
+ cerr << "Error opening " << Opts.getOutputFilename()
+ << "!\n";
+ delete C;
+ return 1;
+ }
+ }
+
+ // Okay, we're done now... write out result...
+ WriteBytecodeToFile(C, *Out);
+ delete C;
+
+ if (Out != &cout) delete Out;
+ return 0;
+}
diff --git a/tools/opt/test.sh b/tools/opt/test.sh
new file mode 100755
index 0000000..b6d13f4
--- /dev/null
+++ b/tools/opt/test.sh
@@ -0,0 +1,4 @@
+#!/bin/sh
+
+../as/as < ../../test/$1 | ./opt -constprop -dce | ../dis/dis
+
diff --git a/tools/opt/testinline.sh b/tools/opt/testinline.sh
new file mode 100755
index 0000000..ff16a66
--- /dev/null
+++ b/tools/opt/testinline.sh
@@ -0,0 +1,3 @@
+#!/bin/sh
+
+../as/as < ../../test/$1 | ./opt -inline -constprop -dce | dis
diff --git a/tools/opt/teststrip.sh b/tools/opt/teststrip.sh
new file mode 100755
index 0000000..2cff3bf
--- /dev/null
+++ b/tools/opt/teststrip.sh
@@ -0,0 +1,3 @@
+#!/bin/sh
+
+../as/as < ../../test/$1 | ./opt -strip | dis