blob: 096506ff39295dc19fd70fd45759f40dbd4c4081 [file] [log] [blame]
Chris Lattner76351aa2004-04-02 05:06:57 +00001//===- opt.cpp - The LLVM Modular Optimizer -------------------------------===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
John Criswell7c0e0222003-10-20 17:47:21 +00003// 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.
Misha Brukman3da94ae2005-04-22 00:00:37 +00007//
John Criswell7c0e0222003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
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"
Owen Anderson07000c62006-05-12 06:33:49 +000021#include "llvm/Target/TargetData.h"
Vikram S. Adve18fdfc42002-09-16 16:09:43 +000022#include "llvm/Target/TargetMachine.h"
Chris Lattner2053a2a2002-07-26 21:09:32 +000023#include "llvm/Support/PassNameParser.h"
Chris Lattnerbed85ff2004-05-27 05:41:36 +000024#include "llvm/System/Signals.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000025#include "llvm/Support/PluginLoader.h"
26#include "llvm/Support/SystemUtils.h"
Jeff Cohen4b807e02005-01-06 06:02:53 +000027#include "llvm/Transforms/LinkAllPasses.h"
Reid Spenceraf303d52006-06-07 23:03:13 +000028#include "llvm/LinkAllVMCore.h"
Chris Lattner73e11d72001-10-18 06:13:08 +000029#include <fstream>
Chris Lattner63202322001-11-26 19:22:39 +000030#include <memory>
Chris Lattnerc0ce68b2002-07-23 18:12:22 +000031#include <algorithm>
Anand Shukla63aaa112002-06-25 21:43:28 +000032
Brian Gaeked0fde302003-11-11 22:41:34 +000033using namespace llvm;
Chris Lattner9d6e7eb2002-04-12 18:21:13 +000034
Chris Lattnerc0ce68b2002-07-23 18:12:22 +000035// The OptimizationList is automatically populated with registered Passes by the
36// PassNameParser.
37//
Chris Lattner2053a2a2002-07-26 21:09:32 +000038static cl::list<const PassInfo*, bool,
39 FilteredPassNameParser<PassInfo::Optimization> >
Chris Lattnerc0ce68b2002-07-23 18:12:22 +000040OptimizationList(cl::desc("Optimizations available:"));
41
42
43// Other command line options...
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000044//
Chris Lattner6c8103f2003-05-22 20:13:16 +000045static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000046InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
47
Chris Lattner6c8103f2003-05-22 20:13:16 +000048static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000049OutputFilename("o", cl::desc("Override output filename"),
Chris Lattnerb592fc22003-12-10 14:41:33 +000050 cl::value_desc("filename"), cl::init("-"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000051
52static cl::opt<bool>
53Force("f", cl::desc("Overwrite output files"));
54
55static cl::opt<bool>
56PrintEachXForm("p", cl::desc("Print module after each transformation"));
57
58static cl::opt<bool>
Chris Lattnerddd5b412003-02-26 20:00:41 +000059NoOutput("disable-output",
60 cl::desc("Do not write result bytecode file"), cl::Hidden);
Chris Lattnerd70b68e2003-02-12 18:43:33 +000061
62static cl::opt<bool>
Chris Lattnerddd5b412003-02-26 20:00:41 +000063NoVerify("disable-verify", cl::desc("Do not verify result module"), cl::Hidden);
Chris Lattnerf3bafc12003-02-12 18:45:08 +000064
65static cl::opt<bool>
Chris Lattner3153e4f2004-05-27 20:32:10 +000066Quiet("q", cl::desc("Obsolete option"), cl::Hidden);
Chris Lattner5ff62e92002-07-22 02:10:13 +000067
Reid Spencerec7eb452004-05-27 16:28:54 +000068static cl::alias
69QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet));
70
Chris Lattner0be41012002-02-01 04:54:11 +000071
Chris Lattnerc0ce68b2002-07-23 18:12:22 +000072//===----------------------------------------------------------------------===//
73// main for opt
74//
Chris Lattner00950542001-06-06 20:29:01 +000075int main(int argc, char **argv) {
Reid Spencer1ef8bda2004-12-30 05:36:08 +000076 try {
77 cl::ParseCommandLineOptions(argc, argv,
78 " llvm .bc -> .bc modular optimizer\n");
79 sys::PrintStackTraceOnErrorSignal();
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000080
Reid Spencer1ef8bda2004-12-30 05:36:08 +000081 // Allocate a full target machine description only if necessary...
82 // FIXME: The choice of target should be controllable on the command line.
83 std::auto_ptr<TargetMachine> target;
Vikram S. Adve18fdfc42002-09-16 16:09:43 +000084
Reid Spencer1ef8bda2004-12-30 05:36:08 +000085 TargetMachine* TM = NULL;
86 std::string ErrorMessage;
Vikram S. Adve18fdfc42002-09-16 16:09:43 +000087
Reid Spencer1ef8bda2004-12-30 05:36:08 +000088 // Load the input module...
89 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename, &ErrorMessage));
90 if (M.get() == 0) {
91 std::cerr << argv[0] << ": ";
92 if (ErrorMessage.size())
93 std::cerr << ErrorMessage << "\n";
94 else
95 std::cerr << "bytecode didn't read correctly.\n";
Chris Lattner00950542001-06-06 20:29:01 +000096 return 1;
97 }
Chris Lattner76d12292002-04-18 19:55:25 +000098
Reid Spencer1ef8bda2004-12-30 05:36:08 +000099 // Figure out what stream we are supposed to write to...
Jeff Cohen5fb6ed42005-01-22 17:36:17 +0000100 // FIXME: cout is not binary!
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000101 std::ostream *Out = &std::cout; // Default to printing to stdout...
102 if (OutputFilename != "-") {
103 if (!Force && std::ifstream(OutputFilename.c_str())) {
104 // If force is not specified, make sure not to overwrite a file!
105 std::cerr << argv[0] << ": error opening '" << OutputFilename
106 << "': file exists!\n"
107 << "Use -f command line argument to force output\n";
108 return 1;
109 }
Jeff Cohen5fb6ed42005-01-22 17:36:17 +0000110 std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
111 std::ios::binary;
112 Out = new std::ofstream(OutputFilename.c_str(), io_mode);
Chris Lattner00950542001-06-06 20:29:01 +0000113
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000114 if (!Out->good()) {
115 std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
116 return 1;
117 }
Chris Lattner76351aa2004-04-02 05:06:57 +0000118
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000119 // Make sure that the Output file gets unlinked from the disk if we get a
120 // SIGINT
121 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
122 }
Chris Lattner00950542001-06-06 20:29:01 +0000123
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000124 // If the output is set to be emitted to standard out, and standard out is a
Jeff Cohen5fb6ed42005-01-22 17:36:17 +0000125 // console, print out a warning message and refuse to do it. We don't
126 // impress anyone by spewing tons of binary goo to a terminal.
Reid Spencer564a5712005-01-05 17:31:55 +0000127 if (!Force && !NoOutput && CheckBytecodeOutputToConsole(Out,!Quiet)) {
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000128 NoOutput = true;
129 }
Chris Lattner9c3b55e2003-04-24 19:13:02 +0000130
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000131 // Create a PassManager to hold and optimize the collection of passes we are
132 // about to build...
133 //
134 PassManager Passes;
135
136 // Add an appropriate TargetData instance for this module...
Chris Lattner831b1212006-06-16 18:23:49 +0000137 Passes.add(new TargetData(M.get()));
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000138
139 // Create a new optimization pass for each one specified on the command line
140 for (unsigned i = 0; i < OptimizationList.size(); ++i) {
141 const PassInfo *Opt = OptimizationList[i];
Misha Brukman3da94ae2005-04-22 00:00:37 +0000142
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000143 if (Opt->getNormalCtor())
144 Passes.add(Opt->getNormalCtor()());
145 else if (Opt->getTargetCtor()) {
Chris Lattner0af1e8e2003-04-16 23:02:16 +0000146#if 0
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000147 if (target.get() == NULL)
148 target.reset(allocateSparcTargetMachine()); // FIXME: target option
Chris Lattner0af1e8e2003-04-16 23:02:16 +0000149#endif
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000150 assert(target.get() && "Could not allocate target machine!");
151 Passes.add(Opt->getTargetCtor()(*target.get()));
152 } else
153 std::cerr << argv[0] << ": cannot create pass: " << Opt->getPassName()
154 << "\n";
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000155
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000156 if (PrintEachXForm)
157 Passes.add(new PrintModulePass(&std::cerr));
158 }
159
160 // Check that the module is well formed on completion of optimization
161 if (!NoVerify)
162 Passes.add(createVerifierPass());
163
164 // Write bytecode out to disk or cout as the last step...
165 if (!NoOutput)
166 Passes.add(new WriteBytecodePass(Out, Out != &std::cout));
167
168 // Now that we have all of the passes ready, run them.
169 Passes.run(*M.get());
170
171 return 0;
172 } catch (const std::string& msg) {
173 std::cerr << argv[0] << ": " << msg << "\n";
174 } catch (...) {
175 std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000176 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000177 return 1;
Chris Lattner00950542001-06-06 20:29:01 +0000178}