blob: 463b37c387f7eaec19fdaa8d0b18a5239adcd9fc [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"
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000010#include "llvm/PassManager.h"
Chris Lattner00950542001-06-06 20:29:01 +000011#include "llvm/Bytecode/Reader.h"
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000012#include "llvm/Bytecode/WriteBytecodePass.h"
Chris Lattnerffa6f9c2001-10-19 15:39:14 +000013#include "llvm/Assembly/PrintModulePass.h"
Chris Lattner22d26d72002-02-20 17:56:53 +000014#include "llvm/Analysis/Verifier.h"
Chris Lattnere04f4b62002-05-10 15:43:07 +000015#include "llvm/Target/TargetData.h"
Chris Lattner2053a2a2002-07-26 21:09:32 +000016#include "llvm/Support/PassNameParser.h"
Chris Lattner76d12292002-04-18 19:55:25 +000017#include "Support/Signals.h"
Chris Lattner73e11d72001-10-18 06:13:08 +000018#include <fstream>
Chris Lattner63202322001-11-26 19:22:39 +000019#include <memory>
Chris Lattnerc0ce68b2002-07-23 18:12:22 +000020#include <algorithm>
Anand Shukla63aaa112002-06-25 21:43:28 +000021
22using std::cerr;
Chris Lattnerc7a09852002-07-25 16:31:09 +000023using std::string;
Chris Lattnere04f4b62002-05-10 15:43:07 +000024
Chris Lattner9d6e7eb2002-04-12 18:21:13 +000025
Chris Lattnerc0ce68b2002-07-23 18:12:22 +000026// The OptimizationList is automatically populated with registered Passes by the
27// PassNameParser.
28//
Chris Lattner2053a2a2002-07-26 21:09:32 +000029static cl::list<const PassInfo*, bool,
30 FilteredPassNameParser<PassInfo::Optimization> >
Chris Lattnerc0ce68b2002-07-23 18:12:22 +000031OptimizationList(cl::desc("Optimizations available:"));
32
33
34// Other command line options...
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000035//
Chris Lattner5ff62e92002-07-22 02:10:13 +000036static cl::opt<string>
37InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
38
39static cl::opt<string>
40OutputFilename("o", cl::desc("Override output filename"),
41 cl::value_desc("filename"));
42
43static cl::opt<bool>
44Force("f", cl::desc("Overwrite output files"));
45
46static cl::opt<bool>
47PrintEachXForm("p", cl::desc("Print module after each transformation"));
48
49static cl::opt<bool>
50Quiet("q", cl::desc("Don't print modifying pass names"));
51
52static cl::alias
53QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet));
54
Chris Lattner0be41012002-02-01 04:54:11 +000055
Chris Lattnerc0ce68b2002-07-23 18:12:22 +000056//===----------------------------------------------------------------------===//
57// main for opt
58//
Chris Lattner00950542001-06-06 20:29:01 +000059int main(int argc, char **argv) {
Chris Lattner8f367bd2001-07-23 02:35:57 +000060 cl::ParseCommandLineOptions(argc, argv,
61 " llvm .bc -> .bc modular optimizer\n");
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000062
Chris Lattnerc0ce68b2002-07-23 18:12:22 +000063 // FIXME: This should be parameterizable eventually for different target
64 // types...
65 TargetData TD("opt target");
66
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000067 // Load the input module...
Chris Lattner63202322001-11-26 19:22:39 +000068 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
69 if (M.get() == 0) {
Chris Lattner00950542001-06-06 20:29:01 +000070 cerr << "bytecode didn't read correctly.\n";
71 return 1;
72 }
73
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000074 // Figure out what stream we are supposed to write to...
Chris Lattner697954c2002-01-20 22:54:45 +000075 std::ostream *Out = &std::cout; // Default to printing to stdout...
Chris Lattner1e78f362001-07-23 19:27:24 +000076 if (OutputFilename != "") {
Chris Lattner888912d2002-01-22 21:07:24 +000077 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner697954c2002-01-20 22:54:45 +000078 // If force is not specified, make sure not to overwrite a file!
79 cerr << "Error opening '" << OutputFilename << "': File exists!\n"
80 << "Use -f command line argument to force output\n";
81 return 1;
82 }
83 Out = new std::ofstream(OutputFilename.c_str());
84
Chris Lattner00950542001-06-06 20:29:01 +000085 if (!Out->good()) {
Chris Lattner1e78f362001-07-23 19:27:24 +000086 cerr << "Error opening " << OutputFilename << "!\n";
Chris Lattner00950542001-06-06 20:29:01 +000087 return 1;
88 }
Chris Lattner76d12292002-04-18 19:55:25 +000089
90 // Make sure that the Output file gets unlink'd from the disk if we get a
91 // SIGINT
92 RemoveFileOnSignal(OutputFilename);
Chris Lattner00950542001-06-06 20:29:01 +000093 }
94
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000095 // Create a PassManager to hold and optimize the collection of passes we are
96 // about to build...
97 //
98 PassManager Passes;
Chris Lattner00950542001-06-06 20:29:01 +000099
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000100 // Create a new optimization pass for each one specified on the command line
101 for (unsigned i = 0; i < OptimizationList.size(); ++i) {
Chris Lattnerc0ce68b2002-07-23 18:12:22 +0000102 const PassInfo *Opt = OptimizationList[i];
103
104 if (Opt->getNormalCtor())
105 Passes.add(Opt->getNormalCtor()());
106 else if (Opt->getDataCtor())
107 Passes.add(Opt->getDataCtor()(TD)); // Pass dummy target data...
108 else
109 cerr << "Cannot create pass: " << Opt->getPassName() << "\n";
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000110
111 if (PrintEachXForm)
Chris Lattnerc0ce68b2002-07-23 18:12:22 +0000112 Passes.add(new PrintModulePass(&cerr));
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000113 }
114
Chris Lattner22d26d72002-02-20 17:56:53 +0000115 // Check that the module is well formed on completion of optimization
116 Passes.add(createVerifierPass());
117
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000118 // Write bytecode out to disk or cout as the last step...
119 Passes.add(new WriteBytecodePass(Out, Out != &std::cout));
120
121 // Now that we have all of the passes ready, run them.
Chris Lattner7e708292002-06-25 16:13:24 +0000122 if (Passes.run(*M.get()) && !Quiet)
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000123 cerr << "Program modified.\n";
124
Chris Lattner00950542001-06-06 20:29:01 +0000125 return 0;
126}