blob: 940108c6058e3ed696c3fc95507f4613e7ea1577 [file] [log] [blame]
Chris Lattner0eafc312001-10-18 06:05:15 +00001//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00002// LLVM 'OPT' UTILITY
3//
Chris Lattner00950542001-06-06 20:29:01 +00004// Optimizations may be specified an arbitrary number of times on the command
5// line, they are run in the order specified.
6//
Chris Lattner0eafc312001-10-18 06:05:15 +00007//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00008
Chris Lattner00950542001-06-06 20:29:01 +00009#include "llvm/Module.h"
10#include "llvm/Bytecode/Reader.h"
11#include "llvm/Bytecode/Writer.h"
Chris Lattner57dbb3a2001-07-23 17:46:59 +000012#include "llvm/Support/CommandLine.h"
Chris Lattner95781b62001-06-30 06:38:31 +000013#include "llvm/Optimizations/AllOpts.h"
Chris Lattner0eafc312001-10-18 06:05:15 +000014#include "llvm/Transforms/Instrumentation/TraceValues.h"
Chris Lattnerffa6f9c2001-10-19 15:39:14 +000015#include "llvm/Assembly/PrintModulePass.h"
Chris Lattner9effd692001-10-18 20:06:45 +000016#include "llvm/Transforms/ConstantMerge.h"
Chris Lattnere166fe12001-10-31 04:29:44 +000017#include "llvm/Transforms/CleanupGCCOutput.h"
Chris Lattner73e11d72001-10-18 06:13:08 +000018#include <fstream>
Chris Lattner95781b62001-06-30 06:38:31 +000019
20using namespace opt;
Chris Lattner00950542001-06-06 20:29:01 +000021
Chris Lattner8f367bd2001-07-23 02:35:57 +000022enum Opts {
23 // Basic optimizations
Chris Lattner9effd692001-10-18 20:06:45 +000024 dce, constprop, inlining, mergecons, strip, mstrip,
Chris Lattner8f367bd2001-07-23 02:35:57 +000025
Chris Lattner0eafc312001-10-18 06:05:15 +000026 // Miscellaneous Transformations
Chris Lattnere166fe12001-10-31 04:29:44 +000027 trace, tracem, print, cleangcc,
Chris Lattner0eafc312001-10-18 06:05:15 +000028
Chris Lattner8f367bd2001-07-23 02:35:57 +000029 // More powerful optimizations
Chris Lattner6dcf92a2001-09-07 16:59:35 +000030 indvars, sccp, adce, raise,
Chris Lattner00950542001-06-06 20:29:01 +000031};
32
Chris Lattner8f367bd2001-07-23 02:35:57 +000033struct {
34 enum Opts OptID;
Chris Lattner6db0f472001-10-18 01:31:43 +000035 Pass *ThePass;
Chris Lattner8f367bd2001-07-23 02:35:57 +000036} OptTable[] = {
Chris Lattner6db0f472001-10-18 01:31:43 +000037 { dce , new opt::DeadCodeElimination() },
38 { constprop, new opt::ConstantPropogation() },
39 { inlining , new opt::MethodInlining() },
Chris Lattner9effd692001-10-18 20:06:45 +000040 { mergecons, new ConstantMerge() },
Chris Lattner6db0f472001-10-18 01:31:43 +000041 { strip , new opt::SymbolStripping() },
42 { mstrip , new opt::FullSymbolStripping() },
43 { indvars , new opt::InductionVariableCannonicalize() },
44 { sccp , new opt::SCCPPass() },
45 { adce , new opt::AgressiveDCE() },
46 { raise , new opt::RaiseRepresentation() },
Chris Lattner0eafc312001-10-18 06:05:15 +000047 { trace , new InsertTraceCode(true, true) },
48 { tracem , new InsertTraceCode(false, true) },
49 { print , new PrintModulePass("Current Method: \n",&cerr) },
Chris Lattnere166fe12001-10-31 04:29:44 +000050 { cleangcc , new CleanupGCCOutput() },
Chris Lattner8f367bd2001-07-23 02:35:57 +000051};
52
Chris Lattnera8e1fd32001-07-23 20:22:30 +000053cl::String InputFilename ("", "Load <arg> file to optimize", cl::NoFlags, "-");
54cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "");
55cl::Flag Force ("f", "Overwrite output files", cl::NoFlags, false);
Chris Lattner8f367bd2001-07-23 02:35:57 +000056cl::Flag Quiet ("q", "Don't print modifying pass names", 0, false);
Chris Lattnera8e1fd32001-07-23 20:22:30 +000057cl::Alias QuietA ("quiet", "Alias for -q", cl::NoFlags, Quiet);
Chris Lattner8f367bd2001-07-23 02:35:57 +000058cl::EnumList<enum Opts> OptimizationList(cl::NoFlags,
59 clEnumVal(dce , "Dead Code Elimination"),
60 clEnumVal(constprop, "Simple Constant Propogation"),
Chris Lattnerafb0cbb2001-07-23 23:02:51 +000061 clEnumValN(inlining , "inline", "Method Integration"),
Chris Lattner9effd692001-10-18 20:06:45 +000062 clEnumVal(mergecons, "Merge identical global constants"),
Chris Lattner8f367bd2001-07-23 02:35:57 +000063 clEnumVal(strip , "Strip Symbols"),
64 clEnumVal(mstrip , "Strip Module Symbols"),
65 clEnumVal(indvars , "Simplify Induction Variables"),
66 clEnumVal(sccp , "Sparse Conditional Constant Propogation"),
Chris Lattner8f367bd2001-07-23 02:35:57 +000067 clEnumVal(adce , "Agressive DCE"),
Chris Lattnere166fe12001-10-31 04:29:44 +000068 clEnumVal(cleangcc , "Cleanup GCC Output"),
Chris Lattner8f367bd2001-07-23 02:35:57 +000069 clEnumVal(raise , "Raise to Higher Level"),
Chris Lattner0eafc312001-10-18 06:05:15 +000070 clEnumVal(trace , "Insert BB & Method trace code"),
71 clEnumVal(tracem , "Insert Method trace code only"),
72 clEnumVal(print , "Print working method to stderr"),
Chris Lattner8f367bd2001-07-23 02:35:57 +0000730);
74
75
Chris Lattner00950542001-06-06 20:29:01 +000076int main(int argc, char **argv) {
Chris Lattner8f367bd2001-07-23 02:35:57 +000077 cl::ParseCommandLineOptions(argc, argv,
78 " llvm .bc -> .bc modular optimizer\n");
79
Chris Lattner1e78f362001-07-23 19:27:24 +000080 Module *C = ParseBytecodeFile(InputFilename);
Chris Lattner00950542001-06-06 20:29:01 +000081 if (C == 0) {
82 cerr << "bytecode didn't read correctly.\n";
83 return 1;
84 }
85
Chris Lattner8f367bd2001-07-23 02:35:57 +000086 for (unsigned i = 0; i < OptimizationList.size(); ++i) {
87 enum Opts Opt = OptimizationList[i];
Chris Lattner00950542001-06-06 20:29:01 +000088
Chris Lattner00950542001-06-06 20:29:01 +000089 unsigned j;
Chris Lattner8f367bd2001-07-23 02:35:57 +000090 for (j = 0; j < sizeof(OptTable)/sizeof(OptTable[0]); ++j) {
91 if (Opt == OptTable[j].OptID) {
Chris Lattner6db0f472001-10-18 01:31:43 +000092 if (OptTable[j].ThePass->run(C) && !Quiet)
Chris Lattner8f367bd2001-07-23 02:35:57 +000093 cerr << OptimizationList.getArgName(Opt)
94 << " pass made modifications!\n";
Chris Lattner00950542001-06-06 20:29:01 +000095 break;
96 }
97 }
98
99 if (j == sizeof(OptTable)/sizeof(OptTable[0]))
Chris Lattner8f367bd2001-07-23 02:35:57 +0000100 cerr << "Optimization tables inconsistent!!\n";
Chris Lattner00950542001-06-06 20:29:01 +0000101 }
102
Chris Lattner8f367bd2001-07-23 02:35:57 +0000103 ostream *Out = &cout; // Default to printing to stdout...
Chris Lattner1e78f362001-07-23 19:27:24 +0000104 if (OutputFilename != "") {
105 Out = new ofstream(OutputFilename.c_str(),
106 (Force ? 0 : ios::noreplace)|ios::out);
Chris Lattner00950542001-06-06 20:29:01 +0000107 if (!Out->good()) {
Chris Lattner1e78f362001-07-23 19:27:24 +0000108 cerr << "Error opening " << OutputFilename << "!\n";
Chris Lattner00950542001-06-06 20:29:01 +0000109 delete C;
110 return 1;
111 }
112 }
113
114 // Okay, we're done now... write out result...
115 WriteBytecodeToFile(C, *Out);
116 delete C;
117
118 if (Out != &cout) delete Out;
119 return 0;
120}