blob: fe0e03649ddc911f4fcd4742ec62cf17c1ebc743 [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//
Chris Lattner21c62da2007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// 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
Reid Spencerfd90dd52006-08-18 06:34:30 +000011// line, They are run in the order specified.
Chris Lattner00950542001-06-06 20:29:01 +000012//
Chris Lattner0eafc312001-10-18 06:05:15 +000013//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +000014
Owen Anderson8b477ed2009-07-01 16:58:40 +000015#include "llvm/LLVMContext.h"
Chris Lattner00950542001-06-06 20:29:01 +000016#include "llvm/Module.h"
Devang Patel2d7551c2008-09-16 22:25:14 +000017#include "llvm/ModuleProvider.h"
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000018#include "llvm/PassManager.h"
Devang Patel28552da2007-06-28 23:09:25 +000019#include "llvm/CallGraphSCCPass.h"
Chris Lattnerb330e382007-05-06 02:42:03 +000020#include "llvm/Bitcode/ReaderWriter.h"
Chris Lattnerffa6f9c2001-10-19 15:39:14 +000021#include "llvm/Assembly/PrintModulePass.h"
Chris Lattner22d26d72002-02-20 17:56:53 +000022#include "llvm/Analysis/Verifier.h"
Devang Patel1bc89362007-03-07 00:26:10 +000023#include "llvm/Analysis/LoopPass.h"
Devang Patel28552da2007-06-28 23:09:25 +000024#include "llvm/Analysis/CallGraph.h"
Owen Anderson07000c62006-05-12 06:33:49 +000025#include "llvm/Target/TargetData.h"
Vikram S. Adve18fdfc42002-09-16 16:09:43 +000026#include "llvm/Target/TargetMachine.h"
Chris Lattner2053a2a2002-07-26 21:09:32 +000027#include "llvm/Support/PassNameParser.h"
Chris Lattnerbed85ff2004-05-27 05:41:36 +000028#include "llvm/System/Signals.h"
Dan Gohman99ed4162009-09-03 16:00:08 +000029#include "llvm/Support/IRReader.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000030#include "llvm/Support/ManagedStatic.h"
Chris Lattnerb330e382007-05-06 02:42:03 +000031#include "llvm/Support/MemoryBuffer.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000032#include "llvm/Support/PluginLoader.h"
Daniel Dunbarca8131e2009-06-03 18:22:15 +000033#include "llvm/Support/StandardPasses.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000034#include "llvm/Support/SystemUtils.h"
Daniel Dunbar3b0da262008-10-22 03:25:22 +000035#include "llvm/Support/raw_ostream.h"
Reid Spencer62c51052006-08-21 05:34:03 +000036#include "llvm/LinkAllPasses.h"
Reid Spenceraf303d52006-06-07 23:03:13 +000037#include "llvm/LinkAllVMCore.h"
Chris Lattner63202322001-11-26 19:22:39 +000038#include <memory>
Chris Lattnerc0ce68b2002-07-23 18:12:22 +000039#include <algorithm>
Brian Gaeked0fde302003-11-11 22:41:34 +000040using namespace llvm;
Chris Lattner9d6e7eb2002-04-12 18:21:13 +000041
Chris Lattnerc0ce68b2002-07-23 18:12:22 +000042// The OptimizationList is automatically populated with registered Passes by the
43// PassNameParser.
44//
Chris Lattner7f500f72006-08-27 22:07:01 +000045static cl::list<const PassInfo*, bool, PassNameParser>
46PassList(cl::desc("Optimizations available:"));
Chris Lattnerc0ce68b2002-07-23 18:12:22 +000047
Chris Lattnerc0ce68b2002-07-23 18:12:22 +000048// Other command line options...
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000049//
Chris Lattner6c8103f2003-05-22 20:13:16 +000050static cl::opt<std::string>
Eric Christophera887ae42009-08-21 23:29:40 +000051InputFilename(cl::Positional, cl::desc("<input bitcode file>"),
Reid Spencerfd90dd52006-08-18 06:34:30 +000052 cl::init("-"), cl::value_desc("filename"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000053
Chris Lattner6c8103f2003-05-22 20:13:16 +000054static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000055OutputFilename("o", cl::desc("Override output filename"),
Chris Lattnerb592fc22003-12-10 14:41:33 +000056 cl::value_desc("filename"), cl::init("-"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000057
58static cl::opt<bool>
Dan Gohmanbaa26392009-08-25 15:34:52 +000059Force("f", cl::desc("Enable binary output on terminals"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000060
61static cl::opt<bool>
62PrintEachXForm("p", cl::desc("Print module after each transformation"));
63
64static cl::opt<bool>
Chris Lattnerddd5b412003-02-26 20:00:41 +000065NoOutput("disable-output",
Gabor Greifa99be512007-07-05 17:07:56 +000066 cl::desc("Do not write result bitcode file"), cl::Hidden);
Chris Lattnerd70b68e2003-02-12 18:43:33 +000067
68static cl::opt<bool>
Daniel Dunbar8c042c22009-09-05 11:34:53 +000069OutputAssembly("S",
70 cl::desc("Write output as LLVM assembly"), cl::Hidden);
71
72static cl::opt<bool>
Chris Lattnerddd5b412003-02-26 20:00:41 +000073NoVerify("disable-verify", cl::desc("Do not verify result module"), cl::Hidden);
Chris Lattnerf3bafc12003-02-12 18:45:08 +000074
75static cl::opt<bool>
Reid Spencer74ed9972007-02-02 14:46:29 +000076VerifyEach("verify-each", cl::desc("Verify after each transform"));
77
78static cl::opt<bool>
79StripDebug("strip-debug",
80 cl::desc("Strip debugger symbol info from translation unit"));
81
82static cl::opt<bool>
83DisableInline("disable-inlining", cl::desc("Do not run the inliner pass"));
84
Eric Christophera887ae42009-08-21 23:29:40 +000085static cl::opt<bool>
86DisableOptimizations("disable-opt",
Reid Spencer74ed9972007-02-02 14:46:29 +000087 cl::desc("Do not run any optimization passes"));
88
Eric Christophera887ae42009-08-21 23:29:40 +000089static cl::opt<bool>
Daniel Dunbaradc82882009-07-17 18:09:39 +000090DisableInternalize("disable-internalize",
91 cl::desc("Do not mark all symbols as internal"));
92
Reid Spencer74ed9972007-02-02 14:46:29 +000093static cl::opt<bool>
Eric Christophera887ae42009-08-21 23:29:40 +000094StandardCompileOpts("std-compile-opts",
Reid Spencer74ed9972007-02-02 14:46:29 +000095 cl::desc("Include the standard compile time optimizations"));
96
97static cl::opt<bool>
Eric Christophera887ae42009-08-21 23:29:40 +000098StandardLinkOpts("std-link-opts",
Daniel Dunbaradc82882009-07-17 18:09:39 +000099 cl::desc("Include the standard link time optimizations"));
100
101static cl::opt<bool>
Devang Patel2d7551c2008-09-16 22:25:14 +0000102OptLevelO1("O1",
103 cl::desc("Optimization level 1. Similar to llvm-gcc -O1"));
104
105static cl::opt<bool>
106OptLevelO2("O2",
Devang Pateld9424ed2008-09-17 00:01:04 +0000107 cl::desc("Optimization level 2. Similar to llvm-gcc -O2"));
Devang Patel2d7551c2008-09-16 22:25:14 +0000108
109static cl::opt<bool>
110OptLevelO3("O3",
Devang Pateld9424ed2008-09-17 00:01:04 +0000111 cl::desc("Optimization level 3. Similar to llvm-gcc -O3"));
Devang Patel2d7551c2008-09-16 22:25:14 +0000112
113static cl::opt<bool>
114UnitAtATime("funit-at-a-time",
Eric Christopherc4769ba2009-08-21 23:30:30 +0000115 cl::desc("Enable IPO. This is same as llvm-gcc's -funit-at-a-time"),
116 cl::init(true));
Devang Patel2d7551c2008-09-16 22:25:14 +0000117
118static cl::opt<bool>
119DisableSimplifyLibCalls("disable-simplify-libcalls",
Devang Patel442b1aa2008-09-17 16:01:39 +0000120 cl::desc("Disable simplify-libcalls"));
Devang Patel2d7551c2008-09-16 22:25:14 +0000121
122static cl::opt<bool>
Chris Lattner3153e4f2004-05-27 20:32:10 +0000123Quiet("q", cl::desc("Obsolete option"), cl::Hidden);
Chris Lattner5ff62e92002-07-22 02:10:13 +0000124
Reid Spencerec7eb452004-05-27 16:28:54 +0000125static cl::alias
126QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet));
127
Reid Spencerfd90dd52006-08-18 06:34:30 +0000128static cl::opt<bool>
129AnalyzeOnly("analyze", cl::desc("Only perform analysis, no optimization"));
130
Reid Spencerfd90dd52006-08-18 06:34:30 +0000131// ---------- Define Printers for module and function passes ------------
132namespace {
133
Devang Patel28552da2007-06-28 23:09:25 +0000134struct CallGraphSCCPassPrinter : public CallGraphSCCPass {
135 static char ID;
136 const PassInfo *PassToPrint;
Eric Christophera887ae42009-08-21 23:29:40 +0000137 CallGraphSCCPassPrinter(const PassInfo *PI) :
Dan Gohman865f0062009-02-18 05:09:16 +0000138 CallGraphSCCPass(&ID), PassToPrint(PI) {}
Devang Patel28552da2007-06-28 23:09:25 +0000139
Chris Lattner5095e3d2009-08-31 00:19:58 +0000140 virtual bool runOnSCC(std::vector<CallGraphNode *>&SCC) {
Devang Patel28552da2007-06-28 23:09:25 +0000141 if (!Quiet) {
Dan Gohmanac95cc72009-07-16 15:30:09 +0000142 outs() << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
Devang Patel28552da2007-06-28 23:09:25 +0000143
144 for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
145 Function *F = SCC[i]->getFunction();
Dan Gohmanac95cc72009-07-16 15:30:09 +0000146 if (F) {
Chris Lattner45cfe542009-08-23 06:03:38 +0000147 getAnalysisID<Pass>(PassToPrint).print(outs(), F->getParent());
Dan Gohmanac95cc72009-07-16 15:30:09 +0000148 }
Devang Patel28552da2007-06-28 23:09:25 +0000149 }
150 }
151 // Get and print pass...
152 return false;
153 }
Eric Christophera887ae42009-08-21 23:29:40 +0000154
Devang Patel28552da2007-06-28 23:09:25 +0000155 virtual const char *getPassName() const { return "'Pass' Printer"; }
156
157 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
158 AU.addRequiredID(PassToPrint);
159 AU.setPreservesAll();
160 }
161};
162
163char CallGraphSCCPassPrinter::ID = 0;
164
Reid Spencerfd90dd52006-08-18 06:34:30 +0000165struct ModulePassPrinter : public ModulePass {
Devang Patel19974732007-05-03 01:11:54 +0000166 static char ID;
Reid Spencerfd90dd52006-08-18 06:34:30 +0000167 const PassInfo *PassToPrint;
Dan Gohman865f0062009-02-18 05:09:16 +0000168 ModulePassPrinter(const PassInfo *PI) : ModulePass(&ID),
Devang Patel794fd752007-05-01 21:15:47 +0000169 PassToPrint(PI) {}
Reid Spencerfd90dd52006-08-18 06:34:30 +0000170
171 virtual bool runOnModule(Module &M) {
172 if (!Quiet) {
Dan Gohmanac95cc72009-07-16 15:30:09 +0000173 outs() << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
Chris Lattner45cfe542009-08-23 06:03:38 +0000174 getAnalysisID<Pass>(PassToPrint).print(outs(), &M);
Reid Spencerfd90dd52006-08-18 06:34:30 +0000175 }
176
177 // Get and print pass...
178 return false;
179 }
180
181 virtual const char *getPassName() const { return "'Pass' Printer"; }
182
183 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
184 AU.addRequiredID(PassToPrint);
185 AU.setPreservesAll();
186 }
187};
188
Devang Patel19974732007-05-03 01:11:54 +0000189char ModulePassPrinter::ID = 0;
Reid Spencerfd90dd52006-08-18 06:34:30 +0000190struct FunctionPassPrinter : public FunctionPass {
191 const PassInfo *PassToPrint;
Devang Patel19974732007-05-03 01:11:54 +0000192 static char ID;
Dan Gohman865f0062009-02-18 05:09:16 +0000193 FunctionPassPrinter(const PassInfo *PI) : FunctionPass(&ID),
Devang Patel794fd752007-05-01 21:15:47 +0000194 PassToPrint(PI) {}
Reid Spencerfd90dd52006-08-18 06:34:30 +0000195
196 virtual bool runOnFunction(Function &F) {
Eric Christophera887ae42009-08-21 23:29:40 +0000197 if (!Quiet) {
Dan Gohmanac95cc72009-07-16 15:30:09 +0000198 outs() << "Printing analysis '" << PassToPrint->getPassName()
199 << "' for function '" << F.getName() << "':\n";
Reid Spencerfd90dd52006-08-18 06:34:30 +0000200 }
201 // Get and print pass...
Chris Lattner45cfe542009-08-23 06:03:38 +0000202 getAnalysisID<Pass>(PassToPrint).print(outs(), F.getParent());
Reid Spencerfd90dd52006-08-18 06:34:30 +0000203 return false;
204 }
205
206 virtual const char *getPassName() const { return "FunctionPass Printer"; }
207
208 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
209 AU.addRequiredID(PassToPrint);
210 AU.setPreservesAll();
211 }
212};
213
Devang Patel19974732007-05-03 01:11:54 +0000214char FunctionPassPrinter::ID = 0;
Devang Patel56fb1642007-07-05 15:32:03 +0000215
216struct LoopPassPrinter : public LoopPass {
217 static char ID;
218 const PassInfo *PassToPrint;
Eric Christophera887ae42009-08-21 23:29:40 +0000219 LoopPassPrinter(const PassInfo *PI) :
Dan Gohman865f0062009-02-18 05:09:16 +0000220 LoopPass(&ID), PassToPrint(PI) {}
Devang Patel56fb1642007-07-05 15:32:03 +0000221
222 virtual bool runOnLoop(Loop *L, LPPassManager &LPM) {
223 if (!Quiet) {
Dan Gohmanac95cc72009-07-16 15:30:09 +0000224 outs() << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
Chris Lattner45cfe542009-08-23 06:03:38 +0000225 getAnalysisID<Pass>(PassToPrint).print(outs(),
Devang Patel56fb1642007-07-05 15:32:03 +0000226 L->getHeader()->getParent()->getParent());
227 }
228 // Get and print pass...
229 return false;
230 }
Eric Christophera887ae42009-08-21 23:29:40 +0000231
Devang Patel56fb1642007-07-05 15:32:03 +0000232 virtual const char *getPassName() const { return "'Pass' Printer"; }
233
234 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
235 AU.addRequiredID(PassToPrint);
236 AU.setPreservesAll();
237 }
238};
239
240char LoopPassPrinter::ID = 0;
241
Reid Spencerfd90dd52006-08-18 06:34:30 +0000242struct BasicBlockPassPrinter : public BasicBlockPass {
243 const PassInfo *PassToPrint;
Devang Patel19974732007-05-03 01:11:54 +0000244 static char ID;
Eric Christophera887ae42009-08-21 23:29:40 +0000245 BasicBlockPassPrinter(const PassInfo *PI)
Dan Gohman865f0062009-02-18 05:09:16 +0000246 : BasicBlockPass(&ID), PassToPrint(PI) {}
Reid Spencerfd90dd52006-08-18 06:34:30 +0000247
248 virtual bool runOnBasicBlock(BasicBlock &BB) {
249 if (!Quiet) {
Dan Gohmanac95cc72009-07-16 15:30:09 +0000250 outs() << "Printing Analysis info for BasicBlock '" << BB.getName()
251 << "': Pass " << PassToPrint->getPassName() << ":\n";
Reid Spencerfd90dd52006-08-18 06:34:30 +0000252 }
253
254 // Get and print pass...
Chris Lattner45cfe542009-08-23 06:03:38 +0000255 getAnalysisID<Pass>(PassToPrint).print(outs(), BB.getParent()->getParent());
Reid Spencerfd90dd52006-08-18 06:34:30 +0000256 return false;
257 }
258
259 virtual const char *getPassName() const { return "BasicBlockPass Printer"; }
260
261 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
262 AU.addRequiredID(PassToPrint);
263 AU.setPreservesAll();
264 }
265};
266
Devang Patel19974732007-05-03 01:11:54 +0000267char BasicBlockPassPrinter::ID = 0;
Reid Spencer74ed9972007-02-02 14:46:29 +0000268inline void addPass(PassManager &PM, Pass *P) {
269 // Add the pass to the pass manager...
270 PM.add(P);
271
272 // If we are verifying all of the intermediate steps, add the verifier...
273 if (VerifyEach) PM.add(createVerifierPass());
274}
275
Eric Christophera887ae42009-08-21 23:29:40 +0000276/// AddOptimizationPasses - This routine adds optimization passes
277/// based on selected optimization level, OptLevel. This routine
Devang Patel2d7551c2008-09-16 22:25:14 +0000278/// duplicates llvm-gcc behaviour.
279///
280/// OptLevel - Optimization Level
Zhongxing Xu3253f4c2008-11-26 02:57:24 +0000281void AddOptimizationPasses(PassManager &MPM, FunctionPassManager &FPM,
282 unsigned OptLevel) {
Daniel Dunbarca8131e2009-06-03 18:22:15 +0000283 createStandardFunctionPasses(&FPM, OptLevel);
Devang Patel2d7551c2008-09-16 22:25:14 +0000284
Daniel Dunbarca8131e2009-06-03 18:22:15 +0000285 llvm::Pass *InliningPass = OptLevel > 1 ? createFunctionInliningPass() : 0;
286 createStandardModulePasses(&MPM, OptLevel,
287 /*OptimizeSize=*/ false,
288 UnitAtATime,
289 /*UnrollLoops=*/ OptLevel > 1,
290 !DisableSimplifyLibCalls,
291 /*HaveExceptions=*/ true,
292 InliningPass);
Devang Patel2d7551c2008-09-16 22:25:14 +0000293}
294
Reid Spencer74ed9972007-02-02 14:46:29 +0000295void AddStandardCompilePasses(PassManager &PM) {
296 PM.add(createVerifierPass()); // Verify that input is correct
297
298 addPass(PM, createLowerSetJmpPass()); // Lower llvm.setjmp/.longjmp
Reid Spencer74ed9972007-02-02 14:46:29 +0000299
300 // If the -strip-debug command line option was specified, do it.
301 if (StripDebug)
302 addPass(PM, createStripSymbolsPass(true));
303
304 if (DisableOptimizations) return;
305
Daniel Dunbarca8131e2009-06-03 18:22:15 +0000306 llvm::Pass *InliningPass = !DisableInline ? createFunctionInliningPass() : 0;
Reid Spencer74ed9972007-02-02 14:46:29 +0000307
Daniel Dunbarca8131e2009-06-03 18:22:15 +0000308 // -std-compile-opts adds the same module passes as -O3.
Eric Christophera887ae42009-08-21 23:29:40 +0000309 createStandardModulePasses(&PM, 3,
Daniel Dunbarca8131e2009-06-03 18:22:15 +0000310 /*OptimizeSize=*/ false,
311 /*UnitAtATime=*/ true,
312 /*UnrollLoops=*/ true,
313 /*SimplifyLibCalls=*/ true,
314 /*HaveExceptions=*/ true,
315 InliningPass);
Reid Spencer74ed9972007-02-02 14:46:29 +0000316}
317
Daniel Dunbaradc82882009-07-17 18:09:39 +0000318void AddStandardLinkPasses(PassManager &PM) {
319 PM.add(createVerifierPass()); // Verify that input is correct
320
321 // If the -strip-debug command line option was specified, do it.
322 if (StripDebug)
323 addPass(PM, createStripSymbolsPass(true));
324
325 if (DisableOptimizations) return;
326
327 createStandardLTOPasses(&PM, /*Internalize=*/ !DisableInternalize,
328 /*RunInliner=*/ !DisableInline,
329 /*VerifyEach=*/ VerifyEach);
330}
331
Reid Spencerfd90dd52006-08-18 06:34:30 +0000332} // anonymous namespace
333
Chris Lattner0be41012002-02-01 04:54:11 +0000334
Chris Lattnerc0ce68b2002-07-23 18:12:22 +0000335//===----------------------------------------------------------------------===//
336// main for opt
337//
Chris Lattner00950542001-06-06 20:29:01 +0000338int main(int argc, char **argv) {
Chris Lattnerc30598b2006-12-06 01:18:01 +0000339 llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
Owen Anderson0d7c6952009-07-15 22:16:10 +0000340 LLVMContext &Context = getGlobalContext();
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000341 try {
342 cl::ParseCommandLineOptions(argc, argv,
Dan Gohman82a13c92007-10-08 15:45:12 +0000343 "llvm .bc -> .bc modular optimizer and analysis printer\n");
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000344 sys::PrintStackTraceOnErrorSignal();
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000345
Devang Patelff5d06d2008-08-27 20:51:49 +0000346 // Allocate a full target machine description only if necessary.
347 // FIXME: The choice of target should be controllable on the command line.
348 std::auto_ptr<TargetMachine> target;
349
Dan Gohman99ed4162009-09-03 16:00:08 +0000350 SMDiagnostic Err;
Vikram S. Adve18fdfc42002-09-16 16:09:43 +0000351
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000352 // Load the input module...
Chris Lattnerb330e382007-05-06 02:42:03 +0000353 std::auto_ptr<Module> M;
Dan Gohman99ed4162009-09-03 16:00:08 +0000354 M.reset(ParseIRFile(InputFilename, Err, Context));
Eric Christophera887ae42009-08-21 23:29:40 +0000355
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000356 if (M.get() == 0) {
Dan Gohman99ed4162009-09-03 16:00:08 +0000357 Err.Print(argv[0], errs());
Chris Lattner00950542001-06-06 20:29:01 +0000358 return 1;
359 }
Chris Lattner76d12292002-04-18 19:55:25 +0000360
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000361 // Figure out what stream we are supposed to write to...
Dan Gohmanac95cc72009-07-16 15:30:09 +0000362 // FIXME: outs() is not binary!
Dan Gohmana1bdced2009-07-15 17:29:42 +0000363 raw_ostream *Out = &outs(); // Default to printing to stdout...
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000364 if (OutputFilename != "-") {
Dan Gohmanec080462009-09-11 20:46:33 +0000365 // Make sure that the Output file gets unlinked from the disk if we get a
366 // SIGINT
367 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
368
Dan Gohmana1bdced2009-07-15 17:29:42 +0000369 std::string ErrorInfo;
Chris Lattner17e9edc2009-08-23 02:51:22 +0000370 Out = new raw_fd_ostream(OutputFilename.c_str(), ErrorInfo,
Dan Gohmanbaa26392009-08-25 15:34:52 +0000371 raw_fd_ostream::F_Binary);
Dan Gohmana1bdced2009-07-15 17:29:42 +0000372 if (!ErrorInfo.empty()) {
373 errs() << ErrorInfo << '\n';
Dan Gohmana1bdced2009-07-15 17:29:42 +0000374 delete Out;
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000375 return 1;
376 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000377 }
Chris Lattner00950542001-06-06 20:29:01 +0000378
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000379 // If the output is set to be emitted to standard out, and standard out is a
Jeff Cohen5fb6ed42005-01-22 17:36:17 +0000380 // console, print out a warning message and refuse to do it. We don't
381 // impress anyone by spewing tons of binary goo to a terminal.
Daniel Dunbar8c042c22009-09-05 11:34:53 +0000382 if (!Force && !NoOutput && !OutputAssembly)
383 if (CheckBitcodeOutputToConsole(*Out, !Quiet))
384 NoOutput = true;
Chris Lattner9c3b55e2003-04-24 19:13:02 +0000385
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000386 // Create a PassManager to hold and optimize the collection of passes we are
387 // about to build...
388 //
389 PassManager Passes;
390
391 // Add an appropriate TargetData instance for this module...
Chris Lattner831b1212006-06-16 18:23:49 +0000392 Passes.add(new TargetData(M.get()));
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000393
Devang Patel2d7551c2008-09-16 22:25:14 +0000394 FunctionPassManager *FPasses = NULL;
395 if (OptLevelO1 || OptLevelO2 || OptLevelO3) {
396 FPasses = new FunctionPassManager(new ExistingModuleProvider(M.get()));
397 FPasses->add(new TargetData(M.get()));
398 }
Eric Christophera887ae42009-08-21 23:29:40 +0000399
Chris Lattner3dda08a2008-07-13 19:35:21 +0000400 // If the -strip-debug command line option was specified, add it. If
401 // -std-compile-opts was also specified, it will handle StripDebug.
402 if (StripDebug && !StandardCompileOpts)
Reid Spencer74ed9972007-02-02 14:46:29 +0000403 addPass(Passes, createStripSymbolsPass(true));
404
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000405 // Create a new optimization pass for each one specified on the command line
Chris Lattner7f500f72006-08-27 22:07:01 +0000406 for (unsigned i = 0; i < PassList.size(); ++i) {
Duncan Sands56eb1332008-07-13 20:14:38 +0000407 // Check to see if -std-compile-opts was specified before this option. If
Chris Lattner3dda08a2008-07-13 19:35:21 +0000408 // so, handle it.
Eric Christophera887ae42009-08-21 23:29:40 +0000409 if (StandardCompileOpts &&
Chris Lattner3dda08a2008-07-13 19:35:21 +0000410 StandardCompileOpts.getPosition() < PassList.getPosition(i)) {
411 AddStandardCompilePasses(Passes);
412 StandardCompileOpts = false;
413 }
Eric Christophera887ae42009-08-21 23:29:40 +0000414
415 if (StandardLinkOpts &&
Daniel Dunbaradc82882009-07-17 18:09:39 +0000416 StandardLinkOpts.getPosition() < PassList.getPosition(i)) {
417 AddStandardLinkPasses(Passes);
418 StandardLinkOpts = false;
419 }
Eric Christophera887ae42009-08-21 23:29:40 +0000420
Devang Patel2d7551c2008-09-16 22:25:14 +0000421 if (OptLevelO1 && OptLevelO1.getPosition() < PassList.getPosition(i)) {
422 AddOptimizationPasses(Passes, *FPasses, 1);
423 OptLevelO1 = false;
424 }
425
426 if (OptLevelO2 && OptLevelO2.getPosition() < PassList.getPosition(i)) {
427 AddOptimizationPasses(Passes, *FPasses, 2);
428 OptLevelO2 = false;
429 }
430
431 if (OptLevelO3 && OptLevelO3.getPosition() < PassList.getPosition(i)) {
432 AddOptimizationPasses(Passes, *FPasses, 3);
433 OptLevelO3 = false;
434 }
435
Chris Lattner7f500f72006-08-27 22:07:01 +0000436 const PassInfo *PassInf = PassList[i];
437 Pass *P = 0;
438 if (PassInf->getNormalCtor())
439 P = PassInf->getNormalCtor()();
Chris Lattnercd950a52006-12-01 21:59:37 +0000440 else
Dan Gohmanac95cc72009-07-16 15:30:09 +0000441 errs() << argv[0] << ": cannot create pass: "
442 << PassInf->getPassName() << "\n";
Chris Lattner7f500f72006-08-27 22:07:01 +0000443 if (P) {
Nuno Lopes641397f2008-11-04 23:03:58 +0000444 bool isBBPass = dynamic_cast<BasicBlockPass*>(P) != 0;
445 bool isLPass = !isBBPass && dynamic_cast<LoopPass*>(P) != 0;
446 bool isFPass = !isLPass && dynamic_cast<FunctionPass*>(P) != 0;
447 bool isCGSCCPass = !isFPass && dynamic_cast<CallGraphSCCPass*>(P) != 0;
448
Reid Spencer74ed9972007-02-02 14:46:29 +0000449 addPass(Passes, P);
Nuno Lopes641397f2008-11-04 23:03:58 +0000450
Chris Lattner7f500f72006-08-27 22:07:01 +0000451 if (AnalyzeOnly) {
Nuno Lopes641397f2008-11-04 23:03:58 +0000452 if (isBBPass)
Chris Lattner7f500f72006-08-27 22:07:01 +0000453 Passes.add(new BasicBlockPassPrinter(PassInf));
Nuno Lopes641397f2008-11-04 23:03:58 +0000454 else if (isLPass)
455 Passes.add(new LoopPassPrinter(PassInf));
456 else if (isFPass)
Chris Lattner7f500f72006-08-27 22:07:01 +0000457 Passes.add(new FunctionPassPrinter(PassInf));
Nuno Lopes641397f2008-11-04 23:03:58 +0000458 else if (isCGSCCPass)
Devang Patel28552da2007-06-28 23:09:25 +0000459 Passes.add(new CallGraphSCCPassPrinter(PassInf));
Chris Lattner7f500f72006-08-27 22:07:01 +0000460 else
461 Passes.add(new ModulePassPrinter(PassInf));
462 }
463 }
Eric Christophera887ae42009-08-21 23:29:40 +0000464
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000465 if (PrintEachXForm)
Daniel Dunbar3b0da262008-10-22 03:25:22 +0000466 Passes.add(createPrintModulePass(&errs()));
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000467 }
Eric Christophera887ae42009-08-21 23:29:40 +0000468
Chris Lattner3dda08a2008-07-13 19:35:21 +0000469 // If -std-compile-opts was specified at the end of the pass list, add them.
470 if (StandardCompileOpts) {
471 AddStandardCompilePasses(Passes);
472 StandardCompileOpts = false;
Eric Christophera887ae42009-08-21 23:29:40 +0000473 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000474
Daniel Dunbaradc82882009-07-17 18:09:39 +0000475 if (StandardLinkOpts) {
476 AddStandardLinkPasses(Passes);
477 StandardLinkOpts = false;
Eric Christophera887ae42009-08-21 23:29:40 +0000478 }
Daniel Dunbaradc82882009-07-17 18:09:39 +0000479
Devang Patel2d7551c2008-09-16 22:25:14 +0000480 if (OptLevelO1) {
Daniel Dunbaradc82882009-07-17 18:09:39 +0000481 AddOptimizationPasses(Passes, *FPasses, 1);
482 }
Devang Patel2d7551c2008-09-16 22:25:14 +0000483
484 if (OptLevelO2) {
Daniel Dunbaradc82882009-07-17 18:09:39 +0000485 AddOptimizationPasses(Passes, *FPasses, 2);
486 }
Devang Patel2d7551c2008-09-16 22:25:14 +0000487
488 if (OptLevelO3) {
Daniel Dunbaradc82882009-07-17 18:09:39 +0000489 AddOptimizationPasses(Passes, *FPasses, 3);
490 }
Devang Patel2d7551c2008-09-16 22:25:14 +0000491
492 if (OptLevelO1 || OptLevelO2 || OptLevelO3) {
Bob Wilsond8aa9fc2009-07-17 19:05:13 +0000493 FPasses->doInitialization();
Devang Patel2d7551c2008-09-16 22:25:14 +0000494 for (Module::iterator I = M.get()->begin(), E = M.get()->end();
495 I != E; ++I)
496 FPasses->run(*I);
497 }
498
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000499 // Check that the module is well formed on completion of optimization
Reid Spencer74ed9972007-02-02 14:46:29 +0000500 if (!NoVerify && !VerifyEach)
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000501 Passes.add(createVerifierPass());
502
Daniel Dunbar8c042c22009-09-05 11:34:53 +0000503 // Write bitcode or assembly out to disk or outs() as the last step...
504 if (!NoOutput && !AnalyzeOnly) {
505 if (OutputAssembly)
506 Passes.add(createPrintModulePass(Out));
507 else
508 Passes.add(createBitcodeWriterPass(*Out));
509 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000510
511 // Now that we have all of the passes ready, run them.
512 Passes.run(*M.get());
513
Dan Gohmanac95cc72009-07-16 15:30:09 +0000514 // Delete the raw_fd_ostream.
Dan Gohmana1bdced2009-07-15 17:29:42 +0000515 if (Out != &outs())
Chris Lattnerd44ae902007-05-06 19:17:23 +0000516 delete Out;
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000517 return 0;
Reid Spencerfd90dd52006-08-18 06:34:30 +0000518
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000519 } catch (const std::string& msg) {
Dan Gohmanac95cc72009-07-16 15:30:09 +0000520 errs() << argv[0] << ": " << msg << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000521 } catch (...) {
Dan Gohmanac95cc72009-07-16 15:30:09 +0000522 errs() << argv[0] << ": Unexpected unknown exception occurred.\n";
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000523 }
Chris Lattner03315242007-01-31 04:45:28 +0000524 llvm_shutdown();
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000525 return 1;
Chris Lattner00950542001-06-06 20:29:01 +0000526}