blob: bac0d469479986e99fce797b5dfa65b3e68aa7b9 [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"
Nadav Rotemcbd9a192012-10-18 23:22:48 +000021#include "llvm/CodeGen/CommandFlags.h"
Chris Lattnerb330e382007-05-06 02:42:03 +000022#include "llvm/Bitcode/ReaderWriter.h"
Chris Lattnerffa6f9c2001-10-19 15:39:14 +000023#include "llvm/Assembly/PrintModulePass.h"
Chris Lattner22d26d72002-02-20 17:56:53 +000024#include "llvm/Analysis/Verifier.h"
Devang Patel1bc89362007-03-07 00:26:10 +000025#include "llvm/Analysis/LoopPass.h"
Tobias Grosser65513602010-10-20 01:54:44 +000026#include "llvm/Analysis/RegionPass.h"
Devang Patel28552da2007-06-28 23:09:25 +000027#include "llvm/Analysis/CallGraph.h"
Chris Lattner2a66aca2011-02-18 22:13:01 +000028#include "llvm/Target/TargetLibraryInfo.h"
Vikram S. Adve18fdfc42002-09-16 16:09:43 +000029#include "llvm/Target/TargetMachine.h"
Devang Patel1be99802011-04-04 19:51:17 +000030#include "llvm/ADT/StringSet.h"
Chris Lattner2a66aca2011-02-18 22:13:01 +000031#include "llvm/ADT/Triple.h"
Chris Lattner2053a2a2002-07-26 21:09:32 +000032#include "llvm/Support/PassNameParser.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000033#include "llvm/Support/Signals.h"
David Greene08fc0d32010-01-05 01:30:32 +000034#include "llvm/Support/Debug.h"
Dan Gohman99ed4162009-09-03 16:00:08 +000035#include "llvm/Support/IRReader.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000036#include "llvm/Support/ManagedStatic.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000037#include "llvm/Support/PluginLoader.h"
Chris Lattnerc0d91b72009-12-09 00:41:28 +000038#include "llvm/Support/PrettyStackTrace.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000039#include "llvm/Support/SystemUtils.h"
Nadav Rotemcbd9a192012-10-18 23:22:48 +000040#include "llvm/Support/TargetRegistry.h"
Dan Gohmane4f1a9b2010-10-07 20:32:40 +000041#include "llvm/Support/ToolOutputFile.h"
Nadav Rotem0873bea2012-10-24 17:23:50 +000042#include "llvm/Support/TargetSelect.h"
Nadav Rotemcbd9a192012-10-18 23:22:48 +000043#include "llvm/MC/SubtargetFeature.h"
Reid Spencer62c51052006-08-21 05:34:03 +000044#include "llvm/LinkAllPasses.h"
Reid Spenceraf303d52006-06-07 23:03:13 +000045#include "llvm/LinkAllVMCore.h"
Rafael Espindola3d453ac2011-08-02 21:50:24 +000046#include "llvm/Transforms/IPO/PassManagerBuilder.h"
Chris Lattner63202322001-11-26 19:22:39 +000047#include <memory>
Chris Lattnerc0ce68b2002-07-23 18:12:22 +000048#include <algorithm>
Brian Gaeked0fde302003-11-11 22:41:34 +000049using namespace llvm;
Chris Lattner9d6e7eb2002-04-12 18:21:13 +000050
Chris Lattnerc0ce68b2002-07-23 18:12:22 +000051// The OptimizationList is automatically populated with registered Passes by the
52// PassNameParser.
53//
Chris Lattner7f500f72006-08-27 22:07:01 +000054static cl::list<const PassInfo*, bool, PassNameParser>
55PassList(cl::desc("Optimizations available:"));
Chris Lattnerc0ce68b2002-07-23 18:12:22 +000056
Chris Lattnerc0ce68b2002-07-23 18:12:22 +000057// Other command line options...
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000058//
Chris Lattner6c8103f2003-05-22 20:13:16 +000059static cl::opt<std::string>
Eric Christophera887ae42009-08-21 23:29:40 +000060InputFilename(cl::Positional, cl::desc("<input bitcode file>"),
Reid Spencerfd90dd52006-08-18 06:34:30 +000061 cl::init("-"), cl::value_desc("filename"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000062
Chris Lattner6c8103f2003-05-22 20:13:16 +000063static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000064OutputFilename("o", cl::desc("Override output filename"),
Dan Gohman86cbc1b2010-08-18 17:40:10 +000065 cl::value_desc("filename"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000066
67static cl::opt<bool>
Dan Gohmanbaa26392009-08-25 15:34:52 +000068Force("f", cl::desc("Enable binary output on terminals"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000069
70static cl::opt<bool>
71PrintEachXForm("p", cl::desc("Print module after each transformation"));
72
73static cl::opt<bool>
Chris Lattnerddd5b412003-02-26 20:00:41 +000074NoOutput("disable-output",
Gabor Greifa99be512007-07-05 17:07:56 +000075 cl::desc("Do not write result bitcode file"), cl::Hidden);
Chris Lattnerd70b68e2003-02-12 18:43:33 +000076
77static cl::opt<bool>
Duncan Sands81b0b642009-10-14 20:01:39 +000078OutputAssembly("S", cl::desc("Write output as LLVM assembly"));
Daniel Dunbar8c042c22009-09-05 11:34:53 +000079
80static cl::opt<bool>
Chris Lattnerddd5b412003-02-26 20:00:41 +000081NoVerify("disable-verify", cl::desc("Do not verify result module"), cl::Hidden);
Chris Lattnerf3bafc12003-02-12 18:45:08 +000082
83static cl::opt<bool>
Reid Spencer74ed9972007-02-02 14:46:29 +000084VerifyEach("verify-each", cl::desc("Verify after each transform"));
85
86static cl::opt<bool>
87StripDebug("strip-debug",
88 cl::desc("Strip debugger symbol info from translation unit"));
89
90static cl::opt<bool>
91DisableInline("disable-inlining", cl::desc("Do not run the inliner pass"));
92
Eric Christophera887ae42009-08-21 23:29:40 +000093static cl::opt<bool>
94DisableOptimizations("disable-opt",
Reid Spencer74ed9972007-02-02 14:46:29 +000095 cl::desc("Do not run any optimization passes"));
96
Eric Christophera887ae42009-08-21 23:29:40 +000097static cl::opt<bool>
Daniel Dunbaradc82882009-07-17 18:09:39 +000098DisableInternalize("disable-internalize",
99 cl::desc("Do not mark all symbols as internal"));
100
Reid Spencer74ed9972007-02-02 14:46:29 +0000101static cl::opt<bool>
Eric Christophera887ae42009-08-21 23:29:40 +0000102StandardCompileOpts("std-compile-opts",
Reid Spencer74ed9972007-02-02 14:46:29 +0000103 cl::desc("Include the standard compile time optimizations"));
104
105static cl::opt<bool>
Eric Christophera887ae42009-08-21 23:29:40 +0000106StandardLinkOpts("std-link-opts",
Daniel Dunbaradc82882009-07-17 18:09:39 +0000107 cl::desc("Include the standard link time optimizations"));
108
109static cl::opt<bool>
Devang Patel2d7551c2008-09-16 22:25:14 +0000110OptLevelO1("O1",
Chandler Carruth8bc34342012-05-16 08:32:49 +0000111 cl::desc("Optimization level 1. Similar to clang -O1"));
Devang Patel2d7551c2008-09-16 22:25:14 +0000112
113static cl::opt<bool>
114OptLevelO2("O2",
Chandler Carruth8bc34342012-05-16 08:32:49 +0000115 cl::desc("Optimization level 2. Similar to clang -O2"));
116
117static cl::opt<bool>
118OptLevelOs("Os",
119 cl::desc("Like -O2 with extra optimizations for size. Similar to clang -Os"));
120
121static cl::opt<bool>
122OptLevelOz("Oz",
123 cl::desc("Like -Os but reduces code size further. Similar to clang -Oz"));
Devang Patel2d7551c2008-09-16 22:25:14 +0000124
125static cl::opt<bool>
126OptLevelO3("O3",
Chandler Carruth8bc34342012-05-16 08:32:49 +0000127 cl::desc("Optimization level 3. Similar to clang -O3"));
Devang Patel2d7551c2008-09-16 22:25:14 +0000128
Joe Groffe652b522012-04-17 23:05:48 +0000129static cl::opt<std::string>
130TargetTriple("mtriple", cl::desc("Override target triple for module"));
131
Devang Patel2d7551c2008-09-16 22:25:14 +0000132static cl::opt<bool>
133UnitAtATime("funit-at-a-time",
Eric Christopherc4769ba2009-08-21 23:30:30 +0000134 cl::desc("Enable IPO. This is same as llvm-gcc's -funit-at-a-time"),
Duncan Sands34727662010-07-12 08:16:59 +0000135 cl::init(true));
Devang Patel2d7551c2008-09-16 22:25:14 +0000136
137static cl::opt<bool>
138DisableSimplifyLibCalls("disable-simplify-libcalls",
Devang Patel442b1aa2008-09-17 16:01:39 +0000139 cl::desc("Disable simplify-libcalls"));
Devang Patel2d7551c2008-09-16 22:25:14 +0000140
141static cl::opt<bool>
Chris Lattner3153e4f2004-05-27 20:32:10 +0000142Quiet("q", cl::desc("Obsolete option"), cl::Hidden);
Chris Lattner5ff62e92002-07-22 02:10:13 +0000143
Reid Spencerec7eb452004-05-27 16:28:54 +0000144static cl::alias
145QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet));
146
Reid Spencerfd90dd52006-08-18 06:34:30 +0000147static cl::opt<bool>
148AnalyzeOnly("analyze", cl::desc("Only perform analysis, no optimization"));
149
Devang Patelf5f23002010-12-07 00:33:43 +0000150static cl::opt<bool>
Andrew Tricka41af7a2011-04-05 18:41:31 +0000151PrintBreakpoints("print-breakpoints-for-testing",
Devang Patelf5f23002010-12-07 00:33:43 +0000152 cl::desc("Print select breakpoints location for testing"));
153
Chris Lattnerd331cb32009-10-22 00:44:10 +0000154static cl::opt<std::string>
Andrew Tricka41af7a2011-04-05 18:41:31 +0000155DefaultDataLayout("default-data-layout",
Chris Lattnerd331cb32009-10-22 00:44:10 +0000156 cl::desc("data layout string to use if not specified by module"),
157 cl::value_desc("layout-string"), cl::init(""));
158
Reid Spencerfd90dd52006-08-18 06:34:30 +0000159// ---------- Define Printers for module and function passes ------------
160namespace {
161
Devang Patel28552da2007-06-28 23:09:25 +0000162struct CallGraphSCCPassPrinter : public CallGraphSCCPass {
163 static char ID;
164 const PassInfo *PassToPrint;
Dan Gohman4931b312010-08-18 17:42:59 +0000165 raw_ostream &Out;
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000166 std::string PassName;
167
Dan Gohman4931b312010-08-18 17:42:59 +0000168 CallGraphSCCPassPrinter(const PassInfo *PI, raw_ostream &out) :
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000169 CallGraphSCCPass(ID), PassToPrint(PI), Out(out) {
170 std::string PassToPrintName = PassToPrint->getPassName();
171 PassName = "CallGraphSCCPass Printer: " + PassToPrintName;
172 }
Devang Patel28552da2007-06-28 23:09:25 +0000173
Chris Lattner2decb222010-04-16 22:42:17 +0000174 virtual bool runOnSCC(CallGraphSCC &SCC) {
Tobias Grossere9dcd032010-09-08 15:02:51 +0000175 if (!Quiet)
Dan Gohman4931b312010-08-18 17:42:59 +0000176 Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
Devang Patel28552da2007-06-28 23:09:25 +0000177
Devang Patel28552da2007-06-28 23:09:25 +0000178 // Get and print pass...
Tobias Grossere9dcd032010-09-08 15:02:51 +0000179 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
180 Function *F = (*I)->getFunction();
181 if (F)
182 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
183 F->getParent());
184 }
Devang Patel28552da2007-06-28 23:09:25 +0000185 return false;
186 }
Eric Christophera887ae42009-08-21 23:29:40 +0000187
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000188 virtual const char *getPassName() const { return PassName.c_str(); }
Devang Patel28552da2007-06-28 23:09:25 +0000189
190 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Owen Anderson90c579d2010-08-06 18:33:48 +0000191 AU.addRequiredID(PassToPrint->getTypeInfo());
Devang Patel28552da2007-06-28 23:09:25 +0000192 AU.setPreservesAll();
193 }
194};
195
196char CallGraphSCCPassPrinter::ID = 0;
197
Reid Spencerfd90dd52006-08-18 06:34:30 +0000198struct ModulePassPrinter : public ModulePass {
Devang Patel19974732007-05-03 01:11:54 +0000199 static char ID;
Reid Spencerfd90dd52006-08-18 06:34:30 +0000200 const PassInfo *PassToPrint;
Dan Gohman4931b312010-08-18 17:42:59 +0000201 raw_ostream &Out;
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000202 std::string PassName;
203
Dan Gohman4931b312010-08-18 17:42:59 +0000204 ModulePassPrinter(const PassInfo *PI, raw_ostream &out)
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000205 : ModulePass(ID), PassToPrint(PI), Out(out) {
206 std::string PassToPrintName = PassToPrint->getPassName();
207 PassName = "ModulePass Printer: " + PassToPrintName;
208 }
Reid Spencerfd90dd52006-08-18 06:34:30 +0000209
210 virtual bool runOnModule(Module &M) {
Tobias Grossere9dcd032010-09-08 15:02:51 +0000211 if (!Quiet)
Dan Gohman4931b312010-08-18 17:42:59 +0000212 Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
Reid Spencerfd90dd52006-08-18 06:34:30 +0000213
214 // Get and print pass...
Tobias Grossere9dcd032010-09-08 15:02:51 +0000215 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out, &M);
Reid Spencerfd90dd52006-08-18 06:34:30 +0000216 return false;
217 }
218
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000219 virtual const char *getPassName() const { return PassName.c_str(); }
Reid Spencerfd90dd52006-08-18 06:34:30 +0000220
221 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Owen Anderson90c579d2010-08-06 18:33:48 +0000222 AU.addRequiredID(PassToPrint->getTypeInfo());
Reid Spencerfd90dd52006-08-18 06:34:30 +0000223 AU.setPreservesAll();
224 }
225};
226
Devang Patel19974732007-05-03 01:11:54 +0000227char ModulePassPrinter::ID = 0;
Reid Spencerfd90dd52006-08-18 06:34:30 +0000228struct FunctionPassPrinter : public FunctionPass {
229 const PassInfo *PassToPrint;
Dan Gohman4931b312010-08-18 17:42:59 +0000230 raw_ostream &Out;
Devang Patel19974732007-05-03 01:11:54 +0000231 static char ID;
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000232 std::string PassName;
233
Dan Gohman4931b312010-08-18 17:42:59 +0000234 FunctionPassPrinter(const PassInfo *PI, raw_ostream &out)
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000235 : FunctionPass(ID), PassToPrint(PI), Out(out) {
236 std::string PassToPrintName = PassToPrint->getPassName();
237 PassName = "FunctionPass Printer: " + PassToPrintName;
238 }
Reid Spencerfd90dd52006-08-18 06:34:30 +0000239
240 virtual bool runOnFunction(Function &F) {
Tobias Grossere9dcd032010-09-08 15:02:51 +0000241 if (!Quiet)
Dan Gohman4931b312010-08-18 17:42:59 +0000242 Out << "Printing analysis '" << PassToPrint->getPassName()
243 << "' for function '" << F.getName() << "':\n";
Tobias Grossere9dcd032010-09-08 15:02:51 +0000244
Reid Spencerfd90dd52006-08-18 06:34:30 +0000245 // Get and print pass...
Dan Gohman4931b312010-08-18 17:42:59 +0000246 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
Owen Anderson90c579d2010-08-06 18:33:48 +0000247 F.getParent());
Reid Spencerfd90dd52006-08-18 06:34:30 +0000248 return false;
249 }
250
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000251 virtual const char *getPassName() const { return PassName.c_str(); }
Reid Spencerfd90dd52006-08-18 06:34:30 +0000252
253 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Owen Anderson90c579d2010-08-06 18:33:48 +0000254 AU.addRequiredID(PassToPrint->getTypeInfo());
Reid Spencerfd90dd52006-08-18 06:34:30 +0000255 AU.setPreservesAll();
256 }
257};
258
Devang Patel19974732007-05-03 01:11:54 +0000259char FunctionPassPrinter::ID = 0;
Devang Patel56fb1642007-07-05 15:32:03 +0000260
261struct LoopPassPrinter : public LoopPass {
262 static char ID;
263 const PassInfo *PassToPrint;
Dan Gohman4931b312010-08-18 17:42:59 +0000264 raw_ostream &Out;
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000265 std::string PassName;
266
Dan Gohman4931b312010-08-18 17:42:59 +0000267 LoopPassPrinter(const PassInfo *PI, raw_ostream &out) :
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000268 LoopPass(ID), PassToPrint(PI), Out(out) {
269 std::string PassToPrintName = PassToPrint->getPassName();
270 PassName = "LoopPass Printer: " + PassToPrintName;
271 }
272
Devang Patel56fb1642007-07-05 15:32:03 +0000273
274 virtual bool runOnLoop(Loop *L, LPPassManager &LPM) {
Tobias Grossere9dcd032010-09-08 15:02:51 +0000275 if (!Quiet)
Dan Gohman4931b312010-08-18 17:42:59 +0000276 Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
Tobias Grossere9dcd032010-09-08 15:02:51 +0000277
Devang Patel56fb1642007-07-05 15:32:03 +0000278 // Get and print pass...
Tobias Grossere9dcd032010-09-08 15:02:51 +0000279 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
280 L->getHeader()->getParent()->getParent());
Devang Patel56fb1642007-07-05 15:32:03 +0000281 return false;
282 }
Eric Christophera887ae42009-08-21 23:29:40 +0000283
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000284 virtual const char *getPassName() const { return PassName.c_str(); }
Devang Patel56fb1642007-07-05 15:32:03 +0000285
286 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Owen Anderson90c579d2010-08-06 18:33:48 +0000287 AU.addRequiredID(PassToPrint->getTypeInfo());
Devang Patel56fb1642007-07-05 15:32:03 +0000288 AU.setPreservesAll();
289 }
290};
291
292char LoopPassPrinter::ID = 0;
293
Tobias Grosser65513602010-10-20 01:54:44 +0000294struct RegionPassPrinter : public RegionPass {
295 static char ID;
296 const PassInfo *PassToPrint;
297 raw_ostream &Out;
298 std::string PassName;
299
300 RegionPassPrinter(const PassInfo *PI, raw_ostream &out) : RegionPass(ID),
301 PassToPrint(PI), Out(out) {
302 std::string PassToPrintName = PassToPrint->getPassName();
Tobias Grosser58396142011-01-20 21:03:20 +0000303 PassName = "RegionPass Printer: " + PassToPrintName;
Tobias Grosser65513602010-10-20 01:54:44 +0000304 }
305
306 virtual bool runOnRegion(Region *R, RGPassManager &RGM) {
307 if (!Quiet) {
308 Out << "Printing analysis '" << PassToPrint->getPassName() << "' for "
Benjamin Kramera7b0cb72011-11-15 16:27:03 +0000309 << "region: '" << R->getNameStr() << "' in function '"
310 << R->getEntry()->getParent()->getName() << "':\n";
Tobias Grosser65513602010-10-20 01:54:44 +0000311 }
312 // Get and print pass...
313 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
314 R->getEntry()->getParent()->getParent());
315 return false;
316 }
317
Tobias Grosser58396142011-01-20 21:03:20 +0000318 virtual const char *getPassName() const { return PassName.c_str(); }
Tobias Grosser65513602010-10-20 01:54:44 +0000319
320 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
321 AU.addRequiredID(PassToPrint->getTypeInfo());
322 AU.setPreservesAll();
323 }
324};
325
326char RegionPassPrinter::ID = 0;
327
Reid Spencerfd90dd52006-08-18 06:34:30 +0000328struct BasicBlockPassPrinter : public BasicBlockPass {
329 const PassInfo *PassToPrint;
Dan Gohman4931b312010-08-18 17:42:59 +0000330 raw_ostream &Out;
Devang Patel19974732007-05-03 01:11:54 +0000331 static char ID;
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000332 std::string PassName;
333
Dan Gohman4931b312010-08-18 17:42:59 +0000334 BasicBlockPassPrinter(const PassInfo *PI, raw_ostream &out)
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000335 : BasicBlockPass(ID), PassToPrint(PI), Out(out) {
336 std::string PassToPrintName = PassToPrint->getPassName();
337 PassName = "BasicBlockPass Printer: " + PassToPrintName;
338 }
Reid Spencerfd90dd52006-08-18 06:34:30 +0000339
340 virtual bool runOnBasicBlock(BasicBlock &BB) {
Tobias Grossere9dcd032010-09-08 15:02:51 +0000341 if (!Quiet)
Dan Gohman4931b312010-08-18 17:42:59 +0000342 Out << "Printing Analysis info for BasicBlock '" << BB.getName()
343 << "': Pass " << PassToPrint->getPassName() << ":\n";
Reid Spencerfd90dd52006-08-18 06:34:30 +0000344
345 // Get and print pass...
Andrew Tricka41af7a2011-04-05 18:41:31 +0000346 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
Owen Anderson90c579d2010-08-06 18:33:48 +0000347 BB.getParent()->getParent());
Reid Spencerfd90dd52006-08-18 06:34:30 +0000348 return false;
349 }
350
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000351 virtual const char *getPassName() const { return PassName.c_str(); }
Reid Spencerfd90dd52006-08-18 06:34:30 +0000352
353 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Owen Anderson90c579d2010-08-06 18:33:48 +0000354 AU.addRequiredID(PassToPrint->getTypeInfo());
Reid Spencerfd90dd52006-08-18 06:34:30 +0000355 AU.setPreservesAll();
356 }
357};
358
Devang Patel19974732007-05-03 01:11:54 +0000359char BasicBlockPassPrinter::ID = 0;
Devang Patelf5f23002010-12-07 00:33:43 +0000360
Devang Patel1be99802011-04-04 19:51:17 +0000361struct BreakpointPrinter : public ModulePass {
Devang Patelf5f23002010-12-07 00:33:43 +0000362 raw_ostream &Out;
363 static char ID;
364
365 BreakpointPrinter(raw_ostream &out)
Devang Patel1be99802011-04-04 19:51:17 +0000366 : ModulePass(ID), Out(out) {
Devang Patelf5f23002010-12-07 00:33:43 +0000367 }
368
Devang Patel1be99802011-04-04 19:51:17 +0000369 void getContextName(DIDescriptor Context, std::string &N) {
370 if (Context.isNameSpace()) {
371 DINameSpace NS(Context);
372 if (!NS.getName().empty()) {
373 getContextName(NS.getContext(), N);
374 N = N + NS.getName().str() + "::";
Devang Patel97f6d5b2011-01-31 21:36:24 +0000375 }
Devang Patel1be99802011-04-04 19:51:17 +0000376 } else if (Context.isType()) {
377 DIType TY(Context);
378 if (!TY.getName().empty()) {
379 getContextName(TY.getContext(), N);
380 N = N + TY.getName().str() + "::";
381 }
382 }
383 }
384
385 virtual bool runOnModule(Module &M) {
386 StringSet<> Processed;
387 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
388 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
389 std::string Name;
390 DISubprogram SP(NMD->getOperand(i));
391 if (SP.Verify())
392 getContextName(SP.getContext(), Name);
393 Name = Name + SP.getDisplayName().str();
394 if (!Name.empty() && Processed.insert(Name)) {
395 Out << Name << "\n";
396 }
397 }
Devang Patelf5f23002010-12-07 00:33:43 +0000398 return false;
399 }
400
401 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
402 AU.setPreservesAll();
403 }
404};
Chris Lattner52b28892011-05-22 00:21:33 +0000405
406} // anonymous namespace
Devang Patelf5f23002010-12-07 00:33:43 +0000407
408char BreakpointPrinter::ID = 0;
409
Chris Lattner52b28892011-05-22 00:21:33 +0000410static inline void addPass(PassManagerBase &PM, Pass *P) {
Reid Spencer74ed9972007-02-02 14:46:29 +0000411 // Add the pass to the pass manager...
412 PM.add(P);
413
414 // If we are verifying all of the intermediate steps, add the verifier...
415 if (VerifyEach) PM.add(createVerifierPass());
416}
417
Eric Christophera887ae42009-08-21 23:29:40 +0000418/// AddOptimizationPasses - This routine adds optimization passes
419/// based on selected optimization level, OptLevel. This routine
Devang Patel2d7551c2008-09-16 22:25:14 +0000420/// duplicates llvm-gcc behaviour.
421///
422/// OptLevel - Optimization Level
Chris Lattner52b28892011-05-22 00:21:33 +0000423static void AddOptimizationPasses(PassManagerBase &MPM,FunctionPassManager &FPM,
Chandler Carruth8bc34342012-05-16 08:32:49 +0000424 unsigned OptLevel, unsigned SizeLevel) {
Duncan Sandsa3e585d2011-12-07 17:14:20 +0000425 FPM.add(createVerifierPass()); // Verify that input is correct
426
Chris Lattner52b28892011-05-22 00:21:33 +0000427 PassManagerBuilder Builder;
428 Builder.OptLevel = OptLevel;
Chandler Carruth8bc34342012-05-16 08:32:49 +0000429 Builder.SizeLevel = SizeLevel;
Devang Patel2d7551c2008-09-16 22:25:14 +0000430
Eli Friedman74733a72010-01-18 22:38:31 +0000431 if (DisableInline) {
432 // No inlining pass
Eli Friedman42d41fd2011-06-06 22:13:27 +0000433 } else if (OptLevel > 1) {
Jakob Stoklund Olesene6c6cec2010-11-02 23:40:28 +0000434 unsigned Threshold = 225;
Chandler Carruth8bc34342012-05-16 08:32:49 +0000435 if (SizeLevel == 1) // -Os
436 Threshold = 75;
437 else if (SizeLevel == 2) // -Oz
438 Threshold = 25;
Eli Friedman74733a72010-01-18 22:38:31 +0000439 if (OptLevel > 2)
Jakob Stoklund Olesene6c6cec2010-11-02 23:40:28 +0000440 Threshold = 275;
Chris Lattner52b28892011-05-22 00:21:33 +0000441 Builder.Inliner = createFunctionInliningPass(Threshold);
Eli Friedman74733a72010-01-18 22:38:31 +0000442 } else {
Chris Lattner52b28892011-05-22 00:21:33 +0000443 Builder.Inliner = createAlwaysInlinerPass();
Eli Friedman74733a72010-01-18 22:38:31 +0000444 }
Chris Lattner52b28892011-05-22 00:21:33 +0000445 Builder.DisableUnitAtATime = !UnitAtATime;
446 Builder.DisableUnrollLoops = OptLevel == 0;
447 Builder.DisableSimplifyLibCalls = DisableSimplifyLibCalls;
448
449 Builder.populateFunctionPassManager(FPM);
450 Builder.populateModulePassManager(MPM);
Devang Patel2d7551c2008-09-16 22:25:14 +0000451}
452
Chris Lattner52b28892011-05-22 00:21:33 +0000453static void AddStandardCompilePasses(PassManagerBase &PM) {
Reid Spencer74ed9972007-02-02 14:46:29 +0000454 PM.add(createVerifierPass()); // Verify that input is correct
455
Reid Spencer74ed9972007-02-02 14:46:29 +0000456 // If the -strip-debug command line option was specified, do it.
457 if (StripDebug)
458 addPass(PM, createStripSymbolsPass(true));
459
460 if (DisableOptimizations) return;
461
Daniel Dunbarca8131e2009-06-03 18:22:15 +0000462 // -std-compile-opts adds the same module passes as -O3.
Chris Lattner52b28892011-05-22 00:21:33 +0000463 PassManagerBuilder Builder;
464 if (!DisableInline)
465 Builder.Inliner = createFunctionInliningPass();
466 Builder.OptLevel = 3;
467 Builder.DisableSimplifyLibCalls = DisableSimplifyLibCalls;
468 Builder.populateModulePassManager(PM);
Reid Spencer74ed9972007-02-02 14:46:29 +0000469}
470
Chris Lattner52b28892011-05-22 00:21:33 +0000471static void AddStandardLinkPasses(PassManagerBase &PM) {
Daniel Dunbaradc82882009-07-17 18:09:39 +0000472 PM.add(createVerifierPass()); // Verify that input is correct
473
474 // If the -strip-debug command line option was specified, do it.
475 if (StripDebug)
476 addPass(PM, createStripSymbolsPass(true));
477
478 if (DisableOptimizations) return;
479
Chris Lattner52b28892011-05-22 00:21:33 +0000480 PassManagerBuilder Builder;
481 Builder.populateLTOPassManager(PM, /*Internalize=*/ !DisableInternalize,
482 /*RunInliner=*/ !DisableInline);
Daniel Dunbaradc82882009-07-17 18:09:39 +0000483}
484
Nadav Rotemcbd9a192012-10-18 23:22:48 +0000485//===----------------------------------------------------------------------===//
486// CodeGen-related helper functions.
487//
488static TargetOptions GetTargetOptions() {
489 TargetOptions Options;
490 Options.LessPreciseFPMADOption = EnableFPMAD;
491 Options.NoFramePointerElim = DisableFPElim;
492 Options.NoFramePointerElimNonLeaf = DisableFPElimNonLeaf;
493 Options.AllowFPOpFusion = FuseFPOps;
494 Options.UnsafeFPMath = EnableUnsafeFPMath;
495 Options.NoInfsFPMath = EnableNoInfsFPMath;
496 Options.NoNaNsFPMath = EnableNoNaNsFPMath;
497 Options.HonorSignDependentRoundingFPMathOption =
498 EnableHonorSignDependentRoundingFPMath;
499 Options.UseSoftFloat = GenerateSoftFloatCalls;
500 if (FloatABIForCalls != FloatABI::Default)
501 Options.FloatABIType = FloatABIForCalls;
502 Options.NoZerosInBSS = DontPlaceZerosInBSS;
503 Options.GuaranteedTailCallOpt = EnableGuaranteedTailCallOpt;
504 Options.DisableTailCalls = DisableTailCalls;
505 Options.StackAlignmentOverride = OverrideStackAlignment;
506 Options.RealignStack = EnableRealignStack;
507 Options.TrapFuncName = TrapFuncName;
508 Options.PositionIndependentExecutable = EnablePIE;
509 Options.EnableSegmentedStacks = SegmentedStacks;
510 Options.UseInitArray = UseInitArray;
511 Options.SSPBufferSize = SSPBufferSize;
512 return Options;
513}
514
515CodeGenOpt::Level GetCodeGenOptLevel() {
516 if (OptLevelO1)
517 return CodeGenOpt::Less;
518 if (OptLevelO2)
519 return CodeGenOpt::Default;
520 if (OptLevelO3)
521 return CodeGenOpt::Aggressive;
522 return CodeGenOpt::None;
523}
524
525// Returns the TargetMachine instance or zero if no triple is provided.
526static TargetMachine* GetTargetMachine(std::string TripleStr) {
527 if (TripleStr.empty())
528 return 0;
529
530 // Get the target specific parser.
531 std::string Error;
532 Triple TheTriple(Triple::normalize(TargetTriple));
533
534 const Target *TheTarget = TargetRegistry::lookupTarget(MArch, TheTriple,
535 Error);
536 if (!TheTarget) {
537 return 0;
538 }
539
540 // Package up features to be passed to target/subtarget
541 std::string FeaturesStr;
542 if (MAttrs.size()) {
543 SubtargetFeatures Features;
544 for (unsigned i = 0; i != MAttrs.size(); ++i)
545 Features.AddFeature(MAttrs[i]);
546 FeaturesStr = Features.getString();
547 }
548
549 return TheTarget->createTargetMachine(TheTriple.getTriple(),
550 MCPU, FeaturesStr, GetTargetOptions(),
551 RelocModel, CMModel,
552 GetCodeGenOptLevel());
553}
Chris Lattner0be41012002-02-01 04:54:11 +0000554
Chris Lattnerc0ce68b2002-07-23 18:12:22 +0000555//===----------------------------------------------------------------------===//
556// main for opt
557//
Chris Lattner00950542001-06-06 20:29:01 +0000558int main(int argc, char **argv) {
Chris Lattnerc0d91b72009-12-09 00:41:28 +0000559 sys::PrintStackTraceOnErrorSignal();
560 llvm::PrettyStackTraceProgram X(argc, argv);
Dan Gohmand4c45432010-09-01 14:20:41 +0000561
David Greene08fc0d32010-01-05 01:30:32 +0000562 // Enable debug stream buffering.
563 EnableDebugBuffering = true;
564
Chris Lattnerc0d91b72009-12-09 00:41:28 +0000565 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Owen Anderson0d7c6952009-07-15 22:16:10 +0000566 LLVMContext &Context = getGlobalContext();
Andrew Tricka41af7a2011-04-05 18:41:31 +0000567
Nadav Rotem0873bea2012-10-24 17:23:50 +0000568 InitializeAllTargets();
569 InitializeAllTargetMCs();
Nadav Rotem0873bea2012-10-24 17:23:50 +0000570
Owen Anderson081c34b2010-10-19 17:21:58 +0000571 // Initialize passes
572 PassRegistry &Registry = *PassRegistry::getPassRegistry();
573 initializeCore(Registry);
574 initializeScalarOpts(Registry);
Hal Finkelde5e5ec2012-02-01 03:51:43 +0000575 initializeVectorization(Registry);
Owen Anderson081c34b2010-10-19 17:21:58 +0000576 initializeIPO(Registry);
577 initializeAnalysis(Registry);
578 initializeIPA(Registry);
579 initializeTransformUtils(Registry);
580 initializeInstCombine(Registry);
581 initializeInstrumentation(Registry);
582 initializeTarget(Registry);
Andrew Tricka41af7a2011-04-05 18:41:31 +0000583
Chris Lattner61db1a12009-10-22 00:46:41 +0000584 cl::ParseCommandLineOptions(argc, argv,
585 "llvm .bc -> .bc modular optimizer and analysis printer\n");
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000586
Tobias Grosser7593f342010-12-02 20:35:16 +0000587 if (AnalyzeOnly && NoOutput) {
588 errs() << argv[0] << ": analyze mode conflicts with no-output mode.\n";
589 return 1;
590 }
591
Chris Lattner61db1a12009-10-22 00:46:41 +0000592 SMDiagnostic Err;
Vikram S. Adve18fdfc42002-09-16 16:09:43 +0000593
Chris Lattner61db1a12009-10-22 00:46:41 +0000594 // Load the input module...
595 std::auto_ptr<Module> M;
596 M.reset(ParseIRFile(InputFilename, Err, Context));
Eric Christophera887ae42009-08-21 23:29:40 +0000597
Chris Lattner61db1a12009-10-22 00:46:41 +0000598 if (M.get() == 0) {
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000599 Err.print(argv[0], errs());
Chris Lattner61db1a12009-10-22 00:46:41 +0000600 return 1;
601 }
602
Joe Groffe652b522012-04-17 23:05:48 +0000603 // If we are supposed to override the target triple, do so now.
604 if (!TargetTriple.empty())
605 M->setTargetTriple(Triple::normalize(TargetTriple));
606
Chris Lattner61db1a12009-10-22 00:46:41 +0000607 // Figure out what stream we are supposed to write to...
Dan Gohmand5826a32010-08-20 01:07:01 +0000608 OwningPtr<tool_output_file> Out;
Dan Gohman4931b312010-08-18 17:42:59 +0000609 if (NoOutput) {
Dan Gohman86cbc1b2010-08-18 17:40:10 +0000610 if (!OutputFilename.empty())
611 errs() << "WARNING: The -o (output filename) option is ignored when\n"
Dan Gohman4931b312010-08-18 17:42:59 +0000612 "the --disable-output option is used.\n";
Dan Gohman86cbc1b2010-08-18 17:40:10 +0000613 } else {
614 // Default to standard output.
615 if (OutputFilename.empty())
616 OutputFilename = "-";
Chris Lattner61db1a12009-10-22 00:46:41 +0000617
Dan Gohman86cbc1b2010-08-18 17:40:10 +0000618 std::string ErrorInfo;
Dan Gohmand5826a32010-08-20 01:07:01 +0000619 Out.reset(new tool_output_file(OutputFilename.c_str(), ErrorInfo,
620 raw_fd_ostream::F_Binary));
Dan Gohman86cbc1b2010-08-18 17:40:10 +0000621 if (!ErrorInfo.empty()) {
622 errs() << ErrorInfo << '\n';
Dan Gohman86cbc1b2010-08-18 17:40:10 +0000623 return 1;
Chris Lattner00950542001-06-06 20:29:01 +0000624 }
Chris Lattner61db1a12009-10-22 00:46:41 +0000625 }
Chris Lattner76d12292002-04-18 19:55:25 +0000626
Chris Lattner61db1a12009-10-22 00:46:41 +0000627 // If the output is set to be emitted to standard out, and standard out is a
628 // console, print out a warning message and refuse to do it. We don't
629 // impress anyone by spewing tons of binary goo to a terminal.
Dan Gohmanb56bf582010-01-17 17:47:24 +0000630 if (!Force && !NoOutput && !AnalyzeOnly && !OutputAssembly)
Dan Gohmand4c45432010-09-01 14:20:41 +0000631 if (CheckBitcodeOutputToConsole(Out->os(), !Quiet))
Chris Lattner61db1a12009-10-22 00:46:41 +0000632 NoOutput = true;
Dan Gohmanec080462009-09-11 20:46:33 +0000633
Chris Lattner61db1a12009-10-22 00:46:41 +0000634 // Create a PassManager to hold and optimize the collection of passes we are
Chris Lattner2a66aca2011-02-18 22:13:01 +0000635 // about to build.
Chris Lattner61db1a12009-10-22 00:46:41 +0000636 //
637 PassManager Passes;
Chris Lattner00950542001-06-06 20:29:01 +0000638
Chris Lattner2a66aca2011-02-18 22:13:01 +0000639 // Add an appropriate TargetLibraryInfo pass for the module's triple.
Chris Lattner188a7e02011-02-18 22:34:03 +0000640 TargetLibraryInfo *TLI = new TargetLibraryInfo(Triple(M->getTargetTriple()));
Andrew Tricka41af7a2011-04-05 18:41:31 +0000641
Chris Lattner188a7e02011-02-18 22:34:03 +0000642 // The -disable-simplify-libcalls flag actually disables all builtin optzns.
643 if (DisableSimplifyLibCalls)
644 TLI->disableAllFunctions();
645 Passes.add(TLI);
Andrew Tricka41af7a2011-04-05 18:41:31 +0000646
Micah Villmow791cfc22012-10-08 16:39:34 +0000647 // Add an appropriate DataLayout instance for this module.
648 DataLayout *TD = 0;
Chris Lattner61db1a12009-10-22 00:46:41 +0000649 const std::string &ModuleDataLayout = M.get()->getDataLayout();
650 if (!ModuleDataLayout.empty())
Micah Villmow791cfc22012-10-08 16:39:34 +0000651 TD = new DataLayout(ModuleDataLayout);
Kenneth Uildriksb908f8a2009-11-03 15:29:06 +0000652 else if (!DefaultDataLayout.empty())
Micah Villmow791cfc22012-10-08 16:39:34 +0000653 TD = new DataLayout(DefaultDataLayout);
Chris Lattner9c3b55e2003-04-24 19:13:02 +0000654
Chris Lattner61db1a12009-10-22 00:46:41 +0000655 if (TD)
656 Passes.add(TD);
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000657
Nadav Rotemcbd9a192012-10-18 23:22:48 +0000658 std::auto_ptr<TargetMachine> TM(GetTargetMachine(TargetTriple));
659 if (TM.get()) {
660 Passes.add(new TargetTransformInfo(TM->getScalarTargetTransformInfo(),
661 TM->getVectorTargetTransformInfo()));
662 }
663
Chris Lattner52b28892011-05-22 00:21:33 +0000664 OwningPtr<FunctionPassManager> FPasses;
Chandler Carruth8bc34342012-05-16 08:32:49 +0000665 if (OptLevelO1 || OptLevelO2 || OptLevelOs || OptLevelOz || OptLevelO3) {
Chris Lattner52b28892011-05-22 00:21:33 +0000666 FPasses.reset(new FunctionPassManager(M.get()));
Chris Lattnerd331cb32009-10-22 00:44:10 +0000667 if (TD)
Micah Villmow791cfc22012-10-08 16:39:34 +0000668 FPasses->add(new DataLayout(*TD));
Chris Lattner61db1a12009-10-22 00:46:41 +0000669 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000670
Devang Patelf5f23002010-12-07 00:33:43 +0000671 if (PrintBreakpoints) {
672 // Default to standard output.
673 if (!Out) {
674 if (OutputFilename.empty())
675 OutputFilename = "-";
Andrew Tricka41af7a2011-04-05 18:41:31 +0000676
Devang Patelf5f23002010-12-07 00:33:43 +0000677 std::string ErrorInfo;
678 Out.reset(new tool_output_file(OutputFilename.c_str(), ErrorInfo,
679 raw_fd_ostream::F_Binary));
680 if (!ErrorInfo.empty()) {
681 errs() << ErrorInfo << '\n';
682 return 1;
683 }
684 }
685 Passes.add(new BreakpointPrinter(Out->os()));
686 NoOutput = true;
687 }
688
Chris Lattner61db1a12009-10-22 00:46:41 +0000689 // If the -strip-debug command line option was specified, add it. If
690 // -std-compile-opts was also specified, it will handle StripDebug.
691 if (StripDebug && !StandardCompileOpts)
692 addPass(Passes, createStripSymbolsPass(true));
Eric Christophera887ae42009-08-21 23:29:40 +0000693
Chris Lattner61db1a12009-10-22 00:46:41 +0000694 // Create a new optimization pass for each one specified on the command line
695 for (unsigned i = 0; i < PassList.size(); ++i) {
696 // Check to see if -std-compile-opts was specified before this option. If
697 // so, handle it.
698 if (StandardCompileOpts &&
699 StandardCompileOpts.getPosition() < PassList.getPosition(i)) {
Chris Lattner3dda08a2008-07-13 19:35:21 +0000700 AddStandardCompilePasses(Passes);
701 StandardCompileOpts = false;
Eric Christophera887ae42009-08-21 23:29:40 +0000702 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000703
Chris Lattner61db1a12009-10-22 00:46:41 +0000704 if (StandardLinkOpts &&
705 StandardLinkOpts.getPosition() < PassList.getPosition(i)) {
Daniel Dunbaradc82882009-07-17 18:09:39 +0000706 AddStandardLinkPasses(Passes);
707 StandardLinkOpts = false;
Eric Christophera887ae42009-08-21 23:29:40 +0000708 }
Daniel Dunbaradc82882009-07-17 18:09:39 +0000709
Chris Lattner61db1a12009-10-22 00:46:41 +0000710 if (OptLevelO1 && OptLevelO1.getPosition() < PassList.getPosition(i)) {
Chandler Carruth8bc34342012-05-16 08:32:49 +0000711 AddOptimizationPasses(Passes, *FPasses, 1, 0);
Chris Lattner61db1a12009-10-22 00:46:41 +0000712 OptLevelO1 = false;
Daniel Dunbaradc82882009-07-17 18:09:39 +0000713 }
Devang Patel2d7551c2008-09-16 22:25:14 +0000714
Chris Lattner61db1a12009-10-22 00:46:41 +0000715 if (OptLevelO2 && OptLevelO2.getPosition() < PassList.getPosition(i)) {
Chandler Carruth8bc34342012-05-16 08:32:49 +0000716 AddOptimizationPasses(Passes, *FPasses, 2, 0);
Chris Lattner61db1a12009-10-22 00:46:41 +0000717 OptLevelO2 = false;
Daniel Dunbaradc82882009-07-17 18:09:39 +0000718 }
Devang Patel2d7551c2008-09-16 22:25:14 +0000719
Chandler Carruth8bc34342012-05-16 08:32:49 +0000720 if (OptLevelOs && OptLevelOs.getPosition() < PassList.getPosition(i)) {
721 AddOptimizationPasses(Passes, *FPasses, 2, 1);
722 OptLevelOs = false;
723 }
724
725 if (OptLevelOz && OptLevelOz.getPosition() < PassList.getPosition(i)) {
726 AddOptimizationPasses(Passes, *FPasses, 2, 2);
727 OptLevelOz = false;
728 }
729
Chris Lattner61db1a12009-10-22 00:46:41 +0000730 if (OptLevelO3 && OptLevelO3.getPosition() < PassList.getPosition(i)) {
Chandler Carruth8bc34342012-05-16 08:32:49 +0000731 AddOptimizationPasses(Passes, *FPasses, 3, 0);
Chris Lattner61db1a12009-10-22 00:46:41 +0000732 OptLevelO3 = false;
Daniel Dunbaradc82882009-07-17 18:09:39 +0000733 }
Devang Patel2d7551c2008-09-16 22:25:14 +0000734
Chris Lattner61db1a12009-10-22 00:46:41 +0000735 const PassInfo *PassInf = PassList[i];
736 Pass *P = 0;
737 if (PassInf->getNormalCtor())
738 P = PassInf->getNormalCtor()();
739 else
740 errs() << argv[0] << ": cannot create pass: "
741 << PassInf->getPassName() << "\n";
742 if (P) {
Benjamin Kramer3460f222010-02-18 12:57:05 +0000743 PassKind Kind = P->getPassKind();
Chris Lattner61db1a12009-10-22 00:46:41 +0000744 addPass(Passes, P);
745
746 if (AnalyzeOnly) {
Benjamin Kramer3460f222010-02-18 12:57:05 +0000747 switch (Kind) {
Chris Lattner476e9bd2010-01-22 06:03:06 +0000748 case PT_BasicBlock:
Dan Gohmand4c45432010-09-01 14:20:41 +0000749 Passes.add(new BasicBlockPassPrinter(PassInf, Out->os()));
Chris Lattner476e9bd2010-01-22 06:03:06 +0000750 break;
Tobias Grosser65513602010-10-20 01:54:44 +0000751 case PT_Region:
752 Passes.add(new RegionPassPrinter(PassInf, Out->os()));
753 break;
Chris Lattner476e9bd2010-01-22 06:03:06 +0000754 case PT_Loop:
Dan Gohmand4c45432010-09-01 14:20:41 +0000755 Passes.add(new LoopPassPrinter(PassInf, Out->os()));
Chris Lattner476e9bd2010-01-22 06:03:06 +0000756 break;
757 case PT_Function:
Dan Gohmand4c45432010-09-01 14:20:41 +0000758 Passes.add(new FunctionPassPrinter(PassInf, Out->os()));
Chris Lattner476e9bd2010-01-22 06:03:06 +0000759 break;
760 case PT_CallGraphSCC:
Dan Gohmand4c45432010-09-01 14:20:41 +0000761 Passes.add(new CallGraphSCCPassPrinter(PassInf, Out->os()));
Chris Lattner476e9bd2010-01-22 06:03:06 +0000762 break;
763 default:
Dan Gohmand4c45432010-09-01 14:20:41 +0000764 Passes.add(new ModulePassPrinter(PassInf, Out->os()));
Chris Lattner476e9bd2010-01-22 06:03:06 +0000765 break;
766 }
Chris Lattner61db1a12009-10-22 00:46:41 +0000767 }
Devang Patel2d7551c2008-09-16 22:25:14 +0000768 }
769
Chris Lattner61db1a12009-10-22 00:46:41 +0000770 if (PrintEachXForm)
771 Passes.add(createPrintModulePass(&errs()));
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000772 }
Chris Lattner61db1a12009-10-22 00:46:41 +0000773
774 // If -std-compile-opts was specified at the end of the pass list, add them.
775 if (StandardCompileOpts) {
776 AddStandardCompilePasses(Passes);
777 StandardCompileOpts = false;
778 }
779
780 if (StandardLinkOpts) {
781 AddStandardLinkPasses(Passes);
782 StandardLinkOpts = false;
783 }
784
785 if (OptLevelO1)
Chandler Carruth8bc34342012-05-16 08:32:49 +0000786 AddOptimizationPasses(Passes, *FPasses, 1, 0);
Chris Lattner61db1a12009-10-22 00:46:41 +0000787
788 if (OptLevelO2)
Chandler Carruth8bc34342012-05-16 08:32:49 +0000789 AddOptimizationPasses(Passes, *FPasses, 2, 0);
790
791 if (OptLevelOs)
792 AddOptimizationPasses(Passes, *FPasses, 2, 1);
793
794 if (OptLevelOz)
795 AddOptimizationPasses(Passes, *FPasses, 2, 2);
Chris Lattner61db1a12009-10-22 00:46:41 +0000796
797 if (OptLevelO3)
Chandler Carruth8bc34342012-05-16 08:32:49 +0000798 AddOptimizationPasses(Passes, *FPasses, 3, 0);
Chris Lattner61db1a12009-10-22 00:46:41 +0000799
Chandler Carruth8bc34342012-05-16 08:32:49 +0000800 if (OptLevelO1 || OptLevelO2 || OptLevelOs || OptLevelOz || OptLevelO3) {
Chris Lattner3e8984a2011-05-22 06:44:19 +0000801 FPasses->doInitialization();
Chris Lattner52b28892011-05-22 00:21:33 +0000802 for (Module::iterator F = M->begin(), E = M->end(); F != E; ++F)
803 FPasses->run(*F);
Chris Lattner3e8984a2011-05-22 06:44:19 +0000804 FPasses->doFinalization();
805 }
Chris Lattner61db1a12009-10-22 00:46:41 +0000806
807 // Check that the module is well formed on completion of optimization
808 if (!NoVerify && !VerifyEach)
809 Passes.add(createVerifierPass());
810
Dan Gohman4931b312010-08-18 17:42:59 +0000811 // Write bitcode or assembly to the output as the last step...
Chris Lattner61db1a12009-10-22 00:46:41 +0000812 if (!NoOutput && !AnalyzeOnly) {
813 if (OutputAssembly)
Dan Gohmand4c45432010-09-01 14:20:41 +0000814 Passes.add(createPrintModulePass(&Out->os()));
Chris Lattner61db1a12009-10-22 00:46:41 +0000815 else
Dan Gohmand4c45432010-09-01 14:20:41 +0000816 Passes.add(createBitcodeWriterPass(Out->os()));
Chris Lattner61db1a12009-10-22 00:46:41 +0000817 }
818
Andrew Trickce969022011-04-05 18:54:36 +0000819 // Before executing passes, print the final values of the LLVM options.
820 cl::PrintOptionValues();
821
Chris Lattner61db1a12009-10-22 00:46:41 +0000822 // Now that we have all of the passes ready, run them.
823 Passes.run(*M.get());
824
Dan Gohmand5826a32010-08-20 01:07:01 +0000825 // Declare success.
Devang Patelf5f23002010-12-07 00:33:43 +0000826 if (!NoOutput || PrintBreakpoints)
Dan Gohmand5826a32010-08-20 01:07:01 +0000827 Out->keep();
828
Chris Lattner61db1a12009-10-22 00:46:41 +0000829 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000830}