blob: 4ada7d1e76d46ebd3e842c929242f137fba52372 [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"
Bill Wendling0bcbd1d2012-06-28 00:05:13 +000016#include "llvm/DebugInfo.h"
Chris Lattner00950542001-06-06 20:29:01 +000017#include "llvm/Module.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"
Tobias Grosser65513602010-10-20 01:54:44 +000024#include "llvm/Analysis/RegionPass.h"
Devang Patel28552da2007-06-28 23:09:25 +000025#include "llvm/Analysis/CallGraph.h"
Owen Anderson07000c62006-05-12 06:33:49 +000026#include "llvm/Target/TargetData.h"
Chris Lattner2a66aca2011-02-18 22:13:01 +000027#include "llvm/Target/TargetLibraryInfo.h"
Vikram S. Adve18fdfc42002-09-16 16:09:43 +000028#include "llvm/Target/TargetMachine.h"
Devang Patel1be99802011-04-04 19:51:17 +000029#include "llvm/ADT/StringSet.h"
Chris Lattner2a66aca2011-02-18 22:13:01 +000030#include "llvm/ADT/Triple.h"
Chris Lattner2053a2a2002-07-26 21:09:32 +000031#include "llvm/Support/PassNameParser.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000032#include "llvm/Support/Signals.h"
David Greene08fc0d32010-01-05 01:30:32 +000033#include "llvm/Support/Debug.h"
Dan Gohman99ed4162009-09-03 16:00:08 +000034#include "llvm/Support/IRReader.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000035#include "llvm/Support/ManagedStatic.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000036#include "llvm/Support/PluginLoader.h"
Chris Lattnerc0d91b72009-12-09 00:41:28 +000037#include "llvm/Support/PrettyStackTrace.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000038#include "llvm/Support/SystemUtils.h"
Dan Gohmane4f1a9b2010-10-07 20:32:40 +000039#include "llvm/Support/ToolOutputFile.h"
Reid Spencer62c51052006-08-21 05:34:03 +000040#include "llvm/LinkAllPasses.h"
Reid Spenceraf303d52006-06-07 23:03:13 +000041#include "llvm/LinkAllVMCore.h"
Rafael Espindola3d453ac2011-08-02 21:50:24 +000042#include "llvm/Transforms/IPO/PassManagerBuilder.h"
Chris Lattner63202322001-11-26 19:22:39 +000043#include <memory>
Chris Lattnerc0ce68b2002-07-23 18:12:22 +000044#include <algorithm>
Brian Gaeked0fde302003-11-11 22:41:34 +000045using namespace llvm;
Chris Lattner9d6e7eb2002-04-12 18:21:13 +000046
Chris Lattnerc0ce68b2002-07-23 18:12:22 +000047// The OptimizationList is automatically populated with registered Passes by the
48// PassNameParser.
49//
Chris Lattner7f500f72006-08-27 22:07:01 +000050static cl::list<const PassInfo*, bool, PassNameParser>
51PassList(cl::desc("Optimizations available:"));
Chris Lattnerc0ce68b2002-07-23 18:12:22 +000052
Chris Lattnerc0ce68b2002-07-23 18:12:22 +000053// Other command line options...
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000054//
Chris Lattner6c8103f2003-05-22 20:13:16 +000055static cl::opt<std::string>
Eric Christophera887ae42009-08-21 23:29:40 +000056InputFilename(cl::Positional, cl::desc("<input bitcode file>"),
Reid Spencerfd90dd52006-08-18 06:34:30 +000057 cl::init("-"), cl::value_desc("filename"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000058
Chris Lattner6c8103f2003-05-22 20:13:16 +000059static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000060OutputFilename("o", cl::desc("Override output filename"),
Dan Gohman86cbc1b2010-08-18 17:40:10 +000061 cl::value_desc("filename"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000062
63static cl::opt<bool>
Dan Gohmanbaa26392009-08-25 15:34:52 +000064Force("f", cl::desc("Enable binary output on terminals"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000065
66static cl::opt<bool>
67PrintEachXForm("p", cl::desc("Print module after each transformation"));
68
69static cl::opt<bool>
Chris Lattnerddd5b412003-02-26 20:00:41 +000070NoOutput("disable-output",
Gabor Greifa99be512007-07-05 17:07:56 +000071 cl::desc("Do not write result bitcode file"), cl::Hidden);
Chris Lattnerd70b68e2003-02-12 18:43:33 +000072
73static cl::opt<bool>
Duncan Sands81b0b642009-10-14 20:01:39 +000074OutputAssembly("S", cl::desc("Write output as LLVM assembly"));
Daniel Dunbar8c042c22009-09-05 11:34:53 +000075
76static cl::opt<bool>
Chris Lattnerddd5b412003-02-26 20:00:41 +000077NoVerify("disable-verify", cl::desc("Do not verify result module"), cl::Hidden);
Chris Lattnerf3bafc12003-02-12 18:45:08 +000078
79static cl::opt<bool>
Reid Spencer74ed9972007-02-02 14:46:29 +000080VerifyEach("verify-each", cl::desc("Verify after each transform"));
81
82static cl::opt<bool>
83StripDebug("strip-debug",
84 cl::desc("Strip debugger symbol info from translation unit"));
85
86static cl::opt<bool>
87DisableInline("disable-inlining", cl::desc("Do not run the inliner pass"));
88
Eric Christophera887ae42009-08-21 23:29:40 +000089static cl::opt<bool>
90DisableOptimizations("disable-opt",
Reid Spencer74ed9972007-02-02 14:46:29 +000091 cl::desc("Do not run any optimization passes"));
92
Eric Christophera887ae42009-08-21 23:29:40 +000093static cl::opt<bool>
Daniel Dunbaradc82882009-07-17 18:09:39 +000094DisableInternalize("disable-internalize",
95 cl::desc("Do not mark all symbols as internal"));
96
Reid Spencer74ed9972007-02-02 14:46:29 +000097static cl::opt<bool>
Eric Christophera887ae42009-08-21 23:29:40 +000098StandardCompileOpts("std-compile-opts",
Reid Spencer74ed9972007-02-02 14:46:29 +000099 cl::desc("Include the standard compile time optimizations"));
100
101static cl::opt<bool>
Eric Christophera887ae42009-08-21 23:29:40 +0000102StandardLinkOpts("std-link-opts",
Daniel Dunbaradc82882009-07-17 18:09:39 +0000103 cl::desc("Include the standard link time optimizations"));
104
105static cl::opt<bool>
Devang Patel2d7551c2008-09-16 22:25:14 +0000106OptLevelO1("O1",
Chandler Carruth8bc34342012-05-16 08:32:49 +0000107 cl::desc("Optimization level 1. Similar to clang -O1"));
Devang Patel2d7551c2008-09-16 22:25:14 +0000108
109static cl::opt<bool>
110OptLevelO2("O2",
Chandler Carruth8bc34342012-05-16 08:32:49 +0000111 cl::desc("Optimization level 2. Similar to clang -O2"));
112
113static cl::opt<bool>
114OptLevelOs("Os",
115 cl::desc("Like -O2 with extra optimizations for size. Similar to clang -Os"));
116
117static cl::opt<bool>
118OptLevelOz("Oz",
119 cl::desc("Like -Os but reduces code size further. Similar to clang -Oz"));
Devang Patel2d7551c2008-09-16 22:25:14 +0000120
121static cl::opt<bool>
122OptLevelO3("O3",
Chandler Carruth8bc34342012-05-16 08:32:49 +0000123 cl::desc("Optimization level 3. Similar to clang -O3"));
Devang Patel2d7551c2008-09-16 22:25:14 +0000124
Joe Groffe652b522012-04-17 23:05:48 +0000125static cl::opt<std::string>
126TargetTriple("mtriple", cl::desc("Override target triple for module"));
127
Devang Patel2d7551c2008-09-16 22:25:14 +0000128static cl::opt<bool>
129UnitAtATime("funit-at-a-time",
Eric Christopherc4769ba2009-08-21 23:30:30 +0000130 cl::desc("Enable IPO. This is same as llvm-gcc's -funit-at-a-time"),
Duncan Sands34727662010-07-12 08:16:59 +0000131 cl::init(true));
Devang Patel2d7551c2008-09-16 22:25:14 +0000132
133static cl::opt<bool>
134DisableSimplifyLibCalls("disable-simplify-libcalls",
Devang Patel442b1aa2008-09-17 16:01:39 +0000135 cl::desc("Disable simplify-libcalls"));
Devang Patel2d7551c2008-09-16 22:25:14 +0000136
137static cl::opt<bool>
Chris Lattner3153e4f2004-05-27 20:32:10 +0000138Quiet("q", cl::desc("Obsolete option"), cl::Hidden);
Chris Lattner5ff62e92002-07-22 02:10:13 +0000139
Reid Spencerec7eb452004-05-27 16:28:54 +0000140static cl::alias
141QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet));
142
Reid Spencerfd90dd52006-08-18 06:34:30 +0000143static cl::opt<bool>
144AnalyzeOnly("analyze", cl::desc("Only perform analysis, no optimization"));
145
Devang Patelf5f23002010-12-07 00:33:43 +0000146static cl::opt<bool>
Andrew Tricka41af7a2011-04-05 18:41:31 +0000147PrintBreakpoints("print-breakpoints-for-testing",
Devang Patelf5f23002010-12-07 00:33:43 +0000148 cl::desc("Print select breakpoints location for testing"));
149
Chris Lattnerd331cb32009-10-22 00:44:10 +0000150static cl::opt<std::string>
Andrew Tricka41af7a2011-04-05 18:41:31 +0000151DefaultDataLayout("default-data-layout",
Chris Lattnerd331cb32009-10-22 00:44:10 +0000152 cl::desc("data layout string to use if not specified by module"),
153 cl::value_desc("layout-string"), cl::init(""));
154
Reid Spencerfd90dd52006-08-18 06:34:30 +0000155// ---------- Define Printers for module and function passes ------------
156namespace {
157
Devang Patel28552da2007-06-28 23:09:25 +0000158struct CallGraphSCCPassPrinter : public CallGraphSCCPass {
159 static char ID;
160 const PassInfo *PassToPrint;
Dan Gohman4931b312010-08-18 17:42:59 +0000161 raw_ostream &Out;
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000162 std::string PassName;
163
Dan Gohman4931b312010-08-18 17:42:59 +0000164 CallGraphSCCPassPrinter(const PassInfo *PI, raw_ostream &out) :
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000165 CallGraphSCCPass(ID), PassToPrint(PI), Out(out) {
166 std::string PassToPrintName = PassToPrint->getPassName();
167 PassName = "CallGraphSCCPass Printer: " + PassToPrintName;
168 }
Devang Patel28552da2007-06-28 23:09:25 +0000169
Chris Lattner2decb222010-04-16 22:42:17 +0000170 virtual bool runOnSCC(CallGraphSCC &SCC) {
Tobias Grossere9dcd032010-09-08 15:02:51 +0000171 if (!Quiet)
Dan Gohman4931b312010-08-18 17:42:59 +0000172 Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
Devang Patel28552da2007-06-28 23:09:25 +0000173
Devang Patel28552da2007-06-28 23:09:25 +0000174 // Get and print pass...
Tobias Grossere9dcd032010-09-08 15:02:51 +0000175 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
176 Function *F = (*I)->getFunction();
177 if (F)
178 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
179 F->getParent());
180 }
Devang Patel28552da2007-06-28 23:09:25 +0000181 return false;
182 }
Eric Christophera887ae42009-08-21 23:29:40 +0000183
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000184 virtual const char *getPassName() const { return PassName.c_str(); }
Devang Patel28552da2007-06-28 23:09:25 +0000185
186 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Owen Anderson90c579d2010-08-06 18:33:48 +0000187 AU.addRequiredID(PassToPrint->getTypeInfo());
Devang Patel28552da2007-06-28 23:09:25 +0000188 AU.setPreservesAll();
189 }
190};
191
192char CallGraphSCCPassPrinter::ID = 0;
193
Reid Spencerfd90dd52006-08-18 06:34:30 +0000194struct ModulePassPrinter : public ModulePass {
Devang Patel19974732007-05-03 01:11:54 +0000195 static char ID;
Reid Spencerfd90dd52006-08-18 06:34:30 +0000196 const PassInfo *PassToPrint;
Dan Gohman4931b312010-08-18 17:42:59 +0000197 raw_ostream &Out;
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000198 std::string PassName;
199
Dan Gohman4931b312010-08-18 17:42:59 +0000200 ModulePassPrinter(const PassInfo *PI, raw_ostream &out)
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000201 : ModulePass(ID), PassToPrint(PI), Out(out) {
202 std::string PassToPrintName = PassToPrint->getPassName();
203 PassName = "ModulePass Printer: " + PassToPrintName;
204 }
Reid Spencerfd90dd52006-08-18 06:34:30 +0000205
206 virtual bool runOnModule(Module &M) {
Tobias Grossere9dcd032010-09-08 15:02:51 +0000207 if (!Quiet)
Dan Gohman4931b312010-08-18 17:42:59 +0000208 Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
Reid Spencerfd90dd52006-08-18 06:34:30 +0000209
210 // Get and print pass...
Tobias Grossere9dcd032010-09-08 15:02:51 +0000211 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out, &M);
Reid Spencerfd90dd52006-08-18 06:34:30 +0000212 return false;
213 }
214
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000215 virtual const char *getPassName() const { return PassName.c_str(); }
Reid Spencerfd90dd52006-08-18 06:34:30 +0000216
217 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Owen Anderson90c579d2010-08-06 18:33:48 +0000218 AU.addRequiredID(PassToPrint->getTypeInfo());
Reid Spencerfd90dd52006-08-18 06:34:30 +0000219 AU.setPreservesAll();
220 }
221};
222
Devang Patel19974732007-05-03 01:11:54 +0000223char ModulePassPrinter::ID = 0;
Reid Spencerfd90dd52006-08-18 06:34:30 +0000224struct FunctionPassPrinter : public FunctionPass {
225 const PassInfo *PassToPrint;
Dan Gohman4931b312010-08-18 17:42:59 +0000226 raw_ostream &Out;
Devang Patel19974732007-05-03 01:11:54 +0000227 static char ID;
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000228 std::string PassName;
229
Dan Gohman4931b312010-08-18 17:42:59 +0000230 FunctionPassPrinter(const PassInfo *PI, raw_ostream &out)
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000231 : FunctionPass(ID), PassToPrint(PI), Out(out) {
232 std::string PassToPrintName = PassToPrint->getPassName();
233 PassName = "FunctionPass Printer: " + PassToPrintName;
234 }
Reid Spencerfd90dd52006-08-18 06:34:30 +0000235
236 virtual bool runOnFunction(Function &F) {
Tobias Grossere9dcd032010-09-08 15:02:51 +0000237 if (!Quiet)
Dan Gohman4931b312010-08-18 17:42:59 +0000238 Out << "Printing analysis '" << PassToPrint->getPassName()
239 << "' for function '" << F.getName() << "':\n";
Tobias Grossere9dcd032010-09-08 15:02:51 +0000240
Reid Spencerfd90dd52006-08-18 06:34:30 +0000241 // Get and print pass...
Dan Gohman4931b312010-08-18 17:42:59 +0000242 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
Owen Anderson90c579d2010-08-06 18:33:48 +0000243 F.getParent());
Reid Spencerfd90dd52006-08-18 06:34:30 +0000244 return false;
245 }
246
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000247 virtual const char *getPassName() const { return PassName.c_str(); }
Reid Spencerfd90dd52006-08-18 06:34:30 +0000248
249 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Owen Anderson90c579d2010-08-06 18:33:48 +0000250 AU.addRequiredID(PassToPrint->getTypeInfo());
Reid Spencerfd90dd52006-08-18 06:34:30 +0000251 AU.setPreservesAll();
252 }
253};
254
Devang Patel19974732007-05-03 01:11:54 +0000255char FunctionPassPrinter::ID = 0;
Devang Patel56fb1642007-07-05 15:32:03 +0000256
257struct LoopPassPrinter : public LoopPass {
258 static char ID;
259 const PassInfo *PassToPrint;
Dan Gohman4931b312010-08-18 17:42:59 +0000260 raw_ostream &Out;
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000261 std::string PassName;
262
Dan Gohman4931b312010-08-18 17:42:59 +0000263 LoopPassPrinter(const PassInfo *PI, raw_ostream &out) :
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000264 LoopPass(ID), PassToPrint(PI), Out(out) {
265 std::string PassToPrintName = PassToPrint->getPassName();
266 PassName = "LoopPass Printer: " + PassToPrintName;
267 }
268
Devang Patel56fb1642007-07-05 15:32:03 +0000269
270 virtual bool runOnLoop(Loop *L, LPPassManager &LPM) {
Tobias Grossere9dcd032010-09-08 15:02:51 +0000271 if (!Quiet)
Dan Gohman4931b312010-08-18 17:42:59 +0000272 Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
Tobias Grossere9dcd032010-09-08 15:02:51 +0000273
Devang Patel56fb1642007-07-05 15:32:03 +0000274 // Get and print pass...
Tobias Grossere9dcd032010-09-08 15:02:51 +0000275 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
276 L->getHeader()->getParent()->getParent());
Devang Patel56fb1642007-07-05 15:32:03 +0000277 return false;
278 }
Eric Christophera887ae42009-08-21 23:29:40 +0000279
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000280 virtual const char *getPassName() const { return PassName.c_str(); }
Devang Patel56fb1642007-07-05 15:32:03 +0000281
282 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Owen Anderson90c579d2010-08-06 18:33:48 +0000283 AU.addRequiredID(PassToPrint->getTypeInfo());
Devang Patel56fb1642007-07-05 15:32:03 +0000284 AU.setPreservesAll();
285 }
286};
287
288char LoopPassPrinter::ID = 0;
289
Tobias Grosser65513602010-10-20 01:54:44 +0000290struct RegionPassPrinter : public RegionPass {
291 static char ID;
292 const PassInfo *PassToPrint;
293 raw_ostream &Out;
294 std::string PassName;
295
296 RegionPassPrinter(const PassInfo *PI, raw_ostream &out) : RegionPass(ID),
297 PassToPrint(PI), Out(out) {
298 std::string PassToPrintName = PassToPrint->getPassName();
Tobias Grosser58396142011-01-20 21:03:20 +0000299 PassName = "RegionPass Printer: " + PassToPrintName;
Tobias Grosser65513602010-10-20 01:54:44 +0000300 }
301
302 virtual bool runOnRegion(Region *R, RGPassManager &RGM) {
303 if (!Quiet) {
304 Out << "Printing analysis '" << PassToPrint->getPassName() << "' for "
Benjamin Kramera7b0cb72011-11-15 16:27:03 +0000305 << "region: '" << R->getNameStr() << "' in function '"
306 << R->getEntry()->getParent()->getName() << "':\n";
Tobias Grosser65513602010-10-20 01:54:44 +0000307 }
308 // Get and print pass...
309 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
310 R->getEntry()->getParent()->getParent());
311 return false;
312 }
313
Tobias Grosser58396142011-01-20 21:03:20 +0000314 virtual const char *getPassName() const { return PassName.c_str(); }
Tobias Grosser65513602010-10-20 01:54:44 +0000315
316 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
317 AU.addRequiredID(PassToPrint->getTypeInfo());
318 AU.setPreservesAll();
319 }
320};
321
322char RegionPassPrinter::ID = 0;
323
Reid Spencerfd90dd52006-08-18 06:34:30 +0000324struct BasicBlockPassPrinter : public BasicBlockPass {
325 const PassInfo *PassToPrint;
Dan Gohman4931b312010-08-18 17:42:59 +0000326 raw_ostream &Out;
Devang Patel19974732007-05-03 01:11:54 +0000327 static char ID;
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000328 std::string PassName;
329
Dan Gohman4931b312010-08-18 17:42:59 +0000330 BasicBlockPassPrinter(const PassInfo *PI, raw_ostream &out)
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000331 : BasicBlockPass(ID), PassToPrint(PI), Out(out) {
332 std::string PassToPrintName = PassToPrint->getPassName();
333 PassName = "BasicBlockPass Printer: " + PassToPrintName;
334 }
Reid Spencerfd90dd52006-08-18 06:34:30 +0000335
336 virtual bool runOnBasicBlock(BasicBlock &BB) {
Tobias Grossere9dcd032010-09-08 15:02:51 +0000337 if (!Quiet)
Dan Gohman4931b312010-08-18 17:42:59 +0000338 Out << "Printing Analysis info for BasicBlock '" << BB.getName()
339 << "': Pass " << PassToPrint->getPassName() << ":\n";
Reid Spencerfd90dd52006-08-18 06:34:30 +0000340
341 // Get and print pass...
Andrew Tricka41af7a2011-04-05 18:41:31 +0000342 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
Owen Anderson90c579d2010-08-06 18:33:48 +0000343 BB.getParent()->getParent());
Reid Spencerfd90dd52006-08-18 06:34:30 +0000344 return false;
345 }
346
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000347 virtual const char *getPassName() const { return PassName.c_str(); }
Reid Spencerfd90dd52006-08-18 06:34:30 +0000348
349 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Owen Anderson90c579d2010-08-06 18:33:48 +0000350 AU.addRequiredID(PassToPrint->getTypeInfo());
Reid Spencerfd90dd52006-08-18 06:34:30 +0000351 AU.setPreservesAll();
352 }
353};
354
Devang Patel19974732007-05-03 01:11:54 +0000355char BasicBlockPassPrinter::ID = 0;
Devang Patelf5f23002010-12-07 00:33:43 +0000356
Devang Patel1be99802011-04-04 19:51:17 +0000357struct BreakpointPrinter : public ModulePass {
Devang Patelf5f23002010-12-07 00:33:43 +0000358 raw_ostream &Out;
359 static char ID;
360
361 BreakpointPrinter(raw_ostream &out)
Devang Patel1be99802011-04-04 19:51:17 +0000362 : ModulePass(ID), Out(out) {
Devang Patelf5f23002010-12-07 00:33:43 +0000363 }
364
Devang Patel1be99802011-04-04 19:51:17 +0000365 void getContextName(DIDescriptor Context, std::string &N) {
366 if (Context.isNameSpace()) {
367 DINameSpace NS(Context);
368 if (!NS.getName().empty()) {
369 getContextName(NS.getContext(), N);
370 N = N + NS.getName().str() + "::";
Devang Patel97f6d5b2011-01-31 21:36:24 +0000371 }
Devang Patel1be99802011-04-04 19:51:17 +0000372 } else if (Context.isType()) {
373 DIType TY(Context);
374 if (!TY.getName().empty()) {
375 getContextName(TY.getContext(), N);
376 N = N + TY.getName().str() + "::";
377 }
378 }
379 }
380
381 virtual bool runOnModule(Module &M) {
382 StringSet<> Processed;
383 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
384 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
385 std::string Name;
386 DISubprogram SP(NMD->getOperand(i));
387 if (SP.Verify())
388 getContextName(SP.getContext(), Name);
389 Name = Name + SP.getDisplayName().str();
390 if (!Name.empty() && Processed.insert(Name)) {
391 Out << Name << "\n";
392 }
393 }
Devang Patelf5f23002010-12-07 00:33:43 +0000394 return false;
395 }
396
397 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
398 AU.setPreservesAll();
399 }
400};
Chris Lattner52b28892011-05-22 00:21:33 +0000401
402} // anonymous namespace
Devang Patelf5f23002010-12-07 00:33:43 +0000403
404char BreakpointPrinter::ID = 0;
405
Chris Lattner52b28892011-05-22 00:21:33 +0000406static inline void addPass(PassManagerBase &PM, Pass *P) {
Reid Spencer74ed9972007-02-02 14:46:29 +0000407 // Add the pass to the pass manager...
408 PM.add(P);
409
410 // If we are verifying all of the intermediate steps, add the verifier...
411 if (VerifyEach) PM.add(createVerifierPass());
412}
413
Eric Christophera887ae42009-08-21 23:29:40 +0000414/// AddOptimizationPasses - This routine adds optimization passes
415/// based on selected optimization level, OptLevel. This routine
Devang Patel2d7551c2008-09-16 22:25:14 +0000416/// duplicates llvm-gcc behaviour.
417///
418/// OptLevel - Optimization Level
Chris Lattner52b28892011-05-22 00:21:33 +0000419static void AddOptimizationPasses(PassManagerBase &MPM,FunctionPassManager &FPM,
Chandler Carruth8bc34342012-05-16 08:32:49 +0000420 unsigned OptLevel, unsigned SizeLevel) {
Duncan Sandsa3e585d2011-12-07 17:14:20 +0000421 FPM.add(createVerifierPass()); // Verify that input is correct
422
Chris Lattner52b28892011-05-22 00:21:33 +0000423 PassManagerBuilder Builder;
424 Builder.OptLevel = OptLevel;
Chandler Carruth8bc34342012-05-16 08:32:49 +0000425 Builder.SizeLevel = SizeLevel;
Devang Patel2d7551c2008-09-16 22:25:14 +0000426
Eli Friedman74733a72010-01-18 22:38:31 +0000427 if (DisableInline) {
428 // No inlining pass
Eli Friedman42d41fd2011-06-06 22:13:27 +0000429 } else if (OptLevel > 1) {
Jakob Stoklund Olesene6c6cec2010-11-02 23:40:28 +0000430 unsigned Threshold = 225;
Chandler Carruth8bc34342012-05-16 08:32:49 +0000431 if (SizeLevel == 1) // -Os
432 Threshold = 75;
433 else if (SizeLevel == 2) // -Oz
434 Threshold = 25;
Eli Friedman74733a72010-01-18 22:38:31 +0000435 if (OptLevel > 2)
Jakob Stoklund Olesene6c6cec2010-11-02 23:40:28 +0000436 Threshold = 275;
Chris Lattner52b28892011-05-22 00:21:33 +0000437 Builder.Inliner = createFunctionInliningPass(Threshold);
Eli Friedman74733a72010-01-18 22:38:31 +0000438 } else {
Chris Lattner52b28892011-05-22 00:21:33 +0000439 Builder.Inliner = createAlwaysInlinerPass();
Eli Friedman74733a72010-01-18 22:38:31 +0000440 }
Chris Lattner52b28892011-05-22 00:21:33 +0000441 Builder.DisableUnitAtATime = !UnitAtATime;
442 Builder.DisableUnrollLoops = OptLevel == 0;
443 Builder.DisableSimplifyLibCalls = DisableSimplifyLibCalls;
444
445 Builder.populateFunctionPassManager(FPM);
446 Builder.populateModulePassManager(MPM);
Devang Patel2d7551c2008-09-16 22:25:14 +0000447}
448
Chris Lattner52b28892011-05-22 00:21:33 +0000449static void AddStandardCompilePasses(PassManagerBase &PM) {
Reid Spencer74ed9972007-02-02 14:46:29 +0000450 PM.add(createVerifierPass()); // Verify that input is correct
451
Reid Spencer74ed9972007-02-02 14:46:29 +0000452 // If the -strip-debug command line option was specified, do it.
453 if (StripDebug)
454 addPass(PM, createStripSymbolsPass(true));
455
456 if (DisableOptimizations) return;
457
Daniel Dunbarca8131e2009-06-03 18:22:15 +0000458 // -std-compile-opts adds the same module passes as -O3.
Chris Lattner52b28892011-05-22 00:21:33 +0000459 PassManagerBuilder Builder;
460 if (!DisableInline)
461 Builder.Inliner = createFunctionInliningPass();
462 Builder.OptLevel = 3;
463 Builder.DisableSimplifyLibCalls = DisableSimplifyLibCalls;
464 Builder.populateModulePassManager(PM);
Reid Spencer74ed9972007-02-02 14:46:29 +0000465}
466
Chris Lattner52b28892011-05-22 00:21:33 +0000467static void AddStandardLinkPasses(PassManagerBase &PM) {
Daniel Dunbaradc82882009-07-17 18:09:39 +0000468 PM.add(createVerifierPass()); // Verify that input is correct
469
470 // If the -strip-debug command line option was specified, do it.
471 if (StripDebug)
472 addPass(PM, createStripSymbolsPass(true));
473
474 if (DisableOptimizations) return;
475
Chris Lattner52b28892011-05-22 00:21:33 +0000476 PassManagerBuilder Builder;
477 Builder.populateLTOPassManager(PM, /*Internalize=*/ !DisableInternalize,
478 /*RunInliner=*/ !DisableInline);
Daniel Dunbaradc82882009-07-17 18:09:39 +0000479}
480
Chris Lattner0be41012002-02-01 04:54:11 +0000481
Chris Lattnerc0ce68b2002-07-23 18:12:22 +0000482//===----------------------------------------------------------------------===//
483// main for opt
484//
Chris Lattner00950542001-06-06 20:29:01 +0000485int main(int argc, char **argv) {
Chris Lattnerc0d91b72009-12-09 00:41:28 +0000486 sys::PrintStackTraceOnErrorSignal();
487 llvm::PrettyStackTraceProgram X(argc, argv);
Dan Gohmand4c45432010-09-01 14:20:41 +0000488
David Greene08fc0d32010-01-05 01:30:32 +0000489 // Enable debug stream buffering.
490 EnableDebugBuffering = true;
491
Chris Lattnerc0d91b72009-12-09 00:41:28 +0000492 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Owen Anderson0d7c6952009-07-15 22:16:10 +0000493 LLVMContext &Context = getGlobalContext();
Andrew Tricka41af7a2011-04-05 18:41:31 +0000494
Owen Anderson081c34b2010-10-19 17:21:58 +0000495 // Initialize passes
496 PassRegistry &Registry = *PassRegistry::getPassRegistry();
497 initializeCore(Registry);
498 initializeScalarOpts(Registry);
Hal Finkelde5e5ec2012-02-01 03:51:43 +0000499 initializeVectorization(Registry);
Owen Anderson081c34b2010-10-19 17:21:58 +0000500 initializeIPO(Registry);
501 initializeAnalysis(Registry);
502 initializeIPA(Registry);
503 initializeTransformUtils(Registry);
504 initializeInstCombine(Registry);
505 initializeInstrumentation(Registry);
506 initializeTarget(Registry);
Andrew Tricka41af7a2011-04-05 18:41:31 +0000507
Chris Lattner61db1a12009-10-22 00:46:41 +0000508 cl::ParseCommandLineOptions(argc, argv,
509 "llvm .bc -> .bc modular optimizer and analysis printer\n");
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000510
Tobias Grosser7593f342010-12-02 20:35:16 +0000511 if (AnalyzeOnly && NoOutput) {
512 errs() << argv[0] << ": analyze mode conflicts with no-output mode.\n";
513 return 1;
514 }
515
Chris Lattner61db1a12009-10-22 00:46:41 +0000516 // Allocate a full target machine description only if necessary.
517 // FIXME: The choice of target should be controllable on the command line.
518 std::auto_ptr<TargetMachine> target;
Devang Patelff5d06d2008-08-27 20:51:49 +0000519
Chris Lattner61db1a12009-10-22 00:46:41 +0000520 SMDiagnostic Err;
Vikram S. Adve18fdfc42002-09-16 16:09:43 +0000521
Chris Lattner61db1a12009-10-22 00:46:41 +0000522 // Load the input module...
523 std::auto_ptr<Module> M;
524 M.reset(ParseIRFile(InputFilename, Err, Context));
Eric Christophera887ae42009-08-21 23:29:40 +0000525
Chris Lattner61db1a12009-10-22 00:46:41 +0000526 if (M.get() == 0) {
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000527 Err.print(argv[0], errs());
Chris Lattner61db1a12009-10-22 00:46:41 +0000528 return 1;
529 }
530
Joe Groffe652b522012-04-17 23:05:48 +0000531 // If we are supposed to override the target triple, do so now.
532 if (!TargetTriple.empty())
533 M->setTargetTriple(Triple::normalize(TargetTriple));
534
Chris Lattner61db1a12009-10-22 00:46:41 +0000535 // Figure out what stream we are supposed to write to...
Dan Gohmand5826a32010-08-20 01:07:01 +0000536 OwningPtr<tool_output_file> Out;
Dan Gohman4931b312010-08-18 17:42:59 +0000537 if (NoOutput) {
Dan Gohman86cbc1b2010-08-18 17:40:10 +0000538 if (!OutputFilename.empty())
539 errs() << "WARNING: The -o (output filename) option is ignored when\n"
Dan Gohman4931b312010-08-18 17:42:59 +0000540 "the --disable-output option is used.\n";
Dan Gohman86cbc1b2010-08-18 17:40:10 +0000541 } else {
542 // Default to standard output.
543 if (OutputFilename.empty())
544 OutputFilename = "-";
Chris Lattner61db1a12009-10-22 00:46:41 +0000545
Dan Gohman86cbc1b2010-08-18 17:40:10 +0000546 std::string ErrorInfo;
Dan Gohmand5826a32010-08-20 01:07:01 +0000547 Out.reset(new tool_output_file(OutputFilename.c_str(), ErrorInfo,
548 raw_fd_ostream::F_Binary));
Dan Gohman86cbc1b2010-08-18 17:40:10 +0000549 if (!ErrorInfo.empty()) {
550 errs() << ErrorInfo << '\n';
Dan Gohman86cbc1b2010-08-18 17:40:10 +0000551 return 1;
Chris Lattner00950542001-06-06 20:29:01 +0000552 }
Chris Lattner61db1a12009-10-22 00:46:41 +0000553 }
Chris Lattner76d12292002-04-18 19:55:25 +0000554
Chris Lattner61db1a12009-10-22 00:46:41 +0000555 // If the output is set to be emitted to standard out, and standard out is a
556 // console, print out a warning message and refuse to do it. We don't
557 // impress anyone by spewing tons of binary goo to a terminal.
Dan Gohmanb56bf582010-01-17 17:47:24 +0000558 if (!Force && !NoOutput && !AnalyzeOnly && !OutputAssembly)
Dan Gohmand4c45432010-09-01 14:20:41 +0000559 if (CheckBitcodeOutputToConsole(Out->os(), !Quiet))
Chris Lattner61db1a12009-10-22 00:46:41 +0000560 NoOutput = true;
Dan Gohmanec080462009-09-11 20:46:33 +0000561
Chris Lattner61db1a12009-10-22 00:46:41 +0000562 // Create a PassManager to hold and optimize the collection of passes we are
Chris Lattner2a66aca2011-02-18 22:13:01 +0000563 // about to build.
Chris Lattner61db1a12009-10-22 00:46:41 +0000564 //
565 PassManager Passes;
Chris Lattner00950542001-06-06 20:29:01 +0000566
Chris Lattner2a66aca2011-02-18 22:13:01 +0000567 // Add an appropriate TargetLibraryInfo pass for the module's triple.
Chris Lattner188a7e02011-02-18 22:34:03 +0000568 TargetLibraryInfo *TLI = new TargetLibraryInfo(Triple(M->getTargetTriple()));
Andrew Tricka41af7a2011-04-05 18:41:31 +0000569
Chris Lattner188a7e02011-02-18 22:34:03 +0000570 // The -disable-simplify-libcalls flag actually disables all builtin optzns.
571 if (DisableSimplifyLibCalls)
572 TLI->disableAllFunctions();
573 Passes.add(TLI);
Andrew Tricka41af7a2011-04-05 18:41:31 +0000574
Chris Lattner2a66aca2011-02-18 22:13:01 +0000575 // Add an appropriate TargetData instance for this module.
Chris Lattner61db1a12009-10-22 00:46:41 +0000576 TargetData *TD = 0;
577 const std::string &ModuleDataLayout = M.get()->getDataLayout();
578 if (!ModuleDataLayout.empty())
579 TD = new TargetData(ModuleDataLayout);
Kenneth Uildriksb908f8a2009-11-03 15:29:06 +0000580 else if (!DefaultDataLayout.empty())
Chris Lattner61db1a12009-10-22 00:46:41 +0000581 TD = new TargetData(DefaultDataLayout);
Chris Lattner9c3b55e2003-04-24 19:13:02 +0000582
Chris Lattner61db1a12009-10-22 00:46:41 +0000583 if (TD)
584 Passes.add(TD);
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000585
Chris Lattner52b28892011-05-22 00:21:33 +0000586 OwningPtr<FunctionPassManager> FPasses;
Chandler Carruth8bc34342012-05-16 08:32:49 +0000587 if (OptLevelO1 || OptLevelO2 || OptLevelOs || OptLevelOz || OptLevelO3) {
Chris Lattner52b28892011-05-22 00:21:33 +0000588 FPasses.reset(new FunctionPassManager(M.get()));
Chris Lattnerd331cb32009-10-22 00:44:10 +0000589 if (TD)
Chris Lattner61db1a12009-10-22 00:46:41 +0000590 FPasses->add(new TargetData(*TD));
591 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000592
Devang Patelf5f23002010-12-07 00:33:43 +0000593 if (PrintBreakpoints) {
594 // Default to standard output.
595 if (!Out) {
596 if (OutputFilename.empty())
597 OutputFilename = "-";
Andrew Tricka41af7a2011-04-05 18:41:31 +0000598
Devang Patelf5f23002010-12-07 00:33:43 +0000599 std::string ErrorInfo;
600 Out.reset(new tool_output_file(OutputFilename.c_str(), ErrorInfo,
601 raw_fd_ostream::F_Binary));
602 if (!ErrorInfo.empty()) {
603 errs() << ErrorInfo << '\n';
604 return 1;
605 }
606 }
607 Passes.add(new BreakpointPrinter(Out->os()));
608 NoOutput = true;
609 }
610
Chris Lattner61db1a12009-10-22 00:46:41 +0000611 // If the -strip-debug command line option was specified, add it. If
612 // -std-compile-opts was also specified, it will handle StripDebug.
613 if (StripDebug && !StandardCompileOpts)
614 addPass(Passes, createStripSymbolsPass(true));
Eric Christophera887ae42009-08-21 23:29:40 +0000615
Chris Lattner61db1a12009-10-22 00:46:41 +0000616 // Create a new optimization pass for each one specified on the command line
617 for (unsigned i = 0; i < PassList.size(); ++i) {
618 // Check to see if -std-compile-opts was specified before this option. If
619 // so, handle it.
620 if (StandardCompileOpts &&
621 StandardCompileOpts.getPosition() < PassList.getPosition(i)) {
Chris Lattner3dda08a2008-07-13 19:35:21 +0000622 AddStandardCompilePasses(Passes);
623 StandardCompileOpts = false;
Eric Christophera887ae42009-08-21 23:29:40 +0000624 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000625
Chris Lattner61db1a12009-10-22 00:46:41 +0000626 if (StandardLinkOpts &&
627 StandardLinkOpts.getPosition() < PassList.getPosition(i)) {
Daniel Dunbaradc82882009-07-17 18:09:39 +0000628 AddStandardLinkPasses(Passes);
629 StandardLinkOpts = false;
Eric Christophera887ae42009-08-21 23:29:40 +0000630 }
Daniel Dunbaradc82882009-07-17 18:09:39 +0000631
Chris Lattner61db1a12009-10-22 00:46:41 +0000632 if (OptLevelO1 && OptLevelO1.getPosition() < PassList.getPosition(i)) {
Chandler Carruth8bc34342012-05-16 08:32:49 +0000633 AddOptimizationPasses(Passes, *FPasses, 1, 0);
Chris Lattner61db1a12009-10-22 00:46:41 +0000634 OptLevelO1 = false;
Daniel Dunbaradc82882009-07-17 18:09:39 +0000635 }
Devang Patel2d7551c2008-09-16 22:25:14 +0000636
Chris Lattner61db1a12009-10-22 00:46:41 +0000637 if (OptLevelO2 && OptLevelO2.getPosition() < PassList.getPosition(i)) {
Chandler Carruth8bc34342012-05-16 08:32:49 +0000638 AddOptimizationPasses(Passes, *FPasses, 2, 0);
Chris Lattner61db1a12009-10-22 00:46:41 +0000639 OptLevelO2 = false;
Daniel Dunbaradc82882009-07-17 18:09:39 +0000640 }
Devang Patel2d7551c2008-09-16 22:25:14 +0000641
Chandler Carruth8bc34342012-05-16 08:32:49 +0000642 if (OptLevelOs && OptLevelOs.getPosition() < PassList.getPosition(i)) {
643 AddOptimizationPasses(Passes, *FPasses, 2, 1);
644 OptLevelOs = false;
645 }
646
647 if (OptLevelOz && OptLevelOz.getPosition() < PassList.getPosition(i)) {
648 AddOptimizationPasses(Passes, *FPasses, 2, 2);
649 OptLevelOz = false;
650 }
651
Chris Lattner61db1a12009-10-22 00:46:41 +0000652 if (OptLevelO3 && OptLevelO3.getPosition() < PassList.getPosition(i)) {
Chandler Carruth8bc34342012-05-16 08:32:49 +0000653 AddOptimizationPasses(Passes, *FPasses, 3, 0);
Chris Lattner61db1a12009-10-22 00:46:41 +0000654 OptLevelO3 = false;
Daniel Dunbaradc82882009-07-17 18:09:39 +0000655 }
Devang Patel2d7551c2008-09-16 22:25:14 +0000656
Chris Lattner61db1a12009-10-22 00:46:41 +0000657 const PassInfo *PassInf = PassList[i];
658 Pass *P = 0;
659 if (PassInf->getNormalCtor())
660 P = PassInf->getNormalCtor()();
661 else
662 errs() << argv[0] << ": cannot create pass: "
663 << PassInf->getPassName() << "\n";
664 if (P) {
Benjamin Kramer3460f222010-02-18 12:57:05 +0000665 PassKind Kind = P->getPassKind();
Chris Lattner61db1a12009-10-22 00:46:41 +0000666 addPass(Passes, P);
667
668 if (AnalyzeOnly) {
Benjamin Kramer3460f222010-02-18 12:57:05 +0000669 switch (Kind) {
Chris Lattner476e9bd2010-01-22 06:03:06 +0000670 case PT_BasicBlock:
Dan Gohmand4c45432010-09-01 14:20:41 +0000671 Passes.add(new BasicBlockPassPrinter(PassInf, Out->os()));
Chris Lattner476e9bd2010-01-22 06:03:06 +0000672 break;
Tobias Grosser65513602010-10-20 01:54:44 +0000673 case PT_Region:
674 Passes.add(new RegionPassPrinter(PassInf, Out->os()));
675 break;
Chris Lattner476e9bd2010-01-22 06:03:06 +0000676 case PT_Loop:
Dan Gohmand4c45432010-09-01 14:20:41 +0000677 Passes.add(new LoopPassPrinter(PassInf, Out->os()));
Chris Lattner476e9bd2010-01-22 06:03:06 +0000678 break;
679 case PT_Function:
Dan Gohmand4c45432010-09-01 14:20:41 +0000680 Passes.add(new FunctionPassPrinter(PassInf, Out->os()));
Chris Lattner476e9bd2010-01-22 06:03:06 +0000681 break;
682 case PT_CallGraphSCC:
Dan Gohmand4c45432010-09-01 14:20:41 +0000683 Passes.add(new CallGraphSCCPassPrinter(PassInf, Out->os()));
Chris Lattner476e9bd2010-01-22 06:03:06 +0000684 break;
685 default:
Dan Gohmand4c45432010-09-01 14:20:41 +0000686 Passes.add(new ModulePassPrinter(PassInf, Out->os()));
Chris Lattner476e9bd2010-01-22 06:03:06 +0000687 break;
688 }
Chris Lattner61db1a12009-10-22 00:46:41 +0000689 }
Devang Patel2d7551c2008-09-16 22:25:14 +0000690 }
691
Chris Lattner61db1a12009-10-22 00:46:41 +0000692 if (PrintEachXForm)
693 Passes.add(createPrintModulePass(&errs()));
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000694 }
Chris Lattner61db1a12009-10-22 00:46:41 +0000695
696 // If -std-compile-opts was specified at the end of the pass list, add them.
697 if (StandardCompileOpts) {
698 AddStandardCompilePasses(Passes);
699 StandardCompileOpts = false;
700 }
701
702 if (StandardLinkOpts) {
703 AddStandardLinkPasses(Passes);
704 StandardLinkOpts = false;
705 }
706
707 if (OptLevelO1)
Chandler Carruth8bc34342012-05-16 08:32:49 +0000708 AddOptimizationPasses(Passes, *FPasses, 1, 0);
Chris Lattner61db1a12009-10-22 00:46:41 +0000709
710 if (OptLevelO2)
Chandler Carruth8bc34342012-05-16 08:32:49 +0000711 AddOptimizationPasses(Passes, *FPasses, 2, 0);
712
713 if (OptLevelOs)
714 AddOptimizationPasses(Passes, *FPasses, 2, 1);
715
716 if (OptLevelOz)
717 AddOptimizationPasses(Passes, *FPasses, 2, 2);
Chris Lattner61db1a12009-10-22 00:46:41 +0000718
719 if (OptLevelO3)
Chandler Carruth8bc34342012-05-16 08:32:49 +0000720 AddOptimizationPasses(Passes, *FPasses, 3, 0);
Chris Lattner61db1a12009-10-22 00:46:41 +0000721
Chandler Carruth8bc34342012-05-16 08:32:49 +0000722 if (OptLevelO1 || OptLevelO2 || OptLevelOs || OptLevelOz || OptLevelO3) {
Chris Lattner3e8984a2011-05-22 06:44:19 +0000723 FPasses->doInitialization();
Chris Lattner52b28892011-05-22 00:21:33 +0000724 for (Module::iterator F = M->begin(), E = M->end(); F != E; ++F)
725 FPasses->run(*F);
Chris Lattner3e8984a2011-05-22 06:44:19 +0000726 FPasses->doFinalization();
727 }
Chris Lattner61db1a12009-10-22 00:46:41 +0000728
729 // Check that the module is well formed on completion of optimization
730 if (!NoVerify && !VerifyEach)
731 Passes.add(createVerifierPass());
732
Dan Gohman4931b312010-08-18 17:42:59 +0000733 // Write bitcode or assembly to the output as the last step...
Chris Lattner61db1a12009-10-22 00:46:41 +0000734 if (!NoOutput && !AnalyzeOnly) {
735 if (OutputAssembly)
Dan Gohmand4c45432010-09-01 14:20:41 +0000736 Passes.add(createPrintModulePass(&Out->os()));
Chris Lattner61db1a12009-10-22 00:46:41 +0000737 else
Dan Gohmand4c45432010-09-01 14:20:41 +0000738 Passes.add(createBitcodeWriterPass(Out->os()));
Chris Lattner61db1a12009-10-22 00:46:41 +0000739 }
740
Andrew Trickce969022011-04-05 18:54:36 +0000741 // Before executing passes, print the final values of the LLVM options.
742 cl::PrintOptionValues();
743
Chris Lattner61db1a12009-10-22 00:46:41 +0000744 // Now that we have all of the passes ready, run them.
745 Passes.run(*M.get());
746
Dan Gohmand5826a32010-08-20 01:07:01 +0000747 // Declare success.
Devang Patelf5f23002010-12-07 00:33:43 +0000748 if (!NoOutput || PrintBreakpoints)
Dan Gohmand5826a32010-08-20 01:07:01 +0000749 Out->keep();
750
Chris Lattner61db1a12009-10-22 00:46:41 +0000751 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000752}