blob: 8d8d73179e523e69f0269bb9197ddb79d1a194c8 [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 Rotemcbd9a192012-10-18 23:22:48 +000042#include "llvm/MC/SubtargetFeature.h"
Reid Spencer62c51052006-08-21 05:34:03 +000043#include "llvm/LinkAllPasses.h"
Reid Spenceraf303d52006-06-07 23:03:13 +000044#include "llvm/LinkAllVMCore.h"
Rafael Espindola3d453ac2011-08-02 21:50:24 +000045#include "llvm/Transforms/IPO/PassManagerBuilder.h"
Chris Lattner63202322001-11-26 19:22:39 +000046#include <memory>
Chris Lattnerc0ce68b2002-07-23 18:12:22 +000047#include <algorithm>
Brian Gaeked0fde302003-11-11 22:41:34 +000048using namespace llvm;
Chris Lattner9d6e7eb2002-04-12 18:21:13 +000049
Chris Lattnerc0ce68b2002-07-23 18:12:22 +000050// The OptimizationList is automatically populated with registered Passes by the
51// PassNameParser.
52//
Chris Lattner7f500f72006-08-27 22:07:01 +000053static cl::list<const PassInfo*, bool, PassNameParser>
54PassList(cl::desc("Optimizations available:"));
Chris Lattnerc0ce68b2002-07-23 18:12:22 +000055
Chris Lattnerc0ce68b2002-07-23 18:12:22 +000056// Other command line options...
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000057//
Chris Lattner6c8103f2003-05-22 20:13:16 +000058static cl::opt<std::string>
Eric Christophera887ae42009-08-21 23:29:40 +000059InputFilename(cl::Positional, cl::desc("<input bitcode file>"),
Reid Spencerfd90dd52006-08-18 06:34:30 +000060 cl::init("-"), cl::value_desc("filename"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000061
Chris Lattner6c8103f2003-05-22 20:13:16 +000062static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000063OutputFilename("o", cl::desc("Override output filename"),
Dan Gohman86cbc1b2010-08-18 17:40:10 +000064 cl::value_desc("filename"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000065
66static cl::opt<bool>
Dan Gohmanbaa26392009-08-25 15:34:52 +000067Force("f", cl::desc("Enable binary output on terminals"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000068
69static cl::opt<bool>
70PrintEachXForm("p", cl::desc("Print module after each transformation"));
71
72static cl::opt<bool>
Chris Lattnerddd5b412003-02-26 20:00:41 +000073NoOutput("disable-output",
Gabor Greifa99be512007-07-05 17:07:56 +000074 cl::desc("Do not write result bitcode file"), cl::Hidden);
Chris Lattnerd70b68e2003-02-12 18:43:33 +000075
76static cl::opt<bool>
Duncan Sands81b0b642009-10-14 20:01:39 +000077OutputAssembly("S", cl::desc("Write output as LLVM assembly"));
Daniel Dunbar8c042c22009-09-05 11:34:53 +000078
79static cl::opt<bool>
Chris Lattnerddd5b412003-02-26 20:00:41 +000080NoVerify("disable-verify", cl::desc("Do not verify result module"), cl::Hidden);
Chris Lattnerf3bafc12003-02-12 18:45:08 +000081
82static cl::opt<bool>
Reid Spencer74ed9972007-02-02 14:46:29 +000083VerifyEach("verify-each", cl::desc("Verify after each transform"));
84
85static cl::opt<bool>
86StripDebug("strip-debug",
87 cl::desc("Strip debugger symbol info from translation unit"));
88
89static cl::opt<bool>
90DisableInline("disable-inlining", cl::desc("Do not run the inliner pass"));
91
Eric Christophera887ae42009-08-21 23:29:40 +000092static cl::opt<bool>
93DisableOptimizations("disable-opt",
Reid Spencer74ed9972007-02-02 14:46:29 +000094 cl::desc("Do not run any optimization passes"));
95
Eric Christophera887ae42009-08-21 23:29:40 +000096static cl::opt<bool>
Daniel Dunbaradc82882009-07-17 18:09:39 +000097DisableInternalize("disable-internalize",
98 cl::desc("Do not mark all symbols as internal"));
99
Reid Spencer74ed9972007-02-02 14:46:29 +0000100static cl::opt<bool>
Eric Christophera887ae42009-08-21 23:29:40 +0000101StandardCompileOpts("std-compile-opts",
Reid Spencer74ed9972007-02-02 14:46:29 +0000102 cl::desc("Include the standard compile time optimizations"));
103
104static cl::opt<bool>
Eric Christophera887ae42009-08-21 23:29:40 +0000105StandardLinkOpts("std-link-opts",
Daniel Dunbaradc82882009-07-17 18:09:39 +0000106 cl::desc("Include the standard link time optimizations"));
107
108static cl::opt<bool>
Devang Patel2d7551c2008-09-16 22:25:14 +0000109OptLevelO1("O1",
Chandler Carruth8bc34342012-05-16 08:32:49 +0000110 cl::desc("Optimization level 1. Similar to clang -O1"));
Devang Patel2d7551c2008-09-16 22:25:14 +0000111
112static cl::opt<bool>
113OptLevelO2("O2",
Chandler Carruth8bc34342012-05-16 08:32:49 +0000114 cl::desc("Optimization level 2. Similar to clang -O2"));
115
116static cl::opt<bool>
117OptLevelOs("Os",
118 cl::desc("Like -O2 with extra optimizations for size. Similar to clang -Os"));
119
120static cl::opt<bool>
121OptLevelOz("Oz",
122 cl::desc("Like -Os but reduces code size further. Similar to clang -Oz"));
Devang Patel2d7551c2008-09-16 22:25:14 +0000123
124static cl::opt<bool>
125OptLevelO3("O3",
Chandler Carruth8bc34342012-05-16 08:32:49 +0000126 cl::desc("Optimization level 3. Similar to clang -O3"));
Devang Patel2d7551c2008-09-16 22:25:14 +0000127
Joe Groffe652b522012-04-17 23:05:48 +0000128static cl::opt<std::string>
129TargetTriple("mtriple", cl::desc("Override target triple for module"));
130
Devang Patel2d7551c2008-09-16 22:25:14 +0000131static cl::opt<bool>
132UnitAtATime("funit-at-a-time",
Eric Christopherc4769ba2009-08-21 23:30:30 +0000133 cl::desc("Enable IPO. This is same as llvm-gcc's -funit-at-a-time"),
Duncan Sands34727662010-07-12 08:16:59 +0000134 cl::init(true));
Devang Patel2d7551c2008-09-16 22:25:14 +0000135
136static cl::opt<bool>
137DisableSimplifyLibCalls("disable-simplify-libcalls",
Devang Patel442b1aa2008-09-17 16:01:39 +0000138 cl::desc("Disable simplify-libcalls"));
Devang Patel2d7551c2008-09-16 22:25:14 +0000139
140static cl::opt<bool>
Chris Lattner3153e4f2004-05-27 20:32:10 +0000141Quiet("q", cl::desc("Obsolete option"), cl::Hidden);
Chris Lattner5ff62e92002-07-22 02:10:13 +0000142
Reid Spencerec7eb452004-05-27 16:28:54 +0000143static cl::alias
144QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet));
145
Reid Spencerfd90dd52006-08-18 06:34:30 +0000146static cl::opt<bool>
147AnalyzeOnly("analyze", cl::desc("Only perform analysis, no optimization"));
148
Devang Patelf5f23002010-12-07 00:33:43 +0000149static cl::opt<bool>
Andrew Tricka41af7a2011-04-05 18:41:31 +0000150PrintBreakpoints("print-breakpoints-for-testing",
Devang Patelf5f23002010-12-07 00:33:43 +0000151 cl::desc("Print select breakpoints location for testing"));
152
Chris Lattnerd331cb32009-10-22 00:44:10 +0000153static cl::opt<std::string>
Andrew Tricka41af7a2011-04-05 18:41:31 +0000154DefaultDataLayout("default-data-layout",
Chris Lattnerd331cb32009-10-22 00:44:10 +0000155 cl::desc("data layout string to use if not specified by module"),
156 cl::value_desc("layout-string"), cl::init(""));
157
Reid Spencerfd90dd52006-08-18 06:34:30 +0000158// ---------- Define Printers for module and function passes ------------
159namespace {
160
Devang Patel28552da2007-06-28 23:09:25 +0000161struct CallGraphSCCPassPrinter : public CallGraphSCCPass {
162 static char ID;
163 const PassInfo *PassToPrint;
Dan Gohman4931b312010-08-18 17:42:59 +0000164 raw_ostream &Out;
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000165 std::string PassName;
166
Dan Gohman4931b312010-08-18 17:42:59 +0000167 CallGraphSCCPassPrinter(const PassInfo *PI, raw_ostream &out) :
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000168 CallGraphSCCPass(ID), PassToPrint(PI), Out(out) {
169 std::string PassToPrintName = PassToPrint->getPassName();
170 PassName = "CallGraphSCCPass Printer: " + PassToPrintName;
171 }
Devang Patel28552da2007-06-28 23:09:25 +0000172
Chris Lattner2decb222010-04-16 22:42:17 +0000173 virtual bool runOnSCC(CallGraphSCC &SCC) {
Tobias Grossere9dcd032010-09-08 15:02:51 +0000174 if (!Quiet)
Dan Gohman4931b312010-08-18 17:42:59 +0000175 Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
Devang Patel28552da2007-06-28 23:09:25 +0000176
Devang Patel28552da2007-06-28 23:09:25 +0000177 // Get and print pass...
Tobias Grossere9dcd032010-09-08 15:02:51 +0000178 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
179 Function *F = (*I)->getFunction();
180 if (F)
181 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
182 F->getParent());
183 }
Devang Patel28552da2007-06-28 23:09:25 +0000184 return false;
185 }
Eric Christophera887ae42009-08-21 23:29:40 +0000186
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000187 virtual const char *getPassName() const { return PassName.c_str(); }
Devang Patel28552da2007-06-28 23:09:25 +0000188
189 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Owen Anderson90c579d2010-08-06 18:33:48 +0000190 AU.addRequiredID(PassToPrint->getTypeInfo());
Devang Patel28552da2007-06-28 23:09:25 +0000191 AU.setPreservesAll();
192 }
193};
194
195char CallGraphSCCPassPrinter::ID = 0;
196
Reid Spencerfd90dd52006-08-18 06:34:30 +0000197struct ModulePassPrinter : public ModulePass {
Devang Patel19974732007-05-03 01:11:54 +0000198 static char ID;
Reid Spencerfd90dd52006-08-18 06:34:30 +0000199 const PassInfo *PassToPrint;
Dan Gohman4931b312010-08-18 17:42:59 +0000200 raw_ostream &Out;
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000201 std::string PassName;
202
Dan Gohman4931b312010-08-18 17:42:59 +0000203 ModulePassPrinter(const PassInfo *PI, raw_ostream &out)
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000204 : ModulePass(ID), PassToPrint(PI), Out(out) {
205 std::string PassToPrintName = PassToPrint->getPassName();
206 PassName = "ModulePass Printer: " + PassToPrintName;
207 }
Reid Spencerfd90dd52006-08-18 06:34:30 +0000208
209 virtual bool runOnModule(Module &M) {
Tobias Grossere9dcd032010-09-08 15:02:51 +0000210 if (!Quiet)
Dan Gohman4931b312010-08-18 17:42:59 +0000211 Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
Reid Spencerfd90dd52006-08-18 06:34:30 +0000212
213 // Get and print pass...
Tobias Grossere9dcd032010-09-08 15:02:51 +0000214 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out, &M);
Reid Spencerfd90dd52006-08-18 06:34:30 +0000215 return false;
216 }
217
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000218 virtual const char *getPassName() const { return PassName.c_str(); }
Reid Spencerfd90dd52006-08-18 06:34:30 +0000219
220 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Owen Anderson90c579d2010-08-06 18:33:48 +0000221 AU.addRequiredID(PassToPrint->getTypeInfo());
Reid Spencerfd90dd52006-08-18 06:34:30 +0000222 AU.setPreservesAll();
223 }
224};
225
Devang Patel19974732007-05-03 01:11:54 +0000226char ModulePassPrinter::ID = 0;
Reid Spencerfd90dd52006-08-18 06:34:30 +0000227struct FunctionPassPrinter : public FunctionPass {
228 const PassInfo *PassToPrint;
Dan Gohman4931b312010-08-18 17:42:59 +0000229 raw_ostream &Out;
Devang Patel19974732007-05-03 01:11:54 +0000230 static char ID;
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000231 std::string PassName;
232
Dan Gohman4931b312010-08-18 17:42:59 +0000233 FunctionPassPrinter(const PassInfo *PI, raw_ostream &out)
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000234 : FunctionPass(ID), PassToPrint(PI), Out(out) {
235 std::string PassToPrintName = PassToPrint->getPassName();
236 PassName = "FunctionPass Printer: " + PassToPrintName;
237 }
Reid Spencerfd90dd52006-08-18 06:34:30 +0000238
239 virtual bool runOnFunction(Function &F) {
Tobias Grossere9dcd032010-09-08 15:02:51 +0000240 if (!Quiet)
Dan Gohman4931b312010-08-18 17:42:59 +0000241 Out << "Printing analysis '" << PassToPrint->getPassName()
242 << "' for function '" << F.getName() << "':\n";
Tobias Grossere9dcd032010-09-08 15:02:51 +0000243
Reid Spencerfd90dd52006-08-18 06:34:30 +0000244 // Get and print pass...
Dan Gohman4931b312010-08-18 17:42:59 +0000245 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
Owen Anderson90c579d2010-08-06 18:33:48 +0000246 F.getParent());
Reid Spencerfd90dd52006-08-18 06:34:30 +0000247 return false;
248 }
249
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000250 virtual const char *getPassName() const { return PassName.c_str(); }
Reid Spencerfd90dd52006-08-18 06:34:30 +0000251
252 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Owen Anderson90c579d2010-08-06 18:33:48 +0000253 AU.addRequiredID(PassToPrint->getTypeInfo());
Reid Spencerfd90dd52006-08-18 06:34:30 +0000254 AU.setPreservesAll();
255 }
256};
257
Devang Patel19974732007-05-03 01:11:54 +0000258char FunctionPassPrinter::ID = 0;
Devang Patel56fb1642007-07-05 15:32:03 +0000259
260struct LoopPassPrinter : public LoopPass {
261 static char ID;
262 const PassInfo *PassToPrint;
Dan Gohman4931b312010-08-18 17:42:59 +0000263 raw_ostream &Out;
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000264 std::string PassName;
265
Dan Gohman4931b312010-08-18 17:42:59 +0000266 LoopPassPrinter(const PassInfo *PI, raw_ostream &out) :
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000267 LoopPass(ID), PassToPrint(PI), Out(out) {
268 std::string PassToPrintName = PassToPrint->getPassName();
269 PassName = "LoopPass Printer: " + PassToPrintName;
270 }
271
Devang Patel56fb1642007-07-05 15:32:03 +0000272
273 virtual bool runOnLoop(Loop *L, LPPassManager &LPM) {
Tobias Grossere9dcd032010-09-08 15:02:51 +0000274 if (!Quiet)
Dan Gohman4931b312010-08-18 17:42:59 +0000275 Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
Tobias Grossere9dcd032010-09-08 15:02:51 +0000276
Devang Patel56fb1642007-07-05 15:32:03 +0000277 // Get and print pass...
Tobias Grossere9dcd032010-09-08 15:02:51 +0000278 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
279 L->getHeader()->getParent()->getParent());
Devang Patel56fb1642007-07-05 15:32:03 +0000280 return false;
281 }
Eric Christophera887ae42009-08-21 23:29:40 +0000282
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000283 virtual const char *getPassName() const { return PassName.c_str(); }
Devang Patel56fb1642007-07-05 15:32:03 +0000284
285 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Owen Anderson90c579d2010-08-06 18:33:48 +0000286 AU.addRequiredID(PassToPrint->getTypeInfo());
Devang Patel56fb1642007-07-05 15:32:03 +0000287 AU.setPreservesAll();
288 }
289};
290
291char LoopPassPrinter::ID = 0;
292
Tobias Grosser65513602010-10-20 01:54:44 +0000293struct RegionPassPrinter : public RegionPass {
294 static char ID;
295 const PassInfo *PassToPrint;
296 raw_ostream &Out;
297 std::string PassName;
298
299 RegionPassPrinter(const PassInfo *PI, raw_ostream &out) : RegionPass(ID),
300 PassToPrint(PI), Out(out) {
301 std::string PassToPrintName = PassToPrint->getPassName();
Tobias Grosser58396142011-01-20 21:03:20 +0000302 PassName = "RegionPass Printer: " + PassToPrintName;
Tobias Grosser65513602010-10-20 01:54:44 +0000303 }
304
305 virtual bool runOnRegion(Region *R, RGPassManager &RGM) {
306 if (!Quiet) {
307 Out << "Printing analysis '" << PassToPrint->getPassName() << "' for "
Benjamin Kramera7b0cb72011-11-15 16:27:03 +0000308 << "region: '" << R->getNameStr() << "' in function '"
309 << R->getEntry()->getParent()->getName() << "':\n";
Tobias Grosser65513602010-10-20 01:54:44 +0000310 }
311 // Get and print pass...
312 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
313 R->getEntry()->getParent()->getParent());
314 return false;
315 }
316
Tobias Grosser58396142011-01-20 21:03:20 +0000317 virtual const char *getPassName() const { return PassName.c_str(); }
Tobias Grosser65513602010-10-20 01:54:44 +0000318
319 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
320 AU.addRequiredID(PassToPrint->getTypeInfo());
321 AU.setPreservesAll();
322 }
323};
324
325char RegionPassPrinter::ID = 0;
326
Reid Spencerfd90dd52006-08-18 06:34:30 +0000327struct BasicBlockPassPrinter : public BasicBlockPass {
328 const PassInfo *PassToPrint;
Dan Gohman4931b312010-08-18 17:42:59 +0000329 raw_ostream &Out;
Devang Patel19974732007-05-03 01:11:54 +0000330 static char ID;
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000331 std::string PassName;
332
Dan Gohman4931b312010-08-18 17:42:59 +0000333 BasicBlockPassPrinter(const PassInfo *PI, raw_ostream &out)
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000334 : BasicBlockPass(ID), PassToPrint(PI), Out(out) {
335 std::string PassToPrintName = PassToPrint->getPassName();
336 PassName = "BasicBlockPass Printer: " + PassToPrintName;
337 }
Reid Spencerfd90dd52006-08-18 06:34:30 +0000338
339 virtual bool runOnBasicBlock(BasicBlock &BB) {
Tobias Grossere9dcd032010-09-08 15:02:51 +0000340 if (!Quiet)
Dan Gohman4931b312010-08-18 17:42:59 +0000341 Out << "Printing Analysis info for BasicBlock '" << BB.getName()
342 << "': Pass " << PassToPrint->getPassName() << ":\n";
Reid Spencerfd90dd52006-08-18 06:34:30 +0000343
344 // Get and print pass...
Andrew Tricka41af7a2011-04-05 18:41:31 +0000345 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
Owen Anderson90c579d2010-08-06 18:33:48 +0000346 BB.getParent()->getParent());
Reid Spencerfd90dd52006-08-18 06:34:30 +0000347 return false;
348 }
349
Tobias Grosser4207d6f2010-09-08 15:02:47 +0000350 virtual const char *getPassName() const { return PassName.c_str(); }
Reid Spencerfd90dd52006-08-18 06:34:30 +0000351
352 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Owen Anderson90c579d2010-08-06 18:33:48 +0000353 AU.addRequiredID(PassToPrint->getTypeInfo());
Reid Spencerfd90dd52006-08-18 06:34:30 +0000354 AU.setPreservesAll();
355 }
356};
357
Devang Patel19974732007-05-03 01:11:54 +0000358char BasicBlockPassPrinter::ID = 0;
Devang Patelf5f23002010-12-07 00:33:43 +0000359
Devang Patel1be99802011-04-04 19:51:17 +0000360struct BreakpointPrinter : public ModulePass {
Devang Patelf5f23002010-12-07 00:33:43 +0000361 raw_ostream &Out;
362 static char ID;
363
364 BreakpointPrinter(raw_ostream &out)
Devang Patel1be99802011-04-04 19:51:17 +0000365 : ModulePass(ID), Out(out) {
Devang Patelf5f23002010-12-07 00:33:43 +0000366 }
367
Devang Patel1be99802011-04-04 19:51:17 +0000368 void getContextName(DIDescriptor Context, std::string &N) {
369 if (Context.isNameSpace()) {
370 DINameSpace NS(Context);
371 if (!NS.getName().empty()) {
372 getContextName(NS.getContext(), N);
373 N = N + NS.getName().str() + "::";
Devang Patel97f6d5b2011-01-31 21:36:24 +0000374 }
Devang Patel1be99802011-04-04 19:51:17 +0000375 } else if (Context.isType()) {
376 DIType TY(Context);
377 if (!TY.getName().empty()) {
378 getContextName(TY.getContext(), N);
379 N = N + TY.getName().str() + "::";
380 }
381 }
382 }
383
384 virtual bool runOnModule(Module &M) {
385 StringSet<> Processed;
386 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
387 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
388 std::string Name;
389 DISubprogram SP(NMD->getOperand(i));
390 if (SP.Verify())
391 getContextName(SP.getContext(), Name);
392 Name = Name + SP.getDisplayName().str();
393 if (!Name.empty() && Processed.insert(Name)) {
394 Out << Name << "\n";
395 }
396 }
Devang Patelf5f23002010-12-07 00:33:43 +0000397 return false;
398 }
399
400 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
401 AU.setPreservesAll();
402 }
403};
Chris Lattner52b28892011-05-22 00:21:33 +0000404
405} // anonymous namespace
Devang Patelf5f23002010-12-07 00:33:43 +0000406
407char BreakpointPrinter::ID = 0;
408
Chris Lattner52b28892011-05-22 00:21:33 +0000409static inline void addPass(PassManagerBase &PM, Pass *P) {
Reid Spencer74ed9972007-02-02 14:46:29 +0000410 // Add the pass to the pass manager...
411 PM.add(P);
412
413 // If we are verifying all of the intermediate steps, add the verifier...
414 if (VerifyEach) PM.add(createVerifierPass());
415}
416
Eric Christophera887ae42009-08-21 23:29:40 +0000417/// AddOptimizationPasses - This routine adds optimization passes
418/// based on selected optimization level, OptLevel. This routine
Devang Patel2d7551c2008-09-16 22:25:14 +0000419/// duplicates llvm-gcc behaviour.
420///
421/// OptLevel - Optimization Level
Chris Lattner52b28892011-05-22 00:21:33 +0000422static void AddOptimizationPasses(PassManagerBase &MPM,FunctionPassManager &FPM,
Chandler Carruth8bc34342012-05-16 08:32:49 +0000423 unsigned OptLevel, unsigned SizeLevel) {
Duncan Sandsa3e585d2011-12-07 17:14:20 +0000424 FPM.add(createVerifierPass()); // Verify that input is correct
425
Chris Lattner52b28892011-05-22 00:21:33 +0000426 PassManagerBuilder Builder;
427 Builder.OptLevel = OptLevel;
Chandler Carruth8bc34342012-05-16 08:32:49 +0000428 Builder.SizeLevel = SizeLevel;
Devang Patel2d7551c2008-09-16 22:25:14 +0000429
Eli Friedman74733a72010-01-18 22:38:31 +0000430 if (DisableInline) {
431 // No inlining pass
Eli Friedman42d41fd2011-06-06 22:13:27 +0000432 } else if (OptLevel > 1) {
Jakob Stoklund Olesene6c6cec2010-11-02 23:40:28 +0000433 unsigned Threshold = 225;
Chandler Carruth8bc34342012-05-16 08:32:49 +0000434 if (SizeLevel == 1) // -Os
435 Threshold = 75;
436 else if (SizeLevel == 2) // -Oz
437 Threshold = 25;
Eli Friedman74733a72010-01-18 22:38:31 +0000438 if (OptLevel > 2)
Jakob Stoklund Olesene6c6cec2010-11-02 23:40:28 +0000439 Threshold = 275;
Chris Lattner52b28892011-05-22 00:21:33 +0000440 Builder.Inliner = createFunctionInliningPass(Threshold);
Eli Friedman74733a72010-01-18 22:38:31 +0000441 } else {
Chris Lattner52b28892011-05-22 00:21:33 +0000442 Builder.Inliner = createAlwaysInlinerPass();
Eli Friedman74733a72010-01-18 22:38:31 +0000443 }
Chris Lattner52b28892011-05-22 00:21:33 +0000444 Builder.DisableUnitAtATime = !UnitAtATime;
445 Builder.DisableUnrollLoops = OptLevel == 0;
446 Builder.DisableSimplifyLibCalls = DisableSimplifyLibCalls;
447
448 Builder.populateFunctionPassManager(FPM);
449 Builder.populateModulePassManager(MPM);
Devang Patel2d7551c2008-09-16 22:25:14 +0000450}
451
Chris Lattner52b28892011-05-22 00:21:33 +0000452static void AddStandardCompilePasses(PassManagerBase &PM) {
Reid Spencer74ed9972007-02-02 14:46:29 +0000453 PM.add(createVerifierPass()); // Verify that input is correct
454
Reid Spencer74ed9972007-02-02 14:46:29 +0000455 // If the -strip-debug command line option was specified, do it.
456 if (StripDebug)
457 addPass(PM, createStripSymbolsPass(true));
458
459 if (DisableOptimizations) return;
460
Daniel Dunbarca8131e2009-06-03 18:22:15 +0000461 // -std-compile-opts adds the same module passes as -O3.
Chris Lattner52b28892011-05-22 00:21:33 +0000462 PassManagerBuilder Builder;
463 if (!DisableInline)
464 Builder.Inliner = createFunctionInliningPass();
465 Builder.OptLevel = 3;
466 Builder.DisableSimplifyLibCalls = DisableSimplifyLibCalls;
467 Builder.populateModulePassManager(PM);
Reid Spencer74ed9972007-02-02 14:46:29 +0000468}
469
Chris Lattner52b28892011-05-22 00:21:33 +0000470static void AddStandardLinkPasses(PassManagerBase &PM) {
Daniel Dunbaradc82882009-07-17 18:09:39 +0000471 PM.add(createVerifierPass()); // Verify that input is correct
472
473 // If the -strip-debug command line option was specified, do it.
474 if (StripDebug)
475 addPass(PM, createStripSymbolsPass(true));
476
477 if (DisableOptimizations) return;
478
Chris Lattner52b28892011-05-22 00:21:33 +0000479 PassManagerBuilder Builder;
480 Builder.populateLTOPassManager(PM, /*Internalize=*/ !DisableInternalize,
481 /*RunInliner=*/ !DisableInline);
Daniel Dunbaradc82882009-07-17 18:09:39 +0000482}
483
Nadav Rotemcbd9a192012-10-18 23:22:48 +0000484//===----------------------------------------------------------------------===//
485// CodeGen-related helper functions.
486//
487static TargetOptions GetTargetOptions() {
488 TargetOptions Options;
489 Options.LessPreciseFPMADOption = EnableFPMAD;
490 Options.NoFramePointerElim = DisableFPElim;
491 Options.NoFramePointerElimNonLeaf = DisableFPElimNonLeaf;
492 Options.AllowFPOpFusion = FuseFPOps;
493 Options.UnsafeFPMath = EnableUnsafeFPMath;
494 Options.NoInfsFPMath = EnableNoInfsFPMath;
495 Options.NoNaNsFPMath = EnableNoNaNsFPMath;
496 Options.HonorSignDependentRoundingFPMathOption =
497 EnableHonorSignDependentRoundingFPMath;
498 Options.UseSoftFloat = GenerateSoftFloatCalls;
499 if (FloatABIForCalls != FloatABI::Default)
500 Options.FloatABIType = FloatABIForCalls;
501 Options.NoZerosInBSS = DontPlaceZerosInBSS;
502 Options.GuaranteedTailCallOpt = EnableGuaranteedTailCallOpt;
503 Options.DisableTailCalls = DisableTailCalls;
504 Options.StackAlignmentOverride = OverrideStackAlignment;
505 Options.RealignStack = EnableRealignStack;
506 Options.TrapFuncName = TrapFuncName;
507 Options.PositionIndependentExecutable = EnablePIE;
508 Options.EnableSegmentedStacks = SegmentedStacks;
509 Options.UseInitArray = UseInitArray;
510 Options.SSPBufferSize = SSPBufferSize;
511 return Options;
512}
513
514CodeGenOpt::Level GetCodeGenOptLevel() {
515 if (OptLevelO1)
516 return CodeGenOpt::Less;
517 if (OptLevelO2)
518 return CodeGenOpt::Default;
519 if (OptLevelO3)
520 return CodeGenOpt::Aggressive;
521 return CodeGenOpt::None;
522}
523
524// Returns the TargetMachine instance or zero if no triple is provided.
525static TargetMachine* GetTargetMachine(std::string TripleStr) {
526 if (TripleStr.empty())
527 return 0;
528
529 // Get the target specific parser.
530 std::string Error;
531 Triple TheTriple(Triple::normalize(TargetTriple));
532
533 const Target *TheTarget = TargetRegistry::lookupTarget(MArch, TheTriple,
534 Error);
535 if (!TheTarget) {
536 return 0;
537 }
538
539 // Package up features to be passed to target/subtarget
540 std::string FeaturesStr;
541 if (MAttrs.size()) {
542 SubtargetFeatures Features;
543 for (unsigned i = 0; i != MAttrs.size(); ++i)
544 Features.AddFeature(MAttrs[i]);
545 FeaturesStr = Features.getString();
546 }
547
548 return TheTarget->createTargetMachine(TheTriple.getTriple(),
549 MCPU, FeaturesStr, GetTargetOptions(),
550 RelocModel, CMModel,
551 GetCodeGenOptLevel());
552}
Chris Lattner0be41012002-02-01 04:54:11 +0000553
Chris Lattnerc0ce68b2002-07-23 18:12:22 +0000554//===----------------------------------------------------------------------===//
555// main for opt
556//
Chris Lattner00950542001-06-06 20:29:01 +0000557int main(int argc, char **argv) {
Chris Lattnerc0d91b72009-12-09 00:41:28 +0000558 sys::PrintStackTraceOnErrorSignal();
559 llvm::PrettyStackTraceProgram X(argc, argv);
Dan Gohmand4c45432010-09-01 14:20:41 +0000560
David Greene08fc0d32010-01-05 01:30:32 +0000561 // Enable debug stream buffering.
562 EnableDebugBuffering = true;
563
Chris Lattnerc0d91b72009-12-09 00:41:28 +0000564 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Owen Anderson0d7c6952009-07-15 22:16:10 +0000565 LLVMContext &Context = getGlobalContext();
Andrew Tricka41af7a2011-04-05 18:41:31 +0000566
Owen Anderson081c34b2010-10-19 17:21:58 +0000567 // Initialize passes
568 PassRegistry &Registry = *PassRegistry::getPassRegistry();
569 initializeCore(Registry);
570 initializeScalarOpts(Registry);
Hal Finkelde5e5ec2012-02-01 03:51:43 +0000571 initializeVectorization(Registry);
Owen Anderson081c34b2010-10-19 17:21:58 +0000572 initializeIPO(Registry);
573 initializeAnalysis(Registry);
574 initializeIPA(Registry);
575 initializeTransformUtils(Registry);
576 initializeInstCombine(Registry);
577 initializeInstrumentation(Registry);
578 initializeTarget(Registry);
Andrew Tricka41af7a2011-04-05 18:41:31 +0000579
Chris Lattner61db1a12009-10-22 00:46:41 +0000580 cl::ParseCommandLineOptions(argc, argv,
581 "llvm .bc -> .bc modular optimizer and analysis printer\n");
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000582
Tobias Grosser7593f342010-12-02 20:35:16 +0000583 if (AnalyzeOnly && NoOutput) {
584 errs() << argv[0] << ": analyze mode conflicts with no-output mode.\n";
585 return 1;
586 }
587
Chris Lattner61db1a12009-10-22 00:46:41 +0000588 SMDiagnostic Err;
Vikram S. Adve18fdfc42002-09-16 16:09:43 +0000589
Chris Lattner61db1a12009-10-22 00:46:41 +0000590 // Load the input module...
591 std::auto_ptr<Module> M;
592 M.reset(ParseIRFile(InputFilename, Err, Context));
Eric Christophera887ae42009-08-21 23:29:40 +0000593
Chris Lattner61db1a12009-10-22 00:46:41 +0000594 if (M.get() == 0) {
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000595 Err.print(argv[0], errs());
Chris Lattner61db1a12009-10-22 00:46:41 +0000596 return 1;
597 }
598
Joe Groffe652b522012-04-17 23:05:48 +0000599 // If we are supposed to override the target triple, do so now.
600 if (!TargetTriple.empty())
601 M->setTargetTriple(Triple::normalize(TargetTriple));
602
Chris Lattner61db1a12009-10-22 00:46:41 +0000603 // Figure out what stream we are supposed to write to...
Dan Gohmand5826a32010-08-20 01:07:01 +0000604 OwningPtr<tool_output_file> Out;
Dan Gohman4931b312010-08-18 17:42:59 +0000605 if (NoOutput) {
Dan Gohman86cbc1b2010-08-18 17:40:10 +0000606 if (!OutputFilename.empty())
607 errs() << "WARNING: The -o (output filename) option is ignored when\n"
Dan Gohman4931b312010-08-18 17:42:59 +0000608 "the --disable-output option is used.\n";
Dan Gohman86cbc1b2010-08-18 17:40:10 +0000609 } else {
610 // Default to standard output.
611 if (OutputFilename.empty())
612 OutputFilename = "-";
Chris Lattner61db1a12009-10-22 00:46:41 +0000613
Dan Gohman86cbc1b2010-08-18 17:40:10 +0000614 std::string ErrorInfo;
Dan Gohmand5826a32010-08-20 01:07:01 +0000615 Out.reset(new tool_output_file(OutputFilename.c_str(), ErrorInfo,
616 raw_fd_ostream::F_Binary));
Dan Gohman86cbc1b2010-08-18 17:40:10 +0000617 if (!ErrorInfo.empty()) {
618 errs() << ErrorInfo << '\n';
Dan Gohman86cbc1b2010-08-18 17:40:10 +0000619 return 1;
Chris Lattner00950542001-06-06 20:29:01 +0000620 }
Chris Lattner61db1a12009-10-22 00:46:41 +0000621 }
Chris Lattner76d12292002-04-18 19:55:25 +0000622
Chris Lattner61db1a12009-10-22 00:46:41 +0000623 // If the output is set to be emitted to standard out, and standard out is a
624 // console, print out a warning message and refuse to do it. We don't
625 // impress anyone by spewing tons of binary goo to a terminal.
Dan Gohmanb56bf582010-01-17 17:47:24 +0000626 if (!Force && !NoOutput && !AnalyzeOnly && !OutputAssembly)
Dan Gohmand4c45432010-09-01 14:20:41 +0000627 if (CheckBitcodeOutputToConsole(Out->os(), !Quiet))
Chris Lattner61db1a12009-10-22 00:46:41 +0000628 NoOutput = true;
Dan Gohmanec080462009-09-11 20:46:33 +0000629
Chris Lattner61db1a12009-10-22 00:46:41 +0000630 // Create a PassManager to hold and optimize the collection of passes we are
Chris Lattner2a66aca2011-02-18 22:13:01 +0000631 // about to build.
Chris Lattner61db1a12009-10-22 00:46:41 +0000632 //
633 PassManager Passes;
Chris Lattner00950542001-06-06 20:29:01 +0000634
Chris Lattner2a66aca2011-02-18 22:13:01 +0000635 // Add an appropriate TargetLibraryInfo pass for the module's triple.
Chris Lattner188a7e02011-02-18 22:34:03 +0000636 TargetLibraryInfo *TLI = new TargetLibraryInfo(Triple(M->getTargetTriple()));
Andrew Tricka41af7a2011-04-05 18:41:31 +0000637
Chris Lattner188a7e02011-02-18 22:34:03 +0000638 // The -disable-simplify-libcalls flag actually disables all builtin optzns.
639 if (DisableSimplifyLibCalls)
640 TLI->disableAllFunctions();
641 Passes.add(TLI);
Andrew Tricka41af7a2011-04-05 18:41:31 +0000642
Micah Villmow791cfc22012-10-08 16:39:34 +0000643 // Add an appropriate DataLayout instance for this module.
644 DataLayout *TD = 0;
Chris Lattner61db1a12009-10-22 00:46:41 +0000645 const std::string &ModuleDataLayout = M.get()->getDataLayout();
646 if (!ModuleDataLayout.empty())
Micah Villmow791cfc22012-10-08 16:39:34 +0000647 TD = new DataLayout(ModuleDataLayout);
Kenneth Uildriksb908f8a2009-11-03 15:29:06 +0000648 else if (!DefaultDataLayout.empty())
Micah Villmow791cfc22012-10-08 16:39:34 +0000649 TD = new DataLayout(DefaultDataLayout);
Chris Lattner9c3b55e2003-04-24 19:13:02 +0000650
Chris Lattner61db1a12009-10-22 00:46:41 +0000651 if (TD)
652 Passes.add(TD);
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000653
Nadav Rotemcbd9a192012-10-18 23:22:48 +0000654 std::auto_ptr<TargetMachine> TM(GetTargetMachine(TargetTriple));
655 if (TM.get()) {
656 Passes.add(new TargetTransformInfo(TM->getScalarTargetTransformInfo(),
657 TM->getVectorTargetTransformInfo()));
658 }
659
Chris Lattner52b28892011-05-22 00:21:33 +0000660 OwningPtr<FunctionPassManager> FPasses;
Chandler Carruth8bc34342012-05-16 08:32:49 +0000661 if (OptLevelO1 || OptLevelO2 || OptLevelOs || OptLevelOz || OptLevelO3) {
Chris Lattner52b28892011-05-22 00:21:33 +0000662 FPasses.reset(new FunctionPassManager(M.get()));
Chris Lattnerd331cb32009-10-22 00:44:10 +0000663 if (TD)
Micah Villmow791cfc22012-10-08 16:39:34 +0000664 FPasses->add(new DataLayout(*TD));
Chris Lattner61db1a12009-10-22 00:46:41 +0000665 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000666
Devang Patelf5f23002010-12-07 00:33:43 +0000667 if (PrintBreakpoints) {
668 // Default to standard output.
669 if (!Out) {
670 if (OutputFilename.empty())
671 OutputFilename = "-";
Andrew Tricka41af7a2011-04-05 18:41:31 +0000672
Devang Patelf5f23002010-12-07 00:33:43 +0000673 std::string ErrorInfo;
674 Out.reset(new tool_output_file(OutputFilename.c_str(), ErrorInfo,
675 raw_fd_ostream::F_Binary));
676 if (!ErrorInfo.empty()) {
677 errs() << ErrorInfo << '\n';
678 return 1;
679 }
680 }
681 Passes.add(new BreakpointPrinter(Out->os()));
682 NoOutput = true;
683 }
684
Chris Lattner61db1a12009-10-22 00:46:41 +0000685 // If the -strip-debug command line option was specified, add it. If
686 // -std-compile-opts was also specified, it will handle StripDebug.
687 if (StripDebug && !StandardCompileOpts)
688 addPass(Passes, createStripSymbolsPass(true));
Eric Christophera887ae42009-08-21 23:29:40 +0000689
Chris Lattner61db1a12009-10-22 00:46:41 +0000690 // Create a new optimization pass for each one specified on the command line
691 for (unsigned i = 0; i < PassList.size(); ++i) {
692 // Check to see if -std-compile-opts was specified before this option. If
693 // so, handle it.
694 if (StandardCompileOpts &&
695 StandardCompileOpts.getPosition() < PassList.getPosition(i)) {
Chris Lattner3dda08a2008-07-13 19:35:21 +0000696 AddStandardCompilePasses(Passes);
697 StandardCompileOpts = false;
Eric Christophera887ae42009-08-21 23:29:40 +0000698 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000699
Chris Lattner61db1a12009-10-22 00:46:41 +0000700 if (StandardLinkOpts &&
701 StandardLinkOpts.getPosition() < PassList.getPosition(i)) {
Daniel Dunbaradc82882009-07-17 18:09:39 +0000702 AddStandardLinkPasses(Passes);
703 StandardLinkOpts = false;
Eric Christophera887ae42009-08-21 23:29:40 +0000704 }
Daniel Dunbaradc82882009-07-17 18:09:39 +0000705
Chris Lattner61db1a12009-10-22 00:46:41 +0000706 if (OptLevelO1 && OptLevelO1.getPosition() < PassList.getPosition(i)) {
Chandler Carruth8bc34342012-05-16 08:32:49 +0000707 AddOptimizationPasses(Passes, *FPasses, 1, 0);
Chris Lattner61db1a12009-10-22 00:46:41 +0000708 OptLevelO1 = false;
Daniel Dunbaradc82882009-07-17 18:09:39 +0000709 }
Devang Patel2d7551c2008-09-16 22:25:14 +0000710
Chris Lattner61db1a12009-10-22 00:46:41 +0000711 if (OptLevelO2 && OptLevelO2.getPosition() < PassList.getPosition(i)) {
Chandler Carruth8bc34342012-05-16 08:32:49 +0000712 AddOptimizationPasses(Passes, *FPasses, 2, 0);
Chris Lattner61db1a12009-10-22 00:46:41 +0000713 OptLevelO2 = false;
Daniel Dunbaradc82882009-07-17 18:09:39 +0000714 }
Devang Patel2d7551c2008-09-16 22:25:14 +0000715
Chandler Carruth8bc34342012-05-16 08:32:49 +0000716 if (OptLevelOs && OptLevelOs.getPosition() < PassList.getPosition(i)) {
717 AddOptimizationPasses(Passes, *FPasses, 2, 1);
718 OptLevelOs = false;
719 }
720
721 if (OptLevelOz && OptLevelOz.getPosition() < PassList.getPosition(i)) {
722 AddOptimizationPasses(Passes, *FPasses, 2, 2);
723 OptLevelOz = false;
724 }
725
Chris Lattner61db1a12009-10-22 00:46:41 +0000726 if (OptLevelO3 && OptLevelO3.getPosition() < PassList.getPosition(i)) {
Chandler Carruth8bc34342012-05-16 08:32:49 +0000727 AddOptimizationPasses(Passes, *FPasses, 3, 0);
Chris Lattner61db1a12009-10-22 00:46:41 +0000728 OptLevelO3 = false;
Daniel Dunbaradc82882009-07-17 18:09:39 +0000729 }
Devang Patel2d7551c2008-09-16 22:25:14 +0000730
Chris Lattner61db1a12009-10-22 00:46:41 +0000731 const PassInfo *PassInf = PassList[i];
732 Pass *P = 0;
733 if (PassInf->getNormalCtor())
734 P = PassInf->getNormalCtor()();
735 else
736 errs() << argv[0] << ": cannot create pass: "
737 << PassInf->getPassName() << "\n";
738 if (P) {
Benjamin Kramer3460f222010-02-18 12:57:05 +0000739 PassKind Kind = P->getPassKind();
Chris Lattner61db1a12009-10-22 00:46:41 +0000740 addPass(Passes, P);
741
742 if (AnalyzeOnly) {
Benjamin Kramer3460f222010-02-18 12:57:05 +0000743 switch (Kind) {
Chris Lattner476e9bd2010-01-22 06:03:06 +0000744 case PT_BasicBlock:
Dan Gohmand4c45432010-09-01 14:20:41 +0000745 Passes.add(new BasicBlockPassPrinter(PassInf, Out->os()));
Chris Lattner476e9bd2010-01-22 06:03:06 +0000746 break;
Tobias Grosser65513602010-10-20 01:54:44 +0000747 case PT_Region:
748 Passes.add(new RegionPassPrinter(PassInf, Out->os()));
749 break;
Chris Lattner476e9bd2010-01-22 06:03:06 +0000750 case PT_Loop:
Dan Gohmand4c45432010-09-01 14:20:41 +0000751 Passes.add(new LoopPassPrinter(PassInf, Out->os()));
Chris Lattner476e9bd2010-01-22 06:03:06 +0000752 break;
753 case PT_Function:
Dan Gohmand4c45432010-09-01 14:20:41 +0000754 Passes.add(new FunctionPassPrinter(PassInf, Out->os()));
Chris Lattner476e9bd2010-01-22 06:03:06 +0000755 break;
756 case PT_CallGraphSCC:
Dan Gohmand4c45432010-09-01 14:20:41 +0000757 Passes.add(new CallGraphSCCPassPrinter(PassInf, Out->os()));
Chris Lattner476e9bd2010-01-22 06:03:06 +0000758 break;
759 default:
Dan Gohmand4c45432010-09-01 14:20:41 +0000760 Passes.add(new ModulePassPrinter(PassInf, Out->os()));
Chris Lattner476e9bd2010-01-22 06:03:06 +0000761 break;
762 }
Chris Lattner61db1a12009-10-22 00:46:41 +0000763 }
Devang Patel2d7551c2008-09-16 22:25:14 +0000764 }
765
Chris Lattner61db1a12009-10-22 00:46:41 +0000766 if (PrintEachXForm)
767 Passes.add(createPrintModulePass(&errs()));
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000768 }
Chris Lattner61db1a12009-10-22 00:46:41 +0000769
770 // If -std-compile-opts was specified at the end of the pass list, add them.
771 if (StandardCompileOpts) {
772 AddStandardCompilePasses(Passes);
773 StandardCompileOpts = false;
774 }
775
776 if (StandardLinkOpts) {
777 AddStandardLinkPasses(Passes);
778 StandardLinkOpts = false;
779 }
780
781 if (OptLevelO1)
Chandler Carruth8bc34342012-05-16 08:32:49 +0000782 AddOptimizationPasses(Passes, *FPasses, 1, 0);
Chris Lattner61db1a12009-10-22 00:46:41 +0000783
784 if (OptLevelO2)
Chandler Carruth8bc34342012-05-16 08:32:49 +0000785 AddOptimizationPasses(Passes, *FPasses, 2, 0);
786
787 if (OptLevelOs)
788 AddOptimizationPasses(Passes, *FPasses, 2, 1);
789
790 if (OptLevelOz)
791 AddOptimizationPasses(Passes, *FPasses, 2, 2);
Chris Lattner61db1a12009-10-22 00:46:41 +0000792
793 if (OptLevelO3)
Chandler Carruth8bc34342012-05-16 08:32:49 +0000794 AddOptimizationPasses(Passes, *FPasses, 3, 0);
Chris Lattner61db1a12009-10-22 00:46:41 +0000795
Chandler Carruth8bc34342012-05-16 08:32:49 +0000796 if (OptLevelO1 || OptLevelO2 || OptLevelOs || OptLevelOz || OptLevelO3) {
Chris Lattner3e8984a2011-05-22 06:44:19 +0000797 FPasses->doInitialization();
Chris Lattner52b28892011-05-22 00:21:33 +0000798 for (Module::iterator F = M->begin(), E = M->end(); F != E; ++F)
799 FPasses->run(*F);
Chris Lattner3e8984a2011-05-22 06:44:19 +0000800 FPasses->doFinalization();
801 }
Chris Lattner61db1a12009-10-22 00:46:41 +0000802
803 // Check that the module is well formed on completion of optimization
804 if (!NoVerify && !VerifyEach)
805 Passes.add(createVerifierPass());
806
Dan Gohman4931b312010-08-18 17:42:59 +0000807 // Write bitcode or assembly to the output as the last step...
Chris Lattner61db1a12009-10-22 00:46:41 +0000808 if (!NoOutput && !AnalyzeOnly) {
809 if (OutputAssembly)
Dan Gohmand4c45432010-09-01 14:20:41 +0000810 Passes.add(createPrintModulePass(&Out->os()));
Chris Lattner61db1a12009-10-22 00:46:41 +0000811 else
Dan Gohmand4c45432010-09-01 14:20:41 +0000812 Passes.add(createBitcodeWriterPass(Out->os()));
Chris Lattner61db1a12009-10-22 00:46:41 +0000813 }
814
Andrew Trickce969022011-04-05 18:54:36 +0000815 // Before executing passes, print the final values of the LLVM options.
816 cl::PrintOptionValues();
817
Chris Lattner61db1a12009-10-22 00:46:41 +0000818 // Now that we have all of the passes ready, run them.
819 Passes.run(*M.get());
820
Dan Gohmand5826a32010-08-20 01:07:01 +0000821 // Declare success.
Devang Patelf5f23002010-12-07 00:33:43 +0000822 if (!NoOutput || PrintBreakpoints)
Dan Gohmand5826a32010-08-20 01:07:01 +0000823 Out->keep();
824
Chris Lattner61db1a12009-10-22 00:46:41 +0000825 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000826}