blob: fd339d92d98d91ecc49ef7c6553a2c0b3a3ea61c [file] [log] [blame]
Chris Lattner76351aa2004-04-02 05:06:57 +00001//===- opt.cpp - The LLVM Modular Optimizer -------------------------------===//
John Criswell7c0e0222003-10-20 17:47:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
Chris Lattner00950542001-06-06 20:29:01 +000010// Optimizations may be specified an arbitrary number of times on the command
11// line, they are run in the order specified.
12//
Chris Lattner0eafc312001-10-18 06:05:15 +000013//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +000014
Chris Lattner00950542001-06-06 20:29:01 +000015#include "llvm/Module.h"
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000016#include "llvm/PassManager.h"
Chris Lattner00950542001-06-06 20:29:01 +000017#include "llvm/Bytecode/Reader.h"
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000018#include "llvm/Bytecode/WriteBytecodePass.h"
Chris Lattnerffa6f9c2001-10-19 15:39:14 +000019#include "llvm/Assembly/PrintModulePass.h"
Chris Lattner22d26d72002-02-20 17:56:53 +000020#include "llvm/Analysis/Verifier.h"
Vikram S. Adve18fdfc42002-09-16 16:09:43 +000021#include "llvm/Target/TargetMachine.h"
Chris Lattner2053a2a2002-07-26 21:09:32 +000022#include "llvm/Support/PassNameParser.h"
Chris Lattnerbed85ff2004-05-27 05:41:36 +000023#include "llvm/System/Signals.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000024#include "llvm/Support/PluginLoader.h"
25#include "llvm/Support/SystemUtils.h"
Jeff Cohen4b807e02005-01-06 06:02:53 +000026#include "llvm/Transforms/LinkAllPasses.h"
Chris Lattner73e11d72001-10-18 06:13:08 +000027#include <fstream>
Chris Lattner63202322001-11-26 19:22:39 +000028#include <memory>
Chris Lattnerc0ce68b2002-07-23 18:12:22 +000029#include <algorithm>
Anand Shukla63aaa112002-06-25 21:43:28 +000030
Brian Gaeked0fde302003-11-11 22:41:34 +000031using namespace llvm;
Chris Lattner9d6e7eb2002-04-12 18:21:13 +000032
Chris Lattnerc0ce68b2002-07-23 18:12:22 +000033// The OptimizationList is automatically populated with registered Passes by the
34// PassNameParser.
35//
Chris Lattner2053a2a2002-07-26 21:09:32 +000036static cl::list<const PassInfo*, bool,
37 FilteredPassNameParser<PassInfo::Optimization> >
Chris Lattnerc0ce68b2002-07-23 18:12:22 +000038OptimizationList(cl::desc("Optimizations available:"));
39
40
41// Other command line options...
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000042//
Chris Lattner6c8103f2003-05-22 20:13:16 +000043static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000044InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
45
Chris Lattner6c8103f2003-05-22 20:13:16 +000046static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000047OutputFilename("o", cl::desc("Override output filename"),
Chris Lattnerb592fc22003-12-10 14:41:33 +000048 cl::value_desc("filename"), cl::init("-"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000049
50static cl::opt<bool>
51Force("f", cl::desc("Overwrite output files"));
52
53static cl::opt<bool>
54PrintEachXForm("p", cl::desc("Print module after each transformation"));
55
56static cl::opt<bool>
Chris Lattnerddd5b412003-02-26 20:00:41 +000057NoOutput("disable-output",
58 cl::desc("Do not write result bytecode file"), cl::Hidden);
Chris Lattnerd70b68e2003-02-12 18:43:33 +000059
60static cl::opt<bool>
Chris Lattnerddd5b412003-02-26 20:00:41 +000061NoVerify("disable-verify", cl::desc("Do not verify result module"), cl::Hidden);
Chris Lattnerf3bafc12003-02-12 18:45:08 +000062
63static cl::opt<bool>
Chris Lattner3153e4f2004-05-27 20:32:10 +000064Quiet("q", cl::desc("Obsolete option"), cl::Hidden);
Chris Lattner5ff62e92002-07-22 02:10:13 +000065
Reid Spencerec7eb452004-05-27 16:28:54 +000066static cl::alias
67QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet));
68
Chris Lattner0be41012002-02-01 04:54:11 +000069
Chris Lattnerc0ce68b2002-07-23 18:12:22 +000070//===----------------------------------------------------------------------===//
71// main for opt
72//
Chris Lattner00950542001-06-06 20:29:01 +000073int main(int argc, char **argv) {
Reid Spencer1ef8bda2004-12-30 05:36:08 +000074 try {
75 cl::ParseCommandLineOptions(argc, argv,
76 " llvm .bc -> .bc modular optimizer\n");
77 sys::PrintStackTraceOnErrorSignal();
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000078
Reid Spencer1ef8bda2004-12-30 05:36:08 +000079 // Allocate a full target machine description only if necessary...
80 // FIXME: The choice of target should be controllable on the command line.
81 std::auto_ptr<TargetMachine> target;
Vikram S. Adve18fdfc42002-09-16 16:09:43 +000082
Reid Spencer1ef8bda2004-12-30 05:36:08 +000083 TargetMachine* TM = NULL;
84 std::string ErrorMessage;
Vikram S. Adve18fdfc42002-09-16 16:09:43 +000085
Reid Spencer1ef8bda2004-12-30 05:36:08 +000086 // Load the input module...
87 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename, &ErrorMessage));
88 if (M.get() == 0) {
89 std::cerr << argv[0] << ": ";
90 if (ErrorMessage.size())
91 std::cerr << ErrorMessage << "\n";
92 else
93 std::cerr << "bytecode didn't read correctly.\n";
Chris Lattner00950542001-06-06 20:29:01 +000094 return 1;
95 }
Chris Lattner76d12292002-04-18 19:55:25 +000096
Reid Spencer1ef8bda2004-12-30 05:36:08 +000097 // Figure out what stream we are supposed to write to...
98 std::ostream *Out = &std::cout; // Default to printing to stdout...
99 if (OutputFilename != "-") {
100 if (!Force && std::ifstream(OutputFilename.c_str())) {
101 // If force is not specified, make sure not to overwrite a file!
102 std::cerr << argv[0] << ": error opening '" << OutputFilename
103 << "': file exists!\n"
104 << "Use -f command line argument to force output\n";
105 return 1;
106 }
107 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattner00950542001-06-06 20:29:01 +0000108
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000109 if (!Out->good()) {
110 std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
111 return 1;
112 }
Chris Lattner76351aa2004-04-02 05:06:57 +0000113
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000114 // Make sure that the Output file gets unlinked from the disk if we get a
115 // SIGINT
116 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
117 }
Chris Lattner00950542001-06-06 20:29:01 +0000118
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000119 // If the output is set to be emitted to standard out, and standard out is a
120 // console, print out a warning message and refuse to do it. We don't impress
121 // anyone by spewing tons of binary goo to a terminal.
Reid Spencer564a5712005-01-05 17:31:55 +0000122 if (!Force && !NoOutput && CheckBytecodeOutputToConsole(Out,!Quiet)) {
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000123 NoOutput = true;
124 }
Chris Lattner9c3b55e2003-04-24 19:13:02 +0000125
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000126 // Create a PassManager to hold and optimize the collection of passes we are
127 // about to build...
128 //
129 PassManager Passes;
130
131 // Add an appropriate TargetData instance for this module...
132 Passes.add(new TargetData("opt", M.get()));
133
134 // Create a new optimization pass for each one specified on the command line
135 for (unsigned i = 0; i < OptimizationList.size(); ++i) {
136 const PassInfo *Opt = OptimizationList[i];
137
138 if (Opt->getNormalCtor())
139 Passes.add(Opt->getNormalCtor()());
140 else if (Opt->getTargetCtor()) {
Chris Lattner0af1e8e2003-04-16 23:02:16 +0000141#if 0
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000142 if (target.get() == NULL)
143 target.reset(allocateSparcTargetMachine()); // FIXME: target option
Chris Lattner0af1e8e2003-04-16 23:02:16 +0000144#endif
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000145 assert(target.get() && "Could not allocate target machine!");
146 Passes.add(Opt->getTargetCtor()(*target.get()));
147 } else
148 std::cerr << argv[0] << ": cannot create pass: " << Opt->getPassName()
149 << "\n";
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000150
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000151 if (PrintEachXForm)
152 Passes.add(new PrintModulePass(&std::cerr));
153 }
154
155 // Check that the module is well formed on completion of optimization
156 if (!NoVerify)
157 Passes.add(createVerifierPass());
158
159 // Write bytecode out to disk or cout as the last step...
160 if (!NoOutput)
161 Passes.add(new WriteBytecodePass(Out, Out != &std::cout));
162
163 // Now that we have all of the passes ready, run them.
164 Passes.run(*M.get());
165
166 return 0;
167 } catch (const std::string& msg) {
168 std::cerr << argv[0] << ": " << msg << "\n";
169 } catch (...) {
170 std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000171 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000172 return 1;
Chris Lattner00950542001-06-06 20:29:01 +0000173}