blob: 706a7d51380bddbd122afb6cf1fcf44076bcea42 [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"
Micah Villmow791cfc22012-10-08 16:39:34 +000016#include "llvm/DataLayout.h"
Bill Wendling0bcbd1d2012-06-28 00:05:13 +000017#include "llvm/DebugInfo.h"
Chris Lattner00950542001-06-06 20:29:01 +000018#include "llvm/Module.h"
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000019#include "llvm/PassManager.h"
Devang Patel28552da2007-06-28 23:09:25 +000020#include "llvm/CallGraphSCCPass.h"
Chris Lattnerb330e382007-05-06 02:42:03 +000021#include "llvm/Bitcode/ReaderWriter.h"
Chris Lattnerffa6f9c2001-10-19 15:39:14 +000022#include "llvm/Assembly/PrintModulePass.h"
Chris Lattner22d26d72002-02-20 17:56:53 +000023#include "llvm/Analysis/Verifier.h"
Devang Patel1bc89362007-03-07 00:26:10 +000024#include "llvm/Analysis/LoopPass.h"
Tobias Grosser65513602010-10-20 01:54:44 +000025#include "llvm/Analysis/RegionPass.h"
Devang Patel28552da2007-06-28 23:09:25 +000026#include "llvm/Analysis/CallGraph.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 SMDiagnostic Err;
Vikram S. Adve18fdfc42002-09-16 16:09:43 +0000517
Chris Lattner61db1a12009-10-22 00:46:41 +0000518 // Load the input module...
519 std::auto_ptr<Module> M;
520 M.reset(ParseIRFile(InputFilename, Err, Context));
Eric Christophera887ae42009-08-21 23:29:40 +0000521
Chris Lattner61db1a12009-10-22 00:46:41 +0000522 if (M.get() == 0) {
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000523 Err.print(argv[0], errs());
Chris Lattner61db1a12009-10-22 00:46:41 +0000524 return 1;
525 }
526
Joe Groffe652b522012-04-17 23:05:48 +0000527 // If we are supposed to override the target triple, do so now.
528 if (!TargetTriple.empty())
529 M->setTargetTriple(Triple::normalize(TargetTriple));
530
Chris Lattner61db1a12009-10-22 00:46:41 +0000531 // Figure out what stream we are supposed to write to...
Dan Gohmand5826a32010-08-20 01:07:01 +0000532 OwningPtr<tool_output_file> Out;
Dan Gohman4931b312010-08-18 17:42:59 +0000533 if (NoOutput) {
Dan Gohman86cbc1b2010-08-18 17:40:10 +0000534 if (!OutputFilename.empty())
535 errs() << "WARNING: The -o (output filename) option is ignored when\n"
Dan Gohman4931b312010-08-18 17:42:59 +0000536 "the --disable-output option is used.\n";
Dan Gohman86cbc1b2010-08-18 17:40:10 +0000537 } else {
538 // Default to standard output.
539 if (OutputFilename.empty())
540 OutputFilename = "-";
Chris Lattner61db1a12009-10-22 00:46:41 +0000541
Dan Gohman86cbc1b2010-08-18 17:40:10 +0000542 std::string ErrorInfo;
Dan Gohmand5826a32010-08-20 01:07:01 +0000543 Out.reset(new tool_output_file(OutputFilename.c_str(), ErrorInfo,
544 raw_fd_ostream::F_Binary));
Dan Gohman86cbc1b2010-08-18 17:40:10 +0000545 if (!ErrorInfo.empty()) {
546 errs() << ErrorInfo << '\n';
Dan Gohman86cbc1b2010-08-18 17:40:10 +0000547 return 1;
Chris Lattner00950542001-06-06 20:29:01 +0000548 }
Chris Lattner61db1a12009-10-22 00:46:41 +0000549 }
Chris Lattner76d12292002-04-18 19:55:25 +0000550
Chris Lattner61db1a12009-10-22 00:46:41 +0000551 // If the output is set to be emitted to standard out, and standard out is a
552 // console, print out a warning message and refuse to do it. We don't
553 // impress anyone by spewing tons of binary goo to a terminal.
Dan Gohmanb56bf582010-01-17 17:47:24 +0000554 if (!Force && !NoOutput && !AnalyzeOnly && !OutputAssembly)
Dan Gohmand4c45432010-09-01 14:20:41 +0000555 if (CheckBitcodeOutputToConsole(Out->os(), !Quiet))
Chris Lattner61db1a12009-10-22 00:46:41 +0000556 NoOutput = true;
Dan Gohmanec080462009-09-11 20:46:33 +0000557
Chris Lattner61db1a12009-10-22 00:46:41 +0000558 // Create a PassManager to hold and optimize the collection of passes we are
Chris Lattner2a66aca2011-02-18 22:13:01 +0000559 // about to build.
Chris Lattner61db1a12009-10-22 00:46:41 +0000560 //
561 PassManager Passes;
Chris Lattner00950542001-06-06 20:29:01 +0000562
Chris Lattner2a66aca2011-02-18 22:13:01 +0000563 // Add an appropriate TargetLibraryInfo pass for the module's triple.
Chris Lattner188a7e02011-02-18 22:34:03 +0000564 TargetLibraryInfo *TLI = new TargetLibraryInfo(Triple(M->getTargetTriple()));
Andrew Tricka41af7a2011-04-05 18:41:31 +0000565
Chris Lattner188a7e02011-02-18 22:34:03 +0000566 // The -disable-simplify-libcalls flag actually disables all builtin optzns.
567 if (DisableSimplifyLibCalls)
568 TLI->disableAllFunctions();
569 Passes.add(TLI);
Andrew Tricka41af7a2011-04-05 18:41:31 +0000570
Micah Villmow791cfc22012-10-08 16:39:34 +0000571 // Add an appropriate DataLayout instance for this module.
572 DataLayout *TD = 0;
Chris Lattner61db1a12009-10-22 00:46:41 +0000573 const std::string &ModuleDataLayout = M.get()->getDataLayout();
574 if (!ModuleDataLayout.empty())
Micah Villmow791cfc22012-10-08 16:39:34 +0000575 TD = new DataLayout(ModuleDataLayout);
Kenneth Uildriksb908f8a2009-11-03 15:29:06 +0000576 else if (!DefaultDataLayout.empty())
Micah Villmow791cfc22012-10-08 16:39:34 +0000577 TD = new DataLayout(DefaultDataLayout);
Chris Lattner9c3b55e2003-04-24 19:13:02 +0000578
Chris Lattner61db1a12009-10-22 00:46:41 +0000579 if (TD)
580 Passes.add(TD);
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000581
Chris Lattner52b28892011-05-22 00:21:33 +0000582 OwningPtr<FunctionPassManager> FPasses;
Chandler Carruth8bc34342012-05-16 08:32:49 +0000583 if (OptLevelO1 || OptLevelO2 || OptLevelOs || OptLevelOz || OptLevelO3) {
Chris Lattner52b28892011-05-22 00:21:33 +0000584 FPasses.reset(new FunctionPassManager(M.get()));
Chris Lattnerd331cb32009-10-22 00:44:10 +0000585 if (TD)
Micah Villmow791cfc22012-10-08 16:39:34 +0000586 FPasses->add(new DataLayout(*TD));
Chris Lattner61db1a12009-10-22 00:46:41 +0000587 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000588
Devang Patelf5f23002010-12-07 00:33:43 +0000589 if (PrintBreakpoints) {
590 // Default to standard output.
591 if (!Out) {
592 if (OutputFilename.empty())
593 OutputFilename = "-";
Andrew Tricka41af7a2011-04-05 18:41:31 +0000594
Devang Patelf5f23002010-12-07 00:33:43 +0000595 std::string ErrorInfo;
596 Out.reset(new tool_output_file(OutputFilename.c_str(), ErrorInfo,
597 raw_fd_ostream::F_Binary));
598 if (!ErrorInfo.empty()) {
599 errs() << ErrorInfo << '\n';
600 return 1;
601 }
602 }
603 Passes.add(new BreakpointPrinter(Out->os()));
604 NoOutput = true;
605 }
606
Chris Lattner61db1a12009-10-22 00:46:41 +0000607 // If the -strip-debug command line option was specified, add it. If
608 // -std-compile-opts was also specified, it will handle StripDebug.
609 if (StripDebug && !StandardCompileOpts)
610 addPass(Passes, createStripSymbolsPass(true));
Eric Christophera887ae42009-08-21 23:29:40 +0000611
Chris Lattner61db1a12009-10-22 00:46:41 +0000612 // Create a new optimization pass for each one specified on the command line
613 for (unsigned i = 0; i < PassList.size(); ++i) {
614 // Check to see if -std-compile-opts was specified before this option. If
615 // so, handle it.
616 if (StandardCompileOpts &&
617 StandardCompileOpts.getPosition() < PassList.getPosition(i)) {
Chris Lattner3dda08a2008-07-13 19:35:21 +0000618 AddStandardCompilePasses(Passes);
619 StandardCompileOpts = false;
Eric Christophera887ae42009-08-21 23:29:40 +0000620 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000621
Chris Lattner61db1a12009-10-22 00:46:41 +0000622 if (StandardLinkOpts &&
623 StandardLinkOpts.getPosition() < PassList.getPosition(i)) {
Daniel Dunbaradc82882009-07-17 18:09:39 +0000624 AddStandardLinkPasses(Passes);
625 StandardLinkOpts = false;
Eric Christophera887ae42009-08-21 23:29:40 +0000626 }
Daniel Dunbaradc82882009-07-17 18:09:39 +0000627
Chris Lattner61db1a12009-10-22 00:46:41 +0000628 if (OptLevelO1 && OptLevelO1.getPosition() < PassList.getPosition(i)) {
Chandler Carruth8bc34342012-05-16 08:32:49 +0000629 AddOptimizationPasses(Passes, *FPasses, 1, 0);
Chris Lattner61db1a12009-10-22 00:46:41 +0000630 OptLevelO1 = false;
Daniel Dunbaradc82882009-07-17 18:09:39 +0000631 }
Devang Patel2d7551c2008-09-16 22:25:14 +0000632
Chris Lattner61db1a12009-10-22 00:46:41 +0000633 if (OptLevelO2 && OptLevelO2.getPosition() < PassList.getPosition(i)) {
Chandler Carruth8bc34342012-05-16 08:32:49 +0000634 AddOptimizationPasses(Passes, *FPasses, 2, 0);
Chris Lattner61db1a12009-10-22 00:46:41 +0000635 OptLevelO2 = false;
Daniel Dunbaradc82882009-07-17 18:09:39 +0000636 }
Devang Patel2d7551c2008-09-16 22:25:14 +0000637
Chandler Carruth8bc34342012-05-16 08:32:49 +0000638 if (OptLevelOs && OptLevelOs.getPosition() < PassList.getPosition(i)) {
639 AddOptimizationPasses(Passes, *FPasses, 2, 1);
640 OptLevelOs = false;
641 }
642
643 if (OptLevelOz && OptLevelOz.getPosition() < PassList.getPosition(i)) {
644 AddOptimizationPasses(Passes, *FPasses, 2, 2);
645 OptLevelOz = false;
646 }
647
Chris Lattner61db1a12009-10-22 00:46:41 +0000648 if (OptLevelO3 && OptLevelO3.getPosition() < PassList.getPosition(i)) {
Chandler Carruth8bc34342012-05-16 08:32:49 +0000649 AddOptimizationPasses(Passes, *FPasses, 3, 0);
Chris Lattner61db1a12009-10-22 00:46:41 +0000650 OptLevelO3 = false;
Daniel Dunbaradc82882009-07-17 18:09:39 +0000651 }
Devang Patel2d7551c2008-09-16 22:25:14 +0000652
Chris Lattner61db1a12009-10-22 00:46:41 +0000653 const PassInfo *PassInf = PassList[i];
654 Pass *P = 0;
655 if (PassInf->getNormalCtor())
656 P = PassInf->getNormalCtor()();
657 else
658 errs() << argv[0] << ": cannot create pass: "
659 << PassInf->getPassName() << "\n";
660 if (P) {
Benjamin Kramer3460f222010-02-18 12:57:05 +0000661 PassKind Kind = P->getPassKind();
Chris Lattner61db1a12009-10-22 00:46:41 +0000662 addPass(Passes, P);
663
664 if (AnalyzeOnly) {
Benjamin Kramer3460f222010-02-18 12:57:05 +0000665 switch (Kind) {
Chris Lattner476e9bd2010-01-22 06:03:06 +0000666 case PT_BasicBlock:
Dan Gohmand4c45432010-09-01 14:20:41 +0000667 Passes.add(new BasicBlockPassPrinter(PassInf, Out->os()));
Chris Lattner476e9bd2010-01-22 06:03:06 +0000668 break;
Tobias Grosser65513602010-10-20 01:54:44 +0000669 case PT_Region:
670 Passes.add(new RegionPassPrinter(PassInf, Out->os()));
671 break;
Chris Lattner476e9bd2010-01-22 06:03:06 +0000672 case PT_Loop:
Dan Gohmand4c45432010-09-01 14:20:41 +0000673 Passes.add(new LoopPassPrinter(PassInf, Out->os()));
Chris Lattner476e9bd2010-01-22 06:03:06 +0000674 break;
675 case PT_Function:
Dan Gohmand4c45432010-09-01 14:20:41 +0000676 Passes.add(new FunctionPassPrinter(PassInf, Out->os()));
Chris Lattner476e9bd2010-01-22 06:03:06 +0000677 break;
678 case PT_CallGraphSCC:
Dan Gohmand4c45432010-09-01 14:20:41 +0000679 Passes.add(new CallGraphSCCPassPrinter(PassInf, Out->os()));
Chris Lattner476e9bd2010-01-22 06:03:06 +0000680 break;
681 default:
Dan Gohmand4c45432010-09-01 14:20:41 +0000682 Passes.add(new ModulePassPrinter(PassInf, Out->os()));
Chris Lattner476e9bd2010-01-22 06:03:06 +0000683 break;
684 }
Chris Lattner61db1a12009-10-22 00:46:41 +0000685 }
Devang Patel2d7551c2008-09-16 22:25:14 +0000686 }
687
Chris Lattner61db1a12009-10-22 00:46:41 +0000688 if (PrintEachXForm)
689 Passes.add(createPrintModulePass(&errs()));
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000690 }
Chris Lattner61db1a12009-10-22 00:46:41 +0000691
692 // If -std-compile-opts was specified at the end of the pass list, add them.
693 if (StandardCompileOpts) {
694 AddStandardCompilePasses(Passes);
695 StandardCompileOpts = false;
696 }
697
698 if (StandardLinkOpts) {
699 AddStandardLinkPasses(Passes);
700 StandardLinkOpts = false;
701 }
702
703 if (OptLevelO1)
Chandler Carruth8bc34342012-05-16 08:32:49 +0000704 AddOptimizationPasses(Passes, *FPasses, 1, 0);
Chris Lattner61db1a12009-10-22 00:46:41 +0000705
706 if (OptLevelO2)
Chandler Carruth8bc34342012-05-16 08:32:49 +0000707 AddOptimizationPasses(Passes, *FPasses, 2, 0);
708
709 if (OptLevelOs)
710 AddOptimizationPasses(Passes, *FPasses, 2, 1);
711
712 if (OptLevelOz)
713 AddOptimizationPasses(Passes, *FPasses, 2, 2);
Chris Lattner61db1a12009-10-22 00:46:41 +0000714
715 if (OptLevelO3)
Chandler Carruth8bc34342012-05-16 08:32:49 +0000716 AddOptimizationPasses(Passes, *FPasses, 3, 0);
Chris Lattner61db1a12009-10-22 00:46:41 +0000717
Chandler Carruth8bc34342012-05-16 08:32:49 +0000718 if (OptLevelO1 || OptLevelO2 || OptLevelOs || OptLevelOz || OptLevelO3) {
Chris Lattner3e8984a2011-05-22 06:44:19 +0000719 FPasses->doInitialization();
Chris Lattner52b28892011-05-22 00:21:33 +0000720 for (Module::iterator F = M->begin(), E = M->end(); F != E; ++F)
721 FPasses->run(*F);
Chris Lattner3e8984a2011-05-22 06:44:19 +0000722 FPasses->doFinalization();
723 }
Chris Lattner61db1a12009-10-22 00:46:41 +0000724
725 // Check that the module is well formed on completion of optimization
726 if (!NoVerify && !VerifyEach)
727 Passes.add(createVerifierPass());
728
Dan Gohman4931b312010-08-18 17:42:59 +0000729 // Write bitcode or assembly to the output as the last step...
Chris Lattner61db1a12009-10-22 00:46:41 +0000730 if (!NoOutput && !AnalyzeOnly) {
731 if (OutputAssembly)
Dan Gohmand4c45432010-09-01 14:20:41 +0000732 Passes.add(createPrintModulePass(&Out->os()));
Chris Lattner61db1a12009-10-22 00:46:41 +0000733 else
Dan Gohmand4c45432010-09-01 14:20:41 +0000734 Passes.add(createBitcodeWriterPass(Out->os()));
Chris Lattner61db1a12009-10-22 00:46:41 +0000735 }
736
Andrew Trickce969022011-04-05 18:54:36 +0000737 // Before executing passes, print the final values of the LLVM options.
738 cl::PrintOptionValues();
739
Chris Lattner61db1a12009-10-22 00:46:41 +0000740 // Now that we have all of the passes ready, run them.
741 Passes.run(*M.get());
742
Dan Gohmand5826a32010-08-20 01:07:01 +0000743 // Declare success.
Devang Patelf5f23002010-12-07 00:33:43 +0000744 if (!NoOutput || PrintBreakpoints)
Dan Gohmand5826a32010-08-20 01:07:01 +0000745 Out->keep();
746
Chris Lattner61db1a12009-10-22 00:46:41 +0000747 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000748}